Repository navigation
[None][fix] Runtime fixes found while bringing up Mistral Large 4 #21788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
816cb57
[None][fix] FlashMLA: release the GIL in the pybind launch wrappers
yizhang-nv b141beb
[None][fix] Attention DP: give the padding dummy a cache in the paire…
yizhang-nv daead92
[None][fix] Decoder warmup: run the context warmup beside a capturabl…
yizhang-nv 3f2deaf
[None][fix] Attention DP: report each rank's own speculative-decoding…
yizhang-nv d755419
[None][chore] FlashMLA: explain the GIL deadlock beside the bindings,…
yizhang-nv fa2a0ac
[None][chore] FlashMLA: ask for a re-check of the GIL release conditi…
yizhang-nv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| --- a/csrc/api/api.cpp | ||
| +++ b/csrc/api/api.cpp | ||
| @@ -7,9 +7,44 @@ | ||
|
|
||
| PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { | ||
| m.doc() = "FlashMLA"; | ||
| - m.def("sparse_decode_fwd", &sparse_attn_decode_interface); | ||
| - m.def("dense_decode_fwd", &dense_attn_decode_interface); | ||
| - m.def("sparse_prefill_fwd", &sparse_attn_prefill_interface); | ||
| - m.def("dense_prefill_fwd", &FMHACutlassSM100FwdRun); | ||
| - m.def("dense_prefill_bwd", &FMHACutlassSM100BwdRun); | ||
| + // Every binding releases the GIL for the duration of its C++ call. | ||
| + // | ||
| + // A binding that kept the GIL could deadlock a process that also queues Python host functions on a | ||
| + // CUDA stream, as TensorRT-LLM does. What is seen then: under load, the thread that runs the model | ||
| + // never returns from a call into this module, and in a multi-rank deployment the other ranks stall | ||
| + // in their next collective. | ||
| + // | ||
| + // Three facts make the cycle: | ||
| + // 1. A kernel launch is normally asynchronous, but once the launch queue is full it blocks until | ||
| + // the device has worked through part of the queue. | ||
| + // 2. A host function queued with cudaLaunchHostFunc blocks the work queued behind it until it has | ||
| + // returned, and one that calls back into Python must take the GIL before it can run. | ||
| + // 3. pybind11 keeps the GIL for the whole call of a function bound with a plain m.def, also while | ||
| + // a launch inside it blocks. | ||
| + // So when a launch made from here blocks on a full queue that has a Python host function ahead of | ||
| + // it, the launch waits for the host function and the host function waits for the GIL. | ||
| + // | ||
| + // In TensorRT-LLM the host functions come from tensorrt_llm/_torch/hostfunc.py; the capturable | ||
| + // guided decoder (guided decoding beside one-model speculative decoding) queues them for every | ||
| + // step. Those captured into a CUDA graph run when a replay reaches them, which is after the Python | ||
| + // call that started the replay has returned, so they can still be pending when the next step runs | ||
| + // eagerly and calls into this module. Whether the launch queue is full at that moment depends on | ||
| + // the load, so the hang shows up under serving load and not on every request. | ||
| + // | ||
| + // Releasing the GIL is safe for the functions bound here: their arguments and results are tensors | ||
| + // and scalars, which pybind11 converts before the guard is constructed and after it is destroyed; | ||
| + // they never call into Python; and they keep no mutable state between calls, so calls from | ||
| + // different Python threads need no serialization by the GIL. A binding that takes or returns | ||
| + // Python objects, calls into Python, or shares mutable state must not get this guard. | ||
| + // | ||
| + // These conditions were checked for the FlashMLA commit pinned in 3rdparty/fetch_content.json, and | ||
| + // they are properties of that commit only. Whenever the pin moves, check them again for every | ||
| + // binding of the new version and for the functions they bind. The patch still applying does not | ||
| + // show that they hold: a binding added upstream gets no guard (and can deadlock as described above | ||
| + // if it launches kernels), and a change inside a bound function leaves this file untouched. | ||
| + m.def("sparse_decode_fwd", &sparse_attn_decode_interface, pybind11::call_guard<pybind11::gil_scoped_release>()); | ||
| + m.def("dense_decode_fwd", &dense_attn_decode_interface, pybind11::call_guard<pybind11::gil_scoped_release>()); | ||
| + m.def("sparse_prefill_fwd", &sparse_attn_prefill_interface, pybind11::call_guard<pybind11::gil_scoped_release>()); | ||
| + m.def("dense_prefill_fwd", &FMHACutlassSM100FwdRun, pybind11::call_guard<pybind11::gil_scoped_release>()); | ||
| + m.def("dense_prefill_bwd", &FMHACutlassSM100BwdRun, pybind11::call_guard<pybind11::gil_scoped_release>()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.