Skip to content

Commit b94e2f1

Browse files
davschnellerclaude
andcommitted
Merge branch 'multi-element-halves' into dev3
Matrix step 2.2 (A in TF32 halves, off by default) on top of the engines port and the NVIDIA auto-fission. Conflicts in three files, each where both sides added to the same call or list: - element_group.py: `lower()` takes both sides' parameters (`ring`, `registers`; `halves`), its docstring both. A section a producer's ring takes returns as the engines port has it, without halves; the second-stage form takes them (`_halved`). `shared_bytes` counts the rings and both halves (`Buffer.room`). In `fill()`, whose candidates the engines port made pairs of lowerings and engine, each mma_sync tiling is expanded by its halves where `HALVES` is on -- an asynchronous engine's reads none, `halvable` taking only A that ldmatrix reads -- before the producer rings go ahead of all. `__all__` the union. - generator.py: the planned section re-lowered with the plan's ring, registers and halves. - mutation_check.py: the engines port's anchor at that call's `double=` line, which the merged call keeps. Checked: the merged tree is file for file the one tested on the local sm_120 and gfx1150 -- the multi-element, asynchronous multi-element, producer, mbarrier ring, tcgen05, PIR engine, fission, fission order and TMA tests: 947 passed, 53 skipped (no sm_100 or asynchronous engine, no room for a ring at sm_120, gfx1150's 64 kB). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 parents 1b9841e + 361c3b1 commit b94e2f1

6 files changed

Lines changed: 638 additions & 76 deletions

File tree

‎src/tensorforge/backend/instructions/compute/multi_element.py‎

Lines changed: 79 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@
3939
its own window of the destination -- and the matrix tiling of an operator
4040
step (`mma_sync`, `dmma`; `_matrix`), which reads the same buffers: A from
4141
the group operand's (by ldmatrix in F32, where the buffer is pitched for
42-
it), B from the operator's stage, and D reaching its consumer's buffer, or
43-
the output from the accumulators: one reduction a cell (`red.global.add`),
44-
a store where the step assigns. Where the operators' nonzeros are known,
45-
`fma` walks only those (`_sparse`): a lane executes the nonzeros of its
46-
output's row, the rows sorted so that a warp's lanes walk about as many
47-
each.
42+
it -- both TF32 halves where the star step wrote them,
43+
`element_group.Buffer.halves`), B from the operator's stage, and D
44+
reaching its consumer's buffer, or the output from the accumulators: one
45+
reduction a cell (`red.global.add`), a store where the step assigns.
46+
Where the operators' nonzeros are known, `fma` walks only those
47+
(`_sparse`): a lane executes the nonzeros of its output's row, the rows
48+
sorted so that a warp's lanes walk about as many each.
4849
4950
The lanes form (`element_group.Lanes`) is lowered by `_lanes` instead: a
5051
lane its element, read and written through its own pointers -- 16 bytes of
@@ -867,9 +868,12 @@ def _mix(trip, steps) -> None:
867868
`X_d`'s written in one access where the consumer's pitch does -- rows of
868869
the group 16 bytes times an odd number apart, eight threads' rows on
869870
eight bank groups. Rows past the window are written zero: the
870-
destination's padding, which the tiling's padded K reads. The block's
871-
threads past the group's rows take more of them: `sets` threads a row,
872-
each every `sets`-th group of rows."""
871+
destination's padding, which the tiling's padded K reads. Where the
872+
tiling reads a destination in TF32 halves (`element_group.Buffer.
873+
halves`), each value is split as the tiling would split it, and both
874+
halves are written, each 16 bytes at a time where the pitch allows.
875+
The block's threads past the group's rows take more of them: `sets`
876+
threads a row, each every `sets`-th group of rows."""
873877
b, fp = trip.builder, trip.fp
874878
section, group = trip.section, trip.group
875879
lead = steps[0].products[0]
@@ -959,13 +963,29 @@ def block(g):
959963
values = [acc[d][v] if live[v] is None else b.op(
960964
'select', fp, live[v], acc[d][v], b.const(0.0, fp),
961965
hint='y') for v in range(vec)]
962-
if stores[d]:
963-
b.store(target, b.pack(vector, *values, hint='y'), at,
964-
align=16)
965-
continue
966-
for v in range(vec):
967-
b.store(target, values[v], _index(b, 'add', at, v, 'a'),
968-
predicate=live[v])
966+
writes = [(at, values)]
967+
if dest.halves:
968+
# The tiling reads the destination split
969+
# (`element_group.Buffer.halves`): each value's TF32
970+
# halves, the upper at its cell and the lower past the
971+
# group's upper ones -- the split the tiling would make
972+
# at every read of the value, made once.
973+
from tensorforge.backend.instructions.compute.primitives \
974+
import nvidia
975+
split = nvidia.tfconvert(b, values)
976+
writes = [(at, [_as_float(b, u) for u, _ in split]),
977+
(_index(b, 'add', at,
978+
dest.half_offset(group.elements), 'a'),
979+
[_as_float(b, l) for _, l in split])]
980+
for cell, data in writes:
981+
if stores[d]:
982+
b.store(target, b.pack(vector, *data, hint='y'),
983+
cell, align=16)
984+
continue
985+
for v in range(vec):
986+
b.store(target, data[v],
987+
_index(b, 'add', cell, v, 'a'),
988+
predicate=live[v])
969989

970990
with b.for_(0, rounds, 1, hint='g') as loop:
971991
g = _index(b, 'add', b.op('mul', INDEX, loop.induction, sets,
@@ -1138,8 +1158,10 @@ def _matrix(trip, step) -> None:
11381158
element after element -- N the destination's rows, K each product's
11391159
contraction, the products one after another (`element_group.Step`). A
11401160
is the group operand's buffer as it stands, row `m` of it an element's
1141-
column, the contraction contiguous; B the product's stage, zero outside
1142-
its window and in the padding. A warp takes the tiles of D in turn, D
1161+
column, the contraction contiguous -- in F32 its values' TF32 halves
1162+
where its producer wrote those (`element_group.Buffer.halves`), else
1163+
split here; B the product's stage, zero outside its window and in the
1164+
padding, split there or here. A warp takes the tiles of D in turn, D
11431165
in registers across the products, and hands each to the destination:
11441166
the intermediate's buffer, or the output, straight from the
11451167
accumulators -- a lane's cells through the pointers of their rows'
@@ -1174,6 +1196,10 @@ def _matrix(trip, step) -> None:
11741196
# four registers are A's in the instruction's order (`ldmatrixX4`).
11751197
ldmatrix = step.lowering == 'mma_sync' and all(
11761198
section.buffer(p.second.tensor).stride for p in step.products)
1199+
if not ldmatrix and any(section.buffer(p.second.tensor).halves
1200+
for p in step.products):
1201+
raise InternalError(f'{step.dest.alias}: A held in TF32 halves '
1202+
f'where it is read lane by lane')
11771203
if ldmatrix:
11781204
if [a_cells[slot] for slot in sorted(a_cells)] != [
11791205
(0, 0), (8, 0), (0, 4), (8, 4)]:
@@ -1235,19 +1261,21 @@ def at_(index, scale, offset, hint):
12351261
return _index(b, 'add', b.op('mul', INDEX, index, scale, hint=hint),
12361262
offset, hint)
12371263

1238-
def a_tile(mt, buffer, source, kt, k_lo, depth):
1264+
def a_tile(mt, buffer, source, kt, k_lo, depth, offset=0):
12391265
"""A's registers of M tile `mt` at K `kt` of a product: by ldmatrix
12401266
-- the padding and the clamped rows past the group read numbers,
1241-
zeros or rows whose D is not written -- or lane by lane, zero past
1242-
the group and past the product's depth."""
1267+
zeros or rows whose D is not written -- from the cells `offset`
1268+
further on where that is given (a buffer's lower TF32 halves), or
1269+
lane by lane, zero past the group and past the product's depth."""
12431270
m0 = b.op('mul', INDEX, mt, m_, hint='m')
12441271
if ldmatrix:
12451272
lm = b.op('add', INDEX, m0, lrow, hint='m')
12461273
if M % m_:
12471274
lm = b.op('select', INDEX, b.op('lt', BOOL, lm, M, hint='in'),
12481275
lm, 0, hint='m')
1249-
at = rows_of(lm, buffer, cols[0], _index(b, 'add', b.op(
1250-
'add', INDEX, lcol, kt, hint='k'), k_lo, 'k'))
1276+
at = _index(b, 'add', rows_of(lm, buffer, cols[0], _index(
1277+
b, 'add', b.op('add', INDEX, lcol, kt, hint='k'), k_lo,
1278+
'k')), offset, 'a')
12511279
return list(b.matrix_load(source, at, count=4, type_=fp,
12521280
origins=[a_cells[slot] for slot
12531281
in sorted(a_cells)],
@@ -1380,8 +1408,22 @@ def epilogue(accs, m0, n0):
13801408
hint='k')
13811409
a = [a_tile(mt, buffer, source, kt, k_lo, depth)
13821410
for mt in mts]
1383-
halves = ([nvidia.tfconvert(b, x) for x in a]
1384-
if atom.mode == nvidia.MMAMode.TF32 else None)
1411+
if buffer.halves:
1412+
# A as its producer split it (`element_group.
1413+
# Buffer.halves`): the upper halves where the values
1414+
# would be, the lower ones from their block, as the
1415+
# instruction's operands -- nothing converted.
1416+
lower = buffer.half_offset(trip.group.elements)
1417+
halves = [[(nvidia._as_tf32(b, u),
1418+
nvidia._as_tf32(b, l))
1419+
for u, l in zip(x, a_tile(
1420+
mt, buffer, source, kt, k_lo, depth,
1421+
lower))]
1422+
for mt, x in zip(mts, a)]
1423+
else:
1424+
halves = ([nvidia.tfconvert(b, x) for x in a]
1425+
if atom.mode == nvidia.MMAMode.TF32
1426+
else None)
13851427
for s in range(slots):
13861428
place = read(t_tile, at_(e, slots, s, 'o'), 'o')
13871429
# A slot's tile where it holds one: always, with
@@ -2870,7 +2912,7 @@ def gen_ir(self, builder) -> None:
28702912
elements = self._group.elements
28712913
for buffer in section.inputs + section.intermediates:
28722914
trip.buffers[id(buffer.tensor)] = builder.alloc(
2873-
section.dtype, (elements * buffer.per_element,),
2915+
section.dtype, (elements * buffer.room,),
28742916
MemSpace.SHARED, arena=GeneralLexicon.TOTAL_SHR_MEM,
28752917
hint=_hint('g', buffer.tensor))
28762918
# A pointer table's entries on 8 bytes, in the arena's elements.
@@ -3266,19 +3308,26 @@ def _stage(self, trip, buffer, symbol) -> list:
32663308
def _pad(trip, buffer) -> None:
32673309
"""Zeros past the rows of every column of the warp's element, where
32683310
an intermediate's buffer has a pitch (`element_group.Buffer`): what
3269-
the matrix tiling's padded steps of K read, to multiply zeros. An
3270-
input's are staged (`_stage`)."""
3311+
the matrix tiling's padded steps of K read, to multiply zeros -- of
3312+
both TF32 halves, where the buffer holds them. An input's are
3313+
staged (`_stage`)."""
32713314
b = trip.builder
32723315
pad = buffer.pitch - buffer.height
32733316
width = buffer.cols[1] - buffer.cols[0]
3317+
target = trip.buffers[id(buffer.tensor)]
32743318
with b.for_(trip.lane, pad * width, trip.group.threads, hint='z',
32753319
uniform=Uniformity.LANE) as loop:
32763320
row = _index(b, 'add', b.op('rem', INDEX, loop.induction, pad,
32773321
hint='r'), buffer.rows[1], 'r')
32783322
col = _index(b, 'add', b.op('div', INDEX, loop.induction, pad,
32793323
hint='c'), buffer.cols[0], 'c')
3280-
b.store(trip.buffers[id(buffer.tensor)], b.const(0.0, trip.fp),
3281-
trip.at(buffer, row, col))
3324+
zero = b.const(0.0, trip.fp)
3325+
at = trip.at(buffer, row, col)
3326+
b.store(target, zero, at)
3327+
if buffer.halves:
3328+
b.store(target, zero, _index(
3329+
b, 'add', at, buffer.half_offset(trip.group.elements),
3330+
'a'))
32823331

32833332
def __str__(self) -> str:
32843333
section = self._group.section

0 commit comments

Comments
 (0)