Views
No views yet
--- Eval Results ---
Comparing 'openai/whisper-large-v3-turbo' with 'marianbasti/whisper-large-v3-turbo-latam/' on 3952 samples of "mozilla-foundation/common_voice_17_0" (Spanish subset, evaluation split).
--- Word error rate ---
Default model (openai/whisper-large-v3-turbo): 15.44%
Finetuned model (marianbasti/whisper-large-v3-turbo-lataml): 7.80%
Improvement: 7.64% (better)
--- Average inference time ---
Default model: 0.56 seconds per sample
Custom model: 0.34 seconds per sample
Speed improvement: 1.62x faster1pip install --upgrade pip
2pip install --upgrade transformers datasets[audio] acceleratepipeline
class to transcribe audios of arbitrary length:1import torch
2from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
3from datasets import load_dataset
4
5
6device = "cuda:0" if torch.cuda.is_available() else "cpu"
7torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
8
9model_id = "marianbasti/whisper-large-v3-turbo-latam"
10
11model = AutoModelForSpeechSeq2Seq.from_pretrained(
12 model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
13)
14model.to(device)
15
16processor = AutoProcessor.from_pretrained(model_id)
17
18pipe = pipeline(
19 "automatic-speech-recognition",
20 model=model,
21 tokenizer=processor.tokenizer,
22 feature_extractor=processor.feature_extractor,
23 torch_dtype=torch_dtype,
24 device=device,
25)
26
27dataset = load_dataset("distil-whisper/librispeech_long", "clean", split="validation")
28sample = dataset[0]["audio"]
29
30result = pipe(sample)
31print(result["text"])result = pipe("audio.mp3")batch_size parameter:result = pipe(["audio_1.mp3", "audio_2.mp3"], batch_size=2)1generate_kwargs = {
2 "max_new_tokens": 448,
3 "num_beams": 1,
4 "condition_on_prev_tokens": False,
5 "compression_ratio_threshold": 1.35, # zlib compression ratio threshold (in token space)
6 "temperature": (0.0, 0.2, 0.4, 0.6, 0.8, 1.0),
7 "logprob_threshold": -1.0,
8 "no_speech_threshold": 0.6,
9 "return_timestamps": True,
10}
11
12result = pipe(sample, generate_kwargs=generate_kwargs)result = pipe(sample, generate_kwargs={"language": "english"})"translate":result = pipe(sample, generate_kwargs={"task": "translate"})return_timestamps argument:1result = pipe(sample, return_timestamps=True)
2print(result["chunks"])1result = pipe(sample, return_timestamps="word")
2print(result["chunks"])1result = pipe(sample, return_timestamps=True, generate_kwargs={"language": "french", "task": "translate"})
2print(result["chunks"])1import torch
2from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
3from datasets import Audio, load_dataset
4
5
6device = "cuda:0" if torch.cuda.is_available() else "cpu"
7torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
8
9model_id = "marianbasti/whisper-large-v3-turbo-latam"
10
11model = AutoModelForSpeechSeq2Seq.from_pretrained(
12 model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True
13)
14model.to(device)
15
16processor = AutoProcessor.from_pretrained(model_id)
17
18dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
19dataset = dataset.cast_column("audio", Audio(processor.feature_extractor.sampling_rate))
20sample = dataset[0]["audio"]
21
22inputs = processor(
23 sample["array"],
24 sampling_rate=sample["sampling_rate"],
25 return_tensors="pt",
26 truncation=False,
27 padding="longest",
28 return_attention_mask=True,
29)
30inputs = inputs.to(device, dtype=torch_dtype)
31
32gen_kwargs = {
33 "max_new_tokens": 448,
34 "num_beams": 1,
35 "condition_on_prev_tokens": False,
36 "compression_ratio_threshold": 1.35, # zlib compression ratio threshold (in token space)
37 "temperature": (0.0, 0.2, 0.4, 0.6, 0.8, 1.0),
38 "logprob_threshold": -1.0,
39 "no_speech_threshold": 0.6,
40 "return_timestamps": True,
41}
42
43pred_ids = model.generate(**inputs, **gen_kwargs)
44pred_text = processor.batch_decode(pred_ids, skip_special_tokens=True, decode_with_timestamps=False)
45
46print(pred_text)chunk_length_s
parameter to the pipeline. For large-v3, a chunk length of 30-seconds is optimal. To activate batching over long
audio files, pass the argument batch_size:1import torch
2from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
3from datasets import load_dataset
4
5
6device = "cuda:0" if torch.cuda.is_available() else "cpu"
7torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
8
9model_id = "marianbasti/whisper-large-v3-turbo-latam"
10
11model = AutoModelForSpeechSeq2Seq.from_pretrained(
12 model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True
13)
14model.to(device)
15
16processor = AutoProcessor.from_pretrained(model_id)
17
18pipe = pipeline(
19 "automatic-speech-recognition",
20 model=model,
21 tokenizer=processor.tokenizer,
22 feature_extractor=processor.feature_extractor,
23 chunk_length_s=30,
24 batch_size=16, # batch size for inference - set based on your device
25 torch_dtype=torch_dtype,
26 device=device,
27)
28
29dataset = load_dataset("distil-whisper/librispeech_long", "clean", split="validation")
30sample = dataset[0]["audio"]
31
32result = pipe(sample)
33print(result["text"])torch.compile
for 4.5x speed-ups.torch.compile is currently not compatible with the Chunked long-form algorithm or Flash Attention 2 ⚠️1import torch
2from torch.nn.attention import SDPBackend, sdpa_kernel
3from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
4from datasets import load_dataset
5from tqdm import tqdm
6
7torch.set_float32_matmul_precision("high")
8
9device = "cuda:0" if torch.cuda.is_available() else "cpu"
10torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
11
12model_id = "marianbasti/whisper-large-v3-turbo-latam"
13
14model = AutoModelForSpeechSeq2Seq.from_pretrained(
15 model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True
16).to(device)
17
18# Enable static cache and compile the forward pass
19model.generation_config.cache_implementation = "static"
20model.generation_config.max_new_tokens = 256
21model.forward = torch.compile(model.forward, mode="reduce-overhead", fullgraph=True)
22
23processor = AutoProcessor.from_pretrained(model_id)
24
25pipe = pipeline(
26 "automatic-speech-recognition",
27 model=model,
28 tokenizer=processor.tokenizer,
29 feature_extractor=processor.feature_extractor,
30 torch_dtype=torch_dtype,
31 device=device,
32)
33
34dataset = load_dataset("distil-whisper/librispeech_long", "clean", split="validation")
35sample = dataset[0]["audio"]
36
37# 2 warmup steps
38for _ in tqdm(range(2), desc="Warm-up step"):
39 with sdpa_kernel(SDPBackend.MATH):
40 result = pipe(sample.copy(), generate_kwargs={"min_new_tokens": 256, "max_new_tokens": 256})
41
42# fast run
43with sdpa_kernel(SDPBackend.MATH):
44 result = pipe(sample.copy())
45
46print(result["text"])pip install flash-attn --no-build-isolationattn_implementation="flash_attention_2" to from_pretrained:model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, attn_implementation="flash_attention_2")1from transformers.utils import is_torch_sdpa_available
2
3print(is_torch_sdpa_available())True, you have a valid version of PyTorch installed and SDPA is activated by default. If it
returns False, you need to upgrade your PyTorch version according to the official instructionsattn_implementation="sdpa" as follows:model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, attn_implementation="sdpa")1@misc{radford2022whisper,
2 doi = {10.48550/ARXIV.2212.04356},
3 url = {https://arxiv.org/abs/2212.04356},
4 author = {Radford, Alec and Kim, Jong Wook and Xu, Tao and Brockman, Greg and McLeavey, Christine and Sutskever, Ilya},
5 title = {Robust Speech Recognition via Large-Scale Weak Supervision},
6 publisher = {arXiv},
7 year = {2022},
8 copyright = {arXiv.org perpetual, non-exclusive license}
9}