zhoubofan.zbf commited on
Commit
53a3c1b
1 Parent(s): 5f21aef
cosyvoice/bin/export_trt.py CHANGED
@@ -38,23 +38,21 @@ def main():
38
  args = get_args()
39
 
40
  cosyvoice = CosyVoice(args.model_dir, load_jit=False, load_trt=False)
41
-
42
- flow = cosyvoice.model.flow
43
  estimator = cosyvoice.model.flow.decoder.estimator
44
 
45
  dtype = torch.float32 if not args.export_half else torch.float16
46
  device = torch.device("cuda")
47
  batch_size = 1
48
- seq_len = 1024
49
- hidden_size = flow.output_size
50
  x = torch.rand((batch_size, hidden_size, seq_len), dtype=dtype, device=device)
51
- mask = torch.zeros((batch_size, 1, seq_len), dtype=dtype, device=device)
52
  mu = torch.rand((batch_size, hidden_size, seq_len), dtype=dtype, device=device)
53
- t = torch.tensor([0.], dtype=dtype, device=device)
54
  spks = torch.rand((batch_size, hidden_size), dtype=dtype, device=device)
55
  cond = torch.rand((batch_size, hidden_size, seq_len), dtype=dtype, device=device)
56
 
57
- onnx_file_name = 'estimator_fp16.onnx' if args.export_half else 'estimator_fp32.onnx'
58
  onnx_file_path = os.path.join(args.model_dir, onnx_file_name)
59
  dummy_input = (x, mask, mu, t, spks, cond)
60
 
@@ -90,14 +88,24 @@ def main():
90
  print(f"Adding TensorRT lib path {trt_lib_path} to LD_LIBRARY_PATH.")
91
  os.environ['LD_LIBRARY_PATH'] = f"{os.environ.get('LD_LIBRARY_PATH', '')}:{trt_lib_path}"
92
 
93
- trt_file_name = 'estimator_fp16.plan' if args.export_half else 'estimator_fp32.plan'
94
  trt_file_path = os.path.join(args.model_dir, trt_file_name)
95
 
96
  trtexec_cmd = f"{tensorrt_path}/bin/trtexec --onnx={onnx_file_path} --saveEngine={trt_file_path} " \
97
  "--minShapes=x:1x80x1,mask:1x1x1,mu:1x80x1,t:1,spks:1x80,cond:1x80x1 " \
98
- "--maxShapes=x:1x80x4096,mask:1x1x4096,mu:1x80x4096,t:1,spks:1x80,cond:1x80x4096 --verbose"
 
 
 
99
 
100
  os.system(trtexec_cmd)
101
 
 
 
 
 
 
 
 
102
  if __name__ == "__main__":
103
  main()
 
38
  args = get_args()
39
 
40
  cosyvoice = CosyVoice(args.model_dir, load_jit=False, load_trt=False)
 
 
41
  estimator = cosyvoice.model.flow.decoder.estimator
42
 
43
  dtype = torch.float32 if not args.export_half else torch.float16
44
  device = torch.device("cuda")
45
  batch_size = 1
46
+ seq_len = 256
47
+ hidden_size = cosyvoice.model.flow.output_size
48
  x = torch.rand((batch_size, hidden_size, seq_len), dtype=dtype, device=device)
49
+ mask = torch.ones((batch_size, 1, seq_len), dtype=dtype, device=device)
50
  mu = torch.rand((batch_size, hidden_size, seq_len), dtype=dtype, device=device)
51
+ t = torch.rand((batch_size, ), dtype=dtype, device=device)
52
  spks = torch.rand((batch_size, hidden_size), dtype=dtype, device=device)
53
  cond = torch.rand((batch_size, hidden_size, seq_len), dtype=dtype, device=device)
54
 
55
+ onnx_file_name = 'estimator_fp32.onnx' if not args.export_half else 'estimator_fp16.onnx'
56
  onnx_file_path = os.path.join(args.model_dir, onnx_file_name)
57
  dummy_input = (x, mask, mu, t, spks, cond)
58
 
 
88
  print(f"Adding TensorRT lib path {trt_lib_path} to LD_LIBRARY_PATH.")
89
  os.environ['LD_LIBRARY_PATH'] = f"{os.environ.get('LD_LIBRARY_PATH', '')}:{trt_lib_path}"
90
 
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 "")
98
+ # /ossfs/workspace/TensorRT-10.2.0.19/bin/trtexec --onnx=estimator_fp32.onnx --saveEngine=estimator_fp32.plan --minShapes=x:1x80x1,mask:1x1x1,mu:1x80x1,t:1,spks:1x80,cond:1x80x1 --maxShapes=x:1x80x4096,mask:1x1x4096,mu:1x80x4096,t:1,spks:1x80,cond:1x80x4096 --verbose
99
+ print("execute ", trtexec_cmd)
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()
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=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,11 +39,14 @@ class CosyVoice:
39
  self.model.load('{}/llm.pt'.format(model_dir),
40
  '{}/flow.pt'.format(model_dir),
41
  '{}/hift.pt'.format(model_dir))
 
42
  if load_jit:
43
  self.model.load_jit('{}/llm.text_encoder.fp16.zip'.format(model_dir),
44
  '{}/llm.llm.fp16.zip'.format(model_dir))
 
45
  if load_trt:
46
  self.model.load_trt(model_dir, use_fp16)
 
47
  del configs
48
 
49
  def list_avaliable_spks(self):
 
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
  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))
46
+
47
  if load_trt:
48
  self.model.load_trt(model_dir, use_fp16)
49
+
50
  del configs
51
 
52
  def list_avaliable_spks(self):
cosyvoice/flow/flow.py CHANGED
@@ -107,7 +107,7 @@ class MaskedDiffWithXvec(torch.nn.Module):
107
  # concat text and prompt_text
108
  token_len1, token_len2 = prompt_token.shape[1], token.shape[1]
109
  token, token_len = torch.concat([prompt_token, token], dim=1), prompt_token_len + token_len
110
- mask = (~make_pad_mask(token_len)).float().unsqueeze(-1).to(embedding)
111
  token = self.input_embedding(torch.clamp(token, min=0)) * mask
112
 
113
  # text encode
 
107
  # concat text and prompt_text
108
  token_len1, token_len2 = prompt_token.shape[1], token.shape[1]
109
  token, token_len = torch.concat([prompt_token, token], dim=1), prompt_token_len + token_len
110
+ mask = (~make_pad_mask(token_len)).to(embedding.dtype).unsqueeze(-1).to(embedding)
111
  token = self.input_embedding(torch.clamp(token, min=0)) * mask
112
 
113
  # text encode
cosyvoice/flow/flow_matching.py CHANGED
@@ -32,6 +32,7 @@ class ConditionalCFM(BASECFM):
32
 
33
  self.estimator_context = None
34
  self.estimator_engine = None
 
35
 
36
  @torch.inference_mode()
37
  def forward(self, mu, mask, n_timesteps, temperature=1.0, spks=None, cond=None):
@@ -123,6 +124,41 @@ class ConditionalCFM(BASECFM):
123
  self.estimator_context.execute_async_v3(stream_handle=handle)
124
  return ret
125
  else:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  return self.estimator.forward(x, mask, mu, t, spks, cond)
127
 
128
  def compute_loss(self, x1, mask, mu, spks=None, cond=None):
 
32
 
33
  self.estimator_context = None
34
  self.estimator_engine = None
35
+ self.is_saved = None
36
 
37
  @torch.inference_mode()
38
  def forward(self, mu, mask, n_timesteps, temperature=1.0, spks=None, cond=None):
 
124
  self.estimator_context.execute_async_v3(stream_handle=handle)
125
  return ret
126
  else:
127
+
128
+ if self.is_saved == None:
129
+ self.is_saved = True
130
+ output = self.estimator.forward(x, mask, mu, t, spks, cond)
131
+ torch.save(x, "x.pt")
132
+ torch.save(mask, "mask.pt")
133
+ torch.save(mu, "mu.pt")
134
+ torch.save(t, "t.pt")
135
+ torch.save(spks, "spks.pt")
136
+ torch.save(cond, "cond.pt")
137
+ torch.save(output, "output.pt")
138
+ dummy_input = (x, mask, mu, t, spks, cond)
139
+ torch.onnx.export(
140
+ self.estimator,
141
+ dummy_input,
142
+ "estimator_fp32.onnx",
143
+ export_params=True,
144
+ opset_version=17,
145
+ do_constant_folding=True,
146
+ input_names=['x', 'mask', 'mu', 't', 'spks', 'cond'],
147
+ output_names=['output'],
148
+ dynamic_axes={
149
+ 'x': {2: 'seq_len'},
150
+ 'mask': {2: 'seq_len'},
151
+ 'mu': {2: 'seq_len'},
152
+ 'cond': {2: 'seq_len'},
153
+ 'output': {2: 'seq_len'},
154
+ }
155
+ )
156
+ # print("x, x.shape", x, x.shape)
157
+ # print("mask, mask.shape", mask, mask.shape)
158
+ # print("mu, mu.shape", mu, mu.shape)
159
+ # print("t, t.shape", t, t.shape)
160
+ # print("spks, spks.shape", spks, spks.shape)
161
+ # print("cond, cond.shape", cond, cond.shape)
162
  return self.estimator.forward(x, mask, mu, t, spks, cond)
163
 
164
  def compute_loss(self, x1, mask, mu, spks=None, cond=None):