quantize-rs 0.7.0

Neural network quantization toolkit for ONNX models
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
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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
//! Graph-level operations for quantized ONNX models.
//!
//! Three responsibilities:
//!   1. **QDQ transform** — replace FP32 initializers with INT8 + DequantizeLinear
//!   2. **Connectivity validation** — walk the graph and verify every edge resolves
//!   3. **Opset management** — ensure the model declares a sufficient opset and
//!      upgrades deprecated op attributes when bumping across breaking opset boundaries

use crate::errors::{QuantizeError, Result};
use crate::onnx_proto::{
    attribute_proto, tensor_proto, AttributeProto, GraphProto, ModelProto, OperatorSetIdProto,
    TensorProto,
};
use std::collections::{HashMap, HashSet};

use super::quantization_nodes::{
    build_dequantize_linear_node, build_quantized_weight_tensor, build_scale_tensor,
    build_zero_point_tensor, DequantLinearNames,
};

// ===========================================================================
// Public types
// ===========================================================================

/// One weight to convert: FP32 initializer → INT8 + DequantizeLinear block.
#[derive(Debug)]
pub struct QdqWeightInput {
    /// Original initializer name (e.g., `"conv1.weight"`)
    pub original_name: String,
    /// Quantized values as i8.  For INT4 these are in [-8, 7]; for INT8 in [-128, 127].
    /// Always unpacked — one value per element.
    pub quantized_values: Vec<i8>,
    /// Quantization scales (FP32).
    /// Length 1 for per-tensor; one per channel for per-channel.
    pub scales: Vec<f32>,
    /// Zero points (INT8).
    /// Same length as `scales`.
    pub zero_points: Vec<i8>,
    /// Original bit-width (4 or 8).  Informational only — ONNX storage is always INT8.
    /// Persisted in model metadata so `load_quantized_info` can recover it.
    pub bits: u8,
    /// Per-channel quantization axis, or `None` for per-tensor.
    pub axis: Option<usize>,
}

/// Result of a graph-connectivity check.
#[derive(Debug)]
#[must_use]
pub struct ConnectivityReport {
    /// `true` if every node input resolves to a known tensor.
    pub valid: bool,
    /// Human-readable description of every dangling reference.  Empty when valid.
    pub broken_refs: Vec<String>,
}

impl ConnectivityReport {
    /// Render the report as a printable string (useful for CLI `validate` output).
    pub fn summary(&self) -> String {
        if self.valid {
            "  Graph connectivity: OK\n".to_string()
        } else {
            let mut s = format!(
                "  Graph connectivity: BROKEN ({} dangling reference{})\n",
                self.broken_refs.len(),
                if self.broken_refs.len() == 1 { "" } else { "s" }
            );
            for (i, r) in self.broken_refs.iter().enumerate() {
                s.push_str(&format!("    {}. {}\n", i + 1, r));
            }
            s
        }
    }
}

// ===========================================================================
// Connectivity validation
// ===========================================================================

/// Walk the graph and verify every node input resolves to *something*.
///
/// A valid input is exactly one of:
///   • a declared graph input (`graph.input`)
///   • an initializer name (`graph.initializer`)
///   • the output of a node that appears **earlier** in `graph.node`
///
/// This is the check ONNX Runtime performs on load — and the check that
/// v0.2.0's `validate` command skipped, letting the rename bug through.
pub fn validate_graph_connectivity(graph: &GraphProto) -> ConnectivityReport {
    let mut known: HashSet<String> = HashSet::new();

    // Seed: graph inputs + initializers are always available
    for inp in &graph.input {
        known.insert(inp.name.clone());
    }
    for init in &graph.initializer {
        known.insert(init.name.clone());
    }

    let mut broken = Vec::new();

    // Walk nodes in serialized order; each node's outputs become known afterwards
    for node in &graph.node {
        for name in &node.input {
            if name.is_empty() {
                continue; // optional input slot — empty string is valid
            }
            if !known.contains(name.as_str()) {
                broken.push(format!(
                    "Node '{}' (op={}) → unknown input '{}'",
                    node.name, node.op_type, name
                ));
            }
        }
        // Register outputs so later nodes can consume them
        for name in &node.output {
            if !name.is_empty() {
                known.insert(name.clone());
            }
        }
    }

    ConnectivityReport {
        valid: broken.is_empty(),
        broken_refs: broken,
    }
}

