Skip to content

compressed_tensors.quantization.quant_args

Classes:

Functions:

ActivationOrdering

Bases: Aliasable, str, Enum

Enum storing strategies for activation ordering during GPTQ calibration

Weight: Changes the way calibration occurs but doesn't change the quantization format compared to no activation ordering (normal latency). Compared to Group, it has lower latency and slightly worse accuracy. Compared to no activation ordering during calibration it has slightly better accuracy.

Static: alias for Weight

DynamicType

Bases: str, Enum

Enum storing potential dynamic types.

  1. If dynamic is True, all quantization parameters are generated on the fly.
  2. If dynamic is False, all quantization parameters generated are static.
  3. If "local" is provided, only local quantization parameters are dynamic.

Note: "local" requires the TENSOR_GROUP strategy, which currently only applies to NVFP4.

FP4_E2M1_DATA

Bases: FloatArgs

Methods:

  • cast_to_fp4 –

    Round float values to the nearest E2M1 representable value.

cast_to_fp4 staticmethod

cast_to_fp4(x: Tensor) -> torch.Tensor

Round float values to the nearest E2M1 representable value.

Uses Triton for CUDA/XPU tensors when available, falls back to torch.compile for CPU tensors.

Parameters:

  • x (Tensor) –

    input tensor to quantize

Returns:

  • Tensor –

    input tensor after rounding to fp4 (maintains same dtype)

Source code in src/compressed_tensors/quantization/quant_args.py
@staticmethod
def cast_to_fp4(x: torch.Tensor) -> torch.Tensor:
    """Round float values to the nearest E2M1 representable value.

    Uses Triton for CUDA/XPU tensors when available, falls back to
    torch.compile for CPU tensors.

    :param x: input tensor to quantize
    :return: input tensor after rounding to fp4 (maintains same dtype)
    """
    from compressed_tensors.quantization.utils.fp4_utils import cast_to_fp4

    return cast_to_fp4(x)

QuantizationArgs

Bases: BaseModel

User facing arguments used to define a quantization config for weights or activations

Parameters:

  • num_bits –

    quantization bit depth

  • type –

    dtype to quantize to, either int or float

  • symmetric –

    whether or not quantization scale is symmetric about zero-point

  • strategy –

    string id determining the scope of scale/zero-point to apply

  • group_size –

    group length to use for the group strategy

  • block_structure –

    2d block structure to use for the block strategy; must be a list of two ints [rows, cols] like [128, 128].

  • dynamic –

    set True to perform dynamic quantization - values will not be calibrated during calibration phase, instead during inference new quantization ranges will be observed with every sample. Defaults to False for static quantization. Note that enabling dynamic quantization will change the default observer to a memoryless one

  • actorder –

    activation ordering strategy for GPTQ calibration. Options are GROUP (reorder by activation with g_idx mapping, higher accuracy but higher latency -- removed 2026/08/27), WEIGHT (reorder columns by activation magnitude during calibration only, normal latency with slight accuracy improvement) or None (no activation ordering).

QuantizationStrategy

Bases: str, Enum

Enum storing quantization strategy options

QuantizationType

Bases: str, Enum

Enum storing quantization type options

round_to_quantized_type_args

round_to_quantized_type_args(tensor: Tensor, args: QuantizationArgs, min: Tensor, max: Tensor, cast_to_original_dtype: bool = True) -> torch.Tensor

Rounds an input tensor to the nearest quantized representation given quantization args. The original dtype is kept post-rounding.

Parameters:

  • tensor (Tensor) –

    tensor to round

  • args (QuantizationArgs) –

    quantization args to use for rounding

  • min (Tensor) –

    min value to use for clamping

  • max (Tensor) –

    max value to use for clamping

  • cast_to_original_dtype (bool, default: True ) –

    whether or not we cast the rounded tensor to the original dtype

Returns:

  • Tensor –

    rounded tensor

Source code in src/compressed_tensors/quantization/quant_args.py
def round_to_quantized_type_args(
    tensor: torch.Tensor,
    args: QuantizationArgs,
    min: torch.Tensor,
    max: torch.Tensor,
    cast_to_original_dtype: bool = True,
) -> torch.Tensor:
    """
    Rounds an input tensor to the nearest quantized representation given
    quantization args. The original dtype is kept post-rounding.

    :param tensor: tensor to round
    :param args: quantization args to use for rounding
    :param min: min value to use for clamping
    :param max: max value to use for clamping
    :param cast_to_original_dtype: whether or not we cast the rounded tensor to
        the original dtype
    :return: rounded tensor
    """

    original_dtype = tensor.dtype
    tensor = torch.clamp(tensor, min, max)
    if args.type == QuantizationType.FLOAT:
        if args.num_bits == 8:
            rounded = tensor.to(FP8_E4M3_DATA.dtype)
        elif args.num_bits == 4:
            rounded = FP4_E2M1_DATA.cast_to_fp4(tensor)
        else:
            raise NotImplementedError("Only num_bits in (4, 8) are supported")
    elif args.type == QuantizationType.INT:
        rounded = torch.round(tensor)
    else:
        raise ValueError(f"Invalid quantization type {args.type}")

    if cast_to_original_dtype:
        return rounded.to(original_dtype)
    return rounded

round_to_quantized_type_dtype

round_to_quantized_type_dtype(tensor: Tensor, dtype: dtype, cast_to_original_dtype: bool = True) -> torch.Tensor

Rounds an input tensor to the nearest quantized representation given a dtype. The original dtype is kept post-rounding.

Parameters:

  • tensor (Tensor) –

    tensor to round

  • dtype (dtype) –

    dtype to use for rounding

  • cast_to_original_dtype (bool, default: True ) –

    whether or not we cast the rounded tensor to the original dtype

Returns:

  • Tensor –

    rounded tensor

Source code in src/compressed_tensors/quantization/quant_args.py
def round_to_quantized_type_dtype(
    tensor: torch.Tensor,
    dtype: torch.dtype,
    cast_to_original_dtype: bool = True,
) -> torch.Tensor:
    """
    Rounds an input tensor to the nearest quantized representation given a dtype.
    The original dtype is kept post-rounding.

    :param tensor: tensor to round
    :param dtype: dtype to use for rounding
    :param cast_to_original_dtype: whether or not we cast the rounded tensor to
        the original dtype
    :return: rounded tensor
    """
    original_dtype = tensor.dtype
    if torch.is_floating_point(torch.tensor([], dtype=dtype)):
        finfo = torch.finfo(dtype)
        rounded = torch.clamp(tensor, finfo.min, finfo.max).to(dtype)
    else:
        iinfo = torch.iinfo(dtype)
        rounded = torch.round(torch.clamp(tensor, iinfo.min, iinfo.max)).to(dtype)

    if cast_to_original_dtype:
        return rounded.to(original_dtype)
    return rounded