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
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789 | class NestedTensor(torch.Tensor):
r"""
A container for variable-length tensors that enables efficient batch operations.
`NestedTensor` solves a fundamental problem in deep learning: handling sequences of different lengths
in batch operations. Instead of excessive padding or complex bucketing, `NestedTensor` provides an
elegant solution that maintains both efficiency and usability.
The class provides three main views of the data:
- `.tensor`: A padded tensor with zeros (or other value) in place of missing elements
- `.mask`: A boolean mask indicating which elements are real vs padding
- `.concat`: The packed tensor containing all elements concatenated without padding
When indexing a `NestedTensor`, the behavior depends on the index type:
1. Integer index (`nt[0]`): Returns a single tensor without padding
2. Slice index (`nt[:]`): Returns a new `NestedTensor` containing the selected batch elements
3. Tuple index (`nt[:, 1:]`): Returns a new `NestedTensor` with the specified sliced shape
Attributes:
_packed_values: Packed tensor data
_offsets: Top-level cumulative element counts, shape (B+1,)
_permutation: Canonical logical-to-packed dimension permutation
_physical_shape: Per-element physical shapes, shape (B, max_ndim)
batch_first: Whether the first dimension is the batch dimension (B, N, *)
If `False`, the first dimension is the sequence dimension (N, B, *)
padding_value: Value used for padding in the padded tensor
mask_value: Boolean fill value for padding positions in generated masks.
- ``mask_value=False`` (default): valid positions are ``True`` and padding is ``False``.
- ``mask_value=True``: padding positions are ``True`` and valid positions are ``False``.
Examples:
Basic usage:
>>> nested_tensor = NestedTensor(torch.tensor([1, 2, 3]), torch.tensor([4, 5]))
>>> nested_tensor.shape
torch.Size([2, 3])
>>> nested_tensor.tensor # Padded representation
tensor([[1, 2, 3],
[4, 5, 0]])
>>> nested_tensor.mask # Mask showing real vs padding values
tensor([[ True, True, True],
[ True, True, False]])
>>> nested_tensor.concat # Concatenated version (no padding)
tensor([1, 2, 3, 4, 5])
Indexing:
>>> nested_tensor[0] # First tensor (no padding)
tensor([1, 2, 3])
>>> nested_tensor[:2] # Returns a NestedTensor slice
NestedTensor([
[1, 2, 3],
[4, 5]
])
>>> nested_tensor[:, 1:] # Slice operations return a new NestedTensor
NestedTensor([
[2, 3],
[5]
])
Type conversion:
>>> nested_tensor.to(torch.float).tensor
tensor([[1., 2., 3.],
[4., 5., 0.]])
>>> nested_tensor.half().tensor
tensor([[1., 2., 3.],
[4., 5., 0.]], dtype=torch.float16)
Conversion to Python types:
>>> nested_tensor.tolist()
[[1, 2, 3], [4, 5]]
Creating from Python lists:
>>> NestedTensor(*[[1, 2, 3], [4, 5]])
NestedTensor([
[1, 2, 3],
[4, 5]
])
"""
_compiled_packed_constructor: ClassVar[_PackedConstructor]
_packed_values: Tensor
_offsets: Tensor
_permutation: tuple[int, ...]
_physical_shape: Tensor
_flatten_sentinel: Tensor = torch.empty(0)
_compile_max_length_binding: Tensor
_logical_shape: torch.Size
_ragged_dims: tuple[int, ...]
_ragged_dims_explicit: bool
_batch_first: bool
_padding_value: float
_mask_value: bool
_pin_memory: bool
_packed_sizes: tuple[int, ...] | None
_element_shapes: tuple[tuple[int, ...], ...] | None
_cached_storage: tuple[Tensor, ...] | None
_cached_packed_projection: tuple[tuple[int, ...], Tensor] | None
_cached_hierarchical_offsets: tuple[Tensor, ...] | None
_cached_tensor_view: tuple[bool, float, tuple[int, ...], Tensor] | None
_cached_mask_view: tuple[bool, bool, tuple[int, ...], Tensor] | None
_cached_packed_batch_indices: dict[tuple[str, torch.dtype, tuple[int, ...]], Tensor] | None
_cached_packed_local_indices: dict[tuple[int, str, torch.dtype, tuple[int, ...]], Tensor] | None
_cached_packed_offsets: dict[tuple[str, torch.dtype, tuple[int, ...]], Tensor] | None
_cached_ragged_level_offsets: dict[tuple[int, str, torch.dtype, tuple[int, ...]], Tensor] | None
_unflattened_autograd: bool
_is_aot_tangent: bool
_RAGGED_OFFSETS_PREFIX = "_ragged_offsets_"
_AOT_CACHE_HASH_VERSION = 3
# Construction & Initialization
def __init_subclass__(cls, **kwargs) -> None:
super().__init_subclass__(**kwargs)
cls._compiled_packed_constructor = staticmethod(_make_nested_tensor_from_packed_constructor(cls))
@staticmethod
def __new__(
cls,
*tensors: Iterable[Tensor],
dtype: torch.dtype | None = None,
device: torch.device | None = None,
requires_grad: bool | None = None,
pin_memory: bool = False,
ragged_dims: tuple[int, ...] | None = None,
batch_first: bool = True,
padding_value: SupportsFloat = 0.0,
mask_value: bool = False,
_lenient_layout: bool = False,
):
r"""
Construct packed storage from individual tensors or an iterable of elements.
Args:
*tensors (Tensor | Iterable): Individual tensors or one iterable of elements.
Non-tensor elements are converted with ``torch.tensor``.
dtype: Optional common dtype. If omitted, input dtypes are promoted together.
device: Optional target device. If omitted, inputs must already share a device.
requires_grad: Whether converted inputs require gradients. ``None`` preserves
each tensor's existing gradient requirement.
pin_memory: Whether to pin CPU storage for host-to-device transfers.
ragged_dims: Ordered element dimensions that remain ragged even when their
sizes happen to be equal. ``None`` infers them from the input shapes.
batch_first: Whether the logical batch axis precedes the element dimensions.
padding_value: Fill value for padded tensor views.
mask_value: Fill value for padding positions in masks; valid positions use its inverse.
Returns:
(NestedTensor): The packed batch, preserving differentiable tensor inputs.
Raises:
ValueError: If the input is not iterable, element ranks differ, devices differ
without an explicit target device, or declared ragged dimensions are invalid.
"""
if len(tensors) == 1 and not isinstance(tensors[0], Tensor):
if isinstance(tensors[0], Iterable):
tensors = tuple(tensors[0]) # type: ignore
else:
raise ValueError(f"tensors must be an Iterable, but got {type(tensors[0])}.")
# Validate and convert tensors
validated = cls._coerce_tensors(
tensors, dtype=dtype, device=device, requires_grad=requires_grad, pin_memory=pin_memory
)
# Determine dtype/device from validated tensors or fallbacks
out_dtype = validated[0].dtype if validated else (dtype or torch.get_default_dtype())
out_device = validated[0].device if validated else (device or torch.device("cpu"))
if ragged_dims is not None and _lenient_layout:
# A rebuild from per-element results carries its source's declaration only where the
# results still fit it; otherwise the layout is inferred again from the results.
shapes = tuple(tuple(int(size) for size in tensor.shape) for tensor in validated)
if not shapes or any(len(shape) != len(shapes[0]) for shape in shapes):
ragged_dims = None
else:
try:
cls._pack_layout_from_declared_ragged_dims(shapes, ragged_dims)
except ValueError:
ragged_dims = None
# Pack into values, offsets, tensor-shape metadata, and Python metadata.
values, offsets, shape_tensor, packed_sizes, element_shapes = cls._pack(
validated,
dtype=out_dtype,
device=out_device,
ragged_dims=ragged_dims,
)
values = cls._maybe_pin_values(values, pin_memory)
if ragged_dims is None:
resolved_ragged_dims, static_dims = cls._pack_layout_from_element_shapes(element_shapes)
else:
resolved_ragged_dims, static_dims = cls._pack_layout_from_declared_ragged_dims(element_shapes, ragged_dims)
permutation = resolved_ragged_dims + static_dims
# Compute logical shape
logical_shape = cls._compute_logical_shape(validated, batch_first)
if requires_grad is not None and values.requires_grad != requires_grad:
values.requires_grad_(requires_grad)
out_requires_grad = values.requires_grad
result = torch.Tensor._make_wrapper_subclass(
cls,
logical_shape,
dtype=out_dtype,
device=out_device,
requires_grad=out_requires_grad,
)
result._packed_values = values
result._offsets = offsets
result._permutation = permutation
result._ragged_dims = resolved_ragged_dims
# A layout inferred from this batch is declared from here on, exactly as if the caller
# had passed ``ragged_dims``: later batches with coincidentally equal extents keep the
# same schema, and the wrapper stays tensor-backed for compilation.
declared = ragged_dims is not None or bool(resolved_ragged_dims)
result._ragged_dims_explicit = declared
result._physical_shape = shape_tensor
result._logical_shape = logical_shape
result._set_runtime_config(
batch_first=batch_first,
padding_value=padding_value,
mask_value=mask_value,
)
result._pin_memory = bool(pin_memory and values.device.type == "cpu" and values.is_pinned())
ragged_offsets = cls._resolve_persistent_ragged_offsets(
offsets,
shape_tensor,
permutation=permutation,
ragged_dims=resolved_ragged_dims if declared else None,
element_shapes=element_shapes,
)
# A tensor-backed layout is completely described by its metadata tensors; the
# per-element Python shape caches stay off it in eager exactly as under compile.
# They still validate this construction from user tensors below.
# Standalone FakeTensor execution keeps them: fake values leave no other source of sizes.
keep_python_metadata = ragged_offsets is None or _is_fake_tensor(values)
result._packed_sizes = packed_sizes if keep_python_metadata else None
result._element_shapes = element_shapes if keep_python_metadata else None
cls._install_persistent_ragged_offsets(result, ragged_offsets)
result._invalidate_transient_caches()
result._mark_tensor_backed_dynamic_dims()
cls._validate_packed_metadata(
result.concat,
result._offsets,
result._physical_shape,
permutation=result._permutation,
ragged_dims=result._ragged_dims,
logical_shape=result._logical_shape,
batch_first=result.batch_first,
packed_sizes=packed_sizes,
element_shapes=element_shapes,
ragged_offsets=ragged_offsets,
)
if torch.is_grad_enabled() and values.requires_grad:
return _PackedLikeAutograd.apply(values, _PackedStructureReference(result, mark_dynamic=True))
return result
def __init__(self, *args, **kwargs):
pass # All init in __new__
# ------------------------------------------------------------------
# Packed representation helpers
# ------------------------------------------------------------------
@staticmethod
def _coerce_tensors(
tensors: tuple,
*,
dtype: torch.dtype | None = None,
device: torch.device | None = None,
requires_grad: bool | None = None,
pin_memory: bool = False,
) -> tuple[Tensor, ...]:
if not isinstance(tensors, Iterable):
raise ValueError(f"tensors must be an Iterable, but got {type(tensors)}.")
if isinstance(tensors, Tensor) and hasattr(tensors, "unbind"):
tensors = tensors.unbind()
result: list[Tensor] = []
common_device: torch.device | None = None
common_ndim: int | None = None
# Only track dtype promotion when the caller did not specify an explicit dtype.
# When dtype is given, t.to(device, dtype=dtype) already handles casting in
# the first pass, so the promotion loop and second pass are both unnecessary.
needs_promotion = dtype is None
common_dtype: torch.dtype | None = None
for t in tensors:
if not isinstance(t, Tensor):
t = torch.tensor(t, dtype=dtype, device=device, pin_memory=pin_memory)
else:
t = t.to(device, dtype=dtype)
if requires_grad is not None:
t.requires_grad_(requires_grad)
if common_device is None:
common_device = t.device
elif t.device != common_device:
raise ValueError(
f"All tensors in NestedTensor must be on the same device, but got {common_device} and {t.device}"
)
if needs_promotion:
if common_dtype is None:
common_dtype = t.dtype
else:
common_dtype = torch.promote_types(common_dtype, t.dtype)
if common_ndim is None:
common_ndim = t.ndim
elif t.ndim != common_ndim:
raise ValueError(
f"All tensors must have the same number of dimensions, got ndim {common_ndim} and {t.ndim}. "
"If using a DataLoader with drop_last=False, squeeze the last batch before constructing "
"NestedTensor."
)
result.append(t)
if not result:
return ()
# Second pass only when dtype=None AND promotion actually changed the dtype.
if needs_promotion and common_dtype is not None and any(t.dtype != common_dtype for t in result):
return tuple(t.to(dtype=common_dtype) for t in result)
return tuple(result)
@staticmethod
def _pack(
tensors: tuple[Tensor, ...],
*,
dtype: torch.dtype | None = None,
device: torch.device | None = None,
permutation: tuple[int, ...] | None = None,
ragged_dims: tuple[int, ...] | None = None,
) -> tuple[Tensor, Tensor, Tensor, tuple[int, ...], tuple[tuple[int, ...], ...]]:
r"""Pack a sequence of tensors into values, offsets, tensor metadata, and Python metadata."""
if not tensors:
return (
torch.empty(0, dtype=dtype or torch.get_default_dtype(), device=device),
torch.zeros(1, dtype=torch.long),
torch.empty(0, 0, dtype=torch.long),
(),
(),
)
max_ndim = max(t.ndim for t in tensors)
element_shapes = tuple(tuple(int(dim) for dim in t.shape) for t in tensors)
declared_layout = None
if ragged_dims is not None:
declared_layout = NestedTensor._pack_layout_from_declared_ragged_dims(element_shapes, ragged_dims)
# Offsets and shape_tensor are metadata - always on CPU to avoid CUDA syncs.
shape_tensor = torch.tensor([list(t.shape) + [0] * (max_ndim - t.ndim) for t in tensors], dtype=torch.long)
if max_ndim == 0:
values = torch.stack(tensors)
sizes = torch.ones(len(tensors), dtype=torch.long)
packed_sizes = tuple(1 for _ in tensors)
else:
if permutation is None:
if declared_layout is None:
varying_dims, static_dims = NestedTensor._pack_layout_from_element_shapes(element_shapes)
else:
varying_dims, static_dims = declared_layout
permutation = varying_dims + static_dims
else:
permutation = tuple(int(dim) for dim in permutation)
if len(permutation) != max_ndim or tuple(sorted(permutation)) != tuple(range(max_ndim)):
raise ValueError(f"Invalid permutation dims {permutation} for tensors with rank {max_ndim}")
if declared_layout is None:
ragged_rank = len(NestedTensor._hierarchical_level_sizes_from_element_shapes(element_shapes))
varying_dims = permutation[:ragged_rank]
static_dims = permutation[ragged_rank:]
else:
varying_dims, _ = declared_layout
if permutation[: len(varying_dims)] != varying_dims:
raise ValueError(
"permutation must begin with ragged_dims in the declared order, "
f"got permutation={permutation} and ragged_dims={varying_dims}"
)
static_dims = permutation[len(varying_dims) :]
packed = []
packed_sizes_list = []
identity_permutation = tuple(range(max_ndim))
for tensor, shape in zip(tensors, element_shapes):
packed_size = NestedTensor._packed_size_from_shape(shape, varying_dims)
packed_sizes_list.append(packed_size)
packed_tensor = tensor if permutation == identity_permutation else tensor.permute(permutation)
suffix_shape = tuple(shape[dim] for dim in static_dims)
packed.append(packed_tensor.reshape((packed_size, *suffix_shape) if suffix_shape else (packed_size,)))
values = torch.cat(packed, dim=0)
sizes = torch.tensor(packed_sizes_list, dtype=torch.long)
packed_sizes = tuple(packed_sizes_list)
offsets = torch.zeros(len(tensors) + 1, dtype=torch.long)
torch.cumsum(sizes, dim=0, out=offsets[1:])
return values, offsets, shape_tensor, packed_sizes, element_shapes
@staticmethod
def _normalize_ragged_dims(ragged_dims: tuple[int, ...], physical_rank: int) -> tuple[int, ...]:
if not isinstance(ragged_dims, tuple):
raise TypeError(f"ragged_dims must be a tuple of ints or None, got {type(ragged_dims).__name__}")
normalized: list[int] = []
for dim in ragged_dims:
if isinstance(dim, torch.SymInt):
# AOTAutograd can round-trip literal layout dimensions through
# wrapper-subclass metadata as constant SymInts. Layout axes are
# structural integers, never data-dependent sizes, so normalize
# those constants before applying the public type/range contract.
dim = int(dim)
if not isinstance(dim, int) or isinstance(dim, bool):
raise TypeError(f"ragged_dims must contain only ints, got {type(dim).__name__}")
normalized_dim = dim + physical_rank if dim < 0 else dim
if normalized_dim < 0 or normalized_dim >= physical_rank:
raise ValueError(f"ragged_dims contains dim {dim} outside element rank {physical_rank}")
if normalized_dim in normalized:
raise ValueError(f"ragged_dims must not contain duplicate dimensions, got {ragged_dims}")
normalized.append(normalized_dim)
return tuple(normalized)
@staticmethod
def _is_tensor_backed_layout(
permutation: tuple[int, ...] | None,
ragged_dims: tuple[int, ...] | None,
) -> bool:
r"""Return whether tensor metadata fully encodes an explicit packed-prefix layout."""
if not ragged_dims or permutation is None:
return False
ragged_rank = len(ragged_dims)
return tuple(permutation[:ragged_rank]) == tuple(ragged_dims)
@classmethod
def _ragged_offset_names(cls, ragged_rank: int) -> tuple[str, ...]:
r"""Return stable wrapper-child names for persistent multi-level row splits."""
if ragged_rank <= 1:
return ()
return tuple(f"{cls._RAGGED_OFFSETS_PREFIX}{level}" for level in range(ragged_rank))
@classmethod
def _build_explicit_ragged_offsets(
cls,
shape_tensor: Tensor,
ragged_dims: tuple[int, ...],
*,
dtype: torch.dtype,
) -> tuple[Tensor, ...]:
r"""Build CSR row splits for an explicit packed-prefix ragged hierarchy."""
if _is_fake_tensor(shape_tensor):
raise RuntimeError("Cannot derive explicit multi-ragged offsets from data-less FakeTensor shape metadata.")
batch_size = int(shape_tensor.size(0))
parent_counts = torch.ones(batch_size, dtype=torch.long, device=shape_tensor.device)
offsets: list[Tensor] = []
for dim in ragged_dims:
widths = torch.repeat_interleave(shape_tensor[:, dim].to(torch.long), parent_counts)
offsets.append(cls._offsets_from_sizes(widths, dtype=dtype).contiguous())
parent_counts = parent_counts * shape_tensor[:, dim].to(torch.long)
return tuple(offsets)
@classmethod
def _resolve_persistent_ragged_offsets(
cls,
offsets: Tensor,
shape_tensor: Tensor,
*,
permutation: tuple[int, ...] | None,
ragged_dims: tuple[int, ...] | None,
ragged_offsets: tuple[Tensor, ...] | None = None,
element_shapes: tuple[tuple[int, ...], ...] | None = None,
) -> tuple[Tensor, ...] | None:
r"""Resolve persistent row splits for an explicit packed-prefix ragged layout."""
if not cls._is_tensor_backed_layout(permutation, ragged_dims):
if ragged_offsets is not None:
raise ValueError("ragged_offsets require an explicit layout whose packed order begins with ragged_dims")
return None
assert ragged_dims is not None
ragged_rank = len(ragged_dims)
if ragged_rank == 1:
if ragged_offsets is not None:
if len(ragged_offsets) != 1:
raise ValueError(f"Expected one ragged offset tensor, got {len(ragged_offsets)}")
supplied = ragged_offsets[0]
if (
supplied is not offsets
and not (_is_fake_tensor(supplied) or _is_fake_tensor(offsets))
and not torch.equal(supplied, offsets)
):
raise ValueError("The supplied single-level ragged offsets must match offsets")
return (offsets,)
if ragged_offsets is not None:
if len(ragged_offsets) != ragged_rank:
raise ValueError(f"Expected {ragged_rank} ragged offset tensors, got {len(ragged_offsets)}")
return tuple(ragged_offsets)
if _is_fake_tensor(shape_tensor):
if element_shapes is None:
_compile_unsupported(
"NestedTensor._from_packed",
"explicit multi-ragged FakeTensor rebuilds require concrete element shapes or persistent offsets",
)
assert element_shapes is not None
level_sizes = cls._hierarchical_level_sizes_from_element_shapes(element_shapes, ragged_dims)
return tuple(offsets.new_empty((len(sizes) + 1,), dtype=offsets.dtype) for sizes in level_sizes)
if _is_compiling():
_compile_unsupported(
"NestedTensor._from_packed",
"explicit multi-ragged rebuilds require persistent ragged offset tensors",
)
return cls._build_explicit_ragged_offsets(shape_tensor, ragged_dims, dtype=offsets.dtype)
@classmethod
def _install_persistent_ragged_offsets(
cls,
result: Self,
ragged_offsets: tuple[Tensor, ...] | None,
) -> None:
r"""Install persistent multi-level row splits as traceable wrapper children."""
instance_attrs = vars(result)
if f"{cls._RAGGED_OFFSETS_PREFIX}0" in instance_attrs:
for name in tuple(instance_attrs):
if name.startswith(cls._RAGGED_OFFSETS_PREFIX):
delattr(result, name)
if ragged_offsets is None or len(ragged_offsets) <= 1:
return
for name, level_offsets in zip(cls._ragged_offset_names(len(ragged_offsets)), ragged_offsets):
setattr(result, name, level_offsets)
def _persistent_ragged_offsets(self) -> tuple[Tensor, ...] | None:
r"""Return tensor-backed row splits when this instance owns a complete topology."""
instance_attrs = vars(self)
if (
"_offsets" not in instance_attrs
or "_permutation" not in instance_attrs
or "_ragged_dims" not in instance_attrs
or "_ragged_dims_explicit" not in instance_attrs
):
return None
declared_ragged_dims = self._ragged_dims if self._ragged_dims_explicit else None
if not type(self)._is_tensor_backed_layout(self._permutation, declared_ragged_dims):
return None
if self._ragged_rank == 1:
# Every explicit single-ragged layout is already packed with that ragged
# dimension first. Its row splits and physical-shape tensor completely
# describe the topology, including the ordinary leading-ragged case. Do not
# retain legacy per-element Python shape caches in the compile contract.
return (self._offsets,)
names = type(self)._ragged_offset_names(self._ragged_rank)
if any(name not in instance_attrs for name in names):
return None
return tuple(instance_attrs[name] for name in names)
@classmethod
def _pack_layout_from_declared_ragged_dims(
cls,
element_shapes: tuple[tuple[int, ...], ...],
ragged_dims: tuple[int, ...],
) -> tuple[tuple[int, ...], tuple[int, ...]]:
physical_rank = len(element_shapes[0]) if element_shapes else 0
varying_dims = cls._normalize_ragged_dims(ragged_dims, physical_rank)
static_dims = tuple(dim for dim in range(physical_rank) if dim not in varying_dims)
if element_shapes:
reference = element_shapes[0]
for dim in static_dims:
if any(len(shape) != physical_rank or shape[dim] != reference[dim] for shape in element_shapes[1:]):
raise ValueError(
"Dimensions not listed in ragged_dims must have identical sizes across elements, "
f"but dim {dim} varies for shapes {element_shapes}"
)
return varying_dims, static_dims
def _project_declared_ragged_dims(
self,
*,
prefix: Sequence[int] = (),
keep_dims: Sequence[int] | None = None,
) -> tuple[int, ...] | None:
r"""Remap declared ragged dimensions through a basic physical-dimension projection."""
if not self._ragged_dims_explicit:
return None
if keep_dims is None:
keep_dims = tuple(range(self._physical_shape.size(1)))
old_to_new = {int(dim): len(prefix) + index for index, dim in enumerate(keep_dims)}
return tuple(old_to_new[dim] for dim in self._ragged_dims if dim in old_to_new)
def _project_permutation(
self,
*,
prefix: Sequence[int] = (),
keep_dims: Sequence[int] | None = None,
suffix: Sequence[int] = (),
) -> tuple[int, ...]:
r"""Remap packed dimension order through a projection with dense prefix/suffix dimensions."""
if keep_dims is None:
keep_dims = tuple(range(self._physical_shape.size(1)))
keep_dims = tuple(int(dim) for dim in keep_dims)
prefix_rank = len(prefix)
old_to_new = {dim: prefix_rank + index for index, dim in enumerate(keep_dims)}
retained = tuple(old_to_new[dim] for dim in self._permutation if dim in old_to_new)
prefix_dims = tuple(range(prefix_rank))
suffix_start = prefix_rank + len(keep_dims)
suffix_dims = tuple(range(suffix_start, suffix_start + len(suffix)))
retained_set = set(retained)
added_static = tuple(dim for dim in (*prefix_dims, *suffix_dims) if dim not in retained_set)
return retained + added_static
@classmethod
def _ragged_dims_from_packed_layout(
cls,
values: Tensor,
physical_rank: int,
permutation: tuple[int, ...],
ragged_dims: tuple[int, ...] | None,
) -> tuple[int, ...]:
if ragged_dims is not None:
resolved = cls._normalize_ragged_dims(ragged_dims, physical_rank)
if permutation[: len(resolved)] != resolved:
raise ValueError(
"permutation must begin with ragged_dims in the declared order, "
f"got permutation={permutation} and ragged_dims={resolved}"
)
return resolved
ragged_rank = physical_rank - max(values.dim() - 1, 0)
if ragged_rank < 0 or ragged_rank > physical_rank:
raise ValueError(
"Packed values rank is inconsistent with the element rank, "
f"got values rank {values.dim()} and element rank {physical_rank}"
)
return permutation[:ragged_rank]
@staticmethod
def _maybe_pin_values(values: Tensor, pin_memory: bool) -> Tensor:
r"""Pin packed storage when requested and the values live on CPU."""
if pin_memory and values.device.type == "cpu" and not values.is_pinned():
return values.pin_memory()
return values
@staticmethod
def _trim_shape(shape: Sequence[int]) -> tuple[int, ...]:
end = len(shape)
while end > 0 and shape[end - 1] == 0:
end -= 1
return tuple(int(shape[i]) for i in range(end))
@staticmethod
def _shape_numel(shape: tuple[int, ...]) -> int:
size = 1
for dim in shape:
size *= int(dim)
return size
@classmethod
def _permutation_from_element_shapes(cls, element_shapes: tuple[tuple[int, ...], ...]) -> tuple[int, ...]:
varying_dims, static_dims = cls._pack_layout_from_element_shapes(element_shapes)
return varying_dims + static_dims
@classmethod
def _permutation_from_physical_shape(
cls,
physical_shape: Tensor,
element_shapes: tuple[tuple[int, ...], ...] | None,
) -> tuple[int, ...]:
varying_dims, static_dims = cls._pack_layout_meta(physical_shape, element_shapes)
return varying_dims + static_dims
@staticmethod
def _offsets_from_sizes(sizes: Tensor | Sequence[int], *, dtype: torch.dtype = torch.long) -> Tensor:
sizes_tensor = sizes if isinstance(sizes, Tensor) else torch.tensor(sizes, dtype=dtype)
if sizes_tensor.dtype != dtype:
sizes_tensor = sizes_tensor.to(dtype)
if sizes_tensor.numel() == 0:
return torch.zeros((1,), dtype=dtype, device=sizes_tensor.device)
return torch.cat([sizes_tensor.new_zeros((1,)), torch.cumsum(sizes_tensor, dim=0)])
@staticmethod
def _meta_tensor_equal(
lhs: Tensor,
rhs: Tensor,
message: str = "NestedTensor metadata must match",
*,
runtime_assert: bool = False,
) -> bool:
if lhs is rhs:
return True
if lhs.dim() != rhs.dim():
return False
if _is_compiling() or _is_fake_tensor(lhs) or _is_fake_tensor(rhs):
from torch.fx.experimental.symbolic_shapes import statically_known_true
if any(statically_known_true(lhs_size != rhs_size) for lhs_size, rhs_size in zip(lhs.shape, rhs.shape)):
return False
if (
not runtime_assert
and not _is_compiling()
and all(statically_known_true(lhs_size == rhs_size) for lhs_size, rhs_size in zip(lhs.shape, rhs.shape))
):
# Standalone FakeTensor execution has nowhere to retain a runtime
# value assertion. Preserve the conservative eager-Fake contract
# when the shapes are already known; an unbacked shape still takes
# the opaque assertion path used by compiled reconstruction.
return False
torch._assert_async(_metadata_tensors_equal(lhs, rhs), message)
return True
if lhs.shape != rhs.shape:
return False
if runtime_assert:
torch._assert_async(torch.all(lhs == rhs), message)
return True
return bool(torch.equal(lhs, rhs))
@classmethod
def _hierarchical_level_sizes_from_element_shapes(
cls,
element_shapes: tuple[tuple[int, ...], ...],
ragged_dims: tuple[int, ...] | None = None,
) -> tuple[tuple[int, ...], ...]:
if not element_shapes:
return ()
if ragged_dims is None:
varying_dims, _ = cls._pack_layout_from_element_shapes(element_shapes)
else:
varying_dims, _ = cls._pack_layout_from_declared_ragged_dims(element_shapes, ragged_dims)
if not varying_dims:
return ()
level_sizes: list[tuple[int, ...]] = []
prefix_products = [1] * len(element_shapes)
for dim in varying_dims:
sizes: list[int] = []
next_prefix_products: list[int] = []
for shape, prefix in zip(element_shapes, prefix_products):
dim_size = int(shape[dim])
sizes.extend([dim_size] * prefix)
next_prefix_products.append(prefix * dim_size)
level_sizes.append(tuple(sizes))
prefix_products = next_prefix_products
return tuple(level_sizes)
@classmethod
def _hierarchical_level_sizes_from_physical_shape(
cls,
physical_shape: Tensor,
element_shapes: tuple[tuple[int, ...], ...] | None = None,
ragged_dims: tuple[int, ...] | None = None,
) -> tuple[tuple[int, ...], ...]:
if physical_shape.numel() == 0:
return ()
if element_shapes is not None:
return cls._hierarchical_level_sizes_from_element_shapes(element_shapes, ragged_dims)
if _is_fake_tensor(physical_shape):
return ()
if ragged_dims is None:
varying_dims, _ = cls._pack_layout_meta(physical_shape, None)
else:
varying_dims = cls._normalize_ragged_dims(ragged_dims, int(physical_shape.size(1)))
if not varying_dims:
return ()
shape_rows = tuple(cls._trim_shape(row) for row in physical_shape.tolist())
level_sizes: list[tuple[int, ...]] = []
prefix_products = [1] * len(shape_rows)
for dim in varying_dims:
sizes: list[int] = []
next_prefix_products: list[int] = []
for shape, prefix in zip(shape_rows, prefix_products):
dim_size = int(shape[dim]) if dim < len(shape) else 0
sizes.extend([dim_size] * prefix)
next_prefix_products.append(prefix * dim_size)
level_sizes.append(tuple(sizes))
prefix_products = next_prefix_products
return tuple(level_sizes)
@staticmethod
def _inverse_permutation(permutation: tuple[int, ...]) -> tuple[int, ...]:
inverse = [0] * len(permutation)
for index, dim in enumerate(permutation):
inverse[dim] = index
return tuple(inverse)
@classmethod
def _pack_layout_from_element_shapes(
cls,
element_shapes: tuple[tuple[int, ...], ...],
) -> tuple[tuple[int, ...], tuple[int, ...]]:
if not element_shapes:
return (), ()
ndim = len(element_shapes[0])
if ndim == 0:
return (), ()
reference = element_shapes[0]
static_dims = [
dim
for dim in range(ndim)
if all(len(shape) == ndim and shape[dim] == reference[dim] for shape in element_shapes[1:])
]
if len(static_dims) == ndim:
static_dims = list(range(1, ndim))
static_dims_tuple = tuple(static_dims)
varying_dims = tuple(dim for dim in range(ndim) if dim not in static_dims_tuple)
return varying_dims, static_dims_tuple
@classmethod
def _pack_layout_meta(
cls,
physical_shape: Tensor,
element_shapes: tuple[tuple[int, ...], ...] | None,
) -> tuple[tuple[int, ...], tuple[int, ...]]:
if element_shapes is not None and (element_shapes or int(physical_shape.size(1)) == 0):
return cls._pack_layout_from_element_shapes(element_shapes)
ndim = int(physical_shape.size(1))
if ndim == 0:
return (), ()
if physical_shape.size(0) == 0:
return (0,), tuple(range(1, ndim))
static_dims = tuple(
dim
for dim in range(ndim)
if bool(torch.equal(physical_shape[:, dim], physical_shape[:1, dim].expand(physical_shape.size(0))))
)
if len(static_dims) == ndim:
static_dims = tuple(range(1, ndim))
varying_dims = tuple(dim for dim in range(ndim) if dim not in static_dims)
return varying_dims, static_dims
@staticmethod
def _packed_size_from_shape(shape: tuple[int, ...], varying_dims: tuple[int, ...]) -> int:
if not shape or not varying_dims:
return 1
size = 1
for dim in varying_dims:
size *= int(shape[dim])
return size
@classmethod
def _python_meta_from_packed(
cls,
values: Tensor,
offsets: Tensor,
shape_tensor: Tensor,
*,
packed_sizes: tuple[int, ...] | None = None,
element_shapes: tuple[tuple[int, ...], ...] | None = None,
) -> tuple[tuple[int, ...], tuple[tuple[int, ...], ...]]:
if packed_sizes is None:
packed_sizes = tuple(int(size) for size in (offsets[1:] - offsets[:-1]).tolist())
if element_shapes is None:
element_shapes = tuple(cls._trim_shape(shape) for shape in shape_tensor.tolist())
return packed_sizes, element_shapes
@classmethod
@torch._dynamo.disable
def _infer_python_meta_from_packed(
cls,
values: Tensor,
offsets: Tensor,
shape_tensor: Tensor,
*,
packed_sizes: tuple[int, ...] | None = None,
element_shapes: tuple[tuple[int, ...], ...] | None = None,
) -> tuple[tuple[int, ...], tuple[tuple[int, ...], ...]]:
return cls._python_meta_from_packed(
values,
offsets,
shape_tensor,
packed_sizes=packed_sizes,
element_shapes=element_shapes,
)
@staticmethod
def _compute_logical_shape(tensors: tuple[Tensor, ...], batch_first: bool) -> torch.Size:
r"""Compute the logical shape [B, max_d0, max_d1, ...] from individual tensors."""
if not tensors:
return torch.Size((0,))
if max(t.dim() for t in tensors) == 0:
return torch.Size((len(tensors),))
ndim = max(t.dim() for t in tensors)
size = [max(t.shape[i] if i < len(t.shape) else 0 for t in tensors) for i in range(ndim)]
size.insert(0 if batch_first else 1, len(tensors))
return torch.Size(size)
@staticmethod
def _logical_shape_from_physical_shape(physical_shape: Tensor, offsets: Tensor, batch_first: bool) -> torch.Size:
r"""Compute logical shape from packed metadata without unpacking elements."""
batch_size = len(offsets) - 1
if batch_size == 0:
return torch.Size((0,))
if physical_shape.numel() == 0:
return torch.Size((batch_size,))
size = [int(physical_shape[:, d].max().item()) for d in range(physical_shape.size(1))]
while size and size[-1] == 0:
size.pop()
size.insert(0 if batch_first else 1, batch_size)
return torch.Size(size)
@staticmethod
def _batch_dim_from_logical_shape(logical_shape: torch.Size, batch_first: bool) -> int:
r"""Return the batch dimension index for a logical NestedTensor shape."""
return 0 if len(logical_shape) <= 1 or batch_first else 1
@classmethod
def _validate_packed_metadata(
cls,
values: Tensor,
offsets: Tensor,
shape_tensor: Tensor,
*,
permutation: tuple[int, ...],
ragged_dims: tuple[int, ...],
logical_shape: torch.Size,
batch_first: bool,
packed_sizes: tuple[int, ...] | None,
element_shapes: tuple[tuple[int, ...], ...] | None,
ragged_offsets: tuple[Tensor, ...] | None,
) -> None:
r"""Validate that packed storage and metadata describe a coherent NestedTensor layout."""
if offsets.device.type != "cpu":
raise ValueError(f"offsets must be on CPU, got {offsets.device}")
if shape_tensor.device.type != "cpu":
raise ValueError(f"shape_tensor must be on CPU, got {shape_tensor.device}")
if offsets.dim() != 1:
raise ValueError(f"offsets must be 1-D, got shape {tuple(offsets.shape)}")
if shape_tensor.dim() != 2:
raise ValueError(f"shape_tensor must be 2-D, got shape {tuple(shape_tensor.shape)}")
if offsets.dtype.is_floating_point or offsets.dtype.is_complex or offsets.dtype == torch.bool:
raise ValueError(f"offsets must use an integer dtype, got {offsets.dtype}")
if shape_tensor.dtype.is_floating_point or shape_tensor.dtype.is_complex or shape_tensor.dtype == torch.bool:
raise ValueError(f"shape_tensor must use an integer dtype, got {shape_tensor.dtype}")
batch_size = int(shape_tensor.size(0))
if offsets.numel() != batch_size + 1:
raise ValueError(
"offsets length must equal batch size + 1, got "
f"offsets.numel()={offsets.numel()}, batch_size={batch_size}"
)
physical_rank = int(shape_tensor.size(1))
if len(logical_shape) != physical_rank + 1:
raise ValueError(
"logical shape rank must equal physical rank + 1, got "
f"logical rank={len(logical_shape)}, physical rank={physical_rank}"
)
batch_dim = cls._batch_dim_from_logical_shape(logical_shape, batch_first)
logical_batch = logical_shape[batch_dim]
if logical_batch != batch_size:
raise ValueError(f"logical batch size {logical_batch} does not match metadata batch size {batch_size}")
if len(permutation) != physical_rank or tuple(sorted(int(dim) for dim in permutation)) != tuple(
range(physical_rank)
):
raise ValueError(f"Invalid permutation dims {permutation} for shape with {physical_rank} dims")
normalized_ragged_dims = cls._normalize_ragged_dims(ragged_dims, physical_rank)
if permutation[: len(normalized_ragged_dims)] != normalized_ragged_dims:
raise ValueError(
"permutation must begin with ragged_dims in the declared order, "
f"got permutation={permutation} and ragged_dims={normalized_ragged_dims}"
)
static_dims = tuple(int(dim) for dim in permutation[len(normalized_ragged_dims) :])
expected_values_rank = 1 + len(static_dims)
if values.dim() != expected_values_rank:
raise ValueError(
"Packed values rank is inconsistent with ragged_dims, "
f"got values rank {values.dim()}, expected {expected_values_rank} for "
f"physical rank {physical_rank} and ragged_dims={normalized_ragged_dims}"
)
tensor_backed_layout = cls._is_tensor_backed_layout(permutation, normalized_ragged_dims)
if ragged_offsets is not None:
if not tensor_backed_layout:
raise ValueError("ragged_offsets require an explicit layout whose packed order begins with ragged_dims")
if len(ragged_offsets) != len(normalized_ragged_dims):
raise ValueError(
f"Expected {len(normalized_ragged_dims)} ragged offset tensors, got {len(ragged_offsets)}"
)
for level, level_offsets in enumerate(ragged_offsets):
if level_offsets.device.type != "cpu":
raise ValueError(f"ragged_offsets[{level}] must be on CPU, got {level_offsets.device}")
if level_offsets.dim() != 1:
raise ValueError(
f"ragged_offsets[{level}] must be one-dimensional, got shape {tuple(level_offsets.shape)}"
)
if (
level_offsets.dtype.is_floating_point
or level_offsets.dtype.is_complex
or level_offsets.dtype == torch.bool
):
raise ValueError(f"ragged_offsets[{level}] must use an integer dtype, got {level_offsets.dtype}")
if packed_sizes is not None:
if len(packed_sizes) != batch_size:
raise ValueError(
f"packed_sizes must have one entry per element, got {len(packed_sizes)} for batch size {batch_size}"
)
if any(int(size) < 0 for size in packed_sizes):
raise ValueError("packed_sizes must be non-negative")
if sum(int(size) for size in packed_sizes) != int(values.shape[0]):
raise ValueError("packed_sizes must sum to the packed values length")
if element_shapes is not None:
if len(element_shapes) != batch_size:
raise ValueError(
"element_shapes must have one entry per element, got "
f"{len(element_shapes)} for batch size {batch_size}"
)
normalized_shapes = tuple(tuple(int(dim) for dim in shape) for shape in element_shapes)
if any(len(shape) != physical_rank for shape in normalized_shapes):
raise ValueError(
f"element_shapes rank must match physical rank {physical_rank}, got {normalized_shapes}"
)
if any(any(dim < 0 for dim in shape) for shape in normalized_shapes):
raise ValueError("element_shapes must be non-negative")
if not _is_fake_tensor(shape_tensor):
shape_rows = tuple(tuple(int(size) for size in row) for row in shape_tensor.tolist())
if normalized_shapes != shape_rows:
raise ValueError("element_shapes must match shape_tensor exactly")
# The packed leading dimension is the product of declared ragged
# dimensions. Every remaining dimension is represented directly
# in the packed value tail and therefore must be uniform.
if normalized_shapes:
cls._pack_layout_from_declared_ragged_dims(normalized_shapes, normalized_ragged_dims)
expected_packed_sizes = tuple(
cls._packed_size_from_shape(shape, normalized_ragged_dims) for shape in normalized_shapes
)
if packed_sizes is not None and tuple(int(size) for size in packed_sizes) != expected_packed_sizes:
raise ValueError(
"packed_sizes must equal the product of ragged dimensions for every element, "
f"got {packed_sizes} and expected {expected_packed_sizes}"
)
if normalized_shapes:
expected_tail = tuple(normalized_shapes[0][dim] for dim in static_dims)
if tuple(values.shape[1:]) != expected_tail:
raise ValueError(
"Packed values tail must match static dimensions in permutation order, "
f"got {tuple(values.shape[1:])} and expected {expected_tail}"
)
if _is_fake_tensor(offsets) or _is_fake_tensor(shape_tensor):
return
if bool((shape_tensor < 0).any()):
raise ValueError("shape_tensor must be non-negative")
if int(offsets[0].item()) != 0:
raise ValueError("offsets must start at 0")
deltas = offsets[1:] - offsets[:-1]
if bool((deltas < 0).any()):
raise ValueError("offsets must be monotonically non-decreasing")
if packed_sizes is not None:
delta_sizes = tuple(int(size) for size in deltas.tolist())
normalized_packed_sizes = tuple(int(size) for size in packed_sizes)
if delta_sizes != normalized_packed_sizes:
raise ValueError(
"offset deltas must match packed_sizes exactly, " f"got {delta_sizes} and {normalized_packed_sizes}"
)
if packed_sizes is None and int(offsets[-1].item()) != int(values.shape[0]):
raise ValueError(
f"offsets[-1] must equal packed values length, got offsets[-1]={int(offsets[-1].item())} "
f"and values.shape[0]={int(values.shape[0])}"
)
if ragged_offsets is not None:
expected_ragged_offsets = cls._build_explicit_ragged_offsets(
shape_tensor,
normalized_ragged_dims,
dtype=offsets.dtype,
)
for level, (actual, expected) in enumerate(zip(ragged_offsets, expected_ragged_offsets)):
if not torch.equal(actual, expected):
raise ValueError(f"ragged_offsets[{level}] does not match physical_shape")
sample_leaf_offsets = ragged_offsets[0]
for level_offsets in ragged_offsets[1:]:
sample_leaf_offsets = level_offsets.index_select(0, sample_leaf_offsets.to(torch.long))
if not torch.equal(sample_leaf_offsets.to(offsets.dtype), offsets):
raise ValueError("ragged_offsets do not reproduce the packed sample offsets")
if int(ragged_offsets[-1][-1].item()) != int(values.shape[0]):
raise ValueError("Final ragged offsets must cover the packed values leading dimension")
def _validate_metadata(self) -> None:
r"""Validate the current packed storage and metadata."""
type(self)._validate_packed_metadata(
self.concat,
self._offsets,
self._physical_shape,
permutation=self._permutation,
ragged_dims=self._ragged_dims,
logical_shape=self._logical_shape,
batch_first=self.batch_first,
packed_sizes=self._packed_sizes,
element_shapes=self._element_shapes,
ragged_offsets=self._persistent_ragged_offsets(),
)
@staticmethod
def _coerce_batch_first(value: bool) -> bool:
if not isinstance(value, bool):
raise TypeError(f"batch_first must be a bool, got {type(value).__name__}")
return value
@staticmethod
def _coerce_mask_value(value: bool) -> bool:
if not isinstance(value, bool):
raise TypeError(f"mask_value must be a bool, got {type(value).__name__}")
return value
@staticmethod
def _coerce_padding_value(value: SupportsFloat) -> float:
try:
return float(value)
except (TypeError, ValueError) as exc:
raise TypeError(f"padding_value must be float-convertible, got {type(value).__name__}") from exc
def _set_runtime_config(
self,
*,
batch_first: bool,
padding_value: SupportsFloat,
mask_value: bool,
) -> None:
self._batch_first = type(self)._coerce_batch_first(batch_first)
self._padding_value = type(self)._coerce_padding_value(padding_value)
self._mask_value = type(self)._coerce_mask_value(mask_value)
def _invalidate_transient_caches(self) -> None:
r"""Drop all lazily materialized views derived from packed storage."""
self._cached_storage = None
self._cached_packed_projection = None
self._cached_hierarchical_offsets = None
self._cached_tensor_view = None
self._cached_mask_view = None
self._cached_packed_batch_indices = None
self._cached_packed_local_indices = None
self._cached_packed_offsets = None
self._cached_ragged_level_offsets = None
self._unflattened_autograd = False
def _same_row_splits(self, other: NestedTensor) -> bool:
r"""Return whether both wrappers address rows through the very same split tensors."""
if self._offsets is not other._offsets:
return False
mine = self._persistent_ragged_offsets()
theirs = other._persistent_ragged_offsets()
if mine is None or theirs is None:
# Inferred hierarchies are derived from the shape tensor as well.
return mine is theirs and self._physical_shape is other._physical_shape
return len(mine) == len(theirs) and all(lhs is rhs for lhs, rhs in zip(mine, theirs))
def _share_offset_caches(self, result: NestedTensor) -> None:
r"""Let ``result`` reuse this wrapper's derived-offset caches.
Callers must have established :meth:`_same_row_splits`. The cached conversions depend
only on the row-split tensors and their version counters; numerical and autograd views
are never inherited. Missing dictionaries are created here so that every wrapper derived
later fills and reads one shared cache instead of converting (and synchronising) again.
"""
if _is_fake_tensor(self._offsets):
return
if self._cached_packed_offsets is None:
self._cached_packed_offsets = {}
if self._cached_ragged_level_offsets is None:
self._cached_ragged_level_offsets = {}
result._cached_hierarchical_offsets = self._cached_hierarchical_offsets
result._cached_packed_offsets = self._cached_packed_offsets
result._cached_ragged_level_offsets = self._cached_ragged_level_offsets
def _layout_state(self) -> tuple:
r"""Return the metadata a storage-free layout wrapper is rebuilt from (see ``_PackedLikeAutograd``)."""
return (
type(self),
self._offsets,
self._physical_shape,
self._permutation,
self._ragged_dims if self._ragged_dims_explicit else None,
self._logical_shape,
self.batch_first,
self.padding_value,
self.mask_value,
self._persistent_ragged_offsets(),
)
@staticmethod
def _layout_from_state(state: tuple, like: Tensor, packed_shape: torch.Size) -> NestedTensor:
r"""Rebuild a layout wrapper from :meth:`_layout_state` around a zero-stride child on ``like``'s device."""
cls, offsets, shape_tensor, permutation, ragged_dims, logical_shape, batch_first, padding, mask, splits = state
return cls._from_packed(
like.new_empty(()).expand(packed_shape),
offsets,
shape_tensor,
permutation=permutation,
ragged_dims=ragged_dims,
batch_first=batch_first,
padding_value=padding,
mask_value=mask,
outer_size=logical_shape,
ragged_offsets=splits,
validate=False,
mark_dynamic=False,
)
def _mark_tensor_backed_dynamic_dims(self, *, mark_values: bool = True) -> None:
r"""Mark packed and logical ragged extents dynamic without guarded user state.
Factories and eager operator results retain these hints before entering a compiled
region. Automatic dynamic shapes alone can duck-size an unmarked ragged extent with
an unrelated, equally sized metadata dimension and then specialize both. Operators
already inside a compiled region preserve their symbolic extents without remarking.
"""
ragged_offsets = self._persistent_ragged_offsets()
if ragged_offsets is None:
return
# Go through the public marker rather than writing the private attribute behind it, so
# the mark keeps working if PyTorch renames its bookkeeping. This is the same call
# torch's own jagged NestedTensor makes for its ragged dim: a hint that a dimension may
# vary, which lets a NestedTensor cross a graph boundary without specializing its ragged
# or packed extent. The stricter `mark_dynamic` is wrong here because it asserts the
# dimension really is dynamic, and layouts whose ragged extent is fixed would fail it.
from torch._dynamo import maybe_mark_dynamic
# The marker only inspects wrapper metadata and annotates dimensions.
# Avoid routing its native Tensor descriptors through our generic
# __torch_function__ fallback repeatedly. Our Python dim()/size()
# methods still report the exact logical shape inside this context.
with torch._C.DisableTorchFunctionSubclass():
for physical_dim in self._ragged_dims:
logical_dim = physical_dim + 1 if self.batch_first or physical_dim > 0 else physical_dim
maybe_mark_dynamic(self, logical_dim)
if mark_values:
maybe_mark_dynamic(self.concat, 0)
for level_offsets in ragged_offsets:
maybe_mark_dynamic(level_offsets, 0)
def _inherit_tensor_backed_dynamic_dims(self, source: NestedTensor) -> None:
r"""Carry factory dynamism onto a fresh eager operator result before attaching autograd."""
ragged_offsets = self._persistent_ragged_offsets()
if ragged_offsets is None:
return
source_attrs = vars(source)
if (
type(self) is not NestedTensor
or type(self._packed_values) is not Tensor
or _get_current_dispatch_mode() is not None
or _is_fake_tensor(self._offsets)
or _is_fake_tensor(self._physical_shape)
or not isinstance(source_attrs.get("_dynamo_weak_dynamic_indices"), set)
or source_attrs.get("_has_dynamo_dim_marking") is not True
):
self._mark_tensor_backed_dynamic_dims()
return
# maybe_mark_dynamic installed these annotations on the source factory result.
# Propagate its weak-mark protocol, remapping logical axes and owning a fresh set.
# Recalling the public marker on a wrapper repeatedly flattens all its children;
# plain packed/offset tensors below do not have that overhead. Missing weak-mark
# metadata and tensor subclasses retain the public path above.
self._dynamo_weak_dynamic_indices = {
dim + 1 if self.batch_first or dim > 0 else dim for dim in self._ragged_dims
}
self._has_dynamo_dim_marking = True
from torch._dynamo import maybe_mark_dynamic
# This wrapper is still private: querying its raw child's metadata cannot add an
# unused projection to a checkpoint's saved-tensor sequence.
maybe_mark_dynamic(self._packed_values, 0)
for level_offsets in ragged_offsets:
maybe_mark_dynamic(level_offsets, 0)
# The public wrapper marker also annotates children with the same rank. For 1-D
# elements this includes the 2-D physical shape table.
if len(self._logical_shape) == self._physical_shape.dim():
for dim in self._dynamo_weak_dynamic_indices:
maybe_mark_dynamic(self._physical_shape, dim)
def _values_cache_token(self) -> tuple[int, ...]:
r"""Return a cache token for views that depend on packed values and layout metadata.
Tensors created under ``torch.inference_mode`` do not track version
counters, even after leaving the context. Fall back to object identity
for those immutable tensors so cached views remain usable.
"""
return (self._cache_version(self.concat), *self._shape_cache_token())
def _shape_cache_token(self) -> tuple[int, ...]:
r"""Return a cache token for views that depend only on shape metadata."""
ragged_offsets = self._persistent_ragged_offsets() or ()
return (
self._cache_version(self._offsets),
self._cache_version(self._physical_shape),
*(self._cache_version(level_offsets) for level_offsets in ragged_offsets),
)
@staticmethod
def _cache_version(tensor: Tensor) -> int:
try:
return int(tensor._version)
except RuntimeError as exc:
if "Inference tensors do not track version counter" not in str(exc):
raise
return id(tensor)
@staticmethod
def _offset_conversion_device_key(device: torch.device) -> str | None:
r"""Return an unambiguous per-device cache key for derived offsets.
An index-less non-CPU device follows PyTorch's current-device semantics.
Its concrete target can therefore change between calls, so it must not
participate in the conversion cache. CPU has no per-process current
index and remains safely cacheable.
"""
if device.type != "cpu" and device.index is None:
return None
return str(device)
@classmethod
def _from_packed(
cls,
values: Tensor,
offsets: Tensor,
shape_tensor: Tensor,
*,
permutation: tuple[int, ...] | None = None,
ragged_dims: tuple[int, ...] | None = None,
batch_first: bool = True,
padding_value: float = 0.0,
mask_value: bool = False,
pin_memory: bool = False,
outer_size: torch.Size | tuple | None = None,
packed_sizes: tuple[int, ...] | None = None,
element_shapes: tuple[tuple[int, ...], ...] | None = None,
ragged_offsets: tuple[Tensor, ...] | None = None,
validate: bool = True,
materialize_python_metadata: bool = True,
mark_dynamic: bool = True,
mark_values_dynamic: bool = True,
) -> Self:
r"""Construct a NestedTensor directly from packed representation.
``mark_dynamic`` hints Dynamo that the ragged extents may vary. Public factories and
eager operator results need it; operator intermediates already inside a compiled
region pass ``False`` (see :meth:`_mark_tensor_backed_dynamic_dims`).
"""
# offsets and shape_tensor MUST live on CPU to avoid implicit CUDA syncs
# when handlers call .item() / .tolist() on them.
if offsets.device.type != "cpu":
raise ValueError(f"offsets must be on CPU, got {offsets.device}")
if shape_tensor.device.type != "cpu":
raise ValueError(f"shape_tensor must be on CPU, got {shape_tensor.device}")
compiling = _is_compiling()
if validate and compiling:
_compile_unsupported("NestedTensor._from_packed", "metadata validation is eager-only")
if outer_size is not None:
logical_shape = torch.Size(outer_size)
elif compiling:
_compile_unsupported("NestedTensor._from_packed", "outer_size must be provided for compile-safe rebuilds")
else:
logical_shape = cls._logical_shape_from_physical_shape(shape_tensor, offsets, batch_first)
# An omitted permutation is resolved to ``ragged_dims`` followed by the static dims, which
# is tensor-backed by construction; an omitted ``ragged_dims`` is inferred below and then
# declared, so its layout is tensor-backed as well unless it turns out to be empty.
tensor_backed_layout = ragged_dims is None or (
permutation is None or cls._is_tensor_backed_layout(permutation, ragged_dims)
)
if tensor_backed_layout:
# A tensor-backed layout is completely described by its metadata tensors; the
# per-element Python shape caches stay off it in eager exactly as under compile.
materialize_python_metadata = False
if packed_sizes is None and materialize_python_metadata and not _is_fake_tensor(offsets):
packed_sizes = tuple(int(size) for size in (offsets[1:] - offsets[:-1]).tolist())
if element_shapes is None and materialize_python_metadata and not _is_fake_tensor(shape_tensor):
element_shapes = tuple(cls._trim_shape(shape) for shape in shape_tensor.tolist())
physical_rank = int(shape_tensor.size(1))
if permutation is None:
if ragged_dims is None:
resolved_permutation = cls._permutation_from_physical_shape(shape_tensor, element_shapes)
else:
resolved_ragged_dims = cls._normalize_ragged_dims(ragged_dims, physical_rank)
resolved_permutation = resolved_ragged_dims + tuple(
dim for dim in range(physical_rank) if dim not in resolved_ragged_dims
)
else:
resolved_permutation = tuple(int(dim) for dim in permutation)
resolved_ragged_dims = cls._ragged_dims_from_packed_layout(
values,
physical_rank,
resolved_permutation,
ragged_dims,
)
# An inferred layout is declared once resolved (see ``__new__``).
ragged_dims_explicit = ragged_dims is not None or bool(resolved_ragged_dims)
resolved_ragged_offsets = cls._resolve_persistent_ragged_offsets(
offsets,
shape_tensor,
permutation=resolved_permutation,
ragged_dims=resolved_ragged_dims if ragged_dims_explicit else None,
ragged_offsets=ragged_offsets,
element_shapes=element_shapes,
)
# The caller's Python metadata still validates the rebuild below; only the stored
# wrapper drops it for a tensor-backed layout.
stored_packed_sizes: tuple[int, ...] | None = packed_sizes
stored_element_shapes: tuple[tuple[int, ...], ...] | None = element_shapes
if resolved_ragged_offsets is not None:
# Standalone FakeTensor execution keeps them: fake values leave no other source of sizes.
if not compiling and not _is_fake_tensor(values):
stored_packed_sizes = None
stored_element_shapes = None
elif compiling and physical_rank > 0 and (packed_sizes is None or element_shapes is None):
_compile_unsupported(
"NestedTensor._from_packed",
"per-element Python metadata may be omitted only for a tensor-backed layout",
)
if (
not compiling
and _is_fake_tensor(values)
and not (_is_fake_tensor(offsets) and _is_fake_tensor(shape_tensor))
):
from torch._subclasses.fake_tensor import maybe_get_fake_mode
fake_mode = maybe_get_fake_mode(values)
if fake_mode is not None:
# Standalone FakeTensor execution has no shape environment to express
# data-dependent sizes, so the concrete row splits read here before the
# conversion stay on the wrapper as Python metadata.
if not _is_fake_tensor(offsets):
if stored_packed_sizes is None:
# ``tolist`` reads memory directly; a sliced difference would dispatch a
# real tensor through the active fake mode.
splits = offsets.tolist()
stored_packed_sizes = tuple(int(end - start) for start, end in zip(splits[:-1], splits[1:]))
offsets = fake_mode.from_tensor(offsets, static_shapes=True, trace=False)
if not _is_fake_tensor(shape_tensor):
if stored_element_shapes is None:
rows = shape_tensor.tolist()
stored_element_shapes = (
tuple(tuple(int(size) for size in row) for row in rows)
if resolved_ragged_offsets is not None
else tuple(cls._trim_shape(row) for row in rows)
)
shape_tensor = fake_mode.from_tensor(shape_tensor, static_shapes=True, trace=False)
if resolved_ragged_offsets is not None:
if len(resolved_ragged_offsets) == 1:
resolved_ragged_offsets = (offsets,)
else:
resolved_ragged_offsets = tuple(
(
level_offsets
if _is_fake_tensor(level_offsets)
else fake_mode.from_tensor(level_offsets, static_shapes=True, trace=False)
)
for level_offsets in resolved_ragged_offsets
)
values = cls._maybe_pin_values(values, pin_memory)
result: Self
if compiling:
constructor = cls._compiled_packed_constructor
result = cast(
Self,
constructor(
values,
offsets,
shape_tensor,
logical_shape,
resolved_permutation,
resolved_ragged_dims,
ragged_dims_explicit,
bool(batch_first),
float(padding_value),
bool(mask_value),
bool(pin_memory and values.device.type == "cpu" and values.is_pinned()),
stored_packed_sizes,
stored_element_shapes,
resolved_ragged_offsets,
),
)
else:
result = torch.Tensor._make_wrapper_subclass(
cls,
logical_shape,
dtype=values.dtype,
device=values.device,
requires_grad=values.requires_grad,
)
result._packed_values = values
result._offsets = offsets
result._permutation = resolved_permutation
result._ragged_dims = resolved_ragged_dims
result._ragged_dims_explicit = ragged_dims_explicit
result._physical_shape = shape_tensor
result._logical_shape = logical_shape
result._set_runtime_config(
batch_first=batch_first,
padding_value=padding_value,
mask_value=mask_value,
)
result._pin_memory = bool(pin_memory and values.device.type == "cpu" and values.is_pinned())
result._packed_sizes = stored_packed_sizes
result._element_shapes = stored_element_shapes
cls._install_persistent_ragged_offsets(result, resolved_ragged_offsets)
result._invalidate_transient_caches()
if mark_dynamic:
result._mark_tensor_backed_dynamic_dims(mark_values=mark_values_dynamic)
if validate:
cls._validate_packed_metadata(
result.concat,
result._offsets,
result._physical_shape,
permutation=result._permutation,
ragged_dims=result._ragged_dims,
logical_shape=result._logical_shape,
batch_first=result.batch_first,
packed_sizes=packed_sizes,
element_shapes=element_shapes,
ragged_offsets=resolved_ragged_offsets,
)
return result
# ------------------------------------------------------------------
# torch.compile support
# ------------------------------------------------------------------
@staticmethod
def _tensor_metadata_for_caching(tensor: Tensor) -> tuple[Any, ...]:
r"""Return stable tensor metadata without inspecting storage contents.
Shapes and strides may contain ``SymInt`` expressions. Their ``repr`` is
intentionally retained by :meth:`_stable_hash_for_caching`, matching the
representation-based stable hash used by PyTorch's ``DTensor`` cache
extension. Storage addresses and tensor values are excluded;
``storage_offset`` itself remains ordinary tensor metadata.
"""
return (
str(tensor.dtype),
tensor.device.type,
tensor.device.index,
str(tensor.layout),
tuple(tensor.shape),
tuple(tensor.stride()),
tensor.storage_offset(),
bool(tensor.requires_grad),
bool(tensor.is_conj()),
bool(tensor.is_neg()),
bool(tensor.is_inference()),
)
def _stable_hash_for_caching(self) -> str:
r"""Return a deterministic metadata hash for PyTorch's AOTAutograd cache.
Tensor-backed offsets, physical shapes, and hierarchical row splits are
flattened children. Only their tensor metadata participates in this hash;
their data does not. Consequently, two dynamic calls with the same static
structure can reuse one cache entry even when their ragged lengths differ.
Legacy layouts whose topology still lives in ``packed_sizes`` or
``element_shapes`` retain those tuples in the static flatten context and
therefore remain safely layout-specific.
"""
inner_tensor_names, context = self.__tensor_flatten__()
tensor_metadata = type(self)._tensor_metadata_for_caching
payload = (
"danling.NestedTensor.aot_autograd",
type(self)._AOT_CACHE_HASH_VERSION,
type(self).__module__,
type(self).__qualname__,
tensor_metadata(self),
tuple((name, tensor_metadata(getattr(self, name))) for name in inner_tensor_names),
tuple(context.items()),
)
return hashlib.blake2b(repr(payload).encode("utf-8"), digest_size=16).hexdigest()
@property
def _max_length_binding(self) -> Tensor:
r"""Bind the data-derived logical maximum with one tensor dimension."""
binding = vars(self).get("_compile_max_length_binding")
if binding is not None:
return binding
max_length = cast(int, self._physical_shape[:, 0].max().item()) if self._physical_shape.size(0) else 0
return self._offsets.new_empty(()).expand(max_length)
@_max_length_binding.setter
def _max_length_binding(self, binding: Tensor) -> None:
self._compile_max_length_binding = binding
def __tensor_flatten__(self):
# During tracing, wrapper instances can be inspected while being built.
# Only expose tensor attrs that already exist so Dynamo/FakeTensor can
# inspect partially constructed wrapper subclasses safely.
instance_attrs = vars(self)
inner_tensors = [name for name in ("_offsets", "_physical_shape") if name in instance_attrs]
if "_compile_max_length_binding" in instance_attrs:
inner_tensors.append("_max_length_binding")
# One packed child owns both the public projection and its tangent.
# Exposing the raw storage again would introduce a second autograd edge.
if "_packed_values" in instance_attrs:
inner_tensors.append("concat")
if not inner_tensors:
inner_tensors = ["_flatten_sentinel"]
permutation = getattr(self, "_permutation", ())
ragged_dims = getattr(self, "_ragged_dims", ()) if getattr(self, "_ragged_dims_explicit", False) else None
ragged_offsets = self._persistent_ragged_offsets() if "_offsets" in instance_attrs else None
if ragged_offsets is not None and len(ragged_offsets) > 1:
inner_tensors.extend(type(self)._ragged_offset_names(len(ragged_offsets)))
tensor_backed_layout = ragged_offsets is not None
return inner_tensors, {
"requires_grad": self.requires_grad,
"is_aot_tangent": vars(self).get("_is_aot_tangent", False),
"batch_first": getattr(self, "batch_first", True),
"padding_value": getattr(self, "padding_value", 0.0),
"mask_value": getattr(self, "mask_value", False),
"pin_memory": getattr(self, "_pin_memory", False),
"packed_sizes": None if tensor_backed_layout else getattr(self, "_packed_sizes", ()),
"element_shapes": None if tensor_backed_layout else getattr(self, "_element_shapes", ()),
"permutation": permutation,
"ragged_dims": ragged_dims,
}
@classmethod
def __tensor_unflatten__(cls, inner_tensors, ctx, outer_size, outer_stride):
values = inner_tensors.get("concat", inner_tensors.get("_flatten_sentinel"))
if values is None:
raise RuntimeError("NestedTensor requires concat during tensor unflatten.")
ctx = dict(ctx)
wrapper_requires_grad = bool(ctx.pop("requires_grad", values.requires_grad))
is_aot_tangent = bool(ctx.pop("is_aot_tangent", False))
offsets = inner_tensors.get("_offsets")
shape_tensor = inner_tensors.get("_physical_shape")
if offsets is not None and shape_tensor is not None:
# During backward, outer_size may reflect a transposed view
# (e.g., (seq, batch, hidden) from MHA's batch-dim transpose).
# Detect and correct so _from_packed validation passes.
batch_size = len(offsets) - 1
outer = tuple(outer_size)
batch_first = ctx.get("batch_first", True)
if len(outer) >= 2 and (
(batch_first and outer[0] != batch_size and outer[1] == batch_size)
or (not batch_first and outer[1] != batch_size and outer[0] == batch_size)
):
outer = (outer[1], outer[0], *outer[2:])
max_length_binding = inner_tensors.get("_max_length_binding")
preserve_tensor_metadata = (
cls._is_tensor_backed_layout(ctx.get("permutation"), ctx.get("ragged_dims"))
and ctx.get("packed_sizes") is None
and ctx.get("element_shapes") is None
)
ragged_rank = len(ctx.get("ragged_dims") or ())
if preserve_tensor_metadata and ragged_rank > 1:
names = cls._ragged_offset_names(ragged_rank)
missing = tuple(name for name in names if name not in inner_tensors)
if missing:
raise RuntimeError(
"NestedTensor tensor-backed multi-ragged unflatten is missing row-split children: "
+ ", ".join(missing)
)
ragged_offsets = tuple(inner_tensors[name] for name in names)
elif preserve_tensor_metadata and ragged_rank == 1:
ragged_offsets = (offsets,)
else:
ragged_offsets = None
result = cls._from_packed(
values,
offsets,
shape_tensor,
outer_size=outer,
validate=False,
materialize_python_metadata=not preserve_tensor_metadata,
ragged_offsets=ragged_offsets,
**ctx,
)
if torch.Tensor.requires_grad.__get__(result) != wrapper_requires_grad:
torch.Tensor.requires_grad.__set__(result, wrapper_requires_grad)
if max_length_binding is not None:
result._max_length_binding = max_length_binding
result._is_aot_tangent = is_aot_tangent
result._unflattened_autograd = True
return result
result = torch.Tensor._make_wrapper_subclass(
cls,
torch.Size(outer_size),
dtype=values.dtype,
device=values.device,
requires_grad=wrapper_requires_grad,
)
result._packed_values = values
if offsets is not None:
result._offsets = offsets
if shape_tensor is not None:
result._physical_shape = shape_tensor
result._logical_shape = torch.Size(outer_size)
result._set_runtime_config(
batch_first=ctx["batch_first"],
padding_value=ctx["padding_value"],
mask_value=ctx["mask_value"],
)
result._pin_memory = ctx["pin_memory"]
result._packed_sizes = ctx["packed_sizes"]
result._element_shapes = ctx["element_shapes"]
result._permutation = tuple(int(dim) for dim in ctx["permutation"])
declared_ragged_dims = ctx.get("ragged_dims")
result._ragged_dims = cls._ragged_dims_from_packed_layout(
values,
len(result._permutation),
result._permutation,
declared_ragged_dims,
)
result._ragged_dims_explicit = declared_ragged_dims is not None or bool(result._ragged_dims)
ragged_rank = len(result._ragged_dims)
names = cls._ragged_offset_names(ragged_rank)
ragged_offsets = tuple(inner_tensors[name] for name in names if name in inner_tensors)
if ragged_rank == 1 and ctx.get("packed_sizes") is None and ctx.get("element_shapes") is None:
ragged_offsets = (offsets,) if offsets is not None else ()
cls._install_persistent_ragged_offsets(result, ragged_offsets or None)
max_length_binding = inner_tensors.get("_max_length_binding")
if max_length_binding is not None:
result._max_length_binding = max_length_binding
result._invalidate_transient_caches()
result._is_aot_tangent = is_aot_tangent
result._unflattened_autograd = True
result._mark_tensor_backed_dynamic_dims()
return result
def __coerce_tangent_metadata__(self):
r"""Classify this wrapper as a tangent through tracing and runtime layout coercion."""
self._is_aot_tangent = True
return self
def __coerce_same_metadata_as_tangent__(self, expected_meta, expected_type=None):
r"""Apply the tangent classification expected by AOT's compiled backward."""
if expected_type is not None and expected_type is not type(self):
return None
self._is_aot_tangent = bool(expected_meta.get("is_aot_tangent", False))
return self
# ------------------------------------------------------------------
# Dispatch
# ------------------------------------------------------------------
@classmethod
def __torch_function__(cls, func, types, args=(), kwargs=None) -> Any:
if kwargs is None:
kwargs = {}
# Handle size() specially to avoid infinite recursion
if func is torch.Tensor.size:
self = args[0]
dim = args[1] if len(args) > 1 else kwargs.get("dim")
return self.size(dim)
handler = NestedTensorFuncRegistry.get(func)
if handler is not None:
if _is_compiling() and not NestedTensorFuncRegistry.is_compile_safe(func, args, kwargs):
name = getattr(func, "__qualname__", getattr(func, "__name__", repr(func)))
_compile_unsupported(name, "handler is marked eager-only")
return handler(*args, **kwargs)
with torch._C.DisableTorchFunctionSubclass():
return func(*args, **kwargs)
@staticmethod
def __torch_dispatch__( # type: ignore[override]
func: torch._ops.OpOverload,
types: Iterable[type],
args: tuple[object, ...] = (),
kwargs: dict[str, object] | None = None,
) -> object:
# Tensor's stub treats its C dispatch sentinel as an instance method;
# the runtime protocol passes the operator first, without an instance.
if kwargs is None:
kwargs = {}
if func in NestedTensorAtenRegistry:
if _is_compiling() and not NestedTensorAtenRegistry.is_compile_safe(func, args, kwargs):
name = getattr(func, "name", None)
if callable(name):
name = name()
_compile_unsupported(name or repr(func), "aten handler is marked eager-only")
return NestedTensorAtenRegistry[func](func, args, kwargs)
if _is_compiling():
name = getattr(func, "name", None)
if callable(name):
name = name()
_compile_unsupported(name or repr(func), "would fall back to per-element eager execution")
return per_element_fallback(func, args, kwargs)
# ------------------------------------------------------------------
# Layout & Metadata Helpers
# ------------------------------------------------------------------
def _unpack(self) -> tuple[Tensor, ...]:
r"""Reconstruct individual tensors from packed representation."""
_check_execution_guard(_ExecutionGuardKind.STORAGE_MAP, "NestedTensor._unpack")
batch_size = len(self._offsets) - 1
if batch_size == 0:
return ()
packed_sizes = self._packed_sizes
if packed_sizes is None:
if _is_fake_tensor(self._offsets):
raise RuntimeError("NestedTensor packed sizes are unavailable for this instance.")
packed_sizes = tuple(int(size) for size in (self._offsets[1:] - self._offsets[:-1]).tolist())
element_shapes = self._element_shapes
if element_shapes is None:
element_shapes = tuple(tuple(int(dim) for dim in shape) for shape in self._original_shapes())
splits = self.concat.split(packed_sizes, dim=0)
permutation = self._permutation
if permutation:
varying_dims = self._varying_dims
static_dims = self._static_dims
else:
varying_dims, static_dims = type(self)._pack_layout_meta(self._physical_shape, element_shapes)
permutation = varying_dims + static_dims
inverse_permutation = type(self)._inverse_permutation(permutation)
result = []
for chunk, shape in zip(splits, element_shapes):
if not shape:
result.append(chunk[0])
else:
packed_shape = tuple(shape[dim] for dim in varying_dims) + tuple(shape[dim] for dim in static_dims)
unpacked = chunk.reshape(packed_shape)
if permutation != tuple(range(len(shape))):
unpacked = unpacked.permute(inverse_permutation)
result.append(unpacked)
return tuple(result)
def _repack(self, tensors: Sequence) -> None:
r"""
Re-pack from already-validated tensors. Skips coercion — callers must ensure
tensors share device, dtype, and ndim (which is always true for internal paths
since tensors originate from _unpack or __setitem__ validation)."""
self._invalidate_transient_caches()
tensors = tuple(tensors) if not isinstance(tensors, tuple) else tensors
if tensors and len(self._permutation) != tensors[0].ndim:
raise RuntimeError(
"NestedTensor._repack received tensors with rank "
f"{tensors[0].ndim} but current permutation has rank {len(self._permutation)}"
)
if self._ragged_dims_explicit and tensors:
# Keep the declaration only where the new elements still fit it.
shapes = tuple(tuple(int(size) for size in tensor.shape) for tensor in tensors)
try:
type(self)._pack_layout_from_declared_ragged_dims(shapes, self._ragged_dims)
except ValueError:
self._ragged_dims_explicit = False
values, offsets, shape_tensor, packed_sizes, element_shapes = self._pack(
tensors,
permutation=self._permutation if tensors else None,
ragged_dims=self._ragged_dims if self._ragged_dims_explicit else None,
)
values = type(self)._maybe_pin_values(values, self._pin_memory)
self._packed_values = values
self._offsets = offsets
self._physical_shape = shape_tensor
self._logical_shape = self._compute_logical_shape(tensors, self.batch_first)
if not self._ragged_dims_explicit:
self._ragged_dims = type(self)._ragged_dims_from_packed_layout(
values,
int(shape_tensor.size(1)),
self._permutation,
None,
)
self._ragged_dims_explicit = bool(self._ragged_dims)
ragged_offsets = type(self)._resolve_persistent_ragged_offsets(
offsets,
shape_tensor,
permutation=self._permutation,
ragged_dims=self._ragged_dims if self._ragged_dims_explicit else None,
)
self._packed_sizes = None if ragged_offsets is not None else packed_sizes
self._element_shapes = None if ragged_offsets is not None else element_shapes
type(self)._install_persistent_ragged_offsets(self, ragged_offsets)
self._mark_tensor_backed_dynamic_dims()
self._validate_metadata()
@property
def _hierarchical_offsets(self) -> tuple[Tensor, ...]:
persistent = self._persistent_ragged_offsets()
if persistent is not None:
return persistent
if self._cached_hierarchical_offsets is None:
level_sizes = type(self)._hierarchical_level_sizes_from_physical_shape(
self._physical_shape,
self._element_shapes,
self._ragged_dims,
)
if not level_sizes:
if self._element_shapes is None and self._packed_sizes is not None:
self._cached_hierarchical_offsets = (
type(self)._offsets_from_sizes(self._packed_sizes, dtype=self._offsets.dtype),
)
elif self._element_shapes is None and _is_fake_tensor(self._physical_shape):
self._cached_hierarchical_offsets = (self._offsets,)
else:
self._cached_hierarchical_offsets = ()
elif len(level_sizes) == 1:
self._cached_hierarchical_offsets = (self._offsets,)
else:
self._cached_hierarchical_offsets = tuple(
type(self)._offsets_from_sizes(level_sizes[level], dtype=self._offsets.dtype)
for level in range(len(level_sizes))
)
return self._cached_hierarchical_offsets
@property
def _ragged_rank(self) -> int:
return len(self._ragged_dims)
def _ragged_level_offsets(self, level: int = -1) -> Tensor:
offsets = self._hierarchical_offsets
if not offsets:
return self._offsets
return offsets[level]
def packed_offsets(
self,
*,
device: torch.device | str | None = None,
dtype: torch.dtype | None = None,
) -> Tensor:
r"""Return logical-batch boundaries in the packed leading dimension.
The returned offsets delimit each logical batch element in ``concat``.
This differs from :meth:`ragged_level_offsets` for multi-ragged layouts:
``packed_offsets`` always addresses complete per-sample packed chunks,
while ragged-level offsets address rows within the ragged hierarchy.
Device and dtype conversions are cached per ``NestedTensor`` instance.
"""
offsets = self._offsets
target_device = offsets.device if device is None else torch.device(device)
target_dtype = offsets.dtype if dtype is None else dtype
if offsets.device == target_device and offsets.dtype == target_dtype:
return offsets
device_key = type(self)._offset_conversion_device_key(target_device)
key = None if device_key is None else (device_key, target_dtype, self._shape_cache_token())
if key is not None and self._cached_packed_offsets is not None:
cached = self._cached_packed_offsets.get(key)
if cached is not None:
return cached
elif key is not None and not _is_fake_tensor(offsets):
self._cached_packed_offsets = {}
converted = offsets.to(device=target_device, dtype=target_dtype)
if key is not None and self._cached_packed_offsets is not None:
self._cached_packed_offsets[key] = converted
return converted
def element_sizes(self) -> Tensor:
r"""Return every logical element shape as a CPU integer tensor.
Rows follow logical batch order and columns follow logical element
dimension order, independent of ``batch_first`` and physical packed
storage order. The returned ``(batch_size, element_rank)`` tensor is
the canonical tensor-backed shape metadata and is therefore returned
without a copy.
"""
return self._physical_shape
def ragged_level_offsets(
self,
level: int = -1,
*,
device: torch.device | str | None = None,
dtype: torch.dtype | None = None,
) -> Tensor:
r"""Return ragged-level offsets, caching device and dtype conversions."""
offsets = self._ragged_level_offsets(level)
target_device = offsets.device if device is None else torch.device(device)
target_dtype = offsets.dtype if dtype is None else dtype
if offsets.device == target_device and offsets.dtype == target_dtype:
return offsets
device_key = type(self)._offset_conversion_device_key(target_device)
key = None if device_key is None else (int(level), device_key, target_dtype, self._shape_cache_token())
if key is not None and self._cached_ragged_level_offsets is not None:
cached = self._cached_ragged_level_offsets.get(key)
if cached is not None:
return cached
elif key is not None and not _is_fake_tensor(offsets):
self._cached_ragged_level_offsets = {}
converted = offsets.to(device=target_device, dtype=target_dtype)
if key is not None and self._cached_ragged_level_offsets is not None:
self._cached_ragged_level_offsets[key] = converted
return converted
def _ragged_level_sizes(self, level: int = -1) -> Tensor:
offsets = self._ragged_level_offsets(level)
return offsets[1:] - offsets[:-1]
def packed_local_indices(
self,
level: int = 0,
*,
device: torch.device | str | None = None,
dtype: torch.dtype = torch.long,
) -> Tensor:
r"""Return local coordinates within the selected packed ragged level."""
target_device = self.device if device is None else torch.device(device)
level = int(level)
key = (level, str(target_device), dtype, self._shape_cache_token())
if self._cached_packed_local_indices is not None:
cached = self._cached_packed_local_indices.get(key)
if cached is not None:
return cached
elif not _is_fake_tensor(self._offsets) and not _is_compiling():
self._cached_packed_local_indices = {}
packed_sizes = self._packed_sizes
if (
level == 0
and self._ragged_rank == 1
and packed_sizes is not None
and (_is_compiling() or _is_fake_tensor(self._offsets))
):
lengths_tuple = tuple(int(size) for size in packed_sizes)
starts_tuple: list[int] = []
running = 0
for length in lengths_tuple:
starts_tuple.append(running)
running += length
lengths = torch.tensor(lengths_tuple, dtype=torch.long, device=target_device)
starts_source = torch.tensor(starts_tuple, dtype=dtype, device=target_device)
total = running
else:
hierarchical_offsets = self._hierarchical_offsets
if hierarchical_offsets:
normalized_level = level if level >= 0 else len(hierarchical_offsets) + level
if normalized_level < 0 or normalized_level >= len(hierarchical_offsets):
raise IndexError(f"ragged level {level} is out of range for rank {len(hierarchical_offsets)}")
offsets = hierarchical_offsets[normalized_level]
is_last_level = normalized_level == len(hierarchical_offsets) - 1
else:
normalized_level = 0
offsets = self._offsets
is_last_level = True
lengths = offsets[1:] - offsets[:-1]
# A cache miss must not project an autograd edge just to inspect shape:
# checkpoint replay can reuse these indices and skip that projection's save.
total = (
self._packed_values.shape[0]
if is_last_level
else hierarchical_offsets[normalized_level + 1].numel() - 1
)
starts_source = offsets[:-1].to(device=target_device, dtype=dtype)
positions = torch.arange(total, dtype=dtype, device=target_device)
starts = torch.repeat_interleave(starts_source, lengths.to(target_device), output_size=total)
local_indices = positions - starts
if self._cached_packed_local_indices is not None:
self._cached_packed_local_indices[key] = local_indices
return local_indices
def packed_batch_indices(
self,
*,
device: torch.device | str | None = None,
dtype: torch.dtype = torch.long,
) -> Tensor:
r"""Return batch coordinates for packed values."""
target_device = self.device if device is None else torch.device(device)
key = (str(target_device), dtype, self._shape_cache_token())
if self._cached_packed_batch_indices is not None:
cached = self._cached_packed_batch_indices.get(key)
if cached is not None:
return cached
elif not _is_fake_tensor(self._offsets) and not _is_compiling():
self._cached_packed_batch_indices = {}
packed_sizes = self._packed_sizes
if packed_sizes is not None and (_is_compiling() or _is_fake_tensor(self._offsets)):
lengths_tuple = tuple(int(size) for size in packed_sizes)
lengths = torch.tensor(lengths_tuple, dtype=torch.long, device=target_device)
total = sum(lengths_tuple)
batch_source = torch.arange(len(lengths_tuple), dtype=dtype, device=target_device)
else:
offsets = self._offsets
lengths = offsets[1:] - offsets[:-1]
# Keep cached integer metadata independent of the autograd save sequence.
total = self._packed_values.shape[0]
batch_source = torch.arange(offsets.numel() - 1, dtype=dtype, device=target_device)
batch_indices = torch.repeat_interleave(batch_source, lengths.to(target_device), output_size=total)
if self._cached_packed_batch_indices is not None:
self._cached_packed_batch_indices[key] = batch_indices
return batch_indices
@property
def _varying_dims(self) -> tuple[int, ...]:
return self._ragged_dims
@property
def _static_dims(self) -> tuple[int, ...]:
return tuple(int(dim) for dim in self._permutation[len(self._ragged_dims) :])
def _has_same_structure(self, other: Self) -> bool:
if (
self.batch_first != other.batch_first
or self._permutation != other._permutation
or self._ragged_dims != other._ragged_dims
):
return False
if self._element_shapes is not None and other._element_shapes is not None:
lhs_levels = type(self)._hierarchical_level_sizes_from_element_shapes(
self._element_shapes,
self._ragged_dims,
)
rhs_levels = type(self)._hierarchical_level_sizes_from_element_shapes(
other._element_shapes,
other._ragged_dims,
)
if lhs_levels or rhs_levels:
return lhs_levels == rhs_levels
return len(self) == len(other)
lhs_offsets = self._hierarchical_offsets
rhs_offsets = other._hierarchical_offsets
if len(lhs_offsets) != len(rhs_offsets):
return False
runtime_assert = _is_compiling() or _is_fake_tensor(self._offsets) or _is_fake_tensor(other._offsets)
if lhs_offsets:
return all(
type(self)._meta_tensor_equal(
lhs,
rhs,
"NestedTensor ragged offsets must match",
runtime_assert=runtime_assert,
)
for lhs, rhs in zip(lhs_offsets, rhs_offsets)
)
return type(self)._meta_tensor_equal(
self._offsets,
other._offsets,
"NestedTensor ragged offsets must match",
runtime_assert=runtime_assert,
)
def _has_same_layout(self, other: Self) -> bool:
if not self._has_same_structure(other):
return False
if self._element_shapes is not None and other._element_shapes is not None:
if self._element_shapes != other._element_shapes:
return False
if self._packed_sizes is not None and other._packed_sizes is not None:
return self._packed_sizes == other._packed_sizes
return True
if (
self._packed_sizes is not None
and other._packed_sizes is not None
and self._packed_sizes != other._packed_sizes
):
return False
runtime_assert = _is_compiling() or _is_fake_tensor(self._offsets) or _is_fake_tensor(other._offsets)
if not type(self)._meta_tensor_equal(
self._physical_shape,
other._physical_shape,
"NestedTensor physical shapes must match",
runtime_assert=runtime_assert,
):
return False
return type(self)._meta_tensor_equal(
self._offsets,
other._offsets,
"NestedTensor ragged offsets must match",
runtime_assert=runtime_assert,
)
def _packed_flat_index(
self,
*,
device: torch.device | None = None,
dtype: torch.dtype = torch.long,
) -> Tensor:
target_device = self.device if device is None else device
leading = self.concat.size(0) if self.concat.dim() > 0 else self.concat.numel()
return torch.arange(leading, device=target_device, dtype=dtype)
def _packed_batch_local_indices(
self,
flat_idx: Tensor | None = None,
*,
device: torch.device | None = None,
dtype: torch.dtype = torch.long,
) -> tuple[Tensor, Tensor]:
target_device = self.device if device is None else device
if flat_idx is None:
flat_idx = self._packed_flat_index(device=target_device, dtype=dtype)
batch_idx = self.packed_batch_indices(device=target_device, dtype=dtype)
offsets = self._offsets.to(device=target_device, dtype=dtype)
lookup_idx = batch_idx if batch_idx.dtype == torch.long else batch_idx.to(dtype=torch.long)
return batch_idx, flat_idx - offsets[lookup_idx]
offsets = self._offsets.to(device=target_device, dtype=dtype)
batch_idx = torch.searchsorted(offsets[1:], flat_idx, right=True)
local_idx = flat_idx - offsets[batch_idx]
return batch_idx, local_idx
def _packed_varying_coords(
self,
batch_idx: Tensor,
local_idx: Tensor,
*,
device: torch.device | None = None,
dtype: torch.dtype = torch.long,
) -> tuple[Tensor, ...]:
target_device = self.device if device is None else device
varying_dims = self._varying_dims
if not varying_dims:
return ()
varying_sizes = self._physical_shape[:, list(varying_dims)].to(device=target_device, dtype=dtype)[batch_idx]
strides = torch.ones_like(varying_sizes)
running = torch.ones(varying_sizes.size(0), dtype=dtype, device=target_device)
for dim in range(varying_sizes.size(1) - 1, -1, -1):
strides[:, dim] = running
running = running * varying_sizes[:, dim]
coords: list[Tensor] = []
remainder = local_idx
for dim in range(varying_sizes.size(1)):
coord = remainder // strides[:, dim]
coords.append(coord)
remainder = remainder - coord * strides[:, dim]
return tuple(coords)
def _packed_dense_index(
self,
flat_idx: Tensor | None = None,
*,
device: torch.device | None = None,
dtype: torch.dtype = torch.long,
) -> tuple[Tensor | slice, ...]:
target_device = self.device if device is None else device
batch_idx, local_idx = self._packed_batch_local_indices(flat_idx, device=target_device, dtype=dtype)
varying_dims = self._varying_dims
coords = self._packed_varying_coords(batch_idx, local_idx, device=target_device, dtype=dtype)
coord_by_dim = dict(zip(varying_dims, coords))
dense_index: list[Tensor | slice] = [batch_idx]
for dim in range(self._physical_shape.size(1)):
dense_index.append(coord_by_dim[dim] if dim in coord_by_dim else slice(None))
return tuple(dense_index)
def _physical_shape_like_batch_dense(self, batch_dense_shape: Sequence[int]) -> Tensor:
r"""Return per-element shapes for a batch-leading dense tensor with this NestedTensor's ragged structure."""
expected_ndim = self._physical_shape.size(1) + 1
if len(batch_dense_shape) != expected_ndim:
raise ValueError(
"Batch-leading dense tensor rank does not match NestedTensor layout, "
f"expected {expected_ndim}, got {len(batch_dense_shape)}"
)
shape, _, _ = self._shape_meta_from_components(
replace_dims={int(dim): int(batch_dense_shape[dim + 1]) for dim in self._static_dims}
)
return shape
def _element_shapes_like_batch_dense(
self,
batch_dense_shape: Sequence[int],
) -> tuple[tuple[int, ...], ...] | None:
r"""Return Python element-shape metadata for a batch-leading dense tensor with this NestedTensor's layout."""
expected_ndim = self._physical_shape.size(1) + 1
if len(batch_dense_shape) != expected_ndim:
raise ValueError(
"Batch-leading dense tensor rank does not match NestedTensor layout, "
f"expected {expected_ndim}, got {len(batch_dense_shape)}"
)
_, _, element_shapes = self._shape_meta_from_components(
replace_dims={int(dim): int(batch_dense_shape[dim + 1]) for dim in self._static_dims}
)
return element_shapes
def _shape_meta_from_components(
self,
*,
prefix: Sequence[int] = (),
keep_dims: Sequence[int] | None = None,
suffix: Sequence[int] = (),
replace_dims: Mapping[int, int] | None = None,
) -> tuple[Tensor, tuple[int, ...] | None, tuple[tuple[int, ...], ...] | None]:
r"""Build packed shape metadata by keeping selected dims and applying constant prefix/suffix updates."""
if keep_dims is None:
keep_dims = tuple(range(self._physical_shape.size(1)))
keep_dims = tuple(int(dim) for dim in keep_dims)
prefix = tuple(int(size) for size in prefix)
suffix = tuple(int(size) for size in suffix)
updates = {int(dim): int(size) for dim, size in (replace_dims or {}).items()}
if self._element_shapes:
element_shapes_list: list[tuple[int, ...]] = []
for element_shape in self._element_shapes:
projected = [*prefix, *(int(element_shape[dim]) for dim in keep_dims), *suffix]
for dim, size in updates.items():
projected[dim] = size
element_shapes_list.append(tuple(projected))
element_shapes = tuple(element_shapes_list)
max_ndim = max(len(shape) for shape in element_shapes)
shape = torch.tensor(
[list(shape) + [0] * (max_ndim - len(shape)) for shape in element_shapes],
dtype=torch.long,
)
output_ragged_dims = None
if self._ragged_dims_explicit:
output_ragged_dims = tuple(
len(prefix) + keep_dims.index(dim) for dim in self._ragged_dims if dim in keep_dims
)
return shape, self._packed_sizes_like(element_shapes, output_ragged_dims), element_shapes
parts: list[Tensor] = []
batch_size = len(self)
if prefix:
parts.append(self._physical_shape.new_tensor(prefix).reshape(1, -1).expand(batch_size, -1))
if keep_dims == tuple(range(self._physical_shape.size(1))):
parts.append(self._physical_shape.clone())
elif keep_dims:
parts.append(self._physical_shape[:, list(keep_dims)].clone())
if suffix:
parts.append(self._physical_shape.new_tensor(suffix).reshape(1, -1).expand(batch_size, -1))
if parts:
shape = torch.cat(parts, dim=1)
else:
shape = self._physical_shape.new_empty((batch_size, 0))
for dim, size in updates.items():
shape[:, dim] = size
return shape, None, None
def _max_physical_dims(self) -> tuple[int, ...]:
r"""Return the maximum per-element size for each physical dimension (excluding batch)."""
batch_dim = type(self)._batch_dim_from_logical_shape(self._logical_shape, self.batch_first)
return tuple(size for index, size in enumerate(self._logical_shape) if index != batch_dim)
def _logical_shape_from_physical_dims(self, physical_dims: Sequence[int]) -> torch.Size:
r"""Build a logical outer shape from non-batch physical-dimension sizes."""
physical_dims = tuple(physical_dims)
batch_size = len(self)
if self.batch_first:
return torch.Size((batch_size, *physical_dims))
if not physical_dims:
return torch.Size((batch_size,))
return torch.Size((physical_dims[0], batch_size, *physical_dims[1:]))
def _logical_shape_from_components(
self,
*,
prefix: Sequence[int] = (),
keep_dims: Sequence[int] | None = None,
suffix: Sequence[int] = (),
replace_dims: Mapping[int, int] | None = None,
) -> torch.Size:
r"""Build a logical outer shape by projecting the current physical-dimension extents."""
physical_dims = list(self._max_physical_dims())
if keep_dims is None:
keep_dims = tuple(range(len(physical_dims)))
projected = [*prefix, *(physical_dims[int(dim)] for dim in keep_dims)]
projected.extend(suffix)
for dim, size in (replace_dims or {}).items():
projected[int(dim)] = size
return self._logical_shape_from_physical_dims(projected)
def _leading_dim_preserving_meta(
self,
suffix: Sequence[int],
) -> tuple[Tensor, torch.Size, tuple[int, ...] | None, tuple[tuple[int, ...], ...] | None]:
r"""Build metadata for ops that preserve the first per-element dim and replace all trailing dims uniformly."""
keep_dims = (0,) if self._physical_shape.size(1) > 0 else ()
shape, packed_sizes, element_shapes = self._shape_meta_from_components(keep_dims=keep_dims, suffix=suffix)
return shape, self._leading_dim_preserving_outer_size(suffix), packed_sizes, element_shapes
def _leading_dim_preserving_outer_size(self, suffix: Sequence[int]) -> torch.Size:
r"""Return logical outer size for ops that preserve per-element dim-0 and replace trailing dims uniformly."""
suffix = tuple(int(size) for size in suffix)
batch_size = len(self)
batch_dim = 0 if self.batch_first else 1
logical = list(self._logical_shape)
non_batch = [int(logical[index]) for index in range(len(logical)) if index != batch_dim]
new_non_batch: list[int] = []
if self._physical_shape.size(1) > 0 and non_batch:
new_non_batch.append(non_batch[0])
new_non_batch.extend(suffix)
if self.batch_first:
return torch.Size((batch_size, *new_non_batch))
if not new_non_batch:
return torch.Size((batch_size,))
return torch.Size((new_non_batch[0], batch_size, *new_non_batch[1:]))
def _drop_trailing_physical_dims_meta(
self,
count: int,
*,
suffix: Sequence[int] = (),
) -> tuple[Tensor, tuple[int, ...] | None, tuple[tuple[int, ...], ...] | None]:
r"""Build metadata after dropping trailing per-element dims and optionally appending a dense suffix."""
keep_dims = tuple(range(max(self._physical_shape.size(1) - int(count), 0)))
return self._shape_meta_from_components(keep_dims=keep_dims, suffix=suffix)
def _replace_trailing_physical_dims_meta(
self,
trailing_sizes: Sequence[int],
) -> tuple[Tensor, tuple[int, ...] | None, tuple[tuple[int, ...], ...] | None]:
r"""Build metadata after replacing the last physical dims with uniform sizes."""
trailing_sizes = tuple(int(size) for size in trailing_sizes)
if not trailing_sizes:
return self._shape_meta_from_components()
ndim = self._physical_shape.size(1)
if len(trailing_sizes) > ndim:
raise ValueError(f"Cannot replace {len(trailing_sizes)} trailing dims for per-element rank {ndim}")
start = ndim - len(trailing_sizes)
return self._shape_meta_from_components(
replace_dims={start + index: size for index, size in enumerate(trailing_sizes)}
)
def _permutation_after_dropping_trailing_dims(self, count: int) -> tuple[int, ...]:
r"""Return the canonical permutation after dropping trailing physical dims."""
count = int(count)
new_rank = max(self._physical_shape.size(1) - count, 0)
if not self._permutation:
return tuple(range(new_rank))
return tuple(int(dim) for dim in self._permutation if dim < new_rank)
def _permutation_after_replacing_trailing_dims(self, removed_count: int, added_count: int) -> tuple[int, ...]:
r"""Return the canonical permutation after replacing trailing physical dims with a new suffix."""
removed_count = int(removed_count)
added_count = int(added_count)
retained_rank = max(self._physical_shape.size(1) - removed_count, 0)
retained = self._permutation_after_dropping_trailing_dims(removed_count)
appended = tuple(range(retained_rank, retained_rank + added_count))
return retained + appended
def _scalar_result_meta(
self,
) -> tuple[Tensor, Tensor, torch.Size, tuple[int, ...] | None, tuple[tuple[int, ...], ...] | None]:
r"""Build metadata for one-scalar-per-element outputs."""
shape, packed_sizes, element_shapes = self._shape_meta_from_components(keep_dims=())
offsets = torch.arange(len(self) + 1, dtype=self._offsets.dtype, device=self._offsets.device)
logical_shape = type(self)._logical_shape_from_physical_shape(shape, self._offsets, self.batch_first)
return offsets, shape, logical_shape, packed_sizes, element_shapes
def _from_scalar_result_values(self, values: Tensor) -> Self:
r"""Wrap one scalar per element using the canonical scalar-result metadata."""
cls = type(self)
offsets, shape, outer_size, packed_sizes, element_shapes = self._scalar_result_meta()
return cls._from_packed(
values,
offsets,
shape,
batch_first=self.batch_first,
padding_value=self.padding_value,
mask_value=self.mask_value,
pin_memory=self._pin_memory,
outer_size=outer_size,
packed_sizes=packed_sizes,
element_shapes=element_shapes,
validate=False,
)
@classmethod
def _cat_batch_packed(cls, tensors: Sequence[Self]) -> Self | None:
r"""Merge batch-dim concatenation directly from packed storage when layouts are compatible."""
if not tensors:
raise ValueError("Expected at least one NestedTensor to concatenate.")
ref = tensors[0]
packed_rank = ref.concat.dim()
packed_tail = ref.concat.shape[1:]
reference_permutation = ref._permutation
for tensor in tensors[1:]:
if tensor.concat.dim() != packed_rank:
return None
if tensor._permutation != reference_permutation:
return None
if packed_rank > 1 and tensor.concat.shape[1:] != packed_tail:
return None
new_values = torch.cat([tensor.concat for tensor in tensors], dim=0)
# Rebasing each operand's offsets on the running row total is tensor arithmetic; reading
# that total back with ``.item()`` makes the rebase depend on a value, which is what
# stopped batch concatenation from tracing while the slower non-batch path traced fine.
offset_parts = [tensors[0]._offsets]
for tensor in tensors[1:]:
offset_parts.append(tensor._offsets[1:] + offset_parts[-1][-1])
new_offsets = torch.cat(offset_parts, dim=0)
max_cols = max(tensor._physical_shape.size(1) for tensor in tensors)
if max_cols > 0:
padded_shapes = []
for tensor in tensors:
physical_shape = tensor._physical_shape
if physical_shape.size(1) < max_cols:
physical_shape = torch.nn.functional.pad(physical_shape, (0, max_cols - physical_shape.size(1)))
padded_shapes.append(physical_shape)
new_physical_shape = torch.cat(padded_shapes, dim=0)
else:
new_physical_shape = torch.empty(len(new_offsets) - 1, 0, dtype=torch.long)
batch_dim = 0 if ref.batch_first else 1
out_logical = list(ref._logical_shape)
if len(out_logical) <= batch_dim:
out_logical.extend(0 for _ in range(batch_dim + 1 - len(out_logical)))
out_logical[batch_dim] = sum(len(tensor) for tensor in tensors)
for logical_dim in range(len(out_logical)):
if logical_dim == batch_dim:
continue
out_logical[logical_dim] = max(
int(tensor._logical_shape[logical_dim]) if logical_dim < len(tensor._logical_shape) else 0
for tensor in tensors
)
packed_sizes = None
if all(tensor._packed_sizes is not None for tensor in tensors):
packed_sizes = tuple(size for tensor in tensors for size in cast(tuple[int, ...], tensor._packed_sizes))
element_shapes = None
if all(tensor._element_shapes is not None for tensor in tensors):
element_shapes = tuple(
shape for tensor in tensors for shape in cast(tuple[tuple[int, ...], ...], tensor._element_shapes)
)
return cls._from_packed(
new_values,
new_offsets,
new_physical_shape,
permutation=reference_permutation,
ragged_dims=ref._ragged_dims if ref._ragged_dims_explicit else None,
batch_first=ref.batch_first,
padding_value=ref.padding_value,
mask_value=ref.mask_value,
pin_memory=ref._pin_memory,
outer_size=tuple(out_logical),
packed_sizes=packed_sizes,
element_shapes=element_shapes,
validate=False,
)
@property
def _storage(self) -> tuple[Tensor, ...]:
if not self._can_cache_materialized_views():
return self._unpack()
cached = self._cached_storage
if cached is None or self._storage_cache_dropped_grad(cached):
cached = self._unpack()
self._cached_storage = cached
return cached
@_storage.setter
def _storage(self, tensors: Sequence) -> None:
self._repack(tensors)
def _storage_cache_dropped_grad(self, cached: tuple[Tensor, ...]) -> bool:
r"""
Report whether a cached unpack predates the autograd graph now on ``concat``.
``_cached_storage`` keeps whatever the first access produced, so a cache filled
under ``no_grad`` (or below the autograd layer inside ``__torch_dispatch__``) holds
detached views. Serving those later would silently cut the graph for every consumer
that reads elements instead of ``concat``.
"""
return torch.is_grad_enabled() and bool(cached) and self.concat.requires_grad and cached[0].grad_fn is None
def _can_cache_materialized_views(self) -> bool:
r"""Avoid retaining a differentiable view whose graph saves this wrapper."""
if not torch.is_grad_enabled():
return True
return not ((vars(self).get("_unflattened_autograd", False) and self.requires_grad) or self.grad_fn is not None)
# ------------------------------------------------------------------
# Cached materialized views
# ------------------------------------------------------------------
def _tensor_cached_view(self) -> Tensor:
cacheable = self._can_cache_materialized_views()
cached = self._cached_tensor_view if cacheable else None
token = self._values_cache_token() if cacheable else ()
if (
cached is not None
and cached[0] is self.batch_first
and cached[1] == self.padding_value
and cached[2] == token
and not self._storage_cache_dropped_grad((cached[3],))
):
return cached[3]
batch_leading = self._materialize_batch_leading(self.padding_value)
tensor = batch_leading if self.batch_first else batch_leading.movedim(0, 1)
if cacheable:
self._cached_tensor_view = (self.batch_first, self.padding_value, token, tensor)
return tensor
def _mask_cached_view(self) -> Tensor:
cached = self._cached_mask_view
token = self._shape_cache_token()
if cached is not None and cached[0] is self.batch_first and cached[1] is self.mask_value and cached[2] == token:
return cached[3]
mask = self._materialize_mask()
self._cached_mask_view = (self.batch_first, self.mask_value, token, mask)
return mask
@property
def tensor_mask(self) -> tuple[Tensor, Tensor]:
r"""
Return a tuple of padded tensor and mask tensor.
Examples:
>>> nested_tensor = NestedTensor([torch.tensor([1, 2, 3]), torch.tensor([4, 5])])
>>> nested_tensor.tensor_mask
(tensor([[1, 2, 3],
[4, 5, 0]]), tensor([[ True, True, True],
[ True, True, False]]))
"""
return self._tensor_cached_view(), self._mask_cached_view()
@property
def tensor(self) -> Tensor:
r"""
Return a single tensor by padding all the tensors.
Examples:
>>> nested_tensor = NestedTensor([torch.tensor([1, 2, 3]), torch.tensor([4, 5])])
>>> nested_tensor.tensor
tensor([[1, 2, 3],
[4, 5, 0]])
"""
return self._tensor_cached_view()
@property
def mask(self) -> Tensor:
r"""
Padding mask of `tensor`.
`mask_value` controls which boolean value denotes padding in this mask.
With the default `mask_value=False`, `True` means valid data.
Examples:
>>> nested_tensor = NestedTensor([torch.tensor([1, 2, 3]), torch.tensor([4, 5])])
>>> nested_tensor.mask
tensor([[ True, True, True],
[ True, True, False]])
"""
return self._mask_cached_view()
def _mask_squeezes_channel(self) -> bool:
return self._physical_shape.size(1) > 1 and (self._physical_shape.size(1) - 1) in self._static_dims
def _materialize_mask(self) -> Tensor:
batch_size = len(self)
logical_shape = self._logical_shape
squeeze_channel = self._mask_squeezes_channel()
if batch_size == 0:
mask_shape = logical_shape[:-1] if squeeze_channel else logical_shape
return torch.empty(mask_shape, dtype=torch.bool, device=self.device)
if self._physical_shape.size(1) == 0:
return torch.full((batch_size,), not self.mask_value, dtype=torch.bool, device=self.device)
effective_shape = logical_shape[:-1] if squeeze_channel else logical_shape
batch_dim = 0 if self.batch_first else 1
non_batch_sizes = [effective_shape[i] for i in range(len(effective_shape)) if i != batch_dim]
sizes = self._physical_shape[:, :-1] if squeeze_channel else self._physical_shape
sizes = sizes.to(device=self.device, dtype=torch.long)
valid = _batch_leading_valid_mask_from_sizes(
sizes,
non_batch_sizes,
device=self.device,
)
if not self.batch_first:
valid = valid.movedim(0, 1)
return valid if not self.mask_value else ~valid
def _materialize_batch_leading(self, fill_value) -> Tensor:
r"""Materialize a padded dense tensor with the batch dimension in front."""
_check_execution_guard(_ExecutionGuardKind.PADDED_MATERIALIZATION, "NestedTensor._materialize_batch_leading")
logical_shape = self._logical_shape
batch_size = len(self)
if batch_size == 0:
if self.batch_first:
return torch.empty(logical_shape, dtype=self.concat.dtype, device=self.device)
if len(logical_shape) <= 1:
return torch.empty((0,), dtype=self.concat.dtype, device=self.device)
non_batch = list(logical_shape)
non_batch.pop(1)
return torch.empty((0, *non_batch), dtype=self.concat.dtype, device=self.device)
if self._physical_shape.size(1) == 0:
return self.concat.reshape((batch_size,))
tensor_shape = list(logical_shape)
tensor_shape.pop(0 if self.batch_first else 1)
batch_leading = self.concat.new_full((batch_size, *tensor_shape), fill_value)
if self.concat.size(0) > 0:
batch_leading[self._packed_dense_index(device=batch_leading.device)] = self.concat
return batch_leading
def _original_shapes(self) -> tuple[torch.Size, ...]:
if self._element_shapes is not None:
return tuple(torch.Size(shape) for shape in self._element_shapes)
if not _is_fake_tensor(self._physical_shape):
if self._persistent_ragged_offsets() is not None:
# Tensor-backed explicit layouts have a fixed, exact physical
# rank. Their trailing zeros are real dimensions rather than
# padding columns (for example an empty square is ``(0, 0)``).
return tuple(torch.Size(row) for row in self._physical_shape.tolist())
return tuple(torch.Size(type(self)._trim_shape(row)) for row in self._physical_shape.tolist())
raise RuntimeError("NestedTensor shape metadata is unavailable for this instance.")
@property
def concat(self) -> Tensor:
r"""
Flatten elements and concatenate along the ragged dimension (no padding).
This is particularly useful when calculating loss or passing `Linear` to avoid unnecessary computation.
Examples:
>>> nested_tensor = NestedTensor([torch.randn(9, 8), torch.randn(11, 8)])
>>> nested_tensor.concat.shape
torch.Size([20, 8])
>>> nested_tensor = NestedTensor([torch.randn(9, 9, 8), torch.randn(11, 11, 8)])
>>> nested_tensor.concat.shape
torch.Size([202, 8])
>>> nested_tensor = NestedTensor([torch.randn(9, 9, 8, 6), torch.randn(11, 11, 8, 6)])
>>> nested_tensor.concat.shape
torch.Size([202, 8, 6])
>>> nested_tensor = NestedTensor([torch.randn(9, 9, 8, 7), torch.randn(11, 11, 8, 6)])
>>> nested_tensor.concat.shape
torch.Size([1293, 8])
>>> nested_tensor = NestedTensor([torch.randn(1, 9, 9, 5), torch.randn(1, 11, 11, 5)])
>>> nested_tensor.concat.shape
torch.Size([202, 1, 5])
"""
values = self._packed_values
if not torch.is_grad_enabled():
return values
# Attribute reads on the wrapper itself would each re-enter ``__torch_function__``.
with torch._C.DisableTorchFunctionSubclass():
grad_fn = self.grad_fn
wrapper_version = self._cache_version(self)
if vars(self).get("_unflattened_autograd", False):
needs_projection = self.requires_grad
else:
# Saved-tensor hooks can restore this wrapper's autograd edge while
# leaving its packed child detached. Ownership must remain the same
# on the original forward and checkpoint recomputation.
needs_projection = grad_fn is not None
if not needs_projection:
return values
if (
type(values) is Tensor
and values.requires_grad
and getattr(grad_fn, "_forward_cls", None) is _PackedLikeAutograd
and wrapper_version == 0
and torch._C._autograd._top_saved_tensors_default_hooks(True) is None
):
# The common eager case of the packed-factory rule below. A plain dense child that
# is fake or functionally wrapped is also answered with itself, so none of the
# tracing checks below can change the result.
return values
from torch._subclasses.functional_tensor import FunctionalTensor
from torch.utils._python_dispatch import _detect_infra_mode
# Both wrapper-owned autograd paths are also inspected as metadata by
# FakeTensor conversion and AOT's functional-tensor unwrapping. Those
# reads must expose the child without recording any tensor operation.
if isinstance(values, FunctionalTensor):
if _detect_infra_mode(torch._C._TorchDispatchModeKey.FUNCTIONAL) is None:
return values
elif _is_fake_tensor(values) or torch._C._get_dispatch_mode(torch._C._TorchDispatchModeKey.FAKE) is not None:
return values
# Dynamo's alias guards require a stable child object. A nonleaf
# projection can be cached because its backward saves a detached layout,
# not this owner. Leaf projections would retain their owner through
# AccumulateGrad, and functional tracing must build its own graph.
# Saved-tensor hooks must observe each projection on both the original
# forward and recomputation, so never reuse a graph across their scope.
cacheable = (
grad_fn is not None
and not isinstance(values, FunctionalTensor)
and torch._C._autograd._top_saved_tensors_default_hooks(True) is None
)
# A packed factory transparently wraps an existing dense autograd edge,
# which can be reused as an input between fresh forward/backward steps.
# Layout transforms and inplace updates can change the wrapper's edge
# without changing the child's graph, so they still need projection.
# Hook scopes always project the stable wrapper edge on both forward
# and checkpoint recomputation, even when the child was detached.
if (
cacheable
and values.requires_grad
and wrapper_version == 0
and getattr(grad_fn, "_forward_cls", None) is _PackedLikeAutograd
):
return values
token = (
(id(values), wrapper_version, self._cache_version(values), *self._shape_cache_token()) if cacheable else ()
)
cached = vars(self).get("_cached_packed_projection") if cacheable else None
if cached is not None and cached[0] == token:
return cached[1]
projected = _project_packed_values(self)
if cacheable:
self._cached_packed_projection = (token, projected)
return projected
@concat.setter
def concat(self, values: Tensor) -> None:
# AOT's memory-format coercion updates the single packed tangent read
# by backward, while ordinary user assignment remains prohibited.
if not vars(self).get("_is_aot_tangent", False):
raise AttributeError("NestedTensor.concat is read-only")
if (
not isinstance(values, Tensor)
or isinstance(values, NestedTensor)
or values.shape != self._packed_values.shape
or values.dtype != self._packed_values.dtype
or values.device != self._packed_values.device
or values.requires_grad
or values.grad_fn is not None
):
raise AttributeError("Invalid AOT tangent projection for NestedTensor.concat")
self._packed_values = values
self._cached_packed_projection = None
@property
def packed_dim_order(self) -> tuple[int, ...]:
r"""Logical element dimensions in physical packed-storage order.
The tuple is a read-only structural descriptor. Identity order means
packed storage follows the element's logical dimension order; operations
that permute logical dimensions may retain the same packed values while
changing this mapping.
"""
return self._permutation
@property
def ragged_dims(self) -> tuple[int, ...]:
r"""Logical element dimensions represented by packed ragged levels.
The order is stable when ``ragged_dims`` was declared at construction,
even when all elements in a particular batch happen to have equal sizes.
"""
return self._ragged_dims
def concatenate(self) -> tuple[Tensor, tuple[torch.Size, ...]]:
r"""
Concatenate tensors in padding dimension and return structural information for reconstruction.
Returns:
A tuple containing:
- concat_tensor: The concatenated tensor (same as .concat property)
- shapes: Tuple of original tensor shapes for reconstruction
Examples:
>>> nested_tensor = NestedTensor([torch.randn(9, 8), torch.randn(11, 8)])
>>> concat_tensor, shapes = nested_tensor.concatenate()
>>> concat_tensor.shape
torch.Size([20, 8])
>>> shapes
(torch.Size([9, 8]), torch.Size([11, 8]))
>>> reconstructed = NestedTensor.from_concatenated(concat_tensor, shapes)
>>> torch.equal(nested_tensor.tensor, reconstructed.tensor)
True
"""
batch_size = len(self._offsets) - 1
if batch_size == 0:
return torch.empty(0, dtype=self.concat.dtype, device=self.device), ()
return self.concat, self._original_shapes()
# ------------------------------------------------------------------
# Container protocol
# ------------------------------------------------------------------
def __len__(self) -> int:
r"""Return the number of tensors in the batch."""
if not hasattr(self, "_offsets"):
with torch._C.DisableTorchFunctionSubclass():
full_size = torch.Tensor.size(self)
if len(full_size) == 0:
return 0
batch_dim = 0 if getattr(self, "batch_first", True) else (1 if len(full_size) > 1 else 0)
return int(full_size[batch_dim])
return len(self._offsets) - 1
def __repr__(self):
r"""Return a human-readable string representation of the NestedTensor."""
if torch._dynamo.is_compiling():
try:
shape = tuple(self.size())
except Exception:
shape = "?"
return (
f"{self.__class__.__name__}(shape={shape}, dtype={self.dtype}, "
f"device={self.device}, batch_first={getattr(self, 'batch_first', True)})"
)
try:
from torch._subclasses.fake_tensor import is_fake
for name in ("_packed_values", "_offsets", "_physical_shape"):
value = self.__dict__.get(name)
if isinstance(value, Tensor) and is_fake(value):
shape = tuple(self.size())
return (
f"{self.__class__.__name__}(shape={shape}, dtype={self.dtype}, "
f"device={self.device}, batch_first={getattr(self, 'batch_first', True)})"
)
except Exception:
pass
if not all(name in self.__dict__ for name in ("_packed_values", "_offsets", "_physical_shape")):
try:
shape = tuple(self.size())
except Exception:
shape = "?"
return (
f"{self.__class__.__name__}(shape={shape}, dtype={self.dtype}, "
f"device={self.device}, batch_first={getattr(self, 'batch_first', True)})"
)
if len(self) == 0:
return self.__class__.__name__ + "()"
storage = self._storage
truncated = len(storage) > 10
if truncated:
storage = storage[:5]
indent = " "
# Strip "tensor(" wrapper from each element's repr,
# keeping PyTorch's internal number formatting (precision, alignment).
data_parts = []
for t in storage:
s = repr(t)
paren_idx = s.index("(")
data = s[paren_idx + 1 : -1] # noqa: E203
# Re-indent continuation lines for multi-line element reprs (e.g. 2D tensors)
if "\n" in data:
lines = data.split("\n")
data = lines[0] + "\n" + "\n".join(indent + " " + line.lstrip() for line in lines[1:])
data_parts.append(data)
result_lines = [self.__class__.__name__ + "(["]
for i, part in enumerate(data_parts):
suffix = "," if i < len(data_parts) - 1 or truncated else ""
result_lines.append(indent + part + suffix)
if truncated:
result_lines.append(indent + f"... ({len(self)} tensors)")
result_lines.append("])")
return "\n".join(result_lines)
def __bool__(self) -> bool:
r"""NestedTensor follows tensor-style truthiness and never acts like a Python container."""
raise RuntimeError(
"Boolean value of NestedTensor is ambiguous. Use .numel(), .any(), .all(), or an explicit reduction."
)
def __iter__(self):
r"""Iterate over the tensors in the batch."""
_check_execution_guard(_ExecutionGuardKind.ITERATION, "NestedTensor.__iter__")
return iter(self._storage)
@staticmethod
def _operator_result(op, *args):
try:
return op(*args)
except TypeError:
return NotImplemented
def __add__(self, other):
return self._operator_result(torch.add, self, other)
def __radd__(self, other):
return self._operator_result(torch.add, other, self)
def __sub__(self, other):
return self._operator_result(torch.sub, self, other)
def __rsub__(self, other):
return self._operator_result(torch.sub, other, self)
def __mul__(self, other):
return self._operator_result(torch.mul, self, other)
def __rmul__(self, other):
return self._operator_result(torch.mul, other, self)
def __truediv__(self, other):
return self._operator_result(torch.true_divide, self, other)
def __rtruediv__(self, other):
return self._operator_result(torch.true_divide, other, self)
def __floordiv__(self, other):
return self._operator_result(torch.floor_divide, self, other)
def __rfloordiv__(self, other):
return self._operator_result(torch.floor_divide, other, self)
def __mod__(self, other):
return self._operator_result(torch.remainder, self, other)
def __rmod__(self, other):
return self._operator_result(torch.remainder, other, self)
def __pow__(self, other):
return self._operator_result(torch.pow, self, other)
def __rpow__(self, other):
return self._operator_result(torch.pow, other, self)
def __matmul__(self, other):
return self._operator_result(torch.matmul, self, other)
def __rmatmul__(self, other):
return self._operator_result(torch.matmul, other, self)
def __neg__(self):
return self._operator_result(torch.neg, self)
def __abs__(self):
return self._operator_result(torch.abs, self)
def __eq__(self, other): # type: ignore[override]
r"""Element-wise equality comparison."""
try:
return torch.eq(self, other)
except TypeError:
return NotImplemented
def __ne__(self, other): # type: ignore[override]
r"""Element-wise inequality comparison."""
try:
return torch.ne(self, other)
except TypeError:
return NotImplemented
# Python sets __hash__ = None when __eq__ is overridden in a subclass.
# Preserve Tensor's identity hash so AOT/torch.compile memoization works.
__hash__ = Tensor.__hash__
# ------------------------------------------------------------------
# Conversion & Factory Methods
# ------------------------------------------------------------------
@classmethod
def from_concatenated(cls, concat_tensor: Tensor, shapes: tuple[torch.Size, ...], **kwargs) -> Self:
r"""
Reconstruct a NestedTensor from a concatenated tensor and shape information.
Args:
concat_tensor: The concatenated tensor returned by concatenate()
shapes: Tuple of original tensor shapes returned by concatenate()
**kwargs (object): Keyword options forwarded to ``NestedTensor.__new__``,
including dtype, device, gradient, pinning, ragged-dimension and padding options.
Returns:
Reconstructed NestedTensor
Examples:
>>> nested_tensor = NestedTensor([torch.randn(9, 9, 8), torch.randn(11, 11, 8)])
>>> concat_tensor, shapes = nested_tensor.concatenate()
>>> reconstructed = NestedTensor.from_concatenated(concat_tensor, shapes)
>>> concat_tensor.shape
torch.Size([202, 8])
>>> reconstructed.shape
torch.Size([2, 11, 11, 8])
>>> torch.equal(nested_tensor.tensor, reconstructed.tensor)
True
"""
if not shapes:
if "dtype" not in kwargs:
kwargs["dtype"] = concat_tensor.dtype
if "device" not in kwargs:
kwargs["device"] = concat_tensor.device
return cls([], **kwargs)
num_elements = [shape.numel() for shape in shapes]
element_shapes = tuple(tuple(int(dim) for dim in shape) for shape in shapes)
declared_ragged_dims = kwargs.get("ragged_dims")
if declared_ragged_dims is None:
varying_dims, static_dims = cls._pack_layout_from_element_shapes(element_shapes)
else:
varying_dims, static_dims = cls._pack_layout_from_declared_ragged_dims(element_shapes, declared_ragged_dims)
permutation = varying_dims + static_dims
identity_permutation = tuple(range(len(element_shapes[0]))) if element_shapes and element_shapes[0] else ()
# The common sequence layout is already packed in its final order.
# Retain construction's copy semantics, but avoid slicing it into a
# Python list only to concatenate those views back into the same shape.
# In backward this also avoids one scatter per input sequence.
if (
cls is NestedTensor
and type(concat_tensor) is Tensor
and not _is_compiling()
and set(kwargs) == {"ragged_dims"}
and declared_ragged_dims == (0,)
and all(shape and all(size >= 0 for size in shape) for shape in element_shapes)
and tuple(concat_tensor.shape) == (sum(shape[0] for shape in element_shapes), *element_shapes[0][1:])
and _get_current_dispatch_mode() is None
):
sizes = tuple(shape[0] for shape in element_shapes)
offsets = torch.zeros(len(sizes) + 1, dtype=torch.long)
torch.cumsum(torch.tensor(sizes, dtype=torch.long), dim=0, out=offsets[1:])
shape_tensor = torch.tensor(element_shapes, dtype=torch.long)
values = concat_tensor.clone(memory_format=torch.contiguous_format)
result = cls._from_packed(
values,
offsets,
shape_tensor,
permutation=permutation,
ragged_dims=declared_ragged_dims,
outer_size=torch.Size((len(sizes), max(sizes), *element_shapes[0][1:])),
packed_sizes=sizes,
element_shapes=element_shapes,
ragged_offsets=(offsets,),
)
if torch.is_grad_enabled() and values.requires_grad:
return _PackedLikeAutograd.apply(values, _PackedStructureReference(result, mark_dynamic=True))
return result
if len(set(shapes)) == 1 and permutation == identity_permutation:
uniform_shape = shapes[0]
total_elements = sum(num_elements)
if concat_tensor.numel() == total_elements:
try:
reshaped = concat_tensor.reshape(len(shapes), *uniform_shape)
except (RuntimeError, ValueError):
# The reshape fast path is opportunistic; a normal unpack fallback
# is expected for non-view-compatible inputs.
pass
else:
tensors = [t.reshape(uniform_shape) for t in reshaped.unbind(0)]
return cls(tensors, **kwargs)
packed_sizes = tuple(cls._packed_size_from_shape(shape, varying_dims) for shape in element_shapes)
total_expected = sum(num_elements)
num_provided = concat_tensor.numel()
if num_provided != total_expected:
raise ValueError(
f"Concatenated tensor has {num_provided} elements "
f"but expected {total_expected} based on shapes {shapes}"
)
tensors = []
start = 0
inverse_permutation = cls._inverse_permutation(permutation)
for shape, packed_size in zip(element_shapes, packed_sizes):
end = start + packed_size
chunk = concat_tensor.narrow(0, start, packed_size)
packed_shape = tuple(shape[dim] for dim in varying_dims) + tuple(shape[dim] for dim in static_dims)
tensor_data = chunk.reshape(packed_shape)
if permutation != tuple(range(len(shape))):
tensor_data = tensor_data.permute(inverse_permutation)
tensors.append(tensor_data)
start = end
return cls(tensors, **kwargs)
@classmethod
def from_tensor_mask(cls, tensor: Tensor, mask: Tensor, *, batched: bool = False, **kwargs):
r"""
Build a `NestedTensor` object from a padded `Tensor` and corresponding mask `Tensor`.
Args:
tensor: Padded Tensor.
mask: Tensor Mask.
The mask uses the same convention as ``mask_value``:
padding positions equal ``mask_value`` and valid positions equal ``not mask_value``.
batched: When ``True`` and ``mask.ndim == 1``, treat ``mask`` as a per-batch-element
selector (each ``True`` entry selects a row from ``tensor``) rather than a
contiguous-prefix length indicator.
Examples:
>>> padded_tensor = torch.tensor([[1, 2, 3, 0, 0],
... [4, 5, 0, 0, 0],
... [6, 7, 8, 9, 0]])
>>> mask_tensor = torch.tensor([[1, 1, 1, 0, 0],
... [1, 1, 0, 0, 0],
... [1, 1, 1, 1, 0]])
>>> nested_tensor = NestedTensor.from_tensor_mask(padded_tensor, mask_tensor)
>>> nested_tensor
NestedTensor([
[1, 2, 3],
[4, 5],
[6, 7, 8, 9]
])
"""
mask = mask.to(dtype=torch.bool)
mask_value = kwargs.get("mask_value", False)
effective_mask = ~mask if mask_value else mask
if mask.ndim == 1:
if batched:
indices = effective_mask.nonzero(as_tuple=False).flatten()
return cls([tensor[int(i)] for i in indices], dtype=tensor.dtype, **kwargs)
return cls(tensor[effective_mask], dtype=tensor.dtype, **kwargs)
# ndim >= 2: batch setup is shared, per-element trim differs by rank
batch_first = kwargs.get("batch_first", True)
tensor_iter = tensor if batch_first else tensor.transpose(0, 1)
mask_iter = effective_mask if batch_first else effective_mask.transpose(0, 1)
if tensor_iter.size(0) != mask_iter.size(0):
raise ValueError("Tensor/mask batch dimension mismatch: " f"{tensor_iter.size(0)} vs {mask_iter.size(0)}")
trimmed = []
def _is_prefix_mask(mask_1d: Tensor) -> bool:
count = int(mask_1d.sum().item())
prefix = torch.arange(mask_1d.size(0), device=mask_1d.device, dtype=torch.long) < count
return bool(torch.equal(mask_1d, prefix))
def _is_hierarchical_prefix_mask(mask_nd: Tensor) -> bool:
if mask_nd.dim() == 1:
return _is_prefix_mask(mask_nd)
leading_valid = mask_nd.reshape(mask_nd.size(0), -1).any(dim=1)
valid_count = int(leading_valid.sum().item())
prefix = torch.arange(mask_nd.size(0), device=mask_nd.device, dtype=torch.long) < valid_count
if not torch.equal(leading_valid, prefix):
return False
return all(_is_hierarchical_prefix_mask(mask_nd[index]) for index in range(valid_count))
if mask.ndim == 2:
# 1-D per-element mask: only contiguous-prefix masks can be reconstructed
# via slicing without changing dense semantics.
counts = mask_iter.sum(dim=1, dtype=torch.long)
prefix = torch.arange(mask_iter.size(1), device=mask_iter.device, dtype=torch.long).unsqueeze(0)
prefix = prefix < counts.unsqueeze(1)
if not torch.equal(mask_iter, prefix):
raise ValueError(
"from_tensor_mask() with 2-D masks requires each row to be a valid prefix mask; "
"interior False gaps are not supported."
)
for t, count in zip(tensor_iter, counts.tolist()):
trimmed.append(t[:count])
else:
# N-D per-element mask: only hierarchical ragged-prefix masks are representable as NestedTensor.
extents = torch.zeros((mask_iter.size(0), mask_iter.dim() - 1), dtype=torch.long, device=mask_iter.device)
nonzero = mask_iter.nonzero(as_tuple=False)
if nonzero.numel() > 0:
batch_index = nonzero[:, :1].expand(-1, extents.size(1))
extents.scatter_reduce_(0, batch_index, nonzero[:, 1:] + 1, reduce="amax", include_self=False)
extent_rows = extents.cpu().tolist()
for t, em, sizes in zip(tensor_iter, mask_iter, extent_rows):
if not _is_hierarchical_prefix_mask(em):
raise ValueError(
"from_tensor_mask() with N-D masks requires each element mask to be a valid hierarchical "
"ragged prefix; "
"interior False gaps are not supported."
)
slices = tuple(slice(0, size) for size in sizes)
t_slice = t[slices]
m_slice = em[slices]
valid_mask = m_slice
if t_slice.dim() > m_slice.dim():
valid_mask = m_slice.view(m_slice.shape + (1,) * (t_slice.dim() - m_slice.dim()))
trimmed.append(t_slice.masked_fill(~valid_mask, kwargs.get("padding_value", 0.0)))
return cls(trimmed, dtype=tensor.dtype, **kwargs)
def _dense_to_packed_values(self, tensor: Tensor) -> Tensor | None:
r"""
Convert a batch-aligned dense tensor to ``self``'s packed ``concat`` layout.
Returns ``None`` when the dense tensor does not cover the current logical
padded extents and we must fall back to per-element slicing/repacking.
"""
batch_leading = tensor.to(device=self.device)
if self.dim() > 1 and not self.batch_first:
batch_leading = batch_leading.movedim(1, 0)
logical_shape = list(self.shape)
if logical_shape:
batch_dim = 0 if self.dim() <= 1 or self.batch_first else 1
logical_shape.pop(batch_dim)
if batch_leading.dim() != len(logical_shape) + 1:
return None
dense_sizes = tuple(int(batch_leading.size(dim + 1)) for dim in range(batch_leading.dim() - 1))
if any(dense_sizes[dim] < int(size) for dim, size in enumerate(logical_shape)):
return None
if logical_shape:
batch_leading = batch_leading[(slice(None), *[slice(0, int(size)) for size in logical_shape])]
if batch_leading.dim() <= 1:
return batch_leading.contiguous()
return batch_leading[self._packed_dense_index(device=batch_leading.device)].contiguous()
def _packed_sizes_like(
self,
element_shapes: tuple[tuple[int, ...], ...],
ragged_dims: tuple[int, ...] | None = None,
) -> tuple[int, ...]:
if ragged_dims is None:
varying_dims, _ = type(self)._pack_layout_from_element_shapes(element_shapes)
else:
varying_dims, _ = type(self)._pack_layout_from_declared_ragged_dims(element_shapes, ragged_dims)
return tuple(type(self)._packed_size_from_shape(shape, varying_dims) for shape in element_shapes)
def _packed_like_unchecked_raw(self, packed_values: Tensor) -> Self:
r"""Rebuild directly when the caller already proved shape compatibility."""
# The ordinary eager path already owns a validated, unchanged layout.
# Re-resolving its permutation, ragged axes and persistent offsets for
# every pointwise operation is unnecessary. Keep tracing/subclass modes
# on the general constructor, which performs their metadata conversion.
if (
type(self) is NestedTensor
and type(packed_values) is Tensor
and not _is_compiling()
and _get_current_dispatch_mode() is None
and not (_is_fake_tensor(self._offsets) or _is_fake_tensor(self._physical_shape))
):
values = type(self)._maybe_pin_values(packed_values, self._pin_memory)
result = torch.Tensor._make_wrapper_subclass(
type(self),
self._logical_shape,
dtype=values.dtype,
device=values.device,
requires_grad=values.requires_grad,
)
result._packed_values = values
result._offsets = self._offsets
result._physical_shape = self._physical_shape
result._logical_shape = self._logical_shape
result._permutation = self._permutation
result._ragged_dims = self._ragged_dims
result._ragged_dims_explicit = self._ragged_dims_explicit
result._batch_first = self._batch_first
result._padding_value = self._padding_value
result._mask_value = self._mask_value
result._pin_memory = bool(self._pin_memory and values.device.type == "cpu" and values.is_pinned())
result._packed_sizes = self._packed_sizes
result._element_shapes = self._element_shapes
type(self)._install_persistent_ragged_offsets(result, self._persistent_ragged_offsets())
result._invalidate_transient_caches()
self._share_offset_caches(result)
result._inherit_tensor_backed_dynamic_dims(self)
return result
result = type(self)._from_packed(
packed_values,
self._offsets,
self._physical_shape,
permutation=self._permutation,
ragged_dims=self._ragged_dims if self._ragged_dims_explicit else None,
batch_first=self.batch_first,
padding_value=self.padding_value,
mask_value=self.mask_value,
pin_memory=self._pin_memory,
outer_size=self._logical_shape,
packed_sizes=self._packed_sizes,
element_shapes=self._element_shapes,
ragged_offsets=self._persistent_ragged_offsets(),
validate=False,
)
if (
self._cached_hierarchical_offsets is not None
and result._offsets is self._offsets
and result._physical_shape is self._physical_shape
):
result._cached_hierarchical_offsets = self._cached_hierarchical_offsets
return result
def _packed_like_unchecked(self, packed_values: Tensor, *, reuse_wrapper: bool = False) -> Self:
r"""Rebuild from compatible packed values, preserving compiled autograd edges."""
# A layout-changing handler has just constructed a private wrapper with
# these exact values. Attach its autograd edge in place rather than
# constructing another identical wrapper. Callers must never request
# reuse for an already exposed input or output.
if (
reuse_wrapper
and not _is_compiling()
and type(self) is NestedTensor
and type(packed_values) is Tensor
and packed_values is self._packed_values
and _get_current_dispatch_mode() is None
and not (_is_fake_tensor(self._offsets) or _is_fake_tensor(self._physical_shape))
and torch._C._autograd._top_saved_tensors_default_hooks(True) is None
):
if torch.is_grad_enabled() and packed_values.requires_grad:
return _PackedLikeAutograd.apply(packed_values, _PackedStructureReference(self, reuse_wrapper=True))
return self
if _is_compiling() or (torch.is_grad_enabled() and packed_values.requires_grad):
return _PackedLikeAutograd.apply(packed_values, _PackedStructureReference(self))
return self._packed_like_unchecked_raw(packed_values)
def packed_like(self, packed_values: Tensor) -> Self:
r"""Wrap packed values with this ``NestedTensor``'s structure.
``packed_values`` must have exactly the same shape as :attr:`concat`.
The returned ``NestedTensor`` shares ``packed_values`` directly, so its
dtype, device, strides, pinning, and autograd history all come from the
supplied tensor. Ragged offsets, element shapes, permutation, logical
shape, and runtime configuration are inherited from ``self``.
Args:
packed_values: Dense packed storage for the returned
``NestedTensor``.
Returns:
A ``NestedTensor`` with ``self``'s structure and
``packed_values`` as its packed storage.
Raises:
TypeError: If ``packed_values`` is not a dense ``Tensor``.
ValueError: If its shape differs from ``self.concat.shape``.
Examples:
>>> reference = NestedTensor([torch.randn(2, 3), torch.randn(4, 3)])
>>> values = torch.ones_like(reference.concat)
>>> output = reference.packed_like(values)
>>> output.concat is values
True
>>> output.shape == reference.shape
True
"""
if (
not isinstance(packed_values, Tensor)
or isinstance(packed_values, NestedTensor)
or packed_values.is_nested
or packed_values.layout != torch.strided
):
raise TypeError(
"packed_values must be a dense Tensor with torch.strided layout, "
f"got {type(packed_values).__name__} with layout "
f"{getattr(packed_values, 'layout', None)}"
)
if packed_values.shape != self.concat.shape:
raise ValueError(
"packed_values must have exactly the same shape as the reference packed storage, "
f"got {packed_values.shape} and expected {self.concat.shape}"
)
pin_memory = bool(packed_values.device.type == "cpu" and packed_values.is_pinned())
result = type(self)._from_packed(
packed_values,
self._offsets,
self._physical_shape,
permutation=self._permutation,
ragged_dims=self._ragged_dims if self._ragged_dims_explicit else None,
batch_first=self.batch_first,
padding_value=self.padding_value,
mask_value=self.mask_value,
pin_memory=pin_memory,
outer_size=self._logical_shape,
packed_sizes=self._packed_sizes,
element_shapes=self._element_shapes,
ragged_offsets=self._persistent_ragged_offsets(),
validate=False,
)
if (
self._cached_hierarchical_offsets is not None
and result._offsets is self._offsets
and result._physical_shape is self._physical_shape
):
result._cached_hierarchical_offsets = self._cached_hierarchical_offsets
if torch.is_grad_enabled() and packed_values.requires_grad:
return _PackedLikeAutograd.apply(packed_values, _PackedStructureReference(result, mark_dynamic=True))
return result
def packed_with_static_tail(self, packed_values: Tensor) -> Self:
r"""Wrap packed values after replacing this tensor's static tail.
This operation preserves every declared ragged level and replaces all
static element dimensions with ``packed_values.shape[1:]``. A
canonical reference may change the number of static dimensions. An
explicit tensor-backed reference with one non-leading logical ragged
dimension may instead replace its existing static dimensions in packed
order, but must keep the same static rank. This preserves where those
dimensions appear in the logical element layout. The returned tensor
shares ``packed_values`` directly.
Args:
packed_values: Dense packed storage whose leading dimension equals
``self.concat.shape[0]``. Remaining dimensions become the new
static tail.
Returns:
A ``NestedTensor`` preserving the declared ragged topology and
supported packed layout, with ``packed_values`` as its packed
storage.
Raises:
TypeError: If ``packed_values`` is not a dense strided tensor.
ValueError: If the reference has no ragged dimensions, is neither
canonical nor a supported tensor-backed non-leading layout,
changes the static rank of such a non-leading layout, or has a
mismatched packed leading dimension.
Examples:
>>> atoms = NestedTensor([torch.zeros(2), torch.zeros(4)])
>>> values = torch.randn(6, 3)
>>> output = atoms.packed_with_static_tail(values)
>>> [tuple(element.shape) for element in output]
[(2, 3), (4, 3)]
>>> output.concat is values
True
"""
if (
not isinstance(packed_values, Tensor)
or isinstance(packed_values, NestedTensor)
or packed_values.is_nested
or packed_values.layout != torch.strided
):
raise TypeError(
"packed_values must be a dense Tensor with torch.strided layout, "
f"got {type(packed_values).__name__} with layout "
f"{getattr(packed_values, 'layout', None)}"
)
if packed_values.dim() == 0:
raise ValueError("packed_values must have a leading packed dimension")
ragged_rank = len(self._ragged_dims)
canonical_ragged_dims = tuple(range(ragged_rank))
canonical_order = tuple(range(len(self._permutation)))
if ragged_rank == 0:
raise ValueError("packed_with_static_tail requires at least one ragged dimension")
canonical_layout = self._ragged_dims == canonical_ragged_dims and self._permutation == canonical_order
nonleading_tensor_backed_layout = (
self._ragged_dims_explicit
and ragged_rank == 1
and self._ragged_dims[0] != 0
and self._permutation[0] == self._ragged_dims[0]
and self._persistent_ragged_offsets() is not None
)
if not canonical_layout and not nonleading_tensor_backed_layout:
raise ValueError(
"packed_with_static_tail requires canonical packed order or an explicit tensor-backed "
"single non-leading ragged dimension, "
f"got ragged_dims={self._ragged_dims} and packed_dim_order={self._permutation}"
)
if packed_values.shape[0] != self.concat.shape[0]:
raise ValueError(
"packed_values leading dimension must equal the reference packed length, "
f"got {packed_values.shape[0]} and expected {self.concat.shape[0]}"
)
static_tail = tuple(packed_values.shape[1:])
if canonical_layout:
physical_shape, packed_sizes, element_shapes = self._shape_meta_from_components(
keep_dims=self._ragged_dims,
suffix=static_tail,
)
permutation = tuple(range(ragged_rank + len(static_tail)))
ragged_dims = canonical_ragged_dims
outer_size = self._logical_shape_from_components(
keep_dims=self._ragged_dims,
suffix=static_tail,
)
else:
static_dims = self._static_dims
if len(static_tail) != len(static_dims):
raise ValueError(
"packed_with_static_tail requires a non-leading ragged layout to keep the same "
"number of packed static dimensions, "
f"got {len(static_tail)} and expected {len(static_dims)}"
)
replacements = dict(zip(static_dims, static_tail))
# This is the production packed path: update the fixed-rank tensor metadata
# directly instead of rebuilding one Python shape tuple per sample. Dropping
# those legacy caches also ensures that outputs remain layout-dynamic when an
# eager reference enters a compiled graph.
physical_shape = self._physical_shape
if _is_fake_tensor(packed_values) and not _is_fake_tensor(physical_shape):
from torch._subclasses.fake_tensor import maybe_get_fake_mode
fake_mode = maybe_get_fake_mode(packed_values)
if fake_mode is not None:
physical_shape = fake_mode.from_tensor(physical_shape, static_shapes=True, trace=False)
physical_shape = physical_shape.clone()
for dim, size in replacements.items():
physical_shape[:, dim] = size
packed_sizes = None
element_shapes = None
permutation = self._permutation
ragged_dims = self._ragged_dims
outer_size = self._logical_shape_from_components(replace_dims=replacements)
if len(self) == 0:
packed_sizes = ()
element_shapes = ()
pin_memory = bool(packed_values.device.type == "cpu" and packed_values.is_pinned())
result = type(self)._from_packed(
packed_values,
self._offsets,
physical_shape,
permutation=permutation,
ragged_dims=ragged_dims,
batch_first=self.batch_first,
padding_value=self.padding_value,
mask_value=self.mask_value,
pin_memory=pin_memory,
outer_size=outer_size,
packed_sizes=packed_sizes,
element_shapes=element_shapes,
ragged_offsets=self._persistent_ragged_offsets(),
validate=False,
)
if torch.is_grad_enabled() and packed_values.requires_grad:
return _PackedLikeAutograd.apply(packed_values, _PackedStructureReference(result))
return result
def packed_with_lengths(self, packed_values: Tensor, lengths: Tensor) -> Self:
r"""Wrap packed values with new leading ragged lengths.
``lengths`` defines one canonical leading ragged dimension and
``packed_values.shape[1:]`` defines the static tail. The reference
supplies only the batch size, subclass, and runtime configuration; its
existing ragged lengths are replaced. Both packed values and their
autograd history are shared without copying. Compiled reconstruction
keeps offsets and element shapes as tensor-backed graph data, so fixed
batch sizes do not create per-element Python output metadata.
Args:
packed_values: Dense packed storage with shape
``(sum(lengths), *static_tail)``.
lengths: One-dimensional CPU integer tensor with one non-negative
length per batch element.
Returns:
A canonical one-ragged-dimension ``NestedTensor`` backed directly
by ``packed_values``.
Raises:
TypeError: If either tensor has an unsupported type, dtype, or
layout.
ValueError: If ``lengths`` is not valid one-dimensional CPU
metadata, has the wrong batch size, contains a negative value,
or does not sum to the packed leading dimension.
Examples:
>>> reference = NestedTensor([torch.zeros(4, 2), torch.zeros(6, 2)])
>>> lengths = torch.tensor([2, 3])
>>> values = torch.randn(5, 7)
>>> output = reference.packed_with_lengths(values, lengths)
>>> [tuple(element.shape) for element in output]
[(2, 7), (3, 7)]
>>> output.concat is values
True
"""
if (
not isinstance(packed_values, Tensor)
or isinstance(packed_values, NestedTensor)
or packed_values.is_nested
or packed_values.layout != torch.strided
):
raise TypeError(
"packed_values must be a dense Tensor with torch.strided layout, "
f"got {type(packed_values).__name__} with layout "
f"{getattr(packed_values, 'layout', None)}"
)
if packed_values.dim() == 0:
raise ValueError("packed_values must have a leading packed dimension")
if (
not isinstance(lengths, Tensor)
or isinstance(lengths, NestedTensor)
or lengths.is_nested
or lengths.layout != torch.strided
):
raise TypeError(
"lengths must be a dense Tensor with torch.strided layout, "
f"got {type(lengths).__name__} with layout {getattr(lengths, 'layout', None)}"
)
if lengths.device.type != "cpu":
raise ValueError(f"lengths must be on CPU, got {lengths.device}")
if lengths.dtype.is_floating_point or lengths.dtype.is_complex or lengths.dtype == torch.bool:
raise TypeError(f"lengths must use an integer dtype, got {lengths.dtype}")
if lengths.dim() != 1:
raise ValueError(f"lengths must be one-dimensional, got shape {tuple(lengths.shape)}")
if lengths.numel() != len(self):
raise ValueError(
"lengths must contain one value per batch element, "
f"got {lengths.numel()} values for batch size {len(self)}"
)
symbolic_lengths = _is_fake_tensor(lengths)
if symbolic_lengths:
if not _is_fake_tensor(packed_values):
raise ValueError(
"FakeTensor lengths require FakeTensor packed_values; "
"pass concrete CPU lengths for standalone FakeTensor reconstruction"
)
torch._assert_async(torch.all(lengths >= 0), "lengths must be non-negative")
torch._assert_async(
lengths.sum() == packed_values.shape[0],
"lengths must sum to the packed values leading dimension",
)
else:
if bool(torch.any(lengths < 0)):
raise ValueError("lengths must be non-negative")
packed_length = int(lengths.sum().item())
if packed_length != packed_values.shape[0]:
raise ValueError(
"lengths must sum to the packed values leading dimension, "
f"got sum {packed_length} and packed length {packed_values.shape[0]}"
)
static_tail = tuple(packed_values.shape[1:])
physical_rank = 1 + len(static_tail)
if lengths.numel():
tail_shape = lengths.new_empty((lengths.numel(), len(static_tail)), dtype=torch.long)
for dim, size in enumerate(static_tail):
tail_shape[:, dim] = size
physical_shape = torch.cat((lengths.to(dtype=torch.long).reshape(-1, 1), tail_shape), dim=1)
else:
physical_shape = lengths.new_empty((0, physical_rank), dtype=torch.long)
offsets = torch.nn.functional.pad(lengths.to(dtype=torch.long).cumsum(0), (1, 0))
max_length = cast(int, lengths.max().item()) if lengths.numel() else 0
if self.batch_first:
logical_shape = torch.Size((len(self), max_length, *static_tail))
else:
logical_shape = torch.Size((max_length, len(self), *static_tail))
pin_memory = bool(packed_values.device.type == "cpu" and packed_values.is_pinned())
result = type(self)._from_packed(
packed_values,
offsets,
physical_shape,
permutation=tuple(range(physical_rank)),
ragged_dims=(0,),
batch_first=self.batch_first,
padding_value=self.padding_value,
mask_value=self.mask_value,
pin_memory=pin_memory,
outer_size=logical_shape,
packed_sizes=None,
element_shapes=None,
validate=False,
materialize_python_metadata=False,
)
if symbolic_lengths:
result._max_length_binding = result._offsets.new_empty(()).expand(max_length)
if torch.is_grad_enabled() and packed_values.requires_grad:
return _PackedLikeAutograd.apply(packed_values, _PackedStructureReference(result))
return result
def packed_with_square_lengths(self, packed_values: Tensor, lengths: Tensor) -> Self:
r"""Wrap packed values as canonical square ragged elements.
``lengths`` defines two canonical ragged dimensions with equal sizes,
so batch element ``i`` has shape
``(lengths[i], lengths[i], *packed_values.shape[1:])``. The reference
supplies only the batch size, subclass, and runtime configuration; its
existing topology is not retained. The returned tensor shares
``packed_values`` and its autograd history directly.
Args:
packed_values: Dense packed storage with shape
``(sum(lengths.square()), *static_tail)``.
lengths: One-dimensional CPU integer tensor with one non-negative
length per batch element.
Returns:
A canonical two-ragged-dimension ``NestedTensor`` backed directly
by ``packed_values``.
Raises:
TypeError: If either tensor has an unsupported type, dtype, or
layout.
ValueError: If ``lengths`` is not valid one-dimensional CPU
metadata, has the wrong batch size, contains a negative value,
its squared sizes or their cumulative sum exceed the int64
metadata range, or they do not sum to the packed leading
dimension.
Examples:
>>> reference = NestedTensor([torch.zeros(1), torch.zeros(1)])
>>> lengths = torch.tensor([2, 3])
>>> values = torch.randn(13, 4)
>>> output = reference.packed_with_square_lengths(values, lengths)
>>> [tuple(element.shape) for element in output]
[(2, 2, 4), (3, 3, 4)]
>>> output.concat is values
True
"""
if (
not isinstance(packed_values, Tensor)
or isinstance(packed_values, NestedTensor)
or packed_values.is_nested
or packed_values.layout != torch.strided
):
raise TypeError(
"packed_values must be a dense Tensor with torch.strided layout, "
f"got {type(packed_values).__name__} with layout "
f"{getattr(packed_values, 'layout', None)}"
)
if packed_values.dim() == 0:
raise ValueError("packed_values must have a leading packed dimension")
if (
not isinstance(lengths, Tensor)
or isinstance(lengths, NestedTensor)
or lengths.is_nested
or lengths.layout != torch.strided
):
raise TypeError(
"lengths must be a dense Tensor with torch.strided layout, "
f"got {type(lengths).__name__} with layout {getattr(lengths, 'layout', None)}"
)
if lengths.device.type != "cpu":
raise ValueError(f"lengths must be on CPU, got {lengths.device}")
if lengths.dtype.is_floating_point or lengths.dtype.is_complex or lengths.dtype == torch.bool:
raise TypeError(f"lengths must use an integer dtype, got {lengths.dtype}")
if lengths.dim() != 1:
raise ValueError(f"lengths must be one-dimensional, got shape {tuple(lengths.shape)}")
if lengths.numel() != len(self):
raise ValueError(
"lengths must contain one value per batch element, "
f"got {lengths.numel()} values for batch size {len(self)}"
)
long_lengths = lengths.to(dtype=torch.long)
symbolic_lengths = _is_fake_tensor(lengths)
if symbolic_lengths:
if not _is_fake_tensor(packed_values):
raise ValueError(
"FakeTensor lengths require FakeTensor packed_values; "
"pass concrete CPU lengths for standalone FakeTensor reconstruction"
)
torch._assert_async(torch.all(long_lengths >= 0), "lengths must be non-negative")
torch._assert_async(
torch.all(long_lengths <= _INT64_SQUARE_ROOT_MAX),
"each squared length must fit in torch.int64",
)
else:
_validate_concrete_square_lengths(long_lengths)
packed_sizes = long_lengths.square()
packed_prefix = packed_sizes.cumsum(0)
if symbolic_lengths:
torch._assert_async(
torch.all(packed_prefix >= 0),
"the cumulative sum of squared lengths must fit in torch.int64",
)
torch._assert_async(
packed_sizes.sum() == packed_values.shape[0],
"squared lengths must sum to the packed values leading dimension",
)
else:
packed_length = int(packed_sizes.sum().item())
if packed_length != packed_values.shape[0]:
raise ValueError(
"squared lengths must sum to the packed values leading dimension, "
f"got sum {packed_length} and packed length {packed_values.shape[0]}"
)
static_tail = tuple(packed_values.shape[1:])
physical_rank = 2 + len(static_tail)
if lengths.numel():
square_shape = long_lengths.reshape(-1, 1).expand(-1, 2)
tail_shape = long_lengths.new_empty((lengths.numel(), len(static_tail)))
for dim, size in enumerate(static_tail):
tail_shape[:, dim] = size
physical_shape = torch.cat((square_shape, tail_shape), dim=1)
else:
physical_shape = long_lengths.new_empty((0, physical_rank))
outer_offsets = torch.nn.functional.pad(packed_prefix, (1, 0))
level_zero_offsets = torch.nn.functional.pad(long_lengths.cumsum(0), (1, 0))
level_one_offsets = _square_row_splits(long_lengths)
max_length = cast(int, long_lengths.max().item()) if lengths.numel() else 0
if self.batch_first:
logical_shape = torch.Size((len(self), max_length, max_length, *static_tail))
else:
logical_shape = torch.Size((max_length, len(self), max_length, *static_tail))
pin_memory = bool(packed_values.device.type == "cpu" and packed_values.is_pinned())
# Both ragged axes and their offsets already carry the dynamic shape contract. Marking
# caller-owned packed storage here would mutate a compiled graph input after Dynamo has
# installed its guards, forcing an otherwise unnecessary second compilation.
result = type(self)._from_packed(
packed_values,
outer_offsets,
physical_shape,
permutation=tuple(range(physical_rank)),
ragged_dims=(0, 1),
batch_first=self.batch_first,
padding_value=self.padding_value,
mask_value=self.mask_value,
pin_memory=pin_memory,
outer_size=logical_shape,
packed_sizes=None,
element_shapes=None,
ragged_offsets=(level_zero_offsets, level_one_offsets),
validate=False,
materialize_python_metadata=False,
mark_values_dynamic=False,
)
if symbolic_lengths:
result._max_length_binding = result._offsets.new_empty(()).expand(max_length)
if torch.is_grad_enabled() and packed_values.requires_grad:
return _PackedLikeAutograd.apply(packed_values, _PackedStructureReference(result))
return result
def packed_with_rectangular_lengths(
self,
packed_values: Tensor,
row_lengths: Tensor,
column_lengths: Tensor,
) -> Self:
r"""Wrap packed values as canonical rectangular ragged elements.
Batch element ``i`` has shape
``(row_lengths[i], column_lengths[i], *packed_values.shape[1:])``.
Both ragged axes and their hierarchical row splits remain tensor-backed,
so a fixed outer batch can reuse one dynamic fullgraph across different
rectangular layouts. The returned tensor shares ``packed_values`` and
its autograd history directly.
Args:
packed_values: Dense storage with leading length
``sum(row_lengths * column_lengths)``.
row_lengths: One-dimensional CPU integer row counts.
column_lengths: One-dimensional CPU integer column counts.
Returns:
A canonical two-ragged-dimension ``NestedTensor``.
Examples:
>>> reference = NestedTensor([torch.zeros(1), torch.zeros(1)])
>>> rows, columns = torch.tensor([2, 3]), torch.tensor([4, 1])
>>> values = torch.randn(11, 5)
>>> output = reference.packed_with_rectangular_lengths(values, rows, columns)
>>> [tuple(element.shape) for element in output]
[(2, 4, 5), (3, 1, 5)]
>>> output.concat is values
True
"""
if (
not isinstance(packed_values, Tensor)
or isinstance(packed_values, NestedTensor)
or packed_values.is_nested
or packed_values.layout != torch.strided
):
raise TypeError(
"packed_values must be a dense Tensor with torch.strided layout, "
f"got {type(packed_values).__name__} with layout "
f"{getattr(packed_values, 'layout', None)}"
)
if packed_values.dim() == 0:
raise ValueError("packed_values must have a leading packed dimension")
batch_size = self._physical_shape.shape[0]
for name, lengths in (("row_lengths", row_lengths), ("column_lengths", column_lengths)):
if (
not isinstance(lengths, Tensor)
or isinstance(lengths, NestedTensor)
or lengths.is_nested
or lengths.layout != torch.strided
):
raise TypeError(
f"{name} must be a dense Tensor with torch.strided layout, "
f"got {type(lengths).__name__} with layout {getattr(lengths, 'layout', None)}"
)
if lengths.device.type != "cpu":
raise ValueError(f"{name} must be on CPU, got {lengths.device}")
if lengths.dtype.is_floating_point or lengths.dtype.is_complex or lengths.dtype == torch.bool:
raise TypeError(f"{name} must use an integer dtype, got {lengths.dtype}")
if lengths.dim() != 1:
raise ValueError(f"{name} must be one-dimensional, got shape {tuple(lengths.shape)}")
if lengths.numel() != batch_size:
raise ValueError(
f"{name} must contain one value per batch element, "
f"got {lengths.numel()} values for batch size {batch_size}"
)
long_rows = row_lengths.to(dtype=torch.long)
long_columns = column_lengths.to(dtype=torch.long)
packed_sizes = long_rows * long_columns
symbolic_lengths = _is_fake_tensor(row_lengths) or _is_fake_tensor(column_lengths)
if symbolic_lengths:
if not (
_is_fake_tensor(row_lengths) and _is_fake_tensor(column_lengths) and _is_fake_tensor(packed_values)
):
raise ValueError(
"FakeTensor rectangular lengths require FakeTensor packed_values and matching FakeTensor metadata"
)
torch._assert_async(torch.all(long_rows >= 0), "row_lengths must be non-negative")
torch._assert_async(torch.all(long_columns >= 0), "column_lengths must be non-negative")
torch._assert_async(
packed_sizes.sum() == packed_values.shape[0],
"rectangular lengths must cover the packed values leading dimension",
)
else:
if bool(torch.any(long_rows < 0)):
raise ValueError("row_lengths must be non-negative")
if bool(torch.any(long_columns < 0)):
raise ValueError("column_lengths must be non-negative")
packed_length = int(packed_sizes.sum().item())
if packed_length != packed_values.shape[0]:
raise ValueError(
"row_lengths * column_lengths must sum to the packed values leading dimension, "
f"got sum {packed_length} and packed length {packed_values.shape[0]}"
)
static_tail = tuple(packed_values.shape[1:])
physical_rank = 2 + len(static_tail)
if row_lengths.numel():
rectangular_shape = torch.stack((long_rows, long_columns), dim=1)
tail_shape = long_rows.new_empty((row_lengths.numel(), len(static_tail)))
for dim, size in enumerate(static_tail):
tail_shape[:, dim] = size
physical_shape = torch.cat((rectangular_shape, tail_shape), dim=1)
else:
physical_shape = long_rows.new_empty((0, physical_rank))
outer_offsets = torch.nn.functional.pad(packed_sizes.cumsum(0), (1, 0))
level_zero_offsets = torch.nn.functional.pad(long_rows.cumsum(0), (1, 0))
level_one_offsets = _rectangular_row_splits(long_rows, long_columns)
max_rows = cast(int, long_rows.max().item()) if row_lengths.numel() else 0
max_columns = cast(int, long_columns.max().item()) if column_lengths.numel() else 0
if self.batch_first:
logical_shape = torch.Size((batch_size, max_rows, max_columns, *static_tail))
else:
logical_shape = torch.Size((max_rows, batch_size, max_columns, *static_tail))
pin_memory = bool(packed_values.device.type == "cpu" and packed_values.is_pinned())
result = type(self)._from_packed(
packed_values,
outer_offsets,
physical_shape,
permutation=tuple(range(physical_rank)),
ragged_dims=(0, 1),
batch_first=self.batch_first,
padding_value=self.padding_value,
mask_value=self.mask_value,
pin_memory=pin_memory,
outer_size=logical_shape,
packed_sizes=None,
element_shapes=None,
ragged_offsets=(level_zero_offsets, level_one_offsets),
validate=False,
materialize_python_metadata=False,
)
# The generic fallback binds only physical dim 0. Rectangular outputs
# carry two independent data-derived maxima, so expose a zero-stride
# two-dimensional child for both eager and Fake/compiled rebuilds.
result._max_length_binding = result._offsets.new_empty(()).expand(max_rows, max_columns)
if torch.is_grad_enabled() and packed_values.requires_grad:
return _PackedLikeAutograd.apply(packed_values, _PackedStructureReference(result))
return result
def nested_like(self, tensor: Tensor, strict: bool = True) -> NestedTensor:
r"""
Create a new `NestedTensor` from a `Tensor`.
The newly created `NestedTensor` will have the same shape as current `NestedTensor`.
Args:
tensor: The tensor to be converted to `NestedTensor`.
strict: Check if the shape of `tensor` is the same as the current `NestedTensor`.
Examples:
>>> nested_tensor = NestedTensor([torch.tensor([1, 2, 3]), torch.tensor([4, 5])])
>>> (nested_tensor == nested_tensor.nested_like(nested_tensor)).all()
tensor(True)
>>> tensor = nested_tensor.tensor
>>> (nested_tensor == nested_tensor.nested_like(tensor)).all()
tensor(True)
>>> f = nested_tensor.nested_like(torch.randn(2, 2))
Traceback (most recent call last):
...
ValueError: The shape of NestedTensor and input tensor does not match, ...
>>> p = nested_tensor.nested_like(torch.randn(2, 2), False)
>>> p = nested_tensor.nested_like(torch.randn(3, 3), False)
Traceback (most recent call last):
...
ValueError: The batch size of NestedTensor and input tensor does not match, 2 != 3
"""
if isinstance(tensor, NestedTensor):
return cast(NestedTensor, tensor.clone())
if strict and self.shape != tensor.shape:
raise ValueError(
f"The shape of NestedTensor and input tensor does not match, {self.shape} != {tensor.shape}"
)
batch_dim = 0 if self.dim() <= 1 or self.batch_first else 1
if len(self) != tensor.size(batch_dim):
raise ValueError(
"The batch size of NestedTensor and input tensor does not match, "
f"{len(self)} != {tensor.size(batch_dim)}"
)
values = self._dense_to_packed_values(tensor)
if values is not None:
element_shapes = self._element_shapes
return self.__class__._from_packed(
values,
self._offsets,
self._physical_shape,
permutation=self._permutation,
ragged_dims=self._ragged_dims if self._ragged_dims_explicit else None,
batch_first=self.batch_first,
padding_value=self.padding_value,
mask_value=self.mask_value,
pin_memory=self._pin_memory,
outer_size=self._logical_shape,
packed_sizes=self._packed_sizes,
element_shapes=element_shapes,
validate=False,
)
dense_tensor = tensor.to(device=self.device)
element_shapes = self._original_shapes()
new_storage = []
slices: tuple[int | slice, ...]
for idx, shape in enumerate(element_shapes):
if self.batch_first:
slices = (idx, *[slice(0, int(dim)) for dim in shape])
else:
if len(shape) == 0:
slices = (idx,)
else:
slices = (slice(0, int(shape[0])), idx, *[slice(0, int(dim)) for dim in shape[1:]])
# .contiguous() ensures storage elements don't inherit non-trivial
# strides from the padded tensor (e.g. after transpose).
new_storage.append(dense_tensor[slices].contiguous())
return self.__class__(new_storage, dtype=tensor.dtype, **self._meta(include_dtype=False))
@property
def occupancy(self) -> float:
r"""
Occupancy of the NestedTensor.
Examples:
>>> nested_tensor = NestedTensor([torch.tensor([1, 2, 3, 4]), torch.tensor([5, 6])])
>>> nested_tensor.occupancy
0.75
"""
if len(self) == 0:
return 0.0
denom = self.shape.numel() # type: ignore[union-attr]
if denom == 0:
return 0.0
return self.numel() / denom # type: ignore[union-attr]
def to_torch_nested(self) -> Tensor:
r"""
Create a `torch.nested.nested_tensor` object from `self`.
Examples:
>>> nested_tensor = NestedTensor([[2, 3, 5], [7, 8]])
>>> nt = nested_tensor.to_torch_nested()
>>> nt.layout == torch.jagged
True
>>> nt.values()
tensor([2, 3, 5, 7, 8])
"""
storage = list(self._storage)
if not storage or all(t.dim() > 0 for t in storage):
return nested.nested_tensor(storage, layout=torch.jagged)
return nested.nested_tensor(storage)
def unbind(self, dim: int = 0) -> tuple[Tensor, ...]:
r"""
Unbind the NestedTensor.
"""
return torch.unbind(self, dim=dim)
def _maybe_exact_shape_nested_like(self, tensor: object) -> NestedTensor | None:
r"""
Convert an exact-shape dense tensor to this NestedTensor's layout.
This is the shared policy boundary for dense-to-nested alignment used by
operator helpers: only non-scalar dense tensors with logical shape exactly
matching ``self.shape`` are converted, and the conversion always uses
``nested_like(..., strict=False)``.
"""
if not isinstance(tensor, Tensor) or isinstance(tensor, type(self)):
return None
if tensor.dim() == 0 or tensor.shape != self.shape:
return None
return self.nested_like(tensor, strict=False)
# ------------------------------------------------------------------
# Indexing
# ------------------------------------------------------------------
def _batch_select_static_position(self, position: int, tail_index: tuple = ()) -> Tensor | None:
r"""Select one ragged position from every batch element without unpacking storage."""
if self._varying_dims != (0,):
return None
if len(self) == 0:
extent = int(self._max_physical_dims()[0])
normalized_position = int(position)
if normalized_position < 0:
normalized_position += extent
if normalized_position < 0 or normalized_position >= extent:
raise IndexError(f"index {position} is out of bounds for dimension 0 with size {extent}")
return self.concat.narrow(0, 0, 0)[(slice(None), *tail_index)]
if self._packed_sizes is not None:
lengths = self._packed_sizes
elif _is_compiling() or _is_fake_tensor(self._offsets):
_compile_unsupported(
"NestedTensor select",
"tensor-backed per-element index bounds are not implemented",
)
else:
lengths = tuple(int(size) for size in (self._offsets[1:] - self._offsets[:-1]).tolist())
idx = int(position)
if idx >= 0:
if any(idx >= int(length) for length in lengths):
raise IndexError(f"index {position} is out of bounds for at least one NestedTensor element")
local: Tensor | int = idx
else:
normalized: list[int] = []
for length in lengths:
idx = int(position)
idx += int(length)
if idx < 0 or idx >= int(length):
raise IndexError(f"index {position} is out of bounds for at least one NestedTensor element")
normalized.append(idx)
local = torch.as_tensor(normalized, dtype=torch.long, device=self.concat.device)
offsets = self._offsets[:-1].to(device=self.concat.device, dtype=torch.long)
selected = self.concat.index_select(0, offsets + local)
if tail_index:
selected = selected[(slice(None), *tail_index)]
return selected
def _packed_physical_slice(self, rest: tuple) -> Self | None:
r"""Slice packed physical dims when the batch dim is untouched."""
if not self.batch_first:
return None
physical_rank = int(self._physical_shape.size(1))
if len(rest) > physical_rank:
return None
index = tuple(rest) + (slice(None),) * (physical_rank - len(rest))
if not all(isinstance(selector, slice) for selector in index):
return None
static_dims = self._static_dims
static_lookup = {dim: axis for axis, dim in enumerate(static_dims)}
varying_dims = self._varying_dims
physical_dims = list(self._max_physical_dims())
value_index: list[slice] = [slice(None)] * self.concat.dim()
replace_dims: dict[int, int] = {}
ragged_selector: tuple[int, slice] | None = None
changed = False
for dim, selector in enumerate(index):
if selector.step is not None and selector.step <= 0:
raise ValueError("step must be greater than zero")
axis = static_lookup.get(dim)
if axis is None:
if selector.start is None and selector.stop is None and selector.step is None:
continue
if len(varying_dims) != 1 or dim != varying_dims[0] or ragged_selector is not None:
return None
ragged_selector = (dim, selector)
size = int(physical_dims[dim])
start, stop, step = selector.indices(size)
retained_size = len(range(start, stop, step))
replace_dims[dim] = retained_size
physical_dims[dim] = retained_size
changed = True
else:
size = int(physical_dims[dim])
start, stop, step = selector.indices(size)
new_size = len(range(start, stop, step))
value_index[1 + axis] = slice(start, stop, step)
replace_dims[dim] = new_size
physical_dims[dim] = new_size
changed = changed or start != 0 or stop != size or step != 1
if not changed:
return self
packed_sizes = self._packed_sizes
offsets = self._offsets
values = self.concat[tuple(value_index)]
if ragged_selector is not None:
ragged_dim, selector = ragged_selector
if self._packed_sizes is None and (
_is_compiling() or _is_fake_tensor(self._offsets) or _is_fake_tensor(self._physical_shape)
):
_compile_unsupported(
"NestedTensor slice",
"tensor-backed ragged slice metadata is not implemented",
)
if _is_fake_tensor(self._offsets):
return None
starts = []
new_sizes = []
lengths = (self._offsets[1:] - self._offsets[:-1]).tolist()
step = 1
for length in lengths:
start, stop, step = selector.indices(int(length))
starts.append(start)
new_sizes.append(len(range(start, stop, step)))
offsets = type(self)._offsets_from_sizes(new_sizes, dtype=self._offsets.dtype)
total = int(offsets[-1].item()) if len(new_sizes) > 0 else 0
if total == 0:
gather = torch.empty((0,), device=self.concat.device, dtype=torch.long)
else:
offsets_dev = self._offsets.to(device=self.concat.device, dtype=torch.long)
new_offsets_dev = offsets.to(device=self.concat.device, dtype=torch.long)
starts_dev = torch.as_tensor(starts, device=self.concat.device, dtype=torch.long)
sizes_dev = torch.as_tensor(new_sizes, device=self.concat.device, dtype=torch.long)
batch_idx = torch.arange(len(new_sizes), device=self.concat.device, dtype=torch.long).repeat_interleave(
sizes_dev, output_size=total
)
local_rank = (
torch.arange(total, device=self.concat.device, dtype=torch.long) - new_offsets_dev[batch_idx]
)
gather = offsets_dev[batch_idx] + starts_dev[batch_idx] + local_rank * step
values = self.concat.index_select(0, gather)[tuple(value_index)]
if new_sizes or not (len(self) == 0 and self._ragged_dims_explicit):
replace_dims[ragged_dim] = max(new_sizes, default=0)
physical_dims[ragged_dim] = max(new_sizes, default=0)
packed_sizes = tuple(new_sizes)
element_shapes = None
if self._element_shapes is not None:
if any(len(shape) != physical_rank for shape in self._element_shapes):
return None
rows = []
for shape in self._element_shapes:
row = []
for dim, size in enumerate(shape):
if ragged_selector is not None and dim == ragged_selector[0]:
start, stop, step = ragged_selector[1].indices(int(size))
row.append(len(range(start, stop, step)))
else:
row.append(replace_dims.get(dim, int(size)))
rows.append(tuple(row))
element_shapes = tuple(rows)
new_physical_shape = self._physical_shape.clone()
for dim, size in replace_dims.items():
if ragged_selector is not None and dim == ragged_selector[0] and packed_sizes is not None:
new_physical_shape[:, dim] = new_physical_shape.new_tensor(packed_sizes)
else:
new_physical_shape[:, dim] = size
return type(self)._from_packed(
values,
offsets,
new_physical_shape,
permutation=self._permutation,
ragged_dims=self._ragged_dims if self._ragged_dims_explicit else None,
batch_first=self.batch_first,
padding_value=self.padding_value,
mask_value=self.mask_value,
pin_memory=self._pin_memory,
outer_size=self._logical_shape_from_physical_dims(physical_dims),
packed_sizes=packed_sizes,
element_shapes=element_shapes,
ragged_offsets=self._persistent_ragged_offsets() if ragged_selector is None else None,
validate=False,
)
def _packed_static_integer_index(self, rest: tuple) -> Self | None:
r"""Consume one static element axis directly on packed values."""
physical_rank = int(self._physical_shape.size(1))
if len(rest) > physical_rank:
return None
selectors = tuple(rest) + (slice(None),) * (physical_rank - len(rest))
integer_dims = [dim for dim, selector in enumerate(selectors) if type(selector) is int]
if len(integer_dims) != 1:
return None
physical_dim = integer_dims[0]
if any(
not (
type(selector) is int
or (
isinstance(selector, slice)
and selector.start is None
and selector.stop is None
and selector.step is None
)
)
for selector in selectors
):
return None
values_dim = _physical_to_values_dim(self, physical_dim)
if values_dim is None:
return None
from .aten_functions import _packed_without_dim
values = self.concat.select(values_dim, int(selectors[physical_dim]))
return cast(Self, _packed_without_dim(self, physical_dim, values))
def _packed_newaxis_index(self, rest: tuple) -> Self | None:
r"""Insert basic ``None`` axes without unpacking an untouched batch."""
if not self.batch_first or not any(selector is None for selector in rest):
return None
physical_rank = int(self._physical_shape.size(1))
consumed = sum(selector is not None for selector in rest)
if consumed > physical_rank:
return None
selectors = tuple(rest) + (slice(None),) * (physical_rank - consumed)
if any(
selector is not None
and not (
isinstance(selector, slice)
and selector.start is None
and selector.stop is None
and selector.step is None
)
for selector in selectors
):
return None
result = self
for logical_dim, selector in enumerate(selectors, start=1):
if selector is None:
result = result.unsqueeze(logical_dim)
return result
def _empty_batch_like(self) -> Self:
r"""Return a source-derived empty batch without discarding element-rank topology."""
outer_size = list(self._logical_shape)
batch_dim = type(self)._batch_dim_from_logical_shape(self._logical_shape, self.batch_first)
outer_size[batch_dim] = 0
return type(self)._from_packed(
self.concat.narrow(0, 0, 0),
self._offsets.new_zeros((1,)),
self._physical_shape[:0],
permutation=self._permutation,
ragged_dims=self._ragged_dims if self._ragged_dims_explicit else None,
batch_first=self.batch_first,
padding_value=self.padding_value,
mask_value=self.mask_value,
pin_memory=self._pin_memory,
outer_size=torch.Size(outer_size),
packed_sizes=(),
element_shapes=(),
validate=False,
)
def _empty_batch_basic_index(self, rest: tuple) -> Self | None:
r"""Project a source-derived empty batch through basic element indexing.
With no physical rows, ``_physical_shape`` cannot report the output
extents. ``_empty_batch_like`` deliberately retains those maxima in the
logical shape, so use them as structural extents and rebuild metadata
without inventing a representative element.
"""
if len(self) != 0 or not self._ragged_dims_explicit:
return None
physical_rank = int(self._physical_shape.size(1))
selectors: list[tuple[int | None, object]] = []
old_dim = 0
for selector in rest:
if selector is None:
selectors.append((None, selector))
continue
if old_dim >= physical_rank:
raise IndexError(f"too many indices for NestedTensor with element rank {physical_rank}")
selectors.append((old_dim, selector))
old_dim += 1
while old_dim < physical_rank:
selectors.append((old_dim, slice(None)))
old_dim += 1
if any(
type(selector) is not int and not isinstance(selector, slice) and selector is not None
for _, selector in selectors
):
return None
physical_extents = self._max_physical_dims()
static_lookup = {dim: axis for axis, dim in enumerate(self._static_dims)}
static_selectors: dict[int, int | slice] = {}
old_to_new: dict[int, int] = {}
inserted_dims: list[int] = []
projected_extents: list[int] = []
for dim, selector in selectors:
if dim is None:
inserted_dims.append(len(projected_extents))
projected_extents.append(1)
continue
extent = int(physical_extents[dim])
if type(selector) is int:
index = int(selector)
if index < 0:
index += extent
if index < 0 or index >= extent:
raise IndexError(f"index {selector} is out of bounds for dimension {dim} with size {extent}")
if dim in static_lookup:
static_selectors[dim] = index
continue
assert isinstance(selector, slice)
if selector.step is not None and selector.step <= 0:
raise ValueError("step must be greater than zero")
start, stop, step = selector.indices(extent)
old_to_new[dim] = len(projected_extents)
projected_extents.append(len(range(start, stop, step)))
if dim in static_lookup:
static_selectors[dim] = slice(start, stop, step)
values_index = (slice(None), *(static_selectors.get(dim, slice(None)) for dim in self._static_dims))
values = self.concat[values_index]
packed_static_dims = [old_to_new[dim] for dim in self._static_dims if dim in old_to_new]
for dim in inserted_dims:
values = values.unsqueeze(-1)
packed_static_dims.append(dim)
ragged_dims = tuple(old_to_new[dim] for dim in self._ragged_dims if dim in old_to_new)
static_dims = tuple(dim for dim in range(len(projected_extents)) if dim not in ragged_dims)
values = values.permute((0, *(1 + packed_static_dims.index(dim) for dim in static_dims)))
offsets = self._offsets.new_zeros((1,))
physical_shape = self._physical_shape.new_empty((0, len(projected_extents)))
ragged_offsets = tuple(offsets for _ in ragged_dims) if ragged_dims else None
return type(self)._from_packed(
values,
offsets,
physical_shape,
permutation=ragged_dims + static_dims,
ragged_dims=ragged_dims,
batch_first=self.batch_first,
padding_value=self.padding_value,
mask_value=self.mask_value,
pin_memory=self._pin_memory,
outer_size=self._logical_shape_from_physical_dims(projected_extents),
packed_sizes=(),
element_shapes=(),
ragged_offsets=ragged_offsets,
validate=False,
)
def _meta_after_basic_index(self, rest: tuple, *, include_dtype: bool = True) -> Mapping:
r"""Return reconstruction metadata after basic indexing of physical element dimensions."""
meta = dict(self._meta(include_dtype=include_dtype))
if not self._ragged_dims_explicit:
return meta
old_to_new: dict[int, int] = {}
old_dim = 0
new_dim = 0
for selector in rest:
if selector is None:
new_dim += 1
continue
if old_dim >= self._physical_shape.size(1):
meta["ragged_dims"] = None
return meta
if isinstance(selector, int):
old_dim += 1
continue
if not isinstance(selector, slice):
# Advanced indexing can reorder dimensions; without explicit
# provenance it is safer to drop the declaration than guess.
meta["ragged_dims"] = None
return meta
old_to_new[old_dim] = new_dim
old_dim += 1
new_dim += 1
while old_dim < self._physical_shape.size(1):
old_to_new[old_dim] = new_dim
old_dim += 1
new_dim += 1
meta["ragged_dims"] = tuple(old_to_new[dim] for dim in self._ragged_dims if dim in old_to_new)
return meta
def __getitem__(self, index: _Index | tuple[_Index, ...]) -> Tensor | NestedTensor:
r"""Retrieve element(s) by index, slice, list, tuple, or tensor mask."""
if isinstance(index, int):
return self._storage[index]
if isinstance(index, (slice, list)):
if isinstance(index, list) and index and all(isinstance(i, bool) for i in index):
if len(index) != len(self):
raise IndexError(f"Boolean index has length {len(index)} but batch size is {len(self)}")
index = [i for i, flag in enumerate(index) if flag]
storage = tuple(self._storage[index] if isinstance(index, slice) else [self._storage[i] for i in index])
if not storage and self._ragged_dims_explicit:
return self._empty_batch_like()
return self.__class__(storage, **self._meta(include_dtype=True))
if isinstance(index, tuple):
if len(index) == 0:
return self
# Expand Ellipsis: ``nt[..., :2]`` on a 4-D NestedTensor becomes
# ``nt[:, :, :, :2]``. The batch dim is consumed first, so Ellipsis
# fills the gap between the number of explicit indices and the total
# number of logical dimensions.
if index.count(Ellipsis) > 1:
raise IndexError("an index can only have a single ellipsis ('...')")
if Ellipsis in index:
eidx = index.index(Ellipsis)
n_explicit = sum(1 for entry in index if entry is not Ellipsis and entry is not None)
n_expand = self.dim() - n_explicit
index = index[:eidx] + (slice(None),) * n_expand + index[eidx + 1 :]
batch_index, *rest = index
symbolic_tensor_metadata = self._packed_sizes is None and (
_is_compiling() or _is_fake_tensor(self._offsets) or _is_fake_tensor(self._physical_shape)
)
if symbolic_tensor_metadata and batch_index == slice(None) and rest:
newaxis_output = self._packed_newaxis_index(tuple(rest))
if newaxis_output is not None:
return newaxis_output
integer_output = self._packed_static_integer_index(tuple(rest))
if integer_output is not None:
return integer_output
first_selector = rest[0]
if isinstance(first_selector, int):
_compile_unsupported(
"NestedTensor select",
"tensor-backed per-element index bounds are not implemented",
)
if isinstance(first_selector, slice) and first_selector != slice(None):
_compile_unsupported(
"NestedTensor slice",
"tensor-backed ragged slice metadata is not implemented",
)
if isinstance(batch_index, (Tensor, NestedTensor)):
return self.tensor[index]
if isinstance(batch_index, list) and batch_index and all(isinstance(i, bool) for i in batch_index):
if len(batch_index) != len(self):
raise IndexError(f"Boolean index has length {len(batch_index)} but batch size is {len(self)}")
batch_index = [i for i, flag in enumerate(batch_index) if flag]
if isinstance(batch_index, int):
tensor = self._storage[batch_index]
if rest:
return tensor[tuple(rest)]
return tensor
elif isinstance(batch_index, (slice, list)):
if (
self.batch_first
and isinstance(batch_index, slice)
and batch_index == slice(None)
and rest
and isinstance(rest[0], int)
):
static_position = self._batch_select_static_position(rest[0], tuple(rest[1:]))
if static_position is not None:
return static_position
if isinstance(batch_index, slice) and batch_index == slice(None) and rest:
newaxis_output = self._packed_newaxis_index(tuple(rest))
if newaxis_output is not None:
return newaxis_output
integer_output = self._packed_static_integer_index(tuple(rest))
if integer_output is not None:
return integer_output
slice_output = self._packed_physical_slice(tuple(rest))
if slice_output is not None:
return slice_output
empty_output = self._empty_batch_basic_index(tuple(rest))
if empty_output is not None:
return empty_output
if isinstance(batch_index, slice):
selected = self._storage[batch_index]
else:
selected = tuple(self._storage[i] for i in batch_index)
if rest:
rest_tuple = tuple(rest)
selected = tuple(t[rest_tuple] for t in selected)
meta = self._meta_after_basic_index(rest_tuple, include_dtype=True)
else:
meta = self._meta(include_dtype=True)
if not selected and self._ragged_dims_explicit:
empty = self._empty_batch_like()
if not rest:
return empty
rest_tuple = tuple(rest)
projected = empty._empty_batch_basic_index(rest_tuple)
if projected is not None:
return projected
return self.__class__(selected, **meta)
raise ValueError(f"Unsupported batch index type {type(batch_index)}")
if isinstance(index, NestedTensor):
if len(self) != len(index):
raise ValueError(
"NestedTensor batch length mismatch between self and index: "
f"self={len(self)}, index={len(index)}"
)
return self.__class__(
[t[i] for t, i in zip(self._storage, index._storage)], **self._meta(include_dtype=True)
)
if isinstance(index, Tensor):
if index.dim() == 0 and index.dtype in (torch.int8, torch.int16, torch.int32, torch.int64, torch.uint8):
return self._storage[int(index.item())]
if index.dim() == 1:
if index.dtype in (torch.bool, torch.uint8):
if index.numel() != len(self):
raise IndexError(f"Boolean index has length {index.numel()} but batch size is {len(self)}")
selected = tuple(self._storage[i] for i, flag in enumerate(index.tolist()) if bool(flag))
if not selected and self._ragged_dims_explicit:
return self._empty_batch_like()
return self.__class__(selected, **self._meta(include_dtype=True))
if index.dtype in (torch.int8, torch.int16, torch.int32, torch.int64, torch.uint8):
if index.numel() == 0 and self._ragged_dims_explicit:
return self._empty_batch_like()
return self.__class__(
[self._storage[int(i)] for i in index.tolist()],
**self._meta(include_dtype=True),
)
index = self.nested_like(index, strict=False)
return self.__class__(
[t[i] for t, i in zip(self._storage, index._storage)], **self._meta(include_dtype=True)
)
raise ValueError(f"Unsupported index type {type(index)}")
def __setitem__(self, index: _Index | tuple[_Index, ...], value: Tensor | int | float | bool) -> None:
r"""
Set values in the NestedTensor at the specified index.
Args:
index: The index to modify. Can be an integer, slice, list, or tuple.
value: The new value to set. Can be a Tensor or NestedTensor.
Examples:
>>> nested_tensor = NestedTensor([torch.tensor([1, 2, 3]), torch.tensor([4, 5])])
>>> nested_tensor[0] = torch.tensor([6, 7, 8])
>>> nested_tensor[0]
tensor([6, 7, 8])
>>> nested_tensor[1] = torch.tensor([9, 10, 11, 12])
>>> nested_tensor.shape
torch.Size([2, 4])
"""
if isinstance(index, int):
self._invalidate_transient_caches()
if isinstance(value, NestedTensor):
if len(value._storage) != 1:
raise ValueError(
f"When setting with an integer index, value must have a single tensor, but got {len(value)}"
)
value = value._storage[0]
if not isinstance(value, Tensor):
value = torch.tensor(value, device=self.device, dtype=self.dtype)
else:
value = value.to(device=self.device, dtype=self.dtype)
if self.requires_grad:
value.requires_grad_(True)
# Normalize negative index
idx = index + len(self) if index < 0 else index
if idx < 0 or idx >= len(self):
raise IndexError(f"index {index} is out of range for NestedTensor with {len(self)} elements")
expected_ndim = self._physical_shape.size(1)
if value.dim() != expected_ndim:
raise ValueError(
f"Assigned tensor ndim must match existing ndim {expected_ndim}, but got {value.dim()}"
)
old_start = int(self._offsets[idx].item())
old_end = int(self._offsets[idx + 1].item())
old_size = old_end - old_start
new_shape_row = torch.tensor(list(value.shape), dtype=self._physical_shape.dtype)
permutation = self._permutation
identity_permutation = tuple(range(expected_ndim))
varying_dims = self._varying_dims
static_dims = self._static_dims
packed_size = type(self)._packed_size_from_shape(tuple(int(dim) for dim in value.shape), varying_dims)
packed_value = value if permutation == identity_permutation else value.permute(permutation)
suffix_shape = tuple(int(value.shape[dim]) for dim in static_dims)
new_payload = packed_value.reshape((packed_size, *suffix_shape) if suffix_shape else (packed_size,))
new_size = packed_size
if self.concat.dim() > 1 and new_payload.shape[1:] != self.concat.shape[1:]:
storage_list = list(self._storage)
storage_list[idx] = value
self._repack(storage_list)
return
if new_size == old_size:
# Same packed span size: direct overwrite keeps concat allocation.
self._packed_values[old_start:old_end] = new_payload
self._physical_shape[idx] = new_shape_row
else:
# Different packed span size: splice concat and shift subsequent offsets.
self._packed_values = torch.cat([self.concat[:old_start], new_payload, self.concat[old_end:]], dim=0)
delta = new_size - old_size
self._offsets = self._offsets.clone()
self._offsets[idx + 1 :] += delta # noqa: E203
self._physical_shape = self._physical_shape.clone()
self._physical_shape[idx] = new_shape_row
self._logical_shape = self._logical_shape_from_physical_shape(
self._physical_shape, self._offsets, self.batch_first
)
if self._element_shapes is not None and self._packed_sizes is not None:
element_shapes = list(self._element_shapes)
element_shapes[idx] = tuple(int(dim) for dim in value.shape)
self._element_shapes = tuple(element_shapes)
packed_sizes = list(self._packed_sizes)
packed_sizes[idx] = self._packed_sizes_like(
(self._element_shapes[idx],),
self._ragged_dims if self._ragged_dims_explicit else None,
)[0]
self._packed_sizes = tuple(packed_sizes)
self._validate_metadata()
elif isinstance(index, (slice, list)):
if isinstance(index, list) and index and all(isinstance(i, bool) for i in index):
if len(index) != len(self):
raise IndexError(f"Boolean index has length {len(index)} but batch size is {len(self)}")
index = [i for i, flag in enumerate(index) if flag]
if not isinstance(value, Tensor):
raise TypeError("Replacing complete batch elements requires tensor values")
if not isinstance(value, NestedTensor):
if value.dim() > 1 and value.size(0) > 1:
value = self.__class__(value.unbind(0), **self._meta())
else:
value = self.__class__([value], **self._meta())
if isinstance(index, slice):
start, stop, step = index.indices(len(self))
indices = range(start, stop, step)
else:
indices = index # type: ignore[assignment]
if len(indices) != len(value._storage):
raise ValueError(
f"Size mismatch: tried to assign {len(value._storage)} values to {len(indices)} indices"
)
storage_list = list(self._storage)
for i, idx in enumerate(indices):
storage_list[idx] = value._storage[i]
self._storage = tuple(storage_list)
elif isinstance(index, tuple):
if len(index) == 0:
return
# Expand Ellipsis (e.g. ``nt[..., 0] = 0``) the same way __getitem__ does:
# the batch dim is consumed first, so Ellipsis fills the gap between the
# explicit indices and the total number of logical dimensions.
if index.count(Ellipsis) > 1:
raise IndexError("an index can only have a single ellipsis ('...')")
if Ellipsis in index:
eidx = index.index(Ellipsis)
n_explicit = sum(1 for entry in index if entry is not Ellipsis and entry is not None)
n_expand = self.dim() - n_explicit
index = index[:eidx] + (slice(None),) * n_expand + index[eidx + 1 :]
if len(index) == 1:
self[index[0]] = value
return
first_idx, rest_idx = index[0], index[1:]
batch_indices: list[int]
if isinstance(first_idx, int):
batch_indices = [first_idx]
elif isinstance(first_idx, (slice, list)):
if isinstance(first_idx, list) and first_idx and all(isinstance(i, bool) for i in first_idx):
if len(first_idx) != len(self):
raise IndexError(f"Boolean index has length {len(first_idx)} but batch size is {len(self)}")
batch_indices = [i for i, flag in enumerate(first_idx) if flag]
elif isinstance(first_idx, slice):
start, stop, step = first_idx.indices(len(self))
batch_indices = list(range(start, stop, step))
else:
batch_indices = list(first_idx) # type: ignore[arg-type]
else:
raise ValueError(f"Unsupported first index type {type(first_idx)}")
assigned_values: list[Tensor | int | float | bool]
if isinstance(value, NestedTensor):
if len(batch_indices) != len(value._storage):
raise ValueError(
f"Size mismatch: tried to assign {len(value._storage)} values to {len(batch_indices)} indices"
)
assigned_values = list(value._storage)
else:
assigned_values = [value] * len(batch_indices)
elems = list(self._storage)
for position, idx in enumerate(batch_indices):
elem = elems[idx].clone()
elem[rest_idx] = assigned_values[position]
elems[idx] = elem
self._storage = tuple(elems)
else:
raise ValueError(f"Unsupported index type {type(index)}")
# ------------------------------------------------------------------
# Properties: runtime config, dtype, device, requires_grad
# ------------------------------------------------------------------
@property
def batch_first(self) -> bool:
r"""Whether the logical outer shape uses ``(B, ...)`` instead of ``(..., B, ...)``."""
return self._batch_first
@batch_first.setter
def batch_first(self, value: bool):
new_value = type(self)._coerce_batch_first(value)
old_value = getattr(self, "_batch_first", None)
self._batch_first = new_value
if old_value is None or old_value == new_value:
return
if hasattr(self, "_physical_shape") and hasattr(self, "_offsets") and hasattr(self, "_logical_shape"):
self._logical_shape = type(self)._logical_shape_from_physical_shape(
self._physical_shape,
self._offsets,
new_value,
)
if hasattr(self, "_cached_tensor_view"):
self._invalidate_transient_caches()
@property
def padding_value(self) -> float:
r"""Padding fill value used when materializing dense views."""
return self._padding_value
@padding_value.setter
def padding_value(self, value: SupportsFloat):
new_value = type(self)._coerce_padding_value(value)
old_value = getattr(self, "_padding_value", None)
self._padding_value = new_value
if old_value is None or old_value == new_value:
return
if hasattr(self, "_cached_tensor_view"):
self._cached_tensor_view = None
@property
def mask_value(self) -> bool:
r"""Boolean value used to denote padding positions in generated masks."""
return self._mask_value
@mask_value.setter
def mask_value(self, value: bool):
new_value = type(self)._coerce_mask_value(value)
old_value = getattr(self, "_mask_value", None)
self._mask_value = new_value
if old_value is None or old_value == new_value:
return
if hasattr(self, "_cached_mask_view"):
self._cached_mask_view = None
@property
def dtype(self) -> torch.dtype: # type: ignore[override]
r"""Data type of the underlying tensor elements."""
values = vars(self).get("_packed_values")
if isinstance(values, Tensor):
return values.dtype
return super().dtype
@dtype.setter
def dtype(self, value: torch.dtype | None):
r"""`dtype` is read-only; use `.to(dtype=...)` to convert."""
raise AttributeError("NestedTensor.dtype is read-only; use .to(dtype=...) to create a converted tensor.")
@property
def device(self) -> torch.device: # type: ignore[override]
r"""Device on which the underlying tensor data resides."""
values = vars(self).get("_packed_values")
if isinstance(values, Tensor):
return values.device
return torch.Tensor.device.__get__(self)
@device.setter
def device(self, value: torch.device | None):
r"""`device` is read-only; use `.to(device=...)` to move tensors."""
raise AttributeError("NestedTensor.device is read-only; use .to(device=...) to create a moved tensor.")
@property
def requires_grad(self) -> bool: # type: ignore[override]
r"""Whether gradient computation is enabled for this tensor."""
return super().requires_grad
@requires_grad.setter
def requires_grad(self, value: bool):
r"""Enable or disable gradient computation for this tensor."""
if super().requires_grad == value:
return
values = vars(self).get("_packed_values")
if not self.is_leaf:
raise RuntimeError("you can only change requires_grad flags of leaf variables")
if isinstance(values, Tensor) and values.requires_grad != value and not values.is_leaf:
raise RuntimeError("you can only change requires_grad flags of leaf variables")
torch.Tensor.requires_grad.__set__(self, value) # type: ignore[attr-defined]
if isinstance(values, Tensor) and values.requires_grad != value:
values.requires_grad_(value)
# ------------------------------------------------------------------
# State management
# ------------------------------------------------------------------
def _meta(self, *, include_dtype: bool | None = None) -> Mapping:
r"""Metadata used for structure-preserving reconstruction."""
if include_dtype is None:
# Empty reconstructions cannot infer dtype from storage; include it by default.
include_dtype = self.concat.numel() == 0
if include_dtype:
return {
"batch_first": self.batch_first,
"padding_value": self.padding_value,
"mask_value": self.mask_value,
"pin_memory": self._pin_memory,
"ragged_dims": self._ragged_dims if self._ragged_dims_explicit else None,
"_lenient_layout": True,
"device": self.concat.device,
"dtype": self.dtype,
}
return {
"batch_first": self.batch_first,
"padding_value": self.padding_value,
"mask_value": self.mask_value,
"pin_memory": self._pin_memory,
"ragged_dims": self._ragged_dims if self._ragged_dims_explicit else None,
"_lenient_layout": True,
"device": self.concat.device,
}
def __getstate__(self) -> dict:
return {
"_packed_values": self.concat,
"_offsets": self._offsets,
"_permutation": self._permutation,
"_ragged_dims": self._ragged_dims if self._ragged_dims_explicit else None,
"_physical_shape": self._physical_shape,
"_logical_shape": self._logical_shape,
"batch_first": self.batch_first,
"padding_value": self.padding_value,
"mask_value": self.mask_value,
"_pin_memory": self._pin_memory,
"_packed_sizes": self._packed_sizes,
"_element_shapes": self._element_shapes,
"_ragged_offsets": self._persistent_ragged_offsets(),
}
def __setstate__(self, state: Mapping) -> None:
self._packed_values = state["_packed_values"]
self._offsets = state["_offsets"].cpu()
self._permutation = tuple(int(dim) for dim in state["_permutation"])
declared_ragged_dims = state["_ragged_dims"]
self._ragged_dims = type(self)._ragged_dims_from_packed_layout(
self.concat,
len(self._permutation),
self._permutation,
declared_ragged_dims,
)
self._ragged_dims_explicit = declared_ragged_dims is not None or bool(self._ragged_dims)
self._physical_shape = state["_physical_shape"].cpu()
self._logical_shape = state["_logical_shape"]
self._set_runtime_config(
batch_first=state["batch_first"],
padding_value=state["padding_value"],
mask_value=state["mask_value"],
)
self._pin_memory = bool(state["_pin_memory"] and self.concat.device.type == "cpu" and self.concat.is_pinned())
self._packed_sizes = state["_packed_sizes"]
self._element_shapes = state["_element_shapes"]
serialized_ragged_offsets = state["_ragged_offsets"]
if serialized_ragged_offsets is not None:
serialized_ragged_offsets = tuple(level_offsets.cpu() for level_offsets in serialized_ragged_offsets)
ragged_offsets = type(self)._resolve_persistent_ragged_offsets(
self._offsets,
self._physical_shape,
permutation=self._permutation,
ragged_dims=self._ragged_dims if self._ragged_dims_explicit else None,
ragged_offsets=serialized_ragged_offsets,
)
if ragged_offsets is not None:
self._packed_sizes = None
self._element_shapes = None
type(self)._install_persistent_ragged_offsets(self, ragged_offsets)
# Serialized state intentionally excludes transient caches.
self._invalidate_transient_caches()
self._mark_tensor_backed_dynamic_dims()
self._validate_metadata()
def __reduce__(self):
return (self.__class__._from_state, (self.__getstate__(),))
@classmethod
def _from_state(cls, state: dict) -> Self:
serialized_ragged_offsets = state["_ragged_offsets"]
if serialized_ragged_offsets is not None:
serialized_ragged_offsets = tuple(level_offsets.cpu() for level_offsets in serialized_ragged_offsets)
return cls._from_packed(
state["_packed_values"],
state["_offsets"].cpu(),
state["_physical_shape"].cpu(),
permutation=tuple(int(dim) for dim in state["_permutation"]),
ragged_dims=state["_ragged_dims"],
batch_first=state["batch_first"],
padding_value=state["padding_value"],
mask_value=state["mask_value"],
pin_memory=state["_pin_memory"],
outer_size=state["_logical_shape"],
packed_sizes=state["_packed_sizes"],
element_shapes=state["_element_shapes"],
ragged_offsets=serialized_ragged_offsets,
)
def __copy__(self):
r"""Shallow copy: new NestedTensor sharing underlying tensor data."""
return self.__class__._from_packed(
self.concat,
self._offsets,
self._physical_shape,
permutation=self._permutation,
ragged_dims=self._ragged_dims if self._ragged_dims_explicit else None,
batch_first=self.batch_first,
padding_value=self.padding_value,
mask_value=self.mask_value,
pin_memory=self._pin_memory,
outer_size=self._logical_shape,
packed_sizes=self._packed_sizes,
element_shapes=self._element_shapes,
ragged_offsets=self._persistent_ragged_offsets(),
validate=False,
)
def __deepcopy__(self, memo):
r"""Deep copy: clones all tensor data."""
result = self.__class__._from_packed(
self.concat.clone(),
self._offsets.clone(),
self._physical_shape.clone(),
permutation=self._permutation,
ragged_dims=self._ragged_dims if self._ragged_dims_explicit else None,
batch_first=self.batch_first,
padding_value=self.padding_value,
mask_value=self.mask_value,
pin_memory=self._pin_memory,
outer_size=self._logical_shape,
packed_sizes=self._packed_sizes,
element_shapes=self._element_shapes,
ragged_offsets=(
tuple(level_offsets.clone() for level_offsets in self._persistent_ragged_offsets() or ()) or None
),
validate=False,
)
memo[id(self)] = result
return result
# ------------------------------------------------------------------
# Tensor-like methods
# ------------------------------------------------------------------
def all(self, dim: int | tuple[int, ...] | list[int] | None = None, keepdim: bool = False) -> Tensor:
r"""
Tests if all elements in NestedTensor evaluate to True.
Examples:
>>> nested_tensor = NestedTensor([torch.ones(2, 4, dtype=torch.bool), torch.ones(3, 5, dtype=torch.bool)])
>>> nested_tensor.all()
tensor(True)
>>> nested_tensor.all(dim=0)
tensor([True, True])
>>> nested_tensor.all(dim=0, keepdim=True)
tensor([[True, True]])
>>> nested_tensor.all(dim=1)
NestedTensor([
[True, True, True, True],
[True, True, True, True, True]
])
>>> nested_tensor.all(dim=1, keepdim=True)
NestedTensor([
[[True, True, True, True]],
[[True, True, True, True, True]]
])
>>> nested_tensor.batch_first = False
>>> nested_tensor.all(dim=1)
tensor([True, True])
>>> nested_tensor.all(dim=0)
NestedTensor([
[True, True, True, True],
[True, True, True, True, True]
])
>>> nested_tensor.all(dim=-2)
tensor([True, True])
"""
return torch.all(self, dim=dim, keepdim=keepdim)
def any(self, dim: int | tuple[int, ...] | list[int] | None = None, keepdim: bool = False) -> Tensor:
r"""
Tests if any elements in NestedTensor evaluate to True.
Examples:
>>> nested_tensor = NestedTensor([torch.zeros(2, dtype=torch.bool), torch.ones(3, dtype=torch.bool)])
>>> nested_tensor.any()
tensor(True)
>>> nested_tensor.any(dim=0)
tensor([False, True])
"""
return torch.any(self, dim=dim, keepdim=keepdim)
def dim(self) -> int:
r"""
Number of dimension of the NestedTensor.
Examples:
>>> nested_tensor = NestedTensor([torch.tensor([1, 2, 3]), torch.tensor([4, 5])])
>>> nested_tensor.dim()
2
"""
if not hasattr(self, "_logical_shape"):
with torch._C.DisableTorchFunctionSubclass():
return len(torch.Tensor.size(self))
return len(self._logical_shape)
def mean(
self,
dim: int | tuple[int, ...] | list[int] | None = None,
keepdim: bool = False,
*,
dtype: torch.dtype | None = None, # type: ignore[name-defined]
) -> Tensor | NestedTensor:
r"""Return the mean value, optionally along a given dimension."""
return torch.mean(self, dim=dim, keepdim=keepdim, dtype=dtype)
@property
def mT(self) -> Self: # type: ignore[override]
r"""Matrix transpose over the last two per-element dimensions."""
ndims = self.dim()
batch_dim = 0 if self.batch_first else 1
elem_dims = [d for d in range(ndims) if d != batch_dim]
if len(elem_dims) < 2:
raise RuntimeError(
f"tensor.mT is only supported on matrices or batches of matrices. Got {len(elem_dims)}-D tensor."
)
return cast(Self, torch.transpose(self, elem_dims[-2], elem_dims[-1]))
def numel(self) -> int:
r"""
Number of elements in the NestedTensor.
Examples:
>>> nested_tensor = NestedTensor([torch.tensor([1, 2, 3]), torch.tensor([4, 5])])
>>> nested_tensor.numel()
5
"""
return self.concat.numel()
def permute(self, *dims) -> Self:
r"""
Apply permutation to each tensor in the NestedTensor.
Args:
*dims (int): The desired ordering of logical dimensions, including the batch dimension.
Returns:
NestedTensor: A new NestedTensor with each tensor permuted.
Examples:
>>> nested_tensor = NestedTensor([torch.randn(3, 4, 5), torch.randn(2, 4, 5)])
>>> permuted = nested_tensor.permute(0, 3, 1, 2)
>>> permuted.shape
torch.Size([2, 5, 3, 4])
"""
return cast(Self, torch.permute(self, dims))
def moveaxis(self, source, destination) -> Self:
r"""Move per-element dimensions to new positions."""
return cast(Self, torch.moveaxis(self, source, destination))
def movedim(self, source, destination) -> Self:
r"""Alias for `moveaxis()`."""
return cast(Self, torch.movedim(self, source, destination))
# to(), clone(), detach(), contiguous(), half(), float(), double(), etc.
# are all handled by aten dispatch in aten_functions.py (aten._to_copy, aten.clone,
# aten.detach). No custom Python methods needed.
def pin_memory(self, device: torch.device | str | int | None = None) -> Self:
r"""Pin the underlying tensor memory for faster host-to-device transfer."""
return type(self)._from_packed(
self.concat.pin_memory(device=device),
self._offsets,
self._physical_shape,
permutation=self._permutation,
ragged_dims=self._ragged_dims if self._ragged_dims_explicit else None,
batch_first=self.batch_first,
padding_value=self.padding_value,
mask_value=self.mask_value,
pin_memory=True,
outer_size=self._logical_shape,
packed_sizes=self._packed_sizes,
element_shapes=self._element_shapes,
ragged_offsets=self._persistent_ragged_offsets(),
validate=False,
)
def prod(
self,
dim: int | None = None,
keepdim: bool = False,
*,
dtype: torch.dtype | None = None, # type: ignore[name-defined]
) -> Tensor | NestedTensor:
r"""Return the product of elements, optionally along a given dimension."""
if dim is None:
return torch.prod(self, dtype=dtype)
return torch.prod(self, dim=dim, keepdim=keepdim, dtype=dtype)
def requires_grad_(self, requires_grad: bool = True):
r"""Enable or disable gradient computation in-place."""
self.requires_grad = requires_grad
return self
def reshape(self, *shape) -> Self:
r"""
Reshape each tensor in the NestedTensor.
Args:
*shape (int | tuple[int, ...] | list[int] | torch.Size): Target sizes as separate
integers or one tuple, list or ``torch.Size``. ``-1`` infers a dimension size.
Returns:
NestedTensor: A new NestedTensor with each tensor reshaped.
Examples:
>>> nested_tensor = NestedTensor([torch.tensor([[1, 2], [3, 4]]), torch.tensor([[5, 6], [7, 8]])])
>>> reshaped = nested_tensor.reshape(4)
>>> reshaped.shape
torch.Size([2, 4])
"""
if not shape:
raise TypeError("reshape() missing shape")
target_shape = shape[0] if len(shape) == 1 and isinstance(shape[0], (tuple, list, torch.Size)) else shape
return cast(Self, torch.reshape(self, target_shape))
def repeat_batch(self, repeats: int, *, output_size: int | None = None) -> Self:
r"""Repeat complete logical batch elements in interleaved order."""
from .torch_functions import _repeat_interleave_packed_batch
return cast(Self, _repeat_interleave_packed_batch(self, repeats, output_size=output_size))
def flatten(self, start_dim: int = 0, end_dim: int = -1):
r"""Flatten each tensor in the NestedTensor."""
return torch.flatten(self, start_dim=start_dim, end_dim=end_dim)
@property
def shape(self) -> torch.Size: # type: ignore[override, name-defined]
r"""
Alias for `size()`.
"""
return self.size()
@overload
def size(self, dim: None = None) -> torch.Size: ...
@overload
def size(self, dim: int) -> int: ...
def size(self, dim: int | None = None) -> torch.Size | int:
r"""
Returns the size of the self `NestedTensor`.
Args:
dim: If not specified, the returned value is a `torch.Size`, a subclass of `tuple`.
If specified, returns an `int` holding the size of that dimension.
Defaults to `None`.
Examples:
>>> nested_tensor = NestedTensor([torch.tensor([1, 2, 3]), torch.tensor([4, 5])])
>>> nested_tensor.size()
torch.Size([2, 3])
>>> nested_tensor.size(0)
2
>>> nested_tensor[1] = torch.tensor([4, 5, 6, 7])
>>> nested_tensor.shape
torch.Size([2, 4])
>>> nested_tensor.size(1)
4
"""
if hasattr(self, "_logical_shape"):
full_size = self._logical_shape
else:
with torch._C.DisableTorchFunctionSubclass():
full_size = torch.Tensor.size(self)
if dim is not None:
dim = dim + len(full_size) if dim < 0 else dim
return full_size[dim]
return full_size
def sum(
self,
dim: int | Sequence[int] | None = None,
keepdim: bool = False,
*,
dtype: torch.dtype | None = None, # type: ignore[name-defined]
) -> Tensor | NestedTensor:
r"""
Returns the sum of each tensor over the given dimension(s).
Args:
dim: The dimension or dimensions to reduce. If None, sum over all dimensions.
Supports int, Sequence[int], or None. Negative dimensions are supported.
keepdim: Whether to retain reduced dimensions with size 1.
dtype: The desired data type of returned tensor.
Returns:
Tensor or NestedTensor depending on the dimensions being reduced.
Examples:
>>> nested_tensor = NestedTensor([torch.tensor([1, 2, 3]), torch.tensor([4, 5])])
>>> nested_tensor.sum()
tensor(15)
>>> nested_tensor.sum(dim=0) # when dim=0, sum across batch dimension
tensor([6, 9])
>>> nested_tensor.sum(dim=1)
tensor([6, 9])
>>> nested_tensor.sum(dim=[0, 1])
tensor(15)
>>> nested_tensor.sum(dim=0, keepdim=True)
tensor([[6, 9]])
>>> nested_tensor.sum(dtype=torch.float32)
tensor(15.)
"""
dims = tuple(dim) if dim is not None and not isinstance(dim, int) else dim
return torch.sum(self, dim=dims, keepdim=keepdim, dtype=dtype)
@property
def T(self) -> Self: # type: ignore[override]
r"""Transpose: reverse per-element dims while keeping batch dim fixed."""
ndims = self.dim()
if ndims <= 1:
return self
batch_dim = 0 if self.batch_first else 1
elem_dims = [d for d in range(ndims) if d != batch_dim]
order = list(reversed(elem_dims))
order.insert(batch_dim, batch_dim)
return cast(Self, torch.permute(self, tuple(order)))
def tolist(self) -> list:
r"""
Convert a NestedTensor to a list of lists of values.
Examples:
>>> nested_tensor = NestedTensor([torch.tensor([1, 2, 3]), torch.tensor([4, 5])])
>>> nested_tensor.tolist()
[[1, 2, 3], [4, 5]]
"""
return [t.tolist() for t in self._storage]
def transpose(self, dim0: int, dim1: int) -> Self: # type: ignore[valid-type]
r"""
Transpose dimensions dim0 and dim1 for each tensor in the NestedTensor.
Args:
dim0: First dimension to transpose (in NestedTensor coordinate system).
dim1: Second dimension to transpose (in NestedTensor coordinate system).
Returns:
NestedTensor: A new NestedTensor with each tensor transposed.
Examples:
>>> nested_tensor = NestedTensor([torch.randn(3, 4), torch.randn(2, 4)])
>>> # NestedTensor shape is [2, 3, 4], underlying tensors are [3, 4] and [2, 4]
>>> transposed = nested_tensor.transpose(1, 2) # transpose dims 1 and 2
>>> transposed.shape # batch dimension is still first
torch.Size([2, 4, 3])
"""
return cast(Self, torch.transpose(self, dim0, dim1))
def swapaxes(self, axis0: int, axis1: int) -> Self:
r"""Alias for `transpose()`."""
return cast(Self, torch.swapaxes(self, axis0, axis1))
def swapdims(self, dim0: int, dim1: int) -> Self:
r"""Alias for `swapaxes()`."""
return cast(Self, torch.swapdims(self, dim0, dim1))
def unsqueeze(self, dim: int) -> Self: # type: ignore[valid-type]
r"""
Unsqueeze each tensor in the NestedTensor by adding a singleton dimension at the specified position.
Args:
dim: The dimension at which to add the singleton dimension. This is in the NestedTensor's
coordinate system (where dim 0 is the batch dimension).
Returns:
NestedTensor: A new NestedTensor with each tensor unsqueezed at the specified dimension.
Examples:
>>> nested_tensor = NestedTensor([torch.tensor([1, 2, 3]), torch.tensor([4, 5])])
>>> # Original shape: [2, 3] (batch_size=2, max_seq_len=3)
>>> unsqueezed = nested_tensor.unsqueeze(1)
>>> unsqueezed.shape
torch.Size([2, 1, 3])
>>> # Now each underlying tensor has shape [1, seq_len] instead of [seq_len]
>>> nested_tensor_2d = NestedTensor([torch.randn(3, 4), torch.randn(2, 4)])
>>> # Original shape: [2, 3, 4] (batch_size=2, max_len1=3, max_len2=4)
>>> unsqueezed_2d = nested_tensor_2d.unsqueeze(2)
>>> unsqueezed_2d.shape
torch.Size([2, 3, 1, 4])
>>> # Now each underlying tensor has shape [len1, 1, len2] instead of [len1, len2]
"""
return cast(Self, torch.unsqueeze(self, dim))
def unflatten(self, dim: int, sizes) -> Self: # type: ignore[valid-type]
r"""Unflatten one dimension of each tensor in the NestedTensor."""
return cast(Self, torch.unflatten(self, dim, sizes))
def roll(self, shifts, dims=None) -> Self:
r"""Roll each tensor in the NestedTensor along the given dimensions."""
return cast(Self, torch.roll(self, shifts, dims=dims))
def rot90(self, k: int = 1, dims: Sequence[int] = (0, 1)) -> Self:
r"""Rotate each tensor in the NestedTensor by 90 degrees in the given plane."""
return cast(Self, torch.rot90(self, k, tuple(dims)))
def view(self, *shape) -> Self:
r"""
View each tensor in the NestedTensor with a different shape.
Args:
*shape (int | tuple[int, ...] | list[int] | torch.Size): Target sizes as separate
integers or one tuple, list or ``torch.Size``. ``-1`` infers a dimension size.
Returns:
NestedTensor: A new NestedTensor with each tensor viewed with the new shape.
Examples:
>>> nested_tensor = NestedTensor([torch.tensor([[1, 2], [3, 4]]), torch.tensor([[5, 6], [7, 8]])])
>>> viewed = nested_tensor.view(4) # View each 2x2 tensor as 4
>>> viewed.shape
torch.Size([2, 4])
>>> type(viewed).__name__
'NestedTensor'
"""
if not shape:
raise TypeError("view() missing shape")
target_shape = shape[0] if len(shape) == 1 and isinstance(shape[0], (tuple, list, torch.Size)) else shape
return NestedTensorAtenRegistry[torch.ops.aten.view.default](
torch.ops.aten.view.default, (self, list(target_shape)), {}
)
def _view_shapes(self, shape) -> list[tuple[int, ...]]: # type: ignore[valid-type]
r"""
Compute per-element view shapes, adjusting ragged dimensions.
Batch-dim detection rules:
1. If ``shape[batch_dim]`` does not match the batch size, batch dim is NOT included.
2. If ``len(shape) != self.dim()``, batch dim IS included (unambiguous).
3. If ``len(shape) == self.dim()`` (ambiguous), batch dim is included only when
at least one other dimension matches max_sizes or is -1.
For ragged dimensions, each target dimension that matches the corresponding
max size is substituted with the element's actual size. When a target dimension
matches a max size at a different position (e.g. after inserting a dim), a
single-candidate search resolves the mapping.
"""
if len(shape) == 1 and isinstance(shape[0], (tuple, list, torch.Size)):
shape = tuple(shape[0])
batch_dim = 0 if self.batch_first else 1
batch_size = len(self)
# Step 1: Determine if batch dim is in the target shape
include_batch = False
if len(shape) > batch_dim:
if shape[batch_dim] == batch_size and len(shape) != self.dim():
include_batch = True
elif shape[batch_dim] in (-1, batch_size) and len(shape) == self.dim():
# Ambiguous: same dim count → confirm via dimension matching
max_sizes = list(self.size()) # type: ignore[arg-type]
if max_sizes:
max_sizes.pop(batch_dim)
non_batch = [i for i in range(len(shape)) if i != batch_dim]
include_batch = any(
j < len(max_sizes) and (shape[d] == -1 or shape[d] == max_sizes[j]) for j, d in enumerate(non_batch)
)
# Step 2: Strip batch dim from target shape
target = list(shape)
if include_batch:
if target[batch_dim] == -1:
target[batch_dim] = batch_size
if target[batch_dim] != batch_size:
raise ValueError(f"Batch dimension mismatch: expected {batch_size} but got {target[batch_dim]}")
target.pop(batch_dim)
# Step 3: Per-element shape adjustment (ragged dim substitution)
max_sizes = list(self.size()) # type: ignore[arg-type]
if max_sizes:
max_sizes.pop(batch_dim)
element_shapes = self._element_shapes
if element_shapes is None:
if _is_compiling() or _is_fake_tensor(self._physical_shape):
_compile_unsupported(
"NestedTensor view/reshape",
"tensor-backed per-element view shape remapping is not implemented",
)
element_shapes = tuple(tuple(shape) for shape in self._original_shapes())
view_shapes = []
for element_shape in element_shapes:
adjusted = list(target)
available = list(range(len(max_sizes)))
for i in range(min(len(adjusted), len(max_sizes))):
if adjusted[i] == -1:
continue
# Direct match: same position in max_sizes
if adjusted[i] == max_sizes[i]:
adjusted[i] = element_shape[i]
if i in available:
available.remove(i)
continue
# Indirect match: search remaining positions for unique candidate
candidates = [j for j in available if max_sizes[j] == adjusted[i]]
if len(candidates) == 1:
j = candidates[0]
adjusted[i] = element_shape[j]
available.remove(j)
if adjusted.count(-1) == 1:
missing = adjusted.index(-1)
known = 1
for dim in adjusted:
if dim != -1:
known *= dim
element_numel = type(self)._shape_numel(element_shape)
if known != 0 and element_numel % known == 0:
adjusted[missing] = element_numel // known
view_shapes.append(tuple(adjusted))
return view_shapes
def where(self, condition: Tensor, other: Tensor | int | float | complex | bool) -> Self:
r"""
Return a NestedTensor of elements selected from either self or other, depending on condition.
Examples:
>>> nested_tensor = NestedTensor([torch.tensor([1, 2, 3]), torch.tensor([4, 5])])
>>> nested_tensor.where(nested_tensor > 2, torch.tensor([[6, 5, 4], [3, 2, 1]]))
NestedTensor([
[6, 5, 3],
[4, 5]
])
>>> nested_tensor.where(nested_tensor > 2, NestedTensor([[6, 5, 4], [3, 2]]))
NestedTensor([
[6, 5, 3],
[4, 5]
])
>>> nested_tensor.where(torch.tensor(True), NestedTensor([[6, 5, 4], [3, 2]]))
NestedTensor([
[1, 2, 3],
[4, 5]
])
"""
return cast(Self, torch.where(condition, self, other))
def cdist(
self,
other: Tensor | NestedTensor,
p: float = 2.0,
compute_mode: str = "use_mm_for_euclid_dist_if_necessary",
) -> NestedTensor:
r"""Compute per-sample pairwise distances through the traceable packed path.
This method shares :func:`torch.cdist`'s ``p`` and ``compute_mode``
contract. Unlike the built-in spelling, Dynamo can inline this explicit
NestedTensor handler, so AOT/Inductor preserve gradients to prebuilt
packed leaves when the result is consumed inside the compiled region.
Wrapper-only compiled outputs retain their outer autograd edge, and
:attr:`concat` projects that edge back to packed values without padding.
"""
from .torch_functions import cdist
return cdist(self, other, p, compute_mode)
def cumprod(self, dim: int, *, dtype: torch.dtype | None = None) -> NestedTensor:
r"""Compute cumulative products through the traceable packed segmented path.
The explicit method keeps gradients connected to prebuilt packed leaves
across AOTAutograd/Inductor, including wrapper-only compiled outputs.
"""
op = torch.ops.aten.cumprod.default
kwargs = {} if dtype is None else {"dtype": dtype}
return NestedTensorAtenRegistry[op](op, (self, dim), kwargs)
|