pytorch - 💡(How to fix) Fix [inductor] CPU codegen does not preserve explicit float32 to float16 to float32 cast semantics

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Code Example

import torch

def fn(x):
    y = x.to(dtype=torch.float16).to(dtype=torch.float32)
    return y, y.sum(dim=1)

x = torch.arange(20, dtype=torch.float32).reshape(5, 4).t() / 7.0

compiled_fn = torch.compile(fn, backend="inductor", dynamic=False)

eager_y, eager_sum = fn(x)
compiled_y, compiled_sum = compiled_fn(x)

print("eager_y:",eager_y)
print("compiled_y:",compiled_y)
print("max_abs_y:", (eager_y - compiled_y).abs().max())

print("eager_sum:",eager_sum)
print("compiled_sum:",compiled_sum)
print("max_abs_sum", (eager_sum - compiled_sum).abs().max())

---

eager_y: tensor([[0.0000, 0.5713, 1.1426, 1.7139, 2.2852],
        [0.1428, 0.7144, 1.2861, 1.8574, 2.4277],
        [0.2856, 0.8569, 1.4287, 2.0000, 2.5723],
        [0.4285, 1.0000, 1.5713, 2.1426, 2.7148]])
compiled_y: tensor([[0.0000, 0.5714, 1.1429, 1.7143, 2.2857],
        [0.1429, 0.7143, 1.2857, 1.8571, 2.4286],
        [0.2857, 0.8571, 1.4286, 2.0000, 2.5714],
        [0.4286, 1.0000, 1.5714, 2.1429, 2.7143]])
max_abs_y: tensor(0.0008)
eager_sum: tensor([5.7129, 6.4285, 7.1436, 7.8572])
compiled_sum: tensor([5.7143, 6.4286, 7.1429, 7.8571])
max_abs_sum tensor(0.0014)
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

x.to(torch.float16).to(torch.float32) should preserve the intermediate fp16 rounding. In eager mode, the first cast to fp16 rounds the values, and the second cast returns those rounded values back to float32.

However, with torch.compile(..., backend="inductor") on CPU, the compiled result appears to skip or eliminate the intermediate fp16 rounding. The compiled output is closer to the original float32 input than to the eager result.

This is a silent numerical mismatch. It looks related to the existing fp16 cast preservation / CPU precision-cast family, but this repro only uses an explicit cast pair and a simple reduction.

Expected behavior: compiled output should match eager output after the explicit float32 -> float16 -> float32 conversion.

Actual behavior: compiled output differs from eager output.

import torch

def fn(x):
    y = x.to(dtype=torch.float16).to(dtype=torch.float32)
    return y, y.sum(dim=1)

x = torch.arange(20, dtype=torch.float32).reshape(5, 4).t() / 7.0

compiled_fn = torch.compile(fn, backend="inductor", dynamic=False)

eager_y, eager_sum = fn(x)
compiled_y, compiled_sum = compiled_fn(x)

print("eager_y:",eager_y)
print("compiled_y:",compiled_y)
print("max_abs_y:", (eager_y - compiled_y).abs().max())

print("eager_sum:",eager_sum)
print("compiled_sum:",compiled_sum)
print("max_abs_sum", (eager_sum - compiled_sum).abs().max())

Output:

eager_y: tensor([[0.0000, 0.5713, 1.1426, 1.7139, 2.2852],
        [0.1428, 0.7144, 1.2861, 1.8574, 2.4277],
        [0.2856, 0.8569, 1.4287, 2.0000, 2.5723],
        [0.4285, 1.0000, 1.5713, 2.1426, 2.7148]])
compiled_y: tensor([[0.0000, 0.5714, 1.1429, 1.7143, 2.2857],
        [0.1429, 0.7143, 1.2857, 1.8571, 2.4286],
        [0.2857, 0.8571, 1.4286, 2.0000, 2.5714],
        [0.4286, 1.0000, 1.5714, 2.1429, 2.7143]])
max_abs_y: tensor(0.0008)
eager_sum: tensor([5.7129, 6.4285, 7.1436, 7.8572])
compiled_sum: tensor([5.7143, 6.4286, 7.1429, 7.8571])
max_abs_sum tensor(0.0014)

Versions

PyTorch version: 2.10.0+cpu

cc @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10 @aditew01 @chauhang @penguinwu @voznesenskym @EikanWang @Guobing-Chen @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @aakhundov @coconutruben @jataylo

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING