Commit f9cf751
Don't destroy Python objects under the JIT compilation lock
Summary:
I ran the AI Labs test in D115743871 and got an error and the following stack trace (just relevant parts shown below):
```
9133 | Stack trace for 1 thread(s) [8782 cinderx_compile]:
9139 | PyEval_AcquireThread ← blocked waiting for the GIL
9140 | pybind11::gil_scoped_acquire() ← explicitly asking for it
9142 | c10::TensorImpl::decref_pyobject() ← must drop a Python ref
9145 | c10::TensorImpl::~TensorImpl() ← a torch Tensor being freed
9153 | _PyObject_FreeInstanceAttributes
9159 | allocator_traits<pair<CompilationKey, ...Ref<PyFunctionObject>>>::destroy
9160 | cinderx::jit::Context::finalizeMultiThreadedCompile() ← holds the mutex
9818 | Stack trace for 1 thread(s) [8772 icvr_launcher_m]:
9819 | __GI___lll_lock_wait ← blocked, untimed
9822 | pthread_mutex_lock
9824 | cinderx::jit::Context::funcDestroyed(...) ← wants the mutex
9835 | Python stack trace for this thread (holds GIL)
9840 | torch/_inductor/compile_fx.py:773 _recursive_post_grad_passes
```
These logs are from the `watcher.cpp` tool, which prints all the stacktraces of all current threads when it sees that the GIL is being held for a while.
It seems that there's a deadlock with `funcDestroyed()` having the GIL and waiting on `JITCompilationLock` while `finalizeMultiThreadedCompile()` has `JITCompilationLock` and is waiting on the GIL in order to do deallocations.
The fix here makes it so that the deallocations in `finalizeMultiThreadedCompile()` happen outside the `JITCompilationLock`.
Reviewed By: DinoV
Differential Revision: D116940812
fbshipit-source-id: d9ed1c346a0b36e3cf32e960148a3685cb1a24ce1 parent 7de3591 commit f9cf751
1 file changed
Lines changed: 26 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
482 | 482 | | |
483 | 483 | | |
484 | 484 | | |
485 | | - | |
486 | | - | |
487 | | - | |
488 | | - | |
489 | | - | |
490 | | - | |
491 | | - | |
492 | | - | |
493 | | - | |
494 | | - | |
495 | | - | |
496 | | - | |
497 | | - | |
498 | | - | |
499 | | - | |
500 | | - | |
501 | | - | |
502 | | - | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
503 | 510 | | |
| 511 | + | |
504 | 512 | | |
505 | | - | |
506 | 513 | | |
507 | 514 | | |
508 | 515 | | |
| |||
0 commit comments