Skip to content

Commit b07be36

Browse files
authored
Fix compiled model export. (#6568)
1 parent c8a3918 commit b07be36

File tree

7 files changed

+27
-4
lines changed

7 files changed

+27
-4
lines changed

backends/apple/coreml/compiler/coreml_preprocess.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,12 +425,15 @@ def preprocess(
425425
CoreMLBackend.op_linear_quantizer_config_from_compile_specs(compile_specs)
426426
)
427427

428+
# Load the model if MODEL_TYPE is 'COMPILED_MODEL'. This step is necessary because
429+
# get_compiled_model_path() requires a loaded model.
430+
skip_model_load = model_type != CoreMLBackend.MODEL_TYPE.COMPILED_MODEL
428431
mlmodel = ct.convert(
429432
model=edge_program,
430433
source="pytorch",
431434
convert_to="mlprogram",
432435
pass_pipeline=ct.PassPipeline.DEFAULT,
433-
skip_model_load=True,
436+
skip_model_load=skip_model_load,
434437
compute_precision=model_compute_precision,
435438
minimum_deployment_target=minimum_deployment_target,
436439
compute_units=compute_units,

backends/apple/coreml/runtime/delegate/backend_delegate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class BackendDelegate {
2828
// Max models cache size in bytes.
2929
size_t max_models_cache_size = 10 * size_t(1024) * size_t(1024) * size_t(1024);
3030
// If set to `true`, delegate pre-warms the most recently used asset.
31-
bool should_prewarm_asset = true;
31+
bool should_prewarm_asset = false;
3232
// If set to `true`, delegate pre-warms the model in `init`.
3333
bool should_prewarm_model = true;
3434
};

backends/apple/coreml/runtime/delegate/com.apple.executorchcoreml_config.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<plist version="1.0">
44
<dict>
55
<key>shouldPrewarmAsset</key>
6-
<true/>
6+
<false/>
77
<key>shouldPrewarmModel</key>
88
<true/>
99
<key>maxAssetsSizeInBytes</key>

backends/apple/coreml/runtime/test/CoreMLBackendDelegateTests.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ - (void)testStateProgramExecute {
209209
}
210210
#endif
211211

212+
- (void)testAddMulCompiledProgramExecute {
213+
NSURL *modelURL = [[self class] bundledResourceWithName:@"add_mul_compiled_coreml_all" extension:@"pte"];
214+
XCTAssertNotNil(modelURL);
215+
[self executeModelAtURL:modelURL nLoads:1 nExecutions:2];
216+
}
217+
212218
- (void)executeMultipleModelsConcurrently:(NSArray<NSURL *> *)modelURLs
213219
nLoads:(NSUInteger)nLoads
214220
nExecutions:(NSUInteger)nExecutions

backends/apple/coreml/runtime/workspace/executorchcoreml.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
8307EB8A2C9262060011AE6D /* state_coreml_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = 8307EB892C9262060011AE6D /* state_coreml_all.pte */; };
11+
838CA6872CD1965700462190 /* add_mul_compiled_coreml_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = 838CA6862CD1965700462190 /* add_mul_compiled_coreml_all.pte */; };
1112
83BB78A02C65DA7300274ED7 /* ETCoreMLModelDebugInfo.mm in Sources */ = {isa = PBXBuildFile; fileRef = 83BB789F2C65DA7300274ED7 /* ETCoreMLModelDebugInfo.mm */; };
1213
83BB78BF2C66AAAE00274ED7 /* add_mul_coreml_all.bin in Resources */ = {isa = PBXBuildFile; fileRef = 83BB78BD2C66AAAE00274ED7 /* add_mul_coreml_all.bin */; };
1314
83BB78C02C66AAAE00274ED7 /* add_mul_coreml_all.pte in Resources */ = {isa = PBXBuildFile; fileRef = 83BB78BE2C66AAAE00274ED7 /* add_mul_coreml_all.pte */; };
@@ -122,6 +123,7 @@
122123

123124
/* Begin PBXFileReference section */
124125
8307EB892C9262060011AE6D /* state_coreml_all.pte */ = {isa = PBXFileReference; lastKnownFileType = file; name = state_coreml_all.pte; path = ../test/models/state_coreml_all.pte; sourceTree = "<group>"; };
126+
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 = "<group>"; };
125127
83BB789E2C65DA7300274ED7 /* ETCoreMLModelDebugInfo.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = ETCoreMLModelDebugInfo.h; path = ../sdk/ETCoreMLModelDebugInfo.h; sourceTree = "<group>"; };
126128
83BB789F2C65DA7300274ED7 /* ETCoreMLModelDebugInfo.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = ETCoreMLModelDebugInfo.mm; path = ../sdk/ETCoreMLModelDebugInfo.mm; sourceTree = "<group>"; };
127129
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 = "<group>"; };
@@ -606,6 +608,7 @@
606608
C98551992AD2542D009143F9 /* mul_coreml_all.bin */,
607609
C985519C2AD2542D009143F9 /* mul_coreml_all.pte */,
608610
C985519B2AD2542D009143F9 /* mv3_coreml_all.bin */,
611+
838CA6862CD1965700462190 /* add_mul_compiled_coreml_all.pte */,
609612
C98551982AD2542D009143F9 /* mv3_coreml_all.pte */,
610613
83BB78BD2C66AAAE00274ED7 /* add_mul_coreml_all.bin */,
611614
83BB78BE2C66AAAE00274ED7 /* add_mul_coreml_all.pte */,
@@ -680,6 +683,7 @@
680683
C985519E2AD2542D009143F9 /* mv3_coreml_all.pte in Resources */,
681684
C98551A02AD2542D009143F9 /* add_coreml_all.bin in Resources */,
682685
C98551A22AD2542D009143F9 /* mul_coreml_all.pte in Resources */,
686+
838CA6872CD1965700462190 /* add_mul_compiled_coreml_all.pte in Resources */,
683687
8307EB8A2C9262060011AE6D /* state_coreml_all.pte in Resources */,
684688
C98551A32AD2542D009143F9 /* add_coreml_all.pte in Resources */,
685689
);

backends/apple/coreml/scripts/generate_test_models.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,12 @@ done
3131

3232
echo "Executorch: Generating stateful model"
3333
python3 "$SCRIPT_DIR_PATH/../runtime/test/export_stateful_model.py"
34+
35+
COMPILE_MODELS=("add_mul")
36+
echo "Executorch: Generating compiled model"
37+
for MODEL in "${COMPILE_MODELS[@]}"
38+
do
39+
echo "Executorch: Generating compiled $MODEL model"
40+
python3 -m examples.apple.coreml.scripts.export --model_name "$MODEL" --compile
41+
mv -f "$MODEL""_compiled_coreml_all.pte" "$COREML_DIR_PATH/runtime/test/models"
42+
done

examples/apple/coreml/scripts/export.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ def main():
192192
example_inputs,
193193
)
194194

195-
save_executorch_program(exec_program, args.model_name, args.compute_unit)
195+
model_name = f"{args.model_name}_compiled" if args.compile else args.model_name
196+
save_executorch_program(exec_program, model_name, args.compute_unit)
196197
generate_etrecord(f"{args.model_name}_coreml_etrecord.bin", edge_copy, exec_program)
197198

198199
if args.save_processed_bytes and lowered_module is not None:

0 commit comments

Comments
 (0)