8000 [torchgen] Refactor `torchgen.utils.FileManager` to accept `pathlib.P… · pytorch/pytorch@014726d · GitHub
[go: up one dir, main page]

Skip to content

Commit 014726d

Browse files
XuehaiPanpytorchmergebot
authored andcommitted
[torchgen] Refactor torchgen.utils.FileManager to accept pathlib.Path (#150726)
This PR allows `FileManager` to accept `pathlib.Path` as arguments while keeping the original `str` path support. This allows us to simplify the code such as: 1. `os.path.join(..., ...)` with `Path.__floordiv__(..., ...)`. https://github.com/pytorch/pytorch/blob/95a5958db490608cacca75b89d9a1d2e955b60e8/torchgen/utils.py#L155 https://github.com/pytorch/pytorch/blob/95a5958db490608cacca75b89d9a1d2e955b60e8/torchgen/utils.py#L176 2. `os.path.basename(...)` with `Path(...).name`. https://github.com/pytorch/pytorch/blob/95a5958db490608cacca75b89d9a1d2e955b60e8/torchgen/utils.py#L161 3. Manual file extension split with `Path(...).with_stem(new_stem)` https://github.com/pytorch/pytorch/blob/95a5958db490608cacca75b89d9a1d2e955b60e8/torchgen/utils.py#L241-L256 ------ Pull Request resolved: #150726 Approved by: https://github.com/aorenste
1 parent 881a598 commit 014726d

File tree

12 files changed

+111
-75
lines changed

12 files changed

+111
-75
lines changed

tools/autograd/gen_autograd_functions.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,7 @@ def gen_autograd_functions_lib(
555555
fname,
556556
lambda: {
557557
"generated_comment": "@"
558-
+ f"generated from {fm.template_dir_for_comments()}/"
559-
+ fname,
558+
+ f"generated from {fm.template_dir_for_comments()}/{fname}",
560559
"autograd_function_declarations": declarations,
561560
"autograd_function_definitions": definitions,
562561
},

tools/autograd/gen_view_funcs.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,7 @@ def gen_view_funcs(
331331
fname,
332332
lambda: {
333333
"generated_comment": "@"
334-
+ f"generated from {fm.template_dir_for_comments()}/"
335-
+ fname,
334+
+ f"generated from {fm.template_dir_for_comments()}/{fname}",
336335
"view_func_declarations": declarations,
337336
"view_func_definitions": definitions,
338337
"ops_headers": ops_headers,

torchgen/api/cpp.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
from typing import TYPE_CHECKING
4+
from typing_extensions import assert_never
45

56
from torchgen import local
67
from torchgen.api.types import (
@@ -48,7 +49,6 @@
4849
TensorOptionsArguments,
4950
Type,
5051
)
51-
from torchgen.utils import assert_never
5252

5353

5454
if TYPE_CHECKING:

torchgen/api/dispatcher.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import itertools
44
from typing import TYPE_CHECKING
5+
from typing_extensions import assert_never
56

67
from torchgen.api import cpp
78
from torchgen.api.types import ArgName, Binding, CType, NamedCType
@@ -13,7 +14,7 @@
1314
TensorOptionsArguments,
1415
Type,
1516
)
16-
from torchgen.utils import assert_never, concatMap
17+
from torchgen.utils import concatMap
1718

1819

1920
if TYPE_CHECKING:

torchgen/api/native.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
from typing import TYPE_CHECKING
4+
from typing_extensions import assert_never
45

56
from torchgen import local
67
from torchgen.api import cpp
@@ -29,7 +30,6 @@
2930
TensorOptionsArguments,
3031
Type,
3132
)
32-
from torchgen.utils import assert_never
3333

3434

3535
if TYPE_CHECKING:

torchgen/api/structured.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
from typing_extensions import assert_never
4+
35
from torchgen.api import cpp
46
from torchgen.api.types import (
57
ArgName,
@@ -30,7 +32,6 @@
3032
TensorOptionsArguments,
3133
Type,
3234
)
33-
from torchgen.utils import assert_never
3435

3536

3637
# This file describes the translation of JIT schema to the structured functions API.

torchgen/dest/register_dispatch_key.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import textwrap
55
from dataclasses import dataclass
66
from typing import Literal, TYPE_CHECKING
7+
from typing_extensions import assert_never
78

89
import torchgen.api.cpp as cpp
910
import torchgen.api.meta as meta
@@ -36,7 +37,7 @@
3637
SchemaKind,
3738
TensorOptionsArguments,
3839
)
39-
from torchgen.utils import assert_never, mapMaybe, Target
40+
from torchgen.utils import mapMaybe, Target
4041

4142

4243
if TYPE_CHECKING:

torchgen/executorch/api/et_cpp.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
from typing import TYPE_CHECKING
4+
from typing_extensions import assert_never
45

56
from torchgen import local
67
from torchgen.api.types import (
@@ -37,7 +38,6 @@
3738
TensorOptionsArguments,
3839
Type,
3940
)
40-
from torchgen.utils import assert_never
4141

4242

4343
if TYPE_CHECKING:

torchgen/executorch/model.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from collections import defaultdict, namedtuple
88
from dataclasses import dataclass
99
from enum import IntEnum
10+
from typing_extensions import assert_never
1011

1112
from torchgen.model import (
1213
BackendIndex,
@@ -16,7 +17,6 @@
1617
NativeFunctionsGroup,
1718
OperatorName,
1819
)
19-
from torchgen.utils import assert_never
2020

2121

2222
KERNEL_KEY_VERSION = 1

torchgen/gen.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from dataclasses import dataclass, field
1010
from pathlib import Path
1111
from typing import Any, Callable, Literal, TYPE_CHECKING, TypeVar
12+
from typing_extensions import assert_never
1213

1314
import yaml
1415

@@ -84,7 +85,6 @@
8485
)
8586
from torchgen.selective_build.selector import SelectiveBuilder
8687
from torchgen.utils import (
87-
assert_never,
8888
concatMap,
8989
context,
9090
FileManager,

torchgen/model.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
from dataclasses import dataclass
77
from enum import auto, Enum
88
from typing import Callable, Optional, TYPE_CHECKING
9+
from typing_extensions import assert_never
910

10-
from torchgen.utils import assert_never, NamespaceHelper, OrderedSet
11+
from torchgen.utils import NamespaceHelper, OrderedSet
1112

1213 420B

1314
if TYPE_CHECKING:

0 commit comments

Comments
 (0)
0