Fix: Ensure memory is cleared on unload
This commit is contained in:
@@ -237,9 +237,9 @@ def unload_model(language: Languages) -> None:
|
|||||||
|
|
||||||
if language in __loaded_models:
|
if language in __loaded_models:
|
||||||
del __loaded_models[language]
|
del __loaded_models[language]
|
||||||
gc.collect()
|
|
||||||
if torch.cuda.is_available():
|
if torch.cuda.is_available():
|
||||||
torch.cuda.empty_cache()
|
torch.cuda.empty_cache()
|
||||||
|
gc.collect()
|
||||||
logger.info(f"Unloaded the {language.name} BERT model")
|
logger.info(f"Unloaded the {language.name} BERT model")
|
||||||
|
|
||||||
|
|
||||||
@@ -256,8 +256,6 @@ def unload_tokenizer(language: Languages) -> None:
|
|||||||
if language in __loaded_tokenizers:
|
if language in __loaded_tokenizers:
|
||||||
del __loaded_tokenizers[language]
|
del __loaded_tokenizers[language]
|
||||||
gc.collect()
|
gc.collect()
|
||||||
if torch.cuda.is_available():
|
|
||||||
torch.cuda.empty_cache()
|
|
||||||
logger.info(f"Unloaded the {language.name} BERT tokenizer")
|
logger.info(f"Unloaded the {language.name} BERT tokenizer")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import gc
|
||||||
import time
|
import time
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import TYPE_CHECKING, Any, Optional, Sequence, Union
|
from typing import TYPE_CHECKING, Any, Optional, Sequence, Union
|
||||||
@@ -226,13 +227,30 @@ class TTSModel:
|
|||||||
def unload(self) -> None:
|
def unload(self) -> None:
|
||||||
"""
|
"""
|
||||||
音声合成モデルをデバイスからアンロードする。
|
音声合成モデルをデバイスからアンロードする。
|
||||||
|
PyTorch モデルの場合は CUDA メモリも解放される。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import torch
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
|
||||||
|
# PyTorch 推論時
|
||||||
if self.net_g is not None:
|
if self.net_g is not None:
|
||||||
|
del self.net_g
|
||||||
self.net_g = None
|
self.net_g = None
|
||||||
|
|
||||||
|
# CUDA キャッシュをクリア
|
||||||
|
if torch.cuda.is_available():
|
||||||
|
torch.cuda.empty_cache()
|
||||||
|
|
||||||
|
# ONNX 推論時
|
||||||
if self.onnx_session is not None:
|
if self.onnx_session is not None:
|
||||||
|
del self.onnx_session
|
||||||
self.onnx_session = None
|
self.onnx_session = None
|
||||||
|
|
||||||
|
gc.collect()
|
||||||
|
logger.info(f"Model unloaded successfully ({time.time() - start_time:.2f}s)")
|
||||||
|
|
||||||
def get_style_vector(self, style_id: int, weight: float = 1.0) -> NDArray[Any]:
|
def get_style_vector(self, style_id: int, weight: float = 1.0) -> NDArray[Any]:
|
||||||
"""
|
"""
|
||||||
スタイルベクトルを取得する。
|
スタイルベクトルを取得する。
|
||||||
@@ -443,8 +461,7 @@ class TTSModel:
|
|||||||
|
|
||||||
# 改行ごとに分割して音声を生成
|
# 改行ごとに分割して音声を生成
|
||||||
else:
|
else:
|
||||||
texts = text.split("\n")
|
texts = [t for t in text.split("\n") if t != ""]
|
||||||
texts = [t for t in texts if t != ""]
|
|
||||||
audios = []
|
audios = []
|
||||||
with torch.no_grad():
|
with torch.no_grad():
|
||||||
for i, t in enumerate(texts):
|
for i, t in enumerate(texts):
|
||||||
@@ -504,8 +521,7 @@ class TTSModel:
|
|||||||
|
|
||||||
# 改行ごとに分割して音声を生成
|
# 改行ごとに分割して音声を生成
|
||||||
else:
|
else:
|
||||||
texts = text.split("\n")
|
texts = [t for t in text.split("\n") if t != ""]
|
||||||
texts = [t for t in texts if t != ""]
|
|
||||||
audios = []
|
audios = []
|
||||||
for i, t in enumerate(texts):
|
for i, t in enumerate(texts):
|
||||||
audios.append(
|
audios.append(
|
||||||
|
|||||||
Reference in New Issue
Block a user