oarust 0.8.6

GPU-first semantic computing engine written in Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
// @generated by tool/gen/fn/generate.py; DO NOT EDIT.
// schema=matrix_reduce.json schema_version=1 generator_version=64
// schema_sha256=82fbdb597e6fd7557a8c081a6302ceb383b2df455716387a549bed1a21f44fdd
//! Matrix reductions and shared checked axis inference.

use crate::{
	DType, Error, Matrix, OpAttribute, Result,
	matrix::autograd,
	runtime::{BufferBinding, ComputeDispatch, KernelId, PushConstant, SemanticDispatch},
};

struct AxisShape {
	outer_size: u32,
	dim_size: u32,
	inner_size: u32,
	group_count: u32,
}

fn resolve_axis(input: &Matrix, dim: i32, operation: &'static str) -> Result<AxisShape> {
	if input.dtype() != DType::F32 || input.shape().is_empty() || input.num_elements() == 0 {
		return Err(Error::invalid_argument(format!(
			"{operation} requires a nonempty FP32 matrix"
		)));
	}
	let byte_size = input
		.num_elements()
		.checked_mul(DType::F32.size_bytes())
		.ok_or_else(|| Error::invalid_argument(format!("{operation} byte size overflows usize")))?;
	u32::try_from(byte_size).map_err(|_| {
		Error::invalid_argument(format!(
			"{operation} byte-address range exceeds the admitted shader ABI"
		))
	})?;
	let rank = input.shape().len();
	let axis = if dim == -1 {
		rank - 1
	} else {
		usize::try_from(dim).map_err(|_| {
			Error::invalid_argument(format!(
				"{operation} dim must be -1 or a valid non-negative axis; found {dim}"
			))
		})?
	};
	if axis >= rank {
		return Err(Error::invalid_argument(format!(
			"{operation} dim {dim} is invalid for rank {rank}"
		)));
	}
	let outer_size = input.shape()[..axis]
		.iter()
		.try_fold(1_usize, |product, extent| product.checked_mul(*extent))
		.ok_or_else(|| Error::invalid_argument(format!("{operation} outer size overflows usize")))?;
	let dim_size = input.shape()[axis];
	let inner_size = input.shape()[axis + 1..]
		.iter()
		.try_fold(1_usize, |product, extent| product.checked_mul(*extent))
		.ok_or_else(|| Error::invalid_argument(format!("{operation} inner size overflows usize")))?;
	let group_count = outer_size.checked_mul(inner_size).ok_or_else(|| {
		Error::invalid_argument(format!("{operation} dispatch count overflows usize"))
	})?;
	Ok(AxisShape {
		outer_size: u32::try_from(outer_size)
			.map_err(|_| Error::invalid_argument(format!("{operation} outer size exceeds u32")))?,
		dim_size: u32::try_from(dim_size)
			.map_err(|_| Error::invalid_argument(format!("{operation} axis size exceeds u32")))?,
		inner_size: u32::try_from(inner_size)
			.map_err(|_| Error::invalid_argument(format!("{operation} inner size exceeds u32")))?,
		group_count: u32::try_from(group_count)
			.map_err(|_| Error::invalid_argument(format!("{operation} dispatch count exceeds u32")))?,
	})
}

fn sum_shape(input: &Matrix, dim: i32, operation: &'static str) -> Result<(AxisShape, Vec<usize>)> {
	if dim < -1 {
		return Err(Error::invalid_argument(format!(
			"{operation} dim must be -1 or a valid non-negative axis; found {dim}"
		)));
	}
	if dim == -1 {
		resolve_axis(input, -1, operation)?;
		let element_count = u32::try_from(input.num_elements())
			.map_err(|_| Error::invalid_argument(format!("{operation} element count exceeds u32")))?;
		return Ok((
			AxisShape {
				outer_size: 1,
				dim_size: element_count,
				inner_size: 1,
				group_count: 1,
			},
			vec![1],
		));
	}
	let axis = resolve_axis(input, dim, operation)?;
	let mut output_shape = input.shape().to_vec();
	output_shape[usize::try_from(dim).map_err(|_| {
		Error::invalid_argument(format!("{operation} dim cannot be represented as usize"))
	})?] = 1;
	Ok((axis, output_shape))
}

/// Stable Softmax over `dim`, where `-1` selects the last dimension.
///
/// The operation records asynchronously and returns its output immediately.
///
/// # Errors
///
/// Returns an error when the input is empty or not FP32, `dim` is invalid, an
/// axis extent or dispatch count exceeds the shader ABI, allocation fails, or
/// runtime recording fails.
pub fn softmax(input: &Matrix, dim: i32) -> Result<Matrix> {
	let contract = crate::core::operation::matrix::SOFTMAX;
	let kernel = KernelId::MatrixSoftmaxF32;
	let operation = contract.name();
	let axis = resolve_axis(input, dim, operation)?;
	let output = Matrix::allocate(
		input.engine_handle(),
		input.shape().to_vec(),
		input.num_elements(),
		DType::F32,
	)?;
	let buffers = [
		BufferBinding::read(input.storage()),
		BufferBinding::write(output.storage()),
	];
	let push_constants = [
		PushConstant::U32(axis.outer_size),
		PushConstant::U32(axis.dim_size),
		PushConstant::U32(axis.inner_size),
	];
	let attributes = [OpAttribute::SignedInteger {
		name: "dim".into(),
		value: i64::from(dim),
	}];
	let inputs = [input];
	let outputs = [&output];
	input.engine_handle().record_semantic(
		ComputeDispatch {
			kernel,
			buffers: &buffers,
			push_constants: &push_constants,
			workgroups: [axis.group_count, 1, 1],
		},
		SemanticDispatch {
			contract,
			inputs: &inputs,
			outputs: &outputs,
			attributes: &attributes,
		},
	)?;
	autograd::record_softmax(input, &output, dim)?;
	Ok(output)
}

pub(crate) fn softmax_backward(
	forward_output: &Matrix,
	output_gradient: &Matrix,
	dim: i32,
) -> Result<Matrix> {
	let contract = crate::core::operation::matrix::SOFTMAX_BACKWARD;
	let kernel = KernelId::MatrixSoftmaxBackwardF32;
	let operation = contract.name();
	if forward_output.shape() != output_gradient.shape()
		|| forward_output.dtype() != output_gradient.dtype()
	{
		return Err(Error::invalid_argument(format!(
			"{operation} requires matching forward-output and output-gradient matrices"
		)));
	}
	if !forward_output
		.engine_handle()
		.same_as(output_gradient.engine_handle())
	{
		return Err(Error::invalid_argument(format!(
			"{operation} inputs must belong to the same engine"
		)));
	}
	let axis = resolve_axis(forward_output, dim, operation)?;
	let input_gradient = Matrix::allocate(
		forward_output.engine_handle(),
		forward_output.shape().to_vec(),
		forward_output.num_elements(),
		DType::F32,
	)?;
	let buffers = [
		BufferBinding::read(forward_output.storage()),
		BufferBinding::read(output_gradient.storage()),
		BufferBinding::write(input_gradient.storage()),
	];
	let push_constants = [
		PushConstant::U32(axis.outer_size),
		PushConstant::U32(axis.dim_size),
		PushConstant::U32(axis.inner_size),
	];
	let attributes = [OpAttribute::SignedInteger {
		name: "dim".into(),
		value: i64::from(dim),
	}];
	let inputs = [forward_output, output_gradient];
	let outputs = [&input_gradient];
	forward_output.engine_handle().record_semantic(
		ComputeDispatch {
			kernel,
			buffers: &buffers,
			push_constants: &push_constants,
			workgroups: [axis.group_count, 1, 1],
		},
		SemanticDispatch {
			contract,
			inputs: &inputs,
			outputs: &outputs,
			attributes: &attributes,
		},
	)?;
	Ok(input_gradient)
}

