Skip to content

Studio: skip QKV fusion when projections are kept packed - #17132

Merged
danielhanchen merged 2 commits into
mainfrom
studio-fuse-qkv-skip-packed-projections
Nov 8, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-fuse-qkv-skip-packed-projections

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #17067.

Problem

Max speed fuses QKV projections through diffusers' fuse_qkv_projections, which reads each projection's .weight and concatenates them into new dense to_qkv / to_added_qkv Linears. The guard in _fuse_qkv (and the matching fused_qkv_added_bytes estimate) only recognised NativeWeightOnlyLinear. A PackedBlockLinear from #17067 materialises a dense matrix from its .weight property, so a packed nvfp4 / mxfp8 transformer got persistent dense QKV copies next to its packed buffers, exactly when packed storage was picked to keep resident memory down.

Reproduced with a tiny real FluxTransformer2DModel whose Q/K/V are packed: _fuse_qkv returned True and added a dense transformer_blocks.0.attn.to_qkv (96 x 32).

Fix

One predicate, _quantised_projections(pipe), used by both _fuse_qkv and fused_qkv_added_bytes: True when any denoiser (every DiT, and the pipe's U-Net, which the pipe-level fuse also reaches) holds a native weight-only Linear or a packed block Linear. Fusion is then skipped with the existing log line, and the headroom estimate stays at 0 for such pipes. Dense pipes fuse as before.

Tests

  • test_qkv_fusion_skips_packed_projections: a packed denoiser is not fused, a dense one still is.
  • test_qkv_fusion_of_a_packed_flux_builds_no_dense_to_qkv: tiny real FluxTransformer2DModel with packed Q/K/V, byte count unchanged and no to_qkv / to_added_qkv module after _fuse_qkv.
origin/main this branch
the two tests above (CPU) 2 failed 2 passed
test_diffusion_packed_block.py, test_diffusion_native_quant.py, test_diffusion_drop_unfused_qkv.py, test_diffusion_speed.py, test_diffusion_deferred_headroom.py 430 passed, 8 skipped, 1 failed (test_unpinned_torchao_host_copy_is_a_distinct_wrapper_over_the_same_data, a torchao version mismatch on this host, unrelated and the same on main)

No runtime path inside a compiled region changes: the check runs once at optimisation time in eager.

tests/test_diffusion_packed_block.py on a Kaggle T4 (CUDA cases included): 31 passed.

@danielhanchen danielhanchen self-assigned this Nov 8, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-08T09:53:38.780112Z e130671 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: e130671f6c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit da0df6a into main Nov 8, 2026
@danielhanchen
danielhanchen deleted the studio-fuse-qkv-skip-packed-projections branch November 8, 2026 14:57
danielhanchen added a commit that referenced this pull request Nov 8, 2026
…ichever rank fails first (#17151)

## Summary

`Repo tests (CPU, rest)` fails on some PRs at a test #15880 added
yesterday. Today that includes #17129 and #17132, neither of which
touches context parallelism:

```
tests/test_remote_moe_shims.py:920: in test_without_context_parallelism_idle_experts_stall_ddp
E   AssertionError: Regex pattern did not match.
E     Expected regex: 'not close|finished reduction|did not receive grad'
E     Actual message: '-- Process 1 terminated with the following error:
      ...
      File "tests/test_remote_moe_shims.py", line 895, in _moe_ddp_worker
        assert got.grad is not None, name  # a zero gradient, never None, for DDP
    AssertionError: experts.0.gate_proj.weight
```

The test is a negative control. With context parallelism off, each rank
leaves the experts its peer routes to with a `None` gradient, so DDP
without `find_unused_parameters` never reduces that bucket. The test
expects the run to fail, and it does, every time. Which error the test
sees depends on which of the two spawned ranks fails first:

- the rank that **used** an expert holds an unreduced local gradient,
and `assert_close` fails with `not close`, which the regex accepts;
- the rank that left the **same** expert idle reaches `assert got.grad
is not None` first, and that assertion's message is just the parameter
name, which the regex does not accept.

`torch.multiprocessing.spawn` reports whichever process dies first. So
the test passes or fails on scheduling, while the product behaviour it
checks is identical either way. Context parallelism itself is correct.
The positive test next to it, with context parallelism on, passes.

## Change

`tests/test_remote_moe_shims.py`, test only:

- The worker's gradient assertion says what it found: `f"{name}: no
gradient"`.
- The negative control's regex also accepts `no gradient`, with a
comment on why both ranks' errors are the same finding.

The control still fails if context-parallel-off training ever stops
stalling DDP, because then nothing raises at all.

## Testing

| | result |
|---|---|
| main, with rank 0 delayed 3 s before its checks so rank 1 fails first
(the CI case) | fails: `Regex pattern did not match`, `Process 1
terminated` |
| this branch, same delay | passes |
| this branch, `tests/test_remote_moe_shims.py` | 49 passed |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant