Skip to content

Commit

Permalink
Enable MacOS CI (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchen351 authored Mar 13, 2024
1 parent 7eb8c3c commit f12ca83
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 33 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/mac-cpu-arm64-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ jobs:
run: |
- name: Build with CMake
run: |
cmake -G "Ninja" -B build -S . -DCMAKE_BUILD_TYPE=Release -DUSE_CUDA=OFF
cmake --build build --config Release --parallel
continue-on-error: true
cmake --preset macos_cpu_release
cmake --build --preset macos_cpu_release --parallel
continue-on-error: false

- name: Verify Build Artifacts
if: always()
Expand Down
20 changes: 2 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,7 @@ if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_C_COMPILER_VERSION VERSION_
message(FATAL_ERROR "GCC version must be greater than or equal to 8")
endif()

if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_C_COMPILER_VERSION VERSION_LESS 10)
add_compile_definitions(USE_CXX17=1)
message("USE C++17")
set(CMAKE_CXX_STANDARD 17)
elseif(USE_CUDA AND CMAKE_CUDA_COMPILER AND CMAKE_CUDA_COMPILER_VERSION VERSION_LESS 12)
add_compile_definitions(USE_CXX17=1)
message("USE C++17 Because of CUDA Version is less than 12")
set(CMAKE_CXX_STANDARD 17)
else()
message("USE C++20")
set(CMAKE_CXX_STANDARD 20)
endif()
include(cmake/cxx_standard.cmake)

set(GENERATORS_ROOT ${PROJECT_SOURCE_DIR}/src)
set(MODELS_ROOT ${PROJECT_SOURCE_DIR}/src/models)
Expand Down Expand Up @@ -139,11 +128,6 @@ else()
target_include_directories(onnxruntime-genai-static PUBLIC ${TOKENIZER_ROOT})
target_link_libraries(onnxruntime-genai PRIVATE tokenizer)
target_link_libraries(onnxruntime-genai-static PUBLIC tokenizer)
# if(NOT WIN32 AND USE_CXX17)
# target_link_libraries(onnxruntime-genai PRIVATE stdc++fs)
# target_link_libraries(onnxruntime-genai-static PRIVATE stdc++fs)
# message("Linking filesystem")
# endif()
endif()

