summaryrefslogtreecommitdiff
path: root/nn/common/Validation.cpp
blob: 68f778d2205d2f686e309e4e533ead75a1bcc288 (plain)
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
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Validation.h"

#include <android-base/logging.h>

#include <algorithm>
#include <cctype>
#include <functional>
#include <limits>
#include <memory>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <string_view>
#include <tuple>
#include <utility>
#include <variant>
#include <vector>

#include "ControlFlow.h"
#include "OperandTypes.h"
#include "OperationResolver.h"
#include "OperationTypes.h"
#include "Result.h"
#include "TypeUtils.h"
#include "Types.h"

// The NN_VALIDATE family of macros defined below is similar to the CHECK family defined in
// system/libbase/include/android-base/logging.h
//
// The difference is that NN_VALIDATE macros use LOG(ERROR) instead of LOG(FATAL)
// and return false instead of aborting.

// Logs an error and returns false or INVALID. Append context using << after. For example:
//
//   NN_VALIDATE_FAIL() << "Something went wrong";
//
// The containing function must return a bool or Version.
#define NN_VALIDATE_FAIL() \
    return NN_ERROR() << "NN_VALIDATE failed (" << __FILE__ << ":" << __LINE__ << "): "

// Logs an error and returns false or Version::INVALID if condition is false. Extra logging can be
// appended using << after. For example:
//
//   NN_VALIDATE(false) << "Something went wrong";
//
// The containing function must return a bool.
#define NN_VALIDATE(condition) \
    while (UNLIKELY(!(condition))) NN_VALIDATE_FAIL() << #condition << " "

// Helper for NN_VALIDATE_xx(x, y) macros.
#define NN_VALIDATE_OP(LHS, RHS, OP)                                                        \
    for (auto _values = ::android::base::MakeEagerEvaluator(LHS, RHS);                      \
         UNLIKELY(!(_values.lhs.v OP _values.rhs.v));                                       \
         /* empty */)                                                                       \
    NN_VALIDATE_FAIL()                                                                      \
            << #LHS << " " << #OP << " " << #RHS << " (" << #LHS << " = "                   \
            << ::android::base::LogNullGuard<decltype(_values.lhs.v)>::Guard(_values.lhs.v) \
            << ", " << #RHS << " = "                                                        \
            << ::android::base::LogNullGuard<decltype(_values.rhs.v)>::Guard(_values.rhs.v) \
            << ") "

// Logs an error and returns false or Version::INVALID if a condition between x and y does not hold.
// Extra logging can be appended using << after. For example:
//
//   NN_VALIDATE_EQ(a, b) << "Something went wrong";
//
// The values must implement the appropriate comparison operator as well as
// `operator<<(std::ostream&, ...)`.
// The containing function must return a bool or Version.
#define NN_VALIDATE_EQ(x, y) NN_VALIDATE_OP(x, y, ==)
#define NN_VALIDATE_NE(x, y) NN_VALIDATE_OP(x, y, !=)
#define NN_VALIDATE_LE(x, y) NN_VALIDATE_OP(x, y, <=)
#define NN_VALIDATE_LT(x, y) NN_VALIDATE_OP(x, y, <)
#define NN_VALIDATE_GE(x, y) NN_VALIDATE_OP(x, y, >=)
#define NN_VALIDATE_GT(x, y) NN_VALIDATE_OP(x, y, >)

