LogSoftMax

Applicability

Product

Supported

Atlas 350 Accelerator Card

Atlas A3 training product / Atlas A3 inference product

Atlas A2 training product / Atlas A2 inference product

Atlas 200I/500 A2 inference product

x

Atlas inference product AI Core

Atlas inference product Vector Core

x

Atlas training product

x

Function Usage

Performs LogSoftmax computation on the input tensor. Below is the formula.

For ease of understanding, the formula expressed through a Python script is as follows, where src is the source operand (input), and dst, sum, and max are the destination operands (output).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def log_softmax(src):
    # Perform rowmax (taking the maximum value by row) processing along the last axis.
    max = np.max(src, axis=-1, keepdims=True)
    sub = src - max
    exp = np.exp(sub)
   # Perform rowsum (taking the sum by row) processing along the last axis.
    sum = np.sum(exp, axis=-1, keepdims=True)
    dst = exp / sum
    dst = np.log10(dst)
    return dst, max, sum

Principles

The following figure shows the internal algorithm diagram of the LogSoftMax high-level APIs by taking the input tensor of the float type, in ND format, and with shape [m, k] as an example.

Figure 1 Diagram of the LogSoftMax algorithm

The computation process is divided into the following steps, all of which are performed on vectors:

  1. reducemax: Compute the maximum value of each row of input x to obtain [m, 1]. The computation result is saved to a temporary space (temp).
  2. broadcast: Pad the data [m, 1] in temp by data block. For example, for the float type, extend [m, 1] to [m, 8] and output max.
  3. sub: Subtract max from all data of input x by row.
  4. exp: Calculate exp for all data after sub.
  5. reducesum: Sum up each row of data after exp is performed to obtain [m, 1]. The computation result is saved to temp.
  6. broadcast: Pad [m, 1] in temp by data block. For example, for the float type, extend [m, 1] to [m, 8] and output sum.
  7. div: Divide all data generated after exp by sum at each row.
  8. log: Perform log10 computation on all data after div by row and output y.

Prototype

1
2
template <typename T, bool isReuseSource = false, bool isDataFormatNZ = false>
__aicore__ inline void LogSoftMax(const LocalTensor<T>& dst, const LocalTensor<T>& sum, const LocalTensor<T>& max, const LocalTensor<T>& src, const LocalTensor<uint8_t>& sharedTmpBuffer, const LogSoftMaxTiling& tiling, const SoftMaxShapeInfo& softmaxShapeInfo = {})

Due to the complex mathematical computation involved in the internal implementation of this API, extra temporary space is required to store intermediate variables generated during computation. The temporary space needs to be passed through the sharedTmpBuffer input parameter by developers. To obtain the size of the temporary space (BufferSize) to be reserved, use the API provided in LogSoftMax Tiling.

Parameters

Table 1 Template 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 Atlas A3 training product / Atlas A3 inference product , the supported data types are half and float.

For the Atlas A2 training product / Atlas A2 inference product , the supported data types are half and float.

For the Atlas inference product AI Core, the supported data types are half and float.

isReuseSource

Whether the source operand can be modified. This parameter is reserved. Pass the default value false.

isDataFormatNZ

Whether the source operand is in NZ format. The default value is false.

Table 2 API parameters

Parameter

Input/Output

Description

dst

Output

Destination operand.

The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT.

The length of the last axis must be 32-byte aligned.

sum

Output

reduceSum operand.

The reduceSum operand must have the same data type as the destination operand.

The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT.

  • The length of the last axis of sum is fixed at 32 bytes. It is the length of a data block. All data in this data block shares a common value. For example, in the half data type, all 16 numbers in this data block possess an identical reducesum value.
  • The length of the non-last axis is the same as that of the destination operand.

max

Output

reduceMax operand.

The reduceMax operand must have the same data type as the destination operand.

The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT.

  • The length of the last axis of max is fixed at 32 bytes. It is the length of a data block. All data in this data block has the same value. For example, in the half data type, all 16 numbers in this data block possess an identical reducemax value.
  • The length of the non-last axis is the same as that of the destination operand.

src

Input

Source operand.

The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT.

The source operand must have the same data type as the destination operand.

sharedTmpBuffer

Input

Temporary buffer. For details about how to obtain the temporary space size (BufferSize), see LogSoftMax Tiling.

The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT.

tiling

Input

Tiling information required for LogSoftMax computation. For details about how to obtain the tiling information, see LogSoftMax Tiling.

softmaxShapeInfo

Input

Shape of src, which is of the SoftMaxShapeInfo type. The definition is as follows:

1
2
3
4
5
6
struct SoftMaxShapeInfo {
    uint32_t srcM; // Product of lengths of non-last axes
    uint32_t srcK; // Length of the last axis, which must be 32-byte aligned
    uint32_t oriSrcM; // Product of lengths of original non-last axes
    uint32_t oriSrcK;  // Length of the original last axis
};

Note that when the input and output data format is NZ (FRACTAL_NZ), the length of the last axis is the length of the reduced axis, which is W0 × W1 in Figure 2. The length of each non-last axis is H0 × H1.

Returns

None

Constraints

  • The value range of the input source data must be [-2147483647.0, 2147483647.0]. If the input is not within the range, the output is invalid.
  • The source operand address must not overlap the destination operand address.
  • The address of sharedTmpBuffer cannot overlap that of the source or destination operand.
  • 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.

Examples

For details about the complete example, click logsoftmax operator sample.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// dstLocal: tensor for storing the LogSoftMax computation result
// sumLocal: tensor for storing the reducesum result during LogSoftMax computation
// maxLocal: tensor for storing the reduceMax result during LogSoftMax computation
// srcLocal: tensor for storing the LogSoftMax computation input
// sharedTmpBuffer: tensor for storing the temporary buffer during LogSoftMax computation
// softmaxTiling: structure for storing the tiling information required for LogSoftMax computation, which can be obtained through the LogSoftMaxTilingFunc 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
);
AscendC::LogSoftMax<DTYPE_X, false>(dstLocal, sumLocal, maxLocal, srcLocal, sharedTmpBuffer, 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.   ]]
Output (maxLocal):
[[-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 (sumLocal):
[[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.       ]]
Output (dstLocal):
[[-39.08944    -30.40355    -21.71766    -17.374716    -8.688826    -4.345881    -2.1744084   -0.00293603]
 [ -3.2391157   -2.8048213   -2.3705268   -1.9362322   -1.5019379   -1.0676433   -0.6333489   -0.19905435]
 [ -1.2755501   -1.0584029   -0.971544    -0.88468516  -0.8412557   -0.8195409   -0.7978263   -0.77611154]
 [ -0.97811854  -0.9564038   -0.93468904  -0.9129743   -0.89125955  -0.86954486  -0.85217315  -0.84826446]
 [ -0.9461945   -0.94576025  -0.94185156  -0.92447984  -0.90276515  -0.88105035  -0.8593356   -0.8376209 ]
 [ -1.0233353   -1.0016205   -0.97990584  -0.95819116  -0.93647635  -0.89304686  -0.806188    -0.7193291 ]
 [ -3.022131    -2.8049836   -2.3706892   -1.9363947   -1.5021002   -1.0678058   -0.6335113   -0.1992168 ]
 [-30.834908   -30.400614   -28.229141   -26.057669   -21.714724   -13.028834    -8.685889     0.        ]]