Skip to content

Commit 7edd2fa

Browse files
cymbalrushfacebook-github-bot
authored andcommitted
Fix tests compilation on sdk < 14.4 (#2484)
Summary: The delegate tests are failing compilation on macOS sdk < 14.4, added header check to make sure that MLComputePlan changes are not compiled on lower sdks. Tested on macOS sdk 14.4 and sdk 14.2. Pull Request resolved: #2484 Reviewed By: digantdesai Differential Revision: D55018113 Pulled By: shoumikhin fbshipit-source-id: d61031c9607db6c61bac3a9cc42b5c4f398b44d8
1 parent 8e4918f commit 7edd2fa

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

backends/apple/coreml/runtime/sdk/ETCoreMLModelProfiler.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@
66
// Please refer to the license found in the LICENSE file in the root directory of the source tree.
77

88
#import <CoreML/CoreML.h>
9-
10-
#import "ETCoreMLPair.h"
9+
#import <ETCoreMLPair.h>
10+
#import <TargetConditionals.h>
1111

1212
@class ETCoreMLModel;
1313
@class ETCoreMLModelStructurePath;
1414
@class ETCoreMLOperationProfilingInfo;
1515

1616
typedef NSDictionary<ETCoreMLModelStructurePath*, ETCoreMLOperationProfilingInfo*> ETCoreMLModelProfilingResult;
1717

18+
#if !defined(MODEL_PROFILING_IS_AVAILABLE) && __has_include(<CoreML/MLComputePlan.h>)
19+
#define MODEL_PROFILING_IS_AVAILABLE 1
20+
#endif
21+
1822
NS_ASSUME_NONNULL_BEGIN
1923
/// A class responsible for profiling a model.
2024
__attribute__((objc_subclassing_restricted)) @interface ETCoreMLModelProfiler : NSObject

backends/apple/coreml/runtime/sdk/ETCoreMLModelProfiler.mm

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
namespace {
2020
using namespace executorchcoreml::modelstructure;
2121

22+
#if MODEL_PROFILING_IS_AVAILABLE
23+
2224
API_AVAILABLE(macos(14.4), ios(17.4), tvos(17.4), watchos(10.4))
2325
MLComputePlan *_Nullable get_compute_plan_of_model_at_url(NSURL *model_url,
2426
MLModelConfiguration *configuration,
@@ -212,20 +214,24 @@ void set_model_outputs(id<MLFeatureProvider> output_features,
212214

213215
*model_outputs = values;
214216
}
217+
218+
#endif
219+
215220
}
216221

217-
API_AVAILABLE(macos(14.4), ios(17.4), tvos(17.4), watchos(10.4))
218222
@interface ETCoreMLModelProfiler ()
219223
/// The CoreML model.
220224
@property (readonly, strong, nonatomic) MLModel *model;
221225
/// The model output names.
222226
@property (readonly, copy, nonatomic) NSOrderedSet<NSString *> *outputNames;
227+
#if MODEL_PROFILING_IS_AVAILABLE
223228
/// 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
225233
/// The mapping from operation to it's path in the model structure.
226234
@property (readonly, strong, nonatomic) NSDictionary<NSValue *, ETCoreMLModelStructurePath *> *operationToPathMap;
227-
/// The topologically sorted operations.
228-
@property (readonly, copy, nonatomic) NSArray<MLModelStructureProgramOperation *> *topologicallySortedOperations;
229235
/// The profiling infos for all the operations.
230236
@property (readonly, copy, nonatomic) NSDictionary<ETCoreMLModelStructurePath *, ETCoreMLOperationProfilingInfo *> *profilingInfos;
231237

@@ -237,6 +243,7 @@ - (nullable instancetype)initWithCompiledModelAsset:(ETCoreMLAsset *)compiledMod
237243
outputNames:(NSOrderedSet<NSString *> *)outputNames
238244
configuration:(MLModelConfiguration *)configuration
239245
error:(NSError * __autoreleasing *)error {
246+
#if MODEL_PROFILING_IS_AVAILABLE
240247
if (@available(macOS 14.4, iOS 17.4, tvOS 17.4, watchOS 10.4, *)) {
241248
NSURL *compiledModelURL = compiledModelAsset.contentURL;
242249
MLComputePlan *computePlan = get_compute_plan_of_model_at_url(compiledModelURL,
@@ -282,20 +289,21 @@ - (nullable instancetype)initWithCompiledModelAsset:(ETCoreMLAsset *)compiledMod
282289
}
283290

284291
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;
291292
}
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;
292299
}
293300

294301
- (nullable ETCoreMLModelProfilingResult *)profilingInfoForOperationsAtPaths:(NSArray<ETCoreMLModelStructurePath *> *)paths
295302
options:(MLPredictionOptions *)options
296303
inputs:(id<MLFeatureProvider>)inputs
297304
modelOutputs:(NSArray<MLMultiArray *> *_Nullable __autoreleasing *_Nonnull)modelOutputs
298305
error:(NSError* __autoreleasing *)error {
306+
#if MODEL_PROFILING_IS_AVAILABLE
299307
uint64_t modelExecutionStartTime = mach_absolute_time();
300308
id<MLFeatureProvider> outputFeatures = [self.model predictionFromFeatures:inputs options:options error:error];
301309
uint64_t modelExecutionEndTime = mach_absolute_time();
@@ -319,14 +327,15 @@ - (nullable ETCoreMLModelProfilingResult *)profilingInfoForOperationsAtPaths:(NS
319327

320328
return profilingInfos;
321329
}
322-
330+
#endif
323331
return nil;
324332
}
325333

326334
- (nullable ETCoreMLModelProfilingResult *)profilingInfoForAllOperationsWithOptions:(MLPredictionOptions *)options
327335
inputs:(id<MLFeatureProvider>)inputs
328336
modelOutputs:(NSArray<MLMultiArray *> *_Nullable __autoreleasing *_Nonnull)modelOutputs
329337
error:(NSError* __autoreleasing *)error {
338+
#if MODEL_PROFILING_IS_AVAILABLE
330339
if (@available(macOS 14.4, iOS 17.4, tvOS 17.4, watchOS 10.4, *)) {
331340
__block NSMutableArray<ETCoreMLModelStructurePath *> *paths = [NSMutableArray array];
332341
visit_program_operation(self.computePlan.modelStructure, ^BOOL(MLModelStructureProgramOperation *operation, ETCoreMLModelStructurePath *path) {
@@ -342,7 +351,7 @@ - (nullable ETCoreMLModelProfilingResult *)profilingInfoForAllOperationsWithOpti
342351
modelOutputs:modelOutputs
343352
error:error];
344353
}
345-
354+
#endif
346355
return nil;
347356
}
348357

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import <XCTest/XCTest.h>
1010
#import <ETCoreMLModelAnalyzer.h>
11+
#import <ETCoreMLModelProfiler.h>
1112
#import <ETCoreMLOperationProfilingInfo.h>
1213
#import <ETCoreMLModelStructurePath.h>
1314
#import <model_logging_options.h>
@@ -97,7 +98,8 @@ - (void)profileModelWithName:(NSString *)modelName
9798
}
9899

99100
- (void)testAddProgramProfiling {
100-
if (@available(macOS 14.4, iOS 17.4, tvOS 17.4, watchOS 10.4, *)) {
101+
#if MODEL_PROFILING_IS_AVAILABLE
102+
if (@available(macOS 14.4, iOS 17.4, watchOS 10.4, tvOS 17.4, *)) {
101103
NotifyFn notify = [](NSDictionary<ETCoreMLModelStructurePath *, ETCoreMLOperationProfilingInfo *> *profilingResult,
102104
NSDictionary<ETCoreMLModelStructurePath *, NSString *> *__unused pathToSymbolNameMap) {
103105
// There are 3 add ops, we verify that the profiling info exists for the ops.
@@ -110,9 +112,13 @@ - (void)testAddProgramProfiling {
110112
repeatedInputValues:@[@(1), @(2)]
111113
notify:notify];
112114
}
115+
#else
116+
XCTSkip(@"Model profiling is only available on macOS 14.4, iOS 17.4, tvOS 17.4, and watchOS 10.4.");
117+
#endif
113118
}
114119

115120
- (void)testMulProgramProfiling {
121+
#if MODEL_PROFILING_IS_AVAILABLE
116122
if (@available(macOS 14.4, iOS 17.4, tvOS 17.4, watchOS 10.4, *)) {
117123
NotifyFn notify = [](NSDictionary<ETCoreMLModelStructurePath *, ETCoreMLOperationProfilingInfo *> *profilingResult,
118124
NSDictionary<ETCoreMLModelStructurePath *, NSString *> *__unused pathToSymbolNameMap) {
@@ -124,9 +130,13 @@ - (void)testMulProgramProfiling {
124130
repeatedInputValues:@[@(1), @(2)]
125131
notify:notify];
126132
}
133+
#else
134+
XCTSkip(@"Model profiling is only available on macOS 14.4, iOS 17.4, tvOS 17.4, and watchOS 10.4.");
135+
#endif
127136
}
128137

129138
- (void)testMV3ProgramProfiling {
139+
#if MODEL_PROFILING_IS_AVAILABLE
130140
if (@available(macOS 14.4, iOS 17.4, tvOS 17.4, watchOS 10.4, *)) {
131141
NotifyFn notify = [](NSDictionary<ETCoreMLModelStructurePath *, ETCoreMLOperationProfilingInfo *> *profilingResult,
132142
NSDictionary<ETCoreMLModelStructurePath *, NSString *> *__unused pathToSymbolNameMap) {
@@ -143,6 +153,9 @@ - (void)testMV3ProgramProfiling {
143153
repeatedInputValues:@[@(1), @(2)]
144154
notify:notify];
145155
}
156+
#else
157+
XCTSkip(@"Model profiling is only available on macOS 14.4, iOS 17.4, tvOS 17.4, and watchOS 10.4.");
158+
#endif
146159
}
147160

148161
@end

0 commit comments

Comments
 (0)