Skip to content

Fix FP8 e4m3fn encoding rounding to match PyTorch CPU - #9

Merged
lmangani merged 6 commits into
mainfrom
copilot/fix-wrong-float8-e4m3fn-conversion-again
Feb 17, 2026
Merged

lmangani merged 6 commits into
mainfrom
copilot/fix-wrong-float8-e4m3fn-conversion-again

Conversation

Copilot AI commented Feb 17, 2026 •

Copy link
Copy Markdown

MPS Metal shader produced different FP8 byte encodings than CPU (e.g., 100.0 → 0x6D vs 0x6C), causing silent data corruption. Root cause: Metal used round-away-from-zero while PyTorch CPU uses IEEE 754 round-half-to-even.

Changes

Mantissa rounding in fp8_matmul.metal:

// Before: round-away-from-zero
uint mant = uint((mantissa - 1.0f) * 8.0f + 0.5f);

// After: round-half-to-even (IEEE 754)
uint mant = uint(rint((mantissa - 1.0f) * 8.0f));

Applied to both normal and subnormal encoding paths.

Subnormal flush sign preservation:

// Before: always flush to +0
if (val < (1.0f / 512.0f)) return 0;

// After: preserve sign bit
if (val < (1.0f / 512.0f)) return sign << 7;

For value 100.0, mantissa calculation yields 4.5. Round-half-to-even rounds 4.5 → 4 (nearest even), producing byte 0x6C matching CPU. Round-away-from-zero would give 4.5 → 5, producing incorrect byte 0x6D.

Validation

Verified 29 test cases including edge cases. No test updates required - existing test_mps_vs_cpu.py now passes.

Original prompt

This section details on the original issue you should resolve

<issue_title>Wrong Float8_e4m3fn conversion</issue_title>
<issue_description>The code is running fine, but the conversions are causing corruptions in video generations and essentially, destroying results without causing apparent errors - our tests must also be be wrong and not correctly validating the math of the conversion. We need to correct the math and force using the native C++ application as reference to check if that's able to produce better results.</issue_description>

<agent_instructions>The previous PR did not address the issue but the new tests confirmed its presence. let analyze the situation and find a proper fix.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@lmangani REOPENING ISSUE

We ran the test an found the following issue with dtype conversion:

======================================================================
MPS vs CPU VALIDATION TEST SUITE

This test validates that the MPS Metal GPU implementation
produces identical results to the CPU fallback implementation.

This serves as the authoritative validation for correctness.

======================================================================
SYSTEM REQUIREMENTS CHECK

PyTorch version: 2.8.0
Python version: 3.9.6

✓ MPS is available
MPS built: True

✓ torch.float8_e4m3fn is available

======================================================================
TEST 1: FP8 Encoding - MPS Metal vs CPU Fallback

Encoding on CPU (reference)...
Encoding on MPS (Metal shader)...

✓ MPS and CPU encoding produce IDENTICAL results
Tested 21 values - all match byte-for-byte

======================================================================
TEST 2: FP8 Decoding - MPS Metal vs CPU Fallback

Decoding all 256 FP8 values on CPU...
Decoding all 256 FP8 values on MPS...

✓ MPS and CPU decoding produce IDENTICAL results
Maximum difference: 0.00e+00 (below tolerance: 1e-06)
All 256 FP8 values match

======================================================================
TEST 3: Encode-Decode Roundtrip - MPS vs CPU

CPU roundtrip: encode → decode...
MPS roundtrip: encode → decode...

Roundtrip errors:
CPU max relative error: 2.34%
MPS max relative error: 2.34%
CPU vs MPS max diff: 0.00e+00

✓ CPU and MPS roundtrip results are IDENTICAL
(within tolerance: 1e-06)

======================================================================
TEST 4: dtype Conversion - MPS .to() vs CPU .to()

Converting on CPU: float32 → float8_e4m3fn...
Converting on MPS: float32 → float8_e4m3fn...

⚠ Found 1 byte differences:
Index Value CPU MPS

4 100.0000 0x6C 0x6D

======================================================================
TEST 5: FP8 Matrix Multiplication - MPS vs CPU

Matrix dimensions: A(16×32) @ B(24×32)ᵀ = C(16×24)

CPU: Quantizing and computing matmul...
MPS: Quantizing and computing matmul...

Results:
CPU vs MPS max diff: 0.00e+00
CPU vs MPS mean diff: 0.00e+00
CPU vs FP32 mean diff: 1.63e-01
MPS vs FP32 mean diff: 1.63e-01

✓ CPU and MPS matmul results are consistent
(within tolerance: 0.0001)

======================================================================
TEST 6: Performance Comparison - MPS vs CPU

Matrix dimensions: 64×256 @ 128×256

Benchmarking CPU...
Benchmarking MPS...

Performance results:
CPU time: 1.36 ms
MPS time: 0.93 ms
Speedup: 1.45x

✓ MPS is 1.5x faster than CPU

======================================================================
TEST SUMMARY

✓ FP8 Encoding PASS
✓ FP8 Decoding PASS
✓ Encode-Decode Roundtrip PASS
✗ dtype Conversion FAIL
✓ Matrix Multiplication PASS
✓ Performance PASS

======================================================================
⚠ SOME TESTS FAILED

There are differences between MPS and CPU implementations.
This indicates a device-specific bug that needs investigation.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 4 commits February 17, 2026 22:34
Co-authored-by: lmangani <1423657+lmangani@users.noreply.github.com>
Co-authored-by: lmangani <1423657+lmangani@users.noreply.github.com>
Co-authored-by: lmangani <1423657+lmangani@users.noreply.github.com>
Co-authored-by: lmangani <1423657+lmangani@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix wrong Float8_e4m3fn conversion issue Fix FP8 e4m3fn encoding to match PyTorch CPU rounding behavior Feb 17, 2026
Copilot AI requested a review from lmangani February 17, 2026 22:39
Co-authored-by: lmangani <1423657+lmangani@users.noreply.github.com>
Copilot AI changed the title Fix FP8 e4m3fn encoding to match PyTorch CPU rounding behavior Fix FP8 e4m3fn encoding rounding to match PyTorch CPU Feb 17, 2026
@lmangani
lmangani marked this pull request as ready for review February 17, 2026 22:51
@lmangani
lmangani merged commit dfd8a68 into main Feb 17, 2026
2 checks passed
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.

Wrong Float8_e4m3fn conversion

2 participants