Copy

Applicability

Product

Supported

Atlas A3 training products / Atlas A3 inference products

Atlas A2 training products / Atlas A2 inference products

Atlas 200I/500 A2 inference products

Atlas inference product 's AI Core

x

Atlas inference product 's Vector Core

x

Atlas training products

x

Function

Moves data between VECIN, VECCALC, and VECOUT, which supports the mask operation and data block interval operation.

Prototype

  • High-dimensional tensor sharding computation
    • Bitwise mask mode
      1
      2
      template <typename T, bool isSetMask = true>
      __aicore__ inline void Copy(const LocalTensor<T>& dst, const LocalTensor<T>& src, const uint64_t mask[], const uint8_t repeatTime, const CopyRepeatParams& repeatParams)
      
    • Contiguous mask mode
      1
      2
      template <typename T, bool isSetMask = true>
      __aicore__ inline void Copy(const LocalTensor<T>& dst, const LocalTensor<T>& src, const uint64_t mask, const uint8_t repeatTime, const CopyRepeatParams& repeatParams)
      

Parameters

Table 1 Template parameters

Parameter

Description

T

Data type of the operand.

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

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

For Atlas 200I/500 A2 inference products , the supported data types are uint16_t, int16_t, half, bfloat16_t, uint32_t, int32_t, and float.

isSetMask

Indicates whether to set mask inside the API.

  • true: sets mask inside the API.
  • false: sets mask outside the API. Developers need to use the SetVectorMask API to set the mask value. In this mode, the mask value in the input parameter of this API must be set to the placeholder MASK_PLACEHOLDER.
Table 2 Parameters

Parameter

Input/Output

Description

dst

Output

Destination operand.

The type is LocalTensor, and the supported TPosition is VECIN/VECCALC/VECOUT. The start address must be 32-byte aligned.

src

Input

Source operand.

The type is LocalTensor, and the supported TPosition is VECIN/VECCALC/VECOUT. The start address must be 32-byte aligned.

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

mask/mask[]

Input

mask is used to control the elements that participate in computation in each iteration.

  • Bitwise mode: controls which elements are involved in computation bit by bit. A bit value of 1 means the corresponding element participates in computation, while 0 means it does not.

    The mask value is an array. The array length and the value range of the array elements are related to the operand data type. When the operand is 16-bit, the array length is 2, mask[0] and mask[1] ∈ [0, 264 -1] and cannot be 0 at the same time. When the operand is 32-bit, the array length is 1 and mask[0] ∈ (0, 264 – 1]. When the operand is 64-bit, the array length is 1 and mask[0] ∈ (0, 232 – 1].

    For example, if mask = [0, 8] and 8 = 0b1000, only the fourth element participates in computation.

  • Contiguous mode: indicates the number of contiguous elements that participate in computation. The value range is related to the operand data type. The maximum number of elements that can be processed in each repeat varies according to the data type. When the operand is 16-bit, mask ∈ [1, 128]. When the operand is 32-bit, mask ∈ [1, 64]. When the operand is 64-bit, mask ∈ [1, 32].

repeatTime

Input

Number of iteration repeats. The vector compute unit reads 256 bytes of contiguous data for computation each time. To process the input data, the data needs to be read and computed over multiple repeats. repeatTime indicates the number of repeats.

For details about this parameter, see High-dimensional Sharding APIs.

repeatParams

Input

Data structure that controls the operand address strides. It is of the CopyRepeatParams type.

For details, see ${INSTALL_DIR}/include/ascendc/basic_api/interface/kernel_struct_data_copy.h. Replace ${INSTALL_DIR} with the actual CANN component directory.

For details about the parameter description, see Table 3.

Table 3 Parameters in the CopyRepeatParams structure

Parameter

Description

dstStride and srcStride

Address stride of data blocks in the same iteration. The value range is [0, 65535].

For details about the address stride parameters of data blocks in the same iteration, see dataBlockStride.

dstRepeatSize and srcRepeatSize

Address stride between adjacent iterations. The value range is [0, 4095].

For details about the address step between adjacent iterations, see repeatStride.

Returns

None

