---
title: 使用说明
description: "初始化Linear+resext、Linear+resmul、Linear+residential。构造时需要传入linear输入矩阵、linear权重矩阵、linear输出矩阵、res输入矩阵、linearRes输出矩阵的tensor信息，其中偏置矩阵可以不送，会调用第二个构造函数进行初始化。"
url: https://www.hikunpeng.com/document/detail/zh/kunpenghpcs/hpckit/devg/KunpengHPCKit_developer_170.html
sourcePath: /source/zh/kunpenghpcs/hpckit/devg/KunpengHPCKit_developer_170.html
indexId: b68ad3a24bf29e819d1a4136f61326286fe0506e3a59d6ea5f28dfebca99d66c67
---
# 使用说明

#### 接口定义

初始化Linear+resext、Linear+resmul、Linear+residential。构造时需要传入linear输入矩阵、linear权重矩阵、linear输出矩阵、res输入矩阵、linearRes输出矩阵的tensor信息，其中偏置矩阵可以不送，会调用第二个构造函数进行初始化。


- LinearResFWD(const TensorInfo &srcInfo, const TensorInfo &weiInfo, const TensorInfo &dstInfo, const TensorInfo &biasInfo, const TensorInfo &resInfo, const TensorInfo &linearResInfo, float alpha = 1.0f, float beta = 0.0f, float gamma = 1.0f, ResOpsFunction kind = ResOpsFunction::RES_IDENTIAL, int numthreads)->void
- LinearResFWD(const TensorInfo &srcInfo, const TensorInfo &weiInfo, const TensorInfo &dstInfo, const TensorInfo &resInfo, const TensorInfo &linearResInfo, float alpha = 1.0f, float beta = 0.0f, float gamma = 1.0f, ResOpsFunction kind = ResOpsFunction::RES_IDENTIAL, int numthreads)->void


**表1 LinearResFWD函数输入参数**

| 参数名称 | 数据类型 | 描述 | 取值范围 |
| --- | --- | --- | --- |
| srcInfo | KuDNN::TensorInfo | src张量信息。 | {shape，type，layout} |
| weiInfo | KuDNN::TensorInfo | weight张量信息。 | {shape，type，layout} |
| dstInfo | KuDNN::TensorInfo | dst张量信息。 | {shape，type，layout} |
| biasInfo | KuDNN::TensorInfo | bias张量信息。 | {shape，type，layout} |
| resInfo | KuDNN::TensorInfo | res张量信息。 | {shape，type，layout} |
| linearResInfo | KuDNN::TensorInfo | linear+res输出张量信息。 | {shape，type，layout} |
| alpha | float | linear参数。 | 默认1.0 |
| beta | float | linear参数。 | 默认0.0 |
| gamma | float | res操作参数。 | 默认1.0（kind为RES\_EXT时gamma与res矩阵相乘，其他时候不生效） |
| kind | enum class | 激活函数类型。 | 默认KuDNN::ResOpsFunction::RES\_IDENTIAL 可选RES\_EXT/RES\_MU |
| numthreads | int | 线程数。 | 默认0 |


执行算子运算，要传入存放输入的内存地址和输出的内存地址，a、b、c、bias分别是输入矩阵、权重矩阵、输出矩阵及偏置矩阵的内存地址。其中偏置矩阵的地址要根据构造时是否传入bias选择是否传入。

- Run(const void *a, const void *b, void *c, const void *bias, const void *res, void *linearRes, float gamma = 1.0f, int numThreads = 0)->void
- Run(const void *a, const void *b, void *c, const void *res, void *linearRes, float gamma = 1.0f, int numThreads = 0)->void


**表2 Run函数输入参数**

| 参数名称 | 数据类型 | 描述 | 取值范围 |
| --- | --- | --- | --- |
| a | void \* | 输入指针。 | 大小为MxKxtype.GetSize()的指针 |
| b | void \* | 权重指针。 | 大小为KxNxtype.GetSize()的指针 |
| c | void \* | 输出指针。 | 大小为MxNxtype.GetSize()的指针 |
| bias | void \* | 偏置指针。 | 大小为MxNxtype.GetSize()的指针 |
| res | void \* | ops操作的输入指针。 | 如果ops操作是相乘，res为NxOxtype.GetSize()的指针 如果ops操作是相加，res为MxNxtype.GetSize()的指针 |
| linearRes | void \* | linear+res操作结果指针。 | 如果ops操作是相乘，res为MxOxtype.GetSize()的指针 如果ops操作是相加，res为MxNxtype.GetSize()的指针 |
| numThreads（可选） | int | 线程数。 | 默认送0时会使用GetMaxNumThreads返回的最大线程数 |


ValidateInput负责验证Linear+resext、Linear+resmul、Linear+residential输入参数，也分为有bias版本和无bias版本。在算子构造过程中自动触发执行。

- ValidateInput(const TensorInfo &srcInfo, const TensorInfo &weiInfo, const TensorInfo &dstInfo, const TensorInfo &biasInfo, const TensorInfo &resInfo, const TensorInfo &linearResInfo, float alpha = 1.0f, float beta = 0.0f, float gamma = 1.0f, ResOpsFunction kind = ResOpsFunction::RES_IDENTIAL, int numthreads = 0)->KuDNN::Status
- ValidateInput(const TensorInfo &srcInfo, const TensorInfo &weiInfo, const TensorInfo &dstInfo, const TensorInfo &resInfo, const TensorInfo &linearResInfo, float alpha = 1.0f, float beta = 0.0f, float gamma = 1.0f, ResOpsFunction kind = ResOpsFunction::RES_IDENTIAL, int numthreads = 0)->KuDNN::Status


**表3 ValidateInput函数输入参数**

| 参数名称 | 数据类型 | 描述 | 取值范围 |
| --- | --- | --- | --- |
| srcInfo | KuDNN::TensorInfo | src张量信息。 | {shape，type，layout} |
| weiInfo | KuDNN::TensorInfo | weight张量信息。 | {shape，type，layout} |
| dstInfo | KuDNN::TensorInfo | dst张量信息。 | {shape，type，layout} |
| biasInfo | KuDNN::TensorInfo | bias张量信息。 | {shape，type，layout} |
| resInfo | KuDNN::TensorInfo | res张量信息。 | {shape，type，layout} |
| linearResInfo | KuDNN::TensorInfo | linear+res输出张量信息。 | {shape，type，layout} |
| alpha | float | linear参数。 | 默认1.0 |
| beta | float | linear参数。 | 默认0.0 |
| gamma | float | res操作参数。 | 默认1.0（kind为RES\_EXT时gamma与res矩阵相乘，其他时候不生效） |
| kind | enum class | 激活函数类型。 | 默认KuDNN::ResOpsFunction::RES\_IDENTIAL 可选RES\_EXT/RES\_MU |
| numthreads | int | 线程数。 | 默认0 |


#### 支持数据类型

- postops支持fp16/fp32/bf16数据类型（TensorInfo对象初始化时需传入Shape、Type、Layout参数，此处列出为Type支持数据类型。）
**表4 TensorInfo对象初始化时支持的Type类型**

| srcInfo | weiInfo | dstInfo | biasInfo | resInfo | linearResInfo |
| --- | --- | --- | --- | --- | --- |
| KuDNN::Element::TypeT::F16(fp16) | KuDNN::Element::TypeT::F16(fp16) | KuDNN::Element::TypeT::F16(fp16) | KuDNN::Element::TypeT::F16(fp16) | KuDNN::Element::TypeT::F16(fp16) | KuDNN::Element::TypeT::F16(fp16) |
| KuDNN::Element::TypeT::F32(fp32) | KuDNN::Element::TypeT::F32(fp32) | KuDNN::Element::TypeT::F32(fp32) | KuDNN::Element::TypeT::F32(fp32) | KuDNN::Element::TypeT::F32(fp32) | KuDNN::Element::TypeT::F32(fp32) |
| KuDNN::Element::TypeT::BF16(bf16) | KuDNN::Element::TypeT::BF16(bf16) | KuDNN::Element::TypeT::BF16(bf16) | KuDNN::Element::TypeT::BF16(bf16) | KuDNN::Element::TypeT::BF16(bf16) | KuDNN::Element::TypeT::BF16(bf16) |


