Slice Data Transfer

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

Supports slice data transfer, which extracts subsets of multi-dimensional tensor data for transfer.

Prototype

  • Global Memory -> Local Memory
    1
    2
    template <typename T>
    __aicore__ inline void DataCopy(const LocalTensor<T>& dst, const GlobalTensor<T>& src, const SliceInfo dstSliceInfo[], const SliceInfo srcSliceInfo[], const uint32_t dimValue = 1)
    
  • Local Memory -> Global Memory
    1
    2
    template <typename T>
    __aicore__ inline void DataCopy(const GlobalTensor<T> &dst, const LocalTensor<T> &src, const SliceInfo dstSliceInfo[], const SliceInfo srcSliceInfo[], const uint32_t dimValue = 1)
    

For details about supported transfer paths and data types of each prototype, see Supported Paths and Data Types.

Parameters

Table 1 Template parameters

Parameter

Description

T

Data types of the source and destination operands. For details about supported data types, see Supported Paths and Data Types.

Table 2 Parameters of the slice data transfer API

Parameter

Input/Output

Description

dst

Output

Destination operand, which is of the LocalTensor or GlobalTensor type.

src

Input

Source operand, which is of the LocalTensor or GlobalTensor type.

srcSliceInfo/dstSliceInfo

Input

Destination operand/Source operand slice information, which is of the SliceInfo type.

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

dimValue

Input

Operand dimension information. The default value is 1.

Table 3 Parameters in the SliceInfo structure

Parameter

Description

startIndex

Location of the start element of a slice.

endIndex

Location of the end element of a slice.

stride

Number of interval elements of the slice.

burstLen

Length of each horizontal data slice. This parameter only takes effect when the dimension is 1, and must be set to 1 (cannot be assigned any other values) for dimensions greater than 1. The unit is data block (32 bytes). For example, if the srcSliceInfo list is {{16, 70, 7, 3, 87}, {0, 2, 1, 1, 3}}, {16, 70, 7, 3, 87} refers to the slicing information of the first dimension, where burstLen is set to 3, meaning each sliced data segment occupies 3 data blocks, and {0, 2, 1, 1, 3} refers to the slicing information of the second dimension, where burstLen can only be set to 1.

shapeValue

Original length of the current dimension. The unit is the number of elements.

The preceding parameters are parsed by using specific examples. The following figure shows the details.

Figure 1 Parameter parsing
  • dimValue of 2 indicates that the operand has 2 dimensions.
  • srcSliceInfo is {{16, 70, 7, 3, 87}, {0, 2, 1, 1, 3}}.
    • {16, 70, 7, 3, 87}: For a single row in the one-dimensional view, each value corresponds to a specific parameter value:

      startIndex = 16 indicates that the valid data segment starts from the 16th element.

      endIndex = 70 indicates that the valid data segment ends at the 70th element.

      stride = 7, in units of elements, indicates that there are 7 zero-valued elements between two adjacent sliced data segments.

      burstLen = 3, in units of 32 bytes, indicates that in this valid data segment, the size of a sliced data segment is 3 data blocks.

      shapeValue = 87 indicates the total length of the single row in elements, which equals 8 × 10 + 7 = 87 elements.

    • {0, 2, 1, 1, 3}: For multiple rows in the two-dimensional view, each value corresponds to a row-related parameter value:

      startIndex = 0 indicates that the valid data segment starts from row 0.

      endIndex = 2 indicates that the valid data segment ends at row 2.

      stride = 1 indicates that there is one row between two adjacent sliced data segments.

      burstLen = 1, which is mandatory when dimValue is greater than 1.

      shapeValue = 3 indicates that there are three rows in total.

  • dstSliceInfo is {{0, 47, 0, 3, 48}, {0, 1, 0, 1, 2}}.
    • {0, 47, 0, 3, 48}: For a single row in the one-dimensional view, each value corresponds to a specific parameter value:

      startIndex = 0 indicates that the valid data segment starts from the 0th element.

      endIndex = 47 indicates that the valid data segment ends at the 47th element.

      stride = 0, in units of elements, indicates that there is no gap between two adjacent sliced data segments.

      burstLen = 3, in units of 32 bytes, indicates that in this valid data segment, the size of a sliced data segment is 3 data blocks.

      shapeValue = 48 indicates the total length of the single row in elements, which equals 8 × 6 = 48 elements.

    • {0, 1, 0, 1, 2}: For multiple rows in the two-dimensional view, each value corresponds to a row-related parameter value:

      startIndex = 0 indicates that the valid data segment starts from row 0.

      endIndex = 1 indicates that the valid data segment ends at row 1.

      stride = 0 indicates that there is no gap between two adjacent sliced data segments.

      burstLen = 1, which is mandatory when dimValue is greater than 1.

      shapeValue = 2 indicates that there are two rows in total.

Returns

None

Restrictions

  • When configuring the horizontal burstLen for slice data transfer, calculate it using the formula: Number of horizontal slice elements × sizeof(T)/32 bytes. The product of the number of horizontal slice elements and sizeof(T) must be a multiple of 32 bytes.
  • The array size of the SliceInfo structure must match dimValue and must not exceed 8.
  • The array sizes of srcSliceInfo and dstSliceInfo must be identical, and their respective burstLen values must be equal, that is, srcSliceInfo[i].burstLen = dstSliceInfo[i].burstLen.
  • Slice data transfer has certain requirements on parameters. You are advised to refer to the calling examples and verify correctness through CPU simulation before executing on the NPU.

Supported Paths and Data Types

The following data paths are expressed using logical positions TPosition, with the corresponding physical paths noted. For details about the mapping between TPosition and the physical memory, see Table 1.

Table 4 Specific paths and supported data types of Global Memory -> Local Memory

Product Model

Data Path

Data Types of the Source and Destination Operands (Same)

Atlas 350 Accelerator Card

GM -> VECIN (GM -> UB)

bool, int8_t, uint8_t, hifloat8_t, fp8_e5m2_t, fp8_e4m3fn_t, fp8_e8m0_t, int16_t, uint16_t, half, bfloat16_t, int32_t, uint32_t, float, complex32, int64_t, uint64_t, double, complex64

Atlas inference product AI Core

GM -> VECIN (GM -> UB)

int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, half, float

Atlas A2 training product/Atlas A2 inference product

GM -> VECIN (GM -> UB)

int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, half, bfloat16_t, float

Atlas A3 training product/Atlas A3 inference product

GM -> VECIN (GM -> UB)

int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, half, bfloat16_t, float

Table 5 Specific paths and supported data types of Local Memory -> Global Memory

Product Model

Data Path

Data Types of the Source and Destination Operands (Same)

Atlas 350 Accelerator Card

VECOUT -> GM (UB -> GM)

bool, int8_t, uint8_t, hifloat8_t, fp8_e5m2_t, fp8_e4m3fn_t, fp8_e8m0_t, int16_t, uint16_t, half, bfloat16_t, int32_t, uint32_t, float, complex32, int64_t, uint64_t, double, complex64

Atlas inference product AI Core

VECOUT, CO2 -> GM (UB -> GM)

int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, half, float

Atlas A2 training product/Atlas A2 inference product

VECOUT -> GM (UB -> GM)

int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, half, bfloat16_t, float

Atlas A3 training product/Atlas A3 inference product

VECOUT -> GM (UB -> GM)

int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, half, bfloat16_t, float

Examples

For details about the parsing and result examples of srcSliceInfo and dstSliceInfo, see Figure 1.

// srcLocal and dstLocal are LocalTensors of the int32_t type, and srcGlobal and dstGlobal are GlobalTensors of the int32_t type.
uint32_t dimValue = 2; // The operand dimension is 2.
AscendC::SliceInfo srcSliceInfo[] = {{16, 70, 7, 3, 87}, {0, 2, 1, 1, 3}};
AscendC::SliceInfo dstSliceInfo[] = {{0, 47, 0, 3, 48}, {0, 1, 0, 1, 2}};
// Global Memory -> Local Memory
AscendC::DataCopy(srcLocal, srcGlobal,  dstSliceInfo, srcSliceInfo, dimValue);
// Local Memory -> Global Memory
AscendC::DataCopy(dstGlobal, dstLocal, dstSliceInfo, dstSliceInfo, dimValue);