Restrictions

  • The start addresses of the source and destination operands must be 32-byte aligned.
  • Like the vector calculation APIs, the copy API can be used together with the mask operation API. However, when the high-dimensional sharding computation API is used with the counter mode, it is different from the general counter mode. Specifically:
    • General counter mode: The mask parameter indicates the number of elements participating in the entire vector computation, and the number of iterations is ignored.
    • Counter mode combined with the copy high-dimensional sharding computation API: The mask parameter indicates the number of elements processed in each repeat, and the number of iterations takes effect. The following figure shows the details.

Example

This example shows only part of the code involved in the computation process. For the complete code, see Template Sample.

In this example, the operand data type is int16_t.

  • Contiguous mask mode
    1
    2
    3
    4
    5
    uint64_t mask = 128;
    // repeatTime = 4, 128 elements one repeat, 512 elements total
    // dstStride, srcStride = 1, no gap between blocks in one repeat
    // dstRepStride, srcRepStride = 8, no gap between repeats
    AscendC::Copy(dstLocal, srcLocal, mask, 4, { 1, 1, 8, 8 });
    

    Result example:

    Input (srcLocal): [9 -2 8 ... 9]
    Output (dstLocal):
    [9 -2 8 ... 9]
  • Bitwise mask mode
    1
    2
    3
    4
    5
    uint64_t mask[2] = { UINT64_MAX, UINT64_MAX };
    // repeatTime = 4, 128 elements one repeat, 512 elements total
    // dstStride, srcStride = 1, no gap between blocks in one repeat
    // dstRepStride, srcRepStride = 8, no gap between repeats
    AscendC::Copy(dstLocal, srcLocal, mask, 4, { 1, 1, 8, 8 });
    

    Result example:

    Input (srcLocal): [9 -2 8 ... 9]
    Output (dstLocal):
    [9 -2 8 ... 9]

Template Sample

 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
#include "kernel_operator.h"
class KernelCopy {
public:
    __aicore__ inline KernelCopy() {}
    __aicore__ inline void Init(__gm__ uint8_t* srcGm, __gm__ uint8_t* dstGm)
    {
        srcGlobal.SetGlobalBuffer((__gm__ int32_t*)srcGm);
        dstGlobal.SetGlobalBuffer((__gm__ int32_t*)dstGm);
        pipe.InitBuffer(inQueueSrc, 1, 512 * sizeof(int32_t));
        pipe.InitBuffer(outQueueDst, 1, 512 * sizeof(int32_t));
    }
    __aicore__ inline void Process()
    {
        CopyIn();
        Compute();
        CopyOut();
    }
private:
    __aicore__ inline void CopyIn()
    {
        AscendC::LocalTensor<int32_t> srcLocal = inQueueSrc.AllocTensor<int32_t>();
        AscendC::DataCopy(srcLocal, srcGlobal, 512);
        inQueueSrc.EnQue(srcLocal);
    }
    __aicore__ inline void Compute()
    {
        AscendC::LocalTensor<int32_t> srcLocal = inQueueSrc.DeQue<int32_t>();
        AscendC::LocalTensor<int32_t> dstLocal = outQueueDst.AllocTensor<int32_t>();
        uint64_t mask = 64;
        AscendC::Copy(dstLocal, srcLocal, mask, 4, { 1, 1, 8, 8 });
        outQueueDst.EnQue<int32_t>(dstLocal);
        inQueueSrc.FreeTensor(srcLocal);
    }
    __aicore__ inline void CopyOut()
    {
        AscendC::LocalTensor<int32_t> dstLocal = outQueueDst.DeQue<int32_t>();
        AscendC::DataCopy(dstGlobal, dstLocal, 512);
        outQueueDst.FreeTensor(dstLocal);
    }
private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, 1> inQueueSrc;
    AscendC::TQue<AscendC::TPosition::VECOUT, 1> outQueueDst;
    AscendC::GlobalTensor<int32_t> srcGlobal, dstGlobal;
};
extern "C" __global__ __aicore__ void copy_simple_kernel(__gm__ uint8_t* srcGm, __gm__ uint8_t* dstGm)
{
    KernelCopy op;
    op.Init(srcGm, dstGm);
    op.Process();
}