namespace android::nn {
namespace {

constexpr auto kNullptrVariant = std::variant<const void*, void*>{};
constexpr auto kInvalidMemoryDomainToken = Request::MemoryDomainToken{};

template <typename Type, typename ValidationFunction>
Result<Version> validateVector(const std::vector<Type>& objects,
                               const ValidationFunction& validationFunction) {
    auto version = Version::ANDROID_OC_MR1;
    for (const auto& object : objects) {
        version = combineVersions(version, NN_TRY(validationFunction(object)));
    }
    return version;
}

bool isValidExtensionName(const std::string& name) {
    constexpr auto validSymbol = [](char symbol) {
        return std::islower(symbol) || std::isdigit(symbol) || symbol == '.' || symbol == '_';
    };
    const bool hasOnlyValidSymbols = std::all_of(name.begin(), name.end(), validSymbol);
    const bool hasAtLeastOnePeriod = std::find(name.begin(), name.end(), '.') != name.end();
    return hasOnlyValidSymbols && hasAtLeastOnePeriod;
}

Result<Version> validateDeviceStatus(const DeviceStatus& deviceStatus) {
    switch (deviceStatus) {
        case DeviceStatus::AVAILABLE:
        case DeviceStatus::BUSY:
        case DeviceStatus::OFFLINE:
        case DeviceStatus::UNKNOWN:
            return Version::ANDROID_OC_MR1;
    }
    NN_VALIDATE_FAIL() << "Invalid DeviceStatus " << deviceStatus;
}

Result<Version> validateExecutionPreference(const ExecutionPreference& executionPreference) {
    switch (executionPreference) {
        case ExecutionPreference::LOW_POWER:
        case ExecutionPreference::FAST_SINGLE_ANSWER:
        case ExecutionPreference::SUSTAINED_SPEED:
            return Version::ANDROID_P;
    }
    NN_VALIDATE_FAIL() << "Invalid ExecutionPreference " << executionPreference;
}

Result<Version> validateDeviceType(const DeviceType& deviceType) {
    switch (deviceType) {
        case DeviceType::UNKNOWN:
            // DeviceType was introduced in the 1.2 NN HAL. DeviceType::UNKNOWN is returned when
            // querying versions that are prior to the 1.2 NN HAL. DeviceType::UNKNOWN is not a
            // valid code to return for a driver that implement at least a 1.2 NN HAL. If we need a
            // range of versions, make ANDROID_Q (NN HAL 1.2) the exclusive upper bound for
            // DeviceType::UNKNOWN.
            return Version::ANDROID_OC_MR1;
        case DeviceType::OTHER:
        case DeviceType::CPU:
        case DeviceType::GPU:
        case DeviceType::ACCELERATOR:
            return Version::ANDROID_Q;
    }
    NN_VALIDATE_FAIL() << "Invalid DeviceType " << deviceType;
}

Result<Version> validateMeasureTiming(const MeasureTiming& measureTiming) {
    switch (measureTiming) {
        case MeasureTiming::NO:
        case MeasureTiming::YES:
            return Version::ANDROID_Q;
    }
    NN_VALIDATE_FAIL() << "Invalid MeasureTiming " << measureTiming;
}

Result<Version> validateOperandType(const OperandType& operandType) {
    switch (operandType) {
        case OperandType::FLOAT32:
        case OperandType::INT32:
        case OperandType::UINT32:
        case OperandType::TENSOR_FLOAT32:
        case OperandType::TENSOR_INT32:
        case OperandType::TENSOR_QUANT8_ASYMM:
        case OperandType::OEM:
        case OperandType::TENSOR_OEM_BYTE:
            return Version::ANDROID_OC_MR1;
        case OperandType::BOOL:
        case OperandType::TENSOR_QUANT16_SYMM:
        case OperandType::TENSOR_FLOAT16:
        case OperandType::TENSOR_BOOL8:
        case OperandType::FLOAT16:
        case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
        case OperandType::TENSOR_QUANT16_ASYMM:
        case OperandType::TENSOR_QUANT8_SYMM:
            return Version::ANDROID_Q;
        case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
        case OperandType::SUBGRAPH:
            return Version::ANDROID_R;
    }
    if (isExtension(operandType)) {
        return Version::ANDROID_Q;
    }
    NN_VALIDATE_FAIL() << "Invalid OperandType " << operandType;
}

Result<Version> validateOperandLifeTime(const Operand& operand) {
    // Make sure SUBGRAPH operand type and lifetime always go together.
    NN_VALIDATE_EQ((operand.type == OperandType::SUBGRAPH),
                   (operand.lifetime == Operand::LifeTime::SUBGRAPH))
            << "Operand of type " << operand.type << " cannot have lifetime " << operand.lifetime;

    switch (operand.lifetime) {
        case Operand::LifeTime::TEMPORARY_VARIABLE:
        case Operand::LifeTime::SUBGRAPH_INPUT:
        case Operand::LifeTime::SUBGRAPH_OUTPUT:
        case Operand::LifeTime::CONSTANT_COPY:
        case Operand::LifeTime::CONSTANT_REFERENCE:
        case Operand::LifeTime::NO_VALUE:
        case Operand::LifeTime::POINTER:
            return Version::ANDROID_OC_MR1;
        case Operand::LifeTime::SUBGRAPH:
            return Version::ANDROID_R;
    }
    NN_VALIDATE_FAIL() << "Invalid Operand::LifeTime " << operand.lifetime;
}

Result<Version> validatePriority(const Priority& priority) {
    switch (priority) {
        case Priority::LOW:
        case Priority::MEDIUM:
        case Priority::HIGH:
            return Version::ANDROID_R;
    }
    NN_VALIDATE_FAIL() << "Invalid Priority " << priority;
}

Result<Version> validateErrorStatus(const ErrorStatus& errorStatus) {
    // Note that MISSED_DEADLINE_*, RESOURCE_EXHAUSTED_*, and DEAD_OBJECT were introduced ih
    // ANDROID_R, but these can be cast to ANDROID_OC_MR1 as GENERAL_FAILURE.
    switch (errorStatus) {
        case ErrorStatus::NONE:
        case ErrorStatus::DEVICE_UNAVAILABLE:
        case ErrorStatus::GENERAL_FAILURE:
        case ErrorStatus::OUTPUT_INSUFFICIENT_SIZE:
        case ErrorStatus::INVALID_ARGUMENT:
        case ErrorStatus::MISSED_DEADLINE_TRANSIENT:
        case ErrorStatus::MISSED_DEADLINE_PERSISTENT:
        case ErrorStatus::RESOURCE_EXHAUSTED_TRANSIENT:
        case ErrorStatus::RESOURCE_EXHAUSTED_PERSISTENT:
        case ErrorStatus::DEAD_OBJECT:
            return Version::ANDROID_OC_MR1;
    }
    NN_VALIDATE_FAIL() << "Invalid ErrorStatus " << errorStatus;
}

Result<Version> validateFusedActivationFunc(const FusedActivationFunc& activation) {
    switch (activation) {
        case FusedActivationFunc::NONE:
        case FusedActivationFunc::RELU:
        case FusedActivationFunc::RELU1:
        case FusedActivationFunc::RELU6:
            return Version::ANDROID_OC_MR1;
    }
    NN_VALIDATE_FAIL() << "Invalid FusedActivationFunc " << activation;
}

Result<Version> validateOutputShape(const OutputShape& /*outputShape*/) {
    return Version::ANDROID_Q;
}

Result<Version> validateTiming(const Timing& timing) {
    if (timing.timeInDriver != kNoTiming && timing.timeOnDevice != kNoTiming) {
        NN_VALIDATE_LE(timing.timeOnDevice, timing.timeInDriver);
    }
    return Version::ANDROID_Q;
}

Result<Version> validateCapabilitiesPerformanceInfo(
        const Capabilities::PerformanceInfo& performanceInfo) {
    NN_VALIDATE_GT(performanceInfo.execTime, 0.0f);
    NN_VALIDATE_GT(performanceInfo.powerUsage, 0.0f);
    return Version::ANDROID_OC_MR1;
}

Result<Version> validateCapabilitiesOperandPerformance(
        const Capabilities::OperandPerformance& operandPerformance) {
    auto version = NN_TRY(validateOperandType(operandPerformance.type));
    return combineVersions(version,
                           NN_TRY(validateCapabilitiesPerformanceInfo(operandPerformance.info)));
}

Result<Version> validateCapabilitiesOperandPerformanceTable(
        const Capabilities::OperandPerformanceTable& operandPerformances) {
    // OperandPerformanceTable's order was validated when it was created, and it is castable to any
    // version. If an OperandType does not exist in the lower version being converted to, that
    // OperandPerformance will be dropped.
    NN_TRY(validateVector(operandPerformances.asVector(), validateCapabilitiesOperandPerformance));
    return Version::ANDROID_OC_MR1;
}

Result<Version> validateCapabilities(const Capabilities& capabilities) {
    auto version =
            NN_TRY(validateCapabilitiesOperandPerformanceTable(capabilities.operandPerformance));

    version = combineVersions(version,
                              NN_TRY(validateCapabilitiesPerformanceInfo(
                                      capabilities.relaxedFloat32toFloat16PerformanceScalar)));
    version = combineVersions(version,
                              NN_TRY(validateCapabilitiesPerformanceInfo(
                                      capabilities.relaxedFloat32toFloat16PerformanceTensor)));
    version = combineVersions(
            version, NN_TRY(validateCapabilitiesPerformanceInfo(capabilities.ifPerformance)));
    version = combineVersions(
            version, NN_TRY(validateCapabilitiesPerformanceInfo(capabilities.whilePerformance)));

    return version;
}

Result<Version> validateExtensionOperandTypeInformation(
        const Extension::OperandTypeInformation& operandTypeInformation) {
    NN_VALIDATE_GT(operandTypeInformation.byteSize, 0u);
    return Version::ANDROID_Q;
}

Result<Version> validateExtension(const Extension& extension) {
    NN_VALIDATE(isValidExtensionName(extension.name));

    // Verify all OperandTypeInformations have unique types.
    std::vector<uint16_t> types;
    types.reserve(extension.operandTypes.size());
    std::transform(extension.operandTypes.begin(), extension.operandTypes.end(),
                   std::back_inserter(types),
                   [](const Extension::OperandTypeInformation& operandTypeInformation) {
                       return operandTypeInformation.type;
                   });
    std::sort(types.begin(), types.end());
    const auto iter = std::adjacent_find(types.begin(), types.end());
    NN_VALIDATE(iter == types.end()) << "Extension has duplicate type " << *iter;

    return combineVersions(Version::ANDROID_Q,
                           NN_TRY(validateVector(extension.operandTypes,
                                                 validateExtensionOperandTypeInformation)));
}

Result<Version> validateExtensions(const std::vector<Extension>& extensions) {
    const auto version = NN_TRY(validateVector(extensions, validateExtension));

    // Verify all extensions have unique names.
    std::vector<std::reference_wrapper<const std::string>> names;
    names.reserve(extensions.size());
    std::transform(extensions.begin(), extensions.end(), std::back_inserter(names),
                   [](const Extension& extension) { return std::cref(extension.name); });
    std::sort(names.begin(), names.end(), std::less<std::string>{});
    const auto nameIter =
            std::adjacent_find(names.begin(), names.end(), std::equal_to<std::string>{});
    NN_VALIDATE(nameIter == names.end())
            << "Two or more extensions have the duplicate name " << nameIter->get();

    return version;
}

Result<Version> validateOperandDataLocation(const Operand& operand, size_t operandValuesSize,
                                            const std::vector<size_t>& poolSizes,
                                            size_t subgraphCount) {
    const DataLocation& location = operand.location;
    switch (operand.lifetime) {
        case Operand::LifeTime::CONSTANT_COPY:
            NN_VALIDATE(location.pointer == kNullptrVariant)
                    << "CONSTANT_COPY with a non-null pointer";
            NN_VALIDATE_EQ(location.poolIndex, 0u)
                    << "CONSTANT_COPY with a non-zero poolIndex " << location.poolIndex;
            // Do the addition using uint64_t to avoid potential wrap-around problems.
            NN_VALIDATE_LE(static_cast<uint64_t>(location.offset) + location.length,
                           operandValuesSize)
                    << "OperandValue location out of range.  Starts at " << location.offset
                    << ", length " << location.length << ", max " << operandValuesSize;
            return Version::ANDROID_OC_MR1;
        case Operand::LifeTime::CONSTANT_REFERENCE:
            NN_VALIDATE_LT(location.poolIndex, poolSizes.size());
            // Do the addition using uint64_t to avoid potential wrap-around problems.
            NN_VALIDATE_LE(static_cast<uint64_t>(location.offset) + location.length,
                           poolSizes[location.poolIndex])
                    << "OperandValue location out of range.  Starts at " << location.offset
                    << ", length " << location.length << ", max " << poolSizes[location.poolIndex];
            return Version::ANDROID_OC_MR1;
        case Operand::LifeTime::TEMPORARY_VARIABLE:
        case Operand::LifeTime::SUBGRAPH_INPUT:
        case Operand::LifeTime::SUBGRAPH_OUTPUT:
        case Operand::LifeTime::NO_VALUE:
            NN_VALIDATE(location.pointer == kNullptrVariant)
                    << "Unexpected pointer value for operand of lifetime " << operand.lifetime;
            NN_VALIDATE_EQ(location.poolIndex, 0u)
                    << "Unexpected poolIndex " << location.poolIndex << " for operand of lifetime "
                    << operand.lifetime;
            NN_VALIDATE_EQ(location.offset, 0u) << "Unexpected offset " << location.offset
                                                << " for operand of lifetime " << operand.lifetime;
            NN_VALIDATE_EQ(location.length, 0u) << "Unexpected length " << location.length
                                                << " for operand of lifetime " << operand.lifetime;
            return Version::ANDROID_OC_MR1;
        case Operand::LifeTime::SUBGRAPH:
            NN_VALIDATE(location.pointer == kNullptrVariant) << "SUBGRAPH with a non-null pointer";
            NN_VALIDATE_EQ(location.poolIndex, 0u)
                    << "SUBGRAPH with a non-zero poolIndex " << location.poolIndex;
            NN_VALIDATE_LT(location.offset, subgraphCount)
                    << "Subgraph index out of range: " << location.offset
                    << " >= " << subgraphCount;
            NN_VALIDATE_EQ(location.length, 0u)
                    << "SUBGRAPH with a non-zero length " << location.length;
            return Version::ANDROID_R;
        case Operand::LifeTime::POINTER: {
            const bool nonNull =
                    std::visit([](auto* ptr) { return ptr != nullptr; }, location.pointer);
            NN_VALIDATE(nonNull) << "POINTER with a null pointer";
            NN_VALIDATE_EQ(location.poolIndex, 0u)
                    << "POINTER with a non-zero poolIndex " << location.poolIndex;
            NN_VALIDATE_EQ(location.offset, 0u)
                    << "POINTER with a non-zero offset " << location.offset;
            return Version::ANDROID_OC_MR1;
        }
    }
    NN_VALIDATE_FAIL() << "Invalid Operand::LifeTime " << operand.lifetime;
}

Result<Version> validateOperandDimensions(const Operand& operand) {
    switch (operand.type) {
        case OperandType::FLOAT32:
        case OperandType::INT32:
        case OperandType::UINT32:
        case OperandType::BOOL:
        case OperandType::FLOAT16:
        case OperandType::SUBGRAPH:
        case OperandType::OEM:
            NN_VALIDATE(operand.dimensions.empty())
                    << "Scalar data has dimensions of rank " << operand.dimensions.size();
            return Version::ANDROID_OC_MR1;
        case OperandType::TENSOR_FLOAT32:
        case OperandType::TENSOR_INT32:
        case OperandType::TENSOR_QUANT8_ASYMM:
        case OperandType::TENSOR_QUANT16_SYMM:
        case OperandType::TENSOR_FLOAT16:
        case OperandType::TENSOR_BOOL8:
        case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
        case OperandType::TENSOR_QUANT16_ASYMM:
        case OperandType::TENSOR_QUANT8_SYMM:
        case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
        case OperandType::TENSOR_OEM_BYTE: {
            if (operand.lifetime == Operand::LifeTime::CONSTANT_COPY ||
                operand.lifetime == Operand::LifeTime::CONSTANT_REFERENCE ||
                operand.lifetime == Operand::LifeTime::POINTER) {
                NN_VALIDATE(!operand.dimensions.empty())
                        << "Tensor has lifetime of " << operand.lifetime
                        << " but dimensions of rank 0";
                const auto size = getNonExtensionSize(operand);
                NN_VALIDATE(size.has_value()) << "Tensor dimensions overflow";
                NN_VALIDATE_NE(size.value(), 0u) << "Tensor has at least one unknown dimension";
            }
            // TODO(b/165152547): aren't NO_VALUE arguments allowed to be .empty() even before
            // Android Q?
            if (operand.dimensions.empty()) {
                // Unspecified rank was added in Android Q.
                return Version::ANDROID_Q;
            }
            return Version::ANDROID_OC_MR1;
        }
    }
    if (isExtension(operand.type)) {
        // Extension types were added in Android Q.
        return Version::ANDROID_Q;
    }
    NN_VALIDATE_FAIL() << "Invalid OperandType " << operand.type;
}

Result<Version> validateOperandScale(const Operand& operand) {
    switch (operand.type) {
        case OperandType::FLOAT32:
        case OperandType::INT32:
        case OperandType::UINT32:
        case OperandType::TENSOR_FLOAT32:
        case OperandType::BOOL:
        case OperandType::TENSOR_FLOAT16:
        case OperandType::TENSOR_BOOL8:
        case OperandType::FLOAT16:
        case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
        case OperandType::SUBGRAPH:
            NN_VALIDATE_EQ(operand.scale, 0.0f)
                    << "Operand of type " << operand.type << " with a non-zero scale ("
                    << operand.scale << ")";
            return Version::ANDROID_OC_MR1;
        case OperandType::TENSOR_INT32:
            // TENSOR_INT32 may be used with or without scale, depending on the operation.
            // TODO(b/119869082) We should have a separate type for TENSOR_INT32 with a scale.
            NN_VALIDATE_GE(operand.scale, 0.0f)
                    << "Operand of type " << operand.type << " with a negative scale";
            return Version::ANDROID_OC_MR1;
        case OperandType::TENSOR_QUANT8_ASYMM:
        case OperandType::TENSOR_QUANT16_SYMM:
        case OperandType::TENSOR_QUANT16_ASYMM:
        case OperandType::TENSOR_QUANT8_SYMM:
        case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
            NN_VALIDATE_GT(operand.scale, 0.0f)
                    << "Operand of type " << operand.type << " with a non-positive scale";
            return Version::ANDROID_OC_MR1;
        case OperandType::OEM:
        case OperandType::TENSOR_OEM_BYTE:
            // No validation for OEM types.
            return Version::ANDROID_OC_MR1;
    }
    if (isExtension(operand.type)) {
        NN_VALIDATE_EQ(operand.scale, 0.0f) << "Operand of type " << operand.type
                                            << " with a non-zero scale (" << operand.scale << ")";
        return Version::ANDROID_Q;
    }
    NN_VALIDATE_FAIL() << "Invalid OperandType " << operand.type;
}

Result<Version> validateOperandZeroPoint(const Operand& operand) {
    switch (operand.type) {
        case OperandType::FLOAT32:
        case OperandType::INT32:
        case OperandType::UINT32:
        case OperandType::TENSOR_FLOAT32:
        case OperandType::TENSOR_INT32:
        case OperandType::BOOL:
        case OperandType::TENSOR_FLOAT16:
        case OperandType::TENSOR_BOOL8:
        case OperandType::FLOAT16:
        case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
        case OperandType::TENSOR_QUANT8_SYMM:
        case OperandType::SUBGRAPH:
            NN_VALIDATE_EQ(operand.zeroPoint, 0)
                    << "Operand of type " << operand.type << " with a non-zero zeroPoint "
                    << operand.zeroPoint;
            return Version::ANDROID_OC_MR1;
        case OperandType::TENSOR_QUANT8_ASYMM:
            NN_VALIDATE(operand.zeroPoint >= 0 && operand.zeroPoint <= 255)
                    << "Operand of type " << operand.type << " with an invalid zeroPoint "
                    << operand.zeroPoint << ", must be in range [0, 255]";
            return Version::ANDROID_OC_MR1;
        case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
            NN_VALIDATE(operand.zeroPoint >= -128 && operand.zeroPoint <= 127)
                    << "Operand of type " << operand.type << " with an invalid zeroPoint "
                    << operand.zeroPoint << ", must be in range [-128, 127]";
            return Version::ANDROID_OC_MR1;
        case OperandType::TENSOR_QUANT16_ASYMM:
            NN_VALIDATE(operand.zeroPoint >= 0 && operand.zeroPoint <= 65535)
                    << "Operand of type " << operand.type << " with an invalid zeroPoint "
                    << operand.zeroPoint << ", must be in range [0, 65535]";
            return Version::ANDROID_OC_MR1;
        case OperandType::TENSOR_QUANT16_SYMM:
            NN_VALIDATE_EQ(operand.zeroPoint, 0)
                    << "Operand of type " << operand.type << " with a non-zero zeroPoint "
                    << operand.zeroPoint;
            return Version::ANDROID_OC_MR1;
        case OperandType::OEM:
        case OperandType::TENSOR_OEM_BYTE:
            // No validation for OEM types.
            return Version::ANDROID_OC_MR1;
    }
    if (isExtension(operand.type)) {
        NN_VALIDATE_EQ(operand.zeroPoint, 0) << "Operand of type " << operand.type
                                             << " with a non-zero zeroPoint " << operand.zeroPoint;
        return Version::ANDROID_Q;
    }
    NN_VALIDATE_FAIL() << "Invalid OperandType " << operand.type;
}

Result<Version> validateOperandExtraParams(const Operand& operand) {
    switch (operand.type) {
        case OperandType::FLOAT32:
        case OperandType::INT32:
        case OperandType::UINT32:
        case OperandType::TENSOR_FLOAT32:
        case OperandType::TENSOR_INT32:
        case OperandType::TENSOR_QUANT8_ASYMM:
        case OperandType::BOOL:
        case OperandType::TENSOR_QUANT16_SYMM:
        case OperandType::TENSOR_FLOAT16:
        case OperandType::TENSOR_BOOL8:
        case OperandType::FLOAT16:
        case OperandType::TENSOR_QUANT16_ASYMM:
        case OperandType::TENSOR_QUANT8_SYMM:
        case OperandType::TENSOR_QUANT8_ASYMM_SIGNED:
        case OperandType::SUBGRAPH:
            NN_VALIDATE(std::holds_alternative<Operand::NoParams>(operand.extraParams))
                    << "Operand of type " << operand.type
                    << " has extraParams when there must be none";
            return Version::ANDROID_OC_MR1;
        case OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: {
            NN_VALIDATE(
                    std::holds_alternative<Operand::SymmPerChannelQuantParams>(operand.extraParams))
                    << "Operand of type " << operand.type
                    << " without a Channel Quantization params";
            const auto& channelQuant =
                    std::get<Operand::SymmPerChannelQuantParams>(operand.extraParams);

            const size_t count = operand.dimensions.size();
            NN_VALIDATE_LT(channelQuant.channelDim, count)
                    << "Operand of type " << operand.type
                    << " with an invalid channelQuant.channelDim " << channelQuant.channelDim
                    << ", must be valid dimension index in range [0, " << count << ")";
            const uint32_t expected = operand.dimensions[channelQuant.channelDim];
            NN_VALIDATE_EQ(channelQuant.scales.size(), expected)
                    << "Operand of type " << operand.type << " with a wrong-sized scales, expected "
                    << expected << " was " << channelQuant.scales.size();
            NN_VALIDATE_NE(expected, 0u)
                    << "Operand of type " << operand.type << " channel dimension "
                    << channelQuant.channelDim << " is underspecified (can't be 0)";
            for (uint32_t i = 0; i < expected; ++i) {
                NN_VALIDATE_GT(channelQuant.scales[i], 0.0f)
                        << "Operand of type " << operand.type
                        << " with a non-positive value in scales[" << i
                        << "]=" << channelQuant.scales[i];
            }
            return Version::ANDROID_Q;
        }
        case OperandType::OEM:
        case OperandType::TENSOR_OEM_BYTE:
            // No validation for OEM types.
            return Version::ANDROID_OC_MR1;
    }
    if (isExtension(operand.type)) {
        NN_VALIDATE(std::holds_alternative<Operand::NoParams>(operand.extraParams) ||
                    std::holds_alternative<Operand::ExtensionParams>(operand.extraParams))
                << "Extension operand of type " << operand.type
                << " must not have SymmPerChannelQuant extraParams";
        return Version::ANDROID_OC_MR1;
    }
    NN_VALIDATE_FAIL() << "Invalid OperandType " << operand.type;
}

Result<Version> validateOperand(const Operand& operand, size_t operandValuesSize,
                                const std::vector<size_t>& poolSizes, size_t subgraphCount) {
    auto version = NN_TRY(validateOperandType(operand.type));
    version = combineVersions(version, NN_TRY(validateOperandLifeTime(operand)));
    version = combineVersions(version, NN_TRY(validateOperandDimensions(operand)));
    version = combineVersions(version, NN_TRY(validateOperandScale(operand)));
    version = combineVersions(version, NN_TRY(validateOperandZeroPoint(operand)));
    version = combineVersions(version, NN_TRY(validateOperandExtraParams(operand)));
    version =
            combineVersions(version, NN_TRY(validateOperandDataLocation(operand, operandValuesSize,
                                                                        poolSizes, subgraphCount)));

    // For constants, validate that the length is as expected. The other lifetimes
    // expect the length to be 0. Don't validate for OEM types.
    if (operand.lifetime == Operand::LifeTime::CONSTANT_REFERENCE ||
        operand.lifetime == Operand::LifeTime::CONSTANT_COPY ||
        operand.lifetime == Operand::LifeTime::POINTER) {
        if (!isExtension(operand.type) && operand.type != OperandType::OEM &&
            operand.type != OperandType::TENSOR_OEM_BYTE) {
            const auto expectedLength = getNonExtensionSize(operand).value();
            NN_VALIDATE_EQ(operand.location.length, expectedLength)
                    << "For operand " << operand.type << " expected a size of " << expectedLength
                    << " but got " << operand.location.length;
        }
    }

    return version;
}

Result<Version> validateOperands(const std::vector<Operand>& operands, size_t operandValuesSize,
                                 const std::vector<size_t>& poolSizes, size_t subgraphCount) {
    auto version = Version::ANDROID_OC_MR1;
    for (size_t i = 0; i < operands.size(); ++i) {
        auto result = validateOperand(operands[i], operandValuesSize, poolSizes, subgraphCount);
        if (!result.has_value()) {
            return NN_ERROR() << std::move(result).error() << " for operand " << i;
        }
        version = combineVersions(version, result.value());
    }
    return version;
}

Result<Version> validateOperationImpl(const Operation& operation,
                                      const std::vector<Operand>& operands,
                                      const std::vector<Model::Subgraph>& subgraphs);

Result<Version> validateOperations(const std::vector<Operation>& operations,
                                   const std::vector<Operand>& operands,
                                   const std::vector<Model::Subgraph>& subgraphs) {
    auto version = Version::ANDROID_OC_MR1;
    for (size_t i = 0; i < operations.size(); ++i) {
        auto result = validateOperationImpl(operations[i], operands, subgraphs);
        if (!result.has_value()) {
            return NN_ERROR() << std::move(result).error() << " for operation " << i;
        }
        version = combineVersions(version, result.value());
    }
    return version;
}

Result<Version> validateNativeHandle(const NativeHandle& handle) {
    NN_VALIDATE(handle != nullptr);
    return Version::ANDROID_OC_MR1;
}

Result<Version> validateMemory(const Memory& memory) {
    NN_TRY(validateNativeHandle(memory.handle));

    if (memory.name == "ashmem") {
        NN_VALIDATE_NE(memory.size, 0u);
        return Version::ANDROID_OC_MR1;
    }
    if (memory.name == "mmap_fd") {
        NN_VALIDATE_NE(memory.size, 0u);
        return Version::ANDROID_OC_MR1;
    }
    if (memory.name == "hardware_buffer_blob") {
        NN_VALIDATE_NE(memory.size, 0u);
        return Version::ANDROID_Q;
    }
    if (memory.name == "hardware_buffer") {
        // For hardware_buffer memory, all size information is bound to the AHardwareBuffer, so
        // memory.size must be 0.
        NN_VALIDATE_EQ(memory.size, 0u);
        return Version::ANDROID_Q;
    }

    NN_VALIDATE_FAIL() << "Unknown memory type " << memory.name;
}

Result<void> validateModelSubgraphInputOutputs(const std::vector<uint32_t>& indexes,
                                               const std::vector<Operand>& operands,
                                               Operand::LifeTime lifetime) {
    const size_t operandCount = operands.size();
    for (uint32_t i : indexes) {
        NN_VALIDATE_LT(i, operandCount)
                << "Model " << lifetime << " input or output index out of range: " << i << "/"
                << operandCount;
        const Operand& operand = operands[i];
        NN_VALIDATE_EQ(operand.lifetime, lifetime)
                << "Model " << lifetime << " operand " << i << " has lifetime of "
                << operand.lifetime << " instead of the expected " << lifetime;
    }

    std::vector<uint32_t> sortedIndexes = indexes;
    std::sort(sortedIndexes.begin(), sortedIndexes.end());
    const auto iter = std::adjacent_find(sortedIndexes.begin(), sortedIndexes.end());
    NN_VALIDATE(iter == sortedIndexes.end())
            << "Model input or output occurs multiple times: " << *iter;

    for (size_t i = 0; i < operands.size(); ++i) {
        if (operands[i].lifetime == lifetime) {
            const auto containsIndex = [&sortedIndexes](size_t index) {
                return binary_search(sortedIndexes.begin(), sortedIndexes.end(), index);
            };
            NN_VALIDATE(containsIndex(i))
                    << "Operand " << i << " marked as " << lifetime
                    << " but is not included in Model input or output indexes";
        }
    }

    return {};
}

Result<void> validateExecutionOrder(const Model::Subgraph& subgraph) {
    // Either the operand has a known value before model execution begins, or we've seen a writer
    // for this operand while walking operands in execution order. Initialize to known operands.
    std::vector<bool> operandValueKnown;
    operandValueKnown.reserve(subgraph.operands.size());
    std::transform(subgraph.operands.begin(), subgraph.operands.end(),
                   std::back_inserter(operandValueKnown), [](const Operand& operand) {
                       return operand.lifetime != Operand::LifeTime::TEMPORARY_VARIABLE &&
                              operand.lifetime != Operand::LifeTime::SUBGRAPH_OUTPUT;
                   });

    // Validate that operations are sorted into execution order.
    //
    // If there is a cycle in the graph, the operations will not
    // appear to be sorted into execution order: Some operation will
    // have an input for which operandValueKnown[] is false.
    for (size_t i = 0; i < subgraph.operations.size(); ++i) {
        const auto& operation = subgraph.operations[i];

        for (size_t j = 0; j < operation.inputs.size(); ++j) {
            const uint32_t k = operation.inputs[j];
            NN_VALIDATE(operandValueKnown[k]) << "Operation " << i << " input " << j << " (operand "
                                              << k << ") is read before it is written";
        }

        for (size_t j = 0; j < operation.outputs.size(); ++j) {
            const uint32_t k = operation.outputs[j];
            // Assuming validateOperations() has returned true, we know that this output is
            // TEMPORARY_VARIABLE or MODEL_OUTPUT, and so the only way operandValueKnown[k] can be
            // true is if we've already seen a writer for this operand.
            NN_VALIDATE(!operandValueKnown[k]) << "Operation " << i << " output " << j
                                               << " (operand " << k << ") has already been written";
            operandValueKnown[k] = true;
        }
    }

    // Verify all operands are written.
    for (size_t i = 0; i < subgraph.operands.size(); ++i) {
        NN_VALIDATE(operandValueKnown[i]) << "Operand " << i << " is never written";
    }

    // TODO(b/77871786): verify that every operation has at least one output operand that is read?

    return {};
}

Result<Version> validateModelSubgraph(const Model::Subgraph& subgraph, size_t operandValuesSize,
                                      const std::vector<size_t>& poolSizes,
                                      const std::vector<Model::Subgraph>& referenced) {
    NN_VALIDATE(!subgraph.operands.empty());
    NN_VALIDATE(!subgraph.operations.empty());
    // TODO(b/134529942#comment7): should we verify !subgraph.inputIndexes.empty()?
    NN_VALIDATE(!subgraph.inputIndexes.empty());
    NN_VALIDATE(!subgraph.outputIndexes.empty());

    auto version = NN_TRY(
            validateOperands(subgraph.operands, operandValuesSize, poolSizes, referenced.size()));
    version = combineVersions(version, NN_TRY(validateOperations(subgraph.operations,
                                                                 subgraph.operands, referenced)));

    NN_TRY(validateModelSubgraphInputOutputs(subgraph.inputIndexes, subgraph.operands,
                                             Operand::LifeTime::SUBGRAPH_INPUT));
    NN_TRY(validateModelSubgraphInputOutputs(subgraph.outputIndexes, subgraph.operands,
                                             Operand::LifeTime::SUBGRAPH_OUTPUT));

    NN_TRY(validateExecutionOrder(subgraph));

    return version;
}

Result<Version> validateModelExtensionNamesAndPrefixes(
        const std::vector<Model::ExtensionNameAndPrefix>& extensionNamesAndPrefixes) {
    for (const auto& extensionNameAndPrefix : extensionNamesAndPrefixes) {
        NN_VALIDATE(isValidExtensionName(extensionNameAndPrefix.name));
    }

    std::vector<std::reference_wrapper<const std::string>> names;
    names.reserve(extensionNamesAndPrefixes.size());
    std::transform(extensionNamesAndPrefixes.begin(), extensionNamesAndPrefixes.end(),
                   std::back_inserter(names),
                   [](const Model::ExtensionNameAndPrefix& extensionNameAndPrefix) {
                       return std::cref(extensionNameAndPrefix.name);
                   });
    std::sort(names.begin(), names.end(), std::less<std::string>{});
    const auto nameIter =
            std::adjacent_find(names.begin(), names.end(), std::equal_to<std::string>{});
    NN_VALIDATE(nameIter == names.end())
            << "ExtensionNamesAndPrefixes has duplicate name " << nameIter->get();

    std::vector<uint16_t> types;
    types.reserve(extensionNamesAndPrefixes.size());
    std::transform(extensionNamesAndPrefixes.begin(), extensionNamesAndPrefixes.end(),
                   std::back_inserter(types),
                   [](const Model::ExtensionNameAndPrefix& extensionNameAndPrefix) {
                       return extensionNameAndPrefix.prefix;
                   });
    std::sort(types.begin(), types.end());
    const auto typeIter = std::adjacent_find(types.begin(), types.end());
    NN_VALIDATE(typeIter == types.end())
            << "ExtensionNamesAndPrefixes has duplicate type " << *typeIter;

    const bool hasExtensions = !extensionNamesAndPrefixes.empty();
    return hasExtensions ? Version::ANDROID_Q : Version::ANDROID_OC_MR1;
}

// Makes sure the model does not contain subgraph reference cycles.
Result<void> checkNoReferenceCycles(const Model& model, const Model::Subgraph& subgraph,
                                    std::set<const Model::Subgraph*>* path) {
    const auto [_, isNew] = path->insert(&subgraph);
    NN_VALIDATE(isNew) << "Model contains a circular subgraph reference";
    // TODO(b/165154824): It looks like this functions is doing a lot of redundant work.
    for (const Operand& operand : subgraph.operands) {
        if (operand.lifetime == Operand::LifeTime::SUBGRAPH) {
            const uint32_t refSubgraphIndex = operand.location.offset;
            NN_TRY(checkNoReferenceCycles(model, model.referenced[refSubgraphIndex], path));
        }
    }
    path->erase(&subgraph);
    return {};
}

Result<void> checkNoReferenceCycles(const Model& model) {
    std::set<const Model::Subgraph*> path;
    return checkNoReferenceCycles(model, model.main, &path);
}

Result<Version> validateModel(const Model& model) {
    auto version = NN_TRY(validateVector(model.pools, validateMemory));
    version = combineVersions(
            version, NN_TRY(validateModelExtensionNamesAndPrefixes(model.extensionNameToPrefix)));

    // Ignore relaxComputationFloat32toFloat16 version because in the worst case it makes the
    // execution stricter.

    // Referenced models were introduced in Android R.
    const bool hasReferencedModels = !model.referenced.empty();
    const auto referenceModelVersion =
            hasReferencedModels ? Version::ANDROID_R : Version::ANDROID_OC_MR1;
    version = combineVersions(version, referenceModelVersion);

    // Get memory sizes.
    std::vector<size_t> poolSizes;
    poolSizes.reserve(model.pools.size());
    std::transform(model.pools.begin(), model.pools.end(), std::back_inserter(poolSizes),
                   [](const Memory& memory) { return memory.size; });
    const size_t operandValuesSize = model.operandValues.size();

    // Validate main subgraph.
    auto result = validateModelSubgraph(model.main, operandValuesSize, poolSizes, model.referenced);
    if (!result.has_value()) {
        return NN_ERROR() << std::move(result).error() << " for main subgraph";
    }
    version = combineVersions(version, result.value());

    // Validate referenced subgraphs.
    for (size_t i = 0; i < model.referenced.size(); ++i) {
        const auto& subgraph = model.referenced[i];
        auto result =
                validateModelSubgraph(subgraph, operandValuesSize, poolSizes, model.referenced);
        if (!result.has_value()) {
            return NN_ERROR() << std::move(result).error() << " for referenced subgraph" << i;
        }
        version = combineVersions(version, result.value());
    }

    // Ensure that there are no references formed by calling the subgraphs.
    NN_TRY(checkNoReferenceCycles(model));

    return version;
}

Result<Version> validateBufferDesc(const BufferDesc& /*bufferDesc*/) {
    return Version::ANDROID_R;
}

Result<Version> validateBufferRole(const BufferRole& bufferRole) {
    NN_VALIDATE_GT(bufferRole.frequency, 0.0f);
    NN_VALIDATE_LE(bufferRole.frequency, 1.0f);
    return Version::ANDROID_R;
}

Result<Version> validateRequestArgument(const Request::Argument& requestArgument,
                                        const std::vector<size_t>& memorySizes, bool isOutput) {
    const auto lifetime = requestArgument.lifetime;
    const auto& location = requestArgument.location;
    const auto& dimensions = requestArgument.dimensions;

    switch (lifetime) {
        case Request::Argument::LifeTime::POOL: {
            NN_VALIDATE(location.pointer == kNullptrVariant);
            NN_VALIDATE_LT(location.poolIndex, memorySizes.size());
            // Do the addition using uint64_t to avoid potential wrap-around problems.
            const auto lastPosition = static_cast<uint64_t>(location.offset) + location.length;
            NN_VALIDATE_LE(lastPosition, memorySizes[location.poolIndex]);
            return Version::ANDROID_OC_MR1;
        }
        case Request::Argument::LifeTime::NO_VALUE:
            NN_VALIDATE(location.pointer == kNullptrVariant);
            NN_VALIDATE_EQ(location.poolIndex, 0u);
            NN_VALIDATE_EQ(location.offset, 0u);
            NN_VALIDATE_EQ(location.length, 0u);
            NN_VALIDATE(dimensions.empty());
            return Version::ANDROID_OC_MR1;
        case Request::Argument::LifeTime::POINTER: {
            const bool isNullptr =
                    std::visit([](auto ptr) { return ptr == nullptr; }, location.pointer);
            NN_VALIDATE(!isNullptr);
            NN_VALIDATE_EQ(location.poolIndex, 0u);
            NN_VALIDATE_EQ(location.offset, 0u);
            NN_VALIDATE_NE(location.length, 0u);
            if (isOutput) {
                NN_VALIDATE(std::holds_alternative<void*>(location.pointer));
            }
            return Version::ANDROID_OC_MR1;
        }
    }
    NN_VALIDATE_FAIL() << "Invalid Request::Argument::LifeTime " << lifetime;
}

Result<Version> validateRequestMemoryPool(const Request::MemoryPool& memoryPool) {
    if (std::holds_alternative<Request::MemoryDomainToken>(memoryPool)) {
        NN_VALIDATE(std::get<Request::MemoryDomainToken>(memoryPool) != kInvalidMemoryDomainToken);
        return Version::ANDROID_R;
    }
    if (std::holds_alternative<std::shared_ptr<const IBuffer>>(memoryPool)) {
        NN_VALIDATE(std::get<std::shared_ptr<const IBuffer>>(memoryPool) != nullptr);
        return Version::ANDROID_R;
    }
    return validateMemory(std::get<Memory>(memoryPool));
}

Result<Version> validateRequest(const Request& request) {
    auto version = NN_TRY(validateVector(request.pools, validateRequestMemoryPool));

    // Get memory sizes. For IBuffer or MemoryDomainToken types, set size to 0.
    std::vector<size_t> memorySizes;
    memorySizes.reserve(request.pools.size());
    std::transform(request.pools.begin(), request.pools.end(), std::back_inserter(memorySizes),
                   [](const Request::MemoryPool& memoryPool) {
                       const auto* memory = std::get_if<Memory>(&memoryPool);
                       return memory != nullptr ? memory->size : 0;
                   });

    for (size_t i = 0; i < request.inputs.size(); ++i) {
        const auto& input = request.inputs[i];
        auto result = validateRequestArgument(input, memorySizes, /*isOutput=*/false);
        if (!result.has_value()) {
            return NN_ERROR() << std::move(result).error() << " for input RequestArgument " << i;
        }
        version = combineVersions(version, result.value());
    }
    for (size_t i = 0; i < request.outputs.size(); ++i) {
        const auto& output = request.outputs[i];
        auto result = validateRequestArgument(output, memorySizes, /*isOutput=*/true);
        if (!result.has_value()) {
            return NN_ERROR() << std::move(result).error() << " for output RequestArgument " << i;
        }
        version = combineVersions(version, result.value());
    }

    return version;
}

Result<Version> validateOptionalTimePoint(const OptionalTimePoint& optionalTimePoint) {
    if (optionalTimePoint.has_value()) {
        NN_VALIDATE_GE(optionalTimePoint.value().time_since_epoch().count(), 0);
    }
    return Version::ANDROID_R;
}

Result<Version> validateOptionalTimeoutDuration(
        const OptionalTimeoutDuration& optionalTimeoutDuration) {
    if (optionalTimeoutDuration.has_value()) {
        NN_VALIDATE_GE(optionalTimeoutDuration.value().count(), 0);
    }
    return Version::ANDROID_R;
}

Result<Version> validateRequestArgumentsForModel(
        const std::vector<Request::Argument>& requestArguments,
        const std::vector<uint32_t>& operandIndexes, const std::vector<Operand>& operands,
        bool isOutput) {
    auto version = Version::ANDROID_OC_MR1;
    // The request should specify as many arguments as were described in the model.
    const std::string_view type = isOutput ? "output" : "input";
    const size_t requestArgumentCount = requestArguments.size();
    NN_VALIDATE_EQ(requestArgumentCount, operandIndexes.size())
            << "Request specifies " << requestArgumentCount << " " << type << "s but the model has "
            << operandIndexes.size();
    for (size_t requestArgumentIndex = 0; requestArgumentIndex < requestArgumentCount;
         requestArgumentIndex++) {
        const Request::Argument& requestArgument = requestArguments[requestArgumentIndex];
        // Get the operand index for this argument. We extract it from the list
        // that was provided in the call to ANeuralNetworksModel_identifyInputsAndOutputs.
        // We assume in this function that the model has been validated already.
        const uint32_t operandIndex = operandIndexes[requestArgumentIndex];
        const Operand& operand = operands[operandIndex];
        if (requestArgument.lifetime != Request::Argument::LifeTime::NO_VALUE) {
            // If the argument specified a dimension, validate it.
            uint32_t modelRank = operand.dimensions.size();
            uint32_t requestRank = requestArgument.dimensions.size();
            if (requestRank == 0) {
                // NOTE: validateRequestArguments cannot validate unknown tensor rank with
                // extension operand type.
                if (!isExtension(operand.type) && !isNonExtensionScalar(operand.type)) {
                    if (modelRank <= 0) {
                        NN_VALIDATE(isOutput)
                                << "Model has unknown input rank but the request does not "
                                   "specify the rank.";
                        // Unspecified output dimensions introduced in Android Q.
                        version = combineVersions(version, Version::ANDROID_Q);
                    }
                }
                // Validate that all the dimensions are specified in the model.
                for (size_t i = 0; i < modelRank; i++) {
                    if (operand.dimensions[i] == 0) {
                        NN_VALIDATE(isOutput)
                                << "Model has dimension " << i
                                << " set to 0 but the request does not specify the dimension.";
                        // Unspecified output dimensions introduced in Android Q.
                        version = combineVersions(version, Version::ANDROID_Q);
                    }
                }
            } else {
                NN_VALIDATE(modelRank == 0 || requestRank == modelRank)
                        << "Request " << type << " " << requestArgumentIndex
                        << " has number of dimensions (" << requestRank
                        << ") different than the model's (" << modelRank << ")";
                for (size_t i = 0; i < requestRank; i++) {
                    NN_VALIDATE(modelRank == 0 || operand.dimensions[i] == 0 ||
                                requestArgument.dimensions[i] == operand.dimensions[i])
                            << "Request " << type << " " << requestArgumentIndex
                            << " has dimension " << i << " of " << requestArgument.dimensions[i]
                            << " different than the model's " << operand.dimensions[i];
                    if (requestArgument.dimensions[i] == 0) {
                        NN_VALIDATE(isOutput) << "Request " << type << " " << requestArgumentIndex
                                              << " has dimension " << i << " of zero";
                        // Unspecified output dimensions introduced in Android Q.
                        version = combineVersions(version, Version::ANDROID_Q);
                    }
                }
            }
        }
    }
    return version;
}

Result<Version> validateRequestForModelImpl(const Request& request, const Model& model) {
    auto version = NN_TRY(validateRequest(request));
    version = combineVersions(version, NN_TRY(validateModel(model)));
    version = combineVersions(version, NN_TRY(validateRequestArgumentsForModel(
                                               request.inputs, model.main.inputIndexes,
                                               model.main.operands, /*isOutput=*/false)));
    version = combineVersions(version, NN_TRY(validateRequestArgumentsForModel(
                                               request.outputs, model.main.outputIndexes,
                                               model.main.operands, /*isOutput=*/true)));
    return version;
}

Result<Version> validateMemoryDescImpl(
        const BufferDesc& desc,
        const std::vector<std::shared_ptr<const IPreparedModel>>& preparedModels,
        const std::vector<BufferRole>& inputRoles, const std::vector<BufferRole>& outputRoles,
        const std::function<const Model*(const std::shared_ptr<const IPreparedModel>&)>& getModel,
        std::set<PreparedModelRole>* preparedModelRoles, Operand* combinedOperand) {
    NN_VALIDATE(!preparedModels.empty());
    NN_VALIDATE(!inputRoles.empty() || !outputRoles.empty());

    std::set<PreparedModelRole> roles;
    std::vector<nn::Operand> operands;
    operands.reserve(inputRoles.size() + outputRoles.size());
    for (const auto& role : inputRoles) {
        NN_VALIDATE_LT(role.modelIndex, preparedModels.size());
        const auto& preparedModel = preparedModels[role.modelIndex];
        NN_VALIDATE(preparedModel != nullptr);
        const auto* model = getModel(preparedModel);
        NN_VALIDATE(model != nullptr);
        const auto& inputIndexes = model->main.inputIndexes;
        NN_VALIDATE_LT(role.ioIndex, inputIndexes.size());
        NN_VALIDATE_GT(role.frequency, 0.0f);
        NN_VALIDATE_LE(role.frequency, 1.0f);
        const auto [it, success] = roles.emplace(preparedModel.get(), IOType::INPUT, role.ioIndex);
        NN_VALIDATE(success);
        operands.push_back(model->main.operands[inputIndexes[role.ioIndex]]);
    }
    for (const auto& role : outputRoles) {
        NN_VALIDATE_LT(role.modelIndex, preparedModels.size());
        const auto& preparedModel = preparedModels[role.modelIndex];
        NN_VALIDATE(preparedModel != nullptr);
        const auto* model = getModel(preparedModel);
        NN_VALIDATE(model != nullptr);
        const auto& outputIndexes = model->main.outputIndexes;
        NN_VALIDATE_LT(role.ioIndex, outputIndexes.size());
        NN_VALIDATE_GT(role.frequency, 0.0f);
        NN_VALIDATE_LE(role.frequency, 1.0f);
        const auto [it, success] = roles.emplace(preparedModel.get(), IOType::OUTPUT, role.ioIndex);
        NN_VALIDATE(success);
        operands.push_back(model->main.operands[outputIndexes[role.ioIndex]]);
    }

    CHECK(!operands.empty());
    const auto opType = operands.front().type;

    Dimensions dimensions = desc.dimensions;
    for (const auto& operand : operands) {
        NN_VALIDATE_EQ(operand.type, opType) << operand.type << " vs " << operands.front().type;
        NN_VALIDATE_EQ(operand.scale, operands.front().scale);
        NN_VALIDATE_EQ(operand.zeroPoint, operands.front().zeroPoint);
        // NOTE: validateMemoryDesc cannot validate extra parameters for extension operand type.
        if (!isExtension(opType)) {
            NN_VALIDATE_EQ(operand.extraParams, operands.front().extraParams)
                    << operand.extraParams << " vs " << operands.front().extraParams;
        }
        dimensions = NN_TRY(combineDimensions(dimensions, operand.dimensions));
    }

    // NOTE: validateMemoryDesc cannot validate scalar dimensions with extension operand type.
    if (!isExtension(opType)) {
        NN_VALIDATE(!isNonExtensionScalar(opType) || dimensions.empty())
                << "invalid dimensions with scalar operand type.";
    }

    if (preparedModelRoles != nullptr) {
        *preparedModelRoles = std::move(roles);
    }
    if (combinedOperand != nullptr) {
        *combinedOperand = operands.front();
        combinedOperand->dimensions = dimensions;
    }
    return Version::ANDROID_R;
}

class OperationValidationContext : public IOperationValidationContext {
    DISALLOW_IMPLICIT_CONSTRUCTORS(OperationValidationContext);

