|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
Affine (scale + zero-point) tensor quantization for the NPU runtime. More...
Go to the source code of this file.
Enumerations | |
| enum | ra8_npu_quant_range_t : int32_t { k_ra8_npu_quant_i8_min = -128 , k_ra8_npu_quant_i8_max = 127 , k_ra8_npu_quant_u8_min = 0 , k_ra8_npu_quant_u8_max = 255 } |
| Saturation bounds of the supported quantized element types. More... | |
Functions | |
| ra8_err_t | ra8_npu_quantize_i8 (const float *in, int8_t *out, size_t count, float scale, int32_t zero_point) |
| Quantize a float arena into an INT8 tensor (affine, saturating). | |
| ra8_err_t | ra8_npu_dequantize_i8 (const int8_t *in, float *out, size_t count, float scale, int32_t zero_point) |
| Dequantize an INT8 tensor into a float arena (affine). | |
| ra8_err_t | ra8_npu_quantize_u8 (const float *in, uint8_t *out, size_t count, float scale, int32_t zero_point) |
| Quantize a float arena into a UINT8 tensor (affine, saturating). | |
| ra8_err_t | ra8_npu_dequantize_u8 (const uint8_t *in, float *out, size_t count, float scale, int32_t zero_point) |
| Dequantize a UINT8 tensor into a float arena (affine). | |
Affine (scale + zero-point) tensor quantization for the NPU runtime.
Input/output quantization helpers for the Ethos-U55 inference runtime. An Ethos-U55 command stream operates on INT8 / UINT8 quantized tensors; the application holds its data as real-valued float. These four functions bridge the two with the standard TFLite affine mapping
real = scale * (quantized - zero_point) quantized = clamp(round(real / scale) + zero_point, qmin, qmax)
so a runtime can ra8_npu_quantize_*() a float input arena into the INT8 / UINT8 tensor the NPU reads, then ra8_npu_dequantize_*() the NPU output back to float for the application.
Rounding is done with float arithmetic and an explicit cast rather than lroundf(), so this translation unit pulls in NO libm dependency. That matters because it is compiled into every app (it lives in the always-built ra8_hal source set); linking libm into apps that never quantize would be an unwanted cost. Unreferenced, --gc-sections drops it entirely.
Unlike ra8_npu.h this header is NOT behind RA8_HAS_NPU: the mapping is plain arithmetic with no MMIO, useful (and host-testable) on any device, and the NPU-only driver stays the single owner of the RA8_HAS_NPU gate.
Re-entrant and stateless: every function reads only its arguments and writes only the caller's output buffer. Safe to call from any context.
Definition in file ra8_npu_quant.h.
| enum ra8_npu_quant_range_t : int32_t |
Saturation bounds of the supported quantized element types.
The quantize helpers clamp the rounded result to the closed range of the destination element type so an out-of-range real value saturates instead of wrapping. INT8 spans [-128, 127]; UINT8 spans [0, 255].
Definition at line 71 of file ra8_npu_quant.h.
|
nodiscard |
Dequantize an INT8 tensor into a float arena (affine).
Maps each in[i] to scale * (in[i] - zero_point).
| [in] | in | Source INT8 arena of count elements (non-NULL). |
| [out] | out | Destination float arena of count elements (non-NULL). |
| [in] | count | Number of elements to convert (0 is a valid no-op). |
| [in] | scale | Quantization scale (the same value used to quantize). |
| [in] | zero_point | Quantization zero-point (subtracted before scaling). |
| k_ra8_ok | All count elements dequantized into out. |
| k_ra8_err_null_ptr | in or out was nullptr. |
count elements. in. out is left unmodified.Definition at line 128 of file ra8_npu_quant.c.
References k_ra8_ok, RA8_CHECK_NULL_PTR, and s_tag.
|
nodiscard |
Dequantize a UINT8 tensor into a float arena (affine).
Maps each in[i] to scale * (in[i] - zero_point).
| [in] | in | Source UINT8 arena of count elements (non-NULL). |
| [out] | out | Destination float arena of count elements (non-NULL). |
| [in] | count | Number of elements to convert (0 is a valid no-op). |
| [in] | scale | Quantization scale (the same value used to quantize). |
| [in] | zero_point | Quantization zero-point (subtracted before scaling). |
| k_ra8_ok | All count elements dequantized into out. |
| k_ra8_err_null_ptr | in or out was nullptr. |
count elements. in. out is left unmodified.Definition at line 158 of file ra8_npu_quant.c.
References k_ra8_ok, RA8_CHECK_NULL_PTR, and s_tag.
Referenced by internal_npu_infer_execute().
|
nodiscard |
Quantize a float arena into an INT8 tensor (affine, saturating).
Maps each in[i] to clamp(round(in[i] / scale) + zero_point, -128, 127). Rounding is half-away-from-zero. The intermediate is bounded before the cast so an extreme real value saturates rather than triggering undefined float-to-int conversion.
| [in] | in | Source float arena of count elements (non-NULL). |
| [out] | out | Destination INT8 arena of count elements (non-NULL). |
| [in] | count | Number of elements to convert (0 is a valid no-op). |
| [in] | scale | Quantization scale, strictly positive (> 0). |
| [in] | zero_point | Quantization zero-point (added after rounding). |
| k_ra8_ok | All count elements quantized into out. |
| k_ra8_err_null_ptr | in or out was nullptr. |
| k_ra8_err_invalid_arg | scale was not strictly positive. |
count elements. out is left unmodified.Definition at line 109 of file ra8_npu_quant.c.
References internal_quant_round_clamp(), k_ra8_err_invalid_arg, k_ra8_npu_quant_i8_max, k_ra8_npu_quant_i8_min, k_ra8_ok, RA8_CHECK_NULL_PTR, ra8_log_error, and s_tag.
|
nodiscard |
Quantize a float arena into a UINT8 tensor (affine, saturating).
Maps each in[i] to clamp(round(in[i] / scale) + zero_point, 0, 255). Rounding is half-away-from-zero. The intermediate is bounded before the cast so an extreme real value saturates rather than triggering undefined float-to-int conversion.
| [in] | in | Source float arena of count elements (non-NULL). |
| [out] | out | Destination UINT8 arena of count elements (non-NULL). |
| [in] | count | Number of elements to convert (0 is a valid no-op). |
| [in] | scale | Quantization scale, strictly positive (> 0). |
| [in] | zero_point | Quantization zero-point (added after rounding). |
| k_ra8_ok | All count elements quantized into out. |
| k_ra8_err_null_ptr | in or out was nullptr. |
| k_ra8_err_invalid_arg | scale was not strictly positive. |
count elements. out is left unmodified.Definition at line 139 of file ra8_npu_quant.c.
References internal_quant_round_clamp(), k_ra8_err_invalid_arg, k_ra8_npu_quant_u8_max, k_ra8_npu_quant_u8_min, k_ra8_ok, RA8_CHECK_NULL_PTR, ra8_log_error, and s_tag.
Referenced by internal_npu_infer_prepare_input().