diff --git a/.circleci/codegen_validation/normalize_yaml_fragment.py b/.circleci/codegen_validation/normalize_yaml_fragment.py index 6d15f1a5a5b7..232eaa833b93 100755 --- a/.circleci/codegen_validation/normalize_yaml_fragment.py +++ b/.circleci/codegen_validation/normalize_yaml_fragment.py @@ -7,7 +7,7 @@ import yaml # Need to import modules that lie on an upward-relative path -sys.path.append(os.path.join(sys.path[0], "..")) +sys.path.append(os.path.dirname(sys.path[0])) import cimodel.lib.miniyaml as miniyaml diff --git a/.github/scripts/delete_old_branches.py b/.github/scripts/delete_old_branches.py index 9ca82eb71392..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__).parent.parent.parent +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 9e464f0dc256..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__).resolve().parent.parent.parent +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_binary_build_matrix.py b/.github/scripts/generate_binary_build_matrix.py index eee9c6581de9..5295e31a8291 100644 --- a/.github/scripts/generate_binary_build_matrix.py +++ b/.github/scripts/generate_binary_build_matrix.py @@ -94,7 +94,7 @@ def get_nccl_submodule_version() -> str: from pathlib import Path nccl_version_mk = ( - Path(__file__).absolute().parent.parent.parent + Path(__file__).absolute().parents[2] / "third_party" / "nccl" / "nccl" diff --git a/.github/scripts/gitutils.py b/.github/scripts/gitutils.py index 505ba2680017..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__).resolve().parent.parent.parent)) + 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 4dfe9fd63e2e..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__).parent.parent.parent / 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 c4137bad31e1..b269cac3bc5f 100644 --- a/.github/scripts/test_gitutils.py +++ b/.github/scripts/test_gitutils.py @@ -68,7 +68,7 @@ class TestRetriesDecorator(TestCase): class TestGitRepo(TestCase): def setUp(self) -> None: - repo_dir = BASE_DIR.parent.parent.absolute() + repo_dir = BASE_DIR.absolute().parent.parent if not (repo_dir / ".git").is_dir(): raise SkipTest( "Can't find git directory, make sure to run this test on real repo checkout" diff --git a/benchmarks/dynamo/ci_expected_accuracy/update_expected.py b/benchmarks/dynamo/ci_expected_accuracy/update_expected.py index 289f96b90e67..d767b0c2609f 100644 --- a/benchmarks/dynamo/ci_expected_accuracy/update_expected.py +++ b/benchmarks/dynamo/ci_expected_accuracy/update_expected.py @@ -71,7 +71,7 @@ ARTIFACTS_QUERY_URL = ( "c1cdfadc-6bb2-4a91-bbf9-3d19e1981cd4/run?format=JSON" ) CSV_LINTER = str( - Path(__file__).absolute().parent.parent.parent.parent + Path(__file__).absolute().parents[3] / "tools/linter/adapters/no_merge_conflict_csv_linter.py" ) diff --git a/docs/source/scripts/build_opsets.py b/docs/source/scripts/build_opsets.py index 84bc97b76d41..c752ade4d831 100644 --- a/docs/source/scripts/build_opsets.py +++ b/docs/source/scripts/build_opsets.py @@ -7,9 +7,9 @@ import torch._prims as prims from torchgen.gen import parse_native_yaml -ROOT = Path(__file__).absolute().parent.parent.parent.parent -NATIVE_FUNCTION_YAML_PATH = ROOT / Path("aten/src/ATen/native/native_functions.yaml") -TAGS_YAML_PATH = ROOT / Path("aten/src/ATen/native/tags.yaml") +ROOT = Path(__file__).absolute().parents[3] +NATIVE_FUNCTION_YAML_PATH = ROOT / "aten/src/ATen/native/native_functions.yaml" +TAGS_YAML_PATH = ROOT / "aten/src/ATen/native/tags.yaml" BUILD_DIR = "build/ir" ATEN_OPS_CSV_FILE = "aten_ops.csv" diff --git a/docs/source/scripts/build_quantization_configs.py b/docs/source/scripts/build_quantization_configs.py index bf4056620404..5d1f445ade9a 100644 --- a/docs/source/scripts/build_quantization_configs.py +++ b/docs/source/scripts/build_quantization_configs.py @@ -15,7 +15,7 @@ from torch.ao.quantization.backend_config.utils import ( # Create a directory for the images, if it doesn't exist QUANTIZATION_BACKEND_CONFIG_IMAGE_PATH = os.path.join( - os.path.realpath(os.path.join(__file__, "..")), "quantization_backend_configs" + os.path.realpath(os.path.dirname(__file__)), "quantization_backend_configs" ) if not os.path.exists(QUANTIZATION_BACKEND_CONFIG_IMAGE_PATH): diff --git a/docs/source/scripts/exportdb/generate_example_rst.py b/docs/source/scripts/exportdb/generate_example_rst.py index 1910ea4856d1..4b7803b494e9 100644 --- a/docs/source/scripts/exportdb/generate_example_rst.py +++ b/docs/source/scripts/exportdb/generate_example_rst.py @@ -11,9 +11,9 @@ from torch.export import export PWD = Path(__file__).absolute().parent -ROOT = Path(__file__).absolute().parent.parent.parent.parent -SOURCE = ROOT / Path("source") -EXPORTDB_SOURCE = SOURCE / Path("generated") / Path("exportdb") +ROOT = Path(__file__).absolute().parents[3] +SOURCE = ROOT / "source" +EXPORTDB_SOURCE = SOURCE / "generated" / "exportdb" def generate_example_rst(example_case: ExportCase): diff --git a/scripts/compile_tests/update_failures.py b/scripts/compile_tests/update_failures.py index a56e30e99870..73fb354a8d15 100755 --- a/scripts/compile_tests/update_failures.py +++ b/scripts/compile_tests/update_failures.py @@ -194,7 +194,7 @@ if __name__ == "__main__": "filename", nargs="?", default=str( - Path(__file__).absolute().parent.parent.parent + Path(__file__).absolute().parents[2] / "torch/testing/_internal/dynamo_test_failures.py" ), help="Optional path to dynamo_test_failures.py", @@ -203,7 +203,7 @@ if __name__ == "__main__": parser.add_argument( "test_dir", nargs="?", - default=str(Path(__file__).absolute().parent.parent.parent / "test"), + default=str(Path(__file__).absolute().parents[2] / "test"), help="Optional path to test folder", ) parser.add_argument( diff --git a/test/jit/test_backend_nnapi.py b/test/jit/test_backend_nnapi.py index 47e7ab1dab4e..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__).resolve().parent.parent.parent +torch_root = Path(__file__).resolve().parents[2] lib_path = torch_root / "build" / "lib" / "libnnapi_backend.so" diff --git a/test/onnx/dynamo/test_dynamo_with_onnxruntime_backend.py b/test/onnx/dynamo/test_dynamo_with_onnxruntime_backend.py index 1d93fdf553de..e13d8def9fe2 100644 --- a/test/onnx/dynamo/test_dynamo_with_onnxruntime_backend.py +++ b/test/onnx/dynamo/test_dynamo_with_onnxruntime_backend.py @@ -7,6 +7,7 @@ import dataclasses import os import sys import unittest +from pathlib import Path from typing import Tuple import onnxruntime @@ -24,7 +25,8 @@ from torch.testing._internal import common_utils from torch.testing._internal.common_utils import skipIfNNModuleInlined -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +sys.path.append(str(Path(__file__).absolute().parents[1])) + import onnx_test_common diff --git a/test/onnx/onnx_test_common.py b/test/onnx/onnx_test_common.py index 46e0efff46b7..69a9a3b4e556 100644 --- a/test/onnx/onnx_test_common.py +++ b/test/onnx/onnx_test_common.py @@ -45,8 +45,7 @@ _InputArgsType = Optional[ _OutputsType = Sequence[_NumericType] onnx_model_dir = os.path.join( - os.path.dirname(os.path.realpath(__file__)), - os.pardir, + os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "repos", "onnx", "onnx", @@ -54,11 +53,7 @@ onnx_model_dir = os.path.join( "test", "data", ) - - pytorch_converted_dir = os.path.join(onnx_model_dir, "pytorch-converted") - - pytorch_operator_dir = os.path.join(onnx_model_dir, "pytorch-operator") diff --git a/test/onnx/torch_export/test_torch_export_with_onnxruntime.py b/test/onnx/torch_export/test_torch_export_with_onnxruntime.py index df51b02584ff..3f4deda7dcbd 100644 --- a/test/onnx/torch_export/test_torch_export_with_onnxruntime.py +++ b/test/onnx/torch_export/test_torch_export_with_onnxruntime.py @@ -1,8 +1,8 @@ # Owner(s): ["module: onnx"] from __future__ import annotations -import os import sys +from pathlib import Path import torch import torch.onnx @@ -10,7 +10,8 @@ from torch.testing._internal import common_utils from torch.utils import _pytree as torch_pytree -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +sys.path.append(str(Path(__file__).absolute().parents[1])) + import onnx_test_common diff --git a/test/quantization/core/test_docs.py b/test/quantization/core/test_docs.py index 646236699245..2222ef64b62e 100644 --- a/test/quantization/core/test_docs.py +++ b/test/quantization/core/test_docs.py @@ -51,7 +51,7 @@ class TestQuantizationDocs(QuantizationTestCase): "been updated to have the correct relative path between " "test_docs.py and the docs." ) - pytorch_root = core_dir.parent.parent.parent + pytorch_root = core_dir.parents[2] return pytorch_root / path_from_pytorch path_to_file = get_correct_path(path_from_pytorch) diff --git a/test/test_typing.py b/test/test_typing.py index 7df3096fcc63..bd7998fee7f2 100644 --- a/test/test_typing.py +++ b/test/test_typing.py @@ -30,7 +30,7 @@ DATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "typing")) REVEAL_DIR = os.path.join(DATA_DIR, "reveal") PASS_DIR = os.path.join(DATA_DIR, "pass") FAIL_DIR = os.path.join(DATA_DIR, "fail") -MYPY_INI = os.path.join(DATA_DIR, os.pardir, os.pardir, "mypy.ini") +MYPY_INI = os.path.join(os.path.dirname(os.path.dirname(DATA_DIR)), "mypy.ini") CACHE_DIR = os.path.join(DATA_DIR, ".mypy_cache") diff --git a/tools/amd_build/build_amd.py b/tools/amd_build/build_amd.py index 60a1be73fbb2..26af7eec1fbe 100755 --- a/tools/amd_build/build_amd.py +++ b/tools/amd_build/build_amd.py @@ -4,15 +4,16 @@ import argparse import os import sys +from pathlib import Path -sys.path.append( - os.path.realpath( - os.path.join( - __file__, os.path.pardir, os.path.pardir, os.path.pardir, "torch", "utils" - ) - ) -) +# NOTE: `tools/amd_build/build_amd.py` could be a symlink. +# The behavior of `symlink / '..'` is different from `symlink.parent`. +# Use `pardir` three times rather than using `path.parents[2]`. +REPO_ROOT = ( + Path(__file__).absolute() / os.path.pardir / os.path.pardir / os.path.pardir +).resolve() +sys.path.append(str(REPO_ROOT / "torch" / "utils")) from hipify import hipify_python # type: ignore[import] @@ -53,8 +54,9 @@ parser.add_argument( args = parser.parse_args() +# NOTE: `tools/amd_build/build_amd.py` could be a symlink. amd_build_dir = os.path.dirname(os.path.realpath(__file__)) -proj_dir = os.path.join(os.path.dirname(os.path.dirname(amd_build_dir))) +proj_dir = os.path.dirname(os.path.dirname(amd_build_dir)) if args.project_directory: proj_dir = args.project_directory diff --git a/tools/build_libtorch.py b/tools/build_libtorch.py index b1cda9575c96..192b75c75a0c 100644 --- a/tools/build_libtorch.py +++ b/tools/build_libtorch.py @@ -1,13 +1,13 @@ import argparse import sys -from os.path import abspath, dirname +from pathlib import Path -# By appending pytorch_root to sys.path, this module can import other torch +# By appending REPO_ROOT to sys.path, this module can import other torch # modules even when run as a standalone script. i.e., it's okay either you # do `python build_libtorch.py` or `python -m tools.build_libtorch`. -pytorch_root = dirname(dirname(abspath(__file__))) -sys.path.append(pytorch_root) +REPO_ROOT = Path(__file__).absolute().parent.parent +sys.path.append(str(REPO_ROOT)) from tools.build_pytorch_libs import build_pytorch from tools.setup_helpers.cmake import CMake diff --git a/tools/code_coverage/package/oss/utils.py b/tools/code_coverage/package/oss/utils.py index c4019d762893..a5a5e5112a5d 100644 --- a/tools/code_coverage/package/oss/utils.py +++ b/tools/code_coverage/package/oss/utils.py @@ -43,9 +43,7 @@ def get_llvm_tool_path() -> str: def get_pytorch_folder() -> str: # TOOLS_FOLDER in oss: pytorch/tools/code_coverage return os.path.abspath( - os.environ.get( - "PYTORCH_FOLDER", os.path.join(TOOLS_FOLDER, os.path.pardir, os.path.pardir) - ) + os.environ.get("PYTORCH_FOLDER", os.path.dirname(os.path.dirname(TOOLS_FOLDER))) ) diff --git a/tools/code_coverage/package/util/setting.py b/tools/code_coverage/package/util/setting.py index 42a94409b5e2..9d31fa58f141 100644 --- a/tools/code_coverage/package/util/setting.py +++ b/tools/code_coverage/package/util/setting.py @@ -2,13 +2,12 @@ from __future__ import annotations import os from enum import Enum +from pathlib import Path # HOME_DIR = os.environ["HOME"] -TOOLS_FOLDER = os.path.join( - os.path.dirname(os.path.realpath(__file__)), os.path.pardir, os.path.pardir -) +TOOLS_FOLDER = str(Path(__file__).resolve().parents[2]) # diff --git a/tools/gen_vulkan_spv.py b/tools/gen_vulkan_spv.py index a64fb45591f2..767ba3925a7d 100644 --- a/tools/gen_vulkan_spv.py +++ b/tools/gen_vulkan_spv.py @@ -10,24 +10,28 @@ import glob import io import os import re -import sys -from itertools import product - -sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) import subprocess +import sys import textwrap from dataclasses import dataclass +from itertools import product +from pathlib import Path from typing import Any import yaml from yaml.constructor import ConstructorError from yaml.nodes import MappingNode + try: from yaml import CLoader as Loader except ImportError: from yaml import Loader # type: ignore[assignment, misc] + +REPO_ROOT = Path(__file__).absolute().parent.parent +sys.path.append(str(REPO_ROOT)) + CPP_H_NAME = "spv.h" CPP_SRC_NAME = "spv.cpp" diff --git a/tools/linter/adapters/s3_init.py b/tools/linter/adapters/s3_init.py index 3f2649bd225c..80e61efb612f 100644 --- a/tools/linter/adapters/s3_init.py +++ b/tools/linter/adapters/s3_init.py @@ -26,10 +26,7 @@ try: PYTORCH_ROOT = result.stdout.decode("utf-8").strip() except subprocess.CalledProcessError: # If git is not installed, compute repo root as 3 folders up from this file - path_ = os.path.abspath(__file__) - for _ in range(4): - path_ = os.path.dirname(path_) - PYTORCH_ROOT = path_ + PYTORCH_ROOT = str(Path(__file__).absolute().parents[3]) DRY_RUN = False diff --git a/tools/onnx/update_default_opset_version.py b/tools/onnx/update_default_opset_version.py index 8c9710da37b1..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__).parent.parent.parent.resolve() + pytorch_dir = Path(__file__).parents[2].resolve() onnx_dir = pytorch_dir / "third_party" / "onnx" os.chdir(onnx_dir) diff --git a/tools/setup_helpers/cmake.py b/tools/setup_helpers/cmake.py index 84e4dad32d31..abb9aa4890d2 100644 --- a/tools/setup_helpers/cmake.py +++ b/tools/setup_helpers/cmake.py @@ -8,6 +8,7 @@ import platform import sys import sysconfig from distutils.version import LooseVersion +from pathlib import Path from subprocess import CalledProcessError, check_call, check_output from typing import Any, cast @@ -173,9 +174,7 @@ class CMake: toolset_expr = ",".join([f"{k}={v}" for k, v in toolset_dict.items()]) args.append("-T" + toolset_expr) - base_dir = os.path.dirname( - os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - ) + base_dir = str(Path(__file__).absolute().parents[2]) install_dir = os.path.join(base_dir, "torch") _mkdir_p(install_dir) diff --git a/tools/setup_helpers/gen.py b/tools/setup_helpers/gen.py index d7e63d9ed4ae..fb3b21fbc8c4 100644 --- a/tools/setup_helpers/gen.py +++ b/tools/setup_helpers/gen.py @@ -1,11 +1,11 @@ # Little stub file to get BUILD.bazel to play along -import os.path import sys +from pathlib import Path -root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -sys.path.insert(0, root) +REPO_ROOT = Path(__file__).absolute().parents[2] +sys.path.insert(0, str(REPO_ROOT)) import torchgen.gen diff --git a/tools/setup_helpers/gen_unboxing.py b/tools/setup_helpers/gen_unboxing.py index 91c61f0ab204..6e733d710596 100644 --- a/tools/setup_helpers/gen_unboxing.py +++ b/tools/setup_helpers/gen_unboxing.py @@ -1,11 +1,11 @@ # Little stub file to get BUILD.bazel to play along -import os.path import sys +from pathlib import Path -root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -sys.path.insert(0, root) +REPO_ROOT = Path(__file__).absolute().parents[2] +sys.path.insert(0, str(REPO_ROOT)) import tools.jit.gen_unboxing diff --git a/tools/setup_helpers/generate_code.py b/tools/setup_helpers/generate_code.py index 9fee1909970d..6e0a64888f0a 100644 --- a/tools/setup_helpers/generate_code.py +++ b/tools/setup_helpers/generate_code.py @@ -15,6 +15,7 @@ try: except ImportError: from yaml import SafeLoader as YamlLoader # type: ignore[assignment, misc] + NATIVE_FUNCTIONS_PATH = "aten/src/ATen/native/native_functions.yaml" TAGS_PATH = "aten/src/ATen/native/tags.yaml" @@ -110,8 +111,9 @@ def get_selector( operators_yaml_path: str | None, ) -> Any: # cwrap depends on pyyaml, so we can't import it earlier - root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) - sys.path.insert(0, root) + REPO_ROOT = Path(__file__).absolute().parents[2] + sys.path.insert(0, str(REPO_ROOT)) + from torchgen.selective_build.selector import SelectiveBuilder assert not ( diff --git a/tools/stats/export_test_times.py b/tools/stats/export_test_times.py index 9bfb6b3810b5..ae8771883303 100644 --- a/tools/stats/export_test_times.py +++ b/tools/stats/export_test_times.py @@ -2,8 +2,9 @@ import sys from pathlib import Path -REPO_ROOT = Path(__file__).resolve().parent.parent.parent +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 57ceb9b8b49f..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__).resolve().parent.parent.parent +REPO_ROOT = Path(__file__).resolve().parents[2] def get_disabled_issues() -> list[str]: diff --git a/tools/test/heuristics/test_heuristics.py b/tools/test/heuristics/test_heuristics.py index a47292615039..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__).resolve().parent.parent.parent.parent +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 59b6c13e6397..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__).resolve().parent.parent.parent.parent +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 6deb797d31c6..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__).resolve().parent.parent.parent.parent +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_gen_backend_stubs.py b/tools/test/test_gen_backend_stubs.py index 303c6f227a86..5c7b5e4cfc83 100644 --- a/tools/test/test_gen_backend_stubs.py +++ b/tools/test/test_gen_backend_stubs.py @@ -2,7 +2,6 @@ from __future__ import annotations -import os import tempfile import unittest @@ -12,10 +11,6 @@ from torchgen.gen import _GLOBAL_PARSE_NATIVE_YAML_CACHE # noqa: F401 from torchgen.gen_backend_stubs import run -path = os.path.dirname(os.path.realpath(__file__)) -gen_backend_stubs_path = os.path.join(path, "../torchgen/gen_backend_stubs.py") - - # gen_backend_stubs.py is an integration point that is called directly by external backends. # The tests here are to confirm that badly formed inputs result in reasonable error messages. class TestGenBackendStubs(expecttest.TestCase): diff --git a/tools/test/test_test_run.py b/tools/test/test_test_run.py index 7e9a8f6685c6..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__).resolve().parent.parent.parent +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 5e3e7a949fa3..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__).resolve().parent.parent.parent +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 996c87a39420..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__).resolve().parent.parent.parent +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 3cebaf44756e..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__).resolve().parent.parent.parent +REPO_ROOT = Path(__file__).resolve().parents[2] def parse_test_module(test: str) -> str: diff --git a/tools/testing/do_target_determination_for_s3.py b/tools/testing/do_target_determination_for_s3.py index a280e5bfaf25..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__).resolve().parent.parent.parent +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 bc7736194f48..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__).parent.parent.parent +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 760fed9ad4ee..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__).resolve().parent.parent.parent +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 e6576979de93..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__).resolve().parent.parent.parent.parent +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 ae1ef5ab2603..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__).parent.parent.parent.parent +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 b046f96dafbb..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__).resolve().parent.parent.parent.parent.parent +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 a17145a7eca1..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__).resolve().parent.parent.parent.parent.parent +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 86a71b768683..d9e9b002e379 100644 --- a/tools/testing/target_determination/heuristics/utils.py +++ b/tools/testing/target_determination/heuristics/utils.py @@ -15,7 +15,8 @@ from warnings import warn if TYPE_CHECKING: from tools.testing.test_run import TestRun -REPO_ROOT = Path(__file__).resolve().parent.parent.parent.parent.parent + +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 b74f5b954c4a..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__).resolve().parent.parent.parent +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/torch/_inductor/runtime/compile_tasks.py b/torch/_inductor/runtime/compile_tasks.py index 17788ab7920f..3a9626875710 100644 --- a/torch/_inductor/runtime/compile_tasks.py +++ b/torch/_inductor/runtime/compile_tasks.py @@ -5,6 +5,7 @@ import functools import os import sys import warnings +from pathlib import Path from types import ModuleType from typing import Any, Callable, Dict @@ -51,15 +52,13 @@ def _reload_python_module(key, path): def _set_triton_ptxas_path() -> None: if os.environ.get("TRITON_PTXAS_PATH") is not None: return - ptxas_path = os.path.abspath( - os.path.join(os.path.dirname(__file__), "..", "bin", "ptxas") - ) - if not os.path.exists(ptxas_path): + ptxas = Path(__file__).absolute().parents[1] / "bin" / "ptxas" + if not ptxas.exists(): return - if os.path.isfile(ptxas_path) and os.access(ptxas_path, os.X_OK): - os.environ["TRITON_PTXAS_PATH"] = ptxas_path + if ptxas.is_file() and os.access(ptxas, os.X_OK): + os.environ["TRITON_PTXAS_PATH"] = str(ptxas) else: - warnings.warn(f"{ptxas_path} exists but is not an executable") + warnings.warn(f"{ptxas} exists but is not an executable") def _worker_compile_triton(load_kernel: Callable[[], Any], extra_env: Dict[str, str]): diff --git a/torch/testing/_internal/common_utils.py b/torch/testing/_internal/common_utils.py index 2a9f90b2baf0..bef72ad54551 100644 --- a/torch/testing/_internal/common_utils.py +++ b/torch/testing/_internal/common_utils.py @@ -5008,7 +5008,7 @@ def find_library_location(lib_name: str) -> Path: path = torch_root / 'lib' / lib_name if os.path.exists(path): return path - torch_root = Path(__file__).resolve().parent.parent.parent + torch_root = Path(__file__).resolve().parents[2] return torch_root / 'build' / 'lib' / lib_name def skip_but_pass_in_sandcastle(reason): diff --git a/torchgen/_autoheuristic/mixed_mm/gen_data_mixed_mm.py b/torchgen/_autoheuristic/mixed_mm/gen_data_mixed_mm.py index fddcfe813547..48dfa788977d 100644 --- a/torchgen/_autoheuristic/mixed_mm/gen_data_mixed_mm.py +++ b/torchgen/_autoheuristic/mixed_mm/gen_data_mixed_mm.py @@ -1,13 +1,12 @@ # mypy: ignore-errors -import os import random import sys - - -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) - +from pathlib import Path from typing import Any + +sys.path.append(str(Path(__file__).absolute().parents[1])) + from benchmark_runner import BenchmarkRunner # type: ignore[import-not-found] from benchmark_utils import ( # type: ignore[import-not-found] fits_in_memory, diff --git a/torchgen/_autoheuristic/mixed_mm/test_mixed_mm.py b/torchgen/_autoheuristic/mixed_mm/test_mixed_mm.py index 839e7ff87b2b..d7add94ec127 100644 --- a/torchgen/_autoheuristic/mixed_mm/test_mixed_mm.py +++ b/torchgen/_autoheuristic/mixed_mm/test_mixed_mm.py @@ -1,12 +1,12 @@ -import os import sys import unittest - - -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from pathlib import Path from expecttest import TestCase + +sys.path.append(str(Path(__file__).absolute().parents[1])) + from test_utils import read_file_to_string, run_bash # type: ignore[import-not-found] diff --git a/torchgen/_autoheuristic/mixed_mm/train_decision_mixedmm.py b/torchgen/_autoheuristic/mixed_mm/train_decision_mixedmm.py index df96f020dc6d..4316d36eaf5b 100644 --- a/torchgen/_autoheuristic/mixed_mm/train_decision_mixedmm.py +++ b/torchgen/_autoheuristic/mixed_mm/train_decision_mixedmm.py @@ -1,9 +1,9 @@ # mypy: ignore-errors -import os import sys +from pathlib import Path -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +sys.path.append(str(Path(__file__).absolute().parents[1])) from train_decision import AHTrainDecisionTree diff --git a/torchgen/_autoheuristic/mm/gen_data_mm.py b/torchgen/_autoheuristic/mm/gen_data_mm.py index 4e1e819869aa..8ad6dc1c008d 100644 --- a/torchgen/_autoheuristic/mm/gen_data_mm.py +++ b/torchgen/_autoheuristic/mm/gen_data_mm.py @@ -1,13 +1,12 @@ import itertools -import os import random import sys - - -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) - +from pathlib import Path from typing import Any + +sys.path.append(str(Path(__file__).absolute().parents[1])) + from benchmark_runner import BenchmarkRunner # type: ignore[import-not-found] from benchmark_utils import ( # type: ignore[import-not-found] fits_in_memory, diff --git a/torchgen/_autoheuristic/mm/train_decision_mm.py b/torchgen/_autoheuristic/mm/train_decision_mm.py index 945dcc985614..0eecee8146be 100644 --- a/torchgen/_autoheuristic/mm/train_decision_mm.py +++ b/torchgen/_autoheuristic/mm/train_decision_mm.py @@ -1,11 +1,11 @@ # mypy: ignore-errors -import os import sys +from pathlib import Path import pandas as pd # type: ignore[import-untyped] -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +sys.path.append(str(Path(__file__).absolute().parents[1])) from train_decision import AHTrainDecisionTree diff --git a/torchgen/_autoheuristic/pad_mm/gen_data_pad_mm.py b/torchgen/_autoheuristic/pad_mm/gen_data_pad_mm.py index 80366bc2fbe5..d5ddc44c1b7b 100644 --- a/torchgen/_autoheuristic/pad_mm/gen_data_pad_mm.py +++ b/torchgen/_autoheuristic/pad_mm/gen_data_pad_mm.py @@ -1,12 +1,11 @@ -import os import random import sys - - -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) - +from pathlib import Path from typing import Any + +sys.path.append(str(Path(__file__).absolute().parents[1])) + from benchmark_runner import BenchmarkRunner # type: ignore[import-not-found] from benchmark_utils import ( # type: ignore[import-not-found] fits_in_memory, diff --git a/torchgen/_autoheuristic/pad_mm/test_pad_mm.py b/torchgen/_autoheuristic/pad_mm/test_pad_mm.py index 6469a6cd37de..dcb7920b83de 100644 --- a/torchgen/_autoheuristic/pad_mm/test_pad_mm.py +++ b/torchgen/_autoheuristic/pad_mm/test_pad_mm.py @@ -1,12 +1,12 @@ -import os import sys import unittest - - -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from pathlib import Path from expecttest import TestCase + +sys.path.append(str(Path(__file__).absolute().parents[1])) + from test_utils import read_file_to_string, run_bash # type: ignore[import-not-found] diff --git a/torchgen/_autoheuristic/pad_mm/train_decision_pad_mm.py b/torchgen/_autoheuristic/pad_mm/train_decision_pad_mm.py index 9ed37b7a00d8..9e37892146f4 100644 --- a/torchgen/_autoheuristic/pad_mm/train_decision_pad_mm.py +++ b/torchgen/_autoheuristic/pad_mm/train_decision_pad_mm.py @@ -1,9 +1,9 @@ # mypy: ignore-errors -import os import sys +from pathlib import Path -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +sys.path.append(str(Path(__file__).absolute().parents[1])) from train_decision import AHTrainDecisionTree diff --git a/torchgen/_autoheuristic/pad_mm/train_pad_mm.py b/torchgen/_autoheuristic/pad_mm/train_pad_mm.py index ab60c44dac02..58c4802d4977 100644 --- a/torchgen/_autoheuristic/pad_mm/train_pad_mm.py +++ b/torchgen/_autoheuristic/pad_mm/train_pad_mm.py @@ -1,9 +1,9 @@ # mypy: ignore-errors -import os import sys +from pathlib import Path -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +sys.path.append(str(Path(__file__).absolute().parents[1])) from train_regression import AHTrainRegressionTree diff --git a/torchgen/_autoheuristic/pad_mm/train_regression_pad_mm.py b/torchgen/_autoheuristic/pad_mm/train_regression_pad_mm.py index e9cdbf517e02..bf9201e49a2b 100644 --- a/torchgen/_autoheuristic/pad_mm/train_regression_pad_mm.py +++ b/torchgen/_autoheuristic/pad_mm/train_regression_pad_mm.py @@ -1,9 +1,9 @@ # mypy: ignore-errors -import os import sys +from pathlib import Path -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +sys.path.append(str(Path(__file__).absolute().parents[1])) from train_regression import AHTrainRegressionTree diff --git a/torchgen/gen_backend_stubs.py b/torchgen/gen_backend_stubs.py index b891c17671fc..299a41081da0 100644 --- a/torchgen/gen_backend_stubs.py +++ b/torchgen/gen_backend_stubs.py @@ -533,7 +533,7 @@ def run( source_yaml: str, output_dir: str, dry_run: bool, impl_path: str | None = None ) -> None: # Assumes that this file lives at PYTORCH_ROOT/torchgen/gen_backend_stubs.py - pytorch_root = Path(__file__).parent.parent.absolute() + pytorch_root = Path(__file__).absolute().parent.parent template_dir = os.path.join(pytorch_root, "aten/src/ATen/templates") def make_file_manager(install_dir: str) -> FileManager: diff --git a/torchgen/gen_lazy_tensor.py b/torchgen/gen_lazy_tensor.py index a15fa62fd1ee..e397561d378e 100644 --- a/torchgen/gen_lazy_tensor.py +++ b/torchgen/gen_lazy_tensor.py @@ -256,7 +256,7 @@ def main() -> None: options = parser.parse_args() # Assumes that this file lives at PYTORCH_ROOT/torchgen/gen_backend_stubs.py - torch_root = Path(__file__).parent.parent.parent.absolute() + torch_root = Path(__file__).absolute().parents[2] aten_path = str(torch_root / "aten" / "src" / "ATen") lazy_ir_generator: type[GenLazyIR] = default_args.lazy_ir_generator if options.gen_ts_lowerings: