Skip to content

utils

Classes

Functions

quantize_multiplier

quantize_multiplier(real_multiplier: float) -> tuple[int, int]

Convert a floating point multiplier into a fixed-point multiplier and shift.

Given a real multiplier, this function computes a pair (quantized_multiplier, shift) such that the fixed-point multiplication approximates the real multiplier:

real_multiplier ~ quantized_multiplier / (2^(31 - shift))

This is typically used in quantized inference to convert floating-point scaling factors to fixed-point representation.

Parameters:

  • real_multiplier

    (float) –

    The floating-point multiplier.

Returns:

  • tuple ( tuple[int, int] ) –

    (quantized_multiplier, shift)

compute_fused_activation_range

compute_fused_activation_range(activation_function: AirActivationType, output_zero_point: int = 0, output_scale: float = 1.0, qmin: int = -128, qmax: int = 127) -> tuple[int, int]

Compute the activation min and max values based on the fused activation function.

Parameters:

  • activation_function

    (ActivationFunctionType) –

    The activation function type.

  • output_zero_point

    (int, default: 0 ) –

    The zero point for the output tensor. Defaults to 0.

  • output_scale

    (float, default: 1.0 ) –

    The scale for the output tensor. Defaults to 1.0.

  • qmin

    (int, default: -128 ) –

    Minimum quantized value. Defaults to -128.

  • qmax

    (int, default: 127 ) –

    Maximum quantized value. Defaults to 127.

Returns:

  • tuple[int, int]

    tuple[int, int]: The activation min and max values.

compute_padding_with_offset

compute_padding_with_offset(in_size: int, filter_size: int, stride: int, dilation_rate: int = 1, padding_type: int = AirPaddingType.SAME) -> tuple[int, int]

Computes a single padding value (top/left) for one dimension with offset.

Parameters:

  • in_size

    (int) –

    Input dimension (height or width).

  • filter_size

    (int) –

    Kernel dimension (height or width).

  • stride

    (int) –

    Stride for that dimension.

  • dilation_rate

    (int, default: 1 ) –

    Dilation rate for that dimension (default is 1).

  • padding_type

    (int, default: SAME ) –

    0 for SAME padding, 1 for VALID padding (default is SAME).

Returns:

  • int ( tuple[int, int] ) –

    The computed top or left padding value.

compute_padding

compute_padding(in_size: int, filter_size: int, stride: int, dilation_rate: int = 1, padding_type: int = AirPaddingType.SAME) -> int

Computes a single padding value (top/left) for one dimension.

Parameters:

  • in_size

    (int) –

    Input dimension (height or width).

  • filter_size

    (int) –

    Kernel dimension (height or width).

  • stride

    (int) –

    Stride for that dimension.

  • dilation_rate

    (int, default: 1 ) –

    Dilation rate for that dimension (default is 1).

  • padding_type

    (int, default: SAME ) –

    0 for SAME padding, 1 for VALID padding (default is SAME).

Returns:

  • int ( int ) –

    The computed top or left padding value.

get_expanded_dims_shape

get_expanded_dims_shape(x: NDArray, num_dims: int = 4, pre: bool = True) -> tuple[int, ...]

Expand the shape of a tensor to a specified number of dimensions.

Parameters:

  • x

    (NDArray) –

    Input tensor whose shape is to be expanded.

  • num_dims

    (int, default: 4 ) –

    Number of dimensions to expand to. Defaults to 4.

Returns:

  • tuple[int, ...]

    tuple[int, ...]: Expanded shape of the tensor.