if(ENABLE_TESTS)
Expand Down Expand Up @@ -174,7 +158,7 @@ if(USE_CUDA AND CMAKE_CUDA_COMPILER)
set_target_properties(onnxruntime-genai PROPERTIES LINKER_LANGUAGE CUDA)
target_link_libraries(onnxruntime-genai PRIVATE cublasLt cublas curand cufft cudart)
# onnxruntime-genai-static is statically linked under Windows
if(NOT WIN32)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set_target_properties(onnxruntime-genai-static PROPERTIES LINKER_LANGUAGE CUDA)
target_link_libraries(onnxruntime-genai-static PRIVATE cublasLt cublas curand cufft cudart)
endif()
Expand Down
4 changes: 3 additions & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
},
"include": [
"cmake/presets/CMakeWinBuildPresets.json",
"cmake/presets/CMakeLinuxBuildPresets.json"
"cmake/presets/CMakeLinuxBuildPresets.json",
"cmake/presets/CMakeMacOSBuildPresets.json"

]

}
12 changes: 12 additions & 0 deletions cmake/cxx_standard.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_C_COMPILER_VERSION VERSION_LESS 10)
add_compile_definitions(USE_CXX17=1)
message("Test is using C++17 because GCC Version is less than 10")
set(CMAKE_CXX_STANDARD 17)
elseif (USE_CUDA AND CMAKE_CUDA_COMPILER AND CMAKE_CUDA_COMPILER_VERSION VERSION_LESS 12)
add_compile_definitions(USE_CXX17=1)
message("Test is using C++17 Because CUDA Version is less than 12")
set(CMAKE_CXX_STANDARD 17)
else ()
message("Test is using C++20")
set(CMAKE_CXX_STANDARD 20)
endif ()
24 changes: 24 additions & 0 deletions cmake/presets/CMakeMacOSBuildPresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"version": 6,
"include": [
"CMakeMacOSConfigPresets.json"
],
"buildPresets": [
{
"name": "macos_cpu_release",
"configurePreset": "macos_cpu_release"
},
{
"name": "macos_cpu_debug",
"configurePreset": "macos_cpu_debug"
},
{
"name": "macos_cpu_relwithdebinfo",
"configurePreset": "macos_cpu_relwithdebinfo"
},
{
"name": "macos_cpu_minsizerel",
"configurePreset": "macos_cpu_minsizerel"
}
]
}
91 changes: 91 additions & 0 deletions cmake/presets/CMakeMacOSConfigPresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"version": 6,
"include": [
"CMakeLinuxDefaultConfigPresets.json"
],
"configurePresets": [
{
"name": "macos_default",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build/cpu",
"cacheVariables": {
"CMAKE_POSITION_INDEPENDENT_CODE": "ON",
"CMAKE_OSX_ARCHITECTURES": "arm64",
"USE_CUDA": "OFF"
},
"environment": {
"CC": "clang",
"CXX": "clang++"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Darwin"
}
},
{
"name": "macos_release_default",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_C_FLAGS": " -O3 -pipe",
"CMAKE_CXX_FLAGS": " -O3 -pipe"
}
},
{
"name": "macos_relwithdebinfo_default",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
"CMAKE_C_FLAGS": "-O3 -pipe -ggdb3",
"CMAKE_CXX_FLAGS": "-O3 -pipe -ggdb3"
}
},
{
"name": "macos_debug_default",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_C_FLAGS": "-ggdb3 -O0",
"CMAKE_CXX_FLAGS": "-ggdb3 -O0 -D_GLIBCXX_DEBUG"
}
},
{
"name": "macos_minsizerel_default",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "MinSizeRel",
"CMAKE_C_FLAGS": "-Os -pipe -ggdb3",
"CMAKE_CXX_FLAGS": "-Os -pipe -ggdb3"
}
},
{
"name": "macos_cpu_release",
"displayName": "macos cpu release",
"inherits": [
"macos_default",
"macos_release_default"
]
},
{
"name": "macos_cpu_debug",
"displayName": "macos cpu debug",
"inherits": [
"macos_default",
"macos_debug_default"
]
},
{
"name": "macos_cpu_relwithdebinfo",
"displayName": "macos cpu relwithdebinfo",
"inherits": [
"macos_default",
"macos_relwithdebinfo_default"
]
},
{
"name": "macos_cpu_minsizerel",
"displayName": "macos cpu minsizerel",
"inherits": [
"macos_default",
"macos_minsizerel_default"
]
}
]
}
2 changes: 1 addition & 1 deletion src/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void JSON::Parse_Array(Element& element) {

double JSON::Parse_Number() {
double value = NAN;
#ifndef USE_CXX17
#if !defined(USE_CXX17) && !defined(__APPLE__)
auto result = std::from_chars(current_, end_, value);
if (result.ec != std::errc{}) {
throw std::runtime_error("Expecting number");
Expand Down
7 changes: 7 additions & 0 deletions src/models/onnxruntime_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,14 @@ inline std::unique_ptr<Allocator> Allocator::Create(const OrtSession& sess, cons
}

inline void SetCurrentGpuDeviceId(int device_id) {
#ifdef __APPLE__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-result"
#endif
api->SetCurrentGpuDeviceId(device_id);
#ifdef __APPLE__
#pragma clang diagnostic pop
#endif
}

inline int GetCurrentGpuDeviceId() {
Expand Down
7 changes: 2 additions & 5 deletions src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ pybind11_add_module(python ${python_srcs})
target_include_directories(python PRIVATE ${ORT_HEADER_DIR})
target_link_directories(python PRIVATE ${ORT_LIB_DIR})
target_link_libraries(python PRIVATE onnxruntime-genai-static ${ONNXRUNTIME_LIB})
if(NOT WIN32)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set_property(TARGET python APPEND_STRING PROPERTY LINK_FLAGS " -Xlinker -rpath=\\$ORIGIN")
target_link_libraries(python PRIVATE stdc++fs)
endif()
set_target_properties(python PROPERTIES OUTPUT_NAME "onnxruntime_genai")

Expand All @@ -29,10 +30,6 @@ if(CMAKE_GENERATOR_TOOLSET MATCHES "Visual Studio")
target_compile_options(python PRIVATE "/sdl" PRIVATE "/Qspectre")
endif()

if(NOT WIN32)
target_link_libraries(python PRIVATE stdc++fs)
endif()

if(USE_CUDA AND CMAKE_CUDA_COMPILER)
cmake_policy(SET CMP0104 OLD)
enable_language(CUDA)
Expand Down
5 changes: 5 additions & 0 deletions src/python/python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ struct PyGeneratorParams : GeneratorParams {

if (py_whisper_input_features_.size() != 0) {
GeneratorParams::Whisper& whisper = inputs.emplace<GeneratorParams::Whisper>();
#ifdef __APPLE__
std::span shape(reinterpret_cast<const int64_t*>(py_whisper_input_features_.shape()),
py_whisper_input_features_.ndim());
#else
std::span<const int64_t> shape(py_whisper_input_features_.shape(), py_whisper_input_features_.ndim());
#endif
whisper.input_features = OrtValue::CreateTensor<float>(Ort::Allocator::GetWithDefaultOptions().GetInfo(), ToSpan(py_whisper_input_features_), shape);
whisper.decoder_input_ids = ToSpan(py_whisper_decoder_input_ids_);
batch_size = 1;
Expand Down
5 changes: 1 addition & 4 deletions src/tokenizer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ FetchContent_MakeAvailable(simdjson)

add_library(tokenizer STATIC ${tokenizer_srcs})

if(NOT WIN32)
target_link_libraries(tokenizer PRIVATE stdc++fs)
endif()

message(STATUS "GSL_SOURCE_DIR: ${GSL_SOURCE_DIR}")
target_include_directories(tokenizer PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
Expand All @@ -40,6 +36,7 @@ target_include_directories(tokenizer PUBLIC
target_compile_definitions(tokenizer PRIVATE _SILENCE_ALL_CXX20_DEPRECATION_WARNINGS)

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_link_libraries(tokenizer PRIVATE stdc++fs)
set_target_properties(tokenizer PROPERTIES POSITION_INDEPENDENT_CODE ON)
set_target_properties(simdjson PROPERTIES POSITION_INDEPENDENT_CODE ON)
endif()
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ target_link_libraries(unit_tests PRIVATE
GTest::gtest_main
)

if(NOT WIN32)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_link_libraries(unit_tests PRIVATE stdc++fs)
endif()

Expand Down

0 comments on commit f12ca83

Please sign in to comment.