oxionnx 0.1.3

Pure Rust ONNX inference engine — zero C/C++ dependencies
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
//! Tests for IoBinding and Session::run_with_binding.

use oxionnx::{IoBinding, Session, Tensor};
use oxionnx_core::{Attributes, Graph, Node, OpKind};
use std::collections::HashMap;

fn make_identity_session() -> Session {
    let graph = Graph {
        nodes: vec![Node {
            op: OpKind::Identity,
            name: "id".to_string(),
            inputs: vec!["x".to_string()],
            outputs: vec!["y".to_string()],
            attrs: Attributes::default(),
        }],
        input_names: vec!["x".to_string()],
        output_names: vec!["y".to_string()],
        input_infos: vec![],
        output_infos: vec![],
        name: String::new(),
    };
    Session::from_graph(graph, HashMap::new()).expect("session creation should succeed")
}

#[test]
fn test_io_binding_basic() {
    let session = make_identity_session();
    let mut binding = IoBinding::new();

    let data = vec![1.0f32, 2.0, 3.0, 4.0];
    let shape = vec![2, 2];
    binding.bind_input("x", Tensor::new(data.clone(), shape.clone()));

    session
        .run_with_binding(&mut binding)
        .expect("run_with_binding should succeed");

    let out = binding
        .get_output("y")
        .expect("output 'y' should be present");
    assert_eq!(out.shape, shape);
    assert_eq!(out.data, data);
}

#[test]
fn test_io_binding_output_reuse() {
    let session = make_identity_session();
    let mut binding = IoBinding::new();

    let data1 = vec![1.0f32, 2.0, 3.0, 4.0];
    let shape = vec![2, 2];

    // First run
    binding.bind_input("x", Tensor::new(data1.clone(), shape.clone()));
    session
        .run_with_binding(&mut binding)
        .expect("first run_with_binding should succeed");

    let out1 = binding.get_output("y").expect("output 'y' after first run");
    assert_eq!(out1.data, data1);
    assert_eq!(out1.shape, shape);

    // Second run with same shapes — output buffer should be reused
    let data2 = vec![5.0f32, 6.0, 7.0, 8.0];
    binding.clear_inputs();
    binding.bind_input("x", Tensor::new(data2.clone(), shape.clone()));
    session
        .run_with_binding(&mut binding)
        .expect("second run_with_binding should succeed");

    let out2 = binding
        .get_output("y")
        .expect("output 'y' after second run");
    assert_eq!(out2.data, data2, "output should reflect new input values");
    assert_eq!(out2.shape, shape);
}

#[test]
fn test_io_binding_clear_inputs() {
    let session = make_identity_session();
    let mut binding = IoBinding::new();

    let data1 = vec![10.0f32, 20.0, 30.0];
    let shape = vec![3];

    // First run
    binding.bind_input("x", Tensor::new(data1.clone(), shape.clone()));
    session
        .run_with_binding(&mut binding)
        .expect("first run_with_binding should succeed");

    let out1 = binding.get_output("y").expect("output 'y' after first run");
    assert_eq!(out1.data, data1);

    // clear_inputs then rebind with different values
    binding.clear_inputs();

    // After clear_inputs, input_names should be empty
    assert_eq!(binding.input_names().count(), 0);

    let data2 = vec![100.0f32, 200.0, 300.0];
    binding.bind_input("x", Tensor::new(data2.clone(), shape.clone()));
    session
        .run_with_binding(&mut binding)
        .expect("second run_with_binding should succeed");

    let out2 = binding
        .get_output("y")
        .expect("output 'y' after second run");
    assert_eq!(
        out2.data, data2,
        "output should have changed after rebinding input"
    );
    assert_ne!(out2.data, data1, "output should not be the old values");
}

// ── New Phase F / IoBinding tests ────────────────────────────────────────────

/// Pre-binding an output buffer with correct shape causes run_with_binding to
/// copy data in-place. The output pointer address is stable (no realloc).
#[test]
fn test_bind_output_prealloc_copy_in_place() {
    let session = make_identity_session();
    let mut binding = IoBinding::new();

    let shape = vec![4];
    let initial_buf = vec![0.0f32; 4];

    // Pre-bind an output buffer of the exact shape
    binding.bind_output("y", Tensor::new(initial_buf.clone(), shape.clone()));

    let input_data = vec![7.0f32, 8.0, 9.0, 10.0];
    binding.bind_input("x", Tensor::new(input_data.clone(), shape.clone()));

    session
        .run_with_binding(&mut binding)
        .expect("run_with_binding with pre-bound output");

    let out = binding
        .get_output("y")
        .expect("output 'y' should be present");
    assert_eq!(
        out.data, input_data,
        "pre-bound buffer should have input data"
    );
    assert_eq!(out.shape, shape);
}

