Skip to content

Commit 7d43a53

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 and breaking-change labels cover the move. While moving, both outputs of merge_ptes write through the shared scripts/_atomic_output writer from the base PR's :scripts_atomic_output library -- the PTD first, so a failed copy leaves the existing output PTE untouched -- 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 (now also registered as a Buck python_unittest): import strings point at the new module path, the CLI test checks PTE and PTD output permissions against distinctive input modes, --output-ptd without --input-ptd writes nothing, and a failed PTD copy leaves neither output nor temp files. The suite needs flatc, which this environment lacks, and runs in CI. - backends/cuda/tests/test_remap_pte_devices.py still passes (20 tests) on this stack.
1 parent 896b9e6 commit 7d43a53

6 files changed

Lines changed: 116 additions & 25 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,12 @@ fbcode_target(
152152
_kind = runtime.python_library,
153153
name = "merge_ptes",
154154
srcs = [
155-
"merge_ptes.py",
155+
"scripts/merge_ptes.py",
156156
],
157157
visibility = ["PUBLIC"],
158158
deps = [
159159
":cuda_backend",
160+
":scripts_atomic_output",
160161
"//caffe2:torch",
161162
"//executorch/exir/_serialize:lib",
162163
"//executorch/exir:schema",
Lines changed: 23 additions & 18 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

@@ -280,14 +278,16 @@ def _validate_shared_weights(
280278
!= reference_entry.storage_nbytes
281279
):
282280
raise ValueError(
283-
f"CUDA weight {fqn!r} has an invalid size in {reference.source.pte_path}"
281+
f"CUDA weight {fqn!r} has an invalid size in "
282+
f"{reference.source.pte_path}"
284283
)
285284
if (
286285
candidate.blob_size(candidate_entry.storage_key)
287286
!= candidate_entry.storage_nbytes
288287
):
289288
raise ValueError(
290-
f"CUDA weight {fqn!r} has an invalid size in {candidate.source.pte_path}"
289+
f"CUDA weight {fqn!r} has an invalid size in "
290+
f"{candidate.source.pte_path}"
291291
)
292292
if reference.blob_sha256(reference_entry.storage_key) != candidate.blob_sha256(
293293
candidate_entry.storage_key
@@ -559,7 +559,9 @@ def _prepare_merged_output(reference: _Artifact) -> Tuple[Program, NamedDataStor
559559
for key, entry in entries.items()
560560
if key not in cuda_so_keys
561561
}
562-
for filename, entries in reference.pte.named_data.external_data.items()
562+
for filename, entries in (
563+
reference.pte.named_data.external_data.items()
564+
)
563565
},
564566
)
565567
)
@@ -700,6 +702,8 @@ def main() -> None:
700702
raise ValueError("--input-ptd must be provided once per --input-pte")
701703
if args.input_ptd and args.output_ptd is None:
702704
raise ValueError("--output-ptd is required when --input-ptd is provided")
705+
if not args.input_ptd and args.output_ptd is not None:
706+
raise ValueError("--output-ptd requires --input-ptd")
703707
if args.fallback_ptd is not None and args.fallback_pte is None:
704708
raise ValueError("--fallback-ptd requires --fallback-pte")
705709
sources = [
@@ -718,20 +722,21 @@ def main() -> None:
718722
else None
719723
)
720724
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)
728-
725+
# The (much larger) PTD goes first, so a failed copy leaves the
726+
# existing output PTE untouched instead of pairing a new PTE with
727+
# a stale weights blob.
729728
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)
733729
if args.input_ptd[0].resolve() != args.output_ptd.resolve():
734-
shutil.copyfile(args.input_ptd[0], args.output_ptd)
730+
with args.input_ptd[0].open("rb") as source:
731+
write_atomically(
732+
args.output_ptd,
733+
lambda output: shutil.copyfileobj(source, output),
734+
mode_from=args.input_ptd[0],
735+
)
736+
737+
write_atomically(
738+
args.output_pte, result.pte.write_to_file, mode_from=sources[0].pte_path
739+
)
735740

736741
print("Merged CUDA code provenance:")
737742
print("delegate\tkind\ttarget\tsource PTE")

