TCOLSUM¶
指令示意图¶
简介¶
通过对行求和来归约每一列。
数学语义¶
设 R = src.GetValidRow(),C = src.GetValidCol()。对 0 <= j < C:
\[ \mathrm{dst}_{0,j} = \sum_{i=0}^{R-1} \mathrm{src}_{i,j} \]
isBinary 选择实现路径(二叉树累加vs. 顺序累加)。
汇编语法¶
同步形式:
%dst = tcolsum %src {isBinary = false} : !pto.tile<...> -> !pto.tile<...>
降阶时可能引入内部临时Tile;C++内建接口需要显式传入 tmp 操作数。
AS Level 1(SSA)¶
%dst = pto.tcolsum %src : !pto.tile<...> -> !pto.tile<...>
%dst = pto.tcolsum %src, %tmp {isBinary = false} : (!pto.tile<...>, !pto.tile<...>) -> !pto.tile<...>
AS Level 2(DPS)¶
pto.tcolsum ins(%src : !pto.tile_buf<...>) outs(%dst : !pto.tile_buf<...>)
pto.tcolsum ins(%src, %tmp {isBinary = false} : !pto.tile_buf<...>, !pto.tile_buf<...>) outs(%dst : !pto.tile_buf<...>)
C++内建接口¶
声明于 include/pto/common/pto_instr.hpp:
公共包含头为
<pto/pto-inst.hpp>,内部声明位于pto/common/pto_instr.hpp。
template <typename TileDataOut, typename TileDataIn, typename... WaitEvents>
PTO_INST RecordEvent TCOLSUM(TileDataOut &dst, TileDataIn &src, WaitEvents &... events);
template <typename TileDataOut, typename TileDataIn, typename TileDataTmp, typename... WaitEvents>
PTO_INST RecordEvent TCOLSUM(TileDataOut &dst, TileDataIn &src, TileDataTmp &tmp, bool isBinary, WaitEvents &... events);
约束¶
通用约束或检查¶
dst和src必须为TileType::Vec。dst和src必须使用标准ND布局:行主且非分形(BLayout::RowMajor、SLayout::NoneBox)。dst和src的元素类型必须一致。- 运行时检查:
src.GetValidCol() == dst.GetValidCol()src.GetValidRow() != 0(为零时实现静默返回,不执行计算)src.GetValidCol() != 0(为零时实现静默返回,不执行计算)src.GetValidCol()必须不大于按src元素计的tmp行跨度(即tmp.RowStride * sizeof(TmpDType) / sizeof(DType) >= src.GetValidCol())
isBinary选择已检查到的后端路径:true:使用tmp做二叉树累加false:直接在dst上做顺序累加
Atlas A2/A3 训练系列产品/Atlas A2/A3 推理系列产品实现检查¶
- 支持的元素类型:
half、float、int16_t、int32_t。 tmp必须为TileType::Vec,且使用标准ND布局:行主且非分形(BLayout::RowMajor、SLayout::NoneBox)。tmp的元素类型必须与src和dst一致。- 若
src.GetValidRow() == 0或src.GetValidCol() == 0,实现会直接返回。
Ascend 950PR/Ascend 950DT实现检查¶
- Ascend 950PR/Ascend 950DT共享列归约检查允许的元素类型为:
half、float、int8_t、uint8_t、int16_t、uint16_t、int32_t、uint32_t、bfloat16_t。 - 已检查到的Ascend 950PR/Ascend 950DT
TCOLSUM路径中,tmp仍只用于二叉累加路径;TCOLSUM_IMPL中没有额外显式加入tmp的编译期类型/布局断言。
临时空间¶
无 tmp(2参数重载:TCOLSUM(dst, src))¶
不需要 tmp。Atlas A2/A3 训练系列产品/Atlas A2/A3 推理系列产品和Ascend 950PR/Ascend 950DT均使用直接在 dst 上的顺序累加。
带 tmp 和 isBinary(4参数重载:TCOLSUM(dst, src, tmp, isBinary))¶
Atlas A2/A3 训练系列产品/Atlas A2/A3 推理系列产品¶
- 当
isBinary = true时:tmp被使用于二叉树累加。src中相邻行对被求和到tmp,然后tmp被递归折半直到仅剩单行。 tmp必须与src/dst具有相同的元素类型。tmp必须是TileType::Vec,行主序,非分形。tmp.GetValidCol() >= src.GetValidCol()(以元素数计,考虑tmp跨度)。tmp至少需要ceil(src.GetValidRow() / 2)行。- 当
isBinary = false时:tmp被接受但实现使用直接在dst上的顺序累加;tmp未被主动使用。
Ascend 950PR/Ascend 950DT¶
- 当
isBinary = true时:tmp被使用于基于向量寄存器的二叉树累加(使用UB存储)。 tmp必须与src/dst具有相同的元素类型。tmp.GetValidCol() >= src.GetValidCol()(以元素数计,考虑tmp跨度)。tmp至少需要ceil(src.GetValidRow() / 2)行。- 当
isBinary = false时:tmp未被主动使用;实现使用通过TColReduceInstr的顺序归约。
示例¶
自动(Auto)¶
#include <pto/pto-inst.hpp>
using namespace pto;
void example_auto() {
using SrcT = Tile<TileType::Vec, float, 16, 16>;
using DstT = Tile<TileType::Vec, float, 1, 16>;
using TmpT = Tile<TileType::Vec, float, 16, 16>;
SrcT src;
DstT dst;
TmpT tmp;
TCOLSUM(dst, src, tmp, /*isBinary=*/false);
}
手动(Manual)¶
#include <pto/pto-inst.hpp>
using namespace pto;
void example_manual() {
using SrcT = Tile<TileType::Vec, float, 16, 16>;
using DstT = Tile<TileType::Vec, float, 1, 16>;
using TmpT = Tile<TileType::Vec, float, 16, 16>;
SrcT src;
DstT dst;
TmpT tmp;
TASSIGN(src, 0x1000);
TASSIGN(dst, 0x2000);
TASSIGN(tmp, 0x3000);
TCOLSUM(dst, src, tmp, /*isBinary=*/false);
}
汇编示例(ASM)¶
自动模式¶
# 自动模式:由编译器/运行时负责资源放置与调度。
%dst = pto.tcolsum %src : !pto.tile<...> -> !pto.tile<...>
手动模式¶
# 手动模式:先显式绑定资源,再发射指令。
# 可选(当该指令包含 tile 操作数时):
# pto.tassign %arg0, @tile(0x1000)
# pto.tassign %arg1, @tile(0x2000)
%dst = pto.tcolsum %src : !pto.tile<...> -> !pto.tile<...>
PTO汇编形式¶
%dst = tcolsum %src {isBinary = false} : !pto.tile<...> -> !pto.tile<...>
# AS Level 2 (DPS)
pto.tcolsum ins(%src : !pto.tile_buf<...>) outs(%dst : !pto.tile_buf<...>)