Skip to content

Alloc

产品支持情况​

  • Ascend 950PR&950DT系列产品:支持
  • Atlas A3系列产品:支持
  • Atlas A2系列产品:支持
  • Atlas 200I/500 A2推理产品:不支持
  • Atlas推理系列产品AI Core:支持
  • Atlas推理系列产品Vector Core:支持
  • Atlas训练系列产品:不支持

功能说明​

头文件路径为:"basic_api/kernel_tensor.h"。

根据用户指定的逻辑位置、数据类型、数据长度返回对应的LocalTensor对象。

函数原型​

  • 原型1:tileSize为模板参数

    C++
    // 当tileSize为常量时,建议使用此接口,以获得更优的性能
    template <class DataType, uint32_t tileSize> LocalTensor<DataType> __aicore__ inline Alloc()
    template <TPosition pos, class DataType, uint32_t tileSize> __aicore__ inline LocalTensor<DataType> Alloc()
    
  • 原型2:tileSize为接口入参

    C++
    // 当tileSize为动态参数时使用此接口
    template <class DataType> LocalTensor<DataType> __aicore__ inline Alloc(uint32_t tileSize)
    template <TPosition pos, class DataType> LocalTensor<DataType> __aicore__ inline Alloc(uint32_t tileSize)
    
  • 原型3:使用TensorTrait时使用此接口

    C++
    template <class DataType> LocalTensor<DataType> __aicore__ inline Alloc()
    

参数说明​

表1 原型1和原型2模板参数说明

参数名

描述

pos

TPosition位置,需要符合LocalMemAllocator中指定的Hardware物理位置(C++ Tensor编程场景下,此参数可以省略)。

DataType

LocalTensor的数据类型,只支持基础数据类型,不支持TensorTrait类型。

tileSize

LocalTensor的元素个数,必须大于0,所占字节数须为32的整数倍,且不应超过当前物理位置剩余的内存空间。对于half、float等按整字节存储的数据类型,须满足(tileSize * sizeof(DataType)) % 32 == 0。

表2 原型2参数说明

参数名

输入/输出

描述

tileSize

输入

LocalTensor的元素个数,必须大于0,所占字节数须为32的整数倍,且不应超过当前物理位置剩余的内存空间。对于half、float等按整字节存储的数据类型,须满足(tileSize * sizeof(DataType)) % 32 == 0。

剩余的内存空间可以通过物理内存最大值与当前可用内存地址(GetCurAddr返回值)的差值来计算。

表3 原型3模板参数说明

参数名

描述

TensorTraitType

只支持传入TensorTrait类型,TensorTrait的数据类型/逻辑位置/Shape大小需要匹配LocalMemAllocator中指定的物理位置及其剩余空间。

返回值说明​

根据用户输入构造的LocalTensor对象。

约束说明​

  • LocalTensor所占字节数须为32的整数倍,Alloc不会自动将tileSize向上补齐。该要求同时适用于tileSize为模板参数和接口入参的原型;使用TensorTrait时,由其布局确定的元素个数与元素类型换算得到的字节数也须满足此要求。
  • 分配长度不满足对齐要求时,构造LocalTensor的内部校验会在CPU调试模式下触发断言;相关构造约束请参见LocalTensor构造函数。
  • 针对Ascend 950PR&950DT系列产品,对Unified Buffer(UB)的内存分配,会基于静态内存的基础上进行分配,即动态内存初始位置,参考内存层级。
  • 当前暂不支持与L1 Buffer, L0A Buffer, L0B Buffer, L0C Buffer, BiasTable静态数组混用。

调用示例​

例如,需要容纳350个有效元素时,half和float类型均可将tileSize向上补齐为352,分别占用704字节和1408字节,均为32字节的整数倍;350个元素对应的700字节和1400字节均不满足要求。half类型的tileSize应为16的整数倍,float类型应为8的整数倍。补齐后的分配容量须在剩余内存范围内;实际搬运和计算仍应按有效数据量及所用API的约束处理,避免访问Global Memory中的无效数据或将未初始化的补齐区域参与计算。

C++
template <uint32_t v>
using UIntImm = Std::integral_constant<uint32_t, v>;
...
AscendC::LocalMemAllocator allocator;
// 原型1:float类型,Tensor中有1024个元素,用户可以指定逻辑位置(或者不指定,由Alloc函数根据物理位置给出默认值,不影响功能)
auto tensor1 = allocator.Alloc<AscendC::TPosition::VECIN, float, 1024>();
auto tensor1 = allocator.Alloc<float, 1024>();

// 原型2:float类型,Tensor中有tileLength个元素,用户可以指定逻辑位置(或者不指定,由Alloc函数根据物理位置给出默认值,不影响功能)
auto tensor1 = allocator.Alloc<AscendC::TPosition::VECIN, float>(tileLength);

// 原型3:用户指定逻辑位置VECIN,数据类型为float,Tensor中元素个数为16*16*16
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)>();

免责声明:本站内容由 asc-devkit 仓 master 分支自动编译生成,属于持续开发版本,可能存在缺陷,仅供预览与参考。如需稳定及商用资料,请查阅官方 昇腾社区。