// ===========================================================================
// Opset version management
// ===========================================================================

/// Ensure the default ONNX domain opset is at least `min_version`.
///
/// DequantizeLinear requires opset ≥ 10 (per-tensor) or ≥ 13 (per-channel axis).
///
/// When bumping the opset past a breaking boundary, this function also
/// upgrades deprecated op attributes:
///   - **opset 9**: `BatchNormalization.spatial` removed (was always 1)
///   - **opset 12**: `Dropout.ratio` migrated from attribute to input
pub fn ensure_opset_version(model: &mut ModelProto, min_version: i64) {
    let old_version = get_opset_version(model);

    // Update or insert the default-domain opset entry
    let mut found = false;
    for opset in model.opset_import.iter_mut() {
        if opset.domain.is_empty() {
            if opset.version < min_version {
                opset.version = min_version;
            }
            found = true;
            break;
        }
    }
    if !found {
        model.opset_import.push(OperatorSetIdProto {
            domain: String::new(),
            version: min_version,
        });
    }

    // Strip deprecated attributes when crossing breaking opset boundaries
    if old_version < min_version {
        if let Some(graph) = model.graph.as_mut() {
            upgrade_deprecated_ops(graph, old_version, min_version);
        }
    }
}

/// Get the current default-domain opset version (0 if not present).
fn get_opset_version(model: &ModelProto) -> i64 {
    model
        .opset_import
        .iter()
        .find(|o| o.domain.is_empty())
        .map_or(0, |o| o.version)
}

/// Upgrade graph nodes whose attribute semantics changed between `old_opset`
/// and `new_opset`.
///
/// - **BatchNormalization** (opset 9): `spatial` attribute removed (was always 1)
/// - **Dropout** (opset 12): `ratio` attribute → 2nd input
/// - **Softmax / LogSoftmax** (opset 13): default axis changed from 1 to -1
fn upgrade_deprecated_ops(graph: &mut GraphProto, old_opset: i64, new_opset: i64) {
    let mut new_initializers: Vec<TensorProto> = Vec::new();

    for node in graph.node.iter_mut() {
        // Opset 9: BatchNormalization removed the `spatial` attribute.
        // It was always 1 (the only valid value) and had no effect.
        if node.op_type == "BatchNormalization" && old_opset < 9 && new_opset >= 9 {
            node.attribute.retain(|a| a.name != "spatial");
        }

        // Opset 12: Dropout `ratio` moved from attribute to 2nd input.
        // Extract the value, remove the attribute, and wire in a constant.
        if node.op_type == "Dropout" && old_opset < 12 && new_opset >= 12 {
            let ratio = node
                .attribute
                .iter()
                .find(|a| a.name == "ratio")
                .map(|a| a.f)
                .unwrap_or(0.5);
            node.attribute.retain(|a| a.name != "ratio");

            let init_name = format!(
                "_quantize_rs_dropout_ratio_{}",
                node.output.first().map_or("", |s| s.as_str()),
            );
            new_initializers.push(TensorProto {
                name: init_name.clone(),
                data_type: tensor_proto::DataType::Float as i32,
                float_data: vec![ratio],
                ..Default::default()
            });

            if node.input.len() < 2 {
                node.input.push(init_name);
            } else {
                node.input[1] = init_name;
            }
        }

        // Opset 13: Softmax/LogSoftmax default axis changed from 1 to -1.
        // If the node has no explicit axis attribute, add axis=1 to preserve
        // the old behavior.
        if (node.op_type == "Softmax" || node.op_type == "LogSoftmax")
            && old_opset < 13
            && new_opset >= 13
        {
            let has_axis = node.attribute.iter().any(|a| a.name == "axis");
            if !has_axis {
                node.attribute.push(AttributeProto {
                    name: "axis".to_string(),
                    r#type: attribute_proto::AttributeType::Int as i32,
                    i: 1, // old default
                    ..Default::default()
                });
            }
        }
    }

    graph.initializer.extend(new_initializers);
}

// ===========================================================================
// QDQ transform
// ===========================================================================

/// Replace FP32 weight initializers with INT8 quantized equivalents +
/// DequantizeLinear nodes.
///
/// ### What happens per weight in `inputs`:
///
/// **Removed:**
///   - Initializer `"{name}"` (the original FP32 weight data)
///
/// **Added (initializers):**
///   - `"{name}_quantized"` — INT8, same shape as original
///   - `"{name}_scale"`     — FP32 scalar
///   - `"{name}_zp"`        — INT8 scalar
///
/// **Added (node, prepended before all existing nodes):**
///   - `DequantizeLinear` with output = `"{name}"`
///
/// Because the DequantizeLinear output carries the **original** name, every
/// downstream node (Conv, MatMul, BatchNorm, …) remains completely unchanged.
/// Graph connectivity is preserved by construction.
///
/// ---
/// ### INT4 storage note
///
/// ONNX `DequantizeLinear` requires INT8 input in opsets < 21.  INT4-quantized
/// values (range [-8, 7]) are widened to INT8 here.  The quantization *accuracy*
/// is INT4-level (scale and zero_point were computed for the 4-bit range), but
/// on-disk storage is 4× compression rather than the 8× that bit-packing would
/// give.  True INT4 packing is planned for a future version (opset 21 or custom op).
pub fn apply_qdq_transform(graph: &mut GraphProto, inputs: &[QdqWeightInput]) -> Result<()> {
    // -----------------------------------------------------------------------
    // 0.  Snapshot shapes before modifying the initializer list
    // -----------------------------------------------------------------------
    let shape_map: HashMap<String, Vec<i64>> = graph
        .initializer
        .iter()
        .map(|init| (init.name.clone(), init.dims.clone()))
        .collect();

    let quant_set: HashSet<&str> = inputs.iter().map(|i| i.original_name.as_str()).collect();

    // -----------------------------------------------------------------------
    // 1.  Remove the original FP32 initializers for every weight we're replacing
    // -----------------------------------------------------------------------
    graph
        .initializer
        .retain(|init| !quant_set.contains(init.name.as_str()));

    // -----------------------------------------------------------------------
    // 1b. Also remove weights from graph.input (critical fix for "Duplicate definition")
    // -----------------------------------------------------------------------
    // Some ONNX models list weights as both initializers AND graph inputs.
    // This is valid ONNX, but when DequantizeLinear outputs reuse the original
    // weight names, ONNX Runtime sees two definitions of the same tensor.
    graph
        .input
        .retain(|inp| !quant_set.contains(inp.name.as_str()));

    // -----------------------------------------------------------------------
    // 2.  Add quantized initializer triples + build DequantizeLinear nodes
    // -----------------------------------------------------------------------
    let mut dq_nodes = Vec::new();

    for inp in inputs {
        let shape =
            shape_map
                .get(&inp.original_name)
                .ok_or_else(|| QuantizeError::GraphTransform {
                    reason: format!(
                        "Weight '{}' not found in model initializers — \
                         verify the name matches exactly",
                        inp.original_name
                    ),
                })?;

        let expected_len: i64 = shape.iter().product();
        if inp.quantized_values.len() as i64 != expected_len {
            return Err(QuantizeError::GraphTransform {
                reason: format!(
                    "Weight '{}': quantized_values has {} elements but shape {:?} expects {}",
                    inp.original_name,
                    inp.quantized_values.len(),
                    shape,
                    expected_len
                ),
            });
        }

        let names = DequantLinearNames::from_original(&inp.original_name);

        graph.initializer.push(build_quantized_weight_tensor(
            &names,
            &inp.quantized_values,
            shape,
        ));
        graph
            .initializer
            .push(build_scale_tensor(&names, &inp.scales));
        graph
            .initializer
            .push(build_zero_point_tensor(&names, &inp.zero_points));

        dq_nodes.push(build_dequantize_linear_node(&names, inp.axis));
    }

    // -----------------------------------------------------------------------
    // 3.  Prepend DequantizeLinear nodes before all existing computation nodes.
    //     They must appear first so their outputs are "known" when the validator
    //     (or ONNX Runtime) walks the node list in order.
    // -----------------------------------------------------------------------
    let existing_nodes = std::mem::take(&mut graph.node);
    graph.node = dq_nodes;
    graph.node.extend(existing_nodes);

    Ok(())
}

// ===========================================================================
// Tests
// ===========================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::onnx_proto::{
        tensor_proto, GraphProto, ModelProto, NodeProto, OperatorSetIdProto, TensorProto,
        ValueInfoProto,
    };

    // -----------------------------------------------------------------------
    // Test helpers
    // -----------------------------------------------------------------------

    /// Minimal graph: one graph input "input", one FP32 initializer "w" (shape [2,2]),
    /// one Conv node consuming both, producing "out".
    fn make_simple_graph() -> GraphProto {
        GraphProto {
            input: vec![ValueInfoProto {
                name: "input".to_string(),
                ..Default::default()
            }],
            initializer: vec![TensorProto {
                name: "w".to_string(),
                data_type: tensor_proto::DataType::Float as i32,
                dims: vec![2, 2],
                float_data: vec![1.0, 2.0, 3.0, 4.0],
                ..Default::default()
            }],
            node: vec![NodeProto {
                op_type: "Conv".to_string(),
                name: "conv0".to_string(),
                input: vec!["input".to_string(), "w".to_string()],
                output: vec!["out".to_string()],
                ..Default::default()
            }],
            ..Default::default()
        }
    }

    /// Two-weight graph: "w1" and "w2", two Conv nodes chained.
    fn make_two_weight_graph() -> GraphProto {
        GraphProto {
            input: vec![ValueInfoProto {
                name: "input".to_string(),
                ..Default::default()
            }],
            initializer: vec![
                TensorProto {
                    name: "w1".to_string(),
                    data_type: tensor_proto::DataType::Float as i32,
                    dims: vec![2, 2],
                    float_data: vec![1.0, 2.0, 3.0, 4.0],
                    ..Default::default()
                },
                TensorProto {
                    name: "w2".to_string(),
                    data_type: tensor_proto::DataType::Float as i32,
                    dims: vec![2, 2],
                    float_data: vec![5.0, 6.0, 7.0, 8.0],
                    ..Default::default()
                },
            ],
            node: vec![
                NodeProto {
                    op_type: "Conv".to_string(),
                    name: "conv1".to_string(),
                    input: vec!["input".to_string(), "w1".to_string()],
                    output: vec!["mid".to_string()],
                    ..Default::default()
                },
                NodeProto {
                    op_type: "Conv".to_string(),
                    name: "conv2".to_string(),
                    input: vec!["mid".to_string(), "w2".to_string()],
                    output: vec!["out".to_string()],
                    ..Default::default()
                },
            ],
            ..Default::default()
        }
    }

    // -----------------------------------------------------------------------
    // Connectivity validation tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_connectivity_passes_on_valid_graph() {
        let graph = make_simple_graph();
        let report = validate_graph_connectivity(&graph);
        assert!(
            report.valid,
            "original graph should be valid; broken: {:?}",
            report.broken_refs
        );
    }

    #[test]
    fn test_connectivity_detects_renamed_initializer() {
        // Simulate the exact v0.2.0 bug: rename "w" in the initializer list
        // without updating the Conv node that references it.
        let mut graph = make_simple_graph();

        for init in graph.initializer.iter_mut() {
            if init.name == "w" {
                init.name = "w__qINT8_s0.00392_z-3_len4".to_string();
            }
        }

        let report = validate_graph_connectivity(&graph);
        assert!(!report.valid, "should detect broken reference to 'w'");
        assert_eq!(report.broken_refs.len(), 1);
        assert!(
            report.broken_refs[0].contains("'w'"),
            "error should mention 'w': {}",
            report.broken_refs[0]
        );
    }

    #[test]
    fn test_connectivity_detects_multiple_broken_refs() {
        let mut graph = make_two_weight_graph();

        for init in graph.initializer.iter_mut() {
            if init.name == "w1" {
                init.name = "w1_broken".to_string();
            } else if init.name == "w2" {
                init.name = "w2_broken".to_string();
            }
        }

        let report = validate_graph_connectivity(&graph);
        assert!(!report.valid);
        assert_eq!(report.broken_refs.len(), 2);
    }

    #[test]
    fn test_connectivity_summary_formatting() {
        let valid = ConnectivityReport {
            valid: true,
            broken_refs: vec![],
        };
        assert!(valid.summary().contains("OK"));

        let broken = ConnectivityReport {
            valid: false,
            broken_refs: vec!["Node 'x' → unknown input 'y'".to_string()],
        };
        let s = broken.summary();
        assert!(s.contains("BROKEN"));
        assert!(s.contains("1 dangling reference"));
        assert!(s.contains("unknown input 'y'"));
    }

    // -----------------------------------------------------------------------
    // Opset version tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_ensure_opset_bumps_low_version() {
        let mut model = ModelProto {
            opset_import: vec![OperatorSetIdProto {
                domain: String::new(),
                version: 10,
            }],
            ..Default::default()
        };

        ensure_opset_version(&mut model, 13);

        assert_eq!(model.opset_import[0].version, 13);
    }

    #[test]
    fn test_ensure_opset_leaves_sufficient_version() {
        let mut model = ModelProto {
            opset_import: vec![OperatorSetIdProto {
                domain: String::new(),
                version: 17,
            }],
            ..Default::default()
        };

        ensure_opset_version(&mut model, 13);

        assert_eq!(model.opset_import[0].version, 17, "should not downgrade");
    }

    #[test]
    fn test_ensure_opset_adds_missing_default_domain() {
        let mut model = ModelProto::default();
        // No opset_import at all
        ensure_opset_version(&mut model, 13);

        assert_eq!(model.opset_import.len(), 1);
        assert!(model.opset_import[0].domain.is_empty());
        assert_eq!(model.opset_import[0].version, 13);
    }

    // -----------------------------------------------------------------------
    // QDQ transform tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_qdq_single_weight_produces_valid_graph() {
        let mut graph = make_simple_graph();

        let inputs = vec![QdqWeightInput {
            original_name: "w".to_string(),
            quantized_values: vec![25, 51, 76, 102],
            scales: vec![0.039_215_686], // ≈ 1/25.5
            zero_points: vec![0],
            bits: 8,
            axis: None,
        }];

        apply_qdq_transform(&mut graph, &inputs).expect("QDQ transform failed");

        let report = validate_graph_connectivity(&graph);
        assert!(
            report.valid,
            "graph after QDQ must be valid; broken: {:?}",
            report.broken_refs
        );
    }

    #[test]
    fn test_qdq_adds_correct_initializers() {
        let mut graph = make_simple_graph();

        let inputs = vec![QdqWeightInput {
            original_name: "w".to_string(),
            quantized_values: vec![10, 20, 30, 40],
            scales: vec![0.1],
            zero_points: vec![-5],
            bits: 8,
            axis: None,
        }];

        apply_qdq_transform(&mut graph, &inputs).expect("QDQ transform failed");

        let init_names: Vec<&str> = graph.initializer.iter().map(|i| i.name.as_str()).collect();

        assert!(init_names.contains(&"w_quantized"), "missing w_quantized");
        assert!(init_names.contains(&"w_scale"), "missing w_scale");
        assert!(init_names.contains(&"w_zp"), "missing w_zp");
        assert!(
            !init_names.contains(&"w"),
            "original FP32 'w' should be removed"
        );
    }

    #[test]
    fn test_qdq_node_order_dequant_first() {
        let mut graph = make_simple_graph();

        let inputs = vec![QdqWeightInput {
            original_name: "w".to_string(),
            quantized_values: vec![10, 20, 30, 40],
            scales: vec![0.1],
            zero_points: vec![0],
            bits: 8,
            axis: None,
        }];

        apply_qdq_transform(&mut graph, &inputs).expect("QDQ transform failed");

        let ops: Vec<&str> = graph.node.iter().map(|n| n.op_type.as_str()).collect();

        assert_eq!(ops.len(), 2);
        assert_eq!(ops[0], "DequantizeLinear");
        assert_eq!(ops[1], "Conv");
    }

    #[test]
    fn test_qdq_dequant_output_is_original_name() {
        let mut graph = make_simple_graph();

        let inputs = vec![QdqWeightInput {
            original_name: "w".to_string(),
            quantized_values: vec![1, 2, 3, 4],
            scales: vec![1.0],
            zero_points: vec![0],
            bits: 8,
            axis: None,
        }];

        apply_qdq_transform(&mut graph, &inputs).expect("QDQ transform failed");

        let dq = &graph.node[0]; // first node = DequantizeLinear
        assert_eq!(
            dq.output[0], "w",
            "DequantizeLinear output must be original name"
        );
    }

    #[test]
    fn test_qdq_two_weights_both_transformed() {
        let mut graph = make_two_weight_graph();

        let inputs = vec![
            QdqWeightInput {
                original_name: "w1".to_string(),
                quantized_values: vec![10, 20, 30, 40],
                scales: vec![0.1],
                zero_points: vec![0],
                bits: 8,
                axis: None,
            },
            QdqWeightInput {
                original_name: "w2".to_string(),
                quantized_values: vec![50, 60, 70, 80],
                scales: vec![0.2],
                zero_points: vec![-1],
                bits: 8,
                axis: None,
            },
        ];

        apply_qdq_transform(&mut graph, &inputs).expect("QDQ transform failed");

        // Connectivity must still be valid
        let report = validate_graph_connectivity(&graph);
        assert!(
            report.valid,
            "two-weight graph broken: {:?}",
            report.broken_refs
        );

        // Should have 2 DequantizeLinear + 2 Conv = 4 nodes
        assert_eq!(graph.node.len(), 4);

        // First two nodes are DequantizeLinear
        assert_eq!(graph.node[0].op_type, "DequantizeLinear");
        assert_eq!(graph.node[1].op_type, "DequantizeLinear");

        // Their outputs are the original weight names
        let dq_outputs: Vec<&str> = graph
            .node
            .iter()
            .take(2)
            .map(|n| n.output[0].as_str())
            .collect();
        assert!(dq_outputs.contains(&"w1"));
        assert!(dq_outputs.contains(&"w2"));
    }

    #[test]
    fn test_qdq_int4_values_stored_as_int8() {
        let mut graph = make_simple_graph();

        // INT4 range [-8, 7] — these arrive as i8 from ensure_unpacked()
        let inputs = vec![QdqWeightInput {
            original_name: "w".to_string(),
            quantized_values: vec![-8, -1, 0, 7],
            scales: vec![0.5],
            zero_points: vec![0],
            bits: 4, // flag says INT4; storage must still be INT8
            axis: None,
        }];

        apply_qdq_transform(&mut graph, &inputs).expect("QDQ transform failed");

        let quant_init = graph
            .initializer
            .iter()
            .find(|i| i.name == "w_quantized")
            .expect("w_quantized not found");

        // Data type must be INT8 (ONNX DequantizeLinear requirement)
        assert_eq!(quant_init.data_type, tensor_proto::DataType::Int8 as i32);

        // Byte-level round-trip must be exact
        let recovered: Vec<i8> = quant_init.raw_data.iter().map(|&b| b as i8).collect();
        assert_eq!(recovered, vec![-8, -1, 0, 7]);
    }

    #[test]
    fn test_qdq_unknown_weight_returns_error() {
        let mut graph = make_simple_graph();

        let inputs = vec![QdqWeightInput {
            original_name: "does_not_exist".to_string(),
            quantized_values: vec![1, 2, 3],
            scales: vec![1.0],
            zero_points: vec![0],
            bits: 8,
            axis: None,
        }];

        let result = apply_qdq_transform(&mut graph, &inputs);
        assert!(result.is_err());
        assert!(
            result.unwrap_err().to_string().contains("does_not_exist"),
            "error should name the missing weight"
        );
    }

    #[test]
    fn test_qdq_non_quantized_initializers_preserved() {
        // Add an extra initializer "bias" that is NOT being quantized.
        // It must survive the transform untouched.
        let mut graph = make_simple_graph();

        graph.initializer.push(TensorProto {
            name: "bias".to_string(),
            data_type: tensor_proto::DataType::Float as i32,
            dims: vec![2],
            float_data: vec![0.1, 0.2],
            ..Default::default()
        });

        // Also add "bias" as a Conv input so connectivity stays valid
        graph.node[0].input.push("bias".to_string());

        let inputs = vec![QdqWeightInput {
            original_name: "w".to_string(),
            quantized_values: vec![10, 20, 30, 40],
            scales: vec![0.1],
            zero_points: vec![0],
            bits: 8,
            axis: None,
        }];

        apply_qdq_transform(&mut graph, &inputs).expect("QDQ transform failed");

        // "bias" must still be present and untouched
        let bias_init = graph.initializer.iter().find(|i| i.name == "bias");

        assert!(
            bias_init.is_some(),
            "non-quantized 'bias' initializer must be preserved"
        );
        assert!((bias_init.unwrap().float_data[0] - 0.1).abs() < 1e-6);

        // Full connectivity check
        let report = validate_graph_connectivity(&graph);
        assert!(report.valid, "broken: {:?}", report.broken_refs);
    }

    #[test]
    fn test_ensure_opset_strips_deprecated_attrs() {
        use crate::onnx_proto::NodeProto;

        let mut model = ModelProto {
            opset_import: vec![OperatorSetIdProto {
                domain: String::new(),
                version: 7,
            }],
            graph: Some(GraphProto {
                node: vec![
                    // BatchNormalization with deprecated `spatial` attr (removed opset 9)
                    NodeProto {
                        op_type: "BatchNormalization".to_string(),
                        input: vec!["x".into(), "s".into(), "b".into(), "m".into(), "v".into()],
                        output: vec!["bn_out".into()],
                        attribute: vec![
                            AttributeProto {
                                name: "epsilon".to_string(),
                                r#type: attribute_proto::AttributeType::Float as i32,
                                f: 1e-5,
                                ..Default::default()
                            },
                            AttributeProto {
                                name: "spatial".to_string(),
                                r#type: attribute_proto::AttributeType::Int as i32,
                                i: 1,
                                ..Default::default()
                            },
                        ],
                        ..Default::default()
                    },
                    // Dropout with `ratio` attribute — should be migrated
                    // from attribute to 2nd input.
                    NodeProto {
                        op_type: "Dropout".to_string(),
                        input: vec!["bn_out".into()],
                        output: vec!["drop_out".into(), "drop_mask".into()],
                        attribute: vec![AttributeProto {
                            name: "ratio".to_string(),
                            r#type: attribute_proto::AttributeType::Float as i32,
                            f: 0.3,
                            ..Default::default()
                        }],
                        ..Default::default()
                    },
                    // Softmax with NO axis attribute — old default is 1,
                    // opset 13 changes default to -1, so axis=1 must be added.
                    NodeProto {
                        op_type: "Softmax".to_string(),
                        input: vec!["drop_out".into()],
                        output: vec!["sm_out".into()],
                        attribute: vec![],
                        ..Default::default()
                    },
                ],
                ..Default::default()
            }),
            ..Default::default()
        };

        ensure_opset_version(&mut model, 13);

        // Opset should be 13
        let opset = model
            .opset_import
            .iter()
            .find(|o| o.domain.is_empty())
            .unwrap();
        assert_eq!(opset.version, 13);

        let graph = model.graph.as_ref().unwrap();

        // BatchNormalization: `spatial` must be removed, `epsilon` kept
        let bn = &graph.node[0];
        assert!(
            !bn.attribute.iter().any(|a| a.name == "spatial"),
            "BatchNormalization.spatial should be stripped"
        );
        assert!(
            bn.attribute.iter().any(|a| a.name == "epsilon"),
            "BatchNormalization.epsilon should be preserved"
        );

        // Dropout: `ratio` attribute must be removed and moved to 2nd input
        let drop = &graph.node[1];
        assert!(
            !drop.attribute.iter().any(|a| a.name == "ratio"),
            "Dropout.ratio attribute should be removed"
        );
        assert_eq!(drop.input.len(), 2, "Dropout should now have 2 inputs");
        let ratio_init_name = &drop.input[1];

        // The ratio value should be stored as an initializer
        let ratio_init = graph
            .initializer
            .iter()
            .find(|i| &i.name == ratio_init_name)
            .expect("Dropout ratio initializer should exist");
        assert_eq!(ratio_init.data_type, tensor_proto::DataType::Float as i32);
        assert!(
            (ratio_init.float_data[0] - 0.3).abs() < 1e-6,
            "ratio should be 0.3"
        );

        // Softmax: must have explicit axis=1 added (old default, since opset 13 changed it to -1)
        let sm = &graph.node[2];
        assert_eq!(sm.op_type, "Softmax");
        let axis_attr = sm
            .attribute
            .iter()
            .find(|a| a.name == "axis")
            .expect("Softmax should have axis attribute added");
        assert_eq!(axis_attr.i, 1, "Softmax axis should be 1 (old default)");
    }

    #[test]
    fn test_ensure_opset_no_downgrade() {
        let mut model = ModelProto {
            opset_import: vec![OperatorSetIdProto {
                domain: String::new(),
                version: 15,
            }],
            graph: Some(GraphProto::default()),
            ..Default::default()
        };

        // Requesting 10 should NOT downgrade from 15
        ensure_opset_version(&mut model, 10);
        let opset = model
            .opset_import
            .iter()
            .find(|o| o.domain.is_empty())
            .unwrap();
        assert_eq!(opset.version, 15);
    }
}