基於 NXP 的 eIQ Auto API 的使用方法简介

一、eIQ Auto API

本文主要针对 针对 eIQ_Auto_UserGuide.pdf 中所列的 API 进行测试,说明 API 的使用方法。

1、Matmul

Matmul 是对输入 tensor 矩阵相乘的节点操作,在 cpu 中执行。

在 airunner_node_config.hpp 中 airunner :: MatmulConfig结构中的参数选项如下:

struct MatMulConfig
{
bool mTransposeA; /*!< if true, a is transposed before multiplication */
bool mTransposeB; /*!< if true, b is transposed before multiplication */
bool mAdjointA; /*!< if true, a is conjugated and transposed before multiplication */
bool mAdjointB; /*!< if true, b is conjugated and transposed before multiplication */
bool mAisSparse; /*!< if true, a is treated as a sparse matrix */
bool mBisSparse; /*!< if true, b is treated as a sparse matrix */
ActivationConfig mActivation; /*!< Activation function */
};
template<> std::unique_ptr NodeFactory::Create(const MatMulConfig &aConfig) noexcept;

 

调用代码如下:

lGraph->AddNode(
NodeFactory::Create(
MatMulConfig {false, false, false, false, false, false,std::move(aActivation)}),
{lNetInputTensor.get(),lNetInputTensor.get()},{outputTensor});



2、Transpose

Transpose 是对输入 tensor transpose函数作用是对矩阵进行转换操作.

在 airunner_node_config.hpp 中 airunner :: TransposeConfig结构中的参数选项如下:

struct TransposeConfig
{
std::array<int32_t, 4U> mPerm; /*!< the permutation of the output tensors from innermost to outermost*/
};
template<> std::unique_ptr NodeFactory::Create(const TransposeConfig &aConfig) noexcept;

调用代码如下:

lGraph->AddNode(
NodeFactory::Create(
TransposeConfig{{akH, akW}, {aStrideH, aStrideW}, {1, 1}, std::move(aPadding), std::move(aActivation)}),
{lNetInputTensor.get(), lWeightTensor.get(), lBiasTensor.get()}, {outputTensor});

测试代码如下:

lRetVal += Transpose_net_construction(10, 3, 3, 28, 28, 1, 1, 30, {PaddingScheme_t::SAME, 0, 0, 0, 0}, {ActivationFunction_t::NONE, 0.0, 0.0}, checkRef);



3、Squeeze

Squeeze 是对输入 tensor 大小调整的节点操作,它也可以用作具有单个输入的激活层。从张量形状中移除大小为1的维度.给定一个张量 input,该操作返回一个与已经移除的所有大小为1的维度具有相同类型的张量.如果您不想删除所有大小为1的维度,则可以通过指定 axis 来删除特定的大小为1的维度.

在 airunner_node_config.hpp 中 airunner :: SqueezeConfig结构中的参数选项如下:

struct SqueezeConfig
{
std::array<bool, TENSOR_DIMS_MAX> mAxes; /*!< Specific axes to squeeze, 0 = Channels, 1 = Width, 2 = Height, 3 = Batch*/
};
template<> std::unique_ptr NodeFactory::Create(const SqueezeConfig &aConfig) noexcept;

调用代码如下:

lGraph->AddNode(
NodeFactory::Create(
SqueezeConfig{ 0}),
{lNetInputTensor.get(), lWeightTensor.get(), lBiasTensor.get()}, {outputTensor});

测试代码如下:

lRetVal += Squeeze_net_construction(10, 3, 3, 28, 28, 1, 1, 30, {PaddingScheme_t::SAME, 0, 0, 0, 0}, {ActivationFunction_t::NONE, 0.0, 0.0}, checkRef);



4、Sqrt

Softmax 是对输入 tensor 要进行平方运算 。

在 airunner_node_config.hpp 中 airunner :: SqrtConfig结构中的参数选项如下:

