ort 2.0.0-rc.15

A safe Rust wrapper for ONNX Runtime 1.31 - Optimize and accelerate machine learning inference & training
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
use alloc::sync::Arc;
use core::sync::atomic::{AtomicBool, Ordering};

use crate::{
	Result,
	logging::LogLevel,
	operator::{BoxedKernel, ComputeContext, Kernel, KernelContext, Operator, OperatorDomain, OperatorInput, OperatorOutput},
	session::Session,
	test_util::test_env,
	value::{Tensor, TensorElementType}
};

struct CustomOpOne;

impl Operator for CustomOpOne {
	type Kernel<'attr> = BoxedKernel<'attr>;

	const INPLACES: &[(u32, u32)] = &[(0, 0), (1, 0)];

	fn name(&self) -> &str {
		"CustomOpOne"
	}

	fn inputs(&self) -> impl IntoIterator<Item = OperatorInput> {
		[OperatorInput::required(TensorElementType::Float32), OperatorInput::required(TensorElementType::Float32)]
	}

	fn outputs(&self) -> impl IntoIterator<Item = OperatorOutput> {
		[OperatorOutput::required(TensorElementType::Float32)]
	}

	fn create_kernel<'attr>(&self, _: &KernelContext<'attr>) -> Result<Self::Kernel<'attr>> {
		Ok(Box::new(|ctx: &ComputeContext| {
			let logger = ctx.logger()?;
			crate::log!(logger, Warning @ "$sentinel1$");

			let x = ctx.input(0)?.ok_or_else(|| crate::Error::new("missing input"))?;
			let y = ctx.input(1)?.ok_or_else(|| crate::Error::new("missing input"))?;
			let (x_shape, x) = x.try_extract_tensor::<f32>()?;
			let (y_shape, y) = y.try_extract_tensor::<f32>()?;

			let mut z = ctx.output(0, x_shape.to_vec())?.ok_or_else(|| crate::Error::new("missing input"))?;
			let (_, z_ref) = z.try_extract_tensor_mut::<f32>()?;
			for i in 0..y_shape.iter().copied().reduce(|acc, e| acc * e).unwrap_or(0) as usize {
				if i % 2 == 0 {
					z_ref[i] = x[i];
				} else {
					z_ref[i] = y[i];
				}
			}
			Ok(())
		}))
	}
}

struct CustomOpTwo;

impl Operator for CustomOpTwo {
	type Kernel<'attr> = BoxedKernel<'attr>;

	const INPLACES: &[(u32, u32)] = &[(0, 0)];

	fn name(&self) -> &str {
		"CustomOpTwo"
	}

	fn inputs(&self) -> impl IntoIterator<Item = OperatorInput> {
		[OperatorInput::required(TensorElementType::Float32)]
	}

	fn outputs(&self) -> impl IntoIterator<Item = OperatorOutput> {
		[OperatorOutput::required(TensorElementType::Int32)]
	}

	fn create_kernel<'attr>(&self, _: &KernelContext<'attr>) -> crate::Result<Self::Kernel<'attr>> {
		Ok(Box::new(|ctx: &ComputeContext| {
			let logger = ctx.logger()?;
			crate::log!(logger, Verbose @ "$sentinel2$");

			let x = ctx.input(0)?.ok_or_else(|| crate::Error::new("missing input"))?;
			let (x_shape, x) = x.try_extract_tensor::<f32>()?;
			let mut z = ctx.output(0, x_shape.to_vec())?.ok_or_else(|| crate::Error::new("missing input"))?;
			let (_, z_ref) = z.try_extract_tensor_mut::<i32>()?;
			for i in 0..x_shape.iter().copied().reduce(|acc, e| acc * e).unwrap_or(0) as usize {
				z_ref[i] = (x[i] * i as f32) as i32;
			}
			Ok(())
		}))
	}
}

