fix(deepseek-v4): restore batch axis for packed-sequence (THD) forward#2651
Open
akoumpa wants to merge 1 commit into
Open
fix(deepseek-v4): restore batch axis for packed-sequence (THD) forward#2651akoumpa wants to merge 1 commit into
akoumpa wants to merge 1 commit into
Conversation
NVBugs: 6329577 Signed-off-by: Alexandros Koumparoulis <akoumparouli@nvidia.com>
Contributor
Author
|
/ok to test 0862c83 |
HuiyingLi
approved these changes
Jun 20, 2026
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.
NVBug: 6329577
What
Add the missing leading batch dimension to
inputs_embedsinDeepseekV4Model.forwardwhen the input arrives in packed-sequence (THD) layout, before the
hc_multexpansion.Why
Packed-sequence finetuning of DeepSeek-V4-Flash crashes on the first optim step in the model
forward (NVBugs 6329577):
at
nemo_automodel/components/models/deepseek_v4/model.py:450:Root cause: the THD packed path (
make_cp_batch_and_ctx(use_te=True)->process_input_for_thd) collapses the batch dimension, handing the model a rank-1input_idsof shape[T].embed_tokens([T])then yields a rank-2[T, H]inputs_embeds,so
unsqueeze(2)->[T, H, 1]andexpand(-1,-1,hc_mult,-1)tries to resize the (non-singleton)hidden dim to
hc_multand fails. The model already restores the batch dim on the OUTPUT side(
compute_lm_head_logits(is_thd=True)doesunsqueeze(0)->[1, T, V]); the input side justlacked the symmetric up-rank. (The original NVBug "suggested fix" was a no-op — identical to the
existing code — and did not address the actual rank mismatch.)
How
In
DeepseekV4Model.forward, after computinginputs_embedsand before the hc_mult expand:This is a no-op for the normal BSHD
[B, S, H]path and mirrors the existing output-side THDrestoration; downstream
position_ids(1-D) andseq_lens(1-D) up-ranks were already present.7 lines added, 0 removed, 1 file.
How tested
hc_mult=4,hidden_size=512) that feedsinput_idsthrough the realprocess_input_for_thdto produce the exact rank-1[T]layout:model.py:450(Tensor sizes: [512, 512, 1]).[1, 512, 256]=[1, T, vocab], no NaN.pytest tests/unit_tests/models/deepseek_v4/test_dsv4_model_smoke.py-> 17 passed(incl. THD / forward / backward smoke tests). No regressions.