Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DirectMLを使用する #81

Merged
merged 82 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from 80 commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
9059af2
動かないけどとりあえずコミット
Oyaki122 Feb 9, 2022
1ca9a60
Yosshiさんのモデルで動作した
Oyaki122 Feb 13, 2022
d3a839c
コメント追加・dml,cuda分岐の作成
Oyaki122 Feb 13, 2022
21f2499
ちょっと変更
Oyaki122 Feb 14, 2022
7a9cb7c
突然initで失敗するようになったが、dllをすべて読み込む(PR#49をなくす)と動いた
Oyaki122 Feb 15, 2022
d38c3a6
DirectML.dllのみ明示的に読み込むようにした
Oyaki122 Feb 18, 2022
5758ef8
検証用のゴミを削除
Oyaki122 Feb 18, 2022
88d2078
workflowを変更
Oyaki122 Feb 18, 2022
35bf378
dml_urlを追加
Oyaki122 Feb 18, 2022
3c4a3bb
エラー修正
Oyaki122 Feb 18, 2022
ea4b9f6
cacheのpathを追加
Oyaki122 Feb 18, 2022
59833b2
スペル修正
Oyaki122 Feb 18, 2022
d15151d
cmakeがonnxruntimeを見つけられないのでデバッグ
Oyaki122 Feb 18, 2022
3ce40a0
fix
Oyaki122 Feb 18, 2022
2d8dc90
fix
Oyaki122 Feb 18, 2022
8e86006
directmlはビルド時にいらないようなので削除
Oyaki122 Feb 18, 2022
d990b31
Merge branch 'main' into dml
Oyaki122 Feb 18, 2022
61c8f22
dll周り以外動くようにした
Oyaki122 Feb 18, 2022
41b3289
ctypesで動いた
Oyaki122 Feb 18, 2022
9962df7
lib作成呼び出しの削除他
Oyaki122 Feb 19, 2022
ba29158
ちょこちょこ変更
Oyaki122 Feb 19, 2022
460acf9
Merge branch 'windows_config' into dml
Oyaki122 Feb 19, 2022
ed65259
pip install できるようにした
Oyaki122 Feb 19, 2022
b60ac56
Merge branch 'windows_config' into dml
Oyaki122 Feb 19, 2022
2cf1404
dmlをcmakeのオプション扱いに、__init__.pyをdml専用に
Oyaki122 Feb 19, 2022
c7b43c9
configure_directmlを作成
Oyaki122 Feb 19, 2022
6bf5fc4
setup.pyからcythonを完全排除
Oyaki122 Feb 19, 2022
fec5551
readmeを大幅改変
Oyaki122 Feb 19, 2022
7b04b37
リドミ微編、buildからinstall requirement.txtを削除
Oyaki122 Feb 19, 2022
a265269
Merge branch 'windows_config' into dml
Oyaki122 Feb 19, 2022
d6d9844
delete numpy
Oyaki122 Feb 19, 2022
ec43e81
numpyをインストールするように変更
Oyaki122 Feb 19, 2022
d8e12f8
npつけ忘れ
Oyaki122 Feb 19, 2022
7224757
良うわからんけどrpathを消してみる
Oyaki122 Feb 19, 2022
bf2afe3
fix
Oyaki122 Feb 19, 2022
5820913
fix
Oyaki122 Feb 19, 2022
f7e4d2a
fix
Oyaki122 Feb 19, 2022
f0a0591
fix
Oyaki122 Feb 19, 2022
7f5a6d2
fix
Oyaki122 Feb 19, 2022
f595d03
fix
Oyaki122 Feb 19, 2022
dc191f3
fix
Oyaki122 Feb 19, 2022
fe6d180
fix
Oyaki122 Feb 19, 2022
8aa4120
fix
Oyaki122 Feb 19, 2022
1ed5446
fix
Oyaki122 Feb 19, 2022
8a00426
macでrpathがうまく設定されていなかったのでどうにか設定した
Oyaki122 Feb 19, 2022
c42d9c9
検証用コードを削除
Oyaki122 Feb 19, 2022
9158a19
微変更
Oyaki122 Feb 19, 2022
ca9ed5d
__init__の中身を削除
Oyaki122 Feb 19, 2022
2bb8f7f
微修正
Oyaki122 Feb 19, 2022
869f88d
.gitignoreを元に戻した
Oyaki122 Feb 19, 2022
32d670a
検証
Oyaki122 Feb 20, 2022
75698d0
fix
Oyaki122 Feb 20, 2022
f806a8b
Merge branch 'windows_config' into dml
Oyaki122 Feb 20, 2022
84ad483
.pydが管理対象になっていたので外す
Oyaki122 Feb 20, 2022
c764b45
configureでdml版onnxruntimeが落とせるように
Oyaki122 Feb 20, 2022
3d70eed
検証用のprintを削除
Oyaki122 Feb 20, 2022
20e8a26
ゴミを削除
Oyaki122 Feb 20, 2022
ea408e9
cmakeが壊れていたので修正
Oyaki122 Feb 20, 2022
a1bda8e
rpath関連を書くべき場所があったので移動
Oyaki122 Feb 20, 2022
978a9d9
やっぱりビルドにdirectmlが必要だった
Oyaki122 Feb 20, 2022
bd31d0b
Merge branch 'windows_config' into dml
Oyaki122 Feb 20, 2022
a592152
Merge branch 'main' into dml
Oyaki122 Feb 20, 2022
4a606e4
configure_directml.pyをconfigure.pyに統合
Oyaki122 Feb 21, 2022
09601d4
りどみ変更
Oyaki122 Feb 21, 2022
e8f6a0e
ビルド変更
Oyaki122 Feb 21, 2022
e87c37e
add dml url
Oyaki122 Feb 21, 2022
8c38882
ARM判定を変更
Oyaki122 Feb 21, 2022
37e30ef
fix
Oyaki122 Feb 22, 2022
44074e0
退避modelを削除
Oyaki122 Feb 22, 2022
102ad36
Merge branch 'main' into dml
Oyaki122 Feb 22, 2022
06fcfc8
mergeミスを修正
Oyaki122 Feb 22, 2022
ef65d7b
readmeを改良
Oyaki122 Feb 22, 2022
0720254
dml版onnxruntimeの存在判定対象を変更
Oyaki122 Feb 22, 2022
f7cbdec
検証用のゴミを削除
Oyaki122 Feb 22, 2022
fad5be8
fix
Oyaki122 Feb 22, 2022
15e3dd4
fix
Oyaki122 Feb 22, 2022
f994d93
fix
Oyaki122 Feb 22, 2022
f226073
dml-onnxruntime判定を変更
Oyaki122 Feb 22, 2022
ec9deb5
core_cpu->core_gpu
Oyaki122 Feb 23, 2022
4e2aeea
Update DetermineTargetArchitecture.cmake
Oyaki122 Feb 26, 2022
cd08c59
onnxruntimeがあったときに、上書きするか残すか聞くようにする
Oyaki122 Feb 26, 2022
cd52468
cpu版もビルドするようにする
Oyaki122 Feb 26, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 66 additions & 27 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,37 @@ jobs:
device: gpu
python_architecture: 'x64'
onnxruntime_url: https://github.com/microsoft/onnxruntime/releases/download/v1.10.0/onnxruntime-win-x64-gpu-1.10.0.zip
artifact_name: windows-x64-gpu
artifact_name: windows-x64-cuda

- os: windows-2019
device: cpu-x64
device: directml-x64
python_architecture: 'x64'
onnxruntime_url: https://github.com/microsoft/onnxruntime/releases/download/v1.10.0/onnxruntime-win-x64-1.10.0.zip
artifact_name: windows-x64-cpu
onnxruntime_url: https://github.com/microsoft/onnxruntime/releases/download/v1.10.0/Microsoft.ML.OnnxRuntime.DirectML.1.10.0.zip
directml_url: https://www.nuget.org/api/v2/package/Microsoft.AI.DirectML/1.8.0
artifact_name: windows-x64-directml
cmake_additional_options: -DDIRECTML=ON -DDIRECTML_DIR=download/directml

- os: windows-2019
device: cpu-x86
device: directml-x86
python_architecture: 'x86'
onnxruntime_url: https://github.com/microsoft/onnxruntime/releases/download/v1.10.0/onnxruntime-win-x86-1.10.0.zip
cmake_additional_options: -DCMAKE_GENERATOR_PLATFORM=Win32
artifact_name: windows-x86-cpu
onnxruntime_url: https://github.com/microsoft/onnxruntime/releases/download/v1.10.0/Microsoft.ML.OnnxRuntime.DirectML.1.10.0.zip
directml_url: https://www.nuget.org/api/v2/package/Microsoft.AI.DirectML/1.8.0
cmake_additional_options: -DCMAKE_GENERATOR_PLATFORM=Win32 -DDIRECTML=ON -DDIRECTML_DIR=download/directml
artifact_name: windows-x86-directml

- os: windows-2019
device: cpu-arm64
onnxruntime_url: https://github.com/microsoft/onnxruntime/releases/download/v1.10.0/onnxruntime-win-arm64-1.10.0.zip
cmake_additional_options: -DCMAKE_GENERATOR_PLATFORM=arm64
artifact_name: windows-arm64-cpu
device: directml-arm64
onnxruntime_url: https://github.com/microsoft/onnxruntime/releases/download/v1.10.0/Microsoft.ML.OnnxRuntime.DirectML.1.10.0.zip
directml_url: https://www.nuget.org/api/v2/package/Microsoft.AI.DirectML/1.8.0
cmake_additional_options: -DCMAKE_GENERATOR_PLATFORM=arm64 -DDIRECTML=ON -DDIRECTML_DIR=download/directml
artifact_name: windows-arm64-directml

- os: windows-2019
device: cpu-arm
onnxruntime_url: https://github.com/microsoft/onnxruntime/releases/download/v1.10.0/onnxruntime-win-arm-1.10.0.zip
cmake_additional_options: -DCMAKE_GENERATOR_PLATFORM=arm
artifact_name: windows-arm-cpu
device: directml-arm
onnxruntime_url: https://github.com/microsoft/onnxruntime/releases/download/v1.10.0/Microsoft.ML.OnnxRuntime.DirectML.1.10.0.zip
directml_url: https://www.nuget.org/api/v2/package/Microsoft.AI.DirectML/1.8.0
cmake_additional_options: -DCMAKE_GENERATOR_PLATFORM=arm -DDIRECTML=ON -DDIRECTML_DIR=download/directml
artifact_name: windows-arm-directml

- os: macos-10.15
device: cpu-x64
Expand Down Expand Up @@ -122,6 +127,19 @@ jobs:
key: onnxruntime-cache-v1-${{ hashFiles('download/onnxruntime_url.txt') }}
path: download/onnxruntime

- name: Export DirectML url to calc hash
if: endswith(matrix.artifact_name, '-directml')
shell: bash
run: echo "${{matrix.directml_url}}" >> download/directml_url.txt

- name: Cache DirectML
if: endswith(matrix.artifact_name, '-directml')
uses: actions/cache@v2
id: directml-cache
with:
key: directml-cache-v1-${{ hashFiles('download/directml_url.txt') }}
path: download/directml

# download/onnxruntime/lib/onnxruntime.dll
- name: Download ONNX Runtime (zip)
if: steps.onnxruntime-cache.outputs.cache-hit != 'true' && endsWith(matrix.onnxruntime_url, '.zip')
Expand All @@ -133,11 +151,14 @@ jobs:
# strip-components
TEMPDIR=$(mktemp -d)
unzip download/onnxruntime.zip -d "${TEMPDIR}"
mv "${TEMPDIR}"/*/* download/onnxruntime/
mv "${TEMPDIR}"/* download/onnxruntime/.
rm -rf "${TEMPDIR}"

rm download/onnxruntime.zip

- run: ls download
- run: ls download/onnxruntime

# download/onnxruntime/lib/libonnxruntime.so
# download/onnxruntime/lib/libonnxruntime.dylib
- name: Download ONNX Runtime (tgz)
Expand All @@ -149,6 +170,24 @@ jobs:
tar xf download/onnxruntime.tgz -C download/onnxruntime --strip-components 1
rm download/onnxruntime.tgz

# download directml
- name: Download DirectML
if: steps.directml-cache.outputs.cache-hit != 'true' && endswith(matrix.artifact_name, '-directml')
shell: bash
run: |
curl -L "${{matrix.directml_url}}" -o download/directml.zip
mkdir -p download/directml

# strip-components
TEMPDIR=$(mktemp -d)
unzip download/directml.zip -d "${TEMPDIR}"
mv "${TEMPDIR}"/* download/directml/.
rm -rf "${TEMPDIR}"

rm download/directml.zip

ls download/directml

# Build
- if: startsWith(matrix.os, 'windows')
uses: ilammy/msvc-dev-cmd@v1
Expand Down Expand Up @@ -255,16 +294,16 @@ jobs:
mkdir release

readarray -t MAPPINGS <<EOF
windows-x64-gpu core.dll core_gpu_x64_nvidia.dll
windows-x64-cpu core.dll core_cpu_x64.dll
windows-x86-cpu core.dll core_cpu_x86.dll
windows-arm64-cpu core.dll core_cpu_arm64.dll
windows-arm-cpu core.dll core_cpu_arm.dll
osx-universal2-cpu libcore.dylib libcore_cpu_universal2.dylib
linux-x64-gpu libcore.so libcore_gpu_x64_nvidia.so
linux-x64-cpu libcore.so libcore_cpu_x64.so
linux-armhf-cpu libcore.so libcore_cpu_armhf.so
linux-arm64-cpu libcore.so libcore_cpu_arm64.so
windows-x64-cuda core.dll core_gpu_x64_nvidia.dll
windows-x64-directml core.dll core_gpu_x64_directml.dll
windows-x86-directml core.dll core_gpu_x86_directml.dll
windows-arm64-directml core.dll core_gpu_arm64_directml.dll
windows-arm-directml core.dll core_gpu_arm_directml.dll
osx-universal2-cpu libcore.dylib libcore_cpu_universal2.dylib
linux-x64-gpu libcore.so libcore_gpu_x64_nvidia.so
linux-x64-cpu libcore.so libcore_cpu_x64.so
linux-armhf-cpu libcore.so libcore_cpu_armhf.so
linux-arm64-cpu libcore.so libcore_cpu_arm64.so
EOF

for line in "${MAPPINGS[@]}"; do
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
# ONNX
onnxruntime*/

# DirectML
directml*/

# Build artifacts
build/
lib/
Expand Down
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,13 @@ project(VoiceVoxCore)
# TODO: download onnxruntime
set(ONNXRUNTIME_DIR "${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime" CACHE PATH "Path to ONNX Runtime")

option(DIRECTML "Enables building for DirectML" OFF)
set(DIRECTML_DIR "${CMAKE_CURRENT_SOURCE_DIR}/directml" CACHE PATH "Path to ONNX Runtime")
if(DIRECTML)
# DirectML読み込み用にCPUのアーキテクチャを求める
set(DML_ARCH)
include(${CMAKE_CURRENT_SOURCE_DIR}/DetermineTargetArchitecture.cmake)
determine_target_architecture(DML_ARCH)
endif()

add_subdirectory(core)
62 changes: 62 additions & 0 deletions DetermineTargetArchitecture.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# https://github.com/civetweb/civetweb/blob/master/cmake/DetermineTargetArchitecture.cmake
Oyaki122 marked this conversation as resolved.
Show resolved Hide resolved
#
# Copyright (c) 2013-2021 The CivetWeb developers (https://github.com/civetweb/civetweb/blob/master/CREDITS.md)
# Copyright (c) 2004-2013 Sergey Lyubka
# Copyright (c) 2013 No Face Press, LLC (Thomas Davis)
# Copyright (c) 2013 F-Secure Corporation
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# - Determines the target architecture of the compilation
#
# This function checks the architecture that will be built by the compiler
# and sets a variable to the architecture
#
# determine_target_architecture(<OUTPUT_VAR>)
#
# - Example
#
# include(DetermineTargetArchitecture)
# determine_target_architecture(PROJECT_NAME_ARCHITECTURE)

if(__determine_target_architecture)
return()
endif()
set(__determine_target_architecture INCLUDED)

function(determine_target_architecture FLAG)
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
if (MSVC)
if("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "X86")
set(ARCH "x86")
elseif("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "x64")
set(ARCH "x64")
elseif("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "ARM64")
set(ARCH "arm64")
elseif("${MSVC_C_ARCHITECTURE_ID}" MATCHES "ARM*")
set(ARCH "arm")
else()
message(FATAL_ERROR "Failed to determine the MSVC target architecture: ${MSVC_C_ARCHITECTURE_ID}")
endif()
else()
execute_process(
COMMAND ${CMAKE_C_COMPILER} -dumpmachine
RESULT_VARIABLE RESULT
OUTPUT_VARIABLE ARCH
ERROR_QUIET
)
if (RESULT)
message(FATAL_ERROR "Failed to determine target architecture triplet: ${RESULT}")
endif()
string(REGEX MATCH "([^-]+).*" ARCH_MATCH ${ARCH})
if (NOT CMAKE_MATCH_1 OR NOT ARCH_MATCH)
message(FATAL_ERROR "Failed to match the target architecture triplet: ${ARCH}")
endif()
set(ARCH ${CMAKE_MATCH_1})
endif()
message(STATUS "Target architecture - ${ARCH}")
set(${FLAG} ${ARCH} PARENT_SCOPE)
endfunction()
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ pip install .
- ONNX Runtime v1.9.0/v1.9.1

#### GPUを使用する場合
##### CUDA
Windows, Linux上でnvidia製GPUを使用してCUDAを用いた合成を行う場合、[CUDA 11.1](https://developer.nvidia.com/cuda-11.1.0-download-archive),[CUDNN](https://developer.nvidia.com/cudnn)のインストールに加えてGPU に対応した [ONNXRUNTIME](https://github.com/microsoft/onnxruntime) のダウンロードが必要です。

Windows, Linux上でnvidia製GPUを使用する場合、CUDA11.1,CUDNNのインストールに加えてGPU に対応した [ONNXRUNTIME](https://github.com/microsoft/onnxruntime) のダウンロードが必要です。
##### DirectML
Windows上でDirectX12に対応したGPUを使用してDirectMLを用いた合成を行う場合、[DirectML](https://www.nuget.org/packages/Microsoft.AI.DirectML)及びDirectMLに対応した[ONNXRUNTIME](https://github.com/microsoft/onnxruntime) のダウンロードが必要です。

DirectMLは.nupkgファイルで提供されますが、拡張子を.zipに変更した上で、リポジトリに`directml`というディレクトリ名で展開してください。


#### Raspberry Pi (armhf)の場合
Expand Down Expand Up @@ -61,10 +66,19 @@ pip install .
### 注意
#### GPUの使用について

##### CUDA
nvidia製GPUを搭載したWindows, Linux PCではCUDAを用いた合成が可能です。
CUDAを使用する場合、[CUDA 11.1](https://developer.nvidia.com/cuda-11.1.0-download-archive) と [CUDNN](https://developer.nvidia.com/cudnn) をインストールした上で、環境構築時、上記例の代わりに
```bash
python configure.py --use_gpu
python configure.py --use_cuda
```
を実行する必要があります

##### DirectML
DirectX12に対応したGPUを搭載したWindows PCではDirectMLを用いた合成が可能です
DirectMLを使用する場合、環境構築時、上記例の代わりに
```bash
python configure.py --use_directml
```
を実行する必要があります

Expand Down Expand Up @@ -116,14 +130,18 @@ python run.py \
## コアライブラリのビルド

[Releases](https://github.com/Hiroshiba/voicevox_core/releases) にあるビルド済みのコアライブラリを利用せず、自分で一からビルドする場合こちらを参照してください。ビルドにはONNXRUNTIMEに加えてCMake 3.16以上が必要です。

modelフォルダにあるonnxモデルはダミーのため、ノイズの混じった音声が出力されます

```bash
# C++モジュールのビルド
mkdir build
cd build
# もしダウンロードしたonnx runtimeが別のところにあるなら、以下のコマンドを
# cmake .. -DONNXRUNTIME_DIR=(ダウンロードしたonnx runtimeのパス) に変更する。

# cmake .. 時のオプション
# -DONNXRUNTIME_DIR=(パス) ダウンロードしたonnxruntimeが別フォルダにある時指定
# -DDIRECTML=ON DirectMLを使用する場合指定
# -DDIRECTML_DIR=(パス) ダウンロードしたDirectMLが別フォルダにある時指定
cmake ..
cmake --build . --config Release
cmake --install .
Expand Down
Loading