Skip to content

Commit acb51ec

Browse files
author
Gin
committed
Add ONNX Runtime with macOS CoreML support
1 parent db1803c commit acb51ec

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

SerialPrograms/CMakeLists.txt

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ set(CMAKE_AUTOUIC ON)
1919
add_custom_target(build-time-make-directory ALL
2020
COMMAND ${CMAKE_COMMAND} -E make_directory Assembly/)
2121

22+
# Find repo root folder path
23+
string(FIND "${CMAKE_CURRENT_SOURCE_DIR}" "Arduino-Source-Internal" internal_repro_position) # determine if this is an internal repo
24+
if (internal_repro_position EQUAL -1) # no "Arduino-Source-Internal" substr in the current source dir
25+
# we are building the public repo
26+
set(REPO_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../")
27+
else()
28+
# we are building the internal repo
29+
set(REPO_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../")
30+
endif()
31+
2232
#Find threads library
2333
set(THREADS_PREFER_PTHREAD_FLAG ON)
2434
find_package(Threads REQUIRED)
@@ -2402,14 +2412,7 @@ if (APPLE)
24022412

24032413
# make sure Packages repo, https://github.com/PokemonAutomation/Packages is placed in the same folder as this Arduino-Source repo
24042414
# so the post-build command can copy the resources folder Packages/SerialPrograms/Resources/ into the built app bundle:
2405-
string(FIND "${CMAKE_CURRENT_SOURCE_DIR}" "Arduino-Source-Internal" internal_repro_position) # determine if this is an internal repo
2406-
if (internal_repro_position EQUAL -1) # no "Arduino-Source-Internal" substr in the current source dir
2407-
# we are building the public repo
2408-
set(RESOURCES_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../Packages/SerialPrograms/Resources")
2409-
else()
2410-
# we are building the internal repo
2411-
set(RESOURCES_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../../Packages/SerialPrograms/Resources")
2412-
endif()
2415+
set(RESOURCES_PATH "${REPO_ROOT_DIR}../Packages/SerialPrograms/Resources")
24132416
message(STATUS "Set Resources folder path from Packages repo: ${RESOURCES_PATH}")
24142417
add_custom_command(
24152418
TARGET SerialPrograms
@@ -2551,9 +2554,25 @@ else() # macOS and Linux
25512554
# Save the root path as CMake variable HOMEBREW_PREFIX.
25522555
execute_process(COMMAND brew --prefix OUTPUT_VARIABLE HOMEBREW_PREFIX OUTPUT_STRIP_TRAILING_WHITESPACE)
25532556

2554-
# Get ONNX Runtime paths
2555-
target_include_directories(SerialPrograms PRIVATE ${HOMEBREW_PREFIX}/include/onnxruntime)
2556-
target_link_libraries(SerialPrograms PRIVATE ${HOMEBREW_PREFIX}/lib/libonnxruntime.dylib)
2557+
# ==== Get ONNX Runtime paths ====
2558+
# First, try to see if there is an ONNX Runtime library built from source.
2559+
# Assume the onnxruntime repo, https://github.com/microsoft/onnxruntime is placed in the same folder as
2560+
# this Arduino-Source repo:
2561+
set(ONNXRUNTIME_ROOTDIR "${REPO_ROOT_DIR}../onnxruntime/")
2562+
set(ONNXRUNTIME_HEADER_DIR "${ONNXRUNTIME_ROOTDIR}include/onnxruntime/core/session/")
2563+
if(EXISTS "${ONNXRUNTIME_HEADER_DIR}")
2564+
# we have ONNX Runtime built from source
2565+
message("Use ONNX Runtime built from source at ${ONNXRUNTIME_ROOTDIR}")
2566+
target_include_directories(SerialPrograms PRIVATE ${ONNXRUNTIME_HEADER_DIR})
2567+
link_directories(SerialPrograms PRIVATE ${ONNXRUNTIME_ROOTDIR}/build/MacOS/Release)
2568+
target_link_libraries(SerialPrograms PRIVATE ${ONNXRUNTIME_ROOTDIR}/build/MacOS/Release/libonnxruntime.dylib)
2569+
else()
2570+
message("Built-from-source ONNX Runtime folder ${ONNXRUNTIME_ROOTDIR} does not exist.")
2571+
message("Use ONNX Runtime installed by Homebrew.")
2572+
target_include_directories(SerialPrograms PRIVATE ${HOMEBREW_PREFIX}/include/onnxruntime)
2573+
target_link_libraries(SerialPrograms PRIVATE ${HOMEBREW_PREFIX}/lib/libonnxruntime.dylib)
2574+
endif()
2575+
25572576
else() # Linux
25582577
# Instructions took from https://github.com/microsoft/onnxruntime/discussions/6489
25592578
# Step 1: Download the file

SerialPrograms/Source/ML/Models/ML_ONNXRuntimeHelpers.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@ namespace PokemonAutomation{
1313
namespace ML{
1414

1515
Ort::SessionOptions create_session_option(){
16-
return Ort::SessionOptions{};
16+
Ort::SessionOptions so;
1717

18-
// create session using Apple ML
18+
#if __APPLE__
19+
// create session using Apple ML acceleration library CoreML
20+
std::unordered_map<std::string, std::string> provider_options;
21+
// See for provider options: https://onnxruntime.ai/docs/execution-providers/CoreML-ExecutionProvider.html
22+
// "NeuralNetwork" is a faster ModelFormat than "MLProgram".
23+
provider_options["ModelFormat"] = std::string("NeuralNetwork");
24+
provider_options["ModelCacheDirectory"] = "./ModelCache/";
25+
// provider_options["MLComputeUnits"] = "ALL";
26+
// provider_options["RequireStaticInputShapes"] = "0";
27+
// provider_options["EnableOnSubgraphs"] = "0";
28+
so.AppendExecutionProvider("CoreML", provider_options);
29+
#endif
1930

20-
// Ort::SessionOptions so;
21-
// std::unordered_map<std::string, std::string> provider_options;
22-
// provider_options["ModelFormat"] = "NeuralNetwork";
23-
// so.AppendExecutionProvider("CoreML", provider_options);
24-
// return so;
31+
// use CPU session
32+
return so;
2533
}
2634

2735

SerialPrograms/Source/ML/Models/ML_ONNXRuntimeHelpers.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ namespace PokemonAutomation{
1717
namespace ML{
1818

1919

20-
// Create an ONNX Runtiem session options object.
21-
// For now it only sets the session to be CPU. In future we can create options for GPU or macOS MPS
20+
// Create an ONNX Runtime session options object.
21+
// If on macOS, will use CoreML as the backend.
22+
// Otherwise, use CPU to run the model.
23+
// TODO: add Cuda backend for Windows machine.
2224
Ort::SessionOptions create_session_option();
2325

2426
// Handy function to create an ONNX Runtime tensor view class from a vector-like `buffer` object holding

0 commit comments

Comments
 (0)