turbo_quant

Applicable Products

  • Ascend 950PR&950DT products: Not supported
  • Atlas A3 products: Supported
  • Atlas A2 products: Supported
  • Atlas 200I/500 A2 inference products: Not supported
  • Atlas inference products: Not supported
  • Atlas training products: Not supported

Function

  • Description: turbo_quant normalizes the KV latent of MLA by token and quantizes it into 4-bit codebook indexes, and returns the L2 norm of each token. The input latent must have completed signed Hadamard rotation and must not be normalized in advance. The underlying implementation is aclnnTurboQuant.

  • Formula:

    1. Compute the L2 norm of each token:
    $$ scale_i = norm_i = \sqrt{\sum_d latent_{i,d}^2 + 10^{-16}} $$
    1. Normalize with norm_i:
    $$ u_{i,d} = latent_{i,d} / scale_i $$
    1. Select the nearest 4-bit index based on the midpoint of adjacent codebook centroids:
    $$ nibble_{i,d} = \sum_{b=0}^{14} [u_{i,d} \ge (centroids_b + centroids_{b+1}) / 2] $$
    1. Pack the indexes of two adjacent dimensions into one byte. The low nibble stores the index of the even dimension, and the high nibble stores the index of the odd dimension:
    $$ y_{i,k} = nibble_{i,2k} \mathbin{|} (nibble_{i,2k+1} \ll 4) $$

Prototype

cann_ops_nn.turbo_quant(
    latent,
    centroids,
) -> (Tensor, Tensor)

You can also directly call the registered PyTorch operator:

torch.ops.cann_ops_nn.turbo_quant(latent, centroids)

Parameters

NameTypeRequired/OptionalDescriptionData TypeDimension (Shape)
latentTensorRequiredInput latent, corresponding to latent in the formula. latent must have completed signed Hadamard rotation and not be normalized.float32[numTokens, 512]
centroidsTensorRequired16 Lloyd-Max codebook centroids in ascending order, corresponding to centroids in the formula.float32[16]

Return Values

OutputTypeRequired/OptionalDescriptionData TypeDimension (Shape)
yTensorRequiredQuantization result after packing two 4-bit indexes, corresponding to y in the formula, with the low nibble first.uint8[numTokens, 256]
scaleTensorRequiredL2 norm of each token, corresponding to scale in the formula.float16[numTokens]

Constraints

  • Inputs and outputs must be on the NPU. Non-contiguous tensors are not supported.
  • This API supports both training and inference use cases.
  • This API is only supported for single-operator mode calls and is not supported for TorchAir graph mode calls.

Deterministic Computation

Defaults to a deterministic implementation.

Example

  • Single-operator mode call (eager)

    import torch
    import torch_npu
    import cann_ops_nn
    
    # latent must have completed signed Hadamard rotation and not be normalized.
    latent = torch.randn((2, 512), dtype=torch.float32).npu()
    # centroids must be 16 center points in ascending order.
    centroids = torch.linspace(-1.0, 1.0, 16, dtype=torch.float32).npu()
    y, scale = cann_ops_nn.turbo_quant(latent, centroids)
    print("y:============", y.shape, y.cpu())
    print("scale:============", scale.shape, scale.cpu())