#[test]
fn test_custom_ops() -> crate::Result<()> {
	let logged_values = Arc::new((AtomicBool::new(false), AtomicBool::new(false)));
	let model = std::fs::read("tests/data/custom_op_test.onnx").expect("");
	let mut session = Session::builder(test_env())?
		.with_operators(OperatorDomain::new("test.customop")?.add(CustomOpOne)?.add(CustomOpTwo)?)?
		.with_logger(Arc::new({
			let logged_values = logged_values.clone();
			move |_level: LogLevel, _category: &str, _id: &str, _code_location: &str, message: &str| match message {
				"$sentinel1$" => logged_values.0.store(true, Ordering::Release),
				"$sentinel2$" => logged_values.1.store(true, Ordering::Release),
				_ => {}
			}
		}))?
		.commit_from_memory(&model)?;

	let allocator = session.allocator();
	let mut value1 = Tensor::<f32>::new(allocator, [3_usize, 5])?;
	{
		let (_, data) = value1.extract_tensor_mut();
		for datum in data {
			*datum = 0.;
		}
	}
	let mut value2 = Tensor::<f32>::new(allocator, [3_usize, 5])?;
	{
		let (_, data) = value2.extract_tensor_mut();
		for datum in data {
			*datum = 1.;
		}
	}
	let values = session.run(crate::inputs![&value1, &value2])?;
	assert_eq!(values[0].try_extract_tensor::<i32>()?.1, [0, 1, 0, 3, 0, 5, 0, 7, 0, 9, 0, 11, 0, 13, 0]);

	assert!(logged_values.0.load(Ordering::Acquire));
	assert!(logged_values.1.load(Ordering::Acquire));

	Ok(())
}

struct AttrTesterIntFloat;

impl Operator for AttrTesterIntFloat {
	type Kernel<'attr> = BoxedKernel<'attr>;

	const INPLACES: &[(u32, u32)] = &[(0, 0)];

	fn name(&self) -> &str {
		"AttrTesterIntFloat"
	}

	fn inputs(&self) -> impl IntoIterator<Item = OperatorInput> {
		[OperatorInput::required(TensorElementType::Float32)]
	}

	fn outputs(&self) -> impl IntoIterator<Item = OperatorOutput> {
		[OperatorOutput::required(TensorElementType::Float32)]
	}

	fn infer_shape(&self, ctx: &mut super::ShapeInferenceContext) -> crate::Result<()> {
		assert!(matches!(ctx.attr("a_int"), Ok(1_i64)));
		assert!(matches!(ctx.attr("a_float"), Ok(2.0_f32)));
		assert!(matches!(ctx.attr::<Vec<i64>>("ints").as_deref(), Ok(&[3, 4, 5])));
		assert!(matches!(ctx.attr::<Vec<f32>>("floats").as_deref(), Ok(&[6., 7., 8.])));

		ctx.set_output(0, &ctx.inputs()[0])?;

		Ok(())
	}

	fn create_kernel<'attr>(&self, _: &KernelContext<'attr>) -> crate::Result<Self::Kernel<'attr>> {
		Ok(Box::new(|ctx: &ComputeContext| {
			let x = ctx.input(0)?.ok_or_else(|| crate::Error::new("missing input"))?;
			let (x_shape, x) = x.try_extract_tensor::<f32>()?;
			let mut z = ctx.output(0, x_shape.to_vec())?.ok_or_else(|| crate::Error::new("missing input"))?;
			let (_, z_ref) = z.try_extract_tensor_mut::<f32>()?;
			for i in 0..x_shape.iter().copied().reduce(|acc, e| acc * e).unwrap_or(0) as usize {
				z_ref[i] = x[i] * 2.;
			}
			Ok(())
		}))
	}
}

struct AttrTesterString;

impl Operator for AttrTesterString {
	type Kernel<'attr> = BoxedKernel<'attr>;

	const INPLACES: &[(u32, u32)] = &[(0, 0)];

