8000 [2/N] Use std::filesystem by cyyever · Pull Request #152586 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

[2/N] Use std::filesystem #152586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

[2/N] Use std::filesystem #152586

wants to merge 4 commits into from

Conversation

cyyever
Copy link
Collaborator
@cyyever cyyever commented May 1, 2025

Use std::filesystem in most inductor code. This is follow-up of #152288 .
The check of std::filesystem::create_directories has been fixed because it may return false when the directory to create already exists.
cc @H-Huang @awgu @wanchaol @fegin @fduwjj @wz337 @wconstab @d4l3k

Copy link
pytorch-bot bot commented May 1, 2025

🔗 Helpful Links

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

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

✅ You can merge normally! (2 Unrelated Failures)

As of commit d2f2e86 with merge base 61aa77e (image):

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

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

@cyyever cyyever marked this pull request as draft May 1, 2025 04:10
@cyyever cyyever force-pushed the fs_path2 branch 2 times, most recently from 4691e9b to 80db20b Compare May 1, 2025 06:00
@cyyever cyyever changed the title Fs path2 [2/N] Use std::filesystem May 1, 2025
@cyyever cyyever marked this pull request as ready for review May 1, 2025 06:09
@cyyever cyyever force-pushed the fs_path2 branch 2 times, most recently from 2256eb7 to 9870e67 Compare May 1, 2025 08:43
@pytorch-bot pytorch-bot bot added the oncall: distributed Add this issue/PR to distributed oncall triage queue label May 1, 2025
@cyyever cyyever force-pushed the fs_path2 branch 4 times, most recently from 6fc4158 to 340f73e Compare May 1, 2025 09:58
@cyyever cyyever marked this pull request as draft May 1, 2025 12:49
@cyyever cyyever force-pushed the fs_path2 branch 3 times, most recently from 64b1f8e to c5b9334 Compare May 1, 2025 13:08
@cyyever cyyever marked this pull request as ready for review May 1, 2025 14:16
@cyyever cyyever requested review from malfet, swolchok and desertfire May 1, 2025 14:23
@HDCharles HDCharles requested review from Skylion007 and janeyx99 May 2, 2025 03:55
@HDCharles HDCharles added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label May 2, 2025
@@ -42,7 +42,8 @@ void ProcessGroupUCCLogger::flushComms(int rank, int world_size) {
std::filesystem::path trace_filename =
fullpath / fmt::format("rank{}.json", rank);
std::error_code ec{};
if (!std::filesystem::create_directories(fullpath, ec)) {
std::filesystem::create_directories(fullpath, ec);
Copy link
Collaborator
< 8000 div class="edit-comment-hide">

Choose a reason for hiding this comment

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

What about if create_directory fails because there is a currently a file a the location. Nothing is printed then

Copy link
Collaborator

Choose a reason for hiding this comment

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

The below if should be relying on the error code, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Use std::filesystem::is_directory is more robust.

if (!create_directories(tmp_folder)) {
std::error_code ec{};
std::filesystem::create_directories(tmp_folder, ec);
if (!std::filesystem::is_directory(tmp_folder)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same problem here

@janeyx99 janeyx99 removed their request for review May 2, 2025 15:43
@cyyever
Copy link
Collaborator Author
cyyever commented May 2, 2025

@Skylion007 create_directories may return false if the directory exists. In such cases, we can't rely on std::error_code having a failure value !=0 since the error code is platform dependent (C++ doesn't define a successful value and we have to consider non-UNIX platforms). The most reliable way is to check by std::filesystem::is_directory. If that fails, then std::filesystem:: create_directories should have failed (except for rare race-conditions that some action was applied to the dir externally between the two calls), then by API conversion, std::error_code should record the reason. This line of reasoning leads to the code changes.

In summary, there is not platform independent system call values of success and failure... And it is unlikely that the current implementations are all based on POSIX APIs.

@Skylion007
Copy link
Collaborator

@Skylion007 create_directories may return false if the directory exists. In such cases, we can't rely on std::error_code having a failure value !=0 since the error code is platform dependent (C++ doesn't define a successful value and we have to consider non-UNIX platforms). The most reliable way is to check by std::filesystem::is_directory. If that fails, then std::filesystem:: create_directories should have failed (except for rare race-conditions that some action was applied to the dir externally between the two calls), then by API conversion, std::error_code should record the reason. This line of reasoning leads to the code changes.

In summary, there is not platform independent system call values of success and failure... And it is unlikely that the current implementations are all based on POSIX APIs.

Even if there isn't a platform independent way of checking error, we only really need to check error for POSIX and Windows based systems as the code was doing before.

@cyyever
Copy link
Collaborator Author
cyyever commented May 4, 2025

@Skylion007 On these systems, if the directory didn't exist before creation, std::filesystem::create_directories returns true and std::error_code is the default value. Otherwise std::filesystem::create_directories returns false and std::error_code is an error code. Use std::filesystem::is_directory in the following check is more robust and simple. After all, what we really care is that the directory exists when trying to open file in it.

@cyyever cyyever force-pushed the fs_path2 branch 3 times, most recently from 9b7cabb to ccb2459 Compare May 6, 2025 13:37
@cyyever
Copy link
Collaborator Author
cyyever commented May 6, 2025

We shouldn't support GCC<9 in inductor or distributed code...

@cyyever cyyever force-pushed the fs_path2 branch 2 times, most recently from f117b1e to b0eab07 Compare May 7, 2025 00:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ciflow/inductor oncall: distributed Add this issue/PR to distributed oncall triage queue open source release notes: inductor (aoti) triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0