Context handling

class apytypes.APyFixedCastContext
__init__(self, quantization: QuantizationMode | None = None, overflow: OverflowMode | None = None) None
class apytypes.APyFloatQuantizationContext

Context for changing the quantization mode used for floating-point operations.

If not specified explicitly, floating-point operations will use the quantization mode that is set globally, which is QuantizationMode.TIES_EVEN by default. The mode however can be changed using the static method apytypes.set_float_quantization_mode(), or, preferably, by using a so-called quantization context. With a quantization context one can change the quantization mode used by all operations within a specific section in the code.

Examples

>>> from apytypes import APyFloat, QuantizationMode
>>> from apytypes import APyFloatQuantizationContext

Addition using the default round to nearest, ties even

>>> a = APyFloat.from_float(0.123, exp_bits=5, man_bits=10)
>>> b = APyFloat.from_float(3.21, exp_bits=5, man_bits=10)
>>> a + b
APyFloat(sign=0, exp=16, man=683, exp_bits=5, man_bits=10)

Addition using round towards zero

>>> m = QuantizationMode.TO_ZERO
>>> with APyFloatQuantizationContext(quantization=m):
...     a + b
APyFloat(sign=0, exp=16, man=682, exp_bits=5, man_bits=10)

Stochastic rounding with an optional seed

>>> m = QuantizationMode.STOCH_WEIGHTED
>>> s = 0x1234
>>> with APyFloatQuantizationContext(quantization=m, quantization_seed=s):
...     a + b
APyFloat(sign=0, exp=16, man=683, exp_bits=5, man_bits=10)

The quantization mode is reverted automatically upon exiting the context. Nesting the contexts is also possible.

__init__(self, quantization: QuantizationMode, quantization_seed: int | None = None) None
class apytypes.APyFixedAccumulatorContext
__init__(self, int_bits: int | None = None, frac_bits: int | None = None, quantization: QuantizationMode | None = None, overflow: OverflowMode | None = None, bits: int | None = None) None
class apytypes.APyFloatAccumulatorContext

Context for using custom accumulators when performing inner products and matrix multiplications.

Inner products and matrix multiplications will by default perform the summation in the resulting format of the operands, but with APyFloatAccumulatorContext a custom accumulator can be simulated as seen in the example below.

Examples

>>> import numpy as np
>>> from apytypes import APyFloatArray, QuantizationMode
>>> from apytypes import APyFloatAccumulatorContext
>>> An = np.random.normal(1, 2, size=(100, 100))
>>> A = APyFloatArray.from_float(An, exp_bits=5, man_bits=10)
>>> bn = np.random.uniform(0, 1, size=100),
>>> b = APyFloatArray.from_float(bn, exp_bits=5, man_bits=10)

Normal matrix multiplication

>>> c = A @ b.T

Matrix multiplication using stochastic quantization and a wider accumulator

>>> m = QuantizationMode.STOCH_WEIGHTED
>>> with APyFloatAccumulatorContext(exp_bits=6, man_bits=15, quantization=m):
...     d = A @ b.T

If no quantization mode is specified to the accumulator context it will fallback to the mode set globally, see APyFloatQuantizationContext.

__init__(self, exp_bits: int | None = None, man_bits: int | None = None, bias: int | None = None, quantization: QuantizationMode | None = None) None