reduce_tile

template<PoolType reduce_type, ReduceDim reduce_dim, bool is_fp32_dest_acc_en = DST_ACCUM_MODE>
void ckernel::reduce_init(std::uint32_t icb, std::uint32_t icb_scaler, std::uint32_t ocb, std::uint32_t call_line = __builtin_LINE())

Performs the necessary hardware and software initialization for reduce operation for provided circular buffer identifiers (CB IDs). In order for reduce operation to be performed, this function call must be followed by a call to reduce_tile or reduce_tile_math. If another reduce op is needed which uses different CB IDs, then another reduce_init needs to be called as a part of that reduce operation.

The icb_scaler circular buffer must contain the scaling factors for the reduction. The most straightforward way of filling the icb_scaler with the scaling factors is to populate first row of each face with the following values:

  • If reduce_type = SUM, all scaling factors should preferably be 1.

  • If reduce_type = AVG, all scaling factors should preferably be 1/N (where N is the number of elements being averaged, except if the reduction dimension is scalar, in which case the scaling factor should be 1/sqrt(N)).

  • If reduce_type = MAX, all scaling factors should preferably be 1.

NOTE: For SUM and AVG operations, the value in icb_scaler is a scaling factor of the final sum of values across rows/columns/both, so there is no real constraint in terms of it’s value. For MAX operation, maximum value will be obtained as expected, but it will be scaled by the values in icb_scaler. In any case, it is recommended to use the above-mentioned scaling factors to ensure that operations function as intended. Refer to ISA documentation for more details. NOTE: For other valid ways of populating the icb_scaler, refer to the ISA documentation.

Output tile layout (packer-zeroing contract): for all three values of reduce_dim, reduce_init programs the packer’s edge masks (_llk_pack_reduce_mask_config_<reduce_dim, …>) so that any output datum that is not part of the reduction result is written to CB by the packer as zero. Specifically, for any tile packed into ocb while this reduce_init’s packer state is in effect:

  • REDUCE_SCALAR: the scalar result is at face-0 [0, 0]; every other datum in the tile is zero.

  • REDUCE_ROW: each row’s reduced value is at column 0 of that row; every other datum in the tile is zero.

  • REDUCE_COL: each column’s reduced value is at row 0 of that column; every other datum in the tile is zero. A reset to the default packer mask happens via reduce_uninit (or by the next non-reduce init); until then this contract holds for every pack into ocb.

Return value: None

Param Type Name

Template reduce_type

Template reduce_dim

Function icb

Function icb_scaler

Function ocb

template<PoolType reduce_type, ReduceDim reduce_dim, bool is_fp32_dest_acc_en = DST_ACCUM_MODE>
void ckernel::reduce_tile(std::uint32_t icb, std::uint32_t icb_scaler, std::uint32_t itile, std::uint32_t itile_scaler, std::uint32_t idst)

Performs a reduction operation B = reduce(A) using reduce_func for dimension reduction on a tile in the CB at a given index and writes the result to the DST register at index dst_tile_index. Reduction can be of type Reduce::R, Reduce::C, or Reduce::RC, identifying the dimension(s) to be reduced in size to 1. The DST register buffer must be in acquired state via acquire_dst call.

The icb_scaler circular buffer must contain the scaling factors for the reduction. The most straightforward way of filling the icb_scaler with the scaling factors is to populate first row of each face with the following values:

  • If reduce_type = SUM, all scaling factors should preferably be 1.

  • If reduce_type = AVG, all scaling factors should preferably be 1/N (where N is the number of elements being averaged, except if the reduction dimension is scalar, in which case the scaling factor should be 1/sqrt(N)).

  • If reduce_type = MAX, all scaling factors should preferably be 1.

The templates take reduce_type which can be ReduceFunc::Sum, ReduceFunc::Avg, or ReduceFunc::Max and reduce_dim which can be Reduce::R, Reduce::C, or Reduce::RC. They can also be specified by defines REDUCE_OP and REDUCE_DIM.

NOTE: Before the next operation is initialized, the reduce_uninit function must be called to reset the packer state to default. NOTE: For SUM and AVG operations, the value in icb_scaler is a scaling factor of the final sum of values across rows/columns/both, so there is no real constraint in terms of it’s value. For MAX operation, maximum value will be obtained as expected, but it will be scaled by the values in icb_scaler. In any case, it is recommended to use the above-mentioned scaling factors to ensure that operations function as intended. Refer to ISA documentation for more details. NOTE: For other valid ways of populating the icb_scaler, refer to the ISA documentation. Return value: None

Param Type Name

Template reduce_type

Template reduce_dim

Function icb

Function icb_scaler

Function itile

Function itile_scaler

Function idst

template<PoolType reduce_type, ReduceDim reduce_dim, bool is_fp32_dest_acc_en = DST_ACCUM_MODE>
void ckernel::reduce_block(std::uint32_t icb, std::uint32_t icb_scaler, std::uint32_t start_itile, std::uint32_t itile_scaler, std::uint32_t start_idst, std::uint32_t ntiles)

Performs a reduction operation B = reduce(A) on ntiles consecutive tiles from the input CB, writing each partial result to a consecutive DST register slot. This is the uniform block entry point for the reduce op group: its body is a simple loop over reduce_tile, so it inherits reduce_tile’s semantics and requires the same initialization (reduce_init) to have been called first. The scaling-factor tile (itile_scaler) is reused for every tile in the block. The DST register buffer must be in acquired state via acquire_dst call.

NOTE: The loop implementation is transitional. In the future this for-loop must be folded into a hardware MOP / REPLAY buffer (as is being done for Quasar) so the whole block issues as a single packed op; the blocking then lives in llk-lib without changing this signature. Tracked under the Compute API Split effort (tt-metal#35739); the per-op push-down lands in tt-metal#47478. NOTE: Before the next operation is initialized, the reduce_uninit function must be called to reset the packer state to default.

Return value: None

Param Type Name

Template reduce_type

Template reduce_dim

Function icb

Function icb_scaler

Function start_itile

Function itile_scaler

Function start_idst

Function ntiles