Skip to content

Qwen image latent pack unpack - #410

Merged
Flink-ddd merged 2 commits into
RL-Align:test-qwenimagefrom
nodeeeeee:qwen-image-latent-pack-unpack
Nov 20, 2026
Merged

Flink-ddd merged 2 commits into
RL-Align:test-qwenimagefrom
nodeeeeee:qwen-image-latent-pack-unpack

Conversation

@nodeeeeee

@nodeeeeee nodeeeeee commented Sep 12, 2026 •

Copy link
Copy Markdown

Add Qwen-Image latent pack/unpack kernels

Adds CUDA, Triton, and PyTorch backends with forward/backward support for FP32, FP16, and BF16. Pack converts spatial latents into tokens by grouping 2x2 regions across channels; unpack restores the spatial layout. Both preserve values and bit patterns exactly.

CUDA uses a two-stage shared-memory transpose with a 32x32 data tile and 32x8 threads. Autograd executes the inverse kernel for backward. Backend registration preserves existing dispatch behavior.

Validation passed 241 tests. Numerical correctness was verified against the PyTorch reference for pack/unpack outputs and gradients using zero tolerances and byte-for-byte equality. Batch invariance was tested by comparing the first sample processed alone with the same sample processed in batches of 1, 2, 4, and 8. Tests also cover round trips, edge shapes, special values, CUDA streams, fallback, and build configuration. Recorded sanitizer runs reported zero memory errors and race hazards; three launch configurations passed 90 cases.

H100 measurements cover 36 configurations: C=16, three image sizes, batches 1/2/4/8, and three dtypes. The table shows geometric-mean speedups over PyTorch; values above 1 are faster. CUDA Graph ranges show minimum-to-maximum speedups. Eager backward remains slightly slower. Measurements predate the readability refactor and do not represent end-to-end training speed.

Operation CUDA Graph CUDA Graph range CUDA eager events CUDA eager wall time Triton Graph
Pack forward 1.337x 1.050-1.609x 1.092x 1.091x 0.543x
Unpack forward 1.646x 1.294-1.975x 1.096x 1.095x 0.958x
Pack backward 1.644x 1.299-1.972x 0.945x 0.945x 0.957x
Unpack backward 1.337x 1.046-1.606x 0.956x 0.956x 0.543x

#386

@coderabbitai

coderabbitai Bot commented Sep 12, 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: 7f6214a3-f02c-4198-97ac-3d53978aa4d3

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

@Flink-ddd Flink-ddd added multimodal Features, bugs, or optimizations specific to multimodal support. platform: cuda Specific optimizations or bugs in NVIDIA graphics cards (such as FlashInfer, TMA optimizations) labels Sep 12, 2026
@nodeeeeee
nodeeeeee changed the base branch from test to test-qwenimage October 1, 2026 11:26

@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.

The permutation logic looks reasonable, but please resolve the merge conflicts and migrate the implementation, tests, registry, and build wiring to the canonical paths from #497. Please also fix the lint and DCO checks, then rerun the bitwise forward and backward tests on the updated branch before merging.

Add the Qwen-Image latent pack/unpack permutation (RFC RL-Align#386) with CUDA,
Triton and PyTorch backends for FP32/FP16/BF16. Forward and backward are
bit-exact permutations; autograd runs the inverse kernel for backward.

Migrated to the RL-Align#497 layout: CUDA source under
csrc/cuda/model_specific/qwenimage, backends under
rl_engine/backends/{cuda,shared/triton}/model_specific/qwenimage, the
reference and shape contract under rl_engine/reference/model_specific/qwenimage,
shared autograd in rl_engine/ops/packing, registry entries in
rl_engine/runtime/registry.py, bindings in csrc/bindings/ops.cpp, build
wiring in build_tools/extensions.py, and tests under tests/models/qwenimage.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: nodeeeeee <[email protected]>
@nodeeeeee
nodeeeeee force-pushed the qwen-image-latent-pack-unpack branch from 446b80a to 93e8c1a Compare November 6, 2026 09:22
@nodeeeeee
nodeeeeee requested a review from ryankert01 as a code owner November 6, 2026 09:22
@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

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

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

⚠️ 4 warning(s) occurred during review.


test · low

📄 tests/models/qwenimage/test_latent_pack_unpack.py (L57-L57)

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

For the PYTORCH / PYTORCH_CPU parameterizations of ops, pack/unpack resolve to the backend registered for OpBackend.PYTORCH_LATENT_*, which is the same reference module (rl_engine.reference.model_specific.qwenimage.latent_pack_unpack) used here to compute expected. assert_bits(packed, expected) therefore compares the reference against itself for those parameters and provides no independent signal — only the CUDA/TRITON parametrizations actually validate the kernel output. Consider dropping the PYTORCH parametrizations from this exactness test (they are still exercised implicitly) so the acceptance check only mounts where an independent implementation is compared.


⚠️ Warnings:

  • rl_engine/ops/packing/latent_permutation.py,rl_engine/runtime/registry.py (review_limit_reached): reached the aggregate token budget before finishing; the grace round completed the task
  • build_tools/extensions.py,csrc/bindings/ops.cpp,csrc/cuda/model_specific/qwenimage/latent_pack_unpack.cu,rl_engine/_C.pyi,rl_engine/backends/cuda/model_specific/qwenimage/latent_pack_unpack.py (review_limit_reached): reached the aggregate token budget before finishing; the grace round completed the task
  • rl_engine/reference/model_specific/qwenimage/latent_pack_unpack.py,tests/models/qwenimage/test_latent_pack_unpack.py (review_limit_reached): reached the aggregate token budget before finishing; the grace round did not complete the task
  • rl_engine/reference/model_specific/qwenimage/latent_pack_unpack.py (token_budget_reached): stopped group "rl_engine/reference/model_specific/qwenimage/latent_pack_unpack.py,tests/models/qwenimage/test_latent_pack_unpack.py" mid-review: used 779033 tokens exceeds budget 500000

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

@nodeeeeee 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.

@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

@Flink-ddd
Flink-ddd merged commit 3887357 into RL-Align:test-qwenimage Nov 20, 2026
3 checks passed
fusheng-ji added a commit to fusheng-ji/RL-Kernel that referenced this pull request Nov 20, 2026
…L-Align#538

Resolve additive conflicts in build_tools/extensions.py, csrc/bindings/ops.cpp
and rl_engine/runtime/registry.py by keeping both the final_adaln_out
(token_sum) entries and the upstream latent pack/unpack entries.

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

multimodal Features, bugs, or optimizations specific to multimodal support. new-model platform: cuda Specific optimizations or bugs in NVIDIA graphics cards (such as FlashInfer, TMA optimizations) qwen

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants