Float32 Semantics #
This tutorial runs the same compact MLP under two executable binary32 semantics:
Float32: Lean's native binary32 type and runtime implementation;FloatLib.Floats.ExecFloat.Binary 8 23: FloatLib's configured executable binary32 model. We run a single forward pass and a single reverse-mode VJP (seeded with1.0) and then reportmax_abs_diffbetween native Float32 and configured binary32 after converting both results to hostFloatfor display. PyTorch comparison:
import torch
from torch import nn
model = nn.Sequential(nn.Linear(2, 3), nn.ReLU(), nn.Linear(3, 1)).to(torch.float32)
# PyTorch runs the model through its selected float32 kernels.
The TorchLean example adds an independent second execution. Lean's native Float32 supplies the
ordinary runtime path, while ExecFloat.Binary 8 23 computes from explicit binary32 fields and
rounding rules.
The model and autograd program stay unchanged because their element type is generic.
Run:
scripts/lake.sh exe torchlean float32_semantics
For editor inspection, put the cursor on the #float32_* commands below. Those widgets are for
visualization only; the actual tutorial code uses ordinary def and IO definitions.
Float32 widget probes #
0.1 is the canonical "not exactly representable" decimal. The widget shows the binary32 value
that ExecFloat.Binary 8 23 receives after rounding from the host literal.
PyTorch analogue:
torch.tensor(0.1, dtype=torch.float32)
Instances For
The network run with native Float32 and configured binary32 (Binary 8 23).
Instances For
Parameter and buffer layout generated by the two affine layers in model.
Instances For
The model builder produces exactly layout.
Named outputs from one forward and reverse-mode execution.
- output : TorchLean.Tensor α [1]
- outputWeightGrad : TorchLean.Tensor α [1, 3]
- outputBiasGrad : TorchLean.Tensor α [1]
- inputGrad : TorchLean.Tensor α [2]
Instances For
Convert every tensor in a run result while preserving its statically known shape.
Instances For
Run one forward and backward pass over the fixed parameters at scalar type α.
The scalar parameter α lets native Float32 and configured binary32 (Binary 8 23) use the same
code, source literals, and graph. Both runs use binary32 precision; the comparison exercises their
scalar conversions and arithmetic implementations.
Instances For
Command-line help for the Float32 semantics tutorial.