oxionnx 0.1.2

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
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
//! Synthetic BERT-tiny end-to-end inference tests for oxionnx.
//!
//! Builds a BERT-tiny-like architecture programmatically (no ONNX file needed)
//! and runs inference through the session API.
//!
//! BERT-tiny config:
//!   hidden=128, heads=2, intermediate=512, layers=2, seq_len=16, vocab_size=100

use std::collections::HashMap;

use oxionnx::{Attributes, Graph, Node, NodeProfile, OpKind, OptLevel, Session, Tensor};

const HIDDEN: usize = 128;
const SEQ_LEN: usize = 16;
const INTERMEDIATE: usize = 512;
const NUM_LAYERS: usize = 2;

/// Generate a deterministic tensor with small values in approximately [-1, 1].
fn det_tensor(shape: &[usize], seed: u32) -> Tensor {
    let n: usize = shape.iter().product();
    let data: Vec<f32> = (0..n)
        .map(|i| {
            let x = ((i as u32).wrapping_mul(seed).wrapping_add(17)) as f32;
            (x % 200.0 - 100.0) * 0.01
        })
        .collect();
    Tensor::new(data, shape.to_vec())
}

/// Helper: create a node with default attributes.
fn make_node(op: OpKind, name: &str, inputs: &[&str], outputs: &[&str]) -> Node {
    Node {
        op,
        name: name.to_string(),
        inputs: inputs.iter().map(|s| s.to_string()).collect(),
        outputs: outputs.iter().map(|s| s.to_string()).collect(),
        attrs: Attributes::default(),
    }
}

/// Helper: create a node with custom attributes.
fn make_node_attrs(
    op: OpKind,
    name: &str,
    inputs: &[&str],
    outputs: &[&str],
    attrs: Attributes,
) -> Node {
    Node {
        op,
        name: name.to_string(),
        inputs: inputs.iter().map(|s| s.to_string()).collect(),
        outputs: outputs.iter().map(|s| s.to_string()).collect(),
        attrs,
    }
}

