fix(inference): size DynamicInferenceContext KV layer_map for non-uniform PP#4775
Draft
athitten wants to merge 2 commits into
Draft
fix(inference): size DynamicInferenceContext KV layer_map for non-uniform PP#4775athitten wants to merge 2 commits into
athitten wants to merge 2 commits into
Conversation
…form PP Replace num_layers // pp_size with get_num_layers_to_build so the attention layer count matches TransformerBlock on each PP rank. Embedding/loss pipeline splits and uneven first/last stages otherwise left layer_map too short, causing KeyError in append_key_value_cache.
Author
|
/ok to test 0825f05 |
Add TestDynamicContext.test_uneven_decoder_pp_layer_map_matches_get_num_layers_to_build for num_layers_in_first/last_pipeline_stage with PP=2. Asserts DynamicInferenceContext num_attention_layers and identity layer_map length match get_num_layers_to_build on each rank, unlike uniform num_layers // pp_size.
Author
|
@NVIDIA/mcore-oncall can you help start CI on this PR ? Thanks. Looks like I dont have permission |
santhnm2
reviewed
May 14, 2026
Comment on lines
+371
to
+373
| vp_sz = parallel_state.get_virtual_pipeline_model_parallel_world_size() | ||
| if vp_sz is not None and vp_sz > 1: | ||
| vp_stage = parallel_state.get_virtual_pipeline_model_parallel_rank() |
Contributor
There was a problem hiding this comment.
Can we derive this from pg_collection.pp instead?
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 ?
Fixes incorrect sizing of the dynamic inference KV cache layer map for pure Transformer models when pipeline parallelism does not give every rank the same number of decoder layers (e.g. embedding/loss accounted in the PP split, uneven first/last stages, or explicit PP layouts).
Problem
DynamicInferenceContextset:num_attention_layers = model_config.num_layers // pipeline_model_parallel_sizeand built layer_map as
{0:0, 1:1, …, num_attention_layers-1}. That matches uniform PP only.When
account_for_embedding_in_pipeline_split/account_for_loss_in_pipeline_split,num_layers_in_first_pipeline_stage/num_layers_in_last_pipeline_stage, orpipeline_model_parallel_layoutis used, the actual number of transformer layers on a rank is given byget_num_layers_to_build()(same asTransformerBlock). It can be larger thannum_layers // pp_sizeon some ranks.The model then runs attention for global layer indices that exceed
len(layer_map) - 1, andappend_key_value_cachedoesself.layer_map[layer_number - 1]leading to**KeyError**(e.g. missing key 5), often surfaced during warmup or larger batch scheduling.Hit this error while running evaluation on
Qwen3-235B-A22BSolution
For the non-hybrid (pure Transformer) branch, compute:
num_attention_layers = get_num_layers_to_build(model_config, vp_stage=..., pp_rank=None)with
vp_stagederived from virtual PP world size / rank when VPP > 1, matchingTransformerBlock.Import
get_num_layers_to_buildinside the branch to limit import-time coupling withtransformer_block(which pulls in inference contexts).Before fix:
After fix in this PR:
The error does not occur anymore and evaluation runs fine on
Qwen3-235B-A22BIssue tracking
For PRs from open-source community contributors:
Linked issue:
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.