Skip to content

[TRTLLM-17679][perf] Autotuner: one build per kernel across shapes; opt-in call-wide PARALLEL plan - #22917

Draft
chenfeiz0326 wants to merge 34 commits into
NVIDIA:mainfrom
chenfeiz0326:chenfeiz/kb-build-tune-sched
Draft

chenfeiz0326 wants to merge 34 commits into
NVIDIA:mainfrom
chenfeiz0326:chenfeiz/kb-build-tune-sched

Conversation

@chenfeiz0326

@chenfeiz0326 chenfeiz0326 commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Stacked on #22374. Review only the last 3 commits; the others are #22374's.

  • 3b953644 Build pool: a build another rank claimed or made takes no worker. The pool settles it when the build is asked for, and again while it waits for a worker or for GPU room.
  • 836eff14 Autotuner: one build per kernel across profiles, claimed per kernel. TunableRunner.build_key(inputs, profile, tactic) names what a build compiles; the default is one build per profile and tactic. Tune mode builds each key once, on the first profile that needs it. Under SHARED, the ranks claim each build by its key. The CuTe DSL MoE gather and finalize runners and the MLA decode runner return their kernel_cache key, so a kernel that serves several shape buckets, or every expert-parallel rank, is built once.
  • 5b528357 Autotuner: an opt-in PARALLEL plan over all profiles of a call. TuningConfig.parallel_plan="call", or TLLM_AUTOTUNER_PARALLEL_PLAN=call for every op, plans the (profile, runner, tactic) pairs of a choose_one call across the tuning group at once: the largest first, each to the least loaded rank. The default, "profile", deals each profile on its own, as before.

Results

GB300, one allocation per case, 2 runs per arm. Startup is worker_submit_to_ready on the slowest rank, in seconds; before is #22374 at 7644f8b.

Case Before After After, TLLM_AUTOTUNER_PARALLEL_PLAN=call
MiniMax-M3 ctx_only TP2 1,059, 1,061 1,058, 1,057 1,058, 1,051
Kimi-K3 gen_only Helix8 563, 581 588, 584 588, 601
  • No change beyond run-to-run variation. The Kimi-K3 gen server's checkpoint load varies by ±40 s. Its autotuner step takes about 22 s or about 45 s in every arm. In a rerun that alternated before and after, before took 44.2, 21.5 and 21.5 s, and after took 21.9, 21.6 and 21.3 s.
  • Neither case has the work these changes save. Kimi-K3 tunes each MoE GEMM for one row bucket per call, inside the fused MoE op's tuning, and neither case tunes several shapes per call under PARALLEL on more than 2 ranks.

Test Coverage

PR Checklist

  • Please check this after reviewing the above items as appropriate for this PR.

🤖 Generated with Claude Code

KimiLinearModel._prewarm_k3_moe_front_kernels compiled the sort-routing
kernels for self.num_experts, which KimiLinearModel does not have, and
kimi_k3_routing.precompile passed the current device's index to
supports_device, which reads a torch.device's type. Either AttributeError
ended prewarm_decode_kernels there (DecoderWarmup logs it and goes on),
so the sort-routing, decode attention-residual and FP8 low-M GEMM kernels
compiled in the first forwards that reached them instead. Take the
routed-expert count from the MoE runtime, as its forward does, and check
the device by index, as the router-logits prewarm does.

Signed-off-by: chenfeiz0326 <[email protected]>
… on per-rank worker processes

The memory-profiling warmup of each rank builds JIT kernels in parallel on
worker processes (RankWorker), which exist only inside that build pass. A
warmup with the final KV cache starts none and builds as before.

- KernelBuilder, for ops that are not autotuned: in build mode an op's calls
  list the kernels for the shapes its BuildConfig sweeps, for the workers to
  build; in inference mode nothing is listed. A kernel the op needs before a
  worker has started its build is built by the op itself.
- Autotuner tune mode, inside the build pass: for an op that sets
  build_in_workers, the workers build every (profile, runner, tactic) before
  the main process times them.
- process_built: CuTe DSL compile functions whose kernels a worker builds
  into the rank's store, which the rank then loads; with TLLM_JIT_SHARE_DIR
  set, the store is shared and the ranks claim builds.
- MiniMax-M3's MSA attention variants build on the workers.

A rank starts at most 8 workers by default, and only while its GPU keeps
8 GiB free. TLLM_KERNEL_BUILDER=0 restores main's behavior.

Signed-off-by: chenfeiz0326 <[email protected]>
…ore cannot be made

