Skip to content

[WS2][CUDA][Qwen-Image] Add sequence sharding/gather and arbitrary-length support - #705

Merged
Flink-ddd merged 1 commit into
RL-Align:test-qwenimagefrom
MaTwickenham:matwickenham/qwenimage-sp
Nov 17, 2026
Merged

Flink-ddd merged 1 commit into
RL-Align:test-qwenimagefrom
MaTwickenham:matwickenham/qwenimage-sp

Conversation

@MaTwickenham

Copy link
Copy Markdown

Summary

Implements two Qwen-Image WS2 work items from #386:

  • sp_shard_gather: Contiguous image-token sharding with co-sharded three-axis RoPE positions and rank-ordered gathering.
  • sp_arbitrary_length: Support for non-divisible sequence lengths, with padding confined to communication buffers.

Implementation

  • Add shard_image_sequence() and gather_sequence() with first-order autograd support.
  • Preserve original global token positions and byte-exact forward round trips across SP ranks.
  • Support SP sizes 1/2/4/8, including uneven and empty local shards.
  • Reuse TorchDistributedDeterministicCollective for NCCL/Gloo transport and fixed-order FP32 gradient accumulation.
  • Add distributed correctness tests, a benchmark runner, and operator documentation.

Validation

Validated on 4× NVIDIA A100-SXM4-80GB, Python 3.11.10, PyTorch 2.9.0+cu130, CUDA 13.0, NCCL 2.27.7.

65 tests passed: 26 sequence-parallel tests and 39 existing collective regression tests.

Coverage includes:

  • SP=1/2/4 on Gloo and NCCL, with FP32/BF16.
  • Reference sequence lengths 4096, 6032, and 6889.
  • Byte-exact shard/gather recovery, position preservation, and batch placement.
  • Forward/backward gradient correctness, noncontiguous inputs, empty shards, and communication padding.
OMP_NUM_THREADS=1 NCCL_DEBUG=WARN python -m pytest \
  tests/distributed/sp/test_sequence_parallel.py \
  tests/distributed/collectives/test_transport_deterministic_collective.py -q

Benchmark

On 4× A100, B=1, S=6889, C=3072, BF16, with 5 warmup and 20 measured iterations:

SP Gather (ms) Gather Fwd+Bwd (ms)
1 0.057 0.121
2 1.283 3.268
4 1.663 4.849

Measurements include collective setup and use the slowest rank's mean wall time.

Scope

This PR provides the sequence layout and communication primitives for subsequent Qwen-Image SP attention integration. Distributed execution is validated through SP=4; SP=8 currently has layout-level coverage.

Related: #386.

@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0ca9af3a-31ca-4c29-bf9f-8a4184bb7901

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

🔍 OpenCodeReview found 2 issue(s) in this PR.

  • ✅ Successfully posted inline: 0 comment(s)
  • 📋 Routed to summary by policy: 2 comment(s)

⚠️ 2 warning(s) occurred during review.


performance · medium

📄 rl_engine/distributed/algorithms/sequence_parallel.py (L84-L88)

⚠️ GitHub could not post this as an inline comment: Routed to summary (severity medium · category performance)

Every gather/backward constructs a brand-new TorchDistributedDeterministicCollective and closes it immediately. Because a fresh instance is used per call, _validate_matching_capacity and _validate_matching_signature each run an all_gather_object collective on every call (the per-instance signature cache never warms up), and __exit__ -> close() frees the workspace each time, so the workspace is reallocated for every gather/reduce-scatter. For a per-layer SP operator this adds two extra Python-object collectives and a large buffer allocation per call, and it bypasses the repo's hot-path cache (collective_for_group/create_deterministic_collective in rl_engine/distributed/algorithms/collectives.py). Consider caching the collective keyed by (group, device) instead of creating one per invocation (this also inflates the benchmark times, which include collective setup).


documentation · low

📄 benchmarks/distributed/benchmark_sequence_parallel.py (L130-L131)

⚠️ GitHub could not post this as an inline comment: Routed to summary (severity low · category documentation)

These metadata fields only identify CUDA/NCCL. On a ROCm build torch.version.cuda is None while torch.cuda.nccl.version() returns the RCCL version, so a ROCm run is recorded as CUDA/nccl with cuda: null, and no HIP/ROCm field is emitted. That fails to identify the backend that actually executed and makes CUDA vs ROCm runs indistinguishable in the JSON. Consider recording torch.version.hip (and a derived backend label) in addition to the CUDA/NCCL fields.


⚠️ Warnings:

  • benchmarks/distributed/benchmark_sequence_parallel.py,rl_engine/distributed/algorithms/sequence_parallel.py,tests/distributed/sp/__init__.py,tests/distributed/sp/test_sequence_parallel.py (review_limit_reached): reached the aggregate token budget before finishing; the grace round did not complete the task
  • benchmarks/distributed/benchmark_sequence_parallel.py (token_budget_reached): stopped group "benchmarks/distributed/benchmark_sequence_parallel.py,rl_engine/distributed/algorithms/sequence_parallel.py,tests/distributed/sp/init.py,tests/distributed/sp/test_sequence_parallel.py" mid-review: used 598839 tokens exceeds budget 500000

@github-actions

Copy link
Copy Markdown

@MaTwickenham The automated OCR review has completed.

Please review and address all automated review bot findings, then push your updates. Maintainers will begin manual review after the bot findings have been addressed.

@MaTwickenham
MaTwickenham force-pushed the matwickenham/qwenimage-sp branch from 046f43a to 557aa3e Compare November 15, 2026 12:40
@Flink-ddd Flink-ddd added platform: cuda Specific optimizations or bugs in NVIDIA graphics cards (such as FlashInfer, TMA optimizations) new-model qwen WS2 labels Nov 17, 2026

@Flink-ddd Flink-ddd left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, the remaining bot suggestions are non-blocking and can be addressed in a follow-up.

@Flink-ddd
Flink-ddd merged commit 48bbad2 into RL-Align:test-qwenimage Nov 17, 2026
3 checks passed
fusheng-ji added a commit to fusheng-ji/RL-Kernel that referenced this pull request Nov 18, 2026


Conflicts in docs/.nav.yml and docs/operators/README.md: both sides added
a Qwen-Image operator page at the same place; both entries are kept.

Signed-off-by: Wenbo Ji <[email protected]>
fusheng-ji added a commit to fusheng-ji/RL-Kernel that referenced this pull request Nov 20, 2026
shard_image_sequence (from RL-Align#705 on test-qwenimage) assigns a 2-tuple and then
a 3-tuple to expected_shape, which mypy 2.4 rejects in the CI linting job.
Declare it as tuple[int, ...]; behavior is unchanged.

Signed-off-by: Wenbo Ji <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-model platform: cuda Specific optimizations or bugs in NVIDIA graphics cards (such as FlashInfer, TMA optimizations) qwen WS2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants