Inference User Guide
A practical guide to running inference with Megatron Core (MCore) using the dynamic inference path. This is the recommended and actively developed inference stack in Megatron-LM.
The legacy static engine is deprecated. New work should target the dynamic path described here.
Table of Contents
- What Megatron Inference Is For
- Supported Model Architectures
- Supported Features
- Basic Usage: The High-Level API
- Async Scheduling
- Streaming
- OpenAI-Compatible HTTP Server
- Weight Refit and Resharding for RL
- Multimodal (Vision-Language) Inference
- Disaggregated Prefill and Decode
- Customizing the Pipeline
- Examples Directory
- Known Limitations
- Roadmap and Future Work
- Additional Resources
What Megatron Inference Is For
Scope. Megatron Inference is an MCore-native generation capability built for RL rollouts, evaluation, and debugging of MCore models. It is complementary to dedicated serving engines such as vLLM, SGLang, and TensorRT-LLM: use it when you want generation to run on the same MCore model, parallelism, and kernels you train with, and reach for a dedicated serving engine when standalone production serving is the goal. Performance varies by model, workload, and which consistency features you enable.
Megatron Inference is built primarily as the generation engine for reinforcement learning (RL), not as a standalone serving engine. Its design center is the RL loop, where a model alternates between training and rollout phases inside the same process. A rollout is typically generation plus sandboxing or environment infrastructure. Megatron Inference provides the generation portion.
This focus drives the major design benefits:
- Consistency between training and inference. RL is extremely sensitive to numerical mismatch between the framework that trains the policy and the one that generates rollouts. Running both in MCore removes the cross-framework portion of this gap and makes the remaining numerical mismatch far easier to control (refer to batch-invariant kernels below).
- No model conversion. Because generation runs on the same MCore model, there is no Hugging Face to MCore conversion step between training and generation. Architectures land in the inference stack close behind their training support, subject to the gaps in Known Limitations.
- Inexpensive training to inference transitions. This is because tight coupling enables in-place weight refit and shared memory management, drastically cutting re-initialization cost relative to standing up an external inference engine each rollout.
- Colocated and non-colocated deployments. Megatron Inference supports weight refit and resharding between training and inference, so the same weights can be moved between the two phases under different parallelism layouts. This covers both colocated setups (where training and inference share the same GPUs) and non-colocated setups (where training and inference run on separate resources), with the engine resharding weights to the inference-time parallel configuration during the swap.
- First-class parallelism reuse. Inference reuses Megatron Core’s existing tensor parallelism (TP), expert parallelism (EP), and pipeline parallelism (PP) infrastructure directly.
Supported Model Architectures
The table below lists the architectures the dynamic inference path supports and the configurations they have been exercised in.
A note on performance. Throughput relative to dedicated serving engines varies by architecture, and Megatron Inference is not uniformly faster. Most optimization work to date has targeted the Nemotron model families, where rollout throughput is competitive with external engines. On architectures that have received less tuning, Megatron Inference can be somewhat slower; closing those gaps is in active development. Results are also sensitive to batch size, sequence length, parallelism layout, and which consistency features (for example, batch-invariant kernels) are enabled, so measure on your own model and workload rather than generalizing from any single comparison.
Supported Features
Batch-invariant kernels (training and inference log-prob consistency). Standard GEMM, attention, and norm kernels can produce slightly different numerics depending on batch composition, which shows up as log-prob mismatch between training and inference. This mismatch is a real source of error and instability in RL. Megatron Inference offers batch-invariant kernels whose outputs do not depend on how requests are batched, so per-token log-probs match between the training and inference forward passes.
Enable it with
batch_invariant_modeon the model’sTransformerConfig— it is not anInferenceConfigfield, so it must be set when you build the model, not on the engine config. Two companion fields tune it:batch_invariant_backend(te_nativeby default, ortriton;deepgemmis deprecated and runs aste_native) selects the GEMM backend, andbatch_invariant_collective(orderedby default, ormultimem) selects the cross-rank expert-combine reduction.Both dense and MoE models are supported. Batch-invariant MoE supports BF16 and MXFP8 squared-ReLU/SwiGLU experts, requires unfused permute/unpermute, and under
--transformer-impl inference_optimizedrequiresinference_grouped_gemm_backendofvllm,torch, orflashinfer(plus thenvlstoken dispatcher whenEP > 1). FlashInfer supports MXFP8 non-gated squared-ReLU experts, including selective BF16 layers, but its batch invariance does not imply bitwise parity with TE training. Use Torch/vLLM for exact training-policy comparisons. Batch invariance needs no extra dependency: training experts use TE’s native grouped GEMM under the starved cuBLASLt workspace with either backend, and thetorchinference backend’s BF16 experts usetorch._grouped_mm. Context parallelism and attention dropout are not supported in either case. MXFP8 training usesbatch_invariant_backend="te_native". Legacy TE GroupedLinear remains the default. On SM100 with cuBLASLt 13.5.1,moe_use_grouped_tensor=Trueopts into TE device-metadata grouped GEMM and its required 256-row expert padding. This requires a TE build exposingGroupedLinear(use_grouped_tensor=...); the old environment switch alone is not sufficient. Other GPU/library pairs are rejected: this path requires a full workspace, so its forward invariance must be validated numerically rather than inferred from alignment. Keepuse_transformer_engine_op_fuser=False. Forward batch invariance does not promise bitwise-identical gradients across different minibatches.Batch-invariant MoE matches training and inference only at the same expert-parallel size: the expert combine adds per-EP-rank partials. To train and generate at different EP sizes (for example EP8 training, EP4 generation), also set
moe_ep_invariant_combine=Trueon both configs. Every EP size then sums a token’s expert outputs over one fixed fp32 tree over the expert ids, so MoE outputs are bitwise identical at every power-of-two EP that dividesnum_moe_experts. It covers the training AllToAll dispatcher and the inferencenvlsdispatcher with thetorchorvllmgrouped-GEMM backend, at any expert-tensor-parallel size: inference sums each expert’s rows over expert TP before the combine, as training does.
Selective MXFP8 parameter storage. Use the same Transformer Engine per-module precision recipe for training and inference through
TransformerConfig.quant_recipe(or--te-precision-config-file). The recipe selects storage before TE initializes each module: BF16 modules allocate ordinary BF16 parameters and load checkpoint values directly, without an intermediate MXFP8 conversion or a checkpoint callback. With globalfp8_param=Trueandfp8_recipe="mxfp8", an MXFP8 recipe that omitsfp8_paraminherits the global policy, including first/last BF16 layers. For routed-expert-only quantization, match*mlp.experts.linear_fc1and*mlp.experts.linear_fc2to MXFP8, with an MTP BF16 rule first and a catch-all BF16 rule last. See TE precision recipes.Within one MoE layer, every local expert’s FC1 and FC2 weight must use the same precision. The
flashinfer,torch, andvllmgrouped-GEMM backends support mixtures of BF16 and MXFP8 layers. vLLM uses its fused kernel for BF16 expert layers and MCore’s scaled grouped GEMM for MXFP8 expert layers.
Many of these are toggled through InferenceConfig. Refer to the
Engine configuration.
Basic Usage: The High-Level API
The API lives in
megatron/core/inference/apis/ and gives
you a vLLM-style generate(prompts, sampling_params) interface. It hides
the underlying pipeline (DynamicInferenceContext to GPTInferenceWrapper to
TextGenerationController to DynamicInferenceEngine) so that you do not have to
wire it up by hand.
The two classes: MegatronLLM and MegatronAsyncLLM
Both expose the underlying building blocks as read-only properties. Use these for advanced customization:
llm.enginellm.contextllm.controllerllm.is_primary_rank
Both also expose submit(coro) and run_sync(coro), which schedule a coroutine
on the engine’s background runtime loop. Use these to reach the lower-level
async surface (for example InferenceClient streaming) without standing up your
own loop.
Constructor arguments worth knowing: use_coordinator (defaults to True),
coordinator_host / coordinator_port, and inference_wrapper_cls (defaults to
GPTInferenceWrapper; pass VLMInferenceWrapper for vision-language models).
Caller responsibilities (before construction):
- Call
initialize_megatron(...)to perform full Megatron distributed setup. - Build the model and call
model.eval(). The API does not toggle model state. - Have a tokenizer ready.
Direct Mode Compared to Coordinator (Indirect) Mode
Megatron Inference supports two operating modes. Direct mode is simpler but limited. Coordinator mode adds a routing layer that enables serving, expert parallelism, and lifecycle controls. Coordinator mode is the default (use_coordinator=True); opt into direct mode explicitly.
Direct Mode (use_coordinator=False)
Direct mode is the simplest configuration for offline batch generation:
- Every rank is treated as primary and runs the engine synchronously.
- You own data sharding, which means that you decide the prompts that are assigned to which
data-parallel replica and call
generateon each. - The simplest path for offline batch generation when you already shard the data yourself (typical for many RL rollout setups).
- Lifecycle controls (
pause/suspend/…) are not available and raiseRuntimeError. So aresubmit/run_sync, which need the background runtime loop. - Not allowed with expert parallelism (
EP > 1). This is because EP routing requires the coordinator. - Text-only:
multi_modal_datais rejected in direct mode.
Coordinator Mode (use_coordinator=True)
Coordinator mode adds a background routing layer and is required for serving and advanced features:
- A background data-parallel coordinator routes requests across DP
replicas for you. An
InferenceClienton global rank 0 submits work. - Required for: HTTP serving (
serve), expert parallelism (EP > 1), multimodal inputs, streaming, and the lifecycle controls (pause/unpause/suspend/resume). generatemay only be called on the primary rank (rank 0). Worker ranks block until shutdown propagates.- Internally spins up a daemon-thread event loop so the engine’s asyncio primitives don’t collide with your loop.
Mode and class compatibility:
MegatronAsyncLLMrequiresuse_coordinator=True(direct async is rejected at__init__).MegatronLLMsupports both. So the three supported combinations are: sync+direct, sync+coordinator, async+coordinator.
serve()is not async-only. BothMegatronLLM.serve(...)andMegatronAsyncLLM.serve(...)start the HTTP frontend; what serving requires is coordinator mode, not the async class. UseMegatronLLM.serve(...)from a plain synchronous launcher script, andMegatronAsyncLLM.serve(...)when you are already inside an event loop.
Sync Offline Batch Generation
The runnable end-to-end script is
examples/inference/offline_inference.py.
A minimal version:
generate accepts a single prompt or a batch, as strings or pre-tokenized
token-id lists:
"a single string": returns a 1-element list["a", "b"]: returns a list in input order[1, 2, 3]: a single token-id prompt[[1, 2], [3, 4]]: a batch of token-id prompts
MegatronLLM.generate always returns a list[DynamicInferenceRequest],
even for single-prompt input.
Async Generation
MegatronAsyncLLM mirrors the sync API with await. There is a deliberate
asymmetry:
- async
generatereturns a single request for single input - list for batched input
Sampling Parameters
SamplingParams controls decoding behavior for each generate call:
An explicit seed assigns random draws by request seed and absolute token
position, so moving a request between dynamic batch slots does not reassign its
random stream. /v1/chat/completions and /v1/completions accept the same field;
chat choice i or completion prompt i uses (seed + i) % 2**63. Use a different
seed for each independently sampled rollout and reuse it when retrying that
logical request. Omitting seed retains the shared-generator sampling path.
This requires dynamic inference with sampling_backend="torch" and no
speculative decoding. Unsupported modes reject seeded requests. CUDA seeded
draws require Triton and use one batched noise-generation launch per sampling
bucket, keyed by request seed, absolute position, and vocabulary index. Explicit
request seeds require CUDA logits; CPU seeded sampling is not supported.
Unseeded rows retain the shared-generator path and seeded rows do not consume
its random draws. Exact
seeded CUDA token sequences can differ from the earlier per-row Torch implementation:
the same seed identifies a different random stream, not a loss of repeatability.
Replay requires the same implementation and runtime, not different RNG algorithms
or CPU/CUDA backends. This feature does not make model logits
batch invariant, freeze policy weights, or snapshot external tool execution.
Identical generated tokens still require identical logits and sampling settings.
prompt_tokensis no longer echoed by default. The engine drops prompt token ids before serializing a finished request, which saves the transmission cost for long prompts.prompt_lengthis always reported. Setreturn_prompt_tokens=Trueif your client needs the ids.
RL note: Prompt log-probs require every position’s logits, so requesting
return_log_probswithoutskip_prompt_log_probsalso requiresInferenceConfig.materialize_only_last_token_logits=False. The engine asserts on this combination. If you only need generated log-probs, setskip_prompt_log_probs=Trueand leavematerialize_only_last_token_logitsat its defaultTrue, which is cheaper.
InferenceConfig.logprobs_mode controls which log-probs you get:
'raw_logprobs' (default) returns the unmodified model log-probs, while
'processed_logprobs' returns log-probs of sampled tokens after temperature,
top-k, and top-p have been applied. The mode applies to both the selected-token
log-probs and the top-N log-probs (top_n_logprobs). Prompt log-probs, including
prompt top-N log-probs, are always raw in both modes, because prompt tokens were
not sampled from the processed distribution.
'processed_logprobs' is not yet supported with speculative decoding.
With prefix caching, prompt log-probs are cached on the KV blocks of the
tokens that produced them, so a request that returns prompt log-probs can
still skip a cached prefix. It skips only the blocks whose cached rows cover
its top_n_logprobs, and computes the rest. Requests with top_n_logprobs
above 100 share cached KV but compute every prompt token.
Engine Configuration
InferenceConfig configures the engine, KV-cache, and CUDA-graph behavior and is
where most features are turned on. Construct it directly, or derive it from
model and CLI args using the function
megatron.inference.utils.get_inference_config_from_model_and_args. Frequently
used fields:
Some inference-relevant switches live on the model’s TransformerConfig rather
than InferenceConfig, because they must be set when you build the model:
batch_invariant_mode (and batch_invariant_backend /
batch_invariant_collective), inference_moe_token_dispatcher_type (nvls by
default, or nccl), inference_grouped_gemm_backend (vllm by default, or
torch / flashinfer), inference_moe_router_backend (triton by default, or
torch), quant_recipe, moe_enable_routing_replay, and window_size for
sliding-window attention.
Tanh-clamped squared ReLU with FlashInfer
On SM10x GPUs, BF16 and MXFP8 experts support the activation (L * tanh(relu(x) / L)) ** 2 with
inference_grouped_gemm_backend="flashinfer", activation_func=squared_relu,
activation_func_tanh_clamp_scale=L, gated_linear_unit=False,
and add_bias_linear=False.
Requires FlashInfer >= 0.7.2, with ActivationType.ClampedRelu2 and
clamped_relu2_limit support. This path does not support
inference_flashinfer_mxfp8_token_capacity or batch_invariant_mode.
Reading Results
generate returns DynamicInferenceRequest objects. The most commonly used fields are:
generated_text: Decoded output stringgenerated_tokens: Output token-idsprompt: Echoed prompt textprompt_length: Prompt length in tokens, always reportedprompt_tokens: Prompt token ids, only whenSamplingParams.return_prompt_tokens=Trueprompt_log_probs,generated_log_probs: Log-probs (when requested)ttft: Time-to-first-token (seconds)status: Terminal request status
Lifecycle Controls
In coordinator mode, you can drive the engine’s state machine. This is important for the RL loop where you alternate generation and training:
pause()/unpause()— halt and resume scheduling.suspend()/resume()— offload/reload GPU buffers (KV cache, Mamba states). Callpause()beforesuspend().shutdown()/wait_for_shutdown()— tear down or block until the engine loop terminates.
These raise RuntimeError in direct mode. The context-manager exit calls
shutdown() for you.
suspend() / resume() are also the hook for weight refit or resharding
between training and inference: suspend the engine (optionally offloading the
KV cache), refit or reshard the updated weights into the inference parallel layout,
then resume. This is what enables both colocated (training and inference on
the same GPUs) and non-colocated (separate resources) RL deployments. Refer to
Weight Refit and Resharding for RL for the
refit call itself.
Starting Suspended: Serving Before the Weights Are Final
InferenceConfig(start_suspended=True) builds the engine in the SUSPENDED
state. The coordinator, the engine loop, and the serve() frontends come up
immediately and submitted requests are queued rather than failed, while
CUDA-graph capture waits for the first resume(). The inference-state buffers
are allocated at construction and are then held or released according to
kv_cache_management_mode, as after a regular suspend(). Use it when the
weights the engine will run with are not in place at construction, for example
an RL rollout engine that receives them through a refit:
Call resume() and then unpause() once the weights are in place; until then
submitted requests wait, and pause() and shutdown() behave as on a regularly
suspended engine. Coordinator mode only: direct mode has no resume(), so
start_suspended raises ValueError at __init__.
Async Scheduling
Async scheduling reorders request processing to prepare the next forward pass before resolving the previous one, overlapping host-side bookkeeping with GPU work. It is enabled by default. Select legacy scheduling explicitly when a feature such as MoE router replay requires it:
The equivalent command-line option is
--inference-dynamic-batching-async-sched-mode legacy.
The engine decides per step whether overlapping is profitable, so enabling the mode does not force overlap on every step. Restrictions to be aware of:
- MoE router replay is not supported. Async scheduling and
moe_enable_routing_replayare mutually exclusive and the engine raises if both are set. - Paused requests are not supported by the overlapped ordering.
- Speculative decoding needs one MTP depth per speculative token, so
num_speculative_tokensmay not exceed the model’s MTP depth count.
Prompt log-probs work under async scheduling but require materializing every
position’s logits, which gives back much of the overlap benefit. The performance
recipes therefore pass --skip-prompt-log-probs.
Streaming
Streaming emits partial results as tokens are produced instead of waiting for the
whole completion. It is a per-request setting on SamplingParams and requires
coordinator mode.
streaming_interval is the minimum number of unsent tokens per partial reply
(an integer >= 1); raise it to trade latency for fewer messages.
Over HTTP, pass the usual OpenAI field. Both /v1/completions and
/v1/chat/completions accept "stream": true and return an SSE stream
(text/event-stream), and both honor "streaming_interval" and
"stream_options": {"include_usage": true}. Chat streaming emits content,
reasoning_content, and incremental tool_calls deltas. HTTP streaming requires
a Hugging Face fast tokenizer; other tokenizers get an HTTP 400.
Programmatically, stream through InferenceClient.add_request_streaming,
which returns an async iterator yielding {"partial": {"request_id", "new_tokens"}}
frames followed by exactly one {"final": ...} frame. MegatronLLM and
MegatronAsyncLLM do not yet expose a streaming generate, so reach the client
through the engine and drive it on the runtime loop with llm.submit(...) or
llm.run_sync(...). InferenceClient also offers abort_request(request_id).
OpenAI-Compatible HTTP Server
Megatron Inference can serve requests over HTTP using the OpenAI API format. This section explains how to start the server and query it.
serve(...) — available on both MegatronLLM and MegatronAsyncLLM — starts
the HTTP frontend on the primary rank (global rank 0). Serving requires
coordinator mode and raises ValueError otherwise. The routes are:
Each route is also served without the /v1 prefix. Chat completions report
prefix-cache hits as usage.prompt_tokens_details.cached_tokens.
The runnable script is
examples/inference/launch_inference_server.py,
with the shell wrapper
examples/inference/run_inference_server.sh
(packaged for a Nemotron-6 3B hybrid MoE config: TP 2, EP 8, PP 1).
ServeConfig fields: host ("0.0.0.0"), port (5000), parsers ([] —
response/reasoning/tool parsers, named by their registry keys:
deepseek-r1-reasoning, nemotron-v3-reasoning, qwen3-coder-tool), verbose
(False — per-request logging), frontend_replicas (4 — HTTP frontend
processes on the primary rank), and sock (None — an already-bound listening
socket to use instead of binding host:port).
default_temperature (1.0), default_top_p (1.0), and default_top_k
(0) provide sampling defaults for HTTP requests that omit those fields.
eval_mode (False) switches the frontend to evaluation-oriented response
defaults, avoiding prompt-token transmission unless a request opts in.
The same call works from a synchronous launcher:
To launch the server using the wrapper:
To verify that the server is ready, verify that you receive the following output:
Then query it with any OpenAI-compatible client. Chat templates, tool calling, and reasoning parsers are supported.
The dynamic server returns
"model": "EMPTY"and does not validate the requestmodelfield. You can pass anything you like. Refer to Known Limitations.
Weight Refit and Resharding for RL
After a training step, the RL loop has to push the updated policy weights into the inference engine — potentially across a different parallelism layout, and potentially across a process boundary. Megatron Core does this through the resharding module rather than through the inference API:
The transport is selected by refit_method, exposed on the command line as
--refit-method:
MXFP8 targets are handled transparently: when the destination model uses
--transformer-impl inference_optimized with --fp8-recipe mxfp8,
prepare_swap_model_weights installs a quantizing transform that later
swap_model_weights calls pick up. The refit plan follows the TE per-module
precision recipe: only destination weights initialized with MXFP8 storage are
quantized, while BF16 parameters remain BF16. The built-in RL loop calls
swap_model_weights(model, inference_model, args.refit_method); refer to
megatron/core/resharding/README.md for
the plan-building and caching details.
Multimodal (Vision-Language) Inference
Image inputs are supported on the dynamic-batching path. This requires
coordinator mode and a wrapper override, because the high-level API defaults to
GPTInferenceWrapper:
multi_modal_data follows vLLM’s modality-dictionary shape; "image" accepts
raw bytes, a list of raw bytes, or a preprocessed tensor dictionary. Batched
prompts take one dictionary per prompt.
Over HTTP, /v1/chat/completions accepts standard OpenAI multimodal content
parts: image_url blocks with either a base64 data URL or a remote http(s)
URL. Remote fetches refuse redirects and non-public addresses.
Supported models are LLaVA-style models and Nemotron Omni; the wrapped model must
implement forward_lm_only. As an alternative to wiring this up yourself,
tools/run_dynamic_text_generation_server.py auto-detects VLM versus GPT from
the checkpoint. Refer to Known Limitations for what is not
yet covered — notably video, audio, and pipeline parallelism.
Disaggregated Prefill and Decode
The native HTTP server can run prefill and decode on separate GPUs; no Dynamo
deployment or client-side handoff is needed. Enable it with role-tagged
--inference-shards and prefix caching on
examples/inference/launch_inference_server.py.
Starting from a working server launch, keep your model, checkpoint, tokenizer,
and cache-sizing arguments (MODEL_ARGS below), and use two GPUs for one
prefill engine and one decode engine:
MODEL_ARGS is your existing shell array of model-specific arguments, not a
Megatron option. Each engine must have enough memory for its model and cache.
The shard sizes must cover the entire torchrun world. For example, four GPUs
can use tp=2,role=prefill+tp=1,dp=2,role=decode for one TP=2 prefill engine
and two TP=1 decode engines. NCCL works within this shared launch; alternatively,
select nixl (the default) with NIXL installed on every engine rank.
Send requests to the same frontend as usual:
There is no disaggregation flag in the HTTP request. The native coordinator
routes prefill, reserves a decode engine, transfers KV state (plus conv/SSM
state for hybrid models), and returns the response through the same frontend.
Ordinary clients use SUBMIT_REQUEST; the runtime sends
SUBMIT_REQUEST_WITH_KV directly to the selected decode engine.
Prefix caching must remain enabled on both roles. For hybrid models, leave
--inference-dynamic-batching-prefix-caching-mamba-gb unset on the shared launch:
decode imports recurrent state into live slots, not the prefix-cache state buffer. Native disaggregation
currently accepts text/token prompts, not multimodal inputs. Generated-token
log-probs are supported with skip_prompt_log_probs=True; prompt and top-N
log-probs are not.
Use the default persist KV-cache management mode. Disaggregated transfers
currently reject offload and recompute because suspend/resume can invalidate
the registered KV/SSM buffers. Under memory pressure, evicted decode requests
recover through the engine’s local recomputation path; new requests still
route to prefill.
Keep --inference-cuda-graph-all-prefills enabled for prefill workers when desired;
it gates both full and chunked prefill admissions on compatible captured graphs.
Shared launches can pass this flag unchanged: decode workers automatically disable
the admission gate so eviction recovery can prefill without a matching graph.
This does not disable CUDA-graph replay on decode workers.
For RL, only the final decode completion writes to the request metadata ledger or payload stager. Handoffs preserve the request UID, policy/KV epoch history, and prefill eviction history. MoE routing replay is not supported: transfer setup rejects it because handoffs do not transfer the prompt’s routing buffers.
Shard models require a distributed checkpoint format such as torch_dist; legacy torch
checkpoints are rejected. For native RL, configure parallelism in
--inference-shards, not the global --rl-inference-*-model-parallel-size
overrides. Disaggregated RL currently rejects --rl-verify-model-weights-swap;
the existing verifier does not support the different shard layouts. NIXL weight
refit also rejects multiple destination pools because it binds a single receiver
map; use a supported refit backend such as NCCL. This is separate from NIXL
KV/SSM handoff, which remains supported.
RL generation-lag autotuning and HTTP concurrency limits still use local shard
capacity rather than aggregate decode capacity. Set --rl-generation-lag
explicitly for partial rollouts; capacity aggregation and the remaining
limitations are tracked in #8200.
Customizing the Pipeline
MegatronLLM and MegatronAsyncLLM cover most use cases. For more control, you can assemble or subclass the underlying components directly. Common reasons to do this include:
- Implementing step-level scheduling control.
- Adding custom sampling or logit processing.
- Migrating an existing pipeline to Megatron Inference.
Pipeline Anatomy
MegatronLLM and MegatronAsyncLLM build the following pipeline for you:
You can reach any of these from a constructed llm through llm.context,
llm.controller, and llm.engine. Or build them explicitly, which is exactly
what MegatronLLM and MegatronAsyncLLM do internally:
Customizing the TextGenerationController
The TextGenerationController manages tokenization, the forward pass, sampling, and detokenization. To inject custom behavior, subclass it and pass your instance to the engine.
Override these methods to customize the pipeline:
sample_from_logits(...): custom sampling or logit processing (constrained decoding, custom penalties, grammar masks).tokenize_prompt(...)/detokenize_generations(...): custom tokenization or detokenization.generate_output_tokens_dynamic_batch(...): custom batch forward-step integration.
Customizing the DynamicInferenceContext
The DynamicInferenceContext holds the KV cache, paging, and the
scheduling and bookkeeping state. For hybrid and SSM models it also manages the
recurrent state alongside the attention KV cache, that is sized using the
mamba_inference_state_config and mamba_memory_ratio. Mamba, Gated Delta Net,
and Gated Delta Product layers all share this one slot-indexed recurrent-state
cache, which is why a single model may not mix Mamba and GDN layers. Refer to
Known Limitations for the per-mixer feature gaps.
For MLA models the context stores compressed latents rather than full K and V,
which is why cache_mla_latents=True and a block size of exactly 64 are
required.
Configure it through InferenceConfig, which controls buffer size, block size,
prefix caching, chunked prefill, CUDA graphs, suspend and resume memory mode,
and recurrent state. Refer to Engine configuration.
To customize KV-cache layouts, eviction policies, or scheduling logic, subclass the context and pass it into the wrapper and engine.
Driving the Engine Directly
For full step-level control, skip generate and drive the engine’s
add_request and step_modern loops yourself. This is how you implement custom
arrival schedules, batch-drain modes, or suspend and resume policies:
The engine owns checkpoint records internally and returns one flat,
token-complete DynamicInferenceRequest per finished request. Its
generated_text starts as None; direct low-level callers decode the complete
token stream once by calling finalize_text(tokenizer) where text is needed.
The fully worked manual-stepping example is
examples/inference/advanced/gpt_dynamic_inference.py.
It demonstrates arrival scheduling, batch-drain, suspend and resume, CUDA-graph
bucketing, log-probs, and JSON dumping. For explicit coordinator with InferenceClient
lifecycle management, refer to
gpt_dynamic_inference_with_coordinator.py.
Examples Directory
Everything above is runnable from
examples/inference/:
Run the offline example across modes:
All supported modes produce numerically identical generated text. Note that the
example script’s --use-coordinator flag defaults to off (direct mode), which
is the opposite of the MegatronLLM constructor default.
Known Limitations
Model architecture gaps
- Vision-language models cover images only. Video and audio have no supported
preprocessing or modeling format and raise
NotImplementedError. VLM dynamic inference also requiresPP=1(pipeline and virtual pipeline parallelism both raise), and in-core static-tiling preprocessing was removed — clients needing static tiling must submit a preprocessed tensor payload. - MLA requires a specific configuration.
cache_mla_latents=True, a KV block size of exactly 64, and theflash_mlapackage. RoPE fusion, sliding-window attention, Flash Decoding, and the Triton KV-append fast path are all incompatible with the latent cache. - GDN2 is not supported.
experimental_attention_variant='gdn2'raisesNotImplementedError. GDN is also dynamic-batching only; static batching raises. - A model may not mix Mamba and GDN layers, because the recurrent-state cache and prefill metadata use one shared shape and chunk size.
- Attention-free stacks are not supported. A pipeline stage holding zero attention layers raises.
- Prefix caching is limited for the Gated Delta variants. GDN rejects
enable_prefix_cachingoutright. GDP allows KV-block prefix caching but not recurrent-state caching, so settingprefix_caching_mamba_gbon a GDP model raises. GDN additionally does not support chunked prefill (GDP does), and neither supports speculative decoding, batch-invariant mode, or context parallelism.
Feature interactions
- Sequence parallelism is rejected for dynamic batching unless both
TP > 1andEP > 1. This is easy to trip by reusing a training config that sets--sequence-parallel. - The FlashInfer sampling backend never runs under CUDA graphs. Its kernel choice is data-dependent and it bakes the RNG state into a capture as a by-value constant, so the sampler always runs eagerly. That is a deliberate correctness trade-off against sampling-step latency.
logprobs_mode='processed_logprobs'is incompatible with speculative decoding.sampling_backend='flashinfer'silently falls back to'torch'with a warning if FlashInfer is not installed.- Async scheduling excludes MoE router replay and does not support paused requests. Refer to Async Scheduling.
- Batch-invariant MoE supports BF16 and MXFP8 squared-ReLU/SwiGLU experts.
MXFP8 uses TE
te_nativefor training and Torch/vLLM for inference. FlashInfer additionally supports batch-invariant MXFP8 squared-ReLU inference, without bitwise TE training-policy parity. It requires unfused permute/unpermute and excludes context parallelism and attention dropout. Device-metadata training GEMM is opt-in viamoe_use_grouped_tensor=True, restricted to SM100 with cuBLASLt 13.5.1 and a TE build exposing that constructor argument. - MXFP8 fused quantization supports squared-ReLU only. SwiGLU uses separate BF16 activation and MXFP8 quantization kernels; it does not fall back to BF16 GEMM.
- Disaggregated handoff supports generated-token log-probs only. Set
skip_prompt_log_probs=Truewhen requesting them; prompt log-probs andtop_n_logprobs > 0are not supported.
Engine and serving
- High-level coordinator reset is not synchronized. Once an engine is drained,
engine.reset()preserves its coordinator mode and long-lived asyncio objects, and accepts resets only whileRUNNINGorPAUSED. The high-level coordinator API cannot yet prove that its background engine loop has finished bookkeeping after the final reply, however, so an immediate reset can still race that loop. The offline example therefore blocks--inference-repeat-n > 1together with--use-coordinator. Direct-mode reset is safe. - HTTP frontend is fixed to global rank 0. There is no per-rank
roleoverride onServeConfig. Control placement through the launcher (for example, torchrun rank-0 placement).ServeConfig.socklets you pre-bind the listening socket, but it still only takes effect on rank 0. - Server returns
"model": "EMPTY". The HTTP frontend doesn’t echo or validate a configured model name and exposes noGET /v1/modelsendpoint. Clients may pass anymodelvalue. It is ignored. - HTTP streaming requires a Hugging Face fast tokenizer; other tokenizers get an HTTP 400.
- Streaming is not on the high-level API.
MegatronLLMandMegatronAsyncLLMhave no streaminggenerate; stream throughInferenceClientor the HTTP frontend. - Test coverage is uneven across the newer architectures. The dynamic-inference functional suites cover GPT, MoE, and Mamba hybrid models. GDN and GDP have dynamic-inference unit tests only, and MLA dynamic inference has neither — its coverage is at the attention-layer level.
Roadmap and Future Work
API and serving:
megatron serveCLI — a single-binary launcher mirroringvllm serve, with single-node and multi-node or headless modes.- Config-based model construction —
MegatronLLM(model="...")with model recipes and checkpoint resolution. Use to remove manual model building. - Streaming on the high-level API — a streaming
generateonMegatronLLMandMegatronAsyncLLM, so streaming does not require dropping toInferenceClient. - Simplified inference API overall.
Models and performance:
- FlashInfer integration for attention and Mamba kernels (sampling, fused RoPE, and MoE grouped GEMM are already integrated).
- All2Allv-based token dispatcher for MoE.
- Large-scale inference optimizations (large models and long sequences).
- Low-precision numerics for KV cache and Mamba state. MXFP8 currently covers weights; the KV cache and recurrent state are still bf16, fp16, or fp32.
- Broadening the newer architectures — prefix caching and speculative decoding for Gated Delta variants, chunked prefill for GDN, pipeline parallelism for VLM, and functional-test coverage for MLA, VLM, GDN, and GDP.
Additional Resources
- API reference and mental model documentation:
megatron/core/inference/README.md - Examples overview:
examples/inference/README.md - Low-level engine source:
megatron/core/inference/ - High-level API source:
megatron/core/inference/apis/ - Weight refit and resharding:
megatron/core/resharding/README.md - MoE router replay:
docs/api-guide/router_replay.md - MoE routing trace tooling:
examples/inference/README.md - Functional tests:
tests/functional_tests/test_cases/gpt/gpt_offline_inference_*,gpt_inference_server_smoke_*,gpt_dynamic_inference_*,tests/functional_tests/test_cases/hybrid/hybrid_dynamic_inference_* - Unit tests:
tests/unit_tests/inference/