rten 0.24.0

Machine learning runtime
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
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
// Code in this module should be careful about whether narrowing conversions
// should error, wrap or saturate depending on the context.
#![deny(clippy::as_conversions)]

use std::cell::Cell;
use std::fmt;
use std::sync::Arc;

use rten_base::bit_set::BitSet;
use rten_base::num::LeBytes;
use rten_onnx::onnx;
use rustc_hash::FxHashMap;
use smallvec::SmallVec;

use super::ReadOpError;
use crate::graph::Graph;
use crate::operator::Operator;
use crate::ops;
use crate::ops::{
    AccuracyLevel, BoxOrder, CoordTransformMode, DepthToSpaceMode, Direction, NearestMode, PadMode,
    Padding, ResizeMode, ScatterReduction,
};
use crate::value::{DataType, Scalar};

/// Deserialize operators from .onnx model files.
#[derive(Default)]
pub struct OnnxOpRegistry {
    /// Map from operator type (the `NodeProto.op_type` protobuf field) to
    /// deserialization function.
    ops: FxHashMap<OpId<'static>, &'static ReadOpFunction>,
}

impl OnnxOpRegistry {
    pub fn new() -> Self {
        OnnxOpRegistry {
            ops: FxHashMap::default(),
        }
    }

    /// Register the default/built-in implementation of an operator.
    pub fn register_op<Op: ReadOp + 'static>(&mut self) {
        self.register_op_with_factory(Op::id(), &Op::read_boxed);
    }

    /// Register an operator with a custom factory to deserialize it from a
    /// model file.
    fn register_op_with_factory(&mut self, id: OpId<'static>, factory: &'static ReadOpFunction) {
        self.ops.insert(id, factory);
    }

    /// Deserialize an operator from a model file using the operators in the
    /// registry.
    pub(crate) fn read_op(&self, op: &onnx::NodeProto, ctx: &dyn OpLoadContext) -> ReadOpResult {
        let op_type = op.op_type.as_deref().unwrap_or_default();
        let id = if let Some(domain) = op.domain.as_deref()
            && !domain.is_empty()
        {
            OpId::with_domain(domain, op_type)
        } else {
            OpId::new(op_type)
        };
        self.ops
            .get(&id)
            .ok_or_else(|| ReadOpError::OperatorUnavailable {
                name: Some(id.to_string()),
            })
            .and_then(|read_fn| read_fn(op, ctx))
    }

    pub fn with_all_ops() -> Self {
        let mut reg = OnnxOpRegistry::new();

        // As of 2025-10 there are 128 ops defined. Use a power of 2 with some
        // future headroom.
        reg.ops.reserve(256);

        macro_rules! register_op {
            ($op:ident) => {
                reg.register_op::<ops::$op>()
            };

            ($op:ident, feature=$feature:literal) => {
                #[cfg(feature = $feature)]
                reg.register_op::<ops::$op>();
                #[cfg(not(feature = $feature))]
                {
                    fn stub(_op: &onnx::NodeProto, _ctx: &dyn OpLoadContext) -> ReadOpResult {
                        Err(ReadOpError::FeatureNotEnabled {
                            name: stringify!($op).to_string(),
                            feature: $feature.to_string(),
                        })
                    }
                    let id = OpId::new(stringify!($op));
                    reg.register_op_with_factory(id, &stub);
                }
            };
        }

        // ai.onnx ops.
        register_op!(Abs);
        register_op!(Acos);
        register_op!(Add);
        register_op!(And);
        register_op!(ArgMax);
        register_op!(ArgMin);
        register_op!(Asin);
        register_op!(Atan);
        register_op!(AveragePool);
        register_op!(BatchNormalization);
        register_op!(Cast);
        register_op!(CastLike);
        register_op!(Ceil);
        register_op!(Clip);
        register_op!(Concat);
        register_op!(ConcatFromSequence);
        register_op!(Conv);
        register_op!(ConvInteger);
        register_op!(ConstantOfShape);
        register_op!(ConvTranspose);
        register_op!(Cos);
        register_op!(CumSum);
        register_op!(DequantizeLinear);
        register_op!(DepthToSpace);
        register_op!(Div);
        register_op!(Dropout, feature = "random");
        register_op!(DynamicQuantizeLinear);
        register_op!(Einsum);
        register_op!(Elu);
        register_op!(Equal);
        register_op!(Erf);
        register_op!(Exp);
        register_op!(Expand);
        register_op!(EyeLike);
        register_op!(Flatten);
        register_op!(Floor);
        register_op!(Gather);
        register_op!(GatherElements);
        register_op!(GatherND);
        register_op!(Gelu);
        register_op!(Gemm);
        register_op!(GlobalAveragePool);
        register_op!(GlobalMaxPool);
        register_op!(Greater);
        register_op!(GreaterOrEqual);
        register_op!(GridSample);
        register_op!(GRU);
        register_op!(HardSigmoid);
        register_op!(HardSwish);
        register_op!(Identity);
        register_op!(If);
        register_op!(InstanceNormalization);
        register_op!(IsInf);
        register_op!(IsNaN);
        register_op!(LayerNormalization);
        register_op!(LeakyRelu);
        register_op!(Less);
        register_op!(LessOrEqual);
        register_op!(Log);
        register_op!(LogSoftmax);
        register_op!(Loop);
        register_op!(LSTM);
        register_op!(MatMul);
        register_op!(MatMulInteger);
        register_op!(Max);
        register_op!(MaxPool);
        register_op!(Mean);
        register_op!(Min);
        register_op!(Mod);
        register_op!(Mul);
        register_op!(Neg);
        register_op!(NonMaxSuppression);
        register_op!(NonZero);
        register_op!(Not);
        register_op!(OneHot);
        register_op!(Or);
        register_op!(Pad);
        register_op!(Pow);
        register_op!(PRelu);
        register_op!(QuantizeLinear);
        register_op!(RandomNormal, feature = "random");
        register_op!(RandomNormalLike, feature = "random");
        register_op!(RandomUniform, feature = "random");
        register_op!(RandomUniformLike, feature = "random");
        register_op!(Range);
        register_op!(Reciprocal);
        register_op!(ReduceL2);
        register_op!(ReduceMax);
        register_op!(ReduceMean);
        register_op!(ReduceMin);
        register_op!(ReduceProd);
        register_op!(ReduceSum);
        register_op!(ReduceSumSquare);
        register_op!(Relu);
        register_op!(Reshape);
        register_op!(Resize);
        register_op!(Round);
        register_op!(ScatterElements);
        register_op!(ScatterND);
        register_op!(SequenceAt);
        register_op!(SequenceConstruct);
        register_op!(SequenceEmpty);
        register_op!(SequenceErase);
        register_op!(SequenceInsert);
        register_op!(SequenceLength);
        register_op!(Shape);
        register_op!(Sigmoid);
        register_op!(Sign);
        register_op!(Sin);
        register_op!(Size);
        register_op!(Slice);
        register_op!(Softmax);
        register_op!(Softplus);
        register_op!(Split);
        register_op!(SplitToSequence);
        register_op!(Sqrt);
        register_op!(Squeeze);
        register_op!(STFT, feature = "fft");
        register_op!(Sub);
        register_op!(Sum);
        register_op!(Tan);
        register_op!(Tanh);
        register_op!(Tile);
        register_op!(TopK);
        register_op!(Transpose);
        register_op!(Trilu);
        register_op!(Unsqueeze);
        register_op!(Where);
        register_op!(Xor);

        // com.microsoft ops.
        register_op!(MatMulNBits);

        reg
    }
}

