-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusicgen_lora.py
1594 lines (1387 loc) · 66.9 KB
/
musicgen_lora.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging
import os
import sys
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Union
import datasets
import numpy as np
import torch
from datasets import DatasetDict, load_dataset
import transformers
from transformers import (
AutoConfig,
AutoFeatureExtractor,
AutoModelForTextToWaveform,
AutoModel,
AutoProcessor,
HfArgumentParser,
Seq2SeqTrainer,
Seq2SeqTrainingArguments,
set_seed,
)
from transformers.trainer_utils import get_last_checkpoint, is_main_process
from transformers.utils import check_min_version, send_example_telemetry
from transformers.utils.versions import require_version
from transformers.integrations import is_wandb_available
from multiprocess import set_start_method
# 检查 Transformers 的最小版本是否为 "4.40.0.dev0"
check_min_version("4.40.0.dev0")
# 检查 Datasets 的最小版本是否为 "2.12.0"
require_version("datasets>=2.12.0")
# 获取当前模块的日志记录器
logger = logging.getLogger(__name__)
def list_field(default=None, metadata=None):
"""
创建一个返回列表的字段,默认值为 `default`,并附加 `metadata`。
Args:
default (list, optional): 默认的列表值。默认为 None。
metadata (dict, optional): 附加的元数据信息。默认为 None。
Returns:
dataclasses.field: 配置好的数据类字段。
"""
# 使用 lambda 函数作为 default_factory,确保每次实例化时都返回一个新的列表
return field(default_factory=lambda: default, metadata=metadata)
class MusicgenTrainer(Seq2SeqTrainer):
def _pad_tensors_to_max_len(self, tensor, max_length):
"""
将张量填充到指定的最大长度。
如果张量的长度小于最大长度,则在末尾填充指定的填充标记(pad token)。
如果张量的长度大于或等于最大长度,则截断到最大长度。
Args:
tensor (torch.Tensor): 需要填充的张量,形状应为 [batch_size, seq_length, ...]。
max_length (int): 填充后的最大长度。
Returns:
torch.Tensor: 填充后的张量,形状为 [batch_size, max_length, ...]。
Raises:
ValueError: 如果模型配置中没有设置 pad_token_id。
"""
# 检查 tokenizer 是否存在并且具有 pad_token_id 属性
if self.tokenizer is not None and hasattr(self.tokenizer, "pad_token_id"):
# If PAD token is not defined at least EOS token has to be defined
# 如果 tokenizer 有 pad_token_id,则使用它
pad_token_id = (
self.tokenizer.pad_token_id
if self.tokenizer.pad_token_id is not None
# 如果没有 pad_token_id,则使用 eos_token_id 作为备选
else self.tokenizer.eos_token_id
)
else:
# 如果 tokenizer 不存在或没有 pad_token_id,则检查模型配置中是否有 pad_token_id
if self.model.config.pad_token_id is not None:
# 使用模型配置中的 pad_token_id
pad_token_id = self.model.config.pad_token_id
else:
# 如果模型配置中也没有设置 pad_token_id,则抛出错误
raise ValueError(
"Pad_token_id must be set in the configuration of the model, in order to pad tensors"
)
# 创建一个填充后的张量,形状为 [batch_size, max_length, tensor.shape[2]]
# 填充值为 pad_token_id
padded_tensor = pad_token_id * torch.ones(
(tensor.shape[0], max_length, tensor.shape[2]),
dtype=tensor.dtype,
device=tensor.device,
)
# 计算需要填充的长度,确保不超过最大长度
length = min(max_length, tensor.shape[1])
# 将原始张量的前 `length` 个时间步赋值给填充后的张量
padded_tensor[:, :length] = tensor[:, :length]
# 返回填充后的张量
return padded_tensor
@dataclass
class ModelArguments:
"""
Arguments pertaining to which model/config/tokenizer we are going to fine-tune from.
"""
"""
与我们将要微调的具体模型配置/分词器相关的参数。
"""
# 预训练模型的路径或模型标识符。可以是本地路径或 Hugging Face 模型库中的名称。
model_name_or_path: str = field(
metadata={
"help": "Path to pretrained model or model identifier from huggingface.co/models"
}
)
# 预训练配置名称或路径。如果未指定,则默认使用 model_name 对应的配置。
config_name: Optional[str] = field(
default=None,
metadata={
"help": "Pretrained config name or path if not the same as model_name"
},
)
# 预训练处理器名称或路径。如果未指定,则默认使用 model_name 对应的处理器。
processor_name: Optional[str] = field(
default=None,
metadata={
"help": "Pretrained processor name or path if not the same as model_name"
},
)
# 预训练模型下载后存储的缓存目录。如果未指定,则使用默认缓存目录。
cache_dir: Optional[str] = field(
default=None,
metadata={
"help": "Where to store the pretrained models downloaded from huggingface.co"
},
)
# 是否使用快速分词器(基于 tokenizers 库)。默认为 True。
use_fast_tokenizer: bool = field(
default=True,
metadata={
"help": "Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."
},
)
# 要使用的模型版本。可以是分支名称、标签名称或提交ID。默认为 "main"。
model_revision: str = field(
default="main",
metadata={
"help": "The specific model version to use (can be a branch name, tag name or commit id)."
},
)
# 填充标记的ID。如果指定,则更改模型的填充标记ID。
pad_token_id: int = field(
default=None,
metadata={"help": "If specified, change the model pad token id."},
)
# 解码器起始标记的ID。如果指定,则更改模型的解码器起始标记ID。
decoder_start_token_id: int = field(
default=None,
metadata={"help": "If specified, change the model decoder start token id."},
)
# 是否冻结文本编码器。如果为 True,则在训练过程中不更新文本编码器的参数。默认为 True。
freeze_text_encoder: bool = field(
default=True,
metadata={"help": "Whether to freeze the text encoder."},
)
# 用于在评估期间计算音频相似度的模型路径或标识符。默认为 "laion/larger_clap_music_and_speech"。
clap_model_name_or_path: str = field(
default="laion/larger_clap_music_and_speech",
metadata={
"help": "Used to compute audio similarity during evaluation. Path to pretrained model or model identifier from huggingface.co/models"
},
)
# 是否使用 Lora 技术进行模型微调。默认为 False。
use_lora: bool = field(
default=False,
metadata={"help": "Whether to use Lora."},
)
# 模型的指导尺度。如果指定,则更改模型的指导尺度。
guidance_scale: float = field(
default=None,
metadata={"help": "If specified, change the model guidance scale."},
)
@dataclass
class DataSeq2SeqTrainingArguments:
"""
Arguments pertaining to what data we are going to input our model for training and eval.
Using `HfArgumentParser` we can turn this class into argparse arguments to be able to specify them on the command line.
"""
"""
与我们将输入模型进行训练和评估的数据相关的参数。
使用 `HfArgumentParser` 可以将这个类转换为命令行参数,以便在命令行中指定它们。
"""
# 要使用的数据集的配置名称(通过 datasets 库)。
dataset_name: str = field(
metadata={
"help": "The configuration name of the dataset to use (via the datasets library)."
}
)
# 数据集的配置名称(通过 datasets 库)。如果未指定,则使用默认配置。
dataset_config_name: str = field(
default=None,
metadata={
"help": "The configuration name of the dataset to use (via the datasets library)."
},
)
# 要使用的训练数据集分割的名称(通过 datasets 库)。默认为 "train+validation"。
train_split_name: str = field(
default="train+validation",
metadata={
"help": (
"The name of the training data set split to use (via the datasets library). Defaults to "
"'train+validation'"
)
},
)
# 要使用的评估数据集分割的名称(通过 datasets 库)。默认为 'test'。
eval_split_name: str = field(
default="test",
metadata={
"help": "The name of the evaluation data set split to use (via the datasets library). Defaults to 'test'"
},
)
# 包含目标音频数据的列名。默认为 'audio'。
target_audio_column_name: str = field(
default="audio",
metadata={
"help": "The name of the dataset column containing the target audio data. Defaults to 'audio'"
},
)
# 如果设置,则为包含文本数据的描述列的名称。如果未设置,应将 `add_metadata` 设置为 True,以自动生成音乐描述。
text_column_name: str = field(
default=None,
metadata={
"help": "If set, the name of the description column containing the text data. If not, you should set `add_metadata` to True, to automatically generates music descriptions ."
},
)
# 如果设置并且 `add_metadata=True`,则将实例提示添加到音乐描述中。这允许使用此实例提示作为锚点,让模型学习将其与数据集的特定性相关联。
instance_prompt: str = field(
default=None,
metadata={
"help": "If set and `add_metadata=True`, will add the instance prompt to the music description. For example, if you set this to `punk`, `punk` will be added to the descriptions. This allows to use this instance prompt as an anchor for your model to learn to associate it to the specificities of your dataset."
},
)
# 如果设置,则为包含条件音频数据的列名。这是完全可选的,仅用于条件引导生成。
conditional_audio_column_name: str = field(
default=None,
metadata={
"help": "If set, the name of the dataset column containing conditional audio data. This is entirely optional and only used for conditional guided generation."
},
)
# 是否覆盖缓存的预处理数据集。
overwrite_cache: bool = field(
default=False,
metadata={"help": "Overwrite the cached preprocessed datasets or not."},
)
# 用于预处理的进程数。
preprocessing_num_workers: Optional[int] = field(
default=None,
metadata={"help": "The number of processes to use for the preprocessing."},
)
# 出于调试目的或加快训练速度,如果设置,则将训练示例的数量截断为此值。
max_train_samples: Optional[int] = field(
default=None,
metadata={
"help": (
"For debugging purposes or quicker training, truncate the number of training examples to this "
"value if set."
)
},
)
# 出于调试目的或加快训练速度,如果设置,则将验证示例的数量截断为此值。
max_eval_samples: Optional[int] = field(
default=None,
metadata={
"help": (
"For debugging purposes or quicker training, truncate the number of validation examples to this "
"value if set."
)
},
)
# 过滤超过 `max_duration_in_seconds` 秒的音频文件。
max_duration_in_seconds: float = field(
default=30.0,
metadata={
"help": (
"Filter audio files that are longer than `max_duration_in_seconds` seconds to"
" 'max_duration_in_seconds`"
)
},
)
# 过滤少于 `min_duration_in_seconds` 秒的音频文件。
min_duration_in_seconds: float = field(
default=0.0,
metadata={
"help": "Filter audio files that are shorter than `min_duration_in_seconds` seconds"
},
)
# 在评估期间将使用此提示作为额外的生成样本。
full_generation_sample_text: str = field(
default="80s blues track.",
metadata={
"help": (
"This prompt will be used during evaluation as an additional generated sample."
)
},
)
# 用于远程文件的 HTTP 承载授权的令牌。如果未指定,将使用运行 `huggingface-cli login` 时生成的令牌(存储在 `~/.huggingface` 中)。
token: str = field(
default=None,
metadata={
"help": (
"The token to use as HTTP bearer authorization for remote files. If not specified, will use the token "
"generated when running `huggingface-cli login` (stored in `~/.huggingface`)."
)
},
)
# `use_auth_token` 参数已弃用,将在 v4.34 中删除。请使用 `token` 代替。
use_auth_token: bool = field(
default=None,
metadata={
"help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token` instead."
},
)
# 是否允许在 Hub 上定义自己的模型文件中的自定义模型。此选项应仅对您信任的存储库以及您已阅读代码的存储库设置为 `True`。
trust_remote_code: bool = field(
default=False,
metadata={
"help": (
"Whether or not to allow for custom models defined on the Hub in their own modeling files. This option "
"should only be set to `True` for repositories you trust and in which you have read the code, as it will "
"execute code present on the Hub on your local machine."
)
},
)
# 如果设置并且 `wandb` 在 args.report_to 中,则将生成的音频样本添加到 wandb 日志中。在训练开始和结束时生成音频以显示演变。
add_audio_samples_to_wandb: bool = field(
default=False,
metadata={
"help": "If set and if `wandb` in args.report_to, will add generated audio samples to wandb logs."
"Generates audio at the beginning and the end of the training to show evolution."
},
)
# 如果 `True`,则使用 librosa 和 msclap 自动生成歌曲描述。不要忘记安装这些库:`pip install msclap librosa`。
add_metadata: bool = field(
default=False,
metadata={
"help": (
"If `True`, automatically generates song descriptions, using librosa and msclap."
"Don't forget to install these libraries: `pip install msclap librosa`"
)
},
)
# 如果指定并且 `add_metadata=True`,则将丰富的数据集推送到 Hub。如果只想计算一次,这很有用。
push_metadata_repo_id: str = field(
default=None,
metadata={
"help": (
"if specified and `add_metada=True`, will push the enriched dataset to the hub. Useful if you want to compute it only once."
)
},
)
# 如果使用 `wandb` 进行日志记录,则表示要生成的测试集样本数量。
num_samples_to_generate: int = field(
default=4,
metadata={
"help": (
"If logging with `wandb`, indicates the number of samples from the test set to generate"
)
},
)
# 如果设置,则使用 demucs 执行音频分离。
audio_separation: bool = field(
default=False,
metadata={"help": ("If set, performs audio separation using demucs.")},
)
# 如果 `audio_separation`,则表示传递给 demucs 的批量大小。
audio_separation_batch_size: int = field(
default=10,
metadata={
"help": (
"If `audio_separation`, indicates the batch size passed to demucs."
)
},
)
@dataclass
class DataCollatorMusicGenWithPadding:
"""
Data collator that will dynamically pad the inputs received.
Args:
processor (:class:`~transformers.AutoProcessor`)
The processor used for proccessing the data.
padding (:obj:`bool`, :obj:`str` or :class:`~transformers.tokenization_utils_base.PaddingStrategy`, `optional`, defaults to :obj:`True`):
Select a strategy to pad the returned sequences (according to the model's padding side and padding index)
among:
* :obj:`True` or :obj:`'longest'`: Pad to the longest sequence in the batch (or no padding if only a single
sequence if provided).
* :obj:`'max_length'`: Pad to a maximum length specified with the argument :obj:`max_length` or to the
maximum acceptable input length for the model if that argument is not provided.
* :obj:`False` or :obj:`'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of
different lengths).
"""
"""
数据收集器,将动态填充接收到的输入。
Args:
processor (:class:`~transformers.AutoProcessor`):
用于处理数据的处理器。
padding (:obj:`bool`, :obj:`str` 或 :class:`~transformers.tokenization_utils_base.PaddingStrategy`, 可选, 默认为 :obj:`True`):
选择填充返回序列的策略(根据模型的填充侧和填充索引),选项包括:
* :obj:`True` 或 :obj:`'longest'`: 填充到批次中最长的序列(如果只提供一个序列,则不填充)。
* :obj:`'max_length'`: 填充到通过参数 :obj:`max_length` 指定的最大长度,或者如果未提供该参数,则填充到模型可接受的最大输入长度。
* :obj:`False` 或 :obj:`'do_not_pad'`(默认): 不填充(即,可以输出包含不同长度序列的批次)。
"""
# 用于处理数据的处理器
processor: AutoProcessor
# 填充策略,默认为 "longest"
padding: Union[bool, str] = "longest"
# 特征提取器输入名称,默认为 "input_values"
feature_extractor_input_name: Optional[str] = "input_values"
def __call__(
self, features: List[Dict[str, Union[List[int], torch.Tensor]]]
) -> Dict[str, torch.Tensor]:
"""
处理输入数据并返回填充后的批次。
Args:
features (List[Dict[str, Union[List[int], torch.Tensor]]]): 输入特征列表,每个特征都是一个字典,包含输入ID和标签。
Returns:
Dict[str, torch.Tensor]: 填充后的批次数据。
"""
# 分离输入和标签,因为它们需要不同的长度和不同的填充方法
labels = [
torch.tensor(feature["labels"]).transpose(0, 1) for feature in features
]
# (bsz, seq_len, num_codebooks)
# (batch_size, sequence_length, num_codebooks)
# 对标签进行填充,填充值为 -100
labels = torch.nn.utils.rnn.pad_sequence(
labels, batch_first=True, padding_value=-100
)
# 对输入ID进行填充
input_ids = [{"input_ids": feature["input_ids"]} for feature in features]
input_ids = self.processor.tokenizer.pad(input_ids, return_tensors="pt")
# 构建批次字典
batch = {"labels": labels, **input_ids}
# 如果特征中包含特征提取器的输入名称,则对输入值进行填充
if self.feature_extractor_input_name in features[0]:
input_values = [
{
self.feature_extractor_input_name: feature[
self.feature_extractor_input_name
]
}
for feature in features
]
input_values = self.processor.feature_extractor.pad(
input_values, return_tensors="pt"
)
# 将填充后的输入值添加到批次字典中
batch[self.feature_extractor_input_name : input_values]
return batch
def main():
# See all possible arguments in src/transformers/training_args.py
# or by passing the --help flag to this script.
# We now keep distinct sets of args, for a cleaner separation of concerns.
"""
主函数,负责解析参数、设置日志、检测检查点以及启动训练或评估过程。
"""
# 查看所有可能的参数,可以在 src/transformers/training_args.py 中找到
# 或者通过向此脚本传递 --help 标志来查看。
# 我们现在保持不同的参数集,以便更清晰地分离关注点。
# 初始化参数解析器,传入模型参数、数据参数和训练参数类
parser = HfArgumentParser(
(ModelArguments, DataSeq2SeqTrainingArguments, Seq2SeqTrainingArguments)
)
# 判断命令行参数数量和格式
if len(sys.argv) == 2 and sys.argv[1].endswith(".json"):
# 如果我们只向脚本传递一个参数,并且它是一个 JSON 文件的路径,
# 那么我们解析这个 JSON 文件来获取我们的参数。
model_args, data_args, training_args = parser.parse_json_file(
json_file=os.path.abspath(sys.argv[1])
)
else:
# 否则,从命令行参数中解析参数到数据类中
model_args, data_args, training_args = parser.parse_args_into_dataclasses()
# 发送遥测数据。跟踪示例用法有助于我们更好地分配资源以维护它们。
# 发送的信息包括作为参数传递的信息以及您的 Python/PyTorch 版本。
send_example_telemetry("run_musicgen_melody", model_args, data_args)
# Detecting last checkpoint.
# 检测上一个检查点
last_checkpoint = None
if (
os.path.isdir(training_args.output_dir) # 检查输出目录是否存在
and training_args.do_train # 检查是否需要训练
and not training_args.overwrite_output_dir # 检查是否不需要覆盖输出目录
):
# 获取最后一个检查点
last_checkpoint = get_last_checkpoint(training_args.output_dir)
if last_checkpoint is None and len(os.listdir(training_args.output_dir)) > 0:
# 如果没有找到检查点,但输出目录不为空,则抛出错误
raise ValueError(
f"Output directory ({training_args.output_dir}) already exists and is not empty. "
"Use --overwrite_output_dir to overcome."
)
elif last_checkpoint is not None:
# 如果找到了检查点,则记录日志信息
logger.info(
f"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change "
"the `--output_dir` or add `--overwrite_output_dir` to train from scratch."
)
# Setup logging
# 设置日志记录
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", # 日志格式
datefmt="%m/%d/%Y %H:%M:%S", # 日期格式
handlers=[logging.StreamHandler(sys.stdout)], # 日志处理器,输出到标准输出
)
# 在每个进程上记录简要摘要
logger.setLevel(
logging.INFO if is_main_process(training_args.local_rank) else logging.WARN
)
# Log on each process the small summary:
# 在每个进程上记录简要摘要
logger.warning(
f"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}, "
f"distributed training: {training_args.parallel_mode.value == 'distributed'}, 16-bits training: {training_args.fp16}"
)
# Set the verbosity to info of the Transformers logger (on main process only):
# 仅在主进程上将 Transformers 日志的详细程度设置为信息级别
if is_main_process(training_args.local_rank):
transformers.utils.logging.set_verbosity_info()
# 记录训练/评估参数
logger.info("Training/evaluation parameters %s", training_args)
# Set seed before initializing model.
# 在初始化模型之前设置随机种子,以确保结果的可重复性
set_seed(training_args.seed)
# 1. First, let's load the dataset
# 1. 首先,让我们加载数据集
# 创建一个空的 DatasetDict 对象,用于存储原始数据集
raw_datasets = DatasetDict()
# 获取数据预处理的进程数
num_workers = data_args.preprocessing_num_workers
# 获取是否添加元数据的标志
add_metadata = data_args.add_metadata
# 检查 add_metadata 和 text_column_name 是否同时为 True
if add_metadata and data_args.text_column_name:
raise ValueError(
"add_metadata and text_column_name are both True, chose the former if you want automatically generated music descriptions or the latter if you want to use your own set of descriptions."
)
if training_args.do_train:
# 如果需要训练,则加载训练数据集
raw_datasets["train"] = load_dataset(
data_args.dataset_name, # 数据集名称
data_args.dataset_config_name, # 数据集配置名称
split=data_args.train_split_name, # 训练数据集分割名称
num_proc=num_workers, # 预处理进程数
)
# 检查目标音频列是否存在于训练数据集中
if data_args.target_audio_column_name not in raw_datasets["train"].column_names:
raise ValueError(
f"--target_audio_column_name '{data_args.target_audio_column_name}' not found in dataset '{data_args.dataset_name}'."
" Make sure to set `--target_audio_column_name` to the correct audio column - one of"
f" {', '.join(raw_datasets['train'].column_names)}."
)
# 检查是否提供了实例提示或文本列
if data_args.instance_prompt is not None:
logger.warning(
f"Using the following instance prompt: {data_args.instance_prompt}"
)
elif data_args.text_column_name not in raw_datasets["train"].column_names:
raise ValueError(
f"--text_column_name {data_args.text_column_name} not found in dataset '{data_args.dataset_name}'. "
"Make sure to set `--text_column_name` to the correct text column - one of "
f"{', '.join(raw_datasets['train'].column_names)}."
)
elif data_args.text_column_name is None and data_args.instance_prompt is None:
raise ValueError("--instance_prompt or --text_column_name must be set.")
# 如果设置了最大训练样本数,则截断数据集
if data_args.max_train_samples is not None:
raw_datasets["train"] = (
raw_datasets["train"]
.shuffle() # 打乱数据集
.select(range(data_args.max_train_samples)) # 选择前 max_train_samples 个样本
)
if training_args.do_eval:
# 如果需要评估,则加载评估数据集
raw_datasets["eval"] = load_dataset(
data_args.dataset_name, # 数据集名称
data_args.dataset_config_name, # 数据集配置名称
split=data_args.eval_split_name, # 评估数据集分割名称
num_proc=num_workers, # 预处理进程数
)
# 如果设置了最大评估样本数,则截断数据集
if data_args.max_eval_samples is not None:
raw_datasets["eval"] = raw_datasets["eval"].select(
# 选择前 max_eval_samples 个样本
range(data_args.max_eval_samples)
)
if data_args.audio_separation:
try:
from demucs import pretrained
except ImportError:
print(
"To perform audio separation, you should install additional packages, run: `pip install -e .[metadata]]` or `pip install demucs`."
)
# 从 demucs.apply 导入 apply_model 函数,用于应用分离模型
from demucs.apply import apply_model
# 从 demucs.audio 导入 convert_audio 函数,用于转换音频格式
from demucs.audio import convert_audio
# 从 datasets 导入 Audio 类,用于处理音频数据
from datasets import Audio
# 加载预训练的 demucs 模型
demucs = pretrained.get_model("htdemucs")
# 检查是否有可用的 GPU
if torch.cuda.device_count() > 0:
# 如果有 GPU,将模型移动到第一个 GPU
demucs.to("cuda:0")
# 定义目标音频列的名称
audio_column_name = data_args.target_audio_column_name
# 定义一个函数,用于将音频数据转换为适合 demucs 处理的格式
def wrap_audio(audio, sr):
return {"array": audio.cpu().numpy(), "sampling_rate": sr}
# 定义一个函数,用于过滤音频的各个部分(人声、鼓、贝斯等)
def filter_stems(batch, rank=None):
# 如果有可用的 GPU,则使用 GPU,否则使用 CPU
device = "cpu" if torch.cuda.device_count() == 0 else "cuda:0"
if rank is not None:
# move the model to the right GPU if not there already
# 如果有多个 GPU,则根据进程编号分配 GPU
device = f"cuda:{(rank or 0)% torch.cuda.device_count()}"
# move to device and create pipeline here because the pipeline moves to the first GPU it finds anyway
# 将模型移动到指定的 GPU
demucs.to(device)
# 检查音频数据是否为列表类型
if isinstance(batch[audio_column_name], list):
# 将音频数据转换为 PyTorch 张量,并移动到指定的设备
wavs = [
convert_audio(
torch.tensor(audio["array"][None], device=device).to(
torch.float32
),
audio["sampling_rate"], # 原始采样率
demucs.samplerate, # demucs 模型的采样率
demucs.audio_channels, # 音频通道数
).T
for audio in batch["audio"]
]
# 获取每个音频的长度
wavs_length = [audio.shape[0] for audio in wavs]
# 对音频数据进行填充,使其长度一
wavs = torch.nn.utils.rnn.pad_sequence(
wavs, batch_first=True, padding_value=0.0
).transpose(1, 2)
# 应用 demucs 模型进行音频分离
stems = apply_model(demucs, wavs)
# 对分离后的音频进行后处理
batch[audio_column_name] = [
wrap_audio(s[:-1, :, :length].sum(0).mean(0), demucs.samplerate)
for (s, length) in zip(stems, wavs_length)
]
else:
# 如果音频数据不是列表类型,则直接处理单个音频文件
audio = torch.tensor(
batch[audio_column_name]["array"].squeeze(), device=device
).to(torch.float32)
sample_rate = batch[audio_column_name]["sampling_rate"]
audio = convert_audio(
audio, sample_rate, demucs.samplerate, demucs.audio_channels
)
# 应用 demucs 模型进行音频分离
stems = apply_model(demucs, audio[None])
# 对分离后的音频进行后处理
batch[audio_column_name] = wrap_audio(
stems[0, :-1].mean(0), demucs.samplerate
)
return batch
# 计算进程数,如果有多块 GPU,则使用 GPU 数,否则使用预处理的进程数
num_proc = (
torch.cuda.device_count() if torch.cuda.device_count() >= 1 else num_workers
)
# 应用音频分离函数到数据集
raw_datasets = raw_datasets.map(
filter_stems,
batched=True,
batch_size=data_args.audio_separation_batch_size,
with_rank=True,
num_proc=num_proc,
)
# 将音频列转换为 Audio 类型
raw_datasets = raw_datasets.cast_column(audio_column_name, Audio())
# 删除模型以释放内存
del demucs
if add_metadata:
try:
from msclap import CLAP
import librosa
except ImportError:
print(
"To add metadata, you should install additional packages, run: `pip install -e .[metadata]]"
)
# 从 labels 模块导入乐器类别、类型标签和情绪主题类别
from labels import instrument_classes, genre_labels, mood_theme_classes
# 导入 tempfile 模块,用于创建临时目录和文件
import tempfile
# 导入 torchaudio,用于处理音频文件
import torchaudio
# 导入 random,用于随机打乱元数据
import random
# 初始化 CLAP 模型,version 为 "2023",不使用 CUDA
clap_model = CLAP(version="2023", use_cuda=False)
# 获取乐器类别的文本嵌入
instrument_embeddings = clap_model.get_text_embeddings(instrument_classes)
# 获取类型标签的文本嵌入
genre_embeddings = clap_model.get_text_embeddings(genre_labels)
# 获取情绪主题类别的文本嵌入
mood_embeddings = clap_model.get_text_embeddings(mood_theme_classes)
# 定义一个函数,用于丰富文本数据
def enrich_text(batch):
# 从 batch 中提取音频数据和采样率
audio, sampling_rate = (
batch["audio"]["array"],
batch["audio"]["sampling_rate"],
)
# 使用 librosa 计算节奏(BPM)和和弦特征
tempo, _ = librosa.beat.beat_track(y=audio, sr=sampling_rate)
tempo = f"{str(round(tempo))} bpm" # 通常不准确
chroma = librosa.feature.chroma_stft(y=audio, sr=sampling_rate)
# 计算最可能的调性
key = np.argmax(np.sum(chroma, axis=1))
key = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"][key]
# 使用临时目录保存音频文件
with tempfile.TemporaryDirectory() as tempdir:
path = os.path.join(tempdir, "tmp.wav")
torchaudio.save(path, torch.tensor(audio).unsqueeze(0), sampling_rate)
# 获取音频嵌入
audio_embeddings = clap_model.get_audio_embeddings([path])
# 计算与乐器类别的相似度,并选择最相似的类别
instrument = clap_model.compute_similarity(
audio_embeddings, instrument_embeddings
).argmax(dim=1)[0]
# 计算与类型标签的相似度,并选择最相似的类别
genre = clap_model.compute_similarity(
audio_embeddings, genre_embeddings
).argmax(dim=1)[0]
# 计算与情绪主题类别的相似度,并选择最相似的类别
mood = clap_model.compute_similarity(
audio_embeddings, mood_embeddings
).argmax(dim=1)[0]
# 将索引转换为实际的类别名称
instrument = instrument_classes[instrument]
genre = genre_labels[genre]
mood = mood_theme_classes[mood]
# 组合元数据
metadata = [key, tempo, instrument, genre, mood]
# 随机打乱元数据顺序
random.shuffle(metadata)
# 将元数据列表转换为字符串
batch["metadata"] = ", ".join(metadata)
return batch
# 使用 enrich_text 函数处理数据集
raw_datasets = raw_datasets.map(
enrich_text,
# 如果有 GPU,则使用单进程,否则使用预处理进程数
num_proc=1 if torch.cuda.device_count() > 0 else num_workers,
desc="add metadata",
)
# 删除模型和嵌入以释放内存
del clap_model, instrument_embeddings, genre_embeddings, mood_embeddings
# 如果指定了 push_metadata_repo_id,则将丰富后的数据集推送到 Hub
if data_args.push_metadata_repo_id:
raw_datasets.push_to_hub(data_args.push_metadata_repo_id)
# 3. Next, let's load the config as we might need it to create
# load config
# 3. 接下来,让我们加载配置,因为我们可能需要它来创建模型
# 加载配置
config = AutoConfig.from_pretrained(
model_args.model_name_or_path, # 模型名称或路径
cache_dir=model_args.cache_dir, # 缓存目录
token=data_args.token,
trust_remote_code=data_args.trust_remote_code, # 是否信任远程代码
revision=model_args.model_revision, # 模型版本
)
# update pad token id and decoder_start_token_id
# 更新填充标记ID和解码器起始标记ID
config.update(
{
"pad_token_id": model_args.pad_token_id
if model_args.pad_token_id is not None
else model.config.pad_token_id, # 如果指定了填充标记ID,则使用它,否则使用模型的填充标记ID
"decoder_start_token_id": model_args.decoder_start_token_id
if model_args.decoder_start_token_id is not None
else model.config.decoder_start_token_id, # 如果指定了解码器起始标记ID,则使用它,否则使用模型解码器的起始标记ID
}
)
config.decoder.update(
{
"pad_token_id": model_args.pad_token_id
if model_args.pad_token_id is not None
else model.config.decoder.pad_token_id, # 如果指定了填充标记ID,则使用它,否则使用模型解码器的填充标记ID
"decoder_start_token_id": model_args.decoder_start_token_id
if model_args.decoder_start_token_id is not None
else model.config.decoder.decoder_start_token_id, # 如果指定了解码器起始标记ID,则使用它,否则使用模型解码器的解码器起始标记ID
}
)
# 4. Now we can instantiate the processor and model
# 4. 现在我们可以实例化处理器和模型
# Note for distributed training, the .from_pretrained methods guarantee that only
# one local process can concurrently download model & vocab.
# 注意对于分布式训练,`.from_pretrained` 方法保证只有一个本地进程可以同时下载模型和词汇表。
# load processor
# 加载处理器
processor = AutoProcessor.from_pretrained(
model_args.processor_name or model_args.model_name_or_path, # 使用处理器名称或模型名称/路径
cache_dir=model_args.cache_dir, # 缓存目录
token=data_args.token,
trust_remote_code=data_args.trust_remote_code, # 是否信任远程代码
)
# 处理实例提示和完整生成样本文本
instance_prompt = data_args.instance_prompt
instance_prompt_tokenized = None
full_generation_sample_text = data_args.full_generation_sample_text
if data_args.instance_prompt is not None:
# 如果提供了实例提示,则对其进行分词
instance_prompt_tokenized = processor.tokenizer(instance_prompt)
if full_generation_sample_text is not None:
# 如果提供了完整生成样本文本,则对其进行分词并返回张量
full_generation_sample_text = processor.tokenizer(
full_generation_sample_text, return_tensors="pt"
)
# create model
# 创建模型
model = AutoModelForTextToWaveform.from_pretrained(
model_args.model_name_or_path, # 模型名称或路径
cache_dir=model_args.cache_dir, # 缓存目录
config=config, # 模型配置
token=data_args.token,
trust_remote_code=data_args.trust_remote_code, # 是否信任远程代码
revision=model_args.model_revision, # 模型版本
)
# take audio_encoder_feature_extractor
# 获取音频编码器的特征提取器
audio_encoder_feature_extractor = AutoFeatureExtractor.from_pretrained(
model.config.audio_encoder._name_or_path, # 音频编码器的名称或路径
)
# 5. Now we preprocess the datasets including loading the audio, resampling and normalization
# 5. 现在我们预处理数据集,包括加载音频、重采样和归一化
# Thankfully, `datasets` takes care of automatically loading and resampling the audio,
# so that we just need to set the correct target sampling rate and normalize the input
# via the `feature_extractor`
# 值得庆幸的是,`datasets` 自动处理加载和重采样音频,
# 所以我们只需要通过 `feature_extractor` 设置正确的目标采样率和归一化输入
# resample target audio
# 重采样目标音频
dataset_sampling_rate = (
next(iter(raw_datasets.values())) # 获取第一个数据集
.features[data_args.target_audio_column_name] # 获取目标音频列的特征
.sampling_rate # 获取采样率
)
if dataset_sampling_rate != audio_encoder_feature_extractor.sampling_rate:
# 如果数据集的采样率与特征提取器的采样率不同,则进行重采样
raw_datasets = raw_datasets.cast_column(
data_args.target_audio_column_name,
datasets.features.Audio(
sampling_rate=audio_encoder_feature_extractor.sampling_rate
),
)
# 如果有条件音频列,则也进行重采样
if data_args.conditional_audio_column_name is not None:
dataset_sampling_rate = (
next(iter(raw_datasets.values())) # 获取第一个数据集
.features[data_args.conditional_audio_column_name] # 获取条件音频列的特征
.sampling_rate # 获取采样率
)
if dataset_sampling_rate != processor.feature_extractor.sampling_rate:
# 如果数据集的采样率与处理器的特征提取器的采样率不同,则进行重采样
raw_datasets = raw_datasets.cast_column(
data_args.conditional_audio_column_name,
datasets.features.Audio(
sampling_rate=processor.feature_extractor.sampling_rate
),
)
# derive max & min input length for sample rate & max duration
# 根据采样率和最大时长计算输入的最大和最小长度