   public:
    OperationValidationContext(const char* operationName, const std::vector<uint32_t>& inputIndexes,
                               const std::vector<uint32_t>& outputIndexes,
                               const std::vector<Operand>& operands, HalVersion version)
        : operationName(operationName),
          inputIndexes(inputIndexes),
          outputIndexes(outputIndexes),
          operands(operands),
          version(version) {}

    const char* getOperationName() const override;
    HalVersion getHalVersion() const override;

    uint32_t getNumInputs() const override;
    OperandType getInputType(uint32_t index) const override;
    Shape getInputShape(uint32_t index) const override;
    const Operand::ExtraParams& getInputExtraParams(uint32_t index) const override;

    uint32_t getNumOutputs() const override;
    OperandType getOutputType(uint32_t index) const override;
    Shape getOutputShape(uint32_t index) const override;

   private:
    const Operand* getInputOperand(uint32_t index) const;
    const Operand* getOutputOperand(uint32_t index) const;

    const char* operationName;
    const std::vector<uint32_t>& inputIndexes;
    const std::vector<uint32_t>& outputIndexes;
    const std::vector<Operand>& operands;
    HalVersion version;
};

const char* OperationValidationContext::getOperationName() const {
    return operationName;
}

HalVersion OperationValidationContext::getHalVersion() const {
    return version;
}

const Operand* OperationValidationContext::getInputOperand(uint32_t index) const {
    return &operands.at(inputIndexes.at(index));
}

const Operand* OperationValidationContext::getOutputOperand(uint32_t index) const {
    return &operands.at(outputIndexes.at(index));
}

uint32_t OperationValidationContext::getNumInputs() const {
    auto count = inputIndexes.size();
    CHECK_LE(count, std::numeric_limits<uint32_t>::max());
    return static_cast<uint32_t>(count);
}

uint32_t OperationValidationContext::getNumOutputs() const {
    auto count = outputIndexes.size();
    CHECK_LE(count, std::numeric_limits<uint32_t>::max());
    return static_cast<uint32_t>(count);
}

OperandType OperationValidationContext::getInputType(uint32_t index) const {
    return getInputOperand(index)->type;
}

Shape OperationValidationContext::getInputShape(uint32_t index) const {
    const Operand* operand = getInputOperand(index);
    return {operand->type, operand->dimensions, operand->scale, operand->zeroPoint,
            operand->extraParams};
}

const Operand::ExtraParams& OperationValidationContext::getInputExtraParams(uint32_t index) const {
    return getInputOperand(index)->extraParams;
}

OperandType OperationValidationContext::getOutputType(uint32_t index) const {
    return getOutputOperand(index)->type;
}

Shape OperationValidationContext::getOutputShape(uint32_t index) const {
    const Operand* operand = getOutputOperand(index);
    return {operand->type, operand->dimensions, operand->scale, operand->zeroPoint,
            operand->extraParams};
}

// TODO(b/169345292): reduce the duplicate validation here

Result<void> validateOperandSymmPerChannelQuantParamsImpl(
        const Operand& operand, const Operand::SymmPerChannelQuantParams& channelQuant,
        const char* tag) {
    if (operand.type != OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL) {
        NN_VALIDATE_FAIL();
    }

    NN_VALIDATE_LT(channelQuant.channelDim, operand.dimensions.size()) << tag;
    NN_VALIDATE(!channelQuant.scales.empty()) << tag;
    NN_VALIDATE_EQ(channelQuant.scales.size(), operand.dimensions[channelQuant.channelDim]) << tag;
    NN_VALIDATE_NE(operand.dimensions[channelQuant.channelDim], 0u)
            << tag << " channel dimension " << channelQuant.channelDim << " is underspecified";
    for (uint32_t i = 0; i < operand.dimensions[channelQuant.channelDim]; i++) {
        NN_VALIDATE_GT(channelQuant.scales[i], 0.0f) << tag << " invalid scaleArray[" << i << "]";
    }
    return {};
}

Result<void> validateScalarDimensions(const Operand& type, const char* tag) {
    NN_VALIDATE(type.dimensions.empty()) << tag << " invalid dimensions for scalar type";
    return {};
}

Result<void> validateQuant8AsymmParams(const Operand& type, const char* tag) {
    NN_VALIDATE(0 <= type.zeroPoint && type.zeroPoint <= 255)
            << tag << " invalid zeroPoint: " << type.zeroPoint;
    NN_VALIDATE_GT(type.scale, 0.0f) << tag << " invalid scale";
    return {};
}

Result<void> validateQuant8AsymmSignedParams(const Operand& type, const char* tag) {
    NN_VALIDATE(-128 <= type.zeroPoint && type.zeroPoint <= 127)
            << tag << " invalid zeroPoint: " << type.zeroPoint;
    NN_VALIDATE_GT(type.scale, 0.0f) << tag << " invalid scale";
    return {};
}

Result<void> validateQuant8SymmParams(const Operand& type, const char* tag) {
    NN_VALIDATE_EQ(type.zeroPoint, 0) << tag << " invalid zeroPoint: " << type.zeroPoint;
    NN_VALIDATE_GT(type.scale, 0.0f) << tag << " invalid scale";
    return {};
}

Result<void> validateQuant16AsymmParams(const Operand& type, const char* tag) {
    NN_VALIDATE(0 <= type.zeroPoint && type.zeroPoint <= 65535)
            << tag << " invalid zeroPoint: " << type.zeroPoint;
    NN_VALIDATE_GT(type.scale, 0.0f) << tag << " invalid scale";
    return {};
}

Result<void> validateQuantSymmParams(const Operand& type, const char* tag) {
    NN_VALIDATE_EQ(type.zeroPoint, 0) << tag << " zeroPoint is not zero";
    NN_VALIDATE_GT(type.scale, 0.0f) << tag << " invalid scale";
    return {};
}

Result<void> validateNoQuantParams(const Operand& type, const char* tag) {
    NN_VALIDATE_EQ(type.zeroPoint, 0) << tag << " zeroPoint is not zero";
    NN_VALIDATE_EQ(type.scale, 0.0f) << tag << " scale is not zero";
    return {};
}

Result<void> validateTensorDimensions(
        const Operand& type, const Extension::OperandTypeInformation* extensionOperandTypeInfo,
        const char* tag, bool allowPartial) {
    if (!allowPartial) {
        NN_VALIDATE(!type.dimensions.empty()) << tag << " invalid operand dimensions";
    }
    uint64_t size = isExtension(type.type) ? extensionOperandTypeInfo->byteSize
                                           : getNonExtensionSize(type.type);
    constexpr uint64_t kMaxSize = std::numeric_limits<uint32_t>::max();
    for (size_t i = 0; i < type.dimensions.size(); i++) {
        if (!allowPartial) {
            NN_VALIDATE_NE(type.dimensions[i], 0u) << tag << " invalid operand dimensions";
        }
        if (type.dimensions[i] != 0) {
            size *= type.dimensions[i];
            NN_VALIDATE_LE(size, kMaxSize) << tag << " operand byte size exceeds " << kMaxSize;
        }
    }
    return {};
}

Result<void> validateOperandTypeImpl(
        const Operand& type,
        const Extension::OperandTypeInformation* const extensionOperandTypeInfo, const char* tag,
        bool allowPartial) {
    if (isExtension(type.type)) {
        NN_VALIDATE(extensionOperandTypeInfo != nullptr);
        if (extensionOperandTypeInfo->isTensor) {
            NN_TRY(validateTensorDimensions(type, extensionOperandTypeInfo, tag, allowPartial));
        } else {
            NN_TRY(validateScalarDimensions(type, tag));
        }
        return validateNoQuantParams(type, tag);
    }

    NN_VALIDATE(extensionOperandTypeInfo == nullptr);
    NN_TRY(validateOperandType(type.type));

    if (isNonExtensionScalar(type.type)) {
        NN_TRY(validateScalarDimensions(type, tag));
        if (type.type != OperandType::OEM) {  // Historically, we have allowed OEM types
                                              // to use quantization parameters.
            NN_TRY(validateNoQuantParams(type, tag));
        }
    } else {
        NN_TRY(validateTensorDimensions(type, extensionOperandTypeInfo, tag, allowPartial));
        if (type.type == OperandType::TENSOR_QUANT8_ASYMM) {
            NN_TRY(validateQuant8AsymmParams(type, tag));
        } else if (type.type == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
            NN_TRY(validateQuant8AsymmSignedParams(type, tag));
        } else if (type.type == OperandType::TENSOR_QUANT8_SYMM) {
            NN_TRY(validateQuant8SymmParams(type, tag));
        } else if (type.type == OperandType::TENSOR_QUANT16_ASYMM) {
            NN_TRY(validateQuant16AsymmParams(type, tag));
        } else if (type.type == OperandType::TENSOR_QUANT16_SYMM) {
            NN_TRY(validateQuantSymmParams(type, tag));
        } else if (type.type == OperandType::TENSOR_INT32 ||
                   type.type == OperandType::TENSOR_OEM_BYTE) {
            // TODO(b/119869082): TENSOR_INT32 should not use quantization parameters.
            // Historically, we have allowed OEM types to use quantization parameters.
        } else {
            NN_TRY(validateNoQuantParams(type, tag));
        }
    }

    return {};
}

Result<void> validateOperandListImpl(const std::vector<uint32_t>& list, size_t operandCount,
                                     const char* tag) {
    for (size_t i = 0; i < list.size(); i++) {
        NN_VALIDATE_LT(list[i], operandCount) << tag << " invalid operand index at " << i << " = "
                                              << list[i] << ", operandCount " << operandCount;
    }
    return {};
}

Result<void> validateOperationOperandTypes(const std::vector<Operand>& operands,
                                           const std::vector<uint32_t>& inputIndexes,
                                           const std::vector<OperandType>& inExpectedTypes,
                                           const std::vector<uint32_t>& outputIndexes,
                                           const std::vector<OperandType>& outExpectedInTypes) {
    NN_VALIDATE_EQ(inputIndexes.size(), inExpectedTypes.size())
            << "Wrong operand count: expected " << inputIndexes.size() << " inputs, got "
            << inputIndexes.size() << " inputs";
    NN_VALIDATE_EQ(outputIndexes.size(), outExpectedInTypes.size())
            << "Wrong operand count: expected " << outputIndexes.size() << " outputs, got "
            << outputIndexes.size() << " outputs";
    for (size_t i = 0; i < inputIndexes.size(); i++) {
        NN_VALIDATE_EQ(operands[inputIndexes[i]].type, inExpectedTypes[i])
                << "Invalid input tensor type " << operands[inputIndexes[i]].type << " for input "
                << i << ", expected " << inExpectedTypes[i];
    }
    for (size_t i = 0; i < outputIndexes.size(); i++) {
        NN_VALIDATE_EQ(operands[outputIndexes[i]].type, outExpectedInTypes[i])
                << "Invalid output tensor type " << operands[outputIndexes[i]].type << " for input "
                << i << ", expected " << outExpectedInTypes[i];
    }

    return {};
}

Result<void> validateSubgraphReference(const std::vector<Model::Subgraph>& subgraphs,
                                       const Operand& modelOperand) {
    NN_VALIDATE_EQ(modelOperand.type, OperandType::SUBGRAPH)
            << "Unexpected operand type: " << modelOperand.type;
    NN_VALIDATE_LT(modelOperand.location.offset, subgraphs.size()) << "Invalid subgraph reference";
    return {};
}
const Model::Subgraph& getSubgraph(const std::vector<Model::Subgraph>& subgraphs,
                                   const Operand& modelOperand) {
    return subgraphs.at(modelOperand.location.offset);
}
uint32_t getInputCount(const std::vector<Model::Subgraph>& subgraphs, const Operand& modelOperand) {
    return getSubgraph(subgraphs, modelOperand).inputIndexes.size();
}
uint32_t getOutputCount(const std::vector<Model::Subgraph>& subgraphs,
                        const Operand& modelOperand) {
    return getSubgraph(subgraphs, modelOperand).outputIndexes.size();
}
const Operand& getInputOperand(const std::vector<Model::Subgraph>& subgraphs,
                               const Operand& modelOperand, uint32_t index) {
    const Model::Subgraph& subgraph = getSubgraph(subgraphs, modelOperand);
    return subgraph.operands.at(subgraph.inputIndexes.at(index));
}
const Operand& getOutputOperand(const std::vector<Model::Subgraph>& subgraphs,
                                const Operand& modelOperand, uint32_t index) {
    const Model::Subgraph& subgraph = getSubgraph(subgraphs, modelOperand);
    return subgraph.operands.at(subgraph.outputIndexes.at(index));
}

// Checks if two operands have the same types, ranks (if specified), dimensions
// (if specified), scales, zeroPoints, and extraParams.
Result<void> compatible(const Operand& a, const Operand& b) {
    NN_VALIDATE_EQ(a.type, b.type) << a.type << " != " << b.type;
    if (!a.dimensions.empty() && !b.dimensions.empty()) {
        NN_VALIDATE_EQ(a.dimensions.size(), b.dimensions.size()) << "Incompatible dimensions";
        for (uint32_t i = 0, n = a.dimensions.size(); i < n; ++i) {
            if (a.dimensions[i] != 0 && b.dimensions[i] != 0) {
                NN_VALIDATE_EQ(a.dimensions[i], b.dimensions[i]) << "Incompatible dimensions";
            }
        }
    }
    NN_VALIDATE_EQ(a.scale, b.scale);
    NN_VALIDATE_EQ(a.zeroPoint, b.zeroPoint);
    NN_VALIDATE_EQ(a.extraParams, b.extraParams) << a.extraParams << " != " << b.extraParams;
    return {};
}

Result<void> validateConditionOperand(const Operand& operand) {
    NN_VALIDATE_EQ(operand.type, OperandType::TENSOR_BOOL8)
            << "Unexpected condition operand type: " << operand.type;
    NN_VALIDATE_EQ(operand.dimensions.size(), 1u) << "Condition operand must be a singleton";
    NN_VALIDATE_EQ(operand.dimensions[0], 1u) << "Condition operand must be a singleton";
    return {};
}

Result<Version> validateIfOperation(const std::vector<uint32_t>& inputs,
                                    const std::vector<uint32_t>& outputs,
                                    const std::vector<Operand>& operands,
                                    const std::vector<Model::Subgraph>& subgraphs) {
    namespace op = operation_if;
    NN_VALIDATE_GE(inputs.size(), 3u) << "IF must have at least 3 inputs";
    NN_VALIDATE_GE(outputs.size(), 1u) << "IF must have at least 1 output";
    auto validateBranchOperand = [&](const Operand& branchModelOperand) -> Result<void> {
        auto result = validateSubgraphReference(subgraphs, branchModelOperand);
        if (!result.has_value()) {
            return NN_ERROR() << std::move(result).error()
                              << "Operand is not a valid subgraph reference";
        }
        const uint32_t branchModelInputCount = getInputCount(subgraphs, branchModelOperand);
        const uint32_t branchModelOutputCount = getOutputCount(subgraphs, branchModelOperand);
        NN_VALIDATE_EQ(inputs.size(), op::kFirstInput + branchModelInputCount);
        NN_VALIDATE_EQ(outputs.size(), branchModelOutputCount);
        for (uint32_t i = 0; i < branchModelInputCount; ++i) {
            const Operand& innerOperand = getInputOperand(subgraphs, branchModelOperand, i);
            const Operand& outerOperand = operands[inputs[op::kFirstInput + i]];
            NN_TRY(compatible(innerOperand, outerOperand));
        }
        for (uint32_t i = 0; i < branchModelOutputCount; ++i) {
            const Operand& innerOperand = getOutputOperand(subgraphs, branchModelOperand, i);
            const Operand& outerOperand = operands[outputs[i]];
            NN_TRY(compatible(innerOperand, outerOperand));
        }
        return {};
    };
    auto result = validateConditionOperand(operands[inputs[op::kCondBoolOperand]]);
    if (!result.has_value()) {
        return NN_ERROR() << std::move(result).error()
                          << "Validation failed for IF condition operand";
    }
    result = validateBranchOperand(operands[inputs[op::kThenModelOperand]]);
    if (!result.has_value()) {
        return NN_ERROR() << std::move(result).error() << "Validation failed for IF then model";
    }
    result = validateBranchOperand(operands[inputs[op::kElseModelOperand]]);
    if (!result.has_value()) {
        return NN_ERROR() << std::move(result).error() << "Validation failed for IF else model";
    }
    return Version::ANDROID_R;
}

Result<Version> validateControlFlowOperandUnknownSize(const Operand& operand) {
    if (!isExtension(operand.type) && getNonExtensionSize(operand).value() == 0) {
        // 1.3 HAL (corresponding to Version::ANDROID_R) does not support CF operations with
        // operands of unknown size. See http://b/132458982#comment63.
        return Version::CURRENT_RUNTIME;
    }
    return Version::ANDROID_R;
}

Result<Version> validateWhileOperation(const std::vector<uint32_t>& inputs,
                                       const std::vector<uint32_t>& outputs,
                                       const std::vector<Operand>& operands,
                                       const std::vector<Model::Subgraph>& subgraphs) {
    // Let the loop have
    // - m >= 1 input-output operands,
    // - k >= 0 state-only operands, and
    // - n >= 0 input-only operands.
    // Then
    // - the WHILE loop operation has (2 + m + k + n) inputs and m outputs.
    // - the condition model has (m + k + n) inputs and 1 output.
    // - the body model has (m + k + n) inputs and (m + k) outputs.
    namespace op = operation_while;
    NN_VALIDATE_GE(inputs.size(), 3u) << "WHILE must have at least 3 inputs";
    NN_VALIDATE_GE(outputs.size(), 1u) << "WHILE must have at least 1 output";
    auto validateCondOperand = [&](const Operand& condModelOperand) -> Result<Version> {
        Version version = Version::ANDROID_R;
        auto result = validateSubgraphReference(subgraphs, condModelOperand);
        if (!result.has_value()) {
            return NN_ERROR() << std::move(result).error()
                              << "Operand is not a valid subgraph reference";
        }
        const uint32_t condModelInputCount = getInputCount(subgraphs, condModelOperand);
        const uint32_t condModelOutputCount = getOutputCount(subgraphs, condModelOperand);
        NN_VALIDATE_EQ(inputs.size(), op::kFirstInput + condModelInputCount);
        NN_VALIDATE_EQ(condModelOutputCount, 1u);
        for (uint32_t i = 0; i < condModelInputCount; ++i) {
            const Operand& innerOperand = getInputOperand(subgraphs, condModelOperand, i);
            const Operand& outerOperand = operands[inputs[op::kFirstInput + i]];
            NN_TRY(compatible(innerOperand, outerOperand));
            version = combineVersions(version,
                                      NN_TRY(validateControlFlowOperandUnknownSize(innerOperand)));
            version = combineVersions(version,
                                      NN_TRY(validateControlFlowOperandUnknownSize(outerOperand)));
        }
        NN_TRY(validateConditionOperand(getOutputOperand(subgraphs, condModelOperand, 0)));
        return version;
    };
    auto validateBodyOperand = [&](const Operand& bodyModelOperand) -> Result<Version> {
        Version version = Version::ANDROID_R;
        auto result = validateSubgraphReference(subgraphs, bodyModelOperand);
        if (!result.has_value()) {
            return NN_ERROR() << std::move(result).error()
                              << "Operand is not a valid subgraph reference";
        }
        const uint32_t bodyModelInputCount = getInputCount(subgraphs, bodyModelOperand);
        const uint32_t bodyModelOutputCount = getOutputCount(subgraphs, bodyModelOperand);
        NN_VALIDATE_EQ(inputs.size(), op::kFirstInput + bodyModelInputCount);
        NN_VALIDATE_GE(bodyModelOutputCount, outputs.size());
        NN_VALIDATE_GE(bodyModelInputCount, bodyModelOutputCount);
        const uint32_t inputOutputCount = outputs.size();
        const uint32_t stateOnlyCount = bodyModelOutputCount - inputOutputCount;
        const uint32_t inputOnlyCount = bodyModelInputCount - bodyModelOutputCount;
        for (uint32_t i = 0, n = inputOutputCount + stateOnlyCount + inputOnlyCount; i < n; ++i) {
            const Operand& innerOperand = getInputOperand(subgraphs, bodyModelOperand, i);
            const Operand& outerOperand = operands[inputs[op::kFirstInput + i]];
            NN_TRY(compatible(innerOperand, outerOperand));
            version = combineVersions(version,
                                      NN_TRY(validateControlFlowOperandUnknownSize(innerOperand)));
            version = combineVersions(version,
                                      NN_TRY(validateControlFlowOperandUnknownSize(outerOperand)));
        }
        for (uint32_t i = 0; i < inputOutputCount; ++i) {
            const Operand& innerOperand = getOutputOperand(subgraphs, bodyModelOperand, i);
            const Operand& outerOperand = operands[outputs[i]];
            NN_TRY(compatible(innerOperand, outerOperand));
            version = combineVersions(version,
                                      NN_TRY(validateControlFlowOperandUnknownSize(outerOperand)));
        }
        for (uint32_t i = 0, n = inputOutputCount + stateOnlyCount; i < n; ++i) {
            const Operand& inputOperand = getInputOperand(subgraphs, bodyModelOperand, i);
            const Operand& outputOperand = getOutputOperand(subgraphs, bodyModelOperand, i);
            NN_TRY(compatible(inputOperand, outputOperand));
            version = combineVersions(version,
                                      NN_TRY(validateControlFlowOperandUnknownSize(outputOperand)));
        }
        return version;
    };
    auto result = validateCondOperand(operands[inputs[op::kCondModelOperand]]);
    if (!result.has_value()) {
        return NN_ERROR() << std::move(result).error()
                          << "Validation failed for WHILE condition model";
    }
    auto version = result.value();
    result = validateBodyOperand(operands[inputs[op::kBodyModelOperand]]);
    if (!result.has_value()) {
        return NN_ERROR() << std::move(result).error() << "Validation failed for WHILE body model";
    }
    version = combineVersions(version, result.value());
    return version;
}

Result<Version> validateOperationImpl(const Operation& operation,
                                      const std::vector<Operand>& operands,
                                      const std::vector<Model::Subgraph>& subgraphs) {
    const auto opType = operation.type;
    const auto& inputIndexes = operation.inputs;
    const auto& outputIndexes = operation.outputs;

    NN_TRY(validateOperandListImpl(inputIndexes, operands.size(),
                                   "ANeuralNetworksModel_addOperation inputs"));
    NN_TRY(validateOperandListImpl(outputIndexes, operands.size(),
                                   "ANeuralNetworksModel_addOperation outputs"));

    if (isExtension(opType)) {
        // There is no other validation we can do for an extension operation.
        return Version::ANDROID_Q;
    }

    auto invalidInOutNumberMessage = [opType, &inputIndexes, &outputIndexes](int expIn,
                                                                             int expOut) {
        std::ostringstream os;
        os << "Invalid number of input operands (" << inputIndexes.size() << ", expected " << expIn
           << ") or output operands (" << outputIndexes.size() << ", expected " << expOut
           << ") for operation " << opType;
        return os.str();
    };

    switch (opType) {
        case OperationType::OEM_OPERATION: {
            return Version::ANDROID_OC_MR1;
        }
        case OperationType::RESHAPE: {
            NN_VALIDATE(inputIndexes.size() == 2 && outputIndexes.size() == 1)
                    << invalidInOutNumberMessage(2, 1);
            auto inputType = operands[inputIndexes[0]].type;
            Version version;
            std::vector<OperandType> inExpectedTypes;
            std::vector<OperandType> outExpectedTypes;
            if (inputType == OperandType::TENSOR_FLOAT32) {
                version = Version::ANDROID_OC_MR1;
                inExpectedTypes = {OperandType::TENSOR_FLOAT32, OperandType::TENSOR_INT32};
                outExpectedTypes = {OperandType::TENSOR_FLOAT32};
            } else if (inputType == OperandType::TENSOR_FLOAT16) {
                version = Version::ANDROID_Q;
                inExpectedTypes = {OperandType::TENSOR_FLOAT16, OperandType::TENSOR_INT32};
                outExpectedTypes = {OperandType::TENSOR_FLOAT16};
            } else if (inputType == OperandType::TENSOR_QUANT8_ASYMM) {
                version = Version::ANDROID_OC_MR1;
                inExpectedTypes = {OperandType::TENSOR_QUANT8_ASYMM, OperandType::TENSOR_INT32};
                outExpectedTypes = {OperandType::TENSOR_QUANT8_ASYMM};
            } else if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                version = Version::ANDROID_R;
                inExpectedTypes = {OperandType::TENSOR_QUANT8_ASYMM_SIGNED,
                                   OperandType::TENSOR_INT32};
                outExpectedTypes = {OperandType::TENSOR_QUANT8_ASYMM_SIGNED};
            } else {
                NN_VALIDATE_FAIL() << "Unsupported input tensor type for operation " << opType;
            }
            const auto inputRank = operands[inputIndexes[0]].dimensions.size();
            NN_VALIDATE_LE(inputRank, 4u)
                    << "Unsupported input tensor rank for operation " << opType;
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::DEPTH_TO_SPACE: {
            NN_VALIDATE((inputIndexes.size() == 3 || inputIndexes.size() == 2) &&
                        outputIndexes.size() == 1)
                    << "Invalid number of input operands (" << inputIndexes.size()
                    << ", expected 3 or 2) or output operands (" << outputIndexes.size()
                    << ", expected 1) for operation " << opType;
            auto inputType = operands[inputIndexes[0]].type;
            Version version;
            std::vector<OperandType> inExpectedTypes;
            std::vector<OperandType> outExpectedTypes;
            if (inputType == OperandType::TENSOR_FLOAT32) {
                version = Version::ANDROID_OC_MR1;
                inExpectedTypes = {OperandType::TENSOR_FLOAT32, OperandType::INT32};
                outExpectedTypes = {OperandType::TENSOR_FLOAT32};
            } else if (inputType == OperandType::TENSOR_FLOAT16) {
                version = Version::ANDROID_Q;
                inExpectedTypes = {OperandType::TENSOR_FLOAT16, OperandType::INT32};
                outExpectedTypes = {OperandType::TENSOR_FLOAT16};
            } else if (inputType == OperandType::TENSOR_QUANT8_ASYMM) {
                version = Version::ANDROID_OC_MR1;
                inExpectedTypes = {OperandType::TENSOR_QUANT8_ASYMM, OperandType::INT32};
                outExpectedTypes = {OperandType::TENSOR_QUANT8_ASYMM};
            } else if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                version = Version::ANDROID_R;
                inExpectedTypes = {OperandType::TENSOR_QUANT8_ASYMM_SIGNED, OperandType::INT32};
                outExpectedTypes = {OperandType::TENSOR_QUANT8_ASYMM_SIGNED};
            } else {
                NN_VALIDATE_FAIL() << "Unsupported input tensor type for operation " << opType;
            }
            if (inputIndexes.size() == 3) {
                inExpectedTypes.push_back(OperandType::BOOL);
                version = combineVersions(version, Version::ANDROID_Q);
            } else {
                version = combineVersions(version, Version::ANDROID_OC_MR1);
            }
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::SPACE_TO_DEPTH: {
            NN_VALIDATE((inputIndexes.size() == 3 || inputIndexes.size() == 2) &&
                        outputIndexes.size() == 1)
                    << "Invalid number of input operands (" << inputIndexes.size()
                    << ", expected 3 or 2) or output operands (" << outputIndexes.size()
                    << ", expected 1) for operation " << opType;
            auto inputType = operands[inputIndexes[0]].type;
            Version version;
            std::vector<OperandType> inExpectedTypes;
            std::vector<OperandType> outExpectedTypes;
            if (inputType == OperandType::TENSOR_FLOAT32) {
                version = Version::ANDROID_OC_MR1;
                inExpectedTypes = {OperandType::TENSOR_FLOAT32, OperandType::INT32};
                outExpectedTypes = {OperandType::TENSOR_FLOAT32};
            } else if (inputType == OperandType::TENSOR_FLOAT16) {
                version = Version::ANDROID_Q;
                inExpectedTypes = {OperandType::TENSOR_FLOAT16, OperandType::INT32};
                outExpectedTypes = {OperandType::TENSOR_FLOAT16};
            } else if (inputType == OperandType::TENSOR_QUANT8_ASYMM) {
                version = Version::ANDROID_OC_MR1;
                inExpectedTypes = {OperandType::TENSOR_QUANT8_ASYMM, OperandType::INT32};
                outExpectedTypes = {OperandType::TENSOR_QUANT8_ASYMM};
            } else if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                version = Version::ANDROID_R;
                inExpectedTypes = {OperandType::TENSOR_QUANT8_ASYMM_SIGNED, OperandType::INT32};
                outExpectedTypes = {OperandType::TENSOR_QUANT8_ASYMM_SIGNED};
            } else {
                NN_VALIDATE_FAIL() << "Unsupported input tensor type for operation " << opType;
            }
            if (inputIndexes.size() == 3) {
                inExpectedTypes.push_back(OperandType::BOOL);
                version = combineVersions(version, Version::ANDROID_Q);
            } else {
                version = combineVersions(version, Version::ANDROID_OC_MR1);
            }
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::EMBEDDING_LOOKUP: {
            NN_VALIDATE(inputIndexes.size() == 2 && outputIndexes.size() == 1)
                    << invalidInOutNumberMessage(2, 1);
            auto inputType = operands[inputIndexes[1]].type;
            NN_VALIDATE(inputType == OperandType::TENSOR_FLOAT16 ||
                        inputType == OperandType::TENSOR_FLOAT32 ||
                        inputType == OperandType::TENSOR_INT32 ||
                        inputType == OperandType::TENSOR_QUANT8_ASYMM ||
                        inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)
                    << "Unsupported input tensor type for operation " << opType;
            Version version;
            std::vector<OperandType> inExpectedTypes = {OperandType::TENSOR_INT32, inputType};
            std::vector<OperandType> outExpectedTypes = {inputType};
            if (inputType == OperandType::TENSOR_FLOAT16 ||
                inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                version = Version::ANDROID_R;
            } else if (inputType == OperandType::TENSOR_INT32 ||
                       inputType == OperandType::TENSOR_QUANT8_ASYMM) {
                version = Version::ANDROID_Q;
            } else {
                version = Version::ANDROID_OC_MR1;
            }
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::HASHTABLE_LOOKUP: {
            NN_VALIDATE(inputIndexes.size() == 3 && outputIndexes.size() == 2)
                    << invalidInOutNumberMessage(3, 2);
            auto inputType = operands[inputIndexes[2]].type;
            NN_VALIDATE(inputType == OperandType::TENSOR_FLOAT32 ||
                        inputType == OperandType::TENSOR_INT32 ||
                        inputType == OperandType::TENSOR_QUANT8_ASYMM)
                    << "Unsupported input tensor type for operation " << opType;
            std::vector<OperandType> inExpectedTypes = {OperandType::TENSOR_INT32,
                                                        OperandType::TENSOR_INT32, inputType};
            std::vector<OperandType> outExpectedTypes = {inputType,
                                                         OperandType::TENSOR_QUANT8_ASYMM};
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return Version::ANDROID_OC_MR1;
        }
        case OperationType::LSH_PROJECTION: {
            NN_VALIDATE(inputIndexes.size() == 4 && outputIndexes.size() == 1)
                    << invalidInOutNumberMessage(4, 1);
            auto inputType = operands[inputIndexes[1]].type;
            NN_VALIDATE(inputType == OperandType::TENSOR_FLOAT16 ||
                        inputType == OperandType::TENSOR_FLOAT32 ||
                        inputType == OperandType::TENSOR_INT32 ||
                        inputType == OperandType::TENSOR_QUANT8_ASYMM)
                    << "Unsupported input tensor type for operation " << opType;
            auto hashType = operands[inputIndexes[0]].type;
            Version version;
            std::vector<OperandType> inExpectedTypes;
            if (hashType == OperandType::TENSOR_FLOAT16) {
                version = Version::ANDROID_Q;
                inExpectedTypes = {
                        OperandType::TENSOR_FLOAT16,
                        inputType,
                        OperandType::TENSOR_FLOAT16,
                        OperandType::INT32,
                };
            } else if (hashType == OperandType::TENSOR_FLOAT32) {
                version = Version::ANDROID_OC_MR1;
                inExpectedTypes = {
                        OperandType::TENSOR_FLOAT32,
                        inputType,
                        OperandType::TENSOR_FLOAT32,
                        OperandType::INT32,
                };
            } else {
                NN_VALIDATE_FAIL() << "Unsupported hash tensor type for operation " << opType;
            }
            std::vector<OperandType> outExpectedTypes = {OperandType::TENSOR_INT32};
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::BIDIRECTIONAL_SEQUENCE_LSTM: {
            const uint32_t kNumOutputs = 2;
            const uint32_t kNumOutputsMerged = 1;
            const uint32_t kNumOutputsWithState = 6;
            const uint32_t kNumOutputsMergedWithState = 5;
            NN_VALIDATE(inputIndexes.size() == 61 &&
                        (outputIndexes.size() == kNumOutputs ||
                         outputIndexes.size() == kNumOutputsMerged ||
                         outputIndexes.size() == kNumOutputsWithState ||
                         outputIndexes.size() == kNumOutputsMergedWithState))
                    << "Invalid number of input operands (" << inputIndexes.size()
                    << ", expected 61) or output operands (" << outputIndexes.size()
                    << ", expected 1, 2, 5 or 6) for operation " << opType;

            std::vector<OperandType> inExpectedTypes;
            auto inputType = operands[inputIndexes[0]].type;
            NN_VALIDATE(inputType == OperandType::TENSOR_FLOAT32 ||
                        inputType == OperandType::TENSOR_FLOAT16)
                    << "Unsupported input tensor type for operation " << opType;

            inExpectedTypes = {};
            for (int i = 0; i < 48; ++i) {
                inExpectedTypes.push_back(inputType);
            }
            inExpectedTypes.push_back(OperandType::INT32);
            inExpectedTypes.push_back(inputType == OperandType::TENSOR_FLOAT32
                                              ? OperandType::FLOAT32
                                              : OperandType::FLOAT16);
            inExpectedTypes.push_back(inputType == OperandType::TENSOR_FLOAT32
                                              ? OperandType::FLOAT32
                                              : OperandType::FLOAT16);
            inExpectedTypes.push_back(OperandType::BOOL);
            inExpectedTypes.push_back(OperandType::BOOL);
            for (int i = 0; i < 8; ++i) {
                inExpectedTypes.push_back(inputType);
            }

            Version version = Version::ANDROID_Q;
            if (outputIndexes.size() == kNumOutputsWithState ||
                outputIndexes.size() == kNumOutputsMergedWithState) {
                version = Version::ANDROID_R;
            }
            std::vector<OperandType> outExpectedTypes(outputIndexes.size(), inputType);
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::LSTM: {
            NN_VALIDATE((inputIndexes.size() == 23 || inputIndexes.size() == 27) &&
                        outputIndexes.size() == 4)
                    << "Invalid number of input operands (" << inputIndexes.size()
                    << ", expected 23 or 27) or output operands (" << outputIndexes.size()
                    << ", expected 4) for operation " << opType;
            std::vector<OperandType> inExpectedTypes;
            std::vector<OperandType> outExpectedTypes;
            auto inputType = operands[inputIndexes[0]].type;
            NN_VALIDATE(inputType == OperandType::TENSOR_FLOAT32 ||
                        inputType == OperandType::TENSOR_FLOAT16)
                    << "Unsupported input tensor type for operation " << opType;

            Version version = Version::ANDROID_OC_MR1;
            inExpectedTypes = {inputType,         inputType, inputType, inputType, inputType,
                               inputType,         inputType, inputType, inputType, inputType,
                               inputType,         inputType, inputType, inputType, inputType,
                               inputType,         inputType, inputType, inputType, inputType,
                               OperandType::INT32};
            if (inputType == OperandType::TENSOR_FLOAT32) {
                inExpectedTypes.push_back(OperandType::FLOAT32);
                inExpectedTypes.push_back(OperandType::FLOAT32);
            } else {
                version = Version::ANDROID_Q;
                inExpectedTypes.push_back(OperandType::FLOAT16);
                inExpectedTypes.push_back(OperandType::FLOAT16);
            }

            outExpectedTypes = {inputType, inputType, inputType, inputType};
            if (inputIndexes.size() == 23) {
                version = combineVersions(version, Version::ANDROID_OC_MR1);
            } else {
                version = combineVersions(version, Version::ANDROID_Q);
                for (int i = 0; i < 4; ++i) {
                    inExpectedTypes.push_back(inputType);
                }
            }
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::QUANTIZED_16BIT_LSTM: {
            NN_VALIDATE(inputIndexes.size() == 15 && outputIndexes.size() == 2)
                    << invalidInOutNumberMessage(15, 2);
            std::vector<OperandType> inExpectedTypes = {
                    OperandType::TENSOR_QUANT8_ASYMM, OperandType::TENSOR_QUANT8_ASYMM,
                    OperandType::TENSOR_QUANT8_ASYMM, OperandType::TENSOR_QUANT8_ASYMM,
                    OperandType::TENSOR_QUANT8_ASYMM, OperandType::TENSOR_QUANT8_ASYMM,
                    OperandType::TENSOR_QUANT8_ASYMM, OperandType::TENSOR_QUANT8_ASYMM,
                    OperandType::TENSOR_QUANT8_ASYMM, OperandType::TENSOR_INT32,
                    OperandType::TENSOR_INT32,        OperandType::TENSOR_INT32,
                    OperandType::TENSOR_INT32,        OperandType::TENSOR_QUANT16_SYMM,
                    OperandType::TENSOR_QUANT8_ASYMM};
            std::vector<OperandType> outExpectedTypes = {OperandType::TENSOR_QUANT16_SYMM,
                                                         OperandType::TENSOR_QUANT8_ASYMM};
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return Version::ANDROID_Q;
        }
        case OperationType::RANDOM_MULTINOMIAL: {
            NN_VALIDATE(inputIndexes.size() == 3 && outputIndexes.size() == 1)
                    << invalidInOutNumberMessage(3, 1);
            OperandType inputType = operands[inputIndexes[0]].type;
            std::vector<OperandType> inExpectedTypes;
            if (inputType == OperandType::TENSOR_FLOAT32 ||
                inputType == OperandType::TENSOR_FLOAT16) {
                inExpectedTypes = {inputType, OperandType::INT32, OperandType::TENSOR_INT32};
            } else {
                NN_VALIDATE_FAIL() << "Unsupported input tensor type for operation " << opType;
            }
            std::vector<OperandType> outExpectedTypes = {OperandType::TENSOR_INT32};
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return Version::ANDROID_Q;
        }
        case OperationType::RNN: {
            NN_VALIDATE(inputIndexes.size() == 6 && outputIndexes.size() == 2)
                    << invalidInOutNumberMessage(6, 2);
            OperandType inputType = operands[inputIndexes[0]].type;
            Version version;
            std::vector<OperandType> inExpectedTypes;
            std::vector<OperandType> outExpectedTypes;
            if (inputType == OperandType::TENSOR_FLOAT32) {
                version = Version::ANDROID_OC_MR1;
                inExpectedTypes = {
                        OperandType::TENSOR_FLOAT32, OperandType::TENSOR_FLOAT32,
                        OperandType::TENSOR_FLOAT32, OperandType::TENSOR_FLOAT32,
                        OperandType::TENSOR_FLOAT32, OperandType::INT32,
                };
                outExpectedTypes = {
                        OperandType::TENSOR_FLOAT32,
                        OperandType::TENSOR_FLOAT32,
                };
            } else if (inputType == OperandType::TENSOR_FLOAT16) {
                version = Version::ANDROID_Q;
                inExpectedTypes = {
                        OperandType::TENSOR_FLOAT16, OperandType::TENSOR_FLOAT16,
                        OperandType::TENSOR_FLOAT16, OperandType::TENSOR_FLOAT16,
                        OperandType::TENSOR_FLOAT16, OperandType::INT32,
                };
                outExpectedTypes = {
                        OperandType::TENSOR_FLOAT16,
                        OperandType::TENSOR_FLOAT16,
                };
            } else {
                NN_VALIDATE_FAIL() << "Unsupported input tensor type for operation " << opType;
            }
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::SVDF: {
            NN_VALIDATE(inputIndexes.size() == 7 && outputIndexes.size() == 2)
                    << invalidInOutNumberMessage(7, 2);
            Version version;
            OperandType inputType = operands[inputIndexes[0]].type;
            if (inputType == OperandType::TENSOR_FLOAT32) {
                version = Version::ANDROID_OC_MR1;
            } else if (inputType == OperandType::TENSOR_FLOAT16) {
                version = Version::ANDROID_Q;
            } else {
                NN_VALIDATE_FAIL() << "Unsupported input tensor type for operation " << opType;
            }
            std::vector<OperandType> inExpectedTypes = {
                    inputType, inputType,          inputType,          inputType,
                    inputType, OperandType::INT32, OperandType::INT32,
            };
            std::vector<OperandType> outExpectedTypes = {inputType, inputType};
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::BATCH_TO_SPACE_ND: {
            NN_VALIDATE((inputIndexes.size() == 3 || inputIndexes.size() == 2) &&
                        outputIndexes.size() == 1)
                    << "Invalid number of input operands (" << inputIndexes.size()
                    << ", expected 3 or 2) or output operands (" << outputIndexes.size()
                    << ", expected 1) for operation " << opType;
            auto inputType = operands[inputIndexes[0]].type;
            Version version = Version::ANDROID_OC_MR1;
            std::vector<OperandType> inExpectedTypes;
            std::vector<OperandType> outExpectedTypes;
            if (inputType == OperandType::TENSOR_FLOAT32) {
                inExpectedTypes = {
                        OperandType::TENSOR_FLOAT32,
                        OperandType::TENSOR_INT32,
                };
                outExpectedTypes = {OperandType::TENSOR_FLOAT32};
            } else if (inputType == OperandType::TENSOR_FLOAT16) {
                version = Version::ANDROID_Q;
                inExpectedTypes = {
                        OperandType::TENSOR_FLOAT16,
                        OperandType::TENSOR_INT32,
                };
                outExpectedTypes = {OperandType::TENSOR_FLOAT16};
            } else if (inputType == OperandType::TENSOR_QUANT8_ASYMM) {
                inExpectedTypes = {
                        OperandType::TENSOR_QUANT8_ASYMM,
                        OperandType::TENSOR_INT32,
                };
                outExpectedTypes = {OperandType::TENSOR_QUANT8_ASYMM};
            } else if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                version = Version::ANDROID_R;
                inExpectedTypes = {
                        OperandType::TENSOR_QUANT8_ASYMM_SIGNED,
                        OperandType::TENSOR_INT32,
                };
                outExpectedTypes = {OperandType::TENSOR_QUANT8_ASYMM_SIGNED};
            } else {
                NN_VALIDATE_FAIL() << "Unsupported input tensor type for operation " << opType;
            }
            if (inputIndexes.size() == 3) {
                inExpectedTypes.push_back(OperandType::BOOL);
                version = combineVersions(version, Version::ANDROID_Q);
            } else {
                version = combineVersions(version, Version::ANDROID_P);
            }
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::SPACE_TO_BATCH_ND: {
            NN_VALIDATE((inputIndexes.size() == 4 || inputIndexes.size() == 3) &&
                        outputIndexes.size() == 1)
                    << "Invalid number of input operands (" << inputIndexes.size()
                    << ", expected 4 or 3) or output operands (" << outputIndexes.size()
                    << ", expected 1) for operation " << opType;
            auto inputType = operands[inputIndexes[0]].type;
            Version version = Version::ANDROID_OC_MR1;
            std::vector<OperandType> inExpectedTypes;
            std::vector<OperandType> outExpectedTypes;
            if (inputType == OperandType::TENSOR_FLOAT32) {
                inExpectedTypes = {
                        OperandType::TENSOR_FLOAT32,
                        OperandType::TENSOR_INT32,
                        OperandType::TENSOR_INT32,
                };
                outExpectedTypes = {OperandType::TENSOR_FLOAT32};
            } else if (inputType == OperandType::TENSOR_FLOAT16) {
                version = Version::ANDROID_Q;
                inExpectedTypes = {
                        OperandType::TENSOR_FLOAT16,
                        OperandType::TENSOR_INT32,
                        OperandType::TENSOR_INT32,
                };
                outExpectedTypes = {OperandType::TENSOR_FLOAT16};
            } else if (inputType == OperandType::TENSOR_QUANT8_ASYMM) {
                if (operands[inputIndexes[0]].zeroPoint != 0) {
                    version = Version::ANDROID_Q;
                }
                inExpectedTypes = {
                        OperandType::TENSOR_QUANT8_ASYMM,
                        OperandType::TENSOR_INT32,
                        OperandType::TENSOR_INT32,
                };
                outExpectedTypes = {OperandType::TENSOR_QUANT8_ASYMM};
            } else if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                version = Version::ANDROID_R;
                inExpectedTypes = {
                        OperandType::TENSOR_QUANT8_ASYMM_SIGNED,
                        OperandType::TENSOR_INT32,
                        OperandType::TENSOR_INT32,
                };
                outExpectedTypes = {OperandType::TENSOR_QUANT8_ASYMM_SIGNED};
            } else {
                NN_VALIDATE_FAIL() << "Unsupported input tensor type for operation " << opType;
            }
            if (inputIndexes.size() == 4) {
                inExpectedTypes.push_back(OperandType::BOOL);
                version = combineVersions(version, Version::ANDROID_Q);
            } else {
                version = combineVersions(version, Version::ANDROID_P);
            }
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::PAD: {
            NN_VALIDATE(inputIndexes.size() == 2 && outputIndexes.size() == 1)
                    << invalidInOutNumberMessage(2, 1);
            auto inputType = operands[inputIndexes[0]].type;
            Version version;
            std::vector<OperandType> inExpectedTypes;
            std::vector<OperandType> outExpectedTypes;
            if (inputType == OperandType::TENSOR_FLOAT32) {
                version = Version::ANDROID_P;
                inExpectedTypes = {
                        OperandType::TENSOR_FLOAT32,
                        OperandType::TENSOR_INT32,
                };
                outExpectedTypes = {OperandType::TENSOR_FLOAT32};
            } else if (inputType == OperandType::TENSOR_FLOAT16) {
                version = Version::ANDROID_Q;
                inExpectedTypes = {
                        OperandType::TENSOR_FLOAT16,
                        OperandType::TENSOR_INT32,
                };
                outExpectedTypes = {OperandType::TENSOR_FLOAT16};
            } else if (inputType == OperandType::TENSOR_QUANT8_ASYMM ||
                       inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                    version = Version::ANDROID_R;
                } else {
                    if (operands[inputIndexes[0]].zeroPoint == 0) {
                        version = Version::ANDROID_P;
                    } else {
                        version = Version::ANDROID_Q;
                    }
                }
                inExpectedTypes = {
                        inputType,
                        OperandType::TENSOR_INT32,
                };
                outExpectedTypes = {inputType};
            } else {
                NN_VALIDATE_FAIL() << "Unsupported input tensor type for operation " << opType;
            }
            const auto inputRank = operands[inputIndexes[0]].dimensions.size();
            NN_VALIDATE_LE(inputRank, 4u)
                    << "Unsupported input tensor rank for operation " << opType;
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::PAD_V2: {
            NN_VALIDATE(inputIndexes.size() == 3 && outputIndexes.size() == 1)
                    << invalidInOutNumberMessage(3, 1);
            auto inputType = operands[inputIndexes[0]].type;
            Version version;
            std::vector<OperandType> inExpectedTypes;
            std::vector<OperandType> outExpectedTypes;
            if (inputType == OperandType::TENSOR_FLOAT32) {
                version = Version::ANDROID_Q;
                inExpectedTypes = {
                        OperandType::TENSOR_FLOAT32,
                        OperandType::TENSOR_INT32,
                        OperandType::FLOAT32,
                };
                outExpectedTypes = {OperandType::TENSOR_FLOAT32};
            } else if (inputType == OperandType::TENSOR_FLOAT16) {
                version = Version::ANDROID_Q;
                inExpectedTypes = {
                        OperandType::TENSOR_FLOAT16,
                        OperandType::TENSOR_INT32,
                        OperandType::FLOAT16,
                };
                outExpectedTypes = {OperandType::TENSOR_FLOAT16};
            } else if (inputType == OperandType::TENSOR_QUANT8_ASYMM ||
                       inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                    version = Version::ANDROID_R;
                } else {
                    version = Version::ANDROID_Q;
                }
                inExpectedTypes = {
                        inputType,
                        OperandType::TENSOR_INT32,
                        OperandType::INT32,
                };  // TODO(b/116699425): Make it UINT8.
                outExpectedTypes = {inputType};
            } else {
                NN_VALIDATE_FAIL() << "Unsupported input tensor type for operation " << opType;
            }
            const auto inputRank = operands[inputIndexes[0]].dimensions.size();
            NN_VALIDATE_LE(inputRank, 4u)
                    << "Unsupported input tensor rank for operation " << opType;
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::CAST: {
            NN_VALIDATE(inputIndexes.size() == 1 && outputIndexes.size() == 1)
                    << invalidInOutNumberMessage(1, 1);
            auto inputOperand = operands[inputIndexes[0]];
            auto outputOperand = operands[outputIndexes[0]];
            auto inputType = inputOperand.type;
            auto outputType = outputOperand.type;
            Version version;
            std::vector<OperandType> inExpectedTypes;
            std::vector<OperandType> outExpectedTypes;
            if ((inputType == OperandType::TENSOR_FLOAT16 ||
                 inputType == OperandType::TENSOR_FLOAT32 ||
                 inputType == OperandType::TENSOR_INT32 ||
                 inputType == OperandType::TENSOR_QUANT8_ASYMM) &&
                (outputType == OperandType::TENSOR_FLOAT16 ||
                 outputType == OperandType::TENSOR_FLOAT32 ||
                 outputType == OperandType::TENSOR_INT32 ||
                 outputType == OperandType::TENSOR_QUANT8_ASYMM)) {
                version = Version::ANDROID_Q;
                inExpectedTypes = {inputType};
                outExpectedTypes = {outputType};
            } else if (inputType == OperandType::TENSOR_BOOL8 ||
                       inputType == OperandType::TENSOR_QUANT16_ASYMM ||
                       inputType == OperandType::TENSOR_QUANT16_SYMM ||
                       inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED ||
                       inputType == OperandType::TENSOR_QUANT8_SYMM) {
                version = Version::ANDROID_R;
                inExpectedTypes = {inputType};
                outExpectedTypes = {inputType};  // Only identity CAST is supported.
            } else {
                NN_VALIDATE_FAIL() << "Unsupported data type for operation " << opType;
            }
            // Validate that output shape is equal to input shape if dimensions
            // are already known.
            auto getNumberOfElements = [](const std::vector<uint32_t>& dims) {
                if (dims.empty()) {
                    return 0;
                }
                return std::accumulate(dims.begin(), dims.end(), 1, std::multiplies<>());
            };
            NN_VALIDATE(inputOperand.dimensions.empty() || outputOperand.dimensions.empty() ||
                        getNumberOfElements(outputOperand.dimensions) == 0 ||
                        inputOperand.dimensions == outputOperand.dimensions);
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::MEAN: {
            NN_VALIDATE(inputIndexes.size() == 3 && outputIndexes.size() == 1)
                    << invalidInOutNumberMessage(3, 1);
            const auto inputRank = operands[inputIndexes[0]].dimensions.size();
            NN_VALIDATE_LE(inputRank, 4u)
                    << "Unsupported input tensor rank for operation " << opType;
            auto inputType = operands[inputIndexes[0]].type;
            Version version;
            if (inputType == OperandType::TENSOR_FLOAT32 ||
                inputType == OperandType::TENSOR_QUANT8_ASYMM) {
                version = Version::ANDROID_P;
            } else if (inputType == OperandType::TENSOR_FLOAT16) {
                version = Version::ANDROID_Q;
            } else if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                version = Version::ANDROID_R;
            } else {
                NN_VALIDATE_FAIL() << "Unsupported input tensor type for operation " << opType;
            }
            std::vector<OperandType> inExpectedTypes = {inputType, OperandType::TENSOR_INT32,
                                                        OperandType::INT32};
            std::vector<OperandType> outExpectedTypes = {inputType};
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::ARGMAX:
        case OperationType::ARGMIN: {
            NN_VALIDATE(inputIndexes.size() == 2 && outputIndexes.size() == 1)
                    << invalidInOutNumberMessage(2, 1);
            auto inputType = operands[inputIndexes[0]].type;
            std::vector<OperandType> inExpectedTypes;
            std::vector<OperandType> outExpectedTypes;
            if (inputType == OperandType::TENSOR_FLOAT16 ||
                inputType == OperandType::TENSOR_FLOAT32 ||
                inputType == OperandType::TENSOR_INT32 ||
                inputType == OperandType::TENSOR_QUANT8_ASYMM ||
                inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                inExpectedTypes = {inputType, OperandType::INT32};
                outExpectedTypes = {OperandType::TENSOR_INT32};
            } else {
                NN_VALIDATE_FAIL() << "Unsupported input tensor type for operation " << opType;
            }
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return Version::ANDROID_Q;
        }
        case OperationType::EXPAND_DIMS: {
            NN_VALIDATE(inputIndexes.size() == 2 && outputIndexes.size() == 1)
                    << invalidInOutNumberMessage(2, 1);
            auto inputType = operands[inputIndexes[0]].type;
            std::vector<OperandType> inExpectedTypes;
            std::vector<OperandType> outExpectedTypes;
            if (inputType == OperandType::TENSOR_FLOAT16 ||
                inputType == OperandType::TENSOR_FLOAT32 ||
                inputType == OperandType::TENSOR_INT32 ||
                inputType == OperandType::TENSOR_QUANT8_ASYMM ||
                inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                inExpectedTypes = {inputType, OperandType::INT32};
                outExpectedTypes = {inputType};
            } else {
                NN_VALIDATE_FAIL() << "Unsupported input tensor type for operation " << opType;
            }
            Version version;
            if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                version = Version::ANDROID_R;
            } else {
                version = Version::ANDROID_Q;
            }
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::SPLIT: {
            NN_VALIDATE_EQ(inputIndexes.size(), 3u)
                    << "Invalid number of input operands (" << inputIndexes.size()
                    << ", expected 3)" << opType;
            auto inputType = operands[inputIndexes[0]].type;
            NN_VALIDATE(inputType == OperandType::TENSOR_FLOAT16 ||
                        inputType == OperandType::TENSOR_FLOAT32 ||
                        inputType == OperandType::TENSOR_INT32 ||
                        inputType == OperandType::TENSOR_QUANT8_ASYMM ||
                        inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED)
                    << "Unsupported input tensor type for operation " << opType;
            Version version;
            if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                version = Version::ANDROID_R;
            } else {
                version = Version::ANDROID_Q;
            }
            std::vector<OperandType> inExpectedTypes = {inputType, OperandType::INT32,
                                                        OperandType::INT32};
            std::vector<OperandType> outExpectedTypes(outputIndexes.size(), inputType);
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::MAXIMUM:
        case OperationType::MINIMUM: {
            NN_VALIDATE(inputIndexes.size() == 2 && outputIndexes.size() == 1)
                    << invalidInOutNumberMessage(2, 1);
            std::vector<OperandType> inExpectedTypes;
            std::vector<OperandType> outExpectedTypes;
            OperandType inputType = operands[inputIndexes[0]].type;
            if (inputType == OperandType::TENSOR_FLOAT16 ||
                inputType == OperandType::TENSOR_FLOAT32 ||
                inputType == OperandType::TENSOR_INT32 ||
                inputType == OperandType::TENSOR_QUANT8_ASYMM ||
                inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                inExpectedTypes = {inputType, inputType};
                outExpectedTypes = {inputType};
            } else {
                NN_VALIDATE_FAIL() << "Unsupported input tensor type for operation " << opType;
            }
            Version version;
            if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                version = Version::ANDROID_R;
            } else {
                version = Version::ANDROID_Q;
            }
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::GROUPED_CONV_2D: {
            NN_VALIDATE((inputIndexes.size() == 12 || inputIndexes.size() == 9) &&
                        outputIndexes.size() == 1)
                    << "Invalid number of input operands (" << inputIndexes.size()
                    << ", expected 12 or 9) or output operands (" << outputIndexes.size()
                    << ", expected 1) for operation " << opType;
            auto inputType = operands[inputIndexes[0]].type;
            auto filterType = operands[inputIndexes[1]].type;
            std::vector<OperandType> inExpectedTypes;
            std::vector<OperandType> outExpectedTypes;
            if (inputType == OperandType::TENSOR_FLOAT32) {
                inExpectedTypes = {OperandType::TENSOR_FLOAT32, OperandType::TENSOR_FLOAT32,
                                   OperandType::TENSOR_FLOAT32, OperandType::INT32,
                                   OperandType::INT32,          OperandType::INT32,
                                   OperandType::INT32,          OperandType::INT32};
                outExpectedTypes = {OperandType::TENSOR_FLOAT32};
            } else if (inputType == OperandType::TENSOR_FLOAT16) {
                inExpectedTypes = {OperandType::TENSOR_FLOAT16, OperandType::TENSOR_FLOAT16,
                                   OperandType::TENSOR_FLOAT16, OperandType::INT32,
                                   OperandType::INT32,          OperandType::INT32,
                                   OperandType::INT32,          OperandType::INT32};
                outExpectedTypes = {OperandType::TENSOR_FLOAT16};
            } else if (inputType == OperandType::TENSOR_QUANT8_ASYMM ||
                       inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                NN_VALIDATE(filterType == inputType ||
                            filterType == OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL)
                        << "Unsupported filter tensor type for operation " << opType;

                NN_VALIDATE(filterType != OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL ||
                            std::get<Operand::SymmPerChannelQuantParams>(
                                    operands[inputIndexes[1]].extraParams)
                                            .channelDim == 0)
                        << "Unsupported filter tensor channel dimension for operation " << opType;

                inExpectedTypes = {
                        inputType,          filterType,         OperandType::TENSOR_INT32,
                        OperandType::INT32, OperandType::INT32, OperandType::INT32,
                        OperandType::INT32, OperandType::INT32};
                outExpectedTypes = {inputType};
            } else {
                NN_VALIDATE_FAIL() << "Unsupported input tensor type for operation " << opType;
            }

            if (inputIndexes.size() == 12) {
                std::vector<OperandType> explicitScalarTypes(3, OperandType::INT32);
                inExpectedTypes.insert(inExpectedTypes.end(), explicitScalarTypes.begin(),
                                       explicitScalarTypes.end());
            }
            inExpectedTypes.push_back(OperandType::BOOL);
            Version version;
            if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                version = Version::ANDROID_R;
            } else {
                version = Version::ANDROID_Q;
            }
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::TILE: {
            NN_VALIDATE(inputIndexes.size() == 2 && outputIndexes.size() == 1)
                    << invalidInOutNumberMessage(2, 1);
            auto inputType = operands[inputIndexes[0]].type;
            std::vector<OperandType> inExpectedTypes;
            std::vector<OperandType> outExpectedTypes;
            if (inputType == OperandType::TENSOR_FLOAT16 ||
                inputType == OperandType::TENSOR_FLOAT32 ||
                inputType == OperandType::TENSOR_INT32 ||
                inputType == OperandType::TENSOR_QUANT8_ASYMM ||
                inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                inExpectedTypes = {inputType, OperandType::TENSOR_INT32};
                outExpectedTypes = {inputType};
            } else {
                NN_VALIDATE_FAIL() << "Unsupported input tensor type for operation " << opType;
            }
            Version version;
            if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                version = Version::ANDROID_R;
            } else {
                version = Version::ANDROID_Q;
            }
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::POW: {
            NN_VALIDATE(inputIndexes.size() == 2 && outputIndexes.size() == 1)
                    << invalidInOutNumberMessage(2, 1);
            auto inputType = operands[inputIndexes[0]].type;
            std::vector<OperandType> inExpectedTypes;
            std::vector<OperandType> outExpectedTypes;
            if (inputType == OperandType::TENSOR_FLOAT16 ||
                inputType == OperandType::TENSOR_FLOAT32) {
                inExpectedTypes = {inputType, inputType};
                outExpectedTypes = {inputType};
            } else {
                NN_VALIDATE_FAIL() << "Unsupported input tensor type for operation " << opType;
            }
            Version version;
            if (inputType == OperandType::TENSOR_QUANT8_ASYMM_SIGNED) {
                version = Version::ANDROID_R;
            } else {
                version = Version::ANDROID_Q;
            }
            NN_TRY(validateOperationOperandTypes(operands, inputIndexes, inExpectedTypes,
                                                 outputIndexes, outExpectedTypes));
            return version;
        }
        case OperationType::IF: {
            return validateIfOperation(inputIndexes, outputIndexes, operands, subgraphs);
        }
        case OperationType::WHILE: {
            return validateWhileOperation(inputIndexes, outputIndexes, operands, subgraphs);
        }
        default: {
            const OperationRegistration* operationRegistration =
                    BuiltinOperationResolver::get()->findOperation(
                            static_cast<OperationType>(opType));
            // TODO: return ErrorStatus::UNEXPECTED_NULL
            NN_VALIDATE(operationRegistration != nullptr) << opType << " not registered";
            // TODO: return ErrorStatus::UNEXPECTED_NULL
            NN_VALIDATE(operationRegistration->validate != nullptr)
                    << "Incomplete operation registration: " << opType;

            constexpr HalVersion kHalVersions[] = {HalVersion::V1_0, HalVersion::V1_1,
                                                   HalVersion::V1_2, HalVersion::V1_3};
            constexpr Version kVersions[] = {Version::ANDROID_OC_MR1, Version::ANDROID_P,
                                             Version::ANDROID_Q, Version::ANDROID_R};
            static_assert(std::size(kHalVersions) == std::size(kVersions));

            for (size_t i = 0; i < std::size(kHalVersions); ++i) {
                OperationValidationContext context(operationRegistration->name, inputIndexes,
                                                   outputIndexes, operands, kHalVersions[i]);
                auto valid = operationRegistration->validate(&context);
                if (valid) {
                    return kVersions[i];
                }
            }

            return NN_ERROR() << "Validation failed for operation " << opType;
        }
    }
}

}  // anonymous namespace

Version combineVersions(Version lhs, Version rhs) {
    return std::max<Version>(lhs, rhs);
}

Result<Version> validate(const DeviceStatus& deviceStatus) {
    return validateDeviceStatus(deviceStatus);
}

Result<Version> validate(const ExecutionPreference& executionPreference) {
    return validateExecutionPreference(executionPreference);
}

Result<Version> validate(const DeviceType& deviceType) {
    return validateDeviceType(deviceType);
}

Result<Version> validate(const MeasureTiming& measureTiming) {
    return validateMeasureTiming(measureTiming);
}

Result<Version> validate(const Priority& priority) {
    return validatePriority(priority);
}

Result<Version> validate(const ErrorStatus& errorStatus) {
    return validateErrorStatus(errorStatus);
}

Result<Version> validate(const FusedActivationFunc& activation) {
    return validateFusedActivationFunc(activation);
}

Result<Version> validate(const OutputShape& outputShape) {
    return validateOutputShape(outputShape);
}

Result<Version> validate(const Timing& timing) {
    return validateTiming(timing);
}

Result<Version> validate(const Capabilities& capabilities) {
    return validateCapabilities(capabilities);
}

Result<Version> validate(const Extension& extension) {
    return validateExtension(extension);
}

Result<Version> validate(const NativeHandle& handle) {
    return validateNativeHandle(handle);
}

Result<Version> validate(const Memory& memory) {
    return validateMemory(memory);
}

Result<Version> validate(const Model& model) {
    return validateModel(model);
}

Result<Version> validate(const BufferDesc& bufferDesc) {
    return validateBufferDesc(bufferDesc);
}

Result<Version> validate(const BufferRole& bufferRole) {
    return validateBufferRole(bufferRole);
}

Result<Version> validate(const Request& request) {
    return validateRequest(request);
}

Result<Version> validate(const OptionalTimePoint& optionalTimePoint) {
    return validateOptionalTimePoint(optionalTimePoint);
}

Result<Version> validate(const OptionalTimeoutDuration& optionalTimeoutDuration) {
    return validateOptionalTimeoutDuration(optionalTimeoutDuration);
}

Result<Version> validate(const std::vector<OutputShape>& outputShapes) {
    return validateVector(outputShapes, validateOutputShape);
}

Result<Version> validate(const std::vector<Extension>& extensions) {
    return validateExtensions(extensions);
}

Result<Version> validate(const std::vector<NativeHandle>& handles) {
    return validateVector(handles, validateNativeHandle);
}

Result<Version> validate(const std::vector<BufferRole>& bufferRoles) {
    return validateVector(bufferRoles, validateBufferRole);
}

Result<Version> validateRequestForModel(const Request& request, const Model& model) {
    return validateRequestForModelImpl(request, model);
}

Result<Version> validateMemoryDesc(
        const BufferDesc& desc,
        const std::vector<std::shared_ptr<const IPreparedModel>>& preparedModels,
        const std::vector<BufferRole>& inputRoles, const std::vector<BufferRole>& outputRoles,
        const std::function<const Model*(const std::shared_ptr<const IPreparedModel>&)>& getModel,
        std::set<PreparedModelRole>* preparedModelRoles, Operand* combinedOperand) {
    return validateMemoryDescImpl(desc, preparedModels, inputRoles, outputRoles, getModel,
                                  preparedModelRoles, combinedOperand);
}

Result<void> validateOperandSymmPerChannelQuantParams(
        const Operand& operand, const Operand::SymmPerChannelQuantParams& channelQuant,
        const char* tag) {
    return validateOperandSymmPerChannelQuantParamsImpl(operand, channelQuant, tag);
}

Result<void> validateOperandType(const Operand& type,
                                 const Extension::OperandTypeInformation* extensionOperandTypeInfo,
                                 const char* tag, bool allowPartial) {
    return validateOperandTypeImpl(type, extensionOperandTypeInfo, tag, allowPartial);
}

Result<void> validateOperandList(const std::vector<uint32_t>& list, size_t operandCount,
                                 const char* tag) {
    return validateOperandListImpl(list, operandCount, tag);
}

Result<Version> validateOperation(const Operation& operation, const std::vector<Operand>& operands,
                                  const std::vector<Model::Subgraph>& subgraphs) {
    return validateOperationImpl(operation, operands, subgraphs);
}

}  // namespace android::nn