Skip to content

Studio: build the ConvRot rotation from its definition - #11807

Merged
danielhanchen merged 3 commits into
mainfrom
studio-convrot-own-hadamard
Sep 25, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-convrot-own-hadamard

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

What

Rewrites the ConvRot rotation helpers in studio/backend/core/inference/diffusion_convrot.py as our own implementation:

  • build_convrot_hadamard now assembles kron(H4, ..., H4) / sqrt(n) from its closed-form sign pattern (entry (i, j) is negative when an odd number of base-4 digits of i ^ j equal 3) instead of repeated kron. The normalizer is an exact power of two.
  • rotate_convrot_activation / rotate_convrot_weight_ are simplified; names, signatures and caching are unchanged, so callers (including the H3 conditioner) are untouched.
  • Comments in diffusion_convrot.py, video_minimax_h3_te.py and the ConvRot test now describe the rotation in neutral terms.

Why

Hosted ConvRot checkpoints were rotated with this exact matrix, so the output must not move. It does not: the new matrix and the rotated activations and weights are bitwise equal to the previous code for sizes 4..4096 in fp32/fp16/bf16 on CPU and CUDA, including non-contiguous inputs (276 checks, 0 mismatches).

Tests

New tests in tests/test_diffusion_convrot.py:

  • bitwise equality with a reference built inline from the kron definition, sizes 4..1024, fp32/fp16/bf16, CPU and CUDA when available
  • exact orthogonality and symmetry in float64
  • activation and weight rotations match an explicit block-diagonal matmul

Results: test_diffusion_convrot.py 84 passed. ConvRot, H3 INT8 and prequant suites (test_video_h3_te_quant, test_diffusion_prequant, test_diffusion_pipeline_prequant, test_diffusion_te_prequant, test_build_prequant_checkpoint, test_prequant_safetensors, test_video_prequant, test_video_minimax_h3_adaln): 429 passed, 1 skipped, 1 failed. The one failure (test_the_read_layer_matches_the_diffusers_pipeline) is a local bitsandbytes import error inside diffusers and fails identically on main. test_sd_cpp_h3_matrix and test_diffusion_h3_trainer: 275 passed, 6 skipped.

Assemble the normalized H4 kron-power Hadamard from its closed-form sign
pattern instead of repeated kron, and simplify the block rotation helpers.
Output is bit-identical to the previous construction for every size and
dtype, so hosted ConvRot checkpoints are unaffected. Comments now describe
the rotation in neutral terms.

Adds tests pinning bitwise equality with an inline kron reference for
sizes 4..1024 in fp32/fp16/bf16 (CPU and CUDA), exact orthogonality, and
agreement of the activation and weight rotations with an explicit block
diagonal.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-24T13:52:36.665001Z 0e8b5e7 Manual request
🔒 Security Review ⚠️ Failed 2026-09-24T13:13:52.296441Z 0107852 PR opened
ℹ️ 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 010785292e

ℹ️ 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".

# a base-4 digit is 3 when both of its bits are set: keep the low bit of each such pair
threes = both & (both >> 1) & 0x5555555555555555
parity = torch.zeros_like(threes)
while bool(threes.any()):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Replace tensor-dependent loop to preserve fullgraph compilation

On a cold Hadamard cache, a compiled ConvRotLinear.forward enters this loop and evaluates bool() on a tensor value, which TorchDynamo cannot capture under the normal fullgraph=True regional compilation configured in diffusion_speed.py. This occurs, for example, when MiniMax-H3 uses the ConvRot INT8 denoiser with the dense text encoder, so the conditioner has not already populated this cache; the first denoiser forward then drops the quantized block to the eager fallback, which this codebase documents as roughly 30× slower. Iterate a fixed number of base-4 digits derived from the Python size instead of using tensor data as the termination condition.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 0e8b5e7: the parity loop now runs a fixed number of base-4 digits taken from the Python size. A cold-cache fullgraph compile of a rotated projection failed with a data-dependent guard error before and traces cleanly now; new test covers it.

The sign parity loop stopped on a tensor value, which Dynamo cannot trace
under fullgraph=True, so a rotated projection whose first forward builds
the matrix failed the regional compile. Loop over the fixed number of
base-4 digits taken from the Python size instead.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 0e8b5e7984

ℹ️ 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 3ed8f68 into main Sep 25, 2026
46 checks passed
@danielhanchen
danielhanchen deleted the studio-convrot-own-hadamard branch September 25, 2026 13:43
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
* Studio: build the ConvRot rotation from its definition

Assemble the normalized H4 kron-power Hadamard from its closed-form sign
pattern instead of repeated kron, and simplify the block rotation helpers.
Output is bit-identical to the previous construction for every size and
dtype, so hosted ConvRot checkpoints are unaffected. Comments now describe
the rotation in neutral terms.

Adds tests pinning bitwise equality with an inline kron reference for
sizes 4..1024 in fp32/fp16/bf16 (CPU and CUDA), exact orthogonality, and
agreement of the activation and weight rotations with an explicit block
diagonal.

* Studio: keep the ConvRot Hadamard build traceable under fullgraph

The sign parity loop stopped on a tensor value, which Dynamo cannot trace
under fullgraph=True, so a rotated projection whose first forward builds
the matrix failed the regional compile. Loop over the fixed number of
base-4 digits taken from the Python size instead.

* Trim ConvRot comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant