Skip to content

Commit 4ab9a72

Browse files
committed
Re-land asseet management changes
1 parent 16ff41b commit 4ab9a72

File tree

4 files changed

+75
-13
lines changed

4 files changed

+75
-13
lines changed

backends/apple/coreml/runtime/delegate/ETCoreMLAssetManager.mm

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,13 @@ - (nullable ETCoreMLAsset *)_storeAssetAtURL:(NSURL *)srcURL
437437
}
438438

439439
// If a file already exists at `dstURL`, move it to the trash for removal.
440-
move_to_directory(dstURL, self.trashDirectoryURL, self.fileManager, nil);
440+
if (dstURL && [self.fileManager fileExistsAtPath:dstURL.path]) {
441+
if (!move_to_directory(dstURL, self.trashDirectoryURL, self.fileManager, error)) {
442+
// If we failed to move the file to the trash, we can't proceed
443+
return false;
444+
}
445+
}
446+
441447
// Move the asset to assets directory.
442448
if (![self.fileManager moveItemAtURL:srcURL toURL:dstURL error:error]) {
443449
return false;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
@class ETCoreMLModel;
1111
@class ETCoreMLAssetManager;
12+
@class ETCoreMLAsset;
1213

1314
namespace executorchcoreml {
1415
struct ModelMetadata;
@@ -23,6 +24,12 @@ __attribute__((objc_subclassing_restricted))
2324

2425
- (instancetype)init NS_UNAVAILABLE;
2526

27+
28+
+ (nullable ETCoreMLModel*)loadModelWithCompiledAsset:(ETCoreMLAsset*)compiledAsset
29+
configuration:(MLModelConfiguration*)configuration
30+
metadata:(const executorchcoreml::ModelMetadata&)metadata
31+
error:(NSError* __autoreleasing*)error;
32+
2633
/// Synchronously loads a model given the location of its on-disk representation and configuration.
2734
///
2835
/// @param compiledModelURL The location of the model's on-disk representation (.mlmodelc directory).

backends/apple/coreml/runtime/delegate/ETCoreMLModelLoader.mm

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@
4444

4545
@implementation ETCoreMLModelLoader
4646

47+
+ (nullable ETCoreMLModel *)loadModelWithCompiledAsset:(ETCoreMLAsset *)compiledAsset
48+
configuration:(MLModelConfiguration *)configuration
49+
metadata:(const executorchcoreml::ModelMetadata&)metadata
50+
error:(NSError * __autoreleasing *)error {
51+
NSError *localError = nil;
52+
ETCoreMLModel *model = (compiledAsset != nil) ? get_model_from_asset(compiledAsset, configuration, metadata, &localError) : nil;
53+
if (model) {
54+
return model;
55+
}
56+
if (error) {
57+
*error = localError;
58+
}
59+
return nil;
60+
}
61+
62+
4763
+ (nullable ETCoreMLModel *)loadModelWithContentsOfURL:(NSURL *)compiledModelURL
4864
configuration:(MLModelConfiguration *)configuration
4965
metadata:(const executorchcoreml::ModelMetadata&)metadata
@@ -55,10 +71,28 @@ + (nullable ETCoreMLModel *)loadModelWithContentsOfURL:(NSURL *)compiledModelURL
5571
if ([assetManager hasAssetWithIdentifier:identifier error:&localError]) {
5672
asset = [assetManager assetWithIdentifier:identifier error:&localError];
5773
} else {
74+
ETCoreMLLogInfo("Storing asset with identifier=%@ in assetManager", identifier);
5875
asset = [assetManager storeAssetAtURL:compiledModelURL withIdentifier:identifier error:&localError];
5976
}
60-
61-
ETCoreMLModel *model = (asset != nil) ? get_model_from_asset(asset, configuration, metadata, &localError) : nil;
77+
78+
if (asset == nil) {
79+
ETCoreMLLogInfo("Failed to retrieve or store asset with identifier=%@ in assetManager", identifier);
80+
ETCoreMLLogInfo("compiledModelURL=%@", compiledModelURL);
81+
ETCoreMLLogInfo("error=%@", localError);
82+
ETCoreMLLogInfo("Attempting to fall back by loading model from compiledModelURL without transferring to assetManager");
83+
auto backingAsset = Asset::make(compiledModelURL, identifier, assetManager.fileManager, &localError);
84+
if (!backingAsset) {
85+
ETCoreMLLogInfo("Failed to create a backing asset with error=%@", localError);
86+
return nil;
87+
}
88+
asset = [[ETCoreMLAsset alloc] initWithBackingAsset:backingAsset.value()];
89+
}
90+
91+
ETCoreMLModel *model;
92+
if (asset != nil) {
93+
model = [self loadModelWithCompiledAsset:asset configuration:configuration metadata:metadata error:&localError];
94+
}
95+
6296
if (model) {
6397
return model;
6498
}

backends/apple/coreml/runtime/delegate/ETCoreMLModelManager.mm

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -472,28 +472,44 @@ - (nullable ETCoreMLAsset *)compiledModelAssetWithMetadata:(const ModelMetadata&
472472
__block ETCoreMLAsset *compiledModelAsset = [self assetWithIdentifier:identifier];
473473
if (compiledModelAsset) {
474474
ETCoreMLLogInfo("Cache Hit: Successfully retrieved compiled model with identifier=%@ from the models cache.", identifier);
475-
} else {
476-
ETCoreMLLogInfo("Cache Miss: Compiled Model with identifier=%@ was not found in the models cache.", identifier);
475+
return compiledModelAsset;
477476
}
478-
477+
478+
ETCoreMLLogInfo("Cache Miss: Compiled Model with identifier=%@ was not found in the models cache.", identifier);
479+
__block NSURL *compiledModelURL;
479480
[self.assetManager withTemporaryDirectory:^(NSURL * _Nonnull directoryURL) {
480-
if (compiledModelAsset) {
481-
return;
482-
}
483-
484481
// The directory specified by `directoryURL` is unique and will be automatically cleaned up
485482
// once the enclosing block completes.
486-
NSURL *compiledModelURL = [self compiledModelURLWithIdentifier:identifier
483+
compiledModelURL = [self compiledModelURLWithIdentifier:identifier
487484
modelURL:modelURL
488485
inMemoryFS:inMemoryFS
489486
dstURL:directoryURL
490487
error:error];
491488
if (compiledModelURL) {
492489
// Move the compiled model to the asset manager to transfer ownership.
490+
ETCoreMLLogInfo("Successfully compiled model with identifier=%@. Transferring ownership to assetManager.", identifier);
493491
compiledModelAsset = [self.assetManager storeAssetAtURL:compiledModelURL withIdentifier:identifier error:error];
494492
}
495493
}];
496494

495+
if (!compiledModelAsset) {
496+
ETCoreMLLogInfo("Failed to transfer ownership of asset with identifier=%@ to assetManager", identifier);
497+
if (compiledModelURL && [self.fileManager fileExistsAtPath:compiledModelURL.path]) {
498+
// Log what error was since we now attempt backup path, and previous error is overwritten
499+
if (error && *error) {
500+
ETCoreMLLogInfo("error=%@", (*error).localizedDescription);
501+
*error = nil;
502+
}
503+
ETCoreMLLogInfo("Attempting to fall back by loading model without transferring ownership");
504+
auto backingAsset = Asset::make(compiledModelURL, identifier, self.assetManager.fileManager, error);
505+
if (backingAsset) {
506+
compiledModelAsset = [[ETCoreMLAsset alloc] initWithBackingAsset:backingAsset.value()];
507+
}
508+
}
509+
}
510+
511+
// compiledModelAsset can still be nil if our backup path failed
512+
497513
return compiledModelAsset;
498514
}
499515

@@ -582,10 +598,9 @@ - (nullable ETCoreMLAsset *)modelAssetWithMetadata:(const ModelMetadata&)metadat
582598
return nil;
583599
}
584600

585-
ETCoreMLModel *model = [ETCoreMLModelLoader loadModelWithContentsOfURL:compiledModelAsset.contentURL
601+
ETCoreMLModel *model = [ETCoreMLModelLoader loadModelWithCompiledAsset:compiledModelAsset
586602
configuration:configuration
587603
metadata:metadata
588-
assetManager:self.assetManager
589604
error:error];
590605
if (!model) {
591606
return nil;

0 commit comments

Comments
 (0)