Simple and stable Inference APIs#4697
Conversation
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…ad CUDA device Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…inference/legacy/ Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…ith reused legacy goldens Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
… bespoke driver Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…al inference sections Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
This PR has been automatically converted to draft because all PRs must start as drafts. When you are ready for review, click Ready for Review to begin the review process. This will:
See the contribution guide for more details. |
|
/ok to test 5f651b7 |
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…ron-LM into inference_apis
|
/ok to test 963f663 |
ko3n1g
left a comment
There was a problem hiding this comment.
This will require changes in the NeMo side. Can we work with @oyilmaz-nvidia before the merge?
ko3n1g
left a comment
There was a problem hiding this comment.
Thanks for sharing more background in our offline discussion, all good now
|
/ok to test 27b920e |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/26608290361 |
|
/ok to test 4e3269d |
|
/ok to test 87ba21c |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/26617664504 |
What does this PR do ?
Motivation And Goals
The current Megatron inference APIs expose many internal building blocks:
InferenceConfigDynamicInferenceContextGPTInferenceWrapperTextGenerationControllerDynamicInferenceEngineInferenceClientDataParallelInferenceCoordinatorThis is powerful, but it makes simple usage verbose. A user who wants to run offline generation or serve requests must understand engine construction, context selection, tokenizer setup, coordinator lifecycle, and per-rank behavior.
APIs
Inspired by vLLM, we propose two dimensions for the API design.
Sync and Async APIs
We propose two major APIs for Megatron inference:
MegatronLLM: synchronous offline inference API. Calls block for finaloutputs.
MegatronAsyncLLM: asyncio-native generation, online serving (OpenAIcompatible).
Both classes support offline inference, lifecycle control (
pause/unpause/suspend/resume), and access to the underlying engine for expert use. The differentiators are:MegatronAsyncLLMexposes async methods and the online HTTP server (serve(...));MegatronLLMexposes sync methods.The underlying primitive APIs can also be accessed through corresponding
property attributes (
engine,context,controller).Coordinator
Both sync and async APIs support direct mode and coordinator mode, specified by the
use_coordinatorargument in the API constructor. We also provide anis_primary_rankproperty to help users understand which rank should feed data and collect outputs.Without coordinator, all ranks are treated as user-managed ranks, and users need to handle load balancing between different DP/EP ranks. Every rank's
is_primary_rankreturns true: the API does not decide which rank should receive which prompts or which rank should emit output. Users must split data across different DP/EP ranks, ensure consistent inputs across TP/PP/CP ranks, and gather/aggregate results from different DP/EP ranks. If users do not shard inputs correctly, they may duplicate work or violate TP/PP/EP/DP group expectations.With coordinator, the coordinator manages load balancing. Users feed data on the coordinator (primary) rank and collect output on that rank.
is_primary_rankreturns true only on the coordinator rank, which is global rank 0. Online serving mode requiresuse_coordinator=Truewhen DP/EP size is greater than 1.Lifecycle methods (
pause/unpause/suspend/resume) are only meaningful in coordinator mode. They raiseRuntimeErrorin direct mode.Examples
Here we list some common examples, for details check
examples/inferenceOffline Sync Generation With Coordinator
Concurrent Async Generation With Multiple Prompts
Programmatic OpenAI-Compatible Server
PR review
The major files to review are newly added examples in
examples/inferenceand the high level implementations inmegatron/inference, the rest are most test coverage and doc changes.Contribution process
Pre-checks
Code review
Feel free to message or comment the @mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!
All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.
Step 1: Mark PR as "Ready for Review"
.github/CODEOWNERS.Final Review might get declined if these requirements are not fulfilled.
Step 2: Final Review
For PRs that change
megatron/core, once all expert reviewers have approved, theFinal Reviewlabel is applied automatically and final reviewers are assigned.For PRs outside
megatron/core, this step is skipped.Step 3: Approved
Once all required reviewers have approved, the
Approvedlabel is applied automatically.Merge
Any member of mcore-engineers will be able to merge your PR.
For MRs into `dev` branch
The proposed review process for `dev` branch is under active discussion.MRs are mergable after one approval by either
eharper@nvidia.comorzijiey@nvidia.com.