/// When bind_output provides a buffer with the wrong shape, run_with_binding
/// replaces the buffer entirely with the correctly-shaped result.
#[test]
fn test_bind_output_shape_mismatch_replaced() {
    let session = make_identity_session();
    let mut binding = IoBinding::new();

    // Pre-bind a buffer with a different shape
    binding.bind_output("y", Tensor::new(vec![0.0f32; 2], vec![2]));

    let input_data = vec![1.0f32, 2.0, 3.0, 4.0];
    let shape = vec![4];
    binding.bind_input("x", Tensor::new(input_data.clone(), shape.clone()));

    session
        .run_with_binding(&mut binding)
        .expect("run_with_binding with mismatched pre-bound output");

    let out = binding
        .get_output("y")
        .expect("output 'y' should be present");
    assert_eq!(out.shape, shape, "shape must match input after replacement");
    assert_eq!(
        out.data, input_data,
        "data must match input after replacement"
    );
}

/// bind_output + get_output round-trip: bound buffer is readable and named correctly.
#[test]
fn test_bind_output_readable_via_get_output() {
    let mut binding = IoBinding::new();
    let data = vec![1.0f32, 2.0];
    binding.bind_output("z", Tensor::new(data.clone(), vec![2]));

    // Buffer visible through get_output immediately after binding
    let found = binding.get_output("z").expect("'z' should be present");
    assert_eq!(found.data, data);

    // output_names iterator includes "z"
    let names: Vec<&str> = binding.output_names().collect();
    assert!(names.contains(&"z"), "output_names must include 'z'");

    // Absent key returns None
    assert!(
        binding.get_output("not_there").is_none(),
        "absent key must return None"
    );
}

/// supports_output_slots returns true for IdentityOp and execute_into_slots
/// copies data into a correctly-sized slot.
#[test]
fn test_execute_into_slots_identity_success() {
    use oxionnx_core::{Attributes, Node, OpContext, OpKind, Operator, Tensor};
    use oxionnx_ops::registry::misc_ops::IdentityOp;

    let input = Tensor::new(vec![1.0f32, 2.0, 3.0], vec![3]);
    let node = Node {
        op: OpKind::Identity,
        name: "id".to_string(),
        inputs: vec!["x".to_string()],
        outputs: vec!["y".to_string()],
        attrs: Attributes::default(),
    };

    let ctx = OpContext {
        node: &node,
        inputs: vec![Some(&input)],
        outer_scope: None,
        registry: None,
    };

    let op = IdentityOp;
    assert!(
        op.supports_output_slots(),
        "IdentityOp must support output slots"
    );

    // Provide a correctly-shaped slot
    let mut slots = vec![Tensor::new(vec![0.0f32; 3], vec![3])];
    op.execute_into_slots(&ctx, &mut slots)
        .expect("execute_into_slots should succeed with matching slot");

    assert_eq!(slots[0].data, vec![1.0f32, 2.0, 3.0]);
    assert_eq!(slots[0].shape, vec![3]);
}

/// execute_into_slots on IdentityOp with empty slots returns an error.
#[test]
fn test_execute_into_slots_identity_empty_slots_error() {
    use oxionnx_core::{Attributes, Node, OpContext, OpKind, Operator, Tensor};
    use oxionnx_ops::registry::misc_ops::IdentityOp;

    let input = Tensor::new(vec![1.0f32, 2.0], vec![2]);
    let node = Node {
        op: OpKind::Identity,
        name: "id".to_string(),
        inputs: vec!["x".to_string()],
        outputs: vec!["y".to_string()],
        attrs: Attributes::default(),
    };

    let ctx = OpContext {
        node: &node,
        inputs: vec![Some(&input)],
        outer_scope: None,
        registry: None,
    };

    let op = IdentityOp;
    let mut slots: Vec<Tensor> = vec![];
    let result = op.execute_into_slots(&ctx, &mut slots);
    assert!(
        result.is_err(),
        "execute_into_slots with empty slots must return error for IdentityOp"
    );
}

/// execute_into_slots on AddOp writes sum into a pre-allocated slot.
#[test]
fn test_execute_into_slots_add_op() {
    use oxionnx_core::{Attributes, Node, OpContext, OpKind, Operator, Tensor};
    use oxionnx_ops::registry::math_ops::AddOp;

    let a = Tensor::new(vec![1.0f32, 2.0, 3.0], vec![3]);
    let b = Tensor::new(vec![4.0f32, 5.0, 6.0], vec![3]);
    let node = Node {
        op: OpKind::Add,
        name: "add".to_string(),
        inputs: vec!["a".to_string(), "b".to_string()],
        outputs: vec!["c".to_string()],
        attrs: Attributes::default(),
    };

    let ctx = OpContext {
        node: &node,
        inputs: vec![Some(&a), Some(&b)],
        outer_scope: None,
        registry: None,
    };

    let op = AddOp;
    assert!(
        op.supports_output_slots(),
        "AddOp must support output slots"
    );

    let mut slots = vec![Tensor::new(vec![0.0f32; 3], vec![3])];
    op.execute_into_slots(&ctx, &mut slots)
        .expect("execute_into_slots Add should succeed");

    assert_eq!(slots[0].data, vec![5.0f32, 7.0, 9.0]);
    assert_eq!(slots[0].shape, vec![3]);
}