/// Stable LogSoftmax over `dim`, where `-1` selects the last dimension.
///
/// The operation records asynchronously and returns its output immediately.
///
/// # Errors
///
/// Returns an error when the input is empty or not FP32, `dim` is invalid, an
/// axis extent or dispatch count exceeds the shader ABI, allocation fails, or
/// runtime recording fails.
pub fn log_softmax(input: &Matrix, dim: i32) -> Result<Matrix> {
	let contract = crate::core::operation::matrix::LOG_SOFTMAX;
	let kernel = KernelId::MatrixLogSoftmaxF32;
	let operation = contract.name();
	let axis = resolve_axis(input, dim, operation)?;
	let output = Matrix::allocate(
		input.engine_handle(),
		input.shape().to_vec(),
		input.num_elements(),
		DType::F32,
	)?;
	let buffers = [
		BufferBinding::read(input.storage()),
		BufferBinding::write(output.storage()),
	];
	let push_constants = [
		PushConstant::U32(axis.outer_size),
		PushConstant::U32(axis.dim_size),
		PushConstant::U32(axis.inner_size),
	];
	let attributes = [OpAttribute::SignedInteger {
		name: "dim".into(),
		value: i64::from(dim),
	}];
	let inputs = [input];
	let outputs = [&output];
	input.engine_handle().record_semantic(
		ComputeDispatch {
			kernel,
			buffers: &buffers,
			push_constants: &push_constants,
			workgroups: [axis.group_count, 1, 1],
		},
		SemanticDispatch {
			contract,
			inputs: &inputs,
			outputs: &outputs,
			attributes: &attributes,
		},
	)?;
	autograd::record_log_softmax(input, &output, dim)?;
	Ok(output)
}

pub(crate) fn log_softmax_backward(
	forward_output: &Matrix,
	output_gradient: &Matrix,
	dim: i32,
) -> Result<Matrix> {
	let contract = crate::core::operation::matrix::LOG_SOFTMAX_BACKWARD;
	let kernel = KernelId::MatrixLogSoftmaxBackwardF32;
	let operation = contract.name();
	if forward_output.shape() != output_gradient.shape()
		|| forward_output.dtype() != output_gradient.dtype()
	{
		return Err(Error::invalid_argument(format!(
			"{operation} requires matching forward-output and output-gradient matrices"
		)));
	}
	if !forward_output
		.engine_handle()
		.same_as(output_gradient.engine_handle())
	{
		return Err(Error::invalid_argument(format!(
			"{operation} inputs must belong to the same engine"
		)));
	}
	let axis = resolve_axis(forward_output, dim, operation)?;
	let input_gradient = Matrix::allocate(
		forward_output.engine_handle(),
		forward_output.shape().to_vec(),
		forward_output.num_elements(),
		DType::F32,
	)?;
	let buffers = [
		BufferBinding::read(forward_output.storage()),
		BufferBinding::read(output_gradient.storage()),
		BufferBinding::write(input_gradient.storage()),
	];
	let push_constants = [
		PushConstant::U32(axis.outer_size),
		PushConstant::U32(axis.dim_size),
		PushConstant::U32(axis.inner_size),
	];
	let attributes = [OpAttribute::SignedInteger {
		name: "dim".into(),
		value: i64::from(dim),
	}];
	let inputs = [forward_output, output_gradient];
	let outputs = [&input_gradient];
	forward_output.engine_handle().record_semantic(
		ComputeDispatch {
			kernel,
			buffers: &buffers,
			push_constants: &push_constants,
			workgroups: [axis.group_count, 1, 1],
		},
		SemanticDispatch {
			contract,
			inputs: &inputs,
			outputs: &outputs,
			attributes: &attributes,
		},
	)?;
	Ok(input_gradient)
}

/// Sum all values when `dim` is `-1`, or keep and reduce one selected axis.
///
/// A full reduction returns shape `[1]`; an axis reduction preserves rank and
/// replaces the selected extent with one. The operation records asynchronously.
///
/// # Errors
///
/// Returns an error when the input is empty or not FP32, `dim` is invalid, a
/// size exceeds the admitted shader ABI, allocation fails, or recording fails.
pub fn sum(input: &Matrix, dim: i32) -> Result<Matrix> {
	let contract = crate::core::operation::matrix::SUM;
	let operation = contract.name();
	let (axis, output_shape) = sum_shape(input, dim, operation)?;
	let output_count = if dim == -1 {
		1
	} else {
		usize::try_from(axis.group_count)
			.map_err(|_| Error::invalid_argument(format!("{operation} output count exceeds usize")))?
	};
	let output = Matrix::allocate(
		input.engine_handle(),
		output_shape,
		output_count,
		DType::F32,
	)?;
	let buffers = [
		BufferBinding::read(input.storage()),
		BufferBinding::write(output.storage()),
	];
	let full_push = [PushConstant::U32(axis.dim_size)];
	let axis_push = [
		PushConstant::U32(axis.outer_size),
		PushConstant::U32(axis.dim_size),
		PushConstant::U32(axis.inner_size),
	];
	let (kernel, push_constants, workgroups) = if dim == -1 {
		(KernelId::MatrixSumF32, full_push.as_slice(), [1, 1, 1])
	} else {
		(
			KernelId::MatrixSumAxisF32,
			axis_push.as_slice(),
			[axis.group_count.div_ceil(256), 1, 1],
		)
	};
	let attributes = [OpAttribute::SignedInteger {
		name: "dim".into(),
		value: i64::from(dim),
	}];
	let inputs = [input];
	let outputs = [&output];
	input.engine_handle().record_semantic(
		ComputeDispatch {
			kernel,
			buffers: &buffers,
			push_constants,
			workgroups,
		},
		SemanticDispatch {
			contract,
			inputs: &inputs,
			outputs: &outputs,
			attributes: &attributes,
		},
	)?;
	autograd::record_sum(input, &output, dim)?;
	Ok(output)
}

pub(crate) fn sum_backward(input: &Matrix, output_gradient: &Matrix, dim: i32) -> Result<Matrix> {
	let contract = crate::core::operation::matrix::SUM_BACKWARD;
	let operation = contract.name();
	let (axis, output_shape) = sum_shape(input, dim, operation)?;
	if output_gradient.dtype() != DType::F32 || output_gradient.shape() != output_shape {
		return Err(Error::invalid_argument(format!(
			"{operation} output gradient has {:?} {}; expected {:?} F32",
			output_gradient.shape(),
			output_gradient.dtype().token(),
			output_shape
		)));
	}
	if !input
		.engine_handle()
		.same_as(output_gradient.engine_handle())
	{
		return Err(Error::invalid_argument(format!(
			"{operation} inputs must belong to the same engine"
		)));
	}
	let input_gradient = Matrix::allocate(
		input.engine_handle(),
		input.shape().to_vec(),
		input.num_elements(),
		DType::F32,
	)?;
	let buffers = [
		BufferBinding::read(output_gradient.storage()),
		BufferBinding::write(input_gradient.storage()),
	];
	let push_constants = [
		PushConstant::U32(axis.outer_size),
		PushConstant::U32(axis.dim_size),
		PushConstant::U32(axis.inner_size),
	];
	let attributes = [OpAttribute::SignedInteger {
		name: "dim".into(),
		value: i64::from(dim),
	}];
	let inputs = [input, output_gradient];
	let outputs = [&input_gradient];
	input.engine_handle().record_semantic(
		ComputeDispatch {
			kernel: KernelId::MatrixSumBackwardF32,
			buffers: &buffers,
			push_constants: &push_constants,
			workgroups: [
				u32::try_from(input.num_elements())
					.map_err(|_| Error::invalid_argument(format!("{operation} element count exceeds u32")))?
					.div_ceil(256),
				1,
				1,
			],
		},
		SemanticDispatch {
			contract,
			inputs: &inputs,
			outputs: &outputs,
			attributes: &attributes,
		},
	)?;
	Ok(input_gradient)
}

