From ddf2e8a6ae9b6ef1cb7aba737b24d6c18bbb6c58 Mon Sep 17 00:00:00 2001 From: pacowong Date: Thu, 23 Jan 2025 14:44:04 +0800 Subject: [PATCH 1/8] Build for WASM --- .circleci/config.yml | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index 788a83e26aa46..e601bfc6f3600 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -66,8 +66,44 @@ jobs: - store_artifacts: path: /Users/distiller/project/onnxruntime_xcframework.zip destination: onnxruntime_xcframework.zip + + build-and-test-wasm: + macos: + xcode: << pipeline.parameters.xcode-version >> + resource_class: << pipeline.parameters.macos-resource-class >> + + shell: /bin/bash --login -o pipefail + + steps: + - run: + name: check Xcode version and Python 3 + command: | + /usr/bin/xcodebuild -version + python3 --version + python --version + which python3 + echo $CIRCLE_WORKING_DIRECTORY + - checkout + - run: + name: create directory to store the library + command: | + mkdir -p $HOME/onnxlibrary/macabi_release_v20230327_2320 + - run: + name: install tools for build + command: | + brew install cmake + pip install -r ${CIRCLE_WORKING_DIRECTORY}/requirements-dev.txt + - run: + name: build static wasm library + command: | + export PYTHONPATH=${CIRCLE_WORKING_DIRECTORY}/tools/python:$PYTHONPATH + bash ${CIRCLE_WORKING_DIRECTORY}/build.sh --build_wasm_static_lib --enable_wasm_simd --enable_wasm_threads + workflows: version: 2 build-and-test-xcframework: jobs: - build-and-test-xcframework + build-and-test-wasm: + jobs: + - build-and-test-wasm From 4338da279240aadc5ae7f8ef9b091caef4bb3031 Mon Sep 17 00:00:00 2001 From: Paco Wong Date: Sun, 2 Feb 2025 20:50:21 +0800 Subject: [PATCH 2/8] Create thread pool --- onnxruntime/test/wasm/test_inference.cc | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/onnxruntime/test/wasm/test_inference.cc b/onnxruntime/test/wasm/test_inference.cc index 0153770e138fb..5db3062786e71 100644 --- a/onnxruntime/test/wasm/test_inference.cc +++ b/onnxruntime/test/wasm/test_inference.cc @@ -6,7 +6,22 @@ #include "core/session/onnxruntime_cxx_api.h" TEST(WebAssemblyTest, test) { - Ort::Env ort_env; + auto numIntraOpsThreads = 10; + // OrtThreadingOptions *threadingOptions; + // Ort::SessionOptions sessionOptions; + // sessionOptions.DisablePerSessionThreads(); + // Ort::Env ort_env(threadingOptions, ORT_LOGGING_LEVEL_WARNING, "test"); + + OrtEnv* environment; + OrtThreadingOptions* envOpts = nullptr; + Ort::GetApi().CreateThreadingOptions(&envOpts); + Ort::GetApi().SetGlobalIntraOpNumThreads(envOpts, numIntraOpsThreads); + Ort::GetApi().SetGlobalInterOpNumThreads(envOpts, numInterOpsThreads); + Ort::GetApi().SetGlobalSpinControl(envOpts, 1); + Ort::GetApi().CreateEnvWithGlobalThreadPools(ORT_LOGGING_LEVEL_WARNING, "test", envOpts, &environment); + env = Ort::Env(environment); + + // Ort::Env ort_env; Ort::Session session{ort_env, "testdata/mul_1.onnx", Ort::SessionOptions{nullptr}}; auto memory_info = Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU); @@ -39,4 +54,4 @@ TEST(WebAssemblyTest, test) { for (size_t i = 0; i != total_len; ++i) { ASSERT_EQ(expected_data[i], result[i]); } -} \ No newline at end of file +} From dd6bc30ad02fe4d0cdcf2588f8dbb51048e652b6 Mon Sep 17 00:00:00 2001 From: pacowong Date: Sun, 2 Feb 2025 21:59:20 +0800 Subject: [PATCH 3/8] Update test_inference.cc --- onnxruntime/test/wasm/test_inference.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onnxruntime/test/wasm/test_inference.cc b/onnxruntime/test/wasm/test_inference.cc index 5db3062786e71..dabe66e1fb47a 100644 --- a/onnxruntime/test/wasm/test_inference.cc +++ b/onnxruntime/test/wasm/test_inference.cc @@ -19,7 +19,7 @@ TEST(WebAssemblyTest, test) { Ort::GetApi().SetGlobalInterOpNumThreads(envOpts, numInterOpsThreads); Ort::GetApi().SetGlobalSpinControl(envOpts, 1); Ort::GetApi().CreateEnvWithGlobalThreadPools(ORT_LOGGING_LEVEL_WARNING, "test", envOpts, &environment); - env = Ort::Env(environment); + Ort::Env ort_env = Ort::Env(environment); // Ort::Env ort_env; Ort::Session session{ort_env, "testdata/mul_1.onnx", Ort::SessionOptions{nullptr}}; From bcf186e9d8a084f6224a091ca468ed1090777e89 Mon Sep 17 00:00:00 2001 From: pacowong Date: Sun, 2 Feb 2025 23:15:24 +0800 Subject: [PATCH 4/8] Update test_inference.cc --- onnxruntime/test/wasm/test_inference.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/onnxruntime/test/wasm/test_inference.cc b/onnxruntime/test/wasm/test_inference.cc index dabe66e1fb47a..436275934467a 100644 --- a/onnxruntime/test/wasm/test_inference.cc +++ b/onnxruntime/test/wasm/test_inference.cc @@ -6,7 +6,8 @@ #include "core/session/onnxruntime_cxx_api.h" TEST(WebAssemblyTest, test) { - auto numIntraOpsThreads = 10; + auto numInterOpsThreads = 4; + auto numIntraOpsThreads = 4; // OrtThreadingOptions *threadingOptions; // Ort::SessionOptions sessionOptions; // sessionOptions.DisablePerSessionThreads(); From c8be7deda15dfb4d87da798f0a5f872f5daed5c2 Mon Sep 17 00:00:00 2001 From: pacowong Date: Mon, 3 Feb 2025 09:30:30 +0800 Subject: [PATCH 5/8] Disable MLOptimizationHints --- onnxruntime/core/providers/coreml/model/model.mm | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/onnxruntime/core/providers/coreml/model/model.mm b/onnxruntime/core/providers/coreml/model/model.mm index 5211b89ec17c6..b0db82099d54f 100644 --- a/onnxruntime/core/providers/coreml/model/model.mm +++ b/onnxruntime/core/providers/coreml/model/model.mm @@ -547,10 +547,12 @@ Status Predict(const std::unordered_map& inputs, } // Set the specialization strategy to FastPrediction for macOS 10.15+ - if (HAS_COREML8_OR_LATER) { - ConfigureOptimizationHints(config, coreml_options_); - } else { - LOGS(logger_, WARNING) << "iOS 17.4+/macOS 14.4+ or later is required to ConfigureOptimizationHints"; + if (@available(macOS 14.4, iOS 17.4, *)) { + if (HAS_COREML8_OR_LATER) { + ConfigureOptimizationHints(config, coreml_options_); + } else { + LOGS(logger_, WARNING) << "iOS 17.4+/macOS 14.4+ or later is required to ConfigureOptimizationHints"; + } } if (coreml_options_.ProfileComputePlan()) { From 759eb6c2a8c54d501eeec465fdf2702cf25879ea Mon Sep 17 00:00:00 2001 From: pacowong Date: Mon, 3 Feb 2025 20:09:10 +0800 Subject: [PATCH 6/8] Update model.mm --- onnxruntime/core/providers/coreml/model/model.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onnxruntime/core/providers/coreml/model/model.mm b/onnxruntime/core/providers/coreml/model/model.mm index b0db82099d54f..8357f8a20cf33 100644 --- a/onnxruntime/core/providers/coreml/model/model.mm +++ b/onnxruntime/core/providers/coreml/model/model.mm @@ -369,7 +369,7 @@ void ProfileComputePlan(NSURL* compileUrl, MLModelConfiguration* config) { #define HAS_COREMLOPTIMIZATIONHINT 0 #endif -API_AVAILABLE_COREML8 +API_AVAILABLE(macos(14.4), ios(17.4)) void ConfigureOptimizationHints(MLModelConfiguration* config, const CoreMLOptions& coreml_options) { #if HAS_COREMLOPTIMIZATIONHINT MLOptimizationHints* optimizationHints = [[MLOptimizationHints alloc] init]; From 15d3ef480ec46e4af26b7684dd4211145f7ad211 Mon Sep 17 00:00:00 2001 From: pacowong Date: Mon, 3 Feb 2025 23:15:52 +0800 Subject: [PATCH 7/8] Update model.mm --- onnxruntime/core/providers/coreml/model/model.mm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/onnxruntime/core/providers/coreml/model/model.mm b/onnxruntime/core/providers/coreml/model/model.mm index 8357f8a20cf33..9e8add4ef9382 100644 --- a/onnxruntime/core/providers/coreml/model/model.mm +++ b/onnxruntime/core/providers/coreml/model/model.mm @@ -371,6 +371,7 @@ void ProfileComputePlan(NSURL* compileUrl, MLModelConfiguration* config) { API_AVAILABLE(macos(14.4), ios(17.4)) void ConfigureOptimizationHints(MLModelConfiguration* config, const CoreMLOptions& coreml_options) { + if (@available(macOS 14.4, iOS 17.4, *)) { #if HAS_COREMLOPTIMIZATIONHINT MLOptimizationHints* optimizationHints = [[MLOptimizationHints alloc] init]; if (coreml_options.UseStrategy("FastPrediction")) { @@ -383,6 +384,7 @@ void ConfigureOptimizationHints(MLModelConfiguration* config, const CoreMLOption // not set } #endif + } } Status CompileOrReadCachedModel(NSURL* modelUrl, const CoreMLOptions& coreml_options, From 1b9367f8e94a8c9ae97d0db9f1c9c77c15884ee2 Mon Sep 17 00:00:00 2001 From: pacowong Date: Tue, 4 Feb 2025 09:23:47 +0800 Subject: [PATCH 8/8] Update model.mm --- onnxruntime/core/providers/coreml/model/model.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onnxruntime/core/providers/coreml/model/model.mm b/onnxruntime/core/providers/coreml/model/model.mm index 9e8add4ef9382..14eb844670b51 100644 --- a/onnxruntime/core/providers/coreml/model/model.mm +++ b/onnxruntime/core/providers/coreml/model/model.mm @@ -369,7 +369,7 @@ void ProfileComputePlan(NSURL* compileUrl, MLModelConfiguration* config) { #define HAS_COREMLOPTIMIZATIONHINT 0 #endif -API_AVAILABLE(macos(14.4), ios(17.4)) +API_AVAILABLE(macos(14.4), ios(17.4), tvos(17.4), watchos(10.4)) void ConfigureOptimizationHints(MLModelConfiguration* config, const CoreMLOptions& coreml_options) { if (@available(macOS 14.4, iOS 17.4, *)) { #if HAS_COREMLOPTIMIZATIONHINT