/// Build the full BERT-tiny graph, returning the Graph, weights, and output tensor name.
///
/// Architecture per layer:
///   Self-Attention (single-head simplified):
///     Q = MatMul(input, Wq), K = MatMul(input, Wk), V = MatMul(input, Wv)
///     scores = Softmax(MatMul(Q, Transpose(K)) / sqrt(hidden))
///     attn = MatMul(scores, V)
///     proj = MatMul(attn, Wo)
///     res1 = Add(input, proj)
///   FFN:
///     ffn1 = Gelu(MatMul(res1, W1))
///     ffn2 = MatMul(ffn1, W2)
///     layer_out = Add(res1, ffn2)
fn build_bert_tiny_graph() -> (Graph, HashMap<String, Tensor>, String) {
    let mut nodes = Vec::new();
    let mut weights = HashMap::new();
    let mut node_id: u32 = 0;

    let mut make_name = |prefix: &str| -> String {
        node_id += 1;
        format!("{}_{}", prefix, node_id)
    };

    // Current activation name flowing through the network.
    // Input is [1, SEQ_LEN, HIDDEN] - skip embedding lookup, test transformer layers.
    let mut current = "input_embed".to_string();

    for layer in 0..NUM_LAYERS {
        let prefix = format!("L{}", layer);

        // === Self-Attention (simplified single-head) ===

        // Q = MatMul(current, Wq)
        let wq_name = format!("{}_Wq", prefix);
        weights.insert(
            wq_name.clone(),
            det_tensor(&[HIDDEN, HIDDEN], layer as u32 * 100 + 1),
        );
        let q_name = make_name("Q");
        let q_out = format!("{}_out", q_name);
        nodes.push(make_node(
            OpKind::MatMul,
            &q_name,
            &[&current, &wq_name],
            &[&q_out],
        ));

        // K = MatMul(current, Wk)
        let wk_name = format!("{}_Wk", prefix);
        weights.insert(
            wk_name.clone(),
            det_tensor(&[HIDDEN, HIDDEN], layer as u32 * 100 + 2),
        );
        let k_name = make_name("K");
        let k_out = format!("{}_out", k_name);
        nodes.push(make_node(
            OpKind::MatMul,
            &k_name,
            &[&current, &wk_name],
            &[&k_out],
        ));

        // V = MatMul(current, Wv)
        let wv_name = format!("{}_Wv", prefix);
        weights.insert(
            wv_name.clone(),
            det_tensor(&[HIDDEN, HIDDEN], layer as u32 * 100 + 3),
        );
        let v_name = make_name("V");
        let v_out = format!("{}_out", v_name);
        nodes.push(make_node(
            OpKind::MatMul,
            &v_name,
            &[&current, &wv_name],
            &[&v_out],
        ));

        // Transpose K: [1, seq, hidden] -> [1, hidden, seq]
        let kt_name = make_name("KT");
        let kt_out = format!("{}_out", kt_name);
        let mut transpose_attrs = Attributes::default();
        transpose_attrs
            .int_lists
            .insert("perm".into(), vec![0, 2, 1]);
        nodes.push(make_node_attrs(
            OpKind::Transpose,
            &kt_name,
            &[&k_out],
            &[&kt_out],
            transpose_attrs,
        ));

        // scores = MatMul(Q, K^T) -> [1, seq, seq]
        let scores_name = make_name("scores");
        let scores_out = format!("{}_out", scores_name);
        nodes.push(make_node(
            OpKind::MatMul,
            &scores_name,
            &[&q_out, &kt_out],
            &[&scores_out],
        ));

        // Scale by 1/sqrt(hidden) using Div
        let scale_name = format!("{}_scale", prefix);
        let scale_val = (HIDDEN as f32).sqrt();
        weights.insert(scale_name.clone(), Tensor::new(vec![scale_val], vec![1]));
        let scaled_name = make_name("scaled_scores");
        let scaled_out = format!("{}_out", scaled_name);
        nodes.push(make_node(
            OpKind::Div,
            &scaled_name,
            &[&scores_out, &scale_name],
            &[&scaled_out],
        ));

        // Softmax on last axis
        let softmax_name = make_name("softmax");
        let softmax_out = format!("{}_out", softmax_name);
        let mut softmax_attrs = Attributes::default();
        softmax_attrs.ints.insert("axis".into(), -1);
        nodes.push(make_node_attrs(
            OpKind::Softmax,
            &softmax_name,
            &[&scaled_out],
            &[&softmax_out],
            softmax_attrs,
        ));

        // attn = MatMul(softmax, V) -> [1, seq, hidden]
        let attn_name = make_name("attn");
        let attn_out = format!("{}_out", attn_name);
        nodes.push(make_node(
            OpKind::MatMul,
            &attn_name,
            &[&softmax_out, &v_out],
            &[&attn_out],
        ));

        // Output projection: proj = MatMul(attn, Wo)
        let wo_name = format!("{}_Wo", prefix);
        weights.insert(
            wo_name.clone(),
            det_tensor(&[HIDDEN, HIDDEN], layer as u32 * 100 + 4),
        );
        let proj_name = make_name("proj");
        let proj_out = format!("{}_out", proj_name);
        nodes.push(make_node(
            OpKind::MatMul,
            &proj_name,
            &[&attn_out, &wo_name],
            &[&proj_out],
        ));

        // Residual: res1 = Add(current, proj)
        let res1_name = make_name("res1");
        let res1_out = format!("{}_out", res1_name);
        nodes.push(make_node(
            OpKind::Add,
            &res1_name,
            &[&current, &proj_out],
            &[&res1_out],
        ));

        // === Feed-Forward Network ===

        // ffn1 = MatMul(res1, W1) -> [1, seq, intermediate]
        let w1_name = format!("{}_W1", prefix);
        weights.insert(
            w1_name.clone(),
            det_tensor(&[HIDDEN, INTERMEDIATE], layer as u32 * 100 + 5),
        );
        let ffn1_name = make_name("ffn1");
        let ffn1_out = format!("{}_out", ffn1_name);
        nodes.push(make_node(
            OpKind::MatMul,
            &ffn1_name,
            &[&res1_out, &w1_name],
            &[&ffn1_out],
        ));

        // Gelu activation
        let gelu_name = make_name("gelu");
        let gelu_out = format!("{}_out", gelu_name);
        nodes.push(make_node(
            OpKind::Gelu,
            &gelu_name,
            &[&ffn1_out],
            &[&gelu_out],
        ));

        // ffn2 = MatMul(gelu, W2) -> [1, seq, hidden]
        let w2_name = format!("{}_W2", prefix);
        weights.insert(
            w2_name.clone(),
            det_tensor(&[INTERMEDIATE, HIDDEN], layer as u32 * 100 + 6),
        );
        let ffn2_name = make_name("ffn2");
        let ffn2_out = format!("{}_out", ffn2_name);
        nodes.push(make_node(
            OpKind::MatMul,
            &ffn2_name,
            &[&gelu_out, &w2_name],
            &[&ffn2_out],
        ));

        // Residual: layer_out = Add(res1, ffn2)
        let res2_name = make_name("res2");
        let res2_out = format!("{}_out", res2_name);
        nodes.push(make_node(
            OpKind::Add,
            &res2_name,
            &[&res1_out, &ffn2_out],
            &[&res2_out],
        ));

        current = res2_out;
    }

    let graph = Graph {
        nodes,
        input_names: vec!["input_embed".into()],
        output_names: vec![current.clone()],
        ..Default::default()
    };

    (graph, weights, current)
}

/// Validate that a BERT output tensor has the expected shape, contains no NaN/Inf,
/// and values are within a reasonable magnitude.
fn validate_bert_output(output: &Tensor, output_name: &str) {
    // Shape must be [1, SEQ_LEN, HIDDEN]
    assert_eq!(
        output.shape,
        vec![1, SEQ_LEN, HIDDEN],
        "unexpected output shape for '{}'",
        output_name
    );

    // No NaN or Inf
    assert!(
        output.data.iter().all(|v| v.is_finite()),
        "output '{}' contains NaN or Inf",
        output_name
    );

    // Values should not have exploded
    let max_abs = output.data.iter().map(|v| v.abs()).fold(0.0f32, f32::max);
    assert!(
        max_abs < 1e6,
        "output '{}' values exploded: max_abs={}",
        output_name,
        max_abs
    );
}

