Skip to content

Commit

Permalink
Add MacOS COREML support for vsort (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuygfgg authored Sep 7, 2024
1 parent 28325f3 commit d6fa596
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 1 deletion.
139 changes: 139 additions & 0 deletions .github/workflows/macos-ort.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
name: Build (macOS-ORT)

on:
push:
paths:
- 'common/**'
- 'vsort/**'
- '.github/workflows/macos-ort.yml'
workflow_dispatch:

jobs:
build-macos:
runs-on: macos-latest

defaults:
run:
working-directory: vsort

steps:
- name: Checkout repo
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Ninja
run: brew install ninja

- name: Cache protobuf
id: cache-protobuf
uses: actions/cache@v4
with:
path: vsort/protobuf/install
key: ${{ runner.os }}-vsort-protobuf-v1

- name: Checkout protobuf
uses: actions/checkout@v4
if: steps.cache-protobuf.outputs.cache-hit != 'true'
with:
repository: protocolbuffers/protobuf
ref: v3.21.12
fetch-depth: 1
path: vsort/protobuf

- name: Configure protobuf
if: steps.cache-protobuf.outputs.cache-hit != 'true'
run: cmake -S protobuf/cmake -B protobuf/build_rel -G Ninja -LA
-D CMAKE_BUILD_TYPE=Release
-D CMAKE_POSITION_INDEPENDENT_CODE=ON
-D protobuf_BUILD_SHARED_LIBS=OFF
-D protobuf_BUILD_TESTS=OFF

- name: Build protobuf
if: steps.cache-protobuf.outputs.cache-hit != 'true'
run: cmake --build protobuf/build_rel --verbose

- name: Install protobuf
if: steps.cache-protobuf.outputs.cache-hit != 'true'
run: cmake --install protobuf/build_rel --prefix protobuf/install

- name: Cache onnx
id: cache-onnx
uses: actions/cache@v4
with:
path: vsort/onnx/install
key: ${{ runner.os }}-vsort-onnx-v1

- name: Checkout onnx
if: steps.cache-onnx.outputs.cache-hit != 'true'
uses: actions/checkout@v4
with:
repository: onnx/onnx
ref: b86cc54efce19530fb953e4b21f57e6b3888534c
fetch-depth: 1
path: vsort/onnx

- name: Configure onnx
if: steps.cache-onnx.outputs.cache-hit != 'true'
run: cmake -S onnx -B onnx/build -G Ninja -LA
-D CMAKE_BUILD_TYPE=Release
-D CMAKE_POSITION_INDEPENDENT_CODE=ON
-D Protobuf_PROTOC_EXECUTABLE=protobuf/install/bin/protoc
-D Protobuf_LITE_LIBRARY=protobuf/install/lib
-D Protobuf_LIBRARIES=protobuf/install/lib
-D ONNX_USE_LITE_PROTO=ON
-D ONNX_USE_PROTOBUF_SHARED_LIBS=OFF
-D ONNX_GEN_PB_TYPE_STUBS=OFF
-D ONNX_ML=0

- name: Build onnx
if: steps.cache-onnx.outputs.cache-hit != 'true'
run: cmake --build onnx/build --verbose

- name: Install onnx
if: steps.cache-onnx.outputs.cache-hit != 'true'
run: cmake --install onnx/build --prefix onnx/install

- name: Download VapourSynth headers
run: |
curl -L -o vs.zip https://github.com/vapoursynth/vapoursynth/archive/refs/tags/R57.zip
unzip -q vs.zip
mv vapoursynth*/ vapoursynth
- name: Setup ONNX Runtime
run: |
curl -L -o ort.tgz https://github.com/microsoft/onnxruntime/releases/download/v1.17.1/onnxruntime-osx-arm64-1.17.1.tgz
tar -xf ort.tgz
mv onnxruntime-* onnxruntime
- name: Configure
run: cmake -S . -B build -G Ninja -LA
-D CMAKE_BUILD_TYPE=Release
-D CMAKE_CXX_FLAGS="-Wall -ffast-math -march=armv8-a"
-D VAPOURSYNTH_INCLUDE_DIRECTORY="`pwd`/vapoursynth/include"
-D ONNX_RUNTIME_API_DIRECTORY=onnxruntime/include
-D ONNX_RUNTIME_LIB_DIRECTORY=onnxruntime/lib
-D protobuf_DIR=protobuf/install/lib/cmake/protobuf
-D ONNX_DIR=onnx/install/lib/cmake/ONNX
-D CMAKE_CXX_STANDARD=20
-D ENABLE_COREML=ON

- name: Build
run: cmake --build build --verbose

- name: Install
run: cmake --install build --prefix install

- name: Prepare for upload
run: |
mkdir artifact
cp -v install/lib/*.dylib artifact
- name: Describe
run: git describe --tags --long

- name: Upload
uses: actions/upload-artifact@v3
with:
name: vsort-macos-arm64
path: vsort/artifact
22 changes: 22 additions & 0 deletions scripts/vsmlrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ class ORT_CPU:

# internal backend attributes
supports_onnx_serialization: bool = True

@dataclass(frozen=False)
class ORT_COREML:
""" backend for coreml """
num_streams: int = 1
verbosity: int = 0
fp16: bool = False
fp16_blacklist_ops: typing.Optional[typing.Sequence[str]] = None

# internal backend attributes
supports_onnx_serialization: bool = True

@dataclass(frozen=False)
class ORT_CUDA:
Expand Down Expand Up @@ -2357,6 +2368,17 @@ def _inference(
fp16_blacklist_ops=backend.fp16_blacklist_ops,
**kwargs
)
elif isinstance(backend, Backend.ORT_COREML):
ret = core.ort.Model(
clips, network_path,
provider="COREML", builtin=False,
num_streams=backend.num_streams,
verbosity=backend.verbosity,
fp16=backend.fp16,
path_is_serialization=path_is_serialization,
fp16_blacklist_ops=backend.fp16_blacklist_ops,
**kwargs
)
elif isinstance(backend, Backend.ORT_CUDA):
version_list = core.ort.Version().get("onnxruntime_version", b"0.0.0").split(b'.')
if len(version_list) != 3:
Expand Down
5 changes: 5 additions & 0 deletions vsort/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(ONNX_RUNTIME_LIB_DIRECTORY "" CACHE PATH "Path to ONNX Runtime libraries")

set(ENABLE_CUDA OFF CACHE BOOL "Enable CUDA backend")
set(ENABLE_DML OFF CACHE BOOL "Enable DirectML backend")
set(ENABLE_COREML OFF CACHE BOOL "Enable CoreML support")

find_package(protobuf REQUIRED CONFIG)
find_package(ONNX REQUIRED CONFIG)
Expand Down Expand Up @@ -64,6 +65,10 @@ if (ENABLE_DML)
add_compile_definitions(ENABLE_DML)
endif()

if(ENABLE_COREML)
add_compile_definitions(ENABLE_COREML=1)
endif()

target_include_directories(vsort PUBLIC
"${PROJECT_BINARY_DIR}"
)
Expand Down
2 changes: 1 addition & 1 deletion vsort/vs_onnxruntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ static void VS_CC vsOrtCreate(
}
#endif // ENABLE_CUDA
#ifdef ENABLE_COREML
else if (d->backend == Backend::COREML) {
if (d->backend == Backend::COREML) {
checkError(OrtSessionOptionsAppendExecutionProvider_CoreML(
session_options,
0
Expand Down

0 comments on commit d6fa596

Please sign in to comment.