Skip to content

Commit a209266

Browse files
desertfirepytorchmergebot
authored andcommitted
[AOTI] Refactor path operations in AotCodeCompiler (pytorch#143350)
Summary: Use safer pathlib operation instead of direct string manipulation; Update some path naming to make them more meaningful. Pull Request resolved: pytorch#143350 Approved by: https://github.com/yushangdi, https://github.com/chenyang78
1 parent 24a18d7 commit a209266

File tree

1 file changed

+42
-54
lines changed

1 file changed

+42
-54
lines changed

torch/_inductor/codecache.py

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -172,21 +172,6 @@ def use_global_cache() -> bool: # type: ignore[misc]
172172
log = logging.getLogger(__name__)
173173

174174

175-
def cpp_wrapper_cache_dir(name: str) -> str:
176-
cu_str = (
177-
"cpu"
178-
if torch.version.cuda is None
179-
else f'cu{torch.version.cuda.replace(".", "")}'
180-
)
181-
python_version = f"py{sys.version_info.major}{sys.version_info.minor}"
182-
build_folder = f"{python_version}_{cu_str}"
183-
184-
cpp_wrapper_dir = os.path.join(cache_dir(), build_folder)
185-
cpp_wrapper_build_directory = os.path.join(cpp_wrapper_dir, name)
186-
os.makedirs(cpp_wrapper_build_directory, exist_ok=True)
187-
return cpp_wrapper_build_directory
188-
189-
190175
def get_cpp_wrapper_cubin_path_name() -> str:
191176
return "cubin_path" if torch.version.hip is None else "hsaco_path"
192177

@@ -1464,33 +1449,34 @@ def compile(
14641449

14651450
(
14661451
specified_output_path,
1467-
specified_so_name,
1452+
specified_artifact_name,
14681453
) = split_aot_inductor_output_path(config.aot_inductor.output_path)
1469-
key, input_path = write(
1454+
key, cpp_path = write(
14701455
source_code,
14711456
"cpp",
14721457
extra=cpp_command,
14731458
specified_dir=specified_output_path,
14741459
)
14751460

14761461
if config.aot_inductor.package:
1477-
generated_files.append(input_path)
1462+
generated_files.append(cpp_path)
14781463

1479-
output_code_log.info("Output code written to: %s", input_path)
1464+
output_code_log.info("Output code written to: %s", cpp_path)
14801465
trace_structured(
14811466
"graph_dump",
14821467
lambda: {
14831468
"name": "inductor_aot_code",
14841469
"type": "cpp",
1485-
"filename": input_path,
1470+
"filename": cpp_path,
14861471
},
14871472
payload_fn=lambda: source_code,
14881473
)
14891474

14901475
# We use a file lock below to protect FS operations. The lock file
14911476
# is scoped to the 'key', so make sure the consts_s is protected
14921477
# by the same lock:
1493-
consts_specified_dir = os.path.join(os.path.split(input_path)[0], key)
1478+
cpp_path_operator = Path(cpp_path)
1479+
consts_specified_dir = os.path.join(cpp_path_operator.parent, key)
14941480

14951481
def _compile_consts(consts: bytes, platform: str) -> str:
14961482
if platform == "linux":
@@ -1534,10 +1520,7 @@ def _compile_consts(consts: bytes, platform: str) -> str:
15341520
"S",
15351521
specified_dir=consts_specified_dir,
15361522
)
1537-
(
1538-
object_output_name,
1539-
object_output_dir,
1540-
) = get_name_and_dir_from_output_file_path(consts_s)
1523+
consts_s = Path(consts_s)
15411524
object_build_options = CppTorchDeviceOptions(
15421525
# Intel compiler failed to compile this manully constructed assembly file.
15431526
# it is ok to use gcc to compile the .S to a .o and linked with Intel comiler .
@@ -1547,17 +1530,17 @@ def _compile_consts(consts: bytes, platform: str) -> str:
15471530
use_absolute_path=use_absolute_path,
15481531
)
15491532
object_builder = CppBuilder(
1550-
name=object_output_name,
1551-
sources=consts_s,
1552-
output_dir=object_output_dir,
1533+
name=str(consts_s.stem),
1534+
sources=str(consts_s),
1535+
output_dir=str(consts_s.parent),
15531536
BuildOption=object_build_options,
15541537
)
15551538
compile_cmd = object_builder.get_command_line()
15561539
consts_o = object_builder.get_target_file_path()
15571540
if fbcode_aot_cpu_re:
15581541
# TODO: refactor fbcode_aot_cpu_re logic into CppBuilder
1559-
consts_o = os.path.splitext(consts_s)[0] + ".o"
1560-
compile_file(consts_s, consts_o, compile_cmd.split())
1542+
consts_o = str(consts_s.with_suffix(".o"))
1543+
compile_file(str(consts_s), consts_o, compile_cmd.split())
15611544
os.chmod(consts_o, 0o644)
15621545
else:
15631546
run_command_and_check(compile_cmd)
@@ -1582,7 +1565,7 @@ def _compile_consts(consts: bytes, platform: str) -> str:
15821565
lock = FileLock(os.path.join(lock_dir, key + ".lock"), timeout=LOCK_TIMEOUT)
15831566
with lock:
15841567
if serialized_extern_kernel_nodes:
1585-
extern_kernel_nodes_json = os.path.splitext(input_path)[0] + ".json"
1568+
extern_kernel_nodes_json = str(cpp_path_operator.with_suffix(".json"))
15861569
with open(extern_kernel_nodes_json, "w") as f:
15871570
f.write(serialized_extern_kernel_nodes)
15881571

@@ -1593,7 +1576,9 @@ def _compile_consts(consts: bytes, platform: str) -> str:
15931576
metadata["AOTI_DEVICE_KEY"] = device_type
15941577

15951578
# Save user provided metadata
1596-
meta_json = os.path.splitext(input_path)[0] + "_metadata.json"
1579+
meta_json = str(
1580+
cpp_path_operator.with_name(f"{cpp_path_operator.stem}_metadata.json")
1581+
)
15971582
for k, v in config.aot_inductor.metadata.items():
15981583
assert isinstance(k, str) and isinstance(
15991584
v, (str)
@@ -1607,12 +1592,9 @@ def _compile_consts(consts: bytes, platform: str) -> str:
16071592

16081593
output_so = (
16091594
config.aot_inductor.output_path
1610-
if specified_so_name
1611-
else os.path.splitext(input_path)[0] + ".so"
1595+
if specified_artifact_name
1596+
else str(cpp_path_operator.with_suffix(".so"))
16121597
)
1613-
1614-
output_o = os.path.splitext(input_path)[0] + ".o"
1615-
16161598
all_cuda = all(
16171599
graph.get_original_value_of_constant(name).is_cuda
16181600
for name in graph.constants.keys()
@@ -1665,10 +1647,6 @@ def _pad_to_alignment(raw_bytes: bytes) -> bytes:
16651647
if config.aot_inductor.force_mmap_weights:
16661648
use_mmap_weights = True
16671649

1668-
(
1669-
object_output_name,
1670-
object_output_dir,
1671-
) = get_name_and_dir_from_output_file_path(input_path)
16721650
object_build_options = CppTorchDeviceOptions(
16731651
vec_isa=picked_vec_isa,
16741652
device_type=device_type,
@@ -1678,9 +1656,9 @@ def _pad_to_alignment(raw_bytes: bytes) -> bytes:
16781656
use_mmap_weights=use_mmap_weights,
16791657
)
16801658
object_builder = CppBuilder(
1681-
name=object_output_name,
1682-
sources=input_path,
1683-
output_dir=object_output_dir,
1659+
name=str(cpp_path_operator.stem),
1660+
sources=cpp_path,
1661+
output_dir=str(cpp_path_operator.parent),
16841662
BuildOption=object_build_options,
16851663
)
16861664
compile_cmd = object_builder.get_command_line()
@@ -1689,14 +1667,18 @@ def _pad_to_alignment(raw_bytes: bytes) -> bytes:
16891667
log.debug("aot compilation command: %s", compile_cmd)
16901668
if not config.aot_inductor.package_cpp_only:
16911669
if fbcode_aot_cpu_re:
1692-
output_o = os.path.splitext(input_path)[0] + ".o"
1693-
compile_file(input_path, output_o, compile_cmd.split())
1670+
output_o = str(cpp_path_operator.with_suffix(".o"))
1671+
compile_file(cpp_path, output_o, compile_cmd.split())
16941672
os.chmod(output_o, 0o644)
16951673
else:
16961674
run_command_and_check(compile_cmd)
16971675

16981676
if config.aot_inductor.package_cpp_only:
1699-
compile_flags = os.path.splitext(input_path)[0] + "_compile_flags.json"
1677+
compile_flags = str(
1678+
cpp_path_operator.with_name(
1679+
f"{cpp_path_operator.stem}_compile_flags.json"
1680+
)
1681+
)
17001682
object_build_options.save_flags_to_file(compile_flags)
17011683
generated_files.append(compile_flags)
17021684

@@ -1740,22 +1722,28 @@ def _pad_to_alignment(raw_bytes: bytes) -> bytes:
17401722
log.debug("aot linkage command: %s", link_cmd)
17411723

17421724
# Append cmds to the end of codegen-ed wrapper file
1743-
with open(input_path, "a") as f:
1725+
with open(cpp_path, "a") as f:
17441726
f.write("\n")
17451727
f.write(f"// Compile cmd\n// {compile_cmd}\n")
17461728
f.write(f"// Link cmd\n// {link_cmd}\n")
17471729

17481730
if config.aot_inductor.package_cpp_only:
1749-
linker_flags = os.path.splitext(input_path)[0] + "_linker_flags.json"
1731+
linker_flags = str(
1732+
cpp_path_operator.with_name(
1733+
f"{cpp_path_operator.stem}_linker_flags.json"
1734+
)
1735+
)
17501736
so_build_options.save_flags_to_file(linker_flags)
17511737
generated_files.append(linker_flags)
17521738

17531739
# If we only want to package the cpp, then we need to save the
17541740
# weights separately into a bin, and we also need to prevent compiling the so
17551741

17561742
if use_mmap_weights:
1757-
weight_file = (
1758-
os.path.splitext(input_path)[0] + "_serialized_weights.bin"
1743+
weight_file = str(
1744+
cpp_path_operator.with_name(
1745+
f"{cpp_path_operator.stem}_serialized_weights.bin"
1746+
)
17591747
)
17601748
with open(weight_file, "wb") as f_weights:
17611749
f_weights.write(serialized_weights)
@@ -1770,8 +1758,8 @@ def _pad_to_alignment(raw_bytes: bytes) -> bytes:
17701758
if fbcode_aot_cpu_re:
17711759
output_so = (
17721760
config.aot_inductor.output_path
1773-
if specified_so_name
1774-
else os.path.splitext(input_path)[0] + ".so"
1761+
if specified_artifact_name
1762+
else str(cpp_path_operator.with_suffix(".so"))
17751763
)
17761764
compile_file([output_o, consts_o], output_so, link_cmd.split())
17771765
os.chmod(output_so, 0o755)
@@ -1781,7 +1769,7 @@ def _pad_to_alignment(raw_bytes: bytes) -> bytes:
17811769
for o_file in [
17821770
output_o,
17831771
consts_o,
1784-
os.path.splitext(consts_o)[0] + ".S",
1772+
str(Path(consts_o).with_suffix(".S")),
17851773
]:
17861774
# Remove these as they are not needed anymore
17871775
os.remove(o_file)

0 commit comments

Comments
 (0)