	fn name(&self) -> &str {
		"AttrTesterString"
	}

	fn inputs(&self) -> impl IntoIterator<Item = OperatorInput> {
		[OperatorInput::required(TensorElementType::Float32)]
	}

	fn outputs(&self) -> impl IntoIterator<Item = OperatorOutput> {
		[OperatorOutput::required(TensorElementType::Float32)]
	}

	fn infer_shape(&self, ctx: &mut super::ShapeInferenceContext) -> crate::Result<()> {
		assert!(matches!(ctx.attr::<String>("a_string").as_deref(), Ok("iamastring")));

		ctx.set_output(0, &ctx.inputs()[0])?;

		Ok(())
	}

	fn create_kernel<'attr>(&self, ctx: &KernelContext<'attr>) -> crate::Result<Self::Kernel<'attr>> {
		#[cfg(feature = "api-25")]
		assert_eq!(ctx.get::<Vec<String>>("strings"), Some(vec![String::from("more"), String::from("strings")]));

		Ok(Box::new(|ctx: &ComputeContext| {
			let x = ctx.input(0)?.ok_or_else(|| crate::Error::new("missing input"))?;
			let (x_shape, x) = x.try_extract_tensor::<f32>()?;
			let mut z = ctx.output(0, x_shape.to_vec())?.ok_or_else(|| crate::Error::new("missing input"))?;
			let (_, z_ref) = z.try_extract_tensor_mut::<f32>()?;
			for i in 0..x_shape.iter().copied().reduce(|acc, e| acc * e).unwrap_or(0) as usize {
				z_ref[i] = x[i] * 3.;
			}
			Ok(())
		}))
	}
}

#[test]
fn test_op_attrs() -> crate::Result<()> {
	let mut session = Session::builder(test_env())?
		.with_operators(OperatorDomain::new("test.customop")?.add(AttrTesterIntFloat)?.add(AttrTesterString)?)?
		.commit_from_file("tests/data/attr_tester.onnx")?;

	let value1 = Tensor::from_array(([5], vec![1.0_f32, 2.0, 3.0, 4.0, 5.0]))?;

	let values = session.run(crate::inputs!["input_0" => &value1])?;
	assert_eq!(values[0].try_extract_tensor::<f32>()?.1, [6.0, 12.0, 18.0, 24.0, 30.0]);

	Ok(())
}

struct CopyTensorArrayAllVariadic;

impl Operator for CopyTensorArrayAllVariadic {
	type Kernel<'attr> = CopyVariadic;

	fn name(&self) -> &str {
		"CopyTensorArrayAllVariadic"
	}

	fn inputs(&self) -> impl IntoIterator<Item = OperatorInput> {
		[OperatorInput::variadic(1).homogenous(TensorElementType::Float32)]
	}

	fn outputs(&self) -> impl IntoIterator<Item = OperatorOutput> {
		[OperatorOutput::variadic(1).homogenous(TensorElementType::Float32)]
	}

	fn infer_shape(&self, ctx: &mut super::ShapeInferenceContext) -> crate::Result<()> {
		for (i, input) in ctx.inputs().into_iter().enumerate() {
			ctx.set_output(i, &input)?;
		}

		Ok(())
	}

	fn create_kernel<'attr>(&self, _: &KernelContext<'attr>) -> crate::Result<Self::Kernel<'attr>> {
		Ok(CopyVariadic { start: 0 })
	}
}

struct CopyVariadic {
	start: usize
}

impl Kernel for CopyVariadic {
	fn compute(&mut self, ctx: &ComputeContext) -> crate::Result<()> {
		for i in self.start..ctx.num_inputs()? {
			let input = ctx.input(i)?.ok_or_else(|| crate::Error::new("missing input"))?;
			let mut output = ctx
				.output(i, input.shape().clone())?
				.ok_or_else(|| crate::Error::new(format!("failed to allocate output {i}")))?;

			output.try_extract_tensor_mut::<f32>()?.1.copy_from_slice(input.try_extract_tensor()?.1);
		}
		Ok(())
	}
}

