19
19
namespace {
20
20
using namespace executorchcoreml ::modelstructure;
21
21
22
+ #if MODEL_PROFILING_IS_AVAILABLE
23
+
22
24
API_AVAILABLE (macos(14.4 ), ios(17.4 ), tvos(17.4 ), watchos(10.4 ))
23
25
MLComputePlan *_Nullable get_compute_plan_of_model_at_url(NSURL *model_url,
24
26
MLModelConfiguration *configuration,
@@ -212,20 +214,24 @@ void set_model_outputs(id<MLFeatureProvider> output_features,
212
214
213
215
*model_outputs = values;
214
216
}
217
+
218
+ #endif
219
+
215
220
}
216
221
217
- API_AVAILABLE (macos(14.4 ), ios(17.4 ), tvos(17.4 ), watchos(10.4 ))
218
222
@interface ETCoreMLModelProfiler ()
219
223
// / The CoreML model.
220
224
@property (readonly , strong , nonatomic ) MLModel *model;
221
225
// / The model output names.
222
226
@property (readonly , copy , nonatomic ) NSOrderedSet <NSString *> *outputNames;
227
+ #if MODEL_PROFILING_IS_AVAILABLE
223
228
// / The compute plan.
224
- @property (readonly, strong, nonatomic) MLComputePlan *computePlan;
229
+ @property (readonly , strong , nonatomic ) MLComputePlan *computePlan API_AVAILABLE (macos(14.4 ), ios(17.4 ), tvos(17.4 ), watchos(10.4 ));
230
+ // / The topologically sorted operations.
231
+ @property (readonly , copy , nonatomic ) NSArray <MLModelStructureProgramOperation *> *topologicallySortedOperations API_AVAILABLE (macos(14.4 ), ios(17.4 ), tvos(17.4 ), watchos(10.4 ));
232
+ #endif
225
233
// / The mapping from operation to it's path in the model structure.
226
234
@property (readonly , strong , nonatomic ) NSDictionary <NSValue *, ETCoreMLModelStructurePath *> *operationToPathMap;
227
- // / The topologically sorted operations.
228
- @property (readonly, copy, nonatomic) NSArray <MLModelStructureProgramOperation *> *topologicallySortedOperations;
229
235
// / The profiling infos for all the operations.
230
236
@property (readonly , copy , nonatomic ) NSDictionary <ETCoreMLModelStructurePath *, ETCoreMLOperationProfilingInfo *> *profilingInfos;
231
237
@@ -237,6 +243,7 @@ - (nullable instancetype)initWithCompiledModelAsset:(ETCoreMLAsset *)compiledMod
237
243
outputNames : (NSOrderedSet <NSString *> *)outputNames
238
244
configuration : (MLModelConfiguration *)configuration
239
245
error : (NSError * __autoreleasing *)error {
246
+ #if MODEL_PROFILING_IS_AVAILABLE
240
247
if (@available (macOS 14.4 , iOS 17.4 , tvOS 17.4 , watchOS 10.4 , *)) {
241
248
NSURL *compiledModelURL = compiledModelAsset.contentURL ;
242
249
MLComputePlan *computePlan = get_compute_plan_of_model_at_url (compiledModelURL,
@@ -282,20 +289,21 @@ - (nullable instancetype)initWithCompiledModelAsset:(ETCoreMLAsset *)compiledMod
282
289
}
283
290
284
291
return self;
285
- } else {
286
- ETCoreMLLogErrorAndSetNSError (error,
287
- ETCoreMLErrorCorruptedModel,
288
- " %@: Model profiling is only available for macOS >= 14.4, iOS >= 17.4, tvOS >= 17.4 and watchOS >= 10.4." ,
289
- NSStringFromClass (self.class ));
290
- return nil ;
291
292
}
293
+ #endif
294
+ ETCoreMLLogErrorAndSetNSError (error,
295
+ ETCoreMLErrorCorruptedModel,
296
+ " %@: Model profiling is only available for macOS >= 14.4, iOS >= 17.4, tvOS >= 17.4 and watchOS >= 10.4." ,
297
+ NSStringFromClass (self.class ));
298
+ return nil ;
292
299
}
293
300
294
301
- (nullable ETCoreMLModelProfilingResult *)profilingInfoForOperationsAtPaths : (NSArray <ETCoreMLModelStructurePath *> *)paths
295
302
options : (MLPredictionOptions *)options
296
303
inputs : (id <MLFeatureProvider>)inputs
297
304
modelOutputs : (NSArray <MLMultiArray *> *_Nullable __autoreleasing *_Nonnull)modelOutputs
298
305
error : (NSError * __autoreleasing *)error {
306
+ #if MODEL_PROFILING_IS_AVAILABLE
299
307
uint64_t modelExecutionStartTime = mach_absolute_time ();
300
308
id <MLFeatureProvider> outputFeatures = [self .model predictionFromFeatures: inputs options: options error: error];
301
309
uint64_t modelExecutionEndTime = mach_absolute_time ();
@@ -319,14 +327,15 @@ - (nullable ETCoreMLModelProfilingResult *)profilingInfoForOperationsAtPaths:(NS
319
327
320
328
return profilingInfos;
321
329
}
322
-
330
+ # endif
323
331
return nil ;
324
332
}
325
333
326
334
- (nullable ETCoreMLModelProfilingResult *)profilingInfoForAllOperationsWithOptions : (MLPredictionOptions *)options
327
335
inputs : (id <MLFeatureProvider>)inputs
328
336
modelOutputs : (NSArray <MLMultiArray *> *_Nullable __autoreleasing *_Nonnull)modelOutputs
329
337
error : (NSError * __autoreleasing *)error {
338
+ #if MODEL_PROFILING_IS_AVAILABLE
330
339
if (@available (macOS 14.4 , iOS 17.4 , tvOS 17.4 , watchOS 10.4 , *)) {
331
340
__block NSMutableArray <ETCoreMLModelStructurePath *> *paths = [NSMutableArray array ];
332
341
visit_program_operation (self.computePlan .modelStructure , ^BOOL (MLModelStructureProgramOperation *operation, ETCoreMLModelStructurePath *path) {
@@ -342,7 +351,7 @@ - (nullable ETCoreMLModelProfilingResult *)profilingInfoForAllOperationsWithOpti
342
351
modelOutputs: modelOutputs
343
352
error: error];
344
353
}
345
-
354
+ # endif
346
355
return nil ;
347
356
}
348
357
0 commit comments