| AIfES 2 2.0.0

Data Structures | Typedefs | Functions | Variables
aimath_q7.h File Reference

Definition of the Q7 (aiq7) data-type. More...

Go to the source code of this file.

## Data Structures
struct | aimath_q7_params
| Parameters used for the quantized Q7 values, used as property of a tensor. More...
struct | aiscalar_q7
| Single quantized Q7 value/scalar. More...

## Typedefs
typedef struct aimath_q7_params | aimath_q7_params_t
typedef struct aiscalar_q7 | aiscalar_q7_t

## Functions
void | aimath_q7_print_aitensor (const aitensor_t *tensor)
| Printing a Q7 tensor to console. More...
void | aimath_q7_print_aiscalar (const void *scalar)
| Printing a Q7 scalar to console. More...
void | aimath_q7_calc_q_params_from_f32 (float min_value, float max_value, aimath_q7_params_t *q_params)
| Calculates the aimath_q7_params parameters. More...
void | aimath_q7_quantize_tensor_from_f32 (const aitensor_t *tensor_f32, aitensor_t *tensor_q7)
| Converts a float f32 tensor into a quantized q7 tensor. More...

## Variables
const aimath_dtype_t * | aiq7
| The Q7 data-type indicator. More...

## Detailed Description

Definition of the Q7 (aiq7) data-type.

Version
  2.2.0

Copyright
  Copyright (C) 2020-2023 Fraunhofer Institute for Microelectronic Circuits and Systems. All rights reserved.
  AIfES is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
  You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.

The Q7 (aiq7) data-type stores data as quantized 8 bit integer values. In addition a scaling factor (shift) and a zero point (zero_point) is needed to fully define the value.

The Q7 quantization is an asymmetric 8 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 Q7 tensor
The tensor

\[ \left( \begin{array}{rrr} 0 & 1 & 2 \\ 3 & 4 & 5 \end{array}\right) \]

can be created with In C:

int8_t example_data[] = {0, 1, 2
3, 4, 5};
uint16_t example_shape[] = {2, 3};
aimath_q7_params_t example_q_params = { .shift = 0, .zero_point = 0 };
aitensor_t example_tensor = {
.dtype = aiq7,
.dim = 2,
.shape = example_shape,
.tensor_params = example_q_params,
.data = example_data
};
aiq7
const aimath_dtype_t * aiq7
The Q7 data-type indicator.
aimath_q7_params
Parameters used for the quantized Q7 values, used as property of a tensor.
Definition: aimath_q7.h:148
aimath_q7_params::shift
uint16_t shift
The scaling factor of the quantization (The total scale is calculated with )
Definition: aimath_q7.h:149
aitensor
A tensor in AIfES.
Definition: aifes_math.h:89
aitensor::dtype
const aimath_dtype_t * dtype
The datatype of the tensor, e.g.
Definition: aifes_math.h:90

In C, C++ and on Arduino:

int8_t example_data[] = {0, 1, 2
3, 4, 5};
uint16_t example_shape[] = {2, 3};
aimath_q7_params_t example_q_params = { .shift = 0, .zero_point = 0 };
aitensor_t example_tensor = AITENSOR_2D_Q7(example_shape, &example_q_params, example_data);

Example: Create a Q7 scalar
Either create it with the helper macro

aiscalar_q7_t scalar = AISCALAR_Q7(4.2f, 4, 0);
aiscalar_q7
Single quantized Q7 value/scalar.
Definition: aimath_q7.h:155

or manually with

aiscalar_q7_t scalar = { .value = 67, .shift = 4, .zero_point = 0};
aiscalar_q7::value
int8_t value
Quantized value .
Definition: aimath_q7.h:156

Example: Print a Q7 tensor to the console

print_aitensor(&example_tensor);
print_aitensor
void print_aitensor(const aitensor_t *tensor)
Printing a tensor to console.

Example: Print a Q7 scalar to the console

print_aiscalar(&scalar, aiq7);
print_aiscalar
void print_aiscalar(const void *scalar, const aimath_dtype_t *dtype)
Printing a scalar to console.

## Function Documentation

## ◆ aimath_q7_calc_q_params_from_f32()

void aimath_q7_calc_q_params_from_f32 | ( | float | min_value,
|  | float | max_value,
|  | aimath_q7_params_t * | q_params
| ) |  |

Calculates the aimath_q7_params parameters.

Given a minimum and a maximum value, this function calculates the scaling shift and zero shift parameters for aimath_q7_params

Parameters

  min_value | Minimum value of a given range
  max_value | Maximum value of a given range
  *q_params | Pointer to write the results aimath_q7_params::zero_point and aimath_q7_params::shift into

## ◆ aimath_q7_print_aiscalar()

void aimath_q7_print_aiscalar | ( | const void * | scalar | ) |

Printing a Q7 scalar to console.

For users the function

print_aiscalar(&scalar, aiq7);

is prefered.

Parameters

  *scalar | The scalar (type: aiscalar_q7_t) to print.

## ◆ aimath_q7_print_aitensor()

void aimath_q7_print_aitensor | ( | const aitensor_t * | tensor | ) |

Printing a Q7 tensor to console.

For users the function

print_aitensor(&tensor);

is prefered.

Parameters

  *tensor | The tensor to print.

## ◆ aimath_q7_quantize_tensor_from_f32()

void aimath_q7_quantize_tensor_from_f32 | ( | const aitensor_t * | tensor_f32,
|  | aitensor_t * | tensor_q7
| ) |  |

Converts a float f32 tensor into a quantized q7 tensor.

Converts a float f32 tensor into a quantized q7 tensor by applying the aimath_q7_params_t given in the quantized q7 tensor

Parameters

  *tensor_f32 | Float f32 tensor to convert from
  *tensor_q7 | quantized q7 tensor to convert to, included aimath_q7_params must be set beforehand

## Variable Documentation

## ◆ aiq7

const aimath_dtype_t* aiq7
| extern

The Q7 data-type indicator.

Use this variable to configure some element with the Q7 data-type,