huggingface / distil-whisper

Distilled variant of Whisper for speech recognition. 6x faster, 50% smaller, within 1% word error rate.
MIT License
3.33k stars 238 forks source link

beamsize > 1 #26

Open GuoYi0 opened 7 months ago

GuoYi0 commented 7 months ago

How to deal with the situations when beamsize > 1 ?

sanchit-gandhi commented 7 months ago

Hey @GuoYi0 - you can activate beam search with the num_beams argument to generate:

- result = pipe(sample)
+ result = pipe(sample, generate_kwargs:{"num_beams": 5})

In the end-to-end example:

import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
from datasets import load_dataset

device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

model_id = "distil-whisper/distil-large-v2"

model = AutoModelForSpeechSeq2Seq.from_pretrained(
    model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
)
model.to(device)

processor = AutoProcessor.from_pretrained(model_id)

pipe = pipeline(
    "automatic-speech-recognition",
    model=model,
    tokenizer=processor.tokenizer,
    feature_extractor=processor.feature_extractor,
    max_new_tokens=128,
    chunk_length_s=15,
    batch_size=16,
    torch_dtype=torch_dtype,
    device=device,
)

dataset = load_dataset("distil-whisper/librispeech_long", "clean", split="validation")
sample = dataset[0]["audio"]

result = pipe(sample, generate_kwargs:{"num_beams": 5})
print(result["text"])
GuoYi0 commented 7 months ago

@sanchit-gandhi Sincerely thank you for your reply. What I want to know is,how to deal with beamsize >1 in speculative decoding?When draft model generated 4 beams, for example, and the target model reject three and accept one, then what can I do next?

GuoYi0 commented 7 months ago

Also, could you please tell me when the code will be released?

patrickvonplaten commented 7 months ago

@sanchit-gandhi Sincerely thank you for your reply. What I want to know is,how to deal with beamsize >1 in speculative decoding?When draft model generated 4 beams, for example, and the target model reject three and accept one, then what can I do next?

Hey @GuoYi0,

I'm currently working on getting speculative decoding working for batch sizes > 1. It's already working on this branch https://github.com/huggingface/transformers/pull/26875 but needs some cleaning before it can be merged. You can however already try it out by installing Transformers as follows:

- pip install transformers
+ pip install git+https://github.com/huggingface/transformers.git@assistant_decoding_batch

And then you should be able to run the following snippet:

from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
import torch

device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

model_id = "openai/whisper-large-v2"

model = AutoModelForSpeechSeq2Seq.from_pretrained(
    model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
)
model.to(device)

processor = AutoProcessor.from_pretrained(model_id)

from transformers import AutoModelForCausalLM
assistant_model_id = "distil-whisper/distil-large-v2"

assistant_model = AutoModelForCausalLM.from_pretrained(
    assistant_model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
)
assistant_model.to(device)

pipe = pipeline(
    "automatic-speech-recognition",
    model=model,
    tokenizer=processor.tokenizer,
    feature_extractor=processor.feature_extractor,
    max_new_tokens=128,
    generate_kwargs={"assistant_model": assistant_model},
    torch_dtype=torch_dtype,
    device=device,
    batch_size=4,
)
patrickvonplaten commented 7 months ago

Also see this issue: https://github.com/huggingface/distil-whisper/issues/11

souvikqb commented 7 months ago

@patrickvonplaten @sanchit-gandhi Does distil whisper support parameters like best_of, vad_filter, prompt ?

patrickvonplaten commented 7 months ago

@souvikqb, please open a new issue as this question is not related to beamsize