昇腾社区首页
中文
注册

SoftMax

功能说明

对输入tensor做softmax计算。为方便理解,通过python脚本实现的方式,表达其计算公式(以输入为ND格式为例)如下,其中src是源操作数(输入),dst、sum、max为目的操作数(输出)。

def softmax(src):
    #基于last轴进行rowmax(按行取最大值)处理
    max = np.max(src, axis=-1, keepdims=True)
    sub = src - max
    exp = np.exp(sub)
    #基于last轴进行rowsum(按行求和)处理
    sum = np.sum(exp, axis=-1, keepdims=True)
    dst = exp / sum
    return dst, max, sum

当输入的数据排布格式不同时,内部的reduce过程会有所不同:当输入为ND格式时,内部的reduce过程按last轴进行;当输入为NZ格式时,内部的reduce过程按照last轴和first轴进行,reduce过程如下图所示:

图1 ND格式的reduce过程
图2 NZ格式的reduce过程

函数原型

  • 接口框架申请临时空间

    template <typename T, bool isReuseSource = false, bool isBasicBlock = false, bool isDataFormatNZ = false>

    __aicore__ inline void SoftMax(const LocalTensor<T>& dstTensor, const LocalTensor<T>& sumTensor,

    const LocalTensor<T>& maxTensor, const LocalTensor<T>& srcTensor, const SoftMaxTiling& tiling,

    const SoftMaxShapeInfo& softmaxShapeInfo = {})

    template <typename T, bool isReuseSource = false, bool isBasicBlock = false, bool isDataFormatNZ = false>

    __aicore__ inline void SoftMax(const LocalTensor<half>& dst, const LocalTensor<float>& sumTensor,

    const LocalTensor<float>& maxTensor, const LocalTensor<half>& src, const SoftMaxTiling& tiling,

    const SoftMaxShapeInfo& softmaxShapeInfo = {})

    template <typename T, bool isReuseSource = false, bool isBasicBlock = false>

    __aicore__ inline void SoftMax(const LocalTensor<T>& dstTensor, const LocalTensor<T>& srcTensor, const SoftMaxTiling& tiling,

    const SoftMaxShapeInfo& softmaxShapeInfo = {})

  • 通过sharedTmpBuffer入参传入临时空间

    template <typename T, bool isReuseSource = false, bool isBasicBlock = false, bool isDataFormatNZ = false>

    __aicore__ inline void SoftMax(const LocalTensor<T>& dstTensor, const LocalTensor<T>& sumTensor,

    const LocalTensor<T>& maxTensor, const LocalTensor<T>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer,

    const SoftMaxTiling& tiling, const SoftMaxShapeInfo& softmaxShapeInfo = {})

    template <typename T, bool isReuseSource = false, bool isBasicBlock = false, bool isDataFormatNZ = false>

    __aicore__ inline void SoftMax(const LocalTensor<half>& dst, const LocalTensor<float>& sumTensor,

    const LocalTensor<float>& maxTensor, const LocalTensor<half>& src, const LocalTensor<uint8_t>& sharedTmpBuffer,

    const SoftMaxTiling& tiling, const SoftMaxShapeInfo& softmaxShapeInfo = {})

    template <typename T, bool isReuseSource = false, bool isBasicBlock = false>

    __aicore__ inline void SoftMax(const LocalTensor<T>& dstTensor, const LocalTensor<T>& srcTensor,

    const LocalTensor<uint8_t>& sharedTmpBuffer, const SoftMaxTiling& tiling, const SoftMaxShapeInfo& softmaxShapeInfo = {})

由于该接口的内部实现中涉及复杂的计算,需要额外的临时空间来存储计算过程中的中间变量。临时空间支持接口框架申请和开发者通过sharedTmpBuffer入参传入两种方式。

  • 接口框架申请临时空间,开发者无需申请,但是需要预留临时空间的大小。
  • 通过sharedTmpBuffer入参传入,使用该tensor作为临时空间进行处理,接口框架不再申请。该方式开发者可以自行管理sharedTmpBuffer内存空间,并在接口调用完成后,复用该部分内存,内存不会反复申请释放,灵活性较高,内存利用率也较高。

接口框架申请的方式,开发者需要预留临时空间;通过sharedTmpBuffer传入的情况,开发者需要为tensor申请空间。临时空间大小BufferSize的获取方式如下:通过SoftMax/SimpleSoftMax Tiling中提供的GetSoftMaxMaxTmpSize/GetSoftMaxMinTmpSize接口获取所需最大和最小临时空间大小,最小空间可以保证功能正确,最大空间用于提升性能。

参数说明

表1 接口参数说明

参数名

输入/输出

描述

dstTensor

输出

目的操作数。

类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。

Atlas A2训练系列产品/Atlas 800I A2推理产品,支持的数据类型为:half/float

Atlas推理系列产品AI Core,支持的数据类型为:half/float

Atlas 200I/500 A2推理产品,支持的数据类型为:half/float

dstTensor的shape和源操作数srcTensor一致。

sumTensor

输出

目的操作数。

类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。

Atlas A2训练系列产品/Atlas 800I A2推理产品,支持的数据类型为:half/float

Atlas推理系列产品AI Core,支持的数据类型为:half/float

Atlas 200I/500 A2推理产品,支持的数据类型为:half/float

用于保存softmax计算过程中reducesum的结果。

  • sumTensor的last轴长度固定为32Byte,即一个block长度。该block中的所有数据为同一个值,比如half数据类型下,该block中的16个数均为相同的reducesum的值。
  • 非last轴的长度与dstTensor保持一致。

maxTensor

输出

目的操作数。

类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。

Atlas A2训练系列产品/Atlas 800I A2推理产品,支持的数据类型为:half/float

Atlas推理系列产品AI Core,支持的数据类型为:half/float

Atlas 200I/500 A2推理产品,支持的数据类型为:half/float

用于保存softmax计算过程中reducemax的结果。

  • maxTensor的last轴长度固定为32Byte,即一个block长度。该block中的所有数据为同一个值。比如half数据类型下,该block中的16个数均为相同的reducemax的值。
  • 非last轴的长度与dstTensor保持一致。

srcTensor

输入

源操作数。

类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。

Atlas A2训练系列产品/Atlas 800I A2推理产品,支持的数据类型为:half/float

Atlas推理系列产品AI Core,支持的数据类型为:half/float

Atlas 200I/500 A2推理产品,支持的数据类型为:half/float

last轴长度需要32Byte对齐。

sharedTmpBuffer

输入

临时空间。

类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。

该操作数的数据类型固定uint8_t。

接口内部复杂计算时用于存储中间变量,由开发者提供。

临时空间大小BufferSize的获取方式请参考SoftMax/SimpleSoftMax Tiling

tiling

输入

softmax计算所需Tiling信息,Tiling信息的获取请参考SoftMax/SimpleSoftMax Tiling

softmaxShapeInfo

输入

srcTensor的shape信息。SoftMaxShapeInfo类型,具体定义如下:

struct SoftMaxShapeInfo {
uint32_t srcM; // 非尾轴长度的乘积
uint32_t srcK; // 尾轴长度,必须32Byte对齐
uint32_t oriSrcM; // 原始非尾轴长度的乘积
uint32_t oriSrcK;  // 原始尾轴长度
};

需要注意,当输入输出的数据格式为NZ格式时,尾轴长度为reduce轴长度即图2中的W0*W1,非尾轴为H0*H1。

isReuseSource

输入

预留参数,暂未启用,为后续的功能扩展做保留,保持默认值即可。

isBasicBlock

输入

srcTensor和dstTensor的shape信息和Tiling切分策略满足基本块要求的情况下,可以使能该参数用于提升性能,默认不使能。基本块要求如下:

  • srcTensor和dstTensor的shape信息需要满足如下条件:last轴长度小于2048并且是64的倍数,非last轴长度的乘积为8的倍数。
  • 通过调用IsBasicBlockInSoftMax判断Tiling切分策略是否满足基本块的切分要求。

针对Atlas 200I/500 A2推理产品,该参数为预留参数,暂未启用,为后续的功能扩展做保留,保持默认值即可。

isDataFormatNZ

输入

当前输入输出的数据格式是否为NZ格式,默认数据格式为ND,即默认取值为false。

