Skip to content

fix(models): use physical boundaries for Qwen packed attention - #5630

Closed
yaoyu-33 wants to merge 4 commits into
mainfrom
yuya/qwen3-vl-packed-attention-tail-padding
Closed

yaoyu-33 wants to merge 4 commits into
mainfrom
yuya/qwen3-vl-packed-attention-tail-padding

Conversation

@yaoyu-33

Copy link
Copy Markdown
Contributor

What

  • Pass physical cumulative sequence boundaries to Qwen3-VL core attention when Energon provides logical and padded THD metadata.
  • Keep the original logical/padded metadata for Qwen MRoPE, hybrid GDN, and other decoder consumers.
  • Add focused coverage for tail-only padding and the unpadded identity path.

Why

Energon online packing can produce a pack where only the final cumulative offset differs between logical and physical layouts. Transformer Engine's padded-layout inference does not detect that tail-only difference, so attention backward can interpret a longer physical THD stream with inconsistent logical boundaries and produce exploding gradients or NaNs.

The physical-boundary view is limited to causal core attention. It avoids unsupported explicit padding selection while preserving the richer metadata everywhere else.

Follow-up to #5455.

Validation

  • 54 focused Qwen attention, packed-sequence utility, in-batch packing, and VLM-step unit tests passed.
  • uv run pre-commit run --all-files passed.
  • A matched 5-step 32-GPU BF16 Energon online-packing run completed with finite loss/gradients and zero skipped/NaN iterations.
  • A matched 100-step run then completed all 100 iterations with 100 finite metric records, zero skipped/NaN iterations, and successful model plus dataloader checkpointing.
  • The non-packing control also remained finite.

The runtime validation used 16K sequences, TP1/CP1/PP1/EP32, MBS1/GBS32, and packing_buffer_size=16.

Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
(cherry picked from commit c5c7e7c0a870b0d343f2f187029248e77d960d05)
Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
@copy-pr-bot

copy-pr-bot Bot commented Aug 18, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@yaoyu-33 yaoyu-33 added area:data Dataset builders, preprocessing, and samplers area:model Model implementations and HF bridge logic bug Something isn't working needs-review PR is ready for code review and waiting on a reviewer labels Aug 18, 2026
@yaoyu-33

Copy link
Copy Markdown
Contributor Author

/ok to test 6ff7723

@claude

claude Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

LGTM — focused, correct fix for Qwen3-VL core attention THD boundaries, with direct unit coverage.

Review notes:

  • _get_core_attention_packed_seq_params uses a shallow copy() then reassigns attributes; it does not mutate the original tensors, and test_core_attention_packed_params_use_physical_tail_boundary asserts the source PackedSeqParams is left intact.
  • Swapping cu_seqlens_q/kv to the padded boundaries makes core attention consistent with the rope path (attention.py:247-255), which already used the padded cumulative offsets.
  • max_seqlen_q/kv are intentionally left unchanged. This is safe: in qwen3_vl_step.py and utils.py the logical and padded boundaries are equal, and in the energon path they differ only at the tail, so max_seqlen still bounds the largest physical segment.
  • Both branches of the helper are unit-tested (padded -> derived copy; unpadded -> identity return).
  • No typos, no stale docs, and 3rdparty/Megatron-LM/ is untouched.

Suggested test cases

  • No perf tests impacted.
  • test_core_attention_packed_params_use_physical_tail_boundary (added — verifies padded boundaries are promoted, padded fields cleared, pad_between_seqs=False, original untouched)
  • test_core_attention_packed_params_preserve_unpadded_metadata (added — verifies identity return when no padded layout)

@yaoyu-33 yaoyu-33 added the needs-more-tests Requires additional L0 and L1 test coverage before merge label Aug 18, 2026
Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
@yaoyu-33

Copy link
Copy Markdown
Contributor Author

/ok to test b8af67f

…attention-tail-padding

Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
@yaoyu-33
yaoyu-33 marked this pull request as ready for review August 26, 2026 23:42
@yaoyu-33

Copy link
Copy Markdown
Contributor Author

/ok to test 7304302

@claude

claude Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

LGTM - focused, well-reasoned fix for Qwen3-VL THD core attention.

What the change does: _get_core_attention_packed_seq_params shallow-copies PackedSeqParams and passes the physical (padded) THD boundaries to the TE core-attention kernel, sidestepping the padded-layout inference in TE that mishandles the final cumulative offset. The original params are left intact for the surrounding hybrid-layer logic.

Correctness review (no blocking issues):

  • copy(packed_seq_params) correctly preserves the original - verified by test_core_attention_packed_params_use_physical_tail_boundary asserting the source cu_seqlens_q / cu_seqlens_q_padded are unchanged. Tensors are only reassigned, never mutated in place, so sharing references is safe.
  • The cu_seqlens_kv_padded is None fallback to cu_seqlens_q_padded is reasonable.
  • The early return on cu_seqlens_q_padded is None is covered by the unpadded test.
  • Both consuming call sites (checkpointed forward and static-batching kernel) use the new params; the dynamic-batching path does not consume packed_seq_params, so no change is needed there - correct.

Minor (non-blocking): Tests cover the helper in isolation but not that forward() actually threads core_attention_packed_seq_params into the TE call. That path needs GPU/TE so a unit test is not practical - acceptable.

Suggested test cases:

  • test_core_attention_packed_params_use_physical_tail_boundary (added - verifies physical boundary swap + original preserved)
  • test_core_attention_packed_params_preserve_unpadded_metadata (added - verifies identity return when no padded layout)
  • No perf tests impacted (change is in models/qwen_vl/modelling_qwen3_vl/attention.py; no scripts/performance/configs/ recipes touched).

@lkhphuc

lkhphuc commented Aug 27, 2026

Copy link
Copy Markdown

Hi, why is this closed without merged? Is the problem not exist anymore?

This branch was previously deployed

2 inactive deployments
public — 73043026 Deployed Aug 27, 2026 by copy-pr-bot[bot] via release / finalize / publish-docs #5099
test — 73043026 Deployed Aug 26, 2026 by copy-pr-bot[bot] via cicd-wait-in-queue #21079
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:data Dataset builders, preprocessing, and samplers area:model Model implementations and HF bridge logic bug Something isn't working needs-more-tests Requires additional L0 and L1 test coverage before merge needs-review PR is ready for code review and waiting on a reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants