Optimize: 推理完成后自动触发gc回收

在推理完成之后,使用gc控制器对内存进行回收。
#1218
This commit is contained in:
CyberWon 2024-06-23 12:43:10 +08:00 committed by GitHub
parent e4b17c40bf
commit c3da64b0ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,6 +1,6 @@
from copy import deepcopy from copy import deepcopy
import math import math
import os, sys import os, sys, gc
import random import random
import traceback import traceback
@ -865,7 +865,8 @@ class TTS:
self.empty_cache() self.empty_cache()
def empty_cache(self): def empty_cache(self):
try: try:
gc.collect() # 触发gc的垃圾回收。避免内存一直增长。
if "cuda" in str(self.configs.device): if "cuda" in str(self.configs.device):
torch.cuda.empty_cache() torch.cuda.empty_cache()
elif str(self.configs.device) == "mps": elif str(self.configs.device) == "mps":
@ -935,4 +936,4 @@ def speed_change(input_audio:np.ndarray, speed:float, sr:int):
# 将管道输出解码为 NumPy 数组 # 将管道输出解码为 NumPy 数组
processed_audio = np.frombuffer(out, np.int16) processed_audio = np.frombuffer(out, np.int16)
return processed_audio return processed_audio