Simple and stable Inference APIs#4697
Open
YangFei1990 wants to merge 18 commits intoNVIDIA:mainfrom
Open
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>
Contributor
|
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. |
Contributor
Author
|
/ok to test 5f651b7 |
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…ron-LM into inference_apis
Contributor
Author
|
/ok to test 963f663 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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.