Skip to content

vllm.model_executor.layers.quantization.input_hadamard

Block Hadamard rotation of linear-layer inputs, independent of the weight format.

Rotating each contiguous block of the input dimension by an orthonormal Hadamard matrix H on both operands leaves the product unchanged, (x H)(w H)^T = x w^T, while spreading outliers across each block, which shrinks the error of block-scaled quantization (MXFP4, MXFP8, ...).

Classes:

Functions:

InputHadamardFusable

Mixin for a linear method that can take over the input rotation of InputHadamardLinearMethod, for example inside its activation quant.

Methods:

fuse_input_hadamard(layer, size)

Called after process_weights_after_loading. Return True only if apply will now rotate each contiguous size-wide block of the input by the orthonormal Sylvester Hadamard matrix itself.

InputHadamardLinearMethod

Bases: LinearMethodBase

Wraps a linear method and rotates the layer input by an orthonormal block Hadamard matrix before the wrapped apply, in the manner of CompressedTensorsLinearTransformMethod.

With rotate_weight, the weight ([out, in]) is rotated the same way right before the wrapped method processes it, for example before online quantization. Otherwise the checkpoint stores the weight already rotated, together with an int8 input_rotation tensor holding the unnormalized (+-1) Sylvester matrix, which is checked and then dropped.

The activation rotation runs in PyTorch unless the wrapped method takes it over through InputHadamardFusable.

_load_input_rotation(param, loaded_weight, *args, **kwargs)

Every shard of a fused layer carries the same rotation, and it must be the Sylvester matrix apply rotates by.

block_hadamard_rotate(x, hadamard)

Rotate each contiguous hadamard.shape[0]-wide block of the last dimension of x by hadamard, computed in fp32 and returned in x.dtype.

hadamard_matrix(size, device) cached

Orthonormal Sylvester Hadamard matrix, scipy.linalg.hadamard(size) scaled by 1 / sqrt(size), in fp32. size must be a power of two.