文档
注册

GlobalTensor

功能说明

GlobalTensor用来存放Global Memory(外部存储)的全局数据。

定义原型

template <typename T> class GlobalTensor {
    T GetValue(const uint64_t offset) const;                     // 获取Tensor的相应偏移位置的值
    void SetValue(const uint64_t offset, T value);               // 设置Tensor相应偏移位置的值。
    void SetGlobalBuffer(__gm__ T* buffer, uint32_t bufferSize); // 传入全局数据的指针,并手动设置一个buffer size,初始化GlobalTensor
    const __gm__ T* GetPhyAddr();                                // 返回全局数据的地址
    uint64_t GetSize();                                          // 返回Tensor中的element个数
    GlobalTensor operator[](const uint64_t offset);              // 指定偏移返回一个GlobalTensor,offset单位为element
    T& operator()(const uint32_t index) const;                   // 返回某个index位置的元素的引用
    
}

函数说明

类型T支持所有数据类型,但需要遵循使用此GlobalTensor的指令的数据类型支持情况。

表1 函数说明

函数名称

入参说明

含义

GetValue

offset:偏移量,单位为 element

获取 GlobalTensor 中的某个值,返回 T 类型的立即数。

SetValue

offset:偏移量,单位为 element

value: 设置值,单位为任意类型

设置 GlobalTensor 中的某个值。

SetGlobalBuffer

buffer:主机侧传入的全局数据指针

bufferSize:所包含的类型为T的数据个数,单位为 element,需自行保证不会超出实际数据的长度

设置GlobalTensor的存储位置:buffer指向外部存储的起始地址,bufferSize为Tensor所占外部存储的大小,如指向的外部存储有连续256个int32_t,则其dataSize为256。

GetPhyAddr

-

返回GlobalTensor的地址

GetSize

-

返回GlobalTensor的element个数

operator[]

offset:用户指定的偏移位置

根据输入的offset偏移返回新的Tensor,offset的单位为element的个数

operator()

index: 下标索引

获取本GlobalTensor的第index个变量的引用。与LocalTensor的operator()LocalTensor类似。

该功能仅在Atlas A2训练系列产品/Atlas 800I A2推理产品产品型号支持。

调用示例

void Init(__gm__ uint8_t *src_gm, __gm__ uint8_t *dst_gm)
{
    uint32_t dataSize = 256; //设置input_global的大小为256

    GlobalTensor<int32_t> inputGlobal; // 类型为int32_t
    inputGlobal.SetGlobalBuffer(reinterpret_cast<__gm__ int32_t *>(src_gm), dataSize); // 设置源操作数在Global Memmory上的起始地址为src_gm,所占外部存储的大小为256个int32_t

    LocalTensor<int32_t> inputLocal = inQueueX.AllocTensor<int32_t>();    
    DataCopy(inputLocal, inputGlobal, dataSize); // 将Global Memmory上的inputGlobal拷贝到Local Memmory的inputLocal上
    ...
}
搜索结果
找到“0”个结果

当前产品无相关内容

未找到相关内容,请尝试其他搜索词