/// Count rows whose last-axis FP32 argmax equals the integer class label.
///
/// The returned `[1]` U32 Matrix remains device-resident. Reading it is an
/// explicit synchronization boundary.
///
/// # Errors
///
/// Returns an error when logits are not a nonempty rank-two-or-greater FP32
/// Matrix, labels do not match the leading logits shape or use U8/U32/I32,
/// inputs belong to different engines, a size exceeds the shader ABI,
/// allocation fails, or recording fails.
pub fn categorical_accuracy_count(logits: &Matrix, labels: &Matrix) -> Result<Matrix> {
	let contract = crate::core::operation::matrix::CATEGORICAL_ACCURACY_COUNT;
	let mask = None::<&Matrix>;
	let operation = contract.name();
	if logits.dtype() != DType::F32 || logits.shape().len() < 2 || logits.num_elements() == 0 {
		return Err(Error::invalid_argument(format!(
			"{operation} requires nonempty rank-two-or-greater F32 logits"
		)));
	}
	let classes = *logits.shape().last().ok_or_else(|| {
		Error::invalid_argument(format!("{operation} logits must expose a class axis"))
	})?;
	let label_shape = &logits.shape()[..logits.shape().len() - 1];
	if labels.shape() != label_shape {
		return Err(Error::invalid_argument(format!(
			"{operation} labels have shape {:?}; expected {:?}",
			labels.shape(),
			label_shape
		)));
	}
	let label_dtype = match labels.dtype() {
		DType::U8 => 0,
		DType::U32 => 1,
		DType::I32 => 2,
		_ => {
			return Err(Error::invalid_argument(format!(
				"{operation} labels must use U8, U32, or I32; found {}",
				labels.dtype().token()
			)));
		}
	};
	let engine = logits.engine_handle();
	if !engine.same_as(labels.engine_handle()) {
		return Err(Error::invalid_argument(format!(
			"{operation} inputs must belong to the same engine"
		)));
	}
	if let Some(mask) = mask {
		if mask.dtype() != DType::F32 || mask.shape() != label_shape {
			return Err(Error::invalid_argument(format!(
				"{operation} mask has shape {:?} {}; expected {:?} F32",
				mask.shape(),
				mask.dtype().token(),
				label_shape
			)));
		}
		if !engine.same_as(mask.engine_handle()) {
			return Err(Error::invalid_argument(format!(
				"{operation} inputs must belong to the same engine"
			)));
		}
	}
	let validate_byte_range = |matrix: &Matrix, label: &str| -> Result<()> {
		let byte_size = matrix
			.num_elements()
			.checked_mul(matrix.dtype().size_bytes())
			.ok_or_else(|| {
				Error::invalid_argument(format!("{operation} {label} byte size overflows usize"))
			})?;
		u32::try_from(byte_size).map_err(|_| {
			Error::invalid_argument(format!(
				"{operation} {label} byte-address range exceeds the shader ABI"
			))
		})?;
		Ok(())
	};
	validate_byte_range(logits, "logits")?;
	validate_byte_range(labels, "labels")?;
	if let Some(mask) = mask {
		validate_byte_range(mask, "mask")?;
	}
	let rows = u32::try_from(labels.num_elements())
		.map_err(|_| Error::invalid_argument(format!("{operation} row count exceeds u32")))?;
	let classes = u32::try_from(classes)
		.map_err(|_| Error::invalid_argument(format!("{operation} class count exceeds u32")))?;
	let output = Matrix::allocate(engine, vec![1], 1, DType::U32)?;
	let push_constants = [
		PushConstant::U32(rows),
		PushConstant::U32(classes),
		PushConstant::U32(label_dtype),
	];
	let outputs = [&output];
	if let Some(mask) = mask {
		let buffers = [
			BufferBinding::read(logits.storage()),
			BufferBinding::read(labels.storage()),
			BufferBinding::read(mask.storage()),
			BufferBinding::write(output.storage()),
		];
		let inputs = [logits, labels, mask];
		engine.record_semantic(
			ComputeDispatch {
				kernel: KernelId::MatrixMaskedCategoricalAccuracyCountF32,
				buffers: &buffers,
				push_constants: &push_constants,
				workgroups: [1, 1, 1],
			},
			SemanticDispatch {
				contract,
				inputs: &inputs,
				outputs: &outputs,
				attributes: &[],
			},
		)?;
	} else {
		let buffers = [
			BufferBinding::read(logits.storage()),
			BufferBinding::read(labels.storage()),
			BufferBinding::write(output.storage()),
		];
		let inputs = [logits, labels];
		engine.record_semantic(
			ComputeDispatch {
				kernel: KernelId::MatrixCategoricalAccuracyCountF32,
				buffers: &buffers,
				push_constants: &push_constants,
				workgroups: [1, 1, 1],
			},
			SemanticDispatch {
				contract,
				inputs: &inputs,
				outputs: &outputs,
				attributes: &[],
			},
		)?;
	}
	Ok(output)
}

