Skip to content

The charge comes back from the load that will deliver it (#29) - #82

Merged
feitreim merged 1 commit into
mainfrom
expect-tx-derived-from-loads
Jul 28, 2026
Merged

feitreim merged 1 commit into
mainfrom
expect-tx-derived-from-loads

Conversation

@feitreim

Copy link
Copy Markdown
Owner

Closes #29.

Every TMA producer wrote its own transaction-byte total and kept it in step
with the loads it issued by reading both:

sem.expect_tx((ATile::BYTES + BTile::BYTES) as u32);

Now the charge comes back from the load:

let keys   = k_ring.tile(i).tma_load(k_map, base, head, load.sem(i));
let values = v_ring.tile(i).tma_load(v_map, base, head, load.sem(i));
load.sem(i).expect_tx(keys + values);

Which shape, and why

#29 named two and preferred the second, wanting a look at the pipeline stages
first. The look says take it, and the reason is sharper than "convenient":

expect_tiles(&[…]) is the same fact stated twice with nicer syntax. A
list can omit a tile exactly as a sum can omit a term. What has to agree with
the charge is the set of loads issued, so the charge has to come from them
and not from anything that restates them.

Semaphore::expect_tx therefore takes a TransactionBytes and nothing else,
and the only way to obtain one is to issue a load. Two things fall out, and
they are the point:

  • A wrong number is not expressible. No public constructor, not an
    integer. There is nothing to mistype.
  • Issuing a load and dropping its charge is a compile error.
    TransactionBytes is #[must_use], so tile.tma_load(…); as a statement
    fails under -D warnings — that is the "add a load, forget the sum" edit
    expect_tx byte accounting is hand-summed and can silently disagree with the loads issued #29 is about — and a charge bound to a name that never reaches an
    expect_tx fails as an unused binding. It is also not Copy, so
    charging the same bytes to two barriers is a use of a moved value.

The lint is not theoretical: it fired on tests/gaps.rs the moment the return
type existed, on the two calls that name the signatures without using them.

Stated plainly, because the type does less than it looks: it says how many
bytes, never which barrier.
A load aimed at one semaphore whose charge is
paid to another still type-checks — and has to, because that is exactly what a
cluster stage does.

The cluster case, which #50 settled and this had to honour

#71 said the RANKS * form was the interface between the two issues and that
whichever shape #29 took, the cluster case is that total scaled by the ranks
staging into the barrier. It is:

let a_bytes = a_ring.tile(k).tma_load_2d_arriving_at(a_map, column, a_row, stage);
let b_bytes = b_ring.tile(k).tma_load_2d_arriving_at(b_map, column, b_row, stage);
if rank == LEADER {
    load.sem(k).expect_tx((a_bytes + b_bytes).across_ranks(RANKS));
}

Every rank derives the same half-stage charge from the loads it just issued,
because a cluster stage is symmetric by construction — the same symmetry the
MMA descriptor reads across the pair on. The leader scales its half by RANKS
to cover the peer's; the peer drops its copy. Nothing new is expressed and
nothing is fought: the leader still charges its own barrier for bytes it does
not issue, because mbarrier.arrive.expect_tx is .shared::cta only.

Charge and completions still commute. The GEMM's expect_tx now sits
after its loads rather than before, which the signed transaction counter
permits and sync.rs already documented. No sync sits between them and none
became owed. This is the one ordering change in the PR and it is the one the
B200 run is the gate on.

Where the charge could still be prose, and now cannot

tma_load_at — the partial-box form that builds a 128-row operand out of two
64-row global ranges — asked its caller in prose to charge
box_rows * C * E::BYTES, with box_rows a property of the map and
unreadable from the tile. It is tma_load_at::<BOX_ROWS> now, const so the
charge is derivable, with a const { assert!(BOX_ROWS <= R) } under it. It
has no callers in the repo; leaving the one load in the family whose charge is
a sentence would have left the hole open for the kernel that first wants it.

SharedTile::BYTES stays public and is untouched: it is a layout fact, and
the four kernels' shared plans are built out of it. CHARGE is the transfer
fact and is crate-private, because a kernel able to name a charge without
issuing a load is back to writing the number down.

flash_forward's KTile/VTile aliases are gone. Their last use was the
hand-sum.

The test that would have caught the drift

every_producers_derived_charge_is_the_sum_it_used_to_write_down — one case
per expect_tx in examples/, with the tile types the kernel declares on one
side and the literal byte count its hand-sum came to on the other. Both sides
are spelled out; a derivation checked against a hand-sum computed from the
same expression would be testing nothing.

a_tiles_charge_is_the_boxes_its_load_issues holds the identity underneath
it: CHARGE counts what the load loop does — one box per subtile, R rows of
one swizzle atom each — and BYTES counts the tile, and they are the same
number at every legal shape. Plus the partial-box arithmetic: half the rows,
half the bytes, and two halves back to the whole.

Verification

All run against this branch.

  • modal run modal_app.py::build — passes. Real cargo oxide build --arch sm_100a on library, examples and harness; 86 host tests.
  • modal run modal_app.py::examples — pass gemm 512x256x256 exact on a
    B200, plus pass softmax and pass layernorm. The gate that matters: the
    failure modes under change here are a deadlock and silently wrong numbers.
  • modal run modal_app.py::device_tests — 30 of 30 cases passed on a B200.
  • modal run modal_app.py::regcount — measured on this branch and on main,
    byte-for-byte identical across all 168 entry functions, every counter.
    A call-site refactor should move no registers, and it moves none.
  • cargo fmt, cargo clippy --all-targets -- -D warnings, cargo test (76
    passed) locally.

One thing outside my lane

device-tests/src/main.rs has ten expect_tx call sites and would not compile
against the new signature, so they are converted here — mechanically, two lines
each, no test's behaviour touched. Another agent is working in that directory
on a residency probe; this is a signature migration and should merge past it,
but it is worth knowing about rather than discovering in a conflict.

tma_load_2d_multicast_cg2 hands back one tile — what a single destination
receives, which is the whole of it for the own-bit mask the GEMM uses and the
only mask any kernel here passes. Whether a truly replicating mask charges its
one named barrier once or once per destination is #49's question, and it is
documented as open rather than guessed at here.

Every TMA producer wrote its own transaction-byte total by hand and had to
keep it in step with the set of `tma_load` calls it issued. Under-charging
flips the barrier before the data lands and the consumer reads a half-written
tile — no fault, no diagnostic. Over-charging hangs the block. Nothing was
watching for either.

`tma_load` and its family now return a `TransactionBytes`, and
`Semaphore::expect_tx` takes nothing else. There is no public constructor, so
a wrong number is not expressible; the type is `#[must_use]` and not `Copy`,
so issuing a load and dropping its charge, or paying one charge twice, are
compile errors.

`expect_tiles(&[…])` was the lighter alternative and is the same fact stated
twice: a list can omit a tile exactly as a sum can omit a term.

The cluster case is `TransactionBytes::across_ranks(RANKS)` — the `RANKS *`
form #50 named as the interface between the two issues, now derived from the
loads each rank issued rather than written down. Charge and completions still
commute; the GEMM's charge simply follows the calls it comes from.
@feitreim
feitreim merged commit 24bdf33 into main Jul 28, 2026
2 checks passed
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.

expect_tx byte accounting is hand-summed and can silently disagree with the loads issued

1 participant