Skip to content

Commit

Permalink
t
Browse files Browse the repository at this point in the history
  • Loading branch information
wejoncy committed Dec 10, 2024
1 parent 692f149 commit d539da2
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions onnxruntime/core/providers/coreml/model/model.mm
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ Status GetMLMultiArrayCopyInfo(const MLMultiArray* _Nonnull array,

// since macos(14.4), ios(17.4), MLComputePlan is introduced in <CoreML/CoreML.h>
// Otherwise, the compiler will complain `MLComputePlan` is not defined.
// we define __clang_analyzer__ here is for bypass static analysis
#if __has_include(<CoreML/MLComputePlan.h>)
#define HAS_COREMLPLAN 1
#else
Expand Down Expand Up @@ -331,7 +330,6 @@ void ProfileBlock(MLComputePlan* _Nullable computePlan, MLModelStructureProgramB

// since macos(14.4), ios(17.4), MLComputePlan is introduced in <CoreML/CoreML.h>
// Otherwise, the compiler will complain `MLComputePlan` is not defined.
// we define __clang_analyzer__ here is for bypass static analysis
API_AVAILABLE(macos(14.4), ios(17.4), tvos(17.4), watchos(10.4))
void ProfileComputePlan(NSURL* compileUrl, MLModelConfiguration* config) {
#if HAS_COREMLPLAN
Expand Down Expand Up @@ -365,9 +363,15 @@ void ProfileComputePlan(NSURL* compileUrl, MLModelConfiguration* config) {
#endif
}

#if __has_include(<CoreML/MLOptimizationHints.h>)
#define HAS_COREMLOPTIMIZATIONHINT 1
#else
#define HAS_COREMLOPTIMIZATIONHINT 0
#endif

API_AVAILABLE_COREML8
void ConfigureOptimizationHints(MLModelConfiguration* config, const CoreMLOptions& coreml_options) {
#if !defined(__clang_analyzer__)
#if HAS_COREMLOPTIMIZATIONHINT
MLOptimizationHints* optimizationHints = [[MLOptimizationHints alloc] init];
if (coreml_options.UseStrategy("FastPrediction")) {
optimizationHints.specializationStrategy = MLSpecializationStrategyFastPrediction;
Expand All @@ -388,7 +392,7 @@ Status CompileOrReadCachedModel(NSURL* modelUrl, const CoreMLOptions& coreml_opt
cached_model_base_url = [cached_model_base_url URLByDeletingLastPathComponent];
}

if ([cached_model_base_url path] == nil) {
if (cached_model_base_url == nil) {
return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Error creating cached model URL");
}

Expand All @@ -397,12 +401,12 @@ Status CompileOrReadCachedModel(NSURL* modelUrl, const CoreMLOptions& coreml_opt
// if cached_model_url is existed, just return
NSError* error = nil;
NSString* cached_model_path = [cached_model_url path];
NS_ASSUME_NONNULL_BEGIN

if ([[NSFileManager defaultManager] fileExistsAtPath:cached_model_path]) {
[compiled_model_path appendString:cached_model_path];
return Status::OK();
}
NS_ASSUME_NONNULL_END

// TODO: Update this to version with callback handler as the API used here is deprecated.
// https://developer.apple.com/documentation/coreml/mlmodel/3929553-compilemodelaturl
// As we call loadModel during EP Compile there shouldn't be an issue letting the actual compile run in the
Expand Down Expand Up @@ -535,11 +539,8 @@ Status Predict(const std::unordered_map<std::string, OnnxTensorData>& inputs,
}

// Set the specialization strategy to FastPrediction for macOS 10.15+
// we define __clang_analyzer__ here is for bypass static analysis
if (HAS_COREML8_OR_LATER) {
#if !defined(__clang_analyzer__)
ConfigureOptimizationHints(config, coreml_options_);
#endif
} else {
LOGS(logger_, WARNING) << "iOS 17.4+/macOS 14.4+ or later is required to ConfigureOptimizationHints";
}
Expand Down

0 comments on commit d539da2

Please sign in to comment.