tns_mm_left fixture: matmul2d with a cooperative left input, and its construction
The owned fixture tns_mm_left, built for macOS 27 (each run the header's _v2 form), computes out = A * B + C over the descriptors macOS 27's gather matmul kernels use, 16 x 32 x 32 and 32 x 32 x 32 over one simdgroup, A a cooperative left input filled by coordinate, B transposed or not in device memory, in threadgroup memory or as a cooperative right input, in float, half and bfloat (36 variants), one M x 32 tile per threadgroup from blocks a buffer names. Its second kernel, tns_init_left, reads what constructing a cooperative left input in a loop's body leaves in its storage, as tns_init reads a destination's and a right input's. The run forms are the gather kernels' own: run_cooperative_<t>_dv_<t>_f32_v2, run_cooperative_<t>_tg_<t>_f32_v2 and run_cooperative_<t>_<t>_f32_v2.
This commit is contained in:
1 parent
80a3cf4bb1
commit
b620952743
5 files changed
+16121
No files matched your search
@@ -227,6 +227,7 @@ depth buffer 2 holds, bounded by its stack); an authored case that executes a lo
|
||||
| `tns_mm_edge` | `tns_mm_edge` | `matmul2d` float sums where `tns_mm_float` does not reach: extents that cut k, m, and n or pass k, denormal, infinite, NaN, and overflowing values, relaxed precision, `multiply` mode, a transposed or cooperative right input, and tiles of two and four simdgroups (function constant 0; extents and the tile block from a buffer) | `out[64 m + n]` = C after the product, by coordinate, one tile per threadgroup (cases `matmul2d-edge-*`) |
|
||||
| `tns_mm_flags` | `tns_mm_flags` | `matmul2d` float sums with relaxed precision and in `multiply` mode over the shapes, transposes, element types, cooperative right input, and scopes of one, two, and four simdgroups the plain sums were read over, and the plain sums over the two shapes only the layout probe read (128 x 128 x 32 and 8 x 128 x 32 over four simdgroups) (function constant 0; extents, the tile block, and the output row stride from a buffer) | `out[stride m + n]` = C after the product, by coordinate, one tile per threadgroup (cases `matmul2d-relaxed-*`, `matmul2d-multiply-*`, `matmul2d-plain-*`) |
|
||||
| `tns_mm_float` | `tns_mm_float` | `matmul2d` of `float`, `half`, and `bfloat` into a cooperative `float` destination, B in device or threadgroup memory or a cooperative right input (function constant 0), one 16 x 16 x 32 tile per threadgroup from blocks a buffer names | Metal's float sums: pair, C-position, product-rounding, and random tiles (cases `matmul2d-*-sum-order-*`) |
|
||||
| `tns_mm_left` | `tns_mm_left`, `tns_init_left` | `matmul2d` with a cooperative left input, built for macOS 27 (`_v2` runs): 16 x 32 x 32 and 32 x 32 x 32, B transposed or not, in device or threadgroup memory or a cooperative right input, `float`, `half`, and `bfloat` (function constant 0; one M x 32 tile per threadgroup from blocks a buffer names); and what constructing a cooperative left input in a loop's body leaves in its storage, as `tns_init` reads the other operands | Metal's float sums with a cooperative left input (cases `matmul2d-left-cooperative-sum-order-*`); per round, each element's bits and the sum read back (cases `matmul2d-cooperative-left-construction-*`) |
|
||||
| `tns_mm_v2` | `tns_mm_v2` | `tns_mm_float` built for macOS 27, where every `run` is the header's `_v2` form (no scale factors, dynamic extents), with a transposed B also as a cooperative right input and in threadgroup memory (variants 10 to 15) | Metal's float sums through the `_v2` runs |
|
||||
| `wrg_sampler_positions` | `wrg_sampler_positions` | Metal's position of a normalized coordinate (the wrap into one period first, the product rounded ties away from zero) at texel centres, footprint steps, a few ulps either side, outside [0, 1), and at exact ties: linear, nearest, and bicubic samples and gathers of a 2D texture under repeat, mirrored repeat, clamp-to-zero, and clamp-to-edge, a 1D texture's nearest samples, and a 2D array's gather | texel `(i, j)` holds `(i + 1, j + 1, layer, 1)`, so each result names the texels it read (cases `sampler-positions-at-13x11`, `-7x5`, `-255x3`; at power-of-two sizes, `-16x8` and `-4x32`, and `-16x11`, whose 11-texel axis is not one) |
|
||||
| `wrg_gather_depth_positions` | `wrg_gather_depth_positions` | the same positions for a depth texture's gathers under the four address modes and `gather_compare` (less) under three | texel `(i, j)` holds `(i + 1 + 256 (j + 1)) / 2^20` (cases `depth-gather-positions-at-13x11`, `-255x3`, and `-16x8`) |
|
||||
|
||||
@@ -366,6 +366,7 @@ fixture tns_mm_edge: metal: Apple metal version 32023.921 (metalfe-32023.921.6);
|
||||
fixture tns_mm_flags: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425)
|
||||
fixture tns_mm_float: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425)
|
||||
fixture tns_mm_int: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425)
|
||||
fixture tns_mm_left: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118)
|
||||
fixture tns_mm_v2: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118)
|
||||
fixture tob_renders: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425)
|
||||
fixture trh_forward: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425)
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,180 @@
|
||||
// metal-std: metal4.1
|
||||
// metal-macos-min: 27.0
|
||||
// Owned fixture: matmul2d with a cooperative left input, built for macOS 27 (each run in the
|
||||
// header's `_v2` form, no scale factors and dynamic extents), over the descriptors macOS 27's
|
||||
// gather matmul kernels use: 16 x 32 x 32 and 32 x 32 x 32, B transposed or not, one simdgroup.
|
||||
//
|
||||
// `tns_mm_left`: threadgroup t (32 threads, one simdgroup) computes one M x 32 tile,
|
||||
// out[32 M t ...] = A * B + C, through a cooperative destination of float initialized from C by
|
||||
// coordinate. tiles[t] names the blocks it reads: A is block tiles[t].x of a (M x 32, row-major,
|
||||
// M * 32 elements a block), a cooperative left input filled by coordinate; B block tiles[t].y of b
|
||||
// (32 x 32, row-major, 1024 elements; transposed, its rows are B's columns), in device memory,
|
||||
// staged into threadgroup memory, or a cooperative right input filled by coordinate; C block
|
||||
// tiles[t].z of c (M x 32 floats). Function constant 0 (0 when absent) selects M, the transpose,
|
||||
// where B lives, and the element type of A and B (float, half, bfloat).
|
||||
//
|
||||
// `tns_init_left`: what constructing a cooperative left input leaves in its storage, as
|
||||
// `tns_init` reads a destination's and a right input's. For each of `rounds` rounds (a buffer, so
|
||||
// the loop stays a loop), each thread constructs the left input in the loop's body and writes the
|
||||
// bits of each element i it holds (at most 64) to out[(round * threads + tid) * 64 + i]; then it
|
||||
// sets element i to round * 32 + i + 1 and writes the sum of its elements, read back, to
|
||||
// out[rounds * threads * 64 + round * threads + tid]. Function constant 0 selects M and the
|
||||
// element type. Loops bounded by the capacity.
|
||||
#include <metal_stdlib>
|
||||
#include <metal_tensor>
|
||||
#include <MetalPerformancePrimitives/MetalPerformancePrimitives.h>
|
||||
using namespace metal;
|
||||
using namespace mpp::tensor_ops;
|
||||
|
||||
constant uint selected [[function_constant(0)]];
|
||||
// Without the constant, variant 0.
|
||||
constant uint variant = is_function_constant_defined(selected) ? selected : 0;
|
||||
|
||||
using Mode = matmul2d_descriptor::mode;
|
||||
constexpr constant matmul2d_descriptor nn16 =
|
||||
matmul2d_descriptor(16, 32, 32, false, false, false, Mode::multiply_accumulate);
|
||||
constexpr constant matmul2d_descriptor nt16 =
|
||||
matmul2d_descriptor(16, 32, 32, false, true, false, Mode::multiply_accumulate);
|
||||
constexpr constant matmul2d_descriptor nn32 =
|
||||
matmul2d_descriptor(32, 32, 32, false, false, false, Mode::multiply_accumulate);
|
||||
constexpr constant matmul2d_descriptor nt32 =
|
||||
matmul2d_descriptor(32, 32, 32, false, true, false, Mode::multiply_accumulate);
|
||||
|
||||
enum Where { Device, Threadgroup, Cooperative };
|
||||
|
||||
template <typename E, matmul2d_descriptor D, Where R>
|
||||
static void tile(device const uchar *a_bytes, device const uchar *b_bytes, uint3 blocks,
|
||||
device const float *c, device float *out, threadgroup E *staged, uint lane) {
|
||||
using DT = tensor<device E, dextents<int32_t, 2>, tensor_inline>;
|
||||
using TT = tensor<threadgroup E, dextents<int32_t, 2>, tensor_inline>;
|
||||
device const E *a = (device const E *)a_bytes + blocks.x * D.m * 32;
|
||||
device E *b = const_cast<device E *>((device const E *)b_bytes) + blocks.y * 1024;
|
||||
c += blocks.z * D.m * 32;
|
||||
// B's extents: (columns, rows) of b as it is stored.
|
||||
dextents<int32_t, 2> b_extents = dextents<int32_t, 2>(32, 32);
|
||||
matmul2d<D, execution_simdgroup> op;
|
||||
auto lt = op.template get_left_input_cooperative_tensor<E, E, float>();
|
||||
for (ushort i = 0; i < lt.get_capacity(); ++i) {
|
||||
if (lt.is_valid_element(i)) {
|
||||
auto idx = lt.get_multidimensional_index(i);
|
||||
lt[i] = a[idx[1] * 32 + idx[0]];
|
||||
}
|
||||
}
|
||||
auto ct = op.template get_destination_cooperative_tensor<DT, DT, float>();
|
||||
for (ushort i = 0; i < ct.get_capacity(); ++i) {
|
||||
if (ct.is_valid_element(i)) {
|
||||
auto idx = ct.get_multidimensional_index(i);
|
||||
ct[i] = c[idx[1] * 32 + idx[0]];
|
||||
}
|
||||
}
|
||||
if constexpr (R == Device) {
|
||||
DT B(b, b_extents);
|
||||
op.run(lt, B, ct);
|
||||
} else if constexpr (R == Threadgroup) {
|
||||
for (uint i = lane; i < 1024; i += 32)
|
||||
staged[i] = b[i];
|
||||
threadgroup_barrier(mem_flags::mem_threadgroup);
|
||||
TT B(staged, b_extents);
|
||||
op.run(lt, B, ct);
|
||||
} else {
|
||||
auto rt = op.template get_right_input_cooperative_tensor<E, E, float>();
|
||||
for (ushort i = 0; i < rt.get_capacity(); ++i) {
|
||||
if (rt.is_valid_element(i)) {
|
||||
auto idx = rt.get_multidimensional_index(i);
|
||||
rt[i] = b[idx[1] * 32 + idx[0]];
|
||||
}
|
||||
}
|
||||
op.run(lt, rt, ct);
|
||||
}
|
||||
for (ushort i = 0; i < ct.get_capacity(); ++i) {
|
||||
if (ct.is_valid_element(i)) {
|
||||
auto idx = ct.get_multidimensional_index(i);
|
||||
out[idx[1] * 32 + idx[0]] = ct[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static void products(device const uchar *a, device const uchar *b, uint3 blocks,
|
||||
device const float *c, device float *out, threadgroup E *staged, uint lane,
|
||||
uint v) {
|
||||
switch (v) {
|
||||
case 0: tile<E, nn16, Device>(a, b, blocks, c, out, staged, lane); break;
|
||||
case 1: tile<E, nt16, Device>(a, b, blocks, c, out, staged, lane); break;
|
||||
case 2: tile<E, nn32, Device>(a, b, blocks, c, out, staged, lane); break;
|
||||
case 3: tile<E, nt32, Device>(a, b, blocks, c, out, staged, lane); break;
|
||||
case 4: tile<E, nn16, Threadgroup>(a, b, blocks, c, out, staged, lane); break;
|
||||
case 5: tile<E, nt16, Threadgroup>(a, b, blocks, c, out, staged, lane); break;
|
||||
case 6: tile<E, nn32, Threadgroup>(a, b, blocks, c, out, staged, lane); break;
|
||||
case 7: tile<E, nt32, Threadgroup>(a, b, blocks, c, out, staged, lane); break;
|
||||
case 8: tile<E, nn16, Cooperative>(a, b, blocks, c, out, staged, lane); break;
|
||||
case 9: tile<E, nt16, Cooperative>(a, b, blocks, c, out, staged, lane); break;
|
||||
case 10: tile<E, nn32, Cooperative>(a, b, blocks, c, out, staged, lane); break;
|
||||
default: tile<E, nt32, Cooperative>(a, b, blocks, c, out, staged, lane); break;
|
||||
}
|
||||
}
|
||||
|
||||
// Variants 0 to 11 are float, 12 to 23 half, 24 to 35 bfloat, each `products`' twelve: M 16 or 32,
|
||||
// B not transposed or transposed, in device memory (0 to 3), threadgroup memory (4 to 7), or a
|
||||
// cooperative right input (8 to 11).
|
||||
kernel void tns_mm_left(device const uchar *a [[buffer(0)]],
|
||||
device const uchar *b [[buffer(1)]],
|
||||
device const float *c [[buffer(2)]],
|
||||
device float *out [[buffer(3)]],
|
||||
constant uint4 *tiles [[buffer(4)]],
|
||||
uint t [[threadgroup_position_in_grid]],
|
||||
uint lane [[thread_index_in_threadgroup]]) {
|
||||
threadgroup float staged_f[1024];
|
||||
threadgroup half staged_h[1024];
|
||||
threadgroup bfloat staged_b[1024];
|
||||
uint3 blocks = tiles[t].xyz;
|
||||
// M is 16 for the variants whose `products` index is 0, 1, 4, 5, 8 or 9, else 32.
|
||||
uint v = variant % 12;
|
||||
uint m = (v % 4) < 2 ? 16 : 32;
|
||||
out += t * m * 32;
|
||||
if (variant < 12)
|
||||
products<float>(a, b, blocks, c, out, staged_f, lane, v);
|
||||
else if (variant < 24)
|
||||
products<half>(a, b, blocks, c, out, staged_h, lane, v);
|
||||
else
|
||||
products<bfloat>(a, b, blocks, c, out, staged_b, lane, v);
|
||||
}
|
||||
|
||||
constant uint MAX_HELD = 64;
|
||||
|
||||
static uint bits(float v) { return as_type<uint>(v); }
|
||||
static uint bits(half v) { return uint(as_type<ushort>(v)); }
|
||||
static uint bits(bfloat v) { return uint(as_type<ushort>(v)); }
|
||||
|
||||
template <matmul2d_descriptor D, typename E>
|
||||
static void constructed(device uint *out, uint tid, uint threads, uint rounds) {
|
||||
matmul2d<D, execution_simdgroup> op;
|
||||
for (uint r = 0; r < rounds; ++r) {
|
||||
auto ct = op.template get_left_input_cooperative_tensor<E, E, float>();
|
||||
uint cap = min(uint(ct.get_capacity()), MAX_HELD);
|
||||
device uint *held = out + (r * threads + tid) * MAX_HELD;
|
||||
for (uint i = 0; i < cap; ++i)
|
||||
held[i] = bits(E(ct[i]));
|
||||
for (uint i = 0; i < cap; ++i)
|
||||
ct[i] = E(r * 32 + i + 1);
|
||||
uint sum = 0;
|
||||
for (uint i = 0; i < cap; ++i)
|
||||
sum += uint(float(E(ct[i])));
|
||||
out[rounds * threads * MAX_HELD + r * threads + tid] = sum;
|
||||
}
|
||||
}
|
||||
|
||||
// Variants 0 to 2: M 16 in float, half, bfloat; 3 to 5: M 32.
|
||||
kernel void tns_init_left(device uint *out [[buffer(0)]],
|
||||
constant uint &rounds [[buffer(1)]],
|
||||
uint tid [[thread_index_in_threadgroup]],
|
||||
uint threads [[threads_per_threadgroup]]) {
|
||||
switch (variant) {
|
||||
case 0: constructed<nn16, float>(out, tid, threads, rounds); break;
|
||||
case 1: constructed<nn16, half>(out, tid, threads, rounds); break;
|
||||
case 2: constructed<nn16, bfloat>(out, tid, threads, rounds); break;
|
||||
case 3: constructed<nn32, float>(out, tid, threads, rounds); break;
|
||||
case 4: constructed<nn32, half>(out, tid, threads, rounds); break;
|
||||
default: constructed<nn32, bfloat>(out, tid, threads, rounds); break;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user