torch.nn.functional.quantize_tensor_dual#
- torch.nn.functional.quantize_tensor_dual(input, *, dim, scaling_type, qdata_dtype, scaling_algorithm, swizzle_type, scaling_type_use_square_block_size=False) tuple[Tensor, Tensor, Tensor, Tensor][source]#
Subdivide
inputinto 2D regions ofscaling_typealong the axes specified indimas well asdim[::-1], then quantize according toscaling_algorithmandqdata_dtypeand return(qdata0, scale0, qdata1, scale1)for use withscaled_mm().See
quantize_tensor()to quantize along only one direction.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.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. Note: currently onlyFalseis supported. Default:False.
- Returns:
(qdata, scale, qdata_reverse, scale_reverse).For example, for input of shape
(M, K)and themxfp8format, both quantized tensors have shape(M, K). Whendim=(0, 1), the dim-K result is contiguous; the dim-M result has strides(1, M). Their E8M0 scales have shapes(1, L)and(L, 1), respectively, whereL = 512 * ceil(M / 128) * ceil(K / 128).
- 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-km with swizzled scales >>> qdata_k, scale_k, qdata_m, scale_m = F.quantize_tensor_dual(x, dim=(-2, -1), **mxfp8_kwargs)