AIfES 2
2.0.0
|
Definition of the Q31 (aiq31) data-type. More...
Go to the source code of this file.
Data Structures | |
struct | aimath_q31_params |
Parameters used for the quantized Q31 values, used as property of a tensor. More... | |
struct | aiscalar_q31 |
Single quantized Q31 value/scalar. More... | |
Typedefs | |
typedef struct aimath_q31_params | aimath_q31_params_t |
typedef struct aiscalar_q31 | aiscalar_q31_t |
Functions | |
void | aimath_q31_print_aitensor (const aitensor_t *tensor) |
Printing a Q31 tensor to console. More... | |
void | aimath_q31_print_aiscalar (const void *scalar) |
Printing a Q31 scalar to console. More... | |
void | aimath_q31_calc_q_params_from_f32 (float min_value, float max_value, aimath_q31_params_t *q_params) |
Calculates the aimath_q31_params parameters. More... | |
void | aimath_q31_quantize_tensor_from_f32 (const aitensor_t *tensor_f32, aitensor_t *tensor_q31) |
Converts a float f32 tensor into a quantized q31 tensor. More... | |
Variables | |
const aimath_dtype_t * | aiq31 |
The Q31 data-type indicator. More... | |
Definition of the Q31 (aiq31) data-type.
The Q31 (aiq31) data-type stores data as quantized 32 bit integer values. In addition a scaling factor (shift) and a zero point (zero_point) is needed to fully define the value.
The Q31 quantization is a asymmetric 32 bit integer quantization that allows integer-only calculations on real values. A real value \( r \) is represented by an integer value \( q \), the scaling factor / shift \( s \) and the zero point \( z \) according to the following formula:
\[ r = 2^{-s} * (q - z) \]
To get the quantized value \( q \) out of a real value you have to calculate
\[ q = round(\frac{x}{2^{-s}} + z) \]
Example: Create a Q31 tensor
The tensor
\[ \left( \begin{array}{rrr} 0 & 1 & 2 \\ 3 & 4 & 5 \end{array}\right) \]
can be created with In C:
In C, C++ and on Arduino:
Example: Create a Q31 scalar
Either create it with the helper macro
or manual with
Example: Print a Q31 tensor to the console
Example: Print a Q31 scalar to the console
void aimath_q31_calc_q_params_from_f32 | ( | float | min_value, |
float | max_value, | ||
aimath_q31_params_t * | q_params | ||
) |
Calculates the aimath_q31_params parameters.
Given a minimum and a maximum value, this function calculates the scaling shift and zero shift parameters for aimath_q31_params
min_value | Minimum value of a given range |
max_value | Maximum value of a given range |
*q_params | Pointer to write the results aimath_q31_params::zero_point and aimath_q31_params::shift into |
void aimath_q31_print_aiscalar | ( | const void * | scalar | ) |
Printing a Q31 scalar to console.
For users the function
is prefered.
*scalar | The scalar (type: aiscalar_q31_t) to print. |
void aimath_q31_print_aitensor | ( | const aitensor_t * | tensor | ) |
Printing a Q31 tensor to console.
For users the function
is prefered.
*tensor | The tensor to print. |
void aimath_q31_quantize_tensor_from_f32 | ( | const aitensor_t * | tensor_f32, |
aitensor_t * | tensor_q31 | ||
) |
Converts a float f32 tensor into a quantized q31 tensor.
Converts a float f32 tensor into a quantized q31 tensor by applying the aimath_q31_params_t given in the quantized q31 tensor
*tensor_f32 | Float f32 tensor to convert from |
*tensor_q31 | quantized q31 tensor to convert to, included aimath_q31_params must be set beforehand |
|
extern |
The Q31 data-type indicator.
Use this variable to configure some element with the Q31 data-type,