Skip to content

Commit

Permalink
Fix hipify_python (#52756)
Browse files Browse the repository at this point in the history
Co-authored-by: rraminen <rraminen@amd.com>
Co-authored-by: Nikita Shulga <nshulga@fb.com>
  • Loading branch information
3 people committed Feb 26, 2021
1 parent 49b74a5 commit 37c1f4a
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions torch/utils/hipify/hipify_python.py 100755 → 100644
Expand Up @@ -174,7 +174,7 @@ def preprocess_file_and_save_result(
result = preprocessor(output_directory, filepath, all_files, includes, stats,
hip_clang_launch, is_pytorch_extension, clean_ctx, show_progress)

fin_path = os.path.join(output_directory, filepath)
fin_path = os.path.abspath(os.path.join(output_directory, filepath))
# Show what happened
if show_progress:
print(
Expand Down Expand Up @@ -711,7 +711,7 @@ def preprocessor(
clean_ctx: GeneratedFileCleaner,
show_progress: bool) -> HipifyResult:
""" Executes the CUDA -> HIP conversion on the specified file. """
fin_path = os.path.join(output_directory, filepath)
fin_path = os.path.abspath(os.path.join(output_directory, filepath))

with open(fin_path, 'r', encoding='utf-8') as fin:
if fin.readline() == HIPIFY_C_BREADCRUMB:
Expand All @@ -721,7 +721,7 @@ def preprocessor(

orig_output_source = output_source

fout_path = os.path.join(output_directory, get_hip_file_path(filepath, is_pytorch_extension))
fout_path = os.path.abspath(os.path.join(output_directory, get_hip_file_path(filepath, is_pytorch_extension)))
if not os.path.exists(os.path.dirname(fout_path)):
clean_ctx.makedirs(os.path.dirname(fout_path))

Expand Down Expand Up @@ -829,9 +829,14 @@ def repl(m):
with open(fout_path, 'r', encoding='utf-8') as fout_old:
do_write = fout_old.read() != output_source
if do_write:
with clean_ctx.open(fout_path, 'w', encoding='utf-8') as fout:
fout.write(output_source)
return {"hipified_path": fout_path, "status": "ok"}
try:
with clean_ctx.open(fout_path, 'w', encoding='utf-8') as fout:
fout.write(output_source)
return {"hipified_path": fout_path, "status": "ok"}
except PermissionError as e:
print(f"{bcolors.WARNING}Failed to save {fout_path} with \"{e.strerror}\", leaving {fin_path} unchanged.{bcolors.ENDC}",
file=sys.stderr)
return {"hipified_path": fin_path, "status": "skipped"}
else:
return {"hipified_path": fout_path, "status": "skipped"}

Expand Down

0 comments on commit 37c1f4a

Please sign in to comment.