文档
注册

Get

功能说明

从TBuf上获取指定长度的Tensor,或者获取全部长度的Tensor。

函数原型

LocalTensor<T> Get<T>();
LocalTensor<T> Get<T>(uint32_t len);

参数说明

表1 参数说明

参数名称

输入/输出

含义

len

输入

需要获取的Tensor元素个数

支持的型号

Atlas 训练系列产品

Atlas推理系列产品(Ascend 310P处理器)AI Core

Atlas推理系列产品(Ascend 310P处理器)Vector Core

Atlas A2训练系列产品/Atlas 800I A2推理产品

Atlas 200/500 A2推理产品

注意事项

len的数值是Tensor中元素的个数,len*sizeof(T)不能超过TBuf初始化时的长度。

返回值

调用示例

// 为TBuf初始化分配内存,分配内存长度为1024字节
TPipe pipe;
TBuf<TPosition::VECCALC> calcBuf; // 模板参数为TPosition中的VECCALC类型
uint32_t byteLen = 1024;
pipe.InitBuffer(calcBuf, byteLen);

// 从calcBuf获取Tensor,Tensor为pipe分配的所有内存大小,为1024字节
LocalTensor<int32_t> tempTensor1 = calcBuf.Get<int32_t>();
// 从calcBuf获取Tensor,Tensor为128个int32_t类型元素的内存大小,为512字节
LocalTensor<int32_t> tempTensor1 = calcBuf.Get<int32_t>(128);

/* 在相对复杂计算场景,可以使用TBuf作为临时变量,存储中间计算结果,避免复杂的出队,入队过程。
 * 下面代码来源于某种距离计算的API中,C矩阵需要除以A矩阵和B矩阵的点乘结果。在该算法中所有矩阵均提前转换成向量。
 */
auto normADotB = calcBuf.Get<int32_t>(); // 存储A矩阵和B矩阵点乘后的结果
auto normB = qidVecIn.AllocTensor<DTypeOut>();
...
normB= qidVecIn.DeQue<DTypeOut>(); // 获取B矩阵
for(int i = 0; i < tiling.baseM; i++) {
   AscendC::Muls(normADotB[i * tiling.baseN], normB, normA.GetValue(i), tiling.baseN); // A矩阵和B矩阵均转换为向量后做数乘,normADotB作为临时变量存储结果。
}
qidVecIn.FreeTensor(normB);
...
for(int i = 0; i < tiling.baseM; i++) {
   AscendC::Mul(baseCVecFloat[i * tiling.baseN], baseCVecFloat[i * tiling.baseN], normADotB[i * tiling.baseN], tiling.baseN); // 通过计算获取C矩阵,并除以normADotB
}
搜索结果
找到“0”个结果

当前产品无相关内容

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