Improve: Make ONNX inference tests more rigorous
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING, Any, cast, Optional, Sequence, Union
|
||||
from typing import TYPE_CHECKING, Any, Optional, Sequence, Union, cast
|
||||
|
||||
import numpy as np
|
||||
from numpy.typing import NDArray
|
||||
@@ -108,9 +108,9 @@ def extract_bert_feature_onnx(
|
||||
res = session.run(
|
||||
[output_name],
|
||||
{
|
||||
"input_ids": cast(NDArray[Any], inputs["input_ids"]).astype(np.int64),
|
||||
"token_type_ids": cast(NDArray[Any], inputs["token_type_ids"]).astype(np.int64),
|
||||
"attention_mask": cast(NDArray[Any], inputs["attention_mask"]).astype(np.int64),
|
||||
"input_ids": inputs["input_ids"].astype(np.int64), # type: ignore
|
||||
"token_type_ids": inputs["token_type_ids"].astype(np.int64), # type: ignore
|
||||
"attention_mask": inputs["attention_mask"].astype(np.int64), # type: ignore
|
||||
},
|
||||
)[0]
|
||||
|
||||
@@ -120,9 +120,9 @@ def extract_bert_feature_onnx(
|
||||
style_res = session.run(
|
||||
[output_name],
|
||||
{
|
||||
"input_ids": cast(NDArray[Any], style_inputs["input_ids"]).astype(np.int64),
|
||||
"token_type_ids": cast(NDArray[Any], style_inputs["token_type_ids"]).astype(np.int64),
|
||||
"attention_mask": cast(NDArray[Any], style_inputs["attention_mask"]).astype(np.int64),
|
||||
"input_ids": style_inputs["input_ids"].astype(np.int64), # type: ignore
|
||||
"token_type_ids": style_inputs["token_type_ids"].astype(np.int64), # type: ignore
|
||||
"attention_mask": style_inputs["attention_mask"].astype(np.int64), # type: ignore
|
||||
},
|
||||
)[0]
|
||||
style_res_mean = np.mean(style_res, axis=0)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING, Any, cast, Optional, Sequence, Union
|
||||
from typing import TYPE_CHECKING, Any, Optional, Sequence, Union, cast
|
||||
|
||||
import numpy as np
|
||||
from numpy.typing import NDArray
|
||||
@@ -108,8 +108,8 @@ def extract_bert_feature_onnx(
|
||||
res = session.run(
|
||||
[output_name],
|
||||
{
|
||||
"input_ids": cast(NDArray[Any], inputs["input_ids"]).astype(np.int64),
|
||||
"attention_mask": cast(NDArray[Any], inputs["attention_mask"]).astype(np.int64),
|
||||
"input_ids": inputs["input_ids"].astype(np.int64), # type: ignore
|
||||
"attention_mask": inputs["attention_mask"].astype(np.int64), # type: ignore
|
||||
},
|
||||
)[0]
|
||||
|
||||
@@ -119,8 +119,8 @@ def extract_bert_feature_onnx(
|
||||
style_res = session.run(
|
||||
[output_name],
|
||||
{
|
||||
"input_ids": cast(NDArray[Any], style_inputs["input_ids"]).astype(np.int64),
|
||||
"attention_mask": cast(NDArray[Any], style_inputs["attention_mask"]).astype(np.int64),
|
||||
"input_ids": style_inputs["input_ids"].astype(np.int64), # type: ignore
|
||||
"attention_mask": style_inputs["attention_mask"].astype(np.int64), # type: ignore
|
||||
},
|
||||
)[0]
|
||||
style_res_mean = np.mean(style_res, axis=0)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING, Any, cast, Optional, Sequence, Union
|
||||
from typing import TYPE_CHECKING, Any, Optional, Sequence, Union, cast
|
||||
|
||||
import numpy as np
|
||||
from numpy.typing import NDArray
|
||||
@@ -121,8 +121,8 @@ def extract_bert_feature_onnx(
|
||||
res = session.run(
|
||||
[output_name],
|
||||
{
|
||||
"input_ids": cast(NDArray[Any], inputs["input_ids"]).astype(np.int64),
|
||||
"attention_mask": cast(NDArray[Any], inputs["attention_mask"]).astype(np.int64),
|
||||
"input_ids": inputs["input_ids"].astype(np.int64), # type: ignore
|
||||
"attention_mask": inputs["attention_mask"].astype(np.int64), # type: ignore
|
||||
},
|
||||
)[0]
|
||||
|
||||
@@ -132,8 +132,8 @@ def extract_bert_feature_onnx(
|
||||
style_res = session.run(
|
||||
[output_name],
|
||||
{
|
||||
"input_ids": cast(NDArray[Any], style_inputs["input_ids"]).astype(np.int64),
|
||||
"attention_mask": cast(NDArray[Any], style_inputs["attention_mask"]).astype(np.int64),
|
||||
"input_ids": style_inputs["input_ids"].astype(np.int64), # type: ignore
|
||||
"attention_mask": style_inputs["attention_mask"].astype(np.int64), # type: ignore
|
||||
},
|
||||
)[0]
|
||||
style_res_mean = np.mean(style_res, axis=0)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Literal, Sequence, Union
|
||||
from typing import Any, Literal, Sequence
|
||||
|
||||
import pytest
|
||||
from scipy.io import wavfile
|
||||
@@ -10,8 +10,8 @@ from style_bert_vits2.tts_model import TTSModelHolder
|
||||
def synthesize(
|
||||
inference_type: Literal["torch", "onnx"] = "torch",
|
||||
device: str = "cpu",
|
||||
onnx_providers: Sequence[Union[str, tuple[str, dict[str, Any]]]] = [
|
||||
"CPUExecutionProvider"
|
||||
onnx_providers: Sequence[tuple[str, dict[str, Any]]] = [
|
||||
("CPUExecutionProvider", {}),
|
||||
],
|
||||
):
|
||||
|
||||
@@ -45,6 +45,11 @@ def synthesize(
|
||||
model = model_holder.get_model(model_info.name, model_files[0])
|
||||
model.load()
|
||||
|
||||
# ロードされた InferenceSession の ExecutionProvider が一致するか確認
|
||||
# 一致しない場合、指定された ExecutionProvider で推論できない状態
|
||||
assert model.onnx_session is not None
|
||||
assert model.onnx_session.get_providers()[0] == onnx_providers[0][0]
|
||||
|
||||
# すべてのスタイルに対して音声合成を実行
|
||||
for style in model_info.styles:
|
||||
|
||||
@@ -87,23 +92,35 @@ def test_synthesize_cpu():
|
||||
|
||||
|
||||
def test_synthesize_cuda():
|
||||
pytest.importorskip("torch.cuda")
|
||||
synthesize(inference_type="torch", device="cuda")
|
||||
|
||||
|
||||
def test_synthesize_onnx_cpu():
|
||||
synthesize(inference_type="onnx", onnx_providers=["CPUExecutionProvider"])
|
||||
synthesize(
|
||||
inference_type="onnx",
|
||||
onnx_providers=[
|
||||
("CPUExecutionProvider", {}),
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
def test_synthesize_onnx_cuda():
|
||||
synthesize(
|
||||
inference_type="onnx",
|
||||
onnx_providers=[
|
||||
("CUDAExecutionProvider", {"cudnn_conv_algo_search": "DEFAULT"})
|
||||
("CUDAExecutionProvider", {"cudnn_conv_algo_search": "DEFAULT"}),
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
def test_synthesize_onnx_directml():
|
||||
pytest.importorskip("onnxruntime_directml")
|
||||
synthesize(inference_type="onnx", onnx_providers=["DmlExecutionProvider"])
|
||||
synthesize(
|
||||
inference_type="onnx",
|
||||
onnx_providers=[
|
||||
# device_id: 0 は、システムにインストールされているプライマリディスプレイ用 GPU に対応する
|
||||
# プライマリディスプレイ用 GPU (GPU 0) よりも性能の高い GPU が接続されている環境では、
|
||||
# 適宜 device_id を変更する必要がある
|
||||
# ref: https://github.com/w-okada/voice-changer/issues/410#issuecomment-1627994911
|
||||
("DmlExecutionProvider", {"device_id": 0}),
|
||||
],
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user