// SPDX-License-Identifier: Apache-2.0 // K12 rerow (ttaw 0.24.0, ops/rerow.py): a ROW_MAJOR DRAM-interleaved reshape that only changes the page width, // [Rin, Win] -> [Rout, Wout] with Rin * Win == Rout * Wout and one page width a whole multiple M of the other, as a // plain page copy at DRAM bandwidth (ttnn's RM reshape kernel: ~60 GB/s at METEOR's [864, 30720] -> [165888, 160]). // The bytes of the logical row-major stream do not move; only their page (and so their DRAM bank) does. // // Terms: the "big" side has pages of M * PS bytes, the "small" side pages of PS bytes (PS % 64 == 0: every DRAM // address and L1 offset below is 64 B aligned). A chunk = up to K consecutive small pages inside ONE big page // (CPB = ceil(M / K) chunks per big page): one transfer of n * PS bytes at offset j0 * PS of big page b on the big // side, n transfers of single pages b * M + j0 + t on the small side. DIR 0: big input -> small output (split), // DIR 1: small input -> big output (join). // One RISC / NOC does all reads and writes of a core (no two-NOC small DRAM writes: Blackhole SYS-1419). Two L1 // slots: the reads of chunk q + 1 are in flight while chunk q is written. // Work split (no per-core RT args, probe P3): core i = y * GX + x of the first NC cores owns chunks // [i * B + min(i, E), + B + (i < E)), B = NCH / NC, E = NCH % NC. // CT args: [0] NCH, [1] NC, [2] GX, [3] M, [4] K, [5] PS, [6] DIR, [7] CPB, [8] SLOT (bytes per slot, >= K * PS), // then the TensorAccessorArgs of the input and of the output. Common RT args: [in_addr, out_addr]. // CB 0: 2 * SLOT bytes, reserved never; used as plain L1 scratch through get_write_ptr. #include #include "api/dataflow/dataflow_api.h" constexpr uint32_t NCH = get_compile_time_arg_val(0); constexpr uint32_t NC = get_compile_time_arg_val(1); constexpr uint32_t GX = get_compile_time_arg_val(2); constexpr uint32_t M = get_compile_time_arg_val(3); constexpr uint32_t K = get_compile_time_arg_val(4); constexpr uint32_t PS = get_compile_time_arg_val(5); constexpr uint32_t DIR = get_compile_time_arg_val(6); constexpr uint32_t CPB = get_compile_time_arg_val(7); constexpr uint32_t SLOT = get_compile_time_arg_val(8); constexpr auto in_args = TensorAccessorArgs<9>(); constexpr auto out_args = TensorAccessorArgs(); constexpr uint32_t PB = M * PS; // big page bytes constexpr uint32_t IN_PAGE = DIR == 0 ? PB : PS; constexpr uint32_t OUT_PAGE = DIR == 0 ? PS : PB; template FORCE_INLINE void big_read(const A& src, uint32_t b, uint32_t j0, uint32_t n, uint32_t buf) { noc_async_read(src.get_noc_addr(b, j0 * PS), buf, n * PS); } void kernel_main() { const uint32_t core = get_absolute_logical_y() * GX + get_absolute_logical_x(); if (core >= NC) { return; } constexpr uint32_t B = NCH / NC, E = NCH % NC; const uint32_t q0 = core * B + (core < E ? core : E); const uint32_t nq = B + (core < E ? 1 : 0); if (nq == 0) { return; } const auto in = TensorAccessor(in_args, get_common_arg_val(0), IN_PAGE); const auto out = TensorAccessor(out_args, get_common_arg_val(1), OUT_PAGE); const uint32_t buf0 = get_write_ptr(0); auto issue_reads = [&](uint32_t q, uint32_t buf) { const uint32_t b = q / CPB; const uint32_t j0 = (q - b * CPB) * K; const uint32_t n = (M - j0 < K) ? M - j0 : K; if constexpr (DIR == 0) { big_read(in, b, j0, n, buf); } else { const uint32_t p0 = b * M + j0; for (uint32_t t = 0; t < n; ++t) { noc_async_read(in.get_noc_addr(p0 + t), buf + t * PS, PS); } } }; auto issue_writes = [&](uint32_t q, uint32_t buf) { const uint32_t b = q / CPB; const uint32_t j0 = (q - b * CPB) * K; const uint32_t n = (M - j0 < K) ? M - j0 : K; if constexpr (DIR == 0) { const uint32_t p0 = b * M + j0; for (uint32_t t = 0; t < n; ++t) { noc_async_write(buf + t * PS, out.get_noc_addr(p0 + t), PS); } } else { noc_async_write(buf, out.get_noc_addr(b, j0 * PS), n * PS); } }; issue_reads(q0, buf0); for (uint32_t i = 0; i < nq; ++i) { const uint32_t buf = buf0 + (i & 1) * SLOT; noc_async_read_barrier(); // chunk i is in its slot if (i + 1 < nq) { // the other slot held chunk i - 1, whose writes were flushed at the end of the previous iteration issue_reads(q0 + i + 1, buf0 + ((i + 1) & 1) * SLOT); } issue_writes(q0 + i, buf); noc_async_writes_flushed(); // chunk i has left L1: its slot may be refilled } noc_async_write_barrier(); }