Skip to content

Conversation

@XuehaiPan
Copy link
Collaborator

@XuehaiPan XuehaiPan commented Jun 24, 2024

Stack from ghstack (oldest at bottom):

Changes by apply order:

  1. Replace all ".." and os.pardir usage with os.path.dirname(...).

  2. Replace nested os.path.dirname(os.path.dirname(...)) call with str(Path(...).parent.parent).

  3. Reorder .absolute() / .resolve() and .parent: always resolve the path first.

    .parent{...}.absolute() -> .absolute().parent{...}

  4. Replace chained .parent x N with .parents[${N - 1}]: the code is easier to read (see 5.)

    .parent.parent.parent.parent -> .parents[3]

  5. Replace .parents[${N - 1}] with .parents[${N} - 1]: the code is easier to read and does not introduce any runtime overhead.

    .parents[3] -> .parents[4 - 1]

  6. Replace .parents[2 - 1] with .parent.parent: because the code is shorter and easier to read.

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @yf225 @chenyang78 @kadeng @muchulee8 @ColinPeppler @amjames @desertfire @chauhang @aakhundov @rec @peterbell10

[ghstack-poisoned]
@pytorch-bot
Copy link

pytorch-bot bot commented Jun 24, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/129374

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 8df444f with merge base 9694158 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@XuehaiPan XuehaiPan added better-engineering Relatively self-contained tasks for better engineering contributors topic: not user facing topic category labels Jun 24, 2024
@XuehaiPan XuehaiPan changed the title [BE][Easy] use pathlib.Path in tools and torchgen [BE][Easy] use pathlib.Path instead of dirname / ".." / pardir Jun 24, 2024
[ghstack-poisoned]
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Jun 24, 2024
Copy link
Contributor

@malfet malfet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before landing this change, can you please explain why:

  • You sometime replace parents[1] with .parent.parent
  • Why parents[x - 1] notation?
  • Path("/").parents[0] will throw an exception, while Path("/").parent is always safe to call, should this be a considered in some of the cases?

Copy link
Collaborator

@justinchuby justinchuby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onnx changes lgtm

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@Skylion007
Copy link
Collaborator

FYI. Probably fine in this context but Pathlib can be really slow in performance critical situations compared to string handling.

@wdvr
Copy link
Contributor

wdvr commented Dec 26, 2024

@pytorchmergebot revert -m "failing internal ROCM builds with error: ModuleNotFoundError: No module named hipify" -c ghfirst

File "/re_cwd/buck-out/v2/gen/fbcode/a82b0712bf2bb755/caffe2/tools/amd_build/build_amd/build_amd#link-tree/build_amd.py", line 13, in
from hipify import hipify_python # type: ignore[import]
ModuleNotFoundError: No module named 'hipify'

@pytorchmergebot
Copy link
Collaborator

@pytorchbot successfully started a revert job. Check the current status here.
Questions? Feedback? Please reach out to the PyTorch DevX Team

@pytorchmergebot
Copy link
Collaborator

Reverting PR 129374 failed

Reason: Command git -C /home/runner/work/pytorch/pytorch revert --no-edit 2293fe1024812d6349f6e2b3b7de82c6b73f11e4 returned non-zero exit code 1

