Skip to content

Open Compute Project Microscaling (MX) tensors in the LibTorch backend - #37

Open
kbr- wants to merge 3 commits into
lean-dojo:mainfrom
kbr-:mx-formats
Open

kbr- wants to merge 3 commits into
lean-dojo:mainfrom
kbr-:mx-formats

Conversation

@kbr-

@kbr- kbr- commented Nov 4, 2026

Copy link
Copy Markdown

TorchLean's LibTorch backend, which runs on NVIDIA's graphics processors (GPU) through CUDA, had no
way to hold or convert tensors in the block-scaled low-precision formats of the Open Compute Project
(OCP) Microscaling Formats (MX) Specification 1.0. Its six formats are named by their exponent and
mantissa bits, E5M2, E4M3, E3M2, E2M3 and E2M1, beside the integer INT8, and store blocks of 32
elements sharing one 8-bit power-of-two scale (E8M0). FloatLib, already TorchLean's dependency,
defined the six formats with their decoding and their conversion of §6.3 in
FloatLib.Floats.Formats.OCP.MX.Standard, but no TorchLean module used it.

This adds MX tensors to the LibTorch backend, with FloatLib's definitions as their semantics. A
tensor is a row-major matrix blocked by 32 along each row, held on the device as its codes and
scales. It is uploaded and downloaded as bytes, decoded to a binary32 buffer, and converted from one
at the scales §6.3 selects or at scales the caller gives, saturating (SAT) or overflowing (OVF) for
the 8-bit floating formats (FP8). Lean computes each format's values from FloatLib and passes them
to the native side, which implements the remaining rules itself: the scale's value, the scale
selection of §6.3, the block of NaN and the order of ties; the CUDA check ties those to FloatLib. A
proof shows that every finite decoded value within binary32's finite range is a binary32 value, so
decoding's one binary32 multiplication can be exact, and the CUDA check confirms it bit for bit on
every lane that is not NaN.

  • Add OCP MX tensor shapes and the exactness of decoding in binary32 -- the shape, its byte
    lengths, and the exactness theorem.
  • Add CUDA operations on OCP MX tensors -- upload, download, decode and quantize, with their
    placeholders for builds without LibTorch.
  • Check the MX operations against FloatLib in the CUDA suite -- a CUDA check comparing the four
    operations with FloatLib.

At the branch's tip, scripts/lake.sh build, test, lint and build NNCI pass, and so does the
CUDA suite with TORCHLEAN_REQUIRE_CUDA=1 on an RTX 4060 Laptop GPU, with LibTorch 2.15.0 for CUDA
13.2 and the CUDA 13.3 toolkit. CI builds only the CPU configuration, so the CUDA operations are
checked by the CUDA suite alone, and no other GPU, LibTorch version or CUDA toolkit was tried.

kbr- added 3 commits November 4, 2026 14:38
A shape fixes an MX profile, a row-major matrix blocked by 32 along each row, and the byte lengths
of its codes and scales, proved to divide exactly. decodeLane_binary32 shows that a block's finite
lane value within binary32's finite range is a binary32 value, so a decoder multiplying an
element's binary32 value by the block's power of two can return it exactly.
not_isBinary32_half_smallest shows that 2^-150, half binary32's smallest subnormal, fails the bound
the theorem's binary32 values carry.
upload, download, decode to binary32 and quantize from binary32, at the scales OCP MX 1.0 section
6.3 selects or at given ones, with SAT or OVF for FP8. Lean computes each profile's values from
FloatLib (decoded values, rounding candidates, the FP8 overflow bound and words, the largest
power-of-two exponent) and passes them; the native side implements the E8M0 scale's value, the
section 6.3 scale selection, the NaN block and the order of ties itself, which the CUDA check ties
to FloatLib. Builds without LibTorch link placeholders that return an IO error.
Every code of every profile decoded at seven scales; conversion of every rounding boundary, special
and pseudo-random blocks under SAT and OVF, at selected and given scales; each refusal beside a
passing twin, an empty tensor and handle ownership.
@Robertboy18

Copy link
Copy Markdown
Member

Thanks for this. Main is about to move to Lean 4.36 in one large commit (branch torchlean-release), which is why this conflicts now. Could you rebase it onto main once that lands? I'll review it then.

@kbr-

kbr- commented Nov 19, 2026

Copy link
Copy Markdown
Author

Sure.

@Robertboy18

Copy link
Copy Markdown
Member

Hi! The Lean 4.36 update has landed on main, and this PR still has merge conflicts with the reorganized TorchLean modules. Could you rebase onto current main and move the MX code to the new module layout? Once it is updated, I can review the FloatLib semantics and run the CPU/CUDA checks. Thanks again for working on this!

This branch has not been deployed

No deployments
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.

2 participants