videopython-chatterbox 0.1.7.post1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- chatterbox/__init__.py +14 -0
- chatterbox/models/__init__.py +0 -0
- chatterbox/models/s3gen/__init__.py +2 -0
- chatterbox/models/s3gen/configs.py +10 -0
- chatterbox/models/s3gen/const.py +2 -0
- chatterbox/models/s3gen/decoder.py +333 -0
- chatterbox/models/s3gen/f0_predictor.py +55 -0
- chatterbox/models/s3gen/flow.py +198 -0
- chatterbox/models/s3gen/flow_matching.py +246 -0
- chatterbox/models/s3gen/hifigan.py +474 -0
- chatterbox/models/s3gen/matcha/decoder.py +443 -0
- chatterbox/models/s3gen/matcha/flow_matching.py +129 -0
- chatterbox/models/s3gen/matcha/text_encoder.py +413 -0
- chatterbox/models/s3gen/matcha/transformer.py +316 -0
- chatterbox/models/s3gen/s3gen.py +362 -0
- chatterbox/models/s3gen/transformer/__init__.py +0 -0
- chatterbox/models/s3gen/transformer/activation.py +84 -0
- chatterbox/models/s3gen/transformer/attention.py +330 -0
- chatterbox/models/s3gen/transformer/convolution.py +145 -0
- chatterbox/models/s3gen/transformer/embedding.py +294 -0
- chatterbox/models/s3gen/transformer/encoder_layer.py +236 -0
- chatterbox/models/s3gen/transformer/positionwise_feed_forward.py +115 -0
- chatterbox/models/s3gen/transformer/subsampling.py +383 -0
- chatterbox/models/s3gen/transformer/upsample_encoder.py +318 -0
- chatterbox/models/s3gen/utils/class_utils.py +71 -0
- chatterbox/models/s3gen/utils/intmeanflow.py +36 -0
- chatterbox/models/s3gen/utils/mask.py +194 -0
- chatterbox/models/s3gen/utils/mel.py +85 -0
- chatterbox/models/s3gen/xvector.py +428 -0
- chatterbox/models/s3tokenizer/__init__.py +30 -0
- chatterbox/models/s3tokenizer/s3tokenizer.py +168 -0
- chatterbox/models/t3/__init__.py +1 -0
- chatterbox/models/t3/inference/alignment_stream_analyzer.py +181 -0
- chatterbox/models/t3/inference/t3_hf_backend.py +116 -0
- chatterbox/models/t3/llama_configs.py +73 -0
- chatterbox/models/t3/modules/cond_enc.py +97 -0
- chatterbox/models/t3/modules/learned_pos_emb.py +32 -0
- chatterbox/models/t3/modules/perceiver.py +212 -0
- chatterbox/models/t3/modules/t3_config.py +41 -0
- chatterbox/models/t3/t3.py +490 -0
- chatterbox/models/tokenizers/__init__.py +1 -0
- chatterbox/models/tokenizers/tokenizer.py +313 -0
- chatterbox/models/utils.py +4 -0
- chatterbox/models/voice_encoder/__init__.py +1 -0
- chatterbox/models/voice_encoder/config.py +18 -0
- chatterbox/models/voice_encoder/melspec.py +78 -0
- chatterbox/models/voice_encoder/voice_encoder.py +274 -0
- chatterbox/mtl_tts.py +317 -0
- chatterbox/tts.py +272 -0
- chatterbox/tts_turbo.py +296 -0
- chatterbox/vc.py +104 -0
- videopython_chatterbox-0.1.7.post1.dist-info/METADATA +154 -0
- videopython_chatterbox-0.1.7.post1.dist-info/RECORD +56 -0
- videopython_chatterbox-0.1.7.post1.dist-info/WHEEL +4 -0
- videopython_chatterbox-0.1.7.post1.dist-info/licenses/LICENSE +21 -0
- videopython_chatterbox-0.1.7.post1.dist-info/licenses/NOTICE +29 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
# Copyright (c) 2024 Alibaba Inc (authors: Xiang Lyu, Zhihao Du)
|
|
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
|
+
import threading
|
|
15
|
+
import torch
|
|
16
|
+
import torch.nn.functional as F
|
|
17
|
+
from .matcha.flow_matching import BASECFM
|
|
18
|
+
from .configs import CFM_PARAMS
|
|
19
|
+
from tqdm import tqdm
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def cast_all(*args, dtype):
|
|
23
|
+
return [a if (not a.dtype.is_floating_point) or a.dtype == dtype else a.to(dtype) for a in args]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class ConditionalCFM(BASECFM):
|
|
27
|
+
def __init__(self, in_channels, cfm_params, n_spks=1, spk_emb_dim=64, estimator: torch.nn.Module = None):
|
|
28
|
+
super().__init__(
|
|
29
|
+
n_feats=in_channels,
|
|
30
|
+
cfm_params=cfm_params,
|
|
31
|
+
n_spks=n_spks,
|
|
32
|
+
spk_emb_dim=spk_emb_dim,
|
|
33
|
+
)
|
|
34
|
+
self.t_scheduler = cfm_params.t_scheduler
|
|
35
|
+
self.training_cfg_rate = cfm_params.training_cfg_rate
|
|
36
|
+
self.inference_cfg_rate = cfm_params.inference_cfg_rate
|
|
37
|
+
in_channels = in_channels + (spk_emb_dim if n_spks > 0 else 0)
|
|
38
|
+
# Just change the architecture of the estimator here
|
|
39
|
+
self.estimator = estimator
|
|
40
|
+
|
|
41
|
+
@torch.inference_mode()
|
|
42
|
+
def forward(self, mu, mask, n_timesteps, temperature=1.0, spks=None, cond=None, prompt_len=0, flow_cache=torch.zeros(1, 80, 0, 2)):
|
|
43
|
+
"""Forward diffusion
|
|
44
|
+
|
|
45
|
+
Args:
|
|
46
|
+
mu (torch.Tensor): output of encoder
|
|
47
|
+
shape: (batch_size, n_feats, mel_timesteps)
|
|
48
|
+
mask (torch.Tensor): output_mask
|
|
49
|
+
shape: (batch_size, 1, mel_timesteps)
|
|
50
|
+
n_timesteps (int): number of diffusion steps
|
|
51
|
+
temperature (float, optional): temperature for scaling noise. Defaults to 1.0.
|
|
52
|
+
spks (torch.Tensor, optional): speaker ids. Defaults to None.
|
|
53
|
+
shape: (batch_size, spk_emb_dim)
|
|
54
|
+
cond: Not used but kept for future purposes
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
sample: generated mel-spectrogram
|
|
58
|
+
shape: (batch_size, n_feats, mel_timesteps)
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
raise NotImplementedError("unused, needs updating for meanflow model")
|
|
62
|
+
|
|
63
|
+
z = torch.randn_like(mu).to(mu.device).to(mu.dtype) * temperature
|
|
64
|
+
cache_size = flow_cache.shape[2]
|
|
65
|
+
# fix prompt and overlap part mu and z
|
|
66
|
+
if cache_size != 0:
|
|
67
|
+
z[:, :, :cache_size] = flow_cache[:, :, :, 0]
|
|
68
|
+
mu[:, :, :cache_size] = flow_cache[:, :, :, 1]
|
|
69
|
+
z_cache = torch.concat([z[:, :, :prompt_len], z[:, :, -34:]], dim=2)
|
|
70
|
+
mu_cache = torch.concat([mu[:, :, :prompt_len], mu[:, :, -34:]], dim=2)
|
|
71
|
+
flow_cache = torch.stack([z_cache, mu_cache], dim=-1)
|
|
72
|
+
|
|
73
|
+
t_span = torch.linspace(0, 1, n_timesteps + 1, device=mu.device, dtype=mu.dtype)
|
|
74
|
+
if self.t_scheduler == 'cosine':
|
|
75
|
+
t_span = 1 - torch.cos(t_span * 0.5 * torch.pi)
|
|
76
|
+
return self.solve_euler(z, t_span=t_span, mu=mu, mask=mask, spks=spks, cond=cond), flow_cache
|
|
77
|
+
|
|
78
|
+
def solve_euler(self, x, t_span, mu, mask, spks, cond, meanflow=False):
|
|
79
|
+
"""
|
|
80
|
+
Fixed euler solver for ODEs.
|
|
81
|
+
Args:
|
|
82
|
+
x (torch.Tensor): random noise
|
|
83
|
+
t_span (torch.Tensor): n_timesteps interpolated
|
|
84
|
+
shape: (n_timesteps + 1,)
|
|
85
|
+
mu (torch.Tensor): output of encoder
|
|
86
|
+
shape: (batch_size, n_feats, mel_timesteps)
|
|
87
|
+
mask (torch.Tensor): output_mask
|
|
88
|
+
shape: (batch_size, 1, mel_timesteps)
|
|
89
|
+
spks (torch.Tensor, optional): speaker ids. Defaults to None.
|
|
90
|
+
shape: (batch_size, spk_emb_dim)
|
|
91
|
+
cond: Not used but kept for future purposes
|
|
92
|
+
meanflow: meanflow mode
|
|
93
|
+
"""
|
|
94
|
+
in_dtype = x.dtype
|
|
95
|
+
x, t_span, mu, mask, spks, cond = cast_all(x, t_span, mu, mask, spks, cond, dtype=self.estimator.dtype)
|
|
96
|
+
|
|
97
|
+
# Duplicated batch dims are for CFG
|
|
98
|
+
# Do not use concat, it may cause memory format changed and trt infer with wrong results!
|
|
99
|
+
B, T = mu.size(0), x.size(2)
|
|
100
|
+
x_in = torch.zeros([2 * B, 80, T], device=x.device, dtype=x.dtype)
|
|
101
|
+
mask_in = torch.zeros([2 * B, 1, T], device=x.device, dtype=x.dtype)
|
|
102
|
+
mu_in = torch.zeros([2 * B, 80, T], device=x.device, dtype=x.dtype)
|
|
103
|
+
t_in = torch.zeros([2 * B ], device=x.device, dtype=x.dtype)
|
|
104
|
+
spks_in = torch.zeros([2 * B, 80 ], device=x.device, dtype=x.dtype)
|
|
105
|
+
cond_in = torch.zeros([2 * B, 80, T], device=x.device, dtype=x.dtype)
|
|
106
|
+
r_in = torch.zeros([2 * B ], device=x.device, dtype=x.dtype) # (only used for meanflow)
|
|
107
|
+
|
|
108
|
+
for t, r in zip(t_span[:-1], t_span[1:]):
|
|
109
|
+
t = t.unsqueeze(dim=0)
|
|
110
|
+
r = r.unsqueeze(dim=0)
|
|
111
|
+
# Shapes:
|
|
112
|
+
# x_in ( 2B, 80, T )
|
|
113
|
+
# mask_in ( 2B, 1, T )
|
|
114
|
+
# mu_in ( 2B, 80, T )
|
|
115
|
+
# t_in ( 2B, )
|
|
116
|
+
# spks_in ( 2B, 80, )
|
|
117
|
+
# cond_in ( 2B, 80, T )
|
|
118
|
+
# r_in ( 2B, )
|
|
119
|
+
# x ( B, 80, T )
|
|
120
|
+
# mask ( B, 1, T )
|
|
121
|
+
# mu ( B, 80, T )
|
|
122
|
+
# t ( B, )
|
|
123
|
+
# spks ( B, 80, )
|
|
124
|
+
# cond ( B, 80, T )
|
|
125
|
+
# r ( B, )
|
|
126
|
+
|
|
127
|
+
x_in[:B] = x_in[B:] = x
|
|
128
|
+
mask_in[:B] = mask_in[B:] = mask
|
|
129
|
+
mu_in[:B] = mu
|
|
130
|
+
t_in[:B] = t_in[B:] = t
|
|
131
|
+
spks_in[:B] = spks
|
|
132
|
+
cond_in[:B] = cond
|
|
133
|
+
r_in[:B] = r_in[B:] = r # (only used for meanflow)
|
|
134
|
+
dxdt = self.estimator.forward(
|
|
135
|
+
x=x_in, mask=mask_in, mu=mu_in, t=t_in, spks=spks_in, cond=cond_in,
|
|
136
|
+
r=r_in if meanflow else None,
|
|
137
|
+
)
|
|
138
|
+
dxdt, cfg_dxdt = torch.split(dxdt, [B, B], dim=0)
|
|
139
|
+
dxdt = ((1.0 + self.inference_cfg_rate) * dxdt - self.inference_cfg_rate * cfg_dxdt)
|
|
140
|
+
dt = r - t
|
|
141
|
+
x = x + dt * dxdt
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
return x.to(in_dtype)
|
|
146
|
+
|
|
147
|
+
def compute_loss(self, x1, mask, mu, spks=None, cond=None):
|
|
148
|
+
"""Computes diffusion loss
|
|
149
|
+
|
|
150
|
+
Args:
|
|
151
|
+
x1 (torch.Tensor): Target
|
|
152
|
+
shape: (batch_size, n_feats, mel_timesteps)
|
|
153
|
+
mask (torch.Tensor): target mask
|
|
154
|
+
shape: (batch_size, 1, mel_timesteps)
|
|
155
|
+
mu (torch.Tensor): output of encoder
|
|
156
|
+
shape: (batch_size, n_feats, mel_timesteps)
|
|
157
|
+
spks (torch.Tensor, optional): speaker embedding. Defaults to None.
|
|
158
|
+
shape: (batch_size, spk_emb_dim)
|
|
159
|
+
|
|
160
|
+
Returns:
|
|
161
|
+
loss: conditional flow matching loss
|
|
162
|
+
y: conditional flow
|
|
163
|
+
shape: (batch_size, n_feats, mel_timesteps)
|
|
164
|
+
"""
|
|
165
|
+
b, _, t = mu.shape
|
|
166
|
+
|
|
167
|
+
# random timestep
|
|
168
|
+
t = torch.rand([b, 1, 1], device=mu.device, dtype=mu.dtype)
|
|
169
|
+
if self.t_scheduler == 'cosine':
|
|
170
|
+
t = 1 - torch.cos(t * 0.5 * torch.pi)
|
|
171
|
+
# sample noise p(x_0)
|
|
172
|
+
z = torch.randn_like(x1)
|
|
173
|
+
|
|
174
|
+
y = (1 - (1 - self.sigma_min) * t) * z + t * x1
|
|
175
|
+
u = x1 - (1 - self.sigma_min) * z
|
|
176
|
+
|
|
177
|
+
# during training, we randomly drop condition to trade off mode coverage and sample fidelity
|
|
178
|
+
if self.training_cfg_rate > 0:
|
|
179
|
+
cfg_mask = torch.rand(b, device=x1.device) > self.training_cfg_rate
|
|
180
|
+
mu = mu * cfg_mask.view(-1, 1, 1)
|
|
181
|
+
spks = spks * cfg_mask.view(-1, 1)
|
|
182
|
+
cond = cond * cfg_mask.view(-1, 1, 1)
|
|
183
|
+
|
|
184
|
+
pred = self.estimator(y, mask, mu, t.squeeze(), spks, cond)
|
|
185
|
+
loss = F.mse_loss(pred * mask, u * mask, reduction="sum") / (torch.sum(mask) * u.shape[1])
|
|
186
|
+
return loss, y
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
class CausalConditionalCFM(ConditionalCFM):
|
|
190
|
+
def __init__(self, in_channels=240, cfm_params=CFM_PARAMS, n_spks=1, spk_emb_dim=80, estimator=None):
|
|
191
|
+
super().__init__(in_channels, cfm_params, n_spks, spk_emb_dim, estimator)
|
|
192
|
+
# TODO: BAD BAD IDEA - IT'LL MESS UP DISTILLATION - SETTING TO NONE
|
|
193
|
+
self.rand_noise = None
|
|
194
|
+
|
|
195
|
+
@torch.inference_mode()
|
|
196
|
+
def forward(self, mu, mask, n_timesteps, temperature=1.0, spks=None, cond=None, noised_mels=None, meanflow=False):
|
|
197
|
+
"""Forward diffusion
|
|
198
|
+
|
|
199
|
+
Args:
|
|
200
|
+
mu (torch.Tensor): output of encoder
|
|
201
|
+
shape: (batch_size, n_feats, mel_timesteps)
|
|
202
|
+
mask (torch.Tensor): output_mask
|
|
203
|
+
shape: (batch_size, 1, mel_timesteps)
|
|
204
|
+
n_timesteps (int): number of diffusion steps
|
|
205
|
+
temperature (float, optional): temperature for scaling noise. Defaults to 1.0.
|
|
206
|
+
spks (torch.Tensor, optional): speaker ids. Defaults to None.
|
|
207
|
+
shape: (batch_size, spk_emb_dim)
|
|
208
|
+
cond: Not used but kept for future purposes
|
|
209
|
+
noised_mels: gt mels noised a time t
|
|
210
|
+
Returns:
|
|
211
|
+
sample: generated mel-spectrogram
|
|
212
|
+
shape: (batch_size, n_feats, mel_timesteps)
|
|
213
|
+
"""
|
|
214
|
+
|
|
215
|
+
B = mu.size(0)
|
|
216
|
+
z = torch.randn_like(mu)
|
|
217
|
+
|
|
218
|
+
if noised_mels is not None:
|
|
219
|
+
prompt_len = mu.size(2) - noised_mels.size(2)
|
|
220
|
+
z[..., prompt_len:] = noised_mels
|
|
221
|
+
|
|
222
|
+
# time steps for reverse diffusion
|
|
223
|
+
t_span = torch.linspace(0, 1, n_timesteps + 1, device=mu.device, dtype=mu.dtype)
|
|
224
|
+
if (not meanflow) and (self.t_scheduler == 'cosine'):
|
|
225
|
+
t_span = 1 - torch.cos(t_span * 0.5 * torch.pi)
|
|
226
|
+
|
|
227
|
+
# NOTE: right now, the only meanflow models are also distilled models, which don't need CFG
|
|
228
|
+
# because they were distilled with CFG outputs. We would need to add another hparam and
|
|
229
|
+
# change the conditional logic here if we want to use CFG inference with a meanflow model.
|
|
230
|
+
if meanflow:
|
|
231
|
+
return self.basic_euler(z, t_span=t_span, mu=mu, mask=mask, spks=spks, cond=cond), None
|
|
232
|
+
|
|
233
|
+
return self.solve_euler(z, t_span=t_span, mu=mu, mask=mask, spks=spks, cond=cond, meanflow=meanflow), None
|
|
234
|
+
|
|
235
|
+
def basic_euler(self, x, t_span, mu, mask, spks, cond):
|
|
236
|
+
in_dtype = x.dtype
|
|
237
|
+
x, t_span, mu, mask, spks, cond = cast_all(x, t_span, mu, mask, spks, cond, dtype=self.estimator.dtype)
|
|
238
|
+
|
|
239
|
+
print("S3 Token -> Mel Inference...")
|
|
240
|
+
for t, r in tqdm(zip(t_span[..., :-1], t_span[..., 1:]), total=t_span.shape[-1] - 1):
|
|
241
|
+
t, r = t[None], r[None]
|
|
242
|
+
dxdt = self.estimator.forward(x, mask=mask, mu=mu, t=t, spks=spks, cond=cond, r=r)
|
|
243
|
+
dt = r - t
|
|
244
|
+
x = x + dt * dxdt
|
|
245
|
+
|
|
246
|
+
return x.to(in_dtype)
|