-
Notifications
You must be signed in to change notification settings - Fork 33.1k
[whisper] compile compatibility with long-form decoding #31772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sanchit-gandhi
merged 11 commits into
huggingface:main
from
sanchit-gandhi:whisper-long-form-static
Aug 1, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6375d4b
[whisper] compile compatibility with long-form decoding
1fa3637
clarify comment
8507ee0
fix after rebase
sanchit-gandhi aa7577c
finalise
2828f51
fix bsz
4150700
fix cache split
4f69693
remove contiguous
0b6acfb
style
719a80a
finish
21002ac
update doc
ef47d06
prevent cuda graph trace
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -126,12 +126,24 @@ def _get_attr_from_logit_processors(logits_processor, logit_processor_class, att | |
|
|
||
|
|
||
| def _pad_to_max_length( | ||
| current_segments, pad_token_id, device, padding="right", bos_token_tensor=None, cut_off_length=None | ||
| current_segments, | ||
| pad_token_id, | ||
| device, | ||
| padding_side="right", | ||
| padding="longest", | ||
| bos_token_tensor=None, | ||
| cut_off_length=None, | ||
| ): | ||
| max_total_length = 0 | ||
| sequences = [] | ||
| if padding not in ["right", "left"]: | ||
| raise ValueError(f"`padding` must be either 'right' or 'left', not {padding}") | ||
|
|
||
| if padding_side not in ["right", "left"]: | ||
| raise ValueError(f"`padding_side` must be either 'right' or 'left', not {padding_side}") | ||
|
|
||
| if padding not in ["longest", "max_length"]: | ||
| raise ValueError(f"`padding` must be either 'longest' or 'max_length', not {padding}") | ||
| elif padding == "max_length" and cut_off_length is None: | ||
| raise ValueError("`cut_off_length` must be specified when `padding='max_length'`") | ||
|
|
||
| for current_segment_list in current_segments: | ||
| if current_segment_list is not None and len([d["tokens"] for d in current_segment_list]) > 0: | ||
|
|
@@ -150,9 +162,10 @@ def _pad_to_max_length( | |
| else: | ||
| sequences.append(torch.tensor([], device=device)) | ||
|
|
||
| max_total_length = cut_off_length + 1 if padding == "max_length" else max_total_length | ||
| for i in range(len(current_segments)): | ||
| pad_length = max_total_length - len(sequences[i]) | ||
| pad = (0, pad_length) if padding == "right" else (pad_length, 0) | ||
| pad = (0, pad_length) if padding_side == "right" else (pad_length, 0) | ||
| sequences[i] = F.pad(sequences[i], pad=pad, value=pad_token_id) | ||
|
|
||
| sequences = torch.stack(sequences, dim=0) | ||
|
|
@@ -672,6 +685,7 @@ def generate( | |
| return_token_timestamps=return_token_timestamps, | ||
| do_condition_on_prev_tokens=do_condition_on_prev_tokens, | ||
| is_shortform=is_shortform, | ||
| batch_size=batch_size, | ||
|
sanchit-gandhi marked this conversation as resolved.
Outdated
|
||
| kwargs=kwargs, | ||
| ) | ||
|
|
||
|
|
@@ -712,7 +726,7 @@ def generate( | |
| ) | ||
|
|
||
| sequences = _pad_to_max_length( | ||
| final_segments, generation_config.pad_token_id, device=self.device, padding="right" | ||
| final_segments, generation_config.pad_token_id, device=self.device, padding_side="right" | ||
| ) | ||
|
|
||
| # 8. If we return all segments, the predicted output sequences are put under `"sequences"`. | ||
|
|
@@ -775,6 +789,7 @@ def generate_with_fallback( | |
| return_token_timestamps, | ||
| do_condition_on_prev_tokens, | ||
| is_shortform, | ||
| batch_size, | ||
|
sanchit-gandhi marked this conversation as resolved.
Outdated
|
||
| kwargs, | ||
| ): | ||
| kwargs = copy.copy(kwargs) | ||
|
|
@@ -798,6 +813,22 @@ def generate_with_fallback( | |
| for key in ["do_sample", "temperature", "num_beams"]: | ||
| if key in generate_kwargs: | ||
| del generate_kwargs[key] | ||
|
|
||
| cur_bsz = decoder_input_ids.shape[0] | ||
| if generation_config.cache_implementation == "static" and cur_bsz < batch_size: | ||
| segment_input = F.pad(segment_input, (0, 0, 0, 0, 0, batch_size - cur_bsz), value=0) | ||
| decoder_input_ids = F.pad( | ||
| decoder_input_ids, (0, 0, 0, batch_size - cur_bsz), value=generation_config.pad_token_id | ||
| ) | ||
| if generate_kwargs.get("decoder_attention_mask") is not None: | ||
| generate_kwargs["decoder_attention_mask"] = F.pad( | ||
| generate_kwargs["decoder_attention_mask"], (0, 0, 0, batch_size - cur_bsz), value=True | ||
| ) | ||
| if generate_kwargs.get("encoder_outputs") is not None: | ||
| generate_kwargs["encoder_outputs"] = F.pad( | ||
| generate_kwargs["encoder_outputs"], (0, 0, 0, 0, 0, batch_size - cur_bsz), value=0 | ||
| ) | ||
|
ArthurZucker marked this conversation as resolved.
|
||
|
|
||
| seek_outputs = super().generate( | ||
| segment_input, | ||
| generation_config=generation_config, | ||
|
|
@@ -820,6 +851,10 @@ def generate_with_fallback( | |
| is_shortform=is_shortform, | ||
| ) | ||
|
|
||
| if cur_bsz < batch_size: | ||
| seek_sequences = seek_sequences[:cur_bsz] | ||
| seek_outputs = seek_outputs[:cur_bsz] | ||
|
sanchit-gandhi marked this conversation as resolved.
Outdated
|
||
|
|
||
| # 6.7 Extract cut sequences from every sequence and check if fallback should be applied | ||
| # Loop over each decoded audio individually as each decoding can be of a different length | ||
| new_fallback_index_map = [] | ||
|
|
@@ -925,17 +960,27 @@ def split_by_batch_index(values, key, batch_idx, is_shortform): | |
| if not is_shortform: | ||
| # we don't save `past_key_values` as this is too costly for longform | ||
| return None | ||
| elif isinstance(values, EncoderDecoderCache): | ||
| all_past_key_values = [] | ||
| for layer_idx in range(self.config.decoder_layers): | ||
| layer_past_key_values = [] | ||
| for cache_cls in [values.self_attention_cache, values.cross_attention_cache]: | ||
| for v in [cache_cls.key_cache, cache_cls.value_cache]: | ||
| layer_past_key_values.append(v[layer_idx][batch_idx][None].cpu()) | ||
| all_past_key_values.append(tuple(layer_past_key_values)) | ||
| return tuple(all_past_key_values) | ||
| else: | ||
| return tuple(tuple(w[batch_idx][None].cpu() for w in values[v]) for v in range(len(values))) | ||
| all_past_key_values = [] | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just a re-factor to split the single line list iteration over several lines, in order to be more verbose |
||
| for v in range(len(values)): | ||
| layer_past_key_values = [] | ||
| for w in values[v]: | ||
| layer_past_key_values.append(w[batch_idx][None].cpu()) | ||
| all_past_key_values.append(tuple(layer_past_key_values)) | ||
| return tuple(all_past_key_values) | ||
|
|
||
| return values[batch_idx].cpu() | ||
|
|
||
| sequence_tokens = seek_outputs["sequences"] | ||
|
|
||
| if hasattr(seek_outputs, "past_key_values") and seek_outputs.past_key_values is not None: | ||
| if isinstance(seek_outputs["past_key_values"], EncoderDecoderCache): | ||
| seek_outputs.past_key_values = seek_outputs.past_key_values.to_legacy_cache() | ||
|
|
||
| seek_outputs = [ | ||
| {k: split_by_batch_index(v, k, i, is_shortform) for k, v in seek_outputs.items()} | ||
| for i in range(sequence_tokens.shape[0]) | ||
|
|
@@ -1613,11 +1658,14 @@ def _prepare_decoder_input_ids( | |
| one_tensor = torch.ones((cur_bsz, 1), device=device, dtype=torch.long) | ||
| prev_ids = prev_start_of_text * one_tensor[0] if prev_start_of_text is not None else None | ||
|
|
||
| padding = "max_length" if generation_config.cache_implementation == "static" else "longest" | ||
|
|
||
| prev_tokens = _pad_to_max_length( | ||
| active_segments, | ||
| generation_config.pad_token_id, | ||
| device=device, | ||
| padding="left", | ||
| padding_side="left", | ||
| padding=padding, | ||
| bos_token_tensor=prev_ids, | ||
| cut_off_length=cut_off_length, | ||
| ) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.