From 289605a56c20aa76ea71877de56c209512ec555e Mon Sep 17 00:00:00 2001 From: Gyanendra Sinha Date: Tue, 29 Oct 2024 15:23:40 -0700 Subject: [PATCH] Fix compiled model export. --- backends/apple/coreml/compiler/coreml_preprocess.py | 5 ++++- .../apple/coreml/runtime/delegate/backend_delegate.h | 2 +- .../delegate/com.apple.executorchcoreml_config.plist | 2 +- .../coreml/runtime/test/CoreMLBackendDelegateTests.mm | 6 ++++++ .../workspace/executorchcoreml.xcodeproj/project.pbxproj | 4 ++++ backends/apple/coreml/scripts/generate_test_models.sh | 9 +++++++++ examples/apple/coreml/scripts/export.py | 3 ++- 7 files changed, 27 insertions(+), 4 deletions(-) diff --git a/backends/apple/coreml/compiler/coreml_preprocess.py b/backends/apple/coreml/compiler/coreml_preprocess.py index 5084405c468..c7828888ee5 100644 --- a/backends/apple/coreml/compiler/coreml_preprocess.py +++ b/backends/apple/coreml/compiler/coreml_preprocess.py @@ -425,12 +425,15 @@ def preprocess( CoreMLBackend.op_linear_quantizer_config_from_compile_specs(compile_specs) ) + # Load the model if MODEL_TYPE is 'COMPILED_MODEL'. This step is necessary because + # get_compiled_model_path() requires a loaded model. + skip_model_load = model_type != CoreMLBackend.MODEL_TYPE.COMPILED_MODEL mlmodel = ct.convert( model=edge_program, source="pytorch", convert_to="mlprogram", pass_pipeline=ct.PassPipeline.DEFAULT, - skip_model_load=True, + skip_model_load=skip_model_load, compute_precision=model_compute_precision, minimum_deployment_target=minimum_deployment_target, compute_units=compute_units, diff --git a/backends/apple/coreml/runtime/delegate/backend_delegate.h b/backends/apple/coreml/runtime/delegate/backend_delegate.h index ed921fb35bd..a6e012a4480 100644 --- a/backends/apple/coreml/runtime/delegate/backend_delegate.h +++ b/backends/apple/coreml/runtime/delegate/backend_delegate.h @@ -28,7 +28,7 @@ class BackendDelegate { // Max models cache size in bytes. size_t max_models_cache_size = 10 * size_t(1024) * size_t(1024) * size_t(1024); // If set to `true`, delegate pre-warms the most recently used asset. - bool should_prewarm_asset = true; + bool should_prewarm_asset = false; // If set to `true`, delegate pre-warms the model in `init`. bool should_prewarm_model = true; }; diff --git a/backends/apple/coreml/runtime/delegate/com.apple.executorchcoreml_config.plist b/backends/apple/coreml/runtime/delegate/com.apple.executorchcoreml_config.plist index df37a47755f..899bf12bbea 100644 --- a/backends/apple/coreml/runtime/delegate/com.apple.executorchcoreml_config.plist +++ b/backends/apple/coreml/runtime/delegate/com.apple.executorchcoreml_config.plist @@ -3,7 +3,7 @@ shouldPrewarmAsset - + shouldPrewarmModel maxAssetsSizeInBytes diff --git a/backends/apple/coreml/runtime/test/CoreMLBackendDelegateTests.mm b/backends/apple/coreml/runtime/test/CoreMLBackendDelegateTests.mm index ef114546fec..661f91aa702 100644 --- a/backends/apple/coreml/runtime/test/CoreMLBackendDelegateTests.mm +++ b/backends/apple/coreml/runtime/test/CoreMLBackendDelegateTests.mm @@ -209,6 +209,12 @@ - (void)testStateProgramExecute { } #endif +- (void)testAddMulCompiledProgramExecute { + NSURL *modelURL = [[self class] bundledResourceWithName:@"add_mul_compiled_coreml_all" extension:@"pte"]; + XCTAssertNotNil(modelURL); + [self executeModelAtURL:modelURL nLoads:1 nExecutions:2]; +} + - (void)executeMultipleModelsConcurrently:(NSArray *)modelURLs nLoads:(NSUInteger)nLoads nExecutions:(NSUInteger)nExecutions diff --git a/backends/apple/coreml/runtime/workspace/executorchcoreml.xcodeproj/project.pbxproj b/backends/apple/coreml/runtime/workspace/executorchcoreml.xcodeproj/project.pbxproj index 1cb29d7c962..6ff30636a3b 100644 --- a/backends/apple/coreml/runtime/workspace/executorchcoreml.xcodeproj/project.pbxproj +++ b/backends/apple/coreml/runtime/workspace/executorchcoreml.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* 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 */; }; 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 */; }; @@ -122,6 +123,7 @@ /* 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 = ""; }; 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 = ""; }; @@ -606,6 +608,7 @@ 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 */, @@ -680,6 +683,7 @@ 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_all.pte in Resources */, ); diff --git a/backends/apple/coreml/scripts/generate_test_models.sh b/backends/apple/coreml/scripts/generate_test_models.sh index 0c1822aa828..001ba362393 100755 --- a/backends/apple/coreml/scripts/generate_test_models.sh +++ b/backends/apple/coreml/scripts/generate_test_models.sh @@ -31,3 +31,12 @@ done echo "Executorch: Generating stateful model" python3 "$SCRIPT_DIR_PATH/../runtime/test/export_stateful_model.py" + +COMPILE_MODELS=("add_mul") +echo "Executorch: Generating compiled model" +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""_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 a83f1695f66..53316ea2001 100644 --- a/examples/apple/coreml/scripts/export.py +++ b/examples/apple/coreml/scripts/export.py @@ -192,7 +192,8 @@ def main(): example_inputs, ) - save_executorch_program(exec_program, args.model_name, args.compute_unit) + 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: