dlxj commited on
Commit
d0768a8
·
1 Parent(s): 448d4f1
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +7 -0
  2. .gitignore +40 -0
  3. .python-version +1 -0
  4. DISCLAIMER +43 -0
  5. LICENSE +57 -0
  6. LICENSE_ZH.txt +52 -0
  7. MANIFEST.in +3 -0
  8. README.md +492 -0
  9. archive/README_INDEXTTS_1_5.md +247 -0
  10. assets/IndexTTS.png +3 -0
  11. assets/IndexTTS2-video-pic.png +3 -0
  12. assets/IndexTTS2.mp4 +3 -0
  13. assets/IndexTTS2.png +3 -0
  14. assets/IndexTTS2_banner.png +3 -0
  15. assets/img.png +3 -0
  16. assets/index_icon.png +3 -0
  17. check_imports.py +18 -0
  18. checkpoints/config.yaml +120 -0
  19. docs/README_zh.md +399 -0
  20. freeze.txt +0 -0
  21. indextts/BigVGAN/ECAPA_TDNN.py +656 -0
  22. indextts/BigVGAN/__init__.py +0 -0
  23. indextts/BigVGAN/activations.py +122 -0
  24. indextts/BigVGAN/alias_free_activation/__init__.py +0 -0
  25. indextts/BigVGAN/alias_free_activation/cuda/.gitignore +1 -0
  26. indextts/BigVGAN/alias_free_activation/cuda/__init__.py +0 -0
  27. indextts/BigVGAN/alias_free_activation/cuda/activation1d.py +76 -0
  28. indextts/BigVGAN/alias_free_activation/cuda/anti_alias_activation.cpp +23 -0
  29. indextts/BigVGAN/alias_free_activation/cuda/anti_alias_activation_cuda.cu +256 -0
  30. indextts/BigVGAN/alias_free_activation/cuda/compat.h +29 -0
  31. indextts/BigVGAN/alias_free_activation/cuda/load.py +121 -0
  32. indextts/BigVGAN/alias_free_activation/cuda/type_shim.h +92 -0
  33. indextts/BigVGAN/alias_free_activation/torch/__init__.py +6 -0
  34. indextts/BigVGAN/alias_free_activation/torch/act.py +31 -0
  35. indextts/BigVGAN/alias_free_activation/torch/filter.py +102 -0
  36. indextts/BigVGAN/alias_free_activation/torch/resample.py +58 -0
  37. indextts/BigVGAN/alias_free_torch/__init__.py +6 -0
  38. indextts/BigVGAN/alias_free_torch/act.py +29 -0
  39. indextts/BigVGAN/alias_free_torch/filter.py +96 -0
  40. indextts/BigVGAN/alias_free_torch/resample.py +49 -0
  41. indextts/BigVGAN/bigvgan.py +534 -0
  42. indextts/BigVGAN/models.py +451 -0
  43. indextts/BigVGAN/nnet/CNN.py +546 -0
  44. indextts/BigVGAN/nnet/__init__.py +0 -0
  45. indextts/BigVGAN/nnet/linear.py +89 -0
  46. indextts/BigVGAN/nnet/normalization.py +670 -0
  47. indextts/BigVGAN/utils.py +101 -0
  48. indextts/__init__.py +0 -0
  49. indextts/accel/__init__.py +9 -0
  50. indextts/accel/accel_engine.py +609 -0
.gitattributes CHANGED
@@ -1,4 +1,9 @@
 
1
  *.7z filter=lfs diff=lfs merge=lfs -text
 
 
 
 
2
  *.arrow filter=lfs diff=lfs merge=lfs -text
3
  *.bin filter=lfs diff=lfs merge=lfs -text
4
  *.bz2 filter=lfs diff=lfs merge=lfs -text
@@ -57,3 +62,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
57
  # Video files - compressed
58
  *.mp4 filter=lfs diff=lfs merge=lfs -text
59
  *.webm filter=lfs diff=lfs merge=lfs -text
 
 
 
1
+ *.t7 filter=lfs diff=lfs merge=lfs -text
2
  *.7z filter=lfs diff=lfs merge=lfs -text
3
+ *.dll filter=lfs diff=lfs merge=lfs -text
4
+ *.so filter=lfs diff=lfs merge=lfs -text
5
+ *.exe filter=lfs diff=lfs merge=lfs -text
6
+ *.icns filter=lfs diff=lfs merge=lfs -text
7
  *.arrow filter=lfs diff=lfs merge=lfs -text
8
  *.bin filter=lfs diff=lfs merge=lfs -text
9
  *.bz2 filter=lfs diff=lfs merge=lfs -text
 
62
  # Video files - compressed
63
  *.mp4 filter=lfs diff=lfs merge=lfs -text
64
  *.webm filter=lfs diff=lfs merge=lfs -text
