onnx-runtime-session 0.1.0-dev.5

Session and inference API for the ORT 2.0 runtime: intent-based SessionBuilder and sequential executor (skeleton)
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
//! End-to-end integration tests for the ONNX **Sequence** ops, exercised
//! through the public [`InferenceSession`] surface. They cover:
//!
//! * `SequenceConstruct` → `SequenceAt` round-trips (element values preserved);
//! * `SequenceInsert` / `SequenceErase` positional semantics (incl. negatives);
//! * `SequenceLength` → int64 scalar;
//! * `SplitToSequence` (default per-slice, keepdims, and explicit `split`);
//! * `ConcatFromSequence` (existing axis and `new_axis=1` stacking);
//! * the **no-copy proof**: a tensor inserted into a sequence and read back with
//!   `SequenceAt`, then consumed by `Identity`, yields the exact same values —
//!   and the round-trip through the sequence never mutates data;
//! * actionable error paths (out-of-bounds index, empty-sequence concat).

use onnx_runtime_ir::{
    Attribute, DataType, Graph, Node, NodeId, TensorData, ValueId, WeightRef, static_shape,
};
use onnx_runtime_session::InferenceSession;

fn f32_bytes(data: &[f32]) -> Vec<u8> {
    data.iter().flat_map(|v| v.to_le_bytes()).collect()
}
fn i64_bytes(data: &[i64]) -> Vec<u8> {
    data.iter().flat_map(|v| v.to_le_bytes()).collect()
}

fn f32_init(g: &mut Graph, name: &str, dims: &[usize], data: &[f32]) -> ValueId {
    let vid = g.create_named_value(name, DataType::Float32, static_shape(dims.iter().copied()));
    g.set_initializer(
        vid,
        WeightRef::Inline(TensorData::from_raw(
            DataType::Float32,
            dims.to_vec(),
            f32_bytes(data),
        )),
    );
    vid
}
fn i64_init(g: &mut Graph, name: &str, dims: &[usize], data: &[i64]) -> ValueId {
    let vid = g.create_named_value(name, DataType::Int64, static_shape(dims.iter().copied()));
    g.set_initializer(
        vid,
        WeightRef::Inline(TensorData::from_raw(
            DataType::Int64,
            dims.to_vec(),
            i64_bytes(data),
        )),
    );
    vid
}

/// Insert an op node whose single output is a fresh anonymous value. `out_dtype`
/// / `out_dims` describe the (tensor) output; Sequence-typed outputs pass a
/// placeholder shape (unused by the executor for sequence values).
fn op(
    g: &mut Graph,
    op_type: &str,
    inputs: &[ValueId],
    out_dtype: DataType,
    out_dims: &[usize],
    attrs: &[(&str, Attribute)],
) -> ValueId {
    g.opset_imports.entry(String::new()).or_insert(17);
    let out = g.create_value(out_dtype, static_shape(out_dims.iter().copied()));
    let mut node = Node::new(
        NodeId(0),
        op_type,
        inputs.iter().map(|&v| Some(v)).collect(),
        vec![out],
    );
    for (k, v) in attrs {
        node.attributes.insert((*k).to_string(), v.clone());
    }
    g.insert_node(node);
    out
}

/// SequenceConstruct(a, b, c) then SequenceAt(seq, idx) returns element `idx`.
#[test]
fn construct_then_at_roundtrips_values() {
    let mut g = Graph::new();
    let a = f32_init(&mut g, "a", &[2], &[1., 2.]);
    let b = f32_init(&mut g, "b", &[2], &[3., 4.]);
    let c = f32_init(&mut g, "c", &[2], &[5., 6.]);
    let seq = op(
        &mut g,
        "SequenceConstruct",
        &[a, b, c],
        DataType::Float32,
        &[],
        &[],
    );
    let idx = i64_init(&mut g, "idx", &[], &[1]);
    let at = op(
        &mut g,
        "SequenceAt",
        &[seq, idx],
        DataType::Float32,
        &[2],
        &[],
    );
    g.add_output(at);

    let mut s = InferenceSession::from_graph(g).expect("build");
    let out = s.run(&[]).expect("run");
    assert_eq!(out[0].shape, vec![2]);
    assert_eq!(out[0].to_vec_f32(), vec![3., 4.]);
}

/// Negative index counts from the end.
#[test]
fn at_negative_index() {
    let mut g = Graph::new();
    let a = f32_init(&mut g, "a", &[1], &[10.]);
    let b = f32_init(&mut g, "b", &[1], &[20.]);
    let seq = op(
        &mut g,
        "SequenceConstruct",
        &[a, b],
        DataType::Float32,
        &[],
        &[],
    );
    let idx = i64_init(&mut g, "idx", &[], &[-1]);
    let at = op(
        &mut g,
        "SequenceAt",
        &[seq, idx],
        DataType::Float32,
        &[1],
        &[],
    );
    g.add_output(at);
    let mut s = InferenceSession::from_graph(g).expect("build");
    let out = s.run(&[]).expect("run");
    assert_eq!(out[0].to_vec_f32(), vec![20.]);
}

/// Insert at a position, then read it back.
#[test]
fn insert_at_position_then_at() {
    let mut g = Graph::new();
    let a = f32_init(&mut g, "a", &[1], &[1.]);
    let b = f32_init(&mut g, "b", &[1], &[2.]);
    let ins = f32_init(&mut g, "ins", &[1], &[99.]);
    let seq = op(
        &mut g,
        "SequenceConstruct",
        &[a, b],
        DataType::Float32,
        &[],
        &[],
    );
    let pos = i64_init(&mut g, "pos", &[], &[0]);
    let seq2 = op(
        &mut g,
        "SequenceInsert",
        &[seq, ins, pos],
        DataType::Float32,
        &[],
        &[],
    );
    // seq2 == [99, 1, 2]; read index 0
    let idx = i64_init(&mut g, "idx", &[], &[0]);
    let at = op(
        &mut g,
        "SequenceAt",
        &[seq2, idx],
        DataType::Float32,
        &[1],
        &[],
    );
    g.add_output(at);
    let mut s = InferenceSession::from_graph(g).expect("build");
    let out = s.run(&[]).expect("run");
    assert_eq!(out[0].to_vec_f32(), vec![99.]);
}

/// Insert with default (append) position, then length is 3.
#[test]
fn insert_default_append_and_length() {
    let mut g = Graph::new();
    let a = f32_init(&mut g, "a", &[1], &[1.]);
    let b = f32_init(&mut g, "b", &[1], &[2.]);
    let ins = f32_init(&mut g, "ins", &[1], &[3.]);
    let seq = op(
        &mut g,
        "SequenceConstruct",
        &[a, b],
        DataType::Float32,
        &[],
        &[],
    );
    let seq2 = op(
        &mut g,
        "SequenceInsert",
        &[seq, ins],
        DataType::Float32,
        &[],
        &[],
    );
    let len = op(&mut g, "SequenceLength", &[seq2], DataType::Int64, &[], &[]);
    g.add_output(len);
    let mut s = InferenceSession::from_graph(g).expect("build");
    let out = s.run(&[]).expect("run");
    assert_eq!(out[0].dtype, DataType::Int64);
    assert_eq!(out[0].to_vec_i64(), vec![3]);
}

/// Exercise the required multi-op path in one graph, including negative
/// insertion and indexing plus scalar int64 length output.
#[test]
fn construct_insert_at_and_length_execute_together() {
    let mut g = Graph::new();
    let a = f32_init(&mut g, "a", &[1], &[1.]);
    let b = f32_init(&mut g, "b", &[1], &[2.]);
    let inserted = f32_init(&mut g, "inserted", &[1], &[99.]);
    let seq = op(
        &mut g,
        "SequenceConstruct",
        &[a, b],
        DataType::Float32,
        &[],
        &[],
    );
    let insert_pos = i64_init(&mut g, "insert_pos", &[], &[-1]);
    let seq2 = op(
        &mut g,
        "SequenceInsert",
        &[seq, inserted, insert_pos],
        DataType::Float32,
        &[],
        &[],
    );
    let at_pos = i64_init(&mut g, "at_pos", &[], &[-2]);
    let at = op(
        &mut g,
        "SequenceAt",
        &[seq2, at_pos],
        DataType::Float32,
        &[1],
        &[],
    );
    let len = op(&mut g, "SequenceLength", &[seq2], DataType::Int64, &[], &[]);
    g.add_output(at);
    g.add_output(len);

    let mut s = InferenceSession::from_graph(g).expect("build");
    let out = s.run(&[]).expect("run");
    assert_eq!(out[0].to_vec_f32(), vec![99.]);
    assert_eq!(out[1].dtype, DataType::Int64);
    assert!(out[1].shape.is_empty());
    assert_eq!(out[1].to_vec_i64(), vec![3]);
}

