harp-dev commited on
Commit
e3ba266
·
verified ·
1 Parent(s): 4f08e87

Run ACE-Step here instead of forwarding to the authors' Space.

Browse files
.harp/manifest.json DELETED
@@ -1,38 +0,0 @@
1
- {
2
- "backend_space": "ACE-Step/ACE-Step",
3
- "deploy_mode": "remote-backend",
4
- "entry": "app.py",
5
- "framework": "gradio_client",
6
- "generated": true,
7
- "io": {
8
- "inputs": [
9
- "slider",
10
- "textbox",
11
- "textbox",
12
- "slider",
13
- "slider",
14
- "slider",
15
- "textbox",
16
- "slider",
17
- "slider",
18
- "slider",
19
- "checkbox",
20
- "checkbox",
21
- "checkbox",
22
- "textbox",
23
- "slider",
24
- "slider",
25
- "checkbox",
26
- "slider",
27
- "audio",
28
- "dropdown"
29
- ],
30
- "outputs": [
31
- "audio"
32
- ]
33
- },
34
- "repo_id": "ACE-Step/ACE-Step",
35
- "source": "recipe",
36
- "space_layout": "huggingface-gradio",
37
- "task": "custom"
38
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
LICENSE ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [2025] Timedomain Inc. and stepfun
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
README.md CHANGED
@@ -4,22 +4,27 @@ emoji: 🎼
4
  colorFrom: purple
5
  colorTo: pink
6
  sdk: gradio
7
- sdk_version: 6.24.0
 
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
  short_description: Text-to-music generation with lyrics and style control
 
 
 
12
  tags:
13
  - category:generation
14
  - subcategory:holistic
15
  - input:text
16
  - input:audio
17
  - output:audio
18
- - zerogpu
19
  ---
20
 
21
- Text-to-music generation with lyrics and style control, using [ACE-Step](https://github.com/ace-step/ACE-Step), accessible in HARP.
 
22
 
23
- ## Paused
24
-
25
- This Space forwards inference to a Space outside the organization, so a request depends on a deployment we do not control and spends an allowance we cannot see. It is paused until the model either runs here or forwards to a backend hosted under `teamup-tech`. See the "Calling Another Space" section of HARP's deployment guidelines.
 
 
4
  colorFrom: purple
5
  colorTo: pink
6
  sdk: gradio
7
+ sdk_version: 6.17.3
8
+ python_version: "3.10"
9
  app_file: app.py
10
  pinned: false
11
  license: apache-2.0
12
  short_description: Text-to-music generation with lyrics and style control
13
+ preload_from_hub:
14
+ - ACE-Step/ACE-Step-v1-3.5B
15
+ - ACE-Step/ACE-Step-v1-chinese-rap-LoRA
16
  tags:
17
  - category:generation
18
  - subcategory:holistic
19
  - input:text
20
  - input:audio
21
  - output:audio
 
22
  ---
23
 
24
+ Text-to-music generation with lyrics and style control, using
25
+ [ACE-Step](https://github.com/ace-step/ACE-Step), accessible in HARP.
26
 
27
+ The pipeline is vendored from the authors' Space,
28
+ [ACE-Step/ACE-Step](https://huggingface.co/spaces/ACE-Step/ACE-Step), under its Apache 2.0 license
29
+ (see `LICENSE`), and runs here on ZeroGPU. Gradio is held at 6.17.3, the newest release whose
30
+ `huggingface-hub` floor admits the `transformers` 4.50 the pipeline needs.
apg_guidance.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+
4
+ class MomentumBuffer:
5
+ def __init__(self, momentum: float = -0.75):
6
+ self.momentum = momentum
7
+ self.running_average = 0
8
+
9
+ def update(self, update_value: torch.Tensor):
10
+ new_average = self.momentum * self.running_average
11
+ self.running_average = update_value + new_average
12
+
13
+
14
+ def project(
15
+ v0: torch.Tensor, # [B, C, H, W]
16
+ v1: torch.Tensor, # [B, C, H, W]
17
+ dims=[-1, -2],
18
+ ):
19
+ dtype = v0.dtype
20
+ device_type = v0.device.type
21
+ if device_type == "mps":
22
+ v0, v1 = v0.cpu(), v1.cpu()
23
+
24
+ v0, v1 = v0.double(), v1.double()
25
+ v1 = torch.nn.functional.normalize(v1, dim=dims)
26
+ v0_parallel = (v0 * v1).sum(dim=dims, keepdim=True) * v1
27
+ v0_orthogonal = v0 - v0_parallel
28
+ return v0_parallel.to(dtype).to(device_type), v0_orthogonal.to(dtype).to(device_type)
29
+
30
+
31
+ def apg_forward(
32
+ pred_cond: torch.Tensor, # [B, C, H, W]
33
+ pred_uncond: torch.Tensor, # [B, C, H, W]
34
+ guidance_scale: float,
35
+ momentum_buffer: MomentumBuffer = None,
36
+ eta: float = 0.0,
37
+ norm_threshold: float = 2.5,
38
+ dims=[-1, -2],
39
+ ):
40
+ diff = pred_cond - pred_uncond
41
+ if momentum_buffer is not None:
42
+ momentum_buffer.update(diff)
43
+ diff = momentum_buffer.running_average
44
+
45
+ if norm_threshold > 0:
46
+ ones = torch.ones_like(diff)
47
+ diff_norm = diff.norm(p=2, dim=dims, keepdim=True)
48
+ scale_factor = torch.minimum(ones, norm_threshold / diff_norm)
49
+ diff = diff * scale_factor
50
+
51
+ diff_parallel, diff_orthogonal = project(diff, pred_cond, dims)
52
+ normalized_update = diff_orthogonal + eta * diff_parallel
53
+ pred_guided = pred_cond + (guidance_scale - 1) * normalized_update
54
+ return pred_guided
55
+
56
+
57
+ def cfg_forward(cond_output, uncond_output, cfg_strength):
58
+ return uncond_output + cfg_strength * (cond_output - uncond_output)
59
+
60
+
61
+ def cfg_double_condition_forward(
62
+ cond_output,
63
+ uncond_output,
64
+ only_text_cond_output,
65
+ guidance_scale_text,
66
+ guidance_scale_lyric,
67
+ ):
68
+ return (1 - guidance_scale_text) * uncond_output + (guidance_scale_text - guidance_scale_lyric) * only_text_cond_output + guidance_scale_lyric * cond_output
69
+
70
+
71
+ def optimized_scale(positive_flat, negative_flat):
72
+
73
+ # Calculate dot production
74
+ dot_product = torch.sum(positive_flat * negative_flat, dim=1, keepdim=True)
75
+
76
+ # Squared norm of uncondition
77
+ squared_norm = torch.sum(negative_flat ** 2, dim=1, keepdim=True) + 1e-8
78
+
79
+ # st_star = v_cond^T * v_uncond / ||v_uncond||^2
80
+ st_star = dot_product / squared_norm
81
+
82
+ return st_star
83
+
84
+
85
+ def cfg_zero_star(noise_pred_with_cond, noise_pred_uncond, guidance_scale, i, zero_steps=1, use_zero_init=True):
86
+ bsz = noise_pred_with_cond.shape[0]
87
+ positive_flat = noise_pred_with_cond.view(bsz, -1)
88
+ negative_flat = noise_pred_uncond.view(bsz, -1)
89
+ alpha = optimized_scale(positive_flat, negative_flat)
90
+ alpha = alpha.view(bsz, 1, 1, 1)
91
+ if (i <= zero_steps) and use_zero_init:
92
+ noise_pred = noise_pred_with_cond * 0.
93
+ else:
94
+ noise_pred = noise_pred_uncond * alpha + guidance_scale * (noise_pred_with_cond - noise_pred_uncond * alpha)
95
+ return noise_pred
app.py CHANGED
@@ -1,199 +1,86 @@
1
- from __future__ import annotations
 
2
 
3
- import inspect
4
- import os
5
- import time
6
- import urllib.request
 
7
 
8
  import gradio as gr
 
9
 
10
  from pyharp import *
11
- from gradio_client import Client, handle_file
12
-
13
-
14
- _TOKEN_ARG = ("token" if "token" in inspect.signature(Client.__init__).parameters
15
- else "hf_token")
16
-
17
- _BACKEND_SPACE = "ACE-Step/ACE-Step"
18
- _BACKEND_API_NAME = "/__call__"
19
- _BACKEND_TOKEN_ENV = "HF_TOKEN"
20
- _ACCEPT_USER_TOKEN = False
21
- # How many times to wake+retry a sleeping backend, and how long to wait for
22
- # it to boot (a free Space cold start can take a few minutes).
23
- _CALL_RETRIES = int(os.environ.get("BACKEND_CALL_RETRIES", "4"))
24
- _WAKE_TIMEOUT = float(os.environ.get("BACKEND_WAKE_TIMEOUT", "420"))
25
- _client = None
26
-
27
-
28
- def _backend_client():
29
- # Lazily create and cache one warm connection using this Space's own
30
- # token (from the HF_TOKEN secret) or anonymous if none is set. User
31
- # tokens are NOT cached here -- they get a fresh per-call connection.
32
- global _client
33
- if _client is None:
34
- _token = os.environ.get(_BACKEND_TOKEN_ENV) or None
35
- _client = Client(_BACKEND_SPACE, **{_TOKEN_ARG: _token})
36
- return _client
37
-
38
-
39
- def _reset_client():
40
- # Drop the cached connection so the next attempt reconnects to a Space
41
- # that has since finished waking.
42
- global _client
43
- _client = None
44
-
45
-
46
- def _make_conn(tok):
47
- tok = (tok or '').strip()
48
- if tok:
49
- return Client(_BACKEND_SPACE, **{_TOKEN_ARG: tok})
50
- return _backend_client()
51
-
52
-
53
- def _space_url(space):
54
- slug = space.strip().lower().replace('/', '-').replace('_', '-')
55
- return f'https://{slug}.hf.space/'
56
-
57
-
58
- def _is_cold_start(message):
59
- # Errors that mean 'the backend was asleep/booting', worth waking+retrying
60
- # (vs. a real application error, which we surface immediately).
61
- _low = (message or '').lower()
62
- return any(s in _low for s in (
63
- 'read operation timed out', 'timed out', 'timeout', 'starting',
64
- 'building', 'not ready', 'no application', 'connection', '503', '502',
65
- ))
66
-
67
-
68
- def _wake_backend():
69
- # A sleeping Space boots when its URL is hit; poll until it answers (or
70
- # the budget expires) so the retried call lands on a running backend.
71
- _url = _space_url(_BACKEND_SPACE)
72
- _deadline = time.time() + _WAKE_TIMEOUT
73
- _delay = 5.0
74
- while time.time() < _deadline:
75
- try:
76
- _req = urllib.request.Request(_url, headers={'User-Agent': 'harp-frontend'})
77
- with urllib.request.urlopen(_req, timeout=30) as _resp:
78
- if getattr(_resp, 'status', 200) < 500:
79
- return True
80
- except Exception:
81
- pass
82
- time.sleep(_delay)
83
- _delay = min(_delay * 1.5, 30.0)
84
- return False
85
-
86
-
87
- def _quota_hint(message):
88
- # Turn a backend error into an actionable message.
89
- # NOTE: 'message' is the backend's error text; it never contains our token.
90
- _low = (message or "").lower()
91
- if "quota" in _low or "zerogpu" in _low:
92
- if _ACCEPT_USER_TOKEN:
93
- return (
94
- "The backend's ZeroGPU quota is exhausted for the identity making "
95
- "this call. Paste your own Hugging Face token in the token field "
96
- "(read scope) so usage is attributed to your account."
97
- )
98
- return (
99
- "The backend's ZeroGPU quota is exhausted. This Space's calls are "
100
- "anonymous unless an HF_TOKEN secret is set (Settings -> Variables "
101
- "and secrets); use a token from a PRO account or a ZeroGPU-enabled org."
102
- )
103
- # Opaque backend failure: the Space raised an exception it refuses to
104
- # expose (it runs with show_error=False), so all we get is a generic
105
- # 'Internal Gradio error'. The frontend can't fix a server-side crash --
106
- # point the user at where the real cause lives.
107
- if (
108
- "internal gradio error" in _low
109
- or "internal server error" in _low
110
- or "apperror" in _low
111
- or _low.strip() in ("", "none")
112
- ):
113
- _hint = (
114
- "The backend Space raised an error it did not expose (it runs with "
115
- "show_error disabled), so the real cause is only in the backend "
116
- "Space's Logs tab (" + _BACKEND_SPACE + ")."
117
- )
118
- if _ACCEPT_USER_TOKEN:
119
- _hint += (
120
- " If it is a ZeroGPU Space, an anonymous call can fail this way -- "
121
- "paste a Hugging Face token in the token field and retry."
122
- )
123
- return _hint
124
- return message or "Backend call failed."
125
 
126
 
127
  model_card = ModelCard(
128
  name="ACE-Step",
129
  description=(
130
- "Generates a complete song, vocals and backing together, from style "
131
- "tags and lyrics. A diffusion transformer runs over a heavily "
132
- "compressed audio representation, which is what lets it render minutes "
133
- "of music in seconds instead of one token at a time. Tags set the "
134
- "genre, instrumentation and mood, and the lyrics are sung. Turning on "
135
- "Audio2Audio conditions the result on a reference recording so it takes "
136
- "on that recording's character. Audio Duration sets the length, up to "
137
- "four minutes, or -1 to let the model choose. Inference runs on the "
138
- "authors' own Space (ACE-Step/ACE-Step) rather than here, so a request "
139
- "needs that Space's ZeroGPU allowance and queues behind everyone else "
140
- "calling it. Supplying your own Hugging Face token bills the call to "
141
- "your account instead. Repository: https://github.com/ace-step/ACE-Step "
142
  "Paper: https://arxiv.org/abs/2506.00045"
143
  ),
144
  author="Junmin Gong, Wenxiao Zhao, Sen Wang, Shengyuan Xu, Jing Guo",
145
- tags=[Subcategory.HOLISTIC, 'zerogpu'],
146
  )
147
 
148
 
149
- def process_fn(audio_duration, prompt, lyrics, infer_step, guidance_scale, omega_scale, manual_seeds, guidance_interval, guidance_interval_decay, min_guidance_scale, use_erg_tag, use_erg_lyric, use_erg_diffusion, oss_steps, guidance_scale_text, guidance_scale_lyric, audio2audio_enable, ref_audio_strength, ref_audio_input, lora_name_or_path):
150
- _tok = ''
151
- # Call the backend, waking it and retrying if it was asleep (a cold
152
- # start otherwise fails the first hit with 'read operation timed out').
153
- _raw = None
154
- for _attempt in range(_CALL_RETRIES + 1):
155
- try:
156
- _conn = _make_conn(_tok)
157
- _raw = _conn.predict(
158
- audio_duration,
159
- prompt,
160
- lyrics,
161
- infer_step,
162
- guidance_scale,
163
- 'euler',
164
- 'apg',
165
- omega_scale,
166
- manual_seeds,
167
- guidance_interval,
168
- guidance_interval_decay,
169
- min_guidance_scale,
170
- use_erg_tag,
171
- use_erg_lyric,
172
- use_erg_diffusion,
173
- oss_steps,
174
- guidance_scale_text,
175
- guidance_scale_lyric,
176
- audio2audio_enable,
177
- ref_audio_strength,
178
- (handle_file(ref_audio_input) if ref_audio_input else None),
179
- lora_name_or_path,
180
- api_name="/__call__",
181
- )
182
- break
183
- except Exception as _exc: # never surfaces the token
184
- if _attempt < _CALL_RETRIES and _is_cold_start(str(_exc)):
185
- _reset_client()
186
- _wake_backend()
187
- continue
188
- raise gr.Error(_quota_hint(str(_exc)))
189
- _values = list(_raw) if isinstance(_raw, (list, tuple)) else [_raw]
190
- _detail = " | ".join(str(_v) for _v in _values if isinstance(_v, str) and _v.strip())
191
- _out_text2music_generated_audio_1 = _values[0] if len(_values) > 0 else None
192
- if not _out_text2music_generated_audio_1:
193
- raise gr.Error(_detail or "The backend Space returned no 'text2music_generated_audio_1' output. Check the backend Space's logs; if it uses ZeroGPU it may need a moment to warm up.")
194
- return _out_text2music_generated_audio_1
195
-
196
-
197
  if __name__ == "__main__":
198
  with gr.Blocks() as demo:
199
  input_components = [
 
1
+ """
2
+ ACE-Step: generates a complete song, vocals and backing together, from style tags and lyrics.
3
 
4
+ The pipeline is vendored from the authors' Space (ACE-Step/ACE-Step), which installs nothing
5
+ from PyPI, and runs here on ZeroGPU rather than being called over the network.
6
+ """
7
+
8
+ import tempfile
9
 
10
  import gradio as gr
11
+ from huggingface_hub import snapshot_download
12
 
13
  from pyharp import *
14
+ from pipeline_ace_step import ACEStepPipeline
15
+
16
+ # Fetched when the Space builds (preload_from_hub in README.md), so this resolves the local copy.
17
+ # Its layout is the one the pipeline expects of a checkpoint directory.
18
+ CHECKPOINT_DIR = snapshot_download("ACE-Step/ACE-Step-v1-3.5B")
19
+
20
+ # Construction only records where the weights are. They load on the first call, inside the GPU
21
+ # reservation, which is how the authors' Space runs it as well.
22
+ pipeline = ACEStepPipeline(
23
+ checkpoint_dir=CHECKPOINT_DIR,
24
+ dtype="bfloat16",
25
+ persistent_storage_path=tempfile.gettempdir(),
26
+ torch_compile=False,
27
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
 
30
  model_card = ModelCard(
31
  name="ACE-Step",
32
  description=(
33
+ "Generates a complete song, vocals and backing together, from style tags and lyrics. A "
34
+ "diffusion transformer runs over a heavily compressed audio representation, which is what "
35
+ "lets it render minutes of music in seconds instead of one token at a time. Tags set the "
36
+ "genre, instrumentation and mood, and the lyrics are sung. Turning on Audio2Audio "
37
+ "conditions the result on a reference recording so it takes on that recording's "
38
+ "character. Audio Duration sets the length, up to four minutes, or -1 to let the model "
39
+ "choose. Repository: https://github.com/ace-step/ACE-Step "
 
 
 
 
 
40
  "Paper: https://arxiv.org/abs/2506.00045"
41
  ),
42
  author="Junmin Gong, Wenxiao Zhao, Sen Wang, Shengyuan Xu, Jing Guo",
43
+ tags=[Subcategory.HOLISTIC],
44
  )
45
 
46
 
47
+ def process_fn(audio_duration, prompt, lyrics, infer_step, guidance_scale, omega_scale,
48
+ manual_seeds, guidance_interval, guidance_interval_decay, min_guidance_scale,
49
+ use_erg_tag, use_erg_lyric, use_erg_diffusion, oss_steps, guidance_scale_text,
50
+ guidance_scale_lyric, audio2audio_enable, ref_audio_strength, ref_audio_input,
51
+ lora_name_or_path):
52
+ outputs = pipeline(
53
+ audio_duration=audio_duration,
54
+ prompt=prompt,
55
+ lyrics=lyrics,
56
+ infer_step=int(infer_step),
57
+ guidance_scale=guidance_scale,
58
+ # The scheduler and guidance type the authors' interface uses
59
+ scheduler_type="euler",
60
+ cfg_type="apg",
61
+ omega_scale=omega_scale,
62
+ manual_seeds=manual_seeds,
63
+ guidance_interval=guidance_interval,
64
+ guidance_interval_decay=guidance_interval_decay,
65
+ min_guidance_scale=min_guidance_scale,
66
+ use_erg_tag=use_erg_tag,
67
+ use_erg_lyric=use_erg_lyric,
68
+ use_erg_diffusion=use_erg_diffusion,
69
+ oss_steps=oss_steps,
70
+ guidance_scale_text=guidance_scale_text,
71
+ guidance_scale_lyric=guidance_scale_lyric,
72
+ audio2audio_enable=audio2audio_enable,
73
+ ref_audio_strength=ref_audio_strength,
74
+ ref_audio_input=ref_audio_input,
75
+ lora_name_or_path=lora_name_or_path,
76
+ save_path=tempfile.mkdtemp(),
77
+ )
78
+
79
+ # The audio it wrote, followed by a JSON record of the parameters used
80
+ return outputs[0]
81
+
82
+
83
+ # The processing worker imports this file, so the app must not be built there
 
 
 
 
 
 
 
 
 
 
 
84
  if __name__ == "__main__":
85
  with gr.Blocks() as demo:
86
  input_components = [
config/zh_rap_lora_config.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "r": 256,
3
+ "lora_alpha": 32,
4
+ "target_modules": [
5
+ "speaker_embedder",
6
+ "linear_q",
7
+ "linear_k",
8
+ "linear_v",
9
+ "to_q",
10
+ "to_k",
11
+ "to_v",
12
+ "to_out.0"
13
+ ],
14
+ "use_rslora": true
15
+ }
language_segmentation/LangSegment.py ADDED
@@ -0,0 +1,866 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This file bundles language identification functions.
3
+
4
+ Modifications (fork): Copyright (c) 2021, Adrien Barbaresi.
5
+
6
+ Original code: Copyright (c) 2011 Marco Lui <saffsd@gmail.com>.
7
+ Based on research by Marco Lui and Tim Baldwin.
8
+
9
+ See LICENSE file for more info.
10
+ https://github.com/adbar/py3langid
11
+
12
+ Projects:
13
+ https://github.com/juntaosun/LangSegment
14
+ """
15
+
16
+ import os
17
+ import re
18
+ import sys
19
+ import numpy as np
20
+ from collections import Counter
21
+ from collections import defaultdict
22
+
23
+ # import langid
24
+ # import py3langid as langid
25
+ # pip install py3langid==0.2.2
26
+
27
+ # 启用语言预测概率归一化,概率预测的分数。因此,实现重新规范化 产生 0-1 范围内的输出。
28
+ # langid disables probability normalization by default. For command-line usages of , it can be enabled by passing the flag.
29
+ # For probability normalization in library use, the user must instantiate their own . An example of such usage is as follows:
30
+ from py3langid.langid import LanguageIdentifier, MODEL_FILE
31
+
32
+ # Digital processing
33
+ try:from .utils.num import num2str
34
+ except ImportError:
35
+ try:from utils.num import num2str
36
+ except ImportError as e:
37
+ raise e
38
+
39
+ # -----------------------------------
40
+ # 更新日志:新版本分词更加精准。
41
+ # Changelog: The new version of the word segmentation is more accurate.
42
+ # チェンジログ:新しいバージョンの単語セグメンテーションはより正確です。
43
+ # Changelog: 분할이라는 단어의 새로운 버전이 더 정확합니다.
44
+ # -----------------------------------
45
+
46
+
47
+ # Word segmentation function:
48
+ # automatically identify and split the words (Chinese/English/Japanese/Korean) in the article or sentence according to different languages,
49
+ # making it more suitable for TTS processing.
50
+ # This code is designed for front-end text multi-lingual mixed annotation distinction, multi-language mixed training and inference of various TTS projects.
51
+ # This processing result is mainly for (Chinese = zh, Japanese = ja, English = en, Korean = ko), and can actually support up to 97 different language mixing processing.
52
+
53
+ #===========================================================================================================
54
+ #分かち書き機能:文章や文章の中の例えば(中国語/英語/日本語/韓国語)を、異なる言語で自動的に認識して分割し、TTS処理により適したものにします。
55
+ #このコードは、さまざまなTTSプロジェクトのフロントエンドテキストの多言語混合注釈区別、多言語混合トレーニング、および推論のために特別に作成されています。
56
+ #===========================================================================================================
57
+ #(1)自動分詞:「韓国語では何を読むのですかあなたの体育の先生は誰ですか?今回の発表会では、iPhone 15シリーズの4機種が登場しました」
58
+ #(2)手动分词:“あなたの名前は<ja>佐々木ですか?<ja>ですか?”
59
+ #この処理結果は主に(中国語=ja、日本語=ja、英語=en、韓国語=ko)を対象としており、実際には最大97の異なる言語の混合処理をサポートできます。
60
+ #===========================================================================================================
61
+
62
+ #===========================================================================================================
63
+ # 단어 분할 기능: 기사 또는 문장에서 단어(중국어/영어/일본어/한국어)를 다른 언어에 따라 자동으로 식별하고 분할하여 TTS 처리에 더 적합합니다.
64
+ # 이 코드는 프런트 엔드 텍스트 다국어 혼합 주석 분화, 다국어 혼합 교육 및 다양한 TTS 프로젝트의 추론을 위해 설계되었습니다.
65
+ #===========================================================================================================
66
+ # (1) 자동 단어 분할: "한국어로 무엇을 읽습니까? 스포츠 씨? 이 컨퍼런스는 4개의 iPhone 15 시리즈 모델을 제공합니다."
67
+ # (2) 수동 참여: "이름이 <ja>Saki입니까? <ja>?"
68
+ # 이 처리 결과는 주로 (중국어 = zh, 일본어 = ja, 영어 = en, 한국어 = ko)를 위한 것이며 실제로 혼합 처리를 위해 최대 97개의 언어를 지원합니다.
69
+ #===========================================================================================================
70
+
71
+ # ===========================================================================================================
72
+ # 分词功能:将文章或句子里的例如(中/英/日/韩),按不同语言自动识别并拆分,让它更适合TTS处理。
73
+ # 本代码专为各种 TTS 项目的前端文本多语种混合标注区分,多语言混合训练和推理而编写。
74
+ # ===========================================================================================================
75
+ # (1)自动分词:“韩语中的오빠读什么呢?あなたの体育の先生は誰ですか? 此次发布会带来了四款iPhone 15系列机型”
76
+ # (2)手动分词:“你的名字叫<ja>佐々木?<ja>吗?”
77
+ # 本处理结果主要针对(中文=zh , 日文=ja , 英文=en , 韩语=ko), 实际上可支持多达 97 种不同的语言混合处理。
78
+ # ===========================================================================================================
79
+
80
+
81
+ # 手动分词标签规范:<语言标签>文本内容</语言标签>
82
+ # 수동 단어 분할 태그 사양: <언어 태그> 텍스트 내용</언어 태그>
83
+ # Manual word segmentation tag specification: <language tags> text content </language tags>
84
+ # 手動分詞タグ仕様:<言語タグ>テキスト内容</言語タグ>
85
+ # ===========================================================================================================
86
+ # For manual word segmentation, labels need to appear in pairs, such as:
87
+ # 如需手动分词,标签需要成对出现,例如:“<ja>佐々木<ja>” 或者 “<ja>佐々木</ja>”
88
+ # 错误示范:“你的名字叫<ja>佐々木。” 此句子中出现的单个<ja>标签将被忽略,不会处理。
89
+ # Error demonstration: "Your name is <ja>佐々木。" Single <ja> tags that appear in this sentence will be ignored and will not be processed.
90
+ # ===========================================================================================================
91
+
92
+
93
+ # ===========================================================================================================
94
+ # 语音合成标记语言 SSML , 这里只支持它的标签(非 XML)Speech Synthesis Markup Language SSML, only its tags are supported here (not XML)
95
+ # 想支持更多的 SSML 标签?欢迎 PR! Want to support more SSML tags? PRs are welcome!
96
+ # 说明:除了中文以外,它也可改造成支持多语种 SSML ,不仅仅是中文。
97
+ # Note: In addition to Chinese, it can also be modified to support multi-language SSML, not just Chinese.
98
+ # ===========================================================================================================
99
+ # 中文实现:Chinese implementation:
100
+ # 【SSML】<number>=中文大写数字读法(单字)
101
+ # 【SSML】<telephone>=数字转成中文电话号码大写汉字(单字)
102
+ # 【SSML】<currency>=按金额发音。
103
+ # 【SSML】<date>=按日期发音。支持 2024年08月24, 2024/8/24, 2024-08, 08-24, 24 等输入。
104
+ # ===========================================================================================================
105
+ class LangSSML:
106
+
107
+ def __init__(self):
108
+ # 纯数字
109
+ self._zh_numerals_number = {
110
+ '0': '零',
111
+ '1': '一',
112
+ '2': '二',
113
+ '3': '三',
114
+ '4': '四',
115
+ '5': '五',
116
+ '6': '六',
117
+ '7': '七',
118
+ '8': '八',
119
+ '9': '九'
120
+ }
121
+
122
+ # 将2024/8/24, 2024-08, 08-24, 24 标准化“年月日”
123
+ # Standardize 2024/8/24, 2024-08, 08-24, 24 to "year-month-day"
124
+ def _format_chinese_data(self, date_str:str):
125
+ # 处理日期格式
126
+ input_date = date_str
127
+ if date_str is None or date_str.strip() == "":return ""
128
+ date_str = re.sub(r"[\/\._|年|月]","-",date_str)
129
+ date_str = re.sub(r"日",r"",date_str)
130
+ date_arrs = date_str.split(' ')
131
+ if len(date_arrs) == 1 and ":" in date_arrs[0]:
132
+ time_str = date_arrs[0]
133
+ date_arrs = []
134
+ else:
135
+ time_str = date_arrs[1] if len(date_arrs) >=2 else ""
136
+ def nonZero(num,cn,func=None):
137
+ if func is not None:num=func(num)
138
+ return f"{num}{cn}" if num is not None and num != "" and num != "0" else ""
139
+ f_number = self.to_chinese_number
140
+ f_currency = self.to_chinese_currency
141
+ # year, month, day
142
+ year_month_day = ""
143
+ if len(date_arrs) > 0:
144
+ year, month, day = "","",""
145
+ parts = date_arrs[0].split('-')
146
+ if len(parts) == 3: # 格式为 YYYY-MM-DD
147
+ year, month, day = parts
148
+ elif len(parts) == 2: # 格式为 MM-DD 或 YYYY-MM
149
+ if len(parts[0]) == 4: # 年-月
150
+ year, month = parts
151
+ else:month, day = parts # 月-日
152
+ elif len(parts[0]) > 0: # 仅有月-日或年
153
+ if len(parts[0]) == 4:
154
+ year = parts[0]
155
+ else:day = parts[0]
156
+ year,month,day = nonZero(year,"年",f_number),nonZero(month,"月",f_currency),nonZero(day,"日",f_currency)
157
+ year_month_day = re.sub(r"([年|月|日])+",r"\1",f"{year}{month}{day}")
158
+ # hours, minutes, seconds
159
+ time_str = re.sub(r"[\/\.\-:_]",":",time_str)
160
+ time_arrs = time_str.split(":")
161
+ hours, minutes, seconds = "","",""
162
+ if len(time_arrs) == 3: # H/M/S
163
+ hours, minutes, seconds = time_arrs
164
+ elif len(time_arrs) == 2:# H/M
165
+ hours, minutes = time_arrs
166
+ elif len(time_arrs[0]) > 0:hours = f'{time_arrs[0]}点' # H
167
+ if len(time_arrs) > 1:
168
+ hours, minutes, seconds = nonZero(hours,"点",f_currency),nonZero(minutes,"分",f_currency),nonZero(seconds,"秒",f_currency)
169
+ hours_minutes_seconds = re.sub(r"([点|分|秒])+",r"\1",f"{hours}{minutes}{seconds}")
170
+ output_date = f"{year_month_day}{hours_minutes_seconds}"
171
+ return output_date
172
+
173
+ # 【SSML】number=中文大写数字读法(单字)
174
+ # Chinese Numbers(single word)
175
+ def to_chinese_number(self, num:str):
176
+ pattern = r'(\d+)'
177
+ zh_numerals = self._zh_numerals_number
178
+ arrs = re.split(pattern, num)
179
+ output = ""
180
+ for item in arrs:
181
+ if re.match(pattern,item):
182
+ output += ''.join(zh_numerals[digit] if digit in zh_numerals else "" for digit in str(item))
183
+ else:output += item
184
+ output = output.replace(".","点")
185
+ return output
186
+
187
+ # 【SSML】telephone=数字转成中文电话号码大写汉字(单字)
188
+ # Convert numbers to Chinese phone numbers in uppercase Chinese characters(single word)
189
+ def to_chinese_telephone(self, num:str):
190
+ output = self.to_chinese_number(num.replace("+86","")) # zh +86
191
+ output = output.replace("一","幺")
192
+ return output
193
+
194
+ # 【SSML】currency=按金额发音。
195
+ # Digital processing from GPT_SoVITS num.py (thanks)
196
+ def to_chinese_currency(self, num:str):
197
+ pattern = r'(\d+)'
198
+ arrs = re.split(pattern, num)
199
+ output = ""
200
+ for item in arrs:
201
+ if re.match(pattern,item):
202
+ output += num2str(item)
203
+ else:output += item
204
+ output = output.replace(".","点")
205
+ return output
206
+
207
+ # 【SSML】date=按日期发音。支持 2024年08月24, 2024/8/24, 2024-08, 08-24, 24 等输入。
208
+ def to_chinese_date(self, num:str):
209
+ chinese_date = self._format_chinese_data(num)
210
+ return chinese_date
211
+
212
+
213
+ class LangSegment:
214
+
215
+ def __init__(self):
216
+
217
+ self.langid = LanguageIdentifier.from_pickled_model(MODEL_FILE, norm_probs=True)
218
+
219
+ self._text_cache = None
220
+ self._text_lasts = None
221
+ self._text_langs = None
222
+ self._lang_count = None
223
+ self._lang_eos = None
224
+
225
+ # 可自定义语言匹配标签:カスタマイズ可能な言語対応タグ:사용자 지정 가능한 언어 일치 태그:
226
+ # Customizable language matching tags: These are supported,이 표현들은 모두 지지합니다
227
+ # <zh>你好<zh> , <ja>佐々木</ja> , <en>OK<en> , <ko>오빠</ko> 这些写法均支持
228
+ self.SYMBOLS_PATTERN = r'(<([a-zA-Z|-]*)>(.*?)<\/*[a-zA-Z|-]*>)'
229
+
230
+ # 语言过滤组功能, 可以指定保留语言。不在过滤组中的语言将被清除。您可随心搭配TTS语音合成所支持的语言。
231
+ # 언어 필터 그룹 기능을 사용하면 예약된 언어를 지정할 수 있습니다. 필터 그룹에 없는 언어는 지워집니다. TTS 텍스트에서 지원하는 언어를 원하는 대로 일치시킬 수 있습니다.
232
+ # 言語フィルターグループ機能では、予約言語を指定できます。フィルターグループに含まれていない言語はクリアされます。TTS音声合成がサポートする言語を自由に組み合わせることができます。
233
+ # The language filter group function allows you to specify reserved languages.
234
+ # Languages not in the filter group will be cleared. You can match the languages supported by TTS Text To Speech as you like.
235
+ # 排名越前,优先级越高,The higher the ranking, the higher the priority,ランキングが上位になるほど、優先度が高くなります。
236
+
237
+ # 系统默认过滤器。System default filter。(ISO 639-1 codes given)
238
+ # ----------------------------------------------------------------------------------------------------------------------------------
239
+ # "zh"中文=Chinese ,"en"英语=English ,"ja"日语=Japanese ,"ko"韩语=Korean ,"fr"法语=French ,"vi"越南语=Vietnamese , "ru"俄语=Russian
240
+ # "th"泰语=Thai
241
+ # ----------------------------------------------------------------------------------------------------------------------------------
242
+ self.DEFAULT_FILTERS = ["zh", "ja", "ko", "en"]
243
+
244
+ # 用户可自定义过滤器。User-defined filters
245
+ self.Langfilters = self.DEFAULT_FILTERS[:] # 创建副本
246
+
247
+ # 合并文本
248
+ self.isLangMerge = True
249
+
250
+ # 试验性支持:您可自定义添加:"fr"法语 , "vi"越南语。Experimental: You can customize to add: "fr" French, "vi" Vietnamese.
251
+ # 请使用API启用:self.setfilters(["zh", "en", "ja", "ko", "fr", "vi" , "ru" , "th"]) # 您可自定义添加,如:"fr"法语 , "vi"越南语。
252
+
253
+ # 预览版功能,自动启用或禁用,无需设置
254
+ # Preview feature, automatically enabled or disabled, no settings required
255
+ self.EnablePreview = False
256
+
257
+ # 除此以外,它支持简写过滤器,只需按不同语种任意组合即可。
258
+ # In addition to that, it supports abbreviation filters, allowing for any combination of different languages.
259
+ # 示例:您可以任意指定多种组合,进行过滤
260
+ # Example: You can specify any combination to filter
261
+
262
+ # 中/日语言优先级阀值(评分范围为 0 ~ 1):评分低于设定阀值 <0.89 时,启用 filters 中的优先级。\n
263
+ # 중/일본어 우선 순위 임계값(점수 범위 0-1): 점수가 설정된 임계값 <0.89보다 낮을 때 필터에서 우선 순위를 활성화합니다.
264
+ # 中国語/日本語の優先度しきい値(スコア範囲0〜1):スコアが設定されたしきい値<0.89未満の場合、フィルターの優先度が有効になります。\n
265
+ # Chinese and Japanese language priority threshold (score range is 0 ~ 1): The default threshold is 0.89. \n
266
+ # Only the common characters between Chinese and Japanese are processed with confidence and priority. \n
267
+ self.LangPriorityThreshold = 0.89
268
+
269
+ # Langfilters = ["zh"] # 按中文识别
270
+ # Langfilters = ["en"] # 按英文识别
271
+ # Langfilters = ["ja"] # 按日文识别
272
+ # Langfilters = ["ko"] # 按韩文识别
273
+ # Langfilters = ["zh_ja"] # 中日混合识别
274
+ # Langfilters = ["zh_en"] # 中英混合识别
275
+ # Langfilters = ["ja_en"] # 日英混合识别
276
+ # Langfilters = ["zh_ko"] # 中韩混合识别
277
+ # Langfilters = ["ja_ko"] # 日韩混合识别
278
+ # Langfilters = ["en_ko"] # 英韩混合识别
279
+ # Langfilters = ["zh_ja_en"] # 中日英混合识别
280
+ # Langfilters = ["zh_ja_en_ko"] # 中日英韩混合识别
281
+
282
+ # 更多过滤组合,请您随意。。。For more filter combinations, please feel free to......
283
+ # より多くのフィルターの組み合わせ、お気軽に。。。더 많은 필터 조합을 원하시면 자유롭게 해주세요. .....
284
+
285
+ # 可选保留:支持中文数字拼音格式,更方便前端实现拼音音素修改和推理,默认关闭 False 。
286
+ # 开启后 True ,括号内的数字拼音格式均保留,并识别输出为:"zh"中文。
287
+ self.keepPinyin = False
288
+
289
+ # DEFINITION
290
+ self.PARSE_TAG = re.compile(r'(⑥\$*\d+[\d]{6,}⑥)')
291
+
292
+ self.LangSSML = LangSSML()
293
+
294
+ def _clears(self):
295
+ self._text_cache = None
296
+ self._text_lasts = None
297
+ self._text_langs = None
298
+ self._text_waits = None
299
+ self._lang_count = None
300
+ self._lang_eos = None
301
+
302
+ def _is_english_word(self, word):
303
+ return bool(re.match(r'^[a-zA-Z]+$', word))
304
+
305
+ def _is_chinese(self, word):
306
+ for char in word:
307
+ if '\u4e00' <= char <= '\u9fff':
308
+ return True
309
+ return False
310
+
311
+ def _is_japanese_kana(self, word):
312
+ pattern = re.compile(r'[\u3040-\u309F\u30A0-\u30FF]+')
313
+ matches = pattern.findall(word)
314
+ return len(matches) > 0
315
+
316
+ def _insert_english_uppercase(self, word):
317
+ modified_text = re.sub(r'(?<!\b)([A-Z])', r' \1', word)
318
+ modified_text = modified_text.strip('-')
319
+ return modified_text + " "
320
+
321
+ def _split_camel_case(self, word):
322
+ return re.sub(r'(?<!^)(?=[A-Z])', ' ', word)
323
+
324
+ def _statistics(self, language, text):
325
+ # Language word statistics:
326
+ # Chinese characters usually occupy double bytes
327
+ if self._lang_count is None or not isinstance(self._lang_count, defaultdict):
328
+ self._lang_count = defaultdict(int)
329
+ lang_count = self._lang_count
330
+ if not "|" in language:
331
+ lang_count[language] += int(len(text)*2) if language == "zh" else len(text)
332
+ self._lang_count = lang_count
333
+
334
+ def _clear_text_number(self, text):
335
+ if text == "\n":return text,False # Keep Line Breaks
336
+ clear_text = re.sub(r'([^\w\s]+)','',re.sub(r'\n+','',text)).strip()
337
+ is_number = len(re.sub(re.compile(r'(\d+)'),'',clear_text)) == 0
338
+ return clear_text,is_number
339
+
340
+ def _saveData(self, words,language:str,text:str,score:float,symbol=None):
341
+ # Pre-detection
342
+ clear_text , is_number = self._clear_text_number(text)
343
+ # Merge the same language and save the results
344
+ preData = words[-1] if len(words) > 0 else None
345
+ if symbol is not None:pass
346
+ elif preData is not None and preData["symbol"] is None:
347
+ if len(clear_text) == 0:language = preData["lang"]
348
+ elif is_number == True:language = preData["lang"]
349
+ _ , pre_is_number = self._clear_text_number(preData["text"])
350
+ if (preData["lang"] == language):
351
+ self._statistics(preData["lang"],text)
352
+ text = preData["text"] + text
353
+ preData["text"] = text
354
+ return preData
355
+ elif pre_is_number == True:
356
+ text = f'{preData["text"]}{text}'
357
+ words.pop()
358
+ elif is_number == True:
359
+ priority_language = self._get_filters_string()[:2]
360
+ if priority_language in "ja-zh-en-ko-fr-vi":language = priority_language
361
+ data = {"lang":language,"text": text,"score":score,"symbol":symbol}
362
+ filters = self.Langfilters
363
+ if filters is None or len(filters) == 0 or "?" in language or \
364
+ language in filters or language in filters[0] or \
365
+ filters[0] == "*" or filters[0] in "alls-mixs-autos":
366
+ words.append(data)
367
+ self._statistics(data["lang"],data["text"])
368
+ return data
369
+
370
+ def _addwords(self, words,language,text,score,symbol=None):
371
+ if text == "\n":pass # Keep Line Breaks
372
+ elif text is None or len(text.strip()) == 0:return True
373
+ if language is None:language = ""
374
+ language = language.lower()
375
+ if language == 'en':text = self._insert_english_uppercase(text)
376
+ # text = re.sub(r'[(())]', ',' , text) # Keep it.
377
+ text_waits = self._text_waits
378
+ ispre_waits = len(text_waits)>0
379
+ preResult = text_waits.pop() if ispre_waits else None
380
+ if preResult is None:preResult = words[-1] if len(words) > 0 else None
381
+ if preResult and ("|" in preResult["lang"]):
382
+ pre_lang = preResult["lang"]
383
+ if language in pre_lang:preResult["lang"] = language = language.split("|")[0]
384
+ else:preResult["lang"]=pre_lang.split("|")[0]
385
+ if ispre_waits:preResult = self._saveData(words,preResult["lang"],preResult["text"],preResult["score"],preResult["symbol"])
386
+ pre_lang = preResult["lang"] if preResult else None
387
+ if ("|" in language) and (pre_lang and not pre_lang in language and not "…" in language):language = language.split("|")[0]
388
+ if "|" in language:self._text_waits.append({"lang":language,"text": text,"score":score,"symbol":symbol})
389
+ else:self._saveData(words,language,text,score,symbol)
390
+ return False
391
+
392
+ def _get_prev_data(self, words):
393
+ data = words[-1] if words and len(words) > 0 else None
394
+ if data:return (data["lang"] , data["text"])
395
+ return (None,"")
396
+
397
+ def _match_ending(self, input , index):
398
+ if input is None or len(input) == 0:return False,None
399
+ input = re.sub(r'\s+', '', input)
400
+ if len(input) == 0 or abs(index) > len(input):return False,None
401
+ ending_pattern = re.compile(r'([「」“”‘’"\'::。.!!?.?])')
402
+ return ending_pattern.match(input[index]),input[index]
403
+
404
+ def _cleans_text(self, cleans_text):
405
+ cleans_text = re.sub(r'(.*?)([^\w]+)', r'\1 ', cleans_text)
406
+ cleans_text = re.sub(r'(.)\1+', r'\1', cleans_text)
407
+ return cleans_text.strip()
408
+
409
+ def _mean_processing(self, text:str):
410
+ if text is None or (text.strip()) == "":return None , 0.0
411
+ arrs = self._split_camel_case(text).split(" ")
412
+ langs = []
413
+ for t in arrs:
414
+ if len(t.strip()) <= 3:continue
415
+ language, score = self.langid.classify(t)
416
+ langs.append({"lang":language})
417
+ if len(langs) == 0:return None , 0.0
418
+ return Counter([item['lang'] for item in langs]).most_common(1)[0][0],1.0
419
+
420
+ def _lang_classify(self, cleans_text):
421
+ language, score = self.langid.classify(cleans_text)
422
+ # fix: Huggingface is np.float32
423
+ if score is not None and isinstance(score, np.generic) and hasattr(score,"item"):
424
+ score = score.item()
425
+ score = round(score , 3)
426
+ return language, score
427
+
428
+ def _get_filters_string(self):
429
+ filters = self.Langfilters
430
+ return "-".join(filters).lower().strip() if filters is not None else ""
431
+
432
+ def _parse_language(self, words , segment):
433
+ LANG_JA = "ja"
434
+ LANG_ZH = "zh"
435
+ LANG_ZH_JA = f'{LANG_ZH}|{LANG_JA}'
436
+ LANG_JA_ZH = f'{LANG_JA}|{LANG_ZH}'
437
+ language = LANG_ZH
438
+ regex_pattern = re.compile(r'([^\w\s]+)')
439
+ lines = regex_pattern.split(segment)
440
+ lines_max = len(lines)
441
+ LANG_EOS =self._lang_eos
442
+ for index, text in enumerate(lines):
443
+ if len(text) == 0:continue
444
+ EOS = index >= (lines_max - 1)
445
+ nextId = index + 1
446
+ nextText = lines[nextId] if not EOS else ""
447
+ nextPunc = len(re.sub(regex_pattern,'',re.sub(r'\n+','',nextText)).strip()) == 0
448
+ textPunc = len(re.sub(regex_pattern,'',re.sub(r'\n+','',text)).strip()) == 0
449
+ if not EOS and (textPunc == True or ( len(nextText.strip()) >= 0 and nextPunc == True)):
450
+ lines[nextId] = f'{text}{nextText}'
451
+ continue
452
+ number_tags = re.compile(r'(⑥\d{6,}⑥)')
453
+ cleans_text = re.sub(number_tags, '' ,text)
454
+ cleans_text = re.sub(r'\d+', '' ,cleans_text)
455
+ cleans_text = self._cleans_text(cleans_text)
456
+ # fix:Langid's recognition of short sentences is inaccurate, and it is spliced longer.
457
+ if not EOS and len(cleans_text) <= 2:
458
+ lines[nextId] = f'{text}{nextText}'
459
+ continue
460
+ language,score = self._lang_classify(cleans_text)
461
+ prev_language , prev_text = self._get_prev_data(words)
462
+ if language != LANG_ZH and all('\u4e00' <= c <= '\u9fff' for c in re.sub(r'\s','',cleans_text)):language,score = LANG_ZH,1
463
+ if len(cleans_text) <= 5 and self._is_chinese(cleans_text):
464
+ filters_string = self._get_filters_string()
465
+ if score < self.LangPriorityThreshold and len(filters_string) > 0:
466
+ index_ja , index_zh = filters_string.find(LANG_JA) , filters_string.find(LANG_ZH)
467
+ if index_ja != -1 and index_ja < index_zh:language = LANG_JA
468
+ elif index_zh != -1 and index_zh < index_ja:language = LANG_ZH
469
+ if self._is_japanese_kana(cleans_text):language = LANG_JA
470
+ elif len(cleans_text) > 2 and score > 0.90:pass
471
+ elif EOS and LANG_EOS:language = LANG_ZH if len(cleans_text) <= 1 else language
472
+ else:
473
+ LANG_UNKNOWN = LANG_ZH_JA if language == LANG_ZH or (len(cleans_text) <=2 and prev_language == LANG_ZH) else LANG_JA_ZH
474
+ match_end,match_char = self._match_ending(text, -1)
475
+ referen = prev_language in LANG_UNKNOWN or LANG_UNKNOWN in prev_language if prev_language else False
476
+ if match_char in "。.": language = prev_language if referen and len(words) > 0 else language
477
+ else:language = f"{LANG_UNKNOWN}|…"
478
+ text,*_ = re.subn(number_tags , self._restore_number , text )
479
+ self._addwords(words,language,text,score)
480
+
481
+ # ----------------------------------------------------------
482
+ # 【SSML】中文数字处理:Chinese Number Processing (SSML support)
483
+ # 这里默认都是中文,用于处理 SSML 中文标签。当然可以支持任意语言,例如:
484
+ # The default here is Chinese, which is used to process SSML Chinese tags. Of course, any language can be supported, for example:
485
+ # 中文电话号码:<telephone>1234567</telephone>
486
+ # 中文数字号码:<number>1234567</number>
487
+ def _process_symbol_SSML(self, words,data):
488
+ tag , match = data
489
+ language = SSML = match[1]
490
+ text = match[2]
491
+ score = 1.0
492
+ if SSML == "telephone":
493
+ # 中文-电话号码
494
+ language = "zh"
495
+ text = self.LangSSML.to_chinese_telephone(text)
496
+ elif SSML == "number":
497
+ # 中文-数字读法
498
+ language = "zh"
499
+ text = self.LangSSML.to_chinese_number(text)
500
+ elif SSML == "currency":
501
+ # 中文-按金额发音
502
+ language = "zh"
503
+ text = self.LangSSML.to_chinese_currency(text)
504
+ elif SSML == "date":
505
+ # 中文-按金额发音
506
+ language = "zh"
507
+ text = self.LangSSML.to_chinese_date(text)
508
+ self._addwords(words,language,text,score,SSML)
509
+
510
+ # ----------------------------------------------------------
511
+ def _restore_number(self, matche):
512
+ value = matche.group(0)
513
+ text_cache = self._text_cache
514
+ if value in text_cache:
515
+ process , data = text_cache[value]
516
+ tag , match = data
517
+ value = match
518
+ return value
519
+
520
+ def _pattern_symbols(self, item , text):
521
+ if text is None:return text
522
+ tag , pattern , process = item
523
+ matches = pattern.findall(text)
524
+ if len(matches) == 1 and "".join(matches[0]) == text:
525
+ return text
526
+ for i , match in enumerate(matches):
527
+ key = f"⑥{tag}{i:06d}⑥"
528
+ text = re.sub(pattern , key , text , count=1)
529
+ self._text_cache[key] = (process , (tag , match))
530
+ return text
531
+
532
+ def _process_symbol(self, words,data):
533
+ tag , match = data
534
+ language = match[1]
535
+ text = match[2]
536
+ score = 1.0
537
+ filters = self._get_filters_string()
538
+ if language not in filters:
539
+ self._process_symbol_SSML(words,data)
540
+ else:
541
+ self._addwords(words,language,text,score,True)
542
+
543
+ def _process_english(self, words,data):
544
+ tag , match = data
545
+ text = match[0]
546
+ filters = self._get_filters_string()
547
+ priority_language = filters[:2]
548
+ # Preview feature, other language segmentation processing
549
+ enablePreview = self.EnablePreview
550
+ if enablePreview == True:
551
+ # Experimental: Other language support
552
+ regex_pattern = re.compile(r'(.*?[。.??!!]+[\n]{,1})')
553
+ lines = regex_pattern.split(text)
554
+ for index , text in enumerate(lines):
555
+ if len(text.strip()) == 0:continue
556
+ cleans_text = self._cleans_text(text)
557
+ language,score = self._lang_classify(cleans_text)
558
+ if language not in filters:
559
+ language,score = self._mean_processing(cleans_text)
560
+ if language is None or score <= 0.0:continue
561
+ elif language in filters:pass # pass
562
+ elif score >= 0.95:continue # High score, but not in the filter, excluded.
563
+ elif score <= 0.15 and filters[:2] == "fr":language = priority_language
564
+ else:language = "en"
565
+ self._addwords(words,language,text,score)
566
+ else:
567
+ # Default is English
568
+ language, score = "en", 1.0
569
+ self._addwords(words,language,text,score)
570
+
571
+ def _process_Russian(self, words,data):
572
+ tag , match = data
573
+ text = match[0]
574
+ language = "ru"
575
+ score = 1.0
576
+ self._addwords(words,language,text,score)
577
+
578
+ def _process_Thai(self, words,data):
579
+ tag , match = data
580
+ text = match[0]
581
+ language = "th"
582
+ score = 1.0
583
+ self._addwords(words,language,text,score)
584
+
585
+ def _process_korean(self, words,data):
586
+ tag , match = data
587
+ text = match[0]
588
+ language = "ko"
589
+ score = 1.0
590
+ self._addwords(words,language,text,score)
591
+
592
+ def _process_quotes(self, words,data):
593
+ tag , match = data
594
+ text = "".join(match)
595
+ childs = self.PARSE_TAG.findall(text)
596
+ if len(childs) > 0:
597
+ self._process_tags(words , text , False)
598
+ else:
599
+ cleans_text = self._cleans_text(match[1])
600
+ if len(cleans_text) <= 5:
601
+ self._parse_language(words,text)
602
+ else:
603
+ language,score = self._lang_classify(cleans_text)
604
+ self._addwords(words,language,text,score)
605
+
606
+ def _process_pinyin(self, words,data):
607
+ tag , match = data
608
+ text = match
609
+ language = "zh"
610
+ score = 1.0
611
+ self._addwords(words,language,text,score)
612
+
613
+ def _process_number(self, words,data): # "$0" process only
614
+ """
615
+ Numbers alone cannot accurately identify language.
616
+ Because numbers are universal in all languages.
617
+ So it won't be executed here, just for testing.
618
+ """
619
+ tag , match = data
620
+ language = words[0]["lang"] if len(words) > 0 else "zh"
621
+ text = match
622
+ score = 0.0
623
+ self._addwords(words,language,text,score)
624
+
625
+ def _process_tags(self, words , text , root_tag):
626
+ text_cache = self._text_cache
627
+ segments = re.split(self.PARSE_TAG, text)
628
+ segments_len = len(segments) - 1
629
+ for index , text in enumerate(segments):
630
+ if root_tag:self._lang_eos = index >= segments_len
631
+ if self.PARSE_TAG.match(text):
632
+ process , data = text_cache[text]
633
+ if process:process(words , data)
634
+ else:
635
+ self._parse_language(words , text)
636
+ return words
637
+
638
+ def _merge_results(self, words):
639
+ new_word = []
640
+ for index , cur_data in enumerate(words):
641
+ if "symbol" in cur_data:del cur_data["symbol"]
642
+ if index == 0:new_word.append(cur_data)
643
+ else:
644
+ pre_data = new_word[-1]
645
+ if cur_data["lang"] == pre_data["lang"]:
646
+ pre_data["text"] = f'{pre_data["text"]}{cur_data["text"]}'
647
+ else:new_word.append(cur_data)
648
+ return new_word
649
+
650
+ def _parse_symbols(self, text):
651
+ TAG_NUM = "00" # "00" => default channels , "$0" => testing channel
652
+ TAG_S1,TAG_S2,TAG_P1,TAG_P2,TAG_EN,TAG_KO,TAG_RU,TAG_TH = "$1" ,"$2" ,"$3" ,"$4" ,"$5" ,"$6" ,"$7","$8"
653
+ TAG_BASE = re.compile(fr'(([【《((“‘"\']*[LANGUAGE]+[\W\s]*)+)')
654
+ # Get custom language filter
655
+ filters = self.Langfilters
656
+ filters = filters if filters is not None else ""
657
+ # =======================================================================================================
658
+ # Experimental: Other language support.Thử nghiệm: Hỗ trợ ngôn ngữ khác.Expérimental : prise en charge d’autres langues.
659
+ # 相关语言字符如有缺失,熟悉相关语言的朋友,可以提交把缺失的发音符号补全。
660
+ # If relevant language characters are missing, friends who are familiar with the relevant languages can submit a submission to complete the missing pronunciation symbols.
661
+ # S'il manque des caractères linguistiques pertinents, les amis qui connaissent les langues concernées peuvent soumettre une soumission pour compléter les symboles de prononciation manquants.
662
+ # Nếu thiếu ký tự ngôn ngữ liên quan, những người bạn quen thuộc với ngôn ngữ liên quan có thể gửi bài để hoàn thành các ký hiệu phát âm còn thiếu.
663
+ # -------------------------------------------------------------------------------------------------------
664
+ # Preview feature, other language support
665
+ enablePreview = self.EnablePreview
666
+ if "fr" in filters or \
667
+ "vi" in filters:enablePreview = True
668
+ self.EnablePreview = enablePreview
669
+ # 实验性:法语字符支持。Prise en charge des caractères français
670
+ RE_FR = "" if not enablePreview else "àáâãäåæçèéêëìíîïðñòóôõöùúûüýþÿ"
671
+ # 实验性:越南语字符支持。Hỗ trợ ký tự tiếng Việt
672
+ RE_VI = "" if not enablePreview else "đơưăáàảãạắằẳẵặấầẩẫậéèẻẽẹếềểễệíìỉĩịóòỏõọốồổỗộớờởỡợúùủũụứừửữựôâêơưỷỹ"
673
+ # -------------------------------------------------------------------------------------------------------
674
+ # Basic options:
675
+ process_list = [
676
+ ( TAG_S1 , re.compile(self.SYMBOLS_PATTERN) , self._process_symbol ), # Symbol Tag
677
+ ( TAG_KO , re.compile(re.sub(r'LANGUAGE',f'\uac00-\ud7a3',TAG_BASE.pattern)) , self._process_korean ), # Korean words
678
+ ( TAG_TH , re.compile(re.sub(r'LANGUAGE',f'\u0E00-\u0E7F',TAG_BASE.pattern)) , self._process_Thai ), # Thai words support.
679
+ ( TAG_RU , re.compile(re.sub(r'LANGUAGE',f'А-Яа-яЁё',TAG_BASE.pattern)) , self._process_Russian ), # Russian words support.
680
+ ( TAG_NUM , re.compile(r'(\W*\d+\W+\d*\W*\d*)') , self._process_number ), # Number words, Universal in all languages, Ignore it.
681
+ ( TAG_EN , re.compile(re.sub(r'LANGUAGE',f'a-zA-Z{RE_FR}{RE_VI}',TAG_BASE.pattern)) , self._process_english ), # English words + Other language support.
682
+ ( TAG_P1 , re.compile(r'(["\'])(.*?)(\1)') , self._process_quotes ), # Regular quotes
683
+ ( TAG_P2 , re.compile(r'([\n]*[【《((“‘])([^【《((“‘’”))》】]{3,})([’”))》】][\W\s]*[\n]{,1})') , self._process_quotes ), # Special quotes, There are left and right.
684
+ ]
685
+ # Extended options: Default False
686
+ if self.keepPinyin == True:process_list.insert(1 ,
687
+ ( TAG_S2 , re.compile(r'([\(({](?:\s*\w*\d\w*\s*)+[})\)])') , self._process_pinyin ), # Chinese Pinyin Tag.
688
+ )
689
+ # -------------------------------------------------------------------------------------------------------
690
+ words = []
691
+ lines = re.findall(r'.*\n*', re.sub(self.PARSE_TAG, '' ,text))
692
+ for index , text in enumerate(lines):
693
+ if len(text.strip()) == 0:continue
694
+ self._lang_eos = False
695
+ self._text_cache = {}
696
+ for item in process_list:
697
+ text = self._pattern_symbols(item , text)
698
+ cur_word = self._process_tags([] , text , True)
699
+ if len(cur_word) == 0:continue
700
+ cur_data = cur_word[0] if len(cur_word) > 0 else None
701
+ pre_data = words[-1] if len(words) > 0 else None
702
+ if cur_data and pre_data and cur_data["lang"] == pre_data["lang"] \
703
+ and cur_data["symbol"] == False and pre_data["symbol"] :
704
+ cur_data["text"] = f'{pre_data["text"]}{cur_data["text"]}'
705
+ words.pop()
706
+ words += cur_word
707
+ if self.isLangMerge == True:words = self._merge_results(words)
708
+ lang_count = self._lang_count
709
+ if lang_count and len(lang_count) > 0:
710
+ lang_count = dict(sorted(lang_count.items(), key=lambda x: x[1], reverse=True))
711
+ lang_count = list(lang_count.items())
712
+ self._lang_count = lang_count
713
+ return words
714
+
715
+ def setfilters(self, filters):
716
+ # 当过滤器更改时,清除缓存
717
+ # 필터가 변경되면 캐시를 지웁니다.
718
+ # フィルタが変更されると、キャッシュがクリアされます
719
+ # When the filter changes, clear the cache
720
+ if self.Langfilters != filters:
721
+ self._clears()
722
+ self.Langfilters = filters
723
+
724
+ def getfilters(self):
725
+ return self.Langfilters
726
+
727
+ def setPriorityThreshold(self, threshold:float):
728
+ self.LangPriorityThreshold = threshold
729
+
730
+ def getPriorityThreshold(self):
731
+ return self.LangPriorityThreshold
732
+
733
+ def getCounts(self):
734
+ lang_count = self._lang_count
735
+ if lang_count is not None:return lang_count
736
+ text_langs = self._text_langs
737
+ if text_langs is None or len(text_langs) == 0:return [("zh",0)]
738
+ lang_counts = defaultdict(int)
739
+ for d in text_langs:lang_counts[d['lang']] += int(len(d['text'])*2) if d['lang'] == "zh" else len(d['text'])
740
+ lang_counts = dict(sorted(lang_counts.items(), key=lambda x: x[1], reverse=True))
741
+ lang_counts = list(lang_counts.items())
742
+ self._lang_count = lang_counts
743
+ return lang_counts
744
+
745
+ def getTexts(self, text:str):
746
+ if text is None or len(text.strip()) == 0:
747
+ self._clears()
748
+ return []
749
+ # lasts
750
+ text_langs = self._text_langs
751
+ if self._text_lasts == text and text_langs is not None:return text_langs
752
+ # parse
753
+ self._text_waits = []
754
+ self._lang_count = None
755
+ self._text_lasts = text
756
+ text = self._parse_symbols(text)
757
+ self._text_langs = text
758
+ return text
759
+
760
+ def classify(self, text:str):
761
+ return self.getTexts(text)
762
+
763
+ def printList(langlist):
764
+ """
765
+ 功能:打印数组结果
766
+ 기능: 어레이 결과 인쇄
767
+ 機能:配列結果を印刷
768
+ Function: Print array results
769
+ """
770
+ print("\n===================【打印结果】===================")
771
+ if langlist is None or len(langlist) == 0:
772
+ print("无内容结果,No content result")
773
+ return
774
+ for line in langlist:
775
+ print(line)
776
+ pass
777
+
778
+
779
+
780
+ def main():
781
+
782
+ # -----------------------------------
783
+ # 更新日志:新版本分词更加精准。
784
+ # Changelog: The new version of the word segmentation is more accurate.
785
+ # チェンジログ:新しいバージョンの単語セグメンテーションはより正確です。
786
+ # Changelog: 분할이라는 단어의 새로운 버전이 더 정확합니다.
787
+ # -----------------------------------
788
+
789
+ # 输入示例1:(包含日文,中文)Input Example 1: (including Japanese, Chinese)
790
+ # text = "“昨日は雨が降った,音楽、映画。。。”你今天学习日语了吗?春は桜の季節です。语种分词是语音合成必不可少的环节。言語分詞は音声合成に欠かせない環節である!"
791
+
792
+ # 输入示例2:(包含日文,中文)Input Example 1: (including Japanese, Chinese)
793
+ # text = "欢迎来玩。東京,は日本の首都です。欢迎来玩. 太好了!"
794
+
795
+ # 输入示例3:(包含日文,中文)Input Example 1: (including Japanese, Chinese)
796
+ # text = "明日、私たちは海辺にバカンスに行きます。你会说日语吗:“中国語、話せますか” 你的日语真好啊!"
797
+
798
+
799
+ # 输入示例4:(包含日文,中文,韩语,英文)Input Example 4: (including Japanese, Chinese, Korean, English)
800
+ # text = "你的名字叫<ja>佐々木?<ja>吗?韩语中的안녕 오빠读什么呢?あなたの体育の先生は誰ですか? 此次发布会带来了四款iPhone 15系列机型和三款Apple Watch等一系列新品,这次的iPad Air采用了LCD屏幕"
801
+
802
+
803
+ # 试验性支持:"fr"法语 , "vi"越南语 , "ru"俄语 , "th"泰语。Experimental: Other language support.
804
+ langsegment = LangSegment()
805
+ langsegment.setfilters(["fr", "vi" , "ja", "zh", "ko", "en" , "ru" , "th"])
806
+ text = """
807
+ 我喜欢在雨天里听音乐。
808
+ I enjoy listening to music on rainy days.
809
+ 雨の日に音楽を聴くのが好きです。
810
+ 비 오는 날에 음악을 듣는 것을 즐깁니다。
811
+ J'aime écouter de la musique les jours de pluie.
812
+ Tôi thích nghe nhạc vào những ngày mưa.
813
+ Мне нравится слушать музыку в дождливую погоду.
814
+ ฉันชอบฟังเพลงในวันที่ฝนตก
815
+ """
816
+
817
+
818
+
819
+ # 进行分词:(接入TTS项目仅需一行代码调用)Segmentation: (Only one line of code is required to access the TTS project)
820
+ langlist = langsegment.getTexts(text)
821
+ printList(langlist)
822
+
823
+
824
+ # 语种统计:Language statistics:
825
+ print("\n===================【语种统计】===================")
826
+ # 获取所有语种数组结果,根据内容字数降序排列
827
+ # Get the array results in all languages, sorted in descending order according to the number of content words
828
+ langCounts = langsegment.getCounts()
829
+ print(langCounts , "\n")
830
+
831
+ # 根据结果获取内容的主要语种 (语言,字数含标点)
832
+ # Get the main language of content based on the results (language, word count including punctuation)
833
+ lang , count = langCounts[0]
834
+ print(f"输入内容的主要语言为 = {lang} ,字数 = {count}")
835
+ print("==================================================\n")
836
+
837
+
838
+ # 分词输出:lang=语言,text=内容。Word output: lang = language, text = content
839
+ # ===================【打印结果】===================
840
+ # {'lang': 'zh', 'text': '你的名字叫'}
841
+ # {'lang': 'ja', 'text': '佐々木?'}
842
+ # {'lang': 'zh', 'text': '吗?韩语中的'}
843
+ # {'lang': 'ko', 'text': '안녕 오빠'}
844
+ # {'lang': 'zh', 'text': '读什么呢?'}
845
+ # {'lang': 'ja', 'text': 'あなたの体育の先生は誰ですか?'}
846
+ # {'lang': 'zh', 'text': ' 此次发布会带来了四款'}
847
+ # {'lang': 'en', 'text': 'i Phone '}
848
+ # {'lang': 'zh', 'text': '15系列机型和三款'}
849
+ # {'lang': 'en', 'text': 'Apple Watch '}
850
+ # {'lang': 'zh', 'text': '等一系列新品,这次的'}
851
+ # {'lang': 'en', 'text': 'i Pad Air '}
852
+ # {'lang': 'zh', 'text': '采用了'}
853
+ # {'lang': 'en', 'text': 'L C D '}
854
+ # {'lang': 'zh', 'text': '屏幕'}
855
+ # ===================【语种统计】===================
856
+
857
+ # ===================【语种统计】===================
858
+ # [('zh', 51), ('ja', 19), ('en', 18), ('ko', 5)]
859
+
860
+ # 输入内容的主要语言为 = zh ,字数 = 51
861
+ # ==================================================
862
+ # The main language of the input content is = zh, word count = 51
863
+
864
+
865
+ if __name__ == "__main__":
866
+ main()
language_segmentation/__init__.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ from .LangSegment import LangSegment
2
+
3
+
4
+ # release
5
+ __version__ = '0.3.5'
6
+
7
+
8
+ # develop
9
+ __develop__ = 'dev-0.0.1'
language_segmentation/utils/__init__.py ADDED
File without changes
language_segmentation/utils/num.py ADDED
@@ -0,0 +1,327 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # Digital processing from GPT_SoVITS num.py (thanks)
15
+ """
16
+ Rules to verbalize numbers into Chinese characters.
17
+ https://zh.wikipedia.org/wiki/中文数字#現代中文
18
+ """
19
+
20
+ import re
21
+ from collections import OrderedDict
22
+ from typing import List
23
+
24
+ DIGITS = {str(i): tran for i, tran in enumerate('零一二三四五六七八九')}
25
+ UNITS = OrderedDict({
26
+ 1: '十',
27
+ 2: '百',
28
+ 3: '千',
29
+ 4: '万',
30
+ 8: '亿',
31
+ })
32
+
33
+ COM_QUANTIFIERS = '(处|台|架|枚|趟|幅|平|方|堵|间|床|株|批|项|例|列|篇|栋|注|亩|封|艘|把|目|套|段|人|所|朵|匹|张|座|回|场|尾|条|个|首|阙|阵|网|炮|顶|丘|棵|只|支|袭|辆|挑|担|颗|壳|窠|曲|墙|群|腔|砣|座|客|贯|扎|捆|刀|令|打|手|罗|坡|山|岭|江|溪|钟|队|单|双|对|出|口|头|脚|板|跳|枝|件|贴|针|线|管|名|位|身|堂|课|本|页|家|户|层|丝|毫|厘|分|钱|两|斤|担|铢|石|钧|锱|忽|(千|毫|微)克|毫|厘|(公)分|分|寸|尺|丈|里|寻|常|铺|程|(千|分|厘|毫|微)米|米|撮|勺|合|升|斗|石|盘|碗|碟|叠|桶|笼|盆|盒|杯|钟|斛|锅|簋|篮|盘|桶|罐|瓶|壶|卮|盏|箩|箱|煲|啖|袋|钵|年|月|日|季|刻|时|周|天|秒|分|小时|旬|纪|岁|世|更|夜|春|夏|秋|冬|代|伏|辈|丸|泡|粒|颗|幢|堆|条|根|支|道|面|片|张|颗|块|元|(亿|千万|百万|万|千|百)|(亿|千万|百万|万|千|百|美|)元|(亿|千万|百万|万|千|百|十|)吨|(亿|千万|百万|万|千|百|)块|角|毛|分)'
34
+
35
+ # 分数表达式
36
+ RE_FRAC = re.compile(r'(-?)(\d+)/(\d+)')
37
+
38
+
39
+ def replace_frac(match) -> str:
40
+ """
41
+ Args:
42
+ match (re.Match)
43
+ Returns:
44
+ str
45
+ """
46
+ sign = match.group(1)
47
+ nominator = match.group(2)
48
+ denominator = match.group(3)
49
+ sign: str = "负" if sign else ""
50
+ nominator: str = num2str(nominator)
51
+ denominator: str = num2str(denominator)
52
+ result = f"{sign}{denominator}分之{nominator}"
53
+ return result
54
+
55
+
56
+ # 百分数表达式
57
+ RE_PERCENTAGE = re.compile(r'(-?)(\d+(\.\d+)?)%')
58
+
59
+
60
+ def replace_percentage(match) -> str:
61
+ """
62
+ Args:
63
+ match (re.Match)
64
+ Returns:
65
+ str
66
+ """
67
+ sign = match.group(1)
68
+ percent = match.group(2)
69
+ sign: str = "负" if sign else ""
70
+ percent: str = num2str(percent)
71
+ result = f"{sign}百分之{percent}"
72
+ return result
73
+
74
+
75
+ # 整数表达式
76
+ # 带负号的整数 -10
77
+ RE_INTEGER = re.compile(r'(-)' r'(\d+)')
78
+
79
+
80
+ def replace_negative_num(match) -> str:
81
+ """
82
+ Args:
83
+ match (re.Match)
84
+ Returns:
85
+ str
86
+ """
87
+ sign = match.group(1)
88
+ number = match.group(2)
89
+ sign: str = "负" if sign else ""
90
+ number: str = num2str(number)
91
+ result = f"{sign}{number}"
92
+ return result
93
+
94
+
95
+ # 编号-无符号整形
96
+ # 00078
97
+ RE_DEFAULT_NUM = re.compile(r'\d{3}\d*')
98
+
99
+
100
+ def replace_default_num(match):
101
+ """
102
+ Args:
103
+ match (re.Match)
104
+ Returns:
105
+ str
106
+ """
107
+ number = match.group(0)
108
+ return verbalize_digit(number, alt_one=True)
109
+
110
+
111
+ # 加减乘除
112
+ # RE_ASMD = re.compile(
113
+ # r'((-?)((\d+)(\.\d+)?)|(\.(\d+)))([\+\-\×÷=])((-?)((\d+)(\.\d+)?)|(\.(\d+)))')
114
+ RE_ASMD = re.compile(
115
+ r'((-?)((\d+)(\.\d+)?[⁰¹²³⁴⁵⁶⁷⁸⁹ˣʸⁿ]*)|(\.\d+[⁰¹²³⁴⁵⁶⁷⁸⁹ˣʸⁿ]*)|([A-Za-z][⁰¹²³⁴⁵⁶⁷⁸⁹ˣʸⁿ]*))([\+\-\×÷=])((-?)((\d+)(\.\d+)?[⁰¹²³⁴⁵⁶⁷⁸⁹ˣʸⁿ]*)|(\.\d+[⁰¹²³⁴⁵⁶⁷⁸⁹ˣʸⁿ]*)|([A-Za-z][⁰¹²³⁴⁵⁶⁷⁸⁹ˣʸⁿ]*))')
116
+
117
+ asmd_map = {
118
+ '+': '加',
119
+ '-': '减',
120
+ '×': '乘',
121
+ '÷': '除',
122
+ '=': '等于'
123
+ }
124
+
125
+ def replace_asmd(match) -> str:
126
+ """
127
+ Args:
128
+ match (re.Match)
129
+ Returns:
130
+ str
131
+ """
132
+ result = match.group(1) + asmd_map[match.group(8)] + match.group(9)
133
+ return result
134
+
135
+
136
+ # 次方专项
137
+ RE_POWER = re.compile(r'[⁰¹²³⁴⁵⁶⁷⁸⁹ˣʸⁿ]+')
138
+
139
+ power_map = {
140
+ '⁰': '0',
141
+ '¹': '1',
142
+ '²': '2',
143
+ '³': '3',
144
+ '⁴': '4',
145
+ '⁵': '5',
146
+ '⁶': '6',
147
+ '⁷': '7',
148
+ '⁸': '8',
149
+ '⁹': '9',
150
+ 'ˣ': 'x',
151
+ 'ʸ': 'y',
152
+ 'ⁿ': 'n'
153
+ }
154
+
155
+ def replace_power(match) -> str:
156
+ """
157
+ Args:
158
+ match (re.Match)
159
+ Returns:
160
+ str
161
+ """
162
+ power_num = ""
163
+ for m in match.group(0):
164
+ power_num += power_map[m]
165
+ result = "的" + power_num + "次方"
166
+ return result
167
+
168
+
169
+ # 数字表达式
170
+ # 纯小数
171
+ RE_DECIMAL_NUM = re.compile(r'(-?)((\d+)(\.\d+))' r'|(\.(\d+))')
172
+ # 正整数 + 量词
173
+ RE_POSITIVE_QUANTIFIERS = re.compile(r"(\d+)([多余几\+])?" + COM_QUANTIFIERS)
174
+ RE_NUMBER = re.compile(r'(-?)((\d+)(\.\d+)?)' r'|(\.(\d+))')
175
+
176
+
177
+ def replace_positive_quantifier(match) -> str:
178
+ """
179
+ Args:
180
+ match (re.Match)
181
+ Returns:
182
+ str
183
+ """
184
+ number = match.group(1)
185
+ match_2 = match.group(2)
186
+ if match_2 == "+":
187
+ match_2 = "多"
188
+ match_2: str = match_2 if match_2 else ""
189
+ quantifiers: str = match.group(3)
190
+ number: str = num2str(number)
191
+ result = f"{number}{match_2}{quantifiers}"
192
+ return result
193
+
194
+
195
+ def replace_number(match) -> str:
196
+ """
197
+ Args:
198
+ match (re.Match)
199
+ Returns:
200
+ str
201
+ """
202
+ sign = match.group(1)
203
+ number = match.group(2)
204
+ pure_decimal = match.group(5)
205
+ if pure_decimal:
206
+ result = num2str(pure_decimal)
207
+ else:
208
+ sign: str = "负" if sign else ""
209
+ number: str = num2str(number)
210
+ result = f"{sign}{number}"
211
+ return result
212
+
213
+
214
+ # 范围表达式
215
+ # match.group(1) and match.group(8) are copy from RE_NUMBER
216
+
217
+ RE_RANGE = re.compile(
218
+ r"""
219
+ (?<![\d\+\-\×÷=]) # 使用反向前瞻以确保数字范围之前没有其他数字和操作符
220
+ ((-?)((\d+)(\.\d+)?)) # 匹配范围起始的负数或正数(整数或小数)
221
+ [-~] # 匹配范围分隔符
222
+ ((-?)((\d+)(\.\d+)?)) # 匹配范围结束的负数或正数(整数或小数)
223
+ (?![\d\+\-\×÷=]) # 使用正向前瞻以确保数字范围之后没有其他数字和操作符
224
+ """, re.VERBOSE)
225
+
226
+
227
+ def replace_range(match) -> str:
228
+ """
229
+ Args:
230
+ match (re.Match)
231
+ Returns:
232
+ str
233
+ """
234
+ first, second = match.group(1), match.group(6)
235
+ first = RE_NUMBER.sub(replace_number, first)
236
+ second = RE_NUMBER.sub(replace_number, second)
237
+ result = f"{first}到{second}"
238
+ return result
239
+
240
+
241
+ # ~至表达式
242
+ RE_TO_RANGE = re.compile(
243
+ r'((-?)((\d+)(\.\d+)?)|(\.(\d+)))(%|°C|℃|度|摄氏度|cm2|cm²|cm3|cm³|cm|db|ds|kg|km|m2|m²|m³|m3|ml|m|mm|s)[~]((-?)((\d+)(\.\d+)?)|(\.(\d+)))(%|°C|℃|度|摄氏度|cm2|cm²|cm3|cm³|cm|db|ds|kg|km|m2|m²|m³|m3|ml|m|mm|s)')
244
+
245
+ def replace_to_range(match) -> str:
246
+ """
247
+ Args:
248
+ match (re.Match)
249
+ Returns:
250
+ str
251
+ """
252
+ result = match.group(0).replace('~', '至')
253
+ return result
254
+
255
+
256
+ def _get_value(value_string: str, use_zero: bool=True) -> List[str]:
257
+ stripped = value_string.lstrip('0')
258
+ if len(stripped) == 0:
259
+ return []
260
+ elif len(stripped) == 1:
261
+ if use_zero and len(stripped) < len(value_string):
262
+ return [DIGITS['0'], DIGITS[stripped]]
263
+ else:
264
+ return [DIGITS[stripped]]
265
+ else:
266
+ largest_unit = next(
267
+ power for power in reversed(UNITS.keys()) if power < len(stripped))
268
+ first_part = value_string[:-largest_unit]
269
+ second_part = value_string[-largest_unit:]
270
+ return _get_value(first_part) + [UNITS[largest_unit]] + _get_value(
271
+ second_part)
272
+
273
+
274
+ def verbalize_cardinal(value_string: str) -> str:
275
+ if not value_string:
276
+ return ''
277
+
278
+ # 000 -> '零' , 0 -> '零'
279
+ value_string = value_string.lstrip('0')
280
+ if len(value_string) == 0:
281
+ return DIGITS['0']
282
+
283
+ result_symbols = _get_value(value_string)
284
+ # verbalized number starting with '一十*' is abbreviated as `十*`
285
+ if len(result_symbols) >= 2 and result_symbols[0] == DIGITS[
286
+ '1'] and result_symbols[1] == UNITS[1]:
287
+ result_symbols = result_symbols[1:]
288
+ return ''.join(result_symbols)
289
+
290
+
291
+ def verbalize_digit(value_string: str, alt_one=False) -> str:
292
+ result_symbols = [DIGITS[digit] for digit in value_string]
293
+ result = ''.join(result_symbols)
294
+ if alt_one:
295
+ result = result.replace("一", "幺")
296
+ return result
297
+
298
+
299
+ def num2str(value_string: str) -> str:
300
+ integer_decimal = value_string.split('.')
301
+ if len(integer_decimal) == 1:
302
+ integer = integer_decimal[0]
303
+ decimal = ''
304
+ elif len(integer_decimal) == 2:
305
+ integer, decimal = integer_decimal
306
+ else:
307
+ raise ValueError(
308
+ f"The value string: '${value_string}' has more than one point in it."
309
+ )
310
+
311
+ result = verbalize_cardinal(integer)
312
+
313
+ decimal = decimal.rstrip('0')
314
+ if decimal:
315
+ # '.22' is verbalized as '零点二二'
316
+ # '3.20' is verbalized as '三点二
317
+ result = result if result else "零"
318
+ result += '点' + verbalize_digit(decimal)
319
+ return result
320
+
321
+
322
+ if __name__ == "__main__":
323
+
324
+ text = ""
325
+ text = num2str(text)
326
+ print(text)
327
+ pass
models/ace_step_transformer.py ADDED
@@ -0,0 +1,475 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2024 The HuggingFace Team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ from dataclasses import dataclass
15
+ from typing import Any, Dict, Optional, Tuple, List, Union
16
+
17
+ import torch
18
+ import torch.nn.functional as F
19
+ from torch import nn
20
+
21
+ from diffusers.configuration_utils import ConfigMixin, register_to_config
22
+ from diffusers.utils import BaseOutput, is_torch_version
23
+ from diffusers.models.modeling_utils import ModelMixin
24
+ from diffusers.models.embeddings import TimestepEmbedding, Timesteps
25
+ from diffusers.loaders import FromOriginalModelMixin, PeftAdapterMixin
26
+
27
+
28
+ from .attention import LinearTransformerBlock, t2i_modulate
29
+ from .lyrics_utils.lyric_encoder import ConformerEncoder as LyricEncoder
30
+
31
+
32
+ def cross_norm(hidden_states, controlnet_input):
33
+ # input N x T x c
34
+ mean_hidden_states, std_hidden_states = hidden_states.mean(dim=(1,2), keepdim=True), hidden_states.std(dim=(1,2), keepdim=True)
35
+ mean_controlnet_input, std_controlnet_input = controlnet_input.mean(dim=(1,2), keepdim=True), controlnet_input.std(dim=(1,2), keepdim=True)
36
+ controlnet_input = (controlnet_input - mean_controlnet_input) * (std_hidden_states / (std_controlnet_input + 1e-12)) + mean_hidden_states
37
+ return controlnet_input
38
+
39
+
40
+ # Copied from transformers.models.mixtral.modeling_mixtral.MixtralRotaryEmbedding with Mixtral->Qwen2
41
+ class Qwen2RotaryEmbedding(nn.Module):
42
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
43
+ super().__init__()
44
+
45
+ self.dim = dim
46
+ self.max_position_embeddings = max_position_embeddings
47
+ self.base = base
48
+ inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2, dtype=torch.int64).float().to(device) / self.dim))
49
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
50
+
51
+ # Build here to make `torch.jit.trace` work.
52
+ self._set_cos_sin_cache(
53
+ seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.get_default_dtype()
54
+ )
55
+
56
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
57
+ self.max_seq_len_cached = seq_len
58
+ t = torch.arange(self.max_seq_len_cached, device=device, dtype=torch.int64).type_as(self.inv_freq)
59
+
60
+ freqs = torch.outer(t, self.inv_freq)
61
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
62
+ emb = torch.cat((freqs, freqs), dim=-1)
63
+ self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
64
+ self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
65
+
66
+ def forward(self, x, seq_len=None):
67
+ # x: [bs, num_attention_heads, seq_len, head_size]
68
+ if seq_len > self.max_seq_len_cached:
69
+ self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)
70
+
71
+ return (
72
+ self.cos_cached[:seq_len].to(dtype=x.dtype),
73
+ self.sin_cached[:seq_len].to(dtype=x.dtype),
74
+ )
75
+
76
+
77
+ class T2IFinalLayer(nn.Module):
78
+ """
79
+ The final layer of Sana.
80
+ """
81
+
82
+ def __init__(self, hidden_size, patch_size=[16, 1], out_channels=256):
83
+ super().__init__()
84
+ self.norm_final = nn.RMSNorm(hidden_size, elementwise_affine=False, eps=1e-6)
85
+ self.linear = nn.Linear(hidden_size, patch_size[0] * patch_size[1] * out_channels, bias=True)
86
+ self.scale_shift_table = nn.Parameter(torch.randn(2, hidden_size) / hidden_size**0.5)
87
+ self.out_channels = out_channels
88
+ self.patch_size = patch_size
89
+
90
+ def unpatchfy(
91
+ self,
92
+ hidden_states: torch.Tensor,
93
+ width: int,
94
+ ):
95
+ # 4 unpatchify
96
+ new_height, new_width = 1, hidden_states.size(1)
97
+ hidden_states = hidden_states.reshape(
98
+ shape=(hidden_states.shape[0], new_height, new_width, self.patch_size[0], self.patch_size[1], self.out_channels)
99
+ ).contiguous()
100
+ hidden_states = torch.einsum("nhwpqc->nchpwq", hidden_states)
101
+ output = hidden_states.reshape(
102
+ shape=(hidden_states.shape[0], self.out_channels, new_height * self.patch_size[0], new_width * self.patch_size[1])
103
+ ).contiguous()
104
+ if width > new_width:
105
+ output = torch.nn.functional.pad(output, (0, width - new_width, 0, 0), 'constant', 0)
106
+ elif width < new_width:
107
+ output = output[:, :, :, :width]
108
+ return output
109
+
110
+ def forward(self, x, t, output_length):
111
+ shift, scale = (self.scale_shift_table[None] + t[:, None]).chunk(2, dim=1)
112
+ x = t2i_modulate(self.norm_final(x), shift, scale)
113
+ x = self.linear(x)
114
+ # unpatchify
115
+ output = self.unpatchfy(x, output_length)
116
+ return output
117
+
118
+
119
+ class PatchEmbed(nn.Module):
120
+ """2D Image to Patch Embedding"""
121
+
122
+ def __init__(
123
+ self,
124
+ height=16,
125
+ width=4096,
126
+ patch_size=(16, 1),
127
+ in_channels=8,
128
+ embed_dim=1152,
129
+ bias=True,
130
+ ):
131
+ super().__init__()
132
+ patch_size_h, patch_size_w = patch_size
133
+ self.early_conv_layers = nn.Sequential(
134
+ nn.Conv2d(in_channels, in_channels*256, kernel_size=patch_size, stride=patch_size, padding=0, bias=bias),
135
+ torch.nn.GroupNorm(num_groups=32, num_channels=in_channels*256, eps=1e-6, affine=True),
136
+ nn.Conv2d(in_channels*256, embed_dim, kernel_size=1, stride=1, padding=0, bias=bias)
137
+ )
138
+ self.patch_size = patch_size
139
+ self.height, self.width = height // patch_size_h, width // patch_size_w
140
+ self.base_size = self.width
141
+
142
+ def forward(self, latent):
143
+ # early convolutions, N x C x H x W -> N x 256 * sqrt(patch_size) x H/patch_size x W/patch_size
144
+ latent = self.early_conv_layers(latent)
145
+ latent = latent.flatten(2).transpose(1, 2) # BCHW -> BNC
146
+ return latent
147
+
148
+
149
+ @dataclass
150
+ class Transformer2DModelOutput(BaseOutput):
151
+
152
+ sample: torch.FloatTensor
153
+ proj_losses: Optional[Tuple[Tuple[str, torch.Tensor]]] = None
154
+
155
+
156
+ class ACEStepTransformer2DModel(ModelMixin, ConfigMixin, PeftAdapterMixin, FromOriginalModelMixin):
157
+ _supports_gradient_checkpointing = True
158
+
159
+ @register_to_config
160
+ def __init__(
161
+ self,
162
+ in_channels: Optional[int] = 8,
163
+ num_layers: int = 28,
164
+ inner_dim: int = 1536,
165
+ attention_head_dim: int = 64,
166
+ num_attention_heads: int = 24,
167
+ mlp_ratio: float = 4.0,
168
+ out_channels: int = 8,
169
+ max_position: int = 32768,
170
+ rope_theta: float = 1000000.0,
171
+ speaker_embedding_dim: int = 512,
172
+ text_embedding_dim: int = 768,
173
+ ssl_encoder_depths: List[int] = [9, 9],
174
+ ssl_names: List[str] = ["mert", "m-hubert"],
175
+ ssl_latent_dims: List[int] = [1024, 768],
176
+ lyric_encoder_vocab_size: int = 6681,
177
+ lyric_hidden_size: int = 1024,
178
+ patch_size: List[int] = [16, 1],
179
+ max_height: int = 16,
180
+ max_width: int = 4096,
181
+ **kwargs,
182
+ ):
183
+ super().__init__()
184
+
185
+ self.num_attention_heads = num_attention_heads
186
+ self.attention_head_dim = attention_head_dim
187
+ inner_dim = num_attention_heads * attention_head_dim
188
+ self.inner_dim = inner_dim
189
+ self.out_channels = out_channels
190
+ self.max_position = max_position
191
+ self.patch_size = patch_size
192
+
193
+ self.rope_theta = rope_theta
194
+
195
+ self.rotary_emb = Qwen2RotaryEmbedding(
196
+ dim=self.attention_head_dim,
197
+ max_position_embeddings=self.max_position,
198
+ base=self.rope_theta,
199
+ )
200
+
201
+ # 2. Define input layers
202
+ self.in_channels = in_channels
203
+
204
+ # 3. Define transformers blocks
205
+ self.transformer_blocks = nn.ModuleList(
206
+ [
207
+ LinearTransformerBlock(
208
+ dim=self.inner_dim,
209
+ num_attention_heads=self.num_attention_heads,
210
+ attention_head_dim=attention_head_dim,
211
+ mlp_ratio=mlp_ratio,
212
+ add_cross_attention=True,
213
+ add_cross_attention_dim=self.inner_dim,
214
+ )
215
+ for i in range(self.config.num_layers)
216
+ ]
217
+ )
218
+ self.num_layers = num_layers
219
+
220
+ self.time_proj = Timesteps(num_channels=256, flip_sin_to_cos=True, downscale_freq_shift=0)
221
+ self.timestep_embedder = TimestepEmbedding(in_channels=256, time_embed_dim=self.inner_dim)
222
+ self.t_block = nn.Sequential(nn.SiLU(), nn.Linear(self.inner_dim, 6 * self.inner_dim, bias=True))
223
+
224
+ # speaker
225
+ self.speaker_embedder = nn.Linear(speaker_embedding_dim, self.inner_dim)
226
+
227
+ # genre
228
+ self.genre_embedder = nn.Linear(text_embedding_dim, self.inner_dim)
229
+
230
+ # lyric
231
+ self.lyric_embs = nn.Embedding(lyric_encoder_vocab_size, lyric_hidden_size)
232
+ self.lyric_encoder = LyricEncoder(input_size=lyric_hidden_size, static_chunk_size=0)
233
+ self.lyric_proj = nn.Linear(lyric_hidden_size, self.inner_dim)
234
+
235
+ projector_dim = 2 * self.inner_dim
236
+
237
+ self.projectors = nn.ModuleList([
238
+ nn.Sequential(
239
+ nn.Linear(self.inner_dim, projector_dim),
240
+ nn.SiLU(),
241
+ nn.Linear(projector_dim, projector_dim),
242
+ nn.SiLU(),
243
+ nn.Linear(projector_dim, ssl_dim),
244
+ ) for ssl_dim in ssl_latent_dims
245
+ ])
246
+
247
+ self.ssl_latent_dims = ssl_latent_dims
248
+ self.ssl_encoder_depths = ssl_encoder_depths
249
+
250
+ self.cosine_loss = torch.nn.CosineEmbeddingLoss(margin=0.0, reduction='mean')
251
+ self.ssl_names = ssl_names
252
+
253
+ self.proj_in = PatchEmbed(
254
+ height=max_height,
255
+ width=max_width,
256
+ patch_size=patch_size,
257
+ embed_dim=self.inner_dim,
258
+ bias=True,
259
+ )
260
+
261
+ self.final_layer = T2IFinalLayer(self.inner_dim, patch_size=patch_size, out_channels=out_channels)
262
+ self.gradient_checkpointing = False
263
+
264
+ # Copied from diffusers.models.unets.unet_3d_condition.UNet3DConditionModel.enable_forward_chunking
265
+ def enable_forward_chunking(self, chunk_size: Optional[int] = None, dim: int = 0) -> None:
266
+ """
267
+ Sets the attention processor to use [feed forward
268
+ chunking](https://huggingface.co/blog/reformer#2-chunked-feed-forward-layers).
269
+
270
+ Parameters:
271
+ chunk_size (`int`, *optional*):
272
+ The chunk size of the feed-forward layers. If not specified, will run feed-forward layer individually
273
+ over each tensor of dim=`dim`.
274
+ dim (`int`, *optional*, defaults to `0`):
275
+ The dimension over which the feed-forward computation should be chunked. Choose between dim=0 (batch)
276
+ or dim=1 (sequence length).
277
+ """
278
+ if dim not in [0, 1]:
279
+ raise ValueError(f"Make sure to set `dim` to either 0 or 1, not {dim}")
280
+
281
+ # By default chunk size is 1
282
+ chunk_size = chunk_size or 1
283
+
284
+ def fn_recursive_feed_forward(module: torch.nn.Module, chunk_size: int, dim: int):
285
+ if hasattr(module, "set_chunk_feed_forward"):
286
+ module.set_chunk_feed_forward(chunk_size=chunk_size, dim=dim)
287
+
288
+ for child in module.children():
289
+ fn_recursive_feed_forward(child, chunk_size, dim)
290
+
291
+ for module in self.children():
292
+ fn_recursive_feed_forward(module, chunk_size, dim)
293
+
294
+ def _set_gradient_checkpointing(self, module, value=False):
295
+ if hasattr(module, "gradient_checkpointing"):
296
+ module.gradient_checkpointing = value
297
+
298
+ def forward_lyric_encoder(
299
+ self,
300
+ lyric_token_idx: Optional[torch.LongTensor] = None,
301
+ lyric_mask: Optional[torch.LongTensor] = None,
302
+ ):
303
+ # N x T x D
304
+ lyric_embs = self.lyric_embs(lyric_token_idx)
305
+ prompt_prenet_out, _mask = self.lyric_encoder(lyric_embs, lyric_mask, decoding_chunk_size=1, num_decoding_left_chunks=-1)
306
+ prompt_prenet_out = self.lyric_proj(prompt_prenet_out)
307
+ return prompt_prenet_out
308
+
309
+ def encode(
310
+ self,
311
+ encoder_text_hidden_states: Optional[torch.Tensor] = None,
312
+ text_attention_mask: Optional[torch.LongTensor] = None,
313
+ speaker_embeds: Optional[torch.FloatTensor] = None,
314
+ lyric_token_idx: Optional[torch.LongTensor] = None,
315
+ lyric_mask: Optional[torch.LongTensor] = None,
316
+ ):
317
+
318
+ bs = encoder_text_hidden_states.shape[0]
319
+ device = encoder_text_hidden_states.device
320
+
321
+ # speaker embedding
322
+ encoder_spk_hidden_states = self.speaker_embedder(speaker_embeds).unsqueeze(1)
323
+ speaker_mask = torch.ones(bs, 1, device=device)
324
+
325
+ # genre embedding
326
+ encoder_text_hidden_states = self.genre_embedder(encoder_text_hidden_states)
327
+
328
+ # lyric
329
+ encoder_lyric_hidden_states = self.forward_lyric_encoder(
330
+ lyric_token_idx=lyric_token_idx,
331
+ lyric_mask=lyric_mask,
332
+ )
333
+
334
+ encoder_hidden_states = torch.cat([encoder_spk_hidden_states, encoder_text_hidden_states, encoder_lyric_hidden_states], dim=1)
335
+ encoder_hidden_mask = torch.cat([speaker_mask, text_attention_mask, lyric_mask], dim=1)
336
+ return encoder_hidden_states, encoder_hidden_mask
337
+
338
+ def decode(
339
+ self,
340
+ hidden_states: torch.Tensor,
341
+ attention_mask: torch.Tensor,
342
+ encoder_hidden_states: torch.Tensor,
343
+ encoder_hidden_mask: torch.Tensor,
344
+ timestep: Optional[torch.Tensor],
345
+ ssl_hidden_states: Optional[List[torch.Tensor]] = None,
346
+ output_length: int = 0,
347
+ block_controlnet_hidden_states: Optional[Union[List[torch.Tensor], torch.Tensor]] = None,
348
+ controlnet_scale: Union[float, torch.Tensor] = 1.0,
349
+ return_dict: bool = True,
350
+ ):
351
+
352
+ embedded_timestep = self.timestep_embedder(self.time_proj(timestep).to(dtype=hidden_states.dtype))
353
+ temb = self.t_block(embedded_timestep)
354
+
355
+ hidden_states = self.proj_in(hidden_states)
356
+
357
+ # controlnet logic
358
+ if block_controlnet_hidden_states is not None:
359
+ control_condi = cross_norm(hidden_states, block_controlnet_hidden_states)
360
+ hidden_states = hidden_states + control_condi * controlnet_scale
361
+
362
+ inner_hidden_states = []
363
+
364
+ rotary_freqs_cis = self.rotary_emb(hidden_states, seq_len=hidden_states.shape[1])
365
+ encoder_rotary_freqs_cis = self.rotary_emb(encoder_hidden_states, seq_len=encoder_hidden_states.shape[1])
366
+
367
+ for index_block, block in enumerate(self.transformer_blocks):
368
+
369
+ if self.training and self.gradient_checkpointing:
370
+
371
+ def create_custom_forward(module, return_dict=None):
372
+ def custom_forward(*inputs):
373
+ if return_dict is not None:
374
+ return module(*inputs, return_dict=return_dict)
375
+ else:
376
+ return module(*inputs)
377
+
378
+ return custom_forward
379
+
380
+ ckpt_kwargs: Dict[str, Any] = {"use_reentrant": False} if is_torch_version(">=", "1.11.0") else {}
381
+ hidden_states = torch.utils.checkpoint.checkpoint(
382
+ create_custom_forward(block),
383
+ hidden_states=hidden_states,
384
+ attention_mask=attention_mask,
385
+ encoder_hidden_states=encoder_hidden_states,
386
+ encoder_attention_mask=encoder_hidden_mask,
387
+ rotary_freqs_cis=rotary_freqs_cis,
388
+ rotary_freqs_cis_cross=encoder_rotary_freqs_cis,
389
+ temb=temb,
390
+ **ckpt_kwargs,
391
+ )
392
+
393
+ else:
394
+ hidden_states = block(
395
+ hidden_states=hidden_states,
396
+ attention_mask=attention_mask,
397
+ encoder_hidden_states=encoder_hidden_states,
398
+ encoder_attention_mask=encoder_hidden_mask,
399
+ rotary_freqs_cis=rotary_freqs_cis,
400
+ rotary_freqs_cis_cross=encoder_rotary_freqs_cis,
401
+ temb=temb,
402
+ )
403
+
404
+ for ssl_encoder_depth in self.ssl_encoder_depths:
405
+ if index_block == ssl_encoder_depth:
406
+ inner_hidden_states.append(hidden_states)
407
+
408
+ proj_losses = []
409
+ if len(inner_hidden_states) > 0 and ssl_hidden_states is not None and len(ssl_hidden_states) > 0:
410
+
411
+ for inner_hidden_state, projector, ssl_hidden_state, ssl_name in zip(inner_hidden_states, self.projectors, ssl_hidden_states, self.ssl_names):
412
+ if ssl_hidden_state is None:
413
+ continue
414
+ # 1. N x T x D1 -> N x D x D2
415
+ est_ssl_hidden_state = projector(inner_hidden_state)
416
+ # 3. projection loss
417
+ bs = inner_hidden_state.shape[0]
418
+ proj_loss = 0.0
419
+ for i, (z, z_tilde) in enumerate(zip(ssl_hidden_state, est_ssl_hidden_state)):
420
+ # 2. interpolate
421
+ z_tilde = F.interpolate(z_tilde.unsqueeze(0).transpose(1, 2), size=len(z), mode='linear', align_corners=False).transpose(1, 2).squeeze(0)
422
+
423
+ z_tilde = torch.nn.functional.normalize(z_tilde, dim=-1)
424
+ z = torch.nn.functional.normalize(z, dim=-1)
425
+ # T x d -> T x 1 -> 1
426
+ target = torch.ones(z.shape[0], device=z.device)
427
+ proj_loss += self.cosine_loss(z, z_tilde, target)
428
+ proj_losses.append((ssl_name, proj_loss / bs))
429
+
430
+ output = self.final_layer(hidden_states, embedded_timestep, output_length)
431
+ if not return_dict:
432
+ return (output, proj_losses)
433
+
434
+ return Transformer2DModelOutput(sample=output, proj_losses=proj_losses)
435
+
436
+ # @torch.compile
437
+ def forward(
438
+ self,
439
+ hidden_states: torch.Tensor,
440
+ attention_mask: torch.Tensor,
441
+ encoder_text_hidden_states: Optional[torch.Tensor] = None,
442
+ text_attention_mask: Optional[torch.LongTensor] = None,
443
+ speaker_embeds: Optional[torch.FloatTensor] = None,
444
+ lyric_token_idx: Optional[torch.LongTensor] = None,
445
+ lyric_mask: Optional[torch.LongTensor] = None,
446
+ timestep: Optional[torch.Tensor] = None,
447
+ ssl_hidden_states: Optional[List[torch.Tensor]] = None,
448
+ block_controlnet_hidden_states: Optional[Union[List[torch.Tensor], torch.Tensor]] = None,
449
+ controlnet_scale: Union[float, torch.Tensor] = 1.0,
450
+ return_dict: bool = True,
451
+ ):
452
+ encoder_hidden_states, encoder_hidden_mask = self.encode(
453
+ encoder_text_hidden_states=encoder_text_hidden_states,
454
+ text_attention_mask=text_attention_mask,
455
+ speaker_embeds=speaker_embeds,
456
+ lyric_token_idx=lyric_token_idx,
457
+ lyric_mask=lyric_mask,
458
+ )
459
+
460
+ output_length = hidden_states.shape[-1]
461
+
462
+ output = self.decode(
463
+ hidden_states=hidden_states,
464
+ attention_mask=attention_mask,
465
+ encoder_hidden_states=encoder_hidden_states,
466
+ encoder_hidden_mask=encoder_hidden_mask,
467
+ timestep=timestep,
468
+ ssl_hidden_states=ssl_hidden_states,
469
+ output_length=output_length,
470
+ block_controlnet_hidden_states=block_controlnet_hidden_states,
471
+ controlnet_scale=controlnet_scale,
472
+ return_dict=return_dict,
473
+ )
474
+
475
+ return output
models/attention.py ADDED
@@ -0,0 +1,319 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2024 The HuggingFace Team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ from typing import Tuple, Union
15
+
16
+ import torch
17
+ import torch.nn.functional as F
18
+ from torch import nn
19
+
20
+ from diffusers.utils import logging
21
+ from diffusers.models.normalization import RMSNorm
22
+
23
+
24
+ try:
25
+ # from .dcformer import DCMHAttention
26
+ from .customer_attention_processor import Attention, CustomLiteLAProcessor2_0, CustomerAttnProcessor2_0
27
+ except ImportError:
28
+ # from dcformer import DCMHAttention
29
+ from customer_attention_processor import Attention, CustomLiteLAProcessor2_0, CustomerAttnProcessor2_0
30
+
31
+
32
+ logger = logging.get_logger(__name__)
33
+
34
+
35
+ def val2list(x: list or tuple or any, repeat_time=1) -> list: # type: ignore
36
+ """Repeat `val` for `repeat_time` times and return the list or val if list/tuple."""
37
+ if isinstance(x, (list, tuple)):
38
+ return list(x)
39
+ return [x for _ in range(repeat_time)]
40
+
41
+
42
+ def val2tuple(x: list or tuple or any, min_len: int = 1, idx_repeat: int = -1) -> tuple: # type: ignore
43
+ """Return tuple with min_len by repeating element at idx_repeat."""
44
+ # convert to list first
45
+ x = val2list(x)
46
+
47
+ # repeat elements if necessary
48
+ if len(x) > 0:
49
+ x[idx_repeat:idx_repeat] = [x[idx_repeat] for _ in range(min_len - len(x))]
50
+
51
+ return tuple(x)
52
+
53
+
54
+ def t2i_modulate(x, shift, scale):
55
+ return x * (1 + scale) + shift
56
+
57
+
58
+ def get_same_padding(kernel_size: Union[int, Tuple[int, ...]]) -> Union[int, Tuple[int, ...]]:
59
+ if isinstance(kernel_size, tuple):
60
+ return tuple([get_same_padding(ks) for ks in kernel_size])
61
+ else:
62
+ assert kernel_size % 2 > 0, f"kernel size {kernel_size} should be odd number"
63
+ return kernel_size // 2
64
+
65
+ class ConvLayer(nn.Module):
66
+ def __init__(
67
+ self,
68
+ in_dim: int,
69
+ out_dim: int,
70
+ kernel_size=3,
71
+ stride=1,
72
+ dilation=1,
73
+ groups=1,
74
+ padding: Union[int, None] = None,
75
+ use_bias=False,
76
+ norm=None,
77
+ act=None,
78
+ ):
79
+ super().__init__()
80
+ if padding is None:
81
+ padding = get_same_padding(kernel_size)
82
+ padding *= dilation
83
+
84
+ self.in_dim = in_dim
85
+ self.out_dim = out_dim
86
+ self.kernel_size = kernel_size
87
+ self.stride = stride
88
+ self.dilation = dilation
89
+ self.groups = groups
90
+ self.padding = padding
91
+ self.use_bias = use_bias
92
+
93
+ self.conv = nn.Conv1d(
94
+ in_dim,
95
+ out_dim,
96
+ kernel_size=kernel_size,
97
+ stride=stride,
98
+ padding=padding,
99
+ dilation=dilation,
100
+ groups=groups,
101
+ bias=use_bias,
102
+ )
103
+ if norm is not None:
104
+ self.norm = RMSNorm(out_dim, elementwise_affine=False)
105
+ else:
106
+ self.norm = None
107
+ if act is not None:
108
+ self.act = nn.SiLU(inplace=True)
109
+ else:
110
+ self.act = None
111
+
112
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
113
+ x = self.conv(x)
114
+ if self.norm:
115
+ x = self.norm(x)
116
+ if self.act:
117
+ x = self.act(x)
118
+ return x
119
+
120
+
121
+ class GLUMBConv(nn.Module):
122
+ def __init__(
123
+ self,
124
+ in_features: int,
125
+ hidden_features: int,
126
+ out_feature=None,
127
+ kernel_size=3,
128
+ stride=1,
129
+ padding: Union[int, None] = None,
130
+ use_bias=False,
131
+ norm=(None, None, None),
132
+ act=("silu", "silu", None),
133
+ dilation=1,
134
+ ):
135
+ out_feature = out_feature or in_features
136
+ super().__init__()
137
+ use_bias = val2tuple(use_bias, 3)
138
+ norm = val2tuple(norm, 3)
139
+ act = val2tuple(act, 3)
140
+
141
+ self.glu_act = nn.SiLU(inplace=False)
142
+ self.inverted_conv = ConvLayer(
143
+ in_features,
144
+ hidden_features * 2,
145
+ 1,
146
+ use_bias=use_bias[0],
147
+ norm=norm[0],
148
+ act=act[0],
149
+ )
150
+ self.depth_conv = ConvLayer(
151
+ hidden_features * 2,
152
+ hidden_features * 2,
153
+ kernel_size,
154
+ stride=stride,
155
+ groups=hidden_features * 2,
156
+ padding=padding,
157
+ use_bias=use_bias[1],
158
+ norm=norm[1],
159
+ act=None,
160
+ dilation=dilation,
161
+ )
162
+ self.point_conv = ConvLayer(
163
+ hidden_features,
164
+ out_feature,
165
+ 1,
166
+ use_bias=use_bias[2],
167
+ norm=norm[2],
168
+ act=act[2],
169
+ )
170
+
171
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
172
+ x = x.transpose(1, 2)
173
+ x = self.inverted_conv(x)
174
+ x = self.depth_conv(x)
175
+
176
+ x, gate = torch.chunk(x, 2, dim=1)
177
+ gate = self.glu_act(gate)
178
+ x = x * gate
179
+
180
+ x = self.point_conv(x)
181
+ x = x.transpose(1, 2)
182
+
183
+ return x
184
+
185
+
186
+ class LinearTransformerBlock(nn.Module):
187
+ """
188
+ A Sana block with global shared adaptive layer norm (adaLN-single) conditioning.
189
+ """
190
+ def __init__(
191
+ self,
192
+ dim,
193
+ num_attention_heads,
194
+ attention_head_dim,
195
+ use_adaln_single=True,
196
+ cross_attention_dim=None,
197
+ added_kv_proj_dim=None,
198
+ context_pre_only=False,
199
+ mlp_ratio=4.0,
200
+ add_cross_attention=False,
201
+ add_cross_attention_dim=None,
202
+ qk_norm=None,
203
+ ):
204
+ super().__init__()
205
+
206
+ self.norm1 = RMSNorm(dim, elementwise_affine=False, eps=1e-6)
207
+ self.attn = Attention(
208
+ query_dim=dim,
209
+ cross_attention_dim=cross_attention_dim,
210
+ added_kv_proj_dim=added_kv_proj_dim,
211
+ dim_head=attention_head_dim,
212
+ heads=num_attention_heads,
213
+ out_dim=dim,
214
+ bias=True,
215
+ qk_norm=qk_norm,
216
+ processor=CustomLiteLAProcessor2_0(),
217
+ )
218
+
219
+ self.add_cross_attention = add_cross_attention
220
+ self.context_pre_only = context_pre_only
221
+
222
+ if add_cross_attention and add_cross_attention_dim is not None:
223
+ self.cross_attn = Attention(
224
+ query_dim=dim,
225
+ cross_attention_dim=add_cross_attention_dim,
226
+ added_kv_proj_dim=add_cross_attention_dim,
227
+ dim_head=attention_head_dim,
228
+ heads=num_attention_heads,
229
+ out_dim=dim,
230
+ context_pre_only=context_pre_only,
231
+ bias=True,
232
+ qk_norm=qk_norm,
233
+ processor=CustomerAttnProcessor2_0(),
234
+ )
235
+
236
+ self.norm2 = RMSNorm(dim, 1e-06, elementwise_affine=False)
237
+
238
+ self.ff = GLUMBConv(
239
+ in_features=dim,
240
+ hidden_features=int(dim * mlp_ratio),
241
+ use_bias=(True, True, False),
242
+ norm=(None, None, None),
243
+ act=("silu", "silu", None),
244
+ )
245
+ self.use_adaln_single = use_adaln_single
246
+ if use_adaln_single:
247
+ self.scale_shift_table = nn.Parameter(torch.randn(6, dim) / dim**0.5)
248
+
249
+ def forward(
250
+ self,
251
+ hidden_states: torch.FloatTensor,
252
+ encoder_hidden_states: torch.FloatTensor = None,
253
+ attention_mask: torch.FloatTensor = None,
254
+ encoder_attention_mask: torch.FloatTensor = None,
255
+ rotary_freqs_cis: Union[torch.Tensor, Tuple[torch.Tensor]] = None,
256
+ rotary_freqs_cis_cross: Union[torch.Tensor, Tuple[torch.Tensor]] = None,
257
+ temb: torch.FloatTensor = None,
258
+ ):
259
+
260
+ N = hidden_states.shape[0]
261
+
262
+ # step 1: AdaLN single
263
+ if self.use_adaln_single:
264
+ shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp = (
265
+ self.scale_shift_table[None] + temb.reshape(N, 6, -1)
266
+ ).chunk(6, dim=1)
267
+
268
+ norm_hidden_states = self.norm1(hidden_states)
269
+ if self.use_adaln_single:
270
+ norm_hidden_states = norm_hidden_states * (1 + scale_msa) + shift_msa
271
+
272
+ # step 2: attention
273
+ if not self.add_cross_attention:
274
+ attn_output, encoder_hidden_states = self.attn(
275
+ hidden_states=norm_hidden_states,
276
+ attention_mask=attention_mask,
277
+ encoder_hidden_states=encoder_hidden_states,
278
+ encoder_attention_mask=encoder_attention_mask,
279
+ rotary_freqs_cis=rotary_freqs_cis,
280
+ rotary_freqs_cis_cross=rotary_freqs_cis_cross,
281
+ )
282
+ else:
283
+ attn_output, _ = self.attn(
284
+ hidden_states=norm_hidden_states,
285
+ attention_mask=attention_mask,
286
+ encoder_hidden_states=None,
287
+ encoder_attention_mask=None,
288
+ rotary_freqs_cis=rotary_freqs_cis,
289
+ rotary_freqs_cis_cross=None,
290
+ )
291
+
292
+ if self.use_adaln_single:
293
+ attn_output = gate_msa * attn_output
294
+ hidden_states = attn_output + hidden_states
295
+
296
+ if self.add_cross_attention:
297
+ attn_output = self.cross_attn(
298
+ hidden_states=hidden_states,
299
+ attention_mask=attention_mask,
300
+ encoder_hidden_states=encoder_hidden_states,
301
+ encoder_attention_mask=encoder_attention_mask,
302
+ rotary_freqs_cis=rotary_freqs_cis,
303
+ rotary_freqs_cis_cross=rotary_freqs_cis_cross,
304
+ )
305
+ hidden_states = attn_output + hidden_states
306
+
307
+ # step 3: add norm
308
+ norm_hidden_states = self.norm2(hidden_states)
309
+ if self.use_adaln_single:
310
+ norm_hidden_states = norm_hidden_states * (1 + scale_mlp) + shift_mlp
311
+
312
+ # step 4: feed forward
313
+ ff_output = self.ff(norm_hidden_states)
314
+ if self.use_adaln_single:
315
+ ff_output = gate_mlp * ff_output
316
+
317
+ hidden_states = hidden_states + ff_output
318
+
319
+ return hidden_states
models/config.json ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_class_name": "Transformer2DModel",
3
+ "_diffusers_version": "0.27.2",
4
+ "in_channels": 8,
5
+ "num_layers": 24,
6
+ "inner_dim": 2560,
7
+ "attention_head_dim": 128,
8
+ "num_attention_heads": 20,
9
+ "mlp_ratio": 2.5,
10
+ "out_channels": 8,
11
+ "max_position": 32768,
12
+ "rope_theta": 1000000.0,
13
+ "speaker_embedding_dim": 512,
14
+ "text_embedding_dim": 768,
15
+ "ssl_encoder_depths": [8, 8],
16
+ "ssl_names": ["mert", "m-hubert"],
17
+ "ssl_latent_dims": [1024, 768],
18
+ "patch_size": [16, 1],
19
+ "max_height": 16,
20
+ "max_width": 32768,
21
+ "lyric_encoder_vocab_size": 6693,
22
+ "lyric_hidden_size": 1024
23
+ }
models/customer_attention_processor.py ADDED
@@ -0,0 +1,339 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2024 The HuggingFace Team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ from typing import Optional, Union, Tuple
15
+
16
+ import torch
17
+ import torch.nn.functional as F
18
+ from torch import nn
19
+
20
+ from diffusers.utils import logging
21
+ from diffusers.models.attention_processor import Attention
22
+
23
+ logger = logging.get_logger(__name__) # pylint: disable=invalid-name
24
+
25
+
26
+ class CustomLiteLAProcessor2_0:
27
+ """Attention processor used typically in processing the SD3-like self-attention projections. add rms norm for query and key and apply RoPE"""
28
+
29
+ def __init__(self):
30
+ self.kernel_func = nn.ReLU(inplace=False)
31
+ self.eps = 1e-15
32
+ self.pad_val = 1.0
33
+
34
+ def apply_rotary_emb(
35
+ self,
36
+ x: torch.Tensor,
37
+ freqs_cis: Union[torch.Tensor, Tuple[torch.Tensor]],
38
+ ) -> Tuple[torch.Tensor, torch.Tensor]:
39
+ """
40
+ Apply rotary embeddings to input tensors using the given frequency tensor. This function applies rotary embeddings
41
+ to the given query or key 'x' tensors using the provided frequency tensor 'freqs_cis'. The input tensors are
42
+ reshaped as complex numbers, and the frequency tensor is reshaped for broadcasting compatibility. The resulting
43
+ tensors contain rotary embeddings and are returned as real tensors.
44
+
45
+ Args:
46
+ x (`torch.Tensor`):
47
+ Query or key tensor to apply rotary embeddings. [B, H, S, D] xk (torch.Tensor): Key tensor to apply
48
+ freqs_cis (`Tuple[torch.Tensor]`): Precomputed frequency tensor for complex exponentials. ([S, D], [S, D],)
49
+
50
+ Returns:
51
+ Tuple[torch.Tensor, torch.Tensor]: Tuple of modified query tensor and key tensor with rotary embeddings.
52
+ """
53
+ cos, sin = freqs_cis # [S, D]
54
+ cos = cos[None, None]
55
+ sin = sin[None, None]
56
+ cos, sin = cos.to(x.device), sin.to(x.device)
57
+
58
+ x_real, x_imag = x.reshape(*x.shape[:-1], -1, 2).unbind(-1) # [B, S, H, D//2]
59
+ x_rotated = torch.stack([-x_imag, x_real], dim=-1).flatten(3)
60
+ out = (x.float() * cos + x_rotated.float() * sin).to(x.dtype)
61
+
62
+ return out
63
+
64
+ def __call__(
65
+ self,
66
+ attn: Attention,
67
+ hidden_states: torch.FloatTensor,
68
+ encoder_hidden_states: torch.FloatTensor = None,
69
+ attention_mask: Optional[torch.FloatTensor] = None,
70
+ encoder_attention_mask: Optional[torch.FloatTensor] = None,
71
+ rotary_freqs_cis: Union[torch.Tensor, Tuple[torch.Tensor]] = None,
72
+ rotary_freqs_cis_cross: Union[torch.Tensor, Tuple[torch.Tensor]] = None,
73
+ *args,
74
+ **kwargs,
75
+ ) -> torch.FloatTensor:
76
+ hidden_states_len = hidden_states.shape[1]
77
+
78
+ input_ndim = hidden_states.ndim
79
+ if input_ndim == 4:
80
+ batch_size, channel, height, width = hidden_states.shape
81
+ hidden_states = hidden_states.view(batch_size, channel, height * width).transpose(1, 2)
82
+ if encoder_hidden_states is not None:
83
+ context_input_ndim = encoder_hidden_states.ndim
84
+ if context_input_ndim == 4:
85
+ batch_size, channel, height, width = encoder_hidden_states.shape
86
+ encoder_hidden_states = encoder_hidden_states.view(batch_size, channel, height * width).transpose(1, 2)
87
+
88
+ batch_size = hidden_states.shape[0]
89
+
90
+ # `sample` projections.
91
+ dtype = hidden_states.dtype
92
+ query = attn.to_q(hidden_states)
93
+ key = attn.to_k(hidden_states)
94
+ value = attn.to_v(hidden_states)
95
+
96
+ # `context` projections.
97
+ has_encoder_hidden_state_proj = hasattr(attn, "add_q_proj") and hasattr(attn, "add_k_proj") and hasattr(attn, "add_v_proj")
98
+ if encoder_hidden_states is not None and has_encoder_hidden_state_proj:
99
+ encoder_hidden_states_query_proj = attn.add_q_proj(encoder_hidden_states)
100
+ encoder_hidden_states_key_proj = attn.add_k_proj(encoder_hidden_states)
101
+ encoder_hidden_states_value_proj = attn.add_v_proj(encoder_hidden_states)
102
+
103
+ # attention
104
+ if not attn.is_cross_attention:
105
+ query = torch.cat([query, encoder_hidden_states_query_proj], dim=1)
106
+ key = torch.cat([key, encoder_hidden_states_key_proj], dim=1)
107
+ value = torch.cat([value, encoder_hidden_states_value_proj], dim=1)
108
+ else:
109
+ query = hidden_states
110
+ key = encoder_hidden_states
111
+ value = encoder_hidden_states
112
+
113
+ inner_dim = key.shape[-1]
114
+ head_dim = inner_dim // attn.heads
115
+
116
+ query = query.transpose(-1, -2).reshape(batch_size, attn.heads, head_dim, -1)
117
+ key = key.transpose(-1, -2).reshape(batch_size, attn.heads, head_dim, -1).transpose(-1, -2)
118
+ value = value.transpose(-1, -2).reshape(batch_size, attn.heads, head_dim, -1)
119
+
120
+ # RoPE需要 [B, H, S, D] 输入
121
+ # 此时 query是 [B, H, D, S], 需要转成 [B, H, S, D] 才能应用RoPE
122
+ query = query.permute(0, 1, 3, 2) # [B, H, S, D] (从 [B, H, D, S])
123
+
124
+ # Apply query and key normalization if needed
125
+ if attn.norm_q is not None:
126
+ query = attn.norm_q(query)
127
+ if attn.norm_k is not None:
128
+ key = attn.norm_k(key)
129
+
130
+ # Apply RoPE if needed
131
+ if rotary_freqs_cis is not None:
132
+ query = self.apply_rotary_emb(query, rotary_freqs_cis)
133
+ if not attn.is_cross_attention:
134
+ key = self.apply_rotary_emb(key, rotary_freqs_cis)
135
+ elif rotary_freqs_cis_cross is not None and has_encoder_hidden_state_proj:
136
+ key = self.apply_rotary_emb(key, rotary_freqs_cis_cross)
137
+
138
+ # 此时 query是 [B, H, S, D],需要还原成 [B, H, D, S]
139
+ query = query.permute(0, 1, 3, 2) # [B, H, D, S]
140
+
141
+ if attention_mask is not None:
142
+ # attention_mask: [B, S] -> [B, 1, S, 1]
143
+ attention_mask = attention_mask[:, None, :, None].to(key.dtype) # [B, 1, S, 1]
144
+ query = query * attention_mask.permute(0, 1, 3, 2) # [B, H, S, D] * [B, 1, S, 1]
145
+ if not attn.is_cross_attention:
146
+ key = key * attention_mask # key: [B, h, S, D] 与 mask [B, 1, S, 1] 相乘
147
+ value = value * attention_mask.permute(0, 1, 3, 2) # 如果 value 是 [B, h, D, S],那么需调整mask以匹配S维度
148
+
149
+ if attn.is_cross_attention and encoder_attention_mask is not None and has_encoder_hidden_state_proj:
150
+ encoder_attention_mask = encoder_attention_mask[:, None, :, None].to(key.dtype) # [B, 1, S_enc, 1]
151
+ # 此时 key: [B, h, S_enc, D], value: [B, h, D, S_enc]
152
+ key = key * encoder_attention_mask # [B, h, S_enc, D] * [B, 1, S_enc, 1]
153
+ value = value * encoder_attention_mask.permute(0, 1, 3, 2) # [B, h, D, S_enc] * [B, 1, 1, S_enc]
154
+
155
+ query = self.kernel_func(query)
156
+ key = self.kernel_func(key)
157
+
158
+ query, key, value = query.float(), key.float(), value.float()
159
+
160
+ value = F.pad(value, (0, 0, 0, 1), mode="constant", value=self.pad_val)
161
+
162
+ vk = torch.matmul(value, key)
163
+
164
+ hidden_states = torch.matmul(vk, query)
165
+
166
+ if hidden_states.dtype in [torch.float16, torch.bfloat16]:
167
+ hidden_states = hidden_states.float()
168
+
169
+ hidden_states = hidden_states[:, :, :-1] / (hidden_states[:, :, -1:] + self.eps)
170
+
171
+ hidden_states = hidden_states.view(batch_size, attn.heads * head_dim, -1).permute(0, 2, 1)
172
+
173
+ hidden_states = hidden_states.to(dtype)
174
+ if encoder_hidden_states is not None:
175
+ encoder_hidden_states = encoder_hidden_states.to(dtype)
176
+
177
+ # Split the attention outputs.
178
+ if encoder_hidden_states is not None and not attn.is_cross_attention and has_encoder_hidden_state_proj:
179
+ hidden_states, encoder_hidden_states = (
180
+ hidden_states[:, : hidden_states_len],
181
+ hidden_states[:, hidden_states_len:],
182
+ )
183
+
184
+ # linear proj
185
+ hidden_states = attn.to_out[0](hidden_states)
186
+ # dropout
187
+ hidden_states = attn.to_out[1](hidden_states)
188
+ if encoder_hidden_states is not None and not attn.context_pre_only and not attn.is_cross_attention and hasattr(attn, "to_add_out"):
189
+ encoder_hidden_states = attn.to_add_out(encoder_hidden_states)
190
+
191
+ if input_ndim == 4:
192
+ hidden_states = hidden_states.transpose(-1, -2).reshape(batch_size, channel, height, width)
193
+ if encoder_hidden_states is not None and context_input_ndim == 4:
194
+ encoder_hidden_states = encoder_hidden_states.transpose(-1, -2).reshape(batch_size, channel, height, width)
195
+
196
+ if torch.get_autocast_gpu_dtype() == torch.float16:
197
+ hidden_states = hidden_states.clip(-65504, 65504)
198
+ if encoder_hidden_states is not None:
199
+ encoder_hidden_states = encoder_hidden_states.clip(-65504, 65504)
200
+
201
+ return hidden_states, encoder_hidden_states
202
+
203
+
204
+ class CustomerAttnProcessor2_0:
205
+ r"""
206
+ Processor for implementing scaled dot-product attention (enabled by default if you're using PyTorch 2.0).
207
+ """
208
+
209
+ def __init__(self):
210
+ if not hasattr(F, "scaled_dot_product_attention"):
211
+ raise ImportError("AttnProcessor2_0 requires PyTorch 2.0, to use it, please upgrade PyTorch to 2.0.")
212
+
213
+ def apply_rotary_emb(
214
+ self,
215
+ x: torch.Tensor,
216
+ freqs_cis: Union[torch.Tensor, Tuple[torch.Tensor]],
217
+ ) -> Tuple[torch.Tensor, torch.Tensor]:
218
+ """
219
+ Apply rotary embeddings to input tensors using the given frequency tensor. This function applies rotary embeddings
220
+ to the given query or key 'x' tensors using the provided frequency tensor 'freqs_cis'. The input tensors are
221
+ reshaped as complex numbers, and the frequency tensor is reshaped for broadcasting compatibility. The resulting
222
+ tensors contain rotary embeddings and are returned as real tensors.
223
+
224
+ Args:
225
+ x (`torch.Tensor`):
226
+ Query or key tensor to apply rotary embeddings. [B, H, S, D] xk (torch.Tensor): Key tensor to apply
227
+ freqs_cis (`Tuple[torch.Tensor]`): Precomputed frequency tensor for complex exponentials. ([S, D], [S, D],)
228
+
229
+ Returns:
230
+ Tuple[torch.Tensor, torch.Tensor]: Tuple of modified query tensor and key tensor with rotary embeddings.
231
+ """
232
+ cos, sin = freqs_cis # [S, D]
233
+ cos = cos[None, None]
234
+ sin = sin[None, None]
235
+ cos, sin = cos.to(x.device), sin.to(x.device)
236
+
237
+ x_real, x_imag = x.reshape(*x.shape[:-1], -1, 2).unbind(-1) # [B, S, H, D//2]
238
+ x_rotated = torch.stack([-x_imag, x_real], dim=-1).flatten(3)
239
+ out = (x.float() * cos + x_rotated.float() * sin).to(x.dtype)
240
+
241
+ return out
242
+
243
+ def __call__(
244
+ self,
245
+ attn: Attention,
246
+ hidden_states: torch.FloatTensor,
247
+ encoder_hidden_states: torch.FloatTensor = None,
248
+ attention_mask: Optional[torch.FloatTensor] = None,
249
+ encoder_attention_mask: Optional[torch.FloatTensor] = None,
250
+ rotary_freqs_cis: Union[torch.Tensor, Tuple[torch.Tensor]] = None,
251
+ rotary_freqs_cis_cross: Union[torch.Tensor, Tuple[torch.Tensor]] = None,
252
+ *args,
253
+ **kwargs,
254
+ ) -> torch.Tensor:
255
+
256
+ residual = hidden_states
257
+ input_ndim = hidden_states.ndim
258
+
259
+ if input_ndim == 4:
260
+ batch_size, channel, height, width = hidden_states.shape
261
+ hidden_states = hidden_states.view(batch_size, channel, height * width).transpose(1, 2)
262
+
263
+ batch_size, sequence_length, _ = (
264
+ hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states.shape
265
+ )
266
+
267
+ has_encoder_hidden_state_proj = hasattr(attn, "add_q_proj") and hasattr(attn, "add_k_proj") and hasattr(attn, "add_v_proj")
268
+
269
+ if attn.group_norm is not None:
270
+ hidden_states = attn.group_norm(hidden_states.transpose(1, 2)).transpose(1, 2)
271
+
272
+ query = attn.to_q(hidden_states)
273
+
274
+ if encoder_hidden_states is None:
275
+ encoder_hidden_states = hidden_states
276
+ elif attn.norm_cross:
277
+ encoder_hidden_states = attn.norm_encoder_hidden_states(encoder_hidden_states)
278
+
279
+ key = attn.to_k(encoder_hidden_states)
280
+ value = attn.to_v(encoder_hidden_states)
281
+
282
+ inner_dim = key.shape[-1]
283
+ head_dim = inner_dim // attn.heads
284
+
285
+ query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
286
+
287
+ key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
288
+ value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
289
+
290
+ if attn.norm_q is not None:
291
+ query = attn.norm_q(query)
292
+ if attn.norm_k is not None:
293
+ key = attn.norm_k(key)
294
+
295
+ # Apply RoPE if needed
296
+ if rotary_freqs_cis is not None:
297
+ query = self.apply_rotary_emb(query, rotary_freqs_cis)
298
+ if not attn.is_cross_attention:
299
+ key = self.apply_rotary_emb(key, rotary_freqs_cis)
300
+ elif rotary_freqs_cis_cross is not None and has_encoder_hidden_state_proj:
301
+ key = self.apply_rotary_emb(key, rotary_freqs_cis_cross)
302
+
303
+ if attn.is_cross_attention and encoder_attention_mask is not None and has_encoder_hidden_state_proj:
304
+ # attention_mask: N x S1
305
+ # encoder_attention_mask: N x S2
306
+ # cross attention 整合attention_mask和encoder_attention_mask
307
+ combined_mask = attention_mask[:, :, None] * encoder_attention_mask[:, None, :]
308
+ attention_mask = torch.where(combined_mask == 1, 0.0, -torch.inf)
309
+ attention_mask = attention_mask[:, None, :, :].expand(-1, attn.heads, -1, -1).to(query.dtype)
310
+
311
+ elif not attn.is_cross_attention and attention_mask is not None:
312
+ attention_mask = attn.prepare_attention_mask(attention_mask, sequence_length, batch_size)
313
+ # scaled_dot_product_attention expects attention_mask shape to be
314
+ # (batch, heads, source_length, target_length)
315
+ attention_mask = attention_mask.view(batch_size, attn.heads, -1, attention_mask.shape[-1])
316
+
317
+ # the output of sdp = (batch, num_heads, seq_len, head_dim)
318
+ # TODO: add support for attn.scale when we move to Torch 2.1
319
+ hidden_states = F.scaled_dot_product_attention(
320
+ query, key, value, attn_mask=attention_mask, dropout_p=0.0, is_causal=False
321
+ )
322
+
323
+ hidden_states = hidden_states.transpose(1, 2).reshape(batch_size, -1, attn.heads * head_dim)
324
+ hidden_states = hidden_states.to(query.dtype)
325
+
326
+ # linear proj
327
+ hidden_states = attn.to_out[0](hidden_states)
328
+ # dropout
329
+ hidden_states = attn.to_out[1](hidden_states)
330
+
331
+ if input_ndim == 4:
332
+ hidden_states = hidden_states.transpose(-1, -2).reshape(batch_size, channel, height, width)
333
+
334
+ if attn.residual_connection:
335
+ hidden_states = hidden_states + residual
336
+
337
+ hidden_states = hidden_states / attn.rescale_output_factor
338
+
339
+ return hidden_states
models/lyrics_utils/lyric_encoder.py ADDED
@@ -0,0 +1,1070 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Optional, Tuple, Union
2
+ import math
3
+ import torch
4
+ from torch import nn
5
+
6
+ class ConvolutionModule(nn.Module):
7
+ """ConvolutionModule in Conformer model."""
8
+
9
+ def __init__(self,
10
+ channels: int,
11
+ kernel_size: int = 15,
12
+ activation: nn.Module = nn.ReLU(),
13
+ norm: str = "batch_norm",
14
+ causal: bool = False,
15
+ bias: bool = True):
16
+ """Construct an ConvolutionModule object.
17
+ Args:
18
+ channels (int): The number of channels of conv layers.
19
+ kernel_size (int): Kernel size of conv layers.
20
+ causal (int): Whether use causal convolution or not
21
+ """
22
+ super().__init__()
23
+
24
+ self.pointwise_conv1 = nn.Conv1d(
25
+ channels,
26
+ 2 * channels,
27
+ kernel_size=1,
28
+ stride=1,
29
+ padding=0,
30
+ bias=bias,
31
+ )
32
+ # self.lorder is used to distinguish if it's a causal convolution,
33
+ # if self.lorder > 0: it's a causal convolution, the input will be
34
+ # padded with self.lorder frames on the left in forward.
35
+ # else: it's a symmetrical convolution
36
+ if causal:
37
+ padding = 0
38
+ self.lorder = kernel_size - 1
39
+ else:
40
+ # kernel_size should be an odd number for none causal convolution
41
+ assert (kernel_size - 1) % 2 == 0
42
+ padding = (kernel_size - 1) // 2
43
+ self.lorder = 0
44
+ self.depthwise_conv = nn.Conv1d(
45
+ channels,
46
+ channels,
47
+ kernel_size,
48
+ stride=1,
49
+ padding=padding,
50
+ groups=channels,
51
+ bias=bias,
52
+ )
53
+
54
+ assert norm in ['batch_norm', 'layer_norm']
55
+ if norm == "batch_norm":
56
+ self.use_layer_norm = False
57
+ self.norm = nn.BatchNorm1d(channels)
58
+ else:
59
+ self.use_layer_norm = True
60
+ self.norm = nn.LayerNorm(channels)
61
+
62
+ self.pointwise_conv2 = nn.Conv1d(
63
+ channels,
64
+ channels,
65
+ kernel_size=1,
66
+ stride=1,
67
+ padding=0,
68
+ bias=bias,
69
+ )
70
+ self.activation = activation
71
+
72
+ def forward(
73
+ self,
74
+ x: torch.Tensor,
75
+ mask_pad: torch.Tensor = torch.ones((0, 0, 0), dtype=torch.bool),
76
+ cache: torch.Tensor = torch.zeros((0, 0, 0)),
77
+ ) -> Tuple[torch.Tensor, torch.Tensor]:
78
+ """Compute convolution module.
79
+ Args:
80
+ x (torch.Tensor): Input tensor (#batch, time, channels).
81
+ mask_pad (torch.Tensor): used for batch padding (#batch, 1, time),
82
+ (0, 0, 0) means fake mask.
83
+ cache (torch.Tensor): left context cache, it is only
84
+ used in causal convolution (#batch, channels, cache_t),
85
+ (0, 0, 0) meas fake cache.
86
+ Returns:
87
+ torch.Tensor: Output tensor (#batch, time, channels).
88
+ """
89
+ # exchange the temporal dimension and the feature dimension
90
+ x = x.transpose(1, 2) # (#batch, channels, time)
91
+
92
+ # mask batch padding
93
+ if mask_pad.size(2) > 0: # time > 0
94
+ x.masked_fill_(~mask_pad, 0.0)
95
+
96
+ if self.lorder > 0:
97
+ if cache.size(2) == 0: # cache_t == 0
98
+ x = nn.functional.pad(x, (self.lorder, 0), 'constant', 0.0)
99
+ else:
100
+ assert cache.size(0) == x.size(0) # equal batch
101
+ assert cache.size(1) == x.size(1) # equal channel
102
+ x = torch.cat((cache, x), dim=2)
103
+ assert (x.size(2) > self.lorder)
104
+ new_cache = x[:, :, -self.lorder:]
105
+ else:
106
+ # It's better we just return None if no cache is required,
107
+ # However, for JIT export, here we just fake one tensor instead of
108
+ # None.
109
+ new_cache = torch.zeros((0, 0, 0), dtype=x.dtype, device=x.device)
110
+
111
+ # GLU mechanism
112
+ x = self.pointwise_conv1(x) # (batch, 2*channel, dim)
113
+ x = nn.functional.glu(x, dim=1) # (batch, channel, dim)
114
+
115
+ # 1D Depthwise Conv
116
+ x = self.depthwise_conv(x)
117
+ if self.use_layer_norm:
118
+ x = x.transpose(1, 2)
119
+ x = self.activation(self.norm(x))
120
+ if self.use_layer_norm:
121
+ x = x.transpose(1, 2)
122
+ x = self.pointwise_conv2(x)
123
+ # mask batch padding
124
+ if mask_pad.size(2) > 0: # time > 0
125
+ x.masked_fill_(~mask_pad, 0.0)
126
+
127
+ return x.transpose(1, 2), new_cache
128
+
129
+ class PositionwiseFeedForward(torch.nn.Module):
130
+ """Positionwise feed forward layer.
131
+
132
+ FeedForward are appied on each position of the sequence.
133
+ The output dim is same with the input dim.
134
+
135
+ Args:
136
+ idim (int): Input dimenstion.
137
+ hidden_units (int): The number of hidden units.
138
+ dropout_rate (float): Dropout rate.
139
+ activation (torch.nn.Module): Activation function
140
+ """
141
+
142
+ def __init__(
143
+ self,
144
+ idim: int,
145
+ hidden_units: int,
146
+ dropout_rate: float,
147
+ activation: torch.nn.Module = torch.nn.ReLU(),
148
+ ):
149
+ """Construct a PositionwiseFeedForward object."""
150
+ super(PositionwiseFeedForward, self).__init__()
151
+ self.w_1 = torch.nn.Linear(idim, hidden_units)
152
+ self.activation = activation
153
+ self.dropout = torch.nn.Dropout(dropout_rate)
154
+ self.w_2 = torch.nn.Linear(hidden_units, idim)
155
+
156
+ def forward(self, xs: torch.Tensor) -> torch.Tensor:
157
+ """Forward function.
158
+
159
+ Args:
160
+ xs: input tensor (B, L, D)
161
+ Returns:
162
+ output tensor, (B, L, D)
163
+ """
164
+ return self.w_2(self.dropout(self.activation(self.w_1(xs))))
165
+
166
+ class Swish(torch.nn.Module):
167
+ """Construct an Swish object."""
168
+
169
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
170
+ """Return Swish activation function."""
171
+ return x * torch.sigmoid(x)
172
+
173
+ class MultiHeadedAttention(nn.Module):
174
+ """Multi-Head Attention layer.
175
+
176
+ Args:
177
+ n_head (int): The number of heads.
178
+ n_feat (int): The number of features.
179
+ dropout_rate (float): Dropout rate.
180
+
181
+ """
182
+
183
+ def __init__(self,
184
+ n_head: int,
185
+ n_feat: int,
186
+ dropout_rate: float,
187
+ key_bias: bool = True):
188
+ """Construct an MultiHeadedAttention object."""
189
+ super().__init__()
190
+ assert n_feat % n_head == 0
191
+ # We assume d_v always equals d_k
192
+ self.d_k = n_feat // n_head
193
+ self.h = n_head
194
+ self.linear_q = nn.Linear(n_feat, n_feat)
195
+ self.linear_k = nn.Linear(n_feat, n_feat, bias=key_bias)
196
+ self.linear_v = nn.Linear(n_feat, n_feat)
197
+ self.linear_out = nn.Linear(n_feat, n_feat)
198
+ self.dropout = nn.Dropout(p=dropout_rate)
199
+
200
+ def forward_qkv(
201
+ self, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor
202
+ ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
203
+ """Transform query, key and value.
204
+
205
+ Args:
206
+ query (torch.Tensor): Query tensor (#batch, time1, size).
207
+ key (torch.Tensor): Key tensor (#batch, time2, size).
208
+ value (torch.Tensor): Value tensor (#batch, time2, size).
209
+
210
+ Returns:
211
+ torch.Tensor: Transformed query tensor, size
212
+ (#batch, n_head, time1, d_k).
213
+ torch.Tensor: Transformed key tensor, size
214
+ (#batch, n_head, time2, d_k).
215
+ torch.Tensor: Transformed value tensor, size
216
+ (#batch, n_head, time2, d_k).
217
+
218
+ """
219
+ n_batch = query.size(0)
220
+ q = self.linear_q(query).view(n_batch, -1, self.h, self.d_k)
221
+ k = self.linear_k(key).view(n_batch, -1, self.h, self.d_k)
222
+ v = self.linear_v(value).view(n_batch, -1, self.h, self.d_k)
223
+ q = q.transpose(1, 2) # (batch, head, time1, d_k)
224
+ k = k.transpose(1, 2) # (batch, head, time2, d_k)
225
+ v = v.transpose(1, 2) # (batch, head, time2, d_k)
226
+ return q, k, v
227
+
228
+ def forward_attention(
229
+ self,
230
+ value: torch.Tensor,
231
+ scores: torch.Tensor,
232
+ mask: torch.Tensor = torch.ones((0, 0, 0), dtype=torch.bool)
233
+ ) -> torch.Tensor:
234
+ """Compute attention context vector.
235
+
236
+ Args:
237
+ value (torch.Tensor): Transformed value, size
238
+ (#batch, n_head, time2, d_k).
239
+ scores (torch.Tensor): Attention score, size
240
+ (#batch, n_head, time1, time2).
241
+ mask (torch.Tensor): Mask, size (#batch, 1, time2) or
242
+ (#batch, time1, time2), (0, 0, 0) means fake mask.
243
+
244
+ Returns:
245
+ torch.Tensor: Transformed value (#batch, time1, d_model)
246
+ weighted by the attention score (#batch, time1, time2).
247
+
248
+ """
249
+ n_batch = value.size(0)
250
+
251
+ if mask.size(2) > 0: # time2 > 0
252
+ mask = mask.unsqueeze(1).eq(0) # (batch, 1, *, time2)
253
+ # For last chunk, time2 might be larger than scores.size(-1)
254
+ mask = mask[:, :, :, :scores.size(-1)] # (batch, 1, *, time2)
255
+ scores = scores.masked_fill(mask, -float('inf'))
256
+ attn = torch.softmax(scores, dim=-1).masked_fill(
257
+ mask, 0.0) # (batch, head, time1, time2)
258
+
259
+ else:
260
+ attn = torch.softmax(scores, dim=-1) # (batch, head, time1, time2)
261
+
262
+ p_attn = self.dropout(attn)
263
+ x = torch.matmul(p_attn, value) # (batch, head, time1, d_k)
264
+ x = (x.transpose(1, 2).contiguous().view(n_batch, -1,
265
+ self.h * self.d_k)
266
+ ) # (batch, time1, d_model)
267
+
268
+ return self.linear_out(x) # (batch, time1, d_model)
269
+
270
+ def forward(
271
+ self,
272
+ query: torch.Tensor,
273
+ key: torch.Tensor,
274
+ value: torch.Tensor,
275
+ mask: torch.Tensor = torch.ones((0, 0, 0), dtype=torch.bool),
276
+ pos_emb: torch.Tensor = torch.empty(0),
277
+ cache: torch.Tensor = torch.zeros((0, 0, 0, 0))
278
+ ) -> Tuple[torch.Tensor, torch.Tensor]:
279
+ """Compute scaled dot product attention.
280
+
281
+ Args:
282
+ query (torch.Tensor): Query tensor (#batch, time1, size).
283
+ key (torch.Tensor): Key tensor (#batch, time2, size).
284
+ value (torch.Tensor): Value tensor (#batch, time2, size).
285
+ mask (torch.Tensor): Mask tensor (#batch, 1, time2) or
286
+ (#batch, time1, time2).
287
+ 1.When applying cross attention between decoder and encoder,
288
+ the batch padding mask for input is in (#batch, 1, T) shape.
289
+ 2.When applying self attention of encoder,
290
+ the mask is in (#batch, T, T) shape.
291
+ 3.When applying self attention of decoder,
292
+ the mask is in (#batch, L, L) shape.
293
+ 4.If the different position in decoder see different block
294
+ of the encoder, such as Mocha, the passed in mask could be
295
+ in (#batch, L, T) shape. But there is no such case in current
296
+ CosyVoice.
297
+ cache (torch.Tensor): Cache tensor (1, head, cache_t, d_k * 2),
298
+ where `cache_t == chunk_size * num_decoding_left_chunks`
299
+ and `head * d_k == size`
300
+
301
+
302
+ Returns:
303
+ torch.Tensor: Output tensor (#batch, time1, d_model).
304
+ torch.Tensor: Cache tensor (1, head, cache_t + time1, d_k * 2)
305
+ where `cache_t == chunk_size * num_decoding_left_chunks`
306
+ and `head * d_k == size`
307
+
308
+ """
309
+ q, k, v = self.forward_qkv(query, key, value)
310
+ if cache.size(0) > 0:
311
+ key_cache, value_cache = torch.split(cache,
312
+ cache.size(-1) // 2,
313
+ dim=-1)
314
+ k = torch.cat([key_cache, k], dim=2)
315
+ v = torch.cat([value_cache, v], dim=2)
316
+ new_cache = torch.cat((k, v), dim=-1)
317
+
318
+ scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(self.d_k)
319
+ return self.forward_attention(v, scores, mask), new_cache
320
+
321
+
322
+ class RelPositionMultiHeadedAttention(MultiHeadedAttention):
323
+ """Multi-Head Attention layer with relative position encoding.
324
+ Paper: https://arxiv.org/abs/1901.02860
325
+ Args:
326
+ n_head (int): The number of heads.
327
+ n_feat (int): The number of features.
328
+ dropout_rate (float): Dropout rate.
329
+ """
330
+
331
+ def __init__(self,
332
+ n_head: int,
333
+ n_feat: int,
334
+ dropout_rate: float,
335
+ key_bias: bool = True):
336
+ """Construct an RelPositionMultiHeadedAttention object."""
337
+ super().__init__(n_head, n_feat, dropout_rate, key_bias)
338
+ # linear transformation for positional encoding
339
+ self.linear_pos = nn.Linear(n_feat, n_feat, bias=False)
340
+ # these two learnable bias are used in matrix c and matrix d
341
+ # as described in https://arxiv.org/abs/1901.02860 Section 3.3
342
+ self.pos_bias_u = nn.Parameter(torch.Tensor(self.h, self.d_k))
343
+ self.pos_bias_v = nn.Parameter(torch.Tensor(self.h, self.d_k))
344
+ torch.nn.init.xavier_uniform_(self.pos_bias_u)
345
+ torch.nn.init.xavier_uniform_(self.pos_bias_v)
346
+
347
+ def rel_shift(self, x: torch.Tensor) -> torch.Tensor:
348
+ """Compute relative positional encoding.
349
+
350
+ Args:
351
+ x (torch.Tensor): Input tensor (batch, head, time1, 2*time1-1).
352
+ time1 means the length of query vector.
353
+
354
+ Returns:
355
+ torch.Tensor: Output tensor.
356
+
357
+ """
358
+ zero_pad = torch.zeros((x.size()[0], x.size()[1], x.size()[2], 1),
359
+ device=x.device,
360
+ dtype=x.dtype)
361
+ x_padded = torch.cat([zero_pad, x], dim=-1)
362
+
363
+ x_padded = x_padded.view(x.size()[0],
364
+ x.size()[1],
365
+ x.size(3) + 1, x.size(2))
366
+ x = x_padded[:, :, 1:].view_as(x)[
367
+ :, :, :, : x.size(-1) // 2 + 1
368
+ ] # only keep the positions from 0 to time2
369
+ return x
370
+
371
+ def forward(
372
+ self,
373
+ query: torch.Tensor,
374
+ key: torch.Tensor,
375
+ value: torch.Tensor,
376
+ mask: torch.Tensor = torch.ones((0, 0, 0), dtype=torch.bool),
377
+ pos_emb: torch.Tensor = torch.empty(0),
378
+ cache: torch.Tensor = torch.zeros((0, 0, 0, 0))
379
+ ) -> Tuple[torch.Tensor, torch.Tensor]:
380
+ """Compute 'Scaled Dot Product Attention' with rel. positional encoding.
381
+ Args:
382
+ query (torch.Tensor): Query tensor (#batch, time1, size).
383
+ key (torch.Tensor): Key tensor (#batch, time2, size).
384
+ value (torch.Tensor): Value tensor (#batch, time2, size).
385
+ mask (torch.Tensor): Mask tensor (#batch, 1, time2) or
386
+ (#batch, time1, time2), (0, 0, 0) means fake mask.
387
+ pos_emb (torch.Tensor): Positional embedding tensor
388
+ (#batch, time2, size).
389
+ cache (torch.Tensor): Cache tensor (1, head, cache_t, d_k * 2),
390
+ where `cache_t == chunk_size * num_decoding_left_chunks`
391
+ and `head * d_k == size`
392
+ Returns:
393
+ torch.Tensor: Output tensor (#batch, time1, d_model).
394
+ torch.Tensor: Cache tensor (1, head, cache_t + time1, d_k * 2)
395
+ where `cache_t == chunk_size * num_decoding_left_chunks`
396
+ and `head * d_k == size`
397
+ """
398
+ q, k, v = self.forward_qkv(query, key, value)
399
+ q = q.transpose(1, 2) # (batch, time1, head, d_k)
400
+
401
+ if cache.size(0) > 0:
402
+ key_cache, value_cache = torch.split(cache,
403
+ cache.size(-1) // 2,
404
+ dim=-1)
405
+ k = torch.cat([key_cache, k], dim=2)
406
+ v = torch.cat([value_cache, v], dim=2)
407
+ # NOTE(xcsong): We do cache slicing in encoder.forward_chunk, since it's
408
+ # non-trivial to calculate `next_cache_start` here.
409
+ new_cache = torch.cat((k, v), dim=-1)
410
+
411
+ n_batch_pos = pos_emb.size(0)
412
+ p = self.linear_pos(pos_emb).view(n_batch_pos, -1, self.h, self.d_k)
413
+ p = p.transpose(1, 2) # (batch, head, time1, d_k)
414
+
415
+ # (batch, head, time1, d_k)
416
+ q_with_bias_u = (q + self.pos_bias_u).transpose(1, 2)
417
+ # (batch, head, time1, d_k)
418
+ q_with_bias_v = (q + self.pos_bias_v).transpose(1, 2)
419
+
420
+ # compute attention score
421
+ # first compute matrix a and matrix c
422
+ # as described in https://arxiv.org/abs/1901.02860 Section 3.3
423
+ # (batch, head, time1, time2)
424
+ matrix_ac = torch.matmul(q_with_bias_u, k.transpose(-2, -1))
425
+
426
+ # compute matrix b and matrix d
427
+ # (batch, head, time1, time2)
428
+ matrix_bd = torch.matmul(q_with_bias_v, p.transpose(-2, -1))
429
+ # NOTE(Xiang Lyu): Keep rel_shift since espnet rel_pos_emb is used
430
+ if matrix_ac.shape != matrix_bd.shape:
431
+ matrix_bd = self.rel_shift(matrix_bd)
432
+
433
+ scores = (matrix_ac + matrix_bd) / math.sqrt(
434
+ self.d_k) # (batch, head, time1, time2)
435
+
436
+ return self.forward_attention(v, scores, mask), new_cache
437
+
438
+
439
+
440
+ def subsequent_mask(
441
+ size: int,
442
+ device: torch.device = torch.device("cpu"),
443
+ ) -> torch.Tensor:
444
+ """Create mask for subsequent steps (size, size).
445
+
446
+ This mask is used only in decoder which works in an auto-regressive mode.
447
+ This means the current step could only do attention with its left steps.
448
+
449
+ In encoder, fully attention is used when streaming is not necessary and
450
+ the sequence is not long. In this case, no attention mask is needed.
451
+
452
+ When streaming is need, chunk-based attention is used in encoder. See
453
+ subsequent_chunk_mask for the chunk-based attention mask.
454
+
455
+ Args:
456
+ size (int): size of mask
457
+ str device (str): "cpu" or "cuda" or torch.Tensor.device
458
+ dtype (torch.device): result dtype
459
+
460
+ Returns:
461
+ torch.Tensor: mask
462
+
463
+ Examples:
464
+ >>> subsequent_mask(3)
465
+ [[1, 0, 0],
466
+ [1, 1, 0],
467
+ [1, 1, 1]]
468
+ """
469
+ arange = torch.arange(size, device=device)
470
+ mask = arange.expand(size, size)
471
+ arange = arange.unsqueeze(-1)
472
+ mask = mask <= arange
473
+ return mask
474
+
475
+
476
+ def subsequent_chunk_mask(
477
+ size: int,
478
+ chunk_size: int,
479
+ num_left_chunks: int = -1,
480
+ device: torch.device = torch.device("cpu"),
481
+ ) -> torch.Tensor:
482
+ """Create mask for subsequent steps (size, size) with chunk size,
483
+ this is for streaming encoder
484
+
485
+ Args:
486
+ size (int): size of mask
487
+ chunk_size (int): size of chunk
488
+ num_left_chunks (int): number of left chunks
489
+ <0: use full chunk
490
+ >=0: use num_left_chunks
491
+ device (torch.device): "cpu" or "cuda" or torch.Tensor.device
492
+
493
+ Returns:
494
+ torch.Tensor: mask
495
+
496
+ Examples:
497
+ >>> subsequent_chunk_mask(4, 2)
498
+ [[1, 1, 0, 0],
499
+ [1, 1, 0, 0],
500
+ [1, 1, 1, 1],
501
+ [1, 1, 1, 1]]
502
+ """
503
+ ret = torch.zeros(size, size, device=device, dtype=torch.bool)
504
+ for i in range(size):
505
+ if num_left_chunks < 0:
506
+ start = 0
507
+ else:
508
+ start = max((i // chunk_size - num_left_chunks) * chunk_size, 0)
509
+ ending = min((i // chunk_size + 1) * chunk_size, size)
510
+ ret[i, start:ending] = True
511
+ return ret
512
+
513
+ def add_optional_chunk_mask(xs: torch.Tensor,
514
+ masks: torch.Tensor,
515
+ use_dynamic_chunk: bool,
516
+ use_dynamic_left_chunk: bool,
517
+ decoding_chunk_size: int,
518
+ static_chunk_size: int,
519
+ num_decoding_left_chunks: int,
520
+ enable_full_context: bool = True):
521
+ """ Apply optional mask for encoder.
522
+
523
+ Args:
524
+ xs (torch.Tensor): padded input, (B, L, D), L for max length
525
+ mask (torch.Tensor): mask for xs, (B, 1, L)
526
+ use_dynamic_chunk (bool): whether to use dynamic chunk or not
527
+ use_dynamic_left_chunk (bool): whether to use dynamic left chunk for
528
+ training.
529
+ decoding_chunk_size (int): decoding chunk size for dynamic chunk, it's
530
+ 0: default for training, use random dynamic chunk.
531
+ <0: for decoding, use full chunk.
532
+ >0: for decoding, use fixed chunk size as set.
533
+ static_chunk_size (int): chunk size for static chunk training/decoding
534
+ if it's greater than 0, if use_dynamic_chunk is true,
535
+ this parameter will be ignored
536
+ num_decoding_left_chunks: number of left chunks, this is for decoding,
537
+ the chunk size is decoding_chunk_size.
538
+ >=0: use num_decoding_left_chunks
539
+ <0: use all left chunks
540
+ enable_full_context (bool):
541
+ True: chunk size is either [1, 25] or full context(max_len)
542
+ False: chunk size ~ U[1, 25]
543
+
544
+ Returns:
545
+ torch.Tensor: chunk mask of the input xs.
546
+ """
547
+ # Whether to use chunk mask or not
548
+ if use_dynamic_chunk:
549
+ max_len = xs.size(1)
550
+ if decoding_chunk_size < 0:
551
+ chunk_size = max_len
552
+ num_left_chunks = -1
553
+ elif decoding_chunk_size > 0:
554
+ chunk_size = decoding_chunk_size
555
+ num_left_chunks = num_decoding_left_chunks
556
+ else:
557
+ # chunk size is either [1, 25] or full context(max_len).
558
+ # Since we use 4 times subsampling and allow up to 1s(100 frames)
559
+ # delay, the maximum frame is 100 / 4 = 25.
560
+ chunk_size = torch.randint(1, max_len, (1, )).item()
561
+ num_left_chunks = -1
562
+ if chunk_size > max_len // 2 and enable_full_context:
563
+ chunk_size = max_len
564
+ else:
565
+ chunk_size = chunk_size % 25 + 1
566
+ if use_dynamic_left_chunk:
567
+ max_left_chunks = (max_len - 1) // chunk_size
568
+ num_left_chunks = torch.randint(0, max_left_chunks,
569
+ (1, )).item()
570
+ chunk_masks = subsequent_chunk_mask(xs.size(1), chunk_size,
571
+ num_left_chunks,
572
+ xs.device) # (L, L)
573
+ chunk_masks = chunk_masks.unsqueeze(0) # (1, L, L)
574
+ chunk_masks = masks & chunk_masks # (B, L, L)
575
+ elif static_chunk_size > 0:
576
+ num_left_chunks = num_decoding_left_chunks
577
+ chunk_masks = subsequent_chunk_mask(xs.size(1), static_chunk_size,
578
+ num_left_chunks,
579
+ xs.device) # (L, L)
580
+ chunk_masks = chunk_masks.unsqueeze(0) # (1, L, L)
581
+ chunk_masks = masks & chunk_masks # (B, L, L)
582
+ else:
583
+ chunk_masks = masks
584
+ return chunk_masks
585
+
586
+
587
+ class ConformerEncoderLayer(nn.Module):
588
+ """Encoder layer module.
589
+ Args:
590
+ size (int): Input dimension.
591
+ self_attn (torch.nn.Module): Self-attention module instance.
592
+ `MultiHeadedAttention` or `RelPositionMultiHeadedAttention`
593
+ instance can be used as the argument.
594
+ feed_forward (torch.nn.Module): Feed-forward module instance.
595
+ `PositionwiseFeedForward` instance can be used as the argument.
596
+ feed_forward_macaron (torch.nn.Module): Additional feed-forward module
597
+ instance.
598
+ `PositionwiseFeedForward` instance can be used as the argument.
599
+ conv_module (torch.nn.Module): Convolution module instance.
600
+ `ConvlutionModule` instance can be used as the argument.
601
+ dropout_rate (float): Dropout rate.
602
+ normalize_before (bool):
603
+ True: use layer_norm before each sub-block.
604
+ False: use layer_norm after each sub-block.
605
+ """
606
+
607
+ def __init__(
608
+ self,
609
+ size: int,
610
+ self_attn: torch.nn.Module,
611
+ feed_forward: Optional[nn.Module] = None,
612
+ feed_forward_macaron: Optional[nn.Module] = None,
613
+ conv_module: Optional[nn.Module] = None,
614
+ dropout_rate: float = 0.1,
615
+ normalize_before: bool = True,
616
+ ):
617
+ """Construct an EncoderLayer object."""
618
+ super().__init__()
619
+ self.self_attn = self_attn
620
+ self.feed_forward = feed_forward
621
+ self.feed_forward_macaron = feed_forward_macaron
622
+ self.conv_module = conv_module
623
+ self.norm_ff = nn.LayerNorm(size, eps=1e-5) # for the FNN module
624
+ self.norm_mha = nn.LayerNorm(size, eps=1e-5) # for the MHA module
625
+ if feed_forward_macaron is not None:
626
+ self.norm_ff_macaron = nn.LayerNorm(size, eps=1e-5)
627
+ self.ff_scale = 0.5
628
+ else:
629
+ self.ff_scale = 1.0
630
+ if self.conv_module is not None:
631
+ self.norm_conv = nn.LayerNorm(size, eps=1e-5) # for the CNN module
632
+ self.norm_final = nn.LayerNorm(
633
+ size, eps=1e-5) # for the final output of the block
634
+ self.dropout = nn.Dropout(dropout_rate)
635
+ self.size = size
636
+ self.normalize_before = normalize_before
637
+
638
+ def forward(
639
+ self,
640
+ x: torch.Tensor,
641
+ mask: torch.Tensor,
642
+ pos_emb: torch.Tensor,
643
+ mask_pad: torch.Tensor = torch.ones((0, 0, 0), dtype=torch.bool),
644
+ att_cache: torch.Tensor = torch.zeros((0, 0, 0, 0)),
645
+ cnn_cache: torch.Tensor = torch.zeros((0, 0, 0, 0)),
646
+ ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
647
+ """Compute encoded features.
648
+
649
+ Args:
650
+ x (torch.Tensor): (#batch, time, size)
651
+ mask (torch.Tensor): Mask tensor for the input (#batch, time,time),
652
+ (0, 0, 0) means fake mask.
653
+ pos_emb (torch.Tensor): positional encoding, must not be None
654
+ for ConformerEncoderLayer.
655
+ mask_pad (torch.Tensor): batch padding mask used for conv module.
656
+ (#batch, 1,time), (0, 0, 0) means fake mask.
657
+ att_cache (torch.Tensor): Cache tensor of the KEY & VALUE
658
+ (#batch=1, head, cache_t1, d_k * 2), head * d_k == size.
659
+ cnn_cache (torch.Tensor): Convolution cache in conformer layer
660
+ (#batch=1, size, cache_t2)
661
+ Returns:
662
+ torch.Tensor: Output tensor (#batch, time, size).
663
+ torch.Tensor: Mask tensor (#batch, time, time).
664
+ torch.Tensor: att_cache tensor,
665
+ (#batch=1, head, cache_t1 + time, d_k * 2).
666
+ torch.Tensor: cnn_cahce tensor (#batch, size, cache_t2).
667
+ """
668
+
669
+ # whether to use macaron style
670
+ if self.feed_forward_macaron is not None:
671
+ residual = x
672
+ if self.normalize_before:
673
+ x = self.norm_ff_macaron(x)
674
+ x = residual + self.ff_scale * self.dropout(
675
+ self.feed_forward_macaron(x))
676
+ if not self.normalize_before:
677
+ x = self.norm_ff_macaron(x)
678
+
679
+ # multi-headed self-attention module
680
+ residual = x
681
+ if self.normalize_before:
682
+ x = self.norm_mha(x)
683
+ x_att, new_att_cache = self.self_attn(x, x, x, mask, pos_emb,
684
+ att_cache)
685
+ x = residual + self.dropout(x_att)
686
+ if not self.normalize_before:
687
+ x = self.norm_mha(x)
688
+
689
+ # convolution module
690
+ # Fake new cnn cache here, and then change it in conv_module
691
+ new_cnn_cache = torch.zeros((0, 0, 0), dtype=x.dtype, device=x.device)
692
+ if self.conv_module is not None:
693
+ residual = x
694
+ if self.normalize_before:
695
+ x = self.norm_conv(x)
696
+ x, new_cnn_cache = self.conv_module(x, mask_pad, cnn_cache)
697
+ x = residual + self.dropout(x)
698
+
699
+ if not self.normalize_before:
700
+ x = self.norm_conv(x)
701
+
702
+ # feed forward module
703
+ residual = x
704
+ if self.normalize_before:
705
+ x = self.norm_ff(x)
706
+
707
+ x = residual + self.ff_scale * self.dropout(self.feed_forward(x))
708
+ if not self.normalize_before:
709
+ x = self.norm_ff(x)
710
+
711
+ if self.conv_module is not None:
712
+ x = self.norm_final(x)
713
+
714
+ return x, mask, new_att_cache, new_cnn_cache
715
+
716
+
717
+
718
+ class EspnetRelPositionalEncoding(torch.nn.Module):
719
+ """Relative positional encoding module (new implementation).
720
+
721
+ Details can be found in https://github.com/espnet/espnet/pull/2816.
722
+
723
+ See : Appendix B in https://arxiv.org/abs/1901.02860
724
+
725
+ Args:
726
+ d_model (int): Embedding dimension.
727
+ dropout_rate (float): Dropout rate.
728
+ max_len (int): Maximum input length.
729
+
730
+ """
731
+
732
+ def __init__(self, d_model: int, dropout_rate: float, max_len: int = 5000):
733
+ """Construct an PositionalEncoding object."""
734
+ super(EspnetRelPositionalEncoding, self).__init__()
735
+ self.d_model = d_model
736
+ self.xscale = math.sqrt(self.d_model)
737
+ self.dropout = torch.nn.Dropout(p=dropout_rate)
738
+ self.pe = None
739
+ self.extend_pe(torch.tensor(0.0).expand(1, max_len))
740
+
741
+ def extend_pe(self, x: torch.Tensor):
742
+ """Reset the positional encodings."""
743
+ if self.pe is not None:
744
+ # self.pe contains both positive and negative parts
745
+ # the length of self.pe is 2 * input_len - 1
746
+ if self.pe.size(1) >= x.size(1) * 2 - 1:
747
+ if self.pe.dtype != x.dtype or self.pe.device != x.device:
748
+ self.pe = self.pe.to(dtype=x.dtype, device=x.device)
749
+ return
750
+ # Suppose `i` means to the position of query vecotr and `j` means the
751
+ # position of key vector. We use position relative positions when keys
752
+ # are to the left (i>j) and negative relative positions otherwise (i<j).
753
+ pe_positive = torch.zeros(x.size(1), self.d_model)
754
+ pe_negative = torch.zeros(x.size(1), self.d_model)
755
+ position = torch.arange(0, x.size(1), dtype=torch.float32).unsqueeze(1)
756
+ div_term = torch.exp(
757
+ torch.arange(0, self.d_model, 2, dtype=torch.float32)
758
+ * -(math.log(10000.0) / self.d_model)
759
+ )
760
+ pe_positive[:, 0::2] = torch.sin(position * div_term)
761
+ pe_positive[:, 1::2] = torch.cos(position * div_term)
762
+ pe_negative[:, 0::2] = torch.sin(-1 * position * div_term)
763
+ pe_negative[:, 1::2] = torch.cos(-1 * position * div_term)
764
+
765
+ # Reserve the order of positive indices and concat both positive and
766
+ # negative indices. This is used to support the shifting trick
767
+ # as in https://arxiv.org/abs/1901.02860
768
+ pe_positive = torch.flip(pe_positive, [0]).unsqueeze(0)
769
+ pe_negative = pe_negative[1:].unsqueeze(0)
770
+ pe = torch.cat([pe_positive, pe_negative], dim=1)
771
+ self.pe = pe.to(device=x.device, dtype=x.dtype)
772
+
773
+ def forward(self, x: torch.Tensor, offset: Union[int, torch.Tensor] = 0) \
774
+ -> Tuple[torch.Tensor, torch.Tensor]:
775
+ """Add positional encoding.
776
+
777
+ Args:
778
+ x (torch.Tensor): Input tensor (batch, time, `*`).
779
+
780
+ Returns:
781
+ torch.Tensor: Encoded tensor (batch, time, `*`).
782
+
783
+ """
784
+ self.extend_pe(x)
785
+ x = x * self.xscale
786
+ pos_emb = self.position_encoding(size=x.size(1), offset=offset)
787
+ return self.dropout(x), self.dropout(pos_emb)
788
+
789
+ def position_encoding(self,
790
+ offset: Union[int, torch.Tensor],
791
+ size: int) -> torch.Tensor:
792
+ """ For getting encoding in a streaming fashion
793
+
794
+ Attention!!!!!
795
+ we apply dropout only once at the whole utterance level in a none
796
+ streaming way, but will call this function several times with
797
+ increasing input size in a streaming scenario, so the dropout will
798
+ be applied several times.
799
+
800
+ Args:
801
+ offset (int or torch.tensor): start offset
802
+ size (int): required size of position encoding
803
+
804
+ Returns:
805
+ torch.Tensor: Corresponding encoding
806
+ """
807
+ pos_emb = self.pe[
808
+ :,
809
+ self.pe.size(1) // 2 - size + 1: self.pe.size(1) // 2 + size,
810
+ ]
811
+ return pos_emb
812
+
813
+
814
+
815
+ class LinearEmbed(torch.nn.Module):
816
+ """Linear transform the input without subsampling
817
+
818
+ Args:
819
+ idim (int): Input dimension.
820
+ odim (int): Output dimension.
821
+ dropout_rate (float): Dropout rate.
822
+
823
+ """
824
+
825
+ def __init__(self, idim: int, odim: int, dropout_rate: float,
826
+ pos_enc_class: torch.nn.Module):
827
+ """Construct an linear object."""
828
+ super().__init__()
829
+ self.out = torch.nn.Sequential(
830
+ torch.nn.Linear(idim, odim),
831
+ torch.nn.LayerNorm(odim, eps=1e-5),
832
+ torch.nn.Dropout(dropout_rate),
833
+ )
834
+ self.pos_enc = pos_enc_class #rel_pos_espnet
835
+
836
+ def position_encoding(self, offset: Union[int, torch.Tensor],
837
+ size: int) -> torch.Tensor:
838
+ return self.pos_enc.position_encoding(offset, size)
839
+
840
+ def forward(
841
+ self,
842
+ x: torch.Tensor,
843
+ offset: Union[int, torch.Tensor] = 0
844
+ ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
845
+ """Input x.
846
+
847
+ Args:
848
+ x (torch.Tensor): Input tensor (#batch, time, idim).
849
+ x_mask (torch.Tensor): Input mask (#batch, 1, time).
850
+
851
+ Returns:
852
+ torch.Tensor: linear input tensor (#batch, time', odim),
853
+ where time' = time .
854
+ torch.Tensor: linear input mask (#batch, 1, time'),
855
+ where time' = time .
856
+
857
+ """
858
+ x = self.out(x)
859
+ x, pos_emb = self.pos_enc(x, offset)
860
+ return x, pos_emb
861
+
862
+
863
+ ATTENTION_CLASSES = {
864
+ "selfattn": MultiHeadedAttention,
865
+ "rel_selfattn": RelPositionMultiHeadedAttention,
866
+ }
867
+
868
+ ACTIVATION_CLASSES = {
869
+ "hardtanh": torch.nn.Hardtanh,
870
+ "tanh": torch.nn.Tanh,
871
+ "relu": torch.nn.ReLU,
872
+ "selu": torch.nn.SELU,
873
+ "swish": getattr(torch.nn, "SiLU", Swish),
874
+ "gelu": torch.nn.GELU,
875
+ }
876
+
877
+
878
+ def make_pad_mask(lengths: torch.Tensor, max_len: int = 0) -> torch.Tensor:
879
+ """Make mask tensor containing indices of padded part.
880
+
881
+ See description of make_non_pad_mask.
882
+
883
+ Args:
884
+ lengths (torch.Tensor): Batch of lengths (B,).
885
+ Returns:
886
+ torch.Tensor: Mask tensor containing indices of padded part.
887
+
888
+ Examples:
889
+ >>> lengths = [5, 3, 2]
890
+ >>> make_pad_mask(lengths)
891
+ masks = [[0, 0, 0, 0 ,0],
892
+ [0, 0, 0, 1, 1],
893
+ [0, 0, 1, 1, 1]]
894
+ """
895
+ batch_size = lengths.size(0)
896
+ max_len = max_len if max_len > 0 else lengths.max().item()
897
+ seq_range = torch.arange(0,
898
+ max_len,
899
+ dtype=torch.int64,
900
+ device=lengths.device)
901
+ seq_range_expand = seq_range.unsqueeze(0).expand(batch_size, max_len)
902
+ seq_length_expand = lengths.unsqueeze(-1)
903
+ mask = seq_range_expand >= seq_length_expand
904
+ return mask
905
+
906
+ #https://github.com/FunAudioLLM/CosyVoice/blob/main/examples/magicdata-read/cosyvoice/conf/cosyvoice.yaml
907
+ class ConformerEncoder(torch.nn.Module):
908
+ """Conformer encoder module."""
909
+
910
+ def __init__(
911
+ self,
912
+ input_size: int,
913
+ output_size: int = 1024,
914
+ attention_heads: int = 16,
915
+ linear_units: int = 4096,
916
+ num_blocks: int = 6,
917
+ dropout_rate: float = 0.1,
918
+ positional_dropout_rate: float = 0.1,
919
+ attention_dropout_rate: float = 0.0,
920
+ input_layer: str = 'linear',
921
+ pos_enc_layer_type: str = 'rel_pos_espnet',
922
+ normalize_before: bool = True,
923
+ static_chunk_size: int = 1, # 1: causal_mask; 0: full_mask
924
+ use_dynamic_chunk: bool = False,
925
+ use_dynamic_left_chunk: bool = False,
926
+ positionwise_conv_kernel_size: int = 1,
927
+ macaron_style: bool =False,
928
+ selfattention_layer_type: str = "rel_selfattn",
929
+ activation_type: str = "swish",
930
+ use_cnn_module: bool = False,
931
+ cnn_module_kernel: int = 15,
932
+ causal: bool = False,
933
+ cnn_module_norm: str = "batch_norm",
934
+ key_bias: bool = True,
935
+ gradient_checkpointing: bool = False,
936
+ ):
937
+ """Construct ConformerEncoder
938
+
939
+ Args:
940
+ input_size to use_dynamic_chunk, see in BaseEncoder
941
+ positionwise_conv_kernel_size (int): Kernel size of positionwise
942
+ conv1d layer.
943
+ macaron_style (bool): Whether to use macaron style for
944
+ positionwise layer.
945
+ selfattention_layer_type (str): Encoder attention layer type,
946
+ the parameter has no effect now, it's just for configure
947
+ compatibility. #'rel_selfattn'
948
+ activation_type (str): Encoder activation function type.
949
+ use_cnn_module (bool): Whether to use convolution module.
950
+ cnn_module_kernel (int): Kernel size of convolution module.
951
+ causal (bool): whether to use causal convolution or not.
952
+ key_bias: whether use bias in attention.linear_k, False for whisper models.
953
+ """
954
+ super().__init__()
955
+ self.output_size = output_size
956
+ self.embed = LinearEmbed(input_size, output_size, dropout_rate,
957
+ EspnetRelPositionalEncoding(output_size, positional_dropout_rate))
958
+ self.normalize_before = normalize_before
959
+ self.after_norm = torch.nn.LayerNorm(output_size, eps=1e-5)
960
+ self.gradient_checkpointing = gradient_checkpointing
961
+ self.use_dynamic_chunk = use_dynamic_chunk
962
+
963
+ self.static_chunk_size = static_chunk_size
964
+ self.use_dynamic_chunk = use_dynamic_chunk
965
+ self.use_dynamic_left_chunk = use_dynamic_left_chunk
966
+ activation = ACTIVATION_CLASSES[activation_type]()
967
+
968
+ # self-attention module definition
969
+ encoder_selfattn_layer_args = (
970
+ attention_heads,
971
+ output_size,
972
+ attention_dropout_rate,
973
+ key_bias,
974
+ )
975
+ # feed-forward module definition
976
+ positionwise_layer_args = (
977
+ output_size,
978
+ linear_units,
979
+ dropout_rate,
980
+ activation,
981
+ )
982
+ # convolution module definition
983
+ convolution_layer_args = (output_size, cnn_module_kernel, activation,
984
+ cnn_module_norm, causal)
985
+
986
+ self.encoders = torch.nn.ModuleList([
987
+ ConformerEncoderLayer(
988
+ output_size,
989
+ RelPositionMultiHeadedAttention(
990
+ *encoder_selfattn_layer_args),
991
+ PositionwiseFeedForward(*positionwise_layer_args),
992
+ PositionwiseFeedForward(
993
+ *positionwise_layer_args) if macaron_style else None,
994
+ ConvolutionModule(
995
+ *convolution_layer_args) if use_cnn_module else None,
996
+ dropout_rate,
997
+ normalize_before,
998
+ ) for _ in range(num_blocks)
999
+ ])
1000
+
1001
+ def forward_layers(self, xs: torch.Tensor, chunk_masks: torch.Tensor,
1002
+ pos_emb: torch.Tensor,
1003
+ mask_pad: torch.Tensor) -> torch.Tensor:
1004
+ for layer in self.encoders:
1005
+ xs, chunk_masks, _, _ = layer(xs, chunk_masks, pos_emb, mask_pad)
1006
+ return xs
1007
+
1008
+ @torch.jit.unused
1009
+ def forward_layers_checkpointed(self, xs: torch.Tensor,
1010
+ chunk_masks: torch.Tensor,
1011
+ pos_emb: torch.Tensor,
1012
+ mask_pad: torch.Tensor) -> torch.Tensor:
1013
+ for layer in self.encoders:
1014
+ xs, chunk_masks, _, _ = ckpt.checkpoint(layer.__call__, xs,
1015
+ chunk_masks, pos_emb,
1016
+ mask_pad)
1017
+ return xs
1018
+
1019
+ def forward(
1020
+ self,
1021
+ xs: torch.Tensor,
1022
+ pad_mask: torch.Tensor,
1023
+ decoding_chunk_size: int = 0,
1024
+ num_decoding_left_chunks: int = -1,
1025
+ ) -> Tuple[torch.Tensor, torch.Tensor]:
1026
+ """Embed positions in tensor.
1027
+
1028
+ Args:
1029
+ xs: padded input tensor (B, T, D)
1030
+ xs_lens: input length (B)
1031
+ decoding_chunk_size: decoding chunk size for dynamic chunk
1032
+ 0: default for training, use random dynamic chunk.
1033
+ <0: for decoding, use full chunk.
1034
+ >0: for decoding, use fixed chunk size as set.
1035
+ num_decoding_left_chunks: number of left chunks, this is for decoding,
1036
+ the chunk size is decoding_chunk_size.
1037
+ >=0: use num_decoding_left_chunks
1038
+ <0: use all left chunks
1039
+ Returns:
1040
+ encoder output tensor xs, and subsampled masks
1041
+ xs: padded output tensor (B, T' ~= T/subsample_rate, D)
1042
+ masks: torch.Tensor batch padding mask after subsample
1043
+ (B, 1, T' ~= T/subsample_rate)
1044
+ NOTE(xcsong):
1045
+ We pass the `__call__` method of the modules instead of `forward` to the
1046
+ checkpointing API because `__call__` attaches all the hooks of the module.
1047
+ https://discuss.pytorch.org/t/any-different-between-model-input-and-model-forward-input/3690/2
1048
+ """
1049
+ T = xs.size(1)
1050
+ masks = pad_mask.to(torch.bool).unsqueeze(1) # (B, 1, T)
1051
+ xs, pos_emb = self.embed(xs)
1052
+ mask_pad = masks # (B, 1, T/subsample_rate)
1053
+ chunk_masks = add_optional_chunk_mask(xs, masks,
1054
+ self.use_dynamic_chunk,
1055
+ self.use_dynamic_left_chunk,
1056
+ decoding_chunk_size,
1057
+ self.static_chunk_size,
1058
+ num_decoding_left_chunks)
1059
+ if self.gradient_checkpointing and self.training:
1060
+ xs = self.forward_layers_checkpointed(xs, chunk_masks, pos_emb,
1061
+ mask_pad)
1062
+ else:
1063
+ xs = self.forward_layers(xs, chunk_masks, pos_emb, mask_pad)
1064
+ if self.normalize_before:
1065
+ xs = self.after_norm(xs)
1066
+ # Here we assume the mask is not changed in encoder layers, so just
1067
+ # return the masks before encoder layers, and the masks will be used
1068
+ # for cross attention with decoder later
1069
+ return xs, masks
1070
+
models/lyrics_utils/lyric_normalizer.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from opencc import OpenCC
3
+
4
+
5
+ t2s_converter = OpenCC('t2s')
6
+ s2t_converter = OpenCC('s2t')
7
+
8
+
9
+ EMOJI_PATTERN = re.compile(
10
+ "["
11
+ "\U0001F600-\U0001F64F" # Emoticons
12
+ "]+", flags=re.UNICODE
13
+ )
14
+
15
+ # 创建一个翻译表,用于替换和移除字符
16
+ TRANSLATION_TABLE = str.maketrans({
17
+ '-': ' ', # 将 '-' 替换为空格
18
+ ',': None,
19
+ '.': None,
20
+ ',': None,
21
+ '。': None,
22
+ '!': None,
23
+ '!': None,
24
+ '?': None,
25
+ '?': None,
26
+ '…': None,
27
+ ';': None,
28
+ ';': None,
29
+ ':': None,
30
+ ':': None,
31
+ '\u3000': ' ', # 将全角空格替换为空格
32
+ })
33
+
34
+ # 替换括号中的内容,包括中括号和小括号
35
+ BACKSLASH_PATTERN = re.compile(r'\(.*?\)|\[.*?\]')
36
+
37
+ SPACE_PATTERN = re.compile('(?<!^)\s+(?!$)')
38
+
39
+
40
+ def normalize_text(text, language, strip=True):
41
+ """
42
+ 对文本进行标准化处理,去除标点符号,转为小写(如果适用)
43
+ """
44
+ # Step 1: 替换 '-' 为 ' ' 并移除标点符号
45
+ text = text.translate(TRANSLATION_TABLE)
46
+
47
+ # Step 2: 移除表情符号
48
+ text = EMOJI_PATTERN.sub('', text)
49
+
50
+ # Step 3: 连续空白字符替换为单个空格,首位除外
51
+ text = SPACE_PATTERN.sub(' ', text)
52
+
53
+ # Step 4: 去除首尾空白字符(如果需要)
54
+ if strip:
55
+ text = text.strip()
56
+
57
+ # Step 5: 转为小写
58
+ text = text.lower()
59
+
60
+ # Step 6: 多语言转换
61
+ if language == "zh":
62
+ text = t2s_converter.convert(text)
63
+ if language == "yue":
64
+ text = s2t_converter.convert(text)
65
+ # 其他语言根据需要添加
66
+ return text
models/lyrics_utils/lyric_tokenizer.py ADDED
@@ -0,0 +1,883 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import textwrap
4
+ from functools import cached_property
5
+
6
+ import pypinyin
7
+ import torch
8
+ from hangul_romanize import Transliter
9
+ from hangul_romanize.rule import academic
10
+ from num2words import num2words
11
+ from spacy.lang.ar import Arabic
12
+ from spacy.lang.en import English
13
+ from spacy.lang.es import Spanish
14
+ from spacy.lang.ja import Japanese
15
+ from spacy.lang.zh import Chinese
16
+ from tokenizers import Tokenizer
17
+
18
+ from .zh_num2words import TextNorm as zh_num2words
19
+ from typing import Dict, List, Optional, Set, Union
20
+
21
+
22
+ #copy from https://github.com/coqui-ai/TTS/blob/dbf1a08a0d4e47fdad6172e433eeb34bc6b13b4e/TTS/tts/layers/xtts/tokenizer.py
23
+ def get_spacy_lang(lang):
24
+ if lang == "zh":
25
+ return Chinese()
26
+ elif lang == "ja":
27
+ return Japanese()
28
+ elif lang == "ar":
29
+ return Arabic()
30
+ elif lang == "es":
31
+ return Spanish()
32
+ else:
33
+ # For most languages, Enlish does the job
34
+ return English()
35
+
36
+
37
+ def split_sentence(text, lang, text_split_length=250):
38
+ """Preprocess the input text"""
39
+ text_splits = []
40
+ if text_split_length is not None and len(text) >= text_split_length:
41
+ text_splits.append("")
42
+ nlp = get_spacy_lang(lang)
43
+ nlp.add_pipe("sentencizer")
44
+ doc = nlp(text)
45
+ for sentence in doc.sents:
46
+ if len(text_splits[-1]) + len(str(sentence)) <= text_split_length:
47
+ # if the last sentence + the current sentence is less than the text_split_length
48
+ # then add the current sentence to the last sentence
49
+ text_splits[-1] += " " + str(sentence)
50
+ text_splits[-1] = text_splits[-1].lstrip()
51
+ elif len(str(sentence)) > text_split_length:
52
+ # if the current sentence is greater than the text_split_length
53
+ for line in textwrap.wrap(
54
+ str(sentence),
55
+ width=text_split_length,
56
+ drop_whitespace=True,
57
+ break_on_hyphens=False,
58
+ tabsize=1,
59
+ ):
60
+ text_splits.append(str(line))
61
+ else:
62
+ text_splits.append(str(sentence))
63
+
64
+ if len(text_splits) > 1:
65
+ if text_splits[0] == "":
66
+ del text_splits[0]
67
+ else:
68
+ text_splits = [text.lstrip()]
69
+
70
+ return text_splits
71
+
72
+
73
+ _whitespace_re = re.compile(r"\s+")
74
+
75
+ # List of (regular expression, replacement) pairs for abbreviations:
76
+ _abbreviations = {
77
+ "en": [
78
+ (re.compile("\\b%s\\." % x[0], re.IGNORECASE), x[1])
79
+ for x in [
80
+ ("mrs", "misess"),
81
+ ("mr", "mister"),
82
+ ("dr", "doctor"),
83
+ ("st", "saint"),
84
+ ("co", "company"),
85
+ ("jr", "junior"),
86
+ ("maj", "major"),
87
+ ("gen", "general"),
88
+ ("drs", "doctors"),
89
+ ("rev", "reverend"),
90
+ ("lt", "lieutenant"),
91
+ ("hon", "honorable"),
92
+ ("sgt", "sergeant"),
93
+ ("capt", "captain"),
94
+ ("esq", "esquire"),
95
+ ("ltd", "limited"),
96
+ ("col", "colonel"),
97
+ ("ft", "fort"),
98
+ ]
99
+ ],
100
+ "es": [
101
+ (re.compile("\\b%s\\." % x[0], re.IGNORECASE), x[1])
102
+ for x in [
103
+ ("sra", "señora"),
104
+ ("sr", "señor"),
105
+ ("dr", "doctor"),
106
+ ("dra", "doctora"),
107
+ ("st", "santo"),
108
+ ("co", "compañía"),
109
+ ("jr", "junior"),
110
+ ("ltd", "limitada"),
111
+ ]
112
+ ],
113
+ "fr": [
114
+ (re.compile("\\b%s\\." % x[0], re.IGNORECASE), x[1])
115
+ for x in [
116
+ ("mme", "madame"),
117
+ ("mr", "monsieur"),
118
+ ("dr", "docteur"),
119
+ ("st", "saint"),
120
+ ("co", "compagnie"),
121
+ ("jr", "junior"),
122
+ ("ltd", "limitée"),
123
+ ]
124
+ ],
125
+ "de": [
126
+ (re.compile("\\b%s\\." % x[0], re.IGNORECASE), x[1])
127
+ for x in [
128
+ ("fr", "frau"),
129
+ ("dr", "doktor"),
130
+ ("st", "sankt"),
131
+ ("co", "firma"),
132
+ ("jr", "junior"),
133
+ ]
134
+ ],
135
+ "pt": [
136
+ (re.compile("\\b%s\\." % x[0], re.IGNORECASE), x[1])
137
+ for x in [
138
+ ("sra", "senhora"),
139
+ ("sr", "senhor"),
140
+ ("dr", "doutor"),
141
+ ("dra", "doutora"),
142
+ ("st", "santo"),
143
+ ("co", "companhia"),
144
+ ("jr", "júnior"),
145
+ ("ltd", "limitada"),
146
+ ]
147
+ ],
148
+ "it": [
149
+ (re.compile("\\b%s\\." % x[0], re.IGNORECASE), x[1])
150
+ for x in [
151
+ # ("sig.ra", "signora"),
152
+ ("sig", "signore"),
153
+ ("dr", "dottore"),
154
+ ("st", "santo"),
155
+ ("co", "compagnia"),
156
+ ("jr", "junior"),
157
+ ("ltd", "limitata"),
158
+ ]
159
+ ],
160
+ "pl": [
161
+ (re.compile("\\b%s\\." % x[0], re.IGNORECASE), x[1])
162
+ for x in [
163
+ ("p", "pani"),
164
+ ("m", "pan"),
165
+ ("dr", "doktor"),
166
+ ("sw", "święty"),
167
+ ("jr", "junior"),
168
+ ]
169
+ ],
170
+ "ar": [
171
+ (re.compile("\\b%s\\." % x[0], re.IGNORECASE), x[1])
172
+ for x in [
173
+ # There are not many common abbreviations in Arabic as in English.
174
+ ]
175
+ ],
176
+ "zh": [
177
+ (re.compile("\\b%s\\." % x[0], re.IGNORECASE), x[1])
178
+ for x in [
179
+ # Chinese doesn't typically use abbreviations in the same way as Latin-based scripts.
180
+ ]
181
+ ],
182
+ "cs": [
183
+ (re.compile("\\b%s\\." % x[0], re.IGNORECASE), x[1])
184
+ for x in [
185
+ ("dr", "doktor"), # doctor
186
+ ("ing", "inženýr"), # engineer
187
+ ("p", "pan"), # Could also map to pani for woman but no easy way to do it
188
+ # Other abbreviations would be specialized and not as common.
189
+ ]
190
+ ],
191
+ "ru": [
192
+ (re.compile("\\b%s\\b" % x[0], re.IGNORECASE), x[1])
193
+ for x in [
194
+ ("г-жа", "госпожа"), # Mrs.
195
+ ("г-н", "господин"), # Mr.
196
+ ("д-р", "доктор"), # doctor
197
+ # Other abbreviations are less common or specialized.
198
+ ]
199
+ ],
200
+ "nl": [
201
+ (re.compile("\\b%s\\." % x[0], re.IGNORECASE), x[1])
202
+ for x in [
203
+ ("dhr", "de heer"), # Mr.
204
+ ("mevr", "mevrouw"), # Mrs.
205
+ ("dr", "dokter"), # doctor
206
+ ("jhr", "jonkheer"), # young lord or nobleman
207
+ # Dutch uses more abbreviations, but these are the most common ones.
208
+ ]
209
+ ],
210
+ "tr": [
211
+ (re.compile("\\b%s\\." % x[0], re.IGNORECASE), x[1])
212
+ for x in [
213
+ ("b", "bay"), # Mr.
214
+ ("byk", "büyük"), # büyük
215
+ ("dr", "doktor"), # doctor
216
+ # Add other Turkish abbreviations here if needed.
217
+ ]
218
+ ],
219
+ "hu": [
220
+ (re.compile("\\b%s\\." % x[0], re.IGNORECASE), x[1])
221
+ for x in [
222
+ ("dr", "doktor"), # doctor
223
+ ("b", "bácsi"), # Mr.
224
+ ("nőv", "nővér"), # nurse
225
+ # Add other Hungarian abbreviations here if needed.
226
+ ]
227
+ ],
228
+ "ko": [
229
+ (re.compile("\\b%s\\." % x[0], re.IGNORECASE), x[1])
230
+ for x in [
231
+ # Korean doesn't typically use abbreviations in the same way as Latin-based scripts.
232
+ ]
233
+ ],
234
+ }
235
+
236
+
237
+ def expand_abbreviations_multilingual(text, lang="en"):
238
+ for regex, replacement in _abbreviations[lang]:
239
+ text = re.sub(regex, replacement, text)
240
+ return text
241
+
242
+
243
+ _symbols_multilingual = {
244
+ "en": [
245
+ (re.compile(r"%s" % re.escape(x[0]), re.IGNORECASE), x[1])
246
+ for x in [
247
+ ("&", " and "),
248
+ ("@", " at "),
249
+ ("%", " percent "),
250
+ ("#", " hash "),
251
+ ("$", " dollar "),
252
+ ("£", " pound "),
253
+ ("°", " degree "),
254
+ ]
255
+ ],
256
+ "es": [
257
+ (re.compile(r"%s" % re.escape(x[0]), re.IGNORECASE), x[1])
258
+ for x in [
259
+ ("&", " y "),
260
+ ("@", " arroba "),
261
+ ("%", " por ciento "),
262
+ ("#", " numeral "),
263
+ ("$", " dolar "),
264
+ ("£", " libra "),
265
+ ("°", " grados "),
266
+ ]
267
+ ],
268
+ "fr": [
269
+ (re.compile(r"%s" % re.escape(x[0]), re.IGNORECASE), x[1])
270
+ for x in [
271
+ ("&", " et "),
272
+ ("@", " arobase "),
273
+ ("%", " pour cent "),
274
+ ("#", " dièse "),
275
+ ("$", " dollar "),
276
+ ("£", " livre "),
277
+ ("°", " degrés "),
278
+ ]
279
+ ],
280
+ "de": [
281
+ (re.compile(r"%s" % re.escape(x[0]), re.IGNORECASE), x[1])
282
+ for x in [
283
+ ("&", " und "),
284
+ ("@", " at "),
285
+ ("%", " prozent "),
286
+ ("#", " raute "),
287
+ ("$", " dollar "),
288
+ ("£", " pfund "),
289
+ ("°", " grad "),
290
+ ]
291
+ ],
292
+ "pt": [
293
+ (re.compile(r"%s" % re.escape(x[0]), re.IGNORECASE), x[1])
294
+ for x in [
295
+ ("&", " e "),
296
+ ("@", " arroba "),
297
+ ("%", " por cento "),
298
+ ("#", " cardinal "),
299
+ ("$", " dólar "),
300
+ ("£", " libra "),
301
+ ("°", " graus "),
302
+ ]
303
+ ],
304
+ "it": [
305
+ (re.compile(r"%s" % re.escape(x[0]), re.IGNORECASE), x[1])
306
+ for x in [
307
+ ("&", " e "),
308
+ ("@", " chiocciola "),
309
+ ("%", " per cento "),
310
+ ("#", " cancelletto "),
311
+ ("$", " dollaro "),
312
+ ("£", " sterlina "),
313
+ ("°", " gradi "),
314
+ ]
315
+ ],
316
+ "pl": [
317
+ (re.compile(r"%s" % re.escape(x[0]), re.IGNORECASE), x[1])
318
+ for x in [
319
+ ("&", " i "),
320
+ ("@", " małpa "),
321
+ ("%", " procent "),
322
+ ("#", " krzyżyk "),
323
+ ("$", " dolar "),
324
+ ("£", " funt "),
325
+ ("°", " stopnie "),
326
+ ]
327
+ ],
328
+ "ar": [
329
+ # Arabic
330
+ (re.compile(r"%s" % re.escape(x[0]), re.IGNORECASE), x[1])
331
+ for x in [
332
+ ("&", " و "),
333
+ ("@", " على "),
334
+ ("%", " في المئة "),
335
+ ("#", " رقم "),
336
+ ("$", " دولار "),
337
+ ("£", " جنيه "),
338
+ ("°", " درجة "),
339
+ ]
340
+ ],
341
+ "zh": [
342
+ # Chinese
343
+ (re.compile(r"%s" % re.escape(x[0]), re.IGNORECASE), x[1])
344
+ for x in [
345
+ ("&", " 和 "),
346
+ ("@", " 在 "),
347
+ ("%", " 百分之 "),
348
+ ("#", " 号 "),
349
+ ("$", " 美元 "),
350
+ ("£", " 英镑 "),
351
+ ("°", " 度 "),
352
+ ]
353
+ ],
354
+ "cs": [
355
+ # Czech
356
+ (re.compile(r"%s" % re.escape(x[0]), re.IGNORECASE), x[1])
357
+ for x in [
358
+ ("&", " a "),
359
+ ("@", " na "),
360
+ ("%", " procento "),
361
+ ("#", " křížek "),
362
+ ("$", " dolar "),
363
+ ("£", " libra "),
364
+ ("°", " stupně "),
365
+ ]
366
+ ],
367
+ "ru": [
368
+ # Russian
369
+ (re.compile(r"%s" % re.escape(x[0]), re.IGNORECASE), x[1])
370
+ for x in [
371
+ ("&", " и "),
372
+ ("@", " собака "),
373
+ ("%", " процентов "),
374
+ ("#", " номер "),
375
+ ("$", " доллар "),
376
+ ("£", " фунт "),
377
+ ("°", " градус "),
378
+ ]
379
+ ],
380
+ "nl": [
381
+ # Dutch
382
+ (re.compile(r"%s" % re.escape(x[0]), re.IGNORECASE), x[1])
383
+ for x in [
384
+ ("&", " en "),
385
+ ("@", " bij "),
386
+ ("%", " procent "),
387
+ ("#", " hekje "),
388
+ ("$", " dollar "),
389
+ ("£", " pond "),
390
+ ("°", " graden "),
391
+ ]
392
+ ],
393
+ "tr": [
394
+ (re.compile(r"%s" % re.escape(x[0]), re.IGNORECASE), x[1])
395
+ for x in [
396
+ ("&", " ve "),
397
+ ("@", " at "),
398
+ ("%", " yüzde "),
399
+ ("#", " diyez "),
400
+ ("$", " dolar "),
401
+ ("£", " sterlin "),
402
+ ("°", " derece "),
403
+ ]
404
+ ],
405
+ "hu": [
406
+ (re.compile(r"%s" % re.escape(x[0]), re.IGNORECASE), x[1])
407
+ for x in [
408
+ ("&", " és "),
409
+ ("@", " kukac "),
410
+ ("%", " százalék "),
411
+ ("#", " kettőskereszt "),
412
+ ("$", " dollár "),
413
+ ("£", " font "),
414
+ ("°", " fok "),
415
+ ]
416
+ ],
417
+ "ko": [
418
+ # Korean
419
+ (re.compile(r"%s" % re.escape(x[0]), re.IGNORECASE), x[1])
420
+ for x in [
421
+ ("&", " 그리고 "),
422
+ ("@", " 에 "),
423
+ ("%", " 퍼센트 "),
424
+ ("#", " 번호 "),
425
+ ("$", " 달러 "),
426
+ ("£", " 파운드 "),
427
+ ("°", " 도 "),
428
+ ]
429
+ ],
430
+ }
431
+
432
+
433
+ def expand_symbols_multilingual(text, lang="en"):
434
+ for regex, replacement in _symbols_multilingual[lang]:
435
+ text = re.sub(regex, replacement, text)
436
+ text = text.replace(" ", " ") # Ensure there are no double spaces
437
+ return text.strip()
438
+
439
+
440
+ _ordinal_re = {
441
+ "en": re.compile(r"([0-9]+)(st|nd|rd|th)"),
442
+ "es": re.compile(r"([0-9]+)(º|ª|er|o|a|os|as)"),
443
+ "fr": re.compile(r"([0-9]+)(º|ª|er|re|e|ème)"),
444
+ "de": re.compile(r"([0-9]+)(st|nd|rd|th|º|ª|\.(?=\s|$))"),
445
+ "pt": re.compile(r"([0-9]+)(º|ª|o|a|os|as)"),
446
+ "it": re.compile(r"([0-9]+)(º|°|ª|o|a|i|e)"),
447
+ "pl": re.compile(r"([0-9]+)(º|ª|st|nd|rd|th)"),
448
+ "ar": re.compile(r"([0-9]+)(ون|ين|ث|ر|ى)"),
449
+ "cs": re.compile(r"([0-9]+)\.(?=\s|$)"), # In Czech, a dot is often used after the number to indicate ordinals.
450
+ "ru": re.compile(r"([0-9]+)(-й|-я|-е|-ое|-ье|-го)"),
451
+ "nl": re.compile(r"([0-9]+)(de|ste|e)"),
452
+ "tr": re.compile(r"([0-9]+)(\.|inci|nci|uncu|üncü|\.)"),
453
+ "hu": re.compile(r"([0-9]+)(\.|adik|edik|odik|edik|ödik|ödike|ik)"),
454
+ "ko": re.compile(r"([0-9]+)(번째|번|차|째)"),
455
+ }
456
+ _number_re = re.compile(r"[0-9]+")
457
+ _currency_re = {
458
+ "USD": re.compile(r"((\$[0-9\.\,]*[0-9]+)|([0-9\.\,]*[0-9]+\$))"),
459
+ "GBP": re.compile(r"((£[0-9\.\,]*[0-9]+)|([0-9\.\,]*[0-9]+£))"),
460
+ "EUR": re.compile(r"(([0-9\.\,]*[0-9]+€)|((€[0-9\.\,]*[0-9]+)))"),
461
+ }
462
+
463
+ _comma_number_re = re.compile(r"\b\d{1,3}(,\d{3})*(\.\d+)?\b")
464
+ _dot_number_re = re.compile(r"\b\d{1,3}(.\d{3})*(\,\d+)?\b")
465
+ _decimal_number_re = re.compile(r"([0-9]+[.,][0-9]+)")
466
+
467
+
468
+ def _remove_commas(m):
469
+ text = m.group(0)
470
+ if "," in text:
471
+ text = text.replace(",", "")
472
+ return text
473
+
474
+
475
+ def _remove_dots(m):
476
+ text = m.group(0)
477
+ if "." in text:
478
+ text = text.replace(".", "")
479
+ return text
480
+
481
+
482
+ def _expand_decimal_point(m, lang="en"):
483
+ amount = m.group(1).replace(",", ".")
484
+ return num2words(float(amount), lang=lang if lang != "cs" else "cz")
485
+
486
+
487
+ def _expand_currency(m, lang="en", currency="USD"):
488
+ amount = float((re.sub(r"[^\d.]", "", m.group(0).replace(",", "."))))
489
+ full_amount = num2words(amount, to="currency", currency=currency, lang=lang if lang != "cs" else "cz")
490
+
491
+ and_equivalents = {
492
+ "en": ", ",
493
+ "es": " con ",
494
+ "fr": " et ",
495
+ "de": " und ",
496
+ "pt": " e ",
497
+ "it": " e ",
498
+ "pl": ", ",
499
+ "cs": ", ",
500
+ "ru": ", ",
501
+ "nl": ", ",
502
+ "ar": ", ",
503
+ "tr": ", ",
504
+ "hu": ", ",
505
+ "ko": ", ",
506
+ }
507
+
508
+ if amount.is_integer():
509
+ last_and = full_amount.rfind(and_equivalents[lang])
510
+ if last_and != -1:
511
+ full_amount = full_amount[:last_and]
512
+
513
+ return full_amount
514
+
515
+
516
+ def _expand_ordinal(m, lang="en"):
517
+ return num2words(int(m.group(1)), ordinal=True, lang=lang if lang != "cs" else "cz")
518
+
519
+
520
+ def _expand_number(m, lang="en"):
521
+ return num2words(int(m.group(0)), lang=lang if lang != "cs" else "cz")
522
+
523
+
524
+ def expand_numbers_multilingual(text, lang="en"):
525
+ if lang == "zh":
526
+ text = zh_num2words()(text)
527
+ else:
528
+ if lang in ["en", "ru"]:
529
+ text = re.sub(_comma_number_re, _remove_commas, text)
530
+ else:
531
+ text = re.sub(_dot_number_re, _remove_dots, text)
532
+ try:
533
+ text = re.sub(_currency_re["GBP"], lambda m: _expand_currency(m, lang, "GBP"), text)
534
+ text = re.sub(_currency_re["USD"], lambda m: _expand_currency(m, lang, "USD"), text)
535
+ text = re.sub(_currency_re["EUR"], lambda m: _expand_currency(m, lang, "EUR"), text)
536
+ except:
537
+ pass
538
+ if lang != "tr":
539
+ text = re.sub(_decimal_number_re, lambda m: _expand_decimal_point(m, lang), text)
540
+ text = re.sub(_ordinal_re[lang], lambda m: _expand_ordinal(m, lang), text)
541
+ text = re.sub(_number_re, lambda m: _expand_number(m, lang), text)
542
+ return text
543
+
544
+
545
+ def lowercase(text):
546
+ return text.lower()
547
+
548
+
549
+ def collapse_whitespace(text):
550
+ return re.sub(_whitespace_re, " ", text)
551
+
552
+
553
+ def multilingual_cleaners(text, lang):
554
+ text = text.replace('"', "")
555
+ if lang == "tr":
556
+ text = text.replace("İ", "i")
557
+ text = text.replace("Ö", "ö")
558
+ text = text.replace("Ü", "ü")
559
+ text = lowercase(text)
560
+ try:
561
+ text = expand_numbers_multilingual(text, lang)
562
+ except:
563
+ pass
564
+ try:
565
+ text = expand_abbreviations_multilingual(text, lang)
566
+ except:
567
+ pass
568
+ try:
569
+ text = expand_symbols_multilingual(text, lang=lang)
570
+ except:
571
+ pass
572
+ text = collapse_whitespace(text)
573
+ return text
574
+
575
+
576
+ def basic_cleaners(text):
577
+ """Basic pipeline that lowercases and collapses whitespace without transliteration."""
578
+ text = lowercase(text)
579
+ text = collapse_whitespace(text)
580
+ return text
581
+
582
+
583
+ def chinese_transliterate(text):
584
+ return "".join(
585
+ [p[0] for p in pypinyin.pinyin(text, style=pypinyin.Style.TONE3, heteronym=False, neutral_tone_with_five=True)]
586
+ )
587
+
588
+
589
+ def japanese_cleaners(text, katsu):
590
+ text = katsu.romaji(text)
591
+ text = lowercase(text)
592
+ return text
593
+
594
+
595
+ def korean_transliterate(text):
596
+ r = Transliter(academic)
597
+ return r.translit(text)
598
+
599
+
600
+ DEFAULT_VOCAB_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)), "vocab.json")
601
+
602
+
603
+ class VoiceBpeTokenizer:
604
+ def __init__(self, vocab_file=DEFAULT_VOCAB_FILE):
605
+ self.tokenizer = None
606
+ if vocab_file is not None:
607
+ self.tokenizer = Tokenizer.from_file(vocab_file)
608
+ self.char_limits = {
609
+ "en": 10000,
610
+ "de": 253,
611
+ "fr": 273,
612
+ "es": 239,
613
+ "it": 213,
614
+ "pt": 203,
615
+ "pl": 224,
616
+ "zh": 82,
617
+ "ar": 166,
618
+ "cs": 186,
619
+ "ru": 182,
620
+ "nl": 251,
621
+ "tr": 226,
622
+ "ja": 71,
623
+ "hu": 224,
624
+ "ko": 95,
625
+ }
626
+
627
+ @cached_property
628
+ def katsu(self):
629
+ import cutlet
630
+
631
+ return cutlet.Cutlet()
632
+
633
+ def check_input_length(self, txt, lang):
634
+ lang = lang.split("-")[0] # remove the region
635
+ limit = self.char_limits.get(lang, 250)
636
+ # if len(txt) > limit:
637
+ # print(
638
+ # f"[!] Warning: The text length exceeds the character limit of {limit} for language '{lang}', this might cause truncated audio."
639
+ # )
640
+
641
+ def preprocess_text(self, txt, lang):
642
+ if lang in {"ar", "cs", "de", "en", "es", "fr", "hu", "it", "nl", "pl", "pt", "ru", "tr", "zh", "ko"}:
643
+ txt = multilingual_cleaners(txt, lang)
644
+ if lang == "zh":
645
+ txt = chinese_transliterate(txt)
646
+ if lang == "ko":
647
+ txt = korean_transliterate(txt)
648
+ elif lang == "ja":
649
+ txt = japanese_cleaners(txt, self.katsu)
650
+ elif lang == "hi":
651
+ # @manmay will implement this
652
+ txt = basic_cleaners(txt)
653
+ else:
654
+ raise NotImplementedError(f"Language '{lang}' is not supported.")
655
+ return txt
656
+
657
+ def encode(self, txt, lang):
658
+ lang = lang.split("-")[0] # remove the region
659
+ self.check_input_length(txt, lang)
660
+ txt = self.preprocess_text(txt, lang)
661
+ lang = "zh-cn" if lang == "zh" else lang
662
+ txt = f"[{lang}]{txt}"
663
+ txt = txt.replace(" ", "[SPACE]")
664
+ return self.tokenizer.encode(txt).ids
665
+
666
+ def decode(self, seq, skip_special_tokens=False):
667
+ if isinstance(seq, torch.Tensor):
668
+ seq = seq.cpu().numpy()
669
+ txt = self.tokenizer.decode(seq, skip_special_tokens=False).replace(" ", "")
670
+ txt = txt.replace("[SPACE]", " ")
671
+ txt = txt.replace("[STOP]", "")
672
+ # txt = txt.replace("[UNK]", "")
673
+ return txt
674
+
675
+
676
+ #copy from https://github.com/huggingface/transformers/blob/main/src/transformers/tokenization_utils_base.py#L3936
677
+ def batch_decode(
678
+ self,
679
+ sequences: Union[List[int], List[List[int]], "np.ndarray", "torch.Tensor", "tf.Tensor"],
680
+ skip_special_tokens: bool = False,
681
+ ) -> List[str]:
682
+ """
683
+ Convert a list of lists of token ids into a list of strings by calling decode.
684
+
685
+ Args:
686
+ sequences (`Union[List[int], List[List[int]], np.ndarray, torch.Tensor, tf.Tensor]`):
687
+ List of tokenized input ids. Can be obtained using the `__call__` method.
688
+ skip_special_tokens (`bool`, *optional*, defaults to `False`):
689
+ Whether or not to remove special tokens in the decoding.
690
+ kwargs (additional keyword arguments, *optional*):
691
+ Will be passed to the underlying model specific decode method.
692
+
693
+ Returns:
694
+ `List[str]`: The list of decoded sentences.
695
+ """
696
+ return [
697
+ self.decode(seq)
698
+ for seq in sequences
699
+ ]
700
+
701
+ #https://github.com/coqui-ai/TTS/blob/dev/TTS/tts/layers/xtts/trainer/dataset.py#L202
702
+ # def pad(self):
703
+
704
+ def __len__(self):
705
+ return self.tokenizer.get_vocab_size()
706
+
707
+ def get_number_tokens(self):
708
+ return max(self.tokenizer.get_vocab().values()) + 1
709
+
710
+
711
+ def test_expand_numbers_multilingual():
712
+ test_cases = [
713
+ # English
714
+ ("In 12.5 seconds.", "In twelve point five seconds.", "en"),
715
+ ("There were 50 soldiers.", "There were fifty soldiers.", "en"),
716
+ ("This is a 1st test", "This is a first test", "en"),
717
+ ("That will be $20 sir.", "That will be twenty dollars sir.", "en"),
718
+ ("That will be 20€ sir.", "That will be twenty euro sir.", "en"),
719
+ ("That will be 20.15€ sir.", "That will be twenty euro, fifteen cents sir.", "en"),
720
+ ("That's 100,000.5.", "That's one hundred thousand point five.", "en"),
721
+ # French
722
+ ("En 12,5 secondes.", "En douze virgule cinq secondes.", "fr"),
723
+ ("Il y avait 50 soldats.", "Il y avait cinquante soldats.", "fr"),
724
+ ("Ceci est un 1er test", "Ceci est un premier test", "fr"),
725
+ ("Cela vous fera $20 monsieur.", "Cela vous fera vingt dollars monsieur.", "fr"),
726
+ ("Cela vous fera 20€ monsieur.", "Cela vous fera vingt euros monsieur.", "fr"),
727
+ ("Cela vous fera 20,15€ monsieur.", "Cela vous fera vingt euros et quinze centimes monsieur.", "fr"),
728
+ ("Ce sera 100.000,5.", "Ce sera cent mille virgule cinq.", "fr"),
729
+ # German
730
+ ("In 12,5 Sekunden.", "In zwölf Komma fünf Sekunden.", "de"),
731
+ ("Es gab 50 Soldaten.", "Es gab fünfzig Soldaten.", "de"),
732
+ ("Dies ist ein 1. Test", "Dies ist ein erste Test", "de"), # Issue with gender
733
+ ("Das macht $20 Herr.", "Das macht zwanzig Dollar Herr.", "de"),
734
+ ("Das macht 20€ Herr.", "Das macht zwanzig Euro Herr.", "de"),
735
+ ("Das macht 20,15€ Herr.", "Das macht zwanzig Euro und fünfzehn Cent Herr.", "de"),
736
+ # Spanish
737
+ ("En 12,5 segundos.", "En doce punto cinco segundos.", "es"),
738
+ ("Había 50 soldados.", "Había cincuenta soldados.", "es"),
739
+ ("Este es un 1er test", "Este es un primero test", "es"),
740
+ ("Eso le costará $20 señor.", "Eso le costará veinte dólares señor.", "es"),
741
+ ("Eso le costará 20€ señor.", "Eso le costará veinte euros señor.", "es"),
742
+ ("Eso le costará 20,15€ señor.", "Eso le costará veinte euros con quince céntimos señor.", "es"),
743
+ # Italian
744
+ ("In 12,5 secondi.", "In dodici virgola cinque secondi.", "it"),
745
+ ("C'erano 50 soldati.", "C'erano cinquanta soldati.", "it"),
746
+ ("Questo è un 1° test", "Questo è un primo test", "it"),
747
+ ("Ti costerà $20 signore.", "Ti costerà venti dollari signore.", "it"),
748
+ ("Ti costerà 20€ signore.", "Ti costerà venti euro signore.", "it"),
749
+ ("Ti costerà 20,15€ signore.", "Ti costerà venti euro e quindici centesimi signore.", "it"),
750
+ # Portuguese
751
+ ("Em 12,5 segundos.", "Em doze vírgula cinco segundos.", "pt"),
752
+ ("Havia 50 soldados.", "Havia cinquenta soldados.", "pt"),
753
+ ("Este é um 1º teste", "Este é um primeiro teste", "pt"),
754
+ ("Isso custará $20 senhor.", "Isso custará vinte dólares senhor.", "pt"),
755
+ ("Isso custará 20€ senhor.", "Isso custará vinte euros senhor.", "pt"),
756
+ (
757
+ "Isso custará 20,15€ senhor.",
758
+ "Isso custará vinte euros e quinze cêntimos senhor.",
759
+ "pt",
760
+ ), # "cêntimos" should be "centavos" num2words issue
761
+ # Polish
762
+ ("W 12,5 sekundy.", "W dwanaście przecinek pięć sekundy.", "pl"),
763
+ ("Było 50 żołnierzy.", "Było pięćdziesiąt żołnierzy.", "pl"),
764
+ ("To będzie kosztować 20€ panie.", "To będzie kosztować dwadzieścia euro panie.", "pl"),
765
+ ("To będzie kosztować 20,15€ panie.", "To będzie kosztować dwadzieścia euro, piętnaście centów panie.", "pl"),
766
+ # Arabic
767
+ ("في الـ 12,5 ثانية.", "في الـ اثنا عشر , خمسون ثانية.", "ar"),
768
+ ("كان هناك 50 جنديًا.", "كان هناك خمسون جنديًا.", "ar"),
769
+ # ("ستكون النتيجة $20 يا سيد.", 'ستكون النتيجة عشرون دولار يا سيد.', 'ar'), # $ and € are mising from num2words
770
+ # ("ستكون النتيجة 20€ يا سيد.", 'ستكون النتيجة عشرون يورو يا سيد.', 'ar'),
771
+ # Czech
772
+ ("Za 12,5 vteřiny.", "Za dvanáct celá pět vteřiny.", "cs"),
773
+ ("Bylo tam 50 vojáků.", "Bylo tam padesát vojáků.", "cs"),
774
+ ("To bude stát 20€ pane.", "To bude stát dvacet euro pane.", "cs"),
775
+ ("To bude 20.15€ pane.", "To bude dvacet euro, patnáct centů pane.", "cs"),
776
+ # Russian
777
+ ("Через 12.5 секунды.", "Через двенадцать запятая пять секунды.", "ru"),
778
+ ("Там было 50 солдат.", "Там было пятьдесят солдат.", "ru"),
779
+ ("Это будет 20.15€ сэр.", "Это будет двадцать евро, пятнадцать центов сэр.", "ru"),
780
+ ("Это будет стоить 20€ господин.", "Это будет стоить двадцать евро господин.", "ru"),
781
+ # Dutch
782
+ ("In 12,5 seconden.", "In twaalf komma vijf seconden.", "nl"),
783
+ ("Er waren 50 soldaten.", "Er waren vijftig soldaten.", "nl"),
784
+ ("Dat wordt dan $20 meneer.", "Dat wordt dan twintig dollar meneer.", "nl"),
785
+ ("Dat wordt dan 20€ meneer.", "Dat wordt dan twintig euro meneer.", "nl"),
786
+ # Chinese (Simplified)
787
+ ("在12.5秒内", "在十二点五秒内", "zh"),
788
+ ("有50名士兵", "有五十名士兵", "zh"),
789
+ # ("那将是$20先生", '那将是二十美元先生', 'zh'), currency doesn't work
790
+ # ("那将是20€先生", '那将是二十欧元先生', 'zh'),
791
+ # Turkish
792
+ # ("12,5 saniye içinde.", 'On iki virgül beş saniye içinde.', 'tr'), # decimal doesn't work for TR
793
+ ("50 asker vardı.", "elli asker vardı.", "tr"),
794
+ ("Bu 1. test", "Bu birinci test", "tr"),
795
+ # ("Bu 100.000,5.", 'Bu yüz bin virgül beş.', 'tr'),
796
+ # Hungarian
797
+ ("12,5 másodperc alatt.", "tizenkettő egész öt tized másodperc alatt.", "hu"),
798
+ ("50 katona volt.", "ötven katona volt.", "hu"),
799
+ ("Ez az 1. teszt", "Ez az első teszt", "hu"),
800
+ # Korean
801
+ ("12.5 초 안에.", "십이 점 다섯 초 안에.", "ko"),
802
+ ("50 명의 병사가 있었다.", "오십 명의 병사가 있었다.", "ko"),
803
+ ("이것은 1 번째 테스트입니다", "이것은 첫 번째 테스트입니다", "ko"),
804
+ ]
805
+ for a, b, lang in test_cases:
806
+ out = expand_numbers_multilingual(a, lang=lang)
807
+ assert out == b, f"'{out}' vs '{b}'"
808
+
809
+
810
+ def test_abbreviations_multilingual():
811
+ test_cases = [
812
+ # English
813
+ ("Hello Mr. Smith.", "Hello mister Smith.", "en"),
814
+ ("Dr. Jones is here.", "doctor Jones is here.", "en"),
815
+ # Spanish
816
+ ("Hola Sr. Garcia.", "Hola señor Garcia.", "es"),
817
+ ("La Dra. Martinez es muy buena.", "La doctora Martinez es muy buena.", "es"),
818
+ # French
819
+ ("Bonjour Mr. Dupond.", "Bonjour monsieur Dupond.", "fr"),
820
+ ("Mme. Moreau est absente aujourd'hui.", "madame Moreau est absente aujourd'hui.", "fr"),
821
+ # German
822
+ ("Frau Dr. Müller ist sehr klug.", "Frau doktor Müller ist sehr klug.", "de"),
823
+ # Portuguese
824
+ ("Olá Sr. Silva.", "Olá senhor Silva.", "pt"),
825
+ ("Dra. Costa, você está disponível?", "doutora Costa, você está disponível?", "pt"),
826
+ # Italian
827
+ ("Buongiorno, Sig. Rossi.", "Buongiorno, signore Rossi.", "it"),
828
+ # ("Sig.ra Bianchi, posso aiutarti?", 'signora Bianchi, posso aiutarti?', 'it'), # Issue with matching that pattern
829
+ # Polish
830
+ ("Dzień dobry, P. Kowalski.", "Dzień dobry, pani Kowalski.", "pl"),
831
+ ("M. Nowak, czy mogę zadać pytanie?", "pan Nowak, czy mogę zadać pytanie?", "pl"),
832
+ # Czech
833
+ ("P. Novák", "pan Novák", "cs"),
834
+ ("Dr. Vojtěch", "doktor Vojtěch", "cs"),
835
+ # Dutch
836
+ ("Dhr. Jansen", "de heer Jansen", "nl"),
837
+ ("Mevr. de Vries", "mevrouw de Vries", "nl"),
838
+ # Russian
839
+ ("Здравствуйте Г-�� Иванов.", "Здравствуйте господин Иванов.", "ru"),
840
+ ("Д-р Смирнов здесь, чтобы увидеть вас.", "доктор Смирнов здесь, чтобы увидеть вас.", "ru"),
841
+ # Turkish
842
+ ("Merhaba B. Yılmaz.", "Merhaba bay Yılmaz.", "tr"),
843
+ ("Dr. Ayşe burada.", "doktor Ayşe burada.", "tr"),
844
+ # Hungarian
845
+ ("Dr. Szabó itt van.", "doktor Szabó itt van.", "hu"),
846
+ ]
847
+
848
+ for a, b, lang in test_cases:
849
+ out = expand_abbreviations_multilingual(a, lang=lang)
850
+ assert out == b, f"'{out}' vs '{b}'"
851
+
852
+
853
+ def test_symbols_multilingual():
854
+ test_cases = [
855
+ ("I have 14% battery", "I have 14 percent battery", "en"),
856
+ ("Te veo @ la fiesta", "Te veo arroba la fiesta", "es"),
857
+ ("J'ai 14° de fièvre", "J'ai 14 degrés de fièvre", "fr"),
858
+ ("Die Rechnung beträgt £ 20", "Die Rechnung beträgt pfund 20", "de"),
859
+ ("O meu email é ana&joao@gmail.com", "O meu email é ana e joao arroba gmail.com", "pt"),
860
+ ("linguaggio di programmazione C#", "linguaggio di programmazione C cancelletto", "it"),
861
+ ("Moja temperatura to 36.6°", "Moja temperatura to 36.6 stopnie", "pl"),
862
+ ("Mám 14% baterie", "Mám 14 procento baterie", "cs"),
863
+ ("Těším se na tebe @ party", "Těším se na tebe na party", "cs"),
864
+ ("У меня 14% заряда", "У меня 14 процентов заряда", "ru"),
865
+ ("Я буду @ дома", "Я буду собака дома", "ru"),
866
+ ("Ik heb 14% batterij", "Ik heb 14 procent batterij", "nl"),
867
+ ("Ik zie je @ het feest", "Ik zie je bij het feest", "nl"),
868
+ ("لدي 14% في البطارية", "لدي 14 في المئة في البطارية", "ar"),
869
+ ("我的电量为 14%", "我的电量为 14 百分之", "zh"),
870
+ ("Pilim %14 dolu.", "Pilim yüzde 14 dolu.", "tr"),
871
+ ("Az akkumulátorom töltöttsége 14%", "Az akkumulátorom töltöttsége 14 százalék", "hu"),
872
+ ("배터리 잔량이 14%입니다.", "배터리 잔량이 14 퍼센트입니다.", "ko"),
873
+ ]
874
+
875
+ for a, b, lang in test_cases:
876
+ out = expand_symbols_multilingual(a, lang=lang)
877
+ assert out == b, f"'{out}' vs '{b}'"
878
+
879
+
880
+ if __name__ == "__main__":
881
+ test_expand_numbers_multilingual()
882
+ test_abbreviations_multilingual()
883
+ test_symbols_multilingual()
models/lyrics_utils/vocab.json ADDED
The diff for this file is too large to render. See raw diff
 
models/lyrics_utils/zh_num2words.py ADDED
@@ -0,0 +1,1209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Authors:
2
+ # 2019.5 Zhiyang Zhou (https://github.com/Joee1995/chn_text_norm.git)
3
+ # 2019.9 - 2022 Jiayu DU
4
+ #copy from https://github.com/coqui-ai/TTS/blob/dbf1a08a0d4e47fdad6172e433eeb34bc6b13b4e/TTS/tts/layers/xtts/zh_num2words.py
5
+ import argparse
6
+ import csv
7
+ import os
8
+ import re
9
+ import string
10
+ import sys
11
+
12
+ # fmt: off
13
+
14
+ # ================================================================================ #
15
+ # basic constant
16
+ # ================================================================================ #
17
+ CHINESE_DIGIS = "零一二三四五六七八九"
18
+ BIG_CHINESE_DIGIS_SIMPLIFIED = "零壹贰叁肆伍陆柒捌玖"
19
+ BIG_CHINESE_DIGIS_TRADITIONAL = "零壹貳參肆伍陸柒捌玖"
20
+ SMALLER_BIG_CHINESE_UNITS_SIMPLIFIED = "十百千万"
21
+ SMALLER_BIG_CHINESE_UNITS_TRADITIONAL = "拾佰仟萬"
22
+ LARGER_CHINESE_NUMERING_UNITS_SIMPLIFIED = "亿兆京垓秭穰沟涧正载"
23
+ LARGER_CHINESE_NUMERING_UNITS_TRADITIONAL = "億兆京垓秭穰溝澗正載"
24
+ SMALLER_CHINESE_NUMERING_UNITS_SIMPLIFIED = "十百千万"
25
+ SMALLER_CHINESE_NUMERING_UNITS_TRADITIONAL = "拾佰仟萬"
26
+
27
+ ZERO_ALT = "〇"
28
+ ONE_ALT = "幺"
29
+ TWO_ALTS = ["两", "兩"]
30
+
31
+ POSITIVE = ["正", "正"]
32
+ NEGATIVE = ["负", "負"]
33
+ POINT = ["点", "點"]
34
+ # PLUS = [u'加', u'加']
35
+ # SIL = [u'杠', u'槓']
36
+
37
+ FILLER_CHARS = ["呃", "啊"]
38
+
39
+ ER_WHITELIST = (
40
+ "(儿女|儿子|儿孙|女儿|儿媳|妻儿|"
41
+ "胎儿|婴儿|新生儿|婴幼儿|幼儿|少儿|小儿|儿歌|儿童|儿科|托儿所|孤儿|"
42
+ "儿戏|儿化|台儿庄|鹿儿岛|正儿八经|吊儿郎当|生儿育女|托儿带女|养儿防老|痴儿呆女|"
43
+ "佳儿佳妇|儿怜兽扰|儿无常父|儿不嫌母丑|儿行千里母担忧|儿大不由爷|苏乞儿)"
44
+ )
45
+ ER_WHITELIST_PATTERN = re.compile(ER_WHITELIST)
46
+
47
+ # 中文数字系统类型
48
+ NUMBERING_TYPES = ["low", "mid", "high"]
49
+
50
+ CURRENCY_NAMES = "(人民币|美元|日元|英镑|欧元|马克|法郎|加拿大元|澳元|港币|先令|芬兰马克|爱尔兰镑|" "里拉|荷兰盾|埃斯库多|比塞塔|印尼盾|林吉特|新西兰元|比索|卢布|新加坡元|韩元|泰铢)"
51
+ CURRENCY_UNITS = "((亿|千万|百万|万|千|百)|(亿|千万|百万|万|千|百|)元|(亿|千万|百万|万|千|百|)块|角|毛|分)"
52
+ COM_QUANTIFIERS = (
53
+ "(匹|张|座|回|场|尾|条|个|首|阙|阵|网|炮|顶|丘|棵|只|支|袭|辆|挑|担|颗|壳|窠|曲|墙|群|腔|"
54
+ "砣|座|客|贯|扎|捆|刀|令|打|手|罗|坡|山|岭|江|溪|钟|队|单|双|对|出|口|头|脚|板|跳|枝|件|贴|"
55
+ "针|线|管|名|位|身|堂|课|本|页|家|户|层|丝|毫|厘|分|钱|两|斤|担|铢|石|钧|锱|忽|(千|毫|微)克|"
56
+ "毫|厘|分|寸|尺|丈|里|寻|常|铺|程|(千|分|厘|毫|微)米|撮|勺|合|升|斗|石|盘|碗|碟|叠|桶|笼|盆|"
57
+ "盒|杯|钟|斛|锅|簋|篮|盘|桶|罐|瓶|壶|卮|盏|箩|箱|煲|啖|袋|钵|年|月|日|季|刻|时|周|天|秒|分|旬|"
58
+ "纪|岁|世|更|夜|春|夏|秋|冬|代|伏|辈|丸|泡|粒|颗|幢|堆|条|根|支|道|面|片|张|颗|块)"
59
+ )
60
+
61
+
62
+ # Punctuation information are based on Zhon project (https://github.com/tsroten/zhon.git)
63
+ CN_PUNCS_STOP = "!?。。"
64
+ CN_PUNCS_NONSTOP = ""#$%&'()*+,-/:;<=>@[\]^_`{|}~⦅⦆「」、、〃《》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏·〈〉-"
65
+ CN_PUNCS = CN_PUNCS_STOP + CN_PUNCS_NONSTOP
66
+
67
+ PUNCS = CN_PUNCS + string.punctuation
68
+ PUNCS_TRANSFORM = str.maketrans(PUNCS, "," * len(PUNCS), "") # replace puncs with English comma
69
+
70
+
71
+ # https://zh.wikipedia.org/wiki/全行和半行
72
+ QJ2BJ = {
73
+ " ": " ",
74
+ "!": "!",
75
+ """: '"',
76
+ "#": "#",
77
+ "$": "$",
78
+ "%": "%",
79
+ "&": "&",
80
+ "'": "'",
81
+ "(": "(",
82
+ ")": ")",
83
+ "*": "*",
84
+ "+": "+",
85
+ ",": ",",
86
+ "-": "-",
87
+ ".": ".",
88
+ "/": "/",
89
+ "0": "0",
90
+ "1": "1",
91
+ "2": "2",
92
+ "3": "3",
93
+ "4": "4",
94
+ "5": "5",
95
+ "6": "6",
96
+ "7": "7",
97
+ "8": "8",
98
+ "9": "9",
99
+ ":": ":",
100
+ ";": ";",
101
+ "<": "<",
102
+ "=": "=",
103
+ ">": ">",
104
+ "?": "?",
105
+ "@": "@",
106
+ "A": "A",
107
+ "B": "B",
108
+ "C": "C",
109
+ "D": "D",
110
+ "E": "E",
111
+ "F": "F",
112
+ "G": "G",
113
+ "H": "H",
114
+ "I": "I",
115
+ "J": "J",
116
+ "K": "K",
117
+ "L": "L",
118
+ "M": "M",
119
+ "N": "N",
120
+ "O": "O",
121
+ "P": "P",
122
+ "Q": "Q",
123
+ "R": "R",
124
+ "S": "S",
125
+ "T": "T",
126
+ "U": "U",
127
+ "V": "V",
128
+ "W": "W",
129
+ "X": "X",
130
+ "Y": "Y",
131
+ "Z": "Z",
132
+ "[": "[",
133
+ "\": "\\",
134
+ "]": "]",
135
+ "^": "^",
136
+ "_": "_",
137
+ "`": "`",
138
+ "a": "a",
139
+ "b": "b",
140
+ "c": "c",
141
+ "d": "d",
142
+ "e": "e",
143
+ "f": "f",
144
+ "g": "g",
145
+ "h": "h",
146
+ "i": "i",
147
+ "j": "j",
148
+ "k": "k",
149
+ "l": "l",
150
+ "m": "m",
151
+ "n": "n",
152
+ "o": "o",
153
+ "p": "p",
154
+ "q": "q",
155
+ "r": "r",
156
+ "s": "s",
157
+ "t": "t",
158
+ "u": "u",
159
+ "v": "v",
160
+ "w": "w",
161
+ "x": "x",
162
+ "y": "y",
163
+ "z": "z",
164
+ "{": "{",
165
+ "|": "|",
166
+ "}": "}",
167
+ "~": "~",
168
+ }
169
+ QJ2BJ_TRANSFORM = str.maketrans("".join(QJ2BJ.keys()), "".join(QJ2BJ.values()), "")
170
+
171
+
172
+ # 2013 China National Standard: https://zh.wikipedia.org/wiki/通用规范汉字表, raw resources:
173
+ # https://github.com/mozillazg/pinyin-data/blob/master/kMandarin_8105.txt with 8105 chinese chars in total
174
+ CN_CHARS_COMMON = (
175
+ "一丁七万丈三上下不与丏丐丑专且丕世丘丙业丛东丝丞丢两严丧个丫中丰串临丸丹为主丽举"
176
+ "乂乃久么义之乌乍乎乏乐乒乓乔乖乘乙乜九乞也习乡书乩买乱乳乸乾了予争事二亍于亏云互"
177
+ "亓五井亘亚些亟亡亢交亥亦产亨亩享京亭亮亲亳亵亶亸亹人亿什仁仂仃仄仅仆仇仉今介仍从"
178
+ "仑仓仔仕他仗付仙仝仞仟仡代令以仨仪仫们仰仲仳仵件价任份仿企伈伉伊伋伍伎伏伐休众优"
179
+ "伙会伛伞伟传伢伣伤伥伦伧伪伫伭伯估伲伴伶伸伺似伽伾佁佃但位低住佐佑体何佖佗佘余佚"
180
+ "佛作佝佞佟你佣佤佥佩佬佯佰佳佴佶佸佺佻佼佽佾使侁侂侃侄侈侉例侍侏侑侔侗侘供依侠侣"
181
+ "侥侦侧侨侩侪侬侮侯侴侵侹便促俄俅俊俍俎俏俐俑俗俘俙俚俜保俞俟信俣俦俨俩俪俫俭修俯"
182
+ "俱俳俵俶俸俺俾倌倍倏倒倓倔倕倘候倚倜倞借倡倥倦倧倨倩倪倬倭倮倴债倻值倾偁偃假偈偌"
183
+ "偎偏偓偕做停偡健偬偭偰偲偶偷偻偾偿傀傃傅傈傉傍傒傕傣傥傧储傩催傲傺傻僇僎像僔僖僚"
184
+ "僦僧僬僭僮僰僳僵僻儆儇儋儒儡儦儳儴儿兀允元兄充兆先光克免兑兔兕兖党兜兢入全八公六"
185
+ "兮兰共关兴兵其具典兹养兼兽冀冁内冈冉册再冏冒冔冕冗写军农冠冢冤冥冬冮冯冰冱冲决况"
186
+ "冶冷冻冼冽净凄准凇凉凋凌减凑凓凘凛凝几凡凤凫凭凯凰凳凶凸凹出击凼函凿刀刁刃分切刈"
187
+ "刊刍刎刑划刖列刘则刚创初删判刨利别刬刭刮到刳制刷券刹刺刻刽刿剀剁剂剃剅削剋剌前剐"
188
+ "剑剔剕剖剜剞剟剡剥剧剩剪副割剽剿劁劂劄劈劐劓力劝办功加务劢劣动助努劫劬劭励劲劳劼"
189
+ "劾势勃勇勉勋勍勐勒勔勖勘勚募勠勤勰勺勾勿匀包匆匈匍匏匐匕化北匙匜匝匠匡匣匦匪匮匹"
190
+ "区医匼匾匿十千卅升午卉半华协卑卒卓单卖南博卜卞卟占卡卢卣卤卦卧卫卬卮卯印危即却卵"
191
+ "卷卸卺卿厂厄厅历厉压厌厍厕厖厘厚厝原厢厣厥厦厨厩厮去厾县叁参叆叇又叉及友双反发叔"
192
+ "叕取受变叙叚叛叟叠口古句另叨叩只叫召叭叮可台叱史右叵叶号司叹叻叼叽吁吃各吆合吉吊"
193
+ "同名后吏吐向吒吓吕吖吗君吝吞吟吠吡吣否吧吨吩含听吭吮启吱吲吴吵吸吹吻吼吽吾呀呃呆"
194
+ "呇呈告呋呐呒呓呔呕呖呗员呙呛呜呢呣呤呦周呱呲味呵呶呷呸呻呼命咀咂咄咆咇咉咋和咍咎"
195
+ "咏咐咒咔咕咖咙咚咛咝咡咣咤咥咦咧咨咩咪咫咬咯咱咳咴咸咺咻咽咿哀品哂哃哄哆哇哈哉哌"
196
+ "响哎哏哐哑哒哓哔哕哗哙哚哝哞哟哢哥哦哧哨哩哪哭哮哱哲哳哺哼哽哿唁唆唇唉唏唐唑唔唛"
197
+ "唝唠唢唣唤唧唪唬售唯唰唱唳唵唷唼唾唿啁啃啄商啉啊啐啕啖啜啡啤啥啦啧啪啫啬啭啮啰啴"
198
+ "啵啶啷啸啻啼啾喀喁喂喃善喆喇喈喉喊喋喏喑喔喘喙喜喝喟喤喧喱喳喵喷喹喻喽喾嗄嗅嗉嗌"
199
+ "嗍嗐嗑嗒嗓嗔嗖嗜嗝嗞嗟嗡嗣嗤嗥嗦嗨嗪嗫嗬嗯嗲嗳嗵嗷嗽嗾嘀嘁嘈嘉嘌嘎嘏嘘嘚嘛嘞嘟嘡"
200
+ "嘣嘤嘧嘬嘭嘱嘲嘴嘶嘹嘻嘿噀噂噇噌噍噎噔噗噘噙噜噢噤器噩噪噫噬噱噶噻噼嚄嚅嚆嚎嚏嚓"
201
+ "嚚嚣嚭嚯嚷嚼囊囔囚四回囟因囡团囤囫园困囱围囵囷囹固国图囿圃圄圆圈圉圊圌圐圙圜土圢"
202
+ "圣在圩圪圫圬圭圮圯地圲圳圹场圻圾址坂均坉坊坋坌坍坎坏坐坑坒块坚坛坜坝坞坟坠坡坤坥"
203
+ "坦坨坩坪坫坬坭坯坰坳坷坻坼坽垂垃垄垆垈型垌垍垎垏垒垓垕垙垚垛垞垟垠垡垢垣垤垦垧垩"
204
+ "垫垭垮垯垱垲垴垵垸垺垾垿埂埃埆埇埋埌城埏埒埔埕埗埘埙埚埝域埠埤埪埫埭埯埴埵埸培基"
205
+ "埼埽堂堃堆堇堉堋堌堍堎堐堑堕堙堞堠堡堤堧堨堪堰堲堵堼堽堾塄塅塆塌塍塑塔塘塝塞塥填"
206
+ "塬塱塾墀墁境墅墈墉墐墒墓墕墘墙墚增墟墡墣墦墨墩墼壁壅壑壕壤士壬壮声壳壶壸壹处备复"
207
+ "夏夐夔夕外夙多夜够夤夥大天太夫夬夭央夯失头夷夸夹夺夼奁奂奄奇奈奉奋奎奏契奓奔奕奖"
208
+ "套奘奚奠奡奢奥奭女奴奶奸她好妁如妃妄妆妇妈妊妍妒妓妖妗妘妙妞妣妤妥妧妨妩妪妫妭妮"
209
+ "妯妲妹妻妾姆姈姊始姐姑姒姓委姗姘姚姜姝姞姣姤姥姨姬姮姱姶姹姻姽姿娀威娃娄娅娆娇娈"
210
+ "娉娌娑娓娘娜娟娠娣娥娩娱娲娴娵娶娼婀婆婉婊婌婍婕婘婚婞婠婢婤婧婪婫婳婴婵婶婷婺���"
211
+ "婼婿媂媄媆媒媓媖媚媛媞媪媭媱媲媳媵媸媾嫁嫂嫄嫉嫌嫒嫔嫕嫖嫘嫚嫜嫠嫡嫣嫦嫩嫪嫫嫭嫱"
212
+ "嫽嬉嬖嬗嬛嬥嬬嬴嬷嬿孀孅子孑孓孔孕孖字存孙孚孛孜孝孟孢季孤孥学孩孪孬孰孱孳孵孺孽"
213
+ "宁它宄宅宇守安宋完宏宓宕宗官宙定宛宜宝实宠审客宣室宥宦宧宪宫宬宰害宴宵家宸容宽宾"
214
+ "宿寁寂寄寅密寇富寐寒寓寝寞察寡寤寥寨寮寰寸对寺寻导寿封射将尉尊小少尔尕尖尘尚尜尝"
215
+ "尢尤尥尧尨尪尬就尴尸尹尺尻尼尽尾尿局屁层屃居屈屉届屋屎屏屐屑展屙属屠屡屣履屦屯山"
216
+ "屹屺屼屾屿岁岂岈岊岌岍岐岑岔岖岗岘岙岚岛岜岞岠岢岣岨岩岫岬岭岱岳岵岷岸岽岿峁峂峃"
217
+ "峄峋峒峗峘峙峛峡峣峤峥峦峧峨峪峭峰峱峻峿崀崁崂崃崄崆崇崌崎崒崔崖崚崛崞崟崡崤崦崧"
218
+ "崩崭崮崴崶崽崾崿嵁嵅嵇嵊嵋嵌嵎嵖嵘嵚嵛嵝嵩嵫嵬嵯嵲嵴嶂嶅嶍嶒嶓嶙嶝嶟嶦嶲嶷巅巇巉"
219
+ "巍川州巡巢工左巧巨巩巫差巯己已巳巴巷巽巾币市布帅帆师希帏帐帑帔帕帖帘帙帚帛帜帝帡"
220
+ "带帧帨席帮帱帷常帻帼帽幂幄幅幌幔幕幖幛幞幡幢幪干平年并幸幺幻幼幽广庄庆庇床庋序庐"
221
+ "庑库应底庖店庙庚府庞废庠庤庥度座庭庱庳庵庶康庸庹庼庾廆廉廊廋廑廒廓廖廙廛廨廪延廷"
222
+ "建廿开弁异弃弄弆弇弈弊弋式弑弓引弗弘弛弟张弢弥弦弧弨弩弭弯弱弶弸弹强弼彀归当录彖"
223
+ "彗彘彝彟形彤彦彧彩彪彬彭彰影彳彷役彻彼往征徂径待徇很徉徊律徐徒徕得徘徙徛徜御徨循"
224
+ "徭微徵德徼徽心必忆忉忌忍忏忐忑忒忖志忘忙忝忞忠忡忤忧忪快忭忮忱忳念忸忺忻忽忾忿怀"
225
+ "态怂怃怄怅怆怊怍怎怏怒怔怕怖怙怛怜思怠怡急怦性怨怩怪怫怯怵总怼怿恁恂恃恋恍恐恒恓"
226
+ "恔恕恙恚恝恢恣恤恧恨恩恪恫恬恭息恰恳恶恸恹恺恻恼恽恿悃悄悆悈悉悌悍悒悔悖悚悛悝悟"
227
+ "悠悢患悦您悫悬悭悯悰悱悲悴悸悻悼情惆惇惊惋惎惑惔惕惘惙惚惛惜惝惟惠惦惧惨惩惫惬惭"
228
+ "惮惯惰想惴惶惹惺愀愁愃愆愈愉愍愎意愐愔愕愚感愠愣愤愦愧愫愭愿慆慈慊慌慎慑慕慝慢慥"
229
+ "慧慨慬慭慰慵慷憋憎憔憕憙憧憨憩憬憭憷憺憾懂懈懊懋懑懒懔懦懵懿戆戈戊戋戌戍戎戏成我"
230
+ "戒戕或戗战戚戛戟戡戢戣戤戥截戬戭戮戳戴户戽戾房所扁扂扃扅扆扇扈扉扊手才扎扑扒打扔"
231
+ "托扛扞扣扦执扩扪扫扬扭扮扯扰扳扶批扺扼扽找承技抃抄抉把抑抒抓抔投抖抗折抚抛抟抠抡"
232
+ "抢护报抨披抬抱抵抹抻押抽抿拂拃拄担拆拇拈拉拊拌拍拎拐拒拓拔拖拗拘拙招拜拟拢拣拤拥"
233
+ "拦拧拨择括拭拮拯拱拳拴拶拷拼拽拾拿持挂指挈按挎挑挓挖挚挛挝挞挟挠挡挣挤挥挦挨挪挫"
234
+ "振挲挹挺挽捂捃捅捆捉捋捌捍捎捏捐捕捞损捡换捣捧捩捭据捯捶捷捺捻捽掀掂掇授掉掊掌掎"
235
+ "掏掐排掖掘掞掠探掣接控推掩措掬掭掮掰掳掴掷掸掺掼掾揄揆揉揍描提插揕揖揠握揣揩揪揭"
236
+ "揳援揶揸揽揿搀搁搂搅搋搌搏搐搒搓搔搛搜搞搠搡搦搪搬搭搴携搽摁摄摅摆摇摈摊摏摒摔摘"
237
+ "摛摞摧摩摭摴摸摹摽撂撄撅撇撑撒撕撖撙撞撤撩撬播撮撰撵撷撸撺撼擀擂擅操擎擐擒擘擞擢"
238
+ "擤擦擿攀攉攒攘攥攫攮支收攸改攻攽放政故效敉敌敏救敔敕敖教敛敝敞敢散敦敩敫敬数敲整"
239
+ "敷文斋斌斐斑斓斗料斛斜斝斟斠斡斤斥斧斩斫断斯新斶方於施旁旃旄旅旆旋旌旎族旐旒旖旗"
240
+ "旞无既日旦旧旨早旬旭旮旯旰旱旴旵时旷旸旺旻旿昀昂昃昄昆昇昈昉昊昌明昏昒易昔昕昙昝"
241
+ "星映昡昣昤春昧昨昪昫昭是昱昳昴昵昶昺昼昽显晁晃晅晊晋晌晏晐晒晓晔晕晖晗晙晚晞晟晡"
242
+ "晢晤晦晨晪晫普景晰晱晴晶晷智晾暂暄暅暇暌暑暕暖暗暝暧暨暮暲暴暵暶暹暾暿曈曌曙曛曜"
243
+ "曝曦曩曰曲曳更曷曹曼曾替最月有朋服朏朐朓朔朕朗望朝期朦木未末本札术朱朳朴朵朸机朽"
244
+ "杀杂权杄杆杈杉杌李杏材村杓杕杖杙杜杞束杠条来杧杨杩杪杭杯杰杲杳杵杷杻杼松板极构枅"
245
+ "枇枉枋枍析枕林枘枚果枝枞枢枣枥枧枨枪枫枭枯枰枲枳枵架枷枸枹柁柃柄柈柊柏某柑柒染柔"
246
+ "柖柘柙柚柜柝柞柠柢查柩柬柯柰柱柳柴柷柽柿栀栅标栈栉栊栋栌栎栏栐树栒栓栖栗栝栟校栩"
247
+ "株栲栳栴样核根栻格栽栾桀桁桂桃桄桅框案桉桊桌桎桐桑桓桔桕桠桡桢档桤桥桦桧桨桩桫桯"
248
+ "桲桴桶桷桹梁梃梅梆梌梏梓梗梠梢梣梦梧梨梭梯械梳梴梵梼梽梾梿检棁棂棉棋棍棐棒棓棕棘"
249
+ "棚棠棣棤棨棪棫棬森棰棱棵棹棺棻棼棽椀椁椅椆椋植椎椐椑椒椓椟椠椤椪椭椰椴椸椹椽椿楂"
250
+ "楒楔楗楙楚楝楞楠楣楦楩楪楫楮楯楷楸楹楼概榃榄榅榆榇榈榉榍榑榔榕榖榛榜榧榨榫榭榰���"
251
+ "榴榷榻槁槃槊槌槎槐槔槚槛槜槟槠槭槱槲槽槿樊樗樘樟模樨横樯樱樵樽樾橄橇橐橑橘橙橛橞"
252
+ "橡橥橦橱橹橼檀檄檎檐檑檗檞檠檩檫檬櫆欂欠次欢欣欤欧欲欸欹欺欻款歃歅歆歇歉歌歙止正"
253
+ "此步武歧歪歹死歼殁殂殃殄殆殇殉殊残殍殒殓殖殚殛殡殣殪殳殴段殷殿毁毂毅毋毌母每毐毒"
254
+ "毓比毕毖毗毙毛毡毪毫毯毳毵毹毽氅氆氇氍氏氐民氓气氕氖氘氙氚氛氟氡氢氤氦氧氨氩氪氮"
255
+ "氯氰氲水永氾氿汀汁求汆汇汈汉汊汋汐汔汕汗汛汜汝汞江池污汤汧汨汩汪汫汭汰汲汴汶汹汽"
256
+ "汾沁沂沃沄沅沆沇沈沉沌沏沐沓沔沘沙沚沛沟没沣沤沥沦沧沨沩沪沫沭沮沱河沸油沺治沼沽"
257
+ "沾沿泂泃泄泅泇泉泊泌泐泓泔法泖泗泙泚泛泜泞泠泡波泣泥注泪泫泮泯泰泱泳泵泷泸泺泻泼"
258
+ "泽泾洁洄洇洈洋洌洎洑洒洓洗洘洙洚洛洞洢洣津洧洨洪洫洭洮洱洲洳洴洵洸洹洺活洼洽派洿"
259
+ "流浃浅浆浇浈浉浊测浍济浏浐浑浒浓浔浕浙浚浛浜浞浟浠浡浣浥浦浩浪浬浭浮浯浰浲浴海浸"
260
+ "浼涂涄涅消涉涌涍涎涐涑涓涔涕涘涛涝涞涟涠涡涢涣涤润涧涨涩涪涫涮涯液涴涵涸涿淀淄淅"
261
+ "淆淇淋淌淏淑淖淘淙淜淝淞淟淠淡淤淦淫淬淮淯深淳淴混淹添淼清渊渌渍渎渐渑渔渗渚渝渟"
262
+ "渠渡渣渤渥温渫渭港渰渲渴游渺渼湃湄湉湍湎湑湓湔湖湘湛湜湝湟湣湫湮湲湴湾湿溁溃溅溆"
263
+ "溇溉溍溏源溘溚溜溞溟溠溢溥溦溧溪溯溱溲溴溵溶溷溹溺溻溽滁滂滃滆滇滉滋滍滏滑滓滔滕"
264
+ "滗滘滚滞滟滠满滢滤滥滦滧滨滩滪滫滴滹漂漆漈漉漋漏漓演漕漖漠漤漦漩漪漫漭漯漱漳漴漶"
265
+ "漷漹漻漼漾潆潇潋潍潏潖潘潜潞潟潢潦潩潭潮潲潴潵潸潺潼潽潾澂澄澈澉澌澍澎澛澜澡澥澧"
266
+ "澪澭澳澴澶澹澼澽激濂濉濋濑濒濞濠濡濩濮濯瀌瀍瀑瀔瀚瀛瀣瀱瀵瀹瀼灈灌灏灞火灭灯灰灵"
267
+ "灶灸灼灾灿炀炅炆炉炊炌炎炒炔炕炖炘炙炜炝炟炣炫炬炭炮炯炱炳炷炸点炻炼炽烀烁烂烃烈"
268
+ "烊烔烘烙烛烜烝烟烠烤烦烧烨烩烫烬热烯烶烷烹烺烻烽焆焉焊焌焐焓焕焖焗焘焙焚焜焞焦焯"
269
+ "焰焱然煁煃煅煊煋煌煎煓煜煞煟煤煦照煨煮煲煳煴煸煺煽熄熇熊熏熔熘熙熛熜熟熠熥熨熬熵"
270
+ "熹熻燃燊燋燎燏燔燕燚燠燥燧燮燹爆爇爔爚爝爟爨爪爬爰爱爵父爷爸爹爻爽爿牁牂片版牌牍"
271
+ "牒牖牙牚牛牝牟牡牢牤牥牦牧物牮牯牲牵特牺牻牾牿犀犁犄犇犊犋犍犏犒犟犨犬犯犰犴状犷"
272
+ "犸犹狁狂狃狄狈狉狍狎狐狒狗狙狝狞狠狡狨狩独狭狮狯狰狱狲狳狴狷狸狺狻狼猁猃猄猇猊猎"
273
+ "猕猖猗猛猜猝猞猡猢猥猩猪猫猬献猯猰猱猴猷猹猺猾猿獍獐獒獗獠獬獭獯獴獾玃玄率玉王玎"
274
+ "玑玒玓玕玖玘玙玚玛玞玟玠玡玢玤玥玦玩玫玭玮环现玱玲玳玶玷玹玺玻玼玿珀珂珅珇珈珉珊"
275
+ "珋珌珍珏珐珑珒珕珖珙珛珝珞珠珢珣珥珦珧珩珪珫班珰珲珵珷珸珹珺珽琀球琄琅理琇琈琉琊"
276
+ "琎琏琐琔琚琛琟琡琢琤琥琦琨琪琫琬琭琮琯琰琲琳琴琵琶琼瑀瑁瑂瑃瑄瑅瑆瑑瑓瑔瑕瑖瑗瑙"
277
+ "瑚瑛瑜瑝瑞瑟瑢瑧瑨瑬瑭瑰瑱瑳瑶瑷瑾璀璁璃璆璇璈璋璎璐璒璘璜璞璟璠璥璧璨璩璪璬璮璱"
278
+ "璲璺瓀瓒瓖瓘瓜瓞瓠瓢瓣瓤瓦瓮瓯瓴瓶瓷瓻瓿甄甍甏甑甓甗甘甚甜生甡甥甦用甩甪甫甬甭甯"
279
+ "田由甲申电男甸町画甾畀畅畈畋界畎畏畔畖留畚畛畜畤略畦番畬畯畲畴畸畹畿疁疃疆疍疏疐"
280
+ "疑疔疖疗疙疚疝疟疠疡疢疣疤疥疫疬疭疮疯疰疱疲疳疴疵疸疹疼疽疾痂痃痄病症痈痉痊痍痒"
281
+ "痓痔痕痘痛痞痢痣痤痦痧痨痪痫痰痱痴痹痼痿瘀瘁瘃瘅瘆瘊瘌瘐瘕瘗瘘瘙瘛瘟瘠瘢瘤瘥瘦瘩"
282
+ "瘪瘫瘭瘰瘳瘴瘵瘸瘼瘾瘿癀癃癌癍癔癖癗癜癞癣癫癯癸登白百癿皂的皆皇皈皋皎皑皓皕皖皙"
283
+ "皛皞皤皦皭皮皱皲皴皿盂盅盆盈盉益盍盎盏盐监盒盔盖盗盘盛盟盥盦目盯盱盲直盷相盹盼盾"
284
+ "省眄眇眈眉眊看眍眙眚真眠眢眦眨眩眬眭眯眵眶眷眸眺眼着睁睃睄睇睎睐睑睚睛睡睢督睥睦"
285
+ "睨睫睬睹睽睾睿瞀瞄瞅瞋瞌瞍瞎瞑瞒瞟瞠瞢瞥瞧瞩瞪瞫瞬瞭瞰瞳瞵瞻瞽瞿矍矗矛矜矞矢矣知"
286
+ "矧矩矫矬短矮矰石矶矸矻矼矾矿砀码砂砄砆砉砌砍砑砒研砖砗砘砚砜砝砟砠砣砥砧砫砬砭砮"
287
+ "砰破砵砷砸砹砺砻砼砾础硁硅硇硊硌硍硎硐硒硔硕硖硗硙硚硝硪硫硬硭确硼硿碃碇碈碉碌碍"
288
+ "碎碏碑碓碗碘碚碛碜碟碡碣碥碧碨碰碱碲碳碴碶碹碾磁磅磉磊磋磏磐磔磕磙磜磡磨磬磲磴磷"
289
+ "磹磻礁礅礌礓礞礴礵示礼社祀祁祃祆祇祈祉祊祋祎祏祐祓祕祖祗祚祛祜祝神祟祠祢祥祧票祭"
290
+ "祯祲祷祸祺祼祾禀禁禄禅禊禋福禒禔禘禚禛禤禧禳禹禺离禽禾秀私秃秆秉秋种科秒秕秘租���"
291
+ "秤秦秧秩秫秬秭积称秸移秽秾稀稂稃稆程稌稍税稑稔稗稙稚稞稠稣稳稷稹稻稼稽稿穄穆穑穗"
292
+ "穙穜穟穰穴究穷穸穹空穿窀突窃窄窅窈窊窍窎窑窒窕窖窗窘窜窝窟窠窣窥窦窨窬窭窳窸窿立"
293
+ "竑竖竘站竞竟章竣童竦竫竭端竹竺竽竿笃笄笆笈笊笋笏笑笔笕笙笛笞笠笤笥符笨笪笫第笮笯"
294
+ "笱笳笸笺笼笾筀筅筇等筋筌筏筐筑筒答策筘筚筛筜筝筠筢筤筥筦筮筱筲筵筶筷筹筻筼签简箅"
295
+ "箍箐箓箔箕箖算箜管箢箦箧箨箩箪箫箬箭箱箴箸篁篆篇篌篑篓篙篚篝篡篥篦篪篮篯篱篷篼篾"
296
+ "簃簇簉簋簌簏簕簖簝簟簠簧簪簰簸簿籀籁籍籥米籴类籼籽粉粑粒粕粗粘粜粝粞粟粢粤粥粪粮"
297
+ "粱粲粳粹粼粽精粿糁糅糇糈糊糌糍糒糕糖糗糙糜糟糠糨糯糵系紊素索紧紫累絜絮絷綦綮縠縢"
298
+ "縻繁繄繇纂纛纠纡红纣纤纥约级纨纩纪纫纬纭纮纯纰纱纲纳纴纵纶纷纸纹纺纻纼纽纾线绀绁"
299
+ "绂练组绅细织终绉绊绋绌绍绎经绐绑绒结绔绕绖绗绘给绚绛络绝绞统绠绡绢绣绤绥绦继绨绩"
300
+ "绪绫续绮绯绰绱绲绳维绵绶绷绸绹绺绻综绽绾绿缀缁缂缃缄缅缆缇缈缉缊缌缎缐缑缒缓缔缕"
301
+ "编缗缘缙缚缛缜缝缞缟缠缡缢缣缤缥缦缧缨缩缪缫缬缭缮缯缰缱缲缳缴缵缶缸缺罂罄罅罍罐"
302
+ "网罔罕罗罘罚罟罡罢罨罩罪置罱署罴罶罹罽罾羁羊羌美羑羓羔羕羖羚羝羞羟羡群羧羯羰羱羲"
303
+ "羸羹羼羽羿翀翁翂翃翅翈翊翌翎翔翕翘翙翚翛翟翠翡翥翦翩翮翯翰翱翳翷翻翼翾耀老考耄者"
304
+ "耆耇耋而耍耏耐耑耒耔耕耖耗耘耙耜耠耢耤耥耦耧耨耩耪耰耱耳耵耶耷耸耻耽耿聂聃聆聊聋"
305
+ "职聍聒联聘聚聩聪聱聿肃肄肆肇肉肋肌肓肖肘肚肛肝肟肠股肢肤肥肩肪肫肭肮肯肱育肴肷肸"
306
+ "肺肼肽肾肿胀胁胂胃胄胆胈背胍胎胖胗胙胚胛胜胝胞胠胡胣胤胥胧胨胩胪胫胬胭胯胰胱胲胳"
307
+ "胴胶胸胺胼能脂脆脉脊脍脎脏脐脑脒脓脔脖脘脚脞脟脩脬脯脱脲脶脸脾脿腆腈腊腋腌腐腑腒"
308
+ "腓腔腕腘腙腚腠腥腧腨腩腭腮腯腰腱腴腹腺腻腼腽腾腿膀膂膈膊膏膑膘膙膛膜膝膦膨膳膺膻"
309
+ "臀臂臃臆臊臌臑臜臣臧自臬臭至致臻臼臾舀舁舂舄舅舆舌舍舐舒舔舛舜舞舟舠舢舣舥航舫般"
310
+ "舭舯舰舱舲舳舴舵舶舷舸船舻舾艄艅艇艉艋艎艏艘艚艟艨艮良艰色艳艴艺艽艾艿节芃芄芈芊"
311
+ "芋芍芎芏芑芒芗芘芙芜芝芟芠芡芣芤芥芦芨芩芪芫芬芭芮芯芰花芳芴芷芸芹芼芽芾苁苄苇苈"
312
+ "苉苊苋苌苍苎苏苑苒苓苔苕苗苘苛苜苞苟苠苡苣苤若苦苧苫苯英苴苷苹苻苾茀茁茂范茄茅茆"
313
+ "茈茉茋茌茎茏茑茓茔茕茗茚茛茜茝茧茨茫茬茭茯茱茳茴茵茶茸茹茺茼茽荀荁荃荄荆荇草荏荐"
314
+ "荑荒荓荔荖荙荚荛荜荞荟荠荡荣荤荥荦荧荨荩荪荫荬荭荮药荷荸荻荼荽莅莆莉莎莒莓莘莙莛"
315
+ "莜莝莞莠莨莩莪莫莰莱莲莳莴莶获莸莹莺莼莽莿菀菁菂菅菇菉菊菌菍菏菔菖菘菜菝菟菠菡菥"
316
+ "菩菪菰菱菲菹菼菽萁萃萄萆萋萌萍萎萏萑萘萚萜萝萣萤营萦萧萨萩萱萳萸萹萼落葆葎葑葖著"
317
+ "葙葚葛葜葡董葩葫葬葭葰葱葳葴葵葶葸葺蒂蒄蒇蒈蒉蒋蒌蒎蒐蒗蒙蒜蒟蒡蒨蒯蒱蒲蒴蒸蒹蒺"
318
+ "蒻蒽蒿蓁蓂蓄蓇蓉蓊蓍蓏蓐蓑蓓蓖蓝蓟蓠蓢蓣蓥蓦蓬蓰蓼蓿蔀蔃蔈蔊蔌蔑蔓蔗蔚蔟蔡蔫蔬蔷"
319
+ "蔸蔹蔺蔻蔼蔽蕃蕈蕉蕊蕖蕗蕙蕞蕤蕨蕰蕲蕴蕹蕺蕻蕾薁薄薅薇薏薛薜薢薤薨薪薮薯薰薳薷薸"
320
+ "薹薿藁藉藏藐藓藕藜藟藠藤藦藨藩藻藿蘅蘑蘖蘘蘧蘩蘸蘼虎虏虐虑虒虓虔虚虞虢虤虫虬虮虱"
321
+ "虷虸虹虺虻虼虽虾虿蚀蚁蚂蚄蚆蚊蚋蚌蚍蚓蚕蚜蚝蚣蚤蚧蚨蚩蚪蚬蚯蚰蚱蚲蚴蚶蚺蛀蛃蛄蛆"
322
+ "蛇蛉蛊蛋蛎蛏蛐蛑蛔蛘蛙蛛蛞蛟蛤蛩蛭蛮蛰蛱蛲蛳蛴蛸蛹蛾蜀蜂蜃蜇蜈蜉蜊蜍蜎蜐蜒蜓蜕蜗"
323
+ "蜘蜚蜜蜞蜡蜢蜣蜥蜩蜮蜱蜴蜷蜻蜾蜿蝇蝈蝉蝌蝎蝓蝗蝘蝙蝠蝣蝤蝥蝮蝰蝲蝴蝶蝻蝼蝽蝾螂螃"
324
+ "螅螈螋融螗螟螠螣螨螫螬螭螯螱螳螵螺螽蟀蟆蟊蟋蟏蟑蟒蟛蟠蟥蟪蟫蟮蟹蟾蠃蠊蠋蠓蠕蠖蠡"
325
+ "蠢蠲蠹蠼血衃衄衅行衍衎衒衔街衙衠衡衢衣补表衩衫衬衮衰衲衷衽衾衿袁袂袄袅袆袈袋袍袒"
326
+ "袖袗袜袢袤袪被袭袯袱袷袼裁裂装裆裈裉裎裒裔裕裘裙裛裟裢裣裤裥裨裰裱裳裴裸裹裼裾褂"
327
+ "褊褐褒褓褕褙褚褛褟褡褥褪褫褯褰褴褶襁襄襕襚襜襞襟襦襫襻西要覃覆见观觃规觅视觇览觉"
328
+ "觊觋觌觎觏觐觑角觖觚觜觞觟解觥触觫觭觯觱觳觿言訄訇訚訾詈詟詹誉誊誓謇警譬计订讣认"
329
+ "讥讦讧讨让讪讫训议讯记讱讲讳讴讵讶讷许讹论讻讼讽设访诀证诂诃评诅识诇诈诉诊诋诌词"
330
+ "诎诏诐译诒诓诔试诖诗诘诙诚诛诜话诞诟诠诡询诣诤该详诧诨诩诫诬语诮误诰诱诲诳说诵���"
331
+ "诸诹诺读诼诽课诿谀谁谂调谄谅谆谇谈谊谋谌谍谎谏谐谑谒谓谔谕谖谗谙谚谛谜谝谞谟谠谡"
332
+ "谢谣谤谥谦谧谨谩谪谫谬谭谮谯谰谱谲谳谴谵谶谷谼谿豁豆豇豉豌豕豚象豢豨豪豫豮豳豸豹"
333
+ "豺貂貅貆貉貊貌貔貘贝贞负贡财责贤败账货质贩贪贫贬购贮贯贰贱贲贳贴贵贶贷贸费贺贻贼"
334
+ "贽贾贿赀赁赂赃资赅赆赇赈赉赊赋赌赍赎赏赐赑赒赓赔赕赖赗赘赙赚赛赜赝赞赟赠赡赢赣赤"
335
+ "赦赧赪赫赭走赳赴赵赶起趁趄超越趋趑趔趟趣趯趱足趴趵趸趺趼趾趿跂跃跄跆跋跌跎跏跐跑"
336
+ "跖跗跚跛距跞跟跣跤跨跪跬路跱跳践跶跷跸跹跺跻跽踅踉踊踌踏踒踔踝踞踟踢踣踦踩踪踬踮"
337
+ "踯踱踵踶踹踺踽蹀蹁蹂蹄蹅蹇蹈蹉蹊蹋蹐蹑蹒蹙蹚蹜蹢蹦蹩蹬蹭蹯蹰蹲蹴蹶蹼蹽蹾蹿躁躅躇"
338
+ "躏躐躔躜躞身躬躯躲躺车轧轨轩轪轫转轭轮软轰轱轲轳轴轵轶轷轸轹轺轻轼载轾轿辀辁辂较"
339
+ "辄辅辆辇辈辉辊辋辌辍辎辏辐辑辒输辔辕辖辗辘辙辚辛辜辞辟辣辨辩辫辰辱边辽达辿迁迂迄"
340
+ "迅过迈迎运近迓返迕还这进远违连迟迢迤迥迦迨迩迪迫迭迮述迳迷迸迹迺追退送适逃逄逅逆"
341
+ "选逊逋逍透逐逑递途逖逗通逛逝逞速造逡逢逦逭逮逯逴逵逶逸逻逼逾遁遂遄遆遇遍遏遐遑遒"
342
+ "道遗遘遛遢遣遥遨遭遮遴遵遹遽避邀邂邃邈邋邑邓邕邗邘邙邛邝邠邡邢那邦邨邪邬邮邯邰邱"
343
+ "邲邳邴邵邶邸邹邺邻邽邾邿郁郃郄郅郇郈郊郎郏郐郑郓郗郚郛郜郝郡郢郤郦郧部郪郫郭郯郴"
344
+ "郸都郾郿鄀鄂鄃鄄鄅鄌鄑鄗鄘鄙鄚鄜鄞鄠鄢鄣鄫鄯鄱鄹酂酃酅酆酉酊酋酌配酎酏酐酒酗酚酝"
345
+ "酞酡酢酣酤酥酦酩酪酬酮酯酰酱酲酴酵酶酷酸酹酺酽酾酿醅醇醉醋醌醍醐醑醒醚醛醢醨醪醭"
346
+ "醮醯醴醵醺醾采釉释里重野量釐金釜鉴銎銮鋆鋈錾鍪鎏鏊鏖鐾鑫钆钇针钉钊钋钌钍钎钏钐钒"
347
+ "钓钔钕钖钗钘钙钚钛钜钝钞钟钠钡钢钣钤钥钦钧钨钩钪钫钬钭钮钯钰钱钲钳钴钵钷钹钺钻钼"
348
+ "钽钾钿铀铁铂铃铄铅铆铈铉铊铋铌铍铎铏铐铑铒铕铖铗铘铙铚铛铜铝铞铟铠铡铢铣铤铥铧铨"
349
+ "铩铪铫铬铭铮铯铰铱铲铳铴铵银铷铸铹铺铻铼铽链铿销锁锂锃锄锅锆锇锈锉锊锋锌锍锎锏锐"
350
+ "锑锒锓锔锕锖锗锘错锚锛锜锝锞锟锡锢锣锤锥锦锧锨锩锪锫锬锭键锯锰锱锲锳锴锵锶锷锸锹"
351
+ "锺锻锼锽锾锿镀镁镂镃镄镅镆镇镈镉镊镋镌镍镎镏镐镑镒镓镔镕镖镗镘镚镛镜镝镞镠镡镢镣"
352
+ "镤镥镦镧镨镩镪镫镬镭镮镯镰镱镲镳镴镵镶长门闩闪闫闭问闯闰闱闲闳间闵闶闷闸闹闺闻闼"
353
+ "闽闾闿阀阁阂阃阄阅阆阇阈阉阊阋阌阍阎阏阐阑阒阔阕阖阗阘阙阚阜队阡阪阮阱防阳阴阵阶"
354
+ "阻阼阽阿陀陂附际陆陇陈陉陋陌降陎限陑陔陕陛陞陟陡院除陧陨险陪陬陲陴陵陶陷隃隅隆隈"
355
+ "隋隍随隐隔隗隘隙障隧隩隰隳隶隹隺隼隽难雀雁雄雅集雇雉雊雌雍雎雏雒雕雠雨雩雪雯雱雳"
356
+ "零雷雹雾需霁霄霅霆震霈霉霍霎霏霓霖霜霞霨霪霭霰露霸霹霾青靓靖静靛非靠靡面靥革靬靰"
357
+ "靳靴靶靸靺靼靽靿鞁鞅鞋鞍鞑鞒鞔鞘鞠鞡鞣鞧鞨鞫鞬鞭鞮鞯鞲鞳鞴韂韦韧韨韩韪韫韬韭音韵"
358
+ "韶页顶顷顸项顺须顼顽顾顿颀颁颂颃预颅领颇颈颉颊颋颌颍颎颏颐频颓颔颖颗题颙颚颛颜额"
359
+ "颞颟颠颡颢颤颥颦颧风飏飐飑飒飓飔飕飗飘飙飞食飧飨餍餐餮饔饕饥饧饨饩饪饫饬饭饮饯饰"
360
+ "饱饲饳饴饵饶饷饸饹饺饻饼饽饿馁馃馄馅馆馇馈馉馊馋馌馍馏馐馑馒馓馔馕首馗馘香馝馞馥"
361
+ "馧馨马驭驮驯驰驱驲驳驴驵驶驷驸驹驺驻驼驽驾驿骀骁骂骃骄骅骆骇骈骉骊骋验骍骎骏骐骑"
362
+ "骒骓骕骖骗骘骙骚骛骜骝骞骟骠骡骢骣骤骥骦骧骨骰骱骶骷骸骺骼髀髁髂髃髅髋髌髎髑髓高"
363
+ "髡髢髦髫髭髯髹髻髽鬃鬈鬏鬒鬓鬘鬟鬣鬯鬲鬶鬷鬻鬼魁魂魃魄魅魆魇魈魉魋魍魏魑魔鱼鱽鱾"
364
+ "鱿鲀鲁鲂鲃鲅鲆鲇鲈鲉鲊鲋鲌鲍鲎鲏鲐鲑鲒鲔鲕鲖鲗鲘鲙鲚鲛鲜鲝鲞鲟鲠鲡鲢鲣鲤鲥鲦鲧鲨"
365
+ "鲩鲪鲫鲬鲭鲮鲯鲰鲱鲲鲳鲴鲵鲷鲸鲹鲺鲻鲼鲽鲾鲿鳀鳁鳂鳃鳄鳅鳇鳈鳉鳊鳌鳍鳎鳏鳐鳑鳒鳓"
366
+ "鳔鳕鳖鳗鳘鳙鳚鳛鳜鳝鳞鳟鳠鳡鳢鳣鳤鸟鸠鸡鸢鸣鸤鸥鸦鸧鸨鸩鸪鸫鸬鸭鸮鸯鸰鸱鸲鸳鸵鸶"
367
+ "鸷鸸鸹鸺鸻鸼鸽鸾鸿鹀鹁鹂鹃鹄鹅鹆鹇鹈鹉鹊鹋鹌鹍鹎鹏鹐鹑鹒鹔鹕鹖鹗鹘鹙鹚鹛鹜鹝鹞鹟"
368
+ "鹠鹡鹢鹣鹤鹦鹧鹨鹩鹪鹫鹬鹭鹮鹯鹰鹱鹲鹳鹴鹾鹿麀麂麇麈麋麑麒麓麖麝麟麦麸麹麻麽麾黄"
369
+ "黇黉黍黎黏黑黔默黛黜黝黟黠黡黢黥黧黩黪黯黹黻黼黾鼋鼍鼎鼐鼒鼓鼗鼙鼠鼢鼩鼫鼬鼯鼱鼷"
370
+ "鼹鼻鼽鼾齁齇齉齐齑齿龀龁龂龃龄龅龆龇龈龉龊龋龌龙龚龛龟龠龢鿍鿎鿏㑇㑊㕮㘎㙍㙘㙦���"
371
+ "㛚㛹㟃㠇㠓㤘㥄㧐㧑㧟㫰㬊㬎㬚㭎㭕㮾㰀㳇㳘㳚㴔㵐㶲㸆㸌㺄㻬㽏㿠䁖䂮䃅䃎䅟䌹䎃䎖䏝䏡"
372
+ "䏲䐃䓖䓛䓨䓫䓬䗖䗛䗪䗴䜣䝙䢺䢼䣘䥽䦃䲟䲠䲢䴓䴔䴕䴖䴗䴘䴙䶮𠅤𠙶𠳐𡎚𡐓𣗋𣲗𣲘𣸣𤧛𤩽"
373
+ "𤫉𥔲𥕢𥖨𥻗𦈡𦒍𦙶𦝼𦭜𦰡𧿹𨐈𨙸𨚕𨟠𨭉𨱇𨱏𨱑𨱔𨺙𩽾𩾃𩾌𪟝𪣻𪤗𪨰𪨶𪩘𪾢𫄧𫄨𫄷𫄸𫇭𫌀𫍣𫍯"
374
+ "𫍲𫍽𫐄𫐐𫐓𫑡𫓧𫓯𫓶𫓹𫔍𫔎𫔶𫖮𫖯𫖳𫗧𫗴𫘜𫘝𫘦𫘧𫘨𫘪𫘬𫚕𫚖𫚭𫛭𫞩𫟅𫟦𫟹𫟼𫠆𫠊𫠜𫢸𫫇𫭟"
375
+ "𫭢𫭼𫮃𫰛𫵷𫶇𫷷𫸩𬀩𬀪𬂩𬃊𬇕𬇙𬇹𬉼𬊈𬊤𬌗𬍛𬍡𬍤𬒈𬒔𬒗𬕂𬘓𬘘𬘡𬘩𬘫𬘬𬘭𬘯𬙂𬙊𬙋𬜬𬜯𬞟"
376
+ "𬟁𬟽𬣙𬣞𬣡𬣳𬤇𬤊𬤝𬨂𬨎𬩽𬪩𬬩𬬭𬬮𬬱𬬸𬬹𬬻𬬿𬭁𬭊𬭎𬭚𬭛𬭤𬭩𬭬𬭯𬭳𬭶𬭸𬭼𬮱𬮿𬯀𬯎𬱖𬱟"
377
+ "𬳵𬳶𬳽𬳿𬴂𬴃𬴊𬶋𬶍𬶏𬶐𬶟𬶠𬶨𬶭𬶮𬷕𬸘𬸚𬸣𬸦𬸪𬹼𬺈𬺓"
378
+ )
379
+ CN_CHARS_EXT = "吶诶屌囧飚屄"
380
+
381
+ CN_CHARS = CN_CHARS_COMMON + CN_CHARS_EXT
382
+ IN_CH_CHARS = {c: True for c in CN_CHARS}
383
+
384
+ EN_CHARS = string.ascii_letters + string.digits
385
+ IN_EN_CHARS = {c: True for c in EN_CHARS}
386
+
387
+ VALID_CHARS = CN_CHARS + EN_CHARS + " "
388
+ IN_VALID_CHARS = {c: True for c in VALID_CHARS}
389
+
390
+
391
+ # ================================================================================ #
392
+ # basic class
393
+ # ================================================================================ #
394
+ class ChineseChar(object):
395
+ """
396
+ 中文字符
397
+ 每个字符对应简体和繁体,
398
+ e.g. 简体 = '负', 繁体 = '負'
399
+ 转换时可转换为简体或繁体
400
+ """
401
+
402
+ def __init__(self, simplified, traditional):
403
+ self.simplified = simplified
404
+ self.traditional = traditional
405
+ # self.__repr__ = self.__str__
406
+
407
+ def __str__(self):
408
+ return self.simplified or self.traditional or None
409
+
410
+ def __repr__(self):
411
+ return self.__str__()
412
+
413
+
414
+ class ChineseNumberUnit(ChineseChar):
415
+ """
416
+ 中文数字/数位字符
417
+ 每个字符除繁简体外还有一个额外的大写字符
418
+ e.g. '陆' 和 '陸'
419
+ """
420
+
421
+ def __init__(self, power, simplified, traditional, big_s, big_t):
422
+ super(ChineseNumberUnit, self).__init__(simplified, traditional)
423
+ self.power = power
424
+ self.big_s = big_s
425
+ self.big_t = big_t
426
+
427
+ def __str__(self):
428
+ return "10^{}".format(self.power)
429
+
430
+ @classmethod
431
+ def create(cls, index, value, numbering_type=NUMBERING_TYPES[1], small_unit=False):
432
+ if small_unit:
433
+ return ChineseNumberUnit(
434
+ power=index + 1, simplified=value[0], traditional=value[1], big_s=value[1], big_t=value[1]
435
+ )
436
+ elif numbering_type == NUMBERING_TYPES[0]:
437
+ return ChineseNumberUnit(
438
+ power=index + 8, simplified=value[0], traditional=value[1], big_s=value[0], big_t=value[1]
439
+ )
440
+ elif numbering_type == NUMBERING_TYPES[1]:
441
+ return ChineseNumberUnit(
442
+ power=(index + 2) * 4, simplified=value[0], traditional=value[1], big_s=value[0], big_t=value[1]
443
+ )
444
+ elif numbering_type == NUMBERING_TYPES[2]:
445
+ return ChineseNumberUnit(
446
+ power=pow(2, index + 3), simplified=value[0], traditional=value[1], big_s=value[0], big_t=value[1]
447
+ )
448
+ else:
449
+ raise ValueError("Counting type should be in {0} ({1} provided).".format(NUMBERING_TYPES, numbering_type))
450
+
451
+
452
+ class ChineseNumberDigit(ChineseChar):
453
+ """
454
+ 中文数字字符
455
+ """
456
+
457
+ def __init__(self, value, simplified, traditional, big_s, big_t, alt_s=None, alt_t=None):
458
+ super(ChineseNumberDigit, self).__init__(simplified, traditional)
459
+ self.value = value
460
+ self.big_s = big_s
461
+ self.big_t = big_t
462
+ self.alt_s = alt_s
463
+ self.alt_t = alt_t
464
+
465
+ def __str__(self):
466
+ return str(self.value)
467
+
468
+ @classmethod
469
+ def create(cls, i, v):
470
+ return ChineseNumberDigit(i, v[0], v[1], v[2], v[3])
471
+
472
+
473
+ class ChineseMath(ChineseChar):
474
+ """
475
+ 中文数位字符
476
+ """
477
+
478
+ def __init__(self, simplified, traditional, symbol, expression=None):
479
+ super(ChineseMath, self).__init__(simplified, traditional)
480
+ self.symbol = symbol
481
+ self.expression = expression
482
+ self.big_s = simplified
483
+ self.big_t = traditional
484
+
485
+
486
+ CC, CNU, CND, CM = ChineseChar, ChineseNumberUnit, ChineseNumberDigit, ChineseMath
487
+
488
+
489
+ class NumberSystem(object):
490
+ """
491
+ 中文数字系统
492
+ """
493
+
494
+ pass
495
+
496
+
497
+ class MathSymbol(object):
498
+ """
499
+ 用于中文数字系统的数学符号 (繁/简体), e.g.
500
+ positive = ['正', '正']
501
+ negative = ['负', '負']
502
+ point = ['点', '點']
503
+ """
504
+
505
+ def __init__(self, positive, negative, point):
506
+ self.positive = positive
507
+ self.negative = negative
508
+ self.point = point
509
+
510
+ def __iter__(self):
511
+ for v in self.__dict__.values():
512
+ yield v
513
+
514
+
515
+ # class OtherSymbol(object):
516
+ # """
517
+ # 其他符号
518
+ # """
519
+ #
520
+ # def __init__(self, sil):
521
+ # self.sil = sil
522
+ #
523
+ # def __iter__(self):
524
+ # for v in self.__dict__.values():
525
+ # yield v
526
+
527
+
528
+ # ================================================================================ #
529
+ # basic utils
530
+ # ================================================================================ #
531
+ def create_system(numbering_type=NUMBERING_TYPES[1]):
532
+ """
533
+ 根据数字系统类型返回创建相应的数字系统,默认为 mid
534
+ NUMBERING_TYPES = ['low', 'mid', 'high']: 中文数字系统类型
535
+ low: '兆' = '亿' * '十' = $10^{9}$, '京' = '兆' * '十', etc.
536
+ mid: '兆' = '亿' * '万' = $10^{12}$, '京' = '兆' * '万', etc.
537
+ high: '兆' = '亿' * '亿' = $10^{16}$, '京' = '兆' * '兆', etc.
538
+ 返回对应的数字系统
539
+ """
540
+
541
+ # chinese number units of '亿' and larger
542
+ all_larger_units = zip(LARGER_CHINESE_NUMERING_UNITS_SIMPLIFIED, LARGER_CHINESE_NUMERING_UNITS_TRADITIONAL)
543
+ larger_units = [CNU.create(i, v, numbering_type, False) for i, v in enumerate(all_larger_units)]
544
+ # chinese number units of '十, 百, 千, 万'
545
+ all_smaller_units = zip(SMALLER_CHINESE_NUMERING_UNITS_SIMPLIFIED, SMALLER_CHINESE_NUMERING_UNITS_TRADITIONAL)
546
+ smaller_units = [CNU.create(i, v, small_unit=True) for i, v in enumerate(all_smaller_units)]
547
+ # digis
548
+ chinese_digis = zip(CHINESE_DIGIS, CHINESE_DIGIS, BIG_CHINESE_DIGIS_SIMPLIFIED, BIG_CHINESE_DIGIS_TRADITIONAL)
549
+ digits = [CND.create(i, v) for i, v in enumerate(chinese_digis)]
550
+ digits[0].alt_s, digits[0].alt_t = ZERO_ALT, ZERO_ALT
551
+ digits[1].alt_s, digits[1].alt_t = ONE_ALT, ONE_ALT
552
+ digits[2].alt_s, digits[2].alt_t = TWO_ALTS[0], TWO_ALTS[1]
553
+
554
+ # symbols
555
+ positive_cn = CM(POSITIVE[0], POSITIVE[1], "+", lambda x: x)
556
+ negative_cn = CM(NEGATIVE[0], NEGATIVE[1], "-", lambda x: -x)
557
+ point_cn = CM(POINT[0], POINT[1], ".", lambda x, y: float(str(x) + "." + str(y)))
558
+ # sil_cn = CM(SIL[0], SIL[1], '-', lambda x, y: float(str(x) + '-' + str(y)))
559
+ system = NumberSystem()
560
+ system.units = smaller_units + larger_units
561
+ system.digits = digits
562
+ system.math = MathSymbol(positive_cn, negative_cn, point_cn)
563
+ # system.symbols = OtherSymbol(sil_cn)
564
+ return system
565
+
566
+
567
+ def chn2num(chinese_string, numbering_type=NUMBERING_TYPES[1]):
568
+ def get_symbol(char, system):
569
+ for u in system.units:
570
+ if char in [u.traditional, u.simplified, u.big_s, u.big_t]:
571
+ return u
572
+ for d in system.digits:
573
+ if char in [d.traditional, d.simplified, d.big_s, d.big_t, d.alt_s, d.alt_t]:
574
+ return d
575
+ for m in system.math:
576
+ if char in [m.traditional, m.simplified]:
577
+ return m
578
+
579
+ def string2symbols(chinese_string, system):
580
+ int_string, dec_string = chinese_string, ""
581
+ for p in [system.math.point.simplified, system.math.point.traditional]:
582
+ if p in chinese_string:
583
+ int_string, dec_string = chinese_string.split(p)
584
+ break
585
+ return [get_symbol(c, system) for c in int_string], [get_symbol(c, system) for c in dec_string]
586
+
587
+ def correct_symbols(integer_symbols, system):
588
+ """
589
+ 一百八 to 一百八十
590
+ 一亿一千三百万 to 一亿 一千万 三百万
591
+ """
592
+
593
+ if integer_symbols and isinstance(integer_symbols[0], CNU):
594
+ if integer_symbols[0].power == 1:
595
+ integer_symbols = [system.digits[1]] + integer_symbols
596
+
597
+ if len(integer_symbols) > 1:
598
+ if isinstance(integer_symbols[-1], CND) and isinstance(integer_symbols[-2], CNU):
599
+ integer_symbols.append(CNU(integer_symbols[-2].power - 1, None, None, None, None))
600
+
601
+ result = []
602
+ unit_count = 0
603
+ for s in integer_symbols:
604
+ if isinstance(s, CND):
605
+ result.append(s)
606
+ unit_count = 0
607
+ elif isinstance(s, CNU):
608
+ current_unit = CNU(s.power, None, None, None, None)
609
+ unit_count += 1
610
+
611
+ if unit_count == 1:
612
+ result.append(current_unit)
613
+ elif unit_count > 1:
614
+ for i in range(len(result)):
615
+ if isinstance(result[-i - 1], CNU) and result[-i - 1].power < current_unit.power:
616
+ result[-i - 1] = CNU(result[-i - 1].power + current_unit.power, None, None, None, None)
617
+ return result
618
+
619
+ def compute_value(integer_symbols):
620
+ """
621
+ Compute the value.
622
+ When current unit is larger than previous unit, current unit * all previous units will be used as all previous units.
623
+ e.g. '两千万' = 2000 * 10000 not 2000 + 10000
624
+ """
625
+ value = [0]
626
+ last_power = 0
627
+ for s in integer_symbols:
628
+ if isinstance(s, CND):
629
+ value[-1] = s.value
630
+ elif isinstance(s, CNU):
631
+ value[-1] *= pow(10, s.power)
632
+ if s.power > last_power:
633
+ value[:-1] = list(map(lambda v: v * pow(10, s.power), value[:-1]))
634
+ last_power = s.power
635
+ value.append(0)
636
+ return sum(value)
637
+
638
+ system = create_system(numbering_type)
639
+ int_part, dec_part = string2symbols(chinese_string, system)
640
+ int_part = correct_symbols(int_part, system)
641
+ int_str = str(compute_value(int_part))
642
+ dec_str = "".join([str(d.value) for d in dec_part])
643
+ if dec_part:
644
+ return "{0}.{1}".format(int_str, dec_str)
645
+ else:
646
+ return int_str
647
+
648
+
649
+ def num2chn(
650
+ number_string,
651
+ numbering_type=NUMBERING_TYPES[1],
652
+ big=False,
653
+ traditional=False,
654
+ alt_zero=False,
655
+ alt_one=False,
656
+ alt_two=True,
657
+ use_zeros=True,
658
+ use_units=True,
659
+ ):
660
+ def get_value(value_string, use_zeros=True):
661
+ striped_string = value_string.lstrip("0")
662
+
663
+ # record nothing if all zeros
664
+ if not striped_string:
665
+ return []
666
+
667
+ # record one digits
668
+ elif len(striped_string) == 1:
669
+ if use_zeros and len(value_string) != len(striped_string):
670
+ return [system.digits[0], system.digits[int(striped_string)]]
671
+ else:
672
+ return [system.digits[int(striped_string)]]
673
+
674
+ # recursively record multiple digits
675
+ else:
676
+ result_unit = next(u for u in reversed(system.units) if u.power < len(striped_string))
677
+ result_string = value_string[: -result_unit.power]
678
+ return get_value(result_string) + [result_unit] + get_value(striped_string[-result_unit.power :])
679
+
680
+ system = create_system(numbering_type)
681
+
682
+ int_dec = number_string.split(".")
683
+ if len(int_dec) == 1:
684
+ int_string = int_dec[0]
685
+ dec_string = ""
686
+ elif len(int_dec) == 2:
687
+ int_string = int_dec[0]
688
+ dec_string = int_dec[1]
689
+ else:
690
+ raise ValueError("invalid input num string with more than one dot: {}".format(number_string))
691
+
692
+ if use_units and len(int_string) > 1:
693
+ result_symbols = get_value(int_string)
694
+ else:
695
+ result_symbols = [system.digits[int(c)] for c in int_string]
696
+ dec_symbols = [system.digits[int(c)] for c in dec_string]
697
+ if dec_string:
698
+ result_symbols += [system.math.point] + dec_symbols
699
+
700
+ if alt_two:
701
+ liang = CND(2, system.digits[2].alt_s, system.digits[2].alt_t, system.digits[2].big_s, system.digits[2].big_t)
702
+ for i, v in enumerate(result_symbols):
703
+ if isinstance(v, CND) and v.value == 2:
704
+ next_symbol = result_symbols[i + 1] if i < len(result_symbols) - 1 else None
705
+ previous_symbol = result_symbols[i - 1] if i > 0 else None
706
+ if isinstance(next_symbol, CNU) and isinstance(previous_symbol, (CNU, type(None))):
707
+ if next_symbol.power != 1 and ((previous_symbol is None) or (previous_symbol.power != 1)):
708
+ result_symbols[i] = liang
709
+
710
+ # if big is True, '两' will not be used and `alt_two` has no impact on output
711
+ if big:
712
+ attr_name = "big_"
713
+ if traditional:
714
+ attr_name += "t"
715
+ else:
716
+ attr_name += "s"
717
+ else:
718
+ if traditional:
719
+ attr_name = "traditional"
720
+ else:
721
+ attr_name = "simplified"
722
+
723
+ result = "".join([getattr(s, attr_name) for s in result_symbols])
724
+
725
+ # if not use_zeros:
726
+ # result = result.strip(getattr(system.digits[0], attr_name))
727
+
728
+ if alt_zero:
729
+ result = result.replace(getattr(system.digits[0], attr_name), system.digits[0].alt_s)
730
+
731
+ if alt_one:
732
+ result = result.replace(getattr(system.digits[1], attr_name), system.digits[1].alt_s)
733
+
734
+ for i, p in enumerate(POINT):
735
+ if result.startswith(p):
736
+ return CHINESE_DIGIS[0] + result
737
+
738
+ # ^10, 11, .., 19
739
+ if (
740
+ len(result) >= 2
741
+ and result[1] in [SMALLER_CHINESE_NUMERING_UNITS_SIMPLIFIED[0], SMALLER_CHINESE_NUMERING_UNITS_TRADITIONAL[0]]
742
+ and result[0] in [CHINESE_DIGIS[1], BIG_CHINESE_DIGIS_SIMPLIFIED[1], BIG_CHINESE_DIGIS_TRADITIONAL[1]]
743
+ ):
744
+ result = result[1:]
745
+
746
+ return result
747
+
748
+
749
+ # ================================================================================ #
750
+ # different types of rewriters
751
+ # ================================================================================ #
752
+ class Cardinal:
753
+ """
754
+ CARDINAL类
755
+ """
756
+
757
+ def __init__(self, cardinal=None, chntext=None):
758
+ self.cardinal = cardinal
759
+ self.chntext = chntext
760
+
761
+ def chntext2cardinal(self):
762
+ return chn2num(self.chntext)
763
+
764
+ def cardinal2chntext(self):
765
+ return num2chn(self.cardinal)
766
+
767
+
768
+ class Digit:
769
+ """
770
+ DIGIT类
771
+ """
772
+
773
+ def __init__(self, digit=None, chntext=None):
774
+ self.digit = digit
775
+ self.chntext = chntext
776
+
777
+ # def chntext2digit(self):
778
+ # return chn2num(self.chntext)
779
+
780
+ def digit2chntext(self):
781
+ return num2chn(self.digit, alt_two=False, use_units=False)
782
+
783
+
784
+ class TelePhone:
785
+ """
786
+ TELEPHONE类
787
+ """
788
+
789
+ def __init__(self, telephone=None, raw_chntext=None, chntext=None):
790
+ self.telephone = telephone
791
+ self.raw_chntext = raw_chntext
792
+ self.chntext = chntext
793
+
794
+ # def chntext2telephone(self):
795
+ # sil_parts = self.raw_chntext.split('<SIL>')
796
+ # self.telephone = '-'.join([
797
+ # str(chn2num(p)) for p in sil_parts
798
+ # ])
799
+ # return self.telephone
800
+
801
+ def telephone2chntext(self, fixed=False):
802
+ if fixed:
803
+ sil_parts = self.telephone.split("-")
804
+ self.raw_chntext = "<SIL>".join([num2chn(part, alt_two=False, use_units=False) for part in sil_parts])
805
+ self.chntext = self.raw_chntext.replace("<SIL>", "")
806
+ else:
807
+ sp_parts = self.telephone.strip("+").split()
808
+ self.raw_chntext = "<SP>".join([num2chn(part, alt_two=False, use_units=False) for part in sp_parts])
809
+ self.chntext = self.raw_chntext.replace("<SP>", "")
810
+ return self.chntext
811
+
812
+
813
+ class Fraction:
814
+ """
815
+ FRACTION类
816
+ """
817
+
818
+ def __init__(self, fraction=None, chntext=None):
819
+ self.fraction = fraction
820
+ self.chntext = chntext
821
+
822
+ def chntext2fraction(self):
823
+ denominator, numerator = self.chntext.split("分之")
824
+ return chn2num(numerator) + "/" + chn2num(denominator)
825
+
826
+ def fraction2chntext(self):
827
+ numerator, denominator = self.fraction.split("/")
828
+ return num2chn(denominator) + "分之" + num2chn(numerator)
829
+
830
+
831
+ class Date:
832
+ """
833
+ DATE类
834
+ """
835
+
836
+ def __init__(self, date=None, chntext=None):
837
+ self.date = date
838
+ self.chntext = chntext
839
+
840
+ # def chntext2date(self):
841
+ # chntext = self.chntext
842
+ # try:
843
+ # year, other = chntext.strip().split('年', maxsplit=1)
844
+ # year = Digit(chntext=year).digit2chntext() + '年'
845
+ # except ValueError:
846
+ # other = chntext
847
+ # year = ''
848
+ # if other:
849
+ # try:
850
+ # month, day = other.strip().split('月', maxsplit=1)
851
+ # month = Cardinal(chntext=month).chntext2cardinal() + '月'
852
+ # except ValueError:
853
+ # day = chntext
854
+ # month = ''
855
+ # if day:
856
+ # day = Cardinal(chntext=day[:-1]).chntext2cardinal() + day[-1]
857
+ # else:
858
+ # month = ''
859
+ # day = ''
860
+ # date = year + month + day
861
+ # self.date = date
862
+ # return self.date
863
+
864
+ def date2chntext(self):
865
+ date = self.date
866
+ try:
867
+ year, other = date.strip().split("年", 1)
868
+ year = Digit(digit=year).digit2chntext() + "年"
869
+ except ValueError:
870
+ other = date
871
+ year = ""
872
+ if other:
873
+ try:
874
+ month, day = other.strip().split("月", 1)
875
+ month = Cardinal(cardinal=month).cardinal2chntext() + "月"
876
+ except ValueError:
877
+ day = date
878
+ month = ""
879
+ if day:
880
+ day = Cardinal(cardinal=day[:-1]).cardinal2chntext() + day[-1]
881
+ else:
882
+ month = ""
883
+ day = ""
884
+ chntext = year + month + day
885
+ self.chntext = chntext
886
+ return self.chntext
887
+
888
+
889
+ class Money:
890
+ """
891
+ MONEY类
892
+ """
893
+
894
+ def __init__(self, money=None, chntext=None):
895
+ self.money = money
896
+ self.chntext = chntext
897
+
898
+ # def chntext2money(self):
899
+ # return self.money
900
+
901
+ def money2chntext(self):
902
+ money = self.money
903
+ pattern = re.compile(r"(\d+(\.\d+)?)")
904
+ matchers = pattern.findall(money)
905
+ if matchers:
906
+ for matcher in matchers:
907
+ money = money.replace(matcher[0], Cardinal(cardinal=matcher[0]).cardinal2chntext())
908
+ self.chntext = money
909
+ return self.chntext
910
+
911
+
912
+ class Percentage:
913
+ """
914
+ PERCENTAGE类
915
+ """
916
+
917
+ def __init__(self, percentage=None, chntext=None):
918
+ self.percentage = percentage
919
+ self.chntext = chntext
920
+
921
+ def chntext2percentage(self):
922
+ return chn2num(self.chntext.strip().strip("百分之")) + "%"
923
+
924
+ def percentage2chntext(self):
925
+ return "百分之" + num2chn(self.percentage.strip().strip("%"))
926
+
927
+
928
+ def normalize_nsw(raw_text):
929
+ text = "^" + raw_text + "$"
930
+
931
+ # 规范化日期
932
+ pattern = re.compile(r"\D+((([089]\d|(19|20)\d{2})年)?(\d{1,2}月(\d{1,2}[日号])?)?)")
933
+ matchers = pattern.findall(text)
934
+ if matchers:
935
+ # print('date')
936
+ for matcher in matchers:
937
+ text = text.replace(matcher[0], Date(date=matcher[0]).date2chntext(), 1)
938
+
939
+ # 规范化金钱
940
+ pattern = re.compile(r"\D+((\d+(\.\d+)?)[多余几]?" + CURRENCY_UNITS + r"(\d" + CURRENCY_UNITS + r"?)?)")
941
+ matchers = pattern.findall(text)
942
+ if matchers:
943
+ # print('money')
944
+ for matcher in matchers:
945
+ text = text.replace(matcher[0], Money(money=matcher[0]).money2chntext(), 1)
946
+
947
+ # 规范化固话/手机号码
948
+ # 手机
949
+ # http://www.jihaoba.com/news/show/13680
950
+ # 移动:139、138、137、136、135、134、159、158、157、150、151、152、188、187、182、183、184、178、198
951
+ # 联通:130、131、132、156、155、186、185、176
952
+ # 电信:133、153、189、180、181、177
953
+ pattern = re.compile(r"\D((\+?86 ?)?1([38]\d|5[0-35-9]|7[678]|9[89])\d{8})\D")
954
+ matchers = pattern.findall(text)
955
+ if matchers:
956
+ # print('telephone')
957
+ for matcher in matchers:
958
+ text = text.replace(matcher[0], TelePhone(telephone=matcher[0]).telephone2chntext(), 1)
959
+ # 固话
960
+ pattern = re.compile(r"\D((0(10|2[1-3]|[3-9]\d{2})-?)?[1-9]\d{6,7})\D")
961
+ matchers = pattern.findall(text)
962
+ if matchers:
963
+ # print('fixed telephone')
964
+ for matcher in matchers:
965
+ text = text.replace(matcher[0], TelePhone(telephone=matcher[0]).telephone2chntext(fixed=True), 1)
966
+
967
+ # 规范化分数
968
+ pattern = re.compile(r"(\d+/\d+)")
969
+ matchers = pattern.findall(text)
970
+ if matchers:
971
+ # print('fraction')
972
+ for matcher in matchers:
973
+ text = text.replace(matcher, Fraction(fraction=matcher).fraction2chntext(), 1)
974
+
975
+ # 规范化百分数
976
+ text = text.replace("%", "%")
977
+ pattern = re.compile(r"(\d+(\.\d+)?%)")
978
+ matchers = pattern.findall(text)
979
+ if matchers:
980
+ # print('percentage')
981
+ for matcher in matchers:
982
+ text = text.replace(matcher[0], Percentage(percentage=matcher[0]).percentage2chntext(), 1)
983
+
984
+ # 规范化纯数+量词
985
+ pattern = re.compile(r"(\d+(\.\d+)?)[多余几]?" + COM_QUANTIFIERS)
986
+ matchers = pattern.findall(text)
987
+ if matchers:
988
+ # print('cardinal+quantifier')
989
+ for matcher in matchers:
990
+ text = text.replace(matcher[0], Cardinal(cardinal=matcher[0]).cardinal2chntext(), 1)
991
+
992
+ # 规范化数字编号
993
+ pattern = re.compile(r"(\d{4,32})")
994
+ matchers = pattern.findall(text)
995
+ if matchers:
996
+ # print('digit')
997
+ for matcher in matchers:
998
+ text = text.replace(matcher, Digit(digit=matcher).digit2chntext(), 1)
999
+
1000
+ # 规范化纯数
1001
+ pattern = re.compile(r"(\d+(\.\d+)?)")
1002
+ matchers = pattern.findall(text)
1003
+ if matchers:
1004
+ # print('cardinal')
1005
+ for matcher in matchers:
1006
+ text = text.replace(matcher[0], Cardinal(cardinal=matcher[0]).cardinal2chntext(), 1)
1007
+
1008
+ # restore P2P, O2O, B2C, B2B etc
1009
+ pattern = re.compile(r"(([a-zA-Z]+)二([a-zA-Z]+))")
1010
+ matchers = pattern.findall(text)
1011
+ if matchers:
1012
+ # print('particular')
1013
+ for matcher in matchers:
1014
+ text = text.replace(matcher[0], matcher[1] + "2" + matcher[2], 1)
1015
+
1016
+ return text.lstrip("^").rstrip("$")
1017
+
1018
+
1019
+ def remove_erhua(text):
1020
+ """
1021
+ 去除儿化音词中的儿:
1022
+ 他女儿在那边儿 -> 他女儿在那边
1023
+ """
1024
+
1025
+ new_str = ""
1026
+ while re.search("儿", text):
1027
+ a = re.search("儿", text).span()
1028
+ remove_er_flag = 0
1029
+
1030
+ if ER_WHITELIST_PATTERN.search(text):
1031
+ b = ER_WHITELIST_PATTERN.search(text).span()
1032
+ if b[0] <= a[0]:
1033
+ remove_er_flag = 1
1034
+
1035
+ if remove_er_flag == 0:
1036
+ new_str = new_str + text[0 : a[0]]
1037
+ text = text[a[1] :]
1038
+ else:
1039
+ new_str = new_str + text[0 : b[1]]
1040
+ text = text[b[1] :]
1041
+
1042
+ text = new_str + text
1043
+ return text
1044
+
1045
+
1046
+ def remove_space(text):
1047
+ tokens = text.split()
1048
+ new = []
1049
+ for k, t in enumerate(tokens):
1050
+ if k != 0:
1051
+ if IN_EN_CHARS.get(tokens[k - 1][-1]) and IN_EN_CHARS.get(t[0]):
1052
+ new.append(" ")
1053
+ new.append(t)
1054
+ return "".join(new)
1055
+
1056
+
1057
+ class TextNorm:
1058
+ def __init__(
1059
+ self,
1060
+ to_banjiao: bool = False,
1061
+ to_upper: bool = False,
1062
+ to_lower: bool = False,
1063
+ remove_fillers: bool = False,
1064
+ remove_erhua: bool = False,
1065
+ check_chars: bool = False,
1066
+ remove_space: bool = False,
1067
+ cc_mode: str = "",
1068
+ ):
1069
+ self.to_banjiao = to_banjiao
1070
+ self.to_upper = to_upper
1071
+ self.to_lower = to_lower
1072
+ self.remove_fillers = remove_fillers
1073
+ self.remove_erhua = remove_erhua
1074
+ self.check_chars = check_chars
1075
+ self.remove_space = remove_space
1076
+
1077
+ self.cc = None
1078
+ if cc_mode:
1079
+ from opencc import OpenCC # Open Chinese Convert: pip install opencc
1080
+
1081
+ self.cc = OpenCC(cc_mode)
1082
+
1083
+ def __call__(self, text):
1084
+ if self.cc:
1085
+ text = self.cc.convert(text)
1086
+
1087
+ if self.to_banjiao:
1088
+ text = text.translate(QJ2BJ_TRANSFORM)
1089
+
1090
+ if self.to_upper:
1091
+ text = text.upper()
1092
+
1093
+ if self.to_lower:
1094
+ text = text.lower()
1095
+
1096
+ if self.remove_fillers:
1097
+ for c in FILLER_CHARS:
1098
+ text = text.replace(c, "")
1099
+
1100
+ if self.remove_erhua:
1101
+ text = remove_erhua(text)
1102
+
1103
+ text = normalize_nsw(text)
1104
+
1105
+ text = text.translate(PUNCS_TRANSFORM)
1106
+
1107
+ if self.check_chars:
1108
+ for c in text:
1109
+ if not IN_VALID_CHARS.get(c):
1110
+ print(f"WARNING: illegal char {c} in: {text}", file=sys.stderr)
1111
+ return ""
1112
+
1113
+ if self.remove_space:
1114
+ text = remove_space(text)
1115
+
1116
+ return text
1117
+
1118
+
1119
+ if __name__ == "__main__":
1120
+ p = argparse.ArgumentParser()
1121
+
1122
+ # normalizer options
1123
+ p.add_argument("--to_banjiao", action="store_true", help="convert quanjiao chars to banjiao")
1124
+ p.add_argument("--to_upper", action="store_true", help="convert to upper case")
1125
+ p.add_argument("--to_lower", action="store_true", help="convert to lower case")
1126
+ p.add_argument("--remove_fillers", action="store_true", help='remove filler chars such as "呃, 啊"')
1127
+ p.add_argument("--remove_erhua", action="store_true", help='remove erhua chars such as "他女儿在那边儿 -> 他女儿在那边"')
1128
+ p.add_argument("--check_chars", action="store_true", help="skip sentences containing illegal chars")
1129
+ p.add_argument("--remove_space", action="store_true", help="remove whitespace")
1130
+ p.add_argument(
1131
+ "--cc_mode", choices=["", "t2s", "s2t"], default="", help="convert between traditional to simplified"
1132
+ )
1133
+
1134
+ # I/O options
1135
+ p.add_argument("--log_interval", type=int, default=10000, help="log interval in number of processed lines")
1136
+ p.add_argument("--has_key", action="store_true", help="will be deprecated, set --format ark instead")
1137
+ p.add_argument("--format", type=str, choices=["txt", "ark", "tsv"], default="txt", help="input format")
1138
+ p.add_argument("ifile", help="input filename, assume utf-8 encoding")
1139
+ p.add_argument("ofile", help="output filename")
1140
+
1141
+ args = p.parse_args()
1142
+
1143
+ if args.has_key:
1144
+ args.format = "ark"
1145
+
1146
+ normalizer = TextNorm(
1147
+ to_banjiao=args.to_banjiao,
1148
+ to_upper=args.to_upper,
1149
+ to_lower=args.to_lower,
1150
+ remove_fillers=args.remove_fillers,
1151
+ remove_erhua=args.remove_erhua,
1152
+ check_chars=args.check_chars,
1153
+ remove_space=args.remove_space,
1154
+ cc_mode=args.cc_mode,
1155
+ )
1156
+
1157
+ normalizer = TextNorm(
1158
+ to_banjiao=args.to_banjiao,
1159
+ to_upper=args.to_upper,
1160
+ to_lower=args.to_lower,
1161
+ remove_fillers=args.remove_fillers,
1162
+ remove_erhua=args.remove_erhua,
1163
+ check_chars=args.check_chars,
1164
+ remove_space=args.remove_space,
1165
+ cc_mode=args.cc_mode,
1166
+ )
1167
+
1168
+ ndone = 0
1169
+ with open(args.ifile, "r", encoding="utf-8") as istream, open(args.ofile, "w+", encoding="utf-8") as ostream:
1170
+ if args.format == "tsv":
1171
+ reader = csv.DictReader(istream, delimiter="\t")
1172
+ assert "TEXT" in reader.fieldnames
1173
+ print("\t".join(reader.fieldnames), file=ostream)
1174
+
1175
+ for item in reader:
1176
+ text = item["TEXT"]
1177
+
1178
+ if text:
1179
+ text = normalizer(text)
1180
+
1181
+ if text:
1182
+ item["TEXT"] = text
1183
+ print("\t".join([item[f] for f in reader.fieldnames]), file=ostream)
1184
+
1185
+ ndone += 1
1186
+ if ndone % args.log_interval == 0:
1187
+ print(f"text norm: {ndone} lines done.", file=sys.stderr, flush=True)
1188
+ else:
1189
+ for l in istream:
1190
+ key, text = "", ""
1191
+ if args.format == "ark": # KALDI archive, line format: "key text"
1192
+ cols = l.strip().split(maxsplit=1)
1193
+ key, text = cols[0], cols[1] if len(cols) == 2 else ""
1194
+ else:
1195
+ text = l.strip()
1196
+
1197
+ if text:
1198
+ text = normalizer(text)
1199
+
1200
+ if text:
1201
+ if args.format == "ark":
1202
+ print(key + "\t" + text, file=ostream)
1203
+ else:
1204
+ print(text, file=ostream)
1205
+
1206
+ ndone += 1
1207
+ if ndone % args.log_interval == 0:
1208
+ print(f"text norm: {ndone} lines done.", file=sys.stderr, flush=True)
1209
+ print(f"text norm: {ndone} lines done in total.", file=sys.stderr, flush=True)
music_dcae/__init__.py ADDED
File without changes
music_dcae/music_dcae_pipeline.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ from diffusers import AutoencoderDC
4
+ import torchaudio
5
+ import torchvision.transforms as transforms
6
+ from diffusers.models.modeling_utils import ModelMixin
7
+ from diffusers.loaders import FromOriginalModelMixin
8
+ from diffusers.configuration_utils import ConfigMixin, register_to_config
9
+
10
+
11
+ try:
12
+ from .music_vocoder import ADaMoSHiFiGANV1
13
+ except ImportError:
14
+ from music_vocoder import ADaMoSHiFiGANV1
15
+
16
+
17
+ root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
18
+ DEFAULT_PRETRAINED_PATH = os.path.join(root_dir, "checkpoints", "music_dcae_f8c8")
19
+ VOCODER_PRETRAINED_PATH = os.path.join(root_dir, "checkpoints", "music_vocoder")
20
+
21
+
22
+ class MusicDCAE(ModelMixin, ConfigMixin, FromOriginalModelMixin):
23
+ @register_to_config
24
+ def __init__(self, source_sample_rate=None, dcae_checkpoint_path=DEFAULT_PRETRAINED_PATH, vocoder_checkpoint_path=VOCODER_PRETRAINED_PATH):
25
+ super(MusicDCAE, self).__init__()
26
+
27
+ self.dcae = AutoencoderDC.from_pretrained(dcae_checkpoint_path)
28
+ self.vocoder = ADaMoSHiFiGANV1.from_pretrained(vocoder_checkpoint_path)
29
+
30
+ if source_sample_rate is None:
31
+ source_sample_rate = 48000
32
+
33
+ self.resampler = torchaudio.transforms.Resample(source_sample_rate, 44100)
34
+
35
+ self.transform = transforms.Compose([
36
+ transforms.Normalize(0.5, 0.5),
37
+ ])
38
+ self.min_mel_value = -11.0
39
+ self.max_mel_value = 3.0
40
+ self.audio_chunk_size = int(round((1024 * 512 / 44100 * 48000)))
41
+ self.mel_chunk_size = 1024
42
+ self.time_dimention_multiple = 8
43
+ self.latent_chunk_size = self.mel_chunk_size // self.time_dimention_multiple
44
+ self.scale_factor = 0.1786
45
+ self.shift_factor = -1.9091
46
+
47
+ def load_audio(self, audio_path):
48
+ audio, sr = torchaudio.load(audio_path)
49
+ return audio, sr
50
+
51
+ def forward_mel(self, audios):
52
+ mels = []
53
+ for i in range(len(audios)):
54
+ image = self.vocoder.mel_transform(audios[i])
55
+ mels.append(image)
56
+ mels = torch.stack(mels)
57
+ return mels
58
+
59
+ @torch.no_grad()
60
+ def encode(self, audios, audio_lengths=None, sr=None):
61
+ if audio_lengths is None:
62
+ audio_lengths = torch.tensor([audios.shape[2]] * audios.shape[0])
63
+ audio_lengths = audio_lengths.to(audios.device)
64
+
65
+ # audios: N x 2 x T, 48kHz
66
+ device = audios.device
67
+ dtype = audios.dtype
68
+
69
+ if sr is None:
70
+ sr = 48000
71
+ resampler = self.resampler
72
+ else:
73
+ resampler = torchaudio.transforms.Resample(sr, 44100).to(device).to(dtype)
74
+
75
+ audio = resampler(audios)
76
+
77
+ max_audio_len = audio.shape[-1]
78
+ if max_audio_len % (8 * 512) != 0:
79
+ audio = torch.nn.functional.pad(audio, (0, 8 * 512 - max_audio_len % (8 * 512)))
80
+
81
+ mels = self.forward_mel(audio)
82
+ mels = (mels - self.min_mel_value) / (self.max_mel_value - self.min_mel_value)
83
+ mels = self.transform(mels)
84
+ latents = []
85
+ for mel in mels:
86
+ latent = self.dcae.encoder(mel.unsqueeze(0))
87
+ latents.append(latent)
88
+ latents = torch.cat(latents, dim=0)
89
+ latent_lengths = (audio_lengths / sr * 44100 / 512 / self.time_dimention_multiple).long()
90
+ latents = (latents - self.shift_factor) * self.scale_factor
91
+ return latents, latent_lengths
92
+
93
+ @torch.no_grad()
94
+ def decode(self, latents, audio_lengths=None, sr=None):
95
+ latents = latents / self.scale_factor + self.shift_factor
96
+
97
+ pred_wavs = []
98
+
99
+ for latent in latents:
100
+ mels = self.dcae.decoder(latent.unsqueeze(0))
101
+ mels = mels * 0.5 + 0.5
102
+ mels = mels * (self.max_mel_value - self.min_mel_value) + self.min_mel_value
103
+ wav = self.vocoder.decode(mels[0]).squeeze(1)
104
+
105
+ if sr is not None:
106
+ resampler = torchaudio.transforms.Resample(44100, sr).to(latents.device).to(latents.dtype)
107
+ wav = resampler(wav)
108
+ else:
109
+ sr = 44100
110
+ pred_wavs.append(wav)
111
+
112
+ if audio_lengths is not None:
113
+ pred_wavs = [wav[:, :length].cpu() for wav, length in zip(pred_wavs, audio_lengths)]
114
+ return sr, pred_wavs
115
+
116
+ def forward(self, audios, audio_lengths=None, sr=None):
117
+ latents, latent_lengths = self.encode(audios=audios, audio_lengths=audio_lengths, sr=sr)
118
+ sr, pred_wavs = self.decode(latents=latents, audio_lengths=audio_lengths, sr=sr)
119
+ return sr, pred_wavs, latents, latent_lengths
120
+
121
+
122
+ if __name__ == "__main__":
123
+
124
+ audio, sr = torchaudio.load("test.wav")
125
+ audio_lengths = torch.tensor([audio.shape[1]])
126
+ audios = audio.unsqueeze(0)
127
+
128
+ # test encode only
129
+ model = MusicDCAE()
130
+ # latents, latent_lengths = model.encode(audios, audio_lengths)
131
+ # print("latents shape: ", latents.shape)
132
+ # print("latent_lengths: ", latent_lengths)
133
+
134
+ # test encode and decode
135
+ sr, pred_wavs, latents, latent_lengths = model(audios, audio_lengths, sr)
136
+ print("reconstructed wavs: ", pred_wavs[0].shape)
137
+ print("latents shape: ", latents.shape)
138
+ print("latent_lengths: ", latent_lengths)
139
+ print("sr: ", sr)
140
+ torchaudio.save("test_reconstructed.flac", pred_wavs[0], sr)
141
+ print("test_reconstructed.flac")
music_dcae/music_log_mel.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from torch import Tensor
4
+ from torchaudio.transforms import MelScale
5
+
6
+
7
+ class LinearSpectrogram(nn.Module):
8
+ def __init__(
9
+ self,
10
+ n_fft=2048,
11
+ win_length=2048,
12
+ hop_length=512,
13
+ center=False,
14
+ mode="pow2_sqrt",
15
+ ):
16
+ super().__init__()
17
+
18
+ self.n_fft = n_fft
19
+ self.win_length = win_length
20
+ self.hop_length = hop_length
21
+ self.center = center
22
+ self.mode = mode
23
+
24
+ self.register_buffer("window", torch.hann_window(win_length))
25
+
26
+ def forward(self, y: Tensor) -> Tensor:
27
+ if y.ndim == 3:
28
+ y = y.squeeze(1)
29
+
30
+ y = torch.nn.functional.pad(
31
+ y.unsqueeze(1),
32
+ (
33
+ (self.win_length - self.hop_length) // 2,
34
+ (self.win_length - self.hop_length + 1) // 2,
35
+ ),
36
+ mode="reflect",
37
+ ).squeeze(1)
38
+ dtype = y.dtype
39
+ spec = torch.stft(
40
+ y.float(),
41
+ self.n_fft,
42
+ hop_length=self.hop_length,
43
+ win_length=self.win_length,
44
+ window=self.window,
45
+ center=self.center,
46
+ pad_mode="reflect",
47
+ normalized=False,
48
+ onesided=True,
49
+ return_complex=True,
50
+ )
51
+ spec = torch.view_as_real(spec)
52
+
53
+ if self.mode == "pow2_sqrt":
54
+ spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)
55
+ spec = spec.to(dtype)
56
+ return spec
57
+
58
+
59
+ class LogMelSpectrogram(nn.Module):
60
+ def __init__(
61
+ self,
62
+ sample_rate=44100,
63
+ n_fft=2048,
64
+ win_length=2048,
65
+ hop_length=512,
66
+ n_mels=128,
67
+ center=False,
68
+ f_min=0.0,
69
+ f_max=None,
70
+ ):
71
+ super().__init__()
72
+
73
+ self.sample_rate = sample_rate
74
+ self.n_fft = n_fft
75
+ self.win_length = win_length
76
+ self.hop_length = hop_length
77
+ self.center = center
78
+ self.n_mels = n_mels
79
+ self.f_min = f_min
80
+ self.f_max = f_max or sample_rate // 2
81
+
82
+ self.spectrogram = LinearSpectrogram(n_fft, win_length, hop_length, center)
83
+ self.mel_scale = MelScale(
84
+ self.n_mels,
85
+ self.sample_rate,
86
+ self.f_min,
87
+ self.f_max,
88
+ self.n_fft // 2 + 1,
89
+ "slaney",
90
+ "slaney",
91
+ )
92
+
93
+ def compress(self, x: Tensor) -> Tensor:
94
+ return torch.log(torch.clamp(x, min=1e-5))
95
+
96
+ def decompress(self, x: Tensor) -> Tensor:
97
+ return torch.exp(x)
98
+
99
+ def forward(self, x: Tensor, return_linear: bool = False) -> Tensor:
100
+ linear = self.spectrogram(x)
101
+ x = self.mel_scale(linear)
102
+ x = self.compress(x)
103
+ # print(x.shape)
104
+ if return_linear:
105
+ return x, self.compress(linear)
106
+
107
+ return x
music_dcae/music_vocoder.py ADDED
@@ -0,0 +1,576 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import librosa
2
+ import torch
3
+ from torch import nn
4
+
5
+ from functools import partial
6
+ from math import prod
7
+ from typing import Callable, Tuple, List
8
+
9
+ import numpy as np
10
+ import torch.nn.functional as F
11
+ from torch.nn import Conv1d
12
+ from torch.nn.utils import weight_norm
13
+ from torch.nn.utils.parametrize import remove_parametrizations as remove_weight_norm
14
+ from diffusers.models.modeling_utils import ModelMixin
15
+ from diffusers.loaders import FromOriginalModelMixin
16
+ from diffusers.configuration_utils import ConfigMixin, register_to_config
17
+
18
+
19
+ try:
20
+ from music_log_mel import LogMelSpectrogram
21
+ except ImportError:
22
+ from .music_log_mel import LogMelSpectrogram
23
+
24
+
25
+ def drop_path(
26
+ x, drop_prob: float = 0.0, training: bool = False, scale_by_keep: bool = True
27
+ ):
28
+ """Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
29
+
30
+ This is the same as the DropConnect impl I created for EfficientNet, etc networks, however,
31
+ the original name is misleading as 'Drop Connect' is a different form of dropout in a separate paper...
32
+ See discussion: https://github.com/tensorflow/tpu/issues/494#issuecomment-532968956 ... I've opted for
33
+ changing the layer and argument names to 'drop path' rather than mix DropConnect as a layer name and use
34
+ 'survival rate' as the argument.
35
+
36
+ """ # noqa: E501
37
+
38
+ if drop_prob == 0.0 or not training:
39
+ return x
40
+ keep_prob = 1 - drop_prob
41
+ shape = (x.shape[0],) + (1,) * (
42
+ x.ndim - 1
43
+ ) # work with diff dim tensors, not just 2D ConvNets
44
+ random_tensor = x.new_empty(shape).bernoulli_(keep_prob)
45
+ if keep_prob > 0.0 and scale_by_keep:
46
+ random_tensor.div_(keep_prob)
47
+ return x * random_tensor
48
+
49
+
50
+ class DropPath(nn.Module):
51
+ """Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).""" # noqa: E501
52
+
53
+ def __init__(self, drop_prob: float = 0.0, scale_by_keep: bool = True):
54
+ super(DropPath, self).__init__()
55
+ self.drop_prob = drop_prob
56
+ self.scale_by_keep = scale_by_keep
57
+
58
+ def forward(self, x):
59
+ return drop_path(x, self.drop_prob, self.training, self.scale_by_keep)
60
+
61
+ def extra_repr(self):
62
+ return f"drop_prob={round(self.drop_prob,3):0.3f}"
63
+
64
+
65
+ class LayerNorm(nn.Module):
66
+ r"""LayerNorm that supports two data formats: channels_last (default) or channels_first.
67
+ The ordering of the dimensions in the inputs. channels_last corresponds to inputs with
68
+ shape (batch_size, height, width, channels) while channels_first corresponds to inputs
69
+ with shape (batch_size, channels, height, width).
70
+ """ # noqa: E501
71
+
72
+ def __init__(self, normalized_shape, eps=1e-6, data_format="channels_last"):
73
+ super().__init__()
74
+ self.weight = nn.Parameter(torch.ones(normalized_shape))
75
+ self.bias = nn.Parameter(torch.zeros(normalized_shape))
76
+ self.eps = eps
77
+ self.data_format = data_format
78
+ if self.data_format not in ["channels_last", "channels_first"]:
79
+ raise NotImplementedError
80
+ self.normalized_shape = (normalized_shape,)
81
+
82
+ def forward(self, x):
83
+ if self.data_format == "channels_last":
84
+ return F.layer_norm(
85
+ x, self.normalized_shape, self.weight, self.bias, self.eps
86
+ )
87
+ elif self.data_format == "channels_first":
88
+ u = x.mean(1, keepdim=True)
89
+ s = (x - u).pow(2).mean(1, keepdim=True)
90
+ x = (x - u) / torch.sqrt(s + self.eps)
91
+ x = self.weight[:, None] * x + self.bias[:, None]
92
+ return x
93
+
94
+
95
+ class ConvNeXtBlock(nn.Module):
96
+ r"""ConvNeXt Block. There are two equivalent implementations:
97
+ (1) DwConv -> LayerNorm (channels_first) -> 1x1 Conv -> GELU -> 1x1 Conv; all in (N, C, H, W)
98
+ (2) DwConv -> Permute to (N, H, W, C); LayerNorm (channels_last) -> Linear -> GELU -> Linear; Permute back
99
+ We use (2) as we find it slightly faster in PyTorch
100
+
101
+ Args:
102
+ dim (int): Number of input channels.
103
+ drop_path (float): Stochastic depth rate. Default: 0.0
104
+ layer_scale_init_value (float): Init value for Layer Scale. Default: 1e-6.
105
+ mlp_ratio (float): Ratio of mlp hidden dim to embedding dim. Default: 4.0.
106
+ kernel_size (int): Kernel size for depthwise conv. Default: 7.
107
+ dilation (int): Dilation for depthwise conv. Default: 1.
108
+ """ # noqa: E501
109
+
110
+ def __init__(
111
+ self,
112
+ dim: int,
113
+ drop_path: float = 0.0,
114
+ layer_scale_init_value: float = 1e-6,
115
+ mlp_ratio: float = 4.0,
116
+ kernel_size: int = 7,
117
+ dilation: int = 1,
118
+ ):
119
+ super().__init__()
120
+
121
+ self.dwconv = nn.Conv1d(
122
+ dim,
123
+ dim,
124
+ kernel_size=kernel_size,
125
+ padding=int(dilation * (kernel_size - 1) / 2),
126
+ groups=dim,
127
+ ) # depthwise conv
128
+ self.norm = LayerNorm(dim, eps=1e-6)
129
+ self.pwconv1 = nn.Linear(
130
+ dim, int(mlp_ratio * dim)
131
+ ) # pointwise/1x1 convs, implemented with linear layers
132
+ self.act = nn.GELU()
133
+ self.pwconv2 = nn.Linear(int(mlp_ratio * dim), dim)
134
+ self.gamma = (
135
+ nn.Parameter(layer_scale_init_value *
136
+ torch.ones((dim)), requires_grad=True)
137
+ if layer_scale_init_value > 0
138
+ else None
139
+ )
140
+ self.drop_path = DropPath(
141
+ drop_path) if drop_path > 0.0 else nn.Identity()
142
+
143
+ def forward(self, x, apply_residual: bool = True):
144
+ input = x
145
+
146
+ x = self.dwconv(x)
147
+ x = x.permute(0, 2, 1) # (N, C, L) -> (N, L, C)
148
+ x = self.norm(x)
149
+ x = self.pwconv1(x)
150
+ x = self.act(x)
151
+ x = self.pwconv2(x)
152
+
153
+ if self.gamma is not None:
154
+ x = self.gamma * x
155
+
156
+ x = x.permute(0, 2, 1) # (N, L, C) -> (N, C, L)
157
+ x = self.drop_path(x)
158
+
159
+ if apply_residual:
160
+ x = input + x
161
+
162
+ return x
163
+
164
+
165
+ class ParallelConvNeXtBlock(nn.Module):
166
+ def __init__(self, kernel_sizes: List[int], *args, **kwargs):
167
+ super().__init__()
168
+ self.blocks = nn.ModuleList(
169
+ [
170
+ ConvNeXtBlock(kernel_size=kernel_size, *args, **kwargs)
171
+ for kernel_size in kernel_sizes
172
+ ]
173
+ )
174
+
175
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
176
+ return torch.stack(
177
+ [block(x, apply_residual=False) for block in self.blocks] + [x],
178
+ dim=1,
179
+ ).sum(dim=1)
180
+
181
+
182
+ class ConvNeXtEncoder(nn.Module):
183
+ def __init__(
184
+ self,
185
+ input_channels=3,
186
+ depths=[3, 3, 9, 3],
187
+ dims=[96, 192, 384, 768],
188
+ drop_path_rate=0.0,
189
+ layer_scale_init_value=1e-6,
190
+ kernel_sizes: Tuple[int] = (7,),
191
+ ):
192
+ super().__init__()
193
+ assert len(depths) == len(dims)
194
+
195
+ self.channel_layers = nn.ModuleList()
196
+ stem = nn.Sequential(
197
+ nn.Conv1d(
198
+ input_channels,
199
+ dims[0],
200
+ kernel_size=7,
201
+ padding=3,
202
+ padding_mode="replicate",
203
+ ),
204
+ LayerNorm(dims[0], eps=1e-6, data_format="channels_first"),
205
+ )
206
+ self.channel_layers.append(stem)
207
+
208
+ for i in range(len(depths) - 1):
209
+ mid_layer = nn.Sequential(
210
+ LayerNorm(dims[i], eps=1e-6, data_format="channels_first"),
211
+ nn.Conv1d(dims[i], dims[i + 1], kernel_size=1),
212
+ )
213
+ self.channel_layers.append(mid_layer)
214
+
215
+ block_fn = (
216
+ partial(ConvNeXtBlock, kernel_size=kernel_sizes[0])
217
+ if len(kernel_sizes) == 1
218
+ else partial(ParallelConvNeXtBlock, kernel_sizes=kernel_sizes)
219
+ )
220
+
221
+ self.stages = nn.ModuleList()
222
+ drop_path_rates = [
223
+ x.item() for x in torch.linspace(0, drop_path_rate, sum(depths))
224
+ ]
225
+
226
+ cur = 0
227
+ for i in range(len(depths)):
228
+ stage = nn.Sequential(
229
+ *[
230
+ block_fn(
231
+ dim=dims[i],
232
+ drop_path=drop_path_rates[cur + j],
233
+ layer_scale_init_value=layer_scale_init_value,
234
+ )
235
+ for j in range(depths[i])
236
+ ]
237
+ )
238
+ self.stages.append(stage)
239
+ cur += depths[i]
240
+
241
+ self.norm = LayerNorm(dims[-1], eps=1e-6, data_format="channels_first")
242
+ self.apply(self._init_weights)
243
+
244
+ def _init_weights(self, m):
245
+ if isinstance(m, (nn.Conv1d, nn.Linear)):
246
+ nn.init.trunc_normal_(m.weight, std=0.02)
247
+ nn.init.constant_(m.bias, 0)
248
+
249
+ def forward(
250
+ self,
251
+ x: torch.Tensor,
252
+ ) -> torch.Tensor:
253
+ for channel_layer, stage in zip(self.channel_layers, self.stages):
254
+ x = channel_layer(x)
255
+ x = stage(x)
256
+
257
+ return self.norm(x)
258
+
259
+
260
+ def init_weights(m, mean=0.0, std=0.01):
261
+ classname = m.__class__.__name__
262
+ if classname.find("Conv") != -1:
263
+ m.weight.data.normal_(mean, std)
264
+
265
+
266
+ def get_padding(kernel_size, dilation=1):
267
+ return (kernel_size * dilation - dilation) // 2
268
+
269
+
270
+ class ResBlock1(torch.nn.Module):
271
+ def __init__(self, channels, kernel_size=3, dilation=(1, 3, 5)):
272
+ super().__init__()
273
+
274
+ self.convs1 = nn.ModuleList(
275
+ [
276
+ weight_norm(
277
+ Conv1d(
278
+ channels,
279
+ channels,
280
+ kernel_size,
281
+ 1,
282
+ dilation=dilation[0],
283
+ padding=get_padding(kernel_size, dilation[0]),
284
+ )
285
+ ),
286
+ weight_norm(
287
+ Conv1d(
288
+ channels,
289
+ channels,
290
+ kernel_size,
291
+ 1,
292
+ dilation=dilation[1],
293
+ padding=get_padding(kernel_size, dilation[1]),
294
+ )
295
+ ),
296
+ weight_norm(
297
+ Conv1d(
298
+ channels,
299
+ channels,
300
+ kernel_size,
301
+ 1,
302
+ dilation=dilation[2],
303
+ padding=get_padding(kernel_size, dilation[2]),
304
+ )
305
+ ),
306
+ ]
307
+ )
308
+ self.convs1.apply(init_weights)
309
+
310
+ self.convs2 = nn.ModuleList(
311
+ [
312
+ weight_norm(
313
+ Conv1d(
314
+ channels,
315
+ channels,
316
+ kernel_size,
317
+ 1,
318
+ dilation=1,
319
+ padding=get_padding(kernel_size, 1),
320
+ )
321
+ ),
322
+ weight_norm(
323
+ Conv1d(
324
+ channels,
325
+ channels,
326
+ kernel_size,
327
+ 1,
328
+ dilation=1,
329
+ padding=get_padding(kernel_size, 1),
330
+ )
331
+ ),
332
+ weight_norm(
333
+ Conv1d(
334
+ channels,
335
+ channels,
336
+ kernel_size,
337
+ 1,
338
+ dilation=1,
339
+ padding=get_padding(kernel_size, 1),
340
+ )
341
+ ),
342
+ ]
343
+ )
344
+ self.convs2.apply(init_weights)
345
+
346
+ def forward(self, x):
347
+ for c1, c2 in zip(self.convs1, self.convs2):
348
+ xt = F.silu(x)
349
+ xt = c1(xt)
350
+ xt = F.silu(xt)
351
+ xt = c2(xt)
352
+ x = xt + x
353
+ return x
354
+
355
+ def remove_weight_norm(self):
356
+ for conv in self.convs1:
357
+ remove_weight_norm(conv)
358
+ for conv in self.convs2:
359
+ remove_weight_norm(conv)
360
+
361
+
362
+ class HiFiGANGenerator(nn.Module):
363
+ def __init__(
364
+ self,
365
+ *,
366
+ hop_length: int = 512,
367
+ upsample_rates: Tuple[int] = (8, 8, 2, 2, 2),
368
+ upsample_kernel_sizes: Tuple[int] = (16, 16, 8, 2, 2),
369
+ resblock_kernel_sizes: Tuple[int] = (3, 7, 11),
370
+ resblock_dilation_sizes: Tuple[Tuple[int]] = (
371
+ (1, 3, 5), (1, 3, 5), (1, 3, 5)),
372
+ num_mels: int = 128,
373
+ upsample_initial_channel: int = 512,
374
+ use_template: bool = True,
375
+ pre_conv_kernel_size: int = 7,
376
+ post_conv_kernel_size: int = 7,
377
+ post_activation: Callable = partial(nn.SiLU, inplace=True),
378
+ ):
379
+ super().__init__()
380
+
381
+ assert (
382
+ prod(upsample_rates) == hop_length
383
+ ), f"hop_length must be {prod(upsample_rates)}"
384
+
385
+ self.conv_pre = weight_norm(
386
+ nn.Conv1d(
387
+ num_mels,
388
+ upsample_initial_channel,
389
+ pre_conv_kernel_size,
390
+ 1,
391
+ padding=get_padding(pre_conv_kernel_size),
392
+ )
393
+ )
394
+
395
+ self.num_upsamples = len(upsample_rates)
396
+ self.num_kernels = len(resblock_kernel_sizes)
397
+
398
+ self.noise_convs = nn.ModuleList()
399
+ self.use_template = use_template
400
+ self.ups = nn.ModuleList()
401
+
402
+ for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)):
403
+ c_cur = upsample_initial_channel // (2 ** (i + 1))
404
+ self.ups.append(
405
+ weight_norm(
406
+ nn.ConvTranspose1d(
407
+ upsample_initial_channel // (2**i),
408
+ upsample_initial_channel // (2 ** (i + 1)),
409
+ k,
410
+ u,
411
+ padding=(k - u) // 2,
412
+ )
413
+ )
414
+ )
415
+
416
+ if not use_template:
417
+ continue
418
+
419
+ if i + 1 < len(upsample_rates):
420
+ stride_f0 = np.prod(upsample_rates[i + 1:])
421
+ self.noise_convs.append(
422
+ Conv1d(
423
+ 1,
424
+ c_cur,
425
+ kernel_size=stride_f0 * 2,
426
+ stride=stride_f0,
427
+ padding=stride_f0 // 2,
428
+ )
429
+ )
430
+ else:
431
+ self.noise_convs.append(Conv1d(1, c_cur, kernel_size=1))
432
+
433
+ self.resblocks = nn.ModuleList()
434
+ for i in range(len(self.ups)):
435
+ ch = upsample_initial_channel // (2 ** (i + 1))
436
+ for k, d in zip(resblock_kernel_sizes, resblock_dilation_sizes):
437
+ self.resblocks.append(ResBlock1(ch, k, d))
438
+
439
+ self.activation_post = post_activation()
440
+ self.conv_post = weight_norm(
441
+ nn.Conv1d(
442
+ ch,
443
+ 1,
444
+ post_conv_kernel_size,
445
+ 1,
446
+ padding=get_padding(post_conv_kernel_size),
447
+ )
448
+ )
449
+ self.ups.apply(init_weights)
450
+ self.conv_post.apply(init_weights)
451
+
452
+ def forward(self, x, template=None):
453
+ x = self.conv_pre(x)
454
+
455
+ for i in range(self.num_upsamples):
456
+ x = F.silu(x, inplace=True)
457
+ x = self.ups[i](x)
458
+
459
+ if self.use_template:
460
+ x = x + self.noise_convs[i](template)
461
+
462
+ xs = None
463
+
464
+ for j in range(self.num_kernels):
465
+ if xs is None:
466
+ xs = self.resblocks[i * self.num_kernels + j](x)
467
+ else:
468
+ xs += self.resblocks[i * self.num_kernels + j](x)
469
+
470
+ x = xs / self.num_kernels
471
+
472
+ x = self.activation_post(x)
473
+ x = self.conv_post(x)
474
+ x = torch.tanh(x)
475
+
476
+ return x
477
+
478
+ def remove_weight_norm(self):
479
+ for up in self.ups:
480
+ remove_weight_norm(up)
481
+ for block in self.resblocks:
482
+ block.remove_weight_norm()
483
+ remove_weight_norm(self.conv_pre)
484
+ remove_weight_norm(self.conv_post)
485
+
486
+
487
+ class ADaMoSHiFiGANV1(ModelMixin, ConfigMixin, FromOriginalModelMixin):
488
+
489
+ @register_to_config
490
+ def __init__(
491
+ self,
492
+ input_channels: int = 128,
493
+ depths: List[int] = [3, 3, 9, 3],
494
+ dims: List[int] = [128, 256, 384, 512],
495
+ drop_path_rate: float = 0.0,
496
+ kernel_sizes: Tuple[int] = (7,),
497
+ upsample_rates: Tuple[int] = (4, 4, 2, 2, 2, 2, 2),
498
+ upsample_kernel_sizes: Tuple[int] = (8, 8, 4, 4, 4, 4, 4),
499
+ resblock_kernel_sizes: Tuple[int] = (3, 7, 11, 13),
500
+ resblock_dilation_sizes: Tuple[Tuple[int]] = (
501
+ (1, 3, 5), (1, 3, 5), (1, 3, 5), (1, 3, 5)),
502
+ num_mels: int = 512,
503
+ upsample_initial_channel: int = 1024,
504
+ use_template: bool = False,
505
+ pre_conv_kernel_size: int = 13,
506
+ post_conv_kernel_size: int = 13,
507
+ sampling_rate: int = 44100,
508
+ n_fft: int = 2048,
509
+ win_length: int = 2048,
510
+ hop_length: int = 512,
511
+ f_min: int = 40,
512
+ f_max: int = 16000,
513
+ n_mels: int = 128,
514
+ ):
515
+ super().__init__()
516
+
517
+ self.backbone = ConvNeXtEncoder(
518
+ input_channels=input_channels,
519
+ depths=depths,
520
+ dims=dims,
521
+ drop_path_rate=drop_path_rate,
522
+ kernel_sizes=kernel_sizes,
523
+ )
524
+
525
+ self.head = HiFiGANGenerator(
526
+ hop_length=hop_length,
527
+ upsample_rates=upsample_rates,
528
+ upsample_kernel_sizes=upsample_kernel_sizes,
529
+ resblock_kernel_sizes=resblock_kernel_sizes,
530
+ resblock_dilation_sizes=resblock_dilation_sizes,
531
+ num_mels=num_mels,
532
+ upsample_initial_channel=upsample_initial_channel,
533
+ use_template=use_template,
534
+ pre_conv_kernel_size=pre_conv_kernel_size,
535
+ post_conv_kernel_size=post_conv_kernel_size,
536
+ )
537
+ self.sampling_rate = sampling_rate
538
+ self.mel_transform = LogMelSpectrogram(
539
+ sample_rate=sampling_rate,
540
+ n_fft=n_fft,
541
+ win_length=win_length,
542
+ hop_length=hop_length,
543
+ f_min=f_min,
544
+ f_max=f_max,
545
+ n_mels=n_mels,
546
+ )
547
+ self.eval()
548
+
549
+ @torch.no_grad()
550
+ def decode(self, mel):
551
+ y = self.backbone(mel)
552
+ y = self.head(y)
553
+ return y
554
+
555
+ @torch.no_grad()
556
+ def encode(self, x):
557
+ return self.mel_transform(x)
558
+
559
+ def forward(self, mel):
560
+ y = self.backbone(mel)
561
+ y = self.head(y)
562
+ return y
563
+
564
+
565
+ if __name__ == "__main__":
566
+ import soundfile as sf
567
+
568
+ x = "test_audio.flac"
569
+ model = ADaMoSHiFiGANV1.from_pretrained("./checkpoints/music_vocoder", local_files_only=True)
570
+
571
+ wav, sr = librosa.load(x, sr=44100, mono=True)
572
+ wav = torch.from_numpy(wav).float()[None]
573
+ mel = model.encode(wav)
574
+
575
+ wav = model.decode(mel)[0].mT
576
+ sf.write("test_audio_vocoder_rec.flac", wav.cpu().numpy(), 44100)
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ ffmpeg
pipeline_ace_step.py ADDED
@@ -0,0 +1,1744 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import time
3
+ import os
4
+ import re
5
+ import spaces
6
+ import torch
7
+ import torch.nn as nn
8
+ from loguru import logger
9
+ from tqdm import tqdm
10
+ import json
11
+ import math
12
+ from huggingface_hub import hf_hub_download, snapshot_download
13
+
14
+ # from diffusers.pipelines.pipeline_utils import DiffusionPipeline
15
+ from schedulers.scheduling_flow_match_euler_discrete import (
16
+ FlowMatchEulerDiscreteScheduler,
17
+ )
18
+ from schedulers.scheduling_flow_match_heun_discrete import (
19
+ FlowMatchHeunDiscreteScheduler,
20
+ )
21
+ from diffusers.pipelines.stable_diffusion_3.pipeline_stable_diffusion_3 import (
22
+ retrieve_timesteps,
23
+ )
24
+ from diffusers.utils.torch_utils import randn_tensor
25
+ from transformers import UMT5EncoderModel, AutoTokenizer
26
+
27
+ from language_segmentation import LangSegment
28
+ from music_dcae.music_dcae_pipeline import MusicDCAE
29
+ from models.ace_step_transformer import ACEStepTransformer2DModel
30
+ from models.lyrics_utils.lyric_tokenizer import VoiceBpeTokenizer
31
+ from apg_guidance import (
32
+ apg_forward,
33
+ MomentumBuffer,
34
+ cfg_forward,
35
+ cfg_zero_star,
36
+ cfg_double_condition_forward,
37
+ )
38
+ import torchaudio
39
+ import torio
40
+
41
+
42
+ torch.backends.cudnn.benchmark = False
43
+ torch.set_float32_matmul_precision("high")
44
+ torch.backends.cudnn.deterministic = True
45
+ torch.backends.cuda.matmul.allow_tf32 = True
46
+ os.environ["TOKENIZERS_PARALLELISM"] = "false"
47
+
48
+
49
+ SUPPORT_LANGUAGES = {
50
+ "en": 259,
51
+ "de": 260,
52
+ "fr": 262,
53
+ "es": 284,
54
+ "it": 285,
55
+ "pt": 286,
56
+ "pl": 294,
57
+ "tr": 295,
58
+ "ru": 267,
59
+ "cs": 293,
60
+ "nl": 297,
61
+ "ar": 5022,
62
+ "zh": 5023,
63
+ "ja": 5412,
64
+ "hu": 5753,
65
+ "ko": 6152,
66
+ "hi": 6680,
67
+ }
68
+
69
+ structure_pattern = re.compile(r"\[.*?\]")
70
+
71
+
72
+ def ensure_directory_exists(directory):
73
+ directory = str(directory)
74
+ if not os.path.exists(directory):
75
+ os.makedirs(directory)
76
+
77
+
78
+ REPO_ID = "ACE-Step/ACE-Step-v1-3.5B"
79
+
80
+
81
+ # class ACEStepPipeline(DiffusionPipeline):
82
+ class ACEStepPipeline:
83
+
84
+ def __init__(
85
+ self,
86
+ checkpoint_dir=None,
87
+ device_id=0,
88
+ dtype="bfloat16",
89
+ text_encoder_checkpoint_path=None,
90
+ persistent_storage_path=None,
91
+ torch_compile=False,
92
+ **kwargs,
93
+ ):
94
+ if not checkpoint_dir:
95
+ if persistent_storage_path is None:
96
+ checkpoint_dir = os.path.join(os.path.dirname(__file__), "checkpoints")
97
+ else:
98
+ checkpoint_dir = os.path.join(persistent_storage_path, "checkpoints")
99
+ ensure_directory_exists(checkpoint_dir)
100
+ self.checkpoint_dir = checkpoint_dir
101
+ device = (
102
+ torch.device(f"cuda:{device_id}")
103
+ if torch.cuda.is_available()
104
+ else torch.device("cpu")
105
+ )
106
+ if device.type == "cpu" and torch.backends.mps.is_available():
107
+ device = torch.device("mps")
108
+ self.dtype = torch.bfloat16 if dtype == "bfloat16" else torch.float32
109
+ if device.type == "mps":
110
+ self.dtype = torch.float32
111
+ self.device = device
112
+ self.loaded = False
113
+ self.torch_compile = torch_compile
114
+ self.lora_path = "none"
115
+
116
+ def load_lora(self, lora_name_or_path):
117
+ if lora_name_or_path != self.lora_path and lora_name_or_path != "none":
118
+ if not os.path.exists(lora_name_or_path):
119
+ lora_download_path = snapshot_download(
120
+ lora_name_or_path, cache_dir=self.checkpoint_dir
121
+ )
122
+ else:
123
+ lora_download_path = lora_name_or_path
124
+ if self.lora_path != "none":
125
+ self.ace_step_transformer.unload_lora()
126
+ self.ace_step_transformer.load_lora_adapter(
127
+ os.path.join(lora_download_path, "pytorch_lora_weights.safetensors"),
128
+ adapter_name="zh_rap_lora",
129
+ with_alpha=True,
130
+ )
131
+ logger.info(
132
+ f"Loading lora weights from: {lora_name_or_path} download path is: {lora_download_path}"
133
+ )
134
+ self.lora_path = lora_name_or_path
135
+ elif self.lora_path != "none" and lora_name_or_path == "none":
136
+ logger.info("No lora weights to load.")
137
+ self.ace_step_transformer.unload_lora()
138
+
139
+ def load_checkpoint(self, checkpoint_dir=None):
140
+ device = self.device
141
+
142
+ dcae_model_path = os.path.join(checkpoint_dir, "music_dcae_f8c8")
143
+ vocoder_model_path = os.path.join(checkpoint_dir, "music_vocoder")
144
+ ace_step_model_path = os.path.join(checkpoint_dir, "ace_step_transformer")
145
+ text_encoder_model_path = os.path.join(checkpoint_dir, "umt5-base")
146
+
147
+ files_exist = (
148
+ os.path.exists(os.path.join(dcae_model_path, "config.json"))
149
+ and os.path.exists(
150
+ os.path.join(dcae_model_path, "diffusion_pytorch_model.safetensors")
151
+ )
152
+ and os.path.exists(os.path.join(vocoder_model_path, "config.json"))
153
+ and os.path.exists(
154
+ os.path.join(vocoder_model_path, "diffusion_pytorch_model.safetensors")
155
+ )
156
+ and os.path.exists(os.path.join(ace_step_model_path, "config.json"))
157
+ and os.path.exists(
158
+ os.path.join(ace_step_model_path, "diffusion_pytorch_model.safetensors")
159
+ )
160
+ and os.path.exists(os.path.join(text_encoder_model_path, "config.json"))
161
+ and os.path.exists(
162
+ os.path.join(text_encoder_model_path, "model.safetensors")
163
+ )
164
+ and os.path.exists(
165
+ os.path.join(text_encoder_model_path, "special_tokens_map.json")
166
+ )
167
+ and os.path.exists(
168
+ os.path.join(text_encoder_model_path, "tokenizer_config.json")
169
+ )
170
+ and os.path.exists(os.path.join(text_encoder_model_path, "tokenizer.json"))
171
+ )
172
+
173
+ if not files_exist:
174
+ logger.info(
175
+ f"Checkpoint directory {checkpoint_dir} is not complete, downloading from Hugging Face Hub"
176
+ )
177
+
178
+ # download music dcae model
179
+ os.makedirs(dcae_model_path, exist_ok=True)
180
+ hf_hub_download(
181
+ repo_id=REPO_ID,
182
+ subfolder="music_dcae_f8c8",
183
+ filename="config.json",
184
+ local_dir=checkpoint_dir,
185
+ local_dir_use_symlinks=False,
186
+ )
187
+ hf_hub_download(
188
+ repo_id=REPO_ID,
189
+ subfolder="music_dcae_f8c8",
190
+ filename="diffusion_pytorch_model.safetensors",
191
+ local_dir=checkpoint_dir,
192
+ local_dir_use_symlinks=False,
193
+ )
194
+
195
+ # download vocoder model
196
+ os.makedirs(vocoder_model_path, exist_ok=True)
197
+ hf_hub_download(
198
+ repo_id=REPO_ID,
199
+ subfolder="music_vocoder",
200
+ filename="config.json",
201
+ local_dir=checkpoint_dir,
202
+ local_dir_use_symlinks=False,
203
+ )
204
+ hf_hub_download(
205
+ repo_id=REPO_ID,
206
+ subfolder="music_vocoder",
207
+ filename="diffusion_pytorch_model.safetensors",
208
+ local_dir=checkpoint_dir,
209
+ local_dir_use_symlinks=False,
210
+ )
211
+
212
+ # download ace_step transformer model
213
+ os.makedirs(ace_step_model_path, exist_ok=True)
214
+ hf_hub_download(
215
+ repo_id=REPO_ID,
216
+ subfolder="ace_step_transformer",
217
+ filename="config.json",
218
+ local_dir=checkpoint_dir,
219
+ local_dir_use_symlinks=False,
220
+ )
221
+ hf_hub_download(
222
+ repo_id=REPO_ID,
223
+ subfolder="ace_step_transformer",
224
+ filename="diffusion_pytorch_model.safetensors",
225
+ local_dir=checkpoint_dir,
226
+ local_dir_use_symlinks=False,
227
+ )
228
+
229
+ # download text encoder model
230
+ os.makedirs(text_encoder_model_path, exist_ok=True)
231
+ hf_hub_download(
232
+ repo_id=REPO_ID,
233
+ subfolder="umt5-base",
234
+ filename="config.json",
235
+ local_dir=checkpoint_dir,
236
+ local_dir_use_symlinks=False,
237
+ )
238
+ hf_hub_download(
239
+ repo_id=REPO_ID,
240
+ subfolder="umt5-base",
241
+ filename="model.safetensors",
242
+ local_dir=checkpoint_dir,
243
+ local_dir_use_symlinks=False,
244
+ )
245
+ hf_hub_download(
246
+ repo_id=REPO_ID,
247
+ subfolder="umt5-base",
248
+ filename="special_tokens_map.json",
249
+ local_dir=checkpoint_dir,
250
+ local_dir_use_symlinks=False,
251
+ )
252
+ hf_hub_download(
253
+ repo_id=REPO_ID,
254
+ subfolder="umt5-base",
255
+ filename="tokenizer_config.json",
256
+ local_dir=checkpoint_dir,
257
+ local_dir_use_symlinks=False,
258
+ )
259
+ hf_hub_download(
260
+ repo_id=REPO_ID,
261
+ subfolder="umt5-base",
262
+ filename="tokenizer.json",
263
+ local_dir=checkpoint_dir,
264
+ local_dir_use_symlinks=False,
265
+ )
266
+
267
+ logger.info("Models downloaded")
268
+
269
+ dcae_checkpoint_path = dcae_model_path
270
+ vocoder_checkpoint_path = vocoder_model_path
271
+ ace_step_checkpoint_path = ace_step_model_path
272
+ text_encoder_checkpoint_path = text_encoder_model_path
273
+
274
+ self.music_dcae = MusicDCAE(
275
+ dcae_checkpoint_path=dcae_checkpoint_path,
276
+ vocoder_checkpoint_path=vocoder_checkpoint_path,
277
+ )
278
+ self.music_dcae.to(device).eval().to(self.dtype)
279
+
280
+ self.ace_step_transformer = ACEStepTransformer2DModel.from_pretrained(
281
+ ace_step_checkpoint_path, torch_dtype=self.dtype
282
+ )
283
+ self.ace_step_transformer.to(device).eval().to(self.dtype)
284
+
285
+ lang_segment = LangSegment()
286
+
287
+ lang_segment.setfilters(
288
+ [
289
+ "af",
290
+ "am",
291
+ "an",
292
+ "ar",
293
+ "as",
294
+ "az",
295
+ "be",
296
+ "bg",
297
+ "bn",
298
+ "br",
299
+ "bs",
300
+ "ca",
301
+ "cs",
302
+ "cy",
303
+ "da",
304
+ "de",
305
+ "dz",
306
+ "el",
307
+ "en",
308
+ "eo",
309
+ "es",
310
+ "et",
311
+ "eu",
312
+ "fa",
313
+ "fi",
314
+ "fo",
315
+ "fr",
316
+ "ga",
317
+ "gl",
318
+ "gu",
319
+ "he",
320
+ "hi",
321
+ "hr",
322
+ "ht",
323
+ "hu",
324
+ "hy",
325
+ "id",
326
+ "is",
327
+ "it",
328
+ "ja",
329
+ "jv",
330
+ "ka",
331
+ "kk",
332
+ "km",
333
+ "kn",
334
+ "ko",
335
+ "ku",
336
+ "ky",
337
+ "la",
338
+ "lb",
339
+ "lo",
340
+ "lt",
341
+ "lv",
342
+ "mg",
343
+ "mk",
344
+ "ml",
345
+ "mn",
346
+ "mr",
347
+ "ms",
348
+ "mt",
349
+ "nb",
350
+ "ne",
351
+ "nl",
352
+ "nn",
353
+ "no",
354
+ "oc",
355
+ "or",
356
+ "pa",
357
+ "pl",
358
+ "ps",
359
+ "pt",
360
+ "qu",
361
+ "ro",
362
+ "ru",
363
+ "rw",
364
+ "se",
365
+ "si",
366
+ "sk",
367
+ "sl",
368
+ "sq",
369
+ "sr",
370
+ "sv",
371
+ "sw",
372
+ "ta",
373
+ "te",
374
+ "th",
375
+ "tl",
376
+ "tr",
377
+ "ug",
378
+ "uk",
379
+ "ur",
380
+ "vi",
381
+ "vo",
382
+ "wa",
383
+ "xh",
384
+ "zh",
385
+ "zu",
386
+ ]
387
+ )
388
+ self.lang_segment = lang_segment
389
+ self.lyric_tokenizer = VoiceBpeTokenizer()
390
+ text_encoder_model = UMT5EncoderModel.from_pretrained(
391
+ text_encoder_checkpoint_path, torch_dtype=self.dtype
392
+ ).eval()
393
+ text_encoder_model = text_encoder_model.to(device).to(self.dtype)
394
+ text_encoder_model.requires_grad_(False)
395
+ self.text_encoder_model = text_encoder_model
396
+ self.text_tokenizer = AutoTokenizer.from_pretrained(
397
+ text_encoder_checkpoint_path
398
+ )
399
+ self.loaded = True
400
+
401
+ # compile
402
+ if self.torch_compile:
403
+ self.music_dcae = torch.compile(self.music_dcae)
404
+ self.ace_step_transformer = torch.compile(self.ace_step_transformer)
405
+ self.text_encoder_model = torch.compile(self.text_encoder_model)
406
+
407
+ def get_text_embeddings(self, texts, device, text_max_length=256):
408
+ inputs = self.text_tokenizer(
409
+ texts,
410
+ return_tensors="pt",
411
+ padding=True,
412
+ truncation=True,
413
+ max_length=text_max_length,
414
+ )
415
+ inputs = {key: value.to(device) for key, value in inputs.items()}
416
+ if self.text_encoder_model.device != device:
417
+ self.text_encoder_model.to(device)
418
+ with torch.no_grad():
419
+ outputs = self.text_encoder_model(**inputs)
420
+ last_hidden_states = outputs.last_hidden_state
421
+ attention_mask = inputs["attention_mask"]
422
+ return last_hidden_states, attention_mask
423
+
424
+ def get_text_embeddings_null(
425
+ self, texts, device, text_max_length=256, tau=0.01, l_min=8, l_max=10
426
+ ):
427
+ inputs = self.text_tokenizer(
428
+ texts,
429
+ return_tensors="pt",
430
+ padding=True,
431
+ truncation=True,
432
+ max_length=text_max_length,
433
+ )
434
+ inputs = {key: value.to(device) for key, value in inputs.items()}
435
+ if self.text_encoder_model.device != device:
436
+ self.text_encoder_model.to(device)
437
+
438
+ def forward_with_temperature(inputs, tau=0.01, l_min=8, l_max=10):
439
+ handlers = []
440
+
441
+ def hook(module, input, output):
442
+ output[:] *= tau
443
+ return output
444
+
445
+ for i in range(l_min, l_max):
446
+ handler = (
447
+ self.text_encoder_model.encoder.block[i]
448
+ .layer[0]
449
+ .SelfAttention.q.register_forward_hook(hook)
450
+ )
451
+ handlers.append(handler)
452
+
453
+ with torch.no_grad():
454
+ outputs = self.text_encoder_model(**inputs)
455
+ last_hidden_states = outputs.last_hidden_state
456
+
457
+ for hook in handlers:
458
+ hook.remove()
459
+
460
+ return last_hidden_states
461
+
462
+ last_hidden_states = forward_with_temperature(inputs, tau, l_min, l_max)
463
+ return last_hidden_states
464
+
465
+ def set_seeds(self, batch_size, manual_seeds=None):
466
+ processed_input_seeds = None
467
+ if manual_seeds is not None:
468
+ if isinstance(manual_seeds, str):
469
+ if "," in manual_seeds:
470
+ processed_input_seeds = list(map(int, manual_seeds.split(",")))
471
+ elif manual_seeds.isdigit():
472
+ processed_input_seeds = int(manual_seeds)
473
+ elif isinstance(manual_seeds, list) and all(
474
+ isinstance(s, int) for s in manual_seeds
475
+ ):
476
+ if len(manual_seeds) > 0:
477
+ processed_input_seeds = list(manual_seeds)
478
+ elif isinstance(manual_seeds, int):
479
+ processed_input_seeds = manual_seeds
480
+ random_generators = [
481
+ torch.Generator(device=self.device) for _ in range(batch_size)
482
+ ]
483
+ actual_seeds = []
484
+ for i in range(batch_size):
485
+ current_seed_for_generator = None
486
+ if processed_input_seeds is None:
487
+ current_seed_for_generator = torch.randint(0, 2**32, (1,)).item()
488
+ elif isinstance(processed_input_seeds, int):
489
+ current_seed_for_generator = processed_input_seeds
490
+ elif isinstance(processed_input_seeds, list):
491
+ if i < len(processed_input_seeds):
492
+ current_seed_for_generator = processed_input_seeds[i]
493
+ else:
494
+ current_seed_for_generator = processed_input_seeds[-1]
495
+ if current_seed_for_generator is None:
496
+ current_seed_for_generator = torch.randint(0, 2**32, (1,)).item()
497
+ random_generators[i].manual_seed(current_seed_for_generator)
498
+ actual_seeds.append(current_seed_for_generator)
499
+ return random_generators, actual_seeds
500
+
501
+ def get_lang(self, text):
502
+ language = "en"
503
+ try:
504
+ _ = self.lang_segment.getTexts(text)
505
+ langCounts = self.lang_segment.getCounts()
506
+ language = langCounts[0][0]
507
+ if len(langCounts) > 1 and language == "en":
508
+ language = langCounts[1][0]
509
+ except Exception as err:
510
+ language = "en"
511
+ return language
512
+
513
+ def tokenize_lyrics(self, lyrics, debug=False):
514
+ lines = lyrics.split("\n")
515
+ lyric_token_idx = [261]
516
+ for line in lines:
517
+ line = line.strip()
518
+ if not line:
519
+ lyric_token_idx += [2]
520
+ continue
521
+
522
+ lang = self.get_lang(line)
523
+
524
+ if lang not in SUPPORT_LANGUAGES:
525
+ lang = "en"
526
+ if "zh" in lang:
527
+ lang = "zh"
528
+ if "spa" in lang:
529
+ lang = "es"
530
+
531
+ try:
532
+ if structure_pattern.match(line):
533
+ token_idx = self.lyric_tokenizer.encode(line, "en")
534
+ else:
535
+ token_idx = self.lyric_tokenizer.encode(line, lang)
536
+ if debug:
537
+ toks = self.lyric_tokenizer.batch_decode(
538
+ [[tok_id] for tok_id in token_idx]
539
+ )
540
+ logger.info(f"debbug {line} --> {lang} --> {toks}")
541
+ lyric_token_idx = lyric_token_idx + token_idx + [2]
542
+ except Exception as e:
543
+ print("tokenize error", e, "for line", line, "major_language", lang)
544
+ return lyric_token_idx
545
+
546
+ def calc_v(
547
+ self,
548
+ zt_src,
549
+ zt_tar,
550
+ t,
551
+ encoder_text_hidden_states,
552
+ text_attention_mask,
553
+ target_encoder_text_hidden_states,
554
+ target_text_attention_mask,
555
+ speaker_embds,
556
+ target_speaker_embeds,
557
+ lyric_token_ids,
558
+ lyric_mask,
559
+ target_lyric_token_ids,
560
+ target_lyric_mask,
561
+ do_classifier_free_guidance=False,
562
+ guidance_scale=1.0,
563
+ target_guidance_scale=1.0,
564
+ cfg_type="apg",
565
+ attention_mask=None,
566
+ momentum_buffer=None,
567
+ momentum_buffer_tar=None,
568
+ return_src_pred=True,
569
+ ):
570
+ noise_pred_src = None
571
+ if return_src_pred:
572
+ src_latent_model_input = (
573
+ torch.cat([zt_src, zt_src]) if do_classifier_free_guidance else zt_src
574
+ )
575
+ timestep = t.expand(src_latent_model_input.shape[0])
576
+ # source
577
+ noise_pred_src = self.ace_step_transformer(
578
+ hidden_states=src_latent_model_input,
579
+ attention_mask=attention_mask,
580
+ encoder_text_hidden_states=encoder_text_hidden_states,
581
+ text_attention_mask=text_attention_mask,
582
+ speaker_embeds=speaker_embds,
583
+ lyric_token_idx=lyric_token_ids,
584
+ lyric_mask=lyric_mask,
585
+ timestep=timestep,
586
+ ).sample
587
+
588
+ if do_classifier_free_guidance:
589
+ noise_pred_with_cond_src, noise_pred_uncond_src = noise_pred_src.chunk(
590
+ 2
591
+ )
592
+ if cfg_type == "apg":
593
+ noise_pred_src = apg_forward(
594
+ pred_cond=noise_pred_with_cond_src,
595
+ pred_uncond=noise_pred_uncond_src,
596
+ guidance_scale=guidance_scale,
597
+ momentum_buffer=momentum_buffer,
598
+ )
599
+ elif cfg_type == "cfg":
600
+ noise_pred_src = cfg_forward(
601
+ cond_output=noise_pred_with_cond_src,
602
+ uncond_output=noise_pred_uncond_src,
603
+ cfg_strength=guidance_scale,
604
+ )
605
+
606
+ tar_latent_model_input = (
607
+ torch.cat([zt_tar, zt_tar]) if do_classifier_free_guidance else zt_tar
608
+ )
609
+ timestep = t.expand(tar_latent_model_input.shape[0])
610
+ # target
611
+ noise_pred_tar = self.ace_step_transformer(
612
+ hidden_states=tar_latent_model_input,
613
+ attention_mask=attention_mask,
614
+ encoder_text_hidden_states=target_encoder_text_hidden_states,
615
+ text_attention_mask=target_text_attention_mask,
616
+ speaker_embeds=target_speaker_embeds,
617
+ lyric_token_idx=target_lyric_token_ids,
618
+ lyric_mask=target_lyric_mask,
619
+ timestep=timestep,
620
+ ).sample
621
+
622
+ if do_classifier_free_guidance:
623
+ noise_pred_with_cond_tar, noise_pred_uncond_tar = noise_pred_tar.chunk(2)
624
+ if cfg_type == "apg":
625
+ noise_pred_tar = apg_forward(
626
+ pred_cond=noise_pred_with_cond_tar,
627
+ pred_uncond=noise_pred_uncond_tar,
628
+ guidance_scale=target_guidance_scale,
629
+ momentum_buffer=momentum_buffer_tar,
630
+ )
631
+ elif cfg_type == "cfg":
632
+ noise_pred_tar = cfg_forward(
633
+ cond_output=noise_pred_with_cond_tar,
634
+ uncond_output=noise_pred_uncond_tar,
635
+ cfg_strength=target_guidance_scale,
636
+ )
637
+ return noise_pred_src, noise_pred_tar
638
+
639
+ @torch.no_grad()
640
+ def flowedit_diffusion_process(
641
+ self,
642
+ encoder_text_hidden_states,
643
+ text_attention_mask,
644
+ speaker_embds,
645
+ lyric_token_ids,
646
+ lyric_mask,
647
+ target_encoder_text_hidden_states,
648
+ target_text_attention_mask,
649
+ target_speaker_embeds,
650
+ target_lyric_token_ids,
651
+ target_lyric_mask,
652
+ src_latents,
653
+ random_generators=None,
654
+ infer_steps=60,
655
+ guidance_scale=15.0,
656
+ n_min=0,
657
+ n_max=1.0,
658
+ n_avg=1,
659
+ ):
660
+
661
+ do_classifier_free_guidance = True
662
+ if guidance_scale == 0.0 or guidance_scale == 1.0:
663
+ do_classifier_free_guidance = False
664
+
665
+ target_guidance_scale = guidance_scale
666
+ device = encoder_text_hidden_states.device
667
+ dtype = encoder_text_hidden_states.dtype
668
+ bsz = encoder_text_hidden_states.shape[0]
669
+
670
+ scheduler = FlowMatchEulerDiscreteScheduler(
671
+ num_train_timesteps=1000,
672
+ shift=3.0,
673
+ )
674
+
675
+ T_steps = infer_steps
676
+ frame_length = src_latents.shape[-1]
677
+ attention_mask = torch.ones(bsz, frame_length, device=device, dtype=dtype)
678
+
679
+ timesteps, T_steps = retrieve_timesteps(
680
+ scheduler, T_steps, device, timesteps=None
681
+ )
682
+
683
+ if do_classifier_free_guidance:
684
+ attention_mask = torch.cat([attention_mask] * 2, dim=0)
685
+
686
+ encoder_text_hidden_states = torch.cat(
687
+ [
688
+ encoder_text_hidden_states,
689
+ torch.zeros_like(encoder_text_hidden_states),
690
+ ],
691
+ 0,
692
+ )
693
+ text_attention_mask = torch.cat([text_attention_mask] * 2, dim=0)
694
+
695
+ target_encoder_text_hidden_states = torch.cat(
696
+ [
697
+ target_encoder_text_hidden_states,
698
+ torch.zeros_like(target_encoder_text_hidden_states),
699
+ ],
700
+ 0,
701
+ )
702
+ target_text_attention_mask = torch.cat(
703
+ [target_text_attention_mask] * 2, dim=0
704
+ )
705
+
706
+ speaker_embds = torch.cat(
707
+ [speaker_embds, torch.zeros_like(speaker_embds)], 0
708
+ )
709
+ target_speaker_embeds = torch.cat(
710
+ [target_speaker_embeds, torch.zeros_like(target_speaker_embeds)], 0
711
+ )
712
+
713
+ lyric_token_ids = torch.cat(
714
+ [lyric_token_ids, torch.zeros_like(lyric_token_ids)], 0
715
+ )
716
+ lyric_mask = torch.cat([lyric_mask, torch.zeros_like(lyric_mask)], 0)
717
+
718
+ target_lyric_token_ids = torch.cat(
719
+ [target_lyric_token_ids, torch.zeros_like(target_lyric_token_ids)], 0
720
+ )
721
+ target_lyric_mask = torch.cat(
722
+ [target_lyric_mask, torch.zeros_like(target_lyric_mask)], 0
723
+ )
724
+
725
+ momentum_buffer = MomentumBuffer()
726
+ momentum_buffer_tar = MomentumBuffer()
727
+ x_src = src_latents
728
+ zt_edit = x_src.clone()
729
+ xt_tar = None
730
+ n_min = int(infer_steps * n_min)
731
+ n_max = int(infer_steps * n_max)
732
+
733
+ logger.info("flowedit start from {} to {}".format(n_min, n_max))
734
+
735
+ for i, t in tqdm(enumerate(timesteps), total=T_steps):
736
+
737
+ if i < n_min:
738
+ continue
739
+
740
+ t_i = t / 1000
741
+
742
+ if i + 1 < len(timesteps):
743
+ t_im1 = (timesteps[i + 1]) / 1000
744
+ else:
745
+ t_im1 = torch.zeros_like(t_i).to(t_i.device)
746
+
747
+ if i < n_max:
748
+ # Calculate the average of the V predictions
749
+ V_delta_avg = torch.zeros_like(x_src)
750
+ for k in range(n_avg):
751
+ fwd_noise = randn_tensor(
752
+ shape=x_src.shape,
753
+ generator=random_generators,
754
+ device=device,
755
+ dtype=dtype,
756
+ )
757
+
758
+ zt_src = (1 - t_i) * x_src + (t_i) * fwd_noise
759
+
760
+ zt_tar = zt_edit + zt_src - x_src
761
+
762
+ Vt_src, Vt_tar = self.calc_v(
763
+ zt_src=zt_src,
764
+ zt_tar=zt_tar,
765
+ t=t,
766
+ encoder_text_hidden_states=encoder_text_hidden_states,
767
+ text_attention_mask=text_attention_mask,
768
+ target_encoder_text_hidden_states=target_encoder_text_hidden_states,
769
+ target_text_attention_mask=target_text_attention_mask,
770
+ speaker_embds=speaker_embds,
771
+ target_speaker_embeds=target_speaker_embeds,
772
+ lyric_token_ids=lyric_token_ids,
773
+ lyric_mask=lyric_mask,
774
+ target_lyric_token_ids=target_lyric_token_ids,
775
+ target_lyric_mask=target_lyric_mask,
776
+ do_classifier_free_guidance=do_classifier_free_guidance,
777
+ guidance_scale=guidance_scale,
778
+ target_guidance_scale=target_guidance_scale,
779
+ attention_mask=attention_mask,
780
+ momentum_buffer=momentum_buffer,
781
+ )
782
+ V_delta_avg += (1 / n_avg) * (
783
+ Vt_tar - Vt_src
784
+ ) # - (hfg-1)*( x_src))
785
+
786
+ # propagate direct ODE
787
+ zt_edit = zt_edit.to(torch.float32)
788
+ zt_edit = zt_edit + (t_im1 - t_i) * V_delta_avg
789
+ zt_edit = zt_edit.to(V_delta_avg.dtype)
790
+ else: # i >= T_steps-n_min # regular sampling for last n_min steps
791
+ if i == n_max:
792
+ fwd_noise = randn_tensor(
793
+ shape=x_src.shape,
794
+ generator=random_generators,
795
+ device=device,
796
+ dtype=dtype,
797
+ )
798
+ scheduler._init_step_index(t)
799
+ sigma = scheduler.sigmas[scheduler.step_index]
800
+ xt_src = sigma * fwd_noise + (1.0 - sigma) * x_src
801
+ xt_tar = zt_edit + xt_src - x_src
802
+
803
+ _, Vt_tar = self.calc_v(
804
+ zt_src=None,
805
+ zt_tar=xt_tar,
806
+ t=t,
807
+ encoder_text_hidden_states=encoder_text_hidden_states,
808
+ text_attention_mask=text_attention_mask,
809
+ target_encoder_text_hidden_states=target_encoder_text_hidden_states,
810
+ target_text_attention_mask=target_text_attention_mask,
811
+ speaker_embds=speaker_embds,
812
+ target_speaker_embeds=target_speaker_embeds,
813
+ lyric_token_ids=lyric_token_ids,
814
+ lyric_mask=lyric_mask,
815
+ target_lyric_token_ids=target_lyric_token_ids,
816
+ target_lyric_mask=target_lyric_mask,
817
+ do_classifier_free_guidance=do_classifier_free_guidance,
818
+ guidance_scale=guidance_scale,
819
+ target_guidance_scale=target_guidance_scale,
820
+ attention_mask=attention_mask,
821
+ momentum_buffer_tar=momentum_buffer_tar,
822
+ return_src_pred=False,
823
+ )
824
+
825
+ dtype = Vt_tar.dtype
826
+ xt_tar = xt_tar.to(torch.float32)
827
+ prev_sample = xt_tar + (t_im1 - t_i) * Vt_tar
828
+ prev_sample = prev_sample.to(dtype)
829
+ xt_tar = prev_sample
830
+
831
+ target_latents = zt_edit if xt_tar is None else xt_tar
832
+ return target_latents
833
+
834
+ def add_latents_noise(
835
+ self,
836
+ gt_latents,
837
+ variance,
838
+ noise,
839
+ scheduler,
840
+ ):
841
+
842
+ bsz = gt_latents.shape[0]
843
+ u = torch.tensor([variance] * bsz, dtype=gt_latents.dtype)
844
+ indices = (u * scheduler.config.num_train_timesteps).long()
845
+ timesteps = scheduler.timesteps.unsqueeze(1).to(gt_latents.dtype)
846
+ indices = indices.to(timesteps.device).to(gt_latents.dtype).unsqueeze(1)
847
+ nearest_idx = torch.argmin(torch.cdist(indices, timesteps), dim=1)
848
+ sigma = (
849
+ scheduler.sigmas[nearest_idx]
850
+ .flatten()
851
+ .to(gt_latents.device)
852
+ .to(gt_latents.dtype)
853
+ )
854
+ while len(sigma.shape) < gt_latents.ndim:
855
+ sigma = sigma.unsqueeze(-1)
856
+ noisy_image = sigma * noise + (1.0 - sigma) * gt_latents
857
+ init_timestep = indices[0]
858
+ return noisy_image, init_timestep
859
+
860
+ @torch.no_grad()
861
+ def text2music_diffusion_process(
862
+ self,
863
+ duration,
864
+ encoder_text_hidden_states,
865
+ text_attention_mask,
866
+ speaker_embds,
867
+ lyric_token_ids,
868
+ lyric_mask,
869
+ random_generators=None,
870
+ infer_steps=60,
871
+ guidance_scale=15.0,
872
+ omega_scale=10.0,
873
+ scheduler_type="euler",
874
+ cfg_type="apg",
875
+ zero_steps=1,
876
+ use_zero_init=True,
877
+ guidance_interval=0.5,
878
+ guidance_interval_decay=1.0,
879
+ min_guidance_scale=3.0,
880
+ oss_steps=[],
881
+ encoder_text_hidden_states_null=None,
882
+ use_erg_lyric=False,
883
+ use_erg_diffusion=False,
884
+ retake_random_generators=None,
885
+ retake_variance=0.5,
886
+ add_retake_noise=False,
887
+ guidance_scale_text=0.0,
888
+ guidance_scale_lyric=0.0,
889
+ repaint_start=0,
890
+ repaint_end=0,
891
+ src_latents=None,
892
+ audio2audio_enable=False,
893
+ ref_audio_strength=0.5,
894
+ ref_latents=None,
895
+ ):
896
+
897
+ logger.info(
898
+ "cfg_type: {}, guidance_scale: {}, omega_scale: {}".format(
899
+ cfg_type, guidance_scale, omega_scale
900
+ )
901
+ )
902
+ do_classifier_free_guidance = True
903
+ if guidance_scale == 0.0 or guidance_scale == 1.0:
904
+ do_classifier_free_guidance = False
905
+
906
+ do_double_condition_guidance = False
907
+ if (
908
+ guidance_scale_text is not None
909
+ and guidance_scale_text > 1.0
910
+ and guidance_scale_lyric is not None
911
+ and guidance_scale_lyric > 1.0
912
+ ):
913
+ do_double_condition_guidance = True
914
+ logger.info(
915
+ "do_double_condition_guidance: {}, guidance_scale_text: {}, guidance_scale_lyric: {}".format(
916
+ do_double_condition_guidance,
917
+ guidance_scale_text,
918
+ guidance_scale_lyric,
919
+ )
920
+ )
921
+
922
+ device = encoder_text_hidden_states.device
923
+ dtype = encoder_text_hidden_states.dtype
924
+ bsz = encoder_text_hidden_states.shape[0]
925
+
926
+ if scheduler_type == "euler":
927
+ scheduler = FlowMatchEulerDiscreteScheduler(
928
+ num_train_timesteps=1000,
929
+ shift=3.0,
930
+ )
931
+ elif scheduler_type == "heun":
932
+ scheduler = FlowMatchHeunDiscreteScheduler(
933
+ num_train_timesteps=1000,
934
+ shift=3.0,
935
+ )
936
+
937
+ frame_length = int(duration * 44100 / 512 / 8)
938
+ if src_latents is not None:
939
+ frame_length = src_latents.shape[-1]
940
+
941
+ if ref_latents is not None:
942
+ frame_length = ref_latents.shape[-1]
943
+
944
+ if len(oss_steps) > 0:
945
+ infer_steps = max(oss_steps)
946
+ scheduler.set_timesteps
947
+ timesteps, num_inference_steps = retrieve_timesteps(
948
+ scheduler,
949
+ num_inference_steps=infer_steps,
950
+ device=device,
951
+ timesteps=None,
952
+ )
953
+ new_timesteps = torch.zeros(len(oss_steps), dtype=dtype, device=device)
954
+ for idx in range(len(oss_steps)):
955
+ new_timesteps[idx] = timesteps[oss_steps[idx] - 1]
956
+ num_inference_steps = len(oss_steps)
957
+ sigmas = (new_timesteps / 1000).float().cpu().numpy()
958
+ timesteps, num_inference_steps = retrieve_timesteps(
959
+ scheduler,
960
+ num_inference_steps=num_inference_steps,
961
+ device=device,
962
+ sigmas=sigmas,
963
+ )
964
+ logger.info(
965
+ f"oss_steps: {oss_steps}, num_inference_steps: {num_inference_steps} after remapping to timesteps {timesteps}"
966
+ )
967
+ else:
968
+ timesteps, num_inference_steps = retrieve_timesteps(
969
+ scheduler,
970
+ num_inference_steps=infer_steps,
971
+ device=device,
972
+ timesteps=None,
973
+ )
974
+
975
+ target_latents = randn_tensor(
976
+ shape=(bsz, 8, 16, frame_length),
977
+ generator=random_generators,
978
+ device=device,
979
+ dtype=dtype,
980
+ )
981
+
982
+ is_repaint = False
983
+ is_extend = False
984
+ if add_retake_noise:
985
+ n_min = int(infer_steps * (1 - retake_variance))
986
+ retake_variance = (
987
+ torch.tensor(retake_variance * math.pi / 2).to(device).to(dtype)
988
+ )
989
+ retake_latents = randn_tensor(
990
+ shape=(bsz, 8, 16, frame_length),
991
+ generator=retake_random_generators,
992
+ device=device,
993
+ dtype=dtype,
994
+ )
995
+ repaint_start_frame = int(repaint_start * 44100 / 512 / 8)
996
+ repaint_end_frame = int(repaint_end * 44100 / 512 / 8)
997
+ x0 = src_latents
998
+ # retake
999
+ is_repaint = repaint_end_frame - repaint_start_frame != frame_length
1000
+
1001
+ is_extend = (repaint_start_frame < 0) or (repaint_end_frame > frame_length)
1002
+ if is_extend:
1003
+ is_repaint = True
1004
+
1005
+ # TODO: train a mask aware repainting controlnet
1006
+ # to make sure mean = 0, std = 1
1007
+ if not is_repaint:
1008
+ target_latents = (
1009
+ torch.cos(retake_variance) * target_latents
1010
+ + torch.sin(retake_variance) * retake_latents
1011
+ )
1012
+ elif not is_extend:
1013
+ # if repaint_end_frame
1014
+ repaint_mask = torch.zeros(
1015
+ (bsz, 8, 16, frame_length), device=device, dtype=dtype
1016
+ )
1017
+ repaint_mask[:, :, :, repaint_start_frame:repaint_end_frame] = 1.0
1018
+ repaint_noise = (
1019
+ torch.cos(retake_variance) * target_latents
1020
+ + torch.sin(retake_variance) * retake_latents
1021
+ )
1022
+ repaint_noise = torch.where(
1023
+ repaint_mask == 1.0, repaint_noise, target_latents
1024
+ )
1025
+ zt_edit = x0.clone()
1026
+ z0 = repaint_noise
1027
+ elif is_extend:
1028
+ to_right_pad_gt_latents = None
1029
+ to_left_pad_gt_latents = None
1030
+ gt_latents = src_latents
1031
+ src_latents_length = gt_latents.shape[-1]
1032
+ max_infer_fame_length = int(240 * 44100 / 512 / 8)
1033
+ left_pad_frame_length = 0
1034
+ right_pad_frame_length = 0
1035
+ right_trim_length = 0
1036
+ left_trim_length = 0
1037
+ if repaint_start_frame < 0:
1038
+ left_pad_frame_length = abs(repaint_start_frame)
1039
+ frame_length = left_pad_frame_length + gt_latents.shape[-1]
1040
+ extend_gt_latents = torch.nn.functional.pad(
1041
+ gt_latents, (left_pad_frame_length, 0), "constant", 0
1042
+ )
1043
+ if frame_length > max_infer_fame_length:
1044
+ right_trim_length = frame_length - max_infer_fame_length
1045
+ extend_gt_latents = extend_gt_latents[
1046
+ :, :, :, :max_infer_fame_length
1047
+ ]
1048
+ to_right_pad_gt_latents = extend_gt_latents[
1049
+ :, :, :, -right_trim_length:
1050
+ ]
1051
+ frame_length = max_infer_fame_length
1052
+ repaint_start_frame = 0
1053
+ gt_latents = extend_gt_latents
1054
+
1055
+ if repaint_end_frame > src_latents_length:
1056
+ right_pad_frame_length = repaint_end_frame - gt_latents.shape[-1]
1057
+ frame_length = gt_latents.shape[-1] + right_pad_frame_length
1058
+ extend_gt_latents = torch.nn.functional.pad(
1059
+ gt_latents, (0, right_pad_frame_length), "constant", 0
1060
+ )
1061
+ if frame_length > max_infer_fame_length:
1062
+ left_trim_length = frame_length - max_infer_fame_length
1063
+ extend_gt_latents = extend_gt_latents[
1064
+ :, :, :, -max_infer_fame_length:
1065
+ ]
1066
+ to_left_pad_gt_latents = extend_gt_latents[
1067
+ :, :, :, :left_trim_length
1068
+ ]
1069
+ frame_length = max_infer_fame_length
1070
+ repaint_end_frame = frame_length
1071
+ gt_latents = extend_gt_latents
1072
+
1073
+ repaint_mask = torch.zeros(
1074
+ (bsz, 8, 16, frame_length), device=device, dtype=dtype
1075
+ )
1076
+ if left_pad_frame_length > 0:
1077
+ repaint_mask[:, :, :, :left_pad_frame_length] = 1.0
1078
+ if right_pad_frame_length > 0:
1079
+ repaint_mask[:, :, :, -right_pad_frame_length:] = 1.0
1080
+ x0 = gt_latents
1081
+ padd_list = []
1082
+ if left_pad_frame_length > 0:
1083
+ padd_list.append(retake_latents[:, :, :, :left_pad_frame_length])
1084
+ padd_list.append(
1085
+ target_latents[
1086
+ :,
1087
+ :,
1088
+ :,
1089
+ left_trim_length : target_latents.shape[-1] - right_trim_length,
1090
+ ]
1091
+ )
1092
+ if right_pad_frame_length > 0:
1093
+ padd_list.append(retake_latents[:, :, :, -right_pad_frame_length:])
1094
+ target_latents = torch.cat(padd_list, dim=-1)
1095
+ assert (
1096
+ target_latents.shape[-1] == x0.shape[-1]
1097
+ ), f"{target_latents.shape=} {x0.shape=}"
1098
+ zt_edit = x0.clone()
1099
+ z0 = target_latents
1100
+
1101
+ init_timestep = 1000
1102
+ if audio2audio_enable and ref_latents is not None:
1103
+ target_latents, init_timestep = self.add_latents_noise(
1104
+ gt_latents=ref_latents,
1105
+ variance=(1 - ref_audio_strength),
1106
+ noise=target_latents,
1107
+ scheduler=scheduler,
1108
+ )
1109
+
1110
+ attention_mask = torch.ones(bsz, frame_length, device=device, dtype=dtype)
1111
+
1112
+ # guidance interval
1113
+ start_idx = int(num_inference_steps * ((1 - guidance_interval) / 2))
1114
+ end_idx = int(num_inference_steps * (guidance_interval / 2 + 0.5))
1115
+ logger.info(
1116
+ f"start_idx: {start_idx}, end_idx: {end_idx}, num_inference_steps: {num_inference_steps}"
1117
+ )
1118
+
1119
+ momentum_buffer = MomentumBuffer()
1120
+
1121
+ def forward_encoder_with_temperature(self, inputs, tau=0.01, l_min=4, l_max=6):
1122
+ handlers = []
1123
+
1124
+ def hook(module, input, output):
1125
+ output[:] *= tau
1126
+ return output
1127
+
1128
+ for i in range(l_min, l_max):
1129
+ handler = self.ace_step_transformer.lyric_encoder.encoders[
1130
+ i
1131
+ ].self_attn.linear_q.register_forward_hook(hook)
1132
+ handlers.append(handler)
1133
+
1134
+ encoder_hidden_states, encoder_hidden_mask = (
1135
+ self.ace_step_transformer.encode(**inputs)
1136
+ )
1137
+
1138
+ for hook in handlers:
1139
+ hook.remove()
1140
+
1141
+ return encoder_hidden_states
1142
+
1143
+ # P(speaker, text, lyric)
1144
+ encoder_hidden_states, encoder_hidden_mask = self.ace_step_transformer.encode(
1145
+ encoder_text_hidden_states,
1146
+ text_attention_mask,
1147
+ speaker_embds,
1148
+ lyric_token_ids,
1149
+ lyric_mask,
1150
+ )
1151
+
1152
+ if use_erg_lyric:
1153
+ # P(null_speaker, text_weaker, lyric_weaker)
1154
+ encoder_hidden_states_null = forward_encoder_with_temperature(
1155
+ self,
1156
+ inputs={
1157
+ "encoder_text_hidden_states": (
1158
+ encoder_text_hidden_states_null
1159
+ if encoder_text_hidden_states_null is not None
1160
+ else torch.zeros_like(encoder_text_hidden_states)
1161
+ ),
1162
+ "text_attention_mask": text_attention_mask,
1163
+ "speaker_embeds": torch.zeros_like(speaker_embds),
1164
+ "lyric_token_idx": lyric_token_ids,
1165
+ "lyric_mask": lyric_mask,
1166
+ },
1167
+ )
1168
+ else:
1169
+ # P(null_speaker, null_text, null_lyric)
1170
+ encoder_hidden_states_null, _ = self.ace_step_transformer.encode(
1171
+ torch.zeros_like(encoder_text_hidden_states),
1172
+ text_attention_mask,
1173
+ torch.zeros_like(speaker_embds),
1174
+ torch.zeros_like(lyric_token_ids),
1175
+ lyric_mask,
1176
+ )
1177
+
1178
+ encoder_hidden_states_no_lyric = None
1179
+ if do_double_condition_guidance:
1180
+ # P(null_speaker, text, lyric_weaker)
1181
+ if use_erg_lyric:
1182
+ encoder_hidden_states_no_lyric = forward_encoder_with_temperature(
1183
+ self,
1184
+ inputs={
1185
+ "encoder_text_hidden_states": encoder_text_hidden_states,
1186
+ "text_attention_mask": text_attention_mask,
1187
+ "speaker_embeds": torch.zeros_like(speaker_embds),
1188
+ "lyric_token_idx": lyric_token_ids,
1189
+ "lyric_mask": lyric_mask,
1190
+ },
1191
+ )
1192
+ # P(null_speaker, text, no_lyric)
1193
+ else:
1194
+ encoder_hidden_states_no_lyric, _ = self.ace_step_transformer.encode(
1195
+ encoder_text_hidden_states,
1196
+ text_attention_mask,
1197
+ torch.zeros_like(speaker_embds),
1198
+ torch.zeros_like(lyric_token_ids),
1199
+ lyric_mask,
1200
+ )
1201
+
1202
+ def forward_diffusion_with_temperature(
1203
+ self, hidden_states, timestep, inputs, tau=0.01, l_min=15, l_max=20
1204
+ ):
1205
+ handlers = []
1206
+
1207
+ def hook(module, input, output):
1208
+ output[:] *= tau
1209
+ return output
1210
+
1211
+ for i in range(l_min, l_max):
1212
+ handler = self.ace_step_transformer.transformer_blocks[
1213
+ i
1214
+ ].attn.to_q.register_forward_hook(hook)
1215
+ handlers.append(handler)
1216
+ handler = self.ace_step_transformer.transformer_blocks[
1217
+ i
1218
+ ].cross_attn.to_q.register_forward_hook(hook)
1219
+ handlers.append(handler)
1220
+
1221
+ sample = self.ace_step_transformer.decode(
1222
+ hidden_states=hidden_states, timestep=timestep, **inputs
1223
+ ).sample
1224
+
1225
+ for hook in handlers:
1226
+ hook.remove()
1227
+
1228
+ return sample
1229
+
1230
+ for i, t in tqdm(enumerate(timesteps), total=num_inference_steps):
1231
+
1232
+ if t > init_timestep:
1233
+ continue
1234
+
1235
+ if is_repaint:
1236
+ if i < n_min:
1237
+ continue
1238
+ elif i == n_min:
1239
+ t_i = t / 1000
1240
+ zt_src = (1 - t_i) * x0 + (t_i) * z0
1241
+ target_latents = zt_edit + zt_src - x0
1242
+ logger.info(f"repaint start from {n_min} add {t_i} level of noise")
1243
+
1244
+ # expand the latents if we are doing classifier free guidance
1245
+ latents = target_latents
1246
+
1247
+ is_in_guidance_interval = start_idx <= i < end_idx
1248
+ if is_in_guidance_interval and do_classifier_free_guidance:
1249
+ # compute current guidance scale
1250
+ if guidance_interval_decay > 0:
1251
+ # Linearly interpolate to calculate the current guidance scale
1252
+ progress = (i - start_idx) / (
1253
+ end_idx - start_idx - 1
1254
+ ) # 归一化到[0,1]
1255
+ current_guidance_scale = (
1256
+ guidance_scale
1257
+ - (guidance_scale - min_guidance_scale)
1258
+ * progress
1259
+ * guidance_interval_decay
1260
+ )
1261
+ else:
1262
+ current_guidance_scale = guidance_scale
1263
+
1264
+ latent_model_input = latents
1265
+ timestep = t.expand(latent_model_input.shape[0])
1266
+ output_length = latent_model_input.shape[-1]
1267
+ # P(x|speaker, text, lyric)
1268
+ noise_pred_with_cond = self.ace_step_transformer.decode(
1269
+ hidden_states=latent_model_input,
1270
+ attention_mask=attention_mask,
1271
+ encoder_hidden_states=encoder_hidden_states,
1272
+ encoder_hidden_mask=encoder_hidden_mask,
1273
+ output_length=output_length,
1274
+ timestep=timestep,
1275
+ ).sample
1276
+
1277
+ noise_pred_with_only_text_cond = None
1278
+ if (
1279
+ do_double_condition_guidance
1280
+ and encoder_hidden_states_no_lyric is not None
1281
+ ):
1282
+ noise_pred_with_only_text_cond = self.ace_step_transformer.decode(
1283
+ hidden_states=latent_model_input,
1284
+ attention_mask=attention_mask,
1285
+ encoder_hidden_states=encoder_hidden_states_no_lyric,
1286
+ encoder_hidden_mask=encoder_hidden_mask,
1287
+ output_length=output_length,
1288
+ timestep=timestep,
1289
+ ).sample
1290
+
1291
+ if use_erg_diffusion:
1292
+ noise_pred_uncond = forward_diffusion_with_temperature(
1293
+ self,
1294
+ hidden_states=latent_model_input,
1295
+ timestep=timestep,
1296
+ inputs={
1297
+ "encoder_hidden_states": encoder_hidden_states_null,
1298
+ "encoder_hidden_mask": encoder_hidden_mask,
1299
+ "output_length": output_length,
1300
+ "attention_mask": attention_mask,
1301
+ },
1302
+ )
1303
+ else:
1304
+ noise_pred_uncond = self.ace_step_transformer.decode(
1305
+ hidden_states=latent_model_input,
1306
+ attention_mask=attention_mask,
1307
+ encoder_hidden_states=encoder_hidden_states_null,
1308
+ encoder_hidden_mask=encoder_hidden_mask,
1309
+ output_length=output_length,
1310
+ timestep=timestep,
1311
+ ).sample
1312
+
1313
+ if (
1314
+ do_double_condition_guidance
1315
+ and noise_pred_with_only_text_cond is not None
1316
+ ):
1317
+ noise_pred = cfg_double_condition_forward(
1318
+ cond_output=noise_pred_with_cond,
1319
+ uncond_output=noise_pred_uncond,
1320
+ only_text_cond_output=noise_pred_with_only_text_cond,
1321
+ guidance_scale_text=guidance_scale_text,
1322
+ guidance_scale_lyric=guidance_scale_lyric,
1323
+ )
1324
+
1325
+ elif cfg_type == "apg":
1326
+ noise_pred = apg_forward(
1327
+ pred_cond=noise_pred_with_cond,
1328
+ pred_uncond=noise_pred_uncond,
1329
+ guidance_scale=current_guidance_scale,
1330
+ momentum_buffer=momentum_buffer,
1331
+ )
1332
+ elif cfg_type == "cfg":
1333
+ noise_pred = cfg_forward(
1334
+ cond_output=noise_pred_with_cond,
1335
+ uncond_output=noise_pred_uncond,
1336
+ cfg_strength=current_guidance_scale,
1337
+ )
1338
+ elif cfg_type == "cfg_star":
1339
+ noise_pred = cfg_zero_star(
1340
+ noise_pred_with_cond=noise_pred_with_cond,
1341
+ noise_pred_uncond=noise_pred_uncond,
1342
+ guidance_scale=current_guidance_scale,
1343
+ i=i,
1344
+ zero_steps=zero_steps,
1345
+ use_zero_init=use_zero_init,
1346
+ )
1347
+ else:
1348
+ latent_model_input = latents
1349
+ timestep = t.expand(latent_model_input.shape[0])
1350
+ noise_pred = self.ace_step_transformer.decode(
1351
+ hidden_states=latent_model_input,
1352
+ attention_mask=attention_mask,
1353
+ encoder_hidden_states=encoder_hidden_states,
1354
+ encoder_hidden_mask=encoder_hidden_mask,
1355
+ output_length=latent_model_input.shape[-1],
1356
+ timestep=timestep,
1357
+ ).sample
1358
+
1359
+ if is_repaint and i >= n_min:
1360
+ t_i = t / 1000
1361
+ if i + 1 < len(timesteps):
1362
+ t_im1 = (timesteps[i + 1]) / 1000
1363
+ else:
1364
+ t_im1 = torch.zeros_like(t_i).to(t_i.device)
1365
+ dtype = noise_pred.dtype
1366
+ target_latents = target_latents.to(torch.float32)
1367
+ prev_sample = target_latents + (t_im1 - t_i) * noise_pred
1368
+ prev_sample = prev_sample.to(dtype)
1369
+ target_latents = prev_sample
1370
+ zt_src = (1 - t_im1) * x0 + (t_im1) * z0
1371
+ target_latents = torch.where(
1372
+ repaint_mask == 1.0, target_latents, zt_src
1373
+ )
1374
+ else:
1375
+ target_latents = scheduler.step(
1376
+ model_output=noise_pred,
1377
+ timestep=t,
1378
+ sample=target_latents,
1379
+ return_dict=False,
1380
+ omega=omega_scale,
1381
+ )[0]
1382
+
1383
+ if is_extend:
1384
+ if to_right_pad_gt_latents is not None:
1385
+ target_latents = torch.cat(
1386
+ [target_latents, to_right_pad_gt_latents], dim=-1
1387
+ )
1388
+ if to_left_pad_gt_latents is not None:
1389
+ target_latents = torch.cat(
1390
+ [to_right_pad_gt_latents, target_latents], dim=0
1391
+ )
1392
+ return target_latents
1393
+
1394
+ def latents2audio(
1395
+ self,
1396
+ latents,
1397
+ target_wav_duration_second=30,
1398
+ sample_rate=48000,
1399
+ save_path=None,
1400
+ format="mp3",
1401
+ ):
1402
+ output_audio_paths = []
1403
+ bs = latents.shape[0]
1404
+ audio_lengths = [target_wav_duration_second * sample_rate] * bs
1405
+ pred_latents = latents
1406
+ with torch.no_grad():
1407
+ _, pred_wavs = self.music_dcae.decode(pred_latents, sr=sample_rate)
1408
+ pred_wavs = [pred_wav.cpu().float() for pred_wav in pred_wavs]
1409
+ for i in tqdm(range(bs)):
1410
+ output_audio_path = self.save_wav_file(
1411
+ pred_wavs[i], i, sample_rate=sample_rate
1412
+ )
1413
+ output_audio_paths.append(output_audio_path)
1414
+ return output_audio_paths
1415
+
1416
+ def save_wav_file(
1417
+ self, target_wav, idx, save_path=None, sample_rate=48000, format="mp3"
1418
+ ):
1419
+ if save_path is None:
1420
+ logger.warning("save_path is None, using default path ./outputs/")
1421
+ base_path = f"./outputs"
1422
+ ensure_directory_exists(base_path)
1423
+ else:
1424
+ base_path = save_path
1425
+ ensure_directory_exists(base_path)
1426
+
1427
+ output_path_flac = (
1428
+ f"{base_path}/output_{time.strftime('%Y%m%d%H%M%S')}_{idx}.{format}"
1429
+ )
1430
+ target_wav = target_wav.float()
1431
+ torchaudio.save(
1432
+ output_path_flac,
1433
+ target_wav,
1434
+ sample_rate=sample_rate,
1435
+ format=format,
1436
+ compression=torio.io.CodecConfig(bit_rate=320000),
1437
+ )
1438
+ return output_path_flac
1439
+
1440
+ def infer_latents(self, input_audio_path):
1441
+ if input_audio_path is None:
1442
+ return None
1443
+ input_audio, sr = self.music_dcae.load_audio(input_audio_path)
1444
+ input_audio = input_audio.unsqueeze(0)
1445
+ device, dtype = self.device, self.dtype
1446
+ input_audio = input_audio.to(device=device, dtype=dtype)
1447
+ latents, _ = self.music_dcae.encode(input_audio, sr=sr)
1448
+ return latents
1449
+
1450
+ @spaces.GPU(duration=120)
1451
+ def __call__(
1452
+ self,
1453
+ audio_duration: float = 60.0,
1454
+ prompt: str = None,
1455
+ lyrics: str = None,
1456
+ infer_step: int = 60,
1457
+ guidance_scale: float = 15.0,
1458
+ scheduler_type: str = "euler",
1459
+ cfg_type: str = "apg",
1460
+ omega_scale: int = 10.0,
1461
+ manual_seeds: list = None,
1462
+ guidance_interval: float = 0.5,
1463
+ guidance_interval_decay: float = 0.0,
1464
+ min_guidance_scale: float = 3.0,
1465
+ use_erg_tag: bool = True,
1466
+ use_erg_lyric: bool = True,
1467
+ use_erg_diffusion: bool = True,
1468
+ oss_steps: str = None,
1469
+ guidance_scale_text: float = 0.0,
1470
+ guidance_scale_lyric: float = 0.0,
1471
+ audio2audio_enable: bool = False,
1472
+ ref_audio_strength: float = 0.5,
1473
+ ref_audio_input: str = None,
1474
+ lora_name_or_path: str = "none",
1475
+ retake_seeds: list = None,
1476
+ retake_variance: float = 0.5,
1477
+ task: str = "text2music",
1478
+ repaint_start: int = 0,
1479
+ repaint_end: int = 0,
1480
+ src_audio_path: str = None,
1481
+ edit_target_prompt: str = None,
1482
+ edit_target_lyrics: str = None,
1483
+ edit_n_min: float = 0.0,
1484
+ edit_n_max: float = 1.0,
1485
+ edit_n_avg: int = 1,
1486
+ save_path: str = None,
1487
+ format: str = "mp3",
1488
+ batch_size: int = 1,
1489
+ debug: bool = False,
1490
+ ):
1491
+
1492
+ start_time = time.time()
1493
+
1494
+ if audio2audio_enable and ref_audio_input is not None:
1495
+ task = "audio2audio"
1496
+
1497
+ if not self.loaded:
1498
+ logger.warning("Checkpoint not loaded, loading checkpoint...")
1499
+ self.load_checkpoint(self.checkpoint_dir)
1500
+ load_model_cost = time.time() - start_time
1501
+ logger.info(f"Model loaded in {load_model_cost:.2f} seconds.")
1502
+ self.load_lora(lora_name_or_path)
1503
+ start_time = time.time()
1504
+
1505
+ random_generators, actual_seeds = self.set_seeds(batch_size, manual_seeds)
1506
+ retake_random_generators, actual_retake_seeds = self.set_seeds(
1507
+ batch_size, retake_seeds
1508
+ )
1509
+
1510
+ if isinstance(oss_steps, str) and len(oss_steps) > 0:
1511
+ oss_steps = list(map(int, oss_steps.split(",")))
1512
+ else:
1513
+ oss_steps = []
1514
+
1515
+ texts = [prompt]
1516
+ encoder_text_hidden_states, text_attention_mask = self.get_text_embeddings(
1517
+ texts, self.device
1518
+ )
1519
+ encoder_text_hidden_states = encoder_text_hidden_states.repeat(batch_size, 1, 1)
1520
+ text_attention_mask = text_attention_mask.repeat(batch_size, 1)
1521
+
1522
+ encoder_text_hidden_states_null = None
1523
+ if use_erg_tag:
1524
+ encoder_text_hidden_states_null = self.get_text_embeddings_null(
1525
+ texts, self.device
1526
+ )
1527
+ encoder_text_hidden_states_null = encoder_text_hidden_states_null.repeat(
1528
+ batch_size, 1, 1
1529
+ )
1530
+
1531
+ # not support for released checkpoint
1532
+ speaker_embeds = torch.zeros(batch_size, 512).to(self.device).to(self.dtype)
1533
+
1534
+ # 6 lyric
1535
+ lyric_token_idx = torch.tensor([0]).repeat(batch_size, 1).to(self.device).long()
1536
+ lyric_mask = torch.tensor([0]).repeat(batch_size, 1).to(self.device).long()
1537
+ if len(lyrics) > 0:
1538
+ lyric_token_idx = self.tokenize_lyrics(lyrics, debug=debug)
1539
+ lyric_mask = [1] * len(lyric_token_idx)
1540
+ lyric_token_idx = (
1541
+ torch.tensor(lyric_token_idx)
1542
+ .unsqueeze(0)
1543
+ .to(self.device)
1544
+ .repeat(batch_size, 1)
1545
+ )
1546
+ lyric_mask = (
1547
+ torch.tensor(lyric_mask)
1548
+ .unsqueeze(0)
1549
+ .to(self.device)
1550
+ .repeat(batch_size, 1)
1551
+ )
1552
+
1553
+ if audio_duration <= 0:
1554
+ audio_duration = random.uniform(30.0, 240.0)
1555
+ logger.info(f"random audio duration: {audio_duration}")
1556
+
1557
+ end_time = time.time()
1558
+ preprocess_time_cost = end_time - start_time
1559
+ start_time = end_time
1560
+
1561
+ add_retake_noise = task in ("retake", "repaint", "extend")
1562
+ # retake equal to repaint
1563
+ if task == "retake":
1564
+ repaint_start = 0
1565
+ repaint_end = audio_duration
1566
+
1567
+ src_latents = None
1568
+ if src_audio_path is not None:
1569
+ assert src_audio_path is not None and task in (
1570
+ "repaint",
1571
+ "edit",
1572
+ "extend",
1573
+ ), "src_audio_path is required for retake/repaint/extend task"
1574
+ assert os.path.exists(
1575
+ src_audio_path
1576
+ ), f"src_audio_path {src_audio_path} does not exist"
1577
+ src_latents = self.infer_latents(src_audio_path)
1578
+
1579
+ ref_latents = None
1580
+ if ref_audio_input is not None and audio2audio_enable:
1581
+ assert (
1582
+ ref_audio_input is not None
1583
+ ), "ref_audio_input is required for audio2audio task"
1584
+ assert os.path.exists(
1585
+ ref_audio_input
1586
+ ), f"ref_audio_input {ref_audio_input} does not exist"
1587
+ ref_latents = self.infer_latents(ref_audio_input)
1588
+
1589
+ if task == "edit":
1590
+ texts = [edit_target_prompt]
1591
+ target_encoder_text_hidden_states, target_text_attention_mask = (
1592
+ self.get_text_embeddings(texts, self.device)
1593
+ )
1594
+ target_encoder_text_hidden_states = (
1595
+ target_encoder_text_hidden_states.repeat(batch_size, 1, 1)
1596
+ )
1597
+ target_text_attention_mask = target_text_attention_mask.repeat(
1598
+ batch_size, 1
1599
+ )
1600
+
1601
+ target_lyric_token_idx = (
1602
+ torch.tensor([0]).repeat(batch_size, 1).to(self.device).long()
1603
+ )
1604
+ target_lyric_mask = (
1605
+ torch.tensor([0]).repeat(batch_size, 1).to(self.device).long()
1606
+ )
1607
+ if len(edit_target_lyrics) > 0:
1608
+ target_lyric_token_idx = self.tokenize_lyrics(
1609
+ edit_target_lyrics, debug=True
1610
+ )
1611
+ target_lyric_mask = [1] * len(target_lyric_token_idx)
1612
+ target_lyric_token_idx = (
1613
+ torch.tensor(target_lyric_token_idx)
1614
+ .unsqueeze(0)
1615
+ .to(self.device)
1616
+ .repeat(batch_size, 1)
1617
+ )
1618
+ target_lyric_mask = (
1619
+ torch.tensor(target_lyric_mask)
1620
+ .unsqueeze(0)
1621
+ .to(self.device)
1622
+ .repeat(batch_size, 1)
1623
+ )
1624
+
1625
+ target_speaker_embeds = speaker_embeds.clone()
1626
+
1627
+ target_latents = self.flowedit_diffusion_process(
1628
+ encoder_text_hidden_states=encoder_text_hidden_states,
1629
+ text_attention_mask=text_attention_mask,
1630
+ speaker_embds=speaker_embeds,
1631
+ lyric_token_ids=lyric_token_idx,
1632
+ lyric_mask=lyric_mask,
1633
+ target_encoder_text_hidden_states=target_encoder_text_hidden_states,
1634
+ target_text_attention_mask=target_text_attention_mask,
1635
+ target_speaker_embeds=target_speaker_embeds,
1636
+ target_lyric_token_ids=target_lyric_token_idx,
1637
+ target_lyric_mask=target_lyric_mask,
1638
+ src_latents=src_latents,
1639
+ random_generators=retake_random_generators, # more diversity
1640
+ infer_steps=infer_step,
1641
+ guidance_scale=guidance_scale,
1642
+ n_min=edit_n_min,
1643
+ n_max=edit_n_max,
1644
+ n_avg=edit_n_avg,
1645
+ )
1646
+ else:
1647
+ target_latents = self.text2music_diffusion_process(
1648
+ duration=audio_duration,
1649
+ encoder_text_hidden_states=encoder_text_hidden_states,
1650
+ text_attention_mask=text_attention_mask,
1651
+ speaker_embds=speaker_embeds,
1652
+ lyric_token_ids=lyric_token_idx,
1653
+ lyric_mask=lyric_mask,
1654
+ guidance_scale=guidance_scale,
1655
+ omega_scale=omega_scale,
1656
+ infer_steps=infer_step,
1657
+ random_generators=random_generators,
1658
+ scheduler_type=scheduler_type,
1659
+ cfg_type=cfg_type,
1660
+ guidance_interval=guidance_interval,
1661
+ guidance_interval_decay=guidance_interval_decay,
1662
+ min_guidance_scale=min_guidance_scale,
1663
+ oss_steps=oss_steps,
1664
+ encoder_text_hidden_states_null=encoder_text_hidden_states_null,
1665
+ use_erg_lyric=use_erg_lyric,
1666
+ use_erg_diffusion=use_erg_diffusion,
1667
+ retake_random_generators=retake_random_generators,
1668
+ retake_variance=retake_variance,
1669
+ add_retake_noise=add_retake_noise,
1670
+ guidance_scale_text=guidance_scale_text,
1671
+ guidance_scale_lyric=guidance_scale_lyric,
1672
+ repaint_start=repaint_start,
1673
+ repaint_end=repaint_end,
1674
+ src_latents=src_latents,
1675
+ audio2audio_enable=audio2audio_enable,
1676
+ ref_audio_strength=ref_audio_strength,
1677
+ ref_latents=ref_latents,
1678
+ )
1679
+
1680
+ end_time = time.time()
1681
+ diffusion_time_cost = end_time - start_time
1682
+ start_time = end_time
1683
+
1684
+ output_paths = self.latents2audio(
1685
+ latents=target_latents,
1686
+ target_wav_duration_second=audio_duration,
1687
+ save_path=save_path,
1688
+ format=format,
1689
+ )
1690
+
1691
+ end_time = time.time()
1692
+ latent2audio_time_cost = end_time - start_time
1693
+ timecosts = {
1694
+ "preprocess": preprocess_time_cost,
1695
+ "diffusion": diffusion_time_cost,
1696
+ "latent2audio": latent2audio_time_cost,
1697
+ }
1698
+
1699
+ input_params_json = {
1700
+ "lora_name_or_path": lora_name_or_path,
1701
+ "task": task,
1702
+ "prompt": prompt if task != "edit" else edit_target_prompt,
1703
+ "lyrics": lyrics if task != "edit" else edit_target_lyrics,
1704
+ "audio_duration": audio_duration,
1705
+ "infer_step": infer_step,
1706
+ "guidance_scale": guidance_scale,
1707
+ "scheduler_type": scheduler_type,
1708
+ "cfg_type": cfg_type,
1709
+ "omega_scale": omega_scale,
1710
+ "guidance_interval": guidance_interval,
1711
+ "guidance_interval_decay": guidance_interval_decay,
1712
+ "min_guidance_scale": min_guidance_scale,
1713
+ "use_erg_tag": use_erg_tag,
1714
+ "use_erg_lyric": use_erg_lyric,
1715
+ "use_erg_diffusion": use_erg_diffusion,
1716
+ "oss_steps": oss_steps,
1717
+ "timecosts": timecosts,
1718
+ "actual_seeds": actual_seeds,
1719
+ "retake_seeds": actual_retake_seeds,
1720
+ "retake_variance": retake_variance,
1721
+ "guidance_scale_text": guidance_scale_text,
1722
+ "guidance_scale_lyric": guidance_scale_lyric,
1723
+ "repaint_start": repaint_start,
1724
+ "repaint_end": repaint_end,
1725
+ "edit_n_min": edit_n_min,
1726
+ "edit_n_max": edit_n_max,
1727
+ "edit_n_avg": edit_n_avg,
1728
+ "src_audio_path": src_audio_path,
1729
+ "edit_target_prompt": edit_target_prompt,
1730
+ "edit_target_lyrics": edit_target_lyrics,
1731
+ "audio2audio_enable": audio2audio_enable,
1732
+ "ref_audio_strength": ref_audio_strength,
1733
+ "ref_audio_input": ref_audio_input,
1734
+ }
1735
+ # save input_params_json
1736
+ for output_audio_path in output_paths:
1737
+ input_params_json_save_path = output_audio_path.replace(
1738
+ f".{format}", "_input_params.json"
1739
+ )
1740
+ input_params_json["audio_path"] = output_audio_path
1741
+ with open(input_params_json_save_path, "w", encoding="utf-8") as f:
1742
+ json.dump(input_params_json, f, indent=4, ensure_ascii=False)
1743
+
1744
+ return output_paths + [input_params_json]
requirements.txt CHANGED
@@ -1,2 +1,28 @@
1
- git+https://github.com/TEAMuP-dev/pyharp.git@v0.4.1
 
2
  spaces>=0.51.1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # pyharp v0.4.1
2
+ git+https://github.com/TEAMuP-dev/pyharp.git@9a31acb35563c0bcf5a93f968d8285c7c3180aba
3
  spaces>=0.51.1
4
+ # The authors' Space, less gradio, which the SDK version sets
5
+ datasets==3.4.1
6
+ diffusers==0.32.2
7
+ librosa==0.11.0
8
+ loguru==0.7.3
9
+ matplotlib==3.10.1
10
+ numpy
11
+ pypinyin==0.53.0
12
+ pytorch_lightning==2.5.1
13
+ soundfile==0.13.1
14
+ torch==2.8.0
15
+ torchaudio==2.8.0
16
+ torchvision==0.23
17
+ tqdm
18
+ transformers==4.50.0
19
+ py3langid==0.3.0
20
+ hangul-romanize==0.1.0
21
+ num2words==0.5.14
22
+ spacy==3.8.4
23
+ accelerate==1.6.0
24
+ cutlet
25
+ fugashi[unidic-lite]
26
+ peft
27
+ tensorboard
28
+ tensorboardX
schedulers/scheduling_flow_match_euler_discrete.py ADDED
@@ -0,0 +1,394 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2024 Stability AI, Katherine Crowson and The HuggingFace Team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import math
16
+ from dataclasses import dataclass
17
+ from typing import List, Optional, Tuple, Union
18
+
19
+ import numpy as np
20
+ import torch
21
+
22
+ from diffusers.configuration_utils import ConfigMixin, register_to_config
23
+ from diffusers.utils import BaseOutput, logging
24
+ from diffusers.schedulers.scheduling_utils import SchedulerMixin
25
+
26
+
27
+ logger = logging.get_logger(__name__) # pylint: disable=invalid-name
28
+
29
+
30
+ @dataclass
31
+ class FlowMatchEulerDiscreteSchedulerOutput(BaseOutput):
32
+ """
33
+ Output class for the scheduler's `step` function output.
34
+
35
+ Args:
36
+ prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):
37
+ Computed sample `(x_{t-1})` of previous timestep. `prev_sample` should be used as next model input in the
38
+ denoising loop.
39
+ """
40
+
41
+ prev_sample: torch.FloatTensor
42
+
43
+
44
+ class FlowMatchEulerDiscreteScheduler(SchedulerMixin, ConfigMixin):
45
+ """
46
+ Euler scheduler.
47
+
48
+ This model inherits from [`SchedulerMixin`] and [`ConfigMixin`]. Check the superclass documentation for the generic
49
+ methods the library implements for all schedulers such as loading and saving.
50
+
51
+ Args:
52
+ num_train_timesteps (`int`, defaults to 1000):
53
+ The number of diffusion steps to train the model.
54
+ timestep_spacing (`str`, defaults to `"linspace"`):
55
+ The way the timesteps should be scaled. Refer to Table 2 of the [Common Diffusion Noise Schedules and
56
+ Sample Steps are Flawed](https://huggingface.co/papers/2305.08891) for more information.
57
+ shift (`float`, defaults to 1.0):
58
+ The shift value for the timestep schedule.
59
+ """
60
+
61
+ _compatibles = []
62
+ order = 1
63
+
64
+ @register_to_config
65
+ def __init__(
66
+ self,
67
+ num_train_timesteps: int = 1000,
68
+ shift: float = 1.0,
69
+ use_dynamic_shifting=False,
70
+ base_shift: Optional[float] = 0.5,
71
+ max_shift: Optional[float] = 1.15,
72
+ base_image_seq_len: Optional[int] = 256,
73
+ max_image_seq_len: Optional[int] = 4096,
74
+ ):
75
+ timesteps = np.linspace(1, num_train_timesteps, num_train_timesteps, dtype=np.float32)[::-1].copy()
76
+ timesteps = torch.from_numpy(timesteps).to(dtype=torch.float32)
77
+
78
+ sigmas = timesteps / num_train_timesteps
79
+ if not use_dynamic_shifting:
80
+ # when use_dynamic_shifting is True, we apply the timestep shifting on the fly based on the image resolution
81
+ sigmas = shift * sigmas / (1 + (shift - 1) * sigmas)
82
+
83
+ self.timesteps = sigmas * num_train_timesteps
84
+
85
+ self._step_index = None
86
+ self._begin_index = None
87
+
88
+ self.sigmas = sigmas.to("cpu") # to avoid too much CPU/GPU communication
89
+ self.sigma_min = self.sigmas[-1].item()
90
+ self.sigma_max = self.sigmas[0].item()
91
+
92
+ @property
93
+ def step_index(self):
94
+ """
95
+ The index counter for current timestep. It will increase 1 after each scheduler step.
96
+ """
97
+ return self._step_index
98
+
99
+ @property
100
+ def begin_index(self):
101
+ """
102
+ The index for the first timestep. It should be set from pipeline with `set_begin_index` method.
103
+ """
104
+ return self._begin_index
105
+
106
+ # Copied from diffusers.schedulers.scheduling_dpmsolver_multistep.DPMSolverMultistepScheduler.set_begin_index
107
+ def set_begin_index(self, begin_index: int = 0):
108
+ """
109
+ Sets the begin index for the scheduler. This function should be run from pipeline before the inference.
110
+
111
+ Args:
112
+ begin_index (`int`):
113
+ The begin index for the scheduler.
114
+ """
115
+ self._begin_index = begin_index
116
+
117
+ def scale_noise(
118
+ self,
119
+ sample: torch.FloatTensor,
120
+ timestep: Union[float, torch.FloatTensor],
121
+ noise: Optional[torch.FloatTensor] = None,
122
+ ) -> torch.FloatTensor:
123
+ """
124
+ Forward process in flow-matching
125
+
126
+ Args:
127
+ sample (`torch.FloatTensor`):
128
+ The input sample.
129
+ timestep (`int`, *optional*):
130
+ The current timestep in the diffusion chain.
131
+
132
+ Returns:
133
+ `torch.FloatTensor`:
134
+ A scaled input sample.
135
+ """
136
+ # Make sure sigmas and timesteps have the same device and dtype as original_samples
137
+ sigmas = self.sigmas.to(device=sample.device, dtype=sample.dtype)
138
+
139
+ if sample.device.type == "mps" and torch.is_floating_point(timestep):
140
+ # mps does not support float64
141
+ schedule_timesteps = self.timesteps.to(sample.device, dtype=torch.float32)
142
+ timestep = timestep.to(sample.device, dtype=torch.float32)
143
+ else:
144
+ schedule_timesteps = self.timesteps.to(sample.device)
145
+ timestep = timestep.to(sample.device)
146
+
147
+ # self.begin_index is None when scheduler is used for training, or pipeline does not implement set_begin_index
148
+ if self.begin_index is None:
149
+ step_indices = [self.index_for_timestep(t, schedule_timesteps) for t in timestep]
150
+ elif self.step_index is not None:
151
+ # add_noise is called after first denoising step (for inpainting)
152
+ step_indices = [self.step_index] * timestep.shape[0]
153
+ else:
154
+ # add noise is called before first denoising step to create initial latent(img2img)
155
+ step_indices = [self.begin_index] * timestep.shape[0]
156
+
157
+ sigma = sigmas[step_indices].flatten()
158
+ while len(sigma.shape) < len(sample.shape):
159
+ sigma = sigma.unsqueeze(-1)
160
+
161
+ sample = sigma * noise + (1.0 - sigma) * sample
162
+
163
+ return sample
164
+
165
+ def _sigma_to_t(self, sigma):
166
+ return sigma * self.config.num_train_timesteps
167
+
168
+ def time_shift(self, mu: float, sigma: float, t: torch.Tensor):
169
+ return math.exp(mu) / (math.exp(mu) + (1 / t - 1) ** sigma)
170
+
171
+ def set_timesteps(
172
+ self,
173
+ num_inference_steps: int = None,
174
+ device: Union[str, torch.device] = None,
175
+ sigmas: Optional[List[float]] = None,
176
+ mu: Optional[float] = None,
177
+ ):
178
+ """
179
+ Sets the discrete timesteps used for the diffusion chain (to be run before inference).
180
+
181
+ Args:
182
+ num_inference_steps (`int`):
183
+ The number of diffusion steps used when generating samples with a pre-trained model.
184
+ device (`str` or `torch.device`, *optional*):
185
+ The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
186
+ """
187
+
188
+ if self.config.use_dynamic_shifting and mu is None:
189
+ raise ValueError(" you have a pass a value for `mu` when `use_dynamic_shifting` is set to be `True`")
190
+
191
+ if sigmas is None:
192
+ self.num_inference_steps = num_inference_steps
193
+ timesteps = np.linspace(
194
+ self._sigma_to_t(self.sigma_max), self._sigma_to_t(self.sigma_min), num_inference_steps
195
+ )
196
+
197
+ sigmas = timesteps / self.config.num_train_timesteps
198
+
199
+ if self.config.use_dynamic_shifting:
200
+ sigmas = self.time_shift(mu, 1.0, sigmas)
201
+ else:
202
+ sigmas = self.config.shift * sigmas / (1 + (self.config.shift - 1) * sigmas)
203
+
204
+ sigmas = torch.from_numpy(sigmas).to(dtype=torch.float32, device=device)
205
+ timesteps = sigmas * self.config.num_train_timesteps
206
+
207
+ self.timesteps = timesteps.to(device=device)
208
+ self.sigmas = torch.cat([sigmas, torch.zeros(1, device=sigmas.device)])
209
+
210
+ self._step_index = None
211
+ self._begin_index = None
212
+
213
+ def index_for_timestep(self, timestep, schedule_timesteps=None):
214
+ if schedule_timesteps is None:
215
+ schedule_timesteps = self.timesteps
216
+
217
+ indices = (schedule_timesteps == timestep).nonzero()
218
+
219
+ # The sigma index that is taken for the **very** first `step`
220
+ # is always the second index (or the last index if there is only 1)
221
+ # This way we can ensure we don't accidentally skip a sigma in
222
+ # case we start in the middle of the denoising schedule (e.g. for image-to-image)
223
+ pos = 1 if len(indices) > 1 else 0
224
+
225
+ return indices[pos].item()
226
+
227
+ def _init_step_index(self, timestep):
228
+ if self.begin_index is None:
229
+ if isinstance(timestep, torch.Tensor):
230
+ timestep = timestep.to(self.timesteps.device)
231
+ self._step_index = self.index_for_timestep(timestep)
232
+ else:
233
+ self._step_index = self._begin_index
234
+
235
+ def step(
236
+ self,
237
+ model_output: torch.FloatTensor,
238
+ timestep: Union[float, torch.FloatTensor],
239
+ sample: torch.FloatTensor,
240
+ s_churn: float = 0.0,
241
+ s_tmin: float = 0.0,
242
+ s_tmax: float = float("inf"),
243
+ s_noise: float = 1.0,
244
+ generator: Optional[torch.Generator] = None,
245
+ return_dict: bool = True,
246
+ omega: Union[float, np.array] = 0.0
247
+ ) -> Union[FlowMatchEulerDiscreteSchedulerOutput, Tuple]:
248
+ """
249
+ Predict the sample from the previous timestep by reversing the SDE. This function propagates the diffusion
250
+ process from the learned model outputs (most often the predicted noise).
251
+
252
+ Args:
253
+ model_output (`torch.FloatTensor`):
254
+ The direct output from learned diffusion model.
255
+ timestep (`float`):
256
+ The current discrete timestep in the diffusion chain.
257
+ sample (`torch.FloatTensor`):
258
+ A current instance of a sample created by the diffusion process.
259
+ s_churn (`float`):
260
+ s_tmin (`float`):
261
+ s_tmax (`float`):
262
+ s_noise (`float`, defaults to 1.0):
263
+ Scaling factor for noise added to the sample.
264
+ generator (`torch.Generator`, *optional*):
265
+ A random number generator.
266
+ return_dict (`bool`):
267
+ Whether or not to return a [`~schedulers.scheduling_euler_discrete.EulerDiscreteSchedulerOutput`] or
268
+ tuple.
269
+
270
+ Returns:
271
+ [`~schedulers.scheduling_euler_discrete.EulerDiscreteSchedulerOutput`] or `tuple`:
272
+ If return_dict is `True`, [`~schedulers.scheduling_euler_discrete.EulerDiscreteSchedulerOutput`] is
273
+ returned, otherwise a tuple is returned where the first element is the sample tensor.
274
+ """
275
+
276
+ def logistic_function(x, L=0.9, U=1.1, x_0=0.0, k=1):
277
+ # L = Lower bound
278
+ # U = Upper bound
279
+ # x_0 = Midpoint (x corresponding to y = 1.0)
280
+ # k = Steepness, can adjust based on preference
281
+
282
+ if isinstance(x, torch.Tensor):
283
+ device_ = x.device
284
+ x = x.to(torch.float).cpu().numpy()
285
+
286
+ new_x = L + (U - L) / (1 + np.exp(-k * (x - x_0)))
287
+
288
+ if isinstance(new_x, np.ndarray):
289
+ new_x = torch.from_numpy(new_x).to(device_)
290
+ return new_x
291
+
292
+ self.omega_bef_rescale = omega
293
+ omega = logistic_function(omega, k=0.1)
294
+ self.omega_aft_rescale = omega
295
+
296
+ if (
297
+ isinstance(timestep, int)
298
+ or isinstance(timestep, torch.IntTensor)
299
+ or isinstance(timestep, torch.LongTensor)
300
+ ):
301
+ raise ValueError(
302
+ (
303
+ "Passing integer indices (e.g. from `enumerate(timesteps)`) as timesteps to"
304
+ " `EulerDiscreteScheduler.step()` is not supported. Make sure to pass"
305
+ " one of the `scheduler.timesteps` as a timestep."
306
+ ),
307
+ )
308
+
309
+ if self.step_index is None:
310
+ self._init_step_index(timestep)
311
+
312
+ # Upcast to avoid precision issues when computing prev_sample
313
+ sample = sample.to(torch.float32)
314
+
315
+ sigma = self.sigmas[self.step_index]
316
+ sigma_next = self.sigmas[self.step_index + 1]
317
+
318
+ ## --
319
+ ## mean shift 1
320
+ dx = (sigma_next - sigma) * model_output
321
+ m = dx.mean()
322
+ # print(dx.shape) # torch.Size([1, 16, 128, 128])
323
+ # print(f'm: {m}') # m: -0.0014209747314453125
324
+ # raise NotImplementedError
325
+ dx_ = (dx - m) * omega + m
326
+ prev_sample = sample + dx_
327
+
328
+ # ## --
329
+ # ## mean shift 2
330
+ # m = model_output.mean()
331
+ # model_output_ = (model_output - m) * omega + m
332
+ # prev_sample = sample + (sigma_next - sigma) * model_output_
333
+
334
+ # ## --
335
+ # ## original
336
+ # prev_sample = sample + (sigma_next - sigma) * model_output * omega
337
+
338
+ # ## --
339
+ # ## spatial mean 1
340
+ # dx = (sigma_next - sigma) * model_output
341
+ # m = dx.mean(dim=(0, 1), keepdim=True)
342
+ # # print(dx.shape) # torch.Size([1, 16, 128, 128])
343
+ # # print(m.shape) # torch.Size([1, 1, 128, 128])
344
+ # # raise NotImplementedError
345
+ # dx_ = (dx - m) * omega + m
346
+ # prev_sample = sample + dx_
347
+
348
+ # ## --
349
+ # ## spatial mean 2
350
+ # m = model_output.mean(dim=(0, 1), keepdim=True)
351
+ # model_output_ = (model_output - m) * omega + m
352
+ # prev_sample = sample + (sigma_next - sigma) * model_output_
353
+
354
+ # ## --
355
+ # ## channel mean 1
356
+ # m = model_output.mean(dim=(2, 3), keepdim=True)
357
+ # # print(m.shape) # torch.Size([1, 16, 1, 1])
358
+ # model_output_ = (model_output - m) * omega + m
359
+ # prev_sample = sample + (sigma_next - sigma) * model_output_
360
+
361
+ # ## --
362
+ # ## channel mean 2
363
+ # dx = (sigma_next - sigma) * model_output
364
+ # m = dx.mean(dim=(2, 3), keepdim=True)
365
+ # # print(m.shape) # torch.Size([1, 16, 1, 1])
366
+ # dx_ = (dx - m) * omega + m
367
+ # prev_sample = sample + dx_
368
+
369
+ # ## --
370
+ # ## keep sample mean
371
+ # m_tgt = sample.mean()
372
+ # prev_sample_ = sample + (sigma_next - sigma) * model_output * omega
373
+ # m_src = prev_sample_.mean()
374
+ # prev_sample = prev_sample_ - m_src + m_tgt
375
+
376
+ # ## --
377
+ # ## test
378
+ # # print(sample.mean())
379
+ # prev_sample = sample + (sigma_next - sigma) * model_output * omega
380
+ # # raise NotImplementedError
381
+
382
+ # Cast sample back to model compatible dtype
383
+ prev_sample = prev_sample.to(model_output.dtype)
384
+
385
+ # upon completion increase step index by one
386
+ self._step_index += 1
387
+
388
+ if not return_dict:
389
+ return (prev_sample,)
390
+
391
+ return FlowMatchEulerDiscreteSchedulerOutput(prev_sample=prev_sample)
392
+
393
+ def __len__(self):
394
+ return self.config.num_train_timesteps
schedulers/scheduling_flow_match_heun_discrete.py ADDED
@@ -0,0 +1,348 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2024 Stability AI, Katherine Crowson and The HuggingFace Team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from dataclasses import dataclass
16
+ from typing import Optional, Tuple, Union
17
+
18
+ import numpy as np
19
+ import torch
20
+
21
+ from diffusers.configuration_utils import ConfigMixin, register_to_config
22
+ from diffusers.utils import BaseOutput, logging
23
+ from diffusers.utils.torch_utils import randn_tensor
24
+ from diffusers.schedulers.scheduling_utils import SchedulerMixin
25
+
26
+
27
+ logger = logging.get_logger(__name__) # pylint: disable=invalid-name
28
+
29
+
30
+ @dataclass
31
+ class FlowMatchHeunDiscreteSchedulerOutput(BaseOutput):
32
+ """
33
+ Output class for the scheduler's `step` function output.
34
+
35
+ Args:
36
+ prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):
37
+ Computed sample `(x_{t-1})` of previous timestep. `prev_sample` should be used as next model input in the
38
+ denoising loop.
39
+ """
40
+
41
+ prev_sample: torch.FloatTensor
42
+
43
+
44
+ class FlowMatchHeunDiscreteScheduler(SchedulerMixin, ConfigMixin):
45
+ """
46
+ Heun scheduler.
47
+
48
+ This model inherits from [`SchedulerMixin`] and [`ConfigMixin`]. Check the superclass documentation for the generic
49
+ methods the library implements for all schedulers such as loading and saving.
50
+
51
+ Args:
52
+ num_train_timesteps (`int`, defaults to 1000):
53
+ The number of diffusion steps to train the model.
54
+ timestep_spacing (`str`, defaults to `"linspace"`):
55
+ The way the timesteps should be scaled. Refer to Table 2 of the [Common Diffusion Noise Schedules and
56
+ Sample Steps are Flawed](https://huggingface.co/papers/2305.08891) for more information.
57
+ shift (`float`, defaults to 1.0):
58
+ The shift value for the timestep schedule.
59
+ """
60
+
61
+ _compatibles = []
62
+ order = 2
63
+
64
+ @register_to_config
65
+ def __init__(
66
+ self,
67
+ num_train_timesteps: int = 1000,
68
+ shift: float = 1.0,
69
+ ):
70
+ timesteps = np.linspace(1, num_train_timesteps, num_train_timesteps, dtype=np.float32)[::-1].copy()
71
+ timesteps = torch.from_numpy(timesteps).to(dtype=torch.float32)
72
+
73
+ sigmas = timesteps / num_train_timesteps
74
+ sigmas = shift * sigmas / (1 + (shift - 1) * sigmas)
75
+
76
+ self.timesteps = sigmas * num_train_timesteps
77
+
78
+ self._step_index = None
79
+ self._begin_index = None
80
+
81
+ self.sigmas = sigmas.to("cpu") # to avoid too much CPU/GPU communication
82
+ self.sigma_min = self.sigmas[-1].item()
83
+ self.sigma_max = self.sigmas[0].item()
84
+
85
+ @property
86
+ def step_index(self):
87
+ """
88
+ The index counter for current timestep. It will increase 1 after each scheduler step.
89
+ """
90
+ return self._step_index
91
+
92
+ @property
93
+ def begin_index(self):
94
+ """
95
+ The index for the first timestep. It should be set from pipeline with `set_begin_index` method.
96
+ """
97
+ return self._begin_index
98
+
99
+ # Copied from diffusers.schedulers.scheduling_dpmsolver_multistep.DPMSolverMultistepScheduler.set_begin_index
100
+ def set_begin_index(self, begin_index: int = 0):
101
+ """
102
+ Sets the begin index for the scheduler. This function should be run from pipeline before the inference.
103
+
104
+ Args:
105
+ begin_index (`int`):
106
+ The begin index for the scheduler.
107
+ """
108
+ self._begin_index = begin_index
109
+
110
+ def scale_noise(
111
+ self,
112
+ sample: torch.FloatTensor,
113
+ timestep: Union[float, torch.FloatTensor],
114
+ noise: Optional[torch.FloatTensor] = None,
115
+ ) -> torch.FloatTensor:
116
+ """
117
+ Forward process in flow-matching
118
+
119
+ Args:
120
+ sample (`torch.FloatTensor`):
121
+ The input sample.
122
+ timestep (`int`, *optional*):
123
+ The current timestep in the diffusion chain.
124
+
125
+ Returns:
126
+ `torch.FloatTensor`:
127
+ A scaled input sample.
128
+ """
129
+ if self.step_index is None:
130
+ self._init_step_index(timestep)
131
+
132
+ sigma = self.sigmas[self.step_index]
133
+ sample = sigma * noise + (1.0 - sigma) * sample
134
+
135
+ return sample
136
+
137
+ def _sigma_to_t(self, sigma):
138
+ return sigma * self.config.num_train_timesteps
139
+
140
+ def set_timesteps(self, num_inference_steps: int, device: Union[str, torch.device] = None):
141
+ """
142
+ Sets the discrete timesteps used for the diffusion chain (to be run before inference).
143
+
144
+ Args:
145
+ num_inference_steps (`int`):
146
+ The number of diffusion steps used when generating samples with a pre-trained model.
147
+ device (`str` or `torch.device`, *optional*):
148
+ The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
149
+ """
150
+ self.num_inference_steps = num_inference_steps
151
+
152
+ timesteps = np.linspace(
153
+ self._sigma_to_t(self.sigma_max), self._sigma_to_t(self.sigma_min), num_inference_steps
154
+ )
155
+
156
+ sigmas = timesteps / self.config.num_train_timesteps
157
+ sigmas = self.config.shift * sigmas / (1 + (self.config.shift - 1) * sigmas)
158
+ sigmas = torch.from_numpy(sigmas).to(dtype=torch.float32, device=device)
159
+
160
+ timesteps = sigmas * self.config.num_train_timesteps
161
+ timesteps = torch.cat([timesteps[:1], timesteps[1:].repeat_interleave(2)])
162
+ self.timesteps = timesteps.to(device=device)
163
+
164
+ sigmas = torch.cat([sigmas, torch.zeros(1, device=sigmas.device)])
165
+ self.sigmas = torch.cat([sigmas[:1], sigmas[1:-1].repeat_interleave(2), sigmas[-1:]])
166
+
167
+ # empty dt and derivative
168
+ self.prev_derivative = None
169
+ self.dt = None
170
+
171
+ self._step_index = None
172
+ self._begin_index = None
173
+
174
+ def index_for_timestep(self, timestep, schedule_timesteps=None):
175
+ if schedule_timesteps is None:
176
+ schedule_timesteps = self.timesteps
177
+
178
+ indices = (schedule_timesteps == timestep).nonzero()
179
+
180
+ # The sigma index that is taken for the **very** first `step`
181
+ # is always the second index (or the last index if there is only 1)
182
+ # This way we can ensure we don't accidentally skip a sigma in
183
+ # case we start in the middle of the denoising schedule (e.g. for image-to-image)
184
+ pos = 1 if len(indices) > 1 else 0
185
+
186
+ return indices[pos].item()
187
+
188
+ def _init_step_index(self, timestep):
189
+ if self.begin_index is None:
190
+ if isinstance(timestep, torch.Tensor):
191
+ timestep = timestep.to(self.timesteps.device)
192
+ self._step_index = self.index_for_timestep(timestep)
193
+ else:
194
+ self._step_index = self._begin_index
195
+
196
+ @property
197
+ def state_in_first_order(self):
198
+ return self.dt is None
199
+
200
+ def step(
201
+ self,
202
+ model_output: torch.FloatTensor,
203
+ timestep: Union[float, torch.FloatTensor],
204
+ sample: torch.FloatTensor,
205
+ s_churn: float = 0.0,
206
+ s_tmin: float = 0.0,
207
+ s_tmax: float = float("inf"),
208
+ s_noise: float = 1.0,
209
+ generator: Optional[torch.Generator] = None,
210
+ return_dict: bool = True,
211
+ omega: Union[float, np.array] = 0.0
212
+ ) -> Union[FlowMatchHeunDiscreteSchedulerOutput, Tuple]:
213
+ """
214
+ Predict the sample from the previous timestep by reversing the SDE. This function propagates the diffusion
215
+ process from the learned model outputs (most often the predicted noise).
216
+
217
+ Args:
218
+ model_output (`torch.FloatTensor`):
219
+ The direct output from learned diffusion model.
220
+ timestep (`float`):
221
+ The current discrete timestep in the diffusion chain.
222
+ sample (`torch.FloatTensor`):
223
+ A current instance of a sample created by the diffusion process.
224
+ s_churn (`float`):
225
+ s_tmin (`float`):
226
+ s_tmax (`float`):
227
+ s_noise (`float`, defaults to 1.0):
228
+ Scaling factor for noise added to the sample.
229
+ generator (`torch.Generator`, *optional*):
230
+ A random number generator.
231
+ return_dict (`bool`):
232
+ Whether or not to return a [`~schedulers.scheduling_Heun_discrete.HeunDiscreteSchedulerOutput`] or
233
+ tuple.
234
+
235
+ Returns:
236
+ [`~schedulers.scheduling_Heun_discrete.HeunDiscreteSchedulerOutput`] or `tuple`:
237
+ If return_dict is `True`, [`~schedulers.scheduling_Heun_discrete.HeunDiscreteSchedulerOutput`] is
238
+ returned, otherwise a tuple is returned where the first element is the sample tensor.
239
+ """
240
+
241
+ def logistic_function(x, L=0.9, U=1.1, x_0=0.0, k=1):
242
+ # L = Lower bound
243
+ # U = Upper bound
244
+ # x_0 = Midpoint (x corresponding to y = 1.0)
245
+ # k = Steepness, can adjust based on preference
246
+
247
+ if isinstance(x, torch.Tensor):
248
+ device_ = x.device
249
+ x = x.to(torch.float).cpu().numpy()
250
+
251
+ new_x = L + (U - L) / (1 + np.exp(-k * (x - x_0)))
252
+
253
+ if isinstance(new_x, np.ndarray):
254
+ new_x = torch.from_numpy(new_x).to(device_)
255
+ return new_x
256
+
257
+ self.omega_bef_rescale = omega
258
+ omega = logistic_function(omega, k=0.1)
259
+ self.omega_aft_rescale = omega
260
+
261
+ if (
262
+ isinstance(timestep, int)
263
+ or isinstance(timestep, torch.IntTensor)
264
+ or isinstance(timestep, torch.LongTensor)
265
+ ):
266
+ raise ValueError(
267
+ (
268
+ "Passing integer indices (e.g. from `enumerate(timesteps)`) as timesteps to"
269
+ " `HeunDiscreteScheduler.step()` is not supported. Make sure to pass"
270
+ " one of the `scheduler.timesteps` as a timestep."
271
+ ),
272
+ )
273
+
274
+ if self.step_index is None:
275
+ self._init_step_index(timestep)
276
+
277
+ # Upcast to avoid precision issues when computing prev_sample
278
+ sample = sample.to(torch.float32)
279
+
280
+ if self.state_in_first_order:
281
+ sigma = self.sigmas[self.step_index]
282
+ sigma_next = self.sigmas[self.step_index + 1]
283
+ else:
284
+ # 2nd order / Heun's method
285
+ sigma = self.sigmas[self.step_index - 1]
286
+ sigma_next = self.sigmas[self.step_index]
287
+
288
+ gamma = min(s_churn / (len(self.sigmas) - 1), 2**0.5 - 1) if s_tmin <= sigma <= s_tmax else 0.0
289
+
290
+ sigma_hat = sigma * (gamma + 1)
291
+
292
+ if gamma > 0:
293
+ noise = randn_tensor(
294
+ model_output.shape, dtype=model_output.dtype, device=model_output.device, generator=generator
295
+ )
296
+ eps = noise * s_noise
297
+ sample = sample + eps * (sigma_hat**2 - sigma**2) ** 0.5
298
+
299
+ if self.state_in_first_order:
300
+ # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
301
+ denoised = sample - model_output * sigma
302
+ # 2. convert to an ODE derivative for 1st order
303
+ derivative = (sample - denoised) / sigma_hat
304
+ # 3. Delta timestep
305
+ dt = sigma_next - sigma_hat
306
+
307
+ # store for 2nd order step
308
+ self.prev_derivative = derivative
309
+ self.dt = dt
310
+ self.sample = sample
311
+ else:
312
+ # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
313
+ denoised = sample - model_output * sigma_next
314
+ # 2. 2nd order / Heun's method
315
+ derivative = (sample - denoised) / sigma_next
316
+ derivative = 0.5 * (self.prev_derivative + derivative)
317
+
318
+ # 3. take prev timestep & sample
319
+ dt = self.dt
320
+ sample = self.sample
321
+
322
+ # free dt and derivative
323
+ # Note, this puts the scheduler in "first order mode"
324
+ self.prev_derivative = None
325
+ self.dt = None
326
+ self.sample = None
327
+
328
+ # original sample way
329
+ # prev_sample = sample + derivative * dt
330
+
331
+ dx = derivative * dt
332
+ m = dx.mean()
333
+ dx_ = (dx - m) * omega + m
334
+ prev_sample = sample + dx_
335
+
336
+ # Cast sample back to model compatible dtype
337
+ prev_sample = prev_sample.to(model_output.dtype)
338
+
339
+ # upon completion increase step index by one
340
+ self._step_index += 1
341
+
342
+ if not return_dict:
343
+ return (prev_sample,)
344
+
345
+ return FlowMatchHeunDiscreteSchedulerOutput(prev_sample=prev_sample)
346
+
347
+ def __len__(self):
348
+ return self.config.num_train_timesteps