Repository navigation
[None][perf] Overlap TRTLLM-Gen FMHA JIT warmup compilation with engine warmup (opt-in) #18542
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
Open
aaronmeoded-b10
wants to merge
21
commits into
NVIDIA:main
Choose a base branch
from
aaronmeoded-b10:fmha-async-jit-warmup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,241
−19
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
62b9b93
[None][perf] Overlap TRTLLM-Gen FMHA JIT warmup compilation with engi…
aaronmeoded-b10 59821ba
[None][fix] Address review: per-device warmup registry, drain on kern…
aaronmeoded-b10 c32cc97
[None][chore] Apply clang-format
aaronmeoded-b10 b72ad82
[None][fix] Per-kernel-instance warmup registry isolation; leak regis…
aaronmeoded-b10 4fd977c
Merge branch 'main' of NVIDIA/TensorRT-LLM into fmha-async-jit-warmup
aaronmeoded-b10 68bcdce
[None][fix] Pre-capture drain-and-verify barrier, ordered worker shut…
aaronmeoded-b10 9bea9ea
[None][test] Cover the async FMHA JIT warmup: worker, registry, engin…
aaronmeoded-b10 ea0455c
[None][chore] Apply clang-format
aaronmeoded-b10 dc07ad4
[None][fix] Sync ranks after the JIT warmup drain; fix barrier test t…
aaronmeoded-b10 f7b99d3
[None][fix] Count reported cache misses; make worker shutdown safe fo…
aaronmeoded-b10 e1280c2
[None][chore] Pre-commit: restore upstream comment alignment, codespell
aaronmeoded-b10 0d7c229
Merge branch 'main' of NVIDIA/TensorRT-LLM into fmha-async-jit-warmup
aaronmeoded-b10 98d97bb
[None][fix] Run the JIT warmup barrier before the steady-state A2A bu…
aaronmeoded-b10 c425372
Merge branch 'main' into fmha-async-jit-warmup
aaronmeoded-b10 91f3057
Merge branch 'main' into fmha-async-jit-warmup
aaronmeoded-b10 80ac500
[None][test] Move FMHA warmup barrier test into pyexecutor
chienchunhung 3d5ab2e
Merge branch 'main' into fmha-async-jit-warmup
aaronmeoded-b10 788b63b
Merge branch 'main' into fmha-async-jit-warmup
aaronmeoded-b10 5e10ec1
[None][fix] Fix CI failures in async FMHA JIT warmup tests
aaronmeoded-b10 fd34cbe
[None][fix] Fix pre-commit formatting and missing mapping in barrier …
aaronmeoded-b10 e7624d0
[None][fix] Address review on the async FMHA JIT warmup
aaronmeoded-b10 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
459 changes: 459 additions & 0 deletions
459
cpp/tensorrt_llm/kernels/trtllmGenKernels/fmha/fmhaJitWarmup.h
Large diffs are not rendered by default.
Oops, something went wrong.
467 changes: 449 additions & 18 deletions
467
cpp/tensorrt_llm/kernels/trtllmGenKernels/fmha/fmhaKernels.h
Large diffs are not rendered by default.
Oops, something went wrong.
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,74 @@ | ||
| /* | ||
| * Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "tensorrt_llm/common/config.h" | ||
| #include "tensorrt_llm/kernels/trtllmGenKernels/fmha/fmhaKernels.h" | ||
|
|
||
| #include <torch/library.h> | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| TRTLLM_NAMESPACE_BEGIN | ||
|
|
||
| namespace torch_ext | ||
| { | ||
|
|
||
| // Waits for the background TRTLLM-Gen FMHA JIT warmup sweeps and re-runs each | ||
| // finished sweep once on the calling thread, so every kernel the warmup | ||
| // derived is compiled before CUDA graph capture and before the engine reports | ||
| // ready. Returns the number of sweeps verified here. No-op unless | ||
| // TRTLLM_GEN_FMHA_ASYNC_WARMUP=1. | ||
| int64_t trtllmGenFmhaJitWarmupDrainAndVerify() | ||
| { | ||
| return tensorrt_llm::kernels::TllmGenFmhaKernel::drainAndVerifyAllJITWarmups(); | ||
| } | ||
|
|
||
| // Number of TRTLLM-Gen FMHA kernel-cache misses (NVRTC compiles) the export | ||
| // library reported in this process so far. A value that does not change across | ||
| // a request proves that the request compiled nothing. | ||
| int64_t trtllmGenFmhaJitNumCacheMisses() | ||
| { | ||
| return tensorrt_llm::kernels::TllmGenFmhaKernel::numJITCacheMisses(); | ||
| } | ||
|
|
||
| // Number of compile requests whose cache result the export library did not | ||
| // report. Non-zero means the miss count above is not a complete measure. | ||
| int64_t trtllmGenFmhaJitNumUnknownCacheResults() | ||
| { | ||
| return tensorrt_llm::kernels::TllmGenFmhaKernel::numJITUnknownCacheResults(); | ||
| } | ||
|
|
||
| // Whether this process runs the TRTLLM-Gen FMHA JIT warmup on the background | ||
| // thread (TRTLLM_GEN_FMHA_ASYNC_WARMUP=1, read once per process). | ||
| bool trtllmGenFmhaAsyncJitWarmupEnabled() | ||
| { | ||
| return tensorrt_llm::kernels::asyncJITWarmupEnabled(); | ||
| } | ||
|
|
||
| } // namespace torch_ext | ||
|
|
||
| TRTLLM_NAMESPACE_END | ||
|
|
||
| TORCH_LIBRARY_FRAGMENT(trtllm, m) | ||
| { | ||
| m.def("trtllm_gen_fmha_jit_warmup_drain_and_verify() -> int", | ||
| &tensorrt_llm::torch_ext::trtllmGenFmhaJitWarmupDrainAndVerify); | ||
| m.def("trtllm_gen_fmha_jit_num_cache_misses() -> int", &tensorrt_llm::torch_ext::trtllmGenFmhaJitNumCacheMisses); | ||
| m.def("trtllm_gen_fmha_jit_num_unknown_cache_results() -> int", | ||
| &tensorrt_llm::torch_ext::trtllmGenFmhaJitNumUnknownCacheResults); | ||
| m.def("trtllm_gen_fmha_async_jit_warmup_enabled() -> bool", | ||
| &tensorrt_llm::torch_ext::trtllmGenFmhaAsyncJitWarmupEnabled); | ||
| } |
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
161 changes: 161 additions & 0 deletions
161
cpp/tests/unit_tests/kernels/fmhaJitWarmupFingerprintTest.cpp
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,161 @@ | ||
| /* | ||
| * Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| // Tests for TllmGenFmhaKernel::jitWarmupFingerprint(), the key that | ||
| // deduplicates warmup sweeps (in sync and async mode alike). Two sweeps that | ||
| // would compile different kernels must never share a fingerprint. No kernel is | ||
| // loaded or launched: the kernel objects here carry an empty kernel table. | ||
|
|
||
| #include "tensorrt_llm/kernels/trtllmGenKernels/fmha/fmhaKernels.h" | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <memory> | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| using tensorrt_llm::kernels::Data_type; | ||
| using tensorrt_llm::kernels::FmhaKernelType; | ||
| using tensorrt_llm::kernels::kSM_100; | ||
| using tensorrt_llm::kernels::TllmGenFmhaKernel; | ||
|
|
||
| using RunnerParams = TllmGenFmhaKernel::RunnerParams; | ||
|
|
||
| // Kernel objects are not copyable; hand them out on the heap. | ||
| std::unique_ptr<TllmGenFmhaKernel> makeKernel(Data_type dtypeOut = Data_type::DATA_TYPE_BF16) | ||
| { | ||
| return std::make_unique<TllmGenFmhaKernel>(/*pMetaStart=*/nullptr, /*nMetaCount=*/0, Data_type::DATA_TYPE_BF16, | ||
| Data_type::DATA_TYPE_BF16, Data_type::DATA_TYPE_BF16, dtypeOut, kSM_100); | ||
| } | ||
|
|
||
| // A generation-phase sweep request as the warmup forward issues it. | ||
| RunnerParams makeParams() | ||
| { | ||
| RunnerParams params{}; | ||
| params.mKernelType = FmhaKernelType::Generation; | ||
| params.mHeadDimQk = 128; | ||
| params.mHeadDimV = 128; | ||
| params.mNumHeadsQ = 32; | ||
| params.mNumHeadsKv = 8; | ||
| params.mNumHeadsQPerKv = 4; | ||
| params.mMaxSeqLenQ = 1; | ||
| params.mNumTokensPerPage = 64; | ||
| params.mMaxNumPagesPerSeqKv = 256; | ||
| params.mJITWarmupMaxNumRequests = 64; | ||
| params.mJITWarmupMaxSeqLenQ = 1; | ||
| params.mJITWarmupMaxSeqLenKv = 16384; | ||
| params.mMultiProcessorCount = 148; | ||
| return params; | ||
| } | ||
|
|
||
| TEST(TllmGenFmhaJitWarmupFingerprint, IsDeterministicForIdenticalRequests) | ||
| { | ||
| auto const kernel = makeKernel(); | ||
| auto const params = makeParams(); | ||
| EXPECT_EQ(kernel->jitWarmupFingerprint(params, 0), kernel->jitWarmupFingerprint(params, 0)); | ||
| // A second kernel object with the same configuration derives the same key. | ||
| EXPECT_EQ(makeKernel()->jitWarmupFingerprint(params, 0), kernel->jitWarmupFingerprint(params, 0)); | ||
| } | ||
|
|
||
| TEST(TllmGenFmhaJitWarmupFingerprint, IgnoresPerCallPointersAndStream) | ||
| { | ||
| auto const kernel = makeKernel(); | ||
| auto const base = makeParams(); | ||
| auto withPointers = base; | ||
| withPointers.stream = reinterpret_cast<cudaStream_t>(0x1); | ||
| withPointers.qPtr = reinterpret_cast<void const*>(0x2); | ||
| withPointers.kvPtr = reinterpret_cast<void const*>(0x3); | ||
| EXPECT_EQ(kernel->jitWarmupFingerprint(withPointers, 0), kernel->jitWarmupFingerprint(base, 0)); | ||
| } | ||
|
|
||
| TEST(TllmGenFmhaJitWarmupFingerprint, DistinguishesDevices) | ||
| { | ||
| auto const kernel = makeKernel(); | ||
| auto const params = makeParams(); | ||
| EXPECT_NE(kernel->jitWarmupFingerprint(params, 0), kernel->jitWarmupFingerprint(params, 1)); | ||
| } | ||
|
|
||
| TEST(TllmGenFmhaJitWarmupFingerprint, DistinguishesKernelObjectConfigurations) | ||
| { | ||
| auto const params = makeParams(); | ||
| EXPECT_NE(makeKernel(Data_type::DATA_TYPE_BF16)->jitWarmupFingerprint(params, 0), | ||
| makeKernel(Data_type::DATA_TYPE_E4M3)->jitWarmupFingerprint(params, 0)); | ||
| } | ||
|
|
||
| TEST(TllmGenFmhaJitWarmupFingerprint, DistinguishesKernelSelectingRunnerParams) | ||
| { | ||
| auto const kernel = makeKernel(); | ||
| auto const base = makeParams(); | ||
| auto const baseKey = kernel->jitWarmupFingerprint(base, 0); | ||
|
|
||
| // Eagle3 one-model: target layers (Q > 1, causal spec-decoding kernel) and | ||
| // draft layers (Q = 1) must get separate sweeps. | ||
| auto specTarget = base; | ||
| specTarget.mMaxSeqLenQ = 5; | ||
| EXPECT_NE(kernel->jitWarmupFingerprint(specTarget, 0), baseKey); | ||
|
|
||
| auto specTree = specTarget; | ||
| specTree.mIsSpecDecTree = true; | ||
| EXPECT_NE(kernel->jitWarmupFingerprint(specTree, 0), kernel->jitWarmupFingerprint(specTarget, 0)); | ||
|
|
||
| auto wideGrid = base; | ||
| wideGrid.mJITWarmupMaxSeqLenKv = base.mJITWarmupMaxSeqLenKv * 2; | ||
| EXPECT_NE(kernel->jitWarmupFingerprint(wideGrid, 0), baseKey); | ||
|
|
||
| auto moreHeads = base; | ||
| moreHeads.mNumHeadsQPerKv = 32; | ||
| EXPECT_NE(kernel->jitWarmupFingerprint(moreHeads, 0), baseKey); | ||
|
|
||
| auto sparse = base; | ||
| sparse.mSparseTopK = 2048; | ||
| EXPECT_NE(kernel->jitWarmupFingerprint(sparse, 0), baseKey); | ||
| } | ||
|
|
||
| TEST(TllmGenFmhaJitWarmupFingerprint, DistinguishesSlidingWindows) | ||
| { | ||
| auto const kernel = makeKernel(); | ||
| auto const base = makeParams(); | ||
| auto const baseKey = kernel->jitWarmupFingerprint(base, 0); | ||
|
|
||
| auto leftWindow = base; | ||
| leftWindow.mLeftSlidingWindow = 128; | ||
| EXPECT_NE(kernel->jitWarmupFingerprint(leftWindow, 0), baseKey); | ||
|
|
||
| auto rightWindow = base; | ||
| rightWindow.mRightSlidingWindow = 128; | ||
| EXPECT_NE(kernel->jitWarmupFingerprint(rightWindow, 0), baseKey); | ||
| EXPECT_NE(kernel->jitWarmupFingerprint(leftWindow, 0), kernel->jitWarmupFingerprint(rightWindow, 0)); | ||
| } | ||
|
|
||
| TEST(TllmGenFmhaJitWarmupFingerprint, DistinguishesDsv4EpilogueFusion) | ||
| { | ||
| // The fused epilogue selects a different kernel (and forces an E4M3 | ||
| // output), so a fused and a non-fused sweep with otherwise identical | ||
| // params, on an E4M3-output kernel object, must not deduplicate to one. | ||
| auto const kernel = makeKernel(Data_type::DATA_TYPE_E4M3); | ||
| auto const unfused = makeParams(); | ||
| auto fused = unfused; | ||
| fused.mDsv4EpilogueFusion.enabled = true; | ||
| EXPECT_NE(kernel->jitWarmupFingerprint(fused, 0), kernel->jitWarmupFingerprint(unfused, 0)); | ||
|
|
||
| // The fusion's per-call buffers do not take part. | ||
| auto fusedWithBuffers = fused; | ||
| fusedWithBuffers.mDsv4EpilogueFusion.cosSinCache = reinterpret_cast<float const*>(0x4); | ||
| EXPECT_EQ(kernel->jitWarmupFingerprint(fusedWithBuffers, 0), kernel->jitWarmupFingerprint(fused, 0)); | ||
| } | ||
|
|
||
| } // namespace |
91 changes: 91 additions & 0 deletions
91
cpp/tests/unit_tests/kernels/fmhaJitWarmupProcessExitTest.cpp
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,91 @@ | ||
| /* | ||
| * Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| // Process-exit test for the asynchronous TRTLLM-Gen FMHA JIT warmup worker, run | ||
| // in a child process (gtest death test). The worker is created first, through | ||
| // the pre-capture barrier with nothing queued, before the install-path string | ||
| // or any other sweep dependency has been touched; work is queued afterwards | ||
| // and is still pending when the process exits. The child exits 0 only if that | ||
| // pending work ran to completion during exit and observed the same install path | ||
| // the main thread saw, i.e. the worker was shut down with its dependencies | ||
| // intact regardless of which path created it. | ||
|
|
||
| #include "tensorrt_llm/kernels/trtllmGenKernels/fmha/fmhaKernels.h" | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <atomic> | ||
| #include <chrono> | ||
| #include <cstdlib> | ||
| #include <string> | ||
| #include <thread> | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| using tensorrt_llm::kernels::asyncJITWarmupWorkerCreated; | ||
| using tensorrt_llm::kernels::getAsyncJITWarmupWorker; | ||
| using tensorrt_llm::kernels::TllmGenFmhaKernel; | ||
|
|
||
| std::atomic<bool> gTaskFinished{false}; | ||
| std::atomic<bool> gTaskSawSameExecPath{false}; | ||
| std::string gMainThreadExecPath; | ||
|
|
||
| [[noreturn]] void runChild() | ||
| { | ||
| setenv("TRTLLM_GEN_FMHA_ASYNC_WARMUP", "1", 1); | ||
|
|
||
| // Registered first, so it runs last, after the worker's own atexit shutdown. | ||
| std::atexit( | ||
| []() | ||
| { | ||
| bool const ok = gTaskFinished.load() && gTaskSawSameExecPath.load(); | ||
| std::_Exit(ok ? 0 : 42); | ||
| }); | ||
|
|
||
| // "Empty barrier": nothing has been queued and the install path has never | ||
| // been computed. This must not create a worker whose shutdown ordering | ||
| // depends on static objects that do not exist yet. | ||
| EXPECT_EQ(TllmGenFmhaKernel::drainAndVerifyAllJITWarmups(), 0); | ||
| EXPECT_FALSE(asyncJITWarmupWorkerCreated().load()); | ||
|
|
||
| // Create the worker explicitly before the install path exists, which is the | ||
| // worst-case creation order, then compute the path on this thread. | ||
| auto& worker = getAsyncJITWarmupWorker(); | ||
| EXPECT_TRUE(asyncJITWarmupWorkerCreated().load()); | ||
| gMainThreadExecPath = TllmGenFmhaKernel::getExecPath(); | ||
|
|
||
| // Queue work that is still pending when exit() starts. | ||
| bool const queued = worker.enqueue( | ||
| []() | ||
| { | ||
| std::this_thread::sleep_for(std::chrono::milliseconds(300)); | ||
| gTaskSawSameExecPath = (TllmGenFmhaKernel::getExecPath() == gMainThreadExecPath); | ||
| gTaskFinished = true; | ||
| }); | ||
| EXPECT_TRUE(queued); | ||
| EXPECT_EQ(worker.numPendingTasks(), 1); | ||
|
|
||
| // Normal process exit: atexit handlers and static destruction run. | ||
| std::exit(0); | ||
| } | ||
|
|
||
| TEST(TllmGenFmhaAsyncJITWarmupProcessExit, PendingWorkFinishesWithDependenciesAliveAtExit) | ||
| { | ||
| EXPECT_EXIT(runChild(), ::testing::ExitedWithCode(0), ""); | ||
| } | ||
|
|
||
| } // namespace |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fmhaJitWarmupTestis labelledhostwith nogpulabel. Please confirm a CPP stage actually schedules host-only labelled tests viactest -L; if the selection isgpu-driven, this test is built but never run.