Alloc

Product Support

Product

Supported

Atlas A3 training products/Atlas A3 inference products

Atlas A2 training products/Atlas A2 inference products

Atlas 200I/500 A2 inference products

x

Atlas inference product's AI Core

Atlas inference product's Vector Core

Atlas training products

x

Function

Returns the corresponding LocalTensor object based on the specified logical position, data type, and data length.

Prototype

  • Prototype 1: tileSize is a template parameter.
    1
    2
    3
    // When tileSize is a constant, you are advised to use this API to obtain better performance.
    template <class DataType, uint32_t tileSize> LocalTensor<DataType> __aicore__ inline Alloc()
    template <TPosition pos, class DataType, uint32_t tileSize> __aicore__ inline LocalTensor<DataType> Alloc()
    
  • Prototype 2: tileSize is an API input parameter.
    1
    2
    3
    // This API is used when tileSize is a dynamic parameter.
    template <class DataType> LocalTensor<DataType> __aicore__ inline Alloc(uint32_t tileSize)
    template <TPosition pos, class DataType> LocalTensor<DataType> __aicore__ inline Alloc(uint32_t tileSize)
    
  • Prototype 3: This API is used when TensorTrait is used.
    1
    template <class TensorTraitType> LocalTensor<TensorTraitType> __aicore__ inline Alloc()
    

Parameters

Table 1 Template parameters of prototype 1 and prototype 2

Parameter

Description

pos

TPosition position. The value must comply with the hardware physical position specified in LocalMemAllocator. (In static tensor programming scenarios, this parameter can be omitted.)

DataType

Data type of LocalTensor. Only basic data types are supported. The TensorTrait type is not supported.

tileSize

Number of elements in the LocalTensor object. The value cannot exceed the remaining memory space at the current physical position.

Table 2 Parameters of prototype 2

Parameter

Input/Output

Description

tileSize

Input

Number of elements in the LocalTensor object. The value cannot exceed the remaining memory space at the current physical position.

The remaining memory space can be calculated by subtracting the current available memory address (returned by GetCurAddr) from the maximum physical memory size.

Table 3 Template parameters of prototype 3

Parameter

Description

TensorTraitType

Only the TensorTrait type is supported. The data type, logical location, and shape size of TensorTrait must match the physical location and remaining space specified in LocalMemAllocator.

Returns

LocalTensor object constructed based on the user input.

Restrictions

None

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
template <uint32_t v>
using UIntImm = Std::integral_constant<uint32_t, v>;
...
AscendC::LocalMemAllocator allocator;
// Prototype 1: float type. The tensor contains 1024 elements. You can specify the logical location (or do not specify the logical location. The Alloc function provides the default value based on the physical location.).
auto tensor1 = allocator.Alloc<AscendC::TPosition::VECIN, float, 1024>();
auto tensor1 = allocator.Alloc<float, 1024>();

// Prototype 2: float type. The number of elements in the tensor is the value of tileLength. You can specify the logical location (or do not specify the logical location. The Alloc function provides the default value based on the physical location.).
auto tensor1 = allocator.Alloc<AscendC::TPosition::VECIN, float>(tileLength);

// Prototype 3: The logical location is VECIN, the data type is float, and the tensor contains 16 × 16 × 16 elements.
auto shape = AscendC::MakeShape(UIntImm<16>{}, UIntImm<16>{}, UIntImm<16>{});
auto stride = AscendC::MakeStride(UIntImm<0>{}, UIntImm<0>{}, UIntImm<0>{});
auto layoutMake = AscendC::MakeLayout(shape, stride);
auto tensorTraitMake = AscendC::MakeTensorTrait<float, AscendC::TPosition::VECIN>(layoutMake);
auto tensor3 = allocator.Alloc<decltype(tensorTraitMake)>();