// ── Test 1: Basic BERT-tiny inference ───────────────────────────────────────

#[test]
fn test_bert_tiny_synthetic() {
    let (graph, weights, output_name) = build_bert_tiny_graph();

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

    // Verify model info
    let info = session.model_info();
    assert!(
        info.node_count > 0,
        "model should have at least one node after optimization"
    );
    assert!(
        info.parameter_count > 0,
        "model should have weight parameters"
    );

    // Input: [1, SEQ_LEN, HIDDEN]
    let input = det_tensor(&[1, SEQ_LEN, HIDDEN], 42);
    let outputs = session
        .run_one("input_embed", input)
        .expect("BERT run failed");
    let output = outputs
        .get(&output_name)
        .expect("output tensor not found in results");

    validate_bert_output(output, &output_name);

    // Run a second time to verify determinism
    let input2 = det_tensor(&[1, SEQ_LEN, HIDDEN], 42);
    let outputs2 = session
        .run_one("input_embed", input2)
        .expect("BERT second run failed");
    let output2 = outputs2
        .get(&output_name)
        .expect("output tensor not found in second run");

    assert_eq!(
        output.data, output2.data,
        "two runs with the same input should produce identical output"
    );
}

// ── Test 2: BERT-tiny with profiling ────────────────────────────────────────

#[test]
fn test_bert_tiny_with_profiling() {
    let (graph, weights, output_name) = build_bert_tiny_graph();

    let session = Session::builder()
        .with_optimization_level(OptLevel::All)
        .with_profiling()
        .build_from_graph(graph, weights)
        .expect("BERT profiling build failed");

    let input = det_tensor(&[1, SEQ_LEN, HIDDEN], 99);
    let outputs = session
        .run_one("input_embed", input)
        .expect("BERT profiling run failed");
    let output = outputs.get(&output_name).expect("output tensor not found");

    validate_bert_output(output, &output_name);

    // Check profiling results
    let profiles: Vec<NodeProfile> = session
        .profiling_results()
        .expect("profiling should be enabled");
    assert!(
        !profiles.is_empty(),
        "profiling should have recorded at least one node"
    );

    // Every profiled node should have a non-empty op_type
    for profile in &profiles {
        assert!(
            !profile.op_type.is_empty(),
            "node '{}' has empty op_type",
            profile.node_name
        );
        assert!(
            !profile.output_shapes.is_empty(),
            "node '{}' has no output shapes",
            profile.node_name
        );
    }

    // We expect MatMul nodes to be present (the main compute ops)
    let matmul_count = profiles.iter().filter(|p| p.op_type == "MatMul").count();
    assert!(
        matmul_count > 0,
        "expected at least one MatMul in profiling data, got 0"
    );

    // Total profiled time should be non-zero
    let total_ns: u128 = profiles.iter().map(|p| p.duration.as_nanos()).sum();
    assert!(
        total_ns > 0,
        "total profiled time should be greater than zero"
    );
}

// ── Test 3: BERT-tiny with parallel execution ───────────────────────────────

#[test]
fn test_bert_tiny_with_parallel() {
    let (graph, weights, output_name) = build_bert_tiny_graph();

    // Build sequential session
    let (graph_seq, weights_seq, _) = build_bert_tiny_graph();
    let session_seq = Session::builder()
        .with_optimization_level(OptLevel::All)
        .build_from_graph(graph_seq, weights_seq)
        .expect("sequential build failed");

    // Build parallel session
    let session_par = Session::builder()
        .with_optimization_level(OptLevel::All)
        .with_parallel_execution(true)
        .build_from_graph(graph, weights)
        .expect("parallel build failed");

    let input = det_tensor(&[1, SEQ_LEN, HIDDEN], 77);

    let outputs_seq = session_seq
        .run_one("input_embed", input.clone())
        .expect("sequential run failed");
    let outputs_par = session_par
        .run_one("input_embed", input)
        .expect("parallel run failed");

    let out_seq = outputs_seq
        .get(&output_name)
        .expect("sequential output not found");
    let out_par = outputs_par
        .get(&output_name)
        .expect("parallel output not found");

    validate_bert_output(out_seq, &output_name);
    validate_bert_output(out_par, &output_name);

    // Sequential and parallel should produce the same shape
    assert_eq!(
        out_seq.shape, out_par.shape,
        "sequential and parallel output shapes differ"
    );

    // Values should be very close (floating-point reorder may cause tiny differences)
    let max_diff = out_seq
        .data
        .iter()
        .zip(out_par.data.iter())
        .map(|(a, b)| (a - b).abs())
        .fold(0.0f32, f32::max);

    // Allow small tolerance for floating-point reordering in parallel execution
    assert!(
        max_diff < 1e-3,
        "sequential vs parallel max diff = {} (expected < 1e-3)",
        max_diff
    );
}