Skip to content

[BE][Easy] use pathlib.Path instead of dirname / ".." / pardir#129374

Closed
XuehaiPan wants to merge 24 commits intogh/XuehaiPan/67/basefrom
gh/XuehaiPan/67/head
Closed

[BE][Easy] use pathlib.Path instead of dirname / ".." / pardir#129374
XuehaiPan wants to merge 24 commits intogh/XuehaiPan/67/basefrom
gh/XuehaiPan/67/head

Conversation

@XuehaiPan
Copy link
Copy Markdown
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
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Collaborator

@XuehaiPan your PR has been successfully reverted.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Oct 3, 2024

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@XuehaiPan
Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorchmergebot
Copy link
Copy Markdown
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
Copy Markdown
Collaborator

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

@wdvr
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

@malfet
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Collaborator

@XuehaiPan your PR has been successfully reverted.

Comment on lines +10 to +11
REPO_ROOT = Path(__file__).absolute().parents[2]
sys.path.append(str((REPO_ROOT / "torch" / "utils").resolve()))
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorchmergebot
Copy link
Copy Markdown
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

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.

8 participants