|
39 | 39 | its own window of the destination -- and the matrix tiling of an operator |
40 | 40 | step (`mma_sync`, `dmma`; `_matrix`), which reads the same buffers: A from |
41 | 41 | 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. |
48 | 49 |
|
49 | 50 | The lanes form (`element_group.Lanes`) is lowered by `_lanes` instead: a |
50 | 51 | lane its element, read and written through its own pointers -- 16 bytes of |
@@ -867,9 +868,12 @@ def _mix(trip, steps) -> None: |
867 | 868 | `X_d`'s written in one access where the consumer's pitch does -- rows of |
868 | 869 | the group 16 bytes times an odd number apart, eight threads' rows on |
869 | 870 | 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.""" |
873 | 877 | b, fp = trip.builder, trip.fp |
874 | 878 | section, group = trip.section, trip.group |
875 | 879 | lead = steps[0].products[0] |
@@ -959,13 +963,29 @@ def block(g): |
959 | 963 | values = [acc[d][v] if live[v] is None else b.op( |
960 | 964 | 'select', fp, live[v], acc[d][v], b.const(0.0, fp), |
961 | 965 | 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]) |
969 | 989 |
|
970 | 990 | with b.for_(0, rounds, 1, hint='g') as loop: |
971 | 991 | g = _index(b, 'add', b.op('mul', INDEX, loop.induction, sets, |
@@ -1138,8 +1158,10 @@ def _matrix(trip, step) -> None: |
1138 | 1158 | element after element -- N the destination's rows, K each product's |
1139 | 1159 | contraction, the products one after another (`element_group.Step`). A |
1140 | 1160 | 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 |
1143 | 1165 | in registers across the products, and hands each to the destination: |
1144 | 1166 | the intermediate's buffer, or the output, straight from the |
1145 | 1167 | accumulators -- a lane's cells through the pointers of their rows' |
@@ -1174,6 +1196,10 @@ def _matrix(trip, step) -> None: |
1174 | 1196 | # four registers are A's in the instruction's order (`ldmatrixX4`). |
1175 | 1197 | ldmatrix = step.lowering == 'mma_sync' and all( |
1176 | 1198 | 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') |
1177 | 1203 | if ldmatrix: |
1178 | 1204 | if [a_cells[slot] for slot in sorted(a_cells)] != [ |
1179 | 1205 | (0, 0), (8, 0), (0, 4), (8, 4)]: |
@@ -1235,19 +1261,21 @@ def at_(index, scale, offset, hint): |
1235 | 1261 | return _index(b, 'add', b.op('mul', INDEX, index, scale, hint=hint), |
1236 | 1262 | offset, hint) |
1237 | 1263 |
|
1238 | | - def a_tile(mt, buffer, source, kt, k_lo, depth): |
| 1264 | + def a_tile(mt, buffer, source, kt, k_lo, depth, offset=0): |
1239 | 1265 | """A's registers of M tile `mt` at K `kt` of a product: by ldmatrix |
1240 | 1266 | -- 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.""" |
1243 | 1270 | m0 = b.op('mul', INDEX, mt, m_, hint='m') |
1244 | 1271 | if ldmatrix: |
1245 | 1272 | lm = b.op('add', INDEX, m0, lrow, hint='m') |
1246 | 1273 | if M % m_: |
1247 | 1274 | lm = b.op('select', INDEX, b.op('lt', BOOL, lm, M, hint='in'), |
1248 | 1275 | 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') |
1251 | 1279 | return list(b.matrix_load(source, at, count=4, type_=fp, |
1252 | 1280 | origins=[a_cells[slot] for slot |
1253 | 1281 | in sorted(a_cells)], |
@@ -1380,8 +1408,22 @@ def epilogue(accs, m0, n0): |
1380 | 1408 | hint='k') |
1381 | 1409 | a = [a_tile(mt, buffer, source, kt, k_lo, depth) |
1382 | 1410 | 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) |
1385 | 1427 | for s in range(slots): |
1386 | 1428 | place = read(t_tile, at_(e, slots, s, 'o'), 'o') |
1387 | 1429 | # A slot's tile where it holds one: always, with |
@@ -2870,7 +2912,7 @@ def gen_ir(self, builder) -> None: |
2870 | 2912 | elements = self._group.elements |
2871 | 2913 | for buffer in section.inputs + section.intermediates: |
2872 | 2914 | trip.buffers[id(buffer.tensor)] = builder.alloc( |
2873 | | - section.dtype, (elements * buffer.per_element,), |
| 2915 | + section.dtype, (elements * buffer.room,), |
2874 | 2916 | MemSpace.SHARED, arena=GeneralLexicon.TOTAL_SHR_MEM, |
2875 | 2917 | hint=_hint('g', buffer.tensor)) |
2876 | 2918 | # A pointer table's entries on 8 bytes, in the arena's elements. |
@@ -3266,19 +3308,26 @@ def _stage(self, trip, buffer, symbol) -> list: |
3266 | 3308 | def _pad(trip, buffer) -> None: |
3267 | 3309 | """Zeros past the rows of every column of the warp's element, where |
3268 | 3310 | 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`).""" |
3271 | 3314 | b = trip.builder |
3272 | 3315 | pad = buffer.pitch - buffer.height |
3273 | 3316 | width = buffer.cols[1] - buffer.cols[0] |
| 3317 | + target = trip.buffers[id(buffer.tensor)] |
3274 | 3318 | with b.for_(trip.lane, pad * width, trip.group.threads, hint='z', |
3275 | 3319 | uniform=Uniformity.LANE) as loop: |
3276 | 3320 | row = _index(b, 'add', b.op('rem', INDEX, loop.induction, pad, |
3277 | 3321 | hint='r'), buffer.rows[1], 'r') |
3278 | 3322 | col = _index(b, 'add', b.op('div', INDEX, loop.induction, pad, |
3279 | 3323 | 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')) |
3282 | 3331 |
|
3283 | 3332 | def __str__(self) -> str: |
3284 | 3333 | section = self._group.section |
|
0 commit comments