Repository navigation
Studio: packed nvfp4 / mxfp8 layers follow a module dtype cast - #17131
Merged
Merged
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Follow-up to #17067.
Problem
PackedBlockLinearkeeps its nvfp4 / mxfp8 weight as integer buffers and widens it per forward intoself.compute_dtype, a plain attribute set at load. A module-widemodel.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 handsF.linearan fp16 input and a bf16 weight:The dense layer this replaces (the pre-#17067 load) forwards fine after the same cast.
Fix
PackedBlockLinear._applyruns 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, soModule.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_applyusers 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_buffersnow also checks the compute dtype follows the cast and returns to the exact load weight on the way back.origin/maintests/test_diffusion_packed_block.py(CPU)torch.compile: a cast layer under
fullgraph=Trueforward + 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:origin/mainRuntimeError: ... Half and BFloat16RuntimeError: ... Float and BFloat16tests/test_diffusion_packed_block.pyon that GPU (CUDA Triton widen cases included): 37 passed.