AdjustSoftMaxRes
Applicability
Product |
Supported |
|---|---|
√ |
|
√ |
|
x |
|
√ |
|
x |
|
x |
Function
Adjusts the computation result of SoftMax to a specified value. It is mainly used to postprocess SoftMax computation results. If a specified value exists in the input max, the result in the corresponding softmaxres is adjusted to a user-defined value. The preceding adjustment is performed by row. That is, when the value of max in a row is specified, the value of softmaxres in the corresponding row is adjusted to a user-defined value.
For ease of understanding, the formula expressed through a Python script is as follows, where res is both input and output, and max, from, to, and res_shape are inputs.
1 2 3 4 5 6 | def adjust_softmax_res(res, max, from, to, res_shape): for i in res_shape[0]: if max[i] == from: for j in res_shape[1]: res[i][j] = to return |
Prototype
1 2 | template <typename T1, typename T2, bool isDataFormatNZ = false, uint8_t stepSizeMode = 0> __aicore__ inline bool AdjustSoftMaxRes(const LocalTensor<T1>& softMaxRes, const LocalTensor<T2>& maxTensor, const uint32_t from, const T1 to, const SoftMaxShapeInfo& softmaxShapeInfo) |
Parameters
Parameter |
Description |
|---|---|
T1 |
Data type of softMaxRes. For the For the For the |
T2 |
Data type of maxTensor. For the For the For the |
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. |
stepSizeMode |
Mode of the step length for obtaining elements from maxTensor. The values are as follows:
|
Parameter |
Input/Output |
Description |
||
|---|---|---|---|---|
softMaxRes |
Input/Output |
Source operand and destination operand. The type is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT. For details about the definition of the LocalTensor data structure, see LocalTensor. The length of the last axis must be 32-byte aligned. Its value is the output result of softmax computation. |
||
maxTensor |
Input |
Source operand. The type is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT. Result of reducemax during softmax computation.
|
||
from |
Input |
Source operand of the uint32_t type. Value of maxTensor to be checked. Note that the values of maxTensor are floating point numbers. Therefore, you need to input a hexadecimal value corresponding to a floating point number. For example, if you want to check whether the value of maxTensor is 1.0, set from to the hexadecimal value 0x3f800000 corresponding to 1.0. |
||
to |
Input |
Source operand. The data type is the same as that of softMaxRes. Value to be padded in softMaxRes. |
||
softmaxShapeInfo |
Input |
Shape information of softMaxRes. The structure is defined as follows:
Currently, only ND input is supported. |
Returns
A Boolean value is returned. true indicates that there is a value to be checked exists in maxTensor. false indicates that there is no value to be checked exists in maxTensor.
Restrictions
- For details about the operand address alignment requirements, see General Address Alignment Restrictions.
- 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.
Example
In this sample, postprocessing is performed on the SoftMax computation result to check whether 0xFF7FFFFF exists in maxTensor. If 0xFF7FFFFF exists, the result is updated to 0. In this example, the operator is AdjustSoftMaxResCustom with the fixed shape of input x[32, 32] and output y[32, 32]. The shape of the input softMaxRes is [32, 32], and the shape of the maxTensor is [32, 8]. The data type is float. For a complete operator example, see adjustsoftmaxres operator sample.
1 2 3 4 5 6 7 8 | AscendC::LocalTensor<float> srcLocal = inQueueSrc.DeQue<float>(); AscendC::LocalTensor<float> maxLocal = inQueueMax.DeQue<float>(); AscendC::LocalTensor<float> dstLocal = outQueueDst.AllocTensor<float>(); AscendC::LocalTensor<float> tmpTensor = calcBuf.Get<float>(); AscendC::SoftMaxShapeInfo srcShape = {height, width, height, width}; AscendC::AdjustSoftMaxRes<float, float>(srcLocal, maxLocal, FROM, TO, srcShape); AscendC::DataCopy(tmpTensor, srcLocal, height * width); AscendC::DataCopy(dstLocal, tmpTensor, height * width); |