/// Identifier for an ONNX operator.
///
/// See https://onnx.ai/onnx/intro/concepts.html#list-of-available-operators-and-domains
/// and the `NodeProto` message in the ONNX Protocol Buffers schema.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub struct OpId<'a> {
    /// Reverse DNS domain.
    ///
    /// The default domain used by most standard operators is "ai.onnx".
    pub domain: &'a str,
    /// Name of the operator (eg. "MatMul").
    pub op_type: &'a str,
}

const DEFAULT_DOMAIN: &str = "ai.onnx";

impl<'a> OpId<'a> {
    /// Create an operator ID using the default domain.
    fn new(op_type: &'a str) -> Self {
        Self {
            domain: DEFAULT_DOMAIN,
            op_type,
        }
    }

    /// Create an operator ID using a custom domain.
    fn with_domain(domain: &'a str, op_type: &'a str) -> Self {
        Self { domain, op_type }
    }
}

impl fmt::Display for OpId<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.domain == DEFAULT_DOMAIN {
            write!(f, "{}", self.op_type)
        } else {
            write!(f, "{}/{}", self.domain, self.op_type)
        }
    }
}

/// Context object passed to [`ReadOp::read`] implementations.
pub trait OpLoadContext {
    /// Deserialize a graph definition.
    fn load_graph(&self, graph: &onnx::GraphProto) -> Result<Graph, ReadOpError>;
}

/// Value for an operator input created from an attribute (`onnx::AttributeProto`).
#[derive(Debug, PartialEq)]
pub enum ConstInput {
    Ints(Vec<i64>),
    Float(f32),
}

/// Result of deserializing an ONNX operator and converting it to RTen's
/// corresponding internal type.
pub struct ParsedOp<Op: Operator + Send + Sync> {
    op: Op,

    /// Tuples of (input_index, value) for operator inputs generated from
    /// attributes.
    ///
    /// This is used to handle cases where ONNX attributes have been upgraded
    /// to inputs in newer ONNX releases.
    const_inputs: Vec<(u32, ConstInput)>,

    /// Indices of unused attributes in the [`onnx::NodeProto::attribute`] field.
    unused_attrs: BitSet,
}

impl<Op: Operator + Send + Sync> From<Op> for ParsedOp<Op> {
    fn from(op: Op) -> Self {
        Self::new(op)
    }
}

impl<Op: Operator + Send + Sync> ParsedOp<Op> {
    fn new(op: Op) -> Self {
        Self {
            op,
            const_inputs: Vec::new(),
            unused_attrs: BitSet::default(),
        }
    }

    fn with_inputs(mut self, inputs: Vec<(u32, ConstInput)>) -> Self {
        self.const_inputs = inputs;
        self
    }

    fn with_unused_attrs(mut self, attrs: BitSet) -> Self {
        self.unused_attrs = attrs;
        self
    }
}

/// Type-erased version of [`ParsedOp`].
pub struct DynParsedOp {
    pub op: Arc<dyn Operator + Send + Sync>,
    pub const_inputs: Vec<(u32, ConstInput)>,
    pub unused_attrs: BitSet,
}

impl<Op: Operator + Send + Sync> From<ParsedOp<Op>> for DynParsedOp {
    fn from(val: ParsedOp<Op>) -> DynParsedOp {
        let ParsedOp {
            op,
            const_inputs,
            unused_attrs,
        } = val;
        DynParsedOp {
            op: Arc::new(op),
            const_inputs,
            unused_attrs,
        }
    }
}

type ReadOpResult = Result<DynParsedOp, ReadOpError>;

type ReadOpFunction = dyn Fn(&onnx::NodeProto, &dyn OpLoadContext) -> ReadOpResult;

pub trait ReadOp: Operator + Sized + Send + Sync {
    /// Return the operator domain and op type.
    fn id() -> OpId<'static>;

    /// Deserialize an operator from a `NodeProto` into an RTen operator.
    fn read(op: &onnx::NodeProto, ctx: &dyn OpLoadContext) -> Result<ParsedOp<Self>, ReadOpError>;

    /// Deserialize an operator into a boxed `dyn Operator`.
    ///
    /// The node's type must correspond to the result of `op_type`.
    fn read_boxed(op: &onnx::NodeProto, ctx: &dyn OpLoadContext) -> ReadOpResult {
        let op = Self::read(op, ctx)?;
        Ok(op.into())
    }
}

/// Wrapper around the attributes of an ONNX operator.
///
/// This provides methods to find attributes by name and convert them to a
/// target type. It also records which attributes have been read, to enable
/// detecting unsupported attributes.
struct Attrs<'a> {
    attrs: &'a [onnx::AttributeProto],
    unused_attrs: Cell<BitSet>,
}

impl<'a> Attrs<'a> {
    fn new(attrs: &'a [onnx::AttributeProto]) -> Self {
        // Assume there will be at most 32 attributes. If there are more, we'll
        // just ignore them.
        let n_attrs: u32 = attrs.len().min(BitSet::BITS).try_into().unwrap();
        let unused_attrs = Cell::new(BitSet::ones(n_attrs));

        Self {
            attrs,
            unused_attrs,
        }
    }

    /// Get an optional attribute.
    fn get(&self, name: &'static str) -> Option<Attr<'a>> {
        let (pos, val) = self
            .attrs
            .iter()
            .enumerate()
            .find(|(_pos, att)| att.name.as_deref() == Some(name))?;

        let mut unused_attrs = self.unused_attrs.take();
        if pos < BitSet::BITS {
            unused_attrs.delete(pos.try_into().unwrap());
            self.unused_attrs.set(unused_attrs);
        }

        Some(Attr::new(name, val))
    }

    /// Get an attribute and cast it to a given type.
    fn get_as<T>(&self, name: &'static str) -> Option<T>
    where
        Attr<'a>: Into<T>,
    {
        self.get(name).map(|v| v.into())
    }

    /// Get an attribute and cast it to an integer type.
    fn get_as_int<T: TryFrom<i64, Error = std::num::TryFromIntError>>(
        &self,
        name: &'static str,
    ) -> Result<Option<T>, ReadOpError> {
        self.get(name).map(|v| v.cast_int()).transpose()
    }

    /// Get a required attribute.
    fn require(&self, name: &'static str) -> Result<Attr<'a>, ReadOpError> {
        self.get(name)
            .ok_or_else(|| ReadOpError::attr_error(name, "required attribute missing"))
    }

    /// Return the indices of unused attributes
    fn unused_attrs(&self) -> BitSet {
        self.unused_attrs.get()
    }

    /// Check that an attribute is either unset or has the value `expected`.
    fn check_eq<T>(&self, name: &'static str, expected: T) -> Result<(), ReadOpError>
    where
        T: From<Attr<'a>> + PartialEq,
    {
        self.check(name, |val: T| val == expected)
    }

    /// Check that an attribute is either unset or matches `predicate`.
    fn check<T>(&self, name: &'static str, predicate: impl Fn(T) -> bool) -> Result<(), ReadOpError>
    where
        T: From<Attr<'a>>,
    {
        let Some(attr) = self.get(name) else {
            return Ok(());
        };
        let val = T::from(attr);
        if predicate(val) {
            Ok(())
        } else {
            Err(ReadOpError::attr_error(name, "unsupported value"))
        }
    }

    /// Check the type of an attribute and mark it as used, but ignore the value.
    ///
    /// This is useful for attributes which are redundant or only applicable
    /// at training time.
    fn check_unused<T>(&self, name: &'static str) -> Result<(), ReadOpError>
    where
        T: From<Attr<'a>>,
    {
        self.check(name, |_val: T| true)
    }
}

/// Wrapper around an ONNX attribute value.
///
/// This provides methods to extract the value of a given type.
#[derive(Copy, Clone)]
struct Attr<'a> {
    name: &'static str,
    attr: &'a onnx::AttributeProto,
}

impl<'a> Attr<'a> {
    fn new(name: &'static str, attr: &'a onnx::AttributeProto) -> Self {
        Self { name, attr }
    }

    fn as_bool(&self) -> bool {
        self.as_i64() != 0
    }

    fn as_f32(&self) -> f32 {
        self.attr.f.unwrap_or_default()
    }

    fn as_floats(&self) -> &'a [f32] {
        &self.attr.floats
    }

    fn as_i64(&self) -> i64 {
        self.attr.i.unwrap_or_default()
    }

    fn cast_int<T: TryFrom<i64, Error = std::num::TryFromIntError>>(
        &self,
    ) -> Result<T, ReadOpError> {
        self.as_i64()
            .try_into()
            .map_err(|_| ReadOpError::attr_error(self.name, "value is out of range"))
    }

    fn as_ints(&self) -> &'a [i64] {
        &self.attr.ints
    }

    fn as_str(&self) -> &'a str {
        self.attr.s.as_deref().unwrap_or_default()
    }

    fn as_strings(&self) -> &'a [String] {
        &self.attr.strings
    }

    /// Get the RTen data type which corresponds to the ONNX data type.
    ///
    /// This can fail if the ONNX data type is unsupported in RTen.
    fn as_dtype(&self) -> Result<DataType, ReadOpError> {
        let onnx_dtype = onnx::DataType(self.cast_int()?);
        match onnx_dtype {
            onnx::DataType::FLOAT => Ok(DataType::Float),
            onnx::DataType::INT32 | onnx::DataType::INT64 | onnx::DataType::BOOL => {
                Ok(DataType::Int32)
            }
            _ => Err(ReadOpError::attr_error(
                self.name,
                format!("unsupported data type {onnx_dtype}"),
            )),
        }
    }

    fn as_graph(&self) -> Result<&onnx::GraphProto, ReadOpError> {
        self.attr
            .g
            .as_ref()
            .ok_or_else(|| ReadOpError::attr_error(self.name, "attribute is not a graph"))
    }

    /// Get the value of a string enum and convert it to enum type `T`.
    fn as_string_enum<T>(&self, map: impl Fn(&str) -> Option<T>) -> Result<T, ReadOpError> {
        let str_val = self.as_str();
        map(str_val).ok_or_else(|| ReadOpError::attr_error(self.name, "unsupported value"))
    }

    /// Get the value of an ints attribute, converted to type T.
    fn cast_ints<T: TryFrom<i64, Error = std::num::TryFromIntError>>(
        &self,
    ) -> Result<Vec<T>, ReadOpError> {
        self.as_ints()
            .iter()
            .map(|i| (*i).try_into())
            .collect::<Result<Vec<T>, _>>()
            .map_err(|_| ReadOpError::attr_error(self.name, "value is out of range"))
    }
}

macro_rules! impl_from_attr {
    ($into:ty, $as_fn:ident) => {
        impl<'a> From<Attr<'a>> for $into {
            fn from(val: Attr<'a>) -> Self {
                val.$as_fn()
            }
        }
    };
}

impl_from_attr!(f32, as_f32);
impl_from_attr!(&'a [f32], as_floats);
impl_from_attr!(i64, as_i64);
impl_from_attr!(&'a [i64], as_ints);
impl_from_attr!(bool, as_bool);
impl_from_attr!(&'a str, as_str);
impl_from_attr!(&'a [String], as_strings);

macro_rules! impl_read_op {
    ($op:ident) => {
        impl ReadOp for ops::$op {
            fn id() -> OpId<'static> {
                OpId::new(stringify!($op))
            }

            fn read(
                op: &onnx::NodeProto,
                _ctx: &dyn OpLoadContext,
            ) -> Result<ParsedOp<Self>, ReadOpError> {
                let attrs = Attrs::new(&op.attribute);
                Ok(ParsedOp::new(ops::$op {}).with_unused_attrs(attrs.unused_attrs()))
            }
        }
    };

    ($op:ident, $read:expr) => {
        impl ReadOp for ops::$op {
            fn id() -> OpId<'static> {
                OpId::new(stringify!($op))
            }

            fn read(
                op: &onnx::NodeProto,
                _ctx: &dyn OpLoadContext,
            ) -> Result<ParsedOp<Self>, ReadOpError> {
                let attrs = Attrs::new(&op.attribute);
                $read(&attrs).map(|op| ParsedOp::from(op).with_unused_attrs(attrs.unused_attrs()))
            }
        }
    };

    ($domain:literal, $op:ident, $read:expr) => {
        impl ReadOp for ops::$op {
            fn id() -> OpId<'static> {
                OpId::with_domain($domain, stringify!($op))
            }

            fn read(
                op: &onnx::NodeProto,
                _ctx: &dyn OpLoadContext,
            ) -> Result<ParsedOp<Self>, ReadOpError> {
                let attrs = Attrs::new(&op.attribute);
                $read(&attrs).map(|op| ParsedOp::from(op).with_unused_attrs(attrs.unused_attrs()))
            }
        }
    };
}

impl_read_op!(Abs);
impl_read_op!(Acos);
impl_read_op!(Add);
impl_read_op!(And);

struct ArgReduceAttrs {
    axis: isize,
    keep_dims: bool,
}

fn get_common_arg_reduce_attrs(attrs: &Attrs) -> Result<ArgReduceAttrs, ReadOpError> {
    attrs.check_eq("select_last_index", 0)?;

    let axis = attrs.get_as_int("axis")?.unwrap_or(0);
    let keep_dims = attrs.get_as("keepdims").unwrap_or(true);
    Ok(ArgReduceAttrs { axis, keep_dims })
}

impl_read_op!(ArgMax, |attrs: &Attrs| {
    let ArgReduceAttrs { axis, keep_dims } = get_common_arg_reduce_attrs(attrs)?;
    Ok(ops::ArgMax { axis, keep_dims })
});

impl_read_op!(ArgMin, |attrs: &Attrs| {
    let ArgReduceAttrs { axis, keep_dims } = get_common_arg_reduce_attrs(attrs)?;
    Ok(ops::ArgMin { axis, keep_dims })
});

impl_read_op!(Asin);
impl_read_op!(Atan);

impl_read_op!(AveragePool, |attrs: &Attrs| {
    let PoolAttrs {
        ceil_mode,
        kernel_size,
        padding,
        strides,
    } = get_common_pool_attrs(attrs)?;
    let count_include_pad = attrs.get_as("count_include_pad").unwrap_or(false);

    Ok(ops::AveragePool {
        ceil_mode,
        count_include_pad,
        kernel_size,
        padding,
        strides,
    })
});

impl_read_op!(BatchNormalization, |attrs: &Attrs| {
    attrs.check_eq("training_mode", 0)?;

    // Ignore attributes which are valid only if training_mode=1, which is
    // unsupported.
    attrs.check_unused::<f32>("momentum")?;

    let epsilon = attrs.get("epsilon").map(|v| v.as_f32()).unwrap_or(1e-05);
    Ok(ops::BatchNormalization { epsilon })
});

impl_read_op!(Cast, |attrs: &Attrs| {
    // The "saturate" attribute only applies to FP8, which is unsupported.
    // Conversions to other types do not saturate, even if this attribute is 1.
    attrs.check_eq("saturate", 1)?;

    let to = attrs.require("to")?.as_dtype()?;
    Ok(ops::Cast { to })
});

impl_read_op!(CastLike);
impl_read_op!(Ceil);

impl_read_op!(Clip, |attrs: &Attrs| {
    let mut const_inputs = Vec::new();

    if let Some(min) = attrs.get("min") {
        const_inputs.push((1, ConstInput::Float(min.as_f32())));
    }
    if let Some(max) = attrs.get("max") {
        const_inputs.push((2, ConstInput::Float(max.as_f32())));
    }

    Ok(ParsedOp::new(ops::Clip {}).with_inputs(const_inputs))
});

impl_read_op!(Concat, |attrs: &Attrs| {
    let axis = attrs.require("axis")?.cast_int()?;
    Ok(ops::Concat { axis })
});

impl_read_op!(ConcatFromSequence, |attrs: &Attrs| {
    let axis = attrs.require("axis")?.cast_int()?;
    let new_axis = attrs.get("new_axis").map(|v| v.as_bool()).unwrap_or(false);
    Ok(ops::ConcatFromSequence { axis, new_axis })
});

/// Read padding attributes from a convolution or pooling operator.
fn get_padding(attrs: &Attrs, n_spatial_dims: usize) -> Result<Padding, ReadOpError> {
    let auto_pad = match attrs.get_as("auto_pad") {
        // "VALID" means no padding. The "pads" attribute should be unset and
        // it will default to zeros.
        Some("NOTSET" | "VALID") | None => false,
        Some("SAME_UPPER" | "SAME_LOWER") => true,
        Some(_) => {
            return Err(ReadOpError::attr_error("auto_pad", "unsupported value"));
        }
    };
    let pads = attrs
        .get("pads")
        .map(|v| v.cast_ints())
        .transpose()?
        .unwrap_or_else(|| vec![0; n_spatial_dims * 2])
        .into();
    if auto_pad {
        Ok(Padding::Same)
    } else {
        Ok(Padding::Fixed(pads))
    }
}

struct ConvAttrs {
    dilations: Vec<usize>,
    padding: Padding,
    groups: usize,
    strides: Vec<usize>,
}

fn get_common_conv_attrs(attrs: &Attrs) -> Result<ConvAttrs, ReadOpError> {
    // nb. Spec says that spatial dims should be inferred from input if
    // `kernel_shape` attribute is not set. We don't have access to the input
    // here, so this would have to be handled by making various fields optional
    // in convolution operators.
    let n_spatial_dims = attrs
        .get("kernel_shape")
        .map(|v| v.as_ints().len())
        .unwrap_or(0);

    let dilations = attrs
        .get("dilations")
        .map(|v| v.cast_ints())
        .transpose()?
        .unwrap_or_else(|| vec![1; n_spatial_dims]);
    let padding = get_padding(attrs, n_spatial_dims)?;
    let groups = attrs.get_as_int("group")?.unwrap_or(1);

    let strides = attrs
        .get("strides")
        .map(|v| v.cast_ints())
        .transpose()?
        .unwrap_or_else(|| vec![1; n_spatial_dims]);

    Ok(ConvAttrs {
        dilations,
        padding,
        groups,
        strides,
    })
}

impl_read_op!(Conv, |attrs: &Attrs| {
    let ConvAttrs {
        dilations,
        padding,
        groups,
        strides,
    } = get_common_conv_attrs(attrs)?;

    Ok(ops::Conv {
        dilations,
        groups,
        padding,
        strides,
    })
});

impl_read_op!(ConvInteger, |attrs: &Attrs| {
    let ConvAttrs {
        dilations,
        padding,
        groups,
        strides,
    } = get_common_conv_attrs(attrs)?;

    Ok(ops::ConvInteger {
        dilations,
        groups,
        padding,
        strides,
    })
});

/// Helper for extracting scalar values from a `TensorProto`.
///
/// The data may be stored either as little-endian bytes in the `raw_data` field
/// or in one of the repeated numeric fields (`float_data`, `int32_data` etc.)
/// The numeric field may have a wider type than the tensor's data type (eg.
/// int8 values are stored in the int32_data field).
fn extract_scalar<T: Copy + LeBytes, U: Copy + TryInto<T>>(
    raw_data: Option<&[u8]>,
    typed_data: &[U],
) -> Result<T, ReadOpError> {
    if let Some(data) = raw_data
        && let Ok(bytes) = data.try_into()
    {
        Ok(T::from_le_bytes(bytes))
    } else if typed_data.len() == 1
        && let Ok(value) = typed_data[0].try_into()
    {
        Ok(value)
    } else {
        Err(ReadOpError::attr_error("value", "invalid scalar value"))
    }
}

impl_read_op!(ConstantOfShape, |attrs: &Attrs| {
    let value = attrs
        .get("value")
        .map(|attr| {
            let Some(tensor) = attr.attr.t.as_ref() else {
                return Err(ReadOpError::attr_error("value", "missing tensor value"));
            };

            let raw_data = tensor.raw_data.as_ref().map(|data| data.take());

            match tensor.data_type {
                Some(onnx::DataType::FLOAT) => {
                    let value = extract_scalar(raw_data.as_deref(), &tensor.float_data)?;
                    Ok(Scalar::Float(value))
                }
                Some(onnx::DataType::INT64) => {
                    let value: i64 = extract_scalar(raw_data.as_deref(), &tensor.int64_data)?;
                    #[allow(clippy::as_conversions)]
                    Ok(Scalar::Int(value as i32))
                }
                Some(onnx::DataType::INT32) => {
                    let value: i32 = extract_scalar(raw_data.as_deref(), &tensor.int32_data)?;
                    Ok(Scalar::Int(value))
                }
                Some(onnx::DataType::BOOL) => {
                    let value: u8 = extract_scalar(raw_data.as_deref(), &tensor.int32_data)?;
                    let value = if value != 0 { 1 } else { 0 };
                    Ok(Scalar::Int(value))
                }
                _ => Err(ReadOpError::attr_error(
                    "value",
                    "unsupported data type for ConstantOfShape",
                )),
            }
        })
        .transpose()?
        .unwrap_or(Scalar::Float(0.));

    Ok(ops::ConstantOfShape { value })
});

impl_read_op!(ConvTranspose, |attrs: &Attrs| {
    let ConvAttrs {
        dilations,
        padding,
        groups,
        strides,
    } = get_common_conv_attrs(attrs)?;

    if !dilations.iter().all(|d| *d == 1) {
        return Err(ReadOpError::attr_error("dilations", "unsupported value"));
    }

    let output_padding = attrs
        .get("output_padding")
        .map(|v| v.cast_ints())
        .transpose()?;

    Ok(ops::ConvTranspose {
        padding,
        strides,
        groups,
        output_padding,
    })
});

impl_read_op!(Cos);
impl_read_op!(CumSum, |attrs: &Attrs| {
    attrs.check_eq("exclusive", 0)?;
    attrs.check_eq("reverse", 0)?;
    Ok(ops::CumSum {})
});

impl_read_op!(DequantizeLinear, |attrs: &Attrs| {
    let axis = attrs.get_as_int("axis")?.unwrap_or(1);
    Ok(ops::DequantizeLinear { axis })
});

impl_read_op!(DepthToSpace, |attrs: &Attrs| {
    let mode = attrs
        .get("mode")
        .map(|v| {
            v.as_string_enum(|mode| match mode {
                "DCR" => Some(DepthToSpaceMode::DepthColumnRow),
                "CRD" => Some(DepthToSpaceMode::ColumnRowDepth),
                _ => None,
            })
        })
        .transpose()?
        .unwrap_or(DepthToSpaceMode::DepthColumnRow);
    let block_size = attrs.require("blocksize")?.cast_int()?;
    Ok(ops::DepthToSpace { mode, block_size })
});

impl_read_op!(Div);

#[cfg(feature = "random")]
impl_read_op!(Dropout, |attrs: &Attrs| {
    let seed = attrs.get_as_int("seed")?;
    Ok(ops::Dropout { seed })
});

impl_read_op!(DynamicQuantizeLinear);

impl_read_op!(Einsum, |attrs: &Attrs| {
    let equation = attrs.require("equation")?.as_str().to_string();
    Ok(ops::Einsum { equation })
});

impl_read_op!(Elu, |attrs: &Attrs| {
    let alpha = attrs.get_as("alpha").unwrap_or(1.0);
    Ok(ops::Elu { alpha })
});

impl_read_op!(Equal);
impl_read_op!(Erf);
impl_read_op!(Exp);
impl_read_op!(Expand);

impl_read_op!(EyeLike, |attrs: &Attrs| {
    let dtype = attrs.get("dtype").map(|v| v.as_dtype()).transpose()?;
    let k = attrs.get_as_int("k")?.unwrap_or(0);
    Ok(ops::EyeLike { dtype, k })
});

impl_read_op!(Flatten, |attrs: &Attrs| {
    let axis = attrs.get_as_int("axis")?.unwrap_or(1);
    Ok(ops::Flatten { axis })
});
impl_read_op!(Floor);

impl_read_op!(Gather, |attrs: &Attrs| {
    let axis = attrs.get_as_int("axis")?.unwrap_or(0);
    Ok(ops::Gather { axis })
});

impl_read_op!(GatherElements, |attrs: &Attrs| {
    let axis = attrs.get_as_int("axis")?.unwrap_or(0);
    Ok(ops::GatherElements { axis })
});

impl_read_op!(GatherND, |attrs: &Attrs| {
    let batch_dims = attrs.get_as_int("batch_dims")?.unwrap_or(0);
    Ok(ops::GatherND { batch_dims })
});

impl_read_op!(Gelu, |attrs: &Attrs| {
    let approximate = attrs
        .get("approximate")
        .map(|v| {
            v.as_string_enum(|approx| match approx {
                "tanh" => Some(true),
                "none" => Some(false),
                _ => None,
            })
        })
        .transpose()?
        .unwrap_or(false);
    Ok(ops::Gelu { approximate })
});

impl_read_op!(Gemm, |attrs: &Attrs| {
    let alpha = attrs.get_as("alpha").unwrap_or(1.0);
    let beta = attrs.get_as("beta").unwrap_or(1.0);
    let transpose_a = attrs.get_as("transA").unwrap_or(false);
    let transpose_b = attrs.get_as("transB").unwrap_or(false);

    Ok(ops::Gemm {
        alpha,
        beta,
        transpose_a,
        transpose_b,
    })
});

impl_read_op!(GlobalAveragePool);
impl_read_op!(GlobalMaxPool);
impl_read_op!(Greater);
impl_read_op!(GreaterOrEqual);

impl_read_op!(GridSample, |attrs: &Attrs| {
    let align_corners = attrs.get_as("align_corners").unwrap_or(false);
    attrs.check_eq("mode", "bilinear")?;
    attrs.check_eq("padding_mode", "zeros")?;

    Ok(ops::GridSample { align_corners })
});

impl_read_op!(GRU, |attrs: &Attrs| {
    let RnnAttrs {
        direction,
        hidden_size,
    } = get_common_rnn_attrs(attrs)?;

    let linear_before_reset = attrs.get_as("linear_before_reset").unwrap_or(false);
    Ok(ops::GRU {
        direction,
        hidden_size,
        linear_before_reset,
    })
});

impl_read_op!(HardSigmoid, |attrs: &Attrs| {
    let alpha = attrs.get_as("alpha").unwrap_or(0.2);
    let beta = attrs.get_as("beta").unwrap_or(0.5);
    Ok(ops::HardSigmoid { alpha, beta })
});

impl_read_op!(HardSwish);
impl_read_op!(Identity);

impl ReadOp for ops::If {
    fn id() -> OpId<'static> {
        OpId::new("If")
    }

    fn read(op: &onnx::NodeProto, ctx: &dyn OpLoadContext) -> Result<ParsedOp<Self>, ReadOpError> {
        let attrs = Attrs::new(&op.attribute);
        let then_branch = ctx.load_graph(attrs.require("then_branch")?.as_graph()?)?;
        let else_branch = ctx.load_graph(attrs.require("else_branch")?.as_graph()?)?;
        Ok(ops::If {
            then_branch,
            else_branch,
        }
        .into())
    }
}

impl_read_op!(InstanceNormalization, |attrs: &Attrs| {
    let epsilon = attrs.get_as("epsilon");
    Ok(ops::InstanceNormalization { epsilon })
});

impl_read_op!(IsInf, |attrs: &Attrs| {
    attrs.check_eq("detect_positive", 1)?;
    attrs.check_eq("detect_negative", 1)?;

    Ok(ops::IsInf {})
});
impl_read_op!(IsNaN);

impl_read_op!(LayerNormalization, |attrs: &Attrs| {
    let axis = attrs.get_as_int("axis")?.unwrap_or(-1);
    let epsilon = attrs.get_as("epsilon");
    attrs.check_eq("stash_type", 1)?;

    Ok(ops::LayerNormalization { axis, epsilon })
});

impl_read_op!(LeakyRelu, |attrs: &Attrs| {
    let alpha = attrs.get_as("alpha").unwrap_or(0.01);
    Ok(ops::LeakyRelu { alpha })
});

impl_read_op!(Less);
impl_read_op!(LessOrEqual);
impl_read_op!(Log);

impl_read_op!(LogSoftmax, |attrs: &Attrs| {
    let axis = attrs.get_as_int("axis")?.unwrap_or(-1);
    Ok(ops::LogSoftmax { axis })
});

impl ReadOp for ops::Loop {
    fn id() -> OpId<'static> {
        OpId::new("Loop")
    }

    fn read(op: &onnx::NodeProto, ctx: &dyn OpLoadContext) -> Result<ParsedOp<Self>, ReadOpError> {
        let attrs = Attrs::new(&op.attribute);
        let body = ctx.load_graph(attrs.require("body")?.as_graph()?)?;
        Ok(ops::Loop { body }.into())
    }
}

struct RnnAttrs {
    hidden_size: usize,
    direction: Direction,
}

fn get_common_rnn_attrs(attrs: &Attrs) -> Result<RnnAttrs, ReadOpError> {
    // ONNX spec does not state that hidden_size is required, but doesn't
    // provide a default. ONNX Runtime requires it to be present and non-zero.
    let hidden_size = attrs.require("hidden_size")?.cast_int()?;
    let direction = attrs
        .get("direction")
        .map(|v| {
            v.as_string_enum(|dir| match dir {
                "forward" => Some(Direction::Forward),
                "reverse" => Some(Direction::Reverse),
                "bidirectional" => Some(Direction::Bidirectional),
                _ => None,
            })
        })
        .transpose()?
        .unwrap_or(Direction::Forward);

    Ok(RnnAttrs {
        hidden_size,
        direction,
    })
}

impl_read_op!(LSTM, |attrs: &Attrs| {
    let RnnAttrs {
        direction,
        hidden_size,
    } = get_common_rnn_attrs(attrs)?;

    attrs.check("activation_alpha", |val: &[f32]| val.is_empty())?;
    attrs.check("activation_beta", |val: &[f32]| val.is_empty())?;
    attrs.check("activations", |val: &[String]| val.is_empty())?;
    attrs.check_eq("clip", 0.)?;
    attrs.check_eq("input_forget", 0)?;
    attrs.check_eq("layout", 0)?;

    Ok(ops::LSTM {
        direction,
        hidden_size,
    })
});

