|
|
|
from transformers import AutoTokenizer, AutoProcessor, AutoConfig, AutoFeatureExtractor, AutoTokenizer, AutoProcessor, \ |
|
AutoModelForSpeechSeq2Seq, set_seed, get_linear_schedule_with_warmup |
|
from datasets import load_dataset, DatasetDict, interleave_datasets, IterableDatasetDict |
|
from transformers.models.whisper.english_normalizer import BasicTextNormalizer |
|
import transformers |
|
import argparse |
|
import datasets |
|
import evaluate |
|
import string |
|
from accelerate import Accelerator |
|
from dataclasses import dataclass |
|
from typing import Any, Dict, List, Optional, Union |
|
import torch |
|
import os |
|
from tqdm.auto import tqdm |
|
import numpy as np |
|
|
|
|
|
def load_streaming_dataset(dataset_name, dataset_config_name, split, **kwargs): |
|
if "+" in split: |
|
|
|
dataset_splits = [load_dataset(dataset_name, dataset_config_name, split=split_name, streaming=True, **kwargs) for split_name in split.split("+")] |
|
|
|
interleaved_dataset = interleave_datasets(dataset_splits) |
|
return interleaved_dataset |
|
else: |
|
|
|
dataset = load_dataset(dataset_name, dataset_config_name, split=split, streaming=True, **kwargs) |
|
return dataset |
|
|
|
|
|
def train(args, accelerator: Accelerator): |
|
|
|
raw_datasets = IterableDatasetDict() |
|
raw_datasets["train"] = load_streaming_dataset(args.train_dataset_name, args.train_dataset_config_name, |
|
split=args.train_split_name, |
|
cache_dir=args.data_cache_dir) |
|
raw_datasets["validation"] = load_streaming_dataset(args.validation_dataset_name, |
|
args.validation_dataset_config_name, |
|
split=args.validation_split_name, |
|
cache_dir=args.data_cache_dir) |
|
|
|
|
|
|
|
|
|
assert args.audio_column in raw_datasets["train"].column_names |
|
assert args.text_column in raw_datasets["train"].column_names |
|
|
|
with accelerator.main_process_first(): |
|
if args.max_train_samples is not None: |
|
raw_datasets["train"] = raw_datasets["train"].take(args.max_train_samples) |
|
if args.max_val_samples is not None: |
|
raw_datasets["validation"] = raw_datasets["validation"].take(args.max_val_samples) |
|
|
|
student_config = AutoConfig.from_pretrained(args.student_model_name_or_path, cache_dir=args.student_cache_dir) |
|
teacher_config = AutoConfig.from_pretrained(args.teacher_model_name_or_path, cache_dir=args.teacher_cache_dir) |
|
|
|
|
|
feature_extractor = AutoFeatureExtractor.from_pretrained(args.teacher_model_name_or_path, cache_dir=args.teacher_cache_dir) |
|
tokenizer = AutoTokenizer.from_pretrained(args.teacher_model_name_or_path, cache_dir=args.teacher_cache_dir) |
|
processor = AutoProcessor.from_pretrained(args.teacher_model_name_or_path, cache_dir=args.teacher_cache_dir) |
|
|
|
|
|
assert teacher_config.decoder_start_token_id is not None |
|
assert student_config.decoder_start_token_id is not None |
|
|
|
|
|
|
|
student_config.forced_decoder_ids = None |
|
|
|
teacher_config.forced_decoder_ids = None |
|
|
|
student_config.suppress_tokens = [] |
|
teacher_config.suppress_tokens = [] |
|
|
|
student_model = AutoModelForSpeechSeq2Seq.from_pretrained(args.student_model_name_or_path, config=student_config) |
|
teacher_model = AutoModelForSpeechSeq2Seq.from_pretrained(args.teacher_model_name_or_path, config=teacher_config, |
|
cache_dir=args.teacher_cache_dir) |
|
|
|
accelerator.print( |
|
f"Loaded the model on device: student: {student_model.device}, teacher:{teacher_model.device}, accelerator:{accelerator.device}") |
|
|
|
|
|
for p in teacher_model.parameters(): |
|
p.requires_grad = False |
|
|
|
if args.freeze_encoder: |
|
accelerator.print("Freezing encoder") |
|
student_model.freeze_encoder() |
|
student_model.model.encoder.gradient_checkpointing = False |
|
|
|
|
|
with accelerator.main_process_first(): |
|
|
|
|
|
raw_datasets = raw_datasets.cast_column(args.audio_column, datasets.Audio(sampling_rate=16000)) |
|
|
|
|
|
|
|
normalizer = BasicTextNormalizer() |
|
|
|
def prepare_dataset(batch): |
|
|
|
sample = batch[args.audio_column] |
|
|
|
batch["input_features"] = \ |
|
processor.feature_extractor(sample["array"], sampling_rate=sample["sampling_rate"]).input_features[0] |
|
|
|
batch["input_length"] = len(sample["array"]) / sample["sampling_rate"] |
|
|
|
transcription = batch[args.text_column] |
|
if not args.keep_case: |
|
transcription = transcription.lower() |
|
if not args.keep_punctuation: |
|
transcription = normalizer(transcription).strip() |
|
batch["labels"] = processor.tokenizer(transcription).input_ids |
|
return batch |
|
|
|
with accelerator.main_process_first(): |
|
vectorized_datasets = raw_datasets.map(prepare_dataset, |
|
remove_columns=raw_datasets["train"].column_names) |
|
|
|
|
|
def is_audio_in_length_range(length): |
|
|
|
return args.min_duration_in_seconds <= length <= args.max_duration_in_seconds |
|
|
|
with accelerator.main_process_first(): |
|
vectorized_datasets = vectorized_datasets.filter(is_audio_in_length_range, |
|
input_columns=["input_length"]) |
|
|
|
@dataclass |
|
class DataCollatorForSeq2SeqWithPadding: |
|
processor: Any |
|
|
|
def __call__(self, features: List[Union[Dict[str, torch.Tensor], Dict[str, Any]]]) -> Dict[str, torch.Tensor]: |
|
|
|
|
|
input_features = [{"input_features": feature["input_features"]} for feature in features] |
|
batch = self.processor.feature_extractor.pad(input_features, return_tensors="pt") |
|
|
|
|
|
label_features = [{"input_ids": feature["labels"]} for feature in features] |
|
labels_batch = self.processor.tokenizer.pad(label_features, return_tensors="pt") |
|
|
|
labels = labels_batch["input_ids"].masked_fill(labels_batch.attention_mask.ne(1), -100) |
|
|
|
|
|
if (labels[:, 0] == self.processor.tokenizer.bos_token_id).all().cpu().item(): |
|
labels = labels[:, 1:] |
|
|
|
batch["labels"] = labels |
|
return batch |
|
|
|
data_collator = DataCollatorForSeq2SeqWithPadding(processor=processor) |
|
|
|
|
|
train_dataloader = torch.utils.data.DataLoader(vectorized_datasets["train"], shuffle=False, collate_fn=data_collator, |
|
batch_size=args.per_device_train_batch_size) |
|
eval_dataloader = torch.utils.data.DataLoader(vectorized_datasets["validation"], shuffle=False, |
|
collate_fn=data_collator, batch_size=args.per_device_eval_batch_size) |
|
|
|
|
|
optimizer = torch.optim.AdamW(list(student_model.parameters()), lr=args.learning_rate) |
|
|
|
|
|
lr_scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=args.warmup_steps, |
|
num_training_steps=args.train_steps) |
|
|
|
|
|
|
|
student_model, teacher_model, optimizer, train_dataloader, lr_scheduler = accelerator.prepare( |
|
student_model, teacher_model, optimizer, train_dataloader, lr_scheduler) |
|
accelerator.print( |
|
f"Distributed the model on device: student: {student_model.device}, teacher:{teacher_model.device}, accelerator:{accelerator.device}") |
|
|
|
global_step = 0 |
|
total_loss = 0 |
|
total_kl_loss = 0 |
|
total_ce_loss = 0 |
|
|
|
if args.resume_from_checkpoint is not None: |
|
accelerator.print(f"Loading checkpoint: {args.resume_from_checkpoint}") |
|
accelerator.load_state(args.resume_from_checkpoint) |
|
steps_completed = int(args.resume_from_checkpoint.split("-")[-1]) |
|
global_step += steps_completed |
|
train_dataloader = accelerator.skip_first_batches(train_dataloader, steps_completed) |
|
|
|
|
|
wer_metric = evaluate.load("wer") |
|
cer_metric = evaluate.load("cer") |
|
all_punctuations = list(string.punctuation.replace("'", "")) |
|
|
|
def compute_metrics(preds, labels): |
|
|
|
for idx in range(len(labels)): |
|
labels[idx][labels[idx] == -100] = tokenizer.pad_token_id |
|
|
|
pred_str = tokenizer.batch_decode(preds, skip_special_tokens=True) |
|
label_str = tokenizer.batch_decode(labels, skip_special_tokens=True) |
|
|
|
pred_str = [_.strip() for _ in pred_str] |
|
label_str = [_.strip() for _ in label_str] |
|
|
|
|
|
spaced_pred_str = [pred_str[i].replace(punctuation, "") for punctuation in all_punctuations for i in |
|
range(len(pred_str))] |
|
spaced_label_str = [label_str[i].replace(punctuation, "") for punctuation in all_punctuations for i in |
|
range(len(label_str))] |
|
|
|
|
|
wer_ortho = 100 * wer_metric.compute(predictions=spaced_pred_str, references=spaced_label_str) |
|
cer_ortho = 100 * cer_metric.compute(predictions=spaced_pred_str, references=spaced_label_str) |
|
accelerator.print( |
|
f"\nspaced_pred_str: {[_ for i, _ in enumerate(spaced_pred_str) if i < 3]}, \nspaced_label_str: {[_ for i, _ in enumerate(spaced_label_str) if i < 3]}") |
|
|
|
norm_pred_str = [normalizer(pred) for pred in pred_str] |
|
norm_label_str = [normalizer(label) for label in label_str] |
|
|
|
norm_pred_str = [norm_pred_str[i] for i in range(len(norm_label_str)) if len(norm_label_str[i]) > 0] |
|
norm_label_str = [norm_label_str[i] for i in range(len(norm_label_str)) if len(norm_label_str[i]) > 0] |
|
accelerator.print( |
|
f"\nnorm_pred_str: {[_ for i, _ in enumerate(norm_pred_str) if i < 3]}, \nnorm_label_str: {[_ for i, _ in enumerate(norm_label_str) if i < 3]}") |
|
|
|
wer = 100 * wer_metric.compute(predictions=norm_pred_str, references=norm_label_str) |
|
cer = 100 * cer_metric.compute(predictions=norm_pred_str, references=norm_label_str) |
|
|
|
return {"wer": wer, "wer_ortho": wer_ortho, "cer": cer, "cer_ortho": cer_ortho}, pred_str, label_str |
|
|
|
|
|
with accelerator.main_process_first(): |
|
output_dir = args.output_dir |
|
feature_extractor.save_pretrained(output_dir) |
|
tokenizer.save_pretrained(output_dir) |
|
student_config.save_pretrained(output_dir) |
|
teacher_config.save_pretrained(output_dir) |
|
|
|
progress_bar = tqdm(range(global_step, args.train_steps), disable=not accelerator.is_main_process) |
|
|
|
while global_step < args.train_steps: |
|
student_model.train() |
|
for batch in train_dataloader: |
|
with accelerator.accumulate(student_model): |
|
|
|
outputs = student_model(**batch) |
|
ce_loss = outputs.loss |
|
logits = outputs.logits |
|
with torch.no_grad(): |
|
teacher_logits = teacher_model(**batch).logits |
|
|
|
kl_loss = torch.nn.functional.kl_div(torch.nn.functional.log_softmax(logits / args.temperature, dim=-1), |
|
torch.nn.functional.softmax(teacher_logits / args.temperature, |
|
dim=-1), |
|
reduction="batchmean") * (args.temperature ** 2) |
|
|
|
loss = args.alpha_ce * ce_loss + args.alpha_distil * kl_loss |
|
|
|
total_kl_loss += kl_loss.detach().item() |
|
total_ce_loss += ce_loss.detach().item() |
|
total_loss += loss.detach().item() |
|
accelerator.backward(loss) |
|
optimizer.step() |
|
lr_scheduler.step() |
|
optimizer.zero_grad() |
|
|
|
global_step += 1 |
|
progress_bar.update(1) |
|
|
|
|
|
eval_metrics = {} |
|
eval_preds = [] |
|
eval_labels = [] |
|
|
|
if global_step % args.eval_steps == 0: |
|
student_model.eval() |
|
valid_loss = 0 |
|
|
|
for batch in eval_dataloader: |
|
with torch.no_grad(): |
|
batch.to(accelerator.device) |
|
references = batch.labels |
|
if not args.predict_without_generate: |
|
|
|
|
|
if accelerator.num_processes > 1: |
|
|
|
predictions = student_model.module.generate(batch.input_features) |
|
else: |
|
predictions = student_model.generate(batch.input_features) |
|
else: |
|
|
|
outputs = student_model(**batch) |
|
valid_loss += outputs.loss.detach().item() |
|
pred_logits = outputs.logits |
|
predictions = pred_logits.argmax(-1) |
|
|
|
|
|
|
|
|
|
predictions, references = accelerator.gather_for_metrics((predictions, references)) |
|
|
|
|
|
|
|
|
|
|
|
for idx, pred in enumerate(predictions): |
|
first_eos_token_idx = (pred == tokenizer.eos_token_id).nonzero(as_tuple=True)[0] |
|
if len(first_eos_token_idx) > 0: |
|
predictions[idx, first_eos_token_idx[0] + 1:] = tokenizer.eos_token_id |
|
|
|
|
|
eval_preds.extend(predictions) |
|
eval_labels.extend(references) |
|
|
|
|
|
accelerator.print(f"\npredictions: {eval_preds[:3]}, \nreferences: {eval_preds[:3]}") |
|
accelerator.print(f"\nlen(eval_preds): {len(eval_preds)}, \nlen(eval_labels): {len(eval_labels)}") |
|
eval_metrics, eval_preds, eval_labels = compute_metrics(eval_preds, eval_labels) |
|
train_loss = total_loss / ( |
|
args.eval_steps * args.per_device_train_batch_size * accelerator.num_processes) |
|
train_kl_loss = total_kl_loss / ( |
|
args.eval_steps * args.per_device_train_batch_size * accelerator.num_processes) |
|
train_ce_loss = total_ce_loss / ( |
|
args.eval_steps * args.per_device_train_batch_size * accelerator.num_processes) |
|
|
|
accelerator.print( |
|
f"Step: {global_step}, Train Loss: {train_loss}, Train KL Loss: {train_kl_loss}, Train CE Loss: {train_ce_loss}, \ |
|
Eval WER: {eval_metrics['wer']}, Eval WER Ortho: {eval_metrics['wer_ortho']}, Eval CER: {eval_metrics['cer']}, \ |
|
Eval CER Ortho: {eval_metrics['cer_ortho']}") |
|
accelerator.log( |
|
{"cer": eval_metrics["cer"], "cer_ortho": eval_metrics["cer_ortho"], "wer": eval_metrics["wer"], |
|
"wer_ortho": eval_metrics["wer_ortho"], |
|
"train_loss": train_loss, |
|
"train_kl_loss": train_kl_loss, |
|
"train_ce_loss": train_ce_loss, |
|
|
|
}) |
|
|
|
output_dir = os.path.join(args.output_dir, f"checkpoint-{global_step}") |
|
|
|
accelerator.save_state(output_dir) |
|
accelerator.wait_for_everyone() |
|
unwrapped_model = accelerator.unwrap_model(student_model) |
|
unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save, |
|
is_main_process=accelerator.is_main_process) |
|
total_loss = 0 |
|
total_kl_loss = 0 |
|
total_ce_loss = 0 |
|
student_model.train() |
|
|
|
|
|
def main(): |
|
parser = argparse.ArgumentParser() |
|
parser.add_argument("--teacher_model_name_or_path", type=str, default="openai/whisper-large-v2") |
|
parser.add_argument("--student_model_name_or_path", type=str, default="distil-whisper/large-v2-8") |
|
parser.add_argument("--output_dir", type=str, default="output") |
|
parser.add_argument("--per_device_train_batch_size", type=int, default=16) |
|
parser.add_argument("--per_device_eval_batch_size", type=int, default=16) |
|
parser.add_argument("--learning_rate", type=float, default=2e-5) |
|
parser.add_argument("--freeze_encoder", action="store_true") |
|
parser.add_argument("--temperature", type=float, default=2.0) |
|
parser.add_argument("--alpha_ce", type=float, default=0.5) |
|
parser.add_argument("--alpha_distil", type=float, default=0.5) |
|
parser.add_argument("--language", type=str, default="en") |
|
parser.add_argument("--task", type=str, default="transcribe") |
|
parser.add_argument("--train_steps", type=int, default=100000) |
|
parser.add_argument("--eval_steps", type=int, default=100) |
|
parser.add_argument("--warmup_steps", type=int, default=2000) |
|
parser.add_argument("--gradient_accumulation_steps", type=int, default=4) |
|
parser.add_argument("--seed", type=int, default=42) |
|
parser.add_argument("--data_cache_dir", type=str, default="data/cache") |
|
parser.add_argument("--teacher_cache_dir", type=str, default="model/cache") |
|
parser.add_argument("--student_cache_dir", type=str, default="model/cache") |
|
parser.add_argument("--mixed_precision", type=str, default="fp16") |
|
parser.add_argument("--max_train_samples", type=int, default=None) |
|
parser.add_argument("--max_val_samples", type=int, default=None) |
|
parser.add_argument("--max_test_samples", type=int, default=None) |
|
parser.add_argument("--audio_column", type=str, default="audio") |
|
parser.add_argument("--text_column", type=str, default="text") |
|
parser.add_argument("--max_duration_in_seconds", type=float, default=30) |
|
parser.add_argument("--min_duration_in_seconds", type=float, default=1) |
|
parser.add_argument("--keep_case", action="store_true") |
|
parser.add_argument("--keep_punctuation", action="store_true") |
|
parser.add_argument("--resume_from_checkpoint", type=str, default=None) |
|
parser.add_argument("--num_workers", type=int, default=16) |
|
parser.add_argument("--predict_without_generate", action="store_true") |
|
parser.add_argument("--train_dataset_name", type=str, default="librispeech_asr") |
|
parser.add_argument("--train_dataset_config_name", type=str, default="all") |
|
parser.add_argument("--train_split_name", type=str, default="train.clean.100+train.clean.360+train.other.500") |
|
parser.add_argument("--validation_dataset_name", type=str, default="librispeech_asr") |
|
parser.add_argument("--validation_dataset_config_name", type=str, default="all") |
|
parser.add_argument("--validation_split_name", type=str, default="validation.clean") |
|
|
|
args = parser.parse_args() |
|
|
|
|
|
set_seed(args.seed) |
|
|
|
if args.teacher_model_name_or_path is None or args.student_model_name_or_path is None: |
|
raise ValueError("teacher_model_name_or_path and student_model_name_or_path cannot be None") |
|
|
|
accelerator = Accelerator(mixed_precision=args.mixed_precision, gradient_accumulation_steps=1, |
|
log_with="tensorboard", logging_dir=args.output_dir) |
|
|
|
|
|
if accelerator.is_main_process: |
|
datasets.utils.logging.set_verbosity_info() |
|
transformers.utils.logging.set_verbosity_info() |
|
else: |
|
datasets.utils.logging.set_verbosity_error() |
|
transformers.utils.logging.set_verbosity_error() |
|
|
|
|
|
track_config = {"lr": args.learning_rate, |
|
"train_batch_size": args.per_device_train_batch_size, |
|
"eval_batch_size": args.per_device_eval_batch_size, |
|
"seed": args.seed, |
|
"train_steps": args.train_steps} |
|
accelerator.init_trackers('runs', track_config) |
|
train(args, accelerator) |
|
accelerator.end_training() |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|