8000 [BE][Easy] replace `import pathlib` with `from pathlib import Path` (… · pytorch/pytorch@4ee1cb9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4ee1cb9

Browse files
XuehaiPanpytorchmergebot
authored andcommitted
[BE][Easy] replace import pathlib with from pathlib import Path (#129426)
Pull Request resolved: #129426 Approved by: https://github.com/malfet
1 parent 2effbcf commit 4ee1cb9

32 files changed

+153
-134
lines changed

aten/src/ATen/nnapi/codegen.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
we need with dlsym. We also generate a "check" wrapper that checks
88
return values and throws C++ exceptions on errors.
99
"""
10-
import pathlib
10+
1111
import re
1212
import sys
1313
import textwrap
14+
from pathlib import Path
1415

1516

1617
PREFIX = """\
@@ -231,7 +232,7 @@ def main(argv):
231232
)
232233
)
233234

234-
out_dir = pathlib.Path(__file__).parent
235+
out_dir = Path(__file__).parent
235236

236237
(out_dir / "nnapi_wrapper.h").write_text(
237238
PREFIX

benchmarks/dynamo/ci_expected_accuracy/update_expected.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
import argparse
1919
import json
2020
import os
21-
import pathlib
2221
import subprocess
2322
import sys
2423
import urllib
2524
from io import BytesIO
2625
from itertools import product
26+
from pathlib import Path
2727
from urllib.request import urlopen
2828
from zipfile import ZipFile
2929

@@ -34,7 +34,7 @@
3434
# https://console.rockset.com/lambdas/details/commons.artifacts
3535
ARTIFACTS_QUERY_URL = "https://api.usw2a1.rockset.com/v1/public/shared_lambdas/4ca0033e-0117-41f5-b043-59cde19eff35"
3636
CSV_LINTER = str(
37-
pathlib.Path(__file__).absolute().parent.parent.parent.parent
37+
Path(__file__).absolute().parent.parent.parent.parent
3838
/ "tools/linter/adapters/no_merge_conflict_csv_linter.py"
3939
)
4040

benchmarks/dynamo/common.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from __future__ import annotations
33

44
import abc
5-
65
import argparse
76
import collections
87
import contextlib
@@ -14,15 +13,14 @@
1413
import itertools
1514
import logging
1615
import os
17-
import pathlib
1816
import shutil
1917
import signal
2018
import subprocess
2119
import sys
2220
import time
2321
import weakref
2422
from contextlib import contextmanager
25-
23+
from pathlib import Path
2624
from typing import (
2725
Any,
2826
Callable,
@@ -60,6 +58,7 @@
6058
same,
6159
)
6260

61+
6362
try:
6463
from torch._dynamo.utils import (
6564
clone_inputs,
@@ -81,6 +80,7 @@
8180
from torch.utils import _pytree as pytree
8281
from torch.utils._pytree import tree_map, tree_map_only
8382

83+
8484
try:
8585
import torch_xla
8686
import torch_xla.core.xla_model as xm
@@ -920,7 +920,7 @@ def speedup_experiment_onnx(
920920
2. Running ORT with OnnxModel.
921921
922922
Writes to ./{output_filename}, which should be
923-
`pathlib.Path(self.output_dir) / f"{self.compiler}_{suite}_{self.dtype}_{self.mode}_{self.device}_{self.testing}.csv".
923+
`Path(self.output_dir) / f"{self.compiler}_{suite}_{self.dtype}_{self.mode}_{self.device}_{self.testing}.csv".
924924
925925
TODO(bowbao): Record export time and export peak memory usage.
926926
"""
@@ -1347,8 +1347,8 @@ def deepcopy_model_and_inputs_to_device(self, model, example_inputs, target_devi
13471347
@classmethod
13481348
def _generate_onnx_model_directory(
13491349
cls, output_directory: str, compiler_name: str, model_name: str
1350-
) -> pathlib.Path:
1351-
model_path = pathlib.Path(
1350+
) -> Path:
1351+
model_path = Path(
13521352
output_directory,
13531353
".onnx_models",
13541354
model_name,
@@ -2389,7 +2389,6 @@ def get_fsdp_auto_wrap_policy(self, model_name: str):
23892389
from diffusers.models.transformer_2d import Transformer2DModel
23902390
from torchbenchmark.models.nanogpt.model import Block
23912391
from transformers.models.llama.modeling_llama import LlamaDecoderLayer
2392-
23932392
from transformers.models.t5.modeling_t5 import T5Block
23942393
from transformers.models.whisper.modeling_whisper import WhisperEncoderLayer
23952394

scripts/compile_tests/update_failures.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env python3
22
import argparse
33
import os
4-
import pathlib
54
import subprocess
5+
from pathlib import Path
66

77
from common import (
88
get_testcases,
@@ -194,7 +194,7 @@ def read_test_results(directory):
194194
"filename",
195195
nargs="?",
196196
default=str(
197-
pathlib.Path(__file__).absolute().parent.parent.parent
197+
Path(__file__).absolute().parent.parent.parent
198198
/ "torch/testing/_internal/dynamo_test_failures.py"
199199
),
200200
help="Optional path to dynamo_test_failures.py",
@@ -203,7 +203,7 @@ def read_test_results(directory):
203203
parser.add_argument(
204204
"test_dir",
205205
nargs="?",
206-
default=str(pathlib.Path(__file__).absolute().parent.parent.parent / "test"),
206+
default=str(Path(__file__).absolute().parent.parent.parent / "test"),
207207
help="Optional path to test folder",
208208
)
209209
parser.add_argument(
@@ -219,7 +219,7 @@ def read_test_results(directory):
219219
action="store_true",
220220
)
221221
args = parser.parse_args()
222-
assert pathlib.Path(args.filename).exists(), args.filename
223-
assert pathlib.Path(args.test_dir).exists(), args.test_dir
222+
assert Path(args.filename).exists(), args.filename
223+
assert Path(args.test_dir).exists(), args.test_dir
224224
dynamo38, dynamo311 = download_reports(args.commit, ("dynamo38", "dynamo311"))
225225
update(args.filename, args.test_dir, dynamo38, dynamo311, args.also_remove_skips)

test/distributed/nn/jit/test_instantiator.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#!/usr/bin/env python3
22
# Owner(s): ["oncall: distributed"]
33

4-
import pathlib
54
import sys
5+
from pathlib import Path
66
from typing import Tuple
77

88
import torch
99
import torch.distributed as dist
1010
from torch import nn, Tensor
1111

12+
1213
if not dist.is_available():
1314
print("Distributed not available, skipping tests", file=sys.stderr)
1415
sys.exit(0)
@@ -45,7 +46,7 @@ def test_get_arg_return_types_from_interface(self):
4546
self.assertEqual(return_type_str, "Tuple[Tensor, int, str]")
4647

4748
def test_instantiate_scripted_remote_module_template(self):
48-
dir_path = pathlib.Path(instantiator.INSTANTIATED_TEMPLATE_DIR_PATH)
49+
dir_path = Path(instantiator.INSTANTIATED_TEMPLATE_DIR_PATH)
4950

5051
# Cleanup.
5152
file_paths = dir_path.glob(f"{instantiator._FILE_PREFIX}*.py")
@@ -69,7 +70,7 @@ def test_instantiate_scripted_remote_module_template(self):
6970
self.assertEqual(num_files_after, 1)
7071

7172
def test_instantiate_non_scripted_remote_module_template(self):
72-
dir_path = pathlib.Path(instantiator.INSTANTIATED_TEMPLATE_DIR_PATH)
73+
dir_path = Path(instantiator.INSTANTIATED_TEMPLATE_DIR_PATH)
7374

7475
# Cleanup.
7576
file_paths = dir_path.glob(f"{instantiator._FILE_PREFIX}*.py")

test/export/test_serialize.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
# Owner(s): ["oncall: export"]
88
import copy
99
import io
10-
import pathlib
1110
import tempfile
1211
import unittest
1312
import zipfile
13+
from pathlib import Path
1414

1515
import torch
1616
import torch._dynamo as torchdynamo
@@ -38,7 +38,6 @@
3838
TemporaryFileName,
3939
TestCase,
4040
)
41-
4241
from torch.testing._internal.torchbind_impls import init_torchbind_implementations
4342

4443

@@ -1052,7 +1051,7 @@ def forward(self, x, y):
10521051
ep = export(f, inp)
10531052

10541053
with TemporaryFileName() as fname:
1055-
path = pathlib.Path(fname)
1054+
path = Path(fname)
10561055
save(ep, path)
10571056
loaded_ep = load(path)
10581057

test/inductor/test_debug_trace.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
# Owner(s): ["module: inductor"]
22
import logging
33
import os
4-
import pathlib
54
import re
65
import shutil
76
import sys
87
import unittest
8+
from pathlib import Path
99

1010
import torch
1111
from torch._inductor import config, test_operators
1212
from torch.testing._internal.inductor_utils import GPU_TYPE, HAS_GPU
1313

14+
1415
try:
1516
try:
1617
from . import test_torchinductor
@@ -22,7 +23,7 @@
2223
raise
2324

2425

25-
def filesize(filename: pathlib.Path):
26+
def filesize(filename: Path):
2627
assert filename.exists(), f"{filename} is missing"
2728
return os.stat(filename).st_size
2829

@@ -43,7 +44,7 @@ def fn(a, b):
4344
self.assertEqual(len(cm.output), 1)
4445
m = re.match(r"WARNING.* debug trace: (.*)", cm.output[0])
4546
self.assertTrue(m)
46-
filename = pathlib.Path(m.group(1))
47+
filename = Path(m.group(1))
4748
self.assertTrue(filename.is_dir())
4849
self.assertGreater(filesize(filename / "fx_graph_readable.py"), 512)
4950
self.assertGreater(filesize(filename / "fx_graph_runnable.py"), 512)

test/jit/test_save_load.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
import io
44
import os
5-
import pathlib
65
import sys
6+
from pathlib import Path
77
from typing import NamedTuple, Optional
88

99
import torch
1010
from torch import Tensor
1111
from torch.testing._internal.common_utils import skipIfTorchDynamo, TemporaryFileName
1212

13+
1314
# Make the helper files in test/ importable
1415
pytorch_test_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
1516
sys.path.append(pytorch_test_dir)
@@ -397,7 +398,7 @@ def forward(self, a):
397398

398399
# Save then load.
399400
with TemporaryFileName() as fname:
400-
path = pathlib.Path(fname)
401+
path = Path(fname)
401402
m.save(path)
402403
m2 = torch.jit.load(path)
403404

@@ -624,7 +625,7 @@ def get_loaded_inputs(inputs):
624625
traced_module = torch.jit.trace(module, input1)
625626
traced_inputs = list(traced_module.graph.inputs())
626627
with TemporaryFileName() as fname:
627-
path = pathlib.Path(fname)
628+
path = Path(fname)
628629
traced_module.save(path)
629630
print(traced_module.graph)
630631
loaded_module = torch.jit.load(path, _restore_shapes=True)
@@ -640,7 +641,7 @@ def get_loaded_inputs(inputs):
640641
traced_module._c._retrieve_traced_inputs()["forward"], [input_tensor]
641642
)
642643
with TemporaryFileName() as fname:
643-
path = pathlib.Path(fname)
644+
path = Path(fname)
644645
traced_module.save(path)
645646
loaded_module = torch.jit.load(path, _restore_shapes=True)
646647
loaded_inputs = list(loaded_module.graph.inputs())
@@ -659,7 +660,7 @@ def get_loaded_inputs(inputs):
659660
self.assertEqual(len(traced_module._c._retrieve_traced_inputs()), 0)
660661

661662
with TemporaryFileName() as fname:
662-
path = pathlib.Path(fname)
663+
path = Path(fname)
663664
traced_module.save(path)
664665
loaded_module = torch.jit.load(path, _restore_shapes=True)
665666
loaded_inputs = list(loaded_module.graph.inputs())
@@ -1055,7 +1056,7 @@ def forward(self, a):
10551056

10561057
# Save then load.
10571058
with TemporaryFileName() as fname:
1058-
path = pathlib.Path(fname)
1059+
path = Path(fname)
10591060
torch.jit.save_jit_module_to_flatbuffer(m, path)
10601061
m2 = torch.jit.load(path)
10611062

test/lazy/test_ts_opinfo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import functools
44
import itertools
55
import os
6-
import pathlib
6+
from pathlib import Path
77
from typing import Sequence
88
from unittest import skip
99

@@ -20,10 +20,10 @@
2020
ops,
2121
)
2222
from torch.testing._internal.common_methods_invocations import op_db
23-
2423
from torch.testing._internal.common_utils import run_tests, TestCase
2524
from torch.testing._internal.jit_utils import JitTestCase
2625

26+
2727
torch._lazy.ts_backend.init()
2828

2929

@@ -36,7 +36,7 @@ def remove_suffixes(l):
3636

3737

3838
def init_lists():
39-
path_to_script = pathlib.Path(os.path.abspath(os.path.dirname(__file__)))
39+
path_to_script = Path(os.path.abspath(os.path.dirname(__file__)))
4040
TS_NATIVE_FUNCTIONS_PATH = (
4141
path_to_script.parent.parent / "aten/src/ATen/native/ts_native_functions.yaml"
4242
)

0 commit comments

Comments
 (0)
0