Auto-merging .github/scripts/delete_old_branches.py
CONFLICT (content): Merge conflict in .github/scripts/delete_old_branches.py
Auto-merging .github/scripts/ensure_actions_will_cancel.py
CONFLICT (content): Merge conflict in .github/scripts/ensure_actions_will_cancel.py
Auto-merging .github/scripts/generate_binary_build_matrix.py
Auto-merging .github/scripts/gitutils.py
CONFLICT (content): Merge conflict in .github/scripts/gitutils.py
Auto-merging .github/scripts/lint_native_functions.py
CONFLICT (content): Merge conflict in .github/scripts/lint_native_functions.py
Auto-merging .github/scripts/test_gitutils.py
Auto-merging test/jit/test_backend_nnapi.py
CONFLICT (content): Merge conflict in test/jit/test_backend_nnapi.py
Auto-merging test/quantization/core/test_docs.py
Auto-merging tools/amd_build/build_amd.py
CONFLICT (content): Merge conflict in tools/amd_build/build_amd.py
Auto-merging tools/code_coverage/package/util/setting.py
CONFLICT (content): Merge conflict in tools/code_coverage/package/util/setting.py
Auto-merging tools/onnx/update_default_opset_version.py
CONFLICT (content): Merge conflict in tools/onnx/update_default_opset_version.py
Auto-merging tools/setup_helpers/generate_code.py
Auto-merging tools/stats/export_test_times.py
CONFLICT (content): Merge conflict in tools/stats/export_test_times.py
Auto-merging tools/stats/import_test_stats.py
CONFLICT (content): Merge conflict in tools/stats/import_test_stats.py
Auto-merging tools/test/heuristics/test_heuristics.py
CONFLICT (content): Merge conflict in tools/test/heuristics/test_heuristics.py
Auto-merging tools/test/heuristics/test_interface.py
CONFLICT (content): Merge conflict in tools/test/heuristics/test_interface.py
Auto-merging tools/test/heuristics/test_utils.py
CONFLICT (content): Merge conflict in tools/test/heuristics/test_utils.py
Auto-merging tools/test/test_test_run.py
CONFLICT (content): Merge conflict in tools/test/test_test_run.py
Auto-merging tools/test/test_test_selections.py
CONFLICT (content): Merge conflict in tools/test/test_test_selections.py
Auto-merging tools/test/test_upload_stats_lib.py
CONFLICT (content): Merge conflict in tools/test/test_upload_stats_lib.py
Auto-merging tools/testing/discover_tests.py
CONFLICT (content): Merge conflict in tools/testing/discover_tests.py
Auto-merging tools/testing/do_target_determination_for_s3.py
CONFLICT (content): Merge conflict in tools/testing/do_target_determination_for_s3.py
Auto-merging tools/testing/explicit_ci_jobs.py
CONFLICT (content): Merge conflict in tools/testing/explicit_ci_jobs.py
Auto-merging tools/testing/modulefinder_determinator.py
CONFLICT (content): Merge conflict in tools/testing/modulefinder_determinator.py
Auto-merging tools/testing/target_determination/gen_artifact.py
CONFLICT (content): Merge conflict in tools/testing/target_determination/gen_artifact.py
Auto-merging tools/testing/target_determination/heuristics/filepath.py
CONFLICT (content): Merge conflict in tools/testing/target_determination/heuristics/filepath.py
Auto-merging tools/testing/target_determination/heuristics/llm.py
CONFLICT (content): Merge conflict in tools/testing/target_determination/heuristics/llm.py
Auto-merging tools/testing/target_determination/heuristics/previously_failed_in_pr.py
CONFLICT (content): Merge conflict in tools/testing/target_determination/heuristics/previously_failed_in_pr.py
Auto-merging tools/testing/target_determination/heuristics/utils.py
CONFLICT (content): Merge conflict in tools/testing/target_determination/heuristics/utils.py
Auto-merging tools/testing/test_selections.py
CONFLICT (content): Merge conflict in tools/testing/test_selections.py
Auto-merging torch/testing/_internal/common_utils.py
CONFLICT (content): Merge conflict in torch/testing/_internal/common_utils.py
error: could not revert 2293fe10248... [BE][Easy] use `pathlib.Path` instead of `dirname` / `".."` / `pardir` (#129374)
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git revert --continue".
hint: You can instead skip this commit with "git revert --skip".
hint: To abort and get back to the state before "git revert",
hint: run "git revert --abort".
hint: Disable this message with "git config advice.mergeConflict false"
Details for Dev Infra team Raised by workflow job

pytorchmergebot added a commit that referenced this pull request Dec 26, 2024
pytorchmergebot referenced this pull request Dec 26, 2024
Changes:

1. Always explicit `.absolute()`: `Path(__file__)` -> `Path(__file__).absolute()`
2. Replace `path.resolve()` with `path.absolute()` if the code is resolving the PyTorch repo root directory.

Pull Request resolved: #129409
Approved by: https://github.com/albanD
@malfet
Copy link
Contributor

malfet commented Dec 26, 2024

@pytorchmergebot revert -m "failing internal ROCM builds with error: ModuleNotFoundError: No module named hipify" -c ghfirst

@pytorchmergebot
Copy link
Collaborator

@pytorchbot successfully started a revert job. Check the current status here.
Questions? Feedback? Please reach out to the PyTorch DevX Team

@pytorchmergebot
Copy link
Collaborator

@XuehaiPan your PR has been successfully reverted.

pytorchmergebot added a commit that referenced this pull request Dec 26, 2024
… `pardir` (#129374)"

This reverts commit 2293fe1.

Reverted #129374 on behalf of https://github.com/malfet due to failing internal ROCM builds with error: ModuleNotFoundError: No module named hipify ([comment](#129374 (comment)))
@pytorchmergebot pytorchmergebot added the ci-no-td Do not run TD on this PR label Dec 26, 2024
Comment on lines 10 to 11
REPO_ROOT = Path(__file__).absolute().parents[2]
sys.path.append(str((REPO_ROOT / "torch" / "utils").resolve()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I vaguely remember talking about it in the past: it will resolve to a different folder if __file__ is a symlink to some other place. I.e. imagine a folder structure like (which is used by build system that bazel/buck, but could be the case of many other builds):

   - actual_files
           - build_amd.py
           - hipify.py
   - deploy 
     - tools
          - amd_build
              - build_amd.py -> ../../../build_amd.py   
     - torch
         -  utils
            - hipify.py -> ../../../hipify.py 

In the previous version of the logic, it will resolve path correctly, but after the reorder it will point to non-existing /torch/utils/ folder

Copy link
Collaborator Author

@XuehaiPan XuehaiPan Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, today I have learned os.path.join(file, os.pardir) may have different behavior of os.path.dirname(file).

  • If file is a regular file, both paths resolve to the same path.
  • If file is a symlink, resolve will treat the "file" as a directory.
import os
from pathlib import Path

REPO_ROOT = Path(__file__).absolute().parents[2]

print(__file__)
print(os.path.realpath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, 'torch', 'utils')))
print(os.path.realpath(os.path.join(__file__, os.pardir, os.pardir, os.pardir, 'torch', 'utils')))
print((REPO_ROOT / "torch" / "utils").resolve())

The output is:

/Users/PanXuehai/Projects/pytorch/tools/amd_build/test.py
/Users/PanXuehai/Projects/pytorch/torch/utils
/Users/PanXuehai/Projects/torch/utils
/Users/PanXuehai/Projects/pytorch/torch/utils

[ghstack-poisoned]
[ghstack-poisoned]
Comment on lines +10 to +16
# 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"))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the patch and added a note for it.

XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Dec 27, 2024
pytorch#129374)

Changes by apply order:

1. Replace all `".."` and `os.pardir` usage with `os.path.dirname(...)`.
2. Replace nested `os.path.dirname(os.path.dirname(...))` call with `str(Path(...).parent.parent)`.
3. Reorder `.absolute()` ~/ `.resolve()`~ and `.parent`: always resolve the path first.

    `.parent{...}.absolute()` -> `.absolute().parent{...}`

4. Replace chained `.parent x N` with `.parents[${N - 1}]`: the code is easier to read (see 5.)

    `.parent.parent.parent.parent` -> `.parents[3]`

5. ~Replace `.parents[${N - 1}]` with `.parents[${N} - 1]`: the code is easier to read and does not introduce any runtime overhead.~

    ~`.parents[3]` -> `.parents[4 - 1]`~

6. ~Replace `.parents[2 - 1]` with `.parent.parent`: because the code is shorter and easier to read.~

Pull Request resolved: pytorch#129374
Approved by: https://github.com/justinchuby, https://github.com/malfet
ghstack-source-id: bc880ec
@XuehaiPan
Copy link
Collaborator Author

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

galexite pushed a commit to graphcore/pytorch-fork that referenced this pull request Dec 30, 2024
pytorch#129374)

Changes by apply order:

1. Replace all `".."` and `os.pardir` usage with `os.path.dirname(...)`.
2. Replace nested `os.path.dirname(os.path.dirname(...))` call with `str(Path(...).parent.parent)`.
3. Reorder `.absolute()` ~/ `.resolve()`~ and `.parent`: always resolve the path first.

    `.parent{...}.absolute()` -> `.absolute().parent{...}`

4. Replace chained `.parent x N` with `.parents[${N - 1}]`: the code is easier to read (see 5.)

    `.parent.parent.parent.parent` -> `.parents[3]`

5. ~Replace `.parents[${N - 1}]` with `.parents[${N} - 1]`: the code is easier to read and does not introduce any runtime overhead.~

    ~`.parents[3]` -> `.parents[4 - 1]`~

6. ~Replace `.parents[2 - 1]` with `.parent.parent`: because the code is shorter and easier to read.~

Pull Request resolved: pytorch#129374
Approved by: https://github.com/justinchuby, https://github.com/malfet
@github-actions github-actions bot deleted the gh/XuehaiPan/67/head branch January 29, 2025 02:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

better-engineering Relatively self-contained tasks for better engineering contributors ci-no-td Do not run TD on this PR ciflow/inductor ciflow/rocm Trigger "default" config CI on ROCm ciflow/trunk Trigger trunk jobs on your pull request Merged module: dynamo module: inductor open source release notes: quantization release notes category release notes: releng release notes category Reverted Stale suppress-bc-linter Suppresses the failures of API backward-compatibility linter (Lint/bc_linter) topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants