oarust 0.18.0

GPU-first semantic computing runtime written in Rust
Documentation
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
//! ML-owned Matrix unfold/fold (im2col/col2im) operations.

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

use crate::ml::autograd;

use super::super::validation::{shader_u32, validate_f32_same_runtime};

#[derive(Clone, Copy)]
struct FoldGeometry {
	batch_size: u32,
	input_channels: u32,
	input_height: u32,
	input_width: u32,
	kernel_size: u32,
	stride: u32,
	padding: u32,
	dilation: u32,
	output_height: u32,
	output_width: u32,
}

impl FoldGeometry {
	fn sliding_extent(
		input: usize,
		kernel_size: usize,
		stride: usize,
		padding: usize,
		dilation: usize,
		operation: &'static str,
	) -> Result<usize> {
		let padded = input
			.checked_add(padding.checked_mul(2).ok_or_else(|| {
				Error::invalid_argument(format!("{operation} doubled padding overflows usize"))
			})?)
			.ok_or_else(|| {
				Error::invalid_argument(format!("{operation} padded extent overflows usize"))
			})?;
		let effective_kernel = dilation
			.checked_mul(kernel_size.saturating_sub(1))
			.and_then(|span| span.checked_add(1))
			.ok_or_else(|| {
				Error::invalid_argument(format!("{operation} effective kernel overflows usize"))
			})?;
		if padded < effective_kernel {
			return Err(Error::invalid_argument(format!(
				"{operation} effective kernel does not fit the padded input"
			)));
		}
		Ok((padded - effective_kernel) / stride + 1)
	}

	fn resolve(
		input_channels: usize,
		input_height: usize,
		input_width: usize,
		geometry: [usize; 4],
		batch_size: usize,
		operation: &'static str,
	) -> Result<Self> {
		let [kernel_size, stride, padding, dilation] = geometry;
		if input_channels == 0
			|| input_height == 0
			|| input_width == 0
			|| kernel_size == 0
			|| stride == 0
			|| dilation == 0
			|| batch_size == 0
		{
			return Err(Error::invalid_argument(format!(
				"{operation} requires nonzero batch, channels, spatial extents, kernel, stride, and dilation"
			)));
		}
		let output_height = Self::sliding_extent(
			input_height,
			kernel_size,
			stride,
			padding,
			dilation,
			operation,
		)?;
		let output_width = Self::sliding_extent(
			input_width,
			kernel_size,
			stride,
			padding,
			dilation,
			operation,
		)?;
		for (label, value) in [
			("input height", input_height),
			("input width", input_width),
			("output height", output_height),
			("output width", output_width),
			("kernel size", kernel_size),
			("stride", stride),
			("padding", padding),
			("dilation", dilation),
		] {
			if value > i32::MAX as usize {
				return Err(Error::invalid_argument(format!(
					"{operation} {label} exceeds the signed shader coordinate ABI"
				)));
			}
		}
		Ok(Self {
			batch_size: shader_u32(batch_size, "batch size", operation)?,
			input_channels: shader_u32(input_channels, "input channel count", operation)?,
			input_height: shader_u32(input_height, "input height", operation)?,
			input_width: shader_u32(input_width, "input width", operation)?,
			kernel_size: shader_u32(kernel_size, "kernel size", operation)?,
			stride: shader_u32(stride, "stride", operation)?,
			padding: shader_u32(padding, "padding", operation)?,
			dilation: shader_u32(dilation, "dilation", operation)?,
			output_height: shader_u32(output_height, "output height", operation)?,
			output_width: shader_u32(output_width, "output width", operation)?,
		})
	}

	fn output_length(self) -> usize {
		self.output_height as usize * self.output_width as usize
	}

	fn dimensions(self) -> [PushConstant; 10] {
		[
			PushConstant::U32(self.batch_size),
			PushConstant::U32(self.input_channels),
			PushConstant::U32(self.input_height),
			PushConstant::U32(self.input_width),
			PushConstant::U32(self.kernel_size),
			PushConstant::U32(self.stride),
			PushConstant::U32(self.padding),
			PushConstant::U32(self.dilation),
			PushConstant::U32(self.output_height),
			PushConstant::U32(self.output_width),
		]
	}

	fn unfold_attributes(self) -> [OpAttribute; 4] {
		[
			OpAttribute::UnsignedInteger {
				name: "kernel_size".into(),
				value: u64::from(self.kernel_size),
			},
			OpAttribute::UnsignedInteger {
				name: "stride".into(),
				value: u64::from(self.stride),
			},
			OpAttribute::UnsignedInteger {
				name: "padding".into(),
				value: u64::from(self.padding),
			},
			OpAttribute::UnsignedInteger {
				name: "dilation".into(),
				value: u64::from(self.dilation),
			},
		]
	}

	fn fold_attributes(self) -> [OpAttribute; 6] {
		[
			OpAttribute::UnsignedInteger {
				name: "output_height".into(),
				value: u64::from(self.input_height),
			},
			OpAttribute::UnsignedInteger {
				name: "output_width".into(),
				value: u64::from(self.input_width),
			},
			OpAttribute::UnsignedInteger {
				name: "kernel_size".into(),
				value: u64::from(self.kernel_size),
			},
			OpAttribute::UnsignedInteger {
				name: "stride".into(),
				value: u64::from(self.stride),
			},
			OpAttribute::UnsignedInteger {
				name: "padding".into(),
				value: u64::from(self.padding),
			},
			OpAttribute::UnsignedInteger {
				name: "dilation".into(),
				value: u64::from(self.dilation),
			},
		]
	}
}

/// Extract sliding NCHW kernel windows into a `[N, C * kernel², L]` column Matrix.
///
/// Each column holds one `kernel_size Ɨ kernel_size` patch per input channel in
/// `(channel, kernel_y, kernel_x)` order, laid out over the
/// `L = output_height * output_width` sliding positions where
/// `output = floor((input + 2 * padding - dilation * (kernel - 1) - 1) / stride) + 1`
/// on every spatial axis. Out-of-bounds taps read as zero, matching the donor
/// im2col contract.
///
/// # Errors
///
/// Returns an error unless `input` is a nonempty NCHW rank-four F32 Matrix,
/// kernel/stride/dilation are nonzero, the effective kernel fits the padded
/// input, every extent fits the shader ABI, or recording fails.
pub fn unfold(
	input: &Matrix,
	kernel_size: usize,
	stride: usize,
	padding: usize,
	dilation: usize,
) -> Result<Matrix> {
	validate_f32_same_runtime(crate::core::operation::ml::UNFOLD.name(), &[input])?;
	let output = unfold_dispatch(input, kernel_size, stride, padding, dilation)?;
	autograd::record_unfold(input, kernel_size, stride, padding, dilation, &output)?;
	Ok(output)
}

/// Combine `[N, C * kernel², L]` sliding columns into an overlapping NCHW Matrix.
///
/// This is the deterministic adjoint of [`unfold`]: overlapping taps are summed
/// per output element without atomics. `output_height` and `output_width` name
/// the reconstructed image extent and must reproduce exactly
/// `L = output_height_positions * output_width_positions` sliding columns under
/// the same kernel geometry.
///
/// # Errors
///
/// Returns an error unless `input` is a nonempty `[N, C * kernel², L]`
/// rank-three F32 Matrix whose channel count is divisible by `kernel²` and
/// whose column count equals the sliding extent of the requested output image,
/// kernel/stride/dilation are nonzero, extents fit the shader ABI, or
/// recording fails.
pub fn fold(
	input: &Matrix,
	output_height: usize,
	output_width: usize,
	kernel_size: usize,
	stride: usize,
	padding: usize,
	dilation: usize,
) -> Result<Matrix> {
	let columns = fold_dispatch(
		input,
		output_height,
		output_width,
		kernel_size,
		stride,
		padding,
		dilation,
	)?;
	autograd::record_fold(input, kernel_size, stride, padding, dilation, &columns)?;
	Ok(columns)
}

pub(in crate::ml) fn unfold_backward(
	input: &Matrix,
	output_gradient: &Matrix,
	kernel_size: usize,
	stride: usize,
	padding: usize,
	dilation: usize,
) -> Result<Matrix> {
	let operation = crate::core::operation::ml::FOLD.name();
	validate_f32_same_runtime(operation, &[input, output_gradient])?;
	let [_, _, input_height, input_width] = input.shape() else {
		return Err(Error::invalid_argument(format!(
			"{operation} adjoint requires NCHW rank-four saved input; found {:?}",
			input.shape()
		)));
	};
	let output = fold_dispatch(
		output_gradient,
		*input_height,
		*input_width,
		kernel_size,
		stride,
		padding,
		dilation,
	)?;
	if output.shape() != input.shape() {
		return Err(Error::invalid_argument(format!(
			"{operation} reconstructed adjoint shape {:?} disagrees with saved input {:?}",
			output.shape(),
			input.shape()
		)));
	}
	Ok(output)
}

pub(in crate::ml) fn fold_backward(
	input: &Matrix,
	output_gradient: &Matrix,
	kernel_size: usize,
	stride: usize,
	padding: usize,
	dilation: usize,
) -> Result<Matrix> {
	let operation = crate::core::operation::ml::UNFOLD.name();
	validate_f32_same_runtime(operation, &[input, output_gradient])?;
	if output_gradient.shape().len() != 4 {
		return Err(Error::invalid_argument(format!(
			"{operation} output gradient must be NCHW rank four; found {:?}",
			output_gradient.shape()
		)));
	}
	let columns = unfold_dispatch(output_gradient, kernel_size, stride, padding, dilation)?;
	if columns.shape() != input.shape() {
		return Err(Error::invalid_argument(format!(
			"{operation} reconstructed column shape {:?} disagrees with saved input {:?}",
			columns.shape(),
			input.shape()
		)));
	}
	Ok(columns)
}

fn unfold_dispatch(
	input: &Matrix,
	kernel_size: usize,
	stride: usize,
	padding: usize,
	dilation: usize,
) -> Result<Matrix> {
	let contract = crate::core::operation::ml::UNFOLD;
	let operation = contract.name();
	let [batch_size, input_channels, input_height, input_width] = input.shape() else {
		return Err(Error::invalid_argument(format!(
			"{operation} requires NCHW rank-four input; found {:?}",
			input.shape()
		)));
	};
	let geometry = FoldGeometry::resolve(
		*input_channels,
		*input_height,
		*input_width,
		[kernel_size, stride, padding, dilation],
		*batch_size,
		operation,
	)?;
	let column_size = input_channels
		.checked_mul(kernel_size)
		.and_then(|count| count.checked_mul(kernel_size))
		.ok_or_else(|| Error::invalid_argument(format!("{operation} column size overflows usize")))?;
	let element_count = batch_size
		.checked_mul(column_size)
		.and_then(|count| count.checked_mul(geometry.output_length()))
		.ok_or_else(|| {
			Error::invalid_argument(format!("{operation} column element count overflows usize"))
		})?;
	let output = Matrix::allocate(
		input.runtime(),
		vec![*batch_size, column_size, geometry.output_length()],
		element_count,
		DType::F32,
	)?;
	let buffers = [
		BufferBinding::read(input.storage()),
		BufferBinding::write(output.storage()),
	];
	let constants = geometry.dimensions();
	let attributes = geometry.unfold_attributes();
	let kernel = KernelId::MlUnfold2dF32;
	{
		let inputs: &[&Matrix] = &[input];
		let outputs: &[&Matrix] = &[&output];
		let attributes: &[OpAttribute] = &attributes;
		let dispatch = ComputeDispatch {
			kernel,
			buffers: &buffers,
			push_constants: &constants,
			workgroups: kernel.linear_workgroups(shader_u32(
				element_count,
				"column element count",
				operation,
			)?),
			indirect: None,
		};
		input.runtime().record_semantic(
			dispatch,
			SemanticDispatch {
				contract,
				inputs,
				outputs,
				attributes,
			},
		)?;
	}
	Ok(output)
}

fn fold_dispatch(
	input: &Matrix,
	output_height: usize,
	output_width: usize,
	kernel_size: usize,
	stride: usize,
	padding: usize,
	dilation: usize,
) -> Result<Matrix> {
	let contract = crate::core::operation::ml::FOLD;
	let operation = contract.name();
	validate_f32_same_runtime(operation, &[input])?;
	let [batch_size, column_size, output_length] = input.shape() else {
		return Err(Error::invalid_argument(format!(
			"{operation} requires [N, C * kernel², L] rank-three input; found {:?}",
			input.shape()
		)));
	};
	let kernel_area = kernel_size
		.checked_mul(kernel_size)
		.ok_or_else(|| Error::invalid_argument(format!("{operation} kernel area overflows usize")))?;
	if kernel_area == 0 || !column_size.is_multiple_of(kernel_area) {
		return Err(Error::invalid_argument(format!(
			"{operation} column channels must be a nonzero multiple of kernel²; found {column_size} and kernel {kernel_size}"
		)));
	}
	let input_channels = column_size / kernel_area;
	let geometry = FoldGeometry::resolve(
		input_channels,
		output_height,
		output_width,
		[kernel_size, stride, padding, dilation],
		*batch_size,
		operation,
	)?;
	if *output_length != geometry.output_length() {
		return Err(Error::invalid_argument(format!(
			"{operation} column count {output_length} must equal the sliding extent {}",
			geometry.output_length()
		)));
	}
	let element_count = batch_size
		.checked_mul(input_channels)
		.and_then(|count| count.checked_mul(output_height))
		.and_then(|count| count.checked_mul(output_width))
		.ok_or_else(|| {
			Error::invalid_argument(format!("{operation} image element count overflows usize"))
		})?;
	let output = Matrix::allocate(
		input.runtime(),
		vec![*batch_size, input_channels, output_height, output_width],
		element_count,
		DType::F32,
	)?;
	let buffers = [
		BufferBinding::read(input.storage()),
		BufferBinding::write(output.storage()),
	];
	let constants = geometry.dimensions();
	let attributes = geometry.fold_attributes();
	let kernel = KernelId::MlFold2dF32;
	{
		let inputs: &[&Matrix] = &[input];
		let outputs: &[&Matrix] = &[&output];
		let attributes: &[OpAttribute] = &attributes;
		let dispatch = ComputeDispatch {
			kernel,
			buffers: &buffers,
			push_constants: &constants,
			workgroups: kernel.linear_workgroups(shader_u32(
				element_count,
				"image element count",
				operation,
			)?),
			indirect: None,
		};
		input.runtime().record_semantic(
			dispatch,
			SemanticDispatch {
				contract,
				inputs,
				outputs,
				attributes,
			},
		)?;
	}
	Ok(output)
}