/// Erase an element (default last), then length drops by one.
#[test]
fn erase_then_length() {
    let mut g = Graph::new();
    let a = f32_init(&mut g, "a", &[1], &[1.]);
    let b = f32_init(&mut g, "b", &[1], &[2.]);
    let c = f32_init(&mut g, "c", &[1], &[3.]);
    let seq = op(
        &mut g,
        "SequenceConstruct",
        &[a, b, c],
        DataType::Float32,
        &[],
        &[],
    );
    let erased = op(&mut g, "SequenceErase", &[seq], DataType::Float32, &[], &[]);
    let len = op(
        &mut g,
        "SequenceLength",
        &[erased],
        DataType::Int64,
        &[],
        &[],
    );
    g.add_output(len);
    let mut s = InferenceSession::from_graph(g).expect("build");
    let out = s.run(&[]).expect("run");
    assert_eq!(out[0].to_vec_i64(), vec![2]);
}

/// The no-copy proof at the executor boundary: a tensor round-trips through a
/// sequence (Construct → Insert front → Erase front → At) and feeds `Identity`
/// (a contiguous-only consumer). Correct values prove the shared element bytes
/// were never corrupted, and — combined with the value-layer `Arc::ptr_eq`
/// unit test — that the round-trip did no deep copy of element data.
#[test]
fn seq_at_feeds_consumer_no_copy_roundtrip() {
    let mut g = Graph::new();
    let a = f32_init(&mut g, "a", &[3], &[7., 8., 9.]);
    let filler = f32_init(&mut g, "filler", &[3], &[0., 0., 0.]);
    let seq = op(
        &mut g,
        "SequenceConstruct",
        &[a],
        DataType::Float32,
        &[],
        &[],
    );
    let pos0 = i64_init(&mut g, "pos0", &[], &[0]);
    // Insert filler at front → [filler, a]; a is now index 1.
    let seq2 = op(
        &mut g,
        "SequenceInsert",
        &[seq, filler, pos0],
        DataType::Float32,
        &[],
        &[],
    );
    // Erase front → [a]; a is now index 0.
    let pos0b = i64_init(&mut g, "pos0b", &[], &[0]);
    let seq3 = op(
        &mut g,
        "SequenceErase",
        &[seq2, pos0b],
        DataType::Float32,
        &[],
        &[],
    );
    let idx = i64_init(&mut g, "idx", &[], &[0]);
    let at = op(
        &mut g,
        "SequenceAt",
        &[seq3, idx],
        DataType::Float32,
        &[3],
        &[],
    );
    // Consume the seq-backed tensor with Identity (reads it zero-copy).
    let ident = op(&mut g, "Identity", &[at], DataType::Float32, &[3], &[]);
    g.add_output(ident);
    let mut s = InferenceSession::from_graph(g).expect("build");
    let out = s.run(&[]).expect("run");
    assert_eq!(out[0].to_vec_f32(), vec![7., 8., 9.]);
}

/// SplitToSequence with no `split`: one element per slice along axis 0
/// (keepdims default 1), reassembled with ConcatFromSequence(axis=0) →
/// identity.
#[test]
fn split_then_concat_roundtrips() {
    let mut g = Graph::new();
    let data = f32_init(&mut g, "data", &[3, 2], &[1., 2., 3., 4., 5., 6.]);
    let seq = op(
        &mut g,
        "SplitToSequence",
        &[data],
        DataType::Float32,
        &[],
        &[("axis", Attribute::Int(0))],
    );
    let cat = op(
        &mut g,
        "ConcatFromSequence",
        &[seq],
        DataType::Float32,
        &[3, 2],
        &[("axis", Attribute::Int(0))],
    );
    g.add_output(cat);
    let mut s = InferenceSession::from_graph(g).expect("build");
    let out = s.run(&[]).expect("run");
    assert_eq!(out[0].shape, vec![3, 2]);
    assert_eq!(out[0].to_vec_f32(), vec![1., 2., 3., 4., 5., 6.]);
}

