File: gen_pyi.py

package info (click to toggle)
pytorch-cuda 2.6.0%2Bdfsg-7
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 161,620 kB
  • sloc: python: 1,278,832; cpp: 900,322; ansic: 82,710; asm: 7,754; java: 3,363; sh: 2,811; javascript: 2,443; makefile: 597; ruby: 195; xml: 84; objc: 68
file content (1504 lines) | stat: -rw-r--r-- 56,101 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
from __future__ import annotations

import argparse
import collections
import importlib
import sys
from pprint import pformat
from typing import Sequence
from unittest.mock import Mock, patch
from warnings import warn

from tools.autograd.gen_python_functions import (
    group_overloads,
    load_signatures,
    should_generate_py_binding,
)

from torchgen.api.python import (
    PythonSignatureGroup,
    PythonSignatureNativeFunctionPair,
    returns_structseq_pyi,
)
from torchgen.gen import parse_native_yaml, parse_tags_yaml
from torchgen.model import _TorchDispatchModeKey, DispatchKey, Variant
from torchgen.utils import FileManager


"""
This module implements generation of type stubs for PyTorch,
enabling use of autocomplete in IDEs like PyCharm, which otherwise
don't understand C extension modules.

At the moment, this module only handles type stubs for torch and
torch.Tensor.  It should eventually be expanded to cover all functions
which come are autogenerated.

Here's our general strategy:

- We start off with a hand-written __init__.pyi.in file.  This
  file contains type definitions for everything we cannot automatically
  generate, including pure Python definitions directly in __init__.py
  (the latter case should be pretty rare).

- We go through automatically bound functions based on the
  type information recorded in native_functions.yaml and
  generate type hints for them (generate_type_hints)

There are a number of type hints which we've special-cased;
read gen_pyi for the gory details.
"""


def get_py_torch_functions(
    python_funcs: Sequence[PythonSignatureNativeFunctionPair],
    method: bool = False,
) -> Sequence[PythonSignatureGroup]:
    """
    Get declarations (grouped by name) which should be generated
    as either functions in the "torch" module or methods on Tensor.
    """

    def should_bind_function(python_func: PythonSignatureNativeFunctionPair) -> bool:
        return (
            should_generate_py_binding(python_func.function)
            and not python_func.function.python_module
            and Variant.function in python_func.function.variants
        )

    def should_bind_method(python_func: PythonSignatureNativeFunctionPair) -> bool:
        return (
            should_generate_py_binding(python_func.function)
            and not python_func.function.python_module
            and Variant.method in python_func.function.variants
        )

    should_bind = should_bind_method if method else should_bind_function
    return group_overloads([f for f in python_funcs if should_bind(f)])


# TODO: Consider defining some aliases for our Union[...] types, to make
# the stubs to read on the human eye.

DEVICE_PARAM = "device: Optional[DeviceLikeType] = None"
FACTORY_PARAMS = f"dtype: Optional[_dtype] = None, {DEVICE_PARAM}, requires_grad: _bool = False, pin_memory: _bool = False"

# NOTE: specifying indices for Tensor.__getitem__
# We can imitate numpy's definition of ndarray.__getitem__ found in numpy/__init__.pyi:
#
# key: (
#     None
#     | slice
#     | ellipsis
#     | SupportsIndex
#     | _ArrayLikeInt_co
#     | tuple[None | slice | ellipsis | _ArrayLikeInt_co | SupportsIndex, ...]
# )
#
# where:
#
# _ArrayLikeInt_co = _DualArrayLike[
#     dtype[Union[bool_, integer[Any]]],
#     Union[bool, int],
# ]
#
# and
#
# _DualArrayLike = Union[
#     _SupportsArray[_DType],
#     _NestedSequence[_SupportsArray[_DType]],
#     _T,
#     _NestedSequence[_T],
# ]
#
# Moreover, _NestedSequence is a Protocol that matches arbitrary nesting of list/tuple.
# We can substitute and simplify:
# _SupportsArray -> Tensor
# _ArrayLikeInt_co -> [bool | int | | Tensor | NestedSequence[bool | int] | NestedSequence[Tensor]]
# which leaves us with key: T | tuple[T, ...], where T is:
# T = (
#     None | bool | int | slice | ellipsis | SupportsIndex
#     | Tensor | _NestedSequence[Tensor] | _NestedSequence[bool | int]
# )

# NOTE: ellipsis is equal to type[Ellipsis] in stub files.
_leaf_types = "Union[None, _bool, _int, slice, ellipsis, Tensor]"  # not SupportsIndex!
_index = f"Union[SupportsIndex, {_leaf_types}, _NestedSequence[{_leaf_types}]]"
INDICES = f"indices: Union[{_index}, tuple[{_index}, ...]]"

blocklist = [
    "__init_subclass__",
    "__new__",
    "__subclasshook__",
    "cdist",
    "device",
    "grad",
    "requires_grad",
    "range",
    # defined in functional
    "einsum",
    # Somehow, these are defined in both _C and in functional. Ick!
    "broadcast_tensors",
    # Manually define named tensor type stubs in __init__.pyi.in
    "align_tensors",
    "meshgrid",
    "cartesian_prod",
    "block_diag",
    "norm",
    "chain_matmul",
    "stft",
    "tensordot",
    "split",
    "unique_consecutive",
    "atleast_1d",
    "atleast_2d",
    "atleast_3d",
    # These are handled specially by python_arg_parser.cpp
    "add",
    "add_",
    "add_out",
    "sub",
    "sub_",
    "sub_out",
    "mul",
    "mul_",
    "mul_out",
    "div",
    "div_",
    "div_out",
    "true_divide",
    "true_divide_",
    "true_divide_out",
    "floor_divide",
    "floor_divide_",
    "floor_divide_out",
    "to",
    "_to_copy",
    "copy_",
]

shift_ops = (
    "lshift",
    "rshift",
    "ilshift",
    "irshift",  # inplace ops
)
arithmetic_ops = (
    "add",
    "sub",
    "mul",
    "div",
    "pow",
    "mod",
    "truediv",
    "matmul",
    "floordiv",
    "radd",
    "rsub",
    "rmul",
    "rtruediv",
    "rfloordiv",
    "rpow",  # reverse arithmetic
    "iadd",
    "idiv",
    "imul",
    "isub",
    "ifloordiv",
    "imod",  # inplace ops
)
logic_ops = (
    "and",
    "or",
    "xor",
    "rand",
    "ror",
    "rxor",  # reverse logic
    "iand",
    "ior",
    "ixor",  # inplace ops
)
binary_ops = shift_ops + arithmetic_ops + logic_ops

