Skip to content

Commit

Permalink
coreのctypes実装を追加&古いコアを利用できるようにする (#254)
Browse files Browse the repository at this point in the history
* 古いコアを使えるように

* pysen

* 全てcore_wrapperを使うよう変更

* onnx版対応

* fix typo

* 読み込み順修正

* ビルド周りの修正

* fix dll path

* fix cancellable engine

* fix format

* ビルド周り修正

* 重複処理を関数化

* パスを厳密に指定

* libtorchの読み込み修正

* expanduserの処理を追加

* 作業ディレクトリを変更

* fix format

* expanduser処理の場所を修正

* mockが起動するように変更

* MacでのLibtorch対応を削除

* Revert "作業ディレクトリを変更"

This reverts commit 0e0fa53.

* fix format

* 依存関係インストールの重複を削除

* build.ymlを修正

* VOICEVOX_CORE_SOURCE_VERSIONを削除

* 複数バージョン対応

* ImportErrorを修正

* ミスを修正

* コンフリクト解消時にデグレってたので修正

* まだデグレってたので修正

* コメントを追加

* 変数ミスを修正

* 変数名を分かりやすく変更

* 引数名のdirをdirsに変更

* 引数名をmodel_libからruntimeに変更

* (本体側)引数名を変更

* speaker_infoに引数を追加

* cancellableを使用しようとした際に例外を投げるようにする

* 不要なcopyを削除

* 関数名を変更

* アサーションを追加

* 型ヒント修正&pysen

* Dictの引数ミス修正

* 型ヒントの修正漏れを追記

* cancellableを起動不可にする
  • Loading branch information
takana-v authored Jan 24, 2022
1 parent 59e39b3 commit 6f2415a
Show file tree
Hide file tree
Showing 9 changed files with 467 additions and 253 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/build-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ env:
IMAGE_NAME: ${{ secrets.DOCKERHUB_USERNAME }}/voicevox_engine
PYTHON_VERSION: '3.8.10'
VOICEVOX_CORE_VERSION: '0.10.preview.3'
VOICEVOX_CORE_SOURCE_VERSION: '0.10.preview.3'
VOICEVOX_ENGINE_VERSION: |- # releaseのときはタグが、それ以外はlatestがバージョン名に
${{ github.event.release.tag_name != '' && github.event.release.tag_name || 'latest' }}
Expand Down Expand Up @@ -109,7 +108,6 @@ jobs:
PYTHON_VERSION=${{ env.PYTHON_VERSION }}
VOICEVOX_ENGINE_VERSION=${{ env.VOICEVOX_ENGINE_VERSION }}
VOICEVOX_CORE_VERSION=${{ env.VOICEVOX_CORE_VERSION }}
VOICEVOX_CORE_SOURCE_VERSION=${{ env.VOICEVOX_CORE_SOURCE_VERSION }}
VOICEVOX_CORE_LIBRARY_NAME=${{ matrix.voicevox_core_library_name }}
ONNXRUNTIME_URL=${{ matrix.onnxruntime_url }}
target: ${{ matrix.target }}
Expand Down
99 changes: 6 additions & 93 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ env:
PYTHON_VERSION: '3.8.10'
VOICEVOX_RESOURCE_VERSION: '0.10.preview.2'
VOICEVOX_CORE_VERSION: '0.10.preview.3'
VOICEVOX_CORE_SOURCE_VERSION: '0.10.preview.3'
VOICEVOX_ENGINE_VERSION: |- # releaseのときはタグが、それ以外はlatestがバージョン名に
${{ github.event.release.tag_name != '' && github.event.release.tag_name || 'latest' }}
Expand Down Expand Up @@ -144,44 +143,6 @@ jobs:
ditto -x -k --sequesterRsrc --rsrc download/core.zip download/
rm download/core.zip
# Install VOICEVOX Core Python package
- name: Prepare VOICEVOX Core source cache
uses: actions/cache@v2
id: voicevox-core-source-cache
with:
key: ${{ matrix.os }}-voicevox-core-source-${{ env.VOICEVOX_CORE_SOURCE_VERSION }}
path: download/voicevox_core_source

- name: Checkout VOICEVOX Core source
if: steps.voicevox-core-source-cache.outputs.cache-hit != 'true'
uses: actions/checkout@v2
with:
repository: VOICEVOX/voicevox_core
ref: ${{ env.VOICEVOX_CORE_SOURCE_VERSION }}
path: download/voicevox_core_source

- name: Install VOICEVOX Core Python package
shell: bash
run: |
set -eux
mkdir -p download/voicevox_core_source/core/lib
# Copy VOICEVOX Core dylib to core/lib
cp download/core/${{ matrix.voicevox_core_library_name }} download/voicevox_core_source/core/lib/libcore.dylib
cd download/voicevox_core_source
cp core/src/core.h core/lib/
cd -
# Copy ONNX Runtime dylib
cp download/onnxruntime/lib/libonnxruntime.*.dylib download/voicevox_core_source/core/lib/
# Install VOICEVOX Core Python package with libcore.dylib & libonnxruntime.*.dylib
cd download/voicevox_core_source
NUMPY_INCLUDE=`python -c "import numpy; print(numpy.get_include())"`
CPATH="$NUMPY_INCLUDE:${CPATH:-}" pip install .
- name: Download PyOpenJTalk dictionary
shell: bash
run: |
Expand Down Expand Up @@ -228,7 +189,8 @@ jobs:
--include-data-file=../user.dic=./ \
--include-data-file=../download/core/*.bin=./ \
--include-data-file=../download/core/metas.json=./ \
--include-data-file=../download/onnxruntime/lib/libonnxruntime.*.dylib=./ \
--include-data-file=../download/core/${{ matrix.voicevox_core_library_name }}=./ \
--include-data-file=../download/onnxruntime/lib/libonnxruntime.dylib=./ \
--include-data-file=${{ env.pythonLocation }}/lib/python*/site-packages/scipy/.dylibs/*.dylib=./scipy/.dylibs/ \
--include-data-file=${{ env.pythonLocation }}/lib/python*/site-packages/_soundfile_data/*=./_soundfile_data/ \
--include-data-dir=../speaker_info=./speaker_info \
Expand Down Expand Up @@ -352,7 +314,6 @@ jobs:
PYTHON_VERSION=${{ env.PYTHON_VERSION }}
VOICEVOX_ENGINE_VERSION=${{ env.VOICEVOX_ENGINE_VERSION }}
VOICEVOX_CORE_VERSION=${{ env.VOICEVOX_CORE_VERSION }}
VOICEVOX_CORE_SOURCE_VERSION=${{ env.VOICEVOX_CORE_SOURCE_VERSION }}
VOICEVOX_CORE_LIBRARY_NAME=${{ matrix.voicevox_core_library_name }}
ONNXRUNTIME_URL=${{ matrix.onnxruntime_url }}
target: ${{ matrix.target }}
Expand Down Expand Up @@ -650,49 +611,6 @@ jobs:
unzip download/core.zip -d download/
rm download/core.zip
# Install VOICEVOX Core Python package
- name: Prepare VOICEVOX Core source cache
uses: actions/cache@v2
id: voicevox-core-source-cache
with:
key: ${{ matrix.os }}-voicevox-core-source-${{ env.VOICEVOX_CORE_SOURCE_VERSION }}
path: download/voicevox_core_source

- name: Checkout VOICEVOX Core source
if: steps.voicevox-core-source-cache.outputs.cache-hit != 'true'
uses: actions/checkout@v2
with:
repository: VOICEVOX/voicevox_core
ref: ${{ env.VOICEVOX_CORE_SOURCE_VERSION }}
path: download/voicevox_core_source

- name: Install VOICEVOX Core Python package
shell: bash
run: |
set -eux
# Generate VOICEVOX Core LIB from DLL
cp download/core/${{ matrix.voicevox_core_dll_name }} download/voicevox_core_source/example/python/core.dll
cd download/voicevox_core_source/example/python
./makelib.bat core
cd -
# Copy VOICEVOX Core DLL & LIB to core/lib
cd download/voicevox_core_source
mkdir -p core/lib
mv example/python/core.dll core/lib/
mv example/python/core.lib core/lib/
cp core/src/core.h core/lib/
cd -
# Copy ONNX Runtime DLLs
cp download/onnxruntime/lib/*.dll download/voicevox_core_source/core/lib/
# Install VOICEVOX Core Python package with core.dll & onnxruntime*.dll
cd download/voicevox_core_source
pip install .
- name: Generate licenses.json
shell: bash
run: python generate_licenses.py > licenses.json
Expand Down Expand Up @@ -720,7 +638,7 @@ jobs:
# Replace version
sed -i "s/__version__ = \"latest\"/__version__ = \"${{ env.VOICEVOX_ENGINE_VERSION }}\"/" voicevox_engine/__init__.py
python -m nuitka \
--standalone \
--assume-yes-for-downloads \
Expand All @@ -737,6 +655,8 @@ jobs:
--include-data-file="presets.yaml=./" \
--include-data-file=download/core/*.bin=./ \
--include-data-file="download/core/metas.json=./" \
--include-data-file="download/onnxruntime/lib/onnxruntime.dll=./" \
--include-data-file="download/core/${{ matrix.voicevox_core_dll_name }}=./" \
--include-data-dir="speaker_info=./speaker_info" \
--msvc=14.2 \
--follow-imports \
Expand All @@ -758,13 +678,6 @@ jobs:
run: |
set -eux
# Workaround: Move core.dll to run.dist/core/lib/
# Nuitka copies core.dll to run.dist/core.dll
# but core Python module will load core.dll from run.dist/core/lib/core.dll.
mkdir -p run.dist/core/lib
mv run.dist/core.dll run.dist/core/lib/
mv run.dist/onnxruntime.dll run.dist/core/lib/
# Build artifact directory
mkdir -p artifact
ln -sf "$(pwd)/run.dist"/* artifact/
Expand All @@ -773,7 +686,7 @@ jobs:
if [ -f "download/onnxruntime/lib/onnxruntime_providers_cuda.dll" ]; then
# ONNX Runtime providers (Nuitka does not copy dynamic loaded libraries)
ln -sf "$(pwd)/download/onnxruntime/lib"/onnxruntime_*.dll artifact/core/lib/
ln -sf "$(pwd)/download/onnxruntime/lib"/onnxruntime_*.dll artifact/
# CUDA
ln -sf "$(pwd)/download/cuda/bin"/cublas64_*.dll artifact/
Expand Down
49 changes: 5 additions & 44 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,8 @@ RUN <<EOF
mkdir /opt/voicevox_core
mv "./core/${VOICEVOX_CORE_LIBRARY_NAME}" /opt/voicevox_core/

if [ "${VOICEVOX_CORE_LIBRARY_NAME}" != "libcore.so" ]; then
# Create relative symbolic link
cd /opt/voicevox_core
ln -sf "${VOICEVOX_CORE_LIBRARY_NAME}" libcore.so
cd -
fi

# Move Voice Library to /opt/voicevox_core/
mv ./core/*.bin ./core/core.h ./core/metas.json /opt/voicevox_core/
mv ./core/*.bin ./core/metas.json /opt/voicevox_core/

# Move documents to /opt/voicevox_core/
mv ./core/README.txt ./core/VERSION /opt/voicevox_core/
Expand Down Expand Up @@ -202,40 +195,6 @@ COPY --from=download-core-env /opt/voicevox_core /opt/voicevox_core
# COPY --from=download-onnxruntime-env /etc/ld.so.conf.d/onnxruntime.conf /etc/ld.so.conf.d/onnxruntime.conf
COPY --from=download-onnxruntime-env /opt/onnxruntime /opt/onnxruntime

# Install VOICEVOX Core Python module
ARG VOICEVOX_CORE_SOURCE_VERSION=0.10.preview.3
RUN <<EOF
set -eux

git clone -b "${VOICEVOX_CORE_SOURCE_VERSION}" --depth 1 https://github.com/VOICEVOX/voicevox_core.git /tmp/voicevox_core_source

# Copy shared libraries to repo to be packed in core package
# ONNX Runtime: included
# CUDA/cuDNN: not included

mkdir -p /tmp/voicevox_core_source/core/lib
cp /opt/voicevox_core/libcore.so /tmp/voicevox_core_source/core/lib/

# Copy versioned shared library only (setup.py will copy package data as real file on install)
cd /opt/onnxruntime/lib
find . -name 'libonnxruntime.so.*' -or -name 'libonnxruntime_*.so' | xargs -I% cp -v % /tmp/voicevox_core_source/core/lib/
cd -

# Duplicate core.h in repo
cp /tmp/voicevox_core_source/core/src/core.h /tmp/voicevox_core_source/core/lib/

# Install voicevox_core Python module
# Files will be generated at build time, so chown for general user
chown -R user:user /tmp/voicevox_core_source

cd /tmp/voicevox_core_source

gosu user /opt/python/bin/pip3 install .

# remove cloned repository before layer end to reduce image size
rm -rf /tmp/voicevox_core_source
EOF

# Add local files
ADD ./voicevox_engine /opt/voicevox_engine/voicevox_engine
ADD ./docs /opt/voicevox_engine/docs
Expand Down Expand Up @@ -291,11 +250,11 @@ exec "\$@"
EOF

ENTRYPOINT [ "/entrypoint.sh" ]
CMD [ "gosu", "user", "/opt/python/bin/python3", "./run.py", "--voicelib_dir", "/opt/voicevox_core/", "--host", "0.0.0.0" ]
CMD [ "gosu", "user", "/opt/python/bin/python3", "./run.py", "--voicelib_dir", "/opt/voicevox_core/", "--runtime_dir", "/opt/onnxruntime/lib", "--host", "0.0.0.0" ]

# Enable use_gpu
FROM runtime-env AS runtime-nvidia-env
CMD [ "gosu", "user", "/opt/python/bin/python3", "./run.py", "--use_gpu", "--voicelib_dir", "/opt/voicevox_core/", "--host", "0.0.0.0" ]
CMD [ "gosu", "user", "/opt/python/bin/python3", "./run.py", "--use_gpu", "--voicelib_dir", "/opt/voicevox_core/", "--runtime_dir", "/opt/onnxruntime/lib", "--host", "0.0.0.0" ]

# Binary build environment (common to CPU, GPU)
FROM runtime-env AS build-env
Expand Down Expand Up @@ -369,6 +328,8 @@ RUN <<EOF
--include-data-file=/opt/voicevox_engine/user.dic=./ \
--include-data-file=/opt/voicevox_core/*.bin=./ \
--include-data-file=/opt/voicevox_core/metas.json=./ \
--include-data-file=/opt/voicevox_core/*.so=./ \
--include-data-file=/opt/onnxruntime/lib/libonnxruntime.so=./ \
--include-data-dir=/opt/voicevox_engine/speaker_info=./speaker_info \
--follow-imports \
--no-prefer-source-code \
Expand Down
Loading

0 comments on commit 6f2415a

Please sign in to comment.