/// SplitToSequence with keepdims=0 squeezes the split axis; each element is a
/// 1-D row. Read element 1 back.
#[test]
fn split_keepdims0_squeezes() {
    let mut g = Graph::new();
    let data = f32_init(&mut g, "data", &[3, 2], &[1., 2., 3., 4., 5., 6.]);
    let seq = op(
        &mut g,
        "SplitToSequence",
        &[data],
        DataType::Float32,
        &[],
        &[("axis", Attribute::Int(0)), ("keepdims", Attribute::Int(0))],
    );
    let idx = i64_init(&mut g, "idx", &[], &[1]);
    let at = op(
        &mut g,
        "SequenceAt",
        &[seq, idx],
        DataType::Float32,
        &[2],
        &[],
    );
    g.add_output(at);
    let mut s = InferenceSession::from_graph(g).expect("build");
    let out = s.run(&[]).expect("run");
    assert_eq!(out[0].shape, vec![2]); // axis 0 squeezed
    assert_eq!(out[0].to_vec_f32(), vec![3., 4.]);
}

/// SplitToSequence with explicit uneven `split` sizes along axis 1.
#[test]
fn split_explicit_sizes() {
    let mut g = Graph::new();
    let data = f32_init(&mut g, "data", &[2, 3], &[0., 1., 2., 3., 4., 5.]);
    let split = i64_init(&mut g, "split", &[2], &[1, 2]);
    let seq = op(
        &mut g,
        "SplitToSequence",
        &[data, split],
        DataType::Float32,
        &[],
        &[("axis", Attribute::Int(1))],
    );
    let idx = i64_init(&mut g, "idx", &[], &[1]);
    let at = op(
        &mut g,
        "SequenceAt",
        &[seq, idx],
        DataType::Float32,
        &[2, 2],
        &[],
    );
    g.add_output(at);
    let mut s = InferenceSession::from_graph(g).expect("build");
    let out = s.run(&[]).expect("run");
    assert_eq!(out[0].shape, vec![2, 2]);
    assert_eq!(out[0].to_vec_f32(), vec![1., 2., 4., 5.]);
}

/// A rank-1 split tensor with one element is a sizes vector, not a scalar
/// chunk-size input, and therefore must cover the whole split axis.
#[test]
fn split_one_element_1d_sizes_must_sum_to_axis() {
    let mut g = Graph::new();
    let data = f32_init(&mut g, "data", &[4], &[1., 2., 3., 4.]);
    let split = i64_init(&mut g, "split", &[1], &[2]);
    let seq = op(
        &mut g,
        "SplitToSequence",
        &[data, split],
        DataType::Float32,
        &[],
        &[],
    );
    let len = op(&mut g, "SequenceLength", &[seq], DataType::Int64, &[], &[]);
    g.add_output(len);

    let mut s = InferenceSession::from_graph(g).expect("build");
    let err = s.run(&[]).unwrap_err();
    let msg = err.to_string();
    assert!(msg.contains("sizes sum to 2"), "msg: {msg}");
    assert!(msg.contains("axis 0 has extent 4"), "msg: {msg}");
}

/// A one-element rank-1 split vector is valid when its size is the full axis.
#[test]
fn split_one_element_1d_sizes_produces_one_chunk() {
    let mut g = Graph::new();
    let data = f32_init(&mut g, "data", &[1], &[42.]);
    let split = i64_init(&mut g, "split", &[1], &[1]);
    let seq = op(
        &mut g,
        "SplitToSequence",
        &[data, split],
        DataType::Float32,
        &[],
        &[],
    );
    let idx = i64_init(&mut g, "idx", &[], &[0]);
    let at = op(
        &mut g,
        "SequenceAt",
        &[seq, idx],
        DataType::Float32,
        &[1],
        &[],
    );
    g.add_output(at);

    let mut s = InferenceSession::from_graph(g).expect("build");
    let out = s.run(&[]).expect("run");
    assert_eq!(out[0].shape, vec![1]);
    assert_eq!(out[0].to_vec_f32(), vec![42.]);
}

/// A rank-0 split tensor is the scalar chunk size, producing even chunks.
#[test]
fn split_scalar_chunk_size_produces_even_chunks() {
    let mut g = Graph::new();
    let data = f32_init(&mut g, "data", &[4], &[1., 2., 3., 4.]);
    let split = i64_init(&mut g, "split", &[], &[2]);
    let seq = op(
        &mut g,
        "SplitToSequence",
        &[data, split],
        DataType::Float32,
        &[],
        &[],
    );
    let len = op(&mut g, "SequenceLength", &[seq], DataType::Int64, &[], &[]);
    g.add_output(len);

    let mut s = InferenceSession::from_graph(g).expect("build");
    let out = s.run(&[]).expect("run");
    assert_eq!(out[0].to_vec_i64(), vec![2]);
}