- 最高支持5Dtensor，支持顺序数据排布：a、ab、abc、abcd、abcde。
  对应KuDNN::Layout::A、KuDNN::Layout::AB、KuDNN::Layout::ABC、KuDNN::Layout::ABCD、KuDNN::Layout::ABCDE。

**表5 TensorInfo对象初始化时支持的Layout类型**

| dimension | srcInfo | weiInfo | dstInfo | biasInfo |
| --- | --- | --- | --- | --- |
| 2D | ab/ba | ab/ba | ab/ba | ab/ba |
| 3D | abc/acb | abc/acb | abc/acb | abc/acb |
| 4D | abcd/abdc | abcd/abdc | abcd/abdc | abcd/abdc |
| 5D | abcde/abced | abcde/abced | abcde/abced | abcde/abced |


#### 使用示例

二维数据类型为FP32 ops为RES_IDENTIAL的linear+ops操作。


```
// 示例
// using SizeType = KuDNN::SizeType;
using Shape = KuDNN::Shape;
using Type KuDNN::Element::TypeT
// Tensor初始化
const KuDNN::TensorInfo srcTensor= {{3, 2}, KuDNN::Element::TypeT::F32, KuDNN::Layout::AB};
const KuDNN::TensorInfo weiTensor= {{2, 2}, KuDNN::Element::TypeT::F32, KuDNN::Layout::AB};
const KuDNN::TensorInfo dstTensor= {{3, 2}, KuDNN::Element::TypeT::F32, KuDNN::Layout::AB};
const KuDNN::TensorInfo biasTensor = {{3, 2}, KuDNN::Element::TypeT::F32, KuDNN::Layout::AB};
const KuDNN::TensorInfo resTensor = {{3, 2}, KuDNN::Element::TypeT::F32, KuDNN::Layout::AB};
const KuDNN::TensorInfo linearResTensor = {{3, 2}, KuDNN::Element::TypeT::F32, KuDNN::Layout::AB};
float alpha = 1.0f;
float beta = 0.0f;
float gamma = 1.0f;KuDNN::ResOpsFunction algKind = KuDNN::ResOpsFunction::RES_IDENTIAL;
int numThreads = 0;
SizeType srcSize = srcATensor.GetTotalTensorSize();
SizeType dstSize = weiBTensor.GetTotalTensorSize();
SizeType weiSize = dstCTensor.GetTotalTensorSize();
SizeType biasSize = biasTensor.GetTotalTensorSize();
SizeType resSize = resTensor.GetTotalTensorSize();
SizeType linearResSize = linearResTensor.GetTotalTensorSize();
// 申请存放入参和结果的内存空间
float * src = malloc(srcSize* sizeof(float));
float * wei = malloc(weiSize* sizeof(float));
float * dst = malloc(dstSize* sizeof(float));
float * bias = malloc(biasSize* sizeof(float));
float * res = malloc(resSize* sizeof(float));
float * linearRes = malloc(linearResSize* sizeof(float));
// 构造算子
KuDNN::LinearResFWD
linearResFwd(srcTensor, weiTensor, dstTensor,
biasTensor, resTensor, linearResTensor, alpha, beta, gamma, algKind, numThreads);
// 算子执行
linearResFwd.Run(src, wei, dst, bias, res, linearRes, gamma, numThreads);
```