针对Atlas 200I/500 A2推理产品,不支持配置为NZ格式。

返回值

支持的型号

Atlas A2训练系列产品/Atlas 800I A2推理产品

Atlas推理系列产品AI Core

Atlas 200I/500 A2推理产品

注意事项

  • srcTensor和dstTensor的Tensor空间可以复用。
  • sumTensor和maxTensor为输出,并且last轴长度必须固定32Byte,非last轴大小需要和srcTensor以及dstTensor保持一致。
  • sumTensor和maxTensor的数据类型需要保持一致。

调用示例

本样例中输入srcTensor和输出dstTensor的shape大小为[320,64],中间计算结果sumTensor和maxTensor的shape大小为[320,16],数据类型均为half,输入输出的数据排布格式为ND,srcTensor和dstTensor空间不复用,不使能基本块。

#include "kernel_operator.h"

namespace AscendC {
template <typename T> class KernelSoftmax {
public:
    __aicore__ inline KernelSoftmax() {}
    __aicore__ inline void Init(__gm__ uint8_t* srcGm, __gm__ uint8_t* dstGm, const SoftMaxTiling& tilingData)
    {
        elementNumPerBlk = 32 / sizeof(T);
        src1Global.SetGlobalBuffer((__gm__ T*)srcGm);
        dstGlobal.SetGlobalBuffer((__gm__ T*)dstGm);
        pipe.InitBuffer(inQueueSrc, 1, height * width * sizeof(T));
        pipe.InitBuffer(maxQueue, 1, height * elementNumPerBlk * sizeof(T));
        pipe.InitBuffer(sumQueue, 1, height * elementNumPerBlk * sizeof(T));
        pipe.InitBuffer(outQueueDst, 1, height * width * sizeof(T));
        tiling = tilingData;
    }
    __aicore__ inline void Process()
    {
        CopyIn();
        Compute();
        CopyOut();
    }

private:
    __aicore__ inline void CopyIn()
    {
        LocalTensor<T> srcLocal = inQueueSrc.AllocTensor<T>();
        DataCopy(srcLocal, src1Global, height * width);
        inQueueSrc.EnQue(srcLocal);
    }
    __aicore__ inline void Compute()
    {
        LocalTensor<T> srcLocal = inQueueSrc.DeQue<T>();
        LocalTensor<T> sumTempLocal = sumQueue.AllocTensor<T>();
        LocalTensor<T> maxTempLocal = maxQueue.AllocTensor<T>();
        LocalTensor<T> dstLocal = outQueueDst.AllocTensor<T>();

        SoftMaxShapeInfo srcShape = { height, width, height, width };
        SoftMax<T>(dstLocal, sumTempLocal, maxTempLocal, srcLocal, tiling, srcShape);

        outQueueDst.EnQue<T>(dstLocal);
        maxQueue.FreeTensor(maxTempLocal);
        sumQueue.FreeTensor(sumTempLocal);
        inQueueSrc.FreeTensor(srcLocal);
    }
    __aicore__ inline void CopyOut()
    {
        LocalTensor<T> dstLocal = outQueueDst.DeQue<T>();
        DataCopy(dstGlobal, dstLocal, height * width);
        outQueueDst.FreeTensor(dstLocal);
    }

private:
    TPipe pipe;
    TQue<QuePosition::VECIN, 1> inQueueSrc;
    TQue<QuePosition::VECIN, 1> maxQueue;
    TQue<QuePosition::VECIN, 1> sumQueue;
    TQue<QuePosition::VECOUT, 1> outQueueDst;
    GlobalTensor<T> src1Global, dstGlobal;
    uint32_t elementNumPerBlk = 0;
    uint32_t width = 64;
    uint32_t height = 320;
    SoftMaxTiling tiling;
};
} // namespace AscendC

extern "C" __global__ __aicore__ void softmax_kernel_half(__gm__ uint8_t* srcGm, __gm__ uint8_t* dstGm, __gm__ uint8_t* tiling)
{
    GET_TILING_DATA(tilingData, tiling);
    AscendC::KernelSoftmax<half> op;
    op.Init(srcGm, dstGm, tilingData.softmaxTilingData);
    op.Process();
}