TanH Prototype and Function List¶
Description¶
This kernel performs hyperbolic tangent activation function on input tensor elementwise and stores the result to the output tensor.
Where:
\(x_{i}\) - \(i_{\text{th}}\) value in input tensor
\(y_{i}\) - \(i_{\text{th}}\) value in output tensor
This kernel uses a look-up table (LUTs) to perform data transformation. See Look-Up Tables (LUT) Manipulation Prototypes and Function List section and the pseudo-code sample for more details on LUT structure preparation. Use the following functions for the purpose:
mli_krn_tanh_get_lut_size
mli_krn_tanh_create_lut
Functions¶
Kernels which implement TanH functions have the following prototype:
mli_status mli_krn_tanh_<data_format>(
const mli_tensor *in,
const mli_lut *lut,
mli_tensor *out);
where data_format
is one of the data formats listed in Table MLI Data Formats and the function
parameters are shown in the following table:
Parameter |
Type |
Description |
---|---|---|
|
|
[IN] Pointer to constant input tensor. |
|
|
[IN] Pointer to a valid LUT table structure prepared for TanH activation. |
|
|
[IN | OUT] Pointer to output tensor. Result is stored here. |
Function Name |
Details |
---|---|
|
All tensors data format: sa8 |
|
All tensors data format: fx16 |
Conditions¶
Ensure that you satisfy the following general conditions before calling the function:
in
andout
tensors must be valid (see mli_tensor Structure Field Descriptions) and satisfy data requirements of the selected version of the kernel.
in
andout
tensors must be of the same shape
lut
structure must be valid and prepared for the TanH activation function (see Look-Up Tables (LUT) Manipulation Prototypes and Function List).
mem_stride
of the innermost dimension must be equal to 1 for all the tensors.
For sa8 versions of kernel, in addition to general conditions, ensure that you satisfy the following quantization conditions before calling the function:
in
tensor must be quantized on the tensor level. This implies that the tensor contains a single scale factor and a single zero offset.Zero offset of
in
tensor must be within [-128, 127] range.
Ensure that you satisfy the platform-specific conditions in addition to those listed above (see the Platform Specific Details chapter).
Result¶
These functions modify:
Memory pointed by
out.data.mem
field.
el_params
field ofout
tensor.
It is assumed that all the other fields and structures are properly populated to be used in calculations and are not modified by the kernel.
The range of this function is (-1, 1). Depending on the data type, quantization parameters of the output tensor are configured in the following way:
fx16
out.el_params.fx.frac_bits
is set to 15. Hence, the maximum representable value of TanH is equivalent to 0.999969482421875 (not 1.0).sa8
out.el_params.sa.zero_point.mem.i16
is set to 0
out.el_params.sa.scale.mem.i16
is set to 1
out.el_params.sa.scale_frac_bits.mem.i8
is set to 8
The kernel supports in-place computation. It means that out
and in
tensor structures
can point to the same memory with the same memory strides but without shift.
It can affect performance for some platforms.
Warning
Only an exact overlap of starting address and memory stride of the in
and out
tensors is acceptable. Partial overlaps result in undefined behavior.
Depending on the debug level (see section Error Codes) this function performs a parameter
check and returns the result as an mli_status
code as described in section Kernel Specific Configuration Structures.