torch.nn.functional.quantize_tensor#
- torch.nn.functional.quantize_tensor(input, *, dim, scaling_type, qdata_dtype, scaling_algorithm, swizzle_type, scaling_type_use_square_block_size=False) tuple[Tensor, Tensor][source]#
Subdivide
inputinto 2D regions ofscaling_typealong the axes specified indim, then quantize according toscaling_algorithmandqdata_dtypeand return(qdata, scale)for use withscaled_mm(). Currently supported formats:mxfp8. See the code samples at the bottom of this docblock for how to configure supported formats.A swizzled scale layout is modeled as packed
[1, L]over the requested quantization dims, withLbeing swizzle-specific. For example, for an input of shape(B, M, K)anddim=(1, 2)andswizzle_type=SwizzleType.SWIZZLE_32_4_4, the output scale shape will be(B, 1, ceil_div(M, 128) * ceil_div(K, 128) * 32 * 16).Both outputs preserve the axis positions of the input. For both outputs, the quantization axes from
dimform contiguous slices when viewed indim=(outer, inner)order, with the inner dim varying the fastest and outer next-fastest. For example:mxfp8unswizzled quantization of input shaped(M, K)with strides(K, 1)anddim=(0, 1)will result inqdatawith shape(M, K)and strides(K, 1), andscalewith shape(M, K // 32)and strides(K // 32, 1).mxfp8swizzled quantization of input shaped(M, K)with strides(K, 1)anddim=(1, 0)will result inqdatawith shape(M, K)and strides(1, M), andscalewith shape(L, 1)and strides(1, L), withLbeing swizzle-specific.mxfp8swizzled quantization of input shaped(K, M)with strides(1, K)anddim=(0, 1)will result inqdatawith shape(K, M)and strides(M, 1), andscalewith shape(1, L)and strides(L, 1), withLbeing swizzle-specific.mxfp8swizzled quantization of input shaped(B, K, N)with strides(N * K, 1, K)anddim=(2, 1)will result inqdatawith shape(B, K, N)and strides(N * K, 1, K)andscalewith shape(B, L, 1)and strides(L, 1, L), withLbeing swizzle-specific.
Automatic autograd is not supported for this op, users are encouraged to define their own derivative.
This implementation currently requires an NVIDIA SM100 or newer GPU and the optional
nvidia-cutlass-dslandapache-tvm-ffipackages. The hardware support may expand in the future.- Parameters:
input (Tensor) – Tensor with at least two dimensions and dtype
float16,bfloat16, orfloat32.dim (tuple[int, int]) – Ordered outer and inner quantization axes.
scaling_type (ScalingType) – The size of the local region corresponding to a single inner scale, as well as the layout of the output inner scale. Currently supported values:
ScalingType.BlockWise1x32.qdata_dtype (
torch.dtype) – The dtype of the resulting quantized data. Currently supported values:torch.float8_e4m3fn. Special value handling is format dependent. For example, formxfp8an inputNaNorInfmakes every quantized value sharing its scaleNaN; all-zero blocks quantize to zero.scaling_algorithm (ScalingAlgorithm) – The algorithm to convert from a local region of the
input(with its size specified byscaling_type) to the inner scale and inverse inner scale corresponding to that region. Currently supported values:ScalingAlgorithm.MXFP_E8M0_RU.swizzle_type (SwizzleType) – The swizzle to apply to output inner scale. Currently supported values:
SwizzleType.NO_SWIZZLEandSwizzleType.SWIZZLE_32_4_4.scaling_type_use_square_block_size (bool, optional) – When
True, modify the size of the local region corresponding to a single scale value to be a square (for example, modify1x32to be32x32). Note that the output scale size is still controlled byscaling_typeandswizzle_type. If this flag isTrue, currently onlySwizzleType.SWIZZLE_32_4_4is supported. Default:False.
- Returns:
- Quantized data in the logical input shape and
scales with the dtype and layout honoring the
scaling_algorithm,scaling_type, andswizzle_typesettings. For example, formxfp8on a 2D tensor withdim=(0, 1), unswizzled scales have shape(rows, cols // 32); swizzled scales have shape(1, L), whereL = 512 * ceil(rows / 128) * ceil(cols / 128). Batch axes retain their positions in both results.
- Return type:
Examples:
>>> import torch >>> import torch.nn.functional as F >>> x = torch.randn(128, 128, device="cuda", dtype=torch.bfloat16) >>> mxfp8_kwargs = dict( ... scaling_type=F.ScalingType.BlockWise1x32, ... qdata_dtype=torch.float8_e4m3fn, ... scaling_algorithm=F.ScalingAlgorithm.MXFP_E8M0_RU, ... swizzle_type=F.SwizzleType.SWIZZLE_32_4_4, ... ) >>> # mxfp8 dim-k with swizzled scales >>> qdata_k, scale_k = F.quantize_tensor(x, dim=(-2, -1), **mxfp8_kwargs) >>> # mxfp8 dim-m on the same tensor >>> qdata_m, scale_m = F.quantize_tensor(x, dim=(-1, -2), **mxfp8_kwargs) >>> # mxfp8 dim-k with scales shared over 32x32 blocks >>> qdata_square, scale_square = F.quantize_tensor( ... x, dim=(-2, -1), **mxfp8_kwargs, scaling_type_use_square_block_size=True ... ) >>> # mxfp8 dim-k with unswizzled scales >>> plain_kwargs = {**mxfp8_kwargs, "swizzle_type": F.SwizzleType.NO_SWIZZLE} >>> qdata_plain, scale_plain = F.quantize_tensor(x, dim=(-2, -1), **plain_kwargs)