/// Scalar chunk sizes retain an uneven final chunk when they do not divide axis.
#[test]
fn split_scalar_chunk_size_keeps_uneven_final_chunk() {
    let mut g = Graph::new();
    let data = f32_init(&mut g, "data", &[4], &[1., 2., 3., 4.]);
    let split = i64_init(&mut g, "split", &[], &[3]);
    let seq = op(
        &mut g,
        "SplitToSequence",
        &[data, split],
        DataType::Float32,
        &[],
        &[],
    );
    let idx = i64_init(&mut g, "idx", &[], &[1]);
    let at = op(
        &mut g,
        "SequenceAt",
        &[seq, idx],
        DataType::Float32,
        &[1],
        &[],
    );
    g.add_output(at);

    let mut s = InferenceSession::from_graph(g).expect("build");
    let out = s.run(&[]).expect("run");
    assert_eq!(out[0].shape, vec![1]);
    assert_eq!(out[0].to_vec_f32(), vec![4.]);
}

/// ConcatFromSequence with new_axis=1 stacks elements along a fresh axis.
#[test]
fn concat_new_axis_stacks() {
    let mut g = Graph::new();
    let a = f32_init(&mut g, "a", &[2], &[1., 2.]);
    let b = f32_init(&mut g, "b", &[2], &[3., 4.]);
    let seq = op(
        &mut g,
        "SequenceConstruct",
        &[a, b],
        DataType::Float32,
        &[],
        &[],
    );
    let cat = op(
        &mut g,
        "ConcatFromSequence",
        &[seq],
        DataType::Float32,
        &[2, 2],
        &[("axis", Attribute::Int(0)), ("new_axis", Attribute::Int(1))],
    );
    g.add_output(cat);
    let mut s = InferenceSession::from_graph(g).expect("build");
    let out = s.run(&[]).expect("run");
    assert_eq!(out[0].shape, vec![2, 2]);
    assert_eq!(out[0].to_vec_f32(), vec![1., 2., 3., 4.]);
}

/// SequenceEmpty then SequenceInsert builds a one-element sequence.
#[test]
fn empty_then_insert() {
    let mut g = Graph::new();
    let t = f32_init(&mut g, "t", &[1], &[42.]);
    let seq = op(
        &mut g,
        "SequenceEmpty",
        &[],
        DataType::Float32,
        &[],
        &[("dtype", Attribute::Int(1))], // 1 == float32
    );
    let seq2 = op(
        &mut g,
        "SequenceInsert",
        &[seq, t],
        DataType::Float32,
        &[],
        &[],
    );
    let idx = i64_init(&mut g, "idx", &[], &[0]);
    let at = op(
        &mut g,
        "SequenceAt",
        &[seq2, idx],
        DataType::Float32,
        &[1],
        &[],
    );
    g.add_output(at);
    let mut s = InferenceSession::from_graph(g).expect("build");
    let out = s.run(&[]).expect("run");
    assert_eq!(out[0].to_vec_f32(), vec![42.]);
}

/// SequenceEmpty's declared dtype must be preserved even before its first insert.
#[test]
fn empty_insert_mismatched_dtype_errors_actionably() {
    let mut g = Graph::new();
    let t = f32_init(&mut g, "t", &[1], &[42.]);
    let seq = op(
        &mut g,
        "SequenceEmpty",
        &[],
        DataType::Int64,
        &[],
        &[("dtype", Attribute::Int(7))], // 7 == int64
    );
    let seq2 = op(
        &mut g,
        "SequenceInsert",
        &[seq, t],
        DataType::Int64,
        &[],
        &[],
    );
    let len = op(&mut g, "SequenceLength", &[seq2], DataType::Int64, &[], &[]);
    g.add_output(len);

    let mut s = InferenceSession::from_graph(g).expect("build");
    let err = s.run(&[]).unwrap_err();
    let msg = err.to_string();
    assert!(msg.contains("does not match"), "msg: {msg}");
    assert!(msg.contains("To fix"), "msg: {msg}");
}

/// Out-of-bounds SequenceAt is an actionable error (states valid range).
#[test]
fn at_out_of_bounds_errors_actionably() {
    let mut g = Graph::new();
    let a = f32_init(&mut g, "a", &[1], &[1.]);
    let seq = op(
        &mut g,
        "SequenceConstruct",
        &[a],
        DataType::Float32,
        &[],
        &[],
    );
    let idx = i64_init(&mut g, "idx", &[], &[5]);
    let at = op(
        &mut g,
        "SequenceAt",
        &[seq, idx],
        DataType::Float32,
        &[1],
        &[],
    );
    g.add_output(at);
    let mut s = InferenceSession::from_graph(g).expect("build");
    let err = s.run(&[]).unwrap_err();
    let msg = err.to_string();
    assert!(msg.contains("out of bounds"), "msg: {msg}");
    assert!(msg.contains("valid range"), "msg: {msg}");
}

#[test]
fn at_empty_sequence_errors_cleanly() {
    let mut g = Graph::new();
    let seq = op(
        &mut g,
        "SequenceEmpty",
        &[],
        DataType::Float32,
        &[],
        &[("dtype", Attribute::Int(1))],
    );
    let idx = i64_init(&mut g, "idx", &[], &[0]);
    let at = op(
        &mut g,
        "SequenceAt",
        &[seq, idx],
        DataType::Float32,
        &[1],
        &[],
    );
    g.add_output(at);

    let mut s = InferenceSession::from_graph(g).expect("build");
    let err = s.run(&[]).unwrap_err();
    let msg = err.to_string();
    assert!(msg.contains("out of bounds"), "msg: {msg}");
    assert!(msg.contains("length 0"), "msg: {msg}");
}

#[test]
fn construct_non_homogeneous_dtypes_errors_cleanly() {
    let mut g = Graph::new();
    let floats = f32_init(&mut g, "floats", &[1], &[1.]);
    let integers = i64_init(&mut g, "integers", &[1], &[2]);
    let seq = op(
        &mut g,
        "SequenceConstruct",
        &[floats, integers],
        DataType::Float32,
        &[],
        &[],
    );
    let len = op(&mut g, "SequenceLength", &[seq], DataType::Int64, &[], &[]);
    g.add_output(len);

    let mut s = InferenceSession::from_graph(g).expect("build");
    let err = s.run(&[]).unwrap_err();
    let msg = err.to_string();
    assert!(msg.contains("SequenceConstruct"), "msg: {msg}");
    assert!(msg.contains("homogeneous"), "msg: {msg}");
    assert!(msg.contains("does not match"), "msg: {msg}");
}

#[test]
fn sequence_empty_graph_output_succeeds() {
    let mut g = Graph::new();
    let seq = op(
        &mut g,
        "SequenceEmpty",
        &[],
        DataType::Float32,
        &[],
        &[("dtype", Attribute::Int(1))],
    );
    g.add_output(seq);
    let mut s = InferenceSession::from_graph(g).expect("build");
    let mut outputs = s.run_outputs(&[]).expect("run");
    let sequence = outputs.remove(0).into_sequence().expect("sequence output");
    assert_eq!(sequence.elem_dtype(), DataType::Float32);
    assert_eq!(sequence.length(), 0);
}

#[test]
fn sequence_construct_graph_output_succeeds() {
    let mut g = Graph::new();
    let a = f32_init(&mut g, "a", &[2], &[1., 2.]);
    let b = f32_init(&mut g, "b", &[2], &[3., 4.]);
    let seq = op(
        &mut g,
        "SequenceConstruct",
        &[a, b],
        DataType::Float32,
        &[],
        &[],
    );
    g.add_output(seq);
    let mut s = InferenceSession::from_graph(g).expect("build");
    let mut outputs = s.run_outputs(&[]).expect("run");
    let sequence = outputs.remove(0).into_sequence().expect("sequence output");
    assert_eq!(sequence.length(), 2);
    assert_eq!(
        sequence.elements()[0].contiguous_bytes().unwrap(),
        f32_bytes(&[1., 2.])
    );
    assert_eq!(
        sequence.elements()[1].contiguous_bytes().unwrap(),
        f32_bytes(&[3., 4.])
    );
    let rerun = s.run_outputs(&[]).expect("rerun with first output alive");
    assert_eq!(rerun[0].as_sequence().expect("sequence output").length(), 2);
    assert_eq!(
        sequence.elements()[0].contiguous_bytes().unwrap(),
        f32_bytes(&[1., 2.])
    );
}

#[test]
fn split_to_sequence_graph_output_succeeds() {
    let mut g = Graph::new();
    let data = f32_init(&mut g, "data", &[2, 3], &[0., 1., 2., 3., 4., 5.]);
    let seq = op(
        &mut g,
        "SplitToSequence",
        &[data],
        DataType::Float32,
        &[],
        &[("axis", Attribute::Int(1))],
    );
    g.add_output(seq);
    let mut s = InferenceSession::from_graph(g).expect("build");
    let mut outputs = s.run_outputs(&[]).expect("run");
    let sequence = outputs.remove(0).into_sequence().expect("sequence output");
    assert_eq!(sequence.length(), 3);
    assert_eq!(
        sequence.elements()[1].contiguous_bytes().unwrap(),
        f32_bytes(&[1., 4.])
    );
}

#[test]
fn sequence_at_writes_bound_output_bytes() {
    let mut g = Graph::new();
    let a = f32_init(&mut g, "a", &[2], &[1., 2.]);
    let b = f32_init(&mut g, "b", &[2], &[3., 4.]);
    let seq = op(
        &mut g,
        "SequenceConstruct",
        &[a, b],
        DataType::Float32,
        &[],
        &[],
    );
    let idx = i64_init(&mut g, "idx", &[], &[1]);
    let output = op(
        &mut g,
        "SequenceAt",
        &[seq, idx],
        DataType::Float32,
        &[2],
        &[],
    );
    g.value_mut(output).name = Some("output".to_string());
    g.add_output(output);
    let mut s = InferenceSession::from_graph(g).expect("build");
    let mut binding = s
        .allocate_device_output_binding("output", DataType::Float32, vec![2], vec![2])
        .expect("binding");
    binding.write_bytes(0, &f32_bytes(&[-1., -1.])).unwrap();
    let outputs = s
        .run_with_device_bindings(&[], std::slice::from_mut(&mut binding))
        .expect("run");
    assert!(outputs[0].is_none());
    assert_eq!(binding.read_bytes().unwrap(), f32_bytes(&[3., 4.]));
}

#[test]
fn sequence_length_writes_bound_output_bytes() {
    let mut g = Graph::new();
    let a = f32_init(&mut g, "a", &[1], &[1.]);
    let b = f32_init(&mut g, "b", &[1], &[2.]);
    let seq = op(
        &mut g,
        "SequenceConstruct",
        &[a, b],
        DataType::Float32,
        &[],
        &[],
    );
    let output = op(&mut g, "SequenceLength", &[seq], DataType::Int64, &[], &[]);
    g.value_mut(output).name = Some("output".to_string());
    g.add_output(output);
    let mut s = InferenceSession::from_graph(g).expect("build");
    let mut binding = s
        .allocate_device_output_binding("output", DataType::Int64, vec![], vec![])
        .expect("binding");
    binding.write_bytes(0, &(-1i64).to_le_bytes()).unwrap();
    let outputs = s
        .run_with_device_bindings(&[], std::slice::from_mut(&mut binding))
        .expect("run");
    assert!(outputs[0].is_none());
    assert_eq!(binding.read_bytes().unwrap()[..8], 2i64.to_le_bytes());
}

#[test]
fn concat_from_sequence_writes_bound_output_bytes() {
    let mut g = Graph::new();
    let a = f32_init(&mut g, "a", &[2], &[1., 2.]);
    let b = f32_init(&mut g, "b", &[2], &[3., 4.]);
    let seq = op(
        &mut g,
        "SequenceConstruct",
        &[a, b],
        DataType::Float32,
        &[],
        &[],
    );
    let output = op(
        &mut g,
        "ConcatFromSequence",
        &[seq],
        DataType::Float32,
        &[4],
        &[("axis", Attribute::Int(0))],
    );
    g.value_mut(output).name = Some("output".to_string());
    g.add_output(output);
    let mut s = InferenceSession::from_graph(g).expect("build");
    let mut binding = s
        .allocate_device_output_binding("output", DataType::Float32, vec![4], vec![4])
        .expect("binding");
    binding
        .write_bytes(0, &f32_bytes(&[-1., -1., -1., -1.]))
        .unwrap();
    let outputs = s
        .run_with_device_bindings(&[], std::slice::from_mut(&mut binding))
        .expect("run");
    assert!(outputs[0].is_none());
    assert_eq!(binding.read_bytes().unwrap(), f32_bytes(&[1., 2., 3., 4.]));
}