Repository navigation
Declare sub-byte microfloats with their real width on Julia 1.14 - #37
Open
AntonOresten wants to merge 1 commit into
Open
AntonOresten wants to merge 1 commit into
AntonOresten wants to merge 1 commit into
Conversation
Julia 1.14 lets a primitive type declare fewer bits than it stores (JuliaLang/julia#61359). There, `@microfloat` now declares a type narrower than a byte with its sign + exponent + significand bits, so `Core.bitsizeof(Float4_E2M1FN) == 4`; before 1.14 it keeps 8. Values then cannot carry bits above the width, and `===` agrees with every other operation. `raw_bits` and `from_raw_bits` widen and truncate such types. Base's `reinterpret` refuses them, so on 1.14 Microfloats defines `reinterpret` between a microfloat and `UInt8`, which keeps single-value conversions working (dropping bits above the width), and BitPacking 0.2.5's packing with them. Arrays of these types can no longer be reinterpreted on 1.14. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #37 +/- ##
==========================================
+ Coverage 87.04% 87.24% +0.19%
==========================================
Files 14 14
Lines 664 674 +10
==========================================
+ Hits 578 588 +10
Misses 86 86 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This branch has not been deployed
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.
On Julia 1.14 (JuliaLang/julia#61359), a primitive type can declare any number of bits. Building on #36, this declares each microfloat with its real width there:
Float4_E2M1FNis a 4-bit primitive type,Float6_E2M3FNis a 6-bit one, and so on. Each still occupies one byte. On earlier versions nothing changes, since the declared width falls back to 8.What changes on 1.14
Core.bitsizeof(T) == bitwidth(T).===,hashandobjectidignore the unused high bits. Loads and stores mask them, so junk bits can no longer be stored.raw_bits/from_raw_bitsusezext_int/trunc_intfor sub-byte types, and keepbitcast(plus the Ignore bits above a microfloat's width #36 mask) for 8-bit types and older Julia.reinterpret(UInt8, x)andreinterpret(T, ::UInt8)are defined for all microfloats. Base'sbitcastwould otherwise reject a 4-bit ↔ 8-bit reinterpret, so existing scalar code (e.g.reinterpret(UInt8, Float4_E2M1FN(x))in PTX tests) keeps working.reinterpret(T, 0xf3)drops the high bits.@microfloatdeclaresprimitive type T <: Microfloat N endviadeclared_bits(N), and its docstring and theMicrofloatdocstring describe both versions.Breaking on 1.14
Array reinterpret of a sub-byte microfloat, e.g.
reinterpret(UInt8, [Float4_E2M1FN(1)]), now throwsArgumentError, because Base refuses to reinterpret arrays whose elements have unused bits (Base.has_bit_padding). The fix israw_bits.(xs)/from_raw_bits.(T, bytes), or packing through BitPacking. Since this breaks code that works today, this probably wants 0.3.0.BitPacking 0.2.5 works as is, because it only scalar-reinterprets. MurrellGroup/BitPacking.jl#17 adds the native-width paths there too.
Tests
stored(U, x)reads the bytes in memory through a pointer, sincereinterpretcan no longer do that for sub-byte types. The vectorization and "results fit the width" tests use it.sizeof == 1,Core.bitsizeof(T) == bitwidth(T)on 1.14, scalar reinterpret round trips including high-bit truncation, and@test_throws ArgumentErrorfor the array case.Codegen
The generated machine code is identical to the 8-bit build except in the scalar narrowing loop, which gains one
and 15per element (152 → 160 instructions) from the store mask.🤖 Generated with Claude Code