# Biasadd — MindSpore Signal+ 使用手册 alpha 文档

# Biasadd

将偏置向量 input_bias 加到输入张量 input_x 上。这是一个特殊的广播加法，其中偏置向量会沿着非通道维度进行广播。

\[\text{Output} = \text{Input} + \text{Bias}\]

对于NCHW格式，计算为 \(\text{Output}(n, c, h, w) = \text{Input}(n, c, h, w) + \text{Bias}(c)\)。 对于NHWC格式，计算为 \(\text{Output}(n, h, w, c) = \text{Input}(n, h, w, c) + \text{Bias}(c)\)。

输入：

  - input_x - 输入张量的数据地址。

  - input_bias - 1D偏置张量的数据地址。其长度必须等于输入张量的通道维度。

  - dims - 输入张量的维度信息数组。

  - shape_size - 输入张量的维度数。

  - data_format - 数据布局格式，支持 "NCHW" 和 "NHWC"。

  - length - 输入张量 input_x 的总元素数量。

  - core_mask - 核掩码。

输出：

  - output - 输出张量的数据地址，其维度与`input_x`相同。

支持平台：

  `FT78NE` `MT7004`

备注

- FT78NE 支持int8, int16, int32, fp32, fp64, cplx64, cplx128

- MT7004 支持fp16, fp32, int16, int32, cplx64

共享存储版本:

void i8_biasadd_s(int8_t *input_x, int8_t *input_bias, int8_t *output, int *dims, int shape_size, char *data_format, int length, int core_mask)

void i16_biasadd_s(int16_t *input_x, int16_t *input_bias, int16_t *output, int *dims, int shape_size, char *data_format, int length, int core_mask)

void i32_biasadd_s(int32_t *input_x, int32_t *input_bias, int32_t *output, int *dims, int shape_size, char *data_format, int length, int core_mask)

void fp_biasadd_s(float *input_x, float *input_bias, float *output, int *dims, int shape_size, char *data_format, int length, int core_mask)

void hp_biasadd_s(half *input_x, half *input_bias, half *output, int *dims, int shape_size, char *data_format, int length, int core_mask)

void dp_biasadd_s(double *input_x, double *input_bias, double *output, int *dims, int shape_size, char *data_format, int length, int core_mask)

void c64_biasadd_s(float *input_x, float *input_bias, float *output, int *dims, int shape_size, char *data_format, int length, int core_mask)

void c128_biasadd_s(double *input_x, double *input_bias, double *output, int *dims, int shape_size, char *data_format, int length, int core_mask)

C调用示例：

     1//FT78NE示例
     2#include <stdio.h>
     3#include <biasadd.h>
     4int main(int argc, char* argv[]) {
     5    float *input_x = (float *)0xA0000000;    // input_x 在DDR空间
     6    float *input_bias = (float *)0xB0000000; // input_bias
     7    float *output = (float *)0xC0000000;     // output
     8
     9    // NHWC format
    10    int dims[] = {2, 224, 224, 3}; // N, H, W, C
    11    int shape_size = 4;
    12    int length = 2 * 224 * 224 * 3;
    13    const char* data_format = "NHWC";
    14    int core_mask = 0xff;
    15
    16    // The length of input_bias should be dims, which is 3.
    17    fp_biasadd_s(input_x, input_bias, output, dims, shape_size, data_format, length, core_mask);
    18    return 0;
    19}

私有存储版本:

void i8_biasadd_p(int8_t *input_x, int8_t *input_bias, int8_t *output, int *dims, int shape_size, char *data_format, int length)

void i16_biasadd_p(int16_t *input_x, int16_t *input_bias, int16_t *output, int *dims, int shape_size, char *data_format, int length)

void i32_biasadd_p(int32_t *input_x, int32_t *input_bias, int32_t *output, int *dims, int shape_size, char *data_format, int length)

void fp_biasadd_p(float *input_x, float *input_bias, float *output, int *dims, int shape_size, char *data_format, int length)

void hp_biasadd_p(half *input_x, half *input_bias, half *output, int *dims, int shape_size, char *data_format, int length)

void dp_biasadd_p(double *input_x, double *input_bias, double *output, int *dims, int shape_size, char *data_format, int length)

void c64_biasadd_p(float *input_x, float *input_bias, float *output, int *dims, int shape_size, char *data_format, int length)

void c128_biasadd_p(double *input_x, double *input_bias, double *output, int *dims, int shape_size, char *data_format, int length)

C调用示例：

     1//FT78NE示例
     2#include <stdio.h>
     3#include <biasadd.h>
     4int main(int argc, char* argv[]) {
     5    float *input_x = (float *)0x10000000;    // input_x 在L2空间
     6    float *input_bias = (float *)0x11000000; // input_bias
     7    float *output = (float *)0x12000000;     // output
     8
     9    // NCHW format
    10    int dims[] = {2, 3, 224, 224}; // N, C, H, W
    11    int shape_size = 4;
    12    int length = 2 * 3 * 224 * 224;
    13    const char* data_format = "NCHW";
    14
    15    // The length of input_bias should be dims, which is 3.
    16    fp_biasadd_p(input_x, input_bias, output, dims, shape_size, data_format, length);
    17    return 0;
    18}