TREMS¶
指令示意图¶
简介¶
与标量的逐元素余数:remainder(src, scalar)。
数学语义¶
对每个元素 (i, j) 在有效区域内:
\[\mathrm{dst}_{i,j} = \mathrm{src}_{i,j} \bmod \mathrm{scalar}\]
汇编语法¶
同步形式:
%dst = trems %src, %scalar : !pto.tile<...>, f32
AS Level 1(SSA)¶
%dst = pto.trems %src, %scalar : (!pto.tile<...>, dtype) -> !pto.tile<...>
AS Level 2(DPS)¶
pto.trems ins(%src, %scalar : !pto.tile_buf<...>, dtype) outs(%dst : !pto.tile_buf<...>)
C++内建接口¶
声明于 include/pto/common/pto_instr.hpp:
公共包含头为
<pto/pto-inst.hpp>,内部声明位于pto/common/pto_instr.hpp。
template <auto PrecisionType = RemSAlgorithm::DEFAULT, typename TileDataDst, typename TileDataSrc, typename TileDataTmp,
typename... WaitEvents>
PTO_INST RecordEvent TREMS(TileDataDst &dst, TileDataSrc &src, typename TileDataSrc::DType scalar, TileDataTmp &tmp,
WaitEvents &...events);
PrecisionType可指定以下值:
RemSAlgorithm::DEFAULT:普通算法,速度快但精度较低。RemSAlgorithm::HIGH_PRECISION:高精度算法,速度较慢,仅支持float类型。
约束¶
- 实现检查 (Atlas A2/A3 训练系列产品/Atlas A2/A3 推理系列产品):
dst和src必须使用相同的元素类型。- 支持的元素类型:
float和int32_t。 dst和src必须是向量Tile。dst和src必须是行主序。- 运行时:
dst.GetValidRow() == src.GetValidRow() > 0且dst.GetValidCol() == src.GetValidCol() > 0。 - tmp缓冲区要求:
tmp.GetValidCol() >= dst.GetValidCol()(至少与dst相同的列数)tmp.GetValidRow() >= 1(至少1行)- 数据类型必须与
TileDataDst::DType匹配。
- 实现检查 (Ascend 950PR/Ascend 950DT):
dst和src必须使用相同的元素类型。- 支持的元素类型:
float、int32_t、uint32_t、half、int16_t和uint16_t。 dst和src必须是向量Tile。- 两个Tile的静态有效边界都必须满足
ValidRow <= Rows且ValidCol <= Cols。 - 运行时:
dst.GetValidRow() == src.GetValidRow()且dst.GetValidCol() == src.GetValidCol()。 - 注意:tmp参数在Ascend 950PR/Ascend 950DT上被接受但不进行验证或使用。
- 除零:
- 行为由目标定义;CPU模拟器在调试构建中会断言。
- 有效区域:
- 该操作使用
dst.GetValidRow()/dst.GetValidCol()作为迭代域。
- 该操作使用
- 对于
int32_t输入(仅Atlas A2/A3 训练系列产品/Atlas A2/A3 推理系列产品):src的元素和scalar必须在[-2^24, 2^24]范围内(即[-16777216, 16777216]),以确保在计算过程中能精确转换为float32。 - 高精度算法
- 仅在Ascend 950PR/Ascend 950DT上有效,
PrecisionType选项在Atlas A3 训练系列产品/Atlas A3 推理系列产品上将被忽略。
- 仅在Ascend 950PR/Ascend 950DT上有效,
示例¶
#include <pto/pto-inst.hpp>
using namespace pto;
void example() {
using TileT = Tile<TileType::Vec, float, 16, 16>;
TileT x, out;
Tile<TileType::Vec, float, 16, 16> tmp;
TREMS(out, x, 3.0f, tmp);
}
汇编示例(ASM)¶
自动模式¶
# 自动模式:由编译器/运行时负责资源放置与调度。
%dst = pto.trems %src, %scalar : (!pto.tile<...>, dtype) -> !pto.tile<...>
手动模式¶
# 手动模式:先显式绑定资源,再发射指令。
# 可选(当该指令包含 tile 操作数时):
# pto.tassign %arg0, @tile(0x1000)
# pto.tassign %arg1, @tile(0x2000)
%dst = pto.trems %src, %scalar : (!pto.tile<...>, dtype) -> !pto.tile<...>
PTO汇编形式¶
%dst = trems %src, %scalar : !pto.tile<...>, f32
# AS Level 2 (DPS)
pto.trems ins(%src, %scalar : !pto.tile_buf<...>, dtype) outs(%dst : !pto.tile_buf<...>)