Skip to content

Studio: packed nvfp4 / mxfp8 layers follow a module dtype cast - #17131

Merged
danielhanchen merged 1 commit into
mainfrom
studio-packed-block-follow-module-cast
Nov 8, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
studio-packed-block-follow-module-cast

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #17067.

Problem

PackedBlockLinear keeps its nvfp4 / mxfp8 weight as integer buffers and widens it per forward into self.compute_dtype, a plain attribute set at load. A module-wide model.to(torch.float16) (or .half(), .float(), .to(torch.float32)) casts the bias and every surrounding module but leaves that attribute on the load dtype, so the next forward hands F.linear an fp16 input and a bf16 weight:

RuntimeError: self and mat2 must have the same dtype, but got Half and BFloat16

The dense layer this replaces (the pre-#17067 load) forwards fine after the same cast.

Fix

PackedBlockLinear._apply runs the cast function on an empty tensor of the current compute dtype after the normal _apply, and adopts the result when it is bf16 / fp16 / fp32. The packed codes and scales stay uint8 / int32 (they are integer, so Module.to(dtype) never touches them), and the widen goes straight from the codes to the new dtype, which is bit-identical to a dense load in that dtype. Moves (.to(device)) and other _apply users keep the dtype as is. No new buffer, so the state dict is unchanged.

Tests

test_a_cast_packed_model_forwards_like_a_dense_load_in_that_dtype (nvfp4 + mxfp8, fp16 + fp32, .to() + .half() / .float()): the widened weight is bitwise the dense load's in that dtype and the forward runs in it. test_module_casts_and_moves_never_touch_the_packed_buffers now also checks the compute dtype follows the cast and returns to the exact load weight on the way back.

origin/main this branch
tests/test_diffusion_packed_block.py (CPU) 9 failed, 25 passed 34 passed, 3 skipped

torch.compile: a cast layer under fullgraph=True forward + backward has 0 graph breaks and matches eager exactly in bf16, fp16 and fp32 (the compute dtype is a guarded constant, so a cast recompiles once, as any dtype change does).

GPU (Kaggle T4, torch 2.11, triton 3.6), one packed nvfp4 layer, model.to(dtype), torch.compile(fullgraph=True, backend="inductor") forward + backward:

dtype origin/main this branch
fp16 RuntimeError: ... Half and BFloat16 0 graph breaks, equal to eager
fp32 RuntimeError: ... Float and BFloat16 0 graph breaks, equal to eager

tests/test_diffusion_packed_block.py on that GPU (CUDA Triton widen cases included): 37 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:26.056985Z 19ff410 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. Can't wait for the next one!

Reviewed commit: 19ff410e68

ℹ️ 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 04cd919 into main Nov 8, 2026
@danielhanchen
danielhanchen deleted the studio-packed-block-follow-module-cast branch November 8, 2026 17:36
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