zhoubofan.zbf commited on
Commit
2940836
1 Parent(s): 6e7f5b9
cosyvoice/bin/export_trt.py CHANGED
@@ -11,7 +11,7 @@ except ImportError:
11
  error_msg_zh = [
12
  "step.1 下载 tensorrt .tar.gz 压缩包并解压,下载地址: https://developer.nvidia.com/tensorrt/download/10x",
13
  "step.2 使用 tensorrt whl 包进行安装根据 python 版本对应进行安装,如 pip install ${TensorRT-Path}/python/tensorrt-10.2.0-cp38-none-linux_x86_64.whl",
14
- "step.3 将 tensorrt 的 lib 路径添加进环境变量中,export LD_LIBRARY_PATH=${TensorRT-Path}/lib/"
15
  ]
16
  print("\n".join(error_msg_zh))
17
  sys.exit(1)
@@ -23,7 +23,7 @@ def get_args():
23
  parser = argparse.ArgumentParser(description='Export your model for deployment')
24
  parser.add_argument('--model_dir',
25
  type=str,
26
- default='pretrained_models/CosyVoice-300M',
27
  help='Local path to the model directory')
28
 
29
  parser.add_argument('--export_half',
@@ -91,7 +91,8 @@ def main():
91
  trt_file_name = 'estimator_fp32.plan' if not args.export_half else 'estimator_fp16.plan'
92
  trt_file_path = os.path.join(args.model_dir, trt_file_name)
93
 
94
- trtexec_cmd = f"{tensorrt_path}/bin/trtexec --onnx={onnx_file_path} --saveEngine={trt_file_path} " \
 
95
  "--minShapes=x:1x80x1,mask:1x1x1,mu:1x80x1,t:1,spks:1x80,cond:1x80x1 " \
96
  "--maxShapes=x:1x80x4096,mask:1x1x4096,mu:1x80x4096,t:1,spks:1x80,cond:1x80x4096 --verbose " + \
97
  ("--fp16" if args.export_half else "")
@@ -100,12 +101,12 @@ def main():
100
 
101
  os.system(trtexec_cmd)
102
 
103
- print("x.shape", x.shape)
104
- print("mask.shape", mask.shape)
105
- print("mu.shape", mu.shape)
106
- print("t.shape", t.shape)
107
- print("spks.shape", spks.shape)
108
- print("cond.shape", cond.shape)
109
 
110
  if __name__ == "__main__":
111
  main()
 
11
  error_msg_zh = [
12
  "step.1 下载 tensorrt .tar.gz 压缩包并解压,下载地址: https://developer.nvidia.com/tensorrt/download/10x",
13
  "step.2 使用 tensorrt whl 包进行安装根据 python 版本对应进行安装,如 pip install ${TensorRT-Path}/python/tensorrt-10.2.0-cp38-none-linux_x86_64.whl",
14
+ "step.3 将 tensorrt 的 lib 路径添加进环境变量中,export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${TensorRT-Path}/lib/"
15
  ]
16
  print("\n".join(error_msg_zh))
17
  sys.exit(1)
 
23
  parser = argparse.ArgumentParser(description='Export your model for deployment')
24
  parser.add_argument('--model_dir',
25
  type=str,
26
+ default='pretrained_models/CosyVoice-300M-SFT',
27
  help='Local path to the model directory')
28
 
29
  parser.add_argument('--export_half',
 
91
  trt_file_name = 'estimator_fp32.plan' if not args.export_half else 'estimator_fp16.plan'
92
  trt_file_path = os.path.join(args.model_dir, trt_file_name)
93
 
94
+ trtexec_bin = os.path.join(tensorrt_path, 'bin/trtexec')
95
+ trtexec_cmd = f"{trtexec_bin} --onnx={onnx_file_path} --saveEngine={trt_file_path} " \
96
  "--minShapes=x:1x80x1,mask:1x1x1,mu:1x80x1,t:1,spks:1x80,cond:1x80x1 " \
97
  "--maxShapes=x:1x80x4096,mask:1x1x4096,mu:1x80x4096,t:1,spks:1x80,cond:1x80x4096 --verbose " + \
98
  ("--fp16" if args.export_half else "")
 
101
 
102
  os.system(trtexec_cmd)
103
 
104
+ # print("x.shape", x.shape)
105
+ # print("mask.shape", mask.shape)
106
+ # print("mu.shape", mu.shape)
107
+ # print("t.shape", t.shape)
108
+ # print("spks.shape", spks.shape)
109
+ # print("cond.shape", cond.shape)
110
 
111
  if __name__ == "__main__":
112
  main()
cosyvoice/cli/cosyvoice.py CHANGED
@@ -21,7 +21,7 @@ from cosyvoice.utils.file_utils import logging
21
 
22
  class CosyVoice:
23
 
24
- def __init__(self, model_dir, load_jit=True, load_trt=False, use_fp16=False):
25
  instruct = True if '-Instruct' in model_dir else False
26
  self.model_dir = model_dir
27
  if not os.path.exists(model_dir):
@@ -39,7 +39,7 @@ class CosyVoice:
39
  self.model.load('{}/llm.pt'.format(model_dir),
40
  '{}/flow.pt'.format(model_dir),
41
  '{}/hift.pt'.format(model_dir))
42
- load_jit = False
43
  if load_jit:
44
  self.model.load_jit('{}/llm.text_encoder.fp16.zip'.format(model_dir),
45
  '{}/llm.llm.fp16.zip'.format(model_dir))
 
21
 
22
  class CosyVoice:
23
 
24
+ def __init__(self, model_dir, load_jit=True, load_trt=True, use_fp16=False):
25
  instruct = True if '-Instruct' in model_dir else False
26
  self.model_dir = model_dir
27
  if not os.path.exists(model_dir):
 
39
  self.model.load('{}/llm.pt'.format(model_dir),
40
  '{}/flow.pt'.format(model_dir),
41
  '{}/hift.pt'.format(model_dir))
42
+
43
  if load_jit:
44
  self.model.load_jit('{}/llm.text_encoder.fp16.zip'.format(model_dir),
45
  '{}/llm.llm.fp16.zip'.format(model_dir))
cosyvoice/cli/model.py CHANGED
@@ -83,7 +83,8 @@ class CosyVoiceModel:
83
  with open(trt_file_path, 'rb') as f:
84
  serialized_engine = f.read()
85
  engine = runtime.deserialize_cuda_engine(serialized_engine)
86
- self.flow.decoder.estimator = engine.create_execution_context()
 
87
 
88
  def llm_job(self, text, prompt_text, llm_prompt_speech_token, llm_embedding, uuid):
89
  with self.llm_context:
 
83
  with open(trt_file_path, 'rb') as f:
84
  serialized_engine = f.read()
85
  engine = runtime.deserialize_cuda_engine(serialized_engine)
86
+ self.flow.decoder.estimator_context = engine.create_execution_context()
87
+ self.flow.decoder.estimator = None
88
 
89
  def llm_job(self, text, prompt_text, llm_prompt_speech_token, llm_embedding, uuid):
90
  with self.llm_context:
cosyvoice/flow/flow_matching.py CHANGED
@@ -99,10 +99,10 @@ class ConditionalCFM(BASECFM):
99
 
100
  def forward_estimator(self, x, mask, mu, t, spks, cond):
101
 
102
- if not isinstance(self.estimator, torch.nn.Module):
103
  return self.estimator.forward(x, mask, mu, t, spks, cond)
104
-
105
  else:
 
106
  assert self.training is False, 'tensorrt cannot be used in training'
107
  bs = x.shape[0]
108
  hs = x.shape[1]
@@ -119,10 +119,10 @@ class ConditionalCFM(BASECFM):
119
  names = ['x', 'mask', 'mu', 't', 'spks', 'cond', 'estimator_out']
120
 
121
  for i in range(len(bindings)):
122
- self.estimator.set_tensor_address(names[i], bindings[i])
123
 
124
  handle = torch.cuda.current_stream().cuda_stream
125
- self.estimator.execute_async_v3(stream_handle=handle)
126
  return ret
127
 
128
  def compute_loss(self, x1, mask, mu, spks=None, cond=None):
 
99
 
100
  def forward_estimator(self, x, mask, mu, t, spks, cond):
101
 
102
+ if self.estimator is not None:
103
  return self.estimator.forward(x, mask, mu, t, spks, cond)
 
104
  else:
105
+ print("-----------")
106
  assert self.training is False, 'tensorrt cannot be used in training'
107
  bs = x.shape[0]
108
  hs = x.shape[1]
 
119
  names = ['x', 'mask', 'mu', 't', 'spks', 'cond', 'estimator_out']
120
 
121
  for i in range(len(bindings)):
122
+ self.estimator_context.set_tensor_address(names[i], bindings[i])
123
 
124
  handle = torch.cuda.current_stream().cuda_stream
125
+ self.estimator_context.execute_async_v3(stream_handle=handle)
126
  return ret
127
 
128
  def compute_loss(self, x1, mask, mu, spks=None, cond=None):