b_asic.quantization¶
B-ASIC quantization module.
- class b_asic.quantization.OverflowMode(value)¶
Overflow types.
- SATURATION = 2¶
Two’s complement saturation.
Overflow return the most positive/negative number.
- WRAPPING = 1¶
Two’s complement overflow, i.e., remove the more significant bits.
- to_apytypes() OverflowMode¶
Convert to APyTypes OverflowMode.
- Returns:
- ApyOverflowMode
The corresponding APyTypes overflow mode.
- class b_asic.quantization.QuantizationMode(value)¶
Quantization types.
- JAMMING = 4¶
Jamming/von Neumann rounding, i.e., set the LSB to one.
- MAGNITUDE_TRUNCATION = 3¶
Magnitude truncation, i.e., round towards zero.
- ROUNDING = 1¶
Standard two’s complement rounding, i.e, tie rounds towards infinity.
- TRUNCATION = 2¶
Two’s complement truncation, i.e., round towards negative infinity.
- UNBIASED_JAMMING = 6¶
Unbiased jamming/von Neumann rounding.
- UNBIASED_ROUNDING = 5¶
Unbiased rounding, i.e., tie rounds towards even.
- to_apytypes() QuantizationMode¶
Convert to APyTypes QuantizationMode.
- Returns:
- ApyQuantizationMode
The corresponding APyTypes quantization mode.
- Raises:
- ValueError
If the mode has no APyTypes equivalent.
- b_asic.quantization.quantize(value: int | float | complex | type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None | APyFixed | APyCFixed | APyFloat | APyCFloat, fractional_bits: int, integer_bits: int = 1, quantization_mode: QuantizationMode = QuantizationMode.TRUNCATION, overflow_mode: OverflowMode = OverflowMode.WRAPPING) int | float | complex | type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None | APyFixed | APyCFixed | APyFloat | APyCFloat¶
Quantize value assuming two’s complement representation.
Quantization happens before overflow, so, e.g., rounding may lead to an overflow.
The total number of bits is fractional_bits + integer_bits. However, there is no check that this will be a positive number. Note that the sign bit is included in these bits. If integer_bits is not given, then use 1, i.e., the result is between
\[-1 \leq \text{value} \leq 1-2^{-\text{fractional_bits}}\]If value is a complex number, the real and imaginary parts are quantized separately.
- Parameters:
- valueint, float, complex
The value to be quantized.
- fractional_bitsint
Number of fractional bits, can be negative.
- integer_bitsint, default: 1
Number of integer bits, can be negative.
- quantization_mode
QuantizationMode, default:QuantizationMode.TRUNCATION Type of quantization to use.
- overflow_mode
OverflowMode, default:OverflowMode.WRAPPING Type of overflow to use.
- Returns:
- int, float, complex
The quantized value.
Examples
>>> from b_asic.quantization import quantize, QuantizationMode, OverflowMode ... ... quantize(0.3, 4) # Truncate 0.3 using four fractional bits and one integer bit 0.25 >>> quantize(0.3, 4, quantization_mode=QuantizationMode.ROUNDING) # As above, but round 0.3125 >>> quantize(1.3, 4) # Will overflow -0.75 >>> quantize(1.3, 4, 2) # Use two integer bits 1.25 >>> quantize(1.3, 4, overflow_mode=OverflowMode.SATURATION) # use saturation 0.9375 >>> quantize(0.3, 4, -1) # Three bits in total, will overflow -0.25