From 99f2491af9133ae3dd1ee9dae7110c3d97b9ca49 Mon Sep 17 00:00:00 2001 From: PyTorch MergeBot Date: Sat, 4 Jan 2025 14:17:20 +0000 Subject: [PATCH] Revert "Use absolute path `path.resolve()` -> `path.absolute()` (#129409)" This reverts commit 45411d1fc9a2b6d2f891b6ab0ae16409719e09fc. Reverted https://github.com/pytorch/pytorch/pull/129409 on behalf of https://github.com/jeanschmidt due to Breaking internal CI, @albanD please help get this PR merged ([comment](https://github.com/pytorch/pytorch/pull/129409#issuecomment-2571316444)) --- .github/scripts/build_triton_wheel.py | 2 +- .../scripts/close_nonexistent_disable_issues.py | 3 +-- .github/scripts/collect_ciflow_labels.py | 2 +- .github/scripts/delete_old_branches.py | 2 +- .github/scripts/ensure_actions_will_cancel.py | 2 +- .github/scripts/generate_ci_workflows.py | 2 +- .github/scripts/gitutils.py | 2 +- .github/scripts/lint_native_functions.py | 2 +- .github/scripts/test_gitutils.py | 2 +- .github/scripts/trymerge.py | 4 ++-- .../cuda/flash_attn/kernels/generate_kernels.py | 2 +- .../mem_eff_attention/kernels/generate_kernels.py | 2 +- aten/src/ATen/nnapi/codegen.py | 2 +- .../distributed/rpc/parameter_server/launcher.py | 2 +- docs/source/scripts/build_activation_images.py | 2 +- mypy_plugins/check_mypy_version.py | 5 +---- test/allowlist_for_publicAPI.json | 3 +-- test/distributed/_tensor/test_dtensor.py | 4 ++-- .../rendezvous/out_of_tree_rendezvous_test.py | 5 ++--- .../flight_recorder/test_fr_analysis.py | 4 ++-- .../dynamo/test_cudagraphs_expandable_segments.py | 5 ++--- .../test_cudagraph_trees_expandable_segments.py | 4 ++-- test/jit/fixtures_srcs/generate_models.py | 2 +- test/jit/test_backend_nnapi.py | 2 +- test/mobile/test_bytecode.py | 2 +- test/mobile/test_upgrader_codegen.py | 2 +- test/mobile/test_upgraders.py | 2 +- test/onnx/error_reproduction.py | 4 ++-- test/package/generate_bc_packages.py | 2 +- test/package/test_directory_reader.py | 5 +++-- test/package/test_load_bc_packages.py | 2 +- test/package/test_misc.py | 2 +- test/package/test_save_load.py | 5 +++-- test/quantization/core/test_docs.py | 2 +- test/run_test.py | 2 +- test/test_cuda_expandable_segments.py | 4 ++-- test/test_serialization.py | 3 ++- test/test_type_hints.py | 4 ++-- tools/build_with_debinfo.py | 4 ++-- tools/code_coverage/package/util/setting.py | 2 +- tools/generate_torch_version.py | 2 +- tools/jit/gen_unboxing.py | 2 +- tools/linter/adapters/clangtidy_linter.py | 4 ++-- tools/nvcc_fix_deps.py | 6 +++--- tools/onnx/update_default_opset_version.py | 2 +- tools/setup_helpers/generate_code.py | 2 +- tools/stats/export_test_times.py | 2 +- tools/stats/import_test_stats.py | 2 +- tools/stats/sccache_stats_to_benchmark_format.py | 2 +- tools/stats/upload_artifacts.py | 2 +- tools/test/heuristics/test_heuristics.py | 2 +- tools/test/heuristics/test_interface.py | 2 +- tools/test/heuristics/test_utils.py | 2 +- tools/test/test_test_run.py | 2 +- tools/test/test_test_selections.py | 2 +- tools/test/test_upload_stats_lib.py | 2 +- tools/testing/discover_tests.py | 4 ++-- tools/testing/do_target_determination_for_s3.py | 2 +- tools/testing/explicit_ci_jobs.py | 2 +- tools/testing/modulefinder_determinator.py | 2 +- .../testing/target_determination/gen_artifact.py | 2 +- .../target_determination/heuristics/filepath.py | 2 +- .../target_determination/heuristics/llm.py | 2 +- .../heuristics/previously_failed_in_pr.py | 2 +- .../target_determination/heuristics/utils.py | 2 +- tools/testing/test_selections.py | 2 +- tools/testing/update_slow_tests.py | 2 +- tools/testing/upload_artifacts.py | 2 +- torch/_inductor/codecache.py | 2 +- torch/_inductor/codegen/multi_kernel.py | 4 ++-- torch/_inductor/pattern_matcher.py | 4 +--- torch/_logging/_internal.py | 6 +++--- .../_internal/exporter/_capture_strategies.py | 4 ++-- torch/onnx/_internal/exporter/_core.py | 4 ++-- torch/package/package_exporter.py | 2 +- torch/testing/_internal/common_utils.py | 15 ++++++++------- torch/utils/cpp_extension.py | 13 +++++++------ torch/utils/data/datapipes/gen_pyi.py | 6 +++--- torchgen/decompositions/gen_jit_decompositions.py | 2 +- torchgen/gen.py | 4 ++-- torchgen/gen_executorch.py | 2 +- .../operator_versions/gen_mobile_upgraders.py | 2 +- .../shape_functions/gen_jit_shape_functions.py | 2 +- 83 files changed, 123 insertions(+), 127 deletions(-) diff --git a/.github/scripts/build_triton_wheel.py b/.github/scripts/build_triton_wheel.py index dd56d2841060..6ae29da339ee 100644 --- a/.github/scripts/build_triton_wheel.py +++ b/.github/scripts/build_triton_wheel.py @@ -9,7 +9,7 @@ from tempfile import TemporaryDirectory from typing import Optional -SCRIPT_DIR = Path(__file__).absolute().parent +SCRIPT_DIR = Path(__file__).parent REPO_DIR = SCRIPT_DIR.parent.parent diff --git a/.github/scripts/close_nonexistent_disable_issues.py b/.github/scripts/close_nonexistent_disable_issues.py index f46edee026aa..da58078d2516 100644 --- a/.github/scripts/close_nonexistent_disable_issues.py +++ b/.github/scripts/close_nonexistent_disable_issues.py @@ -12,9 +12,8 @@ import requests from gitutils import retries_decorator -REPO_ROOT = Path(__file__).absolute().parents[2] +REPO_ROOT = Path(__file__).resolve().parent.parent.parent sys.path.insert(0, str(REPO_ROOT)) - from tools.testing.clickhouse import query_clickhouse diff --git a/.github/scripts/collect_ciflow_labels.py b/.github/scripts/collect_ciflow_labels.py index 701090730d6d..2cd53d14795f 100755 --- a/.github/scripts/collect_ciflow_labels.py +++ b/.github/scripts/collect_ciflow_labels.py @@ -7,7 +7,7 @@ from typing import Any, cast, Dict, List, Set import yaml -GITHUB_DIR = Path(__file__).absolute().parent.parent +GITHUB_DIR = Path(__file__).parent.parent def get_workflows_push_tags() -> Set[str]: diff --git a/.github/scripts/delete_old_branches.py b/.github/scripts/delete_old_branches.py index 99a85727f863..e28d33c642be 100644 --- a/.github/scripts/delete_old_branches.py +++ b/.github/scripts/delete_old_branches.py @@ -22,7 +22,7 @@ TOKEN = os.environ["GITHUB_TOKEN"] if not TOKEN: raise Exception("GITHUB_TOKEN is not set") # noqa: TRY002 -REPO_ROOT = Path(__file__).absolute().parents[2] +REPO_ROOT = Path(__file__).parents[2] # Query for all PRs instead of just closed/merged because it's faster GRAPHQL_ALL_PRS_BY_UPDATED_AT = """ diff --git a/.github/scripts/ensure_actions_will_cancel.py b/.github/scripts/ensure_actions_will_cancel.py index fbedb9e8c408..2c76f09bb67f 100755 --- a/.github/scripts/ensure_actions_will_cancel.py +++ b/.github/scripts/ensure_actions_will_cancel.py @@ -6,7 +6,7 @@ from pathlib import Path import yaml -REPO_ROOT = Path(__file__).absolute().parents[2] +REPO_ROOT = Path(__file__).resolve().parents[2] WORKFLOWS = REPO_ROOT / ".github" / "workflows" EXPECTED_GROUP_PREFIX = ( "${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}" diff --git a/.github/scripts/generate_ci_workflows.py b/.github/scripts/generate_ci_workflows.py index 4971cd60e2a6..8512b27f0c03 100755 --- a/.github/scripts/generate_ci_workflows.py +++ b/.github/scripts/generate_ci_workflows.py @@ -13,7 +13,7 @@ import jinja2 Arch = Literal["windows", "linux", "macos"] -GITHUB_DIR = Path(__file__).absolute().parent.parent +GITHUB_DIR = Path(__file__).resolve().parent.parent LABEL_CIFLOW_TRUNK = "ciflow/trunk" LABEL_CIFLOW_UNSTABLE = "ciflow/unstable" diff --git a/.github/scripts/gitutils.py b/.github/scripts/gitutils.py index 15d1136fd235..42f163660323 100644 --- a/.github/scripts/gitutils.py +++ b/.github/scripts/gitutils.py @@ -32,7 +32,7 @@ def get_git_remote_name() -> str: def get_git_repo_dir() -> str: from pathlib import Path - return os.getenv("GIT_REPO_DIR", str(Path(__file__).absolute().parents[2])) + return os.getenv("GIT_REPO_DIR", str(Path(__file__).resolve().parents[2])) def fuzzy_list_to_dict(items: List[Tuple[str, str]]) -> Dict[str, List[str]]: diff --git a/.github/scripts/lint_native_functions.py b/.github/scripts/lint_native_functions.py index f218ed37286a..07504d7bdf26 100755 --- a/.github/scripts/lint_native_functions.py +++ b/.github/scripts/lint_native_functions.py @@ -26,7 +26,7 @@ def fn(base: str) -> str: return str(base / Path("aten/src/ATen/native/native_functions.yaml")) -with open(Path(__file__).absolute().parents[2] / fn(".")) as f: +with open(Path(__file__).parents[2] / fn(".")) as f: contents = f.read() yaml = ruamel.yaml.YAML() # type: ignore[attr-defined] diff --git a/.github/scripts/test_gitutils.py b/.github/scripts/test_gitutils.py index b631b7ba195d..b269cac3bc5f 100644 --- a/.github/scripts/test_gitutils.py +++ b/.github/scripts/test_gitutils.py @@ -12,7 +12,7 @@ from gitutils import ( ) -BASE_DIR = Path(__file__).absolute().parent +BASE_DIR = Path(__file__).parent class TestPeekableIterator(TestCase): diff --git a/.github/scripts/trymerge.py b/.github/scripts/trymerge.py index 26a77d5391c9..ca18ddcf4712 100755 --- a/.github/scripts/trymerge.py +++ b/.github/scripts/trymerge.py @@ -669,7 +669,7 @@ def get_ghstack_prs( if not open_only or not candidate.is_closed(): return False print( - f"Skipping {idx + 1} of {len(rev_list)} PR (#{candidate.pr_num}) as its already been merged" + f"Skipping {idx+1} of {len(rev_list)} PR (#{candidate.pr_num}) as its already been merged" ) return True @@ -1596,7 +1596,7 @@ def save_merge_record( "_id": f"{project}-{pr_num}-{comment_id}-{os.environ.get('GITHUB_RUN_ID')}", } ] - repo_root = Path(__file__).absolute().parents[2] + repo_root = Path(__file__).resolve().parent.parent.parent with open(repo_root / "merge_record.json", "w") as f: json.dump(data, f) diff --git a/aten/src/ATen/native/transformers/cuda/flash_attn/kernels/generate_kernels.py b/aten/src/ATen/native/transformers/cuda/flash_attn/kernels/generate_kernels.py index 9345f866ad5b..b125d431f496 100644 --- a/aten/src/ATen/native/transformers/cuda/flash_attn/kernels/generate_kernels.py +++ b/aten/src/ATen/native/transformers/cuda/flash_attn/kernels/generate_kernels.py @@ -85,7 +85,7 @@ def write_kernel(kernel: Kernel, autogen_dir: Path) -> None: def main(output_dir: Optional[str]) -> None: if output_dir is None: - output_dir = Path(__file__).absolute().parent + output_dir = Path(__file__).parent else: output_dir = Path(output_dir) diff --git a/aten/src/ATen/native/transformers/cuda/mem_eff_attention/kernels/generate_kernels.py b/aten/src/ATen/native/transformers/cuda/mem_eff_attention/kernels/generate_kernels.py index 0f2067d4f594..74df83f85f18 100644 --- a/aten/src/ATen/native/transformers/cuda/mem_eff_attention/kernels/generate_kernels.py +++ b/aten/src/ATen/native/transformers/cuda/mem_eff_attention/kernels/generate_kernels.py @@ -380,7 +380,7 @@ void dispatch_{family_name}(T cb, int cc = 0) {{ def main(output_dir: Optional[str]) -> None: if output_dir is None: - output_dir = Path(__file__).absolute().parent + output_dir = Path(__file__).parent else: output_dir = Path(output_dir) write_decl_impl( diff --git a/aten/src/ATen/nnapi/codegen.py b/aten/src/ATen/nnapi/codegen.py index b81e5bc7368c..57b1e3a696fa 100755 --- a/aten/src/ATen/nnapi/codegen.py +++ b/aten/src/ATen/nnapi/codegen.py @@ -232,7 +232,7 @@ def main(argv): ) ) - out_dir = Path(__file__).absolute().parent + out_dir = Path(__file__).parent (out_dir / "nnapi_wrapper.h").write_text( PREFIX diff --git a/benchmarks/distributed/rpc/parameter_server/launcher.py b/benchmarks/distributed/rpc/parameter_server/launcher.py index 5becb215bd3f..a084d1757e6e 100644 --- a/benchmarks/distributed/rpc/parameter_server/launcher.py +++ b/benchmarks/distributed/rpc/parameter_server/launcher.py @@ -330,7 +330,7 @@ def get_json_config(file_name: str, id: str): file_name (str): name of configuration file to load id (str): configuration that will be loaded """ - with open(Path(__file__).absolute().parent / file_name) as f: + with open(Path(__file__).parent / file_name) as f: json_config = json.load(f)[id] return json_config diff --git a/docs/source/scripts/build_activation_images.py b/docs/source/scripts/build_activation_images.py index bca09196b0c8..27e35f22a810 100644 --- a/docs/source/scripts/build_activation_images.py +++ b/docs/source/scripts/build_activation_images.py @@ -16,7 +16,7 @@ matplotlib.use("Agg") # Create a directory for the images, if it doesn't exist -ACTIVATION_IMAGE_PATH = Path(__file__).absolute().parent / "activation_images" +ACTIVATION_IMAGE_PATH = Path(__file__).parent / "activation_images" if not ACTIVATION_IMAGE_PATH.exists(): ACTIVATION_IMAGE_PATH.mkdir() diff --git a/mypy_plugins/check_mypy_version.py b/mypy_plugins/check_mypy_version.py index 42a24de460ad..4fa72e855770 100644 --- a/mypy_plugins/check_mypy_version.py +++ b/mypy_plugins/check_mypy_version.py @@ -10,10 +10,7 @@ def get_correct_mypy_version(): (match,) = re.finditer( r"mypy==(\d+(?:\.\d+)*)", ( - Path(__file__).absolute().parent.parent - / ".ci" - / "docker" - / "requirements-ci.txt" + Path(__file__).parent.parent / ".ci" / "docker" / "requirements-ci.txt" ).read_text(), ) (version,) = match.groups() diff --git a/test/allowlist_for_publicAPI.json b/test/allowlist_for_publicAPI.json index 7fd789fdc737..b81fe3929eb9 100644 --- a/test/allowlist_for_publicAPI.json +++ b/test/allowlist_for_publicAPI.json @@ -2391,8 +2391,7 @@ "Set", "Tuple", "Union", - "defaultdict", - "Path" + "defaultdict" ], "torch.utils.data.datapipes.utils.snapshot": [ "IterDataPipe", diff --git a/test/distributed/_tensor/test_dtensor.py b/test/distributed/_tensor/test_dtensor.py index e1e37f1914f7..0e62bbf2ee81 100644 --- a/test/distributed/_tensor/test_dtensor.py +++ b/test/distributed/_tensor/test_dtensor.py @@ -2,9 +2,9 @@ # Owner(s): ["oncall: distributed"] import os +import pathlib import tempfile import unittest -from pathlib import Path from numpy.testing import assert_array_equal @@ -559,7 +559,7 @@ class DTensorTest(DTensorTestBase): import_string = ( "import torch.distributed.tensor;" if should_import else "" ) - filename = Path(f.name) + filename = pathlib.Path(f.name) err_msg = ( ( "_pickle.UnpicklingError: Weights only load failed. " diff --git a/test/distributed/elastic/rendezvous/out_of_tree_rendezvous_test.py b/test/distributed/elastic/rendezvous/out_of_tree_rendezvous_test.py index 19347016188e..4d304bef1bc2 100644 --- a/test/distributed/elastic/rendezvous/out_of_tree_rendezvous_test.py +++ b/test/distributed/elastic/rendezvous/out_of_tree_rendezvous_test.py @@ -5,10 +5,9 @@ # # This source code is licensed under the BSD-style license found in the # LICENSE file in the root directory of this source tree. - +import pathlib import sys import unittest -from pathlib import Path import torch.distributed.elastic.rendezvous as rdvz @@ -19,7 +18,7 @@ TEST_PACKAGE_PATH = "/out_of_tree_test_package/src" class OutOfTreeRendezvousTest(unittest.TestCase): def test_out_of_tree_handler_loading(self): - current_path = str(Path(__file__).absolute().parent) + current_path = str(pathlib.Path(__file__).parent.resolve()) rdvz._register_out_of_tree_handlers() registry_dict = rdvz.rendezvous_handler_registry._registry diff --git a/test/distributed/flight_recorder/test_fr_analysis.py b/test/distributed/flight_recorder/test_fr_analysis.py index 696970136e7f..b7a875302b5d 100644 --- a/test/distributed/flight_recorder/test_fr_analysis.py +++ b/test/distributed/flight_recorder/test_fr_analysis.py @@ -1,11 +1,11 @@ # Owner(s): ["oncall: distributed"] import math +import pathlib import sys -from pathlib import Path -REPO_ROOT = Path(__file__).absolute().parents[3] +REPO_ROOT = pathlib.Path(__file__).resolve().parent.parent.parent.parent sys.path.insert(0, str(REPO_ROOT)) from tools.flight_recorder.components.types import COLLECTIVES, MatchState diff --git a/test/dynamo/test_cudagraphs_expandable_segments.py b/test/dynamo/test_cudagraphs_expandable_segments.py index 25ff400f7075..fe8d23dc82a7 100644 --- a/test/dynamo/test_cudagraphs_expandable_segments.py +++ b/test/dynamo/test_cudagraphs_expandable_segments.py @@ -2,8 +2,8 @@ # run time cuda tests, but with the allocator using expandable segments import os +import pathlib import sys -from pathlib import Path import torch from torch.testing._internal.common_cuda import IS_JETSON, IS_WINDOWS @@ -16,10 +16,9 @@ sys.path.append(pytorch_test_dir) from dynamo.test_cudagraphs import TestAotCudagraphs # noqa: F401 -REPO_ROOT = Path(__file__).absolute().parents[2] +REPO_ROOT = pathlib.Path(__file__).resolve().parent.parent.parent sys.path.insert(0, str(REPO_ROOT)) - from tools.stats.import_test_stats import get_disabled_tests diff --git a/test/inductor/test_cudagraph_trees_expandable_segments.py b/test/inductor/test_cudagraph_trees_expandable_segments.py index 0e20f307baeb..aa1e85fd82d1 100644 --- a/test/inductor/test_cudagraph_trees_expandable_segments.py +++ b/test/inductor/test_cudagraph_trees_expandable_segments.py @@ -2,8 +2,8 @@ # run time cuda tests, but with the allocator using expandable segments import os +import pathlib import sys -from pathlib import Path import torch from torch.testing._internal.common_cuda import IS_JETSON, IS_WINDOWS @@ -22,7 +22,7 @@ if HAS_CUDA and not TEST_WITH_ASAN: CudaGraphTreeTests, ) -REPO_ROOT = Path(__file__).absolute().parents[2] +REPO_ROOT = pathlib.Path(__file__).resolve().parent.parent.parent sys.path.insert(0, str(REPO_ROOT)) from tools.stats.import_test_stats import get_disabled_tests # @manual diff --git a/test/jit/fixtures_srcs/generate_models.py b/test/jit/fixtures_srcs/generate_models.py index e7aa6a092d93..233295cf8b4b 100644 --- a/test/jit/fixtures_srcs/generate_models.py +++ b/test/jit/fixtures_srcs/generate_models.py @@ -111,7 +111,7 @@ Get the path to `test/jit/fixtures`, where all test models for operator changes def get_fixtures_path() -> Path: - pytorch_dir = Path(__file__).absolute().parents[3] + pytorch_dir = Path(__file__).resolve().parents[3] fixtures_path = pytorch_dir / "test" / "jit" / "fixtures" return fixtures_path diff --git a/test/jit/test_backend_nnapi.py b/test/jit/test_backend_nnapi.py index fb0fac508b3c..9f4771665020 100644 --- a/test/jit/test_backend_nnapi.py +++ b/test/jit/test_backend_nnapi.py @@ -41,7 +41,7 @@ Inherits most tests from TestNNAPI, which loads Android NNAPI models without the delegate API. """ # First skip is needed for IS_WINDOWS or IS_MACOS to skip the tests. -torch_root = Path(__file__).absolute().parents[2] +torch_root = Path(__file__).resolve().parents[2] lib_path = torch_root / "build" / "lib" / "libnnapi_backend.so" diff --git a/test/mobile/test_bytecode.py b/test/mobile/test_bytecode.py index 2c589b1efdcd..1e42493a72b8 100644 --- a/test/mobile/test_bytecode.py +++ b/test/mobile/test_bytecode.py @@ -21,7 +21,7 @@ from torch.jit.mobile import ( from torch.testing._internal.common_utils import run_tests, TestCase -pytorch_test_dir = Path(__file__).absolute().parents[1] +pytorch_test_dir = Path(__file__).resolve().parents[1] # script_module_v4.ptl and script_module_v5.ptl source code # class TestModule(torch.nn.Module): diff --git a/test/mobile/test_upgrader_codegen.py b/test/mobile/test_upgrader_codegen.py index 997ff5784d1b..033cb268c6f8 100644 --- a/test/mobile/test_upgrader_codegen.py +++ b/test/mobile/test_upgrader_codegen.py @@ -9,7 +9,7 @@ from torch.testing._internal.common_utils import run_tests, TestCase from torchgen.operator_versions.gen_mobile_upgraders import sort_upgrader, write_cpp -pytorch_caffe2_dir = Path(__file__).absolute().parents[2] +pytorch_caffe2_dir = Path(__file__).resolve().parents[2] class TestLiteScriptModule(TestCase): diff --git a/test/mobile/test_upgraders.py b/test/mobile/test_upgraders.py index 67a81b5de80a..3567e0d030b4 100644 --- a/test/mobile/test_upgraders.py +++ b/test/mobile/test_upgraders.py @@ -10,7 +10,7 @@ from torch.jit.mobile import _load_for_lite_interpreter from torch.testing._internal.common_utils import run_tests, TestCase -pytorch_test_dir = Path(__file__).absolute().parents[1] +pytorch_test_dir = Path(__file__).resolve().parents[1] class TestLiteScriptModule(TestCase): diff --git a/test/onnx/error_reproduction.py b/test/onnx/error_reproduction.py index 4277aa2b1eb0..d0c7a69a3d4c 100644 --- a/test/onnx/error_reproduction.py +++ b/test/onnx/error_reproduction.py @@ -3,11 +3,11 @@ from __future__ import annotations import difflib +import pathlib import platform import sys import time import traceback -from pathlib import Path import numpy as np @@ -169,7 +169,7 @@ torch=={torch.__version__}""" def save_error_report(file_name: str, text: str): - reports_dir = Path("error_reports") + reports_dir = pathlib.Path("error_reports") reports_dir.mkdir(parents=True, exist_ok=True) file_path = reports_dir / file_name with open(file_path, "w", encoding="utf-8") as f: diff --git a/test/package/generate_bc_packages.py b/test/package/generate_bc_packages.py index 9c1151f62909..6fa5e81ef5a8 100644 --- a/test/package/generate_bc_packages.py +++ b/test/package/generate_bc_packages.py @@ -6,7 +6,7 @@ from torch.package import PackageExporter from torch.testing._internal.common_utils import IS_FBCODE, IS_SANDCASTLE -packaging_directory = str(Path(__file__).absolute().parent / "package_bc") +packaging_directory = f"{Path(__file__).parent}/package_bc" torch.package.package_exporter._gate_torchscript_serialization = False diff --git a/test/package/test_directory_reader.py b/test/package/test_directory_reader.py index 24207c92fea6..65cf538810a2 100644 --- a/test/package/test_directory_reader.py +++ b/test/package/test_directory_reader.py @@ -2,7 +2,6 @@ import os import zipfile -from pathlib import Path from sys import version_info from tempfile import TemporaryDirectory from textwrap import dedent @@ -33,8 +32,10 @@ except ImportError: # Support the case where we run this file directly. from common import PackageTestCase +from pathlib import Path -packaging_directory = Path(__file__).absolute().parent + +packaging_directory = Path(__file__).parent @skipIf( diff --git a/test/package/test_load_bc_packages.py b/test/package/test_load_bc_packages.py index e2f9571b90a8..2536f81aaaa2 100644 --- a/test/package/test_load_bc_packages.py +++ b/test/package/test_load_bc_packages.py @@ -13,7 +13,7 @@ except ImportError: # Support the case where we run this file directly. from common import PackageTestCase -packaging_directory = str(Path(__file__).absolute().parent / "package_bc") +packaging_directory = f"{Path(__file__).parent}/package_bc" class TestLoadBCPackages(PackageTestCase): diff --git a/test/package/test_misc.py b/test/package/test_misc.py index 6644acfa6f5d..850dec67681d 100644 --- a/test/package/test_misc.py +++ b/test/package/test_misc.py @@ -198,7 +198,7 @@ class TestMisc(PackageTestCase): def test_load_python_version_from_package(self): """Tests loading a package with a python version embdded""" importer1 = PackageImporter( - str(Path(__file__).absolute().parent / "package_e" / "test_nn_module.pt") + f"{Path(__file__).parent}/package_e/test_nn_module.pt" ) self.assertEqual(importer1.python_version(), "3.9.7") diff --git a/test/package/test_save_load.py b/test/package/test_save_load.py index b9b6344ecfa2..a0cc967787e6 100644 --- a/test/package/test_save_load.py +++ b/test/package/test_save_load.py @@ -2,7 +2,6 @@ import pickle from io import BytesIO -from pathlib import Path from sys import version_info from textwrap import dedent from unittest import skipIf @@ -18,8 +17,10 @@ except ImportError: # Support the case where we run this file directly. from common import PackageTestCase +from pathlib import Path -packaging_directory = Path(__file__).absolute().parent + +packaging_directory = Path(__file__).parent class TestSaveLoad(PackageTestCase): diff --git a/test/quantization/core/test_docs.py b/test/quantization/core/test_docs.py index fcb08afe0ce3..2222ef64b62e 100644 --- a/test/quantization/core/test_docs.py +++ b/test/quantization/core/test_docs.py @@ -44,7 +44,7 @@ class TestQuantizationDocs(QuantizationTestCase): Current working directory when CI is running test seems to vary, this function looks for docs relative to this test file. """ - core_dir = Path(__file__).absolute().parent + core_dir = Path(__file__).parent assert core_dir.match("test/quantization/core/"), ( "test_docs.py is in an unexpected location. If you've been " "moving files around, ensure that the test and build files have " diff --git a/test/run_test.py b/test/run_test.py index 676ad33d73fe..3ba8073fe0f1 100755 --- a/test/run_test.py +++ b/test/run_test.py @@ -41,7 +41,7 @@ from torch.testing._internal.common_utils import ( # using tools/ to optimize test run. -REPO_ROOT = Path(__file__).absolute().parent.parent +REPO_ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(REPO_ROOT)) from tools.stats.import_test_stats import ( diff --git a/test/test_cuda_expandable_segments.py b/test/test_cuda_expandable_segments.py index a6561474b683..78e4cddab84e 100644 --- a/test/test_cuda_expandable_segments.py +++ b/test/test_cuda_expandable_segments.py @@ -1,8 +1,8 @@ # Owner(s): ["module: cuda"] # run time cuda tests, but with the allocator using expandable segments +import pathlib import sys -from pathlib import Path from test_cuda import ( # noqa: F401 TestBlockStateAbsorption, @@ -15,7 +15,7 @@ from torch.testing._internal.common_cuda import IS_JETSON, IS_WINDOWS from torch.testing._internal.common_utils import run_tests, TEST_WITH_ROCM -REPO_ROOT = Path(__file__).absolute().parents[1] +REPO_ROOT = pathlib.Path(__file__).resolve().parent.parent sys.path.insert(0, str(REPO_ROOT)) from tools.stats.import_test_stats import get_disabled_tests diff --git a/test/test_serialization.py b/test/test_serialization.py index 37fb8852e5ef..aea2cf1a6f05 100644 --- a/test/test_serialization.py +++ b/test/test_serialization.py @@ -7,6 +7,7 @@ import gc import gzip import io import os +import pathlib import pickle import platform import re @@ -4444,7 +4445,7 @@ class TestSerialization(TestCase, SerializationMixin): with tempfile.NamedTemporaryFile() as f: njt = torch.nested.nested_tensor([[1, 2, 3], [4, 5]], layout=torch.jagged) torch.save(njt, f) - filename = Path(f.name) + filename = pathlib.Path(f.name) import_string = "import torch._dynamo;" if should_import else "" err_msg = ( "_pickle.UnpicklingError: Weights only load failed. ``torch.nested`` and ``torch._dynamo``" diff --git a/test/test_type_hints.py b/test/test_type_hints.py index 02e8628d43d8..0aae54be9b63 100644 --- a/test/test_type_hints.py +++ b/test/test_type_hints.py @@ -89,7 +89,7 @@ class TestTypeHints(TestCase): """ Run documentation examples through mypy. """ - fn = Path(__file__).absolute().parent / "generated_type_hints_smoketest.py" + fn = Path(__file__).resolve().parent / "generated_type_hints_smoketest.py" fn.write_text(get_all_examples()) # OK, so here's the deal. mypy treats installed packages @@ -130,7 +130,7 @@ class TestTypeHints(TestCase): ) except OSError: raise unittest.SkipTest("cannot symlink") from None - repo_rootdir = Path(__file__).absolute().parent.parent + repo_rootdir = Path(__file__).resolve().parent.parent # TODO: Would be better not to chdir here, this affects the # entire process! with set_cwd(str(repo_rootdir)): diff --git a/tools/build_with_debinfo.py b/tools/build_with_debinfo.py index 741afcb48e60..26c054bf2a0c 100755 --- a/tools/build_with_debinfo.py +++ b/tools/build_with_debinfo.py @@ -13,7 +13,7 @@ from pathlib import Path from typing import Any -PYTORCH_ROOTDIR = Path(__file__).absolute().parent.parent +PYTORCH_ROOTDIR = Path(__file__).resolve().parent.parent TORCH_DIR = PYTORCH_ROOTDIR / "torch" TORCH_LIB_DIR = TORCH_DIR / "lib" BUILD_DIR = PYTORCH_ROOTDIR / "build" @@ -110,7 +110,7 @@ def main() -> None: print("More than 100 items needs to be rebuild, run `ninja torch_python` first") sys.exit(-1) for idx, (name, cmd) in enumerate(build_plan): - print(f"[{idx + 1} / {len(build_plan)}] Building {name}") + print(f"[{idx + 1 } / {len(build_plan)}] Building {name}") if args.verbose: print(cmd) subprocess.check_call(["sh", "-c", cmd], cwd=BUILD_DIR) diff --git a/tools/code_coverage/package/util/setting.py b/tools/code_coverage/package/util/setting.py index 3b5eaff48755..9d31fa58f141 100644 --- a/tools/code_coverage/package/util/setting.py +++ b/tools/code_coverage/package/util/setting.py @@ -7,7 +7,7 @@ from pathlib import Path # HOME_DIR = os.environ["HOME"] -TOOLS_FOLDER = str(Path(__file__).absolute().parents[2]) +TOOLS_FOLDER = str(Path(__file__).resolve().parents[2]) # diff --git a/tools/generate_torch_version.py b/tools/generate_torch_version.py index f523f1db4e07..a33ea171edbb 100644 --- a/tools/generate_torch_version.py +++ b/tools/generate_torch_version.py @@ -85,7 +85,7 @@ if __name__ == "__main__": args.hip_version = None if args.hip_version == "" else args.hip_version args.xpu_version = None if args.xpu_version == "" else args.xpu_version - pytorch_root = Path(__file__).absolute().parent.parent + pytorch_root = Path(__file__).parent.parent version_path = pytorch_root / "torch" / "version.py" # Attempt to get tag first, fall back to sha if a tag was not found tagged_version = get_tag(pytorch_root) diff --git a/tools/jit/gen_unboxing.py b/tools/jit/gen_unboxing.py index 830a071ede39..b63b6f5ed251 100644 --- a/tools/jit/gen_unboxing.py +++ b/tools/jit/gen_unboxing.py @@ -280,7 +280,7 @@ def main(args: list[str]) -> None: gen_unboxing(native_functions=native_functions, cpu_fm=cpu_fm, selector=selector) if options.output_dependencies: - depfile_path = Path(options.output_dependencies).absolute() + depfile_path = Path(options.output_dependencies).resolve() depfile_name = depfile_path.name depfile_stem = depfile_path.stem diff --git a/tools/linter/adapters/clangtidy_linter.py b/tools/linter/adapters/clangtidy_linter.py index e727707b5b6e..0859f6e59d47 100644 --- a/tools/linter/adapters/clangtidy_linter.py +++ b/tools/linter/adapters/clangtidy_linter.py @@ -182,7 +182,7 @@ def check_file( for match in RESULTS_RE.finditer(proc.stdout.decode()): # Convert the reported path to an absolute path. - abs_path = str(Path(match["file"]).absolute()) + abs_path = str(Path(match["file"]).resolve()) message = LintMessage( path=abs_path, name=match["code"], @@ -262,7 +262,7 @@ def main() -> None: print(json.dumps(err_msg._asdict()), flush=True) sys.exit(0) - abs_build_dir = Path(args.build_dir).absolute() + abs_build_dir = Path(args.build_dir).resolve() # Get the absolute path to clang-tidy and use this instead of the relative # path such as .lintbin/clang-tidy. The problem here is that os.chdir is diff --git a/tools/nvcc_fix_deps.py b/tools/nvcc_fix_deps.py index b0f33fb9942a..0c0c9db66693 100644 --- a/tools/nvcc_fix_deps.py +++ b/tools/nvcc_fix_deps.py @@ -85,13 +85,13 @@ def extract_include_arg(include_dirs: list[Path], i: int, args: list[str]) -> No for name in PRE_INCLUDE_ARGS: path = extract_one(name, i, args) if path is not None: - include_dirs.insert(0, Path(path).absolute()) + include_dirs.insert(0, Path(path).resolve()) return for name in POST_INCLUDE_ARGS: path = extract_one(name, i, args) if path is not None: - include_dirs.append(Path(path).absolute()) + include_dirs.append(Path(path).resolve()) return @@ -110,7 +110,7 @@ if __name__ == "__main__": depfile_path = Path(args[i + 1]) elif arg == "-c": # Include the base path of the cuda file - include_dirs.append(Path(args[i + 1]).absolute().parent) + include_dirs.append(Path(args[i + 1]).resolve().parent) else: extract_include_arg(include_dirs, i, args) diff --git a/tools/onnx/update_default_opset_version.py b/tools/onnx/update_default_opset_version.py index 32fca52123c4..88a98e5b27c0 100755 --- a/tools/onnx/update_default_opset_version.py +++ b/tools/onnx/update_default_opset_version.py @@ -30,7 +30,7 @@ def read_sub_write(path: str, prefix_pat: str, new_default: int) -> None: def main(args: Any) -> None: - pytorch_dir = Path(__file__).absolute().parents[2] + pytorch_dir = Path(__file__).parents[2].resolve() onnx_dir = pytorch_dir / "third_party" / "onnx" os.chdir(onnx_dir) diff --git a/tools/setup_helpers/generate_code.py b/tools/setup_helpers/generate_code.py index 65f93defc621..6e0a64888f0a 100644 --- a/tools/setup_helpers/generate_code.py +++ b/tools/setup_helpers/generate_code.py @@ -44,7 +44,7 @@ def generate_code( autograd_gen_dir = os.path.join(install_dir, "autograd", "generated") for d in (autograd_gen_dir, python_install_dir): os.makedirs(d, exist_ok=True) - autograd_dir = os.fspath(Path(__file__).absolute().parent.parent / "autograd") + autograd_dir = os.fspath(Path(__file__).parent.parent / "autograd") if subset == "pybindings" or not subset: gen_autograd_python( diff --git a/tools/stats/export_test_times.py b/tools/stats/export_test_times.py index 1fd85f906b9a..ae8771883303 100644 --- a/tools/stats/export_test_times.py +++ b/tools/stats/export_test_times.py @@ -2,7 +2,7 @@ import sys from pathlib import Path -REPO_ROOT = Path(__file__).absolute().parents[2] +REPO_ROOT = Path(__file__).resolve().parents[2] sys.path.append(str(REPO_ROOT)) from tools.stats.import_test_stats import get_test_class_times, get_test_times diff --git a/tools/stats/import_test_stats.py b/tools/stats/import_test_stats.py index 83dfea2344e6..ca4d8e4a3b47 100644 --- a/tools/stats/import_test_stats.py +++ b/tools/stats/import_test_stats.py @@ -11,7 +11,7 @@ from typing import Any, Callable, cast, Dict from urllib.request import urlopen -REPO_ROOT = Path(__file__).absolute().parents[2] +REPO_ROOT = Path(__file__).resolve().parents[2] def get_disabled_issues() -> list[str]: diff --git a/tools/stats/sccache_stats_to_benchmark_format.py b/tools/stats/sccache_stats_to_benchmark_format.py index 33a0ad66f63b..d0667da4c996 100644 --- a/tools/stats/sccache_stats_to_benchmark_format.py +++ b/tools/stats/sccache_stats_to_benchmark_format.py @@ -5,7 +5,7 @@ from pathlib import Path from typing import Any, Dict -REPO_ROOT = Path(__file__).absolute().parents[2] +REPO_ROOT = Path(__file__).resolve().parent.parent.parent def flatten_data(d: Dict[str, Any]) -> Dict[str, Any]: diff --git a/tools/stats/upload_artifacts.py b/tools/stats/upload_artifacts.py index 4f6421b1976b..5036d745d963 100644 --- a/tools/stats/upload_artifacts.py +++ b/tools/stats/upload_artifacts.py @@ -32,7 +32,7 @@ def get_artifacts(repo: str, workflow_run_id: int, workflow_run_attempt: int) -> # pytorch/pytorch/WORKFLOW_ID/RUN_ATTEMPT/artifact/NAME-SUFFIX.zip s3_filename = re.sub(FILENAME_REGEX, "", artifact_path.name) upload_file_to_s3( - file_name=str(artifact_path.absolute()), + file_name=str(artifact_path.resolve()), bucket=BUCKET_NAME, key=f"{repo}/{workflow_run_id}/{workflow_run_attempt}/artifact/{s3_filename}", ) diff --git a/tools/test/heuristics/test_heuristics.py b/tools/test/heuristics/test_heuristics.py index c8e0753022ec..575d1b5732b7 100644 --- a/tools/test/heuristics/test_heuristics.py +++ b/tools/test/heuristics/test_heuristics.py @@ -10,7 +10,7 @@ from typing import Any from unittest import mock -REPO_ROOT = Path(__file__).absolute().parents[3] +REPO_ROOT = Path(__file__).resolve().parents[3] sys.path.append(str(REPO_ROOT)) from tools.test.heuristics.test_interface import TestTD diff --git a/tools/test/heuristics/test_interface.py b/tools/test/heuristics/test_interface.py index 1043e983e31d..a51ab9394037 100644 --- a/tools/test/heuristics/test_interface.py +++ b/tools/test/heuristics/test_interface.py @@ -6,7 +6,7 @@ from pathlib import Path from typing import Any -REPO_ROOT = Path(__file__).absolute().parents[3] +REPO_ROOT = Path(__file__).resolve().parents[3] sys.path.append(str(REPO_ROOT)) import tools.testing.target_determination.heuristics.interface as interface diff --git a/tools/test/heuristics/test_utils.py b/tools/test/heuristics/test_utils.py index e944658397ff..e1f47b8453e1 100644 --- a/tools/test/heuristics/test_utils.py +++ b/tools/test/heuristics/test_utils.py @@ -6,7 +6,7 @@ from pathlib import Path from typing import Any -REPO_ROOT = Path(__file__).absolute().parents[3] +REPO_ROOT = Path(__file__).resolve().parents[3] sys.path.append(str(REPO_ROOT)) import tools.testing.target_determination.heuristics.utils as utils diff --git a/tools/test/test_test_run.py b/tools/test/test_test_run.py index 502ab974d342..c3fc2736f84d 100644 --- a/tools/test/test_test_run.py +++ b/tools/test/test_test_run.py @@ -3,7 +3,7 @@ import unittest from pathlib import Path -REPO_ROOT = Path(__file__).absolute().parents[2] +REPO_ROOT = Path(__file__).resolve().parents[2] try: # using tools/ to optimize test run. sys.path.append(str(REPO_ROOT)) diff --git a/tools/test/test_test_selections.py b/tools/test/test_test_selections.py index efd2a6375acc..f5164ddbc3a1 100644 --- a/tools/test/test_test_selections.py +++ b/tools/test/test_test_selections.py @@ -8,7 +8,7 @@ from collections import defaultdict from pathlib import Path -REPO_ROOT = Path(__file__).absolute().parents[2] +REPO_ROOT = Path(__file__).resolve().parents[2] try: # using tools/ to optimize test run. sys.path.append(str(REPO_ROOT)) diff --git a/tools/test/test_upload_stats_lib.py b/tools/test/test_upload_stats_lib.py index 4a099eb0c058..8d2a7e639d2f 100644 --- a/tools/test/test_upload_stats_lib.py +++ b/tools/test/test_upload_stats_lib.py @@ -10,7 +10,7 @@ from typing import Any from unittest import mock -REPO_ROOT = Path(__file__).absolute().parents[2] +REPO_ROOT = Path(__file__).resolve().parents[2] sys.path.insert(0, str(REPO_ROOT)) from tools.stats.upload_metrics import add_global_metric, emit_metric, global_metrics diff --git a/tools/testing/discover_tests.py b/tools/testing/discover_tests.py index 64123296e788..614d036b45a9 100644 --- a/tools/testing/discover_tests.py +++ b/tools/testing/discover_tests.py @@ -9,7 +9,7 @@ from pathlib import Path CPP_TEST_PREFIX = "cpp" CPP_TEST_PATH = "build/bin" CPP_TESTS_DIR = os.path.abspath(os.getenv("CPP_TESTS_DIR", default=CPP_TEST_PATH)) -REPO_ROOT = Path(__file__).absolute().parents[2] +REPO_ROOT = Path(__file__).resolve().parents[2] def parse_test_module(test: str) -> str: @@ -43,7 +43,7 @@ def discover_tests( ] cpp_tests_dir = ( - str(base_dir.parent / CPP_TEST_PATH) if cpp_tests_dir is None else cpp_tests_dir + f"{base_dir.parent}/{CPP_TEST_PATH}" if cpp_tests_dir is None else cpp_tests_dir ) # CPP test files are located under pytorch/build/bin. Unlike Python test, C++ tests # are just binaries and could have any name, i.e. basic or atest diff --git a/tools/testing/do_target_determination_for_s3.py b/tools/testing/do_target_determination_for_s3.py index 976170cdb46d..27a0fbb5b422 100644 --- a/tools/testing/do_target_determination_for_s3.py +++ b/tools/testing/do_target_determination_for_s3.py @@ -4,7 +4,7 @@ import sys from pathlib import Path -REPO_ROOT = Path(__file__).absolute().parents[2] +REPO_ROOT = Path(__file__).resolve().parents[2] sys.path.insert(0, str(REPO_ROOT)) from tools.stats.import_test_stats import ( diff --git a/tools/testing/explicit_ci_jobs.py b/tools/testing/explicit_ci_jobs.py index fe6da5783f7e..dcf406472353 100755 --- a/tools/testing/explicit_ci_jobs.py +++ b/tools/testing/explicit_ci_jobs.py @@ -12,7 +12,7 @@ from typing import Any import yaml -REPO_ROOT = Path(__file__).absolute().parents[2] +REPO_ROOT = Path(__file__).parents[2] CONFIG_YML = REPO_ROOT / ".circleci" / "config.yml" WORKFLOWS_DIR = REPO_ROOT / ".github" / "workflows" diff --git a/tools/testing/modulefinder_determinator.py b/tools/testing/modulefinder_determinator.py index c1b4d01e03e7..01f99e745d04 100644 --- a/tools/testing/modulefinder_determinator.py +++ b/tools/testing/modulefinder_determinator.py @@ -8,7 +8,7 @@ from pathlib import Path from typing import Any -REPO_ROOT = Path(__file__).absolute().parents[2] +REPO_ROOT = Path(__file__).resolve().parents[2] # These tests are slow enough that it's worth calculating whether the patch # touched any related files first. This list was manually generated, but for every diff --git a/tools/testing/target_determination/gen_artifact.py b/tools/testing/target_determination/gen_artifact.py index 93783f027543..a28a05bd2bdb 100644 --- a/tools/testing/target_determination/gen_artifact.py +++ b/tools/testing/target_determination/gen_artifact.py @@ -6,7 +6,7 @@ from pathlib import Path from typing import Any -REPO_ROOT = Path(__file__).absolute().parents[3] +REPO_ROOT = Path(__file__).resolve().parents[3] def gen_ci_artifact(included: list[Any], excluded: list[Any]) -> None: diff --git a/tools/testing/target_determination/heuristics/filepath.py b/tools/testing/target_determination/heuristics/filepath.py index 5aa0cbd5c1ed..0005ba5df5e5 100644 --- a/tools/testing/target_determination/heuristics/filepath.py +++ b/tools/testing/target_determination/heuristics/filepath.py @@ -17,7 +17,7 @@ from tools.testing.target_determination.heuristics.utils import ( from tools.testing.test_run import TestRun -REPO_ROOT = Path(__file__).absolute().parents[3] +REPO_ROOT = Path(__file__).parents[3] keyword_synonyms: dict[str, list[str]] = { "amp": ["mixed_precision"], diff --git a/tools/testing/target_determination/heuristics/llm.py b/tools/testing/target_determination/heuristics/llm.py index 7c930b6ce5bd..6c6a4b1be21e 100644 --- a/tools/testing/target_determination/heuristics/llm.py +++ b/tools/testing/target_determination/heuristics/llm.py @@ -16,7 +16,7 @@ from tools.testing.target_determination.heuristics.utils import normalize_rating from tools.testing.test_run import TestRun -REPO_ROOT = Path(__file__).absolute().parents[4] +REPO_ROOT = Path(__file__).resolve().parents[4] class LLM(HeuristicInterface): diff --git a/tools/testing/target_determination/heuristics/previously_failed_in_pr.py b/tools/testing/target_determination/heuristics/previously_failed_in_pr.py index 862eecce6c06..bf0a9549cc9f 100644 --- a/tools/testing/target_determination/heuristics/previously_failed_in_pr.py +++ b/tools/testing/target_determination/heuristics/previously_failed_in_pr.py @@ -20,7 +20,7 @@ from tools.testing.target_determination.heuristics.utils import ( from tools.testing.test_run import TestRun -REPO_ROOT = Path(__file__).absolute().parents[4] +REPO_ROOT = Path(__file__).resolve().parents[4] class PreviouslyFailedInPR(HeuristicInterface): diff --git a/tools/testing/target_determination/heuristics/utils.py b/tools/testing/target_determination/heuristics/utils.py index 2dadaab1e9fd..d9e9b002e379 100644 --- a/tools/testing/target_determination/heuristics/utils.py +++ b/tools/testing/target_determination/heuristics/utils.py @@ -16,7 +16,7 @@ if TYPE_CHECKING: from tools.testing.test_run import TestRun -REPO_ROOT = Path(__file__).absolute().parents[4] +REPO_ROOT = Path(__file__).resolve().parents[4] def python_test_file_to_test_name(tests: set[str]) -> set[str]: diff --git a/tools/testing/test_selections.py b/tools/testing/test_selections.py index c5ddda3704f1..635f512f087b 100644 --- a/tools/testing/test_selections.py +++ b/tools/testing/test_selections.py @@ -14,7 +14,7 @@ if TYPE_CHECKING: from collections.abc import Sequence -REPO_ROOT = Path(__file__).absolute().parents[2] +REPO_ROOT = Path(__file__).resolve().parents[2] IS_MEM_LEAK_CHECK = os.getenv("PYTORCH_TEST_CUDA_MEM_LEAK_CHECK", "0") == "1" BUILD_ENVIRONMENT = os.getenv("BUILD_ENVIRONMENT", "") diff --git a/tools/testing/update_slow_tests.py b/tools/testing/update_slow_tests.py index df6dcdd72eb3..c54399e18cde 100644 --- a/tools/testing/update_slow_tests.py +++ b/tools/testing/update_slow_tests.py @@ -9,7 +9,7 @@ import requests from clickhouse import query_clickhouse # type: ignore[import] -REPO_ROOT = Path(__file__).absolute().parents[2] +REPO_ROOT = Path(__file__).resolve().parent.parent.parent QUERY = """ WITH most_recent_strict_commits AS ( SELECT diff --git a/tools/testing/upload_artifacts.py b/tools/testing/upload_artifacts.py index 0d7ccba656b5..4ebfd03a1465 100644 --- a/tools/testing/upload_artifacts.py +++ b/tools/testing/upload_artifacts.py @@ -7,7 +7,7 @@ from pathlib import Path from typing import Any -REPO_ROOT = Path(__file__).absolute().parents[2] +REPO_ROOT = Path(__file__).resolve().parent.parent.parent LAST_UPDATED = 0.0 diff --git a/torch/_inductor/codecache.py b/torch/_inductor/codecache.py index 929b7a61ff73..42755fb3a508 100644 --- a/torch/_inductor/codecache.py +++ b/torch/_inductor/codecache.py @@ -1807,7 +1807,7 @@ class AotCodeCompiler: @clear_on_fresh_inductor_cache @functools.lru_cache def cpp_prefix_path() -> str: - path = Path(__file__).absolute().parent / "codegen/cpp_prefix.h" + path = Path(__file__).parent / "codegen/cpp_prefix.h" with path.open() as f: content = f.read() _, filename = write( diff --git a/torch/_inductor/codegen/multi_kernel.py b/torch/_inductor/codegen/multi_kernel.py index f2b639d24e25..bb42d340333f 100644 --- a/torch/_inductor/codegen/multi_kernel.py +++ b/torch/_inductor/codegen/multi_kernel.py @@ -2,7 +2,7 @@ import functools import logging import os -from pathlib import Path +import pathlib from typing import Any, List from torch._inductor.metrics import get_metric_table, is_metric_table_enabled @@ -322,7 +322,7 @@ class MultiKernelCall: ) ) _, _, path = get_path(key, "picked_kernel") - return Path(path) + return pathlib.Path(path) def load_cache(self): assert self.picked_kernel is None diff --git a/torch/_inductor/pattern_matcher.py b/torch/_inductor/pattern_matcher.py index 711d3d8f3c57..75688616e51e 100644 --- a/torch/_inductor/pattern_matcher.py +++ b/torch/_inductor/pattern_matcher.py @@ -1483,9 +1483,7 @@ def _serialize_pattern( return pattern -SERIALIZED_PATTERN_PATH = ( - Path(__file__).absolute().parent / "fx_passes" / "serialized_patterns" -) +SERIALIZED_PATTERN_PATH = Path(__file__).parent / "fx_passes" / "serialized_patterns" # This is the set of serialized patterns that we've registered. Used by # test_serialized_patterns_up_to_date() to ensure the patterns are up diff --git a/torch/_logging/_internal.py b/torch/_logging/_internal.py index 230315c27bf4..02f12cb37fb3 100644 --- a/torch/_logging/_internal.py +++ b/torch/_logging/_internal.py @@ -7,13 +7,13 @@ import json import logging import os import os.path +import pathlib import re import sys import tempfile import time from collections import defaultdict from dataclasses import dataclass, field -from pathlib import Path from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union from weakref import WeakSet @@ -768,7 +768,7 @@ def make_module_path_relative(abs_path): a relative path relative to one of the Python search paths. """ - abs_path = Path(abs_path).absolute() + abs_path = pathlib.Path(abs_path).resolve() for path in sys.path: try: @@ -844,7 +844,7 @@ class TorchLogsFormatter(logging.Formatter): filepath = make_module_path_relative(record.pathname) prefix = ( - f"{record.rankprefix}{shortlevel}{record.asctime}.{int(record.msecs * 1000):06d} {record.process} " + f"{record.rankprefix}{shortlevel}{record.asctime}.{int(record.msecs*1000):06d} {record.process} " f"{filepath}:" f"{record.lineno}]{record.traceid}{record.artifactprefix}" ) diff --git a/torch/onnx/_internal/exporter/_capture_strategies.py b/torch/onnx/_internal/exporter/_capture_strategies.py index 509e58dd03e0..90fe1a584b54 100644 --- a/torch/onnx/_internal/exporter/_capture_strategies.py +++ b/torch/onnx/_internal/exporter/_capture_strategies.py @@ -8,7 +8,7 @@ import contextlib import dataclasses import datetime import logging -from pathlib import Path +import pathlib from typing import Any, Callable, TYPE_CHECKING import torch @@ -91,7 +91,7 @@ class CaptureStrategy(abc.ABC): """ self._verbose_print = _verbose_printer(verbose) self._dump = dump - self._artifacts_dir = Path(artifacts_dir) + self._artifacts_dir = pathlib.Path(artifacts_dir) self._timestamp = timestamp or datetime.datetime.now().strftime( "%Y-%m-%d_%H-%M-%S-%f" ) diff --git a/torch/onnx/_internal/exporter/_core.py b/torch/onnx/_internal/exporter/_core.py index 56d4e33d065d..70792c5f2667 100644 --- a/torch/onnx/_internal/exporter/_core.py +++ b/torch/onnx/_internal/exporter/_core.py @@ -8,10 +8,10 @@ import inspect import itertools import logging import operator +import pathlib import textwrap import traceback import typing -from pathlib import Path from typing import Any, Callable, Literal, Mapping, Sequence import onnxscript @@ -1215,7 +1215,7 @@ def export( profiler = _maybe_start_profiler(profile) # Create the artifacts directory if it does not exist - artifacts_dir = Path(artifacts_dir) + artifacts_dir = pathlib.Path(artifacts_dir) if report or profile or dump_exported_program: artifacts_dir.mkdir(parents=True, exist_ok=True) diff --git a/torch/package/package_exporter.py b/torch/package/package_exporter.py index c7b6665e9d28..2ece831fab00 100644 --- a/torch/package/package_exporter.py +++ b/torch/package/package_exporter.py @@ -1011,7 +1011,7 @@ class PackageExporter: def _write_mock_file(self): if "_mock.py" not in self._written_files: - mock_file = str(Path(__file__).absolute().parent / "_mock.py") + mock_file = str(Path(__file__).parent / "_mock.py") self._write_source_string("_mock", _read_file(mock_file), is_package=False) def _execute_dependency_graph(self): diff --git a/torch/testing/_internal/common_utils.py b/torch/testing/_internal/common_utils.py index 7e8deb91c230..7214fc09396f 100644 --- a/torch/testing/_internal/common_utils.py +++ b/torch/testing/_internal/common_utils.py @@ -22,6 +22,7 @@ import logging import math import operator import os +import pathlib import platform import random import re @@ -988,9 +989,9 @@ UNITTEST_ARGS = [sys.argv[0]] + remaining torch.manual_seed(SEED) # CI Prefix path used only on CI environment -CI_TEST_PREFIX = str(Path.cwd()) -CI_PT_ROOT = str(Path(CI_TEST_PREFIX).parent) -CI_FUNCTORCH_ROOT = str(Path(CI_PT_ROOT) / "functorch") +CI_TEST_PREFIX = str(Path(os.getcwd())) +CI_PT_ROOT = str(Path(os.getcwd()).parent) +CI_FUNCTORCH_ROOT = str(os.path.join(Path(os.getcwd()).parent, "functorch")) def wait_for_process(p, timeout=None): try: @@ -4357,7 +4358,7 @@ class TestCase(expecttest.TestCase): def _attempt_load_from_subprocess( self, - file: Path, + file: pathlib.Path, import_string: str, expected_failure_message: Optional[str] = None ) -> None: @@ -4366,7 +4367,7 @@ class TestCase(expecttest.TestCase): weights_only `torch.load` works as expected without global imports. Args: - file (Path): The path to the checkpoint to load. + file (pathlib.Path): The path to the checkpoint to load. import_string (str): import string to add to the script exected_failure_message (str, optional): The expected failure message if the checkpoint fails to load. If None, the test will pass @@ -5009,11 +5010,11 @@ def disable_gc(): def find_library_location(lib_name: str) -> Path: # return the shared library file in the installed folder if exist, # else the file in the build folder - torch_root = Path(torch.__file__).absolute().parent + torch_root = Path(torch.__file__).resolve().parent path = torch_root / 'lib' / lib_name if os.path.exists(path): return path - torch_root = Path(__file__).absolute().parents[2] + torch_root = Path(__file__).resolve().parents[2] return torch_root / 'build' / 'lib' / lib_name def skip_but_pass_in_sandcastle(reason): diff --git a/torch/utils/cpp_extension.py b/torch/utils/cpp_extension.py index 76cc772dab19..826e0da818bf 100644 --- a/torch/utils/cpp_extension.py +++ b/torch/utils/cpp_extension.py @@ -126,11 +126,11 @@ def _find_rocm_home() -> Optional[str]: # Guess #2 hipcc_path = shutil.which('hipcc') if hipcc_path is not None: - rocm_home_path = Path(hipcc_path).resolve().parent.parent + rocm_home = os.path.dirname(os.path.dirname( + os.path.realpath(hipcc_path))) # can be either /hip/bin/hipcc or /bin/hipcc - if rocm_home_path.name == 'hip': - rocm_home_path = rocm_home_path.parent - rocm_home = str(rocm_home_path) + if os.path.basename(rocm_home) == 'hip': + rocm_home = os.path.dirname(rocm_home) else: # Guess #3 fallback_path = '/opt/rocm' @@ -147,7 +147,8 @@ def _find_sycl_home() -> Optional[str]: # Guess 1: for source code build developer/user, we'll have icpx in PATH, # which will tell us the SYCL_HOME location. if icpx_path is not None: - sycl_home = str(Path(icpx_path).resolve().parent.parent) + sycl_home = os.path.dirname(os.path.dirname( + os.path.realpath(icpx_path))) # Guess 2: for users install Pytorch with XPU support, the sycl runtime is # inside intel-sycl-rt, which is automatically installed via pip dependency. @@ -156,7 +157,7 @@ def _find_sycl_home() -> Optional[str]: files = importlib.metadata.files('intel-sycl-rt') or [] for f in files: if f.name == "libsycl.so": - sycl_home = str(Path(f.locate()).resolve().parent.parent) + sycl_home = os.path.dirname(Path(f.locate()).parent.resolve()) break except importlib.metadata.PackageNotFoundError: print("Trying to find SYCL_HOME from intel-sycl-rt package, but it is not installed.", diff --git a/torch/utils/data/datapipes/gen_pyi.py b/torch/utils/data/datapipes/gen_pyi.py index 16557a45f0ca..dbe448b65beb 100644 --- a/torch/utils/data/datapipes/gen_pyi.py +++ b/torch/utils/data/datapipes/gen_pyi.py @@ -1,7 +1,7 @@ # mypy: allow-untyped-defs import os +import pathlib from collections import defaultdict -from pathlib import Path from typing import Any, Dict, List, Set, Tuple, Union @@ -211,7 +211,7 @@ def get_method_definitions( # 3. Remove first argument after self (unless it is "*datapipes"), default args, and spaces """ if root == "": - root = str(Path(__file__).absolute().parent) + root = str(pathlib.Path(__file__).parent.resolve()) file_path = [file_path] if isinstance(file_path, str) else file_path file_path = [os.path.join(root, path) for path in file_path] file_paths = find_file_paths( @@ -288,7 +288,7 @@ def main() -> None: mapDP_method_to_special_output_type, ) - path = Path(__file__).absolute().parent + path = pathlib.Path(__file__).parent.resolve() replacements = [ ("${IterDataPipeMethods}", iter_method_definitions, 4), ("${MapDataPipeMethods}", map_method_definitions, 4), diff --git a/torchgen/decompositions/gen_jit_decompositions.py b/torchgen/decompositions/gen_jit_decompositions.py index 773d8601cb1c..b42948045cbd 100644 --- a/torchgen/decompositions/gen_jit_decompositions.py +++ b/torchgen/decompositions/gen_jit_decompositions.py @@ -86,7 +86,7 @@ def write_decomposition_util_file(path: str) -> None: def main() -> None: - pytorch_dir = Path(__file__).absolute().parents[3] + pytorch_dir = Path(__file__).resolve().parents[3] upgrader_path = pytorch_dir / "torch" / "csrc" / "jit" / "runtime" write_decomposition_util_file(str(upgrader_path)) diff --git a/torchgen/gen.py b/torchgen/gen.py index 9597b9db9cf9..d08c2ff2cb83 100644 --- a/torchgen/gen.py +++ b/torchgen/gen.py @@ -2752,7 +2752,7 @@ def get_torchgen_root() -> Path: If you're depending on torchgen out-of-tree, you can use the root to figure out the path to native_functions.yaml """ - return Path(__file__).absolute().parent + return Path(__file__).parent.resolve() def main() -> None: @@ -3043,7 +3043,7 @@ def main() -> None: gen_declarations_yaml(native_functions=native_functions, cpu_fm=cpu_fm) if options.output_dependencies: - depfile_path = Path(options.output_dependencies).absolute() + depfile_path = Path(options.output_dependencies).resolve() depfile_name = depfile_path.name depfile_stem = depfile_path.stem diff --git a/torchgen/gen_executorch.py b/torchgen/gen_executorch.py index eb9ac75dd67d..7d3cf4edb05f 100644 --- a/torchgen/gen_executorch.py +++ b/torchgen/gen_executorch.py @@ -984,7 +984,7 @@ def main() -> None: ) if options.output_dependencies: - depfile_path = Path(options.output_dependencies).absolute() + depfile_path = Path(options.output_dependencies).resolve() depfile_name = depfile_path.name depfile_stem = depfile_path.stem diff --git a/torchgen/operator_versions/gen_mobile_upgraders.py b/torchgen/operator_versions/gen_mobile_upgraders.py index 7df09fc663ec..845034cb7484 100644 --- a/torchgen/operator_versions/gen_mobile_upgraders.py +++ b/torchgen/operator_versions/gen_mobile_upgraders.py @@ -380,7 +380,7 @@ def main() -> None: for up in sorted_upgrader_list: print("after sort upgrader : ", next(iter(up))) - pytorch_dir = Path(__file__).absolute().parents[2] + pytorch_dir = Path(__file__).resolve().parents[2] upgrader_path = pytorch_dir / "torch" / "csrc" / "jit" / "mobile" write_cpp(str(upgrader_path), sorted_upgrader_list) diff --git a/torchgen/shape_functions/gen_jit_shape_functions.py b/torchgen/shape_functions/gen_jit_shape_functions.py index 83091c0aad17..56a3d8bf0dd3 100644 --- a/torchgen/shape_functions/gen_jit_shape_functions.py +++ b/torchgen/shape_functions/gen_jit_shape_functions.py @@ -173,7 +173,7 @@ def write_decomposition_util_file(path: str) -> None: def main() -> None: - pytorch_dir = Path(__file__).absolute().parents[2] + pytorch_dir = Path(__file__).resolve().parents[2] upgrader_path = pytorch_dir / "torch" / "csrc" / "jit" / "runtime" write_decomposition_util_file(str(upgrader_path))