Skip to content

L2: the banked scratchpad

The systolic tile’s L2 (memory/scratchpad.py) is the first parameterization of the KPU tile’s L2 model (kpugen’s page; #17 here, a port of esg#24). It replaced an idealized scratchpad whose every bank had two read ports, which real SRAM macros don’t have.

The Streamer (controller/execute.py) reads two L2 rows per clock on two read ports, and the BlockMover’s LOAD writes L2 on the write port:

Dataflow Port A (stream) Port B
OS A tile, row-major: R rows, one per clock B tile, column-major: C rows, in the same clocks
WS the streamed tile X (Matmul: the A slot), column-major PRELOAD’s weight columns W (the B slot), beside the previous stream
IS the streamed tile (Matmul: the B slot) PRELOAD’s stationary Aᵀ (the A slot)

Each port reads consecutive rows from its tile’s base, one per clock, for max(K, R, C) (OS) or max(M, R) clocks (stationary). With K >= R, C (or M >= max(R, C)) the next tile’s reads follow with no gap. For a 64×64 int8 array that’s 128 B per clock.

  • Two bank groups, by address. L2’s rows are linear in the local address. The lower half (0 .. l2_group_rows - 1) is the A group and the upper half the B group. SystolicConfig.l2_group_rows is where the B group starts (kpugen’s tile also reads it out as the CSR L2_B_BASE).
  • Banks. Each group has N = l2_group_banks banks: row r of a group is in bank r mod N at bank row r div N. N needn’t be a power of two (the decode is then a constant divider). The banks are 1R1W macros, pipelined, with latency spad_read_delay, and their writes are byte-masked.
  • A crossbar gives each group’s read port to the port that addresses it (port A first). Each bank’s write port takes the BlockMover’s writes in any clock.
port A ──┐ ┌── A group: bank 0 … bank N-1 (rows 0 .. G-1)
├─×─┤
port B ──┘ └── B group: bank 0 … bank N-1 (rows G .. 2G-1)
write ─────────► the bank of the row, in either group

Placement. The ISA (isa.command.TileBuffers) puts A slots in the A group and B slots in the B group. In every dataflow the two reads of a clock then land in different groups. In IS, PRELOAD reads the A slot on port B while COMPUTE streams the B slot on port A, still one read per group. A host that issues commands itself places its tiles the same way, by operand, in every dataflow: A tiles below l2_group_rows, B tiles at or above it. What matters is that the two tiles read in the same clock sit in different groups: the A and B tiles of an OS COMPUTE, and the streamed tile and the next PRELOAD’s tile in the stationary mode. The cocotb systolic_top bench places its B tiles at B_BASE, the B group’s first row. With l2_bank_occupancy > 1, tile bases are also multiples of dim (below).

An L2 access the macros can’t take sets the sticky l2_error (SystolicTop.l2_error, STATUS bit 9):

  • both reads of a clock in one group: port A gets its row, port B’s data is wrong;
  • an access to a bank still busy from its last one, when l2_bank_occupancy > 1;
  • a row past L2.

A schedule whose tiles sit in their own groups never raises it. The unit tests check it every clock over the GEMMs of every dataflow. It is cheap enough to keep in silicon as a debug flag.

SystolicConfig Default Meaning
sp_capacity_kb 256 L2 capacity
spad_read_delay 4 read latency (clocks)
l2_bank_occupancy 1 clocks a macro is busy per access (1 = pipelined)
l2_macro_rows 4096 the deepest macro, in rows (0 = no limit)
l2_ideal False build the idealized two-read-port reference instead
  • rows: sp_total_rows = floor(capacity / row bytes), rounded down to even;
  • group rows: G = sp_total_rows / 2;
  • banks per group, with Nd = ceil(G / l2_macro_rows) (1 if no limit):
    • pipelined (l2_bank_occupancy = 1): N = Nd;
    • otherwise the smallest divisor of dim of at least max(occupancy, Nd), or, if Nd > dim, the smallest multiple of dim of at least Nd. Every Streamer command reads dim consecutive rows from a base that is a multiple of dim, so the banks cycle with no break across commands, or each command reads its own aligned block, and a bank is never asked again within its occupancy. An occupancy above dim is refused: one buffer re-reads the same rows every dim clocks;
  • bank rows: ceil(G / N).

The bank count is set by occupancy and depth, not by latency: a pipelined macro takes a new read every clock, so one bank per group carries the stream. (A count that doesn’t divide dim, such as 3 banks under a 4×4 array at occupancy 3, would reuse a bank one clock after a command’s reads wrap to the next tile: occupancy 3 gives 4 banks there.) At 256 KiB, int8:

dim row (B) L2 rows group rows N (occ 1) N (occ 2) N (occ 4) bank rows (occ 1)
8 8 32768 16384 4 4 4 4096
16 16 16384 8192 2 2 4 4096
24 24 10922 5461 2 2 4 2731
32 32 8192 4096 1 2 4 4096
64 64 4096 2048 1 2 4 2048

With 1024-row macros the counts are 16, 8, 6, 4 and 2. dim 24 at 1024 rows gives 6 banks of 911 rows, neither a power of two.

Built: 1R1W. Measured against single-port (1RW) rather than built:

  • Area. By branes-silicon’s model, a dual-port (sram_hp, 8T) macro costs 8/6 ≈ 1.33× the transistors per KiB of a single-port one (sram_hd, 6T). kpugen’s floorplan carries that cost.
  • Schedule. With 1RW banks, the BlockMover’s writes need clocks in which their bank isn’t being read. In OS both groups are read on every clock of a stream. A write could then only take a bank the stream isn’t reading in that clock. That needs N >= 2 per group, and a write buffer or a write slot schedule that holds for every dataflow and for the corner-turned LOAD’s dim-row bursts. 1R1W needs none of that. Its writes never wait.
  • Cycles. A double-buffered 16×16×24 Matmul in each dataflow runs in the same number of cycles, with the same L2 traffic, on the banked L2 and on the idealized one, and C is bit-identical (test_matmul.py::test_banked_l2_sustains_the_stream_and_matches_the_ideal_model).
  • Two reads per clock.
    • OS reads an A and a B row in every streaming clock.
    • In the stationary mode, PRELOAD reads the B group beside the A group’s stream, back to back, with no bubble, at occupancy 1, 2 and 3 (test_streamer.py::TestOnTheBankedL2).
  • Writes beside reads. The BlockMover writes L2 in clocks where the Streamer reads it, with no conflict.
  • The Combiner’s writes into L2 (#68). Today results go to the Combiner’s own memory, and STORE reads them from there. When the Combiner writes into L2 and merges partials there, the result slots get their own bank group (the model). Here that is #38.
  • kpu-sim’s L2 bank model (stillwater-sc/kpu-sim#388).