From 622470bfeee8533076502aea8c2faf9c187fa005 Mon Sep 17 00:00:00 2001 From: Scott Roy <161522778+metascroy@users.noreply.github.com> Date: Wed, 23 Jul 2025 22:06:44 -0700 Subject: [PATCH 1/8] Update CoreML model testing in CI --- .ci/scripts/test_model.sh | 18 ++-- .github/workflows/trunk.yml | 14 ++- examples/apple/coreml/scripts/export.py | 134 +++++++++++++++++------- 3 files changed, 118 insertions(+), 48 deletions(-) diff --git a/.ci/scripts/test_model.sh b/.ci/scripts/test_model.sh index bc9bbb8bae0..7af74a91c34 100755 --- a/.ci/scripts/test_model.sh +++ b/.ci/scripts/test_model.sh @@ -232,21 +232,23 @@ test_model_with_qnn() { # @param should_test If true, build and test the model using the coreml_executor_runner. test_model_with_coreml() { local should_test="$1" + local test_with_pybindings="$2" if [[ "${BUILD_TOOL}" != "cmake" ]]; then echo "coreml only supports cmake." exit 1 fi - DTYPE=float16 + RUN_WITH_PYBINDINGS="" + if [[ "${test_with_pybindings}" == true ]]; then + echo \"Running with pybindings\" + export RUN_WITH_PYBINDINGS="--run_with_pybindings" + fi - "${PYTHON_EXECUTABLE}" -m examples.apple.coreml.scripts.export --model_name="${MODEL_NAME}" --compute_precision "${DTYPE}" --use_partitioner + "${PYTHON_EXECUTABLE}" -m examples.apple.coreml.scripts.export --model_name="${MODEL_NAME}" --compute_precision float16 --use_partitioner ${RUN_WITH_PYBINDINGS} EXPORTED_MODEL=$(find "." -type f -name "${MODEL_NAME}*.pte" -print -quit) if [ -n "$EXPORTED_MODEL" ]; then - EXPORTED_MODEL_WITH_DTYPE="${EXPORTED_MODEL%.pte}_${DTYPE}.pte" - mv "$EXPORTED_MODEL" "$EXPORTED_MODEL_WITH_DTYPE" - EXPORTED_MODEL="$EXPORTED_MODEL_WITH_DTYPE" echo "OK exported model: $EXPORTED_MODEL" else echo "[error] failed to export model: no .pte file found" @@ -303,7 +305,11 @@ elif [[ "${BACKEND}" == *"coreml"* ]]; then if [[ "${BACKEND}" == *"test"* ]]; then should_test_coreml=true fi - test_model_with_coreml "${should_test_coreml}" + test_with_pybindings=false + if [[ "${BACKEND}" == *"pybind"* ]]; then + test_with_pybindings=true + fi + test_model_with_coreml "${should_test_coreml}" "${test_with_pybindings}" if [[ $? -eq 0 ]]; then prepare_artifacts_upload fi diff --git a/.github/workflows/trunk.yml b/.github/workflows/trunk.yml index d7205514a68..105d313faf6 100644 --- a/.github/workflows/trunk.yml +++ b/.github/workflows/trunk.yml @@ -590,12 +590,20 @@ jobs: echo "Finishing installing coreml." # Build and test coreml model - MODELS=(mv3 ic4 resnet50 edsr mobilebert w2l) - for MODEL_NAME in "${MODELS[@]}"; do + for MODEL_NAME in dl3 edsr emformer_join ic3 ic4 mobilebert mv2 mv3 resnet50 vit w2l; do echo "::group::Exporting coreml model: $MODEL_NAME" - PYTHON_EXECUTABLE=python ${CONDA_RUN} bash .ci/scripts/test_model.sh "${MODEL_NAME}" "${BUILD_TOOL}" "coreml" + if [[ "${MODEL_NAME}" == "mobilebert" ]]; then + # mobilebert has nan output on FP16 + BACKEND="coreml" + else + BACKEND="coreml-pybind" + fi + + PYTHON_EXECUTABLE=python ${CONDA_RUN} bash .ci/scripts/test_model.sh "${MODEL_NAME}" "${BUILD_TOOL}" "${BACKEND}" echo "::endgroup::" + # Build and test mps model + for MODEL_NAME in mv3 ic4 resnet50 edsr mobilebert w2l; do echo "::group::Exporting mps model: $MODEL_NAME" PYTHON_EXECUTABLE=python ${CONDA_RUN} bash .ci/scripts/test_model.sh "${MODEL_NAME}" "${BUILD_TOOL}" "mps" echo "::endgroup::" diff --git a/examples/apple/coreml/scripts/export.py b/examples/apple/coreml/scripts/export.py index b9acc3b8fb9..3e94ab480c2 100644 --- a/examples/apple/coreml/scripts/export.py +++ b/examples/apple/coreml/scripts/export.py @@ -3,6 +3,7 @@ # Please refer to the license found in the LICENSE file in the root directory of the source tree. import argparse +import collections import copy import pathlib @@ -23,8 +24,7 @@ from executorch.exir import to_edge from executorch.exir.backend.backend_api import to_backend - -from torch.export import export +from executorch.extension.export_util.utils import save_pte_program REPO_ROOT = pathlib.Path(__file__).resolve().parent.parent.parent.parent.parent EXAMPLES_DIR = REPO_ROOT / "examples" @@ -41,7 +41,16 @@ ) -def parse_args() -> argparse.ArgumentParser: +def is_fbcode(): + return not hasattr(torch.version, "git_version") + + +_CAN_RUN_WITH_PYBINDINGS = (sys.platform == "darwin") and not is_fbcode() +if _CAN_RUN_WITH_PYBINDINGS: + from executorch.runtime import Runtime + + +def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser() parser.add_argument( @@ -82,9 +91,12 @@ def parse_args() -> argparse.ArgumentParser: required=False, default=False, ) + parser.add_argument( + "--run_with_pybindings", + action=argparse.BooleanOptionalAction, + ) args = parser.parse_args() - # pyre-fixme[7]: Expected `ArgumentParser` but got `Namespace`. return args @@ -95,7 +107,8 @@ def partition_module_to_coreml(module): def lower_module_to_coreml(module, compile_specs, example_inputs): module = module.eval() edge = to_edge( - export(module, example_inputs, strict=True), compile_config=_EDGE_COMPILE_CONFIG + torch.export.export(module, example_inputs, strict=True), + compile_config=_EDGE_COMPILE_CONFIG, ) # All of the subsequent calls on the edge_dialect_graph generated above (such as delegation or # to_executorch()) are done in place and the graph is also modified in place. For debugging purposes @@ -115,20 +128,18 @@ def lower_module_to_coreml(module, compile_specs, example_inputs): def export_lowered_module_to_executorch_program(lowered_module, example_inputs): lowered_module(*example_inputs) exec_prog = to_edge( - export(lowered_module, example_inputs, strict=True), + torch.export.export(lowered_module, example_inputs, strict=True), compile_config=_EDGE_COMPILE_CONFIG, ).to_executorch(config=exir.ExecutorchBackendConfig(extract_delegate_segments=True)) return exec_prog -def save_executorch_program(exec_prog, model_name, compute_unit): - buffer = exec_prog.buffer - filename = f"{model_name}_coreml_{compute_unit}.pte" - print(f"Saving exported program to {filename}") - with open(filename, "wb") as file: - file.write(buffer) - return +def get_pte_name(args: argparse.Namespace) -> str: + pte_name = f"{args.model_name}_coreml_{args.compute_precision}_{args.compute_unit}" + if args.compile: + pte_name += "_compiled" + return pte_name def save_processed_bytes(processed_bytes, model_name, compute_unit): @@ -154,6 +165,37 @@ def generate_compile_specs_from_args(args): ) +def run_with_pybindings(executorch_program, eager_reference, example_inputs, precision): + if not _CAN_RUN_WITH_PYBINDINGS: + raise RuntimeError("Cannot run with pybindings on this platform.") + + dtype = { + "float32": torch.float32, + "float16": torch.float16, + }[precision] + + runtime = Runtime.get() + program = runtime.load_program(executorch_program.buffer) + method = program.load_method("forward") + et_outputs = method.execute(*example_inputs)[0] + eager_outputs = eager_reference(*example_inputs) + if isinstance(eager_outputs, collections.OrderedDict): + eager_outputs = eager_outputs["out"] + if isinstance(eager_outputs, list | tuple): + eager_outputs = eager_outputs[0] + + mse = ((et_outputs - eager_outputs) ** 2).mean().sqrt() + print(f"Mean square error: {mse}") + assert mse < 0.1, "Mean square error is too high." + + if dtype == torch.float32: + assert torch.allclose( + et_outputs, eager_outputs, atol=1e-02, rtol=1e-02 + ), f"""Outputs do not match eager reference: + \tet_outputs (first 5)={et_outputs.reshape(-1)[0:5]} + \teager_outputs (first 5)={eager_outputs.reshape(-1)[0:5]}""" + + def main(): args = parse_args() @@ -170,49 +212,63 @@ def main(): f"Valid compute units are {valid_compute_units}." ) - model, example_inputs, _, dynamic_shapes = EagerModelFactory.create_model( - *MODEL_NAME_TO_MODEL[args.model_name] + model, example_args, example_kwargs, dynamic_shapes = ( + EagerModelFactory.create_model(*MODEL_NAME_TO_MODEL[args.model_name]) ) if not args.dynamic_shapes: dynamic_shapes = None compile_specs = generate_compile_specs_from_args(args) - lowered_module = None - + pte_name = get_pte_name(args) if args.use_partitioner: model.eval() - exir_program_aten = torch.export.export( - model, example_inputs, dynamic_shapes=dynamic_shapes, strict=True - ) - - edge_program_manager = exir.to_edge(exir_program_aten) - edge_copy = copy.deepcopy(edge_program_manager) - partitioner = CoreMLPartitioner( - skip_ops_for_coreml_delegation=None, compile_specs=compile_specs + assert not args.generate_etrecord, "ETRecord is not supported with partitioner" + ep = torch.export.export( + model, + args=example_args, + kwargs=example_kwargs, + dynamic_shapes=dynamic_shapes, ) - delegated_program_manager = edge_program_manager.to_backend(partitioner) - exec_program = delegated_program_manager.to_executorch( - config=exir.ExecutorchBackendConfig(extract_delegate_segments=True) + delegated_program = exir.to_edge_transform_and_lower( + ep, + partitioner=[CoreMLPartitioner(compile_specs=compile_specs)], ) + exec_program = delegated_program.to_executorch() + save_pte_program(exec_program, pte_name) + if args.run_with_pybindings: + run_with_pybindings( + executorch_program=exec_program, + eager_reference=model, + example_inputs=example_args, + precision=args.compute_precision, + ) else: lowered_module, edge_copy = lower_module_to_coreml( module=model, - example_inputs=example_inputs, + example_inputs=example_args, compile_specs=compile_specs, ) exec_program = export_lowered_module_to_executorch_program( lowered_module, - example_inputs, - ) - - model_name = f"{args.model_name}_compiled" if args.compile else args.model_name - save_executorch_program(exec_program, model_name, args.compute_unit) - generate_etrecord(f"{args.model_name}_coreml_etrecord.bin", edge_copy, exec_program) - - if args.save_processed_bytes and lowered_module is not None: - save_processed_bytes( - lowered_module.processed_bytes, args.model_name, args.compute_unit + example_args, ) + save_pte_program(exec_program, pte_name) + if args.generate_etrecord: + generate_etrecord( + f"{args.model_name}_coreml_etrecord.bin", edge_copy, exec_program + ) + + if args.save_processed_bytes: + save_processed_bytes( + lowered_module.processed_bytes, args.model_name, args.compute_unit + ) + if args.run_with_pybindings: + run_with_pybindings( + executorch_program=exec_program, + eager_reference=model, + example_inputs=example_args, + precision=args.compute_precision, + ) if __name__ == "__main__": From eb6c3a047b175724a529c8d86f38b412a9313ab9 Mon Sep 17 00:00:00 2001 From: Scott Roy <161522778+metascroy@users.noreply.github.com> Date: Thu, 24 Jul 2025 11:12:03 -0700 Subject: [PATCH 2/8] up --- .../project.pbxproj | 76 +++++++++---------- backends/apple/coreml/scripts/build_all.sh | 2 +- .../coreml/scripts/generate_test_models.sh | 10 +-- examples/apple/coreml/scripts/export.py | 19 +++-- 4 files changed, 53 insertions(+), 54 deletions(-) diff --git a/backends/apple/coreml/runtime/workspace/executorchcoreml.xcodeproj/project.pbxproj b/backends/apple/coreml/runtime/workspace/executorchcoreml.xcodeproj/project.pbxproj index 1f70876ab7f..d2be3e65187 100644 --- a/backends/apple/coreml/runtime/workspace/executorchcoreml.xcodeproj/project.pbxproj +++ b/backends/apple/coreml/runtime/workspace/executorchcoreml.xcodeproj/project.pbxproj @@ -8,10 +8,10 @@ /* Begin PBXBuildFile section */ 8307EB8A2C9262060011AE6D /* state_coreml_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = 8307EB892C9262060011AE6D /* state_coreml_all.pte */; }; - 838CA6872CD1965700462190 /* add_mul_compiled_coreml_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = 838CA6862CD1965700462190 /* add_mul_compiled_coreml_all.pte */; }; + 838CA6872CD1965700462190 /* add_mul_coreml_float16_all_compiled.pte in Resources */ = {isa = PBXBuildFile; fileRef = 838CA6862CD1965700462190 /* add_mul_coreml_float16_all_compiled.pte */; }; 83BB78A02C65DA7300274ED7 /* ETCoreMLModelDebugInfo.mm in Sources */ = {isa = PBXBuildFile; fileRef = 83BB789F2C65DA7300274ED7 /* ETCoreMLModelDebugInfo.mm */; }; - 83BB78BF2C66AAAE00274ED7 /* add_mul_coreml_all.bin in Resources */ = {isa = PBXBuildFile; fileRef = 83BB78BD2C66AAAE00274ED7 /* add_mul_coreml_all.bin */; }; - 83BB78C02C66AAAE00274ED7 /* add_mul_coreml_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = 83BB78BE2C66AAAE00274ED7 /* add_mul_coreml_all.pte */; }; + 83BB78BF2C66AAAE00274ED7 /* add_mul_coreml_float16_all.bin in Resources */ = {isa = PBXBuildFile; fileRef = 83BB78BD2C66AAAE00274ED7 /* add_mul_coreml_float16_all.bin */; }; + 83BB78C02C66AAAE00274ED7 /* add_mul_coreml_float16_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = 83BB78BE2C66AAAE00274ED7 /* add_mul_coreml_float16_all.pte */; }; C945E8E02B997ECE009C3FAC /* ETCoreMLModelProfiler.mm in Sources */ = {isa = PBXBuildFile; fileRef = C945E8CF2B997ECD009C3FAC /* ETCoreMLModelProfiler.mm */; }; C945E8E12B997ECE009C3FAC /* ETCoreMLModelAnalyzer.mm in Sources */ = {isa = PBXBuildFile; fileRef = C945E8D42B997ECD009C3FAC /* ETCoreMLModelAnalyzer.mm */; }; C945E8E22B997ECE009C3FAC /* program_path.mm in Sources */ = {isa = PBXBuildFile; fileRef = C945E8D52B997ECD009C3FAC /* program_path.mm */; }; @@ -85,12 +85,12 @@ C97716D52AF41B2E00FC0DAC /* json_key_value_store.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C97716D42AF41B2E00FC0DAC /* json_key_value_store.cpp */; }; C97716DA2AF44CFA00FC0DAC /* json_util.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C97716D82AF44CFA00FC0DAC /* json_util.cpp */; }; C97716DD2AF44E7B00FC0DAC /* objc_json_serde.mm in Sources */ = {isa = PBXBuildFile; fileRef = C97716DC2AF44E7B00FC0DAC /* objc_json_serde.mm */; }; - C985519E2AD2542D009143F9 /* mv3_coreml_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = C98551982AD2542D009143F9 /* mv3_coreml_all.pte */; }; - C985519F2AD2542D009143F9 /* mul_coreml_all.bin in Resources */ = {isa = PBXBuildFile; fileRef = C98551992AD2542D009143F9 /* mul_coreml_all.bin */; }; - C98551A02AD2542D009143F9 /* add_coreml_all.bin in Resources */ = {isa = PBXBuildFile; fileRef = C985519A2AD2542D009143F9 /* add_coreml_all.bin */; }; - C98551A12AD2542D009143F9 /* mv3_coreml_all.bin in Resources */ = {isa = PBXBuildFile; fileRef = C985519B2AD2542D009143F9 /* mv3_coreml_all.bin */; }; - C98551A22AD2542D009143F9 /* mul_coreml_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = C985519C2AD2542D009143F9 /* mul_coreml_all.pte */; }; - C98551A32AD2542D009143F9 /* add_coreml_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = C985519D2AD2542D009143F9 /* add_coreml_all.pte */; }; + C985519E2AD2542D009143F9 /* mv3_coreml_float16_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = C98551982AD2542D009143F9 /* mv3_coreml_float16_all.pte */; }; + C985519F2AD2542D009143F9 /* mul_coreml_float16_all.bin in Resources */ = {isa = PBXBuildFile; fileRef = C98551992AD2542D009143F9 /* mul_coreml_float16_all.bin */; }; + C98551A02AD2542D009143F9 /* add_coreml_float16_all.bin in Resources */ = {isa = PBXBuildFile; fileRef = C985519A2AD2542D009143F9 /* add_coreml_float16_all.bin */; }; + C98551A12AD2542D009143F9 /* mv3_coreml_float16_all.bin in Resources */ = {isa = PBXBuildFile; fileRef = C985519B2AD2542D009143F9 /* mv3_coreml_float16_all.bin */; }; + C98551A22AD2542D009143F9 /* mul_coreml_float16_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = C985519C2AD2542D009143F9 /* mul_coreml_float16_all.pte */; }; + C98551A32AD2542D009143F9 /* add_coreml_float16_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = C985519D2AD2542D009143F9 /* add_coreml_float16_all.pte */; }; C99883862B95AD7D000953A3 /* libprotobuf-lite.a in Frameworks */ = {isa = PBXBuildFile; fileRef = C99883852B95AD7D000953A3 /* libprotobuf-lite.a */; }; C99883882B964413000953A3 /* libexecutorch.a in Frameworks */ = {isa = PBXBuildFile; fileRef = C99883872B964413000953A3 /* libexecutorch.a */; }; C998838D2B96841D000953A3 /* ETCoreMLModelStructurePathTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = C998838C2B96841D000953A3 /* ETCoreMLModelStructurePathTests.mm */; }; @@ -123,11 +123,11 @@ /* Begin PBXFileReference section */ 8307EB892C9262060011AE6D /* state_coreml_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = state_coreml_all.pte; path = ../test/models/state_coreml_all.pte; sourceTree = ""; }; - 838CA6862CD1965700462190 /* add_mul_compiled_coreml_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = add_mul_compiled_coreml_all.pte; path = ../test/models/add_mul_compiled_coreml_all.pte; sourceTree = ""; }; + 838CA6862CD1965700462190 /* add_mul_coreml_float16_all_compiled.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = add_mul_coreml_float16_all_compiled.pte; path = ../test/models/add_mul_coreml_float16_all_compiled.pte; sourceTree = ""; }; 83BB789E2C65DA7300274ED7 /* ETCoreMLModelDebugInfo.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = ETCoreMLModelDebugInfo.h; path = ../sdk/ETCoreMLModelDebugInfo.h; sourceTree = ""; }; 83BB789F2C65DA7300274ED7 /* ETCoreMLModelDebugInfo.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = ETCoreMLModelDebugInfo.mm; path = ../sdk/ETCoreMLModelDebugInfo.mm; sourceTree = ""; }; - 83BB78BD2C66AAAE00274ED7 /* add_mul_coreml_all.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = add_mul_coreml_all.bin; path = ../test/models/add_mul_coreml_all.bin; sourceTree = ""; }; - 83BB78BE2C66AAAE00274ED7 /* add_mul_coreml_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = add_mul_coreml_all.pte; path = ../test/models/add_mul_coreml_all.pte; sourceTree = ""; }; + 83BB78BD2C66AAAE00274ED7 /* add_mul_coreml_float16_all.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = add_mul_coreml_float16_all.bin; path = ../test/models/add_mul_coreml_float16_all.bin; sourceTree = ""; }; + 83BB78BE2C66AAAE00274ED7 /* add_mul_coreml_float16_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = add_mul_coreml_float16_all.pte; path = ../test/models/add_mul_coreml_float16_all.pte; sourceTree = ""; }; C945E8CD2B997ECD009C3FAC /* ETCoreMLModelProfiler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ETCoreMLModelProfiler.h; path = ../sdk/ETCoreMLModelProfiler.h; sourceTree = ""; }; C945E8CE2B997ECD009C3FAC /* ETCoreMLModelAnalyzer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ETCoreMLModelAnalyzer.h; path = ../sdk/ETCoreMLModelAnalyzer.h; sourceTree = ""; }; C945E8CF2B997ECD009C3FAC /* ETCoreMLModelProfiler.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = ETCoreMLModelProfiler.mm; path = ../sdk/ETCoreMLModelProfiler.mm; sourceTree = ""; }; @@ -259,12 +259,12 @@ C97716DB2AF44D9A00FC0DAC /* objc_json_serde.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = objc_json_serde.h; path = ../util/objc_json_serde.h; sourceTree = ""; }; C97716DC2AF44E7B00FC0DAC /* objc_json_serde.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = objc_json_serde.mm; path = ../util/objc_json_serde.mm; sourceTree = ""; }; C97716DE2AF44FC400FC0DAC /* objc_safe_cast.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = objc_safe_cast.h; path = ../util/objc_safe_cast.h; sourceTree = ""; }; - C98551982AD2542D009143F9 /* mv3_coreml_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = mv3_coreml_all.pte; path = ../test/models/mv3_coreml_all.pte; sourceTree = ""; }; - C98551992AD2542D009143F9 /* mul_coreml_all.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = mul_coreml_all.bin; path = ../test/models/mul_coreml_all.bin; sourceTree = ""; }; - C985519A2AD2542D009143F9 /* add_coreml_all.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = add_coreml_all.bin; path = ../test/models/add_coreml_all.bin; sourceTree = ""; }; - C985519B2AD2542D009143F9 /* mv3_coreml_all.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = mv3_coreml_all.bin; path = ../test/models/mv3_coreml_all.bin; sourceTree = ""; }; - C985519C2AD2542D009143F9 /* mul_coreml_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = mul_coreml_all.pte; path = ../test/models/mul_coreml_all.pte; sourceTree = ""; }; - C985519D2AD2542D009143F9 /* add_coreml_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = add_coreml_all.pte; path = ../test/models/add_coreml_all.pte; sourceTree = ""; }; + C98551982AD2542D009143F9 /* mv3_coreml_float16_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = mv3_coreml_float16_all.pte; path = ../test/models/mv3_coreml_float16_all.pte; sourceTree = ""; }; + C98551992AD2542D009143F9 /* mul_coreml_float16_all.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = mul_coreml_float16_all.bin; path = ../test/models/mul_coreml_float16_all.bin; sourceTree = ""; }; + C985519A2AD2542D009143F9 /* add_coreml_float16_all.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = add_coreml_float16_all.bin; path = ../test/models/add_coreml_float16_all.bin; sourceTree = ""; }; + C985519B2AD2542D009143F9 /* mv3_coreml_float16_all.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = mv3_coreml_float16_all.bin; path = ../test/models/mv3_coreml_float16_all.bin; sourceTree = ""; }; + C985519C2AD2542D009143F9 /* mul_coreml_float16_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = mul_coreml_float16_all.pte; path = ../test/models/mul_coreml_float16_all.pte; sourceTree = ""; }; + C985519D2AD2542D009143F9 /* add_coreml_float16_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = add_coreml_float16_all.pte; path = ../test/models/add_coreml_float16_all.pte; sourceTree = ""; }; C988D69E2B998D8400979CF6 /* model_event_logger_impl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = model_event_logger_impl.h; path = ../sdk/model_event_logger_impl.h; sourceTree = ""; }; C988D6FB2B9BA94000979CF6 /* range.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; name = range.hpp; path = ../inmemoryfs/range.hpp; sourceTree = ""; }; C99883202B92D220000953A3 /* ETCoreMLComputeUnits.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = ETCoreMLComputeUnits.h; path = ../delegate/ETCoreMLComputeUnits.h; sourceTree = ""; }; @@ -603,15 +603,15 @@ C9E7D7AE2AB6C0C000CCAE5D /* models */ = { isa = PBXGroup; children = ( - C985519A2AD2542D009143F9 /* add_coreml_all.bin */, - C985519D2AD2542D009143F9 /* add_coreml_all.pte */, - C98551992AD2542D009143F9 /* mul_coreml_all.bin */, - C985519C2AD2542D009143F9 /* mul_coreml_all.pte */, - C985519B2AD2542D009143F9 /* mv3_coreml_all.bin */, - 838CA6862CD1965700462190 /* add_mul_compiled_coreml_all.pte */, - C98551982AD2542D009143F9 /* mv3_coreml_all.pte */, - 83BB78BD2C66AAAE00274ED7 /* add_mul_coreml_all.bin */, - 83BB78BE2C66AAAE00274ED7 /* add_mul_coreml_all.pte */, + C985519A2AD2542D009143F9 /* add_coreml_float16_all.bin */, + C985519D2AD2542D009143F9 /* add_coreml_float16_all.pte */, + C98551992AD2542D009143F9 /* mul_coreml_float16_all.bin */, + C985519C2AD2542D009143F9 /* mul_coreml_float16_all.pte */, + C985519B2AD2542D009143F9 /* mv3_coreml_float16_all.bin */, + 838CA6862CD1965700462190 /* add_mul_coreml_float16_all_compiled.pte */, + C98551982AD2542D009143F9 /* mv3_coreml_float16_all.pte */, + 83BB78BD2C66AAAE00274ED7 /* add_mul_coreml_float16_all.bin */, + 83BB78BE2C66AAAE00274ED7 /* add_mul_coreml_float16_all.pte */, 8307EB892C9262060011AE6D /* state_coreml_all.pte */, ); name = models; @@ -676,16 +676,16 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( - C98551A12AD2542D009143F9 /* mv3_coreml_all.bin in Resources */, - C985519F2AD2542D009143F9 /* mul_coreml_all.bin in Resources */, - 83BB78BF2C66AAAE00274ED7 /* add_mul_coreml_all.bin in Resources */, - 83BB78C02C66AAAE00274ED7 /* add_mul_coreml_all.pte in Resources */, - C985519E2AD2542D009143F9 /* mv3_coreml_all.pte in Resources */, - C98551A02AD2542D009143F9 /* add_coreml_all.bin in Resources */, - C98551A22AD2542D009143F9 /* mul_coreml_all.pte in Resources */, - 838CA6872CD1965700462190 /* add_mul_compiled_coreml_all.pte in Resources */, + C98551A12AD2542D009143F9 /* mv3_coreml_float16_all.bin in Resources */, + C985519F2AD2542D009143F9 /* mul_coreml_float16_all.bin in Resources */, + 83BB78BF2C66AAAE00274ED7 /* add_mul_coreml_float16_all.bin in Resources */, + 83BB78C02C66AAAE00274ED7 /* add_mul_coreml_float16_all.pte in Resources */, + C985519E2AD2542D009143F9 /* mv3_coreml_float16_all.pte in Resources */, + C98551A02AD2542D009143F9 /* add_coreml_float16_all.bin in Resources */, + C98551A22AD2542D009143F9 /* mul_coreml_float16_all.pte in Resources */, + 838CA6872CD1965700462190 /* add_mul_coreml_float16_all_compiled.pte in Resources */, 8307EB8A2C9262060011AE6D /* state_coreml_all.pte in Resources */, - C98551A32AD2542D009143F9 /* add_coreml_all.pte in Resources */, + C98551A32AD2542D009143F9 /* add_coreml_float16_all.pte in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -830,7 +830,7 @@ GCC_OPTIMIZATION_LEVEL = 0; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", - "C10_USING_CUSTOM_GENERATED_MACROS", + C10_USING_CUSTOM_GENERATED_MACROS, "$(inherited)", ); GCC_WARN_64_TO_32_BIT_CONVERSION = YES; @@ -912,7 +912,7 @@ DEVELOPMENT_TEAM = ""; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", - "C10_USING_CUSTOM_GENERATED_MACROS", + C10_USING_CUSTOM_GENERATED_MACROS, "ET_EVENT_TRACER_ENABLED=1", "$(inherited)", ); diff --git a/backends/apple/coreml/scripts/build_all.sh b/backends/apple/coreml/scripts/build_all.sh index 8c6595d562e..d21e329f6c6 100755 --- a/backends/apple/coreml/scripts/build_all.sh +++ b/backends/apple/coreml/scripts/build_all.sh @@ -15,7 +15,7 @@ SCRIPT_DIR_PATH="$( EXECUTORCH_ROOT_PATH=$(realpath "$SCRIPT_DIR_PATH/../../../../") COREML_DIR_PATH="$EXECUTORCH_ROOT_PATH/backends/apple/coreml" COREML_EXAMPLES_DIR_PATH="$EXECUTORCH_ROOT_PATH/examples/apple/coreml" -TEST_MODEL_PATH="$COREML_DIR_PATH/runtime/test/models/mv3_coreml_all.pte" +TEST_MODEL_PATH="$COREML_DIR_PATH/runtime/test/models/mv3_coreml_float16_all.pte" red=`tput setaf 1` green=`tput setaf 2` diff --git a/backends/apple/coreml/scripts/generate_test_models.sh b/backends/apple/coreml/scripts/generate_test_models.sh index 001ba362393..9feb21cde55 100755 --- a/backends/apple/coreml/scripts/generate_test_models.sh +++ b/backends/apple/coreml/scripts/generate_test_models.sh @@ -22,11 +22,11 @@ cd "$EXECUTORCH_ROOT_PATH" MODELS=("add" "add_mul" "mul" "mv3") for MODEL in "${MODELS[@]}" do - echo "Executorch: Generating $MODEL model" + echo "Executorch: Generating $MODEL model" # TODO: Don't use the script in examples directory. python3 -m examples.apple.coreml.scripts.export --model_name "$MODEL" --save_processed_bytes - mv -f "$MODEL""_coreml_all.pte" "$COREML_DIR_PATH/runtime/test/models" - mv -f "$MODEL""_coreml_all.bin" "$COREML_DIR_PATH/runtime/test/models" + mv -f "$MODEL""_coreml_float16_all.pte" "$COREML_DIR_PATH/runtime/test/models" + mv -f "$MODEL""_coreml_float16_all.bin" "$COREML_DIR_PATH/runtime/test/models" done echo "Executorch: Generating stateful model" @@ -36,7 +36,7 @@ COMPILE_MODELS=("add_mul") echo "Executorch: Generating compiled model" for MODEL in "${COMPILE_MODELS[@]}" do - echo "Executorch: Generating compiled $MODEL model" + echo "Executorch: Generating compiled $MODEL model" python3 -m examples.apple.coreml.scripts.export --model_name "$MODEL" --compile - mv -f "$MODEL""_compiled_coreml_all.pte" "$COREML_DIR_PATH/runtime/test/models" + mv -f "$MODEL""_coreml_float16_all_compiled.pte" "$COREML_DIR_PATH/runtime/test/models" done diff --git a/examples/apple/coreml/scripts/export.py b/examples/apple/coreml/scripts/export.py index 3e94ab480c2..8c1ef50209e 100644 --- a/examples/apple/coreml/scripts/export.py +++ b/examples/apple/coreml/scripts/export.py @@ -135,15 +135,15 @@ def export_lowered_module_to_executorch_program(lowered_module, example_inputs): return exec_prog -def get_pte_name(args: argparse.Namespace) -> str: +def get_pte_base_name(args: argparse.Namespace) -> str: pte_name = f"{args.model_name}_coreml_{args.compute_precision}_{args.compute_unit}" if args.compile: pte_name += "_compiled" return pte_name -def save_processed_bytes(processed_bytes, model_name, compute_unit): - filename = f"{model_name}_coreml_{compute_unit}.bin" +def save_processed_bytes(processed_bytes, base_name: str): + filename = f"{base_name}.bin" print(f"Saving processed bytes to {filename}") with open(filename, "wb") as file: file.write(processed_bytes) @@ -219,7 +219,7 @@ def main(): dynamic_shapes = None compile_specs = generate_compile_specs_from_args(args) - pte_name = get_pte_name(args) + pte_base_name = get_pte_base_name(args) if args.use_partitioner: model.eval() assert not args.generate_etrecord, "ETRecord is not supported with partitioner" @@ -234,7 +234,7 @@ def main(): partitioner=[CoreMLPartitioner(compile_specs=compile_specs)], ) exec_program = delegated_program.to_executorch() - save_pte_program(exec_program, pte_name) + save_pte_program(exec_program, pte_base_name) if args.run_with_pybindings: run_with_pybindings( executorch_program=exec_program, @@ -252,15 +252,14 @@ def main(): lowered_module, example_args, ) - save_pte_program(exec_program, pte_name) + save_pte_program(exec_program, pte_base_name) if args.generate_etrecord: - generate_etrecord( - f"{args.model_name}_coreml_etrecord.bin", edge_copy, exec_program - ) + generate_etrecord(f"{pte_base_name}_etrecord.bin", edge_copy, exec_program) if args.save_processed_bytes: save_processed_bytes( - lowered_module.processed_bytes, args.model_name, args.compute_unit + lowered_module.processed_bytes, + pte_base_name, ) if args.run_with_pybindings: run_with_pybindings( From 2465cb09ae0ae95937ee73ec8c026930c96f465c Mon Sep 17 00:00:00 2001 From: Scott Roy <161522778+metascroy@users.noreply.github.com> Date: Thu, 24 Jul 2025 12:46:00 -0700 Subject: [PATCH 3/8] up --- .../project.pbxproj | 76 +++++++++---------- backends/apple/coreml/scripts/build_all.sh | 2 +- .../coreml/scripts/generate_test_models.sh | 6 +- examples/apple/coreml/scripts/export.py | 7 +- 4 files changed, 47 insertions(+), 44 deletions(-) diff --git a/backends/apple/coreml/runtime/workspace/executorchcoreml.xcodeproj/project.pbxproj b/backends/apple/coreml/runtime/workspace/executorchcoreml.xcodeproj/project.pbxproj index d2be3e65187..1f70876ab7f 100644 --- a/backends/apple/coreml/runtime/workspace/executorchcoreml.xcodeproj/project.pbxproj +++ b/backends/apple/coreml/runtime/workspace/executorchcoreml.xcodeproj/project.pbxproj @@ -8,10 +8,10 @@ /* Begin PBXBuildFile section */ 8307EB8A2C9262060011AE6D /* state_coreml_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = 8307EB892C9262060011AE6D /* state_coreml_all.pte */; }; - 838CA6872CD1965700462190 /* add_mul_coreml_float16_all_compiled.pte in Resources */ = {isa = PBXBuildFile; fileRef = 838CA6862CD1965700462190 /* add_mul_coreml_float16_all_compiled.pte */; }; + 838CA6872CD1965700462190 /* add_mul_compiled_coreml_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = 838CA6862CD1965700462190 /* add_mul_compiled_coreml_all.pte */; }; 83BB78A02C65DA7300274ED7 /* ETCoreMLModelDebugInfo.mm in Sources */ = {isa = PBXBuildFile; fileRef = 83BB789F2C65DA7300274ED7 /* ETCoreMLModelDebugInfo.mm */; }; - 83BB78BF2C66AAAE00274ED7 /* add_mul_coreml_float16_all.bin in Resources */ = {isa = PBXBuildFile; fileRef = 83BB78BD2C66AAAE00274ED7 /* add_mul_coreml_float16_all.bin */; }; - 83BB78C02C66AAAE00274ED7 /* add_mul_coreml_float16_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = 83BB78BE2C66AAAE00274ED7 /* add_mul_coreml_float16_all.pte */; }; + 83BB78BF2C66AAAE00274ED7 /* add_mul_coreml_all.bin in Resources */ = {isa = PBXBuildFile; fileRef = 83BB78BD2C66AAAE00274ED7 /* add_mul_coreml_all.bin */; }; + 83BB78C02C66AAAE00274ED7 /* add_mul_coreml_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = 83BB78BE2C66AAAE00274ED7 /* add_mul_coreml_all.pte */; }; C945E8E02B997ECE009C3FAC /* ETCoreMLModelProfiler.mm in Sources */ = {isa = PBXBuildFile; fileRef = C945E8CF2B997ECD009C3FAC /* ETCoreMLModelProfiler.mm */; }; C945E8E12B997ECE009C3FAC /* ETCoreMLModelAnalyzer.mm in Sources */ = {isa = PBXBuildFile; fileRef = C945E8D42B997ECD009C3FAC /* ETCoreMLModelAnalyzer.mm */; }; C945E8E22B997ECE009C3FAC /* program_path.mm in Sources */ = {isa = PBXBuildFile; fileRef = C945E8D52B997ECD009C3FAC /* program_path.mm */; }; @@ -85,12 +85,12 @@ C97716D52AF41B2E00FC0DAC /* json_key_value_store.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C97716D42AF41B2E00FC0DAC /* json_key_value_store.cpp */; }; C97716DA2AF44CFA00FC0DAC /* json_util.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C97716D82AF44CFA00FC0DAC /* json_util.cpp */; }; C97716DD2AF44E7B00FC0DAC /* objc_json_serde.mm in Sources */ = {isa = PBXBuildFile; fileRef = C97716DC2AF44E7B00FC0DAC /* objc_json_serde.mm */; }; - C985519E2AD2542D009143F9 /* mv3_coreml_float16_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = C98551982AD2542D009143F9 /* mv3_coreml_float16_all.pte */; }; - C985519F2AD2542D009143F9 /* mul_coreml_float16_all.bin in Resources */ = {isa = PBXBuildFile; fileRef = C98551992AD2542D009143F9 /* mul_coreml_float16_all.bin */; }; - C98551A02AD2542D009143F9 /* add_coreml_float16_all.bin in Resources */ = {isa = PBXBuildFile; fileRef = C985519A2AD2542D009143F9 /* add_coreml_float16_all.bin */; }; - C98551A12AD2542D009143F9 /* mv3_coreml_float16_all.bin in Resources */ = {isa = PBXBuildFile; fileRef = C985519B2AD2542D009143F9 /* mv3_coreml_float16_all.bin */; }; - C98551A22AD2542D009143F9 /* mul_coreml_float16_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = C985519C2AD2542D009143F9 /* mul_coreml_float16_all.pte */; }; - C98551A32AD2542D009143F9 /* add_coreml_float16_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = C985519D2AD2542D009143F9 /* add_coreml_float16_all.pte */; }; + C985519E2AD2542D009143F9 /* mv3_coreml_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = C98551982AD2542D009143F9 /* mv3_coreml_all.pte */; }; + C985519F2AD2542D009143F9 /* mul_coreml_all.bin in Resources */ = {isa = PBXBuildFile; fileRef = C98551992AD2542D009143F9 /* mul_coreml_all.bin */; }; + C98551A02AD2542D009143F9 /* add_coreml_all.bin in Resources */ = {isa = PBXBuildFile; fileRef = C985519A2AD2542D009143F9 /* add_coreml_all.bin */; }; + C98551A12AD2542D009143F9 /* mv3_coreml_all.bin in Resources */ = {isa = PBXBuildFile; fileRef = C985519B2AD2542D009143F9 /* mv3_coreml_all.bin */; }; + C98551A22AD2542D009143F9 /* mul_coreml_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = C985519C2AD2542D009143F9 /* mul_coreml_all.pte */; }; + C98551A32AD2542D009143F9 /* add_coreml_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = C985519D2AD2542D009143F9 /* add_coreml_all.pte */; }; C99883862B95AD7D000953A3 /* libprotobuf-lite.a in Frameworks */ = {isa = PBXBuildFile; fileRef = C99883852B95AD7D000953A3 /* libprotobuf-lite.a */; }; C99883882B964413000953A3 /* libexecutorch.a in Frameworks */ = {isa = PBXBuildFile; fileRef = C99883872B964413000953A3 /* libexecutorch.a */; }; C998838D2B96841D000953A3 /* ETCoreMLModelStructurePathTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = C998838C2B96841D000953A3 /* ETCoreMLModelStructurePathTests.mm */; }; @@ -123,11 +123,11 @@ /* Begin PBXFileReference section */ 8307EB892C9262060011AE6D /* state_coreml_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = state_coreml_all.pte; path = ../test/models/state_coreml_all.pte; sourceTree = ""; }; - 838CA6862CD1965700462190 /* add_mul_coreml_float16_all_compiled.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = add_mul_coreml_float16_all_compiled.pte; path = ../test/models/add_mul_coreml_float16_all_compiled.pte; sourceTree = ""; }; + 838CA6862CD1965700462190 /* add_mul_compiled_coreml_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = add_mul_compiled_coreml_all.pte; path = ../test/models/add_mul_compiled_coreml_all.pte; sourceTree = ""; }; 83BB789E2C65DA7300274ED7 /* ETCoreMLModelDebugInfo.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = ETCoreMLModelDebugInfo.h; path = ../sdk/ETCoreMLModelDebugInfo.h; sourceTree = ""; }; 83BB789F2C65DA7300274ED7 /* ETCoreMLModelDebugInfo.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = ETCoreMLModelDebugInfo.mm; path = ../sdk/ETCoreMLModelDebugInfo.mm; sourceTree = ""; }; - 83BB78BD2C66AAAE00274ED7 /* add_mul_coreml_float16_all.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = add_mul_coreml_float16_all.bin; path = ../test/models/add_mul_coreml_float16_all.bin; sourceTree = ""; }; - 83BB78BE2C66AAAE00274ED7 /* add_mul_coreml_float16_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = add_mul_coreml_float16_all.pte; path = ../test/models/add_mul_coreml_float16_all.pte; sourceTree = ""; }; + 83BB78BD2C66AAAE00274ED7 /* add_mul_coreml_all.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = add_mul_coreml_all.bin; path = ../test/models/add_mul_coreml_all.bin; sourceTree = ""; }; + 83BB78BE2C66AAAE00274ED7 /* add_mul_coreml_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = add_mul_coreml_all.pte; path = ../test/models/add_mul_coreml_all.pte; sourceTree = ""; }; C945E8CD2B997ECD009C3FAC /* ETCoreMLModelProfiler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ETCoreMLModelProfiler.h; path = ../sdk/ETCoreMLModelProfiler.h; sourceTree = ""; }; C945E8CE2B997ECD009C3FAC /* ETCoreMLModelAnalyzer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ETCoreMLModelAnalyzer.h; path = ../sdk/ETCoreMLModelAnalyzer.h; sourceTree = ""; }; C945E8CF2B997ECD009C3FAC /* ETCoreMLModelProfiler.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = ETCoreMLModelProfiler.mm; path = ../sdk/ETCoreMLModelProfiler.mm; sourceTree = ""; }; @@ -259,12 +259,12 @@ C97716DB2AF44D9A00FC0DAC /* objc_json_serde.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = objc_json_serde.h; path = ../util/objc_json_serde.h; sourceTree = ""; }; C97716DC2AF44E7B00FC0DAC /* objc_json_serde.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = objc_json_serde.mm; path = ../util/objc_json_serde.mm; sourceTree = ""; }; C97716DE2AF44FC400FC0DAC /* objc_safe_cast.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = objc_safe_cast.h; path = ../util/objc_safe_cast.h; sourceTree = ""; }; - C98551982AD2542D009143F9 /* mv3_coreml_float16_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = mv3_coreml_float16_all.pte; path = ../test/models/mv3_coreml_float16_all.pte; sourceTree = ""; }; - C98551992AD2542D009143F9 /* mul_coreml_float16_all.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = mul_coreml_float16_all.bin; path = ../test/models/mul_coreml_float16_all.bin; sourceTree = ""; }; - C985519A2AD2542D009143F9 /* add_coreml_float16_all.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = add_coreml_float16_all.bin; path = ../test/models/add_coreml_float16_all.bin; sourceTree = ""; }; - C985519B2AD2542D009143F9 /* mv3_coreml_float16_all.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = mv3_coreml_float16_all.bin; path = ../test/models/mv3_coreml_float16_all.bin; sourceTree = ""; }; - C985519C2AD2542D009143F9 /* mul_coreml_float16_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = mul_coreml_float16_all.pte; path = ../test/models/mul_coreml_float16_all.pte; sourceTree = ""; }; - C985519D2AD2542D009143F9 /* add_coreml_float16_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = add_coreml_float16_all.pte; path = ../test/models/add_coreml_float16_all.pte; sourceTree = ""; }; + C98551982AD2542D009143F9 /* mv3_coreml_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = mv3_coreml_all.pte; path = ../test/models/mv3_coreml_all.pte; sourceTree = ""; }; + C98551992AD2542D009143F9 /* mul_coreml_all.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = mul_coreml_all.bin; path = ../test/models/mul_coreml_all.bin; sourceTree = ""; }; + C985519A2AD2542D009143F9 /* add_coreml_all.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = add_coreml_all.bin; path = ../test/models/add_coreml_all.bin; sourceTree = ""; }; + C985519B2AD2542D009143F9 /* mv3_coreml_all.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = mv3_coreml_all.bin; path = ../test/models/mv3_coreml_all.bin; sourceTree = ""; }; + C985519C2AD2542D009143F9 /* mul_coreml_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = mul_coreml_all.pte; path = ../test/models/mul_coreml_all.pte; sourceTree = ""; }; + C985519D2AD2542D009143F9 /* add_coreml_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = add_coreml_all.pte; path = ../test/models/add_coreml_all.pte; sourceTree = ""; }; C988D69E2B998D8400979CF6 /* model_event_logger_impl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = model_event_logger_impl.h; path = ../sdk/model_event_logger_impl.h; sourceTree = ""; }; C988D6FB2B9BA94000979CF6 /* range.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; name = range.hpp; path = ../inmemoryfs/range.hpp; sourceTree = ""; }; C99883202B92D220000953A3 /* ETCoreMLComputeUnits.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = ETCoreMLComputeUnits.h; path = ../delegate/ETCoreMLComputeUnits.h; sourceTree = ""; }; @@ -603,15 +603,15 @@ C9E7D7AE2AB6C0C000CCAE5D /* models */ = { isa = PBXGroup; children = ( - C985519A2AD2542D009143F9 /* add_coreml_float16_all.bin */, - C985519D2AD2542D009143F9 /* add_coreml_float16_all.pte */, - C98551992AD2542D009143F9 /* mul_coreml_float16_all.bin */, - C985519C2AD2542D009143F9 /* mul_coreml_float16_all.pte */, - C985519B2AD2542D009143F9 /* mv3_coreml_float16_all.bin */, - 838CA6862CD1965700462190 /* add_mul_coreml_float16_all_compiled.pte */, - C98551982AD2542D009143F9 /* mv3_coreml_float16_all.pte */, - 83BB78BD2C66AAAE00274ED7 /* add_mul_coreml_float16_all.bin */, - 83BB78BE2C66AAAE00274ED7 /* add_mul_coreml_float16_all.pte */, + C985519A2AD2542D009143F9 /* add_coreml_all.bin */, + C985519D2AD2542D009143F9 /* add_coreml_all.pte */, + C98551992AD2542D009143F9 /* mul_coreml_all.bin */, + C985519C2AD2542D009143F9 /* mul_coreml_all.pte */, + C985519B2AD2542D009143F9 /* mv3_coreml_all.bin */, + 838CA6862CD1965700462190 /* add_mul_compiled_coreml_all.pte */, + C98551982AD2542D009143F9 /* mv3_coreml_all.pte */, + 83BB78BD2C66AAAE00274ED7 /* add_mul_coreml_all.bin */, + 83BB78BE2C66AAAE00274ED7 /* add_mul_coreml_all.pte */, 8307EB892C9262060011AE6D /* state_coreml_all.pte */, ); name = models; @@ -676,16 +676,16 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( - C98551A12AD2542D009143F9 /* mv3_coreml_float16_all.bin in Resources */, - C985519F2AD2542D009143F9 /* mul_coreml_float16_all.bin in Resources */, - 83BB78BF2C66AAAE00274ED7 /* add_mul_coreml_float16_all.bin in Resources */, - 83BB78C02C66AAAE00274ED7 /* add_mul_coreml_float16_all.pte in Resources */, - C985519E2AD2542D009143F9 /* mv3_coreml_float16_all.pte in Resources */, - C98551A02AD2542D009143F9 /* add_coreml_float16_all.bin in Resources */, - C98551A22AD2542D009143F9 /* mul_coreml_float16_all.pte in Resources */, - 838CA6872CD1965700462190 /* add_mul_coreml_float16_all_compiled.pte in Resources */, + C98551A12AD2542D009143F9 /* mv3_coreml_all.bin in Resources */, + C985519F2AD2542D009143F9 /* mul_coreml_all.bin in Resources */, + 83BB78BF2C66AAAE00274ED7 /* add_mul_coreml_all.bin in Resources */, + 83BB78C02C66AAAE00274ED7 /* add_mul_coreml_all.pte in Resources */, + C985519E2AD2542D009143F9 /* mv3_coreml_all.pte in Resources */, + C98551A02AD2542D009143F9 /* add_coreml_all.bin in Resources */, + C98551A22AD2542D009143F9 /* mul_coreml_all.pte in Resources */, + 838CA6872CD1965700462190 /* add_mul_compiled_coreml_all.pte in Resources */, 8307EB8A2C9262060011AE6D /* state_coreml_all.pte in Resources */, - C98551A32AD2542D009143F9 /* add_coreml_float16_all.pte in Resources */, + C98551A32AD2542D009143F9 /* add_coreml_all.pte in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -830,7 +830,7 @@ GCC_OPTIMIZATION_LEVEL = 0; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", - C10_USING_CUSTOM_GENERATED_MACROS, + "C10_USING_CUSTOM_GENERATED_MACROS", "$(inherited)", ); GCC_WARN_64_TO_32_BIT_CONVERSION = YES; @@ -912,7 +912,7 @@ DEVELOPMENT_TEAM = ""; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", - C10_USING_CUSTOM_GENERATED_MACROS, + "C10_USING_CUSTOM_GENERATED_MACROS", "ET_EVENT_TRACER_ENABLED=1", "$(inherited)", ); diff --git a/backends/apple/coreml/scripts/build_all.sh b/backends/apple/coreml/scripts/build_all.sh index d21e329f6c6..8c6595d562e 100755 --- a/backends/apple/coreml/scripts/build_all.sh +++ b/backends/apple/coreml/scripts/build_all.sh @@ -15,7 +15,7 @@ SCRIPT_DIR_PATH="$( EXECUTORCH_ROOT_PATH=$(realpath "$SCRIPT_DIR_PATH/../../../../") COREML_DIR_PATH="$EXECUTORCH_ROOT_PATH/backends/apple/coreml" COREML_EXAMPLES_DIR_PATH="$EXECUTORCH_ROOT_PATH/examples/apple/coreml" -TEST_MODEL_PATH="$COREML_DIR_PATH/runtime/test/models/mv3_coreml_float16_all.pte" +TEST_MODEL_PATH="$COREML_DIR_PATH/runtime/test/models/mv3_coreml_all.pte" red=`tput setaf 1` green=`tput setaf 2` diff --git a/backends/apple/coreml/scripts/generate_test_models.sh b/backends/apple/coreml/scripts/generate_test_models.sh index 9feb21cde55..6a73d697379 100755 --- a/backends/apple/coreml/scripts/generate_test_models.sh +++ b/backends/apple/coreml/scripts/generate_test_models.sh @@ -25,8 +25,8 @@ do echo "Executorch: Generating $MODEL model" # TODO: Don't use the script in examples directory. python3 -m examples.apple.coreml.scripts.export --model_name "$MODEL" --save_processed_bytes - mv -f "$MODEL""_coreml_float16_all.pte" "$COREML_DIR_PATH/runtime/test/models" - mv -f "$MODEL""_coreml_float16_all.bin" "$COREML_DIR_PATH/runtime/test/models" + mv -f "$MODEL""_coreml_all.pte" "$COREML_DIR_PATH/runtime/test/models" + mv -f "$MODEL""_coreml_all.bin" "$COREML_DIR_PATH/runtime/test/models" done echo "Executorch: Generating stateful model" @@ -38,5 +38,5 @@ for MODEL in "${COMPILE_MODELS[@]}" do echo "Executorch: Generating compiled $MODEL model" python3 -m examples.apple.coreml.scripts.export --model_name "$MODEL" --compile - mv -f "$MODEL""_coreml_float16_all_compiled.pte" "$COREML_DIR_PATH/runtime/test/models" + mv -f "$MODEL""_compiled_coreml_all.pte" "$COREML_DIR_PATH/runtime/test/models" done diff --git a/examples/apple/coreml/scripts/export.py b/examples/apple/coreml/scripts/export.py index 8c1ef50209e..3765c6c2aa4 100644 --- a/examples/apple/coreml/scripts/export.py +++ b/examples/apple/coreml/scripts/export.py @@ -136,9 +136,10 @@ def export_lowered_module_to_executorch_program(lowered_module, example_inputs): def get_pte_base_name(args: argparse.Namespace) -> str: - pte_name = f"{args.model_name}_coreml_{args.compute_precision}_{args.compute_unit}" + pte_name = args.model_name if args.compile: pte_name += "_compiled" + pte_name = f"{pte_name}_{args.compute_unit}" return pte_name @@ -254,7 +255,9 @@ def main(): ) save_pte_program(exec_program, pte_base_name) if args.generate_etrecord: - generate_etrecord(f"{pte_base_name}_etrecord.bin", edge_copy, exec_program) + generate_etrecord( + f"{args.model_name}_coreml_etrecord.bin", edge_copy, exec_program + ) if args.save_processed_bytes: save_processed_bytes( From abf0c3ebc7be7881d50a5f05c7e9294e0f2c192c Mon Sep 17 00:00:00 2001 From: Scott Roy <161522778+metascroy@users.noreply.github.com> Date: Thu, 24 Jul 2025 13:08:44 -0700 Subject: [PATCH 4/8] up --- examples/apple/coreml/scripts/export.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/apple/coreml/scripts/export.py b/examples/apple/coreml/scripts/export.py index 3765c6c2aa4..97557c2c377 100644 --- a/examples/apple/coreml/scripts/export.py +++ b/examples/apple/coreml/scripts/export.py @@ -139,7 +139,7 @@ def get_pte_base_name(args: argparse.Namespace) -> str: pte_name = args.model_name if args.compile: pte_name += "_compiled" - pte_name = f"{pte_name}_{args.compute_unit}" + pte_name = f"{pte_name}_coreml_{args.compute_unit}" return pte_name From 909b30427e708e0590c67c564318d5e19cca744d Mon Sep 17 00:00:00 2001 From: Scott Roy <161522778+metascroy@users.noreply.github.com> Date: Thu, 24 Jul 2025 14:58:11 -0700 Subject: [PATCH 5/8] up --- .github/workflows/trunk.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/trunk.yml b/.github/workflows/trunk.yml index 105d313faf6..fe2a5f40065 100644 --- a/.github/workflows/trunk.yml +++ b/.github/workflows/trunk.yml @@ -598,9 +598,9 @@ jobs: else BACKEND="coreml-pybind" fi - PYTHON_EXECUTABLE=python ${CONDA_RUN} bash .ci/scripts/test_model.sh "${MODEL_NAME}" "${BUILD_TOOL}" "${BACKEND}" echo "::endgroup::" + done # Build and test mps model for MODEL_NAME in mv3 ic4 resnet50 edsr mobilebert w2l; do From deb78ad9a01aa1e86a2c2de49728e1fa9b9b914a Mon Sep 17 00:00:00 2001 From: Scott Roy <161522778+metascroy@users.noreply.github.com> Date: Mon, 28 Jul 2025 10:15:44 -0700 Subject: [PATCH 6/8] up] --- .ci/scripts/test_model.sh | 9 +++++-- .github/workflows/trunk.yml | 50 +++++++++++++++++++++++++------------ 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/.ci/scripts/test_model.sh b/.ci/scripts/test_model.sh index 7af74a91c34..1333f481866 100755 --- a/.ci/scripts/test_model.sh +++ b/.ci/scripts/test_model.sh @@ -233,6 +233,7 @@ test_model_with_qnn() { test_model_with_coreml() { local should_test="$1" local test_with_pybindings="$2" + local dtype="$3" if [[ "${BUILD_TOOL}" != "cmake" ]]; then echo "coreml only supports cmake." @@ -245,7 +246,7 @@ test_model_with_coreml() { export RUN_WITH_PYBINDINGS="--run_with_pybindings" fi - "${PYTHON_EXECUTABLE}" -m examples.apple.coreml.scripts.export --model_name="${MODEL_NAME}" --compute_precision float16 --use_partitioner ${RUN_WITH_PYBINDINGS} + "${PYTHON_EXECUTABLE}" -m examples.apple.coreml.scripts.export --model_name="${MODEL_NAME}" --compute_precision ${dtype} --use_partitioner ${RUN_WITH_PYBINDINGS} EXPORTED_MODEL=$(find "." -type f -name "${MODEL_NAME}*.pte" -print -quit) if [ -n "$EXPORTED_MODEL" ]; then @@ -309,7 +310,11 @@ elif [[ "${BACKEND}" == *"coreml"* ]]; then if [[ "${BACKEND}" == *"pybind"* ]]; then test_with_pybindings=true fi - test_model_with_coreml "${should_test_coreml}" "${test_with_pybindings}" + dtype=float16 + if [[ "${BACKEND}" == *"float32"* ]]; then + dtype=float32 + fi + test_model_with_coreml "${should_test_coreml}" "${test_with_pybindings}" "${dtype}" if [[ $? -eq 0 ]]; then prepare_artifacts_upload fi diff --git a/.github/workflows/trunk.yml b/.github/workflows/trunk.yml index fe2a5f40065..9158d8553a8 100644 --- a/.github/workflows/trunk.yml +++ b/.github/workflows/trunk.yml @@ -18,8 +18,8 @@ concurrency: cancel-in-progress: true jobs: - test-models-macos: - name: test-models-macos + test-models-macos-cpu: + name: test-models-macos-cpu uses: pytorch/test-infra/.github/workflows/macos_job.yml@main strategy: matrix: @@ -568,10 +568,12 @@ jobs: PYTHON_EXECUTABLE=python bash .ci/scripts/build-qnn-sdk.sh PYTHON_EXECUTABLE=python bash .ci/scripts/test_model.sh ${{ matrix.model }} "cmake" "qnn" - test-apple-model: - name: test-apple-model + test-models-macos-coreml: + name: test-models-macos-coreml uses: pytorch/test-infra/.github/workflows/macos_job.yml@main strategy: + matrix: + model: [dl3, edsr, efficient_sam, emformer_join, emformer_transcribe, ic3, ic4, mobilebert, mv2, mv3, resnet50, vit, w2l] fail-fast: false with: runner: macos-m1-stable @@ -580,7 +582,16 @@ jobs: ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }} timeout: 90 script: | + MODEL_NAME=${{ matrix.model }} BUILD_TOOL=cmake + BACKEND="coreml-pybind" + + + # Set model specific overrides + if [[ "${MODEL_NAME}" == "mobilebert" ]]; then + # mobilebert has nan output on FP16, and high MSE on fp32, so we disable runtime test now + BACKEND="coreml-pybind-float32" + fi bash .ci/scripts/setup-conda.sh @@ -589,18 +600,25 @@ jobs: PYTHON_EXECUTABLE=python ${CONDA_RUN} bash backends/apple/coreml/scripts/install_requirements.sh echo "Finishing installing coreml." - # Build and test coreml model - for MODEL_NAME in dl3 edsr emformer_join ic3 ic4 mobilebert mv2 mv3 resnet50 vit w2l; do - echo "::group::Exporting coreml model: $MODEL_NAME" - if [[ "${MODEL_NAME}" == "mobilebert" ]]; then - # mobilebert has nan output on FP16 - BACKEND="coreml" - else - BACKEND="coreml-pybind" - fi - PYTHON_EXECUTABLE=python ${CONDA_RUN} bash .ci/scripts/test_model.sh "${MODEL_NAME}" "${BUILD_TOOL}" "${BACKEND}" - echo "::endgroup::" - done + PYTHON_EXECUTABLE=python ${CONDA_RUN} bash .ci/scripts/test_model.sh "${MODEL_NAME}" "${BUILD_TOOL}" "${BACKEND}" + + test-models-macos-mps: + name: test-models-macos-mps + uses: pytorch/test-infra/.github/workflows/macos_job.yml@main + strategy: + fail-fast: false + with: + runner: macos-m1-stable + python-version: '3.11' + submodules: 'recursive' + ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }} + timeout: 90 + script: | + BUILD_TOOL=cmake + bash .ci/scripts/setup-conda.sh + + # Setup MacOS dependencies as there is no Docker support on MacOS atm + PYTHON_EXECUTABLE=python ${CONDA_RUN} bash .ci/scripts/setup-macos.sh --build-tool "${BUILD_TOOL}" # Build and test mps model for MODEL_NAME in mv3 ic4 resnet50 edsr mobilebert w2l; do From 8e6c8ff04cfd3d36765b8f06f05c024cfc9d4576 Mon Sep 17 00:00:00 2001 From: Scott Roy <161522778+metascroy@users.noreply.github.com> Date: Mon, 28 Jul 2025 10:15:59 -0700 Subject: [PATCH 7/8] up --- examples/apple/coreml/scripts/export.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/apple/coreml/scripts/export.py b/examples/apple/coreml/scripts/export.py index 97557c2c377..0b5f64d13c2 100644 --- a/examples/apple/coreml/scripts/export.py +++ b/examples/apple/coreml/scripts/export.py @@ -222,7 +222,7 @@ def main(): compile_specs = generate_compile_specs_from_args(args) pte_base_name = get_pte_base_name(args) if args.use_partitioner: - model.eval() + model = model.eval() assert not args.generate_etrecord, "ETRecord is not supported with partitioner" ep = torch.export.export( model, @@ -230,6 +230,7 @@ def main(): kwargs=example_kwargs, dynamic_shapes=dynamic_shapes, ) + print(ep) delegated_program = exir.to_edge_transform_and_lower( ep, partitioner=[CoreMLPartitioner(compile_specs=compile_specs)], From 4d7045f17d28d79ea46473fd4fe147f4c45aa3eb Mon Sep 17 00:00:00 2001 From: Scott Roy <161522778+metascroy@users.noreply.github.com> Date: Mon, 28 Jul 2025 10:46:43 -0700 Subject: [PATCH 8/8] up --- .github/workflows/trunk.yml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/workflows/trunk.yml b/.github/workflows/trunk.yml index 9158d8553a8..8857029d96b 100644 --- a/.github/workflows/trunk.yml +++ b/.github/workflows/trunk.yml @@ -589,8 +589,15 @@ jobs: # Set model specific overrides if [[ "${MODEL_NAME}" == "mobilebert" ]]; then + # See https://github.com/pytorch/executorch/issues/12907 # mobilebert has nan output on FP16, and high MSE on fp32, so we disable runtime test now - BACKEND="coreml-pybind-float32" + BACKEND="coreml" + fi + + if [[ "${MODEL_NAME}" == "efficient_sam" ]]; then + # See https://github.com/pytorch/executorch/issues/12906 + # efficient_sam fails to run on CoreML + BACKEND="coreml" fi bash .ci/scripts/setup-conda.sh