- run() and submit() make the pool, and with it the store, before they
  register a build: a store that cannot be made turns the workers off and
  leaves every build to the rank, instead of a pass that waits for a build no
  worker received.
- Shared kernel caches keep CuTe DSL kernels in the store the workers build
  into, from the pass's first build to its end; in a worker process, in the
  store of its rank. autotune() does not share kernels through a
  TLLM_JIT_SHARE_DIR that jit_share.install() refused.
- Tests: a store that cannot be made; the caches' store inside and after a
  pass; the tune-mode builds exist when the first tactic is timed; hold()
  ends after a deadline and the take-back tests release it in finally; the
  unloadable-build test keeps one worker; msa_kernel_builds' replay, variant
  submission and lookup wait, on a stand-in fmha_sm100.

Signed-off-by: chenfeiz0326 <[email protected]>
…n() or submit() first

Each first request gets a fresh rank, and the test checks that no build was
registered and that the workers are off before the rank compiles the kernel
itself.

Signed-off-by: chenfeiz0326 <[email protected]>
…er; builds only long enough for a worker

- BuildStrategy (PARALLEL, INDEPENDENT): how the ranks of a launch split an
  op's kernel builds. TuningConfig.build_strategy replaces share_jit_builds
  (the 7 ops that set it use PARALLEL). KernelBuilder's BuildConfig.strategy
  splits builds through claims in the shared directory, among the ranks whose
  share_scope (where the op's kernels land) is the same; a rank waits for a
  build another rank has started, and makes one it has not. MiniMax-M3's
  fmha_sm100 variants and modules use PARALLEL, scoped to the host and the
  package's cache directory.
- When the processes share their JIT builds, an op with a warmup thread pool
  (DeepGEMM) runs the profiles this process claims on the pool, rather than
  one at a time on the calling thread; the sweep then waits for the others.
- Without TLLM_JIT_SHARE_DIR, the processes of a launch that run on one node
  share their builds through the launch's directory on local disk,
  /tmp/tllm-jit-<uid>/<launch> (PMIx namespace, Slurm job step or torchrun
  run with at least two processes on the node, or a DWDP rendezvous). It
  leaves DeepGEMM's disk cache where it is, and is removed once its processes
  have exited. TLLM_JIT_SHARE_DEFAULT=0 or TLLM_KERNEL_BUILDER=0 turns it off.
- A worker takes a build only when builds of its kind take at least
  TLLM_KERNEL_BUILDER_MIN_BUILD_S seconds (default 5), as measured so far or
  as expected until then (BuildConfig.build_seconds, process_built's
  build_seconds); tune mode builds one in-process, timed, before it hands an
  unmeasured op to the workers. Kimi-K3's decode-kernel prewarm and the low-M
  GEMM races offer their CuTe DSL builds to the workers again; their builds
  take well under a second, so they compile in-process.

Signed-off-by: chenfeiz0326 <[email protected]>
Tune mode measured an op's builds by its first item, which can be a runner
whose kernels ship prebuilt (the low-M race's cuBLAS); its runners are now
measured, and sent to the workers, one by one. Tests: a prebuilt runner ahead
of a JIT runner; the cache scope of fmha_sm100's variants; ranks with
workers of their own and another scope both build; the waits for another
rank's build start before the build ends.

Signed-off-by: chenfeiz0326 <[email protected]>
…wait returns

The waiter thread records the build's state, or its error, the moment its
wait for another rank's build returns.

Signed-off-by: chenfeiz0326 <[email protected]>
…ronous BuildPool

KernelBuilder's build mode and the autotuner's tune mode build an op's
kernels on the rank's worker processes and go on once the builds have
ended. BuildPool (tensorrt_llm/_torch/build_pool.py) replaces RankWorker:
BuildPool.build() runs a list of builds in parallel and returns their
results; a worker claims a build the ranks split when it starts it, and
the other ranks wait for the builds they find claimed; process_built
functions build several kernels at once with build_all(). KernelBuilder
builds what a call lists before the call goes on, so it no longer queues
builds, waits for them at lookups or takes them back.

Signed-off-by: chenfeiz0326 <[email protected]>
…ch as Kimi-K3's prewarm

KernelBuilder.build_kernels([(function, args), ...]) compiles the CuTe DSL
kernels an op knows it needs. In build mode, the kernels of a process_built
function whose compiles take long enough build on the rank's worker
processes, and the calls load them; until a function is measured, its first
kernel compiles in the call, timed. Kimi-K3's decode-kernel prewarm calls it,
so ops outside the autotuner build only through KernelBuilder, and its
report lists them. BuildPool.build_kernels builds process_built kernels into
the store and returns each one's result.

Signed-off-by: chenfeiz0326 <[email protected]>
…rom shapes

The NVFP4 gather (FC1) and finalize (FC2) grouped GEMM runners and the MLA
decode runner set build_in_workers. Their build() compiles the kernel a
tactic runs without launching it. With TunableRunner.builds_from_shapes, a
worker process hands build() meta tensors of the profile's inputs and
allocates none; MLA decode compiles from small CUDA stand-ins of its
tensors' layouts.

TuningConfig.build_seconds gives the time of an op's builds, which tune
mode then does not measure; these ops give theirs (1 s, 4 s for MLA
decode). Tune mode measures a build on one profile's inputs before it makes
the other profiles', each rank that shares the builds on a different
profile and tactic.

Signed-off-by: chenfeiz0326 <[email protected]>
… of its own

isort and yapf wrap the autotuner import differently once it lists
BuildStrategy, so the pre-commit check failed on the file; the import
keeps main's form.

Signed-off-by: chenfeiz0326 <[email protected]>
…or bandit

The workers' messages travel over the pipes between a rank and the worker
processes it starts (sys.executable, a fixed argv, no shell), and the
default share directory is one of this user alone under /tmp.

Signed-off-by: chenfeiz0326 <[email protected]>
… declares its builds

- A worker frees the GPU memory a build allocated once it ends, and reports the most it
  allocated at once; the pool starts a build only while the GPU's free memory covers it
  beside the builds running (the most a build of its kind took, 8 GiB kept free). A kind
  not measured yet builds one at a time; a build that does not fit alone is left to the
  caller.
- DeepGEMM's two swap-AB runners set build_in_workers with their expected build time
  (2 s): under the default 5 s rule their builds stay on the thread pool, as before.
- _build_in_pool decides from the build times it has before it makes any inputs.

Signed-off-by: chenfeiz0326 <[email protected]>
A worker's small inputs kept a CPU copy of their values, and a pickled CPU tensor of a float8 or
float4 dtype does not load in another process (AttributeError: type object
'torch.storage.UntypedStorage' has no attribute 'dtype'): with every build on the workers,
DeepSeek-V3-Lite's prequantized FP8 swap-AB GEMM failed 70 of its builds there, at the small
profiles whose FP8 activation is small enough to keep. The spec now keeps the storage's bytes,
which load for every dtype, and views them as the dtype in the worker.

Signed-off-by: chenfeiz0326 <[email protected]>
…tuning group, share FlashInfer's CuTe DSL kernels

MXFP8GemmUnifiedRunner tunes with DistributedTuningStrategy.PARALLEL: each rank of the tuning group
times its share of the (backend, tactic) pairs and the merge gives every rank the fastest. With
BuildStrategy.PARALLEL and shared JIT builds, FlashInfer's CuTe DSL MXFP8 kernels go through the
launch directory (SharedKernelCache via the runner class's share_kernel_caches()), so each kernel
is compiled once per node and the other ranks load it.

SharedKernelCache gets hooks for caches whose keys or values hold more than the kernel; FlashInfer's
cache keys by device index (named by SM count instead) and keeps the cluster count beside the kernel.

PARALLEL now deals a profile's pairs only when every rank of the group profiles it; a profile some
rank lacks (attention DP: the ranks' own row counts) is timed in full by the ranks that have it.

Signed-off-by: chenfeiz0326 <[email protected]>
…trategy/build_in_workers docs

Signed-off-by: chenfeiz0326 <[email protected]>
…cides; no build-time rule in the autotuner

TuningConfig.build_in_workers becomes use_build_pool_in_warmup: in the memory-profiling warmup,
tune mode builds every (profile, runner, tactic) of an op that sets it on the build pool's
workers. The op decides: the autotuner no longer measures a build to apply the
TLLM_KERNEL_BUILDER_MIN_BUILD_S rule, and TuningConfig.build_seconds is gone (KernelBuilder keeps
both).

DeepGEMM's swap-AB GEMMs, the CuTe DSL NVFP4 MoE fusions, CuTe DSL MLA decode and the low-M
race no longer set it: their builds, about a second or less, build in the rank's process, as
they did under the rule. Their compile-only build() stays.

Signed-off-by: chenfeiz0326 <[email protected]>
The members of BuildStrategy (TuningConfig.build_strategy, BuildConfig.strategy)
say who builds a kernel:
* INDEPENDENT -> PER_RANK: each rank builds every kernel it needs.
* PARALLEL -> SHARED: the ranks that share JIT builds split the builds.

DistributedTuningStrategy keeps its names. The tests named after the old
member are renamed with it.

Signed-off-by: chenfeiz0326 <[email protected]>
…ides; no build-time rule in KernelBuilder

KernelBuilder now works as the autotuner does. In build mode, an op's builds
go to the build pool only when the op sets BuildConfig.use_build_pool_in_warmup;
for a process_built compile function, process_built(use_build_pool_in_warmup=True).
Removed:
* BuildConfig.build_seconds and process_built's build_seconds;
* TLLM_KERNEL_BUILDER_MIN_BUILD_S;
* the pool's build-time measuring: worth_a_worker, unmeasured, build_seconds,
  record_build.

MiniMax-M3's MSA builds (35-60 s each) set it. Kimi-K3's prewarm compile
functions do not, and compile in the rank as before. The rule's tests are
replaced by tests of an op and a compile function that stay off the pool.

Signed-off-by: chenfeiz0326 <[email protected]>
…ch records, shared MXFP8 kernel names, docs

- Build pool: an error serving a build fails that build and ends its worker. process_built
  binds a call's keywords and defaults to one key per kernel. A worker's first build reserves
  room for the CUDA context it makes, and no worker starts beside the reservations of the
  builds running. A shared store that cannot be made leaves the rank a store of its own.
- jit_share: torchrun's default rendezvous id ("none") names no launch. A process's record in
  its launch directory is a file it holds a flock on, which processes of other PID namespaces
  can test. A launch of several processes per node that does not share logs why, once.
- KernelBuilder starts each rank's list at its jit_share slot, not at its device index.
- MXFP8: a FlashInfer CuTe DSL kernel is named by the max active clusters it is compiled for
  (sm_reserve lowers them), and one of another count is neither exported nor loaded.
  TunableRunner.primes_cached_tactics and share_library_kernel_caches() replace the
  kernel_cache alias and the hook found by reflection.
- Autotuner: _agree_profiles_across_ranks; the PARALLEL, deal and vote docs.
- Docs: the default sharing directory, TLLM_JIT_SHARE_DEFAULT, TLLM_KERNEL_BUILDER and
  TLLM_KERNEL_BUILDER_WORKERS, and a release note.
- Tests for each, a per-bucket deal test, the MXFP8 sharing test through tuning, and a check
  that the packaged fmha_sm100 keeps the names the MSA builds use.

Signed-off-by: chenfeiz0326 <[email protected]>
…unt free workers, close a failed start's pipes; name kernels by their source file

- A worker that does not reply to a build within TLLM_KERNEL_BUILDER_BUILD_TIMEOUT_S seconds
  (default 900; 0 waits without a limit) is stopped by a watchdog: its build fails, the pass
  goes on, and the op builds that kernel itself.
- The pool counts the workers free for a build less the builds queued for them, so a later
  batch starts the workers its builds need once the GPU has room again.
- A worker start that fails part way closes the descriptors it opened, and the pass starts no
  other worker.
- process_built kernels and the runners' shared CuTe DSL kernels are also named by a digest of
  the source file that defines them, so a kernel compiled from another version of the file is
  not loaded.

Signed-off-by: chenfeiz0326 <[email protected]>
… tune through the pool store when TLLM_JIT_SHARE_DIR was refused

- A build whose worker ends without settling it (stopped by the watchdog, died, or its
  serving thread raised) has its claim marked failed when that worker made it, so that the
  ranks waiting for the kernel build it at once instead of at the end of
  TLLM_JIT_SHARE_WAIT_S. A claim another rank's worker made stands.
- A TLLM_JIT_SHARE_DIR that install() refused is never a CuTe DSL store. Tuning inside a build
  pass therefore shares the runners' kernel caches through the pool's store whatever the
  variable says, and the rank loads the kernels its workers built instead of compiling them a
  second time.

Signed-off-by: chenfeiz0326 <[email protected]>
…other non-positive or non-finite values

TLLM_KERNEL_BUILDER_BUILD_TIMEOUT_S=0 is the one value that waits without a limit. A negative,
non-finite or unparsable value no longer turns the watchdog off: it keeps the 900 s default,
with a warning. A finite value above what a timer can wait is capped.

Signed-off-by: chenfeiz0326 <[email protected]>
Conflicts:
- autotuner.py: _parallel_jit_warmup keeps this branch's shared-sweep claims
  and its count of the profiles it ran, with main's failure count and first
  failure message in place of the list of exceptions (NVIDIA#22848).

Signed-off-by: chenfeiz0326 <[email protected]>
…ses; count it once it is served

- A worker starts in a session of its own, and a timeout, a failed exchange
  or a forced stop kills its whole process group (the Ninja and nvcc a build
  started) before its claim lets another rank retry the build.
- A worker is counted only once its serving thread runs; one whose thread
  does not start is killed and reaped, and its builds fall back as for a
  worker that does not start.
- A build function that returns False found its kernel made already: the
  build ends present, and KernelBuilder counts it as found built, apart from
  the builds other ranks claimed.

Signed-off-by: chenfeiz0326 <[email protected]>
…ackage name; skip AOT builds

- The worker imports the k2q CSR extension's module as
  fmha_sm100.cute.src.sm100.build_k2q_csr, the name the packaged fmha_sm100
  has it under: src.sm100.build_k2q_csr is not importable there, which left
  the extension to the sparse prefill's first call.
- A variant or module the package loads from its ahead-of-time build
  (BUILD_MSA_AOT, jit._prebuilt) is not listed for a build.

Signed-off-by: chenfeiz0326 <[email protected]>
…ses once its rank is gone

A worker leads a process group of its own (start_new_session), so the end of the rank's
process group does not reach it, and it reads its task pipe only between builds: a rank killed
during a build left the worker and its compiler running. The worker now watches the rank, as
the PrimTS precompile supervisor does (_watch_rank in _prims_ts_worker.py), and once the rank
is gone it kills its own process group.

Signed-off-by: chenfeiz0326 <[email protected]>
…report it built by its library

import_k2q_csr reported a build when the import took a second or more. Every rank's worker
imported the module, and a worker that waited under PyTorch's lock for another rank's compile
took as long, so it reported a build too. The ranks that share PyTorch's extension directory
on a host now split the build (BuildStrategy.SHARED), and the worker that claimed it reports a
build when the extension's library changed during the import. The build is not listed when
the package loads the extension built ahead of time (BUILD_MSA_AOT).

Signed-off-by: chenfeiz0326 <[email protected]>
@chenfeiz0326
chenfeiz0326 force-pushed the chenfeiz/kb-build-tune-sched branch from 8cd5a0c to f0f5ded Compare November 26, 2026 04:37
…ult when priming cached tactics

_prime_cached_tactics read type(r).primes_cached_tactics, which only TunableRunner declares. The
CPU contract test test_megamoe_shared_fc12_autotune.py::CpuContractTests::
test_original_autotuner_primes_each_cached_tactic_before_serving runs the method against a
runner on a stand-in base class, and it failed with an AttributeError in pre-merge
(CPU-Generic-x86-2, CPU-Generic-arm-2). The attribute is now read with getattr and a False
default, as the kernel_cache check beside it is.

Signed-off-by: chenfeiz0326 <[email protected]>
… takes no worker

A build whose claim another process holds ends claimed, and one it made ends
present, when it is submitted, or when the claim appears while the build
waits in the queue or for GPU room (the wait now polls the claim). Before,
such a build took a worker and an admission slot to learn it, and a build
refused for GPU room while another rank built it was left to the op, which
compiled the kernel again.

Signed-off-by: chenfeiz0326 <[email protected]>
… claimed per kernel

Tune mode built every (profile, runner, tactic) and split the items across
ranks as a list named after the pending profiles: a kernel that serves many
profiles was built once per profile, and ranks with different profiles did not
split the builds they share.

TunableRunner.build_key(inputs, profile, tactic) now names a build. Tune mode
lists one build per key, on the first profile that needs it, and the ranks
claim each build as a file named after its key in <TLLM_JIT_SHARE_DIR>/autotuner:
_build_across_ranks claims before it runs a build, and in the build pool the
worker that starts a build claims it. The CuTe DSL MoE finalize and gather
fusions and MLA decode return their kernel_cache key, which serves every row
bucket that compiles the same kernel and every expert-parallel rank.

Signed-off-by: chenfeiz0326 <[email protected]>
…iles of a call

PARALLEL deals each profile's (runner, tactic) pairs round robin on its own.
TuningConfig.parallel_plan="call" (or TLLM_AUTOTUNER_PARALLEL_PLAN=call for
every op) plans the pairs of every profile the group tunes in a choose_one call
at once (plan_parallel_items): largest items first to the least loaded rank,
an item costing 1 plus its profile's input size relative to the largest, so
that the ranks finish together. The plan is the same on every rank. The
default stays "profile", the deal as before; the merge is unchanged.

Signed-off-by: chenfeiz0326 <[email protected]>
@chenfeiz0326
chenfeiz0326 force-pushed the chenfeiz/kb-build-tune-sched branch from f0f5ded to 5b52835 Compare November 26, 2026 12:01

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant