Biasadd
=================



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

.. math::

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

对于NCHW格式，计算为 :math:`\text{Output}(n, c, h, w) = \text{Input}(n, c, h, w) + \text{Bias}(c)`。 
对于NHWC格式，计算为 :math:`\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``

.. note::
    - FT78NE 支持int8, int16, int32, fp32, fp64, cplx64, cplx128
    - MT7004 支持fp16, fp32, int16, int32, cplx64

**共享存储版本:**

.. c:function:: 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)
.. c:function:: 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)
.. c:function:: 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)
.. c:function:: 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)
.. c:function:: 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)
.. c:function:: 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)
.. c:function:: 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)
.. c:function:: 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调用示例：**

.. code-block:: c
    :linenos:
    :emphasize-lines: 17

    //FT78NE示例
    #include <stdio.h>
    #include <biasadd.h>
    int main(int argc, char* argv[]) {
        float *input_x = (float *)0xA0000000;    // input_x 在DDR空间
        float *input_bias = (float *)0xB0000000; // input_bias
        float *output = (float *)0xC0000000;     // output

        // NHWC format
        int dims[] = {2, 224, 224, 3}; // N, H, W, C
        int shape_size = 4;
        int length = 2 * 224 * 224 * 3;
        const char* data_format = "NHWC";
        int core_mask = 0xff;
        
        // The length of input_bias should be dims, which is 3.
        fp_biasadd_s(input_x, input_bias, output, dims, shape_size, data_format, length, core_mask);
        return 0;
    }


**私有存储版本:**

.. c:function:: 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)
.. c:function:: 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)
.. c:function:: 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)
.. c:function:: void fp_biasadd_p(float* input_x, float* input_bias, float* output, int* dims, int shape_size, char* data_format, int length)
.. c:function:: void hp_biasadd_p(half* input_x, half* input_bias, half* output, int* dims, int shape_size, char* data_format, int length)
.. c:function:: void dp_biasadd_p(double* input_x, double* input_bias, double* output, int* dims, int shape_size, char* data_format, int length)
.. c:function:: void c64_biasadd_p(float* input_x, float* input_bias, float* output, int* dims, int shape_size, char* data_format, int length)
.. c:function:: void c128_biasadd_p(double* input_x, double* input_bias, double* output, int* dims, int shape_size, char* data_format, int length)


**C调用示例：**

.. code-block:: c
    :linenos:
    :emphasize-lines: 16

    //FT78NE示例
    #include <stdio.h>
    #include <biasadd.h>
    int main(int argc, char* argv[]) {
        float *input_x = (float *)0x10000000;    // input_x 在L2空间
        float *input_bias = (float *)0x11000000; // input_bias
        float *output = (float *)0x12000000;     // output

        // NCHW format
        int dims[] = {2, 3, 224, 224}; // N, C, H, W
        int shape_size = 4;
        int length = 2 * 3 * 224 * 224;
        const char* data_format = "NCHW";
        
        // The length of input_bias should be dims, which is 3.
        fp_biasadd_p(input_x, input_bias, output, dims, shape_size, data_format, length);
        return 0;
    }