TROWARGMAX

Tile Operation Diagram

TROWARGMAX tile operation

Introduction

Get the column index of the maximum element for each row.

Math Interpretation

Let R = src.GetValidRow() and C = src.GetValidCol(). For 0 <= i < R:

\[ \mathrm{dst}_{i,0} = \max_{0 \le j < C} j_{i} \]

Assembly Syntax

PTO-AS form: see docs/grammar/PTO-AS.md.

Synchronous form:

%dst = trowargmax %src : !pto.tile<...> -> !pto.tile<...>

Lowering may introduce internal scratch tiles; the C++ intrinsic requires an explicit tmp operand.

IR Level 1 (SSA)

%dst = pto.trowargmax %src, %tmp : (!pto.tile<...>, !pto.tile<...>) -> !pto.tile<...>

IR Level 2 (DPS)

pto.trowargmax ins(%src, %tmp : !pto.tile_buf<...>, !pto.tile_buf<...>) outs(%dst : !pto.tile_buf<...>)

C++ Intrinsic

Declared in include/pto/common/pto_instr.hpp:

template <typename TileDataOut, typename TileDataIn, typename TileDataTmp, typename... WaitEvents>
PTO_INST RecordEvent TROWARGMAX(TileDataOut& dst, TileDataIn& src, TileDataTmp& tmp, WaitEvents&... events);

Constraints

Implementation checks (NPU):

  • A2A3:
  • Tile location: dst and src must be TileType::Vec.
  • Tile layout of src: ND fractal (isRowMajor and SLayout::NoneBox).
  • Tile layout of dst:
    • Compact Mode: DN layout Tile of 1D, e.g., Tile<TileType::Vec, T, ROWS, 1, BLayout::ColMajor, ValidRows, 1>, ROWS must be 32b aligned.
    • Traditional Mode: ND layout Tile of 2D, e.g., Tile<TileType::Vec, T, ROWS, COLS, BLayout::RowMajor, ValidRows, 1>.
  • Source data types: half or float.
  • Destination data types: uint32_t or int32_t.
  • Runtime valid checks:
    • srcValidCol != 0 and srcValidRow != 0.
  • A5:
  • Source data types: half or float.
  • Destination data types: uint32_t or int32_t.
  • No explicit runtime assertions on validRow/validCol in the implementation; the loops use src.GetValidRow() and src.GetValidCol().
  • tmp temporary tile is not used, only for compatibility use.

About temporary tile tmp for A3

  • Temporary tile is not used when srcValidCol <= ElementPerRepeat, used when srcValidCol > ElementPerRepeat.
  • tmp tile's rows is the same as src.
  • Simply set tmp tile size the same as src when src is small.
  • tmp tile's stride can be calculated out based on src's validCol using the following formula:
repeats = ceil(validCol / elementPerRepeat)
stride = ceil(repeats * 2 / elementPerBlock) * elementPerBlock + ceil(repeats / elementPerBlock) * elementPerBlock

Examples

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, uint32_t, 16, 1, BLayout::ColMajor>;
  using TmpT = Tile<TileType::Vec, float, 16, 16>;
  SrcT src;
  DstT dst;
  TmpT tmp;
  TROWARGMAX(dst, src, tmp);
}

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, uint32_t, 16, 1, BLayout::ColMajor>;
  using TmpT = Tile<TileType::Vec, float, 16, 16>;
  SrcT src;
  DstT dst;
  TmpT tmp;
  TASSIGN(src, 0x1000);
  TASSIGN(dst, 0x2000);
  TASSIGN(tmp, 0x3000);
  TROWARGMAX(dst, src, tmp);
}

ASM Form Examples

Auto Mode

# Auto mode: compiler/runtime-managed placement and scheduling.
%dst = pto.trowmax %src, %tmp : (!pto.tile<...>, !pto.tile<...>) -> !pto.tile<...>

Manual Mode

# Manual mode: bind resources explicitly before issuing the instruction.
# Optional for tile operands:
# pto.tassign %arg0, @tile(0x1000)
# pto.tassign %arg1, @tile(0x2000)
%dst = pto.trowmax %src, %tmp : (!pto.tile<...>, !pto.tile<...>) -> !pto.tile<...>

PTO Assembly Form

%dst = trowmax %src : !pto.tile<...> -> !pto.tile<...>
# IR Level 2 (DPS)
pto.trowmax ins(%src, %tmp : !pto.tile_buf<...>, !pto.tile_buf<...>) outs(%dst : !pto.tile_buf<...>)