Skip to content

Tensor accumulator take 2 - #495

Open
jel221 wants to merge 1 commit into
masterfrom
feature/tmem-dataflow
Open

jel221 wants to merge 1 commit into
masterfrom
feature/tmem-dataflow

Conversation

@jel221

@jel221 jel221 commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

HUMAN COMMENT:
To reiterate from the previous PR, to justify this design:
I want to decouple the accumulator from the whole pipeline (particularly, the issue stage) because 1. the uops take hostage of the issue pipeline and 2. the accumulators block those registers from being used in other useful computation because of the scoreboard and finally 3. the accumulator itself is limited by the register file size

To accomplish this you need a tensor accumulator that is tihgtly coupled to the tensor core itself and a uop sequencer that directly feeds into the tensor accumulator, and not the issue stage.

Some ISA differences:

tc.mma, tc.cp, tc.commit and tmem.alloc are issued by thread 0 (or in terms of software semantics, just needs the thread0 to be active and have the right register value), and tmem.alloc returns the column in thread 0's register, and software stores it to shared memory.

tc.commit waits for all of the CORE's earlier tc.mma / tc.cp, so if you commit then barrier the wait orders the MMAs against loads and stores which means you don't need the equivalent of a tcgen05.fence.

Your concerns from the previous PR:

I separated out the sparse full K and B prefetch, will make separate PRs for that later.

I added the write D -> read C hazard checker on the accumulator that you mentioned in the review on Wednesday. When a step fires, it puts its accumulator address in a tag FIFO, and when it writes back it gets cleared. Then when a new step fires, it checks its C address against the tags and delays if it maches.

I also addressed the area overhead. In the first iteration I moved the K iterations from software into a hardware job sequencer to see how much better it does. That ended up some ~10% speedup for a lot more area, so I removed the ISA but forgot to remove the block in the initial PR,so it's gone for good now. I also trimmed the TMEM unit itself, the report below will show you how many LUTs it ends up as on the FPGA. Mostly it is the decoder and selector into the actual accumulator array, which is pretty much unavoidable.

The flashattention row gives a 400x speedup, because from the work before there was no TFU and now it does, so it's not actually a real comparison, so I guess you can also see it as also the validity of the TFU work on the side. Ha

=========== AI-generated comment ===========

Tensor memory (TMEM) and the tc.* MMA engine

Adds Blackwell-style (tcgen05) tensor memory to the TCU, behind VX_CFG_TCU_TMEM_ENABLE:

  • Per-core TMEM. It has NUM_TCU_BLOCKS × xtileM lanes by VX_CFG_TCU_TMEM_COLS 32-bit columns, stored in block RAM, fragment-major.
  • A single-thread asynchronous engine.
    • tc.mma: one K step per instruction. D is in TMEM; A comes from shared memory or TMEM; sparse metadata and MX scales come from TMEM.
    • tc.cp: copies rows from shared memory to TMEM.
    • tc.commit: completion arrives at a barrier.
  • Warp transfers. tmem.ld / tmem.st move data between TMEM and registers, lane-per-thread or in fragment shape.
  • Allocation. tmem.alloc / tmem.dealloc work per CTA. The column is returned in thread 0, which stores it to shared memory as tcgen05.alloc does.
  • Launch support. The KMU and CTA dispatch handle the TMEM budget and place warps aligned to TCU blocks.
  • VX_CFG_TCU_WMMA_ENABLE. TMEM builds can drop tcu.wmma/tcu.ld, the landing queue and the metadata store.
  • Layouts. TMEM builds keep row-major A and K-major B only.

The full design is in docs/designs/tensor_memory.md.

Sparse on TMEM. With FEDP2K, a sparse tc.mma step fills the whole doubled-K FEDP with compressed A (2 × TC_K words), so one step covers twice the logical K of a dense step. The core's upper FEDP half selects its B values with the second half-K metadata row from TMEM (sp_sel_hi/sp_mux_hi). This full-K step is internal (TCU_SP_FULLK, defined when TMEM, sparse and FEDP2K are all on); there is no user option. Warp WGMMA_SP is unchanged from master.

Why the full-K step:

  • It is what makes sparsity faster. Without it, a sparse step does the same K work as a dense step, so 2:4 sparsity saves storage but no time.

  • It matches NVIDIA. Every sparse MMA since Ampere covers twice the dense K per instruction: f16 mma.sp m16n8k32 against m16n8k16, wgmma.sp K=32 against 16, tcgen05.mma.sp K=32 against 16. NVIDIA rates sparse throughput at 2× dense on A100, H100 and B200.

  • It is the full 2× in practice (rtlsim, sgemm_tcu_tmem sparse, fp16 → fp32, NT=8, 8 warps, issue width 4):

    GEMM Full-K step (FEDP2K) Half-K step (no FEDP2K) Speedup
    64×64×256 29,144 57,271 1.97×
    128×128×256 116,275 235,193 2.02×
    • The half-K column is the build without FEDP2K. Its sparse step covers the same K as a half-K step would with FEDP2K.
    • The half-K build also runs 1.5× the instructions, because it needs twice as many tc.mma steps.
    • These runs have no added memory latency. Under a long DRAM latency, DXA transfers take a larger share of the time and the gain will be smaller.
  • Area (Beacon, tcuw, NT=32, sparse): the full-K step costs about 57.6K LUT (6.9%) in a TMEM build that keeps WMMA. That was measured by synthesizing with the step forced off.

    • About 22K is the upper-half B gather (sp_mux_hi, 128 instances of 173 LUT).
    • The other ~34K appears only when WMMA is also built.
    • Sparse TMEM without WMMA is 747,994 LUT, +0.5% over the WGMMA-only sparse build.
    • Doubling sparse throughput by adding tensor cores instead would double their area.

Not in this PR (separate PRs): sparse FULLK for warp WGMMA, and the dense K-major B next-block prefetch. (I'm interleaving a human comment here: so I left the sparse fullK option on WGMMA like you asked because it's hard to justify the speedup there, and also causes a software change which can be confusing I agree)

Builds without TMEM keep master's cycle counts (every non-TMEM perf gate is unchanged); their tile buffers are smaller (see Tests).

Performance (NT=8, 4 TCU blocks, 16 warps, FEDP2K, fp16 → fp32)

Against WGMMA. Both builds use the same configuration; the WGMMA build drops TMEM. Each WGMMA kernel is run with its fastest WGMMA_NRC. Cycles are rtlsim (SimX).

Kernel WGMMA only TMEM Speedup
GEMM 128×128×128 113,243 (113,900) 47,036 (48,328) 2.41× (2.36×)
GEMM 128×128×512 454,704 (457,421) 184,044 (185,630) 2.47× (2.46×)
Sparse GEMM 64×64×64 35,475 (34,047) 8,191 (8,509) 4.33× (4.00×)
Flash attention, 256 queries × 256 keys, d=32 — (92,586,760) 228,303 (227,220) see note (~400×)
  • WGMMA kernels.
    • GEMM: sgemm_tcu_wg_dxa with NRC=32. This is the kernel sgemm_tcu_tmem derives from, with the same DXA staging.
    • Sparse: sgemm_tcu_wg_sp.
  • TMEM kernels.
    • GEMM: sgemm_tcu_tmem with PIPE_N=2 and TMEM_NCHUNK=4.
    • Sparse: the same kernel with sparse A and metadata loaded by tc.cp. Each step uses the full-K sparse path.
    • Flash attention: flashattention4 -l64 -n32 -d32 -b4 -c4.
  • Flash attention is not like-for-like.
    • flash_attn_tcu_wg, the only WGMMA flash-attention kernel, computes softmax with software expf. On RV32 that means software double-precision calls.
    • It also doesn't use separate warps for softmax and MMA.
    • flashattention4 uses the TFU for exp and runs softmax and MMA on separate warps.
    • Most of the ~400× therefore comes from the kernel, not from TMEM. The WGMMA run is SimX only because it is too long for rtlsim.

TMEM kernels:

Kernel rtlsim cycles SimX cycles
FlashAttention-4, 4 tiles (-l64 -n32 -d64 -b4) 61,935 61,588
FlashAttention-4, same, without WMMA 61,935 61,588
FlashAttention-4, 256 × 256, d=32 (-l64 -n32 -d32 -b4 -c4) 228,303 227,220
GEMM 128×128×128 47,036 48,328
GEMM 128×128×512 184,044 185,630
Sparse GEMM 64×64×64, metadata by tc.cp 8,191 8,509

Area (Beacon, tcuw DUT: 4 TCU blocks, 8 warps, issue width 4, FEDP2K, fp16, TMEM 512 columns)

Each TMEM column is compared with the WGMMA-only build of the same formats.

