Repository navigation
Fix FP8 e4m3fn encoding rounding to match PyTorch CPU - #9
Merged
Merged
Conversation
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
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
marked this pull request as ready for review
February 17, 2026 22:51
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:Applied to both normal and subnormal encoding paths.
Subnormal flush sign preservation:
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.pynow 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 ISSUEWe 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.