Skip to content

Fix fp8_linear compilability - #47623

Merged
IlyasMoutawwakil merged 4 commits into
mainfrom
fix-fp8-compile
Jul 29, 2026
Merged

Fix fp8_linear compilability #47623
IlyasMoutawwakil merged 4 commits into
mainfrom
fix-fp8-compile

Conversation

@IlyasMoutawwakil

Copy link
Copy Markdown
Member

What does this PR do?

is_deepgemm_loadable does not use the double hop allow_in_graph nor assume_constant_result because i thought i only used it in the already protected loader utils, but it's also used in the fp8_linear path which makes it break it during compilation. In this PR i tag every util that returns a trace time python constant with assume_constant_result and add regression tests.

Code Agent Policy

The Transformers repo is currently being overwhelmed by a large number of PRs and issue comments written by
code agents. These often are low-quality, or fix extremely minor issues that occur rarely or never in practice.
As a result, we're instituting a rule that first-time contributors should not use code agents to submit PRs or issues.
We'd also ask autonomous "OpenClaw"-like agents not to open any PRs or issues.

Issues/PRs from first-time contributors that violate this rule will probably just be closed without review, and we
might block you, especially if you open more than one or appear to be deliberately ignoring this. We especially do not
want new contributors to jump in on random issues to contribute an agent-written fix. This creates lots of noise
for reviewers and other users and will almost certainly get you blocked.

For more information, please read CONTRIBUTING.md.

  • (First-time contributors only): I confirm that this PR description and code is not written by an LLM or code agent

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline and the
    Pull Request checks?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes according to the guidelines?
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@IlyasMoutawwakil
IlyasMoutawwakil requested a review from vasqu July 29, 2026 09:41
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Comment on lines +427 to +436
def test_linear_rejects_float32_scales_on_sm100(self):
# Regression for #47030: rounding a checkpoint's float32 scales up to UE8M0 without requantizing
# silently corrupts the output on SM100. The forward must reject them before the kernel runs (so
# `fp8_linear` falls back to Triton) — no kernel op should execute.
input = torch.randn(4, 128, dtype=torch.bfloat16, device=torch_device)
weight = torch.randn(256, 128, device=torch_device).to(torch.float8_e4m3fn)
weight_scale = torch.ones(2, 1, dtype=torch.float32, device=torch_device)
with self._bundle(is_sm100=True) as captured:
with self.assertRaisesRegex(NotImplementedError, "float32 scale-factor path"):
deepgemm_fp8_fp4_linear(input, weight, weight_scale, block_size=(128, 128))

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.

also proposing a regression test for #47030 which was already fixed

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Gotcha, yea fair enough, let's close after merging this PR please

@IlyasMoutawwakil

Copy link
Copy Markdown
Member Author

@askserge review this pr

@sergereview sergereview Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🤗 Serge says:

This PR cleanly fixes the fp8_linear compilability issue by replacing the fragile allow_in_graph + module-global double-hop for is_sm100() with a direct @torch._dynamo.assume_constant_result decorator, and applies the same decorator to is_deepgemm_loadable() and is_sonicmoe_loadable(). The new SM100 float32-scale guard in fp8_linear correctly routes around the silent-corruption path from #47030, and the added regression tests cover both compile-safety and the backstop rejection. The changes are focused, well-documented, and consistent with the existing assume_constant_result usage in moe.py.

serge v0.1.0 · model: moonshotai/Kimi-K2.7-Code · 32 LLM turns · 36 tool calls · 41.6s · 878536 in / 3872 out tokens

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Smaller comments but overall good, pls double check whether older torch really have that as well 😬

_IS_SM100 = torch.cuda.get_device_capability()[0] >= 10


@torch._dynamo.assume_constant_result

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Wow I didnt know that existed, is it safe for the older torch versions as well? (does it exist)

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.

yes it's safe, we also use it in moe.py

Comment on lines +427 to +436
def test_linear_rejects_float32_scales_on_sm100(self):
# Regression for #47030: rounding a checkpoint's float32 scales up to UE8M0 without requantizing
# silently corrupts the output on SM100. The forward must reject them before the kernel runs (so
# `fp8_linear` falls back to Triton) — no kernel op should execute.
input = torch.randn(4, 128, dtype=torch.bfloat16, device=torch_device)
weight = torch.randn(256, 128, device=torch_device).to(torch.float8_e4m3fn)
weight_scale = torch.ones(2, 1, dtype=torch.float32, device=torch_device)
with self._bundle(is_sm100=True) as captured:
with self.assertRaisesRegex(NotImplementedError, "float32 scale-factor path"):
deepgemm_fp8_fp4_linear(input, weight, weight_scale, block_size=(128, 128))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Gotcha, yea fair enough, let's close after merging this PR please

@IlyasMoutawwakil
IlyasMoutawwakil added this pull request to the merge queue Jul 29, 2026
Merged via the queue into main with commit 1922590 Jul 29, 2026
115 checks passed
@IlyasMoutawwakil
IlyasMoutawwakil deleted the fix-fp8-compile branch July 29, 2026 15:21
stevhliu pushed a commit to stevhliu/transformers that referenced this pull request Jul 30, 2026
* assumle the loadability util a constant and tests

* fix

* test for 47030

* fix
Sainava pushed a commit to Sainava/Sai-transformers that referenced this pull request Aug 3, 2026
* assumle the loadability util a constant and tests

* fix

* test for 47030

* fix
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.

3 participants