apytypes_common.h

Typedefs

template<typename ...NB_ARGS>
using ThirdPartyArray = std::variant<nanobind::ndarray<nanobind::numpy, NB_ARGS...>, nanobind::ndarray<nanobind::pytorch, NB_ARGS...>, nanobind::ndarray<nanobind::tensorflow, NB_ARGS...>, nanobind::ndarray<nanobind::jax, NB_ARGS...>, nanobind::ndarray<nanobind::cupy, NB_ARGS...>>

Type of a third-party array library. Returning this to Python will.

Enums

enum class QuantizationMode

Values:

enumerator TRN
enumerator TRN_INF
enumerator TRN_ZERO
enumerator TRN_AWAY
enumerator TRN_MAG
enumerator RND
enumerator RND_ZERO
enumerator RND_INF
enumerator RND_MIN_INF
enumerator RND_CONV
enumerator RND_CONV_ODD
enumerator JAM
enumerator JAM_UNBIASED
enumerator STOCH_WEIGHTED
enumerator STOCH_EQUAL
enum class OverflowMode

Values:

enumerator WRAP
enumerator SAT
enumerator NUMERIC_STD
enum class ThirdPartyArrayTag

Tags for the supported third-party array libraries.

Values:

enumerator NUMPY
enumerator PYTORCH
enumerator TENSORFLOW
enumerator JAX
enumerator CUPY

Functions

std::uint64_t rnd64_fx()

64-bit uniform random number generator for fixed-point stachastic quantization

std::uint64_t rnd64_fp()
void rst_default_rnd64_fx(std::uint64_t seed)

Reset the default stochastic quantization random number generators.

void rst_default_rnd64_fp(std::uint64_t seed)
std::uint64_t get_rnd64_fx_seed()
std::uint64_t get_rnd64_fp_seed()
void set_float_quantization_mode(QuantizationMode mode)

Set the global quantization mode for APyFloat.

QuantizationMode get_float_quantization_mode()

Return the global quantization mode for APyFloat.

APyFixedCastOption get_fixed_cast_mode()

Return the global cast mode for APyFixed.

std::optional<APyFixedAccumulatorOption> get_accumulator_mode_fixed()

Return the global accumulator mode for APyFixed.

std::optional<APyFloatAccumulatorOption> get_accumulator_mode_float()

Return the global accumulator mode for APyFloat.

void set_preferred_array_lib_from_str(const std::string &array_lib)

Python-exported set preferred array library function.

void set_preferred_array_lib(ThirdPartyArrayTag array_lib)

Set the preferred third-party array library.

ThirdPartyArrayTag get_preferred_array_lib()

Retrieve the currently set preferred third-party array library.

std::string get_preferred_array_lib_as_str()

Retrieve the currently set preferred third-party array library as a string.

template<typename ...NB_ARGS>
ThirdPartyArray<NB_ARGS...> make_third_party_ndarray(const nanobind::ndarray<NB_ARGS...> &array_ref, ThirdPartyArrayTag array_lib)

Convert an un-tagged nb::ndarray (Python capsule around void *) to a third-party array-library specific array.

class ContextManager
#include <apytypes_common.h>

Base class defining the interface for context managers.

Subclassed by APyFixedAccumulatorContext, APyFixedCastContext, APyFloatAccumulatorContext, APyFloatQuantizationContext

Public Functions

virtual ~ContextManager() = default
virtual void enter_context() = 0
virtual void exit_context() = 0
class APyFloatQuantizationContext : public ContextManager

Public Functions

APyFloatQuantizationContext(const QuantizationMode &new_mode, std::optional<std::uint64_t> seed = std::nullopt)
virtual void enter_context() override
virtual void exit_context() override

Private Members

QuantizationMode prev_mode
QuantizationMode new_mode
std::uint64_t prev_seed
std::uint64_t new_seed
const std::function<std::uint64_t()> &prev_engine
std::mt19937_64 default_engine
std::function<std::uint64_t()> new_engine
struct APyFixedCastOption

Public Members

QuantizationMode quantization
OverflowMode overflow

Quantization mode to use for cast operations.

class APyFixedCastContext : public ContextManager

Public Functions

APyFixedCastContext(std::optional<QuantizationMode> quantization = std::nullopt, std::optional<OverflowMode> overflow = std::nullopt)
virtual void enter_context() override
virtual void exit_context() override

Private Members

APyFixedCastOption current_mode
APyFixedCastOption previous_mode
struct APyFixedAccumulatorOption

Public Members

int bits
int int_bits

Total number of bits to use for accumulator.

QuantizationMode quantization

Number of integer bits to use for accumulator.

OverflowMode overflow

Quantization mode to use for multiplication result.

class APyFixedAccumulatorContext : public ContextManager

Public Functions

APyFixedAccumulatorContext(std::optional<int> int_bits = std::nullopt, std::optional<int> frac_bits = std::nullopt, std::optional<QuantizationMode> quantization = std::nullopt, std::optional<OverflowMode> overflow = std::nullopt, std::optional<int> bits = std::nullopt)
virtual void enter_context() override
virtual void exit_context() override

Private Members

std::optional<APyFixedAccumulatorOption> current_mode
std::optional<APyFixedAccumulatorOption> previous_mode
struct APyFloatAccumulatorOption

Public Functions

inline APyFloatSpec get_spec(exp_t backup_bias) const noexcept

Public Members

std::uint8_t exp_bits

Number of exponent bits.

std::uint8_t man_bits

Number of mantissa bits.

std::optional<exp_t> bias

Exponent bias.

QuantizationMode quantization

Quantization mode.

class APyFloatAccumulatorContext : public ContextManager

Public Functions

APyFloatAccumulatorContext(std::optional<int> = std::nullopt, std::optional<int> = std::nullopt, std::optional<exp_t> = std::nullopt, std::optional<QuantizationMode> quantization = std::nullopt)
virtual void enter_context() override
virtual void exit_context() override

Private Members

std::optional<APyFloatAccumulatorOption> current_mode
std::optional<APyFloatAccumulatorOption> previous_mode