Upload model
Browse files- README.md +199 -0
- activation.py +428 -0
- attentions.py +709 -0
- cleaner.py +59 -0
- commons.py +189 -0
- config.json +387 -0
- configuration_gpt_sovits.py +25 -0
- core_vq.py +383 -0
- embedding.py +81 -0
- lr_schedulers.py +82 -0
- mel_processing.py +142 -0
- model_utils.py +228 -0
- modeling_gpt_sovits.py +648 -0
- models.py +991 -0
- modules.py +923 -0
- mrte_model.py +192 -0
- my_utils.py +21 -0
- optim.py +622 -0
- patched_mha_with_cache.py +465 -0
- pytorch_model.bin +3 -0
- quantize.py +118 -0
- scaling.py +332 -0
- symbols.py +412 -0
- t2s_lightning_module.py +140 -0
- t2s_model.py +445 -0
- transformer.py +378 -0
- transforms.py +209 -0
README.md
ADDED
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
library_name: transformers
|
3 |
+
tags: []
|
4 |
+
---
|
5 |
+
|
6 |
+
# Model Card for Model ID
|
7 |
+
|
8 |
+
<!-- Provide a quick summary of what the model is/does. -->
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
## Model Details
|
13 |
+
|
14 |
+
### Model Description
|
15 |
+
|
16 |
+
<!-- Provide a longer summary of what this model is. -->
|
17 |
+
|
18 |
+
This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
|
19 |
+
|
20 |
+
- **Developed by:** [More Information Needed]
|
21 |
+
- **Funded by [optional]:** [More Information Needed]
|
22 |
+
- **Shared by [optional]:** [More Information Needed]
|
23 |
+
- **Model type:** [More Information Needed]
|
24 |
+
- **Language(s) (NLP):** [More Information Needed]
|
25 |
+
- **License:** [More Information Needed]
|
26 |
+
- **Finetuned from model [optional]:** [More Information Needed]
|
27 |
+
|
28 |
+
### Model Sources [optional]
|
29 |
+
|
30 |
+
<!-- Provide the basic links for the model. -->
|
31 |
+
|
32 |
+
- **Repository:** [More Information Needed]
|
33 |
+
- **Paper [optional]:** [More Information Needed]
|
34 |
+
- **Demo [optional]:** [More Information Needed]
|
35 |
+
|
36 |
+
## Uses
|
37 |
+
|
38 |
+
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
39 |
+
|
40 |
+
### Direct Use
|
41 |
+
|
42 |
+
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
43 |
+
|
44 |
+
[More Information Needed]
|
45 |
+
|
46 |
+
### Downstream Use [optional]
|
47 |
+
|
48 |
+
<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
|
49 |
+
|
50 |
+
[More Information Needed]
|
51 |
+
|
52 |
+
### Out-of-Scope Use
|
53 |
+
|
54 |
+
<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
|
55 |
+
|
56 |
+
[More Information Needed]
|
57 |
+
|
58 |
+
## Bias, Risks, and Limitations
|
59 |
+
|
60 |
+
<!-- This section is meant to convey both technical and sociotechnical limitations. -->
|
61 |
+
|
62 |
+
[More Information Needed]
|
63 |
+
|
64 |
+
### Recommendations
|
65 |
+
|
66 |
+
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
67 |
+
|
68 |
+
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
69 |
+
|
70 |
+
## How to Get Started with the Model
|
71 |
+
|
72 |
+
Use the code below to get started with the model.
|
73 |
+
|
74 |
+
[More Information Needed]
|
75 |
+
|
76 |
+
## Training Details
|
77 |
+
|
78 |
+
### Training Data
|
79 |
+
|
80 |
+
<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
|
81 |
+
|
82 |
+
[More Information Needed]
|
83 |
+
|
84 |
+
### Training Procedure
|
85 |
+
|
86 |
+
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
87 |
+
|
88 |
+
#### Preprocessing [optional]
|
89 |
+
|
90 |
+
[More Information Needed]
|
91 |
+
|
92 |
+
|
93 |
+
#### Training Hyperparameters
|
94 |
+
|
95 |
+
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
96 |
+
|
97 |
+
#### Speeds, Sizes, Times [optional]
|
98 |
+
|
99 |
+
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
100 |
+
|
101 |
+
[More Information Needed]
|
102 |
+
|
103 |
+
## Evaluation
|
104 |
+
|
105 |
+
<!-- This section describes the evaluation protocols and provides the results. -->
|
106 |
+
|
107 |
+
### Testing Data, Factors & Metrics
|
108 |
+
|
109 |
+
#### Testing Data
|
110 |
+
|
111 |
+
<!-- This should link to a Dataset Card if possible. -->
|
112 |
+
|
113 |
+
[More Information Needed]
|
114 |
+
|
115 |
+
#### Factors
|
116 |
+
|
117 |
+
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
118 |
+
|
119 |
+
[More Information Needed]
|
120 |
+
|
121 |
+
#### Metrics
|
122 |
+
|
123 |
+
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
124 |
+
|
125 |
+
[More Information Needed]
|
126 |
+
|
127 |
+
### Results
|
128 |
+
|
129 |
+
[More Information Needed]
|
130 |
+
|
131 |
+
#### Summary
|
132 |
+
|
133 |
+
|
134 |
+
|
135 |
+
## Model Examination [optional]
|
136 |
+
|
137 |
+
<!-- Relevant interpretability work for the model goes here -->
|
138 |
+
|
139 |
+
[More Information Needed]
|
140 |
+
|
141 |
+
## Environmental Impact
|
142 |
+
|
143 |
+
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
144 |
+
|
145 |
+
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
146 |
+
|
147 |
+
- **Hardware Type:** [More Information Needed]
|
148 |
+
- **Hours used:** [More Information Needed]
|
149 |
+
- **Cloud Provider:** [More Information Needed]
|
150 |
+
- **Compute Region:** [More Information Needed]
|
151 |
+
- **Carbon Emitted:** [More Information Needed]
|
152 |
+
|
153 |
+
## Technical Specifications [optional]
|
154 |
+
|
155 |
+
### Model Architecture and Objective
|
156 |
+
|
157 |
+
[More Information Needed]
|
158 |
+
|
159 |
+
### Compute Infrastructure
|
160 |
+
|
161 |
+
[More Information Needed]
|
162 |
+
|
163 |
+
#### Hardware
|
164 |
+
|
165 |
+
[More Information Needed]
|
166 |
+
|
167 |
+
#### Software
|
168 |
+
|
169 |
+
[More Information Needed]
|
170 |
+
|
171 |
+
## Citation [optional]
|
172 |
+
|
173 |
+
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
174 |
+
|
175 |
+
**BibTeX:**
|
176 |
+
|
177 |
+
[More Information Needed]
|
178 |
+
|
179 |
+
**APA:**
|
180 |
+
|
181 |
+
[More Information Needed]
|
182 |
+
|
183 |
+
## Glossary [optional]
|
184 |
+
|
185 |
+
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
186 |
+
|
187 |
+
[More Information Needed]
|
188 |
+
|
189 |
+
## More Information [optional]
|
190 |
+
|
191 |
+
[More Information Needed]
|
192 |
+
|
193 |
+
## Model Card Authors [optional]
|
194 |
+
|
195 |
+
[More Information Needed]
|
196 |
+
|
197 |
+
## Model Card Contact
|
198 |
+
|
199 |
+
[More Information Needed]
|
activation.py
ADDED
@@ -0,0 +1,428 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# modified from https://github.com/lifeiteng/vall-e/blob/main/valle/modules/activation.py
|
2 |
+
from typing import Optional
|
3 |
+
from typing import Tuple
|
4 |
+
import torch
|
5 |
+
from torch import Tensor
|
6 |
+
from torch.nn import Linear
|
7 |
+
from torch.nn import Module
|
8 |
+
from torch.nn.init import constant_
|
9 |
+
from torch.nn.init import xavier_normal_
|
10 |
+
from torch.nn.init import xavier_uniform_
|
11 |
+
from torch.nn.modules.linear import NonDynamicallyQuantizableLinear
|
12 |
+
from torch.nn.parameter import Parameter
|
13 |
+
|
14 |
+
from torch.nn import functional as F
|
15 |
+
from .patched_mha_with_cache import multi_head_attention_forward_patched
|
16 |
+
|
17 |
+
F.multi_head_attention_forward = multi_head_attention_forward_patched
|
18 |
+
|
19 |
+
|
20 |
+
class MultiheadAttention(Module):
|
21 |
+
r"""Allows the model to jointly attend to information
|
22 |
+
from different representation subspaces as described in the paper:
|
23 |
+
`Attention Is All You Need <https://arxiv.org/abs/1706.03762>`_.
|
24 |
+
|
25 |
+
Multi-Head Attention is defined as:
|
26 |
+
|
27 |
+
.. math::
|
28 |
+
\text{MultiHead}(Q, K, V) = \text{Concat}(head_1,\dots,head_h)W^O
|
29 |
+
|
30 |
+
where :math:`head_i = \text{Attention}(QW_i^Q, KW_i^K, VW_i^V)`.
|
31 |
+
|
32 |
+
``forward()`` will use a special optimized implementation if all of the following
|
33 |
+
conditions are met:
|
34 |
+
|
35 |
+
- self attention is being computed (i.e., ``query``, ``key``, and ``value`` are the same tensor. This
|
36 |
+
restriction will be loosened in the future.)
|
37 |
+
- Either autograd is disabled (using ``torch.inference_mode`` or ``torch.no_grad``) or no tensor argument ``requires_grad``
|
38 |
+
- training is disabled (using ``.eval()``)
|
39 |
+
- dropout is 0
|
40 |
+
- ``add_bias_kv`` is ``False``
|
41 |
+
- ``add_zero_attn`` is ``False``
|
42 |
+
- ``batch_first`` is ``True`` and the input is batched
|
43 |
+
- ``kdim`` and ``vdim`` are equal to ``embed_dim``
|
44 |
+
- at most one of ``key_padding_mask`` or ``attn_mask`` is passed
|
45 |
+
- if a `NestedTensor <https://pytorch.org/docs/stable/nested.html>`_ is passed, neither ``key_padding_mask``
|
46 |
+
nor ``attn_mask`` is passed
|
47 |
+
|
48 |
+
If the optimized implementation is in use, a
|
49 |
+
`NestedTensor <https://pytorch.org/docs/stable/nested.html>`_ can be passed for
|
50 |
+
``query``/``key``/``value`` to represent padding more efficiently than using a
|
51 |
+
padding mask. In this case, a `NestedTensor <https://pytorch.org/docs/stable/nested.html>`_
|
52 |
+
will be returned, and an additional speedup proportional to the fraction of the input
|
53 |
+
that is padding can be expected.
|
54 |
+
|
55 |
+
Args:
|
56 |
+
embed_dim: Total dimension of the model.
|
57 |
+
num_heads: Number of parallel attention heads. Note that ``embed_dim`` will be split
|
58 |
+
across ``num_heads`` (i.e. each head will have dimension ``embed_dim // num_heads``).
|
59 |
+
dropout: Dropout probability on ``attn_output_weights``. Default: ``0.0`` (no dropout).
|
60 |
+
bias: If specified, adds bias to input / output projection layers. Default: ``True``.
|
61 |
+
add_bias_kv: If specified, adds bias to the key and value sequences at dim=0. Default: ``False``.
|
62 |
+
add_zero_attn: If specified, adds a new batch of zeros to the key and value sequences at dim=1.
|
63 |
+
Default: ``False``.
|
64 |
+
kdim: Total number of features for keys. Default: ``None`` (uses ``kdim=embed_dim``).
|
65 |
+
vdim: Total number of features for values. Default: ``None`` (uses ``vdim=embed_dim``).
|
66 |
+
batch_first: If ``True``, then the input and output tensors are provided
|
67 |
+
as (batch, seq, feature). Default: ``False`` (seq, batch, feature).
|
68 |
+
|
69 |
+
Examples::
|
70 |
+
|
71 |
+
>>> # xdoctest: +SKIP
|
72 |
+
>>> multihead_attn = nn.MultiheadAttention(embed_dim, num_heads)
|
73 |
+
>>> attn_output, attn_output_weights = multihead_attn(query, key, value)
|
74 |
+
|
75 |
+
"""
|
76 |
+
__constants__ = ["batch_first"]
|
77 |
+
bias_k: Optional[torch.Tensor]
|
78 |
+
bias_v: Optional[torch.Tensor]
|
79 |
+
|
80 |
+
def __init__(
|
81 |
+
self,
|
82 |
+
embed_dim,
|
83 |
+
num_heads,
|
84 |
+
dropout=0.0,
|
85 |
+
bias=True,
|
86 |
+
add_bias_kv=False,
|
87 |
+
add_zero_attn=False,
|
88 |
+
kdim=None,
|
89 |
+
vdim=None,
|
90 |
+
batch_first=False,
|
91 |
+
linear1_cls=Linear,
|
92 |
+
linear2_cls=Linear,
|
93 |
+
device=None,
|
94 |
+
dtype=None,
|
95 |
+
) -> None:
|
96 |
+
factory_kwargs = {"device": device, "dtype": dtype}
|
97 |
+
super(MultiheadAttention, self).__init__()
|
98 |
+
self.embed_dim = embed_dim
|
99 |
+
self.kdim = kdim if kdim is not None else embed_dim
|
100 |
+
self.vdim = vdim if vdim is not None else embed_dim
|
101 |
+
self._qkv_same_embed_dim = self.kdim == embed_dim and self.vdim == embed_dim
|
102 |
+
|
103 |
+
self.num_heads = num_heads
|
104 |
+
self.dropout = dropout
|
105 |
+
self.batch_first = batch_first
|
106 |
+
self.head_dim = embed_dim // num_heads
|
107 |
+
assert (
|
108 |
+
self.head_dim * num_heads == self.embed_dim
|
109 |
+
), "embed_dim must be divisible by num_heads"
|
110 |
+
|
111 |
+
if add_bias_kv:
|
112 |
+
self.bias_k = Parameter(torch.empty((1, 1, embed_dim), **factory_kwargs))
|
113 |
+
self.bias_v = Parameter(torch.empty((1, 1, embed_dim), **factory_kwargs))
|
114 |
+
else:
|
115 |
+
self.bias_k = self.bias_v = None
|
116 |
+
|
117 |
+
if linear1_cls == Linear:
|
118 |
+
if not self._qkv_same_embed_dim:
|
119 |
+
self.q_proj_weight = Parameter(
|
120 |
+
torch.empty((embed_dim, embed_dim), **factory_kwargs)
|
121 |
+
)
|
122 |
+
self.k_proj_weight = Parameter(
|
123 |
+
torch.empty((embed_dim, self.kdim), **factory_kwargs)
|
124 |
+
)
|
125 |
+
self.v_proj_weight = Parameter(
|
126 |
+
torch.empty((embed_dim, self.vdim), **factory_kwargs)
|
127 |
+
)
|
128 |
+
self.register_parameter("in_proj_weight", None)
|
129 |
+
else:
|
130 |
+
self.in_proj_weight = Parameter(
|
131 |
+
torch.empty((3 * embed_dim, embed_dim), **factory_kwargs)
|
132 |
+
)
|
133 |
+
self.register_parameter("q_proj_weight", None)
|
134 |
+
self.register_parameter("k_proj_weight", None)
|
135 |
+
self.register_parameter("v_proj_weight", None)
|
136 |
+
|
137 |
+
if bias:
|
138 |
+
self.in_proj_bias = Parameter(
|
139 |
+
torch.empty(3 * embed_dim, **factory_kwargs)
|
140 |
+
)
|
141 |
+
else:
|
142 |
+
self.register_parameter("in_proj_bias", None)
|
143 |
+
self.out_proj = NonDynamicallyQuantizableLinear(
|
144 |
+
embed_dim, embed_dim, bias=bias, **factory_kwargs
|
145 |
+
)
|
146 |
+
|
147 |
+
self._reset_parameters()
|
148 |
+
else:
|
149 |
+
if not self._qkv_same_embed_dim:
|
150 |
+
raise NotImplementedError
|
151 |
+
else:
|
152 |
+
self.in_proj_linear = linear1_cls(
|
153 |
+
embed_dim, 3 * embed_dim, bias=bias, **factory_kwargs
|
154 |
+
)
|
155 |
+
self.in_proj_weight = self.in_proj_linear.weight
|
156 |
+
|
157 |
+
self.register_parameter("q_proj_weight", None)
|
158 |
+
self.register_parameter("k_proj_weight", None)
|
159 |
+
self.register_parameter("v_proj_weight", None)
|
160 |
+
|
161 |
+
if bias:
|
162 |
+
self.in_proj_bias = self.in_proj_linear.bias
|
163 |
+
else:
|
164 |
+
self.register_parameter("in_proj_bias", None)
|
165 |
+
|
166 |
+
self.out_proj = linear2_cls(
|
167 |
+
embed_dim, embed_dim, bias=bias, **factory_kwargs
|
168 |
+
)
|
169 |
+
|
170 |
+
if self.bias_k is not None:
|
171 |
+
xavier_normal_(self.bias_k)
|
172 |
+
if self.bias_v is not None:
|
173 |
+
xavier_normal_(self.bias_v)
|
174 |
+
|
175 |
+
self.add_zero_attn = add_zero_attn
|
176 |
+
|
177 |
+
def _reset_parameters(self):
|
178 |
+
if self._qkv_same_embed_dim:
|
179 |
+
xavier_uniform_(self.in_proj_weight)
|
180 |
+
else:
|
181 |
+
xavier_uniform_(self.q_proj_weight)
|
182 |
+
xavier_uniform_(self.k_proj_weight)
|
183 |
+
xavier_uniform_(self.v_proj_weight)
|
184 |
+
|
185 |
+
if self.in_proj_bias is not None:
|
186 |
+
constant_(self.in_proj_bias, 0.0)
|
187 |
+
constant_(self.out_proj.bias, 0.0)
|
188 |
+
|
189 |
+
if self.bias_k is not None:
|
190 |
+
xavier_normal_(self.bias_k)
|
191 |
+
if self.bias_v is not None:
|
192 |
+
xavier_normal_(self.bias_v)
|
193 |
+
|
194 |
+
def __setstate__(self, state):
|
195 |
+
# Support loading old MultiheadAttention checkpoints generated by v1.1.0
|
196 |
+
if "_qkv_same_embed_dim" not in state:
|
197 |
+
state["_qkv_same_embed_dim"] = True
|
198 |
+
|
199 |
+
super(MultiheadAttention, self).__setstate__(state)
|
200 |
+
|
201 |
+
def forward(
|
202 |
+
self,
|
203 |
+
query: Tensor,
|
204 |
+
key: Tensor,
|
205 |
+
value: Tensor,
|
206 |
+
key_padding_mask: Optional[Tensor] = None,
|
207 |
+
need_weights: bool = True,
|
208 |
+
attn_mask: Optional[Tensor] = None,
|
209 |
+
average_attn_weights: bool = True,
|
210 |
+
cache=None,
|
211 |
+
) -> Tuple[Tensor, Optional[Tensor]]:
|
212 |
+
r"""
|
213 |
+
Args:
|
214 |
+
query: Query embeddings of shape :math:`(L, E_q)` for unbatched input, :math:`(L, N, E_q)` when ``batch_first=False``
|
215 |
+
or :math:`(N, L, E_q)` when ``batch_first=True``, where :math:`L` is the target sequence length,
|
216 |
+
:math:`N` is the batch size, and :math:`E_q` is the query embedding dimension ``embed_dim``.
|
217 |
+
Queries are compared against key-value pairs to produce the output.
|
218 |
+
See "Attention Is All You Need" for more details.
|
219 |
+
key: Key embeddings of shape :math:`(S, E_k)` for unbatched input, :math:`(S, N, E_k)` when ``batch_first=False``
|
220 |
+
or :math:`(N, S, E_k)` when ``batch_first=True``, where :math:`S` is the source sequence length,
|
221 |
+
:math:`N` is the batch size, and :math:`E_k` is the key embedding dimension ``kdim``.
|
222 |
+
See "Attention Is All You Need" for more details.
|
223 |
+
value: Value embeddings of shape :math:`(S, E_v)` for unbatched input, :math:`(S, N, E_v)` when
|
224 |
+
``batch_first=False`` or :math:`(N, S, E_v)` when ``batch_first=True``, where :math:`S` is the source
|
225 |
+
sequence length, :math:`N` is the batch size, and :math:`E_v` is the value embedding dimension ``vdim``.
|
226 |
+
See "Attention Is All You Need" for more details.
|
227 |
+
key_padding_mask: If specified, a mask of shape :math:`(N, S)` indicating which elements within ``key``
|
228 |
+
to ignore for the purpose of attention (i.e. treat as "padding"). For unbatched `query`, shape should be :math:`(S)`.
|
229 |
+
Binary and byte masks are supported.
|
230 |
+
For a binary mask, a ``True`` value indicates that the corresponding ``key`` value will be ignored for
|
231 |
+
the purpose of attention. For a float mask, it will be directly added to the corresponding ``key`` value.
|
232 |
+
need_weights: If specified, returns ``attn_output_weights`` in addition to ``attn_outputs``.
|
233 |
+
Default: ``True``.
|
234 |
+
attn_mask: If specified, a 2D or 3D mask preventing attention to certain positions. Must be of shape
|
235 |
+
:math:`(L, S)` or :math:`(N\cdot\text{num\_heads}, L, S)`, where :math:`N` is the batch size,
|
236 |
+
:math:`L` is the target sequence length, and :math:`S` is the source sequence length. A 2D mask will be
|
237 |
+
broadcasted across the batch while a 3D mask allows for a different mask for each entry in the batch.
|
238 |
+
Binary, byte, and float masks are supported. For a binary mask, a ``True`` value indicates that the
|
239 |
+
corresponding position is not allowed to attend. For a byte mask, a non-zero value indicates that the
|
240 |
+
corresponding position is not allowed to attend. For a float mask, the mask values will be added to
|
241 |
+
the attention weight.
|
242 |
+
average_attn_weights: If true, indicates that the returned ``attn_weights`` should be averaged across
|
243 |
+
heads. Otherwise, ``attn_weights`` are provided separately per head. Note that this flag only has an
|
244 |
+
effect when ``need_weights=True``. Default: ``True`` (i.e. average weights across heads)
|
245 |
+
|
246 |
+
Outputs:
|
247 |
+
- **attn_output** - Attention outputs of shape :math:`(L, E)` when input is unbatched,
|
248 |
+
:math:`(L, N, E)` when ``batch_first=False`` or :math:`(N, L, E)` when ``batch_first=True``,
|
249 |
+
where :math:`L` is the target sequence length, :math:`N` is the batch size, and :math:`E` is the
|
250 |
+
embedding dimension ``embed_dim``.
|
251 |
+
- **attn_output_weights** - Only returned when ``need_weights=True``. If ``average_attn_weights=True``,
|
252 |
+
returns attention weights averaged across heads of shape :math:`(L, S)` when input is unbatched or
|
253 |
+
:math:`(N, L, S)`, where :math:`N` is the batch size, :math:`L` is the target sequence length, and
|
254 |
+
:math:`S` is the source sequence length. If ``average_attn_weights=False``, returns attention weights per
|
255 |
+
head of shape :math:`(\text{num\_heads}, L, S)` when input is unbatched or :math:`(N, \text{num\_heads}, L, S)`.
|
256 |
+
|
257 |
+
.. note::
|
258 |
+
`batch_first` argument is ignored for unbatched inputs.
|
259 |
+
"""
|
260 |
+
is_batched = query.dim() == 3
|
261 |
+
if key_padding_mask is not None:
|
262 |
+
_kpm_dtype = key_padding_mask.dtype
|
263 |
+
if _kpm_dtype != torch.bool and not torch.is_floating_point(
|
264 |
+
key_padding_mask
|
265 |
+
):
|
266 |
+
raise AssertionError(
|
267 |
+
"only bool and floating types of key_padding_mask are supported"
|
268 |
+
)
|
269 |
+
why_not_fast_path = ""
|
270 |
+
if not is_batched:
|
271 |
+
why_not_fast_path = (
|
272 |
+
f"input not batched; expected query.dim() of 3 but got {query.dim()}"
|
273 |
+
)
|
274 |
+
elif query is not key or key is not value:
|
275 |
+
# When lifting this restriction, don't forget to either
|
276 |
+
# enforce that the dtypes all match or test cases where
|
277 |
+
# they don't!
|
278 |
+
why_not_fast_path = "non-self attention was used (query, key, and value are not the same Tensor)"
|
279 |
+
elif self.in_proj_bias is not None and query.dtype != self.in_proj_bias.dtype:
|
280 |
+
why_not_fast_path = f"dtypes of query ({query.dtype}) and self.in_proj_bias ({self.in_proj_bias.dtype}) don't match"
|
281 |
+
elif (
|
282 |
+
self.in_proj_weight is not None and query.dtype != self.in_proj_weight.dtype
|
283 |
+
):
|
284 |
+
# this case will fail anyway, but at least they'll get a useful error message.
|
285 |
+
why_not_fast_path = f"dtypes of query ({query.dtype}) and self.in_proj_weight ({self.in_proj_weight.dtype}) don't match"
|
286 |
+
elif self.training:
|
287 |
+
why_not_fast_path = "training is enabled"
|
288 |
+
elif not self.batch_first:
|
289 |
+
why_not_fast_path = "batch_first was not True"
|
290 |
+
elif self.bias_k is not None:
|
291 |
+
why_not_fast_path = "self.bias_k was not None"
|
292 |
+
elif self.bias_v is not None:
|
293 |
+
why_not_fast_path = "self.bias_v was not None"
|
294 |
+
elif self.dropout:
|
295 |
+
why_not_fast_path = f"dropout was {self.dropout}, required zero"
|
296 |
+
elif self.add_zero_attn:
|
297 |
+
why_not_fast_path = "add_zero_attn was enabled"
|
298 |
+
elif not self._qkv_same_embed_dim:
|
299 |
+
why_not_fast_path = "_qkv_same_embed_dim was not True"
|
300 |
+
elif attn_mask is not None:
|
301 |
+
why_not_fast_path = "attn_mask was not None"
|
302 |
+
elif query.is_nested and key_padding_mask is not None:
|
303 |
+
why_not_fast_path = (
|
304 |
+
"key_padding_mask is not supported with NestedTensor input"
|
305 |
+
)
|
306 |
+
elif self.num_heads % 2 == 1:
|
307 |
+
why_not_fast_path = "num_heads is odd"
|
308 |
+
elif torch.is_autocast_enabled():
|
309 |
+
why_not_fast_path = "autocast is enabled"
|
310 |
+
|
311 |
+
if not why_not_fast_path:
|
312 |
+
tensor_args = (
|
313 |
+
query,
|
314 |
+
key,
|
315 |
+
value,
|
316 |
+
self.in_proj_weight,
|
317 |
+
self.in_proj_bias,
|
318 |
+
self.out_proj.weight,
|
319 |
+
self.out_proj.bias,
|
320 |
+
)
|
321 |
+
# We have to use list comprehensions below because TorchScript does not support
|
322 |
+
# generator expressions.
|
323 |
+
if torch.overrides.has_torch_function(tensor_args):
|
324 |
+
why_not_fast_path = "some Tensor argument has_torch_function"
|
325 |
+
elif not all(
|
326 |
+
[
|
327 |
+
(x is None or x.is_cuda or "cpu" in str(x.device))
|
328 |
+
for x in tensor_args
|
329 |
+
]
|
330 |
+
):
|
331 |
+
why_not_fast_path = "some Tensor argument is neither CUDA nor CPU"
|
332 |
+
elif torch.is_grad_enabled() and any(
|
333 |
+
[x is not None and x.requires_grad for x in tensor_args]
|
334 |
+
):
|
335 |
+
why_not_fast_path = (
|
336 |
+
"grad is enabled and at least one of query or the "
|
337 |
+
"input/output projection weights or biases requires_grad"
|
338 |
+
)
|
339 |
+
if not why_not_fast_path:
|
340 |
+
return torch._native_multi_head_attention(
|
341 |
+
query,
|
342 |
+
key,
|
343 |
+
value,
|
344 |
+
self.embed_dim,
|
345 |
+
self.num_heads,
|
346 |
+
self.in_proj_weight,
|
347 |
+
self.in_proj_bias,
|
348 |
+
self.out_proj.weight,
|
349 |
+
self.out_proj.bias,
|
350 |
+
key_padding_mask if key_padding_mask is not None else attn_mask,
|
351 |
+
need_weights,
|
352 |
+
average_attn_weights,
|
353 |
+
1
|
354 |
+
if key_padding_mask is not None
|
355 |
+
else 0
|
356 |
+
if attn_mask is not None
|
357 |
+
else None,
|
358 |
+
)
|
359 |
+
|
360 |
+
any_nested = query.is_nested or key.is_nested or value.is_nested
|
361 |
+
assert not any_nested, (
|
362 |
+
"MultiheadAttention does not support NestedTensor outside of its fast path. "
|
363 |
+
+ f"The fast path was not hit because {why_not_fast_path}"
|
364 |
+
)
|
365 |
+
|
366 |
+
if self.batch_first and is_batched:
|
367 |
+
# make sure that the transpose op does not affect the "is" property
|
368 |
+
if key is value:
|
369 |
+
if query is key:
|
370 |
+
query = key = value = query.transpose(1, 0)
|
371 |
+
else:
|
372 |
+
query, key = [x.transpose(1, 0) for x in (query, key)]
|
373 |
+
value = key
|
374 |
+
else:
|
375 |
+
query, key, value = [x.transpose(1, 0) for x in (query, key, value)]
|
376 |
+
|
377 |
+
if not self._qkv_same_embed_dim:
|
378 |
+
attn_output, attn_output_weights = F.multi_head_attention_forward(
|
379 |
+
query,
|
380 |
+
key,
|
381 |
+
value,
|
382 |
+
self.embed_dim,
|
383 |
+
self.num_heads,
|
384 |
+
self.in_proj_weight,
|
385 |
+
self.in_proj_bias,
|
386 |
+
self.bias_k,
|
387 |
+
self.bias_v,
|
388 |
+
self.add_zero_attn,
|
389 |
+
self.dropout,
|
390 |
+
self.out_proj.weight,
|
391 |
+
self.out_proj.bias,
|
392 |
+
training=self.training,
|
393 |
+
key_padding_mask=key_padding_mask,
|
394 |
+
need_weights=need_weights,
|
395 |
+
attn_mask=attn_mask,
|
396 |
+
use_separate_proj_weight=True,
|
397 |
+
q_proj_weight=self.q_proj_weight,
|
398 |
+
k_proj_weight=self.k_proj_weight,
|
399 |
+
v_proj_weight=self.v_proj_weight,
|
400 |
+
average_attn_weights=average_attn_weights,
|
401 |
+
cache=cache,
|
402 |
+
)
|
403 |
+
else:
|
404 |
+
attn_output, attn_output_weights = F.multi_head_attention_forward(
|
405 |
+
query,
|
406 |
+
key,
|
407 |
+
value,
|
408 |
+
self.embed_dim,
|
409 |
+
self.num_heads,
|
410 |
+
self.in_proj_weight,
|
411 |
+
self.in_proj_bias,
|
412 |
+
self.bias_k,
|
413 |
+
self.bias_v,
|
414 |
+
self.add_zero_attn,
|
415 |
+
self.dropout,
|
416 |
+
self.out_proj.weight,
|
417 |
+
self.out_proj.bias,
|
418 |
+
training=self.training,
|
419 |
+
key_padding_mask=key_padding_mask,
|
420 |
+
need_weights=need_weights,
|
421 |
+
attn_mask=attn_mask,
|
422 |
+
average_attn_weights=average_attn_weights,
|
423 |
+
cache=cache,
|
424 |
+
)
|
425 |
+
if self.batch_first and is_batched:
|
426 |
+
return attn_output.transpose(1, 0), attn_output_weights
|
427 |
+
else:
|
428 |
+
return attn_output, attn_output_weights
|
attentions.py
ADDED
@@ -0,0 +1,709 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import torch
|
3 |
+
from torch import nn
|
4 |
+
from torch.nn import functional as F
|
5 |
+
|
6 |
+
from . import commons
|
7 |
+
from .modules import LayerNorm
|
8 |
+
|
9 |
+
|
10 |
+
class Encoder(nn.Module):
|
11 |
+
def __init__(
|
12 |
+
self,
|
13 |
+
hidden_channels,
|
14 |
+
filter_channels,
|
15 |
+
n_heads,
|
16 |
+
n_layers,
|
17 |
+
kernel_size=1,
|
18 |
+
p_dropout=0.0,
|
19 |
+
window_size=4,
|
20 |
+
isflow=False,
|
21 |
+
**kwargs
|
22 |
+
):
|
23 |
+
super().__init__()
|
24 |
+
self.hidden_channels = hidden_channels
|
25 |
+
self.filter_channels = filter_channels
|
26 |
+
self.n_heads = n_heads
|
27 |
+
self.n_layers = n_layers
|
28 |
+
self.kernel_size = kernel_size
|
29 |
+
self.p_dropout = p_dropout
|
30 |
+
self.window_size = window_size
|
31 |
+
|
32 |
+
self.drop = nn.Dropout(p_dropout)
|
33 |
+
self.attn_layers = nn.ModuleList()
|
34 |
+
self.norm_layers_1 = nn.ModuleList()
|
35 |
+
self.ffn_layers = nn.ModuleList()
|
36 |
+
self.norm_layers_2 = nn.ModuleList()
|
37 |
+
for i in range(self.n_layers):
|
38 |
+
self.attn_layers.append(
|
39 |
+
MultiHeadAttention(
|
40 |
+
hidden_channels,
|
41 |
+
hidden_channels,
|
42 |
+
n_heads,
|
43 |
+
p_dropout=p_dropout,
|
44 |
+
window_size=window_size,
|
45 |
+
)
|
46 |
+
)
|
47 |
+
self.norm_layers_1.append(LayerNorm(hidden_channels))
|
48 |
+
self.ffn_layers.append(
|
49 |
+
FFN(
|
50 |
+
hidden_channels,
|
51 |
+
hidden_channels,
|
52 |
+
filter_channels,
|
53 |
+
kernel_size,
|
54 |
+
p_dropout=p_dropout,
|
55 |
+
)
|
56 |
+
)
|
57 |
+
self.norm_layers_2.append(LayerNorm(hidden_channels))
|
58 |
+
if isflow:
|
59 |
+
cond_layer = torch.nn.Conv1d(
|
60 |
+
kwargs["gin_channels"], 2 * hidden_channels * n_layers, 1
|
61 |
+
)
|
62 |
+
self.cond_pre = torch.nn.Conv1d(hidden_channels, 2 * hidden_channels, 1)
|
63 |
+
self.cond_layer = weight_norm_modules(cond_layer, name="weight")
|
64 |
+
self.gin_channels = kwargs["gin_channels"]
|
65 |
+
|
66 |
+
def forward(self, x, x_mask, g=None):
|
67 |
+
attn_mask = x_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
|
68 |
+
x = x * x_mask
|
69 |
+
if g is not None:
|
70 |
+
g = self.cond_layer(g)
|
71 |
+
|
72 |
+
for i in range(self.n_layers):
|
73 |
+
if g is not None:
|
74 |
+
x = self.cond_pre(x)
|
75 |
+
cond_offset = i * 2 * self.hidden_channels
|
76 |
+
g_l = g[:, cond_offset : cond_offset + 2 * self.hidden_channels, :]
|
77 |
+
x = commons.fused_add_tanh_sigmoid_multiply(
|
78 |
+
x, g_l, torch.IntTensor([self.hidden_channels])
|
79 |
+
)
|
80 |
+
y = self.attn_layers[i](x, x, attn_mask)
|
81 |
+
y = self.drop(y)
|
82 |
+
x = self.norm_layers_1[i](x + y)
|
83 |
+
|
84 |
+
y = self.ffn_layers[i](x, x_mask)
|
85 |
+
y = self.drop(y)
|
86 |
+
x = self.norm_layers_2[i](x + y)
|
87 |
+
x = x * x_mask
|
88 |
+
return x
|
89 |
+
|
90 |
+
|
91 |
+
class Decoder(nn.Module):
|
92 |
+
def __init__(
|
93 |
+
self,
|
94 |
+
hidden_channels,
|
95 |
+
filter_channels,
|
96 |
+
n_heads,
|
97 |
+
n_layers,
|
98 |
+
kernel_size=1,
|
99 |
+
p_dropout=0.0,
|
100 |
+
proximal_bias=False,
|
101 |
+
proximal_init=True,
|
102 |
+
**kwargs
|
103 |
+
):
|
104 |
+
super().__init__()
|
105 |
+
self.hidden_channels = hidden_channels
|
106 |
+
self.filter_channels = filter_channels
|
107 |
+
self.n_heads = n_heads
|
108 |
+
self.n_layers = n_layers
|
109 |
+
self.kernel_size = kernel_size
|
110 |
+
self.p_dropout = p_dropout
|
111 |
+
self.proximal_bias = proximal_bias
|
112 |
+
self.proximal_init = proximal_init
|
113 |
+
|
114 |
+
self.drop = nn.Dropout(p_dropout)
|
115 |
+
self.self_attn_layers = nn.ModuleList()
|
116 |
+
self.norm_layers_0 = nn.ModuleList()
|
117 |
+
self.encdec_attn_layers = nn.ModuleList()
|
118 |
+
self.norm_layers_1 = nn.ModuleList()
|
119 |
+
self.ffn_layers = nn.ModuleList()
|
120 |
+
self.norm_layers_2 = nn.ModuleList()
|
121 |
+
for i in range(self.n_layers):
|
122 |
+
self.self_attn_layers.append(
|
123 |
+
MultiHeadAttention(
|
124 |
+
hidden_channels,
|
125 |
+
hidden_channels,
|
126 |
+
n_heads,
|
127 |
+
p_dropout=p_dropout,
|
128 |
+
proximal_bias=proximal_bias,
|
129 |
+
proximal_init=proximal_init,
|
130 |
+
)
|
131 |
+
)
|
132 |
+
self.norm_layers_0.append(LayerNorm(hidden_channels))
|
133 |
+
self.encdec_attn_layers.append(
|
134 |
+
MultiHeadAttention(
|
135 |
+
hidden_channels, hidden_channels, n_heads, p_dropout=p_dropout
|
136 |
+
)
|
137 |
+
)
|
138 |
+
self.norm_layers_1.append(LayerNorm(hidden_channels))
|
139 |
+
self.ffn_layers.append(
|
140 |
+
FFN(
|
141 |
+
hidden_channels,
|
142 |
+
hidden_channels,
|
143 |
+
filter_channels,
|
144 |
+
kernel_size,
|
145 |
+
p_dropout=p_dropout,
|
146 |
+
causal=True,
|
147 |
+
)
|
148 |
+
)
|
149 |
+
self.norm_layers_2.append(LayerNorm(hidden_channels))
|
150 |
+
|
151 |
+
def forward(self, x, x_mask, h, h_mask):
|
152 |
+
"""
|
153 |
+
x: decoder input
|
154 |
+
h: encoder output
|
155 |
+
"""
|
156 |
+
self_attn_mask = commons.subsequent_mask(x_mask.size(2)).to(
|
157 |
+
device=x.device, dtype=x.dtype
|
158 |
+
)
|
159 |
+
encdec_attn_mask = h_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
|
160 |
+
x = x * x_mask
|
161 |
+
for i in range(self.n_layers):
|
162 |
+
y = self.self_attn_layers[i](x, x, self_attn_mask)
|
163 |
+
y = self.drop(y)
|
164 |
+
x = self.norm_layers_0[i](x + y)
|
165 |
+
|
166 |
+
y = self.encdec_attn_layers[i](x, h, encdec_attn_mask)
|
167 |
+
y = self.drop(y)
|
168 |
+
x = self.norm_layers_1[i](x + y)
|
169 |
+
|
170 |
+
y = self.ffn_layers[i](x, x_mask)
|
171 |
+
y = self.drop(y)
|
172 |
+
x = self.norm_layers_2[i](x + y)
|
173 |
+
x = x * x_mask
|
174 |
+
return x
|
175 |
+
|
176 |
+
|
177 |
+
class MultiHeadAttention(nn.Module):
|
178 |
+
def __init__(
|
179 |
+
self,
|
180 |
+
channels,
|
181 |
+
out_channels,
|
182 |
+
n_heads,
|
183 |
+
p_dropout=0.0,
|
184 |
+
window_size=None,
|
185 |
+
heads_share=True,
|
186 |
+
block_length=None,
|
187 |
+
proximal_bias=False,
|
188 |
+
proximal_init=False,
|
189 |
+
):
|
190 |
+
super().__init__()
|
191 |
+
assert channels % n_heads == 0
|
192 |
+
|
193 |
+
self.channels = channels
|
194 |
+
self.out_channels = out_channels
|
195 |
+
self.n_heads = n_heads
|
196 |
+
self.p_dropout = p_dropout
|
197 |
+
self.window_size = window_size
|
198 |
+
self.heads_share = heads_share
|
199 |
+
self.block_length = block_length
|
200 |
+
self.proximal_bias = proximal_bias
|
201 |
+
self.proximal_init = proximal_init
|
202 |
+
self.attn = None
|
203 |
+
|
204 |
+
self.k_channels = channels // n_heads
|
205 |
+
self.conv_q = nn.Conv1d(channels, channels, 1)
|
206 |
+
self.conv_k = nn.Conv1d(channels, channels, 1)
|
207 |
+
self.conv_v = nn.Conv1d(channels, channels, 1)
|
208 |
+
self.conv_o = nn.Conv1d(channels, out_channels, 1)
|
209 |
+
self.drop = nn.Dropout(p_dropout)
|
210 |
+
|
211 |
+
if window_size is not None:
|
212 |
+
n_heads_rel = 1 if heads_share else n_heads
|
213 |
+
rel_stddev = self.k_channels**-0.5
|
214 |
+
self.emb_rel_k = nn.Parameter(
|
215 |
+
torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels)
|
216 |
+
* rel_stddev
|
217 |
+
)
|
218 |
+
self.emb_rel_v = nn.Parameter(
|
219 |
+
torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels)
|
220 |
+
* rel_stddev
|
221 |
+
)
|
222 |
+
|
223 |
+
nn.init.xavier_uniform_(self.conv_q.weight)
|
224 |
+
nn.init.xavier_uniform_(self.conv_k.weight)
|
225 |
+
nn.init.xavier_uniform_(self.conv_v.weight)
|
226 |
+
if proximal_init:
|
227 |
+
with torch.no_grad():
|
228 |
+
self.conv_k.weight.copy_(self.conv_q.weight)
|
229 |
+
self.conv_k.bias.copy_(self.conv_q.bias)
|
230 |
+
|
231 |
+
def forward(self, x, c, attn_mask=None):
|
232 |
+
q = self.conv_q(x)
|
233 |
+
k = self.conv_k(c)
|
234 |
+
v = self.conv_v(c)
|
235 |
+
|
236 |
+
x, self.attn = self.attention(q, k, v, mask=attn_mask)
|
237 |
+
|
238 |
+
x = self.conv_o(x)
|
239 |
+
return x
|
240 |
+
|
241 |
+
def attention(self, query, key, value, mask=None):
|
242 |
+
# reshape [b, d, t] -> [b, n_h, t, d_k]
|
243 |
+
b, d, t_s, t_t = (*key.size(), query.size(2))
|
244 |
+
query = query.view(b, self.n_heads, self.k_channels, t_t).transpose(2, 3)
|
245 |
+
key = key.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
|
246 |
+
value = value.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
|
247 |
+
|
248 |
+
scores = torch.matmul(query / math.sqrt(self.k_channels), key.transpose(-2, -1))
|
249 |
+
if self.window_size is not None:
|
250 |
+
assert (
|
251 |
+
t_s == t_t
|
252 |
+
), "Relative attention is only available for self-attention."
|
253 |
+
key_relative_embeddings = self._get_relative_embeddings(self.emb_rel_k, t_s)
|
254 |
+
rel_logits = self._matmul_with_relative_keys(
|
255 |
+
query / math.sqrt(self.k_channels), key_relative_embeddings
|
256 |
+
)
|
257 |
+
scores_local = self._relative_position_to_absolute_position(rel_logits)
|
258 |
+
scores = scores + scores_local
|
259 |
+
if self.proximal_bias:
|
260 |
+
assert t_s == t_t, "Proximal bias is only available for self-attention."
|
261 |
+
scores = scores + self._attention_bias_proximal(t_s).to(
|
262 |
+
device=scores.device, dtype=scores.dtype
|
263 |
+
)
|
264 |
+
if mask is not None:
|
265 |
+
scores = scores.masked_fill(mask == 0, -1e4)
|
266 |
+
if self.block_length is not None:
|
267 |
+
assert (
|
268 |
+
t_s == t_t
|
269 |
+
), "Local attention is only available for self-attention."
|
270 |
+
block_mask = (
|
271 |
+
torch.ones_like(scores)
|
272 |
+
.triu(-self.block_length)
|
273 |
+
.tril(self.block_length)
|
274 |
+
)
|
275 |
+
scores = scores.masked_fill(block_mask == 0, -1e4)
|
276 |
+
p_attn = F.softmax(scores, dim=-1) # [b, n_h, t_t, t_s]
|
277 |
+
p_attn = self.drop(p_attn)
|
278 |
+
output = torch.matmul(p_attn, value)
|
279 |
+
if self.window_size is not None:
|
280 |
+
relative_weights = self._absolute_position_to_relative_position(p_attn)
|
281 |
+
value_relative_embeddings = self._get_relative_embeddings(
|
282 |
+
self.emb_rel_v, t_s
|
283 |
+
)
|
284 |
+
output = output + self._matmul_with_relative_values(
|
285 |
+
relative_weights, value_relative_embeddings
|
286 |
+
)
|
287 |
+
output = (
|
288 |
+
output.transpose(2, 3).contiguous().view(b, d, t_t)
|
289 |
+
) # [b, n_h, t_t, d_k] -> [b, d, t_t]
|
290 |
+
return output, p_attn
|
291 |
+
|
292 |
+
def _matmul_with_relative_values(self, x, y):
|
293 |
+
"""
|
294 |
+
x: [b, h, l, m]
|
295 |
+
y: [h or 1, m, d]
|
296 |
+
ret: [b, h, l, d]
|
297 |
+
"""
|
298 |
+
ret = torch.matmul(x, y.unsqueeze(0))
|
299 |
+
return ret
|
300 |
+
|
301 |
+
def _matmul_with_relative_keys(self, x, y):
|
302 |
+
"""
|
303 |
+
x: [b, h, l, d]
|
304 |
+
y: [h or 1, m, d]
|
305 |
+
ret: [b, h, l, m]
|
306 |
+
"""
|
307 |
+
ret = torch.matmul(x, y.unsqueeze(0).transpose(-2, -1))
|
308 |
+
return ret
|
309 |
+
|
310 |
+
def _get_relative_embeddings(self, relative_embeddings, length):
|
311 |
+
max_relative_position = 2 * self.window_size + 1
|
312 |
+
# Pad first before slice to avoid using cond ops.
|
313 |
+
pad_length = max(length - (self.window_size + 1), 0)
|
314 |
+
slice_start_position = max((self.window_size + 1) - length, 0)
|
315 |
+
slice_end_position = slice_start_position + 2 * length - 1
|
316 |
+
if pad_length > 0:
|
317 |
+
padded_relative_embeddings = F.pad(
|
318 |
+
relative_embeddings,
|
319 |
+
commons.convert_pad_shape([[0, 0], [pad_length, pad_length], [0, 0]]),
|
320 |
+
)
|
321 |
+
else:
|
322 |
+
padded_relative_embeddings = relative_embeddings
|
323 |
+
used_relative_embeddings = padded_relative_embeddings[
|
324 |
+
:, slice_start_position:slice_end_position
|
325 |
+
]
|
326 |
+
return used_relative_embeddings
|
327 |
+
|
328 |
+
def _relative_position_to_absolute_position(self, x):
|
329 |
+
"""
|
330 |
+
x: [b, h, l, 2*l-1]
|
331 |
+
ret: [b, h, l, l]
|
332 |
+
"""
|
333 |
+
batch, heads, length, _ = x.size()
|
334 |
+
# Concat columns of pad to shift from relative to absolute indexing.
|
335 |
+
x = F.pad(x, commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, 1]]))
|
336 |
+
|
337 |
+
# Concat extra elements so to add up to shape (len+1, 2*len-1).
|
338 |
+
x_flat = x.view([batch, heads, length * 2 * length])
|
339 |
+
x_flat = F.pad(
|
340 |
+
x_flat, commons.convert_pad_shape([[0, 0], [0, 0], [0, length - 1]])
|
341 |
+
)
|
342 |
+
|
343 |
+
# Reshape and slice out the padded elements.
|
344 |
+
x_final = x_flat.view([batch, heads, length + 1, 2 * length - 1])[
|
345 |
+
:, :, :length, length - 1 :
|
346 |
+
]
|
347 |
+
return x_final
|
348 |
+
|
349 |
+
def _absolute_position_to_relative_position(self, x):
|
350 |
+
"""
|
351 |
+
x: [b, h, l, l]
|
352 |
+
ret: [b, h, l, 2*l-1]
|
353 |
+
"""
|
354 |
+
batch, heads, length, _ = x.size()
|
355 |
+
# padd along column
|
356 |
+
x = F.pad(
|
357 |
+
x, commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, length - 1]])
|
358 |
+
)
|
359 |
+
x_flat = x.view([batch, heads, length**2 + length * (length - 1)])
|
360 |
+
# add 0's in the beginning that will skew the elements after reshape
|
361 |
+
x_flat = F.pad(x_flat, commons.convert_pad_shape([[0, 0], [0, 0], [length, 0]]))
|
362 |
+
x_final = x_flat.view([batch, heads, length, 2 * length])[:, :, :, 1:]
|
363 |
+
return x_final
|
364 |
+
|
365 |
+
def _attention_bias_proximal(self, length):
|
366 |
+
"""Bias for self-attention to encourage attention to close positions.
|
367 |
+
Args:
|
368 |
+
length: an integer scalar.
|
369 |
+
Returns:
|
370 |
+
a Tensor with shape [1, 1, length, length]
|
371 |
+
"""
|
372 |
+
r = torch.arange(length, dtype=torch.float32)
|
373 |
+
diff = torch.unsqueeze(r, 0) - torch.unsqueeze(r, 1)
|
374 |
+
return torch.unsqueeze(torch.unsqueeze(-torch.log1p(torch.abs(diff)), 0), 0)
|
375 |
+
|
376 |
+
|
377 |
+
class FFN(nn.Module):
|
378 |
+
def __init__(
|
379 |
+
self,
|
380 |
+
in_channels,
|
381 |
+
out_channels,
|
382 |
+
filter_channels,
|
383 |
+
kernel_size,
|
384 |
+
p_dropout=0.0,
|
385 |
+
activation=None,
|
386 |
+
causal=False,
|
387 |
+
):
|
388 |
+
super().__init__()
|
389 |
+
self.in_channels = in_channels
|
390 |
+
self.out_channels = out_channels
|
391 |
+
self.filter_channels = filter_channels
|
392 |
+
self.kernel_size = kernel_size
|
393 |
+
self.p_dropout = p_dropout
|
394 |
+
self.activation = activation
|
395 |
+
self.causal = causal
|
396 |
+
|
397 |
+
if causal:
|
398 |
+
self.padding = self._causal_padding
|
399 |
+
else:
|
400 |
+
self.padding = self._same_padding
|
401 |
+
|
402 |
+
self.conv_1 = nn.Conv1d(in_channels, filter_channels, kernel_size)
|
403 |
+
self.conv_2 = nn.Conv1d(filter_channels, out_channels, kernel_size)
|
404 |
+
self.drop = nn.Dropout(p_dropout)
|
405 |
+
|
406 |
+
def forward(self, x, x_mask):
|
407 |
+
x = self.conv_1(self.padding(x * x_mask))
|
408 |
+
if self.activation == "gelu":
|
409 |
+
x = x * torch.sigmoid(1.702 * x)
|
410 |
+
else:
|
411 |
+
x = torch.relu(x)
|
412 |
+
x = self.drop(x)
|
413 |
+
x = self.conv_2(self.padding(x * x_mask))
|
414 |
+
return x * x_mask
|
415 |
+
|
416 |
+
def _causal_padding(self, x):
|
417 |
+
if self.kernel_size == 1:
|
418 |
+
return x
|
419 |
+
pad_l = self.kernel_size - 1
|
420 |
+
pad_r = 0
|
421 |
+
padding = [[0, 0], [0, 0], [pad_l, pad_r]]
|
422 |
+
x = F.pad(x, commons.convert_pad_shape(padding))
|
423 |
+
return x
|
424 |
+
|
425 |
+
def _same_padding(self, x):
|
426 |
+
if self.kernel_size == 1:
|
427 |
+
return x
|
428 |
+
pad_l = (self.kernel_size - 1) // 2
|
429 |
+
pad_r = self.kernel_size // 2
|
430 |
+
padding = [[0, 0], [0, 0], [pad_l, pad_r]]
|
431 |
+
x = F.pad(x, commons.convert_pad_shape(padding))
|
432 |
+
return x
|
433 |
+
|
434 |
+
|
435 |
+
import torch.nn as nn
|
436 |
+
from torch.nn.utils import remove_weight_norm, weight_norm
|
437 |
+
|
438 |
+
|
439 |
+
class Depthwise_Separable_Conv1D(nn.Module):
|
440 |
+
def __init__(
|
441 |
+
self,
|
442 |
+
in_channels,
|
443 |
+
out_channels,
|
444 |
+
kernel_size,
|
445 |
+
stride=1,
|
446 |
+
padding=0,
|
447 |
+
dilation=1,
|
448 |
+
bias=True,
|
449 |
+
padding_mode="zeros", # TODO: refine this type
|
450 |
+
device=None,
|
451 |
+
dtype=None,
|
452 |
+
):
|
453 |
+
super().__init__()
|
454 |
+
self.depth_conv = nn.Conv1d(
|
455 |
+
in_channels=in_channels,
|
456 |
+
out_channels=in_channels,
|
457 |
+
kernel_size=kernel_size,
|
458 |
+
groups=in_channels,
|
459 |
+
stride=stride,
|
460 |
+
padding=padding,
|
461 |
+
dilation=dilation,
|
462 |
+
bias=bias,
|
463 |
+
padding_mode=padding_mode,
|
464 |
+
device=device,
|
465 |
+
dtype=dtype,
|
466 |
+
)
|
467 |
+
self.point_conv = nn.Conv1d(
|
468 |
+
in_channels=in_channels,
|
469 |
+
out_channels=out_channels,
|
470 |
+
kernel_size=1,
|
471 |
+
bias=bias,
|
472 |
+
device=device,
|
473 |
+
dtype=dtype,
|
474 |
+
)
|
475 |
+
|
476 |
+
def forward(self, input):
|
477 |
+
return self.point_conv(self.depth_conv(input))
|
478 |
+
|
479 |
+
def weight_norm(self):
|
480 |
+
self.depth_conv = weight_norm(self.depth_conv, name="weight")
|
481 |
+
self.point_conv = weight_norm(self.point_conv, name="weight")
|
482 |
+
|
483 |
+
def remove_weight_norm(self):
|
484 |
+
self.depth_conv = remove_weight_norm(self.depth_conv, name="weight")
|
485 |
+
self.point_conv = remove_weight_norm(self.point_conv, name="weight")
|
486 |
+
|
487 |
+
|
488 |
+
class Depthwise_Separable_TransposeConv1D(nn.Module):
|
489 |
+
def __init__(
|
490 |
+
self,
|
491 |
+
in_channels,
|
492 |
+
out_channels,
|
493 |
+
kernel_size,
|
494 |
+
stride=1,
|
495 |
+
padding=0,
|
496 |
+
output_padding=0,
|
497 |
+
bias=True,
|
498 |
+
dilation=1,
|
499 |
+
padding_mode="zeros", # TODO: refine this type
|
500 |
+
device=None,
|
501 |
+
dtype=None,
|
502 |
+
):
|
503 |
+
super().__init__()
|
504 |
+
self.depth_conv = nn.ConvTranspose1d(
|
505 |
+
in_channels=in_channels,
|
506 |
+
out_channels=in_channels,
|
507 |
+
kernel_size=kernel_size,
|
508 |
+
groups=in_channels,
|
509 |
+
stride=stride,
|
510 |
+
output_padding=output_padding,
|
511 |
+
padding=padding,
|
512 |
+
dilation=dilation,
|
513 |
+
bias=bias,
|
514 |
+
padding_mode=padding_mode,
|
515 |
+
device=device,
|
516 |
+
dtype=dtype,
|
517 |
+
)
|
518 |
+
self.point_conv = nn.Conv1d(
|
519 |
+
in_channels=in_channels,
|
520 |
+
out_channels=out_channels,
|
521 |
+
kernel_size=1,
|
522 |
+
bias=bias,
|
523 |
+
device=device,
|
524 |
+
dtype=dtype,
|
525 |
+
)
|
526 |
+
|
527 |
+
def forward(self, input):
|
528 |
+
return self.point_conv(self.depth_conv(input))
|
529 |
+
|
530 |
+
def weight_norm(self):
|
531 |
+
self.depth_conv = weight_norm(self.depth_conv, name="weight")
|
532 |
+
self.point_conv = weight_norm(self.point_conv, name="weight")
|
533 |
+
|
534 |
+
def remove_weight_norm(self):
|
535 |
+
remove_weight_norm(self.depth_conv, name="weight")
|
536 |
+
remove_weight_norm(self.point_conv, name="weight")
|
537 |
+
|
538 |
+
|
539 |
+
def weight_norm_modules(module, name="weight", dim=0):
|
540 |
+
if isinstance(module, Depthwise_Separable_Conv1D) or isinstance(
|
541 |
+
module, Depthwise_Separable_TransposeConv1D
|
542 |
+
):
|
543 |
+
module.weight_norm()
|
544 |
+
return module
|
545 |
+
else:
|
546 |
+
return weight_norm(module, name, dim)
|
547 |
+
|
548 |
+
|
549 |
+
def remove_weight_norm_modules(module, name="weight"):
|
550 |
+
if isinstance(module, Depthwise_Separable_Conv1D) or isinstance(
|
551 |
+
module, Depthwise_Separable_TransposeConv1D
|
552 |
+
):
|
553 |
+
module.remove_weight_norm()
|
554 |
+
else:
|
555 |
+
remove_weight_norm(module, name)
|
556 |
+
|
557 |
+
|
558 |
+
class FFT(nn.Module):
|
559 |
+
def __init__(
|
560 |
+
self,
|
561 |
+
hidden_channels,
|
562 |
+
filter_channels,
|
563 |
+
n_heads,
|
564 |
+
n_layers=1,
|
565 |
+
kernel_size=1,
|
566 |
+
p_dropout=0.0,
|
567 |
+
proximal_bias=False,
|
568 |
+
proximal_init=True,
|
569 |
+
isflow=False,
|
570 |
+
**kwargs
|
571 |
+
):
|
572 |
+
super().__init__()
|
573 |
+
self.hidden_channels = hidden_channels
|
574 |
+
self.filter_channels = filter_channels
|
575 |
+
self.n_heads = n_heads
|
576 |
+
self.n_layers = n_layers
|
577 |
+
self.kernel_size = kernel_size
|
578 |
+
self.p_dropout = p_dropout
|
579 |
+
self.proximal_bias = proximal_bias
|
580 |
+
self.proximal_init = proximal_init
|
581 |
+
if isflow:
|
582 |
+
cond_layer = torch.nn.Conv1d(
|
583 |
+
kwargs["gin_channels"], 2 * hidden_channels * n_layers, 1
|
584 |
+
)
|
585 |
+
self.cond_pre = torch.nn.Conv1d(hidden_channels, 2 * hidden_channels, 1)
|
586 |
+
self.cond_layer = weight_norm_modules(cond_layer, name="weight")
|
587 |
+
self.gin_channels = kwargs["gin_channels"]
|
588 |
+
self.drop = nn.Dropout(p_dropout)
|
589 |
+
self.self_attn_layers = nn.ModuleList()
|
590 |
+
self.norm_layers_0 = nn.ModuleList()
|
591 |
+
self.ffn_layers = nn.ModuleList()
|
592 |
+
self.norm_layers_1 = nn.ModuleList()
|
593 |
+
for i in range(self.n_layers):
|
594 |
+
self.self_attn_layers.append(
|
595 |
+
MultiHeadAttention(
|
596 |
+
hidden_channels,
|
597 |
+
hidden_channels,
|
598 |
+
n_heads,
|
599 |
+
p_dropout=p_dropout,
|
600 |
+
proximal_bias=proximal_bias,
|
601 |
+
proximal_init=proximal_init,
|
602 |
+
)
|
603 |
+
)
|
604 |
+
self.norm_layers_0.append(LayerNorm(hidden_channels))
|
605 |
+
self.ffn_layers.append(
|
606 |
+
FFN(
|
607 |
+
hidden_channels,
|
608 |
+
hidden_channels,
|
609 |
+
filter_channels,
|
610 |
+
kernel_size,
|
611 |
+
p_dropout=p_dropout,
|
612 |
+
causal=True,
|
613 |
+
)
|
614 |
+
)
|
615 |
+
self.norm_layers_1.append(LayerNorm(hidden_channels))
|
616 |
+
|
617 |
+
def forward(self, x, x_mask, g=None):
|
618 |
+
"""
|
619 |
+
x: decoder input
|
620 |
+
h: encoder output
|
621 |
+
"""
|
622 |
+
if g is not None:
|
623 |
+
g = self.cond_layer(g)
|
624 |
+
|
625 |
+
self_attn_mask = commons.subsequent_mask(x_mask.size(2)).to(
|
626 |
+
device=x.device, dtype=x.dtype
|
627 |
+
)
|
628 |
+
x = x * x_mask
|
629 |
+
for i in range(self.n_layers):
|
630 |
+
if g is not None:
|
631 |
+
x = self.cond_pre(x)
|
632 |
+
cond_offset = i * 2 * self.hidden_channels
|
633 |
+
g_l = g[:, cond_offset : cond_offset + 2 * self.hidden_channels, :]
|
634 |
+
x = commons.fused_add_tanh_sigmoid_multiply(
|
635 |
+
x, g_l, torch.IntTensor([self.hidden_channels])
|
636 |
+
)
|
637 |
+
y = self.self_attn_layers[i](x, x, self_attn_mask)
|
638 |
+
y = self.drop(y)
|
639 |
+
x = self.norm_layers_0[i](x + y)
|
640 |
+
|
641 |
+
y = self.ffn_layers[i](x, x_mask)
|
642 |
+
y = self.drop(y)
|
643 |
+
x = self.norm_layers_1[i](x + y)
|
644 |
+
x = x * x_mask
|
645 |
+
return x
|
646 |
+
|
647 |
+
|
648 |
+
class TransformerCouplingLayer(nn.Module):
|
649 |
+
def __init__(
|
650 |
+
self,
|
651 |
+
channels,
|
652 |
+
hidden_channels,
|
653 |
+
kernel_size,
|
654 |
+
n_layers,
|
655 |
+
n_heads,
|
656 |
+
p_dropout=0,
|
657 |
+
filter_channels=0,
|
658 |
+
mean_only=False,
|
659 |
+
wn_sharing_parameter=None,
|
660 |
+
gin_channels=0,
|
661 |
+
):
|
662 |
+
assert channels % 2 == 0, "channels should be divisible by 2"
|
663 |
+
super().__init__()
|
664 |
+
self.channels = channels
|
665 |
+
self.hidden_channels = hidden_channels
|
666 |
+
self.kernel_size = kernel_size
|
667 |
+
self.n_layers = n_layers
|
668 |
+
self.half_channels = channels // 2
|
669 |
+
self.mean_only = mean_only
|
670 |
+
|
671 |
+
self.pre = nn.Conv1d(self.half_channels, hidden_channels, 1)
|
672 |
+
self.enc = (
|
673 |
+
Encoder(
|
674 |
+
hidden_channels,
|
675 |
+
filter_channels,
|
676 |
+
n_heads,
|
677 |
+
n_layers,
|
678 |
+
kernel_size,
|
679 |
+
p_dropout,
|
680 |
+
isflow=True,
|
681 |
+
gin_channels=gin_channels,
|
682 |
+
)
|
683 |
+
if wn_sharing_parameter is None
|
684 |
+
else wn_sharing_parameter
|
685 |
+
)
|
686 |
+
self.post = nn.Conv1d(hidden_channels, self.half_channels * (2 - mean_only), 1)
|
687 |
+
self.post.weight.data.zero_()
|
688 |
+
self.post.bias.data.zero_()
|
689 |
+
|
690 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
691 |
+
x0, x1 = torch.split(x, [self.half_channels] * 2, 1)
|
692 |
+
h = self.pre(x0) * x_mask
|
693 |
+
h = self.enc(h, x_mask, g=g)
|
694 |
+
stats = self.post(h) * x_mask
|
695 |
+
if not self.mean_only:
|
696 |
+
m, logs = torch.split(stats, [self.half_channels] * 2, 1)
|
697 |
+
else:
|
698 |
+
m = stats
|
699 |
+
logs = torch.zeros_like(m)
|
700 |
+
|
701 |
+
if not reverse:
|
702 |
+
x1 = m + x1 * torch.exp(logs) * x_mask
|
703 |
+
x = torch.cat([x0, x1], 1)
|
704 |
+
logdet = torch.sum(logs, [1, 2])
|
705 |
+
return x, logdet
|
706 |
+
else:
|
707 |
+
x1 = (x1 - m) * torch.exp(-logs) * x_mask
|
708 |
+
x = torch.cat([x0, x1], 1)
|
709 |
+
return x
|
cleaner.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from . import chinese, japanese, symbols, english
|
2 |
+
from .symbols import cleaned_text_to_sequence
|
3 |
+
|
4 |
+
language_module_map = {"zh": chinese, "ja": japanese, "en": english}
|
5 |
+
special = [
|
6 |
+
# ("%", "zh", "SP"),
|
7 |
+
("¥", "zh", "SP2"),
|
8 |
+
("^", "zh", "SP3"),
|
9 |
+
# ('@', 'zh', "SP4")#不搞鬼畜了,和第二版保持一致吧
|
10 |
+
]
|
11 |
+
|
12 |
+
|
13 |
+
def clean_text(text, language):
|
14 |
+
if(language not in language_module_map):
|
15 |
+
language="en"
|
16 |
+
text=" "
|
17 |
+
for special_s, special_l, target_symbol in special:
|
18 |
+
if special_s in text and language == special_l:
|
19 |
+
return clean_special(text, language, special_s, target_symbol)
|
20 |
+
language_module = language_module_map[language]
|
21 |
+
norm_text = language_module.text_normalize(text)
|
22 |
+
if language == "zh":
|
23 |
+
phones, word2ph = language_module.g2p(norm_text)
|
24 |
+
assert len(phones) == sum(word2ph)
|
25 |
+
assert len(norm_text) == len(word2ph)
|
26 |
+
else:
|
27 |
+
phones = language_module.g2p(norm_text)
|
28 |
+
word2ph = None
|
29 |
+
|
30 |
+
for ph in phones:
|
31 |
+
assert ph in symbols.symbols
|
32 |
+
return phones, word2ph, norm_text
|
33 |
+
|
34 |
+
|
35 |
+
def clean_special(text, language, special_s, target_symbol):
|
36 |
+
"""
|
37 |
+
特殊静音段sp符号处理
|
38 |
+
"""
|
39 |
+
text = text.replace(special_s, ",")
|
40 |
+
language_module = language_module_map[language]
|
41 |
+
norm_text = language_module.text_normalize(text)
|
42 |
+
phones = language_module.g2p(norm_text)
|
43 |
+
new_ph = []
|
44 |
+
for ph in phones[0]:
|
45 |
+
assert ph in symbols
|
46 |
+
if ph == ",":
|
47 |
+
new_ph.append(target_symbol)
|
48 |
+
else:
|
49 |
+
new_ph.append(ph)
|
50 |
+
return new_ph, phones[1], norm_text
|
51 |
+
|
52 |
+
|
53 |
+
def text_to_sequence(text, language):
|
54 |
+
phones = clean_text(text)
|
55 |
+
return cleaned_text_to_sequence(phones)
|
56 |
+
|
57 |
+
|
58 |
+
if __name__ == "__main__":
|
59 |
+
print(clean_text("你好%啊啊啊额、还是到付红四方。", "zh"))
|
commons.py
ADDED
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import torch
|
3 |
+
from torch.nn import functional as F
|
4 |
+
|
5 |
+
|
6 |
+
def init_weights(m, mean=0.0, std=0.01):
|
7 |
+
classname = m.__class__.__name__
|
8 |
+
if classname.find("Conv") != -1:
|
9 |
+
m.weight.data.normal_(mean, std)
|
10 |
+
|
11 |
+
|
12 |
+
def get_padding(kernel_size, dilation=1):
|
13 |
+
return int((kernel_size * dilation - dilation) / 2)
|
14 |
+
|
15 |
+
|
16 |
+
def convert_pad_shape(pad_shape):
|
17 |
+
l = pad_shape[::-1]
|
18 |
+
pad_shape = [item for sublist in l for item in sublist]
|
19 |
+
return pad_shape
|
20 |
+
|
21 |
+
|
22 |
+
def intersperse(lst, item):
|
23 |
+
result = [item] * (len(lst) * 2 + 1)
|
24 |
+
result[1::2] = lst
|
25 |
+
return result
|
26 |
+
|
27 |
+
|
28 |
+
def kl_divergence(m_p, logs_p, m_q, logs_q):
|
29 |
+
"""KL(P||Q)"""
|
30 |
+
kl = (logs_q - logs_p) - 0.5
|
31 |
+
kl += (
|
32 |
+
0.5 * (torch.exp(2.0 * logs_p) + ((m_p - m_q) ** 2)) * torch.exp(-2.0 * logs_q)
|
33 |
+
)
|
34 |
+
return kl
|
35 |
+
|
36 |
+
|
37 |
+
def rand_gumbel(shape):
|
38 |
+
"""Sample from the Gumbel distribution, protect from overflows."""
|
39 |
+
uniform_samples = torch.rand(shape) * 0.99998 + 0.00001
|
40 |
+
return -torch.log(-torch.log(uniform_samples))
|
41 |
+
|
42 |
+
|
43 |
+
def rand_gumbel_like(x):
|
44 |
+
g = rand_gumbel(x.size()).to(dtype=x.dtype, device=x.device)
|
45 |
+
return g
|
46 |
+
|
47 |
+
|
48 |
+
def slice_segments(x, ids_str, segment_size=4):
|
49 |
+
ret = torch.zeros_like(x[:, :, :segment_size])
|
50 |
+
for i in range(x.size(0)):
|
51 |
+
idx_str = ids_str[i]
|
52 |
+
idx_end = idx_str + segment_size
|
53 |
+
ret[i] = x[i, :, idx_str:idx_end]
|
54 |
+
return ret
|
55 |
+
|
56 |
+
|
57 |
+
def rand_slice_segments(x, x_lengths=None, segment_size=4):
|
58 |
+
b, d, t = x.size()
|
59 |
+
if x_lengths is None:
|
60 |
+
x_lengths = t
|
61 |
+
ids_str_max = x_lengths - segment_size + 1
|
62 |
+
ids_str = (torch.rand([b]).to(device=x.device) * ids_str_max).to(dtype=torch.long)
|
63 |
+
ret = slice_segments(x, ids_str, segment_size)
|
64 |
+
return ret, ids_str
|
65 |
+
|
66 |
+
|
67 |
+
def get_timing_signal_1d(length, channels, min_timescale=1.0, max_timescale=1.0e4):
|
68 |
+
position = torch.arange(length, dtype=torch.float)
|
69 |
+
num_timescales = channels // 2
|
70 |
+
log_timescale_increment = math.log(float(max_timescale) / float(min_timescale)) / (
|
71 |
+
num_timescales - 1
|
72 |
+
)
|
73 |
+
inv_timescales = min_timescale * torch.exp(
|
74 |
+
torch.arange(num_timescales, dtype=torch.float) * -log_timescale_increment
|
75 |
+
)
|
76 |
+
scaled_time = position.unsqueeze(0) * inv_timescales.unsqueeze(1)
|
77 |
+
signal = torch.cat([torch.sin(scaled_time), torch.cos(scaled_time)], 0)
|
78 |
+
signal = F.pad(signal, [0, 0, 0, channels % 2])
|
79 |
+
signal = signal.view(1, channels, length)
|
80 |
+
return signal
|
81 |
+
|
82 |
+
|
83 |
+
def add_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4):
|
84 |
+
b, channels, length = x.size()
|
85 |
+
signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
|
86 |
+
return x + signal.to(dtype=x.dtype, device=x.device)
|
87 |
+
|
88 |
+
|
89 |
+
def cat_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4, axis=1):
|
90 |
+
b, channels, length = x.size()
|
91 |
+
signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
|
92 |
+
return torch.cat([x, signal.to(dtype=x.dtype, device=x.device)], axis)
|
93 |
+
|
94 |
+
|
95 |
+
def subsequent_mask(length):
|
96 |
+
mask = torch.tril(torch.ones(length, length)).unsqueeze(0).unsqueeze(0)
|
97 |
+
return mask
|
98 |
+
|
99 |
+
|
100 |
+
@torch.jit.script
|
101 |
+
def fused_add_tanh_sigmoid_multiply(input_a, input_b, n_channels):
|
102 |
+
n_channels_int = n_channels[0]
|
103 |
+
in_act = input_a + input_b
|
104 |
+
t_act = torch.tanh(in_act[:, :n_channels_int, :])
|
105 |
+
s_act = torch.sigmoid(in_act[:, n_channels_int:, :])
|
106 |
+
acts = t_act * s_act
|
107 |
+
return acts
|
108 |
+
|
109 |
+
|
110 |
+
def convert_pad_shape(pad_shape):
|
111 |
+
l = pad_shape[::-1]
|
112 |
+
pad_shape = [item for sublist in l for item in sublist]
|
113 |
+
return pad_shape
|
114 |
+
|
115 |
+
|
116 |
+
def shift_1d(x):
|
117 |
+
x = F.pad(x, convert_pad_shape([[0, 0], [0, 0], [1, 0]]))[:, :, :-1]
|
118 |
+
return x
|
119 |
+
|
120 |
+
|
121 |
+
def sequence_mask(length, max_length=None):
|
122 |
+
if max_length is None:
|
123 |
+
max_length = length.max()
|
124 |
+
x = torch.arange(max_length, dtype=length.dtype, device=length.device)
|
125 |
+
return x.unsqueeze(0) < length.unsqueeze(1)
|
126 |
+
|
127 |
+
|
128 |
+
def generate_path(duration, mask):
|
129 |
+
"""
|
130 |
+
duration: [b, 1, t_x]
|
131 |
+
mask: [b, 1, t_y, t_x]
|
132 |
+
"""
|
133 |
+
device = duration.device
|
134 |
+
|
135 |
+
b, _, t_y, t_x = mask.shape
|
136 |
+
cum_duration = torch.cumsum(duration, -1)
|
137 |
+
|
138 |
+
cum_duration_flat = cum_duration.view(b * t_x)
|
139 |
+
path = sequence_mask(cum_duration_flat, t_y).to(mask.dtype)
|
140 |
+
path = path.view(b, t_x, t_y)
|
141 |
+
path = path - F.pad(path, convert_pad_shape([[0, 0], [1, 0], [0, 0]]))[:, :-1]
|
142 |
+
path = path.unsqueeze(1).transpose(2, 3) * mask
|
143 |
+
return path
|
144 |
+
|
145 |
+
|
146 |
+
def clip_grad_value_(parameters, clip_value, norm_type=2):
|
147 |
+
if isinstance(parameters, torch.Tensor):
|
148 |
+
parameters = [parameters]
|
149 |
+
parameters = list(filter(lambda p: p.grad is not None, parameters))
|
150 |
+
norm_type = float(norm_type)
|
151 |
+
if clip_value is not None:
|
152 |
+
clip_value = float(clip_value)
|
153 |
+
|
154 |
+
total_norm = 0
|
155 |
+
for p in parameters:
|
156 |
+
param_norm = p.grad.data.norm(norm_type)
|
157 |
+
total_norm += param_norm.item() ** norm_type
|
158 |
+
if clip_value is not None:
|
159 |
+
p.grad.data.clamp_(min=-clip_value, max=clip_value)
|
160 |
+
total_norm = total_norm ** (1.0 / norm_type)
|
161 |
+
return total_norm
|
162 |
+
|
163 |
+
|
164 |
+
def squeeze(x, x_mask=None, n_sqz=2):
|
165 |
+
b, c, t = x.size()
|
166 |
+
|
167 |
+
t = (t // n_sqz) * n_sqz
|
168 |
+
x = x[:, :, :t]
|
169 |
+
x_sqz = x.view(b, c, t // n_sqz, n_sqz)
|
170 |
+
x_sqz = x_sqz.permute(0, 3, 1, 2).contiguous().view(b, c * n_sqz, t // n_sqz)
|
171 |
+
|
172 |
+
if x_mask is not None:
|
173 |
+
x_mask = x_mask[:, :, n_sqz - 1 :: n_sqz]
|
174 |
+
else:
|
175 |
+
x_mask = torch.ones(b, 1, t // n_sqz).to(device=x.device, dtype=x.dtype)
|
176 |
+
return x_sqz * x_mask, x_mask
|
177 |
+
|
178 |
+
|
179 |
+
def unsqueeze(x, x_mask=None, n_sqz=2):
|
180 |
+
b, c, t = x.size()
|
181 |
+
|
182 |
+
x_unsqz = x.view(b, n_sqz, c // n_sqz, t)
|
183 |
+
x_unsqz = x_unsqz.permute(0, 2, 3, 1).contiguous().view(b, c // n_sqz, t * n_sqz)
|
184 |
+
|
185 |
+
if x_mask is not None:
|
186 |
+
x_mask = x_mask.unsqueeze(-1).repeat(1, 1, 1, n_sqz).view(b, 1, t * n_sqz)
|
187 |
+
else:
|
188 |
+
x_mask = torch.ones(b, 1, t * n_sqz).to(device=x.device, dtype=x.dtype)
|
189 |
+
return x_unsqz * x_mask, x_mask
|
config.json
ADDED
@@ -0,0 +1,387 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_bert_config_dict": {
|
3 |
+
"_name_or_path": "./ckpts/chinese-roberta-wwm-ext-large",
|
4 |
+
"add_cross_attention": false,
|
5 |
+
"architectures": [
|
6 |
+
"BertForMaskedLM"
|
7 |
+
],
|
8 |
+
"attention_probs_dropout_prob": 0.1,
|
9 |
+
"attn_implementation": null,
|
10 |
+
"bad_words_ids": null,
|
11 |
+
"begin_suppress_tokens": null,
|
12 |
+
"bos_token_id": 0,
|
13 |
+
"chunk_size_feed_forward": 0,
|
14 |
+
"classifier_dropout": null,
|
15 |
+
"cross_attention_hidden_size": null,
|
16 |
+
"decoder_start_token_id": null,
|
17 |
+
"directionality": "bidi",
|
18 |
+
"diversity_penalty": 0.0,
|
19 |
+
"do_sample": false,
|
20 |
+
"early_stopping": false,
|
21 |
+
"encoder_no_repeat_ngram_size": 0,
|
22 |
+
"eos_token_id": 2,
|
23 |
+
"exponential_decay_length_penalty": null,
|
24 |
+
"finetuning_task": null,
|
25 |
+
"forced_bos_token_id": null,
|
26 |
+
"forced_eos_token_id": null,
|
27 |
+
"hidden_act": "gelu",
|
28 |
+
"hidden_dropout_prob": 0.1,
|
29 |
+
"hidden_size": 1024,
|
30 |
+
"id2label": {
|
31 |
+
"0": "LABEL_0",
|
32 |
+
"1": "LABEL_1"
|
33 |
+
},
|
34 |
+
"initializer_range": 0.02,
|
35 |
+
"intermediate_size": 4096,
|
36 |
+
"is_decoder": false,
|
37 |
+
"is_encoder_decoder": false,
|
38 |
+
"label2id": {
|
39 |
+
"LABEL_0": 0,
|
40 |
+
"LABEL_1": 1
|
41 |
+
},
|
42 |
+
"layer_norm_eps": 1e-12,
|
43 |
+
"length_penalty": 1.0,
|
44 |
+
"max_length": 20,
|
45 |
+
"max_position_embeddings": 512,
|
46 |
+
"min_length": 0,
|
47 |
+
"model_type": "bert",
|
48 |
+
"no_repeat_ngram_size": 0,
|
49 |
+
"num_attention_heads": 16,
|
50 |
+
"num_beam_groups": 1,
|
51 |
+
"num_beams": 1,
|
52 |
+
"num_hidden_layers": 24,
|
53 |
+
"num_return_sequences": 1,
|
54 |
+
"output_attentions": false,
|
55 |
+
"output_hidden_states": false,
|
56 |
+
"output_past": true,
|
57 |
+
"output_scores": false,
|
58 |
+
"pad_token_id": 0,
|
59 |
+
"pooler_fc_size": 768,
|
60 |
+
"pooler_num_attention_heads": 12,
|
61 |
+
"pooler_num_fc_layers": 3,
|
62 |
+
"pooler_size_per_head": 128,
|
63 |
+
"pooler_type": "first_token_transform",
|
64 |
+
"position_embedding_type": "absolute",
|
65 |
+
"prefix": null,
|
66 |
+
"problem_type": null,
|
67 |
+
"pruned_heads": {},
|
68 |
+
"remove_invalid_values": false,
|
69 |
+
"repetition_penalty": 1.0,
|
70 |
+
"return_dict": true,
|
71 |
+
"return_dict_in_generate": false,
|
72 |
+
"sep_token_id": null,
|
73 |
+
"suppress_tokens": null,
|
74 |
+
"task_specific_params": null,
|
75 |
+
"temperature": 1.0,
|
76 |
+
"tf_legacy_loss": false,
|
77 |
+
"tie_encoder_decoder": false,
|
78 |
+
"tie_word_embeddings": true,
|
79 |
+
"tokenizer_class": null,
|
80 |
+
"top_k": 50,
|
81 |
+
"top_p": 1.0,
|
82 |
+
"torch_dtype": "float16",
|
83 |
+
"torchscript": false,
|
84 |
+
"transformers_version": "4.37.2",
|
85 |
+
"type_vocab_size": 2,
|
86 |
+
"typical_p": 1.0,
|
87 |
+
"use_bfloat16": false,
|
88 |
+
"use_cache": true,
|
89 |
+
"vocab_size": 21128
|
90 |
+
},
|
91 |
+
"_gpt_config_dict": {
|
92 |
+
"data": {
|
93 |
+
"max_eval_sample": 8,
|
94 |
+
"max_sec": 54,
|
95 |
+
"num_workers": 4,
|
96 |
+
"pad_val": 1024
|
97 |
+
},
|
98 |
+
"inference": {
|
99 |
+
"top_k": 5
|
100 |
+
},
|
101 |
+
"model": {
|
102 |
+
"EOS": 1024,
|
103 |
+
"dropout": 0,
|
104 |
+
"embedding_dim": 512,
|
105 |
+
"head": 16,
|
106 |
+
"hidden_dim": 512,
|
107 |
+
"linear_units": 2048,
|
108 |
+
"n_layer": 24,
|
109 |
+
"phoneme_vocab_size": 512,
|
110 |
+
"random_bert": 0,
|
111 |
+
"vocab_size": 1025
|
112 |
+
},
|
113 |
+
"optimizer": {
|
114 |
+
"decay_steps": 40000,
|
115 |
+
"lr": 0.01,
|
116 |
+
"lr_end": 0.0001,
|
117 |
+
"lr_init": 1e-05,
|
118 |
+
"warmup_steps": 2000
|
119 |
+
},
|
120 |
+
"output_dir": "logs/Lumi_294_dpo/logs_s1",
|
121 |
+
"pretrained_s1": "GPT_SoVITS/pretrained_models/s1bert25hz-2kh-longer-epoch=68e-step=50232.ckpt",
|
122 |
+
"train": {
|
123 |
+
"batch_size": 1,
|
124 |
+
"epochs": 15,
|
125 |
+
"exp_name": "Lumi_294_dpo",
|
126 |
+
"gradient_clip": 1.0,
|
127 |
+
"half_weights_save_dir": "GPT_weights",
|
128 |
+
"if_dpo": true,
|
129 |
+
"if_save_every_weights": true,
|
130 |
+
"if_save_latest": true,
|
131 |
+
"precision": "16-mixed",
|
132 |
+
"save_every_n_epoch": 5,
|
133 |
+
"seed": 1234
|
134 |
+
},
|
135 |
+
"train_phoneme_path": "logs/Lumi_294_dpo/2-name2text.txt",
|
136 |
+
"train_semantic_path": "logs/Lumi_294_dpo/6-name2semantic.tsv"
|
137 |
+
},
|
138 |
+
"_hps_dict": {
|
139 |
+
"content_module": "cnhubert",
|
140 |
+
"data": {
|
141 |
+
"add_blank": true,
|
142 |
+
"cleaned_text": true,
|
143 |
+
"exp_dir": "logs/Lumi_294_dpo",
|
144 |
+
"filter_length": 2048,
|
145 |
+
"hop_length": 640,
|
146 |
+
"max_wav_value": 32768.0,
|
147 |
+
"mel_fmax": null,
|
148 |
+
"mel_fmin": 0.0,
|
149 |
+
"n_mel_channels": 128,
|
150 |
+
"n_speakers": 300,
|
151 |
+
"sampling_rate": 32000,
|
152 |
+
"win_length": 2048
|
153 |
+
},
|
154 |
+
"model": {
|
155 |
+
"filter_channels": 768,
|
156 |
+
"freeze_quantizer": true,
|
157 |
+
"gin_channels": 512,
|
158 |
+
"hidden_channels": 192,
|
159 |
+
"inter_channels": 192,
|
160 |
+
"kernel_size": 3,
|
161 |
+
"n_heads": 2,
|
162 |
+
"n_layers": 6,
|
163 |
+
"n_layers_q": 3,
|
164 |
+
"p_dropout": 0.1,
|
165 |
+
"resblock": "1",
|
166 |
+
"resblock_dilation_sizes": [
|
167 |
+
[
|
168 |
+
1,
|
169 |
+
3,
|
170 |
+
5
|
171 |
+
],
|
172 |
+
[
|
173 |
+
1,
|
174 |
+
3,
|
175 |
+
5
|
176 |
+
],
|
177 |
+
[
|
178 |
+
1,
|
179 |
+
3,
|
180 |
+
5
|
181 |
+
]
|
182 |
+
],
|
183 |
+
"resblock_kernel_sizes": [
|
184 |
+
3,
|
185 |
+
7,
|
186 |
+
11
|
187 |
+
],
|
188 |
+
"semantic_frame_rate": "25hz",
|
189 |
+
"upsample_initial_channel": 512,
|
190 |
+
"upsample_kernel_sizes": [
|
191 |
+
16,
|
192 |
+
16,
|
193 |
+
8,
|
194 |
+
2,
|
195 |
+
2
|
196 |
+
],
|
197 |
+
"upsample_rates": [
|
198 |
+
10,
|
199 |
+
8,
|
200 |
+
2,
|
201 |
+
2,
|
202 |
+
2
|
203 |
+
],
|
204 |
+
"use_spectral_norm": false
|
205 |
+
},
|
206 |
+
"name": "Lumi_294_dpo",
|
207 |
+
"pretrain": null,
|
208 |
+
"resume_step": null,
|
209 |
+
"s2_ckpt_dir": "logs/Lumi_294_dpo",
|
210 |
+
"save_weight_dir": "SoVITS_weights",
|
211 |
+
"train": {
|
212 |
+
"batch_size": 5,
|
213 |
+
"betas": [
|
214 |
+
0.8,
|
215 |
+
0.99
|
216 |
+
],
|
217 |
+
"c_kl": 1.0,
|
218 |
+
"c_mel": 45,
|
219 |
+
"epochs": 16,
|
220 |
+
"eps": 1e-09,
|
221 |
+
"eval_interval": 500,
|
222 |
+
"fp16_run": true,
|
223 |
+
"gpu_numbers": "0",
|
224 |
+
"if_save_every_weights": true,
|
225 |
+
"if_save_latest": true,
|
226 |
+
"init_lr_ratio": 1,
|
227 |
+
"learning_rate": 0.0001,
|
228 |
+
"log_interval": 100,
|
229 |
+
"lr_decay": 0.999875,
|
230 |
+
"pretrained_s2D": "GPT_SoVITS/pretrained_models/s2D488k.pth",
|
231 |
+
"pretrained_s2G": "GPT_SoVITS/pretrained_models/s2G488k.pth",
|
232 |
+
"save_every_epoch": 4,
|
233 |
+
"seed": 1234,
|
234 |
+
"segment_size": 20480,
|
235 |
+
"text_low_lr_rate": 0.4,
|
236 |
+
"warmup_epochs": 0
|
237 |
+
}
|
238 |
+
},
|
239 |
+
"_hubert_config_dict": {
|
240 |
+
"_name_or_path": "./ckpts/chinese-hubert-base",
|
241 |
+
"activation_dropout": 0.1,
|
242 |
+
"add_cross_attention": false,
|
243 |
+
"apply_spec_augment": true,
|
244 |
+
"architectures": [
|
245 |
+
"HubertModel"
|
246 |
+
],
|
247 |
+
"attention_dropout": 0.1,
|
248 |
+
"attn_implementation": null,
|
249 |
+
"bad_words_ids": null,
|
250 |
+
"begin_suppress_tokens": null,
|
251 |
+
"bos_token_id": 1,
|
252 |
+
"chunk_size_feed_forward": 0,
|
253 |
+
"classifier_proj_size": 256,
|
254 |
+
"conv_bias": false,
|
255 |
+
"conv_dim": [
|
256 |
+
512,
|
257 |
+
512,
|
258 |
+
512,
|
259 |
+
512,
|
260 |
+
512,
|
261 |
+
512,
|
262 |
+
512
|
263 |
+
],
|
264 |
+
"conv_kernel": [
|
265 |
+
10,
|
266 |
+
3,
|
267 |
+
3,
|
268 |
+
3,
|
269 |
+
3,
|
270 |
+
2,
|
271 |
+
2
|
272 |
+
],
|
273 |
+
"conv_stride": [
|
274 |
+
5,
|
275 |
+
2,
|
276 |
+
2,
|
277 |
+
2,
|
278 |
+
2,
|
279 |
+
2,
|
280 |
+
2
|
281 |
+
],
|
282 |
+
"cross_attention_hidden_size": null,
|
283 |
+
"ctc_loss_reduction": "sum",
|
284 |
+
"ctc_zero_infinity": false,
|
285 |
+
"decoder_start_token_id": null,
|
286 |
+
"diversity_penalty": 0.0,
|
287 |
+
"do_sample": false,
|
288 |
+
"do_stable_layer_norm": false,
|
289 |
+
"early_stopping": false,
|
290 |
+
"encoder_no_repeat_ngram_size": 0,
|
291 |
+
"eos_token_id": 2,
|
292 |
+
"exponential_decay_length_penalty": null,
|
293 |
+
"feat_extract_activation": "gelu",
|
294 |
+
"feat_extract_norm": "group",
|
295 |
+
"feat_proj_dropout": 0.0,
|
296 |
+
"feat_proj_layer_norm": true,
|
297 |
+
"final_dropout": 0.1,
|
298 |
+
"finetuning_task": null,
|
299 |
+
"forced_bos_token_id": null,
|
300 |
+
"forced_eos_token_id": null,
|
301 |
+
"hidden_act": "gelu",
|
302 |
+
"hidden_dropout": 0.1,
|
303 |
+
"hidden_size": 768,
|
304 |
+
"id2label": {
|
305 |
+
"0": "LABEL_0",
|
306 |
+
"1": "LABEL_1"
|
307 |
+
},
|
308 |
+
"initializer_range": 0.02,
|
309 |
+
"intermediate_size": 3072,
|
310 |
+
"is_decoder": false,
|
311 |
+
"is_encoder_decoder": false,
|
312 |
+
"label2id": {
|
313 |
+
"LABEL_0": 0,
|
314 |
+
"LABEL_1": 1
|
315 |
+
},
|
316 |
+
"layer_norm_eps": 1e-05,
|
317 |
+
"layerdrop": 0.1,
|
318 |
+
"length_penalty": 1.0,
|
319 |
+
"mask_feature_length": 10,
|
320 |
+
"mask_feature_min_masks": 0,
|
321 |
+
"mask_feature_prob": 0.0,
|
322 |
+
"mask_time_length": 10,
|
323 |
+
"mask_time_min_masks": 2,
|
324 |
+
"mask_time_prob": 0.05,
|
325 |
+
"max_length": 20,
|
326 |
+
"min_length": 0,
|
327 |
+
"model_type": "hubert",
|
328 |
+
"no_repeat_ngram_size": 0,
|
329 |
+
"num_attention_heads": 12,
|
330 |
+
"num_beam_groups": 1,
|
331 |
+
"num_beams": 1,
|
332 |
+
"num_conv_pos_embedding_groups": 16,
|
333 |
+
"num_conv_pos_embeddings": 128,
|
334 |
+
"num_feat_extract_layers": 7,
|
335 |
+
"num_hidden_layers": 12,
|
336 |
+
"num_return_sequences": 1,
|
337 |
+
"output_attentions": false,
|
338 |
+
"output_hidden_states": false,
|
339 |
+
"output_scores": false,
|
340 |
+
"pad_token_id": 0,
|
341 |
+
"prefix": null,
|
342 |
+
"problem_type": null,
|
343 |
+
"pruned_heads": {},
|
344 |
+
"remove_invalid_values": false,
|
345 |
+
"repetition_penalty": 1.0,
|
346 |
+
"return_dict": true,
|
347 |
+
"return_dict_in_generate": false,
|
348 |
+
"sep_token_id": null,
|
349 |
+
"suppress_tokens": null,
|
350 |
+
"task_specific_params": null,
|
351 |
+
"temperature": 1.0,
|
352 |
+
"tf_legacy_loss": false,
|
353 |
+
"tie_encoder_decoder": false,
|
354 |
+
"tie_word_embeddings": true,
|
355 |
+
"tokenizer_class": null,
|
356 |
+
"top_k": 50,
|
357 |
+
"top_p": 1.0,
|
358 |
+
"torch_dtype": "float16",
|
359 |
+
"torchscript": false,
|
360 |
+
"transformers_version": "4.37.2",
|
361 |
+
"typical_p": 1.0,
|
362 |
+
"use_bfloat16": false,
|
363 |
+
"use_weighted_layer_sum": false,
|
364 |
+
"vocab_size": 32
|
365 |
+
},
|
366 |
+
"_hubert_extractor_config_dict": {
|
367 |
+
"_processor_class": null,
|
368 |
+
"do_normalize": true,
|
369 |
+
"feature_extractor_type": "Wav2Vec2FeatureExtractor",
|
370 |
+
"feature_size": 1,
|
371 |
+
"padding_side": "right",
|
372 |
+
"padding_value": 0,
|
373 |
+
"return_attention_mask": false,
|
374 |
+
"sampling_rate": 16000
|
375 |
+
},
|
376 |
+
"architectures": [
|
377 |
+
"GPTSoVITSModel"
|
378 |
+
],
|
379 |
+
"auto_map": {
|
380 |
+
"AutoConfig": "configuration_gpt_sovits.GPTSoVITSConfig",
|
381 |
+
"AutoModel": "modeling_gpt_sovits.GPTSoVITSModel"
|
382 |
+
},
|
383 |
+
"model_type": "gpt_sovits",
|
384 |
+
"prompt_language": "zh",
|
385 |
+
"torch_dtype": "float32",
|
386 |
+
"transformers_version": "4.37.2"
|
387 |
+
}
|
configuration_gpt_sovits.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import PretrainedConfig
|
2 |
+
import torch
|
3 |
+
|
4 |
+
|
5 |
+
class GPTSoVITSConfig(PretrainedConfig):
|
6 |
+
model_type = "gpt_sovits"
|
7 |
+
|
8 |
+
def __init__(
|
9 |
+
self,
|
10 |
+
prompt_language: str="zh",
|
11 |
+
_hubert_config_dict: dict[str, any] = None,
|
12 |
+
_hubert_extractor_config_dict: dict[str, any] = None,
|
13 |
+
_bert_config_dict: dict[str, any] = None,
|
14 |
+
_hps_dict: dict[str, any] = None,
|
15 |
+
_gpt_config_dict: dict[str, any] = None,
|
16 |
+
**kwargs
|
17 |
+
):
|
18 |
+
self.prompt_language = prompt_language
|
19 |
+
self._hubert_config_dict = _hubert_config_dict
|
20 |
+
self._hubert_extractor_config_dict = _hubert_extractor_config_dict
|
21 |
+
self._bert_config_dict = _bert_config_dict
|
22 |
+
self._hps_dict = _hps_dict
|
23 |
+
self._gpt_config_dict = _gpt_config_dict
|
24 |
+
|
25 |
+
super().__init__(**kwargs)
|
core_vq.py
ADDED
@@ -0,0 +1,383 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2 |
+
# All rights reserved.
|
3 |
+
#
|
4 |
+
# This source code is licensed under the license found in the
|
5 |
+
# LICENSE file in the root directory of this source tree.
|
6 |
+
#
|
7 |
+
# This implementation is inspired from
|
8 |
+
# https://github.com/lucidrains/vector-quantize-pytorch
|
9 |
+
# which is released under MIT License. Hereafter, the original license:
|
10 |
+
# MIT License
|
11 |
+
#
|
12 |
+
# Copyright (c) 2020 Phil Wang
|
13 |
+
#
|
14 |
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
15 |
+
# of this software and associated documentation files (the "Software"), to deal
|
16 |
+
# in the Software without restriction, including without limitation the rights
|
17 |
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
18 |
+
# copies of the Software, and to permit persons to whom the Software is
|
19 |
+
# furnished to do so, subject to the following conditions:
|
20 |
+
#
|
21 |
+
# The above copyright notice and this permission notice shall be included in all
|
22 |
+
# copies or substantial portions of the Software.
|
23 |
+
#
|
24 |
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
25 |
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
26 |
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
27 |
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
28 |
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
29 |
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
30 |
+
# SOFTWARE.
|
31 |
+
|
32 |
+
"""Core vector quantization implementation."""
|
33 |
+
import typing as tp
|
34 |
+
|
35 |
+
from einops import rearrange, repeat
|
36 |
+
import torch
|
37 |
+
from torch import nn
|
38 |
+
import torch.nn.functional as F
|
39 |
+
from tqdm import tqdm
|
40 |
+
|
41 |
+
|
42 |
+
def default(val: tp.Any, d: tp.Any) -> tp.Any:
|
43 |
+
return val if val is not None else d
|
44 |
+
|
45 |
+
|
46 |
+
def ema_inplace(moving_avg, new, decay: float):
|
47 |
+
moving_avg.data.mul_(decay).add_(new, alpha=(1 - decay))
|
48 |
+
|
49 |
+
|
50 |
+
def laplace_smoothing(x, n_categories: int, epsilon: float = 1e-5):
|
51 |
+
return (x + epsilon) / (x.sum() + n_categories * epsilon)
|
52 |
+
|
53 |
+
|
54 |
+
def uniform_init(*shape: int):
|
55 |
+
t = torch.empty(shape)
|
56 |
+
nn.init.kaiming_uniform_(t)
|
57 |
+
return t
|
58 |
+
|
59 |
+
|
60 |
+
def sample_vectors(samples, num: int):
|
61 |
+
num_samples, device = samples.shape[0], samples.device
|
62 |
+
|
63 |
+
if num_samples >= num:
|
64 |
+
indices = torch.randperm(num_samples, device=device)[:num]
|
65 |
+
else:
|
66 |
+
indices = torch.randint(0, num_samples, (num,), device=device)
|
67 |
+
|
68 |
+
return samples[indices]
|
69 |
+
|
70 |
+
|
71 |
+
def kmeans(samples, num_clusters: int, num_iters: int = 10):
|
72 |
+
dim, dtype = samples.shape[-1], samples.dtype
|
73 |
+
max_kmeans_samples = 500
|
74 |
+
samples = samples[:max_kmeans_samples, :]
|
75 |
+
means = sample_vectors(samples, num_clusters)
|
76 |
+
|
77 |
+
print("kmeans start ... ")
|
78 |
+
for _ in tqdm(range(num_iters)):
|
79 |
+
diffs = rearrange(samples, "n d -> n () d") - rearrange(means, "c d -> () c d")
|
80 |
+
dists = -(diffs**2).sum(dim=-1)
|
81 |
+
|
82 |
+
buckets = dists.max(dim=-1).indices
|
83 |
+
bins = torch.bincount(buckets, minlength=num_clusters)
|
84 |
+
zero_mask = bins == 0
|
85 |
+
bins_min_clamped = bins.masked_fill(zero_mask, 1)
|
86 |
+
|
87 |
+
new_means = buckets.new_zeros(num_clusters, dim, dtype=dtype)
|
88 |
+
new_means.scatter_add_(0, repeat(buckets, "n -> n d", d=dim), samples)
|
89 |
+
new_means = new_means / bins_min_clamped[..., None]
|
90 |
+
|
91 |
+
means = torch.where(zero_mask[..., None], means, new_means)
|
92 |
+
|
93 |
+
return means, bins
|
94 |
+
|
95 |
+
|
96 |
+
class EuclideanCodebook(nn.Module):
|
97 |
+
"""Codebook with Euclidean distance.
|
98 |
+
Args:
|
99 |
+
dim (int): Dimension.
|
100 |
+
codebook_size (int): Codebook size.
|
101 |
+
kmeans_init (bool): Whether to use k-means to initialize the codebooks.
|
102 |
+
If set to true, run the k-means algorithm on the first training batch and use
|
103 |
+
the learned centroids as initialization.
|
104 |
+
kmeans_iters (int): Number of iterations used for k-means algorithm at initialization.
|
105 |
+
decay (float): Decay for exponential moving average over the codebooks.
|
106 |
+
epsilon (float): Epsilon value for numerical stability.
|
107 |
+
threshold_ema_dead_code (int): Threshold for dead code expiration. Replace any codes
|
108 |
+
that have an exponential moving average cluster size less than the specified threshold with
|
109 |
+
randomly selected vector from the current batch.
|
110 |
+
"""
|
111 |
+
|
112 |
+
def __init__(
|
113 |
+
self,
|
114 |
+
dim: int,
|
115 |
+
codebook_size: int,
|
116 |
+
kmeans_init: int = False,
|
117 |
+
kmeans_iters: int = 10,
|
118 |
+
decay: float = 0.99,
|
119 |
+
epsilon: float = 1e-5,
|
120 |
+
threshold_ema_dead_code: int = 2,
|
121 |
+
):
|
122 |
+
super().__init__()
|
123 |
+
self.decay = decay
|
124 |
+
init_fn: tp.Union[tp.Callable[..., torch.Tensor], tp.Any] = (
|
125 |
+
uniform_init if not kmeans_init else torch.zeros
|
126 |
+
)
|
127 |
+
embed = init_fn(codebook_size, dim)
|
128 |
+
|
129 |
+
self.codebook_size = codebook_size
|
130 |
+
|
131 |
+
self.kmeans_iters = kmeans_iters
|
132 |
+
self.epsilon = epsilon
|
133 |
+
self.threshold_ema_dead_code = threshold_ema_dead_code
|
134 |
+
|
135 |
+
self.register_buffer("inited", torch.Tensor([not kmeans_init]))
|
136 |
+
self.register_buffer("cluster_size", torch.zeros(codebook_size))
|
137 |
+
self.register_buffer("embed", embed)
|
138 |
+
self.register_buffer("embed_avg", embed.clone())
|
139 |
+
|
140 |
+
@torch.jit.ignore
|
141 |
+
def init_embed_(self, data):
|
142 |
+
if self.inited:
|
143 |
+
return
|
144 |
+
|
145 |
+
embed, cluster_size = kmeans(data, self.codebook_size, self.kmeans_iters)
|
146 |
+
self.embed.data.copy_(embed)
|
147 |
+
self.embed_avg.data.copy_(embed.clone())
|
148 |
+
self.cluster_size.data.copy_(cluster_size)
|
149 |
+
self.inited.data.copy_(torch.Tensor([True]))
|
150 |
+
# Make sure all buffers across workers are in sync after initialization
|
151 |
+
# broadcast_tensors(self.buffers())
|
152 |
+
|
153 |
+
def replace_(self, samples, mask):
|
154 |
+
modified_codebook = torch.where(
|
155 |
+
mask[..., None], sample_vectors(samples, self.codebook_size), self.embed
|
156 |
+
)
|
157 |
+
self.embed.data.copy_(modified_codebook)
|
158 |
+
|
159 |
+
def expire_codes_(self, batch_samples):
|
160 |
+
if self.threshold_ema_dead_code == 0:
|
161 |
+
return
|
162 |
+
|
163 |
+
expired_codes = self.cluster_size < self.threshold_ema_dead_code
|
164 |
+
if not torch.any(expired_codes):
|
165 |
+
return
|
166 |
+
|
167 |
+
batch_samples = rearrange(batch_samples, "... d -> (...) d")
|
168 |
+
self.replace_(batch_samples, mask=expired_codes)
|
169 |
+
# broadcast_tensors(self.buffers())
|
170 |
+
|
171 |
+
def preprocess(self, x):
|
172 |
+
x = rearrange(x, "... d -> (...) d")
|
173 |
+
return x
|
174 |
+
|
175 |
+
def quantize(self, x):
|
176 |
+
embed = self.embed.t()
|
177 |
+
dist = -(
|
178 |
+
x.pow(2).sum(1, keepdim=True)
|
179 |
+
- 2 * x @ embed
|
180 |
+
+ embed.pow(2).sum(0, keepdim=True)
|
181 |
+
)
|
182 |
+
embed_ind = dist.max(dim=-1).indices
|
183 |
+
return embed_ind
|
184 |
+
|
185 |
+
def postprocess_emb(self, embed_ind, shape):
|
186 |
+
return embed_ind.view(*shape[:-1])
|
187 |
+
|
188 |
+
def dequantize(self, embed_ind):
|
189 |
+
quantize = F.embedding(embed_ind, self.embed)
|
190 |
+
return quantize
|
191 |
+
|
192 |
+
def encode(self, x):
|
193 |
+
shape = x.shape
|
194 |
+
# pre-process
|
195 |
+
x = self.preprocess(x)
|
196 |
+
# quantize
|
197 |
+
embed_ind = self.quantize(x)
|
198 |
+
# post-process
|
199 |
+
embed_ind = self.postprocess_emb(embed_ind, shape)
|
200 |
+
return embed_ind
|
201 |
+
|
202 |
+
def decode(self, embed_ind):
|
203 |
+
quantize = self.dequantize(embed_ind)
|
204 |
+
return quantize
|
205 |
+
|
206 |
+
def forward(self, x):
|
207 |
+
shape, dtype = x.shape, x.dtype
|
208 |
+
x = self.preprocess(x)
|
209 |
+
|
210 |
+
self.init_embed_(x)
|
211 |
+
|
212 |
+
embed_ind = self.quantize(x)
|
213 |
+
embed_onehot = F.one_hot(embed_ind, self.codebook_size).type(dtype)
|
214 |
+
embed_ind = self.postprocess_emb(embed_ind, shape)
|
215 |
+
quantize = self.dequantize(embed_ind)
|
216 |
+
|
217 |
+
if self.training:
|
218 |
+
# We do the expiry of code at that point as buffers are in sync
|
219 |
+
# and all the workers will take the same decision.
|
220 |
+
self.expire_codes_(x)
|
221 |
+
ema_inplace(self.cluster_size, embed_onehot.sum(0), self.decay)
|
222 |
+
embed_sum = x.t() @ embed_onehot
|
223 |
+
ema_inplace(self.embed_avg, embed_sum.t(), self.decay)
|
224 |
+
cluster_size = (
|
225 |
+
laplace_smoothing(self.cluster_size, self.codebook_size, self.epsilon)
|
226 |
+
* self.cluster_size.sum()
|
227 |
+
)
|
228 |
+
embed_normalized = self.embed_avg / cluster_size.unsqueeze(1)
|
229 |
+
self.embed.data.copy_(embed_normalized)
|
230 |
+
|
231 |
+
return quantize, embed_ind
|
232 |
+
|
233 |
+
|
234 |
+
class VectorQuantization(nn.Module):
|
235 |
+
"""Vector quantization implementation.
|
236 |
+
Currently supports only euclidean distance.
|
237 |
+
Args:
|
238 |
+
dim (int): Dimension
|
239 |
+
codebook_size (int): Codebook size
|
240 |
+
codebook_dim (int): Codebook dimension. If not defined, uses the specified dimension in dim.
|
241 |
+
decay (float): Decay for exponential moving average over the codebooks.
|
242 |
+
epsilon (float): Epsilon value for numerical stability.
|
243 |
+
kmeans_init (bool): Whether to use kmeans to initialize the codebooks.
|
244 |
+
kmeans_iters (int): Number of iterations used for kmeans initialization.
|
245 |
+
threshold_ema_dead_code (int): Threshold for dead code expiration. Replace any codes
|
246 |
+
that have an exponential moving average cluster size less than the specified threshold with
|
247 |
+
randomly selected vector from the current batch.
|
248 |
+
commitment_weight (float): Weight for commitment loss.
|
249 |
+
"""
|
250 |
+
|
251 |
+
def __init__(
|
252 |
+
self,
|
253 |
+
dim: int,
|
254 |
+
codebook_size: int,
|
255 |
+
codebook_dim: tp.Optional[int] = None,
|
256 |
+
decay: float = 0.99,
|
257 |
+
epsilon: float = 1e-5,
|
258 |
+
kmeans_init: bool = True,
|
259 |
+
kmeans_iters: int = 50,
|
260 |
+
threshold_ema_dead_code: int = 2,
|
261 |
+
commitment_weight: float = 1.0,
|
262 |
+
):
|
263 |
+
super().__init__()
|
264 |
+
_codebook_dim: int = default(codebook_dim, dim)
|
265 |
+
|
266 |
+
requires_projection = _codebook_dim != dim
|
267 |
+
self.project_in = (
|
268 |
+
nn.Linear(dim, _codebook_dim) if requires_projection else nn.Identity()
|
269 |
+
)
|
270 |
+
self.project_out = (
|
271 |
+
nn.Linear(_codebook_dim, dim) if requires_projection else nn.Identity()
|
272 |
+
)
|
273 |
+
|
274 |
+
self.epsilon = epsilon
|
275 |
+
self.commitment_weight = commitment_weight
|
276 |
+
|
277 |
+
self._codebook = EuclideanCodebook(
|
278 |
+
dim=_codebook_dim,
|
279 |
+
codebook_size=codebook_size,
|
280 |
+
kmeans_init=kmeans_init,
|
281 |
+
kmeans_iters=kmeans_iters,
|
282 |
+
decay=decay,
|
283 |
+
epsilon=epsilon,
|
284 |
+
threshold_ema_dead_code=threshold_ema_dead_code,
|
285 |
+
)
|
286 |
+
self.codebook_size = codebook_size
|
287 |
+
|
288 |
+
@property
|
289 |
+
def codebook(self):
|
290 |
+
return self._codebook.embed
|
291 |
+
|
292 |
+
def encode(self, x):
|
293 |
+
x = rearrange(x, "b d n -> b n d")
|
294 |
+
x = self.project_in(x)
|
295 |
+
embed_in = self._codebook.encode(x)
|
296 |
+
return embed_in
|
297 |
+
|
298 |
+
def decode(self, embed_ind):
|
299 |
+
quantize = self._codebook.decode(embed_ind)
|
300 |
+
quantize = self.project_out(quantize)
|
301 |
+
quantize = rearrange(quantize, "b n d -> b d n")
|
302 |
+
return quantize
|
303 |
+
|
304 |
+
def forward(self, x):
|
305 |
+
device = x.device
|
306 |
+
x = rearrange(x, "b d n -> b n d")
|
307 |
+
x = self.project_in(x)
|
308 |
+
|
309 |
+
quantize, embed_ind = self._codebook(x)
|
310 |
+
|
311 |
+
if self.training:
|
312 |
+
quantize = x + (quantize - x).detach()
|
313 |
+
|
314 |
+
loss = torch.tensor([0.0], device=device, requires_grad=self.training)
|
315 |
+
|
316 |
+
if self.training:
|
317 |
+
if self.commitment_weight > 0:
|
318 |
+
commit_loss = F.mse_loss(quantize.detach(), x)
|
319 |
+
loss = loss + commit_loss * self.commitment_weight
|
320 |
+
|
321 |
+
quantize = self.project_out(quantize)
|
322 |
+
quantize = rearrange(quantize, "b n d -> b d n")
|
323 |
+
return quantize, embed_ind, loss
|
324 |
+
|
325 |
+
|
326 |
+
class ResidualVectorQuantization(nn.Module):
|
327 |
+
"""Residual vector quantization implementation.
|
328 |
+
Follows Algorithm 1. in https://arxiv.org/pdf/2107.03312.pdf
|
329 |
+
"""
|
330 |
+
|
331 |
+
def __init__(self, *, num_quantizers, **kwargs):
|
332 |
+
super().__init__()
|
333 |
+
self.layers = nn.ModuleList(
|
334 |
+
[VectorQuantization(**kwargs) for _ in range(num_quantizers)]
|
335 |
+
)
|
336 |
+
|
337 |
+
def forward(
|
338 |
+
self, x, n_q: tp.Optional[int] = None, layers: tp.Optional[list] = None
|
339 |
+
):
|
340 |
+
quantized_out = 0.0
|
341 |
+
residual = x
|
342 |
+
|
343 |
+
all_losses = []
|
344 |
+
all_indices = []
|
345 |
+
out_quantized = []
|
346 |
+
|
347 |
+
n_q = n_q or len(self.layers)
|
348 |
+
|
349 |
+
for i, layer in enumerate(self.layers[:n_q]):
|
350 |
+
quantized, indices, loss = layer(residual)
|
351 |
+
residual = residual - quantized
|
352 |
+
quantized_out = quantized_out + quantized
|
353 |
+
|
354 |
+
all_indices.append(indices)
|
355 |
+
all_losses.append(loss)
|
356 |
+
if layers and i in layers:
|
357 |
+
out_quantized.append(quantized)
|
358 |
+
|
359 |
+
out_losses, out_indices = map(torch.stack, (all_losses, all_indices))
|
360 |
+
return quantized_out, out_indices, out_losses, out_quantized
|
361 |
+
|
362 |
+
def encode(
|
363 |
+
self, x: torch.Tensor, n_q: tp.Optional[int] = None, st: tp.Optional[int] = None
|
364 |
+
) -> torch.Tensor:
|
365 |
+
residual = x
|
366 |
+
all_indices = []
|
367 |
+
n_q = n_q or len(self.layers)
|
368 |
+
st = st or 0
|
369 |
+
for layer in self.layers[st:n_q]:
|
370 |
+
indices = layer.encode(residual)
|
371 |
+
quantized = layer.decode(indices)
|
372 |
+
residual = residual - quantized
|
373 |
+
all_indices.append(indices)
|
374 |
+
out_indices = torch.stack(all_indices)
|
375 |
+
return out_indices
|
376 |
+
|
377 |
+
def decode(self, q_indices: torch.Tensor, st: int = 0) -> torch.Tensor:
|
378 |
+
quantized_out = torch.tensor(0.0, device=q_indices.device)
|
379 |
+
for i, indices in enumerate(q_indices):
|
380 |
+
layer = self.layers[st + i]
|
381 |
+
quantized = layer.decode(indices)
|
382 |
+
quantized_out = quantized_out + quantized
|
383 |
+
return quantized_out
|
embedding.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# modified from https://github.com/lifeiteng/vall-e/blob/main/valle/modules/embedding.py
|
2 |
+
import math
|
3 |
+
|
4 |
+
import torch
|
5 |
+
from torch import nn
|
6 |
+
|
7 |
+
|
8 |
+
class TokenEmbedding(nn.Module):
|
9 |
+
def __init__(
|
10 |
+
self,
|
11 |
+
embedding_dim: int,
|
12 |
+
vocab_size: int,
|
13 |
+
dropout: float = 0.0,
|
14 |
+
):
|
15 |
+
super().__init__()
|
16 |
+
|
17 |
+
self.vocab_size = vocab_size
|
18 |
+
self.embedding_dim = embedding_dim
|
19 |
+
|
20 |
+
self.dropout = torch.nn.Dropout(p=dropout)
|
21 |
+
self.word_embeddings = nn.Embedding(self.vocab_size, self.embedding_dim)
|
22 |
+
|
23 |
+
@property
|
24 |
+
def weight(self) -> torch.Tensor:
|
25 |
+
return self.word_embeddings.weight
|
26 |
+
|
27 |
+
def embedding(self, index: int) -> torch.Tensor:
|
28 |
+
return self.word_embeddings.weight[index : index + 1]
|
29 |
+
|
30 |
+
def forward(self, x: torch.Tensor):
|
31 |
+
x = self.word_embeddings(x)
|
32 |
+
x = self.dropout(x)
|
33 |
+
return x
|
34 |
+
|
35 |
+
|
36 |
+
class SinePositionalEmbedding(nn.Module):
|
37 |
+
def __init__(
|
38 |
+
self,
|
39 |
+
embedding_dim: int,
|
40 |
+
dropout: float = 0.0,
|
41 |
+
scale: bool = False,
|
42 |
+
alpha: bool = False,
|
43 |
+
):
|
44 |
+
super().__init__()
|
45 |
+
self.embedding_dim = embedding_dim
|
46 |
+
self.x_scale = math.sqrt(embedding_dim) if scale else 1.0
|
47 |
+
self.alpha = nn.Parameter(torch.ones(1), requires_grad=alpha)
|
48 |
+
self.dropout = torch.nn.Dropout(p=dropout)
|
49 |
+
|
50 |
+
self.reverse = False
|
51 |
+
self.pe = None
|
52 |
+
self.extend_pe(torch.tensor(0.0).expand(1, 4000))
|
53 |
+
|
54 |
+
def extend_pe(self, x):
|
55 |
+
"""Reset the positional encodings."""
|
56 |
+
if self.pe is not None:
|
57 |
+
if self.pe.size(1) >= x.size(1):
|
58 |
+
if self.pe.dtype != x.dtype or self.pe.device != x.device:
|
59 |
+
self.pe = self.pe.to(dtype=x.dtype, device=x.device)
|
60 |
+
return
|
61 |
+
pe = torch.zeros(x.size(1), self.embedding_dim)
|
62 |
+
if self.reverse:
|
63 |
+
position = torch.arange(
|
64 |
+
x.size(1) - 1, -1, -1.0, dtype=torch.float32
|
65 |
+
).unsqueeze(1)
|
66 |
+
else:
|
67 |
+
position = torch.arange(0, x.size(1), dtype=torch.float32).unsqueeze(1)
|
68 |
+
div_term = torch.exp(
|
69 |
+
torch.arange(0, self.embedding_dim, 2, dtype=torch.float32)
|
70 |
+
* -(math.log(10000.0) / self.embedding_dim)
|
71 |
+
)
|
72 |
+
pe[:, 0::2] = torch.sin(position * div_term)
|
73 |
+
pe[:, 1::2] = torch.cos(position * div_term)
|
74 |
+
pe = pe.unsqueeze(0)
|
75 |
+
self.pe = pe.to(device=x.device, dtype=x.dtype).detach()
|
76 |
+
|
77 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
78 |
+
self.extend_pe(x)
|
79 |
+
output = x.unsqueeze(-1) if x.ndim == 2 else x
|
80 |
+
output = output * self.x_scale + self.alpha * self.pe[:, : x.size(1)]
|
81 |
+
return self.dropout(output)
|
lr_schedulers.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# modified from https://github.com/feng-yufei/shared_debugging_code/blob/main/model/lr_schedulers.py
|
2 |
+
import math
|
3 |
+
|
4 |
+
import torch
|
5 |
+
from matplotlib import pyplot as plt
|
6 |
+
from torch import nn
|
7 |
+
from torch.optim import Adam
|
8 |
+
|
9 |
+
|
10 |
+
class WarmupCosineLRSchedule(torch.optim.lr_scheduler._LRScheduler):
|
11 |
+
"""
|
12 |
+
Implements Warmup learning rate schedule until 'warmup_steps', going from 'init_lr' to 'peak_lr' for multiple optimizers.
|
13 |
+
"""
|
14 |
+
|
15 |
+
def __init__(
|
16 |
+
self,
|
17 |
+
optimizer,
|
18 |
+
init_lr,
|
19 |
+
peak_lr,
|
20 |
+
end_lr,
|
21 |
+
warmup_steps=10000,
|
22 |
+
total_steps=400000,
|
23 |
+
current_step=0,
|
24 |
+
):
|
25 |
+
self.init_lr = init_lr
|
26 |
+
self.peak_lr = peak_lr
|
27 |
+
self.end_lr = end_lr
|
28 |
+
self.optimizer = optimizer
|
29 |
+
self._warmup_rate = (peak_lr - init_lr) / warmup_steps
|
30 |
+
self._decay_rate = (end_lr - peak_lr) / (total_steps - warmup_steps)
|
31 |
+
self._current_step = current_step
|
32 |
+
self.lr = init_lr
|
33 |
+
self.warmup_steps = warmup_steps
|
34 |
+
self.total_steps = total_steps
|
35 |
+
self._last_lr = [self.lr]
|
36 |
+
|
37 |
+
def set_lr(self, lr):
|
38 |
+
self._last_lr = [g["lr"] for g in self.optimizer.param_groups]
|
39 |
+
for g in self.optimizer.param_groups:
|
40 |
+
# g['lr'] = lr
|
41 |
+
g["lr"] = self.end_lr ###锁定用线性
|
42 |
+
|
43 |
+
def step(self):
|
44 |
+
if self._current_step < self.warmup_steps:
|
45 |
+
lr = self.init_lr + self._warmup_rate * self._current_step
|
46 |
+
|
47 |
+
elif self._current_step > self.total_steps:
|
48 |
+
lr = self.end_lr
|
49 |
+
|
50 |
+
else:
|
51 |
+
decay_ratio = (self._current_step - self.warmup_steps) / (
|
52 |
+
self.total_steps - self.warmup_steps
|
53 |
+
)
|
54 |
+
if decay_ratio < 0.0 or decay_ratio > 1.0:
|
55 |
+
raise RuntimeError(
|
56 |
+
"Decay ratio must be in [0.0, 1.0]. Fix LR scheduler settings."
|
57 |
+
)
|
58 |
+
coeff = 0.5 * (1.0 + math.cos(math.pi * decay_ratio))
|
59 |
+
lr = self.end_lr + coeff * (self.peak_lr - self.end_lr)
|
60 |
+
|
61 |
+
self.lr = lr = self.end_lr = 0.002 ###锁定用线性###不听话,直接锁定!
|
62 |
+
self.set_lr(lr)
|
63 |
+
self.lr = lr
|
64 |
+
self._current_step += 1
|
65 |
+
return self.lr
|
66 |
+
|
67 |
+
|
68 |
+
if __name__ == "__main__":
|
69 |
+
m = nn.Linear(10, 10)
|
70 |
+
opt = Adam(m.parameters(), lr=1e-4)
|
71 |
+
s = WarmupCosineLRSchedule(
|
72 |
+
opt, 1e-6, 2e-4, 1e-6, warmup_steps=2000, total_steps=20000, current_step=0
|
73 |
+
)
|
74 |
+
lrs = []
|
75 |
+
for i in range(25000):
|
76 |
+
s.step()
|
77 |
+
lrs.append(s.lr)
|
78 |
+
print(s.lr)
|
79 |
+
|
80 |
+
plt.plot(lrs)
|
81 |
+
plt.plot(range(0, 25000), lrs)
|
82 |
+
plt.show()
|
mel_processing.py
ADDED
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.utils.data
|
3 |
+
from librosa.filters import mel as librosa_mel_fn
|
4 |
+
|
5 |
+
MAX_WAV_VALUE = 32768.0
|
6 |
+
|
7 |
+
|
8 |
+
def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):
|
9 |
+
"""
|
10 |
+
PARAMS
|
11 |
+
------
|
12 |
+
C: compression factor
|
13 |
+
"""
|
14 |
+
return torch.log(torch.clamp(x, min=clip_val) * C)
|
15 |
+
|
16 |
+
|
17 |
+
def dynamic_range_decompression_torch(x, C=1):
|
18 |
+
"""
|
19 |
+
PARAMS
|
20 |
+
------
|
21 |
+
C: compression factor used to compress
|
22 |
+
"""
|
23 |
+
return torch.exp(x) / C
|
24 |
+
|
25 |
+
|
26 |
+
def spectral_normalize_torch(magnitudes):
|
27 |
+
output = dynamic_range_compression_torch(magnitudes)
|
28 |
+
return output
|
29 |
+
|
30 |
+
|
31 |
+
def spectral_de_normalize_torch(magnitudes):
|
32 |
+
output = dynamic_range_decompression_torch(magnitudes)
|
33 |
+
return output
|
34 |
+
|
35 |
+
|
36 |
+
mel_basis = {}
|
37 |
+
hann_window = {}
|
38 |
+
|
39 |
+
|
40 |
+
def spectrogram_torch(y, n_fft, sampling_rate, hop_size, win_size, center=False):
|
41 |
+
if torch.min(y) < -1.0:
|
42 |
+
print("min value is ", torch.min(y))
|
43 |
+
if torch.max(y) > 1.0:
|
44 |
+
print("max value is ", torch.max(y))
|
45 |
+
|
46 |
+
global hann_window
|
47 |
+
dtype_device = str(y.dtype) + "_" + str(y.device)
|
48 |
+
wnsize_dtype_device = str(win_size) + "_" + dtype_device
|
49 |
+
if wnsize_dtype_device not in hann_window:
|
50 |
+
hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(
|
51 |
+
dtype=y.dtype, device=y.device
|
52 |
+
)
|
53 |
+
|
54 |
+
y = torch.nn.functional.pad(
|
55 |
+
y.unsqueeze(1),
|
56 |
+
(int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)),
|
57 |
+
mode="reflect",
|
58 |
+
)
|
59 |
+
y = y.squeeze(1)
|
60 |
+
spec = torch.stft(
|
61 |
+
y,
|
62 |
+
n_fft,
|
63 |
+
hop_length=hop_size,
|
64 |
+
win_length=win_size,
|
65 |
+
window=hann_window[wnsize_dtype_device],
|
66 |
+
center=center,
|
67 |
+
pad_mode="reflect",
|
68 |
+
normalized=False,
|
69 |
+
onesided=True,
|
70 |
+
return_complex=False,
|
71 |
+
)
|
72 |
+
|
73 |
+
spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)
|
74 |
+
return spec
|
75 |
+
|
76 |
+
|
77 |
+
def spec_to_mel_torch(spec, n_fft, num_mels, sampling_rate, fmin, fmax):
|
78 |
+
global mel_basis
|
79 |
+
dtype_device = str(spec.dtype) + "_" + str(spec.device)
|
80 |
+
fmax_dtype_device = str(fmax) + "_" + dtype_device
|
81 |
+
if fmax_dtype_device not in mel_basis:
|
82 |
+
mel = librosa_mel_fn(
|
83 |
+
sr=sampling_rate, n_fft=n_fft, n_mels=num_mels, fmin=fmin, fmax=fmax
|
84 |
+
)
|
85 |
+
mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(
|
86 |
+
dtype=spec.dtype, device=spec.device
|
87 |
+
)
|
88 |
+
spec = torch.matmul(mel_basis[fmax_dtype_device], spec)
|
89 |
+
spec = spectral_normalize_torch(spec)
|
90 |
+
return spec
|
91 |
+
|
92 |
+
|
93 |
+
def mel_spectrogram_torch(
|
94 |
+
y, n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax, center=False
|
95 |
+
):
|
96 |
+
if torch.min(y) < -1.0:
|
97 |
+
print("min value is ", torch.min(y))
|
98 |
+
if torch.max(y) > 1.0:
|
99 |
+
print("max value is ", torch.max(y))
|
100 |
+
|
101 |
+
global mel_basis, hann_window
|
102 |
+
dtype_device = str(y.dtype) + "_" + str(y.device)
|
103 |
+
fmax_dtype_device = str(fmax) + "_" + dtype_device
|
104 |
+
wnsize_dtype_device = str(win_size) + "_" + dtype_device
|
105 |
+
if fmax_dtype_device not in mel_basis:
|
106 |
+
mel = librosa_mel_fn(
|
107 |
+
sr=sampling_rate, n_fft=n_fft, n_mels=num_mels, fmin=fmin, fmax=fmax
|
108 |
+
)
|
109 |
+
mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(
|
110 |
+
dtype=y.dtype, device=y.device
|
111 |
+
)
|
112 |
+
if wnsize_dtype_device not in hann_window:
|
113 |
+
hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(
|
114 |
+
dtype=y.dtype, device=y.device
|
115 |
+
)
|
116 |
+
|
117 |
+
y = torch.nn.functional.pad(
|
118 |
+
y.unsqueeze(1),
|
119 |
+
(int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)),
|
120 |
+
mode="reflect",
|
121 |
+
)
|
122 |
+
y = y.squeeze(1)
|
123 |
+
|
124 |
+
spec = torch.stft(
|
125 |
+
y,
|
126 |
+
n_fft,
|
127 |
+
hop_length=hop_size,
|
128 |
+
win_length=win_size,
|
129 |
+
window=hann_window[wnsize_dtype_device],
|
130 |
+
center=center,
|
131 |
+
pad_mode="reflect",
|
132 |
+
normalized=False,
|
133 |
+
onesided=True,
|
134 |
+
return_complex=False,
|
135 |
+
)
|
136 |
+
|
137 |
+
spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)
|
138 |
+
|
139 |
+
spec = torch.matmul(mel_basis[fmax_dtype_device], spec)
|
140 |
+
spec = spectral_normalize_torch(spec)
|
141 |
+
|
142 |
+
return spec
|
model_utils.py
ADDED
@@ -0,0 +1,228 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# modified from https://github.com/feng-yufei/shared_debugging_code/blob/main/model/utils.py\
|
2 |
+
import torch
|
3 |
+
import torch.nn.functional as F
|
4 |
+
from typing import Tuple
|
5 |
+
|
6 |
+
def sequence_mask(length, max_length=None):
|
7 |
+
if max_length is None:
|
8 |
+
max_length = length.max()
|
9 |
+
x = torch.arange(max_length, dtype=length.dtype, device=length.device)
|
10 |
+
return x.unsqueeze(0) < length.unsqueeze(1)
|
11 |
+
|
12 |
+
|
13 |
+
def make_pad_mask(lengths: torch.Tensor, max_len: int = 0) -> torch.Tensor:
|
14 |
+
"""
|
15 |
+
Args:
|
16 |
+
lengths:
|
17 |
+
A 1-D tensor containing sentence lengths.
|
18 |
+
max_len:
|
19 |
+
The length of masks.
|
20 |
+
Returns:
|
21 |
+
Return a 2-D bool tensor, where masked positions
|
22 |
+
are filled with `True` and non-masked positions are
|
23 |
+
filled with `False`.
|
24 |
+
|
25 |
+
#>>> lengths = torch.tensor([1, 3, 2, 5])
|
26 |
+
#>>> make_pad_mask(lengths)
|
27 |
+
tensor([[False, True, True, True, True],
|
28 |
+
[False, False, False, True, True],
|
29 |
+
[False, False, True, True, True],
|
30 |
+
[False, False, False, False, False]])
|
31 |
+
"""
|
32 |
+
assert lengths.ndim == 1, lengths.ndim
|
33 |
+
max_len = max(max_len, lengths.max())
|
34 |
+
n = lengths.size(0)
|
35 |
+
seq_range = torch.arange(0, max_len, device=lengths.device)
|
36 |
+
expaned_lengths = seq_range.unsqueeze(0).expand(n, max_len)
|
37 |
+
|
38 |
+
return expaned_lengths >= lengths.unsqueeze(-1)
|
39 |
+
|
40 |
+
|
41 |
+
# https://github.com/microsoft/unilm/blob/master/xtune/src/transformers/modeling_utils.py
|
42 |
+
def top_k_top_p_filtering(
|
43 |
+
logits, top_k=0, top_p=1.0, filter_value=-float("Inf"), min_tokens_to_keep=1
|
44 |
+
):
|
45 |
+
"""Filter a distribution of logits using top-k and/or nucleus (top-p) filtering
|
46 |
+
Args:
|
47 |
+
logits: logits distribution shape (batch size, vocabulary size)
|
48 |
+
if top_k > 0: keep only top k tokens with highest probability (top-k filtering).
|
49 |
+
if top_p < 1.0: keep the top tokens with cumulative probability >= top_p (nucleus filtering).
|
50 |
+
Nucleus filtering is described in Holtzman et al. (http://arxiv.org/abs/1904.09751)
|
51 |
+
Make sure we keep at least min_tokens_to_keep per batch example in the output
|
52 |
+
From: https://gist.github.com/thomwolf/1a5a29f6962089e871b94cbd09daf317
|
53 |
+
"""
|
54 |
+
if top_k > 0:
|
55 |
+
top_k = min(max(top_k, min_tokens_to_keep), logits.size(-1)) # Safety check
|
56 |
+
# Remove all tokens with a probability less than the last token of the top-k
|
57 |
+
indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None]
|
58 |
+
logits[indices_to_remove] = filter_value
|
59 |
+
|
60 |
+
if top_p < 1.0:
|
61 |
+
sorted_logits, sorted_indices = torch.sort(logits, descending=True)
|
62 |
+
cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
|
63 |
+
|
64 |
+
# Remove tokens with cumulative probability above the threshold (token with 0 are kept)
|
65 |
+
sorted_indices_to_remove = cumulative_probs > top_p
|
66 |
+
if min_tokens_to_keep > 1:
|
67 |
+
# Keep at least min_tokens_to_keep (set to min_tokens_to_keep-1 because we add the first one below)
|
68 |
+
sorted_indices_to_remove[..., :min_tokens_to_keep] = 0
|
69 |
+
# Shift the indices to the right to keep also the first token above the threshold
|
70 |
+
sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
|
71 |
+
sorted_indices_to_remove[..., 0] = 0
|
72 |
+
|
73 |
+
# scatter sorted tensors to original indexing
|
74 |
+
indices_to_remove = sorted_indices_to_remove.scatter(
|
75 |
+
1, sorted_indices, sorted_indices_to_remove
|
76 |
+
)
|
77 |
+
logits[indices_to_remove] = filter_value
|
78 |
+
return logits
|
79 |
+
|
80 |
+
|
81 |
+
def topk_sampling(logits, top_k=10, top_p=1.0, temperature=1.0):
|
82 |
+
# temperature: (`optional`) float
|
83 |
+
# The value used to module the next token probabilities. Must be strictly positive. Default to 1.0.
|
84 |
+
# top_k: (`optional`) int
|
85 |
+
# The number of highest probability vocabulary tokens to keep for top-k-filtering. Between 1 and infinity. Default to 50.
|
86 |
+
# top_p: (`optional`) float
|
87 |
+
# The cumulative probability of parameter highest probability vocabulary tokens to keep for nucleus sampling. Must be between 0 and 1. Default to 1.
|
88 |
+
|
89 |
+
# Temperature (higher temperature => more likely to sample low probability tokens)
|
90 |
+
if temperature != 1.0:
|
91 |
+
logits = logits / temperature
|
92 |
+
# Top-p/top-k filtering
|
93 |
+
logits = top_k_top_p_filtering(logits, top_k=top_k, top_p=top_p)
|
94 |
+
# Sample
|
95 |
+
token = torch.multinomial(F.softmax(logits, dim=-1), num_samples=1)
|
96 |
+
return token
|
97 |
+
|
98 |
+
|
99 |
+
from typing import Optional, Tuple
|
100 |
+
|
101 |
+
|
102 |
+
def multinomial_sample_one_no_sync(
|
103 |
+
probs_sort,
|
104 |
+
): # Does multinomial sampling without a cuda synchronization
|
105 |
+
q = torch.empty_like(probs_sort).exponential_(1)
|
106 |
+
return torch.argmax(probs_sort / q, dim=-1, keepdim=True).to(dtype=torch.int)
|
107 |
+
|
108 |
+
|
109 |
+
def logits_to_probs(
|
110 |
+
logits,
|
111 |
+
previous_tokens: Optional[torch.Tensor] = None,
|
112 |
+
temperature: float = 1.0,
|
113 |
+
top_k: Optional[int] = None,
|
114 |
+
top_p: Optional[int] = None,
|
115 |
+
repetition_penalty: float = 1.0,
|
116 |
+
):
|
117 |
+
if previous_tokens is not None:
|
118 |
+
previous_tokens = previous_tokens.squeeze()
|
119 |
+
# print(logits.shape,previous_tokens.shape)
|
120 |
+
# pdb.set_trace()
|
121 |
+
if previous_tokens is not None and repetition_penalty != 1.0:
|
122 |
+
previous_tokens = previous_tokens.long()
|
123 |
+
score = torch.gather(logits, dim=0, index=previous_tokens)
|
124 |
+
score = torch.where(
|
125 |
+
score < 0, score * repetition_penalty, score / repetition_penalty
|
126 |
+
)
|
127 |
+
logits.scatter_(dim=0, index=previous_tokens, src=score)
|
128 |
+
|
129 |
+
if top_p is not None and top_p < 1.0:
|
130 |
+
sorted_logits, sorted_indices = torch.sort(logits, descending=True)
|
131 |
+
cum_probs = torch.cumsum(
|
132 |
+
torch.nn.functional.softmax(sorted_logits, dim=-1), dim=-1
|
133 |
+
)
|
134 |
+
sorted_indices_to_remove = cum_probs > top_p
|
135 |
+
sorted_indices_to_remove[0] = False # keep at least one option
|
136 |
+
indices_to_remove = sorted_indices_to_remove.scatter(
|
137 |
+
dim=0, index=sorted_indices, src=sorted_indices_to_remove
|
138 |
+
)
|
139 |
+
logits = logits.masked_fill(indices_to_remove, -float("Inf"))
|
140 |
+
|
141 |
+
logits = logits / max(temperature, 1e-5)
|
142 |
+
|
143 |
+
if top_k is not None:
|
144 |
+
v, _ = torch.topk(logits, min(top_k, logits.size(-1)))
|
145 |
+
pivot = v.select(-1, -1).unsqueeze(-1)
|
146 |
+
logits = torch.where(logits < pivot, -float("Inf"), logits)
|
147 |
+
|
148 |
+
probs = torch.nn.functional.softmax(logits, dim=-1)
|
149 |
+
return probs
|
150 |
+
|
151 |
+
|
152 |
+
def sample(
|
153 |
+
logits,
|
154 |
+
previous_tokens: Optional[torch.Tensor] = None,
|
155 |
+
**sampling_kwargs,
|
156 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
157 |
+
probs = logits_to_probs(
|
158 |
+
logits=logits, previous_tokens=previous_tokens, **sampling_kwargs
|
159 |
+
)
|
160 |
+
idx_next = multinomial_sample_one_no_sync(probs)
|
161 |
+
return idx_next, probs
|
162 |
+
|
163 |
+
def dpo_loss(policy_chosen_logps: torch.FloatTensor,
|
164 |
+
policy_rejected_logps: torch.FloatTensor,
|
165 |
+
reference_chosen_logps: torch.FloatTensor,
|
166 |
+
reference_rejected_logps: torch.FloatTensor,
|
167 |
+
beta: float,
|
168 |
+
reference_free: bool = False) -> Tuple[torch.FloatTensor, torch.FloatTensor, torch.FloatTensor]:
|
169 |
+
pi_logratios = policy_chosen_logps - policy_rejected_logps
|
170 |
+
ref_logratios = reference_chosen_logps - reference_rejected_logps
|
171 |
+
|
172 |
+
if reference_free:
|
173 |
+
ref_logratios = 0
|
174 |
+
|
175 |
+
logits = pi_logratios - ref_logratios
|
176 |
+
|
177 |
+
losses = -F.logsigmoid(beta * logits)
|
178 |
+
chosen_rewards = beta * (policy_chosen_logps - reference_chosen_logps).detach()
|
179 |
+
rejected_rewards = beta * (policy_rejected_logps - reference_rejected_logps).detach()
|
180 |
+
|
181 |
+
return losses.mean(), chosen_rewards, rejected_rewards
|
182 |
+
|
183 |
+
def get_batch_logps(logits_target: torch.FloatTensor, logits_reject: torch.FloatTensor, labels_target: torch.LongTensor, labels_reject: torch.LongTensor, average_log_prob: bool = False) -> Tuple[torch.FloatTensor, torch.FloatTensor]:
|
184 |
+
|
185 |
+
# dummy token; we'll ignore the losses on these tokens later
|
186 |
+
|
187 |
+
per_token_logps_target = torch.gather(logits_target.log_softmax(-1), dim=2, index=labels_target.unsqueeze(2)).squeeze(2)
|
188 |
+
per_token_logps_reject = torch.gather(logits_reject.log_softmax(-1), dim=2, index=labels_reject.unsqueeze(2)).squeeze(2)
|
189 |
+
|
190 |
+
return per_token_logps_target.sum(-1), per_token_logps_reject.sum(-1)
|
191 |
+
|
192 |
+
def make_reject_y(y_o, y_lens):
|
193 |
+
def repeat_P(y):
|
194 |
+
range_idx, _ = torch.randint(0, len(y), size=(2,)).sort()
|
195 |
+
pre = y[:range_idx[0]]
|
196 |
+
shf = y[range_idx[1]:]
|
197 |
+
range_text = y[range_idx[0]:range_idx[1]]
|
198 |
+
new_y = torch.cat([pre, range_text, range_text, shf])
|
199 |
+
return new_y
|
200 |
+
def lost_P(y):
|
201 |
+
range_idx, _ = torch.randint(0, len(y), size=(2,)).sort()
|
202 |
+
pre = y[:range_idx[0]]
|
203 |
+
shf = y[range_idx[1]:]
|
204 |
+
range_text = y[range_idx[0]:range_idx[1]]
|
205 |
+
new_y = torch.cat([pre, shf])
|
206 |
+
return new_y
|
207 |
+
bs = len(y_lens)
|
208 |
+
reject_y = []
|
209 |
+
reject_y_lens = []
|
210 |
+
for b in range(bs):
|
211 |
+
process_item_idx = torch.randint(0, 1, size=(1, ))[0]
|
212 |
+
if process_item_idx == 0:
|
213 |
+
new_y = repeat_P(y_o[b])
|
214 |
+
reject_y.append(new_y)
|
215 |
+
reject_y_lens.append(len(new_y))
|
216 |
+
elif process_item_idx==1:
|
217 |
+
new_y = lost_P(y_o[b])
|
218 |
+
reject_y.append(new_y)
|
219 |
+
reject_y_lens.append(len(new_y))
|
220 |
+
max_length = max(reject_y_lens)
|
221 |
+
for b in range(bs):
|
222 |
+
pad_length = max_length - reject_y_lens[b]
|
223 |
+
reject_y[b] = torch.cat([reject_y[b], torch.zeros(pad_length, dtype=y_o.dtype, device=y_o.device)], dim=0)
|
224 |
+
|
225 |
+
reject_y = torch.stack(reject_y, dim = 0)
|
226 |
+
reject_y_lens = torch.tensor(reject_y_lens, device=y_lens.device)
|
227 |
+
|
228 |
+
return reject_y, reject_y_lens
|
modeling_gpt_sovits.py
ADDED
@@ -0,0 +1,648 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import PreTrainedModel
|
2 |
+
from .configuration_gpt_sovits import GPTSoVITSConfig
|
3 |
+
|
4 |
+
import os
|
5 |
+
import re
|
6 |
+
import LangSegment
|
7 |
+
import torch
|
8 |
+
import librosa
|
9 |
+
import numpy as np
|
10 |
+
import soundfile as sf
|
11 |
+
from transformers import AutoModelForMaskedLM, BertConfig
|
12 |
+
|
13 |
+
from .t2s_lightning_module import \
|
14 |
+
Text2SemanticLightningModule
|
15 |
+
from . import cnhubert
|
16 |
+
from .mel_processing import spectrogram_torch
|
17 |
+
# from io import BytesIO
|
18 |
+
from .models import SynthesizerTrn
|
19 |
+
from .my_utils import load_audio
|
20 |
+
from .symbols import cleaned_text_to_sequence
|
21 |
+
from .cleaner import clean_text
|
22 |
+
|
23 |
+
from huggingface_hub import hf_hub_download
|
24 |
+
|
25 |
+
class DictToAttrRecursive(dict):
|
26 |
+
def __init__(self, input_dict):
|
27 |
+
super().__init__(input_dict)
|
28 |
+
for key, value in input_dict.items():
|
29 |
+
if isinstance(value, dict):
|
30 |
+
value = DictToAttrRecursive(value)
|
31 |
+
self[key] = value
|
32 |
+
setattr(self, key, value)
|
33 |
+
|
34 |
+
def __getattr__(self, item):
|
35 |
+
try:
|
36 |
+
return self[item]
|
37 |
+
except KeyError:
|
38 |
+
raise AttributeError(f"Attribute {item} not found")
|
39 |
+
|
40 |
+
def __setattr__(self, key, value):
|
41 |
+
if isinstance(value, dict):
|
42 |
+
value = DictToAttrRecursive(value)
|
43 |
+
super(DictToAttrRecursive, self).__setitem__(key, value)
|
44 |
+
super().__setattr__(key, value)
|
45 |
+
|
46 |
+
def __delattr__(self, item):
|
47 |
+
try:
|
48 |
+
del self[item]
|
49 |
+
except KeyError:
|
50 |
+
raise AttributeError(f"Attribute {item} not found")
|
51 |
+
|
52 |
+
dict_language = {
|
53 |
+
"中文": "all_zh",#全部按中文识别
|
54 |
+
"英文": "en",#全部按英文识别#######不变
|
55 |
+
"日文": "all_ja",#全部按日文识别
|
56 |
+
"中英混合": "zh",#按中英混合识别####不变
|
57 |
+
"日英混合": "ja",#按日英混合识别####不变
|
58 |
+
"多语种混合": "auto",#多语种启动切分识别语种
|
59 |
+
"ZH": "zh",
|
60 |
+
"EN": "en",
|
61 |
+
"JA": "ja",
|
62 |
+
"zh": "zh",
|
63 |
+
"en": "en",
|
64 |
+
"ja": "ja",
|
65 |
+
"all_zh": "all_zh", #手动添加,以防万一
|
66 |
+
"all_ja": "all_ja", #手动添加,以防万一
|
67 |
+
"auto": "auto" #手动添加,以防万一
|
68 |
+
}
|
69 |
+
|
70 |
+
splits = {
|
71 |
+
",",
|
72 |
+
"。",
|
73 |
+
"?",
|
74 |
+
"!",
|
75 |
+
",",
|
76 |
+
".",
|
77 |
+
"?",
|
78 |
+
"!",
|
79 |
+
"~",
|
80 |
+
":",
|
81 |
+
":",
|
82 |
+
"—",
|
83 |
+
"…",
|
84 |
+
} # 不考虑省略号
|
85 |
+
|
86 |
+
def splite_en_inf(sentence, language):
|
87 |
+
pattern = re.compile(r'[a-zA-Z ]+')
|
88 |
+
textlist = []
|
89 |
+
langlist = []
|
90 |
+
pos = 0
|
91 |
+
for match in pattern.finditer(sentence):
|
92 |
+
start, end = match.span()
|
93 |
+
if start > pos:
|
94 |
+
textlist.append(sentence[pos:start])
|
95 |
+
langlist.append(language)
|
96 |
+
textlist.append(sentence[start:end])
|
97 |
+
langlist.append("en")
|
98 |
+
pos = end
|
99 |
+
if pos < len(sentence):
|
100 |
+
textlist.append(sentence[pos:])
|
101 |
+
langlist.append(language)
|
102 |
+
# Merge punctuation into previous word
|
103 |
+
for i in range(len(textlist)-1, 0, -1):
|
104 |
+
if re.match(r'^[\W_]+$', textlist[i]):
|
105 |
+
textlist[i-1] += textlist[i]
|
106 |
+
del textlist[i]
|
107 |
+
del langlist[i]
|
108 |
+
# Merge consecutive words with the same language tag
|
109 |
+
i = 0
|
110 |
+
while i < len(langlist) - 1:
|
111 |
+
if langlist[i] == langlist[i+1]:
|
112 |
+
textlist[i] += textlist[i+1]
|
113 |
+
del textlist[i+1]
|
114 |
+
del langlist[i+1]
|
115 |
+
else:
|
116 |
+
i += 1
|
117 |
+
|
118 |
+
return textlist, langlist
|
119 |
+
|
120 |
+
def clean_text_inf(text, language):
|
121 |
+
formattext = ""
|
122 |
+
language = language.replace("all_","")
|
123 |
+
for tmp in LangSegment.getTexts(text):
|
124 |
+
if language == "ja":
|
125 |
+
if tmp["lang"] == language or tmp["lang"] == "zh":
|
126 |
+
formattext += tmp["text"] + " "
|
127 |
+
continue
|
128 |
+
if tmp["lang"] == language:
|
129 |
+
formattext += tmp["text"] + " "
|
130 |
+
while " " in formattext:
|
131 |
+
formattext = formattext.replace(" ", " ")
|
132 |
+
phones, word2ph, norm_text = clean_text(formattext, language)
|
133 |
+
phones = cleaned_text_to_sequence(phones)
|
134 |
+
return phones, word2ph, norm_text
|
135 |
+
|
136 |
+
|
137 |
+
|
138 |
+
def nonen_clean_text_inf(text, language):
|
139 |
+
if(language!="auto"):
|
140 |
+
textlist, langlist = splite_en_inf(text, language)
|
141 |
+
else:
|
142 |
+
textlist=[]
|
143 |
+
langlist=[]
|
144 |
+
for tmp in LangSegment.getTexts(text):
|
145 |
+
langlist.append(tmp["lang"])
|
146 |
+
textlist.append(tmp["text"])
|
147 |
+
phones_list = []
|
148 |
+
word2ph_list = []
|
149 |
+
norm_text_list = []
|
150 |
+
for i in range(len(textlist)):
|
151 |
+
lang = langlist[i]
|
152 |
+
phones, word2ph, norm_text = clean_text_inf(textlist[i], lang)
|
153 |
+
phones_list.append(phones)
|
154 |
+
if lang == "zh":
|
155 |
+
word2ph_list.append(word2ph)
|
156 |
+
norm_text_list.append(norm_text)
|
157 |
+
#【日志】 print(word2ph_list)
|
158 |
+
phones = sum(phones_list, [])
|
159 |
+
word2ph = sum(word2ph_list, [])
|
160 |
+
norm_text = ' '.join(norm_text_list)
|
161 |
+
|
162 |
+
return phones, word2ph, norm_text
|
163 |
+
|
164 |
+
def get_first(text):
|
165 |
+
pattern = "[" + "".join(re.escape(sep) for sep in splits) + "]"
|
166 |
+
text = re.split(pattern, text)[0].strip()
|
167 |
+
return text
|
168 |
+
|
169 |
+
def merge_short_text_in_array(texts, threshold):
|
170 |
+
if (len(texts)) < 2:
|
171 |
+
return texts
|
172 |
+
result = []
|
173 |
+
text = ""
|
174 |
+
for ele in texts:
|
175 |
+
text += ele
|
176 |
+
if len(text) >= threshold:
|
177 |
+
result.append(text)
|
178 |
+
text = ""
|
179 |
+
if (len(text) > 0):
|
180 |
+
if len(result) == 0:
|
181 |
+
result.append(text)
|
182 |
+
else:
|
183 |
+
result[len(result) - 1] += text
|
184 |
+
return result
|
185 |
+
|
186 |
+
# ====== 对输入文本进行切割 =========
|
187 |
+
|
188 |
+
def split(todo_text):
|
189 |
+
"""
|
190 |
+
将大段文本按标点切割,并将每段文本(保留末尾标点)组成列表。
|
191 |
+
"""
|
192 |
+
todo_text = todo_text.replace("……", "。").replace("——", ",")
|
193 |
+
if todo_text[-1] not in splits:
|
194 |
+
todo_text += "。"
|
195 |
+
i_split_head = i_split_tail = 0
|
196 |
+
len_text = len(todo_text)
|
197 |
+
todo_texts = []
|
198 |
+
while 1:
|
199 |
+
if i_split_head >= len_text:
|
200 |
+
break # 结尾一定有标点,所以直接跳出即可,最后一段在上次已加入
|
201 |
+
if todo_text[i_split_head] in splits:
|
202 |
+
i_split_head += 1
|
203 |
+
todo_texts.append(todo_text[i_split_tail:i_split_head])
|
204 |
+
i_split_tail = i_split_head
|
205 |
+
else:
|
206 |
+
i_split_head += 1
|
207 |
+
return todo_texts
|
208 |
+
|
209 |
+
|
210 |
+
def cut1(inp):
|
211 |
+
"""
|
212 |
+
第一种文本分段法:基于重写的split分割后,凑4段语句推理一次。
|
213 |
+
"""
|
214 |
+
inp = inp.strip("\n")
|
215 |
+
inps = split(inp)
|
216 |
+
split_idx = list(range(0, len(inps), 4))
|
217 |
+
split_idx[-1] = None
|
218 |
+
if len(split_idx) > 1:
|
219 |
+
opts = []
|
220 |
+
for idx in range(len(split_idx) - 1):
|
221 |
+
opts.append("".join(inps[split_idx[idx] : split_idx[idx + 1]]))
|
222 |
+
else:
|
223 |
+
opts = [inp]
|
224 |
+
return "\n".join(opts)
|
225 |
+
|
226 |
+
|
227 |
+
def cut2(inp):
|
228 |
+
"""
|
229 |
+
第二种文本分段法:基于重写split分割后,凑50个字推理一次。
|
230 |
+
"""
|
231 |
+
inp = inp.strip("\n")
|
232 |
+
inps = split(inp)
|
233 |
+
if len(inps) < 2:
|
234 |
+
return [inp]
|
235 |
+
opts = []
|
236 |
+
summ = 0
|
237 |
+
tmp_str = ""
|
238 |
+
for i in range(len(inps)):
|
239 |
+
summ += len(inps[i])
|
240 |
+
tmp_str += inps[i]
|
241 |
+
if summ > 50:
|
242 |
+
summ = 0
|
243 |
+
opts.append(tmp_str)
|
244 |
+
tmp_str = ""
|
245 |
+
if tmp_str != "":
|
246 |
+
opts.append(tmp_str)
|
247 |
+
if len(opts[-1]) < 50: ##如果最后一个太短了,和前一个合一起
|
248 |
+
opts[-2] = opts[-2] + opts[-1]
|
249 |
+
opts = opts[:-1]
|
250 |
+
return "\n".join(opts)
|
251 |
+
|
252 |
+
def cut3(inp):
|
253 |
+
"""
|
254 |
+
第三种文本分段法:仅仅按中文句号分割。
|
255 |
+
"""
|
256 |
+
inp = inp.strip("\n")
|
257 |
+
return "\n".join(["%s。" % item for item in inp.strip("。").split("。")])
|
258 |
+
|
259 |
+
# 新增两种切法
|
260 |
+
|
261 |
+
def cut4(inp):
|
262 |
+
"""
|
263 |
+
"按英文句号.切"
|
264 |
+
"""
|
265 |
+
inp = inp.strip("\n")
|
266 |
+
return "\n".join(["%s" % item for item in inp.strip(".").split(".")])
|
267 |
+
|
268 |
+
|
269 |
+
# contributed by https://github.com/AI-Hobbyist/GPT-SoVITS/blob/main/GPT_SoVITS/inference_webui.py
|
270 |
+
def cut5(inp):
|
271 |
+
"""
|
272 |
+
"按标点符号切"
|
273 |
+
"""
|
274 |
+
# if not re.search(r'[^\w\s]', inp[-1]):
|
275 |
+
# inp += '。'
|
276 |
+
inp = inp.strip("\n")
|
277 |
+
punds = r'[,.;?!、,。?!;:…]'
|
278 |
+
items = re.split(f'({punds})', inp)
|
279 |
+
mergeitems = ["".join(group) for group in zip(items[::2], items[1::2])]
|
280 |
+
# 在句子不存在符号或句尾无符号的时候保证文本完整
|
281 |
+
if len(items)%2 == 1:
|
282 |
+
mergeitems.append(items[-1])
|
283 |
+
opt = "\n".join(mergeitems)
|
284 |
+
return opt
|
285 |
+
|
286 |
+
def get_spepc(hps, filename):
|
287 |
+
audio = load_audio(filename, int(hps.data.sampling_rate))
|
288 |
+
audio = torch.FloatTensor(audio)
|
289 |
+
audio_norm = audio
|
290 |
+
audio_norm = audio_norm.unsqueeze(0)
|
291 |
+
spec = spectrogram_torch(audio_norm, hps.data.filter_length, hps.data.sampling_rate, hps.data.hop_length,
|
292 |
+
hps.data.win_length, center=False)
|
293 |
+
return spec
|
294 |
+
|
295 |
+
class GPTSoVITSModel(PreTrainedModel):
|
296 |
+
config_class = GPTSoVITSConfig
|
297 |
+
|
298 |
+
def __init__(self, config: GPTSoVITSConfig):
|
299 |
+
super().__init__(config)
|
300 |
+
self.name_or_path = config.name_or_path
|
301 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
302 |
+
try:
|
303 |
+
for file in ["opencpop-strict.txt","cmudict-fast.rep","cmudict.rep","engdict-hot.rep"]:
|
304 |
+
hf_hub_download(
|
305 |
+
repo_id=self.name_or_path,
|
306 |
+
filename=file,
|
307 |
+
repo_type="model",
|
308 |
+
local_dir=current_dir
|
309 |
+
)
|
310 |
+
except:
|
311 |
+
print("Download not executed: maybe under dev mode, please put the files in current directory")
|
312 |
+
pass
|
313 |
+
|
314 |
+
self.prompt_language = config.prompt_language
|
315 |
+
|
316 |
+
self.ssl_model = cnhubert.CNHubert(config._hubert_config_dict, config._hubert_extractor_config_dict)
|
317 |
+
self.bert_model = AutoModelForMaskedLM.from_config(BertConfig.from_dict(config._bert_config_dict))
|
318 |
+
self.hps = DictToAttrRecursive(config._hps_dict)
|
319 |
+
self.hps.model.semantic_frame_rate = "25hz"
|
320 |
+
self.gpt_config = config._gpt_config_dict
|
321 |
+
self.vq_model = SynthesizerTrn(
|
322 |
+
self.hps.data.filter_length // 2 + 1,
|
323 |
+
self.hps.train.segment_size // self.hps.data.hop_length,
|
324 |
+
n_speakers=self.hps.data.n_speakers,
|
325 |
+
**self.hps.model)
|
326 |
+
self.t2s_model = Text2SemanticLightningModule(self.gpt_config, "ojbk", is_train=False)
|
327 |
+
try:
|
328 |
+
self.ref_wav_path = hf_hub_download(
|
329 |
+
repo_id=self.name_or_path,
|
330 |
+
filename="ref.wav",
|
331 |
+
repo_type="model",
|
332 |
+
local_dir = current_dir
|
333 |
+
)
|
334 |
+
self.prompt_text_path = hf_hub_download(
|
335 |
+
repo_id=self.name_or_path,
|
336 |
+
filename="ref.txt",
|
337 |
+
repo_type="model",
|
338 |
+
local_dir = current_dir
|
339 |
+
)
|
340 |
+
except:
|
341 |
+
self.ref_wav_path = os.path.join(current_dir, "ref.wav")
|
342 |
+
self.prompt_text_path = os.path.join(current_dir, "ref.txt")
|
343 |
+
print("Download not executed: maybe under dev mode, please put the files in current directory")
|
344 |
+
self.refer = get_spepc(self.hps, self.ref_wav_path)
|
345 |
+
|
346 |
+
|
347 |
+
|
348 |
+
|
349 |
+
def get_cleaned_text_final(self,text,language):
|
350 |
+
"""
|
351 |
+
根据语言类型选择适当的文本清洗函数,并返回处理后的音素序列、单词到音素的映射以及规范化文本。
|
352 |
+
-> phones,word2ph,norm_text
|
353 |
+
- clean_text_inf 针对单一语种{"en","all_zh","all_ja"}
|
354 |
+
- clean_text 和 cleaned_text_to_sequence 来自内部text模块cleaner和__init__
|
355 |
+
- nonen_clean_text_inf 针对混合语种{"zh", "ja","auto"}
|
356 |
+
- splite_en_inf
|
357 |
+
"""
|
358 |
+
if language in {"en","all_zh","all_ja"}:
|
359 |
+
phones, word2ph, norm_text = clean_text_inf(text, language)
|
360 |
+
elif language in {"zh", "ja","auto"}:
|
361 |
+
phones, word2ph, norm_text = nonen_clean_text_inf(text, language)
|
362 |
+
return phones, word2ph, norm_text
|
363 |
+
|
364 |
+
def get_bert_inf(self, phones, word2ph, norm_text, language):
|
365 |
+
device = self.device # 【补】
|
366 |
+
is_half = self.dtype == torch.float16 # 【补】
|
367 |
+
|
368 |
+
language=language.replace("all_","")
|
369 |
+
if language == "zh":
|
370 |
+
bert = self.get_bert_feature(norm_text, word2ph).to(device)#.to(dtype)
|
371 |
+
else:
|
372 |
+
bert = torch.zeros(
|
373 |
+
(1024, len(phones)),
|
374 |
+
dtype=torch.float16 if is_half == True else torch.float32,
|
375 |
+
).to(device)
|
376 |
+
|
377 |
+
return bert
|
378 |
+
|
379 |
+
def get_bert_feature(self, text, word2ph, tokenizer):
|
380 |
+
|
381 |
+
is_half = self.dtype == torch.float16 # 【补】
|
382 |
+
device = self.device # 【补】
|
383 |
+
|
384 |
+
with torch.no_grad():
|
385 |
+
inputs = tokenizer(text, return_tensors="pt")
|
386 |
+
for i in inputs:
|
387 |
+
inputs[i] = inputs[i].to(device) #####输入是long不用管精度问题,精度随bert_model
|
388 |
+
res = self.bert_model(**inputs, output_hidden_states=True)
|
389 |
+
res = torch.cat(res["hidden_states"][-3:-2], -1)[0].cpu()[1:-1]
|
390 |
+
assert len(word2ph) == len(text)
|
391 |
+
phone_level_feature = []
|
392 |
+
for i in range(len(word2ph)):
|
393 |
+
repeat_feature = res[i].repeat(word2ph[i], 1)
|
394 |
+
phone_level_feature.append(repeat_feature)
|
395 |
+
phone_level_feature = torch.cat(phone_level_feature, dim=0)
|
396 |
+
if(is_half==True):phone_level_feature=phone_level_feature.half()
|
397 |
+
|
398 |
+
return phone_level_feature.T
|
399 |
+
|
400 |
+
# ======适配混合语种输出======
|
401 |
+
# ===
|
402 |
+
def get_cleaned_text_final(self,text,language):
|
403 |
+
"""
|
404 |
+
根据语言类型选择适当的文本清洗函数,并返回处理后的音素序列、单词到音素的映射以及规范化文本。
|
405 |
+
-> phones,word2ph,norm_text
|
406 |
+
- clean_text_inf 针对单一语种{"en","all_zh","all_ja"}
|
407 |
+
- clean_text 和 cleaned_text_to_sequence 来自内部text模块cleaner和__init__
|
408 |
+
- nonen_clean_text_inf 针对混合语种{"zh", "ja","auto"}
|
409 |
+
- splite_en_inf
|
410 |
+
"""
|
411 |
+
if language in {"en","all_zh","all_ja"}:
|
412 |
+
phones, word2ph, norm_text = clean_text_inf(text, language)
|
413 |
+
elif language in {"zh", "ja","auto"}:
|
414 |
+
phones, word2ph, norm_text = nonen_clean_text_inf(text, language)
|
415 |
+
return phones, word2ph, norm_text
|
416 |
+
|
417 |
+
def get_bert_inf(self, phones, word2ph, norm_text, language, tokenizer):
|
418 |
+
device = self.device # 【补】
|
419 |
+
is_half = self.dtype == torch.float16 # 【补】
|
420 |
+
|
421 |
+
language=language.replace("all_","")
|
422 |
+
if language == "zh":
|
423 |
+
bert = self.get_bert_feature(norm_text, word2ph,tokenizer).to(device)#.to(dtype)
|
424 |
+
else:
|
425 |
+
bert = torch.zeros(
|
426 |
+
(1024, len(phones)),
|
427 |
+
dtype=torch.float16 if is_half == True else torch.float32,
|
428 |
+
).to(device)
|
429 |
+
|
430 |
+
return bert
|
431 |
+
|
432 |
+
def nonen_get_bert_inf(self, text, language, tokenizer):
|
433 |
+
if(language!="auto"):
|
434 |
+
textlist, langlist = splite_en_inf(text, language)
|
435 |
+
else:
|
436 |
+
textlist=[]
|
437 |
+
langlist=[]
|
438 |
+
for tmp in LangSegment.getTexts(text):
|
439 |
+
langlist.append(tmp["lang"])
|
440 |
+
textlist.append(tmp["text"])
|
441 |
+
print(textlist)
|
442 |
+
print(langlist)
|
443 |
+
bert_list = []
|
444 |
+
for i in range(len(textlist)):
|
445 |
+
lang = langlist[i]
|
446 |
+
phones, word2ph, norm_text = clean_text_inf(textlist[i], lang)
|
447 |
+
bert = self.get_bert_inf(phones, word2ph, norm_text, lang,tokenizer)
|
448 |
+
bert_list.append(bert)
|
449 |
+
bert = torch.cat(bert_list, dim=1)
|
450 |
+
|
451 |
+
return bert
|
452 |
+
|
453 |
+
def get_bert_final(self,phones, word2ph, text,language, tokenizer):
|
454 |
+
"""
|
455 |
+
根据语言 选择调用不同的函数来得到一个bert表示。
|
456 |
+
需要输入Get_clean_text_final得到的文字素材
|
457 |
+
-> bert
|
458 |
+
- get_bert_inf 针对纯英文”en”
|
459 |
+
- nonen_get_bert_inf 针对混合语种{"zh", "ja","auto"}
|
460 |
+
- get_bert_feature 针对纯中文”all_zh”
|
461 |
+
"""
|
462 |
+
device = self.device # 【补】
|
463 |
+
|
464 |
+
if language == "en":
|
465 |
+
bert = self.get_bert_inf(phones, word2ph, text, language, tokenizer) # 【补】
|
466 |
+
elif language in {"zh", "ja","auto"}:
|
467 |
+
bert = self.nonen_get_bert_inf(text, language, tokenizer)
|
468 |
+
elif language == "all_zh":
|
469 |
+
bert = self.get_bert_feature(text, word2ph, tokenizer).to(device)
|
470 |
+
else:
|
471 |
+
bert = torch.zeros((1024, len(phones))).to(device)
|
472 |
+
return bert
|
473 |
+
|
474 |
+
# ===
|
475 |
+
# ======适配混合语种输出======
|
476 |
+
|
477 |
+
def infer(self, text, tokenizer, text_language="zh",
|
478 |
+
how_to_cut="凑四句一切",
|
479 |
+
top_k=20, top_p=0.6, temperature=0.6,
|
480 |
+
# 关于上面三个参数 https://github.com/RVC-Boss/GPT-SoVITS/pull/457
|
481 |
+
# 可以通过降低温度,降低top_p,top_k 提升模型输出内容的一致性
|
482 |
+
ref_free = False) -> tuple[np.ndarray,float|int]: # 在不知道参考音频文本的情况下进行推理
|
483 |
+
|
484 |
+
# ====== 函数内变量 ======
|
485 |
+
# ===
|
486 |
+
# 根据声色指定相关模型与参考语音
|
487 |
+
ref_wav_path = self.ref_wav_path
|
488 |
+
|
489 |
+
if not ref_free:
|
490 |
+
prompt_text_path = self.prompt_text_path
|
491 |
+
with open(prompt_text_path, 'r', encoding='utf-8') as file:
|
492 |
+
prompt_text = file.read()
|
493 |
+
# 如果txt中音频文本为空,则也不使用音频文本。
|
494 |
+
if prompt_text is None or len(prompt_text) == 0:
|
495 |
+
ref_free = True
|
496 |
+
prompt_language = self.prompt_language
|
497 |
+
|
498 |
+
|
499 |
+
device = self.device
|
500 |
+
is_half = self.dtype == torch.float16
|
501 |
+
dtype = self.dtype
|
502 |
+
|
503 |
+
hz = 50
|
504 |
+
max_sec = self.gpt_config['data']['max_sec']
|
505 |
+
# ===
|
506 |
+
# ====== 函数内变量 ======
|
507 |
+
|
508 |
+
|
509 |
+
# 确认参考语音和推理文本的语种(可以不必,已对prompt_language和text_language的输入做了严格限制)
|
510 |
+
prompt_language = dict_language[prompt_language]
|
511 |
+
text_language = dict_language[text_language]
|
512 |
+
|
513 |
+
if not ref_free:
|
514 |
+
prompt_text = prompt_text.strip("\n")
|
515 |
+
if (prompt_text[-1] not in splits): prompt_text += "。" if prompt_language != "en" else "."
|
516 |
+
#【日志】 print("实际输入的参考文本:", prompt_text)
|
517 |
+
|
518 |
+
# 预处理推理文本:文本第一段(get_first)若特别短<4字符,则在文本最前方加上句号。
|
519 |
+
text = text.strip("\n")
|
520 |
+
if (text[0] not in splits and len(get_first(text)) < 4): text = "。" + text if text_language != "en" else "." + text
|
521 |
+
|
522 |
+
#【日志】 print("实际输入的目标文本:", text)
|
523 |
+
|
524 |
+
# 创建空音频段
|
525 |
+
# 第一个with torch.no_grad() 从参考音频中提取语义信息,并把空音频段放到参考音频末尾->prompt_semantic
|
526 |
+
zero_wav = np.zeros(
|
527 |
+
int(self.hps.data.sampling_rate * 0.3), # 【补】
|
528 |
+
dtype=np.float16 if is_half == True else np.float32,
|
529 |
+
)
|
530 |
+
with torch.no_grad():
|
531 |
+
wav16k, sr = librosa.load(ref_wav_path, sr=16000)
|
532 |
+
if (wav16k.shape[0] > 160000 or wav16k.shape[0] < 48000):
|
533 |
+
raise OSError("参考音频在3~10秒范围外,请更换!")
|
534 |
+
wav16k = torch.from_numpy(wav16k)
|
535 |
+
zero_wav_torch = torch.from_numpy(zero_wav)
|
536 |
+
if is_half == True:
|
537 |
+
wav16k = wav16k.half().to(device)
|
538 |
+
zero_wav_torch = zero_wav_torch.half().to(device)
|
539 |
+
else:
|
540 |
+
wav16k = wav16k.to(device)
|
541 |
+
zero_wav_torch = zero_wav_torch.to(device)
|
542 |
+
wav16k = torch.cat([wav16k, zero_wav_torch])
|
543 |
+
ssl_content = self.ssl_model.model(wav16k.unsqueeze(0))[
|
544 |
+
"last_hidden_state"
|
545 |
+
].transpose(
|
546 |
+
1, 2
|
547 |
+
) # .float()
|
548 |
+
codes = self.vq_model.extract_latent(ssl_content)
|
549 |
+
|
550 |
+
prompt_semantic = codes[0, 0]
|
551 |
+
|
552 |
+
# 切分推理文本,5种方法。一般可选4句一��和按标点符号切。之后,将其中小于5的语句/短语合并(merge_short_text_in_array)。最终得到推理文本切割列表
|
553 |
+
# -> texts
|
554 |
+
if (how_to_cut == "凑四句一切"):
|
555 |
+
text = cut1(text)
|
556 |
+
elif (how_to_cut == "凑50字一切"):
|
557 |
+
text = cut2(text)
|
558 |
+
elif (how_to_cut == "按中文句号。切"):
|
559 |
+
text = cut3(text)
|
560 |
+
elif (how_to_cut == "按英文句号.切"):
|
561 |
+
text = cut4(text)
|
562 |
+
elif (how_to_cut == "按标点符号切"):
|
563 |
+
text = cut5(text)
|
564 |
+
while "\n\n" in text:
|
565 |
+
text = text.replace("\n\n", "\n")
|
566 |
+
|
567 |
+
#【日志】 print("实际输入的目标文本(切句后):", text)
|
568 |
+
texts = text.split("\n")
|
569 |
+
texts = merge_short_text_in_array(texts, 5)
|
570 |
+
audio_opt = []
|
571 |
+
if not ref_free:
|
572 |
+
# 处理参考文本(get_cleaned_text_final)得到文字素材
|
573 |
+
# -> phones1,word2ph1,norm_text1
|
574 |
+
phones1, word2ph1, norm_text1=self.get_cleaned_text_final(prompt_text, prompt_language)
|
575 |
+
# 处理参考语音(Get_bert_final) 输入文字素材phones1,word2ph1,norm_text1
|
576 |
+
# 得到bert表示
|
577 |
+
# ->bert1
|
578 |
+
bert1=self.get_bert_final(phones1, word2ph1, norm_text1,prompt_language,tokenizer).to(dtype)
|
579 |
+
|
580 |
+
# for循环 处理推理文本,对texts中的每一段语句/短语
|
581 |
+
# 处理文本(get_cleaned_text_final)得到文字素材
|
582 |
+
# -> phones2,word2ph2,norm_text2
|
583 |
+
# 处理参考语音(Get_bert_final) 输入文字素材phones2,word2ph2,norm_text2
|
584 |
+
# 得到bert表示
|
585 |
+
# ->bert2
|
586 |
+
for text in texts:
|
587 |
+
# 解决输入目标文本的空行导致报错的问题
|
588 |
+
if (len(text.strip()) == 0):
|
589 |
+
continue
|
590 |
+
if (text[-1] not in splits): text += "。" if text_language != "en" else "."
|
591 |
+
# 【日志】print("实际输入的目标文本(每句):", text)
|
592 |
+
phones2, word2ph2, norm_text2 = self.get_cleaned_text_final(text, text_language)
|
593 |
+
bert2 = self.get_bert_final(phones2, word2ph2, norm_text2, text_language,tokenizer).to(dtype)
|
594 |
+
if not ref_free:
|
595 |
+
bert = torch.cat([bert1, bert2], 1)
|
596 |
+
all_phoneme_ids = torch.LongTensor(phones1+phones2).to(device).unsqueeze(0)
|
597 |
+
else:
|
598 |
+
bert = bert2
|
599 |
+
all_phoneme_ids = torch.LongTensor(phones2).to(device).unsqueeze(0)
|
600 |
+
|
601 |
+
bert = bert.to(device).unsqueeze(0)
|
602 |
+
all_phoneme_len = torch.tensor([all_phoneme_ids.shape[-1]]).to(device)
|
603 |
+
prompt = prompt_semantic.unsqueeze(0).to(device)
|
604 |
+
|
605 |
+
with torch.no_grad():
|
606 |
+
# pred_semantic = t2s_model.model.infer(
|
607 |
+
pred_semantic, idx = self.t2s_model.model.infer_panel(
|
608 |
+
all_phoneme_ids,
|
609 |
+
all_phoneme_len,
|
610 |
+
None if ref_free else prompt,
|
611 |
+
bert,
|
612 |
+
# prompt_phone_len=ph_offset,
|
613 |
+
top_k=top_k,
|
614 |
+
top_p=top_p,
|
615 |
+
temperature=temperature,
|
616 |
+
early_stop_num=hz * max_sec,
|
617 |
+
)
|
618 |
+
|
619 |
+
# print(pred_semantic.shape,idx)
|
620 |
+
pred_semantic = pred_semantic[:, -idx:].unsqueeze(
|
621 |
+
0
|
622 |
+
) # .unsqueeze(0)#mq要多unsqueeze一次
|
623 |
+
refer = get_spepc(self.hps, ref_wav_path) # .to(device) # 【补】
|
624 |
+
if is_half == True:
|
625 |
+
refer = refer.half().to(device)
|
626 |
+
else:
|
627 |
+
refer = refer.to(device)
|
628 |
+
# audio = vq_model.decode(pred_semantic, all_phoneme_ids, refer).detach().cpu().numpy()[0, 0]
|
629 |
+
audio = (
|
630 |
+
self.vq_model.decode( # 【补】
|
631 |
+
pred_semantic, torch.LongTensor(phones2).to(device).unsqueeze(0), refer
|
632 |
+
)
|
633 |
+
.detach()
|
634 |
+
.cpu()
|
635 |
+
.numpy()[0, 0]
|
636 |
+
) ###试试重建不带上prompt部分
|
637 |
+
max_audio=np.abs(audio).max()#简单防止16bit爆音
|
638 |
+
if max_audio>1:audio/=max_audio
|
639 |
+
audio_opt.append(audio)
|
640 |
+
audio_opt.append(zero_wav)
|
641 |
+
|
642 |
+
sampling_rate, audio_data = self.hps.data.sampling_rate, (np.concatenate(audio_opt, 0) * 32768).astype(
|
643 |
+
np.int16
|
644 |
+
)
|
645 |
+
|
646 |
+
# sf.write(wav_save_path, audio_data, sampling_rate, format='wav')
|
647 |
+
torch.cuda.empty_cache()
|
648 |
+
return audio_data, sampling_rate
|
models.py
ADDED
@@ -0,0 +1,991 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import torch
|
3 |
+
from torch import nn
|
4 |
+
from torch.nn import functional as F
|
5 |
+
|
6 |
+
from . import commons
|
7 |
+
from . import modules
|
8 |
+
from . import attentions
|
9 |
+
|
10 |
+
from torch.nn import Conv1d, ConvTranspose1d, Conv2d
|
11 |
+
from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm
|
12 |
+
from .commons import init_weights, get_padding
|
13 |
+
from .mrte_model import MRTE
|
14 |
+
from .quantize import ResidualVectorQuantizer
|
15 |
+
from . import symbols
|
16 |
+
from torch.cuda.amp import autocast
|
17 |
+
|
18 |
+
|
19 |
+
class StochasticDurationPredictor(nn.Module):
|
20 |
+
def __init__(
|
21 |
+
self,
|
22 |
+
in_channels,
|
23 |
+
filter_channels,
|
24 |
+
kernel_size,
|
25 |
+
p_dropout,
|
26 |
+
n_flows=4,
|
27 |
+
gin_channels=0,
|
28 |
+
):
|
29 |
+
super().__init__()
|
30 |
+
filter_channels = in_channels # it needs to be removed from future version.
|
31 |
+
self.in_channels = in_channels
|
32 |
+
self.filter_channels = filter_channels
|
33 |
+
self.kernel_size = kernel_size
|
34 |
+
self.p_dropout = p_dropout
|
35 |
+
self.n_flows = n_flows
|
36 |
+
self.gin_channels = gin_channels
|
37 |
+
|
38 |
+
self.log_flow = modules.Log()
|
39 |
+
self.flows = nn.ModuleList()
|
40 |
+
self.flows.append(modules.ElementwiseAffine(2))
|
41 |
+
for i in range(n_flows):
|
42 |
+
self.flows.append(
|
43 |
+
modules.ConvFlow(2, filter_channels, kernel_size, n_layers=3)
|
44 |
+
)
|
45 |
+
self.flows.append(modules.Flip())
|
46 |
+
|
47 |
+
self.post_pre = nn.Conv1d(1, filter_channels, 1)
|
48 |
+
self.post_proj = nn.Conv1d(filter_channels, filter_channels, 1)
|
49 |
+
self.post_convs = modules.DDSConv(
|
50 |
+
filter_channels, kernel_size, n_layers=3, p_dropout=p_dropout
|
51 |
+
)
|
52 |
+
self.post_flows = nn.ModuleList()
|
53 |
+
self.post_flows.append(modules.ElementwiseAffine(2))
|
54 |
+
for i in range(4):
|
55 |
+
self.post_flows.append(
|
56 |
+
modules.ConvFlow(2, filter_channels, kernel_size, n_layers=3)
|
57 |
+
)
|
58 |
+
self.post_flows.append(modules.Flip())
|
59 |
+
|
60 |
+
self.pre = nn.Conv1d(in_channels, filter_channels, 1)
|
61 |
+
self.proj = nn.Conv1d(filter_channels, filter_channels, 1)
|
62 |
+
self.convs = modules.DDSConv(
|
63 |
+
filter_channels, kernel_size, n_layers=3, p_dropout=p_dropout
|
64 |
+
)
|
65 |
+
if gin_channels != 0:
|
66 |
+
self.cond = nn.Conv1d(gin_channels, filter_channels, 1)
|
67 |
+
|
68 |
+
def forward(self, x, x_mask, w=None, g=None, reverse=False, noise_scale=1.0):
|
69 |
+
x = torch.detach(x)
|
70 |
+
x = self.pre(x)
|
71 |
+
if g is not None:
|
72 |
+
g = torch.detach(g)
|
73 |
+
x = x + self.cond(g)
|
74 |
+
x = self.convs(x, x_mask)
|
75 |
+
x = self.proj(x) * x_mask
|
76 |
+
|
77 |
+
if not reverse:
|
78 |
+
flows = self.flows
|
79 |
+
assert w is not None
|
80 |
+
|
81 |
+
logdet_tot_q = 0
|
82 |
+
h_w = self.post_pre(w)
|
83 |
+
h_w = self.post_convs(h_w, x_mask)
|
84 |
+
h_w = self.post_proj(h_w) * x_mask
|
85 |
+
e_q = (
|
86 |
+
torch.randn(w.size(0), 2, w.size(2)).to(device=x.device, dtype=x.dtype)
|
87 |
+
* x_mask
|
88 |
+
)
|
89 |
+
z_q = e_q
|
90 |
+
for flow in self.post_flows:
|
91 |
+
z_q, logdet_q = flow(z_q, x_mask, g=(x + h_w))
|
92 |
+
logdet_tot_q += logdet_q
|
93 |
+
z_u, z1 = torch.split(z_q, [1, 1], 1)
|
94 |
+
u = torch.sigmoid(z_u) * x_mask
|
95 |
+
z0 = (w - u) * x_mask
|
96 |
+
logdet_tot_q += torch.sum(
|
97 |
+
(F.logsigmoid(z_u) + F.logsigmoid(-z_u)) * x_mask, [1, 2]
|
98 |
+
)
|
99 |
+
logq = (
|
100 |
+
torch.sum(-0.5 * (math.log(2 * math.pi) + (e_q**2)) * x_mask, [1, 2])
|
101 |
+
- logdet_tot_q
|
102 |
+
)
|
103 |
+
|
104 |
+
logdet_tot = 0
|
105 |
+
z0, logdet = self.log_flow(z0, x_mask)
|
106 |
+
logdet_tot += logdet
|
107 |
+
z = torch.cat([z0, z1], 1)
|
108 |
+
for flow in flows:
|
109 |
+
z, logdet = flow(z, x_mask, g=x, reverse=reverse)
|
110 |
+
logdet_tot = logdet_tot + logdet
|
111 |
+
nll = (
|
112 |
+
torch.sum(0.5 * (math.log(2 * math.pi) + (z**2)) * x_mask, [1, 2])
|
113 |
+
- logdet_tot
|
114 |
+
)
|
115 |
+
return nll + logq # [b]
|
116 |
+
else:
|
117 |
+
flows = list(reversed(self.flows))
|
118 |
+
flows = flows[:-2] + [flows[-1]] # remove a useless vflow
|
119 |
+
z = (
|
120 |
+
torch.randn(x.size(0), 2, x.size(2)).to(device=x.device, dtype=x.dtype)
|
121 |
+
* noise_scale
|
122 |
+
)
|
123 |
+
for flow in flows:
|
124 |
+
z = flow(z, x_mask, g=x, reverse=reverse)
|
125 |
+
z0, z1 = torch.split(z, [1, 1], 1)
|
126 |
+
logw = z0
|
127 |
+
return logw
|
128 |
+
|
129 |
+
|
130 |
+
class DurationPredictor(nn.Module):
|
131 |
+
def __init__(
|
132 |
+
self, in_channels, filter_channels, kernel_size, p_dropout, gin_channels=0
|
133 |
+
):
|
134 |
+
super().__init__()
|
135 |
+
|
136 |
+
self.in_channels = in_channels
|
137 |
+
self.filter_channels = filter_channels
|
138 |
+
self.kernel_size = kernel_size
|
139 |
+
self.p_dropout = p_dropout
|
140 |
+
self.gin_channels = gin_channels
|
141 |
+
|
142 |
+
self.drop = nn.Dropout(p_dropout)
|
143 |
+
self.conv_1 = nn.Conv1d(
|
144 |
+
in_channels, filter_channels, kernel_size, padding=kernel_size // 2
|
145 |
+
)
|
146 |
+
self.norm_1 = modules.LayerNorm(filter_channels)
|
147 |
+
self.conv_2 = nn.Conv1d(
|
148 |
+
filter_channels, filter_channels, kernel_size, padding=kernel_size // 2
|
149 |
+
)
|
150 |
+
self.norm_2 = modules.LayerNorm(filter_channels)
|
151 |
+
self.proj = nn.Conv1d(filter_channels, 1, 1)
|
152 |
+
|
153 |
+
if gin_channels != 0:
|
154 |
+
self.cond = nn.Conv1d(gin_channels, in_channels, 1)
|
155 |
+
|
156 |
+
def forward(self, x, x_mask, g=None):
|
157 |
+
x = torch.detach(x)
|
158 |
+
if g is not None:
|
159 |
+
g = torch.detach(g)
|
160 |
+
x = x + self.cond(g)
|
161 |
+
x = self.conv_1(x * x_mask)
|
162 |
+
x = torch.relu(x)
|
163 |
+
x = self.norm_1(x)
|
164 |
+
x = self.drop(x)
|
165 |
+
x = self.conv_2(x * x_mask)
|
166 |
+
x = torch.relu(x)
|
167 |
+
x = self.norm_2(x)
|
168 |
+
x = self.drop(x)
|
169 |
+
x = self.proj(x * x_mask)
|
170 |
+
return x * x_mask
|
171 |
+
|
172 |
+
|
173 |
+
class TextEncoder(nn.Module):
|
174 |
+
def __init__(
|
175 |
+
self,
|
176 |
+
out_channels,
|
177 |
+
hidden_channels,
|
178 |
+
filter_channels,
|
179 |
+
n_heads,
|
180 |
+
n_layers,
|
181 |
+
kernel_size,
|
182 |
+
p_dropout,
|
183 |
+
latent_channels=192,
|
184 |
+
):
|
185 |
+
super().__init__()
|
186 |
+
self.out_channels = out_channels
|
187 |
+
self.hidden_channels = hidden_channels
|
188 |
+
self.filter_channels = filter_channels
|
189 |
+
self.n_heads = n_heads
|
190 |
+
self.n_layers = n_layers
|
191 |
+
self.kernel_size = kernel_size
|
192 |
+
self.p_dropout = p_dropout
|
193 |
+
self.latent_channels = latent_channels
|
194 |
+
|
195 |
+
self.ssl_proj = nn.Conv1d(768, hidden_channels, 1)
|
196 |
+
|
197 |
+
self.encoder_ssl = attentions.Encoder(
|
198 |
+
hidden_channels,
|
199 |
+
filter_channels,
|
200 |
+
n_heads,
|
201 |
+
n_layers // 2,
|
202 |
+
kernel_size,
|
203 |
+
p_dropout,
|
204 |
+
)
|
205 |
+
|
206 |
+
self.encoder_text = attentions.Encoder(
|
207 |
+
hidden_channels, filter_channels, n_heads, n_layers, kernel_size, p_dropout
|
208 |
+
)
|
209 |
+
self.text_embedding = nn.Embedding(len(symbols.symbols), hidden_channels)
|
210 |
+
|
211 |
+
self.mrte = MRTE()
|
212 |
+
|
213 |
+
self.encoder2 = attentions.Encoder(
|
214 |
+
hidden_channels,
|
215 |
+
filter_channels,
|
216 |
+
n_heads,
|
217 |
+
n_layers // 2,
|
218 |
+
kernel_size,
|
219 |
+
p_dropout,
|
220 |
+
)
|
221 |
+
|
222 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
223 |
+
|
224 |
+
def forward(self, y, y_lengths, text, text_lengths, ge, test=None):
|
225 |
+
y_mask = torch.unsqueeze(commons.sequence_mask(y_lengths, y.size(2)), 1).to(
|
226 |
+
y.dtype
|
227 |
+
)
|
228 |
+
|
229 |
+
y = self.ssl_proj(y * y_mask) * y_mask
|
230 |
+
|
231 |
+
y = self.encoder_ssl(y * y_mask, y_mask)
|
232 |
+
|
233 |
+
text_mask = torch.unsqueeze(
|
234 |
+
commons.sequence_mask(text_lengths, text.size(1)), 1
|
235 |
+
).to(y.dtype)
|
236 |
+
if test == 1:
|
237 |
+
text[:, :] = 0
|
238 |
+
text = self.text_embedding(text).transpose(1, 2)
|
239 |
+
text = self.encoder_text(text * text_mask, text_mask)
|
240 |
+
y = self.mrte(y, y_mask, text, text_mask, ge)
|
241 |
+
|
242 |
+
y = self.encoder2(y * y_mask, y_mask)
|
243 |
+
|
244 |
+
stats = self.proj(y) * y_mask
|
245 |
+
m, logs = torch.split(stats, self.out_channels, dim=1)
|
246 |
+
return y, m, logs, y_mask
|
247 |
+
|
248 |
+
def extract_latent(self, x):
|
249 |
+
x = self.ssl_proj(x)
|
250 |
+
quantized, codes, commit_loss, quantized_list = self.quantizer(x)
|
251 |
+
return codes.transpose(0, 1)
|
252 |
+
|
253 |
+
def decode_latent(self, codes, y_mask, refer, refer_mask, ge):
|
254 |
+
quantized = self.quantizer.decode(codes)
|
255 |
+
|
256 |
+
y = self.vq_proj(quantized) * y_mask
|
257 |
+
y = self.encoder_ssl(y * y_mask, y_mask)
|
258 |
+
|
259 |
+
y = self.mrte(y, y_mask, refer, refer_mask, ge)
|
260 |
+
|
261 |
+
y = self.encoder2(y * y_mask, y_mask)
|
262 |
+
|
263 |
+
stats = self.proj(y) * y_mask
|
264 |
+
m, logs = torch.split(stats, self.out_channels, dim=1)
|
265 |
+
return y, m, logs, y_mask, quantized
|
266 |
+
|
267 |
+
|
268 |
+
class ResidualCouplingBlock(nn.Module):
|
269 |
+
def __init__(
|
270 |
+
self,
|
271 |
+
channels,
|
272 |
+
hidden_channels,
|
273 |
+
kernel_size,
|
274 |
+
dilation_rate,
|
275 |
+
n_layers,
|
276 |
+
n_flows=4,
|
277 |
+
gin_channels=0,
|
278 |
+
):
|
279 |
+
super().__init__()
|
280 |
+
self.channels = channels
|
281 |
+
self.hidden_channels = hidden_channels
|
282 |
+
self.kernel_size = kernel_size
|
283 |
+
self.dilation_rate = dilation_rate
|
284 |
+
self.n_layers = n_layers
|
285 |
+
self.n_flows = n_flows
|
286 |
+
self.gin_channels = gin_channels
|
287 |
+
|
288 |
+
self.flows = nn.ModuleList()
|
289 |
+
for i in range(n_flows):
|
290 |
+
self.flows.append(
|
291 |
+
modules.ResidualCouplingLayer(
|
292 |
+
channels,
|
293 |
+
hidden_channels,
|
294 |
+
kernel_size,
|
295 |
+
dilation_rate,
|
296 |
+
n_layers,
|
297 |
+
gin_channels=gin_channels,
|
298 |
+
mean_only=True,
|
299 |
+
)
|
300 |
+
)
|
301 |
+
self.flows.append(modules.Flip())
|
302 |
+
|
303 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
304 |
+
if not reverse:
|
305 |
+
for flow in self.flows:
|
306 |
+
x, _ = flow(x, x_mask, g=g, reverse=reverse)
|
307 |
+
else:
|
308 |
+
for flow in reversed(self.flows):
|
309 |
+
x = flow(x, x_mask, g=g, reverse=reverse)
|
310 |
+
return x
|
311 |
+
|
312 |
+
|
313 |
+
class PosteriorEncoder(nn.Module):
|
314 |
+
def __init__(
|
315 |
+
self,
|
316 |
+
in_channels,
|
317 |
+
out_channels,
|
318 |
+
hidden_channels,
|
319 |
+
kernel_size,
|
320 |
+
dilation_rate,
|
321 |
+
n_layers,
|
322 |
+
gin_channels=0,
|
323 |
+
):
|
324 |
+
super().__init__()
|
325 |
+
self.in_channels = in_channels
|
326 |
+
self.out_channels = out_channels
|
327 |
+
self.hidden_channels = hidden_channels
|
328 |
+
self.kernel_size = kernel_size
|
329 |
+
self.dilation_rate = dilation_rate
|
330 |
+
self.n_layers = n_layers
|
331 |
+
self.gin_channels = gin_channels
|
332 |
+
|
333 |
+
self.pre = nn.Conv1d(in_channels, hidden_channels, 1)
|
334 |
+
self.enc = modules.WN(
|
335 |
+
hidden_channels,
|
336 |
+
kernel_size,
|
337 |
+
dilation_rate,
|
338 |
+
n_layers,
|
339 |
+
gin_channels=gin_channels,
|
340 |
+
)
|
341 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
342 |
+
|
343 |
+
def forward(self, x, x_lengths, g=None):
|
344 |
+
if g != None:
|
345 |
+
g = g.detach()
|
346 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(x_lengths, x.size(2)), 1).to(
|
347 |
+
x.dtype
|
348 |
+
)
|
349 |
+
x = self.pre(x) * x_mask
|
350 |
+
x = self.enc(x, x_mask, g=g)
|
351 |
+
stats = self.proj(x) * x_mask
|
352 |
+
m, logs = torch.split(stats, self.out_channels, dim=1)
|
353 |
+
z = (m + torch.randn_like(m) * torch.exp(logs)) * x_mask
|
354 |
+
return z, m, logs, x_mask
|
355 |
+
|
356 |
+
|
357 |
+
class WNEncoder(nn.Module):
|
358 |
+
def __init__(
|
359 |
+
self,
|
360 |
+
in_channels,
|
361 |
+
out_channels,
|
362 |
+
hidden_channels,
|
363 |
+
kernel_size,
|
364 |
+
dilation_rate,
|
365 |
+
n_layers,
|
366 |
+
gin_channels=0,
|
367 |
+
):
|
368 |
+
super().__init__()
|
369 |
+
self.in_channels = in_channels
|
370 |
+
self.out_channels = out_channels
|
371 |
+
self.hidden_channels = hidden_channels
|
372 |
+
self.kernel_size = kernel_size
|
373 |
+
self.dilation_rate = dilation_rate
|
374 |
+
self.n_layers = n_layers
|
375 |
+
self.gin_channels = gin_channels
|
376 |
+
|
377 |
+
self.pre = nn.Conv1d(in_channels, hidden_channels, 1)
|
378 |
+
self.enc = modules.WN(
|
379 |
+
hidden_channels,
|
380 |
+
kernel_size,
|
381 |
+
dilation_rate,
|
382 |
+
n_layers,
|
383 |
+
gin_channels=gin_channels,
|
384 |
+
)
|
385 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels, 1)
|
386 |
+
self.norm = modules.LayerNorm(out_channels)
|
387 |
+
|
388 |
+
def forward(self, x, x_lengths, g=None):
|
389 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(x_lengths, x.size(2)), 1).to(
|
390 |
+
x.dtype
|
391 |
+
)
|
392 |
+
x = self.pre(x) * x_mask
|
393 |
+
x = self.enc(x, x_mask, g=g)
|
394 |
+
out = self.proj(x) * x_mask
|
395 |
+
out = self.norm(out)
|
396 |
+
return out
|
397 |
+
|
398 |
+
|
399 |
+
class Generator(torch.nn.Module):
|
400 |
+
def __init__(
|
401 |
+
self,
|
402 |
+
initial_channel,
|
403 |
+
resblock,
|
404 |
+
resblock_kernel_sizes,
|
405 |
+
resblock_dilation_sizes,
|
406 |
+
upsample_rates,
|
407 |
+
upsample_initial_channel,
|
408 |
+
upsample_kernel_sizes,
|
409 |
+
gin_channels=0,
|
410 |
+
):
|
411 |
+
super(Generator, self).__init__()
|
412 |
+
self.num_kernels = len(resblock_kernel_sizes)
|
413 |
+
self.num_upsamples = len(upsample_rates)
|
414 |
+
self.conv_pre = Conv1d(
|
415 |
+
initial_channel, upsample_initial_channel, 7, 1, padding=3
|
416 |
+
)
|
417 |
+
resblock = modules.ResBlock1 if resblock == "1" else modules.ResBlock2
|
418 |
+
|
419 |
+
self.ups = nn.ModuleList()
|
420 |
+
for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)):
|
421 |
+
self.ups.append(
|
422 |
+
weight_norm(
|
423 |
+
ConvTranspose1d(
|
424 |
+
upsample_initial_channel // (2**i),
|
425 |
+
upsample_initial_channel // (2 ** (i + 1)),
|
426 |
+
k,
|
427 |
+
u,
|
428 |
+
padding=(k - u) // 2,
|
429 |
+
)
|
430 |
+
)
|
431 |
+
)
|
432 |
+
|
433 |
+
self.resblocks = nn.ModuleList()
|
434 |
+
for i in range(len(self.ups)):
|
435 |
+
ch = upsample_initial_channel // (2 ** (i + 1))
|
436 |
+
for j, (k, d) in enumerate(
|
437 |
+
zip(resblock_kernel_sizes, resblock_dilation_sizes)
|
438 |
+
):
|
439 |
+
self.resblocks.append(resblock(ch, k, d))
|
440 |
+
|
441 |
+
self.conv_post = Conv1d(ch, 1, 7, 1, padding=3, bias=False)
|
442 |
+
self.ups.apply(init_weights)
|
443 |
+
|
444 |
+
if gin_channels != 0:
|
445 |
+
self.cond = nn.Conv1d(gin_channels, upsample_initial_channel, 1)
|
446 |
+
|
447 |
+
def forward(self, x, g=None):
|
448 |
+
x = self.conv_pre(x)
|
449 |
+
if g is not None:
|
450 |
+
x = x + self.cond(g)
|
451 |
+
|
452 |
+
for i in range(self.num_upsamples):
|
453 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
454 |
+
x = self.ups[i](x)
|
455 |
+
xs = None
|
456 |
+
for j in range(self.num_kernels):
|
457 |
+
if xs is None:
|
458 |
+
xs = self.resblocks[i * self.num_kernels + j](x)
|
459 |
+
else:
|
460 |
+
xs += self.resblocks[i * self.num_kernels + j](x)
|
461 |
+
x = xs / self.num_kernels
|
462 |
+
x = F.leaky_relu(x)
|
463 |
+
x = self.conv_post(x)
|
464 |
+
x = torch.tanh(x)
|
465 |
+
|
466 |
+
return x
|
467 |
+
|
468 |
+
def remove_weight_norm(self):
|
469 |
+
print("Removing weight norm...")
|
470 |
+
for l in self.ups:
|
471 |
+
remove_weight_norm(l)
|
472 |
+
for l in self.resblocks:
|
473 |
+
l.remove_weight_norm()
|
474 |
+
|
475 |
+
|
476 |
+
class DiscriminatorP(torch.nn.Module):
|
477 |
+
def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False):
|
478 |
+
super(DiscriminatorP, self).__init__()
|
479 |
+
self.period = period
|
480 |
+
self.use_spectral_norm = use_spectral_norm
|
481 |
+
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
482 |
+
self.convs = nn.ModuleList(
|
483 |
+
[
|
484 |
+
norm_f(
|
485 |
+
Conv2d(
|
486 |
+
1,
|
487 |
+
32,
|
488 |
+
(kernel_size, 1),
|
489 |
+
(stride, 1),
|
490 |
+
padding=(get_padding(kernel_size, 1), 0),
|
491 |
+
)
|
492 |
+
),
|
493 |
+
norm_f(
|
494 |
+
Conv2d(
|
495 |
+
32,
|
496 |
+
128,
|
497 |
+
(kernel_size, 1),
|
498 |
+
(stride, 1),
|
499 |
+
padding=(get_padding(kernel_size, 1), 0),
|
500 |
+
)
|
501 |
+
),
|
502 |
+
norm_f(
|
503 |
+
Conv2d(
|
504 |
+
128,
|
505 |
+
512,
|
506 |
+
(kernel_size, 1),
|
507 |
+
(stride, 1),
|
508 |
+
padding=(get_padding(kernel_size, 1), 0),
|
509 |
+
)
|
510 |
+
),
|
511 |
+
norm_f(
|
512 |
+
Conv2d(
|
513 |
+
512,
|
514 |
+
1024,
|
515 |
+
(kernel_size, 1),
|
516 |
+
(stride, 1),
|
517 |
+
padding=(get_padding(kernel_size, 1), 0),
|
518 |
+
)
|
519 |
+
),
|
520 |
+
norm_f(
|
521 |
+
Conv2d(
|
522 |
+
1024,
|
523 |
+
1024,
|
524 |
+
(kernel_size, 1),
|
525 |
+
1,
|
526 |
+
padding=(get_padding(kernel_size, 1), 0),
|
527 |
+
)
|
528 |
+
),
|
529 |
+
]
|
530 |
+
)
|
531 |
+
self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
|
532 |
+
|
533 |
+
def forward(self, x):
|
534 |
+
fmap = []
|
535 |
+
|
536 |
+
# 1d to 2d
|
537 |
+
b, c, t = x.shape
|
538 |
+
if t % self.period != 0: # pad first
|
539 |
+
n_pad = self.period - (t % self.period)
|
540 |
+
x = F.pad(x, (0, n_pad), "reflect")
|
541 |
+
t = t + n_pad
|
542 |
+
x = x.view(b, c, t // self.period, self.period)
|
543 |
+
|
544 |
+
for l in self.convs:
|
545 |
+
x = l(x)
|
546 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
547 |
+
fmap.append(x)
|
548 |
+
x = self.conv_post(x)
|
549 |
+
fmap.append(x)
|
550 |
+
x = torch.flatten(x, 1, -1)
|
551 |
+
|
552 |
+
return x, fmap
|
553 |
+
|
554 |
+
|
555 |
+
class DiscriminatorS(torch.nn.Module):
|
556 |
+
def __init__(self, use_spectral_norm=False):
|
557 |
+
super(DiscriminatorS, self).__init__()
|
558 |
+
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
559 |
+
self.convs = nn.ModuleList(
|
560 |
+
[
|
561 |
+
norm_f(Conv1d(1, 16, 15, 1, padding=7)),
|
562 |
+
norm_f(Conv1d(16, 64, 41, 4, groups=4, padding=20)),
|
563 |
+
norm_f(Conv1d(64, 256, 41, 4, groups=16, padding=20)),
|
564 |
+
norm_f(Conv1d(256, 1024, 41, 4, groups=64, padding=20)),
|
565 |
+
norm_f(Conv1d(1024, 1024, 41, 4, groups=256, padding=20)),
|
566 |
+
norm_f(Conv1d(1024, 1024, 5, 1, padding=2)),
|
567 |
+
]
|
568 |
+
)
|
569 |
+
self.conv_post = norm_f(Conv1d(1024, 1, 3, 1, padding=1))
|
570 |
+
|
571 |
+
def forward(self, x):
|
572 |
+
fmap = []
|
573 |
+
|
574 |
+
for l in self.convs:
|
575 |
+
x = l(x)
|
576 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
577 |
+
fmap.append(x)
|
578 |
+
x = self.conv_post(x)
|
579 |
+
fmap.append(x)
|
580 |
+
x = torch.flatten(x, 1, -1)
|
581 |
+
|
582 |
+
return x, fmap
|
583 |
+
|
584 |
+
|
585 |
+
class MultiPeriodDiscriminator(torch.nn.Module):
|
586 |
+
def __init__(self, use_spectral_norm=False):
|
587 |
+
super(MultiPeriodDiscriminator, self).__init__()
|
588 |
+
periods = [2, 3, 5, 7, 11]
|
589 |
+
|
590 |
+
discs = [DiscriminatorS(use_spectral_norm=use_spectral_norm)]
|
591 |
+
discs = discs + [
|
592 |
+
DiscriminatorP(i, use_spectral_norm=use_spectral_norm) for i in periods
|
593 |
+
]
|
594 |
+
self.discriminators = nn.ModuleList(discs)
|
595 |
+
|
596 |
+
def forward(self, y, y_hat):
|
597 |
+
y_d_rs = []
|
598 |
+
y_d_gs = []
|
599 |
+
fmap_rs = []
|
600 |
+
fmap_gs = []
|
601 |
+
for i, d in enumerate(self.discriminators):
|
602 |
+
y_d_r, fmap_r = d(y)
|
603 |
+
y_d_g, fmap_g = d(y_hat)
|
604 |
+
y_d_rs.append(y_d_r)
|
605 |
+
y_d_gs.append(y_d_g)
|
606 |
+
fmap_rs.append(fmap_r)
|
607 |
+
fmap_gs.append(fmap_g)
|
608 |
+
|
609 |
+
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
|
610 |
+
|
611 |
+
|
612 |
+
class ReferenceEncoder(nn.Module):
|
613 |
+
"""
|
614 |
+
inputs --- [N, Ty/r, n_mels*r] mels
|
615 |
+
outputs --- [N, ref_enc_gru_size]
|
616 |
+
"""
|
617 |
+
|
618 |
+
def __init__(self, spec_channels, gin_channels=0):
|
619 |
+
super().__init__()
|
620 |
+
self.spec_channels = spec_channels
|
621 |
+
ref_enc_filters = [32, 32, 64, 64, 128, 128]
|
622 |
+
K = len(ref_enc_filters)
|
623 |
+
filters = [1] + ref_enc_filters
|
624 |
+
convs = [
|
625 |
+
weight_norm(
|
626 |
+
nn.Conv2d(
|
627 |
+
in_channels=filters[i],
|
628 |
+
out_channels=filters[i + 1],
|
629 |
+
kernel_size=(3, 3),
|
630 |
+
stride=(2, 2),
|
631 |
+
padding=(1, 1),
|
632 |
+
)
|
633 |
+
)
|
634 |
+
for i in range(K)
|
635 |
+
]
|
636 |
+
self.convs = nn.ModuleList(convs)
|
637 |
+
# self.wns = nn.ModuleList([weight_norm(num_features=ref_enc_filters[i]) for i in range(K)])
|
638 |
+
|
639 |
+
out_channels = self.calculate_channels(spec_channels, 3, 2, 1, K)
|
640 |
+
self.gru = nn.GRU(
|
641 |
+
input_size=ref_enc_filters[-1] * out_channels,
|
642 |
+
hidden_size=256 // 2,
|
643 |
+
batch_first=True,
|
644 |
+
)
|
645 |
+
self.proj = nn.Linear(128, gin_channels)
|
646 |
+
|
647 |
+
def forward(self, inputs):
|
648 |
+
N = inputs.size(0)
|
649 |
+
out = inputs.view(N, 1, -1, self.spec_channels) # [N, 1, Ty, n_freqs]
|
650 |
+
for conv in self.convs:
|
651 |
+
out = conv(out)
|
652 |
+
# out = wn(out)
|
653 |
+
out = F.relu(out) # [N, 128, Ty//2^K, n_mels//2^K]
|
654 |
+
|
655 |
+
out = out.transpose(1, 2) # [N, Ty//2^K, 128, n_mels//2^K]
|
656 |
+
T = out.size(1)
|
657 |
+
N = out.size(0)
|
658 |
+
out = out.contiguous().view(N, T, -1) # [N, Ty//2^K, 128*n_mels//2^K]
|
659 |
+
|
660 |
+
self.gru.flatten_parameters()
|
661 |
+
memory, out = self.gru(out) # out --- [1, N, 128]
|
662 |
+
|
663 |
+
return self.proj(out.squeeze(0)).unsqueeze(-1)
|
664 |
+
|
665 |
+
def calculate_channels(self, L, kernel_size, stride, pad, n_convs):
|
666 |
+
for i in range(n_convs):
|
667 |
+
L = (L - kernel_size + 2 * pad) // stride + 1
|
668 |
+
return L
|
669 |
+
|
670 |
+
|
671 |
+
class Quantizer_module(torch.nn.Module):
|
672 |
+
def __init__(self, n_e, e_dim):
|
673 |
+
super(Quantizer_module, self).__init__()
|
674 |
+
self.embedding = nn.Embedding(n_e, e_dim)
|
675 |
+
self.embedding.weight.data.uniform_(-1.0 / n_e, 1.0 / n_e)
|
676 |
+
|
677 |
+
def forward(self, x):
|
678 |
+
d = (
|
679 |
+
torch.sum(x**2, 1, keepdim=True)
|
680 |
+
+ torch.sum(self.embedding.weight**2, 1)
|
681 |
+
- 2 * torch.matmul(x, self.embedding.weight.T)
|
682 |
+
)
|
683 |
+
min_indicies = torch.argmin(d, 1)
|
684 |
+
z_q = self.embedding(min_indicies)
|
685 |
+
return z_q, min_indicies
|
686 |
+
|
687 |
+
|
688 |
+
class Quantizer(torch.nn.Module):
|
689 |
+
def __init__(self, embed_dim=512, n_code_groups=4, n_codes=160):
|
690 |
+
super(Quantizer, self).__init__()
|
691 |
+
assert embed_dim % n_code_groups == 0
|
692 |
+
self.quantizer_modules = nn.ModuleList(
|
693 |
+
[
|
694 |
+
Quantizer_module(n_codes, embed_dim // n_code_groups)
|
695 |
+
for _ in range(n_code_groups)
|
696 |
+
]
|
697 |
+
)
|
698 |
+
self.n_code_groups = n_code_groups
|
699 |
+
self.embed_dim = embed_dim
|
700 |
+
|
701 |
+
def forward(self, xin):
|
702 |
+
# B, C, T
|
703 |
+
B, C, T = xin.shape
|
704 |
+
xin = xin.transpose(1, 2)
|
705 |
+
x = xin.reshape(-1, self.embed_dim)
|
706 |
+
x = torch.split(x, self.embed_dim // self.n_code_groups, dim=-1)
|
707 |
+
min_indicies = []
|
708 |
+
z_q = []
|
709 |
+
for _x, m in zip(x, self.quantizer_modules):
|
710 |
+
_z_q, _min_indicies = m(_x)
|
711 |
+
z_q.append(_z_q)
|
712 |
+
min_indicies.append(_min_indicies) # B * T,
|
713 |
+
z_q = torch.cat(z_q, -1).reshape(xin.shape)
|
714 |
+
loss = 0.25 * torch.mean((z_q.detach() - xin) ** 2) + torch.mean(
|
715 |
+
(z_q - xin.detach()) ** 2
|
716 |
+
)
|
717 |
+
z_q = xin + (z_q - xin).detach()
|
718 |
+
z_q = z_q.transpose(1, 2)
|
719 |
+
codes = torch.stack(min_indicies, -1).reshape(B, T, self.n_code_groups)
|
720 |
+
return z_q, loss, codes.transpose(1, 2)
|
721 |
+
|
722 |
+
def embed(self, x):
|
723 |
+
# idx: N, 4, T
|
724 |
+
x = x.transpose(1, 2)
|
725 |
+
x = torch.split(x, 1, 2)
|
726 |
+
ret = []
|
727 |
+
for q, embed in zip(x, self.quantizer_modules):
|
728 |
+
q = embed.embedding(q.squeeze(-1))
|
729 |
+
ret.append(q)
|
730 |
+
ret = torch.cat(ret, -1)
|
731 |
+
return ret.transpose(1, 2) # N, C, T
|
732 |
+
|
733 |
+
|
734 |
+
class CodePredictor(nn.Module):
|
735 |
+
def __init__(
|
736 |
+
self,
|
737 |
+
hidden_channels,
|
738 |
+
filter_channels,
|
739 |
+
n_heads,
|
740 |
+
n_layers,
|
741 |
+
kernel_size,
|
742 |
+
p_dropout,
|
743 |
+
n_q=8,
|
744 |
+
dims=1024,
|
745 |
+
ssl_dim=768,
|
746 |
+
):
|
747 |
+
super().__init__()
|
748 |
+
self.hidden_channels = hidden_channels
|
749 |
+
self.filter_channels = filter_channels
|
750 |
+
self.n_heads = n_heads
|
751 |
+
self.n_layers = n_layers
|
752 |
+
self.kernel_size = kernel_size
|
753 |
+
self.p_dropout = p_dropout
|
754 |
+
|
755 |
+
self.vq_proj = nn.Conv1d(ssl_dim, hidden_channels, 1)
|
756 |
+
self.ref_enc = modules.MelStyleEncoder(
|
757 |
+
ssl_dim, style_vector_dim=hidden_channels
|
758 |
+
)
|
759 |
+
|
760 |
+
self.encoder = attentions.Encoder(
|
761 |
+
hidden_channels, filter_channels, n_heads, n_layers, kernel_size, p_dropout
|
762 |
+
)
|
763 |
+
|
764 |
+
self.out_proj = nn.Conv1d(hidden_channels, (n_q - 1) * dims, 1)
|
765 |
+
self.n_q = n_q
|
766 |
+
self.dims = dims
|
767 |
+
|
768 |
+
def forward(self, x, x_mask, refer, codes, infer=False):
|
769 |
+
x = x.detach()
|
770 |
+
x = self.vq_proj(x * x_mask) * x_mask
|
771 |
+
g = self.ref_enc(refer, x_mask)
|
772 |
+
x = x + g
|
773 |
+
x = self.encoder(x * x_mask, x_mask)
|
774 |
+
x = self.out_proj(x * x_mask) * x_mask
|
775 |
+
logits = x.reshape(x.shape[0], self.n_q - 1, self.dims, x.shape[-1]).transpose(
|
776 |
+
2, 3
|
777 |
+
)
|
778 |
+
target = codes[1:].transpose(0, 1)
|
779 |
+
if not infer:
|
780 |
+
logits = logits.reshape(-1, self.dims)
|
781 |
+
target = target.reshape(-1)
|
782 |
+
loss = torch.nn.functional.cross_entropy(logits, target)
|
783 |
+
return loss
|
784 |
+
else:
|
785 |
+
_, top10_preds = torch.topk(logits, 10, dim=-1)
|
786 |
+
correct_top10 = torch.any(top10_preds == target.unsqueeze(-1), dim=-1)
|
787 |
+
top3_acc = 100 * torch.mean(correct_top10.float()).detach().cpu().item()
|
788 |
+
|
789 |
+
print("Top-10 Accuracy:", top3_acc, "%")
|
790 |
+
|
791 |
+
pred_codes = torch.argmax(logits, dim=-1)
|
792 |
+
acc = 100 * torch.mean((pred_codes == target).float()).detach().cpu().item()
|
793 |
+
print("Top-1 Accuracy:", acc, "%")
|
794 |
+
|
795 |
+
return pred_codes.transpose(0, 1)
|
796 |
+
|
797 |
+
|
798 |
+
class SynthesizerTrn(nn.Module):
|
799 |
+
"""
|
800 |
+
Synthesizer for Training
|
801 |
+
"""
|
802 |
+
|
803 |
+
def __init__(
|
804 |
+
self,
|
805 |
+
spec_channels,
|
806 |
+
segment_size,
|
807 |
+
inter_channels,
|
808 |
+
hidden_channels,
|
809 |
+
filter_channels,
|
810 |
+
n_heads,
|
811 |
+
n_layers,
|
812 |
+
kernel_size,
|
813 |
+
p_dropout,
|
814 |
+
resblock,
|
815 |
+
resblock_kernel_sizes,
|
816 |
+
resblock_dilation_sizes,
|
817 |
+
upsample_rates,
|
818 |
+
upsample_initial_channel,
|
819 |
+
upsample_kernel_sizes,
|
820 |
+
n_speakers=0,
|
821 |
+
gin_channels=0,
|
822 |
+
use_sdp=True,
|
823 |
+
semantic_frame_rate=None,
|
824 |
+
freeze_quantizer=None,
|
825 |
+
**kwargs
|
826 |
+
):
|
827 |
+
super().__init__()
|
828 |
+
self.spec_channels = spec_channels
|
829 |
+
self.inter_channels = inter_channels
|
830 |
+
self.hidden_channels = hidden_channels
|
831 |
+
self.filter_channels = filter_channels
|
832 |
+
self.n_heads = n_heads
|
833 |
+
self.n_layers = n_layers
|
834 |
+
self.kernel_size = kernel_size
|
835 |
+
self.p_dropout = p_dropout
|
836 |
+
self.resblock = resblock
|
837 |
+
self.resblock_kernel_sizes = resblock_kernel_sizes
|
838 |
+
self.resblock_dilation_sizes = resblock_dilation_sizes
|
839 |
+
self.upsample_rates = upsample_rates
|
840 |
+
self.upsample_initial_channel = upsample_initial_channel
|
841 |
+
self.upsample_kernel_sizes = upsample_kernel_sizes
|
842 |
+
self.segment_size = segment_size
|
843 |
+
self.n_speakers = n_speakers
|
844 |
+
self.gin_channels = gin_channels
|
845 |
+
|
846 |
+
self.use_sdp = use_sdp
|
847 |
+
self.enc_p = TextEncoder(
|
848 |
+
inter_channels,
|
849 |
+
hidden_channels,
|
850 |
+
filter_channels,
|
851 |
+
n_heads,
|
852 |
+
n_layers,
|
853 |
+
kernel_size,
|
854 |
+
p_dropout,
|
855 |
+
)
|
856 |
+
self.dec = Generator(
|
857 |
+
inter_channels,
|
858 |
+
resblock,
|
859 |
+
resblock_kernel_sizes,
|
860 |
+
resblock_dilation_sizes,
|
861 |
+
upsample_rates,
|
862 |
+
upsample_initial_channel,
|
863 |
+
upsample_kernel_sizes,
|
864 |
+
gin_channels=gin_channels,
|
865 |
+
)
|
866 |
+
self.enc_q = PosteriorEncoder(
|
867 |
+
spec_channels,
|
868 |
+
inter_channels,
|
869 |
+
hidden_channels,
|
870 |
+
5,
|
871 |
+
1,
|
872 |
+
16,
|
873 |
+
gin_channels=gin_channels,
|
874 |
+
)
|
875 |
+
self.flow = ResidualCouplingBlock(
|
876 |
+
inter_channels, hidden_channels, 5, 1, 4, gin_channels=gin_channels
|
877 |
+
)
|
878 |
+
|
879 |
+
self.ref_enc = modules.MelStyleEncoder(
|
880 |
+
spec_channels, style_vector_dim=gin_channels
|
881 |
+
)
|
882 |
+
|
883 |
+
ssl_dim = 768
|
884 |
+
assert semantic_frame_rate in ["25hz", "50hz"]
|
885 |
+
self.semantic_frame_rate = semantic_frame_rate
|
886 |
+
if semantic_frame_rate == "25hz":
|
887 |
+
self.ssl_proj = nn.Conv1d(ssl_dim, ssl_dim, 2, stride=2)
|
888 |
+
else:
|
889 |
+
self.ssl_proj = nn.Conv1d(ssl_dim, ssl_dim, 1, stride=1)
|
890 |
+
|
891 |
+
self.quantizer = ResidualVectorQuantizer(dimension=ssl_dim, n_q=1, bins=1024)
|
892 |
+
if freeze_quantizer:
|
893 |
+
self.ssl_proj.requires_grad_(False)
|
894 |
+
self.quantizer.requires_grad_(False)
|
895 |
+
# self.enc_p.text_embedding.requires_grad_(False)
|
896 |
+
# self.enc_p.encoder_text.requires_grad_(False)
|
897 |
+
# self.enc_p.mrte.requires_grad_(False)
|
898 |
+
|
899 |
+
def forward(self, ssl, y, y_lengths, text, text_lengths):
|
900 |
+
y_mask = torch.unsqueeze(commons.sequence_mask(y_lengths, y.size(2)), 1).to(
|
901 |
+
y.dtype
|
902 |
+
)
|
903 |
+
ge = self.ref_enc(y * y_mask, y_mask)
|
904 |
+
|
905 |
+
with autocast(enabled=False):
|
906 |
+
ssl = self.ssl_proj(ssl)
|
907 |
+
quantized, codes, commit_loss, quantized_list = self.quantizer(
|
908 |
+
ssl, layers=[0]
|
909 |
+
)
|
910 |
+
|
911 |
+
if self.semantic_frame_rate == "25hz":
|
912 |
+
quantized = F.interpolate(
|
913 |
+
quantized, size=int(quantized.shape[-1] * 2), mode="nearest"
|
914 |
+
)
|
915 |
+
|
916 |
+
x, m_p, logs_p, y_mask = self.enc_p(
|
917 |
+
quantized, y_lengths, text, text_lengths, ge
|
918 |
+
)
|
919 |
+
z, m_q, logs_q, y_mask = self.enc_q(y, y_lengths, g=ge)
|
920 |
+
z_p = self.flow(z, y_mask, g=ge)
|
921 |
+
|
922 |
+
z_slice, ids_slice = commons.rand_slice_segments(
|
923 |
+
z, y_lengths, self.segment_size
|
924 |
+
)
|
925 |
+
o = self.dec(z_slice, g=ge)
|
926 |
+
return (
|
927 |
+
o,
|
928 |
+
commit_loss,
|
929 |
+
ids_slice,
|
930 |
+
y_mask,
|
931 |
+
y_mask,
|
932 |
+
(z, z_p, m_p, logs_p, m_q, logs_q),
|
933 |
+
quantized,
|
934 |
+
)
|
935 |
+
|
936 |
+
def infer(self, ssl, y, y_lengths, text, text_lengths, test=None, noise_scale=0.5):
|
937 |
+
y_mask = torch.unsqueeze(commons.sequence_mask(y_lengths, y.size(2)), 1).to(
|
938 |
+
y.dtype
|
939 |
+
)
|
940 |
+
ge = self.ref_enc(y * y_mask, y_mask)
|
941 |
+
|
942 |
+
ssl = self.ssl_proj(ssl)
|
943 |
+
quantized, codes, commit_loss, _ = self.quantizer(ssl, layers=[0])
|
944 |
+
if self.semantic_frame_rate == "25hz":
|
945 |
+
quantized = F.interpolate(
|
946 |
+
quantized, size=int(quantized.shape[-1] * 2), mode="nearest"
|
947 |
+
)
|
948 |
+
|
949 |
+
x, m_p, logs_p, y_mask = self.enc_p(
|
950 |
+
quantized, y_lengths, text, text_lengths, ge, test=test
|
951 |
+
)
|
952 |
+
z_p = m_p + torch.randn_like(m_p) * torch.exp(logs_p) * noise_scale
|
953 |
+
|
954 |
+
z = self.flow(z_p, y_mask, g=ge, reverse=True)
|
955 |
+
|
956 |
+
o = self.dec((z * y_mask)[:, :, :], g=ge)
|
957 |
+
return o, y_mask, (z, z_p, m_p, logs_p)
|
958 |
+
|
959 |
+
@torch.no_grad()
|
960 |
+
def decode(self, codes, text, refer, noise_scale=0.5):
|
961 |
+
ge = None
|
962 |
+
if refer is not None:
|
963 |
+
refer_lengths = torch.LongTensor([refer.size(2)]).to(refer.device)
|
964 |
+
refer_mask = torch.unsqueeze(
|
965 |
+
commons.sequence_mask(refer_lengths, refer.size(2)), 1
|
966 |
+
).to(refer.dtype)
|
967 |
+
ge = self.ref_enc(refer * refer_mask, refer_mask)
|
968 |
+
|
969 |
+
y_lengths = torch.LongTensor([codes.size(2) * 2]).to(codes.device)
|
970 |
+
text_lengths = torch.LongTensor([text.size(-1)]).to(text.device)
|
971 |
+
|
972 |
+
quantized = self.quantizer.decode(codes)
|
973 |
+
if self.semantic_frame_rate == "25hz":
|
974 |
+
quantized = F.interpolate(
|
975 |
+
quantized, size=int(quantized.shape[-1] * 2), mode="nearest"
|
976 |
+
)
|
977 |
+
|
978 |
+
x, m_p, logs_p, y_mask = self.enc_p(
|
979 |
+
quantized, y_lengths, text, text_lengths, ge
|
980 |
+
)
|
981 |
+
z_p = m_p + torch.randn_like(m_p) * torch.exp(logs_p) * noise_scale
|
982 |
+
|
983 |
+
z = self.flow(z_p, y_mask, g=ge, reverse=True)
|
984 |
+
|
985 |
+
o = self.dec((z * y_mask)[:, :, :], g=ge)
|
986 |
+
return o
|
987 |
+
|
988 |
+
def extract_latent(self, x):
|
989 |
+
ssl = self.ssl_proj(x)
|
990 |
+
quantized, codes, commit_loss, quantized_list = self.quantizer(ssl)
|
991 |
+
return codes.transpose(0, 1)
|
modules.py
ADDED
@@ -0,0 +1,923 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
from torch import nn
|
5 |
+
from torch.nn import functional as F
|
6 |
+
|
7 |
+
from torch.nn import Conv1d
|
8 |
+
from torch.nn.utils import weight_norm, remove_weight_norm
|
9 |
+
|
10 |
+
from . import commons
|
11 |
+
from .commons import init_weights, get_padding
|
12 |
+
from .transforms import piecewise_rational_quadratic_transform
|
13 |
+
import torch.distributions as D
|
14 |
+
|
15 |
+
|
16 |
+
LRELU_SLOPE = 0.1
|
17 |
+
|
18 |
+
|
19 |
+
class LayerNorm(nn.Module):
|
20 |
+
def __init__(self, channels, eps=1e-5):
|
21 |
+
super().__init__()
|
22 |
+
self.channels = channels
|
23 |
+
self.eps = eps
|
24 |
+
|
25 |
+
self.gamma = nn.Parameter(torch.ones(channels))
|
26 |
+
self.beta = nn.Parameter(torch.zeros(channels))
|
27 |
+
|
28 |
+
def forward(self, x):
|
29 |
+
x = x.transpose(1, -1)
|
30 |
+
x = F.layer_norm(x, (self.channels,), self.gamma, self.beta, self.eps)
|
31 |
+
return x.transpose(1, -1)
|
32 |
+
|
33 |
+
|
34 |
+
class ConvReluNorm(nn.Module):
|
35 |
+
def __init__(
|
36 |
+
self,
|
37 |
+
in_channels,
|
38 |
+
hidden_channels,
|
39 |
+
out_channels,
|
40 |
+
kernel_size,
|
41 |
+
n_layers,
|
42 |
+
p_dropout,
|
43 |
+
):
|
44 |
+
super().__init__()
|
45 |
+
self.in_channels = in_channels
|
46 |
+
self.hidden_channels = hidden_channels
|
47 |
+
self.out_channels = out_channels
|
48 |
+
self.kernel_size = kernel_size
|
49 |
+
self.n_layers = n_layers
|
50 |
+
self.p_dropout = p_dropout
|
51 |
+
assert n_layers > 1, "Number of layers should be larger than 0."
|
52 |
+
|
53 |
+
self.conv_layers = nn.ModuleList()
|
54 |
+
self.norm_layers = nn.ModuleList()
|
55 |
+
self.conv_layers.append(
|
56 |
+
nn.Conv1d(
|
57 |
+
in_channels, hidden_channels, kernel_size, padding=kernel_size // 2
|
58 |
+
)
|
59 |
+
)
|
60 |
+
self.norm_layers.append(LayerNorm(hidden_channels))
|
61 |
+
self.relu_drop = nn.Sequential(nn.ReLU(), nn.Dropout(p_dropout))
|
62 |
+
for _ in range(n_layers - 1):
|
63 |
+
self.conv_layers.append(
|
64 |
+
nn.Conv1d(
|
65 |
+
hidden_channels,
|
66 |
+
hidden_channels,
|
67 |
+
kernel_size,
|
68 |
+
padding=kernel_size // 2,
|
69 |
+
)
|
70 |
+
)
|
71 |
+
self.norm_layers.append(LayerNorm(hidden_channels))
|
72 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels, 1)
|
73 |
+
self.proj.weight.data.zero_()
|
74 |
+
self.proj.bias.data.zero_()
|
75 |
+
|
76 |
+
def forward(self, x, x_mask):
|
77 |
+
x_org = x
|
78 |
+
for i in range(self.n_layers):
|
79 |
+
x = self.conv_layers[i](x * x_mask)
|
80 |
+
x = self.norm_layers[i](x)
|
81 |
+
x = self.relu_drop(x)
|
82 |
+
x = x_org + self.proj(x)
|
83 |
+
return x * x_mask
|
84 |
+
|
85 |
+
|
86 |
+
class DDSConv(nn.Module):
|
87 |
+
"""
|
88 |
+
Dialted and Depth-Separable Convolution
|
89 |
+
"""
|
90 |
+
|
91 |
+
def __init__(self, channels, kernel_size, n_layers, p_dropout=0.0):
|
92 |
+
super().__init__()
|
93 |
+
self.channels = channels
|
94 |
+
self.kernel_size = kernel_size
|
95 |
+
self.n_layers = n_layers
|
96 |
+
self.p_dropout = p_dropout
|
97 |
+
|
98 |
+
self.drop = nn.Dropout(p_dropout)
|
99 |
+
self.convs_sep = nn.ModuleList()
|
100 |
+
self.convs_1x1 = nn.ModuleList()
|
101 |
+
self.norms_1 = nn.ModuleList()
|
102 |
+
self.norms_2 = nn.ModuleList()
|
103 |
+
for i in range(n_layers):
|
104 |
+
dilation = kernel_size**i
|
105 |
+
padding = (kernel_size * dilation - dilation) // 2
|
106 |
+
self.convs_sep.append(
|
107 |
+
nn.Conv1d(
|
108 |
+
channels,
|
109 |
+
channels,
|
110 |
+
kernel_size,
|
111 |
+
groups=channels,
|
112 |
+
dilation=dilation,
|
113 |
+
padding=padding,
|
114 |
+
)
|
115 |
+
)
|
116 |
+
self.convs_1x1.append(nn.Conv1d(channels, channels, 1))
|
117 |
+
self.norms_1.append(LayerNorm(channels))
|
118 |
+
self.norms_2.append(LayerNorm(channels))
|
119 |
+
|
120 |
+
def forward(self, x, x_mask, g=None):
|
121 |
+
if g is not None:
|
122 |
+
x = x + g
|
123 |
+
for i in range(self.n_layers):
|
124 |
+
y = self.convs_sep[i](x * x_mask)
|
125 |
+
y = self.norms_1[i](y)
|
126 |
+
y = F.gelu(y)
|
127 |
+
y = self.convs_1x1[i](y)
|
128 |
+
y = self.norms_2[i](y)
|
129 |
+
y = F.gelu(y)
|
130 |
+
y = self.drop(y)
|
131 |
+
x = x + y
|
132 |
+
return x * x_mask
|
133 |
+
|
134 |
+
|
135 |
+
class WN(torch.nn.Module):
|
136 |
+
def __init__(
|
137 |
+
self,
|
138 |
+
hidden_channels,
|
139 |
+
kernel_size,
|
140 |
+
dilation_rate,
|
141 |
+
n_layers,
|
142 |
+
gin_channels=0,
|
143 |
+
p_dropout=0,
|
144 |
+
):
|
145 |
+
super(WN, self).__init__()
|
146 |
+
assert kernel_size % 2 == 1
|
147 |
+
self.hidden_channels = hidden_channels
|
148 |
+
self.kernel_size = (kernel_size,)
|
149 |
+
self.dilation_rate = dilation_rate
|
150 |
+
self.n_layers = n_layers
|
151 |
+
self.gin_channels = gin_channels
|
152 |
+
self.p_dropout = p_dropout
|
153 |
+
|
154 |
+
self.in_layers = torch.nn.ModuleList()
|
155 |
+
self.res_skip_layers = torch.nn.ModuleList()
|
156 |
+
self.drop = nn.Dropout(p_dropout)
|
157 |
+
|
158 |
+
if gin_channels != 0:
|
159 |
+
cond_layer = torch.nn.Conv1d(
|
160 |
+
gin_channels, 2 * hidden_channels * n_layers, 1
|
161 |
+
)
|
162 |
+
self.cond_layer = torch.nn.utils.weight_norm(cond_layer, name="weight")
|
163 |
+
|
164 |
+
for i in range(n_layers):
|
165 |
+
dilation = dilation_rate**i
|
166 |
+
padding = int((kernel_size * dilation - dilation) / 2)
|
167 |
+
in_layer = torch.nn.Conv1d(
|
168 |
+
hidden_channels,
|
169 |
+
2 * hidden_channels,
|
170 |
+
kernel_size,
|
171 |
+
dilation=dilation,
|
172 |
+
padding=padding,
|
173 |
+
)
|
174 |
+
in_layer = torch.nn.utils.weight_norm(in_layer, name="weight")
|
175 |
+
self.in_layers.append(in_layer)
|
176 |
+
|
177 |
+
# last one is not necessary
|
178 |
+
if i < n_layers - 1:
|
179 |
+
res_skip_channels = 2 * hidden_channels
|
180 |
+
else:
|
181 |
+
res_skip_channels = hidden_channels
|
182 |
+
|
183 |
+
res_skip_layer = torch.nn.Conv1d(hidden_channels, res_skip_channels, 1)
|
184 |
+
res_skip_layer = torch.nn.utils.weight_norm(res_skip_layer, name="weight")
|
185 |
+
self.res_skip_layers.append(res_skip_layer)
|
186 |
+
|
187 |
+
def forward(self, x, x_mask, g=None, **kwargs):
|
188 |
+
output = torch.zeros_like(x)
|
189 |
+
n_channels_tensor = torch.IntTensor([self.hidden_channels])
|
190 |
+
|
191 |
+
if g is not None:
|
192 |
+
g = self.cond_layer(g)
|
193 |
+
|
194 |
+
for i in range(self.n_layers):
|
195 |
+
x_in = self.in_layers[i](x)
|
196 |
+
if g is not None:
|
197 |
+
cond_offset = i * 2 * self.hidden_channels
|
198 |
+
g_l = g[:, cond_offset : cond_offset + 2 * self.hidden_channels, :]
|
199 |
+
else:
|
200 |
+
g_l = torch.zeros_like(x_in)
|
201 |
+
|
202 |
+
acts = commons.fused_add_tanh_sigmoid_multiply(x_in, g_l, n_channels_tensor)
|
203 |
+
acts = self.drop(acts)
|
204 |
+
|
205 |
+
res_skip_acts = self.res_skip_layers[i](acts)
|
206 |
+
if i < self.n_layers - 1:
|
207 |
+
res_acts = res_skip_acts[:, : self.hidden_channels, :]
|
208 |
+
x = (x + res_acts) * x_mask
|
209 |
+
output = output + res_skip_acts[:, self.hidden_channels :, :]
|
210 |
+
else:
|
211 |
+
output = output + res_skip_acts
|
212 |
+
return output * x_mask
|
213 |
+
|
214 |
+
def remove_weight_norm(self):
|
215 |
+
if self.gin_channels != 0:
|
216 |
+
torch.nn.utils.remove_weight_norm(self.cond_layer)
|
217 |
+
for l in self.in_layers:
|
218 |
+
torch.nn.utils.remove_weight_norm(l)
|
219 |
+
for l in self.res_skip_layers:
|
220 |
+
torch.nn.utils.remove_weight_norm(l)
|
221 |
+
|
222 |
+
|
223 |
+
class ResBlock1(torch.nn.Module):
|
224 |
+
def __init__(self, channels, kernel_size=3, dilation=(1, 3, 5)):
|
225 |
+
super(ResBlock1, self).__init__()
|
226 |
+
self.convs1 = nn.ModuleList(
|
227 |
+
[
|
228 |
+
weight_norm(
|
229 |
+
Conv1d(
|
230 |
+
channels,
|
231 |
+
channels,
|
232 |
+
kernel_size,
|
233 |
+
1,
|
234 |
+
dilation=dilation[0],
|
235 |
+
padding=get_padding(kernel_size, dilation[0]),
|
236 |
+
)
|
237 |
+
),
|
238 |
+
weight_norm(
|
239 |
+
Conv1d(
|
240 |
+
channels,
|
241 |
+
channels,
|
242 |
+
kernel_size,
|
243 |
+
1,
|
244 |
+
dilation=dilation[1],
|
245 |
+
padding=get_padding(kernel_size, dilation[1]),
|
246 |
+
)
|
247 |
+
),
|
248 |
+
weight_norm(
|
249 |
+
Conv1d(
|
250 |
+
channels,
|
251 |
+
channels,
|
252 |
+
kernel_size,
|
253 |
+
1,
|
254 |
+
dilation=dilation[2],
|
255 |
+
padding=get_padding(kernel_size, dilation[2]),
|
256 |
+
)
|
257 |
+
),
|
258 |
+
]
|
259 |
+
)
|
260 |
+
self.convs1.apply(init_weights)
|
261 |
+
|
262 |
+
self.convs2 = nn.ModuleList(
|
263 |
+
[
|
264 |
+
weight_norm(
|
265 |
+
Conv1d(
|
266 |
+
channels,
|
267 |
+
channels,
|
268 |
+
kernel_size,
|
269 |
+
1,
|
270 |
+
dilation=1,
|
271 |
+
padding=get_padding(kernel_size, 1),
|
272 |
+
)
|
273 |
+
),
|
274 |
+
weight_norm(
|
275 |
+
Conv1d(
|
276 |
+
channels,
|
277 |
+
channels,
|
278 |
+
kernel_size,
|
279 |
+
1,
|
280 |
+
dilation=1,
|
281 |
+
padding=get_padding(kernel_size, 1),
|
282 |
+
)
|
283 |
+
),
|
284 |
+
weight_norm(
|
285 |
+
Conv1d(
|
286 |
+
channels,
|
287 |
+
channels,
|
288 |
+
kernel_size,
|
289 |
+
1,
|
290 |
+
dilation=1,
|
291 |
+
padding=get_padding(kernel_size, 1),
|
292 |
+
)
|
293 |
+
),
|
294 |
+
]
|
295 |
+
)
|
296 |
+
self.convs2.apply(init_weights)
|
297 |
+
|
298 |
+
def forward(self, x, x_mask=None):
|
299 |
+
for c1, c2 in zip(self.convs1, self.convs2):
|
300 |
+
xt = F.leaky_relu(x, LRELU_SLOPE)
|
301 |
+
if x_mask is not None:
|
302 |
+
xt = xt * x_mask
|
303 |
+
xt = c1(xt)
|
304 |
+
xt = F.leaky_relu(xt, LRELU_SLOPE)
|
305 |
+
if x_mask is not None:
|
306 |
+
xt = xt * x_mask
|
307 |
+
xt = c2(xt)
|
308 |
+
x = xt + x
|
309 |
+
if x_mask is not None:
|
310 |
+
x = x * x_mask
|
311 |
+
return x
|
312 |
+
|
313 |
+
def remove_weight_norm(self):
|
314 |
+
for l in self.convs1:
|
315 |
+
remove_weight_norm(l)
|
316 |
+
for l in self.convs2:
|
317 |
+
remove_weight_norm(l)
|
318 |
+
|
319 |
+
|
320 |
+
class ResBlock2(torch.nn.Module):
|
321 |
+
def __init__(self, channels, kernel_size=3, dilation=(1, 3)):
|
322 |
+
super(ResBlock2, self).__init__()
|
323 |
+
self.convs = nn.ModuleList(
|
324 |
+
[
|
325 |
+
weight_norm(
|
326 |
+
Conv1d(
|
327 |
+
channels,
|
328 |
+
channels,
|
329 |
+
kernel_size,
|
330 |
+
1,
|
331 |
+
dilation=dilation[0],
|
332 |
+
padding=get_padding(kernel_size, dilation[0]),
|
333 |
+
)
|
334 |
+
),
|
335 |
+
weight_norm(
|
336 |
+
Conv1d(
|
337 |
+
channels,
|
338 |
+
channels,
|
339 |
+
kernel_size,
|
340 |
+
1,
|
341 |
+
dilation=dilation[1],
|
342 |
+
padding=get_padding(kernel_size, dilation[1]),
|
343 |
+
)
|
344 |
+
),
|
345 |
+
]
|
346 |
+
)
|
347 |
+
self.convs.apply(init_weights)
|
348 |
+
|
349 |
+
def forward(self, x, x_mask=None):
|
350 |
+
for c in self.convs:
|
351 |
+
xt = F.leaky_relu(x, LRELU_SLOPE)
|
352 |
+
if x_mask is not None:
|
353 |
+
xt = xt * x_mask
|
354 |
+
xt = c(xt)
|
355 |
+
x = xt + x
|
356 |
+
if x_mask is not None:
|
357 |
+
x = x * x_mask
|
358 |
+
return x
|
359 |
+
|
360 |
+
def remove_weight_norm(self):
|
361 |
+
for l in self.convs:
|
362 |
+
remove_weight_norm(l)
|
363 |
+
|
364 |
+
|
365 |
+
class Log(nn.Module):
|
366 |
+
def forward(self, x, x_mask, reverse=False, **kwargs):
|
367 |
+
if not reverse:
|
368 |
+
y = torch.log(torch.clamp_min(x, 1e-5)) * x_mask
|
369 |
+
logdet = torch.sum(-y, [1, 2])
|
370 |
+
return y, logdet
|
371 |
+
else:
|
372 |
+
x = torch.exp(x) * x_mask
|
373 |
+
return x
|
374 |
+
|
375 |
+
|
376 |
+
class Flip(nn.Module):
|
377 |
+
def forward(self, x, *args, reverse=False, **kwargs):
|
378 |
+
x = torch.flip(x, [1])
|
379 |
+
if not reverse:
|
380 |
+
logdet = torch.zeros(x.size(0)).to(dtype=x.dtype, device=x.device)
|
381 |
+
return x, logdet
|
382 |
+
else:
|
383 |
+
return x
|
384 |
+
|
385 |
+
|
386 |
+
class ElementwiseAffine(nn.Module):
|
387 |
+
def __init__(self, channels):
|
388 |
+
super().__init__()
|
389 |
+
self.channels = channels
|
390 |
+
self.m = nn.Parameter(torch.zeros(channels, 1))
|
391 |
+
self.logs = nn.Parameter(torch.zeros(channels, 1))
|
392 |
+
|
393 |
+
def forward(self, x, x_mask, reverse=False, **kwargs):
|
394 |
+
if not reverse:
|
395 |
+
y = self.m + torch.exp(self.logs) * x
|
396 |
+
y = y * x_mask
|
397 |
+
logdet = torch.sum(self.logs * x_mask, [1, 2])
|
398 |
+
return y, logdet
|
399 |
+
else:
|
400 |
+
x = (x - self.m) * torch.exp(-self.logs) * x_mask
|
401 |
+
return x
|
402 |
+
|
403 |
+
|
404 |
+
class ResidualCouplingLayer(nn.Module):
|
405 |
+
def __init__(
|
406 |
+
self,
|
407 |
+
channels,
|
408 |
+
hidden_channels,
|
409 |
+
kernel_size,
|
410 |
+
dilation_rate,
|
411 |
+
n_layers,
|
412 |
+
p_dropout=0,
|
413 |
+
gin_channels=0,
|
414 |
+
mean_only=False,
|
415 |
+
):
|
416 |
+
assert channels % 2 == 0, "channels should be divisible by 2"
|
417 |
+
super().__init__()
|
418 |
+
self.channels = channels
|
419 |
+
self.hidden_channels = hidden_channels
|
420 |
+
self.kernel_size = kernel_size
|
421 |
+
self.dilation_rate = dilation_rate
|
422 |
+
self.n_layers = n_layers
|
423 |
+
self.half_channels = channels // 2
|
424 |
+
self.mean_only = mean_only
|
425 |
+
|
426 |
+
self.pre = nn.Conv1d(self.half_channels, hidden_channels, 1)
|
427 |
+
self.enc = WN(
|
428 |
+
hidden_channels,
|
429 |
+
kernel_size,
|
430 |
+
dilation_rate,
|
431 |
+
n_layers,
|
432 |
+
p_dropout=p_dropout,
|
433 |
+
gin_channels=gin_channels,
|
434 |
+
)
|
435 |
+
self.post = nn.Conv1d(hidden_channels, self.half_channels * (2 - mean_only), 1)
|
436 |
+
self.post.weight.data.zero_()
|
437 |
+
self.post.bias.data.zero_()
|
438 |
+
|
439 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
440 |
+
x0, x1 = torch.split(x, [self.half_channels] * 2, 1)
|
441 |
+
h = self.pre(x0) * x_mask
|
442 |
+
h = self.enc(h, x_mask, g=g)
|
443 |
+
stats = self.post(h) * x_mask
|
444 |
+
if not self.mean_only:
|
445 |
+
m, logs = torch.split(stats, [self.half_channels] * 2, 1)
|
446 |
+
else:
|
447 |
+
m = stats
|
448 |
+
logs = torch.zeros_like(m)
|
449 |
+
|
450 |
+
if not reverse:
|
451 |
+
x1 = m + x1 * torch.exp(logs) * x_mask
|
452 |
+
x = torch.cat([x0, x1], 1)
|
453 |
+
logdet = torch.sum(logs, [1, 2])
|
454 |
+
return x, logdet
|
455 |
+
else:
|
456 |
+
x1 = (x1 - m) * torch.exp(-logs) * x_mask
|
457 |
+
x = torch.cat([x0, x1], 1)
|
458 |
+
return x
|
459 |
+
|
460 |
+
|
461 |
+
class ConvFlow(nn.Module):
|
462 |
+
def __init__(
|
463 |
+
self,
|
464 |
+
in_channels,
|
465 |
+
filter_channels,
|
466 |
+
kernel_size,
|
467 |
+
n_layers,
|
468 |
+
num_bins=10,
|
469 |
+
tail_bound=5.0,
|
470 |
+
):
|
471 |
+
super().__init__()
|
472 |
+
self.in_channels = in_channels
|
473 |
+
self.filter_channels = filter_channels
|
474 |
+
self.kernel_size = kernel_size
|
475 |
+
self.n_layers = n_layers
|
476 |
+
self.num_bins = num_bins
|
477 |
+
self.tail_bound = tail_bound
|
478 |
+
self.half_channels = in_channels // 2
|
479 |
+
|
480 |
+
self.pre = nn.Conv1d(self.half_channels, filter_channels, 1)
|
481 |
+
self.convs = DDSConv(filter_channels, kernel_size, n_layers, p_dropout=0.0)
|
482 |
+
self.proj = nn.Conv1d(
|
483 |
+
filter_channels, self.half_channels * (num_bins * 3 - 1), 1
|
484 |
+
)
|
485 |
+
self.proj.weight.data.zero_()
|
486 |
+
self.proj.bias.data.zero_()
|
487 |
+
|
488 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
489 |
+
x0, x1 = torch.split(x, [self.half_channels] * 2, 1)
|
490 |
+
h = self.pre(x0)
|
491 |
+
h = self.convs(h, x_mask, g=g)
|
492 |
+
h = self.proj(h) * x_mask
|
493 |
+
|
494 |
+
b, c, t = x0.shape
|
495 |
+
h = h.reshape(b, c, -1, t).permute(0, 1, 3, 2) # [b, cx?, t] -> [b, c, t, ?]
|
496 |
+
|
497 |
+
unnormalized_widths = h[..., : self.num_bins] / math.sqrt(self.filter_channels)
|
498 |
+
unnormalized_heights = h[..., self.num_bins : 2 * self.num_bins] / math.sqrt(
|
499 |
+
self.filter_channels
|
500 |
+
)
|
501 |
+
unnormalized_derivatives = h[..., 2 * self.num_bins :]
|
502 |
+
|
503 |
+
x1, logabsdet = piecewise_rational_quadratic_transform(
|
504 |
+
x1,
|
505 |
+
unnormalized_widths,
|
506 |
+
unnormalized_heights,
|
507 |
+
unnormalized_derivatives,
|
508 |
+
inverse=reverse,
|
509 |
+
tails="linear",
|
510 |
+
tail_bound=self.tail_bound,
|
511 |
+
)
|
512 |
+
|
513 |
+
x = torch.cat([x0, x1], 1) * x_mask
|
514 |
+
logdet = torch.sum(logabsdet * x_mask, [1, 2])
|
515 |
+
if not reverse:
|
516 |
+
return x, logdet
|
517 |
+
else:
|
518 |
+
return x
|
519 |
+
|
520 |
+
|
521 |
+
class LinearNorm(nn.Module):
|
522 |
+
def __init__(
|
523 |
+
self,
|
524 |
+
in_channels,
|
525 |
+
out_channels,
|
526 |
+
bias=True,
|
527 |
+
spectral_norm=False,
|
528 |
+
):
|
529 |
+
super(LinearNorm, self).__init__()
|
530 |
+
self.fc = nn.Linear(in_channels, out_channels, bias)
|
531 |
+
|
532 |
+
if spectral_norm:
|
533 |
+
self.fc = nn.utils.spectral_norm(self.fc)
|
534 |
+
|
535 |
+
def forward(self, input):
|
536 |
+
out = self.fc(input)
|
537 |
+
return out
|
538 |
+
|
539 |
+
|
540 |
+
class Mish(nn.Module):
|
541 |
+
def __init__(self):
|
542 |
+
super(Mish, self).__init__()
|
543 |
+
|
544 |
+
def forward(self, x):
|
545 |
+
return x * torch.tanh(F.softplus(x))
|
546 |
+
|
547 |
+
|
548 |
+
class Conv1dGLU(nn.Module):
|
549 |
+
"""
|
550 |
+
Conv1d + GLU(Gated Linear Unit) with residual connection.
|
551 |
+
For GLU refer to https://arxiv.org/abs/1612.08083 paper.
|
552 |
+
"""
|
553 |
+
|
554 |
+
def __init__(self, in_channels, out_channels, kernel_size, dropout):
|
555 |
+
super(Conv1dGLU, self).__init__()
|
556 |
+
self.out_channels = out_channels
|
557 |
+
self.conv1 = ConvNorm(in_channels, 2 * out_channels, kernel_size=kernel_size)
|
558 |
+
self.dropout = nn.Dropout(dropout)
|
559 |
+
|
560 |
+
def forward(self, x):
|
561 |
+
residual = x
|
562 |
+
x = self.conv1(x)
|
563 |
+
x1, x2 = torch.split(x, split_size_or_sections=self.out_channels, dim=1)
|
564 |
+
x = x1 * torch.sigmoid(x2)
|
565 |
+
x = residual + self.dropout(x)
|
566 |
+
return x
|
567 |
+
|
568 |
+
|
569 |
+
class ConvNorm(nn.Module):
|
570 |
+
def __init__(
|
571 |
+
self,
|
572 |
+
in_channels,
|
573 |
+
out_channels,
|
574 |
+
kernel_size=1,
|
575 |
+
stride=1,
|
576 |
+
padding=None,
|
577 |
+
dilation=1,
|
578 |
+
bias=True,
|
579 |
+
spectral_norm=False,
|
580 |
+
):
|
581 |
+
super(ConvNorm, self).__init__()
|
582 |
+
|
583 |
+
if padding is None:
|
584 |
+
assert kernel_size % 2 == 1
|
585 |
+
padding = int(dilation * (kernel_size - 1) / 2)
|
586 |
+
|
587 |
+
self.conv = torch.nn.Conv1d(
|
588 |
+
in_channels,
|
589 |
+
out_channels,
|
590 |
+
kernel_size=kernel_size,
|
591 |
+
stride=stride,
|
592 |
+
padding=padding,
|
593 |
+
dilation=dilation,
|
594 |
+
bias=bias,
|
595 |
+
)
|
596 |
+
|
597 |
+
if spectral_norm:
|
598 |
+
self.conv = nn.utils.spectral_norm(self.conv)
|
599 |
+
|
600 |
+
def forward(self, input):
|
601 |
+
out = self.conv(input)
|
602 |
+
return out
|
603 |
+
|
604 |
+
|
605 |
+
class MultiHeadAttention(nn.Module):
|
606 |
+
"""Multi-Head Attention module"""
|
607 |
+
|
608 |
+
def __init__(self, n_head, d_model, d_k, d_v, dropout=0.0, spectral_norm=False):
|
609 |
+
super().__init__()
|
610 |
+
|
611 |
+
self.n_head = n_head
|
612 |
+
self.d_k = d_k
|
613 |
+
self.d_v = d_v
|
614 |
+
|
615 |
+
self.w_qs = nn.Linear(d_model, n_head * d_k)
|
616 |
+
self.w_ks = nn.Linear(d_model, n_head * d_k)
|
617 |
+
self.w_vs = nn.Linear(d_model, n_head * d_v)
|
618 |
+
|
619 |
+
self.attention = ScaledDotProductAttention(
|
620 |
+
temperature=np.power(d_model, 0.5), dropout=dropout
|
621 |
+
)
|
622 |
+
|
623 |
+
self.fc = nn.Linear(n_head * d_v, d_model)
|
624 |
+
self.dropout = nn.Dropout(dropout)
|
625 |
+
|
626 |
+
if spectral_norm:
|
627 |
+
self.w_qs = nn.utils.spectral_norm(self.w_qs)
|
628 |
+
self.w_ks = nn.utils.spectral_norm(self.w_ks)
|
629 |
+
self.w_vs = nn.utils.spectral_norm(self.w_vs)
|
630 |
+
self.fc = nn.utils.spectral_norm(self.fc)
|
631 |
+
|
632 |
+
def forward(self, x, mask=None):
|
633 |
+
d_k, d_v, n_head = self.d_k, self.d_v, self.n_head
|
634 |
+
sz_b, len_x, _ = x.size()
|
635 |
+
|
636 |
+
residual = x
|
637 |
+
|
638 |
+
q = self.w_qs(x).view(sz_b, len_x, n_head, d_k)
|
639 |
+
k = self.w_ks(x).view(sz_b, len_x, n_head, d_k)
|
640 |
+
v = self.w_vs(x).view(sz_b, len_x, n_head, d_v)
|
641 |
+
q = q.permute(2, 0, 1, 3).contiguous().view(-1, len_x, d_k) # (n*b) x lq x dk
|
642 |
+
k = k.permute(2, 0, 1, 3).contiguous().view(-1, len_x, d_k) # (n*b) x lk x dk
|
643 |
+
v = v.permute(2, 0, 1, 3).contiguous().view(-1, len_x, d_v) # (n*b) x lv x dv
|
644 |
+
|
645 |
+
if mask is not None:
|
646 |
+
slf_mask = mask.repeat(n_head, 1, 1) # (n*b) x .. x ..
|
647 |
+
else:
|
648 |
+
slf_mask = None
|
649 |
+
output, attn = self.attention(q, k, v, mask=slf_mask)
|
650 |
+
|
651 |
+
output = output.view(n_head, sz_b, len_x, d_v)
|
652 |
+
output = (
|
653 |
+
output.permute(1, 2, 0, 3).contiguous().view(sz_b, len_x, -1)
|
654 |
+
) # b x lq x (n*dv)
|
655 |
+
|
656 |
+
output = self.fc(output)
|
657 |
+
|
658 |
+
output = self.dropout(output) + residual
|
659 |
+
return output, attn
|
660 |
+
|
661 |
+
|
662 |
+
class ScaledDotProductAttention(nn.Module):
|
663 |
+
"""Scaled Dot-Product Attention"""
|
664 |
+
|
665 |
+
def __init__(self, temperature, dropout):
|
666 |
+
super().__init__()
|
667 |
+
self.temperature = temperature
|
668 |
+
self.softmax = nn.Softmax(dim=2)
|
669 |
+
self.dropout = nn.Dropout(dropout)
|
670 |
+
|
671 |
+
def forward(self, q, k, v, mask=None):
|
672 |
+
attn = torch.bmm(q, k.transpose(1, 2))
|
673 |
+
attn = attn / self.temperature
|
674 |
+
|
675 |
+
if mask is not None:
|
676 |
+
attn = attn.masked_fill(mask, -np.inf)
|
677 |
+
|
678 |
+
attn = self.softmax(attn)
|
679 |
+
p_attn = self.dropout(attn)
|
680 |
+
|
681 |
+
output = torch.bmm(p_attn, v)
|
682 |
+
return output, attn
|
683 |
+
|
684 |
+
|
685 |
+
class MelStyleEncoder(nn.Module):
|
686 |
+
"""MelStyleEncoder"""
|
687 |
+
|
688 |
+
def __init__(
|
689 |
+
self,
|
690 |
+
n_mel_channels=80,
|
691 |
+
style_hidden=128,
|
692 |
+
style_vector_dim=256,
|
693 |
+
style_kernel_size=5,
|
694 |
+
style_head=2,
|
695 |
+
dropout=0.1,
|
696 |
+
):
|
697 |
+
super(MelStyleEncoder, self).__init__()
|
698 |
+
self.in_dim = n_mel_channels
|
699 |
+
self.hidden_dim = style_hidden
|
700 |
+
self.out_dim = style_vector_dim
|
701 |
+
self.kernel_size = style_kernel_size
|
702 |
+
self.n_head = style_head
|
703 |
+
self.dropout = dropout
|
704 |
+
|
705 |
+
self.spectral = nn.Sequential(
|
706 |
+
LinearNorm(self.in_dim, self.hidden_dim),
|
707 |
+
Mish(),
|
708 |
+
nn.Dropout(self.dropout),
|
709 |
+
LinearNorm(self.hidden_dim, self.hidden_dim),
|
710 |
+
Mish(),
|
711 |
+
nn.Dropout(self.dropout),
|
712 |
+
)
|
713 |
+
|
714 |
+
self.temporal = nn.Sequential(
|
715 |
+
Conv1dGLU(self.hidden_dim, self.hidden_dim, self.kernel_size, self.dropout),
|
716 |
+
Conv1dGLU(self.hidden_dim, self.hidden_dim, self.kernel_size, self.dropout),
|
717 |
+
)
|
718 |
+
|
719 |
+
self.slf_attn = MultiHeadAttention(
|
720 |
+
self.n_head,
|
721 |
+
self.hidden_dim,
|
722 |
+
self.hidden_dim // self.n_head,
|
723 |
+
self.hidden_dim // self.n_head,
|
724 |
+
self.dropout,
|
725 |
+
)
|
726 |
+
|
727 |
+
self.fc = LinearNorm(self.hidden_dim, self.out_dim)
|
728 |
+
|
729 |
+
def temporal_avg_pool(self, x, mask=None):
|
730 |
+
if mask is None:
|
731 |
+
out = torch.mean(x, dim=1)
|
732 |
+
else:
|
733 |
+
len_ = (~mask).sum(dim=1).unsqueeze(1)
|
734 |
+
x = x.masked_fill(mask.unsqueeze(-1), 0)
|
735 |
+
x = x.sum(dim=1)
|
736 |
+
out = torch.div(x, len_)
|
737 |
+
return out
|
738 |
+
|
739 |
+
def forward(self, x, mask=None):
|
740 |
+
x = x.transpose(1, 2)
|
741 |
+
if mask is not None:
|
742 |
+
mask = (mask.int() == 0).squeeze(1)
|
743 |
+
max_len = x.shape[1]
|
744 |
+
slf_attn_mask = (
|
745 |
+
mask.unsqueeze(1).expand(-1, max_len, -1) if mask is not None else None
|
746 |
+
)
|
747 |
+
|
748 |
+
# spectral
|
749 |
+
x = self.spectral(x)
|
750 |
+
# temporal
|
751 |
+
x = x.transpose(1, 2)
|
752 |
+
x = self.temporal(x)
|
753 |
+
x = x.transpose(1, 2)
|
754 |
+
# self-attention
|
755 |
+
if mask is not None:
|
756 |
+
x = x.masked_fill(mask.unsqueeze(-1), 0)
|
757 |
+
x, _ = self.slf_attn(x, mask=slf_attn_mask)
|
758 |
+
# fc
|
759 |
+
x = self.fc(x)
|
760 |
+
# temoral average pooling
|
761 |
+
w = self.temporal_avg_pool(x, mask=mask)
|
762 |
+
|
763 |
+
return w.unsqueeze(-1)
|
764 |
+
|
765 |
+
|
766 |
+
class MelStyleEncoderVAE(nn.Module):
|
767 |
+
def __init__(self, spec_channels, z_latent_dim, emb_dim):
|
768 |
+
super().__init__()
|
769 |
+
self.ref_encoder = MelStyleEncoder(spec_channels, style_vector_dim=emb_dim)
|
770 |
+
self.fc1 = nn.Linear(emb_dim, z_latent_dim)
|
771 |
+
self.fc2 = nn.Linear(emb_dim, z_latent_dim)
|
772 |
+
self.fc3 = nn.Linear(z_latent_dim, emb_dim)
|
773 |
+
self.z_latent_dim = z_latent_dim
|
774 |
+
|
775 |
+
def reparameterize(self, mu, logvar):
|
776 |
+
if self.training:
|
777 |
+
std = torch.exp(0.5 * logvar)
|
778 |
+
eps = torch.randn_like(std)
|
779 |
+
return eps.mul(std).add_(mu)
|
780 |
+
else:
|
781 |
+
return mu
|
782 |
+
|
783 |
+
def forward(self, inputs, mask=None):
|
784 |
+
enc_out = self.ref_encoder(inputs.squeeze(-1), mask).squeeze(-1)
|
785 |
+
mu = self.fc1(enc_out)
|
786 |
+
logvar = self.fc2(enc_out)
|
787 |
+
posterior = D.Normal(mu, torch.exp(logvar))
|
788 |
+
kl_divergence = D.kl_divergence(
|
789 |
+
posterior, D.Normal(torch.zeros_like(mu), torch.ones_like(logvar))
|
790 |
+
)
|
791 |
+
loss_kl = kl_divergence.mean()
|
792 |
+
|
793 |
+
z = posterior.rsample()
|
794 |
+
style_embed = self.fc3(z)
|
795 |
+
|
796 |
+
return style_embed.unsqueeze(-1), loss_kl
|
797 |
+
|
798 |
+
def infer(self, inputs=None, random_sample=False, manual_latent=None):
|
799 |
+
if manual_latent is None:
|
800 |
+
if random_sample:
|
801 |
+
dev = next(self.parameters()).device
|
802 |
+
posterior = D.Normal(
|
803 |
+
torch.zeros(1, self.z_latent_dim, device=dev),
|
804 |
+
torch.ones(1, self.z_latent_dim, device=dev),
|
805 |
+
)
|
806 |
+
z = posterior.rsample()
|
807 |
+
else:
|
808 |
+
enc_out = self.ref_encoder(inputs.transpose(1, 2))
|
809 |
+
mu = self.fc1(enc_out)
|
810 |
+
z = mu
|
811 |
+
else:
|
812 |
+
z = manual_latent
|
813 |
+
style_embed = self.fc3(z)
|
814 |
+
return style_embed.unsqueeze(-1), z
|
815 |
+
|
816 |
+
|
817 |
+
class ActNorm(nn.Module):
|
818 |
+
def __init__(self, channels, ddi=False, **kwargs):
|
819 |
+
super().__init__()
|
820 |
+
self.channels = channels
|
821 |
+
self.initialized = not ddi
|
822 |
+
|
823 |
+
self.logs = nn.Parameter(torch.zeros(1, channels, 1))
|
824 |
+
self.bias = nn.Parameter(torch.zeros(1, channels, 1))
|
825 |
+
|
826 |
+
def forward(self, x, x_mask=None, g=None, reverse=False, **kwargs):
|
827 |
+
if x_mask is None:
|
828 |
+
x_mask = torch.ones(x.size(0), 1, x.size(2)).to(
|
829 |
+
device=x.device, dtype=x.dtype
|
830 |
+
)
|
831 |
+
x_len = torch.sum(x_mask, [1, 2])
|
832 |
+
if not self.initialized:
|
833 |
+
self.initialize(x, x_mask)
|
834 |
+
self.initialized = True
|
835 |
+
|
836 |
+
if reverse:
|
837 |
+
z = (x - self.bias) * torch.exp(-self.logs) * x_mask
|
838 |
+
logdet = None
|
839 |
+
return z
|
840 |
+
else:
|
841 |
+
z = (self.bias + torch.exp(self.logs) * x) * x_mask
|
842 |
+
logdet = torch.sum(self.logs) * x_len # [b]
|
843 |
+
return z, logdet
|
844 |
+
|
845 |
+
def store_inverse(self):
|
846 |
+
pass
|
847 |
+
|
848 |
+
def set_ddi(self, ddi):
|
849 |
+
self.initialized = not ddi
|
850 |
+
|
851 |
+
def initialize(self, x, x_mask):
|
852 |
+
with torch.no_grad():
|
853 |
+
denom = torch.sum(x_mask, [0, 2])
|
854 |
+
m = torch.sum(x * x_mask, [0, 2]) / denom
|
855 |
+
m_sq = torch.sum(x * x * x_mask, [0, 2]) / denom
|
856 |
+
v = m_sq - (m**2)
|
857 |
+
logs = 0.5 * torch.log(torch.clamp_min(v, 1e-6))
|
858 |
+
|
859 |
+
bias_init = (
|
860 |
+
(-m * torch.exp(-logs)).view(*self.bias.shape).to(dtype=self.bias.dtype)
|
861 |
+
)
|
862 |
+
logs_init = (-logs).view(*self.logs.shape).to(dtype=self.logs.dtype)
|
863 |
+
|
864 |
+
self.bias.data.copy_(bias_init)
|
865 |
+
self.logs.data.copy_(logs_init)
|
866 |
+
|
867 |
+
|
868 |
+
class InvConvNear(nn.Module):
|
869 |
+
def __init__(self, channels, n_split=4, no_jacobian=False, **kwargs):
|
870 |
+
super().__init__()
|
871 |
+
assert n_split % 2 == 0
|
872 |
+
self.channels = channels
|
873 |
+
self.n_split = n_split
|
874 |
+
self.no_jacobian = no_jacobian
|
875 |
+
|
876 |
+
w_init = torch.linalg.qr(
|
877 |
+
torch.FloatTensor(self.n_split, self.n_split).normal_()
|
878 |
+
)[0]
|
879 |
+
if torch.det(w_init) < 0:
|
880 |
+
w_init[:, 0] = -1 * w_init[:, 0]
|
881 |
+
self.weight = nn.Parameter(w_init)
|
882 |
+
|
883 |
+
def forward(self, x, x_mask=None, g=None, reverse=False, **kwargs):
|
884 |
+
b, c, t = x.size()
|
885 |
+
assert c % self.n_split == 0
|
886 |
+
if x_mask is None:
|
887 |
+
x_mask = 1
|
888 |
+
x_len = torch.ones((b,), dtype=x.dtype, device=x.device) * t
|
889 |
+
else:
|
890 |
+
x_len = torch.sum(x_mask, [1, 2])
|
891 |
+
|
892 |
+
x = x.view(b, 2, c // self.n_split, self.n_split // 2, t)
|
893 |
+
x = (
|
894 |
+
x.permute(0, 1, 3, 2, 4)
|
895 |
+
.contiguous()
|
896 |
+
.view(b, self.n_split, c // self.n_split, t)
|
897 |
+
)
|
898 |
+
|
899 |
+
if reverse:
|
900 |
+
if hasattr(self, "weight_inv"):
|
901 |
+
weight = self.weight_inv
|
902 |
+
else:
|
903 |
+
weight = torch.inverse(self.weight.float()).to(dtype=self.weight.dtype)
|
904 |
+
logdet = None
|
905 |
+
else:
|
906 |
+
weight = self.weight
|
907 |
+
if self.no_jacobian:
|
908 |
+
logdet = 0
|
909 |
+
else:
|
910 |
+
logdet = torch.logdet(self.weight) * (c / self.n_split) * x_len # [b]
|
911 |
+
|
912 |
+
weight = weight.view(self.n_split, self.n_split, 1, 1)
|
913 |
+
z = F.conv2d(x, weight)
|
914 |
+
|
915 |
+
z = z.view(b, 2, self.n_split // 2, c // self.n_split, t)
|
916 |
+
z = z.permute(0, 1, 3, 2, 4).contiguous().view(b, c, t) * x_mask
|
917 |
+
if reverse:
|
918 |
+
return z
|
919 |
+
else:
|
920 |
+
return z, logdet
|
921 |
+
|
922 |
+
def store_inverse(self):
|
923 |
+
self.weight_inv = torch.inverse(self.weight.float()).to(dtype=self.weight.dtype)
|
mrte_model.py
ADDED
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This is Multi-reference timbre encoder
|
2 |
+
|
3 |
+
import torch
|
4 |
+
from torch import nn
|
5 |
+
from torch.nn.utils import remove_weight_norm, weight_norm
|
6 |
+
from .attentions import MultiHeadAttention
|
7 |
+
|
8 |
+
|
9 |
+
class MRTE(nn.Module):
|
10 |
+
def __init__(
|
11 |
+
self,
|
12 |
+
content_enc_channels=192,
|
13 |
+
hidden_size=512,
|
14 |
+
out_channels=192,
|
15 |
+
kernel_size=5,
|
16 |
+
n_heads=4,
|
17 |
+
ge_layer=2,
|
18 |
+
):
|
19 |
+
super(MRTE, self).__init__()
|
20 |
+
self.cross_attention = MultiHeadAttention(hidden_size, hidden_size, n_heads)
|
21 |
+
self.c_pre = nn.Conv1d(content_enc_channels, hidden_size, 1)
|
22 |
+
self.text_pre = nn.Conv1d(content_enc_channels, hidden_size, 1)
|
23 |
+
self.c_post = nn.Conv1d(hidden_size, out_channels, 1)
|
24 |
+
|
25 |
+
def forward(self, ssl_enc, ssl_mask, text, text_mask, ge, test=None):
|
26 |
+
if ge == None:
|
27 |
+
ge = 0
|
28 |
+
attn_mask = text_mask.unsqueeze(2) * ssl_mask.unsqueeze(-1)
|
29 |
+
|
30 |
+
ssl_enc = self.c_pre(ssl_enc * ssl_mask)
|
31 |
+
text_enc = self.text_pre(text * text_mask)
|
32 |
+
if test != None:
|
33 |
+
if test == 0:
|
34 |
+
x = (
|
35 |
+
self.cross_attention(
|
36 |
+
ssl_enc * ssl_mask, text_enc * text_mask, attn_mask
|
37 |
+
)
|
38 |
+
+ ssl_enc
|
39 |
+
+ ge
|
40 |
+
)
|
41 |
+
elif test == 1:
|
42 |
+
x = ssl_enc + ge
|
43 |
+
elif test == 2:
|
44 |
+
x = (
|
45 |
+
self.cross_attention(
|
46 |
+
ssl_enc * 0 * ssl_mask, text_enc * text_mask, attn_mask
|
47 |
+
)
|
48 |
+
+ ge
|
49 |
+
)
|
50 |
+
else:
|
51 |
+
raise ValueError("test should be 0,1,2")
|
52 |
+
else:
|
53 |
+
x = (
|
54 |
+
self.cross_attention(
|
55 |
+
ssl_enc * ssl_mask, text_enc * text_mask, attn_mask
|
56 |
+
)
|
57 |
+
+ ssl_enc
|
58 |
+
+ ge
|
59 |
+
)
|
60 |
+
x = self.c_post(x * ssl_mask)
|
61 |
+
return x
|
62 |
+
|
63 |
+
|
64 |
+
class SpeakerEncoder(torch.nn.Module):
|
65 |
+
def __init__(
|
66 |
+
self,
|
67 |
+
mel_n_channels=80,
|
68 |
+
model_num_layers=2,
|
69 |
+
model_hidden_size=256,
|
70 |
+
model_embedding_size=256,
|
71 |
+
):
|
72 |
+
super(SpeakerEncoder, self).__init__()
|
73 |
+
self.lstm = nn.LSTM(
|
74 |
+
mel_n_channels, model_hidden_size, model_num_layers, batch_first=True
|
75 |
+
)
|
76 |
+
self.linear = nn.Linear(model_hidden_size, model_embedding_size)
|
77 |
+
self.relu = nn.ReLU()
|
78 |
+
|
79 |
+
def forward(self, mels):
|
80 |
+
self.lstm.flatten_parameters()
|
81 |
+
_, (hidden, _) = self.lstm(mels.transpose(-1, -2))
|
82 |
+
embeds_raw = self.relu(self.linear(hidden[-1]))
|
83 |
+
return embeds_raw / torch.norm(embeds_raw, dim=1, keepdim=True)
|
84 |
+
|
85 |
+
|
86 |
+
class MELEncoder(nn.Module):
|
87 |
+
def __init__(
|
88 |
+
self,
|
89 |
+
in_channels,
|
90 |
+
out_channels,
|
91 |
+
hidden_channels,
|
92 |
+
kernel_size,
|
93 |
+
dilation_rate,
|
94 |
+
n_layers,
|
95 |
+
):
|
96 |
+
super().__init__()
|
97 |
+
self.in_channels = in_channels
|
98 |
+
self.out_channels = out_channels
|
99 |
+
self.hidden_channels = hidden_channels
|
100 |
+
self.kernel_size = kernel_size
|
101 |
+
self.dilation_rate = dilation_rate
|
102 |
+
self.n_layers = n_layers
|
103 |
+
|
104 |
+
self.pre = nn.Conv1d(in_channels, hidden_channels, 1)
|
105 |
+
self.enc = WN(hidden_channels, kernel_size, dilation_rate, n_layers)
|
106 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels, 1)
|
107 |
+
|
108 |
+
def forward(self, x):
|
109 |
+
# print(x.shape,x_lengths.shape)
|
110 |
+
x = self.pre(x)
|
111 |
+
x = self.enc(x)
|
112 |
+
x = self.proj(x)
|
113 |
+
return x
|
114 |
+
|
115 |
+
|
116 |
+
class WN(torch.nn.Module):
|
117 |
+
def __init__(self, hidden_channels, kernel_size, dilation_rate, n_layers):
|
118 |
+
super(WN, self).__init__()
|
119 |
+
assert kernel_size % 2 == 1
|
120 |
+
self.hidden_channels = hidden_channels
|
121 |
+
self.kernel_size = kernel_size
|
122 |
+
self.dilation_rate = dilation_rate
|
123 |
+
self.n_layers = n_layers
|
124 |
+
|
125 |
+
self.in_layers = torch.nn.ModuleList()
|
126 |
+
self.res_skip_layers = torch.nn.ModuleList()
|
127 |
+
|
128 |
+
for i in range(n_layers):
|
129 |
+
dilation = dilation_rate**i
|
130 |
+
padding = int((kernel_size * dilation - dilation) / 2)
|
131 |
+
in_layer = nn.Conv1d(
|
132 |
+
hidden_channels,
|
133 |
+
2 * hidden_channels,
|
134 |
+
kernel_size,
|
135 |
+
dilation=dilation,
|
136 |
+
padding=padding,
|
137 |
+
)
|
138 |
+
in_layer = weight_norm(in_layer)
|
139 |
+
self.in_layers.append(in_layer)
|
140 |
+
|
141 |
+
# last one is not necessary
|
142 |
+
if i < n_layers - 1:
|
143 |
+
res_skip_channels = 2 * hidden_channels
|
144 |
+
else:
|
145 |
+
res_skip_channels = hidden_channels
|
146 |
+
|
147 |
+
res_skip_layer = torch.nn.Conv1d(hidden_channels, res_skip_channels, 1)
|
148 |
+
res_skip_layer = weight_norm(res_skip_layer, name="weight")
|
149 |
+
self.res_skip_layers.append(res_skip_layer)
|
150 |
+
|
151 |
+
def forward(self, x):
|
152 |
+
output = torch.zeros_like(x)
|
153 |
+
n_channels_tensor = torch.IntTensor([self.hidden_channels])
|
154 |
+
|
155 |
+
for i in range(self.n_layers):
|
156 |
+
x_in = self.in_layers[i](x)
|
157 |
+
|
158 |
+
acts = fused_add_tanh_sigmoid_multiply(x_in, n_channels_tensor)
|
159 |
+
|
160 |
+
res_skip_acts = self.res_skip_layers[i](acts)
|
161 |
+
if i < self.n_layers - 1:
|
162 |
+
res_acts = res_skip_acts[:, : self.hidden_channels, :]
|
163 |
+
x = x + res_acts
|
164 |
+
output = output + res_skip_acts[:, self.hidden_channels :, :]
|
165 |
+
else:
|
166 |
+
output = output + res_skip_acts
|
167 |
+
return output
|
168 |
+
|
169 |
+
def remove_weight_norm(self):
|
170 |
+
for l in self.in_layers:
|
171 |
+
remove_weight_norm(l)
|
172 |
+
for l in self.res_skip_layers:
|
173 |
+
remove_weight_norm(l)
|
174 |
+
|
175 |
+
|
176 |
+
@torch.jit.script
|
177 |
+
def fused_add_tanh_sigmoid_multiply(input, n_channels):
|
178 |
+
n_channels_int = n_channels[0]
|
179 |
+
t_act = torch.tanh(input[:, :n_channels_int, :])
|
180 |
+
s_act = torch.sigmoid(input[:, n_channels_int:, :])
|
181 |
+
acts = t_act * s_act
|
182 |
+
return acts
|
183 |
+
|
184 |
+
|
185 |
+
if __name__ == "__main__":
|
186 |
+
content_enc = torch.randn(3, 192, 100)
|
187 |
+
content_mask = torch.ones(3, 1, 100)
|
188 |
+
ref_mel = torch.randn(3, 128, 30)
|
189 |
+
ref_mask = torch.ones(3, 1, 30)
|
190 |
+
model = MRTE()
|
191 |
+
out = model(content_enc, content_mask, ref_mel, ref_mask)
|
192 |
+
print(out.shape)
|
my_utils.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import ffmpeg
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
+
|
5 |
+
def load_audio(file, sr):
|
6 |
+
try:
|
7 |
+
# https://github.com/openai/whisper/blob/main/whisper/audio.py#L26
|
8 |
+
# This launches a subprocess to decode audio while down-mixing and resampling as necessary.
|
9 |
+
# Requires the ffmpeg CLI and `ffmpeg-python` package to be installed.
|
10 |
+
file = (
|
11 |
+
file.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
|
12 |
+
) # 防止小白拷路径头尾带了空格和"和回车
|
13 |
+
out, _ = (
|
14 |
+
ffmpeg.input(file, threads=0)
|
15 |
+
.output("-", format="f32le", acodec="pcm_f32le", ac=1, ar=sr)
|
16 |
+
.run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True)
|
17 |
+
)
|
18 |
+
except Exception as e:
|
19 |
+
raise RuntimeError(f"Failed to load audio: {e}")
|
20 |
+
|
21 |
+
return np.frombuffer(out, np.float32).flatten()
|
optim.py
ADDED
@@ -0,0 +1,622 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2022 Xiaomi Corp. (authors: Daniel Povey)
|
2 |
+
#
|
3 |
+
# See ../LICENSE for clarification regarding multiple authors
|
4 |
+
#
|
5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6 |
+
# you may not use this file except in compliance with the License.
|
7 |
+
# You may obtain a copy of the License at
|
8 |
+
#
|
9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
#
|
11 |
+
# Unless required by applicable law or agreed to in writing, software
|
12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 |
+
# See the License for the specific language governing permissions and
|
15 |
+
# limitations under the License.
|
16 |
+
import contextlib
|
17 |
+
import logging
|
18 |
+
from collections import defaultdict
|
19 |
+
from typing import List
|
20 |
+
from typing import Tuple
|
21 |
+
|
22 |
+
import torch
|
23 |
+
from torch import Tensor
|
24 |
+
from torch.optim import Optimizer
|
25 |
+
|
26 |
+
|
27 |
+
class BatchedOptimizer(Optimizer):
|
28 |
+
"""
|
29 |
+
This class adds to class Optimizer the capability to optimize parameters in batches:
|
30 |
+
it will stack the parameters and their grads for you so the optimizer can work
|
31 |
+
on tensors with an extra leading dimension. This is intended for speed with GPUs,
|
32 |
+
as it reduces the number of kernels launched in the optimizer.
|
33 |
+
|
34 |
+
Args:
|
35 |
+
params:
|
36 |
+
"""
|
37 |
+
|
38 |
+
def __init__(self, params, defaults):
|
39 |
+
super(BatchedOptimizer, self).__init__(params, defaults)
|
40 |
+
|
41 |
+
@contextlib.contextmanager
|
42 |
+
def batched_params(self, param_group, group_params_names):
|
43 |
+
"""
|
44 |
+
This function returns (technically, yields) a list of
|
45 |
+
of tuples (p, state), where
|
46 |
+
p is a `fake` parameter that is stacked (over axis 0) from real parameters
|
47 |
+
that share the same shape, and its gradient is also stacked;
|
48 |
+
`state` is the state corresponding to this batch of parameters
|
49 |
+
(it will be physically located in the "state" for one of the real
|
50 |
+
parameters, the last one that has any particular shape and dtype).
|
51 |
+
|
52 |
+
This function is decorated as a context manager so that it can
|
53 |
+
write parameters back to their "real" locations.
|
54 |
+
|
55 |
+
The idea is, instead of doing:
|
56 |
+
<code>
|
57 |
+
for p in group["params"]:
|
58 |
+
state = self.state[p]
|
59 |
+
...
|
60 |
+
</code>
|
61 |
+
you can do:
|
62 |
+
<code>
|
63 |
+
with self.batched_params(group["params"]) as batches:
|
64 |
+
for p, state, p_names in batches:
|
65 |
+
...
|
66 |
+
</code>
|
67 |
+
|
68 |
+
Args:
|
69 |
+
group: a parameter group, which is a list of parameters; should be
|
70 |
+
one of self.param_groups.
|
71 |
+
group_params_names: name for each parameter in group,
|
72 |
+
which is List[str].
|
73 |
+
"""
|
74 |
+
batches = defaultdict(
|
75 |
+
list
|
76 |
+
) # `batches` maps from tuple (dtype_as_str,*shape) to list of nn.Parameter
|
77 |
+
batches_names = defaultdict(
|
78 |
+
list
|
79 |
+
) # `batches` maps from tuple (dtype_as_str,*shape) to list of str
|
80 |
+
|
81 |
+
assert len(param_group) == len(group_params_names)
|
82 |
+
for p, named_p in zip(param_group, group_params_names):
|
83 |
+
key = (str(p.dtype), *p.shape)
|
84 |
+
batches[key].append(p)
|
85 |
+
batches_names[key].append(named_p)
|
86 |
+
|
87 |
+
batches_names_keys = list(batches_names.keys())
|
88 |
+
sorted_idx = sorted(
|
89 |
+
range(len(batches_names)), key=lambda i: batches_names_keys[i])
|
90 |
+
batches_names = [
|
91 |
+
batches_names[batches_names_keys[idx]] for idx in sorted_idx
|
92 |
+
]
|
93 |
+
batches = [batches[batches_names_keys[idx]] for idx in sorted_idx]
|
94 |
+
|
95 |
+
stacked_params_dict = dict()
|
96 |
+
|
97 |
+
# turn batches into a list, in deterministic order.
|
98 |
+
# tuples will contain tuples of (stacked_param, state, stacked_params_names),
|
99 |
+
# one for each batch in `batches`.
|
100 |
+
tuples = []
|
101 |
+
|
102 |
+
for batch, batch_names in zip(batches, batches_names):
|
103 |
+
p = batch[0]
|
104 |
+
# we arbitrarily store the state in the
|
105 |
+
# state corresponding to the 1st parameter in the
|
106 |
+
# group. class Optimizer will take care of saving/loading state.
|
107 |
+
state = self.state[p]
|
108 |
+
p_stacked = torch.stack(batch)
|
109 |
+
grad = torch.stack([
|
110 |
+
torch.zeros_like(p) if p.grad is None else p.grad for p in batch
|
111 |
+
])
|
112 |
+
p_stacked.grad = grad
|
113 |
+
stacked_params_dict[key] = p_stacked
|
114 |
+
tuples.append((p_stacked, state, batch_names))
|
115 |
+
|
116 |
+
yield tuples # <-- calling code will do the actual optimization here!
|
117 |
+
|
118 |
+
for ((stacked_params, _state, _names), batch) in zip(tuples, batches):
|
119 |
+
for i, p in enumerate(batch): # batch is list of Parameter
|
120 |
+
p.copy_(stacked_params[i])
|
121 |
+
|
122 |
+
|
123 |
+
class ScaledAdam(BatchedOptimizer):
|
124 |
+
"""
|
125 |
+
Implements 'Scaled Adam', a variant of Adam where we scale each parameter's update
|
126 |
+
proportional to the norm of that parameter; and also learn the scale of the parameter,
|
127 |
+
in log space, subject to upper and lower limits (as if we had factored each parameter as
|
128 |
+
param = underlying_param * log_scale.exp())
|
129 |
+
|
130 |
+
|
131 |
+
Args:
|
132 |
+
params: The parameters or param_groups to optimize (like other Optimizer subclasses)
|
133 |
+
lr: The learning rate. We will typically use a learning rate schedule that starts
|
134 |
+
at 0.03 and decreases over time, i.e. much higher than other common
|
135 |
+
optimizers.
|
136 |
+
clipping_scale: (e.g. 2.0)
|
137 |
+
A scale for gradient-clipping: if specified, the normalized gradients
|
138 |
+
over the whole model will be clipped to have 2-norm equal to
|
139 |
+
`clipping_scale` times the median 2-norm over the most recent period
|
140 |
+
of `clipping_update_period` minibatches. By "normalized gradients",
|
141 |
+
we mean after multiplying by the rms parameter value for this tensor
|
142 |
+
[for non-scalars]; this is appropriate because our update is scaled
|
143 |
+
by this quantity.
|
144 |
+
betas: beta1,beta2 are momentum constants for regular momentum, and moving sum-sq grad.
|
145 |
+
Must satisfy 0 < beta <= beta2 < 1.
|
146 |
+
scalar_lr_scale: A scaling factor on the learning rate, that we use to update the
|
147 |
+
scale of each parameter tensor and scalar parameters of the mode..
|
148 |
+
If each parameter were decomposed
|
149 |
+
as p * p_scale.exp(), where (p**2).mean().sqrt() == 1.0, scalar_lr_scale
|
150 |
+
would be a the scaling factor on the learning rate of p_scale.
|
151 |
+
eps: A general-purpose epsilon to prevent division by zero
|
152 |
+
param_min_rms: Minimum root-mean-square value of parameter tensor, for purposes of
|
153 |
+
learning the scale on the parameters (we'll constrain the rms of each non-scalar
|
154 |
+
parameter tensor to be >= this value)
|
155 |
+
param_max_rms: Maximum root-mean-square value of parameter tensor, for purposes of
|
156 |
+
learning the scale on the parameters (we'll constrain the rms of each non-scalar
|
157 |
+
parameter tensor to be <= this value)
|
158 |
+
scalar_max: Maximum absolute value for scalar parameters (applicable if your
|
159 |
+
model has any parameters with numel() == 1).
|
160 |
+
size_update_period: The periodicity, in steps, with which we update the size (scale)
|
161 |
+
of the parameter tensor. This is provided to save a little time
|
162 |
+
in the update.
|
163 |
+
clipping_update_period: if clipping_scale is specified, this is the period
|
164 |
+
"""
|
165 |
+
|
166 |
+
def __init__(
|
167 |
+
self,
|
168 |
+
params,
|
169 |
+
lr=3e-02,
|
170 |
+
clipping_scale=None,
|
171 |
+
betas=(0.9, 0.98),
|
172 |
+
scalar_lr_scale=0.1,
|
173 |
+
eps=1.0e-08,
|
174 |
+
param_min_rms=1.0e-05,
|
175 |
+
param_max_rms=3.0,
|
176 |
+
scalar_max=10.0,
|
177 |
+
size_update_period=4,
|
178 |
+
clipping_update_period=100,
|
179 |
+
parameters_names=None,
|
180 |
+
show_dominant_parameters=True, ):
|
181 |
+
|
182 |
+
assert parameters_names is not None, (
|
183 |
+
"Please prepare parameters_names,"
|
184 |
+
"which is a List[List[str]]. Each List[str] is for a group"
|
185 |
+
"and each str is for a parameter")
|
186 |
+
defaults = dict(
|
187 |
+
lr=lr,
|
188 |
+
clipping_scale=clipping_scale,
|
189 |
+
betas=betas,
|
190 |
+
scalar_lr_scale=scalar_lr_scale,
|
191 |
+
eps=eps,
|
192 |
+
param_min_rms=param_min_rms,
|
193 |
+
param_max_rms=param_max_rms,
|
194 |
+
scalar_max=scalar_max,
|
195 |
+
size_update_period=size_update_period,
|
196 |
+
clipping_update_period=clipping_update_period, )
|
197 |
+
|
198 |
+
super(ScaledAdam, self).__init__(params, defaults)
|
199 |
+
assert len(self.param_groups) == len(parameters_names)
|
200 |
+
self.parameters_names = parameters_names
|
201 |
+
self.show_dominant_parameters = show_dominant_parameters
|
202 |
+
|
203 |
+
def __setstate__(self, state):
|
204 |
+
super(ScaledAdam, self).__setstate__(state)
|
205 |
+
|
206 |
+
@torch.no_grad()
|
207 |
+
def step(self, closure=None):
|
208 |
+
"""Performs a single optimization step.
|
209 |
+
|
210 |
+
Arguments:
|
211 |
+
closure (callable, optional): A closure that reevaluates the model
|
212 |
+
and returns the loss.
|
213 |
+
"""
|
214 |
+
loss = None
|
215 |
+
if closure is not None:
|
216 |
+
with torch.enable_grad():
|
217 |
+
loss = closure()
|
218 |
+
|
219 |
+
batch = True
|
220 |
+
|
221 |
+
for group, group_params_names in zip(self.param_groups,
|
222 |
+
self.parameters_names):
|
223 |
+
|
224 |
+
with self.batched_params(group["params"],
|
225 |
+
group_params_names) as batches:
|
226 |
+
|
227 |
+
# batches is list of pairs (stacked_param, state). stacked_param is like
|
228 |
+
# a regular parameter, and will have a .grad, but the 1st dim corresponds to
|
229 |
+
# a stacking dim, it is not a real dim.
|
230 |
+
|
231 |
+
if (len(batches[0][1]) ==
|
232 |
+
0): # if len(first state) == 0: not yet initialized
|
233 |
+
clipping_scale = 1
|
234 |
+
else:
|
235 |
+
clipping_scale = self._get_clipping_scale(group, batches)
|
236 |
+
|
237 |
+
for p, state, _ in batches:
|
238 |
+
# Perform optimization step.
|
239 |
+
# grad is not going to be None, we handled that when creating the batches.
|
240 |
+
grad = p.grad
|
241 |
+
if grad.is_sparse:
|
242 |
+
raise RuntimeError(
|
243 |
+
"ScaledAdam optimizer does not support sparse gradients"
|
244 |
+
)
|
245 |
+
# State initialization
|
246 |
+
if len(state) == 0:
|
247 |
+
self._init_state(group, p, state)
|
248 |
+
|
249 |
+
self._step_one_batch(group, p, state, clipping_scale)
|
250 |
+
|
251 |
+
return loss
|
252 |
+
|
253 |
+
def _init_state(self, group: dict, p: Tensor, state: dict):
|
254 |
+
"""
|
255 |
+
Initializes state dict for parameter 'p'. Assumes that dim 0 of tensor p
|
256 |
+
is actually the batch dimension, corresponding to batched-together
|
257 |
+
parameters of a given shape.
|
258 |
+
|
259 |
+
|
260 |
+
Args:
|
261 |
+
group: Dict to look up configuration values.
|
262 |
+
p: The parameter that we are initializing the state for
|
263 |
+
state: Dict from string to whatever state we are initializing
|
264 |
+
"""
|
265 |
+
size_update_period = group["size_update_period"]
|
266 |
+
|
267 |
+
state["step"] = 0
|
268 |
+
|
269 |
+
kwargs = {"device": p.device, "dtype": p.dtype}
|
270 |
+
|
271 |
+
# 'delta' implements conventional momentum. There are
|
272 |
+
# several different kinds of update going on, so rather than
|
273 |
+
# compute "exp_avg" like in Adam, we store and decay a
|
274 |
+
# parameter-change "delta", which combines all forms of
|
275 |
+
# update. this is equivalent to how it's done in Adam,
|
276 |
+
# except for the first few steps.
|
277 |
+
state["delta"] = torch.zeros_like(
|
278 |
+
p, memory_format=torch.preserve_format)
|
279 |
+
|
280 |
+
batch_size = p.shape[0]
|
281 |
+
numel = p.numel() // batch_size
|
282 |
+
numel = p.numel()
|
283 |
+
|
284 |
+
if numel > 1:
|
285 |
+
# "param_rms" just periodically records the scalar root-mean-square value of
|
286 |
+
# the parameter tensor.
|
287 |
+
# it has a shape like (batch_size, 1, 1, 1, 1)
|
288 |
+
param_rms = (
|
289 |
+
(p**2).mean(dim=list(range(1, p.ndim)), keepdim=True).sqrt())
|
290 |
+
state["param_rms"] = param_rms
|
291 |
+
|
292 |
+
state["scale_exp_avg_sq"] = torch.zeros_like(param_rms)
|
293 |
+
state["scale_grads"] = torch.zeros(size_update_period,
|
294 |
+
*param_rms.shape, **kwargs)
|
295 |
+
|
296 |
+
# exp_avg_sq is the weighted sum of scaled gradients. as in Adam.
|
297 |
+
state["exp_avg_sq"] = torch.zeros_like(
|
298 |
+
p, memory_format=torch.preserve_format)
|
299 |
+
|
300 |
+
def _get_clipping_scale(self,
|
301 |
+
group: dict,
|
302 |
+
tuples: List[Tuple[Tensor, dict, List[str]]]
|
303 |
+
) -> float:
|
304 |
+
"""
|
305 |
+
Returns a scalar factor <= 1.0 that dictates gradient clipping, i.e. we will scale the gradients
|
306 |
+
by this amount before applying the rest of the update.
|
307 |
+
|
308 |
+
Args:
|
309 |
+
group: the parameter group, an item in self.param_groups
|
310 |
+
tuples: a list of tuples of (param, state, param_names)
|
311 |
+
where param is a batched set of parameters,
|
312 |
+
with a .grad (1st dim is batch dim)
|
313 |
+
and state is the state-dict where optimization parameters are kept.
|
314 |
+
param_names is a List[str] while each str is name for a parameter
|
315 |
+
in batched set of parameters "param".
|
316 |
+
"""
|
317 |
+
assert len(tuples) >= 1
|
318 |
+
clipping_scale = group["clipping_scale"]
|
319 |
+
(first_p, first_state, _) = tuples[0]
|
320 |
+
step = first_state["step"]
|
321 |
+
if clipping_scale is None or step == 0:
|
322 |
+
# no clipping. return early on step == 0 because the other
|
323 |
+
# parameters' state won't have been initialized yet.
|
324 |
+
return 1.0
|
325 |
+
clipping_update_period = group["clipping_update_period"]
|
326 |
+
|
327 |
+
tot_sumsq = torch.tensor(0.0, device=first_p.device)
|
328 |
+
for (p, state, param_names) in tuples:
|
329 |
+
grad = p.grad
|
330 |
+
if grad.is_sparse:
|
331 |
+
raise RuntimeError(
|
332 |
+
"ScaledAdam optimizer does not support sparse gradients")
|
333 |
+
if p.numel() == p.shape[0]: # a batch of scalars
|
334 |
+
tot_sumsq += (grad**2).sum() # sum() to change shape [1] to []
|
335 |
+
else:
|
336 |
+
tot_sumsq += ((grad * state["param_rms"])**2).sum()
|
337 |
+
|
338 |
+
tot_norm = tot_sumsq.sqrt()
|
339 |
+
if "model_norms" not in first_state:
|
340 |
+
first_state["model_norms"] = torch.zeros(
|
341 |
+
clipping_update_period, device=p.device)
|
342 |
+
first_state["model_norms"][step % clipping_update_period] = tot_norm
|
343 |
+
|
344 |
+
if step % clipping_update_period == 0:
|
345 |
+
# Print some stats.
|
346 |
+
# We don't reach here if step == 0 because we would have returned
|
347 |
+
# above.
|
348 |
+
sorted_norms = first_state["model_norms"].sort()[0].to("cpu")
|
349 |
+
quartiles = []
|
350 |
+
for n in range(0, 5):
|
351 |
+
index = min(
|
352 |
+
clipping_update_period - 1,
|
353 |
+
(clipping_update_period // 4) * n, )
|
354 |
+
quartiles.append(sorted_norms[index].item())
|
355 |
+
|
356 |
+
median = quartiles[2]
|
357 |
+
threshold = clipping_scale * median
|
358 |
+
first_state["model_norm_threshold"] = threshold
|
359 |
+
percent_clipped = (first_state["num_clipped"] * 100.0 /
|
360 |
+
clipping_update_period
|
361 |
+
if "num_clipped" in first_state else 0.0)
|
362 |
+
first_state["num_clipped"] = 0
|
363 |
+
quartiles = " ".join(["%.3e" % x for x in quartiles])
|
364 |
+
logging.info(
|
365 |
+
f"Clipping_scale={clipping_scale}, grad-norm quartiles {quartiles}, "
|
366 |
+
f"threshold={threshold:.3e}, percent-clipped={percent_clipped:.1f}"
|
367 |
+
)
|
368 |
+
|
369 |
+
if step < clipping_update_period:
|
370 |
+
return 1.0 # We have not yet estimated a norm to clip to.
|
371 |
+
else:
|
372 |
+
try:
|
373 |
+
model_norm_threshold = first_state["model_norm_threshold"]
|
374 |
+
except KeyError:
|
375 |
+
logging.info(
|
376 |
+
"Warning: model_norm_threshold not in state: possibly "
|
377 |
+
"you changed config when restarting, adding clipping_scale option?"
|
378 |
+
)
|
379 |
+
return 1.0
|
380 |
+
ans = min(1.0, (model_norm_threshold / (tot_norm + 1.0e-20)).item())
|
381 |
+
if ans < 1.0:
|
382 |
+
first_state["num_clipped"] += 1
|
383 |
+
if ans < 0.1:
|
384 |
+
logging.warn(
|
385 |
+
f"Scaling gradients by {ans}, model_norm_threshold={model_norm_threshold}"
|
386 |
+
)
|
387 |
+
if self.show_dominant_parameters:
|
388 |
+
assert p.shape[0] == len(param_names)
|
389 |
+
self._show_gradient_dominating_parameter(tuples, tot_sumsq)
|
390 |
+
return ans
|
391 |
+
|
392 |
+
def _show_gradient_dominating_parameter(
|
393 |
+
self, tuples: List[Tuple[Tensor, dict, List[str]]],
|
394 |
+
tot_sumsq: Tensor):
|
395 |
+
"""
|
396 |
+
Show information of parameter wihch dominanting tot_sumsq.
|
397 |
+
|
398 |
+
Args:
|
399 |
+
tuples: a list of tuples of (param, state, param_names)
|
400 |
+
where param is a batched set of parameters,
|
401 |
+
with a .grad (1st dim is batch dim)
|
402 |
+
and state is the state-dict where optimization parameters are kept.
|
403 |
+
param_names is a List[str] while each str is name for a parameter
|
404 |
+
in batched set of parameters "param".
|
405 |
+
tot_sumsq: sumsq of all parameters. Though it's could be calculated
|
406 |
+
from tuples, we still pass it to save some time.
|
407 |
+
"""
|
408 |
+
all_sumsq_orig = {}
|
409 |
+
for (p, state, batch_param_names) in tuples:
|
410 |
+
# p is a stacked batch parameters.
|
411 |
+
batch_grad = p.grad
|
412 |
+
if p.numel() == p.shape[0]: # a batch of scalars
|
413 |
+
batch_sumsq_orig = batch_grad**2
|
414 |
+
# Dummpy values used by following `zip` statement.
|
415 |
+
batch_rms_orig = torch.ones(p.shape[0])
|
416 |
+
else:
|
417 |
+
batch_rms_orig = state["param_rms"]
|
418 |
+
batch_sumsq_orig = ((batch_grad * batch_rms_orig)**2).sum(
|
419 |
+
dim=list(range(1, batch_grad.ndim)))
|
420 |
+
|
421 |
+
for name, sumsq_orig, rms, grad in zip(batch_param_names,
|
422 |
+
batch_sumsq_orig,
|
423 |
+
batch_rms_orig, batch_grad):
|
424 |
+
|
425 |
+
proportion_orig = sumsq_orig / tot_sumsq
|
426 |
+
all_sumsq_orig[name] = (proportion_orig, sumsq_orig, rms, grad)
|
427 |
+
|
428 |
+
assert torch.isclose(
|
429 |
+
sum([value[0] for value in all_sumsq_orig.values()]).cpu(),
|
430 |
+
torch.tensor(1.0), )
|
431 |
+
sorted_by_proportion = {
|
432 |
+
k: v
|
433 |
+
for k, v in sorted(
|
434 |
+
all_sumsq_orig.items(),
|
435 |
+
key=lambda item: item[1][0],
|
436 |
+
reverse=True, )
|
437 |
+
}
|
438 |
+
dominant_param_name = next(iter(sorted_by_proportion))
|
439 |
+
(dominant_proportion, dominant_sumsq, dominant_rms,
|
440 |
+
dominant_grad, ) = sorted_by_proportion[dominant_param_name]
|
441 |
+
logging.info(f"Parameter Dominanting tot_sumsq {dominant_param_name}"
|
442 |
+
f" with proportion {dominant_proportion:.2f},"
|
443 |
+
f" where dominant_sumsq=(grad_sumsq*orig_rms_sq)"
|
444 |
+
f"={dominant_sumsq:.3e},"
|
445 |
+
f" grad_sumsq = {(dominant_grad**2).sum():.3e},"
|
446 |
+
f" orig_rms_sq={(dominant_rms**2).item():.3e}")
|
447 |
+
|
448 |
+
def _step_one_batch(self,
|
449 |
+
group: dict,
|
450 |
+
p: Tensor,
|
451 |
+
state: dict,
|
452 |
+
clipping_scale: float):
|
453 |
+
"""
|
454 |
+
Do the step for one parameter, which is actually going to be a batch of
|
455 |
+
`real` parameters, with dim 0 as the batch dim.
|
456 |
+
Args:
|
457 |
+
group: dict to look up configuration values
|
458 |
+
p: parameter to update (actually multiple parameters stacked together
|
459 |
+
as a batch)
|
460 |
+
state: state-dict for p, to look up the optimizer state
|
461 |
+
"""
|
462 |
+
lr = group["lr"]
|
463 |
+
size_update_period = group["size_update_period"]
|
464 |
+
beta1 = group["betas"][0]
|
465 |
+
|
466 |
+
grad = p.grad
|
467 |
+
if clipping_scale != 1.0:
|
468 |
+
grad = grad * clipping_scale
|
469 |
+
step = state["step"]
|
470 |
+
delta = state["delta"]
|
471 |
+
|
472 |
+
delta.mul_(beta1)
|
473 |
+
batch_size = p.shape[0]
|
474 |
+
numel = p.numel() // batch_size
|
475 |
+
if numel > 1:
|
476 |
+
# Update the size/scale of p, and set param_rms
|
477 |
+
scale_grads = state["scale_grads"]
|
478 |
+
scale_grads[step % size_update_period] = (p * grad).sum(
|
479 |
+
dim=list(range(1, p.ndim)), keepdim=True)
|
480 |
+
if step % size_update_period == size_update_period - 1:
|
481 |
+
param_rms = state["param_rms"] # shape: (batch_size, 1, 1, ..)
|
482 |
+
param_rms.copy_((p**2)
|
483 |
+
.mean(dim=list(range(1, p.ndim)), keepdim=True)
|
484 |
+
.sqrt())
|
485 |
+
if step > 0:
|
486 |
+
# self._size_update() learns the overall scale on the
|
487 |
+
# parameter, by shrinking or expanding it.
|
488 |
+
self._size_update(group, scale_grads, p, state)
|
489 |
+
|
490 |
+
if numel == 1:
|
491 |
+
# For parameters with 1 element we just use regular Adam.
|
492 |
+
# Updates delta.
|
493 |
+
self._step_scalar(group, p, state)
|
494 |
+
else:
|
495 |
+
self._step(group, p, state)
|
496 |
+
|
497 |
+
state["step"] = step + 1
|
498 |
+
|
499 |
+
def _size_update(self,
|
500 |
+
group: dict,
|
501 |
+
scale_grads: Tensor,
|
502 |
+
p: Tensor,
|
503 |
+
state: dict) -> None:
|
504 |
+
"""
|
505 |
+
Called only where p.numel() > 1, this updates the scale of the parameter.
|
506 |
+
If we imagine: p = underlying_param * scale.exp(), and we are doing
|
507 |
+
gradient descent on underlying param and on scale, this function does the update
|
508 |
+
on `scale`.
|
509 |
+
|
510 |
+
Args:
|
511 |
+
group: dict to look up configuration values
|
512 |
+
scale_grads: a tensor of shape (size_update_period, batch_size, 1, 1,...) containing
|
513 |
+
grads w.r.t. the scales.
|
514 |
+
p: The parameter to update
|
515 |
+
state: The state-dict of p
|
516 |
+
"""
|
517 |
+
|
518 |
+
param_rms = state["param_rms"]
|
519 |
+
beta1, beta2 = group["betas"]
|
520 |
+
size_lr = group["lr"] * group["scalar_lr_scale"]
|
521 |
+
param_min_rms = group["param_min_rms"]
|
522 |
+
param_max_rms = group["param_max_rms"]
|
523 |
+
eps = group["eps"]
|
524 |
+
step = state["step"]
|
525 |
+
batch_size = p.shape[0]
|
526 |
+
|
527 |
+
size_update_period = scale_grads.shape[0]
|
528 |
+
# correct beta2 for the size update period: we will have
|
529 |
+
# faster decay at this level.
|
530 |
+
beta2_corr = beta2**size_update_period
|
531 |
+
|
532 |
+
scale_exp_avg_sq = state[
|
533 |
+
"scale_exp_avg_sq"] # shape: (batch_size, 1, 1, ..)
|
534 |
+
scale_exp_avg_sq.mul_(beta2_corr).add_(
|
535 |
+
(scale_grads**2).mean(dim=0), # mean over dim `size_update_period`
|
536 |
+
alpha=1 - beta2_corr, ) # shape is (batch_size, 1, 1, ...)
|
537 |
+
|
538 |
+
# The 1st time we reach here is when size_step == 1.
|
539 |
+
size_step = (step + 1) // size_update_period
|
540 |
+
bias_correction2 = 1 - beta2_corr**size_step
|
541 |
+
# we don't bother with bias_correction1; this will help prevent divergence
|
542 |
+
# at the start of training.
|
543 |
+
|
544 |
+
denom = scale_exp_avg_sq.sqrt() + eps
|
545 |
+
|
546 |
+
scale_step = (-size_lr * (bias_correction2**0.5) *
|
547 |
+
scale_grads.sum(dim=0) / denom)
|
548 |
+
|
549 |
+
is_too_small = param_rms < param_min_rms
|
550 |
+
is_too_large = param_rms > param_max_rms
|
551 |
+
|
552 |
+
# when the param gets too small, just don't shrink it any further.
|
553 |
+
scale_step.masked_fill_(is_too_small, 0.0)
|
554 |
+
# when it gets too large, stop it from getting any larger.
|
555 |
+
scale_step.masked_fill_(is_too_large, -size_lr * size_update_period)
|
556 |
+
delta = state["delta"]
|
557 |
+
# the factor of (1-beta1) relates to momentum.
|
558 |
+
delta.add_(p * scale_step, alpha=(1 - beta1))
|
559 |
+
|
560 |
+
def _step(self, group: dict, p: Tensor, state: dict):
|
561 |
+
"""
|
562 |
+
This function does the core update of self.step(), in the case where the members of
|
563 |
+
the batch have more than 1 element.
|
564 |
+
|
565 |
+
Args:
|
566 |
+
group: A dict which will be used to look up configuration values
|
567 |
+
p: The parameter to be updated
|
568 |
+
grad: The grad of p
|
569 |
+
state: The state-dict corresponding to parameter p
|
570 |
+
|
571 |
+
This function modifies p.
|
572 |
+
"""
|
573 |
+
grad = p.grad
|
574 |
+
lr = group["lr"]
|
575 |
+
beta1, beta2 = group["betas"]
|
576 |
+
eps = group["eps"]
|
577 |
+
param_min_rms = group["param_min_rms"]
|
578 |
+
step = state["step"]
|
579 |
+
|
580 |
+
exp_avg_sq = state["exp_avg_sq"]
|
581 |
+
exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=(1 - beta2))
|
582 |
+
|
583 |
+
this_step = state["step"] - (state["zero_step"]
|
584 |
+
if "zero_step" in state else 0)
|
585 |
+
bias_correction2 = 1 - beta2**(this_step + 1)
|
586 |
+
if bias_correction2 < 0.99:
|
587 |
+
# note: not in-place.
|
588 |
+
exp_avg_sq = exp_avg_sq * (1.0 / bias_correction2)
|
589 |
+
|
590 |
+
denom = exp_avg_sq.sqrt()
|
591 |
+
denom += eps
|
592 |
+
grad = grad / denom
|
593 |
+
|
594 |
+
alpha = -lr * (1 - beta1) * state["param_rms"].clamp(min=param_min_rms)
|
595 |
+
|
596 |
+
delta = state["delta"]
|
597 |
+
delta.add_(grad * alpha)
|
598 |
+
p.add_(delta)
|
599 |
+
|
600 |
+
def _step_scalar(self, group: dict, p: Tensor, state: dict):
|
601 |
+
"""
|
602 |
+
A simplified form of the core update for scalar tensors, where we cannot get a good
|
603 |
+
estimate of the parameter rms.
|
604 |
+
"""
|
605 |
+
beta1, beta2 = group["betas"]
|
606 |
+
scalar_max = group["scalar_max"]
|
607 |
+
eps = group["eps"]
|
608 |
+
lr = group["lr"] * group["scalar_lr_scale"]
|
609 |
+
grad = p.grad
|
610 |
+
|
611 |
+
exp_avg_sq = state["exp_avg_sq"] # shape: (batch_size,)
|
612 |
+
exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1 - beta2)
|
613 |
+
|
614 |
+
# bias_correction2 is like in Adam. Don't bother with bias_correction1;
|
615 |
+
# slower update at the start will help stability anyway.
|
616 |
+
bias_correction2 = 1 - beta2**(state["step"] + 1)
|
617 |
+
denom = (exp_avg_sq / bias_correction2).sqrt() + eps
|
618 |
+
|
619 |
+
delta = state["delta"]
|
620 |
+
delta.add_(grad / denom, alpha=-lr * (1 - beta1))
|
621 |
+
p.clamp_(min=-scalar_max, max=scalar_max)
|
622 |
+
p.add_(delta)
|
patched_mha_with_cache.py
ADDED
@@ -0,0 +1,465 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
from torch.nn.functional import *
|
3 |
+
from torch.nn.functional import (
|
4 |
+
_mha_shape_check,
|
5 |
+
_canonical_mask,
|
6 |
+
_none_or_dtype,
|
7 |
+
_in_projection_packed,
|
8 |
+
)
|
9 |
+
import torch
|
10 |
+
Tensor = torch.Tensor
|
11 |
+
from typing import Optional, Tuple
|
12 |
+
|
13 |
+
|
14 |
+
def multi_head_attention_forward_patched(
|
15 |
+
query: Tensor,
|
16 |
+
key: Tensor,
|
17 |
+
value: Tensor,
|
18 |
+
embed_dim_to_check: int,
|
19 |
+
num_heads: int,
|
20 |
+
in_proj_weight: Optional[Tensor],
|
21 |
+
in_proj_bias: Optional[Tensor],
|
22 |
+
bias_k: Optional[Tensor],
|
23 |
+
bias_v: Optional[Tensor],
|
24 |
+
add_zero_attn: bool,
|
25 |
+
dropout_p: float,
|
26 |
+
out_proj_weight: Tensor,
|
27 |
+
out_proj_bias: Optional[Tensor],
|
28 |
+
training: bool = True,
|
29 |
+
key_padding_mask: Optional[Tensor] = None,
|
30 |
+
need_weights: bool = True,
|
31 |
+
attn_mask: Optional[Tensor] = None,
|
32 |
+
use_separate_proj_weight: bool = False,
|
33 |
+
q_proj_weight: Optional[Tensor] = None,
|
34 |
+
k_proj_weight: Optional[Tensor] = None,
|
35 |
+
v_proj_weight: Optional[Tensor] = None,
|
36 |
+
static_k: Optional[Tensor] = None,
|
37 |
+
static_v: Optional[Tensor] = None,
|
38 |
+
average_attn_weights: bool = True,
|
39 |
+
is_causal: bool = False,
|
40 |
+
cache=None,
|
41 |
+
) -> Tuple[Tensor, Optional[Tensor]]:
|
42 |
+
r"""
|
43 |
+
Args:
|
44 |
+
query, key, value: map a query and a set of key-value pairs to an output.
|
45 |
+
See "Attention Is All You Need" for more details.
|
46 |
+
embed_dim_to_check: total dimension of the model.
|
47 |
+
num_heads: parallel attention heads.
|
48 |
+
in_proj_weight, in_proj_bias: input projection weight and bias.
|
49 |
+
bias_k, bias_v: bias of the key and value sequences to be added at dim=0.
|
50 |
+
add_zero_attn: add a new batch of zeros to the key and
|
51 |
+
value sequences at dim=1.
|
52 |
+
dropout_p: probability of an element to be zeroed.
|
53 |
+
out_proj_weight, out_proj_bias: the output projection weight and bias.
|
54 |
+
training: apply dropout if is ``True``.
|
55 |
+
key_padding_mask: if provided, specified padding elements in the key will
|
56 |
+
be ignored by the attention. This is an binary mask. When the value is True,
|
57 |
+
the corresponding value on the attention layer will be filled with -inf.
|
58 |
+
need_weights: output attn_output_weights.
|
59 |
+
Default: `True`
|
60 |
+
Note: `needs_weight` defaults to `True`, but should be set to `False`
|
61 |
+
For best performance when attention weights are not nedeeded.
|
62 |
+
*Setting needs_weights to `True`
|
63 |
+
leads to a significant performance degradation.*
|
64 |
+
attn_mask: 2D or 3D mask that prevents attention to certain positions. A 2D mask will be broadcasted for all
|
65 |
+
the batches while a 3D mask allows to specify a different mask for the entries of each batch.
|
66 |
+
is_causal: If specified, applies a causal mask as attention mask, and ignores
|
67 |
+
attn_mask for computing scaled dot product attention.
|
68 |
+
Default: ``False``.
|
69 |
+
.. warning::
|
70 |
+
is_causal is provides a hint that the attn_mask is the
|
71 |
+
causal mask.Providing incorrect hints can result in
|
72 |
+
incorrect execution, including forward and backward
|
73 |
+
compatibility.
|
74 |
+
use_separate_proj_weight: the function accept the proj. weights for query, key,
|
75 |
+
and value in different forms. If false, in_proj_weight will be used, which is
|
76 |
+
a combination of q_proj_weight, k_proj_weight, v_proj_weight.
|
77 |
+
q_proj_weight, k_proj_weight, v_proj_weight, in_proj_bias: input projection weight and bias.
|
78 |
+
static_k, static_v: static key and value used for attention operators.
|
79 |
+
average_attn_weights: If true, indicates that the returned ``attn_weights`` should be averaged across heads.
|
80 |
+
Otherwise, ``attn_weights`` are provided separately per head. Note that this flag only has an effect
|
81 |
+
when ``need_weights=True.``. Default: True
|
82 |
+
|
83 |
+
|
84 |
+
Shape:
|
85 |
+
Inputs:
|
86 |
+
- query: :math:`(L, E)` or :math:`(L, N, E)` where L is the target sequence length, N is the batch size, E is
|
87 |
+
the embedding dimension.
|
88 |
+
- key: :math:`(S, E)` or :math:`(S, N, E)`, where S is the source sequence length, N is the batch size, E is
|
89 |
+
the embedding dimension.
|
90 |
+
- value: :math:`(S, E)` or :math:`(S, N, E)` where S is the source sequence length, N is the batch size, E is
|
91 |
+
the embedding dimension.
|
92 |
+
- key_padding_mask: :math:`(S)` or :math:`(N, S)` where N is the batch size, S is the source sequence length.
|
93 |
+
If a FloatTensor is provided, it will be directly added to the value.
|
94 |
+
If a BoolTensor is provided, the positions with the
|
95 |
+
value of ``True`` will be ignored while the position with the value of ``False`` will be unchanged.
|
96 |
+
- attn_mask: 2D mask :math:`(L, S)` where L is the target sequence length, S is the source sequence length.
|
97 |
+
3D mask :math:`(N*num_heads, L, S)` where N is the batch size, L is the target sequence length,
|
98 |
+
S is the source sequence length. attn_mask ensures that position i is allowed to attend the unmasked
|
99 |
+
positions. If a BoolTensor is provided, positions with ``True``
|
100 |
+
are not allowed to attend while ``False`` values will be unchanged. If a FloatTensor
|
101 |
+
is provided, it will be added to the attention weight.
|
102 |
+
- static_k: :math:`(N*num_heads, S, E/num_heads)`, where S is the source sequence length,
|
103 |
+
N is the batch size, E is the embedding dimension. E/num_heads is the head dimension.
|
104 |
+
- static_v: :math:`(N*num_heads, S, E/num_heads)`, where S is the source sequence length,
|
105 |
+
N is the batch size, E is the embedding dimension. E/num_heads is the head dimension.
|
106 |
+
|
107 |
+
Outputs:
|
108 |
+
- attn_output: :math:`(L, E)` or :math:`(L, N, E)` where L is the target sequence length, N is the batch size,
|
109 |
+
E is the embedding dimension.
|
110 |
+
- attn_output_weights: Only returned when ``need_weights=True``. If ``average_attn_weights=True``, returns
|
111 |
+
attention weights averaged across heads of shape :math:`(L, S)` when input is unbatched or
|
112 |
+
:math:`(N, L, S)`, where :math:`N` is the batch size, :math:`L` is the target sequence length, and
|
113 |
+
:math:`S` is the source sequence length. If ``average_attn_weights=False``, returns attention weights per
|
114 |
+
head of shape :math:`(num_heads, L, S)` when input is unbatched or :math:`(N, num_heads, L, S)`.
|
115 |
+
"""
|
116 |
+
tens_ops = (
|
117 |
+
query,
|
118 |
+
key,
|
119 |
+
value,
|
120 |
+
in_proj_weight,
|
121 |
+
in_proj_bias,
|
122 |
+
bias_k,
|
123 |
+
bias_v,
|
124 |
+
out_proj_weight,
|
125 |
+
out_proj_bias,
|
126 |
+
)
|
127 |
+
if has_torch_function(tens_ops):
|
128 |
+
return handle_torch_function(
|
129 |
+
multi_head_attention_forward,
|
130 |
+
tens_ops,
|
131 |
+
query,
|
132 |
+
key,
|
133 |
+
value,
|
134 |
+
embed_dim_to_check,
|
135 |
+
num_heads,
|
136 |
+
in_proj_weight,
|
137 |
+
in_proj_bias,
|
138 |
+
bias_k,
|
139 |
+
bias_v,
|
140 |
+
add_zero_attn,
|
141 |
+
dropout_p,
|
142 |
+
out_proj_weight,
|
143 |
+
out_proj_bias,
|
144 |
+
training=training,
|
145 |
+
key_padding_mask=key_padding_mask,
|
146 |
+
need_weights=need_weights,
|
147 |
+
attn_mask=attn_mask,
|
148 |
+
is_causal=is_causal,
|
149 |
+
use_separate_proj_weight=use_separate_proj_weight,
|
150 |
+
q_proj_weight=q_proj_weight,
|
151 |
+
k_proj_weight=k_proj_weight,
|
152 |
+
v_proj_weight=v_proj_weight,
|
153 |
+
static_k=static_k,
|
154 |
+
static_v=static_v,
|
155 |
+
average_attn_weights=average_attn_weights,
|
156 |
+
cache=cache,
|
157 |
+
)
|
158 |
+
|
159 |
+
is_batched = _mha_shape_check(
|
160 |
+
query, key, value, key_padding_mask, attn_mask, num_heads
|
161 |
+
)
|
162 |
+
|
163 |
+
# For unbatched input, we unsqueeze at the expected batch-dim to pretend that the input
|
164 |
+
# is batched, run the computation and before returning squeeze the
|
165 |
+
# batch dimension so that the output doesn't carry this temporary batch dimension.
|
166 |
+
if not is_batched:
|
167 |
+
# unsqueeze if the input is unbatched
|
168 |
+
query = query.unsqueeze(1)
|
169 |
+
key = key.unsqueeze(1)
|
170 |
+
value = value.unsqueeze(1)
|
171 |
+
if key_padding_mask is not None:
|
172 |
+
key_padding_mask = key_padding_mask.unsqueeze(0)
|
173 |
+
|
174 |
+
# set up shape vars
|
175 |
+
tgt_len, bsz, embed_dim = query.shape
|
176 |
+
src_len, _, _ = key.shape
|
177 |
+
|
178 |
+
key_padding_mask = _canonical_mask(
|
179 |
+
mask=key_padding_mask,
|
180 |
+
mask_name="key_padding_mask",
|
181 |
+
other_type=_none_or_dtype(attn_mask),
|
182 |
+
other_name="attn_mask",
|
183 |
+
target_type=query.dtype,
|
184 |
+
)
|
185 |
+
|
186 |
+
if is_causal and attn_mask is None:
|
187 |
+
raise RuntimeError(
|
188 |
+
"Need attn_mask if specifying the is_causal hint. "
|
189 |
+
"You may use the Transformer module method "
|
190 |
+
"`generate_square_subsequent_mask` to create this mask."
|
191 |
+
)
|
192 |
+
|
193 |
+
if is_causal and key_padding_mask is None and not need_weights:
|
194 |
+
# when we have a kpm or need weights, we need attn_mask
|
195 |
+
# Otherwise, we use the is_causal hint go as is_causal
|
196 |
+
# indicator to SDPA.
|
197 |
+
attn_mask = None
|
198 |
+
else:
|
199 |
+
attn_mask = _canonical_mask(
|
200 |
+
mask=attn_mask,
|
201 |
+
mask_name="attn_mask",
|
202 |
+
other_type=None,
|
203 |
+
other_name="",
|
204 |
+
target_type=query.dtype,
|
205 |
+
check_other=False,
|
206 |
+
)
|
207 |
+
|
208 |
+
if key_padding_mask is not None:
|
209 |
+
# We have the attn_mask, and use that to merge kpm into it.
|
210 |
+
# Turn off use of is_causal hint, as the merged mask is no
|
211 |
+
# longer causal.
|
212 |
+
is_causal = False
|
213 |
+
|
214 |
+
assert (
|
215 |
+
embed_dim == embed_dim_to_check
|
216 |
+
), f"was expecting embedding dimension of {embed_dim_to_check}, but got {embed_dim}"
|
217 |
+
if isinstance(embed_dim, torch.Tensor):
|
218 |
+
# embed_dim can be a tensor when JIT tracing
|
219 |
+
head_dim = embed_dim.div(num_heads, rounding_mode="trunc")
|
220 |
+
else:
|
221 |
+
head_dim = embed_dim // num_heads
|
222 |
+
assert (
|
223 |
+
head_dim * num_heads == embed_dim
|
224 |
+
), f"embed_dim {embed_dim} not divisible by num_heads {num_heads}"
|
225 |
+
if use_separate_proj_weight:
|
226 |
+
# allow MHA to have different embedding dimensions when separate projection weights are used
|
227 |
+
assert (
|
228 |
+
key.shape[:2] == value.shape[:2]
|
229 |
+
), f"key's sequence and batch dims {key.shape[:2]} do not match value's {value.shape[:2]}"
|
230 |
+
else:
|
231 |
+
assert (
|
232 |
+
key.shape == value.shape
|
233 |
+
), f"key shape {key.shape} does not match value shape {value.shape}"
|
234 |
+
|
235 |
+
#
|
236 |
+
# compute in-projection
|
237 |
+
#
|
238 |
+
if not use_separate_proj_weight:
|
239 |
+
assert (
|
240 |
+
in_proj_weight is not None
|
241 |
+
), "use_separate_proj_weight is False but in_proj_weight is None"
|
242 |
+
q, k, v = _in_projection_packed(query, key, value, in_proj_weight, in_proj_bias)
|
243 |
+
else:
|
244 |
+
assert (
|
245 |
+
q_proj_weight is not None
|
246 |
+
), "use_separate_proj_weight is True but q_proj_weight is None"
|
247 |
+
assert (
|
248 |
+
k_proj_weight is not None
|
249 |
+
), "use_separate_proj_weight is True but k_proj_weight is None"
|
250 |
+
assert (
|
251 |
+
v_proj_weight is not None
|
252 |
+
), "use_separate_proj_weight is True but v_proj_weight is None"
|
253 |
+
if in_proj_bias is None:
|
254 |
+
b_q = b_k = b_v = None
|
255 |
+
else:
|
256 |
+
b_q, b_k, b_v = in_proj_bias.chunk(3)
|
257 |
+
q, k, v = _in_projection(
|
258 |
+
query,
|
259 |
+
key,
|
260 |
+
value,
|
261 |
+
q_proj_weight,
|
262 |
+
k_proj_weight,
|
263 |
+
v_proj_weight,
|
264 |
+
b_q,
|
265 |
+
b_k,
|
266 |
+
b_v,
|
267 |
+
)
|
268 |
+
if cache != None:
|
269 |
+
if cache["first_infer"] == 1:
|
270 |
+
cache["k"][cache["stage"]] = k
|
271 |
+
# print(0,cache["k"].shape)
|
272 |
+
cache["v"][cache["stage"]] = v
|
273 |
+
else: ###12个layer每个都要留自己的cache_kv
|
274 |
+
# print(1,cache["k"].shape)
|
275 |
+
cache["k"][cache["stage"]] = torch.cat(
|
276 |
+
[cache["k"][cache["stage"]], k], 0
|
277 |
+
) ##本来时序是1,但是proj的时候可能transpose了所以时序到0维了
|
278 |
+
cache["v"][cache["stage"]] = torch.cat([cache["v"][cache["stage"]], v], 0)
|
279 |
+
# print(2, cache["k"].shape)
|
280 |
+
src_len = cache["k"][cache["stage"]].shape[0]
|
281 |
+
k = cache["k"][cache["stage"]]
|
282 |
+
v = cache["v"][cache["stage"]]
|
283 |
+
# if attn_mask is not None:
|
284 |
+
# attn_mask=attn_mask[-1:,]
|
285 |
+
# print(attn_mask.shape,attn_mask)
|
286 |
+
cache["stage"] = (cache["stage"] + 1) % cache["all_stage"]
|
287 |
+
# print(2333,cache)
|
288 |
+
# prep attention mask
|
289 |
+
|
290 |
+
attn_mask = _canonical_mask(
|
291 |
+
mask=attn_mask,
|
292 |
+
mask_name="attn_mask",
|
293 |
+
other_type=None,
|
294 |
+
other_name="",
|
295 |
+
target_type=q.dtype,
|
296 |
+
check_other=False,
|
297 |
+
)
|
298 |
+
|
299 |
+
if attn_mask is not None:
|
300 |
+
# ensure attn_mask's dim is 3
|
301 |
+
if attn_mask.dim() == 2:
|
302 |
+
correct_2d_size = (tgt_len, src_len)
|
303 |
+
if attn_mask.shape != correct_2d_size:
|
304 |
+
raise RuntimeError(
|
305 |
+
f"The shape of the 2D attn_mask is {attn_mask.shape}, but should be {correct_2d_size}."
|
306 |
+
)
|
307 |
+
attn_mask = attn_mask.unsqueeze(0)
|
308 |
+
elif attn_mask.dim() == 3:
|
309 |
+
correct_3d_size = (bsz * num_heads, tgt_len, src_len)
|
310 |
+
if attn_mask.shape != correct_3d_size:
|
311 |
+
raise RuntimeError(
|
312 |
+
f"The shape of the 3D attn_mask is {attn_mask.shape}, but should be {correct_3d_size}."
|
313 |
+
)
|
314 |
+
else:
|
315 |
+
raise RuntimeError(
|
316 |
+
f"attn_mask's dimension {attn_mask.dim()} is not supported"
|
317 |
+
)
|
318 |
+
|
319 |
+
# add bias along batch dimension (currently second)
|
320 |
+
if bias_k is not None and bias_v is not None:
|
321 |
+
assert static_k is None, "bias cannot be added to static key."
|
322 |
+
assert static_v is None, "bias cannot be added to static value."
|
323 |
+
k = torch.cat([k, bias_k.repeat(1, bsz, 1)])
|
324 |
+
v = torch.cat([v, bias_v.repeat(1, bsz, 1)])
|
325 |
+
if attn_mask is not None:
|
326 |
+
attn_mask = pad(attn_mask, (0, 1))
|
327 |
+
if key_padding_mask is not None:
|
328 |
+
key_padding_mask = pad(key_padding_mask, (0, 1))
|
329 |
+
else:
|
330 |
+
assert bias_k is None
|
331 |
+
assert bias_v is None
|
332 |
+
|
333 |
+
#
|
334 |
+
# reshape q, k, v for multihead attention and make em batch first
|
335 |
+
#
|
336 |
+
q = q.view(tgt_len, bsz * num_heads, head_dim).transpose(0, 1)
|
337 |
+
if static_k is None:
|
338 |
+
k = k.view(k.shape[0], bsz * num_heads, head_dim).transpose(0, 1)
|
339 |
+
else:
|
340 |
+
# TODO finish disentangling control flow so we don't do in-projections when statics are passed
|
341 |
+
assert (
|
342 |
+
static_k.size(0) == bsz * num_heads
|
343 |
+
), f"expecting static_k.size(0) of {bsz * num_heads}, but got {static_k.size(0)}"
|
344 |
+
assert (
|
345 |
+
static_k.size(2) == head_dim
|
346 |
+
), f"expecting static_k.size(2) of {head_dim}, but got {static_k.size(2)}"
|
347 |
+
k = static_k
|
348 |
+
if static_v is None:
|
349 |
+
v = v.view(v.shape[0], bsz * num_heads, head_dim).transpose(0, 1)
|
350 |
+
else:
|
351 |
+
# TODO finish disentangling control flow so we don't do in-projections when statics are passed
|
352 |
+
assert (
|
353 |
+
static_v.size(0) == bsz * num_heads
|
354 |
+
), f"expecting static_v.size(0) of {bsz * num_heads}, but got {static_v.size(0)}"
|
355 |
+
assert (
|
356 |
+
static_v.size(2) == head_dim
|
357 |
+
), f"expecting static_v.size(2) of {head_dim}, but got {static_v.size(2)}"
|
358 |
+
v = static_v
|
359 |
+
|
360 |
+
# add zero attention along batch dimension (now first)
|
361 |
+
if add_zero_attn:
|
362 |
+
zero_attn_shape = (bsz * num_heads, 1, head_dim)
|
363 |
+
k = torch.cat(
|
364 |
+
[k, torch.zeros(zero_attn_shape, dtype=k.dtype, device=k.device)], dim=1
|
365 |
+
)
|
366 |
+
v = torch.cat(
|
367 |
+
[v, torch.zeros(zero_attn_shape, dtype=v.dtype, device=v.device)], dim=1
|
368 |
+
)
|
369 |
+
if attn_mask is not None:
|
370 |
+
attn_mask = pad(attn_mask, (0, 1))
|
371 |
+
if key_padding_mask is not None:
|
372 |
+
key_padding_mask = pad(key_padding_mask, (0, 1))
|
373 |
+
|
374 |
+
# update source sequence length after adjustments
|
375 |
+
src_len = k.size(1)
|
376 |
+
|
377 |
+
# merge key padding and attention masks
|
378 |
+
if key_padding_mask is not None:
|
379 |
+
assert key_padding_mask.shape == (
|
380 |
+
bsz,
|
381 |
+
src_len,
|
382 |
+
), f"expecting key_padding_mask shape of {(bsz, src_len)}, but got {key_padding_mask.shape}"
|
383 |
+
key_padding_mask = (
|
384 |
+
key_padding_mask.view(bsz, 1, 1, src_len)
|
385 |
+
.expand(-1, num_heads, -1, -1)
|
386 |
+
.reshape(bsz * num_heads, 1, src_len)
|
387 |
+
)
|
388 |
+
if attn_mask is None:
|
389 |
+
attn_mask = key_padding_mask
|
390 |
+
else:
|
391 |
+
attn_mask = attn_mask + key_padding_mask
|
392 |
+
|
393 |
+
# adjust dropout probability
|
394 |
+
if not training:
|
395 |
+
dropout_p = 0.0
|
396 |
+
|
397 |
+
#
|
398 |
+
# (deep breath) calculate attention and out projection
|
399 |
+
#
|
400 |
+
|
401 |
+
if need_weights:
|
402 |
+
B, Nt, E = q.shape
|
403 |
+
q_scaled = q / math.sqrt(E)
|
404 |
+
|
405 |
+
assert not (
|
406 |
+
is_causal and attn_mask is None
|
407 |
+
), "FIXME: is_causal not implemented for need_weights"
|
408 |
+
|
409 |
+
if attn_mask is not None:
|
410 |
+
attn_output_weights = torch.baddbmm(
|
411 |
+
attn_mask, q_scaled, k.transpose(-2, -1)
|
412 |
+
)
|
413 |
+
else:
|
414 |
+
attn_output_weights = torch.bmm(q_scaled, k.transpose(-2, -1))
|
415 |
+
attn_output_weights = softmax(attn_output_weights, dim=-1)
|
416 |
+
if dropout_p > 0.0:
|
417 |
+
attn_output_weights = dropout(attn_output_weights, p=dropout_p)
|
418 |
+
|
419 |
+
attn_output = torch.bmm(attn_output_weights, v)
|
420 |
+
|
421 |
+
attn_output = (
|
422 |
+
attn_output.transpose(0, 1).contiguous().view(tgt_len * bsz, embed_dim)
|
423 |
+
)
|
424 |
+
attn_output = linear(attn_output, out_proj_weight, out_proj_bias)
|
425 |
+
attn_output = attn_output.view(tgt_len, bsz, attn_output.size(1))
|
426 |
+
|
427 |
+
# optionally average attention weights over heads
|
428 |
+
attn_output_weights = attn_output_weights.view(bsz, num_heads, tgt_len, src_len)
|
429 |
+
if average_attn_weights:
|
430 |
+
attn_output_weights = attn_output_weights.mean(dim=1)
|
431 |
+
|
432 |
+
if not is_batched:
|
433 |
+
# squeeze the output if input was unbatched
|
434 |
+
attn_output = attn_output.squeeze(1)
|
435 |
+
attn_output_weights = attn_output_weights.squeeze(0)
|
436 |
+
return attn_output, attn_output_weights
|
437 |
+
else:
|
438 |
+
# attn_mask can be either (L,S) or (N*num_heads, L, S)
|
439 |
+
# if attn_mask's shape is (1, L, S) we need to unsqueeze to (1, 1, L, S)
|
440 |
+
# in order to match the input for SDPA of (N, num_heads, L, S)
|
441 |
+
if attn_mask is not None:
|
442 |
+
if attn_mask.size(0) == 1 and attn_mask.dim() == 3:
|
443 |
+
attn_mask = attn_mask.unsqueeze(0)
|
444 |
+
else:
|
445 |
+
attn_mask = attn_mask.view(bsz, num_heads, -1, src_len)
|
446 |
+
|
447 |
+
q = q.view(bsz, num_heads, tgt_len, head_dim)
|
448 |
+
k = k.view(bsz, num_heads, src_len, head_dim)
|
449 |
+
v = v.view(bsz, num_heads, src_len, head_dim)
|
450 |
+
|
451 |
+
# with torch.backends.cuda.sdp_kernel(enable_flash=True, enable_math=True, enable_mem_efficient=True):
|
452 |
+
attn_output = scaled_dot_product_attention(
|
453 |
+
q, k, v, attn_mask, dropout_p, is_causal
|
454 |
+
)
|
455 |
+
|
456 |
+
attn_output = (
|
457 |
+
attn_output.permute(2, 0, 1, 3).contiguous().view(bsz * tgt_len, embed_dim)
|
458 |
+
)
|
459 |
+
|
460 |
+
attn_output = linear(attn_output, out_proj_weight, out_proj_bias)
|
461 |
+
attn_output = attn_output.view(tgt_len, bsz, attn_output.size(1))
|
462 |
+
if not is_batched:
|
463 |
+
# squeeze the output if input was unbatched
|
464 |
+
attn_output = attn_output.squeeze(1)
|
465 |
+
return attn_output, None
|
pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3d2d132bb01eae38f54fd4eb0f2fd8087f0ddbeeeace5b81a39c26586db9a8ee
|
3 |
+
size 2201587998
|
quantize.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2 |
+
# All rights reserved.
|
3 |
+
#
|
4 |
+
# This source code is licensed under the license found in the
|
5 |
+
# LICENSE file in the root directory of this source tree.
|
6 |
+
|
7 |
+
"""Residual vector quantizer implementation."""
|
8 |
+
|
9 |
+
from dataclasses import dataclass, field
|
10 |
+
import typing as tp
|
11 |
+
|
12 |
+
import torch
|
13 |
+
from torch import nn
|
14 |
+
|
15 |
+
from .core_vq import ResidualVectorQuantization
|
16 |
+
|
17 |
+
|
18 |
+
@dataclass
|
19 |
+
class QuantizedResult:
|
20 |
+
quantized: torch.Tensor
|
21 |
+
codes: torch.Tensor
|
22 |
+
bandwidth: torch.Tensor # bandwidth in kb/s used, per batch item.
|
23 |
+
penalty: tp.Optional[torch.Tensor] = None
|
24 |
+
metrics: dict = field(default_factory=dict)
|
25 |
+
|
26 |
+
|
27 |
+
class ResidualVectorQuantizer(nn.Module):
|
28 |
+
"""Residual Vector Quantizer.
|
29 |
+
Args:
|
30 |
+
dimension (int): Dimension of the codebooks.
|
31 |
+
n_q (int): Number of residual vector quantizers used.
|
32 |
+
bins (int): Codebook size.
|
33 |
+
decay (float): Decay for exponential moving average over the codebooks.
|
34 |
+
kmeans_init (bool): Whether to use kmeans to initialize the codebooks.
|
35 |
+
kmeans_iters (int): Number of iterations used for kmeans initialization.
|
36 |
+
threshold_ema_dead_code (int): Threshold for dead code expiration. Replace any codes
|
37 |
+
that have an exponential moving average cluster size less than the specified threshold with
|
38 |
+
randomly selected vector from the current batch.
|
39 |
+
"""
|
40 |
+
|
41 |
+
def __init__(
|
42 |
+
self,
|
43 |
+
dimension: int = 256,
|
44 |
+
n_q: int = 8,
|
45 |
+
bins: int = 1024,
|
46 |
+
decay: float = 0.99,
|
47 |
+
kmeans_init: bool = True,
|
48 |
+
kmeans_iters: int = 50,
|
49 |
+
threshold_ema_dead_code: int = 2,
|
50 |
+
):
|
51 |
+
super().__init__()
|
52 |
+
self.n_q = n_q
|
53 |
+
self.dimension = dimension
|
54 |
+
self.bins = bins
|
55 |
+
self.decay = decay
|
56 |
+
self.kmeans_init = kmeans_init
|
57 |
+
self.kmeans_iters = kmeans_iters
|
58 |
+
self.threshold_ema_dead_code = threshold_ema_dead_code
|
59 |
+
self.vq = ResidualVectorQuantization(
|
60 |
+
dim=self.dimension,
|
61 |
+
codebook_size=self.bins,
|
62 |
+
num_quantizers=self.n_q,
|
63 |
+
decay=self.decay,
|
64 |
+
kmeans_init=self.kmeans_init,
|
65 |
+
kmeans_iters=self.kmeans_iters,
|
66 |
+
threshold_ema_dead_code=self.threshold_ema_dead_code,
|
67 |
+
)
|
68 |
+
|
69 |
+
def forward(
|
70 |
+
self,
|
71 |
+
x: torch.Tensor,
|
72 |
+
n_q: tp.Optional[int] = None,
|
73 |
+
layers: tp.Optional[list] = None,
|
74 |
+
) -> QuantizedResult:
|
75 |
+
"""Residual vector quantization on the given input tensor.
|
76 |
+
Args:
|
77 |
+
x (torch.Tensor): Input tensor.
|
78 |
+
n_q (int): Number of quantizer used to quantize. Default: All quantizers.
|
79 |
+
layers (list): Layer that need to return quantized. Defalt: None.
|
80 |
+
Returns:
|
81 |
+
QuantizedResult:
|
82 |
+
The quantized (or approximately quantized) representation with
|
83 |
+
the associated numbert quantizers and layer quantized required to return.
|
84 |
+
"""
|
85 |
+
n_q = n_q if n_q else self.n_q
|
86 |
+
if layers and max(layers) >= n_q:
|
87 |
+
raise ValueError(
|
88 |
+
f"Last layer index in layers: A {max(layers)}. Number of quantizers in RVQ: B {self.n_q}. A must less than B."
|
89 |
+
)
|
90 |
+
quantized, codes, commit_loss, quantized_list = self.vq(
|
91 |
+
x, n_q=n_q, layers=layers
|
92 |
+
)
|
93 |
+
return quantized, codes, torch.mean(commit_loss), quantized_list
|
94 |
+
|
95 |
+
def encode(
|
96 |
+
self, x: torch.Tensor, n_q: tp.Optional[int] = None, st: tp.Optional[int] = None
|
97 |
+
) -> torch.Tensor:
|
98 |
+
"""Encode a given input tensor with the specified sample rate at the given bandwidth.
|
99 |
+
The RVQ encode method sets the appropriate number of quantizer to use
|
100 |
+
and returns indices for each quantizer.
|
101 |
+
Args:
|
102 |
+
x (torch.Tensor): Input tensor.
|
103 |
+
n_q (int): Number of quantizer used to quantize. Default: All quantizers.
|
104 |
+
st (int): Start to encode input from which layers. Default: 0.
|
105 |
+
"""
|
106 |
+
n_q = n_q if n_q else self.n_q
|
107 |
+
st = st or 0
|
108 |
+
codes = self.vq.encode(x, n_q=n_q, st=st)
|
109 |
+
return codes
|
110 |
+
|
111 |
+
def decode(self, codes: torch.Tensor, st: int = 0) -> torch.Tensor:
|
112 |
+
"""Decode the given codes to the quantized representation.
|
113 |
+
Args:
|
114 |
+
codes (torch.Tensor): Input indices for each quantizer.
|
115 |
+
st (int): Start to decode input codes from which layers. Default: 0.
|
116 |
+
"""
|
117 |
+
quantized = self.vq.decode(codes, st=st)
|
118 |
+
return quantized
|
scaling.py
ADDED
@@ -0,0 +1,332 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2022 Xiaomi Corp. (authors: Daniel Povey)
|
2 |
+
#
|
3 |
+
# See ../../../../LICENSE for clarification regarding multiple authors
|
4 |
+
#
|
5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6 |
+
# you may not use this file except in compliance with the License.
|
7 |
+
# You may obtain a copy of the License at
|
8 |
+
#
|
9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
#
|
11 |
+
# Unless required by applicable law or agreed to in writing, software
|
12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 |
+
# See the License for the specific language governing permissions and
|
15 |
+
# limitations under the License.
|
16 |
+
import random
|
17 |
+
from typing import Optional
|
18 |
+
from typing import Tuple
|
19 |
+
|
20 |
+
import torch
|
21 |
+
import torch.nn as nn
|
22 |
+
from torch import Tensor
|
23 |
+
|
24 |
+
|
25 |
+
class DoubleSwishFunction(torch.autograd.Function):
|
26 |
+
"""
|
27 |
+
double_swish(x) = x * torch.sigmoid(x-1)
|
28 |
+
This is a definition, originally motivated by its close numerical
|
29 |
+
similarity to swish(swish(x)), where swish(x) = x * sigmoid(x).
|
30 |
+
|
31 |
+
Memory-efficient derivative computation:
|
32 |
+
double_swish(x) = x * s, where s(x) = torch.sigmoid(x-1)
|
33 |
+
double_swish'(x) = d/dx double_swish(x) = x * s'(x) + x' * s(x) = x * s'(x) + s(x).
|
34 |
+
Now, s'(x) = s(x) * (1-s(x)).
|
35 |
+
double_swish'(x) = x * s'(x) + s(x).
|
36 |
+
= x * s(x) * (1-s(x)) + s(x).
|
37 |
+
= double_swish(x) * (1-s(x)) + s(x)
|
38 |
+
... so we just need to remember s(x) but not x itself.
|
39 |
+
"""
|
40 |
+
|
41 |
+
@staticmethod
|
42 |
+
def forward(ctx, x: Tensor) -> Tensor:
|
43 |
+
requires_grad = x.requires_grad
|
44 |
+
x_dtype = x.dtype
|
45 |
+
if x.dtype == torch.float16:
|
46 |
+
x = x.to(torch.float32)
|
47 |
+
|
48 |
+
s = torch.sigmoid(x - 1.0)
|
49 |
+
y = x * s
|
50 |
+
|
51 |
+
if requires_grad:
|
52 |
+
deriv = y * (1 - s) + s
|
53 |
+
# notes on derivative of x * sigmoid(x - 1):
|
54 |
+
# https://www.wolframalpha.com/input?i=d%2Fdx+%28x+*+sigmoid%28x-1%29%29
|
55 |
+
# min \simeq -0.043638. Take floor as -0.043637 so it's a lower bund
|
56 |
+
# max \simeq 1.1990. Take ceil to be 1.2 so it's an upper bound.
|
57 |
+
# the combination of "+ torch.rand_like(deriv)" and casting to torch.uint8 (which
|
58 |
+
# floors), should be expectation-preserving.
|
59 |
+
floor = -0.043637
|
60 |
+
ceil = 1.2
|
61 |
+
d_scaled = (deriv - floor) * (255.0 / (ceil - floor)) + torch.rand_like(
|
62 |
+
deriv
|
63 |
+
)
|
64 |
+
if __name__ == "__main__":
|
65 |
+
# for self-testing only.
|
66 |
+
assert d_scaled.min() >= 0.0
|
67 |
+
assert d_scaled.max() < 256.0
|
68 |
+
d_int = d_scaled.to(torch.uint8)
|
69 |
+
ctx.save_for_backward(d_int)
|
70 |
+
if x.dtype == torch.float16 or torch.is_autocast_enabled():
|
71 |
+
y = y.to(torch.float16)
|
72 |
+
return y
|
73 |
+
|
74 |
+
@staticmethod
|
75 |
+
def backward(ctx, y_grad: Tensor) -> Tensor:
|
76 |
+
(d,) = ctx.saved_tensors
|
77 |
+
# the same constants as used in forward pass.
|
78 |
+
floor = -0.043637
|
79 |
+
ceil = 1.2
|
80 |
+
d = d * ((ceil - floor) / 255.0) + floor
|
81 |
+
return y_grad * d
|
82 |
+
|
83 |
+
|
84 |
+
class DoubleSwish(torch.nn.Module):
|
85 |
+
def forward(self, x: Tensor) -> Tensor:
|
86 |
+
"""Return double-swish activation function which is an approximation to Swish(Swish(x)),
|
87 |
+
that we approximate closely with x * sigmoid(x-1).
|
88 |
+
"""
|
89 |
+
if torch.jit.is_scripting() or torch.jit.is_tracing():
|
90 |
+
return x * torch.sigmoid(x - 1.0)
|
91 |
+
return DoubleSwishFunction.apply(x)
|
92 |
+
|
93 |
+
|
94 |
+
class ActivationBalancerFunction(torch.autograd.Function):
|
95 |
+
@staticmethod
|
96 |
+
def forward(
|
97 |
+
ctx,
|
98 |
+
x: Tensor,
|
99 |
+
scale_factor: Tensor,
|
100 |
+
sign_factor: Optional[Tensor],
|
101 |
+
channel_dim: int,
|
102 |
+
) -> Tensor:
|
103 |
+
if channel_dim < 0:
|
104 |
+
channel_dim += x.ndim
|
105 |
+
ctx.channel_dim = channel_dim
|
106 |
+
xgt0 = x > 0
|
107 |
+
if sign_factor is None:
|
108 |
+
ctx.save_for_backward(xgt0, scale_factor)
|
109 |
+
else:
|
110 |
+
ctx.save_for_backward(xgt0, scale_factor, sign_factor)
|
111 |
+
return x
|
112 |
+
|
113 |
+
@staticmethod
|
114 |
+
def backward(ctx, x_grad: Tensor) -> Tuple[Tensor, None, None, None]:
|
115 |
+
if len(ctx.saved_tensors) == 3:
|
116 |
+
xgt0, scale_factor, sign_factor = ctx.saved_tensors
|
117 |
+
for _ in range(ctx.channel_dim, x_grad.ndim - 1):
|
118 |
+
scale_factor = scale_factor.unsqueeze(-1)
|
119 |
+
sign_factor = sign_factor.unsqueeze(-1)
|
120 |
+
factor = sign_factor + scale_factor * (xgt0.to(x_grad.dtype) - 0.5)
|
121 |
+
else:
|
122 |
+
xgt0, scale_factor = ctx.saved_tensors
|
123 |
+
for _ in range(ctx.channel_dim, x_grad.ndim - 1):
|
124 |
+
scale_factor = scale_factor.unsqueeze(-1)
|
125 |
+
factor = scale_factor * (xgt0.to(x_grad.dtype) - 0.5)
|
126 |
+
neg_delta_grad = x_grad.abs() * factor
|
127 |
+
return (
|
128 |
+
x_grad - neg_delta_grad,
|
129 |
+
None,
|
130 |
+
None,
|
131 |
+
None,
|
132 |
+
)
|
133 |
+
|
134 |
+
|
135 |
+
def _compute_scale_factor(
|
136 |
+
x: Tensor,
|
137 |
+
channel_dim: int,
|
138 |
+
min_abs: float,
|
139 |
+
max_abs: float,
|
140 |
+
gain_factor: float,
|
141 |
+
max_factor: float,
|
142 |
+
) -> Tensor:
|
143 |
+
if channel_dim < 0:
|
144 |
+
channel_dim += x.ndim
|
145 |
+
sum_dims = [d for d in range(x.ndim) if d != channel_dim]
|
146 |
+
x_abs_mean = torch.mean(x.abs(), dim=sum_dims).to(torch.float32)
|
147 |
+
|
148 |
+
if min_abs == 0.0:
|
149 |
+
below_threshold = 0.0
|
150 |
+
else:
|
151 |
+
# below_threshold is 0 if x_abs_mean > min_abs, can be at most max_factor if
|
152 |
+
# x_abs)_mean , min_abs.
|
153 |
+
below_threshold = ((min_abs - x_abs_mean) * (gain_factor / min_abs)).clamp(
|
154 |
+
min=0, max=max_factor
|
155 |
+
)
|
156 |
+
|
157 |
+
above_threshold = ((x_abs_mean - max_abs) * (gain_factor / max_abs)).clamp(
|
158 |
+
min=0, max=max_factor
|
159 |
+
)
|
160 |
+
|
161 |
+
return below_threshold - above_threshold
|
162 |
+
|
163 |
+
|
164 |
+
def _compute_sign_factor(
|
165 |
+
x: Tensor,
|
166 |
+
channel_dim: int,
|
167 |
+
min_positive: float,
|
168 |
+
max_positive: float,
|
169 |
+
gain_factor: float,
|
170 |
+
max_factor: float,
|
171 |
+
) -> Tensor:
|
172 |
+
if channel_dim < 0:
|
173 |
+
channel_dim += x.ndim
|
174 |
+
sum_dims = [d for d in range(x.ndim) if d != channel_dim]
|
175 |
+
proportion_positive = torch.mean((x > 0).to(torch.float32), dim=sum_dims)
|
176 |
+
if min_positive == 0.0:
|
177 |
+
factor1 = 0.0
|
178 |
+
else:
|
179 |
+
# 0 if proportion_positive >= min_positive, else can be
|
180 |
+
# as large as max_factor.
|
181 |
+
factor1 = (
|
182 |
+
(min_positive - proportion_positive) * (gain_factor / min_positive)
|
183 |
+
).clamp_(min=0, max=max_factor)
|
184 |
+
|
185 |
+
if max_positive == 1.0:
|
186 |
+
factor2 = 0.0
|
187 |
+
else:
|
188 |
+
# 0 if self.proportion_positive <= max_positive, else can be
|
189 |
+
# as large as -max_factor.
|
190 |
+
factor2 = (
|
191 |
+
(proportion_positive - max_positive) * (gain_factor / (1.0 - max_positive))
|
192 |
+
).clamp_(min=0, max=max_factor)
|
193 |
+
sign_factor = factor1 - factor2
|
194 |
+
# require min_positive != 0 or max_positive != 1:
|
195 |
+
assert not isinstance(sign_factor, float)
|
196 |
+
return sign_factor
|
197 |
+
|
198 |
+
|
199 |
+
class ActivationBalancer(torch.nn.Module):
|
200 |
+
"""
|
201 |
+
Modifies the backpropped derivatives of a function to try to encourage, for
|
202 |
+
each channel, that it is positive at least a proportion `threshold` of the
|
203 |
+
time. It does this by multiplying negative derivative values by up to
|
204 |
+
(1+max_factor), and positive derivative values by up to (1-max_factor),
|
205 |
+
interpolated from 1 at the threshold to those extremal values when none
|
206 |
+
of the inputs are positive.
|
207 |
+
|
208 |
+
Args:
|
209 |
+
num_channels: the number of channels
|
210 |
+
channel_dim: the dimension/axis corresponding to the channel, e.g.
|
211 |
+
-1, 0, 1, 2; will be interpreted as an offset from x.ndim if negative.
|
212 |
+
min_positive: the minimum, per channel, of the proportion of the time
|
213 |
+
that (x > 0), below which we start to modify the derivatives.
|
214 |
+
max_positive: the maximum, per channel, of the proportion of the time
|
215 |
+
that (x > 0), above which we start to modify the derivatives.
|
216 |
+
max_factor: the maximum factor by which we modify the derivatives for
|
217 |
+
either the sign constraint or the magnitude constraint;
|
218 |
+
e.g. with max_factor=0.02, the the derivatives would be multiplied by
|
219 |
+
values in the range [0.98..1.02].
|
220 |
+
sign_gain_factor: determines the 'gain' with which we increase the
|
221 |
+
change in gradient once the constraints on min_positive and max_positive
|
222 |
+
are violated.
|
223 |
+
scale_gain_factor: determines the 'gain' with which we increase the
|
224 |
+
change in gradient once the constraints on min_abs and max_abs
|
225 |
+
are violated.
|
226 |
+
min_abs: the minimum average-absolute-value difference from the mean
|
227 |
+
value per channel, which we allow, before we start to modify
|
228 |
+
the derivatives to prevent this.
|
229 |
+
max_abs: the maximum average-absolute-value difference from the mean
|
230 |
+
value per channel, which we allow, before we start to modify
|
231 |
+
the derivatives to prevent this.
|
232 |
+
min_prob: determines the minimum probability with which we modify the
|
233 |
+
gradients for the {min,max}_positive and {min,max}_abs constraints,
|
234 |
+
on each forward(). This is done randomly to prevent all layers
|
235 |
+
from doing it at the same time. Early in training we may use
|
236 |
+
higher probabilities than this; it will decay to this value.
|
237 |
+
"""
|
238 |
+
|
239 |
+
def __init__(
|
240 |
+
self,
|
241 |
+
num_channels: int,
|
242 |
+
channel_dim: int,
|
243 |
+
min_positive: float = 0.05,
|
244 |
+
max_positive: float = 0.95,
|
245 |
+
max_factor: float = 0.04,
|
246 |
+
sign_gain_factor: float = 0.01,
|
247 |
+
scale_gain_factor: float = 0.02,
|
248 |
+
min_abs: float = 0.2,
|
249 |
+
max_abs: float = 100.0,
|
250 |
+
min_prob: float = 0.1,
|
251 |
+
):
|
252 |
+
super(ActivationBalancer, self).__init__()
|
253 |
+
self.num_channels = num_channels
|
254 |
+
self.channel_dim = channel_dim
|
255 |
+
self.min_positive = min_positive
|
256 |
+
self.max_positive = max_positive
|
257 |
+
self.max_factor = max_factor
|
258 |
+
self.min_abs = min_abs
|
259 |
+
self.max_abs = max_abs
|
260 |
+
self.min_prob = min_prob
|
261 |
+
self.sign_gain_factor = sign_gain_factor
|
262 |
+
self.scale_gain_factor = scale_gain_factor
|
263 |
+
|
264 |
+
# count measures how many times the forward() function has been called.
|
265 |
+
# We occasionally sync this to a tensor called `count`, that exists to
|
266 |
+
# make sure it is synced to disk when we load and save the model.
|
267 |
+
self.cpu_count = 0
|
268 |
+
self.register_buffer("count", torch.tensor(0, dtype=torch.int64))
|
269 |
+
|
270 |
+
def forward(self, x: Tensor) -> Tensor:
|
271 |
+
if torch.jit.is_scripting() or not x.requires_grad or torch.jit.is_tracing():
|
272 |
+
return _no_op(x)
|
273 |
+
|
274 |
+
count = self.cpu_count
|
275 |
+
self.cpu_count += 1
|
276 |
+
|
277 |
+
if random.random() < 0.01:
|
278 |
+
# Occasionally sync self.cpu_count with self.count.
|
279 |
+
# count affects the decay of 'prob'. don't do this on every iter,
|
280 |
+
# because syncing with the GPU is slow.
|
281 |
+
self.cpu_count = max(self.cpu_count, self.count.item())
|
282 |
+
self.count.fill_(self.cpu_count)
|
283 |
+
|
284 |
+
# the prob of doing some work exponentially decreases from 0.5 till it hits
|
285 |
+
# a floor at min_prob (==0.1, by default)
|
286 |
+
prob = max(self.min_prob, 0.5 ** (1 + (count / 4000.0)))
|
287 |
+
|
288 |
+
if random.random() < prob:
|
289 |
+
sign_gain_factor = 0.5
|
290 |
+
if self.min_positive != 0.0 or self.max_positive != 1.0:
|
291 |
+
sign_factor = _compute_sign_factor(
|
292 |
+
x,
|
293 |
+
self.channel_dim,
|
294 |
+
self.min_positive,
|
295 |
+
self.max_positive,
|
296 |
+
gain_factor=self.sign_gain_factor / prob,
|
297 |
+
max_factor=self.max_factor,
|
298 |
+
)
|
299 |
+
else:
|
300 |
+
sign_factor = None
|
301 |
+
|
302 |
+
scale_factor = _compute_scale_factor(
|
303 |
+
x.detach(),
|
304 |
+
self.channel_dim,
|
305 |
+
min_abs=self.min_abs,
|
306 |
+
max_abs=self.max_abs,
|
307 |
+
gain_factor=self.scale_gain_factor / prob,
|
308 |
+
max_factor=self.max_factor,
|
309 |
+
)
|
310 |
+
return ActivationBalancerFunction.apply(
|
311 |
+
x,
|
312 |
+
scale_factor,
|
313 |
+
sign_factor,
|
314 |
+
self.channel_dim,
|
315 |
+
)
|
316 |
+
else:
|
317 |
+
return _no_op(x)
|
318 |
+
|
319 |
+
|
320 |
+
def BalancedDoubleSwish(
|
321 |
+
d_model, channel_dim=-1, max_abs=10.0, min_prob=0.25
|
322 |
+
) -> nn.Sequential:
|
323 |
+
"""
|
324 |
+
ActivationBalancer -> DoubleSwish
|
325 |
+
"""
|
326 |
+
balancer = ActivationBalancer(
|
327 |
+
d_model, channel_dim=channel_dim, max_abs=max_abs, min_prob=min_prob
|
328 |
+
)
|
329 |
+
return nn.Sequential(
|
330 |
+
balancer,
|
331 |
+
DoubleSwish(),
|
332 |
+
)
|
symbols.py
ADDED
@@ -0,0 +1,412 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# punctuation = ['!', '?', '…', ",", ".","@"]#@是SP停顿
|
2 |
+
punctuation = ["!", "?", "…", ",", "."] # @是SP停顿
|
3 |
+
punctuation.append("-")
|
4 |
+
pu_symbols = punctuation + ["SP", "SP2", "SP3", "UNK"]
|
5 |
+
# pu_symbols = punctuation + ["SP", 'SP2', 'SP3','SP4', "UNK"]
|
6 |
+
pad = "_"
|
7 |
+
|
8 |
+
c = [
|
9 |
+
"AA",
|
10 |
+
"EE",
|
11 |
+
"OO",
|
12 |
+
"b",
|
13 |
+
"c",
|
14 |
+
"ch",
|
15 |
+
"d",
|
16 |
+
"f",
|
17 |
+
"g",
|
18 |
+
"h",
|
19 |
+
"j",
|
20 |
+
"k",
|
21 |
+
"l",
|
22 |
+
"m",
|
23 |
+
"n",
|
24 |
+
"p",
|
25 |
+
"q",
|
26 |
+
"r",
|
27 |
+
"s",
|
28 |
+
"sh",
|
29 |
+
"t",
|
30 |
+
"w",
|
31 |
+
"x",
|
32 |
+
"y",
|
33 |
+
"z",
|
34 |
+
"zh",
|
35 |
+
]
|
36 |
+
v = [
|
37 |
+
"E1",
|
38 |
+
"En1",
|
39 |
+
"a1",
|
40 |
+
"ai1",
|
41 |
+
"an1",
|
42 |
+
"ang1",
|
43 |
+
"ao1",
|
44 |
+
"e1",
|
45 |
+
"ei1",
|
46 |
+
"en1",
|
47 |
+
"eng1",
|
48 |
+
"er1",
|
49 |
+
"i1",
|
50 |
+
"i01",
|
51 |
+
"ia1",
|
52 |
+
"ian1",
|
53 |
+
"iang1",
|
54 |
+
"iao1",
|
55 |
+
"ie1",
|
56 |
+
"in1",
|
57 |
+
"ing1",
|
58 |
+
"iong1",
|
59 |
+
"ir1",
|
60 |
+
"iu1",
|
61 |
+
"o1",
|
62 |
+
"ong1",
|
63 |
+
"ou1",
|
64 |
+
"u1",
|
65 |
+
"ua1",
|
66 |
+
"uai1",
|
67 |
+
"uan1",
|
68 |
+
"uang1",
|
69 |
+
"ui1",
|
70 |
+
"un1",
|
71 |
+
"uo1",
|
72 |
+
"v1",
|
73 |
+
"van1",
|
74 |
+
"ve1",
|
75 |
+
"vn1",
|
76 |
+
"E2",
|
77 |
+
"En2",
|
78 |
+
"a2",
|
79 |
+
"ai2",
|
80 |
+
"an2",
|
81 |
+
"ang2",
|
82 |
+
"ao2",
|
83 |
+
"e2",
|
84 |
+
"ei2",
|
85 |
+
"en2",
|
86 |
+
"eng2",
|
87 |
+
"er2",
|
88 |
+
"i2",
|
89 |
+
"i02",
|
90 |
+
"ia2",
|
91 |
+
"ian2",
|
92 |
+
"iang2",
|
93 |
+
"iao2",
|
94 |
+
"ie2",
|
95 |
+
"in2",
|
96 |
+
"ing2",
|
97 |
+
"iong2",
|
98 |
+
"ir2",
|
99 |
+
"iu2",
|
100 |
+
"o2",
|
101 |
+
"ong2",
|
102 |
+
"ou2",
|
103 |
+
"u2",
|
104 |
+
"ua2",
|
105 |
+
"uai2",
|
106 |
+
"uan2",
|
107 |
+
"uang2",
|
108 |
+
"ui2",
|
109 |
+
"un2",
|
110 |
+
"uo2",
|
111 |
+
"v2",
|
112 |
+
"van2",
|
113 |
+
"ve2",
|
114 |
+
"vn2",
|
115 |
+
"E3",
|
116 |
+
"En3",
|
117 |
+
"a3",
|
118 |
+
"ai3",
|
119 |
+
"an3",
|
120 |
+
"ang3",
|
121 |
+
"ao3",
|
122 |
+
"e3",
|
123 |
+
"ei3",
|
124 |
+
"en3",
|
125 |
+
"eng3",
|
126 |
+
"er3",
|
127 |
+
"i3",
|
128 |
+
"i03",
|
129 |
+
"ia3",
|
130 |
+
"ian3",
|
131 |
+
"iang3",
|
132 |
+
"iao3",
|
133 |
+
"ie3",
|
134 |
+
"in3",
|
135 |
+
"ing3",
|
136 |
+
"iong3",
|
137 |
+
"ir3",
|
138 |
+
"iu3",
|
139 |
+
"o3",
|
140 |
+
"ong3",
|
141 |
+
"ou3",
|
142 |
+
"u3",
|
143 |
+
"ua3",
|
144 |
+
"uai3",
|
145 |
+
"uan3",
|
146 |
+
"uang3",
|
147 |
+
"ui3",
|
148 |
+
"un3",
|
149 |
+
"uo3",
|
150 |
+
"v3",
|
151 |
+
"van3",
|
152 |
+
"ve3",
|
153 |
+
"vn3",
|
154 |
+
"E4",
|
155 |
+
"En4",
|
156 |
+
"a4",
|
157 |
+
"ai4",
|
158 |
+
"an4",
|
159 |
+
"ang4",
|
160 |
+
"ao4",
|
161 |
+
"e4",
|
162 |
+
"ei4",
|
163 |
+
"en4",
|
164 |
+
"eng4",
|
165 |
+
"er4",
|
166 |
+
"i4",
|
167 |
+
"i04",
|
168 |
+
"ia4",
|
169 |
+
"ian4",
|
170 |
+
"iang4",
|
171 |
+
"iao4",
|
172 |
+
"ie4",
|
173 |
+
"in4",
|
174 |
+
"ing4",
|
175 |
+
"iong4",
|
176 |
+
"ir4",
|
177 |
+
"iu4",
|
178 |
+
"o4",
|
179 |
+
"ong4",
|
180 |
+
"ou4",
|
181 |
+
"u4",
|
182 |
+
"ua4",
|
183 |
+
"uai4",
|
184 |
+
"uan4",
|
185 |
+
"uang4",
|
186 |
+
"ui4",
|
187 |
+
"un4",
|
188 |
+
"uo4",
|
189 |
+
"v4",
|
190 |
+
"van4",
|
191 |
+
"ve4",
|
192 |
+
"vn4",
|
193 |
+
"E5",
|
194 |
+
"En5",
|
195 |
+
"a5",
|
196 |
+
"ai5",
|
197 |
+
"an5",
|
198 |
+
"ang5",
|
199 |
+
"ao5",
|
200 |
+
"e5",
|
201 |
+
"ei5",
|
202 |
+
"en5",
|
203 |
+
"eng5",
|
204 |
+
"er5",
|
205 |
+
"i5",
|
206 |
+
"i05",
|
207 |
+
"ia5",
|
208 |
+
"ian5",
|
209 |
+
"iang5",
|
210 |
+
"iao5",
|
211 |
+
"ie5",
|
212 |
+
"in5",
|
213 |
+
"ing5",
|
214 |
+
"iong5",
|
215 |
+
"ir5",
|
216 |
+
"iu5",
|
217 |
+
"o5",
|
218 |
+
"ong5",
|
219 |
+
"ou5",
|
220 |
+
"u5",
|
221 |
+
"ua5",
|
222 |
+
"uai5",
|
223 |
+
"uan5",
|
224 |
+
"uang5",
|
225 |
+
"ui5",
|
226 |
+
"un5",
|
227 |
+
"uo5",
|
228 |
+
"v5",
|
229 |
+
"van5",
|
230 |
+
"ve5",
|
231 |
+
"vn5",
|
232 |
+
]
|
233 |
+
|
234 |
+
v_without_tone = [
|
235 |
+
"E",
|
236 |
+
"En",
|
237 |
+
"a",
|
238 |
+
"ai",
|
239 |
+
"an",
|
240 |
+
"ang",
|
241 |
+
"ao",
|
242 |
+
"e",
|
243 |
+
"ei",
|
244 |
+
"en",
|
245 |
+
"eng",
|
246 |
+
"er",
|
247 |
+
"i",
|
248 |
+
"i0",
|
249 |
+
"ia",
|
250 |
+
"ian",
|
251 |
+
"iang",
|
252 |
+
"iao",
|
253 |
+
"ie",
|
254 |
+
"in",
|
255 |
+
"ing",
|
256 |
+
"iong",
|
257 |
+
"ir",
|
258 |
+
"iu",
|
259 |
+
"o",
|
260 |
+
"ong",
|
261 |
+
"ou",
|
262 |
+
"u",
|
263 |
+
"ua",
|
264 |
+
"uai",
|
265 |
+
"uan",
|
266 |
+
"uang",
|
267 |
+
"ui",
|
268 |
+
"un",
|
269 |
+
"uo",
|
270 |
+
"v",
|
271 |
+
"van",
|
272 |
+
"ve",
|
273 |
+
"vn",
|
274 |
+
]
|
275 |
+
|
276 |
+
# japanese
|
277 |
+
ja_symbols = [
|
278 |
+
"I",
|
279 |
+
"N",
|
280 |
+
"U",
|
281 |
+
"a",
|
282 |
+
"b",
|
283 |
+
"by",
|
284 |
+
"ch",
|
285 |
+
"cl",
|
286 |
+
"d",
|
287 |
+
"dy",
|
288 |
+
"e",
|
289 |
+
"f",
|
290 |
+
"g",
|
291 |
+
"gy",
|
292 |
+
"h",
|
293 |
+
"hy",
|
294 |
+
"i",
|
295 |
+
"j",
|
296 |
+
"k",
|
297 |
+
"ky",
|
298 |
+
"m",
|
299 |
+
"my",
|
300 |
+
"n",
|
301 |
+
"ny",
|
302 |
+
"o",
|
303 |
+
"p",
|
304 |
+
"py",
|
305 |
+
"r",
|
306 |
+
"ry",
|
307 |
+
"s",
|
308 |
+
"sh",
|
309 |
+
"t",
|
310 |
+
"ts",
|
311 |
+
"u",
|
312 |
+
"v",
|
313 |
+
"w",
|
314 |
+
"y",
|
315 |
+
"z",
|
316 |
+
# "[", #上升调型
|
317 |
+
# "]", #下降调型
|
318 |
+
# "$", #结束符
|
319 |
+
# "^", #开始符
|
320 |
+
]
|
321 |
+
|
322 |
+
arpa = {
|
323 |
+
"AH0",
|
324 |
+
"S",
|
325 |
+
"AH1",
|
326 |
+
"EY2",
|
327 |
+
"AE2",
|
328 |
+
"EH0",
|
329 |
+
"OW2",
|
330 |
+
"UH0",
|
331 |
+
"NG",
|
332 |
+
"B",
|
333 |
+
"G",
|
334 |
+
"AY0",
|
335 |
+
"M",
|
336 |
+
"AA0",
|
337 |
+
"F",
|
338 |
+
"AO0",
|
339 |
+
"ER2",
|
340 |
+
"UH1",
|
341 |
+
"IY1",
|
342 |
+
"AH2",
|
343 |
+
"DH",
|
344 |
+
"IY0",
|
345 |
+
"EY1",
|
346 |
+
"IH0",
|
347 |
+
"K",
|
348 |
+
"N",
|
349 |
+
"W",
|
350 |
+
"IY2",
|
351 |
+
"T",
|
352 |
+
"AA1",
|
353 |
+
"ER1",
|
354 |
+
"EH2",
|
355 |
+
"OY0",
|
356 |
+
"UH2",
|
357 |
+
"UW1",
|
358 |
+
"Z",
|
359 |
+
"AW2",
|
360 |
+
"AW1",
|
361 |
+
"V",
|
362 |
+
"UW2",
|
363 |
+
"AA2",
|
364 |
+
"ER",
|
365 |
+
"AW0",
|
366 |
+
"UW0",
|
367 |
+
"R",
|
368 |
+
"OW1",
|
369 |
+
"EH1",
|
370 |
+
"ZH",
|
371 |
+
"AE0",
|
372 |
+
"IH2",
|
373 |
+
"IH",
|
374 |
+
"Y",
|
375 |
+
"JH",
|
376 |
+
"P",
|
377 |
+
"AY1",
|
378 |
+
"EY0",
|
379 |
+
"OY2",
|
380 |
+
"TH",
|
381 |
+
"HH",
|
382 |
+
"D",
|
383 |
+
"ER0",
|
384 |
+
"CH",
|
385 |
+
"AO1",
|
386 |
+
"AE1",
|
387 |
+
"AO2",
|
388 |
+
"OY1",
|
389 |
+
"AY2",
|
390 |
+
"IH1",
|
391 |
+
"OW0",
|
392 |
+
"L",
|
393 |
+
"SH",
|
394 |
+
}
|
395 |
+
|
396 |
+
symbols = [pad] + c + v + ja_symbols + pu_symbols + list(arpa)
|
397 |
+
symbols = sorted(set(symbols))
|
398 |
+
|
399 |
+
_symbol_to_id = {s: i for i, s in enumerate(symbols)}
|
400 |
+
|
401 |
+
def cleaned_text_to_sequence(cleaned_text):
|
402 |
+
'''Converts a string of text to a sequence of IDs corresponding to the symbols in the text.
|
403 |
+
Args:
|
404 |
+
text: string to convert to a sequence
|
405 |
+
Returns:
|
406 |
+
List of integers corresponding to the symbols in the text
|
407 |
+
'''
|
408 |
+
phones = [_symbol_to_id[symbol] for symbol in cleaned_text]
|
409 |
+
return phones
|
410 |
+
|
411 |
+
if __name__ == "__main__":
|
412 |
+
print(len(symbols))
|
t2s_lightning_module.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# modified from https://github.com/feng-yufei/shared_debugging_code/blob/main/model/t2s_lightning_module.py
|
2 |
+
import os, sys
|
3 |
+
|
4 |
+
now_dir = os.getcwd()
|
5 |
+
sys.path.append(now_dir)
|
6 |
+
from typing import Dict
|
7 |
+
|
8 |
+
import torch
|
9 |
+
from pytorch_lightning import LightningModule
|
10 |
+
from .t2s_model import Text2SemanticDecoder
|
11 |
+
from .lr_schedulers import WarmupCosineLRSchedule
|
12 |
+
from .optim import ScaledAdam
|
13 |
+
|
14 |
+
class Text2SemanticLightningModule(LightningModule):
|
15 |
+
def __init__(self, config, output_dir, is_train=True):
|
16 |
+
super().__init__()
|
17 |
+
self.config = config
|
18 |
+
self.top_k = 3
|
19 |
+
self.model = Text2SemanticDecoder(config=config, top_k=self.top_k)
|
20 |
+
pretrained_s1 = config.get("pretrained_s1")
|
21 |
+
if pretrained_s1 and is_train:
|
22 |
+
# print(self.load_state_dict(torch.load(pretrained_s1,map_location="cpu")["state_dict"]))
|
23 |
+
print(
|
24 |
+
self.load_state_dict(
|
25 |
+
torch.load(pretrained_s1, map_location="cpu")["weight"]
|
26 |
+
)
|
27 |
+
)
|
28 |
+
if is_train:
|
29 |
+
self.automatic_optimization = False
|
30 |
+
self.save_hyperparameters()
|
31 |
+
self.eval_dir = output_dir / "eval"
|
32 |
+
self.eval_dir.mkdir(parents=True, exist_ok=True)
|
33 |
+
|
34 |
+
def training_step(self, batch: Dict, batch_idx: int):
|
35 |
+
opt = self.optimizers()
|
36 |
+
scheduler = self.lr_schedulers()
|
37 |
+
forward=self.model.forward if self.config["train"].get("if_dpo",False)==True else self.model.forward_old
|
38 |
+
loss, acc = forward(
|
39 |
+
batch["phoneme_ids"],
|
40 |
+
batch["phoneme_ids_len"],
|
41 |
+
batch["semantic_ids"],
|
42 |
+
batch["semantic_ids_len"],
|
43 |
+
batch["bert_feature"],
|
44 |
+
)
|
45 |
+
self.manual_backward(loss)
|
46 |
+
if batch_idx > 0 and batch_idx % 4 == 0:
|
47 |
+
opt.step()
|
48 |
+
opt.zero_grad()
|
49 |
+
scheduler.step()
|
50 |
+
|
51 |
+
self.log(
|
52 |
+
"total_loss",
|
53 |
+
loss,
|
54 |
+
on_step=True,
|
55 |
+
on_epoch=True,
|
56 |
+
prog_bar=True,
|
57 |
+
sync_dist=True,
|
58 |
+
)
|
59 |
+
self.log(
|
60 |
+
"lr",
|
61 |
+
scheduler.get_last_lr()[0],
|
62 |
+
on_epoch=True,
|
63 |
+
prog_bar=True,
|
64 |
+
sync_dist=True,
|
65 |
+
)
|
66 |
+
self.log(
|
67 |
+
f"top_{self.top_k}_acc",
|
68 |
+
acc,
|
69 |
+
on_step=True,
|
70 |
+
on_epoch=True,
|
71 |
+
prog_bar=True,
|
72 |
+
sync_dist=True,
|
73 |
+
)
|
74 |
+
|
75 |
+
def validation_step(self, batch: Dict, batch_idx: int):
|
76 |
+
return
|
77 |
+
|
78 |
+
# # get loss
|
79 |
+
# loss, acc = self.model.forward(
|
80 |
+
# batch['phoneme_ids'], batch['phoneme_ids_len'],
|
81 |
+
# batch['semantic_ids'], batch['semantic_ids_len'],
|
82 |
+
# batch['bert_feature']
|
83 |
+
# )
|
84 |
+
#
|
85 |
+
# self.log(
|
86 |
+
# "val_total_loss",
|
87 |
+
# loss,
|
88 |
+
# on_step=True,
|
89 |
+
# on_epoch=True,
|
90 |
+
# prog_bar=True,
|
91 |
+
# sync_dist=True)
|
92 |
+
# self.log(
|
93 |
+
# f"val_top_{self.top_k}_acc",
|
94 |
+
# acc,
|
95 |
+
# on_step=True,
|
96 |
+
# on_epoch=True,
|
97 |
+
# prog_bar=True,
|
98 |
+
# sync_dist=True)
|
99 |
+
#
|
100 |
+
# # get infer output
|
101 |
+
# semantic_len = batch['semantic_ids'].size(1)
|
102 |
+
# prompt_len = min(int(semantic_len * 0.5), 150)
|
103 |
+
# prompt = batch['semantic_ids'][:, :prompt_len]
|
104 |
+
# pred_semantic = self.model.infer(batch['phoneme_ids'],
|
105 |
+
# batch['phoneme_ids_len'], prompt,
|
106 |
+
# batch['bert_feature']
|
107 |
+
# )
|
108 |
+
# save_name = f'semantic_toks_{batch_idx}.pt'
|
109 |
+
# save_path = os.path.join(self.eval_dir, save_name)
|
110 |
+
# torch.save(pred_semantic.detach().cpu(), save_path)
|
111 |
+
|
112 |
+
def configure_optimizers(self):
|
113 |
+
model_parameters = self.model.parameters()
|
114 |
+
parameters_names = []
|
115 |
+
parameters_names.append(
|
116 |
+
[name_param_pair[0] for name_param_pair in self.model.named_parameters()]
|
117 |
+
)
|
118 |
+
lm_opt = ScaledAdam(
|
119 |
+
model_parameters,
|
120 |
+
lr=0.01,
|
121 |
+
betas=(0.9, 0.95),
|
122 |
+
clipping_scale=2.0,
|
123 |
+
parameters_names=parameters_names,
|
124 |
+
show_dominant_parameters=False,
|
125 |
+
clipping_update_period=1000,
|
126 |
+
)
|
127 |
+
|
128 |
+
return {
|
129 |
+
"optimizer": lm_opt,
|
130 |
+
"lr_scheduler": {
|
131 |
+
"scheduler": WarmupCosineLRSchedule(
|
132 |
+
lm_opt,
|
133 |
+
init_lr=self.config["optimizer"]["lr_init"],
|
134 |
+
peak_lr=self.config["optimizer"]["lr"],
|
135 |
+
end_lr=self.config["optimizer"]["lr_end"],
|
136 |
+
warmup_steps=self.config["optimizer"]["warmup_steps"],
|
137 |
+
total_steps=self.config["optimizer"]["decay_steps"],
|
138 |
+
)
|
139 |
+
},
|
140 |
+
}
|
t2s_model.py
ADDED
@@ -0,0 +1,445 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# modified from https://github.com/feng-yufei/shared_debugging_code/blob/main/model/t2s_model.py
|
2 |
+
import torch
|
3 |
+
from tqdm import tqdm
|
4 |
+
|
5 |
+
from .model_utils import make_pad_mask
|
6 |
+
from .model_utils import (
|
7 |
+
topk_sampling,
|
8 |
+
sample,
|
9 |
+
dpo_loss,
|
10 |
+
make_reject_y,
|
11 |
+
get_batch_logps
|
12 |
+
)
|
13 |
+
from .embedding import SinePositionalEmbedding
|
14 |
+
from .embedding import TokenEmbedding
|
15 |
+
from.transformer import LayerNorm
|
16 |
+
from .transformer import TransformerEncoder
|
17 |
+
from .transformer import TransformerEncoderLayer
|
18 |
+
from torch import nn
|
19 |
+
from torch.nn import functional as F
|
20 |
+
from torchmetrics.classification import MulticlassAccuracy
|
21 |
+
|
22 |
+
default_config = {
|
23 |
+
"embedding_dim": 512,
|
24 |
+
"hidden_dim": 512,
|
25 |
+
"num_head": 8,
|
26 |
+
"num_layers": 12,
|
27 |
+
"num_codebook": 8,
|
28 |
+
"p_dropout": 0.0,
|
29 |
+
"vocab_size": 1024 + 1,
|
30 |
+
"phoneme_vocab_size": 512,
|
31 |
+
"EOS": 1024,
|
32 |
+
}
|
33 |
+
|
34 |
+
|
35 |
+
class Text2SemanticDecoder(nn.Module):
|
36 |
+
def __init__(self, config, norm_first=False, top_k=3):
|
37 |
+
super(Text2SemanticDecoder, self).__init__()
|
38 |
+
self.model_dim = config["model"]["hidden_dim"]
|
39 |
+
self.embedding_dim = config["model"]["embedding_dim"]
|
40 |
+
self.num_head = config["model"]["head"]
|
41 |
+
self.num_layers = config["model"]["n_layer"]
|
42 |
+
self.norm_first = norm_first
|
43 |
+
self.vocab_size = config["model"]["vocab_size"]
|
44 |
+
self.phoneme_vocab_size = config["model"]["phoneme_vocab_size"]
|
45 |
+
self.p_dropout = config["model"]["dropout"]
|
46 |
+
self.EOS = config["model"]["EOS"]
|
47 |
+
self.norm_first = norm_first
|
48 |
+
assert self.EOS == self.vocab_size - 1
|
49 |
+
# should be same as num of kmeans bin
|
50 |
+
# assert self.EOS == 1024
|
51 |
+
self.bert_proj = nn.Linear(1024, self.embedding_dim)
|
52 |
+
self.ar_text_embedding = TokenEmbedding(
|
53 |
+
self.embedding_dim, self.phoneme_vocab_size, self.p_dropout
|
54 |
+
)
|
55 |
+
self.ar_text_position = SinePositionalEmbedding(
|
56 |
+
self.embedding_dim, dropout=0.1, scale=False, alpha=True
|
57 |
+
)
|
58 |
+
self.ar_audio_embedding = TokenEmbedding(
|
59 |
+
self.embedding_dim, self.vocab_size, self.p_dropout
|
60 |
+
)
|
61 |
+
self.ar_audio_position = SinePositionalEmbedding(
|
62 |
+
self.embedding_dim, dropout=0.1, scale=False, alpha=True
|
63 |
+
)
|
64 |
+
|
65 |
+
self.h = TransformerEncoder(
|
66 |
+
TransformerEncoderLayer(
|
67 |
+
d_model=self.model_dim,
|
68 |
+
nhead=self.num_head,
|
69 |
+
dim_feedforward=self.model_dim * 4,
|
70 |
+
dropout=0.1,
|
71 |
+
batch_first=True,
|
72 |
+
norm_first=norm_first,
|
73 |
+
),
|
74 |
+
num_layers=self.num_layers,
|
75 |
+
norm=LayerNorm(self.model_dim) if norm_first else None,
|
76 |
+
)
|
77 |
+
|
78 |
+
self.ar_predict_layer = nn.Linear(self.model_dim, self.vocab_size, bias=False)
|
79 |
+
self.loss_fct = nn.CrossEntropyLoss(reduction="sum")
|
80 |
+
|
81 |
+
self.ar_accuracy_metric = MulticlassAccuracy(
|
82 |
+
self.vocab_size,
|
83 |
+
top_k=top_k,
|
84 |
+
average="micro",
|
85 |
+
multidim_average="global",
|
86 |
+
ignore_index=self.EOS,
|
87 |
+
)
|
88 |
+
|
89 |
+
def make_input_data(self, x, x_lens, y, y_lens, bert_feature):
|
90 |
+
x = self.ar_text_embedding(x)
|
91 |
+
x = x + self.bert_proj(bert_feature.transpose(1, 2))
|
92 |
+
x = self.ar_text_position(x)
|
93 |
+
x_mask = make_pad_mask(x_lens)
|
94 |
+
|
95 |
+
y_mask = make_pad_mask(y_lens)
|
96 |
+
y_mask_int = y_mask.type(torch.int64)
|
97 |
+
codes = y.type(torch.int64) * (1 - y_mask_int)
|
98 |
+
|
99 |
+
# Training
|
100 |
+
# AR Decoder
|
101 |
+
y, targets = self.pad_y_eos(codes, y_mask_int, eos_id=self.EOS)
|
102 |
+
x_len = x_lens.max()
|
103 |
+
y_len = y_lens.max()
|
104 |
+
y_emb = self.ar_audio_embedding(y)
|
105 |
+
y_pos = self.ar_audio_position(y_emb)
|
106 |
+
|
107 |
+
xy_padding_mask = torch.concat([x_mask, y_mask], dim=1)
|
108 |
+
|
109 |
+
ar_xy_padding_mask = xy_padding_mask
|
110 |
+
|
111 |
+
x_attn_mask = F.pad(
|
112 |
+
torch.zeros((x_len, x_len), dtype=torch.bool, device=x.device),
|
113 |
+
(0, y_len),
|
114 |
+
value=True,
|
115 |
+
)
|
116 |
+
|
117 |
+
y_attn_mask = F.pad(
|
118 |
+
torch.triu(
|
119 |
+
torch.ones(y_len, y_len, dtype=torch.bool, device=x.device),
|
120 |
+
diagonal=1,
|
121 |
+
),
|
122 |
+
(x_len, 0),
|
123 |
+
value=False,
|
124 |
+
)
|
125 |
+
|
126 |
+
xy_attn_mask = torch.concat([x_attn_mask, y_attn_mask], dim=0)
|
127 |
+
bsz, src_len = x.shape[0], x_len + y_len
|
128 |
+
_xy_padding_mask = (
|
129 |
+
ar_xy_padding_mask.view(bsz, 1, 1, src_len)
|
130 |
+
.expand(-1, self.num_head, -1, -1)
|
131 |
+
.reshape(bsz * self.num_head, 1, src_len)
|
132 |
+
)
|
133 |
+
xy_attn_mask = xy_attn_mask.logical_or(_xy_padding_mask)
|
134 |
+
new_attn_mask = torch.zeros_like(xy_attn_mask, dtype=x.dtype)
|
135 |
+
new_attn_mask.masked_fill_(xy_attn_mask, float("-inf"))
|
136 |
+
xy_attn_mask = new_attn_mask
|
137 |
+
# x 和完整的 y 一次性输入模型
|
138 |
+
xy_pos = torch.concat([x, y_pos], dim=1)
|
139 |
+
|
140 |
+
return xy_pos, xy_attn_mask, targets
|
141 |
+
|
142 |
+
def forward(self, x, x_lens, y, y_lens, bert_feature):
|
143 |
+
"""
|
144 |
+
x: phoneme_ids
|
145 |
+
y: semantic_ids
|
146 |
+
"""
|
147 |
+
|
148 |
+
reject_y, reject_y_lens = make_reject_y(y, y_lens)
|
149 |
+
|
150 |
+
xy_pos, xy_attn_mask, targets = self.make_input_data(x, x_lens, y, y_lens, bert_feature)
|
151 |
+
|
152 |
+
xy_dec, _ = self.h(
|
153 |
+
(xy_pos, None),
|
154 |
+
mask=xy_attn_mask,
|
155 |
+
)
|
156 |
+
x_len = x_lens.max()
|
157 |
+
logits = self.ar_predict_layer(xy_dec[:, x_len:])
|
158 |
+
|
159 |
+
###### DPO #############
|
160 |
+
reject_xy_pos, reject_xy_attn_mask, reject_targets = self.make_input_data(x, x_lens, reject_y, reject_y_lens, bert_feature)
|
161 |
+
|
162 |
+
reject_xy_dec, _ = self.h(
|
163 |
+
(reject_xy_pos, None),
|
164 |
+
mask=reject_xy_attn_mask,
|
165 |
+
)
|
166 |
+
x_len = x_lens.max()
|
167 |
+
reject_logits = self.ar_predict_layer(reject_xy_dec[:, x_len:])
|
168 |
+
|
169 |
+
# loss
|
170 |
+
# from feiteng: 每次 duration 越多, 梯度更新也应该更多, 所以用 sum
|
171 |
+
|
172 |
+
loss_1 = F.cross_entropy(logits.permute(0, 2, 1), targets, reduction="sum")
|
173 |
+
acc = self.ar_accuracy_metric(logits.permute(0, 2, 1).detach(), targets).item()
|
174 |
+
|
175 |
+
A_logits, R_logits = get_batch_logps(logits, reject_logits, targets, reject_targets)
|
176 |
+
loss_2, _, _ = dpo_loss(A_logits, R_logits, 0, 0, 0.2, reference_free=True)
|
177 |
+
|
178 |
+
loss = loss_1 + loss_2
|
179 |
+
|
180 |
+
return loss, acc
|
181 |
+
|
182 |
+
def forward_old(self, x, x_lens, y, y_lens, bert_feature):
|
183 |
+
"""
|
184 |
+
x: phoneme_ids
|
185 |
+
y: semantic_ids
|
186 |
+
"""
|
187 |
+
x = self.ar_text_embedding(x)
|
188 |
+
x = x + self.bert_proj(bert_feature.transpose(1, 2))
|
189 |
+
x = self.ar_text_position(x)
|
190 |
+
x_mask = make_pad_mask(x_lens)
|
191 |
+
|
192 |
+
y_mask = make_pad_mask(y_lens)
|
193 |
+
y_mask_int = y_mask.type(torch.int64)
|
194 |
+
codes = y.type(torch.int64) * (1 - y_mask_int)
|
195 |
+
|
196 |
+
# Training
|
197 |
+
# AR Decoder
|
198 |
+
y, targets = self.pad_y_eos(codes, y_mask_int, eos_id=self.EOS)
|
199 |
+
x_len = x_lens.max()
|
200 |
+
y_len = y_lens.max()
|
201 |
+
y_emb = self.ar_audio_embedding(y)
|
202 |
+
y_pos = self.ar_audio_position(y_emb)
|
203 |
+
|
204 |
+
xy_padding_mask = torch.concat([x_mask, y_mask], dim=1)
|
205 |
+
ar_xy_padding_mask = xy_padding_mask
|
206 |
+
|
207 |
+
x_attn_mask = F.pad(
|
208 |
+
torch.zeros((x_len, x_len), dtype=torch.bool, device=x.device),
|
209 |
+
(0, y_len),
|
210 |
+
value=True,
|
211 |
+
)
|
212 |
+
y_attn_mask = F.pad(
|
213 |
+
torch.triu(
|
214 |
+
torch.ones(y_len, y_len, dtype=torch.bool, device=x.device),
|
215 |
+
diagonal=1,
|
216 |
+
),
|
217 |
+
(x_len, 0),
|
218 |
+
value=False,
|
219 |
+
)
|
220 |
+
xy_attn_mask = torch.concat([x_attn_mask, y_attn_mask], dim=0)
|
221 |
+
bsz, src_len = x.shape[0], x_len + y_len
|
222 |
+
_xy_padding_mask = (
|
223 |
+
ar_xy_padding_mask.view(bsz, 1, 1, src_len)
|
224 |
+
.expand(-1, self.num_head, -1, -1)
|
225 |
+
.reshape(bsz * self.num_head, 1, src_len)
|
226 |
+
)
|
227 |
+
xy_attn_mask = xy_attn_mask.logical_or(_xy_padding_mask)
|
228 |
+
new_attn_mask = torch.zeros_like(xy_attn_mask, dtype=x.dtype)
|
229 |
+
new_attn_mask.masked_fill_(xy_attn_mask, float("-inf"))
|
230 |
+
xy_attn_mask = new_attn_mask
|
231 |
+
# x 和完整的 y 一次性输入模型
|
232 |
+
xy_pos = torch.concat([x, y_pos], dim=1)
|
233 |
+
xy_dec, _ = self.h(
|
234 |
+
(xy_pos, None),
|
235 |
+
mask=xy_attn_mask,
|
236 |
+
)
|
237 |
+
logits = self.ar_predict_layer(xy_dec[:, x_len:]).permute(0, 2, 1)
|
238 |
+
# loss
|
239 |
+
# from feiteng: 每次 duration 越多, 梯度更新也应该更多, 所以用 sum
|
240 |
+
loss = F.cross_entropy(logits, targets, reduction="sum")
|
241 |
+
acc = self.ar_accuracy_metric(logits.detach(), targets).item()
|
242 |
+
return loss, acc
|
243 |
+
|
244 |
+
# 需要看下这个函数和 forward 的区别以及没有 semantic 的时候 prompts 输入什么
|
245 |
+
def infer(
|
246 |
+
self,
|
247 |
+
x,
|
248 |
+
x_lens,
|
249 |
+
prompts,
|
250 |
+
bert_feature,
|
251 |
+
top_k: int = -100,
|
252 |
+
early_stop_num: int = -1,
|
253 |
+
temperature: float = 1.0,
|
254 |
+
):
|
255 |
+
x = self.ar_text_embedding(x)
|
256 |
+
x = x + self.bert_proj(bert_feature.transpose(1, 2))
|
257 |
+
x = self.ar_text_position(x)
|
258 |
+
|
259 |
+
# AR Decoder
|
260 |
+
y = prompts
|
261 |
+
prefix_len = y.shape[1]
|
262 |
+
x_len = x.shape[1]
|
263 |
+
x_attn_mask = torch.zeros((x_len, x_len), dtype=torch.bool)
|
264 |
+
stop = False
|
265 |
+
for _ in tqdm(range(1500)):
|
266 |
+
y_emb = self.ar_audio_embedding(y)
|
267 |
+
y_pos = self.ar_audio_position(y_emb)
|
268 |
+
# x 和逐渐增长的 y 一起输入给模型
|
269 |
+
xy_pos = torch.concat([x, y_pos], dim=1)
|
270 |
+
y_len = y.shape[1]
|
271 |
+
x_attn_mask_pad = F.pad(
|
272 |
+
x_attn_mask,
|
273 |
+
(0, y_len),
|
274 |
+
value=True,
|
275 |
+
)
|
276 |
+
y_attn_mask = F.pad(
|
277 |
+
torch.triu(torch.ones(y_len, y_len, dtype=torch.bool), diagonal=1),
|
278 |
+
(x_len, 0),
|
279 |
+
value=False,
|
280 |
+
)
|
281 |
+
xy_attn_mask = torch.concat([x_attn_mask_pad, y_attn_mask], dim=0).to(
|
282 |
+
y.device
|
283 |
+
)
|
284 |
+
|
285 |
+
xy_dec, _ = self.h(
|
286 |
+
(xy_pos, None),
|
287 |
+
mask=xy_attn_mask,
|
288 |
+
)
|
289 |
+
logits = self.ar_predict_layer(xy_dec[:, -1])
|
290 |
+
samples = topk_sampling(
|
291 |
+
logits, top_k=top_k, top_p=1.0, temperature=temperature
|
292 |
+
)
|
293 |
+
|
294 |
+
if early_stop_num != -1 and (y.shape[1] - prefix_len) > early_stop_num:
|
295 |
+
print("use early stop num:", early_stop_num)
|
296 |
+
stop = True
|
297 |
+
|
298 |
+
if torch.argmax(logits, dim=-1)[0] == self.EOS or samples[0, 0] == self.EOS:
|
299 |
+
# print(torch.argmax(logits, dim=-1)[0] == self.EOS, samples[0, 0] == self.EOS)
|
300 |
+
stop = True
|
301 |
+
if stop:
|
302 |
+
if prompts.shape[1] == y.shape[1]:
|
303 |
+
y = torch.concat([y, torch.zeros_like(samples)], dim=1)
|
304 |
+
print("bad zero prediction")
|
305 |
+
print(f"T2S Decoding EOS [{prefix_len} -> {y.shape[1]}]")
|
306 |
+
break
|
307 |
+
# 本次生成的 semantic_ids 和之前的 y 构成新的 y
|
308 |
+
# print(samples.shape)#[1,1]#第一个1是bs
|
309 |
+
# import os
|
310 |
+
# os._exit(2333)
|
311 |
+
y = torch.concat([y, samples], dim=1)
|
312 |
+
return y
|
313 |
+
|
314 |
+
def pad_y_eos(self, y, y_mask_int, eos_id):
|
315 |
+
targets = F.pad(y, (0, 1), value=0) + eos_id * F.pad(
|
316 |
+
y_mask_int, (0, 1), value=1
|
317 |
+
)
|
318 |
+
# 错位
|
319 |
+
return targets[:, :-1], targets[:, 1:]
|
320 |
+
|
321 |
+
def infer_panel(
|
322 |
+
self,
|
323 |
+
x, #####全部文本token
|
324 |
+
x_lens,
|
325 |
+
prompts, ####参考音频token
|
326 |
+
bert_feature,
|
327 |
+
top_k: int = -100,
|
328 |
+
top_p: int = 100,
|
329 |
+
early_stop_num: int = -1,
|
330 |
+
temperature: float = 1.0,
|
331 |
+
):
|
332 |
+
x = self.ar_text_embedding(x)
|
333 |
+
x = x + self.bert_proj(bert_feature.transpose(1, 2))
|
334 |
+
x = self.ar_text_position(x)
|
335 |
+
|
336 |
+
# AR Decoder
|
337 |
+
y = prompts
|
338 |
+
|
339 |
+
x_len = x.shape[1]
|
340 |
+
x_attn_mask = torch.zeros((x_len, x_len), dtype=torch.bool)
|
341 |
+
stop = False
|
342 |
+
# print(1111111,self.num_layers)
|
343 |
+
cache = {
|
344 |
+
"all_stage": self.num_layers,
|
345 |
+
"k": [None] * self.num_layers, ###根据配置自己手写
|
346 |
+
"v": [None] * self.num_layers,
|
347 |
+
# "xy_pos":None,##y_pos位置编码每次都不一样的没法缓存,每次都要重新拼xy_pos.主要还是写法原因,其实是可以历史统一一样的,但也没啥计算量就不管了
|
348 |
+
"y_emb": None, ##只需要对最新的samples求emb,再拼历史的就行
|
349 |
+
# "logits":None,###原版就已经只对结尾求再拼接了,不用管
|
350 |
+
# "xy_dec":None,###不需要,本来只需要最后一个做logits
|
351 |
+
"first_infer": 1,
|
352 |
+
"stage": 0,
|
353 |
+
}
|
354 |
+
################### first step ##########################
|
355 |
+
if y is not None:
|
356 |
+
y_emb = self.ar_audio_embedding(y)
|
357 |
+
y_len = y_emb.shape[1]
|
358 |
+
prefix_len = y.shape[1]
|
359 |
+
y_pos = self.ar_audio_position(y_emb)
|
360 |
+
xy_pos = torch.concat([x, y_pos], dim=1)
|
361 |
+
cache["y_emb"] = y_emb
|
362 |
+
ref_free = False
|
363 |
+
else:
|
364 |
+
y_emb = None
|
365 |
+
y_len = 0
|
366 |
+
prefix_len = 0
|
367 |
+
y_pos = None
|
368 |
+
xy_pos = x
|
369 |
+
y = torch.zeros(x.shape[0], 0, dtype=torch.int, device=x.device)
|
370 |
+
ref_free = True
|
371 |
+
|
372 |
+
x_attn_mask_pad = F.pad(
|
373 |
+
x_attn_mask,
|
374 |
+
(0, y_len), ###xx的纯0扩展到xx纯0+xy纯1,(x,x+y)
|
375 |
+
value=True,
|
376 |
+
)
|
377 |
+
y_attn_mask = F.pad( ###yy的右上1扩展到左边xy的0,(y,x+y)
|
378 |
+
torch.triu(torch.ones(y_len, y_len, dtype=torch.bool), diagonal=1),
|
379 |
+
(x_len, 0),
|
380 |
+
value=False,
|
381 |
+
)
|
382 |
+
xy_attn_mask = torch.concat([x_attn_mask_pad, y_attn_mask], dim=0).to(
|
383 |
+
x.device
|
384 |
+
)
|
385 |
+
|
386 |
+
|
387 |
+
for idx in tqdm(range(1500)):
|
388 |
+
|
389 |
+
xy_dec, _ = self.h((xy_pos, None), mask=xy_attn_mask, cache=cache)
|
390 |
+
logits = self.ar_predict_layer(
|
391 |
+
xy_dec[:, -1]
|
392 |
+
) ##不用改,如果用了cache的默认就是只有一帧,取最后一帧一样的
|
393 |
+
# samples = topk_sampling(logits, top_k=top_k, top_p=1.0, temperature=temperature)
|
394 |
+
if(idx==0):###第一次跑不能EOS否则没有了
|
395 |
+
logits = logits[:, :-1] ###刨除1024终止符号的概率
|
396 |
+
samples = sample(
|
397 |
+
logits[0], y, top_k=top_k, top_p=top_p, repetition_penalty=1.35, temperature=temperature
|
398 |
+
)[0].unsqueeze(0)
|
399 |
+
# 本次生成的 semantic_ids 和之前的 y 构成新的 y
|
400 |
+
# print(samples.shape)#[1,1]#第一个1是bs
|
401 |
+
y = torch.concat([y, samples], dim=1)
|
402 |
+
|
403 |
+
if early_stop_num != -1 and (y.shape[1] - prefix_len) > early_stop_num:
|
404 |
+
print("use early stop num:", early_stop_num)
|
405 |
+
stop = True
|
406 |
+
|
407 |
+
if torch.argmax(logits, dim=-1)[0] == self.EOS or samples[0, 0] == self.EOS:
|
408 |
+
# print(torch.argmax(logits, dim=-1)[0] == self.EOS, samples[0, 0] == self.EOS)
|
409 |
+
stop = True
|
410 |
+
if stop:
|
411 |
+
# if prompts.shape[1] == y.shape[1]:
|
412 |
+
# y = torch.concat([y, torch.zeros_like(samples)], dim=1)
|
413 |
+
# print("bad zero prediction")
|
414 |
+
if y.shape[1]==0:
|
415 |
+
y = torch.concat([y, torch.zeros_like(samples)], dim=1)
|
416 |
+
print("bad zero prediction")
|
417 |
+
print(f"T2S Decoding EOS [{prefix_len} -> {y.shape[1]}]")
|
418 |
+
break
|
419 |
+
|
420 |
+
####################### update next step ###################################
|
421 |
+
cache["first_infer"] = 0
|
422 |
+
if cache["y_emb"] is not None:
|
423 |
+
y_emb = torch.cat(
|
424 |
+
[cache["y_emb"], self.ar_audio_embedding(y[:, -1:])], dim = 1
|
425 |
+
)
|
426 |
+
cache["y_emb"] = y_emb
|
427 |
+
y_pos = self.ar_audio_position(y_emb)
|
428 |
+
xy_pos = y_pos[:, -1:]
|
429 |
+
else:
|
430 |
+
y_emb = self.ar_audio_embedding(y[:, -1:])
|
431 |
+
cache["y_emb"] = y_emb
|
432 |
+
y_pos = self.ar_audio_position(y_emb)
|
433 |
+
xy_pos = y_pos
|
434 |
+
y_len = y_pos.shape[1]
|
435 |
+
|
436 |
+
###最右边一列(是错的)
|
437 |
+
# xy_attn_mask=torch.ones((1, x_len+y_len), dtype=torch.bool,device=xy_pos.device)
|
438 |
+
# xy_attn_mask[:,-1]=False
|
439 |
+
###最下面一行(是对的)
|
440 |
+
xy_attn_mask = torch.zeros(
|
441 |
+
(1, x_len + y_len), dtype=torch.bool, device=xy_pos.device
|
442 |
+
)
|
443 |
+
if ref_free:
|
444 |
+
return y[:, :-1], 0
|
445 |
+
return y[:, :-1], idx-1
|
transformer.py
ADDED
@@ -0,0 +1,378 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# modified from https://github.com/lifeiteng/vall-e/blob/main/valle/modules/transformer.py
|
2 |
+
import copy
|
3 |
+
import numbers
|
4 |
+
from functools import partial
|
5 |
+
from typing import Any
|
6 |
+
from typing import Callable
|
7 |
+
from typing import List
|
8 |
+
from typing import Optional
|
9 |
+
from typing import Tuple
|
10 |
+
from typing import Union
|
11 |
+
|
12 |
+
import torch
|
13 |
+
from .activation import MultiheadAttention
|
14 |
+
from .scaling import BalancedDoubleSwish
|
15 |
+
from torch import nn
|
16 |
+
from torch import Tensor
|
17 |
+
from torch.nn import functional as F
|
18 |
+
|
19 |
+
_shape_t = Union[int, List[int], torch.Size]
|
20 |
+
|
21 |
+
|
22 |
+
class LayerNorm(nn.Module):
|
23 |
+
__constants__ = ["normalized_shape", "eps", "elementwise_affine"]
|
24 |
+
normalized_shape: Tuple[int, ...]
|
25 |
+
eps: float
|
26 |
+
elementwise_affine: bool
|
27 |
+
|
28 |
+
def __init__(
|
29 |
+
self,
|
30 |
+
normalized_shape: _shape_t,
|
31 |
+
eps: float = 1e-5,
|
32 |
+
elementwise_affine: bool = True,
|
33 |
+
device=None,
|
34 |
+
dtype=None,
|
35 |
+
) -> None:
|
36 |
+
factory_kwargs = {"device": device, "dtype": dtype}
|
37 |
+
super(LayerNorm, self).__init__()
|
38 |
+
if isinstance(normalized_shape, numbers.Integral):
|
39 |
+
# mypy error: incompatible types in assignment
|
40 |
+
normalized_shape = (normalized_shape,) # type: ignore[assignment]
|
41 |
+
self.normalized_shape = tuple(normalized_shape) # type: ignore[arg-type]
|
42 |
+
self.eps = eps
|
43 |
+
self.elementwise_affine = elementwise_affine
|
44 |
+
if self.elementwise_affine:
|
45 |
+
self.weight = nn.Parameter(
|
46 |
+
torch.empty(self.normalized_shape, **factory_kwargs)
|
47 |
+
)
|
48 |
+
self.bias = nn.Parameter(
|
49 |
+
torch.empty(self.normalized_shape, **factory_kwargs)
|
50 |
+
)
|
51 |
+
else:
|
52 |
+
self.register_parameter("weight", None)
|
53 |
+
self.register_parameter("bias", None)
|
54 |
+
|
55 |
+
self.reset_parameters()
|
56 |
+
|
57 |
+
def reset_parameters(self) -> None:
|
58 |
+
if self.elementwise_affine:
|
59 |
+
nn.init.ones_(self.weight)
|
60 |
+
nn.init.zeros_(self.bias)
|
61 |
+
|
62 |
+
def forward(self, input: Tensor, embedding: Any = None) -> Tensor:
|
63 |
+
if isinstance(input, tuple):
|
64 |
+
input, embedding = input
|
65 |
+
return (
|
66 |
+
F.layer_norm(
|
67 |
+
input,
|
68 |
+
self.normalized_shape,
|
69 |
+
self.weight,
|
70 |
+
self.bias,
|
71 |
+
self.eps,
|
72 |
+
),
|
73 |
+
embedding,
|
74 |
+
)
|
75 |
+
|
76 |
+
assert embedding is None
|
77 |
+
return F.layer_norm(
|
78 |
+
input, self.normalized_shape, self.weight, self.bias, self.eps
|
79 |
+
)
|
80 |
+
|
81 |
+
def extra_repr(self) -> str:
|
82 |
+
return (
|
83 |
+
"{normalized_shape}, eps={eps}, "
|
84 |
+
"elementwise_affine={elementwise_affine}".format(**self.__dict__)
|
85 |
+
)
|
86 |
+
|
87 |
+
|
88 |
+
class IdentityNorm(nn.Module):
|
89 |
+
def __init__(
|
90 |
+
self,
|
91 |
+
d_model: int,
|
92 |
+
eps: float = 1e-5,
|
93 |
+
device=None,
|
94 |
+
dtype=None,
|
95 |
+
) -> None:
|
96 |
+
super(IdentityNorm, self).__init__()
|
97 |
+
|
98 |
+
def forward(self, input: Tensor, embedding: Any = None) -> Tensor:
|
99 |
+
if isinstance(input, tuple):
|
100 |
+
return input
|
101 |
+
|
102 |
+
assert embedding is None
|
103 |
+
return input
|
104 |
+
|
105 |
+
|
106 |
+
class TransformerEncoder(nn.Module):
|
107 |
+
r"""TransformerEncoder is a stack of N encoder layers. Users can build the
|
108 |
+
BERT(https://arxiv.org/abs/1810.04805) model with corresponding parameters.
|
109 |
+
|
110 |
+
Args:
|
111 |
+
encoder_layer: an instance of the TransformerEncoderLayer() class (required).
|
112 |
+
num_layers: the number of sub-encoder-layers in the encoder (required).
|
113 |
+
norm: the layer normalization component (optional).
|
114 |
+
enable_nested_tensor: if True, input will automatically convert to nested tensor
|
115 |
+
(and convert back on output). This will improve the overall performance of
|
116 |
+
TransformerEncoder when padding rate is high. Default: ``True`` (enabled).
|
117 |
+
|
118 |
+
Examples::
|
119 |
+
>>> encoder_layer = TransformerEncoderLayer(d_model=512, nhead=8)
|
120 |
+
>>> transformer_encoder = TransformerEncoder(encoder_layer, num_layers=6)
|
121 |
+
>>> src = torch.rand(10, 32, 512)
|
122 |
+
>>> out = transformer_encoder(src)
|
123 |
+
"""
|
124 |
+
__constants__ = ["norm"]
|
125 |
+
|
126 |
+
def __init__(self, encoder_layer, num_layers, norm=None):
|
127 |
+
super(TransformerEncoder, self).__init__()
|
128 |
+
self.layers = _get_clones(encoder_layer, num_layers)
|
129 |
+
self.num_layers = num_layers
|
130 |
+
self.norm = norm
|
131 |
+
|
132 |
+
def forward(
|
133 |
+
self,
|
134 |
+
src: Tensor,
|
135 |
+
mask: Optional[Tensor] = None,
|
136 |
+
src_key_padding_mask: Optional[Tensor] = None,
|
137 |
+
return_layer_states: bool = False,
|
138 |
+
cache=None,
|
139 |
+
) -> Tensor:
|
140 |
+
r"""Pass the input through the encoder layers in turn.
|
141 |
+
|
142 |
+
Args:
|
143 |
+
src: the sequence to the encoder (required).
|
144 |
+
mask: the mask for the src sequence (optional).
|
145 |
+
src_key_padding_mask: the mask for the src keys per batch (optional).
|
146 |
+
return_layer_states: return layers' state (optional).
|
147 |
+
|
148 |
+
Shape:
|
149 |
+
see the docs in Transformer class.
|
150 |
+
"""
|
151 |
+
if return_layer_states:
|
152 |
+
layer_states = [] # layers' output
|
153 |
+
output = src
|
154 |
+
for mod in self.layers:
|
155 |
+
output = mod(
|
156 |
+
output,
|
157 |
+
src_mask=mask,
|
158 |
+
src_key_padding_mask=src_key_padding_mask,
|
159 |
+
cache=cache,
|
160 |
+
)
|
161 |
+
layer_states.append(output[0])
|
162 |
+
|
163 |
+
if self.norm is not None:
|
164 |
+
output = self.norm(output)
|
165 |
+
|
166 |
+
return layer_states, output
|
167 |
+
|
168 |
+
output = src
|
169 |
+
for mod in self.layers:
|
170 |
+
output = mod(
|
171 |
+
output,
|
172 |
+
src_mask=mask,
|
173 |
+
src_key_padding_mask=src_key_padding_mask,
|
174 |
+
cache=cache,
|
175 |
+
)
|
176 |
+
|
177 |
+
if self.norm is not None:
|
178 |
+
output = self.norm(output)
|
179 |
+
|
180 |
+
return output
|
181 |
+
|
182 |
+
|
183 |
+
class TransformerEncoderLayer(nn.Module):
|
184 |
+
__constants__ = ["batch_first", "norm_first"]
|
185 |
+
|
186 |
+
def __init__(
|
187 |
+
self,
|
188 |
+
d_model: int,
|
189 |
+
nhead: int,
|
190 |
+
dim_feedforward: int = 2048,
|
191 |
+
dropout: float = 0.1,
|
192 |
+
activation: Union[str, Callable[[Tensor], Tensor]] = F.relu,
|
193 |
+
batch_first: bool = False,
|
194 |
+
norm_first: bool = False,
|
195 |
+
device=None,
|
196 |
+
dtype=None,
|
197 |
+
linear1_self_attention_cls: nn.Module = nn.Linear,
|
198 |
+
linear2_self_attention_cls: nn.Module = nn.Linear,
|
199 |
+
linear1_feedforward_cls: nn.Module = nn.Linear,
|
200 |
+
linear2_feedforward_cls: nn.Module = nn.Linear,
|
201 |
+
layer_norm_cls: nn.Module = LayerNorm,
|
202 |
+
layer_norm_eps: float = 1e-5,
|
203 |
+
adaptive_layer_norm=False,
|
204 |
+
) -> None:
|
205 |
+
factory_kwargs = {"device": device, "dtype": dtype}
|
206 |
+
super(TransformerEncoderLayer, self).__init__()
|
207 |
+
# print(233333333333,d_model,nhead)
|
208 |
+
# import os
|
209 |
+
# os._exit(2333333)
|
210 |
+
self.self_attn = MultiheadAttention(
|
211 |
+
d_model, # 512 16
|
212 |
+
nhead,
|
213 |
+
dropout=dropout,
|
214 |
+
batch_first=batch_first,
|
215 |
+
linear1_cls=linear1_self_attention_cls,
|
216 |
+
linear2_cls=linear2_self_attention_cls,
|
217 |
+
**factory_kwargs,
|
218 |
+
)
|
219 |
+
|
220 |
+
# Implementation of Feedforward model
|
221 |
+
self.linear1 = linear1_feedforward_cls(
|
222 |
+
d_model, dim_feedforward, **factory_kwargs
|
223 |
+
)
|
224 |
+
self.dropout = nn.Dropout(dropout)
|
225 |
+
self.linear2 = linear2_feedforward_cls(
|
226 |
+
dim_feedforward, d_model, **factory_kwargs
|
227 |
+
)
|
228 |
+
|
229 |
+
self.norm_first = norm_first
|
230 |
+
self.dropout1 = nn.Dropout(dropout)
|
231 |
+
self.dropout2 = nn.Dropout(dropout)
|
232 |
+
|
233 |
+
# Legacy string support for activation function.
|
234 |
+
if isinstance(activation, str):
|
235 |
+
activation = _get_activation_fn(activation)
|
236 |
+
elif isinstance(activation, partial):
|
237 |
+
activation = activation(d_model)
|
238 |
+
elif activation == BalancedDoubleSwish:
|
239 |
+
activation = BalancedDoubleSwish(d_model)
|
240 |
+
|
241 |
+
# # We can't test self.activation in forward() in TorchScript,
|
242 |
+
# # so stash some information about it instead.
|
243 |
+
# if activation is F.relu or isinstance(activation, torch.nn.ReLU):
|
244 |
+
# self.activation_relu_or_gelu = 1
|
245 |
+
# elif activation is F.gelu or isinstance(activation, torch.nn.GELU):
|
246 |
+
# self.activation_relu_or_gelu = 2
|
247 |
+
# else:
|
248 |
+
# self.activation_relu_or_gelu = 0
|
249 |
+
self.activation = activation
|
250 |
+
|
251 |
+
norm1 = layer_norm_cls(d_model, eps=layer_norm_eps, **factory_kwargs)
|
252 |
+
if layer_norm_cls == IdentityNorm:
|
253 |
+
norm2 = BalancedBasicNorm(d_model, eps=layer_norm_eps, **factory_kwargs)
|
254 |
+
else:
|
255 |
+
norm2 = layer_norm_cls(d_model, eps=layer_norm_eps, **factory_kwargs)
|
256 |
+
|
257 |
+
if adaptive_layer_norm:
|
258 |
+
self.norm1 = AdaptiveLayerNorm(d_model, norm1)
|
259 |
+
self.norm2 = AdaptiveLayerNorm(d_model, norm2)
|
260 |
+
else:
|
261 |
+
self.norm1 = norm1
|
262 |
+
self.norm2 = norm2
|
263 |
+
|
264 |
+
def __setstate__(self, state):
|
265 |
+
super(TransformerEncoderLayer, self).__setstate__(state)
|
266 |
+
if not hasattr(self, "activation"):
|
267 |
+
self.activation = F.relu
|
268 |
+
|
269 |
+
def forward(
|
270 |
+
self,
|
271 |
+
src: Tensor,
|
272 |
+
src_mask: Optional[Tensor] = None,
|
273 |
+
src_key_padding_mask: Optional[Tensor] = None,
|
274 |
+
cache=None,
|
275 |
+
) -> Tensor:
|
276 |
+
r"""Pass the input through the encoder layer.
|
277 |
+
|
278 |
+
Args:
|
279 |
+
src: the sequence to the encoder layer (required).
|
280 |
+
src_mask: the mask for the src sequence (optional).
|
281 |
+
src_key_padding_mask: the mask for the src keys per batch (optional).
|
282 |
+
|
283 |
+
Shape:
|
284 |
+
see the docs in Transformer class.
|
285 |
+
"""
|
286 |
+
x, stage_embedding = src, None
|
287 |
+
is_src_tuple = False
|
288 |
+
if isinstance(src, tuple):
|
289 |
+
x, stage_embedding = src
|
290 |
+
is_src_tuple = True
|
291 |
+
|
292 |
+
if src_key_padding_mask is not None:
|
293 |
+
_skpm_dtype = src_key_padding_mask.dtype
|
294 |
+
if _skpm_dtype != torch.bool and not torch.is_floating_point(
|
295 |
+
src_key_padding_mask
|
296 |
+
):
|
297 |
+
raise AssertionError(
|
298 |
+
"only bool and floating types of key_padding_mask are supported"
|
299 |
+
)
|
300 |
+
|
301 |
+
if self.norm_first:
|
302 |
+
x = x + self._sa_block(
|
303 |
+
self.norm1(x, stage_embedding),
|
304 |
+
src_mask,
|
305 |
+
src_key_padding_mask,
|
306 |
+
cache=cache,
|
307 |
+
)
|
308 |
+
x = x + self._ff_block(self.norm2(x, stage_embedding))
|
309 |
+
else:
|
310 |
+
x = self.norm1(
|
311 |
+
x + self._sa_block(x, src_mask, src_key_padding_mask, cache=cache),
|
312 |
+
stage_embedding,
|
313 |
+
)
|
314 |
+
x = self.norm2(x + self._ff_block(x), stage_embedding)
|
315 |
+
|
316 |
+
if is_src_tuple:
|
317 |
+
return (x, stage_embedding)
|
318 |
+
return x
|
319 |
+
|
320 |
+
# self-attention block
|
321 |
+
def _sa_block(
|
322 |
+
self,
|
323 |
+
x: Tensor,
|
324 |
+
attn_mask: Optional[Tensor],
|
325 |
+
key_padding_mask: Optional[Tensor],
|
326 |
+
cache=None,
|
327 |
+
) -> Tensor:
|
328 |
+
# print(x.shape,attn_mask.shape,key_padding_mask)
|
329 |
+
# torch.Size([1, 188, 512]) torch.Size([188, 188]) None
|
330 |
+
# import os
|
331 |
+
# os._exit(23333)
|
332 |
+
x = self.self_attn(
|
333 |
+
x,
|
334 |
+
x,
|
335 |
+
x,
|
336 |
+
attn_mask=attn_mask,
|
337 |
+
key_padding_mask=key_padding_mask,
|
338 |
+
need_weights=False,
|
339 |
+
cache=cache,
|
340 |
+
)[0]
|
341 |
+
return self.dropout1(x)
|
342 |
+
|
343 |
+
# feed forward block
|
344 |
+
def _ff_block(self, x: Tensor) -> Tensor:
|
345 |
+
x = self.linear2(self.dropout(self.activation(self.linear1(x))))
|
346 |
+
return self.dropout2(x)
|
347 |
+
|
348 |
+
|
349 |
+
class AdaptiveLayerNorm(nn.Module):
|
350 |
+
r"""Adaptive Layer Normalization"""
|
351 |
+
|
352 |
+
def __init__(self, d_model, norm) -> None:
|
353 |
+
super(AdaptiveLayerNorm, self).__init__()
|
354 |
+
self.project_layer = nn.Linear(d_model, 2 * d_model)
|
355 |
+
self.norm = norm
|
356 |
+
self.d_model = d_model
|
357 |
+
self.eps = self.norm.eps
|
358 |
+
|
359 |
+
def forward(self, input: Tensor, embedding: Tensor = None) -> Tensor:
|
360 |
+
if isinstance(input, tuple):
|
361 |
+
input, embedding = input
|
362 |
+
weight, bias = torch.split(
|
363 |
+
self.project_layer(embedding),
|
364 |
+
split_size_or_sections=self.d_model,
|
365 |
+
dim=-1,
|
366 |
+
)
|
367 |
+
return (weight * self.norm(input) + bias, embedding)
|
368 |
+
|
369 |
+
weight, bias = torch.split(
|
370 |
+
self.project_layer(embedding),
|
371 |
+
split_size_or_sections=self.d_model,
|
372 |
+
dim=-1,
|
373 |
+
)
|
374 |
+
return weight * self.norm(input) + bias
|
375 |
+
|
376 |
+
|
377 |
+
def _get_clones(module, N):
|
378 |
+
return nn.ModuleList([copy.deepcopy(module) for i in range(N)])
|
transforms.py
ADDED
@@ -0,0 +1,209 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torch.nn import functional as F
|
3 |
+
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
|
7 |
+
DEFAULT_MIN_BIN_WIDTH = 1e-3
|
8 |
+
DEFAULT_MIN_BIN_HEIGHT = 1e-3
|
9 |
+
DEFAULT_MIN_DERIVATIVE = 1e-3
|
10 |
+
|
11 |
+
|
12 |
+
def piecewise_rational_quadratic_transform(
|
13 |
+
inputs,
|
14 |
+
unnormalized_widths,
|
15 |
+
unnormalized_heights,
|
16 |
+
unnormalized_derivatives,
|
17 |
+
inverse=False,
|
18 |
+
tails=None,
|
19 |
+
tail_bound=1.0,
|
20 |
+
min_bin_width=DEFAULT_MIN_BIN_WIDTH,
|
21 |
+
min_bin_height=DEFAULT_MIN_BIN_HEIGHT,
|
22 |
+
min_derivative=DEFAULT_MIN_DERIVATIVE,
|
23 |
+
):
|
24 |
+
if tails is None:
|
25 |
+
spline_fn = rational_quadratic_spline
|
26 |
+
spline_kwargs = {}
|
27 |
+
else:
|
28 |
+
spline_fn = unconstrained_rational_quadratic_spline
|
29 |
+
spline_kwargs = {"tails": tails, "tail_bound": tail_bound}
|
30 |
+
|
31 |
+
outputs, logabsdet = spline_fn(
|
32 |
+
inputs=inputs,
|
33 |
+
unnormalized_widths=unnormalized_widths,
|
34 |
+
unnormalized_heights=unnormalized_heights,
|
35 |
+
unnormalized_derivatives=unnormalized_derivatives,
|
36 |
+
inverse=inverse,
|
37 |
+
min_bin_width=min_bin_width,
|
38 |
+
min_bin_height=min_bin_height,
|
39 |
+
min_derivative=min_derivative,
|
40 |
+
**spline_kwargs
|
41 |
+
)
|
42 |
+
return outputs, logabsdet
|
43 |
+
|
44 |
+
|
45 |
+
def searchsorted(bin_locations, inputs, eps=1e-6):
|
46 |
+
bin_locations[..., -1] += eps
|
47 |
+
return torch.sum(inputs[..., None] >= bin_locations, dim=-1) - 1
|
48 |
+
|
49 |
+
|
50 |
+
def unconstrained_rational_quadratic_spline(
|
51 |
+
inputs,
|
52 |
+
unnormalized_widths,
|
53 |
+
unnormalized_heights,
|
54 |
+
unnormalized_derivatives,
|
55 |
+
inverse=False,
|
56 |
+
tails="linear",
|
57 |
+
tail_bound=1.0,
|
58 |
+
min_bin_width=DEFAULT_MIN_BIN_WIDTH,
|
59 |
+
min_bin_height=DEFAULT_MIN_BIN_HEIGHT,
|
60 |
+
min_derivative=DEFAULT_MIN_DERIVATIVE,
|
61 |
+
):
|
62 |
+
inside_interval_mask = (inputs >= -tail_bound) & (inputs <= tail_bound)
|
63 |
+
outside_interval_mask = ~inside_interval_mask
|
64 |
+
|
65 |
+
outputs = torch.zeros_like(inputs)
|
66 |
+
logabsdet = torch.zeros_like(inputs)
|
67 |
+
|
68 |
+
if tails == "linear":
|
69 |
+
unnormalized_derivatives = F.pad(unnormalized_derivatives, pad=(1, 1))
|
70 |
+
constant = np.log(np.exp(1 - min_derivative) - 1)
|
71 |
+
unnormalized_derivatives[..., 0] = constant
|
72 |
+
unnormalized_derivatives[..., -1] = constant
|
73 |
+
|
74 |
+
outputs[outside_interval_mask] = inputs[outside_interval_mask]
|
75 |
+
logabsdet[outside_interval_mask] = 0
|
76 |
+
else:
|
77 |
+
raise RuntimeError("{} tails are not implemented.".format(tails))
|
78 |
+
|
79 |
+
(
|
80 |
+
outputs[inside_interval_mask],
|
81 |
+
logabsdet[inside_interval_mask],
|
82 |
+
) = rational_quadratic_spline(
|
83 |
+
inputs=inputs[inside_interval_mask],
|
84 |
+
unnormalized_widths=unnormalized_widths[inside_interval_mask, :],
|
85 |
+
unnormalized_heights=unnormalized_heights[inside_interval_mask, :],
|
86 |
+
unnormalized_derivatives=unnormalized_derivatives[inside_interval_mask, :],
|
87 |
+
inverse=inverse,
|
88 |
+
left=-tail_bound,
|
89 |
+
right=tail_bound,
|
90 |
+
bottom=-tail_bound,
|
91 |
+
top=tail_bound,
|
92 |
+
min_bin_width=min_bin_width,
|
93 |
+
min_bin_height=min_bin_height,
|
94 |
+
min_derivative=min_derivative,
|
95 |
+
)
|
96 |
+
|
97 |
+
return outputs, logabsdet
|
98 |
+
|
99 |
+
|
100 |
+
def rational_quadratic_spline(
|
101 |
+
inputs,
|
102 |
+
unnormalized_widths,
|
103 |
+
unnormalized_heights,
|
104 |
+
unnormalized_derivatives,
|
105 |
+
inverse=False,
|
106 |
+
left=0.0,
|
107 |
+
right=1.0,
|
108 |
+
bottom=0.0,
|
109 |
+
top=1.0,
|
110 |
+
min_bin_width=DEFAULT_MIN_BIN_WIDTH,
|
111 |
+
min_bin_height=DEFAULT_MIN_BIN_HEIGHT,
|
112 |
+
min_derivative=DEFAULT_MIN_DERIVATIVE,
|
113 |
+
):
|
114 |
+
if torch.min(inputs) < left or torch.max(inputs) > right:
|
115 |
+
raise ValueError("Input to a transform is not within its domain")
|
116 |
+
|
117 |
+
num_bins = unnormalized_widths.shape[-1]
|
118 |
+
|
119 |
+
if min_bin_width * num_bins > 1.0:
|
120 |
+
raise ValueError("Minimal bin width too large for the number of bins")
|
121 |
+
if min_bin_height * num_bins > 1.0:
|
122 |
+
raise ValueError("Minimal bin height too large for the number of bins")
|
123 |
+
|
124 |
+
widths = F.softmax(unnormalized_widths, dim=-1)
|
125 |
+
widths = min_bin_width + (1 - min_bin_width * num_bins) * widths
|
126 |
+
cumwidths = torch.cumsum(widths, dim=-1)
|
127 |
+
cumwidths = F.pad(cumwidths, pad=(1, 0), mode="constant", value=0.0)
|
128 |
+
cumwidths = (right - left) * cumwidths + left
|
129 |
+
cumwidths[..., 0] = left
|
130 |
+
cumwidths[..., -1] = right
|
131 |
+
widths = cumwidths[..., 1:] - cumwidths[..., :-1]
|
132 |
+
|
133 |
+
derivatives = min_derivative + F.softplus(unnormalized_derivatives)
|
134 |
+
|
135 |
+
heights = F.softmax(unnormalized_heights, dim=-1)
|
136 |
+
heights = min_bin_height + (1 - min_bin_height * num_bins) * heights
|
137 |
+
cumheights = torch.cumsum(heights, dim=-1)
|
138 |
+
cumheights = F.pad(cumheights, pad=(1, 0), mode="constant", value=0.0)
|
139 |
+
cumheights = (top - bottom) * cumheights + bottom
|
140 |
+
cumheights[..., 0] = bottom
|
141 |
+
cumheights[..., -1] = top
|
142 |
+
heights = cumheights[..., 1:] - cumheights[..., :-1]
|
143 |
+
|
144 |
+
if inverse:
|
145 |
+
bin_idx = searchsorted(cumheights, inputs)[..., None]
|
146 |
+
else:
|
147 |
+
bin_idx = searchsorted(cumwidths, inputs)[..., None]
|
148 |
+
|
149 |
+
input_cumwidths = cumwidths.gather(-1, bin_idx)[..., 0]
|
150 |
+
input_bin_widths = widths.gather(-1, bin_idx)[..., 0]
|
151 |
+
|
152 |
+
input_cumheights = cumheights.gather(-1, bin_idx)[..., 0]
|
153 |
+
delta = heights / widths
|
154 |
+
input_delta = delta.gather(-1, bin_idx)[..., 0]
|
155 |
+
|
156 |
+
input_derivatives = derivatives.gather(-1, bin_idx)[..., 0]
|
157 |
+
input_derivatives_plus_one = derivatives[..., 1:].gather(-1, bin_idx)[..., 0]
|
158 |
+
|
159 |
+
input_heights = heights.gather(-1, bin_idx)[..., 0]
|
160 |
+
|
161 |
+
if inverse:
|
162 |
+
a = (inputs - input_cumheights) * (
|
163 |
+
input_derivatives + input_derivatives_plus_one - 2 * input_delta
|
164 |
+
) + input_heights * (input_delta - input_derivatives)
|
165 |
+
b = input_heights * input_derivatives - (inputs - input_cumheights) * (
|
166 |
+
input_derivatives + input_derivatives_plus_one - 2 * input_delta
|
167 |
+
)
|
168 |
+
c = -input_delta * (inputs - input_cumheights)
|
169 |
+
|
170 |
+
discriminant = b.pow(2) - 4 * a * c
|
171 |
+
assert (discriminant >= 0).all()
|
172 |
+
|
173 |
+
root = (2 * c) / (-b - torch.sqrt(discriminant))
|
174 |
+
outputs = root * input_bin_widths + input_cumwidths
|
175 |
+
|
176 |
+
theta_one_minus_theta = root * (1 - root)
|
177 |
+
denominator = input_delta + (
|
178 |
+
(input_derivatives + input_derivatives_plus_one - 2 * input_delta)
|
179 |
+
* theta_one_minus_theta
|
180 |
+
)
|
181 |
+
derivative_numerator = input_delta.pow(2) * (
|
182 |
+
input_derivatives_plus_one * root.pow(2)
|
183 |
+
+ 2 * input_delta * theta_one_minus_theta
|
184 |
+
+ input_derivatives * (1 - root).pow(2)
|
185 |
+
)
|
186 |
+
logabsdet = torch.log(derivative_numerator) - 2 * torch.log(denominator)
|
187 |
+
|
188 |
+
return outputs, -logabsdet
|
189 |
+
else:
|
190 |
+
theta = (inputs - input_cumwidths) / input_bin_widths
|
191 |
+
theta_one_minus_theta = theta * (1 - theta)
|
192 |
+
|
193 |
+
numerator = input_heights * (
|
194 |
+
input_delta * theta.pow(2) + input_derivatives * theta_one_minus_theta
|
195 |
+
)
|
196 |
+
denominator = input_delta + (
|
197 |
+
(input_derivatives + input_derivatives_plus_one - 2 * input_delta)
|
198 |
+
* theta_one_minus_theta
|
199 |
+
)
|
200 |
+
outputs = input_cumheights + numerator / denominator
|
201 |
+
|
202 |
+
derivative_numerator = input_delta.pow(2) * (
|
203 |
+
input_derivatives_plus_one * theta.pow(2)
|
204 |
+
+ 2 * input_delta * theta_one_minus_theta
|
205 |
+
+ input_derivatives * (1 - theta).pow(2)
|
206 |
+
)
|
207 |
+
logabsdet = torch.log(derivative_numerator) - 2 * torch.log(denominator)
|
208 |
+
|
209 |
+
return outputs, logabsdet
|