Skip to content

Commit 1bf9426

Browse files
committed
CUDA backend: move merge_ptes into backends/cuda/scripts
Stacked on the remap_pte_devices PR, which starts the scripts directory. merge_ptes is the other standalone CUDA backend tool, so it belongs there too: backends/cuda/merge_ptes.py moves to backends/cuda/scripts/merge_ptes.py, and every reference (the CUDA CI workflow, the CUDA examples README, its tests, and the :merge_ptes Buck target) now uses executorch.backends.cuda.scripts.merge_ptes. The old module path is retired outright; the release-notes: devtools label covers the move. While moving, both outputs of merge_ptes write through the shared _scripts_atomic_output writer the remap tool introduced (now its own :scripts_atomic_output library both tools depend on), so they keep the inputs' permissions and remove their temp files when a write fails, instead of leaving 0600 or truncated files behind. The --output-ptd argument check now runs before any output is written. Test Plan: - backends/cuda/tests/test_merge_ptes.py: import strings point at the new module path, and the CLI test now checks the merged PTE keeps its first input's permissions with no temp files left. The suite needs flatc, which this environment lacks, and runs in CI. - backends/cuda/tests/test_remap_pte_devices.py still passes (17 tests) on this stack.
1 parent c1d982c commit 1bf9426

5 files changed

Lines changed: 39 additions & 22 deletions

File tree

‎.github/workflows/cuda.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ jobs:
365365
366366
merged_dir="${RUNNER_ARTIFACT_DIR}/merged"
367367
mkdir -p "${merged_dir}"
368-
python -m executorch.backends.cuda.merge_ptes \
368+
python -m executorch.backends.cuda.scripts.merge_ptes \
369369
--input-pte "${RUNNER_ARTIFACT_DIR}/native-sm80/linear/linear.pte" \
370370
--input-pte "${RUNNER_ARTIFACT_DIR}/native-sm86/linear/linear.pte" \
371371
--input-ptd "${sm80_ptd}" \

‎backends/cuda/BUCK‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,28 @@ fbcode_target(
148148
],
149149
)
150150

151+
fbcode_target(
152+
_kind = runtime.python_library,
153+
name = "scripts_atomic_output",
154+
srcs = [
155+
"scripts/__init__.py",
156+
"scripts/_atomic_output.py",
157+
],
158+
visibility = [
159+
"//executorch/backends/cuda/...",
160+
],
161+
)
162+
151163
fbcode_target(
152164
_kind = runtime.python_library,
153165
name = "merge_ptes",
154166
srcs = [
155-
"merge_ptes.py",
167+
"scripts/merge_ptes.py",
156168
],
157169
visibility = ["PUBLIC"],
158170
deps = [
159171
":cuda_backend",
172+
":scripts_atomic_output",
160173
"//caffe2:torch",
161174
"//executorch/exir/_serialize:lib",
162175
"//executorch/exir:schema",
@@ -168,12 +181,11 @@ fbcode_target(
168181
_kind = runtime.python_library,
169182
name = "remap_pte_devices",
170183
srcs = [
171-
"scripts/__init__.py",
172-
"scripts/_atomic_output.py",
173184
"scripts/remap_pte_devices.py",
174185
],
175186
visibility = ["PUBLIC"],
176187
deps = [
188+
":scripts_atomic_output",
177189
"//executorch/exir/_serialize:lib",
178190
"//executorch/exir/passes:propagate_device_pass",
179191
"//executorch/exir:schema",
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
import argparse
88
import copy
99
import hashlib
10-
import os
1110
import shutil
12-
import tempfile
1311
from dataclasses import dataclass, replace
1412
from pathlib import Path
1513
from typing import Dict, Iterable, List, Optional, Sequence, Tuple
@@ -23,6 +21,7 @@
2321
decode_cuda_aoti_metadata,
2422
encode_cuda_aoti_metadata,
2523
)
24+
from executorch.backends.cuda.scripts._atomic_output import write_atomically
2625
from executorch.exir._serialize._cord import Cord
2726
from executorch.exir._serialize._named_data_store import (
2827
NamedDataStore,
@@ -44,7 +43,6 @@
4443
FlatTensorHeader,
4544
)
4645

47-
4846
CUDA_BACKEND_ID = "CudaBackend"
4947

5048

@@ -700,6 +698,8 @@ def main() -> None:
700698
raise ValueError("--input-ptd must be provided once per --input-pte")
701699
if args.input_ptd and args.output_ptd is None:
702700
raise ValueError("--output-ptd is required when --input-ptd is provided")
701+
if not args.input_ptd and args.output_ptd is not None:
702+
raise ValueError("--output-ptd requires --input-ptd")
703703
if args.fallback_ptd is not None and args.fallback_pte is None:
704704
raise ValueError("--fallback-ptd requires --fallback-pte")
705705
sources = [
@@ -718,20 +718,19 @@ def main() -> None:
718718
else None
719719
)
720720
result = merge_cuda_pte_files_with_provenance(sources, fallback)
721-
args.output_pte.parent.mkdir(parents=True, exist_ok=True)
722-
with tempfile.NamedTemporaryFile(
723-
dir=args.output_pte.parent, prefix=f".{args.output_pte.name}.", delete=False
724-
) as temporary:
725-
temporary_path = Path(temporary.name)
726-
result.pte.write_to_file(temporary)
727-
os.replace(temporary_path, args.output_pte)
721+
write_atomically(
722+
args.output_pte, result.pte.write_to_file, mode_from=sources[0].pte_path
723+
)
728724

729725
if args.output_ptd is not None:
730-
if not args.input_ptd:
731-
raise ValueError("--output-ptd requires --input-ptd")
732-
args.output_ptd.parent.mkdir(parents=True, exist_ok=True)
726+
# Checked above: --input-ptd was provided with --output-ptd.
733727
if args.input_ptd[0].resolve() != args.output_ptd.resolve():
734-
shutil.copyfile(args.input_ptd[0], args.output_ptd)
728+
with args.input_ptd[0].open("rb") as source:
729+
write_atomically(
730+
args.output_ptd,
731+
lambda output: shutil.copyfileobj(source, output),
732+
mode_from=args.input_ptd[0],
733+
)
735734

736735
print("Merged CUDA code provenance:")
737736
print("delegate\tkind\ttarget\tsource PTE")

‎backends/cuda/tests/test_merge_ptes.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
decode_cuda_aoti_metadata,
2121
encode_cuda_aoti_metadata,
2222
)
23-
from executorch.backends.cuda.merge_ptes import (
23+
from executorch.backends.cuda.scripts.merge_ptes import (
2424
CudaPteInput,
2525
main as merge_main,
2626
merge_cuda_pte_files,
@@ -46,7 +46,7 @@
4646
from executorch.extension.flat_tensor.serialize.serialize import FlatTensorSerializer
4747

4848

49-
@patch("executorch.backends.cuda.merge_ptes.torch.version.hip", None)
49+
@patch("executorch.backends.cuda.scripts.merge_ptes.torch.version.hip", None)
5050
class TestMergeCudaPtes(unittest.TestCase):
5151
def _write_artifact(
5252
self,
@@ -585,7 +585,7 @@ def test_rejects_fallback_without_ptx(self) -> None:
585585

586586
def test_rejects_rocm(self) -> None:
587587
with patch(
588-
"executorch.backends.cuda.merge_ptes.torch.version.hip", "6.3"
588+
"executorch.backends.cuda.scripts.merge_ptes.torch.version.hip", "6.3"
589589
), self.assertRaisesRegex(RuntimeError, "only NVIDIA CUDA"):
590590
merge_cuda_pte_files([])
591591

@@ -603,6 +603,7 @@ def test_cli_writes_merged_pte_and_reuses_base_ptd(self) -> None:
603603
second = self._write_artifact(sm120, 120, b"sm120-so", b"weight")
604604
fallback = self._write_artifact(fallback_dir, 75, b"fallback-so", b"weight")
605605
self.assertIsNotNone(first.ptd_path)
606+
first.pte_path.chmod(0o640)
606607
output_pte = output / "model.pte"
607608
output_ptd = output / "aoti_cuda_blob.ptd"
608609

@@ -631,6 +632,11 @@ def test_cli_writes_merged_pte_and_reuses_base_ptd(self) -> None:
631632
merge_main()
632633

633634
self.assertTrue(output_pte.is_file())
635+
self.assertEqual(output_pte.stat().st_mode & 0o777, 0o640)
636+
self.assertEqual(
637+
{entry.name for entry in output.iterdir()},
638+
{"model.pte", "aoti_cuda_blob.ptd"},
639+
)
634640
assert first.ptd_path is not None
635641
self.assertEqual(output_ptd.read_bytes(), first.ptd_path.read_bytes())
636642
report = stdout.getvalue()

‎examples/cuda/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ and weights can be combined so the runtime selects an exactly matching native
4343
AOTI library, or uses one explicitly designated PTX fallback.
4444

4545
```bash
46-
python -m executorch.backends.cuda.merge_ptes \
46+
python -m executorch.backends.cuda.scripts.merge_ptes \
4747
--input-pte a100/model.pte \
4848
--input-pte rtx5090/model.pte \
4949
--input-ptd a100/aoti_cuda_blob.ptd \

0 commit comments

Comments
 (0)