CosyVoice commited on
Commit
8b097f7
1 Parent(s): 9ab298d

add export script

Browse files
Files changed (1) hide show
  1. cosyvoice/bin/export.py +64 -0
cosyvoice/bin/export.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2024 Alibaba Inc (authors: Xiang Lyu)
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from __future__ import print_function
16
+
17
+ import argparse
18
+ import logging
19
+ logging.getLogger('matplotlib').setLevel(logging.WARNING)
20
+ import os
21
+ import sys
22
+ ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
23
+ sys.path.append('{}/../..'.format(ROOT_DIR))
24
+ sys.path.append('{}/../../third_party/Matcha-TTS'.format(ROOT_DIR))
25
+ import torch
26
+ from cosyvoice.cli.cosyvoice import CosyVoice
27
+
28
+ def get_args():
29
+ parser = argparse.ArgumentParser(description='export your model for deployment')
30
+ parser.add_argument('--model_dir',
31
+ type=str,
32
+ default='pretrained_models/CosyVoice-300M',
33
+ help='local path')
34
+ args = parser.parse_args()
35
+ print(args)
36
+ return args
37
+
38
+ def main():
39
+ args = get_args()
40
+ logging.basicConfig(level=logging.DEBUG,
41
+ format='%(asctime)s %(levelname)s %(message)s')
42
+
43
+ torch._C._jit_set_fusion_strategy([('STATIC', 1)])
44
+ torch._C._jit_set_profiling_mode(False)
45
+ torch._C._jit_set_profiling_executor(False)
46
+
47
+ cosyvoice = CosyVoice(args.model_dir, load_script=False)
48
+
49
+ # 1. export llm text_encoder
50
+ llm_text_encoder = cosyvoice.model.llm.text_encoder.half()
51
+ script = torch.jit.script(llm_text_encoder)
52
+ script = torch.jit.freeze(script)
53
+ script = torch.jit.optimize_for_inference(script)
54
+ script.save('{}/llm.text_encoder.fp16.zip'.format(args.model_dir))
55
+
56
+ # 2. export llm llm
57
+ llm_llm = cosyvoice.model.llm.llm.half()
58
+ script = torch.jit.script(llm_llm)
59
+ script = torch.jit.freeze(script, preserved_attrs=['forward_chunk'])
60
+ script = torch.jit.optimize_for_inference(script)
61
+ script.save('{}/llm.llm.fp16.zip'.format(args.model_dir))
62
+
63
+ if __name__ == '__main__':
64
+ main()