/// Count correct categorical predictions only where the FP32 mask is nonzero.
///
/// The returned `[1]` U32 Matrix remains device-resident. Reading it is an
/// explicit synchronization boundary.
///
/// # Errors
///
/// Returns an error under the same conditions as
/// [`categorical_accuracy_count`], or when the mask is not FP32, does not match
/// the labels shape, or belongs to another engine.
pub fn masked_categorical_accuracy_count(
	logits: &Matrix,
	labels: &Matrix,
	mask: &Matrix,
) -> Result<Matrix> {
	let contract = crate::core::operation::matrix::MASKED_CATEGORICAL_ACCURACY_COUNT;
	let mask = Some(mask);
	let operation = contract.name();
	if logits.dtype() != DType::F32 || logits.shape().len() < 2 || logits.num_elements() == 0 {
		return Err(Error::invalid_argument(format!(
			"{operation} requires nonempty rank-two-or-greater F32 logits"
		)));
	}
	let classes = *logits.shape().last().ok_or_else(|| {
		Error::invalid_argument(format!("{operation} logits must expose a class axis"))
	})?;
	let label_shape = &logits.shape()[..logits.shape().len() - 1];
	if labels.shape() != label_shape {
		return Err(Error::invalid_argument(format!(
			"{operation} labels have shape {:?}; expected {:?}",
			labels.shape(),
			label_shape
		)));
	}
	let label_dtype = match labels.dtype() {
		DType::U8 => 0,
		DType::U32 => 1,
		DType::I32 => 2,
		_ => {
			return Err(Error::invalid_argument(format!(
				"{operation} labels must use U8, U32, or I32; found {}",
				labels.dtype().token()
			)));
		}
	};
	let engine = logits.engine_handle();
	if !engine.same_as(labels.engine_handle()) {
		return Err(Error::invalid_argument(format!(
			"{operation} inputs must belong to the same engine"
		)));
	}
	if let Some(mask) = mask {
		if mask.dtype() != DType::F32 || mask.shape() != label_shape {
			return Err(Error::invalid_argument(format!(
				"{operation} mask has shape {:?} {}; expected {:?} F32",
				mask.shape(),
				mask.dtype().token(),
				label_shape
			)));
		}
		if !engine.same_as(mask.engine_handle()) {
			return Err(Error::invalid_argument(format!(
				"{operation} inputs must belong to the same engine"
			)));
		}
	}
	let validate_byte_range = |matrix: &Matrix, label: &str| -> Result<()> {
		let byte_size = matrix
			.num_elements()
			.checked_mul(matrix.dtype().size_bytes())
			.ok_or_else(|| {
				Error::invalid_argument(format!("{operation} {label} byte size overflows usize"))
			})?;
		u32::try_from(byte_size).map_err(|_| {
			Error::invalid_argument(format!(
				"{operation} {label} byte-address range exceeds the shader ABI"
			))
		})?;
		Ok(())
	};
	validate_byte_range(logits, "logits")?;
	validate_byte_range(labels, "labels")?;
	if let Some(mask) = mask {
		validate_byte_range(mask, "mask")?;
	}
	let rows = u32::try_from(labels.num_elements())
		.map_err(|_| Error::invalid_argument(format!("{operation} row count exceeds u32")))?;
	let classes = u32::try_from(classes)
		.map_err(|_| Error::invalid_argument(format!("{operation} class count exceeds u32")))?;
	let output = Matrix::allocate(engine, vec![1], 1, DType::U32)?;
	let push_constants = [
		PushConstant::U32(rows),
		PushConstant::U32(classes),
		PushConstant::U32(label_dtype),
	];
	let outputs = [&output];
	if let Some(mask) = mask {
		let buffers = [
			BufferBinding::read(logits.storage()),
			BufferBinding::read(labels.storage()),
			BufferBinding::read(mask.storage()),
			BufferBinding::write(output.storage()),
		];
		let inputs = [logits, labels, mask];
		engine.record_semantic(
			ComputeDispatch {
				kernel: KernelId::MatrixMaskedCategoricalAccuracyCountF32,
				buffers: &buffers,
				push_constants: &push_constants,
				workgroups: [1, 1, 1],
			},
			SemanticDispatch {
				contract,
				inputs: &inputs,
				outputs: &outputs,
				attributes: &[],
			},
		)?;
	} else {
		let buffers = [
			BufferBinding::read(logits.storage()),
			BufferBinding::read(labels.storage()),
			BufferBinding::write(output.storage()),
		];
		let inputs = [logits, labels];
		engine.record_semantic(
			ComputeDispatch {
				kernel: KernelId::MatrixCategoricalAccuracyCountF32,
				buffers: &buffers,
				push_constants: &push_constants,
				workgroups: [1, 1, 1],
			},
			SemanticDispatch {
				contract,
				inputs: &inputs,
				outputs: &outputs,
				attributes: &[],
			},
		)?;
	}
	Ok(output)
}