symmetric_comparison_ops = ("eq", "ne")
asymmetric_comparison_ops = ("ge", "gt", "lt", "le")
comparison_ops = symmetric_comparison_ops + asymmetric_comparison_ops

unary_ops = ("neg", "abs", "invert")
to_py_type_ops = ("bool", "float", "complex", "long", "index", "int", "nonzero")
all_ops = binary_ops + comparison_ops + unary_ops + to_py_type_ops


def sig_for_ops(opname: str) -> list[str]:
    """sig_for_ops(opname : str) -> List[str]

    Returns signatures for operator special functions (__add__ etc.)"""

    # we have to do this by hand, because they are hand-bound in Python

    assert opname.endswith("__") and opname.startswith("__"), f"Unexpected op {opname}"

    name = opname[2:-2]
    if name == "rpow":
        return [  # somehow required to make mypy ci happy?
            f"def {opname}(self, other: Union[Tensor, Number, _complex]) -> Tensor: ... # type: ignore[has-type]"
        ]
    elif name in arithmetic_ops:
        return [
            f"def {opname}(self, other: Union[Tensor, Number, _complex]) -> Tensor: ..."
        ]
    elif name in logic_ops:
        return [f"def {opname}(self, other: Union[Tensor, _bool]) -> Tensor: ..."]
    elif name in shift_ops:
        return [f"def {opname}(self, other: Union[Tensor, _int]) -> Tensor: ..."]
    elif name in symmetric_comparison_ops:
        return [
            # unsafe override https://github.com/python/mypy/issues/5704
            f"def {opname}(self, other: Union[Tensor, Number, _complex]) -> Tensor: ...  # type: ignore[override]",
            f"def {opname}(self, other: Any) -> _bool: ...",
        ]
    elif name in asymmetric_comparison_ops:
        return [
            f"def {opname}(self, other: Union[Tensor, Number, _complex]) -> Tensor: ..."
        ]
    elif name in unary_ops:
        return [f"def {opname}(self) -> Tensor: ..."]
    elif name in to_py_type_ops:
        if name in {"bool", "float", "complex"}:
            tname = name
        elif name == "nonzero":
            tname = "bool"
        else:
            tname = "int"
        if tname in {"float", "int", "bool", "complex"}:
            tname = "builtins." + tname
        return [f"def {opname}(self) -> {tname}: ..."]
    else:
        raise Exception("unknown op", opname)  # noqa: TRY002


def generate_type_hints(sig_group: PythonSignatureGroup) -> list[str]:
    type_hints: list[str] = []

    # Some deprecated ops that are on the blocklist are still included in pyi
    if sig_group.signature.name in blocklist and not sig_group.signature.deprecated:
        return type_hints

    # deprecated signatures have separate entries for their functional and out variants
    # (as opposed to the native ops, which fuse the two into a single signature).
    # generate the functional variant here, if an out variant exists.
    if sig_group.signature.deprecated and sig_group.outplace is not None:
        type_hint = sig_group.signature.signature_str_pyi(skip_outputs=True)
        type_hints.append(type_hint)

    # PythonSignatureGroups that have both a functional + out variant get a single signature, with an optional out argument
    # Generates the out variant if one exists. Otherwise, generate the functional variant
    type_hint = sig_group.signature.signature_str_pyi(
        skip_outputs=sig_group.outplace is None
    )
    type_hints.append(type_hint)

    # Some operators also additionally have a vararg variant of their signature
    type_hint_vararg = sig_group.signature.signature_str_pyi_vararg(
        skip_outputs=sig_group.outplace is None
    )
    if type_hint_vararg:
        type_hints.append(type_hint_vararg)

    return type_hints


def get_max_pool_dispatch(name: str, arg_list: list[str]) -> dict[str, list[str]]:
    flag_pos = arg_list.index("{return_indices}")
    # If return_indices is positional arg, everything before should have no default
    arg_list_positional = (
        [
            ", ".join(single_arg.split(" = ")[0] for single_arg in arg.split(", "))
            for arg in arg_list[: flag_pos + 1]
        ]
        + ["/"]
        + arg_list[flag_pos + 1 :]
    )
    # Otherwise force return_indices to be kwarg
    arg_list_keyword = arg_list.copy()
    arg_list_keyword.insert(flag_pos, "*")
    tmpl = "def {name}({args}) -> {{return_type}}: ..."
    return {
        name: [
            tmpl.format(name=name, args=", ".join(arg_list)).format(
                return_indices="return_indices: Literal[False] = False",
                return_type="Tensor",
            ),
            tmpl.format(name=name, args=", ".join(arg_list_positional)).format(
                return_indices="return_indices: Literal[True]",
                return_type="Tuple[Tensor, Tensor]",
            ),
            tmpl.format(name=name, args=", ".join(arg_list_keyword)).format(
                return_indices="return_indices: Literal[True]",
                return_type="Tuple[Tensor, Tensor]",
            ),
        ]
    }


def gen_nn_functional(fm: FileManager) -> None:
    INPUT = "input: Tensor"
    KERNEL_SIZE = "kernel_size: Union[_int, _size]"
    STRIDE_PADDING = ", ".join(
        [
            "stride: Optional[Union[_int, _size]] = None",
            "padding: Union[_int, _size] = 0",
        ]
    )

    # TODO the list for `torch._C._nn` is nonexhaustive
    unsorted_c_nn_function_hints: dict[str, list[str]] = {}

    for d in (2, 3):
        unsorted_c_nn_function_hints.update(
            {
                f"avg_pool{d}d": [
                    f"def avg_pool{d}d({{}}) -> Tensor: ...".format(
                        ", ".join(
                            [
                                f"{INPUT}",
                                f"{KERNEL_SIZE}",
                                f"{STRIDE_PADDING}",
                                "ceil_mode: bool = False",
                                "count_include_pad: bool = True",
                                "divisor_override: Optional[int] = None",
                            ]
                        )
                    )
                ],
                f"fractional_max_pool{d}d": [
                    f"def fractional_max_pool{d}d({{}}) -> {{}}: ...".format(
                        ", ".join(
                            [
                                f"{INPUT}",
                                f"{KERNEL_SIZE}",
                                "output_size: Union[_int, _size]",
                                "_random_samples: Tensor",
                            ]
                        ),
                        "Tuple[Tensor, Tensor]",
                    )
                ],
                f"adaptive_max_pool{d}d": [
                    f"def adaptive_max_pool{d}d({{}}) -> {{}}: ...".format(
                        ", ".join([f"{INPUT}", "output_size: Union[_int, _size]"]),
                        "Tuple[Tensor, Tensor]",
                    )
                ],
            }
        )

    unsorted_c_nn_function_hints.update(
        {
            "hardtanh": [
                "def hardtanh({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "input: Tensor",
                            "min_val: float = ...",
                            "max_val: float = ...",
                            "*",
                            "out: Optional[Tensor] = None",
                        ]
                    )
                )
            ],
            "hardtanh_": [
                "def hardtanh_({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "input: Tensor",
                            "min_val: float = ...",
                            "max_val: float = ...",
                        ]
                    )
                )
            ],
            "elu_": ["def elu_(input: Tensor, alpha: float = ...) -> Tensor: ..."],
            "leaky_relu": [
                "def leaky_relu({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "input: Tensor",
                            "negative_slope: float = ...",
                            "*",
                            "out: Optional[Tensor] = None",
                        ]
                    )
                )
            ],
            "leaky_relu_": [
                f"def leaky_relu_({', '.join(['input: Tensor', 'negative_slope: float = ...'])}) -> Tensor: ..."
            ],
            "log_sigmoid": ["def log_sigmoid(input: Tensor) -> Tensor: ..."],
            "gelu": ["def gelu(input: Tensor, approximate: str = ...) -> Tensor: ..."],
            "softplus": [
                "def softplus({}) -> Tensor: ...".format(
                    ", ".join(
                        ["input: Tensor", "beta: float = ...", "threshold: float = ..."]
                    )
                )
            ],
            "softshrink": [
                "def softshrink(input: Tensor, lambd: float = ...) -> Tensor: ..."
            ],
            "hardsigmoid": [
                f"def hardsigmoid({', '.join(['input: Tensor', '*', 'out: Optional[Tensor] = None'])}) -> Tensor: ..."
            ],
            "linear": [
                "def linear({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "input: Tensor",
                            "weight: Tensor",
                            "bias: Optional[Tensor] = None",
                        ]
                    )
                )
            ],
            "pad": [
                "def pad({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "input: Tensor",
                            "pad: Sequence[int]",
                            "mode: str = ...",
                            "value: Optional[float] = None",
                        ]
                    )
                )
            ],
            "one_hot": [
                "def one_hot(tensor: Tensor, num_classes: int = ...) -> Tensor: ..."
            ],
            "scaled_dot_product_attention": [
                "def scaled_dot_product_attention({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "query: Tensor",
                            "key: Tensor",
                            "value: Tensor",
                            "attn_mask: Optional[Tensor] = None",
                            "dropout_p: float = 0.0",
                            "is_causal: bool = False",
                            "scale: Optional[float] = None",
                            "enable_gqa: bool = False",
                        ]
                    )
                )
            ],
        }
    )

    c_nn_function_hints: list[str] = []
    for _, hints in sorted(unsorted_c_nn_function_hints.items()):
        if len(hints) > 1:
            hints = ["@overload\n" + h for h in hints]
        c_nn_function_hints += hints

    # Functions imported into `torch.nn.functional` from `torch`, perhaps being filtered
    # through an `_add_docstr` call
    torch_imports = [
        "conv1d",
        "conv2d",
        "conv3d",
        "conv_transpose1d",
        "conv_transpose2d",
        "conv_transpose3d",
        "conv_tbc",
        "avg_pool1d",
        "adaptive_avg_pool1d",
        "relu_",
        "selu_",
        "celu_",
        "prelu",
        "rrelu_",
        "hardshrink",
        "bilinear",
        "pixel_shuffle",
        "pixel_unshuffle",
        "channel_shuffle",
        "native_channel_shuffle",
        "pairwise_distance",
        "pdist",
        "cosine_similarity",
    ]
    imported_hints = [f"from torch import {_} as {_}" for _ in torch_imports]

    # Functions imported into `torch.nn.functional` from `torch._C._nn`
    c_nn_imports = [
        "avg_pool2d",
        "avg_pool3d",
        "hardtanh_",
        "elu_",
        "leaky_relu_",
        "gelu",
        "softplus",
        "softshrink",
        "linear",
        "pad",
        "one_hot",
        "scaled_dot_product_attention",
    ]
    imported_hints += [f"from torch._C._nn import {_} as {_}" for _ in c_nn_imports]
    # This is from `torch._C._nn` but renamed
    imported_hints.append(
        "from torch._C._nn import log_sigmoid\nlogsigmoid = log_sigmoid"
    )

    # Functions generated by `torch._jit_internal.boolean_dispatch` in `nn.functional`
    unsorted_dispatched_hints: dict[str, list[str]] = {}

    for d in (1, 2, 3):
        unsorted_dispatched_hints.update(
            **get_max_pool_dispatch(
                f"max_pool{d}d",
                [
                    f"{INPUT}",
                    f"{KERNEL_SIZE}",
                    f"{STRIDE_PADDING}",
                    "dilation: Union[_int, _size] = 1",
                    "ceil_mode: bool = False",
                    "{return_indices}",
                ],
            ),
            **get_max_pool_dispatch(
                f"fractional_max_pool{d}d",
                [
                    f"{INPUT}",
                    f"{KERNEL_SIZE}",
                    "output_size: Optional[Union[_int, _size]] = None",
                    "output_ratio: Optional[_ratio_any_t] = None",
                    "{return_indices}",
                    "_random_samples: Optional[Tensor] = None",
                ],
            ),
            **get_max_pool_dispatch(
                f"adaptive_max_pool{d}d",
                [f"{INPUT}", "output_size: Union[_int, _size]", "{return_indices}"],
            ),
        )

    # There's no fractional_max_pool1d
    del unsorted_dispatched_hints["fractional_max_pool1d"]

    dispatched_hints: list[str] = []
    for _, hints in sorted(unsorted_dispatched_hints.items()):
        if len(hints) > 1:
            hints = ["@overload\n" + h for h in hints]
        dispatched_hints += hints

    fm.write_with_template(
        "torch/nn/functional.pyi",
        "torch/nn/functional.pyi.in",
        lambda: {
            "imported_hints": imported_hints,
            "dispatched_hints": dispatched_hints,
        },
    )
    fm.write_with_template(
        "torch/_C/_nn.pyi",
        "torch/_C/_nn.pyi.in",
        lambda: {
            "c_nn_function_hints": c_nn_function_hints,
        },
    )


