Skip to content

[WIP][Speculative Decoding] Enable batch size > 1 - #26875

Closed
patrickvonplaten wants to merge 9 commits into
mainfrom
assistant_decoding_batch
Closed

patrickvonplaten wants to merge 9 commits into
mainfrom
assistant_decoding_batch

Conversation

@patrickvonplaten

@patrickvonplaten patrickvonplaten commented Oct 17, 2023 •

Copy link
Copy Markdown
Contributor

What does this PR do?

This PR enables speculative decoding when batch size is > 1.

This PR has been tested to work correctly for Whisper. You can try it as follows:

from transformers import pipeline, AutoModelForCausalLM, AutoModelForSpeechSeq2Seq, AutoProcessor
import torch
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

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)

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)

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,
    chunk_length_s=15,
    batch_size=4,
    device=device,
)

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

result = pipe(sample)
print(result["text"])

TODOS

  • Test that it works correctly for Whisper
  • Test for Llama
  • Write nice tests
  • Clean code
  • Write docs

@patrickvonplaten
patrickvonplaten marked this pull request as draft October 17, 2023 16:43
Comment thread src/transformers/generation/utils.py Resolved
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint.


# 5.2. Get the valid continuation, after the matching tokens
valid_tokens = selected_tokens[:, : n_matches + 1]
valid_tokens = selected_tokens[:, : n_matches.max() + 1]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a question about a part that I don't understand, given that the results are identical with and without batched spec. decoding

this assumes that all the sequences will have the same number of correct matches, when we choose the accepted tokens number based on the maximum correct matches, we also accept wrong tokens from sequences that had lower number of correct matches than the maximum, the solution would be to choose the minimum which guarantees that all the accepted tokens are correct

Suggested change
valid_tokens = selected_tokens[:, : n_matches.max() + 1]
valid_tokens = selected_tokens[:, : n_matches.min() + 1]

that poses a risk that if a single sequence had zero correct matches, this whole spec. decoding cycle will be discarded for all other sequences even if they have correct matches.

is it possible to use padding to accept different number of candidate tokens per batch element as opposed to accepting the minimum/maximum common number of correct suggested tokens across the batch, this should at least keep the speedups of spec decoding that were observed at small batch sizes (if not even faster at larger sizer)

@learning-chip learning-chip Jan 23, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to use padding to accept different number of candidate tokens per batch element as opposed to accepting the minimum/maximum common number of correct suggested tokens across the batch

This is possible if the padding lengths are adjusted at every iteration. For one sample that progresses slower, it should be padded more, to keep the same length as fast-progress samples.

@preminstrel

Copy link
Copy Markdown

Hi, good work! I am curious about how to deal with the wrong kv cache token rolling back for draft model, or you just mask them?

@github-actions

github-actions Bot commented Jan 7, 2024

Copy link
Copy Markdown
Contributor

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

@github-actions github-actions Bot closed this Jan 15, 2024
@bruno-hays

Copy link
Copy Markdown
Contributor

@patrickvonplaten Is there any plan to merge this PR sometimes or is it canceled ?

@asartran

Copy link
Copy Markdown
Contributor

@Hubert-Bonisseur The PR has closed due to inactivity. I'm not sure of the current status of batch > 1 for speculative decoding, but I'm sure @gante will know :)

@bruno-hays

Copy link
Copy Markdown
Contributor

It is still not supported :
ValueError: assisted generate is only supported for batch_size = 1

But I see that Patrick has left HF since writing this PR, which probably explains why it stalled.
If there is still interest in this PR, I'm willing to help with finish the work of Patrick. It seems like it is very close to be ready and I'm vert interested in this feature.

@bruno-hays

Copy link
Copy Markdown
Contributor

Maybe your whisper expert @sanchit-gandhi also knows the status of this feature ?

@sanchit-gandhi

Copy link
Copy Markdown
Contributor

Hey @Hubert-Bonisseur - handing this one over to @kamilakesbi who will take over the integration!

@bruno-hays

Copy link
Copy Markdown
Contributor

Hey @kamilakesbi, just checking in to see if there are any updates on this. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants