Skip to content

A staged tile can be folded into memory, not only written over it (#169) - #171

Merged
feitreim merged 2 commits into
mainfrom
feat/accumulate-shared-rows
Aug 3, 2026
Merged

feitreim merged 2 commits into
mainfrom
feat/accumulate-shared-rows

Conversation

@feitreim

@feitreim feitreim commented Aug 3, 2026

Copy link
Copy Markdown
Owner

Closes #169.

What

global::accumulate_shared_rows — store_shared_rows' += twin. Same
signature, same chunk split, same access_width ladder, with copy_bytes
replaced by add_bytes: one ld.shared.vN, one ld.global.vN,
Element::add_packed per word, one st.global.vN.

Plus one required method on Element: fn add_packed(current: u32, update: u32) -> u32.
Three impls (Bf16, F16, F32), each unpack, +, pack. It is a trait
method rather than a call-site snippet because a narrow element's C += x has
to widen both sides, add in fp32 and round the sum once — the addend arrived
already rounded, and the second rounding is an error the accumulator did not
have to carry. That rounding belongs beside Element::pack.

Why

C += A·Bᵀ is every backward pass and had no library route at all, so
callers hand-rolled a read-modify-write and dropped off the ladder to 4-byte
accesses. On feitreim/oxide-train's tcgen05 GEMM, where store and accumulate
are the same compute pipeline and differ only in the epilogue (B200, 4096³,
min of 10 after 3 warm-ups):

entry point ms TFLOP/s
bf16 store 0.123 1113.8
bf16 accumulate 0.256 536.2
fp32 store 0.144 956.2
fp32 accumulate 0.275 500.1

The fold costs more than the whole rest of the kernel. Not bandwidth —
re-reading a 4096² bf16 C is 33.5 MB, ~4 µs of HBM against a 133 µs
difference. It is the shape, which is #116's finding one level up.

Held by

  • shared accumulate / shared accumulate wide (device-tests, B200): the two
    shared drain cases with the rectangle seeded with its own identities instead
    of poison, so the answer is every identity doubled. Doubling a bf16 moves the
    exponent and leaves the mantissa alone, so the comparison stays on bit
    patterns with no tolerance
    — a fold that rounded twice, accumulated at bf16,
    or dropped the read lands on a different pattern, not a nearby one. Margins
    stay poison and stay checked, so all of check_shared_drain's geometry
    failures are named here too.
  • f32_add_packed_is_the_fp32_addition_itself and
    bf16_add_packed_pairs_the_halves_lane_wise (host). Bf16::pack is
    cvt.rn.bf16x2.f32 with no host body, so the host half can only pin the
    lane-wise pairing; the rounding is the device cases'.
  • tests/gaps.rs::a_staged_tile_can_be_folded_into_memory. Separate from
    a_staged_drain_is_two_calls because the two can rot apart, and unbounded in
    E where that one bounds Unpacked = [f32; 2] — which is the claim that the
    fold is not an stmatrix path and takes an fp32 destination.

GAPS.md

§2.6 records the addition, and two entries beside it that this work moved:

  • §1.2 was stale. It read "Bf16 is still the only operand impl", and had
    not been true since F16 landed — impl MmaElement for F16 sits eight lines
    above impl MmaElement for Bf16 in the same file, and experiments' gemm_sol
    is benchmarked against a cuBLASLt FP16 baseline because of it. The section's
    own failure mode again. tf32 (KIND = 1) and fp8 are the genuinely absent
    ones — and feitreim/oxide-train layernorm_rows sits at 255 registers with spills and gets 2 blocks/SM where softmax gets 6 #67 was filed believing its "f32" GEMM
    needed tf32, when that kernel's operands are bf16 and only its C is fp32.
  • §2.6 now names why an fp32 epilogue is still on the register route.
    store_shared_rows/accumulate_shared_rows are generic over E and would
    take an SharedTile<F32, …>, but nothing can fill one:
    ldst::store_tile/store_tile_x4 bound E: Element<Unpacked = [f32; 2]>
    because they are stmatrix. So an fp32 C pays store_rows' scattered
    per-value stores — The epilogue's stores were the wrong shape, and stmatrix fixes 20% of them (#15) #116's 20.43 µs/tile against the staged route's 6.68. Filed
    nowhere; what it wants is a plain non-stmatrix store_tile over
    SwizzledChunks, generic in E.

Checked

scripts/fmt --check, cargo clippy --all-targets -- -D warnings, cargo test
(105 + 1 + 75), cargo doc --no-deps with RUSTDOCFLAGS=-D warnings, and the
intra-doc link pass with the cfg_attr commented out (only the known
GlobalLayout host-gated dangles remain). Tier 2 and 3 are CI's — the
device-tests cases have not been run on a B200 from here.

Downstream: feitreim/oxide-train #67 pins kittens to this branch's rev and
will move back to ferro main once this merges.

`global` had `store_shared_rows` and no `+=` twin, so `C += A·Bᵀ` — every
backward pass — had no library route and every caller hand-rolled a
read-modify-write. That drops off the access ladder: `oxide-train`'s tcgen05
GEMM measures 1113.8 TFLOP/s storing and 536.2 accumulating at 4096³ on one
compute pipeline, so the fold costs more than the whole rest of the kernel. It
is not bandwidth — re-reading a 4096² bf16 `C` is 33.5 MB against a 133 µs
difference — it is the shape, which is #116's finding one level up: 4-byte
scattered accesses where the store path issues 16-byte contiguous ones.

`accumulate_shared_rows` is `store_shared_rows` with the copy replaced by a load
of both sides, an `Element::add_packed` and a store: same signature, same chunk
split, same `access_width` ladder, so the fold costs the read and nothing else.

The addition is the element's. A bf16 `C += x` has to widen both sides, add in
fp32 and round the sum **once** — the addend arrived already rounded, and
rounding again after the add is an error the accumulator did not have to carry.
That belongs beside `Element::pack`, so it is a required trait method rather
than a snippet at each epilogue: three impls, each `unpack`, `+`, `pack`.

Held by `shared accumulate` and `shared accumulate wide` on a B200 — the two
`shared drain` cases with the rectangle seeded with its own identities instead
of poison, so the answer is every identity doubled. Doubling a bf16 moves the
exponent and leaves the mantissa alone, so the comparison stays on bit patterns
with no tolerance: a fold that rounded twice, or dropped the read, lands on a
different pattern rather than a nearby one. The margins are still poison and
still checked, so the geometry the store cases prove is proved again here.

GAPS.md §2.6 records it, and two entries beside it that this work falsified or
found:

- §1.2 read "`Bf16` is still the only *operand* impl", and had not been true
  since `F16` landed eight lines above `impl MmaElement for Bf16` in the same
  file. The section's usual failure and not a slow rot. tf32 and fp8 are the
  genuinely absent ones — and `oxide-train` #67 was filed believing its fp32
  GEMM needed tf32, when that kernel's operands are bf16 and only its `C` is
  fp32.
- §2.6 now names the reason an **fp32** epilogue is still on the register route:
  `store_shared_rows` would take an `F32` tile, but nothing can fill one, since
  `ldst::store_tile*` are `stmatrix` and bound `Unpacked = [f32; 2]`. So an fp32
  `C` pays the 20.43 µs/tile scattered drain against the staged route's 6.68.
  Filed nowhere; a plain non-`stmatrix` `store_tile` over `SwizzledChunks` is
  what it wants.
@feitreim feitreim added the gpu-ci Run the B200 device-test tier on this PR (costs GPU minutes) label Aug 3, 2026
@feitreim
feitreim merged commit f45ebbc into main Aug 3, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gpu-ci Run the B200 device-test tier on this PR (costs GPU minutes)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No accumulating epilogue: global can store a staged tile but not fold one into memory

1 participant