struct SqrtConfig
{
ActivationConfig mActivation; /*!< Activation function */
};
template<> std::unique_ptr NodeFactory::Create(const SqrtConfig &aConfig) noexcept;

调用代码如下:

lGraph->AddNode(
NodeFactory::Create(
SqrtConfig{std::move(aActivation)}),
{lNetInputTensor.get(), lWeightTensor.get(), lBiasTensor.get()}, {outputTensor});

测试代码如下:

lRetVal += Sqrt_net_construction(10, 3, 3, 28, 28, 1, 1, 30, {PaddingScheme_t::SAME, 0, 0, 0, 0}, {ActivationFunction_t::NONE, 0.0, 0.0}, checkRef);

 

5、Padding

Padings 也是一个张量,代表每一维填充多少行/列。用来对输入的tensor 进行填充。

在 airunner_node_config.hpp 中 airunner :: PaddingConfig结构中的参数选项如下:

enum class PaddingScheme_t : int8_t
{
VALID = 0 /*!< VALID padding (no padding is applied) */,
SAME /*!< SAME padding */,
CAFFESAME /*!< CAFFE SAME padding */,
EXPLICIT /*!< Specified padding */,
};

调用代码如下:
lRetVal += Softmax_net_construction(10, 3, 3, 28, 28, 1, 1, 30, {PaddingScheme_t::SAME, 0, 0, 0, 0}, {ActivationFunction_t::NONE, 0.0, 0.0}, checkRef);

PaddingScheme_t::SAME 是调用的方法

 

6、Activation

ActivationFunction是每个神经元都必须有激活函数。它们为神经元提供了模拟复杂非线性数据集所必需的非线性特性。该函数取所有输入的加权和,进而生成一个输出信号。你可以把它看作输入和输出之间的转换。使用适当的激活函数,可以将输出值限定在一个定义的范围内。

在 airunner_node_config.hpp 中 ActivationFunction 结构中的参数选项如下:

 

enum class ActivationFunction_t : int16_t
{
NONE = 0 /*!< No activation, y = x */, /* deviation@line@misra_cpp_2008@2-10-5@intentional@"Scoped enum, no risk of confusion" */
RELU /*!< Relu activation: y = max(0, x) */,
BRELU /*!< Bounded Relu: y = min(A, max(0, x)) */,
LEAKYRELU /*!< Leaky Relu: y = I(x < 0)Ax + I(x >= 0)x */,
SIGMOID /*!< Sigmoid: y = 1 / (1 + e^(-x)) */, /*deviation@line@misra_cpp_2008@2-10-1@intentional@"Scoped enum, no risk of confusion" */
TANH /*!< Tanh : y = tanh(x) */, /*deviation@line@misra_cpp_2008@2-10-1@intentional@"Scoped enum, no risk of confusion" */
};

调用代码如下:

lRetVal += Softmax_net_construction(10, 3, 3, 28, 28, 1, 1, 30, {PaddingScheme_t::SAME, 0, 0, 0, 0}, {ActivationFunction_t::NONE, 0.0, 0.0}, checkRef);

ActivationFunction_t::NONE 为调用的代码



二、参考资料

【1】NXP 官网资料

https://www.nxp.com/design/software/development-software/eiq-auto-dl-toolkit:EIQ-AUTO

【2】《eIQ_Auto_DemosGuide.pdf》

【3】《eIQ_Auto_UserGuide.pdf 》

三、总结归纳

       至此,我们的 eIQ  Auto 的内容已经完成,本系列主要是介绍NXP 的 S32V 中 eIQ 的推理框架的kernel算子,在深度学习中所支持的推理 op 。能够为读者提供参考,对 op实现的具体方法有一个初步了解。如果想继续深入了解,还需要多关注后续的 S32V 系列的产品,感谢! 

★博文内容均由个人提供,与平台无关,如有违法或侵权,请与网站管理员联系。

★文明上网,请理性发言。内容一周内被举报5次,发文人进小黑屋喔~

评论