-
Notifications
You must be signed in to change notification settings - Fork 503
fix(qwen3-vl): per-segment mRoPE + vision under CP + THD packing #1308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Zhichenzzz
merged 1 commit into
zhichen/qwen3-vl-thd-miles-hijack
from
fix/1296-qwen3vl-cp-mrope
Jun 19, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| """CPU unit test for the Qwen3-VL CP+THD packed mRoPE reconstruction (issue #1296). | ||
|
|
||
| Under context parallelism each rank's THD row holds only its zigzag chunks of every packed | ||
| segment. `_reassemble_full_row` de-interleaves the all-gathered per-rank rows back to the | ||
| full natural-order row so per-segment MRoPE positions can be rebuilt and re-sliced. This | ||
| test checks that reconstruction is the exact inverse of `slice_with_cp` (the function miles | ||
| uses to shard the tokens), and that re-slicing with `_natural_to_zigzag_slice` round-trips. | ||
| """ | ||
|
|
||
| import pytest | ||
| import torch | ||
| import torch.nn.functional as F | ||
|
|
||
| from miles_plugins.models.qwen3_vl import _natural_to_zigzag_slice, _reassemble_full_row | ||
|
|
||
|
|
||
| def _slice_with_cp(tokens, cp_size, cp_rank, pad_value=0): | ||
| """Reference copy of cp_utils.slice_with_cp's THD zigzag slicing (per sample).""" | ||
| token_len = len(tokens) | ||
| chunk = (token_len + 2 * cp_size - 1) // (2 * cp_size) | ||
| pad = 2 * cp_size * chunk - token_len | ||
| if pad: | ||
| tokens = F.pad(tokens, (0, pad), value=pad_value) | ||
| s1, e1 = chunk * cp_rank, chunk * (cp_rank + 1) | ||
| s2, e2 = chunk * (2 * cp_size - cp_rank - 1), chunk * (2 * cp_size - cp_rank) | ||
| return torch.cat([tokens[s1:e1], tokens[s2:e2]]) | ||
|
|
||
|
|
||
| def _build_like_get_batch(sample_lens, cp_size, pad_size=8): | ||
| """Mimic miles get_batch THD+CP packing: per-sample zigzag slice, concat, pad, cu*cp.""" | ||
| samples = [] | ||
| base = 1 | ||
| for L in sample_lens: | ||
| samples.append(torch.arange(base, base + L)) # unique nonzero ids | ||
| base += L | ||
| per_rank = [] | ||
| for r in range(cp_size): | ||
| row = torch.cat([_slice_with_cp(t, cp_size, r) for t in samples]) | ||
| per_rank.append(row) | ||
| cu = [0] | ||
| for t in samples: | ||
| cu.append(cu[-1] + _slice_with_cp(t, cp_size, 0).size(0)) | ||
| final_pad = (pad_size - per_rank[0].size(0) % pad_size) % pad_size | ||
| if final_pad: | ||
| per_rank = [F.pad(row, (0, final_pad), value=0) for row in per_rank] | ||
| cu.append(cu[-1] + final_pad) | ||
| cu = [x * cp_size for x in cu] | ||
| return samples, per_rank, cu | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "cp_size,sample_lens", | ||
| [(2, [10, 7, 13]), (2, [16, 16]), (4, [20, 9, 30, 5]), (2, [3]), (4, [40, 17])], | ||
| ) | ||
| def test_reassemble_is_inverse_of_slice_with_cp(cp_size, sample_lens): | ||
| samples, per_rank, cu = _build_like_get_batch(sample_lens, cp_size) | ||
| local_len = per_rank[0].size(0) | ||
| assert cu[-1] == cp_size * local_len | ||
|
|
||
| full = _reassemble_full_row(per_rank, cu, cp_size) | ||
| assert full is not None and full.numel() == cu[-1] | ||
|
|
||
| # Each real sample's tokens reappear (in order) at the start of its segment. | ||
| for i, t in enumerate(samples): | ||
| seg = full[cu[i] : cu[i + 1]] | ||
| assert torch.equal(seg[: t.numel()], t) | ||
|
|
||
| # Re-slicing the full row per segment recovers exactly each rank's local chunks. | ||
| for r in range(cp_size): | ||
| recon = [] | ||
| for i in range(len(cu) - 1): | ||
| recon.append(_natural_to_zigzag_slice(full[cu[i] : cu[i + 1]], cp_size, r, dim=0)) | ||
| assert torch.equal(torch.cat(recon), per_rank[r]) | ||
|
|
||
|
|
||
| def test_reassemble_bails_on_indivisible_segment(): | ||
| # A segment length not divisible by 2*cp -> None (caller falls back to dense path). | ||
| cu = [0, 6] # 6 not divisible by 2*cp=4 | ||
| gathered = [torch.zeros(3, dtype=torch.long), torch.zeros(3, dtype=torch.long)] | ||
| assert _reassemble_full_row(gathered, cu, cp_size=2) is None |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To ensure that
_AllGatherVisionEmbeddingsKwargShimbehaves identically to the originalAllGatherVisionEmbeddingsclass (e.g., preserving class attributes, static methods, or satisfyingissubclass/isinstancechecks in downstream code), it is safer to have the shim inherit fromoriginstead of being a completely separate, plain class.