"""
We gather the docstrings for torch with the following steps:
1. Mock torch and torch._C, which are the only dependencies of the docs files
2. Mock the _add_docstr function to save the docstrings
3. Import the docs files to trigger mocked _add_docstr and collect docstrings
"""


def gather_docstrs() -> dict[str, str]:
    docstrs = {}

    def mock_add_docstr(func: Mock, docstr: str) -> None:
        docstrs[func._extract_mock_name()] = docstr.strip()

    # sys.modules and sys.path are restored after the context manager exits
    with patch.dict(sys.modules), patch.object(sys, "path", sys.path + ["torch"]):
        # mock the torch module and torch._C._add_docstr
        sys.modules["torch"] = Mock(name="torch")
        sys.modules["torch._C"] = Mock(_add_docstr=mock_add_docstr)

        try:
            # manually import torch._torch_docs and torch._tensor_docs to trigger
            # the mocked _add_docstr and collect docstrings
            sys.modules["torch._torch_docs"] = importlib.import_module("_torch_docs")
            sys.modules["torch._tensor_docs"] = importlib.import_module("_tensor_docs")
        except ModuleNotFoundError:
            # Gracefully fail if these modules are not importable
            warn(
                "Failed to import _torch_docs/_tensor_docs, skipping docstring in pyi files."
            )

    return docstrs


def add_docstr_to_hint(docstr: str, hint: str) -> str:
    if "..." in hint:  # function or method
        assert hint.endswith("..."), f"Hint `{hint}` does not end with '...'"
        hint = hint[:-3]  # remove "..."
        return "\n    ".join([hint, 'r"""'] + docstr.split("\n") + ['"""', "..."])
    else:  # attribute or property
        return f'{hint}\nr"""{docstr}"""\n'


