benchmarkDecodeMatrices method
Isolates representative real weight matrices from model orchestration.
Each sample reuses fixed inputs for repetitions products in one
command buffer (one input quantization). This is a cache-reuse probe,
not an estimate of a complete token's memory traffic or latency.
q8WorkgroupSweep instead isolates the LM head with four rows per
subgroup and 64/128/256 invocations per workgroup, in six rounds.
q8VectorSweep instead alternates scalar/vector loads at 128
invocations, four rows per subgroup, for six rounds.
Implementation
List<Map<String, Object?>> benchmarkDecodeMatrices({
int repetitions = 32,
bool q8WorkgroupSweep = false,
bool q8VectorSweep = false,
void Function(Map<String, Object?> sample)? onSample,
}) {
if (repetitions < 1 || repetitions > 256) {
throw ArgumentError.value(repetitions, 'repetitions', 'must be 1..256');
}
if (q8WorkgroupSweep && q8VectorSweep) {
throw ArgumentError('choose one Q8 experiment');
}
final matrices = <String, _Matrix>{
if (!q8WorkgroupSweep && !q8VectorSweep) 'ffn_up': _layers.first.w1,
if (!q8WorkgroupSweep && !q8VectorSweep) 'ffn_down': _layers.first.w2,
'lm_head': _lmHead,
};
final x = _newBuffer(matrices.values.map((w) => w.cols).reduce(math.max));
final y = _newBuffer(matrices.values.map((w) => w.rows).reduce(math.max));
for (var i = 0; i < x.length; i++) {
x.view[i] = math.sin(i * 0.17) + 0.25 * math.cos(i * 0.07);
}
void run(_Matrix w, int count) {
_begin();
for (var i = 0; i < count; i++) {
w.encode(x.handle, y.handle, 0, 1);
}
if (_commit(1) != 1) throw StateError('GPU matrix benchmark failed');
}
final samples = <Map<String, Object?>>[];
final references = <String, Uint32List>{};
profiling = false;
try {
setQ8Vector(false);
setQ8Workgroup(128);
decodeRows = 0;
for (final entry in matrices.entries) {
run(entry.value, 1);
references[entry.key] = Uint32List.fromList(
y.view.buffer.asUint32List(y.view.offsetInBytes, entry.value.rows),
);
}
final orders = q8VectorSweep
? [
[0, 1],
[1, 0],
[0, 1],
[1, 0],
[0, 1],
[1, 0],
]
: q8WorkgroupSweep
? [
[64, 128, 256],
[128, 256, 64],
[256, 64, 128],
[256, 128, 64],
[128, 64, 256],
[64, 256, 128],
]
: [
[0, 1, 2, 4],
[1, 2, 4, 0],
[2, 4, 0, 1],
[4, 0, 1, 2],
];
for (var round = 0; round < orders.length; round++) {
for (final variant in orders[round]) {
final rows = (q8WorkgroupSweep || q8VectorSweep) ? 4 : variant;
final workgroup = q8WorkgroupSweep ? variant : 128;
final vector = q8VectorSweep && variant == 1;
if (q8VectorSweep && !setQ8Vector(vector)) continue;
if (q8WorkgroupSweep && !setQ8Workgroup(workgroup)) continue;
decodeRows = rows;
for (final entry in matrices.entries) {
final w = entry.value;
run(w, 2); // warm this layout and matrix before timing
final wall = Stopwatch()..start();
run(w, repetitions);
final wallMs = wall.elapsedMicroseconds / 1000 / repetitions;
final gpuMs = _lastGpuMs() / repetitions;
final bits = y.view.buffer.asUint32List(
y.view.offsetInBytes,
w.rows,
);
final reference = references[entry.key]!;
var differing = 0, nonFinite = 0;
for (var i = 0; i < w.rows; i++) {
if (bits[i] != reference[i]) differing++;
if (!y.view[i].isFinite) nonFinite++;
}
final sample = <String, Object?>{
'matrix': entry.key,
'format': w.q4 ? 'q4' : 'q8',
'matrix_rows': w.rows,
'matrix_cols': w.cols,
'round': round + 1,
'decode_rows': rows,
'q8_workgroup': workgroup,
'q8_vector': vector,
'repetitions': repetitions,
'gpu_ms_per_dispatch': gpuMs,
'wall_ms_per_dispatch': wallMs,
'differing_outputs': differing,
'non_finite': nonFinite,
};
samples.add(sample);
onSample?.call(sample);
}
}
}
} finally {
restoreDecodeRowsDefault();
restoreQ8VectorDefault();
setQ8Workgroup(128);
profiling = false;
}
return samples;
}