chong.zhang commited on
Commit
5827423
·
1 Parent(s): 806cba5
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +39 -0
  2. .gitmodules +3 -0
  3. README.md +5 -6
  4. app.py +237 -4
  5. example/conf/InspireMusic-1.5B-24kHz.yaml +171 -0
  6. example/conf/InspireMusic-1.5B-Long.yaml +171 -0
  7. example/conf/InspireMusic-1.5B.yaml +171 -0
  8. example/conf/InspireMusic-Base-24kHz.yaml +171 -0
  9. example/conf/InspireMusic-Base.yaml +180 -0
  10. inspiremusic/.DS_Store +0 -0
  11. inspiremusic/__init__.py +0 -0
  12. inspiremusic/bin/export_jit.py +74 -0
  13. inspiremusic/bin/export_onnx.py +112 -0
  14. inspiremusic/bin/flow_only_infer.py +150 -0
  15. inspiremusic/bin/inference.py +266 -0
  16. inspiremusic/bin/train.py +194 -0
  17. inspiremusic/cli/__init__.py +0 -0
  18. inspiremusic/cli/frontend.py +100 -0
  19. inspiremusic/cli/inference.py +312 -0
  20. inspiremusic/cli/inspiremusic.py +143 -0
  21. inspiremusic/cli/model.py +295 -0
  22. inspiremusic/dataset/__init__.py +0 -0
  23. inspiremusic/dataset/dataset.py +154 -0
  24. inspiremusic/dataset/processor.py +595 -0
  25. inspiremusic/flow/decoder.py +277 -0
  26. inspiremusic/flow/flow.py +143 -0
  27. inspiremusic/flow/flow_matching.py +167 -0
  28. inspiremusic/flow/length_regulator.py +69 -0
  29. inspiremusic/hifigan/discriminator.py +140 -0
  30. inspiremusic/hifigan/f0_predictor.py +55 -0
  31. inspiremusic/hifigan/generator.py +411 -0
  32. inspiremusic/hifigan/hifigan.py +66 -0
  33. inspiremusic/llm/llm.py +409 -0
  34. inspiremusic/metrics/clap_score.py +135 -0
  35. inspiremusic/metrics/openl3_fd.py +338 -0
  36. inspiremusic/metrics/passt_kld.py +232 -0
  37. inspiremusic/music_tokenizer/__init__.py +0 -0
  38. inspiremusic/music_tokenizer/env.py +29 -0
  39. inspiremusic/music_tokenizer/meldataset.py +226 -0
  40. inspiremusic/music_tokenizer/models.py +548 -0
  41. inspiremusic/music_tokenizer/vqvae.py +58 -0
  42. inspiremusic/text/abs_tokenizer.py +34 -0
  43. inspiremusic/text/tokenizer.py +76 -0
  44. inspiremusic/transformer/__init__.py +0 -0
  45. inspiremusic/transformer/activation.py +84 -0
  46. inspiremusic/transformer/attention.py +328 -0
  47. inspiremusic/transformer/convolution.py +145 -0
  48. inspiremusic/transformer/decoder.py +396 -0
  49. inspiremusic/transformer/decoder_layer.py +132 -0
  50. inspiremusic/transformer/embedding.py +294 -0
.gitattributes CHANGED
@@ -33,3 +33,42 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ example/inspiremusic/inspiremusic_01.wav filter=lfs diff=lfs merge=lfs -text
37
+ example/inspiremusic/inspiremusic_noflow_01.wav filter=lfs diff=lfs merge=lfs -text
38
+ example/inspiremusic/inspiremusic_w_cfm_chorus.wav filter=lfs diff=lfs merge=lfs -text
39
+ example/inspiremusic/inspiremusic_w_cfm_intro.wav filter=lfs diff=lfs merge=lfs -text
40
+ example/inspiremusic/inspiremusic_w_cfm_verse_ras.wav filter=lfs diff=lfs merge=lfs -text
41
+ example/inspiremusic/inspiremusic_w_cfm_verse_topk.wav filter=lfs diff=lfs merge=lfs -text
42
+ example/inspiremusic/inspiremusic_w_cfm_verse.wav filter=lfs diff=lfs merge=lfs -text
43
+ example/inspiremusic/inspiremusic_wo_cfm_chorus.wav filter=lfs diff=lfs merge=lfs -text
44
+ example/inspiremusic/inspiremusic_wo_cfm_intro.wav filter=lfs diff=lfs merge=lfs -text
45
+ example/inspiremusic/inspiremusic_wo_cfm_verse_topk.wav filter=lfs diff=lfs merge=lfs -text
46
+ example/inspiremusic/inspiremusic_wo_cfm_verse.wav filter=lfs diff=lfs merge=lfs -text
47
+ example/ras/chorus/chorus_01.wav filter=lfs diff=lfs merge=lfs -text
48
+ example/ras/chorus/chorus_02.wav filter=lfs diff=lfs merge=lfs -text
49
+ example/ras/chorus/chorus_03.wav filter=lfs diff=lfs merge=lfs -text
50
+ example/ras/chorus/chorus_04.wav filter=lfs diff=lfs merge=lfs -text
51
+ example/ras/chorus/chorus_05.wav filter=lfs diff=lfs merge=lfs -text
52
+ example/ras/chorus/chorus_06.wav filter=lfs diff=lfs merge=lfs -text
53
+ example/ras/chorus/chorus_07.wav filter=lfs diff=lfs merge=lfs -text
54
+ example/ras/chorus/chorus_08.wav filter=lfs diff=lfs merge=lfs -text
55
+ example/ras/chorus/chorus_09.wav filter=lfs diff=lfs merge=lfs -text
56
+ example/ras/chorus/chorus_10.wav filter=lfs diff=lfs merge=lfs -text
57
+ example/ras/intro/intro_01.wav filter=lfs diff=lfs merge=lfs -text
58
+ example/ras/intro/intro_02.wav filter=lfs diff=lfs merge=lfs -text
59
+ example/ras/intro/intro_03.wav filter=lfs diff=lfs merge=lfs -text
60
+ example/ras/intro/intro_04.wav filter=lfs diff=lfs merge=lfs -text
61
+ example/ras/intro/intro_05.wav filter=lfs diff=lfs merge=lfs -text
62
+ example/ras/intro/intro_06.wav filter=lfs diff=lfs merge=lfs -text
63
+ example/ras/outro/outro_01.wav filter=lfs diff=lfs merge=lfs -text
64
+ example/ras/outro/outro_02.wav filter=lfs diff=lfs merge=lfs -text
65
+ example/ras/outro/outro_03.wav filter=lfs diff=lfs merge=lfs -text
66
+ example/ras/outro/outro_04.wav filter=lfs diff=lfs merge=lfs -text
67
+ example/ras/verse/verse_01.wav filter=lfs diff=lfs merge=lfs -text
68
+ example/ras/verse/verse_02.wav filter=lfs diff=lfs merge=lfs -text
69
+ example/ras/verse/verse_03.wav filter=lfs diff=lfs merge=lfs -text
70
+ example/ras/verse/verse_04.wav filter=lfs diff=lfs merge=lfs -text
71
+ example/ras/verse/verse_05.wav filter=lfs diff=lfs merge=lfs -text
72
+ example/ras/verse/verse_06.wav filter=lfs diff=lfs merge=lfs -text
73
+ example/ras/verse/verse_07.wav filter=lfs diff=lfs merge=lfs -text
74
+ example/ras/verse/verse_08.wav filter=lfs diff=lfs merge=lfs -text
.gitmodules ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ [submodule "third_party/Matcha-TTS"]
2
+ path = third_party/Matcha-TTS
3
+ url = https://github.com/shivammehta25/Matcha-TTS.git
README.md CHANGED
@@ -1,14 +1,13 @@
1
  ---
2
  title: InspireMusic
3
- emoji: 🏃
4
- colorFrom: blue
5
- colorTo: blue
6
  sdk: gradio
7
- sdk_version: 5.23.1
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
- short_description: InspireMusic
12
  ---
13
 
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: InspireMusic
3
+ emoji: 🎶
4
+ colorFrom: indigo
5
+ colorTo: purple
6
  sdk: gradio
 
7
  app_file: app.py
8
  pinned: false
9
  license: apache-2.0
10
+ short_description: Music Generation - text to music, music continuation.
11
  ---
12
 
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,7 +1,240 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
1
+ # Copyright (c) 2024 Alibaba Inc (authors: Chong Zhang)
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
+
17
+ os.system('nvidia-smi')
18
+ os.system('apt update -y && apt-get install -y apt-utils && apt install -y unzip')
19
+ os.environ['PYTHONPATH'] = 'third_party/Matcha-TTS'
20
+ os.system('mkdir pretrained_models && cd pretrained_models && git clone https://huggingface.co/FunAudioLLM/InspireMusic-Base.git &&git clone https://huggingface.co/FunAudioLLM/InspireMusic-1.5B-Long.git &&git clone https://huggingface.co/FunAudioLLM/InspireMusic-1.5B.git &&git clone https://huggingface.co/FunAudioLLM/InspireMusic-1.5B-24kHz.git &&git clone https://huggingface.co/FunAudioLLM/InspireMusic-Base-24kHz.git && for i in InspireMusic-Base InspireMusic-Base-24kHz InspireMusic-1.5B InspireMusic-1.5B-24kHz InspireMusic-1.5B-Long; do sed -i -e "s/\.\.\/\.\.\///g" ${i}/inspiremusic.yaml; done && cd ..')
21
+
22
+ import sys
23
+ import torch
24
+ print(torch.backends.cudnn.version())
25
+
26
+ ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
27
+ sys.path.append('{}/third_party/Matcha-TTS'.format(ROOT_DIR))
28
+
29
+ import spaces
30
  import gradio as gr
31
+ from inspiremusic.cli.inference import InspireMusicUnified, set_env_variables
32
+ import torchaudio
33
+ import datetime
34
+ import hashlib
35
+ import threading
36
+ import time
37
+ import importlib
38
+
39
+ MODELS = ["InspireMusic-1.5B-Long", "InspireMusic-1.5B", "InspireMusic-Base", "InspireMusic-1.5B-24kHz", "InspireMusic-Base-24kHz"]
40
+ AUDIO_PROMPT_DIR = "demo/audio_prompts"
41
+ OUTPUT_AUDIO_DIR = "demo/outputs"
42
+
43
+ DEMO_TEXT_PROMPTS = ["Jazz music with drum beats.",
44
+ "A captivating classical piano performance, this piece exudes a dynamic and intense atmosphere, showcasing intricate and expressive instrumental artistry.",
45
+ "A soothing instrumental piece blending elements of light music and pop, featuring a gentle guitar rendition. The overall feel is serene and reflective, likely instrumental with no vocals.",
46
+ "The instrumental rock piece features dynamic oscillations and wave-like progressions, creating an immersive and energetic atmosphere. The music is purely instrumental, with no vocals, and it blends elements of rock and post-rock for a powerful and evocative experience.",
47
+ "The classical instrumental piece exudes a haunting and evocative atmosphere, characterized by its intricate guitar work and profound emotional depth.",
48
+ "Experience a dynamic blend of instrumental electronic music with futuristic house vibes, featuring energetic beats and a captivating rhythm. The tracks are likely instrumental, focusing on the immersive soundscapes rather than vocal performances."]
49
+
50
+ # Shared flag to control the process
51
+ stop_flag = threading.Event()
52
+
53
+ def cancel_process():
54
+ """
55
+ Sets the stop_flag to stop the long-running process.
56
+ """
57
+ stop_flag.set()
58
+ return "Cancellation requested. Please wait for the process to stop."
59
+
60
+ def generate_filename():
61
+ hash_object = hashlib.sha256(str(int(datetime.datetime.now().timestamp())).encode())
62
+ hash_string = hash_object.hexdigest()
63
+ return hash_string
64
+
65
+ def get_args(
66
+ task, text="", audio=None, model_name="InspireMusic-Base",
67
+ chorus="intro",
68
+ output_sample_rate=48000, max_generate_audio_seconds=30.0, time_start = 0.0, time_end=30.0, trim=False):
69
+
70
+ if "24kHz" in model_name:
71
+ output_sample_rate = 24000
72
+
73
+ if output_sample_rate == 24000:
74
+ fast = True
75
+ else:
76
+ fast = False
77
+ # This function constructs the arguments required for InspireMusic
78
+ args = {
79
+ "task" : task,
80
+ "text" : text,
81
+ "audio_prompt" : audio,
82
+ "model_name" : model_name,
83
+ "chorus" : chorus,
84
+ "fast" : fast,
85
+ "fade_out" : True,
86
+ "trim" : trim,
87
+ "output_sample_rate" : output_sample_rate,
88
+ "min_generate_audio_seconds": 10.0,
89
+ "max_generate_audio_seconds": max_generate_audio_seconds,
90
+ "max_audio_prompt_length": 5.0,
91
+ "model_dir" : os.path.join("pretrained_models",
92
+ model_name),
93
+ "result_dir" : OUTPUT_AUDIO_DIR,
94
+ "output_fn" : generate_filename(),
95
+ "format" : "wav",
96
+ "time_start" : time_start,
97
+ "time_end": time_end,
98
+ "fade_out_duration": 1.0,
99
+ }
100
+
101
+ if args["time_start"] is None:
102
+ args["time_start"] = 0.0
103
+ args["time_end"] = args["time_start"] + args["max_generate_audio_seconds"]
104
+
105
+ print(args)
106
+ return args
107
+
108
+
109
+ def trim_audio(audio_file, cut_seconds=5):
110
+ audio, sr = torchaudio.load(audio_file)
111
+ num_samples = cut_seconds * sr
112
+ cutted_audio = audio[:, :num_samples]
113
+ output_path = os.path.join(AUDIO_PROMPT_DIR, "audio_prompt_" + generate_filename() + ".wav")
114
+ torchaudio.save(output_path, cutted_audio, sr)
115
+ return output_path
116
+
117
+ @spaces.GPU(duration=120)
118
+ def music_generation(args):
119
+ set_env_variables()
120
+ model = InspireMusicUnified(
121
+ model_name=args["model_name"],
122
+ model_dir=args["model_dir"],
123
+ min_generate_audio_seconds=args["min_generate_audio_seconds"],
124
+ max_generate_audio_seconds=args["max_generate_audio_seconds"],
125
+ sample_rate=24000,
126
+ output_sample_rate=args["output_sample_rate"],
127
+ load_jit=True,
128
+ load_onnx=False,
129
+ fast=args["fast"],
130
+ result_dir=args["result_dir"])
131
+
132
+ output_path = model.inference(
133
+ task=args["task"],
134
+ text=args["text"],
135
+ audio_prompt=args["audio_prompt"],
136
+ chorus=args["chorus"],
137
+ time_start=args["time_start"],
138
+ time_end=args["time_end"],
139
+ output_fn=args["output_fn"],
140
+ max_audio_prompt_length=args["max_audio_prompt_length"],
141
+ fade_out_duration=args["fade_out_duration"],
142
+ output_format=args["format"],
143
+ fade_out_mode=args["fade_out"],
144
+ trim=args["trim"])
145
+ return output_path
146
+
147
+
148
+ def demo_inspiremusic_t2m(text, model_name, chorus,
149
+ output_sample_rate, max_generate_audio_seconds):
150
+ args = get_args(
151
+ task='text-to-music', text=text, audio=None,
152
+ model_name=model_name, chorus=chorus,
153
+ output_sample_rate=output_sample_rate,
154
+ max_generate_audio_seconds=max_generate_audio_seconds)
155
+ return music_generation(args)
156
+
157
+ def demo_inspiremusic_con(text, audio, model_name, chorus,
158
+ output_sample_rate, max_generate_audio_seconds):
159
+ args = get_args(
160
+ task='continuation', text=text, audio=trim_audio(audio, cut_seconds=5),
161
+ model_name=model_name, chorus=chorus,
162
+ output_sample_rate=output_sample_rate,
163
+ max_generate_audio_seconds=max_generate_audio_seconds)
164
+ return music_generation(args)
165
+
166
+ def process(args, progress=gr.Progress()):
167
+ progress(0, desc="Starting process...")
168
+ idx = 1
169
+ for i in range(idx):
170
+ if stop_flag.is_set():
171
+ progress(i / idx, desc="Process canceled.")
172
+ break
173
+ music_generation(args)
174
+ time.sleep(1)
175
+ progress((i + 1) / idx, desc=f"Processing step {i + 1}/{idx}")
176
+ return "Process completed successfully."
177
+
178
+ def main():
179
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
180
+ gr.Markdown("""
181
+ # InspireMusic
182
+ - Support music generation tasks with long-form and high audio quality, sampling rates up to 48kHz.
183
+ - Github: https://github.com/FunAudioLLM/InspireMusic/ | ModelScope Studio: https://modelscope.cn/studios/iic/InspireMusic
184
+ - Available music generation models: [InspireMusic-1.5B-Long](https://huggingface.co/FunAudioLLM/InspireMusic-1.5B-Long), [InspireMusic-1.5B](https://huggingface.co/FunAudioLLM/InspireMusic-1.5B), [InspireMusic-Base](https://huggingface.co/FunAudioLLM/InspireMusic-Base), [InspireMusic-1.5B-24kHz](https://huggingface.co/FunAudioLLM/InspireMusic-1.5B-24kHz), [InspireMusic-Base-24kHz](https://huggingface.co/FunAudioLLM/InspireMusic-Base-24kHz). Both on Huggingface and ModelScope.
185
+ - Currently only support English text prompts.
186
+ - This page is for demo purpose, if you want to generate long-form audio, e.g., 5mins, please try to deploy locally. Thank you for your support.
187
+ """)
188
+
189
+ with gr.Row(equal_height=True):
190
+ model_name = gr.Dropdown(
191
+ MODELS, label="Select Model Name",
192
+ value="InspireMusic-1.5B-Long")
193
+ chorus = gr.Dropdown(["intro", "verse", "chorus", "outro"],
194
+ label="Chorus Mode", value="intro")
195
+ output_sample_rate = gr.Dropdown([48000, 24000],
196
+ label="Output Audio Sample Rate (Hz)",
197
+ value=48000)
198
+ max_generate_audio_seconds = gr.Slider(10, 300,
199
+ label="Generate Audio Length (s)",
200
+ value=30)
201
+
202
+ with gr.Row(equal_height=True):
203
+ text_input = gr.Textbox(label="Input Text (For Text-to-Music Task)",
204
+ value="Experience soothing and sensual instrumental jazz with a touch of Bossa Nova, perfect for a relaxing restaurant or spa ambiance.")
205
+
206
+ audio_input = gr.Audio(
207
+ label="Input Audio Prompt (For Music Continuation Task)",
208
+ type="filepath")
209
+ music_output = gr.Audio(label="Generated Music", type="filepath", autoplay=True, show_download_button = True)
210
+
211
+ with gr.Row():
212
+ button = gr.Button("Submit Text-to-Music Task")
213
+ button.click(demo_inspiremusic_t2m,
214
+ inputs=[text_input, model_name,
215
+ chorus,
216
+ output_sample_rate,
217
+ max_generate_audio_seconds],
218
+ outputs=music_output)
219
+
220
+ generate_button = gr.Button("Submit Music Continuation Task")
221
+ generate_button.click(demo_inspiremusic_con,
222
+ inputs=[text_input, audio_input, model_name,
223
+ chorus,
224
+ output_sample_rate,
225
+ max_generate_audio_seconds],
226
+ outputs=music_output)
227
+ cancel_button = gr.Button("Cancel")
228
 
229
+ cancel_button.click(
230
+ fn=cancel_process,
231
+ inputs=[],
232
+ outputs="Cancel process."
233
+ )
234
+ t2m_examples = gr.Examples(examples=DEMO_TEXT_PROMPTS, inputs=[text_input])
235
+ demo.launch()
236
 
237
+ if __name__ == '__main__':
238
+ os.makedirs(AUDIO_PROMPT_DIR, exist_ok=True)
239
+ os.makedirs(OUTPUT_AUDIO_DIR, exist_ok=True)
240
+ main()
example/conf/InspireMusic-1.5B-24kHz.yaml ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # set random seed, so that you may reproduce your result.
2
+ __set_seed1: !apply:random.seed [1024]
3
+ __set_seed2: !apply:numpy.random.seed [1024]
4
+ __set_seed3: !apply:torch.manual_seed [1024]
5
+ __set_seed4: !apply:torch.cuda.manual_seed_all [1024]
6
+
7
+ # fixed params
8
+ sample_rate: 24000
9
+ text_encoder_input_size: 512
10
+ llm_input_size: 1536
11
+ llm_output_size: 1536
12
+
13
+ basemodel_path: 'pretrained_models/InspireMusic-1.5B-24kHz/'
14
+ generator_path: 'pretrained_models/InspireMusic-1.5B-24kHz/music_tokenizer'
15
+
16
+ # model params
17
+ # for all class/function included in this repo, we use !<name> or !<new> for intialization, so that user may find all corresponding class/function according to one single yaml.
18
+ # for system/third_party class/function, we do not require this.
19
+ llm: !new:inspiremusic.llm.llm.LLM
20
+ text_encoder_input_size: !ref <text_encoder_input_size>
21
+ llm_input_size: !ref <llm_input_size>
22
+ llm_output_size: !ref <llm_output_size>
23
+ audio_token_size: 4096
24
+ length_normalized_loss: True
25
+ lsm_weight: 0
26
+ text_encoder_conf:
27
+ name: "none"
28
+ llm: !new:inspiremusic.transformer.qwen_encoder.QwenEmbeddingEncoder
29
+ input_size: !ref <text_encoder_input_size>
30
+ pretrain_path: !ref <basemodel_path>
31
+
32
+ sampling: !name:inspiremusic.utils.common.topk_sampling
33
+ top_k: 350
34
+ train_cfg_ratio: 0.2
35
+ infer_cfg_ratio: 3.0
36
+ flow: !new:inspiremusic.flow.flow.MaskedDiff
37
+ input_size: 256
38
+ output_size: 80
39
+ output_type: 'mel'
40
+ vocab_size: 4096
41
+ input_frame_rate: 75
42
+ only_mask_loss: True
43
+ encoder: !new:inspiremusic.transformer.encoder.ConformerEncoder
44
+ output_size: 512
45
+ attention_heads: 4
46
+ linear_units: 1024
47
+ num_blocks: 3
48
+ dropout_rate: 0.1
49
+ positional_dropout_rate: 0.1
50
+ attention_dropout_rate: 0.1
51
+ normalize_before: True
52
+ input_layer: 'linear'
53
+ pos_enc_layer_type: 'rel_pos_espnet'
54
+ selfattention_layer_type: 'rel_selfattn'
55
+ input_size: 256
56
+ use_cnn_module: False
57
+ macaron_style: False
58
+ length_regulator: !new:inspiremusic.flow.length_regulator.InterpolateRegulator
59
+ channels: 512
60
+ sampling_ratios: [1, 1, 1, 1]
61
+ decoder: !new:inspiremusic.flow.flow_matching.ConditionalCFM
62
+ in_channels: 240
63
+ cfm_params: !new:omegaconf.DictConfig
64
+ content:
65
+ sigma_min: 1e-06
66
+ solver: 'euler'
67
+ t_scheduler: 'cosine'
68
+ training_cfg_rate: 0.2
69
+ inference_cfg_rate: 0.7
70
+ reg_loss_type: 'l1'
71
+ estimator: !new:inspiremusic.flow.decoder.ConditionalDecoder
72
+ in_channels: 1024
73
+ out_channels: 512
74
+ channels: [256, 256]
75
+ dropout: 0.0
76
+ attention_head_dim: 64
77
+ n_blocks: 4
78
+ num_mid_blocks: 8
79
+ num_heads: 8
80
+ act_fn: 'gelu'
81
+ generator_model_dir: !ref <generator_path>
82
+
83
+ hift: !new:inspiremusic.hifigan.generator.HiFTGenerator
84
+ in_channels: 80
85
+ base_channels: 512
86
+ nb_harmonics: 8
87
+ sampling_rate: !ref <sample_rate>
88
+ nsf_alpha: 0.1
89
+ nsf_sigma: 0.003
90
+ nsf_voiced_threshold: 10
91
+ upsample_rates: [8, 8]
92
+ upsample_kernel_sizes: [16, 16]
93
+ istft_params:
94
+ n_fft: 16
95
+ hop_len: 4
96
+ resblock_kernel_sizes: [3, 7, 11]
97
+ resblock_dilation_sizes: [[1, 3, 5], [1, 3, 5], [1, 3, 5]]
98
+ source_resblock_kernel_sizes: [7, 11]
99
+ source_resblock_dilation_sizes: [[1, 3, 5], [1, 3, 5]]
100
+ lrelu_slope: 0.1
101
+ audio_limit: 0.99
102
+ f0_predictor: !new:inspiremusic.hifigan.f0_predictor.ConvRNNF0Predictor
103
+ num_class: 1
104
+ in_channels: 80
105
+ cond_channels: 512
106
+
107
+ wavtokenizer: !new:inspiremusic.hifigan.generator.HiFTGenerator
108
+
109
+ # processor functions
110
+ parquet_opener: !name:inspiremusic.dataset.processor.parquet_opener
111
+ get_tokenizer: !name:inspiremusic.text.tokenizer.get_tokenizer
112
+ tokenizer_path: !ref <basemodel_path>
113
+ tokenizer_name: "qwen-2.5"
114
+ allowed_special: 'all'
115
+ tokenize: !name:inspiremusic.dataset.processor.tokenize
116
+ get_tokenizer: !ref <get_tokenizer>
117
+ allowed_special: !ref <allowed_special>
118
+ filter: !name:inspiremusic.dataset.processor.filter
119
+ max_length: 28000
120
+ min_length: 0
121
+ token_max_length: 200
122
+ token_min_length: 1
123
+ resample: !name:inspiremusic.dataset.processor.resample
124
+ resample_rate: !ref <sample_rate>
125
+ feat_extractor: !name:matcha.utils.audio.mel_spectrogram
126
+ n_fft: 1024
127
+ num_mels: 128
128
+ sampling_rate: !ref <sample_rate>
129
+ hop_size: 256
130
+ win_size: 1024
131
+ fmin: 0
132
+ fmax: 24000
133
+ center: False
134
+ compute_fbank: !name:inspiremusic.dataset.processor.compute_fbank
135
+ feat_extractor: !ref <feat_extractor>
136
+ parse_embedding: !name:inspiremusic.dataset.processor.parse_embedding
137
+ normalize: True
138
+ shuffle: !name:inspiremusic.dataset.processor.shuffle
139
+ shuffle_size: 1000
140
+ sort: !name:inspiremusic.dataset.processor.sort
141
+ sort_size: 500 # sort_size should be less than shuffle_size
142
+ batch: !name:inspiremusic.dataset.processor.batch
143
+ batch_type: 'dynamic'
144
+ max_frames_in_batch: 10000 # llm 12000
145
+ padding: !name:inspiremusic.dataset.processor.padding
146
+
147
+ # dataset processor pipeline
148
+ data_pipeline: [
149
+ !ref <parquet_opener>,
150
+ !ref <tokenize>,
151
+ !ref <shuffle>,
152
+ !ref <sort>,
153
+ !ref <filter>,
154
+ !ref <batch>,
155
+ !ref <padding>,
156
+ ]
157
+
158
+
159
+ # train conf
160
+ train_conf:
161
+ optim: adam
162
+ optim_conf:
163
+ lr: 0.0001 # change to 0.001 if you want to train flow from scratch
164
+ scheduler: warmuplr
165
+ scheduler_conf:
166
+ warmup_steps: 5000
167
+ max_epoch: 200
168
+ grad_clip: 5
169
+ accum_grad: 2
170
+ log_interval: 100
171
+ save_per_step: 500
example/conf/InspireMusic-1.5B-Long.yaml ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # set random seed, so that you may reproduce your result.
2
+ __set_seed1: !apply:random.seed [1988]
3
+ __set_seed2: !apply:numpy.random.seed [1988]
4
+ __set_seed3: !apply:torch.manual_seed [1988]
5
+ __set_seed4: !apply:torch.cuda.manual_seed_all [1988]
6
+
7
+ # fixed params
8
+ sample_rate: 24000
9
+ text_encoder_input_size: 512
10
+ llm_input_size: 1536
11
+ llm_output_size: 1536
12
+
13
+ basemodel_path: 'pretrained_models/InspireMusic-1.5B-Long/'
14
+ generator_path: 'pretrained_models/InspireMusic-1.5B-Long/music_tokenizer'
15
+
16
+ # model params
17
+ # for all class/function included in this repo, we use !<name> or !<new> for intialization, so that user may find all corresponding class/function according to one single yaml.
18
+ # for system/third_party class/function, we do not require this.
19
+ llm: !new:inspiremusic.llm.llm.LLM
20
+ text_encoder_input_size: !ref <text_encoder_input_size>
21
+ llm_input_size: !ref <llm_input_size>
22
+ llm_output_size: !ref <llm_output_size>
23
+ audio_token_size: 4096
24
+ length_normalized_loss: True
25
+ lsm_weight: 0
26
+ text_encoder_conf:
27
+ name: "none"
28
+ llm: !new:inspiremusic.transformer.qwen_encoder.QwenEmbeddingEncoder
29
+ input_size: !ref <text_encoder_input_size>
30
+ pretrain_path: !ref <basemodel_path>
31
+
32
+ sampling: !name:inspiremusic.utils.common.topk_sampling
33
+ top_k: 350
34
+ train_cfg_ratio: 0.2
35
+ infer_cfg_ratio: 3.0
36
+ flow: !new:inspiremusic.flow.flow.MaskedDiff
37
+ input_size: 256
38
+ output_size: 80
39
+ output_type: 'mel'
40
+ vocab_size: 4096
41
+ input_frame_rate: 75
42
+ only_mask_loss: True
43
+ encoder: !new:inspiremusic.transformer.encoder.ConformerEncoder
44
+ output_size: 512
45
+ attention_heads: 4
46
+ linear_units: 1024
47
+ num_blocks: 3
48
+ dropout_rate: 0.1
49
+ positional_dropout_rate: 0.1
50
+ attention_dropout_rate: 0.1
51
+ normalize_before: True
52
+ input_layer: 'linear'
53
+ pos_enc_layer_type: 'rel_pos_espnet'
54
+ selfattention_layer_type: 'rel_selfattn'
55
+ input_size: 256
56
+ use_cnn_module: False
57
+ macaron_style: False
58
+ length_regulator: !new:inspiremusic.flow.length_regulator.InterpolateRegulator
59
+ channels: 512
60
+ sampling_ratios: [1, 1, 1, 1]
61
+ decoder: !new:inspiremusic.flow.flow_matching.ConditionalCFM
62
+ in_channels: 240
63
+ cfm_params: !new:omegaconf.DictConfig
64
+ content:
65
+ sigma_min: 1e-06
66
+ solver: 'euler'
67
+ t_scheduler: 'cosine'
68
+ training_cfg_rate: 0.2
69
+ inference_cfg_rate: 0.7
70
+ reg_loss_type: 'l1'
71
+ estimator: !new:inspiremusic.flow.decoder.ConditionalDecoder
72
+ in_channels: 1024
73
+ out_channels: 512
74
+ channels: [256, 256]
75
+ dropout: 0.0
76
+ attention_head_dim: 64
77
+ n_blocks: 4
78
+ num_mid_blocks: 8
79
+ num_heads: 8
80
+ act_fn: 'gelu'
81
+ generator_model_dir: !ref <generator_path>
82
+
83
+ hift: !new:inspiremusic.hifigan.generator.HiFTGenerator
84
+ in_channels: 80
85
+ base_channels: 512
86
+ nb_harmonics: 8
87
+ sampling_rate: !ref <sample_rate>
88
+ nsf_alpha: 0.1
89
+ nsf_sigma: 0.003
90
+ nsf_voiced_threshold: 10
91
+ upsample_rates: [8, 8]
92
+ upsample_kernel_sizes: [16, 16]
93
+ istft_params:
94
+ n_fft: 16
95
+ hop_len: 4
96
+ resblock_kernel_sizes: [3, 7, 11]
97
+ resblock_dilation_sizes: [[1, 3, 5], [1, 3, 5], [1, 3, 5]]
98
+ source_resblock_kernel_sizes: [7, 11]
99
+ source_resblock_dilation_sizes: [[1, 3, 5], [1, 3, 5]]
100
+ lrelu_slope: 0.1
101
+ audio_limit: 0.99
102
+ f0_predictor: !new:inspiremusic.hifigan.f0_predictor.ConvRNNF0Predictor
103
+ num_class: 1
104
+ in_channels: 80
105
+ cond_channels: 512
106
+
107
+ wavtokenizer: !new:inspiremusic.hifigan.generator.HiFTGenerator
108
+
109
+ # processor functions
110
+ parquet_opener: !name:inspiremusic.dataset.processor.parquet_opener
111
+ get_tokenizer: !name:inspiremusic.text.tokenizer.get_tokenizer
112
+ tokenizer_path: !ref <basemodel_path>
113
+ tokenizer_name: "qwen-2.5"
114
+ allowed_special: 'all'
115
+ tokenize: !name:inspiremusic.dataset.processor.tokenize
116
+ get_tokenizer: !ref <get_tokenizer>
117
+ allowed_special: !ref <allowed_special>
118
+ filter: !name:inspiremusic.dataset.processor.filter
119
+ max_length: 28000
120
+ min_length: 0
121
+ token_max_length: 200
122
+ token_min_length: 1
123
+ resample: !name:inspiremusic.dataset.processor.resample
124
+ resample_rate: !ref <sample_rate>
125
+ feat_extractor: !name:matcha.utils.audio.mel_spectrogram
126
+ n_fft: 1024
127
+ num_mels: 128
128
+ sampling_rate: !ref <sample_rate>
129
+ hop_size: 256
130
+ win_size: 1024
131
+ fmin: 0
132
+ fmax: 24000
133
+ center: False
134
+ compute_fbank: !name:inspiremusic.dataset.processor.compute_fbank
135
+ feat_extractor: !ref <feat_extractor>
136
+ parse_embedding: !name:inspiremusic.dataset.processor.parse_embedding
137
+ normalize: True
138
+ shuffle: !name:inspiremusic.dataset.processor.shuffle
139
+ shuffle_size: 1000
140
+ sort: !name:inspiremusic.dataset.processor.sort
141
+ sort_size: 500 # sort_size should be less than shuffle_size
142
+ batch: !name:inspiremusic.dataset.processor.batch
143
+ batch_type: 'dynamic'
144
+ max_frames_in_batch: 10000 # llm 12000
145
+ padding: !name:inspiremusic.dataset.processor.padding
146
+
147
+ # dataset processor pipeline
148
+ data_pipeline: [
149
+ !ref <parquet_opener>,
150
+ !ref <tokenize>,
151
+ !ref <shuffle>,
152
+ !ref <sort>,
153
+ !ref <filter>,
154
+ !ref <batch>,
155
+ !ref <padding>,
156
+ ]
157
+
158
+
159
+ # train conf
160
+ train_conf:
161
+ optim: adam
162
+ optim_conf:
163
+ lr: 0.0001 # change to 0.001 if you want to train flow from scratch
164
+ scheduler: warmuplr
165
+ scheduler_conf:
166
+ warmup_steps: 5000
167
+ max_epoch: 200
168
+ grad_clip: 5
169
+ accum_grad: 2
170
+ log_interval: 100
171
+ save_per_step: 500
example/conf/InspireMusic-1.5B.yaml ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # set random seed, so that you may reproduce your result.
2
+ __set_seed1: !apply:random.seed [1988]
3
+ __set_seed2: !apply:numpy.random.seed [1988]
4
+ __set_seed3: !apply:torch.manual_seed [1988]
5
+ __set_seed4: !apply:torch.cuda.manual_seed_all [1988]
6
+
7
+ # fixed params
8
+ sample_rate: 24000
9
+ text_encoder_input_size: 512
10
+ llm_input_size: 1536
11
+ llm_output_size: 1536
12
+
13
+ basemodel_path: 'pretrained_models/InspireMusic-1.5B/'
14
+ generator_path: 'pretrained_models/InspireMusic-1.5B/music_tokenizer'
15
+
16
+ # model params
17
+ # for all class/function included in this repo, we use !<name> or !<new> for intialization, so that user may find all corresponding class/function according to one single yaml.
18
+ # for system/third_party class/function, we do not require this.
19
+ llm: !new:inspiremusic.llm.llm.LLM
20
+ text_encoder_input_size: !ref <text_encoder_input_size>
21
+ llm_input_size: !ref <llm_input_size>
22
+ llm_output_size: !ref <llm_output_size>
23
+ audio_token_size: 4096
24
+ length_normalized_loss: True
25
+ lsm_weight: 0
26
+ text_encoder_conf:
27
+ name: "none"
28
+ llm: !new:inspiremusic.transformer.qwen_encoder.QwenEmbeddingEncoder
29
+ input_size: !ref <text_encoder_input_size>
30
+ pretrain_path: !ref <basemodel_path>
31
+
32
+ sampling: !name:inspiremusic.utils.common.topk_sampling
33
+ top_k: 350
34
+ train_cfg_ratio: 0.2
35
+ infer_cfg_ratio: 3.0
36
+ flow: !new:inspiremusic.flow.flow.MaskedDiff
37
+ input_size: 256
38
+ output_size: 80
39
+ output_type: 'mel'
40
+ vocab_size: 4096
41
+ input_frame_rate: 75
42
+ only_mask_loss: True
43
+ encoder: !new:inspiremusic.transformer.encoder.ConformerEncoder
44
+ output_size: 512
45
+ attention_heads: 4
46
+ linear_units: 1024
47
+ num_blocks: 3
48
+ dropout_rate: 0.1
49
+ positional_dropout_rate: 0.1
50
+ attention_dropout_rate: 0.1
51
+ normalize_before: True
52
+ input_layer: 'linear'
53
+ pos_enc_layer_type: 'rel_pos_espnet'
54
+ selfattention_layer_type: 'rel_selfattn'
55
+ input_size: 256
56
+ use_cnn_module: False
57
+ macaron_style: False
58
+ length_regulator: !new:inspiremusic.flow.length_regulator.InterpolateRegulator
59
+ channels: 512
60
+ sampling_ratios: [1, 1, 1, 1]
61
+ decoder: !new:inspiremusic.flow.flow_matching.ConditionalCFM
62
+ in_channels: 240
63
+ cfm_params: !new:omegaconf.DictConfig
64
+ content:
65
+ sigma_min: 1e-06
66
+ solver: 'euler'
67
+ t_scheduler: 'cosine'
68
+ training_cfg_rate: 0.2
69
+ inference_cfg_rate: 0.7
70
+ reg_loss_type: 'l1'
71
+ estimator: !new:inspiremusic.flow.decoder.ConditionalDecoder
72
+ in_channels: 1024
73
+ out_channels: 512
74
+ channels: [256, 256]
75
+ dropout: 0.0
76
+ attention_head_dim: 64
77
+ n_blocks: 4
78
+ num_mid_blocks: 8
79
+ num_heads: 8
80
+ act_fn: 'gelu'
81
+ generator_model_dir: !ref <generator_path>
82
+
83
+ hift: !new:inspiremusic.hifigan.generator.HiFTGenerator
84
+ in_channels: 80
85
+ base_channels: 512
86
+ nb_harmonics: 8
87
+ sampling_rate: !ref <sample_rate>
88
+ nsf_alpha: 0.1
89
+ nsf_sigma: 0.003
90
+ nsf_voiced_threshold: 10
91
+ upsample_rates: [8, 8]
92
+ upsample_kernel_sizes: [16, 16]
93
+ istft_params:
94
+ n_fft: 16
95
+ hop_len: 4
96
+ resblock_kernel_sizes: [3, 7, 11]
97
+ resblock_dilation_sizes: [[1, 3, 5], [1, 3, 5], [1, 3, 5]]
98
+ source_resblock_kernel_sizes: [7, 11]
99
+ source_resblock_dilation_sizes: [[1, 3, 5], [1, 3, 5]]
100
+ lrelu_slope: 0.1
101
+ audio_limit: 0.99
102
+ f0_predictor: !new:inspiremusic.hifigan.f0_predictor.ConvRNNF0Predictor
103
+ num_class: 1
104
+ in_channels: 80
105
+ cond_channels: 512
106
+
107
+ wavtokenizer: !new:inspiremusic.hifigan.generator.HiFTGenerator
108
+
109
+ # processor functions
110
+ parquet_opener: !name:inspiremusic.dataset.processor.parquet_opener
111
+ get_tokenizer: !name:inspiremusic.text.tokenizer.get_tokenizer
112
+ tokenizer_path: !ref <basemodel_path>
113
+ tokenizer_name: "qwen-2.5"
114
+ allowed_special: 'all'
115
+ tokenize: !name:inspiremusic.dataset.processor.tokenize
116
+ get_tokenizer: !ref <get_tokenizer>
117
+ allowed_special: !ref <allowed_special>
118
+ filter: !name:inspiremusic.dataset.processor.filter
119
+ max_length: 28000
120
+ min_length: 0
121
+ token_max_length: 200
122
+ token_min_length: 1
123
+ resample: !name:inspiremusic.dataset.processor.resample
124
+ resample_rate: !ref <sample_rate>
125
+ feat_extractor: !name:matcha.utils.audio.mel_spectrogram
126
+ n_fft: 1024
127
+ num_mels: 128
128
+ sampling_rate: !ref <sample_rate>
129
+ hop_size: 256
130
+ win_size: 1024
131
+ fmin: 0
132
+ fmax: 24000
133
+ center: False
134
+ compute_fbank: !name:inspiremusic.dataset.processor.compute_fbank
135
+ feat_extractor: !ref <feat_extractor>
136
+ parse_embedding: !name:inspiremusic.dataset.processor.parse_embedding
137
+ normalize: True
138
+ shuffle: !name:inspiremusic.dataset.processor.shuffle
139
+ shuffle_size: 1000
140
+ sort: !name:inspiremusic.dataset.processor.sort
141
+ sort_size: 500 # sort_size should be less than shuffle_size
142
+ batch: !name:inspiremusic.dataset.processor.batch
143
+ batch_type: 'dynamic'
144
+ max_frames_in_batch: 10000 # llm 12000
145
+ padding: !name:inspiremusic.dataset.processor.padding
146
+
147
+ # dataset processor pipeline
148
+ data_pipeline: [
149
+ !ref <parquet_opener>,
150
+ !ref <tokenize>,
151
+ !ref <shuffle>,
152
+ !ref <sort>,
153
+ !ref <filter>,
154
+ !ref <batch>,
155
+ !ref <padding>,
156
+ ]
157
+
158
+
159
+ # train conf
160
+ train_conf:
161
+ optim: adam
162
+ optim_conf:
163
+ lr: 0.0001 # change to 0.001 if you want to train flow from scratch
164
+ scheduler: warmuplr
165
+ scheduler_conf:
166
+ warmup_steps: 5000
167
+ max_epoch: 200
168
+ grad_clip: 5
169
+ accum_grad: 2
170
+ log_interval: 100
171
+ save_per_step: 500
example/conf/InspireMusic-Base-24kHz.yaml ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # set random seed, so that you may reproduce your result.
2
+ __set_seed1: !apply:random.seed [1024]
3
+ __set_seed2: !apply:numpy.random.seed [1024]
4
+ __set_seed3: !apply:torch.manual_seed [1024]
5
+ __set_seed4: !apply:torch.cuda.manual_seed_all [1024]
6
+
7
+ # fixed params
8
+ sample_rate: 24000
9
+ text_encoder_input_size: 512
10
+ llm_input_size: 896
11
+ llm_output_size: 896
12
+
13
+ basemodel_path: 'pretrained_models/InspireMusic-Base-24kHz/'
14
+ generator_path: 'pretrained_models/InspireMusic-Base-24kHz/music_tokenizer'
15
+
16
+ # model params
17
+ # for all class/function included in this repo, we use !<name> or !<new> for intialization, so that user may find all corresponding class/function according to one single yaml.
18
+ # for system/third_party class/function, we do not require this.
19
+ llm: !new:inspiremusic.llm.llm.LLM
20
+ text_encoder_input_size: !ref <text_encoder_input_size>
21
+ llm_input_size: !ref <llm_input_size>
22
+ llm_output_size: !ref <llm_output_size>
23
+ audio_token_size: 4096
24
+ length_normalized_loss: True
25
+ lsm_weight: 0
26
+ text_encoder_conf:
27
+ name: "none"
28
+ llm: !new:inspiremusic.transformer.qwen_encoder.QwenEmbeddingEncoder
29
+ input_size: !ref <text_encoder_input_size>
30
+ pretrain_path: !ref <basemodel_path>
31
+
32
+ sampling: !name:inspiremusic.utils.common.topk_sampling
33
+ top_k: 350
34
+ train_cfg_ratio: 0.2
35
+ infer_cfg_ratio: 7.0
36
+ flow: !new:inspiremusic.flow.flow.MaskedDiff
37
+ input_size: 256
38
+ output_size: 80
39
+ output_type: 'mel'
40
+ vocab_size: 4096
41
+ input_frame_rate: 75
42
+ only_mask_loss: True
43
+ encoder: !new:inspiremusic.transformer.encoder.ConformerEncoder
44
+ output_size: 512
45
+ attention_heads: 4
46
+ linear_units: 1024
47
+ num_blocks: 3
48
+ dropout_rate: 0.1
49
+ positional_dropout_rate: 0.1
50
+ attention_dropout_rate: 0.1
51
+ normalize_before: True
52
+ input_layer: 'linear'
53
+ pos_enc_layer_type: 'rel_pos_espnet'
54
+ selfattention_layer_type: 'rel_selfattn'
55
+ input_size: 256
56
+ use_cnn_module: False
57
+ macaron_style: False
58
+ length_regulator: !new:inspiremusic.flow.length_regulator.InterpolateRegulator
59
+ channels: 512
60
+ sampling_ratios: [1, 1, 1, 1]
61
+ decoder: !new:inspiremusic.flow.flow_matching.ConditionalCFM
62
+ in_channels: 240
63
+ cfm_params: !new:omegaconf.DictConfig
64
+ content:
65
+ sigma_min: 1e-06
66
+ solver: 'euler'
67
+ t_scheduler: 'cosine'
68
+ training_cfg_rate: 0.2
69
+ inference_cfg_rate: 0.7
70
+ reg_loss_type: 'l1'
71
+ estimator: !new:inspiremusic.flow.decoder.ConditionalDecoder
72
+ in_channels: 1024
73
+ out_channels: 512
74
+ channels: [256, 256]
75
+ dropout: 0.0
76
+ attention_head_dim: 64
77
+ n_blocks: 4
78
+ num_mid_blocks: 8
79
+ num_heads: 8
80
+ act_fn: 'gelu'
81
+ generator_model_dir: !ref <generator_path>
82
+
83
+ hift: !new:inspiremusic.hifigan.generator.HiFTGenerator
84
+ in_channels: 80
85
+ base_channels: 512
86
+ nb_harmonics: 8
87
+ sampling_rate: !ref <sample_rate>
88
+ nsf_alpha: 0.1
89
+ nsf_sigma: 0.003
90
+ nsf_voiced_threshold: 10
91
+ upsample_rates: [8, 8]
92
+ upsample_kernel_sizes: [16, 16]
93
+ istft_params:
94
+ n_fft: 16
95
+ hop_len: 4
96
+ resblock_kernel_sizes: [3, 7, 11]
97
+ resblock_dilation_sizes: [[1, 3, 5], [1, 3, 5], [1, 3, 5]]
98
+ source_resblock_kernel_sizes: [7, 11]
99
+ source_resblock_dilation_sizes: [[1, 3, 5], [1, 3, 5]]
100
+ lrelu_slope: 0.1
101
+ audio_limit: 0.99
102
+ f0_predictor: !new:inspiremusic.hifigan.f0_predictor.ConvRNNF0Predictor
103
+ num_class: 1
104
+ in_channels: 80
105
+ cond_channels: 512
106
+
107
+ wavtokenizer: !new:inspiremusic.hifigan.generator.HiFTGenerator
108
+
109
+ # processor functions
110
+ parquet_opener: !name:inspiremusic.dataset.processor.parquet_opener
111
+ get_tokenizer: !name:inspiremusic.text.tokenizer.get_tokenizer
112
+ tokenizer_path: !ref <basemodel_path>
113
+ tokenizer_name: "qwen-2.0"
114
+ allowed_special: 'all'
115
+ tokenize: !name:inspiremusic.dataset.processor.tokenize
116
+ get_tokenizer: !ref <get_tokenizer>
117
+ allowed_special: !ref <allowed_special>
118
+ filter: !name:inspiremusic.dataset.processor.filter
119
+ max_length: 28000
120
+ min_length: 0
121
+ token_max_length: 200
122
+ token_min_length: 1
123
+ resample: !name:inspiremusic.dataset.processor.resample
124
+ resample_rate: !ref <sample_rate>
125
+ feat_extractor: !name:matcha.utils.audio.mel_spectrogram
126
+ n_fft: 1024
127
+ num_mels: 128
128
+ sampling_rate: !ref <sample_rate>
129
+ hop_size: 256
130
+ win_size: 1024
131
+ fmin: 0
132
+ fmax: 24000
133
+ center: False
134
+ compute_fbank: !name:inspiremusic.dataset.processor.compute_fbank
135
+ feat_extractor: !ref <feat_extractor>
136
+ parse_embedding: !name:inspiremusic.dataset.processor.parse_embedding
137
+ normalize: True
138
+ shuffle: !name:inspiremusic.dataset.processor.shuffle
139
+ shuffle_size: 1000
140
+ sort: !name:inspiremusic.dataset.processor.sort
141
+ sort_size: 500 # sort_size should be less than shuffle_size
142
+ batch: !name:inspiremusic.dataset.processor.batch
143
+ batch_type: 'dynamic'
144
+ max_frames_in_batch: 10000 # llm 12000
145
+ padding: !name:inspiremusic.dataset.processor.padding
146
+
147
+ # dataset processor pipeline
148
+ data_pipeline: [
149
+ !ref <parquet_opener>,
150
+ !ref <tokenize>,
151
+ !ref <shuffle>,
152
+ !ref <sort>,
153
+ !ref <filter>,
154
+ !ref <batch>,
155
+ !ref <padding>,
156
+ ]
157
+
158
+
159
+ # train conf
160
+ train_conf:
161
+ optim: adam
162
+ optim_conf:
163
+ lr: 0.0001 # change to 0.001 if you want to train flow from scratch
164
+ scheduler: warmuplr
165
+ scheduler_conf:
166
+ warmup_steps: 5000
167
+ max_epoch: 200
168
+ grad_clip: 5
169
+ accum_grad: 2
170
+ log_interval: 100
171
+ save_per_step: 500
example/conf/InspireMusic-Base.yaml ADDED
@@ -0,0 +1,180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # set random seed, so that you may reproduce your result.
2
+ __set_seed1: !apply:random.seed [1024]
3
+ __set_seed2: !apply:numpy.random.seed [1024]
4
+ __set_seed3: !apply:torch.manual_seed [1024]
5
+ __set_seed4: !apply:torch.cuda.manual_seed_all [1024]
6
+
7
+ # fixed params
8
+ sample_rate: 24000
9
+ target_sample_rate: 48000
10
+ text_encoder_input_size: 512
11
+ llm_input_size: 896
12
+ llm_output_size: 896
13
+
14
+ basemodel_path: 'pretrained_models/InspireMusic-Base/'
15
+ generator_path: 'pretrained_models/InspireMusic-Base/music_tokenizer'
16
+
17
+ # model params
18
+ # for all class/function included in this repo, we use !<name> or !<new> for intialization, so that user may find all corresponding class/function according to one single yaml.
19
+ # for system/third_party class/function, we do not require this.
20
+ llm: !new:inspiremusic.llm.llm.LLM
21
+ text_encoder_input_size: !ref <text_encoder_input_size>
22
+ llm_input_size: !ref <llm_input_size>
23
+ llm_output_size: !ref <llm_output_size>
24
+ audio_token_size: 4096
25
+ length_normalized_loss: True
26
+ lsm_weight: 0
27
+ text_encoder_conf:
28
+ name: "none"
29
+ llm: !new:inspiremusic.transformer.qwen_encoder.QwenEmbeddingEncoder
30
+ input_size: !ref <text_encoder_input_size>
31
+ pretrain_path: !ref <basemodel_path>
32
+
33
+ sampling: !name:inspiremusic.utils.common.topk_sampling
34
+ top_k: 350
35
+ train_cfg_ratio: 0.2
36
+ infer_cfg_ratio: 3.0
37
+ flow: !new:inspiremusic.flow.flow.MaskedDiff
38
+ input_size: 256
39
+ output_size: 80
40
+ output_type: 'mel'
41
+ vocab_size: 4096
42
+ input_frame_rate: 75
43
+ only_mask_loss: True
44
+ encoder: !new:inspiremusic.transformer.encoder.ConformerEncoder
45
+ output_size: 512
46
+ attention_heads: 4
47
+ linear_units: 1024
48
+ num_blocks: 3
49
+ dropout_rate: 0.1
50
+ positional_dropout_rate: 0.1
51
+ attention_dropout_rate: 0.1
52
+ normalize_before: True
53
+ input_layer: 'linear'
54
+ pos_enc_layer_type: 'rel_pos_espnet'
55
+ selfattention_layer_type: 'rel_selfattn'
56
+ input_size: 256
57
+ use_cnn_module: False
58
+ macaron_style: False
59
+ length_regulator: !new:inspiremusic.flow.length_regulator.InterpolateRegulator
60
+ channels: 512
61
+ sampling_ratios: [1, 1, 1, 1]
62
+ decoder: !new:inspiremusic.flow.flow_matching.ConditionalCFM
63
+ in_channels: 240
64
+ cfm_params: !new:omegaconf.DictConfig
65
+ content:
66
+ sigma_min: 1e-06
67
+ solver: 'euler'
68
+ t_scheduler: 'cosine'
69
+ training_cfg_rate: 0.2
70
+ inference_cfg_rate: 0.7
71
+ reg_loss_type: 'l1'
72
+ estimator: !new:inspiremusic.flow.decoder.ConditionalDecoder
73
+ in_channels: 1024
74
+ out_channels: 512
75
+ channels: [256, 256]
76
+ dropout: 0.0
77
+ attention_head_dim: 64
78
+ n_blocks: 4
79
+ num_mid_blocks: 8
80
+ num_heads: 8
81
+ act_fn: 'gelu'
82
+ generator_model_dir: !ref <generator_path>
83
+
84
+ hift: !new:inspiremusic.hifigan.generator.HiFTGenerator
85
+ in_channels: 80
86
+ base_channels: 512
87
+ nb_harmonics: 8
88
+ sampling_rate: !ref <sample_rate>
89
+ nsf_alpha: 0.1
90
+ nsf_sigma: 0.003
91
+ nsf_voiced_threshold: 10
92
+ upsample_rates: [8, 8]
93
+ upsample_kernel_sizes: [16, 16]
94
+ istft_params:
95
+ n_fft: 16
96
+ hop_len: 4
97
+ resblock_kernel_sizes: [3, 7, 11]
98
+ resblock_dilation_sizes: [[1, 3, 5], [1, 3, 5], [1, 3, 5]]
99
+ source_resblock_kernel_sizes: [7, 11]
100
+ source_resblock_dilation_sizes: [[1, 3, 5], [1, 3, 5]]
101
+ lrelu_slope: 0.1
102
+ audio_limit: 0.99
103
+ f0_predictor: !new:inspiremusic.hifigan.f0_predictor.ConvRNNF0Predictor
104
+ num_class: 1
105
+ in_channels: 80
106
+ cond_channels: 512
107
+
108
+ wavtokenizer: !new:inspiremusic.hifigan.generator.HiFTGenerator
109
+
110
+ # processor functions
111
+ parquet_opener: !name:inspiremusic.dataset.processor.parquet_opener
112
+ get_tokenizer: !name:inspiremusic.text.tokenizer.get_tokenizer
113
+ tokenizer_path: !ref <basemodel_path>
114
+ tokenizer_name: "qwen-2.0"
115
+ allowed_special: 'all'
116
+ tokenize: !name:inspiremusic.dataset.processor.tokenize
117
+ get_tokenizer: !ref <get_tokenizer>
118
+ allowed_special: !ref <allowed_special>
119
+ filter: !name:inspiremusic.dataset.processor.filter
120
+ max_length: 20000
121
+ min_length: 1
122
+ token_max_length: 200
123
+ token_min_length: 1
124
+ max_acoustic_length: 20000
125
+ min_acoustic_length: 1800
126
+ mode: 'train_flow'
127
+
128
+ resample: !name:inspiremusic.dataset.processor.resample
129
+ resample_rate: !ref <sample_rate>
130
+
131
+ feat_extractor: !name:matcha.utils.audio.mel_spectrogram
132
+ n_fft: 1024
133
+ num_mels: 128
134
+ sampling_rate: !ref <sample_rate>
135
+ hop_size: 256
136
+ win_size: 1024
137
+ fmin: 0
138
+ fmax: 24000
139
+ center: False
140
+ compute_fbank: !name:inspiremusic.dataset.processor.compute_fbank
141
+ feat_extractor: !ref <feat_extractor>
142
+ parse_embedding: !name:inspiremusic.dataset.processor.parse_embedding
143
+ normalize: True
144
+ shuffle: !name:inspiremusic.dataset.processor.shuffle
145
+ shuffle_size: 1000
146
+ sort: !name:inspiremusic.dataset.processor.sort
147
+ sort_size: 500 # sort_size should be less than shuffle_size
148
+ batch: !name:inspiremusic.dataset.processor.batch
149
+ batch_type: 'dynamic'
150
+ max_frames_in_batch: 15500 # llm 12000
151
+ # batch_type: 'static'
152
+ # batch_size: 2 # llm 12000
153
+ padding: !name:inspiremusic.dataset.processor.padding
154
+ mode: 'train'
155
+
156
+ # dataset processor pipeline
157
+ data_pipeline: [
158
+ !ref <parquet_opener>,
159
+ !ref <tokenize>,
160
+ !ref <shuffle>,
161
+ !ref <sort>,
162
+ !ref <filter>,
163
+ !ref <batch>,
164
+ !ref <padding>,
165
+ ]
166
+
167
+
168
+ # train conf
169
+ train_conf:
170
+ optim: adam
171
+ optim_conf:
172
+ lr: 0.0001 # change to 0.001 if you want to train flow from scratch
173
+ scheduler: warmuplr
174
+ scheduler_conf:
175
+ warmup_steps: 500
176
+ max_epoch: 200
177
+ grad_clip: 5
178
+ accum_grad: 2
179
+ log_interval: 100
180
+ save_per_step: 500
inspiremusic/.DS_Store ADDED
Binary file (8.2 kB). View file
 
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,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 torch
16
+ from typing import Callable
17
+ import re
18
+ import inflect
19
+ from inspiremusic.cli.model import InspireMusicModel
20
+ from inspiremusic.utils.frontend_utils import contains_chinese, replace_blank, replace_corner_mark, remove_bracket, spell_out_number, split_paragraph
21
+ from inspiremusic.wavtokenizer.decoder.pretrained import WavTokenizer
22
+
23
+ class InspireMusicFrontEnd:
24
+ def __init__(self,
25
+ configs: Callable,
26
+ get_tokenizer: Callable,
27
+ llm_model: str,
28
+ flow_model: str,
29
+ music_tokenizer_dir: str,
30
+ audio_tokenizer_dir: str,
31
+ instruct: bool = False,
32
+ fast: bool = False,
33
+ fp16: bool = True,
34
+ allowed_special: str = 'all'):
35
+ self.tokenizer = get_tokenizer()
36
+ self.audio_tokenizer_dir = audio_tokenizer_dir
37
+ self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
38
+
39
+ self.bandwidth_id = torch.tensor([0]).to(self.device)
40
+ self.wavtokenizer = WavTokenizer.from_pretrained_feat(f"{audio_tokenizer_dir}/config.yaml", f"{audio_tokenizer_dir}/model.pt").to(self.device)
41
+
42
+ self.model = InspireMusicModel(configs['llm'], configs['flow'], configs['hift'], configs['wavtokenizer'], fast, fp16)
43
+ self.model = self.model.load(llm_model, flow_model, music_tokenizer_dir, audio_tokenizer_dir)
44
+
45
+ self.instruct = instruct
46
+ self.allowed_special = allowed_special
47
+ self.inflect_parser = inflect.engine()
48
+
49
+ def _extract_text_token(self, text):
50
+ text_token = self.tokenizer.encode(text, allowed_special=self.allowed_special)
51
+ text_token = torch.tensor([text_token], dtype=torch.int32).to(self.device)
52
+ text_token_len = torch.tensor([text_token.shape[1]], dtype=torch.int32).to(self.device)
53
+ return text_token, text_token_len
54
+
55
+ def _extract_audio_token(self, audio, sample_rate=24000):
56
+ audio = torch.tensor(audio, dtype=torch.float32, device=self.device)
57
+ _, audio_token = self.wavtokenizer.encode_infer(audio, bandwidth_id=self.bandwidth_id)
58
+ audio_token = audio_token.squeeze(0)
59
+ audio_token_len = torch.tensor([audio_token.shape[1]], dtype=torch.int32, device=self.device)
60
+ return audio_token, audio_token_len
61
+
62
+ def text_normalize(self, text, split=True):
63
+ text = text.strip()
64
+ if contains_chinese(text):
65
+ text = text.replace("\n", "")
66
+ text = replace_blank(text)
67
+ text = replace_corner_mark(text)
68
+ text = text.replace(".", "、")
69
+ text = text.replace(" - ", ",")
70
+ text = remove_bracket(text)
71
+ text = re.sub(r'[,,]+$', '。', text)
72
+ texts = list(split_paragraph(text, partial(self.tokenizer.encode, allowed_special=self.allowed_special), "zh", token_max_n=80,
73
+ token_min_n=60, merge_len=20, comma_split=False))
74
+ else:
75
+ text = spell_out_number(text, self.inflect_parser)
76
+ texts = list(split_paragraph(text, partial(self.tokenizer.encode, allowed_special=self.allowed_special), "en", token_max_n=80,
77
+ token_min_n=60, merge_len=20, comma_split=False))
78
+ if split is False:
79
+ return text
80
+ return texts
81
+
82
+ def frontend_text_to_music(self, text, time_start, time_end, chorus):
83
+ text_token, text_token_len = self._extract_text_token(text)
84
+ model_input = {"text": text, "audio_token": None, "audio_token_len": None,
85
+ "text_token": text_token, "text_token_len": text_token_len,
86
+ "embeddings": [time_start, time_end, chorus], "raw_text":text}
87
+ return model_input
88
+
89
+ def frontend_continuation(self, text, audio, time_start, time_end, chorus, target_sr=24000):
90
+ if text is None:
91
+ text_token = None
92
+ text_token_len = None
93
+ else:
94
+ text_token, text_token_len = self._extract_text_token(text)
95
+ audio_token, audio_token_len = self._extract_audio_token(audio, target_sr)
96
+ model_input = {"text": text, "audio_token": audio_token, "audio_token_len": audio_token_len,
97
+ "text_token": text_token, "text_token_len": text_token_len,
98
+ "embeddings": [time_start, time_end, chorus], "raw_text":text}
99
+ return model_input
100
+
inspiremusic/cli/inference.py ADDED
@@ -0,0 +1,312 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ from inspiremusic.cli.inspiremusic import InspireMusic
22
+ from inspiremusic.utils.file_utils import logging
23
+ import torch
24
+ from inspiremusic.utils.audio_utils import trim_audio, fade_out, process_audio
25
+
26
+ def set_env_variables():
27
+ os.environ['PYTHONIOENCODING'] = 'UTF-8'
28
+ os.environ['TOKENIZERS_PARALLELISM'] = 'False'
29
+ main_root = os.getcwd()
30
+ bin_dir = os.path.join(main_root, 'inspiremusic')
31
+ third_party_matcha_tts_path = os.path.join(main_root, 'third_party', 'Matcha-TTS')
32
+ python_path = f"{main_root}:{bin_dir}:{third_party_matcha_tts_path}:{os.environ.get('PYTHONPATH', '')}"
33
+ os.environ['PATH'] = python_path
34
+ sys.path.extend([main_root, third_party_matcha_tts_path])
35
+
36
+ class InspireMusicUnified:
37
+ def __init__(self,
38
+ model_name: str = "InspireMusic-1.5B-Long",
39
+ model_dir: str = None,
40
+ min_generate_audio_seconds: float = 10.0,
41
+ max_generate_audio_seconds: float = 30.0,
42
+ sample_rate: int = 24000,
43
+ output_sample_rate: int = 48000,
44
+ load_jit: bool = True,
45
+ load_onnx: bool = False,
46
+ fast: bool = False,
47
+ fp16: bool = True,
48
+ gpu: int = 0,
49
+ result_dir: str = None,
50
+ hub="modelscope"):
51
+ os.environ['CUDA_VISIBLE_DEVICES'] = str(gpu)
52
+
53
+ # Set model_dir or default to downloading if it doesn't exist
54
+ if model_dir is None:
55
+ model_dir = f"pretrained_models/{model_name}"
56
+ else:
57
+ model_dir = model_dir.replace("../../", "./")
58
+
59
+ if not os.path.isfile(f"{model_dir}/llm.pt"):
60
+ if hub == "modelscope":
61
+ from modelscope import snapshot_download
62
+ if model_name == "InspireMusic-Base":
63
+ snapshot_download(f"iic/InspireMusic", local_dir=model_dir)
64
+ else:
65
+ snapshot_download(f"iic/{model_name}", local_dir=model_dir)
66
+
67
+ self.model_dir = model_dir
68
+ print(self.model_dir)
69
+
70
+ self.sample_rate = sample_rate
71
+ self.output_sample_rate = 24000 if fast else output_sample_rate
72
+ self.result_dir = result_dir or f"exp/{model_name}"
73
+ os.makedirs(self.result_dir, exist_ok=True)
74
+
75
+ self.min_generate_audio_seconds = min_generate_audio_seconds
76
+ self.max_generate_audio_seconds = max_generate_audio_seconds
77
+ self.min_generate_audio_length = int(self.output_sample_rate * self.min_generate_audio_seconds)
78
+ self.max_generate_audio_length = int(self.output_sample_rate * self.max_generate_audio_seconds)
79
+ assert self.min_generate_audio_seconds <= self.max_generate_audio_seconds, "Min audio seconds must be less than or equal to max audio seconds"
80
+
81
+ use_cuda = gpu >= 0 and torch.cuda.is_available()
82
+ self.device = torch.device('cuda' if use_cuda else 'cpu')
83
+ self.model = InspireMusic(self.model_dir, load_jit=load_jit, load_onnx=load_onnx, fast=fast, fp16=fp16)
84
+ self.model.model.llm = self.model.model.llm.to(torch.float16)
85
+
86
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
87
+
88
+ @torch.inference_mode()
89
+ def inference(self,
90
+ task: str = 'text-to-music',
91
+ text: str = None,
92
+ audio_prompt: str = None, # audio prompt file path
93
+ chorus: str = "verse",
94
+ time_start: float = 0.0,
95
+ time_end: float = 30.0,
96
+ output_fn: str = "output_audio",
97
+ max_audio_prompt_length: float = 5.0,
98
+ fade_out_duration: float = 1.0,
99
+ output_format: str = "wav",
100
+ fade_out_mode: bool = True,
101
+ trim: bool = False,
102
+ ):
103
+
104
+ with torch.no_grad():
105
+ text_prompt = f"<|{time_start}|><|{chorus}|><|{text}|><|{time_end}|>"
106
+ chorus_dict = {"random": torch.randint(1, 5, (1,)).item(), "intro" : 0, "verse": 1, "chorus": 2, "outro": 4}
107
+ chorus = chorus_dict.get(chorus, 1)
108
+ chorus = torch.tensor([chorus], dtype=torch.int).to(self.device)
109
+
110
+ time_start_tensor = torch.tensor([time_start], dtype=torch.float64).to(self.device)
111
+ time_end_tensor = torch.tensor([time_end], dtype=torch.float64).to(self.device)
112
+
113
+ music_fn = os.path.join(self.result_dir, f'{output_fn}.{output_format}')
114
+
115
+ bench_start = time.time()
116
+
117
+ if task == 'text-to-music':
118
+ model_input = {
119
+ "text" : text,
120
+ "audio_prompt" : audio_prompt,
121
+ "time_start" : time_start_tensor,
122
+ "time_end" : time_end_tensor,
123
+ "chorus" : chorus,
124
+ "task" : task,
125
+ "stream" : False,
126
+ "duration_to_gen": self.max_generate_audio_seconds,
127
+ "sr" : self.sample_rate
128
+ }
129
+ elif task == 'continuation':
130
+ if audio_prompt is not None:
131
+ audio, _ = process_audio(audio_prompt, self.sample_rate)
132
+ if audio.size(1) < self.sample_rate:
133
+ logging.warning("Warning: Input prompt audio length is shorter than 1s. Please provide an appropriate length audio prompt and try again.")
134
+ audio = None
135
+ else:
136
+ max_audio_prompt_length_samples = int(max_audio_prompt_length * self.sample_rate)
137
+ audio = audio[:, :max_audio_prompt_length_samples] # Trimming prompt audio
138
+
139
+ model_input = {
140
+ "text" : text,
141
+ "audio_prompt" : audio,
142
+ "time_start" : time_start_tensor,
143
+ "time_end" : time_end_tensor,
144
+ "chorus" : chorus,
145
+ "task" : task,
146
+ "stream" : False,
147
+ "duration_to_gen": self.max_generate_audio_seconds,
148
+ "sr" : self.sample_rate
149
+ }
150
+
151
+ music_audios = []
152
+ for model_output in self.model.cli_inference(**model_input):
153
+ music_audios.append(model_output['music_audio'])
154
+
155
+ bench_end = time.time()
156
+
157
+ if trim:
158
+ music_audio = trim_audio(music_audios[0],
159
+ sample_rate=self.output_sample_rate,
160
+ threshold=0.05,
161
+ min_silence_duration=0.8)
162
+ else:
163
+ music_audio = music_audios[0]
164
+
165
+ if music_audio.shape[0] != 0:
166
+ if music_audio.shape[1] > self.max_generate_audio_length:
167
+ music_audio = music_audio[:, :self.max_generate_audio_length]
168
+
169
+ if music_audio.shape[1] >= self.min_generate_audio_length:
170
+ try:
171
+ if fade_out_mode:
172
+ music_audio = fade_out(music_audio, self.output_sample_rate, fade_out_duration)
173
+
174
+ music_audio = music_audio.repeat(2, 1)
175
+
176
+ if output_format in ["wav", "flac"]:
177
+ torchaudio.save(music_fn, music_audio,
178
+ sample_rate=self.output_sample_rate,
179
+ encoding="PCM_S",
180
+ bits_per_sample=24)
181
+ elif output_format in ["mp3", "m4a"]:
182
+ torchaudio.backend.sox_io_backend.save(
183
+ filepath=music_fn, src=music_audio,
184
+ sample_rate=self.output_sample_rate,
185
+ format=output_format)
186
+ else:
187
+ logging.info("Format is not supported. Please choose from wav, mp3, m4a, flac.")
188
+
189
+ except Exception as e:
190
+ logging.error(f"Error saving file: {e}")
191
+ raise
192
+
193
+ audio_duration = music_audio.shape[1] / self.output_sample_rate
194
+ rtf = (bench_end - bench_start) / audio_duration
195
+ logging.info(f"Processing time: {int(bench_end - bench_start)}s, audio length: {int(audio_duration)}s, rtf: {rtf}, text prompt: {text_prompt}")
196
+
197
+ else:
198
+ logging.error(f"Generated audio length is shorter than minimum required audio length.")
199
+ if music_fn:
200
+ if os.path.exists(music_fn):
201
+ logging.info(f"Generated audio file {music_fn} is saved.")
202
+ return music_fn
203
+ else:
204
+ logging.error(f"{music_fn} does not exist.")
205
+
206
+ def get_args():
207
+ parser = argparse.ArgumentParser(description='Run inference with your model')
208
+ parser.add_argument('-m', '--model_name', default="InspireMusic-1.5B-Long",
209
+ help='Model name')
210
+
211
+ parser.add_argument('-d', '--model_dir',
212
+ help='Model folder path')
213
+
214
+ 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.",
215
+ help='Prompt text')
216
+
217
+ parser.add_argument('-a', '--audio_prompt', default=None,
218
+ help='Prompt audio')
219
+
220
+ parser.add_argument('-c', '--chorus', default="intro",
221
+ help='Chorus tag generation mode (e.g., random, verse, chorus, intro, outro)')
222
+
223
+ parser.add_argument('-f', '--fast', type=bool, default=False,
224
+ help='Enable fast inference mode (without flow matching)')
225
+
226
+ parser.add_argument('-g', '--gpu', type=int, default=0,
227
+ help='GPU ID for this rank, -1 for CPU')
228
+
229
+ parser.add_argument('--task', default='text-to-music', choices=['text-to-music', 'continuation', 'reconstruct', 'super_resolution'],
230
+ help='Inference task type: text-to-music, continuation, reconstruct, super_resolution')
231
+
232
+ parser.add_argument('-r', '--result_dir', default="exp/inspiremusic",
233
+ help='Directory to save generated audio')
234
+
235
+ parser.add_argument('-o', '--output_fn', default="output_audio",
236
+ help='Output file name')
237
+
238
+ parser.add_argument('--format', type=str, default="wav", choices=["wav", "mp3", "m4a", "flac"],
239
+ help='Format of output audio')
240
+
241
+ parser.add_argument('--sample_rate', type=int, default=24000,
242
+ help='Sampling rate of input audio')
243
+
244
+ parser.add_argument('--output_sample_rate', type=int, default=48000, choices=[24000, 48000],
245
+ help='Sampling rate of generated output audio')
246
+
247
+ parser.add_argument('-s', '--time_start', type=float, default=0.0,
248
+ help='Start time in seconds')
249
+
250
+ parser.add_argument('-e', '--time_end', type=float, default=30.0,
251
+ help='End time in seconds')
252
+
253
+ parser.add_argument('--max_audio_prompt_length', type=float, default=5.0,
254
+ help='Maximum audio prompt length in seconds')
255
+
256
+ parser.add_argument('--min_generate_audio_seconds', type=float, default=10.0,
257
+ help='Minimum generated audio length in seconds')
258
+
259
+ parser.add_argument('--max_generate_audio_seconds', type=float, default=300.0,
260
+ help='Maximum generated audio length in seconds')
261
+
262
+ parser.add_argument('--fp16', type=bool, default=True,
263
+ help='Inference with fp16 model')
264
+
265
+ parser.add_argument('--fade_out', type=bool, default=True,
266
+ help='Apply fade out effect to generated audio')
267
+
268
+ parser.add_argument('--fade_out_duration', type=float, default=1.0,
269
+ help='Fade out duration in seconds')
270
+
271
+ parser.add_argument('--trim', type=bool, default=False,
272
+ help='Trim the silence ending of generated audio')
273
+
274
+ args = parser.parse_args()
275
+
276
+ if not args.model_dir:
277
+ args.model_dir = os.path.join("pretrained_models", args.model_name)
278
+
279
+ print(args)
280
+ return args
281
+
282
+ def main():
283
+ set_env_variables()
284
+ args = get_args()
285
+ model = InspireMusicUnified(model_name = args.model_name,
286
+ model_dir = args.model_dir,
287
+ min_generate_audio_seconds = args.min_generate_audio_seconds,
288
+ max_generate_audio_seconds = args.max_generate_audio_seconds,
289
+ sample_rate = args.sample_rate,
290
+ output_sample_rate = args.output_sample_rate,
291
+ load_jit = True,
292
+ load_onnx = False,
293
+ fast = args.fast,
294
+ fp16 = args.fp16,
295
+ gpu = args.gpu,
296
+ result_dir = args.result_dir)
297
+
298
+ model.inference(task = args.task,
299
+ text = args.text,
300
+ audio_prompt = args.audio_prompt,
301
+ chorus = args.chorus,
302
+ time_start = args.time_start,
303
+ time_end = args.time_end,
304
+ output_fn = args.output_fn,
305
+ max_audio_prompt_length = args.max_audio_prompt_length,
306
+ fade_out_duration = args.fade_out_duration,
307
+ output_format = args.format,
308
+ fade_out_mode = args.fade_out,
309
+ trim = args.trim)
310
+
311
+ if __name__ == "__main__":
312
+ main()
inspiremusic/cli/inspiremusic.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 os
15
+ import time
16
+ from tqdm import tqdm
17
+ from hyperpyyaml import load_hyperpyyaml
18
+ from inspiremusic.cli.frontend import InspireMusicFrontEnd
19
+ from inspiremusic.cli.model import InspireMusicModel
20
+ from inspiremusic.utils.file_utils import logging
21
+ import torch
22
+
23
+ class InspireMusic:
24
+ def __init__(self, model_dir, load_jit=True, load_onnx=False, fast = False, fp16=True, hub="modelscope"):
25
+ instruct = True if '-Instruct' in model_dir else False
26
+
27
+ if model_dir is None:
28
+ model_dir = f"pretrained_models/InspireMusic-1.5B-Long"
29
+
30
+ if not os.path.isfile(f"{model_dir}/llm.pt"):
31
+ model_name = model_dir.split("/")[-1]
32
+ if hub == "modelscope":
33
+ from modelscope import snapshot_download
34
+ if model_name == "InspireMusic-Base":
35
+ snapshot_download(f"iic/InspireMusic", local_dir=model_dir)
36
+ else:
37
+ snapshot_download(f"iic/{model_name}", local_dir=model_dir)
38
+
39
+ assert os.path.exists(f'{model_dir}/inspiremusic.yaml')
40
+ with open('{}/inspiremusic.yaml'.format(model_dir), 'r') as f:
41
+ configs = load_hyperpyyaml(f)
42
+
43
+ self.frontend = InspireMusicFrontEnd(configs,
44
+ configs['get_tokenizer'],
45
+ '{}/llm.pt'.format(model_dir),
46
+ '{}/flow.pt'.format(model_dir),
47
+ '{}/music_tokenizer/'.format(model_dir),
48
+ '{}/wavtokenizer/'.format(model_dir),
49
+ instruct,
50
+ fast,
51
+ fp16,
52
+ configs['allowed_special'])
53
+
54
+ self.model = InspireMusicModel(configs['llm'], configs['flow'], configs['hift'], configs['wavtokenizer'], fast, fp16)
55
+ self.model.load('{}/llm.pt'.format(model_dir),
56
+ '{}/flow.pt'.format(model_dir),
57
+ '{}/music_tokenizer/'.format(model_dir),
58
+ '{}/wavtokenizer/model.pt'.format(model_dir))
59
+ del configs
60
+
61
+ @torch.inference_mode()
62
+ def inference(self, task, text, audio, time_start, time_end, chorus, stream=False, sr=24000):
63
+ if task == "text-to-music":
64
+ for i in tqdm(self.frontend.text_normalize(text, split=True)):
65
+ model_input = self.frontend.frontend_text_to_music(i, time_start, time_end, chorus)
66
+ start_time = time.time()
67
+ logging.info('prompt text {}'.format(i))
68
+ for model_output in self.model.inference(**model_input, stream=stream):
69
+ music_audios_len = model_output['music_audio'].shape[1] / sr
70
+ logging.info('yield music len {}, rtf {}'.format(music_audios_len, (time.time() - start_time) / music_audios_len))
71
+ yield model_output
72
+ start_time = time.time()
73
+
74
+ elif task == "continuation":
75
+ if text is None:
76
+ if audio is not None:
77
+ for i in tqdm(audio):
78
+ model_input = self.frontend.frontend_continuation(None, i, time_start, time_end, chorus, sr, max_audio_length)
79
+ start_time = time.time()
80
+ logging.info('prompt text {}'.format(i))
81
+ for model_output in self.model.continuation_inference(**model_input, stream=stream):
82
+ music_audios_len = model_output['music_audio'].shape[1] / sr
83
+ logging.info('yield music len {}, rtf {}'.format(music_audios_len, (time.time() - start_time) / music_audios_len))
84
+ yield model_output
85
+ start_time = time.time()
86
+ else:
87
+ if audio is not None:
88
+ for i in tqdm(self.frontend.text_normalize(text, split=True)):
89
+ model_input = self.frontend.frontend_continuation(i, audio, time_start, time_end, chorus, sr, max_audio_length)
90
+ start_time = time.time()
91
+ logging.info('prompt text {}'.format(i))
92
+ for model_output in self.model.continuation_inference(**model_input, stream=stream):
93
+ music_audios_len = model_output['music_audio'].shape[1] / sr
94
+ logging.info('yield music len {}, rtf {}'.format(music_audios_len, (time.time() - start_time) / music_audios_len))
95
+ yield model_output
96
+ start_time = time.time()
97
+ else:
98
+ print("Please input text or audio.")
99
+ else:
100
+ print("Currently only support text-to-music and music continuation tasks.")
101
+
102
+ @torch.inference_mode()
103
+ def cli_inference(self, text, audio_prompt, time_start, time_end, chorus, task, stream=False, duration_to_gen=30, sr=24000):
104
+ if task == "text-to-music":
105
+ model_input = self.frontend.frontend_text_to_music(text, time_start, time_end, chorus)
106
+ logging.info('prompt text {}'.format(text))
107
+ elif task == "continuation":
108
+ model_input = self.frontend.frontend_continuation(text, audio_prompt, time_start, time_end, chorus, sr)
109
+ logging.info('prompt audio length: {}'.format(len(audio_prompt)))
110
+
111
+ start_time = time.time()
112
+ for model_output in self.model.inference(**model_input, duration_to_gen=duration_to_gen, task=task):
113
+ music_audios_len = model_output['music_audio'].shape[1] / sr
114
+ logging.info('yield music len {}, rtf {}'.format(music_audios_len, (time.time() - start_time) / music_audios_len))
115
+ yield model_output
116
+ start_time = time.time()
117
+
118
+ @torch.inference_mode()
119
+ def inference_zero_shot(self, text, prompt_text, prompt_audio_16k, stream=False, sr=24000):
120
+ prompt_text = self.frontend.text_normalize(prompt_text, split=False)
121
+ for i in tqdm(self.frontend.text_normalize(text, split=True)):
122
+ model_input = self.frontend.frontend_zero_shot(i, prompt_text, prompt_audio_16k)
123
+ start_time = time.time()
124
+ logging.info('prompt text {}'.format(i))
125
+ for model_output in self.model.inference(**model_input, stream=stream):
126
+ audio_len = model_output['music_audio'].shape[1] / sr
127
+ logging.info('yield audio len {}, rtf {}'.format(audio_len, (time.time() - start_time) / audio_len))
128
+ yield model_output
129
+ start_time = time.time()
130
+ @torch.inference_mode()
131
+ def inference_instruct(self, text, spk_id, instruct_text, stream=False, sr=24000):
132
+ if self.frontend.instruct is False:
133
+ raise ValueError('{} do not support instruct inference'.format(self.model_dir))
134
+ instruct_text = self.frontend.text_normalize(instruct_text, split=False)
135
+ for i in tqdm(self.frontend.text_normalize(text, split=True)):
136
+ model_input = self.frontend.frontend_instruct(i, spk_id, instruct_text)
137
+ start_time = time.time()
138
+ logging.info('prompt text {}'.format(i))
139
+ for model_output in self.model.inference(**model_input, stream=stream):
140
+ audio_len = model_output['music_audio'].shape[1] / sr
141
+ logging.info('yield audio len {}, rtf {}'.format(audio_len, (time.time() - start_time) / audio_len))
142
+ yield model_output
143
+ start_time = time.time()
inspiremusic/cli/model.py ADDED
@@ -0,0 +1,295 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 numpy as np
15
+ import threading
16
+ import time
17
+ from contextlib import nullcontext
18
+ import uuid
19
+ from inspiremusic.music_tokenizer.vqvae import VQVAE
20
+ from inspiremusic.wavtokenizer.decoder.pretrained import WavTokenizer
21
+ from torch.cuda.amp import autocast
22
+ import logging
23
+ import torch
24
+ import os
25
+
26
+
27
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
28
+
29
+ class InspireMusicModel:
30
+
31
+ def __init__(self,
32
+ llm: torch.nn.Module,
33
+ flow: torch.nn.Module,
34
+ music_tokenizer: torch.nn.Module,
35
+ wavtokenizer: torch.nn.Module,
36
+ fast: bool = False,
37
+ fp16: bool = True,
38
+ ):
39
+ self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
40
+ self.llm = llm
41
+ self.flow = flow
42
+ self.music_tokenizer = music_tokenizer
43
+ self.wavtokenizer = wavtokenizer
44
+ self.fp16 = fp16
45
+ self.token_min_hop_len = 100
46
+ self.token_max_hop_len = 200
47
+ self.token_overlap_len = 20
48
+ # mel fade in out
49
+ self.mel_overlap_len = 34
50
+ self.mel_window = np.hamming(2 * self.mel_overlap_len)
51
+ # hift cache
52
+ self.mel_cache_len = 20
53
+ self.source_cache_len = int(self.mel_cache_len * 256)
54
+ # rtf and decoding related
55
+ self.stream_scale_factor = 1
56
+ assert self.stream_scale_factor >= 1, 'stream_scale_factor should be greater than 1, change it according to your actual rtf'
57
+ self.llm_context = torch.cuda.stream(torch.cuda.Stream(self.device)) if torch.cuda.is_available() else nullcontext()
58
+ self.lock = threading.Lock()
59
+ # dict used to store session related variable
60
+ self.music_token_dict = {}
61
+ self.llm_end_dict = {}
62
+ self.mel_overlap_dict = {}
63
+ self.fast = fast
64
+ self.generator = "hifi"
65
+
66
+ def load(self, llm_model, flow_model, hift_model, wavtokenizer_model):
67
+ if llm_model is not None:
68
+ self.llm.load_state_dict(torch.load(llm_model, map_location=self.device))
69
+ self.llm.to(self.device).eval()
70
+ else:
71
+ self.llm = None
72
+ if flow_model is not None:
73
+ self.flow.load_state_dict(torch.load(flow_model, map_location=self.device))
74
+ self.flow.to(self.device).eval()
75
+ if hift_model is not None:
76
+ if ".pt" not in hift_model:
77
+ self.music_tokenizer = VQVAE( hift_model + '/config.json',
78
+ hift_model + '/model.pt', with_encoder=True)
79
+ else:
80
+ self.music_tokenizer = VQVAE(os.path.dirname(hift_model) + '/config.json',
81
+ hift_model, with_encoder=True)
82
+ self.music_tokenizer.to(self.device).eval()
83
+ if wavtokenizer_model is not None:
84
+ if ".pt" not in wavtokenizer_model:
85
+ self.wavtokenizer = WavTokenizer.from_pretrained_feat( wavtokenizer_model + '/config.yaml',
86
+ wavtokenizer_model + '/model.pt')
87
+ else:
88
+ self.wavtokenizer = WavTokenizer.from_pretrained_feat( os.path.dirname(wavtokenizer_model) + '/config.yaml',
89
+ wavtokenizer_model )
90
+ self.wavtokenizer.to(self.device)
91
+
92
+ def load_jit(self, llm_text_encoder_model, llm_llm_model, flow_encoder_model):
93
+ assert self.fp16 is True, "we only provide fp16 jit model, set fp16=True if you want to use jit model"
94
+ llm_text_encoder = torch.jit.load(llm_text_encoder_model, map_location=self.device)
95
+ self.llm.text_encoder = llm_text_encoder
96
+ llm_llm = torch.jit.load(llm_llm_model)
97
+ self.llm.llm = llm_llm
98
+ flow_encoder = torch.jit.load(flow_encoder_model)
99
+ self.flow.encoder = flow_encoder
100
+
101
+ def load_onnx(self, flow_decoder_estimator_model):
102
+ import onnxruntime
103
+ option = onnxruntime.SessionOptions()
104
+ option.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL
105
+ option.intra_op_num_threads = 1
106
+ providers = ['CUDAExecutionProvider' if torch.cuda.is_available() else 'CPUExecutionProvider']
107
+ del self.flow.decoder.estimator
108
+ self.flow.decoder.estimator = onnxruntime.InferenceSession(flow_decoder_estimator_model, sess_options=option, providers=providers)
109
+
110
+ def llm_job(self, text, audio_token, audio_token_len, prompt_text, llm_prompt_audio_token, embeddings, uuid, duration_to_gen, task):
111
+ with self.llm_context:
112
+ local_res = []
113
+ with autocast(enabled=self.fp16):
114
+ inference_kwargs = {
115
+ 'text': text.to(self.device),
116
+ 'text_len': torch.tensor([text.shape[1]], dtype=torch.int32).to(self.device),
117
+ 'prompt_text': prompt_text.to(self.device),
118
+ 'prompt_text_len': torch.tensor([prompt_text.shape[1]], dtype=torch.int32).to(self.device),
119
+ 'prompt_audio_token': llm_prompt_audio_token.to(self.device),
120
+ 'prompt_audio_token_len': torch.tensor([llm_prompt_audio_token.shape[1]], dtype=torch.int32).to(self.device),
121
+ 'embeddings': embeddings,
122
+ 'duration_to_gen': duration_to_gen,
123
+ 'task': task
124
+ }
125
+
126
+ if audio_token is not None:
127
+ inference_kwargs['audio_token'] = audio_token.to(self.device)
128
+ else:
129
+ inference_kwargs['audio_token'] = torch.Tensor([0]).to(self.device)
130
+
131
+ if audio_token_len is not None:
132
+ inference_kwargs['audio_token_len'] = audio_token_len.to(self.device)
133
+ else:
134
+ inference_kwargs['audio_token_len'] = torch.Tensor([0]).to(self.device)
135
+
136
+ for i in self.llm.inference(**inference_kwargs):
137
+ local_res.append(i)
138
+
139
+ self.music_token_dict[uuid] = local_res
140
+ self.llm_end_dict[uuid] = True
141
+
142
+ # def token2wav(self, token, token_len, text, text_len, uuid, sample_rate, finalize=False):
143
+ def token2wav(self, token, token_len, uuid, sample_rate, finalize=False, flow_cfg=None):
144
+ # if self.flow is not None:
145
+ # if isinstance(self.flow,MaskedDiffWithText):
146
+ # codec_embed = self.flow.inference(token=token.to(self.device),
147
+ # token_len=token_len.to(self.device),
148
+ # text_token=text,
149
+ # text_token_len=text_len,
150
+ # )
151
+ # else:
152
+ if flow_cfg is not None:
153
+ codec_embed = self.flow.inference_cfg(token=token.to(self.device),
154
+ token_len=token_len.to(self.device),
155
+ sample_rate=sample_rate
156
+ )
157
+ else:
158
+ codec_embed = self.flow.inference(token=token.to(self.device),
159
+ token_len=token_len.to(self.device),
160
+ sample_rate=sample_rate
161
+ )
162
+ # use music_tokenizer decoder
163
+ wav = self.music_tokenizer.generator(codec_embed)
164
+ wav = wav.squeeze(0).cpu().detach()
165
+ return wav
166
+
167
+ def acoustictoken2wav(self, token):
168
+ # use music_tokenizer to generate waveform from token
169
+ token = token.view(token.size(0), -1, 4)
170
+ # codec = token.view(1, -1, 4)
171
+ codec_embed = self.music_tokenizer.quantizer.embed(torch.tensor(token).long().to(self.device)).cuda()
172
+ wav = self.music_tokenizer.generator(codec_embed)
173
+ wav = wav.squeeze(0).cpu().detach()
174
+ return wav
175
+
176
+ def semantictoken2wav(self, token):
177
+ # fast mode, use wavtokenizer decoder
178
+ new_tensor = torch.tensor(token.to(self.device)).unsqueeze(0)
179
+ features = self.wavtokenizer.codes_to_features(new_tensor)
180
+ bandwidth_id = torch.tensor([0]).to(self.device)
181
+ wav = self.wavtokenizer.to(self.device).decode(features, bandwidth_id=bandwidth_id)
182
+ wav = wav.cpu().detach()
183
+ return wav
184
+
185
+ @torch.inference_mode()
186
+ def inference(self, text, audio_token, audio_token_len, text_token, text_token_len, embeddings=None,
187
+ prompt_text=torch.zeros(1, 0, dtype=torch.int32),
188
+ llm_prompt_audio_token=torch.zeros(1, 0, dtype=torch.int32),
189
+ flow_prompt_audio_token=torch.zeros(1, 0, dtype=torch.int32),
190
+ prompt_audio_feat=torch.zeros(1, 0, 80), sample_rate=48000, duration_to_gen = 30, task="continuation", trim = True, stream=False, **kwargs):
191
+
192
+ # this_uuid is used to track variables related to this inference thread
193
+ # support tasks:
194
+ # text to music task
195
+ # music continuation task
196
+ # require either audio input only or text and audio inputs
197
+
198
+ this_uuid = str(uuid.uuid1())
199
+
200
+ if self.llm:
201
+ with self.lock:
202
+ self.music_token_dict[this_uuid], self.llm_end_dict[this_uuid] = [], False
203
+
204
+ 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))
205
+ p.start()
206
+
207
+ if stream is True:
208
+ token_hop_len = self.token_min_hop_len
209
+ while True:
210
+ time.sleep(0.1)
211
+ if len(self.music_token_dict[this_uuid]) >= token_hop_len + self.token_overlap_len:
212
+ this_music_audio = self.token2wav(token=text_token,
213
+ token_len=text_token_len,
214
+ uuid=this_uuid,
215
+ sample_rate=sample_rate,
216
+ finalize=False)
217
+ yield {'music_audio': this_music_audio.cpu()}
218
+ with self.lock:
219
+ self.music_token_dict[this_uuid] = self.music_token_dict[this_uuid][token_hop_len:]
220
+ # increase token_hop_len for better audio quality
221
+ token_hop_len = min(self.token_max_hop_len, int(token_hop_len * self.stream_scale_factor))
222
+ if self.llm_end_dict[this_uuid] is True and len(self.music_token_dict[this_uuid]) < token_hop_len + self.token_overlap_len:
223
+ break
224
+ p.join()
225
+ # deal with remain tokens, make sure inference remain token len equals token_hop_len when cache_speech is not None
226
+ this_music_token = torch.concat(self.music_token_dict[this_uuid], dim=1)
227
+ with self.flow_hift_context:
228
+ this_music_audio = self.token2wav(token=this_music_token,
229
+ prompt_token=flow_prompt_audio_token,
230
+ prompt_feat=prompt_audio_feat,
231
+ embedding=flow_embedding,
232
+ uuid=this_uuid,
233
+ sample_rate=sample_rate,
234
+ finalize=True)
235
+ yield {'music_audio': this_music_audio.cpu()}
236
+ else:
237
+ # deal with all tokens
238
+ if self.fast:
239
+ if task == "reconstruct":
240
+ assert audio_token is None
241
+ this_music_token = audio_token
242
+ this_music_audio = self.acoustictoken2wav(token=this_music_token)
243
+ else:
244
+ if self.llm:
245
+ p.join()
246
+ print(len(self.music_token_dict[this_uuid]))
247
+ this_music_token = torch.concat(self.music_token_dict[this_uuid], dim=1)
248
+ print(this_music_token.shape)
249
+ else:
250
+ this_music_token = text_token
251
+
252
+ logging.info("using wavtokenizer generator without flow matching")
253
+ this_music_audio = self.semantictoken2wav(token=this_music_token)
254
+ print(this_music_audio.shape)
255
+
256
+ else:
257
+ if self.llm:
258
+ p.join()
259
+ if len(self.music_token_dict[this_uuid]) != 0:
260
+ this_music_token = torch.concat(self.music_token_dict[this_uuid], dim=1)
261
+ else:
262
+ print(f"The list of tensors is empty for UUID: {this_uuid}")
263
+ else:
264
+ this_music_token = text_token
265
+ logging.info(f"LLM generated audio token length: {this_music_token.shape[1]}")
266
+ logging.info(f"using flow matching and {self.generator} generator")
267
+
268
+ if self.generator == "hifi":
269
+ if (embeddings[1] - embeddings[0]) <= duration_to_gen:
270
+ if trim:
271
+ trim_length = (int((embeddings[1] - embeddings[0])*75))
272
+ this_music_token = this_music_token[:, :trim_length]
273
+ logging.info(f"After trimmed, generated audio token length: {this_music_token.shape[1]}")
274
+ elif (embeddings[1] - embeddings[0]) < 1:
275
+ logging.info(f"Given audio length={(embeddings[1] - embeddings[0])}, which is too short, please give a longer audio length.")
276
+
277
+ this_music_audio = self.token2wav(token=this_music_token,
278
+ token_len=torch.LongTensor([this_music_token.size(1)]),
279
+ uuid=this_uuid,
280
+ sample_rate=sample_rate,
281
+ finalize=True)
282
+ logging.info(f"Generated audio sequence length: {this_music_audio.shape[1]}")
283
+ elif self.generator == "wavtokenizer":
284
+ if (embeddings[1] - embeddings[0]) < duration_to_gen:
285
+ if trim:
286
+ trim_length = (int((embeddings[1] - embeddings[0])*75))
287
+ this_music_token = this_music_token[:,:trim_length]
288
+ logging.info(f"After trimmed, generated audio token length: {this_music_token.shape[1]}")
289
+ elif (embeddings[1] - embeddings[0]) < 1:
290
+ logging.info(f"Given audio length={(embeddings[1] - embeddings[0])}, which is too short, please give a longer audio length.")
291
+
292
+ this_music_audio = self.semantictoken2wav(token=this_music_token)
293
+
294
+ yield {'music_audio': this_music_audio.cpu()}
295
+ 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,409 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.float16)
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 = 300,
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 = 0.9 * 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
+
391
+ if i < int(min_len):
392
+ logp[self.audio_token_size] = torch.tensor(float('-inf'), dtype=torch.float16)
393
+
394
+ if i < int(min_len):
395
+ logp[self.audio_token_size] = torch.tensor(float('-inf'), dtype=torch.float16)
396
+
397
+ top_ids = self.sampling_ids(logp, out_tokens, ignore_eos=i < min_len).item()
398
+
399
+ if top_ids == self.audio_token_size:
400
+ break
401
+
402
+ # # in stream mode, yield token one by one
403
+
404
+ yield torch.tensor([[top_ids]], dtype=torch.int64, device=device)
405
+ out_tokens.append(top_ids)
406
+ offset += lm_input.size(1)
407
+ lm_input = self.speech_embedding.weight[top_ids].reshape(1, 1, -1)
408
+ if infer_cfg:
409
+ 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