compressed_tensors.quantization.utils.helpers
Functions:
-
calculate_block_padding–Calculate the padding needed to make tensor dimensions divisible by block size.
-
calculate_qparams–:param min_vals: tensor of min value(s) to calculate scale(s) and zero point(s)
-
calculate_range–Calculated the effective quantization range for the given Quantization Args
-
can_quantize–Checks if value can be quantized by quant_args.
-
compute_dynamic_scales_and_zp–Returns the computed scales and zero points for dynamic activation
-
generate_gparam–Generate a global scale for an entire tensor (input_tensor).
-
get_torch_bit_depth–Determine the number of bits used to represent the dtype of a tensor
-
is_model_quantized–Check if any modules in a model are quantized, based on the existence of a non-empty
-
is_module_quantized–Check if a module is quantized, based on the existence of a non-empty quantization
-
maybe_pad_tensor_for_block_quant–Pad a tensor so its dimensions are divisible by the block size.
-
module_type–Gets a string representation of a module type
calculate_block_padding
calculate_block_padding(shape: tuple[int, ...], block_structure: tuple[int, int]) -> tuple[int, int]
Calculate the padding needed to make tensor dimensions divisible by block size.
For block quantization, dimensions must be divisible by the block size for proper scale alignment when layers are merged in inference frameworks like vLLM.
Parameters:
-
shape(tuple[int, ...]) –shape of the tensor (at least 2D)
-
block_structure(tuple[int, int]) –[block_height, block_width] for block quantization
Returns:
-
tuple[int, int]–tuple of (pad_rows, pad_cols) needed to make dimensions divisible
Source code in src/compressed_tensors/quantization/utils/helpers.py
calculate_qparams
calculate_qparams(min_vals: Tensor, max_vals: Tensor, quantization_args: QuantizationArgs, global_scale: Tensor | None = None) -> tuple[FloatTensor, IntTensor]
Parameters:
-
min_vals(Tensor) –tensor of min value(s) to calculate scale(s) and zero point(s) from
-
max_vals(Tensor) –tensor of max value(s) to calculate scale(s) and zero point(s) from
-
quantization_args(QuantizationArgs) –settings to quantization
-
global_scale(Tensor | None, default:None) –additional global scale to scale the locally generated scale currently only applied/supported for Fp4
Returns:
-
tuple[FloatTensor, IntTensor]–tuple of the calculated scale(s) and zero point(s). For FP4, the calculated scale is of dtype FP8
Source code in src/compressed_tensors/quantization/utils/helpers.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
calculate_range
calculate_range(quantization_args: QuantizationArgs, device: str) -> tuple[torch.Tensor, torch.Tensor]
Calculated the effective quantization range for the given Quantization Args
Results are memoized on (type, num_bits, device), so the scalar tensors are constructed once per unique key instead of on every call. The returned tensors are shared across calls and must not be modified in place.
Parameters:
-
quantization_args(QuantizationArgs) –quantization args to get range of
-
device(str) –device to store the range to
Returns:
-
tuple[Tensor, Tensor]–tuple endpoints for the given quantization range
Source code in src/compressed_tensors/quantization/utils/helpers.py
can_quantize
Checks if value can be quantized by quant_args.
Parameters:
-
value(Tensor) –tensor to check for quantization
-
quant_args(QuantizationArgs) –QuantizationArgs to use for quantization
Returns:
-
bool–False if value is already quantized to quant_args or value is incompatible with quant_args, True if value can be quantized with quant_args
Source code in src/compressed_tensors/quantization/utils/helpers.py
compute_dynamic_scales_and_zp
compute_dynamic_scales_and_zp(value: Tensor, args: QuantizationArgs, module: Module, global_scale: Tensor | None = None)
Returns the computed scales and zero points for dynamic activation quantization.
Parameters:
-
value(Tensor) –tensor to calculate quantization parameters for
-
args(QuantizationArgs) –quantization args
-
reduce_dims–optional tuple of dimensions to reduce along, returned scale and zero point will be shaped (1,) along the reduced dimensions
Returns:
-
–
tuple of scale and zero point derived from the observed tensor
Source code in src/compressed_tensors/quantization/utils/helpers.py
generate_gparam
generate_gparam(updated_min_val: Tensor, updated_max_val: Tensor, scale_data: FloatArgs | None = FP8_E4M3_DATA, quant_data: FloatArgs | None = FP4_E2M1_DATA, dtype: dtype | None = torch.float32)
Generate a global scale for an entire tensor (input_tensor). Goal of the scale is to ensure that the quantization (local) scale falls into the approproiate dtype range.
E.g. for NVFP4, group (local) scales are in dtype FP8. The global_scale attempts to use the entire FP8 dtype range while mapping a per-group max to the FP4 max.
Source code in src/compressed_tensors/quantization/utils/helpers.py
get_torch_bit_depth
Determine the number of bits used to represent the dtype of a tensor
Parameters:
-
value(Tensor) –tensor to check bit depth of
Returns:
-
int–bit depth of each element in the value tensor
Source code in src/compressed_tensors/quantization/utils/helpers.py
is_model_quantized
Check if any modules in a model are quantized, based on the existence of a non-empty quantization scheme in at least one module
Parameters:
-
model(Module) –pytorch model
Returns:
-
bool–True if model is quantized, False otherwise
Source code in src/compressed_tensors/quantization/utils/helpers.py
is_module_quantized
Check if a module is quantized, based on the existence of a non-empty quantization scheme
Parameters:
-
module(Module) –pytorch module to check
Returns:
-
bool–True if module is quantized, False otherwise
Source code in src/compressed_tensors/quantization/utils/helpers.py
maybe_pad_tensor_for_block_quant
Pad a tensor so its dimensions are divisible by the block size.
This is essential for FP8 block quantization when dimensions are not divisible by block size. The padding ensures that when weights are merged in inference frameworks (like vLLM's gate_up_proj), the scale tensor blocks align correctly.
Parameters:
-
tensor(Tensor) –tensor to pad (at least 2D)
-
block_structure(tuple[int, int]) –[block_height, block_width] for block quantization
Returns:
-
Tensor–padded tensor
Source code in src/compressed_tensors/quantization/utils/helpers.py
module_type
Gets a string representation of a module type
:module: pytorch module to get type of
Returns:
-
str–module type as a string