|
| 1 | +// |
| 2 | +// ETCoreMLModelCache.h |
| 3 | +// |
| 4 | +// Copyright © 2024 Apple Inc. All rights reserved. |
| 5 | +// |
| 6 | +// Please refer to the license found in the LICENSE file in the root directory of the source tree. |
| 7 | + |
| 8 | +#import <Foundation/Foundation.h> |
| 9 | + |
| 10 | +#import "ETCoreMLCacheProtocol.h" |
| 11 | + |
| 12 | +NS_ASSUME_NONNULL_BEGIN |
| 13 | + |
| 14 | +extern NSString* const ETCoreMLModelCacheErrorDomain; |
| 15 | + |
| 16 | +typedef NS_ENUM(NSInteger, ETCoreMLModelCacheErrorCode) { |
| 17 | + ETCoreMLModelCacheErrorCodeUnknown = 0, |
| 18 | + ETCoreMLModelCacheErrorCodeInitializationFailed = 1, |
| 19 | + ETCoreMLModelCacheErrorCodeInvalidIdentifier = 2, |
| 20 | + ETCoreMLModelCacheErrorCodeSourceNotFound = 3, |
| 21 | + ETCoreMLModelCacheErrorCodeDiskFull = 4, |
| 22 | + ETCoreMLModelCacheErrorCodeIOError = 5, |
| 23 | + ETCoreMLModelCacheErrorCodeCorruptedCache = 6, |
| 24 | +}; |
| 25 | + |
| 26 | +/// A simplified, filesystem-based cache for compiled CoreML models. |
| 27 | +/// |
| 28 | +/// This class provides a cache implementation that stores compiled models as directories |
| 29 | +/// in a versioned cache structure. It uses atomic writes (rename) to ensure cache integrity |
| 30 | +/// even in the presence of crashes or concurrent access. |
| 31 | +/// |
| 32 | +/// Directory structure: |
| 33 | +/// ``` |
| 34 | +/// cache_root/ |
| 35 | +/// ├── version.txt (cache format version) |
| 36 | +/// ├── models/ |
| 37 | +/// │ ├── {identifier}.mlmodelc/ (compiled model bundle) |
| 38 | +/// │ ├── {identifier}.accessed (last access time for LRU eviction) |
| 39 | +/// │ └── ... |
| 40 | +/// └── temp/ |
| 41 | +/// └── {uuid}/ (mlpackage files awaiting compilation) |
| 42 | +/// ``` |
| 43 | +/// |
| 44 | +/// ## Thread Safety and Concurrency Guarantees |
| 45 | +/// |
| 46 | +/// This class provides **NO internal synchronization**. It is designed to be used in one of |
| 47 | +/// two ways: |
| 48 | +/// |
| 49 | +/// 1. **Single-threaded access**: All calls to a single instance from one thread/queue. |
| 50 | +/// |
| 51 | +/// 2. **External serialization**: When used via `ETCoreMLModelManager`, access is serialized |
| 52 | +/// by the manager's per-identifier loading queue. This is the expected usage pattern. |
| 53 | +/// |
| 54 | +/// **Multi-process safety** is provided by: |
| 55 | +/// - Atomic filesystem operations (`rename()`) |
| 56 | +/// - Unique temp paths (UUID-based) to avoid conflicts |
| 57 | +/// - "Last writer wins" semantics (acceptable since all writers produce identical output) |
| 58 | +/// |
| 59 | +/// **Multiple instances** pointing to the same directory are safe because: |
| 60 | +/// - Each write uses a unique temp path |
| 61 | +/// - Final placement uses atomic `moveItemAtURL:` (POSIX `rename()`) |
| 62 | +/// - Concurrent writes result in "last writer wins" (both write identical data) |
| 63 | +/// - Cleanup only targets entries older than 24 hours |
| 64 | +/// |
| 65 | +/// **Callers are responsible for**: |
| 66 | +/// - Handling `MLModel` load failures gracefully (cache entry may be replaced/deleted |
| 67 | +/// between URL retrieval and model load) |
| 68 | +/// - Not relying on returned URLs remaining valid indefinitely |
| 69 | +@interface ETCoreMLModelCache : NSObject <ETCoreMLCache> |
| 70 | + |
| 71 | +- (instancetype)init NS_UNAVAILABLE; |
| 72 | ++ (instancetype)new NS_UNAVAILABLE; |
| 73 | + |
| 74 | +/// The root directory for all cache data (contains models/, temp/, version.txt). |
| 75 | +@property (nonatomic, readonly) NSURL* cacheRootDirectory; |
| 76 | + |
| 77 | +/// Whether the cache was initialized successfully and is ready for use. |
| 78 | +/// If NO, all operations will fail. Check this after initialization. |
| 79 | +@property (nonatomic, readonly, getter=isReady) BOOL ready; |
| 80 | + |
| 81 | +/// If `ready` is NO, this contains the error that occurred during initialization. |
| 82 | +@property (nonatomic, readonly, nullable) NSError* initializationError; |
| 83 | + |
| 84 | +/// Initializes the cache with the given root directory. |
| 85 | +/// Creates the directory structure if it doesn't exist. |
| 86 | +/// Check the `ready` property after initialization to verify success. |
| 87 | +/// If initialization fails, `initializationError` will contain the reason. |
| 88 | +/// |
| 89 | +/// @param cacheRootDirectory The root directory for all cache data. |
| 90 | +- (instancetype)initWithCacheRootDirectory:(NSURL*)cacheRootDirectory NS_DESIGNATED_INITIALIZER; |
| 91 | + |
| 92 | +/// Returns the URL of a cached model if it exists and is valid, otherwise nil. |
| 93 | +/// |
| 94 | +/// @param identifier The unique identifier for the cached model. |
| 95 | +/// @param error On failure, error is filled with the failure information. |
| 96 | +/// @return The URL to the cached model bundle, or nil if not found or invalid. |
| 97 | +/// |
| 98 | +/// @warning The returned URL may become invalid before the caller uses it if another |
| 99 | +/// process deletes or replaces the cached model. Callers MUST handle MLModel load |
| 100 | +/// failures gracefully by treating them as cache misses and recompiling. |
| 101 | +- (nullable NSURL*)cachedModelURLForIdentifier:(NSString*)identifier error:(NSError**)error; |
| 102 | + |
| 103 | +/// Stores a compiled model in the cache. Returns the cached URL on success. |
| 104 | +/// |
| 105 | +/// @param compiledModelURL The URL of the compiled model bundle to cache. Must exist. |
| 106 | +/// @param identifier The unique identifier for this model. Must not contain '/' or '..'. |
| 107 | +/// @param error On failure, contains the error. Check for ETCoreMLModelCacheErrorCodeDiskFull |
| 108 | +/// to handle out-of-space conditions specially. |
| 109 | +/// @return The URL of the cached model, or nil on failure. |
| 110 | +- (nullable NSURL*)storeModelAtURL:(NSURL*)compiledModelURL withIdentifier:(NSString*)identifier error:(NSError**)error; |
| 111 | + |
| 112 | +/// Removes a specific cached model. This is a best-effort operation that removes |
| 113 | +/// the model bundle and access time files for the given identifier. |
| 114 | +/// |
| 115 | +/// @param identifier The unique identifier for the cached model to remove. |
| 116 | +/// @param error On failure, error is filled with the failure information. |
| 117 | +/// @return YES on success (including if the model didn't exist), NO on validation errors. |
| 118 | +- (BOOL)removeCachedModelWithIdentifier:(NSString*)identifier error:(NSError**)error; |
| 119 | + |
| 120 | +/// Clears the entire cache, including all cached models. |
| 121 | +/// Recreates the empty directory structure after clearing. |
| 122 | +/// |
| 123 | +/// @param error On failure, error is filled with the failure information. |
| 124 | +/// @return YES if the cache was purged successfully, otherwise NO. |
| 125 | +- (BOOL)purgeAndReturnError:(NSError**)error; |
| 126 | + |
| 127 | +#pragma mark - Temp Directory (for mlpackage extraction before compilation) |
| 128 | + |
| 129 | +/// Returns a temp URL where an mlpackage can be extracted before compilation. |
| 130 | +/// The caller is responsible for cleaning up this directory after compilation completes. |
| 131 | +/// |
| 132 | +/// @param error On failure, error is filled with the failure information. |
| 133 | +/// @return A temp URL where the mlpackage can be extracted, or nil on failure. |
| 134 | +/// |
| 135 | +/// @note The temp URL is unique and includes a UUID to avoid conflicts. |
| 136 | +/// @note Temp entries are automatically cleaned up after 24 hours if not removed. |
| 137 | +- (nullable NSURL*)temporaryDirectoryWithError:(NSError**)error; |
| 138 | + |
| 139 | +@end |
| 140 | + |
| 141 | +NS_ASSUME_NONNULL_END |
0 commit comments