112 lines
3.6 KiB
ReStructuredText
112 lines
3.6 KiB
ReStructuredText
Maximumgrad
|
||
=================
|
||
|
||
计算逐元素 Maximum 操作的梯度。该算子是 Maximum 算子的反向传播部分。梯度 dy 将被路由到在前向传播中值较大的那个输入。
|
||
|
||
.. math::
|
||
|
||
\text{dx0}_i = \begin{cases}
|
||
\text{dy}_i, & \text{if } \text{Input0}_i > \text{Input1}_i \\
|
||
0, & \text{otherwise}
|
||
\end{cases}
|
||
|
||
\text{dx1}_i = \begin{cases}
|
||
\text{dy}_i, & \text{if } \text{Input1}_i \ge \text{Input0}_i \\
|
||
0, & \text{otherwise}
|
||
\end{cases}
|
||
|
||
输入:
|
||
- **Input0** - 前向传播时的第一个输入数据地址。
|
||
- **Input1** - 前向传播时的第二个输入数据地址。
|
||
- **dy** - 后续层反向传播回来的梯度数据地址。
|
||
- **params** - 参数数组:
|
||
- **Input0_dims** - Input0 的维度信息数组。
|
||
- **Input1_dims** - Input1 的维度信息数组。
|
||
- **dy_dims** - dy 的维度信息数组。
|
||
- **num_dims** - 输入张量的维度数量。
|
||
- **core_mask** - 核掩码(仅共享存储版本需要)。
|
||
|
||
输出:
|
||
- **dx0** - 计算出的关于 Input0 的梯度地址。
|
||
- **dx1** - 计算出的关于 Input1 的梯度地址。
|
||
|
||
支持平台:
|
||
``FT78NE``
|
||
``MT7004``
|
||
|
||
.. note::
|
||
- FT78NE 支持fp32
|
||
- MT7004 支持fp16, fp32
|
||
|
||
**共享存储版本:**
|
||
|
||
.. c:function:: void hp_maximum_grad_s(half* Input0, half* Input1, half* dy, half* dx0, half* dx1, long long *params, int core_mask)
|
||
.. c:function:: void fp_maximum_grad_s(float* Input0, float* Input1, float* dy, float* dx0, float* dx1, long long *params, int core_mask)
|
||
|
||
**C调用示例:**
|
||
|
||
.. code-block:: c
|
||
:linenos:
|
||
:emphasize-lines: 17
|
||
|
||
//FT78NE示例
|
||
#include <stdio.h>
|
||
#include <maximumgrad.h> // 假设头文件名为 maximumgrad.h
|
||
|
||
int main(int argc, char* argv[]) {
|
||
// 假设在DDR空间,且形状相同
|
||
float *input0 = (float *)0xA0000000;
|
||
float *input1 = (float *)0xA1000000;
|
||
float *dy = (float *)0xA2000000;
|
||
float *dx0 = (float *)0xB0000000;
|
||
float *dx1 = (float *)0xB1000000;
|
||
|
||
int dims[] = {4, 256};
|
||
int num_dims = 2;
|
||
int core_mask = 0xff;
|
||
|
||
long long params[6];
|
||
params[0] = (long long)dims; // Input0_dims
|
||
params[1] = (long long)dims; // Input1_dims
|
||
params[2] = (long long)dims; // dy_dims
|
||
params[3] = (long long)num_dims; // num_dims
|
||
|
||
fp_maximum_grad_s(input0, input1, dy, dx0, dx1, core_mask);
|
||
return 0;
|
||
}
|
||
|
||
**私有存储版本:**
|
||
|
||
.. c:function:: void hp_maximum_grad_p(half* Input0, half* Input1, half* dy, long long *params, half* dx0, half* dx1)
|
||
.. c:function:: void fp_maximum_grad_p(float* Input0, float* Input1, float* dy, long long *params, float* dx0, float* dx1)
|
||
|
||
**C调用示例:**
|
||
|
||
.. code-block:: c
|
||
:linenos:
|
||
:emphasize-lines: 16
|
||
|
||
//FT78NE示例
|
||
#include <stdio.h>
|
||
#include <maximumgrad.h> // 假设头文件名为 maximumgrad.h
|
||
|
||
int main(int argc, char* argv[]) {
|
||
// 假设在L2空间,且形状相同
|
||
float *input0 = (float *)0x10000000;
|
||
float *input1 = (float *)0x11000000;
|
||
float *dy = (float *)0x12000000;
|
||
float *dx0 = (float *)0x13000000;
|
||
float *dx1 = (float *)0x14000000;
|
||
|
||
int dims[] = {4, 256};
|
||
int num_dims = 2;
|
||
|
||
long long params[6];
|
||
params[0] = (long long)dims; // Input0_dims
|
||
params[1] = (long long)dims; // Input1_dims
|
||
params[2] = (long long)dims; // dy_dims
|
||
params[3] = (long long)num_dims; // num_dims
|
||
|
||
fp_maximum_grad_p(input0, input1, dy, params, dx0, dx1);
|
||
return 0;
|
||
} |