65
+
66
+
.gitignore ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ out.wav
2
+ # Development Tools.
3
+ .mypy_cache/
4
+ .ruff_cache/
5
+ __pycache__/
6
+ .idea/
7
+ .vscode/
8
+
9
+ # Environments.
10
+ .venv*/
11
+ venv*/
12
+ conda_env*/
13
+
14
+ # Python Bytecode.
15
+ *.py[cod]
16
+
17
+ # Distribution/Packaging.
18
+ /build/
19
+ /dist/
20
+ *.egg-info/
21
+ .pypirc
22
+
23
+ # Operating System Junk.
24
+ *.DS_Store
25
+ Thumbs.db
26
+ desktop.ini
27
+
28
+ # IndexTTS.
29
+ /cache/
30
+ /checkpoints/*
31
+ !/checkpoints/*.yaml
32
+ /outputs/
33
+ *processed_data/
34
+ DeepSpeed/
35
+ *datasets/
36
+ hf_cache/
37
+ *_dataset/
38
+ *trained_ckpts*
39
+ prompts/
40
+ *.whl
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.10
DISCLAIMER ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ TTS语音合成技术免责声明
2
+
3
+ 1. 总则
4
+ 本声明适用于 Index-TTS(以下简称"本项目")的所有用户和使用者。使用本项目即表示您已阅读、理解并同意遵守本免责声明的全部内容。
5
+
6
+ 2. 使用限制
7
+ 2.1 本项目仅供用户进行技术研究、学习和合法的创意应用,不得用于任何违反法律法规的活动。
8
+
9
+ 2.2 用户不得使用本项目:
10
+ a) 合成政治人物、公众人物或任何未经授权的个人声音;
11
+ b) 创建诋毁、侮辱、歧视或损害他人名誉和权益的内容;
12
+ c) 进行欺诈、身份盗用或任何形式的违法活动;
13
+ d) 传播虚假信息或制造社会恐慌;
14
+ e) 侵犯他人知识产权、肖像权或隐私权;
15
+ f) 未经授权将合成声音用于商业目的;
16
+ g) 违反特定行业(如金融、医疗等)的法规要求;
17
+ h) 创建或使用涉及未成年人的不当声音内容;
18
+ i) 制作可能威胁国家安全的内容;
19
+ j) 违反任何地区关于深度伪造技术的法律法规。
20
+
21
+ 3. 知识产权与授权
22
+ 3.1 本项目以[开源许可证类型]许可证开源。
23
+ 3.2 用户在使用本项目过程中产生的所有内容及其法律责任由用户自行承担。
24
+
25
+ 4. 责任限制
26
+ 4.1 项目开发者不对用户使用本项目所产生的任何直接或间接后果承担责任。
27
+ 4.2 项目开发者不保证本项目的功能满足用户的所有需求,也不保证运行不会中断或出错。
28
+ 4.3 用户因使用本项目而产生的任何法律纠纷、损失或损害,项目开发者概不负责。
29
+
30
+ 5. 法律适用
31
+ 5.1 本免责声明受[国家/地区]法律管辖。
32
+ 5.2 如本声明的任何条款与适用法律相抵触,则以适用法律为准。
33
+
34
+ 6. 声明更新
35
+ 6.1 项目开发者保留随时更新本免责声明的权利,更新后的声明自发布之日起生效。
36
+ 6.2 用户应定期查阅本声明以了解任何变更。
37
+
38
+ 7. 其他条款
39
+ 7.1 用户在使用本项目前,应确保其使用行为符合所在地区的法律法规。
40
+ 7.2 如用户对本项目的使用引起任何法律纠纷,用户应积极配合相关调查并承担相应责任。
41
+
42
+ 最后更新日期:2025.3.17
43
+ 开发者:Bilibili Index Team
LICENSE ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ bilibili Model Use License Agreement
2
+
3
+ By clicking “I agree” to this bilibili Model Use License Agreement (“this Agreement”) , or by otherwise using any portion or element of the Model or any Derivative Work, you will be deemed to have recognized and accepted the content of this Agreement, which is effective immediately. If you do not agree to this Agreement, you must immediately cease all use and permanently delete the Model and any Derivative Works.
4
+
5
+ 1. Definitions
6
+ 1.1 “This Agreement”: means the bilibili Model Use License Agreement, including all of its terms and conditions.
7
+ 1.2 “We”, “us”, or “our”: means bilibili , the original right-holder of the Model.
8
+ 1.3 “You”: means any natural person or legal entity exercising rights granted by this Agreement and/or using the Model for any purpose and in any field of use.
9
+ 1.4 “Model”: means the artificial-intelligence model named “bilibili indextts2”, including but not limited to model weights and final code, in each case only to the extent that such components are published by us at https://github.com/index-tts/index-tts.
10
+ 1.5 “Derivative Work”: means any derivative of the Model, including without limitation:
11
+  (i) any modification of the Model, model outputs, or their derivatives;
12
+  (ii) any work based on the Model, model outputs, or their derivatives;
13
+  (iii) any other machine learning model which is created by re-training, fine-tuning, quantizing, LoRA, parameter-efficient fine-tuning, or any other method involving incremental weights or merged checkpoints, in each case based on the Model, model outputs, or their derivatives.
14
+ 1.6 “Use”: means downloading, copying, training, modifying, creating Derivative Works, distributing, publishing, running, fine-tuning, publicly displaying, communicating to the public, or otherwise exploiting the Model or any Derivative Work.
15
+
16
+ 2. Scope of License and Restrictions
17
+ 2.1 Subject to the terms and conditions of this Agreement, we grant you a worldwide, non-exclusive, non-transferable, royalty-free limited license to Use the Model or any Derivative Work based on the intellectual properties or other rights owned by Us embodied in the Model or any Derivative Work.
18
+ 2.2 If You intend to Use, or have already Used, the Model or any Derivative Work, and either (i) your or any of your Affiliates’ products or services had more than 100 million monthly active users in the immediately preceding calendar month, or (ii) your or any of your Affiliates’ annual revenue in the immediately preceding calendar year exceeded RMB 1 billion, You must request a separated license from us, which We may grant to You in our sole discretion. You are not authorized to exercise any of the rights under this Agreement unless and until We have expressly granted You such rights in writing.
19
+ 2.3 This Agreement is an open-source license for the Model in which we possess intellectual properties and other rights. It governs your Use of the Model only and does not limit any rights that we have regarding the Model.
20
+
21
+ 3. Disclaimer and Risk Allocation
22
+ 3.1 The Model and any outputs generated thereby are provided “AS IS,” without warranty of any kind, express or implied, including but not limited to warranties of merchantability, fitness for a particular purpose, non-infringement, absence of errors or omissions, continuity, accuracy, reliability, or stability. You are solely responsible for determining the appropriateness of using or redistributing the Model and assume all risks associated with exercising any rights granted under this Agreement.
23
+ 3.2 You shall bear sole responsibility for any infringement, illegality, breach of contract, damages, fines, regulatory investigations, or other liabilities (including, without limitation, infringement of third-party patents, copyrights, trademarks, trade secrets, personality rights, data-protection rights, or any other rights) arising out of or related to your Use of the Model or any outputs generated thereby. We assume no joint, several, supplementary, or advance payment liability.
24
+ 3.3 Under no circumstances shall we be liable to you or any third party for any direct, indirect, incidental, special, punitive, or consequential damages (including, without limitation, loss of data, business interruption, or loss of profits) arising out of or related to the Use of the Model, even if we have been advised of the possibility of such damages.
25
+ 3.4 Additional Obligations for You and Downstream Recipients
26
+ a) You must ensure that any downstream recipient of the Model or any Derivative Work that you distribute complies with this Agreement, and you must impose appropriate contractual terms on such downstream recipients. If any downstream recipient breaches this Agreement, you shall be responsible for the consequences thereof.
27
+ b) You must retain all original copyright notices and a copy of this Agreement in every copy of the Model or any Derivative Work that you Use.
28
+ c) You may not Use the bilibili indextts2 or any Derivative Work to improve any AI model, except for the bilibili indextts2 itself, its Derivative Works,or non-commercial AI models.
29
+
30
+ 4. Compliance Obligations
31
+ 4.1 Usage Restrictions
32
+ a) If you distribute a Derivative Work, you must clearly state in the distribution page or accompanying documentation: “Any modifications made to the original model in this Derivative Work are not endorsed, warranted, or guaranteed by the original right-holder of the original model, and the original right-holder disclaims all liability related to this Derivative Work.”
33
+ b) If your Use of the Model or any Derivative Work incorporates any third-party data or weights, you must obtain all necessary authorizations on your own and bear full responsibility for compliance.
34
+ c) You may not Use the Model or any Derivative Work for any purpose that violates the laws or regulatory requirements of the jurisdiction where the outputs and/or the Model are generated or used (including, without limitation, generating false information, discriminatory content, or content that infringes privacy).
35
+ d) If the Model or any Derivative Work is capable of generating content, you must ensure that such content does not violate the laws or regulatory requirements of the applicable jurisdiction (including, without limitation, generating false information, discriminatory content, or content that infringes privacy).
36
+ 4.2 Prohibited High-Risk Use
37
+ You must ensure that the Model and any Derivative Work are not deployed, directly or indirectly, in high-risk scenarios such as medical diagnosis, autonomous driving, military applications, critical-infrastructure control, large-scale biometric surveillance, or automated decision-making (e.g., credit or employment evaluations). If you insist on such deployment, you must independently complete all compliance obligations under applicable laws and regulations (including but not limited to GDPR, CCPA, HIPAA, export-control laws, and AI-specific regulations), and we shall bear no liability for any consequences arising therefrom.
38
+ 4.3 Infringement Liability
39
+ Should any third party raise claims against you with respect to any Derivative Work you develop or your Use of the Model or any Derivative Work, you shall bear full and independent responsibility for defending against and resolving such claims. If your actions cause us to incur any third-party claims, administrative penalties, or other losses, you shall indemnify us for all losses we thereby suffer, including but not limited to attorney fees, litigation costs, damages, and fines, and shall take all necessary measures to eliminate any adverse impact on us.
40
+
41
+ 5. Reserved Rights
42
+ 5.1 We reserve the right to revoke the license granted to you under this Agreement in the event of your breach. Upon revocation, you must immediately cease all Use and permanently delete all copies of the Model and any Derivative Work. Sections 3 and 6 of this Agreement shall survive termination of this Agreement under this circumstance.
43
+ 5.2 Nothing in this Agreement grants you any right to use our trade names, trademarks, service marks, or product names, except as reasonably and customarily required to describe the origin of the Model or any Derivative Work—such as reproducing the content of a NOTICE file under Section 3.4 of this Agreement.
44
+ 5.3 If you or any of your Affiliates institutes or participates in any legal proceeding (including any cross-claim or counterclaim in a lawsuit) against us or any of our Affiliates, alleging that the Model or any output or any portion thereof infringes any intellectual property or other rights that you own or control, all licenses granted to you under this Agreement shall terminate automatically as of the date such proceeding is filed.
45
+
46
+ 6. Governing Law and Dispute Resolution
47
+ 6.1 This Agreement shall be governed by and construed in accordance with the laws of the People’s Republic of China.
48
+ 6.2 In the event of any dispute arising out of or in connection with this Agreement, the parties shall first attempt to resolve such dispute through friendly negotiation. If negotiation fails, the dispute shall be submitted to the Shanghai Arbitration Commission for arbitration in accordance with its then-effective arbitration rules. The arbitration award shall be final and binding on both parties. The prevailing party shall be entitled to recover reasonable costs, including notarization and investigation fees, arbitration costs, attorneys’ fees, and travel expenses.
49
+
50
+ 7. Severability
51
+ If any provision of this Agreement is held to be invalid or unenforceable, the remaining provisions shall remain in full force and effect. The invalid or unenforceable provision shall be replaced with a valid and enforceable provision that, to the maximum extent permitted by law, most closely reflects the original intent of the invalid or unenforceable provision.
52
+
53
+ 8. Version Updates
54
+ We may release new versions of the AI Model Use License Agreement. Any new version will apply only to Uses occurring after the date of its release. If you obtained the Model under an earlier version, the new version will not have retroactive effect; nevertheless, you are encouraged to adopt the new version voluntarily.
55
+
56
+ 9. Language Version
57
+ In the event of any discrepancy or conflict between the English-language version set forth above and the Chinese-language version of this bilibili Model Use License Agreement, the Chinese-language version shall prevail for all purposes and shall govern the rights and obligations of the parties.
LICENSE_ZH.txt ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ bilibili模型使用许可协议
2
+
3
+ 若您点击同意《bilibili模型使用许可协议》(“本协议”),或使用我方模型或衍生品的任何部分或元素,即视为您已确认并接受本协议内容,本协议立即生效。若您不同意本协议,应立即停止使用并删除模型及衍生品。
4
+
5
+ 1.定义
6
+ 1.1 本协议:指《bilibili 模型使用许可协议》,包括本协议所规定的所有条款和条件。
7
+ 1.2 我方:指bilibili即模型的原始权利人。
8
+ 1.3 您:指行使本许可协议授予的权利和/或使用“模型”的自然人或法人实体。
9
+ 1.4 模型:指名为“bilibili indextts2”的AI模型,包括模型权重、最终代码等组件,具体范围以我方在https://github.com/index-tts/index-tts发布的组件为限。
10
+ 1.5 衍生品:指模型的衍生品,包括但不限于:(i)对模型、模型输出及其衍生品的修改;(ii)基于模型、模型输出及其衍生品的创作;(iii)对模型、模型输出及其衍生品再训练、微调、量化、LoRA、参数高效微调、以任何增量权重或合并的检查点等方式创建的任何模型。
11
+ 1.6 使用:指通过下载、复制、训练、修改、创作衍生品、分发、发布、运行、微调、公开展示、传播或以其他方式利用本模型或其衍生品的行为。
12
+
13
+ 2. 许可范围和限制
14
+ 2.1 根据本协议的条款与条件,基于对模型或其衍生品中包含的我方拥有的任何知识产权和其他权利,我方特此授予您一项全球范围、非独占、不可转让、免费的使用许可。
15
+ 2.2若您拟使用或者已使用我方模型或其衍生品,如果您或者您的关联方提供的产品或服务在前一自然月的月活跃用户数超过1亿,或者如果您或者您的关联方在上一自然年的年收入超过1亿人民币的,您必须向我方申请该模型或其衍生品的商业许可,我方可自行决定是否授予您该许可。您无权行使本协议项下的任何权利,除非我方另行明确授予您该等许可。
16
+ 2.3 本协议作为我方享有知识产权和其他权利的模型的开源许可协议,仅约束您对我方模型的使用行为,并不限制我方对该模型享有的任何权利。
17
+
18
+ 3. 免责声明与风险约定
19
+ 3.1 模型及其任何输出均“按原样”提供,我方及其关联方不提供任何形式的明示或暗示的保证,包括但不限于适销性、特定用途适用性、不侵权、没有错误或疏漏、持续性、准确性、可靠性、稳定性的保证。您需自行负责判断使用或再分发本作品的适当性,并承担行使本许可证所授予权限相关的所有风险。
20
+ 3.2 您因使用模型或利用其输出内容而产生的任何侵权、违法、违约、赔偿、罚款、监管调查或其他法律责任(包括但不限于侵犯第三方专利、版权、商标、商业秘密、人格权、数据保护权等),均由您独自承担。我方不承担任何连带责任、补充责任或垫付责任。
21
+ 3.3 在任何情况下,我方对因使用本模型而产生的任何直接、间接、附带、特殊、惩罚性或后果性损失(包括但不限于数据丢失、业务中断、利润损失等)不承担责任,即使我方已被告知该等损失的可能性。
22
+ 3.4 对您和下游用户的其他约束
23
+ a)您应确保下游用户在使用您发布的本模型或您基于本模型开发的衍生品时,同样遵守本协议的相关规定,并通过合适的协议或条款对下游用户进行约束。若下游用户违反本协议规定,您需承担相应责任。
24
+ b)您需在您使用的本模型或您基于本模型开发的衍生品的所有副本中保留原始版权声明及本使用许可协议。
25
+ c)您不得使用bilibili indextts2或其衍生品来改进任何AI模型(bilibili indextts2或其衍生品、非商业用途的AI模型除外)。
26
+
27
+ 4. 合规义务
28
+ 4.1使用限制
29
+ a) 若您发布模型的衍生品,必须在发布页面或附随文档中清晰声明“该衍生品对原模型所作的任何改动与原模型原始权利人无关,原始权利人对该衍生品不背书、不担保、不承担责任”。
30
+ b) 若您使用模型或模型衍生品的过程中引入任何第三方数据或权重,您须自行取得合法授权并承担全部合规责任。
31
+ c) 不得将模型及模型衍生品用于违反输出地/使用地法律或监管要求的用途(包括但不限于生成虚假信息、歧视性内容、侵犯隐私等)。
32
+ d) 若模型或模型衍生品具备生成内容功能,您须确保其输出内容不违反输出地/使用地法律或监管要求的用途(包括但不限于生成虚假信息、歧视性内容、侵犯隐私等)。
33
+ 4.2 禁止高风险场景
34
+ 您须自行确保不在医疗诊断、自动驾驶、军事、关键基础设施控制、大规模生物识别监控、自动化决策(如信贷、就业评估)等高风险场景直接部署本模型及其衍生品。若您坚持部署,应自行完成符合适用法���(包括 GDPR、CCPA、HIPAA、出口管制、AI 特定法规等)的全部合规要求,我方对因此产生的任何后果概不负责。
35
+ 4.3 侵权责任
36
+ 如第三方就您开发的模型衍生品或您使用模型或其衍生品等行为主张权利,您应独立承担全部责任。若因您的行为导致我方遭受任何第三方索赔、行政处罚或其他损失,您应负责赔偿我方因此遭受的全部损失,包括但不限于律师费、诉讼费、赔偿金、罚款等,并采取一切必要措施消除对我方的负面影响。
37
+
38
+ 5. 保留权利
39
+ 5.1我方保留在您违反协议的情况下撤销本协议对您授权之权利。协议撤销后,您必须立即删除并停止使用材料。在本协议终止后,本协议第3条、第6条仍然有效。
40
+ 5.2 本许可证不授予使用我方的商号、商标、服务标记或产品名称的权限,除非在合理且惯例性地描述模型或衍生品的来源,例如本许可证3.4的规定,以及复制 NOTICE 文件内容时需要使用。
41
+ 5.3 若您或您的关联方对我方或我方任何关联实体提起诉讼或其他程序(包括诉讼中的交叉索赔或反诉),主张模型或其任何输出结果或其任何部分侵犯了您拥有或可许可的知识产权或其他权利,则本协议授予您的所有许可自该诉讼或程序提起之日起终止。
42
+
43
+ 6. 法律适用与争议解决
44
+ 6.1 本协议适用中华人民共和国法律法规。
45
+ 6.2 在本协议履行中,若发生争议,双方应本着友好协商的原则解决问题;如协商不成,双方均应将争议提交至上海仲裁委员会根据其仲裁规则进行仲裁,仲裁是一裁终局的,对双方均有约束力。由仲裁败诉方承担本次仲裁产生的公证调查费、仲裁费、律师费、差旅费等实际产生费用。
46
+
47
+ 7. 可分割性
48
+ 若本协议任何条款被认定为无效或不可执行,不影响其余条款之效力;无效部分应在法律允许的最大范围内按最接近原意的有效条款替代。
49
+
50
+ 8. 协议版本更新
51
+ 我方可发布新版 AI模型使用许可协议。新版仅适用于发布后新产生的使用行为,若您已按旧版获取模型,新版协议并无溯及力,但鼓励您主动更新。
52
+
MANIFEST.in ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ global-exclude *~ *.py[cod]
2
+ include *.cu *.cpp
3
+ include *.h *.hpp
README.md ADDED
@@ -0,0 +1,492 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## Unofficial IndexTTS v2 Training Repo
2
+ > Loop and trainer implemented using Codex CLI and guided prompts
3
+ - Train new languages by extending existing tokenizer
4
+ - tools\tokenizer\train_bpe.py and tools\tokenizer\extend_bpe.py
5
+ - Preprocess data to extract speaker embeddings for timbre, emotion, text, and mel tokens
6
+ - tools\preprocess_data.py and tools\preprocess_multiproc.py (multiproc is an attempt to make it run faster, there are issues with it though crashing)
7
+ - Create prompt/target pairs which is required for how IndexTTS2 trains in order to learn how to speak with speaker timbre while separating emotion (emotion has not yet been investigated)
8
+ - tools\generate_gpt_pairs.py
9
+ - Train/finetune the gpt model to learn to predict tokens for the language
10
+ - trainers\train_gpt_v2.py and train.bat
11
+
12
+ The code here works and Japanese was *mostly* correct shown here: https://www.youtube.com/watch?v=47V7lS-HUpo (this model was trained on 1100 hours of audio for about 1.5 epochs)
13
+
14
+ The latest updates are done with a focus on training a multilingual model which shows promise, while mostly retaining the base model abilities to speak English and Chinese. Emotion finetuning has not been investigated yet and it seems that full finetuning does not mess up the base emotion capabilities of the model.
15
+
16
+ <div align="center">
17
+ <img src='assets/index_icon.png' width="250"/>
18
+ </div>
19
+
20
+ <div align="center">
21
+ <a href="docs/README_zh.md" style="font-size: 24px">简体中文</a> |
22
+ <a href="README.md" style="font-size: 24px">English</a>
23
+ </div>
24
+
25
+ ## 👉🏻 IndexTTS2 👈🏻
26
+
27
+ <center><h3>IndexTTS2: A Breakthrough in Emotionally Expressive and Duration-Controlled Auto-Regressive Zero-Shot Text-to-Speech</h3></center>
28
+
29
+ [![IndexTTS2](assets/IndexTTS2_banner.png)](assets/IndexTTS2_banner.png)
30
+
31
+
32
+ <div align="center">
33
+ <a href='https://arxiv.org/abs/2506.21619'>
34
+ <img src='https://img.shields.io/badge/ArXiv-2506.21619-red?logo=arxiv'/>
35
+ </a>
36
+ <br/>
37
+ <a href='https://github.com/index-tts/index-tts'>
38
+ <img src='https://img.shields.io/badge/GitHub-Code-orange?logo=github'/>
39
+ </a>
40
+ <a href='https://index-tts.github.io/index-tts2.github.io/'>
41
+ <img src='https://img.shields.io/badge/GitHub-Demo-orange?logo=github'/>
42
+ </a>
43
+ <br/>
44
+ <a href='https://huggingface.co/spaces/IndexTeam/IndexTTS-2-Demo'>
45
+ <img src='https://img.shields.io/badge/HuggingFace-Demo-blue?logo=huggingface'/>
46
+ </a>
47
+ <a href='https://huggingface.co/IndexTeam/IndexTTS-2'>
48
+ <img src='https://img.shields.io/badge/HuggingFace-Model-blue?logo=huggingface' />
49
+ </a>
50
+ <br/>
51
+ <a href='https://modelscope.cn/studios/IndexTeam/IndexTTS-2-Demo'>
52
+ <img src='https://img.shields.io/badge/ModelScope-Demo-purple?logo=modelscope'/>
53
+ </>
54
+ <a href='https://modelscope.cn/models/IndexTeam/IndexTTS-2'>
55
+ <img src='https://img.shields.io/badge/ModelScope-Model-purple?logo=modelscope'/>
56
+ </a>
57
+ </div>
58
+
59
+
60
+ ### Abstract
61
+
62
+ Existing autoregressive large-scale text-to-speech (TTS) models have advantages in speech naturalness, but their token-by-token generation mechanism makes it difficult to precisely control the duration of synthesized speech. This becomes a significant limitation in applications requiring strict audio-visual synchronization, such as video dubbing.
63
+
64
+ This paper introduces IndexTTS2, which proposes a novel, general, and autoregressive model-friendly method for speech duration control.
65
+
66
+ The method supports two generation modes: one explicitly specifies the number of generated tokens to precisely control speech duration; the other freely generates speech in an autoregressive manner without specifying the number of tokens, while faithfully reproducing the prosodic features of the input prompt.
67
+
68
+ Furthermore, IndexTTS2 achieves disentanglement between emotional expression and speaker identity, enabling independent control over timbre and emotion. In the zero-shot setting, the model can accurately reconstruct the target timbre (from the timbre prompt) while perfectly reproducing the specified emotional tone (from the style prompt).
69
+
70
+ To enhance speech clarity in highly emotional expressions, we incorporate GPT latent representations and design a novel three-stage training paradigm to improve the stability of the generated speech. Additionally, to lower the barrier for emotional control, we designed a soft instruction mechanism based on text descriptions by fine-tuning Qwen3, effectively guiding the generation of speech with the desired emotional orientation.
71
+
72
+ Finally, experimental results on multiple datasets show that IndexTTS2 outperforms state-of-the-art zero-shot TTS models in terms of word error rate, speaker similarity, and emotional fidelity. Audio samples are available at: <a href="https://index-tts.github.io/index-tts2.github.io/">IndexTTS2 demo page</a>.
73
+
74
+ **Tips:** Please contact the authors for more detailed information. For commercial usage and cooperation, please contact <u>indexspeech@bilibili.com</u>.
75
+
76
+
77
+ ### Feel IndexTTS2
78
+
79
+ <div align="center">
80
+
81
+ **IndexTTS2: The Future of Voice, Now Generating**
82
+
83
+ [![IndexTTS2 Demo](assets/IndexTTS2-video-pic.png)](https://www.bilibili.com/video/BV136a9zqEk5)
84
+
85
+ *Click the image to watch the IndexTTS2 introduction video.*
86
+
87
+ </div>
88
+
89
+
90
+ ### Contact
91
+
92
+ QQ Group:663272642(No.4) 1013410623(No.5) \
93
+ Discord:https://discord.gg/uT32E7KDmy \
94
+ Email:indexspeech@bilibili.com \
95
+ You are welcome to join our community! 🌏 \
96
+ 欢迎大家来交流讨论!
97
+
98
+ > [!CAUTION]
99
+ > Thank you for your support of the bilibili indextts project!
100
+ > Please note that the **only official channel** maintained by the core team is: [https://github.com/index-tts/index-tts](https://github.com/index-tts/index-tts).
101
+ > ***Any other websites or services are not official***, and we cannot guarantee their security, accuracy, or timeliness.
102
+ > For the latest updates, please always refer to this official repository.
103
+
104
+
105
+ ## 📣 Updates
106
+
107
+ - `2025/09/08` 🔥🔥🔥 We release **IndexTTS-2** to the world!
108
+ - The first autoregressive TTS model with precise synthesis duration control, supporting both controllable and uncontrollable modes. <i>This functionality is not yet enabled in this release.</i>
109
+ - The model achieves highly expressive emotional speech synthesis, with emotion-controllable capabilities enabled through multiple input modalities.
110
+ - `2025/05/14` 🔥🔥 We release **IndexTTS-1.5**, significantly improving the model's stability and its performance in the English language.
111
+ - `2025/03/25` 🔥 We release **IndexTTS-1.0** with model weights and inference code.
112
+ - `2025/02/12` 🔥 We submitted our paper to arXiv, and released our demos and test sets.
113
+
114
+
115
+ ## 🖥️ Neural Network Architecture
116
+
117
+ Architectural overview of IndexTTS2, our state-of-the art speech model:
118
+
119
+ <picture>
120
+ <img src="assets/IndexTTS2.png" width="800"/>
121
+ </picture>
122
+
123
+
124
+ The key contributions of **IndexTTS2** are summarized as follows:
125
+
126
+ - We propose a duration adaptation scheme for autoregressive TTS models. IndexTTS2 is the first autoregressive zero-shot TTS model to combine precise duration control with natural duration generation, and the method is scalable for any autoregressive large-scale TTS model.
127
+ - The emotional and speaker-related features are decoupled from the prompts, and a feature fusion strategy is designed to maintain semantic fluency and pronunciation clarity during emotionally rich expressions. Furthermore, a tool was developed for emotion control, utilizing natural language descriptions for the benefit of users.
128
+ - To address the lack of highly expressive speech data, we propose an effective training strategy, significantly enhancing the emotional expressiveness of zeroshot TTS to State-of-the-Art (SOTA) level.
129
+ - We will publicly release the code and pre-trained weights to facilitate future research and practical applications.
130
+
131
+
132
+ ## Model Download
133
+
134
+ | **HuggingFace** | **ModelScope** |
135
+ |----------------------------------------------------------|----------------------------------------------------------|
136
+ | [😁 IndexTTS-2](https://huggingface.co/IndexTeam/IndexTTS-2) | [IndexTTS-2](https://modelscope.cn/models/IndexTeam/IndexTTS-2) |
137
+ | [IndexTTS-1.5](https://huggingface.co/IndexTeam/IndexTTS-1.5) | [IndexTTS-1.5](https://modelscope.cn/models/IndexTeam/IndexTTS-1.5) |
138
+ | [IndexTTS](https://huggingface.co/IndexTeam/Index-TTS) | [IndexTTS](https://modelscope.cn/models/IndexTeam/Index-TTS) |
139
+
140
+
141
+ ## Usage Instructions
142
+
143
+ ### ⚙️ Environment Setup
144
+
145
+ 1. Ensure that you have both [git](https://git-scm.com/downloads)
146
+ and [git-lfs](https://git-lfs.com/) on your system.
147
+
148
+ The Git-LFS plugin must also be enabled on your current user account:
149
+
150
+ ```bash
151
+ git lfs install
152
+ ```
153
+
154
+ 2. Download this repository:
155
+
156
+ ```bash
157
+ git clone https://github.com/index-tts/index-tts.git && cd index-tts
158
+ git lfs pull # download large repository files
159
+ ```
160
+
161
+ 3. Install the [uv package manager](https://docs.astral.sh/uv/getting-started/installation/).
162
+ It is *required* for a reliable, modern installation environment.
163
+
164
+ > [!TIP]
165
+ > **Quick & Easy Installation Method:**
166
+ >
167
+ > There are many convenient ways to install the `uv` command on your computer.
168
+ > Please check the link above to see all options. Alternatively, if you want
169
+ > a very quick and easy method, you can install it as follows:
170
+ >
171
+ > ```bash
172
+ > pip install -U uv
173
+ > ```
174
+
175
+ > [!WARNING]
176
+ > We **only** support the `uv` installation method. Other tools, such as `conda`
177
+ > or `pip`, don't provide any guarantees that they will install the correct
178
+ > dependency versions. You will almost certainly have *random bugs, error messages,*
179
+ > ***missing GPU acceleration**, and various other problems* if you don't use `uv`.
180
+ > Please *do not report any issues* if you use non-standard installations, since
181
+ > almost all such issues are invalid.
182
+ >
183
+ > Furthermore, `uv` is [up to 115x faster](https://github.com/astral-sh/uv/blob/main/BENCHMARKS.md)
184
+ > than `pip`, which is another *great* reason to embrace the new industry-standard
185
+ > for Python project management.
186
+
187
+ 4. Install required dependencies:
188
+
189
+ We use `uv` to manage the project's dependency environment. The following command
190
+ will *automatically* create a `.venv` project-directory and then installs the correct
191
+ versions of Python and all required dependencies:
192
+
193
+ ```bash
194
+ uv sync --all-extras
195
+ ```
196
+
197
+ If the download is slow, please try a *local mirror*, for example any of these
198
+ local mirrors in China (choose one mirror from the list below):
199
+
200
+ ```bash
201
+ uv sync --all-extras --default-index "https://mirrors.aliyun.com/pypi/simple"
202
+
203
+ uv sync --all-extras --default-index "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"
204
+ ```
205
+
206
+ > [!TIP]
207
+ > **Available Extra Features:**
208
+ >
209
+ > - `--all-extras`: Automatically adds *every* extra feature listed below. You can
210
+ > remove this flag if you want to customize your installation choices.
211
+ > - `--extra webui`: Adds WebUI support (recommended).
212
+ > - `--extra deepspeed`: Adds DeepSpeed support (may speed up inference on some
213
+ > systems).
214
+
215
+ > [!IMPORTANT]
216
+ > **Important (Windows):** The DeepSpeed library may be difficult to install for
217
+ > some Windows users. You can skip it by removing the `--all-extras` flag. If you
218
+ > want any of the other extra features above, you can manually add their specific
219
+ > feature flags instead.
220
+ >
221
+ > **Important (Linux/Windows):** If you see an error about CUDA during the installation,
222
+ > please ensure that you have installed NVIDIA's [CUDA Toolkit](https://developer.nvidia.com/cuda-toolkit)
223
+ > version **12.8** (or newer) on your system.
224
+
225
+ 5. Download the required models via [uv tool](https://docs.astral.sh/uv/guides/tools/#installing-tools):
226
+
227
+ Download via `huggingface-cli`:
228
+
229
+ ```bash
230
+ uv tool install "huggingface-hub[cli,hf_xet]"
231
+
232
+ hf download IndexTeam/IndexTTS-2 --local-dir=checkpoints
233
+ ```
234
+
235
+ Or download via `modelscope`:
236
+
237
+ ```bash
238
+ uv tool install "modelscope"
239
+
240
+ modelscope download --model IndexTeam/IndexTTS-2 --local_dir checkpoints
241
+ ```
242
+
243
+ > [!IMPORTANT]
244
+ > If the commands above aren't available, please carefully read the `uv tool`
245
+ > output. It will tell you how to add the tools to your system's path.
246
+
247
+ > [!NOTE]
248
+ > In addition to the above models, some small models will also be automatically
249
+ > downloaded when the project is run for the first time. If your network environment
250
+ > has slow access to HuggingFace, it is recommended to execute the following
251
+ > command before running the code:
252
+ >
253
+ > ```bash
254
+ > export HF_ENDPOINT="https://hf-mirror.com"
255
+ > ```
256
+
257
+
258
+ #### 🖥️ Checking PyTorch GPU Acceleration
259
+
260
+ If you need to diagnose your environment to see which GPUs are detected,
261
+ you can use our included utility to check your system:
262
+
263
+ ```bash
264
+ uv run tools/gpu_check.py
265
+ ```
266
+
267
+
268
+ ### 🔥 IndexTTS2 Quickstart
269
+
270
+ #### 🌐 Web Demo
271
+
272
+ ```bash
273
+ uv run webui.py
274
+ ```
275
+
276
+ Open your browser and visit `http://127.0.0.1:7860` to see the demo.
277
+
278
+ You can also adjust the settings to enable features such as FP16 inference (lower
279
+ VRAM usage), DeepSpeed acceleration, compiled CUDA kernels for speed, etc. All
280
+ available options can be seen via the following command:
281
+
282
+ ```bash
283
+ uv run webui.py -h
284
+ ```
285
+
286
+ Have fun!
287
+
288
+ > [!IMPORTANT]
289
+ > It can be very helpful to use **FP16** (half-precision) inference. It is faster
290
+ > and uses less VRAM, with a very small quality loss.
291
+ >
292
+ > **DeepSpeed** *may* also speed up inference on some systems, but it could also
293
+ > make it slower. The performance impact is highly dependent on your specific
294
+ > hardware, drivers and operating system. Please try with and without it,
295
+ > to discover what works best on your personal system.
296
+ >
297
+ > Lastly, be aware that *all* `uv` commands will **automatically activate** the correct
298
+ > per-project virtual environments. Do *not* manually activate any environments
299
+ > before running `uv` commands, since that could lead to dependency conflicts!
300
+
301
+
302
+ #### 📝 Using IndexTTS2 in Python
303
+
304
+ To run scripts, you *must* use the `uv run <file.py>` command to ensure that
305
+ the code runs inside your current "uv" environment. It *may* sometimes also be
306
+ necessary to add the current directory to your `PYTHONPATH`, to help it find
307
+ the IndexTTS modules.
308
+
309
+ Example of running a script via `uv`:
310
+
311
+ ```bash
312
+ PYTHONPATH="$PYTHONPATH:." uv run indextts/infer_v2.py
313
+ ```
314
+
315
+ Here are several examples of how to use IndexTTS2 in your own scripts:
316
+
317
+ 1. Synthesize new speech with a single reference audio file (voice cloning):
318
+
319
+ ```python
320
+ from indextts.infer_v2 import IndexTTS2
321
+ tts = IndexTTS2(cfg_path="checkpoints/config.yaml", model_dir="checkpoints", use_fp16=False, use_cuda_kernel=False, use_deepspeed=False)
322
+ text = "Translate for me, what is a surprise!"
323
+ tts.infer(spk_audio_prompt='examples/voice_01.wav', text=text, output_path="gen.wav", verbose=True)
324
+ ```
325
+
326
+ 2. Using a separate, emotional reference audio file to condition the speech synthesis:
327
+
328
+ ```python
329
+ from indextts.infer_v2 import IndexTTS2
330
+ tts = IndexTTS2(cfg_path="checkpoints/config.yaml", model_dir="checkpoints", use_fp16=False, use_cuda_kernel=False, use_deepspeed=False)
331
+ text = "酒楼丧尽天良,开始借机竞拍房间,哎,一群蠢货���"
332
+ tts.infer(spk_audio_prompt='examples/voice_07.wav', text=text, output_path="gen.wav", emo_audio_prompt="examples/emo_sad.wav", verbose=True)
333
+ ```
334
+
335
+ 3. When an emotional reference audio file is specified, you can optionally set
336
+ the `emo_alpha` to adjust how much it affects the output.
337
+ Valid range is `0.0 - 1.0`, and the default value is `1.0` (100%):
338
+
339
+ ```python
340
+ from indextts.infer_v2 import IndexTTS2
341
+ tts = IndexTTS2(cfg_path="checkpoints/config.yaml", model_dir="checkpoints", use_fp16=False, use_cuda_kernel=False, use_deepspeed=False)
342
+ text = "酒楼丧尽天良,开始借机竞拍房间,哎,一群蠢货。"
343
+ tts.infer(spk_audio_prompt='examples/voice_07.wav', text=text, output_path="gen.wav", emo_audio_prompt="examples/emo_sad.wav", emo_alpha=0.9, verbose=True)
344
+ ```
345
+
346
+ 4. It's also possible to omit the emotional reference audio and instead provide
347
+ an 8-float list specifying the intensity of each emotion, in the following order:
348
+ `[happy, angry, sad, afraid, disgusted, melancholic, surprised, calm]`.
349
+ You can additionally use the `use_random` parameter to introduce stochasticity
350
+ during inference; the default is `False`, and setting it to `True` enables
351
+ randomness:
352
+
353
+ > [!NOTE]
354
+ > Enabling random sampling will reduce the voice cloning fidelity of the speech
355
+ > synthesis.
356
+
357
+ ```python
358
+ from indextts.infer_v2 import IndexTTS2
359
+ tts = IndexTTS2(cfg_path="checkpoints/config.yaml", model_dir="checkpoints", use_fp16=False, use_cuda_kernel=False, use_deepspeed=False)
360
+ text = "哇塞!这个爆率也太高了!欧皇附体了!"
361
+ tts.infer(spk_audio_prompt='examples/voice_10.wav', text=text, output_path="gen.wav", emo_vector=[0, 0, 0, 0, 0, 0, 0.45, 0], use_random=False, verbose=True)
362
+ ```
363
+
364
+ 5. Alternatively, you can enable `use_emo_text` to guide the emotions based on
365
+ your provided `text` script. Your text script will then automatically
366
+ be converted into emotion vectors.
367
+ It's recommended to use `emo_alpha` around 0.6 (or lower) when using the text
368
+ emotion modes, for more natural sounding speech.
369
+ You can introduce randomness with `use_random` (default: `False`;
370
+ `True` enables randomness):
371
+
372
+ ```python
373
+ from indextts.infer_v2 import IndexTTS2
374
+ tts = IndexTTS2(cfg_path="checkpoints/config.yaml", model_dir="checkpoints", use_fp16=False, use_cuda_kernel=False, use_deepspeed=False)
375
+ text = "快躲起来!是他要来了!他要来抓我们了!"
376
+ tts.infer(spk_audio_prompt='examples/voice_12.wav', text=text, output_path="gen.wav", emo_alpha=0.6, use_emo_text=True, use_random=False, verbose=True)
377
+ ```
378
+
379
+ 6. It's also possible to directly provide a specific text emotion description
380
+ via the `emo_text` parameter. Your emotion text will then automatically be
381
+ converted into emotion vectors. This gives you separate control of the text
382
+ script and the text emotion description:
383
+
384
+ ```python
385
+ from indextts.infer_v2 import IndexTTS2
386
+ tts = IndexTTS2(cfg_path="checkpoints/config.yaml", model_dir="checkpoints", use_fp16=False, use_cuda_kernel=False, use_deepspeed=False)
387
+ text = "快躲起来!是他要来了!他要来抓我们了!"
388
+ emo_text = "你吓死我了!你是鬼吗?"
389
+ tts.infer(spk_audio_prompt='examples/voice_12.wav', text=text, output_path="gen.wav", emo_alpha=0.6, use_emo_text=True, emo_text=emo_text, use_random=False, verbose=True)
390
+ ```
391
+
392
+ > [!TIP]
393
+ > **Pinyin Usage Notes:**
394
+ >
395
+ > IndexTTS2 still supports mixed modeling of Chinese characters and Pinyin.
396
+ > When you need precise pronunciation control, please provide text with specific Pinyin annotations to activate the Pinyin control feature.
397
+ > Note that Pinyin control does not work for every possible consonant–vowel combination; only valid Chinese Pinyin cases are supported.
398
+ > For the full list of valid entries, please refer to `checkpoints/pinyin.vocab`.
399
+ >
400
+ > Example:
401
+ > ```
402
+ > 之前你做DE5很好,所以这一次也DEI3做DE2很好才XING2,如果这次目标完成得不错的话,我们就直接打DI1去银行取钱。
403
+ > ```
404
+
405
+ ### Legacy: IndexTTS1 User Guide
406
+
407
+ You can also use our previous IndexTTS1 model by importing a different module:
408
+
409
+ ```python
410
+ from indextts.infer import IndexTTS
411
+ tts = IndexTTS(model_dir="checkpoints",cfg_path="checkpoints/config.yaml")
412
+ voice = "examples/voice_07.wav"
413
+ text = "大家好,我现在正在bilibili 体验 ai 科技,说实话,来之前我绝对想不到!AI技术已经发展到这样匪夷所思的地步了!比如说,现在正在说话的其实是B站为我现场复刻的数字分身,简直就是平行宇宙的另一个我了。如果大家也想体验更多深入的AIGC功能,可以访问 bilibili studio,相信我,你们也会吃惊的。"
414
+ tts.infer(voice, text, 'gen.wav')
415
+ ```
416
+
417
+ For more detailed information, see [README_INDEXTTS_1_5](archive/README_INDEXTTS_1_5.md),
418
+ or visit the IndexTTS1 repository at <a href="https://github.com/index-tts/index-tts/tree/v1.5.0">index-tts:v1.5.0</a>.
419
+
420
+
421
+ ## Our Releases and Demos
422
+
423
+ ### IndexTTS2: [[Paper]](https://arxiv.org/abs/2506.21619); [[Demo]](https://index-tts.github.io/index-tts2.github.io/); [[ModelScope]](https://modelscope.cn/studios/IndexTeam/IndexTTS-2-Demo); [[HuggingFace]](https://huggingface.co/spaces/IndexTeam/IndexTTS-2-Demo)
424
+
425
+ ### IndexTTS1: [[Paper]](https://arxiv.org/abs/2502.05512); [[Demo]](https://index-tts.github.io/); [[ModelScope]](https://modelscope.cn/studios/IndexTeam/IndexTTS-Demo); [[HuggingFace]](https://huggingface.co/spaces/IndexTeam/IndexTTS)
426
+
427
+
428
+ ## Acknowledgements
429
+
430
+ 1. [tortoise-tts](https://github.com/neonbjb/tortoise-tts)
431
+ 2. [XTTSv2](https://github.com/coqui-ai/TTS)
432
+ 3. [BigVGAN](https://github.com/NVIDIA/BigVGAN)
433
+ 4. [wenet](https://github.com/wenet-e2e/wenet/tree/main)
434
+ 5. [icefall](https://github.com/k2-fsa/icefall)
435
+ 6. [maskgct](https://github.com/open-mmlab/Amphion/tree/main/models/tts/maskgct)
436
+ 7. [seed-vc](https://github.com/Plachtaa/seed-vc)
437
+
438
+ ## Contributors in Bilibili
439
+ We sincerely thank colleagues from different roles at Bilibili, whose combined efforts made the IndexTTS series possible.
440
+
441
+ ### Core Authors
442
+ - **Wei Deng** - Core author; Initiated the IndexTTS project, led the development of the IndexTTS1 data pipeline, model architecture design and training, as well as iterative optimization of the IndexTTS series of models, focusing on fundamental capability building and performance optimization.
443
+ - **Siyi Zhou** – Core author; in IndexTTS2, led model architecture design and training pipeline optimization, focusing on key features such as multilingual and emotional synthesis.
444
+ - **Jingchen Shu** - Core author; worked on overall architecture design, cross-lingual modeling solutions, and training strategy optimization, driving model iteration.
445
+ - **Xun Zhou** - Core author; worked on cross-lingual data processing and experiments, explored multilingual training strategies, and contributed to audio quality improvement and stability evaluation.
446
+ - **Jinchao Wang** - Core author; worked on model development and deployment, building the inference framework and supporting system integration.
447
+ - **Yiquan Zhou** - Core author; contributed to model experiments and validation, and proposed and implemented text-based emotion control.
448
+ - **Yi He** - Core author; contributed to model experiments and validation.
449
+ - **Lu Wang** – Core author; worked on data processing and model evaluation, supporting model training and performance verification.
450
+
451
+ ### Technical Contributors
452
+ - **Yining Wang** - Supporting contributor; contributed to open-source code implementation and maintenance, supporting feature adaptation and community release.
453
+ - **Yong Wu** - Supporting contributor; worked on data processing and experimental support, ensuring data quality and efficiency for model training and iteration.
454
+ - **Yaqin Huang** – Supporting contributor; contributed to systematic model evaluation and effect tracking, providing feedback to support iterative improvements.
455
+ - **Yunhan Xu** – Supporting contributor; provided guidance in recording and data collection, while also offering feedback from a product and operations perspective to improve usability and practical application.
456
+ - **Yuelang Sun** – Supporting contributor; provided professional support in audio recording and data collection, ensuring high-quality data for model training and evaluation.
457
+ - **Yihuang Liang** - Supporting contributor; worked on systematic model evaluation and project promotion, helping IndexTTS expand its reach and engagement.
458
+
459
+ ### Technical Guidance
460
+ - **Huyang Sun** - Provided strong support for the IndexTTS project, ensuring strategic alignment and resource backing.
461
+ - **Bin Xia** - Contributed to the review, optimization, and follow-up of technical solutions, focusing on ensuring model effectiveness.
462
+
463
+
464
+ ## 📚 Citation
465
+
466
+ 🌟 If you find our work helpful, please leave us a star and cite our paper.
467
+
468
+
469
+ IndexTTS2:
470
+
471
+ ```
472
+ @article{zhou2025indextts2,
473
+ title={IndexTTS2: A Breakthrough in Emotionally Expressive and Duration-Controlled Auto-Regressive Zero-Shot Text-to-Speech},
474
+ author={Siyi Zhou, Yiquan Zhou, Yi He, Xun Zhou, Jinchao Wang, Wei Deng, Jingchen Shu},
475
+ journal={arXiv preprint arXiv:2506.21619},
476
+ year={2025}
477
+ }
478
+ ```
479
+
480
+
481
+ IndexTTS:
482
+
483
+ ```
484
+ @article{deng2025indextts,
485
+ title={IndexTTS: An Industrial-Level Controllable and Efficient Zero-Shot Text-To-Speech System},
486
+ author={Wei Deng, Siyi Zhou, Jingchen Shu, Jinchao Wang, Lu Wang},
487
+ journal={arXiv preprint arXiv:2502.05512},
488
+ year={2025},
489
+ doi={10.48550/arXiv.2502.05512},
490
+ url={https://arxiv.org/abs/2502.05512}
491
+ }
492
+ ```
archive/README_INDEXTTS_1_5.md ADDED
@@ -0,0 +1,247 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ <div align="center">
3
+ <img src='assets/index_icon.png' width="250"/>
4
+ </div>
5
+
6
+
7
+ <h2><center>IndexTTS: An Industrial-Level Controllable and Efficient Zero-Shot Text-To-Speech System</h2>
8
+
9
+ <p align="center">
10
+ <a href='https://arxiv.org/abs/2502.05512'><img src='https://img.shields.io/badge/ArXiv-2502.05512-red'></a>
11
+
12
+ ## 👉🏻 IndexTTS 👈🏻
13
+
14
+ [[HuggingFace Demo]](https://huggingface.co/spaces/IndexTeam/IndexTTS) [[ModelScope Demo]](https://modelscope.cn/studios/IndexTeam/IndexTTS-Demo) \
15
+ [[Paper]](https://arxiv.org/abs/2502.05512) [[Demos]](https://index-tts.github.io)
16
+
17
+ **IndexTTS** is a GPT-style text-to-speech (TTS) model mainly based on XTTS and Tortoise. It is capable of correcting the pronunciation of Chinese characters using pinyin and controlling pauses at any position through punctuation marks. We enhanced multiple modules of the system, including the improvement of speaker condition feature representation, and the integration of BigVGAN2 to optimize audio quality. Trained on tens of thousands of hours of data, our system achieves state-of-the-art performance, outperforming current popular TTS systems such as XTTS, CosyVoice2, Fish-Speech, and F5-TTS.
18
+ <span style="font-size:16px;">
19
+ Experience **IndexTTS**: Please contact <u>xuanwu@bilibili.com</u> for more detailed information. </span>
20
+ ### Contact
21
+ QQ群(二群):1048202584 \
22
+ Discord:https://discord.gg/uT32E7KDmy \
23
+ 简历:indexspeech@bilibili.com \
24
+ 欢迎大家来交流讨论!
25
+ ## 📣 Updates
26
+
27
+ - `2025/05/14` 🔥🔥 We release the **IndexTTS-1.5**, Significantly improve the model's stability and its performance in the English language.
28
+ - `2025/03/25` 🔥 We release IndexTTS-1.0 model parameters and inference code.
29
+ - `2025/02/12` 🔥 We submitted our paper on arXiv, and released our demos and test sets.
30
+
31
+ ## 🖥️ Method
32
+
33
+ The overview of IndexTTS is shown as follows.
34
+
35
+ <picture>
36
+ <img src="assets/IndexTTS.png" width="800"/>
37
+ </picture>
38
+
39
+
40
+ The main improvements and contributions are summarized as follows:
41
+ - In Chinese scenarios, we have introduced a character-pinyin hybrid modeling approach. This allows for quick correction of mispronounced characters.
42
+ - **IndexTTS** incorporate a conformer conditioning encoder and a BigVGAN2-based speechcode decoder. This improves training stability, voice timbre similarity, and sound quality.
43
+ - We release all test sets here, including those for polysyllabic words, subjective and objective test sets.
44
+
45
+
46
+
47
+ ## Model Download
48
+ | 🤗**HuggingFace** | **ModelScope** |
49
+ |----------------------------------------------------------|----------------------------------------------------------|
50
+ | [IndexTTS](https://huggingface.co/IndexTeam/Index-TTS) | [IndexTTS](https://modelscope.cn/models/IndexTeam/Index-TTS) |
51
+ | [😁IndexTTS-1.5](https://huggingface.co/IndexTeam/IndexTTS-1.5) | [IndexTTS-1.5](https://modelscope.cn/models/IndexTeam/IndexTTS-1.5) |
52
+
53
+
54
+ ## 📑 Evaluation
55
+
56
+ **Word Error Rate (WER) Results for IndexTTS and Baseline Models on the** [**seed-test**](https://github.com/BytedanceSpeech/seed-tts-eval)
57
+
58
+ | **WER** | **test_zh** | **test_en** | **test_hard** |
59
+ |:----------------------:|:-----------:|:-----------:|:-------------:|
60
+ | **Human** | 1.26 | 2.14 | - |
61
+ | **SeedTTS** | 1.002 | 1.945 | **6.243** |
62
+ | **CosyVoice 2** | 1.45 | 2.57 | 6.83 |
63
+ | **F5TTS** | 1.56 | 1.83 | 8.67 |
64
+ | **FireRedTTS** | 1.51 | 3.82 | 17.45 |
65
+ | **MaskGCT** | 2.27 | 2.62 | 10.27 |
66
+ | **Spark-TTS** | 1.2 | 1.98 | - |
67
+ | **MegaTTS 3** | 1.36 | 1.82 | - |
68
+ | **IndexTTS** | 0.937 | 1.936 | 6.831 |
69
+ | **IndexTTS-1.5** | **0.821** | **1.606** | 6.565 |
70
+
71
+
72
+ **Word Error Rate (WER) Results for IndexTTS and Baseline Models on the other opensource test**
73
+
74
+
75
+ | **Model** | **aishell1_test** | **commonvoice_20_test_zh** | **commonvoice_20_test_en** | **librispeech_test_clean** | **avg** |
76
+ |:---------------:|:-----------------:|:--------------------------:|:--------------------------:|:--------------------------:|:--------:|
77
+ | **Human** | 2.0 | 9.5 | 10.0 | 2.4 | 5.1 |
78
+ | **CosyVoice 2** | 1.8 | 9.1 | 7.3 | 4.9 | 5.9 |
79
+ | **F5TTS** | 3.9 | 11.7 | 5.4 | 7.8 | 8.2 |
80
+ | **Fishspeech** | 2.4 | 11.4 | 8.8 | 8.0 | 8.3 |
81
+ | **FireRedTTS** | 2.2 | 11.0 | 16.3 | 5.7 | 7.7 |
82
+ | **XTTS** | 3.0 | 11.4 | 7.1 | 3.5 | 6.0 |
83
+ | **IndexTTS** | 1.3 | 7.0 | 5.3 | 2.1 | 3.7 |
84
+ | **IndexTTS-1.5** | **1.2** | **6.8** | **3.9** | **1.7** | **3.1** |
85
+
86
+
87
+ **Speaker Similarity (SS) Results for IndexTTS and Baseline Models**
88
+
89
+ | **Model** | **aishell1_test** | **commonvoice_20_test_zh** | **commonvoice_20_test_en** | **librispeech_test_clean** | **avg** |
90
+ |:---------------:|:-----------------:|:--------------------------:|:--------------------------:|:--------------------------:|:---------:|
91
+ | **Human** | 0.846 | 0.809 | 0.820 | 0.858 | 0.836 |
92
+ | **CosyVoice 2** | **0.796** | 0.743 | 0.742 | **0.837** | **0.788** |
93
+ | **F5TTS** | 0.743 | **0.747** | 0.746 | 0.828 | 0.779 |
94
+ | **Fishspeech** | 0.488 | 0.552 | 0.622 | 0.701 | 0.612 |
95
+ | **FireRedTTS** | 0.579 | 0.593 | 0.587 | 0.698 | 0.631 |
96
+ | **XTTS** | 0.573 | 0.586 | 0.648 | 0.761 | 0.663 |
97
+ | **IndexTTS** | 0.744 | 0.742 | **0.758** | 0.823 | 0.776 |
98
+ | **IndexTTS-1.5** | 0.741 | 0.722 | 0.753 | 0.819 | 0.771 |
99
+
100
+
101
+
102
+ **MOS Scores for Zero-Shot Cloned Voice**
103
+
104
+ | **Model** | **Prosody** | **Timbre** | **Quality** | **AVG** |
105
+ |-----------------|:-----------:|:----------:|:-----------:|:---------:|
106
+ | **CosyVoice 2** | 3.67 | 4.05 | 3.73 | 3.81 |
107
+ | **F5TTS** | 3.56 | 3.88 | 3.56 | 3.66 |
108
+ | **Fishspeech** | 3.40 | 3.63 | 3.69 | 3.57 |
109
+ | **FireRedTTS** | 3.79 | 3.72 | 3.60 | 3.70 |
110
+ | **XTTS** | 3.23 | 2.99 | 3.10 | 3.11 |
111
+ | **IndexTTS** | **3.79** | **4.20** | **4.05** | **4.01** |
112
+
113
+
114
+ ## Usage Instructions
115
+ ### Environment Setup
116
+ 1. Download this repository:
117
+ ```bash
118
+ git clone https://github.com/index-tts/index-tts.git
119
+ ```
120
+ 2. Install dependencies:
121
+
122
+ Create a new conda environment and install dependencies:
123
+
124
+ ```bash
125
+ conda create -n index-tts python=3.10
126
+ conda activate index-tts
127
+ apt-get install ffmpeg
128
+ # or use conda to install ffmpeg
129
+ conda install -c conda-forge ffmpeg
130
+ ```
131
+
132
+ Install [PyTorch](https://pytorch.org/get-started/locally/), e.g.:
133
+ ```bash
134
+ pip install torch torchaudio --index-url https://download.pytorch.org/whl/cu118
135
+ ```
136
+
137
+ > [!NOTE]
138
+ > If you are using Windows you may encounter [an error](https://github.com/index-tts/index-tts/issues/61) when installing `pynini`:
139
+ `ERROR: Failed building wheel for pynini`
140
+ > In this case, please install `pynini` via `conda`:
141
+ > ```bash
142
+ > # after conda activate index-tts
143
+ > conda install -c conda-forge pynini==2.1.6
144
+ > pip install WeTextProcessing --no-deps
145
+ > ```
146
+
147
+ Install `IndexTTS` as a package:
148
+ ```bash
149
+ cd index-tts
150
+ pip install -e .
151
+ ```
152
+
153
+ 3. Download models:
154
+
155
+ Download by `huggingface-cli`:
156
+
157
+ ```bash
158
+ huggingface-cli download IndexTeam/IndexTTS-1.5 \
159
+ config.yaml bigvgan_discriminator.pth bigvgan_generator.pth bpe.model dvae.pth gpt.pth unigram_12000.vocab \
160
+ --local-dir checkpoints
161
+ ```
162
+
163
+ Recommended for China users. 如果下载速度慢,可以使用镜像:
164
+ ```bash
165
+ export HF_ENDPOINT="https://hf-mirror.com"
166
+ ```
167
+
168
+ Or by `wget`:
169
+
170
+ ```bash
171
+ wget https://huggingface.co/IndexTeam/IndexTTS-1.5/resolve/main/bigvgan_discriminator.pth -P checkpoints
172
+ wget https://huggingface.co/IndexTeam/IndexTTS-1.5/resolve/main/bigvgan_generator.pth -P checkpoints
173
+ wget https://huggingface.co/IndexTeam/IndexTTS-1.5/resolve/main/bpe.model -P checkpoints
174
+ wget https://huggingface.co/IndexTeam/IndexTTS-1.5/resolve/main/dvae.pth -P checkpoints
175
+ wget https://huggingface.co/IndexTeam/IndexTTS-1.5/resolve/main/gpt.pth -P checkpoints
176
+ wget https://huggingface.co/IndexTeam/IndexTTS-1.5/resolve/main/unigram_12000.vocab -P checkpoints
177
+ wget https://huggingface.co/IndexTeam/IndexTTS-1.5/resolve/main/config.yaml -P checkpoints
178
+ ```
179
+
180
+ > [!NOTE]
181
+ > If you prefer to use the `IndexTTS-1.0` model, please replace `IndexTeam/IndexTTS-1.5` with `IndexTeam/IndexTTS` in the above commands.
182
+
183
+
184
+ 4. Run test script:
185
+
186
+
187
+ ```bash
188
+ # Please put your prompt audio in 'test_data' and rename it to 'input.wav'
189
+ python indextts/infer.py
190
+ ```
191
+
192
+ 5. Use as command line tool:
193
+
194
+ ```bash
195
+ # Make sure pytorch has been installed before running this command
196
+ indextts "大��好,我现在正在bilibili 体验 ai 科技,说实话,来之前我绝对想不到!AI技术已经发展到这样匪夷所思的地步了!" \
197
+ --voice reference_voice.wav \
198
+ --model_dir checkpoints \
199
+ --config checkpoints/config.yaml \
200
+ --output output.wav
201
+ ```
202
+
203
+ Use `--help` to see more options.
204
+ ```bash
205
+ indextts --help
206
+ ```
207
+
208
+ #### Web Demo
209
+ ```bash
210
+ pip install -e ".[webui]" --no-build-isolation
211
+ python webui.py
212
+
213
+ # use another model version:
214
+ python webui.py --model_dir IndexTTS-1.5
215
+ ```
216
+
217
+ Open your browser and visit `http://127.0.0.1:7860` to see the demo.
218
+
219
+
220
+ #### Sample Code
221
+ ```python
222
+ from indextts.infer import IndexTTS
223
+ tts = IndexTTS(model_dir="checkpoints",cfg_path="checkpoints/config.yaml")
224
+ voice="reference_voice.wav"
225
+ text="大家好,我现在正在bilibili 体验 ai 科技,说实话,来之前我绝对想不到!AI技术已经发展到这样匪夷所思的地步了!比如说,现在正在说话的其实是B站为我现场复刻的数字分身,简直就是平行宇宙的另一个我了。如果大家也想体验更多深入的AIGC功能,可以访问 bilibili studio,相信我,你们也会吃惊的。"
226
+ tts.infer(voice, text, output_path)
227
+ ```
228
+
229
+ ## Acknowledge
230
+ 1. [tortoise-tts](https://github.com/neonbjb/tortoise-tts)
231
+ 2. [XTTSv2](https://github.com/coqui-ai/TTS)
232
+ 3. [BigVGAN](https://github.com/NVIDIA/BigVGAN)
233
+ 4. [wenet](https://github.com/wenet-e2e/wenet/tree/main)
234
+ 5. [icefall](https://github.com/k2-fsa/icefall)
235
+
236
+ ## 📚 Citation
237
+
238
+ 🌟 If you find our work helpful, please leave us a star and cite our paper.
239
+
240
+ ```
241
+ @article{deng2025indextts,
242
+ title={IndexTTS: An Industrial-Level Controllable and Efficient Zero-Shot Text-To-Speech System},
243
+ author={Wei Deng, Siyi Zhou, Jingchen Shu, Jinchao Wang, Lu Wang},
244
+ journal={arXiv preprint arXiv:2502.05512},
245
+ year={2025}
246
+ }
247
+ ```
assets/IndexTTS.png ADDED

Git LFS Details

  • SHA256: c40c9c0b65922f6525d8aa7350dbd494e4e1ea5c17ba5151f3941f5e3a30e1eb
  • Pointer size: 131 Bytes
  • Size of remote file: 215 kB
assets/IndexTTS2-video-pic.png ADDED

Git LFS Details

  • SHA256: 5a69fc23580ed1f1d0dcd34105f32633756fea153fdfccdfdb27569fa8db4168
  • Pointer size: 131 Bytes
  • Size of remote file: 540 kB
assets/IndexTTS2.mp4 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3045b3947ce5a61385d1ae7cd7b1ae9c3e171604b53a2f68222f69e51c9dc009
3
+ size 8944379
assets/IndexTTS2.png ADDED

Git LFS Details

  • SHA256: 75340bc15074115c2eb88028ffb54d49bb188c095adc0066d482afec8394212f
  • Pointer size: 130 Bytes
  • Size of remote file: 58.3 kB
assets/IndexTTS2_banner.png ADDED

Git LFS Details

  • SHA256: 29c8a215fd77be2778b091afe78f70fb235abdbc31c55fc032263797c3f13de6
  • Pointer size: 132 Bytes
  • Size of remote file: 3.07 MB
assets/img.png ADDED

Git LFS Details

  • SHA256: f9675d8d105a2a30a1432f26192a24ca87db7337b0b060c9757c73269c3e632c
  • Pointer size: 130 Bytes
  • Size of remote file: 88.7 kB
assets/index_icon.png ADDED

Git LFS Details

  • SHA256: 8383745725b292e2d3422c56aee77bcce2febd0a5f5863cdb21f9d0752edaef9
  • Pointer size: 130 Bytes
  • Size of remote file: 92.4 kB
check_imports.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ try:
3
+ import soundfile
4
+ print("soundfile is available")
5
+ except ImportError:
6
+ print("soundfile is NOT available")
7
+
8
+ try:
9
+ import scipy.io.wavfile
10
+ print("scipy is available")
11
+ except ImportError:
12
+ print("scipy is NOT available")
13
+
14
+ try:
15
+ import torchcodec
16
+ print("torchcodec is available")
17
+ except ImportError:
18
+ print("torchcodec is NOT available")
checkpoints/config.yaml ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ dataset:
2
+ bpe_model: bpe.model
3
+ sample_rate: 24000
4
+ squeeze: false
5
+ mel:
6
+ sample_rate: 24000
7
+ n_fft: 1024
8
+ hop_length: 256
9
+ win_length: 1024
10
+ n_mels: 100
11
+ mel_fmin: 0
12
+ normalize: false
13
+
14
+ gpt:
15
+ model_dim: 1280
16
+ max_mel_tokens: 1815
17
+ max_text_tokens: 600
18
+ heads: 20
19
+ use_mel_codes_as_input: true
20
+ mel_length_compression: 1024
21
+ layers: 24
22
+ number_text_tokens: 12000
23
+ number_mel_codes: 8194
24
+ start_mel_token: 8192
25
+ stop_mel_token: 8193
26
+ start_text_token: 0
27
+ stop_text_token: 1
28
+ train_solo_embeddings: false
29
+ condition_type: "conformer_perceiver"
30
+ condition_module:
31
+ output_size: 512
32
+ linear_units: 2048
33
+ attention_heads: 8
34
+ num_blocks: 6
35
+ input_layer: "conv2d2"
36
+ perceiver_mult: 2
37
+ emo_condition_module:
38
+ output_size: 512
39
+ linear_units: 1024
40
+ attention_heads: 4
41
+ num_blocks: 4
42
+ input_layer: "conv2d2"
43
+ perceiver_mult: 2
44
+
45
+ semantic_codec:
46
+ codebook_size: 8192
47
+ hidden_size: 1024
48
+ codebook_dim: 8
49
+ vocos_dim: 384
50
+ vocos_intermediate_dim: 2048
51
+ vocos_num_layers: 12
52
+
53
+ s2mel:
54
+ preprocess_params:
55
+ sr: 22050
56
+ spect_params:
57
+ n_fft: 1024
58
+ win_length: 1024
59
+ hop_length: 256
60
+ n_mels: 80
61
+ fmin: 0
62
+ fmax: "None"
63
+
64
+ dit_type: "DiT"
65
+ reg_loss_type: "l1"
66
+ style_encoder:
67
+ dim: 192
68
+ length_regulator:
69
+ channels: 512
70
+ is_discrete: false
71
+ in_channels: 1024
72
+ content_codebook_size: 2048
73
+ sampling_ratios: [1, 1, 1, 1]
74
+ vector_quantize: false
75
+ n_codebooks: 1
76
+ quantizer_dropout: 0.0
77
+ f0_condition: false
78
+ n_f0_bins: 512
79
+ DiT:
80
+ hidden_dim: 512
81
+ num_heads: 8
82
+ depth: 13
83
+ class_dropout_prob: 0.1
84
+ block_size: 8192
85
+ in_channels: 80
86
+ style_condition: true
87
+ final_layer_type: 'wavenet'
88
+ target: 'mel'
89
+ content_dim: 512
90
+ content_codebook_size: 1024
91
+ content_type: 'discrete'
92
+ f0_condition: false
93
+ n_f0_bins: 512
94
+ content_codebooks: 1
95
+ is_causal: false
96
+ long_skip_connection: true
97
+ zero_prompt_speech_token: false
98
+ time_as_token: false
99
+ style_as_token: false
100
+ uvit_skip_connection: true
101
+ add_resblock_in_transformer: false
102
+ wavenet:
103
+ hidden_dim: 512
104
+ num_layers: 8
105
+ kernel_size: 5
106
+ dilation_rate: 1
107
+ p_dropout: 0.2
108
+ style_condition: true
109
+
110
+ gpt_checkpoint: gpt.pth
111
+ w2v_stat: wav2vec2bert_stats.pt
112
+ s2mel_checkpoint: s2mel.pth
113
+ emo_matrix: feat2.pt
114
+ spk_matrix: feat1.pt
115
+ emo_num: [3, 17, 2, 8, 4, 5, 10, 24]
116
+ qwen_emo_path: qwen0.6bemo4-merge/
117
+ vocoder:
118
+ type: "bigvgan"
119
+ name: "nvidia/bigvgan_v2_22khz_80band_256x"
120
+ version: 2.0
docs/README_zh.md ADDED
@@ -0,0 +1,399 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ <div align="center">
3
+ <img src='../assets/index_icon.png' width="250"/>
4
+ </div>
5
+
6
+ <div align="center">
7
+ <a href="README_zh.md" style="font-size: 24px">简体中文</a> |
8
+ <a href="../README.md" style="font-size: 24px">English</a>
9
+ </div>
10
+
11
+ ## 👉🏻 IndexTTS2 👈🏻
12
+
13
+ <center><h3>IndexTTS2:情感表达与时长可控的自回归零样本语音合成突破</h3></center>
14
+
15
+ [![IndexTTS2](../assets/IndexTTS2_banner.png)](../assets/IndexTTS2_banner.png)
16
+
17
+ <div align="center">
18
+ <a href='https://arxiv.org/abs/2506.21619'>
19
+ <img src='https://img.shields.io/badge/ArXiv-2506.21619-red?logo=arxiv'/>
20
+ </a>
21
+ <br/>
22
+ <a href='https://github.com/index-tts/index-tts'>
23
+ <img src='https://img.shields.io/badge/GitHub-Code-orange?logo=github'/>
24
+ </a>
25
+ <a href='https://index-tts.github.io/index-tts2.github.io/'>
26
+ <img src='https://img.shields.io/badge/GitHub-Demo-orange?logo=github'/>
27
+ </a>
28
+ <br/>
29
+ <a href='https://huggingface.co/spaces/IndexTeam/IndexTTS-2-Demo'>
30
+ <img src='https://img.shields.io/badge/HuggingFace-Demo-blue?logo=huggingface'/>
31
+ </a>
32
+ <a href='https://huggingface.co/IndexTeam/IndexTTS-2'>
33
+ <img src='https://img.shields.io/badge/HuggingFace-Model-blue?logo=huggingface' />
34
+ </a>
35
+ <br/>
36
+ <a href='https://modelscope.cn/studios/IndexTeam/IndexTTS-2-Demo'>
37
+ <img src='https://img.shields.io/badge/ModelScope-Demo-purple?logo=modelscope'/>
38
+ </>
39
+ <a href='https://modelscope.cn/models/IndexTeam/IndexTTS-2'>
40
+ <img src='https://img.shields.io/badge/ModelScope-Model-purple?logo=modelscope'/>
41
+ </a>
42
+ </div>
43
+
44
+ ### 摘要
45
+
46
+ 现有自回归大规模文本转语音(TTS)模型在语音自然度方面具有优势,但其逐token生成机制难以精确控制合成语音的时长。这在需要严格视音频同步的应用(如视频配音)中成为显著限制。
47
+
48
+ 本文提出了IndexTTS2,创新性地提出了一种通用且适用于自回归模型的语音时长控制方法。
49
+
50
+ 该方法支持两种生成模式:一种可显式指定生成token数量以精确控制语音时长;另一种则自由自回归生成语音,同时忠实还原输入提示的韵律特征。
51
+
52
+ 此外,IndexTTS2实现了情感表达与说话人身份的解耦,可独立控制音色和情感。在零样本设置下,模型能准确复刻目标音色(来自音色提示),同时完美还原指定的情感语调(来自风格提示)。
53
+
54
+ 为提升高情感表达下的语音清晰度,我们引入GPT潜在表示,并设计了三阶段训练范式,提升生成语音的稳定性。为降低情感控制门槛,我们基于文本描述微调Qwen3,设计了软指令机制,有效引导语音生成所需情感。
55
+
56
+ 多数据集实验结果表明,IndexTTS2在词错误率、说话人相似度和情感保真度方面均超越现有零样本TTS模型。音频样例见:<a href="https://index-tts.github.io/index-tts2.github.io/">IndexTTS2演示页面</a>。
57
+
58
+ **Tips:** 如需更多信息请联系作者。商业合作请联系 <u>indexspeech@bilibili.com</u>。
59
+
60
+ ### IndexTTS2体验
61
+
62
+ <div align="center">
63
+
64
+ **IndexTTS2:语音未来,现已生成**
65
+
66
+ [![IndexTTS2 Demo](../assets/IndexTTS2-video-pic.png)](https://www.bilibili.com/video/BV136a9zqEk5)
67
+
68
+ *点击图片观看IndexTTS2介绍视频*
69
+
70
+ </div>
71
+
72
+ ### 联系方式
73
+
74
+ QQ群:663272642(4群) 1013410623(5群) \
75
+ Discord:https://discord.gg/uT32E7KDmy \
76
+ 邮箱:indexspeech@bilibili.com \
77
+ 欢迎加入我们的社区!🌏 \
78
+ 欢迎大家交流讨论!
79
+
80
+ > [!CAUTION]
81
+ > 感谢大家对bilibili indextts项目的支持与关注!
82
+ > 请注意,目前由核心团队直接维护的**官方渠道仅有**: [https://github.com/index-tts/index-tts](https://github.com/index-tts/index-tts).
83
+ > ***其他任何网站或服务均非官方提供***,我们对其内容及安全性、准确性和及时性不作任何担保。
84
+ > 为了保障您的权益,建议通过上述官方渠道获取bilibili indextts项目的最新进展与更新。
85
+
86
+
87
+ ## 📣 更新日志
88
+
89
+ - `2025/09/08` 🔥🔥🔥 IndexTTS-2全球发布!
90
+ - 首个支持精确合成时长控制的自回归TTS模型,支持可控与非可控模式。<i>本版本暂未开放该功能。</i>
91
+ - 模型实现高度情感表达的语音合成,支持多模态情感控制。
92
+ - `2025/05/14` 🔥🔥 IndexTTS-1.5发布,显著提升模型稳定性及英文表现。
93
+ - `2025/03/25` 🔥 IndexTTS-1.0发布,开放模型权重与推理代码。
94
+ - `2025/02/12` 🔥 论文提交arXiv,发布演示与测试集。
95
+
96
+ ## 🖥️ 神经网络架构
97
+
98
+ IndexTTS2架构总览:
99
+
100
+ <picture>
101
+ <img src="../assets/IndexTTS2.png" width="800"/>
102
+ </picture>
103
+
104
+ 主要创新点:
105
+
106
+ - 提出自回归TTS模型的时长自适应方案。IndexTTS2是首个将精确时长控制与自然时长生成结合的自回归零样本TTS模型,方法可扩展至任意自回归大模型。
107
+ - 情感与说话人特征从提示中解耦,设计特征融合策略,在高情感表达下保持语义流畅与发音清晰,并开发了基于自然语言���述的情感控制工具。
108
+ - 针对高表达性语音数据缺乏,提出高效训练策略,显著提升零样本TTS情感表达至SOTA水平。
109
+ - 代码与预训练权重将公开,促进后续研究与应用。
110
+
111
+ ## 模型下载
112
+
113
+ | **HuggingFace** | **ModelScope** |
114
+ |----------------------------------------------------------|----------------------------------------------------------|
115
+ | [😁 IndexTTS-2](https://huggingface.co/IndexTeam/IndexTTS-2) | [IndexTTS-2](https://modelscope.cn/models/IndexTeam/IndexTTS-2) |
116
+ | [IndexTTS-1.5](https://huggingface.co/IndexTeam/IndexTTS-1.5) | [IndexTTS-1.5](https://modelscope.cn/models/IndexTeam/IndexTTS-1.5) |
117
+ | [IndexTTS](https://huggingface.co/IndexTeam/Index-TTS) | [IndexTTS](https://modelscope.cn/models/IndexTeam/Index-TTS) |
118
+
119
+ ## 使用说明
120
+
121
+ ### ⚙️ 环境配置
122
+
123
+ 1. 请确保已安装 [git](https://git-scm.com/downloads) 和 [git-lfs](https://git-lfs.com/)。
124
+
125
+ 在仓库中启用Git-LFS:
126
+
127
+ ```bash
128
+ git lfs install
129
+ ```
130
+
131
+ 2. 下载代码:
132
+
133
+ ```bash
134
+ git clone https://github.com/index-tts/index-tts.git && cd index-tts
135
+ git lfs pull # 下载大文件
136
+ ```
137
+
138
+ 3. 安装 [uv 包管理器](https://docs.astral.sh/uv/getting-started/installation/)。
139
+ *必须*使用uv保证依赖环境可靠。
140
+
141
+ > [!TIP]
142
+ > **快速安装方法:**
143
+ >
144
+ > uv安装方式多样,详见官网。也可快速安装:
145
+ >
146
+ > ```bash
147
+ > pip install -U uv
148
+ > ```
149
+
150
+ > [!WARNING]
151
+ > 本文档仅支持uv安装。其他工具如conda/pip无法保证依赖正确,可能导致*偶发bug、报错、GPU加速失效*等问题。
152
+ >
153
+ > uv比pip快[115倍](https://github.com/astral-sh/uv/blob/main/BENCHMARKS.md),强烈推荐。
154
+
155
+ 4. 安装依赖:
156
+
157
+ 使用uv安装依赖时,会创建虚拟环境,将所有依赖安装到`.venv`目录:
158
+
159
+ ```bash
160
+ uv sync --all-extras
161
+ ```
162
+
163
+ 如中国大陆地区用户下载缓慢,可选用国内镜像:
164
+
165
+ ```bash
166
+ uv sync --all-extras --default-index "https://mirrors.aliyun.com/pypi/simple"
167
+
168
+ uv sync --all-extras --default-index "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"
169
+ ```
170
+
171
+ > [!TIP]
172
+ > **可选功能:**
173
+ >
174
+ > - `--all-extras`:安装全部可选功能。可去除自定义。
175
+ > - `--extra webui`:安装WebUI支持(推荐)。
176
+ > - `--extra deepspeed`:安装DeepSpeed加速。
177
+
178
+ > [!IMPORTANT]
179
+ > **Windows注意:** DeepSpeed在部分Windows环境较难安装,可去除`--all-extras`。
180
+ >
181
+ > **Linux/Windows注意:** 如遇CUDA相关报错,请确保已安装NVIDIA [CUDA Toolkit](https://developer.nvidia.com/cuda-toolkit) 12.8及以上。
182
+
183
+ 5. 下载模型:
184
+
185
+ HuggingFace下载:
186
+
187
+ ```bash
188
+ uv tool install "huggingface-hub[cli,hf_xet]"
189
+
190
+ hf download IndexTeam/IndexTTS-2 --local-dir=checkpoints
191
+ ```
192
+
193
+ ModelScope下载:
194
+
195
+ ```bash
196
+ uv tool install "modelscope"
197
+
198
+ modelscope download --model IndexTeam/IndexTTS-2 --local_dir checkpoints
199
+ ```
200
+
201
+ > [!NOTE]
202
+ > 项目首次运行还会自动下载部分小模型。如网络访问HuggingFace较慢,建议提前设置:
203
+ >
204
+ > ```bash
205
+ > export HF_ENDPOINT="https://hf-mirror.com"
206
+ > ```
207
+
208
+ #### 🖥️ PyTorch GPU 加速检测
209
+
210
+ 可运行脚本检测机器是否有GPU,以及是否安装了GPU版本的PyTorch。(如PyTorch版本不对,可能使用CPU启动,推理会非常慢)
211
+
212
+ ```bash
213
+ uv run tools/gpu_check.py
214
+ ```
215
+
216
+ ### 🔥 IndexTTS2快速体验
217
+
218
+ #### 🌐 Web演示
219
+
220
+ ```bash
221
+ uv run webui.py
222
+ ```
223
+
224
+ 浏览器访问 `http://127.0.0.1:7860` 查看演示。
225
+
226
+ 可通过命令行参数开启FP16推理(降低显存占用)、DeepSpeed加速、CUDA内核编译加速等。可运行以下命令查看所有选项:
227
+
228
+ ```bash
229
+ uv run webui.py -h
230
+ ```
231
+
232
+ 祝使用愉快!
233
+
234
+ #### 📝 Python脚本调用
235
+
236
+ 用`uv run <file.py>`保证程序在uv创建的虚拟环境下运行。部分情况需要指定`PYTHONPATH`。
237
+
238
+ 示例:
239
+
240
+ ```bash
241
+ PYTHONPATH="$PYTHONPATH:." uv run indextts/infer_v2.py
242
+ ```
243
+
244
+ 以下为IndexTTS2脚本调用示例:
245
+
246
+ 1. 单一参考音频(音色克隆):
247
+
248
+ ```python
249
+ from indextts.infer_v2 import IndexTTS2
250
+ tts = IndexTTS2(cfg_path="checkpoints/config.yaml", model_dir="checkpoints", use_fp16=False, use_cuda_kernel=False, use_deepspeed=False)
251
+ text = "Translate for me, what is a surprise!"
252
+ tts.infer(spk_audio_prompt='examples/voice_01.wav', text=text, output_path="gen.wav", verbose=True)
253
+ ```
254
+
255
+ 2. 指定情感参考音频:
256
+
257
+ ```python
258
+ from indextts.infer_v2 import IndexTTS2
259
+ tts = IndexTTS2(cfg_path="checkpoints/config.yaml", model_dir="checkpoints", use_fp16=False, use_cuda_kernel=False, use_deepspeed=False)
260
+ text = "酒楼丧尽天良,开始借机竞拍房间,哎,一群蠢货。"
261
+ tts.infer(spk_audio_prompt='examples/voice_07.wav', text=text, output_path="gen.wav", emo_audio_prompt="examples/emo_sad.wav", verbose=True)
262
+ ```
263
+
264
+ 3. 可调节情感参考音频的权重(`emo_alpha`,范围0.0-1.0,默认1.0):
265
+
266
+ ```python
267
+ from indextts.infer_v2 import IndexTTS2
268
+ tts = IndexTTS2(cfg_path="checkpoints/config.yaml", model_dir="checkpoints", use_fp16=False, use_cuda_kernel=False, use_deepspeed=False)
269
+ text = "酒楼丧尽天良,开始借机竞拍房间,哎,一群蠢货。"
270
+ tts.infer(spk_audio_prompt='examples/voice_07.wav', text=text, output_path="gen.wav", emo_audio_prompt="examples/emo_sad.wav", emo_alpha=0.9, verbose=True)
271
+ ```
272
+
273
+ 4. 可直接指定8维情感向量 `[高兴, 愤怒, 悲伤, 害怕, 厌恶, 忧郁, 惊讶, 平静]`,可用`use_random`开启随机情感采样(默认False):
274
+
275
+ > [!NOTE]
276
+ > 开启随机采样会降低音色的还原度。
277
+
278
+ ```python
279
+ from indextts.infer_v2 import IndexTTS2
280
+ tts = IndexTTS2(cfg_path="checkpoints/config.yaml", model_dir="checkpoints", use_fp16=False, use_cuda_kernel=False, use_deepspeed=False)
281
+ text = "哇塞!这个爆率也太高了!欧皇附体了!"
282
+ tts.infer(spk_audio_prompt='examples/voice_10.wav', text=text, output_path="gen.wav", emo_vector=[0, 0, 0, 0, 0, 0, 0.45, 0], use_random=False, verbose=True)
283
+ ```
284
+
285
+ 5. 可用`use_emo_text`根据文本自动生成情感向量,可用`use_random`开启随机情感采样:
286
+
287
+ ```python
288
+ from indextts.infer_v2 import IndexTTS2
289
+ tts = IndexTTS2(cfg_path="checkpoints/config.yaml", model_dir="checkpoints", use_fp16=False, use_cuda_kernel=False, use_deepspeed=False)
290
+ text = "快躲起来!是他要来了!他要来抓我们了!"
291
+ tts.infer(spk_audio_prompt='examples/voice_12.wav', text=text, output_path="gen.wav", emo_alpha=0.6, use_emo_text=True, use_random=False, verbose=True)
292
+ ```
293
+
294
+ 6. 可直接指定情感文本描述(`emo_text`),实现文本与情感分离控制:
295
+
296
+ ```python
297
+ from indextts.infer_v2 import IndexTTS2
298
+ tts = IndexTTS2(cfg_path="checkpoints/config.yaml", model_dir="checkpoints", use_fp16=False, use_cuda_kernel=False, use_deepspeed=False)
299
+ text = "快躲起来!是他要来了!他要来抓我们了!"
300
+ emo_text = "你吓死我了!你是鬼吗?"
301
+ tts.infer(spk_audio_prompt='examples/voice_12.wav', text=text, output_path="gen.wav", emo_alpha=0.6, use_emo_text=True, emo_text=emo_text, use_random=False, verbose=True)
302
+ ```
303
+
304
+ > [!TIP]
305
+ > **拼音使用注意事项:**
306
+ >
307
+ > IndexTTS2依然支持中文字符与拼音混合建模。
308
+ > 在使用时,如果需要精确的发音控制,请输入包含特定拼音标注的文本来触发拼音控制功能。
309
+ > 需要注意的是:拼音控制并不是对所有声母韵母(辅音、元音)组合都生效,系统仅保留中文合法拼音的发音。
310
+ > 具体合法情况可参考项目中的`checkpoints/pinyin.vocab`文件。
311
+ >
312
+ > 参考样例:
313
+ > ```
314
+ > 之前你做DE5很好,所以这一次也DEI3做DE2很好才XING2,如果这次目标完成得不错的话,我们就直接打DI1去银行取钱。
315
+ > ```
316
+
317
+ ### 旧版IndexTTS1使用指南
318
+
319
+ 如果需要使用旧的IndexTTS1.5模型,可以import旧模块:
320
+
321
+ ```python
322
+ from indextts.infer import IndexTTS
323
+ tts = IndexTTS(model_dir="checkpoints",cfg_path="checkpoints/config.yaml")
324
+ voice = "examples/voice_07.wav"
325
+ text = "大家好,我现在正在bilibili 体验 ai 科技,说实话,来之前我绝对想不到!AI技术已经发展到这样匪夷所思的地步了!比如说,现在正在说话的其实是B站为我现场复刻的数字分身,简直就是平行宇宙的另一个我了。如果大家也想体验更多深入的AIGC功能,可以访问 bilibili studio,相信我,你们也会吃惊的。"
326
+ tts.infer(voice, text, 'gen.wav')
327
+ ```
328
+
329
+ 详细信息见 [README_INDEXTTS_1_5](archive/README_INDEXTTS_1_5.md),或访问 <a href="https://github.com/index-tts/index-tts/tree/v1.5.0">index-tts:v1.5.0</a>。
330
+
331
+ ## 演示
332
+
333
+ ### IndexTTS2: [[论文]](https://arxiv.org/abs/2506.21619); [[演示]](https://index-tts.github.io/index-tts2.github.io/); [[ModelScope]](https://modelscope.cn/studios/IndexTeam/IndexTTS-2-Demo); [[HuggingFace]](https://huggingface.co/spaces/IndexTeam/IndexTTS-2-Demo)
334
+
335
+ ### IndexTTS1: [[论文]](https://arxiv.org/abs/2502.05512); [[演示]](https://index-tts.github.io/); [[ModelScope]](https://modelscope.cn/studios/IndexTeam/IndexTTS-Demo); [[HuggingFace]](https://huggingface.co/spaces/IndexTeam/IndexTTS)
336
+
337
+ ## 致谢
338
+
339
+ 1. [tortoise-tts](https://github.com/neonbjb/tortoise-tts)
340
+ 2. [XTTSv2](https://github.com/coqui-ai/TTS)
341
+ 3. [BigVGAN](https://github.com/NVIDIA/BigVGAN)
342
+ 4. [wenet](https://github.com/wenet-e2e/wenet/tree/main)
343
+ 5. [icefall](https://github.com/k2-fsa/icefall)
344
+ 6. [maskgct](https://github.com/open-mmlab/Amphion/tree/main/models/tts/maskgct)
345
+ 7. [seed-vc](https://github.com/Plachtaa/seed-vc)
346
+
347
+ ## Bilibili 贡献者名录
348
+ 我们诚挚感谢来自Bilibili的同事们,是大家的共同努力让IndexTTS系列得以实现。
349
+
350
+ ### 核心作者
351
+ - **Siyi Zhou** – 核心作者;在IndexTTS2中主导模型架构设计与训练流程优化,重点推动多语言、多情感合成等关键功能。
352
+ - **Wei Deng** – 核心作者;在IndexTTS1中主导模型架构设计与训练流程,负责基础能力建设与性能优化。
353
+ - **Jingchen Shu** – 核心作者;负责整体架构设计、跨语种建模方案与训练策略优化,推动模型迭��。
354
+ - **Xun Zhou** – 核心作者;负责跨语言数据处理与实验,探索多语种训练策略,并在音质提升与稳定性评估方面作出贡献。
355
+ - **Jinchao Wang** – 核心作者;负责模型开发与部署,构建推理框架并支持系统落地。
356
+ - **Yiquan Zhou** – 核心作者;参与模型实验与验证,并提出并实现了基于文本的情感控制。
357
+ - **Yi He** – 核心作者;参与模型实验与验证。
358
+ - **Lu Wang** – 核心作者;负责数据处理与模型评测,支持模型训练与性能验证。
359
+
360
+ ### 技术贡献者
361
+ - **Yining Wang** – 技术贡献者;负责开源代码的实现与维护,支持功能适配与社区发布。
362
+ - **Yong Wu** – 技术贡献者;参与数据处理与实验支持,保障模型训练的数据质量与迭代效率。
363
+ - **Yaqin Huang** – 技术贡献者;参与系统性模型评估与效果跟进,提供反馈以支持迭代优化。
364
+ - **Yunhan Xu** – 技术贡献者;在录音与数据采集方面提供指导,并从产品与运营角度提出改进建议,提升模型的易用性与实际应用效果。
365
+ - **Yuelang Sun** – 技术贡献者;在音频录制与数据采集方面提供专业支持,保障模型训练与评测所需的高质量数据。
366
+ - **Yihuang Liang** – 技术贡献者;参与系统性模型评估与项目推广,帮助IndexTTS项目扩大影响力并提升用户参与度。
367
+
368
+ ### 技术指导
369
+ - **Huyang Sun** – 对IndexTTS项目给予了大力支持,确保了项目的战略方向与资源保障。
370
+ - **Bin Xia** – 参与技术方案的评审、优化与跟进,重点关注模型效果的保障。
371
+
372
+ ## 📚 论文引用
373
+
374
+ 🌟 如果本项目对您有帮助,请为我们点star并引用论文。
375
+
376
+ IndexTTS2:
377
+
378
+ ```
379
+ @article{zhou2025indextts2,
380
+ title={IndexTTS2: A Breakthrough in Emotionally Expressive and Duration-Controlled Auto-Regressive Zero-Shot Text-to-Speech},
381
+ author={Siyi Zhou, Yiquan Zhou, Yi He, Xun Zhou, Jinchao Wang, Wei Deng, Jingchen Shu},
382
+ journal={arXiv preprint arXiv:2506.21619},
383
+ year={2025}
384
+ }
385
+ ```
386
+
387
+ IndexTTS:
388
+
389
+ ```
390
+ @article{deng2025indextts,
391
+ title={IndexTTS: An Industrial-Level Controllable and Efficient Zero-Shot Text-To-Speech System},
392
+ author={Wei Deng, Siyi Zhou, Jingchen Shu, Jinchao Wang, Lu Wang},
393
+ journal={arXiv preprint arXiv:2502.05512},
394
+ year={2025},
395
+ doi={10.48550/arXiv.2502.05512},
396
+ url={https://arxiv.org/abs/2502.05512}
397
+ }
398
+ ```
399
+
freeze.txt ADDED
Binary file (5.87 kB). View file
 
indextts/BigVGAN/ECAPA_TDNN.py ADDED
@@ -0,0 +1,656 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """A popular speaker recognition and diarization model.
2
+
3
+ Authors
4
+ * Hwidong Na 2020
5
+ """
6
+
7
+ import torch # noqa: F401
8
+ import torch.nn as nn
9
+ import torch.nn.functional as F
10
+
11
+ from indextts.BigVGAN.nnet.CNN import Conv1d as _Conv1d
12
+ from indextts.BigVGAN.nnet.linear import Linear
13
+ from indextts.BigVGAN.nnet.normalization import BatchNorm1d as _BatchNorm1d
14
+
15
+
16
+ def length_to_mask(length, max_len=None, dtype=None, device=None):
17
+ """Creates a binary mask for each sequence.
18
+
19
+ Reference: https://discuss.pytorch.org/t/how-to-generate-variable-length-mask/23397/3
20
+
21
+ Arguments
22
+ ---------
23
+ length : torch.LongTensor
24
+ Containing the length of each sequence in the batch. Must be 1D.
25
+ max_len : int
26
+ Max length for the mask, also the size of the second dimension.
27
+ dtype : torch.dtype, default: None
28
+ The dtype of the generated mask.
29
+ device: torch.device, default: None
30
+ The device to put the mask variable.
31
+
32
+ Returns
33
+ -------
34
+ mask : tensor
35
+ The binary mask.
36
+
37
+ Example
38
+ -------
39
+ >>> length=torch.Tensor([1,2,3])
40
+ >>> mask=length_to_mask(length)
41
+ >>> mask
42
+ tensor([[1., 0., 0.],
43
+ [1., 1., 0.],
44
+ [1., 1., 1.]])
45
+ """
46
+ assert len(length.shape) == 1
47
+
48
+ if max_len is None:
49
+ max_len = length.max().long().item() # using arange to generate mask
50
+ mask = torch.arange(
51
+ max_len, device=length.device, dtype=length.dtype
52
+ ).expand(len(length), max_len) < length.unsqueeze(1)
53
+
54
+ if dtype is None:
55
+ dtype = length.dtype
56
+
57
+ if device is None:
58
+ device = length.device
59
+
60
+ mask = torch.as_tensor(mask, dtype=dtype, device=device)
61
+ return mask
62
+
63
+
64
+ # Skip transpose as much as possible for efficiency
65
+ class Conv1d(_Conv1d):
66
+ """1D convolution. Skip transpose is used to improve efficiency."""
67
+
68
+ def __init__(self, *args, **kwargs):
69
+ super().__init__(skip_transpose=True, *args, **kwargs)
70
+
71
+
72
+ class BatchNorm1d(_BatchNorm1d):
73
+ """1D batch normalization. Skip transpose is used to improve efficiency."""
74
+
75
+ def __init__(self, *args, **kwargs):
76
+ super().__init__(skip_transpose=True, *args, **kwargs)
77
+
78
+
79
+ class TDNNBlock(nn.Module):
80
+ """An implementation of TDNN.
81
+
82
+ Arguments
83
+ ---------
84
+ in_channels : int
85
+ Number of input channels.
86
+ out_channels : int
87
+ The number of output channels.
88
+ kernel_size : int
89
+ The kernel size of the TDNN blocks.
90
+ dilation : int
91
+ The dilation of the TDNN block.
92
+ activation : torch class
93
+ A class for constructing the activation layers.
94
+ groups : int
95
+ The groups size of the TDNN blocks.
96
+
97
+ Example
98
+ -------
99
+ >>> inp_tensor = torch.rand([8, 120, 64]).transpose(1, 2)
100
+ >>> layer = TDNNBlock(64, 64, kernel_size=3, dilation=1)
101
+ >>> out_tensor = layer(inp_tensor).transpose(1, 2)
102
+ >>> out_tensor.shape
103
+ torch.Size([8, 120, 64])
104
+ """
105
+
106
+ def __init__(
107
+ self,
108
+ in_channels,
109
+ out_channels,
110
+ kernel_size,
111
+ dilation,
112
+ activation=nn.ReLU,
113
+ groups=1,
114
+ ):
115
+ super().__init__()
116
+ self.conv = Conv1d(
117
+ in_channels=in_channels,
118
+ out_channels=out_channels,
119
+ kernel_size=kernel_size,
120
+ dilation=dilation,
121
+ groups=groups,
122
+ )
123
+ self.activation = activation()
124
+ self.norm = BatchNorm1d(input_size=out_channels)
125
+
126
+ def forward(self, x):
127
+ """Processes the input tensor x and returns an output tensor."""
128
+ return self.norm(self.activation(self.conv(x)))
129
+
130
+
131
+ class Res2NetBlock(torch.nn.Module):
132
+ """An implementation of Res2NetBlock w/ dilation.
133
+
134
+ Arguments
135
+ ---------
136
+ in_channels : int
137
+ The number of channels expected in the input.
138
+ out_channels : int
139
+ The number of output channels.
140
+ scale : int
141
+ The scale of the Res2Net block.
142
+ kernel_size: int
143
+ The kernel size of the Res2Net block.
144
+ dilation : int
145
+ The dilation of the Res2Net block.
146
+
147
+ Example
148
+ -------
149
+ >>> inp_tensor = torch.rand([8, 120, 64]).transpose(1, 2)
150
+ >>> layer = Res2NetBlock(64, 64, scale=4, dilation=3)
151
+ >>> out_tensor = layer(inp_tensor).transpose(1, 2)
152
+ >>> out_tensor.shape
153
+ torch.Size([8, 120, 64])
154
+ """
155
+
156
+ def __init__(
157
+ self, in_channels, out_channels, scale=8, kernel_size=3, dilation=1
158
+ ):
159
+ super().__init__()
160
+ assert in_channels % scale == 0
161
+ assert out_channels % scale == 0
162
+
163
+ in_channel = in_channels // scale
164
+ hidden_channel = out_channels // scale
165
+
166
+ self.blocks = nn.ModuleList(
167
+ [
168
+ TDNNBlock(
169
+ in_channel,
170
+ hidden_channel,
171
+ kernel_size=kernel_size,
172
+ dilation=dilation,
173
+ )
174
+ for i in range(scale - 1)
175
+ ]
176
+ )
177
+ self.scale = scale
178
+
179
+ def forward(self, x):
180
+ """Processes the input tensor x and returns an output tensor."""
181
+ y = []
182
+ for i, x_i in enumerate(torch.chunk(x, self.scale, dim=1)):
183
+ if i == 0:
184
+ y_i = x_i
185
+ elif i == 1:
186
+ y_i = self.blocks[i - 1](x_i)
187
+ else:
188
+ y_i = self.blocks[i - 1](x_i + y_i)
189
+ y.append(y_i)
190
+ y = torch.cat(y, dim=1)
191
+ return y
192
+
193
+
194
+ class SEBlock(nn.Module):
195
+ """An implementation of squeeze-and-excitation block.
196
+
197
+ Arguments
198
+ ---------
199
+ in_channels : int
200
+ The number of input channels.
201
+ se_channels : int
202
+ The number of output channels after squeeze.
203
+ out_channels : int
204
+ The number of output channels.
205
+
206
+ Example
207
+ -------
208
+ >>> inp_tensor = torch.rand([8, 120, 64]).transpose(1, 2)
209
+ >>> se_layer = SEBlock(64, 16, 64)
210
+ >>> lengths = torch.rand((8,))
211
+ >>> out_tensor = se_layer(inp_tensor, lengths).transpose(1, 2)
212
+ >>> out_tensor.shape
213
+ torch.Size([8, 120, 64])
214
+ """
215
+
216
+ def __init__(self, in_channels, se_channels, out_channels):
217
+ super().__init__()
218
+
219
+ self.conv1 = Conv1d(
220
+ in_channels=in_channels, out_channels=se_channels, kernel_size=1
221
+ )
222
+ self.relu = torch.nn.ReLU(inplace=True)
223
+ self.conv2 = Conv1d(
224
+ in_channels=se_channels, out_channels=out_channels, kernel_size=1
225
+ )
226
+ self.sigmoid = torch.nn.Sigmoid()
227
+
228
+ def forward(self, x, lengths=None):
229
+ """Processes the input tensor x and returns an output tensor."""
230
+ L = x.shape[-1]
231
+ if lengths is not None:
232
+ mask = length_to_mask(lengths * L, max_len=L, device=x.device)
233
+ mask = mask.unsqueeze(1)
234
+ total = mask.sum(dim=2, keepdim=True)
235
+ s = (x * mask).sum(dim=2, keepdim=True) / total
236
+ else:
237
+ s = x.mean(dim=2, keepdim=True)
238
+
239
+ s = self.relu(self.conv1(s))
240
+ s = self.sigmoid(self.conv2(s))
241
+
242
+ return s * x
243
+
244
+
245
+ class AttentiveStatisticsPooling(nn.Module):
246
+ """This class implements an attentive statistic pooling layer for each channel.
247
+ It returns the concatenated mean and std of the input tensor.
248
+
249
+ Arguments
250
+ ---------
251
+ channels: int
252
+ The number of input channels.
253
+ attention_channels: int
254
+ The number of attention channels.
255
+ global_context: bool
256
+ Whether to use global context.
257
+
258
+ Example
259
+ -------
260
+ >>> inp_tensor = torch.rand([8, 120, 64]).transpose(1, 2)
261
+ >>> asp_layer = AttentiveStatisticsPooling(64)
262
+ >>> lengths = torch.rand((8,))
263
+ >>> out_tensor = asp_layer(inp_tensor, lengths).transpose(1, 2)
264
+ >>> out_tensor.shape
265
+ torch.Size([8, 1, 128])
266
+ """
267
+
268
+ def __init__(self, channels, attention_channels=128, global_context=True):
269
+ super().__init__()
270
+
271
+ self.eps = 1e-12
272
+ self.global_context = global_context
273
+ if global_context:
274
+ self.tdnn = TDNNBlock(channels * 3, attention_channels, 1, 1)
275
+ else:
276
+ self.tdnn = TDNNBlock(channels, attention_channels, 1, 1)
277
+ self.tanh = nn.Tanh()
278
+ self.conv = Conv1d(
279
+ in_channels=attention_channels, out_channels=channels, kernel_size=1
280
+ )
281
+
282
+ def forward(self, x, lengths=None):
283
+ """Calculates mean and std for a batch (input tensor).
284
+
285
+ Arguments
286
+ ---------
287
+ x : torch.Tensor
288
+ Tensor of shape [N, C, L].
289
+ lengths : torch.Tensor
290
+ The corresponding relative lengths of the inputs.
291
+
292
+ Returns
293
+ -------
294
+ pooled_stats : torch.Tensor
295
+ mean and std of batch
296
+ """
297
+ L = x.shape[-1]
298
+
299
+ def _compute_statistics(x, m, dim=2, eps=self.eps):
300
+ mean = (m * x).sum(dim)
301
+ std = torch.sqrt(
302
+ (m * (x - mean.unsqueeze(dim)).pow(2)).sum(dim).clamp(eps)
303
+ )
304
+ return mean, std
305
+
306
+ if lengths is None:
307
+ lengths = torch.ones(x.shape[0], device=x.device)
308
+
309
+ # Make binary mask of shape [N, 1, L]
310
+ mask = length_to_mask(lengths * L, max_len=L, device=x.device)
311
+ mask = mask.unsqueeze(1)
312
+
313
+ # Expand the temporal context of the pooling layer by allowing the
314
+ # self-attention to look at global properties of the utterance.
315
+ if self.global_context:
316
+ # torch.std is unstable for backward computation
317
+ # https://github.com/pytorch/pytorch/issues/4320
318
+ total = mask.sum(dim=2, keepdim=True).float()
319
+ mean, std = _compute_statistics(x, mask / total)
320
+ mean = mean.unsqueeze(2).repeat(1, 1, L)
321
+ std = std.unsqueeze(2).repeat(1, 1, L)
322
+ attn = torch.cat([x, mean, std], dim=1)
323
+ else:
324
+ attn = x
325
+
326
+ # Apply layers
327
+ attn = self.conv(self.tanh(self.tdnn(attn)))
328
+
329
+ # Filter out zero-paddings
330
+ attn = attn.masked_fill(mask == 0, float("-inf"))
331
+
332
+ attn = F.softmax(attn, dim=2)
333
+ mean, std = _compute_statistics(x, attn)
334
+ # Append mean and std of the batch
335
+ pooled_stats = torch.cat((mean, std), dim=1)
336
+ pooled_stats = pooled_stats.unsqueeze(2)
337
+
338
+ return pooled_stats
339
+
340
+
341
+ class SERes2NetBlock(nn.Module):
342
+ """An implementation of building block in ECAPA-TDNN, i.e.,
343
+ TDNN-Res2Net-TDNN-SEBlock.
344
+
345
+ Arguments
346
+ ---------
347
+ in_channels: int
348
+ Expected size of input channels.
349
+ out_channels: int
350
+ The number of output channels.
351
+ res2net_scale: int
352
+ The scale of the Res2Net block.
353
+ se_channels : int
354
+ The number of output channels after squeeze.
355
+ kernel_size: int
356
+ The kernel size of the TDNN blocks.
357
+ dilation: int
358
+ The dilation of the Res2Net block.
359
+ activation : torch class
360
+ A class for constructing the activation layers.
361
+ groups: int
362
+ Number of blocked connections from input channels to output channels.
363
+
364
+ Example
365
+ -------
366
+ >>> x = torch.rand(8, 120, 64).transpose(1, 2)
367
+ >>> conv = SERes2NetBlock(64, 64, res2net_scale=4)
368
+ >>> out = conv(x).transpose(1, 2)
369
+ >>> out.shape
370
+ torch.Size([8, 120, 64])
371
+ """
372
+
373
+ def __init__(
374
+ self,
375
+ in_channels,
376
+ out_channels,
377
+ res2net_scale=8,
378
+ se_channels=128,
379
+ kernel_size=1,
380
+ dilation=1,
381
+ activation=torch.nn.ReLU,
382
+ groups=1,
383
+ ):
384
+ super().__init__()
385
+ self.out_channels = out_channels
386
+ self.tdnn1 = TDNNBlock(
387
+ in_channels,
388
+ out_channels,
389
+ kernel_size=1,
390
+ dilation=1,
391
+ activation=activation,
392
+ groups=groups,
393
+ )
394
+ self.res2net_block = Res2NetBlock(
395
+ out_channels, out_channels, res2net_scale, kernel_size, dilation
396
+ )
397
+ self.tdnn2 = TDNNBlock(
398
+ out_channels,
399
+ out_channels,
400
+ kernel_size=1,
401
+ dilation=1,
402
+ activation=activation,
403
+ groups=groups,
404
+ )
405
+ self.se_block = SEBlock(out_channels, se_channels, out_channels)
406
+
407
+ self.shortcut = None
408
+ if in_channels != out_channels:
409
+ self.shortcut = Conv1d(
410
+ in_channels=in_channels,
411
+ out_channels=out_channels,
412
+ kernel_size=1,
413
+ )
414
+
415
+ def forward(self, x, lengths=None):
416
+ """Processes the input tensor x and returns an output tensor."""
417
+ residual = x
418
+ if self.shortcut:
419
+ residual = self.shortcut(x)
420
+
421
+ x = self.tdnn1(x)
422
+ x = self.res2net_block(x)
423
+ x = self.tdnn2(x)
424
+ x = self.se_block(x, lengths)
425
+
426
+ return x + residual
427
+
428
+
429
+ class ECAPA_TDNN(torch.nn.Module):
430
+ """An implementation of the speaker embedding model in a paper.
431
+ "ECAPA-TDNN: Emphasized Channel Attention, Propagation and Aggregation in
432
+ TDNN Based Speaker Verification" (https://arxiv.org/abs/2005.07143).
433
+
434
+ Arguments
435
+ ---------
436
+ input_size : int
437
+ Expected size of the input dimension.
438
+ device : str
439
+ Device used, e.g., "cpu" or "cuda".
440
+ lin_neurons : int
441
+ Number of neurons in linear layers.
442
+ activation : torch class
443
+ A class for constructing the activation layers.
444
+ channels : list of ints
445
+ Output channels for TDNN/SERes2Net layer.
446
+ kernel_sizes : list of ints
447
+ List of kernel sizes for each layer.
448
+ dilations : list of ints
449
+ List of dilations for kernels in each layer.
450
+ attention_channels: int
451
+ The number of attention channels.
452
+ res2net_scale : int
453
+ The scale of the Res2Net block.
454
+ se_channels : int
455
+ The number of output channels after squeeze.
456
+ global_context: bool
457
+ Whether to use global context.
458
+ groups : list of ints
459
+ List of groups for kernels in each layer.
460
+
461
+ Example
462
+ -------
463
+ >>> input_feats = torch.rand([5, 120, 80])
464
+ >>> compute_embedding = ECAPA_TDNN(80, lin_neurons=192)
465
+ >>> outputs = compute_embedding(input_feats)
466
+ >>> outputs.shape
467
+ torch.Size([5, 1, 192])
468
+ """
469
+
470
+ def __init__(
471
+ self,
472
+ input_size,
473
+ device="cpu",
474
+ lin_neurons=192,
475
+ activation=torch.nn.ReLU,
476
+ channels=[512, 512, 512, 512, 1536],
477
+ kernel_sizes=[5, 3, 3, 3, 1],
478
+ dilations=[1, 2, 3, 4, 1],
479
+ attention_channels=128,
480
+ res2net_scale=8,
481
+ se_channels=128,
482
+ global_context=True,
483
+ groups=[1, 1, 1, 1, 1],
484
+ ):
485
+ super().__init__()
486
+ assert len(channels) == len(kernel_sizes)
487
+ assert len(channels) == len(dilations)
488
+ self.channels = channels
489
+ self.blocks = nn.ModuleList()
490
+
491
+ # The initial TDNN layer
492
+ self.blocks.append(
493
+ TDNNBlock(
494
+ input_size,
495
+ channels[0],
496
+ kernel_sizes[0],
497
+ dilations[0],
498
+ activation,
499
+ groups[0],
500
+ )
501
+ )
502
+
503
+ # SE-Res2Net layers
504
+ for i in range(1, len(channels) - 1):
505
+ self.blocks.append(
506
+ SERes2NetBlock(
507
+ channels[i - 1],
508
+ channels[i],
509
+ res2net_scale=res2net_scale,
510
+ se_channels=se_channels,
511
+ kernel_size=kernel_sizes[i],
512
+ dilation=dilations[i],
513
+ activation=activation,
514
+ groups=groups[i],
515
+ )
516
+ )
517
+
518
+ # Multi-layer feature aggregation
519
+ self.mfa = TDNNBlock(
520
+ channels[-2] * (len(channels) - 2),
521
+ channels[-1],
522
+ kernel_sizes[-1],
523
+ dilations[-1],
524
+ activation,
525
+ groups=groups[-1],
526
+ )
527
+
528
+ # Attentive Statistical Pooling
529
+ self.asp = AttentiveStatisticsPooling(
530
+ channels[-1],
531
+ attention_channels=attention_channels,
532
+ global_context=global_context,
533
+ )
534
+ self.asp_bn = BatchNorm1d(input_size=channels[-1] * 2)
535
+
536
+ # Final linear transformation
537
+ self.fc = Conv1d(
538
+ in_channels=channels[-1] * 2,
539
+ out_channels=lin_neurons,
540
+ kernel_size=1,
541
+ )
542
+
543
+ def forward(self, x, lengths=None):
544
+ """Returns the embedding vector.
545
+
546
+ Arguments
547
+ ---------
548
+ x : torch.Tensor
549
+ Tensor of shape (batch, time, channel).
550
+ lengths : torch.Tensor
551
+ Corresponding relative lengths of inputs.
552
+
553
+ Returns
554
+ -------
555
+ x : torch.Tensor
556
+ Embedding vector.
557
+ """
558
+ # Minimize transpose for efficiency
559
+ x = x.transpose(1, 2)
560
+
561
+ xl = []
562
+ for layer in self.blocks:
563
+ try:
564
+ x = layer(x, lengths=lengths)
565
+ except TypeError:
566
+ x = layer(x)
567
+ xl.append(x)
568
+
569
+ # Multi-layer feature aggregation
570
+ x = torch.cat(xl[1:], dim=1)
571
+ x = self.mfa(x)
572
+
573
+ # Attentive Statistical Pooling
574
+ x = self.asp(x, lengths=lengths)
575
+ x = self.asp_bn(x)
576
+
577
+ # Final linear transformation
578
+ x = self.fc(x)
579
+
580
+ x = x.transpose(1, 2)
581
+ return x
582
+
583
+
584
+ class Classifier(torch.nn.Module):
585
+ """This class implements the cosine similarity on the top of features.
586
+
587
+ Arguments
588
+ ---------
589
+ input_size : int
590
+ Expected size of input dimension.
591
+ device : str
592
+ Device used, e.g., "cpu" or "cuda".
593
+ lin_blocks : int
594
+ Number of linear layers.
595
+ lin_neurons : int
596
+ Number of neurons in linear layers.
597
+ out_neurons : int
598
+ Number of classes.
599
+
600
+ Example
601
+ -------
602
+ >>> classify = Classifier(input_size=2, lin_neurons=2, out_neurons=2)
603
+ >>> outputs = torch.tensor([ [1., -1.], [-9., 1.], [0.9, 0.1], [0.1, 0.9] ])
604
+ >>> outputs = outputs.unsqueeze(1)
605
+ >>> cos = classify(outputs)
606
+ >>> (cos < -1.0).long().sum()
607
+ tensor(0)
608
+ >>> (cos > 1.0).long().sum()
609
+ tensor(0)
610
+ """
611
+
612
+ def __init__(
613
+ self,
614
+ input_size,
615
+ device="cpu",
616
+ lin_blocks=0,
617
+ lin_neurons=192,
618
+ out_neurons=1211,
619
+ ):
620
+ super().__init__()
621
+ self.blocks = nn.ModuleList()
622
+
623
+ for block_index in range(lin_blocks):
624
+ self.blocks.extend(
625
+ [
626
+ _BatchNorm1d(input_size=input_size),
627
+ Linear(input_size=input_size, n_neurons=lin_neurons),
628
+ ]
629
+ )
630
+ input_size = lin_neurons
631
+
632
+ # Final Layer
633
+ self.weight = nn.Parameter(
634
+ torch.FloatTensor(out_neurons, input_size, device=device)
635
+ )
636
+ nn.init.xavier_uniform_(self.weight)
637
+
638
+ def forward(self, x):
639
+ """Returns the output probabilities over speakers.
640
+
641
+ Arguments
642
+ ---------
643
+ x : torch.Tensor
644
+ Torch tensor.
645
+
646
+ Returns
647
+ -------
648
+ out : torch.Tensor
649
+ Output probabilities over speakers.
650
+ """
651
+ for layer in self.blocks:
652
+ x = layer(x)
653
+
654
+ # Need to be normalized
655
+ x = F.linear(F.normalize(x.squeeze(1)), F.normalize(self.weight))
656
+ return x.unsqueeze(1)
indextts/BigVGAN/__init__.py ADDED
File without changes
indextts/BigVGAN/activations.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Implementation adapted from https://github.com/EdwardDixon/snake under the MIT license.
2
+ # LICENSE is in incl_licenses directory.
3
+
4
+ import torch
5
+ from torch import nn, pow, sin
6
+ from torch.nn import Parameter
7
+
8
+
9
+ class Snake(nn.Module):
10
+ '''
11
+ Implementation of a sine-based periodic activation function
12
+ Shape:
13
+ - Input: (B, C, T)
14
+ - Output: (B, C, T), same shape as the input
15
+ Parameters:
16
+ - alpha - trainable parameter
17
+ References:
18
+ - This activation function is from this paper by Liu Ziyin, Tilman Hartwig, Masahito Ueda:
19
+ https://arxiv.org/abs/2006.08195
20
+ Examples:
21
+ >>> a1 = snake(256)
22
+ >>> x = torch.randn(256)
23
+ >>> x = a1(x)
24
+ '''
25
+
26
+ def __init__(self, in_features, alpha=1.0, alpha_trainable=True, alpha_logscale=False):
27
+ '''
28
+ Initialization.
29
+ INPUT:
30
+ - in_features: shape of the input
31
+ - alpha: trainable parameter
32
+ alpha is initialized to 1 by default, higher values = higher-frequency.
33
+ alpha will be trained along with the rest of your model.
34
+ '''
35
+ super(Snake, self).__init__()
36
+ self.in_features = in_features
37
+
38
+ # initialize alpha
39
+ self.alpha_logscale = alpha_logscale
40
+ if self.alpha_logscale: # log scale alphas initialized to zeros
41
+ self.alpha = Parameter(torch.zeros(in_features) * alpha)
42
+ else: # linear scale alphas initialized to ones
43
+ self.alpha = Parameter(torch.ones(in_features) * alpha)
44
+
45
+ self.alpha.requires_grad = alpha_trainable
46
+
47
+ self.no_div_by_zero = 0.000000001
48
+
49
+ def forward(self, x):
50
+ '''
51
+ Forward pass of the function.
52
+ Applies the function to the input elementwise.
53
+ Snake ∶= x + 1/a * sin^2 (xa)
54
+ '''
55
+ alpha = self.alpha.unsqueeze(0).unsqueeze(-1) # line up with x to [B, C, T]
56
+ if self.alpha_logscale:
57
+ alpha = torch.exp(alpha)
58
+ x = x + (1.0 / (alpha + self.no_div_by_zero)) * pow(sin(x * alpha), 2)
59
+
60
+ return x
61
+
62
+
63
+ class SnakeBeta(nn.Module):
64
+ '''
65
+ A modified Snake function which uses separate parameters for the magnitude of the periodic components
66
+ Shape:
67
+ - Input: (B, C, T)
68
+ - Output: (B, C, T), same shape as the input
69
+ Parameters:
70
+ - alpha - trainable parameter that controls frequency
71
+ - beta - trainable parameter that controls magnitude
72
+ References:
73
+ - This activation function is a modified version based on this paper by Liu Ziyin, Tilman Hartwig, Masahito Ueda:
74
+ https://arxiv.org/abs/2006.08195
75
+ Examples:
76
+ >>> a1 = snakebeta(256)
77
+ >>> x = torch.randn(256)
78
+ >>> x = a1(x)
79
+ '''
80
+
81
+ def __init__(self, in_features, alpha=1.0, alpha_trainable=True, alpha_logscale=False):
82
+ '''
83
+ Initialization.
84
+ INPUT:
85
+ - in_features: shape of the input
86
+ - alpha - trainable parameter that controls frequency
87
+ - beta - trainable parameter that controls magnitude
88
+ alpha is initialized to 1 by default, higher values = higher-frequency.
89
+ beta is initialized to 1 by default, higher values = higher-magnitude.
90
+ alpha will be trained along with the rest of your model.
91
+ '''
92
+ super(SnakeBeta, self).__init__()
93
+ self.in_features = in_features
94
+
95
+ # initialize alpha
96
+ self.alpha_logscale = alpha_logscale
97
+ if self.alpha_logscale: # log scale alphas initialized to zeros
98
+ self.alpha = Parameter(torch.zeros(in_features) * alpha)
99
+ self.beta = Parameter(torch.zeros(in_features) * alpha)
100
+ else: # linear scale alphas initialized to ones
101
+ self.alpha = Parameter(torch.ones(in_features) * alpha)
102
+ self.beta = Parameter(torch.ones(in_features) * alpha)
103
+
104
+ self.alpha.requires_grad = alpha_trainable
105
+ self.beta.requires_grad = alpha_trainable
106
+
107
+ self.no_div_by_zero = 0.000000001
108
+
109
+ def forward(self, x):
110
+ '''
111
+ Forward pass of the function.
112
+ Applies the function to the input elementwise.
113
+ SnakeBeta ∶= x + 1/b * sin^2 (xa)
114
+ '''
115
+ alpha = self.alpha.unsqueeze(0).unsqueeze(-1) # line up with x to [B, C, T]
116
+ beta = self.beta.unsqueeze(0).unsqueeze(-1)
117
+ if self.alpha_logscale:
118
+ alpha = torch.exp(alpha)
119
+ beta = torch.exp(beta)
120
+ x = x + (1.0 / (beta + self.no_div_by_zero)) * pow(sin(x * alpha), 2)
121
+
122
+ return x
indextts/BigVGAN/alias_free_activation/__init__.py ADDED
File without changes
indextts/BigVGAN/alias_free_activation/cuda/.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ /build
indextts/BigVGAN/alias_free_activation/cuda/__init__.py ADDED
File without changes
indextts/BigVGAN/alias_free_activation/cuda/activation1d.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2024 NVIDIA CORPORATION.
2
+ # Licensed under the MIT license.
3
+
4
+ import torch
5
+ import torch.nn as nn
6
+ # load fused CUDA kernel: this enables importing anti_alias_activation_cuda
7
+ from indextts.BigVGAN.alias_free_activation.cuda import load
8
+ from indextts.BigVGAN.alias_free_activation.torch.resample import DownSample1d, UpSample1d
9
+
10
+ anti_alias_activation_cuda = load.load()
11
+
12
+
13
+ class FusedAntiAliasActivation(torch.autograd.Function):
14
+ """
15
+ Assumes filter size 12, replication padding on upsampling/downsampling, and logscale alpha/beta parameters as inputs.
16
+ The hyperparameters are hard-coded in the kernel to maximize speed.
17
+ NOTE: The fused kenrel is incorrect for Activation1d with different hyperparameters.
18
+ """
19
+
20
+ @staticmethod
21
+ def forward(ctx, inputs, up_ftr, down_ftr, alpha, beta):
22
+ activation_results = anti_alias_activation_cuda.forward(
23
+ inputs, up_ftr, down_ftr, alpha, beta
24
+ )
25
+
26
+ return activation_results
27
+
28
+ @staticmethod
29
+ def backward(ctx, output_grads):
30
+ raise NotImplementedError
31
+ return output_grads, None, None
32
+
33
+
34
+ class Activation1d(nn.Module):
35
+ def __init__(
36
+ self,
37
+ activation,
38
+ up_ratio: int = 2,
39
+ down_ratio: int = 2,
40
+ up_kernel_size: int = 12,
41
+ down_kernel_size: int = 12,
42
+ fused: bool = True,
43
+ ):
44
+ super().__init__()
45
+ self.up_ratio = up_ratio
46
+ self.down_ratio = down_ratio
47
+ self.act = activation
48
+ self.upsample = UpSample1d(up_ratio, up_kernel_size)
49
+ self.downsample = DownSample1d(down_ratio, down_kernel_size)
50
+
51
+ self.fused = fused # Whether to use fused CUDA kernel or not
52
+
53
+ def forward(self, x):
54
+ if not self.fused:
55
+ x = self.upsample(x)
56
+ x = self.act(x)
57
+ x = self.downsample(x)
58
+ return x
59
+ else:
60
+ if self.act.__class__.__name__ == "Snake":
61
+ beta = self.act.alpha.data # Snake uses same params for alpha and beta
62
+ else:
63
+ beta = (
64
+ self.act.beta.data
65
+ ) # Snakebeta uses different params for alpha and beta
66
+ alpha = self.act.alpha.data
67
+ if (
68
+ not self.act.alpha_logscale
69
+ ): # Exp baked into cuda kernel, cancel it out with a log
70
+ alpha = torch.log(alpha)
71
+ beta = torch.log(beta)
72
+
73
+ x = FusedAntiAliasActivation.apply(
74
+ x, self.upsample.filter, self.downsample.lowpass.filter, alpha, beta
75
+ )
76
+ return x
indextts/BigVGAN/alias_free_activation/cuda/anti_alias_activation.cpp ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* coding=utf-8
2
+ * Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ #include <torch/extension.h>
18
+
19
+ extern "C" torch::Tensor fwd_cuda(torch::Tensor const &input, torch::Tensor const &up_filter, torch::Tensor const &down_filter, torch::Tensor const &alpha, torch::Tensor const &beta);
20
+
21
+ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
22
+ m.def("forward", &fwd_cuda, "Anti-Alias Activation forward (CUDA)");
23
+ }
indextts/BigVGAN/alias_free_activation/cuda/anti_alias_activation_cuda.cu ADDED
@@ -0,0 +1,256 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* coding=utf-8
2
+ * Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ #include <ATen/ATen.h>
18
+ #include <cuda.h>
19
+ #include <cuda_runtime.h>
20
+ #include <cuda_fp16.h>
21
+ #include <cuda_profiler_api.h>
22
+ #include <ATen/cuda/CUDAContext.h>
23
+ #include <torch/extension.h>
24
+ #include "type_shim.h"
25
+ #include <assert.h>
26
+ #include <cfloat>
27
+ #include <limits>
28
+ #include <stdint.h>
29
+ #include <c10/macros/Macros.h>
30
+
31
+ namespace
32
+ {
33
+ // Hard-coded hyperparameters
34
+ // WARP_SIZE and WARP_BATCH must match the return values batches_per_warp and
35
+ constexpr int ELEMENTS_PER_LDG_STG = 1; //(WARP_ITERATIONS < 4) ? 1 : 4;
36
+ constexpr int BUFFER_SIZE = 32;
37
+ constexpr int FILTER_SIZE = 12;
38
+ constexpr int HALF_FILTER_SIZE = 6;
39
+ constexpr int UPSAMPLE_REPLICATION_PAD = 5; // 5 on each side, matching torch impl
40
+ constexpr int DOWNSAMPLE_REPLICATION_PAD_LEFT = 5; // matching torch impl
41
+ constexpr int DOWNSAMPLE_REPLICATION_PAD_RIGHT = 6; // matching torch impl
42
+
43
+ template <typename input_t, typename output_t, typename acc_t>
44
+ __global__ void anti_alias_activation_forward(
45
+ output_t *dst,
46
+ const input_t *src,
47
+ const acc_t *up_ftr,
48
+ const acc_t *down_ftr,
49
+ const acc_t *alpha,
50
+ const acc_t *beta,
51
+ int batch_size,
52
+ int channels,
53
+ int seq_len)
54
+ {
55
+ // Up and downsample filters
56
+ input_t up_filter[FILTER_SIZE];
57
+ input_t down_filter[FILTER_SIZE];
58
+
59
+ // Load data from global memory including extra indices reserved for replication paddings
60
+ input_t elements[2 * FILTER_SIZE + 2 * BUFFER_SIZE + 2 * UPSAMPLE_REPLICATION_PAD] = {0};
61
+ input_t intermediates[2 * FILTER_SIZE + 2 * BUFFER_SIZE + DOWNSAMPLE_REPLICATION_PAD_LEFT + DOWNSAMPLE_REPLICATION_PAD_RIGHT] = {0};
62
+
63
+ // Output stores downsampled output before writing to dst
64
+ output_t output[BUFFER_SIZE];
65
+
66
+ // blockDim/threadIdx = (128, 1, 1)
67
+ // gridDim/blockIdx = (seq_blocks, channels, batches)
68
+ int block_offset = (blockIdx.x * 128 * BUFFER_SIZE + seq_len * (blockIdx.y + gridDim.y * blockIdx.z));
69
+ int local_offset = threadIdx.x * BUFFER_SIZE;
70
+ int seq_offset = blockIdx.x * 128 * BUFFER_SIZE + local_offset;
71
+
72
+ // intermediate have double the seq_len
73
+ int intermediate_local_offset = threadIdx.x * BUFFER_SIZE * 2;
74
+ int intermediate_seq_offset = blockIdx.x * 128 * BUFFER_SIZE * 2 + intermediate_local_offset;
75
+
76
+ // Get values needed for replication padding before moving pointer
77
+ const input_t *right_most_pntr = src + (seq_len * (blockIdx.y + gridDim.y * blockIdx.z));
78
+ input_t seq_left_most_value = right_most_pntr[0];
79
+ input_t seq_right_most_value = right_most_pntr[seq_len - 1];
80
+
81
+ // Move src and dst pointers
82
+ src += block_offset + local_offset;
83
+ dst += block_offset + local_offset;
84
+
85
+ // Alpha and beta values for snake activatons. Applies exp by default
86
+ alpha = alpha + blockIdx.y;
87
+ beta = beta + blockIdx.y;
88
+
89
+ acc_t alpha_val = expf(alpha[0]);
90
+ acc_t beta_val = expf(beta[0]);
91
+
92
+ #pragma unroll
93
+ for (int it = 0; it < FILTER_SIZE; it += 1)
94
+ {
95
+ up_filter[it] = up_ftr[it];
96
+ down_filter[it] = down_ftr[it];
97
+ }
98
+
99
+ // Apply replication padding for upsampling, matching torch impl
100
+ #pragma unroll
101
+ for (int it = -HALF_FILTER_SIZE; it < BUFFER_SIZE + HALF_FILTER_SIZE; it += 1)
102
+ {
103
+ int element_index = seq_offset + it; // index for element
104
+ if ((element_index < 0) && (element_index >= -UPSAMPLE_REPLICATION_PAD))
105
+ {
106
+ elements[2 * (HALF_FILTER_SIZE + it)] = 2 * seq_left_most_value;
107
+ }
108
+ if ((element_index >= seq_len) && (element_index < seq_len + UPSAMPLE_REPLICATION_PAD))
109
+ {
110
+ elements[2 * (HALF_FILTER_SIZE + it)] = 2 * seq_right_most_value;
111
+ }
112
+ if ((element_index >= 0) && (element_index < seq_len))
113
+ {
114
+ elements[2 * (HALF_FILTER_SIZE + it)] = 2 * src[it];
115
+ }
116
+ }
117
+
118
+ // Apply upsampling strided convolution and write to intermediates. It reserves DOWNSAMPLE_REPLICATION_PAD_LEFT for replication padding of the downsampilng conv later
119
+ #pragma unroll
120
+ for (int it = 0; it < (2 * BUFFER_SIZE + 2 * FILTER_SIZE); it += 1)
121
+ {
122
+ acc_t acc = 0.0;
123
+ int element_index = intermediate_seq_offset + it; // index for intermediate
124
+ #pragma unroll
125
+ for (int f_idx = 0; f_idx < FILTER_SIZE; f_idx += 1)
126
+ {
127
+ if ((element_index + f_idx) >= 0)
128
+ {
129
+ acc += up_filter[f_idx] * elements[it + f_idx];
130
+ }
131
+ }
132
+ intermediates[it + DOWNSAMPLE_REPLICATION_PAD_LEFT] = acc;
133
+ }
134
+
135
+ // Apply activation function. It reserves DOWNSAMPLE_REPLICATION_PAD_LEFT and DOWNSAMPLE_REPLICATION_PAD_RIGHT for replication padding of the downsampilng conv later
136
+ double no_div_by_zero = 0.000000001;
137
+ #pragma unroll
138
+ for (int it = 0; it < 2 * BUFFER_SIZE + 2 * FILTER_SIZE; it += 1)
139
+ {
140
+ acc_t a = sinf(intermediates[it + DOWNSAMPLE_REPLICATION_PAD_LEFT] * alpha_val);
141
+ intermediates[it + DOWNSAMPLE_REPLICATION_PAD_LEFT] += (1.0 / (beta_val + no_div_by_zero)) * a * a;
142
+ }
143
+
144
+ // Apply replication padding before downsampling conv from intermediates
145
+ #pragma unroll
146
+ for (int it = 0; it < DOWNSAMPLE_REPLICATION_PAD_LEFT; it += 1)
147
+ {
148
+ intermediates[it] = intermediates[DOWNSAMPLE_REPLICATION_PAD_LEFT];
149
+ }
150
+ #pragma unroll
151
+ for (int it = DOWNSAMPLE_REPLICATION_PAD_LEFT + 2 * BUFFER_SIZE + 2 * FILTER_SIZE; it < DOWNSAMPLE_REPLICATION_PAD_LEFT + 2 * BUFFER_SIZE + 2 * FILTER_SIZE + DOWNSAMPLE_REPLICATION_PAD_RIGHT; it += 1)
152
+ {
153
+ intermediates[it] = intermediates[DOWNSAMPLE_REPLICATION_PAD_LEFT + 2 * BUFFER_SIZE + 2 * FILTER_SIZE - 1];
154
+ }
155
+
156
+ // Apply downsample strided convolution (assuming stride=2) from intermediates
157
+ #pragma unroll
158
+ for (int it = 0; it < BUFFER_SIZE; it += 1)
159
+ {
160
+ acc_t acc = 0.0;
161
+ #pragma unroll
162
+ for (int f_idx = 0; f_idx < FILTER_SIZE; f_idx += 1)
163
+ {
164
+ // Add constant DOWNSAMPLE_REPLICATION_PAD_RIGHT to match torch implementation
165
+ acc += down_filter[f_idx] * intermediates[it * 2 + f_idx + DOWNSAMPLE_REPLICATION_PAD_RIGHT];
166
+ }
167
+ output[it] = acc;
168
+ }
169
+
170
+ // Write output to dst
171
+ #pragma unroll
172
+ for (int it = 0; it < BUFFER_SIZE; it += ELEMENTS_PER_LDG_STG)
173
+ {
174
+ int element_index = seq_offset + it;
175
+ if (element_index < seq_len)
176
+ {
177
+ dst[it] = output[it];
178
+ }
179
+ }
180
+
181
+ }
182
+
183
+ template <typename input_t, typename output_t, typename acc_t>
184
+ void dispatch_anti_alias_activation_forward(
185
+ output_t *dst,
186
+ const input_t *src,
187
+ const acc_t *up_ftr,
188
+ const acc_t *down_ftr,
189
+ const acc_t *alpha,
190
+ const acc_t *beta,
191
+ int batch_size,
192
+ int channels,
193
+ int seq_len)
194
+ {
195
+ if (seq_len == 0)
196
+ {
197
+ return;
198
+ }
199
+ else
200
+ {
201
+ // Use 128 threads per block to maximimize gpu utilization
202
+ constexpr int threads_per_block = 128;
203
+ constexpr int seq_len_per_block = 4096;
204
+ int blocks_per_seq_len = (seq_len + seq_len_per_block - 1) / seq_len_per_block;
205
+ dim3 blocks(blocks_per_seq_len, channels, batch_size);
206
+ dim3 threads(threads_per_block, 1, 1);
207
+
208
+ anti_alias_activation_forward<input_t, output_t, acc_t>
209
+ <<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(dst, src, up_ftr, down_ftr, alpha, beta, batch_size, channels, seq_len);
210
+ }
211
+ }
212
+ }
213
+
214
+ extern "C" torch::Tensor fwd_cuda(torch::Tensor const &input, torch::Tensor const &up_filter, torch::Tensor const &down_filter, torch::Tensor const &alpha, torch::Tensor const &beta)
215
+ {
216
+ // Input is a 3d tensor with dimensions [batches, channels, seq_len]
217
+ const int batches = input.size(0);
218
+ const int channels = input.size(1);
219
+ const int seq_len = input.size(2);
220
+
221
+ // Output
222
+ auto act_options = input.options().requires_grad(false);
223
+
224
+ torch::Tensor anti_alias_activation_results =
225
+ torch::empty({batches, channels, seq_len}, act_options);
226
+
227
+ using float32 = float;
228
+ // The dtype of input is float16, bfloat16, or float32
229
+ // The dtype of up_filter, down_filter, alpha, and beta is float32
230
+ // printf("input scalar type: %d\n", input.scalar_type());
231
+ // printf("up_filter scalar type: %d\n", up_filter.scalar_type());
232
+ // printf("down_filter scalar type: %d\n", down_filter.scalar_type());
233
+ // printf("alpha scalar type: %d\n", alpha.scalar_type());
234
+ // printf("beta scalar type: %d\n", beta.scalar_type());
235
+ void *input_ptr = static_cast<void *>(input.data_ptr());
236
+ float32 *up_filter_ptr = static_cast<float32 *>(up_filter.data_ptr());
237
+ float32 *down_filter_ptr = static_cast<float32 *>(down_filter.data_ptr());
238
+ float32 *alpha_ptr = static_cast<float32 *>(alpha.data_ptr());
239
+ float32 *beta_ptr = static_cast<float32 *>(beta.data_ptr());
240
+ void *anti_alias_activation_results_ptr = static_cast<void *>(anti_alias_activation_results.data_ptr());
241
+
242
+ DISPATCH_FLOAT_HALF_AND_BFLOAT(
243
+ input.scalar_type(),
244
+ "dispatch anti alias activation_forward",
245
+ dispatch_anti_alias_activation_forward<scalar_t, scalar_t, float32>(
246
+ reinterpret_cast<scalar_t *>(anti_alias_activation_results_ptr),
247
+ reinterpret_cast<const scalar_t *>(input_ptr),
248
+ reinterpret_cast<const float32 *>(up_filter_ptr),
249
+ reinterpret_cast<const float32 *>(down_filter_ptr),
250
+ reinterpret_cast<const float32 *>(alpha_ptr),
251
+ reinterpret_cast<const float32 *>(beta_ptr),
252
+ batches,
253
+ channels,
254
+ seq_len););
255
+ return anti_alias_activation_results;
256
+ }
indextts/BigVGAN/alias_free_activation/cuda/compat.h ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* coding=utf-8
2
+ * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ /*This code is copied fron NVIDIA apex:
18
+ * https://github.com/NVIDIA/apex
19
+ * with minor changes. */
20
+
21
+ #ifndef TORCH_CHECK
22
+ #define TORCH_CHECK AT_CHECK
23
+ #endif
24
+
25
+ #ifdef VERSION_GE_1_3
26
+ #define DATA_PTR data_ptr
27
+ #else
28
+ #define DATA_PTR data
29
+ #endif
indextts/BigVGAN/alias_free_activation/cuda/load.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2024 NVIDIA CORPORATION.
2
+ # Licensed under the MIT license.
3
+
4
+ import os
5
+ import pathlib
6
+ import subprocess
7
+
8
+ from torch.utils import cpp_extension
9
+
10
+ """
11
+ Setting this param to a list has a problem of generating different compilation commands (with diferent order of architectures) and leading to recompilation of fused kernels.
12
+ Set it to empty stringo avoid recompilation and assign arch flags explicity in extra_cuda_cflags below
13
+ """
14
+ os.environ["TORCH_CUDA_ARCH_LIST"] = ""
15
+
16
+
17
+ import re
18
+ import shutil
19
+ import tempfile
20
+
21
+ # 补丁修复:sources 路径含中文字符时,生成 build.ninja 乱码导致编译失败
22
+ # 使用临时目录来规避 ninja 编译失败(比如中文路径)
23
+ def chinese_path_compile_support(sources, buildpath):
24
+ pattern = re.compile(r'[\u4e00-\u9fff]')
25
+ if not bool(pattern.search(str(sources[0].resolve()))):
26
+ return buildpath # 检测非中文路径跳过
27
+ # Create build directory
28
+ resolves = [ item.name for item in sources]
29
+ ninja_compile_dir = os.path.join(tempfile.gettempdir(), "BigVGAN", "cuda")
30
+ os.makedirs(ninja_compile_dir, exist_ok=True)
31
+ new_buildpath = os.path.join(ninja_compile_dir, "build")
32
+ os.makedirs(new_buildpath, exist_ok=True)
33
+ print(f"ninja_buildpath: {new_buildpath}")
34
+ # Copy files to directory
35
+ sources.clear()
36
+ current_dir = os.path.dirname(__file__)
37
+ ALLOWED_EXTENSIONS = {'.py', '.cu', '.cpp', '.h'}
38
+ for filename in os.listdir(current_dir):
39
+ item = pathlib.Path(current_dir).joinpath(filename)
40
+ tar_path = pathlib.Path(ninja_compile_dir).joinpath(item.name)
41
+ if not item.suffix.lower() in ALLOWED_EXTENSIONS:continue
42
+ pathlib.Path(shutil.copy2(item, tar_path))
43
+ if tar_path.name in resolves:sources.append(tar_path)
44
+ return new_buildpath
45
+
46
+
47
+
48
+ def load():
49
+ # Check if cuda 11 is installed for compute capability 8.0
50
+ cc_flag = []
51
+ _, bare_metal_major, _ = _get_cuda_bare_metal_version(cpp_extension.CUDA_HOME)
52
+ if int(bare_metal_major) >= 11:
53
+ cc_flag.append("-gencode")
54
+ cc_flag.append("arch=compute_80,code=sm_80")
55
+
56
+ # Build path
57
+ srcpath = pathlib.Path(__file__).parent.absolute()
58
+ buildpath = srcpath / "build"
59
+ _create_build_dir(buildpath)
60
+
61
+ # Helper function to build the kernels.
62
+ def _cpp_extention_load_helper(name, sources, extra_cuda_flags):
63
+ return cpp_extension.load(
64
+ name=name,
65
+ sources=sources,
66
+ build_directory=buildpath,
67
+ extra_cflags=[
68
+ "-O3",
69
+ ],
70
+ extra_cuda_cflags=[
71
+ "-O3",
72
+ "-gencode",
73
+ "arch=compute_70,code=sm_70",
74
+ "--use_fast_math",
75
+ ]
76
+ + extra_cuda_flags
77
+ + cc_flag,
78
+ verbose=True,
79
+ )
80
+
81
+ extra_cuda_flags = [
82
+ "-U__CUDA_NO_HALF_OPERATORS__",
83
+ "-U__CUDA_NO_HALF_CONVERSIONS__",
84
+ "--expt-relaxed-constexpr",
85
+ "--expt-extended-lambda",
86
+ ]
87
+
88
+ sources = [
89
+ srcpath / "anti_alias_activation.cpp",
90
+ srcpath / "anti_alias_activation_cuda.cu",
91
+ ]
92
+
93
+ # 兼容方案:ninja 特殊字符路径编译支持处理(比如中文路径)
94
+ buildpath = chinese_path_compile_support(sources, buildpath)
95
+
96
+ anti_alias_activation_cuda = _cpp_extention_load_helper(
97
+ "anti_alias_activation_cuda", sources, extra_cuda_flags
98
+ )
99
+
100
+ return anti_alias_activation_cuda
101
+
102
+
103
+ def _get_cuda_bare_metal_version(cuda_dir):
104
+ raw_output = subprocess.check_output(
105
+ [cuda_dir + "/bin/nvcc", "-V"], universal_newlines=True
106
+ )
107
+ output = raw_output.split()
108
+ release_idx = output.index("release") + 1
109
+ release = output[release_idx].split(".")
110
+ bare_metal_major = release[0]
111
+ bare_metal_minor = release[1][0]
112
+
113
+ return raw_output, bare_metal_major, bare_metal_minor
114
+
115
+
116
+ def _create_build_dir(buildpath):
117
+ try:
118
+ os.mkdir(buildpath)
119
+ except OSError:
120
+ if not os.path.isdir(buildpath):
121
+ print(f"Creation of the build directory {buildpath} failed")
indextts/BigVGAN/alias_free_activation/cuda/type_shim.h ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* coding=utf-8
2
+ * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ #include <ATen/ATen.h>
18
+ #include "compat.h"
19
+
20
+ #define DISPATCH_FLOAT_HALF_AND_BFLOAT(TYPE, NAME, ...) \
21
+ switch (TYPE) \
22
+ { \
23
+ case at::ScalarType::Float: \
24
+ { \
25
+ using scalar_t = float; \
26
+ __VA_ARGS__; \
27
+ break; \
28
+ } \
29
+ case at::ScalarType::Half: \
30
+ { \
31
+ using scalar_t = at::Half; \
32
+ __VA_ARGS__; \
33
+ break; \
34
+ } \
35
+ case at::ScalarType::BFloat16: \
36
+ { \
37
+ using scalar_t = at::BFloat16; \
38
+ __VA_ARGS__; \
39
+ break; \
40
+ } \
41
+ default: \
42
+ AT_ERROR(#NAME, " not implemented for '", toString(TYPE), "'"); \
43
+ }
44
+
45
+ #define DISPATCH_FLOAT_HALF_AND_BFLOAT_INOUT_TYPES(TYPEIN, TYPEOUT, NAME, ...) \
46
+ switch (TYPEIN) \
47
+ { \
48
+ case at::ScalarType::Float: \
49
+ { \
50
+ using scalar_t_in = float; \
51
+ switch (TYPEOUT) \
52
+ { \
53
+ case at::ScalarType::Float: \
54
+ { \
55
+ using scalar_t_out = float; \
56
+ __VA_ARGS__; \
57
+ break; \
58
+ } \
59
+ case at::ScalarType::Half: \
60
+ { \
61
+ using scalar_t_out = at::Half; \
62
+ __VA_ARGS__; \
63
+ break; \
64
+ } \
65
+ case at::ScalarType::BFloat16: \
66
+ { \
67
+ using scalar_t_out = at::BFloat16; \
68
+ __VA_ARGS__; \
69
+ break; \
70
+ } \
71
+ default: \
72
+ AT_ERROR(#NAME, " not implemented for '", toString(TYPEOUT), "'"); \
73
+ } \
74
+ break; \
75
+ } \
76
+ case at::ScalarType::Half: \
77
+ { \
78
+ using scalar_t_in = at::Half; \
79
+ using scalar_t_out = at::Half; \
80
+ __VA_ARGS__; \
81
+ break; \
82
+ } \
83
+ case at::ScalarType::BFloat16: \
84
+ { \
85
+ using scalar_t_in = at::BFloat16; \
86
+ using scalar_t_out = at::BFloat16; \
87
+ __VA_ARGS__; \
88
+ break; \
89
+ } \
90
+ default: \
91
+ AT_ERROR(#NAME, " not implemented for '", toString(TYPEIN), "'"); \
92
+ }
indextts/BigVGAN/alias_free_activation/torch/__init__.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ # Adapted from https://github.com/junjun3518/alias-free-torch under the Apache License 2.0
2
+ # LICENSE is in incl_licenses directory.
3
+
4
+ from .act import *
5
+ from .filter import *
6
+ from .resample import *
indextts/BigVGAN/alias_free_activation/torch/act.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Adapted from https://github.com/junjun3518/alias-free-torch under the Apache License 2.0
2
+ # LICENSE is in incl_licenses directory.
3
+
4
+ import torch.nn as nn
5
+
6
+ from .resample import DownSample1d, UpSample1d
7
+
8
+
9
+ class Activation1d(nn.Module):
10
+ def __init__(
11
+ self,
12
+ activation,
13
+ up_ratio: int = 2,
14
+ down_ratio: int = 2,
15
+ up_kernel_size: int = 12,
16
+ down_kernel_size: int = 12,
17
+ ):
18
+ super().__init__()
19
+ self.up_ratio = up_ratio
20
+ self.down_ratio = down_ratio
21
+ self.act = activation
22
+ self.upsample = UpSample1d(up_ratio, up_kernel_size)
23
+ self.downsample = DownSample1d(down_ratio, down_kernel_size)
24
+
25
+ # x: [B,C,T]
26
+ def forward(self, x):
27
+ x = self.upsample(x)
28
+ x = self.act(x)
29
+ x = self.downsample(x)
30
+
31
+ return x
indextts/BigVGAN/alias_free_activation/torch/filter.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Adapted from https://github.com/junjun3518/alias-free-torch under the Apache License 2.0
2
+ # LICENSE is in incl_licenses directory.
3
+
4
+ import math
5
+
6
+ import torch
7
+ import torch.nn as nn
8
+ import torch.nn.functional as F
9
+
10
+ if "sinc" in dir(torch):
11
+ sinc = torch.sinc
12
+ else:
13
+ # This code is adopted from adefossez's julius.core.sinc under the MIT License
14
+ # https://adefossez.github.io/julius/julius/core.html
15
+ # LICENSE is in incl_licenses directory.
16
+ def sinc(x: torch.Tensor):
17
+ """
18
+ Implementation of sinc, i.e. sin(pi * x) / (pi * x)
19
+ __Warning__: Different to julius.sinc, the input is multiplied by `pi`!
20
+ """
21
+ return torch.where(
22
+ x == 0,
23
+ torch.tensor(1.0, device=x.device, dtype=x.dtype),
24
+ torch.sin(math.pi * x) / math.pi / x,
25
+ )
26
+
27
+
28
+ # This code is adopted from adefossez's julius.lowpass.LowPassFilters under the MIT License
29
+ # https://adefossez.github.io/julius/julius/lowpass.html
30
+ # LICENSE is in incl_licenses directory.
31
+ def kaiser_sinc_filter1d(
32
+ cutoff, half_width, kernel_size
33
+ ): # return filter [1,1,kernel_size]
34
+ even = kernel_size % 2 == 0
35
+ half_size = kernel_size // 2
36
+
37
+ # For kaiser window
38
+ delta_f = 4 * half_width
39
+ A = 2.285 * (half_size - 1) * math.pi * delta_f + 7.95
40
+ if A > 50.0:
41
+ beta = 0.1102 * (A - 8.7)
42
+ elif A >= 21.0:
43
+ beta = 0.5842 * (A - 21) ** 0.4 + 0.07886 * (A - 21.0)
44
+ else:
45
+ beta = 0.0
46
+ window = torch.kaiser_window(kernel_size, beta=beta, periodic=False)
47
+
48
+ # ratio = 0.5/cutoff -> 2 * cutoff = 1 / ratio
49
+ if even:
50
+ time = torch.arange(-half_size, half_size) + 0.5
51
+ else:
52
+ time = torch.arange(kernel_size) - half_size
53
+ if cutoff == 0:
54
+ filter_ = torch.zeros_like(time)
55
+ else:
56
+ filter_ = 2 * cutoff * window * sinc(2 * cutoff * time)
57
+ """
58
+ Normalize filter to have sum = 1, otherwise we will have a small leakage of the constant component in the input signal.
59
+ """
60
+ filter_ /= filter_.sum()
61
+ filter = filter_.view(1, 1, kernel_size)
62
+
63
+ return filter
64
+
65
+
66
+ class LowPassFilter1d(nn.Module):
67
+ def __init__(
68
+ self,
69
+ cutoff=0.5,
70
+ half_width=0.6,
71
+ stride: int = 1,
72
+ padding: bool = True,
73
+ padding_mode: str = "replicate",
74
+ kernel_size: int = 12,
75
+ ):
76
+ """
77
+ kernel_size should be even number for stylegan3 setup, in this implementation, odd number is also possible.
78
+ """
79
+ super().__init__()
80
+ if cutoff < -0.0:
81
+ raise ValueError("Minimum cutoff must be larger than zero.")
82
+ if cutoff > 0.5:
83
+ raise ValueError("A cutoff above 0.5 does not make sense.")
84
+ self.kernel_size = kernel_size
85
+ self.even = kernel_size % 2 == 0
86
+ self.pad_left = kernel_size // 2 - int(self.even)
87
+ self.pad_right = kernel_size // 2
88
+ self.stride = stride
89
+ self.padding = padding
90
+ self.padding_mode = padding_mode
91
+ filter = kaiser_sinc_filter1d(cutoff, half_width, kernel_size)
92
+ self.register_buffer("filter", filter)
93
+
94
+ # Input [B, C, T]
95
+ def forward(self, x):
96
+ _, C, _ = x.shape
97
+
98
+ if self.padding:
99
+ x = F.pad(x, (self.pad_left, self.pad_right), mode=self.padding_mode)
100
+ out = F.conv1d(x, self.filter.expand(C, -1, -1), stride=self.stride, groups=C)
101
+
102
+ return out
indextts/BigVGAN/alias_free_activation/torch/resample.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Adapted from https://github.com/junjun3518/alias-free-torch under the Apache License 2.0
2
+ # LICENSE is in incl_licenses directory.
3
+
4
+ import torch.nn as nn
5
+ from torch.nn import functional as F
6
+
7
+ from .filter import LowPassFilter1d, kaiser_sinc_filter1d
8
+
9
+
10
+ class UpSample1d(nn.Module):
11
+ def __init__(self, ratio=2, kernel_size=None):
12
+ super().__init__()
13
+ self.ratio = ratio
14
+ self.kernel_size = (
15
+ int(6 * ratio // 2) * 2 if kernel_size is None else kernel_size
16
+ )
17
+ self.stride = ratio
18
+ self.pad = self.kernel_size // ratio - 1
19
+ self.pad_left = self.pad * self.stride + (self.kernel_size - self.stride) // 2
20
+ self.pad_right = (
21
+ self.pad * self.stride + (self.kernel_size - self.stride + 1) // 2
22
+ )
23
+ filter = kaiser_sinc_filter1d(
24
+ cutoff=0.5 / ratio, half_width=0.6 / ratio, kernel_size=self.kernel_size
25
+ )
26
+ self.register_buffer("filter", filter)
27
+
28
+ # x: [B, C, T]
29
+ def forward(self, x):
30
+ _, C, _ = x.shape
31
+
32
+ x = F.pad(x, (self.pad, self.pad), mode="replicate")
33
+ x = self.ratio * F.conv_transpose1d(
34
+ x, self.filter.expand(C, -1, -1), stride=self.stride, groups=C
35
+ )
36
+ x = x[..., self.pad_left : -self.pad_right]
37
+
38
+ return x
39
+
40
+
41
+ class DownSample1d(nn.Module):
42
+ def __init__(self, ratio=2, kernel_size=None):
43
+ super().__init__()
44
+ self.ratio = ratio
45
+ self.kernel_size = (
46
+ int(6 * ratio // 2) * 2 if kernel_size is None else kernel_size
47
+ )
48
+ self.lowpass = LowPassFilter1d(
49
+ cutoff=0.5 / ratio,
50
+ half_width=0.6 / ratio,
51
+ stride=ratio,
52
+ kernel_size=self.kernel_size,
53
+ )
54
+
55
+ def forward(self, x):
56
+ xx = self.lowpass(x)
57
+
58
+ return xx
indextts/BigVGAN/alias_free_torch/__init__.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ # Adapted from https://github.com/junjun3518/alias-free-torch under the Apache License 2.0
2
+ # LICENSE is in incl_licenses directory.
3
+
4
+ from .act import *
5
+ from .filter import *
6
+ from .resample import *
indextts/BigVGAN/alias_free_torch/act.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Adapted from https://github.com/junjun3518/alias-free-torch under the Apache License 2.0
2
+ # LICENSE is in incl_licenses directory.
3
+
4
+ import torch.nn as nn
5
+
6
+ from .resample import DownSample1d, UpSample1d
7
+
8
+
9
+ class Activation1d(nn.Module):
10
+ def __init__(self,
11
+ activation,
12
+ up_ratio: int = 2,
13
+ down_ratio: int = 2,
14
+ up_kernel_size: int = 12,
15
+ down_kernel_size: int = 12):
16
+ super().__init__()
17
+ self.up_ratio = up_ratio
18
+ self.down_ratio = down_ratio
19
+ self.act = activation
20
+ self.upsample = UpSample1d(up_ratio, up_kernel_size)
21
+ self.downsample = DownSample1d(down_ratio, down_kernel_size)
22
+
23
+ # x: [B,C,T]
24
+ def forward(self, x):
25
+ x = self.upsample(x)
26
+ x = self.act(x)
27
+ x = self.downsample(x)
28
+
29
+ return x
indextts/BigVGAN/alias_free_torch/filter.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Adapted from https://github.com/junjun3518/alias-free-torch under the Apache License 2.0
2
+ # LICENSE is in incl_licenses directory.
3
+
4
+ import math
5
+
6
+ import torch
7
+ import torch.nn as nn
8
+ import torch.nn.functional as F
9
+
10
+ if 'sinc' in dir(torch):
11
+ sinc = torch.sinc
12
+ else:
13
+ # This code is adopted from adefossez's julius.core.sinc under the MIT License
14
+ # https://adefossez.github.io/julius/julius/core.html
15
+ # LICENSE is in incl_licenses directory.
16
+ def sinc(x: torch.Tensor):
17
+ """
18
+ Implementation of sinc, i.e. sin(pi * x) / (pi * x)
19
+ __Warning__: Different to julius.sinc, the input is multiplied by `pi`!
20
+ """
21
+ return torch.where(x == 0,
22
+ torch.tensor(1., device=x.device, dtype=x.dtype),
23
+ torch.sin(math.pi * x) / math.pi / x)
24
+
25
+
26
+ # This code is adopted from adefossez's julius.lowpass.LowPassFilters under the MIT License
27
+ # https://adefossez.github.io/julius/julius/lowpass.html
28
+ # LICENSE is in incl_licenses directory.
29
+ def kaiser_sinc_filter1d(cutoff, half_width, kernel_size): # return filter [1,1,kernel_size]
30
+ even = (kernel_size % 2 == 0)
31
+ half_size = kernel_size // 2
32
+
33
+ #For kaiser window
34
+ delta_f = 4 * half_width
35
+ A = 2.285 * (half_size - 1) * math.pi * delta_f + 7.95
36
+ if A > 50.:
37
+ beta = 0.1102 * (A - 8.7)
38
+ elif A >= 21.:
39
+ beta = 0.5842 * (A - 21)**0.4 + 0.07886 * (A - 21.)
40
+ else:
41
+ beta = 0.
42
+ window = torch.kaiser_window(kernel_size, beta=beta, periodic=False)
43
+
44
+ # ratio = 0.5/cutoff -> 2 * cutoff = 1 / ratio
45
+ if even:
46
+ time = (torch.arange(-half_size, half_size) + 0.5)
47
+ else:
48
+ time = torch.arange(kernel_size) - half_size
49
+ if cutoff == 0:
50
+ filter_ = torch.zeros_like(time)
51
+ else:
52
+ filter_ = 2 * cutoff * window * sinc(2 * cutoff * time)
53
+ # Normalize filter to have sum = 1, otherwise we will have a small leakage
54
+ # of the constant component in the input signal.
55
+ filter_ /= filter_.sum()
56
+ filter = filter_.view(1, 1, kernel_size)
57
+
58
+ return filter
59
+
60
+
61
+ class LowPassFilter1d(nn.Module):
62
+ def __init__(self,
63
+ cutoff=0.5,
64
+ half_width=0.6,
65
+ stride: int = 1,
66
+ padding: bool = True,
67
+ padding_mode: str = 'replicate',
68
+ kernel_size: int = 12):
69
+ # kernel_size should be even number for stylegan3 setup,
70
+ # in this implementation, odd number is also possible.
71
+ super().__init__()
72
+ if cutoff < -0.:
73
+ raise ValueError("Minimum cutoff must be larger than zero.")
74
+ if cutoff > 0.5:
75
+ raise ValueError("A cutoff above 0.5 does not make sense.")
76
+ self.kernel_size = kernel_size
77
+ self.even = (kernel_size % 2 == 0)
78
+ self.pad_left = kernel_size // 2 - int(self.even)
79
+ self.pad_right = kernel_size // 2
80
+ self.stride = stride
81
+ self.padding = padding
82
+ self.padding_mode = padding_mode
83
+ filter = kaiser_sinc_filter1d(cutoff, half_width, kernel_size)
84
+ self.register_buffer("filter", filter)
85
+
86
+ #input [B, C, T]
87
+ def forward(self, x):
88
+ _, C, _ = x.shape
89
+
90
+ if self.padding:
91
+ x = F.pad(x, (self.pad_left, self.pad_right),
92
+ mode=self.padding_mode)
93
+ out = F.conv1d(x, self.filter.expand(C, -1, -1),
94
+ stride=self.stride, groups=C)
95
+
96
+ return out
indextts/BigVGAN/alias_free_torch/resample.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Adapted from https://github.com/junjun3518/alias-free-torch under the Apache License 2.0
2
+ # LICENSE is in incl_licenses directory.
3
+
4
+ import torch.nn as nn
5
+ from torch.nn import functional as F
6
+
7
+ from .filter import LowPassFilter1d, kaiser_sinc_filter1d
8
+
9
+
10
+ class UpSample1d(nn.Module):
11
+ def __init__(self, ratio=2, kernel_size=None):
12
+ super().__init__()
13
+ self.ratio = ratio
14
+ self.kernel_size = int(6 * ratio // 2) * 2 if kernel_size is None else kernel_size
15
+ self.stride = ratio
16
+ self.pad = self.kernel_size // ratio - 1
17
+ self.pad_left = self.pad * self.stride + (self.kernel_size - self.stride) // 2
18
+ self.pad_right = self.pad * self.stride + (self.kernel_size - self.stride + 1) // 2
19
+ filter = kaiser_sinc_filter1d(cutoff=0.5 / ratio,
20
+ half_width=0.6 / ratio,
21
+ kernel_size=self.kernel_size)
22
+ self.register_buffer("filter", filter)
23
+
24
+ # x: [B, C, T]
25
+ def forward(self, x):
26
+ _, C, _ = x.shape
27
+
28
+ x = F.pad(x, (self.pad, self.pad), mode='replicate')
29
+ x = self.ratio * F.conv_transpose1d(
30
+ x, self.filter.expand(C, -1, -1), stride=self.stride, groups=C)
31
+ x = x[..., self.pad_left:-self.pad_right]
32
+
33
+ return x
34
+
35
+
36
+ class DownSample1d(nn.Module):
37
+ def __init__(self, ratio=2, kernel_size=None):
38
+ super().__init__()
39
+ self.ratio = ratio
40
+ self.kernel_size = int(6 * ratio // 2) * 2 if kernel_size is None else kernel_size
41
+ self.lowpass = LowPassFilter1d(cutoff=0.5 / ratio,
42
+ half_width=0.6 / ratio,
43
+ stride=ratio,
44
+ kernel_size=self.kernel_size)
45
+
46
+ def forward(self, x):
47
+ xx = self.lowpass(x)
48
+
49
+ return xx
indextts/BigVGAN/bigvgan.py ADDED
@@ -0,0 +1,534 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2024 NVIDIA CORPORATION.
2
+ # Licensed under the MIT license.
3
+
4
+ # Adapted from https://github.com/jik876/hifi-gan under the MIT license.
5
+ # LICENSE is in incl_licenses directory.
6
+
7
+ import json
8
+ import os
9
+ from pathlib import Path
10
+ from typing import Dict, Optional, Union
11
+
12
+ import torch
13
+ import torch.nn as nn
14
+ from huggingface_hub import PyTorchModelHubMixin, hf_hub_download
15
+ from torch.nn import Conv1d, ConvTranspose1d
16
+ from torch.nn.utils import remove_weight_norm, weight_norm
17
+
18
+ import indextts.BigVGAN.activations as activations
19
+ from indextts.BigVGAN.alias_free_activation.torch.act import \
20
+ Activation1d as TorchActivation1d
21
+ from indextts.BigVGAN.ECAPA_TDNN import ECAPA_TDNN
22
+ from indextts.BigVGAN.env import AttrDict
23
+ from indextts.BigVGAN.utils import get_padding, init_weights
24
+
25
+
26
+ def load_hparams_from_json(path) -> AttrDict:
27
+ with open(path) as f:
28
+ data = f.read()
29
+ return AttrDict(json.loads(data))
30
+
31
+
32
+ class AMPBlock1(torch.nn.Module):
33
+ """
34
+ AMPBlock applies Snake / SnakeBeta activation functions with trainable parameters that control periodicity, defined for each layer.
35
+ AMPBlock1 has additional self.convs2 that contains additional Conv1d layers with a fixed dilation=1 followed by each layer in self.convs1
36
+
37
+ Args:
38
+ h (AttrDict): Hyperparameters.
39
+ channels (int): Number of convolution channels.
40
+ kernel_size (int): Size of the convolution kernel. Default is 3.
41
+ dilation (tuple): Dilation rates for the convolutions. Each dilation layer has two convolutions. Default is (1, 3, 5).
42
+ activation (str): Activation function type. Should be either 'snake' or 'snakebeta'. Default is None.
43
+ """
44
+
45
+ def __init__(
46
+ self,
47
+ h: AttrDict,
48
+ channels: int,
49
+ kernel_size: int = 3,
50
+ dilation: tuple = (1, 3, 5),
51
+ activation: str = None,
52
+ ):
53
+ super().__init__()
54
+
55
+ self.h = h
56
+
57
+ self.convs1 = nn.ModuleList(
58
+ [
59
+ weight_norm(
60
+ Conv1d(
61
+ channels,
62
+ channels,
63
+ kernel_size,
64
+ stride=1,
65
+ dilation=d,
66
+ padding=get_padding(kernel_size, d),
67
+ )
68
+ )
69
+ for d in dilation
70
+ ]
71
+ )
72
+ self.convs1.apply(init_weights)
73
+
74
+ self.convs2 = nn.ModuleList(
75
+ [
76
+ weight_norm(
77
+ Conv1d(
78
+ channels,
79
+ channels,
80
+ kernel_size,
81
+ stride=1,
82
+ dilation=1,
83
+ padding=get_padding(kernel_size, 1),
84
+ )
85
+ )
86
+ for _ in range(len(dilation))
87
+ ]
88
+ )
89
+ self.convs2.apply(init_weights)
90
+
91
+ self.num_layers = len(self.convs1) + len(
92
+ self.convs2
93
+ ) # Total number of conv layers
94
+
95
+ # Select which Activation1d, lazy-load cuda version to ensure backward compatibility
96
+ if self.h.get("use_cuda_kernel", False):
97
+ from alias_free_activation.cuda.activation1d import \
98
+ Activation1d as CudaActivation1d
99
+
100
+ Activation1d = CudaActivation1d
101
+ else:
102
+ Activation1d = TorchActivation1d
103
+
104
+ # Activation functions
105
+ if activation == "snake":
106
+ self.activations = nn.ModuleList(
107
+ [
108
+ Activation1d(
109
+ activation=activations.Snake(
110
+ channels, alpha_logscale=h.snake_logscale
111
+ )
112
+ )
113
+ for _ in range(self.num_layers)
114
+ ]
115
+ )
116
+ elif activation == "snakebeta":
117
+ self.activations = nn.ModuleList(
118
+ [
119
+ Activation1d(
120
+ activation=activations.SnakeBeta(
121
+ channels, alpha_logscale=h.snake_logscale
122
+ )
123
+ )
124
+ for _ in range(self.num_layers)
125
+ ]
126
+ )
127
+ else:
128
+ raise NotImplementedError(
129
+ "activation incorrectly specified. check the config file and look for 'activation'."
130
+ )
131
+
132
+ def forward(self, x):
133
+ acts1, acts2 = self.activations[::2], self.activations[1::2]
134
+ for c1, c2, a1, a2 in zip(self.convs1, self.convs2, acts1, acts2):
135
+ xt = a1(x)
136
+ xt = c1(xt)
137
+ xt = a2(xt)
138
+ xt = c2(xt)
139
+ x = xt + x
140
+
141
+ return x
142
+
143
+ def remove_weight_norm(self):
144
+ for l in self.convs1:
145
+ remove_weight_norm(l)
146
+ for l in self.convs2:
147
+ remove_weight_norm(l)
148
+
149
+
150
+ class AMPBlock2(torch.nn.Module):
151
+ """
152
+ AMPBlock applies Snake / SnakeBeta activation functions with trainable parameters that control periodicity, defined for each layer.
153
+ Unlike AMPBlock1, AMPBlock2 does not contain extra Conv1d layers with fixed dilation=1
154
+
155
+ Args:
156
+ h (AttrDict): Hyperparameters.
157
+ channels (int): Number of convolution channels.
158
+ kernel_size (int): Size of the convolution kernel. Default is 3.
159
+ dilation (tuple): Dilation rates for the convolutions. Each dilation layer has two convolutions. Default is (1, 3, 5).
160
+ activation (str): Activation function type. Should be either 'snake' or 'snakebeta'. Default is None.
161
+ """
162
+
163
+ def __init__(
164
+ self,
165
+ h: AttrDict,
166
+ channels: int,
167
+ kernel_size: int = 3,
168
+ dilation: tuple = (1, 3, 5),
169
+ activation: str = None,
170
+ ):
171
+ super().__init__()
172
+
173
+ self.h = h
174
+
175
+ self.convs = nn.ModuleList(
176
+ [
177
+ weight_norm(
178
+ Conv1d(
179
+ channels,
180
+ channels,
181
+ kernel_size,
182
+ stride=1,
183
+ dilation=d,
184
+ padding=get_padding(kernel_size, d),
185
+ )
186
+ )
187
+ for d in dilation
188
+ ]
189
+ )
190
+ self.convs.apply(init_weights)
191
+
192
+ self.num_layers = len(self.convs) # Total number of conv layers
193
+
194
+ # Select which Activation1d, lazy-load cuda version to ensure backward compatibility
195
+ if self.h.get("use_cuda_kernel", False):
196
+ from alias_free_activation.cuda.activation1d import \
197
+ Activation1d as CudaActivation1d
198
+
199
+ Activation1d = CudaActivation1d
200
+ else:
201
+ Activation1d = TorchActivation1d
202
+
203
+ # Activation functions
204
+ if activation == "snake":
205
+ self.activations = nn.ModuleList(
206
+ [
207
+ Activation1d(
208
+ activation=activations.Snake(
209
+ channels, alpha_logscale=h.snake_logscale
210
+ )
211
+ )
212
+ for _ in range(self.num_layers)
213
+ ]
214
+ )
215
+ elif activation == "snakebeta":
216
+ self.activations = nn.ModuleList(
217
+ [
218
+ Activation1d(
219
+ activation=activations.SnakeBeta(
220
+ channels, alpha_logscale=h.snake_logscale
221
+ )
222
+ )
223
+ for _ in range(self.num_layers)
224
+ ]
225
+ )
226
+ else:
227
+ raise NotImplementedError(
228
+ "activation incorrectly specified. check the config file and look for 'activation'."
229
+ )
230
+
231
+ def forward(self, x):
232
+ for c, a in zip(self.convs, self.activations):
233
+ xt = a(x)
234
+ xt = c(xt)
235
+ x = xt + x
236
+ return x
237
+
238
+ def remove_weight_norm(self):
239
+ for l in self.convs:
240
+ remove_weight_norm(l)
241
+
242
+
243
+ '''
244
+ PyTorchModelHubMixin,
245
+ library_name="bigvgan",
246
+ repo_url="https://github.com/NVIDIA/BigVGAN",
247
+ docs_url="https://github.com/NVIDIA/BigVGAN/blob/main/README.md",
248
+ pipeline_tag="audio-to-audio",
249
+ license="mit",
250
+ tags=["neural-vocoder", "audio-generation", "arxiv:2206.04658"],
251
+ '''
252
+
253
+
254
+ class BigVGAN(
255
+ torch.nn.Module,
256
+ ):
257
+ """
258
+ BigVGAN is a neural vocoder model that applies anti-aliased periodic activation for residual blocks (resblocks).
259
+ New in BigVGAN-v2: it can optionally use optimized CUDA kernels for AMP (anti-aliased multi-periodicity) blocks.
260
+
261
+ Args:
262
+ h (AttrDict): Hyperparameters.
263
+ use_cuda_kernel (bool): If set to True, loads optimized CUDA kernels for AMP. This should be used for inference only, as training is not supported with CUDA kernels.
264
+
265
+ Note:
266
+ - The `use_cuda_kernel` parameter should be used for inference only, as training with CUDA kernels is not supported.
267
+ - Ensure that the activation function is correctly specified in the hyperparameters (h.activation).
268
+ """
269
+
270
+ def __init__(self, h: AttrDict, use_cuda_kernel: bool = False):
271
+ super().__init__()
272
+ self.h = h
273
+ self.h["use_cuda_kernel"] = use_cuda_kernel
274
+
275
+ # Select which Activation1d, lazy-load cuda version to ensure backward compatibility
276
+ if self.h.get("use_cuda_kernel", False):
277
+ from alias_free_activation.cuda.activation1d import \
278
+ Activation1d as CudaActivation1d
279
+
280
+ Activation1d = CudaActivation1d
281
+ else:
282
+ Activation1d = TorchActivation1d
283
+
284
+ self.num_kernels = len(h.resblock_kernel_sizes)
285
+ self.num_upsamples = len(h.upsample_rates)
286
+
287
+ self.feat_upsample = h.feat_upsample
288
+ self.cond_in_each_up_layer = h.cond_d_vector_in_each_upsampling_layer
289
+
290
+ # Pre-conv
291
+ self.conv_pre = weight_norm(
292
+ Conv1d(h.gpt_dim, h.upsample_initial_channel, 7, 1, padding=3)
293
+ )
294
+
295
+ # Define which AMPBlock to use. BigVGAN uses AMPBlock1 as default
296
+ if h.resblock == "1":
297
+ resblock_class = AMPBlock1
298
+ elif h.resblock == "2":
299
+ resblock_class = AMPBlock2
300
+ else:
301
+ raise ValueError(
302
+ f"Incorrect resblock class specified in hyperparameters. Got {h.resblock}"
303
+ )
304
+
305
+ # Transposed conv-based upsamplers. does not apply anti-aliasing
306
+ self.ups = nn.ModuleList()
307
+ for i, (u, k) in enumerate(zip(h.upsample_rates, h.upsample_kernel_sizes)):
308
+ self.ups.append(
309
+ nn.ModuleList(
310
+ [
311
+ weight_norm(
312
+ ConvTranspose1d(
313
+ h.upsample_initial_channel // (2**i),
314
+ h.upsample_initial_channel // (2 ** (i + 1)),
315
+ k,
316
+ u,
317
+ padding=(k - u) // 2,
318
+ )
319
+ )
320
+ ]
321
+ )
322
+ )
323
+
324
+ # Residual blocks using anti-aliased multi-periodicity composition modules (AMP)
325
+ self.resblocks = nn.ModuleList()
326
+ for i in range(len(self.ups)):
327
+ ch = h.upsample_initial_channel // (2 ** (i + 1))
328
+ for j, (k, d) in enumerate(
329
+ zip(h.resblock_kernel_sizes, h.resblock_dilation_sizes)
330
+ ):
331
+ self.resblocks.append(
332
+ resblock_class(h, ch, k, d, activation=h.activation)
333
+ )
334
+
335
+ # Post-conv
336
+ activation_post = (
337
+ activations.Snake(ch, alpha_logscale=h.snake_logscale)
338
+ if h.activation == "snake"
339
+ else (
340
+ activations.SnakeBeta(ch, alpha_logscale=h.snake_logscale)
341
+ if h.activation == "snakebeta"
342
+ else None
343
+ )
344
+ )
345
+ if activation_post is None:
346
+ raise NotImplementedError(
347
+ "activation incorrectly specified. check the config file and look for 'activation'."
348
+ )
349
+
350
+ self.activation_post = Activation1d(activation=activation_post)
351
+
352
+ # Whether to use bias for the final conv_post. Default to True for backward compatibility
353
+ self.use_bias_at_final = h.get("use_bias_at_final", True)
354
+ self.conv_post = weight_norm(
355
+ Conv1d(ch, 1, 7, 1, padding=3, bias=self.use_bias_at_final)
356
+ )
357
+
358
+ # Weight initialization
359
+ for i in range(len(self.ups)):
360
+ self.ups[i].apply(init_weights)
361
+ self.conv_post.apply(init_weights)
362
+
363
+ # Final tanh activation. Defaults to True for backward compatibility
364
+ self.use_tanh_at_final = h.get("use_tanh_at_final", True)
365
+
366
+ self.speaker_encoder = ECAPA_TDNN(h.num_mels, lin_neurons=h.speaker_embedding_dim)
367
+ self.cond_layer = nn.Conv1d(h.speaker_embedding_dim, h.upsample_initial_channel, 1)
368
+ if self.cond_in_each_up_layer:
369
+ self.conds = nn.ModuleList()
370
+ for i in range(len(self.ups)):
371
+ ch = h.upsample_initial_channel // (2 ** (i + 1))
372
+ self.conds.append(nn.Conv1d(h.speaker_embedding_dim, ch, 1))
373
+
374
+ def forward(self, x, mel_refer, lens=None):
375
+ # Speaker reference
376
+ speaker_embedding = self.speaker_encoder(mel_refer, lens)
377
+ n_batch = x.size(0)
378
+ contrastive_loss = None
379
+ if n_batch * 2 == speaker_embedding.size(0):
380
+ spe_emb_chunk1, spe_emb_chunk2 = speaker_embedding[:n_batch, :, :], speaker_embedding[n_batch:, :, :]
381
+ contrastive_loss = self.cal_clip_loss(spe_emb_chunk1.squeeze(1), spe_emb_chunk2.squeeze(1),
382
+ self.logit_scale.exp())
383
+
384
+ speaker_embedding = speaker_embedding[:n_batch, :, :]
385
+ speaker_embedding = speaker_embedding.transpose(1, 2)
386
+
387
+ # upsample feat
388
+ if self.feat_upsample:
389
+ x = torch.nn.functional.interpolate(
390
+ x.transpose(1, 2),
391
+ scale_factor=[4],
392
+ mode="linear",
393
+ ).squeeze(1)
394
+ else:
395
+ x = x.transpose(1, 2)
396
+
397
+ # BigVGAN
398
+ # Pre-conv
399
+ x = self.conv_pre(x)
400
+ x = x + self.cond_layer(speaker_embedding)
401
+
402
+ for i in range(self.num_upsamples):
403
+ # Upsampling
404
+ for i_up in range(len(self.ups[i])):
405
+ x = self.ups[i][i_up](x)
406
+
407
+ if self.cond_in_each_up_layer:
408
+ x = x + self.conds[i](speaker_embedding)
409
+
410
+ # AMP blocks
411
+ xs = None
412
+ for j in range(self.num_kernels):
413
+ if xs is None:
414
+ xs = self.resblocks[i * self.num_kernels + j](x)
415
+ else:
416
+ xs += self.resblocks[i * self.num_kernels + j](x)
417
+ x = xs / self.num_kernels
418
+
419
+ # Post-conv
420
+ x = self.activation_post(x)
421
+ x = self.conv_post(x)
422
+ # Final tanh activation
423
+ if self.use_tanh_at_final:
424
+ x = torch.tanh(x)
425
+ else:
426
+ x = torch.clamp(x, min=-1.0, max=1.0) # Bound the output to [-1, 1]
427
+
428
+ return x, contrastive_loss
429
+
430
+ def remove_weight_norm(self):
431
+ try:
432
+ print("Removing weight norm...")
433
+ for l in self.ups:
434
+ for l_i in l:
435
+ remove_weight_norm(l_i)
436
+ for l in self.resblocks:
437
+ l.remove_weight_norm()
438
+ remove_weight_norm(self.conv_pre)
439
+ remove_weight_norm(self.conv_post)
440
+ except ValueError:
441
+ print("[INFO] Model already removed weight norm. Skipping!")
442
+ pass
443
+
444
+ # Additional methods for huggingface_hub support
445
+ def _save_pretrained(self, save_directory: Path) -> None:
446
+ """Save weights and config.json from a Pytorch model to a local directory."""
447
+
448
+ model_path = save_directory / "bigvgan_generator.pt"
449
+ torch.save({"generator": self.state_dict()}, model_path)
450
+
451
+ config_path = save_directory / "config.json"
452
+ with open(config_path, "w") as config_file:
453
+ json.dump(self.h, config_file, indent=4)
454
+
455
+ @classmethod
456
+ def _from_pretrained(
457
+ cls,
458
+ *,
459
+ model_id: str,
460
+ revision: str,
461
+ cache_dir: str,
462
+ force_download: bool,
463
+ proxies: Optional[Dict],
464
+ resume_download: bool,
465
+ local_files_only: bool,
466
+ token: Union[str, bool, None],
467
+ map_location: str = "cpu", # Additional argument
468
+ strict: bool = False, # Additional argument
469
+ use_cuda_kernel: bool = False,
470
+ **model_kwargs,
471
+ ):
472
+ """Load Pytorch pretrained weights and return the loaded model."""
473
+
474
+ # Download and load hyperparameters (h) used by BigVGAN
475
+ if os.path.isdir(model_id):
476
+ print("Loading config.json from local directory")
477
+ config_file = os.path.join(model_id, "config.json")
478
+ else:
479
+ config_file = hf_hub_download(
480
+ repo_id=model_id,
481
+ filename="config.json",
482
+ revision=revision,
483
+ cache_dir=cache_dir,
484
+ force_download=force_download,
485
+ proxies=proxies,
486
+ resume_download=resume_download,
487
+ token=token,
488
+ local_files_only=local_files_only,
489
+ )
490
+ h = load_hparams_from_json(config_file)
491
+
492
+ # instantiate BigVGAN using h
493
+ if use_cuda_kernel:
494
+ print(
495
+ f"[WARNING] You have specified use_cuda_kernel=True during BigVGAN.from_pretrained(). Only inference is supported (training is not implemented)!"
496
+ )
497
+ print(
498
+ f"[WARNING] You need nvcc and ninja installed in your system that matches your PyTorch build is using to build the kernel. If not, the model will fail to initialize or generate incorrect waveform!"
499
+ )
500
+ print(
501
+ f"[WARNING] For detail, see the official GitHub repository: https://github.com/NVIDIA/BigVGAN?tab=readme-ov-file#using-custom-cuda-kernel-for-synthesis"
502
+ )
503
+ model = cls(h, use_cuda_kernel=use_cuda_kernel)
504
+
505
+ # Download and load pretrained generator weight
506
+ if os.path.isdir(model_id):
507
+ print("Loading weights from local directory")
508
+ model_file = os.path.join(model_id, "bigvgan_generator.pt")
509
+ else:
510
+ print(f"Loading weights from {model_id}")
511
+ model_file = hf_hub_download(
512
+ repo_id=model_id,
513
+ filename="bigvgan_generator.pt",
514
+ revision=revision,
515
+ cache_dir=cache_dir,
516
+ force_download=force_download,
517
+ proxies=proxies,
518
+ resume_download=resume_download,
519
+ token=token,
520
+ local_files_only=local_files_only,
521
+ )
522
+
523
+ checkpoint_dict = torch.load(model_file, map_location=map_location)
524
+
525
+ try:
526
+ model.load_state_dict(checkpoint_dict["generator"])
527
+ except RuntimeError:
528
+ print(
529
+ f"[INFO] the pretrained checkpoint does not contain weight norm. Loading the checkpoint after removing weight norm!"
530
+ )
531
+ model.remove_weight_norm()
532
+ model.load_state_dict(checkpoint_dict["generator"])
533
+
534
+ return model
indextts/BigVGAN/models.py ADDED
@@ -0,0 +1,451 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2022 NVIDIA CORPORATION.
2
+ # Licensed under the MIT license.
3
+
4
+ # Adapted from https://github.com/jik876/hifi-gan under the MIT license.
5
+ # LICENSE is in incl_licenses directory.
6
+ import torch
7
+ import torch.nn as nn
8
+ import torch.nn.functional as F
9
+ from torch.nn import Conv1d, Conv2d, ConvTranspose1d
10
+ from torch.nn.utils import remove_weight_norm, spectral_norm, weight_norm
11
+
12
+ import indextts.BigVGAN.activations as activations
13
+
14
+ from indextts.BigVGAN.ECAPA_TDNN import ECAPA_TDNN
15
+ from indextts.BigVGAN.utils import get_padding, init_weights
16
+
17
+ LRELU_SLOPE = 0.1
18
+
19
+
20
+ class AMPBlock1(torch.nn.Module):
21
+ def __init__(self, h, channels, kernel_size=3, dilation=(1, 3, 5), activation=None):
22
+ super(AMPBlock1, self).__init__()
23
+ self.h = h
24
+
25
+ self.convs1 = nn.ModuleList([
26
+ weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0],
27
+ padding=get_padding(kernel_size, dilation[0]))),
28
+ weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1],
29
+ padding=get_padding(kernel_size, dilation[1]))),
30
+ weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[2],
31
+ padding=get_padding(kernel_size, dilation[2])))
32
+ ])
33
+ self.convs1.apply(init_weights)
34
+
35
+ self.convs2 = nn.ModuleList([
36
+ weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
37
+ padding=get_padding(kernel_size, 1))),
38
+ weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
39
+ padding=get_padding(kernel_size, 1))),
40
+ weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1,
41
+ padding=get_padding(kernel_size, 1)))
42
+ ])
43
+ self.convs2.apply(init_weights)
44
+
45
+ self.num_layers = len(self.convs1) + len(self.convs2) # total number of conv layers
46
+ if self.h.get("use_cuda_kernel", False):
47
+ from indextts.BigVGAN.alias_free_activation.cuda.activation1d import Activation1d
48
+ else:
49
+ from indextts.BigVGAN.alias_free_torch import Activation1d
50
+ if activation == 'snake': # periodic nonlinearity with snake function and anti-aliasing
51
+ self.activations = nn.ModuleList([
52
+ Activation1d(
53
+ activation=activations.Snake(channels, alpha_logscale=h.snake_logscale))
54
+ for _ in range(self.num_layers)
55
+ ])
56
+ elif activation == 'snakebeta': # periodic nonlinearity with snakebeta function and anti-aliasing
57
+ self.activations = nn.ModuleList([
58
+ Activation1d(
59
+ activation=activations.SnakeBeta(channels, alpha_logscale=h.snake_logscale))
60
+ for _ in range(self.num_layers)
61
+ ])
62
+ else:
63
+ raise NotImplementedError("activation incorrectly specified. check the config file and look for 'activation'.")
64
+
65
+ def forward(self, x):
66
+ acts1, acts2 = self.activations[::2], self.activations[1::2]
67
+ for c1, c2, a1, a2 in zip(self.convs1, self.convs2, acts1, acts2):
68
+ xt = a1(x)
69
+ xt = c1(xt)
70
+ xt = a2(xt)
71
+ xt = c2(xt)
72
+ x = xt + x
73
+
74
+ return x
75
+
76
+ def remove_weight_norm(self):
77
+ for l in self.convs1:
78
+ remove_weight_norm(l)
79
+ for l in self.convs2:
80
+ remove_weight_norm(l)
81
+
82
+
83
+ class AMPBlock2(torch.nn.Module):
84
+ def __init__(self, h, channels, kernel_size=3, dilation=(1, 3), activation=None):
85
+ super(AMPBlock2, self).__init__()
86
+ self.h = h
87
+
88
+ self.convs = nn.ModuleList([
89
+ weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0],
90
+ padding=get_padding(kernel_size, dilation[0]))),
91
+ weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1],
92
+ padding=get_padding(kernel_size, dilation[1])))
93
+ ])
94
+ self.convs.apply(init_weights)
95
+
96
+ self.num_layers = len(self.convs) # total number of conv layers
97
+ if self.h.get("use_cuda_kernel", False):
98
+ from indextts.BigVGAN.alias_free_activation.cuda.activation1d import Activation1d
99
+ else:
100
+ from indextts.BigVGAN.alias_free_torch import Activation1d
101
+
102
+ if activation == 'snake': # periodic nonlinearity with snake function and anti-aliasing
103
+ self.activations = nn.ModuleList([
104
+ Activation1d(
105
+ activation=activations.Snake(channels, alpha_logscale=h.snake_logscale))
106
+ for _ in range(self.num_layers)
107
+ ])
108
+ elif activation == 'snakebeta': # periodic nonlinearity with snakebeta function and anti-aliasing
109
+ self.activations = nn.ModuleList([
110
+ Activation1d(
111
+ activation=activations.SnakeBeta(channels, alpha_logscale=h.snake_logscale))
112
+ for _ in range(self.num_layers)
113
+ ])
114
+ else:
115
+ raise NotImplementedError("activation incorrectly specified. check the config file and look for 'activation'.")
116
+
117
+ def forward(self, x):
118
+ for c, a in zip(self.convs, self.activations):
119
+ xt = a(x)
120
+ xt = c(xt)
121
+ x = xt + x
122
+
123
+ return x
124
+
125
+ def remove_weight_norm(self):
126
+ for l in self.convs:
127
+ remove_weight_norm(l)
128
+
129
+
130
+ class BigVGAN(torch.nn.Module):
131
+ # this is our main BigVGAN model. Applies anti-aliased periodic activation for resblocks.
132
+ def __init__(self, h, use_cuda_kernel=False):
133
+ """
134
+ Args:
135
+ h (dict)
136
+ use_cuda_kernel (bool): whether to use custom cuda kernel for anti-aliased activation
137
+ """
138
+ super(BigVGAN, self).__init__()
139
+ self.h = h
140
+ self.h["use_cuda_kernel"] = use_cuda_kernel
141
+
142
+ self.num_kernels = len(h.resblock_kernel_sizes)
143
+ self.num_upsamples = len(h.upsample_rates)
144
+
145
+ self.feat_upsample = h.feat_upsample
146
+ self.cond_in_each_up_layer = h.cond_d_vector_in_each_upsampling_layer
147
+
148
+ # pre conv
149
+ self.conv_pre = weight_norm(Conv1d(h.gpt_dim, h.upsample_initial_channel, 7, 1, padding=3))
150
+
151
+ # define which AMPBlock to use. BigVGAN uses AMPBlock1 as default
152
+ resblock = AMPBlock1 if h.resblock == "1" else AMPBlock2
153
+
154
+ # transposed conv-based upsamplers. does not apply anti-aliasing
155
+ self.ups = nn.ModuleList()
156
+ for i, (u, k) in enumerate(zip(h.upsample_rates, h.upsample_kernel_sizes)):
157
+ self.ups.append(nn.ModuleList([
158
+ weight_norm(ConvTranspose1d(h.upsample_initial_channel // (2 ** i),
159
+ h.upsample_initial_channel // (2 ** (i + 1)),
160
+ k, u, padding=(k - u) // 2))
161
+ ]))
162
+
163
+ # residual blocks using anti-aliased multi-periodicity composition modules (AMP)
164
+ self.resblocks = nn.ModuleList()
165
+ for i in range(len(self.ups)):
166
+ ch = h.upsample_initial_channel // (2 ** (i + 1))
167
+ for j, (k, d) in enumerate(zip(h.resblock_kernel_sizes, h.resblock_dilation_sizes)):
168
+ self.resblocks.append(resblock(self.h, ch, k, d, activation=h.activation))
169
+ if use_cuda_kernel:
170
+ from indextts.BigVGAN.alias_free_activation.cuda.activation1d import Activation1d
171
+ else:
172
+ from indextts.BigVGAN.alias_free_torch import Activation1d
173
+
174
+ # post conv
175
+ if h.activation == "snake": # periodic nonlinearity with snake function and anti-aliasing
176
+ activation_post = activations.Snake(ch, alpha_logscale=h.snake_logscale)
177
+ self.activation_post = Activation1d(activation=activation_post)
178
+ elif h.activation == "snakebeta": # periodic nonlinearity with snakebeta function and anti-aliasing
179
+ activation_post = activations.SnakeBeta(ch, alpha_logscale=h.snake_logscale)
180
+ self.activation_post = Activation1d(activation=activation_post)
181
+ else:
182
+ raise NotImplementedError("activation incorrectly specified. check the config file and look for 'activation'.")
183
+
184
+ self.conv_post = weight_norm(Conv1d(ch, 1, 7, 1, padding=3))
185
+
186
+ # weight initialization
187
+ for i in range(len(self.ups)):
188
+ self.ups[i].apply(init_weights)
189
+ self.conv_post.apply(init_weights)
190
+
191
+ self.speaker_encoder = ECAPA_TDNN(h.num_mels, lin_neurons=h.speaker_embedding_dim)
192
+ self.cond_layer = nn.Conv1d(h.speaker_embedding_dim, h.upsample_initial_channel, 1)
193
+ if self.cond_in_each_up_layer:
194
+ self.conds = nn.ModuleList()
195
+ for i in range(len(self.ups)):
196
+ ch = h.upsample_initial_channel // (2 ** (i + 1))
197
+ self.conds.append(nn.Conv1d(h.speaker_embedding_dim, ch, 1))
198
+
199
+ # self.logit_scale = nn.Parameter(torch.ones([]) * np.log(1 / 0.07))
200
+
201
+ def forward(self, x, mel_ref, lens=None):
202
+ speaker_embedding = self.speaker_encoder(mel_ref, lens)
203
+ n_batch = x.size(0)
204
+ contrastive_loss = None
205
+ if n_batch * 2 == speaker_embedding.size(0):
206
+ spe_emb_chunk1, spe_emb_chunk2 = speaker_embedding[:n_batch, :, :], speaker_embedding[n_batch:, :, :]
207
+ contrastive_loss = self.cal_clip_loss(spe_emb_chunk1.squeeze(1), spe_emb_chunk2.squeeze(1), self.logit_scale.exp())
208
+
209
+ speaker_embedding = speaker_embedding[:n_batch, :, :]
210
+ speaker_embedding = speaker_embedding.transpose(1, 2)
211
+
212
+ # upsample feat
213
+ if self.feat_upsample:
214
+ x = torch.nn.functional.interpolate(
215
+ x.transpose(1, 2),
216
+ scale_factor=[4],
217
+ mode="linear",
218
+ ).squeeze(1)
219
+ else:
220
+ x = x.transpose(1, 2)
221
+
222
+ ### bigVGAN ###
223
+ # pre conv
224
+ x = self.conv_pre(x)
225
+
226
+ x = x + self.cond_layer(speaker_embedding)
227
+
228
+ for i in range(self.num_upsamples):
229
+ # upsampling
230
+ for i_up in range(len(self.ups[i])):
231
+ x = self.ups[i][i_up](x)
232
+
233
+ if self.cond_in_each_up_layer:
234
+ x = x + self.conds[i](speaker_embedding)
235
+
236
+ # AMP blocks
237
+ xs = None
238
+ for j in range(self.num_kernels):
239
+ if xs is None:
240
+ xs = self.resblocks[i * self.num_kernels + j](x)
241
+ else:
242
+ xs += self.resblocks[i * self.num_kernels + j](x)
243
+ x = xs / self.num_kernels
244
+
245
+ # post conv
246
+ x = self.activation_post(x)
247
+ x = self.conv_post(x)
248
+ x = torch.tanh(x)
249
+
250
+ return x, contrastive_loss
251
+
252
+ def remove_weight_norm(self):
253
+ print('Removing weight norm...')
254
+ for l in self.ups:
255
+ for l_i in l:
256
+ remove_weight_norm(l_i)
257
+ for l in self.resblocks:
258
+ l.remove_weight_norm()
259
+ remove_weight_norm(self.conv_pre)
260
+ remove_weight_norm(self.conv_post)
261
+
262
+ def cal_clip_loss(self, image_features, text_features, logit_scale):
263
+ device = image_features.device
264
+ logits_per_image, logits_per_text = self.get_logits(image_features, text_features, logit_scale)
265
+ labels = torch.arange(logits_per_image.shape[0], device=device, dtype=torch.long)
266
+ total_loss = (
267
+ F.cross_entropy(logits_per_image, labels) +
268
+ F.cross_entropy(logits_per_text, labels)
269
+ ) / 2
270
+ return total_loss
271
+
272
+ def get_logits(self, image_features, text_features, logit_scale):
273
+ logits_per_image = logit_scale * image_features @ text_features.T
274
+ logits_per_text = logit_scale * text_features @ image_features.T
275
+ return logits_per_image, logits_per_text
276
+
277
+
278
+ class DiscriminatorP(torch.nn.Module):
279
+ def __init__(self, h, period, kernel_size=5, stride=3, use_spectral_norm=False):
280
+ super(DiscriminatorP, self).__init__()
281
+ self.period = period
282
+ self.d_mult = h.discriminator_channel_mult
283
+ norm_f = weight_norm if use_spectral_norm == False else spectral_norm
284
+ self.convs = nn.ModuleList([
285
+ norm_f(Conv2d(1, int(32 * self.d_mult), (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
286
+ norm_f(Conv2d(int(32 * self.d_mult), int(128 * self.d_mult), (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
287
+ norm_f(Conv2d(int(128 * self.d_mult), int(512 * self.d_mult), (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
288
+ norm_f(Conv2d(int(512 * self.d_mult), int(1024 * self.d_mult), (kernel_size, 1), (stride, 1), padding=(get_padding(5, 1), 0))),
289
+ norm_f(Conv2d(int(1024 * self.d_mult), int(1024 * self.d_mult), (kernel_size, 1), 1, padding=(2, 0))),
290
+ ])
291
+ self.conv_post = norm_f(Conv2d(int(1024 * self.d_mult), 1, (3, 1), 1, padding=(1, 0)))
292
+
293
+ def forward(self, x):
294
+ fmap = []
295
+
296
+ # 1d to 2d
297
+ b, c, t = x.shape
298
+ if t % self.period != 0: # pad first
299
+ n_pad = self.period - (t % self.period)
300
+ x = F.pad(x, (0, n_pad), "reflect")
301
+ t = t + n_pad
302
+ x = x.view(b, c, t // self.period, self.period)
303
+
304
+ for l in self.convs:
305
+ x = l(x)
306
+ x = F.leaky_relu(x, LRELU_SLOPE)
307
+ fmap.append(x)
308
+ x = self.conv_post(x)
309
+ fmap.append(x)
310
+ x = torch.flatten(x, 1, -1)
311
+
312
+ return x, fmap
313
+
314
+
315
+ class MultiPeriodDiscriminator(torch.nn.Module):
316
+ def __init__(self, h):
317
+ super(MultiPeriodDiscriminator, self).__init__()
318
+ self.mpd_reshapes = h.mpd_reshapes
319
+ print("mpd_reshapes: {}".format(self.mpd_reshapes))
320
+ discriminators = [DiscriminatorP(h, rs, use_spectral_norm=h.use_spectral_norm) for rs in self.mpd_reshapes]
321
+ self.discriminators = nn.ModuleList(discriminators)
322
+
323
+ def forward(self, y, y_hat):
324
+ y_d_rs = []
325
+ y_d_gs = []
326
+ fmap_rs = []
327
+ fmap_gs = []
328
+ for i, d in enumerate(self.discriminators):
329
+ y_d_r, fmap_r = d(y)
330
+ y_d_g, fmap_g = d(y_hat)
331
+ y_d_rs.append(y_d_r)
332
+ fmap_rs.append(fmap_r)
333
+ y_d_gs.append(y_d_g)
334
+ fmap_gs.append(fmap_g)
335
+
336
+ return y_d_rs, y_d_gs, fmap_rs, fmap_gs
337
+
338
+
339
+ class DiscriminatorR(nn.Module):
340
+ def __init__(self, cfg, resolution):
341
+ super().__init__()
342
+
343
+ self.resolution = resolution
344
+ assert len(self.resolution) == 3, \
345
+ "MRD layer requires list with len=3, got {}".format(self.resolution)
346
+ self.lrelu_slope = LRELU_SLOPE
347
+
348
+ norm_f = weight_norm if cfg.use_spectral_norm == False else spectral_norm
349
+ if hasattr(cfg, "mrd_use_spectral_norm"):
350
+ print("INFO: overriding MRD use_spectral_norm as {}".format(cfg.mrd_use_spectral_norm))
351
+ norm_f = weight_norm if cfg.mrd_use_spectral_norm == False else spectral_norm
352
+ self.d_mult = cfg.discriminator_channel_mult
353
+ if hasattr(cfg, "mrd_channel_mult"):
354
+ print("INFO: overriding mrd channel multiplier as {}".format(cfg.mrd_channel_mult))
355
+ self.d_mult = cfg.mrd_channel_mult
356
+
357
+ self.convs = nn.ModuleList([
358
+ norm_f(nn.Conv2d(1, int(32 * self.d_mult), (3, 9), padding=(1, 4))),
359
+ norm_f(nn.Conv2d(int(32 * self.d_mult), int(32 * self.d_mult), (3, 9), stride=(1, 2), padding=(1, 4))),
360
+ norm_f(nn.Conv2d(int(32 * self.d_mult), int(32 * self.d_mult), (3, 9), stride=(1, 2), padding=(1, 4))),
361
+ norm_f(nn.Conv2d(int(32 * self.d_mult), int(32 * self.d_mult), (3, 9), stride=(1, 2), padding=(1, 4))),
362
+ norm_f(nn.Conv2d(int(32 * self.d_mult), int(32 * self.d_mult), (3, 3), padding=(1, 1))),
363
+ ])
364
+ self.conv_post = norm_f(nn.Conv2d(int(32 * self.d_mult), 1, (3, 3), padding=(1, 1)))
365
+
366
+ def forward(self, x):
367
+ fmap = []
368
+
369
+ x = self.spectrogram(x)
370
+ x = x.unsqueeze(1)
371
+ for l in self.convs:
372
+ x = l(x)
373
+ x = F.leaky_relu(x, self.lrelu_slope)
374
+ fmap.append(x)
375
+ x = self.conv_post(x)
376
+ fmap.append(x)
377
+ x = torch.flatten(x, 1, -1)
378
+
379
+ return x, fmap
380
+
381
+ def spectrogram(self, x):
382
+ n_fft, hop_length, win_length = self.resolution
383
+ x = F.pad(x, (int((n_fft - hop_length) / 2), int((n_fft - hop_length) / 2)), mode='reflect')
384
+ x = x.squeeze(1)
385
+ x = torch.stft(x, n_fft=n_fft, hop_length=hop_length, win_length=win_length, center=False, return_complex=True)
386
+ x = torch.view_as_real(x) # [B, F, TT, 2]
387
+ mag = torch.norm(x, p=2, dim=-1) # [B, F, TT]
388
+
389
+ return mag
390
+
391
+
392
+ class MultiResolutionDiscriminator(nn.Module):
393
+ def __init__(self, cfg, debug=False):
394
+ super().__init__()
395
+ self.resolutions = cfg.resolutions
396
+ assert len(self.resolutions) == 3, \
397
+ "MRD requires list of list with len=3, each element having a list with len=3. got {}".\
398
+ format(self.resolutions)
399
+ self.discriminators = nn.ModuleList(
400
+ [DiscriminatorR(cfg, resolution) for resolution in self.resolutions]
401
+ )
402
+
403
+ def forward(self, y, y_hat):
404
+ y_d_rs = []
405
+ y_d_gs = []
406
+ fmap_rs = []
407
+ fmap_gs = []
408
+
409
+ for i, d in enumerate(self.discriminators):
410
+ y_d_r, fmap_r = d(x=y)
411
+ y_d_g, fmap_g = d(x=y_hat)
412
+ y_d_rs.append(y_d_r)
413
+ fmap_rs.append(fmap_r)
414
+ y_d_gs.append(y_d_g)
415
+ fmap_gs.append(fmap_g)
416
+
417
+ return y_d_rs, y_d_gs, fmap_rs, fmap_gs
418
+
419
+
420
+ def feature_loss(fmap_r, fmap_g):
421
+ loss = 0
422
+ for dr, dg in zip(fmap_r, fmap_g):
423
+ for rl, gl in zip(dr, dg):
424
+ loss += torch.mean(torch.abs(rl - gl))
425
+
426
+ return loss * 2
427
+
428
+
429
+ def discriminator_loss(disc_real_outputs, disc_generated_outputs):
430
+ loss = 0
431
+ r_losses = []
432
+ g_losses = []
433
+ for dr, dg in zip(disc_real_outputs, disc_generated_outputs):
434
+ r_loss = torch.mean((1 - dr)**2)
435
+ g_loss = torch.mean(dg**2)
436
+ loss += (r_loss + g_loss)
437
+ r_losses.append(r_loss.item())
438
+ g_losses.append(g_loss.item())
439
+
440
+ return loss, r_losses, g_losses
441
+
442
+
443
+ def generator_loss(disc_outputs):
444
+ loss = 0
445
+ gen_losses = []
446
+ for dg in disc_outputs:
447
+ l = torch.mean((1 - dg)**2)
448
+ gen_losses.append(l)
449
+ loss += l
450
+
451
+ return loss, gen_losses
indextts/BigVGAN/nnet/CNN.py ADDED
@@ -0,0 +1,546 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Library implementing convolutional neural networks.
2
+
3
+ Authors
4
+ * Mirco Ravanelli 2020
5
+ * Jianyuan Zhong 2020
6
+ * Cem Subakan 2021
7
+ * Davide Borra 2021
8
+ * Andreas Nautsch 2022
9
+ * Sarthak Yadav 2022
10
+ """
11
+
12
+ import logging
13
+ import math
14
+ from typing import Tuple
15
+
16
+ import numpy as np
17
+ import torch
18
+ import torch.nn as nn
19
+ import torch.nn.functional as F
20
+ import torchaudio
21
+
22
+
23
+ class SincConv(nn.Module):
24
+ """This function implements SincConv (SincNet).
25
+
26
+ M. Ravanelli, Y. Bengio, "Speaker Recognition from raw waveform with
27
+ SincNet", in Proc. of SLT 2018 (https://arxiv.org/abs/1808.00158)
28
+
29
+ Arguments
30
+ ---------
31
+ out_channels : int
32
+ It is the number of output channels.
33
+ kernel_size: int
34
+ Kernel size of the convolutional filters.
35
+ input_shape : tuple
36
+ The shape of the input. Alternatively use ``in_channels``.
37
+ in_channels : int
38
+ The number of input channels. Alternatively use ``input_shape``.
39
+ stride : int
40
+ Stride factor of the convolutional filters. When the stride factor > 1,
41
+ a decimation in time is performed.
42
+ dilation : int
43
+ Dilation factor of the convolutional filters.
44
+ padding : str
45
+ (same, valid, causal). If "valid", no padding is performed.
46
+ If "same" and stride is 1, output shape is the same as the input shape.
47
+ "causal" results in causal (dilated) convolutions.
48
+ padding_mode : str
49
+ This flag specifies the type of padding. See torch.nn documentation
50
+ for more information.
51
+ sample_rate : int
52
+ Sampling rate of the input signals. It is only used for sinc_conv.
53
+ min_low_hz : float
54
+ Lowest possible frequency (in Hz) for a filter. It is only used for
55
+ sinc_conv.
56
+ min_band_hz : float
57
+ Lowest possible value (in Hz) for a filter bandwidth.
58
+
59
+ Example
60
+ -------
61
+ >>> inp_tensor = torch.rand([10, 16000])
62
+ >>> conv = SincConv(input_shape=inp_tensor.shape, out_channels=25, kernel_size=11)
63
+ >>> out_tensor = conv(inp_tensor)
64
+ >>> out_tensor.shape
65
+ torch.Size([10, 16000, 25])
66
+ """
67
+
68
+ def __init__(
69
+ self,
70
+ out_channels,
71
+ kernel_size,
72
+ input_shape=None,
73
+ in_channels=None,
74
+ stride=1,
75
+ dilation=1,
76
+ padding="same",
77
+ padding_mode="reflect",
78
+ sample_rate=16000,
79
+ min_low_hz=50,
80
+ min_band_hz=50,
81
+ ):
82
+ super().__init__()
83
+ self.in_channels = in_channels
84
+ self.out_channels = out_channels
85
+ self.kernel_size = kernel_size
86
+ self.stride = stride
87
+ self.dilation = dilation
88
+ self.padding = padding
89
+ self.padding_mode = padding_mode
90
+ self.sample_rate = sample_rate
91
+ self.min_low_hz = min_low_hz
92
+ self.min_band_hz = min_band_hz
93
+
94
+ # input shape inference
95
+ if input_shape is None and self.in_channels is None:
96
+ raise ValueError("Must provide one of input_shape or in_channels")
97
+
98
+ if self.in_channels is None:
99
+ self.in_channels = self._check_input_shape(input_shape)
100
+
101
+ if self.out_channels % self.in_channels != 0:
102
+ raise ValueError(
103
+ "Number of output channels must be divisible by in_channels"
104
+ )
105
+
106
+ # Initialize Sinc filters
107
+ self._init_sinc_conv()
108
+
109
+ def forward(self, x):
110
+ """Returns the output of the convolution.
111
+
112
+ Arguments
113
+ ---------
114
+ x : torch.Tensor (batch, time, channel)
115
+ input to convolve. 2d or 4d tensors are expected.
116
+
117
+ Returns
118
+ -------
119
+ wx : torch.Tensor
120
+ The convolved outputs.
121
+ """
122
+ x = x.transpose(1, -1)
123
+ self.device = x.device
124
+
125
+ unsqueeze = x.ndim == 2
126
+ if unsqueeze:
127
+ x = x.unsqueeze(1)
128
+
129
+ if self.padding == "same":
130
+ x = self._manage_padding(
131
+ x, self.kernel_size, self.dilation, self.stride
132
+ )
133
+
134
+ elif self.padding == "causal":
135
+ num_pad = (self.kernel_size - 1) * self.dilation
136
+ x = F.pad(x, (num_pad, 0))
137
+
138
+ elif self.padding == "valid":
139
+ pass
140
+
141
+ else:
142
+ raise ValueError(
143
+ "Padding must be 'same', 'valid' or 'causal'. Got %s."
144
+ % (self.padding)
145
+ )
146
+
147
+ sinc_filters = self._get_sinc_filters()
148
+
149
+ wx = F.conv1d(
150
+ x,
151
+ sinc_filters,
152
+ stride=self.stride,
153
+ padding=0,
154
+ dilation=self.dilation,
155
+ groups=self.in_channels,
156
+ )
157
+
158
+ if unsqueeze:
159
+ wx = wx.squeeze(1)
160
+
161
+ wx = wx.transpose(1, -1)
162
+
163
+ return wx
164
+
165
+ def _check_input_shape(self, shape):
166
+ """Checks the input shape and returns the number of input channels."""
167
+
168
+ if len(shape) == 2:
169
+ in_channels = 1
170
+ elif len(shape) == 3:
171
+ in_channels = shape[-1]
172
+ else:
173
+ raise ValueError(
174
+ "sincconv expects 2d or 3d inputs. Got " + str(len(shape))
175
+ )
176
+
177
+ # Kernel size must be odd
178
+ if self.kernel_size % 2 == 0:
179
+ raise ValueError(
180
+ "The field kernel size must be an odd number. Got %s."
181
+ % (self.kernel_size)
182
+ )
183
+ return in_channels
184
+
185
+ def _get_sinc_filters(self):
186
+ """This functions creates the sinc-filters to used for sinc-conv."""
187
+ # Computing the low frequencies of the filters
188
+ low = self.min_low_hz + torch.abs(self.low_hz_)
189
+
190
+ # Setting minimum band and minimum freq
191
+ high = torch.clamp(
192
+ low + self.min_band_hz + torch.abs(self.band_hz_),
193
+ self.min_low_hz,
194
+ self.sample_rate / 2,
195
+ )
196
+ band = (high - low)[:, 0]
197
+
198
+ # Passing from n_ to the corresponding f_times_t domain
199
+ self.n_ = self.n_.to(self.device)
200
+ self.window_ = self.window_.to(self.device)
201
+ f_times_t_low = torch.matmul(low, self.n_)
202
+ f_times_t_high = torch.matmul(high, self.n_)
203
+
204
+ # Left part of the filters.
205
+ band_pass_left = (
206
+ (torch.sin(f_times_t_high) - torch.sin(f_times_t_low))
207
+ / (self.n_ / 2)
208
+ ) * self.window_
209
+
210
+ # Central element of the filter
211
+ band_pass_center = 2 * band.view(-1, 1)
212
+
213
+ # Right part of the filter (sinc filters are symmetric)
214
+ band_pass_right = torch.flip(band_pass_left, dims=[1])
215
+
216
+ # Combining left, central, and right part of the filter
217
+ band_pass = torch.cat(
218
+ [band_pass_left, band_pass_center, band_pass_right], dim=1
219
+ )
220
+
221
+ # Amplitude normalization
222
+ band_pass = band_pass / (2 * band[:, None])
223
+
224
+ # Setting up the filter coefficients
225
+ filters = band_pass.view(self.out_channels, 1, self.kernel_size)
226
+
227
+ return filters
228
+
229
+ def _init_sinc_conv(self):
230
+ """Initializes the parameters of the sinc_conv layer."""
231
+
232
+ # Initialize filterbanks such that they are equally spaced in Mel scale
233
+ high_hz = self.sample_rate / 2 - (self.min_low_hz + self.min_band_hz)
234
+
235
+ mel = torch.linspace(
236
+ self._to_mel(self.min_low_hz),
237
+ self._to_mel(high_hz),
238
+ self.out_channels + 1,
239
+ )
240
+
241
+ hz = self._to_hz(mel)
242
+
243
+ # Filter lower frequency and bands
244
+ self.low_hz_ = hz[:-1].unsqueeze(1)
245
+ self.band_hz_ = (hz[1:] - hz[:-1]).unsqueeze(1)
246
+
247
+ # Maiking freq and bands learnable
248
+ self.low_hz_ = nn.Parameter(self.low_hz_)
249
+ self.band_hz_ = nn.Parameter(self.band_hz_)
250
+
251
+ # Hamming window
252
+ n_lin = torch.linspace(
253
+ 0, (self.kernel_size / 2) - 1, steps=int((self.kernel_size / 2))
254
+ )
255
+ self.window_ = 0.54 - 0.46 * torch.cos(
256
+ 2 * math.pi * n_lin / self.kernel_size
257
+ )
258
+
259
+ # Time axis (only half is needed due to symmetry)
260
+ n = (self.kernel_size - 1) / 2.0
261
+ self.n_ = (
262
+ 2 * math.pi * torch.arange(-n, 0).view(1, -1) / self.sample_rate
263
+ )
264
+
265
+ def _to_mel(self, hz):
266
+ """Converts frequency in Hz to the mel scale."""
267
+ return 2595 * np.log10(1 + hz / 700)
268
+
269
+ def _to_hz(self, mel):
270
+ """Converts frequency in the mel scale to Hz."""
271
+ return 700 * (10 ** (mel / 2595) - 1)
272
+
273
+ def _manage_padding(self, x, kernel_size: int, dilation: int, stride: int):
274
+ """This function performs zero-padding on the time axis
275
+ such that their lengths is unchanged after the convolution.
276
+
277
+ Arguments
278
+ ---------
279
+ x : torch.Tensor
280
+ Input tensor.
281
+ kernel_size : int
282
+ Size of kernel.
283
+ dilation : int
284
+ Dilation used.
285
+ stride : int
286
+ Stride.
287
+
288
+ Returns
289
+ -------
290
+ x : torch.Tensor
291
+ """
292
+
293
+ # Detecting input shape
294
+ L_in = self.in_channels
295
+
296
+ # Time padding
297
+ padding = get_padding_elem(L_in, stride, kernel_size, dilation)
298
+
299
+ # Applying padding
300
+ x = F.pad(x, padding, mode=self.padding_mode)
301
+
302
+ return x
303
+
304
+
305
+ class Conv1d(nn.Module):
306
+ """This function implements 1d convolution.
307
+
308
+ Arguments
309
+ ---------
310
+ out_channels : int
311
+ It is the number of output channels.
312
+ kernel_size : int
313
+ Kernel size of the convolutional filters.
314
+ input_shape : tuple
315
+ The shape of the input. Alternatively use ``in_channels``.
316
+ in_channels : int
317
+ The number of input channels. Alternatively use ``input_shape``.
318
+ stride : int
319
+ Stride factor of the convolutional filters. When the stride factor > 1,
320
+ a decimation in time is performed.
321
+ dilation : int
322
+ Dilation factor of the convolutional filters.
323
+ padding : str
324
+ (same, valid, causal). If "valid", no padding is performed.
325
+ If "same" and stride is 1, output shape is the same as the input shape.
326
+ "causal" results in causal (dilated) convolutions.
327
+ groups : int
328
+ Number of blocked connections from input channels to output channels.
329
+ bias : bool
330
+ Whether to add a bias term to convolution operation.
331
+ padding_mode : str
332
+ This flag specifies the type of padding. See torch.nn documentation
333
+ for more information.
334
+ skip_transpose : bool
335
+ If False, uses batch x time x channel convention of speechbrain.
336
+ If True, uses batch x channel x time convention.
337
+ weight_norm : bool
338
+ If True, use weight normalization,
339
+ to be removed with self.remove_weight_norm() at inference
340
+ conv_init : str
341
+ Weight initialization for the convolution network
342
+ default_padding: str or int
343
+ This sets the default padding mode that will be used by the pytorch Conv1d backend.
344
+
345
+ Example
346
+ -------
347
+ >>> inp_tensor = torch.rand([10, 40, 16])
348
+ >>> cnn_1d = Conv1d(
349
+ ... input_shape=inp_tensor.shape, out_channels=8, kernel_size=5
350
+ ... )
351
+ >>> out_tensor = cnn_1d(inp_tensor)
352
+ >>> out_tensor.shape
353
+ torch.Size([10, 40, 8])
354
+ """
355
+
356
+ def __init__(
357
+ self,
358
+ out_channels,
359
+ kernel_size,
360
+ input_shape=None,
361
+ in_channels=None,
362
+ stride=1,
363
+ dilation=1,
364
+ padding="same",
365
+ groups=1,
366
+ bias=True,
367
+ padding_mode="reflect",
368
+ skip_transpose=False,
369
+ weight_norm=False,
370
+ conv_init=None,
371
+ default_padding=0,
372
+ ):
373
+ super().__init__()
374
+ self.kernel_size = kernel_size
375
+ self.stride = stride
376
+ self.dilation = dilation
377
+ self.padding = padding
378
+ self.padding_mode = padding_mode
379
+ self.unsqueeze = False
380
+ self.skip_transpose = skip_transpose
381
+
382
+ if input_shape is None and in_channels is None:
383
+ raise ValueError("Must provide one of input_shape or in_channels")
384
+
385
+ if in_channels is None:
386
+ in_channels = self._check_input_shape(input_shape)
387
+
388
+ self.in_channels = in_channels
389
+
390
+ self.conv = nn.Conv1d(
391
+ in_channels,
392
+ out_channels,
393
+ self.kernel_size,
394
+ stride=self.stride,
395
+ dilation=self.dilation,
396
+ padding=default_padding,
397
+ groups=groups,
398
+ bias=bias,
399
+ )
400
+
401
+ if conv_init == "kaiming":
402
+ nn.init.kaiming_normal_(self.conv.weight)
403
+ elif conv_init == "zero":
404
+ nn.init.zeros_(self.conv.weight)
405
+ elif conv_init == "normal":
406
+ nn.init.normal_(self.conv.weight, std=1e-6)
407
+
408
+ if weight_norm:
409
+ self.conv = nn.utils.weight_norm(self.conv)
410
+
411
+ def forward(self, x):
412
+ """Returns the output of the convolution.
413
+
414
+ Arguments
415
+ ---------
416
+ x : torch.Tensor (batch, time, channel)
417
+ input to convolve. 2d or 4d tensors are expected.
418
+
419
+ Returns
420
+ -------
421
+ wx : torch.Tensor
422
+ The convolved outputs.
423
+ """
424
+ if not self.skip_transpose:
425
+ x = x.transpose(1, -1)
426
+
427
+ if self.unsqueeze:
428
+ x = x.unsqueeze(1)
429
+
430
+ if self.padding == "same":
431
+ x = self._manage_padding(
432
+ x, self.kernel_size, self.dilation, self.stride
433
+ )
434
+
435
+ elif self.padding == "causal":
436
+ num_pad = (self.kernel_size - 1) * self.dilation
437
+ x = F.pad(x, (num_pad, 0))
438
+
439
+ elif self.padding == "valid":
440
+ pass
441
+
442
+ else:
443
+ raise ValueError(
444
+ "Padding must be 'same', 'valid' or 'causal'. Got "
445
+ + self.padding
446
+ )
447
+
448
+ wx = self.conv(x)
449
+
450
+ if self.unsqueeze:
451
+ wx = wx.squeeze(1)
452
+
453
+ if not self.skip_transpose:
454
+ wx = wx.transpose(1, -1)
455
+
456
+ return wx
457
+
458
+ def _manage_padding(self, x, kernel_size: int, dilation: int, stride: int):
459
+ """This function performs zero-padding on the time axis
460
+ such that their lengths is unchanged after the convolution.
461
+
462
+ Arguments
463
+ ---------
464
+ x : torch.Tensor
465
+ Input tensor.
466
+ kernel_size : int
467
+ Size of kernel.
468
+ dilation : int
469
+ Dilation used.
470
+ stride : int
471
+ Stride.
472
+
473
+ Returns
474
+ -------
475
+ x : torch.Tensor
476
+ The padded outputs.
477
+ """
478
+
479
+ # Detecting input shape
480
+ L_in = self.in_channels
481
+
482
+ # Time padding
483
+ padding = get_padding_elem(L_in, stride, kernel_size, dilation)
484
+
485
+ # Applying padding
486
+ x = F.pad(x, padding, mode=self.padding_mode)
487
+
488
+ return x
489
+
490
+ def _check_input_shape(self, shape):
491
+ """Checks the input shape and returns the number of input channels."""
492
+
493
+ if len(shape) == 2:
494
+ self.unsqueeze = True
495
+ in_channels = 1
496
+ elif self.skip_transpose:
497
+ in_channels = shape[1]
498
+ elif len(shape) == 3:
499
+ in_channels = shape[2]
500
+ else:
501
+ raise ValueError(
502
+ "conv1d expects 2d, 3d inputs. Got " + str(len(shape))
503
+ )
504
+
505
+ # Kernel size must be odd
506
+ if not self.padding == "valid" and self.kernel_size % 2 == 0:
507
+ raise ValueError(
508
+ "The field kernel size must be an odd number. Got %s."
509
+ % (self.kernel_size)
510
+ )
511
+
512
+ return in_channels
513
+
514
+ def remove_weight_norm(self):
515
+ """Removes weight normalization at inference if used during training."""
516
+ self.conv = nn.utils.remove_weight_norm(self.conv)
517
+
518
+
519
+ def get_padding_elem(L_in: int, stride: int, kernel_size: int, dilation: int):
520
+ """This function computes the number of elements to add for zero-padding.
521
+
522
+ Arguments
523
+ ---------
524
+ L_in : int
525
+ stride: int
526
+ kernel_size : int
527
+ dilation : int
528
+
529
+ Returns
530
+ -------
531
+ padding : int
532
+ The size of the padding to be added
533
+ """
534
+ if stride > 1:
535
+ padding = [math.floor(kernel_size / 2), math.floor(kernel_size / 2)]
536
+
537
+ else:
538
+ L_out = (
539
+ math.floor((L_in - dilation * (kernel_size - 1) - 1) / stride) + 1
540
+ )
541
+ padding = [
542
+ math.floor((L_in - L_out) / 2),
543
+ math.floor((L_in - L_out) / 2),
544
+ ]
545
+ return padding
546
+
indextts/BigVGAN/nnet/__init__.py ADDED
File without changes
indextts/BigVGAN/nnet/linear.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Library implementing linear transformation.
2
+
3
+ Authors
4
+ * Mirco Ravanelli 2020
5
+ * Davide Borra 2021
6
+ """
7
+
8
+ import logging
9
+
10
+ import torch
11
+ import torch.nn as nn
12
+
13
+
14
+ class Linear(torch.nn.Module):
15
+ """Computes a linear transformation y = wx + b.
16
+
17
+ Arguments
18
+ ---------
19
+ n_neurons : int
20
+ It is the number of output neurons (i.e, the dimensionality of the
21
+ output).
22
+ input_shape : tuple
23
+ It is the shape of the input tensor.
24
+ input_size : int
25
+ Size of the input tensor.
26
+ bias : bool
27
+ If True, the additive bias b is adopted.
28
+ max_norm : float
29
+ weight max-norm.
30
+ combine_dims : bool
31
+ If True and the input is 4D, combine 3rd and 4th dimensions of input.
32
+
33
+ Example
34
+ -------
35
+ >>> inputs = torch.rand(10, 50, 40)
36
+ >>> lin_t = Linear(input_shape=(10, 50, 40), n_neurons=100)
37
+ >>> output = lin_t(inputs)
38
+ >>> output.shape
39
+ torch.Size([10, 50, 100])
40
+ """
41
+
42
+ def __init__(
43
+ self,
44
+ n_neurons,
45
+ input_shape=None,
46
+ input_size=None,
47
+ bias=True,
48
+ max_norm=None,
49
+ combine_dims=False,
50
+ ):
51
+ super().__init__()
52
+ self.max_norm = max_norm
53
+ self.combine_dims = combine_dims
54
+
55
+ if input_shape is None and input_size is None:
56
+ raise ValueError("Expected one of input_shape or input_size")
57
+
58
+ if input_size is None:
59
+ input_size = input_shape[-1]
60
+ if len(input_shape) == 4 and self.combine_dims:
61
+ input_size = input_shape[2] * input_shape[3]
62
+
63
+ # Weights are initialized following pytorch approach
64
+ self.w = nn.Linear(input_size, n_neurons, bias=bias)
65
+
66
+ def forward(self, x):
67
+ """Returns the linear transformation of input tensor.
68
+
69
+ Arguments
70
+ ---------
71
+ x : torch.Tensor
72
+ Input to transform linearly.
73
+
74
+ Returns
75
+ -------
76
+ wx : torch.Tensor
77
+ The linearly transformed outputs.
78
+ """
79
+ if x.ndim == 4 and self.combine_dims:
80
+ x = x.reshape(x.shape[0], x.shape[1], x.shape[2] * x.shape[3])
81
+
82
+ if self.max_norm is not None:
83
+ self.w.weight.data = torch.renorm(
84
+ self.w.weight.data, p=2, dim=0, maxnorm=self.max_norm
85
+ )
86
+
87
+ wx = self.w(x)
88
+
89
+ return wx
indextts/BigVGAN/nnet/normalization.py ADDED
@@ -0,0 +1,670 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Library implementing normalization.
2
+
3
+ Authors
4
+ * Mirco Ravanelli 2020
5
+ * Guillermo Cámbara 2021
6
+ * Sarthak Yadav 2022
7
+ """
8
+
9
+ import torch
10
+ import torch.nn as nn
11
+
12
+
13
+ class BatchNorm1d(nn.Module):
14
+ """Applies 1d batch normalization to the input tensor.
15
+
16
+ Arguments
17
+ ---------
18
+ input_shape : tuple
19
+ The expected shape of the input. Alternatively, use ``input_size``.
20
+ input_size : int
21
+ The expected size of the input. Alternatively, use ``input_shape``.
22
+ eps : float
23
+ This value is added to std deviation estimation to improve the numerical
24
+ stability.
25
+ momentum : float
26
+ It is a value used for the running_mean and running_var computation.
27
+ affine : bool
28
+ When set to True, the affine parameters are learned.
29
+ track_running_stats : bool
30
+ When set to True, this module tracks the running mean and variance,
31
+ and when set to False, this module does not track such statistics.
32
+ combine_batch_time : bool
33
+ When true, it combines batch an time axis.
34
+ skip_transpose : bool
35
+ Whether to skip the transposition.
36
+
37
+
38
+ Example
39
+ -------
40
+ >>> input = torch.randn(100, 10)
41
+ >>> norm = BatchNorm1d(input_shape=input.shape)
42
+ >>> output = norm(input)
43
+ >>> output.shape
44
+ torch.Size([100, 10])
45
+ """
46
+
47
+ def __init__(
48
+ self,
49
+ input_shape=None,
50
+ input_size=None,
51
+ eps=1e-05,
52
+ momentum=0.1,
53
+ affine=True,
54
+ track_running_stats=True,
55
+ combine_batch_time=False,
56
+ skip_transpose=False,
57
+ ):
58
+ super().__init__()
59
+ self.combine_batch_time = combine_batch_time
60
+ self.skip_transpose = skip_transpose
61
+
62
+ if input_size is None and skip_transpose:
63
+ input_size = input_shape[1]
64
+ elif input_size is None:
65
+ input_size = input_shape[-1]
66
+
67
+ self.norm = nn.BatchNorm1d(
68
+ input_size,
69
+ eps=eps,
70
+ momentum=momentum,
71
+ affine=affine,
72
+ track_running_stats=track_running_stats,
73
+ )
74
+
75
+ def forward(self, x):
76
+ """Returns the normalized input tensor.
77
+
78
+ Arguments
79
+ ---------
80
+ x : torch.Tensor (batch, time, [channels])
81
+ input to normalize. 2d or 3d tensors are expected in input
82
+ 4d tensors can be used when combine_dims=True.
83
+
84
+ Returns
85
+ -------
86
+ x_n : torch.Tensor
87
+ The normalized outputs.
88
+ """
89
+ shape_or = x.shape
90
+ if self.combine_batch_time:
91
+ if x.ndim == 3:
92
+ x = x.reshape(shape_or[0] * shape_or[1], shape_or[2])
93
+ else:
94
+ x = x.reshape(
95
+ shape_or[0] * shape_or[1], shape_or[3], shape_or[2]
96
+ )
97
+
98
+ elif not self.skip_transpose:
99
+ x = x.transpose(-1, 1)
100
+
101
+ x_n = self.norm(x)
102
+
103
+ if self.combine_batch_time:
104
+ x_n = x_n.reshape(shape_or)
105
+ elif not self.skip_transpose:
106
+ x_n = x_n.transpose(1, -1)
107
+
108
+ return x_n
109
+
110
+
111
+ class BatchNorm2d(nn.Module):
112
+ """Applies 2d batch normalization to the input tensor.
113
+
114
+ Arguments
115
+ ---------
116
+ input_shape : tuple
117
+ The expected shape of the input. Alternatively, use ``input_size``.
118
+ input_size : int
119
+ The expected size of the input. Alternatively, use ``input_shape``.
120
+ eps : float
121
+ This value is added to std deviation estimation to improve the numerical
122
+ stability.
123
+ momentum : float
124
+ It is a value used for the running_mean and running_var computation.
125
+ affine : bool
126
+ When set to True, the affine parameters are learned.
127
+ track_running_stats : bool
128
+ When set to True, this module tracks the running mean and variance,
129
+ and when set to False, this module does not track such statistics.
130
+
131
+ Example
132
+ -------
133
+ >>> input = torch.randn(100, 10, 5, 20)
134
+ >>> norm = BatchNorm2d(input_shape=input.shape)
135
+ >>> output = norm(input)
136
+ >>> output.shape
137
+ torch.Size([100, 10, 5, 20])
138
+ """
139
+
140
+ def __init__(
141
+ self,
142
+ input_shape=None,
143
+ input_size=None,
144
+ eps=1e-05,
145
+ momentum=0.1,
146
+ affine=True,
147
+ track_running_stats=True,
148
+ ):
149
+ super().__init__()
150
+
151
+ if input_shape is None and input_size is None:
152
+ raise ValueError("Expected input_shape or input_size as input")
153
+
154
+ if input_size is None:
155
+ input_size = input_shape[-1]
156
+
157
+ self.norm = nn.BatchNorm2d(
158
+ input_size,
159
+ eps=eps,
160
+ momentum=momentum,
161
+ affine=affine,
162
+ track_running_stats=track_running_stats,
163
+ )
164
+
165
+ def forward(self, x):
166
+ """Returns the normalized input tensor.
167
+
168
+ Arguments
169
+ ---------
170
+ x : torch.Tensor (batch, time, channel1, channel2)
171
+ input to normalize. 4d tensors are expected.
172
+
173
+ Returns
174
+ -------
175
+ x_n : torch.Tensor
176
+ The normalized outputs.
177
+ """
178
+ x = x.transpose(-1, 1)
179
+ x_n = self.norm(x)
180
+ x_n = x_n.transpose(1, -1)
181
+
182
+ return x_n
183
+
184
+
185
+ class LayerNorm(nn.Module):
186
+ """Applies layer normalization to the input tensor.
187
+
188
+ Arguments
189
+ ---------
190
+ input_size : int
191
+ The expected size of the dimension to be normalized.
192
+ input_shape : tuple
193
+ The expected shape of the input.
194
+ eps : float
195
+ This value is added to std deviation estimation to improve the numerical
196
+ stability.
197
+ elementwise_affine : bool
198
+ If True, this module has learnable per-element affine parameters
199
+ initialized to ones (for weights) and zeros (for biases).
200
+
201
+ Example
202
+ -------
203
+ >>> input = torch.randn(100, 101, 128)
204
+ >>> norm = LayerNorm(input_shape=input.shape)
205
+ >>> output = norm(input)
206
+ >>> output.shape
207
+ torch.Size([100, 101, 128])
208
+ """
209
+
210
+ def __init__(
211
+ self,
212
+ input_size=None,
213
+ input_shape=None,
214
+ eps=1e-05,
215
+ elementwise_affine=True,
216
+ ):
217
+ super().__init__()
218
+ self.eps = eps
219
+ self.elementwise_affine = elementwise_affine
220
+
221
+ if input_shape is not None:
222
+ input_size = input_shape[2:]
223
+
224
+ self.norm = torch.nn.LayerNorm(
225
+ input_size,
226
+ eps=self.eps,
227
+ elementwise_affine=self.elementwise_affine,
228
+ )
229
+
230
+ def forward(self, x):
231
+ """Returns the normalized input tensor.
232
+
233
+ Arguments
234
+ ---------
235
+ x : torch.Tensor (batch, time, channels)
236
+ input to normalize. 3d or 4d tensors are expected.
237
+
238
+ Returns
239
+ -------
240
+ The normalized outputs.
241
+ """
242
+ return self.norm(x)
243
+
244
+
245
+ class InstanceNorm1d(nn.Module):
246
+ """Applies 1d instance normalization to the input tensor.
247
+
248
+ Arguments
249
+ ---------
250
+ input_shape : tuple
251
+ The expected shape of the input. Alternatively, use ``input_size``.
252
+ input_size : int
253
+ The expected size of the input. Alternatively, use ``input_shape``.
254
+ eps : float
255
+ This value is added to std deviation estimation to improve the numerical
256
+ stability.
257
+ momentum : float
258
+ It is a value used for the running_mean and running_var computation.
259
+ track_running_stats : bool
260
+ When set to True, this module tracks the running mean and variance,
261
+ and when set to False, this module does not track such statistics.
262
+ affine : bool
263
+ A boolean value that when set to True, this module has learnable
264
+ affine parameters, initialized the same way as done for
265
+ batch normalization. Default: False.
266
+
267
+ Example
268
+ -------
269
+ >>> input = torch.randn(100, 10, 20)
270
+ >>> norm = InstanceNorm1d(input_shape=input.shape)
271
+ >>> output = norm(input)
272
+ >>> output.shape
273
+ torch.Size([100, 10, 20])
274
+ """
275
+
276
+ def __init__(
277
+ self,
278
+ input_shape=None,
279
+ input_size=None,
280
+ eps=1e-05,
281
+ momentum=0.1,
282
+ track_running_stats=True,
283
+ affine=False,
284
+ ):
285
+ super().__init__()
286
+
287
+ if input_shape is None and input_size is None:
288
+ raise ValueError("Expected input_shape or input_size as input")
289
+
290
+ if input_size is None:
291
+ input_size = input_shape[-1]
292
+
293
+ self.norm = nn.InstanceNorm1d(
294
+ input_size,
295
+ eps=eps,
296
+ momentum=momentum,
297
+ track_running_stats=track_running_stats,
298
+ affine=affine,
299
+ )
300
+
301
+ def forward(self, x):
302
+ """Returns the normalized input tensor.
303
+
304
+ Arguments
305
+ ---------
306
+ x : torch.Tensor (batch, time, channels)
307
+ input to normalize. 3d tensors are expected.
308
+
309
+ Returns
310
+ -------
311
+ x_n : torch.Tensor
312
+ The normalized outputs.
313
+ """
314
+ x = x.transpose(-1, 1)
315
+ x_n = self.norm(x)
316
+ x_n = x_n.transpose(1, -1)
317
+
318
+ return x_n
319
+
320
+
321
+ class InstanceNorm2d(nn.Module):
322
+ """Applies 2d instance normalization to the input tensor.
323
+
324
+ Arguments
325
+ ---------
326
+ input_shape : tuple
327
+ The expected shape of the input. Alternatively, use ``input_size``.
328
+ input_size : int
329
+ The expected size of the input. Alternatively, use ``input_shape``.
330
+ eps : float
331
+ This value is added to std deviation estimation to improve the numerical
332
+ stability.
333
+ momentum : float
334
+ It is a value used for the running_mean and running_var computation.
335
+ track_running_stats : bool
336
+ When set to True, this module tracks the running mean and variance,
337
+ and when set to False, this module does not track such statistics.
338
+ affine : bool
339
+ A boolean value that when set to True, this module has learnable
340
+ affine parameters, initialized the same way as done for
341
+ batch normalization. Default: False.
342
+
343
+ Example
344
+ -------
345
+ >>> input = torch.randn(100, 10, 20, 2)
346
+ >>> norm = InstanceNorm2d(input_shape=input.shape)
347
+ >>> output = norm(input)
348
+ >>> output.shape
349
+ torch.Size([100, 10, 20, 2])
350
+ """
351
+
352
+ def __init__(
353
+ self,
354
+ input_shape=None,
355
+ input_size=None,
356
+ eps=1e-05,
357
+ momentum=0.1,
358
+ track_running_stats=True,
359
+ affine=False,
360
+ ):
361
+ super().__init__()
362
+
363
+ if input_shape is None and input_size is None:
364
+ raise ValueError("Expected input_shape or input_size as input")
365
+
366
+ if input_size is None:
367
+ input_size = input_shape[-1]
368
+
369
+ self.norm = nn.InstanceNorm2d(
370
+ input_size,
371
+ eps=eps,
372
+ momentum=momentum,
373
+ track_running_stats=track_running_stats,
374
+ affine=affine,
375
+ )
376
+
377
+ def forward(self, x):
378
+ """Returns the normalized input tensor.
379
+
380
+ Arguments
381
+ ---------
382
+ x : torch.Tensor (batch, time, channel1, channel2)
383
+ input to normalize. 4d tensors are expected.
384
+
385
+ Returns
386
+ -------
387
+ x_n : torch.Tensor
388
+ The normalized outputs.
389
+ """
390
+ x = x.transpose(-1, 1)
391
+ x_n = self.norm(x)
392
+ x_n = x_n.transpose(1, -1)
393
+
394
+ return x_n
395
+
396
+
397
+ class GroupNorm(nn.Module):
398
+ """Applies group normalization to the input tensor.
399
+
400
+ Arguments
401
+ ---------
402
+ input_shape : tuple
403
+ The expected shape of the input. Alternatively, use ``input_size``.
404
+ input_size : int
405
+ The expected size of the input. Alternatively, use ``input_shape``.
406
+ num_groups : int
407
+ Number of groups to separate the channels into.
408
+ eps : float
409
+ This value is added to std deviation estimation to improve the numerical
410
+ stability.
411
+ affine : bool
412
+ A boolean value that when set to True, this module has learnable per-channel
413
+ affine parameters initialized to ones (for weights) and zeros (for biases).
414
+
415
+ Example
416
+ -------
417
+ >>> input = torch.randn(100, 101, 128)
418
+ >>> norm = GroupNorm(input_size=128, num_groups=128)
419
+ >>> output = norm(input)
420
+ >>> output.shape
421
+ torch.Size([100, 101, 128])
422
+ """
423
+
424
+ def __init__(
425
+ self,
426
+ input_shape=None,
427
+ input_size=None,
428
+ num_groups=None,
429
+ eps=1e-05,
430
+ affine=True,
431
+ ):
432
+ super().__init__()
433
+ self.eps = eps
434
+ self.affine = affine
435
+
436
+ if input_shape is None and input_size is None:
437
+ raise ValueError("Expected input_shape or input_size as input")
438
+
439
+ if num_groups is None:
440
+ raise ValueError("Expected num_groups as input")
441
+
442
+ if input_shape is not None:
443
+ input_size = input_shape[-1]
444
+
445
+ self.norm = torch.nn.GroupNorm(
446
+ num_groups,
447
+ input_size,
448
+ eps=self.eps,
449
+ affine=self.affine,
450
+ )
451
+
452
+ def forward(self, x):
453
+ """Returns the normalized input tensor.
454
+
455
+ Arguments
456
+ ---------
457
+ x : torch.Tensor (batch, time, channels)
458
+ input to normalize. 3d or 4d tensors are expected.
459
+
460
+ Returns
461
+ -------
462
+ x_n : torch.Tensor
463
+ The normalized outputs.
464
+ """
465
+ x = x.transpose(-1, 1)
466
+ x_n = self.norm(x)
467
+ x_n = x_n.transpose(1, -1)
468
+
469
+ return x_n
470
+
471
+
472
+ class ExponentialMovingAverage(nn.Module):
473
+ """
474
+ Applies learnable exponential moving average, as required by learnable PCEN layer
475
+
476
+ Arguments
477
+ ---------
478
+ input_size : int
479
+ The expected size of the input.
480
+ coeff_init: float
481
+ Initial smoothing coefficient value
482
+ per_channel: bool
483
+ Controls whether every smoothing coefficients are learned
484
+ independently for every input channel
485
+ trainable: bool
486
+ whether to learn the PCEN parameters or use fixed
487
+ skip_transpose : bool
488
+ If False, uses batch x time x channel convention of speechbrain.
489
+ If True, uses batch x channel x time convention.
490
+
491
+ Example
492
+ -------
493
+ >>> inp_tensor = torch.rand([10, 50, 40])
494
+ >>> pcen = ExponentialMovingAverage(40)
495
+ >>> out_tensor = pcen(inp_tensor)
496
+ >>> out_tensor.shape
497
+ torch.Size([10, 50, 40])
498
+ """
499
+
500
+ def __init__(
501
+ self,
502
+ input_size: int,
503
+ coeff_init: float = 0.04,
504
+ per_channel: bool = False,
505
+ trainable: bool = True,
506
+ skip_transpose: bool = False,
507
+ ):
508
+ super().__init__()
509
+ self._coeff_init = coeff_init
510
+ self._per_channel = per_channel
511
+ self.skip_transpose = skip_transpose
512
+ self.trainable = trainable
513
+ weights = (
514
+ torch.ones(
515
+ input_size,
516
+ )
517
+ if self._per_channel
518
+ else torch.ones(
519
+ 1,
520
+ )
521
+ )
522
+ self._weights = nn.Parameter(
523
+ weights * self._coeff_init, requires_grad=trainable
524
+ )
525
+
526
+ def forward(self, x):
527
+ """Returns the normalized input tensor.
528
+
529
+ Arguments
530
+ ---------
531
+ x : torch.Tensor (batch, time, channels)
532
+ input to normalize.
533
+ """
534
+ if not self.skip_transpose:
535
+ x = x.transpose(1, -1)
536
+ w = torch.clamp(self._weights, min=0.0, max=1.0)
537
+ initial_state = x[:, :, 0]
538
+
539
+ def scan(init_state, x, w):
540
+ """Loops and accumulates."""
541
+ x = x.permute(2, 0, 1)
542
+ acc = init_state
543
+ results = []
544
+ for ix in range(x.shape[0]):
545
+ acc = (w * x[ix]) + ((1.0 - w) * acc)
546
+ results.append(acc.unsqueeze(0))
547
+ results = torch.cat(results, dim=0)
548
+ results = results.permute(1, 2, 0)
549
+ return results
550
+
551
+ output = scan(initial_state, x, w)
552
+ if not self.skip_transpose:
553
+ output = output.transpose(1, -1)
554
+ return output
555
+
556
+
557
+ class PCEN(nn.Module):
558
+ """
559
+ This class implements a learnable Per-channel energy normalization (PCEN) layer, supporting both
560
+ original PCEN as specified in [1] as well as sPCEN as specified in [2]
561
+
562
+ [1] Yuxuan Wang, Pascal Getreuer, Thad Hughes, Richard F. Lyon, Rif A. Saurous, "Trainable Frontend For
563
+ Robust and Far-Field Keyword Spotting", in Proc of ICASSP 2017 (https://arxiv.org/abs/1607.05666)
564
+
565
+ [2] Neil Zeghidour, Olivier Teboul, F{\'e}lix de Chaumont Quitry & Marco Tagliasacchi, "LEAF: A LEARNABLE FRONTEND
566
+ FOR AUDIO CLASSIFICATION", in Proc of ICLR 2021 (https://arxiv.org/abs/2101.08596)
567
+
568
+ The default argument values correspond with those used by [2].
569
+
570
+ Arguments
571
+ ---------
572
+ input_size : int
573
+ The expected size of the input.
574
+ alpha: float
575
+ specifies alpha coefficient for PCEN
576
+ smooth_coef: float
577
+ specified smooth coefficient for PCEN
578
+ delta: float
579
+ specifies delta coefficient for PCEN
580
+ root: float
581
+ specifies root coefficient for PCEN
582
+ floor: float
583
+ specifies floor coefficient for PCEN
584
+ trainable: bool
585
+ whether to learn the PCEN parameters or use fixed
586
+ per_channel_smooth_coef: bool
587
+ whether to learn independent smooth coefficients for every channel.
588
+ when True, essentially using sPCEN from [2]
589
+ skip_transpose : bool
590
+ If False, uses batch x time x channel convention of speechbrain.
591
+ If True, uses batch x channel x time convention.
592
+
593
+ Example
594
+ -------
595
+ >>> inp_tensor = torch.rand([10, 50, 40])
596
+ >>> pcen = PCEN(40, alpha=0.96) # sPCEN
597
+ >>> out_tensor = pcen(inp_tensor)
598
+ >>> out_tensor.shape
599
+ torch.Size([10, 50, 40])
600
+ """
601
+
602
+ def __init__(
603
+ self,
604
+ input_size,
605
+ alpha: float = 0.96,
606
+ smooth_coef: float = 0.04,
607
+ delta: float = 2.0,
608
+ root: float = 2.0,
609
+ floor: float = 1e-12,
610
+ trainable: bool = True,
611
+ per_channel_smooth_coef: bool = True,
612
+ skip_transpose: bool = False,
613
+ ):
614
+ super().__init__()
615
+ self._smooth_coef = smooth_coef
616
+ self._floor = floor
617
+ self._per_channel_smooth_coef = per_channel_smooth_coef
618
+ self.skip_transpose = skip_transpose
619
+ self.alpha = nn.Parameter(
620
+ torch.ones(input_size) * alpha, requires_grad=trainable
621
+ )
622
+ self.delta = nn.Parameter(
623
+ torch.ones(input_size) * delta, requires_grad=trainable
624
+ )
625
+ self.root = nn.Parameter(
626
+ torch.ones(input_size) * root, requires_grad=trainable
627
+ )
628
+
629
+ self.ema = ExponentialMovingAverage(
630
+ input_size,
631
+ coeff_init=self._smooth_coef,
632
+ per_channel=self._per_channel_smooth_coef,
633
+ skip_transpose=True,
634
+ trainable=trainable,
635
+ )
636
+
637
+ def forward(self, x):
638
+ """Returns the normalized input tensor.
639
+
640
+ Arguments
641
+ ---------
642
+ x : torch.Tensor (batch, time, channels)
643
+ input to normalize.
644
+
645
+ Returns
646
+ -------
647
+ output : torch.Tensor
648
+ The normalized outputs.
649
+ """
650
+ if not self.skip_transpose:
651
+ x = x.transpose(1, -1)
652
+ alpha = torch.min(
653
+ self.alpha, torch.tensor(1.0, dtype=x.dtype, device=x.device)
654
+ )
655
+ root = torch.max(
656
+ self.root, torch.tensor(1.0, dtype=x.dtype, device=x.device)
657
+ )
658
+ ema_smoother = self.ema(x)
659
+ one_over_root = 1.0 / root
660
+ output = (
661
+ x / (self._floor + ema_smoother) ** alpha.view(1, -1, 1)
662
+ + self.delta.view(1, -1, 1)
663
+ ) ** one_over_root.view(1, -1, 1) - self.delta.view(
664
+ 1, -1, 1
665
+ ) ** one_over_root.view(
666
+ 1, -1, 1
667
+ )
668
+ if not self.skip_transpose:
669
+ output = output.transpose(1, -1)
670
+ return output
indextts/BigVGAN/utils.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Adapted from https://github.com/jik876/hifi-gan under the MIT license.
2
+ # LICENSE is in incl_licenses directory.
3
+
4
+ import glob
5
+ import os
6
+
7
+ import matplotlib
8
+ import matplotlib.pylab as plt
9
+ import torch
10
+ from scipy.io.wavfile import write
11
+ from torch.nn.utils import weight_norm
12
+
13
+ matplotlib.use("Agg")
14
+
15
+ MAX_WAV_VALUE = 32768.0
16
+
17
+
18
+ def plot_spectrogram(spectrogram):
19
+ fig, ax = plt.subplots(figsize=(10, 2))
20
+ im = ax.imshow(spectrogram, aspect="auto", origin="lower", interpolation="none")
21
+ plt.colorbar(im, ax=ax)
22
+
23
+ fig.canvas.draw()
24
+ plt.close()
25
+
26
+ return fig
27
+
28
+
29
+ def plot_spectrogram_clipped(spectrogram, clip_max=2.0):
30
+ fig, ax = plt.subplots(figsize=(10, 2))
31
+ im = ax.imshow(
32
+ spectrogram,
33
+ aspect="auto",
34
+ origin="lower",
35
+ interpolation="none",
36
+ vmin=1e-6,
37
+ vmax=clip_max,
38
+ )
39
+ plt.colorbar(im, ax=ax)
40
+
41
+ fig.canvas.draw()
42
+ plt.close()
43
+
44
+ return fig
45
+
46
+
47
+ def init_weights(m, mean=0.0, std=0.01):
48
+ classname = m.__class__.__name__
49
+ if classname.find("Conv") != -1:
50
+ m.weight.data.normal_(mean, std)
51
+
52
+
53
+ def apply_weight_norm(m):
54
+ classname = m.__class__.__name__
55
+ if classname.find("Conv") != -1:
56
+ weight_norm(m)
57
+
58
+
59
+ def get_padding(kernel_size, dilation=1):
60
+ return int((kernel_size * dilation - dilation) / 2)
61
+
62
+
63
+ def load_checkpoint(filepath, device):
64
+ assert os.path.isfile(filepath)
65
+ print(f"Loading '{filepath}'")
66
+ checkpoint_dict = torch.load(filepath, map_location=device)
67
+ print("Complete.")
68
+ return checkpoint_dict
69
+
70
+
71
+ def save_checkpoint(filepath, obj):
72
+ print(f"Saving checkpoint to {filepath}")
73
+ torch.save(obj, filepath)
74
+ print("Complete.")
75
+
76
+
77
+ def scan_checkpoint(cp_dir, prefix, renamed_file=None):
78
+ # Fallback to original scanning logic first
79
+ pattern = os.path.join(cp_dir, prefix + "????????")
80
+ cp_list = glob.glob(pattern)
81
+
82
+ if len(cp_list) > 0:
83
+ last_checkpoint_path = sorted(cp_list)[-1]
84
+ print(f"[INFO] Resuming from checkpoint: '{last_checkpoint_path}'")
85
+ return last_checkpoint_path
86
+
87
+ # If no pattern-based checkpoints are found, check for renamed file
88
+ if renamed_file:
89
+ renamed_path = os.path.join(cp_dir, renamed_file)
90
+ if os.path.isfile(renamed_path):
91
+ print(f"[INFO] Resuming from renamed checkpoint: '{renamed_file}'")
92
+ return renamed_path
93
+
94
+ return None
95
+
96
+
97
+ def save_audio(audio, path, sr):
98
+ # wav: torch with 1d shape
99
+ audio = audio * MAX_WAV_VALUE
100
+ audio = audio.cpu().numpy().astype("int16")
101
+ write(path, sr, audio)
indextts/__init__.py ADDED
File without changes
indextts/accel/__init__.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ from .accel_engine import AccelInferenceEngine # noqa: F401
2
+ from .attention import ( # noqa: F401
3
+ Attention,
4
+ get_forward_context,
5
+ reset_forward_context,
6
+ set_forward_context,
7
+ )
8
+ from .gpt2_accel import GPT2AccelAttention, GPT2AccelModel # noqa: F401
9
+ from .kv_manager import KVCacheManager, Seq # noqa: F401
indextts/accel/accel_engine.py ADDED
@@ -0,0 +1,609 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ from typing import List, Optional
3
+
4
+ import torch
5
+ from torch import nn
6
+
7
+ from .attention import (
8
+ ForwardContext,
9
+ get_forward_context,
10
+ reset_forward_context,
11
+ set_forward_context,
12
+ )
13
+ from .kv_manager import KVCacheManager, Seq
14
+
15
+
16
+ class Sampler(nn.Module):
17
+ def __init__(self):
18
+ super().__init__()
19
+
20
+ @torch.compile
21
+ def forward(self, logits: torch.Tensor, temperatures: torch.Tensor):
22
+ logits = logits.float().div_(temperatures.unsqueeze(dim=1))
23
+ probs = torch.softmax(logits, dim=-1)
24
+ sample_tokens = probs.div_(
25
+ torch.empty_like(probs).exponential_(1).clamp_min_(1e-10)
26
+ ).argmax(dim=-1)
27
+ return sample_tokens
28
+
29
+
30
+ class AccelInferenceEngine:
31
+ def __init__(
32
+ self,
33
+ model,
34
+ lm_head,
35
+ num_layers: int,
36
+ num_heads: int,
37
+ head_dim: int,
38
+ block_size: int = 256,
39
+ num_blocks: int = 128,
40
+ use_cuda_graph: bool = True,
41
+ ):
42
+ """
43
+ Args:
44
+ model: The GPT transformer model (should have accel attention)
45
+ lm_head: Language model head for generating logits
46
+ num_layers: Number of transformer layers
47
+ num_heads: Number of attention heads
48
+ head_dim: Dimension per head
49
+ block_size: KV cache block size
50
+ num_blocks: Total number of KV cache blocks
51
+ use_cuda_graph: Whether to use CUDA Graph for decode optimization
52
+ """
53
+ self.model = model
54
+ self.lm_head = lm_head
55
+ self.block_size = block_size
56
+ self.num_blocks = num_blocks
57
+ self.use_cuda_graph = use_cuda_graph and torch.cuda.is_available()
58
+ model_dtype = next(model.parameters()).dtype
59
+ self.hidden_size = (
60
+ model.config.hidden_size
61
+ if hasattr(model, "config")
62
+ else head_dim * num_heads
63
+ )
64
+ self.kv_manager = KVCacheManager(
65
+ num_layers=num_layers,
66
+ num_heads=num_heads,
67
+ head_dim=head_dim,
68
+ block_size=block_size,
69
+ num_blocks=num_blocks,
70
+ dtype=torch.float16, # Force fp16 for FlashAttention
71
+ )
72
+ self.kv_manager.wire_kv_cache_to_model(model)
73
+ self.sampler = Sampler()
74
+ self.current_sequences = []
75
+ self.graphs = {}
76
+ self.graph_vars = None
77
+ self.graph_pool = None
78
+ self.graph_captured = False
79
+
80
+ def _prepare_prefill(self, requests: List[Seq]):
81
+ input_ids = []
82
+ positions = []
83
+ cu_seqlens_q = [0]
84
+ cu_seqlens_k = [0]
85
+ max_seqlen_q = 0
86
+ max_seqlen_k = 0
87
+ slot_mapping = []
88
+
89
+ for req in requests:
90
+ seqlen = len(req)
91
+ input_ids.extend(req[req.num_cached_tokens :])
92
+ positions.extend(list(range(req.num_cached_tokens, seqlen)))
93
+ seqlen_q = seqlen - req.num_cached_tokens
94
+ seqlen_k = seqlen
95
+ cu_seqlens_q.append(cu_seqlens_q[-1] + seqlen_q)
96
+ cu_seqlens_k.append(cu_seqlens_k[-1] + seqlen_k)
97
+ max_seqlen_q = max(seqlen_q, max_seqlen_q)
98
+ max_seqlen_k = max(seqlen_k, max_seqlen_k)
99
+
100
+ if req.block_table:
101
+ for i in range(req.num_cached_blocks, req.num_blocks):
102
+ block_id = req.block_table[i]
103
+ start = block_id * self.block_size
104
+ if i != req.num_blocks - 1:
105
+ end = start + self.block_size
106
+ else:
107
+ end = start + req.last_block_num_tokens
108
+ slot_mapping.extend(list(range(start, end)))
109
+
110
+ input_ids = torch.tensor(input_ids, dtype=torch.int64, pin_memory=True).cuda(
111
+ non_blocking=True
112
+ )
113
+ positions = torch.tensor(positions, dtype=torch.int64, pin_memory=True).cuda(
114
+ non_blocking=True
115
+ )
116
+ cu_seqlens_q = torch.tensor(
117
+ cu_seqlens_q, dtype=torch.int32, pin_memory=True
118
+ ).cuda(non_blocking=True)
119
+ cu_seqlens_k = torch.tensor(
120
+ cu_seqlens_k, dtype=torch.int32, pin_memory=True
121
+ ).cuda(non_blocking=True)
122
+ slot_mapping = torch.tensor(
123
+ slot_mapping, dtype=torch.int32, pin_memory=True
124
+ ).cuda(non_blocking=True)
125
+
126
+ block_tables = None
127
+ if cu_seqlens_k[-1] > cu_seqlens_q[-1]:
128
+ max_len = max(len(req.block_table) for req in requests)
129
+ block_tables_list = []
130
+ for req in requests:
131
+ table = req.block_table + [-1] * (max_len - len(req.block_table))
132
+ block_tables_list.append(table)
133
+ block_tables = torch.tensor(
134
+ block_tables_list, dtype=torch.int32, pin_memory=True
135
+ ).cuda(non_blocking=True)
136
+
137
+ set_forward_context(
138
+ True,
139
+ cu_seqlens_q,
140
+ cu_seqlens_k,
141
+ max_seqlen_q,
142
+ max_seqlen_k,
143
+ slot_mapping,
144
+ None,
145
+ block_tables,
146
+ )
147
+
148
+ return input_ids, positions
149
+
150
+ def _prepare_decode(self, requests: List[Seq]):
151
+ if not requests:
152
+ raise RuntimeError("FATAL: No requests provided to _prepare_decode!")
153
+
154
+ input_ids = []
155
+ positions = []
156
+ slot_mapping = []
157
+ context_lens = []
158
+
159
+ for req in requests:
160
+ input_ids.append(req.last_token)
161
+
162
+ pos = len(req) - 1
163
+ if hasattr(self, "_tts_mode") and self._tts_mode:
164
+ pos = pos - (self._tts_prompt_len - 1)
165
+ positions.append(pos)
166
+
167
+ context_lens.append(len(req))
168
+ slot_mapping.append(
169
+ req.block_table[-1] * self.block_size + req.last_block_num_tokens - 1
170
+ )
171
+
172
+ input_ids = torch.tensor(input_ids, dtype=torch.int64, pin_memory=True).cuda(
173
+ non_blocking=True
174
+ )
175
+ positions = torch.tensor(positions, dtype=torch.int64, pin_memory=True).cuda(
176
+ non_blocking=True
177
+ )
178
+ slot_mapping = torch.tensor(
179
+ slot_mapping, dtype=torch.int32, pin_memory=True
180
+ ).cuda(non_blocking=True)
181
+ context_lens = torch.tensor(
182
+ context_lens, dtype=torch.int32, pin_memory=True
183
+ ).cuda(non_blocking=True)
184
+
185
+ max_len = max(len(req.block_table) for req in requests)
186
+ block_tables_list = []
187
+ for req in requests:
188
+ table = req.block_table + [-1] * (max_len - len(req.block_table))
189
+ block_tables_list.append(table)
190
+ block_tables = torch.tensor(
191
+ block_tables_list, dtype=torch.int32, pin_memory=True
192
+ ).cuda(non_blocking=True)
193
+
194
+ assert block_tables.dim() == 2, (
195
+ f"block_tables must be 2D, got shape {block_tables.shape}"
196
+ )
197
+ assert block_tables.size(0) == len(requests), (
198
+ f"block_tables batch size mismatch: {block_tables.size(0)} vs {len(requests)}"
199
+ )
200
+
201
+ set_forward_context(
202
+ False,
203
+ slot_mapping=slot_mapping,
204
+ context_lens=context_lens,
205
+ block_tables=block_tables,
206
+ )
207
+
208
+ return input_ids, positions
209
+
210
+ def _prepare_sample(self, requests: List[Seq], temperature: float):
211
+ temperatures = [temperature] * len(requests)
212
+ temperatures = torch.tensor(
213
+ temperatures, dtype=torch.float32, pin_memory=True
214
+ ).cuda(non_blocking=True)
215
+ return temperatures
216
+
217
+ @torch.inference_mode()
218
+ def _capture_cuda_graphs(self, tts_mel_embedding=None, tts_text_pos_embedding=None):
219
+ print("Capturing CUDA graphs for decode optimization...")
220
+ max_bs = 8 # Support up to batch size 8
221
+ max_num_blocks = (2048 + self.block_size - 1) // self.block_size
222
+ model_dtype = next(self.model.parameters()).dtype
223
+ input_ids = torch.ones(max_bs, dtype=torch.int64, device="cuda") * 8192
224
+ positions = torch.ones(max_bs, dtype=torch.int64, device="cuda")
225
+ slot_mapping = torch.zeros(max_bs, dtype=torch.int32, device="cuda")
226
+ context_lens = torch.zeros(max_bs, dtype=torch.int32, device="cuda")
227
+ block_tables = torch.zeros(
228
+ max_bs, max_num_blocks, dtype=torch.int32, device="cuda"
229
+ )
230
+ outputs = torch.zeros(
231
+ max_bs, self.hidden_size, dtype=model_dtype, device="cuda"
232
+ )
233
+ inputs_embeds_buffer = torch.zeros(
234
+ max_bs, self.hidden_size, dtype=model_dtype, device="cuda"
235
+ )
236
+
237
+ self.graph_bs = [1]
238
+
239
+ use_tts = tts_mel_embedding is not None and tts_text_pos_embedding is not None
240
+
241
+ for bs in reversed(self.graph_bs):
242
+ graph = torch.cuda.CUDAGraph()
243
+
244
+ slot_mapping[:bs] = torch.arange(bs, dtype=torch.int32, device="cuda")
245
+ context_lens[:bs] = bs + 1
246
+ block_tables[:bs, 0] = 0
247
+
248
+ set_forward_context(
249
+ False,
250
+ slot_mapping=slot_mapping[:bs],
251
+ context_lens=context_lens[:bs],
252
+ block_tables=block_tables[:bs],
253
+ )
254
+
255
+ # warmup
256
+ if use_tts:
257
+ assert tts_mel_embedding is not None
258
+ assert tts_text_pos_embedding is not None
259
+ emb = tts_mel_embedding(input_ids[:bs])
260
+ pos_clamped = torch.clamp(positions[:bs], min=0)
261
+ pos_emb = tts_text_pos_embedding.emb(pos_clamped)
262
+ inputs_embeds_buffer[:bs] = emb + pos_emb
263
+ out = self.model(
264
+ inputs_embeds=inputs_embeds_buffer[:bs].unsqueeze(1),
265
+ return_dict=True,
266
+ ).last_hidden_state
267
+ else:
268
+ out = self.model(
269
+ input_ids=input_ids[:bs].unsqueeze(1), return_dict=True
270
+ ).last_hidden_state
271
+ outputs[:bs] = out.squeeze(1) if out.dim() == 3 else out
272
+
273
+ with torch.cuda.graph(graph, self.graph_pool):
274
+ if use_tts:
275
+ assert tts_mel_embedding is not None
276
+ assert tts_text_pos_embedding is not None
277
+ emb = tts_mel_embedding(input_ids[:bs])
278
+ pos_clamped = torch.clamp(positions[:bs], min=0)
279
+ pos_emb = tts_text_pos_embedding.emb(pos_clamped)
280
+ inputs_embeds_buffer[:bs] = emb + pos_emb
281
+ out = self.model(
282
+ inputs_embeds=inputs_embeds_buffer[:bs].unsqueeze(1),
283
+ return_dict=True,
284
+ ).last_hidden_state
285
+ else:
286
+ out = self.model(
287
+ input_ids=input_ids[:bs].unsqueeze(1), return_dict=True
288
+ ).last_hidden_state
289
+ outputs[:bs] = out.squeeze(1) if out.dim() == 3 else out
290
+
291
+ if self.graph_pool is None:
292
+ self.graph_pool = graph.pool()
293
+
294
+ self.graphs[bs] = graph
295
+ torch.cuda.synchronize()
296
+ reset_forward_context()
297
+
298
+ self.graph_vars = {
299
+ "input_ids": input_ids,
300
+ "positions": positions,
301
+ "slot_mapping": slot_mapping,
302
+ "context_lens": context_lens,
303
+ "block_tables": block_tables,
304
+ "outputs": outputs,
305
+ "inputs_embeds": inputs_embeds_buffer,
306
+ }
307
+ print(f"CUDA graphs captured for batch sizes: {self.graph_bs}")
308
+
309
+ @torch.inference_mode()
310
+ def _run_decode_with_graph(
311
+ self,
312
+ input_ids: torch.Tensor,
313
+ positions: torch.Tensor,
314
+ context: ForwardContext,
315
+ tts_mel_embedding: Optional[torch.nn.Module] = None,
316
+ tts_text_pos_embedding: Optional[torch.nn.Module] = None,
317
+ ) -> torch.Tensor:
318
+ bs = input_ids.size(0)
319
+ use_tts_embedding = hasattr(self, "_tts_mode") and self._tts_mode
320
+
321
+ if not self.use_cuda_graph or not self.graphs:
322
+ if use_tts_embedding:
323
+ assert tts_mel_embedding is not None
324
+ assert tts_text_pos_embedding is not None
325
+ inputs_embeds = tts_mel_embedding(input_ids)
326
+ pos_clamped = torch.clamp(positions, min=0)
327
+ pos_emb = tts_text_pos_embedding.emb(pos_clamped)
328
+ inputs_embeds = inputs_embeds + pos_emb
329
+ out = self.model(
330
+ inputs_embeds=inputs_embeds.unsqueeze(1), return_dict=True
331
+ ).last_hidden_state
332
+ else:
333
+ out = self.model(
334
+ input_ids=input_ids.unsqueeze(1), return_dict=True
335
+ ).last_hidden_state
336
+ return out.squeeze(1) if out.dim() == 3 else out
337
+
338
+ graph_bs = next((x for x in self.graph_bs if x >= bs), None)
339
+ if graph_bs is None:
340
+ if use_tts_embedding:
341
+ assert tts_mel_embedding is not None
342
+ assert tts_text_pos_embedding is not None
343
+ inputs_embeds = tts_mel_embedding(input_ids)
344
+ pos_clamped = torch.clamp(positions, min=0)
345
+ pos_emb = tts_text_pos_embedding.emb(pos_clamped)
346
+ inputs_embeds = inputs_embeds + pos_emb
347
+ out = self.model(
348
+ inputs_embeds=inputs_embeds.unsqueeze(1), return_dict=True
349
+ ).last_hidden_state
350
+ else:
351
+ out = self.model(
352
+ input_ids=input_ids.unsqueeze(1), return_dict=True
353
+ ).last_hidden_state
354
+ return out.squeeze(1) if out.dim() == 3 else out
355
+
356
+ graph = self.graphs[graph_bs]
357
+ graph_vars = self.graph_vars
358
+
359
+ if graph_vars is None:
360
+ raise RuntimeError("Graph variables not initialized")
361
+
362
+ set_forward_context(
363
+ False,
364
+ slot_mapping=graph_vars["slot_mapping"][:graph_bs],
365
+ context_lens=graph_vars["context_lens"][:graph_bs],
366
+ block_tables=graph_vars["block_tables"][:graph_bs],
367
+ )
368
+
369
+ graph_vars["input_ids"][:bs] = input_ids
370
+ graph_vars["positions"][:bs] = positions
371
+ graph_vars["slot_mapping"].fill_(-1)
372
+ graph_vars["slot_mapping"][:bs] = context.slot_mapping
373
+ graph_vars["context_lens"].zero_()
374
+ graph_vars["context_lens"][:bs] = context.context_lens
375
+ graph_vars["block_tables"][:bs, : context.block_tables.size(1)] = (
376
+ context.block_tables
377
+ )
378
+ graph.replay()
379
+
380
+ return graph_vars["outputs"][:bs]
381
+
382
+ @torch.inference_mode()
383
+ def generate(
384
+ self,
385
+ input_ids: torch.Tensor,
386
+ max_new_tokens: int = 100,
387
+ temperature: float = 1.0,
388
+ top_k: int = 50,
389
+ top_p: float = 1.0,
390
+ stop_tokens: Optional[List[int]] = None,
391
+ attention_mask: Optional[torch.Tensor] = None,
392
+ tts_embeddings: Optional[
393
+ torch.Tensor
394
+ ] = None, # TTS: [pad][cond][text] embeddings (87 tokens, NO start_mel)
395
+ tts_mel_embedding: Optional[torch.nn.Module] = None, # TTS: mel_embedding layer
396
+ tts_text_pos_embedding: Optional[
397
+ torch.nn.Module
398
+ ] = None, # TTS: text_pos_embedding layer
399
+ ) -> torch.Tensor:
400
+ """
401
+ Generate tokens.
402
+
403
+ Args:
404
+ input_ids: Input token IDs [batch_size, seq_len]
405
+ max_new_tokens: Maximum number of tokens to generate
406
+ temperature: Sampling temperature
407
+ top_k: Top-k sampling
408
+ top_p: Nucleus sampling threshold
409
+ stop_tokens: List of token IDs that stop generation
410
+
411
+ Returns:
412
+ Generated token IDs [batch_size, total_len]
413
+ """
414
+ batch_size = input_ids.size(0)
415
+ device = input_ids.device
416
+
417
+ self._tts_mode = tts_embeddings is not None
418
+ self._tts_prompt_len = input_ids.size(1) if self._tts_mode else 0
419
+
420
+ if self.use_cuda_graph and not self.graph_captured:
421
+ print(
422
+ f"[CAPTURE] use_cuda_graph={self.use_cuda_graph}, graph_captured={self.graph_captured}",
423
+ file=sys.stderr,
424
+ flush=True,
425
+ )
426
+ self._capture_cuda_graphs(
427
+ tts_mel_embedding=tts_mel_embedding,
428
+ tts_text_pos_embedding=tts_text_pos_embedding,
429
+ )
430
+ self.graph_captured = True
431
+ print(
432
+ f"[CAPTURE] Completed! graphs={list(self.graphs.keys())}",
433
+ file=sys.stderr,
434
+ flush=True,
435
+ )
436
+
437
+ if tts_embeddings is not None:
438
+ actual_seq_len = tts_embeddings.size(1) + 1 # embeddings + start_mel_token
439
+ pass
440
+ else:
441
+ actual_seq_len = input_ids.size(1)
442
+
443
+ sequences = []
444
+ for i in range(batch_size):
445
+ token_ids = [1] * actual_seq_len
446
+ if tts_embeddings is not None and actual_seq_len > 0:
447
+ token_ids[-1] = input_ids[i, -1].item() if input_ids.size(1) > 0 else 1
448
+ else:
449
+ token_ids = input_ids[i].tolist()
450
+ req = Seq(token_ids)
451
+ self.kv_manager.allocate(req)
452
+ sequences.append(req)
453
+
454
+ self.current_sequences = sequences
455
+
456
+ # Prefill phase
457
+ prefill_ids, prefill_pos = self._prepare_prefill(sequences)
458
+
459
+ if prefill_ids.dim() == 1:
460
+ prefill_ids = prefill_ids.unsqueeze(
461
+ 0
462
+ ) # [total_tokens] -> [1, total_tokens]
463
+ if prefill_pos.dim() == 1:
464
+ prefill_pos = prefill_pos.unsqueeze(
465
+ 0
466
+ ) # [total_tokens] -> [1, total_tokens]
467
+
468
+ if (
469
+ tts_embeddings is not None
470
+ and tts_mel_embedding is not None
471
+ and tts_text_pos_embedding is not None
472
+ ):
473
+ start_token_id = input_ids[0, -1] if input_ids.size(1) > 0 else 8192
474
+
475
+ start_emb = tts_mel_embedding(
476
+ torch.tensor([[start_token_id]], device="cuda")
477
+ ) # [1, 1, hidden_dim]
478
+
479
+ start_emb = start_emb + tts_text_pos_embedding(start_emb)
480
+
481
+ full_embeddings = torch.cat(
482
+ [tts_embeddings, start_emb], dim=1
483
+ ) # [1, 88, hidden_dim]
484
+
485
+ model_dtype = next(self.model.parameters()).dtype
486
+ if full_embeddings.dtype != model_dtype:
487
+ full_embeddings = full_embeddings.to(model_dtype)
488
+
489
+ hidden_states = self.model(
490
+ inputs_embeds=full_embeddings, return_dict=True
491
+ ).last_hidden_state
492
+
493
+ else:
494
+ hidden_states = self.model(
495
+ input_ids=input_ids, attention_mask=attention_mask, return_dict=True
496
+ ).last_hidden_state
497
+
498
+ reset_forward_context()
499
+
500
+ last_hidden = hidden_states[:, -1, :] # [batch_size, hidden_size]
501
+
502
+ if self.lm_head is not None:
503
+ if last_hidden.dtype != next(self.lm_head.parameters()).dtype:
504
+ last_hidden = last_hidden.to(next(self.lm_head.parameters()).dtype)
505
+ logits = self.lm_head(last_hidden) # [batch_size, vocab_size]
506
+ else:
507
+ logits = self.model.compute_logits(last_hidden) # [batch_size, vocab_size]
508
+
509
+ temperatures = self._prepare_sample(sequences, temperature)
510
+ if temperature > 0:
511
+ first_token = self.sampler(logits, temperatures)
512
+ else:
513
+ first_token = torch.argmax(logits, dim=-1)
514
+
515
+ first_token_list = first_token.tolist()
516
+
517
+ generated_tokens = [[] for _ in range(batch_size)]
518
+ hit_stop_on_first = False
519
+
520
+ for i, token_id in enumerate(first_token_list):
521
+ if stop_tokens and token_id in stop_tokens:
522
+ hit_stop_on_first = True
523
+ else:
524
+ generated_tokens[i].append(token_id)
525
+
526
+ if hit_stop_on_first:
527
+ for req in sequences:
528
+ self.kv_manager.remove_seq(req)
529
+ self.current_sequences = []
530
+
531
+ output_ids = []
532
+ for i in range(batch_size):
533
+ full_sequence = input_ids[i].tolist() + generated_tokens[i]
534
+ output_ids.append(full_sequence)
535
+
536
+ output = torch.tensor(output_ids, dtype=torch.long, device=device)
537
+ return output
538
+
539
+ if not hit_stop_on_first:
540
+ for i, req in enumerate(sequences):
541
+ req.append_token(first_token_list[i])
542
+ self.kv_manager.append_to_seq(req)
543
+
544
+ remaining_tokens = max_new_tokens - 1
545
+
546
+ for step in range(remaining_tokens):
547
+ decode_ids, decode_pos = self._prepare_decode(sequences)
548
+
549
+ # Forward pass
550
+ if batch_size > 8:
551
+ raise RuntimeError(
552
+ f"FATAL: batch_size={batch_size} exceeds CUDA Graph limit (8)!"
553
+ )
554
+
555
+ context = get_forward_context()
556
+ hidden_states = self._run_decode_with_graph(
557
+ decode_ids,
558
+ decode_pos,
559
+ context,
560
+ tts_mel_embedding=tts_mel_embedding,
561
+ tts_text_pos_embedding=tts_text_pos_embedding,
562
+ )
563
+
564
+ # Get logits
565
+ if self.lm_head is not None:
566
+ logits = self.lm_head(hidden_states) # [batch_size, vocab_size]
567
+ else:
568
+ logits = self.model.compute_logits(
569
+ hidden_states
570
+ ) # [batch_size, vocab_size]
571
+
572
+ reset_forward_context()
573
+
574
+ temperatures = self._prepare_sample(sequences, temperature)
575
+ if temperature > 0:
576
+ next_token = self.sampler(logits, temperatures)
577
+ else:
578
+ next_token = torch.argmax(logits, dim=-1)
579
+ next_token_list = next_token.tolist()
580
+
581
+ should_stop = False
582
+ for i, token_id in enumerate(next_token_list):
583
+ if stop_tokens and token_id in stop_tokens:
584
+ should_stop = True
585
+ else:
586
+ sequences[i].append_token(token_id)
587
+ self.kv_manager.append_to_seq(sequences[i])
588
+ generated_tokens[i].append(token_id)
589
+
590
+ if should_stop:
591
+ break
592
+
593
+ for req in sequences:
594
+ self.kv_manager.remove_seq(req)
595
+ self.current_sequences = []
596
+
597
+ output_ids = []
598
+ for i in range(batch_size):
599
+ initial_tokens = sequences[i].token_ids[: sequences[i].num_prompt_tokens]
600
+ full_sequence = initial_tokens + generated_tokens[i]
601
+ output_ids.append(full_sequence)
602
+
603
+ output = torch.tensor(output_ids, dtype=torch.long, device=device)
604
+
605
+ assert output.size(0) == batch_size, (
606
+ f"Output batch size mismatch: {output.size(0)} != {batch_size}"
607
+ )
608
+
609
+ return output