impl_read_op!(MatMul);
impl_read_op!(MatMulInteger);

impl_read_op!("com.microsoft", MatMulNBits, |attrs: &Attrs| {
    // Spec allows any value between 2 and 8.
    attrs.check_eq("bits", 4)?;

    // These are inferred from the inputs.
    attrs.check_unused::<i64>("block_size")?;
    attrs.check_unused::<i64>("K")?;
    attrs.check_unused::<i64>("N")?;

    // accuracy_level specifies the minimum compute accuracy. An implementation
    // may use higher. Current levels: 0 (unset), f32 (1), f16 (2), bf16 (3), i8
    // (4).
    let level = attrs.get_as("accuracy_level").unwrap_or(0);
    let accuracy_level = if level <= 3 {
        AccuracyLevel::Float
    } else {
        AccuracyLevel::Int8
    };

    let block_size = attrs.require("block_size")?.cast_int()?;

    Ok(ops::MatMulNBits {
        accuracy_level,
        bits: 4,
        block_size,
    })
});

impl_read_op!(Max);

struct PoolAttrs {
    ceil_mode: bool,
    kernel_size: SmallVec<[usize; 2]>,
    padding: Padding,
    strides: SmallVec<[usize; 2]>,
}

fn get_common_pool_attrs(attrs: &Attrs) -> Result<PoolAttrs, ReadOpError> {
    attrs.check_eq("storage_order", 0)?;
    attrs.check("dilations", |dilations: &[i64]| {
        dilations.iter().all(|d| *d == 1)
    })?;

    let ceil_mode = attrs.get_as("ceil_mode").unwrap_or(false);
    let kernel_size: SmallVec<[_; 2]> = attrs
        .get("kernel_shape")
        .map(|v| v.cast_ints())
        .transpose()?
        .unwrap_or_default()
        .into();
    let padding = get_padding(attrs, kernel_size.len())?;
    let strides = attrs
        .get("strides")
        .map(|v| v.cast_ints())
        .transpose()?
        .unwrap_or_default()
        .into();
    Ok(PoolAttrs {
        ceil_mode,
        kernel_size,
        padding,
        strides,
    })
}

