MPTorch#

MPTorch is a PyTorch extension for simulating custom and mixed-precision arithmetic in deep-learning workloads, in particular for training. Modern accelerators increasingly use small wordlength formats (e.g. 8-bit floating-point, or 4-bit block-based formats) that were not available just a few years ago. MPTorch allows the user to simulate the use of such formats on hardware that doesn’t necessarily support them.

Every elementary operation can be simulated in a custom arithmetic. The operands are stored in a high precision payload (which can be either bfloat16, or the IEEE 754 binary16, binary32, or binary64 formats), and the operation is carried out in a carrier format, either IEEE binary32 or binary64. The result is rounded to the desired target format. In most cases, this simulated behavior is identical with what would be obtained by actually performing the operation in the target arithmetic.

Features#

  • Custom floating-point format types. BinaryK includes the family of binary floating-point formats specified by IEEE P3109, the upcoming standard for machine-learning arithmetic: any K-bit float with P bits of precision, signed or unsigned, with or without infinities. It goes beyond the standard by allowing custom bias terms and different subnormal policies. SuperFP is a second family that trades mantissa precision in certain portions of the representation domain for dynamic range.

  • Fixed-point formats. FixedPoint is any word length with any number of fractional bits, in two’s complement, symmetric or unsigned, with an elementwise quantizer in every rounding mode (see FixedPoint: one step across the range).

  • Block formats. BlockFormat covers block scaling formats such as the OCP Microscaling (MX) formats and NVFP4, with 1D blocks and 2D tiles, BinaryK or SuperFP elements and scales, and a choice of how each block’s scale is rounded (see Block formats).

  • Tensor rounding functions, with seven rounding modes, including stochastic rounding with a configurable number of random bits, and block quantizers that store a tensor packed in a block format’s own bytes (see Quantizers).

  • Custom arithmetic kernels. The arithmetic inside a dot product is rounded to a format you choose and summed in the order you choose: each product, each addition to the running sum, or each fused multiply-add. This runs on CPU, CUDA and Apple GPUs. One matrix-product routine runs every such computation: matrix products with the full torch.matmul operand contract and gradients, convolutions that do not unfold their inputs, and products of block-format tensors that are decoded as they are read. A palette of up to eight formats can also be used, for mixed precision across the dot products of one matrix product (see Quantized numerical kernels).

  • Custom and mixed precision training support. QLinear, QConv1d/2d/3d and QMatmul are drop-in layers. Each of their signals (input, weight, bias and each gradient) and each of their matrix products (forward, input gradient, weight gradient) can be given its own format (see Layers). Quantizer is a straight-through estimator with one format forward and another backward.

  • Configurable formats for simulating custom floating-point arithmetic. An underlying binary64 arithmetic simulates formats of up to 53 bits of precision and ten exponent bits. carrier=torch.float64 gives float32, float16 and bfloat16 models the same arithmetic while their values stay in their own dtype (see Formats and rounding).

  • Reproducibility. torch.manual_seed controls the stochastic rounding streams, and the deterministic modes are bit-identical between CPU and CUDA. On an Apple GPU every result is the CPU’s, stochastic rounding included, except where the GPU flushes a binary32 subnormal (see On an Apple GPU).

About the examples#

Every code block in these pages is a file under docs/snippets/, and the output shown after it is what that file printed when docs/run_snippets.py ran it. To regenerate them on your own machine:

$ python docs/run_snippets.py            # every snippet
$ python docs/run_snippets.py rounding   # those whose name contains "rounding"

Indices#