Open yingchingl opened 3 months ago
Additional Debugging
When running the test program with PYTHONMALLOC=debug
environment variable, Python will check the status of the GIL when performing memory allocation and deallocation:
PYTHONMALLOC=debug python sdxl_onediff_test.py
The captured stack trace is as follows:
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1 0x000072fcf0ac7859 in __GI_abort () at abort.c:79
#2 0x00000000004a6ef3 in fatal_error_exit (status=<error reading variable: dwarf2_find_location_expression: Corrupted DWARF expression.>) at /usr/local/src/conda/python-3.10.13/Python/pylifecycle.c:2556
#3 fatal_error (fd=<optimized out>, header=header@entry=1, prefix=prefix@entry=0x6b6a90 <__func__.5.lto_priv.0> "_PyMem_DebugFree", msg=msg@entry=0x627e68 "Python memory allocator called without holding the GIL", status=status@entry=-1) at /usr/local/src/conda/python-3.10.13/Python/pylifecycle.c:2667
#4 0x00000000004a727d in _Py_FatalErrorFunc (func=func@entry=0x6b6a90 <__func__.5.lto_priv.0> "_PyMem_DebugFree", msg=msg@entry=0x627e68 "Python memory allocator called without holding the GIL") at /usr/local/src/conda/python-3.10.13/Python/pylifecycle.c:2753
#5 0x0000000000423122 in _PyMem_DebugCheckGIL (func=<error reading variable: dwarf2_find_location_expression: Corrupted DWARF expression.>) at /usr/local/src/conda/python-3.10.13/Objects/obmalloc.c:2646
#6 0x00000000004dd024 in _PyMem_DebugFree (ctx=0x743040 <_PyMem_Debug+96>, ptr=0x72f37c77f4c0) at /usr/local/src/conda/python-3.10.13/Objects/obmalloc.c:2670
#7 0x000072f57c11946c in ?? () from /opt/conda/lib/python3.10/site-packages/oneflow/_oneflow_internal.cpython-310-x86_64-linux-gnu.so
#8 0x000072f45e27ec0d in std::_Sp_counted_ptr_inplace<oneflow::one::LocalTensor, std::allocator<oneflow::one::LocalTensor>, (__gnu_cxx::_Lock_policy)2>::_M_dispose() () from /opt/conda/lib/python3.10/site-packages/oneflow/../oneflow.libs/liboneflow-d0782696.so
#9 0x000072f57c097a1a in oneflow::deleter(DLManagedTensor*) () from /opt/conda/lib/python3.10/site-packages/oneflow/_oneflow_internal.cpython-310-x86_64-linux-gnu.so
#10 0x000072fc54ddb28c in c10::deleteInefficientStdFunctionContext(void*) () from /opt/conda/lib/python3.10/site-packages/torch/lib/libc10.so
#11 0x000072fcbf9bb7b6 in c10::StorageImpl::~StorageImpl() () from /opt/conda/lib/python3.10/site-packages/torch/lib/libtorch_python.so
#12 0x000072fc54e02ca7 in c10::intrusive_ptr<c10::StorageImpl, c10::detail::intrusive_target_default_null_type<c10::StorageImpl> >::reset_() () from /opt/conda/lib/python3.10/site-packages/torch/lib/libc10.so
#13 0x000072fc54dfacb3 in c10::TensorImpl::~TensorImpl() () from /opt/conda/lib/python3.10/site-packages/torch/lib/libc10.so
#14 0x000072fc54dfae49 in c10::TensorImpl::~TensorImpl() () from /opt/conda/lib/python3.10/site-packages/torch/lib/libc10.so
#15 0x000072fcbfc6b738 in THPVariable_clear(THPVariable*) () from /opt/conda/lib/python3.10/site-packages/torch/lib/libtorch_python.so
#16 0x000072fcbfc6bac5 in THPVariable_subclass_dealloc(_object*) () from /opt/conda/lib/python3.10/site-packages/torch/lib/libtorch_python.so
#17 0x00000000004dd8e4 in _Py_Dealloc (op=<error reading variable: dwarf2_find_location_expression: Corrupted DWARF expression.>) at /usr/local/src/conda/python-3.10.13/Objects/object.c:2295
#18 _Py_DECREF (op=<error reading variable: dwarf2_find_location_expression: Corrupted DWARF expression.>) at /usr/local/src/conda/python-3.10.13/Include/object.h:500
#19 _Py_XDECREF (op=<error reading variable: dwarf2_find_location_expression: Corrupted DWARF expression.>) at /usr/local/src/conda/python-3.10.13/Include/object.h:567
#20 list_dealloc (op=0x72f37c78b200) at /usr/local/src/conda/python-3.10.13/Objects/listobject.c:355
#21 0x00000000004f1820 in _Py_Dealloc (op=<error reading variable: dwarf2_find_location_expression: Corrupted DWARF expression.>) at /usr/local/src/conda/python-3.10.13/Objects/object.c:2295
#22 _Py_DECREF (op=<optimized out>) at /usr/local/src/conda/python-3.10.13/Include/object.h:500
#23 _Py_XDECREF (op=<optimized out>) at /usr/local/src/conda/python-3.10.13/Include/object.h:567
#24 _PyEval_EvalFrameDefault (tstate=<optimized out>, f=Frame 0x6c043530, for file /opt/conda/lib/python3.10/site-packages/diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.py, line 1100, in __call__ (self=<StableDiffusionXLControlNetPipeline(_internal_dict=<FrozenDict(vae=('diffusers', 'AutoencoderKL'), text_encoder=('onediff', 'MixedOneflowDeployableModule'), text_encoder_2=('onediff', 'MixedOneflowDeployableModule'), tokenizer=('transformers', 'CLIPTokenizer'), tokenizer_2=('transformers', 'CLIPTokenizer'), unet=('onediff', 'MixedOneflowDeployableModule'), controlnet=('onediff', 'MixedOneflowDeployableModule'), scheduler=('diffusers', 'EulerDiscreteScheduler'), force_zeros_for_empty_prompt=True, _name_or_path='stabilityai/stable-diffusion-xl-base-1.0', _FrozenDict__frozen=True) at remote 0x72f8b83427b0>, vae=<AutoencoderKL(_internal_dict=<FrozenDict(in_channels=3, out_channels=3, down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D', 'DownEncoderBlock2D', 'DownEncoderBlock2D'], up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D', 'UpDecoderBloc...(truncated), throwflag=<optimized out>) at /usr/local/src/conda/python-3.10.13/Python/ceval.c:1882
This reveals that the Python memory allocator is being called without holding the GIL.
Additional Debugging
When running the test program with
PYTHONMALLOC=debug
environment variable, Python will check the status of the GIL when performing memory allocation and deallocation:PYTHONMALLOC=debug python sdxl_onediff_test.py
The captured stack trace is as follows:
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50 #1 0x000072fcf0ac7859 in __GI_abort () at abort.c:79 #2 0x00000000004a6ef3 in fatal_error_exit (status=<error reading variable: dwarf2_find_location_expression: Corrupted DWARF expression.>) at /usr/local/src/conda/python-3.10.13/Python/pylifecycle.c:2556 #3 fatal_error (fd=<optimized out>, header=header@entry=1, prefix=prefix@entry=0x6b6a90 <__func__.5.lto_priv.0> "_PyMem_DebugFree", msg=msg@entry=0x627e68 "Python memory allocator called without holding the GIL", status=status@entry=-1) at /usr/local/src/conda/python-3.10.13/Python/pylifecycle.c:2667 #4 0x00000000004a727d in _Py_FatalErrorFunc (func=func@entry=0x6b6a90 <__func__.5.lto_priv.0> "_PyMem_DebugFree", msg=msg@entry=0x627e68 "Python memory allocator called without holding the GIL") at /usr/local/src/conda/python-3.10.13/Python/pylifecycle.c:2753 #5 0x0000000000423122 in _PyMem_DebugCheckGIL (func=<error reading variable: dwarf2_find_location_expression: Corrupted DWARF expression.>) at /usr/local/src/conda/python-3.10.13/Objects/obmalloc.c:2646 #6 0x00000000004dd024 in _PyMem_DebugFree (ctx=0x743040 <_PyMem_Debug+96>, ptr=0x72f37c77f4c0) at /usr/local/src/conda/python-3.10.13/Objects/obmalloc.c:2670 #7 0x000072f57c11946c in ?? () from /opt/conda/lib/python3.10/site-packages/oneflow/_oneflow_internal.cpython-310-x86_64-linux-gnu.so #8 0x000072f45e27ec0d in std::_Sp_counted_ptr_inplace<oneflow::one::LocalTensor, std::allocator<oneflow::one::LocalTensor>, (__gnu_cxx::_Lock_policy)2>::_M_dispose() () from /opt/conda/lib/python3.10/site-packages/oneflow/../oneflow.libs/liboneflow-d0782696.so #9 0x000072f57c097a1a in oneflow::deleter(DLManagedTensor*) () from /opt/conda/lib/python3.10/site-packages/oneflow/_oneflow_internal.cpython-310-x86_64-linux-gnu.so #10 0x000072fc54ddb28c in c10::deleteInefficientStdFunctionContext(void*) () from /opt/conda/lib/python3.10/site-packages/torch/lib/libc10.so #11 0x000072fcbf9bb7b6 in c10::StorageImpl::~StorageImpl() () from /opt/conda/lib/python3.10/site-packages/torch/lib/libtorch_python.so #12 0x000072fc54e02ca7 in c10::intrusive_ptr<c10::StorageImpl, c10::detail::intrusive_target_default_null_type<c10::StorageImpl> >::reset_() () from /opt/conda/lib/python3.10/site-packages/torch/lib/libc10.so #13 0x000072fc54dfacb3 in c10::TensorImpl::~TensorImpl() () from /opt/conda/lib/python3.10/site-packages/torch/lib/libc10.so #14 0x000072fc54dfae49 in c10::TensorImpl::~TensorImpl() () from /opt/conda/lib/python3.10/site-packages/torch/lib/libc10.so #15 0x000072fcbfc6b738 in THPVariable_clear(THPVariable*) () from /opt/conda/lib/python3.10/site-packages/torch/lib/libtorch_python.so #16 0x000072fcbfc6bac5 in THPVariable_subclass_dealloc(_object*) () from /opt/conda/lib/python3.10/site-packages/torch/lib/libtorch_python.so #17 0x00000000004dd8e4 in _Py_Dealloc (op=<error reading variable: dwarf2_find_location_expression: Corrupted DWARF expression.>) at /usr/local/src/conda/python-3.10.13/Objects/object.c:2295 #18 _Py_DECREF (op=<error reading variable: dwarf2_find_location_expression: Corrupted DWARF expression.>) at /usr/local/src/conda/python-3.10.13/Include/object.h:500 #19 _Py_XDECREF (op=<error reading variable: dwarf2_find_location_expression: Corrupted DWARF expression.>) at /usr/local/src/conda/python-3.10.13/Include/object.h:567 #20 list_dealloc (op=0x72f37c78b200) at /usr/local/src/conda/python-3.10.13/Objects/listobject.c:355 #21 0x00000000004f1820 in _Py_Dealloc (op=<error reading variable: dwarf2_find_location_expression: Corrupted DWARF expression.>) at /usr/local/src/conda/python-3.10.13/Objects/object.c:2295 #22 _Py_DECREF (op=<optimized out>) at /usr/local/src/conda/python-3.10.13/Include/object.h:500 #23 _Py_XDECREF (op=<optimized out>) at /usr/local/src/conda/python-3.10.13/Include/object.h:567 #24 _PyEval_EvalFrameDefault (tstate=<optimized out>, f=Frame 0x6c043530, for file /opt/conda/lib/python3.10/site-packages/diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.py, line 1100, in __call__ (self=<StableDiffusionXLControlNetPipeline(_internal_dict=<FrozenDict(vae=('diffusers', 'AutoencoderKL'), text_encoder=('onediff', 'MixedOneflowDeployableModule'), text_encoder_2=('onediff', 'MixedOneflowDeployableModule'), tokenizer=('transformers', 'CLIPTokenizer'), tokenizer_2=('transformers', 'CLIPTokenizer'), unet=('onediff', 'MixedOneflowDeployableModule'), controlnet=('onediff', 'MixedOneflowDeployableModule'), scheduler=('diffusers', 'EulerDiscreteScheduler'), force_zeros_for_empty_prompt=True, _name_or_path='stabilityai/stable-diffusion-xl-base-1.0', _FrozenDict__frozen=True) at remote 0x72f8b83427b0>, vae=<AutoencoderKL(_internal_dict=<FrozenDict(in_channels=3, out_channels=3, down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D', 'DownEncoderBlock2D', 'DownEncoderBlock2D'], up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D', 'UpDecoderBloc...(truncated), throwflag=<optimized out>) at /usr/local/src/conda/python-3.10.13/Python/ceval.c:1882
This reveals that the Python memory allocator is being called without holding the GIL.
Hi @yingchingl Were you able to solve it? Would you mind kindly to give some advice about it?
@strint Would you mind to give a loot at the issue ? Fatal Python error: _PyMem_DebugFree: Python memory allocator called without holding the GIL
Deeply grateful!
Stack trace (most recent call last) in thread 16004:
Object "/home/admin/miniconda3/lib/python3.9/site-packages/oneflow/_oneflow_internal.cpython-39-x86_64-linux-gnu.so", at 0x7fa7f3eec1d9, in deleter(DLManagedTensor*)
Object "/home/admin/miniconda3/lib/python3.9/site-packages/oneflow/../oneflow.libs/liboneflow-6e5725f6.so", at 0x7fa6fd00b29c, in std::_Sp_counted_ptr_inplace<LocalTensor, std::allocator<LocalTensor>, (__gnu_cxx::_Lock_policy)2>::_M_dispose()
Object "/home/admin/miniconda3/lib/python3.9/site-packages/oneflow/_oneflow_internal.cpython-39-x86_64-linux-gnu.so", at 0x7fa7f3f6e1eb, in
@hjchen2 Would you mind to give a look at the issue
Hi @yingchingl Were you able to solve it? Would you mind kindly to give some advice about it?
Hi @bigmover , No, I can't solve it yet. Because the code of oneflow used by onediff is not public, I cannot debug into it and see where the problem occurs.
Multi-threaded Python Program
Please don't try to use cuda/python/onediff with Multi-thread because it's usually blocked by the Python GIL or not thread-safe.
Using multi-process is recommended.
Hi @yingchingl Were you able to solve it? Would you mind kindly to give some advice about it?
Hi @bigmover , No, I can't solve it yet. Because the code of oneflow used by onediff is not public, I cannot debug into it and see where the problem occurs.
GOT IT! Thank you!
Multi-threaded Python Program
Please don't try to use cuda/python/onediff with Multi-thread because it's usually blocked by the Python GIL or not thread-safe.
Using multi-process is recommended.
@strint OK Thank you for your kindly reply! Onediff have flash attention
? It will use more GPU memory when using oneflow.compile?
Multi-threaded Python Program
Please don't try to use cuda/python/onediff with Multi-thread because it's usually blocked by the Python GIL or not thread-safe. Using multi-process is recommended.
@strint OK Thank you for your kindly reply! Onediff have
flash attention
? It will use more GPU memory when using oneflow.compile?
Yes, we are using a customized flash attention.
Compileing UNet won't use more GPU memory, but compiling VAE does because VAE has a large output tensor. oneflow compile share model weight memory with torch but doesn't share output tensor memory with torch.
Hi @bigmover @yingchingl , I have the same problem, I want to ask if you have any solution or idea.
Your current environment information
Collecting environment information... PyTorch version: 2.1.1+cu118 Is debug build: False CUDA used to build PyTorch: 11.8 ROCM used to build PyTorch: N/A
OneFlow version: path: ['/opt/conda/lib/python3.10/site-packages/oneflow'], version: 0.9.1.dev20240515+cu118, git_commit: ec7b682, cmake_build_type: Release, rdma: True, mlir: True, enterprise: False Nexfort version: none OneDiff version: 1.2.0.dev202407160130 OneDiffX version: 1.2.0.dev1
OS: Ubuntu 20.04.6 LTS (x86_64) GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0 Clang version: Could not collect CMake version: version 3.28.1 Libc version: glibc-2.31
Python version: 3.10.13 (main, Sep 11 2023, 13:44:35) [GCC 11.2.0] (64-bit runtime) Python platform: Linux-5.15.0-97-generic-x86_64-with-glibc2.31 Is CUDA available: True CUDA runtime version: 11.8.89 CUDA_MODULE_LOADING set to: LAZY GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3090 Nvidia driver version: 535.161.07 cuDNN version: Probably one of the following: /usr/lib/x86_64-linux-gnu/libcudnn.so.8.9.0 /usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.9.0 /usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.9.0 /usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.9.0 /usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.9.0 /usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.9.0 /usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.9.0 HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: True
CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian Address sizes: 46 bits physical, 57 bits virtual CPU(s): 112 On-line CPU(s) list: 0-111 Thread(s) per core: 2 Core(s) per socket: 28 Socket(s): 2 NUMA node(s): 2 Vendor ID: GenuineIntel CPU family: 6 Model: 106 Model name: Intel(R) Xeon(R) Gold 6330 CPU @ 2.00GHz Stepping: 6 CPU MHz: 800.000 CPU max MHz: 3100.0000 CPU min MHz: 800.0000 BogoMIPS: 4000.00 Virtualization: VT-x L1d cache: 2.6 MiB L1i cache: 1.8 MiB L2 cache: 70 MiB L3 cache: 84 MiB NUMA node0 CPU(s): 0-27,56-83 NUMA node1 CPU(s): 28-55,84-111 Vulnerability Gather data sampling: Mitigation; Microcode Vulnerability Itlb multihit: Not affected Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable Vulnerability Retbleed: Not affected Vulnerability Spec rstack overflow: Not affected Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization Vulnerability Spectre v2: Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence Vulnerability Srbds: Not affected Vulnerability Tsx async abort: Not affected Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect wbnoinvd dtherm ida arat pln pts avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid fsrm md_clear pconfig flush_l1d arch_capabilities
Versions of relevant libraries: [pip3] diffusers==0.21.0 [pip3] numpy==1.26.0 [pip3] onnx==1.15.0 [pip3] onnxruntime-gpu==1.17.0 [pip3] torch==2.1.1+cu118 [pip3] torchaudio==2.1.1+cu118 [pip3] torchelastic==0.2.2 [pip3] torchsde==0.2.6 [pip3] torchvision==0.16.1+cu118 [pip3] transformers==4.35.2 [pip3] triton==2.1.0 [conda] blas 1.0 mkl [conda] ffmpeg 4.3 hf484d3e_0 pytorch [conda] libjpeg-turbo 2.0.0 h9bf148f_0 pytorch [conda] mkl 2023.1.0 h213fc3f_46343 [conda] mkl-service 2.4.0 py310h5eee18b_1 [conda] mkl_fft 1.3.8 py310h5eee18b_0 [conda] mkl_random 1.2.4 py310hdb19cb5_0 [conda] numpy 1.26.0 py310h5f9d8c6_0 [conda] numpy-base 1.26.0 py310hb5e798b_0 [conda] pytorch-cuda 11.8 h7e8668a_5 pytorch [conda] pytorch-mutex 1.0 cuda pytorch [conda] torch 2.1.1+cu118 pypi_0 pypi [conda] torchaudio 2.1.1+cu118 pypi_0 pypi [conda] torchelastic 0.2.2 pypi_0 pypi [conda] torchsde 0.2.6 pypi_0 pypi [conda] torchtriton 2.1.0 py310 pytorch [conda] torchvision 0.16.1+cu118 pypi_0 pypi
🐛 Describe the bug
I'm experiencing random segmentation faults when using OneDiff to accelerate SDXL inference in a multi-threaded Python environment. The issue appears to be related to memory allocation and deallocation without properly handling the Python Global Interpreter Lock (GIL).
When the issue occurs, the command window outputs one of the following messages:
Since there isn't much information, I used GDB to debug and captured the following stack trace when the segmentation fault occurs:
The program consistently crashes in the
pymalloc_alloc
function, trying to access an invalid memory address. I suspect the issue may be related to the handling of the Python Global Interpreter Lock (GIL) during memory allocation and deallocation processes. To verify this hypothesis, I've created a test program (sdxl_onediff_test.py
) based on the examples of Diffusers and OneDiffX that simulates using OneFlow for accelerated inference while another thread performs Python memory allocation and deallocation:To reproduce:
python sdxl_onediff_test.py --compile_and_save
to compile and save the compiled pipe.python sdxl_onediff_test.py
to start the test.The segmentation fault typically occurs within a few seconds to minutes.