xbtorch.quant.quantizers

Quantization utilities for fixed-point representation in XBTorch using QTorch.

Provides: - Autograd-compatible fixed-point quantization functions - Utility to compute numeric ranges based on fixed-point format

Modules

FixedPointQuantFtorch.autograd.Function

Custom autograd function wrapping QTorch’s fixed-point quantizer.

Functions

fixed_point_quantize_wrapper(input, wl, fl, symmetric=True, rounding=”nearest”)

Convenience wrapper to quantize a tensor with given word length and fractional length.

get_range_fixed_point(num_format)

Computes the representable range for a fixed-point number format.

Functions

fixed_point_quantize_wrapper(input, wl, fl)

Convenience wrapper for FixedPointQuantF.

get_range_fixed_point(num_format)

Compute the numeric range for a given fixed-point format.

Classes

FixedPointQuantF(*args, **kwargs)

Autograd-compatible fixed-point quantization function.

class xbtorch.quant.quantizers.FixedPointQuantF(*args, **kwargs)[source]

Bases: Function

Autograd-compatible fixed-point quantization function.

Uses QTorch’s fixed_point_quantize to quantize the input tensor to a fixed-point representation. The backward pass is a straight-through estimator (STE), returning the gradient unmodified.

static backward(ctx, grad_output)[source]

Straight-through estimator (STE) backward pass.

Returns:

Gradient for input and None for all other arguments.

Return type:

tuple

static forward(ctx, input, wl, fl, symmetric=True, rounding='nearest')[source]

Forward pass: quantizes input to fixed-point.

Parameters:
  • input (torch.Tensor) – Tensor to quantize.

  • wl (int) – Word length (total bits) for fixed-point representation.

  • fl (int) – Fractional length (bits used for fraction).

  • symmetric (bool, default=True) – Whether to use symmetric quantization.

  • rounding (str, default='nearest') – Rounding method (currently only nearest supported).

Returns:

Quantized tensor.

Return type:

torch.Tensor

xbtorch.quant.quantizers.fixed_point_quantize_wrapper(input, wl, fl, symmetric=True, rounding='nearest')[source]

Convenience wrapper for FixedPointQuantF.

Parameters:
  • input (torch.Tensor) – Tensor to quantize.

  • wl (int) – Word length (total bits) for fixed-point representation.

  • fl (int) – Fractional length (bits used for fraction).

  • symmetric (bool, default=True) – Whether to use symmetric quantization.

  • rounding (str, default='nearest') – Rounding method.

Returns:

Quantized tensor.

Return type:

torch.Tensor

xbtorch.quant.quantizers.get_range_fixed_point(num_format)[source]

Compute the numeric range for a given fixed-point format.

Parameters:

num_format (object) – Object with attributes: - wl : word length (bits) - fl : fractional length (bits) - symmetric : bool, symmetric vs asymmetric quantization

Returns:

(min_value, max_value) representable by the fixed-point format.

Return type:

tuple