Skip to content

FP8 fp16 dequant: keep autograd for a trainable weight or scale - #15354

Merged
danielhanchen merged 2 commits into
mainfrom
dh/s7-fp8-fp16-dequant-autograd
Oct 26, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
dh/s7-fp8-fp16-dequant-autograd

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

_scale_mul (used by weight_dequant for per-tensor and rowwise FP8 scales) rounds fp16 output once by calling torch.mul(x16, s_fp32, out = ...), added in #14677 to stop fp16 scales underflowing / overflowing. out= ops have no autograd, so when the weight or the scale requires grad and grad mode is on, eager dequantization now raises:

RuntimeError: mul(): functions with out=... arguments don't support automatic differentiation, but one of the arguments requires grad.

Before #14677 the same call backpropagated. Under torch.compile the code already takes the differentiable fp32 expression, so eager and compiled disagreed. Frozen-base LoRA is unaffected (neither input requires grad); the failure needs a trainable FP8 weight or scale.

Fix

A trainable weight or scale (grad mode on) takes the same fp32 expression the compiled path uses, (x.float() * s.float()).to(fp16), which rounds once and gives the same bits as the out= path. Frozen inputs keep the out= path (no fp32 temporary).

Tests

tests/test_fp8_fp16_scale_rounding.py::test_fp16_dequant_keeps_autograd_for_trainable_inputs (per-tensor and rowwise scale, trainable scale or weight): output equals the fp64 oracle rounded to fp16, backward runs, and the scale gradient is the sum of the weights it covers. On main all 4 cases raise the error above; with this change they pass. test_fp8_fp16_scale_rounding.py, test_fp8_exact_f16_round.py, test_fp8_fp16_lora_dtype.py on CPU: 27 passed, 17 skipped (GPU-only).

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 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-10-26T10:55:45.554046Z bcd715b 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: bcd715b32c

ℹ️ 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 d9b50da into main Oct 26, 2026
27 of 30 checks passed
@danielhanchen
danielhanchen deleted the dh/s7-fp8-fp16-dequant-autograd branch October 26, 2026 12:00
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