def gen_pyi(
    native_yaml_path: str,
    tags_yaml_path: str,
    deprecated_yaml_path: str,
    fm: FileManager,
) -> None:
    """gen_pyi()

    This function generates a pyi file for torch.
    """

    # Some of this logic overlaps with generate_python_signature in
    # tools/autograd/gen_python_functions.py; however, this
    # function is all about generating mypy type signatures, whereas
    # the other function generates are custom format for argument
    # checking.  If you are update this, consider if your change
    # also needs to update the other file.

    # Dictionary for NamedTuple definitions
    structseqs: dict[str, str] = {}

    # Generate type signatures for top-level functions
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    unsorted_function_hints: dict[str, list[str]] = collections.defaultdict(list)

    for n, n1, n2 in [
        ("csr", "crow", "col"),
        ("csc", "ccol", "row"),
        ("bsr", "crow", "col"),
        ("bsc", "ccol", "row"),
    ]:
        unsorted_function_hints.update(
            {
                f"sparse_{n}_tensor": [
                    f"def sparse_{n}_tensor({{}}) -> Tensor: ...".format(
                        ", ".join(
                            [
                                f"{n1}_indices: Union[Tensor, List]",
                                f"{n2}_indices: Union[Tensor, List]",
                                "values: Union[Tensor, List]",
                                "size: Optional[_size] = None",
                                "*",
                                "dtype: Optional[_dtype] = None",
                                "device: Optional[DeviceLikeType] = None",
                                "requires_grad: _bool = False",
                                "check_invariants: Optional[_bool] = None",
                            ]
                        ),
                    )
                ],
            }
        )

    unsorted_function_hints.update(
        {
            "set_flush_denormal": ["def set_flush_denormal(mode: _bool) -> _bool: ..."],
            "get_default_dtype": ["def get_default_dtype() -> _dtype: ..."],
            "asarray": [
                "def asarray({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "obj: Any",
                            "*",
                            "dtype: Optional[_dtype] = None",
                            "device: Optional[DeviceLikeType] = None",
                            "copy: Optional[_bool] = None",
                            "requires_grad: _bool = False",
                        ]
                    )
                )
            ],
            "from_numpy": ["def from_numpy(ndarray) -> Tensor: ..."],
            "frombuffer": [
                "def frombuffer({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "buffer: Any",
                            "*",
                            "dtype: _dtype",
                            "count: int = -1",
                            "offset: int = 0",
                            "requires_grad: _bool = False",
                        ]
                    )
                )
            ],
            "numel": ["def numel(self: Tensor) -> _int: ..."],
            "as_tensor": [
                "def as_tensor({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "data: Any",
                            "dtype: Optional[_dtype] = None",
                            DEVICE_PARAM,
                        ]
                    )
                )
            ],
            "get_num_threads": ["def get_num_threads() -> _int: ..."],
            "set_num_threads": ["def set_num_threads(num: _int) -> None: ..."],
            "init_num_threads": ["def init_num_threads() -> None: ..."],
            "get_num_interop_threads": ["def get_num_interop_threads() -> _int: ..."],
            "set_num_interop_threads": [
                "def set_num_interop_threads(num: _int) -> None: ..."
            ],
            # These functions are explicitly disabled by
            # SKIP_PYTHON_BINDINGS because they are hand bound.
            # Correspondingly, we must hand-write their signatures.
            "tensor": [f"def tensor(data: Any, {FACTORY_PARAMS}) -> Tensor: ..."],
            "sparse_coo_tensor": [
                "def sparse_coo_tensor({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "indices: Tensor",
                            "values: Union[Tensor, List]",
                            "size: Optional[_size] = None",
                            "*",
                            "dtype: Optional[_dtype] = None",
                            "device: Optional[DeviceLikeType] = None",
                            "requires_grad: _bool = False",
                            "check_invariants: Optional[_bool] = None",
                            "is_coalesced: Optional[_bool] = None",
                        ]
                    )
                )
            ],
            "sparse_compressed_tensor": [
                "def sparse_compressed_tensor({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "compressed_indices: Union[Tensor, List]",
                            "plain_indices: Union[Tensor, List]",
                            "values: Union[Tensor, List]",
                            "size: Optional[_size] = None",
                            "*",
                            "dtype: Optional[_dtype] = None",
                            "layout: Optional[_layout] = None",
                            "device: Optional[DeviceLikeType] = None",
                            "requires_grad: _bool = False",
                            "check_invariants: Optional[_bool] = None",
                        ]
                    )
                )
            ],
            "_sync": ["def _sync(t: Tensor) -> None: ..."],
            "_is_functional_tensor": [
                "def _is_functional_tensor(t: Tensor) -> _bool: ..."
            ],
            "_is_functional_tensor_base": [
                "def _is_functional_tensor_base(t: Tensor) -> _bool: ..."
            ],
            "_from_functional_tensor": [
                "def _from_functional_tensor(t: Tensor) -> Tensor: ..."
            ],
            "_to_functional_tensor": [
                "def _to_functional_tensor(t: Tensor) -> Tensor: ..."
            ],
            "_functionalize_replace": [
                "def _functionalize_replace(self_: Tensor, other: Tensor) -> None: ..."
            ],
            "_functionalize_commit_update": [
                "def _functionalize_commit_update(t: Tensor) -> None: ..."
            ],
            "_functionalize_unsafe_set": [
                "def _functionalize_unsafe_set(dst: Tensor, src: Tensor) -> None: ..."
            ],
            "_functionalize_mark_mutation_hidden_from_autograd": [
                "def _functionalize_mark_mutation_hidden_from_autograd(t: Tensor) -> None: ..."
            ],
            "_functionalize_are_all_mutations_hidden_from_autograd": [
                "def _functionalize_are_all_mutations_hidden_from_autograd(t: Tensor) -> _bool: ..."
            ],
            "_functionalize_are_all_mutations_under_no_grad_or_inference_mode": [
                "def _functionalize_are_all_mutations_under_no_grad_or_inference_mode(t: Tensor) -> _bool: ..."
            ],
            "_functionalize_was_inductor_storage_resized": [
                "def _functionalize_was_inductor_storage_resized(t: Tensor) -> _bool: ..."
            ],
            "_functionalize_sync": ["def _functionalize_sync(t: Tensor) -> None: ..."],
            "_functionalize_was_storage_changed": [
                "def _functionalize_was_storage_changed(tensor: Tensor) -> _bool: ..."
            ],
            "_functionalize_set_storage_changed": [
                "def _functionalize_set_storage_changed(tensor: Tensor) -> _bool: ..."
            ],
            "_functionalize_has_metadata_mutation": [
                "def _functionalize_has_metadata_mutation(tensor: Tensor) -> _bool: ..."
            ],
            "_functionalize_apply_view_metas": [
                "def _functionalize_apply_view_metas(tensor: Tensor,  base: Tensor) -> Tensor: ..."
            ],
            "_functionalize_is_symbolic": [
                "def _functionalize_is_symbolic(tensor: Tensor) -> _bool: ..."
            ],
            "_enable_functionalization": [
                "def _enable_functionalization(*, reapply_views: _bool = False): ..."
            ],
            "_disable_functionalization": ["def _disable_functionalization(): ..."],
            "range": [
                "def range({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "start: Number",
                            "end: Number",
                            "step: Number = 1",
                            "*",
                            "out: Optional[Tensor] = None",
                            FACTORY_PARAMS,
                        ]
                    )
                )
            ],
            "arange": [
                "def arange({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "start: Number",
                            "end: Number",
                            "step: Number",
                            "*",
                            "out: Optional[Tensor] = None",
                            FACTORY_PARAMS,
                        ]
                    )
                ),
                "def arange({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "start: Number",
                            "end: Number",
                            "*",
                            "out: Optional[Tensor] = None",
                            FACTORY_PARAMS,
                        ]
                    )
                ),
                "def arange({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "end: Number",
                            "*",
                            "out: Optional[Tensor] = None",
                            FACTORY_PARAMS,
                        ]
                    )
                ),
            ],
            "linspace": [
                "def linspace({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "start: Number",
                            "end: Number",
                            "steps: Optional[_int] = None",
                            "*",
                            "out: Optional[Tensor] = None",
                            FACTORY_PARAMS,
                        ]
                    )
                )
            ],
            "logspace": [
                "def logspace({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "start: Number",
                            "end: Number",
                            "steps: Optional[_int] = None",
                            "base: _float = 10.0",
                            "*",
                            "out: Optional[Tensor] = None",
                            FACTORY_PARAMS,
                        ]
                    )
                )
            ],
            "randint": [
                "def randint({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "low: _int",
                            "high: _int",
                            "size: _size",
                            "*",
                            "generator: Optional[Generator] = None",
                            FACTORY_PARAMS,
                        ]
                    )
                ),
                "def randint({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "high: _int",
                            "size: _size",
                            "*",
                            "generator: Optional[Generator] = None",
                            FACTORY_PARAMS,
                        ]
                    )
                ),
            ],
            "full": [
                "def full({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "size: _size",
                            "fill_value: Union[Number, _complex]",
                            "*",
                            "out: Optional[Tensor] = None",
                            "layout: _layout = strided",
                            FACTORY_PARAMS,
                        ]
                    )
                ),
                "def full({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "size: _size",
                            "fill_value: Union[Number, _complex]",
                            "*",
                            "names: List[Union[str, None]]",
                            "layout: _layout = strided",
                            FACTORY_PARAMS,
                        ]
                    )
                ),
            ],
            "is_grad_enabled": ["def is_grad_enabled() -> _bool: ..."],
            "is_inference_mode_enabled": [
                "def is_inference_mode_enabled() -> _bool: ..."
            ],
            "nonzero": [
                "def nonzero(input: Tensor, *, as_tuple: Literal[False] = False, out: Optional[Tensor] = None) -> Tensor: ...",
                "def nonzero(input: Tensor, *, as_tuple: Literal[True]) -> Tuple[Tensor, ...]: ...",
            ],
            "dsmm": ["def dsmm(input: Tensor, mat2: Tensor) -> Tensor: ..."],
            "hsmm": ["def hsmm(input: Tensor, mat2: Tensor) -> Tensor: ..."],
            "saddmm": [
                "def saddmm({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "input: Tensor",
                            "mat1: Tensor",
                            "mat2: Tensor",
                            "*",
                            "beta: Number = 1",
                            "alpha: Number = 1",
                            "out: Optional[Tensor] = None",
                        ]
                    )
                )
            ],
            "spmm": ["def spmm(input: Tensor, mat2: Tensor) -> Tensor: ..."],
            "div": [
                "def div({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "input: Union[Tensor, Number]",
                            "other: Union[Tensor, Number]",
                            "*",
                            "rounding_mode: Optional[str] = None",
                            "out: Optional[Tensor] = None",
                        ]
                    )
                )
            ],
        }
    )
    for binop in ["true_divide", "floor_divide"]:
        unsorted_function_hints[binop].append(
            f"def {binop}(input: Union[Tensor, Number], other: Union[Tensor, Number], "
            "*, out: Optional[Tensor] = None) -> Tensor: ..."
        )
    for binop in ["mul"]:
        unsorted_function_hints[binop].append(
            f"def {binop}(input: Union[Tensor, Number, _complex], other: Union[Tensor, Number, _complex], "
            "*, out: Optional[Tensor] = None) -> Tensor: ..."
        )
    for binop in ["add", "sub"]:
        unsorted_function_hints[binop].append(
            f"def {binop}(input: Union[Tensor, Number, _complex], other: Union[Tensor, Number, _complex], "
            "*, alpha: Optional[Union[Number, _complex]] = 1, out: Optional[Tensor] = None) -> Tensor: ..."
        )

    native_functions = parse_native_yaml(
        native_yaml_path, tags_yaml_path
    ).native_functions
    native_functions = list(filter(should_generate_py_binding, native_functions))

    function_signatures = load_signatures(
        native_functions, deprecated_yaml_path, method=False, pyi=True
    )
    sig_groups = get_py_torch_functions(function_signatures)
    for group in sorted(sig_groups, key=lambda g: g.signature.name):
        name = group.signature.name
        unsorted_function_hints[name] += generate_type_hints(group)

        structseq = returns_structseq_pyi(group.signature)
        if structseq is not None and not group.signature.deprecated:
            # deprecated structseqs are currently not included for torch functions
            tuple_name, tuple_def = structseq
            if tuple_name in structseqs:
                assert structseqs[tuple_name] == tuple_def
            else:
                structseqs[tuple_name] = tuple_def

    def replace_special_case(hint: str) -> str:
        # NB: Keep this in sync with enum in aten/src/ATen/core/Reduction.h
        hint = hint.replace("at::Reduction::Mean", "1")
        hint = hint.replace(": Tensor = None", ": Optional[Tensor] = None")
        return hint

    docstrs = gather_docstrs()
    function_hints = []
    for name, hints in sorted(unsorted_function_hints.items()):
        hints = [replace_special_case(h) for h in hints]
        if len(hints) > 1:
            hints = ["@overload\n" + h for h in hints]
        docstr = docstrs.get(f"torch.{name}")
        if docstr is not None:
            hints = [add_docstr_to_hint(docstr, h) for h in hints]
        function_hints += hints

    # Generate type signatures for Tensor methods
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    unsorted_tensor_method_hints: dict[str, list[str]] = collections.defaultdict(list)
    unsorted_tensor_method_hints.update(
        {
            "size": [
                "def size(self, dim: None = None) -> Size: ...",
                "def size(self, dim: _int) -> _int: ...",
            ],
            "stride": [
                "def stride(self, dim: None = None) -> Tuple[_int, ...]: ...",
                "def stride(self, dim: _int) -> _int: ...",
            ],
            "new_ones": [
                f"def new_ones(self, size: _size, {FACTORY_PARAMS}) -> Tensor: ..."
            ],
            "new_tensor": [
                f"def new_tensor(self, data: Any, {FACTORY_PARAMS}) -> Tensor: ..."
            ],
            "__new__": ["def __new__(cls, *args, **kwargs) -> Self: ..."],
            # new and __init__ have the same signatures differ only in return type
            # Adapted from legacy_tensor_ctor and legacy_tensor_new
            "new": [
                f"def new(cls, *args: Any, {DEVICE_PARAM}) -> Self: ...",
                "def new(cls, storage: Storage) -> Self: ...",
                "def new(cls, other: Tensor) -> Self: ...",
                f"def new(cls, size: _size, *, {DEVICE_PARAM}) -> Self: ...",
            ],
            "__init__": [
                f"def __init__(self, *args: Any, {DEVICE_PARAM}) -> None: ...",
                "def __init__(self, storage: Storage) -> None: ...",
                "def __init__(self, other: Tensor) -> None: ...",
                f"def __init__(self, size: _size, *, {DEVICE_PARAM}) -> None: ...",
            ],
            "as_subclass": ["def as_subclass(self, cls: _Type[S]) -> S: ..."],
            "_make_subclass": [
                "@staticmethod    \ndef _make_subclass({}) -> S: ...".format(
                    ", ".join(
                        [
                            "cls: _Type[S]",
                            "data: Tensor",
                            "require_grad: _bool = False",
                            "dispatch_strides: _bool = False",
                            "dispatch_device: _bool = False",
                            "device_for_backend_keys: Optional[_device] = None",
                        ]
                    )
                )
            ],
            "__contains__": ["def __contains__(self, other: Any, /) -> _bool: ..."],
            "__getitem__": [f"def __getitem__(self, {INDICES}) -> Tensor: ..."],
            "__setitem__": [
                f"def __setitem__(self, {INDICES}, val: Union[Tensor, Number]) -> None: ..."
            ],
            "tolist": ["def tolist(self) -> List: ..."],
            "requires_grad_": [
                "def requires_grad_(self, mode: _bool = True) -> Tensor: ..."
            ],
            "element_size": ["def element_size(self) -> _int: ..."],
            "data_ptr": ["def data_ptr(self) -> _int: ..."],
            "dim": ["def dim(self) -> _int: ..."],
            "nonzero": [
                "def nonzero(self, *, as_tuple: Literal[False] = False) -> Tensor: ...",
                "def nonzero(self, *, as_tuple: Literal[True]) -> Tuple[Tensor, ...]: ...",
            ],
            "numel": ["def numel(self) -> _int: ..."],
            "ndimension": ["def ndimension(self) -> _int: ..."],
            "nelement": ["def nelement(self) -> _int: ..."],
            "cuda": [
                "def cuda({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "self",
                            "device: Optional[Union[_device, _int, str]] = None",
                            "non_blocking: _bool = False",
                            "memory_format: torch.memory_format = torch.preserve_format",
                        ]
                    )
                )
            ],
            "xpu": [
                "def xpu({}) -> Tensor: ...".format(
                    ", ".join(
                        [
                            "self",
                            "device: Optional[Union[_device, _int, str]] = None",
                            "non_blocking: _bool = False",
                            "memory_format: torch.memory_format = torch.preserve_format",
                        ]
                    )
                )
            ],
            "cpu": [
                "def cpu(self, memory_format: torch.memory_format = torch.preserve_format) -> Tensor: ..."
            ],
            "numpy": ["def numpy(self, *, force: _bool = False) -> numpy.ndarray: ..."],
            "apply_": ["def apply_(self, callable: Callable) -> Tensor: ..."],
            "map_": [
                "def map_(self, tensor: Tensor, callable: Callable) -> Tensor: ..."
            ],
            "map2_": [
                "def map2_(self, x: Tensor, y: Tensor, callable: Callable) -> Tensor: ..."
            ],
            "storage": ["def untyped_storage(self) -> UntypedStorage: ..."],
            "storage_type": ["def storage_type(self) -> Storage: ..."],
            "type": [
                "def type(self, dtype: None = None, non_blocking: _bool = False) -> str: ...",
                "def type(self, dtype: Union[str, _dtype], non_blocking: _bool = False) -> Tensor: ...",
            ],
            "get_device": ["def get_device(self) -> _int: ..."],
            "contiguous": [
                "def contiguous(self, memory_format=torch.contiguous_format) -> Tensor: ..."
            ],
            "has_names": ["def has_names(self) -> _bool: ..."],
            "is_contiguous": [
                "def is_contiguous(self, memory_format=torch.contiguous_format) -> _bool: ..."
            ],
            "_is_view": ["def _is_view(self) -> _bool: ..."],
            "is_cpu": ["is_cpu: _bool"],
            "is_cuda": ["is_cuda: _bool"],
            "is_xpu": ["is_xpu: _bool"],
            "is_leaf": ["is_leaf: _bool"],
            "is_nested": ["is_nested: _bool"],
            "is_sparse": ["is_sparse: _bool"],
            "is_sparse_csr": ["is_sparse_csr: _bool"],
            "is_quantized": ["is_quantized: _bool"],
            "is_meta": ["is_meta: _bool"],
            "is_mps": ["is_mps: _bool"],
            "is_mtia": ["is_mtia: _bool"],
            "is_maia": ["is_maia: _bool"],
            "is_mkldnn": ["is_mkldnn: _bool"],
            "is_vulkan": ["is_vulkan: _bool"],
            "is_ipu": ["is_ipu: _bool"],
            "storage_offset": ["def storage_offset(self) -> Union[_int, SymInt]: ..."],
            "to": [
                (
                    f"def to(self, {args}, non_blocking: _bool = False, copy: _bool = False, *, "
                    "memory_format: Optional[torch.memory_format] = None) -> Tensor: ..."
                )
                for args in [
                    "dtype: _dtype",
                    "device: Optional[DeviceLikeType] = None, dtype: Optional[_dtype] = None",
                    "other: Tensor",
                ]
            ],
            "item": ["def item(self) -> Number: ..."],
            "copy_": [
                "def copy_(self, src: Tensor, non_blocking: _bool = False) -> Tensor: ..."
            ],
            "set_": [
                "def set_(self, storage: Union[Storage, TypedStorage, UntypedStorage], "
                "offset: IntLikeType, size: _symsize, stride: _symsize) -> Tensor: ...",
                "def set_(self, storage: Union[Storage, TypedStorage, UntypedStorage]) -> Tensor: ...",
            ],
            "split": [
                "def split(self, split_size: _int, dim: _int = 0) -> Sequence[Tensor]: ...",
                "def split(self, split_size: Tuple[_int, ...], dim: _int = 0) -> Sequence[Tensor]: ...",
            ],
            "div": [
                "def div(self, other: Union[Tensor, Number], *, rounding_mode: Optional[str] = None) -> Tensor: ..."
            ],
            "div_": [
                "def div_(self, other: Union[Tensor, Number], *, rounding_mode: Optional[str] = None) -> Tensor: ..."
            ],
        }
    )
    for binop in ["true_divide", "floor_divide"]:
        for inplace in [False, True]:
            out_suffix = ", *, out: Optional[Tensor] = None"
            if inplace:
                binop += "_"
                out_suffix = ""
            unsorted_tensor_method_hints[binop].append(
                f"def {binop}(self, other: Union[Tensor, Number, torch.SymInt, torch.SymFloat]{out_suffix})"
                " -> Tensor: ..."
            )
    for binop in ["mul"]:
        for inplace in [False, True]:
            out_suffix = ", *, out: Optional[Tensor] = None"
            if inplace:
                binop += "_"
                out_suffix = ""
            unsorted_tensor_method_hints[binop].append(
                f"def {binop}(self, other: Union[Tensor, Number, _complex, torch.SymInt, torch.SymFloat]{out_suffix})"
                " -> Tensor: ..."
            )
    for binop in ["add", "sub"]:
        for inplace in [False, True]:
            out_suffix = ", out: Optional[Tensor] = None"
            if inplace:
                binop += "_"
                out_suffix = ""
            unsorted_tensor_method_hints[binop].append(
                f"def {binop}(self, other: Union[Tensor, Number, _complex, torch.SymInt, torch.SymFloat], "
                f"*, alpha: Optional[Union[Number, _complex]] = 1{out_suffix})"
                " -> Tensor: ..."
            )
    simple_conversions = [
        "byte",
        "char",
        "double",
        "float",
        "half",
        "int",
        "long",
        "short",
        "bool",
        "bfloat16",
    ]
    for name in simple_conversions:
        unsorted_tensor_method_hints[name].append(f"def {name}(self) -> Tensor: ...")

    # pyi tensor methods don't currently include deprecated signatures for some reason
    # TODO: we should probably add them in
    tensor_method_signatures = load_signatures(
        native_functions,
        deprecated_yaml_path,
        method=True,
        skip_deprecated=True,
        pyi=True,
    )
    tensor_method_sig_groups = get_py_torch_functions(
        tensor_method_signatures, method=True
    )

    for group in sorted(tensor_method_sig_groups, key=lambda g: g.signature.name):
        name = group.signature.name
        unsorted_tensor_method_hints[name] += generate_type_hints(group)

        structseq = returns_structseq_pyi(group.signature)
        if structseq is not None and not group.signature.deprecated:
            # deprecated structseqs are currently not included for torch functions
            tuple_name, tuple_def = structseq
            if tuple_name in structseqs:
                assert structseqs[tuple_name] == tuple_def
            else:
                structseqs[tuple_name] = tuple_def

    for op in all_ops:
        name = f"__{op}__"
        unsorted_tensor_method_hints[name] += sig_for_ops(name)

    tensor_method_hints = []
    for name, hints in sorted(unsorted_tensor_method_hints.items()):
        if len(hints) > 1:
            hints = ["@overload\n" + h for h in hints]
        docstr = docstrs.get(f"torch._C.TensorBase.{name}")
        if docstr is not None:
            hints = [add_docstr_to_hint(docstr, h) for h in hints]
        tensor_method_hints += hints

    # TODO: Missing type hints for nn

    # Generate structseq definitions
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    structseq_defs = [f"{defn}\n" for defn in structseqs.values()]

    # Generate type signatures for legacy classes
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    legacy_storage_base_hints = ["class StorageBase(object): ..."]

    legacy_class_hints = []
    for c in (
        "DoubleTensor",
        "FloatTensor",
        "BFloat16Tensor",
        "LongTensor",
        "IntTensor",
        "ShortTensor",
        "HalfTensor",
        "CharTensor",
        "ByteTensor",
        "BoolTensor",
    ):
        legacy_class_hints.append(f"class {c}(Tensor): ...")

    # Generate type signatures for dtype classes
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    # TODO: don't explicitly list dtypes here; get it from canonical
    # source
    dtype_class_hints = [
        f"{n}: dtype = ..."
        for n in [
            "float32",
            "float",
            "float64",
            "double",
            "float16",
            "bfloat16",
            "float8_e4m3fn",
            "float8_e4m3fnuz",
            "float8_e5m2",
            "float8_e5m2fnuz",
            "half",
            "uint8",
            "uint16",
            "uint32",
            "uint64",
            "int8",
            "int16",
            "short",
            "int32",
            "int",
            "int64",
            "long",
            "complex32",
            "complex64",
            "chalf",
            "cfloat",
            "complex128",
            "cdouble",
            "quint8",
            "qint8",
            "qint32",
            "bool",
            "quint4x2",
            "quint2x4",
            "bits1x8",
            "bits2x4",
            "bits4x2",
            "bits8",
            "bits16",
        ]
    ]

    # Generate __all__ directive
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    # Include only the functions that contain hints, to prevent undefined
    # symbols to be included in the `__all__` directive.
    hinted_function_names = [
        name for name, hint in unsorted_function_hints.items() if hint
    ]
    all_symbols = sorted(list(structseqs.keys()) + hinted_function_names)
    all_directive = pformat(all_symbols, width=100, compact=True).split("\n")
    all_directive[0] = f"__all__ = {all_directive[0]}"

    # Dispatch key hints
    # ~~~~~~~~~~~~~~~~~~
    dispatch_key_hints = [f"{d.name}: DispatchKey = ..." for d in DispatchKey]
    torch_dispatch_mode_key_hints = [
        f"{k.name}: _TorchDispatchModeKey = ..." for k in _TorchDispatchModeKey
    ]

    # Tags Enum type hints
    # ~~~~~~~~~~~~~~~~~~~~

    tag_names = sorted(parse_tags_yaml(tags_yaml_path))
    tag_attributes = "\n".join(
        f"{name}: _int = {index}" for index, name in enumerate(tag_names)
    )

    # Write out the stub
    # ~~~~~~~~~~~~~~~~~~

    env = {
        "structseq_defs": structseq_defs,
        "function_hints": function_hints,
        "tensor_method_hints": tensor_method_hints,
        "legacy_class_hints": legacy_class_hints,
        "legacy_storage_base_hints": legacy_storage_base_hints,
        "dtype_class_hints": dtype_class_hints,
        "dispatch_key_hints": dispatch_key_hints,
        "torch_dispatch_mode_key_hints": torch_dispatch_mode_key_hints,
        "all_directive": all_directive,
        "tag_attributes": tag_attributes,
    }
    fm.write_with_template(
        "torch/_C/__init__.pyi",
        "torch/_C/__init__.pyi.in",
        lambda: env,
    )
    fm.write_with_template(
        "torch/_C/_VariableFunctions.pyi",
        "torch/_C/_VariableFunctions.pyi.in",
        lambda: env,
    )
    fm.write_with_template(
        "torch/_VF.pyi",
        "torch/_C/_VariableFunctions.pyi.in",
        lambda: env,
    )
    fm.write_with_template(
        "torch/return_types.pyi",
        "torch/_C/return_types.pyi.in",
        lambda: env,
    )
    gen_nn_functional(fm)


def main() -> None:
    parser = argparse.ArgumentParser(description="Generate type stubs for PyTorch")
    parser.add_argument(
        "--native-functions-path",
        metavar="NATIVE",
        default="aten/src/ATen/native/native_functions.yaml",
        help="path to native_functions.yaml",
    )
    parser.add_argument(
        "--tags-path",
        metavar="TAGS",
        default="aten/src/ATen/native/tags.yaml",
        help="path to tags.yaml",
    )
    parser.add_argument(
        "--deprecated-functions-path",
        metavar="DEPRECATED",
        default="tools/autograd/deprecated.yaml",
        help="path to deprecated.yaml",
    )
    parser.add_argument(
        "--out", metavar="OUT", default=".", help="path to output directory"
    )
    args = parser.parse_args()
    fm = FileManager(install_dir=args.out, template_dir=".", dry_run=False)
    gen_pyi(
        args.native_functions_path, args.tags_path, args.deprecated_functions_path, fm
    )


if __name__ == "__main__":
    main()