[ROCm] Modify hipify script to work with Windows paths (#135360)

This change modifies the `hipify_python.py` script to properly detect all directories, `include` and `ignore` paths during hipification process on Windows, by changing the path syntax convention to a UNIX-like one.

Since in many places the script assumes a UNIX-like convention by using paths with forward slashes `/`, I decided to accommodate for it by converting Windows paths to UNIX-like ones. By doing it so, the number of changes to the file is limited. Moreover this early-on unification allows for the rest of the code to have a battle-tested linux-like behaviour.

Another option would be to use `Path` object from `pathlib` to represent all paths in the script, however, it would impact a broader share of a code and would hence require a more meticulous evaluation in terms of non-altered logic and edge cases.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/135360
Approved by: https://github.com/jeffdaily, https://github.com/jithunnair-amd
This commit is contained in:
Michal Gallus
2024-10-04 23:43:43 +00:00
committed by PyTorch MergeBot
parent 8b6774d381
commit 79562f3af8

View File

@ -137,6 +137,10 @@ class GeneratedFileCleaner:
os.rmdir(d)
# Follow UNIX convention for paths to use '/' instead of '\\' on Windows
def _to_unix_path(path: str) -> str:
return path.replace(os.sep, '/')
def match_extensions(filename: str, extensions: Iterable) -> bool:
"""Helper method to see if filename ends with certain extension"""
return any(filename.endswith(e) for e in extensions)
@ -173,8 +177,8 @@ def matched_files_iter(
dirs.remove("third_party")
dirs.append("third_party/nvfuser")
for filename in filenames:
filepath = os.path.join(abs_dirpath, filename)
rel_filepath = os.path.join(rel_dirpath, filename)
filepath = _to_unix_path(os.path.join(abs_dirpath, filename))
rel_filepath = _to_unix_path(os.path.join(rel_dirpath, filename))
# We respect extensions, UNLESS you wrote the entire
# filename verbatim, in which case we always accept it
if (
@ -821,7 +825,7 @@ def preprocessor(
hipify_result.current_state = CurrentState.DONE
return hipify_result
rel_filepath = os.path.relpath(filepath, output_directory)
rel_filepath = _to_unix_path(os.path.relpath(filepath, output_directory))
with open(fin_path, encoding='utf-8') as fin:
if fin.readline() == HIPIFY_C_BREADCRUMB:
@ -1113,6 +1117,9 @@ def hipify(
if not os.path.exists(output_directory):
shutil.copytree(project_directory, output_directory)
includes = list(map(_to_unix_path, includes))
ignores = list(map(_to_unix_path, ignores))
all_files = list(matched_files_iter(output_directory, includes=includes,
ignores=ignores, extensions=extensions,
out_of_place_only=out_of_place_only,