struct CopyTensorArrayCombined;

impl Operator for CopyTensorArrayCombined {
	type Kernel<'attr> = BoxedKernel<'attr>;

	fn name(&self) -> &str {
		"CopyTensorArrayCombined"
	}

	fn inputs(&self) -> impl IntoIterator<Item = OperatorInput> {
		vec![OperatorInput::optional(TensorElementType::Float32), OperatorInput::variadic(0).homogenous(TensorElementType::Float32)]
	}

	fn outputs(&self) -> impl IntoIterator<Item = OperatorOutput> {
		vec![OperatorOutput::optional(TensorElementType::Float32), OperatorOutput::variadic(0).homogenous(TensorElementType::Float32)]
	}

	fn infer_shape(&self, ctx: &mut super::ShapeInferenceContext) -> crate::Result<()> {
		for (i, input) in ctx.inputs().into_iter().enumerate() {
			ctx.set_output(i, &input)?;
		}

		Ok(())
	}

	fn create_kernel<'attr>(&self, _info: &KernelContext<'attr>) -> crate::Result<Self::Kernel<'attr>> {
		Ok(Box::new(move |ctx: &ComputeContext| {
			if let Ok(Some(input)) = ctx.input(0) {
				let mut output = ctx
					.output(0, input.shape().clone())?
					.ok_or_else(|| crate::Error::new("failed to allocate output 0"))?;

				output.try_extract_tensor_mut::<f32>()?.1.copy_from_slice(input.try_extract_tensor()?.1);
			}

			CopyVariadic { start: 1 }.compute(ctx)
		}))
	}
}

#[test]
fn test_variadic_io() -> crate::Result<()> {
	let ops = Arc::new(
		OperatorDomain::new("test.customop")?
			.add(CopyTensorArrayAllVariadic)?
			.add(CopyTensorArrayCombined)?
	);
	let env = test_env().clone();

	let mut session = Session::builder(&env)?
		.with_operators(Arc::clone(&ops))?
		.commit_from_file("tests/data/copy_2_inputs_2_outputs.onnx")?;

	let input0 = Tensor::from_array(([15], vec![1.1_f32, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.0, 11.1, 12.2, 13.3, 14.4, 15.5]))?;
	let input1 = Tensor::from_array(([15], vec![15.5_f32, 14.4, 13.3, 12.2, 11.1, 10.0, 9.9, 8.8, 7.7, 6.6, 5.5, 4.4, 3.3, 2.2, 1.1]))?;

	let values = session.run(crate::inputs!["input_0" => &input0, "input_1" => &input1])?;
	assert_eq!(values[0].try_extract_tensor::<f32>()?.1, input0.extract_tensor().1);
	assert_eq!(values[1].try_extract_tensor::<f32>()?.1, input1.extract_tensor().1);

	let mut session = Session::builder(&env)?
		.with_operators(Arc::clone(&ops))?
		.commit_from_file("tests/data/copy_3_inputs_3_outputs.onnx")?;

	let input2 = Tensor::from_array(([15], vec![6.6_f32, 7.7, 8.8, 9.9, 10.0, 1.1, 2.2, 3.3, 4.4, 5.5, 11.1, 12.2, 13.3, 14.4, 15.5]))?;
	let values = session.run(crate::inputs![
		"input_0" => &input0,
		"input_1" => &input1,
		"input_2" => &input2
	])?;
	assert_eq!(values[0].try_extract_tensor::<f32>()?.1, input0.extract_tensor().1);
	assert_eq!(values[1].try_extract_tensor::<f32>()?.1, input1.extract_tensor().1);
	assert_eq!(values[2].try_extract_tensor::<f32>()?.1, input2.extract_tensor().1);

	Ok(())
}

/// Stands in for `CustomOpTwo`, recording the shape of the preallocated output it sees on each run.
#[cfg(feature = "api-30")]
struct PreallocatedOutputOp(Arc<crate::util::Mutex<alloc::vec::Vec<Option<alloc::vec::Vec<i64>>>>>);

#[cfg(feature = "api-30")]
impl Operator for PreallocatedOutputOp {
	type Kernel<'attr> = BoxedKernel<'attr>;

	fn name(&self) -> &str {
		"CustomOpTwo"
	}

	fn inputs(&self) -> impl IntoIterator<Item = OperatorInput> {
		[OperatorInput::required(TensorElementType::Float32)]
	}

	fn outputs(&self) -> impl IntoIterator<Item = OperatorOutput> {
		[OperatorOutput::required(TensorElementType::Int32)]
	}

	fn create_kernel<'attr>(&self, _: &KernelContext<'attr>) -> crate::Result<Self::Kernel<'attr>> {
		let seen = Arc::clone(&self.0);
		Ok(Box::new(move |ctx: &ComputeContext| {
			let x = ctx.input(0)?.ok_or_else(|| crate::Error::new("missing input"))?;
			let (x_shape, _) = x.try_extract_tensor::<f32>()?;

			let preallocated = ctx.preallocated_output(0)?;
			seen.lock().push(preallocated.as_ref().map(|v| v.shape().to_vec()));

			let mut z = match preallocated {
				Some(z) => {
					assert_eq!(z.shape(), x_shape);
					z
				}
				None => ctx.output(0, x_shape.to_vec())?.ok_or_else(|| crate::Error::new("missing output"))?
			};
			let (_, z_ref) = z.try_extract_tensor_mut::<i32>()?;
			for (i, z) in z_ref.iter_mut().enumerate() {
				*z = i as i32 * 10;
			}
			Ok(())
		}))
	}
}

#[test]
#[cfg(feature = "api-30")]
fn test_preallocated_output() -> crate::Result<()> {
	use crate::{
		memory::Allocator,
		session::{OutputSelector, RunOptions}
	};

	let seen = Arc::new(crate::util::Mutex::new(alloc::vec::Vec::new()));
	let mut session = Session::builder(test_env())?
		.with_operators(
			OperatorDomain::new("test.customop")?
				.add(CustomOpOne)?
				.add(PreallocatedOutputOp(Arc::clone(&seen)))?
		)?
		.commit_from_file("tests/data/custom_op_test.onnx")?;

	let x = Tensor::from_array(([3_usize, 5], alloc::vec![0.0_f32; 15]))?;
	let y = Tensor::from_array(([3_usize, 5], alloc::vec![1.0_f32; 15]))?;
	let expected: alloc::vec::Vec<i32> = (0..15).map(|i| i * 10).collect();

	// without a preallocated output, the kernel has to allocate one
	let outputs = session.run(crate::inputs![&x, &y])?;
	assert_eq!(outputs[0].try_extract_tensor::<i32>()?.1, expected);
	drop(outputs);

	// with a preallocated output, the kernel writes straight into it
	let output_name = session.outputs()[0].name().to_string();
	let preallocated = Tensor::<i32>::new(&Allocator::default(), [3_usize, 5])?;
	let preallocated_ptr = preallocated.data_ptr();
	let options = RunOptions::new()?.with_outputs(OutputSelector::default().preallocate(output_name, preallocated));
	let outputs = session.run_with_options(crate::inputs![&x, &y], &options)?;
	assert_eq!(outputs[0].try_extract_tensor::<i32>()?.1, expected);
	assert_eq!(outputs[0].view().downcast::<crate::value::DynTensorValueType>()?.data_ptr(), preallocated_ptr);

	assert_eq!(*seen.lock(), [None, Some(alloc::vec![3, 5])]);
	Ok(())
}