oxionnx 0.1.1

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
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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
//! Conv-related fusion passes:
//! - Conv + BatchNorm folding (weight baking)
//! - Conv + Relu / Clip activation fusion
//! - Conv + Clip(0,6) → Conv with ReLU6 activation
//! - Standalone BatchNorm folding (Mul + Add replacement)

use crate::graph::{Attributes, Node, OpKind};
use crate::tensor::Tensor;
use std::collections::{HashMap, HashSet};

/// Conv + BatchNorm fusion
/// Pattern: node A = Conv(X, W, B), node B = BatchNorm(A.output, scale, bias, mean, var)
/// Fused: Conv with modified weights and bias
pub fn fuse_conv_batchnorm(nodes: Vec<Node>, weights: &mut HashMap<String, Tensor>) -> Vec<Node> {
    if nodes.len() < 2 {
        return nodes;
    }

    let mut producer: HashMap<String, usize> = HashMap::new();
    for (i, node) in nodes.iter().enumerate() {
        for out in &node.outputs {
            producer.insert(out.clone(), i);
        }
    }

    let mut consumer_count: HashMap<String, usize> = HashMap::new();
    for node in &nodes {
        for inp in &node.inputs {
            if !inp.is_empty() {
                *consumer_count.entry(inp.clone()).or_insert(0) += 1;
            }
        }
    }

    let mut skip: HashSet<usize> = HashSet::new();
    let mut replacements: HashMap<usize, Node> = HashMap::new();

    for (i, node) in nodes.iter().enumerate() {
        if skip.contains(&i) {
            continue;
        }
        if !matches!(node.op, OpKind::BatchNorm) {
            continue;
        }
        if node.inputs.len() < 5 {
            continue;
        }

        let conv_tensor = &node.inputs[0];
        let bn_scale_name = &node.inputs[1];
        let bn_bias_name = &node.inputs[2];
        let bn_mean_name = &node.inputs[3];
        let bn_var_name = &node.inputs[4];

        if consumer_count.get(conv_tensor).copied().unwrap_or(0) != 1 {
            continue;
        }

        let conv_idx = match producer.get(conv_tensor) {
            Some(&idx) => idx,
            None => continue,
        };

        if !matches!(nodes[conv_idx].op, OpKind::Conv) {
            continue;
        }

        let bn_scale = match weights.get(bn_scale_name) {
            Some(t) => t.clone(),
            None => continue,
        };
        let bn_bias = match weights.get(bn_bias_name) {
            Some(t) => t.clone(),
            None => continue,
        };
        let bn_mean = match weights.get(bn_mean_name) {
            Some(t) => t.clone(),
            None => continue,
        };
        let bn_var = match weights.get(bn_var_name) {
            Some(t) => t.clone(),
            None => continue,
        };
        let epsilon = node.attrs.floats.get("epsilon").copied().unwrap_or(1e-5);

        let conv_node = &nodes[conv_idx];
        if conv_node.inputs.len() < 2 {
            continue;
        }
        let conv_weight_name = &conv_node.inputs[1];
        let conv_bias_name = conv_node.inputs.get(2).cloned();

        let conv_weight = match weights.get(conv_weight_name) {
            Some(t) => t.clone(),
            None => continue,
        };

        let c_out = bn_scale.data.len();
        if c_out == 0 || conv_weight.data.len() % c_out != 0 {
            continue;
        }
        let weight_per_channel: usize = conv_weight.data.len() / c_out;

        let mut fused_weight = conv_weight.data.clone();
        let mut fused_bias = vec![0.0f32; c_out];

        let conv_bias_data = if let Some(ref name) = conv_bias_name {
            if let Some(b) = weights.get(name) {
                b.data.clone()
            } else {
                vec![0.0f32; c_out]
            }
        } else {
            vec![0.0f32; c_out]
        };

        for c in 0..c_out {
            let inv_std = 1.0 / (bn_var.data[c] + epsilon).sqrt();
            let factor = bn_scale.data[c] * inv_std;

            let start = c * weight_per_channel;
            for w in &mut fused_weight[start..start + weight_per_channel] {
                *w *= factor;
            }

            fused_bias[c] = (conv_bias_data[c] - bn_mean.data[c]) * factor + bn_bias.data[c];
        }

        let fused_weight_name = format!("{}_fused_weight", conv_node.name);
        let fused_bias_name = format!("{}_fused_bias", conv_node.name);
        weights.insert(
            fused_weight_name.clone(),
            Tensor::new(fused_weight, conv_weight.shape.clone()),
        );
        weights.insert(
            fused_bias_name.clone(),
            Tensor::new(fused_bias, vec![c_out]),
        );

        let fused_inputs = vec![
            conv_node.inputs[0].clone(),
            fused_weight_name,
            fused_bias_name,
        ];
        let fused_conv = Node {
            op: OpKind::Conv,
            name: format!("{}_fused_convbn", conv_node.name),
            inputs: fused_inputs,
            outputs: node.outputs.clone(),
            attrs: conv_node.attrs.clone(),
        };

        replacements.insert(conv_idx, fused_conv);
        skip.insert(i);
    }

    nodes
        .into_iter()
        .enumerate()
        .filter(|(i, _)| !skip.contains(i))
        .map(|(i, n)| replacements.remove(&i).unwrap_or(n))
        .collect()
}

