SimpleSoftMax
Applicability
|
Product |
Supported |
|---|---|
|
Atlas 350 Accelerator Card |
√ |
|
|
√ |
|
|
√ |
|
|
√ |
|
|
√ |
|
|
x |
|
|
x |
Function Usage
If the product of non-last axis lengths of the input tensor [m0, m1, ..., mt, n] (t ≥ 0) is considered as m, the shape of the input tensor is [m, n]. Perform the following computation on the input tensor [m, n] by row. Unlike the SoftMax API, this API does not include the reduction process to compute the sum and max data. Instead, it uses the computed sum and max data to perform SoftMax computation on the input tensor. The formula is as follows:

For ease of understanding, the formula expressed through a Python script is as follows, where src, max, and sum are the source operands (input), and dst is the destination operand (output).
1 2 3 |
def simple_softmax(src, max, sum): dst = np.exp(src - max)/sum return dst |
Principles
The following figure shows the internal algorithm diagram of the SimpleSoftMax high-level APIs by taking the input tensor of the float type, in ND format, and with shape [m, k] as an example.
The computation process is divided into the following steps, all of which are performed on vectors:
1. sub: Subtract max from all data of input x by row.
2. exp: Compute exp for all data after sub.
3. div: Divide all data generated after exp by sum at each row to obtain the final result.
Prototype
- Allocate the temporary space through the API framework.
- The data types of LocalTensor are the same.
1 2
template <typename T, bool isReuseSource = false, bool isBasicBlock = false, bool isDataFormatNZ = false, const SoftmaxConfig& config = SOFTMAX_DEFAULT_CFG> __aicore__ inline void SimpleSoftMax(const LocalTensor<T>& dstTensor, const LocalTensor<T>& inSumTensor, const LocalTensor<T>& inMaxTensor, const LocalTensor<T>& srcTensor, const SoftMaxTiling& tiling, const SoftMaxShapeInfo& softmaxShapeInfo = {})
- The data types of LocalTensor are different.
1 2
template <typename T, bool isReuseSource = false, bool isBasicBlock = false, bool isDataFormatNZ = false, const SoftmaxConfig& config = SOFTMAX_DEFAULT_CFG> __aicore__ inline void SimpleSoftMax(const LocalTensor<half>& dstTensor, const LocalTensor<float>& inSumTensor, const LocalTensor<float>& inMaxTensor, const LocalTensor<half>& srcTensor, const SoftMaxTiling& tiling, const SoftMaxShapeInfo& softmaxShapeInfo = {})
- The data types of LocalTensor are the same.
- Pass the temporary space through the sharedTmpBuffer input parameter.
- The data types of LocalTensor are the same.
1 2
template <typename T, bool isReuseSource = false, bool isBasicBlock = false, bool isDataFormatNZ = false, const SoftmaxConfig& config = SOFTMAX_DEFAULT_CFG> __aicore__ inline void SimpleSoftMax(const LocalTensor<T>& dstTensor, const LocalTensor<T>& inSumTensor, const LocalTensor<T>& inMaxTensor, const LocalTensor<T>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const SoftMaxTiling& tiling, const SoftMaxShapeInfo& softmaxShapeInfo = {})
- The data types of LocalTensor are different.
1 2
template <typename T, bool isReuseSource = false, bool isBasicBlock = false, bool isDataFormatNZ = false, const SoftmaxConfig& config = SOFTMAX_DEFAULT_CFG> __aicore__ inline void SimpleSoftMax(const LocalTensor<half>& dstTensor, const LocalTensor<float>& inSumTensor, const LocalTensor<float>& inMaxTensor, const LocalTensor<half>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const SoftMaxTiling& tiling, const SoftMaxShapeInfo& softmaxShapeInfo = {})
- The data types of LocalTensor are the same.
Due to the complex computation involved in the internal implementation of this API, additional temporary space is required to store intermediate variables generated during computation. The temporary space can be allocated through the API framework or passed by developers through the sharedTmpBuffer input parameter.
- When the API framework is used for temporary space allocation, developers do not need to allocate the space, but must reserve the required size for the space.
- When the sharedTmpBuffer input parameter is used for passing the temporary space, the tensor serves as the temporary space. In this case, the API framework is not required for temporary space allocation. This enables developers to manage the sharedTmpBuffer space and reuse the buffer after calling the API, so that the buffer is not repeatedly allocated and deallocated, improving the flexibility and buffer utilization.
If the API framework is used, developers must reserve the temporary space. If sharedTmpBuffer is used, developers must allocate space for the tensor. The method of obtaining the temporary space size (BufferSize) is as follows: Obtain the required maximum and minimum temporary space sizes using the GetSoftMaxMaxTmpSize/GetSoftMaxMinTmpSize API provided in SoftMax/SimpleSoftMax Tiling. The minimum space can ensure correct functionality, while the maximum space is used to improve performance.
Parameters
|
Parameter |
Description |
||||
|---|---|---|---|---|---|
|
T |
Data type of the operand. For the Atlas 350 Accelerator Card, the supported data types are half and float. For the For the For the For the |
||||
|
isReuseSource |
This parameter is reserved. Pass the default value false. |
||||
|
isBasicBlock |
If the shape information and tiling strategy of both srcTensor and dstTensor meet the base block requirements, this parameter can be enabled to improve performance. By default, this parameter is disabled. Use either of the following methods to determine whether the base block requirements are met:
For the Atlas 200/500 A2 Inference Product, this parameter is reserved for future function extension. Retain the default value. |
||||
|
isDataFormatNZ |
Whether the current input and output data is in NZ format. The default data format is ND, that is, the default value of this parameter is false. For the Atlas 200/500 A2 Inference Product, the NZ format is not supported. |
||||
|
config |
(Optional) structure template parameter, which is of the SoftmaxConfig type. The definition is as follows:
A configuration example is as follows:
This parameter is used together with the tiling computation API on the kernel. Note: The priority of the config parameter is lower than that of the template parameter isBasicBlock. If isBasicBlock is enabled, the API splits base blocks for optimization, and the constant shape of the config parameter does not take effect. For the Atlas 350 Accelerator Card, this parameter is reserved for future use. Retain the default value. For the For the For the For the |
|
Parameter |
Input/Output |
Description |
||
|---|---|---|---|---|
|
dstTensor |
Output |
Destination operand. The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT. The shape of dstTensor is the same as that of the source operand srcTensor. |
||
|
inSumTensor |
Input |
Source operand. The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT. Sum value required for softmax computation.
|
||
|
inMaxTensor |
Input |
Source operand. The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT. Max value required for softmax computation.
|
||
|
srcTensor |
Input |
Source operand. The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT. The length of the last axis must be 32-byte aligned. |
||
|
sharedTmpBuffer |
Input |
Temporary space. The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT. The data type of this operand is fixed at uint8_t. This parameter is used to store intermediate variables during complex internal API computation and is provided by developers. For details about how to obtain the temporary space size (BufferSize), see SoftMax/SimpleSoftMax Tiling. |
||
|
tiling |
Input |
Tiling information required for softmax computation. For details about how to obtain the tiling information, see SoftMax/SimpleSoftMax Tiling. |
||
|
softmaxShapeInfo |
Input |
Shape of srcTensor, SoftMaxShapeInfo type. The specific definition is as follows:
Note that when the input and output data is in NZ format, the last axis length is the length of the reduce axis, that is, W0 × W1 in Figure 2 and the length of each non-last axis is H0 × H1. |
Returns
None
Restrictions
- The tensor space of srcTensor and dstTensor can be reused.
- inSumTensor and inMaxTensor are inputs, and the length of the last axis must be fixed at 32 bytes.
- The data types of inSumTensor and inMaxTensor must be the same.
- For details about the operand address alignment requirements, see General Address Alignment Restrictions.
- The address of sharedTmpBuffer cannot overlap that of the source or destination operand.
- When srcM ! is set to oriSrcM or srcK ! is set to oriSrcK in softmaxShapeInfo, for the original input (oriSrcM, oriSrcK) on the GM, you need to pad data to (srcM, srcK) in the M or K direction. The padded data will be involved in some computation. In the scenario where the input and output are reused, the computation result of the API will overwrite the original data padded to the srcTensor. In the scenario where the input and output are not reused, the computation result of the API will overwrite the data in dstTensor corresponding to the padded position of srcTensor.
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// dstLocal: tensor for storing the SimpleSoftMax computation result // sumTempLocal: tensor for storing the sum value required for SimpleSoftMax computation // maxTempLocal: tensor for storing the max value required for SimpleSoftMax computation // srcLocal: input tensor for storing data for SimpleSoftMax computation // sharedTmpBuffer: tensor for storing the temporary buffer during SimpleSoftMax computation // softmaxTiling: structure for storing the tiling information required for SimpleSoftMax computation, which can be obtained through the SoftMaxTilingFunc API AscendC::SoftMaxShapeInfo softmaxInfo( /* Product of lengths of non-last axes */ srcM, /* Length of the last axis, which must be 32-byte aligned */ srcK, /* Product of lengths of original non-last axes */ oriSrcM, /* Length of the original last axis */ oriSrcK ); // Pass the temporary space through the sharedTmpBuffer input parameter and pass the template parameter to turn the shape into a constant value. AscendC::SimpleSoftMax<T, false, false, false, static_config>(dstLocal, sumTempLocal, maxTempLocal, srcLocal, sharedTmpBuffer, softmaxTiling, softmaxInfo); // Pass the temporary space through the sharedTmpBuffer input parameter. AscendC::SimpleSoftMax<T>(dstLocal, sumTempLocal, maxTempLocal, srcLocal, sharedTmpBuffer, softmaxTiling, softmaxInfo); // Allocate the temporary space through the API framework. AscendC::SimpleSoftMax<T>(dstLocal, sumTempLocal, maxTempLocal, srcLocal, softmaxTiling, softmaxInfo); |
Result example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
Input (srcLocal): [[-100. -80. -60. -50. -30. -20. -15. -10. ] [ -9. -8. -7. -6. -5. -4. -3. -2. ] [ -1.5 -1. -0.8 -0.6 -0.5 -0.45 -0.4 -0.35 ] [ -0.3 -0.25 -0.2 -0.15 -0.1 -0.05 -0.01 -0.001] [ 0. 0.001 0.01 0.05 0.1 0.15 0.2 0.25 ] [ 0.3 0.35 0.4 0.45 0.5 0.6 0.8 1. ] [ 1.5 2. 3. 4. 5. 6. 7. 8. ] [ 9. 10. 15. 20. 30. 50. 60. 80. ]] Input (sumTempLocal): [[1.0067834 1.0067834 1.0067834 1.0067834 1.0067834 1.0067834 1.0067834 1.0067834] [1.5814459 1.5814459 1.5814459 1.5814459 1.5814459 1.5814459 1.5814459 1.5814459] [5.971886 5.971886 5.971886 5.971886 5.971886 5.971886 5.971886 5.971886 ] [7.051223 7.051223 7.051223 7.051223 7.051223 7.051223 7.051223 7.051223 ] [6.880514 6.880514 6.880514 6.880514 6.880514 6.880514 6.880514 6.880514 ] [5.239974 5.239974 5.239974 5.239974 5.239974 5.239974 5.239974 5.239974 ] [1.5820376 1.5820376 1.5820376 1.5820376 1.5820376 1.5820376 1.5820376 1.5820376] [1. 1. 1. 1. 1. 1. 1. 1. ]] Input (maxTempLocal): [[-10. -10. -10. -10. -10. -10. -10. -10. ] [ -2. -2. -2. -2. -2. -2. -2. -2. ] [ -0.35 -0.35 -0.35 -0.35 -0.35 -0.35 -0.35 -0.35 ] [ -0.001 -0.001 -0.001 -0.001 -0.001 -0.001 -0.001 -0.001] [ 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 ] [ 1. 1. 1. 1. 1. 1. 1. 1. ] [ 8. 8. 8. 8. 8. 8. 8. 8. ] [ 80. 80. 80. 80. 80. 80. 80. 80. ]] Output (dstLocal): [[0. 0. 0. 0. 0. 0.00004509 0.00669255 0.99326235] [0.00057661 0.0015674 0.00426062 0.01158158 0.03148199 0.08557693 0.23262219 0.63233274] [0.05302124 0.08741724 0.10677165 0.13041118 0.14412664 0.1515162 0.15928458 0.16745128] [0.10516749 0.11055954 0.11622806 0.12218719 0.12845187 0.13503774 0.1405487 0.14181937] [0.11318932 0.11330257 0.1143269 0.11899266 0.12509353 0.13150725 0.13824977 0.14533797] [0.09476865 0.09962755 0.10473556 0.11010546 0.1157507 0.12792432 0.15624711 0.19084065] [0.00095032 0.00156681 0.00425903 0.01157725 0.03147022 0.08554492 0.2325352 0.63209623] [0. 0. 0. 0. 0. 0. 0. 1. ]] |