Build WGMMA only TMEM TMEM, no WMMA
NT=8 FP16 LUT 100,302 106,551 (+6.2%) 101,447 (+1.1%)
FF 60,960 62,412 (+2.4%) 61,220 (+0.4%)
NT=8 sparse LUT 109,580 123,435 (+12.6%) 112,543 (+2.7%)
FF 61,360 64,860 (+5.7%) 63,268 (+3.1%)
NT=8 every format LUT 1,126,082 1,141,462 (+1.4%) 1,121,035 (−0.4%)
FF 263,728 268,513 (+1.8%) 266,921 (+1.2%)
NT=32 FP16 LUT 680,638 700,926 (+3.0%) 669,680 (−1.6%)
FF 378,042 379,615 (+0.4%) 375,351 (−0.7%)
NT=32 sparse LUT 744,037 837,298 (+12.5%) 747,994 (+0.5%)
FF 378,538 388,303 (+2.6%) 383,543 (+1.3%)
NT=32 every format not synthesized: Beacon's source preprocessing runs out of memory
  • Block RAM: TMEM adds 32 block RAMs (36 Kb each) at NT=8 and 116 at NT=32 in every build.
  • Every format: TF32, FP16, FP8, FP4, INT8, INT4, MX (MXFP8/MXFP4/NVFP4) and 2:4 sparsity.
  • No WMMA: this drops tcu.wmma/tcu.ld, the landing queue and the metadata store. No Blackwell kernel mixes mma.sync with tcgen05.
  • Sparse: most of the sparse overhead is the full-K step's second row of 2:4 selects and its upper-half B gather (sp_sel_hi/sp_mux_hi). WGMMA-only sparse shares one select row per FEDP row.
  • No new adders: TMEM adds no floating-point adders, because accumulation stays in the FEDPs.
  • Logic depth: the FP16 and sparse TMEM builds keep the FEDP's 25-level worst path, and TMEM's own worst is 23.
  • MX scales from TMEM: a tc.mma takes its MX scales already placed in TMEM, so it needs no per-step scale-index arithmetic. The per-warp tcu.wmma path computes that index with a multiply and a divide (mx_scale_at in VX_tcu_core.sv). In an every-format TMEM build that keeps WMMA, this gives a 187-level path. Without WMMA the same build is 29 levels, and that is the configuration for MX on TMEM.

Where the LUTs and FFs go, FP16 (TMEM minus WGMMA only):

Unit NT=8 LUT NT=8 FF NT=32 LUT NT=32 FF
VX_tcu_tmem 4,790 1,846 12,225 2,061
A buffers: the A-from-TMEM fill (4 blocks) 936 0 4,052 0
B buffer: K-major row merging, wide N step −26 41 1,242 43
Tile-buffer LMEM port (the tc.cp master) 105 1 397 1
Blocks: engine steps beside warp ops 912 0 3,044 0
Blocks: completion order queue 76 76 76 76
Cores −256 −240 −460 −336
Warp WGMMA front end left out −297 −272 −297 −272
Total +6,249 +1,452 +20,288 +1,573

Inside VX_tcu_tmem. FFs are exact register widths. LUTs are netlist LUTs by source line, scaled to Beacon's module total, so they are approximate.

Part NT=8 LUT NT=8 FF NT=32 LUT NT=32 FF
RAM write data (D, tc.cp row, store in either shape) and port control ~1,730 0 ~7,290 0
Op slots: one registered op per block, without the store data ~870 744 ~1,040 840
tmem.ld / tmem.st transfer units ~440 — ~1,250 —
Response headers and the load/alloc result — 391 — 511
Engine: decode, walk, step register, prologue, write-back queues ~640 348 ~1,400 347
Shared next-state logic ~460 — ~470 —
Op queue and commits (attach, post, fire) ~290 258 ~400 258
Allocator ~270 64 ~300 64
Row-hazard check ~50 24 ~55 24
tc.cp sequencing ~30 17 ~15 17

The area trims cut VX_tcu_tmem at NT=32 from 23,331 to 12,225 LUT and from 6,034 to 2,061 FF:

  • Stores bypass the op slot. A tmem.st writes from the dispatch buffer, which holds the data until accepted, so the slot no longer keeps 1,024 bits of register data.
  • Encoded write select. The RAM write data uses a 4-way select, encoded once per RAM, so each bit is one LUT.
  • Encoded load select. The load read-out uses an encoded select.
  • tmem.alloc returns its result in thread 0 only.

Tests

  • New regressions:
    • sgemm_tcu_tmem: dense, sparse, tc.cp, A from TMEM, MX, and NT=4/16/32.
    • sgemm_tcu_tmem_mx.
    • flashattention4.
  • Each regression runs on SimX and rtlsim, with model-parity cases and one perf gate (perf_gate-tmem).
  • Perf baselines were re-recorded with pytest ci -m perf_gate --update-baselines. Every non-TMEM perf gate keeps master's cycle and instruction counts; the new perf_gate-tmem is 42,569 cycles.
  • Beacon baselines were re-recorded with ci/logic_gate.py --update-baseline [--tool beacon_asic] --against-rev 1061088aa. No unit is deeper in any FPGA or ASIC build. The tile buffers' row-merging write path also makes non-TMEM WGMMA builds smaller: the tcu DUT goes from 117,205 to 96,340 LUT and tensor from 259,758 to 217,719, with the same flip-flops.

@jel221 jel221 mentioned this pull request Nov 22, 2026
….cp/tc.commit, tmem.ld/st, tmem.alloc/dealloc), with VX_CFG_TCU_WMMA_ENABLE; perf baselines re-recorded with pytest ci -m perf_gate --update-baselines, Beacon baselines with ci/logic_gate.py --update-baseline [--tool beacon_asic] --against-rev 1061088
@jel221
jel221 force-pushed the feature/tmem-dataflow branch from a6e49d3 to 92fe49b Compare November 23, 2026 10:25

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant