Spaces:
Running
on
Zero
Running
on
Zero
chong.zhang
commited on
Commit
·
96fe5d9
1
Parent(s):
43dbb02
update
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- app.py +205 -263
- inspiremusic/__init__.py +0 -0
- inspiremusic/bin/export_jit.py +74 -0
- inspiremusic/bin/export_onnx.py +112 -0
- inspiremusic/bin/flow_only_infer.py +150 -0
- inspiremusic/bin/inference.py +266 -0
- inspiremusic/bin/train.py +194 -0
- inspiremusic/cli/__init__.py +0 -0
- inspiremusic/cli/frontend.py +106 -0
- inspiremusic/cli/inference.py +296 -0
- inspiremusic/cli/inspiremusic.py +133 -0
- inspiremusic/cli/model.py +297 -0
- inspiremusic/dataset/__init__.py +0 -0
- inspiremusic/dataset/dataset.py +154 -0
- inspiremusic/dataset/processor.py +595 -0
- inspiremusic/flow/decoder.py +277 -0
- inspiremusic/flow/flow.py +143 -0
- inspiremusic/flow/flow_matching.py +167 -0
- inspiremusic/flow/length_regulator.py +69 -0
- inspiremusic/hifigan/discriminator.py +140 -0
- inspiremusic/hifigan/f0_predictor.py +55 -0
- inspiremusic/hifigan/generator.py +411 -0
- inspiremusic/hifigan/hifigan.py +66 -0
- inspiremusic/llm/llm.py +402 -0
- inspiremusic/metrics/clap_score.py +135 -0
- inspiremusic/metrics/openl3_fd.py +338 -0
- inspiremusic/metrics/passt_kld.py +232 -0
- inspiremusic/music_tokenizer/__init__.py +0 -0
- inspiremusic/music_tokenizer/env.py +29 -0
- inspiremusic/music_tokenizer/meldataset.py +226 -0
- inspiremusic/music_tokenizer/models.py +548 -0
- inspiremusic/music_tokenizer/vqvae.py +58 -0
- inspiremusic/text/abs_tokenizer.py +34 -0
- inspiremusic/text/tokenizer.py +76 -0
- inspiremusic/transformer/__init__.py +0 -0
- inspiremusic/transformer/activation.py +84 -0
- inspiremusic/transformer/attention.py +328 -0
- inspiremusic/transformer/convolution.py +145 -0
- inspiremusic/transformer/decoder.py +396 -0
- inspiremusic/transformer/decoder_layer.py +132 -0
- inspiremusic/transformer/embedding.py +294 -0
- inspiremusic/transformer/encoder.py +477 -0
- inspiremusic/transformer/encoder_layer.py +235 -0
- inspiremusic/transformer/label_smoothing_loss.py +97 -0
- inspiremusic/transformer/positionwise_feed_forward.py +115 -0
- inspiremusic/transformer/qwen_encoder.py +165 -0
- inspiremusic/transformer/subsampling.py +384 -0
- inspiremusic/utils/__init__.py +0 -0
- inspiremusic/utils/audio_utils.py +623 -0
- inspiremusic/utils/binary.py +155 -0
app.py
CHANGED
@@ -1,193 +1,129 @@
|
|
1 |
# coding=utf-8
|
2 |
|
3 |
-
import os
|
4 |
-
import librosa
|
5 |
-
import base64
|
6 |
import io
|
7 |
-
import gradio as gr
|
8 |
-
import re
|
9 |
-
|
10 |
import numpy as np
|
11 |
-
import torch
|
12 |
import torchaudio
|
13 |
-
from modelscope import HubApi
|
14 |
-
|
15 |
-
api = HubApi()
|
16 |
-
|
17 |
-
key = os.environ["apikey"] if "apikey" in os.environ else ""
|
18 |
-
try:
|
19 |
-
api.login(key)
|
20 |
-
except:
|
21 |
-
pass
|
22 |
-
|
23 |
-
from funasr import AutoModel
|
24 |
-
|
25 |
-
model = "iic/SenseVoiceSmall"
|
26 |
-
model = AutoModel(model=model,
|
27 |
-
vad_model="iic/speech_fsmn_vad_zh-cn-16k-common-pytorch",
|
28 |
-
vad_kwargs={"max_single_segment_time": 30000},
|
29 |
-
trust_remote_code=True,
|
30 |
-
)
|
31 |
-
|
32 |
-
import re
|
33 |
-
|
34 |
-
emo_dict = {
|
35 |
-
"<|HAPPY|>": "😊",
|
36 |
-
"<|SAD|>": "😔",
|
37 |
-
"<|ANGRY|>": "😡",
|
38 |
-
"<|NEUTRAL|>": "",
|
39 |
-
"<|FEARFUL|>": "😰",
|
40 |
-
"<|DISGUSTED|>": "🤢",
|
41 |
-
"<|SURPRISED|>": "😮",
|
42 |
-
}
|
43 |
-
|
44 |
-
event_dict = {
|
45 |
-
"<|BGM|>": "🎼",
|
46 |
-
"<|Speech|>": "",
|
47 |
-
"<|Applause|>": "👏",
|
48 |
-
"<|Laughter|>": "😀",
|
49 |
-
"<|Cry|>": "😭",
|
50 |
-
"<|Sneeze|>": "🤧",
|
51 |
-
"<|Breath|>": "",
|
52 |
-
"<|Cough|>": "🤧",
|
53 |
-
}
|
54 |
-
|
55 |
-
emoji_dict = {
|
56 |
-
"<|nospeech|><|Event_UNK|>": "❓",
|
57 |
-
"<|zh|>": "",
|
58 |
-
"<|en|>": "",
|
59 |
-
"<|yue|>": "",
|
60 |
-
"<|ja|>": "",
|
61 |
-
"<|ko|>": "",
|
62 |
-
"<|nospeech|>": "",
|
63 |
-
"<|HAPPY|>": "😊",
|
64 |
-
"<|SAD|>": "😔",
|
65 |
-
"<|ANGRY|>": "😡",
|
66 |
-
"<|NEUTRAL|>": "",
|
67 |
-
"<|BGM|>": "🎼",
|
68 |
-
"<|Speech|>": "",
|
69 |
-
"<|Applause|>": "👏",
|
70 |
-
"<|Laughter|>": "😀",
|
71 |
-
"<|FEARFUL|>": "😰",
|
72 |
-
"<|DISGUSTED|>": "🤢",
|
73 |
-
"<|SURPRISED|>": "😮",
|
74 |
-
"<|Cry|>": "😭",
|
75 |
-
"<|EMO_UNKNOWN|>": "",
|
76 |
-
"<|Sneeze|>": "🤧",
|
77 |
-
"<|Breath|>": "",
|
78 |
-
"<|Cough|>": "😷",
|
79 |
-
"<|Sing|>": "",
|
80 |
-
"<|Speech_Noise|>": "",
|
81 |
-
"<|withitn|>": "",
|
82 |
-
"<|woitn|>": "",
|
83 |
-
"<|GBG|>": "",
|
84 |
-
"<|Event_UNK|>": "",
|
85 |
-
}
|
86 |
-
|
87 |
-
lang_dict = {
|
88 |
-
"<|zh|>": "<|lang|>",
|
89 |
-
"<|en|>": "<|lang|>",
|
90 |
-
"<|yue|>": "<|lang|>",
|
91 |
-
"<|ja|>": "<|lang|>",
|
92 |
-
"<|ko|>": "<|lang|>",
|
93 |
-
"<|nospeech|>": "<|lang|>",
|
94 |
-
}
|
95 |
-
|
96 |
-
|
97 |
-
emo_set = {"😊", "😔", "😡", "😰", "🤢", "😮"}
|
98 |
-
event_set = {"🎼", "👏", "😀", "😭", "🤧", "😷",}
|
99 |
-
|
100 |
-
notes = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
|
101 |
-
|
102 |
-
def format_str(s):
|
103 |
-
for sptk in emoji_dict:
|
104 |
-
s = s.replace(sptk, emoji_dict[sptk])
|
105 |
-
return s
|
106 |
-
|
107 |
-
|
108 |
-
def format_str_v2(s):
|
109 |
-
sptk_dict = {}
|
110 |
-
for sptk in emoji_dict:
|
111 |
-
sptk_dict[sptk] = s.count(sptk)
|
112 |
-
s = s.replace(sptk, "")
|
113 |
-
emo = "<|NEUTRAL|>"
|
114 |
-
for e in emo_dict:
|
115 |
-
if sptk_dict[e] > sptk_dict[emo]:
|
116 |
-
emo = e
|
117 |
-
for e in event_dict:
|
118 |
-
if sptk_dict[e] > 0:
|
119 |
-
s = event_dict[e] + s
|
120 |
-
s = s + emo_dict[emo]
|
121 |
-
|
122 |
-
for emoji in emo_set.union(event_set):
|
123 |
-
s = s.replace(" " + emoji, emoji)
|
124 |
-
s = s.replace(emoji + " ", emoji)
|
125 |
-
return s.strip()
|
126 |
-
|
127 |
-
def format_str_v3(s):
|
128 |
-
def get_emo(s):
|
129 |
-
return s[-1] if s[-1] in emo_set else None
|
130 |
-
def get_event(s):
|
131 |
-
return s[0] if s[0] in event_set else None
|
132 |
-
|
133 |
-
s = s.replace("<|nospeech|><|Event_UNK|>", "❓")
|
134 |
-
for lang in lang_dict:
|
135 |
-
s = s.replace(lang, "<|lang|>")
|
136 |
-
s_list = [format_str_v2(s_i).strip(" ") for s_i in s.split("<|lang|>")]
|
137 |
-
new_s = " " + s_list[0]
|
138 |
-
cur_ent_event = get_event(new_s)
|
139 |
-
for i in range(1, len(s_list)):
|
140 |
-
if len(s_list[i]) == 0:
|
141 |
-
continue
|
142 |
-
if get_event(s_list[i]) == cur_ent_event and get_event(s_list[i]) != None:
|
143 |
-
s_list[i] = s_list[i][1:]
|
144 |
-
#else:
|
145 |
-
cur_ent_event = get_event(s_list[i])
|
146 |
-
if get_emo(s_list[i]) != None and get_emo(s_list[i]) == get_emo(new_s):
|
147 |
-
new_s = new_s[:-1]
|
148 |
-
new_s += s_list[i].strip().lstrip()
|
149 |
-
new_s = new_s.replace("The.", " ")
|
150 |
-
return new_s.strip()
|
151 |
-
|
152 |
-
def model_inference(input_wav, language, fs=16000):
|
153 |
-
# task_abbr = {"Speech Recognition": "ASR", "Rich Text Transcription": ("ASR", "AED", "SER")}
|
154 |
-
language_abbr = {"auto": "auto", "zh": "zh", "en": "en", "yue": "yue", "ja": "ja", "ko": "ko",
|
155 |
-
"nospeech": "nospeech"}
|
156 |
-
|
157 |
-
# task = "Speech Recognition" if task is None else task
|
158 |
-
language = "auto" if len(language) < 1 else language
|
159 |
-
selected_language = language_abbr[language]
|
160 |
-
# selected_task = task_abbr.get(task)
|
161 |
-
|
162 |
-
# print(f"input_wav: {type(input_wav)}, {input_wav[1].shape}, {input_wav}")
|
163 |
-
|
164 |
-
if isinstance(input_wav, tuple):
|
165 |
-
fs, input_wav = input_wav
|
166 |
-
input_wav = input_wav.astype(np.float32) / np.iinfo(np.int16).max
|
167 |
-
if len(input_wav.shape) > 1:
|
168 |
-
input_wav = input_wav.mean(-1)
|
169 |
-
if fs != 16000:
|
170 |
-
print(f"audio_fs: {fs}")
|
171 |
-
resampler = torchaudio.transforms.Resample(fs, 16000)
|
172 |
-
input_wav_t = torch.from_numpy(input_wav).to(torch.float32)
|
173 |
-
input_wav = resampler(input_wav_t[None, :])[0, :].numpy()
|
174 |
-
|
175 |
-
|
176 |
-
merge_vad = True
|
177 |
-
print(f"language: {language}, merge_vad: {merge_vad}")
|
178 |
-
text = model.generate(input=input_wav,
|
179 |
-
cache={},
|
180 |
-
language=language,
|
181 |
-
use_itn=True,
|
182 |
-
batch_size_s=300, merge_vad=merge_vad)
|
183 |
-
|
184 |
-
print(text)
|
185 |
-
text = text[0]["text"]
|
186 |
-
text = format_str_v3(text)
|
187 |
-
print(text)
|
188 |
-
|
189 |
-
return text
|
190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
|
192 |
audio_examples = [
|
193 |
["example/inspiremusic/inspiremusic_01.wav", "text-to-music"],
|
@@ -218,7 +154,7 @@ description = """
|
|
218 |
- `The instrumental rock piece features a prominent bass guitar, delivering a pure and energetic sound.`
|
219 |
- `A serene blend of instrumental and light pop, featuring soothing melodies and a gentle, soulful keyboard performance.`
|
220 |
|
221 |
-
Recommended
|
222 |
|
223 |
"""
|
224 |
|
@@ -232,86 +168,92 @@ html_content = """
|
|
232 |
<p style="font-size: 18px;margin-left: 20px;"><a href="https://github.com/FunAudioLLM/InspireMusic" target="_blank">Code</a> </p>
|
233 |
<p style="font-size: 18px;margin-left: 20px;"><a href="https://iris2c.github.io/InspireMusic" target="_blank">Demo</a></p>
|
234 |
<h2 style="font-size: 22px;margin-left: 0px;">Models</h2>
|
235 |
-
<p style="font-size: 18px;margin-left: 20px;"><a href="https://modelscope.cn/models/iic/InspireMusic/summary" target="_blank">Modelscope Model</a>: </p>
|
236 |
-
<p style="font-size: 18px;margin-left: 20px;"><a href="https://huggingface.co/FunAudioLLM/InspireMusic-
|
237 |
</div>
|
238 |
"""
|
239 |
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
.
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
316 |
|
317 |
|
|
|
1 |
# coding=utf-8
|
2 |
|
|
|
|
|
|
|
3 |
import io
|
|
|
|
|
|
|
4 |
import numpy as np
|
|
|
5 |
import torchaudio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
import torch
|
8 |
+
import soundfile as sf
|
9 |
+
import gradio as gr
|
10 |
+
import spaces
|
11 |
+
from inspiremusic.cli.inference import InspireMusicUnified, set_env_variables
|
12 |
+
import os
|
13 |
+
import sys
|
14 |
+
|
15 |
+
|
16 |
+
def get_args():
|
17 |
+
parser = argparse.ArgumentParser(
|
18 |
+
description='Run inference with your model')
|
19 |
+
parser.add_argument('-m', '--model_name', default="InspireMusic-1.5B-Long",
|
20 |
+
help='Model name')
|
21 |
+
|
22 |
+
parser.add_argument('-d', '--model_dir',
|
23 |
+
help='Model folder path')
|
24 |
+
|
25 |
+
parser.add_argument('-t', '--text',
|
26 |
+
default="Experience soothing and sensual instrumental jazz with a touch of Bossa Nova, perfect for a relaxing restaurant or spa ambiance.",
|
27 |
+
help='Prompt text')
|
28 |
+
|
29 |
+
parser.add_argument('-a', '--audio_prompt', default=None,
|
30 |
+
help='Prompt audio')
|
31 |
+
|
32 |
+
parser.add_argument('-c', '--chorus', default="intro",
|
33 |
+
help='Chorus tag generation mode (e.g., random, verse, chorus, intro, outro)')
|
34 |
+
|
35 |
+
parser.add_argument('--fast', type=bool, default=False,
|
36 |
+
help='Enable fast inference mode (without flow matching)')
|
37 |
+
|
38 |
+
parser.add_argument('-g', '--gpu', type=int, default=0,
|
39 |
+
help='GPU ID for this rank, -1 for CPU')
|
40 |
+
|
41 |
+
parser.add_argument('--task', default='text-to-music',
|
42 |
+
choices=['text-to-music', 'continuation', 'reconstruct', 'super_resolution'],
|
43 |
+
help='Inference task type: text-to-music, continuation, reconstruct, super_resolution')
|
44 |
+
|
45 |
+
parser.add_argument('-r', '--result_dir', default="exp/inspiremusic",
|
46 |
+
help='Directory to save generated audio')
|
47 |
+
|
48 |
+
parser.add_argument('-o', '--output_fn', default="output_audio",
|
49 |
+
help='Output file name')
|
50 |
+
|
51 |
+
parser.add_argument('-f', '--format', type=str, default="wav",
|
52 |
+
choices=["wav", "mp3", "m4a", "flac"],
|
53 |
+
help='Format of output audio')
|
54 |
+
|
55 |
+
parser.add_argument('--sample_rate', type=int, default=24000,
|
56 |
+
help='Sampling rate of input audio')
|
57 |
+
|
58 |
+
parser.add_argument('--output_sample_rate', type=int, default=48000,
|
59 |
+
choices=[24000, 48000],
|
60 |
+
help='Sampling rate of generated output audio')
|
61 |
+
|
62 |
+
parser.add_argument('-s', '--time_start', type=float, default=0.0,
|
63 |
+
help='Start time in seconds')
|
64 |
+
|
65 |
+
parser.add_argument('-e', '--time_end', type=float, default=30.0,
|
66 |
+
help='End time in seconds')
|
67 |
+
|
68 |
+
parser.add_argument('--max_audio_prompt_length', type=float, default=5.0,
|
69 |
+
help='Maximum audio prompt length in seconds')
|
70 |
+
|
71 |
+
parser.add_argument('--min_generate_audio_seconds', type=float,
|
72 |
+
default=10.0,
|
73 |
+
help='Minimum generated audio length in seconds')
|
74 |
+
|
75 |
+
parser.add_argument('--max_generate_audio_seconds', type=float,
|
76 |
+
default=30.0,
|
77 |
+
help='Maximum generated audio length in seconds')
|
78 |
+
|
79 |
+
parser.add_argument('--fp16', type=bool, default=True,
|
80 |
+
help='Inference with fp16 model')
|
81 |
+
|
82 |
+
parser.add_argument('--fade_out', type=bool, default=True,
|
83 |
+
help='Apply fade out effect to generated audio')
|
84 |
+
|
85 |
+
parser.add_argument('--fade_out_duration', type=float, default=1.0,
|
86 |
+
help='Fade out duration in seconds')
|
87 |
+
|
88 |
+
parser.add_argument('--trim', type=bool, default=False,
|
89 |
+
help='Trim the silence ending of generated audio')
|
90 |
+
|
91 |
+
args = parser.parse_args()
|
92 |
+
|
93 |
+
if not args.model_dir:
|
94 |
+
args.model_dir = os.path.join("./pretrained_models", args.model_name)
|
95 |
+
|
96 |
+
print(args)
|
97 |
+
return args
|
98 |
+
|
99 |
+
def InspireMusic(args):
|
100 |
+
set_env_variables()
|
101 |
+
model = InspireMusicUnified(model_name=args.model_name,
|
102 |
+
model_dir=args.model_dir,
|
103 |
+
min_generate_audio_seconds=args.min_generate_audio_seconds,
|
104 |
+
max_generate_audio_seconds=args.max_generate_audio_seconds,
|
105 |
+
sample_rate=args.sample_rate,
|
106 |
+
output_sample_rate=args.output_sample_rate,
|
107 |
+
load_jit=True,
|
108 |
+
load_onnx=False,
|
109 |
+
fast=args.fast,
|
110 |
+
fp16=args.fp16,
|
111 |
+
gpu=args.gpu,
|
112 |
+
result_dir=args.result_dir)
|
113 |
+
|
114 |
+
model.inference(task=args.task,
|
115 |
+
text=args.text,
|
116 |
+
audio_prompt=args.audio_prompt,
|
117 |
+
chorus=args.chorus,
|
118 |
+
time_start=args.time_start,
|
119 |
+
time_end=args.time_end,
|
120 |
+
output_fn=args.output_fn,
|
121 |
+
max_audio_prompt_length=args.max_audio_prompt_length,
|
122 |
+
fade_out_duration=args.fade_out_duration,
|
123 |
+
output_format=args.format,
|
124 |
+
fade_out_mode=args.fade_out,
|
125 |
+
trim=args.trim)
|
126 |
+
return os.path.join(args.result_dir, f"{args.output_fn}.{args.format}")
|
127 |
|
128 |
audio_examples = [
|
129 |
["example/inspiremusic/inspiremusic_01.wav", "text-to-music"],
|
|
|
154 |
- `The instrumental rock piece features a prominent bass guitar, delivering a pure and energetic sound.`
|
155 |
- `A serene blend of instrumental and light pop, featuring soothing melodies and a gentle, soulful keyboard performance.`
|
156 |
|
157 |
+
Recommended audio prompt duration is 5 seconds, generate audio length is below 30 seconds. To generate audio longer than 30 seconds, local deployment is recommended, github repo.
|
158 |
|
159 |
"""
|
160 |
|
|
|
168 |
<p style="font-size: 18px;margin-left: 20px;"><a href="https://github.com/FunAudioLLM/InspireMusic" target="_blank">Code</a> </p>
|
169 |
<p style="font-size: 18px;margin-left: 20px;"><a href="https://iris2c.github.io/InspireMusic" target="_blank">Demo</a></p>
|
170 |
<h2 style="font-size: 22px;margin-left: 0px;">Models</h2>
|
171 |
+
<p style="font-size: 18px;margin-left: 20px;"><a href="https://modelscope.cn/models/iic/InspireMusic-1.5B-Long/summary" target="_blank">Modelscope Model</a>: </p>
|
172 |
+
<p style="font-size: 18px;margin-left: 20px;"><a href="https://huggingface.co/FunAudioLLM/InspireMusic-1.5B-Long" target="_blank">Huggingface Model</a></p>
|
173 |
</div>
|
174 |
"""
|
175 |
|
176 |
+
def music_generation(task, text=None, audio=None):
|
177 |
+
args = get_args()
|
178 |
+
args.task = task
|
179 |
+
args.text = text if text
|
180 |
+
args.audio_prompt = audio if audio
|
181 |
+
generate_audio_path = InspireMusic(args)
|
182 |
+
return generate_audio_path
|
183 |
+
|
184 |
+
demo = gr.Blocks()
|
185 |
+
|
186 |
+
t2m_demo = gr.Interface(
|
187 |
+
fn=music_generation,
|
188 |
+
inputs = [
|
189 |
+
gr.Dropdown(["Text-To-Music"], value="text-to-music", multiselect=False, info="Choose a task."),
|
190 |
+
gr.Text(label="Input Text"),
|
191 |
+
],
|
192 |
+
outputs = [
|
193 |
+
gr.Audio(label="Generated Music", type="generated audio filepath"),
|
194 |
+
],
|
195 |
+
title = "<a href='https://github.com/FunAudioLLM/InspireMusic' target='_blank'>InspireMusic<a/>: A Unified Framework for Music, Song, Audio Generation.",
|
196 |
+
description = ("InspireMusic ([Github Repo](https://github.com/FunAudioLLM/InspireMusic)) is a fundamental AIGC toolkit and models designed for music, song, and audio generation using PyTorch."
|
197 |
+
"To try it, simply type text to generation music, or click one of the examples. "),
|
198 |
+
article = ("<p style='text-align: center'><a href='' target='_blank'>InspireMusic</a> </p>"
|
199 |
+
"<p style='text-align: center'><a href='https://openreview.net/forum?id=yBlVlS2Fd9' target='_blank'>WavTokenizer: an Efficient Acoustic Discrete Codec Tokenizer for Audio Language Modeling</a> </p>"),
|
200 |
+
examples = [
|
201 |
+
["example/inspiremusic/inspiremusic_01.wav", "24000 Hz"],
|
202 |
+
["example/ras/chorus/chorus_01.wav", "48000 Hz"],
|
203 |
+
],
|
204 |
+
cache_examples = True,
|
205 |
+
)
|
206 |
+
|
207 |
+
con_demo = gr.Interface(
|
208 |
+
fn=music_generation,
|
209 |
+
inputs = [
|
210 |
+
gr.Dropdown(["Music Continuation"], value="continuation", multiselect=False, info="Choose a task."),
|
211 |
+
gr.Text(label="Input Text"),
|
212 |
+
gr.Audio(label="Input Audio Prompt", type="audio prompt filepath"),
|
213 |
+
],
|
214 |
+
outputs = [
|
215 |
+
gr.Audio(label="Generated Music", type="generated audio filepath"),
|
216 |
+
],
|
217 |
+
title = "<a href='https://github.com/FunAudioLLM/InspireMusic' target='_blank'>InspireMusic<a/>: A Unified Framework for Music, Song, Audio Generation.",
|
218 |
+
description = ("InspireMusic ([Github Repo](https://github.com/FunAudioLLM/InspireMusic)) is a fundamental AIGC toolkit and models designed for music, song, and audio generation using PyTorch."
|
219 |
+
"To try it, simply type text to generation music, or click one of the examples. "),
|
220 |
+
article = ("<p style='text-align: center'><a href='' target='_blank'>InspireMusic</a> </p>"
|
221 |
+
"<p style='text-align: center'><a href='https://openreview.net/forum?id=yBlVlS2Fd9' target='_blank'>WavTokenizer: an Efficient Acoustic Discrete Codec Tokenizer for Audio Language Modeling</a> </p>"),
|
222 |
+
examples = [
|
223 |
+
["example/inspiremusic/inspiremusic_01.wav", "24000 Hz"],
|
224 |
+
["example/ras/chorus/chorus_01.wav", "48000 Hz"],
|
225 |
+
],
|
226 |
+
cache_examples = True,
|
227 |
+
)
|
228 |
+
|
229 |
+
con_demo = gr.Interface(
|
230 |
+
fn=music_generation,
|
231 |
+
inputs = [
|
232 |
+
gr.Dropdown(["Music Continuation"], value="continuation", multiselect=False, info="Choose a task."),
|
233 |
+
gr.Text(label="Input Text"),
|
234 |
+
gr.Audio(label="Input Audio Prompt", type="audio prompt filepath"),
|
235 |
+
],
|
236 |
+
outputs = [
|
237 |
+
gr.Audio(label="Generated Music", type="generated audio filepath"),
|
238 |
+
],
|
239 |
+
title = "<a href='https://github.com/FunAudioLLM/InspireMusic' target='_blank'>InspireMusic<a/>: A Unified Framework for Music, Song, Audio Generation.",
|
240 |
+
description = ("InspireMusic ([Github Repo](https://github.com/FunAudioLLM/InspireMusic)) is a fundamental AIGC toolkit and models designed for music, song, and audio generation using PyTorch."
|
241 |
+
"To try it, simply type text to generation music, or click one of the examples. "),
|
242 |
+
article = ("<p style='text-align: center'><a href='' target='_blank'>InspireMusic</a> </p>"
|
243 |
+
"<p style='text-align: center'><a href='https://openreview.net/forum?id=yBlVlS2Fd9' target='_blank'>WavTokenizer: an Efficient Acoustic Discrete Codec Tokenizer for Audio Language Modeling</a> </p>"),
|
244 |
+
examples = [
|
245 |
+
["example/inspiremusic/inspiremusic_01.wav", "24000 Hz"],
|
246 |
+
["example/ras/chorus/chorus_01.wav", "48000 Hz"],
|
247 |
+
],
|
248 |
+
cache_examples = True,
|
249 |
+
)
|
250 |
+
|
251 |
+
with demo:
|
252 |
+
gr.TabbedInterface([t2m_demo, con_demo,],
|
253 |
+
["Task 1: Text-to-Music",
|
254 |
+
"Task 2: Music Continuation"])
|
255 |
+
# gr.TabbedInterface([t2m_demo, con_demo, fast_demo], ["Task 1: Text-to-Music", "Task 2: Music Continuation", "Task 3: Without Flow Matching"])
|
256 |
+
|
257 |
+
demo.launch()
|
258 |
|
259 |
|
inspiremusic/__init__.py
ADDED
File without changes
|
inspiremusic/bin/export_jit.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
|
15 |
+
from __future__ import print_function
|
16 |
+
|
17 |
+
import argparse
|
18 |
+
import logging
|
19 |
+
logging.getLogger('matplotlib').setLevel(logging.WARNING)
|
20 |
+
import os
|
21 |
+
import sys
|
22 |
+
import torch
|
23 |
+
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
|
24 |
+
sys.path.append('{}/../..'.format(ROOT_DIR))
|
25 |
+
sys.path.append('{}/../../third_party/Matcha-TTS'.format(ROOT_DIR))
|
26 |
+
from inspiremusic.cli.inspiremusic import InspireMusic
|
27 |
+
|
28 |
+
|
29 |
+
def get_args():
|
30 |
+
parser = argparse.ArgumentParser(description='export your model for deployment')
|
31 |
+
parser.add_argument('--model_dir',
|
32 |
+
type=str,
|
33 |
+
default='pretrained_models/InspireMusic',
|
34 |
+
help='local path')
|
35 |
+
args = parser.parse_args()
|
36 |
+
print(args)
|
37 |
+
return args
|
38 |
+
|
39 |
+
|
40 |
+
def main():
|
41 |
+
args = get_args()
|
42 |
+
logging.basicConfig(level=logging.DEBUG,
|
43 |
+
format='%(asctime)s %(levelname)s %(message)s')
|
44 |
+
|
45 |
+
torch._C._jit_set_fusion_strategy([('STATIC', 1)])
|
46 |
+
torch._C._jit_set_profiling_mode(False)
|
47 |
+
torch._C._jit_set_profiling_executor(False)
|
48 |
+
|
49 |
+
inspiremusic = InspireMusic(args.model_dir, load_jit=False, load_onnx=False)
|
50 |
+
|
51 |
+
# 1. export llm text_encoder
|
52 |
+
llm_text_encoder = inspiremusic.model.llm.text_encoder.half()
|
53 |
+
script = torch.jit.script(llm_text_encoder)
|
54 |
+
script = torch.jit.freeze(script)
|
55 |
+
script = torch.jit.optimize_for_inference(script)
|
56 |
+
script.save('{}/llm.text_encoder.fp16.zip'.format(args.model_dir))
|
57 |
+
|
58 |
+
# 2. export llm llm
|
59 |
+
llm_llm = inspiremusic.model.llm.llm.half()
|
60 |
+
script = torch.jit.script(llm_llm)
|
61 |
+
script = torch.jit.freeze(script, preserved_attrs=['forward_chunk'])
|
62 |
+
script = torch.jit.optimize_for_inference(script)
|
63 |
+
script.save('{}/llm.llm.fp16.zip'.format(args.model_dir))
|
64 |
+
|
65 |
+
# 3. export flow encoder
|
66 |
+
flow_encoder = inspiremusic.model.flow.encoder
|
67 |
+
script = torch.jit.script(flow_encoder)
|
68 |
+
script = torch.jit.freeze(script)
|
69 |
+
script = torch.jit.optimize_for_inference(script)
|
70 |
+
script.save('{}/flow.encoder.fp32.zip'.format(args.model_dir))
|
71 |
+
|
72 |
+
|
73 |
+
if __name__ == '__main__':
|
74 |
+
main()
|
inspiremusic/bin/export_onnx.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Antgroup Inc (authors: Zhoubofan, [email protected])
|
2 |
+
# Copyright (c) 2024 Alibaba Inc
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
|
16 |
+
from __future__ import print_function
|
17 |
+
|
18 |
+
import argparse
|
19 |
+
import logging
|
20 |
+
logging.getLogger('matplotlib').setLevel(logging.WARNING)
|
21 |
+
import os
|
22 |
+
import sys
|
23 |
+
import onnxruntime
|
24 |
+
import random
|
25 |
+
import torch
|
26 |
+
from tqdm import tqdm
|
27 |
+
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
|
28 |
+
sys.path.append('{}/../..'.format(ROOT_DIR))
|
29 |
+
sys.path.append('{}/../../third_party/Matcha-TTS'.format(ROOT_DIR))
|
30 |
+
from inspiremusic.cli.inspiremusic import InspireMusic
|
31 |
+
|
32 |
+
|
33 |
+
def get_dummy_input(batch_size, seq_len, out_channels, device):
|
34 |
+
x = torch.rand((batch_size, out_channels, seq_len), dtype=torch.float32, device=device)
|
35 |
+
mask = torch.ones((batch_size, 1, seq_len), dtype=torch.float32, device=device)
|
36 |
+
mu = torch.rand((batch_size, out_channels, seq_len), dtype=torch.float32, device=device)
|
37 |
+
t = torch.rand((batch_size), dtype=torch.float32, device=device)
|
38 |
+
spks = torch.rand((batch_size, out_channels), dtype=torch.float32, device=device)
|
39 |
+
cond = torch.rand((batch_size, out_channels, seq_len), dtype=torch.float32, device=device)
|
40 |
+
return x, mask, mu, t, spks, cond
|
41 |
+
|
42 |
+
|
43 |
+
def get_args():
|
44 |
+
parser = argparse.ArgumentParser(description='export your model for deployment')
|
45 |
+
parser.add_argument('--model_dir',
|
46 |
+
type=str,
|
47 |
+
default='pretrained_models/InspireMusic',
|
48 |
+
help='local path')
|
49 |
+
args = parser.parse_args()
|
50 |
+
print(args)
|
51 |
+
return args
|
52 |
+
|
53 |
+
|
54 |
+
def main():
|
55 |
+
args = get_args()
|
56 |
+
logging.basicConfig(level=logging.DEBUG,
|
57 |
+
format='%(asctime)s %(levelname)s %(message)s')
|
58 |
+
|
59 |
+
inspiremusic = InspireMusic(args.model_dir, load_jit=False, load_onnx=False)
|
60 |
+
|
61 |
+
# 1. export flow decoder estimator
|
62 |
+
estimator = inspiremusic.model.flow.decoder.estimator
|
63 |
+
|
64 |
+
device = inspiremusic.model.device
|
65 |
+
batch_size, seq_len = 1, 256
|
66 |
+
out_channels = inspiremusic.model.flow.decoder.estimator.out_channels
|
67 |
+
x, mask, mu, t, spks, cond = get_dummy_input(batch_size, seq_len, out_channels, device)
|
68 |
+
torch.onnx.export(
|
69 |
+
estimator,
|
70 |
+
(x, mask, mu, t, spks, cond),
|
71 |
+
'{}/flow.decoder.estimator.fp32.onnx'.format(args.model_dir),
|
72 |
+
export_params=True,
|
73 |
+
opset_version=18,
|
74 |
+
do_constant_folding=True,
|
75 |
+
input_names=['x', 'mask', 'mu', 't', 'spks', 'cond'],
|
76 |
+
output_names=['estimator_out'],
|
77 |
+
dynamic_axes={
|
78 |
+
'x': {0: 'batch_size', 2: 'seq_len'},
|
79 |
+
'mask': {0: 'batch_size', 2: 'seq_len'},
|
80 |
+
'mu': {0: 'batch_size', 2: 'seq_len'},
|
81 |
+
'cond': {0: 'batch_size', 2: 'seq_len'},
|
82 |
+
't': {0: 'batch_size'},
|
83 |
+
'spks': {0: 'batch_size'},
|
84 |
+
'estimator_out': {0: 'batch_size', 2: 'seq_len'},
|
85 |
+
}
|
86 |
+
)
|
87 |
+
|
88 |
+
# 2. test computation consistency
|
89 |
+
option = onnxruntime.SessionOptions()
|
90 |
+
option.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL
|
91 |
+
option.intra_op_num_threads = 1
|
92 |
+
providers = ['CUDAExecutionProvider' if torch.cuda.is_available() else 'CPUExecutionProvider']
|
93 |
+
estimator_onnx = onnxruntime.InferenceSession('{}/flow.decoder.estimator.fp32.onnx'.format(args.model_dir),
|
94 |
+
sess_options=option, providers=providers)
|
95 |
+
|
96 |
+
for _ in tqdm(range(10)):
|
97 |
+
x, mask, mu, t, spks, cond = get_dummy_input(random.randint(1, 6), random.randint(16, 512), out_channels, device)
|
98 |
+
output_pytorch = estimator(x, mask, mu, t, spks, cond)
|
99 |
+
ort_inputs = {
|
100 |
+
'x': x.cpu().numpy(),
|
101 |
+
'mask': mask.cpu().numpy(),
|
102 |
+
'mu': mu.cpu().numpy(),
|
103 |
+
't': t.cpu().numpy(),
|
104 |
+
'spks': spks.cpu().numpy(),
|
105 |
+
'cond': cond.cpu().numpy()
|
106 |
+
}
|
107 |
+
output_onnx = estimator_onnx.run(None, ort_inputs)[0]
|
108 |
+
torch.testing.assert_allclose(output_pytorch, torch.from_numpy(output_onnx).to(device), rtol=1e-2, atol=1e-4)
|
109 |
+
|
110 |
+
|
111 |
+
if __name__ == "__main__":
|
112 |
+
main()
|
inspiremusic/bin/flow_only_infer.py
ADDED
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
|
15 |
+
from __future__ import print_function
|
16 |
+
|
17 |
+
import argparse
|
18 |
+
import logging
|
19 |
+
logging.getLogger('matplotlib').setLevel(logging.WARNING)
|
20 |
+
import os
|
21 |
+
import torch
|
22 |
+
from torch.utils.data import DataLoader
|
23 |
+
import torchaudio
|
24 |
+
from hyperpyyaml import load_hyperpyyaml
|
25 |
+
from tqdm import tqdm
|
26 |
+
from inspiremusic.cli.model import InspireMusicModel
|
27 |
+
from inspiremusic.dataset.dataset import Dataset
|
28 |
+
from inspiremusic.utils.common import MUSIC_STRUCTURE_LABELS
|
29 |
+
|
30 |
+
def get_args():
|
31 |
+
parser = argparse.ArgumentParser(description='inference only with flow model')
|
32 |
+
parser.add_argument('--config', required=True, help='config file')
|
33 |
+
parser.add_argument('--prompt_data', required=True, help='prompt data file')
|
34 |
+
parser.add_argument('--flow_model', required=True, help='flow model file')
|
35 |
+
parser.add_argument('--llm_model', default=None,required=False, help='llm model file')
|
36 |
+
|
37 |
+
parser.add_argument('--music_tokenizer', required=True, help='music tokenizer model file')
|
38 |
+
parser.add_argument('--wavtokenizer', required=True, help='wavtokenizer model file')
|
39 |
+
parser.add_argument('--chorus', default="random",required=False, help='chorus tag generation mode, eg. random, verse, chorus, intro.')
|
40 |
+
parser.add_argument('--sample_rate', type=int, default=48000, required=False,
|
41 |
+
help='sampling rate of generated audio')
|
42 |
+
parser.add_argument('--min_generate_audio_seconds', type=float, default=10.0, required=False,
|
43 |
+
help='the minimum generated audio length in seconds')
|
44 |
+
parser.add_argument('--max_generate_audio_seconds', type=float, default=30.0, required=False,
|
45 |
+
help='the maximum generated audio length in seconds')
|
46 |
+
parser.add_argument('--gpu',
|
47 |
+
type=int,
|
48 |
+
default=-1,
|
49 |
+
help='gpu id for this rank, -1 for cpu')
|
50 |
+
parser.add_argument('--result_dir', required=True, help='asr result file')
|
51 |
+
args = parser.parse_args()
|
52 |
+
print(args)
|
53 |
+
return args
|
54 |
+
|
55 |
+
def main():
|
56 |
+
args = get_args()
|
57 |
+
logging.basicConfig(level=logging.DEBUG,
|
58 |
+
format='%(asctime)s %(levelname)s %(message)s')
|
59 |
+
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu)
|
60 |
+
|
61 |
+
# Init inspiremusic models from configs
|
62 |
+
use_cuda = args.gpu >= 0 and torch.cuda.is_available()
|
63 |
+
device = torch.device('cuda' if use_cuda else 'cpu')
|
64 |
+
with open(args.config, 'r') as f:
|
65 |
+
configs = load_hyperpyyaml(f)
|
66 |
+
|
67 |
+
model = InspireMusicModel(None, configs['flow'], configs['hift'], configs['wavtokenizer'])
|
68 |
+
model.load(args.llm_model, args.flow_model, args.music_tokenizer, args.wavtokenizer)
|
69 |
+
|
70 |
+
if args.llm_model is None:
|
71 |
+
model.llm = None
|
72 |
+
else:
|
73 |
+
model.llm = model.llm.to(torch.float32)
|
74 |
+
|
75 |
+
if args.flow_model is None:
|
76 |
+
model.flow = None
|
77 |
+
|
78 |
+
test_dataset = Dataset(args.prompt_data, data_pipeline=configs['data_pipeline'], mode='inference', shuffle=True, partition=False)
|
79 |
+
test_data_loader = DataLoader(test_dataset, batch_size=None, num_workers=0)
|
80 |
+
|
81 |
+
del configs
|
82 |
+
os.makedirs(args.result_dir, exist_ok=True)
|
83 |
+
fn = os.path.join(args.result_dir, 'wav.scp')
|
84 |
+
f = open(fn, 'w')
|
85 |
+
with torch.no_grad():
|
86 |
+
for _, batch in tqdm(enumerate(test_data_loader)):
|
87 |
+
utts = batch["utts"]
|
88 |
+
assert len(utts) == 1, "inference mode only support batchsize 1"
|
89 |
+
|
90 |
+
if "semantic_token" in batch:
|
91 |
+
token = batch["semantic_token"].to(device)
|
92 |
+
token_len = batch["semantic_token_len"].to(device)
|
93 |
+
else:
|
94 |
+
if audio_token is None:
|
95 |
+
token = None
|
96 |
+
token_len = None
|
97 |
+
else:
|
98 |
+
token = audio_token.view(audio_token.size(0),-1,4)[:,:,0]
|
99 |
+
token_len = audio_token_len / 4
|
100 |
+
|
101 |
+
text_token = batch["text_token"].to(device)
|
102 |
+
text_token_len = batch["text_token_len"].to(device)
|
103 |
+
text = batch["text"]
|
104 |
+
|
105 |
+
if "time_start" not in batch.keys():
|
106 |
+
batch["time_start"] = torch.randint(0, args.min_generate_audio_seconds, (1,)).to(torch.float64)
|
107 |
+
if "time_end" not in batch.keys():
|
108 |
+
batch["time_end"] = torch.randint(args.min_generate_audio_seconds, args.max_generate_audio_seconds, (1,)).to(torch.float64)
|
109 |
+
elif (batch["time_end"].numpy()[0] - batch["time_start"].numpy()[0]) < args.min_generate_audio_seconds:
|
110 |
+
batch["time_end"] = torch.randint(int(batch["time_start"].numpy()[0] + args.min_generate_audio_seconds), int(batch["time_start"].numpy()[0] + args.max_generate_audio_seconds), (1,)).to(torch.float64)
|
111 |
+
|
112 |
+
if "chorus" not in batch.keys():
|
113 |
+
batch["chorus"] = torch.randint(1, 5, (1,))
|
114 |
+
|
115 |
+
if args.chorus == "random":
|
116 |
+
batch["chorus"] = torch.randint(1, 5, (1,))
|
117 |
+
elif args.chorus == "intro":
|
118 |
+
batch["chorus"] = torch.Tensor([0])
|
119 |
+
elif "verse" in args.chorus:
|
120 |
+
batch["chorus"] = torch.Tensor([1])
|
121 |
+
elif args.chorus == "chorus":
|
122 |
+
batch["chorus"] = torch.Tensor([2])
|
123 |
+
elif args.chorus == "outro":
|
124 |
+
batch["chorus"] = torch.Tensor([4])
|
125 |
+
|
126 |
+
time_start = batch["time_start"].to(device)
|
127 |
+
time_end = batch["time_end"].to(device)
|
128 |
+
chorus = batch["chorus"].to(torch.int)
|
129 |
+
|
130 |
+
text_prompt = f"<|{batch['time_start'].numpy()[0]}|><|{MUSIC_STRUCTURE_LABELS[chorus.numpy()[0]]}|><|{batch['text'][0]}|><|{batch['time_end'].numpy()[0]}|>"
|
131 |
+
chorus = chorus.to(device)
|
132 |
+
|
133 |
+
model_input = {"text": text, "audio_token": token, "audio_token_len": token_len,
|
134 |
+
"text_token": text_token, "text_token_len": text_token_len,
|
135 |
+
"embeddings": [time_start, time_end, chorus], "raw_text":text}
|
136 |
+
|
137 |
+
music_audios = []
|
138 |
+
for model_output in model.inference(**model_input):
|
139 |
+
music_audios.append(model_output['music_audio'])
|
140 |
+
|
141 |
+
music_key = utts[0]
|
142 |
+
music_fn = os.path.join(args.result_dir, '{}.wav'.format(music_key))
|
143 |
+
torchaudio.save(music_fn, music_audios[0], sample_rate=args.sample_rate)
|
144 |
+
f.write('{} {}\n'.format(music_key, music_fn))
|
145 |
+
f.flush()
|
146 |
+
f.close()
|
147 |
+
logging.info('Result wav.scp saved in {}'.format(fn))
|
148 |
+
|
149 |
+
if __name__ == '__main__':
|
150 |
+
main()
|
inspiremusic/bin/inference.py
ADDED
@@ -0,0 +1,266 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
|
15 |
+
from __future__ import print_function
|
16 |
+
|
17 |
+
import argparse
|
18 |
+
import logging
|
19 |
+
|
20 |
+
logging.getLogger('matplotlib').setLevel(logging.WARNING)
|
21 |
+
import os
|
22 |
+
import torch
|
23 |
+
from torch.utils.data import DataLoader
|
24 |
+
import torchaudio
|
25 |
+
from hyperpyyaml import load_hyperpyyaml
|
26 |
+
from tqdm import tqdm
|
27 |
+
from inspiremusic.cli.model import InspireMusicModel
|
28 |
+
from inspiremusic.dataset.dataset import Dataset
|
29 |
+
import time
|
30 |
+
from inspiremusic.utils.audio_utils import trim_audio, fade_out, process_audio
|
31 |
+
from inspiremusic.utils.common import MUSIC_STRUCTURE_LABELS
|
32 |
+
|
33 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
34 |
+
|
35 |
+
def get_args():
|
36 |
+
parser = argparse.ArgumentParser(description='inference only with your model')
|
37 |
+
parser.add_argument('--config', required=True, help='config file')
|
38 |
+
parser.add_argument('--prompt_data', required=True, help='prompt data file')
|
39 |
+
parser.add_argument('--flow_model', default=None, required=False, help='flow model file')
|
40 |
+
parser.add_argument('--llm_model', default=None,required=False, help='flow model file')
|
41 |
+
parser.add_argument('--music_tokenizer', required=True, help='music tokenizer model file')
|
42 |
+
parser.add_argument('--wavtokenizer', required=True, help='wavtokenizer model file')
|
43 |
+
parser.add_argument('--chorus', default="random",required=False, help='chorus tag generation mode, eg. random, verse, chorus, intro.')
|
44 |
+
parser.add_argument('--fast', action='store_true', required=False, help='True: fast inference mode, without flow matching for fast inference. False: normal inference mode, with flow matching for high quality.')
|
45 |
+
parser.add_argument('--fp16', default=True, type=bool, required=False, help='inference with fp16 model')
|
46 |
+
parser.add_argument('--fade_out', default=True, type=bool, required=False, help='add fade out effect to generated audio')
|
47 |
+
parser.add_argument('--fade_out_duration', default=1.0, type=float, required=False, help='fade out duration in seconds')
|
48 |
+
parser.add_argument('--trim', default=False, type=bool, required=False, help='trim the silence ending of generated audio')
|
49 |
+
parser.add_argument('--format', type=str, default="wav", required=False,
|
50 |
+
choices=["wav", "mp3", "m4a", "flac"],
|
51 |
+
help='sampling rate of input audio')
|
52 |
+
parser.add_argument('--sample_rate', type=int, default=24000, required=False,
|
53 |
+
help='sampling rate of input audio')
|
54 |
+
parser.add_argument('--output_sample_rate', type=int, default=48000, required=False, choices=[24000, 48000],
|
55 |
+
help='sampling rate of generated output audio')
|
56 |
+
parser.add_argument('--min_generate_audio_seconds', type=float, default=10.0, required=False,
|
57 |
+
help='the minimum generated audio length in seconds')
|
58 |
+
parser.add_argument('--max_generate_audio_seconds', type=float, default=30.0, required=False,
|
59 |
+
help='the maximum generated audio length in seconds')
|
60 |
+
parser.add_argument('--gpu',
|
61 |
+
type=int,
|
62 |
+
default=0,
|
63 |
+
help='gpu id for this rank, -1 for cpu')
|
64 |
+
parser.add_argument('--task',
|
65 |
+
default='text-to-music',
|
66 |
+
choices=['text-to-music', 'continuation', "reconstruct", "super_resolution"],
|
67 |
+
help='choose inference task type. text-to-music: text-to-music task. continuation: music continuation task. reconstruct: reconstruction of original music. super_resolution: convert original 24kHz music into 48kHz music.')
|
68 |
+
parser.add_argument('--result_dir', required=True, help='asr result file')
|
69 |
+
args = parser.parse_args()
|
70 |
+
print(args)
|
71 |
+
return args
|
72 |
+
|
73 |
+
|
74 |
+
def main():
|
75 |
+
args = get_args()
|
76 |
+
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s')
|
77 |
+
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu)
|
78 |
+
|
79 |
+
if args.fast:
|
80 |
+
args.output_sample_rate = 24000
|
81 |
+
|
82 |
+
min_generate_audio_length = int(args.output_sample_rate * args.min_generate_audio_seconds)
|
83 |
+
max_generate_audio_length = int(args.output_sample_rate * args.max_generate_audio_seconds)
|
84 |
+
assert args.min_generate_audio_seconds <= args.max_generate_audio_seconds
|
85 |
+
|
86 |
+
# Init inspiremusic models from configs
|
87 |
+
use_cuda = args.gpu >= 0 and torch.cuda.is_available()
|
88 |
+
device = torch.device('cuda' if use_cuda else 'cpu')
|
89 |
+
with open(args.config, 'r') as f:
|
90 |
+
configs = load_hyperpyyaml(f)
|
91 |
+
|
92 |
+
model = InspireMusicModel(configs['llm'], configs['flow'], configs['hift'], configs['wavtokenizer'], args.fast, args.fp16)
|
93 |
+
|
94 |
+
model.load(args.llm_model, args.flow_model, args.music_tokenizer, args.wavtokenizer)
|
95 |
+
|
96 |
+
if args.llm_model is None:
|
97 |
+
model.llm = None
|
98 |
+
else:
|
99 |
+
model.llm = model.llm.to(torch.float32)
|
100 |
+
|
101 |
+
if args.flow_model is None:
|
102 |
+
model.flow = None
|
103 |
+
|
104 |
+
test_dataset = Dataset(args.prompt_data, data_pipeline=configs['data_pipeline'], mode='inference', shuffle=True, partition=False)
|
105 |
+
test_data_loader = DataLoader(test_dataset, batch_size=None, num_workers=0)
|
106 |
+
|
107 |
+
del configs
|
108 |
+
os.makedirs(args.result_dir, exist_ok=True)
|
109 |
+
fn = os.path.join(args.result_dir, 'wav.scp')
|
110 |
+
f = open(fn, 'w')
|
111 |
+
caption_fn = os.path.join(args.result_dir, 'captions.txt')
|
112 |
+
caption_f = open(caption_fn, 'w')
|
113 |
+
|
114 |
+
with torch.no_grad():
|
115 |
+
for _, batch in tqdm(enumerate(test_data_loader)):
|
116 |
+
utts = batch["utts"]
|
117 |
+
|
118 |
+
assert len(utts) == 1, "inference mode only support batchsize 1"
|
119 |
+
text_token = batch["text_token"].to(device)
|
120 |
+
text_token_len = batch["text_token_len"].to(device)
|
121 |
+
|
122 |
+
if "time_start" not in batch.keys():
|
123 |
+
batch["time_start"] = torch.randint(0, args.min_generate_audio_seconds, (1,)).to(torch.float64)
|
124 |
+
|
125 |
+
if batch["time_start"].numpy()[0] > 300:
|
126 |
+
batch["time_start"] = torch.Tensor([0]).to(torch.float64)
|
127 |
+
|
128 |
+
if "time_end" not in batch.keys():
|
129 |
+
batch["time_end"] = torch.randint(int(batch["time_start"].numpy()[0] + args.min_generate_audio_seconds), int(batch["time_start"].numpy()[0] + args.max_generate_audio_seconds), (1,)).to(torch.float64)
|
130 |
+
else:
|
131 |
+
if (batch["time_end"].numpy()[0] - batch["time_start"].numpy()[0]) < args.min_generate_audio_seconds:
|
132 |
+
batch["time_end"] = torch.randint(int(batch["time_start"].numpy()[0] + args.min_generate_audio_seconds), int(batch["time_start"].numpy()[0] + args.max_generate_audio_seconds), (1,)).to(torch.float64)
|
133 |
+
elif (batch["time_end"].numpy()[0] - batch["time_start"].numpy()[0]) > args.max_generate_audio_seconds:
|
134 |
+
batch["time_end"] = torch.Tensor([(batch["time_start"].numpy()[0] + args.max_generate_audio_seconds)]).to(torch.float64)
|
135 |
+
|
136 |
+
if "chorus" not in batch.keys():
|
137 |
+
batch["chorus"] = torch.randint(1, 5, (1,))
|
138 |
+
|
139 |
+
if args.chorus == "random":
|
140 |
+
batch["chorus"] = torch.randint(1, 5, (1,))
|
141 |
+
elif args.chorus == "intro":
|
142 |
+
batch["chorus"] = torch.Tensor([0])
|
143 |
+
elif "verse" in args.chorus:
|
144 |
+
batch["chorus"] = torch.Tensor([1])
|
145 |
+
elif args.chorus == "chorus":
|
146 |
+
batch["chorus"] = torch.Tensor([2])
|
147 |
+
elif args.chorus == "outro":
|
148 |
+
batch["chorus"] = torch.Tensor([4])
|
149 |
+
else:
|
150 |
+
batch["chorus"] = batch["chorus"]
|
151 |
+
|
152 |
+
time_start = batch["time_start"].to(device)
|
153 |
+
time_end = batch["time_end"].to(device)
|
154 |
+
chorus = batch["chorus"].to(torch.int)
|
155 |
+
|
156 |
+
text_prompt = f"<|{batch['time_start'].numpy()[0]}|><|{MUSIC_STRUCTURE_LABELS[chorus.numpy()[0]]}|><|{batch['text'][0]}|><|{batch['time_end'].numpy()[0]}|>"
|
157 |
+
chorus = chorus.to(device)
|
158 |
+
|
159 |
+
if batch["acoustic_token"] is None:
|
160 |
+
audio_token = None
|
161 |
+
audio_token_len = None
|
162 |
+
else:
|
163 |
+
audio_token = batch["acoustic_token"].to(device)
|
164 |
+
audio_token_len = batch["acoustic_token_len"].to(device)
|
165 |
+
|
166 |
+
text = batch["text"]
|
167 |
+
|
168 |
+
if "semantic_token" in batch:
|
169 |
+
token = batch["semantic_token"].to(device)
|
170 |
+
token_len = batch["semantic_token_len"].to(device)
|
171 |
+
else:
|
172 |
+
if audio_token is None:
|
173 |
+
token = None
|
174 |
+
token_len = None
|
175 |
+
else:
|
176 |
+
token = audio_token.view(audio_token.size(0), -1, 4)[:, :, 0]
|
177 |
+
token_len = audio_token_len / 4
|
178 |
+
|
179 |
+
if args.task in ['text-to-music', 'continuation']:
|
180 |
+
# text to music, music continuation
|
181 |
+
model_input = {"text": text, "audio_token": token,
|
182 |
+
"audio_token_len": token_len,
|
183 |
+
"text_token": text_token,
|
184 |
+
"text_token_len": text_token_len,
|
185 |
+
"embeddings": [time_start, time_end, chorus],
|
186 |
+
"raw_text": text,
|
187 |
+
"sample_rate": args.output_sample_rate,
|
188 |
+
"duration_to_gen": args.max_generate_audio_seconds,
|
189 |
+
"task": args.task}
|
190 |
+
elif args.task in ['reconstruct', 'super_resolution']:
|
191 |
+
# audio reconstruction, audio super resolution
|
192 |
+
model_input = {"text": text, "audio_token": audio_token,
|
193 |
+
"audio_token_len": audio_token_len,
|
194 |
+
"text_token": text_token,
|
195 |
+
"text_token_len": text_token_len,
|
196 |
+
"embeddings": [time_start, time_end, chorus],
|
197 |
+
"raw_text": text,
|
198 |
+
"sample_rate": args.output_sample_rate,
|
199 |
+
"duration_to_gen": args.max_generate_audio_seconds,
|
200 |
+
"task": args.task}
|
201 |
+
else:
|
202 |
+
# zero-shot
|
203 |
+
model_input = {'text' : text,
|
204 |
+
'text_len' : text_token_len,
|
205 |
+
'prompt_text' : text_token,
|
206 |
+
'prompt_text_len' : text_token_len,
|
207 |
+
'llm_prompt_audio_token' : token,
|
208 |
+
'llm_prompt_audio_token_len' : token_len,
|
209 |
+
'flow_prompt_audio_token' : audio_token,
|
210 |
+
'flow_prompt_audio_token_len': audio_token_len,
|
211 |
+
'prompt_audio_feat' : audio_feat,
|
212 |
+
'prompt_audio_feat_len' : audio_feat_len,
|
213 |
+
"embeddings" : [time_start,
|
214 |
+
time_end,
|
215 |
+
chorus]}
|
216 |
+
|
217 |
+
music_key = utts[0]
|
218 |
+
music_audios = []
|
219 |
+
music_fn = os.path.join(args.result_dir, f'{music_key}.{args.format}')
|
220 |
+
bench_start = time.time()
|
221 |
+
|
222 |
+
for model_output in model.inference(**model_input):
|
223 |
+
music_audios.append(model_output['music_audio'])
|
224 |
+
bench_end = time.time()
|
225 |
+
if args.trim:
|
226 |
+
music_audio = trim_audio(music_audios[0],
|
227 |
+
sample_rate=args.output_sample_rate,
|
228 |
+
threshold=0.05,
|
229 |
+
min_silence_duration=0.8)
|
230 |
+
else:
|
231 |
+
music_audio = music_audios[0]
|
232 |
+
if music_audio.shape[0] != 0:
|
233 |
+
if music_audio.shape[1] > max_generate_audio_length:
|
234 |
+
music_audio = music_audio[:, :max_generate_audio_length]
|
235 |
+
if music_audio.shape[1] >= min_generate_audio_length:
|
236 |
+
try:
|
237 |
+
if args.fade_out:
|
238 |
+
music_audio = fade_out(music_audio, args.output_sample_rate, args.fade_out_duration)
|
239 |
+
music_audio = music_audio.repeat(2, 1)
|
240 |
+
if args.format in ["wav", "flac"]:
|
241 |
+
torchaudio.save(music_fn, music_audio, sample_rate=args.output_sample_rate, encoding="PCM_S", bits_per_sample=24)
|
242 |
+
elif args.format in ["mp3", "m4a"]:
|
243 |
+
torchaudio.backend.sox_io_backend.save(filepath=music_fn, src=music_audio, sample_rate=args.output_sample_rate, format=args.format)
|
244 |
+
else:
|
245 |
+
logging.info(f"Format is not supported. Please choose from wav, mp3, m4a, flac.")
|
246 |
+
except Exception as e:
|
247 |
+
logging.info(f"Error saving file: {e}")
|
248 |
+
raise
|
249 |
+
|
250 |
+
audio_duration = music_audio.shape[1] / args.output_sample_rate
|
251 |
+
rtf = (bench_end - bench_start) / audio_duration
|
252 |
+
logging.info(f"processing time: {int(bench_end - bench_start)}s, audio length: {int(audio_duration)}s, rtf: {rtf}, text prompt: {text_prompt}")
|
253 |
+
f.write('{} {}\n'.format(music_key, music_fn))
|
254 |
+
f.flush()
|
255 |
+
caption_f.write('{}\t{}\n'.format(music_key, text_prompt))
|
256 |
+
caption_f.flush()
|
257 |
+
else:
|
258 |
+
logging.info(f"Generate audio length {music_audio.shape[1]} is shorter than min_generate_audio_length.")
|
259 |
+
else:
|
260 |
+
logging.info(f"Generate audio is empty, dim = {music_audio.shape[0]}.")
|
261 |
+
f.close()
|
262 |
+
logging.info('Result wav.scp saved in {}'.format(fn))
|
263 |
+
|
264 |
+
|
265 |
+
if __name__ == '__main__':
|
266 |
+
main()
|
inspiremusic/bin/train.py
ADDED
@@ -0,0 +1,194 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
|
15 |
+
from __future__ import print_function
|
16 |
+
import argparse
|
17 |
+
import datetime
|
18 |
+
import logging
|
19 |
+
logging.getLogger('matplotlib').setLevel(logging.WARNING)
|
20 |
+
from copy import deepcopy
|
21 |
+
import torch
|
22 |
+
import torch.distributed as dist
|
23 |
+
import deepspeed
|
24 |
+
import glob
|
25 |
+
import os
|
26 |
+
from hyperpyyaml import load_hyperpyyaml
|
27 |
+
from torch.cuda.amp import GradScaler, autocast
|
28 |
+
from torch.distributed.elastic.multiprocessing.errors import record
|
29 |
+
from peft import get_peft_config, get_peft_model, LoraConfig, TaskType
|
30 |
+
from inspiremusic.utils.executor import Executor
|
31 |
+
from inspiremusic.utils.train_utils import (
|
32 |
+
init_distributed,
|
33 |
+
init_dataset_and_dataloader,
|
34 |
+
init_optimizer_and_scheduler,
|
35 |
+
init_summarywriter, save_model,
|
36 |
+
wrap_cuda_model, check_modify_and_save_config)
|
37 |
+
|
38 |
+
|
39 |
+
def get_args():
|
40 |
+
parser = argparse.ArgumentParser(description='training your network')
|
41 |
+
parser.add_argument('--train_engine',
|
42 |
+
default='torch_ddp',
|
43 |
+
choices=['torch_ddp', 'deepspeed'],
|
44 |
+
help='Engine for paralleled training')
|
45 |
+
parser.add_argument('--model', required=True, help='model which will be trained')
|
46 |
+
parser.add_argument('--config', required=True, help='config file')
|
47 |
+
parser.add_argument('--train_data', required=True, help='train data file')
|
48 |
+
parser.add_argument('--cv_data', required=True, help='cv data file')
|
49 |
+
parser.add_argument('--checkpoint', help='checkpoint model')
|
50 |
+
parser.add_argument('--model_dir', required=True, help='save model dir')
|
51 |
+
parser.add_argument('--tensorboard_dir',
|
52 |
+
default='tensorboard',
|
53 |
+
help='tensorboard log dir')
|
54 |
+
parser.add_argument('--ddp.dist_backend',
|
55 |
+
dest='dist_backend',
|
56 |
+
default='nccl',
|
57 |
+
choices=['nccl', 'gloo'],
|
58 |
+
help='distributed backend')
|
59 |
+
parser.add_argument('--num_workers',
|
60 |
+
default=0,
|
61 |
+
type=int,
|
62 |
+
help='number of subprocess workers for reading')
|
63 |
+
parser.add_argument('--prefetch',
|
64 |
+
default=100,
|
65 |
+
type=int,
|
66 |
+
help='prefetch number')
|
67 |
+
parser.add_argument('--pin_memory',
|
68 |
+
action='store_true',
|
69 |
+
default=True,
|
70 |
+
help='Use pinned memory buffers used for reading')
|
71 |
+
parser.add_argument('--deepspeed.save_states',
|
72 |
+
dest='save_states',
|
73 |
+
default='model_only',
|
74 |
+
choices=['model_only', 'model+optimizer'],
|
75 |
+
help='save model/optimizer states')
|
76 |
+
parser.add_argument('--timeout',
|
77 |
+
default=30,
|
78 |
+
type=int,
|
79 |
+
help='timeout (in seconds) of inspiremusic_join.')
|
80 |
+
parser.add_argument('--fp16',
|
81 |
+
action='store_true',
|
82 |
+
default=False,
|
83 |
+
help='Enable fp16 mixed precision training')
|
84 |
+
parser.add_argument('--lora',
|
85 |
+
action='store_true',
|
86 |
+
default=False,
|
87 |
+
help='Enable LoRA training')
|
88 |
+
parser.add_argument('--lora_rank',
|
89 |
+
default=4,
|
90 |
+
type=int,
|
91 |
+
help='LoRA rank')
|
92 |
+
parser.add_argument('--lora_alpha',
|
93 |
+
default=16,
|
94 |
+
type=int,
|
95 |
+
help='LoRA alpha')
|
96 |
+
parser.add_argument('--lora_dropout',
|
97 |
+
default=0.1,
|
98 |
+
type=float,
|
99 |
+
help='LoRA dropout rate')
|
100 |
+
parser.add_argument('--lora_target_modules',
|
101 |
+
nargs='+',
|
102 |
+
default=["k_proj","v_proj"],
|
103 |
+
help='Target modules to apply LoRA (e.g., ["q_proj", "v_proj"])')
|
104 |
+
|
105 |
+
parser = deepspeed.add_config_arguments(parser)
|
106 |
+
args = parser.parse_args()
|
107 |
+
return args
|
108 |
+
|
109 |
+
|
110 |
+
@record
|
111 |
+
def main():
|
112 |
+
args = get_args()
|
113 |
+
logging.basicConfig(level=logging.DEBUG,
|
114 |
+
format='%(asctime)s %(levelname)s %(message)s')
|
115 |
+
|
116 |
+
override_dict = {k: None for k in ['llm', 'flow', 'hift'] if k != args.model}
|
117 |
+
with open(args.config, 'r') as f:
|
118 |
+
configs = load_hyperpyyaml(f, overrides=override_dict)
|
119 |
+
configs['train_conf'].update(vars(args))
|
120 |
+
|
121 |
+
# Init env for ddp
|
122 |
+
init_distributed(args)
|
123 |
+
|
124 |
+
# Get dataset & dataloader
|
125 |
+
train_dataset, cv_dataset, train_data_loader, cv_data_loader = \
|
126 |
+
init_dataset_and_dataloader(args, configs)
|
127 |
+
|
128 |
+
# Do some sanity checks and save config to arsg.model_dir
|
129 |
+
configs = check_modify_and_save_config(args, configs)
|
130 |
+
|
131 |
+
# Tensorboard summary
|
132 |
+
writer = init_summarywriter(args)
|
133 |
+
|
134 |
+
# load checkpoint
|
135 |
+
model = configs[args.model]
|
136 |
+
|
137 |
+
if args.checkpoint is not None:
|
138 |
+
model.load_state_dict(torch.load(args.checkpoint, map_location='cpu'))
|
139 |
+
else:
|
140 |
+
# Find and load the latest checkpoint
|
141 |
+
checkpoint_files = glob.glob(os.path.join(args.model_dir, '*.pt'))
|
142 |
+
|
143 |
+
if checkpoint_files:
|
144 |
+
latest_checkpoint = max(checkpoint_files, key=os.path.getctime)
|
145 |
+
logging.info(f"Loaded latest checkpoint from {latest_checkpoint}")
|
146 |
+
|
147 |
+
model.load_state_dict(torch.load(latest_checkpoint, map_location='cpu'))
|
148 |
+
|
149 |
+
if args.lora:
|
150 |
+
logging.info("Applying LoRA to the model...")
|
151 |
+
if not args.lora_target_modules:
|
152 |
+
raise ValueError("No target modules specified for LoRA. Please provide --lora_target_modules.")
|
153 |
+
lora_config = LoraConfig(
|
154 |
+
task_type="CAUSAL_LM", # Change to appropriate task type
|
155 |
+
inference_mode=False,
|
156 |
+
r=args.lora_rank,
|
157 |
+
lora_alpha=args.lora_alpha,
|
158 |
+
lora_dropout=args.lora_dropout,
|
159 |
+
target_modules=args.lora_target_modules
|
160 |
+
)
|
161 |
+
model.llm.model = get_peft_model(model.llm.model, lora_config)
|
162 |
+
# Optionally freeze the base model
|
163 |
+
else:
|
164 |
+
logging.info("LoRA is not enabled. Training the full model.")
|
165 |
+
|
166 |
+
# Dispatch model from cpu to gpu
|
167 |
+
model = wrap_cuda_model(args, model)
|
168 |
+
|
169 |
+
# Get optimizer & scheduler
|
170 |
+
model, optimizer, scheduler = init_optimizer_and_scheduler(args, configs, model)
|
171 |
+
|
172 |
+
# Initialize AMP for torch_ddp if fp16 is enabled
|
173 |
+
scaler = None
|
174 |
+
if args.fp16:
|
175 |
+
scaler = GradScaler()
|
176 |
+
logging.info("Initialized AMP GradScaler for mixed precision training.")
|
177 |
+
|
178 |
+
# Save init checkpoints
|
179 |
+
info_dict = deepcopy(configs['train_conf'])
|
180 |
+
|
181 |
+
# Get executor
|
182 |
+
executor = Executor()
|
183 |
+
|
184 |
+
# Start training loop
|
185 |
+
for epoch in range(info_dict['max_epoch']):
|
186 |
+
executor.epoch = epoch
|
187 |
+
train_dataset.set_epoch(epoch)
|
188 |
+
dist.barrier()
|
189 |
+
group_join = dist.new_group(backend="gloo", timeout=datetime.timedelta(seconds=args.timeout))
|
190 |
+
executor.train_one_epoch(model, optimizer, scheduler, train_data_loader, cv_data_loader, writer, info_dict, group_join, scaler=scaler)
|
191 |
+
dist.destroy_process_group(group_join)
|
192 |
+
|
193 |
+
if __name__ == '__main__':
|
194 |
+
main()
|
inspiremusic/cli/__init__.py
ADDED
File without changes
|
inspiremusic/cli/frontend.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
from functools import partial
|
15 |
+
import onnxruntime
|
16 |
+
import torch
|
17 |
+
import numpy as np
|
18 |
+
import whisper
|
19 |
+
from typing import Callable
|
20 |
+
import torchaudio.compliance.kaldi as kaldi
|
21 |
+
import torchaudio
|
22 |
+
import os
|
23 |
+
import re
|
24 |
+
import inflect
|
25 |
+
from inspiremusic.cli.model import InspireMusicModel
|
26 |
+
from inspiremusic.utils.frontend_utils import contains_chinese, replace_blank, replace_corner_mark, remove_bracket, spell_out_number, split_paragraph
|
27 |
+
from inspiremusic.wavtokenizer.decoder.pretrained import WavTokenizer
|
28 |
+
|
29 |
+
class InspireMusicFrontEnd:
|
30 |
+
def __init__(self,
|
31 |
+
configs: Callable,
|
32 |
+
get_tokenizer: Callable,
|
33 |
+
llm_model: str,
|
34 |
+
flow_model: str,
|
35 |
+
music_tokenizer_dir: str,
|
36 |
+
audio_tokenizer_dir: str,
|
37 |
+
instruct: bool = False,
|
38 |
+
fast: bool = False,
|
39 |
+
fp16: bool = True,
|
40 |
+
allowed_special: str = 'all'):
|
41 |
+
self.tokenizer = get_tokenizer()
|
42 |
+
self.audio_tokenizer_dir = audio_tokenizer_dir
|
43 |
+
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
44 |
+
|
45 |
+
self.bandwidth_id = torch.tensor([0]).to(self.device)
|
46 |
+
self.wavtokenizer = WavTokenizer.from_pretrained_feat(f"{audio_tokenizer_dir}/config.yaml", f"{audio_tokenizer_dir}/model.pt").to(self.device)
|
47 |
+
|
48 |
+
self.model = InspireMusicModel(configs['llm'], configs['flow'], configs['hift'], configs['wavtokenizer'], fast, fp16)
|
49 |
+
self.model = self.model.load(llm_model, flow_model, music_tokenizer_dir, audio_tokenizer_dir)
|
50 |
+
|
51 |
+
self.instruct = instruct
|
52 |
+
self.allowed_special = allowed_special
|
53 |
+
self.inflect_parser = inflect.engine()
|
54 |
+
|
55 |
+
def _extract_text_token(self, text):
|
56 |
+
text_token = self.tokenizer.encode(text, allowed_special=self.allowed_special)
|
57 |
+
text_token = torch.tensor([text_token], dtype=torch.int32).to(self.device)
|
58 |
+
text_token_len = torch.tensor([text_token.shape[1]], dtype=torch.int32).to(self.device)
|
59 |
+
return text_token, text_token_len
|
60 |
+
|
61 |
+
def _extract_audio_token(self, audio, sample_rate=24000):
|
62 |
+
audio = torch.tensor(audio, dtype=torch.float32, device=self.device)
|
63 |
+
_, audio_token = self.wavtokenizer.encode_infer(audio, bandwidth_id=self.bandwidth_id)
|
64 |
+
audio_token = audio_token.squeeze(0)
|
65 |
+
audio_token_len = torch.tensor([audio_token.shape[1]], dtype=torch.int32, device=self.device)
|
66 |
+
return audio_token, audio_token_len
|
67 |
+
|
68 |
+
def text_normalize(self, text, split=True):
|
69 |
+
text = text.strip()
|
70 |
+
if contains_chinese(text):
|
71 |
+
text = text.replace("\n", "")
|
72 |
+
text = replace_blank(text)
|
73 |
+
text = replace_corner_mark(text)
|
74 |
+
text = text.replace(".", "、")
|
75 |
+
text = text.replace(" - ", ",")
|
76 |
+
text = remove_bracket(text)
|
77 |
+
text = re.sub(r'[,,]+$', '。', text)
|
78 |
+
texts = list(split_paragraph(text, partial(self.tokenizer.encode, allowed_special=self.allowed_special), "zh", token_max_n=80,
|
79 |
+
token_min_n=60, merge_len=20, comma_split=False))
|
80 |
+
else:
|
81 |
+
text = spell_out_number(text, self.inflect_parser)
|
82 |
+
texts = list(split_paragraph(text, partial(self.tokenizer.encode, allowed_special=self.allowed_special), "en", token_max_n=80,
|
83 |
+
token_min_n=60, merge_len=20, comma_split=False))
|
84 |
+
if split is False:
|
85 |
+
return text
|
86 |
+
return texts
|
87 |
+
|
88 |
+
def frontend_text_to_music(self, text, time_start, time_end, chorus):
|
89 |
+
text_token, text_token_len = self._extract_text_token(text)
|
90 |
+
model_input = {"text": text, "audio_token": None, "audio_token_len": None,
|
91 |
+
"text_token": text_token, "text_token_len": text_token_len,
|
92 |
+
"embeddings": [time_start, time_end, chorus], "raw_text":text}
|
93 |
+
return model_input
|
94 |
+
|
95 |
+
def frontend_continuation(self, text, audio, time_start, time_end, chorus, target_sr=24000):
|
96 |
+
if text is None:
|
97 |
+
text_token = None
|
98 |
+
text_token_len = None
|
99 |
+
else:
|
100 |
+
text_token, text_token_len = self._extract_text_token(text)
|
101 |
+
audio_token, audio_token_len = self._extract_audio_token(audio, target_sr)
|
102 |
+
model_input = {"text": text, "audio_token": audio_token, "audio_token_len": audio_token_len,
|
103 |
+
"text_token": text_token, "text_token_len": text_token_len,
|
104 |
+
"embeddings": [time_start, time_end, chorus], "raw_text":text}
|
105 |
+
return model_input
|
106 |
+
|
inspiremusic/cli/inference.py
ADDED
@@ -0,0 +1,296 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
|
15 |
+
import os
|
16 |
+
import sys
|
17 |
+
import torchaudio
|
18 |
+
import time
|
19 |
+
import logging
|
20 |
+
import argparse
|
21 |
+
|
22 |
+
from modelscope import snapshot_download
|
23 |
+
from inspiremusic.cli.inspiremusic import InspireMusic
|
24 |
+
from inspiremusic.utils.file_utils import logging
|
25 |
+
import torch
|
26 |
+
from inspiremusic.utils.audio_utils import trim_audio, fade_out
|
27 |
+
from transformers import AutoModel
|
28 |
+
|
29 |
+
def set_env_variables():
|
30 |
+
os.environ['PYTHONIOENCODING'] = 'UTF-8'
|
31 |
+
os.environ['TOKENIZERS_PARALLELISM'] = 'False'
|
32 |
+
current_working_dir = os.getcwd()
|
33 |
+
main_root = os.path.realpath(os.path.join(current_working_dir, '../../'))
|
34 |
+
bin_dir = os.path.join(main_root, 'inspiremusic')
|
35 |
+
third_party_matcha_tts_path = os.path.join(main_root, 'third_party', 'Matcha-TTS')
|
36 |
+
python_path = f"{main_root}:{bin_dir}:{third_party_matcha_tts_path}:{os.environ.get('PYTHONPATH', '')}"
|
37 |
+
os.environ['PYTHONPATH'] = python_path
|
38 |
+
sys.path.extend([main_root, third_party_matcha_tts_path])
|
39 |
+
|
40 |
+
class InspireMusicUnified:
|
41 |
+
def __init__(self,
|
42 |
+
model_name: str = "InspireMusic-1.5B-Long",
|
43 |
+
model_dir: str = None,
|
44 |
+
min_generate_audio_seconds: float = 10.0,
|
45 |
+
max_generate_audio_seconds: float = 30.0,
|
46 |
+
sample_rate: int = 24000,
|
47 |
+
output_sample_rate: int = 48000,
|
48 |
+
load_jit: bool = True,
|
49 |
+
load_onnx: bool = False,
|
50 |
+
fast: bool = False,
|
51 |
+
fp16: bool = True,
|
52 |
+
gpu: int = 0,
|
53 |
+
result_dir: str = None):
|
54 |
+
os.environ['CUDA_VISIBLE_DEVICES'] = str(gpu)
|
55 |
+
|
56 |
+
# Set model_dir or default to downloading if it doesn't exist
|
57 |
+
self.model_dir = model_dir or f"../../pretrained_models/{model_name}"
|
58 |
+
if not os.path.exists(self.model_dir):
|
59 |
+
self.model_dir = snapshot_download(f"iic/{model_name}", cache_dir=self.model_dir)
|
60 |
+
|
61 |
+
self.sample_rate = sample_rate
|
62 |
+
self.output_sample_rate = 24000 if fast else output_sample_rate
|
63 |
+
self.result_dir = result_dir or f"exp/{model_name}"
|
64 |
+
os.makedirs(self.result_dir, exist_ok=True)
|
65 |
+
|
66 |
+
self.min_generate_audio_seconds = min_generate_audio_seconds
|
67 |
+
self.max_generate_audio_seconds = max_generate_audio_seconds
|
68 |
+
self.min_generate_audio_length = int(self.output_sample_rate * self.min_generate_audio_seconds)
|
69 |
+
self.max_generate_audio_length = int(self.output_sample_rate * self.max_generate_audio_seconds)
|
70 |
+
assert self.min_generate_audio_seconds <= self.max_generate_audio_seconds, "Min audio seconds must be less than or equal to max audio seconds"
|
71 |
+
|
72 |
+
use_cuda = gpu >= 0 and torch.cuda.is_available()
|
73 |
+
self.device = torch.device('cuda' if use_cuda else 'cpu')
|
74 |
+
self.model = InspireMusic(self.model_dir, load_jit=load_jit, load_onnx=load_onnx, fast=fast, fp16=fp16)
|
75 |
+
|
76 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
77 |
+
|
78 |
+
@torch.inference_mode()
|
79 |
+
def inference(self,
|
80 |
+
task: str = 'text-to-music',
|
81 |
+
text: str = None,
|
82 |
+
audio_prompt: str = None, # audio prompt file path
|
83 |
+
chorus: str = "verse",
|
84 |
+
time_start: float = 0.0,
|
85 |
+
time_end: float = 30.0,
|
86 |
+
output_fn: str = "output_audio",
|
87 |
+
max_audio_prompt_length: float = 5.0,
|
88 |
+
fade_out_duration: float = 1.0,
|
89 |
+
output_format: str = "wav",
|
90 |
+
fade_out_mode: bool = True,
|
91 |
+
trim: bool = False,
|
92 |
+
):
|
93 |
+
|
94 |
+
with torch.no_grad():
|
95 |
+
text_prompt = f"<|{time_start}|><|{chorus}|><|{text}|><|{time_end}|>"
|
96 |
+
chorus_dict = {"random": torch.randint(1, 5, (1,)).item(), "intro" : 0, "verse": 1, "chorus": 2, "outro": 4}
|
97 |
+
chorus = chorus_dict.get(chorus, 1)
|
98 |
+
chorus = torch.tensor([chorus], dtype=torch.int).to(self.device)
|
99 |
+
|
100 |
+
time_start_tensor = torch.tensor([time_start], dtype=torch.float64).to(self.device)
|
101 |
+
time_end_tensor = torch.tensor([time_end], dtype=torch.float64).to(self.device)
|
102 |
+
|
103 |
+
music_fn = os.path.join(self.result_dir, f'{output_fn}.{output_format}')
|
104 |
+
|
105 |
+
bench_start = time.time()
|
106 |
+
|
107 |
+
if task == 'text-to-music':
|
108 |
+
model_input = {
|
109 |
+
"text" : text,
|
110 |
+
"audio_prompt" : audio_prompt,
|
111 |
+
"time_start" : time_start_tensor,
|
112 |
+
"time_end" : time_end_tensor,
|
113 |
+
"chorus" : chorus,
|
114 |
+
"task" : task,
|
115 |
+
"stream" : False,
|
116 |
+
"duration_to_gen": self.max_generate_audio_seconds,
|
117 |
+
"sr" : self.sample_rate
|
118 |
+
}
|
119 |
+
elif task == 'continuation':
|
120 |
+
if audio_prompt is not None:
|
121 |
+
audio, _ = process_audio(audio_prompt, self.sample_rate)
|
122 |
+
if audio.size(1) < self.sample_rate:
|
123 |
+
logging.warning("Warning: Input prompt audio length is shorter than 1s. Please provide an appropriate length audio prompt and try again.")
|
124 |
+
audio = None
|
125 |
+
else:
|
126 |
+
max_audio_prompt_length_samples = int(max_audio_prompt_length * self.sample_rate)
|
127 |
+
audio = audio[:, :max_audio_prompt_length_samples] # Trimming prompt audio
|
128 |
+
|
129 |
+
model_input = {
|
130 |
+
"text" : text,
|
131 |
+
"audio_prompt" : audio,
|
132 |
+
"time_start" : time_start_tensor,
|
133 |
+
"time_end" : time_end_tensor,
|
134 |
+
"chorus" : chorus,
|
135 |
+
"task" : task,
|
136 |
+
"stream" : False,
|
137 |
+
"duration_to_gen": self.max_generate_audio_seconds,
|
138 |
+
"sr" : self.sample_rate
|
139 |
+
}
|
140 |
+
|
141 |
+
music_audios = []
|
142 |
+
for model_output in self.model.cli_inference(**model_input):
|
143 |
+
music_audios.append(model_output['music_audio'])
|
144 |
+
|
145 |
+
bench_end = time.time()
|
146 |
+
|
147 |
+
if trim:
|
148 |
+
music_audio = trim_audio(music_audios[0],
|
149 |
+
sample_rate=self.output_sample_rate,
|
150 |
+
threshold=0.05,
|
151 |
+
min_silence_duration=0.8)
|
152 |
+
else:
|
153 |
+
music_audio = music_audios[0]
|
154 |
+
|
155 |
+
if music_audio.shape[0] != 0:
|
156 |
+
if music_audio.shape[1] > self.max_generate_audio_length:
|
157 |
+
music_audio = music_audio[:, :self.max_generate_audio_length]
|
158 |
+
|
159 |
+
if music_audio.shape[1] >= self.min_generate_audio_length:
|
160 |
+
try:
|
161 |
+
if fade_out_mode:
|
162 |
+
music_audio = fade_out(music_audio, self.output_sample_rate, fade_out_duration)
|
163 |
+
|
164 |
+
music_audio = music_audio.repeat(2, 1)
|
165 |
+
|
166 |
+
if output_format in ["wav", "flac"]:
|
167 |
+
torchaudio.save(music_fn, music_audio,
|
168 |
+
sample_rate=self.output_sample_rate,
|
169 |
+
encoding="PCM_S",
|
170 |
+
bits_per_sample=24)
|
171 |
+
elif output_format in ["mp3", "m4a"]:
|
172 |
+
torchaudio.backend.sox_io_backend.save(
|
173 |
+
filepath=music_fn, src=music_audio,
|
174 |
+
sample_rate=self.output_sample_rate,
|
175 |
+
format=output_format)
|
176 |
+
else:
|
177 |
+
logging.info("Format is not supported. Please choose from wav, mp3, m4a, flac.")
|
178 |
+
|
179 |
+
except Exception as e:
|
180 |
+
logging.error(f"Error saving file: {e}")
|
181 |
+
raise
|
182 |
+
|
183 |
+
audio_duration = music_audio.shape[1] / self.output_sample_rate
|
184 |
+
rtf = (bench_end - bench_start) / audio_duration
|
185 |
+
logging.info(f"Processing time: {int(bench_end - bench_start)}s, audio length: {int(audio_duration)}s, rtf: {rtf}, text prompt: {text_prompt}")
|
186 |
+
|
187 |
+
else:
|
188 |
+
logging.error(f"Generated audio length is shorter than minimum required audio length.")
|
189 |
+
|
190 |
+
def get_args():
|
191 |
+
parser = argparse.ArgumentParser(description='Run inference with your model')
|
192 |
+
parser.add_argument('-m', '--model_name', default="InspireMusic-1.5B-Long",
|
193 |
+
help='Model name')
|
194 |
+
|
195 |
+
parser.add_argument('-d', '--model_dir',
|
196 |
+
help='Model folder path')
|
197 |
+
|
198 |
+
parser.add_argument('-t', '--text', default="Experience soothing and sensual instrumental jazz with a touch of Bossa Nova, perfect for a relaxing restaurant or spa ambiance.",
|
199 |
+
help='Prompt text')
|
200 |
+
|
201 |
+
parser.add_argument('-a', '--audio_prompt', default=None,
|
202 |
+
help='Prompt audio')
|
203 |
+
|
204 |
+
parser.add_argument('-c', '--chorus', default="intro",
|
205 |
+
help='Chorus tag generation mode (e.g., random, verse, chorus, intro, outro)')
|
206 |
+
|
207 |
+
parser.add_argument('-f', '--fast', type=bool, default=False,
|
208 |
+
help='Enable fast inference mode (without flow matching)')
|
209 |
+
|
210 |
+
parser.add_argument('-g', '--gpu', type=int, default=0,
|
211 |
+
help='GPU ID for this rank, -1 for CPU')
|
212 |
+
|
213 |
+
parser.add_argument('--task', default='text-to-music', choices=['text-to-music', 'continuation', 'reconstruct', 'super_resolution'],
|
214 |
+
help='Inference task type: text-to-music, continuation, reconstruct, super_resolution')
|
215 |
+
|
216 |
+
parser.add_argument('-r', '--result_dir', default="exp/inspiremusic",
|
217 |
+
help='Directory to save generated audio')
|
218 |
+
|
219 |
+
parser.add_argument('-o', '--output_fn', default="output_audio",
|
220 |
+
help='Output file name')
|
221 |
+
|
222 |
+
parser.add_argument('--format', type=str, default="wav", choices=["wav", "mp3", "m4a", "flac"],
|
223 |
+
help='Format of output audio')
|
224 |
+
|
225 |
+
parser.add_argument('--sample_rate', type=int, default=24000,
|
226 |
+
help='Sampling rate of input audio')
|
227 |
+
|
228 |
+
parser.add_argument('--output_sample_rate', type=int, default=48000, choices=[24000, 48000],
|
229 |
+
help='Sampling rate of generated output audio')
|
230 |
+
|
231 |
+
parser.add_argument('-s', '--time_start', type=float, default=0.0,
|
232 |
+
help='Start time in seconds')
|
233 |
+
|
234 |
+
parser.add_argument('-e', '--time_end', type=float, default=30.0,
|
235 |
+
help='End time in seconds')
|
236 |
+
|
237 |
+
parser.add_argument('--max_audio_prompt_length', type=float, default=5.0,
|
238 |
+
help='Maximum audio prompt length in seconds')
|
239 |
+
|
240 |
+
parser.add_argument('--min_generate_audio_seconds', type=float, default=10.0,
|
241 |
+
help='Minimum generated audio length in seconds')
|
242 |
+
|
243 |
+
parser.add_argument('--max_generate_audio_seconds', type=float, default=30.0,
|
244 |
+
help='Maximum generated audio length in seconds')
|
245 |
+
|
246 |
+
parser.add_argument('--fp16', type=bool, default=True,
|
247 |
+
help='Inference with fp16 model')
|
248 |
+
|
249 |
+
parser.add_argument('--fade_out', type=bool, default=True,
|
250 |
+
help='Apply fade out effect to generated audio')
|
251 |
+
|
252 |
+
parser.add_argument('--fade_out_duration', type=float, default=1.0,
|
253 |
+
help='Fade out duration in seconds')
|
254 |
+
|
255 |
+
parser.add_argument('--trim', type=bool, default=False,
|
256 |
+
help='Trim the silence ending of generated audio')
|
257 |
+
|
258 |
+
args = parser.parse_args()
|
259 |
+
|
260 |
+
if not args.model_dir:
|
261 |
+
args.model_dir = os.path.join("../../pretrained_models", args.model_name)
|
262 |
+
|
263 |
+
print(args)
|
264 |
+
return args
|
265 |
+
|
266 |
+
def main():
|
267 |
+
set_env_variables()
|
268 |
+
args = get_args()
|
269 |
+
model = InspireMusicUnified(model_name = args.model_name,
|
270 |
+
model_dir = args.model_dir,
|
271 |
+
min_generate_audio_seconds = args.min_generate_audio_seconds,
|
272 |
+
max_generate_audio_seconds = args.max_generate_audio_seconds,
|
273 |
+
sample_rate = args.sample_rate,
|
274 |
+
output_sample_rate = args.output_sample_rate,
|
275 |
+
load_jit = True,
|
276 |
+
load_onnx = False,
|
277 |
+
fast = args.fast,
|
278 |
+
fp16 = args.fp16,
|
279 |
+
gpu = args.gpu,
|
280 |
+
result_dir = args.result_dir)
|
281 |
+
|
282 |
+
model.inference(task = args.task,
|
283 |
+
text = args.text,
|
284 |
+
audio_prompt = args.audio_prompt,
|
285 |
+
chorus = args.chorus,
|
286 |
+
time_start = args.time_start,
|
287 |
+
time_end = args.time_end,
|
288 |
+
output_fn = args.output_fn,
|
289 |
+
max_audio_prompt_length = args.max_audio_prompt_length,
|
290 |
+
fade_out_duration = args.fade_out_duration,
|
291 |
+
output_format = args.format,
|
292 |
+
fade_out_mode = args.fade_out,
|
293 |
+
trim = args.trim)
|
294 |
+
|
295 |
+
if __name__ == "__main__":
|
296 |
+
main()
|
inspiremusic/cli/inspiremusic.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
import os
|
15 |
+
import time
|
16 |
+
from tqdm import tqdm
|
17 |
+
from hyperpyyaml import load_hyperpyyaml
|
18 |
+
from modelscope import snapshot_download
|
19 |
+
from inspiremusic.cli.frontend import InspireMusicFrontEnd
|
20 |
+
from inspiremusic.cli.model import InspireMusicModel
|
21 |
+
from inspiremusic.utils.file_utils import logging
|
22 |
+
import torch
|
23 |
+
|
24 |
+
class InspireMusic:
|
25 |
+
def __init__(self, model_dir, load_jit=True, load_onnx=False, fast = False, fp16=True):
|
26 |
+
instruct = True if '-Instruct' in model_dir else False
|
27 |
+
self.model_dir = model_dir
|
28 |
+
if not os.path.exists(model_dir):
|
29 |
+
model_dir = snapshot_download(model_dir)
|
30 |
+
with open('{}/inspiremusic.yaml'.format(model_dir), 'r') as f:
|
31 |
+
configs = load_hyperpyyaml(f)
|
32 |
+
|
33 |
+
self.frontend = InspireMusicFrontEnd(configs,
|
34 |
+
configs['get_tokenizer'],
|
35 |
+
'{}/llm.pt'.format(model_dir),
|
36 |
+
'{}/flow.pt'.format(model_dir),
|
37 |
+
'{}/music_tokenizer/'.format(model_dir),
|
38 |
+
'{}/wavtokenizer/'.format(model_dir),
|
39 |
+
instruct,
|
40 |
+
fast,
|
41 |
+
fp16,
|
42 |
+
configs['allowed_special'])
|
43 |
+
|
44 |
+
self.model = InspireMusicModel(configs['llm'], configs['flow'], configs['hift'], configs['wavtokenizer'], fast, fp16)
|
45 |
+
self.model.load('{}/llm.pt'.format(model_dir),
|
46 |
+
'{}/flow.pt'.format(model_dir),
|
47 |
+
'{}/music_tokenizer/'.format(model_dir),
|
48 |
+
'{}/wavtokenizer/model.pt'.format(model_dir))
|
49 |
+
del configs
|
50 |
+
|
51 |
+
@torch.inference_mode()
|
52 |
+
def inference(self, task, text, audio, time_start, time_end, chorus, stream=False, sr=24000):
|
53 |
+
if task == "text-to-music":
|
54 |
+
for i in tqdm(self.frontend.text_normalize(text, split=True)):
|
55 |
+
model_input = self.frontend.frontend_text_to_music(i, time_start, time_end, chorus)
|
56 |
+
start_time = time.time()
|
57 |
+
logging.info('prompt text {}'.format(i))
|
58 |
+
for model_output in self.model.inference(**model_input, stream=stream):
|
59 |
+
music_audios_len = model_output['music_audio'].shape[1] / sr
|
60 |
+
logging.info('yield music len {}, rtf {}'.format(music_audios_len, (time.time() - start_time) / music_audios_len))
|
61 |
+
yield model_output
|
62 |
+
start_time = time.time()
|
63 |
+
|
64 |
+
elif task == "continuation":
|
65 |
+
if text is None:
|
66 |
+
if audio is not None:
|
67 |
+
for i in tqdm(audio):
|
68 |
+
model_input = self.frontend.frontend_continuation(None, i, time_start, time_end, chorus, sr, max_audio_length)
|
69 |
+
start_time = time.time()
|
70 |
+
logging.info('prompt text {}'.format(i))
|
71 |
+
for model_output in self.model.continuation_inference(**model_input, stream=stream):
|
72 |
+
music_audios_len = model_output['music_audio'].shape[1] / sr
|
73 |
+
logging.info('yield music len {}, rtf {}'.format(music_audios_len, (time.time() - start_time) / music_audios_len))
|
74 |
+
yield model_output
|
75 |
+
start_time = time.time()
|
76 |
+
else:
|
77 |
+
if audio is not None:
|
78 |
+
for i in tqdm(self.frontend.text_normalize(text, split=True)):
|
79 |
+
model_input = self.frontend.frontend_continuation(i, audio, time_start, time_end, chorus, sr, max_audio_length)
|
80 |
+
start_time = time.time()
|
81 |
+
logging.info('prompt text {}'.format(i))
|
82 |
+
for model_output in self.model.continuation_inference(**model_input, stream=stream):
|
83 |
+
music_audios_len = model_output['music_audio'].shape[1] / sr
|
84 |
+
logging.info('yield music len {}, rtf {}'.format(music_audios_len, (time.time() - start_time) / music_audios_len))
|
85 |
+
yield model_output
|
86 |
+
start_time = time.time()
|
87 |
+
else:
|
88 |
+
print("Please input text or audio.")
|
89 |
+
else:
|
90 |
+
print("Currently only support text-to-music and music continuation tasks.")
|
91 |
+
|
92 |
+
@torch.inference_mode()
|
93 |
+
def cli_inference(self, text, audio_prompt, time_start, time_end, chorus, task, stream=False, duration_to_gen=30, sr=24000):
|
94 |
+
if task == "text-to-music":
|
95 |
+
model_input = self.frontend.frontend_text_to_music(text, time_start, time_end, chorus)
|
96 |
+
logging.info('prompt text {}'.format(text))
|
97 |
+
elif task == "continuation":
|
98 |
+
model_input = self.frontend.frontend_continuation(text, audio_prompt, time_start, time_end, chorus, sr)
|
99 |
+
logging.info('prompt audio length: {}'.format(len(audio_prompt)))
|
100 |
+
|
101 |
+
start_time = time.time()
|
102 |
+
for model_output in self.model.inference(**model_input, duration_to_gen=duration_to_gen, task=task):
|
103 |
+
music_audios_len = model_output['music_audio'].shape[1] / sr
|
104 |
+
logging.info('yield music len {}, rtf {}'.format(music_audios_len, (time.time() - start_time) / music_audios_len))
|
105 |
+
yield model_output
|
106 |
+
start_time = time.time()
|
107 |
+
|
108 |
+
@torch.inference_mode()
|
109 |
+
def inference_zero_shot(self, text, prompt_text, prompt_audio_16k, stream=False, sr=24000):
|
110 |
+
prompt_text = self.frontend.text_normalize(prompt_text, split=False)
|
111 |
+
for i in tqdm(self.frontend.text_normalize(text, split=True)):
|
112 |
+
model_input = self.frontend.frontend_zero_shot(i, prompt_text, prompt_audio_16k)
|
113 |
+
start_time = time.time()
|
114 |
+
logging.info('prompt text {}'.format(i))
|
115 |
+
for model_output in self.model.inference(**model_input, stream=stream):
|
116 |
+
audio_len = model_output['music_audio'].shape[1] / sr
|
117 |
+
logging.info('yield audio len {}, rtf {}'.format(audio_len, (time.time() - start_time) / audio_len))
|
118 |
+
yield model_output
|
119 |
+
start_time = time.time()
|
120 |
+
@torch.inference_mode()
|
121 |
+
def inference_instruct(self, text, spk_id, instruct_text, stream=False, sr=24000):
|
122 |
+
if self.frontend.instruct is False:
|
123 |
+
raise ValueError('{} do not support instruct inference'.format(self.model_dir))
|
124 |
+
instruct_text = self.frontend.text_normalize(instruct_text, split=False)
|
125 |
+
for i in tqdm(self.frontend.text_normalize(text, split=True)):
|
126 |
+
model_input = self.frontend.frontend_instruct(i, spk_id, instruct_text)
|
127 |
+
start_time = time.time()
|
128 |
+
logging.info('prompt text {}'.format(i))
|
129 |
+
for model_output in self.model.inference(**model_input, stream=stream):
|
130 |
+
audio_len = model_output['music_audio'].shape[1] / sr
|
131 |
+
logging.info('yield audio len {}, rtf {}'.format(audio_len, (time.time() - start_time) / audio_len))
|
132 |
+
yield model_output
|
133 |
+
start_time = time.time()
|
inspiremusic/cli/model.py
ADDED
@@ -0,0 +1,297 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
import torch
|
15 |
+
import numpy as np
|
16 |
+
import threading
|
17 |
+
import time
|
18 |
+
from contextlib import nullcontext
|
19 |
+
import uuid
|
20 |
+
from inspiremusic.utils.common import fade_in_out
|
21 |
+
from inspiremusic.music_tokenizer.vqvae import VQVAE
|
22 |
+
from inspiremusic.wavtokenizer.decoder.pretrained import WavTokenizer
|
23 |
+
from torch.cuda.amp import autocast
|
24 |
+
import logging
|
25 |
+
import torch
|
26 |
+
import os
|
27 |
+
|
28 |
+
|
29 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
30 |
+
|
31 |
+
class InspireMusicModel:
|
32 |
+
|
33 |
+
def __init__(self,
|
34 |
+
llm: torch.nn.Module,
|
35 |
+
flow: torch.nn.Module,
|
36 |
+
music_tokenizer: torch.nn.Module,
|
37 |
+
wavtokenizer: torch.nn.Module,
|
38 |
+
fast: bool = False,
|
39 |
+
fp16: bool = True,
|
40 |
+
):
|
41 |
+
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
42 |
+
self.llm = llm
|
43 |
+
self.flow = flow
|
44 |
+
self.music_tokenizer = music_tokenizer
|
45 |
+
self.wavtokenizer = wavtokenizer
|
46 |
+
self.fp16 = fp16
|
47 |
+
self.token_min_hop_len = 100
|
48 |
+
self.token_max_hop_len = 200
|
49 |
+
self.token_overlap_len = 20
|
50 |
+
# mel fade in out
|
51 |
+
self.mel_overlap_len = 34
|
52 |
+
self.mel_window = np.hamming(2 * self.mel_overlap_len)
|
53 |
+
# hift cache
|
54 |
+
self.mel_cache_len = 20
|
55 |
+
self.source_cache_len = int(self.mel_cache_len * 256)
|
56 |
+
# rtf and decoding related
|
57 |
+
self.stream_scale_factor = 1
|
58 |
+
assert self.stream_scale_factor >= 1, 'stream_scale_factor should be greater than 1, change it according to your actual rtf'
|
59 |
+
self.llm_context = torch.cuda.stream(torch.cuda.Stream(self.device)) if torch.cuda.is_available() else nullcontext()
|
60 |
+
self.lock = threading.Lock()
|
61 |
+
# dict used to store session related variable
|
62 |
+
self.music_token_dict = {}
|
63 |
+
self.llm_end_dict = {}
|
64 |
+
self.mel_overlap_dict = {}
|
65 |
+
self.fast = fast
|
66 |
+
self.generator = "hifi"
|
67 |
+
|
68 |
+
def load(self, llm_model, flow_model, hift_model, wavtokenizer_model):
|
69 |
+
if llm_model is not None:
|
70 |
+
self.llm.load_state_dict(torch.load(llm_model, map_location=self.device))
|
71 |
+
self.llm.to(self.device).eval()
|
72 |
+
else:
|
73 |
+
self.llm = None
|
74 |
+
if flow_model is not None:
|
75 |
+
self.flow.load_state_dict(torch.load(flow_model, map_location=self.device))
|
76 |
+
self.flow.to(self.device).eval()
|
77 |
+
if hift_model is not None:
|
78 |
+
if ".pt" not in hift_model:
|
79 |
+
self.music_tokenizer = VQVAE( hift_model + '/config.json',
|
80 |
+
hift_model + '/model.pt', with_encoder=True)
|
81 |
+
else:
|
82 |
+
self.music_tokenizer = VQVAE(os.path.dirname(hift_model) + '/config.json',
|
83 |
+
hift_model, with_encoder=True)
|
84 |
+
self.music_tokenizer.to(self.device).eval()
|
85 |
+
if wavtokenizer_model is not None:
|
86 |
+
if ".pt" not in wavtokenizer_model:
|
87 |
+
self.wavtokenizer = WavTokenizer.from_pretrained_feat( wavtokenizer_model + '/config.yaml',
|
88 |
+
wavtokenizer_model + '/model.pt')
|
89 |
+
else:
|
90 |
+
self.wavtokenizer = WavTokenizer.from_pretrained_feat( os.path.dirname(wavtokenizer_model) + '/config.yaml',
|
91 |
+
wavtokenizer_model )
|
92 |
+
self.wavtokenizer.to(self.device)
|
93 |
+
|
94 |
+
def load_jit(self, llm_text_encoder_model, llm_llm_model, flow_encoder_model):
|
95 |
+
assert self.fp16 is True, "we only provide fp16 jit model, set fp16=True if you want to use jit model"
|
96 |
+
llm_text_encoder = torch.jit.load(llm_text_encoder_model, map_location=self.device)
|
97 |
+
self.llm.text_encoder = llm_text_encoder
|
98 |
+
llm_llm = torch.jit.load(llm_llm_model)
|
99 |
+
self.llm.llm = llm_llm
|
100 |
+
flow_encoder = torch.jit.load(flow_encoder_model)
|
101 |
+
self.flow.encoder = flow_encoder
|
102 |
+
|
103 |
+
def load_onnx(self, flow_decoder_estimator_model):
|
104 |
+
import onnxruntime
|
105 |
+
option = onnxruntime.SessionOptions()
|
106 |
+
option.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL
|
107 |
+
option.intra_op_num_threads = 1
|
108 |
+
providers = ['CUDAExecutionProvider' if torch.cuda.is_available() else 'CPUExecutionProvider']
|
109 |
+
del self.flow.decoder.estimator
|
110 |
+
self.flow.decoder.estimator = onnxruntime.InferenceSession(flow_decoder_estimator_model, sess_options=option, providers=providers)
|
111 |
+
|
112 |
+
def llm_job(self, text, audio_token, audio_token_len, prompt_text, llm_prompt_audio_token, embeddings, uuid, duration_to_gen, task):
|
113 |
+
with self.llm_context:
|
114 |
+
local_res = []
|
115 |
+
with autocast(enabled=self.fp16):
|
116 |
+
inference_kwargs = {
|
117 |
+
'text': text.to(self.device),
|
118 |
+
'text_len': torch.tensor([text.shape[1]], dtype=torch.int32).to(self.device),
|
119 |
+
'prompt_text': prompt_text.to(self.device),
|
120 |
+
'prompt_text_len': torch.tensor([prompt_text.shape[1]], dtype=torch.int32).to(self.device),
|
121 |
+
'prompt_audio_token': llm_prompt_audio_token.to(self.device),
|
122 |
+
'prompt_audio_token_len': torch.tensor([llm_prompt_audio_token.shape[1]], dtype=torch.int32).to(self.device),
|
123 |
+
'embeddings': embeddings,
|
124 |
+
'duration_to_gen': duration_to_gen,
|
125 |
+
'task': task
|
126 |
+
}
|
127 |
+
|
128 |
+
if audio_token is not None:
|
129 |
+
inference_kwargs['audio_token'] = audio_token.to(self.device)
|
130 |
+
else:
|
131 |
+
inference_kwargs['audio_token'] = torch.Tensor([0]).to(self.device)
|
132 |
+
|
133 |
+
if audio_token_len is not None:
|
134 |
+
inference_kwargs['audio_token_len'] = audio_token_len.to(self.device)
|
135 |
+
else:
|
136 |
+
inference_kwargs['audio_token_len'] = torch.Tensor([0]).to(self.device)
|
137 |
+
|
138 |
+
for i in self.llm.inference(**inference_kwargs):
|
139 |
+
local_res.append(i)
|
140 |
+
|
141 |
+
self.music_token_dict[uuid] = local_res
|
142 |
+
self.llm_end_dict[uuid] = True
|
143 |
+
|
144 |
+
# def token2wav(self, token, token_len, text, text_len, uuid, sample_rate, finalize=False):
|
145 |
+
def token2wav(self, token, token_len, uuid, sample_rate, finalize=False, flow_cfg=None):
|
146 |
+
# if self.flow is not None:
|
147 |
+
# if isinstance(self.flow,MaskedDiffWithText):
|
148 |
+
# codec_embed = self.flow.inference(token=token.to(self.device),
|
149 |
+
# token_len=token_len.to(self.device),
|
150 |
+
# text_token=text,
|
151 |
+
# text_token_len=text_len,
|
152 |
+
# )
|
153 |
+
# else:
|
154 |
+
if flow_cfg is not None:
|
155 |
+
codec_embed = self.flow.inference_cfg(token=token.to(self.device),
|
156 |
+
token_len=token_len.to(self.device),
|
157 |
+
sample_rate=sample_rate
|
158 |
+
)
|
159 |
+
else:
|
160 |
+
codec_embed = self.flow.inference(token=token.to(self.device),
|
161 |
+
token_len=token_len.to(self.device),
|
162 |
+
sample_rate=sample_rate
|
163 |
+
)
|
164 |
+
# use music_tokenizer decoder
|
165 |
+
wav = self.music_tokenizer.generator(codec_embed)
|
166 |
+
wav = wav.squeeze(0).cpu().detach()
|
167 |
+
return wav
|
168 |
+
|
169 |
+
def acoustictoken2wav(self, token):
|
170 |
+
# use music_tokenizer to generate waveform from token
|
171 |
+
token = token.view(token.size(0), -1, 4)
|
172 |
+
# codec = token.view(1, -1, 4)
|
173 |
+
codec_embed = self.music_tokenizer.quantizer.embed(torch.tensor(token).long().to(self.device)).cuda()
|
174 |
+
wav = self.music_tokenizer.generator(codec_embed)
|
175 |
+
wav = wav.squeeze(0).cpu().detach()
|
176 |
+
return wav
|
177 |
+
|
178 |
+
def semantictoken2wav(self, token):
|
179 |
+
# fast mode, use wavtokenizer decoder
|
180 |
+
new_tensor = torch.tensor(token.to(self.device)).unsqueeze(0)
|
181 |
+
features = self.wavtokenizer.codes_to_features(new_tensor)
|
182 |
+
bandwidth_id = torch.tensor([0]).to(self.device)
|
183 |
+
wav = self.wavtokenizer.to(self.device).decode(features, bandwidth_id=bandwidth_id)
|
184 |
+
wav = wav.cpu().detach()
|
185 |
+
return wav
|
186 |
+
|
187 |
+
@torch.inference_mode()
|
188 |
+
def inference(self, text, audio_token, audio_token_len, text_token, text_token_len, embeddings=None,
|
189 |
+
prompt_text=torch.zeros(1, 0, dtype=torch.int32),
|
190 |
+
llm_prompt_audio_token=torch.zeros(1, 0, dtype=torch.int32),
|
191 |
+
flow_prompt_audio_token=torch.zeros(1, 0, dtype=torch.int32),
|
192 |
+
prompt_audio_feat=torch.zeros(1, 0, 80), sample_rate=48000, duration_to_gen = 30, task="continuation", trim = True, stream=False, **kwargs):
|
193 |
+
|
194 |
+
# this_uuid is used to track variables related to this inference thread
|
195 |
+
# support tasks:
|
196 |
+
# text to music task
|
197 |
+
# music continuation task
|
198 |
+
# require either audio input only or text and audio inputs
|
199 |
+
|
200 |
+
this_uuid = str(uuid.uuid1())
|
201 |
+
|
202 |
+
if self.llm:
|
203 |
+
with self.lock:
|
204 |
+
self.music_token_dict[this_uuid], self.llm_end_dict[this_uuid] = [], False
|
205 |
+
|
206 |
+
p = threading.Thread(target=self.llm_job, args=(text_token, audio_token, audio_token_len, prompt_text, llm_prompt_audio_token, embeddings, this_uuid, duration_to_gen, task))
|
207 |
+
p.start()
|
208 |
+
|
209 |
+
if stream is True:
|
210 |
+
token_hop_len = self.token_min_hop_len
|
211 |
+
while True:
|
212 |
+
time.sleep(0.1)
|
213 |
+
if len(self.music_token_dict[this_uuid]) >= token_hop_len + self.token_overlap_len:
|
214 |
+
this_music_audio = self.token2wav(token=text_token,
|
215 |
+
token_len=text_token_len,
|
216 |
+
uuid=this_uuid,
|
217 |
+
sample_rate=sample_rate,
|
218 |
+
finalize=False)
|
219 |
+
yield {'music_audio': this_music_audio.cpu()}
|
220 |
+
with self.lock:
|
221 |
+
self.music_token_dict[this_uuid] = self.music_token_dict[this_uuid][token_hop_len:]
|
222 |
+
# increase token_hop_len for better audio quality
|
223 |
+
token_hop_len = min(self.token_max_hop_len, int(token_hop_len * self.stream_scale_factor))
|
224 |
+
if self.llm_end_dict[this_uuid] is True and len(self.music_token_dict[this_uuid]) < token_hop_len + self.token_overlap_len:
|
225 |
+
break
|
226 |
+
p.join()
|
227 |
+
# deal with remain tokens, make sure inference remain token len equals token_hop_len when cache_speech is not None
|
228 |
+
this_music_token = torch.concat(self.music_token_dict[this_uuid], dim=1)
|
229 |
+
with self.flow_hift_context:
|
230 |
+
this_music_audio = self.token2wav(token=this_music_token,
|
231 |
+
prompt_token=flow_prompt_audio_token,
|
232 |
+
prompt_feat=prompt_audio_feat,
|
233 |
+
embedding=flow_embedding,
|
234 |
+
uuid=this_uuid,
|
235 |
+
sample_rate=sample_rate,
|
236 |
+
finalize=True)
|
237 |
+
yield {'music_audio': this_music_audio.cpu()}
|
238 |
+
else:
|
239 |
+
# deal with all tokens
|
240 |
+
if self.fast:
|
241 |
+
if task == "reconstruct":
|
242 |
+
assert audio_token is None
|
243 |
+
this_music_token = audio_token
|
244 |
+
this_music_audio = self.acoustictoken2wav(token=this_music_token)
|
245 |
+
else:
|
246 |
+
if self.llm:
|
247 |
+
p.join()
|
248 |
+
print(len(self.music_token_dict[this_uuid]))
|
249 |
+
this_music_token = torch.concat(self.music_token_dict[this_uuid], dim=1)
|
250 |
+
print(this_music_token.shape)
|
251 |
+
else:
|
252 |
+
this_music_token = text_token
|
253 |
+
|
254 |
+
logging.info("using wavtokenizer generator without flow matching")
|
255 |
+
this_music_audio = self.semantictoken2wav(token=this_music_token)
|
256 |
+
print(this_music_audio.shape)
|
257 |
+
|
258 |
+
else:
|
259 |
+
if self.llm:
|
260 |
+
p.join()
|
261 |
+
if len(self.music_token_dict[this_uuid]) != 0:
|
262 |
+
this_music_token = torch.concat(self.music_token_dict[this_uuid], dim=1)
|
263 |
+
else:
|
264 |
+
print(f"The list of tensors is empty for UUID: {this_uuid}")
|
265 |
+
else:
|
266 |
+
this_music_token = text_token
|
267 |
+
logging.info(f"LLM generated audio token length: {this_music_token.shape[1]}")
|
268 |
+
logging.info(f"using flow matching and {self.generator} generator")
|
269 |
+
|
270 |
+
if self.generator == "hifi":
|
271 |
+
if (embeddings[1] - embeddings[0]) <= duration_to_gen:
|
272 |
+
if trim:
|
273 |
+
trim_length = (int((embeddings[1] - embeddings[0])*75))
|
274 |
+
this_music_token = this_music_token[:, :trim_length]
|
275 |
+
logging.info(f"After trimmed, generated audio token length: {this_music_token.shape[1]}")
|
276 |
+
elif (embeddings[1] - embeddings[0]) < 1:
|
277 |
+
logging.info(f"Given audio length={(embeddings[1] - embeddings[0])}, which is too short, please give a longer audio length.")
|
278 |
+
|
279 |
+
this_music_audio = self.token2wav(token=this_music_token,
|
280 |
+
token_len=torch.LongTensor([this_music_token.size(1)]),
|
281 |
+
uuid=this_uuid,
|
282 |
+
sample_rate=sample_rate,
|
283 |
+
finalize=True)
|
284 |
+
logging.info(f"Generated audio sequence length: {this_music_audio.shape[1]}")
|
285 |
+
elif self.generator == "wavtokenizer":
|
286 |
+
if (embeddings[1] - embeddings[0]) < duration_to_gen:
|
287 |
+
if trim:
|
288 |
+
trim_length = (int((embeddings[1] - embeddings[0])*75))
|
289 |
+
this_music_token = this_music_token[:,:trim_length]
|
290 |
+
logging.info(f"After trimmed, generated audio token length: {this_music_token.shape[1]}")
|
291 |
+
elif (embeddings[1] - embeddings[0]) < 1:
|
292 |
+
logging.info(f"Given audio length={(embeddings[1] - embeddings[0])}, which is too short, please give a longer audio length.")
|
293 |
+
|
294 |
+
this_music_audio = self.semantictoken2wav(token=this_music_token)
|
295 |
+
|
296 |
+
yield {'music_audio': this_music_audio.cpu()}
|
297 |
+
torch.cuda.synchronize()
|
inspiremusic/dataset/__init__.py
ADDED
File without changes
|
inspiremusic/dataset/dataset.py
ADDED
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2021 Mobvoi Inc. (authors: Binbin Zhang)
|
2 |
+
# 2024 Alibaba Inc
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
|
16 |
+
import random
|
17 |
+
import json
|
18 |
+
import math
|
19 |
+
from functools import partial
|
20 |
+
|
21 |
+
import torch
|
22 |
+
import torch.distributed as dist
|
23 |
+
from torch.utils.data import IterableDataset
|
24 |
+
from inspiremusic.utils.file_utils import read_lists, read_json_lists
|
25 |
+
|
26 |
+
class Processor(IterableDataset):
|
27 |
+
|
28 |
+
def __init__(self, source, f, *args, **kw):
|
29 |
+
assert callable(f)
|
30 |
+
self.source = source
|
31 |
+
self.f = f
|
32 |
+
self.args = args
|
33 |
+
self.kw = kw
|
34 |
+
|
35 |
+
def set_epoch(self, epoch):
|
36 |
+
self.source.set_epoch(epoch)
|
37 |
+
|
38 |
+
def __iter__(self):
|
39 |
+
""" Return an iterator over the source dataset processed by the
|
40 |
+
given processor.
|
41 |
+
"""
|
42 |
+
assert self.source is not None
|
43 |
+
assert callable(self.f)
|
44 |
+
return self.f(iter(self.source), *self.args, **self.kw)
|
45 |
+
|
46 |
+
def apply(self, f):
|
47 |
+
assert callable(f)
|
48 |
+
return Processor(self, f, *self.args, **self.kw)
|
49 |
+
|
50 |
+
|
51 |
+
class DistributedSampler:
|
52 |
+
|
53 |
+
def __init__(self, shuffle=True, partition=True):
|
54 |
+
self.epoch = -1
|
55 |
+
self.update()
|
56 |
+
self.shuffle = shuffle
|
57 |
+
self.partition = partition
|
58 |
+
|
59 |
+
def update(self):
|
60 |
+
assert dist.is_available()
|
61 |
+
if dist.is_initialized():
|
62 |
+
self.rank = dist.get_rank()
|
63 |
+
self.world_size = dist.get_world_size()
|
64 |
+
else:
|
65 |
+
self.rank = 0
|
66 |
+
self.world_size = 1
|
67 |
+
worker_info = torch.utils.data.get_worker_info()
|
68 |
+
if worker_info is None:
|
69 |
+
self.worker_id = 0
|
70 |
+
self.num_workers = 1
|
71 |
+
else:
|
72 |
+
self.worker_id = worker_info.id
|
73 |
+
self.num_workers = worker_info.num_workers
|
74 |
+
return dict(rank=self.rank,
|
75 |
+
world_size=self.world_size,
|
76 |
+
worker_id=self.worker_id,
|
77 |
+
num_workers=self.num_workers)
|
78 |
+
|
79 |
+
def set_epoch(self, epoch):
|
80 |
+
self.epoch = epoch
|
81 |
+
|
82 |
+
def sample(self, data):
|
83 |
+
""" Sample data according to rank/world_size/num_workers
|
84 |
+
|
85 |
+
Args:
|
86 |
+
data(List): input data list
|
87 |
+
|
88 |
+
Returns:
|
89 |
+
List: data list after sample
|
90 |
+
"""
|
91 |
+
data = list(range(len(data)))
|
92 |
+
# force datalist even
|
93 |
+
|
94 |
+
if self.partition:
|
95 |
+
if self.shuffle:
|
96 |
+
random.Random(self.epoch).shuffle(data)
|
97 |
+
if len(data) < self.world_size:
|
98 |
+
print(len(data), self.world_size)
|
99 |
+
data = data * math.ceil(self.world_size / len(data))
|
100 |
+
data = data[:self.world_size]
|
101 |
+
data = data[self.rank::self.world_size]
|
102 |
+
if len(data) < self.num_workers:
|
103 |
+
data = data * math.ceil(self.num_workers / len(data))
|
104 |
+
data = data[:self.num_workers]
|
105 |
+
data = data[self.worker_id::self.num_workers]
|
106 |
+
return data
|
107 |
+
|
108 |
+
|
109 |
+
class DataList(IterableDataset):
|
110 |
+
|
111 |
+
def __init__(self, lists, shuffle=True, partition=True):
|
112 |
+
self.lists = lists
|
113 |
+
self.sampler = DistributedSampler(shuffle, partition)
|
114 |
+
|
115 |
+
def set_epoch(self, epoch):
|
116 |
+
self.sampler.set_epoch(epoch)
|
117 |
+
|
118 |
+
def __iter__(self):
|
119 |
+
sampler_info = self.sampler.update()
|
120 |
+
indexes = self.sampler.sample(self.lists)
|
121 |
+
for index in indexes:
|
122 |
+
data = dict(src=self.lists[index])
|
123 |
+
data.update(sampler_info)
|
124 |
+
yield data
|
125 |
+
|
126 |
+
|
127 |
+
def Dataset(data_list_file,
|
128 |
+
data_pipeline,
|
129 |
+
mode='train',
|
130 |
+
shuffle=True,
|
131 |
+
partition=True
|
132 |
+
):
|
133 |
+
""" Construct dataset from arguments
|
134 |
+
|
135 |
+
We have two shuffle stage in the Dataset. The first is global
|
136 |
+
shuffle at shards tar/raw file level. The second is global shuffle
|
137 |
+
at training samples level.
|
138 |
+
|
139 |
+
Args:
|
140 |
+
data_type(str): raw/shard
|
141 |
+
tokenizer (BaseTokenizer): tokenizer to tokenize
|
142 |
+
partition(bool): whether to do data partition in terms of rank
|
143 |
+
"""
|
144 |
+
assert mode in ['train', 'inference', 'processing']
|
145 |
+
lists = read_lists(data_list_file)
|
146 |
+
|
147 |
+
dataset = DataList(lists,
|
148 |
+
shuffle=shuffle,
|
149 |
+
partition=partition)
|
150 |
+
|
151 |
+
for func in data_pipeline:
|
152 |
+
dataset = Processor(dataset, func, mode=mode)
|
153 |
+
|
154 |
+
return dataset
|
inspiremusic/dataset/processor.py
ADDED
@@ -0,0 +1,595 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
import logging
|
15 |
+
import random
|
16 |
+
|
17 |
+
import pyarrow.parquet as pq
|
18 |
+
import torch
|
19 |
+
import torchaudio
|
20 |
+
from torch.nn.utils.rnn import pad_sequence
|
21 |
+
import torch.nn.functional as F
|
22 |
+
import numpy as np
|
23 |
+
import re
|
24 |
+
|
25 |
+
torchaudio.set_audio_backend('soundfile')
|
26 |
+
|
27 |
+
AUDIO_FORMAT_SETS = {'flac', 'mp3', 'm4a', 'ogg', 'opus', 'wav', 'wma'}
|
28 |
+
CHORUS = {"intro": 0, "chorus": 1, "verse1": 2, "verse2": 3, "verse": 2,
|
29 |
+
"outro": 4}
|
30 |
+
|
31 |
+
metadata_pattern = re.compile(r'^\[(ti|ar|al|by|offset):.*\]$')
|
32 |
+
timestamp_pattern = re.compile(r'^\[\d{2}:\d{2}\.\d{2}\](.*)$')
|
33 |
+
|
34 |
+
|
35 |
+
def parquet_opener(data, mode='train', audio_data={}):
|
36 |
+
""" Give url or local file, return file descriptor
|
37 |
+
Inplace operation.
|
38 |
+
|
39 |
+
Args:
|
40 |
+
data(Iterable[str]): url or local file list
|
41 |
+
|
42 |
+
Returns:
|
43 |
+
Iterable[{src, stream}]
|
44 |
+
"""
|
45 |
+
for sample in data:
|
46 |
+
assert 'src' in sample
|
47 |
+
|
48 |
+
url = sample['src']
|
49 |
+
try:
|
50 |
+
df = pq.read_table(url).to_pandas()
|
51 |
+
for i in df.index:
|
52 |
+
sample.update(dict(df.loc[i]))
|
53 |
+
yield {**sample}
|
54 |
+
except Exception as ex:
|
55 |
+
logging.warning('Failed to open {}, ex info {}'.format(url, ex))
|
56 |
+
|
57 |
+
|
58 |
+
def clean_lyrics(data, mode="train"):
|
59 |
+
for sample in data:
|
60 |
+
lyrics = sample["text"]
|
61 |
+
cleaned = []
|
62 |
+
for line in lyrics.splitlines():
|
63 |
+
if metadata_pattern.match(line):
|
64 |
+
continue
|
65 |
+
timestamp_match = timestamp_pattern.match(line)
|
66 |
+
if timestamp_match:
|
67 |
+
lyric = timestamp_match.group(1).strip()
|
68 |
+
if lyric:
|
69 |
+
cleaned.append(lyric)
|
70 |
+
else:
|
71 |
+
if line.strip():
|
72 |
+
cleaned.append(line.strip())
|
73 |
+
sample["text"] = '\n'.join(cleaned)
|
74 |
+
yield sample
|
75 |
+
|
76 |
+
|
77 |
+
def cut_by_length(data, max_length=8000, num_times=4, mode="train"):
|
78 |
+
for sample in data:
|
79 |
+
if "semantic_token" in sample:
|
80 |
+
sample["semantic_token"] = [
|
81 |
+
sample["semantic_token"][0][:max_length]]
|
82 |
+
if "acoustic_token" not in sample:
|
83 |
+
sample["acoustic_token"] = sample["speech_token"]
|
84 |
+
sample["acoustic_token"] = sample["acoustic_token"][
|
85 |
+
:max_length * num_times]
|
86 |
+
|
87 |
+
yield sample
|
88 |
+
|
89 |
+
|
90 |
+
def filter(data,
|
91 |
+
max_length=22500, # 22500 #5min #10240
|
92 |
+
max_acoustic_length=45000,
|
93 |
+
min_length=10,
|
94 |
+
min_acoustic_length=150,
|
95 |
+
token_max_length=200,
|
96 |
+
token_min_length=1,
|
97 |
+
min_output_input_ratio=0.0005,
|
98 |
+
max_output_input_ratio=1,
|
99 |
+
mode='train'):
|
100 |
+
""" Filter sample according to feature and label length
|
101 |
+
Inplace operation.
|
102 |
+
|
103 |
+
Args::
|
104 |
+
data: Iterable[{key, wav, label, sample_rate}]
|
105 |
+
max_length: drop utterance which is greater than max_length(10ms)
|
106 |
+
min_length: drop utterance which is less than min_length(10ms)
|
107 |
+
token_max_length: drop utterance which is greater than
|
108 |
+
token_max_length, especially when use char unit for
|
109 |
+
english modeling
|
110 |
+
token_min_length: drop utterance which is
|
111 |
+
less than token_max_length
|
112 |
+
min_output_input_ratio: minimal ration of
|
113 |
+
token_length / feats_length(10ms)
|
114 |
+
max_output_input_ratio: maximum ration of
|
115 |
+
token_length / feats_length(10ms)
|
116 |
+
|
117 |
+
Returns:
|
118 |
+
Iterable[{key, wav, label, sample_rate}]
|
119 |
+
"""
|
120 |
+
if mode == "train":
|
121 |
+
for sample in data:
|
122 |
+
if "semantic_token" in sample:
|
123 |
+
new_sample_frames = sample['semantic_token'][0].shape[0]
|
124 |
+
else:
|
125 |
+
new_sample_frames = sample['speech_token']
|
126 |
+
|
127 |
+
if "text_token" in sample:
|
128 |
+
new_sample_frames += len(sample['text_token'])
|
129 |
+
|
130 |
+
if new_sample_frames > max_length or new_sample_frames < min_length:
|
131 |
+
print(f"skipped 1 item length={new_sample_frames}")
|
132 |
+
continue
|
133 |
+
|
134 |
+
sample["chorus"] = sample["chorus"].split(",")
|
135 |
+
if not isinstance(sample["time_start"], np.ndarray):
|
136 |
+
sample["time_start"] = [sample["time_start"]]
|
137 |
+
sample["time_end"] = [sample["time_end"]]
|
138 |
+
for i, t in enumerate(sample["chorus"]):
|
139 |
+
if sample["chorus"][i] == "verse":
|
140 |
+
sample["chorus"][i] = "verse1"
|
141 |
+
|
142 |
+
yield sample
|
143 |
+
|
144 |
+
if mode == "train_flow":
|
145 |
+
for sample in data:
|
146 |
+
if "semantic_token" in sample:
|
147 |
+
new_sample_frames = sample['semantic_token'][0].shape[0]
|
148 |
+
if "acoustic_token" in sample:
|
149 |
+
target_sample_frames = sample['acoustic_token'][0].shape[0]
|
150 |
+
|
151 |
+
if new_sample_frames > max_length or new_sample_frames < min_acoustic_length or new_sample_frames < min_length or target_sample_frames > max_acoustic_length:
|
152 |
+
print(
|
153 |
+
f"skipped 1 item length={new_sample_frames}, target_length={target_sample_frames}")
|
154 |
+
continue
|
155 |
+
|
156 |
+
yield sample
|
157 |
+
|
158 |
+
elif mode == "inference":
|
159 |
+
for sample in data:
|
160 |
+
yield sample
|
161 |
+
|
162 |
+
|
163 |
+
def resample(data, resample_rate=22050, min_sample_rate=16000, mode='train'):
|
164 |
+
""" Resample data.
|
165 |
+
Inplace operation.
|
166 |
+
|
167 |
+
Args:
|
168 |
+
data: Iterable[{key, wav, label, sample_rate}]
|
169 |
+
resample_rate: target resample rate
|
170 |
+
|
171 |
+
Returns:
|
172 |
+
Iterable[{key, wav, label, sample_rate}]
|
173 |
+
"""
|
174 |
+
for sample in data:
|
175 |
+
assert 'sample_rate' in sample
|
176 |
+
assert 'speech' in sample
|
177 |
+
sample_rate = sample['sample_rate']
|
178 |
+
waveform = sample['speech']
|
179 |
+
if sample_rate != resample_rate:
|
180 |
+
if sample_rate < min_sample_rate:
|
181 |
+
continue
|
182 |
+
sample['sample_rate'] = resample_rate
|
183 |
+
sample['speech'] = torchaudio.transforms.Resample(
|
184 |
+
orig_freq=sample_rate, new_freq=resample_rate)(waveform)
|
185 |
+
max_val = sample['speech'].abs().max()
|
186 |
+
if max_val > 1:
|
187 |
+
sample['speech'] /= max_val
|
188 |
+
yield sample
|
189 |
+
|
190 |
+
|
191 |
+
def truncate(data, truncate_length=24576, mode='train'):
|
192 |
+
""" Truncate data.
|
193 |
+
|
194 |
+
Args:
|
195 |
+
data: Iterable[{key, wav, label, sample_rate}]
|
196 |
+
truncate_length: truncate length
|
197 |
+
|
198 |
+
Returns:
|
199 |
+
Iterable[{key, wav, label, sample_rate}]
|
200 |
+
"""
|
201 |
+
for sample in data:
|
202 |
+
waveform = sample['audio']
|
203 |
+
if waveform.shape[1] > truncate_length:
|
204 |
+
start = random.randint(0, waveform.shape[1] - truncate_length)
|
205 |
+
waveform = waveform[:, start: start + truncate_length]
|
206 |
+
else:
|
207 |
+
waveform = torch.concat([waveform, torch.zeros(1, truncate_length -
|
208 |
+
waveform.shape[1])],
|
209 |
+
dim=1)
|
210 |
+
sample['audio'] = waveform
|
211 |
+
yield sample
|
212 |
+
|
213 |
+
|
214 |
+
def upsample(data, resample_rate=48000, min_sample_rate=16000, mode='train',
|
215 |
+
n_codebook=4):
|
216 |
+
""" Resample data.
|
217 |
+
Inplace operation.
|
218 |
+
|
219 |
+
Args:
|
220 |
+
data: Iterable[{key, wav, label, sample_rate}]
|
221 |
+
resample_rate: target resample rate
|
222 |
+
|
223 |
+
Returns:
|
224 |
+
Iterable[{key, wav, label, sample_rate}]
|
225 |
+
"""
|
226 |
+
for sample in data:
|
227 |
+
assert 'semantic_token' in sample
|
228 |
+
# TODO: unify data processing key names
|
229 |
+
if 'acoustic_token' not in sample:
|
230 |
+
continue
|
231 |
+
|
232 |
+
if 'sample_rate' in sample.keys():
|
233 |
+
sample_rate = sample['sample_rate']
|
234 |
+
else:
|
235 |
+
sample_rate = 24000
|
236 |
+
token = np.array(sample['semantic_token'][0][:-1])
|
237 |
+
|
238 |
+
# Calculate the repetition factor for resampling
|
239 |
+
repetition_factor = int(n_codebook * resample_rate / sample_rate)
|
240 |
+
if sample_rate != resample_rate:
|
241 |
+
if sample_rate < min_sample_rate:
|
242 |
+
continue
|
243 |
+
sample['sample_rate'] = resample_rate
|
244 |
+
sample['semantic_token'] = np.array(
|
245 |
+
[np.repeat(token, repetition_factor)])
|
246 |
+
|
247 |
+
yield sample
|
248 |
+
|
249 |
+
def compute_fbank(data,
|
250 |
+
feat_extractor,
|
251 |
+
mode='train'):
|
252 |
+
""" Extract fbank
|
253 |
+
|
254 |
+
Args:
|
255 |
+
data: Iterable[{key, wav, label, sample_rate}]
|
256 |
+
|
257 |
+
Returns:
|
258 |
+
Iterable[{key, feat, label}]
|
259 |
+
"""
|
260 |
+
for sample in data:
|
261 |
+
assert 'sample_rate' in sample
|
262 |
+
assert 'speech' in sample
|
263 |
+
assert 'utt' in sample
|
264 |
+
assert 'text_token' in sample
|
265 |
+
waveform = sample['speech']
|
266 |
+
mat = feat_extractor(waveform).squeeze(dim=0).transpose(0, 1)
|
267 |
+
sample['speech_feat'] = mat
|
268 |
+
del sample['speech']
|
269 |
+
yield sample
|
270 |
+
|
271 |
+
|
272 |
+
def parse_embedding(data, normalize, mode='train'):
|
273 |
+
""" Parse utt_embedding/spk_embedding
|
274 |
+
|
275 |
+
Args:
|
276 |
+
data: Iterable[{key, wav, label, sample_rate}]
|
277 |
+
|
278 |
+
Returns:
|
279 |
+
Iterable[{key, feat, label}]
|
280 |
+
"""
|
281 |
+
|
282 |
+
for sample in data:
|
283 |
+
sample['utt_embedding'] = torch.tensor(sample['utt_embedding'],
|
284 |
+
dtype=torch.float32)
|
285 |
+
sample['spk_embedding'] = torch.tensor(sample['spk_embedding'],
|
286 |
+
dtype=torch.float32)
|
287 |
+
if normalize:
|
288 |
+
sample['utt_embedding'] = F.normalize(sample['utt_embedding'],
|
289 |
+
dim=0)
|
290 |
+
sample['spk_embedding'] = F.normalize(sample['spk_embedding'],
|
291 |
+
dim=0)
|
292 |
+
yield sample
|
293 |
+
|
294 |
+
def tokenize(data, get_tokenizer, allowed_special, mode='train'):
|
295 |
+
""" Decode text to chars or BPE
|
296 |
+
Inplace operation
|
297 |
+
|
298 |
+
Args:
|
299 |
+
data: Iterable[{key, wav, txt, sample_rate}]
|
300 |
+
|
301 |
+
Returns:
|
302 |
+
Iterable[{key, wav, txt, tokens, label, sample_rate}]
|
303 |
+
"""
|
304 |
+
tokenizer = get_tokenizer()
|
305 |
+
|
306 |
+
for sample in data:
|
307 |
+
assert 'text' in sample
|
308 |
+
sample['text_token'] = tokenizer.encode(sample['text'],
|
309 |
+
allowed_special=allowed_special)
|
310 |
+
yield sample
|
311 |
+
|
312 |
+
|
313 |
+
def shuffle(data, shuffle_size=10000, mode='train'):
|
314 |
+
""" Local shuffle the data
|
315 |
+
|
316 |
+
Args:
|
317 |
+
data: Iterable[{key, feat, label}]
|
318 |
+
shuffle_size: buffer size for shuffle
|
319 |
+
|
320 |
+
Returns:
|
321 |
+
Iterable[{key, feat, label}]
|
322 |
+
"""
|
323 |
+
buf = []
|
324 |
+
for sample in data:
|
325 |
+
buf.append(sample)
|
326 |
+
if len(buf) >= shuffle_size:
|
327 |
+
random.shuffle(buf)
|
328 |
+
for x in buf:
|
329 |
+
yield x
|
330 |
+
buf = []
|
331 |
+
# The sample left over
|
332 |
+
random.shuffle(buf)
|
333 |
+
for x in buf:
|
334 |
+
yield x
|
335 |
+
|
336 |
+
|
337 |
+
def sort(data, sort_size=500, mode='train'):
|
338 |
+
""" Sort the data by feature length.
|
339 |
+
Sort is used after shuffle and before batch, so we can group
|
340 |
+
utts with similar lengths into a batch, and `sort_size` should
|
341 |
+
be less than `shuffle_size`
|
342 |
+
|
343 |
+
Args:
|
344 |
+
data: Iterable[{key, feat, label}]
|
345 |
+
sort_size: buffer size for sort
|
346 |
+
|
347 |
+
Returns:
|
348 |
+
Iterable[{key, feat, label}]
|
349 |
+
"""
|
350 |
+
|
351 |
+
buf = []
|
352 |
+
for sample in data:
|
353 |
+
if sample["chorus"] == "verse":
|
354 |
+
sample["chorus"] = "verse1"
|
355 |
+
|
356 |
+
if sample["acoustic_token"].shape[0] == 1:
|
357 |
+
sample["acoustic_token"] = np.concatenate(
|
358 |
+
sample["acoustic_token"][0])
|
359 |
+
else:
|
360 |
+
sample["acoustic_token"] = np.concatenate(sample["acoustic_token"])
|
361 |
+
|
362 |
+
sample["acoustic_token"] = torch.from_numpy(sample["acoustic_token"])
|
363 |
+
buf.append(sample)
|
364 |
+
if len(buf) >= sort_size:
|
365 |
+
buf.sort(key=lambda x: x['acoustic_token'].size(0))
|
366 |
+
for x in buf:
|
367 |
+
yield x
|
368 |
+
buf = []
|
369 |
+
# The sample left over
|
370 |
+
buf.sort(key=lambda x: x['acoustic_token'].size(0))
|
371 |
+
for x in buf:
|
372 |
+
yield x
|
373 |
+
|
374 |
+
|
375 |
+
def static_batch(data, batch_size=32):
|
376 |
+
""" Static batch the data by `batch_size`
|
377 |
+
|
378 |
+
Args:
|
379 |
+
data: Iterable[{key, feat, label}]
|
380 |
+
batch_size: batch size
|
381 |
+
|
382 |
+
Returns:
|
383 |
+
Iterable[List[{key, feat, label}]]
|
384 |
+
"""
|
385 |
+
buf = []
|
386 |
+
data_empty = True
|
387 |
+
for sample in data:
|
388 |
+
data_empty = False
|
389 |
+
buf.append(sample)
|
390 |
+
if len(buf) >= batch_size:
|
391 |
+
yield buf
|
392 |
+
buf = []
|
393 |
+
if data_empty:
|
394 |
+
raise ValueError("data is empty")
|
395 |
+
if len(buf) > 0:
|
396 |
+
yield buf
|
397 |
+
|
398 |
+
|
399 |
+
def dynamic_batch(data, max_frames_in_batch=12000, mode='train'):
|
400 |
+
""" Dynamic batch the data until the total frames in batch
|
401 |
+
reach `max_frames_in_batch`
|
402 |
+
|
403 |
+
Args:
|
404 |
+
data: Iterable[{key, feat, label}]
|
405 |
+
max_frames_in_batch: max_frames in one batch
|
406 |
+
|
407 |
+
Returns:
|
408 |
+
Iterable[List[{key, feat, label}]]
|
409 |
+
"""
|
410 |
+
buf = []
|
411 |
+
longest_frames = 0
|
412 |
+
for sample in data:
|
413 |
+
assert 'acoustic_token' in sample
|
414 |
+
assert isinstance(sample['acoustic_token'], torch.Tensor)
|
415 |
+
|
416 |
+
if 'semantic_token' in sample:
|
417 |
+
new_sample_frames = sample['semantic_token'][0].shape[0]
|
418 |
+
else:
|
419 |
+
new_sample_frames = sample['semantic_token']
|
420 |
+
|
421 |
+
if "text_token" in sample:
|
422 |
+
new_sample_frames += len(sample['text_token'])
|
423 |
+
|
424 |
+
longest_frames = max(longest_frames, new_sample_frames)
|
425 |
+
frames_after_padding = longest_frames * (len(buf) + 1)
|
426 |
+
|
427 |
+
if frames_after_padding > max_frames_in_batch:
|
428 |
+
if len(buf) > 0:
|
429 |
+
yield buf
|
430 |
+
buf = [sample]
|
431 |
+
longest_frames = new_sample_frames
|
432 |
+
else:
|
433 |
+
buf.append(sample)
|
434 |
+
if len(buf) > 0:
|
435 |
+
yield buf
|
436 |
+
|
437 |
+
|
438 |
+
def batch(data, batch_type='static', batch_size=16, max_frames_in_batch=12000,
|
439 |
+
mode='train'):
|
440 |
+
""" Wrapper for static/dynamic batch
|
441 |
+
"""
|
442 |
+
if mode == 'inference':
|
443 |
+
return static_batch(data, 1)
|
444 |
+
elif mode == 'processing':
|
445 |
+
return static_batch(data, batch_size)
|
446 |
+
else:
|
447 |
+
if batch_type == 'static':
|
448 |
+
return static_batch(data, batch_size)
|
449 |
+
elif batch_type == 'dynamic':
|
450 |
+
return dynamic_batch(data, max_frames_in_batch)
|
451 |
+
else:
|
452 |
+
logging.fatal('Unsupported batch type {}'.format(batch_type))
|
453 |
+
|
454 |
+
|
455 |
+
def padding(data, mode='train'):
|
456 |
+
""" Padding the data into training data
|
457 |
+
|
458 |
+
Args:
|
459 |
+
data: Iterable[List[{key, feat, label}]]
|
460 |
+
|
461 |
+
Returns:
|
462 |
+
Iterable[Tuple(keys, feats, labels, feats lengths, label lengths)]
|
463 |
+
"""
|
464 |
+
if mode == "train":
|
465 |
+
for sample in data:
|
466 |
+
assert isinstance(sample, list)
|
467 |
+
if len(sample) != 0:
|
468 |
+
acoustic_feat_len = torch.tensor(
|
469 |
+
[x['acoustic_token'].size(0) for x in sample],
|
470 |
+
dtype=torch.int32)
|
471 |
+
order = torch.argsort(acoustic_feat_len, descending=True)
|
472 |
+
utts = [sample[i]['utt'] for i in order]
|
473 |
+
acoustic_token = [
|
474 |
+
sample[i]['acoustic_token'].clone().to(torch.int32) for i in
|
475 |
+
order]
|
476 |
+
acoustic_token_len = torch.tensor(
|
477 |
+
[i.size(0) for i in acoustic_token], dtype=torch.int32)
|
478 |
+
|
479 |
+
acoustic_token = pad_sequence(acoustic_token,
|
480 |
+
batch_first=True,
|
481 |
+
padding_value=0)
|
482 |
+
|
483 |
+
text = [sample[i]['text'] for i in order]
|
484 |
+
text_token = [torch.tensor(sample[i]['text_token']).long() for i
|
485 |
+
in order]
|
486 |
+
text_token_len = torch.tensor([i.size(0) for i in text_token],
|
487 |
+
dtype=torch.int32)
|
488 |
+
text_token = pad_sequence(text_token, batch_first=True,
|
489 |
+
padding_value=0)
|
490 |
+
time_start = torch.tensor(
|
491 |
+
[sample[i]['time_start'] for i in order])
|
492 |
+
time_end = torch.tensor([sample[i]['time_end'] for i in order])
|
493 |
+
|
494 |
+
if isinstance(sample[0]['chorus'], str):
|
495 |
+
chorus = torch.tensor(
|
496 |
+
[CHORUS[sample[i]['chorus']] for i in order])
|
497 |
+
else:
|
498 |
+
chorus = [
|
499 |
+
torch.tensor([CHORUS[t] for t in sample[i]['chorus']])
|
500 |
+
for i in order]
|
501 |
+
chorus = pad_sequence(chorus, batch_first=True,
|
502 |
+
padding_value=-1)
|
503 |
+
|
504 |
+
batch = {
|
505 |
+
"utts" : utts,
|
506 |
+
"acoustic_token" : acoustic_token,
|
507 |
+
"acoustic_token_len": acoustic_token_len,
|
508 |
+
"time_start" : time_start,
|
509 |
+
"time_end" : time_end,
|
510 |
+
"chorus" : chorus,
|
511 |
+
"text" : text,
|
512 |
+
"text_token" : text_token,
|
513 |
+
"text_token_len" : text_token_len,
|
514 |
+
}
|
515 |
+
|
516 |
+
if "semantic_token" in sample[0]:
|
517 |
+
semantic_token = [
|
518 |
+
torch.tensor(sample[i]['semantic_token'][0],
|
519 |
+
dtype=torch.int32) for i in order]
|
520 |
+
semantic_token_len = torch.tensor(
|
521 |
+
[i.size(0) for i in semantic_token],
|
522 |
+
dtype=torch.int32)
|
523 |
+
semantic_token = pad_sequence(semantic_token,
|
524 |
+
batch_first=True,
|
525 |
+
padding_value=0)
|
526 |
+
batch.update({"semantic_token" : semantic_token,
|
527 |
+
"semantic_token_len": semantic_token_len})
|
528 |
+
|
529 |
+
yield batch
|
530 |
+
else:
|
531 |
+
logging.info("WARNING: sample is empty []!")
|
532 |
+
|
533 |
+
elif mode == "inference":
|
534 |
+
for sample in data:
|
535 |
+
assert isinstance(sample, list)
|
536 |
+
utts = [sample[i]['utt'] for i in range(len(sample))]
|
537 |
+
text = [sample[i]['text'] for i in range(len(sample))]
|
538 |
+
text_token = [torch.tensor(sample[i]['text_token']).long() for i in
|
539 |
+
range(len(sample))]
|
540 |
+
text_token_len = torch.tensor([i.size(0) for i in text_token],
|
541 |
+
dtype=torch.int32)
|
542 |
+
text_token = pad_sequence(text_token, batch_first=True,
|
543 |
+
padding_value=0)
|
544 |
+
time_start = torch.tensor(
|
545 |
+
[sample[i]['time_start'] for i in range(len(sample))])
|
546 |
+
time_end = torch.tensor(
|
547 |
+
[sample[i]['time_end'] for i in range(len(sample))])
|
548 |
+
|
549 |
+
if isinstance(sample[0]['chorus'], str):
|
550 |
+
chorus = torch.tensor([CHORUS[sample[i]['chorus']] for i in
|
551 |
+
range(len(sample))])
|
552 |
+
else:
|
553 |
+
chorus = [torch.tensor([CHORUS[t] for t in sample[i]['chorus']])
|
554 |
+
for i in range(len(sample))]
|
555 |
+
chorus = pad_sequence(chorus, batch_first=True,
|
556 |
+
padding_value=-1)
|
557 |
+
|
558 |
+
if "acoustic_token" in sample[0]:
|
559 |
+
acoustic_token = [
|
560 |
+
sample[i]['acoustic_token'].clone().to(torch.int32) for i in
|
561 |
+
range(len(sample))]
|
562 |
+
acoustic_token_len = torch.tensor(
|
563 |
+
[i.size(0) for i in acoustic_token], dtype=torch.int32)
|
564 |
+
acoustic_token = pad_sequence(acoustic_token,
|
565 |
+
batch_first=True,
|
566 |
+
padding_value=0)
|
567 |
+
else:
|
568 |
+
acoustic_token = None
|
569 |
+
acoustic_token_len = None
|
570 |
+
|
571 |
+
batch = {
|
572 |
+
"utts" : utts,
|
573 |
+
"acoustic_token" : acoustic_token,
|
574 |
+
"acoustic_token_len": acoustic_token_len,
|
575 |
+
"time_start" : time_start,
|
576 |
+
"time_end" : time_end,
|
577 |
+
"chorus" : chorus,
|
578 |
+
"text" : text,
|
579 |
+
"text_token" : text_token,
|
580 |
+
"text_token_len" : text_token_len,
|
581 |
+
}
|
582 |
+
|
583 |
+
if "semantic_token" in sample[0]:
|
584 |
+
semantic_token = [torch.tensor(sample[i]['semantic_token'][0],
|
585 |
+
dtype=torch.int32) for i in
|
586 |
+
range(len(sample))]
|
587 |
+
semantic_token_len = torch.tensor(
|
588 |
+
[i.size(0) for i in semantic_token], dtype=torch.int32)
|
589 |
+
semantic_token = pad_sequence(semantic_token,
|
590 |
+
batch_first=True,
|
591 |
+
padding_value=0)
|
592 |
+
batch.update({"semantic_token" : semantic_token,
|
593 |
+
"semantic_token_len": semantic_token_len})
|
594 |
+
|
595 |
+
yield batch
|
inspiremusic/flow/decoder.py
ADDED
@@ -0,0 +1,277 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
import torch
|
15 |
+
import torch.nn as nn
|
16 |
+
from einops import pack, rearrange, repeat
|
17 |
+
from matcha.models.components.decoder import SinusoidalPosEmb, Block1D, ResnetBlock1D, Downsample1D, TimestepEmbedding, Upsample1D
|
18 |
+
from matcha.models.components.transformer import BasicTransformerBlock
|
19 |
+
|
20 |
+
class Transpose(torch.nn.Module):
|
21 |
+
def __init__(self, dim0: int, dim1: int):
|
22 |
+
super().__init__()
|
23 |
+
self.dim0 = dim0
|
24 |
+
self.dim1 = dim1
|
25 |
+
|
26 |
+
def forward(self, x: torch.Tensor):
|
27 |
+
x = torch.transpose(x, self.dim0, self.dim1)
|
28 |
+
return x
|
29 |
+
|
30 |
+
class CausalBlock1D(Block1D):
|
31 |
+
def __init__(self, dim: int, dim_out: int):
|
32 |
+
super(CausalBlock1D, self).__init__(dim, dim_out)
|
33 |
+
self.block = torch.nn.Sequential(
|
34 |
+
CausalConv1d(dim, dim_out, 3),
|
35 |
+
Transpose(1, 2),
|
36 |
+
nn.LayerNorm(dim_out),
|
37 |
+
Transpose(1, 2),
|
38 |
+
nn.Mish(),
|
39 |
+
)
|
40 |
+
|
41 |
+
def forward(self, x: torch.Tensor, mask: torch.Tensor):
|
42 |
+
output = self.block(x * mask)
|
43 |
+
return output * mask
|
44 |
+
|
45 |
+
|
46 |
+
class CausalResnetBlock1D(ResnetBlock1D):
|
47 |
+
def __init__(self, dim: int, dim_out: int, time_emb_dim: int, groups: int = 8):
|
48 |
+
super(CausalResnetBlock1D, self).__init__(dim, dim_out, time_emb_dim, groups)
|
49 |
+
self.block1 = CausalBlock1D(dim, dim_out)
|
50 |
+
self.block2 = CausalBlock1D(dim_out, dim_out)
|
51 |
+
|
52 |
+
class CausalConv1d(torch.nn.Conv1d):
|
53 |
+
def __init__(
|
54 |
+
self,
|
55 |
+
in_channels: int,
|
56 |
+
out_channels: int,
|
57 |
+
kernel_size: int,
|
58 |
+
stride: int = 1,
|
59 |
+
dilation: int = 1,
|
60 |
+
groups: int = 1,
|
61 |
+
bias: bool = True,
|
62 |
+
padding_mode: str = 'zeros',
|
63 |
+
device=None,
|
64 |
+
dtype=None
|
65 |
+
) -> None:
|
66 |
+
super(CausalConv1d, self).__init__(in_channels, out_channels,
|
67 |
+
kernel_size, stride,
|
68 |
+
padding=0, dilation=dilation,
|
69 |
+
groups=groups, bias=bias,
|
70 |
+
padding_mode=padding_mode,
|
71 |
+
device=device, dtype=dtype)
|
72 |
+
assert stride == 1
|
73 |
+
self.causal_padding = (kernel_size - 1, 0)
|
74 |
+
|
75 |
+
def forward(self, x: torch.Tensor):
|
76 |
+
x = F.pad(x, self.causal_padding)
|
77 |
+
x = super(CausalConv1d, self).forward(x)
|
78 |
+
return x
|
79 |
+
|
80 |
+
class ConditionalDecoder(nn.Module):
|
81 |
+
def __init__(
|
82 |
+
self,
|
83 |
+
in_channels,
|
84 |
+
out_channels,
|
85 |
+
channels=(256, 256),
|
86 |
+
dropout=0.05,
|
87 |
+
attention_head_dim=64,
|
88 |
+
n_blocks=1,
|
89 |
+
num_mid_blocks=2,
|
90 |
+
num_heads=4,
|
91 |
+
act_fn="snake",
|
92 |
+
):
|
93 |
+
"""
|
94 |
+
This decoder requires an input with the same shape of the target. So, if your text content
|
95 |
+
is shorter or longer than the outputs, please re-sampling it before feeding to the decoder.
|
96 |
+
"""
|
97 |
+
super().__init__()
|
98 |
+
channels = tuple(channels)
|
99 |
+
self.in_channels = in_channels
|
100 |
+
self.out_channels = out_channels
|
101 |
+
|
102 |
+
self.time_embeddings = SinusoidalPosEmb(in_channels)
|
103 |
+
time_embed_dim = channels[0] * 4
|
104 |
+
self.time_mlp = TimestepEmbedding(
|
105 |
+
in_channels=in_channels,
|
106 |
+
time_embed_dim=time_embed_dim,
|
107 |
+
act_fn="silu",
|
108 |
+
)
|
109 |
+
self.down_blocks = nn.ModuleList([])
|
110 |
+
self.mid_blocks = nn.ModuleList([])
|
111 |
+
self.up_blocks = nn.ModuleList([])
|
112 |
+
|
113 |
+
output_channel = in_channels
|
114 |
+
for i in range(len(channels)): # pylint: disable=consider-using-enumerate
|
115 |
+
input_channel = output_channel
|
116 |
+
output_channel = channels[i]
|
117 |
+
is_last = i == len(channels) - 1
|
118 |
+
resnet = ResnetBlock1D(dim=input_channel, dim_out=output_channel, time_emb_dim=time_embed_dim)
|
119 |
+
transformer_blocks = nn.ModuleList(
|
120 |
+
[
|
121 |
+
BasicTransformerBlock(
|
122 |
+
dim=output_channel,
|
123 |
+
num_attention_heads=num_heads,
|
124 |
+
attention_head_dim=attention_head_dim,
|
125 |
+
dropout=dropout,
|
126 |
+
activation_fn=act_fn,
|
127 |
+
)
|
128 |
+
for _ in range(n_blocks)
|
129 |
+
]
|
130 |
+
)
|
131 |
+
downsample = (
|
132 |
+
Downsample1D(output_channel) if not is_last else nn.Conv1d(output_channel, output_channel, 3, padding=1)
|
133 |
+
)
|
134 |
+
self.down_blocks.append(nn.ModuleList([resnet, transformer_blocks, downsample]))
|
135 |
+
|
136 |
+
for _ in range(num_mid_blocks):
|
137 |
+
input_channel = channels[-1]
|
138 |
+
out_channels = channels[-1]
|
139 |
+
resnet = ResnetBlock1D(dim=input_channel, dim_out=output_channel, time_emb_dim=time_embed_dim)
|
140 |
+
|
141 |
+
transformer_blocks = nn.ModuleList(
|
142 |
+
[
|
143 |
+
BasicTransformerBlock(
|
144 |
+
dim=output_channel,
|
145 |
+
num_attention_heads=num_heads,
|
146 |
+
attention_head_dim=attention_head_dim,
|
147 |
+
dropout=dropout,
|
148 |
+
activation_fn=act_fn,
|
149 |
+
)
|
150 |
+
for _ in range(n_blocks)
|
151 |
+
]
|
152 |
+
)
|
153 |
+
|
154 |
+
self.mid_blocks.append(nn.ModuleList([resnet, transformer_blocks]))
|
155 |
+
|
156 |
+
channels = channels[::-1] + (channels[0],)
|
157 |
+
for i in range(len(channels) - 1):
|
158 |
+
input_channel = channels[i] * 2
|
159 |
+
output_channel = channels[i + 1]
|
160 |
+
is_last = i == len(channels) - 2
|
161 |
+
resnet = ResnetBlock1D(
|
162 |
+
dim=input_channel,
|
163 |
+
dim_out=output_channel,
|
164 |
+
time_emb_dim=time_embed_dim,
|
165 |
+
)
|
166 |
+
transformer_blocks = nn.ModuleList(
|
167 |
+
[
|
168 |
+
BasicTransformerBlock(
|
169 |
+
dim=output_channel,
|
170 |
+
num_attention_heads=num_heads,
|
171 |
+
attention_head_dim=attention_head_dim,
|
172 |
+
dropout=dropout,
|
173 |
+
activation_fn=act_fn,
|
174 |
+
)
|
175 |
+
for _ in range(n_blocks)
|
176 |
+
]
|
177 |
+
)
|
178 |
+
upsample = (
|
179 |
+
Upsample1D(output_channel, use_conv_transpose=True)
|
180 |
+
if not is_last
|
181 |
+
else nn.Conv1d(output_channel, output_channel, 3, padding=1)
|
182 |
+
)
|
183 |
+
self.up_blocks.append(nn.ModuleList([resnet, transformer_blocks, upsample]))
|
184 |
+
self.final_block = Block1D(channels[-1], channels[-1])
|
185 |
+
self.final_proj = nn.Conv1d(channels[-1], self.out_channels, 1)
|
186 |
+
self.initialize_weights()
|
187 |
+
|
188 |
+
def initialize_weights(self):
|
189 |
+
for m in self.modules():
|
190 |
+
if isinstance(m, nn.Conv1d):
|
191 |
+
nn.init.kaiming_normal_(m.weight, nonlinearity="relu")
|
192 |
+
if m.bias is not None:
|
193 |
+
nn.init.constant_(m.bias, 0)
|
194 |
+
elif isinstance(m, nn.GroupNorm):
|
195 |
+
nn.init.constant_(m.weight, 1)
|
196 |
+
nn.init.constant_(m.bias, 0)
|
197 |
+
elif isinstance(m, nn.Linear):
|
198 |
+
nn.init.kaiming_normal_(m.weight, nonlinearity="relu")
|
199 |
+
if m.bias is not None:
|
200 |
+
nn.init.constant_(m.bias, 0)
|
201 |
+
|
202 |
+
def forward(self, x, mask, mu, t, spks=None, cond=None):
|
203 |
+
"""Forward pass of the UNet1DConditional model.
|
204 |
+
|
205 |
+
Args:
|
206 |
+
x (torch.Tensor): shape (batch_size, in_channels, time)
|
207 |
+
mask (_type_): shape (batch_size, 1, time)
|
208 |
+
t (_type_): shape (batch_size)
|
209 |
+
spks (_type_, optional): shape: (batch_size, condition_channels). Defaults to None.
|
210 |
+
cond (_type_, optional): placeholder for future use. Defaults to None.
|
211 |
+
|
212 |
+
Raises:
|
213 |
+
ValueError: _description_
|
214 |
+
ValueError: _description_
|
215 |
+
|
216 |
+
Returns:
|
217 |
+
_type_: _description_
|
218 |
+
"""
|
219 |
+
|
220 |
+
t = self.time_embeddings(t).to(t.dtype)
|
221 |
+
t = self.time_mlp(t)
|
222 |
+
x = pack([x, mu], "b * t")[0]
|
223 |
+
if spks is not None:
|
224 |
+
spks = repeat(spks, "b c -> b c t", t=x.shape[-1])
|
225 |
+
x = pack([x, spks], "b * t")[0]
|
226 |
+
if cond is not None:
|
227 |
+
x = pack([x, cond], "b * t")[0]
|
228 |
+
hiddens = []
|
229 |
+
masks = [mask]
|
230 |
+
for resnet, transformer_blocks, downsample in self.down_blocks:
|
231 |
+
mask_down = masks[-1]
|
232 |
+
x = resnet(x, mask_down, t)
|
233 |
+
x = rearrange(x, "b c t -> b t c").contiguous()
|
234 |
+
attn_mask = torch.matmul(mask_down.transpose(1, 2).contiguous(), mask_down)
|
235 |
+
for transformer_block in transformer_blocks:
|
236 |
+
x = transformer_block(
|
237 |
+
hidden_states=x,
|
238 |
+
attention_mask=attn_mask,
|
239 |
+
timestep=t,
|
240 |
+
)
|
241 |
+
x = rearrange(x, "b t c -> b c t").contiguous()
|
242 |
+
hiddens.append(x) # Save hidden states for skip connections
|
243 |
+
x = downsample(x * mask_down)
|
244 |
+
masks.append(mask_down[:, :, ::2])
|
245 |
+
masks = masks[:-1]
|
246 |
+
mask_mid = masks[-1]
|
247 |
+
|
248 |
+
for resnet, transformer_blocks in self.mid_blocks:
|
249 |
+
x = resnet(x, mask_mid, t)
|
250 |
+
x = rearrange(x, "b c t -> b t c").contiguous()
|
251 |
+
attn_mask = torch.matmul(mask_mid.transpose(1, 2).contiguous(), mask_mid)
|
252 |
+
for transformer_block in transformer_blocks:
|
253 |
+
x = transformer_block(
|
254 |
+
hidden_states=x,
|
255 |
+
attention_mask=attn_mask,
|
256 |
+
timestep=t,
|
257 |
+
)
|
258 |
+
x = rearrange(x, "b t c -> b c t").contiguous()
|
259 |
+
|
260 |
+
for resnet, transformer_blocks, upsample in self.up_blocks:
|
261 |
+
mask_up = masks.pop()
|
262 |
+
skip = hiddens.pop()
|
263 |
+
x = pack([x[:, :, :skip.shape[-1]], skip], "b * t")[0]
|
264 |
+
x = resnet(x, mask_up, t)
|
265 |
+
x = rearrange(x, "b c t -> b t c").contiguous()
|
266 |
+
attn_mask = torch.matmul(mask_up.transpose(1, 2).contiguous(), mask_up)
|
267 |
+
for transformer_block in transformer_blocks:
|
268 |
+
x = transformer_block(
|
269 |
+
hidden_states=x,
|
270 |
+
attention_mask=attn_mask,
|
271 |
+
timestep=t,
|
272 |
+
)
|
273 |
+
x = rearrange(x, "b t c -> b c t").contiguous()
|
274 |
+
x = upsample(x * mask_up)
|
275 |
+
x = self.final_block(x, mask_up)
|
276 |
+
output = self.final_proj(x * mask_up)
|
277 |
+
return output * mask
|
inspiremusic/flow/flow.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
import logging
|
15 |
+
import random
|
16 |
+
from typing import Dict, Optional
|
17 |
+
import torch
|
18 |
+
import torch.nn as nn
|
19 |
+
from torch.nn import functional as F
|
20 |
+
from omegaconf import DictConfig
|
21 |
+
from inspiremusic.utils.mask import make_pad_mask
|
22 |
+
from inspiremusic.music_tokenizer.vqvae import VQVAE
|
23 |
+
|
24 |
+
class MaskedDiff(torch.nn.Module):
|
25 |
+
def __init__(self,
|
26 |
+
input_size: int = 512,
|
27 |
+
output_size: int = 128,
|
28 |
+
output_type: str = "mel",
|
29 |
+
vocab_size: int = 4096,
|
30 |
+
input_frame_rate: int = 50,
|
31 |
+
only_mask_loss: bool = True,
|
32 |
+
encoder: torch.nn.Module = None,
|
33 |
+
length_regulator: torch.nn.Module = None,
|
34 |
+
decoder: torch.nn.Module = None,
|
35 |
+
decoder_conf: Dict = {'in_channels': 240, 'out_channel': 80,
|
36 |
+
'cfm_params': DictConfig({'sigma_min': 1e-06, 'solver': 'euler', 't_scheduler': 'cosine',
|
37 |
+
'training_cfg_rate': 0.2, 'inference_cfg_rate': 0.7, 'reg_loss_type': 'l1'}),
|
38 |
+
'decoder_params': {'channels': [256, 256], 'dropout': 0.0, 'attention_head_dim': 64,
|
39 |
+
'n_blocks': 4, 'num_mid_blocks': 12, 'num_heads': 8, 'act_fn': 'gelu'}},
|
40 |
+
mel_feat_conf: Dict = {'n_fft': 1024, 'num_mels': 128, 'sampling_rate': 48000,
|
41 |
+
'hop_size': 256, 'win_size': 1024, 'fmin': 0, 'fmax': 48000},
|
42 |
+
generator_model_dir: str = "../../pretrained_models/InspireMusic-Base/music_tokenizer",
|
43 |
+
num_codebooks: int = 4
|
44 |
+
):
|
45 |
+
super().__init__()
|
46 |
+
self.input_size = input_size
|
47 |
+
self.output_size = output_size
|
48 |
+
self.decoder_conf = decoder_conf
|
49 |
+
self.mel_feat_conf = mel_feat_conf
|
50 |
+
self.vocab_size = vocab_size
|
51 |
+
self.output_type = output_type
|
52 |
+
self.input_frame_rate = input_frame_rate
|
53 |
+
logging.info(f"input frame rate={self.input_frame_rate}")
|
54 |
+
self.input_embedding = nn.Embedding(vocab_size, input_size)
|
55 |
+
|
56 |
+
self.encoder = encoder
|
57 |
+
self.encoder_proj = torch.nn.Linear(self.encoder.output_size(), output_size)
|
58 |
+
self.decoder = decoder
|
59 |
+
self.length_regulator = length_regulator
|
60 |
+
self.only_mask_loss = only_mask_loss
|
61 |
+
self.quantizer = VQVAE( f'{generator_model_dir}/config.json',
|
62 |
+
f'{generator_model_dir}/model.pt',with_encoder=True).quantizer
|
63 |
+
self.quantizer.eval()
|
64 |
+
self.num_codebooks = num_codebooks
|
65 |
+
self.cond = None
|
66 |
+
self.interpolate = False
|
67 |
+
|
68 |
+
def forward(
|
69 |
+
self,
|
70 |
+
batch: dict,
|
71 |
+
device: torch.device,
|
72 |
+
) -> Dict[str, Optional[torch.Tensor]]:
|
73 |
+
|
74 |
+
audio_token = batch['acoustic_token'].to(device)
|
75 |
+
audio_token_len = batch['acoustic_token_len'].to(device)
|
76 |
+
audio_token = audio_token.view(audio_token.size(0),-1,self.num_codebooks)
|
77 |
+
if "semantic_token" not in batch:
|
78 |
+
token = audio_token[:,:,0]
|
79 |
+
token_len = (audio_token_len/self.num_codebooks).long()
|
80 |
+
|
81 |
+
else:
|
82 |
+
token = batch['semantic_token'].to(device)
|
83 |
+
token_len = batch['semantic_token_len'].to(device)
|
84 |
+
|
85 |
+
with torch.no_grad():
|
86 |
+
feat = self.quantizer.embed(audio_token)
|
87 |
+
feat_len = (audio_token_len/self.num_codebooks).long()
|
88 |
+
|
89 |
+
token = self.input_embedding(token)
|
90 |
+
h, h_lengths = self.encoder(token, token_len)
|
91 |
+
h, h_lengths = self.length_regulator(h, feat_len)
|
92 |
+
|
93 |
+
# get conditions
|
94 |
+
if self.cond:
|
95 |
+
conds = torch.zeros(feat.shape, device=token.device)
|
96 |
+
for i, j in enumerate(feat_len):
|
97 |
+
if random.random() < 0.5:
|
98 |
+
continue
|
99 |
+
index = random.randint(0, int(0.3 * j))
|
100 |
+
conds[i, :index] = feat[i, :index]
|
101 |
+
conds = conds.transpose(1, 2)
|
102 |
+
else:
|
103 |
+
conds = None
|
104 |
+
|
105 |
+
mask = (~make_pad_mask(feat_len)).to(h)
|
106 |
+
|
107 |
+
loss, _ = self.decoder.compute_loss(
|
108 |
+
feat,
|
109 |
+
mask.unsqueeze(1),
|
110 |
+
h.transpose(1, 2).contiguous(),
|
111 |
+
None,
|
112 |
+
cond=conds
|
113 |
+
)
|
114 |
+
|
115 |
+
return {'loss': loss}
|
116 |
+
|
117 |
+
@torch.inference_mode()
|
118 |
+
def inference(self,
|
119 |
+
token,
|
120 |
+
token_len,
|
121 |
+
sample_rate):
|
122 |
+
assert token.shape[0] == 1
|
123 |
+
|
124 |
+
token = self.input_embedding(torch.clamp(token, min=0))
|
125 |
+
h, h_lengths = self.encoder(token, token_len)
|
126 |
+
|
127 |
+
if sample_rate == 48000:
|
128 |
+
token_len = 2 * token_len
|
129 |
+
|
130 |
+
h, h_lengths = self.length_regulator(h, token_len)
|
131 |
+
|
132 |
+
# get conditions
|
133 |
+
conds = None
|
134 |
+
|
135 |
+
mask = (~make_pad_mask(token_len)).to(h)
|
136 |
+
feat = self.decoder(
|
137 |
+
mu=h.transpose(1, 2).contiguous(),
|
138 |
+
mask=mask.unsqueeze(1),
|
139 |
+
spks=None,
|
140 |
+
cond=conds,
|
141 |
+
n_timesteps=10
|
142 |
+
)
|
143 |
+
return feat
|
inspiremusic/flow/flow_matching.py
ADDED
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
import torch
|
15 |
+
import torch.nn.functional as F
|
16 |
+
from matcha.models.components.flow_matching import BASECFM
|
17 |
+
|
18 |
+
|
19 |
+
class ConditionalCFM(BASECFM):
|
20 |
+
def __init__(self, in_channels, cfm_params, estimator: torch.nn.Module = None):
|
21 |
+
super().__init__(
|
22 |
+
n_feats=in_channels,
|
23 |
+
cfm_params=cfm_params,
|
24 |
+
)
|
25 |
+
self.t_scheduler = cfm_params.t_scheduler
|
26 |
+
self.training_cfg_rate = cfm_params.training_cfg_rate
|
27 |
+
self.inference_cfg_rate = cfm_params.inference_cfg_rate
|
28 |
+
# Just change the architecture of the estimator here
|
29 |
+
self.estimator = estimator
|
30 |
+
|
31 |
+
@torch.inference_mode()
|
32 |
+
def forward(self, mu, mask, n_timesteps, temperature=1.0, spks=None, cond=None):
|
33 |
+
"""Forward diffusion
|
34 |
+
|
35 |
+
Args:
|
36 |
+
mu (torch.Tensor): output of encoder
|
37 |
+
shape: (batch_size, n_feats, mel_timesteps)
|
38 |
+
mask (torch.Tensor): output_mask
|
39 |
+
shape: (batch_size, 1, mel_timesteps)
|
40 |
+
n_timesteps (int): number of diffusion steps
|
41 |
+
temperature (float, optional): temperature for scaling noise. Defaults to 1.0.
|
42 |
+
spks (torch.Tensor, optional): speaker ids. Defaults to None.
|
43 |
+
shape: (batch_size, spk_emb_dim)
|
44 |
+
cond: Not used but kept for future purposes
|
45 |
+
|
46 |
+
Returns:
|
47 |
+
sample: generated mel-spectrogram
|
48 |
+
shape: (batch_size, n_feats, mel_timesteps)
|
49 |
+
"""
|
50 |
+
z = torch.randn_like(mu) * temperature
|
51 |
+
t_span = torch.linspace(0, 1, n_timesteps + 1, device=mu.device, dtype=mu.dtype)
|
52 |
+
if self.t_scheduler == 'cosine':
|
53 |
+
t_span = 1 - torch.cos(t_span * 0.5 * torch.pi)
|
54 |
+
return self.solve_euler(z, t_span=t_span, mu=mu, mask=mask, spks=spks, cond=cond)
|
55 |
+
|
56 |
+
def solve_euler(self, x, t_span, mu, mask, spks, cond):
|
57 |
+
"""
|
58 |
+
Fixed euler solver for ODEs.
|
59 |
+
Args:
|
60 |
+
x (torch.Tensor): random noise
|
61 |
+
t_span (torch.Tensor): n_timesteps interpolated
|
62 |
+
shape: (n_timesteps + 1,)
|
63 |
+
mu (torch.Tensor): output of encoder
|
64 |
+
shape: (batch_size, n_feats, mel_timesteps)
|
65 |
+
mask (torch.Tensor): output_mask
|
66 |
+
shape: (batch_size, 1, mel_timesteps)
|
67 |
+
spks (torch.Tensor, optional): speaker ids. Defaults to None.
|
68 |
+
shape: (batch_size, spk_emb_dim)
|
69 |
+
cond: Not used but kept for future purposes
|
70 |
+
"""
|
71 |
+
t, _, dt = t_span[0], t_span[-1], t_span[1] - t_span[0]
|
72 |
+
t = t.unsqueeze(dim=0)
|
73 |
+
|
74 |
+
# I am storing this because I can later plot it by putting a debugger here and saving it to a file
|
75 |
+
# Or in future might add like a return_all_steps flag
|
76 |
+
sol = []
|
77 |
+
|
78 |
+
for step in range(1, len(t_span)):
|
79 |
+
dphi_dt = self.forward_estimator(x, mask, mu, t, spks, cond)
|
80 |
+
# Classifier-Free Guidance inference introduced in VoiceBox
|
81 |
+
if self.inference_cfg_rate > 0:
|
82 |
+
cfg_dphi_dt = self.forward_estimator(
|
83 |
+
x, mask,
|
84 |
+
torch.zeros_like(mu), t,
|
85 |
+
torch.zeros_like(spks) if spks is not None else None,
|
86 |
+
torch.zeros_like(cond) if cond is not None else None
|
87 |
+
)
|
88 |
+
dphi_dt = ((1.0 + self.inference_cfg_rate) * dphi_dt -
|
89 |
+
self.inference_cfg_rate * cfg_dphi_dt)
|
90 |
+
x = x + dt * dphi_dt
|
91 |
+
t = t + dt
|
92 |
+
sol.append(x)
|
93 |
+
if step < len(t_span) - 1:
|
94 |
+
dt = t_span[step + 1] - t
|
95 |
+
|
96 |
+
return sol[-1]
|
97 |
+
|
98 |
+
def forward_estimator(self, x, mask, mu, t, spks, cond):
|
99 |
+
if isinstance(self.estimator, torch.nn.Module):
|
100 |
+
return self.estimator.forward(x, mask, mu, t, spks, cond)
|
101 |
+
elif isinstance(self.estimator, onnxruntime.InferenceSession):
|
102 |
+
ort_inputs = {
|
103 |
+
'x': x.cpu().numpy(),
|
104 |
+
'mask': mask.cpu().numpy(),
|
105 |
+
'mu': mu.cpu().numpy(),
|
106 |
+
't': t.cpu().numpy(),
|
107 |
+
'spks': spks.cpu().numpy(),
|
108 |
+
'cond': cond.cpu().numpy()
|
109 |
+
}
|
110 |
+
output = self.estimator.run(None, ort_inputs)[0]
|
111 |
+
return torch.tensor(output, dtype=x.dtype, device=x.device)
|
112 |
+
else:
|
113 |
+
self.estimator.set_input_shape('x', (2, 80, x.size(2)))
|
114 |
+
self.estimator.set_input_shape('mask', (2, 1, x.size(2)))
|
115 |
+
self.estimator.set_input_shape('mu', (2, 80, x.size(2)))
|
116 |
+
self.estimator.set_input_shape('t', (2,))
|
117 |
+
self.estimator.set_input_shape('spks', (2, 80))
|
118 |
+
self.estimator.set_input_shape('cond', (2, 80, x.size(2)))
|
119 |
+
# run trt engine
|
120 |
+
self.estimator.execute_v2([x.contiguous().data_ptr(),
|
121 |
+
mask.contiguous().data_ptr(),
|
122 |
+
mu.contiguous().data_ptr(),
|
123 |
+
t.contiguous().data_ptr(),
|
124 |
+
spks.contiguous().data_ptr(),
|
125 |
+
cond.contiguous().data_ptr(),
|
126 |
+
x.data_ptr()])
|
127 |
+
return x
|
128 |
+
|
129 |
+
def compute_loss(self, x1, mask, mu, spks=None, cond=None):
|
130 |
+
"""Computes diffusion loss
|
131 |
+
|
132 |
+
Args:
|
133 |
+
x1 (torch.Tensor): Target
|
134 |
+
shape: (batch_size, n_feats, mo)
|
135 |
+
mask (torch.Tensor): target mask
|
136 |
+
shape: (batch_size, 1, mel_timesteps)
|
137 |
+
mu (torch.Tensor): output of encoder
|
138 |
+
shape: (batch_size, n_feats, mel_timesteps)
|
139 |
+
spks (torch.Tensor, optional): speaker embedding. Defaults to None.
|
140 |
+
shape: (batch_size, spk_emb_dim)
|
141 |
+
|
142 |
+
Returns:
|
143 |
+
loss: conditional flow matching loss
|
144 |
+
y: conditional flow
|
145 |
+
shape: (batch_size, n_feats, mel_timesteps)
|
146 |
+
"""
|
147 |
+
b, _, t = mu.shape
|
148 |
+
|
149 |
+
t = torch.rand([b, 1, 1], device=mu.device, dtype=mu.dtype)
|
150 |
+
if self.t_scheduler == 'cosine':
|
151 |
+
t = 1 - torch.cos(t * 0.5 * torch.pi)
|
152 |
+
|
153 |
+
z = torch.randn_like(x1)
|
154 |
+
y = (1 - (1 - self.sigma_min) * t) * z + t * x1
|
155 |
+
u = x1 - (1 - self.sigma_min) * z
|
156 |
+
|
157 |
+
# during training, we randomly drop condition to trade off mode coverage and sample fidelity
|
158 |
+
if self.training_cfg_rate > 0:
|
159 |
+
cfg_mask = torch.rand(b, device=x1.device) > self.training_cfg_rate
|
160 |
+
mu = mu * cfg_mask.view(-1, 1, 1)
|
161 |
+
if cond is not None:
|
162 |
+
cond = cond * cfg_mask.view(-1, 1, 1)
|
163 |
+
|
164 |
+
pred = self.estimator(y, mask, mu, t.squeeze(), spks, cond)
|
165 |
+
loss = F.mse_loss(pred * mask, u * mask, reduction="sum") / (torch.sum(mask) * u.shape[1])
|
166 |
+
return loss, y
|
167 |
+
|
inspiremusic/flow/length_regulator.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
from typing import Tuple
|
15 |
+
import torch.nn as nn
|
16 |
+
import torch
|
17 |
+
from torch.nn import functional as F
|
18 |
+
from inspiremusic.utils.mask import make_pad_mask
|
19 |
+
|
20 |
+
|
21 |
+
class InterpolateRegulator(nn.Module):
|
22 |
+
def __init__(
|
23 |
+
self,
|
24 |
+
channels: int,
|
25 |
+
sampling_ratios: Tuple,
|
26 |
+
out_channels: int = None,
|
27 |
+
groups: int = 1,
|
28 |
+
):
|
29 |
+
super().__init__()
|
30 |
+
self.sampling_ratios = sampling_ratios
|
31 |
+
out_channels = out_channels or channels
|
32 |
+
model = nn.ModuleList([])
|
33 |
+
if len(sampling_ratios) > 0:
|
34 |
+
for _ in sampling_ratios:
|
35 |
+
module = nn.Conv1d(channels, channels, 3, 1, 1)
|
36 |
+
norm = nn.GroupNorm(groups, channels)
|
37 |
+
act = nn.Mish()
|
38 |
+
model.extend([module, norm, act])
|
39 |
+
model.append(
|
40 |
+
nn.Conv1d(channels, out_channels, 1, 1)
|
41 |
+
)
|
42 |
+
self.model = nn.Sequential(*model)
|
43 |
+
|
44 |
+
def forward(self, x, ylens=None):
|
45 |
+
# x in (B, T, D)
|
46 |
+
mask = (~make_pad_mask(ylens)).to(x).unsqueeze(-1)
|
47 |
+
x = F.interpolate(x.transpose(1, 2).contiguous(), size=ylens.max(), mode='linear')
|
48 |
+
out = self.model(x).transpose(1, 2).contiguous()
|
49 |
+
olens = ylens
|
50 |
+
return out * mask, olens
|
51 |
+
|
52 |
+
def inference(self, x1, x2, mel_len1, mel_len2, input_frame_rate=50):
|
53 |
+
# in inference mode, interploate prompt token and token(head/mid/tail) seprately, so we can get a clear separation point of mel
|
54 |
+
# x in (B, T, D)
|
55 |
+
if x2.shape[1] > 40:
|
56 |
+
x2_head = F.interpolate(x2[:, :20].transpose(1, 2).contiguous(), size=int(20 / input_frame_rate * 22050 / 256), mode='linear')
|
57 |
+
x2_mid = F.interpolate(x2[:, 20:-20].transpose(1, 2).contiguous(), size=mel_len2 - int(20 / input_frame_rate * 22050 / 256) * 2,
|
58 |
+
mode='linear')
|
59 |
+
x2_tail = F.interpolate(x2[:, -20:].transpose(1, 2).contiguous(), size=int(20 / input_frame_rate * 22050 / 256), mode='linear')
|
60 |
+
x2 = torch.concat([x2_head, x2_mid, x2_tail], dim=2)
|
61 |
+
else:
|
62 |
+
x2 = F.interpolate(x2.transpose(1, 2).contiguous(), size=mel_len2, mode='linear')
|
63 |
+
if x1.shape[1] != 0:
|
64 |
+
x1 = F.interpolate(x1.transpose(1, 2).contiguous(), size=mel_len1, mode='linear')
|
65 |
+
x = torch.concat([x1, x2], dim=2)
|
66 |
+
else:
|
67 |
+
x = x2
|
68 |
+
out = self.model(x).transpose(1, 2).contiguous()
|
69 |
+
return out, mel_len1 + mel_len2
|
inspiremusic/hifigan/discriminator.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
from torch.nn.utils import weight_norm
|
4 |
+
from typing import List, Optional, Tuple
|
5 |
+
from einops import rearrange
|
6 |
+
from torchaudio.transforms import Spectrogram
|
7 |
+
|
8 |
+
|
9 |
+
class MultipleDiscriminator(nn.Module):
|
10 |
+
def __init__(
|
11 |
+
self, mpd: nn.Module, mrd: nn.Module
|
12 |
+
):
|
13 |
+
super().__init__()
|
14 |
+
self.mpd = mpd
|
15 |
+
self.mrd = mrd
|
16 |
+
|
17 |
+
def forward(self, y: torch.Tensor, y_hat: torch.Tensor):
|
18 |
+
y_d_rs, y_d_gs, fmap_rs, fmap_gs = [], [], [], []
|
19 |
+
this_y_d_rs, this_y_d_gs, this_fmap_rs, this_fmap_gs = self.mpd(y.unsqueeze(dim=1), y_hat.unsqueeze(dim=1))
|
20 |
+
y_d_rs += this_y_d_rs
|
21 |
+
y_d_gs += this_y_d_gs
|
22 |
+
fmap_rs += this_fmap_rs
|
23 |
+
fmap_gs += this_fmap_gs
|
24 |
+
this_y_d_rs, this_y_d_gs, this_fmap_rs, this_fmap_gs = self.mrd(y, y_hat)
|
25 |
+
y_d_rs += this_y_d_rs
|
26 |
+
y_d_gs += this_y_d_gs
|
27 |
+
fmap_rs += this_fmap_rs
|
28 |
+
fmap_gs += this_fmap_gs
|
29 |
+
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
|
30 |
+
|
31 |
+
|
32 |
+
class MultiResolutionDiscriminator(nn.Module):
|
33 |
+
def __init__(
|
34 |
+
self,
|
35 |
+
fft_sizes: Tuple[int, ...] = (2048, 1024, 512),
|
36 |
+
num_embeddings: Optional[int] = None,
|
37 |
+
):
|
38 |
+
"""
|
39 |
+
Multi-Resolution Discriminator module adapted from https://github.com/descriptinc/descript-audio-codec.
|
40 |
+
Additionally, it allows incorporating conditional information with a learned embeddings table.
|
41 |
+
|
42 |
+
Args:
|
43 |
+
fft_sizes (tuple[int]): Tuple of window lengths for FFT. Defaults to (2048, 1024, 512).
|
44 |
+
num_embeddings (int, optional): Number of embeddings. None means non-conditional discriminator.
|
45 |
+
Defaults to None.
|
46 |
+
"""
|
47 |
+
|
48 |
+
super().__init__()
|
49 |
+
self.discriminators = nn.ModuleList(
|
50 |
+
[DiscriminatorR(window_length=w, num_embeddings=num_embeddings) for w in fft_sizes]
|
51 |
+
)
|
52 |
+
|
53 |
+
def forward(
|
54 |
+
self, y: torch.Tensor, y_hat: torch.Tensor, bandwidth_id: torch.Tensor = None
|
55 |
+
) -> Tuple[List[torch.Tensor], List[torch.Tensor], List[List[torch.Tensor]], List[List[torch.Tensor]]]:
|
56 |
+
y_d_rs = []
|
57 |
+
y_d_gs = []
|
58 |
+
fmap_rs = []
|
59 |
+
fmap_gs = []
|
60 |
+
|
61 |
+
for d in self.discriminators:
|
62 |
+
y_d_r, fmap_r = d(x=y, cond_embedding_id=bandwidth_id)
|
63 |
+
y_d_g, fmap_g = d(x=y_hat, cond_embedding_id=bandwidth_id)
|
64 |
+
y_d_rs.append(y_d_r)
|
65 |
+
fmap_rs.append(fmap_r)
|
66 |
+
y_d_gs.append(y_d_g)
|
67 |
+
fmap_gs.append(fmap_g)
|
68 |
+
|
69 |
+
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
|
70 |
+
|
71 |
+
|
72 |
+
class DiscriminatorR(nn.Module):
|
73 |
+
def __init__(
|
74 |
+
self,
|
75 |
+
window_length: int,
|
76 |
+
num_embeddings: Optional[int] = None,
|
77 |
+
channels: int = 32,
|
78 |
+
hop_factor: float = 0.25,
|
79 |
+
bands: Tuple[Tuple[float, float], ...] = ((0.0, 0.1), (0.1, 0.25), (0.25, 0.5), (0.5, 0.75), (0.75, 1.0)),
|
80 |
+
):
|
81 |
+
super().__init__()
|
82 |
+
self.window_length = window_length
|
83 |
+
self.hop_factor = hop_factor
|
84 |
+
self.spec_fn = Spectrogram(
|
85 |
+
n_fft=window_length, hop_length=int(window_length * hop_factor), win_length=window_length, power=None
|
86 |
+
)
|
87 |
+
n_fft = window_length // 2 + 1
|
88 |
+
bands = [(int(b[0] * n_fft), int(b[1] * n_fft)) for b in bands]
|
89 |
+
self.bands = bands
|
90 |
+
convs = lambda: nn.ModuleList(
|
91 |
+
[
|
92 |
+
weight_norm(nn.Conv2d(2, channels, (3, 9), (1, 1), padding=(1, 4))),
|
93 |
+
weight_norm(nn.Conv2d(channels, channels, (3, 9), (1, 2), padding=(1, 4))),
|
94 |
+
weight_norm(nn.Conv2d(channels, channels, (3, 9), (1, 2), padding=(1, 4))),
|
95 |
+
weight_norm(nn.Conv2d(channels, channels, (3, 9), (1, 2), padding=(1, 4))),
|
96 |
+
weight_norm(nn.Conv2d(channels, channels, (3, 3), (1, 1), padding=(1, 1))),
|
97 |
+
]
|
98 |
+
)
|
99 |
+
self.band_convs = nn.ModuleList([convs() for _ in range(len(self.bands))])
|
100 |
+
|
101 |
+
if num_embeddings is not None:
|
102 |
+
self.emb = torch.nn.Embedding(num_embeddings=num_embeddings, embedding_dim=channels)
|
103 |
+
torch.nn.init.zeros_(self.emb.weight)
|
104 |
+
|
105 |
+
self.conv_post = weight_norm(nn.Conv2d(channels, 1, (3, 3), (1, 1), padding=(1, 1)))
|
106 |
+
|
107 |
+
def spectrogram(self, x):
|
108 |
+
# Remove DC offset
|
109 |
+
x = x - x.mean(dim=-1, keepdims=True)
|
110 |
+
# Peak normalize the volume of input audio
|
111 |
+
x = 0.8 * x / (x.abs().max(dim=-1, keepdim=True)[0] + 1e-9)
|
112 |
+
x = self.spec_fn(x)
|
113 |
+
x = torch.view_as_real(x)
|
114 |
+
x = rearrange(x, "b f t c -> b c t f")
|
115 |
+
# Split into bands
|
116 |
+
x_bands = [x[..., b[0]: b[1]] for b in self.bands]
|
117 |
+
return x_bands
|
118 |
+
|
119 |
+
def forward(self, x: torch.Tensor, cond_embedding_id: torch.Tensor = None):
|
120 |
+
x_bands = self.spectrogram(x)
|
121 |
+
fmap = []
|
122 |
+
x = []
|
123 |
+
for band, stack in zip(x_bands, self.band_convs):
|
124 |
+
for i, layer in enumerate(stack):
|
125 |
+
band = layer(band)
|
126 |
+
band = torch.nn.functional.leaky_relu(band, 0.1)
|
127 |
+
if i > 0:
|
128 |
+
fmap.append(band)
|
129 |
+
x.append(band)
|
130 |
+
x = torch.cat(x, dim=-1)
|
131 |
+
if cond_embedding_id is not None:
|
132 |
+
emb = self.emb(cond_embedding_id)
|
133 |
+
h = (emb.view(1, -1, 1, 1) * x).sum(dim=1, keepdims=True)
|
134 |
+
else:
|
135 |
+
h = 0
|
136 |
+
x = self.conv_post(x)
|
137 |
+
fmap.append(x)
|
138 |
+
x += h
|
139 |
+
|
140 |
+
return x, fmap
|
inspiremusic/hifigan/f0_predictor.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
import torch
|
15 |
+
import torch.nn as nn
|
16 |
+
from torch.nn.utils import weight_norm
|
17 |
+
|
18 |
+
|
19 |
+
class ConvRNNF0Predictor(nn.Module):
|
20 |
+
def __init__(self,
|
21 |
+
num_class: int = 1,
|
22 |
+
in_channels: int = 80,
|
23 |
+
cond_channels: int = 512
|
24 |
+
):
|
25 |
+
super().__init__()
|
26 |
+
|
27 |
+
self.num_class = num_class
|
28 |
+
self.condnet = nn.Sequential(
|
29 |
+
weight_norm(
|
30 |
+
nn.Conv1d(in_channels, cond_channels, kernel_size=3, padding=1)
|
31 |
+
),
|
32 |
+
nn.ELU(),
|
33 |
+
weight_norm(
|
34 |
+
nn.Conv1d(cond_channels, cond_channels, kernel_size=3, padding=1)
|
35 |
+
),
|
36 |
+
nn.ELU(),
|
37 |
+
weight_norm(
|
38 |
+
nn.Conv1d(cond_channels, cond_channels, kernel_size=3, padding=1)
|
39 |
+
),
|
40 |
+
nn.ELU(),
|
41 |
+
weight_norm(
|
42 |
+
nn.Conv1d(cond_channels, cond_channels, kernel_size=3, padding=1)
|
43 |
+
),
|
44 |
+
nn.ELU(),
|
45 |
+
weight_norm(
|
46 |
+
nn.Conv1d(cond_channels, cond_channels, kernel_size=3, padding=1)
|
47 |
+
),
|
48 |
+
nn.ELU(),
|
49 |
+
)
|
50 |
+
self.classifier = nn.Linear(in_features=cond_channels, out_features=self.num_class)
|
51 |
+
|
52 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
53 |
+
x = self.condnet(x)
|
54 |
+
x = x.transpose(1, 2)
|
55 |
+
return torch.abs(self.classifier(x).squeeze(-1))
|
inspiremusic/hifigan/generator.py
ADDED
@@ -0,0 +1,411 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
|
15 |
+
"""HIFI-GAN"""
|
16 |
+
|
17 |
+
from typing import Dict, Optional, List
|
18 |
+
import numpy as np
|
19 |
+
from scipy.signal import get_window
|
20 |
+
import torch
|
21 |
+
import torch.nn as nn
|
22 |
+
import torch.nn.functional as F
|
23 |
+
from torch.nn import Conv1d
|
24 |
+
from torch.nn import ConvTranspose1d
|
25 |
+
from torch.nn.utils import remove_weight_norm
|
26 |
+
from torch.nn.utils import weight_norm
|
27 |
+
from torch.distributions.uniform import Uniform
|
28 |
+
|
29 |
+
from inspiremusic.transformer.activation import Snake
|
30 |
+
from inspiremusic.utils.common import get_padding
|
31 |
+
from inspiremusic.utils.common import init_weights
|
32 |
+
|
33 |
+
|
34 |
+
"""hifigan based generator implementation.
|
35 |
+
|
36 |
+
This code is modified from https://github.com/jik876/hifi-gan
|
37 |
+
,https://github.com/kan-bayashi/ParallelWaveGAN and
|
38 |
+
https://github.com/NVIDIA/BigVGAN
|
39 |
+
|
40 |
+
"""
|
41 |
+
|
42 |
+
|
43 |
+
class ResBlock(torch.nn.Module):
|
44 |
+
"""Residual block module in HiFiGAN/BigVGAN."""
|
45 |
+
def __init__(
|
46 |
+
self,
|
47 |
+
channels: int = 512,
|
48 |
+
kernel_size: int = 3,
|
49 |
+
dilations: List[int] = [1, 3, 5],
|
50 |
+
):
|
51 |
+
super(ResBlock, self).__init__()
|
52 |
+
self.convs1 = nn.ModuleList()
|
53 |
+
self.convs2 = nn.ModuleList()
|
54 |
+
|
55 |
+
for dilation in dilations:
|
56 |
+
self.convs1.append(
|
57 |
+
weight_norm(
|
58 |
+
Conv1d(
|
59 |
+
channels,
|
60 |
+
channels,
|
61 |
+
kernel_size,
|
62 |
+
1,
|
63 |
+
dilation=dilation,
|
64 |
+
padding=get_padding(kernel_size, dilation)
|
65 |
+
)
|
66 |
+
)
|
67 |
+
)
|
68 |
+
self.convs2.append(
|
69 |
+
weight_norm(
|
70 |
+
Conv1d(
|
71 |
+
channels,
|
72 |
+
channels,
|
73 |
+
kernel_size,
|
74 |
+
1,
|
75 |
+
dilation=1,
|
76 |
+
padding=get_padding(kernel_size, 1)
|
77 |
+
)
|
78 |
+
)
|
79 |
+
)
|
80 |
+
self.convs1.apply(init_weights)
|
81 |
+
self.convs2.apply(init_weights)
|
82 |
+
self.activations1 = nn.ModuleList([
|
83 |
+
Snake(channels, alpha_logscale=False)
|
84 |
+
for _ in range(len(self.convs1))
|
85 |
+
])
|
86 |
+
self.activations2 = nn.ModuleList([
|
87 |
+
Snake(channels, alpha_logscale=False)
|
88 |
+
for _ in range(len(self.convs2))
|
89 |
+
])
|
90 |
+
|
91 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
92 |
+
for idx in range(len(self.convs1)):
|
93 |
+
xt = self.activations1[idx](x)
|
94 |
+
xt = self.convs1[idx](xt)
|
95 |
+
xt = self.activations2[idx](xt)
|
96 |
+
xt = self.convs2[idx](xt)
|
97 |
+
x = xt + x
|
98 |
+
return x
|
99 |
+
|
100 |
+
def remove_weight_norm(self):
|
101 |
+
for idx in range(len(self.convs1)):
|
102 |
+
remove_weight_norm(self.convs1[idx])
|
103 |
+
remove_weight_norm(self.convs2[idx])
|
104 |
+
|
105 |
+
|
106 |
+
class SineGen(torch.nn.Module):
|
107 |
+
""" Definition of sine generator
|
108 |
+
SineGen(samp_rate, harmonic_num = 0,
|
109 |
+
sine_amp = 0.1, noise_std = 0.003,
|
110 |
+
voiced_threshold = 0,
|
111 |
+
flag_for_pulse=False)
|
112 |
+
samp_rate: sampling rate in Hz
|
113 |
+
harmonic_num: number of harmonic overtones (default 0)
|
114 |
+
sine_amp: amplitude of sine-wavefrom (default 0.1)
|
115 |
+
noise_std: std of Gaussian noise (default 0.003)
|
116 |
+
voiced_thoreshold: F0 threshold for U/V classification (default 0)
|
117 |
+
flag_for_pulse: this SinGen is used inside PulseGen (default False)
|
118 |
+
Note: when flag_for_pulse is True, the first time step of a voiced
|
119 |
+
segment is always sin(np.pi) or cos(0)
|
120 |
+
"""
|
121 |
+
|
122 |
+
def __init__(self, samp_rate, harmonic_num=0,
|
123 |
+
sine_amp=0.1, noise_std=0.003,
|
124 |
+
voiced_threshold=0):
|
125 |
+
super(SineGen, self).__init__()
|
126 |
+
self.sine_amp = sine_amp
|
127 |
+
self.noise_std = noise_std
|
128 |
+
self.harmonic_num = harmonic_num
|
129 |
+
self.sampling_rate = samp_rate
|
130 |
+
self.voiced_threshold = voiced_threshold
|
131 |
+
|
132 |
+
def _f02uv(self, f0):
|
133 |
+
# generate uv signal
|
134 |
+
uv = (f0 > self.voiced_threshold).type(torch.float32)
|
135 |
+
return uv
|
136 |
+
|
137 |
+
@torch.no_grad()
|
138 |
+
def forward(self, f0):
|
139 |
+
"""
|
140 |
+
:param f0: [B, 1, sample_len], Hz
|
141 |
+
:return: [B, 1, sample_len]
|
142 |
+
"""
|
143 |
+
|
144 |
+
F_mat = torch.zeros((f0.size(0), self.harmonic_num + 1, f0.size(-1))).to(f0.device)
|
145 |
+
for i in range(self.harmonic_num + 1):
|
146 |
+
F_mat[:, i: i + 1, :] = f0 * (i + 1) / self.sampling_rate
|
147 |
+
|
148 |
+
theta_mat = 2 * np.pi * (torch.cumsum(F_mat, dim=-1) % 1)
|
149 |
+
u_dist = Uniform(low=-np.pi, high=np.pi)
|
150 |
+
phase_vec = u_dist.sample(sample_shape=(f0.size(0), self.harmonic_num + 1, 1)).to(F_mat.device)
|
151 |
+
phase_vec[:, 0, :] = 0
|
152 |
+
|
153 |
+
# generate sine waveforms
|
154 |
+
sine_waves = self.sine_amp * torch.sin(theta_mat + phase_vec)
|
155 |
+
|
156 |
+
# generate uv signal
|
157 |
+
uv = self._f02uv(f0)
|
158 |
+
|
159 |
+
# noise: for unvoiced should be similar to sine_amp
|
160 |
+
# std = self.sine_amp/3 -> max value ~ self.sine_amp
|
161 |
+
# . for voiced regions is self.noise_std
|
162 |
+
noise_amp = uv * self.noise_std + (1 - uv) * self.sine_amp / 3
|
163 |
+
noise = noise_amp * torch.randn_like(sine_waves)
|
164 |
+
|
165 |
+
# first: set the unvoiced part to 0 by uv
|
166 |
+
# then: additive noise
|
167 |
+
sine_waves = sine_waves * uv + noise
|
168 |
+
return sine_waves, uv, noise
|
169 |
+
|
170 |
+
|
171 |
+
class SourceModuleHnNSF(torch.nn.Module):
|
172 |
+
""" SourceModule for hn-nsf
|
173 |
+
SourceModule(sampling_rate, harmonic_num=0, sine_amp=0.1,
|
174 |
+
add_noise_std=0.003, voiced_threshod=0)
|
175 |
+
sampling_rate: sampling_rate in Hz
|
176 |
+
harmonic_num: number of harmonic above F0 (default: 0)
|
177 |
+
sine_amp: amplitude of sine source signal (default: 0.1)
|
178 |
+
add_noise_std: std of additive Gaussian noise (default: 0.003)
|
179 |
+
note that amplitude of noise in unvoiced is decided
|
180 |
+
by sine_amp
|
181 |
+
voiced_threshold: threhold to set U/V given F0 (default: 0)
|
182 |
+
Sine_source, noise_source = SourceModuleHnNSF(F0_sampled)
|
183 |
+
F0_sampled (batchsize, length, 1)
|
184 |
+
Sine_source (batchsize, length, 1)
|
185 |
+
noise_source (batchsize, length 1)
|
186 |
+
uv (batchsize, length, 1)
|
187 |
+
"""
|
188 |
+
|
189 |
+
def __init__(self, sampling_rate, upsample_scale, harmonic_num=0, sine_amp=0.1,
|
190 |
+
add_noise_std=0.003, voiced_threshod=0):
|
191 |
+
super(SourceModuleHnNSF, self).__init__()
|
192 |
+
|
193 |
+
self.sine_amp = sine_amp
|
194 |
+
self.noise_std = add_noise_std
|
195 |
+
|
196 |
+
# to produce sine waveforms
|
197 |
+
self.l_sin_gen = SineGen(sampling_rate, harmonic_num,
|
198 |
+
sine_amp, add_noise_std, voiced_threshod)
|
199 |
+
|
200 |
+
# to merge source harmonics into a single excitation
|
201 |
+
self.l_linear = torch.nn.Linear(harmonic_num + 1, 1)
|
202 |
+
self.l_tanh = torch.nn.Tanh()
|
203 |
+
|
204 |
+
def forward(self, x):
|
205 |
+
"""
|
206 |
+
Sine_source, noise_source = SourceModuleHnNSF(F0_sampled)
|
207 |
+
F0_sampled (batchsize, length, 1)
|
208 |
+
Sine_source (batchsize, length, 1)
|
209 |
+
noise_source (batchsize, length 1)
|
210 |
+
"""
|
211 |
+
# source for harmonic branch
|
212 |
+
with torch.no_grad():
|
213 |
+
sine_wavs, uv, _ = self.l_sin_gen(x.transpose(1, 2))
|
214 |
+
sine_wavs = sine_wavs.transpose(1, 2)
|
215 |
+
uv = uv.transpose(1, 2)
|
216 |
+
sine_merge = self.l_tanh(self.l_linear(sine_wavs))
|
217 |
+
|
218 |
+
# source for noise branch, in the same shape as uv
|
219 |
+
noise = torch.randn_like(uv) * self.sine_amp / 3
|
220 |
+
return sine_merge, noise, uv
|
221 |
+
|
222 |
+
|
223 |
+
class HiFTGenerator(nn.Module):
|
224 |
+
"""
|
225 |
+
HiFTNet Generator: Neural Source Filter + ISTFTNet
|
226 |
+
https://arxiv.org/abs/2309.09493
|
227 |
+
"""
|
228 |
+
def __init__(
|
229 |
+
self,
|
230 |
+
in_channels: int = 80,
|
231 |
+
base_channels: int = 512,
|
232 |
+
nb_harmonics: int = 8,
|
233 |
+
sampling_rate: int = 22050,
|
234 |
+
nsf_alpha: float = 0.1,
|
235 |
+
nsf_sigma: float = 0.003,
|
236 |
+
nsf_voiced_threshold: float = 10,
|
237 |
+
upsample_rates: List[int] = [8, 8],
|
238 |
+
upsample_kernel_sizes: List[int] = [16, 16],
|
239 |
+
istft_params: Dict[str, int] = {"n_fft": 16, "hop_len": 4},
|
240 |
+
resblock_kernel_sizes: List[int] = [3, 7, 11],
|
241 |
+
resblock_dilation_sizes: List[List[int]] = [[1, 3, 5], [1, 3, 5], [1, 3, 5]],
|
242 |
+
source_resblock_kernel_sizes: List[int] = [7, 11],
|
243 |
+
source_resblock_dilation_sizes: List[List[int]] = [[1, 3, 5], [1, 3, 5]],
|
244 |
+
lrelu_slope: float = 0.1,
|
245 |
+
audio_limit: float = 0.99,
|
246 |
+
f0_predictor: torch.nn.Module = None,
|
247 |
+
):
|
248 |
+
super(HiFTGenerator, self).__init__()
|
249 |
+
|
250 |
+
self.out_channels = 1
|
251 |
+
self.nb_harmonics = nb_harmonics
|
252 |
+
self.sampling_rate = sampling_rate
|
253 |
+
self.istft_params = istft_params
|
254 |
+
self.lrelu_slope = lrelu_slope
|
255 |
+
self.audio_limit = audio_limit
|
256 |
+
|
257 |
+
self.num_kernels = len(resblock_kernel_sizes)
|
258 |
+
self.num_upsamples = len(upsample_rates)
|
259 |
+
self.m_source = SourceModuleHnNSF(
|
260 |
+
sampling_rate=sampling_rate,
|
261 |
+
upsample_scale=np.prod(upsample_rates) * istft_params["hop_len"],
|
262 |
+
harmonic_num=nb_harmonics,
|
263 |
+
sine_amp=nsf_alpha,
|
264 |
+
add_noise_std=nsf_sigma,
|
265 |
+
voiced_threshod=nsf_voiced_threshold)
|
266 |
+
self.f0_upsamp = torch.nn.Upsample(scale_factor=np.prod(upsample_rates) * istft_params["hop_len"])
|
267 |
+
|
268 |
+
self.conv_pre = weight_norm(
|
269 |
+
Conv1d(in_channels, base_channels, 7, 1, padding=3)
|
270 |
+
)
|
271 |
+
|
272 |
+
# Up
|
273 |
+
self.ups = nn.ModuleList()
|
274 |
+
for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)):
|
275 |
+
self.ups.append(
|
276 |
+
weight_norm(
|
277 |
+
ConvTranspose1d(
|
278 |
+
base_channels // (2**i),
|
279 |
+
base_channels // (2**(i + 1)),
|
280 |
+
k,
|
281 |
+
u,
|
282 |
+
padding=(k - u) // 2,
|
283 |
+
)
|
284 |
+
)
|
285 |
+
)
|
286 |
+
|
287 |
+
# Down
|
288 |
+
self.source_downs = nn.ModuleList()
|
289 |
+
self.source_resblocks = nn.ModuleList()
|
290 |
+
downsample_rates = [1] + upsample_rates[::-1][:-1]
|
291 |
+
downsample_cum_rates = np.cumprod(downsample_rates)
|
292 |
+
for i, (u, k, d) in enumerate(zip(downsample_cum_rates[::-1], source_resblock_kernel_sizes, source_resblock_dilation_sizes)):
|
293 |
+
if u == 1:
|
294 |
+
self.source_downs.append(
|
295 |
+
Conv1d(istft_params["n_fft"] + 2, base_channels // (2 ** (i + 1)), 1, 1)
|
296 |
+
)
|
297 |
+
else:
|
298 |
+
self.source_downs.append(
|
299 |
+
Conv1d(istft_params["n_fft"] + 2, base_channels // (2 ** (i + 1)), u * 2, u, padding=(u // 2))
|
300 |
+
)
|
301 |
+
|
302 |
+
self.source_resblocks.append(
|
303 |
+
ResBlock(base_channels // (2 ** (i + 1)), k, d)
|
304 |
+
)
|
305 |
+
|
306 |
+
self.resblocks = nn.ModuleList()
|
307 |
+
for i in range(len(self.ups)):
|
308 |
+
ch = base_channels // (2**(i + 1))
|
309 |
+
for _, (k, d) in enumerate(zip(resblock_kernel_sizes, resblock_dilation_sizes)):
|
310 |
+
self.resblocks.append(ResBlock(ch, k, d))
|
311 |
+
|
312 |
+
self.conv_post = weight_norm(Conv1d(ch, istft_params["n_fft"] + 2, 7, 1, padding=3))
|
313 |
+
self.ups.apply(init_weights)
|
314 |
+
self.conv_post.apply(init_weights)
|
315 |
+
self.reflection_pad = nn.ReflectionPad1d((1, 0))
|
316 |
+
self.stft_window = torch.from_numpy(get_window("hann", istft_params["n_fft"], fftbins=True).astype(np.float32))
|
317 |
+
self.f0_predictor = f0_predictor
|
318 |
+
|
319 |
+
def remove_weight_norm(self):
|
320 |
+
print('Removing weight norm...')
|
321 |
+
for l in self.ups:
|
322 |
+
remove_weight_norm(l)
|
323 |
+
for l in self.resblocks:
|
324 |
+
l.remove_weight_norm()
|
325 |
+
remove_weight_norm(self.conv_pre)
|
326 |
+
remove_weight_norm(self.conv_post)
|
327 |
+
self.m_source.remove_weight_norm()
|
328 |
+
for l in self.source_downs:
|
329 |
+
remove_weight_norm(l)
|
330 |
+
for l in self.source_resblocks:
|
331 |
+
l.remove_weight_norm()
|
332 |
+
|
333 |
+
def _stft(self, x):
|
334 |
+
spec = torch.stft(
|
335 |
+
x,
|
336 |
+
self.istft_params["n_fft"], self.istft_params["hop_len"], self.istft_params["n_fft"], window=self.stft_window.to(x.device),
|
337 |
+
return_complex=True)
|
338 |
+
spec = torch.view_as_real(spec) # [B, F, TT, 2]
|
339 |
+
return spec[..., 0], spec[..., 1]
|
340 |
+
|
341 |
+
def _istft(self, magnitude, phase):
|
342 |
+
magnitude = torch.clip(magnitude, max=1e2)
|
343 |
+
real = magnitude * torch.cos(phase)
|
344 |
+
img = magnitude * torch.sin(phase)
|
345 |
+
inverse_transform = torch.istft(torch.complex(real, img), self.istft_params["n_fft"], self.istft_params["hop_len"],
|
346 |
+
self.istft_params["n_fft"], window=self.stft_window.to(magnitude.device))
|
347 |
+
return inverse_transform
|
348 |
+
|
349 |
+
def decode(self, x: torch.Tensor, s: torch.Tensor = torch.zeros(1, 1, 0)) -> torch.Tensor:
|
350 |
+
s_stft_real, s_stft_imag = self._stft(s.squeeze(1))
|
351 |
+
s_stft = torch.cat([s_stft_real, s_stft_imag], dim=1)
|
352 |
+
|
353 |
+
x = self.conv_pre(x)
|
354 |
+
for i in range(self.num_upsamples):
|
355 |
+
x = F.leaky_relu(x, self.lrelu_slope)
|
356 |
+
x = self.ups[i](x)
|
357 |
+
|
358 |
+
if i == self.num_upsamples - 1:
|
359 |
+
x = self.reflection_pad(x)
|
360 |
+
|
361 |
+
# fusion
|
362 |
+
si = self.source_downs[i](s_stft)
|
363 |
+
si = self.source_resblocks[i](si)
|
364 |
+
x = x + si
|
365 |
+
|
366 |
+
xs = None
|
367 |
+
for j in range(self.num_kernels):
|
368 |
+
if xs is None:
|
369 |
+
xs = self.resblocks[i * self.num_kernels + j](x)
|
370 |
+
else:
|
371 |
+
xs += self.resblocks[i * self.num_kernels + j](x)
|
372 |
+
x = xs / self.num_kernels
|
373 |
+
|
374 |
+
x = F.leaky_relu(x)
|
375 |
+
x = self.conv_post(x)
|
376 |
+
magnitude = torch.exp(x[:, :self.istft_params["n_fft"] // 2 + 1, :])
|
377 |
+
phase = torch.sin(x[:, self.istft_params["n_fft"] // 2 + 1:, :]) # actually, sin is redundancy
|
378 |
+
|
379 |
+
x = self._istft(magnitude, phase)
|
380 |
+
x = torch.clamp(x, -self.audio_limit, self.audio_limit)
|
381 |
+
return x
|
382 |
+
|
383 |
+
def forward(
|
384 |
+
self,
|
385 |
+
batch: dict,
|
386 |
+
device: torch.device,
|
387 |
+
) -> Dict[str, Optional[torch.Tensor]]:
|
388 |
+
speech_feat = batch['speech_feat'].transpose(1, 2).to(device)
|
389 |
+
# mel->f0
|
390 |
+
f0 = self.f0_predictor(speech_feat)
|
391 |
+
# f0->source
|
392 |
+
s = self.f0_upsamp(f0[:, None]).transpose(1, 2) # bs,n,t
|
393 |
+
s, _, _ = self.m_source(s)
|
394 |
+
s = s.transpose(1, 2)
|
395 |
+
# mel+source->speech
|
396 |
+
generated_speech = self.decode(x=speech_feat, s=s)
|
397 |
+
return generated_speech, f0
|
398 |
+
|
399 |
+
@torch.inference_mode()
|
400 |
+
def inference(self, speech_feat: torch.Tensor, cache_source: torch.Tensor = torch.zeros(1, 1, 0)) -> torch.Tensor:
|
401 |
+
# mel->f0
|
402 |
+
f0 = self.f0_predictor(speech_feat)
|
403 |
+
# f0->source
|
404 |
+
s = self.f0_upsamp(f0[:, None]).transpose(1, 2) # bs,n,t
|
405 |
+
s, _, _ = self.m_source(s)
|
406 |
+
s = s.transpose(1, 2)
|
407 |
+
# use cache_source to avoid glitch
|
408 |
+
if cache_source.shape[2] != 0:
|
409 |
+
s[:, :, :cache_source.shape[2]] = cache_source
|
410 |
+
generated_speech = self.decode(x=speech_feat, s=s)
|
411 |
+
return generated_speech, s
|
inspiremusic/hifigan/hifigan.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, Optional
|
2 |
+
import torch
|
3 |
+
import torch.nn as nn
|
4 |
+
import torch.nn.functional as F
|
5 |
+
from matcha.hifigan.models import feature_loss, generator_loss, discriminator_loss
|
6 |
+
from inspiremusic.utils.losses import tpr_loss, mel_loss
|
7 |
+
|
8 |
+
class HiFiGan(nn.Module):
|
9 |
+
def __init__(self, generator, discriminator, mel_spec_transform,
|
10 |
+
multi_mel_spectral_recon_loss_weight=45, feat_match_loss_weight=2.0,
|
11 |
+
tpr_loss_weight=1.0, tpr_loss_tau=0.04):
|
12 |
+
super(HiFiGan, self).__init__()
|
13 |
+
self.generator = generator
|
14 |
+
self.discriminator = discriminator
|
15 |
+
self.mel_spec_transform = mel_spec_transform
|
16 |
+
self.multi_mel_spectral_recon_loss_weight = multi_mel_spectral_recon_loss_weight
|
17 |
+
self.feat_match_loss_weight = feat_match_loss_weight
|
18 |
+
self.tpr_loss_weight = tpr_loss_weight
|
19 |
+
self.tpr_loss_tau = tpr_loss_tau
|
20 |
+
|
21 |
+
def forward(
|
22 |
+
self,
|
23 |
+
batch: dict,
|
24 |
+
device: torch.device,
|
25 |
+
) -> Dict[str, Optional[torch.Tensor]]:
|
26 |
+
if batch['turn'] == 'generator':
|
27 |
+
return self.forward_generator(batch, device)
|
28 |
+
else:
|
29 |
+
return self.forward_discriminator(batch, device)
|
30 |
+
|
31 |
+
def forward_generator(self, batch, device):
|
32 |
+
real_speech = batch['speech'].to(device)
|
33 |
+
pitch_feat = batch['pitch_feat'].to(device)
|
34 |
+
# 1. calculate generator outputs
|
35 |
+
generated_speech, generated_f0 = self.generator(batch, device)
|
36 |
+
# 2. calculate discriminator outputs
|
37 |
+
y_d_rs, y_d_gs, fmap_rs, fmap_gs = self.discriminator(real_speech, generated_speech)
|
38 |
+
# 3. calculate generator losses, feature loss, mel loss, tpr losses [Optional]
|
39 |
+
loss_gen, _ = generator_loss(y_d_gs)
|
40 |
+
loss_fm = feature_loss(fmap_rs, fmap_gs)
|
41 |
+
loss_mel = mel_loss(real_speech, generated_speech, self.mel_spec_transform)
|
42 |
+
if self.tpr_loss_weight != 0:
|
43 |
+
loss_tpr = tpr_loss(y_d_rs, y_d_gs, self.tpr_loss_tau)
|
44 |
+
else:
|
45 |
+
loss_tpr = torch.zeros(1).to(device)
|
46 |
+
loss_f0 = F.l1_loss(generated_f0, pitch_feat)
|
47 |
+
loss = loss_gen + self.feat_match_loss_weight * loss_fm + \
|
48 |
+
self.multi_mel_spectral_recon_loss_weight * loss_mel + \
|
49 |
+
self.tpr_loss_weight * loss_tpr + loss_f0
|
50 |
+
return {'loss': loss, 'loss_gen': loss_gen, 'loss_fm': loss_fm, 'loss_mel': loss_mel, 'loss_tpr': loss_tpr, 'loss_f0': loss_f0}
|
51 |
+
|
52 |
+
def forward_discriminator(self, batch, device):
|
53 |
+
real_speech = batch['speech'].to(device)
|
54 |
+
# 1. calculate generator outputs
|
55 |
+
with torch.no_grad():
|
56 |
+
generated_speech, generated_f0 = self.generator(batch, device)
|
57 |
+
# 2. calculate discriminator outputs
|
58 |
+
y_d_rs, y_d_gs, fmap_rs, fmap_gs = self.discriminator(real_speech, generated_speech)
|
59 |
+
# 3. calculate discriminator losses, tpr losses [Optional]
|
60 |
+
loss_disc, _, _ = discriminator_loss(y_d_rs, y_d_gs)
|
61 |
+
if self.tpr_loss_weight != 0:
|
62 |
+
loss_tpr = tpr_loss(y_d_rs, y_d_gs, self.tpr_loss_tau)
|
63 |
+
else:
|
64 |
+
loss_tpr = torch.zeros(1).to(device)
|
65 |
+
loss = loss_disc + self.tpr_loss_weight * loss_tpr
|
66 |
+
return {'loss': loss, 'loss_disc': loss_disc, 'loss_tpr': loss_tpr}
|
inspiremusic/llm/llm.py
ADDED
@@ -0,0 +1,402 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
from typing import Dict, Optional, Callable, List, Generator
|
15 |
+
import torch
|
16 |
+
from torch import nn
|
17 |
+
from torch.nn.utils.rnn import pad_sequence, unpad_sequence
|
18 |
+
from inspiremusic.utils.common import IGNORE_ID
|
19 |
+
from inspiremusic.transformer.label_smoothing_loss import LabelSmoothingLoss
|
20 |
+
from inspiremusic.utils.common import th_accuracy
|
21 |
+
from torch import Tensor
|
22 |
+
from math import log
|
23 |
+
from einops import rearrange, reduce, repeat
|
24 |
+
import logging
|
25 |
+
|
26 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
27 |
+
|
28 |
+
class SinusoidalEmbedding(nn.Module):
|
29 |
+
def __init__(self, dim: int):
|
30 |
+
super().__init__()
|
31 |
+
self.dim = dim
|
32 |
+
|
33 |
+
def forward(self, x: Tensor) -> Tensor:
|
34 |
+
device, half_dim = x.device, self.dim // 2
|
35 |
+
emb = torch.tensor(log(10000) / (half_dim - 1), device=device)
|
36 |
+
emb = torch.exp(torch.arange(half_dim, device=device) * -emb)
|
37 |
+
emb = rearrange(x, "i -> i 1") * rearrange(emb, "j -> 1 j")
|
38 |
+
return torch.cat((emb.sin(), emb.cos()), dim=-1).to(torch.float32)
|
39 |
+
|
40 |
+
class LLM(torch.nn.Module):
|
41 |
+
def __init__(
|
42 |
+
self,
|
43 |
+
text_encoder_input_size: int,
|
44 |
+
llm_input_size: int,
|
45 |
+
llm_output_size: int,
|
46 |
+
audio_token_size: int,
|
47 |
+
llm: torch.nn.Module,
|
48 |
+
sampling: Callable,
|
49 |
+
text_encoder_conf: Dict = None,
|
50 |
+
length_normalized_loss: bool = True,
|
51 |
+
lsm_weight: float = 0.0,
|
52 |
+
frozen_input_embed: bool = False,
|
53 |
+
**kwargs,
|
54 |
+
):
|
55 |
+
super().__init__()
|
56 |
+
self.llm_input_size = llm_input_size
|
57 |
+
self.audio_token_size = audio_token_size
|
58 |
+
# 1. build text token inputs related modules
|
59 |
+
|
60 |
+
if llm is None:
|
61 |
+
self.text_embedding = torch.nn.Embedding(text_token_size, text_encoder_input_size)
|
62 |
+
else:
|
63 |
+
self.text_embedding = llm.model.model.embed_tokens
|
64 |
+
if frozen_input_embed:
|
65 |
+
print("Freezing input embedding layer")
|
66 |
+
for p in self.text_embedding.parameters():
|
67 |
+
p.requires_grad = False
|
68 |
+
self.chorus_embedding = torch.nn.Embedding(5, llm_input_size) # intro, chorus, verse1, verse2 , outro
|
69 |
+
|
70 |
+
self.text_encoder_conf = text_encoder_conf
|
71 |
+
self.text_encoder = self.build_encoder(text_encoder_conf)
|
72 |
+
self.infer_cfg_ratio = kwargs.get("infer_cfg_ratio", None)
|
73 |
+
logging.info(f"infer_cfg_ratio: {self.infer_cfg_ratio}")
|
74 |
+
self.train_cfg_ratio = kwargs.get("train_cfg_ratio", None)
|
75 |
+
logging.info(f"train_cfg_ratio: {self.train_cfg_ratio}")
|
76 |
+
# 2. build audio token language model related modules
|
77 |
+
self.sos_eos = 0
|
78 |
+
self.task_id = 1
|
79 |
+
|
80 |
+
self.llm_embedding = torch.nn.Embedding(2, llm_input_size)
|
81 |
+
self.llm = llm
|
82 |
+
self.llm_decoder = nn.Linear(llm_output_size, audio_token_size + 1)
|
83 |
+
self.criterion_ce = LabelSmoothingLoss(
|
84 |
+
size=audio_token_size + 1,
|
85 |
+
padding_idx=IGNORE_ID,
|
86 |
+
smoothing=lsm_weight,
|
87 |
+
normalize_length=length_normalized_loss,
|
88 |
+
)
|
89 |
+
|
90 |
+
# 3. [Optional] build audio token related modules
|
91 |
+
self.speech_embedding = torch.nn.Embedding(audio_token_size, llm_input_size)
|
92 |
+
self.spk_embed_affine_layer = torch.nn.Linear(192, llm_input_size)
|
93 |
+
self.num_codebooks = 4
|
94 |
+
# 4. sampling method
|
95 |
+
self.sampling = sampling
|
96 |
+
self.time_embedding = SinusoidalEmbedding(llm_input_size)
|
97 |
+
|
98 |
+
def cfg_dropout(self, text_token, text_token_len, p):
|
99 |
+
# Classifier-Free Guidance Dropout
|
100 |
+
B = text_token.size(0)
|
101 |
+
num_samples_to_mask = int(p * B)
|
102 |
+
if num_samples_to_mask == 0:
|
103 |
+
num_samples_to_mask = 1
|
104 |
+
indices_to_mask = torch.randperm(B, device=text_token.device)[:num_samples_to_mask]
|
105 |
+
text_token[indices_to_mask] = 0
|
106 |
+
text_token_len[indices_to_mask] = 0
|
107 |
+
|
108 |
+
return text_token, text_token_len
|
109 |
+
|
110 |
+
def build_encoder(self, encoder_conf=None):
|
111 |
+
if encoder_conf is None:
|
112 |
+
assert hasattr(self, "encoder_conf"), \
|
113 |
+
"function param encoder_conf is None and model doesn't has encoder_conf attribute either."
|
114 |
+
encoder_conf = self.encoder_conf
|
115 |
+
|
116 |
+
encoder_name = encoder_conf.pop("name", "transformer")
|
117 |
+
model = None
|
118 |
+
if encoder_name == "transformer":
|
119 |
+
from inspiremusic.transformer.encoder.conformer_encoder import ConformerEncoder
|
120 |
+
model = ConformerEncoder(
|
121 |
+
**encoder_conf,
|
122 |
+
input_size=self.input_size,
|
123 |
+
use_cnn_module=False,
|
124 |
+
macaron_style=False,
|
125 |
+
)
|
126 |
+
elif encoder_name == "conformer":
|
127 |
+
from inspiremusic.transformer.encoder.conformer_encoder import ConformerEncoder
|
128 |
+
model = ConformerEncoder(
|
129 |
+
**encoder_conf,
|
130 |
+
input_size=self.input_size,
|
131 |
+
)
|
132 |
+
elif encoder_name == "llama_encoder":
|
133 |
+
from inspiremusic.transformer.encoder.llama_encoder import LlamaEncoder
|
134 |
+
model = LlamaEncoder(
|
135 |
+
**encoder_conf,
|
136 |
+
input_size=self.input_size,
|
137 |
+
)
|
138 |
+
elif encoder_name == "qwen2":
|
139 |
+
from inspiremusic.transformer.encoder.qwen_encoder import QwenEncoder
|
140 |
+
model = QwenEncoder(
|
141 |
+
**encoder_conf,
|
142 |
+
input_size=self.input_size,
|
143 |
+
)
|
144 |
+
elif encoder_name == "qwen2.5":
|
145 |
+
from inspiremusic.transformer.encoder.qwen_encoder import QwenEncoder
|
146 |
+
model = QwenEncoder(
|
147 |
+
**encoder_conf,
|
148 |
+
input_size=self.input_size,
|
149 |
+
)
|
150 |
+
|
151 |
+
encoder_conf["name"] = encoder_name
|
152 |
+
|
153 |
+
return model
|
154 |
+
|
155 |
+
def encode(self,
|
156 |
+
text: torch.Tensor,
|
157 |
+
text_lengths: torch.Tensor):
|
158 |
+
if self.text_encoder is not None:
|
159 |
+
encoder_out, encoder_mask = self.text_encoder(text, text_lengths,
|
160 |
+
decoding_chunk_size=1,
|
161 |
+
num_decoding_left_chunks=-1)
|
162 |
+
encoder_out_lens = encoder_mask.squeeze(1).sum(1)
|
163 |
+
encoder_out = self.text_encoder_affine_layer(encoder_out)
|
164 |
+
else:
|
165 |
+
encoder_out, encoder_out_lens = text, text_lengths
|
166 |
+
return encoder_out, encoder_out_lens
|
167 |
+
|
168 |
+
def pad_unpad_sequence(self, sos_eos_emb, embeddings, text_token,
|
169 |
+
text_token_len, task_id_emb, audio_token,
|
170 |
+
audio_token_len, seg_len):
|
171 |
+
text_token = unpad_sequence(text_token, text_token_len.cpu(),
|
172 |
+
batch_first=True)
|
173 |
+
|
174 |
+
audio_token = unpad_sequence(audio_token, audio_token_len.cpu(),
|
175 |
+
batch_first=True)
|
176 |
+
|
177 |
+
for i in range(len(embeddings)):
|
178 |
+
embeddings[i] = unpad_sequence(embeddings[i], seg_len.cpu(), batch_first=True)
|
179 |
+
|
180 |
+
lm_input = [torch.concat([sos_eos_emb.squeeze(dim=0)] + [embedding[i] for embedding in embeddings] + [text_token[i], task_id_emb.squeeze(dim=0), audio_token[i]], dim=0) for i in range(len(text_token))]
|
181 |
+
lm_input_len = torch.tensor([i.size(0) for i in lm_input], dtype=torch.int32)
|
182 |
+
lm_input = pad_sequence(lm_input, batch_first=True, padding_value=IGNORE_ID)
|
183 |
+
return lm_input, lm_input_len
|
184 |
+
|
185 |
+
def forward(
|
186 |
+
self,
|
187 |
+
batch: dict,
|
188 |
+
device: torch.device,
|
189 |
+
) -> Dict[str, Optional[torch.Tensor]]:
|
190 |
+
"""
|
191 |
+
Args:
|
192 |
+
text: (B, L, D)
|
193 |
+
text_lengths: (B,)
|
194 |
+
audio: (B, T, N) or (B, T)
|
195 |
+
audio_lengths: (B,)
|
196 |
+
"""
|
197 |
+
mask = True
|
198 |
+
text_token = batch['text_token'].to(device)
|
199 |
+
text_token_len = batch['text_token_len'].to(device)
|
200 |
+
if "semantic_token" not in batch:
|
201 |
+
audio_token = batch['acoustic_token'].to(device)
|
202 |
+
audio_token_len = batch['acoustic_token_len'].to(device)
|
203 |
+
audio_token = audio_token.view(audio_token.size(0), -1, self.num_codebooks)
|
204 |
+
audio_token = audio_token[:, :, 0]
|
205 |
+
audio_token_len = (audio_token_len / self.num_codebooks).long()
|
206 |
+
|
207 |
+
else:
|
208 |
+
audio_token = batch['semantic_token'].to(device)
|
209 |
+
audio_token_len = batch['semantic_token_len'].to(device)
|
210 |
+
|
211 |
+
time_start = batch['time_start'].to(device)
|
212 |
+
time_end = batch['time_end'].to(device)
|
213 |
+
chorus = batch['chorus'].to(device)
|
214 |
+
# 1. encode text_token
|
215 |
+
|
216 |
+
if self.train_cfg_ratio > 0:
|
217 |
+
# Classifier-Free Guidance
|
218 |
+
text_token, _ = self.cfg_dropout(text_token, text_token_len, self.train_cfg_ratio)
|
219 |
+
|
220 |
+
# 2. Time Embedding & chorus embedding
|
221 |
+
text_token = self.text_embedding(text_token)
|
222 |
+
text_token, text_token_len = self.encode(text_token, text_token_len)
|
223 |
+
if mask:
|
224 |
+
time_mask = time_start != -1.0
|
225 |
+
seg_len = time_mask.sum(-1)
|
226 |
+
time_start = time_start.masked_fill(~time_mask, 0.0)
|
227 |
+
time_end = time_end.masked_fill(~time_mask, 0.0)
|
228 |
+
chorus = chorus.masked_fill(~time_mask, 0)
|
229 |
+
time_start_embed = self.time_embedding(time_start.view(-1)).to(text_token.dtype)
|
230 |
+
time_end_embed = self.time_embedding(time_end.view(-1)).to(text_token.dtype)
|
231 |
+
time_start_embed = time_start_embed.view(chorus.size(0), chorus.size(1), -1)
|
232 |
+
time_end_embed = time_end_embed.view(chorus.size(0), chorus.size(1), -1)
|
233 |
+
chorus_embed = self.chorus_embedding(chorus)
|
234 |
+
lm_target = [torch.tensor([IGNORE_ID] * (1 + 3 * seg_len[i] + text_token_len[i]) + audio_token[i,:audio_token_len[i]].tolist() + [self.audio_token_size]) for i in range(text_token.size(0))]
|
235 |
+
else:
|
236 |
+
time_start_embed = self.time_embedding(time_start).to(text_token.dtype)
|
237 |
+
time_end_embed = self.time_embedding(time_end).to(text_token.dtype)
|
238 |
+
chorus_embed = self.chorus_embedding(chorus)
|
239 |
+
|
240 |
+
lm_target = [torch.tensor(
|
241 |
+
[IGNORE_ID] * (4 + text_token_len[i]) + audio_token[i,:audio_token_len[i]].tolist() + [self.audio_token_size]) for i in range(text_token.size(0))]
|
242 |
+
|
243 |
+
lm_target = pad_sequence(lm_target, batch_first=True, padding_value=IGNORE_ID).to(device)
|
244 |
+
|
245 |
+
# 3. eos and task_id
|
246 |
+
sos_eos_emb = self.llm_embedding.weight[self.sos_eos].reshape(1, 1, -1)
|
247 |
+
task_id_emb = self.llm_embedding.weight[self.task_id].reshape(1, 1, -1)
|
248 |
+
|
249 |
+
# 4. encode audio_token
|
250 |
+
audio_token = self.speech_embedding(audio_token)
|
251 |
+
|
252 |
+
# 5. unpad and pad
|
253 |
+
lm_input, lm_input_len = self.pad_unpad_sequence(sos_eos_emb,
|
254 |
+
[time_start_embed,
|
255 |
+
time_end_embed,
|
256 |
+
chorus_embed],
|
257 |
+
text_token,
|
258 |
+
text_token_len,
|
259 |
+
task_id_emb,
|
260 |
+
audio_token,
|
261 |
+
audio_token_len,
|
262 |
+
seg_len)
|
263 |
+
# 6. run lm forward
|
264 |
+
lm_output, lm_output_mask = self.llm(lm_input, lm_input_len.to(device))
|
265 |
+
logits = self.llm_decoder(lm_output)
|
266 |
+
loss = self.criterion_ce(logits, lm_target)
|
267 |
+
|
268 |
+
acc = th_accuracy(logits.view(-1, self.audio_token_size + 1), lm_target, ignore_label=IGNORE_ID)
|
269 |
+
|
270 |
+
return {'loss': loss, 'acc': acc}
|
271 |
+
|
272 |
+
def sampling_ids(
|
273 |
+
self,
|
274 |
+
weighted_scores: torch.Tensor,
|
275 |
+
decoded_tokens: List,
|
276 |
+
ignore_eos: bool = True,
|
277 |
+
):
|
278 |
+
top_ids = self.sampling(weighted_scores, decoded_tokens)
|
279 |
+
return top_ids
|
280 |
+
|
281 |
+
@torch.inference_mode()
|
282 |
+
def inference(
|
283 |
+
self,
|
284 |
+
text: torch.Tensor,
|
285 |
+
text_len: torch.Tensor,
|
286 |
+
audio_token: torch.Tensor,
|
287 |
+
audio_token_len: torch.Tensor,
|
288 |
+
prompt_text: torch.Tensor,
|
289 |
+
prompt_text_len: torch.Tensor,
|
290 |
+
prompt_audio_token: torch.Tensor,
|
291 |
+
prompt_audio_token_len: torch.Tensor,
|
292 |
+
embeddings: List,
|
293 |
+
duration_to_gen: float = 30,
|
294 |
+
task: str = "continuation",
|
295 |
+
token_rate: int = 75,
|
296 |
+
limit_audio_prompt_len: int = 5,
|
297 |
+
) -> Generator[torch.Tensor, None, None]:
|
298 |
+
device = text.device
|
299 |
+
|
300 |
+
if text is not None:
|
301 |
+
text = torch.concat([prompt_text, text], dim=1)
|
302 |
+
text_len += prompt_text_len
|
303 |
+
infer_cfg = self.infer_cfg_ratio >= 0.0
|
304 |
+
if infer_cfg:
|
305 |
+
text_cfg = self.text_embedding(text.new_zeros(text.shape))
|
306 |
+
text = self.text_embedding(text)
|
307 |
+
|
308 |
+
# 1. encode text
|
309 |
+
text, text_len = self.encode(text, text_len)
|
310 |
+
|
311 |
+
# 2. encode embedding
|
312 |
+
if embeddings is not None:
|
313 |
+
time_start, time_end, chorus = embeddings
|
314 |
+
|
315 |
+
if len(chorus.shape) == 1:
|
316 |
+
time_start_embed = self.time_embedding(time_start).reshape(1, 1, -1) # .half()
|
317 |
+
time_end_embed = self.time_embedding(time_end).reshape(1, 1, -1) # .half()
|
318 |
+
chorus_embed = self.chorus_embedding(chorus).reshape(1, 1, -1) # .half()
|
319 |
+
else:
|
320 |
+
time_start_embed = self.time_embedding(
|
321 |
+
time_start.view(-1)).reshape(1, chorus.size(1), -1) # .half()
|
322 |
+
time_end_embed = self.time_embedding(time_end.view(-1)).reshape(1, chorus.size(1), -1) # .half()
|
323 |
+
chorus_embed = self.chorus_embedding(chorus) # .half()
|
324 |
+
|
325 |
+
# 3. concat llm_input
|
326 |
+
sos_eos_emb = self.llm_embedding.weight[self.sos_eos].reshape(1, 1, -1)
|
327 |
+
task_id_emb = self.llm_embedding.weight[self.task_id].reshape(1, 1, -1)
|
328 |
+
|
329 |
+
if audio_token_len:
|
330 |
+
audio_token = audio_token[:, :(limit_audio_prompt_len * token_rate)]
|
331 |
+
audio_token_emb = self.speech_embedding(audio_token)
|
332 |
+
else:
|
333 |
+
audio_token_emb = torch.zeros(1, 0, self.llm_input_size, dtype=text.dtype).to(device)
|
334 |
+
|
335 |
+
if prompt_audio_token_len:
|
336 |
+
prompt_audio_token_emb = self.speech_embedding(prompt_audio_token)
|
337 |
+
else:
|
338 |
+
prompt_audio_token_emb = torch.zeros(1, 0, self.llm_input_size, dtype=text.dtype).to(device)
|
339 |
+
# Check if removing prompt audio token will fail decoding.
|
340 |
+
|
341 |
+
if task == "continuation":
|
342 |
+
lm_input = torch.concat(
|
343 |
+
[sos_eos_emb, time_start_embed, time_end_embed,
|
344 |
+
chorus_embed, text, task_id_emb, audio_token_emb], dim=1)
|
345 |
+
|
346 |
+
if infer_cfg:
|
347 |
+
audio_cfg = self.speech_embedding(
|
348 |
+
audio_token.new_zeros(audio_token.shape))
|
349 |
+
lm_cf_input = torch.concat(
|
350 |
+
[sos_eos_emb, torch.rand_like(time_start_embed),
|
351 |
+
torch.rand_like(time_end_embed),
|
352 |
+
torch.rand_like(chorus_embed), text_cfg, task_id_emb,
|
353 |
+
audio_cfg], dim=1)
|
354 |
+
lm_input = torch.cat([lm_input, lm_cf_input], 0)
|
355 |
+
else:
|
356 |
+
lm_input = torch.concat(
|
357 |
+
[sos_eos_emb, time_start_embed, time_end_embed,
|
358 |
+
chorus_embed, text, task_id_emb], dim=1)
|
359 |
+
if infer_cfg:
|
360 |
+
lm_cf_input = torch.concat(
|
361 |
+
[sos_eos_emb, torch.rand_like(time_start_embed),
|
362 |
+
torch.rand_like(time_end_embed),
|
363 |
+
torch.rand_like(chorus_embed), text_cfg, task_id_emb],
|
364 |
+
dim=1)
|
365 |
+
lm_input = torch.cat([lm_input, lm_cf_input], 0)
|
366 |
+
|
367 |
+
# 4. cal min/max_length
|
368 |
+
min_len = duration_to_gen * token_rate
|
369 |
+
max_len = duration_to_gen * token_rate
|
370 |
+
logging.info(
|
371 |
+
f"LLM generation sequence length: {max_len}, generate audio length {duration_to_gen}s.")
|
372 |
+
|
373 |
+
# 5. step by step decode
|
374 |
+
out_tokens = []
|
375 |
+
offset = 0
|
376 |
+
state = None
|
377 |
+
|
378 |
+
for i in range(int(max_len)):
|
379 |
+
y_pred, _, state = self.llm.forward_one_step(lm_input, torch.ones(lm_input.shape[0], lm_input.shape[1], device=lm_input.device).to(torch.bool), cache=state)
|
380 |
+
logits = self.llm_decoder(y_pred[:, -1])
|
381 |
+
if infer_cfg:
|
382 |
+
# perform context free guidance
|
383 |
+
logits_cf = logits[1]
|
384 |
+
logits = logits[0]
|
385 |
+
infer_cfg_ratio = self.infer_cfg_ratio
|
386 |
+
logits = infer_cfg_ratio * logits + (1 - infer_cfg_ratio) * logits_cf
|
387 |
+
|
388 |
+
logp = logits.log_softmax(dim=-1)
|
389 |
+
logp = logp.squeeze(dim=0)
|
390 |
+
top_ids = self.sampling_ids(logp, out_tokens, ignore_eos=i < min_len).item()
|
391 |
+
|
392 |
+
if top_ids == self.audio_token_size:
|
393 |
+
break
|
394 |
+
|
395 |
+
# # in stream mode, yield token one by one
|
396 |
+
|
397 |
+
yield torch.tensor([[top_ids]], dtype=torch.int64, device=device)
|
398 |
+
out_tokens.append(top_ids)
|
399 |
+
offset += lm_input.size(1)
|
400 |
+
lm_input = self.speech_embedding.weight[top_ids].reshape(1, 1, -1)
|
401 |
+
if infer_cfg:
|
402 |
+
lm_input = lm_input.repeat(2, 1, 1)
|
inspiremusic/metrics/clap_score.py
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
import os
|
15 |
+
import requests
|
16 |
+
from tqdm import tqdm
|
17 |
+
import torch
|
18 |
+
import numpy as np
|
19 |
+
import laion_clap
|
20 |
+
from clap_module.factory import load_state_dict
|
21 |
+
import librosa
|
22 |
+
import pyloudnorm as pyln
|
23 |
+
|
24 |
+
# following documentation from https://github.com/LAION-AI/CLAP
|
25 |
+
def int16_to_float32(x):
|
26 |
+
return (x / 32767.0).astype(np.float32)
|
27 |
+
|
28 |
+
def float32_to_int16(x):
|
29 |
+
x = np.clip(x, a_min=-1., a_max=1.)
|
30 |
+
return (x * 32767.).astype(np.int16)
|
31 |
+
|
32 |
+
|
33 |
+
def clap_score(id2text, audio_path, audio_files_extension='.wav', clap_model='music_audioset_epoch_15_esc_90.14.pt'):
|
34 |
+
"""
|
35 |
+
Cosine similarity is computed between the LAION-CLAP text embedding of the given prompt and
|
36 |
+
the LAION-CLAP audio embedding of the generated audio. LION-CLAP: https://github.com/LAION-AI/CLAP
|
37 |
+
|
38 |
+
This evaluation script assumes that audio_path files are identified with the ids in id2text.
|
39 |
+
|
40 |
+
clap_score() evaluates all ids in id2text.
|
41 |
+
|
42 |
+
GPU-based computation.
|
43 |
+
|
44 |
+
Select one of the following models from https://github.com/LAION-AI/CLAP:
|
45 |
+
- music_speech_audioset_epoch_15_esc_89.98.pt (used by musicgen)
|
46 |
+
- music_audioset_epoch_15_esc_90.14.pt
|
47 |
+
- music_speech_epoch_15_esc_89.25.pt
|
48 |
+
- 630k-audioset-fusion-best.pt (our default, with "fusion" to handle longer inputs)
|
49 |
+
|
50 |
+
Params:
|
51 |
+
-- id2text: dictionary with the mapping between id (generated audio filenames in audio_path)
|
52 |
+
and text (prompt used to generate audio). clap_score() evaluates all ids in id2text.
|
53 |
+
-- audio_path: path where the generated audio files to evaluate are available.
|
54 |
+
-- audio_files_extension: files extension (default .wav) in eval_path.
|
55 |
+
-- clap_model: choose one of the above clap_models (default: '630k-audioset-fusion-best.pt').
|
56 |
+
Returns:
|
57 |
+
-- CLAP-LION score
|
58 |
+
"""
|
59 |
+
# load model
|
60 |
+
if clap_model == 'music_speech_audioset_epoch_15_esc_89.98.pt':
|
61 |
+
url = 'https://huggingface.co/lukewys/laion_clap/resolve/main/music_speech_audioset_epoch_15_esc_89.98.pt'
|
62 |
+
clap_path = 'CLAP/music_speech_audioset_epoch_15_esc_89.98.pt'
|
63 |
+
model = laion_clap.CLAP_Module(enable_fusion=False, amodel='HTSAT-base', device='cuda')
|
64 |
+
elif clap_model == 'music_audioset_epoch_15_esc_90.14.pt':
|
65 |
+
url = 'https://huggingface.co/lukewys/laion_clap/resolve/main/music_audioset_epoch_15_esc_90.14.pt'
|
66 |
+
clap_path = 'CLAP/music_audioset_epoch_15_esc_90.14.pt'
|
67 |
+
model = laion_clap.CLAP_Module(enable_fusion=False, amodel='HTSAT-base', device='cuda')
|
68 |
+
elif clap_model == 'music_speech_epoch_15_esc_89.25.pt':
|
69 |
+
url = 'https://huggingface.co/lukewys/laion_clap/resolve/main/music_speech_epoch_15_esc_89.25.pt'
|
70 |
+
clap_path = 'CLAP/music_speech_epoch_15_esc_89.25.pt'
|
71 |
+
model = laion_clap.CLAP_Module(enable_fusion=False, amodel='HTSAT-base', device='cuda')
|
72 |
+
elif clap_model == '630k-audioset-fusion-best.pt':
|
73 |
+
url = 'https://huggingface.co/lukewys/laion_clap/resolve/main/630k-audioset-fusion-best.pt'
|
74 |
+
clap_path = 'CLAP/630k-audioset-fusion-best.pt'
|
75 |
+
model = laion_clap.CLAP_Module(enable_fusion=True, device='cuda')
|
76 |
+
else:
|
77 |
+
raise ValueError('clap_model not implemented')
|
78 |
+
|
79 |
+
# download clap_model if not already downloaded
|
80 |
+
if not os.path.exists(clap_path):
|
81 |
+
print('Downloading ', clap_model, '...')
|
82 |
+
os.makedirs(os.path.dirname(clap_path), exist_ok=True)
|
83 |
+
|
84 |
+
response = requests.get(url, stream=True)
|
85 |
+
total_size = int(response.headers.get('content-length', 0))
|
86 |
+
|
87 |
+
with open(clap_path, 'wb') as file:
|
88 |
+
with tqdm(total=total_size, unit='B', unit_scale=True) as progress_bar:
|
89 |
+
for data in response.iter_content(chunk_size=8192):
|
90 |
+
file.write(data)
|
91 |
+
progress_bar.update(len(data))
|
92 |
+
|
93 |
+
# fixing CLAP-LION issue, see: https://github.com/LAION-AI/CLAP/issues/118
|
94 |
+
pkg = load_state_dict(clap_path)
|
95 |
+
pkg.pop('text_branch.embeddings.position_ids', None)
|
96 |
+
model.model.load_state_dict(pkg)
|
97 |
+
model.eval()
|
98 |
+
|
99 |
+
if not os.path.isdir(audio_path):
|
100 |
+
raise ValueError(f'audio_path: {audio_path} does not exist')
|
101 |
+
|
102 |
+
if id2text:
|
103 |
+
print('[EXTRACTING TEXT EMBEDDINGS] ')
|
104 |
+
batch_size = 64
|
105 |
+
text_emb = {}
|
106 |
+
for i in tqdm(range(0, len(id2text), batch_size)):
|
107 |
+
batch_ids = list(id2text.keys())[i:i+batch_size]
|
108 |
+
batch_texts = [id2text[id] for id in batch_ids]
|
109 |
+
with torch.no_grad():
|
110 |
+
embeddings = model.get_text_embedding(batch_texts, use_tensor=True)
|
111 |
+
for id, emb in zip(batch_ids, embeddings):
|
112 |
+
text_emb[id] = emb
|
113 |
+
|
114 |
+
else:
|
115 |
+
raise ValueError('Must specify id2text')
|
116 |
+
|
117 |
+
print('[EVALUATING GENERATIONS] ', audio_path)
|
118 |
+
score = 0
|
119 |
+
count = 0
|
120 |
+
for id in tqdm(id2text.keys()):
|
121 |
+
file_path = os.path.join(audio_path, str(id)+audio_files_extension)
|
122 |
+
if os.path.isfile(file_path):
|
123 |
+
with torch.no_grad():
|
124 |
+
audio, _ = librosa.load(file_path, sr=48000, mono=True) # sample rate should be 48000
|
125 |
+
audio = pyln.normalize.peak(audio, -1.0)
|
126 |
+
audio = audio.reshape(1, -1) # unsqueeze (1,T)
|
127 |
+
audio = torch.from_numpy(int16_to_float32(float32_to_int16(audio))).float()
|
128 |
+
audio_embeddings = model.get_audio_embedding_from_data(x = audio, use_tensor=True)
|
129 |
+
cosine_sim = torch.nn.functional.cosine_similarity(audio_embeddings, text_emb[id].unsqueeze(0), dim=1, eps=1e-8)[0]
|
130 |
+
print(f"{id} | CLAP score = {cosine_sim}")
|
131 |
+
score += cosine_sim
|
132 |
+
count += 1
|
133 |
+
|
134 |
+
return score / count if count > 0 else 0
|
135 |
+
|
inspiremusic/metrics/openl3_fd.py
ADDED
@@ -0,0 +1,338 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
import openl3
|
15 |
+
import librosa
|
16 |
+
import numpy as np
|
17 |
+
from scipy import linalg
|
18 |
+
import glob
|
19 |
+
from tqdm import tqdm
|
20 |
+
import os
|
21 |
+
import soxr
|
22 |
+
import pyloudnorm as pyln
|
23 |
+
|
24 |
+
|
25 |
+
def calculate_embd_statistics(embd_lst):
|
26 |
+
if isinstance(embd_lst, list):
|
27 |
+
embd_lst = np.array(embd_lst)
|
28 |
+
mu = np.mean(embd_lst, axis=0)
|
29 |
+
sigma = np.cov(embd_lst, rowvar=False)
|
30 |
+
return mu, sigma
|
31 |
+
|
32 |
+
|
33 |
+
def calculate_frechet_distance(mu1, sigma1, mu2, sigma2, eps=1e-6):
|
34 |
+
"""
|
35 |
+
Adapted from: https://github.com/mseitzer/pytorch-fid/blob/master/src/pytorch_fid/fid_score.py
|
36 |
+
Adapted from: https://github.com/gudgud96/frechet-audio-distance/blob/main/frechet_audio_distance/fad.py
|
37 |
+
|
38 |
+
Numpy implementation of the Frechet Distance.
|
39 |
+
|
40 |
+
The Frechet distance between two multivariate Gaussians X_1 ~ N(mu_1, C_1)
|
41 |
+
and X_2 ~ N(mu_2, C_2) is
|
42 |
+
d^2 = ||mu_1 - mu_2||^2 + Tr(C_1 + C_2 - 2*sqrt(C_1*C_2)).
|
43 |
+
|
44 |
+
Params:
|
45 |
+
-- mu1: Embedding's mean statistics for generated samples.
|
46 |
+
-- mu2: Embedding's mean statistics for reference samples.
|
47 |
+
-- sigma1: Covariance matrix over embeddings for generated samples.
|
48 |
+
-- sigma2: Covariance matrix over embeddings for reference samples.
|
49 |
+
Returns:
|
50 |
+
-- Fréchet Distance.
|
51 |
+
"""
|
52 |
+
|
53 |
+
mu1 = np.atleast_1d(mu1)
|
54 |
+
mu2 = np.atleast_1d(mu2)
|
55 |
+
|
56 |
+
sigma1 = np.atleast_2d(sigma1)
|
57 |
+
sigma2 = np.atleast_2d(sigma2)
|
58 |
+
|
59 |
+
assert mu1.shape == mu2.shape, \
|
60 |
+
'Training and test mean vectors have different lengths'
|
61 |
+
assert sigma1.shape == sigma2.shape, \
|
62 |
+
'Training and test covariances have different dimensions'
|
63 |
+
|
64 |
+
diff = mu1 - mu2
|
65 |
+
|
66 |
+
# product might be almost singular
|
67 |
+
covmean, _ = linalg.sqrtm(sigma1.dot(sigma2), disp=False)
|
68 |
+
if not np.isfinite(covmean).all():
|
69 |
+
msg = ('fid calculation produces singular product; '
|
70 |
+
'adding %s to diagonal of cov estimates') % eps
|
71 |
+
print(msg)
|
72 |
+
offset = np.eye(sigma1.shape[0]) * eps
|
73 |
+
covmean = linalg.sqrtm((sigma1 + offset).dot(sigma2 + offset))
|
74 |
+
|
75 |
+
# numerical error might give slight imaginary component
|
76 |
+
if np.iscomplexobj(covmean):
|
77 |
+
if not np.allclose(np.diagonal(covmean).imag, 0, atol=1e-3):
|
78 |
+
m = np.max(np.abs(covmean.imag))
|
79 |
+
raise ValueError('Imaginary component {}'.format(m))
|
80 |
+
covmean = covmean.real
|
81 |
+
|
82 |
+
tr_covmean = np.trace(covmean)
|
83 |
+
|
84 |
+
return (diff.dot(diff) + np.trace(sigma1)
|
85 |
+
+ np.trace(sigma2) - 2 * tr_covmean)
|
86 |
+
|
87 |
+
|
88 |
+
def extract_embeddings(directory_path, channels, samplingrate, content_type, openl3_hop_size, batch_size=16):
|
89 |
+
"""
|
90 |
+
Given a list of files, compute their embeddings in batches.
|
91 |
+
|
92 |
+
If channels == 1: stereo audio is downmixed to mono. Mono embeddings are of dim=512.
|
93 |
+
|
94 |
+
If channels == 2: mono audio is "faked" to stereo by copying the mono channel.
|
95 |
+
Stereo embeddings are of dim=1024, since we concatenate L (dim=512) and R (dim=512) embeddings.
|
96 |
+
|
97 |
+
Params:
|
98 |
+
-- directory_path: path where the generated audio files are available.
|
99 |
+
-- channels: 1 (mono), or 2 (stereo) to get mono or stereo embeddings.
|
100 |
+
-- samplingrate: max bandwidth at which we evaluate the given signals. Up to 48kHz.
|
101 |
+
-- content_type: 'music' or 'env' to select a content type specific openl3 model.
|
102 |
+
-- openl3_hop_size: analysis resolution of openl3 in seconds. Openl3's input window is 1 sec.
|
103 |
+
-- batch_size: number of audio files to process in each batch.
|
104 |
+
Returns:
|
105 |
+
-- list of embeddings: [np.array[], ...], as expected by calculate_frechet_distance()
|
106 |
+
"""
|
107 |
+
_, extension = os.path.splitext(directory_path)
|
108 |
+
if extension.lower() == ".scp":
|
109 |
+
wav_files = []
|
110 |
+
with open(directory_path, "r") as f:
|
111 |
+
for line in f:
|
112 |
+
sec = line.strip().split(" ")
|
113 |
+
wav_files.append(sec[1])
|
114 |
+
else:
|
115 |
+
wav_files = glob.glob(directory_path)
|
116 |
+
if len(wav_files) == 0:
|
117 |
+
raise ValueError('No files with this extension in this path!')
|
118 |
+
model = openl3.models.load_audio_embedding_model(input_repr="mel256", content_type=content_type, embedding_size=512)
|
119 |
+
|
120 |
+
first = True
|
121 |
+
for i in tqdm(range(0, len(wav_files), batch_size)):
|
122 |
+
batch_files = wav_files[i:i+batch_size]
|
123 |
+
batch_audio_l = []
|
124 |
+
batch_audio_r = []
|
125 |
+
batch_sr = []
|
126 |
+
|
127 |
+
for file in batch_files:
|
128 |
+
audio, sr = librosa.load(file, sr=None, mono=False)
|
129 |
+
audio = audio.T
|
130 |
+
audio = pyln.normalize.peak(audio, -1.0)
|
131 |
+
if audio.shape[0] < sr:
|
132 |
+
print('Audio shorter than 1 sec, openl3 will zero-pad it:', file, audio.shape, sr)
|
133 |
+
|
134 |
+
# resample to the desired evaluation bandwidth
|
135 |
+
audio = soxr.resample(audio, sr, samplingrate) # mono/stereo <- mono/stereo, input sr, output sr
|
136 |
+
|
137 |
+
# mono embeddings are stored in batch_audio_l (R channel not used)
|
138 |
+
if channels == 1:
|
139 |
+
batch_audio_l.append(audio)
|
140 |
+
|
141 |
+
elif channels == 2:
|
142 |
+
if audio.ndim == 1:
|
143 |
+
# if mono, "fake" stereo by copying mono channel to L and R
|
144 |
+
batch_audio_l.append(audio)
|
145 |
+
batch_audio_r.append(audio)
|
146 |
+
elif audio.ndim == 2:
|
147 |
+
# if it's stereo separate channels for openl3
|
148 |
+
batch_audio_l.append(audio[:,0])
|
149 |
+
batch_audio_r.append(audio[:,1])
|
150 |
+
|
151 |
+
batch_sr.append(samplingrate)
|
152 |
+
|
153 |
+
# extracting mono embeddings (dim=512) or the L channel for stereo embeddings
|
154 |
+
emb, _ = openl3.get_audio_embedding(batch_audio_l, batch_sr, model=model, verbose=False, hop_size=openl3_hop_size, batch_size=batch_size)
|
155 |
+
|
156 |
+
# format mono embedding
|
157 |
+
if channels == 1:
|
158 |
+
emb = np.concatenate(emb,axis=0)
|
159 |
+
|
160 |
+
# extracting stereo embeddings (dim=1024), since we concatenate L (dim=512) and R (dim=512) embeddings
|
161 |
+
elif channels == 2:
|
162 |
+
# extract the missing R channel
|
163 |
+
emb_r, _ = openl3.get_audio_embedding(batch_audio_r, batch_sr, model=model, verbose=False, hop_size=openl3_hop_size, batch_size=batch_size)
|
164 |
+
emb = [np.concatenate([l, r], axis=1) for l, r in zip(emb, emb_r)]
|
165 |
+
emb = np.concatenate(emb, axis=0)
|
166 |
+
|
167 |
+
# concatenate embeddings
|
168 |
+
if first:
|
169 |
+
embeddings = emb
|
170 |
+
first = False
|
171 |
+
else:
|
172 |
+
embeddings = np.concatenate([embeddings, emb], axis=0)
|
173 |
+
|
174 |
+
# return as a list of embeddings: [np.array[], ...]
|
175 |
+
return [e for e in embeddings]
|
176 |
+
|
177 |
+
|
178 |
+
def extract_embeddings_nobatching(directory_path, channels, samplingrate, content_type, openl3_hop_size):
|
179 |
+
"""
|
180 |
+
Given a list of files, compute their embeddings one by one.
|
181 |
+
|
182 |
+
If channels == 1: stereo audio is downmixed to mono. Mono embeddings are of dim=512.
|
183 |
+
|
184 |
+
If channels == 2: mono audio is "faked" to stereo by copying the mono channel.
|
185 |
+
Stereo embeddings are of dim=1024, since we concatenate L (dim=512) and R (dim=512) embeddings.
|
186 |
+
|
187 |
+
Params:
|
188 |
+
-- directory_path: path where the generated audio files are available.
|
189 |
+
-- channels: 1 (mono), or 2 (stereo) to get mono or stereo embeddings.
|
190 |
+
-- samplingrate: max bandwidth at which we evaluate the given signals. Up to 48kHz.
|
191 |
+
-- content_type: 'music' or 'env' to select a content type specific openl3 model.
|
192 |
+
-- openl3_hop_size: analysis resolution of openl3 in seconds. Openl3's input window is 1 sec.
|
193 |
+
Returns:
|
194 |
+
-- list of embeddings: [np.array[], ...], as expected by calculate_frechet_distance()
|
195 |
+
"""
|
196 |
+
_, extension = os.path.splitext(directory_path)
|
197 |
+
if extension.lower() == ".scp":
|
198 |
+
wav_files = []
|
199 |
+
with open(directory_path, "r") as f:
|
200 |
+
for line in f:
|
201 |
+
sec = line.strip().split(" ")
|
202 |
+
wav_files.append(sec[1])
|
203 |
+
else:
|
204 |
+
wav_files = glob.glob(directory_path)
|
205 |
+
if len(wav_files) == 0:
|
206 |
+
raise ValueError('No files with this extension in this path!')
|
207 |
+
model = openl3.models.load_audio_embedding_model(input_repr="mel256", content_type=content_type, embedding_size=512)
|
208 |
+
|
209 |
+
first = True
|
210 |
+
for file in tqdm(wav_files):
|
211 |
+
audio, sr = librosa.load(file, sr=None)
|
212 |
+
audio = pyln.normalize.peak(audio, -1.0)
|
213 |
+
if audio.shape[0] < sr:
|
214 |
+
print('Audio shorter than 1 sec, openl3 will zero-pad it:', file, audio.shape, sr)
|
215 |
+
|
216 |
+
# resample to the desired evaluation bandwidth
|
217 |
+
audio = soxr.resample(audio, sr, samplingrate) # mono/stereo <- mono/stereo, input sr, output sr
|
218 |
+
|
219 |
+
# extracting stereo embeddings (dim=1024), since we concatenate L (dim=512) and R (dim=512) embeddings
|
220 |
+
if channels == 2:
|
221 |
+
if audio.ndim == 1:
|
222 |
+
audio_l3, sr_l3 = audio, samplingrate
|
223 |
+
elif audio.ndim == 2:
|
224 |
+
# if it's stereo separate channels for openl3
|
225 |
+
audio_l3 = [audio[:,0], audio[:,1]]
|
226 |
+
sr_l3 = [samplingrate, samplingrate]
|
227 |
+
emb, _ = openl3.get_audio_embedding(audio_l3, sr_l3, model=model, verbose=False, hop_size=openl3_hop_size)
|
228 |
+
if audio.ndim == 1:
|
229 |
+
# if mono audio, "fake" stereo by concatenating mono embedding as L and R embeddings
|
230 |
+
emb = np.concatenate([emb, emb],axis=1)
|
231 |
+
elif audio.ndim == 2:
|
232 |
+
emb = np.concatenate(emb,axis=1)
|
233 |
+
|
234 |
+
# or extracting mono embeddings (dim=512)
|
235 |
+
elif channels == 1:
|
236 |
+
emb, _ = openl3.get_audio_embedding(audio, samplingrate, model=model, verbose=False, hop_size=openl3_hop_size)
|
237 |
+
|
238 |
+
# concatenate embeddings
|
239 |
+
if first:
|
240 |
+
embeddings = emb
|
241 |
+
first = False
|
242 |
+
else:
|
243 |
+
embeddings = np.concatenate([embeddings, emb], axis=0)
|
244 |
+
|
245 |
+
# return as a list of embeddings: [np.array[], ...]
|
246 |
+
return [e for e in embeddings]
|
247 |
+
|
248 |
+
|
249 |
+
def openl3_fd(channels, samplingrate, content_type, openl3_hop_size, eval_path,
|
250 |
+
eval_files_extension='.wav', ref_path=None, ref_files_extension='.wav', load_ref_embeddings=None, batching=False):
|
251 |
+
"""
|
252 |
+
Compute the Fréchet Distance between files in eval_path and ref_path.
|
253 |
+
|
254 |
+
Fréchet distance computed on top of openl3 embeddings.
|
255 |
+
|
256 |
+
GPU-based computation.
|
257 |
+
|
258 |
+
Extracting the embeddings is timeconsuming. After being computed once, we store them.
|
259 |
+
We store pre-computed reference embedding statistics in load/openl3_fd/
|
260 |
+
To load those and save computation, just set the path in load_ref_embeddings.
|
261 |
+
If load_ref_embeddings is set, ref_path is not required.
|
262 |
+
|
263 |
+
Params:
|
264 |
+
-- channels: 1 (mono), or 2 (stereo) to get the Fréchet Distance over mono or stereo embeddings.
|
265 |
+
-- samplingrate: max bandwith at wich we evaluate the given signals. Up to 48kHz.
|
266 |
+
-- content_type: 'music' or 'env' to select a content type for openl3.
|
267 |
+
-- openl3_hop_size: analysis resolution of openl3 in seconds. Openl3's input window is 1 sec.
|
268 |
+
-- eval_path: path where the generated audio files to evaluate are available.
|
269 |
+
-- eval_files_extenstion: files extension (default .wav) in eval_path.
|
270 |
+
-- ref_path: path where the reference audio files are available. (instead of load_ref_embeddings)
|
271 |
+
-- ref_files_extension: files extension (default .wav) in ref_path.
|
272 |
+
-- load_ref_embeddings: path to the reference embedding statistics. (inestead of ref_path)
|
273 |
+
-- batching: set batch size (with an int) or set to False (default False).
|
274 |
+
Returns:
|
275 |
+
-- Fréchet distance.
|
276 |
+
"""
|
277 |
+
|
278 |
+
if not os.path.isdir(eval_path):
|
279 |
+
raise ValueError('eval_path does not exist')
|
280 |
+
|
281 |
+
if load_ref_embeddings:
|
282 |
+
if not os.path.exists(load_ref_embeddings):
|
283 |
+
raise ValueError('load_ref_embeddings does not exist')
|
284 |
+
print('[LOADING REFERENCE EMBEDDINGS] ', load_ref_embeddings)
|
285 |
+
loaded = np.load(load_ref_embeddings)
|
286 |
+
mu_ref = loaded['mu_ref']
|
287 |
+
sigma_ref = loaded['sigma_ref']
|
288 |
+
|
289 |
+
else:
|
290 |
+
if ref_path:
|
291 |
+
if not os.path.isdir(ref_path):
|
292 |
+
if not os.path.isfile(ref_path):
|
293 |
+
raise ValueError("ref_path does not exist")
|
294 |
+
if os.path.isfile(ref_path):
|
295 |
+
path = ref_path
|
296 |
+
else:
|
297 |
+
path = os.path.join(ref_path, '*'+ref_files_extension)
|
298 |
+
print('[EXTRACTING REFERENCE EMBEDDINGS] ', path)
|
299 |
+
if batching:
|
300 |
+
ref_embeddings = extract_embeddings(path, channels, samplingrate, content_type, openl3_hop_size, batch_size=batching)
|
301 |
+
else:
|
302 |
+
ref_embeddings = extract_embeddings_nobatching(path, channels, samplingrate, content_type, openl3_hop_size)
|
303 |
+
mu_ref, sigma_ref = calculate_embd_statistics(ref_embeddings)
|
304 |
+
|
305 |
+
# store statistics to load later on
|
306 |
+
if not os.path.exists('load/openl3_fd'):
|
307 |
+
os.makedirs('load/openl3_fd/')
|
308 |
+
save_ref_embeddings_path = (
|
309 |
+
'load/openl3_fd/' +
|
310 |
+
path.replace('/', '_') +
|
311 |
+
'__channels' + str(channels) +
|
312 |
+
'__' + str(samplingrate) +
|
313 |
+
'__openl3' + str(content_type) +
|
314 |
+
'__openl3hopsize' + str(openl3_hop_size) +
|
315 |
+
'__batch' + str(batching) +
|
316 |
+
'.npz'
|
317 |
+
)
|
318 |
+
np.savez(save_ref_embeddings_path, mu_ref=mu_ref, sigma_ref=sigma_ref)
|
319 |
+
print('[REFERENCE EMBEDDINGS][SAVED] ', save_ref_embeddings_path)
|
320 |
+
|
321 |
+
else:
|
322 |
+
raise ValueError('Must specify ref_path or load_ref_embeddings')
|
323 |
+
|
324 |
+
path = os.path.join(eval_path, '*'+eval_files_extension)
|
325 |
+
print('[EXTRACTING EVALUATION EMBEDDINGS] ', path)
|
326 |
+
if batching:
|
327 |
+
eval_embeddings = extract_embeddings(path, channels, samplingrate, content_type, openl3_hop_size, batch_size=batching)
|
328 |
+
else:
|
329 |
+
eval_embeddings = extract_embeddings_nobatching(path, channels, samplingrate, content_type, openl3_hop_size)
|
330 |
+
mu_eval, sigma_eval = calculate_embd_statistics(eval_embeddings)
|
331 |
+
|
332 |
+
fd = calculate_frechet_distance(mu_eval, sigma_eval, mu_ref, sigma_ref)
|
333 |
+
if load_ref_embeddings:
|
334 |
+
print('[FRéCHET DISTANCE] ', eval_path, load_ref_embeddings, fd)
|
335 |
+
else:
|
336 |
+
print('[FRéCHET DISTANCE] ', eval_path, ref_path, fd)
|
337 |
+
|
338 |
+
return fd
|
inspiremusic/metrics/passt_kld.py
ADDED
@@ -0,0 +1,232 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
import warnings
|
15 |
+
warnings.filterwarnings("ignore", category=UserWarning)
|
16 |
+
warnings.filterwarnings("ignore", category=FutureWarning)
|
17 |
+
|
18 |
+
import os
|
19 |
+
import contextlib
|
20 |
+
from functools import partial
|
21 |
+
from tqdm import tqdm
|
22 |
+
import pickle
|
23 |
+
import numpy as np
|
24 |
+
import librosa
|
25 |
+
from hear21passt.base import get_basic_model
|
26 |
+
import pyloudnorm as pyln
|
27 |
+
|
28 |
+
import torch
|
29 |
+
import torch.nn.functional as F
|
30 |
+
|
31 |
+
|
32 |
+
SAMPLING_RATE = 32000
|
33 |
+
|
34 |
+
|
35 |
+
class _patch_passt_stft:
|
36 |
+
"""
|
37 |
+
From version 1.8.0, return_complex must always be given explicitly
|
38 |
+
for real inputs and return_complex=False has been deprecated.
|
39 |
+
|
40 |
+
Decorator to patch torch.stft in PaSST that uses an old stft version.
|
41 |
+
|
42 |
+
Adapted from: https://github.com/facebookresearch/audiocraft/blob/a2b96756956846e194c9255d0cdadc2b47c93f1b/audiocraft/metrics/kld.py
|
43 |
+
"""
|
44 |
+
def __init__(self):
|
45 |
+
self.old_stft = torch.stft
|
46 |
+
|
47 |
+
def __enter__(self):
|
48 |
+
# return_complex is a mandatory parameter in latest torch versions.
|
49 |
+
# torch is throwing RuntimeErrors when not set.
|
50 |
+
# see: https://pytorch.org/docs/1.7.1/generated/torch.stft.html?highlight=stft#torch.stft
|
51 |
+
# see: https://github.com/kkoutini/passt_hear21/commit/dce83183674e559162b49924d666c0a916dc967a
|
52 |
+
torch.stft = partial(torch.stft, return_complex=False)
|
53 |
+
|
54 |
+
def __exit__(self, *exc):
|
55 |
+
torch.stft = self.old_stft
|
56 |
+
|
57 |
+
|
58 |
+
def return_probabilities(model, audio_path, window_size=10, overlap=5, collect='mean'):
|
59 |
+
"""
|
60 |
+
Given an audio and the PaSST model, return the probabilities of each AudioSet class.
|
61 |
+
|
62 |
+
Audio is converted to mono at 32kHz.
|
63 |
+
|
64 |
+
PaSST model is trained with 10 sec inputs. We refer to this parameter as the window_size.
|
65 |
+
We set it to 10 sec for consistency with PaSST training.
|
66 |
+
|
67 |
+
For longer audios, we split audio into overlapping analysis windows of window_size and overlap of 10 and 5 seconds.
|
68 |
+
PaSST supports 10, 20 or 30 sec inputs. Not longer inputs: https://github.com/kkoutini/PaSST/issues/19
|
69 |
+
|
70 |
+
Note that AudioSet taggers normally use sigmoid output layers. Yet, to compute the
|
71 |
+
KL we work with normalized probabilities by running a softmax over logits as in MusicGen:
|
72 |
+
https://github.com/facebookresearch/audiocraft/blob/a2b96756956846e194c9255d0cdadc2b47c93f1b/audiocraft/metrics/kld.py
|
73 |
+
|
74 |
+
This implementation assumes run will be on GPU.
|
75 |
+
|
76 |
+
Params:
|
77 |
+
-- model: PaSST model on a GPU.
|
78 |
+
-- audio_path: path to the audio to be loaded with librosa.
|
79 |
+
-- window_size (default=10 sec): analysis window (and receptive field) of PaSST.
|
80 |
+
-- overlap (default=5 sec): overlap of the running analysis window for inputs longar than window_size (10 sec).
|
81 |
+
-- collect (default='mean'): for longer inputs, aggregate/collect via 'mean' or 'max' pooling along logits vector.
|
82 |
+
Returns:
|
83 |
+
-- 527 probabilities (after softmax, no logarithm).
|
84 |
+
"""
|
85 |
+
# load the audio using librosa
|
86 |
+
audio, _ = librosa.load(audio_path, sr=SAMPLING_RATE, mono=True)
|
87 |
+
audio = pyln.normalize.peak(audio, -1.0)
|
88 |
+
|
89 |
+
# calculate the step size for the analysis windows with the specified overlap
|
90 |
+
step_size = int((window_size - overlap) * SAMPLING_RATE)
|
91 |
+
|
92 |
+
# iterate over the audio, creating analysis windows
|
93 |
+
probabilities = []
|
94 |
+
for i in range(0, max(step_size, len(audio) - step_size), step_size):
|
95 |
+
# extract the current analysis window
|
96 |
+
window = audio[i:i + int(window_size * SAMPLING_RATE)]
|
97 |
+
|
98 |
+
# pad the window with zeros if it's shorter than the desired window size
|
99 |
+
if len(window) < int(window_size * SAMPLING_RATE):
|
100 |
+
# discard window if it's too small (avoid mostly zeros predicted as silence), as in MusicGen:
|
101 |
+
# https://github.com/facebookresearch/audiocraft/blob/a2b96756956846e194c9255d0cdadc2b47c93f1b/audiocraft/metrics/kld.py
|
102 |
+
if len(window) > int(window_size * SAMPLING_RATE * 0.15):
|
103 |
+
tmp = np.zeros(int(window_size * SAMPLING_RATE))
|
104 |
+
tmp[:len(window)] = window
|
105 |
+
window = tmp
|
106 |
+
|
107 |
+
# convert to a PyTorch tensor and move to GPU
|
108 |
+
audio_wave = torch.from_numpy(window.astype(np.float32)).unsqueeze(0).cuda()
|
109 |
+
|
110 |
+
# get the probabilities for this analysis window
|
111 |
+
with open(os.devnull, 'w') as f, contextlib.redirect_stdout(f):
|
112 |
+
with torch.no_grad(), _patch_passt_stft():
|
113 |
+
logits = model(audio_wave)
|
114 |
+
probabilities.append(torch.squeeze(logits))
|
115 |
+
|
116 |
+
probabilities = torch.stack(probabilities)
|
117 |
+
if collect == 'mean':
|
118 |
+
probabilities = torch.mean(probabilities, dim=0)
|
119 |
+
elif collect == 'max':
|
120 |
+
probabilities, _ = torch.max(probabilities, dim=0)
|
121 |
+
|
122 |
+
return F.softmax(probabilities, dim=0).squeeze().cpu()
|
123 |
+
|
124 |
+
|
125 |
+
def passt_kld(ids, eval_path, eval_files_extension='.wav', ref_path=None, ref_files_extension='.wav', load_ref_probabilities=None, no_ids=[], collect='mean'):
|
126 |
+
"""
|
127 |
+
Compute KL-divergence between the label probabilities of the generated audio with respect to the original audio.
|
128 |
+
Both generated audio (in eval_path) and original audio (in ref_path) are represented by the same prompt/description.
|
129 |
+
Audios are identified by an id, that is the name of the file in both directories and links the audio with the prompt/description.
|
130 |
+
segmenting the audio
|
131 |
+
|
132 |
+
For inputs longer that the 10 sec PaSST was trained on, we aggregate/collect via 'mean' (default) or 'max' pooling along the logits vector.
|
133 |
+
We split the inpot into overlapping analysis windows. Subsequently, we aggregate/collect (accross windows) the generated logits and then apply a softmax.
|
134 |
+
|
135 |
+
This evaluation script assumes that ids are in both ref_path and eval_path.
|
136 |
+
|
137 |
+
We label probabilities via the PaSST model: https://github.com/kkoutini/PaSST
|
138 |
+
|
139 |
+
GPU-based computation.
|
140 |
+
|
141 |
+
Extracting the probabilities is timeconsuming. After being computed once, we store them.
|
142 |
+
We store pre-computed reference probabilities in load/
|
143 |
+
To load those and save computation, just set the path in load_ref_probabilities.
|
144 |
+
If load_ref_probabilities is set, ref_path is not required.
|
145 |
+
|
146 |
+
Params:
|
147 |
+
-- ids: list of ids present in both eval_path and ref_path.
|
148 |
+
-- eval_path: path where the generated audio files to evaluate are available.
|
149 |
+
-- eval_files_extenstion: files extension (default .wav) in eval_path.
|
150 |
+
-- ref_path: path where the reference audio files are available. (instead of load_ref_probabilities)
|
151 |
+
-- ref_files_extenstion: files extension (default .wav) in ref_path.
|
152 |
+
-- load_ref_probabilities: path to the reference probabilities. (inestead of ref_path)
|
153 |
+
-- no_ids: it is possible that some reference audio is corrupted or not present. Ignore some this list of ids.
|
154 |
+
-- collect (default='mean'): for longer inputs, aggregate/collect via 'mean' or 'max' pooling along the logits vector.
|
155 |
+
Returns:
|
156 |
+
-- KL divergence
|
157 |
+
"""
|
158 |
+
with open(os.devnull, 'w') as f, contextlib.redirect_stdout(f): # capturing all useless outputs from passt
|
159 |
+
# load model
|
160 |
+
model = get_basic_model(mode="logits")
|
161 |
+
model.eval()
|
162 |
+
model = model.cuda()
|
163 |
+
|
164 |
+
if not os.path.isdir(eval_path):
|
165 |
+
if not os.path.isfile(eval_path):
|
166 |
+
raise ValueError('eval_path does not exist')
|
167 |
+
|
168 |
+
if load_ref_probabilities:
|
169 |
+
if not os.path.exists(load_ref_probabilities):
|
170 |
+
raise ValueError('load_ref_probabilities does not exist')
|
171 |
+
print('[LOADING REFERENCE PROBABILITIES] ', load_ref_probabilities)
|
172 |
+
with open(load_ref_probabilities, 'rb') as fp:
|
173 |
+
ref_p = pickle.load(fp)
|
174 |
+
|
175 |
+
else:
|
176 |
+
if ref_path:
|
177 |
+
if not os.path.isdir(ref_path):
|
178 |
+
if os.path.isfile(ref_path):
|
179 |
+
id2utt = {}
|
180 |
+
with open(ref_path, "r") as f:
|
181 |
+
for line in f:
|
182 |
+
sec = line.strip().split(" ")
|
183 |
+
id2utt[sec[0]] = sec[1]
|
184 |
+
f.close()
|
185 |
+
else:
|
186 |
+
raise ValueError("ref_path does not exist")
|
187 |
+
print('[EXTRACTING REFERENCE PROBABILITIES] ', ref_path)
|
188 |
+
ref_p = {}
|
189 |
+
for id in tqdm(ids):
|
190 |
+
if id not in no_ids:
|
191 |
+
try:
|
192 |
+
if os.path.isfile(ref_path):
|
193 |
+
if id in id2utt.keys():
|
194 |
+
audio_path = id2utt[id]
|
195 |
+
else:
|
196 |
+
raise ValueError(f"id: {id} not in {ref_path}!")
|
197 |
+
else:
|
198 |
+
audio_path = os.path.join(ref_path, str(id)+ref_files_extension)
|
199 |
+
if os.path.isfile(audio_path):
|
200 |
+
ref_p[id] = return_probabilities(model, audio_path, collect=collect)
|
201 |
+
except Exception as e:
|
202 |
+
print(f"An unexpected error occurred with {id}: {e}\nIf you failed to download it you can add it to no_ids list.")
|
203 |
+
|
204 |
+
# store reference probabilities to load later on
|
205 |
+
if not os.path.exists('load/passt_kld/'):
|
206 |
+
os.makedirs('load/passt_kld/')
|
207 |
+
save_ref_probabilities_path = 'load/passt_kld/'+ref_path.replace('/', '_')+'_collect'+str(collect)+'__reference_probabilities.pkl'
|
208 |
+
with open(save_ref_probabilities_path, 'wb') as fp:
|
209 |
+
pickle.dump(ref_p, fp)
|
210 |
+
print('[REFERENCE EMBEDDINGS][SAVED] ', save_ref_probabilities_path)
|
211 |
+
|
212 |
+
else:
|
213 |
+
raise ValueError('Must specify ref_path or load_ref_probabilities')
|
214 |
+
|
215 |
+
print('[EVALUATING GENERATIONS] ', eval_path)
|
216 |
+
|
217 |
+
passt_kl = 0
|
218 |
+
count = 0
|
219 |
+
for id in tqdm(ids):
|
220 |
+
if id not in no_ids:
|
221 |
+
try:
|
222 |
+
audio_path = os.path.join(eval_path, str(id)+eval_files_extension)
|
223 |
+
if os.path.isfile(audio_path):
|
224 |
+
eval_p = return_probabilities(model, audio_path, collect=collect)
|
225 |
+
# note: F.kl_div(x, y) is KL(y||x)
|
226 |
+
# see: https://github.com/pytorch/pytorch/issues/7337
|
227 |
+
# see: https://discuss.pytorch.org/t/kl-divergence-different-results-from-tf/56903/2
|
228 |
+
passt_kl += F.kl_div((ref_p[id] + 1e-6).log(), eval_p, reduction='sum', log_target=False)
|
229 |
+
count += 1
|
230 |
+
except Exception as e:
|
231 |
+
print(f"An unexpected error occurred with {id}: {e}\nIf you failed to download it you can add it to no_ids list.")
|
232 |
+
return passt_kl / count if count > 0 else 0
|
inspiremusic/music_tokenizer/__init__.py
ADDED
File without changes
|
inspiremusic/music_tokenizer/env.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
|
15 |
+
import os
|
16 |
+
import shutil
|
17 |
+
|
18 |
+
|
19 |
+
class AttrDict(dict):
|
20 |
+
def __init__(self, *args, **kwargs):
|
21 |
+
super(AttrDict, self).__init__(*args, **kwargs)
|
22 |
+
self.__dict__ = self
|
23 |
+
|
24 |
+
|
25 |
+
def build_env(config, config_name, path):
|
26 |
+
t_path = os.path.join(path, config_name)
|
27 |
+
if config != t_path:
|
28 |
+
os.makedirs(path, exist_ok=True)
|
29 |
+
shutil.copyfile(config, os.path.join(path, config_name))
|
inspiremusic/music_tokenizer/meldataset.py
ADDED
@@ -0,0 +1,226 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
|
15 |
+
# code based on https://github.com/b04901014/MQTTS
|
16 |
+
import math
|
17 |
+
import os
|
18 |
+
import random
|
19 |
+
|
20 |
+
import librosa
|
21 |
+
import numpy as np
|
22 |
+
import torch.utils.data
|
23 |
+
from librosa.filters import mel as librosa_mel_fn
|
24 |
+
|
25 |
+
def load_wav(full_path, sr):
|
26 |
+
wav, sr = librosa.load(full_path, sr=sr)
|
27 |
+
return wav, sr
|
28 |
+
|
29 |
+
def dynamic_range_compression(x, C=1, clip_val=1e-5):
|
30 |
+
return np.log(np.clip(x, a_min=clip_val, a_max=None) * C)
|
31 |
+
|
32 |
+
def dynamic_range_decompression(x, C=1):
|
33 |
+
return np.exp(x) / C
|
34 |
+
|
35 |
+
def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):
|
36 |
+
return torch.log(torch.clamp(x, min=clip_val) * C)
|
37 |
+
|
38 |
+
def dynamic_range_decompression_torch(x, C=1):
|
39 |
+
return torch.exp(x) / C
|
40 |
+
|
41 |
+
def spectral_normalize_torch(magnitudes):
|
42 |
+
output = dynamic_range_compression_torch(magnitudes)
|
43 |
+
return output
|
44 |
+
|
45 |
+
def spectral_de_normalize_torch(magnitudes):
|
46 |
+
output = dynamic_range_decompression_torch(magnitudes)
|
47 |
+
return output
|
48 |
+
|
49 |
+
mel_basis = {}
|
50 |
+
hann_window = {}
|
51 |
+
|
52 |
+
## modified to get stft with return complex value = True for pytorch ver2.0
|
53 |
+
def mel_spectrogram(y,
|
54 |
+
n_fft,
|
55 |
+
num_mels,
|
56 |
+
sampling_rate,
|
57 |
+
hop_size,
|
58 |
+
win_size,
|
59 |
+
fmin,
|
60 |
+
fmax,
|
61 |
+
center=False):
|
62 |
+
|
63 |
+
global mel_basis, hann_window
|
64 |
+
if fmax not in mel_basis:
|
65 |
+
mel = librosa_mel_fn(sampling_rate, n_fft, num_mels, fmin, fmax)
|
66 |
+
mel_basis[str(fmax) + '_' +
|
67 |
+
str(y.device)] = torch.from_numpy(mel).float().to(y.device)
|
68 |
+
hann_window[str(y.device)] = torch.hann_window(win_size).to(y.device)
|
69 |
+
|
70 |
+
y = torch.nn.functional.pad(
|
71 |
+
y.unsqueeze(1), (int((n_fft - hop_size) / 2), int(
|
72 |
+
(n_fft - hop_size) / 2)),
|
73 |
+
mode='reflect')
|
74 |
+
y = y.squeeze(1)
|
75 |
+
|
76 |
+
spec = torch.view_as_real(torch.stft(
|
77 |
+
y,
|
78 |
+
n_fft,
|
79 |
+
hop_length=hop_size,
|
80 |
+
win_length=win_size,
|
81 |
+
window=hann_window[str(y.device)],
|
82 |
+
center=center,
|
83 |
+
pad_mode='reflect',
|
84 |
+
normalized=False,
|
85 |
+
onesided=True,
|
86 |
+
return_complex=True
|
87 |
+
))
|
88 |
+
|
89 |
+
spec = torch.sqrt(spec.pow(2).sum(-1) + (1e-9))
|
90 |
+
|
91 |
+
spec = torch.matmul(mel_basis[str(fmax) + '_' + str(y.device)], spec)
|
92 |
+
spec = spectral_normalize_torch(spec)
|
93 |
+
|
94 |
+
return spec
|
95 |
+
|
96 |
+
|
97 |
+
def get_dataset_filelist(a):
|
98 |
+
with open(a.input_training_file, 'r') as f:
|
99 |
+
training_files = [l.strip() for l in f]
|
100 |
+
with open(a.input_validation_file, 'r') as f:
|
101 |
+
validation_files = [l.strip() for l in f]
|
102 |
+
return training_files, validation_files
|
103 |
+
|
104 |
+
|
105 |
+
class MelDataset(torch.utils.data.Dataset):
|
106 |
+
def __init__(self,
|
107 |
+
training_files,
|
108 |
+
segment_size,
|
109 |
+
n_fft,
|
110 |
+
num_mels,
|
111 |
+
hop_size,
|
112 |
+
win_size,
|
113 |
+
sampling_rate,
|
114 |
+
fmin,
|
115 |
+
fmax,
|
116 |
+
split=True,
|
117 |
+
shuffle=True,
|
118 |
+
n_cache_reuse=1,
|
119 |
+
device=None,
|
120 |
+
fmax_loss=None,
|
121 |
+
fine_tuning=False,
|
122 |
+
base_mels_path=None):
|
123 |
+
self.audio_files = training_files
|
124 |
+
random.seed(1234)
|
125 |
+
if shuffle:
|
126 |
+
random.shuffle(self.audio_files)
|
127 |
+
self.segment_size = segment_size
|
128 |
+
self.sampling_rate = sampling_rate
|
129 |
+
self.split = split
|
130 |
+
self.n_fft = n_fft
|
131 |
+
self.num_mels = num_mels
|
132 |
+
self.hop_size = hop_size
|
133 |
+
self.win_size = win_size
|
134 |
+
self.fmin = fmin
|
135 |
+
self.fmax = fmax
|
136 |
+
self.fmax_loss = fmax_loss
|
137 |
+
self.cached_wav = None
|
138 |
+
self.n_cache_reuse = n_cache_reuse
|
139 |
+
self._cache_ref_count = 0
|
140 |
+
self.device = device
|
141 |
+
self.fine_tuning = fine_tuning
|
142 |
+
self.base_mels_path = base_mels_path
|
143 |
+
|
144 |
+
def __getitem__(self, index):
|
145 |
+
filename = self.audio_files[index]
|
146 |
+
if self._cache_ref_count == 0:
|
147 |
+
try:
|
148 |
+
# Note by yuantian: load with the sample_rate of config
|
149 |
+
audio, sampling_rate = load_wav(filename, sr=self.sampling_rate)
|
150 |
+
except Exception as e:
|
151 |
+
print(f"Error on audio: {filename}")
|
152 |
+
audio = np.random.normal(size=(160000, )) * 0.05
|
153 |
+
sampling_rate = self.sampling_rate
|
154 |
+
self.cached_wav = audio
|
155 |
+
if sampling_rate != self.sampling_rate:
|
156 |
+
raise ValueError("{} SR doesn't match target {} SR".format(
|
157 |
+
sampling_rate, self.sampling_rate))
|
158 |
+
self._cache_ref_count = self.n_cache_reuse
|
159 |
+
else:
|
160 |
+
audio = self.cached_wav
|
161 |
+
self._cache_ref_count -= 1
|
162 |
+
|
163 |
+
audio = torch.FloatTensor(audio)
|
164 |
+
audio = audio.unsqueeze(0)
|
165 |
+
|
166 |
+
if not self.fine_tuning:
|
167 |
+
if self.split:
|
168 |
+
if audio.size(1) >= self.segment_size:
|
169 |
+
max_audio_start = audio.size(1) - self.segment_size
|
170 |
+
audio_start = random.randint(0, max_audio_start)
|
171 |
+
audio = audio[:, audio_start:audio_start +
|
172 |
+
self.segment_size]
|
173 |
+
else:
|
174 |
+
audio = torch.nn.functional.pad(audio, (
|
175 |
+
0, self.segment_size - audio.size(1)), 'constant')
|
176 |
+
|
177 |
+
mel = mel_spectrogram(
|
178 |
+
audio,
|
179 |
+
self.n_fft,
|
180 |
+
self.num_mels,
|
181 |
+
self.sampling_rate,
|
182 |
+
self.hop_size,
|
183 |
+
self.win_size,
|
184 |
+
self.fmin,
|
185 |
+
self.fmax,
|
186 |
+
center=False)
|
187 |
+
else:
|
188 |
+
mel = np.load(
|
189 |
+
os.path.join(self.base_mels_path,
|
190 |
+
os.path.splitext(os.path.split(filename)[-1])[0] +
|
191 |
+
'.npy'))
|
192 |
+
mel = torch.from_numpy(mel)
|
193 |
+
|
194 |
+
if len(mel.shape) < 3:
|
195 |
+
mel = mel.unsqueeze(0)
|
196 |
+
|
197 |
+
if self.split:
|
198 |
+
frames_per_seg = math.ceil(self.segment_size / self.hop_size)
|
199 |
+
|
200 |
+
if audio.size(1) >= self.segment_size:
|
201 |
+
mel_start = random.randint(0,
|
202 |
+
mel.size(2) - frames_per_seg - 1)
|
203 |
+
mel = mel[:, :, mel_start:mel_start + frames_per_seg]
|
204 |
+
audio = audio[:, mel_start * self.hop_size:(
|
205 |
+
mel_start + frames_per_seg) * self.hop_size]
|
206 |
+
else:
|
207 |
+
mel = torch.nn.functional.pad(mel, (
|
208 |
+
0, frames_per_seg - mel.size(2)), 'constant')
|
209 |
+
audio = torch.nn.functional.pad(audio, (
|
210 |
+
0, self.segment_size - audio.size(1)), 'constant')
|
211 |
+
|
212 |
+
mel_loss = mel_spectrogram(
|
213 |
+
audio,
|
214 |
+
self.n_fft,
|
215 |
+
self.num_mels,
|
216 |
+
self.sampling_rate,
|
217 |
+
self.hop_size,
|
218 |
+
self.win_size,
|
219 |
+
self.fmin,
|
220 |
+
self.fmax_loss,
|
221 |
+
center=False)
|
222 |
+
|
223 |
+
return (mel.squeeze(), audio.squeeze(0), filename, mel_loss.squeeze())
|
224 |
+
|
225 |
+
def __len__(self):
|
226 |
+
return len(self.audio_files)
|
inspiremusic/music_tokenizer/models.py
ADDED
@@ -0,0 +1,548 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
|
15 |
+
import torch
|
16 |
+
import torch.nn as nn
|
17 |
+
import torch.nn.functional as F
|
18 |
+
from torch.nn import AvgPool1d
|
19 |
+
from torch.nn import Conv1d
|
20 |
+
from torch.nn import Conv2d
|
21 |
+
from torch.nn import ConvTranspose1d
|
22 |
+
from torch.nn.utils import remove_weight_norm
|
23 |
+
from torch.nn.utils import spectral_norm
|
24 |
+
from torch.nn.utils import weight_norm
|
25 |
+
|
26 |
+
from inspiremusic.utils.tokenizer_utils import get_padding
|
27 |
+
from inspiremusic.utils.tokenizer_utils import init_weights
|
28 |
+
|
29 |
+
LRELU_SLOPE = 0.1
|
30 |
+
|
31 |
+
class ResBlock1(torch.nn.Module):
|
32 |
+
def __init__(self, h, channels, kernel_size=3, dilation=(1, 3, 5)):
|
33 |
+
super(ResBlock1, self).__init__()
|
34 |
+
self.h = h
|
35 |
+
self.convs1 = nn.ModuleList([
|
36 |
+
weight_norm(
|
37 |
+
Conv1d(
|
38 |
+
channels,
|
39 |
+
channels,
|
40 |
+
kernel_size,
|
41 |
+
1,
|
42 |
+
dilation=dilation[0],
|
43 |
+
padding=get_padding(kernel_size, dilation[0]))),
|
44 |
+
weight_norm(
|
45 |
+
Conv1d(
|
46 |
+
channels,
|
47 |
+
channels,
|
48 |
+
kernel_size,
|
49 |
+
1,
|
50 |
+
dilation=dilation[1],
|
51 |
+
padding=get_padding(kernel_size, dilation[1]))),
|
52 |
+
weight_norm(
|
53 |
+
Conv1d(
|
54 |
+
channels,
|
55 |
+
channels,
|
56 |
+
kernel_size,
|
57 |
+
1,
|
58 |
+
dilation=dilation[2],
|
59 |
+
padding=get_padding(kernel_size, dilation[2])))
|
60 |
+
])
|
61 |
+
self.convs1.apply(init_weights)
|
62 |
+
|
63 |
+
self.convs2 = nn.ModuleList([
|
64 |
+
weight_norm(
|
65 |
+
Conv1d(
|
66 |
+
channels,
|
67 |
+
channels,
|
68 |
+
kernel_size,
|
69 |
+
1,
|
70 |
+
dilation=1,
|
71 |
+
padding=get_padding(kernel_size, 1))), weight_norm(
|
72 |
+
Conv1d(
|
73 |
+
channels,
|
74 |
+
channels,
|
75 |
+
kernel_size,
|
76 |
+
1,
|
77 |
+
dilation=1,
|
78 |
+
padding=get_padding(kernel_size, 1))), weight_norm(
|
79 |
+
Conv1d(
|
80 |
+
channels,
|
81 |
+
channels,
|
82 |
+
kernel_size,
|
83 |
+
1,
|
84 |
+
dilation=1,
|
85 |
+
padding=get_padding(kernel_size, 1)))
|
86 |
+
])
|
87 |
+
self.convs2.apply(init_weights)
|
88 |
+
|
89 |
+
def forward(self, x):
|
90 |
+
for c1, c2 in zip(self.convs1, self.convs2):
|
91 |
+
xt = F.leaky_relu(x, LRELU_SLOPE)
|
92 |
+
xt = c1(xt)
|
93 |
+
xt = F.leaky_relu(xt, LRELU_SLOPE)
|
94 |
+
xt = c2(xt)
|
95 |
+
x = xt + x
|
96 |
+
return x
|
97 |
+
|
98 |
+
def remove_weight_norm(self):
|
99 |
+
for l in self.convs1:
|
100 |
+
remove_weight_norm(l)
|
101 |
+
for l in self.convs2:
|
102 |
+
remove_weight_norm(l)
|
103 |
+
|
104 |
+
|
105 |
+
class ResBlock2(torch.nn.Module):
|
106 |
+
def __init__(self, h, channels, kernel_size=3, dilation=(1, 3)):
|
107 |
+
super(ResBlock2, self).__init__()
|
108 |
+
self.h = h
|
109 |
+
self.convs = nn.ModuleList([
|
110 |
+
weight_norm(
|
111 |
+
Conv1d(
|
112 |
+
channels,
|
113 |
+
channels,
|
114 |
+
kernel_size,
|
115 |
+
1,
|
116 |
+
dilation=dilation[0],
|
117 |
+
padding=get_padding(kernel_size, dilation[0]))),
|
118 |
+
weight_norm(
|
119 |
+
Conv1d(
|
120 |
+
channels,
|
121 |
+
channels,
|
122 |
+
kernel_size,
|
123 |
+
1,
|
124 |
+
dilation=dilation[1],
|
125 |
+
padding=get_padding(kernel_size, dilation[1])))
|
126 |
+
])
|
127 |
+
self.convs.apply(init_weights)
|
128 |
+
|
129 |
+
def forward(self, x):
|
130 |
+
for c in self.convs:
|
131 |
+
xt = F.leaky_relu(x, LRELU_SLOPE)
|
132 |
+
xt = c(xt)
|
133 |
+
x = xt + x
|
134 |
+
return x
|
135 |
+
|
136 |
+
def remove_weight_norm(self):
|
137 |
+
for l in self.convs:
|
138 |
+
remove_weight_norm(l)
|
139 |
+
|
140 |
+
|
141 |
+
class Generator(torch.nn.Module):
|
142 |
+
def __init__(self, h):
|
143 |
+
super(Generator, self).__init__()
|
144 |
+
self.h = h
|
145 |
+
self.num_kernels = len(h.resblock_kernel_sizes)
|
146 |
+
self.num_upsamples = len(h.upsample_rates)
|
147 |
+
self.conv_pre = weight_norm(
|
148 |
+
Conv1d(512, h.upsample_initial_channel, 7, 1, padding=3))
|
149 |
+
resblock = ResBlock1 if h.resblock == '1' else ResBlock2
|
150 |
+
|
151 |
+
self.ups = nn.ModuleList()
|
152 |
+
for i, (u,
|
153 |
+
k) in enumerate(zip(h.upsample_rates, h.upsample_kernel_sizes)):
|
154 |
+
self.ups.append(
|
155 |
+
weight_norm(
|
156 |
+
ConvTranspose1d(
|
157 |
+
h.upsample_initial_channel // (2**i),
|
158 |
+
h.upsample_initial_channel // (2**(i + 1)),
|
159 |
+
k,
|
160 |
+
u,
|
161 |
+
# padding=(u//2 + u%2),
|
162 |
+
padding=(k - u) // 2,
|
163 |
+
# output_padding=u%2
|
164 |
+
)))
|
165 |
+
|
166 |
+
self.resblocks = nn.ModuleList()
|
167 |
+
for i in range(len(self.ups)):
|
168 |
+
ch = h.upsample_initial_channel // (2**(i + 1))
|
169 |
+
for j, (k, d) in enumerate(
|
170 |
+
zip(h.resblock_kernel_sizes, h.resblock_dilation_sizes)):
|
171 |
+
self.resblocks.append(resblock(h, ch, k, d))
|
172 |
+
|
173 |
+
self.conv_post = weight_norm(Conv1d(ch, 1, 7, 1, padding=3))
|
174 |
+
self.ups.apply(init_weights)
|
175 |
+
self.conv_post.apply(init_weights)
|
176 |
+
|
177 |
+
def forward(self, x):
|
178 |
+
x = self.conv_pre(x)
|
179 |
+
for i in range(self.num_upsamples):
|
180 |
+
x = F.leaky_relu(x, LRELU_SLOPE)
|
181 |
+
x = self.ups[i](x)
|
182 |
+
xs = None
|
183 |
+
for j in range(self.num_kernels):
|
184 |
+
if xs is None:
|
185 |
+
xs = self.resblocks[i * self.num_kernels + j](x)
|
186 |
+
else:
|
187 |
+
xs += self.resblocks[i * self.num_kernels + j](x)
|
188 |
+
x = xs / self.num_kernels
|
189 |
+
x = F.leaky_relu(x, LRELU_SLOPE)
|
190 |
+
x = self.conv_post(x)
|
191 |
+
x = torch.tanh(x)
|
192 |
+
|
193 |
+
return x
|
194 |
+
|
195 |
+
def remove_weight_norm(self):
|
196 |
+
print('Removing weight norm...')
|
197 |
+
for l in self.ups:
|
198 |
+
remove_weight_norm(l)
|
199 |
+
for l in self.resblocks:
|
200 |
+
l.remove_weight_norm()
|
201 |
+
remove_weight_norm(self.conv_pre)
|
202 |
+
remove_weight_norm(self.conv_post)
|
203 |
+
|
204 |
+
|
205 |
+
class DiscriminatorP(torch.nn.Module):
|
206 |
+
def __init__(self, period, kernel_size=5, stride=3,
|
207 |
+
use_spectral_norm=False):
|
208 |
+
super(DiscriminatorP, self).__init__()
|
209 |
+
self.period = period
|
210 |
+
norm_f = weight_norm if use_spectral_norm is False else spectral_norm
|
211 |
+
self.convs = nn.ModuleList([
|
212 |
+
norm_f(
|
213 |
+
Conv2d(
|
214 |
+
1,
|
215 |
+
32, (kernel_size, 1), (stride, 1),
|
216 |
+
padding=(get_padding(5, 1), 0))),
|
217 |
+
norm_f(
|
218 |
+
Conv2d(
|
219 |
+
32,
|
220 |
+
128, (kernel_size, 1), (stride, 1),
|
221 |
+
padding=(get_padding(5, 1), 0))),
|
222 |
+
norm_f(
|
223 |
+
Conv2d(
|
224 |
+
128,
|
225 |
+
512, (kernel_size, 1), (stride, 1),
|
226 |
+
padding=(get_padding(5, 1), 0))),
|
227 |
+
norm_f(
|
228 |
+
Conv2d(
|
229 |
+
512,
|
230 |
+
1024, (kernel_size, 1), (stride, 1),
|
231 |
+
padding=(get_padding(5, 1), 0))),
|
232 |
+
norm_f(Conv2d(1024, 1024, (kernel_size, 1), 1, padding=(2, 0))),
|
233 |
+
])
|
234 |
+
self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
|
235 |
+
|
236 |
+
def forward(self, x):
|
237 |
+
fmap = []
|
238 |
+
|
239 |
+
# 1d to 2d
|
240 |
+
b, c, t = x.shape
|
241 |
+
if t % self.period != 0: # pad first
|
242 |
+
n_pad = self.period - (t % self.period)
|
243 |
+
x = F.pad(x, (0, n_pad), "reflect")
|
244 |
+
t = t + n_pad
|
245 |
+
x = x.view(b, c, t // self.period, self.period)
|
246 |
+
|
247 |
+
for l in self.convs:
|
248 |
+
x = l(x)
|
249 |
+
x = F.leaky_relu(x, LRELU_SLOPE)
|
250 |
+
fmap.append(x)
|
251 |
+
x = self.conv_post(x)
|
252 |
+
fmap.append(x)
|
253 |
+
x = torch.flatten(x, 1, -1)
|
254 |
+
|
255 |
+
return x, fmap
|
256 |
+
|
257 |
+
|
258 |
+
class MultiPeriodDiscriminator(torch.nn.Module):
|
259 |
+
def __init__(self):
|
260 |
+
super(MultiPeriodDiscriminator, self).__init__()
|
261 |
+
self.discriminators = nn.ModuleList([
|
262 |
+
DiscriminatorP(2),
|
263 |
+
DiscriminatorP(3),
|
264 |
+
DiscriminatorP(5),
|
265 |
+
DiscriminatorP(7),
|
266 |
+
DiscriminatorP(11),
|
267 |
+
])
|
268 |
+
|
269 |
+
def forward(self, y, y_hat):
|
270 |
+
y_d_rs = []
|
271 |
+
y_d_gs = []
|
272 |
+
fmap_rs = []
|
273 |
+
fmap_gs = []
|
274 |
+
for i, d in enumerate(self.discriminators):
|
275 |
+
y_d_r, fmap_r = d(y)
|
276 |
+
y_d_g, fmap_g = d(y_hat)
|
277 |
+
y_d_rs.append(y_d_r)
|
278 |
+
fmap_rs.append(fmap_r)
|
279 |
+
y_d_gs.append(y_d_g)
|
280 |
+
fmap_gs.append(fmap_g)
|
281 |
+
|
282 |
+
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
|
283 |
+
|
284 |
+
|
285 |
+
class DiscriminatorS(torch.nn.Module):
|
286 |
+
def __init__(self, use_spectral_norm=False):
|
287 |
+
super(DiscriminatorS, self).__init__()
|
288 |
+
norm_f = weight_norm if use_spectral_norm is False else spectral_norm
|
289 |
+
self.convs = nn.ModuleList([
|
290 |
+
norm_f(Conv1d(1, 128, 15, 1, padding=7)),
|
291 |
+
norm_f(Conv1d(128, 128, 41, 2, groups=4, padding=20)),
|
292 |
+
norm_f(Conv1d(128, 256, 41, 2, groups=16, padding=20)),
|
293 |
+
norm_f(Conv1d(256, 512, 41, 4, groups=16, padding=20)),
|
294 |
+
norm_f(Conv1d(512, 1024, 41, 4, groups=16, padding=20)),
|
295 |
+
norm_f(Conv1d(1024, 1024, 41, 1, groups=16, padding=20)),
|
296 |
+
norm_f(Conv1d(1024, 1024, 5, 1, padding=2)),
|
297 |
+
])
|
298 |
+
self.conv_post = norm_f(Conv1d(1024, 1, 3, 1, padding=1))
|
299 |
+
|
300 |
+
def forward(self, x):
|
301 |
+
fmap = []
|
302 |
+
for l in self.convs:
|
303 |
+
x = l(x)
|
304 |
+
x = F.leaky_relu(x, LRELU_SLOPE)
|
305 |
+
fmap.append(x)
|
306 |
+
x = self.conv_post(x)
|
307 |
+
fmap.append(x)
|
308 |
+
x = torch.flatten(x, 1, -1)
|
309 |
+
|
310 |
+
return x, fmap
|
311 |
+
|
312 |
+
|
313 |
+
class MultiScaleDiscriminator(torch.nn.Module):
|
314 |
+
def __init__(self):
|
315 |
+
super(MultiScaleDiscriminator, self).__init__()
|
316 |
+
self.discriminators = nn.ModuleList([
|
317 |
+
DiscriminatorS(use_spectral_norm=True),
|
318 |
+
DiscriminatorS(),
|
319 |
+
DiscriminatorS(),
|
320 |
+
])
|
321 |
+
self.meanpools = nn.ModuleList(
|
322 |
+
[AvgPool1d(4, 2, padding=2), AvgPool1d(4, 2, padding=2)])
|
323 |
+
|
324 |
+
def forward(self, y, y_hat):
|
325 |
+
y_d_rs = []
|
326 |
+
y_d_gs = []
|
327 |
+
fmap_rs = []
|
328 |
+
fmap_gs = []
|
329 |
+
for i, d in enumerate(self.discriminators):
|
330 |
+
if i != 0:
|
331 |
+
y = self.meanpools[i - 1](y)
|
332 |
+
y_hat = self.meanpools[i - 1](y_hat)
|
333 |
+
y_d_r, fmap_r = d(y)
|
334 |
+
y_d_g, fmap_g = d(y_hat)
|
335 |
+
y_d_rs.append(y_d_r)
|
336 |
+
fmap_rs.append(fmap_r)
|
337 |
+
y_d_gs.append(y_d_g)
|
338 |
+
fmap_gs.append(fmap_g)
|
339 |
+
|
340 |
+
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
|
341 |
+
|
342 |
+
|
343 |
+
def feature_loss(fmap_r, fmap_g):
|
344 |
+
loss = 0
|
345 |
+
for dr, dg in zip(fmap_r, fmap_g):
|
346 |
+
for rl, gl in zip(dr, dg):
|
347 |
+
loss += torch.mean(torch.abs(rl - gl))
|
348 |
+
|
349 |
+
return loss * 2
|
350 |
+
|
351 |
+
|
352 |
+
def discriminator_loss(disc_real_outputs, disc_generated_outputs):
|
353 |
+
loss = 0
|
354 |
+
r_losses = []
|
355 |
+
g_losses = []
|
356 |
+
for dr, dg in zip(disc_real_outputs, disc_generated_outputs):
|
357 |
+
r_loss = torch.mean((1 - dr)**2)
|
358 |
+
g_loss = torch.mean(dg**2)
|
359 |
+
loss += (r_loss + g_loss)
|
360 |
+
r_losses.append(r_loss.item())
|
361 |
+
g_losses.append(g_loss.item())
|
362 |
+
|
363 |
+
return loss, r_losses, g_losses
|
364 |
+
|
365 |
+
|
366 |
+
def generator_loss(disc_outputs):
|
367 |
+
loss = 0
|
368 |
+
gen_losses = []
|
369 |
+
for dg in disc_outputs:
|
370 |
+
l = torch.mean((1 - dg)**2)
|
371 |
+
gen_losses.append(l)
|
372 |
+
loss += l
|
373 |
+
|
374 |
+
return loss, gen_losses
|
375 |
+
|
376 |
+
|
377 |
+
class Encoder(torch.nn.Module):
|
378 |
+
def __init__(self, h):
|
379 |
+
super(Encoder, self).__init__()
|
380 |
+
self.h = h
|
381 |
+
self.num_kernels = len(h.resblock_kernel_sizes)
|
382 |
+
self.num_upsamples = len(h.upsample_rates)
|
383 |
+
self.conv_pre = weight_norm(Conv1d(1, 32, 7, 1, padding=3))
|
384 |
+
self.normalize = nn.ModuleList()
|
385 |
+
resblock = ResBlock1 if h.resblock == '1' else ResBlock2
|
386 |
+
|
387 |
+
self.ups = nn.ModuleList()
|
388 |
+
for i, (u, k) in enumerate(
|
389 |
+
list(
|
390 |
+
reversed(
|
391 |
+
list(zip(h.upsample_rates, h.upsample_kernel_sizes))))):
|
392 |
+
self.ups.append(
|
393 |
+
weight_norm(
|
394 |
+
Conv1d(
|
395 |
+
32 * (2**i),
|
396 |
+
32 * (2**(i + 1)),
|
397 |
+
k,
|
398 |
+
u,
|
399 |
+
padding=((k - u) // 2)
|
400 |
+
# padding=(u//2 + u%2)
|
401 |
+
)))
|
402 |
+
self.resblocks = nn.ModuleList()
|
403 |
+
for i in range(len(self.ups)):
|
404 |
+
ch = 32 * (2**(i + 1))
|
405 |
+
for j, (k, d) in enumerate(
|
406 |
+
zip(
|
407 |
+
list(reversed(h.resblock_kernel_sizes)),
|
408 |
+
list(reversed(h.resblock_dilation_sizes)))):
|
409 |
+
self.resblocks.append(resblock(h, ch, k, d))
|
410 |
+
self.normalize.append(
|
411 |
+
torch.nn.GroupNorm(ch // 16, ch, eps=1e-6, affine=True))
|
412 |
+
self.conv_post = Conv1d(512, 512, 3, 1, padding=1)
|
413 |
+
self.ups.apply(init_weights)
|
414 |
+
self.conv_post.apply(init_weights)
|
415 |
+
|
416 |
+
def forward(self, x):
|
417 |
+
x = self.conv_pre(x)
|
418 |
+
for i in range(self.num_upsamples):
|
419 |
+
x = F.leaky_relu(x, LRELU_SLOPE)
|
420 |
+
x = self.ups[i](x)
|
421 |
+
xs = None
|
422 |
+
for j in range(self.num_kernels):
|
423 |
+
if xs is None:
|
424 |
+
xs = self.resblocks[i * self.num_kernels + j](x)
|
425 |
+
xs = self.normalize[i * self.num_kernels + j](xs)
|
426 |
+
else:
|
427 |
+
xs += self.resblocks[i * self.num_kernels + j](x)
|
428 |
+
xs = self.normalize[i * self.num_kernels + j](xs)
|
429 |
+
x = xs / self.num_kernels
|
430 |
+
x = F.leaky_relu(x)
|
431 |
+
x = self.conv_post(x)
|
432 |
+
return x
|
433 |
+
|
434 |
+
def remove_weight_norm(self):
|
435 |
+
print('Removing weight norm...')
|
436 |
+
for l in self.ups:
|
437 |
+
remove_weight_norm(l)
|
438 |
+
for l in self.resblocks:
|
439 |
+
l.remove_weight_norm()
|
440 |
+
remove_weight_norm(self.conv_pre)
|
441 |
+
|
442 |
+
|
443 |
+
class Quantizer_module(torch.nn.Module):
|
444 |
+
def __init__(self, n_e, e_dim):
|
445 |
+
super(Quantizer_module, self).__init__()
|
446 |
+
self.embedding = nn.Embedding(n_e, e_dim)
|
447 |
+
self.embedding.weight.data.uniform_(-1.0 / n_e, 1.0 / n_e)
|
448 |
+
|
449 |
+
def forward(self, x):
|
450 |
+
# compute Euclidean distance
|
451 |
+
d = torch.sum(x ** 2, 1, keepdim=True) + torch.sum(self.embedding.weight ** 2, 1) \
|
452 |
+
- 2 * torch.matmul(x, self.embedding.weight.T)
|
453 |
+
min_indicies = torch.argmin(d, 1)
|
454 |
+
z_q = self.embedding(min_indicies)
|
455 |
+
return z_q, min_indicies
|
456 |
+
|
457 |
+
|
458 |
+
class Quantizer(torch.nn.Module):
|
459 |
+
def __init__(self, h):
|
460 |
+
super(Quantizer, self).__init__()
|
461 |
+
assert 512 % h.n_code_groups == 0
|
462 |
+
self.quantizer_modules = nn.ModuleList([
|
463 |
+
Quantizer_module(h.n_codes, 512 // h.n_code_groups)
|
464 |
+
for _ in range(h.n_code_groups)
|
465 |
+
])
|
466 |
+
self.quantizer_modules2 = nn.ModuleList([
|
467 |
+
Quantizer_module(h.n_codes, 512 // h.n_code_groups)
|
468 |
+
for _ in range(h.n_code_groups)
|
469 |
+
])
|
470 |
+
self.h = h
|
471 |
+
self.codebook_loss_lambda = self.h.codebook_loss_lambda # e.g., 1
|
472 |
+
self.commitment_loss_lambda = self.h.commitment_loss_lambda # e.g., 0.25
|
473 |
+
self.residul_layer = 2
|
474 |
+
self.n_code_groups = h.n_code_groups
|
475 |
+
|
476 |
+
def for_one_step(self, xin, idx):
|
477 |
+
xin = xin.transpose(1, 2)
|
478 |
+
x = xin.reshape(-1, 512)
|
479 |
+
x = torch.split(x, 512 // self.h.n_code_groups, dim=-1)
|
480 |
+
min_indicies = []
|
481 |
+
z_q = []
|
482 |
+
if idx == 0:
|
483 |
+
for _x, m in zip(x, self.quantizer_modules):
|
484 |
+
_z_q, _min_indicies = m(_x)
|
485 |
+
z_q.append(_z_q)
|
486 |
+
min_indicies.append(_min_indicies) #B * T,
|
487 |
+
z_q = torch.cat(z_q, -1).reshape(xin.shape)
|
488 |
+
# loss = 0.25 * torch.mean((z_q.detach() - xin) ** 2) + torch.mean((z_q - xin.detach()) ** 2)
|
489 |
+
loss = self.codebook_loss_lambda * torch.mean((z_q - xin.detach()) ** 2) \
|
490 |
+
+ self.commitment_loss_lambda * torch.mean((z_q.detach() - xin) ** 2)
|
491 |
+
z_q = xin + (z_q - xin).detach()
|
492 |
+
z_q = z_q.transpose(1, 2)
|
493 |
+
return z_q, loss, min_indicies
|
494 |
+
else:
|
495 |
+
for _x, m in zip(x, self.quantizer_modules2):
|
496 |
+
_z_q, _min_indicies = m(_x)
|
497 |
+
z_q.append(_z_q)
|
498 |
+
min_indicies.append(_min_indicies) #B * T,
|
499 |
+
z_q = torch.cat(z_q, -1).reshape(xin.shape)
|
500 |
+
# loss = 0.25 * torch.mean((z_q.detach() - xin) ** 2) + torch.mean((z_q - xin.detach()) ** 2)
|
501 |
+
loss = self.codebook_loss_lambda * torch.mean((z_q - xin.detach()) ** 2) \
|
502 |
+
+ self.commitment_loss_lambda * torch.mean((z_q.detach() - xin) ** 2)
|
503 |
+
z_q = xin + (z_q - xin).detach()
|
504 |
+
z_q = z_q.transpose(1, 2)
|
505 |
+
return z_q, loss, min_indicies
|
506 |
+
|
507 |
+
def forward(self, xin):
|
508 |
+
#B, C, T
|
509 |
+
quantized_out = 0.0
|
510 |
+
residual = xin
|
511 |
+
all_losses = []
|
512 |
+
all_indices = []
|
513 |
+
for i in range(self.residul_layer):
|
514 |
+
quantized, loss, indices = self.for_one_step(residual, i) #
|
515 |
+
residual = residual - quantized
|
516 |
+
quantized_out = quantized_out + quantized
|
517 |
+
all_indices.extend(indices) #
|
518 |
+
all_losses.append(loss)
|
519 |
+
all_losses = torch.stack(all_losses)
|
520 |
+
loss = torch.mean(all_losses)
|
521 |
+
return quantized_out, loss, all_indices
|
522 |
+
|
523 |
+
def embed(self, x):
|
524 |
+
#idx: N, T, 4
|
525 |
+
#print('x ', x.shape)
|
526 |
+
quantized_out = torch.tensor(0.0, device=x.device)
|
527 |
+
x = torch.split(x, 1, 2) # split, 将最后一个维度分开, 每个属于一个index group
|
528 |
+
#print('x.shape ', len(x),x[0].shape)
|
529 |
+
for i in range(self.residul_layer):
|
530 |
+
ret = []
|
531 |
+
if i == 0:
|
532 |
+
for j in range(self.n_code_groups):
|
533 |
+
q = x[j]
|
534 |
+
embed = self.quantizer_modules[j]
|
535 |
+
q = embed.embedding(q.squeeze(-1).long())
|
536 |
+
ret.append(q)
|
537 |
+
ret = torch.cat(ret, -1)
|
538 |
+
#print(ret.shape)
|
539 |
+
quantized_out = quantized_out + ret
|
540 |
+
else:
|
541 |
+
for j in range(self.n_code_groups):
|
542 |
+
q = x[j + self.n_code_groups]
|
543 |
+
embed = self.quantizer_modules2[j]
|
544 |
+
q = embed.embedding(q.squeeze(-1).long())
|
545 |
+
ret.append(q)
|
546 |
+
ret = torch.cat(ret, -1)
|
547 |
+
quantized_out = quantized_out + ret
|
548 |
+
return quantized_out.transpose(1, 2) #N, C, T
|
inspiremusic/music_tokenizer/vqvae.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
|
15 |
+
import json
|
16 |
+
|
17 |
+
import torch
|
18 |
+
import torch.nn as nn
|
19 |
+
from inspiremusic.music_tokenizer.env import AttrDict
|
20 |
+
from inspiremusic.music_tokenizer.models import Encoder
|
21 |
+
from inspiremusic.music_tokenizer.models import Generator
|
22 |
+
from inspiremusic.music_tokenizer.models import Quantizer
|
23 |
+
|
24 |
+
|
25 |
+
class VQVAE(nn.Module):
|
26 |
+
def __init__(self,
|
27 |
+
config_path,
|
28 |
+
ckpt_path,
|
29 |
+
with_encoder=False):
|
30 |
+
super(VQVAE, self).__init__()
|
31 |
+
ckpt = torch.load(ckpt_path)
|
32 |
+
with open(config_path) as f:
|
33 |
+
data = f.read()
|
34 |
+
json_config = json.loads(data)
|
35 |
+
self.h = AttrDict(json_config)
|
36 |
+
self.quantizer = Quantizer(self.h)
|
37 |
+
self.generator = Generator(self.h)
|
38 |
+
self.generator.load_state_dict(ckpt['generator'])
|
39 |
+
self.quantizer.load_state_dict(ckpt['quantizer'])
|
40 |
+
if with_encoder:
|
41 |
+
self.encoder = Encoder(self.h)
|
42 |
+
self.encoder.load_state_dict(ckpt['encoder'])
|
43 |
+
|
44 |
+
def forward(self, x):
|
45 |
+
# x is the codebook
|
46 |
+
# x.shape (B, T, Nq)
|
47 |
+
quant_emb = self.quantizer.embed(x)
|
48 |
+
return self.generator(quant_emb)
|
49 |
+
|
50 |
+
def encode(self, x):
|
51 |
+
batch_size = x.size(0)
|
52 |
+
if len(x.shape) == 3 and x.shape[-1] == 1:
|
53 |
+
x = x.squeeze(-1)
|
54 |
+
c = self.encoder(x.unsqueeze(1))
|
55 |
+
q, loss_q, c = self.quantizer(c)
|
56 |
+
c = [code.reshape(batch_size, -1) for code in c]
|
57 |
+
# shape: [N, T, 4]
|
58 |
+
return torch.stack(c, -1)
|
inspiremusic/text/abs_tokenizer.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
|
15 |
+
from abc import ABC
|
16 |
+
from abc import abstractmethod
|
17 |
+
from typing import Iterable
|
18 |
+
from typing import List
|
19 |
+
|
20 |
+
|
21 |
+
class AbsTokenizer(ABC):
|
22 |
+
@abstractmethod
|
23 |
+
def text2tokens(self, line: str) -> List[str]:
|
24 |
+
raise NotImplementedError
|
25 |
+
|
26 |
+
@abstractmethod
|
27 |
+
def tokens2text(self, tokens: Iterable[str]) -> str:
|
28 |
+
raise NotImplementedError
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
def encode(self, line: str, **kwargs) -> List[str]:
|
33 |
+
|
34 |
+
return self.text2tokens(line)
|
inspiremusic/text/tokenizer.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
|
15 |
+
import copy
|
16 |
+
import os
|
17 |
+
import re
|
18 |
+
from typing import Iterable, List, Union
|
19 |
+
import numpy as np
|
20 |
+
import torch
|
21 |
+
|
22 |
+
from inspiremusic.text.abs_tokenizer import AbsTokenizer
|
23 |
+
from transformers import AutoTokenizer
|
24 |
+
|
25 |
+
def get_tokenizer(tokenizer_name, tokenizer_path):
|
26 |
+
if "qwen" in tokenizer_name:
|
27 |
+
return QwenTokenizer(tokenizer_path,skip_special_tokens=True)
|
28 |
+
else:
|
29 |
+
return None
|
30 |
+
|
31 |
+
class QwenTokenizer(AbsTokenizer):
|
32 |
+
def __init__(
|
33 |
+
self,
|
34 |
+
token_path: str,
|
35 |
+
skip_special_tokens: bool = True,
|
36 |
+
):
|
37 |
+
super().__init__()
|
38 |
+
# NOTE: non-chat model, all these special tokens keep randomly initialized.
|
39 |
+
special_tokens = {
|
40 |
+
'eos_token': '<|endoftext|>',
|
41 |
+
'pad_token': '<|endoftext|>',
|
42 |
+
'additional_special_tokens': [
|
43 |
+
'<|im_start|>', '<|im_end|>', '<|endofprompt|>',
|
44 |
+
'[breath]', '<strong>', '</strong>', '[noise]',
|
45 |
+
'[laughter]', '[cough]', '[clucking]', '[accent]',
|
46 |
+
'[quick_breath]',
|
47 |
+
]
|
48 |
+
}
|
49 |
+
self.tokenizer = AutoTokenizer.from_pretrained(token_path)
|
50 |
+
self.tokenizer.add_special_tokens(special_tokens)
|
51 |
+
self.skip_special_tokens = skip_special_tokens
|
52 |
+
|
53 |
+
def get_vocab_size(self):
|
54 |
+
return self.tokenizer.vocab_size
|
55 |
+
|
56 |
+
def text2tokens(self, line: str) -> List:
|
57 |
+
tokens = self.tokenizer([line], return_tensors="pt")
|
58 |
+
tokens = tokens["input_ids"][0].cpu().tolist()
|
59 |
+
return tokens
|
60 |
+
|
61 |
+
def tokens2text(self, tokens) -> str:
|
62 |
+
tokens = torch.tensor(tokens, dtype=torch.int64)
|
63 |
+
text = self.tokenizer.batch_decode([tokens], skip_special_tokens=self.skip_special_tokens)[0]
|
64 |
+
return text
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
def get_qwen_vocab_size(token_type: str):
|
69 |
+
if "qwen1.5" in token_type.lower() or "qwen2.0" in token_type.lower() or "qwen2.5" in token_type.lower():
|
70 |
+
# 293 for special and extra tokens, including endoftext, im_start, im_end, endofprompt and others in the future.
|
71 |
+
# model.vocab_size = 151936, tokenizer.vocab_size = 151643
|
72 |
+
# NOTE: the first three special tokens (endoftext, im_start, im_end) are trained in Chat series models,
|
73 |
+
# others are kept in random initialization state.
|
74 |
+
return 151643 + 293
|
75 |
+
else:
|
76 |
+
raise ValueError(f"Unknown tokenizer {token_type}")
|
inspiremusic/transformer/__init__.py
ADDED
File without changes
|
inspiremusic/transformer/activation.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2020 Johns Hopkins University (Shinji Watanabe)
|
2 |
+
# 2020 Northwestern Polytechnical University (Pengcheng Guo)
|
3 |
+
# 2020 Mobvoi Inc (Binbin Zhang)
|
4 |
+
# 2024 Alibaba Inc (Xiang Lyu)
|
5 |
+
#
|
6 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7 |
+
# you may not use this file except in compliance with the License.
|
8 |
+
# You may obtain a copy of the License at
|
9 |
+
#
|
10 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11 |
+
#
|
12 |
+
# Unless required by applicable law or agreed to in writing, software
|
13 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15 |
+
# See the License for the specific language governing permissions and
|
16 |
+
# limitations under the License.
|
17 |
+
"""Swish() activation function for Conformer."""
|
18 |
+
|
19 |
+
import torch
|
20 |
+
from torch import nn, sin, pow
|
21 |
+
from torch.nn import Parameter
|
22 |
+
|
23 |
+
|
24 |
+
class Swish(torch.nn.Module):
|
25 |
+
"""Construct an Swish object."""
|
26 |
+
|
27 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
28 |
+
"""Return Swish activation function."""
|
29 |
+
return x * torch.sigmoid(x)
|
30 |
+
|
31 |
+
|
32 |
+
# Implementation adapted from https://github.com/EdwardDixon/snake under the MIT license.
|
33 |
+
# LICENSE is in incl_licenses directory.
|
34 |
+
class Snake(nn.Module):
|
35 |
+
'''
|
36 |
+
Implementation of a sine-based periodic activation function
|
37 |
+
Shape:
|
38 |
+
- Input: (B, C, T)
|
39 |
+
- Output: (B, C, T), same shape as the input
|
40 |
+
Parameters:
|
41 |
+
- alpha - trainable parameter
|
42 |
+
References:
|
43 |
+
- This activation function is from this paper by Liu Ziyin, Tilman Hartwig, Masahito Ueda:
|
44 |
+
https://arxiv.org/abs/2006.08195
|
45 |
+
Examples:
|
46 |
+
>>> a1 = snake(256)
|
47 |
+
>>> x = torch.randn(256)
|
48 |
+
>>> x = a1(x)
|
49 |
+
'''
|
50 |
+
def __init__(self, in_features, alpha=1.0, alpha_trainable=True, alpha_logscale=False):
|
51 |
+
'''
|
52 |
+
Initialization.
|
53 |
+
INPUT:
|
54 |
+
- in_features: shape of the input
|
55 |
+
- alpha: trainable parameter
|
56 |
+
alpha is initialized to 1 by default, higher values = higher-frequency.
|
57 |
+
alpha will be trained along with the rest of your model.
|
58 |
+
'''
|
59 |
+
super(Snake, self).__init__()
|
60 |
+
self.in_features = in_features
|
61 |
+
|
62 |
+
# initialize alpha
|
63 |
+
self.alpha_logscale = alpha_logscale
|
64 |
+
if self.alpha_logscale: # log scale alphas initialized to zeros
|
65 |
+
self.alpha = Parameter(torch.zeros(in_features) * alpha)
|
66 |
+
else: # linear scale alphas initialized to ones
|
67 |
+
self.alpha = Parameter(torch.ones(in_features) * alpha)
|
68 |
+
|
69 |
+
self.alpha.requires_grad = alpha_trainable
|
70 |
+
|
71 |
+
self.no_div_by_zero = 0.000000001
|
72 |
+
|
73 |
+
def forward(self, x):
|
74 |
+
'''
|
75 |
+
Forward pass of the function.
|
76 |
+
Applies the function to the input elementwise.
|
77 |
+
Snake ∶= x + 1/a * sin^2 (xa)
|
78 |
+
'''
|
79 |
+
alpha = self.alpha.unsqueeze(0).unsqueeze(-1) # line up with x to [B, C, T]
|
80 |
+
if self.alpha_logscale:
|
81 |
+
alpha = torch.exp(alpha)
|
82 |
+
x = x + (1.0 / (alpha + self.no_div_by_zero)) * pow(sin(x * alpha), 2)
|
83 |
+
|
84 |
+
return x
|
inspiremusic/transformer/attention.py
ADDED
@@ -0,0 +1,328 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2019 Shigeki Karita
|
2 |
+
# 2020 Mobvoi Inc (Binbin Zhang)
|
3 |
+
# 2022 Xingchen Song ([email protected])
|
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 |
+
"""Multi-Head Attention layer definition."""
|
17 |
+
|
18 |
+
import math
|
19 |
+
from typing import Tuple
|
20 |
+
|
21 |
+
import torch
|
22 |
+
from torch import nn
|
23 |
+
|
24 |
+
|
25 |
+
class MultiHeadedAttention(nn.Module):
|
26 |
+
"""Multi-Head Attention layer.
|
27 |
+
|
28 |
+
Args:
|
29 |
+
n_head (int): The number of heads.
|
30 |
+
n_feat (int): The number of features.
|
31 |
+
dropout_rate (float): Dropout rate.
|
32 |
+
|
33 |
+
"""
|
34 |
+
|
35 |
+
def __init__(self,
|
36 |
+
n_head: int,
|
37 |
+
n_feat: int,
|
38 |
+
dropout_rate: float,
|
39 |
+
key_bias: bool = True):
|
40 |
+
"""Construct an MultiHeadedAttention object."""
|
41 |
+
super().__init__()
|
42 |
+
assert n_feat % n_head == 0
|
43 |
+
# We assume d_v always equals d_k
|
44 |
+
self.d_k = n_feat // n_head
|
45 |
+
self.h = n_head
|
46 |
+
self.linear_q = nn.Linear(n_feat, n_feat)
|
47 |
+
self.linear_k = nn.Linear(n_feat, n_feat, bias=key_bias)
|
48 |
+
self.linear_v = nn.Linear(n_feat, n_feat)
|
49 |
+
self.linear_out = nn.Linear(n_feat, n_feat)
|
50 |
+
self.dropout = nn.Dropout(p=dropout_rate)
|
51 |
+
|
52 |
+
def forward_qkv(
|
53 |
+
self, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor
|
54 |
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
55 |
+
"""Transform query, key and value.
|
56 |
+
|
57 |
+
Args:
|
58 |
+
query (torch.Tensor): Query tensor (#batch, time1, size).
|
59 |
+
key (torch.Tensor): Key tensor (#batch, time2, size).
|
60 |
+
value (torch.Tensor): Value tensor (#batch, time2, size).
|
61 |
+
|
62 |
+
Returns:
|
63 |
+
torch.Tensor: Transformed query tensor, size
|
64 |
+
(#batch, n_head, time1, d_k).
|
65 |
+
torch.Tensor: Transformed key tensor, size
|
66 |
+
(#batch, n_head, time2, d_k).
|
67 |
+
torch.Tensor: Transformed value tensor, size
|
68 |
+
(#batch, n_head, time2, d_k).
|
69 |
+
|
70 |
+
"""
|
71 |
+
n_batch = query.size(0)
|
72 |
+
q = self.linear_q(query).view(n_batch, -1, self.h, self.d_k)
|
73 |
+
k = self.linear_k(key).view(n_batch, -1, self.h, self.d_k)
|
74 |
+
v = self.linear_v(value).view(n_batch, -1, self.h, self.d_k)
|
75 |
+
q = q.transpose(1, 2) # (batch, head, time1, d_k)
|
76 |
+
k = k.transpose(1, 2) # (batch, head, time2, d_k)
|
77 |
+
v = v.transpose(1, 2) # (batch, head, time2, d_k)
|
78 |
+
|
79 |
+
return q, k, v
|
80 |
+
|
81 |
+
def forward_attention(
|
82 |
+
self,
|
83 |
+
value: torch.Tensor,
|
84 |
+
scores: torch.Tensor,
|
85 |
+
mask: torch.Tensor = torch.ones((0, 0, 0), dtype=torch.bool)
|
86 |
+
) -> torch.Tensor:
|
87 |
+
"""Compute attention context vector.
|
88 |
+
|
89 |
+
Args:
|
90 |
+
value (torch.Tensor): Transformed value, size
|
91 |
+
(#batch, n_head, time2, d_k).
|
92 |
+
scores (torch.Tensor): Attention score, size
|
93 |
+
(#batch, n_head, time1, time2).
|
94 |
+
mask (torch.Tensor): Mask, size (#batch, 1, time2) or
|
95 |
+
(#batch, time1, time2), (0, 0, 0) means fake mask.
|
96 |
+
|
97 |
+
Returns:
|
98 |
+
torch.Tensor: Transformed value (#batch, time1, d_model)
|
99 |
+
weighted by the attention score (#batch, time1, time2).
|
100 |
+
|
101 |
+
"""
|
102 |
+
n_batch = value.size(0)
|
103 |
+
# NOTE(xcsong): When will `if mask.size(2) > 0` be True?
|
104 |
+
# 1. onnx(16/4) [WHY? Because we feed real cache & real mask for the
|
105 |
+
# 1st chunk to ease the onnx export.]
|
106 |
+
# 2. pytorch training
|
107 |
+
if mask.size(2) > 0: # time2 > 0
|
108 |
+
mask = mask.unsqueeze(1).eq(0) # (batch, 1, *, time2)
|
109 |
+
# For last chunk, time2 might be larger than scores.size(-1)
|
110 |
+
mask = mask[:, :, :, :scores.size(-1)] # (batch, 1, *, time2)
|
111 |
+
scores = scores.masked_fill(mask, -float('inf'))
|
112 |
+
attn = torch.softmax(scores, dim=-1).masked_fill(
|
113 |
+
mask, 0.0) # (batch, head, time1, time2)
|
114 |
+
# NOTE(xcsong): When will `if mask.size(2) > 0` be False?
|
115 |
+
# 1. onnx(16/-1, -1/-1, 16/0)
|
116 |
+
# 2. jit (16/-1, -1/-1, 16/0, 16/4)
|
117 |
+
else:
|
118 |
+
attn = torch.softmax(scores, dim=-1) # (batch, head, time1, time2)
|
119 |
+
|
120 |
+
p_attn = self.dropout(attn)
|
121 |
+
x = torch.matmul(p_attn, value) # (batch, head, time1, d_k)
|
122 |
+
x = (x.transpose(1, 2).contiguous().view(n_batch, -1,
|
123 |
+
self.h * self.d_k)
|
124 |
+
) # (batch, time1, d_model)
|
125 |
+
|
126 |
+
return self.linear_out(x) # (batch, time1, d_model)
|
127 |
+
|
128 |
+
def forward(
|
129 |
+
self,
|
130 |
+
query: torch.Tensor,
|
131 |
+
key: torch.Tensor,
|
132 |
+
value: torch.Tensor,
|
133 |
+
mask: torch.Tensor = torch.ones((0, 0, 0), dtype=torch.bool),
|
134 |
+
pos_emb: torch.Tensor = torch.empty(0),
|
135 |
+
cache: torch.Tensor = torch.zeros((0, 0, 0, 0))
|
136 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
137 |
+
"""Compute scaled dot product attention.
|
138 |
+
|
139 |
+
Args:
|
140 |
+
query (torch.Tensor): Query tensor (#batch, time1, size).
|
141 |
+
key (torch.Tensor): Key tensor (#batch, time2, size).
|
142 |
+
value (torch.Tensor): Value tensor (#batch, time2, size).
|
143 |
+
mask (torch.Tensor): Mask tensor (#batch, 1, time2) or
|
144 |
+
(#batch, time1, time2).
|
145 |
+
1.When applying cross attention between decoder and encoder,
|
146 |
+
the batch padding mask for input is in (#batch, 1, T) shape.
|
147 |
+
2.When applying self attention of encoder,
|
148 |
+
the mask is in (#batch, T, T) shape.
|
149 |
+
3.When applying self attention of decoder,
|
150 |
+
the mask is in (#batch, L, L) shape.
|
151 |
+
4.If the different position in decoder see different block
|
152 |
+
of the encoder, such as Mocha, the passed in mask could be
|
153 |
+
in (#batch, L, T) shape. But there is no such case in current
|
154 |
+
InspireMusic.
|
155 |
+
cache (torch.Tensor): Cache tensor (1, head, cache_t, d_k * 2),
|
156 |
+
where `cache_t == chunk_size * num_decoding_left_chunks`
|
157 |
+
and `head * d_k == size`
|
158 |
+
|
159 |
+
|
160 |
+
Returns:
|
161 |
+
torch.Tensor: Output tensor (#batch, time1, d_model).
|
162 |
+
torch.Tensor: Cache tensor (1, head, cache_t + time1, d_k * 2)
|
163 |
+
where `cache_t == chunk_size * num_decoding_left_chunks`
|
164 |
+
and `head * d_k == size`
|
165 |
+
|
166 |
+
"""
|
167 |
+
q, k, v = self.forward_qkv(query, key, value)
|
168 |
+
|
169 |
+
# NOTE(xcsong):
|
170 |
+
# when export onnx model, for 1st chunk, we feed
|
171 |
+
# cache(1, head, 0, d_k * 2) (16/-1, -1/-1, 16/0 mode)
|
172 |
+
# or cache(1, head, real_cache_t, d_k * 2) (16/4 mode).
|
173 |
+
# In all modes, `if cache.size(0) > 0` will alwayse be `True`
|
174 |
+
# and we will always do splitting and
|
175 |
+
# concatnation(this will simplify onnx export). Note that
|
176 |
+
# it's OK to concat & split zero-shaped tensors(see code below).
|
177 |
+
# when export jit model, for 1st chunk, we always feed
|
178 |
+
# cache(0, 0, 0, 0) since jit supports dynamic if-branch.
|
179 |
+
# >>> a = torch.ones((1, 2, 0, 4))
|
180 |
+
# >>> b = torch.ones((1, 2, 3, 4))
|
181 |
+
# >>> c = torch.cat((a, b), dim=2)
|
182 |
+
# >>> torch.equal(b, c) # True
|
183 |
+
# >>> d = torch.split(a, 2, dim=-1)
|
184 |
+
# >>> torch.equal(d[0], d[1]) # True
|
185 |
+
if cache.size(0) > 0:
|
186 |
+
key_cache, value_cache = torch.split(cache,
|
187 |
+
cache.size(-1) // 2,
|
188 |
+
dim=-1)
|
189 |
+
k = torch.cat([key_cache, k], dim=2)
|
190 |
+
v = torch.cat([value_cache, v], dim=2)
|
191 |
+
|
192 |
+
# NOTE(xcsong): We do cache slicing in encoder.forward_chunk, since it's
|
193 |
+
# non-trivial to calculate `next_cache_start` here.
|
194 |
+
new_cache = torch.cat((k, v), dim=-1)
|
195 |
+
|
196 |
+
scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(self.d_k)
|
197 |
+
return self.forward_attention(v, scores, mask), new_cache
|
198 |
+
|
199 |
+
|
200 |
+
class RelPositionMultiHeadedAttention(MultiHeadedAttention):
|
201 |
+
"""Multi-Head Attention layer with relative position encoding.
|
202 |
+
Paper: https://arxiv.org/abs/1901.02860
|
203 |
+
Args:
|
204 |
+
n_head (int): The number of heads.
|
205 |
+
n_feat (int): The number of features.
|
206 |
+
dropout_rate (float): Dropout rate.
|
207 |
+
"""
|
208 |
+
|
209 |
+
def __init__(self,
|
210 |
+
n_head: int,
|
211 |
+
n_feat: int,
|
212 |
+
dropout_rate: float,
|
213 |
+
key_bias: bool = True):
|
214 |
+
"""Construct an RelPositionMultiHeadedAttention object."""
|
215 |
+
super().__init__(n_head, n_feat, dropout_rate, key_bias)
|
216 |
+
# linear transformation for positional encoding
|
217 |
+
self.linear_pos = nn.Linear(n_feat, n_feat, bias=False)
|
218 |
+
# these two learnable bias are used in matrix c and matrix d
|
219 |
+
# as described in https://arxiv.org/abs/1901.02860 Section 3.3
|
220 |
+
self.pos_bias_u = nn.Parameter(torch.Tensor(self.h, self.d_k))
|
221 |
+
self.pos_bias_v = nn.Parameter(torch.Tensor(self.h, self.d_k))
|
222 |
+
torch.nn.init.xavier_uniform_(self.pos_bias_u)
|
223 |
+
torch.nn.init.xavier_uniform_(self.pos_bias_v)
|
224 |
+
|
225 |
+
def rel_shift(self, x: torch.Tensor) -> torch.Tensor:
|
226 |
+
"""Compute relative positional encoding.
|
227 |
+
|
228 |
+
Args:
|
229 |
+
x (torch.Tensor): Input tensor (batch, head, time1, 2*time1-1).
|
230 |
+
time1 means the length of query vector.
|
231 |
+
|
232 |
+
Returns:
|
233 |
+
torch.Tensor: Output tensor.
|
234 |
+
|
235 |
+
"""
|
236 |
+
zero_pad = torch.zeros((x.size()[0], x.size()[1], x.size()[2], 1),
|
237 |
+
device=x.device,
|
238 |
+
dtype=x.dtype)
|
239 |
+
x_padded = torch.cat([zero_pad, x], dim=-1)
|
240 |
+
|
241 |
+
x_padded = x_padded.view(x.size()[0],
|
242 |
+
x.size()[1],
|
243 |
+
x.size(3) + 1, x.size(2))
|
244 |
+
x = x_padded[:, :, 1:].view_as(x)[
|
245 |
+
:, :, :, : x.size(-1) // 2 + 1
|
246 |
+
] # only keep the positions from 0 to time2
|
247 |
+
return x
|
248 |
+
|
249 |
+
def forward(
|
250 |
+
self,
|
251 |
+
query: torch.Tensor,
|
252 |
+
key: torch.Tensor,
|
253 |
+
value: torch.Tensor,
|
254 |
+
mask: torch.Tensor = torch.ones((0, 0, 0), dtype=torch.bool),
|
255 |
+
pos_emb: torch.Tensor = torch.empty(0),
|
256 |
+
cache: torch.Tensor = torch.zeros((0, 0, 0, 0))
|
257 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
258 |
+
"""Compute 'Scaled Dot Product Attention' with rel. positional encoding.
|
259 |
+
Args:
|
260 |
+
query (torch.Tensor): Query tensor (#batch, time1, size).
|
261 |
+
key (torch.Tensor): Key tensor (#batch, time2, size).
|
262 |
+
value (torch.Tensor): Value tensor (#batch, time2, size).
|
263 |
+
mask (torch.Tensor): Mask tensor (#batch, 1, time2) or
|
264 |
+
(#batch, time1, time2), (0, 0, 0) means fake mask.
|
265 |
+
pos_emb (torch.Tensor): Positional embedding tensor
|
266 |
+
(#batch, time2, size).
|
267 |
+
cache (torch.Tensor): Cache tensor (1, head, cache_t, d_k * 2),
|
268 |
+
where `cache_t == chunk_size * num_decoding_left_chunks`
|
269 |
+
and `head * d_k == size`
|
270 |
+
Returns:
|
271 |
+
torch.Tensor: Output tensor (#batch, time1, d_model).
|
272 |
+
torch.Tensor: Cache tensor (1, head, cache_t + time1, d_k * 2)
|
273 |
+
where `cache_t == chunk_size * num_decoding_left_chunks`
|
274 |
+
and `head * d_k == size`
|
275 |
+
"""
|
276 |
+
q, k, v = self.forward_qkv(query, key, value)
|
277 |
+
q = q.transpose(1, 2) # (batch, time1, head, d_k)
|
278 |
+
|
279 |
+
# NOTE(xcsong):
|
280 |
+
# when export onnx model, for 1st chunk, we feed
|
281 |
+
# cache(1, head, 0, d_k * 2) (16/-1, -1/-1, 16/0 mode)
|
282 |
+
# or cache(1, head, real_cache_t, d_k * 2) (16/4 mode).
|
283 |
+
# In all modes, `if cache.size(0) > 0` will alwayse be `True`
|
284 |
+
# and we will always do splitting and
|
285 |
+
# concatnation(this will simplify onnx export). Note that
|
286 |
+
# it's OK to concat & split zero-shaped tensors(see code below).
|
287 |
+
# when export jit model, for 1st chunk, we always feed
|
288 |
+
# cache(0, 0, 0, 0) since jit supports dynamic if-branch.
|
289 |
+
# >>> a = torch.ones((1, 2, 0, 4))
|
290 |
+
# >>> b = torch.ones((1, 2, 3, 4))
|
291 |
+
# >>> c = torch.cat((a, b), dim=2)
|
292 |
+
# >>> torch.equal(b, c) # True
|
293 |
+
# >>> d = torch.split(a, 2, dim=-1)
|
294 |
+
# >>> torch.equal(d[0], d[1]) # True
|
295 |
+
if cache.size(0) > 0:
|
296 |
+
key_cache, value_cache = torch.split(cache,
|
297 |
+
cache.size(-1) // 2,
|
298 |
+
dim=-1)
|
299 |
+
k = torch.cat([key_cache, k], dim=2)
|
300 |
+
v = torch.cat([value_cache, v], dim=2)
|
301 |
+
# NOTE(xcsong): We do cache slicing in encoder.forward_chunk, since it's
|
302 |
+
# non-trivial to calculate `next_cache_start` here.
|
303 |
+
new_cache = torch.cat((k, v), dim=-1)
|
304 |
+
|
305 |
+
n_batch_pos = pos_emb.size(0)
|
306 |
+
p = self.linear_pos(pos_emb).view(n_batch_pos, -1, self.h, self.d_k)
|
307 |
+
p = p.transpose(1, 2) # (batch, head, time1, d_k)
|
308 |
+
|
309 |
+
# (batch, head, time1, d_k)
|
310 |
+
q_with_bias_u = (q + self.pos_bias_u).transpose(1, 2)
|
311 |
+
# (batch, head, time1, d_k)
|
312 |
+
q_with_bias_v = (q + self.pos_bias_v).transpose(1, 2)
|
313 |
+
|
314 |
+
# compute attention score
|
315 |
+
# first compute matrix a and matrix c
|
316 |
+
# as described in https://arxiv.org/abs/1901.02860 Section 3.3
|
317 |
+
# (batch, head, time1, time2)
|
318 |
+
matrix_ac = torch.matmul(q_with_bias_u, k.transpose(-2, -1))
|
319 |
+
# compute matrix b and matrix d
|
320 |
+
# (batch, head, time1, time2)
|
321 |
+
matrix_bd = torch.matmul(q_with_bias_v, p.transpose(-2, -1))
|
322 |
+
# NOTE(Xiang Lyu): Keep rel_shift since espnet rel_pos_emb is used
|
323 |
+
if matrix_ac.shape != matrix_bd.shape:
|
324 |
+
matrix_bd = self.rel_shift(matrix_bd)
|
325 |
+
|
326 |
+
scores = (matrix_ac + matrix_bd) / math.sqrt(self.d_k) # (batch, head, time1, time2)
|
327 |
+
|
328 |
+
return self.forward_attention(v, scores, mask), new_cache
|
inspiremusic/transformer/convolution.py
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2020 Mobvoi Inc. (authors: Binbin Zhang, Di Wu)
|
2 |
+
# 2024 Alibaba Inc (Xiang Lyu)
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
# Modified from ESPnet(https://github.com/espnet/espnet)
|
16 |
+
"""ConvolutionModule definition."""
|
17 |
+
|
18 |
+
from typing import Tuple
|
19 |
+
|
20 |
+
import torch
|
21 |
+
from torch import nn
|
22 |
+
|
23 |
+
|
24 |
+
class ConvolutionModule(nn.Module):
|
25 |
+
"""ConvolutionModule in Conformer model."""
|
26 |
+
|
27 |
+
def __init__(self,
|
28 |
+
channels: int,
|
29 |
+
kernel_size: int = 15,
|
30 |
+
activation: nn.Module = nn.ReLU(),
|
31 |
+
norm: str = "batch_norm",
|
32 |
+
causal: bool = False,
|
33 |
+
bias: bool = True):
|
34 |
+
"""Construct an ConvolutionModule object.
|
35 |
+
Args:
|
36 |
+
channels (int): The number of channels of conv layers.
|
37 |
+
kernel_size (int): Kernel size of conv layers.
|
38 |
+
causal (int): Whether use causal convolution or not
|
39 |
+
"""
|
40 |
+
super().__init__()
|
41 |
+
|
42 |
+
self.pointwise_conv1 = nn.Conv1d(
|
43 |
+
channels,
|
44 |
+
2 * channels,
|
45 |
+
kernel_size=1,
|
46 |
+
stride=1,
|
47 |
+
padding=0,
|
48 |
+
bias=bias,
|
49 |
+
)
|
50 |
+
# self.lorder is used to distinguish if it's a causal convolution,
|
51 |
+
# if self.lorder > 0: it's a causal convolution, the input will be
|
52 |
+
# padded with self.lorder frames on the left in forward.
|
53 |
+
# else: it's a symmetrical convolution
|
54 |
+
if causal:
|
55 |
+
padding = 0
|
56 |
+
self.lorder = kernel_size - 1
|
57 |
+
else:
|
58 |
+
# kernel_size should be an odd number for none causal convolution
|
59 |
+
assert (kernel_size - 1) % 2 == 0
|
60 |
+
padding = (kernel_size - 1) // 2
|
61 |
+
self.lorder = 0
|
62 |
+
self.depthwise_conv = nn.Conv1d(
|
63 |
+
channels,
|
64 |
+
channels,
|
65 |
+
kernel_size,
|
66 |
+
stride=1,
|
67 |
+
padding=padding,
|
68 |
+
groups=channels,
|
69 |
+
bias=bias,
|
70 |
+
)
|
71 |
+
|
72 |
+
assert norm in ['batch_norm', 'layer_norm']
|
73 |
+
if norm == "batch_norm":
|
74 |
+
self.use_layer_norm = False
|
75 |
+
self.norm = nn.BatchNorm1d(channels)
|
76 |
+
else:
|
77 |
+
self.use_layer_norm = True
|
78 |
+
self.norm = nn.LayerNorm(channels)
|
79 |
+
|
80 |
+
self.pointwise_conv2 = nn.Conv1d(
|
81 |
+
channels,
|
82 |
+
channels,
|
83 |
+
kernel_size=1,
|
84 |
+
stride=1,
|
85 |
+
padding=0,
|
86 |
+
bias=bias,
|
87 |
+
)
|
88 |
+
self.activation = activation
|
89 |
+
|
90 |
+
def forward(
|
91 |
+
self,
|
92 |
+
x: torch.Tensor,
|
93 |
+
mask_pad: torch.Tensor = torch.ones((0, 0, 0), dtype=torch.bool),
|
94 |
+
cache: torch.Tensor = torch.zeros((0, 0, 0)),
|
95 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
96 |
+
"""Compute convolution module.
|
97 |
+
Args:
|
98 |
+
x (torch.Tensor): Input tensor (#batch, time, channels).
|
99 |
+
mask_pad (torch.Tensor): used for batch padding (#batch, 1, time),
|
100 |
+
(0, 0, 0) means fake mask.
|
101 |
+
cache (torch.Tensor): left context cache, it is only
|
102 |
+
used in causal convolution (#batch, channels, cache_t),
|
103 |
+
(0, 0, 0) meas fake cache.
|
104 |
+
Returns:
|
105 |
+
torch.Tensor: Output tensor (#batch, time, channels).
|
106 |
+
"""
|
107 |
+
# exchange the temporal dimension and the feature dimension
|
108 |
+
x = x.transpose(1, 2) # (#batch, channels, time)
|
109 |
+
|
110 |
+
# mask batch padding
|
111 |
+
if mask_pad.size(2) > 0: # time > 0
|
112 |
+
x.masked_fill_(~mask_pad, 0.0)
|
113 |
+
|
114 |
+
if self.lorder > 0:
|
115 |
+
if cache.size(2) == 0: # cache_t == 0
|
116 |
+
x = nn.functional.pad(x, (self.lorder, 0), 'constant', 0.0)
|
117 |
+
else:
|
118 |
+
assert cache.size(0) == x.size(0) # equal batch
|
119 |
+
assert cache.size(1) == x.size(1) # equal channel
|
120 |
+
x = torch.cat((cache, x), dim=2)
|
121 |
+
assert (x.size(2) > self.lorder)
|
122 |
+
new_cache = x[:, :, -self.lorder:]
|
123 |
+
else:
|
124 |
+
# It's better we just return None if no cache is required,
|
125 |
+
# However, for JIT export, here we just fake one tensor instead of
|
126 |
+
# None.
|
127 |
+
new_cache = torch.zeros((0, 0, 0), dtype=x.dtype, device=x.device)
|
128 |
+
|
129 |
+
# GLU mechanism
|
130 |
+
x = self.pointwise_conv1(x) # (batch, 2*channel, dim)
|
131 |
+
x = nn.functional.glu(x, dim=1) # (batch, channel, dim)
|
132 |
+
|
133 |
+
# 1D Depthwise Conv
|
134 |
+
x = self.depthwise_conv(x)
|
135 |
+
if self.use_layer_norm:
|
136 |
+
x = x.transpose(1, 2)
|
137 |
+
x = self.activation(self.norm(x))
|
138 |
+
if self.use_layer_norm:
|
139 |
+
x = x.transpose(1, 2)
|
140 |
+
x = self.pointwise_conv2(x)
|
141 |
+
# mask batch padding
|
142 |
+
if mask_pad.size(2) > 0: # time > 0
|
143 |
+
x.masked_fill_(~mask_pad, 0.0)
|
144 |
+
|
145 |
+
return x.transpose(1, 2), new_cache
|
inspiremusic/transformer/decoder.py
ADDED
@@ -0,0 +1,396 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2021 Mobvoi Inc. (authors: Binbin Zhang, Di Wu)
|
2 |
+
# 2024 Alibaba Inc (Xiang Lyu)
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
# Modified from ESPnet(https://github.com/espnet/espnet)
|
16 |
+
"""Decoder definition."""
|
17 |
+
from typing import Tuple, List, Optional
|
18 |
+
|
19 |
+
import torch
|
20 |
+
import torch.utils.checkpoint as ckpt
|
21 |
+
import logging
|
22 |
+
|
23 |
+
from inspiremusic.transformer.decoder_layer import DecoderLayer
|
24 |
+
from inspiremusic.transformer.positionwise_feed_forward import PositionwiseFeedForward
|
25 |
+
from inspiremusic.utils.class_utils import (
|
26 |
+
INSPIREMUSIC_EMB_CLASSES,
|
27 |
+
INSPIREMUSIC_ATTENTION_CLASSES,
|
28 |
+
INSPIREMUSIC_ACTIVATION_CLASSES,
|
29 |
+
)
|
30 |
+
from inspiremusic.utils.mask import (subsequent_mask, make_pad_mask)
|
31 |
+
|
32 |
+
|
33 |
+
class TransformerDecoder(torch.nn.Module):
|
34 |
+
"""Base class of Transfomer decoder module.
|
35 |
+
Args:
|
36 |
+
vocab_size: output dim
|
37 |
+
encoder_output_size: dimension of attention
|
38 |
+
attention_heads: the number of heads of multi head attention
|
39 |
+
linear_units: the hidden units number of position-wise feedforward
|
40 |
+
num_blocks: the number of decoder blocks
|
41 |
+
dropout_rate: dropout rate
|
42 |
+
self_attention_dropout_rate: dropout rate for attention
|
43 |
+
input_layer: input layer type
|
44 |
+
use_output_layer: whether to use output layer
|
45 |
+
pos_enc_class: PositionalEncoding or ScaledPositionalEncoding
|
46 |
+
normalize_before:
|
47 |
+
True: use layer_norm before each sub-block of a layer.
|
48 |
+
False: use layer_norm after each sub-block of a layer.
|
49 |
+
src_attention: if false, encoder-decoder cross attention is not
|
50 |
+
applied, such as CIF model
|
51 |
+
key_bias: whether use bias in attention.linear_k, False for whisper models.
|
52 |
+
gradient_checkpointing: rerunning a forward-pass segment for each
|
53 |
+
checkpointed segment during backward.
|
54 |
+
tie_word_embedding: Tie or clone module weights depending of whether we are
|
55 |
+
using TorchScript or not
|
56 |
+
"""
|
57 |
+
|
58 |
+
def __init__(
|
59 |
+
self,
|
60 |
+
vocab_size: int,
|
61 |
+
encoder_output_size: int,
|
62 |
+
attention_heads: int = 4,
|
63 |
+
linear_units: int = 2048,
|
64 |
+
num_blocks: int = 6,
|
65 |
+
dropout_rate: float = 0.1,
|
66 |
+
positional_dropout_rate: float = 0.1,
|
67 |
+
self_attention_dropout_rate: float = 0.0,
|
68 |
+
src_attention_dropout_rate: float = 0.0,
|
69 |
+
input_layer: str = "embed",
|
70 |
+
use_output_layer: bool = True,
|
71 |
+
normalize_before: bool = True,
|
72 |
+
src_attention: bool = True,
|
73 |
+
key_bias: bool = True,
|
74 |
+
activation_type: str = "relu",
|
75 |
+
gradient_checkpointing: bool = False,
|
76 |
+
tie_word_embedding: bool = False,
|
77 |
+
):
|
78 |
+
super().__init__()
|
79 |
+
attention_dim = encoder_output_size
|
80 |
+
activation = INSPIREMUSIC_ACTIVATION_CLASSES[activation_type]()
|
81 |
+
|
82 |
+
self.embed = torch.nn.Sequential(
|
83 |
+
torch.nn.Identity() if input_layer == "no_pos" else
|
84 |
+
torch.nn.Embedding(vocab_size, attention_dim),
|
85 |
+
INSPIREMUSIC_EMB_CLASSES[input_layer](attention_dim,
|
86 |
+
positional_dropout_rate),
|
87 |
+
)
|
88 |
+
|
89 |
+
self.normalize_before = normalize_before
|
90 |
+
self.after_norm = torch.nn.LayerNorm(attention_dim, eps=1e-5)
|
91 |
+
self.use_output_layer = use_output_layer
|
92 |
+
if use_output_layer:
|
93 |
+
self.output_layer = torch.nn.Linear(attention_dim, vocab_size)
|
94 |
+
else:
|
95 |
+
self.output_layer = torch.nn.Identity()
|
96 |
+
self.num_blocks = num_blocks
|
97 |
+
self.decoders = torch.nn.ModuleList([
|
98 |
+
DecoderLayer(
|
99 |
+
attention_dim,
|
100 |
+
INSPIREMUSIC_ATTENTION_CLASSES["selfattn"](
|
101 |
+
attention_heads, attention_dim,
|
102 |
+
self_attention_dropout_rate, key_bias),
|
103 |
+
INSPIREMUSIC_ATTENTION_CLASSES["selfattn"](
|
104 |
+
attention_heads, attention_dim, src_attention_dropout_rate,
|
105 |
+
key_bias) if src_attention else None,
|
106 |
+
PositionwiseFeedForward(attention_dim, linear_units,
|
107 |
+
dropout_rate, activation),
|
108 |
+
dropout_rate,
|
109 |
+
normalize_before,
|
110 |
+
) for _ in range(self.num_blocks)
|
111 |
+
])
|
112 |
+
|
113 |
+
self.gradient_checkpointing = gradient_checkpointing
|
114 |
+
self.tie_word_embedding = tie_word_embedding
|
115 |
+
|
116 |
+
def forward(
|
117 |
+
self,
|
118 |
+
memory: torch.Tensor,
|
119 |
+
memory_mask: torch.Tensor,
|
120 |
+
ys_in_pad: torch.Tensor,
|
121 |
+
ys_in_lens: torch.Tensor,
|
122 |
+
r_ys_in_pad: torch.Tensor = torch.empty(0),
|
123 |
+
reverse_weight: float = 0.0,
|
124 |
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
125 |
+
"""Forward decoder.
|
126 |
+
Args:
|
127 |
+
memory: encoded memory, float32 (batch, maxlen_in, feat)
|
128 |
+
memory_mask: encoder memory mask, (batch, 1, maxlen_in)
|
129 |
+
ys_in_pad: padded input token ids, int64 (batch, maxlen_out)
|
130 |
+
ys_in_lens: input lengths of this batch (batch)
|
131 |
+
r_ys_in_pad: not used in transformer decoder, in order to unify api
|
132 |
+
with bidirectional decoder
|
133 |
+
reverse_weight: not used in transformer decoder, in order to unify
|
134 |
+
api with bidirectional decode
|
135 |
+
Returns:
|
136 |
+
(tuple): tuple containing:
|
137 |
+
x: decoded token score before softmax (batch, maxlen_out,
|
138 |
+
vocab_size) if use_output_layer is True,
|
139 |
+
torch.tensor(0.0), in order to unify api with bidirectional decoder
|
140 |
+
olens: (batch, )
|
141 |
+
NOTE(xcsong):
|
142 |
+
We pass the `__call__` method of the modules instead of `forward` to the
|
143 |
+
checkpointing API because `__call__` attaches all the hooks of the module.
|
144 |
+
https://discuss.pytorch.org/t/any-different-between-model-input-and-model-forward-input/3690/2
|
145 |
+
"""
|
146 |
+
tgt = ys_in_pad
|
147 |
+
maxlen = tgt.size(1)
|
148 |
+
# tgt_mask: (B, 1, L)
|
149 |
+
tgt_mask = ~make_pad_mask(ys_in_lens, maxlen).unsqueeze(1)
|
150 |
+
tgt_mask = tgt_mask.to(tgt.device)
|
151 |
+
# m: (1, L, L)
|
152 |
+
m = subsequent_mask(tgt_mask.size(-1),
|
153 |
+
device=tgt_mask.device).unsqueeze(0)
|
154 |
+
# tgt_mask: (B, L, L)
|
155 |
+
tgt_mask = tgt_mask & m
|
156 |
+
x, _ = self.embed(tgt)
|
157 |
+
if self.gradient_checkpointing and self.training:
|
158 |
+
x = self.forward_layers_checkpointed(x, tgt_mask, memory,
|
159 |
+
memory_mask)
|
160 |
+
else:
|
161 |
+
x = self.forward_layers(x, tgt_mask, memory, memory_mask)
|
162 |
+
if self.normalize_before:
|
163 |
+
x = self.after_norm(x)
|
164 |
+
if self.use_output_layer:
|
165 |
+
x = self.output_layer(x)
|
166 |
+
olens = tgt_mask.sum(1)
|
167 |
+
return x, torch.tensor(0.0), olens
|
168 |
+
|
169 |
+
def forward_layers(self, x: torch.Tensor, tgt_mask: torch.Tensor,
|
170 |
+
memory: torch.Tensor,
|
171 |
+
memory_mask: torch.Tensor) -> torch.Tensor:
|
172 |
+
for layer in self.decoders:
|
173 |
+
x, tgt_mask, memory, memory_mask = layer(x, tgt_mask, memory,
|
174 |
+
memory_mask)
|
175 |
+
return x
|
176 |
+
|
177 |
+
@torch.jit.unused
|
178 |
+
def forward_layers_checkpointed(self, x: torch.Tensor,
|
179 |
+
tgt_mask: torch.Tensor,
|
180 |
+
memory: torch.Tensor,
|
181 |
+
memory_mask: torch.Tensor) -> torch.Tensor:
|
182 |
+
for layer in self.decoders:
|
183 |
+
x, tgt_mask, memory, memory_mask = ckpt.checkpoint(
|
184 |
+
layer.__call__, x, tgt_mask, memory, memory_mask)
|
185 |
+
return x
|
186 |
+
|
187 |
+
def forward_one_step(
|
188 |
+
self,
|
189 |
+
memory: torch.Tensor,
|
190 |
+
memory_mask: torch.Tensor,
|
191 |
+
tgt: torch.Tensor,
|
192 |
+
tgt_mask: torch.Tensor,
|
193 |
+
cache: Optional[List[torch.Tensor]] = None,
|
194 |
+
) -> Tuple[torch.Tensor, List[torch.Tensor]]:
|
195 |
+
"""Forward one step.
|
196 |
+
This is only used for decoding.
|
197 |
+
Args:
|
198 |
+
memory: encoded memory, float32 (batch, maxlen_in, feat)
|
199 |
+
memory_mask: encoded memory mask, (batch, 1, maxlen_in)
|
200 |
+
tgt: input token ids, int64 (batch, maxlen_out)
|
201 |
+
tgt_mask: input token mask, (batch, maxlen_out)
|
202 |
+
dtype=torch.uint8 in PyTorch 1.2-
|
203 |
+
dtype=torch.bool in PyTorch 1.2+ (include 1.2)
|
204 |
+
cache: cached output list of (batch, max_time_out-1, size)
|
205 |
+
Returns:
|
206 |
+
y, cache: NN output value and cache per `self.decoders`.
|
207 |
+
y.shape` is (batch, maxlen_out, token)
|
208 |
+
"""
|
209 |
+
x, _ = self.embed(tgt)
|
210 |
+
new_cache = []
|
211 |
+
for i, decoder in enumerate(self.decoders):
|
212 |
+
if cache is None:
|
213 |
+
c = None
|
214 |
+
else:
|
215 |
+
c = cache[i]
|
216 |
+
x, tgt_mask, memory, memory_mask = decoder(x,
|
217 |
+
tgt_mask,
|
218 |
+
memory,
|
219 |
+
memory_mask,
|
220 |
+
cache=c)
|
221 |
+
new_cache.append(x)
|
222 |
+
if self.normalize_before:
|
223 |
+
y = self.after_norm(x[:, -1])
|
224 |
+
else:
|
225 |
+
y = x[:, -1]
|
226 |
+
if self.use_output_layer:
|
227 |
+
y = torch.log_softmax(self.output_layer(y), dim=-1)
|
228 |
+
return y, new_cache
|
229 |
+
|
230 |
+
def tie_or_clone_weights(self, jit_mode: bool = True):
|
231 |
+
"""Tie or clone module weights (between word_emb and output_layer)
|
232 |
+
depending of whether we are using TorchScript or not"""
|
233 |
+
if not self.use_output_layer:
|
234 |
+
return
|
235 |
+
if jit_mode:
|
236 |
+
logging.info("clone emb.weight to output.weight")
|
237 |
+
self.output_layer.weight = torch.nn.Parameter(
|
238 |
+
self.embed[0].weight.clone())
|
239 |
+
else:
|
240 |
+
logging.info("tie emb.weight with output.weight")
|
241 |
+
self.output_layer.weight = self.embed[0].weight
|
242 |
+
|
243 |
+
if getattr(self.output_layer, "bias", None) is not None:
|
244 |
+
self.output_layer.bias.data = torch.nn.functional.pad(
|
245 |
+
self.output_layer.bias.data,
|
246 |
+
(
|
247 |
+
0,
|
248 |
+
self.output_layer.weight.shape[0] -
|
249 |
+
self.output_layer.bias.shape[0],
|
250 |
+
),
|
251 |
+
"constant",
|
252 |
+
0,
|
253 |
+
)
|
254 |
+
|
255 |
+
|
256 |
+
class BiTransformerDecoder(torch.nn.Module):
|
257 |
+
"""Base class of Transfomer decoder module.
|
258 |
+
Args:
|
259 |
+
vocab_size: output dim
|
260 |
+
encoder_output_size: dimension of attention
|
261 |
+
attention_heads: the number of heads of multi head attention
|
262 |
+
linear_units: the hidden units number of position-wise feedforward
|
263 |
+
num_blocks: the number of decoder blocks
|
264 |
+
r_num_blocks: the number of right to left decoder blocks
|
265 |
+
dropout_rate: dropout rate
|
266 |
+
self_attention_dropout_rate: dropout rate for attention
|
267 |
+
input_layer: input layer type
|
268 |
+
use_output_layer: whether to use output layer
|
269 |
+
pos_enc_class: PositionalEncoding or ScaledPositionalEncoding
|
270 |
+
normalize_before:
|
271 |
+
True: use layer_norm before each sub-block of a layer.
|
272 |
+
False: use layer_norm after each sub-block of a layer.
|
273 |
+
key_bias: whether use bias in attention.linear_k, False for whisper models.
|
274 |
+
"""
|
275 |
+
|
276 |
+
def __init__(
|
277 |
+
self,
|
278 |
+
vocab_size: int,
|
279 |
+
encoder_output_size: int,
|
280 |
+
attention_heads: int = 4,
|
281 |
+
linear_units: int = 2048,
|
282 |
+
num_blocks: int = 6,
|
283 |
+
r_num_blocks: int = 0,
|
284 |
+
dropout_rate: float = 0.1,
|
285 |
+
positional_dropout_rate: float = 0.1,
|
286 |
+
self_attention_dropout_rate: float = 0.0,
|
287 |
+
src_attention_dropout_rate: float = 0.0,
|
288 |
+
input_layer: str = "embed",
|
289 |
+
use_output_layer: bool = True,
|
290 |
+
normalize_before: bool = True,
|
291 |
+
key_bias: bool = True,
|
292 |
+
gradient_checkpointing: bool = False,
|
293 |
+
tie_word_embedding: bool = False,
|
294 |
+
):
|
295 |
+
|
296 |
+
super().__init__()
|
297 |
+
self.tie_word_embedding = tie_word_embedding
|
298 |
+
self.left_decoder = TransformerDecoder(
|
299 |
+
vocab_size,
|
300 |
+
encoder_output_size,
|
301 |
+
attention_heads,
|
302 |
+
linear_units,
|
303 |
+
num_blocks,
|
304 |
+
dropout_rate,
|
305 |
+
positional_dropout_rate,
|
306 |
+
self_attention_dropout_rate,
|
307 |
+
src_attention_dropout_rate,
|
308 |
+
input_layer,
|
309 |
+
use_output_layer,
|
310 |
+
normalize_before,
|
311 |
+
key_bias=key_bias,
|
312 |
+
gradient_checkpointing=gradient_checkpointing,
|
313 |
+
tie_word_embedding=tie_word_embedding)
|
314 |
+
|
315 |
+
self.right_decoder = TransformerDecoder(
|
316 |
+
vocab_size,
|
317 |
+
encoder_output_size,
|
318 |
+
attention_heads,
|
319 |
+
linear_units,
|
320 |
+
r_num_blocks,
|
321 |
+
dropout_rate,
|
322 |
+
positional_dropout_rate,
|
323 |
+
self_attention_dropout_rate,
|
324 |
+
src_attention_dropout_rate,
|
325 |
+
input_layer,
|
326 |
+
use_output_layer,
|
327 |
+
normalize_before,
|
328 |
+
key_bias=key_bias,
|
329 |
+
gradient_checkpointing=gradient_checkpointing,
|
330 |
+
tie_word_embedding=tie_word_embedding)
|
331 |
+
|
332 |
+
def forward(
|
333 |
+
self,
|
334 |
+
memory: torch.Tensor,
|
335 |
+
memory_mask: torch.Tensor,
|
336 |
+
ys_in_pad: torch.Tensor,
|
337 |
+
ys_in_lens: torch.Tensor,
|
338 |
+
r_ys_in_pad: torch.Tensor,
|
339 |
+
reverse_weight: float = 0.0,
|
340 |
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
341 |
+
"""Forward decoder.
|
342 |
+
Args:
|
343 |
+
memory: encoded memory, float32 (batch, maxlen_in, feat)
|
344 |
+
memory_mask: encoder memory mask, (batch, 1, maxlen_in)
|
345 |
+
ys_in_pad: padded input token ids, int64 (batch, maxlen_out)
|
346 |
+
ys_in_lens: input lengths of this batch (batch)
|
347 |
+
r_ys_in_pad: padded input token ids, int64 (batch, maxlen_out),
|
348 |
+
used for right to left decoder
|
349 |
+
reverse_weight: used for right to left decoder
|
350 |
+
Returns:
|
351 |
+
(tuple): tuple containing:
|
352 |
+
x: decoded token score before softmax (batch, maxlen_out,
|
353 |
+
vocab_size) if use_output_layer is True,
|
354 |
+
r_x: x: decoded token score (right to left decoder)
|
355 |
+
before softmax (batch, maxlen_out, vocab_size)
|
356 |
+
if use_output_layer is True,
|
357 |
+
olens: (batch, )
|
358 |
+
"""
|
359 |
+
l_x, _, olens = self.left_decoder(memory, memory_mask, ys_in_pad,
|
360 |
+
ys_in_lens)
|
361 |
+
r_x = torch.tensor(0.0)
|
362 |
+
if reverse_weight > 0.0:
|
363 |
+
r_x, _, olens = self.right_decoder(memory, memory_mask,
|
364 |
+
r_ys_in_pad, ys_in_lens)
|
365 |
+
return l_x, r_x, olens
|
366 |
+
|
367 |
+
def forward_one_step(
|
368 |
+
self,
|
369 |
+
memory: torch.Tensor,
|
370 |
+
memory_mask: torch.Tensor,
|
371 |
+
tgt: torch.Tensor,
|
372 |
+
tgt_mask: torch.Tensor,
|
373 |
+
cache: Optional[List[torch.Tensor]] = None,
|
374 |
+
) -> Tuple[torch.Tensor, List[torch.Tensor]]:
|
375 |
+
"""Forward one step.
|
376 |
+
This is only used for decoding.
|
377 |
+
Args:
|
378 |
+
memory: encoded memory, float32 (batch, maxlen_in, feat)
|
379 |
+
memory_mask: encoded memory mask, (batch, 1, maxlen_in)
|
380 |
+
tgt: input token ids, int64 (batch, maxlen_out)
|
381 |
+
tgt_mask: input token mask, (batch, maxlen_out)
|
382 |
+
dtype=torch.uint8 in PyTorch 1.2-
|
383 |
+
dtype=torch.bool in PyTorch 1.2+ (include 1.2)
|
384 |
+
cache: cached output list of (batch, max_time_out-1, size)
|
385 |
+
Returns:
|
386 |
+
y, cache: NN output value and cache per `self.decoders`.
|
387 |
+
y.shape` is (batch, maxlen_out, token)
|
388 |
+
"""
|
389 |
+
return self.left_decoder.forward_one_step(memory, memory_mask, tgt,
|
390 |
+
tgt_mask, cache)
|
391 |
+
|
392 |
+
def tie_or_clone_weights(self, jit_mode: bool = True):
|
393 |
+
"""Tie or clone module weights (between word_emb and output_layer)
|
394 |
+
depending of whether we are using TorchScript or not"""
|
395 |
+
self.left_decoder.tie_or_clone_weights(jit_mode)
|
396 |
+
self.right_decoder.tie_or_clone_weights(jit_mode)
|
inspiremusic/transformer/decoder_layer.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2019 Shigeki Karita
|
2 |
+
# 2020 Mobvoi Inc (Binbin Zhang)
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
"""Decoder self-attention layer definition."""
|
16 |
+
from typing import Optional, Tuple
|
17 |
+
|
18 |
+
import torch
|
19 |
+
from torch import nn
|
20 |
+
|
21 |
+
|
22 |
+
class DecoderLayer(nn.Module):
|
23 |
+
"""Single decoder layer module.
|
24 |
+
|
25 |
+
Args:
|
26 |
+
size (int): Input dimension.
|
27 |
+
self_attn (torch.nn.Module): Self-attention module instance.
|
28 |
+
`MultiHeadedAttention` instance can be used as the argument.
|
29 |
+
src_attn (torch.nn.Module): Inter-attention module instance.
|
30 |
+
`MultiHeadedAttention` instance can be used as the argument.
|
31 |
+
If `None` is passed, Inter-attention is not used, such as
|
32 |
+
CIF, GPT, and other decoder only model.
|
33 |
+
feed_forward (torch.nn.Module): Feed-forward module instance.
|
34 |
+
`PositionwiseFeedForward` instance can be used as the argument.
|
35 |
+
dropout_rate (float): Dropout rate.
|
36 |
+
normalize_before (bool):
|
37 |
+
True: use layer_norm before each sub-block.
|
38 |
+
False: to use layer_norm after each sub-block.
|
39 |
+
"""
|
40 |
+
|
41 |
+
def __init__(
|
42 |
+
self,
|
43 |
+
size: int,
|
44 |
+
self_attn: nn.Module,
|
45 |
+
src_attn: Optional[nn.Module],
|
46 |
+
feed_forward: nn.Module,
|
47 |
+
dropout_rate: float,
|
48 |
+
normalize_before: bool = True,
|
49 |
+
):
|
50 |
+
"""Construct an DecoderLayer object."""
|
51 |
+
super().__init__()
|
52 |
+
self.size = size
|
53 |
+
self.self_attn = self_attn
|
54 |
+
self.src_attn = src_attn
|
55 |
+
self.feed_forward = feed_forward
|
56 |
+
self.norm1 = nn.LayerNorm(size, eps=1e-5)
|
57 |
+
self.norm2 = nn.LayerNorm(size, eps=1e-5)
|
58 |
+
self.norm3 = nn.LayerNorm(size, eps=1e-5)
|
59 |
+
self.dropout = nn.Dropout(dropout_rate)
|
60 |
+
self.normalize_before = normalize_before
|
61 |
+
|
62 |
+
def forward(
|
63 |
+
self,
|
64 |
+
tgt: torch.Tensor,
|
65 |
+
tgt_mask: torch.Tensor,
|
66 |
+
memory: torch.Tensor,
|
67 |
+
memory_mask: torch.Tensor,
|
68 |
+
cache: Optional[torch.Tensor] = None
|
69 |
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
|
70 |
+
"""Compute decoded features.
|
71 |
+
|
72 |
+
Args:
|
73 |
+
tgt (torch.Tensor): Input tensor (#batch, maxlen_out, size).
|
74 |
+
tgt_mask (torch.Tensor): Mask for input tensor
|
75 |
+
(#batch, maxlen_out).
|
76 |
+
memory (torch.Tensor): Encoded memory
|
77 |
+
(#batch, maxlen_in, size).
|
78 |
+
memory_mask (torch.Tensor): Encoded memory mask
|
79 |
+
(#batch, maxlen_in).
|
80 |
+
cache (torch.Tensor): cached tensors.
|
81 |
+
(#batch, maxlen_out - 1, size).
|
82 |
+
|
83 |
+
Returns:
|
84 |
+
torch.Tensor: Output tensor (#batch, maxlen_out, size).
|
85 |
+
torch.Tensor: Mask for output tensor (#batch, maxlen_out).
|
86 |
+
torch.Tensor: Encoded memory (#batch, maxlen_in, size).
|
87 |
+
torch.Tensor: Encoded memory mask (#batch, maxlen_in).
|
88 |
+
|
89 |
+
"""
|
90 |
+
residual = tgt
|
91 |
+
if self.normalize_before:
|
92 |
+
tgt = self.norm1(tgt)
|
93 |
+
|
94 |
+
if cache is None:
|
95 |
+
tgt_q = tgt
|
96 |
+
tgt_q_mask = tgt_mask
|
97 |
+
else:
|
98 |
+
# compute only the last frame query keeping dim: max_time_out -> 1
|
99 |
+
assert cache.shape == (
|
100 |
+
tgt.shape[0],
|
101 |
+
tgt.shape[1] - 1,
|
102 |
+
self.size,
|
103 |
+
), "{cache.shape} == {(tgt.shape[0], tgt.shape[1] - 1, self.size)}"
|
104 |
+
tgt_q = tgt[:, -1:, :]
|
105 |
+
residual = residual[:, -1:, :]
|
106 |
+
tgt_q_mask = tgt_mask[:, -1:, :]
|
107 |
+
|
108 |
+
x = residual + self.dropout(
|
109 |
+
self.self_attn(tgt_q, tgt, tgt, tgt_q_mask)[0])
|
110 |
+
if not self.normalize_before:
|
111 |
+
x = self.norm1(x)
|
112 |
+
|
113 |
+
if self.src_attn is not None:
|
114 |
+
residual = x
|
115 |
+
if self.normalize_before:
|
116 |
+
x = self.norm2(x)
|
117 |
+
x = residual + self.dropout(
|
118 |
+
self.src_attn(x, memory, memory, memory_mask)[0])
|
119 |
+
if not self.normalize_before:
|
120 |
+
x = self.norm2(x)
|
121 |
+
|
122 |
+
residual = x
|
123 |
+
if self.normalize_before:
|
124 |
+
x = self.norm3(x)
|
125 |
+
x = residual + self.dropout(self.feed_forward(x))
|
126 |
+
if not self.normalize_before:
|
127 |
+
x = self.norm3(x)
|
128 |
+
|
129 |
+
if cache is not None:
|
130 |
+
x = torch.cat([cache, x], dim=1)
|
131 |
+
|
132 |
+
return x, tgt_mask, memory, memory_mask
|
inspiremusic/transformer/embedding.py
ADDED
@@ -0,0 +1,294 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2020 Mobvoi Inc. (authors: Binbin Zhang, Di Wu)
|
2 |
+
# 2024 Alibaba Inc (Xiang Lyu)
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
# Modified from ESPnet(https://github.com/espnet/espnet)
|
16 |
+
"""Positonal Encoding Module."""
|
17 |
+
|
18 |
+
import math
|
19 |
+
from typing import Tuple, Union
|
20 |
+
|
21 |
+
import torch
|
22 |
+
import torch.nn.functional as F
|
23 |
+
import numpy as np
|
24 |
+
|
25 |
+
|
26 |
+
class PositionalEncoding(torch.nn.Module):
|
27 |
+
"""Positional encoding.
|
28 |
+
|
29 |
+
:param int d_model: embedding dim
|
30 |
+
:param float dropout_rate: dropout rate
|
31 |
+
:param int max_len: maximum input length
|
32 |
+
|
33 |
+
PE(pos, 2i) = sin(pos/(10000^(2i/dmodel)))
|
34 |
+
PE(pos, 2i+1) = cos(pos/(10000^(2i/dmodel)))
|
35 |
+
"""
|
36 |
+
|
37 |
+
def __init__(self,
|
38 |
+
d_model: int,
|
39 |
+
dropout_rate: float,
|
40 |
+
max_len: int = 5000,
|
41 |
+
reverse: bool = False):
|
42 |
+
"""Construct an PositionalEncoding object."""
|
43 |
+
super().__init__()
|
44 |
+
self.d_model = d_model
|
45 |
+
self.xscale = math.sqrt(self.d_model)
|
46 |
+
self.dropout = torch.nn.Dropout(p=dropout_rate)
|
47 |
+
self.max_len = max_len
|
48 |
+
|
49 |
+
self.pe = torch.zeros(self.max_len, self.d_model)
|
50 |
+
position = torch.arange(0, self.max_len,
|
51 |
+
dtype=torch.float32).unsqueeze(1)
|
52 |
+
div_term = torch.exp(
|
53 |
+
torch.arange(0, self.d_model, 2, dtype=torch.float32) *
|
54 |
+
-(math.log(10000.0) / self.d_model))
|
55 |
+
self.pe[:, 0::2] = torch.sin(position * div_term)
|
56 |
+
self.pe[:, 1::2] = torch.cos(position * div_term)
|
57 |
+
self.pe = self.pe.unsqueeze(0)
|
58 |
+
|
59 |
+
def forward(self,
|
60 |
+
x: torch.Tensor,
|
61 |
+
offset: Union[int, torch.Tensor] = 0) \
|
62 |
+
-> Tuple[torch.Tensor, torch.Tensor]:
|
63 |
+
"""Add positional encoding.
|
64 |
+
|
65 |
+
Args:
|
66 |
+
x (torch.Tensor): Input. Its shape is (batch, time, ...)
|
67 |
+
offset (int, torch.tensor): position offset
|
68 |
+
|
69 |
+
Returns:
|
70 |
+
torch.Tensor: Encoded tensor. Its shape is (batch, time, ...)
|
71 |
+
torch.Tensor: for compatibility to RelPositionalEncoding
|
72 |
+
"""
|
73 |
+
|
74 |
+
self.pe = self.pe.to(x.device)
|
75 |
+
pos_emb = self.position_encoding(offset, x.size(1), False)
|
76 |
+
x = x * self.xscale + pos_emb
|
77 |
+
return self.dropout(x), self.dropout(pos_emb)
|
78 |
+
|
79 |
+
def position_encoding(self,
|
80 |
+
offset: Union[int, torch.Tensor],
|
81 |
+
size: int,
|
82 |
+
apply_dropout: bool = True) -> torch.Tensor:
|
83 |
+
""" For getting encoding in a streaming fashion
|
84 |
+
|
85 |
+
Attention!!!!!
|
86 |
+
we apply dropout only once at the whole utterance level in a none
|
87 |
+
streaming way, but will call this function several times with
|
88 |
+
increasing input size in a streaming scenario, so the dropout will
|
89 |
+
be applied several times.
|
90 |
+
|
91 |
+
Args:
|
92 |
+
offset (int or torch.tensor): start offset
|
93 |
+
size (int): required size of position encoding
|
94 |
+
|
95 |
+
Returns:
|
96 |
+
torch.Tensor: Corresponding encoding
|
97 |
+
"""
|
98 |
+
# How to subscript a Union type:
|
99 |
+
# https://github.com/pytorch/pytorch/issues/69434
|
100 |
+
if isinstance(offset, int):
|
101 |
+
assert offset + size <= self.max_len
|
102 |
+
pos_emb = self.pe[:, offset:offset + size]
|
103 |
+
elif isinstance(offset, torch.Tensor) and offset.dim() == 0: # scalar
|
104 |
+
assert offset + size <= self.max_len
|
105 |
+
pos_emb = self.pe[:, offset:offset + size]
|
106 |
+
else: # for batched streaming decoding on GPU
|
107 |
+
assert torch.max(offset) + size <= self.max_len
|
108 |
+
index = offset.unsqueeze(1) + \
|
109 |
+
torch.arange(0, size).to(offset.device) # B X T
|
110 |
+
flag = index > 0
|
111 |
+
# remove negative offset
|
112 |
+
index = index * flag
|
113 |
+
pos_emb = F.embedding(index, self.pe[0]) # B X T X d_model
|
114 |
+
|
115 |
+
if apply_dropout:
|
116 |
+
pos_emb = self.dropout(pos_emb)
|
117 |
+
return pos_emb
|
118 |
+
|
119 |
+
|
120 |
+
class RelPositionalEncoding(PositionalEncoding):
|
121 |
+
"""Relative positional encoding module.
|
122 |
+
See : Appendix B in https://arxiv.org/abs/1901.02860
|
123 |
+
Args:
|
124 |
+
d_model (int): Embedding dimension.
|
125 |
+
dropout_rate (float): Dropout rate.
|
126 |
+
max_len (int): Maximum input length.
|
127 |
+
"""
|
128 |
+
|
129 |
+
def __init__(self, d_model: int, dropout_rate: float, max_len: int = 5000):
|
130 |
+
"""Initialize class."""
|
131 |
+
super().__init__(d_model, dropout_rate, max_len, reverse=True)
|
132 |
+
|
133 |
+
def forward(self,
|
134 |
+
x: torch.Tensor,
|
135 |
+
offset: Union[int, torch.Tensor] = 0) \
|
136 |
+
-> Tuple[torch.Tensor, torch.Tensor]:
|
137 |
+
"""Compute positional encoding.
|
138 |
+
Args:
|
139 |
+
x (torch.Tensor): Input tensor (batch, time, `*`).
|
140 |
+
Returns:
|
141 |
+
torch.Tensor: Encoded tensor (batch, time, `*`).
|
142 |
+
torch.Tensor: Positional embedding tensor (1, time, `*`).
|
143 |
+
"""
|
144 |
+
self.pe = self.pe.to(x.device)
|
145 |
+
x = x * self.xscale
|
146 |
+
pos_emb = self.position_encoding(offset, x.size(1), False)
|
147 |
+
return self.dropout(x), self.dropout(pos_emb)
|
148 |
+
|
149 |
+
|
150 |
+
class WhisperPositionalEncoding(PositionalEncoding):
|
151 |
+
""" Sinusoids position encoding used in openai-whisper.encoder
|
152 |
+
"""
|
153 |
+
|
154 |
+
def __init__(self, d_model: int, dropout_rate: float, max_len: int = 1500):
|
155 |
+
super().__init__(d_model, dropout_rate, max_len)
|
156 |
+
self.xscale = 1.0
|
157 |
+
log_timescale_increment = np.log(10000) / (d_model // 2 - 1)
|
158 |
+
inv_timescales = torch.exp(-log_timescale_increment *
|
159 |
+
torch.arange(d_model // 2))
|
160 |
+
scaled_time = torch.arange(max_len)[:, np.newaxis] * \
|
161 |
+
inv_timescales[np.newaxis, :]
|
162 |
+
pe = torch.cat([torch.sin(scaled_time), torch.cos(scaled_time)], dim=1)
|
163 |
+
delattr(self, "pe")
|
164 |
+
self.register_buffer("pe", pe.unsqueeze(0))
|
165 |
+
|
166 |
+
|
167 |
+
class LearnablePositionalEncoding(PositionalEncoding):
|
168 |
+
""" Learnable position encoding used in openai-whisper.decoder
|
169 |
+
"""
|
170 |
+
|
171 |
+
def __init__(self, d_model: int, dropout_rate: float, max_len: int = 448):
|
172 |
+
super().__init__(d_model, dropout_rate, max_len)
|
173 |
+
# NOTE(xcsong): overwrite self.pe & self.xscale
|
174 |
+
self.pe = torch.nn.Parameter(torch.empty(1, max_len, d_model))
|
175 |
+
self.xscale = 1.0
|
176 |
+
|
177 |
+
|
178 |
+
class NoPositionalEncoding(torch.nn.Module):
|
179 |
+
""" No position encoding
|
180 |
+
"""
|
181 |
+
|
182 |
+
def __init__(self, d_model: int, dropout_rate: float):
|
183 |
+
super().__init__()
|
184 |
+
self.d_model = d_model
|
185 |
+
self.dropout = torch.nn.Dropout(p=dropout_rate)
|
186 |
+
|
187 |
+
def forward(self,
|
188 |
+
x: torch.Tensor,
|
189 |
+
offset: Union[int, torch.Tensor] = 0) \
|
190 |
+
-> Tuple[torch.Tensor, torch.Tensor]:
|
191 |
+
""" Just return zero vector for interface compatibility
|
192 |
+
"""
|
193 |
+
pos_emb = torch.zeros(1, x.size(1), self.d_model).to(x.device)
|
194 |
+
return self.dropout(x), pos_emb
|
195 |
+
|
196 |
+
def position_encoding(self, offset: Union[int, torch.Tensor],
|
197 |
+
size: int) -> torch.Tensor:
|
198 |
+
return torch.zeros(1, size, self.d_model)
|
199 |
+
|
200 |
+
|
201 |
+
class EspnetRelPositionalEncoding(torch.nn.Module):
|
202 |
+
"""Relative positional encoding module (new implementation).
|
203 |
+
|
204 |
+
Details can be found in https://github.com/espnet/espnet/pull/2816.
|
205 |
+
|
206 |
+
See : Appendix B in https://arxiv.org/abs/1901.02860
|
207 |
+
|
208 |
+
Args:
|
209 |
+
d_model (int): Embedding dimension.
|
210 |
+
dropout_rate (float): Dropout rate.
|
211 |
+
max_len (int): Maximum input length.
|
212 |
+
|
213 |
+
"""
|
214 |
+
|
215 |
+
def __init__(self, d_model: int, dropout_rate: float, max_len: int = 5000):
|
216 |
+
"""Construct an PositionalEncoding object."""
|
217 |
+
super(EspnetRelPositionalEncoding, self).__init__()
|
218 |
+
self.d_model = d_model
|
219 |
+
self.xscale = math.sqrt(self.d_model)
|
220 |
+
self.dropout = torch.nn.Dropout(p=dropout_rate)
|
221 |
+
self.pe = None
|
222 |
+
self.extend_pe(torch.tensor(0.0).expand(1, max_len))
|
223 |
+
|
224 |
+
def extend_pe(self, x: torch.Tensor):
|
225 |
+
"""Reset the positional encodings."""
|
226 |
+
if self.pe is not None:
|
227 |
+
# self.pe contains both positive and negative parts
|
228 |
+
# the length of self.pe is 2 * input_len - 1
|
229 |
+
if self.pe.size(1) >= x.size(1) * 2 - 1:
|
230 |
+
if self.pe.dtype != x.dtype or self.pe.device != x.device:
|
231 |
+
self.pe = self.pe.to(dtype=x.dtype, device=x.device)
|
232 |
+
return
|
233 |
+
# Suppose `i` means to the position of query vecotr and `j` means the
|
234 |
+
# position of key vector. We use position relative positions when keys
|
235 |
+
# are to the left (i>j) and negative relative positions otherwise (i<j).
|
236 |
+
pe_positive = torch.zeros(x.size(1), self.d_model)
|
237 |
+
pe_negative = torch.zeros(x.size(1), self.d_model)
|
238 |
+
position = torch.arange(0, x.size(1), dtype=torch.float32).unsqueeze(1)
|
239 |
+
div_term = torch.exp(
|
240 |
+
torch.arange(0, self.d_model, 2, dtype=torch.float32)
|
241 |
+
* -(math.log(10000.0) / self.d_model)
|
242 |
+
)
|
243 |
+
pe_positive[:, 0::2] = torch.sin(position * div_term)
|
244 |
+
pe_positive[:, 1::2] = torch.cos(position * div_term)
|
245 |
+
pe_negative[:, 0::2] = torch.sin(-1 * position * div_term)
|
246 |
+
pe_negative[:, 1::2] = torch.cos(-1 * position * div_term)
|
247 |
+
|
248 |
+
# Reserve the order of positive indices and concat both positive and
|
249 |
+
# negative indices. This is used to support the shifting trick
|
250 |
+
# as in https://arxiv.org/abs/1901.02860
|
251 |
+
pe_positive = torch.flip(pe_positive, [0]).unsqueeze(0)
|
252 |
+
pe_negative = pe_negative[1:].unsqueeze(0)
|
253 |
+
pe = torch.cat([pe_positive, pe_negative], dim=1)
|
254 |
+
self.pe = pe.to(device=x.device, dtype=x.dtype)
|
255 |
+
|
256 |
+
def forward(self, x: torch.Tensor, offset: Union[int, torch.Tensor] = 0) \
|
257 |
+
-> Tuple[torch.Tensor, torch.Tensor]:
|
258 |
+
"""Add positional encoding.
|
259 |
+
|
260 |
+
Args:
|
261 |
+
x (torch.Tensor): Input tensor (batch, time, `*`).
|
262 |
+
|
263 |
+
Returns:
|
264 |
+
torch.Tensor: Encoded tensor (batch, time, `*`).
|
265 |
+
|
266 |
+
"""
|
267 |
+
self.extend_pe(x)
|
268 |
+
x = x * self.xscale
|
269 |
+
pos_emb = self.position_encoding(size=x.size(1), offset=offset)
|
270 |
+
return self.dropout(x), self.dropout(pos_emb)
|
271 |
+
|
272 |
+
def position_encoding(self,
|
273 |
+
offset: Union[int, torch.Tensor],
|
274 |
+
size: int) -> torch.Tensor:
|
275 |
+
""" For getting encoding in a streaming fashion
|
276 |
+
|
277 |
+
Attention!!!!!
|
278 |
+
we apply dropout only once at the whole utterance level in a none
|
279 |
+
streaming way, but will call this function several times with
|
280 |
+
increasing input size in a streaming scenario, so the dropout will
|
281 |
+
be applied several times.
|
282 |
+
|
283 |
+
Args:
|
284 |
+
offset (int or torch.tensor): start offset
|
285 |
+
size (int): required size of position encoding
|
286 |
+
|
287 |
+
Returns:
|
288 |
+
torch.Tensor: Corresponding encoding
|
289 |
+
"""
|
290 |
+
pos_emb = self.pe[
|
291 |
+
:,
|
292 |
+
self.pe.size(1) // 2 - size + 1: self.pe.size(1) // 2 + size,
|
293 |
+
]
|
294 |
+
return pos_emb
|
inspiremusic/transformer/encoder.py
ADDED
@@ -0,0 +1,477 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2021 Mobvoi Inc (Binbin Zhang, Di Wu)
|
2 |
+
# 2022 Xingchen Song ([email protected])
|
3 |
+
# 2024 Alibaba Inc (Xiang Lyu)
|
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 |
+
# Modified from ESPnet(https://github.com/espnet/espnet)
|
17 |
+
"""Encoder definition."""
|
18 |
+
from typing import Tuple
|
19 |
+
|
20 |
+
import torch
|
21 |
+
import torch.utils.checkpoint as ckpt
|
22 |
+
|
23 |
+
from inspiremusic.transformer.convolution import ConvolutionModule
|
24 |
+
from inspiremusic.transformer.encoder_layer import TransformerEncoderLayer
|
25 |
+
from inspiremusic.transformer.encoder_layer import ConformerEncoderLayer
|
26 |
+
from inspiremusic.transformer.positionwise_feed_forward import PositionwiseFeedForward
|
27 |
+
from inspiremusic.utils.class_utils import (
|
28 |
+
INSPIREMUSIC_EMB_CLASSES,
|
29 |
+
INSPIREMUSIC_SUBSAMPLE_CLASSES,
|
30 |
+
INSPIREMUSIC_ATTENTION_CLASSES,
|
31 |
+
INSPIREMUSIC_ACTIVATION_CLASSES,
|
32 |
+
)
|
33 |
+
from inspiremusic.utils.mask import make_pad_mask
|
34 |
+
from inspiremusic.utils.mask import add_optional_chunk_mask
|
35 |
+
|
36 |
+
|
37 |
+
class BaseEncoder(torch.nn.Module):
|
38 |
+
|
39 |
+
def __init__(
|
40 |
+
self,
|
41 |
+
input_size: int,
|
42 |
+
output_size: int = 256,
|
43 |
+
attention_heads: int = 4,
|
44 |
+
linear_units: int = 2048,
|
45 |
+
num_blocks: int = 6,
|
46 |
+
dropout_rate: float = 0.1,
|
47 |
+
positional_dropout_rate: float = 0.1,
|
48 |
+
attention_dropout_rate: float = 0.0,
|
49 |
+
input_layer: str = "conv2d",
|
50 |
+
pos_enc_layer_type: str = "abs_pos",
|
51 |
+
normalize_before: bool = True,
|
52 |
+
static_chunk_size: int = 0,
|
53 |
+
use_dynamic_chunk: bool = False,
|
54 |
+
global_cmvn: torch.nn.Module = None,
|
55 |
+
use_dynamic_left_chunk: bool = False,
|
56 |
+
gradient_checkpointing: bool = False,
|
57 |
+
):
|
58 |
+
"""
|
59 |
+
Args:
|
60 |
+
input_size (int): input dim
|
61 |
+
output_size (int): dimension of attention
|
62 |
+
attention_heads (int): the number of heads of multi head attention
|
63 |
+
linear_units (int): the hidden units number of position-wise feed
|
64 |
+
forward
|
65 |
+
num_blocks (int): the number of decoder blocks
|
66 |
+
dropout_rate (float): dropout rate
|
67 |
+
attention_dropout_rate (float): dropout rate in attention
|
68 |
+
positional_dropout_rate (float): dropout rate after adding
|
69 |
+
positional encoding
|
70 |
+
input_layer (str): input layer type.
|
71 |
+
optional [linear, conv2d, conv2d6, conv2d8]
|
72 |
+
pos_enc_layer_type (str): Encoder positional encoding layer type.
|
73 |
+
opitonal [abs_pos, scaled_abs_pos, rel_pos, no_pos]
|
74 |
+
normalize_before (bool):
|
75 |
+
True: use layer_norm before each sub-block of a layer.
|
76 |
+
False: use layer_norm after each sub-block of a layer.
|
77 |
+
static_chunk_size (int): chunk size for static chunk training and
|
78 |
+
decoding
|
79 |
+
use_dynamic_chunk (bool): whether use dynamic chunk size for
|
80 |
+
training or not, You can only use fixed chunk(chunk_size > 0)
|
81 |
+
or dyanmic chunk size(use_dynamic_chunk = True)
|
82 |
+
global_cmvn (Optional[torch.nn.Module]): Optional GlobalCMVN module
|
83 |
+
use_dynamic_left_chunk (bool): whether use dynamic left chunk in
|
84 |
+
dynamic chunk training
|
85 |
+
key_bias: whether use bias in attention.linear_k, False for whisper models.
|
86 |
+
gradient_checkpointing: rerunning a forward-pass segment for each
|
87 |
+
checkpointed segment during backward.
|
88 |
+
"""
|
89 |
+
super().__init__()
|
90 |
+
self._output_size = output_size
|
91 |
+
|
92 |
+
self.global_cmvn = global_cmvn
|
93 |
+
self.embed = INSPIREMUSIC_SUBSAMPLE_CLASSES[input_layer](
|
94 |
+
input_size,
|
95 |
+
output_size,
|
96 |
+
dropout_rate,
|
97 |
+
INSPIREMUSIC_EMB_CLASSES[pos_enc_layer_type](output_size,
|
98 |
+
positional_dropout_rate),
|
99 |
+
)
|
100 |
+
|
101 |
+
self.normalize_before = normalize_before
|
102 |
+
self.after_norm = torch.nn.LayerNorm(output_size, eps=1e-5)
|
103 |
+
self.static_chunk_size = static_chunk_size
|
104 |
+
self.use_dynamic_chunk = use_dynamic_chunk
|
105 |
+
self.use_dynamic_left_chunk = use_dynamic_left_chunk
|
106 |
+
self.gradient_checkpointing = gradient_checkpointing
|
107 |
+
|
108 |
+
def output_size(self) -> int:
|
109 |
+
return self._output_size
|
110 |
+
|
111 |
+
def forward(
|
112 |
+
self,
|
113 |
+
xs: torch.Tensor,
|
114 |
+
xs_lens: torch.Tensor,
|
115 |
+
decoding_chunk_size: int = 0,
|
116 |
+
num_decoding_left_chunks: int = -1,
|
117 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
118 |
+
"""Embed positions in tensor.
|
119 |
+
|
120 |
+
Args:
|
121 |
+
xs: padded input tensor (B, T, D)
|
122 |
+
xs_lens: input length (B)
|
123 |
+
decoding_chunk_size: decoding chunk size for dynamic chunk
|
124 |
+
0: default for training, use random dynamic chunk.
|
125 |
+
<0: for decoding, use full chunk.
|
126 |
+
>0: for decoding, use fixed chunk size as set.
|
127 |
+
num_decoding_left_chunks: number of left chunks, this is for decoding,
|
128 |
+
the chunk size is decoding_chunk_size.
|
129 |
+
>=0: use num_decoding_left_chunks
|
130 |
+
<0: use all left chunks
|
131 |
+
Returns:
|
132 |
+
encoder output tensor xs, and subsampled masks
|
133 |
+
xs: padded output tensor (B, T' ~= T/subsample_rate, D)
|
134 |
+
masks: torch.Tensor batch padding mask after subsample
|
135 |
+
(B, 1, T' ~= T/subsample_rate)
|
136 |
+
NOTE(xcsong):
|
137 |
+
We pass the `__call__` method of the modules instead of `forward` to the
|
138 |
+
checkpointing API because `__call__` attaches all the hooks of the module.
|
139 |
+
https://discuss.pytorch.org/t/any-different-between-model-input-and-model-forward-input/3690/2
|
140 |
+
"""
|
141 |
+
T = xs.size(1)
|
142 |
+
masks = ~make_pad_mask(xs_lens, T).unsqueeze(1) # (B, 1, T)
|
143 |
+
if self.global_cmvn is not None:
|
144 |
+
xs = self.global_cmvn(xs)
|
145 |
+
xs, pos_emb, masks = self.embed(xs, masks)
|
146 |
+
mask_pad = masks # (B, 1, T/subsample_rate)
|
147 |
+
chunk_masks = add_optional_chunk_mask(xs, masks,
|
148 |
+
self.use_dynamic_chunk,
|
149 |
+
self.use_dynamic_left_chunk,
|
150 |
+
decoding_chunk_size,
|
151 |
+
self.static_chunk_size,
|
152 |
+
num_decoding_left_chunks)
|
153 |
+
|
154 |
+
if self.gradient_checkpointing and self.training:
|
155 |
+
xs = self.forward_layers_checkpointed(xs, chunk_masks, pos_emb,
|
156 |
+
mask_pad)
|
157 |
+
else:
|
158 |
+
xs = self.forward_layers(xs, chunk_masks, pos_emb, mask_pad)
|
159 |
+
if self.normalize_before:
|
160 |
+
xs = self.after_norm(xs)
|
161 |
+
# Here we assume the mask is not changed in encoder layers, so just
|
162 |
+
# return the masks before encoder layers, and the masks will be used
|
163 |
+
# for cross attention with decoder later
|
164 |
+
return xs, masks
|
165 |
+
|
166 |
+
def forward_layers(self, xs: torch.Tensor, chunk_masks: torch.Tensor,
|
167 |
+
pos_emb: torch.Tensor,
|
168 |
+
mask_pad: torch.Tensor) -> torch.Tensor:
|
169 |
+
for layer in self.encoders:
|
170 |
+
xs, chunk_masks, _, _ = layer(xs, chunk_masks, pos_emb, mask_pad)
|
171 |
+
return xs
|
172 |
+
|
173 |
+
@torch.jit.unused
|
174 |
+
def forward_layers_checkpointed(self, xs: torch.Tensor,
|
175 |
+
chunk_masks: torch.Tensor,
|
176 |
+
pos_emb: torch.Tensor,
|
177 |
+
mask_pad: torch.Tensor) -> torch.Tensor:
|
178 |
+
for layer in self.encoders:
|
179 |
+
xs, chunk_masks, _, _ = ckpt.checkpoint(layer.__call__, xs,
|
180 |
+
chunk_masks, pos_emb,
|
181 |
+
mask_pad)
|
182 |
+
return xs
|
183 |
+
|
184 |
+
@torch.jit.export
|
185 |
+
def forward_chunk(
|
186 |
+
self,
|
187 |
+
xs: torch.Tensor,
|
188 |
+
offset: int,
|
189 |
+
required_cache_size: int,
|
190 |
+
att_cache: torch.Tensor = torch.zeros(0, 0, 0, 0),
|
191 |
+
cnn_cache: torch.Tensor = torch.zeros(0, 0, 0, 0),
|
192 |
+
att_mask: torch.Tensor = torch.ones((0, 0, 0), dtype=torch.bool),
|
193 |
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
194 |
+
""" Forward just one chunk
|
195 |
+
|
196 |
+
Args:
|
197 |
+
xs (torch.Tensor): chunk input, with shape (b=1, time, mel-dim),
|
198 |
+
where `time == (chunk_size - 1) * subsample_rate + \
|
199 |
+
subsample.right_context + 1`
|
200 |
+
offset (int): current offset in encoder output time stamp
|
201 |
+
required_cache_size (int): cache size required for next chunk
|
202 |
+
compuation
|
203 |
+
>=0: actual cache size
|
204 |
+
<0: means all history cache is required
|
205 |
+
att_cache (torch.Tensor): cache tensor for KEY & VALUE in
|
206 |
+
transformer/conformer attention, with shape
|
207 |
+
(elayers, head, cache_t1, d_k * 2), where
|
208 |
+
`head * d_k == hidden-dim` and
|
209 |
+
`cache_t1 == chunk_size * num_decoding_left_chunks`.
|
210 |
+
cnn_cache (torch.Tensor): cache tensor for cnn_module in conformer,
|
211 |
+
(elayers, b=1, hidden-dim, cache_t2), where
|
212 |
+
`cache_t2 == cnn.lorder - 1`
|
213 |
+
|
214 |
+
Returns:
|
215 |
+
torch.Tensor: output of current input xs,
|
216 |
+
with shape (b=1, chunk_size, hidden-dim).
|
217 |
+
torch.Tensor: new attention cache required for next chunk, with
|
218 |
+
dynamic shape (elayers, head, ?, d_k * 2)
|
219 |
+
depending on required_cache_size.
|
220 |
+
torch.Tensor: new conformer cnn cache required for next chunk, with
|
221 |
+
same shape as the original cnn_cache.
|
222 |
+
|
223 |
+
"""
|
224 |
+
assert xs.size(0) == 1
|
225 |
+
# tmp_masks is just for interface compatibility
|
226 |
+
tmp_masks = torch.ones(1,
|
227 |
+
xs.size(1),
|
228 |
+
device=xs.device,
|
229 |
+
dtype=torch.bool)
|
230 |
+
tmp_masks = tmp_masks.unsqueeze(1)
|
231 |
+
if self.global_cmvn is not None:
|
232 |
+
xs = self.global_cmvn(xs)
|
233 |
+
# NOTE(xcsong): Before embed, shape(xs) is (b=1, time, mel-dim)
|
234 |
+
|
235 |
+
xs, pos_emb, _ = self.embed(xs, tmp_masks, offset)
|
236 |
+
# NOTE(xcsong): After embed, shape(xs) is (b=1, chunk_size, hidden-dim)
|
237 |
+
elayers, cache_t1 = att_cache.size(0), att_cache.size(2)
|
238 |
+
chunk_size = xs.size(1)
|
239 |
+
attention_key_size = cache_t1 + chunk_size
|
240 |
+
pos_emb = self.embed.position_encoding(offset=offset - cache_t1,
|
241 |
+
size=attention_key_size)
|
242 |
+
if required_cache_size < 0:
|
243 |
+
next_cache_start = 0
|
244 |
+
elif required_cache_size == 0:
|
245 |
+
next_cache_start = attention_key_size
|
246 |
+
else:
|
247 |
+
next_cache_start = max(attention_key_size - required_cache_size, 0)
|
248 |
+
r_att_cache = []
|
249 |
+
r_cnn_cache = []
|
250 |
+
for i, layer in enumerate(self.encoders):
|
251 |
+
# NOTE(xcsong): Before layer.forward
|
252 |
+
# shape(att_cache[i:i + 1]) is (1, head, cache_t1, d_k * 2),
|
253 |
+
# shape(cnn_cache[i]) is (b=1, hidden-dim, cache_t2)
|
254 |
+
|
255 |
+
xs, _, new_att_cache, new_cnn_cache = layer(
|
256 |
+
xs,
|
257 |
+
att_mask,
|
258 |
+
pos_emb,
|
259 |
+
att_cache=att_cache[i:i + 1] if elayers > 0 else att_cache,
|
260 |
+
cnn_cache=cnn_cache[i] if cnn_cache.size(0) > 0 else cnn_cache)
|
261 |
+
# NOTE(xcsong): After layer.forward
|
262 |
+
# shape(new_att_cache) is (1, head, attention_key_size, d_k * 2),
|
263 |
+
# shape(new_cnn_cache) is (b=1, hidden-dim, cache_t2)
|
264 |
+
r_att_cache.append(new_att_cache[:, :, next_cache_start:, :])
|
265 |
+
r_cnn_cache.append(new_cnn_cache.unsqueeze(0))
|
266 |
+
if self.normalize_before:
|
267 |
+
xs = self.after_norm(xs)
|
268 |
+
|
269 |
+
# NOTE(xcsong): shape(r_att_cache) is (elayers, head, ?, d_k * 2),
|
270 |
+
# ? may be larger than cache_t1, it depends on required_cache_size
|
271 |
+
r_att_cache = torch.cat(r_att_cache, dim=0)
|
272 |
+
# NOTE(xcsong): shape(r_cnn_cache) is (e, b=1, hidden-dim, cache_t2)
|
273 |
+
r_cnn_cache = torch.cat(r_cnn_cache, dim=0)
|
274 |
+
|
275 |
+
return (xs, r_att_cache, r_cnn_cache)
|
276 |
+
|
277 |
+
@torch.jit.unused
|
278 |
+
def forward_chunk_by_chunk(
|
279 |
+
self,
|
280 |
+
xs: torch.Tensor,
|
281 |
+
decoding_chunk_size: int,
|
282 |
+
num_decoding_left_chunks: int = -1,
|
283 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
284 |
+
""" Forward input chunk by chunk with chunk_size like a streaming
|
285 |
+
fashion
|
286 |
+
|
287 |
+
Here we should pay special attention to computation cache in the
|
288 |
+
streaming style forward chunk by chunk. Three things should be taken
|
289 |
+
into account for computation in the current network:
|
290 |
+
1. transformer/conformer encoder layers output cache
|
291 |
+
2. convolution in conformer
|
292 |
+
3. convolution in subsampling
|
293 |
+
|
294 |
+
However, we don't implement subsampling cache for:
|
295 |
+
1. We can control subsampling module to output the right result by
|
296 |
+
overlapping input instead of cache left context, even though it
|
297 |
+
wastes some computation, but subsampling only takes a very
|
298 |
+
small fraction of computation in the whole model.
|
299 |
+
2. Typically, there are several covolution layers with subsampling
|
300 |
+
in subsampling module, it is tricky and complicated to do cache
|
301 |
+
with different convolution layers with different subsampling
|
302 |
+
rate.
|
303 |
+
3. Currently, nn.Sequential is used to stack all the convolution
|
304 |
+
layers in subsampling, we need to rewrite it to make it work
|
305 |
+
with cache, which is not preferred.
|
306 |
+
Args:
|
307 |
+
xs (torch.Tensor): (1, max_len, dim)
|
308 |
+
chunk_size (int): decoding chunk size
|
309 |
+
"""
|
310 |
+
assert decoding_chunk_size > 0
|
311 |
+
# The model is trained by static or dynamic chunk
|
312 |
+
assert self.static_chunk_size > 0 or self.use_dynamic_chunk
|
313 |
+
subsampling = self.embed.subsampling_rate
|
314 |
+
context = self.embed.right_context + 1 # Add current frame
|
315 |
+
stride = subsampling * decoding_chunk_size
|
316 |
+
decoding_window = (decoding_chunk_size - 1) * subsampling + context
|
317 |
+
num_frames = xs.size(1)
|
318 |
+
att_cache: torch.Tensor = torch.zeros((0, 0, 0, 0), device=xs.device)
|
319 |
+
cnn_cache: torch.Tensor = torch.zeros((0, 0, 0, 0), device=xs.device)
|
320 |
+
outputs = []
|
321 |
+
offset = 0
|
322 |
+
required_cache_size = decoding_chunk_size * num_decoding_left_chunks
|
323 |
+
|
324 |
+
# Feed forward overlap input step by step
|
325 |
+
for cur in range(0, num_frames - context + 1, stride):
|
326 |
+
end = min(cur + decoding_window, num_frames)
|
327 |
+
chunk_xs = xs[:, cur:end, :]
|
328 |
+
(y, att_cache,
|
329 |
+
cnn_cache) = self.forward_chunk(chunk_xs, offset,
|
330 |
+
required_cache_size, att_cache,
|
331 |
+
cnn_cache)
|
332 |
+
outputs.append(y)
|
333 |
+
offset += y.size(1)
|
334 |
+
ys = torch.cat(outputs, 1)
|
335 |
+
masks = torch.ones((1, 1, ys.size(1)),
|
336 |
+
device=ys.device,
|
337 |
+
dtype=torch.bool)
|
338 |
+
return ys, masks
|
339 |
+
|
340 |
+
|
341 |
+
class TransformerEncoder(BaseEncoder):
|
342 |
+
"""Transformer encoder module."""
|
343 |
+
|
344 |
+
def __init__(
|
345 |
+
self,
|
346 |
+
input_size: int,
|
347 |
+
output_size: int = 256,
|
348 |
+
attention_heads: int = 4,
|
349 |
+
linear_units: int = 2048,
|
350 |
+
num_blocks: int = 6,
|
351 |
+
dropout_rate: float = 0.1,
|
352 |
+
positional_dropout_rate: float = 0.1,
|
353 |
+
attention_dropout_rate: float = 0.0,
|
354 |
+
input_layer: str = "conv2d",
|
355 |
+
pos_enc_layer_type: str = "abs_pos",
|
356 |
+
normalize_before: bool = True,
|
357 |
+
static_chunk_size: int = 0,
|
358 |
+
use_dynamic_chunk: bool = False,
|
359 |
+
global_cmvn: torch.nn.Module = None,
|
360 |
+
use_dynamic_left_chunk: bool = False,
|
361 |
+
key_bias: bool = True,
|
362 |
+
selfattention_layer_type: str = "selfattn",
|
363 |
+
activation_type: str = "relu",
|
364 |
+
gradient_checkpointing: bool = False,
|
365 |
+
):
|
366 |
+
""" Construct TransformerEncoder
|
367 |
+
|
368 |
+
See Encoder for the meaning of each parameter.
|
369 |
+
"""
|
370 |
+
super().__init__(input_size, output_size, attention_heads,
|
371 |
+
linear_units, num_blocks, dropout_rate,
|
372 |
+
positional_dropout_rate, attention_dropout_rate,
|
373 |
+
input_layer, pos_enc_layer_type, normalize_before,
|
374 |
+
static_chunk_size, use_dynamic_chunk, global_cmvn,
|
375 |
+
use_dynamic_left_chunk, gradient_checkpointing)
|
376 |
+
activation = INSPIREMUSIC_ACTIVATION_CLASSES[activation_type]()
|
377 |
+
self.encoders = torch.nn.ModuleList([
|
378 |
+
TransformerEncoderLayer(
|
379 |
+
output_size,
|
380 |
+
INSPIREMUSIC_ATTENTION_CLASSES[selfattention_layer_type](attention_heads,
|
381 |
+
output_size,
|
382 |
+
attention_dropout_rate,
|
383 |
+
key_bias),
|
384 |
+
PositionwiseFeedForward(output_size, linear_units,
|
385 |
+
dropout_rate, activation),
|
386 |
+
dropout_rate, normalize_before) for _ in range(num_blocks)
|
387 |
+
])
|
388 |
+
|
389 |
+
|
390 |
+
class ConformerEncoder(BaseEncoder):
|
391 |
+
"""Conformer encoder module."""
|
392 |
+
|
393 |
+
def __init__(
|
394 |
+
self,
|
395 |
+
input_size: int,
|
396 |
+
output_size: int = 256,
|
397 |
+
attention_heads: int = 4,
|
398 |
+
linear_units: int = 2048,
|
399 |
+
num_blocks: int = 6,
|
400 |
+
dropout_rate: float = 0.1,
|
401 |
+
positional_dropout_rate: float = 0.1,
|
402 |
+
attention_dropout_rate: float = 0.0,
|
403 |
+
input_layer: str = "conv2d",
|
404 |
+
pos_enc_layer_type: str = "rel_pos",
|
405 |
+
normalize_before: bool = True,
|
406 |
+
static_chunk_size: int = 0,
|
407 |
+
use_dynamic_chunk: bool = False,
|
408 |
+
global_cmvn: torch.nn.Module = None,
|
409 |
+
use_dynamic_left_chunk: bool = False,
|
410 |
+
positionwise_conv_kernel_size: int = 1,
|
411 |
+
macaron_style: bool = True,
|
412 |
+
selfattention_layer_type: str = "rel_selfattn",
|
413 |
+
activation_type: str = "swish",
|
414 |
+
use_cnn_module: bool = True,
|
415 |
+
cnn_module_kernel: int = 15,
|
416 |
+
causal: bool = False,
|
417 |
+
cnn_module_norm: str = "batch_norm",
|
418 |
+
key_bias: bool = True,
|
419 |
+
gradient_checkpointing: bool = False,
|
420 |
+
):
|
421 |
+
"""Construct ConformerEncoder
|
422 |
+
|
423 |
+
Args:
|
424 |
+
input_size to use_dynamic_chunk, see in BaseEncoder
|
425 |
+
positionwise_conv_kernel_size (int): Kernel size of positionwise
|
426 |
+
conv1d layer.
|
427 |
+
macaron_style (bool): Whether to use macaron style for
|
428 |
+
positionwise layer.
|
429 |
+
selfattention_layer_type (str): Encoder attention layer type,
|
430 |
+
the parameter has no effect now, it's just for configure
|
431 |
+
compatibility.
|
432 |
+
activation_type (str): Encoder activation function type.
|
433 |
+
use_cnn_module (bool): Whether to use convolution module.
|
434 |
+
cnn_module_kernel (int): Kernel size of convolution module.
|
435 |
+
causal (bool): whether to use causal convolution or not.
|
436 |
+
key_bias: whether use bias in attention.linear_k, False for whisper models.
|
437 |
+
"""
|
438 |
+
super().__init__(input_size, output_size, attention_heads,
|
439 |
+
linear_units, num_blocks, dropout_rate,
|
440 |
+
positional_dropout_rate, attention_dropout_rate,
|
441 |
+
input_layer, pos_enc_layer_type, normalize_before,
|
442 |
+
static_chunk_size, use_dynamic_chunk, global_cmvn,
|
443 |
+
use_dynamic_left_chunk, gradient_checkpointing)
|
444 |
+
activation = INSPIREMUSIC_ACTIVATION_CLASSES[activation_type]()
|
445 |
+
|
446 |
+
# self-attention module definition
|
447 |
+
encoder_selfattn_layer_args = (
|
448 |
+
attention_heads,
|
449 |
+
output_size,
|
450 |
+
attention_dropout_rate,
|
451 |
+
key_bias,
|
452 |
+
)
|
453 |
+
# feed-forward module definition
|
454 |
+
positionwise_layer_args = (
|
455 |
+
output_size,
|
456 |
+
linear_units,
|
457 |
+
dropout_rate,
|
458 |
+
activation,
|
459 |
+
)
|
460 |
+
# convolution module definition
|
461 |
+
convolution_layer_args = (output_size, cnn_module_kernel, activation,
|
462 |
+
cnn_module_norm, causal)
|
463 |
+
|
464 |
+
self.encoders = torch.nn.ModuleList([
|
465 |
+
ConformerEncoderLayer(
|
466 |
+
output_size,
|
467 |
+
INSPIREMUSIC_ATTENTION_CLASSES[selfattention_layer_type](
|
468 |
+
*encoder_selfattn_layer_args),
|
469 |
+
PositionwiseFeedForward(*positionwise_layer_args),
|
470 |
+
PositionwiseFeedForward(
|
471 |
+
*positionwise_layer_args) if macaron_style else None,
|
472 |
+
ConvolutionModule(
|
473 |
+
*convolution_layer_args) if use_cnn_module else None,
|
474 |
+
dropout_rate,
|
475 |
+
normalize_before,
|
476 |
+
) for _ in range(num_blocks)
|
477 |
+
])
|
inspiremusic/transformer/encoder_layer.py
ADDED
@@ -0,0 +1,235 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2021 Mobvoi Inc (Binbin Zhang, Di Wu)
|
2 |
+
# 2022 Xingchen Song ([email protected])
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
# Modified from ESPnet(https://github.com/espnet/espnet)
|
16 |
+
"""Encoder self-attention layer definition."""
|
17 |
+
|
18 |
+
from typing import Optional, Tuple
|
19 |
+
|
20 |
+
import torch
|
21 |
+
from torch import nn
|
22 |
+
|
23 |
+
|
24 |
+
class TransformerEncoderLayer(nn.Module):
|
25 |
+
"""Encoder layer module.
|
26 |
+
|
27 |
+
Args:
|
28 |
+
size (int): Input dimension.
|
29 |
+
self_attn (torch.nn.Module): Self-attention module instance.
|
30 |
+
`MultiHeadedAttention` or `RelPositionMultiHeadedAttention`
|
31 |
+
instance can be used as the argument.
|
32 |
+
feed_forward (torch.nn.Module): Feed-forward module instance.
|
33 |
+
`PositionwiseFeedForward`, instance can be used as the argument.
|
34 |
+
dropout_rate (float): Dropout rate.
|
35 |
+
normalize_before (bool):
|
36 |
+
True: use layer_norm before each sub-block.
|
37 |
+
False: to use layer_norm after each sub-block.
|
38 |
+
"""
|
39 |
+
|
40 |
+
def __init__(
|
41 |
+
self,
|
42 |
+
size: int,
|
43 |
+
self_attn: torch.nn.Module,
|
44 |
+
feed_forward: torch.nn.Module,
|
45 |
+
dropout_rate: float,
|
46 |
+
normalize_before: bool = True,
|
47 |
+
):
|
48 |
+
"""Construct an EncoderLayer object."""
|
49 |
+
super().__init__()
|
50 |
+
self.self_attn = self_attn
|
51 |
+
self.feed_forward = feed_forward
|
52 |
+
self.norm1 = nn.LayerNorm(size, eps=1e-5)
|
53 |
+
self.norm2 = nn.LayerNorm(size, eps=1e-5)
|
54 |
+
self.dropout = nn.Dropout(dropout_rate)
|
55 |
+
self.size = size
|
56 |
+
self.normalize_before = normalize_before
|
57 |
+
|
58 |
+
def forward(
|
59 |
+
self,
|
60 |
+
x: torch.Tensor,
|
61 |
+
mask: torch.Tensor,
|
62 |
+
pos_emb: torch.Tensor,
|
63 |
+
mask_pad: torch.Tensor = torch.ones((0, 0, 0), dtype=torch.bool),
|
64 |
+
att_cache: torch.Tensor = torch.zeros((0, 0, 0, 0)),
|
65 |
+
cnn_cache: torch.Tensor = torch.zeros((0, 0, 0, 0)),
|
66 |
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
|
67 |
+
"""Compute encoded features.
|
68 |
+
|
69 |
+
Args:
|
70 |
+
x (torch.Tensor): (#batch, time, size)
|
71 |
+
mask (torch.Tensor): Mask tensor for the input (#batch, time,time),
|
72 |
+
(0, 0, 0) means fake mask.
|
73 |
+
pos_emb (torch.Tensor): just for interface compatibility
|
74 |
+
to ConformerEncoderLayer
|
75 |
+
mask_pad (torch.Tensor): does not used in transformer layer,
|
76 |
+
just for unified api with conformer.
|
77 |
+
att_cache (torch.Tensor): Cache tensor of the KEY & VALUE
|
78 |
+
(#batch=1, head, cache_t1, d_k * 2), head * d_k == size.
|
79 |
+
cnn_cache (torch.Tensor): Convolution cache in conformer layer
|
80 |
+
(#batch=1, size, cache_t2), not used here, it's for interface
|
81 |
+
compatibility to ConformerEncoderLayer.
|
82 |
+
Returns:
|
83 |
+
torch.Tensor: Output tensor (#batch, time, size).
|
84 |
+
torch.Tensor: Mask tensor (#batch, time, time).
|
85 |
+
torch.Tensor: att_cache tensor,
|
86 |
+
(#batch=1, head, cache_t1 + time, d_k * 2).
|
87 |
+
torch.Tensor: cnn_cahce tensor (#batch=1, size, cache_t2).
|
88 |
+
|
89 |
+
"""
|
90 |
+
residual = x
|
91 |
+
if self.normalize_before:
|
92 |
+
x = self.norm1(x)
|
93 |
+
x_att, new_att_cache = self.self_attn(x, x, x, mask, pos_emb=pos_emb, cache=att_cache)
|
94 |
+
x = residual + self.dropout(x_att)
|
95 |
+
if not self.normalize_before:
|
96 |
+
x = self.norm1(x)
|
97 |
+
|
98 |
+
residual = x
|
99 |
+
if self.normalize_before:
|
100 |
+
x = self.norm2(x)
|
101 |
+
x = residual + self.dropout(self.feed_forward(x))
|
102 |
+
if not self.normalize_before:
|
103 |
+
x = self.norm2(x)
|
104 |
+
|
105 |
+
fake_cnn_cache = torch.zeros((0, 0, 0), dtype=x.dtype, device=x.device)
|
106 |
+
return x, mask, new_att_cache, fake_cnn_cache
|
107 |
+
|
108 |
+
|
109 |
+
class ConformerEncoderLayer(nn.Module):
|
110 |
+
"""Encoder layer module.
|
111 |
+
Args:
|
112 |
+
size (int): Input dimension.
|
113 |
+
self_attn (torch.nn.Module): Self-attention module instance.
|
114 |
+
`MultiHeadedAttention` or `RelPositionMultiHeadedAttention`
|
115 |
+
instance can be used as the argument.
|
116 |
+
feed_forward (torch.nn.Module): Feed-forward module instance.
|
117 |
+
`PositionwiseFeedForward` instance can be used as the argument.
|
118 |
+
feed_forward_macaron (torch.nn.Module): Additional feed-forward module
|
119 |
+
instance.
|
120 |
+
`PositionwiseFeedForward` instance can be used as the argument.
|
121 |
+
conv_module (torch.nn.Module): Convolution module instance.
|
122 |
+
`ConvlutionModule` instance can be used as the argument.
|
123 |
+
dropout_rate (float): Dropout rate.
|
124 |
+
normalize_before (bool):
|
125 |
+
True: use layer_norm before each sub-block.
|
126 |
+
False: use layer_norm after each sub-block.
|
127 |
+
"""
|
128 |
+
|
129 |
+
def __init__(
|
130 |
+
self,
|
131 |
+
size: int,
|
132 |
+
self_attn: torch.nn.Module,
|
133 |
+
feed_forward: Optional[nn.Module] = None,
|
134 |
+
feed_forward_macaron: Optional[nn.Module] = None,
|
135 |
+
conv_module: Optional[nn.Module] = None,
|
136 |
+
dropout_rate: float = 0.1,
|
137 |
+
normalize_before: bool = True,
|
138 |
+
):
|
139 |
+
"""Construct an EncoderLayer object."""
|
140 |
+
super().__init__()
|
141 |
+
self.self_attn = self_attn
|
142 |
+
self.feed_forward = feed_forward
|
143 |
+
self.feed_forward_macaron = feed_forward_macaron
|
144 |
+
self.conv_module = conv_module
|
145 |
+
self.norm_ff = nn.LayerNorm(size, eps=1e-5) # for the FNN module
|
146 |
+
self.norm_mha = nn.LayerNorm(size, eps=1e-5) # for the MHA module
|
147 |
+
if feed_forward_macaron is not None:
|
148 |
+
self.norm_ff_macaron = nn.LayerNorm(size, eps=1e-5)
|
149 |
+
self.ff_scale = 0.5
|
150 |
+
else:
|
151 |
+
self.ff_scale = 1.0
|
152 |
+
if self.conv_module is not None:
|
153 |
+
self.norm_conv = nn.LayerNorm(size, eps=1e-5) # for the CNN module
|
154 |
+
self.norm_final = nn.LayerNorm(
|
155 |
+
size, eps=1e-5) # for the final output of the block
|
156 |
+
self.dropout = nn.Dropout(dropout_rate)
|
157 |
+
self.size = size
|
158 |
+
self.normalize_before = normalize_before
|
159 |
+
|
160 |
+
def forward(
|
161 |
+
self,
|
162 |
+
x: torch.Tensor,
|
163 |
+
mask: torch.Tensor,
|
164 |
+
pos_emb: torch.Tensor,
|
165 |
+
mask_pad: torch.Tensor = torch.ones((0, 0, 0), dtype=torch.bool),
|
166 |
+
att_cache: torch.Tensor = torch.zeros((0, 0, 0, 0)),
|
167 |
+
cnn_cache: torch.Tensor = torch.zeros((0, 0, 0, 0)),
|
168 |
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
|
169 |
+
"""Compute encoded features.
|
170 |
+
|
171 |
+
Args:
|
172 |
+
x (torch.Tensor): (#batch, time, size)
|
173 |
+
mask (torch.Tensor): Mask tensor for the input (#batch, time,time),
|
174 |
+
(0, 0, 0) means fake mask.
|
175 |
+
pos_emb (torch.Tensor): positional encoding, must not be None
|
176 |
+
for ConformerEncoderLayer.
|
177 |
+
mask_pad (torch.Tensor): batch padding mask used for conv module.
|
178 |
+
(#batch, 1,time), (0, 0, 0) means fake mask.
|
179 |
+
att_cache (torch.Tensor): Cache tensor of the KEY & VALUE
|
180 |
+
(#batch=1, head, cache_t1, d_k * 2), head * d_k == size.
|
181 |
+
cnn_cache (torch.Tensor): Convolution cache in conformer layer
|
182 |
+
(#batch=1, size, cache_t2)
|
183 |
+
Returns:
|
184 |
+
torch.Tensor: Output tensor (#batch, time, size).
|
185 |
+
torch.Tensor: Mask tensor (#batch, time, time).
|
186 |
+
torch.Tensor: att_cache tensor,
|
187 |
+
(#batch=1, head, cache_t1 + time, d_k * 2).
|
188 |
+
torch.Tensor: cnn_cahce tensor (#batch, size, cache_t2).
|
189 |
+
"""
|
190 |
+
|
191 |
+
# whether to use macaron style
|
192 |
+
if self.feed_forward_macaron is not None:
|
193 |
+
residual = x
|
194 |
+
if self.normalize_before:
|
195 |
+
x = self.norm_ff_macaron(x)
|
196 |
+
x = residual + self.ff_scale * self.dropout(
|
197 |
+
self.feed_forward_macaron(x))
|
198 |
+
if not self.normalize_before:
|
199 |
+
x = self.norm_ff_macaron(x)
|
200 |
+
# multi-headed self-attention module
|
201 |
+
residual = x
|
202 |
+
if self.normalize_before:
|
203 |
+
x = self.norm_mha(x)
|
204 |
+
x_att, new_att_cache = self.self_attn(x, x, x, mask, pos_emb,
|
205 |
+
att_cache)
|
206 |
+
x = residual + self.dropout(x_att)
|
207 |
+
if not self.normalize_before:
|
208 |
+
x = self.norm_mha(x)
|
209 |
+
|
210 |
+
# convolution module
|
211 |
+
# Fake new cnn cache here, and then change it in conv_module
|
212 |
+
new_cnn_cache = torch.zeros((0, 0, 0), dtype=x.dtype, device=x.device)
|
213 |
+
if self.conv_module is not None:
|
214 |
+
residual = x
|
215 |
+
if self.normalize_before:
|
216 |
+
x = self.norm_conv(x)
|
217 |
+
x, new_cnn_cache = self.conv_module(x, mask_pad, cnn_cache)
|
218 |
+
x = residual + self.dropout(x)
|
219 |
+
|
220 |
+
if not self.normalize_before:
|
221 |
+
x = self.norm_conv(x)
|
222 |
+
|
223 |
+
# feed forward module
|
224 |
+
residual = x
|
225 |
+
if self.normalize_before:
|
226 |
+
x = self.norm_ff(x)
|
227 |
+
|
228 |
+
x = residual + self.ff_scale * self.dropout(self.feed_forward(x))
|
229 |
+
if not self.normalize_before:
|
230 |
+
x = self.norm_ff(x)
|
231 |
+
|
232 |
+
if self.conv_module is not None:
|
233 |
+
x = self.norm_final(x)
|
234 |
+
|
235 |
+
return x, mask, new_att_cache, new_cnn_cache
|
inspiremusic/transformer/label_smoothing_loss.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2019 Shigeki Karita
|
2 |
+
# 2020 Mobvoi Inc (Binbin Zhang)
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
"""Label smoothing module."""
|
16 |
+
|
17 |
+
import torch
|
18 |
+
from torch import nn
|
19 |
+
|
20 |
+
|
21 |
+
class LabelSmoothingLoss(nn.Module):
|
22 |
+
"""Label-smoothing loss.
|
23 |
+
|
24 |
+
In a standard CE loss, the label's data distribution is:
|
25 |
+
[0,1,2] ->
|
26 |
+
[
|
27 |
+
[1.0, 0.0, 0.0],
|
28 |
+
[0.0, 1.0, 0.0],
|
29 |
+
[0.0, 0.0, 1.0],
|
30 |
+
]
|
31 |
+
|
32 |
+
In the smoothing version CE Loss,some probabilities
|
33 |
+
are taken from the true label prob (1.0) and are divided
|
34 |
+
among other labels.
|
35 |
+
|
36 |
+
e.g.
|
37 |
+
smoothing=0.1
|
38 |
+
[0,1,2] ->
|
39 |
+
[
|
40 |
+
[0.9, 0.05, 0.05],
|
41 |
+
[0.05, 0.9, 0.05],
|
42 |
+
[0.05, 0.05, 0.9],
|
43 |
+
]
|
44 |
+
|
45 |
+
Args:
|
46 |
+
size (int): the number of class
|
47 |
+
padding_idx (int): padding class id which will be ignored for loss
|
48 |
+
smoothing (float): smoothing rate (0.0 means the conventional CE)
|
49 |
+
normalize_length (bool):
|
50 |
+
normalize loss by sequence length if True
|
51 |
+
normalize loss by batch size if False
|
52 |
+
"""
|
53 |
+
|
54 |
+
def __init__(self,
|
55 |
+
size: int,
|
56 |
+
padding_idx: int,
|
57 |
+
smoothing: float,
|
58 |
+
normalize_length: bool = False):
|
59 |
+
"""Construct an LabelSmoothingLoss object."""
|
60 |
+
super(LabelSmoothingLoss, self).__init__()
|
61 |
+
self.criterion = nn.KLDivLoss(reduction="none")
|
62 |
+
self.padding_idx = padding_idx
|
63 |
+
self.confidence = 1.0 - smoothing
|
64 |
+
self.smoothing = smoothing
|
65 |
+
self.size = size
|
66 |
+
self.normalize_length = normalize_length
|
67 |
+
|
68 |
+
def forward(self, x: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
|
69 |
+
"""Compute loss between x and target.
|
70 |
+
|
71 |
+
The model outputs and data labels tensors are flatten to
|
72 |
+
(batch*seqlen, class) shape and a mask is applied to the
|
73 |
+
padding part which should not be calculated for loss.
|
74 |
+
|
75 |
+
Args:
|
76 |
+
x (torch.Tensor): prediction (batch, seqlen, class)
|
77 |
+
target (torch.Tensor):
|
78 |
+
target signal masked with self.padding_id (batch, seqlen)
|
79 |
+
Returns:
|
80 |
+
loss (torch.Tensor) : The KL loss, scalar float value
|
81 |
+
"""
|
82 |
+
assert x.size(2) == self.size
|
83 |
+
batch_size = x.size(0)
|
84 |
+
x = x.view(-1, self.size)
|
85 |
+
target = target.view(-1)
|
86 |
+
# use zeros_like instead of torch.no_grad() for true_dist,
|
87 |
+
# since no_grad() can not be exported by JIT
|
88 |
+
true_dist = torch.zeros_like(x)
|
89 |
+
true_dist.fill_(self.smoothing / (self.size - 1))
|
90 |
+
ignore = target == self.padding_idx # (B,)
|
91 |
+
|
92 |
+
total = len(target) - ignore.sum().item()
|
93 |
+
target = target.masked_fill(ignore, 0) # avoid -1 index
|
94 |
+
true_dist.scatter_(1, target.unsqueeze(1), self.confidence)
|
95 |
+
kl = self.criterion(torch.log_softmax(x, dim=1), true_dist)
|
96 |
+
denom = total if self.normalize_length else batch_size
|
97 |
+
return kl.masked_fill(ignore.unsqueeze(1), 0).sum() / denom
|
inspiremusic/transformer/positionwise_feed_forward.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2019 Shigeki Karita
|
2 |
+
# 2020 Mobvoi Inc (Binbin Zhang)
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
"""Positionwise feed forward layer definition."""
|
16 |
+
|
17 |
+
import torch
|
18 |
+
|
19 |
+
|
20 |
+
class PositionwiseFeedForward(torch.nn.Module):
|
21 |
+
"""Positionwise feed forward layer.
|
22 |
+
|
23 |
+
FeedForward are appied on each position of the sequence.
|
24 |
+
The output dim is same with the input dim.
|
25 |
+
|
26 |
+
Args:
|
27 |
+
idim (int): Input dimenstion.
|
28 |
+
hidden_units (int): The number of hidden units.
|
29 |
+
dropout_rate (float): Dropout rate.
|
30 |
+
activation (torch.nn.Module): Activation function
|
31 |
+
"""
|
32 |
+
|
33 |
+
def __init__(
|
34 |
+
self,
|
35 |
+
idim: int,
|
36 |
+
hidden_units: int,
|
37 |
+
dropout_rate: float,
|
38 |
+
activation: torch.nn.Module = torch.nn.ReLU(),
|
39 |
+
):
|
40 |
+
"""Construct a PositionwiseFeedForward object."""
|
41 |
+
super(PositionwiseFeedForward, self).__init__()
|
42 |
+
self.w_1 = torch.nn.Linear(idim, hidden_units)
|
43 |
+
self.activation = activation
|
44 |
+
self.dropout = torch.nn.Dropout(dropout_rate)
|
45 |
+
self.w_2 = torch.nn.Linear(hidden_units, idim)
|
46 |
+
|
47 |
+
def forward(self, xs: torch.Tensor) -> torch.Tensor:
|
48 |
+
"""Forward function.
|
49 |
+
|
50 |
+
Args:
|
51 |
+
xs: input tensor (B, L, D)
|
52 |
+
Returns:
|
53 |
+
output tensor, (B, L, D)
|
54 |
+
"""
|
55 |
+
return self.w_2(self.dropout(self.activation(self.w_1(xs))))
|
56 |
+
|
57 |
+
|
58 |
+
class MoEFFNLayer(torch.nn.Module):
|
59 |
+
"""
|
60 |
+
Mixture of expert with Positionwise feed forward layer
|
61 |
+
See also figure 1 in https://arxiv.org/pdf/2305.15663.pdf
|
62 |
+
The output dim is same with the input dim.
|
63 |
+
|
64 |
+
Modified from https://github.com/Lightning-AI/lit-gpt/pull/823
|
65 |
+
https://github.com/mistralai/mistral-src/blob/b46d6/moe_one_file_ref.py#L203-L219
|
66 |
+
Args:
|
67 |
+
n_expert: number of expert.
|
68 |
+
n_expert_per_token: The actual number of experts used for each frame
|
69 |
+
idim (int): Input dimenstion.
|
70 |
+
hidden_units (int): The number of hidden units.
|
71 |
+
dropout_rate (float): Dropout rate.
|
72 |
+
activation (torch.nn.Module): Activation function
|
73 |
+
"""
|
74 |
+
|
75 |
+
def __init__(
|
76 |
+
self,
|
77 |
+
n_expert: int,
|
78 |
+
n_expert_per_token: int,
|
79 |
+
idim: int,
|
80 |
+
hidden_units: int,
|
81 |
+
dropout_rate: float,
|
82 |
+
activation: torch.nn.Module = torch.nn.ReLU(),
|
83 |
+
):
|
84 |
+
super(MoEFFNLayer, self).__init__()
|
85 |
+
self.gate = torch.nn.Linear(idim, n_expert, bias=False)
|
86 |
+
self.experts = torch.nn.ModuleList(
|
87 |
+
PositionwiseFeedForward(idim, hidden_units, dropout_rate,
|
88 |
+
activation) for _ in range(n_expert))
|
89 |
+
self.n_expert_per_token = n_expert_per_token
|
90 |
+
|
91 |
+
def forward(self, xs: torch.Tensor) -> torch.Tensor:
|
92 |
+
"""Foward function.
|
93 |
+
Args:
|
94 |
+
xs: input tensor (B, L, D)
|
95 |
+
Returns:
|
96 |
+
output tensor, (B, L, D)
|
97 |
+
|
98 |
+
"""
|
99 |
+
B, L, D = xs.size(
|
100 |
+
) # batch size, sequence length, embedding dimension (idim)
|
101 |
+
xs = xs.view(-1, D) # (B*L, D)
|
102 |
+
router = self.gate(xs) # (B*L, n_expert)
|
103 |
+
logits, indices = torch.topk(
|
104 |
+
router, self.n_expert_per_token
|
105 |
+
) # probs:(B*L, n_expert), indices: (B*L, n_expert)
|
106 |
+
weights = torch.nn.functional.softmax(
|
107 |
+
logits, dim=1,
|
108 |
+
dtype=torch.float).to(dtype=xs.dtype) # (B*L, n_expert_per_token)
|
109 |
+
output = torch.zeros_like(xs) # (B*L, D)
|
110 |
+
for i, expert in enumerate(self.experts):
|
111 |
+
mask = indices == i
|
112 |
+
batch_idx, ith_expert = torch.where(mask)
|
113 |
+
output[batch_idx] += weights[batch_idx, ith_expert, None] * expert(
|
114 |
+
xs[batch_idx])
|
115 |
+
return output.view(B, L, D)
|
inspiremusic/transformer/qwen_encoder.py
ADDED
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
|
15 |
+
import torch.nn as nn
|
16 |
+
import torch
|
17 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
18 |
+
from inspiremusic.utils.mask import make_pad_mask
|
19 |
+
from inspiremusic.utils.hinter import hint_once
|
20 |
+
|
21 |
+
class QwenEncoder(nn.Module):
|
22 |
+
def __init__(
|
23 |
+
self,
|
24 |
+
input_size: int,
|
25 |
+
pretrain_path: str = "Qwen/Qwen2.0-0.5B",
|
26 |
+
trainable: bool = False,
|
27 |
+
do_fusion_emb: bool = False,
|
28 |
+
fusion_drop_rate: float = 0.0,
|
29 |
+
):
|
30 |
+
super(QwenEncoder, self).__init__()
|
31 |
+
self.input_size = input_size
|
32 |
+
self.trainable = trainable
|
33 |
+
self.model = AutoModelForCausalLM.from_pretrained(pretrain_path, device_map="cpu")
|
34 |
+
self._output_size = self.model.config.hidden_size
|
35 |
+
self.do_fusion_emb = do_fusion_emb
|
36 |
+
self.hidden_norm = torch.nn.LayerNorm(self._output_size)
|
37 |
+
self.fusion_dropout = nn.Dropout(fusion_drop_rate)
|
38 |
+
if do_fusion_emb:
|
39 |
+
self.fusion_layer = torch.nn.Linear(self._output_size * 2, self._output_size)
|
40 |
+
self.emb_norm = torch.nn.LayerNorm(self._output_size)
|
41 |
+
self.fusion_norm = torch.nn.LayerNorm(self._output_size)
|
42 |
+
from inspiremusic.transformer.activation import Swish
|
43 |
+
self.fusion_act = Swish(self)
|
44 |
+
|
45 |
+
if not self.trainable:
|
46 |
+
self.model.eval()
|
47 |
+
|
48 |
+
def output_size(self) -> int:
|
49 |
+
return self._output_size
|
50 |
+
|
51 |
+
def forward(
|
52 |
+
self,
|
53 |
+
input_ids: torch.Tensor,
|
54 |
+
ilens: torch.Tensor,
|
55 |
+
):
|
56 |
+
device = input_ids.device
|
57 |
+
input_ids = torch.clamp(input_ids, min=0, max=None)
|
58 |
+
input_masks = (~make_pad_mask(ilens)).to(device).long()
|
59 |
+
if not self.trainable:
|
60 |
+
with torch.no_grad():
|
61 |
+
model_outputs = self.model(
|
62 |
+
input_ids=input_ids,
|
63 |
+
attention_mask=input_masks,
|
64 |
+
output_hidden_states=True
|
65 |
+
)
|
66 |
+
else:
|
67 |
+
model_outputs = self.model(
|
68 |
+
input_ids=input_ids,
|
69 |
+
attention_mask=input_masks,
|
70 |
+
output_hidden_states=True
|
71 |
+
)
|
72 |
+
outs = model_outputs.hidden_states[-1]
|
73 |
+
outs = self.hidden_norm(outs)
|
74 |
+
if self.do_fusion_emb:
|
75 |
+
hint_once("fuse embedding and LM outputs", "fuse_emb")
|
76 |
+
outs = self.fusion_dropout(self.fusion_act(outs))
|
77 |
+
emb = model_outputs.hidden_states[0]
|
78 |
+
emb = self.fusion_dropout(self.fusion_act(self.emb_norm(emb)))
|
79 |
+
outs = self.fusion_layer(
|
80 |
+
torch.cat([outs, emb], dim=-1)
|
81 |
+
)
|
82 |
+
outs = self.fusion_act(self.fusion_norm(outs))
|
83 |
+
|
84 |
+
return outs, ilens
|
85 |
+
|
86 |
+
|
87 |
+
class QwenEmbeddingEncoder(nn.Module):
|
88 |
+
def __init__(
|
89 |
+
self,
|
90 |
+
input_size: int,
|
91 |
+
pretrain_path: str = "Qwen/Qwen2.0-0.5B",
|
92 |
+
):
|
93 |
+
super(QwenEmbeddingEncoder, self).__init__()
|
94 |
+
self.input_size = input_size
|
95 |
+
from transformers import Qwen2ForCausalLM
|
96 |
+
self.model = Qwen2ForCausalLM.from_pretrained(pretrain_path, device_map="cpu", attn_implementation="flash_attention_2")
|
97 |
+
self._output_size = self.model.config.hidden_size
|
98 |
+
|
99 |
+
def output_size(self) -> int:
|
100 |
+
return self._output_size
|
101 |
+
|
102 |
+
def forward(
|
103 |
+
self,
|
104 |
+
input_embeds: torch.Tensor,
|
105 |
+
ilens: torch.Tensor,
|
106 |
+
):
|
107 |
+
input_masks = (~make_pad_mask(ilens)).to(input_embeds.device).long()
|
108 |
+
|
109 |
+
outs = self.model(
|
110 |
+
inputs_embeds=input_embeds,
|
111 |
+
attention_mask=input_masks,
|
112 |
+
output_hidden_states=True,
|
113 |
+
return_dict=True,
|
114 |
+
)
|
115 |
+
|
116 |
+
return outs.hidden_states[-1], input_masks
|
117 |
+
|
118 |
+
def forward_one_step(self, xs, masks, cache=None):
|
119 |
+
|
120 |
+
outs = self.model(
|
121 |
+
inputs_embeds=xs,
|
122 |
+
attention_mask=masks,
|
123 |
+
output_hidden_states=True,
|
124 |
+
return_dict=True,
|
125 |
+
use_cache=True,
|
126 |
+
past_key_values=cache,
|
127 |
+
)
|
128 |
+
xs = outs.hidden_states[-1]
|
129 |
+
new_cache = outs.past_key_values
|
130 |
+
|
131 |
+
return xs, masks, new_cache
|
132 |
+
|
133 |
+
|
134 |
+
class QwenInputOnlyEncoder(nn.Module):
|
135 |
+
def __init__(
|
136 |
+
self,
|
137 |
+
input_size: int,
|
138 |
+
pretrain_path: str = "Qwen/Qwen2.0-0.5B",
|
139 |
+
):
|
140 |
+
super(QwenInputOnlyEncoder, self).__init__()
|
141 |
+
self.input_size = input_size
|
142 |
+
from transformers import Qwen2ForCausalLM
|
143 |
+
model = Qwen2ForCausalLM.from_pretrained(pretrain_path, device_map="cpu", attn_implementation="flash_attention_2")
|
144 |
+
self.embed = model.model.embed_tokens
|
145 |
+
for p in self.embed.parameters():
|
146 |
+
p.requires_grad = False
|
147 |
+
# set text embedding to non-trainable
|
148 |
+
|
149 |
+
# self.post_embed = model.model.rotary_emb
|
150 |
+
self._output_size = model.config.hidden_size
|
151 |
+
|
152 |
+
def output_size(self) -> int:
|
153 |
+
return self._output_size
|
154 |
+
|
155 |
+
def forward(
|
156 |
+
self,
|
157 |
+
input_ids: torch.Tensor,
|
158 |
+
ilens: torch.Tensor,
|
159 |
+
):
|
160 |
+
input_masks = (~make_pad_mask(ilens)).to(input_ids.device).long()
|
161 |
+
|
162 |
+
outs = self.embed(input_ids)
|
163 |
+
|
164 |
+
return outs, input_masks
|
165 |
+
|
inspiremusic/transformer/subsampling.py
ADDED
@@ -0,0 +1,384 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2021 Mobvoi Inc (Binbin Zhang, Di Wu)
|
2 |
+
# 2024 Alibaba Inc (Xiang Lyu)
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
# Modified from ESPnet(https://github.com/espnet/espnet)
|
16 |
+
"""Subsampling layer definition."""
|
17 |
+
|
18 |
+
from typing import Tuple, Union
|
19 |
+
|
20 |
+
import torch
|
21 |
+
|
22 |
+
|
23 |
+
class BaseSubsampling(torch.nn.Module):
|
24 |
+
|
25 |
+
def __init__(self):
|
26 |
+
super().__init__()
|
27 |
+
self.right_context = 0
|
28 |
+
self.subsampling_rate = 1
|
29 |
+
|
30 |
+
def position_encoding(self, offset: Union[int, torch.Tensor],
|
31 |
+
size: int) -> torch.Tensor:
|
32 |
+
return self.pos_enc.position_encoding(offset, size)
|
33 |
+
|
34 |
+
|
35 |
+
class EmbedinigNoSubsampling(BaseSubsampling):
|
36 |
+
"""Embedding input without subsampling
|
37 |
+
"""
|
38 |
+
|
39 |
+
def __init__(self, idim: int, odim: int, dropout_rate: float,
|
40 |
+
pos_enc_class: torch.nn.Module):
|
41 |
+
super().__init__()
|
42 |
+
self.embed = torch.nn.Embedding(idim, odim)
|
43 |
+
self.pos_enc = pos_enc_class
|
44 |
+
|
45 |
+
def forward(
|
46 |
+
self,
|
47 |
+
x: torch.Tensor,
|
48 |
+
x_mask: torch.Tensor,
|
49 |
+
offset: Union[int, torch.Tensor] = 0
|
50 |
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
51 |
+
"""Input x.
|
52 |
+
|
53 |
+
Args:
|
54 |
+
x (torch.Tensor): Input tensor (#batch, time, idim).
|
55 |
+
x_mask (torch.Tensor): Input mask (#batch, 1, time).
|
56 |
+
|
57 |
+
Returns:
|
58 |
+
torch.Tensor: linear input tensor (#batch, time', odim),
|
59 |
+
where time' = time .
|
60 |
+
torch.Tensor: linear input mask (#batch, 1, time'),
|
61 |
+
where time' = time .
|
62 |
+
|
63 |
+
"""
|
64 |
+
x = self.embed(x)
|
65 |
+
x, pos_emb = self.pos_enc(x, offset)
|
66 |
+
return x, pos_emb, x_mask
|
67 |
+
|
68 |
+
|
69 |
+
class LinearNoSubsampling(BaseSubsampling):
|
70 |
+
"""Linear transform the input without subsampling
|
71 |
+
|
72 |
+
Args:
|
73 |
+
idim (int): Input dimension.
|
74 |
+
odim (int): Output dimension.
|
75 |
+
dropout_rate (float): Dropout rate.
|
76 |
+
|
77 |
+
"""
|
78 |
+
|
79 |
+
def __init__(self, idim: int, odim: int, dropout_rate: float,
|
80 |
+
pos_enc_class: torch.nn.Module):
|
81 |
+
"""Construct an linear object."""
|
82 |
+
super().__init__()
|
83 |
+
self.out = torch.nn.Sequential(
|
84 |
+
torch.nn.Linear(idim, odim),
|
85 |
+
torch.nn.LayerNorm(odim, eps=1e-5),
|
86 |
+
torch.nn.Dropout(dropout_rate),
|
87 |
+
)
|
88 |
+
self.pos_enc = pos_enc_class
|
89 |
+
self.right_context = 0
|
90 |
+
self.subsampling_rate = 1
|
91 |
+
|
92 |
+
def forward(
|
93 |
+
self,
|
94 |
+
x: torch.Tensor,
|
95 |
+
x_mask: torch.Tensor,
|
96 |
+
offset: Union[int, torch.Tensor] = 0
|
97 |
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
98 |
+
"""Input x.
|
99 |
+
|
100 |
+
Args:
|
101 |
+
x (torch.Tensor): Input tensor (#batch, time, idim).
|
102 |
+
x_mask (torch.Tensor): Input mask (#batch, 1, time).
|
103 |
+
|
104 |
+
Returns:
|
105 |
+
torch.Tensor: linear input tensor (#batch, time', odim),
|
106 |
+
where time' = time .
|
107 |
+
torch.Tensor: linear input mask (#batch, 1, time'),
|
108 |
+
where time' = time .
|
109 |
+
|
110 |
+
"""
|
111 |
+
x = self.out(x)
|
112 |
+
x, pos_emb = self.pos_enc(x, offset)
|
113 |
+
return x, pos_emb, x_mask
|
114 |
+
|
115 |
+
|
116 |
+
class Conv1dSubsampling2(BaseSubsampling):
|
117 |
+
"""Convolutional 1D subsampling (to 1/2 length).
|
118 |
+
It is designed for Whisper, ref:
|
119 |
+
https://github.com/openai/whisper/blob/main/whisper/model.py
|
120 |
+
|
121 |
+
Args:
|
122 |
+
idim (int): Input dimension.
|
123 |
+
odim (int): Output dimension.
|
124 |
+
dropout_rate (float): Dropout rate.
|
125 |
+
|
126 |
+
"""
|
127 |
+
|
128 |
+
def __init__(self, idim: int, odim: int, dropout_rate: float,
|
129 |
+
pos_enc_class: torch.nn.Module):
|
130 |
+
"""Construct an Conv1dSubsampling2 object."""
|
131 |
+
super().__init__()
|
132 |
+
self.conv = torch.nn.Sequential(
|
133 |
+
torch.nn.Conv1d(idim, odim, kernel_size=3, padding=1),
|
134 |
+
torch.nn.GELU(),
|
135 |
+
torch.nn.Conv1d(odim, odim, kernel_size=3, stride=2, padding=1),
|
136 |
+
torch.nn.GELU(),
|
137 |
+
)
|
138 |
+
self.pos_enc = pos_enc_class
|
139 |
+
# The right context for every conv layer is computed by:
|
140 |
+
# (kernel_size - 1) * frame_rate_of_this_layer
|
141 |
+
self.subsampling_rate = 2
|
142 |
+
# 4 = (3 - 1) * 1 + (3 - 1) * 1
|
143 |
+
self.right_context = 4
|
144 |
+
|
145 |
+
def forward(
|
146 |
+
self,
|
147 |
+
x: torch.Tensor,
|
148 |
+
x_mask: torch.Tensor,
|
149 |
+
offset: Union[int, torch.Tensor] = 0
|
150 |
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
151 |
+
"""Subsample x.
|
152 |
+
|
153 |
+
Args:
|
154 |
+
x (torch.Tensor): Input tensor (#batch, time, idim).
|
155 |
+
x_mask (torch.Tensor): Input mask (#batch, 1, time).
|
156 |
+
|
157 |
+
Returns:
|
158 |
+
torch.Tensor: Subsampled tensor (#batch, time', odim),
|
159 |
+
where time' = time // 2.
|
160 |
+
torch.Tensor: Subsampled mask (#batch, 1, time'),
|
161 |
+
where time' = time // 2.
|
162 |
+
torch.Tensor: positional encoding
|
163 |
+
|
164 |
+
"""
|
165 |
+
time = x.size(1)
|
166 |
+
x = x.transpose(1, 2) # (b, f, t)
|
167 |
+
x = self.conv(x)
|
168 |
+
x = x.transpose(1, 2) # (b, t, f)
|
169 |
+
x, pos_emb = self.pos_enc(x, offset)
|
170 |
+
return x, pos_emb, x_mask[:, :, (time + 1) % 2::2]
|
171 |
+
|
172 |
+
|
173 |
+
class Conv2dSubsampling4(BaseSubsampling):
|
174 |
+
"""Convolutional 2D subsampling (to 1/4 length).
|
175 |
+
|
176 |
+
Args:
|
177 |
+
idim (int): Input dimension.
|
178 |
+
odim (int): Output dimension.
|
179 |
+
dropout_rate (float): Dropout rate.
|
180 |
+
|
181 |
+
"""
|
182 |
+
|
183 |
+
def __init__(self, idim: int, odim: int, dropout_rate: float,
|
184 |
+
pos_enc_class: torch.nn.Module):
|
185 |
+
"""Construct an Conv2dSubsampling4 object."""
|
186 |
+
super().__init__()
|
187 |
+
self.conv = torch.nn.Sequential(
|
188 |
+
torch.nn.Conv2d(1, odim, 3, 2),
|
189 |
+
torch.nn.ReLU(),
|
190 |
+
torch.nn.Conv2d(odim, odim, 3, 2),
|
191 |
+
torch.nn.ReLU(),
|
192 |
+
)
|
193 |
+
self.out = torch.nn.Sequential(
|
194 |
+
torch.nn.Linear(odim * (((idim - 1) // 2 - 1) // 2), odim))
|
195 |
+
self.pos_enc = pos_enc_class
|
196 |
+
# The right context for every conv layer is computed by:
|
197 |
+
# (kernel_size - 1) * frame_rate_of_this_layer
|
198 |
+
self.subsampling_rate = 4
|
199 |
+
# 6 = (3 - 1) * 1 + (3 - 1) * 2
|
200 |
+
self.right_context = 6
|
201 |
+
|
202 |
+
def forward(
|
203 |
+
self,
|
204 |
+
x: torch.Tensor,
|
205 |
+
x_mask: torch.Tensor,
|
206 |
+
offset: Union[int, torch.Tensor] = 0
|
207 |
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
208 |
+
"""Subsample x.
|
209 |
+
|
210 |
+
Args:
|
211 |
+
x (torch.Tensor): Input tensor (#batch, time, idim).
|
212 |
+
x_mask (torch.Tensor): Input mask (#batch, 1, time).
|
213 |
+
|
214 |
+
Returns:
|
215 |
+
torch.Tensor: Subsampled tensor (#batch, time', odim),
|
216 |
+
where time' = time // 4.
|
217 |
+
torch.Tensor: Subsampled mask (#batch, 1, time'),
|
218 |
+
where time' = time // 4.
|
219 |
+
torch.Tensor: positional encoding
|
220 |
+
|
221 |
+
"""
|
222 |
+
x = x.unsqueeze(1) # (b, c=1, t, f)
|
223 |
+
x = self.conv(x)
|
224 |
+
b, c, t, f = x.size()
|
225 |
+
x = self.out(x.transpose(1, 2).contiguous().view(b, t, c * f))
|
226 |
+
x, pos_emb = self.pos_enc(x, offset)
|
227 |
+
return x, pos_emb, x_mask[:, :, 2::2][:, :, 2::2]
|
228 |
+
|
229 |
+
|
230 |
+
class Conv2dSubsampling6(BaseSubsampling):
|
231 |
+
"""Convolutional 2D subsampling (to 1/6 length).
|
232 |
+
Args:
|
233 |
+
idim (int): Input dimension.
|
234 |
+
odim (int): Output dimension.
|
235 |
+
dropout_rate (float): Dropout rate.
|
236 |
+
pos_enc (torch.nn.Module): Custom position encoding layer.
|
237 |
+
"""
|
238 |
+
|
239 |
+
def __init__(self, idim: int, odim: int, dropout_rate: float,
|
240 |
+
pos_enc_class: torch.nn.Module):
|
241 |
+
"""Construct an Conv2dSubsampling6 object."""
|
242 |
+
super().__init__()
|
243 |
+
self.conv = torch.nn.Sequential(
|
244 |
+
torch.nn.Conv2d(1, odim, 3, 2),
|
245 |
+
torch.nn.ReLU(),
|
246 |
+
torch.nn.Conv2d(odim, odim, 5, 3),
|
247 |
+
torch.nn.ReLU(),
|
248 |
+
)
|
249 |
+
self.linear = torch.nn.Linear(odim * (((idim - 1) // 2 - 2) // 3),
|
250 |
+
odim)
|
251 |
+
self.pos_enc = pos_enc_class
|
252 |
+
# 10 = (3 - 1) * 1 + (5 - 1) * 2
|
253 |
+
self.subsampling_rate = 6
|
254 |
+
self.right_context = 10
|
255 |
+
|
256 |
+
def forward(
|
257 |
+
self,
|
258 |
+
x: torch.Tensor,
|
259 |
+
x_mask: torch.Tensor,
|
260 |
+
offset: Union[int, torch.Tensor] = 0
|
261 |
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
262 |
+
"""Subsample x.
|
263 |
+
Args:
|
264 |
+
x (torch.Tensor): Input tensor (#batch, time, idim).
|
265 |
+
x_mask (torch.Tensor): Input mask (#batch, 1, time).
|
266 |
+
|
267 |
+
Returns:
|
268 |
+
torch.Tensor: Subsampled tensor (#batch, time', odim),
|
269 |
+
where time' = time // 6.
|
270 |
+
torch.Tensor: Subsampled mask (#batch, 1, time'),
|
271 |
+
where time' = time // 6.
|
272 |
+
torch.Tensor: positional encoding
|
273 |
+
"""
|
274 |
+
x = x.unsqueeze(1) # (b, c, t, f)
|
275 |
+
x = self.conv(x)
|
276 |
+
b, c, t, f = x.size()
|
277 |
+
x = self.linear(x.transpose(1, 2).contiguous().view(b, t, c * f))
|
278 |
+
x, pos_emb = self.pos_enc(x, offset)
|
279 |
+
return x, pos_emb, x_mask[:, :, 2::2][:, :, 4::3]
|
280 |
+
|
281 |
+
|
282 |
+
class Conv2dSubsampling8(BaseSubsampling):
|
283 |
+
"""Convolutional 2D subsampling (to 1/8 length).
|
284 |
+
|
285 |
+
Args:
|
286 |
+
idim (int): Input dimension.
|
287 |
+
odim (int): Output dimension.
|
288 |
+
dropout_rate (float): Dropout rate.
|
289 |
+
|
290 |
+
"""
|
291 |
+
|
292 |
+
def __init__(self, idim: int, odim: int, dropout_rate: float,
|
293 |
+
pos_enc_class: torch.nn.Module):
|
294 |
+
"""Construct an Conv2dSubsampling8 object."""
|
295 |
+
super().__init__()
|
296 |
+
self.conv = torch.nn.Sequential(
|
297 |
+
torch.nn.Conv2d(1, odim, 3, 2),
|
298 |
+
torch.nn.ReLU(),
|
299 |
+
torch.nn.Conv2d(odim, odim, 3, 2),
|
300 |
+
torch.nn.ReLU(),
|
301 |
+
torch.nn.Conv2d(odim, odim, 3, 2),
|
302 |
+
torch.nn.ReLU(),
|
303 |
+
)
|
304 |
+
self.linear = torch.nn.Linear(
|
305 |
+
odim * ((((idim - 1) // 2 - 1) // 2 - 1) // 2), odim)
|
306 |
+
self.pos_enc = pos_enc_class
|
307 |
+
self.subsampling_rate = 8
|
308 |
+
# 14 = (3 - 1) * 1 + (3 - 1) * 2 + (3 - 1) * 4
|
309 |
+
self.right_context = 14
|
310 |
+
|
311 |
+
def forward(
|
312 |
+
self,
|
313 |
+
x: torch.Tensor,
|
314 |
+
x_mask: torch.Tensor,
|
315 |
+
offset: Union[int, torch.Tensor] = 0
|
316 |
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
317 |
+
"""Subsample x.
|
318 |
+
|
319 |
+
Args:
|
320 |
+
x (torch.Tensor): Input tensor (#batch, time, idim).
|
321 |
+
x_mask (torch.Tensor): Input mask (#batch, 1, time).
|
322 |
+
|
323 |
+
Returns:
|
324 |
+
torch.Tensor: Subsampled tensor (#batch, time', odim),
|
325 |
+
where time' = time // 8.
|
326 |
+
torch.Tensor: Subsampled mask (#batch, 1, time'),
|
327 |
+
where time' = time // 8.
|
328 |
+
torch.Tensor: positional encoding
|
329 |
+
"""
|
330 |
+
x = x.unsqueeze(1) # (b, c, t, f)
|
331 |
+
x = self.conv(x)
|
332 |
+
b, c, t, f = x.size()
|
333 |
+
x = self.linear(x.transpose(1, 2).contiguous().view(b, t, c * f))
|
334 |
+
x, pos_emb = self.pos_enc(x, offset)
|
335 |
+
return x, pos_emb, x_mask[:, :, 2::2][:, :, 2::2][:, :, 2::2]
|
336 |
+
|
337 |
+
|
338 |
+
class LegacyLinearNoSubsampling(BaseSubsampling):
|
339 |
+
"""Linear transform the input without subsampling
|
340 |
+
|
341 |
+
Args:
|
342 |
+
idim (int): Input dimension.
|
343 |
+
odim (int): Output dimension.
|
344 |
+
dropout_rate (float): Dropout rate.
|
345 |
+
|
346 |
+
"""
|
347 |
+
|
348 |
+
def __init__(self, idim: int, odim: int, dropout_rate: float,
|
349 |
+
pos_enc_class: torch.nn.Module):
|
350 |
+
"""Construct an linear object."""
|
351 |
+
super().__init__()
|
352 |
+
self.out = torch.nn.Sequential(
|
353 |
+
torch.nn.Linear(idim, odim),
|
354 |
+
torch.nn.LayerNorm(odim, eps=1e-5),
|
355 |
+
torch.nn.Dropout(dropout_rate),
|
356 |
+
torch.nn.ReLU(),
|
357 |
+
)
|
358 |
+
self.pos_enc = pos_enc_class
|
359 |
+
self.right_context = 0
|
360 |
+
self.subsampling_rate = 1
|
361 |
+
|
362 |
+
def forward(
|
363 |
+
self,
|
364 |
+
x: torch.Tensor,
|
365 |
+
x_mask: torch.Tensor,
|
366 |
+
offset: Union[int, torch.Tensor] = 0
|
367 |
+
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
368 |
+
"""Input x.
|
369 |
+
|
370 |
+
Args:
|
371 |
+
x (torch.Tensor): Input tensor (#batch, time, idim).
|
372 |
+
x_mask (torch.Tensor): Input mask (#batch, 1, time).
|
373 |
+
|
374 |
+
Returns:
|
375 |
+
torch.Tensor: linear input tensor (#batch, time', odim),
|
376 |
+
where time' = time .
|
377 |
+
torch.Tensor: linear input mask (#batch, 1, time'),
|
378 |
+
where time' = time .
|
379 |
+
|
380 |
+
"""
|
381 |
+
|
382 |
+
x = self.out(x)
|
383 |
+
x, pos_emb = self.pos_enc(x, offset)
|
384 |
+
return x, pos_emb, x_mask
|
inspiremusic/utils/__init__.py
ADDED
File without changes
|
inspiremusic/utils/audio_utils.py
ADDED
@@ -0,0 +1,623 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024 Alibaba Inc
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
|
15 |
+
#!/usr/bin/env python
|
16 |
+
# -*- coding: utf-8 -*-
|
17 |
+
|
18 |
+
import io
|
19 |
+
import logging
|
20 |
+
import re
|
21 |
+
import sys
|
22 |
+
import inspect
|
23 |
+
import random
|
24 |
+
import typing as tp
|
25 |
+
from functools import partial
|
26 |
+
|
27 |
+
import omegaconf
|
28 |
+
import torch
|
29 |
+
import torchaudio
|
30 |
+
import numpy as np
|
31 |
+
|
32 |
+
from typing_extensions import Literal
|
33 |
+
from typing import (
|
34 |
+
Any,
|
35 |
+
Union,
|
36 |
+
Iterable,
|
37 |
+
List,
|
38 |
+
Dict,
|
39 |
+
Optional,
|
40 |
+
Tuple,
|
41 |
+
)
|
42 |
+
|
43 |
+
from librosa.filters import mel as librosa_mel_fn
|
44 |
+
from scipy.io.wavfile import read
|
45 |
+
|
46 |
+
_BoolLike_co = Union[bool, np.bool_]
|
47 |
+
_IntLike_co = Union[_BoolLike_co, int, "np.integer[Any]"]
|
48 |
+
_FloatLike_co = Union[_IntLike_co, float, "np.floating[Any]"]
|
49 |
+
|
50 |
+
def process_audio(file_path, target_sample_rate=24000):
|
51 |
+
audio, sample_rate = torchaudio.load(file_path)
|
52 |
+
# Check if the audio needs to be resampled
|
53 |
+
if sample_rate != target_sample_rate:
|
54 |
+
audio = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=target_sample_rate)(audio)
|
55 |
+
# Convert stereo to mono (if necessary)
|
56 |
+
audio = audio.mean(dim=0, keepdim=True) if audio.size(0) == 2 else audio
|
57 |
+
return audio, target_sample_rate
|
58 |
+
|
59 |
+
def load_wav(full_path):
|
60 |
+
sampling_rate, data = read(full_path)
|
61 |
+
return data, sampling_rate
|
62 |
+
|
63 |
+
def dynamic_range_compression(x, C=1, clip_val=1e-5):
|
64 |
+
return np.log(np.clip(x, a_min=clip_val, a_max=None) * C)
|
65 |
+
|
66 |
+
|
67 |
+
def dynamic_range_decompression(x, C=1):
|
68 |
+
return np.exp(x) / C
|
69 |
+
|
70 |
+
|
71 |
+
def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):
|
72 |
+
return torch.log(torch.clamp(x, min=clip_val) * C)
|
73 |
+
|
74 |
+
|
75 |
+
def dynamic_range_decompression_torch(x, C=1):
|
76 |
+
return torch.exp(x) / C
|
77 |
+
|
78 |
+
|
79 |
+
def spectral_normalize_torch(magnitudes):
|
80 |
+
output = dynamic_range_compression_torch(magnitudes)
|
81 |
+
return output
|
82 |
+
|
83 |
+
|
84 |
+
def spectral_de_normalize_torch(magnitudes):
|
85 |
+
output = dynamic_range_decompression_torch(magnitudes)
|
86 |
+
return output
|
87 |
+
|
88 |
+
def mel_spectrogram(y, n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax, center=False):
|
89 |
+
if torch.min(y) < -1.0:
|
90 |
+
print("min value is ", torch.min(y))
|
91 |
+
if torch.max(y) > 1.0:
|
92 |
+
print("max value is ", torch.max(y))
|
93 |
+
|
94 |
+
# global mel_basis, hann_window # pylint: disable=global-statement,global-variable-not-assigned
|
95 |
+
mel_basis = {}
|
96 |
+
hann_window = {}
|
97 |
+
if f"{str(fmax)}_{str(y.device)}" not in mel_basis:
|
98 |
+
mel = librosa_mel_fn(sr=sampling_rate, n_fft=n_fft, n_mels=num_mels, fmin=fmin, fmax=fmax)
|
99 |
+
mel_basis[str(fmax) + "_" + str(y.device)] = torch.from_numpy(mel).float().to(y.device)
|
100 |
+
hann_window[str(y.device)] = torch.hann_window(win_size).to(y.device)
|
101 |
+
|
102 |
+
y = torch.nn.functional.pad(
|
103 |
+
y.unsqueeze(1), (int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)), mode="reflect"
|
104 |
+
)
|
105 |
+
y = y.squeeze(1)
|
106 |
+
|
107 |
+
spec = torch.view_as_real(
|
108 |
+
torch.stft(
|
109 |
+
y,
|
110 |
+
n_fft,
|
111 |
+
hop_length=hop_size,
|
112 |
+
win_length=win_size,
|
113 |
+
window=hann_window[str(y.device)],
|
114 |
+
center=center,
|
115 |
+
pad_mode="reflect",
|
116 |
+
normalized=False,
|
117 |
+
onesided=True,
|
118 |
+
return_complex=True,
|
119 |
+
)
|
120 |
+
)
|
121 |
+
|
122 |
+
spec = torch.sqrt(spec.pow(2).sum(-1) + (1e-9))
|
123 |
+
|
124 |
+
spec = torch.matmul(mel_basis[str(fmax) + "_" + str(y.device)], spec)
|
125 |
+
spec = spectral_normalize_torch(spec)
|
126 |
+
|
127 |
+
return spec
|
128 |
+
|
129 |
+
|
130 |
+
def fade_out(audio: torch.Tensor, sample_rate: int,
|
131 |
+
fade_duration: float) -> torch.Tensor:
|
132 |
+
"""
|
133 |
+
Apply a linear fade-out effect to the given audio waveform.
|
134 |
+
|
135 |
+
Parameters:
|
136 |
+
audio (torch.Tensor): The audio waveform tensor.
|
137 |
+
sample_rate (int): Sample rate of the audio.
|
138 |
+
fade_duration (float): Duration of the fade-out effect in seconds.
|
139 |
+
|
140 |
+
Returns:
|
141 |
+
torch.Tensor: The audio with the fade-out effect applied.
|
142 |
+
"""
|
143 |
+
fade_samples = int(fade_duration * sample_rate)
|
144 |
+
|
145 |
+
if fade_samples > audio.shape[1]:
|
146 |
+
fade_samples = audio.shape[
|
147 |
+
1] # use the whole length of audio if necessary
|
148 |
+
|
149 |
+
fade_out_envelope = torch.linspace(1.0, 0.0, fade_samples,
|
150 |
+
dtype=audio.dtype, device=audio.device)
|
151 |
+
|
152 |
+
fade_section = audio[:, -fade_samples:].clone()
|
153 |
+
|
154 |
+
fade_section *= fade_out_envelope
|
155 |
+
|
156 |
+
faded_audio = audio.clone()
|
157 |
+
faded_audio[:, -fade_samples:] = fade_section
|
158 |
+
|
159 |
+
return faded_audio
|
160 |
+
|
161 |
+
def split_wav_into_chunks(num_samples, wav, max_chunk_size, minimum_chunk_size=720):
|
162 |
+
num_chunks = (num_samples + max_chunk_size - 1) // max_chunk_size # Ceiling division
|
163 |
+
wav_chunks = []
|
164 |
+
for i in range(num_chunks):
|
165 |
+
start_idx = i * max_chunk_size
|
166 |
+
end_idx = min(start_idx + max_chunk_size, num_samples)
|
167 |
+
if (end_idx - start_idx) >= minimum_chunk_size:
|
168 |
+
if len(wav.shape) == 2:
|
169 |
+
chunk = wav[:,start_idx:end_idx]
|
170 |
+
else:
|
171 |
+
chunk = wav[start_idx:end_idx]
|
172 |
+
wav_chunks.append(chunk)
|
173 |
+
else:
|
174 |
+
print(f"{num_samples}:{num_chunks}, chunk size={(end_idx - start_idx)} is lower then minimum_chunk_size!")
|
175 |
+
return wav_chunks
|
176 |
+
|
177 |
+
def tiny(x: Union[float, np.ndarray]) -> _FloatLike_co:
|
178 |
+
"""Compute the tiny-value corresponding to an input's data type.
|
179 |
+
"""
|
180 |
+
# Make sure we have an array view
|
181 |
+
x = np.asarray(x)
|
182 |
+
|
183 |
+
# Only floating types generate a tiny
|
184 |
+
if np.issubdtype(x.dtype, np.floating) or np.issubdtype(
|
185 |
+
x.dtype, np.complexfloating
|
186 |
+
):
|
187 |
+
dtype = x.dtype
|
188 |
+
else:
|
189 |
+
dtype = np.dtype(np.float32)
|
190 |
+
|
191 |
+
return np.finfo(dtype).tiny
|
192 |
+
|
193 |
+
def detect_silence(audio, sample_rate, threshold=0.05, min_silence_duration=1):
|
194 |
+
"""
|
195 |
+
Detects the first occurrence of silence in the audio.
|
196 |
+
|
197 |
+
Parameters:
|
198 |
+
audio (Tensor): The audio waveform.
|
199 |
+
sample_rate (int): The sample rate of the audio.
|
200 |
+
threshold (float): The threshold below which the signal is considered silent.
|
201 |
+
min_silence_duration (float): The minimum duration of silence in seconds.
|
202 |
+
|
203 |
+
Returns:
|
204 |
+
int: The timestamp (in samples) where the silence starts.
|
205 |
+
"""
|
206 |
+
# Convert the audio to a numpy array for easier manipulation
|
207 |
+
audio_np = audio.numpy().flatten()
|
208 |
+
# Calculate the energy of the signal
|
209 |
+
energy = np.abs(audio_np)
|
210 |
+
# Find the indices where the energy is below the threshold
|
211 |
+
silent_indices = np.where(energy < threshold)[0]
|
212 |
+
# Find the start and end of contiguous silent regions
|
213 |
+
silent_regions = np.split(silent_indices, np.where(np.diff(silent_indices) != 1)[0] + 1)
|
214 |
+
# Filter out regions that are too short
|
215 |
+
min_silence_samples = int(min_silence_duration * sample_rate)
|
216 |
+
for region in silent_regions:
|
217 |
+
if len(region) >= min_silence_samples:
|
218 |
+
return region[0]
|
219 |
+
|
220 |
+
# If no silence is found, return the length of the audio
|
221 |
+
return len(audio_np)
|
222 |
+
|
223 |
+
def trim_audio(waveform, sample_rate=24000, threshold=0.05, min_silence_duration=1, minimum_silence_start_sample=24000):
|
224 |
+
"""
|
225 |
+
Trims the audio from the beginning to the first occurrence of silence.
|
226 |
+
|
227 |
+
Parameters:
|
228 |
+
waveform (Tensor): The waveform data to the input audio file.
|
229 |
+
sample_rate (int): Sample rate of the input audio file.
|
230 |
+
threshold (float): The threshold below which the signal is considered silent.
|
231 |
+
min_silence_duration (float): The minimum duration of silence in seconds.
|
232 |
+
"""
|
233 |
+
# Detect the first occurrence of silence
|
234 |
+
silence_start_sample = detect_silence(waveform, sample_rate, threshold, min_silence_duration)
|
235 |
+
if silence_start_sample > minimum_silence_start_sample :
|
236 |
+
trimmed_waveform = waveform[:silence_start_sample]
|
237 |
+
else:
|
238 |
+
trimmed_waveform = waveform[:minimum_silence_start_sample]
|
239 |
+
if isinstance(trimmed_waveform, torch.Tensor):
|
240 |
+
return trimmed_waveform
|
241 |
+
else:
|
242 |
+
return trimmed_waveform.unsqueeze()
|
243 |
+
|
244 |
+
def normalize_loudness(wav: torch.Tensor, sample_rate: int, loudness_headroom_db: float = 14,
|
245 |
+
loudness_compressor: bool = False, energy_floor: float = 2e-3):
|
246 |
+
"""Normalize an input signal to a user loudness in dB LKFS.
|
247 |
+
Audio loudness is defined according to the ITU-R BS.1770-4 recommendation.
|
248 |
+
|
249 |
+
Args:
|
250 |
+
wav (torch.Tensor): Input multichannel audio data.
|
251 |
+
sample_rate (int): Sample rate.
|
252 |
+
loudness_headroom_db (float): Target loudness of the output in dB LUFS.
|
253 |
+
loudness_compressor (bool): Uses tanh for soft clipping.
|
254 |
+
energy_floor (float): anything below that RMS level will not be rescaled.
|
255 |
+
Returns:
|
256 |
+
torch.Tensor: Loudness normalized output data.
|
257 |
+
"""
|
258 |
+
energy = wav.pow(2).mean().sqrt().item()
|
259 |
+
if energy < energy_floor:
|
260 |
+
return wav
|
261 |
+
transform = torchaudio.transforms.Loudness(sample_rate)
|
262 |
+
input_loudness_db = transform(wav).item()
|
263 |
+
# calculate the gain needed to scale to the desired loudness level
|
264 |
+
delta_loudness = -loudness_headroom_db - input_loudness_db
|
265 |
+
gain = 10.0 ** (delta_loudness / 20.0)
|
266 |
+
output = gain * wav
|
267 |
+
if loudness_compressor:
|
268 |
+
output = torch.tanh(output)
|
269 |
+
assert output.isfinite().all(), (input_loudness_db, wav.pow(2).mean().sqrt())
|
270 |
+
return output
|
271 |
+
|
272 |
+
def normalize(
|
273 |
+
S: np.ndarray,
|
274 |
+
*,
|
275 |
+
norm: Optional[float] = np.inf,
|
276 |
+
axis: Optional[int] = 0,
|
277 |
+
threshold: Optional[_FloatLike_co] = None,
|
278 |
+
fill: Optional[bool] = None,
|
279 |
+
) -> np.ndarray:
|
280 |
+
"""Normalize an array along a chosen axis.
|
281 |
+
"""
|
282 |
+
# Avoid div-by-zero
|
283 |
+
if threshold is None:
|
284 |
+
threshold = tiny(S)
|
285 |
+
|
286 |
+
elif threshold <= 0:
|
287 |
+
raise ParameterError(f"threshold={threshold} must be strictly positive")
|
288 |
+
|
289 |
+
if fill not in [None, False, True]:
|
290 |
+
raise ParameterError(f"fill={fill} must be None or boolean")
|
291 |
+
|
292 |
+
if not np.isfinite(S).all():
|
293 |
+
raise ParameterError("Input must be finite")
|
294 |
+
|
295 |
+
# All norms only depend on magnitude, let's do that first
|
296 |
+
S = S.numpy()
|
297 |
+
mag = np.abs(S).astype(float)
|
298 |
+
|
299 |
+
# For max/min norms, filling with 1 works
|
300 |
+
fill_norm = 1
|
301 |
+
|
302 |
+
if norm is None:
|
303 |
+
return S
|
304 |
+
|
305 |
+
elif norm == np.inf:
|
306 |
+
length = np.max(mag, axis=axis, keepdims=True)
|
307 |
+
|
308 |
+
elif norm == -np.inf:
|
309 |
+
length = np.min(mag, axis=axis, keepdims=True)
|
310 |
+
|
311 |
+
elif norm == 0:
|
312 |
+
if fill is True:
|
313 |
+
raise ParameterError("Cannot normalize with norm=0 and fill=True")
|
314 |
+
|
315 |
+
length = np.sum(mag > 0, axis=axis, keepdims=True, dtype=mag.dtype)
|
316 |
+
|
317 |
+
elif np.issubdtype(type(norm), np.number) and norm > 0:
|
318 |
+
length = np.sum(mag**norm, axis=axis, keepdims=True) ** (1.0 / norm)
|
319 |
+
|
320 |
+
if axis is None:
|
321 |
+
fill_norm = mag.size ** (-1.0 / norm)
|
322 |
+
else:
|
323 |
+
fill_norm = mag.shape[axis] ** (-1.0 / norm)
|
324 |
+
|
325 |
+
else:
|
326 |
+
raise ParameterError(f"Unsupported norm: {repr(norm)}")
|
327 |
+
|
328 |
+
# indices where norm is below the threshold
|
329 |
+
small_idx = length < threshold
|
330 |
+
|
331 |
+
Snorm = np.empty_like(S)
|
332 |
+
if fill is None:
|
333 |
+
# Leave small indices un-normalized
|
334 |
+
length[small_idx] = 1.0
|
335 |
+
Snorm[:] = S / length
|
336 |
+
|
337 |
+
elif fill:
|
338 |
+
# If we have a non-zero fill value, we locate those entries by
|
339 |
+
# doing a nan-divide.
|
340 |
+
# If S was finite, then length is finite (except for small positions)
|
341 |
+
length[small_idx] = np.nan
|
342 |
+
Snorm[:] = S / length
|
343 |
+
Snorm[np.isnan(Snorm)] = fill_norm
|
344 |
+
else:
|
345 |
+
# Set small values to zero by doing an inf-divide.
|
346 |
+
# This is safe (by IEEE-754) as long as S is finite.
|
347 |
+
length[small_idx] = np.inf
|
348 |
+
Snorm[:] = S / length
|
349 |
+
|
350 |
+
return Snorm
|
351 |
+
|
352 |
+
def normalize_audio(wav: torch.Tensor, normalize: bool = True,
|
353 |
+
strategy: str = 'peak', peak_clip_headroom_db: float = 1,
|
354 |
+
rms_headroom_db: float = 18, loudness_headroom_db: float = 14,
|
355 |
+
loudness_compressor: bool = False, log_clipping: bool = False,
|
356 |
+
sample_rate: tp.Optional[int] = None,
|
357 |
+
stem_name: tp.Optional[str] = None) -> torch.Tensor:
|
358 |
+
"""Normalize the audio according to the prescribed strategy (see after).
|
359 |
+
|
360 |
+
Args:
|
361 |
+
wav (torch.Tensor): Audio data.
|
362 |
+
normalize (bool): if `True` (default), normalizes according to the prescribed
|
363 |
+
strategy (see after). If `False`, the strategy is only used in case clipping
|
364 |
+
would happen.
|
365 |
+
strategy (str): Can be either 'clip', 'peak', or 'rms'. Default is 'peak',
|
366 |
+
i.e. audio is normalized by its largest value. RMS normalizes by root-mean-square
|
367 |
+
with extra headroom to avoid clipping. 'clip' just clips.
|
368 |
+
peak_clip_headroom_db (float): Headroom in dB when doing 'peak' or 'clip' strategy.
|
369 |
+
rms_headroom_db (float): Headroom in dB when doing 'rms' strategy. This must be much larger
|
370 |
+
than the `peak_clip` one to avoid further clipping.
|
371 |
+
loudness_headroom_db (float): Target loudness for loudness normalization.
|
372 |
+
loudness_compressor (bool): If True, uses tanh based soft clipping.
|
373 |
+
log_clipping (bool): If True, basic logging on stderr when clipping still
|
374 |
+
occurs despite strategy (only for 'rms').
|
375 |
+
sample_rate (int): Sample rate for the audio data (required for loudness).
|
376 |
+
stem_name (str, optional): Stem name for clipping logging.
|
377 |
+
Returns:
|
378 |
+
torch.Tensor: Normalized audio.
|
379 |
+
"""
|
380 |
+
scale_peak = 10 ** (-peak_clip_headroom_db / 20)
|
381 |
+
scale_rms = 10 ** (-rms_headroom_db / 20)
|
382 |
+
if strategy == 'peak':
|
383 |
+
rescaling = (scale_peak / wav.abs().max())
|
384 |
+
if normalize or rescaling < 1:
|
385 |
+
wav = wav * rescaling
|
386 |
+
elif strategy == 'clip':
|
387 |
+
wav = wav.clamp(-scale_peak, scale_peak)
|
388 |
+
elif strategy == 'rms':
|
389 |
+
mono = wav.mean(dim=0)
|
390 |
+
rescaling = scale_rms / mono.pow(2).mean().sqrt()
|
391 |
+
if normalize or rescaling < 1:
|
392 |
+
wav = wav * rescaling
|
393 |
+
_clip_wav(wav, log_clipping=log_clipping, stem_name=stem_name)
|
394 |
+
elif strategy == 'loudness':
|
395 |
+
assert sample_rate is not None, "Loudness normalization requires sample rate."
|
396 |
+
wav = normalize_loudness(wav, sample_rate, loudness_headroom_db, loudness_compressor)
|
397 |
+
_clip_wav(wav, log_clipping=log_clipping, stem_name=stem_name)
|
398 |
+
else:
|
399 |
+
assert wav.abs().max() < 1
|
400 |
+
assert strategy == '' or strategy == 'none', f"Unexpected strategy: '{strategy}'"
|
401 |
+
return wav
|
402 |
+
|
403 |
+
|
404 |
+
def f32_pcm(wav: torch.Tensor) -> torch.Tensor:
|
405 |
+
"""
|
406 |
+
Convert audio to float 32 bits PCM format.
|
407 |
+
Args:
|
408 |
+
wav (torch.tensor): Input wav tensor
|
409 |
+
Returns:
|
410 |
+
same wav in float32 PCM format
|
411 |
+
"""
|
412 |
+
if wav.dtype.is_floating_point:
|
413 |
+
return wav
|
414 |
+
elif wav.dtype == torch.int16:
|
415 |
+
return wav.float() / 2**15
|
416 |
+
elif wav.dtype == torch.int32:
|
417 |
+
return wav.float() / 2**31
|
418 |
+
raise ValueError(f"Unsupported wav dtype: {wav.dtype}")
|
419 |
+
|
420 |
+
|
421 |
+
def i16_pcm(wav: torch.Tensor) -> torch.Tensor:
|
422 |
+
"""Convert audio to int 16 bits PCM format.
|
423 |
+
|
424 |
+
..Warning:: There exist many formula for doing this conversion. None are perfect
|
425 |
+
due to the asymmetry of the int16 range. One either have possible clipping, DC offset,
|
426 |
+
or inconsistencies with f32_pcm. If the given wav doesn't have enough headroom,
|
427 |
+
it is possible that `i16_pcm(f32_pcm)) != Identity`.
|
428 |
+
Args:
|
429 |
+
wav (torch.tensor): Input wav tensor
|
430 |
+
Returns:
|
431 |
+
same wav in float16 PCM format
|
432 |
+
"""
|
433 |
+
if wav.dtype.is_floating_point:
|
434 |
+
assert wav.abs().max() <= 1
|
435 |
+
candidate = (wav * 2 ** 15).round()
|
436 |
+
if candidate.max() >= 2 ** 15: # clipping would occur
|
437 |
+
candidate = (wav * (2 ** 15 - 1)).round()
|
438 |
+
return candidate.short()
|
439 |
+
else:
|
440 |
+
assert wav.dtype == torch.int16
|
441 |
+
return wav
|
442 |
+
|
443 |
+
|
444 |
+
def compress(wav: torch.Tensor, sr: int,
|
445 |
+
target_format: tp.Literal["mp3", "ogg", "flac"] = "mp3",
|
446 |
+
bitrate: str = "128k") -> tp.Tuple[torch.Tensor, int]:
|
447 |
+
"""Convert audio wave form to a specified lossy format: mp3, ogg, flac
|
448 |
+
|
449 |
+
Args:
|
450 |
+
wav (torch.Tensor): Input wav tensor.
|
451 |
+
sr (int): Sampling rate.
|
452 |
+
target_format (str): Compression format (e.g., 'mp3').
|
453 |
+
bitrate (str): Bitrate for compression.
|
454 |
+
|
455 |
+
Returns:
|
456 |
+
Tuple of compressed WAV tensor and sampling rate.
|
457 |
+
"""
|
458 |
+
|
459 |
+
# Extract the bit rate from string (e.g., '128k')
|
460 |
+
match = re.search(r"\d+(\.\d+)?", str(bitrate))
|
461 |
+
parsed_bitrate = float(match.group()) if match else None
|
462 |
+
assert parsed_bitrate, f"Invalid bitrate specified (got {parsed_bitrate})"
|
463 |
+
try:
|
464 |
+
# Create a virtual file instead of saving to disk
|
465 |
+
buffer = io.BytesIO()
|
466 |
+
|
467 |
+
torchaudio.save(
|
468 |
+
buffer, wav, sr, format=target_format, bits_per_sample=parsed_bitrate,
|
469 |
+
)
|
470 |
+
# Move to the beginning of the file
|
471 |
+
buffer.seek(0)
|
472 |
+
compressed_wav, sr = torchaudio.load(buffer)
|
473 |
+
return compressed_wav, sr
|
474 |
+
|
475 |
+
except RuntimeError:
|
476 |
+
logger.warning(
|
477 |
+
f"compression failed skipping compression: {format} {parsed_bitrate}"
|
478 |
+
)
|
479 |
+
return wav, sr
|
480 |
+
|
481 |
+
|
482 |
+
def get_mp3(wav_tensor: torch.Tensor, sr: int, bitrate: str = "128k") -> torch.Tensor:
|
483 |
+
"""Convert a batch of audio files to MP3 format, maintaining the original shape.
|
484 |
+
|
485 |
+
This function takes a batch of audio files represented as a PyTorch tensor, converts
|
486 |
+
them to MP3 format using the specified bitrate, and returns the batch in the same
|
487 |
+
shape as the input.
|
488 |
+
|
489 |
+
Args:
|
490 |
+
wav_tensor (torch.Tensor): Batch of audio files represented as a tensor.
|
491 |
+
Shape should be (batch_size, channels, length).
|
492 |
+
sr (int): Sampling rate of the audio.
|
493 |
+
bitrate (str): Bitrate for MP3 conversion, default is '128k'.
|
494 |
+
|
495 |
+
Returns:
|
496 |
+
torch.Tensor: Batch of audio files converted to MP3 format, with the same
|
497 |
+
shape as the input tensor.
|
498 |
+
"""
|
499 |
+
device = wav_tensor.device
|
500 |
+
batch_size, channels, original_length = wav_tensor.shape
|
501 |
+
|
502 |
+
# Flatten tensor for conversion and move to CPU
|
503 |
+
wav_tensor_flat = wav_tensor.view(1, -1).cpu()
|
504 |
+
|
505 |
+
# Convert to MP3 format with specified bitrate
|
506 |
+
wav_tensor_flat, _ = compress(wav_tensor_flat, sr, bitrate=bitrate)
|
507 |
+
|
508 |
+
# Reshape back to original batch format and trim or pad if necessary
|
509 |
+
wav_tensor = wav_tensor_flat.view(batch_size, channels, -1)
|
510 |
+
compressed_length = wav_tensor.shape[-1]
|
511 |
+
if compressed_length > original_length:
|
512 |
+
wav_tensor = wav_tensor[:, :, :original_length] # Trim excess frames
|
513 |
+
elif compressed_length < original_length:
|
514 |
+
padding = torch.zeros(
|
515 |
+
batch_size, channels, original_length - compressed_length, device=device
|
516 |
+
)
|
517 |
+
wav_tensor = torch.cat((wav_tensor, padding), dim=-1) # Pad with zeros
|
518 |
+
|
519 |
+
# Move tensor back to the original device
|
520 |
+
return wav_tensor.to(device)
|
521 |
+
|
522 |
+
|
523 |
+
def get_aac(
|
524 |
+
wav_tensor: torch.Tensor,
|
525 |
+
sr: int,
|
526 |
+
bitrate: str = "128k",
|
527 |
+
lowpass_freq: tp.Optional[int] = None,
|
528 |
+
) -> torch.Tensor:
|
529 |
+
"""Converts a batch of audio tensors to AAC format and then back to tensors.
|
530 |
+
|
531 |
+
This function first saves the input tensor batch as WAV files, then uses FFmpeg to convert
|
532 |
+
these WAV files to AAC format. Finally, it loads the AAC files back into tensors.
|
533 |
+
|
534 |
+
Args:
|
535 |
+
wav_tensor (torch.Tensor): A batch of audio files represented as a tensor.
|
536 |
+
Shape should be (batch_size, channels, length).
|
537 |
+
sr (int): Sampling rate of the audio.
|
538 |
+
bitrate (str): Bitrate for AAC conversion, default is '128k'.
|
539 |
+
lowpass_freq (Optional[int]): Frequency for a low-pass filter. If None, no filter is applied.
|
540 |
+
|
541 |
+
Returns:
|
542 |
+
torch.Tensor: Batch of audio files converted to AAC and back, with the same
|
543 |
+
shape as the input tensor.
|
544 |
+
"""
|
545 |
+
import tempfile
|
546 |
+
import subprocess
|
547 |
+
|
548 |
+
device = wav_tensor.device
|
549 |
+
batch_size, channels, original_length = wav_tensor.shape
|
550 |
+
|
551 |
+
# Parse the bitrate value from the string
|
552 |
+
match = re.search(r"\d+(\.\d+)?", bitrate)
|
553 |
+
parsed_bitrate = (
|
554 |
+
match.group() if match else "128"
|
555 |
+
) # Default to 128 if parsing fails
|
556 |
+
|
557 |
+
# Flatten tensor for conversion and move to CPU
|
558 |
+
wav_tensor_flat = wav_tensor.view(1, -1).cpu()
|
559 |
+
|
560 |
+
with tempfile.NamedTemporaryFile(
|
561 |
+
suffix=".wav"
|
562 |
+
) as f_in, tempfile.NamedTemporaryFile(suffix=".aac") as f_out:
|
563 |
+
input_path, output_path = f_in.name, f_out.name
|
564 |
+
|
565 |
+
# Save the tensor as a WAV file
|
566 |
+
torchaudio.save(input_path, wav_tensor_flat, sr, backend="ffmpeg")
|
567 |
+
|
568 |
+
# Prepare FFmpeg command for AAC conversion
|
569 |
+
command = [
|
570 |
+
"ffmpeg",
|
571 |
+
"-y",
|
572 |
+
"-i",
|
573 |
+
input_path,
|
574 |
+
"-ar",
|
575 |
+
str(sr),
|
576 |
+
"-b:a",
|
577 |
+
f"{parsed_bitrate}k",
|
578 |
+
"-c:a",
|
579 |
+
"aac",
|
580 |
+
]
|
581 |
+
if lowpass_freq is not None:
|
582 |
+
command += ["-cutoff", str(lowpass_freq)]
|
583 |
+
command.append(output_path)
|
584 |
+
|
585 |
+
try:
|
586 |
+
# Run FFmpeg and suppress output
|
587 |
+
subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
588 |
+
|
589 |
+
# Load the AAC audio back into a tensor
|
590 |
+
aac_tensor, _ = torchaudio.load(output_path, backend="ffmpeg")
|
591 |
+
except Exception as exc:
|
592 |
+
raise RuntimeError(
|
593 |
+
"Failed to run command " ".join(command)} "
|
594 |
+
"(Often this means ffmpeg is not installed or the encoder is not supported, "
|
595 |
+
"make sure you installed an older version ffmpeg<5)"
|
596 |
+
) from exc
|
597 |
+
|
598 |
+
original_length_flat = batch_size * channels * original_length
|
599 |
+
compressed_length_flat = aac_tensor.shape[-1]
|
600 |
+
|
601 |
+
# Trim excess frames
|
602 |
+
if compressed_length_flat > original_length_flat:
|
603 |
+
aac_tensor = aac_tensor[:, :original_length_flat]
|
604 |
+
|
605 |
+
# Pad the shortedn frames
|
606 |
+
elif compressed_length_flat < original_length_flat:
|
607 |
+
padding = torch.zeros(
|
608 |
+
1, original_length_flat - compressed_length_flat, device=device
|
609 |
+
)
|
610 |
+
aac_tensor = torch.cat((aac_tensor, padding), dim=-1)
|
611 |
+
|
612 |
+
# Reshape and adjust length to match original tensor
|
613 |
+
wav_tensor = aac_tensor.view(batch_size, channels, -1)
|
614 |
+
compressed_length = wav_tensor.shape[-1]
|
615 |
+
|
616 |
+
assert compressed_length == original_length, (
|
617 |
+
"AAC-compressed audio does not have the same frames as original one. "
|
618 |
+
"One reason can be ffmpeg is not installed and used as proper backed "
|
619 |
+
"for torchaudio, or the AAC encoder is not correct. Run "
|
620 |
+
"`torchaudio.utils.ffmpeg_utils.get_audio_encoders()` and make sure we see entry for"
|
621 |
+
"AAC in the output."
|
622 |
+
)
|
623 |
+
return wav_tensor.to(device)
|
inspiremusic/utils/binary.py
ADDED
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
"""Raw binary format for Encodec compressed audio. Actual compression API is in `encodec.compress`."""
|
7 |
+
import io
|
8 |
+
import json
|
9 |
+
import struct
|
10 |
+
import typing as tp
|
11 |
+
|
12 |
+
# format is `ECDC` magic code, followed by the header size as uint32.
|
13 |
+
# Then an uint8 indicates the protocol version (0.)
|
14 |
+
# The header is then provided as json and should contain all required
|
15 |
+
# informations for decoding. A raw stream of bytes is then provided
|
16 |
+
# and should be interpretable using the json header.
|
17 |
+
_encodec_header_struct = struct.Struct('!4sBI')
|
18 |
+
_ENCODEC_MAGIC = b'ECDC'
|
19 |
+
|
20 |
+
|
21 |
+
def write_ecdc_header(fo: tp.IO[bytes], metadata: tp.Any):
|
22 |
+
meta_dumped = json.dumps(metadata).encode('utf-8')
|
23 |
+
version = 0
|
24 |
+
header = _encodec_header_struct.pack(_ENCODEC_MAGIC, version,
|
25 |
+
len(meta_dumped))
|
26 |
+
fo.write(header)
|
27 |
+
fo.write(meta_dumped)
|
28 |
+
fo.flush()
|
29 |
+
|
30 |
+
|
31 |
+
def _read_exactly(fo: tp.IO[bytes], size: int) -> bytes:
|
32 |
+
buf = b""
|
33 |
+
while len(buf) < size:
|
34 |
+
new_buf = fo.read(size)
|
35 |
+
if not new_buf:
|
36 |
+
raise EOFError("Impossible to read enough data from the stream, "
|
37 |
+
f"{size} bytes remaining.")
|
38 |
+
buf += new_buf
|
39 |
+
size -= len(new_buf)
|
40 |
+
return buf
|
41 |
+
|
42 |
+
|
43 |
+
def read_ecdc_header(fo: tp.IO[bytes]):
|
44 |
+
header_bytes = _read_exactly(fo, _encodec_header_struct.size)
|
45 |
+
magic, version, meta_size = _encodec_header_struct.unpack(header_bytes)
|
46 |
+
if magic != _ENCODEC_MAGIC:
|
47 |
+
raise ValueError("File is not in ECDC format.")
|
48 |
+
if version != 0:
|
49 |
+
raise ValueError("Version not supported.")
|
50 |
+
meta_bytes = _read_exactly(fo, meta_size)
|
51 |
+
return json.loads(meta_bytes.decode('utf-8'))
|
52 |
+
|
53 |
+
|
54 |
+
class BitPacker:
|
55 |
+
"""Simple bit packer to handle ints with a non standard width, e.g. 10 bits.
|
56 |
+
Note that for some bandwidth (1.5, 3), the codebook representation
|
57 |
+
will not cover an integer number of bytes.
|
58 |
+
|
59 |
+
Args:
|
60 |
+
bits (int): number of bits per value that will be pushed.
|
61 |
+
fo (IO[bytes]): file-object to push the bytes to.
|
62 |
+
"""
|
63 |
+
|
64 |
+
def __init__(self, bits: int, fo: tp.IO[bytes]):
|
65 |
+
self._current_value = 0
|
66 |
+
self._current_bits = 0
|
67 |
+
self.bits = bits
|
68 |
+
self.fo = fo
|
69 |
+
|
70 |
+
def push(self, value: int):
|
71 |
+
"""Push a new value to the stream. This will immediately
|
72 |
+
write as many uint8 as possible to the underlying file-object."""
|
73 |
+
self._current_value += (value << self._current_bits)
|
74 |
+
self._current_bits += self.bits
|
75 |
+
while self._current_bits >= 8:
|
76 |
+
lower_8bits = self._current_value & 0xff
|
77 |
+
self._current_bits -= 8
|
78 |
+
self._current_value >>= 8
|
79 |
+
self.fo.write(bytes([lower_8bits]))
|
80 |
+
|
81 |
+
def flush(self):
|
82 |
+
"""Flushes the remaining partial uint8, call this at the end
|
83 |
+
of the stream to encode."""
|
84 |
+
if self._current_bits:
|
85 |
+
self.fo.write(bytes([self._current_value]))
|
86 |
+
self._current_value = 0
|
87 |
+
self._current_bits = 0
|
88 |
+
self.fo.flush()
|
89 |
+
|
90 |
+
|
91 |
+
class BitUnpacker:
|
92 |
+
"""BitUnpacker does the opposite of `BitPacker`.
|
93 |
+
|
94 |
+
Args:
|
95 |
+
bits (int): number of bits of the values to decode.
|
96 |
+
fo (IO[bytes]): file-object to push the bytes to.
|
97 |
+
"""
|
98 |
+
|
99 |
+
def __init__(self, bits: int, fo: tp.IO[bytes]):
|
100 |
+
self.bits = bits
|
101 |
+
self.fo = fo
|
102 |
+
self._mask = (1 << bits) - 1
|
103 |
+
self._current_value = 0
|
104 |
+
self._current_bits = 0
|
105 |
+
|
106 |
+
def pull(self) -> tp.Optional[int]:
|
107 |
+
"""
|
108 |
+
Pull a single value from the stream, potentially reading some
|
109 |
+
extra bytes from the underlying file-object.
|
110 |
+
Returns `None` when reaching the end of the stream.
|
111 |
+
"""
|
112 |
+
while self._current_bits < self.bits:
|
113 |
+
buf = self.fo.read(1)
|
114 |
+
if not buf:
|
115 |
+
return None
|
116 |
+
character = buf[0]
|
117 |
+
self._current_value += character << self._current_bits
|
118 |
+
self._current_bits += 8
|
119 |
+
|
120 |
+
out = self._current_value & self._mask
|
121 |
+
self._current_value >>= self.bits
|
122 |
+
self._current_bits -= self.bits
|
123 |
+
return out
|
124 |
+
|
125 |
+
|
126 |
+
def test():
|
127 |
+
import torch
|
128 |
+
torch.manual_seed(1234)
|
129 |
+
for rep in range(4):
|
130 |
+
length: int = torch.randint(10, 2_000, (1, )).item()
|
131 |
+
bits: int = torch.randint(1, 16, (1, )).item()
|
132 |
+
tokens: tp.List[int] = torch.randint(2**bits, (length, )).tolist()
|
133 |
+
rebuilt: tp.List[int] = []
|
134 |
+
buf = io.BytesIO()
|
135 |
+
packer = BitPacker(bits, buf)
|
136 |
+
for token in tokens:
|
137 |
+
packer.push(token)
|
138 |
+
packer.flush()
|
139 |
+
buf.seek(0)
|
140 |
+
unpacker = BitUnpacker(bits, buf)
|
141 |
+
while True:
|
142 |
+
value = unpacker.pull()
|
143 |
+
if value is None:
|
144 |
+
break
|
145 |
+
rebuilt.append(value)
|
146 |
+
assert len(rebuilt) >= len(tokens), (len(rebuilt), len(tokens))
|
147 |
+
# The flushing mechanism might lead to "ghost" values at the end of the stream.
|
148 |
+
assert len(rebuilt) <= len(tokens) + 8 // bits, (len(rebuilt),
|
149 |
+
len(tokens), bits)
|
150 |
+
for idx, (a, b) in enumerate(zip(tokens, rebuilt)):
|
151 |
+
assert a == b, (idx, a, b)
|
152 |
+
|
153 |
+
|
154 |
+
if __name__ == '__main__':
|
155 |
+
test()
|