impl_read_op!(MaxPool, |attrs: &Attrs| {
    let PoolAttrs {
        ceil_mode,
        kernel_size,
        padding,
        strides,
    } = get_common_pool_attrs(attrs)?;
    Ok(ops::MaxPool {
        ceil_mode,
        kernel_size,
        padding,
        strides,
    })
});

impl_read_op!(Mean);
impl_read_op!(Min);

impl_read_op!(Mod, |attrs: &Attrs| {
    let fmod = attrs.get_as("fmod").unwrap_or(false);
    Ok(ops::Mod { fmod })
});

impl_read_op!(Mul);
impl_read_op!(Neg);

impl_read_op!(NonMaxSuppression, |attrs: &Attrs| {
    let center_point_box = attrs.get_as("center_point_box").unwrap_or(false);
    let box_order = if center_point_box {
        BoxOrder::CenterWidthHeight
    } else {
        BoxOrder::TopLeftBottomRight
    };
    Ok(ops::NonMaxSuppression { box_order })
});

impl_read_op!(NonZero);
impl_read_op!(Not);

impl_read_op!(OneHot, |attrs: &Attrs| {
    let axis = attrs.get_as_int("axis")?.unwrap_or(-1);
    Ok(ops::OneHot { axis })
});
impl_read_op!(Or);

impl_read_op!(Pad, |attrs: &Attrs| {
    let mode = attrs
        .get("mode")
        .map(|v| {
            v.as_string_enum(|val| match val {
                "constant" => Some(PadMode::Constant),
                "reflect" => Some(PadMode::Reflect),
                "edge" => Some(PadMode::Edge),
                "wrap" => Some(PadMode::Wrap),
                _ => None,
            })
        })
        .transpose()?
        .unwrap_or(PadMode::Constant);
    Ok(ops::Pad { mode })
});

impl_read_op!(Pow);
impl_read_op!(PRelu);

impl_read_op!(QuantizeLinear, |attrs: &Attrs| {
    let output_dtype = attrs
        .get("output_dtype")
        .map(|v| v.as_dtype())
        .transpose()?;
    let axis = attrs.get_as_int("axis")?.unwrap_or(-1);
    Ok(ops::QuantizeLinear { axis, output_dtype })
});

#[cfg(feature = "random")]
impl_read_op!(RandomNormal, |attrs: &Attrs| {
    attrs.check_eq("dtype", i64::from(onnx::DataType::FLOAT.0))?;

    let shape = attrs.require("shape")?.cast_ints()?;
    let mean = attrs.get_as("mean").unwrap_or(0.);
    let scale = attrs.get_as("scale").unwrap_or(1.);
    let seed = attrs.get_as("seed");
    Ok(ops::RandomNormal {
        shape,
        mean,
        scale,
        seed,
    })
});

#[cfg(feature = "random")]
impl_read_op!(RandomNormalLike, |attrs: &Attrs| {
    attrs.check_eq("dtype", i64::from(onnx::DataType::FLOAT.0))?;

    let mean = attrs.get_as("mean").unwrap_or(0.);
    let scale = attrs.get_as("scale").unwrap_or(1.);
    let seed = attrs.get_as("seed");
    Ok(ops::RandomNormalLike { mean, scale, seed })
});

#[cfg(feature = "random")]
impl_read_op!(RandomUniform, |attrs: &Attrs| {
    attrs.check_eq("dtype", i64::from(onnx::DataType::FLOAT.0))?;

    let shape = attrs.require("shape")?.cast_ints()?;
    let low = attrs.get_as("low").unwrap_or(0.);
    let high = attrs.get_as("high").unwrap_or(1.);
    let seed = attrs.get_as("seed");
    Ok(ops::RandomUniform {
        shape,
        low,
        high,
        seed,
    })
});

#[cfg(feature = "random")]
impl_read_op!(RandomUniformLike, |attrs: &Attrs| {
    attrs.check_eq("dtype", i64::from(onnx::DataType::FLOAT.0))?;

    let low = attrs.get_as("low").unwrap_or(0.);
    let high = attrs.get_as("high").unwrap_or(1.);
    let seed = attrs.get_as("seed");
    Ok(ops::RandomUniformLike { low, high, seed })
});

impl_read_op!(Range);
impl_read_op!(Reciprocal);

macro_rules! impl_read_op_for_reduce_op {
    ($op:ident) => {
        impl_read_op!($op, |attrs: &Attrs| {
            let axes = attrs.get("axes").map(|v| v.cast_ints()).transpose()?;
            let keep_dims = attrs.get_as("keepdims").unwrap_or(true);
            let noop_with_empty_axes = attrs.get_as("noop_with_empty_axes").unwrap_or(false);
            Ok(ops::$op {
                axes,
                keep_dims,
                noop_with_empty_axes,
            })
        });
    };
}

impl_read_op_for_reduce_op!(ReduceL2);
impl_read_op_for_reduce_op!(ReduceMax);
impl_read_op_for_reduce_op!(ReduceMean);
impl_read_op_for_reduce_op!(ReduceMin);
impl_read_op_for_reduce_op!(ReduceProd);
impl_read_op_for_reduce_op!(ReduceSum);
impl_read_op_for_reduce_op!(ReduceSumSquare);
impl_read_op!(Relu);

impl_read_op!(Reshape, |attrs: &Attrs| {
    let allow_zero = attrs.get_as("allowzero").unwrap_or(false);
    Ok(ops::Reshape { allow_zero })
});

impl_read_op!(Resize, |attrs: &Attrs| {
    attrs.check_eq("antialias", 0)?;

    // rten-convert treats differences from the default as a warning rather than
    // an error, but there is no mechanism to do that here.
    attrs.check_eq("cubic_coeff_a", -0.75)?;

    attrs.check_eq("exclude_outside", 0)?;
    attrs.check_eq("extrapolation_value", 0.)?;
    attrs.check_eq("keep_aspect_ratio_policy", "stretch")?;

    let mode = attrs
        .get("mode")
        .map(|v| {
            v.as_string_enum(|val| match val {
                "nearest" => Some(ResizeMode::Nearest),
                "linear" => Some(ResizeMode::Linear),
                // Cubic resize mode is not currently implemented, fall back
                // to linear. This may degrade accuracy.
                "cubic" => Some(ResizeMode::Linear),
                _ => None,
            })
        })
        .transpose()?
        .unwrap_or(ResizeMode::Nearest);
    let nearest_mode = attrs
        .get("nearest_mode")
        .map(|v| {
            v.as_string_enum(|val| match val {
                "floor" => Some(NearestMode::Floor),
                "ceil" => Some(NearestMode::Ceil),
                "round_prefer_floor" => Some(NearestMode::RoundPreferFloor),
                "round_prefer_ceil" => Some(NearestMode::RoundPreferCeil),
                _ => None,
            })
        })
        .transpose()?
        .unwrap_or(NearestMode::RoundPreferFloor);
    let coord_mode = attrs
        .get("coordinate_transformation_mode")
        .map(|v| {
            v.as_string_enum(|val| match val {
                "asymmetric" => Some(CoordTransformMode::Asymmetric),
                "half_pixel" => Some(CoordTransformMode::HalfPixel),
                "align_corners" => Some(CoordTransformMode::AlignCorners),
                "pytorch_half_pixel" => Some(CoordTransformMode::PytorchHalfPixel),
                _ => None,
            })
        })
        .transpose()?
        .unwrap_or(CoordTransformMode::HalfPixel);

    Ok(ops::Resize {
        mode,
        nearest_mode,
        coord_mode,
    })
});

impl_read_op!(Round);

fn convert_scatter_reduction(
    attr: &'static str,
    val: Option<&str>,
) -> Result<Option<ScatterReduction>, ReadOpError> {
    match val {
        None | Some("none") => Ok(None),
        Some("add") => Ok(Some(ScatterReduction::Add)),
        Some("mul") => Ok(Some(ScatterReduction::Mul)),
        Some("min") => Ok(Some(ScatterReduction::Min)),
        Some("max") => Ok(Some(ScatterReduction::Max)),
        _ => Err(ReadOpError::attr_error(attr, "unknown value")),
    }
}

impl_read_op!(ScatterElements, |attrs: &Attrs| {
    let reduction = attrs.get_as("reduction");
    let reduction = convert_scatter_reduction("reduction", reduction)?;
    let axis = attrs.get_as_int("axis")?.unwrap_or(0);
    Ok(ops::ScatterElements { axis, reduction })
});

impl_read_op!(ScatterND, |attrs: &Attrs| {
    let reduction = attrs.get_as("reduction");
    let reduction = convert_scatter_reduction("reduction", reduction)?;
    Ok(ops::ScatterND { reduction })
});

impl_read_op!(SequenceAt);
impl_read_op!(SequenceConstruct);

impl_read_op!(SequenceEmpty, |attrs: &Attrs| {
    let dtype = attrs.get("dtype").map(|v| v.as_dtype()).transpose()?;
    Ok(ops::SequenceEmpty { dtype })
});

impl_read_op!(SequenceErase);
impl_read_op!(SequenceInsert);
impl_read_op!(SequenceLength);

impl_read_op!(Shape, |attrs: &Attrs| {
    let start = attrs.get_as_int("start")?;
    let end = attrs.get_as_int("end")?;
    Ok(ops::Shape { start, end })
});

impl_read_op!(Sigmoid);
impl_read_op!(Sign);
impl_read_op!(Sin);
impl_read_op!(Size);
impl_read_op!(Slice);

impl_read_op!(Softmax, |attrs: &Attrs| {
    let axis = attrs.get_as_int("axis")?.unwrap_or(-1);
    Ok(ops::Softmax {
        axis,
        flush_nans_to_zero: false,
    })
});

impl_read_op!(Softplus);

impl_read_op!(Split, |attrs: &Attrs| {
    let axis = attrs.get_as_int("axis")?.unwrap_or(0);
    let num_outputs = attrs.get_as_int("num_outputs")?;

    let mut const_inputs = Vec::new();
    if let Some(splits) = attrs.get("split") {
        const_inputs.push((1, ConstInput::Ints(splits.as_ints().into())));
    }

    Ok(ParsedOp::new(ops::Split { axis, num_outputs }).with_inputs(const_inputs))
});

impl_read_op!(SplitToSequence, |attrs: &Attrs| {
    let axis = attrs.get_as_int("axis")?.unwrap_or(0);
    let keep_dims = attrs.get_as("keepdims").unwrap_or(true);
    Ok(ops::SplitToSequence { axis, keep_dims })
});

impl_read_op!(Sqrt);

impl_read_op!(Squeeze, |attrs: &Attrs| {
    let mut const_inputs = Vec::new();
    if let Some(axes) = attrs.get("axes") {
        const_inputs.push((1, ConstInput::Ints(axes.as_ints().to_vec())));
    }
    Ok(ParsedOp::new(ops::Squeeze {}).with_inputs(const_inputs))
});

#[cfg(feature = "fft")]
impl_read_op!(STFT, |attrs: &Attrs| {
    let onesided = attrs.get_as("onesided").unwrap_or(true);
    Ok(ops::STFT { onesided })
});

impl_read_op!(Sub);
impl_read_op!(Sum);
impl_read_op!(Tan);
impl_read_op!(Tanh);
impl_read_op!(Tile);

impl_read_op!(TopK, |attrs: &Attrs| {
    let axis = attrs.get_as_int("axis")?;
    let largest = attrs.get_as("largest").unwrap_or(true);
    let sorted = attrs.get_as("sorted").unwrap_or(true);
    Ok(ops::TopK {
        axis,
        largest,
        sorted,
    })
});

impl_read_op!(Transpose, |attrs: &Attrs| {
    let perm = attrs.get("perm").map(|v| v.cast_ints()).transpose()?;
    Ok(ops::Transpose { perm })
});

impl_read_op!(Trilu, |attrs: &Attrs| {
    let upper = attrs.get_as("upper").unwrap_or(true);
    Ok(ops::Trilu { upper })
});

impl_read_op!(Unsqueeze, |attrs: &Attrs| {
    let mut const_inputs = Vec::new();
    if let Some(axes) = attrs.get("axes") {
        const_inputs.push((1, ConstInput::Ints(axes.as_ints().to_vec())));
    }
    Ok(ParsedOp::new(ops::Unsqueeze {}).with_inputs(const_inputs))
});

impl_read_op!(Where);
impl_read_op!(Xor);

#[cfg(test)]
mod tests {
    use rten_onnx::onnx;
    use rten_testing::TestCases;

    use super::{ConstInput, OnnxOpRegistry, OpLoadContext, ReadOpError};
    use crate::graph::Graph;
    use crate::model::onnx_builder::{NodeProtoExt, TensorData, create_node, create_tensor};
    use crate::ops::{ArgMax, ConstantOfShape, Conv, Padding};
    use crate::value::Scalar;

    struct FakeOpLoadContext;

    impl OpLoadContext for FakeOpLoadContext {
        fn load_graph(&self, _graph: &onnx::GraphProto) -> Result<Graph, ReadOpError> {
            Ok(Graph::new())
        }
    }

    #[test]
    fn test_read_op() {
        let reg = OnnxOpRegistry::with_all_ops();

        // Supported op with no domain.
        let node = create_node("MatMul");
        let op = reg.read_op(&node, &FakeOpLoadContext).unwrap().op;
        assert_eq!(op.name(), "MatMul");

        // Supported op with empty domain.
        let node = create_node("MatMul").with_domain("");
        let op = reg.read_op(&node, &FakeOpLoadContext).unwrap().op;
        assert_eq!(op.name(), "MatMul");
    }

    #[test]
    fn test_read_op_with_attrs() {
        let reg = OnnxOpRegistry::with_all_ops();
        let node = create_node("ArgMax")
            .with_attr("axis", 1)
            .with_attr("keepdims", true);

        let op = reg.read_op(&node, &FakeOpLoadContext).unwrap().op;

        let argmax_op = op.downcast_ref::<ArgMax>().unwrap();
        assert_eq!(argmax_op.axis, 1);
        assert_eq!(argmax_op.keep_dims, true);
    }

    #[test]
    fn test_unused_attrs() {
        let reg = OnnxOpRegistry::with_all_ops();
        let node = create_node("ArgMax")
            .with_attr("axis", 1)
            .with_attr("unused_a", false)
            .with_attr("keepdims", true)
            .with_attr("unused_b", false);

        let op = reg.read_op(&node, &FakeOpLoadContext).unwrap();
        assert_eq!(op.unused_attrs.len(), 2);
        let unused_attrs: Vec<_> = op
            .unused_attrs
            .iter()
            .map(|i| node.attribute[i].name.as_deref().unwrap_or_default())
            .collect();
        assert_eq!(unused_attrs, &["unused_a", "unused_b"]);
    }

    #[test]
    fn test_read_unsupported_op() {
        // Default domain, unknown op type.
        let reg = OnnxOpRegistry::with_all_ops();
        let node = create_node("UnsupportedOp");
        let op = reg.read_op(&node, &FakeOpLoadContext);
        assert!(
            matches!(op, Err(ReadOpError::OperatorUnavailable { name }) if name == Some("UnsupportedOp".to_string()))
        );

        // Known op type, but custom domain.
        let node = create_node("MatMul").with_domain("com.foobar");
        let op = reg.read_op(&node, &FakeOpLoadContext);
        assert!(
            matches!(op, Err(ReadOpError::OperatorUnavailable { name }) if name == Some("com.foobar/MatMul".to_string()))
        );
    }

    #[test]
    fn test_conv_op_defaults() {
        let reg = OnnxOpRegistry::with_all_ops();
        let node = create_node("Conv").with_attr("kernel_shape", vec![3, 3]);

        let op = reg.read_op(&node, &FakeOpLoadContext).unwrap().op;
        let conv_op = op.downcast_ref::<Conv>().unwrap();

        assert_eq!(conv_op.padding, Padding::Fixed([0, 0, 0, 0].into()));
        assert_eq!(conv_op.strides, vec![1, 1]);
        assert_eq!(conv_op.dilations, vec![1, 1]);
    }

    #[test]
    fn test_conv_op_padding() {
        #[derive(Debug)]
        struct Case {
            kernel_shape: Vec<i64>,
            auto_pad: Option<String>,
            pads: Option<Vec<i64>>,
            expected: Padding,
        }

        let cases = [
            Case {
                kernel_shape: [3, 3].into(),
                auto_pad: Some("VALID".into()),
                pads: None,
                expected: Padding::Fixed([0, 0, 0, 0].into()),
            },
            Case {
                kernel_shape: [3, 3].into(),
                auto_pad: Some("SAME_UPPER".into()),
                pads: None,
                expected: Padding::Same,
            },
            Case {
                kernel_shape: [3, 3].into(),
                auto_pad: Some("NOTSET".into()),
                pads: Some([1, 2, 3, 4].into()),
                expected: Padding::Fixed([1, 2, 3, 4].into()),
            },
        ];

        cases.test_each(|case| {
            let reg = OnnxOpRegistry::with_all_ops();
            let mut node = create_node("Conv").with_attr("kernel_shape", case.kernel_shape.clone());
            if let Some(auto_pad) = &case.auto_pad {
                node = node.with_attr("auto_pad", auto_pad.clone());
            }
            if let Some(pads) = &case.pads {
                node = node.with_attr("pads", pads.clone());
            }

            let op = reg.read_op(&node, &FakeOpLoadContext).unwrap().op;
            let conv_op = op.downcast_ref::<Conv>().unwrap();

            assert_eq!(conv_op.padding, case.expected);
        });
    }

    #[test]
    fn test_constant_of_shape_dtypes() {
        #[derive(Debug)]
        struct Case {
            dtype: onnx::DataType,
            data: TensorData,
            expected: ConstantOfShape,
        }

        let cases = [
            // Conversions that don't alter the dtype.
            Case {
                dtype: onnx::DataType::FLOAT,
                data: TensorData::Raw(1.0f32.to_le_bytes().into()),
                expected: ConstantOfShape {
                    value: Scalar::Float(1.0),
                },
            },
            Case {
                dtype: onnx::DataType::INT32,
                data: TensorData::Raw(2i32.to_le_bytes().into()),
                expected: ConstantOfShape {
                    value: Scalar::Int(2),
                },
            },
            // Test conversions that alter the dtype.
            Case {
                dtype: onnx::DataType::BOOL,
                data: TensorData::Int([1].into()),
                expected: ConstantOfShape {
                    value: Scalar::Int(1),
                },
            },
            Case {
                dtype: onnx::DataType::BOOL,
                data: TensorData::Raw([0].into()),
                expected: ConstantOfShape {
                    value: Scalar::Int(0),
                },
            },
            Case {
                dtype: onnx::DataType::INT64,
                data: TensorData::Raw(42i64.to_le_bytes().into()),
                expected: ConstantOfShape {
                    value: Scalar::Int(42),
                },
            },
        ];

        cases.test_each(|case| {
            let reg = OnnxOpRegistry::with_all_ops();
            let tensor = create_tensor("test", &[], case.dtype, case.data.clone());
            let node = create_node("ConstantOfShape").with_attr("value", tensor);
            let op = reg.read_op(&node, &FakeOpLoadContext).unwrap();
            let cos_op = op.op.downcast_ref::<ConstantOfShape>().unwrap();
            assert_eq!(cos_op, &case.expected);
        });
    }

    #[test]
    fn test_promote_attributes() {
        #[derive(Debug)]
        struct Case {
            op: onnx::NodeProto,
            expected_inputs: Vec<(u32, ConstInput)>,
        }

        let cases = [
            Case {
                op: create_node("Clip")
                    .with_attr("min", -0.5)
                    .with_attr("max", 0.5),
                expected_inputs: [(1, ConstInput::Float(-0.5)), (2, ConstInput::Float(0.5))].into(),
            },
            Case {
                op: create_node("Squeeze").with_attr("axes", vec![-1]),
                expected_inputs: [(1, ConstInput::Ints([-1].into()))].into(),
            },
            Case {
                op: create_node("Split").with_attr("split", vec![10]),
                expected_inputs: [(1, ConstInput::Ints([10].into()))].into(),
            },
            Case {
                op: create_node("Unsqueeze").with_attr("axes", vec![-1]),
                expected_inputs: [(1, ConstInput::Ints([-1].into()))].into(),
            },
        ];

        cases.test_each_value(|case| {
            let reg = OnnxOpRegistry::with_all_ops();
            let op = reg.read_op(&case.op, &FakeOpLoadContext).unwrap();
            assert_eq!(op.const_inputs, case.expected_inputs);
        });
    }
}