‎backends/cuda/tests/targets.bzl‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,3 +599,23 @@ def define_common_targets(is_fbcode = False):
599599
"//executorch/exir/backend/test:device_util",
600600
],
601601
)
602+
603+
python_unittest(
604+
name = "test_merge_ptes",
605+
srcs = [
606+
"test_merge_ptes.py",
607+
],
608+
visibility = [
609+
"//executorch/...",
610+
],
611+
deps = [
612+
"//caffe2:torch",
613+
"//executorch/backends/cuda:cuda_backend",
614+
"//executorch/backends/cuda:merge_ptes",
615+
"//executorch/exir:lib",
616+
"//executorch/exir/_serialize:lib",
617+
"//executorch/exir:schema",
618+
"//executorch/exir/backend:compile_spec_schema",
619+
"//executorch/extension/flat_tensor/serialize:serialize",
620+
],
621+
)

‎backends/cuda/tests/test_merge_ptes.py‎

Lines changed: 69 additions & 4 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,9 @@ 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+
assert first.ptd_path is not None
607+
first.pte_path.chmod(0o640)
608+
first.ptd_path.chmod(0o604)
606609
output_pte = output / "model.pte"
607610
output_ptd = output / "aoti_cuda_blob.ptd"
608611

@@ -631,7 +634,12 @@ def test_cli_writes_merged_pte_and_reuses_base_ptd(self) -> None:
631634
merge_main()
632635

633636
self.assertTrue(output_pte.is_file())
634-
assert first.ptd_path is not None
637+
self.assertEqual(output_pte.stat().st_mode & 0o777, 0o640)
638+
self.assertEqual(output_ptd.stat().st_mode & 0o777, 0o604)
639+
self.assertEqual(
640+
{entry.name for entry in output.iterdir()},
641+
{"model.pte", "aoti_cuda_blob.ptd"},
642+
)
635643
self.assertEqual(output_ptd.read_bytes(), first.ptd_path.read_bytes())
636644
report = stdout.getvalue()
637645
self.assertIn(f"cubin\tsm80\t{first.pte_path}", report)
@@ -642,6 +650,63 @@ def test_cli_writes_merged_pte_and_reuses_base_ptd(self) -> None:
642650
report,
643651
)
644652

653+
def test_cli_output_ptd_without_input_ptd_writes_nothing(self) -> None:
654+
with tempfile.TemporaryDirectory() as temporary:
655+
root = Path(temporary)
656+
source = root / "sm80"
657+
source.mkdir()
658+
first = self._write_artifact(source, 80, b"sm80-so", b"weight")
659+
output_pte = root / "output" / "model.pte"
660+
output_ptd = root / "output" / "aoti_cuda_blob.ptd"
661+
with patch(
662+
"sys.argv",
663+
[
664+
"merge_ptes",
665+
"--input-pte",
666+
str(first.pte_path),
667+
"--output-pte",
668+
str(output_pte),
669+
"--output-ptd",
670+
str(output_ptd),
671+
],
672+
), self.assertRaisesRegex(ValueError, "requires --input-ptd"):
673+
merge_main()
674+
self.assertFalse(output_pte.exists())
675+
self.assertFalse(output_ptd.exists())
676+
677+
def test_cli_failed_ptd_copy_leaves_no_output(self) -> None:
678+
with tempfile.TemporaryDirectory() as temporary:
679+
root = Path(temporary)
680+
sm80 = root / "sm80"
681+
sm120 = root / "sm120"
682+
output = root / "output"
683+
sm80.mkdir()
684+
sm120.mkdir()
685+
inputs = [
686+
self._write_artifact(sm80, 80, b"sm80-so", b"weight", ptx_compute=0),
687+
self._write_artifact(sm120, 120, b"sm120-so", b"weight", ptx_compute=0),
688+
]
689+
output_pte = output / "model.pte"
690+
output_ptd = output / "aoti_cuda_blob.ptd"
691+
argv = ["merge_ptes"]
692+
for artifact in inputs:
693+
argv += ["--input-pte", str(artifact.pte_path)]
694+
for artifact in inputs:
695+
assert artifact.ptd_path is not None
696+
argv += ["--input-ptd", str(artifact.ptd_path)]
697+
argv += [
698+
"--output-pte",
699+
str(output_pte),
700+
"--output-ptd",
701+
str(output_ptd),
702+
]
703+
with patch("sys.argv", argv), patch(
704+
"shutil.copyfileobj", side_effect=OSError("copy failed")
705+
), self.assertRaisesRegex(OSError, "copy failed"):
706+
merge_main()
707+
self.assertFalse(output_pte.exists())
708+
self.assertEqual(list(output.iterdir()), [])
709+
645710

646711
if __name__ == "__main__":
647712
unittest.main()

‎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)