/// clear() removes both inputs and outputs from a binding.
#[test]
fn test_io_binding_clear_all() {
    let mut binding = IoBinding::new();
    binding.bind_input("x", Tensor::new(vec![1.0f32], vec![1]));
    binding.bind_output("y", Tensor::new(vec![0.0f32], vec![1]));

    assert_eq!(binding.input_names().count(), 1);
    assert_eq!(binding.output_names().count(), 1);

    binding.clear();

    assert_eq!(binding.input_names().count(), 0, "inputs cleared");
    assert_eq!(binding.output_names().count(), 0, "outputs cleared");
}

// ── F.8 / SessionRunState pool-reuse regression tests ────────────────────────

/// Run a single Add node 100 times via `session.run()` and verify every output
/// is correct. Exercises the `SessionRunState` pool-reuse path: buffers
/// released after each iteration are reclaimed by the pool and reused in the
/// next, so any aliasing or stale-data bug would manifest as wrong values.
#[test]
fn test_pool_reuse_across_consecutive_runs() {
    use oxionnx_core::Graph;

    let add_node = oxionnx_core::Node {
        op: oxionnx_core::OpKind::Add,
        name: "add".to_string(),
        inputs: vec!["a".to_string(), "b".to_string()],
        outputs: vec!["c".to_string()],
        attrs: oxionnx_core::Attributes::default(),
    };
    let graph = Graph {
        nodes: vec![add_node],
        input_names: vec!["a".to_string()],
        output_names: vec!["c".to_string()],
        input_infos: vec![],
        output_infos: vec![],
        name: String::new(),
    };

    // Constant "b" weight so only "a" is a runtime input.
    let mut weights = HashMap::new();
    weights.insert(
        "b".to_string(),
        Tensor::new(vec![1.0f32, 2.0, 3.0], vec![3]),
    );

    let session = Session::from_graph(graph, weights).expect("session build");

    for i in 0..100_usize {
        let factor = (i + 1) as f32;
        let a_data = vec![factor, factor * 2.0, factor * 3.0];
        let expected = vec![factor + 1.0, factor * 2.0 + 2.0, factor * 3.0 + 3.0];

        let mut inputs = HashMap::new();
        inputs.insert("a", Tensor::new(a_data, vec![3]));

        let outputs = session
            .run(&inputs)
            .unwrap_or_else(|e| panic!("run failed at iteration {i}: {e}"));

        let c = outputs
            .get("c")
            .unwrap_or_else(|| panic!("output 'c' missing at iteration {i}"));
        assert_eq!(
            c.data, expected,
            "pool reuse: wrong output at iteration {i}"
        );
        assert_eq!(c.shape, vec![3], "shape mismatch at iteration {i}");
    }
}

/// Regression guard for the `SessionRunState` refactor: build a two-node
/// chain (`Add` followed by `Relu`) and verify the output matches expected
/// values. Uses values that straddle zero so `Relu` actually zeroes elements.
///
/// Graph:  [a, b] -> Add -> "mid" -> Relu -> "out"
///
/// Input:  a = [-2, -1, 1, 2],  b (weight) = [1, 1, 1, 1]
/// After Add:  mid = [-1, 0, 2, 3]
/// After Relu: out = [0, 0, 2, 3]
#[test]
fn test_session_run_correctness_after_f8_refactor() {
    use oxionnx_core::Graph;

    let add_node = oxionnx_core::Node {
        op: oxionnx_core::OpKind::Add,
        name: "add".to_string(),
        inputs: vec!["a".to_string(), "b".to_string()],
        outputs: vec!["mid".to_string()],
        attrs: oxionnx_core::Attributes::default(),
    };
    let relu_node = oxionnx_core::Node {
        op: oxionnx_core::OpKind::Relu,
        name: "relu".to_string(),
        inputs: vec!["mid".to_string()],
        outputs: vec!["out".to_string()],
        attrs: oxionnx_core::Attributes::default(),
    };
    let graph = Graph {
        nodes: vec![add_node, relu_node],
        input_names: vec!["a".to_string()],
        output_names: vec!["out".to_string()],
        input_infos: vec![],
        output_infos: vec![],
        name: String::new(),
    };

    // Constant addend "b" = [1, 1, 1, 1]
    let mut weights = HashMap::new();
    weights.insert(
        "b".to_string(),
        Tensor::new(vec![1.0f32, 1.0, 1.0, 1.0], vec![4]),
    );

    let session = Session::from_graph(graph, weights).expect("session build");

    // a = [-2, -1, 1, 2] → Add → [-1, 0, 2, 3] → Relu → [0, 0, 2, 3]
    let mut inputs = HashMap::new();
    inputs.insert("a", Tensor::new(vec![-2.0f32, -1.0, 1.0, 2.0], vec![4]));

    let outputs = session.run(&inputs).expect("session run");
    let out = outputs.get("out").expect("output 'out' should be present");

    assert_eq!(out.shape, vec![4], "output shape must be [4]");
    assert_eq!(
        out.data,
        vec![0.0f32, 0.0, 2.0, 3.0],
        "Add+Relu correctness: expected [0, 0, 2, 3]"
    );
}