Skip to content

SwizzledChunks spans stacked subtiles - #35

Merged
feitreim merged 3 commits into
mainfrom
issue-25-subtile-chunks
Jul 27, 2026
Merged

feitreim merged 3 commits into
mainfrom
issue-25-subtile-chunks

Conversation

@feitreim

Copy link
Copy Markdown
Owner

SwizzledChunks now walks stacked subtiles, so every width the library can
describe has a register ↔ shared path — not just the 64 bf16 columns of a
single swizzle atom.

The addressing

I agree with the derivation in the brief, and it falls out of a slightly
stronger statement that makes its open question disappear.

A tile is one flat stack of 128-byte rows. Subtile i starts
SUBTILE_BYTES = rows * 128 bytes along (exactly what tma_load's destination
arithmetic does), and SWIZZLE_128B XORs physical address bits [9:7] into
the 16-byte-chunk index. The base is 128-byte aligned, so the 128-byte row index
of subtile i row r is (base >> 7) + i * rows + r, giving:

subtile, chunk = chunk / 8, chunk % 8
row            = subtile * rows + row            // absolute 128-byte row
address        = base + row * 128 + (chunk ^ ((row + phase) & 7)) * 16

Written that way there is no separate "subtile term" and no separate "phase
term": a subtile is more rows, and the phase is the row's own position in the
period. The cursor carries rows (= R) and the logical chunk count alongside
base/phase.

What the phase depends on. The brief's form —
base + subtile * SUBTILE_BYTES + row * 128 + (chunk ^ ((row + phase) & 7)) * 16
— is the above with the subtile * rows contribution to the phase dropped. The
two agree exactly when rows % 8 == 0, i.e. when a subtile is a whole number of
swizzle periods, which holds at R = 64 and R = 128 and at every live call
site. So the brief's formula is right where it is used, and its worry was the
right worry. Rather than add a const assert!(R % 8 == 0) I folded the subtile
into the row: it costs nothing (a subtile * rows add replaces a
subtile * SUBTILE_BYTES add) and makes the precondition unnecessary — the
odd-R case is correct rather than forbidden, and the harness checks it.

ldst::fragment_address needed no change: its chunk index is column / 8,
already counted across the whole logical row, so a column in the second subtile
produces chunk 8.. and resolves itself. That is also why store_fragment and
load_fragment are untouched apart from doc comments.

Narrow path

chunk / 8 == 0 and chunk % 8 == chunk when chunk < 8, so the single-subtile
case is byte-for-byte today's expression. regcount before/after, sm_100a:

kernel main this branch
swizzle_roundtrip 31 31
stmatrix_roundtrip 40 40
ldmatrix_map 32 32

Zero spills either way, and every other kernel in the table is unchanged. The
new wide kernels sit at 32 registers, the 4-row one at 32.

What the device test proves

device-tests had one [64, 64] tile precisely because that was the only width
chunk_writer accepted. The three round trips are now shape-generic probes run
at both widths from the same body, and the narrow cases stay in the suite so a
regression there is distinguishable from a wrong subtile term.

pass  swizzle round trip          4096 elements placed exactly
pass  fragment map (16, 16)       1024 values, all at BaseLdtm's coordinates
pass  fragment map (32, 32)       4096 values, all at BaseLdtm's coordinates
pass  fragment map (32, 128)      16384 values, all at BaseLdtm's coordinates
pass  sttm restage (32, 128)      16384 values, all at BaseLdtm's coordinates
pass  stmatrix round trip         4096 elements placed exactly
pass  ldmatrix map                4096 elements, all at BaseLdtm's coordinates
pass  swizzle round trip wide     8192 elements placed exactly
pass  swizzle phase carry         512 elements placed exactly
pass  stmatrix round trip wide    8192 elements placed exactly
pass  ldmatrix map wide           8192 elements, all at BaseLdtm's coordinates
pass  sttm round trip             16384 registers survived TMEM unchanged
12 of 12 cases passed
  • swizzle round trip wide — TMA a [64, 128] identity tile in, read it
    back through the cursor in logical order. The TMA engine placed the bytes and
    the cursor found them again across the subtile stride: this is the case a
    wrong stride fails, and it fails loudly rather than plausibly.
  • ldmatrix map wide / stmatrix round trip wide — the narrow pair's
    discipline over all 4x8 blocks: the kernel dumps by
    (block, lane, slot, value) and the host applies Fragment::coordinate.
    Neither kernel encodes the expected answer.
  • swizzle phase carry — the case worth having. A [4, 128] tile: four
    rows is not a whole swizzle period, so its second subtile begins at phase 4.
    This is the only shape where "phase follows the absolute row" and "phase
    restarts per subtile" disagree — at R = 64 or 128 they coincide, so the
    wide cases above cannot separate them. It passes, which is silicon confirming
    the absolute-address reading; under the per-subtile one every row's columns
    64.. would come back permuted.

Host side: at is pure const-style math, so it is tested thoroughly — the
per-subtile formula at R = 64, the mid-period phase at R = 4, and
injectivity over [128,128], [64,256] and [4,128] at all eight base phases.
The injectivity test says in its own comment what it cannot see: any phase
permutes a row's eight chunks, so only the stride is at stake there.

Examples

softmax no longer has a GAP fence: it loads its [128, 128] tile with
load_fragment and stores it back with store_fragment, both through the real
cursor. It stays aspirational, and precisely what is still missing is:

(#5 landed, so sub_row/exp2/div_row are real calls now.) #22 is no longer
a blocker for it, only a cost: the [16, 16] band loop is written twice, once
per direction, which is the best argument for load_tile/store_tile yet.
layernorm drops #25 from its list for the same reason and keeps #22.
examples/README.md: item 3 of "Not covered by any open issue" is struck
through with the addressing that replaced it, item 1's tail and item 2 are
updated, and the status table reads #6, #9 for softmax.

Verified

  • modal run modal_app.py::build — green, and again after the rebase onto MMA walks build their own instruction descriptor #34.
    This is the check that matters most here: a removed
    const { assert!(..) } is a post-monomorphization thing.
  • modal run modal_app.py::regcount — the table above, against a stashed
    origin/main build.
  • modal run modal_app.py (B200) — once, the run quoted above.
  • cargo clippy --all-targets, cargo fmt --check, cargo test — clean.

Surprises

  • The stride wanted to be rows, not SUBTILE_BYTES. Folding it into the row
    index is what turns the phase question into a non-question; carrying bytes
    would have forced the R % 8 == 0 assert the brief anticipated.
  • fragment_address was already width-correct. The whole restriction lived in
    one const assert!, and chunk indices >= 8 had never appeared only because
    that assert made such a tile unconstructible.
  • The harness could not test the phase question at any shape the library ships,
    which is why the [4, 128] tile is in there. TMA is happy with a 4-row box
    under a 128B swizzle.

Closes #25

feitreim added 3 commits July 27, 2026 18:15
A tile is one flat stack of 128-byte swizzled rows: stacked subtile i
starts SUBTILE_BYTES = rows * 128 bytes along, and SWIZZLE_128B XORs
*physical* address bits [9:7] into the chunk index, so subtile i's row r
is the tile's 128-byte row i * rows + r and takes that row's phase. The
cursor splits a logical chunk index into subtile and chunk and adds the
two row terms; the phase follows the absolute row, so nothing is assumed
about rows against the 8-row swizzle period.

That deletes chunk_writer's one-subtile const-assert, which was the only
reason 64 bf16 columns was the sole width with a register/shared path at
all. Chunk indices already counted across the whole logical row, so
ldst::fragment_address needed no change: a column past the first subtile
lands in chunk 8.. and resolves itself.

The single-subtile case is byte-for-byte the old formula, subtile = 0.
The three round trips become shape-generic probes, so the wide cases are
the same bodies rather than copies that can drift, and the narrow ones
keep running beside them: the narrow path is what the hardware has
always agreed with, and the subtile term must leave it alone.

New cases:
- swizzle / stmatrix / ldmatrix ... wide: a 64 by 128 tile, two stacked
  subtiles, so every block at columns 64.. crosses the subtile stride.
- swizzle phase carry: a 4 by 128 tile. Four rows is not a whole swizzle
  period, so its second subtile begins mid-period. That is the only
  shape where an absolute swizzle phase and a per-subtile one differ; at
  R = 64 or 128 the two readings coincide.

cell_bits gives columns seven bits so a position identity can name a
128-column tile; the exponent field stays in 0x80..0xbf, still normal.
#25 gives the 128 by 128 tile a register/shared path in both directions
and #5 landed the ops, so the load GAP becomes a real load_fragment loop
and the store side gets its mirror. What is left is #6 -- row_max and
row_sum -- and #9, the TMA store, plus #22 as a cost rather than a hole:
the band loop is written twice because the movers are per-block.

README: item 3 closed, with the addressing that replaced it; status
table and blocked-on lists updated.
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.

SwizzledChunks cannot span stacked subtiles: only 64-wide bf16 tiles have a register<->shared path

1 participant