From 183cd66a451d5e446ae77016c1889742f37cfa6c Mon Sep 17 00:00:00 2001 From: raushan Date: Fri, 14 Jun 2024 09:52:45 +0200 Subject: [PATCH 01/40] draft bart with new cache --- .../generation/candidate_generator.py | 21 +- src/transformers/generation/utils.py | 101 +++++-- src/transformers/models/bart/modeling_bart.py | 284 +++++++++--------- tests/generation/test_utils.py | 53 +++- tests/models/bart/test_modeling_bart.py | 7 +- 5 files changed, 287 insertions(+), 179 deletions(-) diff --git a/src/transformers/generation/candidate_generator.py b/src/transformers/generation/candidate_generator.py index ccbbb412bd1c..c05f9fce5ca5 100644 --- a/src/transformers/generation/candidate_generator.py +++ b/src/transformers/generation/candidate_generator.py @@ -367,16 +367,19 @@ def _crop_past_key_values(model, past_key_values, maximum_length): """Crops the past key values up to a certain maximum length.""" new_past = [] if model.config.is_encoder_decoder: - for idx in range(len(past_key_values)): - new_past.append( - ( - past_key_values[idx][0][:, :, :maximum_length, :], - past_key_values[idx][1][:, :, :maximum_length, :], - past_key_values[idx][2], - past_key_values[idx][3], + if isinstance(past_key_values[0], DynamicCache): + past_key_values[0].crop(maximum_length) + else: + for idx in range(len(past_key_values)): + new_past.append( + ( + past_key_values[idx][0][:, :, :maximum_length, :], + past_key_values[idx][1][:, :, :maximum_length, :], + past_key_values[idx][2], + past_key_values[idx][3], + ) ) - ) - past_key_values = tuple(new_past) + past_key_values = tuple(new_past) # bloom is special elif "bloom" in model.__class__.__name__.lower() or ( model.config.architectures is not None and "bloom" in model.config.architectures[0].lower() diff --git a/src/transformers/generation/utils.py b/src/transformers/generation/utils.py index c68190908925..83c1daa230fb 100644 --- a/src/transformers/generation/utils.py +++ b/src/transformers/generation/utils.py @@ -1390,11 +1390,17 @@ def _get_initial_cache_position(self, input_ids, model_kwargs): return model_kwargs past_length = 0 - if "past_key_values" in model_kwargs: - if isinstance(model_kwargs["past_key_values"], Cache): - past_length = model_kwargs["past_key_values"].get_seq_length() + if model_kwargs.get("past_key_values") is not None: + past_key_values = ( + model_kwargs["past_key_values"] + if not self.config.is_encoder_decoder + else model_kwargs["past_key_values"][0] + ) + if isinstance(past_key_values, Cache): + past_length = past_key_values.get_seq_length() else: - past_length = model_kwargs["past_key_values"][0][0].shape[2] + past_length = past_key_values[0][0].shape[2] + if "inputs_embeds" in model_kwargs: cur_len = model_kwargs["inputs_embeds"].shape[1] else: @@ -1765,15 +1771,31 @@ def generate( ) model_kwargs["past_key_values"] = cache_class(cache_config) + # Use DynamicCache() instance by default. This will avoid back and forth from legacy format that # keeps copying the cache thus using much more memory - elif generation_config.cache_implementation is None and self._supports_default_dynamic_cache(): + # Encoder-decoder models hold a tuple of Cache, for self-attn and cross-attn + elif ( + generation_config.cache_implementation is None + and self._supports_default_dynamic_cache() + and model_kwargs["use_cache"] + ): past = model_kwargs.get("past_key_values", None) if past is None: - model_kwargs["past_key_values"] = DynamicCache() + model_kwargs["past_key_values"] = ( + DynamicCache() if not self.config.is_encoder_decoder else (DynamicCache(), DynamicCache()) + ) use_dynamic_cache_by_default = True - elif isinstance(past, tuple): - model_kwargs["past_key_values"] = DynamicCache.from_legacy_cache(past) + elif isinstance(past, tuple) and not isinstance(past[0], DynamicCache): + if self.config.is_encoder_decoder and not isinstance(past[0], DynamicCache): + self_attn = [cache[:2] for cache in model_kwargs["past_key_values"]] + cross_attn = [cache[2:] for cache in model_kwargs["past_key_values"]] + model_kwargs["past_key_values"] = ( + DynamicCache.from_legacy_cache(self_attn), + DynamicCache.from_legacy_cache(cross_attn), + ) + else: + model_kwargs["past_key_values"] = DynamicCache.from_legacy_cache(past) use_dynamic_cache_by_default = True self._validate_generated_length(generation_config, input_ids_length, has_default_max_length) @@ -2048,6 +2070,13 @@ def typeerror(): if isinstance(result, ModelOutput) and hasattr(result, "past_key_values"): if isinstance(result.past_key_values, DynamicCache): result.past_key_values = result.past_key_values.to_legacy_cache() + elif isinstance(result.past_key_values[0], DynamicCache): + num_hidden_layers = len(result.past_key_values[0].key_cache) + self_attn_cache = result.past_key_values[0].to_legacy_cache() + cross_attn_cache = result.past_key_values[1].to_legacy_cache() + result.past_key_values = tuple( + (*self_attn_cache[i], *cross_attn_cache[i]) for i in range(num_hidden_layers) + ) return result def _has_unfinished_sequences(self, this_peer_finished: bool, synced_gpus: bool, device: torch.device) -> bool: @@ -2215,9 +2244,16 @@ def _contrastive_search( while self._has_unfinished_sequences(this_peer_finished, synced_gpus, device=input_ids.device): # if the first step in the loop, encode all the prefix and obtain: (1) past_key_values; # (2) last_hidden_states; (3) logit_for_next_step; (4) update model kwargs for the next step - if model_kwargs.get("past_key_values") is None or ( - isinstance(model_kwargs["past_key_values"], Cache) - and model_kwargs["past_key_values"].get_seq_length() == 0 + if ( + model_kwargs.get("past_key_values") is None + or ( + isinstance(model_kwargs["past_key_values"], Cache) + and model_kwargs["past_key_values"].get_seq_length() == 0 + ) + or ( + isinstance(model_kwargs["past_key_values"][0], Cache) + and model_kwargs["past_key_values"][0].get_seq_length() == 0 + ) ): # prepare inputs model_kwargs["use_cache"] = True @@ -2260,6 +2296,8 @@ def _contrastive_search( f"{self.__class__.__name__} does not support caching and therefore **can't** be used " "for contrastive search." ) + elif isinstance(past_key_values[0], DynamicCache): + pass # skip if new format cache in enc-dec models elif ( not isinstance(past_key_values[0], (tuple, torch.Tensor)) or past_key_values[0][0].shape[0] != batch_size @@ -2307,6 +2345,9 @@ def _contrastive_search( # If it is a static cache, modify it in-place layer after layer to save memory if isinstance(past, DynamicCache): past.batch_repeat_interleave(top_k) + elif isinstance(past[0], DynamicCache): + past[0].batch_repeat_interleave(top_k) + past[1].batch_repeat_interleave(top_k) else: new_key_values = [] for layer in past: @@ -2337,6 +2378,10 @@ def _contrastive_search( outputs["past_key_values"] = None # Remove last token from past K-V since we don't want to append it at this point model_kwargs["past_key_values"].crop(-1) + elif isinstance(outputs["past_key_values"][0], DynamicCache): + outputs["past_key_values"] = None + model_kwargs["past_key_values"][1].crop(-1) + model_kwargs["past_key_values"][0].crop(-1) all_outputs.append(outputs) outputs = stack_model_outputs(all_outputs) @@ -2409,6 +2454,9 @@ def _contrastive_search( # Do it in-place layer per layer to save memory if isinstance(next_past_key_values, DynamicCache): next_past_key_values.batch_select_indices(augmented_idx) + elif isinstance(next_past_key_values[0], DynamicCache): + next_past_key_values[0].batch_select_indices(augmented_idx) + next_past_key_values[1].batch_select_indices(augmented_idx) else: new_key_values = [] for layer in next_past_key_values: @@ -2482,6 +2530,9 @@ def _contrastive_search( if model_kwargs.get("past_key_values") is not None: if isinstance(model_kwargs["past_key_values"], DynamicCache): model_kwargs["past_key_values"].crop(-1) + elif isinstance(model_kwargs["past_key_values"][0], DynamicCache): + model_kwargs["past_key_values"][1].crop(-1) + model_kwargs["past_key_values"][0].crop(-1) else: past_key_values = [] for layer in model_kwargs["past_key_values"]: @@ -2733,10 +2784,13 @@ def _temporary_reorder_cache(self, past_key_values, beam_idx): for this function, with `Cache.reorder_cache` being the sole remaining code path """ model_class = self.__class__.__name__.lower() - # Exception 1: code path for models using the legacy cache format - if isinstance(past_key_values, (tuple, list)): + # Exception 1: code path for models using tuple of `Cache` (encoder-decoder models) + if isinstance(past_key_values, (tuple)) and isinstance(past_key_values[0], Cache): + past_key_values[0].reorder_cache(beam_idx) + # Exception 2: code path for models using the legacy cache format + elif isinstance(past_key_values, (tuple, list)): past_key_values = self._reorder_cache(past_key_values, beam_idx) - # Exception 2: models with different cache formats. These are limited to `DynamicCache` until their + # Exception 3: models with different cache formats. These are limited to `DynamicCache` until their # cache format is standardized, to avoid adding complexity to the codebase. elif "bloom" in model_class or "gptbigcode" in model_class: if not isinstance(past_key_values, DynamicCache): @@ -3684,8 +3738,13 @@ def _assisted_decoding( model_kwargs = self._get_initial_cache_position(input_ids, model_kwargs) # This is needed if return_dict_in_generate is True - if isinstance(model_kwargs.get("past_key_values", None), DynamicCache): - if len(model_kwargs["past_key_values"]) == 0: + if model_kwargs.get("past_key_values", None) is not None: + if isinstance(model_kwargs["past_key_values"], DynamicCache) and len(model_kwargs["past_key_values"]) == 0: + start_from_empty_dynamic_cache = True + elif ( + isinstance(model_kwargs["past_key_values"][0], DynamicCache) + and len(model_kwargs["past_key_values"][0]) == 0 + ): start_from_empty_dynamic_cache = True else: start_from_empty_dynamic_cache = False @@ -4004,9 +4063,14 @@ def _split(data, full_batch_size: int, split_size: int = None): return [None] * (full_batch_size // split_size) if isinstance(data, torch.Tensor): return [data[i : i + split_size] for i in range(0, full_batch_size, split_size)] - # New cache format + # New cache format (tuple of Cache for encoder-decoder models) elif isinstance(data, DynamicCache): return data.batch_split(full_batch_size, split_size) + elif isinstance(data[0], DynamicCache): + self_attn = data[0].batch_split(full_batch_size, split_size) + cross_attn = data[1].batch_split(full_batch_size, split_size) + split = tuple((self_attn[i], cross_attn[i]) for i in range(0, full_batch_size // split_size)) + return split elif isinstance(data, tuple): # If the elements of the tuple are also tuples (e.g., past_key_values in our earlier example) if isinstance(data[0], tuple): @@ -4113,6 +4177,9 @@ def _concat(data): # New cache format elif isinstance(data[0], DynamicCache): return DynamicCache.from_batch_splits(data) + elif isinstance(data[0][0], DynamicCache): + self_attn, cross_attn = list(zip(*data)) + return (DynamicCache.from_batch_splits(self_attn), DynamicCache.from_batch_splits(cross_attn)) elif isinstance(data[0], tuple): # If the elements of the tuple are also tuples (e.g., past_key_values in our earlier example) if isinstance(data[0][0], tuple): diff --git a/src/transformers/models/bart/modeling_bart.py b/src/transformers/models/bart/modeling_bart.py index e3b2f8a61b28..10cb8cc65630 100755 --- a/src/transformers/models/bart/modeling_bart.py +++ b/src/transformers/models/bart/modeling_bart.py @@ -26,6 +26,7 @@ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACT2FN +from ...cache_utils import Cache, DynamicCache from ...modeling_attn_mask_utils import ( _prepare_4d_attention_mask, _prepare_4d_attention_mask_for_sdpa, @@ -154,6 +155,7 @@ def __init__( is_decoder: bool = False, bias: bool = True, is_causal: bool = False, + layer_idx: int = None, config: Optional[BartConfig] = None, ): super().__init__() @@ -163,6 +165,14 @@ def __init__( self.head_dim = embed_dim // num_heads self.config = config + if layer_idx is None and is_decoder: + logger.warning_once( + f"Instantiating a decoder {self.__class__.__name__} without passing `layer_idx` is not recommended and " + "will to errors during the forward call, if caching is used. Please make sure to provide a `layer_idx` " + "when creating this class." + ) + self.layer_idx = layer_idx + if (self.head_dim * num_heads) != self.embed_dim: raise ValueError( f"embed_dim must be divisible by num_heads (got `embed_dim`: {self.embed_dim}" @@ -184,7 +194,7 @@ def forward( self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[Cache] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, @@ -199,42 +209,27 @@ def forward( # get query proj query_states = self.q_proj(hidden_states) * self.scaling - # get key, value proj - # `past_key_value[0].shape[2] == key_value_states.shape[1]` - # is checking that the `sequence_length` of the `past_key_value` is the same as - # the provided `key_value_states` to support prefix tuning - if ( - is_cross_attention - and past_key_value is not None - and past_key_value[0].shape[2] == key_value_states.shape[1] - ): - # reuse k,v, cross_attentions - key_states = past_key_value[0] - value_states = past_key_value[1] - elif is_cross_attention: - # cross_attentions - key_states = self._shape(self.k_proj(key_value_states), -1, bsz) - value_states = self._shape(self.v_proj(key_value_states), -1, bsz) - elif past_key_value is not None: - # reuse k, v, self_attention - key_states = self._shape(self.k_proj(hidden_states), -1, bsz) - value_states = self._shape(self.v_proj(hidden_states), -1, bsz) - key_states = torch.cat([past_key_value[0], key_states], dim=2) - value_states = torch.cat([past_key_value[1], value_states], dim=2) + + if is_cross_attention: + # check that the `sequence_length` of the `past_key_value` is the same as + # the provided `key_value_states` to support prefix tuning + # try to reuse k,v, cross_attentions. Always layer_idx=0 because there's only + # one encoder_hidden_states for all decoder layers + if past_key_value is None: + key_states = self._shape(self.k_proj(key_value_states), -1, bsz) + value_states = self._shape(self.v_proj(key_value_states), -1, bsz) + elif past_key_value.get_seq_length(self.layer_idx) == key_value_states.shape[1]: + key_states = past_key_value.key_cache[self.layer_idx] + value_states = past_key_value.value_cache[self.layer_idx] + else: + key_states = self._shape(self.k_proj(key_value_states), -1, bsz) + value_states = self._shape(self.v_proj(key_value_states), -1, bsz) + key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx) else: - # self_attention key_states = self._shape(self.k_proj(hidden_states), -1, bsz) value_states = self._shape(self.v_proj(hidden_states), -1, bsz) - - if self.is_decoder: - # if cross_attention save Tuple(torch.Tensor, torch.Tensor) of all cross attention key/value_states. - # Further calls to cross_attention layer can then reuse all cross-attention - # key/value_states (first "if" case) - # if uni-directional self-attention (decoder) save Tuple(torch.Tensor, torch.Tensor) of - # all previous decoder key/value_states. Further calls to uni-directional self-attention - # can concat previous decoder key/value_states to current projected key/value_states (third "elif" case) - # if encoder bi-directional self-attention `past_key_value` is always `None` - past_key_value = (key_states, value_states) + if past_key_value is not None: + key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx) proj_shape = (bsz * self.num_heads, -1, self.head_dim) query_states = self._shape(query_states, tgt_len, bsz).view(*proj_shape) @@ -242,6 +237,7 @@ def forward( value_states = value_states.reshape(*proj_shape) src_len = key_states.size(1) + attn_weights = torch.bmm(query_states, key_states.transpose(1, 2)) if attn_weights.size() != (bsz * self.num_heads, tgt_len, src_len): @@ -317,9 +313,6 @@ def __init__(self, *args, **kwargs): # Beware that with flash_attn<2.1, using q_seqlen != k_seqlen (except for the case q_seqlen == 1) produces a wrong mask (top-left). self._flash_attn_uses_top_left_mask = not is_flash_attn_greater_or_equal_2_10() - def _reshape(self, tensor: torch.Tensor, seq_len: int, bsz: int): - return tensor.view(bsz, seq_len, self.num_heads, self.head_dim) - def forward( self, hidden_states: torch.Tensor, @@ -341,46 +334,30 @@ def forward( # get query proj query_states = self._reshape(self.q_proj(hidden_states), -1, bsz) - # get key, value proj - # `past_key_value[0].shape[2] == key_value_states.shape[1]` - # is checking that the `sequence_length` of the `past_key_value` is the same as - # the provided `key_value_states` to support prefix tuning - if ( - is_cross_attention - and past_key_value is not None - and past_key_value[0].shape[2] == key_value_states.shape[1] - ): - # reuse k,v, cross_attentions - key_states = past_key_value[0].transpose(1, 2) - value_states = past_key_value[1].transpose(1, 2) - elif is_cross_attention: - # cross_attentions - key_states = self._reshape(self.k_proj(key_value_states), -1, bsz) - value_states = self._reshape(self.v_proj(key_value_states), -1, bsz) - elif past_key_value is not None: - # reuse k, v, self_attention - key_states = self._reshape(self.k_proj(hidden_states), -1, bsz) - value_states = self._reshape(self.v_proj(hidden_states), -1, bsz) - key_states = torch.cat([past_key_value[0].transpose(1, 2), key_states], dim=1) - value_states = torch.cat([past_key_value[1].transpose(1, 2), value_states], dim=1) + + if is_cross_attention: + # check that the `sequence_length` of the `past_key_value` is the same as + # the provided `key_value_states` to support prefix tuning + # try to reuse k,v, cross_attentions. Always layer_idx=0 because there's only + # one encoder_hidden_states for all decoder layers + if past_key_value is None: + key_states = self._shape(self.k_proj(key_value_states), -1, bsz) + value_states = self._shape(self.v_proj(key_value_states), -1, bsz) + elif past_key_value.get_seq_length(self.layer_idx) == key_value_states.shape[1]: + key_states = past_key_value.key_cache[self.layer_idx] + value_states = past_key_value.value_cache[self.layer_idx] + else: + key_states = self._shape(self.k_proj(key_value_states), -1, bsz) + value_states = self._shape(self.v_proj(key_value_states), -1, bsz) + key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx) else: - # self_attention - key_states = self._reshape(self.k_proj(hidden_states), -1, bsz) - value_states = self._reshape(self.v_proj(hidden_states), -1, bsz) - - if self.is_decoder: - # if cross_attention save Tuple(torch.Tensor, torch.Tensor) of all cross attention key/value_states. - # Further calls to cross_attention layer can then reuse all cross-attention - # key/value_states (first "if" case) - # if uni-directional self-attention (decoder) save Tuple(torch.Tensor, torch.Tensor) of - # all previous decoder key/value_states. Further calls to uni-directional self-attention - # can concat previous decoder key/value_states to current projected key/value_states (third "elif" case) - # if encoder bi-directional self-attention `past_key_value` is always `None` - past_key_value = (key_states.transpose(1, 2), value_states.transpose(1, 2)) - - kv_seq_len = key_states.shape[-2] - if past_key_value is not None: - kv_seq_len += past_key_value[0].shape[-2] + key_states = self._shape(self.k_proj(hidden_states), -1, bsz) + value_states = self._shape(self.v_proj(hidden_states), -1, bsz) + if past_key_value is not None: + key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx) + + key_states = key_states.transpose(1, 2) + value_states = value_states.transpose(1, 2) # In PEFT, usually we cast the layer norms in float32 for training stability reasons # therefore the input hidden states gets silently casted in float32. Hence, we need @@ -554,42 +531,27 @@ def forward( # get query proj query_states = self.q_proj(hidden_states) - # get key, value proj - # `past_key_value[0].shape[2] == key_value_states.shape[1]` - # is checking that the `sequence_length` of the `past_key_value` is the same as - # the provided `key_value_states` to support prefix tuning - if ( - is_cross_attention - and past_key_value is not None - and past_key_value[0].shape[2] == key_value_states.shape[1] - ): - # reuse k,v, cross_attentions - key_states = past_key_value[0] - value_states = past_key_value[1] - elif is_cross_attention: - # cross_attentions - key_states = self._shape(self.k_proj(key_value_states), -1, bsz) - value_states = self._shape(self.v_proj(key_value_states), -1, bsz) - elif past_key_value is not None: - # reuse k, v, self_attention - key_states = self._shape(self.k_proj(hidden_states), -1, bsz) - value_states = self._shape(self.v_proj(hidden_states), -1, bsz) - key_states = torch.cat([past_key_value[0], key_states], dim=2) - value_states = torch.cat([past_key_value[1], value_states], dim=2) + + if is_cross_attention: + # check that the `sequence_length` of the `past_key_value` is the same as + # the provided `key_value_states` to support prefix tuning + # try to reuse k,v, cross_attentions. Always layer_idx=0 because there's only + # one encoder_hidden_states for all decoder layers + if past_key_value is None: + key_states = self._shape(self.k_proj(key_value_states), -1, bsz) + value_states = self._shape(self.v_proj(key_value_states), -1, bsz) + elif past_key_value.get_seq_length(self.layer_idx) == key_value_states.shape[1]: + key_states = past_key_value.key_cache[self.layer_idx] + value_states = past_key_value.value_cache[self.layer_idx] + else: + key_states = self._shape(self.k_proj(key_value_states), -1, bsz) + value_states = self._shape(self.v_proj(key_value_states), -1, bsz) + key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx) else: - # self_attention key_states = self._shape(self.k_proj(hidden_states), -1, bsz) value_states = self._shape(self.v_proj(hidden_states), -1, bsz) - - if self.is_decoder: - # if cross_attention save Tuple(torch.Tensor, torch.Tensor) of all cross attention key/value_states. - # Further calls to cross_attention layer can then reuse all cross-attention - # key/value_states (first "if" case) - # if uni-directional self-attention (decoder) save Tuple(torch.Tensor, torch.Tensor) of - # all previous decoder key/value_states. Further calls to uni-directional self-attention - # can concat previous decoder key/value_states to current projected key/value_states (third "elif" case) - # if encoder bi-directional self-attention `past_key_value` is always `None` - past_key_value = (key_states, value_states) + if past_key_value is not None: + key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx) query_states = self._shape(query_states, tgt_len, bsz) @@ -643,6 +605,7 @@ def __init__(self, config: BartConfig): num_heads=config.encoder_attention_heads, dropout=config.attention_dropout, config=config, + layer_idx=0, ) self.self_attn_layer_norm = nn.LayerNorm(self.embed_dim) self.dropout = config.dropout @@ -704,9 +667,10 @@ def forward( class BartDecoderLayer(nn.Module): - def __init__(self, config: BartConfig): + def __init__(self, config: BartConfig, layer_idx: int = None): super().__init__() self.embed_dim = config.d_model + self.config = config self.self_attn = BART_ATTENTION_CLASSES[config._attn_implementation]( embed_dim=self.embed_dim, @@ -714,6 +678,7 @@ def __init__(self, config: BartConfig): dropout=config.attention_dropout, is_decoder=True, is_causal=True, + layer_idx=layer_idx, config=config, ) self.dropout = config.dropout @@ -726,6 +691,7 @@ def __init__(self, config: BartConfig): config.decoder_attention_heads, dropout=config.attention_dropout, is_decoder=True, + layer_idx=layer_idx, config=config, ) self.encoder_attn_layer_norm = nn.LayerNorm(self.embed_dim) @@ -765,11 +731,14 @@ def forward( """ residual = hidden_states - # Self Attention - # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 - self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None - # add present self-attn cache to positions 1,2 of present_key_value tuple - hidden_states, self_attn_weights, present_key_value = self.self_attn( + # decoder self-attention Cache object is at position 0 in Encoder-Decoder setting, otherwise it's simply one Cache object + # cross_attn Cache object is at positions 1 of past_key_value tuple + self_attn_past_key_value = cross_attn_past_key_value = None + if past_key_value is not None: + self_attn_past_key_value = past_key_value[0] if self.config.is_encoder_decoder else past_key_value + cross_attn_past_key_value = past_key_value[1] if self.config.is_encoder_decoder else None + + hidden_states, self_attn_weights, past_key_values = self.self_attn( hidden_states=hidden_states, past_key_value=self_attn_past_key_value, attention_mask=attention_mask, @@ -781,14 +750,11 @@ def forward( hidden_states = self.self_attn_layer_norm(hidden_states) # Cross-Attention Block - cross_attn_present_key_value = None cross_attn_weights = None if encoder_hidden_states is not None: residual = hidden_states - # cross_attn cached key/values tuple is at positions 3,4 of present_key_value tuple - cross_attn_past_key_value = past_key_value[-2:] if past_key_value is not None else None - hidden_states, cross_attn_weights, cross_attn_present_key_value = self.encoder_attn( + hidden_states, cross_attn_weights, cross_attn_past_key_value = self.encoder_attn( hidden_states=hidden_states, key_value_states=encoder_hidden_states, attention_mask=encoder_attention_mask, @@ -800,8 +766,9 @@ def forward( hidden_states = residual + hidden_states hidden_states = self.encoder_attn_layer_norm(hidden_states) - # add cross-attn to positions 3,4 of present_key_value tuple - present_key_value = present_key_value + cross_attn_present_key_value + # add self-attn and cross-attn in a past_key_values tuple, otherwise set to None (not tuple of Nones) + if past_key_values is not None and cross_attn_past_key_value is not None: + past_key_values = (past_key_values, cross_attn_past_key_value) # Fully Connected residual = hidden_states @@ -818,7 +785,7 @@ def forward( outputs += (self_attn_weights, cross_attn_weights) if use_cache: - outputs += (present_key_value,) + outputs += (past_key_values,) return outputs @@ -856,6 +823,7 @@ class BartPreTrainedModel(PreTrainedModel): _skip_keys_device_placement = "past_key_values" _supports_flash_attn_2 = True _supports_sdpa = True + _supports_cache_class = True def _init_weights(self, module): std = self.config.init_std @@ -1268,7 +1236,7 @@ def __init__(self, config: BartConfig, embed_tokens: Optional[nn.Embedding] = No config.max_position_embeddings, config.d_model, ) - self.layers = nn.ModuleList([BartDecoderLayer(config) for _ in range(config.decoder_layers)]) + self.layers = nn.ModuleList([BartDecoderLayer(config, i) for i in range(config.decoder_layers)]) self._use_flash_attention_2 = config._attn_implementation == "flash_attention_2" self._use_sdpa = config._attn_implementation == "sdpa" @@ -1384,8 +1352,31 @@ def forward( else: raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds") - # past_key_values_length - past_key_values_length = past_key_values[0][0].shape[2] if past_key_values is not None else 0 + # past_key_values in encpder-decoder models is a tuple of cache objects + # first element holds past_kv for decoder attn, and the second for cross attn + past_key_values_length = 0 + if use_cache: + use_legacy_cache = ( + not (past_key_values is not None and isinstance(past_key_values[0], Cache)) + if self.config.is_encoder_decoder + else not isinstance(past_key_values, Cache) + ) + if self.config.is_encoder_decoder and use_legacy_cache: + self_attn = cross_attn = None + if past_key_values is not None: + self_attn = [cache[:2] for cache in past_key_values] + cross_attn = [cache[2:] for cache in past_key_values] + past_key_values = ( + DynamicCache.from_legacy_cache(self_attn), + DynamicCache.from_legacy_cache(cross_attn), + ) + elif use_legacy_cache: + past_key_values = DynamicCache.from_legacy_cache(past_key_values) + past_key_values_length = ( + past_key_values.get_seq_length() + if not self.config.is_encoder_decoder + else past_key_values[0].get_seq_length() + ) if inputs_embeds is None: inputs_embeds = self.embed_tokens(input) @@ -1447,7 +1438,7 @@ def forward( all_hidden_states = () if output_hidden_states else None all_self_attns = () if output_attentions else None all_cross_attentions = () if (output_attentions and encoder_hidden_states is not None) else None - next_decoder_cache = () if use_cache else None + next_decoder_cache = None # check if head_mask/cross_attn_head_mask has a correct number of layers specified if desired for attn_mask, mask_name in zip([head_mask, cross_attn_head_mask], ["head_mask", "cross_attn_head_mask"]): @@ -1467,8 +1458,6 @@ def forward( if dropout_probability < self.layerdrop: continue - past_key_value = past_key_values[idx] if past_key_values is not None else None - if self.gradient_checkpointing and self.training: layer_outputs = self._gradient_checkpointing_func( decoder_layer.__call__, @@ -1492,14 +1481,14 @@ def forward( cross_attn_layer_head_mask=( cross_attn_head_mask[idx] if cross_attn_head_mask is not None else None ), - past_key_value=past_key_value, + past_key_value=past_key_values, output_attentions=output_attentions, use_cache=use_cache, ) hidden_states = layer_outputs[0] if use_cache: - next_decoder_cache += (layer_outputs[3 if output_attentions else 1],) + next_decoder_cache = layer_outputs[3 if output_attentions else 1] if output_attentions: all_self_attns += (layer_outputs[1],) @@ -1511,7 +1500,19 @@ def forward( if output_hidden_states: all_hidden_states += (hidden_states,) - next_cache = next_decoder_cache if use_cache else None + next_cache = None + if use_cache: + if not use_legacy_cache: + next_cache = next_decoder_cache + else: + if self.config.is_encoder_decoder: + num_hidden_layers = len(next_decoder_cache[0].key_cache) + self_attn_cache = next_decoder_cache[0].to_legacy_cache() + cross_attn_cache = next_decoder_cache[1].to_legacy_cache() + next_cache = tuple((*self_attn_cache[i], *cross_attn_cache[i]) for i in range(num_hidden_layers)) + else: + next_cache = next_decoder_cache.to_legacy_cache() + if not return_dict: return tuple( v @@ -1800,9 +1801,13 @@ def prepare_inputs_for_generation( encoder_outputs=None, **kwargs, ): - # cut decoder_input_ids if past_key_values is used if past_key_values is not None: - past_length = past_key_values[0][0].shape[2] + if isinstance(past_key_values, Cache): # BART as standalone decoder + past_length = past_key_values.get_seq_length() + elif isinstance(past_key_values[0], Cache): # BART in enc-dec setting + past_length = past_key_values[0].get_seq_length() + else: # BART with legacy cache format + past_length = past_key_values[0][0].shape[2] # Some generation methods already pass only the last input ID if decoder_input_ids.shape[1] > past_length: @@ -1833,10 +1838,8 @@ def prepare_decoder_input_ids_from_labels(self, labels: torch.Tensor): def _reorder_cache(past_key_values, beam_idx): reordered_past = () for layer_past in past_key_values: - # cached cross_attention states don't have to be reordered -> they are always the same reordered_past += ( - tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past[:2]) - + layer_past[2:], + tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past), ) return reordered_past @@ -2301,8 +2304,13 @@ def prepare_inputs_for_generation( if attention_mask is None: attention_mask = input_ids.new_ones(input_ids.shape) - if past_key_values: - past_length = past_key_values[0][0].shape[2] + if past_key_values is not None: + if isinstance(past_key_values, Cache): # BART as standalone decoder + past_length = past_key_values.get_seq_length() + elif isinstance(past_key_values[0], Cache): # BART in enc-dec setting + past_length = past_key_values[0].get_seq_length() + else: # BART with legacy cache format + past_length = past_key_values[0][0].shape[2] # Some generation methods already pass only the last input ID if input_ids.shape[1] > past_length: diff --git a/tests/generation/test_utils.py b/tests/generation/test_utils.py index 1981f5a63919..3b9584f0c198 100644 --- a/tests/generation/test_utils.py +++ b/tests/generation/test_utils.py @@ -1101,7 +1101,7 @@ def test_beam_search_low_memory(self): self.assertListEqual(low_output.tolist(), high_output.tolist()) @parameterized.expand([("random",), ("same",)]) - @is_flaky() # Read NOTE (1) below. If there are API issues, all attempts will fail. + # @is_flaky() # Read NOTE (1) below. If there are API issues, all attempts will fail. def test_assisted_decoding_matches_greedy_search(self, assistant_type): # This test ensures that the assisted generation does not introduce output changes over greedy search. # NOTE (1): The sentence above is true most of the time, there is a tiny difference in the logits due to matmul @@ -1623,19 +1623,31 @@ def test_new_cache_format(self, num_beams, do_sample): set_seed(seed) legacy_results = model.generate(input_ids, attention_mask=attention_mask, **generation_kwargs) set_seed(seed) + past_key_values = (DynamicCache(), DynamicCache()) if model.config.is_encoder_decoder else DynamicCache() new_results = model.generate( - input_ids, attention_mask=attention_mask, past_key_values=DynamicCache(), **generation_kwargs + input_ids, attention_mask=attention_mask, past_key_values=past_key_values, **generation_kwargs ) # The two sets of generated sequences must match, despite the cache format between forward passes being # different self.assertListEqual(legacy_results.sequences.tolist(), new_results.sequences.tolist()) self.assertTrue(isinstance(legacy_results.past_key_values, tuple)) - self.assertTrue(isinstance(new_results.past_key_values, DynamicCache)) + if not model.config.is_encoder_decoder: + self.assertTrue(isinstance(new_results.past_key_values, DynamicCache)) + else: + self.assertTrue(isinstance(new_results.past_key_values[0], DynamicCache)) # The contents of the two caches, when converted to the same format (in both directions!), must match legacy_cache = legacy_results.past_key_values - new_cache_converted = new_results.past_key_values.to_legacy_cache() + if model.config.is_encoder_decoder: + num_hidden_layers = len(new_results.past_key_values[0].key_cache) + self_attn_cache = new_results.past_key_values[0].to_legacy_cache() + cross_attn_cache = new_results.past_key_values[1].to_legacy_cache() + new_cache_converted = tuple( + (*self_attn_cache[i], *cross_attn_cache[i]) for i in range(num_hidden_layers) + ) + else: + new_cache_converted = new_results.past_key_values.to_legacy_cache() for layer_idx in range(len(legacy_cache)): for kv_idx in range(len(legacy_cache[layer_idx])): self.assertTrue( @@ -1646,15 +1658,32 @@ def test_new_cache_format(self, num_beams, do_sample): ) new_cache = new_results.past_key_values - legacy_cache_converted = DynamicCache.from_legacy_cache(legacy_results.past_key_values) - for layer_idx in range(len(new_cache)): - for kv_idx in range(len(new_cache[layer_idx])): - self.assertTrue( - torch.allclose( - new_cache[layer_idx][kv_idx], - legacy_cache_converted[layer_idx][kv_idx], + if model.config.is_encoder_decoder: + self_attn = [cache[:2] for cache in legacy_results.past_key_values] + cross_attn = [cache[2:] for cache in legacy_results.past_key_values] + legacy_cache_converted = ( + DynamicCache.from_legacy_cache(self_attn), + DynamicCache.from_legacy_cache(cross_attn), + ) + for cache_obj in range(len(new_cache)): + for layer_idx in range(len(new_cache[cache_obj])): + for kv_idx in range(len(new_cache[cache_obj][layer_idx])): + self.assertTrue( + torch.allclose( + new_cache[cache_obj][layer_idx][kv_idx], + legacy_cache_converted[cache_obj][layer_idx][kv_idx], + ) + ) + else: + legacy_cache_converted = DynamicCache.from_legacy_cache(legacy_results.past_key_values) + for layer_idx in range(len(new_cache)): + for kv_idx in range(len(new_cache[layer_idx])): + self.assertTrue( + torch.allclose( + new_cache[layer_idx][kv_idx], + legacy_cache_converted[layer_idx][kv_idx], + ) ) - ) @require_quanto def test_generate_with_quant_cache(self): diff --git a/tests/models/bart/test_modeling_bart.py b/tests/models/bart/test_modeling_bart.py index ba9e112c186e..342260872d82 100644 --- a/tests/models/bart/test_modeling_bart.py +++ b/tests/models/bart/test_modeling_bart.py @@ -174,6 +174,7 @@ def prepare_config_and_inputs_for_common(self): def create_and_check_decoder_model_past_large_inputs(self, config, inputs_dict): model = BartModel(config=config).get_decoder().to(torch_device).eval() + model.config.is_encoder_decoder = False input_ids = inputs_dict["input_ids"] attention_mask = inputs_dict["attention_mask"] head_mask = inputs_dict["head_mask"] @@ -1461,9 +1462,9 @@ def create_and_check_decoder_model_attention_mask_past( # get two different outputs output_from_no_past = model(next_input_ids, attention_mask=attn_mask)["last_hidden_state"] - output_from_past = model(next_tokens, attention_mask=attn_mask, past_key_values=past_key_values)[ - "last_hidden_state" - ] + output_from_past = model( + next_tokens, attention_mask=attn_mask, past_key_values=past_key_values, use_cache=True + )["last_hidden_state"] # select random slice random_slice_idx = ids_tensor((1,), output_from_past.shape[-1]).item() From 4578bca4db79da9ea628bcf70956dc61a851ce5b Mon Sep 17 00:00:00 2001 From: raushan Date: Fri, 14 Jun 2024 15:09:36 +0200 Subject: [PATCH 02/40] add cache for decoder-only models --- .../generation/candidate_generator.py | 19 +-- .../models/bloom/modeling_bloom.py | 144 +++++++++++------- .../models/codegen/modeling_codegen.py | 75 +++++---- .../models/falcon/modeling_falcon.py | 113 ++++++++------ src/transformers/models/git/modeling_git.py | 96 ++++++------ .../models/gpt_neo/modeling_gpt_neo.py | 100 ++++++------ .../models/gpt_neox/modeling_gpt_neox.py | 108 ++++++++----- src/transformers/models/gptj/modeling_gptj.py | 101 ++++++------ src/transformers/models/opt/modeling_opt.py | 17 ++- 9 files changed, 448 insertions(+), 325 deletions(-) diff --git a/src/transformers/generation/candidate_generator.py b/src/transformers/generation/candidate_generator.py index c05f9fce5ca5..3fc4add67c7b 100644 --- a/src/transformers/generation/candidate_generator.py +++ b/src/transformers/generation/candidate_generator.py @@ -380,19 +380,7 @@ def _crop_past_key_values(model, past_key_values, maximum_length): ) ) past_key_values = tuple(new_past) - # bloom is special - elif "bloom" in model.__class__.__name__.lower() or ( - model.config.architectures is not None and "bloom" in model.config.architectures[0].lower() - ): - for idx in range(len(past_key_values)): - new_past.append( - ( - past_key_values[idx][0][:, :, :maximum_length], - past_key_values[idx][1][:, :maximum_length, :], - ) - ) - past_key_values = tuple(new_past) - # gptbigcode is too + # gptbigcode is special elif "gptbigcode" in model.__class__.__name__.lower() or ( model.config.architectures is not None and "gptbigcode" in model.config.architectures[0].lower() ): @@ -404,13 +392,12 @@ def _crop_past_key_values(model, past_key_values, maximum_length): past_key_values[idx] = past_key_values[idx][:, :, :maximum_length, :] elif isinstance(past_key_values, DynamicCache): past_key_values.crop(maximum_length) - elif past_key_values is not None: for idx in range(len(past_key_values)): new_past.append( ( - past_key_values[idx][0][:, :, :maximum_length, :], - past_key_values[idx][1][:, :, :maximum_length, :], + past_key_values[idx][0][..., :maximum_length, :], + past_key_values[idx][1][..., :maximum_length, :], ) ) past_key_values = tuple(new_past) diff --git a/src/transformers/models/bloom/modeling_bloom.py b/src/transformers/models/bloom/modeling_bloom.py index 0ef158b1f85f..1fd3a7da0b72 100644 --- a/src/transformers/models/bloom/modeling_bloom.py +++ b/src/transformers/models/bloom/modeling_bloom.py @@ -25,6 +25,7 @@ from torch.nn import functional as F from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward +from ...cache_utils import Cache, DynamicCache from ...modeling_attn_mask_utils import _prepare_4d_causal_attention_mask from ...modeling_outputs import ( BaseModelOutputWithPastAndCrossAttentions, @@ -170,7 +171,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: class BloomAttention(nn.Module): - def __init__(self, config: BloomConfig): + def __init__(self, config: BloomConfig, layer_idx=None): super().__init__() self.pretraining_tp = config.pretraining_tp @@ -191,6 +192,13 @@ def __init__(self, config: BloomConfig): # Layer-wise attention scaling self.inv_norm_factor = 1.0 / math.sqrt(self.head_dim) self.beta = 1.0 + self.layer_idx = layer_idx + if layer_idx is None: + logger.warning_once( + f"Instantiating {self.__class__.__name__} without passing a `layer_idx` is not recommended and will " + "lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` " + "when creating this class." + ) self.query_key_value = nn.Linear(self.hidden_size, 3 * self.hidden_size, bias=True) self.dense = nn.Linear(self.hidden_size, self.hidden_size) @@ -256,23 +264,16 @@ def forward( batch_size, q_length, _, _ = query_layer.shape query_layer = query_layer.transpose(1, 2).reshape(batch_size * self.num_heads, q_length, self.head_dim) - key_layer = key_layer.permute(0, 2, 3, 1).reshape(batch_size * self.num_heads, self.head_dim, q_length) + key_layer = key_layer.transpose(1, 2).reshape(batch_size * self.num_heads, q_length, self.head_dim) value_layer = value_layer.transpose(1, 2).reshape(batch_size * self.num_heads, q_length, self.head_dim) + if layer_past is not None: - past_key, past_value = layer_past - # concatenate along seq_length dimension: - # - key: [batch_size * self.num_heads, head_dim, kv_length] - # - value: [batch_size * self.num_heads, kv_length, head_dim] - key_layer = torch.cat((past_key, key_layer), dim=2) - value_layer = torch.cat((past_value, value_layer), dim=1) + key_layer, value_layer = layer_past.update(key_layer, value_layer, self.layer_idx) + # transpose to get key: [batch_size * self.num_heads, head_dim, kv_length] + key_layer = key_layer.transpose(1, 2) _, _, kv_length = key_layer.shape - if use_cache is True: - present = (key_layer, value_layer) - else: - present = None - # [batch_size * num_heads, q_length, kv_length] # we use `torch.Tensor.baddbmm` instead of `torch.baddbmm` as the latter isn't supported by TorchScript v1.11 matmul_result = alibi.baddbmm( @@ -322,7 +323,7 @@ def forward( output_tensor = dropout_add(output_tensor, residual, self.hidden_dropout, self.training) - outputs = (output_tensor, present) + outputs = (output_tensor, layer_past) if output_attentions: outputs += (attention_probs,) @@ -361,13 +362,13 @@ def forward(self, hidden_states: torch.Tensor, residual: torch.Tensor) -> torch. class BloomBlock(nn.Module): - def __init__(self, config: BloomConfig): + def __init__(self, config: BloomConfig, layer_idx=None): super().__init__() hidden_size = config.hidden_size self.input_layernorm = LayerNorm(hidden_size, eps=config.layer_norm_epsilon) self.num_heads = config.n_head - self.self_attention = BloomAttention(config) + self.self_attention = BloomAttention(config, layer_idx) self.post_attention_layernorm = LayerNorm(hidden_size, eps=config.layer_norm_epsilon) self.mlp = BloomMLP(config) @@ -428,7 +429,7 @@ def forward( else: outputs = (output,) + outputs[1:] - return outputs # hidden_states, present, attentions + return outputs # hidden_states, past_kv, attentions class BloomPreTrainedModel(PreTrainedModel): @@ -437,6 +438,7 @@ class BloomPreTrainedModel(PreTrainedModel): supports_gradient_checkpointing = True _no_split_modules = ["BloomBlock"] _skip_keys_device_placement = "past_key_values" + _supports_cache_class = True def __init__(self, *inputs, **kwargs): super().__init__(*inputs, **kwargs) @@ -465,17 +467,23 @@ def _convert_to_standard_cache( Standardizes the format of the cache so as to match most implementations, i.e. to tuple(tuple([batch_size, num_heads, ...])) """ - batch_size_times_num_heads, head_dim, seq_length = past_key_value[0][0].shape + use_legacy_cache = not isinstance(past_key_value, Cache) + batch_size_times_num_heads, seq_length, head_dim = past_key_value[0][0].shape num_heads = batch_size_times_num_heads // batch_size - # key: [batch_size * num_heads, head_dim, seq_length] -> [batch_size, num_heads, head_dim, seq_length] - # value: [batch_size * num_heads, seq_length, head_dim] -> [batch_size, num_heads, seq_length, head_dim] - return tuple( + if use_legacy_cache: + past_key_value = tuple( ( - layer_past[0].view(batch_size, num_heads, head_dim, seq_length), + layer_past[0].view(batch_size, num_heads, seq_length, head_dim), layer_past[1].view(batch_size, num_heads, seq_length, head_dim), ) for layer_past in past_key_value ) + else: + # in-place in case of DynamicCache, because we don't assume cache will become a new object after `prepare_input_for_generation` + for layer_idx in range(len(past_key_value)): + past_key_value.key_cache[layer_idx] = past_key_value.key_cache[layer_idx].view(batch_size, num_heads, seq_length, head_dim) + past_key_value.value_cache[layer_idx] = past_key_value.value_cache[layer_idx].view(batch_size, num_heads, seq_length, head_dim) + return past_key_value @staticmethod def _convert_to_bloom_cache( @@ -484,17 +492,23 @@ def _convert_to_bloom_cache( """ Converts the cache to the format expected by Bloom, i.e. to tuple(tuple([batch_size * num_heads, ...])) """ - batch_size, num_heads, head_dim, seq_length = past_key_value[0][0].shape + use_legacy_cache = not isinstance(past_key_value, Cache) + batch_size, num_heads, seq_length, head_dim = past_key_value[0][0].shape batch_size_times_num_heads = batch_size * num_heads - # key: [batch_size, num_heads, head_dim, seq_length] -> [batch_size * num_heads, head_dim, seq_length] - # value: [batch_size, num_heads, seq_length, head_dim] -> [batch_size * num_heads, seq_length, head_dim] - return tuple( - ( - layer_past[0].view(batch_size_times_num_heads, head_dim, seq_length), - layer_past[1].view(batch_size_times_num_heads, seq_length, head_dim), + if use_legacy_cache: + past_key_value = tuple( + ( + layer_past[0].view(batch_size_times_num_heads, seq_length, head_dim), + layer_past[1].view(batch_size_times_num_heads, seq_length, head_dim), + ) + for layer_past in past_key_value ) - for layer_past in past_key_value - ) + else: + # in-place in case of DynamicCache, because we don't assume cache will become a new object after `prepare_input_for_generation` + for layer_idx in range(len(past_key_value)): + past_key_value.key_cache[layer_idx] = past_key_value.key_cache[layer_idx].view(batch_size_times_num_heads, seq_length, head_dim) + past_key_value.value_cache[layer_idx] = past_key_value.value_cache[layer_idx].view(batch_size_times_num_heads, seq_length, head_dim) + return past_key_value BLOOM_START_DOCSTRING = r""" @@ -583,7 +597,7 @@ def __init__(self, config: BloomConfig): self.word_embeddings_layernorm = LayerNorm(self.embed_dim, eps=config.layer_norm_epsilon) # Transformer blocks - self.h = nn.ModuleList([BloomBlock(config) for _ in range(config.num_hidden_layers)]) + self.h = nn.ModuleList([BloomBlock(config, layer_idx=i) for i in range(config.num_hidden_layers)]) # Final Layer Norm self.ln_f = LayerNorm(self.embed_dim, eps=config.layer_norm_epsilon) @@ -647,8 +661,19 @@ def forward( else: raise ValueError("You have to specify either input_ids or inputs_embeds") - if past_key_values is None: - past_key_values = tuple([None] * len(self.h)) + if self.gradient_checkpointing and self.training: + if use_cache: + logger.warning_once( + "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." + ) + use_cache = False + + past_length = 0 + if use_cache: + use_legacy_cache = not isinstance(past_key_values, Cache) + if use_legacy_cache: + past_key_values = DynamicCache.from_legacy_cache(past_key_values) + past_length = past_key_values.get_seq_length() # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head @@ -661,23 +686,12 @@ def forward( hidden_states = self.word_embeddings_layernorm(inputs_embeds) - presents = () if use_cache else None + next_decoder_cache = None all_self_attentions = () if output_attentions else None all_hidden_states = () if output_hidden_states else None - if self.gradient_checkpointing and self.training: - if use_cache: - logger.warning_once( - "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." - ) - use_cache = False - # Compute alibi tensor: check build_alibi_tensor documentation - seq_length_with_past = seq_length - past_key_values_length = 0 - if past_key_values[0] is not None: - past_key_values_length = past_key_values[0][0].shape[2] - seq_length_with_past = seq_length_with_past + past_key_values_length + seq_length_with_past = seq_length + past_length if attention_mask is None: attention_mask = torch.ones((batch_size, seq_length_with_past), device=hidden_states.device) else: @@ -689,11 +703,11 @@ def forward( attention_mask, input_shape=(batch_size, seq_length), inputs_embeds=inputs_embeds, - past_key_values_length=past_key_values_length, + past_key_values_length=past_length, ) causal_mask = causal_mask.bool() - for i, (block, layer_past) in enumerate(zip(self.h, past_key_values)): + for i, block in enumerate(self.h): if output_hidden_states: all_hidden_states = all_hidden_states + (hidden_states,) @@ -703,7 +717,7 @@ def forward( hidden_states, alibi, causal_mask, - layer_past, + past_key_values, head_mask[i], use_cache, output_attentions, @@ -711,7 +725,7 @@ def forward( else: outputs = block( hidden_states, - layer_past=layer_past, + layer_past=past_key_values, attention_mask=causal_mask, head_mask=head_mask[i], use_cache=use_cache, @@ -720,8 +734,8 @@ def forward( ) hidden_states = outputs[0] - if use_cache is True: - presents = presents + (outputs[1],) + if use_cache: + next_decoder_cache = outputs[1] if output_attentions: all_self_attentions = all_self_attentions + (outputs[2 if use_cache else 1],) @@ -732,12 +746,16 @@ def forward( if output_hidden_states: all_hidden_states = all_hidden_states + (hidden_states,) + next_cache = None + if use_cache: + next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache + if not return_dict: - return tuple(v for v in [hidden_states, presents, all_hidden_states, all_self_attentions] if v is not None) + return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attentions] if v is not None) return BaseModelOutputWithPastAndCrossAttentions( last_hidden_state=hidden_states, - past_key_values=presents, + past_key_values=next_cache, hidden_states=all_hidden_states, attentions=all_self_attentions, ) @@ -776,8 +794,10 @@ def prepare_inputs_for_generation( **kwargs, ) -> dict: # only last tokens for input_ids if past is not None + past_length = 0 if past_key_values is not None: - past_length = past_key_values[0][0].shape[2] + past_length = cache_length = past_key_values.get_seq_length() + max_cache_length = past_key_values.get_max_length() # Some generation methods already pass only the last input ID if input_ids.shape[1] > past_length: @@ -788,12 +808,20 @@ def prepare_inputs_for_generation( input_ids = input_ids[:, remove_prefix_length:] + # If we are about to go beyond the maximum cache length, we need to crop the input attention mask. + if ( + max_cache_length is not None + and attention_mask is not None + and cache_length + input_ids.shape[1] > max_cache_length + ): + attention_mask = attention_mask[:, -max_cache_length:] + # the cache may be in the stardard format (e.g. in contrastive search), convert to bloom's format if needed - if past_key_values[0][0].shape[0] == input_ids.shape[0]: + if past_length != 0 and past_key_values[0][0].shape[0] == input_ids.shape[0]: past_key_values = self._convert_to_bloom_cache(past_key_values) # if `inputs_embeds` are passed, we only want to use them in the 1st generation step - if inputs_embeds is not None and past_key_values is None: + if inputs_embeds is not None and past_length == 0: model_inputs = {"inputs_embeds": inputs_embeds} else: model_inputs = {"input_ids": input_ids} diff --git a/src/transformers/models/codegen/modeling_codegen.py b/src/transformers/models/codegen/modeling_codegen.py index a8df9ed7f3fb..2a58c2873656 100644 --- a/src/transformers/models/codegen/modeling_codegen.py +++ b/src/transformers/models/codegen/modeling_codegen.py @@ -22,6 +22,7 @@ from torch.nn import CrossEntropyLoss from ...activations import ACT2FN +from ...cache_utils import Cache, DynamicCache from ...modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging @@ -57,7 +58,7 @@ def apply_rotary_pos_emb(tensor: torch.Tensor, sin: torch.Tensor, cos: torch.Ten class CodeGenAttention(nn.Module): - def __init__(self, config): + def __init__(self, config, layer_idx=None): super().__init__() max_positions = config.max_position_embeddings @@ -71,6 +72,13 @@ def __init__(self, config): self.attn_dropout = nn.Dropout(config.attn_pdrop) self.resid_dropout = nn.Dropout(config.resid_pdrop) + self.layer_idx = layer_idx + if layer_idx is None: + logger.warning_once( + f"Instantiating {self.__class__.__name__} without passing a `layer_idx` is not recommended and will " + "lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` " + "when creating this class." + ) self.embed_dim = config.hidden_size self.num_attention_heads = config.num_attention_heads @@ -200,18 +208,11 @@ def forward( key = key.permute(0, 2, 1, 3) query = query.permute(0, 2, 1, 3) + # Note that this cast is quite ugly, but is not implemented before ROPE as k_rot in the original codebase is always in fp32. + # Reference: https://github.com/salesforce/CodeGen/blob/f210c3bb1216c975ad858cd4132c0fdeabf4bfc2/codegen1/jaxformer/hf/codegen/modeling_codegen.py#L38 if layer_past is not None: - past_key = layer_past[0] - past_value = layer_past[1] - key = torch.cat((past_key, key), dim=-2) - value = torch.cat((past_value, value), dim=-2) - - if use_cache is True: - # Note that this cast is quite ugly, but is not implemented before ROPE as k_rot in the original codebase is always in fp32. - # Reference: https://github.com/salesforce/CodeGen/blob/f210c3bb1216c975ad858cd4132c0fdeabf4bfc2/codegen1/jaxformer/hf/codegen/modeling_codegen.py#L38 - present = (key.to(hidden_states.dtype), value) - else: - present = None + cache_kwargs = {"sin": sin, "cos": cos, "partial_rotation_size": self.rotary_dim} + key, value = layer_past.update(key.to(hidden_states.dtype), value, self.layer_idx, cache_kwargs) # compute self-attention: V x Softmax(QK^T) attn_output, attn_weights = self._attn(query, key, value, attention_mask, head_mask) @@ -220,7 +221,7 @@ def forward( attn_output = self.out_proj(attn_output) attn_output = self.resid_dropout(attn_output) - outputs = (attn_output, present) + outputs = (attn_output, layer_past) if output_attentions: outputs += (attn_weights,) @@ -250,11 +251,11 @@ def forward(self, hidden_states: Optional[torch.FloatTensor]) -> torch.FloatTens # Copied from transformers.models.gptj.modeling_gptj.GPTJBlock with GPTJ->CodeGen class CodeGenBlock(nn.Module): # Ignore copy - def __init__(self, config): + def __init__(self, config, layer_idx=None): super().__init__() inner_dim = config.n_inner if config.n_inner is not None else 4 * config.n_embd self.ln_1 = nn.LayerNorm(config.n_embd, eps=config.layer_norm_epsilon) - self.attn = CodeGenAttention(config) + self.attn = CodeGenAttention(config, layer_idx) self.mlp = CodeGenMLP(inner_dim, config) def forward( @@ -303,6 +304,7 @@ class CodeGenPreTrainedModel(PreTrainedModel): supports_gradient_checkpointing = True _no_split_modules = ["CodeGenBlock"] _skip_keys_device_placement = "past_key_values" + _supports_cache_class = True def __init__(self, *inputs, **kwargs): super().__init__(*inputs, **kwargs) @@ -397,7 +399,7 @@ def __init__(self, config): self.vocab_size = config.vocab_size self.wte = nn.Embedding(config.vocab_size, self.embed_dim) self.drop = nn.Dropout(config.embd_pdrop) - self.h = nn.ModuleList([CodeGenBlock(config) for _ in range(config.n_layer)]) + self.h = nn.ModuleList([CodeGenBlock(config, layer_idx=i) for i in range(config.n_layer)]) self.ln_f = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_epsilon) self.rotary_dim = min(config.rotary_dim, config.n_ctx // config.num_attention_heads) @@ -457,11 +459,12 @@ def forward( if token_type_ids is not None: token_type_ids = token_type_ids.view(-1, input_shape[-1]) - if past_key_values is None: - past_length = 0 - past_key_values = tuple([None] * len(self.h)) - else: - past_length = past_key_values[0][0].size(-2) + past_length = 0 + if use_cache: + use_legacy_cache = not isinstance(past_key_values, Cache) + if use_legacy_cache: + past_key_values = DynamicCache.from_legacy_cache(past_key_values) + past_length = past_key_values.get_seq_length() if position_ids is None: position_ids = torch.arange(past_length, input_shape[-1] + past_length, dtype=torch.long, device=device) @@ -514,10 +517,10 @@ def forward( ) use_cache = False - presents = () if use_cache else None + next_decoder_cache = None all_self_attentions = () if output_attentions else None all_hidden_states = () if output_hidden_states else None - for i, (block, layer_past) in enumerate(zip(self.h, past_key_values)): + for i, block in enumerate(self.h): if output_hidden_states: all_hidden_states = all_hidden_states + (hidden_states,) @@ -535,7 +538,7 @@ def forward( else: outputs = block( hidden_states=hidden_states, - layer_past=layer_past, + layer_past=past_key_values, attention_mask=attention_mask, position_ids=position_ids, head_mask=head_mask[i], @@ -545,7 +548,7 @@ def forward( hidden_states = outputs[0] if use_cache is True: - presents = presents + (outputs[1],) + next_decoder_cache = outputs[1] if output_attentions: all_self_attentions = all_self_attentions + (outputs[2 if use_cache else 1],) @@ -557,12 +560,16 @@ def forward( if output_hidden_states: all_hidden_states = all_hidden_states + (hidden_states,) + next_cache = None + if use_cache: + next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache + if not return_dict: - return tuple(v for v in [hidden_states, presents, all_hidden_states, all_self_attentions] if v is not None) + return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attentions] if v is not None) return BaseModelOutputWithPast( last_hidden_state=hidden_states, - past_key_values=presents, + past_key_values=next_cache, hidden_states=all_hidden_states, attentions=all_self_attentions, ) @@ -593,9 +600,11 @@ def set_output_embeddings(self, new_embeddings): def prepare_inputs_for_generation(self, input_ids, inputs_embeds=None, past_key_values=None, **kwargs): token_type_ids = kwargs.get("token_type_ids", None) + past_length = 0 # Omit tokens covered by past_key_values if past_key_values: - past_length = past_key_values[0][0].shape[2] + past_length = cache_length = past_key_values.get_seq_length() + max_cache_length = past_key_values.get_max_length() # Some generation methods already pass only the last input ID if input_ids.shape[1] > past_length: @@ -608,6 +617,14 @@ def prepare_inputs_for_generation(self, input_ids, inputs_embeds=None, past_key_ if token_type_ids is not None: token_type_ids = token_type_ids[:, -input_ids.shape[1] :] + # If we are about to go beyond the maximum cache length, we need to crop the input attention mask. + if ( + max_cache_length is not None + and attention_mask is not None + and cache_length + input_ids.shape[1] > max_cache_length + ): + attention_mask = attention_mask[:, -max_cache_length:] + attention_mask = kwargs.get("attention_mask", None) position_ids = kwargs.get("position_ids", None) @@ -619,7 +636,7 @@ def prepare_inputs_for_generation(self, input_ids, inputs_embeds=None, past_key_ position_ids = position_ids[:, -input_ids.shape[1] :] # if `inputs_embeds` are passed, we only want to use them in the 1st generation step - if inputs_embeds is not None and past_key_values is None: + if inputs_embeds is not None and past_length == 0: model_inputs = {"inputs_embeds": inputs_embeds} else: model_inputs = {"input_ids": input_ids.contiguous()} diff --git a/src/transformers/models/falcon/modeling_falcon.py b/src/transformers/models/falcon/modeling_falcon.py index 75346601d75b..450a8e44922e 100644 --- a/src/transformers/models/falcon/modeling_falcon.py +++ b/src/transformers/models/falcon/modeling_falcon.py @@ -24,6 +24,7 @@ from torch.nn import functional as F from ...activations import get_activation +from ...cache_utils import Cache, DynamicCache from ...modeling_attn_mask_utils import ( AttentionMaskConverter, _prepare_4d_causal_attention_mask, @@ -258,7 +259,7 @@ def dropout_add(x: torch.Tensor, residual: torch.Tensor, prob: float, training: class FalconAttention(nn.Module): - def __init__(self, config: FalconConfig): + def __init__(self, config: FalconConfig, layer_idx=None): super().__init__() self.config = config @@ -271,6 +272,13 @@ def __init__(self, config: FalconConfig): self.rope_theta = config.rope_theta self.is_causal = True self._use_sdpa = config._attn_implementation == "sdpa" + self.layer_idx = layer_idx + if layer_idx is None: + logger.warning_once( + f"Instantiating {self.__class__.__name__} without passing a `layer_idx` is not recommended and will " + "lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` " + "when creating this class." + ) if self.head_dim * self.num_heads != self.hidden_size: raise ValueError( @@ -406,25 +414,22 @@ def forward( kv_seq_len = key_layer.shape[-2] if layer_past is not None: - kv_seq_len += layer_past[0].shape[-2] + if self.layer_idx is None: + raise ValueError( + f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} " + "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class " + "with a layer index." + ) + kv_seq_len += layer_past.get_seq_length(self.layer_idx) if alibi is None: cos, sin = self.rotary_emb(value_layer, seq_len=kv_seq_len) query_layer, key_layer = apply_rotary_pos_emb(query_layer, key_layer, cos, sin, position_ids) if layer_past is not None: - past_key, past_value = layer_past - # concatenate along seq_length dimension: - # - key: [batch_size, self.num_heads, kv_length, head_dim] - # - value: [batch_size, self.num_heads, kv_length, head_dim] - key_layer = torch.cat((past_key, key_layer), dim=-2) - value_layer = torch.cat((past_value, value_layer), dim=-2) + cache_kwargs = {"sin": sin, "cos": cos} if alibi is None else None + key_layer, value_layer = layer_past.update(key_layer, value_layer, self.layer_idx, cache_kwargs) kv_length = key_layer.shape[-2] - if use_cache: - present = (key_layer, value_layer) - else: - present = None - if self._use_sdpa and query_layer.device.type == "cuda" and attention_mask is not None: # For torch<=2.1.2, SDPA with memory-efficient backend is bugged with non-contiguous inputs with custom attn_mask, # Reference: https://github.com/pytorch/pytorch/issues/112577. @@ -463,9 +468,9 @@ def forward( attn_output = self.dense(attn_output) if output_attentions: - return attn_output, present, attention_scores + return attn_output, layer_past, attention_scores else: - return attn_output, present + return attn_output, layer_past else: if self._use_sdpa and not output_attentions and head_mask is None: @@ -517,9 +522,9 @@ def forward( attn_output = self.dense(attn_output) if output_attentions: - return attn_output, present, attention_probs + return attn_output, layer_past, attention_probs else: - return attn_output, present + return attn_output, layer_past class FalconFlashAttention2(FalconAttention): @@ -562,20 +567,20 @@ def forward( kv_seq_len = key_layer.shape[-2] if layer_past is not None: - kv_seq_len += layer_past[0].shape[-2] + if self.layer_idx is None: + raise ValueError( + f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} " + "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class " + "with a layer index." + ) + kv_seq_len += layer_past.get_seq_length(self.layer_idx) if alibi is None: cos, sin = self.rotary_emb(value_layer, seq_len=kv_seq_len) query_layer, key_layer = apply_rotary_pos_emb(query_layer, key_layer, cos, sin, position_ids) - if layer_past is not None and use_cache: - past_key, past_value = layer_past - # concatenate along seq_length dimension: - # - key: [batch_size, self.num_heads, kv_length, head_dim] - # - value: [batch_size, self.num_heads, kv_length, head_dim] - key_layer = torch.cat((past_key, key_layer), dim=-2) - value_layer = torch.cat((past_value, value_layer), dim=-2) - - past_key_value = (key_layer, value_layer) if use_cache else None + if layer_past is not None: + cache_kwargs = {"sin": sin, "cos": cos} if alibi is None else None + key_layer, value_layer = layer_past.update(key_layer, value_layer, self.layer_idx, cache_kwargs) # TODO: These transpose are quite inefficient but Flash Attention requires the layout [batch_size, sequence_length, num_heads, head_dim]. We would need to refactor the KV cache # to be able to avoid many of these transpose/reshape/view. @@ -621,7 +626,7 @@ def forward( if not output_attentions: attn_weights = None - return attn_output, past_key_value, attn_weights + return attn_output, layer_past, attn_weights # Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2._flash_attention_forward def _flash_attention_forward( @@ -747,12 +752,12 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: class FalconDecoderLayer(nn.Module): - def __init__(self, config: FalconConfig): + def __init__(self, config: FalconConfig, layer_idx=None): super().__init__() hidden_size = config.hidden_size self.num_heads = config.num_attention_heads - self.self_attention = FALCON_ATTENTION_CLASSES[config._attn_implementation](config) + self.self_attention = FALCON_ATTENTION_CLASSES[config._attn_implementation](config, layer_idx) self.mlp = FalconMLP(config) self.hidden_dropout = config.hidden_dropout self.config = config @@ -836,7 +841,7 @@ def forward( else: outputs = (output,) + outputs[1:] - return outputs # hidden_states, present, attentions + return outputs # hidden_states, past_kv, attentions FALCON_START_DOCSTRING = r""" @@ -926,6 +931,7 @@ class FalconPreTrainedModel(PreTrainedModel): _no_split_modules = ["FalconDecoderLayer"] _supports_flash_attn_2 = True _supports_sdpa = True + _supports_cache_class = True def __init__(self, *inputs, **kwargs): super().__init__(*inputs, **kwargs) @@ -982,7 +988,7 @@ def __init__(self, config: FalconConfig): self.word_embeddings = nn.Embedding(config.vocab_size, self.embed_dim) # Transformer blocks - self.h = nn.ModuleList([FalconDecoderLayer(config) for _ in range(config.num_hidden_layers)]) + self.h = nn.ModuleList([FalconDecoderLayer(config, layer_idx=i) for i in range(config.num_hidden_layers)]) self._use_flash_attention_2 = config._attn_implementation == "flash_attention_2" self._use_sdpa = config._attn_implementation == "sdpa" @@ -1035,9 +1041,6 @@ def forward( else: raise ValueError("You have to specify either input_ids or inputs_embeds") - if past_key_values is None: - past_key_values = tuple([None] * len(self.h)) - if inputs_embeds is None: inputs_embeds = self.word_embeddings(input_ids) @@ -1049,14 +1052,17 @@ def forward( "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." ) use_cache = False - presents = () if use_cache else None + next_decoder_cache = None all_self_attentions = () if output_attentions else None all_hidden_states = () if output_hidden_states else None # Compute alibi tensor: check build_alibi_tensor documentation past_key_values_length = 0 - if past_key_values[0] is not None: - past_key_values_length = past_key_values[0][0].shape[-2] + if use_cache: + use_legacy_cache = not isinstance(past_key_values, Cache) + if use_legacy_cache: + past_key_values = DynamicCache.from_legacy_cache(past_key_values) + past_key_values_length = past_key_values.get_seq_length() if self.use_alibi: mask = ( @@ -1126,7 +1132,7 @@ def forward( # head_mask has shape n_layer x batch x num_heads x N x N head_mask = self.get_head_mask(head_mask, self.config.num_hidden_layers) - for i, (block, layer_past) in enumerate(zip(self.h, past_key_values)): + for i, block in enumerate(self.h): if output_hidden_states: all_hidden_states = all_hidden_states + (hidden_states,) @@ -1138,14 +1144,14 @@ def forward( attention_mask, position_ids, head_mask[i], - layer_past, + past_key_values, use_cache, output_attentions, ) else: outputs = block( hidden_states, - layer_past=layer_past, + layer_past=past_key_values, attention_mask=attention_mask, position_ids=position_ids, head_mask=head_mask[i], @@ -1156,7 +1162,7 @@ def forward( hidden_states = outputs[0] if use_cache is True: - presents = presents + (outputs[1],) + next_decoder_cache = outputs[1] if output_attentions: all_self_attentions = all_self_attentions + (outputs[2 if use_cache else 1],) @@ -1167,12 +1173,16 @@ def forward( if output_hidden_states: all_hidden_states = all_hidden_states + (hidden_states,) + next_cache = None + if use_cache: + next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache + if not return_dict: - return tuple(v for v in [hidden_states, presents, all_hidden_states, all_self_attentions] if v is not None) + return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attentions] if v is not None) return BaseModelOutputWithPastAndCrossAttentions( last_hidden_state=hidden_states, - past_key_values=presents, + past_key_values=next_cache, hidden_states=all_hidden_states, attentions=all_self_attentions, ) @@ -1208,8 +1218,10 @@ def prepare_inputs_for_generation( inputs_embeds: Optional[torch.Tensor] = None, **kwargs, ) -> dict: + past_length = 0 if past_key_values is not None: - past_length = past_key_values[0][0].shape[2] + past_length = cache_length = past_key_values.get_seq_length() + max_cache_length = past_key_values.get_max_length() # Some generation methods already pass only the last input ID if input_ids.shape[1] > past_length: @@ -1220,6 +1232,15 @@ def prepare_inputs_for_generation( input_ids = input_ids[:, remove_prefix_length:] + # If we are about to go beyond the maximum cache length, we need to crop the input attention mask. + if ( + max_cache_length is not None + and attention_mask is not None + and cache_length + input_ids.shape[1] > max_cache_length + ): + attention_mask = attention_mask[:, -max_cache_length:] + + # Note: versions of Falcon with alibi do not use position_ids. It is used with RoPE. if not self.transformer.use_alibi and attention_mask is not None and position_ids is None: # create position_ids on the fly for batch generation @@ -1228,7 +1249,7 @@ def prepare_inputs_for_generation( if past_key_values: position_ids = position_ids[:, -input_ids.shape[1] :] - if inputs_embeds is not None and past_key_values is None: + if inputs_embeds is not None and past_length == 0: model_inputs = {"inputs_embeds": inputs_embeds} else: model_inputs = {"input_ids": input_ids} diff --git a/src/transformers/models/git/modeling_git.py b/src/transformers/models/git/modeling_git.py index 8e14e3a89991..27e4f58a55e9 100644 --- a/src/transformers/models/git/modeling_git.py +++ b/src/transformers/models/git/modeling_git.py @@ -25,6 +25,7 @@ from torch.nn import CrossEntropyLoss from ...activations import ACT2FN +from ...cache_utils import Cache, DynamicCache from ...file_utils import ModelOutput from ...modeling_attn_mask_utils import _prepare_4d_attention_mask from ...modeling_outputs import ( @@ -124,13 +125,20 @@ def forward( class GitSelfAttention(nn.Module): - def __init__(self, config, position_embedding_type=None): + def __init__(self, config, position_embedding_type=None, layer_idx=None): super().__init__() if config.hidden_size % config.num_attention_heads != 0 and not hasattr(config, "embedding_size"): raise ValueError( f"The hidden size ({config.hidden_size}) is not a multiple of the number of attention " f"heads ({config.num_attention_heads})" ) + self.layer_idx = layer_idx + if layer_idx is None: + logger.warning_once( + f"Instantiating {self.__class__.__name__} without passing a `layer_idx` is not recommended and will " + "lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` " + "when creating this class." + ) self.num_attention_heads = config.num_attention_heads self.attention_head_size = int(config.hidden_size / config.num_attention_heads) @@ -168,39 +176,22 @@ def forward( mixed_query_layer = self.query(hidden_states) cutoff = self.image_patch_tokens if pixel_values_present else 0 - if past_key_value is not None: - key_layer = self.transpose_for_scores(self.key(hidden_states)) - value_layer = self.transpose_for_scores(self.value(hidden_states)) - key_layer = torch.cat([key_layer[:, :, :cutoff, :], past_key_value[0], key_layer[:, :, -1:, :]], dim=2) - value_layer = torch.cat( - [value_layer[:, :, :cutoff, :], past_key_value[1], value_layer[:, :, -1:, :]], dim=2 - ) - else: - key_layer = self.transpose_for_scores(self.key(hidden_states)) - value_layer = self.transpose_for_scores(self.value(hidden_states)) + key_layer = self.transpose_for_scores(self.key(hidden_states)) + value_layer = self.transpose_for_scores(self.value(hidden_states)) + if past_key_value is not None: + # NOTE: like in other caches, we store the text component. In GIT it means we discard the image component. + key_layer_past, value_layer_past = past_key_value.update(key_layer[:, :, cutoff:, :], value_layer[:, :, cutoff:, :], self.layer_idx) + key_layer = torch.cat([key_layer[:, :, :cutoff, :], key_layer_past], dim=2) + value_layer = torch.cat([value_layer[:, :, :cutoff, :], value_layer_past], dim=2) query_layer = self.transpose_for_scores(mixed_query_layer) - use_cache = past_key_value is not None - # if cross_attention save Tuple(torch.Tensor, torch.Tensor) of all cross attention key/value_states. - # Further calls to cross_attention layer can then reuse all cross-attention - # key/value_states (first "if" case) - # if uni-directional self-attention (decoder) save Tuple(torch.Tensor, torch.Tensor) of - # all previous decoder key/value_states. Further calls to uni-directional self-attention - # can concat previous decoder key/value_states to current projected key/value_states (third "elif" case) - # if encoder bi-directional self-attention `past_key_value` is always `None` - # NOTE: like in other caches, we store the text component. In GIT it means we discard the image component. - past_key_value = ( - key_layer[:, :, cutoff:, :], - value_layer[:, :, cutoff:, :], - ) - # Take the dot product between "query" and "key" to get the raw attention scores. attention_scores = torch.matmul(query_layer, key_layer.transpose(-1, -2)) if self.position_embedding_type == "relative_key" or self.position_embedding_type == "relative_key_query": query_length, key_length = query_layer.shape[2], key_layer.shape[2] - if use_cache: + if past_key_value is not None: position_ids_l = torch.tensor(key_length - 1, dtype=torch.long, device=hidden_states.device).view( -1, 1 ) @@ -270,10 +261,10 @@ def forward(self, hidden_states: torch.Tensor, input_tensor: torch.Tensor) -> to class GitAttention(nn.Module): # Copied from transformers.models.bert.modeling_bert.BertAttention.__init__ with Bert->Git,BERT->GIT - def __init__(self, config, position_embedding_type=None): + def __init__(self, config, position_embedding_type=None, layer_idx=None): super().__init__() self.self = GIT_SELF_ATTENTION_CLASSES[config._attn_implementation]( - config, position_embedding_type=position_embedding_type + config, position_embedding_type=position_embedding_type, layer_idx=layer_idx ) self.output = GitSelfOutput(config) self.pruned_heads = set() @@ -351,11 +342,11 @@ def forward(self, hidden_states: torch.Tensor, input_tensor: torch.Tensor) -> to class GitLayer(nn.Module): - def __init__(self, config): + def __init__(self, config, layer_idx=None): super().__init__() self.chunk_size_feed_forward = config.chunk_size_feed_forward self.seq_len_dim = 1 - self.attention = GitAttention(config) + self.attention = GitAttention(config, layer_idx=layer_idx) self.intermediate = GitIntermediate(config) self.output = GitOutput(config) @@ -369,13 +360,12 @@ def forward( pixel_values_present: Optional[bool] = False, ) -> Tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 - self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( hidden_states, attention_mask, head_mask, output_attentions=output_attentions, - past_key_value=self_attn_past_key_value, + past_key_value=past_key_value, pixel_values_present=pixel_values_present, ) attention_output = self_attention_outputs[0] @@ -405,7 +395,7 @@ class GitEncoder(nn.Module): def __init__(self, config): super().__init__() self.config = config - self.layer = nn.ModuleList([GitLayer(config) for _ in range(config.num_hidden_layers)]) + self.layer = nn.ModuleList([GitLayer(config, i) for i in range(config.num_hidden_layers)]) self.gradient_checkpointing = False def forward( @@ -427,16 +417,19 @@ def forward( ) use_cache = False + if use_cache: + use_legacy_cache = not isinstance(past_key_values, Cache) + if use_legacy_cache: + past_key_values = DynamicCache.from_legacy_cache(past_key_values) + all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None - - next_decoder_cache = () if use_cache else None + next_decoder_cache = None for i, layer_module in enumerate(self.layer): if output_hidden_states: all_hidden_states = all_hidden_states + (hidden_states,) layer_head_mask = head_mask[i] if head_mask is not None else None - past_key_value = past_key_values[i] if past_key_values is not None else None if self.gradient_checkpointing and self.training: layer_outputs = self._gradient_checkpointing_func( @@ -444,7 +437,7 @@ def forward( hidden_states, attention_mask, layer_head_mask, - past_key_value, + past_key_values, output_attentions, ) else: @@ -452,26 +445,30 @@ def forward( hidden_states, attention_mask, layer_head_mask, - past_key_value, + past_key_values, output_attentions, pixel_values_present, ) hidden_states = layer_outputs[0] if use_cache: - next_decoder_cache += (layer_outputs[-1],) + next_decoder_cache = layer_outputs[-1] if output_attentions: all_self_attentions = all_self_attentions + (layer_outputs[1],) if output_hidden_states: all_hidden_states = all_hidden_states + (hidden_states,) + next_cache = None + if use_cache: + next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache + if not return_dict: return tuple( v for v in [ hidden_states, - next_decoder_cache, + next_cache, all_hidden_states, all_self_attentions, ] @@ -479,7 +476,7 @@ def forward( ) return BaseModelOutputWithPast( last_hidden_state=hidden_states, - past_key_values=next_decoder_cache, + past_key_values=next_cache, hidden_states=all_hidden_states, attentions=all_self_attentions, ) @@ -494,6 +491,7 @@ class GitPreTrainedModel(PreTrainedModel): config_class = GitConfig base_model_prefix = "git" supports_gradient_checkpointing = True + _supports_cache_class = True def _init_weights(self, module): """Initialize the weights""" @@ -1195,7 +1193,9 @@ def forward( seq_length = input_shape[1] # past_key_values_length - past_key_values_length = past_key_values[0][0].shape[2] if past_key_values is not None else 0 + past_key_values_length = 0 + if past_key_values is not None: + past_key_values_length = past_key_values[0][0].shape[2] if not isinstance(past_key_values, Cache) else past_key_values.get_seq_length() # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head @@ -1522,7 +1522,17 @@ def prepare_inputs_for_generation( ): # cut decoder_input_ids if past_key_values is used if past_key_values is not None: - input_ids = input_ids[:, -1:] + past_length = cache_length = past_key_values.get_seq_length() + max_cache_length = past_key_values.get_max_length() + + # Some generation methods already pass only the last input ID + if input_ids.shape[1] > past_length: + remove_prefix_length = past_length + else: + # Default to old behavior: keep only final ID + remove_prefix_length = input_ids.shape[1] - 1 + + input_ids = input_ids[:, remove_prefix_length:] # if model is used as a decoder in encoder-decoder model, the decoder attention mask is created on the fly input_shape = input_ids.shape diff --git a/src/transformers/models/gpt_neo/modeling_gpt_neo.py b/src/transformers/models/gpt_neo/modeling_gpt_neo.py index 694c0bc88b5b..e9205ce80f4b 100755 --- a/src/transformers/models/gpt_neo/modeling_gpt_neo.py +++ b/src/transformers/models/gpt_neo/modeling_gpt_neo.py @@ -24,6 +24,7 @@ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACT2FN +from ...cache_utils import Cache, DynamicCache from ...modeling_attn_mask_utils import _prepare_4d_causal_attention_mask from ...modeling_outputs import ( BaseModelOutputWithPast, @@ -164,7 +165,7 @@ def load_tf_weights_in_gpt_neo(model, config, gpt_neo_checkpoint_path): class GPTNeoSelfAttention(nn.Module): - def __init__(self, config, attention_type): + def __init__(self, config, attention_type, layer_idx=None): super().__init__() self.config = config @@ -185,6 +186,7 @@ def __init__(self, config, attention_type): self.attn_dropout = nn.Dropout(float(config.attention_dropout)) self.resid_dropout = nn.Dropout(float(config.resid_dropout)) self.is_causal = True + self.layer_idx = layer_idx self.embed_dim = config.hidden_size self.num_heads = config.num_heads @@ -265,15 +267,7 @@ def forward( value = self._split_heads(value, self.num_heads, self.head_dim) if layer_past is not None: - past_key = layer_past[0] - past_value = layer_past[1] - key = torch.cat((past_key, key), dim=-2) - value = torch.cat((past_value, value), dim=-2) - - if use_cache is True: - present = (key, value) - else: - present = None + key, value = layer_past.update(key, value, self.layer_idx) attn_output, attn_weights = self._attn(query, key, value, attention_mask, head_mask) @@ -281,11 +275,11 @@ def forward( attn_output = self.out_proj(attn_output) attn_output = self.resid_dropout(attn_output) - outputs = (attn_output, present) + outputs = (attn_output, layer_past) if output_attentions: outputs += (attn_weights,) - return outputs # a, present, (attentions) + return outputs # a, past_kv, (attentions) class GPTNeoFlashAttention2(GPTNeoSelfAttention): @@ -324,15 +318,7 @@ def forward( value = self._split_heads(value, self.num_heads, self.head_dim) if layer_past is not None: - past_key = layer_past[0] - past_value = layer_past[1] - key = torch.cat((past_key, key), dim=-2) - value = torch.cat((past_value, value), dim=-2) - - if use_cache is True: - present = (key, value) - else: - present = None + key, value = layer_past.update(key, value, self.layer_idx) query_length = query.shape[2] tgt_len = key.shape[2] @@ -378,7 +364,7 @@ def forward( attn_output = self.out_proj(attn_weights_reshaped) attn_output = self.resid_dropout(attn_output) - outputs = (attn_output, present) + outputs = (attn_output, layer_past) if output_attentions: outputs += (attn_weights_reshaped,) @@ -491,14 +477,21 @@ def _upad_input(self, query_layer, key_layer, value_layer, attention_mask, query class GPTNeoAttention(nn.Module): - def __init__(self, config, layer_id=0): + def __init__(self, config, layer_idx=None): super().__init__() - self.layer_id = layer_id + self.layer_idx = layer_id = layer_idx + if layer_idx is None: + logger.warning_once( + f"Instantiating {self.__class__.__name__} without passing a `layer_idx` is not recommended and will " + "lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` " + "when creating this class." + ) + layer_id = 0 # keep 0 for BC when used to get attention_type self.attention_layers = config.attention_layers self.attention_type = self.attention_layers[layer_id] if self.attention_type in ["global", "local"]: - self.attention = GPT_NEO_ATTENTION_CLASSES[config._attn_implementation](config, self.attention_type) + self.attention = GPT_NEO_ATTENTION_CLASSES[config._attn_implementation](config, self.attention_type, layer_idx) else: raise NotImplementedError( "Only attn layer types 'global' and 'local' exist, but got `config.attention_layers`: " @@ -542,12 +535,12 @@ def forward(self, hidden_states): class GPTNeoBlock(nn.Module): - def __init__(self, config, layer_id): + def __init__(self, config, layer_idx=None): super().__init__() hidden_size = config.hidden_size inner_dim = config.intermediate_size if config.intermediate_size is not None else 4 * hidden_size self.ln_1 = nn.LayerNorm(hidden_size, eps=config.layer_norm_epsilon) - self.attn = GPTNeoAttention(config, layer_id) + self.attn = GPTNeoAttention(config, layer_idx) self.ln_2 = nn.LayerNorm(hidden_size, eps=config.layer_norm_epsilon) self.mlp = GPTNeoMLP(inner_dim, config) @@ -586,7 +579,7 @@ def forward( else: outputs = (hidden_states,) + outputs[1:] - return outputs # hidden_states, present, (attentions, cross_attentions) + return outputs # hidden_states, past_kv, attentions class GPTNeoPreTrainedModel(PreTrainedModel): @@ -602,6 +595,7 @@ class GPTNeoPreTrainedModel(PreTrainedModel): _no_split_modules = ["GPTNeoBlock"] _skip_keys_device_placement = "past_key_values" _supports_flash_attn_2 = True + _supports_cache_class = True def __init__(self, *inputs, **kwargs): super().__init__(*inputs, **kwargs) @@ -716,7 +710,7 @@ def __init__(self, config): self.wte = nn.Embedding(config.vocab_size, self.embed_dim) self.wpe = nn.Embedding(config.max_position_embeddings, self.embed_dim) self.drop = nn.Dropout(float(config.embed_dropout)) - self.h = nn.ModuleList([GPTNeoBlock(config, layer_id=i) for i in range(config.num_layers)]) + self.h = nn.ModuleList([GPTNeoBlock(config, layer_idx=i) for i in range(config.num_layers)]) self._use_flash_attention_2 = config._attn_implementation == "flash_attention_2" self.ln_f = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_epsilon) @@ -773,12 +767,13 @@ def forward( if token_type_ids is not None: token_type_ids = token_type_ids.view(-1, input_shape[-1]) - if past_key_values is None: - past_length = 0 - past_key_values = tuple([None] * len(self.h)) - else: - past_length = past_key_values[0][0].size(-2) - + past_length = 0 + if use_cache: + use_legacy_cache = not isinstance(past_key_values, Cache) + if use_legacy_cache: + past_key_values = DynamicCache.from_legacy_cache(past_key_values) + past_length = past_key_values.get_seq_length() + if position_ids is None: position_ids = torch.arange(past_length, input_shape[-1] + past_length, dtype=torch.long, device=device) position_ids = position_ids.unsqueeze(0) @@ -817,10 +812,10 @@ def forward( ) use_cache = False - presents = () if use_cache else None + next_decoder_cache = None all_self_attentions = () if output_attentions else None all_hidden_states = () if output_hidden_states else None - for i, (block, layer_past) in enumerate(zip(self.h, past_key_values)): + for i, block in enumerate(self.h): if output_hidden_states: all_hidden_states = all_hidden_states + (hidden_states,) @@ -837,7 +832,7 @@ def forward( else: outputs = block( hidden_states, - layer_past=layer_past, + layer_past=past_key_values, attention_mask=attention_mask, head_mask=head_mask[i], use_cache=use_cache, @@ -845,8 +840,8 @@ def forward( ) hidden_states = outputs[0] - if use_cache is True: - presents = presents + (outputs[1],) + if use_cache: + next_decoder_cache = outputs[1] if output_attentions: all_self_attentions = all_self_attentions + (outputs[2 if use_cache else 1],) @@ -858,12 +853,16 @@ def forward( if output_hidden_states: all_hidden_states = all_hidden_states + (hidden_states,) + next_cache = None + if use_cache: + next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache + if not return_dict: - return tuple(v for v in [hidden_states, presents, all_hidden_states, all_self_attentions] if v is not None) + return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attentions] if v is not None) return BaseModelOutputWithPast( last_hidden_state=hidden_states, - past_key_values=presents, + past_key_values=next_cache, hidden_states=all_hidden_states, attentions=all_self_attentions, ) @@ -895,9 +894,11 @@ def set_output_embeddings(self, new_embeddings): def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_embeds=None, **kwargs): token_type_ids = kwargs.get("token_type_ids", None) + past_length = 0 # Omit tokens covered by past_key_values - if past_key_values: - past_length = past_key_values[0][0].shape[2] + if past_key_values is not None: + past_length = cache_length = past_key_values.get_seq_length() + max_cache_length = past_key_values.get_max_length() # Some generation methods already pass only the last input ID if input_ids.shape[1] > past_length: @@ -907,8 +908,17 @@ def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_ remove_prefix_length = input_ids.shape[1] - 1 input_ids = input_ids[:, remove_prefix_length:] + if token_type_ids is not None: token_type_ids = token_type_ids[:, -input_ids.shape[1] :] + + # If we are about to go beyond the maximum cache length, we need to crop the input attention mask. + if ( + max_cache_length is not None + and attention_mask is not None + and cache_length + input_ids.shape[1] > max_cache_length + ): + attention_mask = attention_mask[:, -max_cache_length:] attention_mask = kwargs.get("attention_mask", None) position_ids = kwargs.get("position_ids", None) @@ -921,7 +931,7 @@ def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_ position_ids = position_ids[:, -input_ids.shape[1] :] # if `inputs_embeds` are passed, we only want to use them in the 1st generation step - if inputs_embeds is not None and past_key_values is None: + if inputs_embeds is not None and past_length == 0: model_inputs = {"inputs_embeds": inputs_embeds} else: model_inputs = {"input_ids": input_ids} diff --git a/src/transformers/models/gpt_neox/modeling_gpt_neox.py b/src/transformers/models/gpt_neox/modeling_gpt_neox.py index ba2fb8aa766f..8a167af5e949 100755 --- a/src/transformers/models/gpt_neox/modeling_gpt_neox.py +++ b/src/transformers/models/gpt_neox/modeling_gpt_neox.py @@ -23,6 +23,7 @@ from torch.nn import functional as F from ...activations import ACT2FN +from ...cache_utils import Cache, DynamicCache from ...file_utils import ( add_code_sample_docstrings, add_start_docstrings, @@ -78,6 +79,7 @@ class GPTNeoXPreTrainedModel(PreTrainedModel): _no_split_modules = ["GPTNeoXLayer"] _skip_keys_device_placement = "past_key_values" _supports_flash_attn_2 = True + _supports_cache_class = True def _init_weights(self, module): """Initialize the weights""" @@ -95,7 +97,7 @@ def _init_weights(self, module): class GPTNeoXAttention(nn.Module): - def __init__(self, config): + def __init__(self, config, layer_idx=None): super().__init__() self.config = config self.num_attention_heads = config.num_attention_heads @@ -111,11 +113,18 @@ def __init__(self, config): self.register_buffer("masked_bias", torch.tensor(-1e9), persistent=False) self._init_rope() + if layer_idx is None: + logger.warning_once( + f"Instantiating {self.__class__.__name__} without passing a `layer_idx` is not recommended and will " + "lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` " + "when creating this class." + ) self.norm_factor = self.head_size**-0.5 self.query_key_value = nn.Linear(config.hidden_size, 3 * config.hidden_size, bias=config.attention_bias) self.dense = nn.Linear(config.hidden_size, config.hidden_size, bias=config.attention_bias) self.attention_dropout = nn.Dropout(config.attention_dropout) self.is_causal = True + self.layer_idx = layer_idx def _init_bias(self, max_positions, device=None): self.register_buffer( @@ -164,8 +173,6 @@ def forward( output_attentions: Optional[bool] = False, padding_mask: Optional[torch.Tensor] = None, ): - has_layer_past = layer_past is not None - # Compute QKV # Attention heads [batch, seq_len, hidden_size] # --> [batch, seq_len, (np * 3 * head_size)] @@ -189,20 +196,24 @@ def forward( # Compute token offset for rotary embeddings (when decoding) seq_len = key.shape[-2] - if has_layer_past: - seq_len += layer_past[0].shape[-2] + if layer_past is not None: + if self.layer_idx is None: + raise ValueError( + f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} " + "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class " + "with a layer index." + ) + seq_len += layer_past.get_seq_length(self.layer_idx) + cos, sin = self.rotary_emb(value, seq_len=seq_len) query, key = apply_rotary_pos_emb(query_rot, key_rot, cos, sin, position_ids) query = torch.cat((query, query_pass), dim=-1) key = torch.cat((key, key_pass), dim=-1) # Cache QKV values - if has_layer_past: - past_key = layer_past[0] - past_value = layer_past[1] - key = torch.cat((past_key, key), dim=-2) - value = torch.cat((past_value, value), dim=-2) - present = (key, value) if use_cache else None + if layer_past is not None: + cache_kwargs = {"sin": sin, "cos": cos, "partial_rotation_size": self.rotary_emb.dim} + key, value = layer_past.update(key, value, self.layer_idx, cache_kwargs) # Compute attention attn_output, attn_weights = self._attn(query, key, value, attention_mask, head_mask) @@ -211,7 +222,7 @@ def forward( attn_output = self._merge_heads(attn_output, self.num_attention_heads, self.head_size) attn_output = self.dense(attn_output) - outputs = (attn_output, present) + outputs = (attn_output, layer_past) if output_attentions: outputs += (attn_weights,) @@ -319,8 +330,6 @@ def forward( use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, ): - has_layer_past = layer_past is not None - # Compute QKV # Attention heads [batch, seq_len, hidden_size] # --> [batch, seq_len, (np * 3 * head_size)] @@ -344,22 +353,27 @@ def forward( key_rot = key[..., : self.rotary_ndims] key_pass = key[..., self.rotary_ndims :] + # Compute token offset for rotary embeddings (when decoding) # Compute token offset for rotary embeddings (when decoding) seq_len = key.shape[-2] - if has_layer_past: - seq_len += layer_past[0].shape[-2] + if layer_past is not None: + if self.layer_idx is None: + raise ValueError( + f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} " + "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class " + "with a layer index." + ) + seq_len += layer_past.get_seq_length(self.layer_idx) + cos, sin = self.rotary_emb(value, seq_len=seq_len) query, key = apply_rotary_pos_emb(query_rot, key_rot, cos, sin, position_ids) query = torch.cat((query, query_pass), dim=-1) key = torch.cat((key, key_pass), dim=-1) # Cache QKV values - if has_layer_past: - past_key = layer_past[0] - past_value = layer_past[1] - key = torch.cat((past_key, key), dim=-2) - value = torch.cat((past_value, value), dim=-2) - present = (key, value) if use_cache else None + if layer_past is not None: + cache_kwargs = {"sin": sin, "cos": cos, "partial_rotation_size": self.rotary_emb.dim} + key, value = layer_past.update(key, value, self.layer_idx, cache_kwargs) # GPT-neo-X casts query and key in fp32 to apply rotary embedding in full precision target_dtype = value.dtype @@ -410,7 +424,7 @@ def forward( ) attn_output = self.dense(attn_output) - outputs = (attn_output, present) + outputs = (attn_output, layer_past) if output_attentions: outputs += (attn_weights,) @@ -664,14 +678,14 @@ def forward(self, hidden_states): class GPTNeoXLayer(nn.Module): - def __init__(self, config): + def __init__(self, config, layer_idx): super().__init__() self.use_parallel_residual = config.use_parallel_residual self.input_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps) self.post_attention_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps) self.post_attention_dropout = nn.Dropout(config.hidden_dropout) self.post_mlp_dropout = nn.Dropout(config.hidden_dropout) - self.attention = GPT_NEOX_ATTENTION_CLASSES[config._attn_implementation](config) + self.attention = GPT_NEOX_ATTENTION_CLASSES[config._attn_implementation](config, layer_idx) self.mlp = GPTNeoXMLP(config) def forward( @@ -784,7 +798,7 @@ def __init__(self, config): self.embed_in = nn.Embedding(config.vocab_size, config.hidden_size) self.emb_dropout = nn.Dropout(config.hidden_dropout) - self.layers = nn.ModuleList([GPTNeoXLayer(config) for _ in range(config.num_hidden_layers)]) + self.layers = nn.ModuleList([GPTNeoXLayer(config, i) for i in range(config.num_hidden_layers)]) self.final_layer_norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps) self._use_flash_attention_2 = config._attn_implementation == "flash_attention_2" @@ -848,11 +862,12 @@ def forward( batch_size, seq_length = input_shape - if past_key_values is None: - past_length = 0 - past_key_values = tuple([None] * self.config.num_hidden_layers) - else: - past_length = past_key_values[0][0].size(-2) + past_length = 0 + if use_cache: + use_legacy_cache = not isinstance(past_key_values, Cache) + if use_legacy_cache: + past_key_values = DynamicCache.from_legacy_cache(past_key_values) + past_length = past_key_values.get_usable_length(seq_length) if position_ids is None: device = input_ids.device if input_ids is not None else inputs_embeds.device @@ -900,10 +915,10 @@ def forward( ) use_cache = False - presents = () if use_cache else None + next_decoder_cache = None all_attentions = () if output_attentions else None all_hidden_states = () if output_hidden_states else None - for i, (layer, layer_past) in enumerate(zip(self.layers, past_key_values)): + for i, layer in enumerate(self.layers,): if output_hidden_states: all_hidden_states = all_hidden_states + (hidden_states,) @@ -924,13 +939,13 @@ def forward( attention_mask=attention_mask, position_ids=position_ids, head_mask=head_mask[i], - layer_past=layer_past, + layer_past=past_key_values, use_cache=use_cache, output_attentions=output_attentions, ) hidden_states = outputs[0] if use_cache is True: - presents = presents + (outputs[1],) + next_decoder_cache = outputs[1] if output_attentions: all_attentions = all_attentions + (outputs[2 if use_cache else 1],) @@ -939,12 +954,16 @@ def forward( if output_hidden_states: all_hidden_states = all_hidden_states + (hidden_states,) + next_cache = None + if use_cache: + next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache + if not return_dict: - return tuple(v for v in [hidden_states, presents, all_hidden_states, all_attentions] if v is not None) + return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_attentions] if v is not None) return BaseModelOutputWithPast( last_hidden_state=hidden_states, - past_key_values=presents, + past_key_values=next_cache, hidden_states=all_hidden_states, attentions=all_attentions, ) @@ -1070,9 +1089,11 @@ def prepare_inputs_for_generation( self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs ): input_shape = input_ids.shape + past_length = 0 # cut decoder_input_ids if past is used if past_key_values is not None: - past_length = past_key_values[0][0].shape[2] + past_length = cache_length = past_key_values.get_seq_length() + max_cache_length = past_key_values.get_max_length() # Some generation methods already pass only the last input ID if input_ids.shape[1] > past_length: @@ -1080,9 +1101,16 @@ def prepare_inputs_for_generation( else: # Default to old behavior: keep only final ID remove_prefix_length = input_ids.shape[1] - 1 - input_ids = input_ids[:, remove_prefix_length:] + # If we are about to go beyond the maximum cache length, we need to crop the input attention mask. + if ( + max_cache_length is not None + and attention_mask is not None + and cache_length + input_ids.shape[1] > max_cache_length + ): + attention_mask = attention_mask[:, -max_cache_length:] + position_ids = kwargs.get("position_ids", None) if attention_mask is not None and position_ids is None: # create position_ids on the fly for batch generation @@ -1096,7 +1124,7 @@ def prepare_inputs_for_generation( attention_mask = input_ids.new_ones(input_shape) # if `inputs_embeds` are passed, we only want to use them in the 1st generation step - if inputs_embeds is not None and past_key_values is None: + if inputs_embeds is not None and past_length == 0: model_inputs = {"inputs_embeds": inputs_embeds} else: model_inputs = {"input_ids": input_ids} diff --git a/src/transformers/models/gptj/modeling_gptj.py b/src/transformers/models/gptj/modeling_gptj.py index 96f4197a87f2..4553490e2b9a 100644 --- a/src/transformers/models/gptj/modeling_gptj.py +++ b/src/transformers/models/gptj/modeling_gptj.py @@ -25,6 +25,7 @@ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACT2FN +from ...cache_utils import Cache, DynamicCache from ...modeling_outputs import ( BaseModelOutputWithPast, CausalLMOutputWithPast, @@ -95,7 +96,7 @@ def apply_rotary_pos_emb(tensor: torch.Tensor, sin: torch.Tensor, cos: torch.Ten class GPTJAttention(nn.Module): - def __init__(self, config): + def __init__(self, config, layer_idx=None): super().__init__() self.config = config max_positions = config.max_position_embeddings @@ -112,6 +113,13 @@ def __init__(self, config): self.resid_dropout = nn.Dropout(config.resid_pdrop) self.is_causal = True + self.layer_idx = layer_idx + if layer_idx is None: + logger.warning_once( + f"Instantiating {self.__class__.__name__} without passing a `layer_idx` is not recommended and will " + "lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` " + "when creating this class." + ) self.embed_dim = config.hidden_size self.num_attention_heads = config.num_attention_heads @@ -259,18 +267,11 @@ def forward( key = key.permute(0, 2, 1, 3) query = query.permute(0, 2, 1, 3) + # Note that this cast is quite ugly, but is not implemented before ROPE as the original codebase keeps the key in float32 all along the computation. + # Reference: https://github.com/kingoflolz/mesh-transformer-jax/blob/f8315e3003033b23f21d78361b288953064e0e76/mesh_transformer/layers.py#L128 if layer_past is not None: - past_key = layer_past[0] - past_value = layer_past[1] - key = torch.cat((past_key, key), dim=-2) - value = torch.cat((past_value, value), dim=-2) - - if use_cache is True: - # Note that this cast is quite ugly, but is not implemented before ROPE as the original codebase keeps the key in float32 all along the computation. - # Reference: https://github.com/kingoflolz/mesh-transformer-jax/blob/f8315e3003033b23f21d78361b288953064e0e76/mesh_transformer/layers.py#L128 - present = (key.to(hidden_states.dtype), value) - else: - present = None + cache_kwargs = {"sin": sin, "cos": cos, "partial_rotation_size": self.rotary_dim} + key, value = layer_past.update(key.to(hidden_states.dtype), value, self.layer_idx, cache_kwargs) # compute self-attention: V x Softmax(QK^T) attn_output, attn_weights = self._attn(query, key, value, attention_mask, head_mask) @@ -279,7 +280,7 @@ def forward( attn_output = self.out_proj(attn_output) attn_output = self.resid_dropout(attn_output) - outputs = (attn_output, present) + outputs = (attn_output, layer_past) if output_attentions: outputs += (attn_weights,) @@ -356,18 +357,11 @@ def forward( query = query.permute(0, 2, 1, 3) # value: batch_size x num_attention_heads x seq_length x head_dim + # Note that this cast is quite ugly, but is not implemented before ROPE as the original codebase keeps the key in float32 all along the computation. + # Reference: https://github.com/kingoflolz/mesh-transformer-jax/blob/f8315e3003033b23f21d78361b288953064e0e76/mesh_transformer/layers.py#L128 if layer_past is not None: - past_key = layer_past[0] - past_value = layer_past[1] - key = torch.cat((past_key, key), dim=-2) - value = torch.cat((past_value, value), dim=-2) - - if use_cache is True: - # Note that this cast is quite ugly, but is not implemented before ROPE as the original codebase keeps the key in float32 all along the computation. - # Reference: https://github.com/kingoflolz/mesh-transformer-jax/blob/f8315e3003033b23f21d78361b288953064e0e76/mesh_transformer/layers.py#L128 - present = (key.to(hidden_states.dtype), value) - else: - present = None + cache_kwargs = {"sin": sin, "cos": cos, "partial_rotation_size": self.rotary_dim} + key, value = layer_past.update(key.to(hidden_states.dtype), value, self.layer_idx, cache_kwargs) # The Flash attention requires the input to have the shape # batch_size x seq_length x head_dim x hidden_dim @@ -424,7 +418,7 @@ def forward( attn_output = self.out_proj(attn_output) attn_output = self.resid_dropout(attn_output) - outputs = (attn_output, present) + outputs = (attn_output, layer_past) if output_attentions: outputs += (attn_weights,) @@ -556,11 +550,11 @@ def forward(self, hidden_states: Optional[torch.FloatTensor]) -> torch.FloatTens class GPTJBlock(nn.Module): - def __init__(self, config): + def __init__(self, config, layer_idx=None): super().__init__() inner_dim = config.n_inner if config.n_inner is not None else 4 * config.n_embd self.ln_1 = nn.LayerNorm(config.n_embd, eps=config.layer_norm_epsilon) - self.attn = GPTJ_ATTENTION_CLASSES[config._attn_implementation](config) + self.attn = GPTJ_ATTENTION_CLASSES[config._attn_implementation](config, layer_idx) self.mlp = GPTJMLP(inner_dim, config) def forward( @@ -611,6 +605,7 @@ class GPTJPreTrainedModel(PreTrainedModel): _no_split_modules = ["GPTJBlock"] _skip_keys_device_placement = "past_key_values" _supports_flash_attn_2 = True + _supports_cache_class = True def __init__(self, *inputs, **kwargs): super().__init__(*inputs, **kwargs) @@ -753,7 +748,7 @@ def __init__(self, config): self.vocab_size = config.vocab_size self.wte = nn.Embedding(config.vocab_size, self.embed_dim) self.drop = nn.Dropout(config.embd_pdrop) - self.h = nn.ModuleList([GPTJBlock(config) for _ in range(config.n_layer)]) + self.h = nn.ModuleList([GPTJBlock(config, layer_idx=i) for i in range(config.n_layer)]) self.ln_f = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_epsilon) # Model parallel @@ -860,11 +855,12 @@ def forward( if token_type_ids is not None: token_type_ids = token_type_ids.view(-1, input_shape[-1]) - if past_key_values is None: - past_length = 0 - past_key_values = tuple([None] * len(self.h)) - else: - past_length = past_key_values[0][0].size(-2) + past_length = 0 + if use_cache: + use_legacy_cache = not isinstance(past_key_values, Cache) + if use_legacy_cache: + past_key_values = DynamicCache.from_legacy_cache(past_key_values) + past_length = past_key_values.get_seq_length() if position_ids is None: position_ids = torch.arange(past_length, input_shape[-1] + past_length, dtype=torch.long, device=device) @@ -917,16 +913,19 @@ def forward( ) use_cache = False - presents = () if use_cache else None + next_decoder_cache = None all_self_attentions = () if output_attentions else None all_hidden_states = () if output_hidden_states else None - for i, (block, layer_past) in enumerate(zip(self.h, past_key_values)): + for i, block in enumerate(self.h): # Model parallel if self.model_parallel: torch.cuda.set_device(hidden_states.device) + # Ensure layer_past is on same device as hidden_states (might not be correct) - if layer_past is not None: - layer_past = tuple(past_state.to(hidden_states.device) for past_state in layer_past) + if past_key_values is not None: + past_key_values.key_cache = past_key_values.key_cache.to(hidden_states.device) + past_key_values.value_cache = past_key_values.value_cache.to(hidden_states.device) + # Ensure that attention_mask is always on the same device as hidden_states if attention_mask is not None: attention_mask = attention_mask.to(hidden_states.device) @@ -949,7 +948,7 @@ def forward( else: outputs = block( hidden_states=hidden_states, - layer_past=layer_past, + layer_past=past_key_values, attention_mask=attention_mask, position_ids=position_ids, head_mask=head_mask[i], @@ -959,7 +958,7 @@ def forward( hidden_states = outputs[0] if use_cache is True: - presents = presents + (outputs[1],) + next_decoder_cache = outputs[1] if output_attentions: all_self_attentions = all_self_attentions + (outputs[2 if use_cache else 1],) @@ -977,12 +976,16 @@ def forward( if output_hidden_states: all_hidden_states = all_hidden_states + (hidden_states,) + next_cache = None + if use_cache: + next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache + if not return_dict: - return tuple(v for v in [hidden_states, presents, all_hidden_states, all_self_attentions] if v is not None) + return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attentions] if v is not None) return BaseModelOutputWithPast( last_hidden_state=hidden_states, - past_key_values=presents, + past_key_values=next_cache, hidden_states=all_hidden_states, attentions=all_self_attentions, ) @@ -1048,9 +1051,11 @@ def set_output_embeddings(self, new_embeddings): def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_embeds=None, **kwargs): token_type_ids = kwargs.get("token_type_ids", None) + past_length = 0 # Omit tokens covered by past_key_values - if past_key_values: - past_length = past_key_values[0][0].shape[2] + if past_key_values is not None: + past_length = cache_length = past_key_values.get_seq_length() + max_cache_length = past_key_values.get_max_length() # Some generation methods already pass only the last input ID if input_ids.shape[1] > past_length: @@ -1062,6 +1067,14 @@ def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_ input_ids = input_ids[:, remove_prefix_length:] if token_type_ids is not None: token_type_ids = token_type_ids[:, -input_ids.shape[1] :] + + # If we are about to go beyond the maximum cache length, we need to crop the input attention mask. + if ( + max_cache_length is not None + and attention_mask is not None + and cache_length + input_ids.shape[1] > max_cache_length + ): + attention_mask = attention_mask[:, -max_cache_length:] attention_mask = kwargs.get("attention_mask", None) position_ids = kwargs.get("position_ids", None) @@ -1074,7 +1087,7 @@ def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_ position_ids = position_ids[:, -input_ids.shape[1] :] # if `inputs_embeds` are passed, we only want to use them in the 1st generation step - if inputs_embeds is not None and past_key_values is None: + if inputs_embeds is not None and past_length == 0: model_inputs = {"inputs_embeds": inputs_embeds} else: model_inputs = {"input_ids": input_ids} diff --git a/src/transformers/models/opt/modeling_opt.py b/src/transformers/models/opt/modeling_opt.py index 42aef28a1c53..d77ea618c393 100644 --- a/src/transformers/models/opt/modeling_opt.py +++ b/src/transformers/models/opt/modeling_opt.py @@ -23,6 +23,7 @@ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACT2FN +from ...cache_utils import Cache, DynamicCache from ...modeling_attn_mask_utils import _prepare_4d_causal_attention_mask from ...modeling_outputs import ( BaseModelOutputWithPast, @@ -641,7 +642,7 @@ def _init_weights(self, module): - 1 indicates the head is **not masked**, - 0 indicates the head is **masked**. - past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape `(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`. @@ -757,7 +758,7 @@ def forward( - 1 indicates the head is **not masked**, - 0 indicates the head is **masked**. - past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of @@ -804,7 +805,13 @@ def forward( inputs_embeds = self.embed_tokens(input_ids) batch_size, seq_length = input_shape - past_key_values_length = past_key_values[0][0].shape[2] if past_key_values is not None else 0 + past_key_values_length = 0 + if use_cache: + use_legacy_cache = not isinstance(past_key_values, Cache) + if use_legacy_cache: + past_key_values = DynamicCache.from_legacy_cache(past_key_values) + past_key_values_length = past_key_values.get_seq_length() + # required mask seq length can be calculated via length of past mask_seq_length = past_key_values_length + seq_length @@ -908,7 +915,9 @@ def forward( if output_hidden_states: all_hidden_states += (hidden_states,) - next_cache = next_decoder_cache if use_cache else None + next_cache = None + if use_cache: + next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache if not return_dict: return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None) return BaseModelOutputWithPast( From 9505ca4c2fda04581b6ee0d226a4aa74a71f8f19 Mon Sep 17 00:00:00 2001 From: raushan Date: Fri, 14 Jun 2024 16:27:29 +0200 Subject: [PATCH 03/40] revert utils --- src/transformers/generation/utils.py | 102 +++++---------------------- 1 file changed, 18 insertions(+), 84 deletions(-) diff --git a/src/transformers/generation/utils.py b/src/transformers/generation/utils.py index 83c1daa230fb..61fb897bd9c4 100644 --- a/src/transformers/generation/utils.py +++ b/src/transformers/generation/utils.py @@ -1,3 +1,4 @@ + # coding=utf-8 # Copyright 2020 The Google AI Language Team Authors, Facebook AI Research authors and The HuggingFace Inc. team. # Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. @@ -1390,17 +1391,11 @@ def _get_initial_cache_position(self, input_ids, model_kwargs): return model_kwargs past_length = 0 - if model_kwargs.get("past_key_values") is not None: - past_key_values = ( - model_kwargs["past_key_values"] - if not self.config.is_encoder_decoder - else model_kwargs["past_key_values"][0] - ) - if isinstance(past_key_values, Cache): - past_length = past_key_values.get_seq_length() + if "past_key_values" in model_kwargs: + if isinstance(model_kwargs["past_key_values"], Cache): + past_length = model_kwargs["past_key_values"].get_seq_length() else: - past_length = past_key_values[0][0].shape[2] - + past_length = model_kwargs["past_key_values"][0][0].shape[2] if "inputs_embeds" in model_kwargs: cur_len = model_kwargs["inputs_embeds"].shape[1] else: @@ -1771,31 +1766,15 @@ def generate( ) model_kwargs["past_key_values"] = cache_class(cache_config) - # Use DynamicCache() instance by default. This will avoid back and forth from legacy format that # keeps copying the cache thus using much more memory - # Encoder-decoder models hold a tuple of Cache, for self-attn and cross-attn - elif ( - generation_config.cache_implementation is None - and self._supports_default_dynamic_cache() - and model_kwargs["use_cache"] - ): + elif generation_config.cache_implementation is None and self._supports_default_dynamic_cache(): past = model_kwargs.get("past_key_values", None) if past is None: - model_kwargs["past_key_values"] = ( - DynamicCache() if not self.config.is_encoder_decoder else (DynamicCache(), DynamicCache()) - ) + model_kwargs["past_key_values"] = DynamicCache() use_dynamic_cache_by_default = True - elif isinstance(past, tuple) and not isinstance(past[0], DynamicCache): - if self.config.is_encoder_decoder and not isinstance(past[0], DynamicCache): - self_attn = [cache[:2] for cache in model_kwargs["past_key_values"]] - cross_attn = [cache[2:] for cache in model_kwargs["past_key_values"]] - model_kwargs["past_key_values"] = ( - DynamicCache.from_legacy_cache(self_attn), - DynamicCache.from_legacy_cache(cross_attn), - ) - else: - model_kwargs["past_key_values"] = DynamicCache.from_legacy_cache(past) + elif isinstance(past, tuple): + model_kwargs["past_key_values"] = DynamicCache.from_legacy_cache(past) use_dynamic_cache_by_default = True self._validate_generated_length(generation_config, input_ids_length, has_default_max_length) @@ -2070,13 +2049,6 @@ def typeerror(): if isinstance(result, ModelOutput) and hasattr(result, "past_key_values"): if isinstance(result.past_key_values, DynamicCache): result.past_key_values = result.past_key_values.to_legacy_cache() - elif isinstance(result.past_key_values[0], DynamicCache): - num_hidden_layers = len(result.past_key_values[0].key_cache) - self_attn_cache = result.past_key_values[0].to_legacy_cache() - cross_attn_cache = result.past_key_values[1].to_legacy_cache() - result.past_key_values = tuple( - (*self_attn_cache[i], *cross_attn_cache[i]) for i in range(num_hidden_layers) - ) return result def _has_unfinished_sequences(self, this_peer_finished: bool, synced_gpus: bool, device: torch.device) -> bool: @@ -2244,16 +2216,9 @@ def _contrastive_search( while self._has_unfinished_sequences(this_peer_finished, synced_gpus, device=input_ids.device): # if the first step in the loop, encode all the prefix and obtain: (1) past_key_values; # (2) last_hidden_states; (3) logit_for_next_step; (4) update model kwargs for the next step - if ( - model_kwargs.get("past_key_values") is None - or ( - isinstance(model_kwargs["past_key_values"], Cache) - and model_kwargs["past_key_values"].get_seq_length() == 0 - ) - or ( - isinstance(model_kwargs["past_key_values"][0], Cache) - and model_kwargs["past_key_values"][0].get_seq_length() == 0 - ) + if model_kwargs.get("past_key_values") is None or ( + isinstance(model_kwargs["past_key_values"], Cache) + and model_kwargs["past_key_values"].get_seq_length() == 0 ): # prepare inputs model_kwargs["use_cache"] = True @@ -2296,8 +2261,6 @@ def _contrastive_search( f"{self.__class__.__name__} does not support caching and therefore **can't** be used " "for contrastive search." ) - elif isinstance(past_key_values[0], DynamicCache): - pass # skip if new format cache in enc-dec models elif ( not isinstance(past_key_values[0], (tuple, torch.Tensor)) or past_key_values[0][0].shape[0] != batch_size @@ -2345,9 +2308,6 @@ def _contrastive_search( # If it is a static cache, modify it in-place layer after layer to save memory if isinstance(past, DynamicCache): past.batch_repeat_interleave(top_k) - elif isinstance(past[0], DynamicCache): - past[0].batch_repeat_interleave(top_k) - past[1].batch_repeat_interleave(top_k) else: new_key_values = [] for layer in past: @@ -2378,10 +2338,6 @@ def _contrastive_search( outputs["past_key_values"] = None # Remove last token from past K-V since we don't want to append it at this point model_kwargs["past_key_values"].crop(-1) - elif isinstance(outputs["past_key_values"][0], DynamicCache): - outputs["past_key_values"] = None - model_kwargs["past_key_values"][1].crop(-1) - model_kwargs["past_key_values"][0].crop(-1) all_outputs.append(outputs) outputs = stack_model_outputs(all_outputs) @@ -2454,9 +2410,6 @@ def _contrastive_search( # Do it in-place layer per layer to save memory if isinstance(next_past_key_values, DynamicCache): next_past_key_values.batch_select_indices(augmented_idx) - elif isinstance(next_past_key_values[0], DynamicCache): - next_past_key_values[0].batch_select_indices(augmented_idx) - next_past_key_values[1].batch_select_indices(augmented_idx) else: new_key_values = [] for layer in next_past_key_values: @@ -2530,9 +2483,6 @@ def _contrastive_search( if model_kwargs.get("past_key_values") is not None: if isinstance(model_kwargs["past_key_values"], DynamicCache): model_kwargs["past_key_values"].crop(-1) - elif isinstance(model_kwargs["past_key_values"][0], DynamicCache): - model_kwargs["past_key_values"][1].crop(-1) - model_kwargs["past_key_values"][0].crop(-1) else: past_key_values = [] for layer in model_kwargs["past_key_values"]: @@ -2784,13 +2734,10 @@ def _temporary_reorder_cache(self, past_key_values, beam_idx): for this function, with `Cache.reorder_cache` being the sole remaining code path """ model_class = self.__class__.__name__.lower() - # Exception 1: code path for models using tuple of `Cache` (encoder-decoder models) - if isinstance(past_key_values, (tuple)) and isinstance(past_key_values[0], Cache): - past_key_values[0].reorder_cache(beam_idx) - # Exception 2: code path for models using the legacy cache format - elif isinstance(past_key_values, (tuple, list)): + # Exception 1: code path for models using the legacy cache format + if isinstance(past_key_values, (tuple, list)): past_key_values = self._reorder_cache(past_key_values, beam_idx) - # Exception 3: models with different cache formats. These are limited to `DynamicCache` until their + # Exception 2: models with different cache formats. These are limited to `DynamicCache` until their # cache format is standardized, to avoid adding complexity to the codebase. elif "bloom" in model_class or "gptbigcode" in model_class: if not isinstance(past_key_values, DynamicCache): @@ -3738,13 +3685,8 @@ def _assisted_decoding( model_kwargs = self._get_initial_cache_position(input_ids, model_kwargs) # This is needed if return_dict_in_generate is True - if model_kwargs.get("past_key_values", None) is not None: - if isinstance(model_kwargs["past_key_values"], DynamicCache) and len(model_kwargs["past_key_values"]) == 0: - start_from_empty_dynamic_cache = True - elif ( - isinstance(model_kwargs["past_key_values"][0], DynamicCache) - and len(model_kwargs["past_key_values"][0]) == 0 - ): + if isinstance(model_kwargs.get("past_key_values", None), DynamicCache): + if len(model_kwargs["past_key_values"]) == 0: start_from_empty_dynamic_cache = True else: start_from_empty_dynamic_cache = False @@ -4063,14 +4005,9 @@ def _split(data, full_batch_size: int, split_size: int = None): return [None] * (full_batch_size // split_size) if isinstance(data, torch.Tensor): return [data[i : i + split_size] for i in range(0, full_batch_size, split_size)] - # New cache format (tuple of Cache for encoder-decoder models) + # New cache format elif isinstance(data, DynamicCache): return data.batch_split(full_batch_size, split_size) - elif isinstance(data[0], DynamicCache): - self_attn = data[0].batch_split(full_batch_size, split_size) - cross_attn = data[1].batch_split(full_batch_size, split_size) - split = tuple((self_attn[i], cross_attn[i]) for i in range(0, full_batch_size // split_size)) - return split elif isinstance(data, tuple): # If the elements of the tuple are also tuples (e.g., past_key_values in our earlier example) if isinstance(data[0], tuple): @@ -4177,9 +4114,6 @@ def _concat(data): # New cache format elif isinstance(data[0], DynamicCache): return DynamicCache.from_batch_splits(data) - elif isinstance(data[0][0], DynamicCache): - self_attn, cross_attn = list(zip(*data)) - return (DynamicCache.from_batch_splits(self_attn), DynamicCache.from_batch_splits(cross_attn)) elif isinstance(data[0], tuple): # If the elements of the tuple are also tuples (e.g., past_key_values in our earlier example) if isinstance(data[0][0], tuple): From 2ab28f34d2f3bf047b97ffc87ee5caff65095db6 Mon Sep 17 00:00:00 2001 From: raushan Date: Fri, 14 Jun 2024 16:53:21 +0200 Subject: [PATCH 04/40] modify docstring --- .../models/bloom/modeling_bloom.py | 56 +++++++++++-------- .../models/codegen/modeling_codegen.py | 20 ++++--- .../models/falcon/modeling_falcon.py | 21 +++---- src/transformers/models/git/modeling_git.py | 35 +++++++----- .../models/gpt_neo/modeling_gpt_neo.py | 28 ++++++---- .../models/gpt_neox/modeling_gpt_neox.py | 30 ++++++---- src/transformers/models/gptj/modeling_gptj.py | 24 +++++--- src/transformers/models/opt/modeling_opt.py | 14 ++--- 8 files changed, 135 insertions(+), 93 deletions(-) diff --git a/src/transformers/models/bloom/modeling_bloom.py b/src/transformers/models/bloom/modeling_bloom.py index 1fd3a7da0b72..b4a619404c8a 100644 --- a/src/transformers/models/bloom/modeling_bloom.py +++ b/src/transformers/models/bloom/modeling_bloom.py @@ -24,8 +24,8 @@ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, LayerNorm, MSELoss from torch.nn import functional as F -from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...cache_utils import Cache, DynamicCache +from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_attn_mask_utils import _prepare_4d_causal_attention_mask from ...modeling_outputs import ( BaseModelOutputWithPastAndCrossAttentions, @@ -251,7 +251,7 @@ def forward( residual: torch.Tensor, alibi: torch.Tensor, attention_mask: torch.Tensor, - layer_past: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + layer_past: Optional[Cache] = None, head_mask: Optional[torch.Tensor] = None, use_cache: bool = False, output_attentions: bool = False, @@ -266,7 +266,7 @@ def forward( query_layer = query_layer.transpose(1, 2).reshape(batch_size * self.num_heads, q_length, self.head_dim) key_layer = key_layer.transpose(1, 2).reshape(batch_size * self.num_heads, q_length, self.head_dim) value_layer = value_layer.transpose(1, 2).reshape(batch_size * self.num_heads, q_length, self.head_dim) - + if layer_past is not None: key_layer, value_layer = layer_past.update(key_layer, value_layer, self.layer_idx) @@ -381,7 +381,7 @@ def forward( hidden_states: torch.Tensor, alibi: torch.Tensor, attention_mask: torch.Tensor, - layer_past: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + layer_past: Optional[Cache] = None, head_mask: Optional[torch.Tensor] = None, use_cache: bool = False, output_attentions: bool = False, @@ -472,18 +472,22 @@ def _convert_to_standard_cache( num_heads = batch_size_times_num_heads // batch_size if use_legacy_cache: past_key_value = tuple( - ( - layer_past[0].view(batch_size, num_heads, seq_length, head_dim), - layer_past[1].view(batch_size, num_heads, seq_length, head_dim), + ( + layer_past[0].view(batch_size, num_heads, seq_length, head_dim), + layer_past[1].view(batch_size, num_heads, seq_length, head_dim), + ) + for layer_past in past_key_value ) - for layer_past in past_key_value - ) else: # in-place in case of DynamicCache, because we don't assume cache will become a new object after `prepare_input_for_generation` for layer_idx in range(len(past_key_value)): - past_key_value.key_cache[layer_idx] = past_key_value.key_cache[layer_idx].view(batch_size, num_heads, seq_length, head_dim) - past_key_value.value_cache[layer_idx] = past_key_value.value_cache[layer_idx].view(batch_size, num_heads, seq_length, head_dim) - return past_key_value + past_key_value.key_cache[layer_idx] = past_key_value.key_cache[layer_idx].view( + batch_size, num_heads, seq_length, head_dim + ) + past_key_value.value_cache[layer_idx] = past_key_value.value_cache[layer_idx].view( + batch_size, num_heads, seq_length, head_dim + ) + return past_key_value @staticmethod def _convert_to_bloom_cache( @@ -506,9 +510,13 @@ def _convert_to_bloom_cache( else: # in-place in case of DynamicCache, because we don't assume cache will become a new object after `prepare_input_for_generation` for layer_idx in range(len(past_key_value)): - past_key_value.key_cache[layer_idx] = past_key_value.key_cache[layer_idx].view(batch_size_times_num_heads, seq_length, head_dim) - past_key_value.value_cache[layer_idx] = past_key_value.value_cache[layer_idx].view(batch_size_times_num_heads, seq_length, head_dim) - return past_key_value + past_key_value.key_cache[layer_idx] = past_key_value.key_cache[layer_idx].view( + batch_size_times_num_heads, seq_length, head_dim + ) + past_key_value.value_cache[layer_idx] = past_key_value.value_cache[layer_idx].view( + batch_size_times_num_heads, seq_length, head_dim + ) + return past_key_value BLOOM_START_DOCSTRING = r""" @@ -539,7 +547,7 @@ def _convert_to_bloom_cache( [`PreTrainedTokenizer.__call__`] for details. [What are input IDs?](../glossary#input-ids) - past_key_values (`Tuple[Tuple[torch.Tensor]]` of length `config.n_layers`): + past_key_values (`Cache` or `Tuple[Tuple[torch.Tensor]]` of length `config.n_layers`): Contains precomputed hidden-states (key and values in the attention blocks) as computed by the model (see `past_key_values` output below). Can be used to speed up sequential decoding. The `input_ids` which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -625,7 +633,7 @@ def set_input_embeddings(self, new_embeddings: torch.Tensor): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None, + past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor, torch.Tensor], ...]]] = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.LongTensor] = None, @@ -691,7 +699,7 @@ def forward( all_hidden_states = () if output_hidden_states else None # Compute alibi tensor: check build_alibi_tensor documentation - seq_length_with_past = seq_length + past_length + seq_length_with_past = seq_length + past_length if attention_mask is None: attention_mask = torch.ones((batch_size, seq_length_with_past), device=hidden_states.device) else: @@ -751,7 +759,9 @@ def forward( next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache if not return_dict: - return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attentions] if v is not None) + return tuple( + v for v in [hidden_states, next_cache, all_hidden_states, all_self_attentions] if v is not None + ) return BaseModelOutputWithPastAndCrossAttentions( last_hidden_state=hidden_states, @@ -788,7 +798,7 @@ def set_output_embeddings(self, new_embeddings: torch.Tensor): def prepare_inputs_for_generation( self, input_ids: torch.LongTensor, - past_key_values: Optional[torch.Tensor] = None, + past_key_values: Optional[Cache] = None, attention_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, **kwargs, @@ -844,7 +854,7 @@ def prepare_inputs_for_generation( def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None, + past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor, torch.Tensor], ...]]] = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, @@ -974,7 +984,7 @@ def __init__(self, config: BloomConfig): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None, + past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor, torch.Tensor], ...]]] = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, @@ -1111,7 +1121,7 @@ def __init__(self, config: BloomConfig): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None, + past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor, torch.Tensor], ...]]] = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, diff --git a/src/transformers/models/codegen/modeling_codegen.py b/src/transformers/models/codegen/modeling_codegen.py index 2a58c2873656..a6c61b99594c 100644 --- a/src/transformers/models/codegen/modeling_codegen.py +++ b/src/transformers/models/codegen/modeling_codegen.py @@ -158,7 +158,7 @@ def _attn( def forward( self, hidden_states: Optional[torch.FloatTensor], - layer_past: Optional[Tuple[torch.Tensor]] = None, + layer_past: Optional[Cache] = None, attention_mask: Optional[torch.FloatTensor] = None, position_ids: Optional[torch.LongTensor] = None, head_mask: Optional[torch.FloatTensor] = None, @@ -261,7 +261,7 @@ def __init__(self, config, layer_idx=None): def forward( self, hidden_states: Optional[torch.FloatTensor], - layer_past: Optional[Tuple[torch.Tensor]] = None, + layer_past: Optional[Cache] = None, attention_mask: Optional[torch.FloatTensor] = None, position_ids: Optional[torch.LongTensor] = None, head_mask: Optional[torch.FloatTensor] = None, @@ -376,6 +376,10 @@ def _init_weights(self, module): Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert *input_ids* indices into associated vectors than the model's internal embedding lookup matrix. + past_key_values (`Cache` or `Tuple[Tuple[torch.Tensor]]` of length `config.num_layers`): + Contains precomputed hidden-states (key and values in the attention blocks) as computed by the model (see + `past_key_values` output below). Can be used to speed up sequential decoding. The `input_ids` which have + their past given to this model should not be passed as `input_ids` as they have already been computed. output_attentions (`bool`, *optional*): Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned tensors for more detail. @@ -423,7 +427,7 @@ def set_input_embeddings(self, new_embeddings): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor]]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, @@ -563,9 +567,11 @@ def forward( next_cache = None if use_cache: next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache - + if not return_dict: - return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attentions] if v is not None) + return tuple( + v for v in [hidden_states, next_cache, all_hidden_states, all_self_attentions] if v is not None + ) return BaseModelOutputWithPast( last_hidden_state=hidden_states, @@ -600,6 +606,7 @@ def set_output_embeddings(self, new_embeddings): def prepare_inputs_for_generation(self, input_ids, inputs_embeds=None, past_key_values=None, **kwargs): token_type_ids = kwargs.get("token_type_ids", None) + attention_mask = kwargs.get("attention_mask", None) past_length = 0 # Omit tokens covered by past_key_values if past_key_values: @@ -625,7 +632,6 @@ def prepare_inputs_for_generation(self, input_ids, inputs_embeds=None, past_key_ ): attention_mask = attention_mask[:, -max_cache_length:] - attention_mask = kwargs.get("attention_mask", None) position_ids = kwargs.get("position_ids", None) if attention_mask is not None and position_ids is None: @@ -661,7 +667,7 @@ def prepare_inputs_for_generation(self, input_ids, inputs_embeds=None, past_key_ def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor]]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, diff --git a/src/transformers/models/falcon/modeling_falcon.py b/src/transformers/models/falcon/modeling_falcon.py index 450a8e44922e..a9f9cf68462e 100644 --- a/src/transformers/models/falcon/modeling_falcon.py +++ b/src/transformers/models/falcon/modeling_falcon.py @@ -396,7 +396,7 @@ def forward( alibi: Optional[torch.Tensor], attention_mask: torch.Tensor, position_ids: Optional[torch.LongTensor] = None, - layer_past: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + layer_past: Optional[Cache] = None, head_mask: Optional[torch.Tensor] = None, use_cache: bool = False, output_attentions: bool = False, @@ -549,7 +549,7 @@ def forward( alibi: Optional[torch.Tensor], attention_mask: torch.Tensor, position_ids: Optional[torch.LongTensor] = None, - layer_past: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + layer_past: Optional[Cache] = None, head_mask: Optional[torch.Tensor] = None, use_cache: bool = False, output_attentions: bool = False, @@ -783,7 +783,7 @@ def forward( alibi: Optional[torch.Tensor], attention_mask: torch.Tensor, position_ids: Optional[torch.LongTensor] = None, - layer_past: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + layer_past: Optional[Union[Cache, Tuple[torch.Tensor, torch.Tensor]]] = None, head_mask: Optional[torch.Tensor] = None, use_cache: bool = False, output_attentions: bool = False, @@ -872,7 +872,7 @@ def forward( [`PreTrainedTokenizer.__call__`] for details. [What are input IDs?](../glossary#input-ids) - past_key_values (`Tuple[Tuple[torch.Tensor]]` of length `config.num_hidden_layers`): + past_key_values (`Cache` or `Tuple[Tuple[torch.Tensor]]` of length `config.num_hidden_layers`): Contains precomputed hidden-states (key and values in the attention blocks) as computed by the model (see `past_key_values` output below). Can be used to speed up sequential decoding. The `input_ids` which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -1015,7 +1015,7 @@ def set_input_embeddings(self, new_embeddings: torch.Tensor): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None, + past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor, torch.Tensor], ...]]] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, head_mask: Optional[torch.LongTensor] = None, @@ -1176,9 +1176,11 @@ def forward( next_cache = None if use_cache: next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache - + if not return_dict: - return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attentions] if v is not None) + return tuple( + v for v in [hidden_states, next_cache, all_hidden_states, all_self_attentions] if v is not None + ) return BaseModelOutputWithPastAndCrossAttentions( last_hidden_state=hidden_states, @@ -1212,7 +1214,7 @@ def set_output_embeddings(self, new_embeddings: torch.Tensor): def prepare_inputs_for_generation( self, input_ids: torch.LongTensor, - past_key_values: Optional[torch.Tensor] = None, + past_key_values: Optional[Union[Cache, torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, @@ -1240,7 +1242,6 @@ def prepare_inputs_for_generation( ): attention_mask = attention_mask[:, -max_cache_length:] - # Note: versions of Falcon with alibi do not use position_ids. It is used with RoPE. if not self.transformer.use_alibi and attention_mask is not None and position_ids is None: # create position_ids on the fly for batch generation @@ -1273,7 +1274,7 @@ def prepare_inputs_for_generation( def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None, + past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor, torch.Tensor], ...]]] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, diff --git a/src/transformers/models/git/modeling_git.py b/src/transformers/models/git/modeling_git.py index 27e4f58a55e9..b98dcb9b4749 100644 --- a/src/transformers/models/git/modeling_git.py +++ b/src/transformers/models/git/modeling_git.py @@ -169,7 +169,7 @@ def forward( hidden_states: torch.Tensor, attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[Cache] = None, output_attentions: Optional[bool] = False, pixel_values_present: Optional[bool] = False, ) -> Tuple[torch.Tensor]: @@ -178,9 +178,11 @@ def forward( cutoff = self.image_patch_tokens if pixel_values_present else 0 key_layer = self.transpose_for_scores(self.key(hidden_states)) value_layer = self.transpose_for_scores(self.value(hidden_states)) - if past_key_value is not None: + if past_key_value is not None: # NOTE: like in other caches, we store the text component. In GIT it means we discard the image component. - key_layer_past, value_layer_past = past_key_value.update(key_layer[:, :, cutoff:, :], value_layer[:, :, cutoff:, :], self.layer_idx) + key_layer_past, value_layer_past = past_key_value.update( + key_layer[:, :, cutoff:, :], value_layer[:, :, cutoff:, :], self.layer_idx + ) key_layer = torch.cat([key_layer[:, :, :cutoff, :], key_layer_past], dim=2) value_layer = torch.cat([value_layer[:, :, :cutoff, :], value_layer_past], dim=2) @@ -293,7 +295,7 @@ def forward( hidden_states: torch.Tensor, attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[Cache] = None, output_attentions: Optional[bool] = False, pixel_values_present: Optional[bool] = False, ) -> Tuple[torch.Tensor]: @@ -355,7 +357,7 @@ def forward( hidden_states: torch.Tensor, attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[Cache] = None, output_attentions: Optional[bool] = False, pixel_values_present: Optional[bool] = False, ) -> Tuple[torch.Tensor]: @@ -403,7 +405,7 @@ def forward( hidden_states: torch.Tensor, attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.FloatTensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, @@ -567,6 +569,10 @@ def _init_weights(self, module): Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert `input_ids` indices into associated vectors than the model's internal embedding lookup matrix. + past_key_values (`Cache` or `Tuple[Tuple[torch.Tensor]]` of length `config.num_layers`): + Contains precomputed hidden-states (key and values in the attention blocks) as computed by the model (see + `past_key_values` output below). Can be used to speed up sequential decoding. The `input_ids` which have + their past given to this model should not be passed as `input_ids` as they have already been computed. output_attentions (`bool`, *optional*): Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned tensors for more detail. @@ -1134,14 +1140,14 @@ def forward( pixel_values: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPooling]: r""" - past_key_values (`tuple(tuple(torch.FloatTensor))` of length `config.n_layers` with each tuple having 4 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): + past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))` of length `config.n_layers` with each tuple having 4 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): Contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that @@ -1195,7 +1201,11 @@ def forward( # past_key_values_length past_key_values_length = 0 if past_key_values is not None: - past_key_values_length = past_key_values[0][0].shape[2] if not isinstance(past_key_values, Cache) else past_key_values.get_seq_length() + past_key_values_length = ( + past_key_values[0][0].shape[2] + if not isinstance(past_key_values, Cache) + else past_key_values.get_seq_length() + ) # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head @@ -1327,7 +1337,7 @@ def forward( head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.Tensor]] = None, + past_key_values: Optional[Union[Cache, List[torch.Tensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, @@ -1338,7 +1348,7 @@ def forward( Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in `[-100, 0, ..., config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are ignored (masked), the loss is only computed for the tokens with labels n `[0, ..., config.vocab_size]` - past_key_values (`tuple(tuple(torch.FloatTensor))` of length `config.n_layers` with each tuple having 4 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): + past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))` of length `config.n_layers` with each tuple having 4 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): Contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that @@ -1522,8 +1532,7 @@ def prepare_inputs_for_generation( ): # cut decoder_input_ids if past_key_values is used if past_key_values is not None: - past_length = cache_length = past_key_values.get_seq_length() - max_cache_length = past_key_values.get_max_length() + past_length = past_key_values.get_seq_length() # Some generation methods already pass only the last input ID if input_ids.shape[1] > past_length: diff --git a/src/transformers/models/gpt_neo/modeling_gpt_neo.py b/src/transformers/models/gpt_neo/modeling_gpt_neo.py index e9205ce80f4b..f00b25f8ada3 100755 --- a/src/transformers/models/gpt_neo/modeling_gpt_neo.py +++ b/src/transformers/models/gpt_neo/modeling_gpt_neo.py @@ -486,12 +486,14 @@ def __init__(self, config, layer_idx=None): "lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` " "when creating this class." ) - layer_id = 0 # keep 0 for BC when used to get attention_type + layer_id = 0 # keep 0 for BC when used to get attention_type self.attention_layers = config.attention_layers self.attention_type = self.attention_layers[layer_id] if self.attention_type in ["global", "local"]: - self.attention = GPT_NEO_ATTENTION_CLASSES[config._attn_implementation](config, self.attention_type, layer_idx) + self.attention = GPT_NEO_ATTENTION_CLASSES[config._attn_implementation]( + config, self.attention_type, layer_idx + ) else: raise NotImplementedError( "Only attn layer types 'global' and 'local' exist, but got `config.attention_layers`: " @@ -647,7 +649,7 @@ def _init_weights(self, module): [`PreTrainedTokenizer.__call__`] for details. [What are input IDs?](../glossary#input-ids) - past_key_values (`Tuple[Tuple[torch.Tensor]]` of length `config.num_layers`): + past_key_values (`Cache` or `Tuple[Tuple[torch.Tensor]]` of length `config.num_layers`): Contains precomputed hidden-states (key and values in the attention blocks) as computed by the model (see `past_key_values` output below). Can be used to speed up sequential decoding. The `input_ids` which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -733,7 +735,7 @@ def set_input_embeddings(self, new_embeddings): def forward( self, input_ids: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[torch.FloatTensor]] = None, + past_key_values: Optional[Union[Cache, Tuple[torch.FloatTensor]]] = None, attention_mask: Optional[torch.Tensor] = None, token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, @@ -773,7 +775,7 @@ def forward( if use_legacy_cache: past_key_values = DynamicCache.from_legacy_cache(past_key_values) past_length = past_key_values.get_seq_length() - + if position_ids is None: position_ids = torch.arange(past_length, input_shape[-1] + past_length, dtype=torch.long, device=device) position_ids = position_ids.unsqueeze(0) @@ -856,9 +858,11 @@ def forward( next_cache = None if use_cache: next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache - + if not return_dict: - return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attentions] if v is not None) + return tuple( + v for v in [hidden_states, next_cache, all_hidden_states, all_self_attentions] if v is not None + ) return BaseModelOutputWithPast( last_hidden_state=hidden_states, @@ -894,6 +898,7 @@ def set_output_embeddings(self, new_embeddings): def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_embeds=None, **kwargs): token_type_ids = kwargs.get("token_type_ids", None) + attention_mask = kwargs.get("attention_mask", None) past_length = 0 # Omit tokens covered by past_key_values if past_key_values is not None: @@ -911,7 +916,7 @@ def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_ if token_type_ids is not None: token_type_ids = token_type_ids[:, -input_ids.shape[1] :] - + # If we are about to go beyond the maximum cache length, we need to crop the input attention mask. if ( max_cache_length is not None @@ -920,7 +925,6 @@ def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_ ): attention_mask = attention_mask[:, -max_cache_length:] - attention_mask = kwargs.get("attention_mask", None) position_ids = kwargs.get("position_ids", None) if attention_mask is not None and position_ids is None: @@ -957,7 +961,7 @@ def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_ def forward( self, input_ids: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[torch.FloatTensor]] = None, + past_key_values: Optional[Union[Cache, Tuple[torch.FloatTensor]]] = None, attention_mask: Optional[torch.Tensor] = None, token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, @@ -1073,7 +1077,7 @@ def __init__(self, config): def forward( self, input_ids: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[torch.FloatTensor]] = None, + past_key_values: Optional[Union[Cache, Tuple[torch.FloatTensor]]] = None, attention_mask: Optional[torch.Tensor] = None, token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, @@ -1197,7 +1201,7 @@ def __init__(self, config): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor]]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, diff --git a/src/transformers/models/gpt_neox/modeling_gpt_neox.py b/src/transformers/models/gpt_neox/modeling_gpt_neox.py index 8a167af5e949..d6dc66849769 100755 --- a/src/transformers/models/gpt_neox/modeling_gpt_neox.py +++ b/src/transformers/models/gpt_neox/modeling_gpt_neox.py @@ -168,7 +168,7 @@ def forward( attention_mask: torch.FloatTensor, position_ids: torch.LongTensor, head_mask: Optional[torch.FloatTensor] = None, - layer_past: Optional[Tuple[torch.Tensor]] = None, + layer_past: Optional[Cache] = None, use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, padding_mask: Optional[torch.Tensor] = None, @@ -204,7 +204,7 @@ def forward( "with a layer index." ) seq_len += layer_past.get_seq_length(self.layer_idx) - + cos, sin = self.rotary_emb(value, seq_len=seq_len) query, key = apply_rotary_pos_emb(query_rot, key_rot, cos, sin, position_ids) query = torch.cat((query, query_pass), dim=-1) @@ -326,7 +326,7 @@ def forward( attention_mask: torch.FloatTensor, position_ids: torch.LongTensor, head_mask: Optional[torch.FloatTensor] = None, - layer_past: Optional[Tuple[torch.Tensor]] = None, + layer_past: Optional[Cache] = None, use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, ): @@ -695,7 +695,7 @@ def forward( position_ids: Optional[torch.LongTensor] = None, head_mask: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = False, - layer_past: Optional[Tuple[torch.Tensor]] = None, + layer_past: Optional[Cache] = None, output_attentions: Optional[bool] = False, ): attention_layer_outputs = self.attention( @@ -776,6 +776,10 @@ def forward( Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert *input_ids* indices into associated vectors than the model's internal embedding lookup matrix. + past_key_values (`Cache` or `Tuple[Tuple[torch.Tensor]]` of length `config.num_layers`): + Contains precomputed hidden-states (key and values in the attention blocks) as computed by the model (see + `past_key_values` output below). Can be used to speed up sequential decoding. The `input_ids` which have + their past given to this model should not be passed as `input_ids` as they have already been computed. output_attentions (`bool`, *optional*): Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned tensors for more detail. @@ -827,14 +831,14 @@ def forward( position_ids: Optional[torch.LongTensor] = None, head_mask: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.FloatTensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, ) -> Union[Tuple, BaseModelOutputWithPast]: r""" - past_key_values (`tuple(tuple(torch.FloatTensor))` of length `config.n_layers` with each tuple having 4 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): + past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))` of length `config.n_layers` with each tuple having 4 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): Contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -918,7 +922,9 @@ def forward( next_decoder_cache = None all_attentions = () if output_attentions else None all_hidden_states = () if output_hidden_states else None - for i, layer in enumerate(self.layers,): + for i, layer in enumerate( + self.layers, + ): if output_hidden_states: all_hidden_states = all_hidden_states + (hidden_states,) @@ -957,7 +963,7 @@ def forward( next_cache = None if use_cache: next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache - + if not return_dict: return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_attentions] if v is not None) @@ -999,7 +1005,7 @@ def forward( position_ids: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.FloatTensor]]]] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1007,7 +1013,7 @@ def forward( return_dict: Optional[bool] = None, ) -> Union[Tuple, CausalLMOutputWithPast]: r""" - past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape `(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`. The two additional tensors are @@ -1187,7 +1193,7 @@ def forward( position_ids: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.FloatTensor]]]] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1299,7 +1305,7 @@ def __init__(self, config): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor]]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, diff --git a/src/transformers/models/gptj/modeling_gptj.py b/src/transformers/models/gptj/modeling_gptj.py index 4553490e2b9a..12896b806894 100644 --- a/src/transformers/models/gptj/modeling_gptj.py +++ b/src/transformers/models/gptj/modeling_gptj.py @@ -219,7 +219,7 @@ def _get_embed_positions(self, position_ids): def forward( self, hidden_states: torch.FloatTensor, - layer_past: Optional[Tuple[torch.Tensor]] = None, + layer_past: Optional[Cache] = None, attention_mask: Optional[torch.FloatTensor] = None, position_ids: Optional[torch.LongTensor] = None, head_mask: Optional[torch.FloatTensor] = None, @@ -305,7 +305,7 @@ def __init__(self, *args, **kwargs): def forward( self, hidden_states: torch.FloatTensor, - layer_past: Optional[Tuple[torch.Tensor]] = None, + layer_past: Optional[Cache] = None, attention_mask: Optional[torch.FloatTensor] = None, position_ids: Optional[torch.LongTensor] = None, head_mask: Optional[torch.FloatTensor] = None, @@ -560,7 +560,7 @@ def __init__(self, config, layer_idx=None): def forward( self, hidden_states: Optional[torch.FloatTensor], - layer_past: Optional[Tuple[torch.Tensor]] = None, + layer_past: Optional[Cache] = None, attention_mask: Optional[torch.FloatTensor] = None, position_ids: Optional[torch.LongTensor] = None, head_mask: Optional[torch.FloatTensor] = None, @@ -677,6 +677,10 @@ def _init_weights(self, module): Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert *input_ids* indices into associated vectors than the model's internal embedding lookup matrix. + past_key_values (`Cache` or `Tuple[Tuple[torch.Tensor]]` of length `config.num_layers`): + Contains precomputed hidden-states (key and values in the attention blocks) as computed by the model (see + `past_key_values` output below). Can be used to speed up sequential decoding. The `input_ids` which have + their past given to this model should not be passed as `input_ids` as they have already been computed. output_attentions (`bool`, *optional*): Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned tensors for more detail. @@ -819,7 +823,7 @@ def set_input_embeddings(self, new_embeddings): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor]]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, @@ -920,7 +924,7 @@ def forward( # Model parallel if self.model_parallel: torch.cuda.set_device(hidden_states.device) - + # Ensure layer_past is on same device as hidden_states (might not be correct) if past_key_values is not None: past_key_values.key_cache = past_key_values.key_cache.to(hidden_states.device) @@ -981,7 +985,9 @@ def forward( next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache if not return_dict: - return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attentions] if v is not None) + return tuple( + v for v in [hidden_states, next_cache, all_hidden_states, all_self_attentions] if v is not None + ) return BaseModelOutputWithPast( last_hidden_state=hidden_states, @@ -1051,6 +1057,7 @@ def set_output_embeddings(self, new_embeddings): def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_embeds=None, **kwargs): token_type_ids = kwargs.get("token_type_ids", None) + attention_mask = kwargs.get("attention_mask", None) past_length = 0 # Omit tokens covered by past_key_values if past_key_values is not None: @@ -1067,7 +1074,7 @@ def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_ input_ids = input_ids[:, remove_prefix_length:] if token_type_ids is not None: token_type_ids = token_type_ids[:, -input_ids.shape[1] :] - + # If we are about to go beyond the maximum cache length, we need to crop the input attention mask. if ( max_cache_length is not None @@ -1076,7 +1083,6 @@ def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_ ): attention_mask = attention_mask[:, -max_cache_length:] - attention_mask = kwargs.get("attention_mask", None) position_ids = kwargs.get("position_ids", None) if attention_mask is not None and position_ids is None: @@ -1114,7 +1120,7 @@ def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_ def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor]]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, diff --git a/src/transformers/models/opt/modeling_opt.py b/src/transformers/models/opt/modeling_opt.py index d77ea618c393..1766e97b8fd8 100644 --- a/src/transformers/models/opt/modeling_opt.py +++ b/src/transformers/models/opt/modeling_opt.py @@ -139,7 +139,7 @@ def forward( self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[Cache] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, @@ -276,7 +276,7 @@ def forward( self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[Cache] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, @@ -495,7 +495,7 @@ def forward( self, hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, - layer_head_mask: Optional[torch.Tensor] = None, + layer_head_mask: Optional[Cache] = None, past_key_value: Optional[Tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, @@ -728,7 +728,7 @@ def forward( input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -960,7 +960,7 @@ def forward( input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1035,7 +1035,7 @@ def forward( input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1066,7 +1066,7 @@ def forward( - 1 indicates the head is **not masked**, - 0 indicates the head is **masked**. - past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape `(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`. The two additional From 5fe4e9e304d8cd3d9a8304da1f1843a18596e2ac Mon Sep 17 00:00:00 2001 From: raushan Date: Fri, 14 Jun 2024 16:53:39 +0200 Subject: [PATCH 05/40] revert bart --- src/transformers/models/bart/modeling_bart.py | 284 +++++++++--------- tests/models/bart/test_modeling_bart.py | 7 +- 2 files changed, 141 insertions(+), 150 deletions(-) diff --git a/src/transformers/models/bart/modeling_bart.py b/src/transformers/models/bart/modeling_bart.py index 10cb8cc65630..e3b2f8a61b28 100755 --- a/src/transformers/models/bart/modeling_bart.py +++ b/src/transformers/models/bart/modeling_bart.py @@ -26,7 +26,6 @@ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACT2FN -from ...cache_utils import Cache, DynamicCache from ...modeling_attn_mask_utils import ( _prepare_4d_attention_mask, _prepare_4d_attention_mask_for_sdpa, @@ -155,7 +154,6 @@ def __init__( is_decoder: bool = False, bias: bool = True, is_causal: bool = False, - layer_idx: int = None, config: Optional[BartConfig] = None, ): super().__init__() @@ -165,14 +163,6 @@ def __init__( self.head_dim = embed_dim // num_heads self.config = config - if layer_idx is None and is_decoder: - logger.warning_once( - f"Instantiating a decoder {self.__class__.__name__} without passing `layer_idx` is not recommended and " - "will to errors during the forward call, if caching is used. Please make sure to provide a `layer_idx` " - "when creating this class." - ) - self.layer_idx = layer_idx - if (self.head_dim * num_heads) != self.embed_dim: raise ValueError( f"embed_dim must be divisible by num_heads (got `embed_dim`: {self.embed_dim}" @@ -194,7 +184,7 @@ def forward( self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Cache] = None, + past_key_value: Optional[Tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, @@ -209,27 +199,42 @@ def forward( # get query proj query_states = self.q_proj(hidden_states) * self.scaling - - if is_cross_attention: - # check that the `sequence_length` of the `past_key_value` is the same as - # the provided `key_value_states` to support prefix tuning - # try to reuse k,v, cross_attentions. Always layer_idx=0 because there's only - # one encoder_hidden_states for all decoder layers - if past_key_value is None: - key_states = self._shape(self.k_proj(key_value_states), -1, bsz) - value_states = self._shape(self.v_proj(key_value_states), -1, bsz) - elif past_key_value.get_seq_length(self.layer_idx) == key_value_states.shape[1]: - key_states = past_key_value.key_cache[self.layer_idx] - value_states = past_key_value.value_cache[self.layer_idx] - else: - key_states = self._shape(self.k_proj(key_value_states), -1, bsz) - value_states = self._shape(self.v_proj(key_value_states), -1, bsz) - key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx) + # get key, value proj + # `past_key_value[0].shape[2] == key_value_states.shape[1]` + # is checking that the `sequence_length` of the `past_key_value` is the same as + # the provided `key_value_states` to support prefix tuning + if ( + is_cross_attention + and past_key_value is not None + and past_key_value[0].shape[2] == key_value_states.shape[1] + ): + # reuse k,v, cross_attentions + key_states = past_key_value[0] + value_states = past_key_value[1] + elif is_cross_attention: + # cross_attentions + key_states = self._shape(self.k_proj(key_value_states), -1, bsz) + value_states = self._shape(self.v_proj(key_value_states), -1, bsz) + elif past_key_value is not None: + # reuse k, v, self_attention + key_states = self._shape(self.k_proj(hidden_states), -1, bsz) + value_states = self._shape(self.v_proj(hidden_states), -1, bsz) + key_states = torch.cat([past_key_value[0], key_states], dim=2) + value_states = torch.cat([past_key_value[1], value_states], dim=2) else: + # self_attention key_states = self._shape(self.k_proj(hidden_states), -1, bsz) value_states = self._shape(self.v_proj(hidden_states), -1, bsz) - if past_key_value is not None: - key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx) + + if self.is_decoder: + # if cross_attention save Tuple(torch.Tensor, torch.Tensor) of all cross attention key/value_states. + # Further calls to cross_attention layer can then reuse all cross-attention + # key/value_states (first "if" case) + # if uni-directional self-attention (decoder) save Tuple(torch.Tensor, torch.Tensor) of + # all previous decoder key/value_states. Further calls to uni-directional self-attention + # can concat previous decoder key/value_states to current projected key/value_states (third "elif" case) + # if encoder bi-directional self-attention `past_key_value` is always `None` + past_key_value = (key_states, value_states) proj_shape = (bsz * self.num_heads, -1, self.head_dim) query_states = self._shape(query_states, tgt_len, bsz).view(*proj_shape) @@ -237,7 +242,6 @@ def forward( value_states = value_states.reshape(*proj_shape) src_len = key_states.size(1) - attn_weights = torch.bmm(query_states, key_states.transpose(1, 2)) if attn_weights.size() != (bsz * self.num_heads, tgt_len, src_len): @@ -313,6 +317,9 @@ def __init__(self, *args, **kwargs): # Beware that with flash_attn<2.1, using q_seqlen != k_seqlen (except for the case q_seqlen == 1) produces a wrong mask (top-left). self._flash_attn_uses_top_left_mask = not is_flash_attn_greater_or_equal_2_10() + def _reshape(self, tensor: torch.Tensor, seq_len: int, bsz: int): + return tensor.view(bsz, seq_len, self.num_heads, self.head_dim) + def forward( self, hidden_states: torch.Tensor, @@ -334,30 +341,46 @@ def forward( # get query proj query_states = self._reshape(self.q_proj(hidden_states), -1, bsz) - - if is_cross_attention: - # check that the `sequence_length` of the `past_key_value` is the same as - # the provided `key_value_states` to support prefix tuning - # try to reuse k,v, cross_attentions. Always layer_idx=0 because there's only - # one encoder_hidden_states for all decoder layers - if past_key_value is None: - key_states = self._shape(self.k_proj(key_value_states), -1, bsz) - value_states = self._shape(self.v_proj(key_value_states), -1, bsz) - elif past_key_value.get_seq_length(self.layer_idx) == key_value_states.shape[1]: - key_states = past_key_value.key_cache[self.layer_idx] - value_states = past_key_value.value_cache[self.layer_idx] - else: - key_states = self._shape(self.k_proj(key_value_states), -1, bsz) - value_states = self._shape(self.v_proj(key_value_states), -1, bsz) - key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx) + # get key, value proj + # `past_key_value[0].shape[2] == key_value_states.shape[1]` + # is checking that the `sequence_length` of the `past_key_value` is the same as + # the provided `key_value_states` to support prefix tuning + if ( + is_cross_attention + and past_key_value is not None + and past_key_value[0].shape[2] == key_value_states.shape[1] + ): + # reuse k,v, cross_attentions + key_states = past_key_value[0].transpose(1, 2) + value_states = past_key_value[1].transpose(1, 2) + elif is_cross_attention: + # cross_attentions + key_states = self._reshape(self.k_proj(key_value_states), -1, bsz) + value_states = self._reshape(self.v_proj(key_value_states), -1, bsz) + elif past_key_value is not None: + # reuse k, v, self_attention + key_states = self._reshape(self.k_proj(hidden_states), -1, bsz) + value_states = self._reshape(self.v_proj(hidden_states), -1, bsz) + key_states = torch.cat([past_key_value[0].transpose(1, 2), key_states], dim=1) + value_states = torch.cat([past_key_value[1].transpose(1, 2), value_states], dim=1) else: - key_states = self._shape(self.k_proj(hidden_states), -1, bsz) - value_states = self._shape(self.v_proj(hidden_states), -1, bsz) - if past_key_value is not None: - key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx) - - key_states = key_states.transpose(1, 2) - value_states = value_states.transpose(1, 2) + # self_attention + key_states = self._reshape(self.k_proj(hidden_states), -1, bsz) + value_states = self._reshape(self.v_proj(hidden_states), -1, bsz) + + if self.is_decoder: + # if cross_attention save Tuple(torch.Tensor, torch.Tensor) of all cross attention key/value_states. + # Further calls to cross_attention layer can then reuse all cross-attention + # key/value_states (first "if" case) + # if uni-directional self-attention (decoder) save Tuple(torch.Tensor, torch.Tensor) of + # all previous decoder key/value_states. Further calls to uni-directional self-attention + # can concat previous decoder key/value_states to current projected key/value_states (third "elif" case) + # if encoder bi-directional self-attention `past_key_value` is always `None` + past_key_value = (key_states.transpose(1, 2), value_states.transpose(1, 2)) + + kv_seq_len = key_states.shape[-2] + if past_key_value is not None: + kv_seq_len += past_key_value[0].shape[-2] # In PEFT, usually we cast the layer norms in float32 for training stability reasons # therefore the input hidden states gets silently casted in float32. Hence, we need @@ -531,27 +554,42 @@ def forward( # get query proj query_states = self.q_proj(hidden_states) - - if is_cross_attention: - # check that the `sequence_length` of the `past_key_value` is the same as - # the provided `key_value_states` to support prefix tuning - # try to reuse k,v, cross_attentions. Always layer_idx=0 because there's only - # one encoder_hidden_states for all decoder layers - if past_key_value is None: - key_states = self._shape(self.k_proj(key_value_states), -1, bsz) - value_states = self._shape(self.v_proj(key_value_states), -1, bsz) - elif past_key_value.get_seq_length(self.layer_idx) == key_value_states.shape[1]: - key_states = past_key_value.key_cache[self.layer_idx] - value_states = past_key_value.value_cache[self.layer_idx] - else: - key_states = self._shape(self.k_proj(key_value_states), -1, bsz) - value_states = self._shape(self.v_proj(key_value_states), -1, bsz) - key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx) + # get key, value proj + # `past_key_value[0].shape[2] == key_value_states.shape[1]` + # is checking that the `sequence_length` of the `past_key_value` is the same as + # the provided `key_value_states` to support prefix tuning + if ( + is_cross_attention + and past_key_value is not None + and past_key_value[0].shape[2] == key_value_states.shape[1] + ): + # reuse k,v, cross_attentions + key_states = past_key_value[0] + value_states = past_key_value[1] + elif is_cross_attention: + # cross_attentions + key_states = self._shape(self.k_proj(key_value_states), -1, bsz) + value_states = self._shape(self.v_proj(key_value_states), -1, bsz) + elif past_key_value is not None: + # reuse k, v, self_attention + key_states = self._shape(self.k_proj(hidden_states), -1, bsz) + value_states = self._shape(self.v_proj(hidden_states), -1, bsz) + key_states = torch.cat([past_key_value[0], key_states], dim=2) + value_states = torch.cat([past_key_value[1], value_states], dim=2) else: + # self_attention key_states = self._shape(self.k_proj(hidden_states), -1, bsz) value_states = self._shape(self.v_proj(hidden_states), -1, bsz) - if past_key_value is not None: - key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx) + + if self.is_decoder: + # if cross_attention save Tuple(torch.Tensor, torch.Tensor) of all cross attention key/value_states. + # Further calls to cross_attention layer can then reuse all cross-attention + # key/value_states (first "if" case) + # if uni-directional self-attention (decoder) save Tuple(torch.Tensor, torch.Tensor) of + # all previous decoder key/value_states. Further calls to uni-directional self-attention + # can concat previous decoder key/value_states to current projected key/value_states (third "elif" case) + # if encoder bi-directional self-attention `past_key_value` is always `None` + past_key_value = (key_states, value_states) query_states = self._shape(query_states, tgt_len, bsz) @@ -605,7 +643,6 @@ def __init__(self, config: BartConfig): num_heads=config.encoder_attention_heads, dropout=config.attention_dropout, config=config, - layer_idx=0, ) self.self_attn_layer_norm = nn.LayerNorm(self.embed_dim) self.dropout = config.dropout @@ -667,10 +704,9 @@ def forward( class BartDecoderLayer(nn.Module): - def __init__(self, config: BartConfig, layer_idx: int = None): + def __init__(self, config: BartConfig): super().__init__() self.embed_dim = config.d_model - self.config = config self.self_attn = BART_ATTENTION_CLASSES[config._attn_implementation]( embed_dim=self.embed_dim, @@ -678,7 +714,6 @@ def __init__(self, config: BartConfig, layer_idx: int = None): dropout=config.attention_dropout, is_decoder=True, is_causal=True, - layer_idx=layer_idx, config=config, ) self.dropout = config.dropout @@ -691,7 +726,6 @@ def __init__(self, config: BartConfig, layer_idx: int = None): config.decoder_attention_heads, dropout=config.attention_dropout, is_decoder=True, - layer_idx=layer_idx, config=config, ) self.encoder_attn_layer_norm = nn.LayerNorm(self.embed_dim) @@ -731,14 +765,11 @@ def forward( """ residual = hidden_states - # decoder self-attention Cache object is at position 0 in Encoder-Decoder setting, otherwise it's simply one Cache object - # cross_attn Cache object is at positions 1 of past_key_value tuple - self_attn_past_key_value = cross_attn_past_key_value = None - if past_key_value is not None: - self_attn_past_key_value = past_key_value[0] if self.config.is_encoder_decoder else past_key_value - cross_attn_past_key_value = past_key_value[1] if self.config.is_encoder_decoder else None - - hidden_states, self_attn_weights, past_key_values = self.self_attn( + # Self Attention + # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 + self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None + # add present self-attn cache to positions 1,2 of present_key_value tuple + hidden_states, self_attn_weights, present_key_value = self.self_attn( hidden_states=hidden_states, past_key_value=self_attn_past_key_value, attention_mask=attention_mask, @@ -750,11 +781,14 @@ def forward( hidden_states = self.self_attn_layer_norm(hidden_states) # Cross-Attention Block + cross_attn_present_key_value = None cross_attn_weights = None if encoder_hidden_states is not None: residual = hidden_states - hidden_states, cross_attn_weights, cross_attn_past_key_value = self.encoder_attn( + # cross_attn cached key/values tuple is at positions 3,4 of present_key_value tuple + cross_attn_past_key_value = past_key_value[-2:] if past_key_value is not None else None + hidden_states, cross_attn_weights, cross_attn_present_key_value = self.encoder_attn( hidden_states=hidden_states, key_value_states=encoder_hidden_states, attention_mask=encoder_attention_mask, @@ -766,9 +800,8 @@ def forward( hidden_states = residual + hidden_states hidden_states = self.encoder_attn_layer_norm(hidden_states) - # add self-attn and cross-attn in a past_key_values tuple, otherwise set to None (not tuple of Nones) - if past_key_values is not None and cross_attn_past_key_value is not None: - past_key_values = (past_key_values, cross_attn_past_key_value) + # add cross-attn to positions 3,4 of present_key_value tuple + present_key_value = present_key_value + cross_attn_present_key_value # Fully Connected residual = hidden_states @@ -785,7 +818,7 @@ def forward( outputs += (self_attn_weights, cross_attn_weights) if use_cache: - outputs += (past_key_values,) + outputs += (present_key_value,) return outputs @@ -823,7 +856,6 @@ class BartPreTrainedModel(PreTrainedModel): _skip_keys_device_placement = "past_key_values" _supports_flash_attn_2 = True _supports_sdpa = True - _supports_cache_class = True def _init_weights(self, module): std = self.config.init_std @@ -1236,7 +1268,7 @@ def __init__(self, config: BartConfig, embed_tokens: Optional[nn.Embedding] = No config.max_position_embeddings, config.d_model, ) - self.layers = nn.ModuleList([BartDecoderLayer(config, i) for i in range(config.decoder_layers)]) + self.layers = nn.ModuleList([BartDecoderLayer(config) for _ in range(config.decoder_layers)]) self._use_flash_attention_2 = config._attn_implementation == "flash_attention_2" self._use_sdpa = config._attn_implementation == "sdpa" @@ -1352,31 +1384,8 @@ def forward( else: raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds") - # past_key_values in encpder-decoder models is a tuple of cache objects - # first element holds past_kv for decoder attn, and the second for cross attn - past_key_values_length = 0 - if use_cache: - use_legacy_cache = ( - not (past_key_values is not None and isinstance(past_key_values[0], Cache)) - if self.config.is_encoder_decoder - else not isinstance(past_key_values, Cache) - ) - if self.config.is_encoder_decoder and use_legacy_cache: - self_attn = cross_attn = None - if past_key_values is not None: - self_attn = [cache[:2] for cache in past_key_values] - cross_attn = [cache[2:] for cache in past_key_values] - past_key_values = ( - DynamicCache.from_legacy_cache(self_attn), - DynamicCache.from_legacy_cache(cross_attn), - ) - elif use_legacy_cache: - past_key_values = DynamicCache.from_legacy_cache(past_key_values) - past_key_values_length = ( - past_key_values.get_seq_length() - if not self.config.is_encoder_decoder - else past_key_values[0].get_seq_length() - ) + # past_key_values_length + past_key_values_length = past_key_values[0][0].shape[2] if past_key_values is not None else 0 if inputs_embeds is None: inputs_embeds = self.embed_tokens(input) @@ -1438,7 +1447,7 @@ def forward( all_hidden_states = () if output_hidden_states else None all_self_attns = () if output_attentions else None all_cross_attentions = () if (output_attentions and encoder_hidden_states is not None) else None - next_decoder_cache = None + next_decoder_cache = () if use_cache else None # check if head_mask/cross_attn_head_mask has a correct number of layers specified if desired for attn_mask, mask_name in zip([head_mask, cross_attn_head_mask], ["head_mask", "cross_attn_head_mask"]): @@ -1458,6 +1467,8 @@ def forward( if dropout_probability < self.layerdrop: continue + past_key_value = past_key_values[idx] if past_key_values is not None else None + if self.gradient_checkpointing and self.training: layer_outputs = self._gradient_checkpointing_func( decoder_layer.__call__, @@ -1481,14 +1492,14 @@ def forward( cross_attn_layer_head_mask=( cross_attn_head_mask[idx] if cross_attn_head_mask is not None else None ), - past_key_value=past_key_values, + past_key_value=past_key_value, output_attentions=output_attentions, use_cache=use_cache, ) hidden_states = layer_outputs[0] if use_cache: - next_decoder_cache = layer_outputs[3 if output_attentions else 1] + next_decoder_cache += (layer_outputs[3 if output_attentions else 1],) if output_attentions: all_self_attns += (layer_outputs[1],) @@ -1500,19 +1511,7 @@ def forward( if output_hidden_states: all_hidden_states += (hidden_states,) - next_cache = None - if use_cache: - if not use_legacy_cache: - next_cache = next_decoder_cache - else: - if self.config.is_encoder_decoder: - num_hidden_layers = len(next_decoder_cache[0].key_cache) - self_attn_cache = next_decoder_cache[0].to_legacy_cache() - cross_attn_cache = next_decoder_cache[1].to_legacy_cache() - next_cache = tuple((*self_attn_cache[i], *cross_attn_cache[i]) for i in range(num_hidden_layers)) - else: - next_cache = next_decoder_cache.to_legacy_cache() - + next_cache = next_decoder_cache if use_cache else None if not return_dict: return tuple( v @@ -1801,13 +1800,9 @@ def prepare_inputs_for_generation( encoder_outputs=None, **kwargs, ): + # cut decoder_input_ids if past_key_values is used if past_key_values is not None: - if isinstance(past_key_values, Cache): # BART as standalone decoder - past_length = past_key_values.get_seq_length() - elif isinstance(past_key_values[0], Cache): # BART in enc-dec setting - past_length = past_key_values[0].get_seq_length() - else: # BART with legacy cache format - past_length = past_key_values[0][0].shape[2] + past_length = past_key_values[0][0].shape[2] # Some generation methods already pass only the last input ID if decoder_input_ids.shape[1] > past_length: @@ -1838,8 +1833,10 @@ def prepare_decoder_input_ids_from_labels(self, labels: torch.Tensor): def _reorder_cache(past_key_values, beam_idx): reordered_past = () for layer_past in past_key_values: + # cached cross_attention states don't have to be reordered -> they are always the same reordered_past += ( - tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past), + tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past[:2]) + + layer_past[2:], ) return reordered_past @@ -2304,13 +2301,8 @@ def prepare_inputs_for_generation( if attention_mask is None: attention_mask = input_ids.new_ones(input_ids.shape) - if past_key_values is not None: - if isinstance(past_key_values, Cache): # BART as standalone decoder - past_length = past_key_values.get_seq_length() - elif isinstance(past_key_values[0], Cache): # BART in enc-dec setting - past_length = past_key_values[0].get_seq_length() - else: # BART with legacy cache format - past_length = past_key_values[0][0].shape[2] + if past_key_values: + past_length = past_key_values[0][0].shape[2] # Some generation methods already pass only the last input ID if input_ids.shape[1] > past_length: diff --git a/tests/models/bart/test_modeling_bart.py b/tests/models/bart/test_modeling_bart.py index 342260872d82..ba9e112c186e 100644 --- a/tests/models/bart/test_modeling_bart.py +++ b/tests/models/bart/test_modeling_bart.py @@ -174,7 +174,6 @@ def prepare_config_and_inputs_for_common(self): def create_and_check_decoder_model_past_large_inputs(self, config, inputs_dict): model = BartModel(config=config).get_decoder().to(torch_device).eval() - model.config.is_encoder_decoder = False input_ids = inputs_dict["input_ids"] attention_mask = inputs_dict["attention_mask"] head_mask = inputs_dict["head_mask"] @@ -1462,9 +1461,9 @@ def create_and_check_decoder_model_attention_mask_past( # get two different outputs output_from_no_past = model(next_input_ids, attention_mask=attn_mask)["last_hidden_state"] - output_from_past = model( - next_tokens, attention_mask=attn_mask, past_key_values=past_key_values, use_cache=True - )["last_hidden_state"] + output_from_past = model(next_tokens, attention_mask=attn_mask, past_key_values=past_key_values)[ + "last_hidden_state" + ] # select random slice random_slice_idx = ids_tensor((1,), output_from_past.shape[-1]).item() From 09413c30c9e56e06e905a2f854258127567bc2f7 Mon Sep 17 00:00:00 2001 From: raushan Date: Fri, 14 Jun 2024 16:56:36 +0200 Subject: [PATCH 06/40] minor fixes --- src/transformers/generation/utils.py | 1 - src/transformers/models/git/modeling_git.py | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/transformers/generation/utils.py b/src/transformers/generation/utils.py index 61fb897bd9c4..c68190908925 100644 --- a/src/transformers/generation/utils.py +++ b/src/transformers/generation/utils.py @@ -1,4 +1,3 @@ - # coding=utf-8 # Copyright 2020 The Google AI Language Team Authors, Facebook AI Research authors and The HuggingFace Inc. team. # Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. diff --git a/src/transformers/models/git/modeling_git.py b/src/transformers/models/git/modeling_git.py index b98dcb9b4749..f0c55c815cc7 100644 --- a/src/transformers/models/git/modeling_git.py +++ b/src/transformers/models/git/modeling_git.py @@ -262,7 +262,6 @@ def forward(self, hidden_states: torch.Tensor, input_tensor: torch.Tensor) -> to class GitAttention(nn.Module): - # Copied from transformers.models.bert.modeling_bert.BertAttention.__init__ with Bert->Git,BERT->GIT def __init__(self, config, position_embedding_type=None, layer_idx=None): super().__init__() self.self = GIT_SELF_ATTENTION_CLASSES[config._attn_implementation]( @@ -393,7 +392,6 @@ def feed_forward_chunk(self, attention_output): class GitEncoder(nn.Module): - # Copied from transformers.models.bert.modeling_bert.BertEncoder.__init__ with Bert->Git def __init__(self, config): super().__init__() self.config = config From 3c276043e89946c993506f235b83c0f0f9d59115 Mon Sep 17 00:00:00 2001 From: raushan Date: Fri, 14 Jun 2024 16:56:52 +0200 Subject: [PATCH 07/40] fix copies (not related) --- src/transformers/models/data2vec/modeling_data2vec_audio.py | 1 + src/transformers/models/hubert/modeling_hubert.py | 1 + src/transformers/models/musicgen/modeling_musicgen.py | 1 + .../models/musicgen_melody/modeling_musicgen_melody.py | 1 + src/transformers/models/sew/modeling_sew.py | 1 + src/transformers/models/unispeech/modeling_unispeech.py | 1 + src/transformers/models/unispeech_sat/modeling_unispeech_sat.py | 1 + 7 files changed, 7 insertions(+) diff --git a/src/transformers/models/data2vec/modeling_data2vec_audio.py b/src/transformers/models/data2vec/modeling_data2vec_audio.py index aaa1dd274f11..b37e3f5b9543 100755 --- a/src/transformers/models/data2vec/modeling_data2vec_audio.py +++ b/src/transformers/models/data2vec/modeling_data2vec_audio.py @@ -622,6 +622,7 @@ def _flash_attention_forward( """ Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token first unpad the input, then computes the attention scores and pad the final attention scores. + Args: query_states (`torch.Tensor`): Input query states to be passed to Flash Attention API diff --git a/src/transformers/models/hubert/modeling_hubert.py b/src/transformers/models/hubert/modeling_hubert.py index 4c22ca8f2b61..f76a3f3caf9d 100755 --- a/src/transformers/models/hubert/modeling_hubert.py +++ b/src/transformers/models/hubert/modeling_hubert.py @@ -692,6 +692,7 @@ def _flash_attention_forward( """ Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token first unpad the input, then computes the attention scores and pad the final attention scores. + Args: query_states (`torch.Tensor`): Input query states to be passed to Flash Attention API diff --git a/src/transformers/models/musicgen/modeling_musicgen.py b/src/transformers/models/musicgen/modeling_musicgen.py index 792cf937f320..4d55431d5e9a 100644 --- a/src/transformers/models/musicgen/modeling_musicgen.py +++ b/src/transformers/models/musicgen/modeling_musicgen.py @@ -453,6 +453,7 @@ def _flash_attention_forward( """ Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token first unpad the input, then computes the attention scores and pad the final attention scores. + Args: query_states (`torch.Tensor`): Input query states to be passed to Flash Attention API diff --git a/src/transformers/models/musicgen_melody/modeling_musicgen_melody.py b/src/transformers/models/musicgen_melody/modeling_musicgen_melody.py index 2b6ff1b6be78..a7cdbc76774b 100644 --- a/src/transformers/models/musicgen_melody/modeling_musicgen_melody.py +++ b/src/transformers/models/musicgen_melody/modeling_musicgen_melody.py @@ -469,6 +469,7 @@ def _flash_attention_forward( """ Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token first unpad the input, then computes the attention scores and pad the final attention scores. + Args: query_states (`torch.Tensor`): Input query states to be passed to Flash Attention API diff --git a/src/transformers/models/sew/modeling_sew.py b/src/transformers/models/sew/modeling_sew.py index 55df2d5bfc71..8746de8fc556 100644 --- a/src/transformers/models/sew/modeling_sew.py +++ b/src/transformers/models/sew/modeling_sew.py @@ -692,6 +692,7 @@ def _flash_attention_forward( """ Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token first unpad the input, then computes the attention scores and pad the final attention scores. + Args: query_states (`torch.Tensor`): Input query states to be passed to Flash Attention API diff --git a/src/transformers/models/unispeech/modeling_unispeech.py b/src/transformers/models/unispeech/modeling_unispeech.py index a16fffb87e80..50db686e48db 100755 --- a/src/transformers/models/unispeech/modeling_unispeech.py +++ b/src/transformers/models/unispeech/modeling_unispeech.py @@ -728,6 +728,7 @@ def _flash_attention_forward( """ Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token first unpad the input, then computes the attention scores and pad the final attention scores. + Args: query_states (`torch.Tensor`): Input query states to be passed to Flash Attention API diff --git a/src/transformers/models/unispeech_sat/modeling_unispeech_sat.py b/src/transformers/models/unispeech_sat/modeling_unispeech_sat.py index 9a5783abc3d3..e9d2350b34af 100755 --- a/src/transformers/models/unispeech_sat/modeling_unispeech_sat.py +++ b/src/transformers/models/unispeech_sat/modeling_unispeech_sat.py @@ -745,6 +745,7 @@ def _flash_attention_forward( """ Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token first unpad the input, then computes the attention scores and pad the final attention scores. + Args: query_states (`torch.Tensor`): Input query states to be passed to Flash Attention API From 350acc5a1c89a0b17fbeb20bdc18e31edc456cbb Mon Sep 17 00:00:00 2001 From: raushan Date: Fri, 14 Jun 2024 17:10:16 +0200 Subject: [PATCH 08/40] revert tests --- tests/generation/test_utils.py | 53 ++++++++-------------------------- 1 file changed, 12 insertions(+), 41 deletions(-) diff --git a/tests/generation/test_utils.py b/tests/generation/test_utils.py index 3b9584f0c198..1981f5a63919 100644 --- a/tests/generation/test_utils.py +++ b/tests/generation/test_utils.py @@ -1101,7 +1101,7 @@ def test_beam_search_low_memory(self): self.assertListEqual(low_output.tolist(), high_output.tolist()) @parameterized.expand([("random",), ("same",)]) - # @is_flaky() # Read NOTE (1) below. If there are API issues, all attempts will fail. + @is_flaky() # Read NOTE (1) below. If there are API issues, all attempts will fail. def test_assisted_decoding_matches_greedy_search(self, assistant_type): # This test ensures that the assisted generation does not introduce output changes over greedy search. # NOTE (1): The sentence above is true most of the time, there is a tiny difference in the logits due to matmul @@ -1623,31 +1623,19 @@ def test_new_cache_format(self, num_beams, do_sample): set_seed(seed) legacy_results = model.generate(input_ids, attention_mask=attention_mask, **generation_kwargs) set_seed(seed) - past_key_values = (DynamicCache(), DynamicCache()) if model.config.is_encoder_decoder else DynamicCache() new_results = model.generate( - input_ids, attention_mask=attention_mask, past_key_values=past_key_values, **generation_kwargs + input_ids, attention_mask=attention_mask, past_key_values=DynamicCache(), **generation_kwargs ) # The two sets of generated sequences must match, despite the cache format between forward passes being # different self.assertListEqual(legacy_results.sequences.tolist(), new_results.sequences.tolist()) self.assertTrue(isinstance(legacy_results.past_key_values, tuple)) - if not model.config.is_encoder_decoder: - self.assertTrue(isinstance(new_results.past_key_values, DynamicCache)) - else: - self.assertTrue(isinstance(new_results.past_key_values[0], DynamicCache)) + self.assertTrue(isinstance(new_results.past_key_values, DynamicCache)) # The contents of the two caches, when converted to the same format (in both directions!), must match legacy_cache = legacy_results.past_key_values - if model.config.is_encoder_decoder: - num_hidden_layers = len(new_results.past_key_values[0].key_cache) - self_attn_cache = new_results.past_key_values[0].to_legacy_cache() - cross_attn_cache = new_results.past_key_values[1].to_legacy_cache() - new_cache_converted = tuple( - (*self_attn_cache[i], *cross_attn_cache[i]) for i in range(num_hidden_layers) - ) - else: - new_cache_converted = new_results.past_key_values.to_legacy_cache() + new_cache_converted = new_results.past_key_values.to_legacy_cache() for layer_idx in range(len(legacy_cache)): for kv_idx in range(len(legacy_cache[layer_idx])): self.assertTrue( @@ -1658,32 +1646,15 @@ def test_new_cache_format(self, num_beams, do_sample): ) new_cache = new_results.past_key_values - if model.config.is_encoder_decoder: - self_attn = [cache[:2] for cache in legacy_results.past_key_values] - cross_attn = [cache[2:] for cache in legacy_results.past_key_values] - legacy_cache_converted = ( - DynamicCache.from_legacy_cache(self_attn), - DynamicCache.from_legacy_cache(cross_attn), - ) - for cache_obj in range(len(new_cache)): - for layer_idx in range(len(new_cache[cache_obj])): - for kv_idx in range(len(new_cache[cache_obj][layer_idx])): - self.assertTrue( - torch.allclose( - new_cache[cache_obj][layer_idx][kv_idx], - legacy_cache_converted[cache_obj][layer_idx][kv_idx], - ) - ) - else: - legacy_cache_converted = DynamicCache.from_legacy_cache(legacy_results.past_key_values) - for layer_idx in range(len(new_cache)): - for kv_idx in range(len(new_cache[layer_idx])): - self.assertTrue( - torch.allclose( - new_cache[layer_idx][kv_idx], - legacy_cache_converted[layer_idx][kv_idx], - ) + legacy_cache_converted = DynamicCache.from_legacy_cache(legacy_results.past_key_values) + for layer_idx in range(len(new_cache)): + for kv_idx in range(len(new_cache[layer_idx])): + self.assertTrue( + torch.allclose( + new_cache[layer_idx][kv_idx], + legacy_cache_converted[layer_idx][kv_idx], ) + ) @require_quanto def test_generate_with_quant_cache(self): From c0adf10d49e12d3e6c65be0062bfc2aa14c8b487 Mon Sep 17 00:00:00 2001 From: raushan Date: Mon, 17 Jun 2024 08:55:44 +0200 Subject: [PATCH 09/40] remove enc-dec related code --- .../generation/candidate_generator.py | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/transformers/generation/candidate_generator.py b/src/transformers/generation/candidate_generator.py index 3fc4add67c7b..2dafbada69d2 100644 --- a/src/transformers/generation/candidate_generator.py +++ b/src/transformers/generation/candidate_generator.py @@ -367,19 +367,16 @@ def _crop_past_key_values(model, past_key_values, maximum_length): """Crops the past key values up to a certain maximum length.""" new_past = [] if model.config.is_encoder_decoder: - if isinstance(past_key_values[0], DynamicCache): - past_key_values[0].crop(maximum_length) - else: - for idx in range(len(past_key_values)): - new_past.append( - ( - past_key_values[idx][0][:, :, :maximum_length, :], - past_key_values[idx][1][:, :, :maximum_length, :], - past_key_values[idx][2], - past_key_values[idx][3], - ) + for idx in range(len(past_key_values)): + new_past.append( + ( + past_key_values[idx][0][:, :, :maximum_length, :], + past_key_values[idx][1][:, :, :maximum_length, :], + past_key_values[idx][2], + past_key_values[idx][3], ) - past_key_values = tuple(new_past) + ) + past_key_values = tuple(new_past) # gptbigcode is special elif "gptbigcode" in model.__class__.__name__.lower() or ( model.config.architectures is not None and "gptbigcode" in model.config.architectures[0].lower() From c18b17751455bc340d079263f641197330d35caa Mon Sep 17 00:00:00 2001 From: raushan Date: Mon, 17 Jun 2024 09:01:32 +0200 Subject: [PATCH 10/40] remove bloom --- .../generation/candidate_generator.py | 19 +- .../models/bloom/modeling_bloom.py | 180 +++++++----------- 2 files changed, 87 insertions(+), 112 deletions(-) diff --git a/src/transformers/generation/candidate_generator.py b/src/transformers/generation/candidate_generator.py index 2dafbada69d2..ccbbb412bd1c 100644 --- a/src/transformers/generation/candidate_generator.py +++ b/src/transformers/generation/candidate_generator.py @@ -377,7 +377,19 @@ def _crop_past_key_values(model, past_key_values, maximum_length): ) ) past_key_values = tuple(new_past) - # gptbigcode is special + # bloom is special + elif "bloom" in model.__class__.__name__.lower() or ( + model.config.architectures is not None and "bloom" in model.config.architectures[0].lower() + ): + for idx in range(len(past_key_values)): + new_past.append( + ( + past_key_values[idx][0][:, :, :maximum_length], + past_key_values[idx][1][:, :maximum_length, :], + ) + ) + past_key_values = tuple(new_past) + # gptbigcode is too elif "gptbigcode" in model.__class__.__name__.lower() or ( model.config.architectures is not None and "gptbigcode" in model.config.architectures[0].lower() ): @@ -389,12 +401,13 @@ def _crop_past_key_values(model, past_key_values, maximum_length): past_key_values[idx] = past_key_values[idx][:, :, :maximum_length, :] elif isinstance(past_key_values, DynamicCache): past_key_values.crop(maximum_length) + elif past_key_values is not None: for idx in range(len(past_key_values)): new_past.append( ( - past_key_values[idx][0][..., :maximum_length, :], - past_key_values[idx][1][..., :maximum_length, :], + past_key_values[idx][0][:, :, :maximum_length, :], + past_key_values[idx][1][:, :, :maximum_length, :], ) ) past_key_values = tuple(new_past) diff --git a/src/transformers/models/bloom/modeling_bloom.py b/src/transformers/models/bloom/modeling_bloom.py index b4a619404c8a..e8ae2e7bdf68 100644 --- a/src/transformers/models/bloom/modeling_bloom.py +++ b/src/transformers/models/bloom/modeling_bloom.py @@ -24,7 +24,6 @@ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, LayerNorm, MSELoss from torch.nn import functional as F -from ...cache_utils import Cache, DynamicCache from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward from ...modeling_attn_mask_utils import _prepare_4d_causal_attention_mask from ...modeling_outputs import ( @@ -171,7 +170,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: class BloomAttention(nn.Module): - def __init__(self, config: BloomConfig, layer_idx=None): + def __init__(self, config: BloomConfig): super().__init__() self.pretraining_tp = config.pretraining_tp @@ -192,13 +191,6 @@ def __init__(self, config: BloomConfig, layer_idx=None): # Layer-wise attention scaling self.inv_norm_factor = 1.0 / math.sqrt(self.head_dim) self.beta = 1.0 - self.layer_idx = layer_idx - if layer_idx is None: - logger.warning_once( - f"Instantiating {self.__class__.__name__} without passing a `layer_idx` is not recommended and will " - "lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` " - "when creating this class." - ) self.query_key_value = nn.Linear(self.hidden_size, 3 * self.hidden_size, bias=True) self.dense = nn.Linear(self.hidden_size, self.hidden_size) @@ -251,7 +243,7 @@ def forward( residual: torch.Tensor, alibi: torch.Tensor, attention_mask: torch.Tensor, - layer_past: Optional[Cache] = None, + layer_past: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, head_mask: Optional[torch.Tensor] = None, use_cache: bool = False, output_attentions: bool = False, @@ -264,16 +256,23 @@ def forward( batch_size, q_length, _, _ = query_layer.shape query_layer = query_layer.transpose(1, 2).reshape(batch_size * self.num_heads, q_length, self.head_dim) - key_layer = key_layer.transpose(1, 2).reshape(batch_size * self.num_heads, q_length, self.head_dim) + key_layer = key_layer.permute(0, 2, 3, 1).reshape(batch_size * self.num_heads, self.head_dim, q_length) value_layer = value_layer.transpose(1, 2).reshape(batch_size * self.num_heads, q_length, self.head_dim) - if layer_past is not None: - key_layer, value_layer = layer_past.update(key_layer, value_layer, self.layer_idx) + past_key, past_value = layer_past + # concatenate along seq_length dimension: + # - key: [batch_size * self.num_heads, head_dim, kv_length] + # - value: [batch_size * self.num_heads, kv_length, head_dim] + key_layer = torch.cat((past_key, key_layer), dim=2) + value_layer = torch.cat((past_value, value_layer), dim=1) - # transpose to get key: [batch_size * self.num_heads, head_dim, kv_length] - key_layer = key_layer.transpose(1, 2) _, _, kv_length = key_layer.shape + if use_cache is True: + present = (key_layer, value_layer) + else: + present = None + # [batch_size * num_heads, q_length, kv_length] # we use `torch.Tensor.baddbmm` instead of `torch.baddbmm` as the latter isn't supported by TorchScript v1.11 matmul_result = alibi.baddbmm( @@ -323,7 +322,7 @@ def forward( output_tensor = dropout_add(output_tensor, residual, self.hidden_dropout, self.training) - outputs = (output_tensor, layer_past) + outputs = (output_tensor, present) if output_attentions: outputs += (attention_probs,) @@ -362,13 +361,13 @@ def forward(self, hidden_states: torch.Tensor, residual: torch.Tensor) -> torch. class BloomBlock(nn.Module): - def __init__(self, config: BloomConfig, layer_idx=None): + def __init__(self, config: BloomConfig): super().__init__() hidden_size = config.hidden_size self.input_layernorm = LayerNorm(hidden_size, eps=config.layer_norm_epsilon) self.num_heads = config.n_head - self.self_attention = BloomAttention(config, layer_idx) + self.self_attention = BloomAttention(config) self.post_attention_layernorm = LayerNorm(hidden_size, eps=config.layer_norm_epsilon) self.mlp = BloomMLP(config) @@ -381,7 +380,7 @@ def forward( hidden_states: torch.Tensor, alibi: torch.Tensor, attention_mask: torch.Tensor, - layer_past: Optional[Cache] = None, + layer_past: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, head_mask: Optional[torch.Tensor] = None, use_cache: bool = False, output_attentions: bool = False, @@ -429,7 +428,7 @@ def forward( else: outputs = (output,) + outputs[1:] - return outputs # hidden_states, past_kv, attentions + return outputs # hidden_states, present, attentions class BloomPreTrainedModel(PreTrainedModel): @@ -438,7 +437,6 @@ class BloomPreTrainedModel(PreTrainedModel): supports_gradient_checkpointing = True _no_split_modules = ["BloomBlock"] _skip_keys_device_placement = "past_key_values" - _supports_cache_class = True def __init__(self, *inputs, **kwargs): super().__init__(*inputs, **kwargs) @@ -467,27 +465,17 @@ def _convert_to_standard_cache( Standardizes the format of the cache so as to match most implementations, i.e. to tuple(tuple([batch_size, num_heads, ...])) """ - use_legacy_cache = not isinstance(past_key_value, Cache) - batch_size_times_num_heads, seq_length, head_dim = past_key_value[0][0].shape + batch_size_times_num_heads, head_dim, seq_length = past_key_value[0][0].shape num_heads = batch_size_times_num_heads // batch_size - if use_legacy_cache: - past_key_value = tuple( - ( - layer_past[0].view(batch_size, num_heads, seq_length, head_dim), - layer_past[1].view(batch_size, num_heads, seq_length, head_dim), - ) - for layer_past in past_key_value + # key: [batch_size * num_heads, head_dim, seq_length] -> [batch_size, num_heads, head_dim, seq_length] + # value: [batch_size * num_heads, seq_length, head_dim] -> [batch_size, num_heads, seq_length, head_dim] + return tuple( + ( + layer_past[0].view(batch_size, num_heads, head_dim, seq_length), + layer_past[1].view(batch_size, num_heads, seq_length, head_dim), ) - else: - # in-place in case of DynamicCache, because we don't assume cache will become a new object after `prepare_input_for_generation` - for layer_idx in range(len(past_key_value)): - past_key_value.key_cache[layer_idx] = past_key_value.key_cache[layer_idx].view( - batch_size, num_heads, seq_length, head_dim - ) - past_key_value.value_cache[layer_idx] = past_key_value.value_cache[layer_idx].view( - batch_size, num_heads, seq_length, head_dim - ) - return past_key_value + for layer_past in past_key_value + ) @staticmethod def _convert_to_bloom_cache( @@ -496,27 +484,17 @@ def _convert_to_bloom_cache( """ Converts the cache to the format expected by Bloom, i.e. to tuple(tuple([batch_size * num_heads, ...])) """ - use_legacy_cache = not isinstance(past_key_value, Cache) - batch_size, num_heads, seq_length, head_dim = past_key_value[0][0].shape + batch_size, num_heads, head_dim, seq_length = past_key_value[0][0].shape batch_size_times_num_heads = batch_size * num_heads - if use_legacy_cache: - past_key_value = tuple( - ( - layer_past[0].view(batch_size_times_num_heads, seq_length, head_dim), - layer_past[1].view(batch_size_times_num_heads, seq_length, head_dim), - ) - for layer_past in past_key_value + # key: [batch_size, num_heads, head_dim, seq_length] -> [batch_size * num_heads, head_dim, seq_length] + # value: [batch_size, num_heads, seq_length, head_dim] -> [batch_size * num_heads, seq_length, head_dim] + return tuple( + ( + layer_past[0].view(batch_size_times_num_heads, head_dim, seq_length), + layer_past[1].view(batch_size_times_num_heads, seq_length, head_dim), ) - else: - # in-place in case of DynamicCache, because we don't assume cache will become a new object after `prepare_input_for_generation` - for layer_idx in range(len(past_key_value)): - past_key_value.key_cache[layer_idx] = past_key_value.key_cache[layer_idx].view( - batch_size_times_num_heads, seq_length, head_dim - ) - past_key_value.value_cache[layer_idx] = past_key_value.value_cache[layer_idx].view( - batch_size_times_num_heads, seq_length, head_dim - ) - return past_key_value + for layer_past in past_key_value + ) BLOOM_START_DOCSTRING = r""" @@ -547,7 +525,7 @@ def _convert_to_bloom_cache( [`PreTrainedTokenizer.__call__`] for details. [What are input IDs?](../glossary#input-ids) - past_key_values (`Cache` or `Tuple[Tuple[torch.Tensor]]` of length `config.n_layers`): + past_key_values (`Tuple[Tuple[torch.Tensor]]` of length `config.n_layers`): Contains precomputed hidden-states (key and values in the attention blocks) as computed by the model (see `past_key_values` output below). Can be used to speed up sequential decoding. The `input_ids` which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -605,7 +583,7 @@ def __init__(self, config: BloomConfig): self.word_embeddings_layernorm = LayerNorm(self.embed_dim, eps=config.layer_norm_epsilon) # Transformer blocks - self.h = nn.ModuleList([BloomBlock(config, layer_idx=i) for i in range(config.num_hidden_layers)]) + self.h = nn.ModuleList([BloomBlock(config) for _ in range(config.num_hidden_layers)]) # Final Layer Norm self.ln_f = LayerNorm(self.embed_dim, eps=config.layer_norm_epsilon) @@ -633,7 +611,7 @@ def set_input_embeddings(self, new_embeddings: torch.Tensor): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor, torch.Tensor], ...]]] = None, + past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.LongTensor] = None, @@ -669,19 +647,8 @@ def forward( else: raise ValueError("You have to specify either input_ids or inputs_embeds") - if self.gradient_checkpointing and self.training: - if use_cache: - logger.warning_once( - "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." - ) - use_cache = False - - past_length = 0 - if use_cache: - use_legacy_cache = not isinstance(past_key_values, Cache) - if use_legacy_cache: - past_key_values = DynamicCache.from_legacy_cache(past_key_values) - past_length = past_key_values.get_seq_length() + if past_key_values is None: + past_key_values = tuple([None] * len(self.h)) # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head @@ -694,12 +661,23 @@ def forward( hidden_states = self.word_embeddings_layernorm(inputs_embeds) - next_decoder_cache = None + presents = () if use_cache else None all_self_attentions = () if output_attentions else None all_hidden_states = () if output_hidden_states else None + if self.gradient_checkpointing and self.training: + if use_cache: + logger.warning_once( + "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." + ) + use_cache = False + # Compute alibi tensor: check build_alibi_tensor documentation - seq_length_with_past = seq_length + past_length + seq_length_with_past = seq_length + past_key_values_length = 0 + if past_key_values[0] is not None: + past_key_values_length = past_key_values[0][0].shape[2] + seq_length_with_past = seq_length_with_past + past_key_values_length if attention_mask is None: attention_mask = torch.ones((batch_size, seq_length_with_past), device=hidden_states.device) else: @@ -711,11 +689,11 @@ def forward( attention_mask, input_shape=(batch_size, seq_length), inputs_embeds=inputs_embeds, - past_key_values_length=past_length, + past_key_values_length=past_key_values_length, ) causal_mask = causal_mask.bool() - for i, block in enumerate(self.h): + for i, (block, layer_past) in enumerate(zip(self.h, past_key_values)): if output_hidden_states: all_hidden_states = all_hidden_states + (hidden_states,) @@ -725,7 +703,7 @@ def forward( hidden_states, alibi, causal_mask, - past_key_values, + layer_past, head_mask[i], use_cache, output_attentions, @@ -733,7 +711,7 @@ def forward( else: outputs = block( hidden_states, - layer_past=past_key_values, + layer_past=layer_past, attention_mask=causal_mask, head_mask=head_mask[i], use_cache=use_cache, @@ -742,8 +720,8 @@ def forward( ) hidden_states = outputs[0] - if use_cache: - next_decoder_cache = outputs[1] + if use_cache is True: + presents = presents + (outputs[1],) if output_attentions: all_self_attentions = all_self_attentions + (outputs[2 if use_cache else 1],) @@ -754,18 +732,12 @@ def forward( if output_hidden_states: all_hidden_states = all_hidden_states + (hidden_states,) - next_cache = None - if use_cache: - next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache - if not return_dict: - return tuple( - v for v in [hidden_states, next_cache, all_hidden_states, all_self_attentions] if v is not None - ) + return tuple(v for v in [hidden_states, presents, all_hidden_states, all_self_attentions] if v is not None) return BaseModelOutputWithPastAndCrossAttentions( last_hidden_state=hidden_states, - past_key_values=next_cache, + past_key_values=presents, hidden_states=all_hidden_states, attentions=all_self_attentions, ) @@ -798,16 +770,14 @@ def set_output_embeddings(self, new_embeddings: torch.Tensor): def prepare_inputs_for_generation( self, input_ids: torch.LongTensor, - past_key_values: Optional[Cache] = None, + past_key_values: Optional[torch.Tensor] = None, attention_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, **kwargs, ) -> dict: # only last tokens for input_ids if past is not None - past_length = 0 if past_key_values is not None: - past_length = cache_length = past_key_values.get_seq_length() - max_cache_length = past_key_values.get_max_length() + past_length = past_key_values[0][0].shape[2] # Some generation methods already pass only the last input ID if input_ids.shape[1] > past_length: @@ -818,20 +788,12 @@ def prepare_inputs_for_generation( input_ids = input_ids[:, remove_prefix_length:] - # If we are about to go beyond the maximum cache length, we need to crop the input attention mask. - if ( - max_cache_length is not None - and attention_mask is not None - and cache_length + input_ids.shape[1] > max_cache_length - ): - attention_mask = attention_mask[:, -max_cache_length:] - # the cache may be in the stardard format (e.g. in contrastive search), convert to bloom's format if needed - if past_length != 0 and past_key_values[0][0].shape[0] == input_ids.shape[0]: + if past_key_values[0][0].shape[0] == input_ids.shape[0]: past_key_values = self._convert_to_bloom_cache(past_key_values) # if `inputs_embeds` are passed, we only want to use them in the 1st generation step - if inputs_embeds is not None and past_length == 0: + if inputs_embeds is not None and past_key_values is None: model_inputs = {"inputs_embeds": inputs_embeds} else: model_inputs = {"input_ids": input_ids} @@ -854,7 +816,7 @@ def prepare_inputs_for_generation( def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor, torch.Tensor], ...]]] = None, + past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, @@ -984,7 +946,7 @@ def __init__(self, config: BloomConfig): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor, torch.Tensor], ...]]] = None, + past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, @@ -1045,7 +1007,7 @@ def forward( sequence_lengths = sequence_lengths.to(logits.device) else: sequence_lengths = -1 - logger.warning( + logger.warning_once( f"{self.__class__.__name__} will not detect padding tokens in `inputs_embeds`. Results may be " "unexpected if using padding tokens in conjunction with `inputs_embeds.`" ) @@ -1121,7 +1083,7 @@ def __init__(self, config: BloomConfig): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor, torch.Tensor], ...]]] = None, + past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, From 582f289d3733e277e9305e7b2ead4eb2e3006fa7 Mon Sep 17 00:00:00 2001 From: raushan Date: Mon, 17 Jun 2024 09:09:51 +0200 Subject: [PATCH 11/40] remove opt (enc-dec) --- src/transformers/models/opt/modeling_opt.py | 33 ++++++++------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/src/transformers/models/opt/modeling_opt.py b/src/transformers/models/opt/modeling_opt.py index 1766e97b8fd8..b2f7d2490e21 100644 --- a/src/transformers/models/opt/modeling_opt.py +++ b/src/transformers/models/opt/modeling_opt.py @@ -23,7 +23,6 @@ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACT2FN -from ...cache_utils import Cache, DynamicCache from ...modeling_attn_mask_utils import _prepare_4d_causal_attention_mask from ...modeling_outputs import ( BaseModelOutputWithPast, @@ -139,7 +138,7 @@ def forward( self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Cache] = None, + past_key_value: Optional[Tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, @@ -276,7 +275,7 @@ def forward( self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Cache] = None, + past_key_value: Optional[Tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, @@ -495,7 +494,7 @@ def forward( self, hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, - layer_head_mask: Optional[Cache] = None, + layer_head_mask: Optional[torch.Tensor] = None, past_key_value: Optional[Tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, @@ -642,7 +641,7 @@ def _init_weights(self, module): - 1 indicates the head is **not masked**, - 0 indicates the head is **masked**. - past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape `(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`. @@ -728,7 +727,7 @@ def forward( input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[List[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -758,7 +757,7 @@ def forward( - 1 indicates the head is **not masked**, - 0 indicates the head is **masked**. - past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of @@ -805,13 +804,7 @@ def forward( inputs_embeds = self.embed_tokens(input_ids) batch_size, seq_length = input_shape - past_key_values_length = 0 - if use_cache: - use_legacy_cache = not isinstance(past_key_values, Cache) - if use_legacy_cache: - past_key_values = DynamicCache.from_legacy_cache(past_key_values) - past_key_values_length = past_key_values.get_seq_length() - + past_key_values_length = past_key_values[0][0].shape[2] if past_key_values is not None else 0 # required mask seq length can be calculated via length of past mask_seq_length = past_key_values_length + seq_length @@ -915,9 +908,7 @@ def forward( if output_hidden_states: all_hidden_states += (hidden_states,) - next_cache = None - if use_cache: - next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache + next_cache = next_decoder_cache if use_cache else None if not return_dict: return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None) return BaseModelOutputWithPast( @@ -960,7 +951,7 @@ def forward( input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[List[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1035,7 +1026,7 @@ def forward( input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[List[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1066,7 +1057,7 @@ def forward( - 1 indicates the head is **not masked**, - 0 indicates the head is **masked**. - past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape `(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`. The two additional @@ -1284,7 +1275,7 @@ def forward( sequence_lengths = sequence_lengths.to(logits.device) else: sequence_lengths = -1 - logger.warning( + logger.warning_once( f"{self.__class__.__name__} will not detect padding tokens in `inputs_embeds`. Results may be " "unexpected if using padding tokens in conjunction with `inputs_embeds.`" ) From 33d54b49f2ce7c4795de14187d44437809cbac05 Mon Sep 17 00:00:00 2001 From: raushan Date: Tue, 18 Jun 2024 07:35:16 +0200 Subject: [PATCH 12/40] update docstring --- .../models/codegen/modeling_codegen.py | 21 ++++++-- .../models/falcon/modeling_falcon.py | 25 ++++++---- src/transformers/models/git/modeling_git.py | 21 ++++++-- .../models/gpt_neo/modeling_gpt_neo.py | 48 +++++++++++-------- .../models/gpt_neox/modeling_gpt_neox.py | 21 ++++++-- src/transformers/models/gptj/modeling_gptj.py | 21 ++++++-- 6 files changed, 112 insertions(+), 45 deletions(-) diff --git a/src/transformers/models/codegen/modeling_codegen.py b/src/transformers/models/codegen/modeling_codegen.py index a6c61b99594c..f596035ae730 100644 --- a/src/transformers/models/codegen/modeling_codegen.py +++ b/src/transformers/models/codegen/modeling_codegen.py @@ -376,10 +376,23 @@ def _init_weights(self, module): Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert *input_ids* indices into associated vectors than the model's internal embedding lookup matrix. - past_key_values (`Cache` or `Tuple[Tuple[torch.Tensor]]` of length `config.num_layers`): - Contains precomputed hidden-states (key and values in the attention blocks) as computed by the model (see - `past_key_values` output below). Can be used to speed up sequential decoding. The `input_ids` which have - their past given to this model should not be passed as `input_ids` as they have already been computed. + past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*): + Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention + blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values` + returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`. + + Two formats are allowed: + - a [`~cache_utils.Cache`] instance; + - Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of + shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy + cache format. + + The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the + legacy cache format will be returned. + + If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't + have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids` + of shape `(batch_size, sequence_length)`. output_attentions (`bool`, *optional*): Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned tensors for more detail. diff --git a/src/transformers/models/falcon/modeling_falcon.py b/src/transformers/models/falcon/modeling_falcon.py index 1a5ff2e9f945..4c4ae3a29d9d 100644 --- a/src/transformers/models/falcon/modeling_falcon.py +++ b/src/transformers/models/falcon/modeling_falcon.py @@ -872,14 +872,23 @@ def forward( [`PreTrainedTokenizer.__call__`] for details. [What are input IDs?](../glossary#input-ids) - past_key_values (`Cache` or `Tuple[Tuple[torch.Tensor]]` of length `config.num_hidden_layers`): - Contains precomputed hidden-states (key and values in the attention blocks) as computed by the model (see - `past_key_values` output below). Can be used to speed up sequential decoding. The `input_ids` which have - their past given to this model should not be passed as `input_ids` as they have already been computed. - - Each element of `past_key_values` is a tuple (past_key, past_value): - - past_key: [batch_size * num_heads, head_dim, kv_length] - - past_value: [batch_size * num_heads, kv_length, head_dim] + past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*): + Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention + blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values` + returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`. + + Two formats are allowed: + - a [`~cache_utils.Cache`] instance; + - Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of + shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy + cache format. + + The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the + legacy cache format will be returned. + + If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't + have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids` + of shape `(batch_size, sequence_length)`. attention_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`: diff --git a/src/transformers/models/git/modeling_git.py b/src/transformers/models/git/modeling_git.py index f0c55c815cc7..ef9ac733909e 100644 --- a/src/transformers/models/git/modeling_git.py +++ b/src/transformers/models/git/modeling_git.py @@ -567,10 +567,23 @@ def _init_weights(self, module): Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert `input_ids` indices into associated vectors than the model's internal embedding lookup matrix. - past_key_values (`Cache` or `Tuple[Tuple[torch.Tensor]]` of length `config.num_layers`): - Contains precomputed hidden-states (key and values in the attention blocks) as computed by the model (see - `past_key_values` output below). Can be used to speed up sequential decoding. The `input_ids` which have - their past given to this model should not be passed as `input_ids` as they have already been computed. + past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*): + Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention + blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values` + returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`. + + Two formats are allowed: + - a [`~cache_utils.Cache`] instance; + - Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of + shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy + cache format. + + The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the + legacy cache format will be returned. + + If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't + have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids` + of shape `(batch_size, sequence_length)`. output_attentions (`bool`, *optional*): Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned tensors for more detail. diff --git a/src/transformers/models/gpt_neo/modeling_gpt_neo.py b/src/transformers/models/gpt_neo/modeling_gpt_neo.py index 5fa866255e15..06f62097df4a 100755 --- a/src/transformers/models/gpt_neo/modeling_gpt_neo.py +++ b/src/transformers/models/gpt_neo/modeling_gpt_neo.py @@ -165,7 +165,7 @@ def load_tf_weights_in_gpt_neo(model, config, gpt_neo_checkpoint_path): class GPTNeoSelfAttention(nn.Module): - def __init__(self, config, attention_type, layer_idx=None): + def __init__(self, config, attention_type, layer_id=None): super().__init__() self.config = config @@ -186,7 +186,7 @@ def __init__(self, config, attention_type, layer_idx=None): self.attn_dropout = nn.Dropout(float(config.attention_dropout)) self.resid_dropout = nn.Dropout(float(config.resid_dropout)) self.is_causal = True - self.layer_idx = layer_idx + self.layer_id = layer_id self.embed_dim = config.hidden_size self.num_heads = config.num_heads @@ -267,7 +267,7 @@ def forward( value = self._split_heads(value, self.num_heads, self.head_dim) if layer_past is not None: - key, value = layer_past.update(key, value, self.layer_idx) + key, value = layer_past.update(key, value, self.layer_id) attn_output, attn_weights = self._attn(query, key, value, attention_mask, head_mask) @@ -318,7 +318,7 @@ def forward( value = self._split_heads(value, self.num_heads, self.head_dim) if layer_past is not None: - key, value = layer_past.update(key, value, self.layer_idx) + key, value = layer_past.update(key, value, self.layer_id) query_length = query.shape[2] tgt_len = key.shape[2] @@ -477,22 +477,15 @@ def _upad_input(self, query_layer, key_layer, value_layer, attention_mask, query class GPTNeoAttention(nn.Module): - def __init__(self, config, layer_idx=None): + def __init__(self, config, layer_id=0): super().__init__() - self.layer_idx = layer_id = layer_idx - if layer_idx is None: - logger.warning_once( - f"Instantiating {self.__class__.__name__} without passing a `layer_idx` is not recommended and will " - "lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` " - "when creating this class." - ) - layer_id = 0 # keep 0 for BC when used to get attention_type + self.layer_id = layer_id self.attention_layers = config.attention_layers self.attention_type = self.attention_layers[layer_id] if self.attention_type in ["global", "local"]: self.attention = GPT_NEO_ATTENTION_CLASSES[config._attn_implementation]( - config, self.attention_type, layer_idx + config, self.attention_type, layer_id ) else: raise NotImplementedError( @@ -537,12 +530,12 @@ def forward(self, hidden_states): class GPTNeoBlock(nn.Module): - def __init__(self, config, layer_idx=None): + def __init__(self, config, layer_id=None): super().__init__() hidden_size = config.hidden_size inner_dim = config.intermediate_size if config.intermediate_size is not None else 4 * hidden_size self.ln_1 = nn.LayerNorm(hidden_size, eps=config.layer_norm_epsilon) - self.attn = GPTNeoAttention(config, layer_idx) + self.attn = GPTNeoAttention(config, layer_id) self.ln_2 = nn.LayerNorm(hidden_size, eps=config.layer_norm_epsilon) self.mlp = GPTNeoMLP(inner_dim, config) @@ -649,10 +642,23 @@ def _init_weights(self, module): [`PreTrainedTokenizer.__call__`] for details. [What are input IDs?](../glossary#input-ids) - past_key_values (`Cache` or `Tuple[Tuple[torch.Tensor]]` of length `config.num_layers`): - Contains precomputed hidden-states (key and values in the attention blocks) as computed by the model (see - `past_key_values` output below). Can be used to speed up sequential decoding. The `input_ids` which have - their past given to this model should not be passed as `input_ids` as they have already been computed. + past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*): + Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention + blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values` + returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`. + + Two formats are allowed: + - a [`~cache_utils.Cache`] instance; + - Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of + shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy + cache format. + + The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the + legacy cache format will be returned. + + If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't + have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids` + of shape `(batch_size, sequence_length)`. attention_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`: @@ -712,7 +718,7 @@ def __init__(self, config): self.wte = nn.Embedding(config.vocab_size, self.embed_dim) self.wpe = nn.Embedding(config.max_position_embeddings, self.embed_dim) self.drop = nn.Dropout(float(config.embed_dropout)) - self.h = nn.ModuleList([GPTNeoBlock(config, layer_idx=i) for i in range(config.num_layers)]) + self.h = nn.ModuleList([GPTNeoBlock(config, layer_id=i) for i in range(config.num_layers)]) self._use_flash_attention_2 = config._attn_implementation == "flash_attention_2" self.ln_f = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_epsilon) diff --git a/src/transformers/models/gpt_neox/modeling_gpt_neox.py b/src/transformers/models/gpt_neox/modeling_gpt_neox.py index d2f08ee972aa..cf550be051d6 100755 --- a/src/transformers/models/gpt_neox/modeling_gpt_neox.py +++ b/src/transformers/models/gpt_neox/modeling_gpt_neox.py @@ -776,10 +776,23 @@ def forward( Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert *input_ids* indices into associated vectors than the model's internal embedding lookup matrix. - past_key_values (`Cache` or `Tuple[Tuple[torch.Tensor]]` of length `config.num_layers`): - Contains precomputed hidden-states (key and values in the attention blocks) as computed by the model (see - `past_key_values` output below). Can be used to speed up sequential decoding. The `input_ids` which have - their past given to this model should not be passed as `input_ids` as they have already been computed. + past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*): + Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention + blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values` + returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`. + + Two formats are allowed: + - a [`~cache_utils.Cache`] instance; + - Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of + shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy + cache format. + + The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the + legacy cache format will be returned. + + If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't + have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids` + of shape `(batch_size, sequence_length)`. output_attentions (`bool`, *optional*): Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned tensors for more detail. diff --git a/src/transformers/models/gptj/modeling_gptj.py b/src/transformers/models/gptj/modeling_gptj.py index dd17f02cb2e5..ead2ed7a100a 100644 --- a/src/transformers/models/gptj/modeling_gptj.py +++ b/src/transformers/models/gptj/modeling_gptj.py @@ -677,10 +677,23 @@ def _init_weights(self, module): Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert *input_ids* indices into associated vectors than the model's internal embedding lookup matrix. - past_key_values (`Cache` or `Tuple[Tuple[torch.Tensor]]` of length `config.num_layers`): - Contains precomputed hidden-states (key and values in the attention blocks) as computed by the model (see - `past_key_values` output below). Can be used to speed up sequential decoding. The `input_ids` which have - their past given to this model should not be passed as `input_ids` as they have already been computed. + past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*): + Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention + blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values` + returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`. + + Two formats are allowed: + - a [`~cache_utils.Cache`] instance; + - Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of + shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy + cache format. + + The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the + legacy cache format will be returned. + + If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't + have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids` + of shape `(batch_size, sequence_length)`. output_attentions (`bool`, *optional*): Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned tensors for more detail. From dd05e6bfb74b112452f3ae1f3efaea10e6c189aa Mon Sep 17 00:00:00 2001 From: raushan Date: Tue, 18 Jun 2024 08:36:40 +0200 Subject: [PATCH 13/40] git, codegen, gpt_neo, gpt_neox, gpj From cb878d5793e3f1fa066d4f8c11b589826d3b6c4a Mon Sep 17 00:00:00 2001 From: raushan Date: Wed, 19 Jun 2024 11:15:19 +0200 Subject: [PATCH 14/40] clean up --- .../models/codegen/modeling_codegen.py | 235 +++++++++++----- .../models/falcon/modeling_falcon.py | 239 +++++++++++------ .../models/gpt_neo/modeling_gpt_neo.py | 222 +++++++++++---- .../models/gpt_neox/modeling_gpt_neox.py | 253 ++++++++++++------ src/transformers/models/gptj/modeling_gptj.py | 248 ++++++++++++----- 5 files changed, 834 insertions(+), 363 deletions(-) diff --git a/src/transformers/models/codegen/modeling_codegen.py b/src/transformers/models/codegen/modeling_codegen.py index f596035ae730..e9a32fa902fc 100644 --- a/src/transformers/models/codegen/modeling_codegen.py +++ b/src/transformers/models/codegen/modeling_codegen.py @@ -22,7 +22,8 @@ from torch.nn import CrossEntropyLoss from ...activations import ACT2FN -from ...cache_utils import Cache, DynamicCache +from ...cache_utils import Cache, DynamicCache, StaticCache +from ...modeling_attn_mask_utils import AttentionMaskConverter from ...modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast from ...modeling_utils import PreTrainedModel from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging @@ -140,8 +141,8 @@ def _attn( attn_weights = torch.where(causal_mask, attn_weights, mask_value) if attention_mask is not None: - # Apply the attention mask - attn_weights = attn_weights + attention_mask + causal_mask = attention_mask[:, :, :, : key.shape[-2]] + attn_weights += causal_mask attn_weights = nn.Softmax(dim=-1)(attn_weights) attn_weights = attn_weights.to(value.dtype) @@ -164,6 +165,7 @@ def forward( head_mask: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, + cache_position: Optional[torch.LongTensor] = None, ) -> Union[ Tuple[torch.Tensor, Tuple[torch.Tensor]], Optional[Tuple[torch.Tensor, Tuple[torch.Tensor], Tuple[torch.Tensor, ...]]], @@ -211,7 +213,12 @@ def forward( # Note that this cast is quite ugly, but is not implemented before ROPE as k_rot in the original codebase is always in fp32. # Reference: https://github.com/salesforce/CodeGen/blob/f210c3bb1216c975ad858cd4132c0fdeabf4bfc2/codegen1/jaxformer/hf/codegen/modeling_codegen.py#L38 if layer_past is not None: - cache_kwargs = {"sin": sin, "cos": cos, "partial_rotation_size": self.rotary_dim} + cache_kwargs = { + "sin": sin, + "cos": cos, + "partial_rotation_size": self.rotary_dim, + "cache_position": cache_position, + } key, value = layer_past.update(key.to(hidden_states.dtype), value, self.layer_idx, cache_kwargs) # compute self-attention: V x Softmax(QK^T) @@ -267,6 +274,7 @@ def forward( head_mask: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, + cache_position: Optional[torch.LongTensor] = None, ) -> Union[Tuple[torch.Tensor], Optional[Tuple[torch.Tensor, Tuple[torch.FloatTensor, ...]]]]: residual = hidden_states hidden_states = self.ln_1(hidden_states) @@ -278,6 +286,7 @@ def forward( head_mask=head_mask, use_cache=use_cache, output_attentions=output_attentions, + cache_position=cache_position, ) attn_output = attn_outputs[0] # output_attn: a, present, (attentions) outputs = attn_outputs[1:] @@ -401,6 +410,10 @@ def _init_weights(self, module): more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. + cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): + Indices depicting the position of the input sequence tokens in the sequence. Contrarily to `position_ids`, + this tensor is not affected by padding. It is used to update the cache in the correct position and to infer + the complete sequence length. """ @@ -450,6 +463,7 @@ def forward( output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, + cache_position: Optional[torch.LongTensor] = None, ) -> Union[Tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( @@ -458,82 +472,59 @@ def forward( use_cache = use_cache if use_cache is not None else self.config.use_cache return_dict = return_dict if return_dict is not None else self.config.use_return_dict - if input_ids is not None and inputs_embeds is not None: - raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time") - elif input_ids is not None: - self.warn_if_padding_and_no_attention_mask(input_ids, attention_mask) - input_shape = input_ids.size() - input_ids = input_ids.view(-1, input_shape[-1]) - batch_size = input_ids.shape[0] - elif inputs_embeds is not None: - input_shape = inputs_embeds.size()[:-1] - batch_size = inputs_embeds.shape[0] - else: - raise ValueError("You have to specify either input_ids or inputs_embeds") + if (input_ids is None) ^ (inputs_embeds is not None): + raise ValueError( + "You cannot specify both input_ids and inputs_embeds at the same time, and must specify either one" + ) - device = input_ids.device if input_ids is not None else inputs_embeds.device + if self.gradient_checkpointing and self.training: + if use_cache: + logger.warning_once( + "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." + ) + use_cache = False - if token_type_ids is not None: - token_type_ids = token_type_ids.view(-1, input_shape[-1]) + if inputs_embeds is None: + inputs_embeds = self.wte(input_ids) - past_length = 0 - if use_cache: - use_legacy_cache = not isinstance(past_key_values, Cache) - if use_legacy_cache: - past_key_values = DynamicCache.from_legacy_cache(past_key_values) - past_length = past_key_values.get_seq_length() + use_legacy_cache = False + if use_cache and not isinstance(past_key_values, Cache): # kept for BC (non `Cache` `past_key_values` inputs) + use_legacy_cache = True + past_key_values = DynamicCache.from_legacy_cache(past_key_values) + logger.warning_once( + "Using `past_key_values` as a tuple is deprecated and will be removed in v4.43. " + "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" + ) + + input_shape = inputs_embeds.shape[:-1] + if cache_position is None: + past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0 + cache_position = torch.arange( + past_seen_tokens, past_seen_tokens + input_shape[-1], device=inputs_embeds.device + ) if position_ids is None: - position_ids = torch.arange(past_length, input_shape[-1] + past_length, dtype=torch.long, device=device) - position_ids = position_ids.unsqueeze(0) + position_ids = cache_position.unsqueeze(0) - # Attention mask. - if attention_mask is not None: - if batch_size <= 0: - raise ValueError("batch_size has to be defined and > 0") - attention_mask = attention_mask.view(batch_size, -1) - # We create a 3D attention mask from a 2D tensor mask. - # Sizes are [batch_size, 1, 1, to_seq_length] - # So we can broadcast to [batch_size, num_heads, from_seq_length, to_seq_length] - # this attention mask is more simple than the triangular masking of causal attention - # used in OpenAI GPT, we just need to prepare the broadcast dimension here. - attention_mask = attention_mask[:, None, None, :] - - # Since attention_mask is 1.0 for positions we want to attend and 0.0 for - # masked positions, this operation will create a tensor which is 0.0 for - # positions we want to attend and the dtype's smallest value for masked positions. - # Since we are adding it to the raw scores before the softmax, this is - # effectively the same as removing these entirely. - attention_mask = attention_mask.to(dtype=self.dtype) # fp16 compatibility - attention_mask = (1.0 - attention_mask) * torch.finfo(self.dtype).min + causal_mask = self._update_causal_mask( + attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions + ) # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head # attention_probs has shape bsz x num_attention_heads x N x N # head_mask has shape n_layer x batch x num_attention_heads x N x N head_mask = self.get_head_mask(head_mask, self.config.n_layer) - - if inputs_embeds is None: - inputs_embeds = self.wte(input_ids) - hidden_states = inputs_embeds if token_type_ids is not None: + token_type_ids = token_type_ids.view(-1, input_shape[-1]) token_type_embeds = self.wte(token_type_ids) hidden_states = hidden_states + token_type_embeds hidden_states = self.drop(hidden_states) - output_shape = input_shape + (hidden_states.size(-1),) - if self.gradient_checkpointing and self.training: - if use_cache: - logger.warning_once( - "`use_cache=True` is incompatible with `config.gradient_checkpointing=True`. Setting " - "`use_cache=False`..." - ) - use_cache = False - next_decoder_cache = None all_self_attentions = () if output_attentions else None all_hidden_states = () if output_hidden_states else None @@ -546,21 +537,23 @@ def forward( block.__call__, hidden_states, None, - attention_mask, + causal_mask, position_ids, head_mask[i], use_cache, output_attentions, + cache_position, ) else: outputs = block( hidden_states=hidden_states, layer_past=past_key_values, - attention_mask=attention_mask, + attention_mask=causal_mask, position_ids=position_ids, head_mask=head_mask[i], use_cache=use_cache, output_attentions=output_attentions, + cache_position=cache_position, ) hidden_states = outputs[0] @@ -593,6 +586,85 @@ def forward( attentions=all_self_attentions, ) + def _update_causal_mask( + self, + attention_mask: torch.Tensor, + input_tensor: torch.Tensor, + cache_position: torch.Tensor, + past_key_values: Cache, + output_attentions: bool, + ): + # TODO: As of torch==2.2.0, the `attention_mask` passed to the model in `generate` is 2D and of dynamic length even when the static + # KV cache is used. This is an issue for torch.compile which then recaptures cudagraphs at each decode steps due to the dynamic shapes. + # (`recording cudagraph tree for symint key 13`, etc.), which is VERY slow. A workaround is `@torch.compiler.disable`, but this prevents using + # `fullgraph=True`. See more context in https://github.com/huggingface/transformers/pull/29114 + + if self.config._attn_implementation == "flash_attention_2": + if attention_mask is not None and 0.0 in attention_mask: + return attention_mask + return None + + # For SDPA, when possible, we will rely on its `is_causal` argument instead of its `attn_mask` argument, in + # order to dispatch on Flash Attention 2. This feature is not compatible with static cache, as SDPA will fail + # to infer the attention mask. + past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0 + using_static_cache = isinstance(past_key_values, StaticCache) + + # When output attentions is True, sdpa implementation's forward method calls the eager implementation's forward + if self.config._attn_implementation == "sdpa" and not using_static_cache and not output_attentions: + if AttentionMaskConverter._ignore_causal_mask_sdpa( + attention_mask, + inputs_embeds=input_tensor, + past_key_values_length=past_seen_tokens, + is_training=self.training, + ): + return None + + dtype, device = input_tensor.dtype, input_tensor.device + min_dtype = torch.finfo(dtype).min + sequence_length = input_tensor.shape[1] + if using_static_cache: + target_length = past_key_values.get_max_length() + else: + target_length = ( + attention_mask.shape[-1] + if isinstance(attention_mask, torch.Tensor) + else past_seen_tokens + sequence_length + 1 + ) + if attention_mask is not None and attention_mask.dim() == 4: + # in this case we assume that the mask comes already in inverted form and requires no inversion or slicing + if attention_mask.max() != 0: + raise ValueError("Custom 4D attention mask should be passed in inverted form with max==0`") + causal_mask = attention_mask + else: + causal_mask = torch.full( + (sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device + ) + if sequence_length != 1: + causal_mask = torch.triu(causal_mask, diagonal=1) + causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1) + causal_mask = causal_mask[None, None, :, :].expand(input_tensor.shape[0], 1, -1, -1) + if attention_mask is not None: + causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit + mask_length = attention_mask.shape[-1] + padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :] + padding_mask = padding_mask == 0 + causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill( + padding_mask, min_dtype + ) + if ( + self.config._attn_implementation == "sdpa" + and attention_mask is not None + and attention_mask.device.type == "cuda" + and not output_attentions + ): + # Attend to all tokens in fully masked rows in the causal_mask, for example the relevant first rows when + # using left padding. This is required by F.scaled_dot_product_attention memory-efficient attention path. + # Details: https://github.com/pytorch/pytorch/issues/110213 + causal_mask = AttentionMaskConverter._unmask_unattended(causal_mask, min_dtype) + + return causal_mask + @add_start_docstrings( """ @@ -617,14 +689,28 @@ def get_output_embeddings(self): def set_output_embeddings(self, new_embeddings): self.lm_head = new_embeddings - def prepare_inputs_for_generation(self, input_ids, inputs_embeds=None, past_key_values=None, **kwargs): + # Copied from transformers.models.gpt_neo.modeling_gpt_neo.GPTNeoForCausalLM.prepare_inputs_for_generation + def prepare_inputs_for_generation( + self, + input_ids, + attention_mask=None, + past_key_values=None, + inputs_embeds=None, + cache_position=None, + use_cache=True, + **kwargs, + ): token_type_ids = kwargs.get("token_type_ids", None) - attention_mask = kwargs.get("attention_mask", None) past_length = 0 # Omit tokens covered by past_key_values - if past_key_values: - past_length = cache_length = past_key_values.get_seq_length() - max_cache_length = past_key_values.get_max_length() + if past_key_values is not None: + past_length = cache_position[0] if cache_position is not None else past_key_values.get_seq_length() + max_cache_length = ( + torch.tensor(past_key_values.get_max_length(), device=input_ids.device) + if past_key_values.get_max_length() is not None + else None + ) + cache_length = past_length if max_cache_length is None else torch.min(max_cache_length, past_length) # Some generation methods already pass only the last input ID if input_ids.shape[1] > past_length: @@ -634,6 +720,7 @@ def prepare_inputs_for_generation(self, input_ids, inputs_embeds=None, past_key_ remove_prefix_length = input_ids.shape[1] - 1 input_ids = input_ids[:, remove_prefix_length:] + if token_type_ids is not None: token_type_ids = token_type_ids[:, -input_ids.shape[1] :] @@ -646,7 +733,6 @@ def prepare_inputs_for_generation(self, input_ids, inputs_embeds=None, past_key_ attention_mask = attention_mask[:, -max_cache_length:] position_ids = kwargs.get("position_ids", None) - if attention_mask is not None and position_ids is None: # create position_ids on the fly for batch generation position_ids = attention_mask.long().cumsum(-1) - 1 @@ -658,17 +744,26 @@ def prepare_inputs_for_generation(self, input_ids, inputs_embeds=None, past_key_ if inputs_embeds is not None and past_length == 0: model_inputs = {"inputs_embeds": inputs_embeds} else: - model_inputs = {"input_ids": input_ids.contiguous()} + model_inputs = {"input_ids": input_ids} + + if use_cache: + input_length = input_ids.shape[-1] + if cache_position is None: + cache_position = torch.arange(past_length, past_length + input_length, device=input_ids.device) + else: + cache_position = cache_position[-input_length:] model_inputs.update( { "past_key_values": past_key_values, - "use_cache": kwargs.get("use_cache"), + "use_cache": use_cache, "position_ids": position_ids, "attention_mask": attention_mask, "token_type_ids": token_type_ids, + "cache_position": cache_position, } ) + return model_inputs @add_start_docstrings_to_model_forward(CODEGEN_INPUTS_DOCSTRING.format("batch_size, sequence_length")) @@ -691,6 +786,7 @@ def forward( output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, + cache_position: Optional[torch.LongTensor] = None, ) -> Union[Tuple, CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): @@ -712,6 +808,7 @@ def forward( output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, + cache_position=cache_position, ) hidden_states = transformer_outputs[0] diff --git a/src/transformers/models/falcon/modeling_falcon.py b/src/transformers/models/falcon/modeling_falcon.py index 4c4ae3a29d9d..62c7f2fabdd3 100644 --- a/src/transformers/models/falcon/modeling_falcon.py +++ b/src/transformers/models/falcon/modeling_falcon.py @@ -24,11 +24,9 @@ from torch.nn import functional as F from ...activations import get_activation -from ...cache_utils import Cache, DynamicCache +from ...cache_utils import Cache, DynamicCache, StaticCache from ...modeling_attn_mask_utils import ( AttentionMaskConverter, - _prepare_4d_causal_attention_mask, - _prepare_4d_causal_attention_mask_for_sdpa, ) from ...modeling_outputs import ( BaseModelOutputWithPastAndCrossAttentions, @@ -400,6 +398,7 @@ def forward( head_mask: Optional[torch.Tensor] = None, use_cache: bool = False, output_attentions: bool = False, + cache_position: Optional[torch.LongTensor] = None, ): fused_qkv = self.query_key_value(hidden_states) # [batch_size, seq_length, 3 x hidden_size] num_kv_heads = self.num_heads if self.new_decoder_architecture else self.num_kv_heads @@ -426,7 +425,9 @@ def forward( query_layer, key_layer = apply_rotary_pos_emb(query_layer, key_layer, cos, sin, position_ids) if layer_past is not None: - cache_kwargs = {"sin": sin, "cos": cos} if alibi is None else None + cache_kwargs = {"cache_position": cache_position} + if alibi is None: + cache_kwargs.update({"sin": sin, "cos": cos}) key_layer, value_layer = layer_past.update(key_layer, value_layer, self.layer_idx, cache_kwargs) kv_length = key_layer.shape[-2] @@ -437,6 +438,9 @@ def forward( key_layer = key_layer.contiguous() value_layer = value_layer.contiguous() + if attention_mask is not None: + attention_mask = attention_mask[:, :, :, : key_layer.shape[-2]] + if alibi is None: if self._use_sdpa and not output_attentions: # We dispatch to SDPA's Flash Attention or Efficient kernels via this if statement instead of an @@ -553,6 +557,7 @@ def forward( head_mask: Optional[torch.Tensor] = None, use_cache: bool = False, output_attentions: bool = False, + cache_position: Optional[torch.LongTensor] = None, ): fused_qkv = self.query_key_value(hidden_states) # [batch_size, seq_length, 3 x hidden_size] num_kv_heads = self.num_heads if self.new_decoder_architecture else self.num_kv_heads @@ -579,7 +584,9 @@ def forward( query_layer, key_layer = apply_rotary_pos_emb(query_layer, key_layer, cos, sin, position_ids) if layer_past is not None: - cache_kwargs = {"sin": sin, "cos": cos} if alibi is None else None + cache_kwargs = {"cache_position": cache_position} + if alibi is None: + cache_kwargs.update({"sin": sin, "cos": cos}) key_layer, value_layer = layer_past.update(key_layer, value_layer, self.layer_idx, cache_kwargs) # TODO: These transpose are quite inefficient but Flash Attention requires the layout [batch_size, sequence_length, num_heads, head_dim]. We would need to refactor the KV cache @@ -787,6 +794,7 @@ def forward( head_mask: Optional[torch.Tensor] = None, use_cache: bool = False, output_attentions: bool = False, + cache_position: Optional[torch.LongTensor] = None, ): residual = hidden_states @@ -806,6 +814,7 @@ def forward( head_mask=head_mask, use_cache=use_cache, output_attentions=output_attentions, + cache_position=cache_position, ) attention_output = attn_outputs[0] @@ -925,6 +934,10 @@ def forward( more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~file_utils.ModelOutput`] instead of a plain tuple. + cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): + Indices depicting the position of the input sequence tokens in the sequence. Contrarily to `position_ids`, + this tensor is not affected by padding. It is used to update the cache in the correct position and to infer + the complete sequence length. """ @@ -1033,6 +1046,7 @@ def forward( output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, + cache_position: Optional[torch.LongTensor] = None, ) -> Union[Tuple[torch.Tensor, ...], BaseModelOutputWithPastAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( @@ -1041,38 +1055,34 @@ def forward( use_cache = use_cache if use_cache is not None else self.config.use_cache return_dict = return_dict if return_dict is not None else self.config.use_return_dict - if input_ids is not None and inputs_embeds is not None: - raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time") - elif input_ids is not None: - batch_size, seq_length = input_ids.shape - elif inputs_embeds is not None: - batch_size, seq_length, _ = inputs_embeds.shape - else: - raise ValueError("You have to specify either input_ids or inputs_embeds") - - if inputs_embeds is None: - inputs_embeds = self.word_embeddings(input_ids) - - hidden_states = inputs_embeds + if (input_ids is None) ^ (inputs_embeds is not None): + raise ValueError( + "You cannot specify both input_ids and inputs_embeds at the same time, and must specify either one" + ) if self.gradient_checkpointing and self.training: if use_cache: - logger.warning( + logger.warning_once( "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." ) use_cache = False - next_decoder_cache = None - all_self_attentions = () if output_attentions else None - all_hidden_states = () if output_hidden_states else None + + if inputs_embeds is None: + inputs_embeds = self.word_embeddings(input_ids) # Compute alibi tensor: check build_alibi_tensor documentation - past_key_values_length = 0 - if use_cache: - use_legacy_cache = not isinstance(past_key_values, Cache) - if use_legacy_cache: - past_key_values = DynamicCache.from_legacy_cache(past_key_values) - past_key_values_length = past_key_values.get_seq_length() + use_legacy_cache = False + if use_cache and not isinstance(past_key_values, Cache): # kept for BC (non `Cache` `past_key_values` inputs) + use_legacy_cache = True + past_key_values = DynamicCache.from_legacy_cache(past_key_values) + logger.warning_once( + "Using `past_key_values` as a tuple is deprecated and will be removed in v4.43. " + "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" + ) + alibi = None + past_key_values_length = past_key_values.get_seq_length() if use_cache else 0 + batch_size, seq_length, _ = inputs_embeds.shape if self.use_alibi: mask = ( torch.ones( @@ -1081,65 +1091,30 @@ def forward( if attention_mask is None else attention_mask ) - alibi = build_alibi_tensor(mask, self.num_heads, dtype=hidden_states.dtype) - else: - alibi = None - if position_ids is None: - device = input_ids.device if input_ids is not None else inputs_embeds.device - position_ids = torch.arange( - past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device - ) - position_ids = position_ids.unsqueeze(0) - - if self._use_flash_attention_2: - # 2d mask is passed through the layers - attention_mask = attention_mask if (attention_mask is not None and 0 in attention_mask) else None - elif self._use_sdpa and not output_attentions: - # output_attentions=True can not be supported when using SDPA, and we fall back on - # the manual implementation that requires a 4D causal mask in all cases. - if alibi is None: - attention_mask = _prepare_4d_causal_attention_mask_for_sdpa( - attention_mask, - (batch_size, seq_length), - inputs_embeds, - past_key_values_length, - ) - elif head_mask is None: - alibi = alibi.reshape(batch_size, -1, *alibi.shape[1:]) + alibi = build_alibi_tensor(mask, self.num_heads, dtype=inputs_embeds.dtype) - # We don't call _prepare_4d_causal_attention_mask_for_sdpa as we need to mask alibi using the 4D attention_mask untouched. - attention_mask = _prepare_4d_causal_attention_mask( - attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length - ) + if cache_position is None: + cache_position = torch.arange( + past_key_values_length, past_key_values_length + seq_length, device=inputs_embeds.device + ) - # We take care to integrate alibi bias in the attention_mask here. - min_dtype = torch.finfo(alibi.dtype).min - attention_mask = torch.masked_fill( - alibi / math.sqrt(self.config.hidden_size // self.num_heads), - attention_mask < -1, - min_dtype, - ) + if position_ids is None: + position_ids = cache_position.unsqueeze(0) - # From PyTorch 2.1 onwards, F.scaled_dot_product_attention with the memory-efficient attention backend - # produces nans if sequences are completely unattended in the attention mask. Details: https://github.com/pytorch/pytorch/issues/110213 - if seq_length > 1 and attention_mask.device.type == "cuda": - attention_mask = AttentionMaskConverter._unmask_unattended(attention_mask, min_dtype=min_dtype) - else: - # PyTorch SDPA does not support head_mask, we fall back on the eager implementation in this case. - attention_mask = _prepare_4d_causal_attention_mask( - attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length - ) - else: - # 4d mask is passed through the layers - attention_mask = _prepare_4d_causal_attention_mask( - attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length - ) + causal_mask = self._update_causal_mask( + attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions + ) # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head # attention_probs has shape batch_size x num_heads x N x N # head_mask has shape n_layer x batch x num_heads x N x N head_mask = self.get_head_mask(head_mask, self.config.num_hidden_layers) + hidden_states = inputs_embeds + + next_decoder_cache = None + all_self_attentions = () if output_attentions else None + all_hidden_states = () if output_hidden_states else None for i, block in enumerate(self.h): if output_hidden_states: @@ -1150,23 +1125,25 @@ def forward( block.__call__, hidden_states, alibi, - attention_mask, + causal_mask, position_ids, head_mask[i], past_key_values, use_cache, output_attentions, + cache_position, ) else: outputs = block( hidden_states, layer_past=past_key_values, - attention_mask=attention_mask, + attention_mask=causal_mask, position_ids=position_ids, head_mask=head_mask[i], use_cache=use_cache, output_attentions=output_attentions, alibi=alibi, + cache_position=cache_position, ) hidden_states = outputs[0] @@ -1198,6 +1175,85 @@ def forward( attentions=all_self_attentions, ) + def _update_causal_mask( + self, + attention_mask: torch.Tensor, + input_tensor: torch.Tensor, + cache_position: torch.Tensor, + past_key_values: Cache, + output_attentions: bool, + ): + # TODO: As of torch==2.2.0, the `attention_mask` passed to the model in `generate` is 2D and of dynamic length even when the static + # KV cache is used. This is an issue for torch.compile which then recaptures cudagraphs at each decode steps due to the dynamic shapes. + # (`recording cudagraph tree for symint key 13`, etc.), which is VERY slow. A workaround is `@torch.compiler.disable`, but this prevents using + # `fullgraph=True`. See more context in https://github.com/huggingface/transformers/pull/29114 + + if self.config._attn_implementation == "flash_attention_2": + if attention_mask is not None and 0.0 in attention_mask: + return attention_mask + return None + + # For SDPA, when possible, we will rely on its `is_causal` argument instead of its `attn_mask` argument, in + # order to dispatch on Flash Attention 2. This feature is not compatible with static cache, as SDPA will fail + # to infer the attention mask. + past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0 + using_static_cache = isinstance(past_key_values, StaticCache) + + # When output attentions is True, sdpa implementation's forward method calls the eager implementation's forward + if self.config._attn_implementation == "sdpa" and not using_static_cache and not output_attentions: + if AttentionMaskConverter._ignore_causal_mask_sdpa( + attention_mask, + inputs_embeds=input_tensor, + past_key_values_length=past_seen_tokens, + is_training=self.training, + ): + return None + + dtype, device = input_tensor.dtype, input_tensor.device + min_dtype = torch.finfo(dtype).min + sequence_length = input_tensor.shape[1] + if using_static_cache: + target_length = past_key_values.get_max_length() + else: + target_length = ( + attention_mask.shape[-1] + if isinstance(attention_mask, torch.Tensor) + else past_seen_tokens + sequence_length + 1 + ) + if attention_mask is not None and attention_mask.dim() == 4: + # in this case we assume that the mask comes already in inverted form and requires no inversion or slicing + if attention_mask.max() != 0: + raise ValueError("Custom 4D attention mask should be passed in inverted form with max==0`") + causal_mask = attention_mask + else: + causal_mask = torch.full( + (sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device + ) + if sequence_length != 1: + causal_mask = torch.triu(causal_mask, diagonal=1) + causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1) + causal_mask = causal_mask[None, None, :, :].expand(input_tensor.shape[0], 1, -1, -1) + if attention_mask is not None: + causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit + mask_length = attention_mask.shape[-1] + padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :] + padding_mask = padding_mask == 0 + causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill( + padding_mask, min_dtype + ) + if ( + self.config._attn_implementation == "sdpa" + and attention_mask is not None + and attention_mask.device.type == "cuda" + and not output_attentions + ): + # Attend to all tokens in fully masked rows in the causal_mask, for example the relevant first rows when + # using left padding. This is required by F.scaled_dot_product_attention memory-efficient attention path. + # Details: https://github.com/pytorch/pytorch/issues/110213 + causal_mask = AttentionMaskConverter._unmask_unattended(causal_mask, min_dtype) + + return causal_mask + @add_start_docstrings( "The Falcon Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings).", @@ -1227,13 +1283,19 @@ def prepare_inputs_for_generation( attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, + cache_position: Optional[torch.LongTensor] = None, + use_cache: bool = True, **kwargs, ) -> dict: past_length = 0 if past_key_values is not None: - past_length = cache_length = past_key_values.get_seq_length() - max_cache_length = past_key_values.get_max_length() - + past_length = cache_position[0] if cache_position is not None else past_key_values.get_seq_length() + max_cache_length = ( + torch.tensor(past_key_values.get_max_length(), device=input_ids.device) + if past_key_values.get_max_length() is not None + else None + ) + cache_length = past_length if max_cache_length is None else torch.min(max_cache_length, past_length) # Some generation methods already pass only the last input ID if input_ids.shape[1] > past_length: remove_prefix_length = past_length @@ -1264,12 +1326,19 @@ def prepare_inputs_for_generation( else: model_inputs = {"input_ids": input_ids} + input_length = position_ids.shape[-1] if position_ids is not None else input_ids.shape[-1] + if cache_position is None: + cache_position = torch.arange(past_length, past_length + input_length, device=input_ids.device) + elif use_cache: + cache_position = cache_position[-input_length:] + model_inputs.update( { "position_ids": position_ids, "past_key_values": past_key_values, - "use_cache": kwargs.get("use_cache"), + "use_cache": use_cache, "attention_mask": attention_mask, + "cache_position": cache_position, } ) return model_inputs @@ -1293,6 +1362,7 @@ def forward( output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, + cache_position: Optional[torch.LongTensor] = None, ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): @@ -1314,6 +1384,7 @@ def forward( output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, + cache_position=cache_position, ) hidden_states = transformer_outputs[0] diff --git a/src/transformers/models/gpt_neo/modeling_gpt_neo.py b/src/transformers/models/gpt_neo/modeling_gpt_neo.py index 06f62097df4a..6dacdf7e782f 100755 --- a/src/transformers/models/gpt_neo/modeling_gpt_neo.py +++ b/src/transformers/models/gpt_neo/modeling_gpt_neo.py @@ -24,8 +24,8 @@ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACT2FN -from ...cache_utils import Cache, DynamicCache -from ...modeling_attn_mask_utils import _prepare_4d_causal_attention_mask +from ...cache_utils import Cache, DynamicCache, StaticCache +from ...modeling_attn_mask_utils import AttentionMaskConverter, _prepare_4d_causal_attention_mask from ...modeling_outputs import ( BaseModelOutputWithPast, BaseModelOutputWithPastAndCrossAttentions, @@ -233,9 +233,9 @@ def _attn(self, query, key, value, attention_mask=None, head_mask=None): mask_value = torch.tensor(mask_value, dtype=attn_weights.dtype).to(attn_weights.device) attn_weights = torch.where(causal_mask, attn_weights, mask_value) - if attention_mask is not None: - # Apply the attention mask - attn_weights = attn_weights + attention_mask + if attention_mask is not None: # no matter the length, we just slice it + causal_mask = attention_mask[:, :, :, : key.shape[-2]] + attn_weights = attn_weights + causal_mask attn_weights = nn.functional.softmax(attn_weights, dim=-1) attn_weights = attn_weights.to(value.dtype) @@ -257,6 +257,7 @@ def forward( head_mask=None, use_cache=False, output_attentions=False, + cache_position=None, ): query = self.q_proj(hidden_states) key = self.k_proj(hidden_states) @@ -267,7 +268,8 @@ def forward( value = self._split_heads(value, self.num_heads, self.head_dim) if layer_past is not None: - key, value = layer_past.update(key, value, self.layer_id) + cache_kwargs = {"cache_position": cache_position} + key, value = layer_past.update(key, value, self.layer_id, cache_kwargs) attn_output, attn_weights = self._attn(query, key, value, attention_mask, head_mask) @@ -306,6 +308,7 @@ def forward( head_mask=None, use_cache=False, output_attentions=False, + cache_position=None, ): bsz, _, _ = hidden_states.size() @@ -318,7 +321,8 @@ def forward( value = self._split_heads(value, self.num_heads, self.head_dim) if layer_past is not None: - key, value = layer_past.update(key, value, self.layer_id) + cache_kwargs = {"cache_position": cache_position} + key, value = layer_past.update(key, value, self.layer_id, cache_kwargs) query_length = query.shape[2] tgt_len = key.shape[2] @@ -331,6 +335,9 @@ def forward( attn_dropout = self.config.attention_dropout if self.training else 0.0 + if attention_mask is not None: # no matter the length, we just slice it + attention_mask = attention_mask[:, :, :, : key.shape[-2]] + # In PEFT, usually we cast the layer norms in float32 for training stability reasons # therefore the input hidden states gets silently casted in float32. Hence, we need # cast them back in the correct dtype just to be sure everything works as expected. @@ -501,6 +508,7 @@ def forward( head_mask=None, use_cache=False, output_attentions=False, + cache_position=None, ): return self.attention( hidden_states, @@ -509,6 +517,7 @@ def forward( head_mask=head_mask, use_cache=use_cache, output_attentions=output_attentions, + cache_position=cache_position, ) @@ -547,6 +556,7 @@ def forward( head_mask=None, use_cache=False, output_attentions=False, + cache_position=None, ): residual = hidden_states hidden_states = self.ln_1(hidden_states) @@ -557,6 +567,7 @@ def forward( head_mask=head_mask, use_cache=use_cache, output_attentions=output_attentions, + cache_position=cache_position, ) attn_output = attn_outputs[0] # output_attn: a, present, (attentions) outputs = attn_outputs[1:] @@ -703,6 +714,10 @@ def _init_weights(self, module): more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. + cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): + Indices depicting the position of the input sequence tokens in the sequence. Contrarily to `position_ids`, + this tensor is not affected by padding. It is used to update the cache in the correct position and to infer + the complete sequence length. """ @@ -751,6 +766,7 @@ def forward( output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, + cache_position: Optional[torch.LongTensor] = None, ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( @@ -759,67 +775,60 @@ def forward( use_cache = use_cache if use_cache is not None else self.config.use_cache return_dict = return_dict if return_dict is not None else self.config.use_return_dict - if input_ids is not None and inputs_embeds is not None: - raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time") - elif input_ids is not None: - self.warn_if_padding_and_no_attention_mask(input_ids, attention_mask) - input_shape = input_ids.size() - input_ids = input_ids.view(-1, input_shape[-1]) - elif inputs_embeds is not None: - input_shape = inputs_embeds.size()[:-1] - else: - raise ValueError("You have to specify either input_ids or inputs_embeds") + if (input_ids is None) ^ (inputs_embeds is not None): + raise ValueError( + "You cannot specify both input_ids and inputs_embeds at the same time, and must specify either one" + ) - device = input_ids.device if input_ids is not None else inputs_embeds.device + if self.gradient_checkpointing and self.training: + if use_cache: + logger.warning_once( + "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." + ) + use_cache = False - if token_type_ids is not None: - token_type_ids = token_type_ids.view(-1, input_shape[-1]) + if inputs_embeds is None: + inputs_embeds = self.wte(input_ids) - past_length = 0 - if use_cache: - use_legacy_cache = not isinstance(past_key_values, Cache) - if use_legacy_cache: - past_key_values = DynamicCache.from_legacy_cache(past_key_values) - past_length = past_key_values.get_seq_length() + input_shape = inputs_embeds.shape[:-1] + use_legacy_cache = False + if use_cache and not isinstance(past_key_values, Cache): # kept for BC (non `Cache` `past_key_values` inputs) + use_legacy_cache = True + past_key_values = DynamicCache.from_legacy_cache(past_key_values) + logger.warning_once( + "Using `past_key_values` as a tuple is deprecated and will be removed in v4.43. " + "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" + ) + + if cache_position is None: + past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0 + cache_position = torch.arange( + past_seen_tokens, past_seen_tokens + input_shape[-1], device=inputs_embeds.device + ) if position_ids is None: - position_ids = torch.arange(past_length, input_shape[-1] + past_length, dtype=torch.long, device=device) - position_ids = position_ids.unsqueeze(0) + position_ids = cache_position.unsqueeze(0) + + causal_mask = self._update_causal_mask( + attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions + ) # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head # attention_probs has shape bsz x num_heads x N x N # head_mask has shape n_layer x batch x num_heads x N x N head_mask = self.get_head_mask(head_mask, self.config.num_layers) - - if inputs_embeds is None: - inputs_embeds = self.wte(input_ids) position_embeds = self.wpe(position_ids) hidden_states = inputs_embeds + position_embeds - # Attention mask. - if self._use_flash_attention_2: - # 2d mask is passed through the layers - attention_mask = attention_mask if (attention_mask is not None and 0 in attention_mask) else None - else: - # 4d mask is passed through the layers - attention_mask = _prepare_4d_causal_attention_mask(attention_mask, input_shape, inputs_embeds, past_length) - if token_type_ids is not None: + token_type_ids = token_type_ids.view(-1, input_shape[-1]) token_type_embeds = self.wte(token_type_ids) hidden_states = hidden_states + token_type_embeds hidden_states = self.drop(hidden_states) - output_shape = (-1,) + input_shape[1:] + (hidden_states.size(-1),) - if self.gradient_checkpointing and self.training: - if use_cache: - logger.warning_once( - "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." - ) - use_cache = False - next_decoder_cache = None all_self_attentions = () if output_attentions else None all_hidden_states = () if output_hidden_states else None @@ -832,19 +841,21 @@ def forward( block.__call__, hidden_states, None, - attention_mask, + causal_mask, head_mask[i], use_cache, output_attentions, + cache_position, ) else: outputs = block( hidden_states, layer_past=past_key_values, - attention_mask=attention_mask, + attention_mask=causal_mask, head_mask=head_mask[i], use_cache=use_cache, output_attentions=output_attentions, + cache_position=cache_position, ) hidden_states = outputs[0] @@ -877,6 +888,85 @@ def forward( attentions=all_self_attentions, ) + def _update_causal_mask( + self, + attention_mask: torch.Tensor, + input_tensor: torch.Tensor, + cache_position: torch.Tensor, + past_key_values: Cache, + output_attentions: bool, + ): + # TODO: As of torch==2.2.0, the `attention_mask` passed to the model in `generate` is 2D and of dynamic length even when the static + # KV cache is used. This is an issue for torch.compile which then recaptures cudagraphs at each decode steps due to the dynamic shapes. + # (`recording cudagraph tree for symint key 13`, etc.), which is VERY slow. A workaround is `@torch.compiler.disable`, but this prevents using + # `fullgraph=True`. See more context in https://github.com/huggingface/transformers/pull/29114 + + if self.config._attn_implementation == "flash_attention_2": + if attention_mask is not None and 0.0 in attention_mask: + return attention_mask + return None + + # For SDPA, when possible, we will rely on its `is_causal` argument instead of its `attn_mask` argument, in + # order to dispatch on Flash Attention 2. This feature is not compatible with static cache, as SDPA will fail + # to infer the attention mask. + past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0 + using_static_cache = isinstance(past_key_values, StaticCache) + + # When output attentions is True, sdpa implementation's forward method calls the eager implementation's forward + if self.config._attn_implementation == "sdpa" and not using_static_cache and not output_attentions: + if AttentionMaskConverter._ignore_causal_mask_sdpa( + attention_mask, + inputs_embeds=input_tensor, + past_key_values_length=past_seen_tokens, + is_training=self.training, + ): + return None + + dtype, device = input_tensor.dtype, input_tensor.device + min_dtype = torch.finfo(dtype).min + sequence_length = input_tensor.shape[1] + if using_static_cache: + target_length = past_key_values.get_max_length() + else: + target_length = ( + attention_mask.shape[-1] + if isinstance(attention_mask, torch.Tensor) + else past_seen_tokens + sequence_length + 1 + ) + if attention_mask is not None and attention_mask.dim() == 4: + # in this case we assume that the mask comes already in inverted form and requires no inversion or slicing + if attention_mask.max() != 0: + raise ValueError("Custom 4D attention mask should be passed in inverted form with max==0`") + causal_mask = attention_mask + else: + causal_mask = torch.full( + (sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device + ) + if sequence_length != 1: + causal_mask = torch.triu(causal_mask, diagonal=1) + causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1) + causal_mask = causal_mask[None, None, :, :].expand(input_tensor.shape[0], 1, -1, -1) + if attention_mask is not None: + causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit + mask_length = attention_mask.shape[-1] + padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :] + padding_mask = padding_mask == 0 + causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill( + padding_mask, min_dtype + ) + if ( + self.config._attn_implementation == "sdpa" + and attention_mask is not None + and attention_mask.device.type == "cuda" + and not output_attentions + ): + # Attend to all tokens in fully masked rows in the causal_mask, for example the relevant first rows when + # using left padding. This is required by F.scaled_dot_product_attention memory-efficient attention path. + # Details: https://github.com/pytorch/pytorch/issues/110213 + causal_mask = AttentionMaskConverter._unmask_unattended(causal_mask, min_dtype) + + return causal_mask + @add_start_docstrings( """ @@ -902,14 +992,27 @@ def get_output_embeddings(self): def set_output_embeddings(self, new_embeddings): self.lm_head = new_embeddings - def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_embeds=None, **kwargs): + def prepare_inputs_for_generation( + self, + input_ids, + attention_mask=None, + past_key_values=None, + inputs_embeds=None, + cache_position=None, + use_cache=True, + **kwargs, + ): token_type_ids = kwargs.get("token_type_ids", None) - attention_mask = kwargs.get("attention_mask", None) past_length = 0 # Omit tokens covered by past_key_values if past_key_values is not None: - past_length = cache_length = past_key_values.get_seq_length() - max_cache_length = past_key_values.get_max_length() + past_length = cache_position[0] if cache_position is not None else past_key_values.get_seq_length() + max_cache_length = ( + torch.tensor(past_key_values.get_max_length(), device=input_ids.device) + if past_key_values.get_max_length() is not None + else None + ) + cache_length = past_length if max_cache_length is None else torch.min(max_cache_length, past_length) # Some generation methods already pass only the last input ID if input_ids.shape[1] > past_length: @@ -932,7 +1035,6 @@ def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_ attention_mask = attention_mask[:, -max_cache_length:] position_ids = kwargs.get("position_ids", None) - if attention_mask is not None and position_ids is None: # create position_ids on the fly for batch generation position_ids = attention_mask.long().cumsum(-1) - 1 @@ -946,13 +1048,21 @@ def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_ else: model_inputs = {"input_ids": input_ids} + if use_cache: + input_length = input_ids.shape[-1] + if cache_position is None: + cache_position = torch.arange(past_length, past_length + input_length, device=input_ids.device) + else: + cache_position = cache_position[-input_length:] + model_inputs.update( { "past_key_values": past_key_values, - "use_cache": kwargs.get("use_cache"), + "use_cache": use_cache, "position_ids": position_ids, "attention_mask": attention_mask, "token_type_ids": token_type_ids, + "cache_position": cache_position, } ) @@ -978,6 +1088,7 @@ def forward( output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, + cache_position: Optional[torch.LongTensor] = None, ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): @@ -999,6 +1110,7 @@ def forward( output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, + cache_position=cache_position, ) hidden_states = transformer_outputs[0] diff --git a/src/transformers/models/gpt_neox/modeling_gpt_neox.py b/src/transformers/models/gpt_neox/modeling_gpt_neox.py index cf550be051d6..ebc7382141f6 100755 --- a/src/transformers/models/gpt_neox/modeling_gpt_neox.py +++ b/src/transformers/models/gpt_neox/modeling_gpt_neox.py @@ -23,13 +23,14 @@ from torch.nn import functional as F from ...activations import ACT2FN -from ...cache_utils import Cache, DynamicCache +from ...cache_utils import Cache, DynamicCache, StaticCache from ...file_utils import ( add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings, ) +from ...modeling_attn_mask_utils import AttentionMaskConverter from ...modeling_outputs import ( BaseModelOutputWithPast, CausalLMOutputWithPast, @@ -172,6 +173,7 @@ def forward( use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, padding_mask: Optional[torch.Tensor] = None, + cache_position: Optional[torch.LongTensor] = None, ): # Compute QKV # Attention heads [batch, seq_len, hidden_size] @@ -212,7 +214,12 @@ def forward( # Cache QKV values if layer_past is not None: - cache_kwargs = {"sin": sin, "cos": cos, "partial_rotation_size": self.rotary_emb.dim} + cache_kwargs = { + "sin": sin, + "cos": cos, + "partial_rotation_size": self.rotary_emb.dim, + "cache_position": cache_position, + } key, value = layer_past.update(key, value, self.layer_idx, cache_kwargs) # Compute attention @@ -288,9 +295,9 @@ def _attn(self, query, key, value, attention_mask=None, head_mask=None): mask_value = torch.tensor(mask_value, dtype=attn_scores.dtype).to(attn_scores.device) attn_scores = torch.where(causal_mask, attn_scores, mask_value) - if attention_mask is not None: - # Apply the attention mask - attn_scores = attn_scores + attention_mask + if attention_mask is not None: # no matter the length, we just slice it + causal_mask = attention_mask[:, :, :, : key.shape[-2]] + attn_scores = attn_scores + causal_mask attn_weights = nn.functional.softmax(attn_scores, dim=-1) attn_weights = attn_weights.to(value.dtype) @@ -329,6 +336,7 @@ def forward( layer_past: Optional[Cache] = None, use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, + cache_position: Optional[torch.LongTensor] = None, ): # Compute QKV # Attention heads [batch, seq_len, hidden_size] @@ -372,7 +380,12 @@ def forward( # Cache QKV values if layer_past is not None: - cache_kwargs = {"sin": sin, "cos": cos, "partial_rotation_size": self.rotary_emb.dim} + cache_kwargs = { + "sin": sin, + "cos": cos, + "partial_rotation_size": self.rotary_emb.dim, + "cache_position": cache_position, + } key, value = layer_past.update(key, value, self.layer_idx, cache_kwargs) # GPT-neo-X casts query and key in fp32 to apply rotary embedding in full precision @@ -697,6 +710,7 @@ def forward( use_cache: Optional[bool] = False, layer_past: Optional[Cache] = None, output_attentions: Optional[bool] = False, + cache_position: Optional[torch.LongTensor] = None, ): attention_layer_outputs = self.attention( self.input_layernorm(hidden_states), @@ -706,6 +720,7 @@ def forward( head_mask=head_mask, use_cache=use_cache, output_attentions=output_attentions, + cache_position=cache_position, ) attn_output = attention_layer_outputs[0] # output_attn: attn_output, present, (attn_weights) attn_output = self.post_attention_dropout(attn_output) @@ -801,6 +816,10 @@ def forward( more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~file_utils.ModelOutput`] instead of a plain tuple. + cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): + Indices depicting the position of the input sequence tokens in the sequence. Contrarily to `position_ids`, + this tensor is not affected by padding. It is used to update the cache in the correct position and to infer + the complete sequence length. """ @@ -849,13 +868,9 @@ def forward( output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, + cache_position: Optional[torch.LongTensor] = None, ) -> Union[Tuple, BaseModelOutputWithPast]: r""" - past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))` of length `config.n_layers` with each tuple having 4 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): - Contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. - If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that - don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all - `decoder_input_ids` of shape `(batch_size, sequence_length)`. use_cache (`bool`, *optional*): If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see `past_key_values`). @@ -867,51 +882,43 @@ def forward( return_dict = return_dict if return_dict is not None else self.config.use_return_dict use_cache = use_cache if use_cache is not None else self.config.use_cache - if input_ids is not None and inputs_embeds is not None: - raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time") - elif input_ids is not None: - self.warn_if_padding_and_no_attention_mask(input_ids, attention_mask) - input_shape = input_ids.size() - elif inputs_embeds is not None: - input_shape = inputs_embeds.size()[:-1] - else: - raise ValueError("You have to specify either input_ids or inputs_embeds") + if (input_ids is None) ^ (inputs_embeds is not None): + raise ValueError( + "You cannot specify both input_ids and inputs_embeds at the same time, and must specify either one" + ) - batch_size, seq_length = input_shape + if self.gradient_checkpointing and self.training: + if use_cache: + logger.warning_once( + "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." + ) + use_cache = False - past_length = 0 - if use_cache: - use_legacy_cache = not isinstance(past_key_values, Cache) - if use_legacy_cache: - past_key_values = DynamicCache.from_legacy_cache(past_key_values) - past_length = past_key_values.get_usable_length(seq_length) + if inputs_embeds is None: + inputs_embeds = self.embed_in(input_ids) + + input_shape = inputs_embeds.shape[:-1] + use_legacy_cache = False + if use_cache and not isinstance(past_key_values, Cache): # kept for BC (non `Cache` `past_key_values` inputs) + use_legacy_cache = True + past_key_values = DynamicCache.from_legacy_cache(past_key_values) + logger.warning_once( + "Using `past_key_values` as a tuple is deprecated and will be removed in v4.43. " + "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" + ) + + if cache_position is None: + past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0 + cache_position = torch.arange( + past_seen_tokens, past_seen_tokens + input_shape[-1], device=inputs_embeds.device + ) if position_ids is None: - device = input_ids.device if input_ids is not None else inputs_embeds.device - position_ids = torch.arange(past_length, seq_length + past_length, dtype=torch.long, device=device) - position_ids = position_ids.unsqueeze(0) + position_ids = cache_position.unsqueeze(0) - # Attention mask. - if attention_mask is not None: - assert batch_size > 0, "batch_size has to be defined and > 0" - attention_mask = attention_mask.view(batch_size, -1) - if self._use_flash_attention_2: - attention_mask = attention_mask if 0 in attention_mask else None - else: - # We create a 3D attention mask from a 2D tensor mask. - # Sizes are [batch_size, 1, 1, to_seq_length] - # So we can broadcast to [batch_size, num_heads, from_seq_length, to_seq_length] - # this attention mask is more simple than the triangular masking of causal attention - # used in OpenAI GPT, we just need to prepare the broadcast dimension here. - attention_mask = attention_mask[:, None, None, :] - - # Since attention_mask is 1.0 for positions we want to attend and 0.0 for - # masked positions, this operation will create a tensor which is 0.0 for - # positions we want to attend and the dtype's smallest value for masked positions. - # Since we are adding it to the raw scores before the softmax, this is - # effectively the same as removing these entirely. - attention_mask = attention_mask.to(dtype=self.dtype) # fp16 compatibility - attention_mask = (1.0 - attention_mask) * torch.finfo(self.dtype).min + causal_mask = self._update_causal_mask( + attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions + ) # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head @@ -919,19 +926,8 @@ def forward( # input head_mask has shape [num_heads] or [num_hidden_layers x num_heads] # and head_mask is converted to shape [num_hidden_layers x batch x num_heads x seq_length x seq_length] head_mask = self.get_head_mask(head_mask, self.config.num_hidden_layers) - - if inputs_embeds is None: - inputs_embeds = self.embed_in(input_ids) - hidden_states = self.emb_dropout(inputs_embeds) - if self.gradient_checkpointing and self.training: - if use_cache: - logger.warning( - "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." - ) - use_cache = False - next_decoder_cache = None all_attentions = () if output_attentions else None all_hidden_states = () if output_hidden_states else None @@ -945,22 +941,24 @@ def forward( outputs = self._gradient_checkpointing_func( layer.__call__, hidden_states, - attention_mask, + causal_mask, position_ids, head_mask[i], use_cache, None, output_attentions, + cache_position, ) else: outputs = layer( hidden_states, - attention_mask=attention_mask, + attention_mask=causal_mask, position_ids=position_ids, head_mask=head_mask[i], layer_past=past_key_values, use_cache=use_cache, output_attentions=output_attentions, + cache_position=cache_position, ) hidden_states = outputs[0] if use_cache is True: @@ -987,6 +985,85 @@ def forward( attentions=all_attentions, ) + def _update_causal_mask( + self, + attention_mask: torch.Tensor, + input_tensor: torch.Tensor, + cache_position: torch.Tensor, + past_key_values: Cache, + output_attentions: bool, + ): + # TODO: As of torch==2.2.0, the `attention_mask` passed to the model in `generate` is 2D and of dynamic length even when the static + # KV cache is used. This is an issue for torch.compile which then recaptures cudagraphs at each decode steps due to the dynamic shapes. + # (`recording cudagraph tree for symint key 13`, etc.), which is VERY slow. A workaround is `@torch.compiler.disable`, but this prevents using + # `fullgraph=True`. See more context in https://github.com/huggingface/transformers/pull/29114 + + if self.config._attn_implementation == "flash_attention_2": + if attention_mask is not None and 0.0 in attention_mask: + return attention_mask + return None + + # For SDPA, when possible, we will rely on its `is_causal` argument instead of its `attn_mask` argument, in + # order to dispatch on Flash Attention 2. This feature is not compatible with static cache, as SDPA will fail + # to infer the attention mask. + past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0 + using_static_cache = isinstance(past_key_values, StaticCache) + + # When output attentions is True, sdpa implementation's forward method calls the eager implementation's forward + if self.config._attn_implementation == "sdpa" and not using_static_cache and not output_attentions: + if AttentionMaskConverter._ignore_causal_mask_sdpa( + attention_mask, + inputs_embeds=input_tensor, + past_key_values_length=past_seen_tokens, + is_training=self.training, + ): + return None + + dtype, device = input_tensor.dtype, input_tensor.device + min_dtype = torch.finfo(dtype).min + sequence_length = input_tensor.shape[1] + if using_static_cache: + target_length = past_key_values.get_max_length() + else: + target_length = ( + attention_mask.shape[-1] + if isinstance(attention_mask, torch.Tensor) + else past_seen_tokens + sequence_length + 1 + ) + if attention_mask is not None and attention_mask.dim() == 4: + # in this case we assume that the mask comes already in inverted form and requires no inversion or slicing + if attention_mask.max() != 0: + raise ValueError("Custom 4D attention mask should be passed in inverted form with max==0`") + causal_mask = attention_mask + else: + causal_mask = torch.full( + (sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device + ) + if sequence_length != 1: + causal_mask = torch.triu(causal_mask, diagonal=1) + causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1) + causal_mask = causal_mask[None, None, :, :].expand(input_tensor.shape[0], 1, -1, -1) + if attention_mask is not None: + causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit + mask_length = attention_mask.shape[-1] + padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :] + padding_mask = padding_mask == 0 + causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill( + padding_mask, min_dtype + ) + if ( + self.config._attn_implementation == "sdpa" + and attention_mask is not None + and attention_mask.device.type == "cuda" + and not output_attentions + ): + # Attend to all tokens in fully masked rows in the causal_mask, for example the relevant first rows when + # using left padding. This is required by F.scaled_dot_product_attention memory-efficient attention path. + # Details: https://github.com/pytorch/pytorch/issues/110213 + causal_mask = AttentionMaskConverter._unmask_unattended(causal_mask, min_dtype) + + return causal_mask + @add_start_docstrings( """GPTNeoX Model with a `language modeling` head on top for CLM fine-tuning.""", GPT_NEOX_START_DOCSTRING @@ -1024,20 +1101,9 @@ def forward( output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, + cache_position: Optional[torch.LongTensor] = None, ) -> Union[Tuple, CausalLMOutputWithPast]: r""" - past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): - Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape - `(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape - `(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`. The two additional tensors are - only required when the model is used as a decoder in a Sequence to Sequence model. - - Contains pre-computed hidden-states (key and values in the self-attention blocks that can be used (see - `past_key_values` input) to speed up sequential decoding. - - If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that - don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all - `decoder_input_ids` of shape `(batch_size, sequence_length)`. labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in `[-100, 0, ..., config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are @@ -1077,6 +1143,7 @@ def forward( output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, + cache_position=cache_position, ) hidden_states = outputs[0] @@ -1105,14 +1172,25 @@ def forward( ) def prepare_inputs_for_generation( - self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs + self, + input_ids, + past_key_values=None, + attention_mask=None, + inputs_embeds=None, + cache_position=None, + use_cache=True, + **kwargs, ): - input_shape = input_ids.shape past_length = 0 - # cut decoder_input_ids if past is used + # cut decoder_input_ids if paspast_length = cache_position[0] if cache_position is not None else past_key_values.get_seq_length() if past_key_values is not None: - past_length = cache_length = past_key_values.get_seq_length() - max_cache_length = past_key_values.get_max_length() + past_length = cache_position[0] if cache_position is not None else past_key_values.get_seq_length() + max_cache_length = ( + torch.tensor(past_key_values.get_max_length(), device=input_ids.device) + if past_key_values.get_max_length() is not None + else None + ) + cache_length = past_length if max_cache_length is None else torch.min(max_cache_length, past_length) # Some generation methods already pass only the last input ID if input_ids.shape[1] > past_length: @@ -1140,19 +1218,28 @@ def prepare_inputs_for_generation( # if model is used as a decoder in encoder-decoder model, the decoder attention mask is created on the fly if attention_mask is None: - attention_mask = input_ids.new_ones(input_shape) + attention_mask = input_ids.new_ones(input_ids.shape) # if `inputs_embeds` are passed, we only want to use them in the 1st generation step if inputs_embeds is not None and past_length == 0: model_inputs = {"inputs_embeds": inputs_embeds} else: model_inputs = {"input_ids": input_ids} + + if use_cache: + input_length = input_ids.shape[-1] + if cache_position is None: + cache_position = torch.arange(past_length, past_length + input_length, device=input_ids.device) + else: + cache_position = cache_position[-input_length:] + model_inputs.update( { "attention_mask": attention_mask, "past_key_values": past_key_values, "position_ids": position_ids, - "use_cache": kwargs.get("use_cache"), + "use_cache": use_cache, + "cache_position": cache_position, } ) diff --git a/src/transformers/models/gptj/modeling_gptj.py b/src/transformers/models/gptj/modeling_gptj.py index ead2ed7a100a..e67834cce0d7 100644 --- a/src/transformers/models/gptj/modeling_gptj.py +++ b/src/transformers/models/gptj/modeling_gptj.py @@ -25,7 +25,8 @@ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss from ...activations import ACT2FN -from ...cache_utils import Cache, DynamicCache +from ...cache_utils import Cache, DynamicCache, StaticCache +from ...modeling_attn_mask_utils import AttentionMaskConverter from ...modeling_outputs import ( BaseModelOutputWithPast, CausalLMOutputWithPast, @@ -193,9 +194,9 @@ def _attn( attn_weights = attn_weights / self.scale_attn - if attention_mask is not None: - # Apply the attention mask - attn_weights = attn_weights + attention_mask + if attention_mask is not None: # no matter the length, we just slice it + causal_mask = attention_mask[:, :, :, : key.shape[-2]] + attn_weights = attn_weights + causal_mask attn_weights = nn.functional.softmax(attn_weights, dim=-1) attn_weights = attn_weights.to(value.dtype) @@ -225,6 +226,7 @@ def forward( head_mask: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, + cache_position: Optional[torch.LongTensor] = None, ) -> Union[ Tuple[torch.Tensor, Tuple[torch.Tensor]], Optional[Tuple[torch.Tensor, Tuple[torch.Tensor], Tuple[torch.Tensor, ...]]], @@ -270,7 +272,12 @@ def forward( # Note that this cast is quite ugly, but is not implemented before ROPE as the original codebase keeps the key in float32 all along the computation. # Reference: https://github.com/kingoflolz/mesh-transformer-jax/blob/f8315e3003033b23f21d78361b288953064e0e76/mesh_transformer/layers.py#L128 if layer_past is not None: - cache_kwargs = {"sin": sin, "cos": cos, "partial_rotation_size": self.rotary_dim} + cache_kwargs = { + "sin": sin, + "cos": cos, + "partial_rotation_size": self.rotary_dim, + "cache_position": cache_position, + } key, value = layer_past.update(key.to(hidden_states.dtype), value, self.layer_idx, cache_kwargs) # compute self-attention: V x Softmax(QK^T) @@ -311,6 +318,7 @@ def forward( head_mask: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, + cache_position: Optional[torch.LongTensor] = None, ) -> Union[ Tuple[torch.Tensor, Tuple[torch.Tensor]], Optional[Tuple[torch.Tensor, Tuple[torch.Tensor], Tuple[torch.Tensor, ...]]], @@ -360,7 +368,12 @@ def forward( # Note that this cast is quite ugly, but is not implemented before ROPE as the original codebase keeps the key in float32 all along the computation. # Reference: https://github.com/kingoflolz/mesh-transformer-jax/blob/f8315e3003033b23f21d78361b288953064e0e76/mesh_transformer/layers.py#L128 if layer_past is not None: - cache_kwargs = {"sin": sin, "cos": cos, "partial_rotation_size": self.rotary_dim} + cache_kwargs = { + "sin": sin, + "cos": cos, + "partial_rotation_size": self.rotary_dim, + "cache_position": cache_position, + } key, value = layer_past.update(key.to(hidden_states.dtype), value, self.layer_idx, cache_kwargs) # The Flash attention requires the input to have the shape @@ -566,6 +579,7 @@ def forward( head_mask: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, + cache_position: Optional[torch.LongTensor] = None, ) -> Union[Tuple[torch.Tensor], Optional[Tuple[torch.Tensor, Tuple[torch.FloatTensor, ...]]]]: residual = hidden_states hidden_states = self.ln_1(hidden_states) @@ -577,6 +591,7 @@ def forward( head_mask=head_mask, use_cache=use_cache, output_attentions=output_attentions, + cache_position=cache_position, ) attn_output = attn_outputs[0] # output_attn: a, present, (attentions) outputs = attn_outputs[1:] @@ -702,6 +717,10 @@ def _init_weights(self, module): more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. + cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): + Indices depicting the position of the input sequence tokens in the sequence. Contrarily to `position_ids`, + this tensor is not affected by padding. It is used to update the cache in the correct position and to infer + the complete sequence length. """ PARALLELIZE_DOCSTRING = r""" @@ -846,6 +865,7 @@ def forward( output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, + cache_position: Optional[torch.LongTensor] = None, ) -> Union[Tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( @@ -854,81 +874,59 @@ def forward( use_cache = use_cache if use_cache is not None else self.config.use_cache return_dict = return_dict if return_dict is not None else self.config.use_return_dict - if input_ids is not None and inputs_embeds is not None: - raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time") - elif input_ids is not None: - self.warn_if_padding_and_no_attention_mask(input_ids, attention_mask) - input_shape = input_ids.size() - input_ids = input_ids.view(-1, input_shape[-1]) - batch_size = input_ids.shape[0] - elif inputs_embeds is not None: - input_shape = inputs_embeds.size()[:-1] - batch_size = inputs_embeds.shape[0] - else: - raise ValueError("You have to specify either input_ids or inputs_embeds") + if (input_ids is None) ^ (inputs_embeds is not None): + raise ValueError( + "You cannot specify both input_ids and inputs_embeds at the same time, and must specify either one" + ) - device = input_ids.device if input_ids is not None else inputs_embeds.device + if self.gradient_checkpointing and self.training: + if use_cache: + logger.warning_once( + "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." + ) + use_cache = False - if token_type_ids is not None: - token_type_ids = token_type_ids.view(-1, input_shape[-1]) + if inputs_embeds is None: + inputs_embeds = self.wte(input_ids) - past_length = 0 - if use_cache: - use_legacy_cache = not isinstance(past_key_values, Cache) - if use_legacy_cache: - past_key_values = DynamicCache.from_legacy_cache(past_key_values) - past_length = past_key_values.get_seq_length() + # Compute alibi tensor: check build_alibi_tensor documentation + use_legacy_cache = False + if use_cache and not isinstance(past_key_values, Cache): # kept for BC (non `Cache` `past_key_values` inputs) + use_legacy_cache = True + past_key_values = DynamicCache.from_legacy_cache(past_key_values) + logger.warning_once( + "Using `past_key_values` as a tuple is deprecated and will be removed in v4.43. " + "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" + ) + + seq_length = inputs_embeds.shape[1] + if cache_position is None: + past_key_values_length = past_key_values.get_seq_length() if use_cache else 0 + cache_position = torch.arange( + past_key_values_length, past_key_values_length + seq_length, device=inputs_embeds.device + ) if position_ids is None: - position_ids = torch.arange(past_length, input_shape[-1] + past_length, dtype=torch.long, device=device) - position_ids = position_ids.unsqueeze(0) + position_ids = cache_position.unsqueeze(0) - if not self._use_flash_attention_2: - # Attention mask. - if attention_mask is not None: - if batch_size <= 0: - raise ValueError("batch_size has to be defined and > 0") - attention_mask = attention_mask.view(batch_size, -1) - # We create a 3D attention mask from a 2D tensor mask. - # Sizes are [batch_size, 1, 1, to_seq_length] - # So we can broadcast to [batch_size, num_heads, from_seq_length, to_seq_length] - # this attention mask is more simple than the triangular masking of causal attention - # used in OpenAI GPT, we just need to prepare the broadcast dimension here. - attention_mask = attention_mask[:, None, None, :] - - # Since attention_mask is 1.0 for positions we want to attend and 0.0 for - # masked positions, this operation will create a tensor which is 0.0 for - # positions we want to attend and the dtype's smallest value for masked positions. - # Since we are adding it to the raw scores before the softmax, this is - # effectively the same as removing these entirely. - attention_mask = attention_mask.to(dtype=self.dtype) # fp16 compatibility - attention_mask = (1.0 - attention_mask) * torch.finfo(self.dtype).min + causal_mask = self._update_causal_mask( + attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions + ) # Prepare head mask if needed # 1.0 in head_mask indicate we keep the head # attention_probs has shape bsz x num_attention_heads x N x N # head_mask has shape n_layer x batch x num_attention_heads x N x N head_mask = self.get_head_mask(head_mask, self.config.n_layer) - - if inputs_embeds is None: - inputs_embeds = self.wte(input_ids) - hidden_states = inputs_embeds if token_type_ids is not None: + token_type_ids = token_type_ids.view(-1, seq_length) token_type_embeds = self.wte(token_type_ids) hidden_states = hidden_states + token_type_embeds hidden_states = self.drop(hidden_states) - - output_shape = (-1,) + input_shape[1:] + (hidden_states.size(-1),) - - if self.gradient_checkpointing and self.training: - if use_cache: - logger.warning_once( - "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." - ) - use_cache = False + output_shape = (-1, seq_length, hidden_states.size(-1)) next_decoder_cache = None all_self_attentions = () if output_attentions else None @@ -944,8 +942,8 @@ def forward( past_key_values.value_cache = past_key_values.value_cache.to(hidden_states.device) # Ensure that attention_mask is always on the same device as hidden_states - if attention_mask is not None: - attention_mask = attention_mask.to(hidden_states.device) + if causal_mask is not None: + causal_mask = causal_mask.to(hidden_states.device) if isinstance(head_mask, torch.Tensor): head_mask = head_mask.to(hidden_states.device) if output_hidden_states: @@ -956,21 +954,23 @@ def forward( block.__call__, hidden_states, None, - attention_mask, + causal_mask, position_ids, head_mask[i], use_cache, output_attentions, + cache_position, ) else: outputs = block( hidden_states=hidden_states, layer_past=past_key_values, - attention_mask=attention_mask, + attention_mask=causal_mask, position_ids=position_ids, head_mask=head_mask[i], use_cache=use_cache, output_attentions=output_attentions, + cache_position=cache_position, ) hidden_states = outputs[0] @@ -1009,6 +1009,86 @@ def forward( attentions=all_self_attentions, ) + def _update_causal_mask( + self, + attention_mask: torch.Tensor, + input_tensor: torch.Tensor, + cache_position: torch.Tensor, + past_key_values: Cache, + output_attentions: bool, + ): + # TODO: As of torch==2.2.0, the `attention_mask` passed to the model in `generate` is 2D and of dynamic length even when the static + # KV cache is used. This is an issue for torch.compile which then recaptures cudagraphs at each decode steps due to the dynamic shapes. + # (`recording cudagraph tree for symint key 13`, etc.), which is VERY slow. A workaround is `@torch.compiler.disable`, but this prevents using + # `fullgraph=True`. See more context in https://github.com/huggingface/transformers/pull/29114 + + if self.config._attn_implementation == "flash_attention_2": + if attention_mask is not None and 0.0 in attention_mask: + return attention_mask + return None + + # For SDPA, when possible, we will rely on its `is_causal` argument instead of its `attn_mask` argument, in + # order to dispatch on Flash Attention 2. This feature is not compatible with static cache, as SDPA will fail + # to infer the attention mask. + past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0 + using_static_cache = isinstance(past_key_values, StaticCache) + + # When output attentions is True, sdpa implementation's forward method calls the eager implementation's forward + if self.config._attn_implementation == "sdpa" and not using_static_cache and not output_attentions: + if AttentionMaskConverter._ignore_causal_mask_sdpa( + attention_mask, + inputs_embeds=input_tensor, + past_key_values_length=past_seen_tokens, + is_training=self.training, + ): + return None + + dtype, device = input_tensor.dtype, input_tensor.device + min_dtype = torch.finfo(dtype).min + sequence_length = input_tensor.shape[1] + if using_static_cache: + target_length = past_key_values.get_max_length() + else: + target_length = ( + attention_mask.shape[-1] + if isinstance(attention_mask, torch.Tensor) + else past_seen_tokens + sequence_length + 1 + ) + + if attention_mask is not None and attention_mask.dim() == 4: + # in this case we assume that the mask comes already in inverted form and requires no inversion or slicing + if attention_mask.max() != 0: + raise ValueError("Custom 4D attention mask should be passed in inverted form with max==0`") + causal_mask = attention_mask + else: + causal_mask = torch.full( + (sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device + ) + if sequence_length != 1: + causal_mask = torch.triu(causal_mask, diagonal=1) + causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1) + causal_mask = causal_mask[None, None, :, :].expand(input_tensor.shape[0], 1, -1, -1) + if attention_mask is not None: + causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit + mask_length = attention_mask.shape[-1] + padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :] + padding_mask = padding_mask == 0 + causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill( + padding_mask, min_dtype + ) + if ( + self.config._attn_implementation == "sdpa" + and attention_mask is not None + and attention_mask.device.type == "cuda" + and not output_attentions + ): + # Attend to all tokens in fully masked rows in the causal_mask, for example the relevant first rows when + # using left padding. This is required by F.scaled_dot_product_attention memory-efficient attention path. + # Details: https://github.com/pytorch/pytorch/issues/110213 + causal_mask = AttentionMaskConverter._unmask_unattended(causal_mask, min_dtype) + + return causal_mask + @add_start_docstrings( """ @@ -1068,14 +1148,28 @@ def get_output_embeddings(self): def set_output_embeddings(self, new_embeddings): self.lm_head = new_embeddings - def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_embeds=None, **kwargs): + # Copied from transformers.models.gpt_neo.modeling_gpt_neo.GPTNeoForCausalLM.prepare_inputs_for_generation + def prepare_inputs_for_generation( + self, + input_ids, + attention_mask=None, + past_key_values=None, + inputs_embeds=None, + cache_position=None, + use_cache=True, + **kwargs, + ): token_type_ids = kwargs.get("token_type_ids", None) - attention_mask = kwargs.get("attention_mask", None) past_length = 0 # Omit tokens covered by past_key_values if past_key_values is not None: - past_length = cache_length = past_key_values.get_seq_length() - max_cache_length = past_key_values.get_max_length() + past_length = cache_position[0] if cache_position is not None else past_key_values.get_seq_length() + max_cache_length = ( + torch.tensor(past_key_values.get_max_length(), device=input_ids.device) + if past_key_values.get_max_length() is not None + else None + ) + cache_length = past_length if max_cache_length is None else torch.min(max_cache_length, past_length) # Some generation methods already pass only the last input ID if input_ids.shape[1] > past_length: @@ -1085,6 +1179,7 @@ def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_ remove_prefix_length = input_ids.shape[1] - 1 input_ids = input_ids[:, remove_prefix_length:] + if token_type_ids is not None: token_type_ids = token_type_ids[:, -input_ids.shape[1] :] @@ -1097,7 +1192,6 @@ def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_ attention_mask = attention_mask[:, -max_cache_length:] position_ids = kwargs.get("position_ids", None) - if attention_mask is not None and position_ids is None: # create position_ids on the fly for batch generation position_ids = attention_mask.long().cumsum(-1) - 1 @@ -1111,13 +1205,21 @@ def prepare_inputs_for_generation(self, input_ids, past_key_values=None, inputs_ else: model_inputs = {"input_ids": input_ids} + if use_cache: + input_length = input_ids.shape[-1] + if cache_position is None: + cache_position = torch.arange(past_length, past_length + input_length, device=input_ids.device) + else: + cache_position = cache_position[-input_length:] + model_inputs.update( { "past_key_values": past_key_values, - "use_cache": kwargs.get("use_cache"), + "use_cache": use_cache, "position_ids": position_ids, "attention_mask": attention_mask, "token_type_ids": token_type_ids, + "cache_position": cache_position, } ) @@ -1144,6 +1246,7 @@ def forward( output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, + cache_position: Optional[torch.LongTensor] = None, ) -> Union[Tuple, CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): @@ -1165,6 +1268,7 @@ def forward( output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, + cache_position=cache_position, ) hidden_states = transformer_outputs[0] From 0588791eae5e1c99eb96b2ed6d948c0bbb197fa7 Mon Sep 17 00:00:00 2001 From: raushan Date: Wed, 19 Jun 2024 12:01:01 +0200 Subject: [PATCH 15/40] copied from statements --- src/transformers/models/codegen/modeling_codegen.py | 13 +++++-------- src/transformers/models/falcon/modeling_falcon.py | 1 + src/transformers/models/gpt_neo/modeling_gpt_neo.py | 12 +++++------- .../models/gpt_neox/modeling_gpt_neox.py | 7 +++---- src/transformers/models/gptj/modeling_gptj.py | 1 + 5 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/transformers/models/codegen/modeling_codegen.py b/src/transformers/models/codegen/modeling_codegen.py index e9a32fa902fc..4168f5b48993 100644 --- a/src/transformers/models/codegen/modeling_codegen.py +++ b/src/transformers/models/codegen/modeling_codegen.py @@ -431,8 +431,6 @@ def __init__(self, config): self.drop = nn.Dropout(config.embd_pdrop) self.h = nn.ModuleList([CodeGenBlock(config, layer_idx=i) for i in range(config.n_layer)]) self.ln_f = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_epsilon) - self.rotary_dim = min(config.rotary_dim, config.n_ctx // config.num_attention_heads) - self.gradient_checkpointing = False # Initialize weights and apply final processing @@ -496,12 +494,10 @@ def forward( "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" ) - input_shape = inputs_embeds.shape[:-1] + seq_length = inputs_embeds.shape[1] if cache_position is None: past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0 - cache_position = torch.arange( - past_seen_tokens, past_seen_tokens + input_shape[-1], device=inputs_embeds.device - ) + cache_position = torch.arange(past_seen_tokens, past_seen_tokens + seq_length, device=inputs_embeds.device) if position_ids is None: position_ids = cache_position.unsqueeze(0) @@ -518,12 +514,12 @@ def forward( hidden_states = inputs_embeds if token_type_ids is not None: - token_type_ids = token_type_ids.view(-1, input_shape[-1]) + token_type_ids = token_type_ids.view(-1, seq_length) token_type_embeds = self.wte(token_type_ids) hidden_states = hidden_states + token_type_embeds hidden_states = self.drop(hidden_states) - output_shape = input_shape + (hidden_states.size(-1),) + output_shape = (-1, seq_length, hidden_states.size(-1)) next_decoder_cache = None all_self_attentions = () if output_attentions else None @@ -586,6 +582,7 @@ def forward( attentions=all_self_attentions, ) + # Copied from transformers.models.llama.modeling_llama.LlamaModel._update_causal_mask def _update_causal_mask( self, attention_mask: torch.Tensor, diff --git a/src/transformers/models/falcon/modeling_falcon.py b/src/transformers/models/falcon/modeling_falcon.py index 62c7f2fabdd3..7c59a0a30295 100644 --- a/src/transformers/models/falcon/modeling_falcon.py +++ b/src/transformers/models/falcon/modeling_falcon.py @@ -1175,6 +1175,7 @@ def forward( attentions=all_self_attentions, ) + # Copied from transformers.models.llama.modeling_llama.LlamaModel._update_causal_mask def _update_causal_mask( self, attention_mask: torch.Tensor, diff --git a/src/transformers/models/gpt_neo/modeling_gpt_neo.py b/src/transformers/models/gpt_neo/modeling_gpt_neo.py index 6dacdf7e782f..eebca8dc872c 100755 --- a/src/transformers/models/gpt_neo/modeling_gpt_neo.py +++ b/src/transformers/models/gpt_neo/modeling_gpt_neo.py @@ -734,7 +734,6 @@ def __init__(self, config): self.wpe = nn.Embedding(config.max_position_embeddings, self.embed_dim) self.drop = nn.Dropout(float(config.embed_dropout)) self.h = nn.ModuleList([GPTNeoBlock(config, layer_id=i) for i in range(config.num_layers)]) - self._use_flash_attention_2 = config._attn_implementation == "flash_attention_2" self.ln_f = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_epsilon) self.gradient_checkpointing = False @@ -790,7 +789,6 @@ def forward( if inputs_embeds is None: inputs_embeds = self.wte(input_ids) - input_shape = inputs_embeds.shape[:-1] use_legacy_cache = False if use_cache and not isinstance(past_key_values, Cache): # kept for BC (non `Cache` `past_key_values` inputs) use_legacy_cache = True @@ -800,11 +798,10 @@ def forward( "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" ) + seq_length = inputs_embeds.shape[1] if cache_position is None: past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0 - cache_position = torch.arange( - past_seen_tokens, past_seen_tokens + input_shape[-1], device=inputs_embeds.device - ) + cache_position = torch.arange(past_seen_tokens, past_seen_tokens + seq_length, device=inputs_embeds.device) if position_ids is None: position_ids = cache_position.unsqueeze(0) @@ -822,12 +819,12 @@ def forward( hidden_states = inputs_embeds + position_embeds if token_type_ids is not None: - token_type_ids = token_type_ids.view(-1, input_shape[-1]) + token_type_ids = token_type_ids.view(-1, seq_length) token_type_embeds = self.wte(token_type_ids) hidden_states = hidden_states + token_type_embeds hidden_states = self.drop(hidden_states) - output_shape = (-1,) + input_shape[1:] + (hidden_states.size(-1),) + output_shape = (-1, seq_length, hidden_states.size(-1)) next_decoder_cache = None all_self_attentions = () if output_attentions else None @@ -888,6 +885,7 @@ def forward( attentions=all_self_attentions, ) + # Copied from transformers.models.llama.modeling_llama.LlamaModel._update_causal_mask def _update_causal_mask( self, attention_mask: torch.Tensor, diff --git a/src/transformers/models/gpt_neox/modeling_gpt_neox.py b/src/transformers/models/gpt_neox/modeling_gpt_neox.py index ebc7382141f6..d8b70c4a4d78 100755 --- a/src/transformers/models/gpt_neox/modeling_gpt_neox.py +++ b/src/transformers/models/gpt_neox/modeling_gpt_neox.py @@ -897,7 +897,6 @@ def forward( if inputs_embeds is None: inputs_embeds = self.embed_in(input_ids) - input_shape = inputs_embeds.shape[:-1] use_legacy_cache = False if use_cache and not isinstance(past_key_values, Cache): # kept for BC (non `Cache` `past_key_values` inputs) use_legacy_cache = True @@ -907,11 +906,10 @@ def forward( "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" ) + seq_length = inputs_embeds.shape[1] if cache_position is None: past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0 - cache_position = torch.arange( - past_seen_tokens, past_seen_tokens + input_shape[-1], device=inputs_embeds.device - ) + cache_position = torch.arange(past_seen_tokens, past_seen_tokens + seq_length, device=inputs_embeds.device) if position_ids is None: position_ids = cache_position.unsqueeze(0) @@ -985,6 +983,7 @@ def forward( attentions=all_attentions, ) + # Copied from transformers.models.llama.modeling_llama.LlamaModel._update_causal_mask def _update_causal_mask( self, attention_mask: torch.Tensor, diff --git a/src/transformers/models/gptj/modeling_gptj.py b/src/transformers/models/gptj/modeling_gptj.py index e67834cce0d7..362c56321514 100644 --- a/src/transformers/models/gptj/modeling_gptj.py +++ b/src/transformers/models/gptj/modeling_gptj.py @@ -1009,6 +1009,7 @@ def forward( attentions=all_self_attentions, ) + # Copied from transformers.models.llama.modeling_llama.LlamaModel._update_causal_mask def _update_causal_mask( self, attention_mask: torch.Tensor, From a27b47c61a25995d4de61562a31bfc961eed016a Mon Sep 17 00:00:00 2001 From: raushan Date: Wed, 19 Jun 2024 12:10:35 +0200 Subject: [PATCH 16/40] revert --- src/transformers/models/codegen/modeling_codegen.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/transformers/models/codegen/modeling_codegen.py b/src/transformers/models/codegen/modeling_codegen.py index 4168f5b48993..7202ff778050 100644 --- a/src/transformers/models/codegen/modeling_codegen.py +++ b/src/transformers/models/codegen/modeling_codegen.py @@ -431,6 +431,8 @@ def __init__(self, config): self.drop = nn.Dropout(config.embd_pdrop) self.h = nn.ModuleList([CodeGenBlock(config, layer_idx=i) for i in range(config.n_layer)]) self.ln_f = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_epsilon) + self.rotary_dim = min(config.rotary_dim, config.n_ctx // config.num_attention_heads) + self.gradient_checkpointing = False # Initialize weights and apply final processing From 1abcf305d2f8ecb9b510e4f04bc42818a774146f Mon Sep 17 00:00:00 2001 From: raushan Date: Wed, 19 Jun 2024 12:51:08 +0200 Subject: [PATCH 17/40] tmp --- bart.py | 16 ++++++++++++++++ update_llava.py | 0 2 files changed, 16 insertions(+) create mode 100644 bart.py create mode 100644 update_llava.py diff --git a/bart.py b/bart.py new file mode 100644 index 000000000000..01262fd86b6a --- /dev/null +++ b/bart.py @@ -0,0 +1,16 @@ +from transformers import AutoTokenizer, BartForConditionalGeneration + +model = BartForConditionalGeneration.from_pretrained("facebook/bart-large-cnn").to("cuda:0") +tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn") + +ARTICLE_TO_SUMMARIZE = ( + "PG&E stated it scheduled the blackouts in response to forecasts for high winds " + "amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were " + "scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow." +) +inputs = tokenizer(ARTICLE_TO_SUMMARIZE, return_tensors="pt").to("cuda:0") + +# Generate Summary +summary_ids = model.generate(**inputs, num_beams=1, do_sample=False, max_new_tokens=30, use_cache=False) +out = tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0] +print(out) diff --git a/update_llava.py b/update_llava.py new file mode 100644 index 000000000000..e69de29bb2d1 From 00ed88c4ba267aded857092f27e41f410a8c7c49 Mon Sep 17 00:00:00 2001 From: raushan Date: Thu, 20 Jun 2024 09:16:08 +0200 Subject: [PATCH 18/40] update warning msg --- src/transformers/models/codegen/modeling_codegen.py | 2 +- src/transformers/models/falcon/modeling_falcon.py | 2 +- src/transformers/models/gpt_neo/modeling_gpt_neo.py | 2 +- src/transformers/models/gpt_neox/modeling_gpt_neox.py | 2 +- src/transformers/models/gptj/modeling_gptj.py | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/transformers/models/codegen/modeling_codegen.py b/src/transformers/models/codegen/modeling_codegen.py index 7202ff778050..9b86d0f06e2d 100644 --- a/src/transformers/models/codegen/modeling_codegen.py +++ b/src/transformers/models/codegen/modeling_codegen.py @@ -492,7 +492,7 @@ def forward( use_legacy_cache = True past_key_values = DynamicCache.from_legacy_cache(past_key_values) logger.warning_once( - "Using `past_key_values` as a tuple is deprecated and will be removed in v4.43. " + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.43. " "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" ) diff --git a/src/transformers/models/falcon/modeling_falcon.py b/src/transformers/models/falcon/modeling_falcon.py index 7c59a0a30295..a1944f7b104a 100644 --- a/src/transformers/models/falcon/modeling_falcon.py +++ b/src/transformers/models/falcon/modeling_falcon.py @@ -1076,7 +1076,7 @@ def forward( use_legacy_cache = True past_key_values = DynamicCache.from_legacy_cache(past_key_values) logger.warning_once( - "Using `past_key_values` as a tuple is deprecated and will be removed in v4.43. " + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.43. " "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" ) diff --git a/src/transformers/models/gpt_neo/modeling_gpt_neo.py b/src/transformers/models/gpt_neo/modeling_gpt_neo.py index eebca8dc872c..2db666a0b5c0 100755 --- a/src/transformers/models/gpt_neo/modeling_gpt_neo.py +++ b/src/transformers/models/gpt_neo/modeling_gpt_neo.py @@ -794,7 +794,7 @@ def forward( use_legacy_cache = True past_key_values = DynamicCache.from_legacy_cache(past_key_values) logger.warning_once( - "Using `past_key_values` as a tuple is deprecated and will be removed in v4.43. " + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.43. " "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" ) diff --git a/src/transformers/models/gpt_neox/modeling_gpt_neox.py b/src/transformers/models/gpt_neox/modeling_gpt_neox.py index d8b70c4a4d78..e420f585ebcf 100755 --- a/src/transformers/models/gpt_neox/modeling_gpt_neox.py +++ b/src/transformers/models/gpt_neox/modeling_gpt_neox.py @@ -902,7 +902,7 @@ def forward( use_legacy_cache = True past_key_values = DynamicCache.from_legacy_cache(past_key_values) logger.warning_once( - "Using `past_key_values` as a tuple is deprecated and will be removed in v4.43. " + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.43. " "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" ) diff --git a/src/transformers/models/gptj/modeling_gptj.py b/src/transformers/models/gptj/modeling_gptj.py index 362c56321514..54bc5850cda6 100644 --- a/src/transformers/models/gptj/modeling_gptj.py +++ b/src/transformers/models/gptj/modeling_gptj.py @@ -895,7 +895,7 @@ def forward( use_legacy_cache = True past_key_values = DynamicCache.from_legacy_cache(past_key_values) logger.warning_once( - "Using `past_key_values` as a tuple is deprecated and will be removed in v4.43. " + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.43. " "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" ) From 6c3b3aa737d3d71e08660a8b9f21db4033630a25 Mon Sep 17 00:00:00 2001 From: raushan Date: Thu, 20 Jun 2024 09:16:20 +0200 Subject: [PATCH 19/40] forgot git --- src/transformers/models/git/modeling_git.py | 24 +++++++-------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/transformers/models/git/modeling_git.py b/src/transformers/models/git/modeling_git.py index ef9ac733909e..a84304d7ad78 100644 --- a/src/transformers/models/git/modeling_git.py +++ b/src/transformers/models/git/modeling_git.py @@ -417,10 +417,14 @@ def forward( ) use_cache = False - if use_cache: - use_legacy_cache = not isinstance(past_key_values, Cache) - if use_legacy_cache: - past_key_values = DynamicCache.from_legacy_cache(past_key_values) + use_legacy_cache = False + if use_cache and not isinstance(past_key_values, Cache): # kept for BC (non `Cache` `past_key_values` inputs) + use_legacy_cache = True + past_key_values = DynamicCache.from_legacy_cache(past_key_values) + logger.warning_once( + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.43. " + "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" + ) all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None @@ -1158,12 +1162,6 @@ def forward( return_dict: Optional[bool] = None, ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPooling]: r""" - past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))` of length `config.n_layers` with each tuple having 4 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): - Contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. - - If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that - don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all - `decoder_input_ids` of shape `(batch_size, sequence_length)`. use_cache (`bool`, *optional*): If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see `past_key_values`). @@ -1359,12 +1357,6 @@ def forward( Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in `[-100, 0, ..., config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are ignored (masked), the loss is only computed for the tokens with labels n `[0, ..., config.vocab_size]` - past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))` of length `config.n_layers` with each tuple having 4 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): - Contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. - - If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that - don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all - `decoder_input_ids` of shape `(batch_size, sequence_length)`. use_cache (`bool`, *optional*): If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see `past_key_values`). From fd5eeabbafed7a3b5cce1bbae3f98b589f5dd2b7 Mon Sep 17 00:00:00 2001 From: raushan Date: Fri, 21 Jun 2024 15:50:02 +0200 Subject: [PATCH 20/40] add more flags --- src/transformers/models/codegen/modeling_codegen.py | 2 ++ src/transformers/models/falcon/modeling_falcon.py | 2 ++ src/transformers/models/git/modeling_git.py | 1 + src/transformers/models/gpt_neo/modeling_gpt_neo.py | 2 ++ src/transformers/models/gpt_neox/modeling_gpt_neox.py | 2 ++ src/transformers/models/gptj/modeling_gptj.py | 2 ++ 6 files changed, 11 insertions(+) diff --git a/src/transformers/models/codegen/modeling_codegen.py b/src/transformers/models/codegen/modeling_codegen.py index 9b86d0f06e2d..e88206b6682a 100644 --- a/src/transformers/models/codegen/modeling_codegen.py +++ b/src/transformers/models/codegen/modeling_codegen.py @@ -314,6 +314,8 @@ class CodeGenPreTrainedModel(PreTrainedModel): _no_split_modules = ["CodeGenBlock"] _skip_keys_device_placement = "past_key_values" _supports_cache_class = True + _supports_quantized_cache = True + _supports_static_cache = True def __init__(self, *inputs, **kwargs): super().__init__(*inputs, **kwargs) diff --git a/src/transformers/models/falcon/modeling_falcon.py b/src/transformers/models/falcon/modeling_falcon.py index a1944f7b104a..0bd87f7d0eb9 100644 --- a/src/transformers/models/falcon/modeling_falcon.py +++ b/src/transformers/models/falcon/modeling_falcon.py @@ -954,6 +954,8 @@ class FalconPreTrainedModel(PreTrainedModel): _supports_flash_attn_2 = True _supports_sdpa = True _supports_cache_class = True + _supports_quantized_cache = True + _supports_static_cache = True def __init__(self, *inputs, **kwargs): super().__init__(*inputs, **kwargs) diff --git a/src/transformers/models/git/modeling_git.py b/src/transformers/models/git/modeling_git.py index a84304d7ad78..9aab084a59bc 100644 --- a/src/transformers/models/git/modeling_git.py +++ b/src/transformers/models/git/modeling_git.py @@ -496,6 +496,7 @@ class GitPreTrainedModel(PreTrainedModel): base_model_prefix = "git" supports_gradient_checkpointing = True _supports_cache_class = True + _supports_quantized_cache = True def _init_weights(self, module): """Initialize the weights""" diff --git a/src/transformers/models/gpt_neo/modeling_gpt_neo.py b/src/transformers/models/gpt_neo/modeling_gpt_neo.py index 2db666a0b5c0..c6f636d081b4 100755 --- a/src/transformers/models/gpt_neo/modeling_gpt_neo.py +++ b/src/transformers/models/gpt_neo/modeling_gpt_neo.py @@ -602,6 +602,8 @@ class GPTNeoPreTrainedModel(PreTrainedModel): _skip_keys_device_placement = "past_key_values" _supports_flash_attn_2 = True _supports_cache_class = True + _supports_quantized_cache = True + _supports_static_cache = True def __init__(self, *inputs, **kwargs): super().__init__(*inputs, **kwargs) diff --git a/src/transformers/models/gpt_neox/modeling_gpt_neox.py b/src/transformers/models/gpt_neox/modeling_gpt_neox.py index e420f585ebcf..adb8683b1607 100755 --- a/src/transformers/models/gpt_neox/modeling_gpt_neox.py +++ b/src/transformers/models/gpt_neox/modeling_gpt_neox.py @@ -81,6 +81,8 @@ class GPTNeoXPreTrainedModel(PreTrainedModel): _skip_keys_device_placement = "past_key_values" _supports_flash_attn_2 = True _supports_cache_class = True + _supports_quantized_cache = True + _supports_static_cache = True def _init_weights(self, module): """Initialize the weights""" diff --git a/src/transformers/models/gptj/modeling_gptj.py b/src/transformers/models/gptj/modeling_gptj.py index 54bc5850cda6..8de4f8a124e7 100644 --- a/src/transformers/models/gptj/modeling_gptj.py +++ b/src/transformers/models/gptj/modeling_gptj.py @@ -621,6 +621,8 @@ class GPTJPreTrainedModel(PreTrainedModel): _skip_keys_device_placement = "past_key_values" _supports_flash_attn_2 = True _supports_cache_class = True + _supports_quantized_cache = True + _supports_static_cache = True def __init__(self, *inputs, **kwargs): super().__init__(*inputs, **kwargs) From e233f2969039d5cd0b049cfe3275dbd8c9bb59cc Mon Sep 17 00:00:00 2001 From: raushan Date: Fri, 21 Jun 2024 15:54:50 +0200 Subject: [PATCH 21/40] run-slow git,codegen,gpt_neo,gpt_neox,gpj From 356d578d03c9b4cb8e43d009c2edfa4390dcb879 Mon Sep 17 00:00:00 2001 From: raushan Date: Tue, 9 Jul 2024 11:48:00 +0200 Subject: [PATCH 22/40] add cache flag to VLMs --- .../models/idefics/modeling_idefics.py | 206 ++++++++++++++---- .../models/llava/modeling_llava.py | 8 + .../models/llava_next/modeling_llava_next.py | 8 + .../models/vipllava/modeling_vipllava.py | 8 + 4 files changed, 189 insertions(+), 41 deletions(-) diff --git a/src/transformers/models/idefics/modeling_idefics.py b/src/transformers/models/idefics/modeling_idefics.py index 6d6582598609..38831579617e 100644 --- a/src/transformers/models/idefics/modeling_idefics.py +++ b/src/transformers/models/idefics/modeling_idefics.py @@ -30,7 +30,8 @@ from ... import PreTrainedModel from ...activations import ACT2FN -from ...modeling_attn_mask_utils import _prepare_4d_causal_attention_mask_for_sdpa +from ...cache_utils import Cache, DynamicCache, StaticCache +from ...modeling_attn_mask_utils import AttentionMaskConverter from ...modeling_outputs import ModelOutput from ...modeling_utils import PretrainedConfig from ...pytorch_utils import ALL_LAYERNORM_LAYERS @@ -184,8 +185,11 @@ def expand_inputs_for_generation( def prepare_inputs_for_generation(input_ids, past_key_values=None, **kwargs): token_type_ids = kwargs.get("token_type_ids", None) + cache_position = kwargs.get("cache_position", None) # only last token for inputs_ids if past is defined in kwargs + past_length = 0 if past_key_values: + past_length = cache_position[0] if cache_position is not None else past_key_values.get_seq_length() input_ids = input_ids[:, -1].unsqueeze(-1) if token_type_ids is not None: token_type_ids = token_type_ids[:, -1].unsqueeze(-1) @@ -206,10 +210,17 @@ def prepare_inputs_for_generation(input_ids, past_key_values=None, **kwargs): image_attention_mask = kwargs.get("image_attention_mask", None) interpolate_pos_encoding = kwargs.get("interpolate_pos_encoding", False) + input_length = position_ids.shape[-1] if position_ids is not None else input_ids.shape[-1] + if cache_position is None: + cache_position = torch.arange(past_length, past_length + input_length, device=input_ids.device) + elif kwargs.get("use_cache", False): + cache_position = cache_position[-input_length:] + return { "input_ids": input_ids, "past_key_values": past_key_values, "use_cache": kwargs.get("use_cache"), + "cache_position": cache_position, "position_ids": position_ids, "attention_mask": attention_mask, "token_type_ids": token_type_ids, @@ -538,6 +549,7 @@ def __init__( is_cross_attention: bool = False, config: PretrainedConfig = None, qk_layer_norms: bool = False, + layer_idx: int = None, ): super().__init__() self.hidden_size = hidden_size @@ -546,6 +558,14 @@ def __init__( self.dropout = dropout self.is_causal = True + self.layer_idx = layer_idx + if layer_idx is None: + logger.warning_once( + f"Instantiating {self.__class__.__name__} without passing a `layer_idx` is not recommended and will " + "lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` " + "when creating this class." + ) + if (self.head_dim * num_heads) != self.hidden_size: raise ValueError( f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}" @@ -612,6 +632,7 @@ def forward( past_key_value: Optional[Tuple[torch.Tensor]] = None, output_attentions: bool = False, use_cache: bool = False, + cache_position: Optional[torch.LongTensor] = None, ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: # if key_value_states are provided this layer is used as a cross-attention layer is_cross_attention = self.is_cross_attention or key_value_states is not None @@ -631,18 +652,16 @@ def forward( kv_seq_len = key_states.shape[-2] if past_key_value is not None: - kv_seq_len += past_key_value[0].shape[-2] + kv_seq_len += past_key_value.get_seq_length(self.layer_idx) if not is_cross_attention: cos, sin = self.rotary_emb(value_states, seq_len=max(kv_seq_len, q_len)) query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids) # [bsz, nh, t, hd] if past_key_value is not None: - # reuse k, v, self_attention - key_states = torch.cat([past_key_value[0], key_states], dim=2) - value_states = torch.cat([past_key_value[1], value_states], dim=2) - - past_key_value = (key_states, value_states) if use_cache else None + # sin and cos are specific to RoPE models; cache_position needed for the static cache + cache_kwargs = {"cache_position": cache_position} + key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs) if self.qk_layer_norms: query_states = self.q_layer_norm(query_states) @@ -697,7 +716,7 @@ def forward( # this was adapted from LlamaDecoderLayer class IdeficsDecoderLayer(nn.Module): - def __init__(self, config: IdeficsConfig): + def __init__(self, config: IdeficsConfig, layer_idx: int = None): super().__init__() self.hidden_size = config.hidden_size self.self_attn = IdeficsAttention( @@ -705,6 +724,7 @@ def __init__(self, config: IdeficsConfig): num_heads=config.num_attention_heads, dropout=config.dropout, config=config, + layer_idx=layer_idx, ) self.mlp = IdeficsMLP( hidden_size=self.hidden_size, @@ -723,6 +743,7 @@ def forward( past_key_value: Optional[Tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, + cache_position: Optional[torch.LongTensor] = None, ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: @@ -750,6 +771,7 @@ def forward( past_key_value=past_key_value, output_attentions=output_attentions, use_cache=use_cache, + cache_position=cache_position, ) hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training) hidden_states = residual + hidden_states @@ -941,6 +963,7 @@ class IdeficsPreTrainedModel(PreTrainedModel): supports_gradient_checkpointing = True _no_split_modules = ["IdeficsDecoderLayer", "IdeficsGatedCrossAttentionLayer"] _supports_sdpa = True + _supports_cache_class = True def _init_weights(self, module): # important: this ported version of Idefics isn't meant for training from scratch - only @@ -1028,6 +1051,10 @@ def _check_and_enable_sdpa(cls, config, hard_check_only: bool = False) -> Pretra more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. + cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): + Indices depicting the position of the input sequence tokens in the sequence. Contrarily to `position_ids`, + this tensor is not affected by padding. It is used to update the cache in the correct position and to infer + the complete sequence length. """ @@ -1073,7 +1100,9 @@ def __init__(self, config: IdeficsConfig): perceiver_config.resampler_n_latents, ) - self.layers = nn.ModuleList([IdeficsDecoderLayer(config) for _ in range(config.num_hidden_layers)]) + self.layers = nn.ModuleList( + [IdeficsDecoderLayer(config, layer_idx=i) for i in range(config.num_hidden_layers)] + ) self.cross_layer_interval = config.cross_layer_interval num_cross_layers = config.num_hidden_layers // self.cross_layer_interval @@ -1129,6 +1158,7 @@ def forward( output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: Optional[bool] = False, return_dict: Optional[bool] = None, + cache_position: Optional[torch.LongTensor] = None, ) -> Union[Tuple, IdeficsBaseModelOutputWithPast]: device = input_ids.device if input_ids is not None else inputs_embeds.device @@ -1140,22 +1170,37 @@ def forward( return_dict = return_dict if return_dict is not None else self.config.use_return_dict - # retrieve input_ids and inputs_embeds - if input_ids is not None and inputs_embeds is not None: - raise ValueError("You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time") - elif input_ids is not None: - batch_size, seq_length = input_ids.shape - elif inputs_embeds is not None: - batch_size, seq_length, _ = inputs_embeds.shape - else: - raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds") + if (input_ids is None) ^ (inputs_embeds is not None): + raise ValueError( + "You cannot specify both input_ids and inputs_embeds at the same time, and must specify either one" + ) - seq_length_with_past = seq_length - past_key_values_length = 0 + if self.gradient_checkpointing and self.training and use_cache: + logger.warning_once( + "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`." + ) + use_cache = False + + if inputs_embeds is None: + inputs_embeds = self.embed_tokens(input_ids) + + return_legacy_cache = False + if use_cache and not isinstance(past_key_values, Cache): + logger.warning_once( + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.43. " + "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" + ) + return_legacy_cache = True + past_key_values = DynamicCache.from_legacy_cache(past_key_values) - if past_key_values is not None: - past_key_values_length = past_key_values[0][0].shape[2] - seq_length_with_past = seq_length_with_past + past_key_values_length + batch_size, seq_length, _ = inputs_embeds.shape + past_key_values_length = past_key_values.get_seq_length() if past_key_values is not None else 0 + seq_length_with_past = seq_length + past_key_values_length + + if cache_position is None: + cache_position = torch.arange( + past_key_values_length, past_key_values_length + inputs_embeds.shape[1], device=inputs_embeds.device + ) if attention_mask is not None and position_ids is None: # create position_ids on the fly for batch generation @@ -1226,37 +1271,27 @@ def forward( device ) - if inputs_embeds is None: - inputs_embeds = self.embed_tokens(input_ids) # embed positions if attention_mask is None: attention_mask = torch.ones( (batch_size, seq_length_with_past), dtype=torch.bool, device=inputs_embeds.device ) - attention_mask = _prepare_4d_causal_attention_mask_for_sdpa( - attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length + + attention_mask = self._update_causal_mask( + attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions ) hidden_states = inputs_embeds - if self.gradient_checkpointing and self.training: - if use_cache: - logger.warning_once( - "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." - ) - use_cache = False - # decoder layers all_hidden_states = () if output_hidden_states else None all_self_attns = () if output_attentions else None - next_decoder_cache = () if use_cache else None + next_decoder_cache = None for idx, decoder_layer in enumerate(self.layers): if output_hidden_states: all_hidden_states += (hidden_states,) - past_key_value = past_key_values[idx] if past_key_values is not None else None - def vblock( main_block, hidden_states, @@ -1271,6 +1306,7 @@ def vblock( layer_idx, cross_layer_interval, gated_cross_attn_layers, + cache_position, ): # TODO(ls): Add cross attention values to respective lists if layer_idx % cross_layer_interval == 0: @@ -1294,12 +1330,13 @@ def vblock( past_key_value=past_key_value, output_attentions=output_attentions, use_cache=use_cache, + cache_position=cache_position, ) return layer_outputs if self.gradient_checkpointing and self.training: - past_key_value = None + past_key_values = None if use_cache: logger.warning_once( "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." @@ -1312,7 +1349,7 @@ def vblock( hidden_states, attention_mask, position_ids, - past_key_value, + past_key_values, image_hidden_states, image_attention_mask, cross_attention_gate, @@ -1321,6 +1358,7 @@ def vblock( idx, self.cross_layer_interval, self.gated_cross_attn_layers, + cache_position, ) else: layer_outputs = vblock( @@ -1328,7 +1366,7 @@ def vblock( hidden_states, attention_mask=attention_mask, position_ids=position_ids, - past_key_value=past_key_value, + past_key_value=past_key_values, image_hidden_states=image_hidden_states, image_attention_mask=image_attention_mask, cross_attention_gate=cross_attention_gate, @@ -1337,12 +1375,13 @@ def vblock( layer_idx=idx, cross_layer_interval=self.cross_layer_interval, gated_cross_attn_layers=self.gated_cross_attn_layers, + cache_position=cache_position, ) hidden_states = layer_outputs[0] if use_cache: - next_decoder_cache += (layer_outputs[2 if output_attentions else 1],) + next_decoder_cache = layer_outputs[2 if output_attentions else 1] if output_attentions: all_self_attns += (layer_outputs[1],) @@ -1354,6 +1393,8 @@ def vblock( all_hidden_states += (hidden_states,) next_cache = next_decoder_cache if use_cache else None + if return_legacy_cache: + next_cache = next_cache.to_legacy_cache() image_hidden_states = image_hidden_states.view(batch_size, num_images, image_seq_len, image_hidden_size) if not return_dict: return tuple( @@ -1369,6 +1410,87 @@ def vblock( image_hidden_states=image_hidden_states, ) + # Copied from transformers.models.llama.modeling_llama.LlamaModel._update_causal_mask + def _update_causal_mask( + self, + attention_mask: torch.Tensor, + input_tensor: torch.Tensor, + cache_position: torch.Tensor, + past_key_values: Cache, + output_attentions: bool, + ): + # TODO: As of torch==2.2.0, the `attention_mask` passed to the model in `generate` is 2D and of dynamic length even when the static + # KV cache is used. This is an issue for torch.compile which then recaptures cudagraphs at each decode steps due to the dynamic shapes. + # (`recording cudagraph tree for symint key 13`, etc.), which is VERY slow. A workaround is `@torch.compiler.disable`, but this prevents using + # `fullgraph=True`. See more context in https://github.com/huggingface/transformers/pull/29114 + + if self.config._attn_implementation == "flash_attention_2": + if attention_mask is not None and 0.0 in attention_mask: + return attention_mask + return None + + # For SDPA, when possible, we will rely on its `is_causal` argument instead of its `attn_mask` argument, in + # order to dispatch on Flash Attention 2. This feature is not compatible with static cache, as SDPA will fail + # to infer the attention mask. + past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0 + using_static_cache = isinstance(past_key_values, StaticCache) + + # When output attentions is True, sdpa implementation's forward method calls the eager implementation's forward + if self.config._attn_implementation == "sdpa" and not using_static_cache and not output_attentions: + if AttentionMaskConverter._ignore_causal_mask_sdpa( + attention_mask, + inputs_embeds=input_tensor, + past_key_values_length=past_seen_tokens, + is_training=self.training, + ): + return None + + dtype, device = input_tensor.dtype, input_tensor.device + min_dtype = torch.finfo(dtype).min + sequence_length = input_tensor.shape[1] + if using_static_cache: + target_length = past_key_values.get_max_length() + else: + target_length = ( + attention_mask.shape[-1] + if isinstance(attention_mask, torch.Tensor) + else past_seen_tokens + sequence_length + 1 + ) + + if attention_mask is not None and attention_mask.dim() == 4: + # in this case we assume that the mask comes already in inverted form and requires no inversion or slicing + if attention_mask.max() != 0: + raise ValueError("Custom 4D attention mask should be passed in inverted form with max==0`") + causal_mask = attention_mask + else: + causal_mask = torch.full( + (sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device + ) + if sequence_length != 1: + causal_mask = torch.triu(causal_mask, diagonal=1) + causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1) + causal_mask = causal_mask[None, None, :, :].expand(input_tensor.shape[0], 1, -1, -1) + if attention_mask is not None: + causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit + mask_length = attention_mask.shape[-1] + padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :] + padding_mask = padding_mask == 0 + causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill( + padding_mask, min_dtype + ) + if ( + self.config._attn_implementation == "sdpa" + and attention_mask is not None + and attention_mask.device.type == "cuda" + and not output_attentions + ): + # Attend to all tokens in fully masked rows in the causal_mask, for example the relevant first rows when + # using left padding. This is required by F.scaled_dot_product_attention memory-efficient attention path. + # Details: https://github.com/pytorch/pytorch/issues/110213 + causal_mask = AttentionMaskConverter._unmask_unattended(causal_mask, min_dtype) + + return causal_mask + class IdeficsForVisionText2Text(IdeficsPreTrainedModel): _keys_to_ignore_on_load_missing = [r"lm_head.weight"] @@ -1447,6 +1569,7 @@ def forward( output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: Optional[bool] = False, return_dict: Optional[bool] = None, + cache_position: Optional[torch.LongTensor] = None, ) -> Union[Tuple, IdeficsCausalLMOutputWithPast]: r""" Args: @@ -1505,6 +1628,7 @@ def forward( output_hidden_states=output_hidden_states, interpolate_pos_encoding=interpolate_pos_encoding, return_dict=return_dict, + cache_position=cache_position, ) hidden_states = outputs[0] diff --git a/src/transformers/models/llava/modeling_llava.py b/src/transformers/models/llava/modeling_llava.py index 0426776beed1..15733ca3378b 100644 --- a/src/transformers/models/llava/modeling_llava.py +++ b/src/transformers/models/llava/modeling_llava.py @@ -157,6 +157,14 @@ def _supports_sdpa(self): """ return self.language_model._supports_sdpa + @property + def _supports_cache_class(self): + """ + Retrieve language_model's attribute to check whether the model supports + SDPA or not. + """ + return self.language_model._supports_cache_class + LLAVA_INPUTS_DOCSTRING = r""" Args: diff --git a/src/transformers/models/llava_next/modeling_llava_next.py b/src/transformers/models/llava_next/modeling_llava_next.py index c052af3b3c8a..ed74a285c3e3 100644 --- a/src/transformers/models/llava_next/modeling_llava_next.py +++ b/src/transformers/models/llava_next/modeling_llava_next.py @@ -263,6 +263,14 @@ def _supports_sdpa(self): """ return self.language_model._supports_sdpa + @property + def _supports_cache_class(self): + """ + Retrieve language_model's attribute to check whether the model supports + SDPA or not. + """ + return self.language_model._supports_cache_class + LLAVA_NEXT_INPUTS_DOCSTRING = r""" Args: diff --git a/src/transformers/models/vipllava/modeling_vipllava.py b/src/transformers/models/vipllava/modeling_vipllava.py index c5f856e78745..73d97c602c23 100644 --- a/src/transformers/models/vipllava/modeling_vipllava.py +++ b/src/transformers/models/vipllava/modeling_vipllava.py @@ -166,6 +166,14 @@ def _supports_sdpa(self): """ return self.language_model._supports_sdpa + @property + def _supports_cache_class(self): + """ + Retrieve language_model's attribute to check whether the model supports + SDPA or not. + """ + return self.language_model._supports_cache_class + VIPLLAVA_INPUTS_DOCSTRING = r""" Args: From c906670172d3de9df77b16af8ad083a390468e6b Mon Sep 17 00:00:00 2001 From: raushan Date: Tue, 9 Jul 2024 11:50:32 +0200 Subject: [PATCH 23/40] remove files --- bart.py | 16 ---------------- update_llava.py | 0 2 files changed, 16 deletions(-) delete mode 100644 bart.py delete mode 100644 update_llava.py diff --git a/bart.py b/bart.py deleted file mode 100644 index 01262fd86b6a..000000000000 --- a/bart.py +++ /dev/null @@ -1,16 +0,0 @@ -from transformers import AutoTokenizer, BartForConditionalGeneration - -model = BartForConditionalGeneration.from_pretrained("facebook/bart-large-cnn").to("cuda:0") -tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn") - -ARTICLE_TO_SUMMARIZE = ( - "PG&E stated it scheduled the blackouts in response to forecasts for high winds " - "amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were " - "scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow." -) -inputs = tokenizer(ARTICLE_TO_SUMMARIZE, return_tensors="pt").to("cuda:0") - -# Generate Summary -summary_ids = model.generate(**inputs, num_beams=1, do_sample=False, max_new_tokens=30, use_cache=False) -out = tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0] -print(out) diff --git a/update_llava.py b/update_llava.py deleted file mode 100644 index e69de29bb2d1..000000000000 From 56c05b2b84b3e93aa1b6574ca738d151af239d0a Mon Sep 17 00:00:00 2001 From: raushan Date: Tue, 9 Jul 2024 12:12:30 +0200 Subject: [PATCH 24/40] style --- .../models/gpt_neox/modeling_gpt_neox.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/transformers/models/gpt_neox/modeling_gpt_neox.py b/src/transformers/models/gpt_neox/modeling_gpt_neox.py index d8f9d920834f..afb911d9c519 100755 --- a/src/transformers/models/gpt_neox/modeling_gpt_neox.py +++ b/src/transformers/models/gpt_neox/modeling_gpt_neox.py @@ -276,7 +276,7 @@ def _attn_projections_and_rope( "cache_position": cache_position, } key, value = layer_past.update(key, value, self.layer_idx, cache_kwargs) - + return query, key, value, layer_past def _attn(self, query, key, value, attention_mask=None, head_mask=None): @@ -359,7 +359,11 @@ def forward( ): # Apply attention-specific projections and rope query, key, value, present = self._attn_projections_and_rope( - hidden_states=hidden_states, position_ids=position_ids, layer_past=layer_past, use_cache=use_cache, cache_position=cache_position + hidden_states=hidden_states, + position_ids=position_ids, + layer_past=layer_past, + use_cache=use_cache, + cache_position=cache_position, ) query_length = query.shape[-2] @@ -567,7 +571,11 @@ def forward( # Apply attention-specific projections and rope query, key, value, present = self._attn_projections_and_rope( - hidden_states=hidden_states, position_ids=position_ids, layer_past=layer_past, use_cache=use_cache, cache_position=cache_position + hidden_states=hidden_states, + position_ids=position_ids, + layer_past=layer_past, + use_cache=use_cache, + cache_position=cache_position, ) # GPT-neo-X casts query and key in fp32 to apply rotary embedding in full precision From 851081092bcf94dc7474f48edc653c9870f38e4e Mon Sep 17 00:00:00 2001 From: raushan Date: Tue, 9 Jul 2024 13:00:40 +0200 Subject: [PATCH 25/40] video LLMs also need a flag --- src/transformers/models/llava/modeling_llava.py | 2 +- src/transformers/models/llava_next/modeling_llava_next.py | 2 +- .../models/llava_next_video/modeling_llava_next_video.py | 8 ++++++++ .../models/video_llava/modeling_video_llava.py | 8 ++++++++ src/transformers/models/vipllava/modeling_vipllava.py | 2 +- 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/transformers/models/llava/modeling_llava.py b/src/transformers/models/llava/modeling_llava.py index 15733ca3378b..a432b546922f 100644 --- a/src/transformers/models/llava/modeling_llava.py +++ b/src/transformers/models/llava/modeling_llava.py @@ -161,7 +161,7 @@ def _supports_sdpa(self): def _supports_cache_class(self): """ Retrieve language_model's attribute to check whether the model supports - SDPA or not. + cache class or not. """ return self.language_model._supports_cache_class diff --git a/src/transformers/models/llava_next/modeling_llava_next.py b/src/transformers/models/llava_next/modeling_llava_next.py index 11d592850986..5592ffad64e7 100644 --- a/src/transformers/models/llava_next/modeling_llava_next.py +++ b/src/transformers/models/llava_next/modeling_llava_next.py @@ -267,7 +267,7 @@ def _supports_sdpa(self): def _supports_cache_class(self): """ Retrieve language_model's attribute to check whether the model supports - SDPA or not. + cache class or not. """ return self.language_model._supports_cache_class diff --git a/src/transformers/models/llava_next_video/modeling_llava_next_video.py b/src/transformers/models/llava_next_video/modeling_llava_next_video.py index 349ccba03965..f68b1191e997 100644 --- a/src/transformers/models/llava_next_video/modeling_llava_next_video.py +++ b/src/transformers/models/llava_next_video/modeling_llava_next_video.py @@ -302,6 +302,14 @@ def _supports_sdpa(self): SDPA or not. """ return self.language_model._supports_sdpa + + @property + def _supports_cache_class(self): + """ + Retrieve language_model's attribute to check whether the model supports + cache class or not. + """ + return self.language_model._supports_cache_class LLAVA_NEXT_VIDEO_INPUTS_DOCSTRING = r""" diff --git a/src/transformers/models/video_llava/modeling_video_llava.py b/src/transformers/models/video_llava/modeling_video_llava.py index b540576d4866..fa1c9797b118 100644 --- a/src/transformers/models/video_llava/modeling_video_llava.py +++ b/src/transformers/models/video_llava/modeling_video_llava.py @@ -154,6 +154,14 @@ def _supports_sdpa(self): SDPA or not. """ return self.language_model._supports_sdpa + + @property + def _supports_cache_class(self): + """ + Retrieve language_model's attribute to check whether the model supports + cache class or not. + """ + return self.language_model._supports_cache_class VIDEO_LLAVA_INPUTS_DOCSTRING = r""" diff --git a/src/transformers/models/vipllava/modeling_vipllava.py b/src/transformers/models/vipllava/modeling_vipllava.py index 73d97c602c23..b1922d463aa3 100644 --- a/src/transformers/models/vipllava/modeling_vipllava.py +++ b/src/transformers/models/vipllava/modeling_vipllava.py @@ -170,7 +170,7 @@ def _supports_sdpa(self): def _supports_cache_class(self): """ Retrieve language_model's attribute to check whether the model supports - SDPA or not. + cache class or not. """ return self.language_model._supports_cache_class From cebb55d4bd8f8419d1195e83ef0c47b40b66c577 Mon Sep 17 00:00:00 2001 From: raushan Date: Tue, 9 Jul 2024 13:02:02 +0200 Subject: [PATCH 26/40] style --- .../models/llava_next_video/modeling_llava_next_video.py | 2 +- src/transformers/models/video_llava/modeling_video_llava.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/transformers/models/llava_next_video/modeling_llava_next_video.py b/src/transformers/models/llava_next_video/modeling_llava_next_video.py index f68b1191e997..5a7256a55f25 100644 --- a/src/transformers/models/llava_next_video/modeling_llava_next_video.py +++ b/src/transformers/models/llava_next_video/modeling_llava_next_video.py @@ -302,7 +302,7 @@ def _supports_sdpa(self): SDPA or not. """ return self.language_model._supports_sdpa - + @property def _supports_cache_class(self): """ diff --git a/src/transformers/models/video_llava/modeling_video_llava.py b/src/transformers/models/video_llava/modeling_video_llava.py index fa1c9797b118..7193884c11b4 100644 --- a/src/transformers/models/video_llava/modeling_video_llava.py +++ b/src/transformers/models/video_llava/modeling_video_llava.py @@ -154,7 +154,7 @@ def _supports_sdpa(self): SDPA or not. """ return self.language_model._supports_sdpa - + @property def _supports_cache_class(self): """ From 8fd9dd1f55cc4a5b92e32381784f939701b18e50 Mon Sep 17 00:00:00 2001 From: raushan Date: Fri, 26 Jul 2024 09:11:35 +0200 Subject: [PATCH 27/40] llava will go in another PR --- src/transformers/models/llava/modeling_llava.py | 8 -------- src/transformers/models/llava_next/modeling_llava_next.py | 8 -------- .../models/llava_next_video/modeling_llava_next_video.py | 8 -------- .../models/video_llava/modeling_video_llava.py | 8 -------- src/transformers/models/vipllava/modeling_vipllava.py | 8 -------- 5 files changed, 40 deletions(-) diff --git a/src/transformers/models/llava/modeling_llava.py b/src/transformers/models/llava/modeling_llava.py index a432b546922f..0426776beed1 100644 --- a/src/transformers/models/llava/modeling_llava.py +++ b/src/transformers/models/llava/modeling_llava.py @@ -157,14 +157,6 @@ def _supports_sdpa(self): """ return self.language_model._supports_sdpa - @property - def _supports_cache_class(self): - """ - Retrieve language_model's attribute to check whether the model supports - cache class or not. - """ - return self.language_model._supports_cache_class - LLAVA_INPUTS_DOCSTRING = r""" Args: diff --git a/src/transformers/models/llava_next/modeling_llava_next.py b/src/transformers/models/llava_next/modeling_llava_next.py index 5592ffad64e7..23e3c25025fc 100644 --- a/src/transformers/models/llava_next/modeling_llava_next.py +++ b/src/transformers/models/llava_next/modeling_llava_next.py @@ -263,14 +263,6 @@ def _supports_sdpa(self): """ return self.language_model._supports_sdpa - @property - def _supports_cache_class(self): - """ - Retrieve language_model's attribute to check whether the model supports - cache class or not. - """ - return self.language_model._supports_cache_class - LLAVA_NEXT_INPUTS_DOCSTRING = r""" Args: diff --git a/src/transformers/models/llava_next_video/modeling_llava_next_video.py b/src/transformers/models/llava_next_video/modeling_llava_next_video.py index 5a7256a55f25..349ccba03965 100644 --- a/src/transformers/models/llava_next_video/modeling_llava_next_video.py +++ b/src/transformers/models/llava_next_video/modeling_llava_next_video.py @@ -303,14 +303,6 @@ def _supports_sdpa(self): """ return self.language_model._supports_sdpa - @property - def _supports_cache_class(self): - """ - Retrieve language_model's attribute to check whether the model supports - cache class or not. - """ - return self.language_model._supports_cache_class - LLAVA_NEXT_VIDEO_INPUTS_DOCSTRING = r""" Args: diff --git a/src/transformers/models/video_llava/modeling_video_llava.py b/src/transformers/models/video_llava/modeling_video_llava.py index 7193884c11b4..b540576d4866 100644 --- a/src/transformers/models/video_llava/modeling_video_llava.py +++ b/src/transformers/models/video_llava/modeling_video_llava.py @@ -155,14 +155,6 @@ def _supports_sdpa(self): """ return self.language_model._supports_sdpa - @property - def _supports_cache_class(self): - """ - Retrieve language_model's attribute to check whether the model supports - cache class or not. - """ - return self.language_model._supports_cache_class - VIDEO_LLAVA_INPUTS_DOCSTRING = r""" Args: diff --git a/src/transformers/models/vipllava/modeling_vipllava.py b/src/transformers/models/vipllava/modeling_vipllava.py index b1922d463aa3..c5f856e78745 100644 --- a/src/transformers/models/vipllava/modeling_vipllava.py +++ b/src/transformers/models/vipllava/modeling_vipllava.py @@ -166,14 +166,6 @@ def _supports_sdpa(self): """ return self.language_model._supports_sdpa - @property - def _supports_cache_class(self): - """ - Retrieve language_model's attribute to check whether the model supports - cache class or not. - """ - return self.language_model._supports_cache_class - VIPLLAVA_INPUTS_DOCSTRING = r""" Args: From aea219ba567814347b6057b9b6639c52744df28b Mon Sep 17 00:00:00 2001 From: raushan Date: Fri, 26 Jul 2024 09:14:11 +0200 Subject: [PATCH 28/40] style --- src/transformers/models/musicgen/modeling_musicgen.py | 1 + src/transformers/models/sew/modeling_sew.py | 1 + src/transformers/models/unispeech/modeling_unispeech.py | 1 + src/transformers/models/unispeech_sat/modeling_unispeech_sat.py | 1 + 4 files changed, 4 insertions(+) diff --git a/src/transformers/models/musicgen/modeling_musicgen.py b/src/transformers/models/musicgen/modeling_musicgen.py index e2776dd2884c..b0e456db8add 100644 --- a/src/transformers/models/musicgen/modeling_musicgen.py +++ b/src/transformers/models/musicgen/modeling_musicgen.py @@ -438,6 +438,7 @@ def forward( return attn_output, attn_weights, past_key_value + class MusicgenSdpaAttention(MusicgenAttention): def forward( self, diff --git a/src/transformers/models/sew/modeling_sew.py b/src/transformers/models/sew/modeling_sew.py index 765de410b867..191d7c2cd8c6 100644 --- a/src/transformers/models/sew/modeling_sew.py +++ b/src/transformers/models/sew/modeling_sew.py @@ -677,6 +677,7 @@ def forward( return attn_output, attn_weights, past_key_value + class SEWSdpaAttention(SEWAttention): # Copied from transformers.models.bart.modeling_bart.BartSdpaAttention.forward with Bart->SEW def forward( diff --git a/src/transformers/models/unispeech/modeling_unispeech.py b/src/transformers/models/unispeech/modeling_unispeech.py index d01b443e2c92..4202f680437c 100755 --- a/src/transformers/models/unispeech/modeling_unispeech.py +++ b/src/transformers/models/unispeech/modeling_unispeech.py @@ -713,6 +713,7 @@ def forward( return attn_output, attn_weights, past_key_value + class UniSpeechSdpaAttention(UniSpeechAttention): # Copied from transformers.models.bart.modeling_bart.BartSdpaAttention.forward with Bart->UniSpeech def forward( diff --git a/src/transformers/models/unispeech_sat/modeling_unispeech_sat.py b/src/transformers/models/unispeech_sat/modeling_unispeech_sat.py index 2bd2af5d54d7..bfb2cbfa4f55 100755 --- a/src/transformers/models/unispeech_sat/modeling_unispeech_sat.py +++ b/src/transformers/models/unispeech_sat/modeling_unispeech_sat.py @@ -730,6 +730,7 @@ def forward( return attn_output, attn_weights, past_key_value + class UniSpeechSatSdpaAttention(UniSpeechSatAttention): # Copied from transformers.models.bart.modeling_bart.BartSdpaAttention.forward with Bart->UniSpeechSat def forward( From 499186332c84d99aa8152c8ec361e98cf289e858 Mon Sep 17 00:00:00 2001 From: raushan Date: Fri, 26 Jul 2024 09:16:15 +0200 Subject: [PATCH 29/40] [run-slow] codegen, falcon, git, gpt_neo, gpt_neox, gptj, idefics From ec306a26585b95ffa1149c0aa54cb7e47c8cdcd3 Mon Sep 17 00:00:00 2001 From: Raushan Turganbay Date: Tue, 30 Jul 2024 10:16:32 +0500 Subject: [PATCH 30/40] Update src/transformers/models/gpt_neo/modeling_gpt_neo.py Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com> --- src/transformers/models/gpt_neo/modeling_gpt_neo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformers/models/gpt_neo/modeling_gpt_neo.py b/src/transformers/models/gpt_neo/modeling_gpt_neo.py index 4232eece5d74..068bf2f8c841 100755 --- a/src/transformers/models/gpt_neo/modeling_gpt_neo.py +++ b/src/transformers/models/gpt_neo/modeling_gpt_neo.py @@ -686,7 +686,7 @@ def forward( inputs_embeds = self.wte(input_ids) use_legacy_cache = False - if use_cache and not isinstance(past_key_values, Cache): # kept for BC (non `Cache` `past_key_values` inputs) + if use_cache and not isinstance(past_key_values, Cache) and not self.training: # kept for BC (non `Cache` `past_key_values` inputs) use_legacy_cache = True past_key_values = DynamicCache.from_legacy_cache(past_key_values) logger.warning_once( From cf793b7d790515eab2cf1b402147cd6f8da26e58 Mon Sep 17 00:00:00 2001 From: raushan Date: Tue, 30 Jul 2024 07:38:50 +0200 Subject: [PATCH 31/40] copy from --- .../models/gpt_neox/modeling_gpt_neox.py | 65 ++++++------------- 1 file changed, 20 insertions(+), 45 deletions(-) diff --git a/src/transformers/models/gpt_neox/modeling_gpt_neox.py b/src/transformers/models/gpt_neox/modeling_gpt_neox.py index 6c5440a54092..c8a649d8cd43 100755 --- a/src/transformers/models/gpt_neox/modeling_gpt_neox.py +++ b/src/transformers/models/gpt_neox/modeling_gpt_neox.py @@ -472,6 +472,10 @@ def forward( cache_position=cache_position, ) + causal_mask = attention_mask + if attention_mask is not None: + causal_mask = causal_mask[:, :, :, : key.shape[-2]] + # GPT-neo-X casts query and key in fp32 to apply rotary embedding in full precision target_dtype = value.dtype if query.dtype != target_dtype: @@ -487,13 +491,13 @@ def forward( # We dispatch to SDPA's Flash Attention or Efficient kernels via this `is_causal` if statement instead of an inline conditional assignment # in SDPA to support both torch.compile's dynamic shapes and full graph options. An inline conditional prevents dynamic shapes from compiling. - is_causal = True if attention_mask is None and q_len > 1 else False + is_causal = True if causal_mask is None and q_len > 1 else False attn_output = torch.nn.functional.scaled_dot_product_attention( query=query, key=key, value=value, - attn_mask=attention_mask, + attn_mask=causal_mask, dropout_p=self.attention_dropout.p if self.training else 0.0, is_causal=is_causal, ) @@ -1136,6 +1140,7 @@ def forward( attentions=outputs.attentions, ) + # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.prepare_inputs_for_generation def prepare_inputs_for_generation( self, input_ids, @@ -1143,37 +1148,19 @@ def prepare_inputs_for_generation( attention_mask=None, inputs_embeds=None, cache_position=None, + position_ids=None, use_cache=True, **kwargs, ): - past_length = 0 - # cut decoder_input_ids if paspast_length = cache_position[0] if cache_position is not None else past_key_values.get_seq_length() + # If we have cache: let's slice `input_ids` through `cache_position`, to keep only the unprocessed tokens + # Exception 1: when passing input_embeds, input_ids may be missing entries + # Exception 2: some generation methods do special slicing of input_ids, so we don't need to do it here if past_key_values is not None: - past_length = cache_position[0] if cache_position is not None else past_key_values.get_seq_length() - max_cache_length = ( - torch.tensor(past_key_values.get_max_length(), device=input_ids.device) - if past_key_values.get_max_length() is not None - else None - ) - cache_length = past_length if max_cache_length is None else torch.min(max_cache_length, past_length) - - # Some generation methods already pass only the last input ID - if input_ids.shape[1] > past_length: - remove_prefix_length = past_length - else: - # Default to old behavior: keep only final ID - remove_prefix_length = input_ids.shape[1] - 1 - input_ids = input_ids[:, remove_prefix_length:] - - # If we are about to go beyond the maximum cache length, we need to crop the input attention mask. - if ( - max_cache_length is not None - and attention_mask is not None - and cache_length + input_ids.shape[1] > max_cache_length - ): - attention_mask = attention_mask[:, -max_cache_length:] + if inputs_embeds is not None: # Exception 1 + input_ids = input_ids[:, -cache_position.shape[0] :] + elif input_ids.shape[1] != cache_position.shape[0]: # Default case (the "else", a no op, is Exception 2) + input_ids = input_ids[:, cache_position] - position_ids = kwargs.get("position_ids", None) if attention_mask is not None and position_ids is None: # create position_ids on the fly for batch generation position_ids = attention_mask.long().cumsum(-1) - 1 @@ -1181,33 +1168,21 @@ def prepare_inputs_for_generation( if past_key_values: position_ids = position_ids[:, -input_ids.shape[1] :] - # if model is used as a decoder in encoder-decoder model, the decoder attention mask is created on the fly - if attention_mask is None: - attention_mask = input_ids.new_ones(input_ids.shape) - # if `inputs_embeds` are passed, we only want to use them in the 1st generation step - if inputs_embeds is not None and past_length == 0: + if inputs_embeds is not None and cache_position[0] == 0: model_inputs = {"inputs_embeds": inputs_embeds} else: - model_inputs = {"input_ids": input_ids} - - if use_cache: - input_length = input_ids.shape[-1] - if cache_position is None: - cache_position = torch.arange(past_length, past_length + input_length, device=input_ids.device) - else: - cache_position = cache_position[-input_length:] + model_inputs = {"input_ids": input_ids.contiguous()} # `contiguous()` needed for compilation use cases model_inputs.update( { - "attention_mask": attention_mask, - "past_key_values": past_key_values, "position_ids": position_ids, - "use_cache": use_cache, "cache_position": cache_position, + "past_key_values": past_key_values, + "use_cache": use_cache, + "attention_mask": attention_mask, } ) - return model_inputs def _reorder_cache(self, past_key_values, beam_idx): From c92409c7d8d2e30e736c1ebb2bded590221051b6 Mon Sep 17 00:00:00 2001 From: raushan Date: Tue, 30 Jul 2024 07:43:47 +0200 Subject: [PATCH 32/40] deprecate until v4.45 and warn if not training --- src/transformers/models/codegen/modeling_codegen.py | 4 ++-- src/transformers/models/falcon/modeling_falcon.py | 4 ++-- src/transformers/models/git/modeling_git.py | 4 ++-- src/transformers/models/gpt_neo/modeling_gpt_neo.py | 4 ++-- src/transformers/models/gpt_neox/modeling_gpt_neox.py | 4 ++-- src/transformers/models/gptj/modeling_gptj.py | 4 ++-- src/transformers/models/idefics/modeling_idefics.py | 7 ++++--- 7 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/transformers/models/codegen/modeling_codegen.py b/src/transformers/models/codegen/modeling_codegen.py index e88206b6682a..79ffe8933bf6 100644 --- a/src/transformers/models/codegen/modeling_codegen.py +++ b/src/transformers/models/codegen/modeling_codegen.py @@ -490,11 +490,11 @@ def forward( inputs_embeds = self.wte(input_ids) use_legacy_cache = False - if use_cache and not isinstance(past_key_values, Cache): # kept for BC (non `Cache` `past_key_values` inputs) + if use_cache and not isinstance(past_key_values, Cache) and not self.training: use_legacy_cache = True past_key_values = DynamicCache.from_legacy_cache(past_key_values) logger.warning_once( - "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.43. " + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.45. " "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" ) diff --git a/src/transformers/models/falcon/modeling_falcon.py b/src/transformers/models/falcon/modeling_falcon.py index 4850d08ddde9..823b5ac7ee0e 100644 --- a/src/transformers/models/falcon/modeling_falcon.py +++ b/src/transformers/models/falcon/modeling_falcon.py @@ -969,11 +969,11 @@ def forward( # Compute alibi tensor: check build_alibi_tensor documentation use_legacy_cache = False - if use_cache and not isinstance(past_key_values, Cache): # kept for BC (non `Cache` `past_key_values` inputs) + if use_cache and not isinstance(past_key_values, Cache) and not self.training: use_legacy_cache = True past_key_values = DynamicCache.from_legacy_cache(past_key_values) logger.warning_once( - "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.43. " + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.45. " "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" ) diff --git a/src/transformers/models/git/modeling_git.py b/src/transformers/models/git/modeling_git.py index c2565787e20b..581f2b3947b4 100644 --- a/src/transformers/models/git/modeling_git.py +++ b/src/transformers/models/git/modeling_git.py @@ -418,11 +418,11 @@ def forward( use_cache = False use_legacy_cache = False - if use_cache and not isinstance(past_key_values, Cache): # kept for BC (non `Cache` `past_key_values` inputs) + if use_cache and not isinstance(past_key_values, Cache) and not self.training: use_legacy_cache = True past_key_values = DynamicCache.from_legacy_cache(past_key_values) logger.warning_once( - "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.43. " + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.45. " "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" ) diff --git a/src/transformers/models/gpt_neo/modeling_gpt_neo.py b/src/transformers/models/gpt_neo/modeling_gpt_neo.py index 068bf2f8c841..a1adc8e0086c 100755 --- a/src/transformers/models/gpt_neo/modeling_gpt_neo.py +++ b/src/transformers/models/gpt_neo/modeling_gpt_neo.py @@ -686,11 +686,11 @@ def forward( inputs_embeds = self.wte(input_ids) use_legacy_cache = False - if use_cache and not isinstance(past_key_values, Cache) and not self.training: # kept for BC (non `Cache` `past_key_values` inputs) + if use_cache and not isinstance(past_key_values, Cache) and not self.training: use_legacy_cache = True past_key_values = DynamicCache.from_legacy_cache(past_key_values) logger.warning_once( - "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.43. " + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.45. " "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" ) diff --git a/src/transformers/models/gpt_neox/modeling_gpt_neox.py b/src/transformers/models/gpt_neox/modeling_gpt_neox.py index c8a649d8cd43..c7b839009c2f 100755 --- a/src/transformers/models/gpt_neox/modeling_gpt_neox.py +++ b/src/transformers/models/gpt_neox/modeling_gpt_neox.py @@ -868,11 +868,11 @@ def forward( inputs_embeds = self.embed_in(input_ids) use_legacy_cache = False - if use_cache and not isinstance(past_key_values, Cache): # kept for BC (non `Cache` `past_key_values` inputs) + if use_cache and not isinstance(past_key_values, Cache) and not self.training: use_legacy_cache = True past_key_values = DynamicCache.from_legacy_cache(past_key_values) logger.warning_once( - "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.43. " + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.45. " "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" ) diff --git a/src/transformers/models/gptj/modeling_gptj.py b/src/transformers/models/gptj/modeling_gptj.py index aecf893625c8..bce8725d3efa 100644 --- a/src/transformers/models/gptj/modeling_gptj.py +++ b/src/transformers/models/gptj/modeling_gptj.py @@ -782,11 +782,11 @@ def forward( # Compute alibi tensor: check build_alibi_tensor documentation use_legacy_cache = False - if use_cache and not isinstance(past_key_values, Cache): # kept for BC (non `Cache` `past_key_values` inputs) + if use_cache and not isinstance(past_key_values, Cache) and not self.training: use_legacy_cache = True past_key_values = DynamicCache.from_legacy_cache(past_key_values) logger.warning_once( - "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.43. " + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.45. " "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" ) diff --git a/src/transformers/models/idefics/modeling_idefics.py b/src/transformers/models/idefics/modeling_idefics.py index 38831579617e..36e30b64d8d2 100644 --- a/src/transformers/models/idefics/modeling_idefics.py +++ b/src/transformers/models/idefics/modeling_idefics.py @@ -652,7 +652,8 @@ def forward( kv_seq_len = key_states.shape[-2] if past_key_value is not None: - kv_seq_len += past_key_value.get_seq_length(self.layer_idx) + kv_seq_len += cache_position[-1] + if not is_cross_attention: cos, sin = self.rotary_emb(value_states, seq_len=max(kv_seq_len, q_len)) query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids) @@ -1185,9 +1186,9 @@ def forward( inputs_embeds = self.embed_tokens(input_ids) return_legacy_cache = False - if use_cache and not isinstance(past_key_values, Cache): + if use_cache and not isinstance(past_key_values, Cache) and not self.training: logger.warning_once( - "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.43. " + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.45. " "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" ) return_legacy_cache = True From c2b97e41a4c986d6b28fdf6b0ebef9d49452c786 Mon Sep 17 00:00:00 2001 From: raushan Date: Tue, 30 Jul 2024 08:01:41 +0200 Subject: [PATCH 33/40] nit --- src/transformers/models/gptj/modeling_gptj.py | 11 +++-------- src/transformers/models/idefics/modeling_idefics.py | 2 +- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/transformers/models/gptj/modeling_gptj.py b/src/transformers/models/gptj/modeling_gptj.py index bce8725d3efa..f22374d32ccf 100644 --- a/src/transformers/models/gptj/modeling_gptj.py +++ b/src/transformers/models/gptj/modeling_gptj.py @@ -254,8 +254,6 @@ def forward( key = key.permute(0, 2, 1, 3) query = query.permute(0, 2, 1, 3) - # Note that this cast is quite ugly, but is not implemented before ROPE as the original codebase keeps the key in float32 all along the computation. - # Reference: https://github.com/kingoflolz/mesh-transformer-jax/blob/f8315e3003033b23f21d78361b288953064e0e76/mesh_transformer/layers.py#L128 if layer_past is not None: cache_kwargs = { "sin": sin, @@ -263,7 +261,7 @@ def forward( "partial_rotation_size": self.rotary_dim, "cache_position": cache_position, } - key, value = layer_past.update(key.to(hidden_states.dtype), value, self.layer_idx, cache_kwargs) + key, value = layer_past.update(key, value, self.layer_idx, cache_kwargs) # compute self-attention: V x Softmax(QK^T) attn_output, attn_weights = self._attn(query, key, value, attention_mask, head_mask) @@ -351,8 +349,6 @@ def forward( query = query.permute(0, 2, 1, 3) # value: batch_size x num_attention_heads x seq_length x head_dim - # Note that this cast is quite ugly, but is not implemented before ROPE as the original codebase keeps the key in float32 all along the computation. - # Reference: https://github.com/kingoflolz/mesh-transformer-jax/blob/f8315e3003033b23f21d78361b288953064e0e76/mesh_transformer/layers.py#L128 if layer_past is not None: cache_kwargs = { "sin": sin, @@ -360,7 +356,7 @@ def forward( "partial_rotation_size": self.rotary_dim, "cache_position": cache_position, } - key, value = layer_past.update(key.to(hidden_states.dtype), value, self.layer_idx, cache_kwargs) + key, value = layer_past.update(key, value, self.layer_idx, cache_kwargs) # The Flash attention requires the input to have the shape # batch_size x seq_length x head_dim x hidden_dim @@ -780,7 +776,6 @@ def forward( if inputs_embeds is None: inputs_embeds = self.wte(input_ids) - # Compute alibi tensor: check build_alibi_tensor documentation use_legacy_cache = False if use_cache and not isinstance(past_key_values, Cache) and not self.training: use_legacy_cache = True @@ -792,7 +787,7 @@ def forward( seq_length = inputs_embeds.shape[1] if cache_position is None: - past_key_values_length = past_key_values.get_seq_length() if use_cache else 0 + past_key_values_length = past_key_values.get_seq_length() if past_key_values is not None else 0 cache_position = torch.arange( past_key_values_length, past_key_values_length + seq_length, device=inputs_embeds.device ) diff --git a/src/transformers/models/idefics/modeling_idefics.py b/src/transformers/models/idefics/modeling_idefics.py index 36e30b64d8d2..35cd509c4c78 100644 --- a/src/transformers/models/idefics/modeling_idefics.py +++ b/src/transformers/models/idefics/modeling_idefics.py @@ -652,7 +652,7 @@ def forward( kv_seq_len = key_states.shape[-2] if past_key_value is not None: - kv_seq_len += cache_position[-1] + kv_seq_len += cache_position[0] # past_key_value.get_seq_length(self.layer_idx) if not is_cross_attention: cos, sin = self.rotary_emb(value_states, seq_len=max(kv_seq_len, q_len)) From 35b60de91b7b306c7643fb802b872767e0985e06 Mon Sep 17 00:00:00 2001 From: raushan Date: Tue, 30 Jul 2024 08:40:29 +0200 Subject: [PATCH 34/40] fix test --- src/transformers/models/falcon/modeling_falcon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformers/models/falcon/modeling_falcon.py b/src/transformers/models/falcon/modeling_falcon.py index 823b5ac7ee0e..acf79c32cd04 100644 --- a/src/transformers/models/falcon/modeling_falcon.py +++ b/src/transformers/models/falcon/modeling_falcon.py @@ -978,7 +978,7 @@ def forward( ) alibi = None - past_key_values_length = past_key_values.get_seq_length() if use_cache else 0 + past_key_values_length = past_key_values.get_seq_length() if past_key_values is not None else 0 batch_size, seq_length, _ = inputs_embeds.shape if self.use_alibi: mask = ( From d2fca9a1f18f1c320ca082e4f4107b0b9ac9fa7b Mon Sep 17 00:00:00 2001 From: raushan Date: Fri, 2 Aug 2024 09:38:33 +0200 Subject: [PATCH 35/40] test static cache --- src/transformers/cache_utils.py | 4 ++- tests/generation/test_utils.py | 49 ++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/transformers/cache_utils.py b/src/transformers/cache_utils.py index 0c03ea2735db..3c2efb223f83 100644 --- a/src/transformers/cache_utils.py +++ b/src/transformers/cache_utils.py @@ -813,7 +813,9 @@ def __init__(self, config: PretrainedConfig, max_batch_size: int, max_cache_len: self.dtype = dtype if dtype is not None else torch.float32 self.num_key_value_heads = ( - config.num_attention_heads if config.num_key_value_heads is None else config.num_key_value_heads + config.num_attention_heads + if getattr(config, "num_key_value_heads", None) is None + else config.num_key_value_heads ) self.key_cache: List[torch.Tensor] = [] diff --git a/tests/generation/test_utils.py b/tests/generation/test_utils.py index 2c440bbd71ae..0b02219c0c8a 100644 --- a/tests/generation/test_utils.py +++ b/tests/generation/test_utils.py @@ -59,7 +59,7 @@ ImageGPTForCausalImageModeling, SpeechEncoderDecoderModel, ) - from transformers.cache_utils import DynamicCache, EncoderDecoderCache, QuantoQuantizedCache + from transformers.cache_utils import DynamicCache, EncoderDecoderCache, QuantoQuantizedCache, StaticCache from transformers.generation import ( BeamSampleDecoderOnlyOutput, BeamSampleEncoderDecoderOutput, @@ -1770,6 +1770,53 @@ def test_new_cache_format(self, num_beams, do_sample): ) ) + def test_generate_with_static_cache(self): + """ + Tests if StaticCache works if we set attn_implementation=static when generation. + This doesn't test if generation quality is good, but tests that models with + self._supports_static_cache don't throw an error when generating and return + a StaticCache object at the end. + """ + for model_class in self.all_generative_model_classes: + if not model_class._supports_static_cache: + self.skipTest(reason="This model does not support the static cache format") + + config, input_ids, attention_mask = self._get_input_ids_and_config() + if config.is_encoder_decoder: + self.skipTest(reason="This model is encoder-decoder and has Encoder-Decoder Cache") + + config.use_cache = True + config.is_decoder = True + batch_size, seq_length = input_ids.shape + max_new_tokens = 20 + + model = model_class(config).to(torch_device).eval() + generation_kwargs = { + "max_length": None, + "max_new_tokens": max_new_tokens, + "cache_implementation": "static", + "return_dict_in_generate": True, # Required to return `past_key_values` + } + + max_cache_len = seq_length + max_new_tokens + head_dim = ( + model.config.head_dim + if hasattr(model.config, "head_dim") + else model.config.hidden_size // model.config.num_attention_heads + ) + num_key_value_heads = ( + model.config.num_attention_heads + if getattr(config, "num_key_value_heads", None) is None + else model.config.num_key_value_heads + ) + num_hidden_layers = config.num_hidden_layers + results = model.generate(input_ids, attention_mask=attention_mask, **generation_kwargs) + + cache_shape = (batch_size, num_key_value_heads, max_cache_len, head_dim) + self.assertTrue(isinstance(results.past_key_values, StaticCache)) + self.assertTrue(len(results.past_key_values.key_cache) == num_hidden_layers) + self.assertTrue(results.past_key_values.key_cache[0].shape == cache_shape) + @require_quanto def test_generate_with_quant_cache(self): for model_class in self.all_generative_model_classes: From 42349d49a043a1e4a254ffe8acd788c22ec444d1 Mon Sep 17 00:00:00 2001 From: raushan Date: Fri, 2 Aug 2024 10:42:36 +0200 Subject: [PATCH 36/40] add more tests and fix models --- .../models/codegen/modeling_codegen.py | 20 +--------- .../models/gpt_neo/modeling_gpt_neo.py | 8 ---- src/transformers/models/gptj/modeling_gptj.py | 19 +-------- .../models/idefics/modeling_idefics.py | 6 +-- tests/models/phi3/test_modeling_phi3.py | 3 +- tests/test_modeling_common.py | 39 +++++++++++++++++++ 6 files changed, 46 insertions(+), 49 deletions(-) diff --git a/src/transformers/models/codegen/modeling_codegen.py b/src/transformers/models/codegen/modeling_codegen.py index 79ffe8933bf6..927f587ce337 100644 --- a/src/transformers/models/codegen/modeling_codegen.py +++ b/src/transformers/models/codegen/modeling_codegen.py @@ -63,14 +63,6 @@ def __init__(self, config, layer_idx=None): super().__init__() max_positions = config.max_position_embeddings - self.register_buffer( - "causal_mask", - torch.tril(torch.ones((max_positions, max_positions), dtype=torch.bool)).view( - 1, 1, max_positions, max_positions - ), - persistent=False, - ) - self.attn_dropout = nn.Dropout(config.attn_pdrop) self.resid_dropout = nn.Dropout(config.resid_pdrop) self.layer_idx = layer_idx @@ -123,27 +115,17 @@ def _attn( attention_mask=None, head_mask=None, ): - # compute causal mask from causal mask buffer - query_length, key_length = query.size(-2), key.size(-2) - causal_mask = self.causal_mask[:, :, key_length - query_length : key_length, :key_length] - # Keep the attention weights computation in fp32 to avoid overflow issues query = query.to(torch.float32) key = key.to(torch.float32) attn_weights = torch.matmul(query, key.transpose(-1, -2)) - attn_weights = attn_weights / self.scale_attn - mask_value = torch.finfo(attn_weights.dtype).min - # Need to be a tensor, otherwise we get error: `RuntimeError: expected scalar type float but found double`. - # Need to be on the same device, otherwise `RuntimeError: ..., x and y to be on the same device` - mask_value = torch.tensor(mask_value, dtype=attn_weights.dtype).to(attn_weights.device) - attn_weights = torch.where(causal_mask, attn_weights, mask_value) - if attention_mask is not None: causal_mask = attention_mask[:, :, :, : key.shape[-2]] attn_weights += causal_mask + attn_weights = attn_weights / self.scale_attn attn_weights = nn.Softmax(dim=-1)(attn_weights) attn_weights = attn_weights.to(value.dtype) attn_weights = self.attn_dropout(attn_weights) diff --git a/src/transformers/models/gpt_neo/modeling_gpt_neo.py b/src/transformers/models/gpt_neo/modeling_gpt_neo.py index a1adc8e0086c..5bf7eb8818c4 100755 --- a/src/transformers/models/gpt_neo/modeling_gpt_neo.py +++ b/src/transformers/models/gpt_neo/modeling_gpt_neo.py @@ -210,14 +210,6 @@ def _attn(self, query, key, value, attention_mask=None, head_mask=None): attn_weights = torch.matmul(query, key.transpose(-1, -2)) - query_length, key_length = query.size(-2), key.size(-2) - causal_mask = self.bias[:, :, key_length - query_length : key_length, :key_length] - mask_value = torch.finfo(attn_weights.dtype).min - # Need to be a tensor, otherwise we get error: `RuntimeError: expected scalar type float but found double`. - # Need to be on the same device, otherwise `RuntimeError: ..., x and y to be on the same device` - mask_value = torch.tensor(mask_value, dtype=attn_weights.dtype).to(attn_weights.device) - attn_weights = torch.where(causal_mask, attn_weights, mask_value) - if attention_mask is not None: # no matter the length, we just slice it causal_mask = attention_mask[:, :, :, : key.shape[-2]] attn_weights = attn_weights + causal_mask diff --git a/src/transformers/models/gptj/modeling_gptj.py b/src/transformers/models/gptj/modeling_gptj.py index 9daa688b9e39..620b95418a51 100644 --- a/src/transformers/models/gptj/modeling_gptj.py +++ b/src/transformers/models/gptj/modeling_gptj.py @@ -86,13 +86,7 @@ def __init__(self, config, layer_idx=None): super().__init__() self.config = config max_positions = config.max_position_embeddings - self.register_buffer( - "bias", - torch.tril(torch.ones((max_positions, max_positions), dtype=torch.bool)).view( - 1, 1, max_positions, max_positions - ), - persistent=False, - ) + self.register_buffer("masked_bias", torch.tensor(-1e9), persistent=False) self.attn_dropout = nn.Dropout(config.attn_pdrop) @@ -161,22 +155,11 @@ def _attn( attention_mask=None, head_mask=None, ): - # compute causal mask from causal mask buffer - query_length, key_length = query.size(-2), key.size(-2) - causal_mask = self.bias[:, :, key_length - query_length : key_length, :key_length] - # Keep the attention weights computation in fp32 to avoid overflow issues query = query.to(torch.float32) key = key.to(torch.float32) attn_weights = torch.matmul(query, key.transpose(-1, -2)) - - mask_value = torch.finfo(attn_weights.dtype).min - # Need to be a tensor, otherwise we get error: `RuntimeError: expected scalar type float but found double`. - # Need to be on the same device, otherwise `RuntimeError: ..., x and y to be on the same device` - mask_value = torch.tensor(mask_value, dtype=attn_weights.dtype).to(attn_weights.device) - attn_weights = torch.where(causal_mask, attn_weights, mask_value) - attn_weights = attn_weights / self.scale_attn if attention_mask is not None: # no matter the length, we just slice it diff --git a/src/transformers/models/idefics/modeling_idefics.py b/src/transformers/models/idefics/modeling_idefics.py index 9c581045addc..073345379a62 100644 --- a/src/transformers/models/idefics/modeling_idefics.py +++ b/src/transformers/models/idefics/modeling_idefics.py @@ -655,7 +655,7 @@ def forward( kv_seq_len = key_states.shape[-2] if past_key_value is not None: - kv_seq_len += cache_position[0] # past_key_value.get_seq_length(self.layer_idx) + kv_seq_len += cache_position[0] if not is_cross_attention: cos, sin = self.rotary_emb(value_states, seq_len=max(kv_seq_len, q_len)) @@ -1692,13 +1692,13 @@ def _update_model_kwargs_for_generation( outputs: ModelOutput, model_kwargs: Dict[str, Any], is_encoder_decoder: bool = False, - standardize_cache_format: bool = False, + **kwargs, ) -> Dict[str, Any]: model_kwargs = super()._update_model_kwargs_for_generation( outputs, model_kwargs, is_encoder_decoder, - standardize_cache_format, + **kwargs, ) if "image_attention_mask" in model_kwargs: diff --git a/tests/models/phi3/test_modeling_phi3.py b/tests/models/phi3/test_modeling_phi3.py index ec3986ff2338..eb113a4df697 100644 --- a/tests/models/phi3/test_modeling_phi3.py +++ b/tests/models/phi3/test_modeling_phi3.py @@ -16,6 +16,7 @@ """Testing suite for the PyTorch Phi-3 model.""" import unittest +from typing import List from parameterized import parameterized @@ -69,7 +70,7 @@ def forward( ).logits @staticmethod - def generate(model: Phi3ForCausalLM, prompt_tokens: torch.LongTensor, max_seq_len: int) -> list[int]: + def generate(model: Phi3ForCausalLM, prompt_tokens: torch.LongTensor, max_seq_len: int) -> List[int]: model = Phi3MiniWithStaticCache(model, 1, max_seq_len + prompt_tokens.shape[-1]) response_tokens = [] diff --git a/tests/test_modeling_common.py b/tests/test_modeling_common.py index 148f33e048d5..7fc6c87b3077 100755 --- a/tests/test_modeling_common.py +++ b/tests/test_modeling_common.py @@ -4587,6 +4587,45 @@ def test_custom_4d_attention_mask(self): normalized_1 = F.softmax(out_shared_prefix_last_tokens) torch.testing.assert_close(normalized_0, normalized_1, rtol=1e-3, atol=1e-4) + @is_flaky("flaky for some models, reason unknown yet!") + def test_static_cache_matches_dynamic(self): + """ + Tests that generating with static cache give almost same results as with dynamic cache. + This test does not compile the model and check only logits similarity for numerical precision + errors. + """ + if len(self.all_generative_model_classes) == 0: + self.skipTest( + reason="Model architecture has no generative classes, and thus not necessarily supporting 4D masks" + ) + + for model_class in self.all_generative_model_classes: + if not model_class._supports_static_cache: + self.skipTest(f"{model_class.__name__} does not support static cache") + + if not model_class._supports_cache_class: + self.skipTest(f"{model_class.__name__} does not support cache class") + + config, inputs = self.model_tester.prepare_config_and_inputs_for_common() + if getattr(config, "sliding_window", 0) > 0: + self.skipTest(f"{model_class.__name__} with sliding window attention is not supported by this test") + + model = model_class(config).to(device=torch_device, dtype=torch.float32) + model.eval() + + dynamic_out = model.generate( + **inputs, do_sample=False, max_new_tokens=10, output_logits=True, return_dict_in_generate=True + ) + static_out = model.generate( + **inputs, + do_sample=False, + max_new_tokens=10, + cache_implementation="static", + output_logits=True, + return_dict_in_generate=True, + ) + self.assertTrue(torch.allclose(dynamic_out.logits[0], static_out.logits[0])) + # For now, Let's focus only on GPU for `torch.compile` @slow @require_torch_gpu From 45c3a1bd875d9d7296dad9fc639c11fc0d8b73b1 Mon Sep 17 00:00:00 2001 From: raushan Date: Fri, 2 Aug 2024 10:53:03 +0200 Subject: [PATCH 37/40] fix copies --- .../models/codegen/modeling_codegen.py | 88 +++++++++---- .../models/falcon/modeling_falcon.py | 88 +++++++++---- .../models/gpt_neo/modeling_gpt_neo.py | 88 +++++++++---- .../models/gpt_neox/modeling_gpt_neox.py | 116 ++++++++++++++---- src/transformers/models/gptj/modeling_gptj.py | 87 +++++++++---- .../models/idefics/modeling_idefics.py | 87 +++++++++---- tests/test_modeling_common.py | 3 +- 7 files changed, 427 insertions(+), 130 deletions(-) diff --git a/src/transformers/models/codegen/modeling_codegen.py b/src/transformers/models/codegen/modeling_codegen.py index 927f587ce337..66dd2e26d0e9 100644 --- a/src/transformers/models/codegen/modeling_codegen.py +++ b/src/transformers/models/codegen/modeling_codegen.py @@ -36,6 +36,60 @@ _CONFIG_FOR_DOC = "CodeGenConfig" +# Copied from transformers.models.llama.modeling_llama._prepare_4d_causal_attention_mask_with_cache_position +def _prepare_4d_causal_attention_mask_with_cache_position( + attention_mask: torch.Tensor, + sequence_length: int, + target_length: int, + dtype: torch.dtype, + device: torch.device, + min_dtype: float, + cache_position: torch.Tensor, + batch_size: int, +): + """ + Creates a causal 4D mask of shape `(batch_size, 1, query_length, key_value_length)` from a 2D mask of shape + `(batch_size, key_value_length)`, or if the input `attention_mask` is already 4D, do nothing. + + Args: + attention_mask (`torch.Tensor`): + A 2D attention mask of shape `(batch_size, key_value_length)` or a 4D attention mask of shape `(batch_size, 1, query_length, key_value_length)`. + sequence_length (`int`): + The sequence length being processed. + target_length (`int`): + The target length: when generating with static cache, the mask should be as long as the static cache, to account for the 0 padding, the part of the cache that is not filled yet. + dtype (`torch.dtype`): + The dtype to use for the 4D attention mask. + device (`torch.device`): + The device to plcae the 4D attention mask on. + min_dtype (`float`): + The minimum value representable with the dtype `dtype`. + cache_position (`torch.Tensor`): + Indices depicting the position of the input sequence tokens in the sequence. + batch_size (`torch.Tensor`): + Batch size. + """ + if attention_mask is not None and attention_mask.dim() == 4: + # In this case we assume that the mask comes already in inverted form and requires no inversion or slicing. + causal_mask = attention_mask + else: + causal_mask = torch.full((sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device) + if sequence_length != 1: + causal_mask = torch.triu(causal_mask, diagonal=1) + causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1) + causal_mask = causal_mask[None, None, :, :].expand(batch_size, 1, -1, -1) + if attention_mask is not None: + causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit + mask_length = attention_mask.shape[-1] + padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :] + padding_mask = padding_mask == 0 + causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill( + padding_mask, min_dtype + ) + + return causal_mask + + # Copied from transformers.models.gptj.modeling_gptj.create_sinusoidal_positions def create_sinusoidal_positions(num_pos: int, dim: int) -> torch.Tensor: inv_freq = 1.0 / (10000 ** (torch.arange(0, dim, 2, dtype=torch.int64) / dim)) @@ -614,27 +668,19 @@ def _update_causal_mask( if isinstance(attention_mask, torch.Tensor) else past_seen_tokens + sequence_length + 1 ) - if attention_mask is not None and attention_mask.dim() == 4: - # in this case we assume that the mask comes already in inverted form and requires no inversion or slicing - if attention_mask.max() != 0: - raise ValueError("Custom 4D attention mask should be passed in inverted form with max==0`") - causal_mask = attention_mask - else: - causal_mask = torch.full( - (sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device - ) - if sequence_length != 1: - causal_mask = torch.triu(causal_mask, diagonal=1) - causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1) - causal_mask = causal_mask[None, None, :, :].expand(input_tensor.shape[0], 1, -1, -1) - if attention_mask is not None: - causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit - mask_length = attention_mask.shape[-1] - padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :] - padding_mask = padding_mask == 0 - causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill( - padding_mask, min_dtype - ) + + # In case the provided `attention` mask is 2D, we generate a causal mask here (4D). + causal_mask = _prepare_4d_causal_attention_mask_with_cache_position( + attention_mask, + sequence_length=sequence_length, + target_length=target_length, + dtype=dtype, + device=device, + min_dtype=min_dtype, + cache_position=cache_position, + batch_size=input_tensor.shape[0], + ) + if ( self.config._attn_implementation == "sdpa" and attention_mask is not None diff --git a/src/transformers/models/falcon/modeling_falcon.py b/src/transformers/models/falcon/modeling_falcon.py index a51cf8011401..780a38f6b147 100644 --- a/src/transformers/models/falcon/modeling_falcon.py +++ b/src/transformers/models/falcon/modeling_falcon.py @@ -61,6 +61,60 @@ _CONFIG_FOR_DOC = "FalconConfig" +# Copied from transformers.models.llama.modeling_llama._prepare_4d_causal_attention_mask_with_cache_position +def _prepare_4d_causal_attention_mask_with_cache_position( + attention_mask: torch.Tensor, + sequence_length: int, + target_length: int, + dtype: torch.dtype, + device: torch.device, + min_dtype: float, + cache_position: torch.Tensor, + batch_size: int, +): + """ + Creates a causal 4D mask of shape `(batch_size, 1, query_length, key_value_length)` from a 2D mask of shape + `(batch_size, key_value_length)`, or if the input `attention_mask` is already 4D, do nothing. + + Args: + attention_mask (`torch.Tensor`): + A 2D attention mask of shape `(batch_size, key_value_length)` or a 4D attention mask of shape `(batch_size, 1, query_length, key_value_length)`. + sequence_length (`int`): + The sequence length being processed. + target_length (`int`): + The target length: when generating with static cache, the mask should be as long as the static cache, to account for the 0 padding, the part of the cache that is not filled yet. + dtype (`torch.dtype`): + The dtype to use for the 4D attention mask. + device (`torch.device`): + The device to plcae the 4D attention mask on. + min_dtype (`float`): + The minimum value representable with the dtype `dtype`. + cache_position (`torch.Tensor`): + Indices depicting the position of the input sequence tokens in the sequence. + batch_size (`torch.Tensor`): + Batch size. + """ + if attention_mask is not None and attention_mask.dim() == 4: + # In this case we assume that the mask comes already in inverted form and requires no inversion or slicing. + causal_mask = attention_mask + else: + causal_mask = torch.full((sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device) + if sequence_length != 1: + causal_mask = torch.triu(causal_mask, diagonal=1) + causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1) + causal_mask = causal_mask[None, None, :, :].expand(batch_size, 1, -1, -1) + if attention_mask is not None: + causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit + mask_length = attention_mask.shape[-1] + padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :] + padding_mask = padding_mask == 0 + causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill( + padding_mask, min_dtype + ) + + return causal_mask + + # NOTE(Hesslow): Unfortunately we did not fuse matmul and bias during training, this means that there's one additional quantization to bfloat16 between the operations. # In order not to degrade the quality of our HF-port, we keep these characteristics in the final model. class FalconLinear(nn.Linear): @@ -1118,27 +1172,19 @@ def _update_causal_mask( if isinstance(attention_mask, torch.Tensor) else past_seen_tokens + sequence_length + 1 ) - if attention_mask is not None and attention_mask.dim() == 4: - # in this case we assume that the mask comes already in inverted form and requires no inversion or slicing - if attention_mask.max() != 0: - raise ValueError("Custom 4D attention mask should be passed in inverted form with max==0`") - causal_mask = attention_mask - else: - causal_mask = torch.full( - (sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device - ) - if sequence_length != 1: - causal_mask = torch.triu(causal_mask, diagonal=1) - causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1) - causal_mask = causal_mask[None, None, :, :].expand(input_tensor.shape[0], 1, -1, -1) - if attention_mask is not None: - causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit - mask_length = attention_mask.shape[-1] - padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :] - padding_mask = padding_mask == 0 - causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill( - padding_mask, min_dtype - ) + + # In case the provided `attention` mask is 2D, we generate a causal mask here (4D). + causal_mask = _prepare_4d_causal_attention_mask_with_cache_position( + attention_mask, + sequence_length=sequence_length, + target_length=target_length, + dtype=dtype, + device=device, + min_dtype=min_dtype, + cache_position=cache_position, + batch_size=input_tensor.shape[0], + ) + if ( self.config._attn_implementation == "sdpa" and attention_mask is not None diff --git a/src/transformers/models/gpt_neo/modeling_gpt_neo.py b/src/transformers/models/gpt_neo/modeling_gpt_neo.py index 5bf7eb8818c4..698025b10c55 100755 --- a/src/transformers/models/gpt_neo/modeling_gpt_neo.py +++ b/src/transformers/models/gpt_neo/modeling_gpt_neo.py @@ -69,6 +69,60 @@ _CHECKPOINT_FOR_DOC = "EleutherAI/gpt-neo-1.3B" +# Copied from transformers.models.llama.modeling_llama._prepare_4d_causal_attention_mask_with_cache_position +def _prepare_4d_causal_attention_mask_with_cache_position( + attention_mask: torch.Tensor, + sequence_length: int, + target_length: int, + dtype: torch.dtype, + device: torch.device, + min_dtype: float, + cache_position: torch.Tensor, + batch_size: int, +): + """ + Creates a causal 4D mask of shape `(batch_size, 1, query_length, key_value_length)` from a 2D mask of shape + `(batch_size, key_value_length)`, or if the input `attention_mask` is already 4D, do nothing. + + Args: + attention_mask (`torch.Tensor`): + A 2D attention mask of shape `(batch_size, key_value_length)` or a 4D attention mask of shape `(batch_size, 1, query_length, key_value_length)`. + sequence_length (`int`): + The sequence length being processed. + target_length (`int`): + The target length: when generating with static cache, the mask should be as long as the static cache, to account for the 0 padding, the part of the cache that is not filled yet. + dtype (`torch.dtype`): + The dtype to use for the 4D attention mask. + device (`torch.device`): + The device to plcae the 4D attention mask on. + min_dtype (`float`): + The minimum value representable with the dtype `dtype`. + cache_position (`torch.Tensor`): + Indices depicting the position of the input sequence tokens in the sequence. + batch_size (`torch.Tensor`): + Batch size. + """ + if attention_mask is not None and attention_mask.dim() == 4: + # In this case we assume that the mask comes already in inverted form and requires no inversion or slicing. + causal_mask = attention_mask + else: + causal_mask = torch.full((sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device) + if sequence_length != 1: + causal_mask = torch.triu(causal_mask, diagonal=1) + causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1) + causal_mask = causal_mask[None, None, :, :].expand(batch_size, 1, -1, -1) + if attention_mask is not None: + causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit + mask_length = attention_mask.shape[-1] + padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :] + padding_mask = padding_mask == 0 + causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill( + padding_mask, min_dtype + ) + + return causal_mask + + def load_tf_weights_in_gpt_neo(model, config, gpt_neo_checkpoint_path): """Load tf checkpoints in a pytorch model""" try: @@ -819,27 +873,19 @@ def _update_causal_mask( if isinstance(attention_mask, torch.Tensor) else past_seen_tokens + sequence_length + 1 ) - if attention_mask is not None and attention_mask.dim() == 4: - # in this case we assume that the mask comes already in inverted form and requires no inversion or slicing - if attention_mask.max() != 0: - raise ValueError("Custom 4D attention mask should be passed in inverted form with max==0`") - causal_mask = attention_mask - else: - causal_mask = torch.full( - (sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device - ) - if sequence_length != 1: - causal_mask = torch.triu(causal_mask, diagonal=1) - causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1) - causal_mask = causal_mask[None, None, :, :].expand(input_tensor.shape[0], 1, -1, -1) - if attention_mask is not None: - causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit - mask_length = attention_mask.shape[-1] - padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :] - padding_mask = padding_mask == 0 - causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill( - padding_mask, min_dtype - ) + + # In case the provided `attention` mask is 2D, we generate a causal mask here (4D). + causal_mask = _prepare_4d_causal_attention_mask_with_cache_position( + attention_mask, + sequence_length=sequence_length, + target_length=target_length, + dtype=dtype, + device=device, + min_dtype=min_dtype, + cache_position=cache_position, + batch_size=input_tensor.shape[0], + ) + if ( self.config._attn_implementation == "sdpa" and attention_mask is not None diff --git a/src/transformers/models/gpt_neox/modeling_gpt_neox.py b/src/transformers/models/gpt_neox/modeling_gpt_neox.py index c7b839009c2f..06b6c5ef70c9 100755 --- a/src/transformers/models/gpt_neox/modeling_gpt_neox.py +++ b/src/transformers/models/gpt_neox/modeling_gpt_neox.py @@ -53,6 +53,60 @@ _CONFIG_FOR_DOC = "GPTNeoXConfig" +# Copied from transformers.models.llama.modeling_llama._prepare_4d_causal_attention_mask_with_cache_position +def _prepare_4d_causal_attention_mask_with_cache_position( + attention_mask: torch.Tensor, + sequence_length: int, + target_length: int, + dtype: torch.dtype, + device: torch.device, + min_dtype: float, + cache_position: torch.Tensor, + batch_size: int, +): + """ + Creates a causal 4D mask of shape `(batch_size, 1, query_length, key_value_length)` from a 2D mask of shape + `(batch_size, key_value_length)`, or if the input `attention_mask` is already 4D, do nothing. + + Args: + attention_mask (`torch.Tensor`): + A 2D attention mask of shape `(batch_size, key_value_length)` or a 4D attention mask of shape `(batch_size, 1, query_length, key_value_length)`. + sequence_length (`int`): + The sequence length being processed. + target_length (`int`): + The target length: when generating with static cache, the mask should be as long as the static cache, to account for the 0 padding, the part of the cache that is not filled yet. + dtype (`torch.dtype`): + The dtype to use for the 4D attention mask. + device (`torch.device`): + The device to plcae the 4D attention mask on. + min_dtype (`float`): + The minimum value representable with the dtype `dtype`. + cache_position (`torch.Tensor`): + Indices depicting the position of the input sequence tokens in the sequence. + batch_size (`torch.Tensor`): + Batch size. + """ + if attention_mask is not None and attention_mask.dim() == 4: + # In this case we assume that the mask comes already in inverted form and requires no inversion or slicing. + causal_mask = attention_mask + else: + causal_mask = torch.full((sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device) + if sequence_length != 1: + causal_mask = torch.triu(causal_mask, diagonal=1) + causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1) + causal_mask = causal_mask[None, None, :, :].expand(batch_size, 1, -1, -1) + if attention_mask is not None: + causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit + mask_length = attention_mask.shape[-1] + padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :] + padding_mask = padding_mask == 0 + causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill( + padding_mask, min_dtype + ) + + return causal_mask + + class GPTNeoXPreTrainedModel(PreTrainedModel): """ An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained @@ -999,27 +1053,19 @@ def _update_causal_mask( if isinstance(attention_mask, torch.Tensor) else past_seen_tokens + sequence_length + 1 ) - if attention_mask is not None and attention_mask.dim() == 4: - # in this case we assume that the mask comes already in inverted form and requires no inversion or slicing - if attention_mask.max() != 0: - raise ValueError("Custom 4D attention mask should be passed in inverted form with max==0`") - causal_mask = attention_mask - else: - causal_mask = torch.full( - (sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device - ) - if sequence_length != 1: - causal_mask = torch.triu(causal_mask, diagonal=1) - causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1) - causal_mask = causal_mask[None, None, :, :].expand(input_tensor.shape[0], 1, -1, -1) - if attention_mask is not None: - causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit - mask_length = attention_mask.shape[-1] - padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :] - padding_mask = padding_mask == 0 - causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill( - padding_mask, min_dtype - ) + + # In case the provided `attention` mask is 2D, we generate a causal mask here (4D). + causal_mask = _prepare_4d_causal_attention_mask_with_cache_position( + attention_mask, + sequence_length=sequence_length, + target_length=target_length, + dtype=dtype, + device=device, + min_dtype=min_dtype, + cache_position=cache_position, + batch_size=input_tensor.shape[0], + ) + if ( self.config._attn_implementation == "sdpa" and attention_mask is not None @@ -1140,7 +1186,6 @@ def forward( attentions=outputs.attentions, ) - # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.prepare_inputs_for_generation def prepare_inputs_for_generation( self, input_ids, @@ -1168,11 +1213,36 @@ def prepare_inputs_for_generation( if past_key_values: position_ids = position_ids[:, -input_ids.shape[1] :] + # This `clone` call is needed to avoid recapturing cuda graphs with `torch.compile`'s `mode="reduce-overhead`, as otherwise the input `position_ids` would have various stride during the decoding. Here, simply using `.contiguous()` is not sufficient as in the batch size = 1 case, `position_ids` is already contiguous but with varying stride which retriggers a capture. + position_ids = position_ids.clone(memory_format=torch.contiguous_format) + # if `inputs_embeds` are passed, we only want to use them in the 1st generation step if inputs_embeds is not None and cache_position[0] == 0: model_inputs = {"inputs_embeds": inputs_embeds} else: - model_inputs = {"input_ids": input_ids.contiguous()} # `contiguous()` needed for compilation use cases + model_inputs = {"input_ids": input_ids} + + if isinstance(past_key_values, StaticCache) and attention_mask.ndim == 2: + if inputs_embeds is not None: + batch_size, sequence_length = inputs_embeds.shape + device = inputs_embeds.device + else: + batch_size, sequence_length = input_ids.shape + device = input_ids.device + + dtype = self.embed_out.weight.dtype + min_dtype = torch.finfo(dtype).min + + attention_mask = _prepare_4d_causal_attention_mask_with_cache_position( + attention_mask, + sequence_length=sequence_length, + target_length=past_key_values.get_max_length(), + dtype=dtype, + device=device, + min_dtype=min_dtype, + cache_position=cache_position, + batch_size=batch_size, + ) model_inputs.update( { diff --git a/src/transformers/models/gptj/modeling_gptj.py b/src/transformers/models/gptj/modeling_gptj.py index 620b95418a51..79c0a3466c8e 100644 --- a/src/transformers/models/gptj/modeling_gptj.py +++ b/src/transformers/models/gptj/modeling_gptj.py @@ -57,6 +57,60 @@ _CONFIG_FOR_DOC = "GPTJConfig" +# Copied from transformers.models.llama.modeling_llama._prepare_4d_causal_attention_mask_with_cache_position +def _prepare_4d_causal_attention_mask_with_cache_position( + attention_mask: torch.Tensor, + sequence_length: int, + target_length: int, + dtype: torch.dtype, + device: torch.device, + min_dtype: float, + cache_position: torch.Tensor, + batch_size: int, +): + """ + Creates a causal 4D mask of shape `(batch_size, 1, query_length, key_value_length)` from a 2D mask of shape + `(batch_size, key_value_length)`, or if the input `attention_mask` is already 4D, do nothing. + + Args: + attention_mask (`torch.Tensor`): + A 2D attention mask of shape `(batch_size, key_value_length)` or a 4D attention mask of shape `(batch_size, 1, query_length, key_value_length)`. + sequence_length (`int`): + The sequence length being processed. + target_length (`int`): + The target length: when generating with static cache, the mask should be as long as the static cache, to account for the 0 padding, the part of the cache that is not filled yet. + dtype (`torch.dtype`): + The dtype to use for the 4D attention mask. + device (`torch.device`): + The device to plcae the 4D attention mask on. + min_dtype (`float`): + The minimum value representable with the dtype `dtype`. + cache_position (`torch.Tensor`): + Indices depicting the position of the input sequence tokens in the sequence. + batch_size (`torch.Tensor`): + Batch size. + """ + if attention_mask is not None and attention_mask.dim() == 4: + # In this case we assume that the mask comes already in inverted form and requires no inversion or slicing. + causal_mask = attention_mask + else: + causal_mask = torch.full((sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device) + if sequence_length != 1: + causal_mask = torch.triu(causal_mask, diagonal=1) + causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1) + causal_mask = causal_mask[None, None, :, :].expand(batch_size, 1, -1, -1) + if attention_mask is not None: + causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit + mask_length = attention_mask.shape[-1] + padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :] + padding_mask = padding_mask == 0 + causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill( + padding_mask, min_dtype + ) + + return causal_mask + + def create_sinusoidal_positions(num_pos: int, dim: int) -> torch.Tensor: inv_freq = 1.0 / (10000 ** (torch.arange(0, dim, 2, dtype=torch.int64) / dim)) sinusoid_inp = torch.einsum("i , j -> i j", torch.arange(num_pos, dtype=torch.int64).float(), inv_freq).float() @@ -926,27 +980,18 @@ def _update_causal_mask( else past_seen_tokens + sequence_length + 1 ) - if attention_mask is not None and attention_mask.dim() == 4: - # in this case we assume that the mask comes already in inverted form and requires no inversion or slicing - if attention_mask.max() != 0: - raise ValueError("Custom 4D attention mask should be passed in inverted form with max==0`") - causal_mask = attention_mask - else: - causal_mask = torch.full( - (sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device - ) - if sequence_length != 1: - causal_mask = torch.triu(causal_mask, diagonal=1) - causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1) - causal_mask = causal_mask[None, None, :, :].expand(input_tensor.shape[0], 1, -1, -1) - if attention_mask is not None: - causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit - mask_length = attention_mask.shape[-1] - padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :] - padding_mask = padding_mask == 0 - causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill( - padding_mask, min_dtype - ) + # In case the provided `attention` mask is 2D, we generate a causal mask here (4D). + causal_mask = _prepare_4d_causal_attention_mask_with_cache_position( + attention_mask, + sequence_length=sequence_length, + target_length=target_length, + dtype=dtype, + device=device, + min_dtype=min_dtype, + cache_position=cache_position, + batch_size=input_tensor.shape[0], + ) + if ( self.config._attn_implementation == "sdpa" and attention_mask is not None diff --git a/src/transformers/models/idefics/modeling_idefics.py b/src/transformers/models/idefics/modeling_idefics.py index 073345379a62..9f53c86b3c41 100644 --- a/src/transformers/models/idefics/modeling_idefics.py +++ b/src/transformers/models/idefics/modeling_idefics.py @@ -51,6 +51,60 @@ _CONFIG_FOR_DOC = "IdeficsConfig" +# Copied from transformers.models.llama.modeling_llama._prepare_4d_causal_attention_mask_with_cache_position +def _prepare_4d_causal_attention_mask_with_cache_position( + attention_mask: torch.Tensor, + sequence_length: int, + target_length: int, + dtype: torch.dtype, + device: torch.device, + min_dtype: float, + cache_position: torch.Tensor, + batch_size: int, +): + """ + Creates a causal 4D mask of shape `(batch_size, 1, query_length, key_value_length)` from a 2D mask of shape + `(batch_size, key_value_length)`, or if the input `attention_mask` is already 4D, do nothing. + + Args: + attention_mask (`torch.Tensor`): + A 2D attention mask of shape `(batch_size, key_value_length)` or a 4D attention mask of shape `(batch_size, 1, query_length, key_value_length)`. + sequence_length (`int`): + The sequence length being processed. + target_length (`int`): + The target length: when generating with static cache, the mask should be as long as the static cache, to account for the 0 padding, the part of the cache that is not filled yet. + dtype (`torch.dtype`): + The dtype to use for the 4D attention mask. + device (`torch.device`): + The device to plcae the 4D attention mask on. + min_dtype (`float`): + The minimum value representable with the dtype `dtype`. + cache_position (`torch.Tensor`): + Indices depicting the position of the input sequence tokens in the sequence. + batch_size (`torch.Tensor`): + Batch size. + """ + if attention_mask is not None and attention_mask.dim() == 4: + # In this case we assume that the mask comes already in inverted form and requires no inversion or slicing. + causal_mask = attention_mask + else: + causal_mask = torch.full((sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device) + if sequence_length != 1: + causal_mask = torch.triu(causal_mask, diagonal=1) + causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1) + causal_mask = causal_mask[None, None, :, :].expand(batch_size, 1, -1, -1) + if attention_mask is not None: + causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit + mask_length = attention_mask.shape[-1] + padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :] + padding_mask = padding_mask == 0 + causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill( + padding_mask, min_dtype + ) + + return causal_mask + + @dataclass class IdeficsBaseModelOutputWithPast(ModelOutput): """ @@ -1461,27 +1515,18 @@ def _update_causal_mask( else past_seen_tokens + sequence_length + 1 ) - if attention_mask is not None and attention_mask.dim() == 4: - # in this case we assume that the mask comes already in inverted form and requires no inversion or slicing - if attention_mask.max() != 0: - raise ValueError("Custom 4D attention mask should be passed in inverted form with max==0`") - causal_mask = attention_mask - else: - causal_mask = torch.full( - (sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=device - ) - if sequence_length != 1: - causal_mask = torch.triu(causal_mask, diagonal=1) - causal_mask *= torch.arange(target_length, device=device) > cache_position.reshape(-1, 1) - causal_mask = causal_mask[None, None, :, :].expand(input_tensor.shape[0], 1, -1, -1) - if attention_mask is not None: - causal_mask = causal_mask.clone() # copy to contiguous memory for in-place edit - mask_length = attention_mask.shape[-1] - padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :] - padding_mask = padding_mask == 0 - causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill( - padding_mask, min_dtype - ) + # In case the provided `attention` mask is 2D, we generate a causal mask here (4D). + causal_mask = _prepare_4d_causal_attention_mask_with_cache_position( + attention_mask, + sequence_length=sequence_length, + target_length=target_length, + dtype=dtype, + device=device, + min_dtype=min_dtype, + cache_position=cache_position, + batch_size=input_tensor.shape[0], + ) + if ( self.config._attn_implementation == "sdpa" and attention_mask is not None diff --git a/tests/test_modeling_common.py b/tests/test_modeling_common.py index 7fc6c87b3077..4a29942641ea 100755 --- a/tests/test_modeling_common.py +++ b/tests/test_modeling_common.py @@ -4587,7 +4587,6 @@ def test_custom_4d_attention_mask(self): normalized_1 = F.softmax(out_shared_prefix_last_tokens) torch.testing.assert_close(normalized_0, normalized_1, rtol=1e-3, atol=1e-4) - @is_flaky("flaky for some models, reason unknown yet!") def test_static_cache_matches_dynamic(self): """ Tests that generating with static cache give almost same results as with dynamic cache. @@ -4624,7 +4623,7 @@ def test_static_cache_matches_dynamic(self): output_logits=True, return_dict_in_generate=True, ) - self.assertTrue(torch.allclose(dynamic_out.logits[0], static_out.logits[0])) + self.assertTrue(torch.allclose(dynamic_out.logits[0], static_out.logits[0], rtol=1e-3, atol=1e-4)) # For now, Let's focus only on GPU for `torch.compile` @slow From 5f2261615170e308e1c266e1e3e88dc2c668b319 Mon Sep 17 00:00:00 2001 From: raushan Date: Fri, 2 Aug 2024 13:13:05 +0200 Subject: [PATCH 38/40] return sliding window mask --- src/transformers/models/gpt_neo/modeling_gpt_neo.py | 11 ++++++++++- src/transformers/models/gptj/modeling_gptj.py | 2 -- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/transformers/models/gpt_neo/modeling_gpt_neo.py b/src/transformers/models/gpt_neo/modeling_gpt_neo.py index 698025b10c55..31128748c671 100755 --- a/src/transformers/models/gpt_neo/modeling_gpt_neo.py +++ b/src/transformers/models/gpt_neo/modeling_gpt_neo.py @@ -264,6 +264,15 @@ def _attn(self, query, key, value, attention_mask=None, head_mask=None): attn_weights = torch.matmul(query, key.transpose(-1, -2)) + # Apply sliding window masking for local attention layers + query_length, key_length = query.size(-2), key.size(-2) + causal_mask = self.bias[:, :, key_length - query_length : key_length, :key_length] + mask_value = torch.finfo(attn_weights.dtype).min + # Need to be a tensor, otherwise we get error: `RuntimeError: expected scalar type float but found double`. + # Need to be on the same device, otherwise `RuntimeError: ..., x and y to be on the same device` + mask_value = torch.tensor(mask_value, dtype=attn_weights.dtype).to(attn_weights.device) + attn_weights = torch.where(causal_mask, attn_weights, mask_value) + if attention_mask is not None: # no matter the length, we just slice it causal_mask = attention_mask[:, :, :, : key.shape[-2]] attn_weights = attn_weights + causal_mask @@ -543,7 +552,7 @@ class GPTNeoPreTrainedModel(PreTrainedModel): _supports_flash_attn_2 = True _supports_cache_class = True _supports_quantized_cache = True - _supports_static_cache = True + _supports_static_cache = False # TODO: needs a HybridCache def __init__(self, *inputs, **kwargs): super().__init__(*inputs, **kwargs) diff --git a/src/transformers/models/gptj/modeling_gptj.py b/src/transformers/models/gptj/modeling_gptj.py index 79c0a3466c8e..159d139594a3 100644 --- a/src/transformers/models/gptj/modeling_gptj.py +++ b/src/transformers/models/gptj/modeling_gptj.py @@ -141,8 +141,6 @@ def __init__(self, config, layer_idx=None): self.config = config max_positions = config.max_position_embeddings - self.register_buffer("masked_bias", torch.tensor(-1e9), persistent=False) - self.attn_dropout = nn.Dropout(config.attn_pdrop) self.resid_dropout = nn.Dropout(config.resid_pdrop) From f5af6a284939a4d33235a1bee247876c48e5cc64 Mon Sep 17 00:00:00 2001 From: raushan Date: Tue, 6 Aug 2024 07:52:00 +0200 Subject: [PATCH 39/40] run slow tests & fix + codestyle --- src/transformers/generation/utils.py | 2 +- .../models/codegen/modeling_codegen.py | 85 ++++++++------- .../models/falcon/modeling_falcon.py | 100 +++++++++++------- .../models/gpt_neo/modeling_gpt_neo.py | 83 +++++++-------- .../models/gpt_neox/modeling_gpt_neox.py | 12 ++- src/transformers/models/gptj/modeling_gptj.py | 85 ++++++++------- .../models/idefics/modeling_idefics.py | 31 +++--- 7 files changed, 207 insertions(+), 191 deletions(-) diff --git a/src/transformers/generation/utils.py b/src/transformers/generation/utils.py index 385d68cfbef3..ed369e0d9061 100644 --- a/src/transformers/generation/utils.py +++ b/src/transformers/generation/utils.py @@ -1470,7 +1470,7 @@ def _get_cache(self, cache_implementation: str, max_batch_size: int, max_cache_l # NOTE: self.dtype is not compatible with torch.compile, as it calls `self.parameters()`. # Workaround: trust the lm_head, whose attribute name is somewhat consistent across generative # models. May cause trobles with non-text modalities. - cache_dtype = self.lm_head.weight.dtype + cache_dtype = self.get_output_embeddings().weight.dtype cache_kwargs = { "config": self.config, diff --git a/src/transformers/models/codegen/modeling_codegen.py b/src/transformers/models/codegen/modeling_codegen.py index 66dd2e26d0e9..6452c2afa0b7 100644 --- a/src/transformers/models/codegen/modeling_codegen.py +++ b/src/transformers/models/codegen/modeling_codegen.py @@ -526,13 +526,14 @@ def forward( inputs_embeds = self.wte(input_ids) use_legacy_cache = False - if use_cache and not isinstance(past_key_values, Cache) and not self.training: + if use_cache and not isinstance(past_key_values, Cache): use_legacy_cache = True past_key_values = DynamicCache.from_legacy_cache(past_key_values) - logger.warning_once( - "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.45. " - "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" - ) + if not self.training: + logger.warning_once( + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.45. " + "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" + ) seq_length = inputs_embeds.shape[1] if cache_position is None: @@ -723,45 +724,26 @@ def prepare_inputs_for_generation( self, input_ids, attention_mask=None, + token_type_ids=None, + position_ids=None, past_key_values=None, inputs_embeds=None, cache_position=None, use_cache=True, **kwargs, ): - token_type_ids = kwargs.get("token_type_ids", None) - past_length = 0 - # Omit tokens covered by past_key_values + # If we have cache: let's slice `input_ids` through `cache_position`, to keep only the unprocessed tokens + # Exception 1: when passing input_embeds, input_ids may be missing entries + # Exception 2: some generation methods do special slicing of input_ids, so we don't need to do it here if past_key_values is not None: - past_length = cache_position[0] if cache_position is not None else past_key_values.get_seq_length() - max_cache_length = ( - torch.tensor(past_key_values.get_max_length(), device=input_ids.device) - if past_key_values.get_max_length() is not None - else None - ) - cache_length = past_length if max_cache_length is None else torch.min(max_cache_length, past_length) - - # Some generation methods already pass only the last input ID - if input_ids.shape[1] > past_length: - remove_prefix_length = past_length - else: - # Default to old behavior: keep only final ID - remove_prefix_length = input_ids.shape[1] - 1 - - input_ids = input_ids[:, remove_prefix_length:] + if inputs_embeds is not None: # Exception 1 + input_ids = input_ids[:, -cache_position.shape[0] :] + elif input_ids.shape[1] != cache_position.shape[0]: # Default case (the "else", a no op, is Exception 2) + input_ids = input_ids[:, cache_position] if token_type_ids is not None: token_type_ids = token_type_ids[:, -input_ids.shape[1] :] - # If we are about to go beyond the maximum cache length, we need to crop the input attention mask. - if ( - max_cache_length is not None - and attention_mask is not None - and cache_length + input_ids.shape[1] > max_cache_length - ): - attention_mask = attention_mask[:, -max_cache_length:] - - position_ids = kwargs.get("position_ids", None) if attention_mask is not None and position_ids is None: # create position_ids on the fly for batch generation position_ids = attention_mask.long().cumsum(-1) - 1 @@ -769,30 +751,47 @@ def prepare_inputs_for_generation( if past_key_values: position_ids = position_ids[:, -input_ids.shape[1] :] + # This `clone` call is needed to avoid recapturing cuda graphs with `torch.compile`'s `mode="reduce-overhead`, as otherwise the input `position_ids` would have various stride during the decoding. Here, simply using `.contiguous()` is not sufficient as in the batch size = 1 case, `position_ids` is already contiguous but with varying stride which retriggers a capture. + position_ids = position_ids.clone(memory_format=torch.contiguous_format) + # if `inputs_embeds` are passed, we only want to use them in the 1st generation step - if inputs_embeds is not None and past_length == 0: + if inputs_embeds is not None and cache_position[0] == 0: model_inputs = {"inputs_embeds": inputs_embeds} else: model_inputs = {"input_ids": input_ids} - if use_cache: - input_length = input_ids.shape[-1] - if cache_position is None: - cache_position = torch.arange(past_length, past_length + input_length, device=input_ids.device) + if isinstance(past_key_values, StaticCache) and attention_mask.ndim == 2: + if inputs_embeds is not None: + batch_size, sequence_length = inputs_embeds.shape + device = inputs_embeds.device else: - cache_position = cache_position[-input_length:] + batch_size, sequence_length = input_ids.shape + device = input_ids.device + + dtype = self.lm_head.weight.dtype + min_dtype = torch.finfo(dtype).min + + attention_mask = _prepare_4d_causal_attention_mask_with_cache_position( + attention_mask, + sequence_length=sequence_length, + target_length=past_key_values.get_max_length(), + dtype=dtype, + device=device, + min_dtype=min_dtype, + cache_position=cache_position, + batch_size=batch_size, + ) model_inputs.update( { + "position_ids": position_ids, + "cache_position": cache_position, "past_key_values": past_key_values, "use_cache": use_cache, - "position_ids": position_ids, - "attention_mask": attention_mask, "token_type_ids": token_type_ids, - "cache_position": cache_position, + "attention_mask": attention_mask, } ) - return model_inputs @add_start_docstrings_to_model_forward(CODEGEN_INPUTS_DOCSTRING.format("batch_size, sequence_length")) diff --git a/src/transformers/models/falcon/modeling_falcon.py b/src/transformers/models/falcon/modeling_falcon.py index 780a38f6b147..6ea7147366a0 100644 --- a/src/transformers/models/falcon/modeling_falcon.py +++ b/src/transformers/models/falcon/modeling_falcon.py @@ -1023,13 +1023,14 @@ def forward( # Compute alibi tensor: check build_alibi_tensor documentation use_legacy_cache = False - if use_cache and not isinstance(past_key_values, Cache) and not self.training: + if use_cache and not isinstance(past_key_values, Cache): use_legacy_cache = True past_key_values = DynamicCache.from_legacy_cache(past_key_values) - logger.warning_once( - "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.45. " - "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" - ) + if not self.training: + logger.warning_once( + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.45. " + "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" + ) alibi = None past_key_values_length = past_key_values.get_seq_length() if past_key_values is not None else 0 @@ -1053,7 +1054,7 @@ def forward( position_ids = cache_position.unsqueeze(0) causal_mask = self._update_causal_mask( - attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions + attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions, head_mask, alibi ) # Prepare head mask if needed @@ -1126,7 +1127,6 @@ def forward( attentions=all_self_attentions, ) - # Copied from transformers.models.llama.modeling_llama.LlamaModel._update_causal_mask def _update_causal_mask( self, attention_mask: torch.Tensor, @@ -1134,6 +1134,8 @@ def _update_causal_mask( cache_position: torch.Tensor, past_key_values: Cache, output_attentions: bool, + head_mask: torch.Tensor, + alibi: torch.Tensor, ): # TODO: As of torch==2.2.0, the `attention_mask` passed to the model in `generate` is 2D and of dynamic length even when the static # KV cache is used. This is an issue for torch.compile which then recaptures cudagraphs at each decode steps due to the dynamic shapes. @@ -1152,7 +1154,13 @@ def _update_causal_mask( using_static_cache = isinstance(past_key_values, StaticCache) # When output attentions is True, sdpa implementation's forward method calls the eager implementation's forward - if self.config._attn_implementation == "sdpa" and not using_static_cache and not output_attentions: + if ( + self.config._attn_implementation == "sdpa" + and not using_static_cache + and not output_attentions + and head_mask is None + and alibi is None + ): if AttentionMaskConverter._ignore_causal_mask_sdpa( attention_mask, inputs_embeds=input_tensor, @@ -1163,7 +1171,7 @@ def _update_causal_mask( dtype, device = input_tensor.dtype, input_tensor.device min_dtype = torch.finfo(dtype).min - sequence_length = input_tensor.shape[1] + batch_size, sequence_length, _ = input_tensor.shape if using_static_cache: target_length = past_key_values.get_max_length() else: @@ -1185,6 +1193,15 @@ def _update_causal_mask( batch_size=input_tensor.shape[0], ) + # We take care to integrate alibi bias in the causal_mask here + if head_mask is None and alibi is not None: + alibi = alibi.reshape(batch_size, -1, *alibi.shape[1:]) + causal_mask = torch.masked_fill( + alibi / math.sqrt(self.config.hidden_size // self.num_heads), + causal_mask < -1, + min_dtype, + ) + if ( self.config._attn_implementation == "sdpa" and attention_mask is not None @@ -1231,31 +1248,14 @@ def prepare_inputs_for_generation( use_cache: bool = True, **kwargs, ) -> dict: - past_length = 0 + # If we have cache: let's slice `input_ids` through `cache_position`, to keep only the unprocessed tokens + # Exception 1: when passing input_embeds, input_ids may be missing entries + # Exception 2: some generation methods do special slicing of input_ids, so we don't need to do it here if past_key_values is not None: - past_length = cache_position[0] if cache_position is not None else past_key_values.get_seq_length() - max_cache_length = ( - torch.tensor(past_key_values.get_max_length(), device=input_ids.device) - if past_key_values.get_max_length() is not None - else None - ) - cache_length = past_length if max_cache_length is None else torch.min(max_cache_length, past_length) - # Some generation methods already pass only the last input ID - if input_ids.shape[1] > past_length: - remove_prefix_length = past_length - else: - # Default to old behavior: keep only final ID - remove_prefix_length = input_ids.shape[1] - 1 - - input_ids = input_ids[:, remove_prefix_length:] - - # If we are about to go beyond the maximum cache length, we need to crop the input attention mask. - if ( - max_cache_length is not None - and attention_mask is not None - and cache_length + input_ids.shape[1] > max_cache_length - ): - attention_mask = attention_mask[:, -max_cache_length:] + if inputs_embeds is not None: # Exception 1 + input_ids = input_ids[:, -cache_position.shape[0] :] + elif input_ids.shape[1] != cache_position.shape[0]: # Default case (the "else", a no op, is Exception 2) + input_ids = input_ids[:, cache_position] # Note: versions of Falcon with alibi do not use position_ids. It is used with RoPE. if not self.transformer.use_alibi and attention_mask is not None and position_ids is None: @@ -1265,24 +1265,44 @@ def prepare_inputs_for_generation( if past_key_values: position_ids = position_ids[:, -input_ids.shape[1] :] - if inputs_embeds is not None and past_length == 0: + # This `clone` call is needed to avoid recapturing cuda graphs with `torch.compile`'s `mode="reduce-overhead`, as otherwise the input `position_ids` would have various stride during the decoding. Here, simply using `.contiguous()` is not sufficient as in the batch size = 1 case, `position_ids` is already contiguous but with varying stride which retriggers a capture. + position_ids = position_ids.clone(memory_format=torch.contiguous_format) + + # if `inputs_embeds` are passed, we only want to use them in the 1st generation step + if inputs_embeds is not None and cache_position[0] == 0: model_inputs = {"inputs_embeds": inputs_embeds} else: model_inputs = {"input_ids": input_ids} - input_length = position_ids.shape[-1] if position_ids is not None else input_ids.shape[-1] - if cache_position is None: - cache_position = torch.arange(past_length, past_length + input_length, device=input_ids.device) - elif use_cache: - cache_position = cache_position[-input_length:] + if isinstance(past_key_values, StaticCache) and attention_mask.ndim == 2: + if inputs_embeds is not None: + batch_size, sequence_length = inputs_embeds.shape + device = inputs_embeds.device + else: + batch_size, sequence_length = input_ids.shape + device = input_ids.device + + dtype = self.lm_head.weight.dtype + min_dtype = torch.finfo(dtype).min + + attention_mask = _prepare_4d_causal_attention_mask_with_cache_position( + attention_mask, + sequence_length=sequence_length, + target_length=past_key_values.get_max_length(), + dtype=dtype, + device=device, + min_dtype=min_dtype, + cache_position=cache_position, + batch_size=batch_size, + ) model_inputs.update( { "position_ids": position_ids, + "cache_position": cache_position, "past_key_values": past_key_values, "use_cache": use_cache, "attention_mask": attention_mask, - "cache_position": cache_position, } ) return model_inputs diff --git a/src/transformers/models/gpt_neo/modeling_gpt_neo.py b/src/transformers/models/gpt_neo/modeling_gpt_neo.py index 31128748c671..8335268e84ac 100755 --- a/src/transformers/models/gpt_neo/modeling_gpt_neo.py +++ b/src/transformers/models/gpt_neo/modeling_gpt_neo.py @@ -744,10 +744,11 @@ def forward( if use_cache and not isinstance(past_key_values, Cache) and not self.training: use_legacy_cache = True past_key_values = DynamicCache.from_legacy_cache(past_key_values) - logger.warning_once( - "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.45. " - "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" - ) + if not self.training: + logger.warning_once( + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.45. " + "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" + ) seq_length = inputs_embeds.shape[1] if cache_position is None: @@ -937,45 +938,26 @@ def prepare_inputs_for_generation( self, input_ids, attention_mask=None, + token_type_ids=None, + position_ids=None, past_key_values=None, inputs_embeds=None, cache_position=None, use_cache=True, **kwargs, ): - token_type_ids = kwargs.get("token_type_ids", None) - past_length = 0 - # Omit tokens covered by past_key_values + # If we have cache: let's slice `input_ids` through `cache_position`, to keep only the unprocessed tokens + # Exception 1: when passing input_embeds, input_ids may be missing entries + # Exception 2: some generation methods do special slicing of input_ids, so we don't need to do it here if past_key_values is not None: - past_length = cache_position[0] if cache_position is not None else past_key_values.get_seq_length() - max_cache_length = ( - torch.tensor(past_key_values.get_max_length(), device=input_ids.device) - if past_key_values.get_max_length() is not None - else None - ) - cache_length = past_length if max_cache_length is None else torch.min(max_cache_length, past_length) - - # Some generation methods already pass only the last input ID - if input_ids.shape[1] > past_length: - remove_prefix_length = past_length - else: - # Default to old behavior: keep only final ID - remove_prefix_length = input_ids.shape[1] - 1 - - input_ids = input_ids[:, remove_prefix_length:] + if inputs_embeds is not None: # Exception 1 + input_ids = input_ids[:, -cache_position.shape[0] :] + elif input_ids.shape[1] != cache_position.shape[0]: # Default case (the "else", a no op, is Exception 2) + input_ids = input_ids[:, cache_position] if token_type_ids is not None: token_type_ids = token_type_ids[:, -input_ids.shape[1] :] - # If we are about to go beyond the maximum cache length, we need to crop the input attention mask. - if ( - max_cache_length is not None - and attention_mask is not None - and cache_length + input_ids.shape[1] > max_cache_length - ): - attention_mask = attention_mask[:, -max_cache_length:] - - position_ids = kwargs.get("position_ids", None) if attention_mask is not None and position_ids is None: # create position_ids on the fly for batch generation position_ids = attention_mask.long().cumsum(-1) - 1 @@ -983,30 +965,47 @@ def prepare_inputs_for_generation( if past_key_values: position_ids = position_ids[:, -input_ids.shape[1] :] + # This `clone` call is needed to avoid recapturing cuda graphs with `torch.compile`'s `mode="reduce-overhead`, as otherwise the input `position_ids` would have various stride during the decoding. Here, simply using `.contiguous()` is not sufficient as in the batch size = 1 case, `position_ids` is already contiguous but with varying stride which retriggers a capture. + position_ids = position_ids.clone(memory_format=torch.contiguous_format) + # if `inputs_embeds` are passed, we only want to use them in the 1st generation step - if inputs_embeds is not None and past_length == 0: + if inputs_embeds is not None and cache_position[0] == 0: model_inputs = {"inputs_embeds": inputs_embeds} else: model_inputs = {"input_ids": input_ids} - if use_cache: - input_length = input_ids.shape[-1] - if cache_position is None: - cache_position = torch.arange(past_length, past_length + input_length, device=input_ids.device) + if isinstance(past_key_values, StaticCache) and attention_mask.ndim == 2: + if inputs_embeds is not None: + batch_size, sequence_length = inputs_embeds.shape + device = inputs_embeds.device else: - cache_position = cache_position[-input_length:] + batch_size, sequence_length = input_ids.shape + device = input_ids.device + + dtype = self.lm_head.weight.dtype + min_dtype = torch.finfo(dtype).min + + attention_mask = _prepare_4d_causal_attention_mask_with_cache_position( + attention_mask, + sequence_length=sequence_length, + target_length=past_key_values.get_max_length(), + dtype=dtype, + device=device, + min_dtype=min_dtype, + cache_position=cache_position, + batch_size=batch_size, + ) model_inputs.update( { + "position_ids": position_ids, + "cache_position": cache_position, "past_key_values": past_key_values, "use_cache": use_cache, - "position_ids": position_ids, - "attention_mask": attention_mask, "token_type_ids": token_type_ids, - "cache_position": cache_position, + "attention_mask": attention_mask, } ) - return model_inputs @add_start_docstrings_to_model_forward(GPT_NEO_INPUTS_DOCSTRING) diff --git a/src/transformers/models/gpt_neox/modeling_gpt_neox.py b/src/transformers/models/gpt_neox/modeling_gpt_neox.py index 06b6c5ef70c9..3e72eec0728f 100755 --- a/src/transformers/models/gpt_neox/modeling_gpt_neox.py +++ b/src/transformers/models/gpt_neox/modeling_gpt_neox.py @@ -922,13 +922,14 @@ def forward( inputs_embeds = self.embed_in(input_ids) use_legacy_cache = False - if use_cache and not isinstance(past_key_values, Cache) and not self.training: + if use_cache and not isinstance(past_key_values, Cache): use_legacy_cache = True past_key_values = DynamicCache.from_legacy_cache(past_key_values) - logger.warning_once( - "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.45. " - "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" - ) + if not self.training: + logger.warning_once( + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.45. " + "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" + ) seq_length = inputs_embeds.shape[1] if cache_position is None: @@ -1186,6 +1187,7 @@ def forward( attentions=outputs.attentions, ) + # can't be copied from llama, gpt-neox has emebd_out and not lm_head def prepare_inputs_for_generation( self, input_ids, diff --git a/src/transformers/models/gptj/modeling_gptj.py b/src/transformers/models/gptj/modeling_gptj.py index 159d139594a3..39b0f1fc268b 100644 --- a/src/transformers/models/gptj/modeling_gptj.py +++ b/src/transformers/models/gptj/modeling_gptj.py @@ -813,13 +813,14 @@ def forward( inputs_embeds = self.wte(input_ids) use_legacy_cache = False - if use_cache and not isinstance(past_key_values, Cache) and not self.training: + if use_cache and not isinstance(past_key_values, Cache): use_legacy_cache = True past_key_values = DynamicCache.from_legacy_cache(past_key_values) - logger.warning_once( - "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.45. " - "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" - ) + if not self.training: + logger.warning_once( + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.45. " + "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" + ) seq_length = inputs_embeds.shape[1] if cache_position is None: @@ -1067,45 +1068,26 @@ def prepare_inputs_for_generation( self, input_ids, attention_mask=None, + token_type_ids=None, + position_ids=None, past_key_values=None, inputs_embeds=None, cache_position=None, use_cache=True, **kwargs, ): - token_type_ids = kwargs.get("token_type_ids", None) - past_length = 0 - # Omit tokens covered by past_key_values + # If we have cache: let's slice `input_ids` through `cache_position`, to keep only the unprocessed tokens + # Exception 1: when passing input_embeds, input_ids may be missing entries + # Exception 2: some generation methods do special slicing of input_ids, so we don't need to do it here if past_key_values is not None: - past_length = cache_position[0] if cache_position is not None else past_key_values.get_seq_length() - max_cache_length = ( - torch.tensor(past_key_values.get_max_length(), device=input_ids.device) - if past_key_values.get_max_length() is not None - else None - ) - cache_length = past_length if max_cache_length is None else torch.min(max_cache_length, past_length) - - # Some generation methods already pass only the last input ID - if input_ids.shape[1] > past_length: - remove_prefix_length = past_length - else: - # Default to old behavior: keep only final ID - remove_prefix_length = input_ids.shape[1] - 1 - - input_ids = input_ids[:, remove_prefix_length:] + if inputs_embeds is not None: # Exception 1 + input_ids = input_ids[:, -cache_position.shape[0] :] + elif input_ids.shape[1] != cache_position.shape[0]: # Default case (the "else", a no op, is Exception 2) + input_ids = input_ids[:, cache_position] if token_type_ids is not None: token_type_ids = token_type_ids[:, -input_ids.shape[1] :] - # If we are about to go beyond the maximum cache length, we need to crop the input attention mask. - if ( - max_cache_length is not None - and attention_mask is not None - and cache_length + input_ids.shape[1] > max_cache_length - ): - attention_mask = attention_mask[:, -max_cache_length:] - - position_ids = kwargs.get("position_ids", None) if attention_mask is not None and position_ids is None: # create position_ids on the fly for batch generation position_ids = attention_mask.long().cumsum(-1) - 1 @@ -1113,30 +1095,47 @@ def prepare_inputs_for_generation( if past_key_values: position_ids = position_ids[:, -input_ids.shape[1] :] + # This `clone` call is needed to avoid recapturing cuda graphs with `torch.compile`'s `mode="reduce-overhead`, as otherwise the input `position_ids` would have various stride during the decoding. Here, simply using `.contiguous()` is not sufficient as in the batch size = 1 case, `position_ids` is already contiguous but with varying stride which retriggers a capture. + position_ids = position_ids.clone(memory_format=torch.contiguous_format) + # if `inputs_embeds` are passed, we only want to use them in the 1st generation step - if inputs_embeds is not None and past_length == 0: + if inputs_embeds is not None and cache_position[0] == 0: model_inputs = {"inputs_embeds": inputs_embeds} else: model_inputs = {"input_ids": input_ids} - if use_cache: - input_length = input_ids.shape[-1] - if cache_position is None: - cache_position = torch.arange(past_length, past_length + input_length, device=input_ids.device) + if isinstance(past_key_values, StaticCache) and attention_mask.ndim == 2: + if inputs_embeds is not None: + batch_size, sequence_length = inputs_embeds.shape + device = inputs_embeds.device else: - cache_position = cache_position[-input_length:] + batch_size, sequence_length = input_ids.shape + device = input_ids.device + + dtype = self.lm_head.weight.dtype + min_dtype = torch.finfo(dtype).min + + attention_mask = _prepare_4d_causal_attention_mask_with_cache_position( + attention_mask, + sequence_length=sequence_length, + target_length=past_key_values.get_max_length(), + dtype=dtype, + device=device, + min_dtype=min_dtype, + cache_position=cache_position, + batch_size=batch_size, + ) model_inputs.update( { + "position_ids": position_ids, + "cache_position": cache_position, "past_key_values": past_key_values, "use_cache": use_cache, - "position_ids": position_ids, - "attention_mask": attention_mask, "token_type_ids": token_type_ids, - "cache_position": cache_position, + "attention_mask": attention_mask, } ) - return model_inputs @add_start_docstrings_to_model_forward(GPTJ_INPUTS_DOCSTRING.format("batch_size, sequence_length")) diff --git a/src/transformers/models/idefics/modeling_idefics.py b/src/transformers/models/idefics/modeling_idefics.py index 9f53c86b3c41..3532219f3d6c 100644 --- a/src/transformers/models/idefics/modeling_idefics.py +++ b/src/transformers/models/idefics/modeling_idefics.py @@ -240,13 +240,12 @@ def expand_inputs_for_generation( def prepare_inputs_for_generation(input_ids, past_key_values=None, **kwargs): token_type_ids = kwargs.get("token_type_ids", None) cache_position = kwargs.get("cache_position", None) - # only last token for inputs_ids if past is defined in kwargs - past_length = 0 - if past_key_values: - past_length = cache_position[0] if cache_position is not None else past_key_values.get_seq_length() - input_ids = input_ids[:, -1].unsqueeze(-1) + # If we have cache: let's slice `input_ids` through `cache_position`, to keep only the unprocessed tokens + if past_key_values is not None: + if input_ids.shape[1] != cache_position.shape[0]: + input_ids = input_ids[:, cache_position] if token_type_ids is not None: - token_type_ids = token_type_ids[:, -1].unsqueeze(-1) + token_type_ids = token_type_ids[:, -input_ids.shape[1] :] attention_mask = kwargs.get("attention_mask", None) position_ids = kwargs.get("position_ids", None) @@ -258,18 +257,15 @@ def prepare_inputs_for_generation(input_ids, past_key_values=None, **kwargs): if past_key_values: position_ids = position_ids[:, -1].unsqueeze(-1) + # This `clone` call is needed to avoid recapturing cuda graphs with `torch.compile`'s `mode="reduce-overhead`, as otherwise the input `position_ids` would have various stride during the decoding. Here, simply using `.contiguous()` is not sufficient as in the batch size = 1 case, `position_ids` is already contiguous but with varying stride which retriggers a capture. + position_ids = position_ids.clone(memory_format=torch.contiguous_format) + pixel_values = kwargs.get("pixel_values", None) image_encoder_embeddings = kwargs.get("image_encoder_embeddings", None) perceiver_embeddings = kwargs.get("perceiver_embeddings", None) image_attention_mask = kwargs.get("image_attention_mask", None) interpolate_pos_encoding = kwargs.get("interpolate_pos_encoding", False) - input_length = position_ids.shape[-1] if position_ids is not None else input_ids.shape[-1] - if cache_position is None: - cache_position = torch.arange(past_length, past_length + input_length, device=input_ids.device) - elif kwargs.get("use_cache", False): - cache_position = cache_position[-input_length:] - return { "input_ids": input_ids, "past_key_values": past_key_values, @@ -1243,11 +1239,12 @@ def forward( inputs_embeds = self.embed_tokens(input_ids) return_legacy_cache = False - if use_cache and not isinstance(past_key_values, Cache) and not self.training: - logger.warning_once( - "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.45. " - "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" - ) + if use_cache and not isinstance(past_key_values, Cache): + if not self.training: + logger.warning_once( + "We detected that you are passing `past_key_values` as a tuple and this is deprecated and will be removed in v4.45. " + "Please use an appropriate `Cache` class (https://huggingface.co/docs/transformers/v4.41.3/en/internal/generation_utils#transformers.Cache)" + ) return_legacy_cache = True past_key_values = DynamicCache.from_legacy_cache(past_key_values) From 21b45c5da60e6b777bc4037202c1363aa303d687 Mon Sep 17 00:00:00 2001 From: raushan Date: Tue, 6 Aug 2024 09:54:29 +0200 Subject: [PATCH 40/40] one more falcon fix for alibi --- src/transformers/models/falcon/modeling_falcon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformers/models/falcon/modeling_falcon.py b/src/transformers/models/falcon/modeling_falcon.py index 6ea7147366a0..5ddc1fba9ef9 100644 --- a/src/transformers/models/falcon/modeling_falcon.py +++ b/src/transformers/models/falcon/modeling_falcon.py @@ -1178,7 +1178,7 @@ def _update_causal_mask( target_length = ( attention_mask.shape[-1] if isinstance(attention_mask, torch.Tensor) - else past_seen_tokens + sequence_length + 1 + else past_seen_tokens + sequence_length ) # In case the provided `attention` mask is 2D, we generate a causal mask here (4D).