Pad

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

Pads a two-dimensional tensor (height * width) up to be 32-byte aligned in the width direction. If the width of the tensor is already 32-byte aligned and all data is valid, this API should not be called for padding. This API applies to the following scenarios:

  • Scenario 1

    The width of the tensor is not 32-byte aligned. Take half for padding as an example, such as 16 x 15. Add a column to the right to obtain 16 x 16.

  • Scenario 2

    The width of the tensor is already 32-byte aligned, but there is some redundant data. Take half as an example, such as 16 x 16 (the last two columns are redundant data). After padding, 16 x 16 remains unchanged, but the redundant data in the last two columns can be padded with configured values.

Prototype

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 method of obtaining the temporary space size (BufferSize) is as follows: Obtain the required maximum and minimum temporary space sizes using the GetPadMaxMinTmpSize API provided in Pad Tiling. The minimum space can ensure correct functionality, while the maximum space is used to improve performance.

The temporary space can be allocated through the API framework or passed by developers through the sharedTmpBuffer input parameter. Therefore, there are two types of function prototypes for the Pad API.

  • Pass the temporary space through the sharedTmpBuffer input parameter.
    1
    2
    template <typename T>
    __aicore__ inline void Pad(const LocalTensor<T>& dstTensor, const LocalTensor<T>& srcTensor, PadParams& padParams, const LocalTensor<uint8_t>& sharedTmpBuffer, PadTiling& tiling)
    

    This method requires you to allocate and manage the temporary buffer space on your own, and reuse the buffer after calling the API, so that the buffer is not repeatedly allocated or deallocated, improving the flexibility and buffer utilization.

  • Allocate the temporary space through the API framework.
    1
    2
    template <typename T>
    __aicore__ inline void Pad(const LocalTensor<T>& dstTensor, const LocalTensor<T>& srcTensor, PadParams& padParams, PadTiling& tiling)
    

    When using this method, you do not need to allocate the space, but must reserve the required temporary space.

Parameters

Table 1 Template parameters

Parameter

Description

T

Operand data type.

For the Atlas 350 Accelerator Card, the supported data types are int16_t, uint16_t, half, int32_t, uint32_t, and float.

For the Atlas A3 training product/Atlas A3 inference product, the supported data types are int16_t, uint16_t, half, int32_t, uint32_t, and float.

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

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

Table 2 API parameters

Parameter

Input/Output

Description

dstTensor

Output

Destination operand, with a two-dimensional shape. For details about the definition of the LocalTensor data structure, see LocalTensor.

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

srcTensor

Input

Source operand, with a two-dimensional shape. For details about the definition of the LocalTensor data structure, see LocalTensor.

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

padParams

Input

Pad parameter of the PadParams type. The parameters in the PadParams structure are as follows:

  • leftPad: data volume for left padding. The unit is column.
  • rightPad: data volume for right padding. The unit is column.
  • padValue: padded value, which can be int32_t.

The definition of the PadParams structure is as follows:

1
2
3
4
5
struct PadParams {
    uint16_t leftPad = 0;
    uint16_t rightPad = 0;
    int32_t padValue = 0;
};

sharedTmpBuffer

Input

Shared buffer, which is used to store temporary data generated during internal API computation. This enables you 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. For details about how to obtain the size of the shared buffer, see Pad Tiling.

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

tiling

Input

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

Returns

None

Constraints

  • In scenario 1, padding can be performed on both the left and right simultaneously.
  • In scenario 2, padding can be performed only on the right.
  • The total width after padding cannot exceed the width obtained after the original width is aligned to the nearest 32 bytes.
  • The data volume of the source operand must be 32-byte aligned.

Examples

This is an example for scenario 1. The width of the tensor is not 32-byte aligned. Take half for padding as an example, such as 16 x 15. Add a column to the right to obtain 16 x 16. The input data type is half. For details about the complete example, see Pad operator sample.

1
2
3
4
5
// dstLocal: output tensor
// srcLocal: input tensor
// padParams: padding parameters
AscendC::PadParams padParams{0, 1, 321}; // No column is padded on the left, 1 column is padded on the right, and the padding value is 321.
AscendC::Pad(dstLocal, srcOutLocal, padParams, tilingData.padTilingData);
 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
Input data:
0 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 37 38 39 40 41 42 43 44
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

Output data:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 321
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 321
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 321
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 321
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 321
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 321
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 321
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 321
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 321
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 321
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 321
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 321
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 321
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 321
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 321
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 321