Skip to content

Commit 2f496bb

Browse files
authored
Make extract_coreml_models.py buck-runnable (pytorch#18371)
Differential Revision: D97498856 Pull Request resolved: pytorch#18371
1 parent 6d04ed3 commit 2f496bb

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

examples/apple/coreml/scripts/BUCK

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ load("@fbcode_macros//build_defs:build_file_migration.bzl", "fbcode_target", "no
33
# targets.bzl. This file can contain fbcode-only targets.
44
load("@fbcode_macros//build_defs:python_binary.bzl", "python_binary")
55

6+
fbcode_target(_kind = python_binary,
7+
name = "extract_coreml_models",
8+
srcs = [
9+
"extract_coreml_models.py",
10+
],
11+
main_function = "executorch.examples.apple.coreml.scripts.extract_coreml_models.main",
12+
deps = [
13+
"//executorch/backends/apple/coreml:executorchcoreml",
14+
"//executorch/exir:schema",
15+
"//executorch/exir/_serialize:lib",
16+
],
17+
)
18+
619
fbcode_target(_kind = python_binary,
720
name = "export",
821
srcs = [

examples/apple/coreml/scripts/extract_coreml_models.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
from typing import Dict, List, Optional
1313

1414
from executorch.backends.apple.coreml import executorchcoreml
15-
from executorch.backends.apple.coreml.compiler import CoreMLBackend
1615
from executorch.exir._serialize._program import deserialize_pte_binary
1716
from executorch.exir.schema import (
1817
BackendDelegate,
1918
BackendDelegateDataReference,
2019
DataLocation,
2120
)
2221

22+
COREML_BACKEND_ID = "CoreMLBackend"
23+
2324

2425
def extract_coreml_models(pte_data: bytes):
2526
pte_file = deserialize_pte_binary(pte_data)
@@ -39,7 +40,7 @@ def extract_coreml_models(pte_data: bytes):
3940
[execution_plan.delegates for execution_plan in program.execution_plan], []
4041
)
4142
coreml_delegates: List[BackendDelegate] = [
42-
delegate for delegate in delegates if delegate.id == CoreMLBackend.__name__
43+
delegate for delegate in delegates if delegate.id == COREML_BACKEND_ID
4344
]
4445

4546
# Track extracted models to avoid duplicates (multifunction models share partitions)
@@ -109,7 +110,7 @@ def extract_coreml_models(pte_data: bytes):
109110
print("The model isn't delegated to Core ML.")
110111

111112

112-
if __name__ == "__main__":
113+
def main() -> None:
113114
"""
114115
Extracts the Core ML models embedded in the ``.pte`` file and saves them to the
115116
file system.
@@ -127,3 +128,7 @@ def extract_coreml_models(pte_data: bytes):
127128
with open(model_path, mode="rb") as pte_file:
128129
pte_data = pte_file.read()
129130
extract_coreml_models(pte_data)
131+
132+
133+
if __name__ == "__main__":
134+
main()

0 commit comments

Comments
 (0)