Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/apple/coreml/scripts/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ load("@fbcode_macros//build_defs:build_file_migration.bzl", "fbcode_target", "no
# targets.bzl. This file can contain fbcode-only targets.
load("@fbcode_macros//build_defs:python_binary.bzl", "python_binary")

fbcode_target(_kind = python_binary,
name = "extract_coreml_models",
srcs = [
"extract_coreml_models.py",
],
main_function = "executorch.examples.apple.coreml.scripts.extract_coreml_models.main",
deps = [
"//executorch/backends/apple/coreml:executorchcoreml",
"//executorch/exir:schema",
"//executorch/exir/_serialize:lib",
],
)

fbcode_target(_kind = python_binary,
name = "export",
srcs = [
Expand Down
11 changes: 8 additions & 3 deletions examples/apple/coreml/scripts/extract_coreml_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
from typing import Dict, List, Optional

from executorch.backends.apple.coreml import executorchcoreml
from executorch.backends.apple.coreml.compiler import CoreMLBackend
from executorch.exir._serialize._program import deserialize_pte_binary
from executorch.exir.schema import (
BackendDelegate,
BackendDelegateDataReference,
DataLocation,
)

COREML_BACKEND_ID = "CoreMLBackend"


def extract_coreml_models(pte_data: bytes):
pte_file = deserialize_pte_binary(pte_data)
Expand All @@ -39,7 +40,7 @@ def extract_coreml_models(pte_data: bytes):
[execution_plan.delegates for execution_plan in program.execution_plan], []
)
coreml_delegates: List[BackendDelegate] = [
delegate for delegate in delegates if delegate.id == CoreMLBackend.__name__
delegate for delegate in delegates if delegate.id == COREML_BACKEND_ID
]

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


if __name__ == "__main__":
def main() -> None:
"""
Extracts the Core ML models embedded in the ``.pte`` file and saves them to the
file system.
Expand All @@ -127,3 +128,7 @@ def extract_coreml_models(pte_data: bytes):
with open(model_path, mode="rb") as pte_file:
pte_data = pte_file.read()
extract_coreml_models(pte_data)


if __name__ == "__main__":
main()
Loading