ReinterpretCast

Applicability

Product

Supported

Atlas 350 Accelerator Card

Atlas A3 training product/Atlas A3 inference product

x

Atlas A2 training product/Atlas A2 inference product

x

Atlas 200I/500 A2 inference product

x

Atlas inference product AI Core

x

Atlas inference product Vector Core

x

Atlas training product

x

Function Usage

Reinterprets the current GlobalTensor to a new type specified by the user. The converted tensor has the same address and content as the original tensor, with the memory size (number of bits) unchanged.

Prototype

1
2
template <typename CAST_T>
__aicore__ inline GlobalTensor<CAST_T> ReinterpretCast() const

Parameters

Table 1 Template parameters

Parameter

Description

CAST_T

A new type after reinterpretation.

Returns

Returns GlobalTensor after reinterpretation.

Restrictions

After the data type is converted, the number of elements may not be rounded up. For example, if three int4b_t elements are converted to uint32_t elements, the GetSize API can only be called to obtain the rounded-down integer. In this scenario, an alarm is reported when the CPU is running.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
uint64_t dataSize = 256; // Set the size of input_global to 256.

AscendC::GlobalTensor<int32_t> inputGlobal; // The type is int32_t.
inputGlobal.SetGlobalBuffer(reinterpret_cast<__gm__ int32_t *>(src_gm), dataSize); // Set the start address of the source operand in the global memory to src_gm and the size of the external storage occupied by the source operand to 256 int32_t data elements.

AscendC::LocalTensor<int32_t> inputLocal = inQueueX.AllocTensor<int32_t>();    
AscendC::DataCopy(inputLocal, inputGlobal, dataSize); // Copy inputGlobal from the global memory to inputLocal of the local memory.
...
// Assume that inputGlobal is of the int32_t type and contains 16 elements (64 bytes).
// Call ReinterpretCast to reinterpret inputGlobal to the int16_t type.
AscendC::GlobalTensor<int16_t> interpreTensor = inputGlobal.template ReinterpretCast<int16_t>();
// The result of this example is as follows. The data of the two is the same and the same address is used in the physical memory. The data is reinterpreted based on different types.
// inputGlobal:0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// interpreTensor:0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 0