export_jit.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. from __future__ import print_function
  15. import argparse
  16. import logging
  17. logging.getLogger('matplotlib').setLevel(logging.WARNING)
  18. import os
  19. import sys
  20. import torch
  21. ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
  22. sys.path.append('{}/../..'.format(ROOT_DIR))
  23. sys.path.append('{}/../../third_party/Matcha-TTS'.format(ROOT_DIR))
  24. from cosyvoice.cli.cosyvoice import CosyVoice, CosyVoice2
  25. def get_args():
  26. parser = argparse.ArgumentParser(description='export your model for deployment')
  27. parser.add_argument('--model_dir',
  28. type=str,
  29. default='pretrained_models/CosyVoice-300M',
  30. help='local path')
  31. args = parser.parse_args()
  32. print(args)
  33. return args
  34. def get_optimized_script(model, preserved_attrs=[]):
  35. script = torch.jit.script(model)
  36. if preserved_attrs != []:
  37. script = torch.jit.freeze(script, preserved_attrs=preserved_attrs)
  38. else:
  39. script = torch.jit.freeze(script)
  40. script = torch.jit.optimize_for_inference(script)
  41. return script
  42. def main():
  43. args = get_args()
  44. logging.basicConfig(level=logging.DEBUG,
  45. format='%(asctime)s %(levelname)s %(message)s')
  46. torch._C._jit_set_fusion_strategy([('STATIC', 1)])
  47. torch._C._jit_set_profiling_mode(False)
  48. torch._C._jit_set_profiling_executor(False)
  49. try:
  50. model = CosyVoice(args.model_dir)
  51. except Exception:
  52. try:
  53. model = CosyVoice2(args.model_dir)
  54. except Exception:
  55. raise TypeError('no valid model_type!')
  56. if not isinstance(model, CosyVoice2):
  57. # 1. export llm text_encoder
  58. llm_text_encoder = model.model.llm.text_encoder
  59. script = get_optimized_script(llm_text_encoder)
  60. script.save('{}/llm.text_encoder.fp32.zip'.format(args.model_dir))
  61. script = get_optimized_script(llm_text_encoder.half())
  62. script.save('{}/llm.text_encoder.fp16.zip'.format(args.model_dir))
  63. # 2. export llm llm
  64. llm_llm = model.model.llm.llm
  65. script = get_optimized_script(llm_llm, ['forward_chunk'])
  66. script.save('{}/llm.llm.fp32.zip'.format(args.model_dir))
  67. script = get_optimized_script(llm_llm.half(), ['forward_chunk'])
  68. script.save('{}/llm.llm.fp16.zip'.format(args.model_dir))
  69. # 3. export flow encoder
  70. flow_encoder = model.model.flow.encoder
  71. script = get_optimized_script(flow_encoder)
  72. script.save('{}/flow.encoder.fp32.zip'.format(args.model_dir))
  73. script = get_optimized_script(flow_encoder.half())
  74. script.save('{}/flow.encoder.fp16.zip'.format(args.model_dir))
  75. if __name__ == '__main__':
  76. main()