/// Conv + Relu/Clip fusion.
/// Pattern: Conv node -> Relu node (or Clip with min=0, max=inf)
/// Merges activation into the Conv node as an attribute.
pub fn fuse_conv_relu(nodes: Vec<Node>) -> Vec<Node> {
    if nodes.len() < 2 {
        return nodes;
    }

    let mut producer: HashMap<String, usize> = HashMap::new();
    for (i, node) in nodes.iter().enumerate() {
        for out in &node.outputs {
            producer.insert(out.clone(), i);
        }
    }

    let mut consumer_count: HashMap<String, usize> = HashMap::new();
    for node in &nodes {
        for inp in &node.inputs {
            if !inp.is_empty() {
                *consumer_count.entry(inp.clone()).or_insert(0) += 1;
            }
        }
    }

    let mut skip: HashSet<usize> = HashSet::new();
    let mut replacements: HashMap<usize, Node> = HashMap::new();

    for (i, node) in nodes.iter().enumerate() {
        if skip.contains(&i) {
            continue;
        }

        let is_relu = matches!(node.op, OpKind::Relu);
        let is_clip = matches!(node.op, OpKind::Clip);
        if !is_relu && !is_clip {
            continue;
        }

        if node.inputs.is_empty() {
            continue;
        }

        if is_clip {
            let min_val = node.attrs.f("min", f32::NEG_INFINITY);
            if min_val != 0.0 && min_val != f32::NEG_INFINITY {
                continue;
            }
        }

        let conv_tensor = &node.inputs[0];

        if consumer_count.get(conv_tensor).copied().unwrap_or(0) != 1 {
            continue;
        }

        let conv_idx = match producer.get(conv_tensor) {
            Some(&idx) => idx,
            None => continue,
        };

        if !matches!(nodes[conv_idx].op, OpKind::Conv) {
            continue;
        }

        let mut fused_attrs = nodes[conv_idx].attrs.clone();

        if is_relu {
            fused_attrs
                .strings
                .insert("activation".to_string(), "relu".to_string());
        } else {
            let min_val = node.attrs.f("min", f32::NEG_INFINITY);
            let max_val = node.attrs.f("max", f32::INFINITY);
            if min_val == 0.0 && max_val == f32::INFINITY {
                fused_attrs
                    .strings
                    .insert("activation".to_string(), "relu".to_string());
            } else {
                fused_attrs
                    .strings
                    .insert("activation".to_string(), "clip".to_string());
                fused_attrs
                    .floats
                    .insert("activation_min".to_string(), min_val);
                fused_attrs
                    .floats
                    .insert("activation_max".to_string(), max_val);
            }
        }

        let fused = Node {
            op: OpKind::Conv,
            name: format!("{}_fused_activation", nodes[conv_idx].name),
            inputs: nodes[conv_idx].inputs.clone(),
            outputs: node.outputs.clone(),
            attrs: fused_attrs,
        };

        replacements.insert(conv_idx, fused);
        skip.insert(i);
    }

    nodes
        .into_iter()
        .enumerate()
        .filter(|(i, _)| !skip.contains(i))
        .map(|(i, n)| replacements.remove(&i).unwrap_or(n))
        .collect()
}

/// Conv + Clip(min=0, max=6) → Conv with ReLU6 activation.
///
/// MobileNet and EfficientNet architectures use ReLU6 (Clip(0, 6)) extensively
/// after convolutions.  This pass specifically recognises the ReLU6 pattern and
/// marks the fused Conv with `activation = "relu6"`, allowing execution engines
/// to dispatch a dedicated fused kernel.
///
/// This is complementary to `fuse_conv_relu` which handles plain Relu and
/// general Clip ranges.  ReLU6 gets its own label because many hardware
/// accelerators have a dedicated ReLU6 instruction.
///
/// Conditions:
/// - Clip's min attribute == 0.0 and max attribute == 6.0.
/// - Clip's sole input comes from a Conv node with a single consumer.
pub fn fuse_conv_clip_to_conv_relu6(nodes: Vec<Node>) -> Vec<Node> {
    if nodes.len() < 2 {
        return nodes;
    }

    let mut producer: HashMap<String, usize> = HashMap::new();
    for (i, node) in nodes.iter().enumerate() {
        for out in &node.outputs {
            producer.insert(out.clone(), i);
        }
    }

    let mut consumer_count: HashMap<String, usize> = HashMap::new();
    for node in &nodes {
        for inp in &node.inputs {
            if !inp.is_empty() {
                *consumer_count.entry(inp.clone()).or_insert(0) += 1;
            }
        }
    }

    let mut skip: HashSet<usize> = HashSet::new();
    let mut replacements: HashMap<usize, Node> = HashMap::new();

    for (i, node) in nodes.iter().enumerate() {
        if skip.contains(&i) {
            continue;
        }
        if !matches!(node.op, OpKind::Clip) {
            continue;
        }
        if node.inputs.is_empty() {
            continue;
        }

        // Must be exactly ReLU6: min=0, max=6
        let min_val = node.attrs.f("min", f32::NEG_INFINITY);
        let max_val = node.attrs.f("max", f32::INFINITY);
        if (min_val - 0.0).abs() > 1e-7 || (max_val - 6.0).abs() > 1e-7 {
            continue;
        }

        let conv_tensor = &node.inputs[0];

        if consumer_count.get(conv_tensor).copied().unwrap_or(0) != 1 {
            continue;
        }

        let conv_idx = match producer.get(conv_tensor) {
            Some(&idx) => idx,
            None => continue,
        };
        if skip.contains(&conv_idx) {
            continue;
        }
        if !matches!(nodes[conv_idx].op, OpKind::Conv) {
            continue;
        }

        let mut fused_attrs = nodes[conv_idx].attrs.clone();
        fused_attrs
            .strings
            .insert("activation".to_string(), "relu6".to_string());
        fused_attrs.floats.insert("activation_min".to_string(), 0.0);
        fused_attrs.floats.insert("activation_max".to_string(), 6.0);

        let fused = Node {
            op: OpKind::Conv,
            name: format!("{}_fused_relu6", nodes[conv_idx].name),
            inputs: nodes[conv_idx].inputs.clone(),
            outputs: node.outputs.clone(),
            attrs: fused_attrs,
        };

        replacements.insert(conv_idx, fused);
        skip.insert(i);
    }

    nodes
        .into_iter()
        .enumerate()
        .filter(|(i, _)| !skip.contains(i))
        .map(|(i, n)| replacements.remove(&i).unwrap_or(n))
        .collect()
}

/// Standalone BatchNormalization folding (inference mode).
///
/// When all BatchNorm parameters (scale, bias, mean, var) are known constants
/// and the BatchNorm node is *not* preceded by a Conv (that case is handled by
/// `fuse_conv_batchnorm`), fold the normalisation into a Mul + Add pair with
/// pre-computed constant weights:
///
/// ```text
/// factor = scale / sqrt(var + epsilon)
/// shift  = bias - mean * factor
/// y      = factor * x + shift
/// ```
///
/// This eliminates the runtime overhead of computing mean/var lookups and the
/// sqrt/div at inference time.
pub fn fold_batch_norm_inference(
    nodes: Vec<Node>,
    weights: &mut HashMap<String, Tensor>,
) -> Vec<Node> {
    if nodes.is_empty() {
        return nodes;
    }

    let mut producer: HashMap<String, usize> = HashMap::new();
    for (i, node) in nodes.iter().enumerate() {
        for out in &node.outputs {
            producer.insert(out.clone(), i);
        }
    }

    let mut skip: HashSet<usize> = HashSet::new();
    let mut new_nodes: Vec<(usize, Vec<Node>)> = Vec::new();

    for (i, node) in nodes.iter().enumerate() {
        if skip.contains(&i) {
            continue;
        }
        if !matches!(node.op, OpKind::BatchNorm) {
            continue;
        }
        if node.inputs.len() < 5 {
            continue;
        }

        let x_name = &node.inputs[0];
        let bn_scale_name = &node.inputs[1];
        let bn_bias_name = &node.inputs[2];
        let bn_mean_name = &node.inputs[3];
        let bn_var_name = &node.inputs[4];

        // Skip if preceded by Conv (handled by fuse_conv_batchnorm)
        if let Some(&prev_idx) = producer.get(x_name) {
            if matches!(nodes[prev_idx].op, OpKind::Conv) {
                continue;
            }
        }

        // All BN params must be constant weights
        let bn_scale = match weights.get(bn_scale_name) {
            Some(t) => t.clone(),
            None => continue,
        };
        let bn_bias = match weights.get(bn_bias_name) {
            Some(t) => t.clone(),
            None => continue,
        };
        let bn_mean = match weights.get(bn_mean_name) {
            Some(t) => t.clone(),
            None => continue,
        };
        let bn_var = match weights.get(bn_var_name) {
            Some(t) => t.clone(),
            None => continue,
        };

        let epsilon = node.attrs.floats.get("epsilon").copied().unwrap_or(1e-5);
        let c_out = bn_scale.data.len();

        // Validate shapes are consistent
        if c_out == 0
            || bn_bias.data.len() != c_out
            || bn_mean.data.len() != c_out
            || bn_var.data.len() != c_out
        {
            continue;
        }

        // Compute factor = scale / sqrt(var + eps)  and  shift = bias - mean * factor
        let mut factor_data = Vec::with_capacity(c_out);
        let mut shift_data = Vec::with_capacity(c_out);
        for c in 0..c_out {
            let inv_std = 1.0 / (bn_var.data[c] + epsilon).sqrt();
            let f = bn_scale.data[c] * inv_std;
            factor_data.push(f);
            shift_data.push(bn_bias.data[c] - bn_mean.data[c] * f);
        }

        let factor_name = format!("{}_bn_factor", node.name);
        let shift_name = format!("{}_bn_shift", node.name);
        let mul_out_name = format!("{}_bn_mul_out", node.name);

        weights.insert(factor_name.clone(), Tensor::new(factor_data, vec![c_out]));
        weights.insert(shift_name.clone(), Tensor::new(shift_data, vec![c_out]));

        // Emit Mul(X, factor) → Add(mul_out, shift)
        let mul_node = Node {
            op: OpKind::Mul,
            name: format!("{}_bn_mul", node.name),
            inputs: vec![x_name.clone(), factor_name],
            outputs: vec![mul_out_name.clone()],
            attrs: Attributes::default(),
        };
        let add_node = Node {
            op: OpKind::Add,
            name: format!("{}_bn_add", node.name),
            inputs: vec![mul_out_name, shift_name],
            outputs: node.outputs.clone(),
            attrs: Attributes::default(),
        };

        skip.insert(i);
        new_nodes.push((i, vec![mul_node, add_node]));
    }

    // Build result: replace skipped BN nodes with their Mul+Add replacements
    let mut result = Vec::with_capacity(nodes.len() + new_nodes.len());
    let replacement_map: HashMap<usize, Vec<Node>> = new_nodes.into_iter().collect();

    for (i, node) in nodes.into_iter().enumerate() {
        if let Some(replacement_nodes) = replacement_map.get(&i) {
            result.extend(replacement_nodes.iter().cloned());
        } else if !skip.contains(&i) {
            result.push(node);
        }
    }

    result
}

/// Conv + Add + ReLU fusion (ResNet residual block pattern).
///
/// Pattern: `Conv(X, W, B) → Add(conv_out, residual) → Relu`
///
/// This is the core building block in ResNet architectures.  The three-op
/// sequence can be fused into a single `ConvAddRelu` node that computes
/// `relu(conv(X, W, B) + residual)` in one pass, eliminating two intermediate
/// tensors and enabling execution engines to use a single fused kernel.
///
/// The fused node has inputs `[X, W, B, residual]`.  If the original Conv has
/// no bias, an empty string is used for the B slot.
///
/// Conditions:
/// - Conv output has exactly one consumer (the Add node).
/// - Add output has exactly one consumer (the Relu node).
/// - One of Add's inputs comes from Conv; the other is the residual / skip connection.
pub fn fuse_conv_add_relu(nodes: Vec<Node>) -> Vec<Node> {
    if nodes.len() < 3 {
        return nodes;
    }

    let mut producer: HashMap<String, usize> = HashMap::new();
    for (i, node) in nodes.iter().enumerate() {
        for out in &node.outputs {
            producer.insert(out.clone(), i);
        }
    }

    let mut consumer_count: HashMap<String, usize> = HashMap::new();
    for node in &nodes {
        for inp in &node.inputs {
            if !inp.is_empty() {
                *consumer_count.entry(inp.clone()).or_insert(0) += 1;
            }
        }
    }

    let mut skip: HashSet<usize> = HashSet::new();
    let mut replacements: HashMap<usize, Node> = HashMap::new();

    for (i, node) in nodes.iter().enumerate() {
        if skip.contains(&i) {
            continue;
        }
        if !matches!(node.op, OpKind::Relu) {
            continue;
        }
        if node.inputs.is_empty() {
            continue;
        }

        let relu_input = &node.inputs[0];

        // Relu input must have exactly one consumer (this Relu)
        if consumer_count.get(relu_input).copied().unwrap_or(0) != 1 {
            continue;
        }

        // Find the Add node producing the Relu's input
        let add_idx = match producer.get(relu_input) {
            Some(&idx) => idx,
            None => continue,
        };
        if skip.contains(&add_idx) {
            continue;
        }
        if !matches!(nodes[add_idx].op, OpKind::Add) {
            continue;
        }
        if nodes[add_idx].inputs.len() < 2 {
            continue;
        }

        // Identify which Add input comes from Conv and which is the residual.
        // Try both orderings: Add(conv_out, residual) and Add(residual, conv_out).
        let add_inp0 = &nodes[add_idx].inputs[0];
        let add_inp1 = &nodes[add_idx].inputs[1];

        let (conv_idx, residual_name) = {
            let try_find_conv =
                |conv_candidate: &str, residual_candidate: &str| -> Option<(usize, String)> {
                    // Conv output must have exactly one consumer (this Add)
                    if consumer_count.get(conv_candidate).copied().unwrap_or(0) != 1 {
                        return None;
                    }
                    let idx = producer.get(conv_candidate)?;
                    if skip.contains(idx) {
                        return None;
                    }
                    if !matches!(nodes[*idx].op, OpKind::Conv) {
                        return None;
                    }
                    Some((*idx, residual_candidate.to_string()))
                };

            match try_find_conv(add_inp0, add_inp1).or_else(|| try_find_conv(add_inp1, add_inp0)) {
                Some(result) => result,
                None => continue,
            }
        };

        let conv_node = &nodes[conv_idx];

        // Build fused ConvAddRelu node
        // Inputs: [X, W, B (or ""), residual]
        let mut fused_inputs = Vec::with_capacity(4);
        // X (data input)
        if conv_node.inputs.is_empty() {
            continue;
        }
        fused_inputs.push(conv_node.inputs[0].clone());
        // W (weight)
        if conv_node.inputs.len() < 2 {
            continue;
        }
        fused_inputs.push(conv_node.inputs[1].clone());
        // B (bias; may be absent)
        fused_inputs.push(conv_node.inputs.get(2).cloned().unwrap_or_default());
        // residual (skip connection)
        fused_inputs.push(residual_name);

        let mut fused_attrs = conv_node.attrs.clone();
        fused_attrs
            .strings
            .insert("fused_ops".to_string(), "Add,Relu".to_string());

        let fused = Node {
            op: OpKind::ConvAddRelu,
            name: format!("{}_fused_conv_add_relu", conv_node.name),
            inputs: fused_inputs,
            outputs: node.outputs.clone(),
            attrs: fused_attrs,
        };

        replacements.insert(conv_idx, fused);
        skip.insert(add_idx);
        skip.insert(i);
    }

    nodes
        .into_iter()
        .enumerate()
        .filter(|(i, _)| !skip.contains(i))
        .map(|(i, n)| replacements.remove(&i).unwrap_or(n))
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::optimizer::test_utils::make_node;

    #[test]
    fn test_fuse_conv_batchnorm() {
        let conv = make_node(
            OpKind::Conv,
            "conv",
            vec!["x", "conv_w", "conv_b"],
            vec!["conv_out"],
        );
        let mut bn = make_node(
            OpKind::BatchNorm,
            "bn",
            vec!["conv_out", "bn_scale", "bn_bias", "bn_mean", "bn_var"],
            vec!["bn_out"],
        );
        bn.attrs.floats.insert("epsilon".to_string(), 1e-5);

        let nodes = vec![conv, bn];
        let mut weights = HashMap::new();
        weights.insert(
            "conv_w".to_string(),
            Tensor::new(vec![1.0], vec![1, 1, 1, 1]),
        );
        weights.insert("conv_b".to_string(), Tensor::new(vec![0.0], vec![1]));
        weights.insert("bn_scale".to_string(), Tensor::new(vec![1.0], vec![1]));
        weights.insert("bn_bias".to_string(), Tensor::new(vec![0.0], vec![1]));
        weights.insert("bn_mean".to_string(), Tensor::new(vec![0.0], vec![1]));
        weights.insert("bn_var".to_string(), Tensor::new(vec![1.0], vec![1]));

        let result = fuse_conv_batchnorm(nodes, &mut weights);
        assert_eq!(result.len(), 1);
        assert!(matches!(result[0].op, OpKind::Conv));
        assert_eq!(result[0].outputs[0], "bn_out");
        assert!(weights.contains_key("conv_fused_weight"));
        assert!(weights.contains_key("conv_fused_bias"));
    }

    #[test]
    fn test_fuse_conv_batchnorm_no_conv_bias() {
        let conv = make_node(OpKind::Conv, "conv", vec!["x", "conv_w"], vec!["conv_out"]);
        let mut bn = make_node(
            OpKind::BatchNorm,
            "bn",
            vec!["conv_out", "bn_scale", "bn_bias", "bn_mean", "bn_var"],
            vec!["bn_out"],
        );
        bn.attrs.floats.insert("epsilon".to_string(), 1e-5);

        let nodes = vec![conv, bn];
        let mut weights = HashMap::new();
        weights.insert(
            "conv_w".to_string(),
            Tensor::new(vec![2.0], vec![1, 1, 1, 1]),
        );
        weights.insert("bn_scale".to_string(), Tensor::new(vec![3.0], vec![1]));
        weights.insert("bn_bias".to_string(), Tensor::new(vec![0.5], vec![1]));
        weights.insert("bn_mean".to_string(), Tensor::new(vec![1.0], vec![1]));
        weights.insert("bn_var".to_string(), Tensor::new(vec![4.0], vec![1]));

        let result = fuse_conv_batchnorm(nodes, &mut weights);
        assert_eq!(result.len(), 1);

        let fused_w = weights.get("conv_fused_weight").expect("fused weight");
        let inv_std = 1.0 / (4.0f32 + 1e-5).sqrt();
        let expected_w = 2.0 * 3.0 * inv_std;
        assert!((fused_w.data[0] - expected_w).abs() < 1e-5);

        let fused_b = weights.get("conv_fused_bias").expect("fused bias");
        let expected_b = (0.0 - 1.0) * 3.0 * inv_std + 0.5;
        assert!((fused_b.data[0] - expected_b).abs() < 1e-5);
    }

    #[test]
    fn test_fuse_conv_batchnorm_multiple_consumers() {
        let conv = make_node(
            OpKind::Conv,
            "conv",
            vec!["x", "conv_w", "conv_b"],
            vec!["conv_out"],
        );
        let mut bn = make_node(
            OpKind::BatchNorm,
            "bn",
            vec!["conv_out", "bn_scale", "bn_bias", "bn_mean", "bn_var"],
            vec!["bn_out"],
        );
        bn.attrs.floats.insert("epsilon".to_string(), 1e-5);
        let relu = make_node(OpKind::Relu, "relu", vec!["conv_out"], vec!["relu_out"]);

        let nodes = vec![conv, bn, relu];
        let mut weights = HashMap::new();
        weights.insert(
            "conv_w".to_string(),
            Tensor::new(vec![1.0], vec![1, 1, 1, 1]),
        );
        weights.insert("conv_b".to_string(), Tensor::new(vec![0.0], vec![1]));
        weights.insert("bn_scale".to_string(), Tensor::new(vec![1.0], vec![1]));
        weights.insert("bn_bias".to_string(), Tensor::new(vec![0.0], vec![1]));
        weights.insert("bn_mean".to_string(), Tensor::new(vec![0.0], vec![1]));
        weights.insert("bn_var".to_string(), Tensor::new(vec![1.0], vec![1]));

        let result = fuse_conv_batchnorm(nodes, &mut weights);
        assert_eq!(result.len(), 3);
    }

    #[test]
    fn test_fuse_conv_relu() {
        let conv = make_node(OpKind::Conv, "conv", vec!["x", "w", "b"], vec!["conv_out"]);
        let relu = make_node(OpKind::Relu, "relu", vec!["conv_out"], vec!["relu_out"]);

        let nodes = vec![conv, relu];
        let result = fuse_conv_relu(nodes);

        assert_eq!(result.len(), 1);
        assert!(matches!(result[0].op, OpKind::Conv));
        assert_eq!(result[0].outputs[0], "relu_out");
        assert_eq!(result[0].attrs.s("activation"), "relu");
    }

    #[test]
    fn test_fuse_conv_clip_as_relu() {
        let conv = make_node(OpKind::Conv, "conv", vec!["x", "w"], vec!["conv_out"]);
        let mut clip = make_node(OpKind::Clip, "clip", vec!["conv_out"], vec!["clip_out"]);
        clip.attrs.floats.insert("min".to_string(), 0.0);
        clip.attrs.floats.insert("max".to_string(), f32::INFINITY);

        let nodes = vec![conv, clip];
        let result = fuse_conv_relu(nodes);

        assert_eq!(result.len(), 1);
        assert!(matches!(result[0].op, OpKind::Conv));
        assert_eq!(result[0].outputs[0], "clip_out");
        assert_eq!(result[0].attrs.s("activation"), "relu");
    }

    #[test]
    fn test_fuse_conv_clip_general() {
        let conv = make_node(OpKind::Conv, "conv", vec!["x", "w"], vec!["conv_out"]);
        let mut clip = make_node(OpKind::Clip, "clip", vec!["conv_out"], vec!["clip_out"]);
        clip.attrs.floats.insert("min".to_string(), 0.0);
        clip.attrs.floats.insert("max".to_string(), 6.0);

        let nodes = vec![conv, clip];
        let result = fuse_conv_relu(nodes);

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].attrs.s("activation"), "clip");
        assert_eq!(result[0].attrs.f("activation_min", -1.0), 0.0);
        assert_eq!(result[0].attrs.f("activation_max", -1.0), 6.0);
    }

    #[test]
    fn test_fuse_conv_relu_no_fusion_multiple_consumers() {
        let conv = make_node(OpKind::Conv, "conv", vec!["x", "w"], vec!["conv_out"]);
        let relu = make_node(OpKind::Relu, "relu", vec!["conv_out"], vec!["relu_out"]);
        let add = make_node(
            OpKind::Add,
            "add",
            vec!["conv_out", "other"],
            vec!["add_out"],
        );

        let nodes = vec![conv, relu, add];
        let result = fuse_conv_relu(nodes);

        assert_eq!(result.len(), 3);
    }

    // --- fuse_conv_clip_to_conv_relu6 tests ---

    #[test]
    fn test_fuse_conv_clip_to_conv_relu6_basic() {
        let conv = make_node(OpKind::Conv, "conv", vec!["x", "w"], vec!["conv_out"]);
        let mut clip = make_node(OpKind::Clip, "clip", vec!["conv_out"], vec!["clip_out"]);
        clip.attrs.floats.insert("min".to_string(), 0.0);
        clip.attrs.floats.insert("max".to_string(), 6.0);

        let nodes = vec![conv, clip];
        let result = fuse_conv_clip_to_conv_relu6(nodes);

        assert_eq!(result.len(), 1);
        assert!(matches!(result[0].op, OpKind::Conv));
        assert_eq!(result[0].attrs.s("activation"), "relu6");
        assert_eq!(result[0].attrs.f("activation_min", -1.0), 0.0);
        assert_eq!(result[0].attrs.f("activation_max", -1.0), 6.0);
        assert_eq!(result[0].outputs, vec!["clip_out"]);
    }

    #[test]
    fn test_fuse_conv_clip_to_conv_relu6_wrong_range() {
        let conv = make_node(OpKind::Conv, "conv", vec!["x", "w"], vec!["conv_out"]);
        let mut clip = make_node(OpKind::Clip, "clip", vec!["conv_out"], vec!["clip_out"]);
        clip.attrs.floats.insert("min".to_string(), 0.0);
        clip.attrs.floats.insert("max".to_string(), 1.0); // Not 6.0

        let nodes = vec![conv, clip];
        let result = fuse_conv_clip_to_conv_relu6(nodes);

        // Not ReLU6 range, no fusion
        assert_eq!(result.len(), 2);
    }

    #[test]
    fn test_fuse_conv_clip_to_conv_relu6_not_conv() {
        // Relu followed by Clip(0,6) — not a Conv, so no fusion
        let relu = make_node(OpKind::Relu, "relu", vec!["x"], vec!["relu_out"]);
        let mut clip = make_node(OpKind::Clip, "clip", vec!["relu_out"], vec!["clip_out"]);
        clip.attrs.floats.insert("min".to_string(), 0.0);
        clip.attrs.floats.insert("max".to_string(), 6.0);

        let nodes = vec![relu, clip];
        let result = fuse_conv_clip_to_conv_relu6(nodes);

        assert_eq!(result.len(), 2);
    }

    #[test]
    fn test_fuse_conv_clip_to_conv_relu6_multiple_consumers() {
        let conv = make_node(OpKind::Conv, "conv", vec!["x", "w"], vec!["conv_out"]);
        let mut clip = make_node(OpKind::Clip, "clip", vec!["conv_out"], vec!["clip_out"]);
        clip.attrs.floats.insert("min".to_string(), 0.0);
        clip.attrs.floats.insert("max".to_string(), 6.0);
        let add = make_node(
            OpKind::Add,
            "add",
            vec!["conv_out", "other"],
            vec!["add_out"],
        );

        let nodes = vec![conv, clip, add];
        let result = fuse_conv_clip_to_conv_relu6(nodes);

        // conv_out has 2 consumers, no fusion
        assert_eq!(result.len(), 3);
    }

    // --- fold_batch_norm_inference tests ---

    #[test]
    fn test_fold_batch_norm_inference_basic() {
        let mut bn = make_node(
            OpKind::BatchNorm,
            "bn",
            vec!["x", "scale", "bias", "mean", "var"],
            vec!["bn_out"],
        );
        bn.attrs.floats.insert("epsilon".to_string(), 1e-5);

        let nodes = vec![bn];
        let mut weights = HashMap::new();
        weights.insert("scale".to_string(), Tensor::new(vec![2.0], vec![1]));
        weights.insert("bias".to_string(), Tensor::new(vec![0.5], vec![1]));
        weights.insert("mean".to_string(), Tensor::new(vec![1.0], vec![1]));
        weights.insert("var".to_string(), Tensor::new(vec![4.0], vec![1]));

        let result = fold_batch_norm_inference(nodes, &mut weights);

        // BN replaced with Mul + Add
        assert_eq!(result.len(), 2);
        assert!(matches!(result[0].op, OpKind::Mul));
        assert!(matches!(result[1].op, OpKind::Add));

        // Check outputs — final output should match BN's output
        assert_eq!(result[1].outputs, vec!["bn_out"]);
        // Mul takes X as input
        assert_eq!(result[0].inputs[0], "x");

        // Verify precomputed factor and shift
        let inv_std = 1.0 / (4.0f32 + 1e-5).sqrt();
        let expected_factor = 2.0 * inv_std;
        let expected_shift = 0.5 - 1.0 * expected_factor;

        let factor = weights.get("bn_bn_factor").expect("factor weight");
        assert!((factor.data[0] - expected_factor).abs() < 1e-5);

        let shift = weights.get("bn_bn_shift").expect("shift weight");
        assert!((shift.data[0] - expected_shift).abs() < 1e-5);
    }

    #[test]
    fn test_fold_batch_norm_inference_skips_conv_preceded() {
        // When BN is preceded by Conv, fuse_conv_batchnorm handles it
        let conv = make_node(OpKind::Conv, "conv", vec!["inp", "w"], vec!["conv_out"]);
        let mut bn = make_node(
            OpKind::BatchNorm,
            "bn",
            vec!["conv_out", "scale", "bias", "mean", "var"],
            vec!["bn_out"],
        );
        bn.attrs.floats.insert("epsilon".to_string(), 1e-5);

        let nodes = vec![conv, bn];
        let mut weights = HashMap::new();
        weights.insert("scale".to_string(), Tensor::new(vec![1.0], vec![1]));
        weights.insert("bias".to_string(), Tensor::new(vec![0.0], vec![1]));
        weights.insert("mean".to_string(), Tensor::new(vec![0.0], vec![1]));
        weights.insert("var".to_string(), Tensor::new(vec![1.0], vec![1]));

        let result = fold_batch_norm_inference(nodes, &mut weights);

        // Should NOT fold — Conv precedes BN
        assert_eq!(result.len(), 2);
        assert!(matches!(result[0].op, OpKind::Conv));
        assert!(matches!(result[1].op, OpKind::BatchNorm));
    }

    #[test]
    fn test_fold_batch_norm_inference_missing_weights() {
        let mut bn = make_node(
            OpKind::BatchNorm,
            "bn",
            vec!["x", "scale", "bias", "mean", "var"],
            vec!["bn_out"],
        );
        bn.attrs.floats.insert("epsilon".to_string(), 1e-5);

        let nodes = vec![bn];
        let mut weights = HashMap::new();
        weights.insert("scale".to_string(), Tensor::new(vec![1.0], vec![1]));
        // Missing bias, mean, var weights

        let result = fold_batch_norm_inference(nodes, &mut weights);

        // Should not fold — missing weights
        assert_eq!(result.len(), 1);
        assert!(matches!(result[0].op, OpKind::BatchNorm));
    }

    #[test]
    fn test_fold_batch_norm_inference_multi_channel() {
        let mut bn = make_node(
            OpKind::BatchNorm,
            "bn",
            vec!["x", "scale", "bias", "mean", "var"],
            vec!["bn_out"],
        );
        bn.attrs.floats.insert("epsilon".to_string(), 0.001);

        let nodes = vec![bn];
        let mut weights = HashMap::new();
        weights.insert(
            "scale".to_string(),
            Tensor::new(vec![1.0, 2.0, 3.0], vec![3]),
        );
        weights.insert(
            "bias".to_string(),
            Tensor::new(vec![0.1, 0.2, 0.3], vec![3]),
        );
        weights.insert(
            "mean".to_string(),
            Tensor::new(vec![0.5, 1.0, 1.5], vec![3]),
        );
        weights.insert("var".to_string(), Tensor::new(vec![1.0, 2.0, 4.0], vec![3]));

        let result = fold_batch_norm_inference(nodes, &mut weights);

        assert_eq!(result.len(), 2);
        assert!(matches!(result[0].op, OpKind::Mul));
        assert!(matches!(result[1].op, OpKind::Add));

        let factor = weights.get("bn_bn_factor").expect("factor");
        assert_eq!(factor.shape, vec![3]);
        let shift = weights.get("bn_bn_shift").expect("shift");
        assert_eq!(shift.shape, vec![3]);

        // Verify channel 0: scale=1.0, var=1.0, eps=0.001
        let inv_std_0 = 1.0 / (1.0f32 + 0.001).sqrt();
        let expected_f0 = 1.0 * inv_std_0;
        assert!((factor.data[0] - expected_f0).abs() < 1e-5);
    }

    #[test]
    fn test_fold_batch_norm_inference_shape_mismatch() {
        let mut bn = make_node(
            OpKind::BatchNorm,
            "bn",
            vec!["x", "scale", "bias", "mean", "var"],
            vec!["bn_out"],
        );
        bn.attrs.floats.insert("epsilon".to_string(), 1e-5);

        let nodes = vec![bn];
        let mut weights = HashMap::new();
        weights.insert("scale".to_string(), Tensor::new(vec![1.0, 2.0], vec![2]));
        weights.insert("bias".to_string(), Tensor::new(vec![0.0], vec![1])); // Mismatch!
        weights.insert("mean".to_string(), Tensor::new(vec![0.0, 0.0], vec![2]));
        weights.insert("var".to_string(), Tensor::new(vec![1.0, 1.0], vec![2]));

        let result = fold_batch_norm_inference(nodes, &mut weights);

        // Shape mismatch — should not fold
        assert_eq!(result.len(), 1);
        assert!(matches!(result[0].op, OpKind::BatchNorm));
    }

    // --- fuse_conv_add_relu tests ---

    #[test]
    fn test_fuse_conv_add_relu_basic() {
        // Conv(x, w, b) → Add(conv_out, residual) → Relu → fused ConvAddRelu
        let conv = make_node(OpKind::Conv, "conv", vec!["x", "w", "b"], vec!["conv_out"]);
        let add = make_node(
            OpKind::Add,
            "add",
            vec!["conv_out", "residual"],
            vec!["add_out"],
        );
        let relu = make_node(OpKind::Relu, "relu", vec!["add_out"], vec!["relu_out"]);

        let nodes = vec![conv, add, relu];
        let result = fuse_conv_add_relu(nodes);

        assert_eq!(result.len(), 1);
        assert!(matches!(result[0].op, OpKind::ConvAddRelu));
        assert_eq!(result[0].inputs, vec!["x", "w", "b", "residual"]);
        assert_eq!(result[0].outputs, vec!["relu_out"]);
    }

    #[test]
    fn test_fuse_conv_add_relu_reversed_add_inputs() {
        // Add(residual, conv_out) — reversed order
        let conv = make_node(OpKind::Conv, "conv", vec!["x", "w", "b"], vec!["conv_out"]);
        let add = make_node(
            OpKind::Add,
            "add",
            vec!["residual", "conv_out"],
            vec!["add_out"],
        );
        let relu = make_node(OpKind::Relu, "relu", vec!["add_out"], vec!["relu_out"]);

        let nodes = vec![conv, add, relu];
        let result = fuse_conv_add_relu(nodes);

        assert_eq!(result.len(), 1);
        assert!(matches!(result[0].op, OpKind::ConvAddRelu));
        assert_eq!(result[0].inputs, vec!["x", "w", "b", "residual"]);
    }

    #[test]
    fn test_fuse_conv_add_relu_no_bias() {
        // Conv without bias input
        let conv = make_node(OpKind::Conv, "conv", vec!["x", "w"], vec!["conv_out"]);
        let add = make_node(
            OpKind::Add,
            "add",
            vec!["conv_out", "residual"],
            vec!["add_out"],
        );
        let relu = make_node(OpKind::Relu, "relu", vec!["add_out"], vec!["relu_out"]);

        let nodes = vec![conv, add, relu];
        let result = fuse_conv_add_relu(nodes);

        assert_eq!(result.len(), 1);
        assert!(matches!(result[0].op, OpKind::ConvAddRelu));
        // Bias slot should be empty string
        assert_eq!(result[0].inputs[2], "");
        assert_eq!(result[0].inputs[3], "residual");
    }

    #[test]
    fn test_fuse_conv_add_relu_no_fusion_conv_multiple_consumers() {
        // conv_out consumed by both Add and another node — don't fuse
        let conv = make_node(OpKind::Conv, "conv", vec!["x", "w", "b"], vec!["conv_out"]);
        let add = make_node(
            OpKind::Add,
            "add",
            vec!["conv_out", "residual"],
            vec!["add_out"],
        );
        let relu = make_node(OpKind::Relu, "relu", vec!["add_out"], vec!["relu_out"]);
        let extra = make_node(OpKind::Relu, "extra", vec!["conv_out"], vec!["extra_out"]);

        let nodes = vec![conv, add, relu, extra];
        let result = fuse_conv_add_relu(nodes);

        assert_eq!(result.len(), 4);
        assert!(matches!(result[0].op, OpKind::Conv));
    }

    #[test]
    fn test_fuse_conv_add_relu_no_fusion_add_multiple_consumers() {
        // add_out consumed by both Relu and another node — don't fuse
        let conv = make_node(OpKind::Conv, "conv", vec!["x", "w", "b"], vec!["conv_out"]);
        let add = make_node(
            OpKind::Add,
            "add",
            vec!["conv_out", "residual"],
            vec!["add_out"],
        );
        let relu = make_node(OpKind::Relu, "relu", vec!["add_out"], vec!["relu_out"]);
        let extra = make_node(OpKind::Sigmoid, "extra", vec!["add_out"], vec!["extra_out"]);

        let nodes = vec![conv, add, relu, extra];
        let result = fuse_conv_add_relu(nodes);

        assert_eq!(result.len(), 4);
        assert!(matches!(result[0].op, OpKind::Conv));
    }

    #[test]
    fn test_fuse_conv_add_relu_no_fusion_not_relu() {
        // Pattern ends with Sigmoid instead of Relu — don't fuse
        let conv = make_node(OpKind::Conv, "conv", vec!["x", "w", "b"], vec!["conv_out"]);
        let add = make_node(
            OpKind::Add,
            "add",
            vec!["conv_out", "residual"],
            vec!["add_out"],
        );
        let sigmoid = make_node(OpKind::Sigmoid, "sigmoid", vec!["add_out"], vec!["sig_out"]);

        let nodes = vec![conv, add, sigmoid];
        let result = fuse_conv_add_relu(nodes);

        // No fusion: pattern requires Relu at the end
        assert_eq!(result.len(), 3);
    }
}