From 35434da084655e6e00546e9c8bbfac7f2d10c6dd Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Mon, 18 Nov 2024 17:37:37 +0100 Subject: [PATCH 01/39] config draft --- src/transformers/models/moonshine/__init__.py | 50 ++++ .../models/moonshine/modular_moonshine.py | 246 ++++++++++++++++++ 2 files changed, 296 insertions(+) create mode 100644 src/transformers/models/moonshine/__init__.py create mode 100644 src/transformers/models/moonshine/modular_moonshine.py diff --git a/src/transformers/models/moonshine/__init__.py b/src/transformers/models/moonshine/__init__.py new file mode 100644 index 00000000000000..d1c88ad1b9cb72 --- /dev/null +++ b/src/transformers/models/moonshine/__init__.py @@ -0,0 +1,50 @@ +# Copyright 2024 The HuggingFace Team. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from typing import TYPE_CHECKING + +from ...utils import ( + OptionalDependencyNotAvailable, + _LazyModule, + is_torch_available, +) + + +_import_structure = { + "configuration_moonshine": ["MoonshineConfig"], +} + +try: + if not is_torch_available(): + raise OptionalDependencyNotAvailable() +except OptionalDependencyNotAvailable: + pass +else: + _import_structure["modeling_gemma2"] = [ + ] + +if TYPE_CHECKING: + from .configuration_moonshine import MoonshineConfig + + try: + if not is_torch_available(): + raise OptionalDependencyNotAvailable() + except OptionalDependencyNotAvailable: + pass + else: + pass + +else: + import sys + + sys.modules[__name__] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__) diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py new file mode 100644 index 00000000000000..7e339bbb648ccc --- /dev/null +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -0,0 +1,246 @@ +from ...configuration_utils import PretrainedConfig + +class MoonshineConfig(PretrainedConfig): + r""" + This is the configuration class to store the configuration of a [`MoonshineModel`]. It is used to instantiate a + Moonshine model according to the specified arguments, defining the model architecture. Instantiating a configuration + with the defaults will yield a similar configuration to that of the Moonshine + [UsefulSensors/moonshine](https://huggingface.co/UsefulSensors/moonshine) architecture. + + Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the + documentation from [`PretrainedConfig`] for more information. + + + Args: + vocab_size (`int`, *optional*, defaults to 32768): + Vocabulary size of the Moonshine model. Defines the number of different tokens that can be represented by the + `decoder_input_ids` passed when calling [`MoonshineModel`] + encoder_layers (`int`, *optional*, defaults to 4): + Number of encoder layers. + decoder_layers (`int`, *optional*, defaults to 4): + Number of decoder layers. + encoder_attention_heads (`int`, *optional*, defaults to 6): + Number of attention heads for each attention layer in the Transformer encoder. + decoder_attention_heads (`int`, *optional*, defaults to 6): + Number of attention heads for each attention layer in the Transformer decoder. + encoder_ffn_dim (`int`, *optional*, defaults to 1536): + Dimensionality of the "intermediate" (often named feed-forward) layer in encoder. + decoder_ffn_dim (`int`, *optional*, defaults to 1536): + Dimensionality of the "intermediate" (often named feed-forward) layer in decoder. + encoder_layerdrop (`float`, *optional*, defaults to 0.0): + The LayerDrop probability for the encoder. See the [LayerDrop paper](see https://arxiv.org/abs/1909.11556) + for more details. + decoder_layerdrop (`float`, *optional*, defaults to 0.0): + The LayerDrop probability for the decoder. See the [LayerDrop paper](see https://arxiv.org/abs/1909.11556) + for more details. + decoder_start_token_id (`int`, *optional*, defaults to 50257): + Corresponds to the "<|startoftranscript|>" token, which is automatically used when no `decoder_input_ids` + are provided to the `generate` function. It is used to guide the model`s generation process depending on + the task. + use_cache (`bool`, *optional*, defaults to `True`): + Whether or not the model should return the last key/values attentions (not used by all models). + is_encoder_decoder (`bool`, *optional*, defaults to `True`): + Whether the model is used as an encoder/decoder or not. + activation_function (`str`, *optional*, defaults to `"gelu"`): + The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`, + `"relu"`, `"silu"` and `"gelu_new"` are supported. + d_model (`int`, *optional*, defaults to 384): + Dimensionality of the layers. + dropout (`float`, *optional*, defaults to 0.1): + The dropout probability for all fully connected layers in the embeddings, encoder, and pooler. + attention_dropout (`float`, *optional*, defaults to 0.0): + The dropout ratio for the attention probabilities. + activation_dropout (`float`, *optional*, defaults to 0.0): + The dropout ratio for activations inside the fully connected layer. + init_std (`float`, *optional*, defaults to 0.02): + The standard deviation of the truncated_normal_initializer for initializing all weight matrices. + scale_embedding (`bool`, *optional*, defaults to False): + Scale embeddings by diving by sqrt(d_model). + max_source_positions (`int`, *optional*, defaults to 1500): + The maximum sequence length of log-mel filter-bank features that this model might ever be used with. + max_target_positions (`int`, *optional*, defaults to 448): + The maximum sequence length that this model might ever be used with. Typically set this to something large + just in case (e.g., 512 or 1024 or 2048). + pad_token_id (`int`, *optional*, defaults to 50256): + Padding token id. + bos_token_id (`int`, *optional*, defaults to 50256): + Begin of stream token id. + eos_token_id (`int`, *optional*, defaults to 50256): + End of stream token id. + suppress_tokens (`List[int]`, *optional*): + A list containing the non-speech tokens that will be used by the logit processor in the `generate` + function. NON_SPEECH_TOKENS and NON_SPEECH_TOKENS_MULTI each correspond to the `english-only` and the + `multilingual` model. + begin_suppress_tokens (`List[int]`, *optional*, defaults to `[220,50256]`): + A list containing tokens that will be supressed at the beginning of the sampling process. Initialized as + the token for `" "` (`blank_token_id`) and the `eos_token_id` + use_weighted_layer_sum (`bool`, *optional*, defaults to `False`): + Whether to use a weighted average of layer outputs with learned weights. Only relevant when using an + instance of [`MoonshineForAudioClassification`]. + classifier_proj_size (`int`, *optional*, defaults to 256): + Dimensionality of the projection before token mean-pooling for classification. Only relevant when using an + instance of [`MoonshineForAudioClassification`]. + apply_spec_augment (`bool`, *optional*, defaults to `False`): + Whether to apply *SpecAugment* data augmentation to the outputs of the feature encoder. For reference see + [SpecAugment: A Simple Data Augmentation Method for Automatic Speech + Recognition](https://arxiv.org/abs/1904.08779). + mask_time_prob (`float`, *optional*, defaults to 0.05): + Percentage (between 0 and 1) of all feature vectors along the time axis which will be masked. The masking + procecure generates `mask_time_prob*len(time_axis)/mask_time_length` independent masks over the axis. If + reasoning from the propability of each feature vector to be chosen as the start of the vector span to be + masked, *mask_time_prob* should be `prob_vector_start*mask_time_length`. Note that overlap may decrease the + actual percentage of masked vectors. This is only relevant if `apply_spec_augment == True`. + mask_time_length (`int`, *optional*, defaults to 10): + Length of vector span along the time axis. + mask_time_min_masks (`int`, *optional*, defaults to 2),: + The minimum number of masks of length `mask_feature_length` generated along the time axis, each time step, + irrespectively of `mask_feature_prob`. Only relevant if ''mask_time_prob*len(time_axis)/mask_time_length < + mask_time_min_masks'' + mask_feature_prob (`float`, *optional*, defaults to 0.0): + Percentage (between 0 and 1) of all feature vectors along the feature axis which will be masked. The + masking procecure generates `mask_feature_prob*len(feature_axis)/mask_time_length` independent masks over + the axis. If reasoning from the propability of each feature vector to be chosen as the start of the vector + span to be masked, *mask_feature_prob* should be `prob_vector_start*mask_feature_length`. Note that overlap + may decrease the actual percentage of masked vectors. This is only relevant if `apply_spec_augment is + True`. + mask_feature_length (`int`, *optional*, defaults to 10): + Length of vector span along the feature axis. + mask_feature_min_masks (`int`, *optional*, defaults to 0),: + The minimum number of masks of length `mask_feature_length` generated along the feature axis, each time + step, irrespectively of `mask_feature_prob`. Only relevant if + `mask_feature_prob*len(feature_axis)/mask_feature_length < mask_feature_min_masks`. + median_filter_width (`int`, *optional*, defaults to 7): + Width of the median filter used to smoothen to cross-attention outputs when computing token timestamps. + Should be an odd number. + + Example: + + ```python + >>> from transformers import MoonshineConfig, MoonshineModel + + >>> # Initializing a Moonshine tiny style configuration + >>> configuration = MoonshineConfig() + + >>> # Initializing a model (with random weights) from the tiny style configuration + >>> model = MoonshineModel(configuration) + + >>> # Accessing the model configuration + >>> configuration = model.config + ```""" + + model_type = "moonshine" + keys_to_ignore_at_inference = ["past_key_values"] + attribute_map = { + "num_key_value_heads": "encoder_attention_heads", + "num_attention_heads": "encoder_attention_heads", + "hidden_size": "d_model", + } + + def __init__( + self, + vocab_size=32768, + encoder_layers=6, + encoder_attention_heads=8, + decoder_layers=6, + decoder_attention_heads=8, + decoder_ffn_dim=1152, + encoder_ffn_dim=1152, + encoder_layerdrop=0.0, + decoder_layerdrop=0.0, + decoder_start_token_id=50257, + use_cache=True, + is_encoder_decoder=True, + activation_function="gelu", + d_model=288, + dropout=0.0, + attention_dropout=0.0, + activation_dropout=0.0, + init_std=0.02, + scale_embedding=False, + max_source_positions=1500, + max_target_positions=448, + pad_token_id=50256, + bos_token_id=50256, + eos_token_id=50256, + suppress_tokens=None, + begin_suppress_tokens=[220, 50256], + use_weighted_layer_sum=False, + classifier_proj_size=256, + apply_spec_augment=False, + mask_time_prob=0.05, + mask_time_length=10, + mask_time_min_masks=2, + mask_feature_prob=0.0, + mask_feature_length=10, + mask_feature_min_masks=0, + median_filter_width=7, + **kwargs, + ): + super().__init__( + pad_token_id=pad_token_id, + bos_token_id=bos_token_id, + eos_token_id=eos_token_id, + is_encoder_decoder=is_encoder_decoder, + decoder_start_token_id=decoder_start_token_id, + suppress_tokens=suppress_tokens, + begin_suppress_tokens=begin_suppress_tokens, + **kwargs, + ) + + self.vocab_size = vocab_size + self.d_model = d_model + self.encoder_layers = encoder_layers + self.encoder_attention_heads = encoder_attention_heads + self.decoder_layers = decoder_layers + self.decoder_attention_heads = decoder_attention_heads + self.decoder_ffn_dim = decoder_ffn_dim + self.encoder_ffn_dim = encoder_ffn_dim + self.dropout = dropout + self.attention_dropout = attention_dropout + self.activation_dropout = activation_dropout + self.activation_function = activation_function + self.init_std = init_std + self.encoder_layerdrop = encoder_layerdrop + self.decoder_layerdrop = decoder_layerdrop + self.use_cache = use_cache + self.num_hidden_layers = encoder_layers + self.scale_embedding = scale_embedding # scale factor will be sqrt(d_model) if True + self.max_source_positions = max_source_positions + self.max_target_positions = max_target_positions + + # Audio Classification-specific parameters. Feel free to ignore for other classes. + self.classifier_proj_size = classifier_proj_size + self.use_weighted_layer_sum = use_weighted_layer_sum + + # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 + self.apply_spec_augment = apply_spec_augment + self.mask_time_prob = mask_time_prob + self.mask_time_length = mask_time_length + self.mask_time_min_masks = mask_time_min_masks + self.mask_feature_prob = mask_feature_prob + self.mask_feature_length = mask_feature_length + self.mask_feature_min_masks = mask_feature_min_masks + + # draft + self.median_filter_width = median_filter_width + self.head_dim = self.d_model // self.encoder_attention_heads + self.max_position_embeddings = 2048 + self.rope_theta = 10000.0 + self.query_pre_attn_scalar = self.head_dim + self.attention_bias = True + self.sliding_window = 4096 + self.final_logit_softcapping = 30.0 + self.attn_logit_softcapping = None + self.final_logit_softcapping_type = None + + +class MoonshineAttention(Gemma2Attention): + pass + + +class MoonshineFlashAttention2(Gemma2FlashAttention2): + pass + + +class MoonshineSdpaAttention(Gemma2SdpaAttention): + pass From 7e180381e079bc8a5de84860f6cd69cc42f3d923 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Mon, 2 Dec 2024 19:00:19 +0100 Subject: [PATCH 02/39] full encoder forward --- .../models/moonshine/modular_moonshine.py | 705 +++++++++++++----- 1 file changed, 508 insertions(+), 197 deletions(-) diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index 7e339bbb648ccc..17d2df243f6258 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -1,127 +1,141 @@ from ...configuration_utils import PretrainedConfig +from ..phi.modeling_phi import PhiAttention, PhiFlashAttention2, PhiSdpaAttention, PhiMLP, PhiRotaryEmbedding +from ..llama.modeling_llama import LlamaDecoderLayer +from ..mistral.modeling_mistral import MistralMLP +from ..whisper.modeling_whisper import WhisperEncoder +from ...cache_utils import Cache, DynamicCache, StaticCache + +from ...modeling_outputs import ( + BaseModelOutput, + BaseModelOutputWithPastAndCrossAttentions, + CausalLMOutputWithCrossAttentions, + Seq2SeqLMOutput, + Seq2SeqModelOutput, + SequenceClassifierOutput, +) +from ...modeling_utils import PreTrainedModel + +import torch.nn as nn +import torch + +from ...utils import ( + add_start_docstrings, + add_start_docstrings_to_model_forward, + is_flash_attn_2_available, + is_flash_attn_greater_or_equal_2_10, + logging, + replace_return_docstrings, +) + +from typing import Optional, Tuple + +from ...activations import ACT2FN + +import copy class MoonshineConfig(PretrainedConfig): r""" - This is the configuration class to store the configuration of a [`MoonshineModel`]. It is used to instantiate a - Moonshine model according to the specified arguments, defining the model architecture. Instantiating a configuration - with the defaults will yield a similar configuration to that of the Moonshine - [UsefulSensors/moonshine](https://huggingface.co/UsefulSensors/moonshine) architecture. + This is the configuration class to store the configuration of a [`MoonshineModel`]. It is used to instantiate a Moonshine + model according to the specified arguments, defining the model architecture. Instantiating a configuration with the + defaults will yield a similar configuration to that of the Moonshine + [UsefulSensors/moonshine](https://huggingface.co/UsefulSensors/moonshine). Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information. - Args: vocab_size (`int`, *optional*, defaults to 32768): Vocabulary size of the Moonshine model. Defines the number of different tokens that can be represented by the - `decoder_input_ids` passed when calling [`MoonshineModel`] - encoder_layers (`int`, *optional*, defaults to 4): - Number of encoder layers. - decoder_layers (`int`, *optional*, defaults to 4): - Number of decoder layers. - encoder_attention_heads (`int`, *optional*, defaults to 6): - Number of attention heads for each attention layer in the Transformer encoder. - decoder_attention_heads (`int`, *optional*, defaults to 6): - Number of attention heads for each attention layer in the Transformer decoder. - encoder_ffn_dim (`int`, *optional*, defaults to 1536): - Dimensionality of the "intermediate" (often named feed-forward) layer in encoder. - decoder_ffn_dim (`int`, *optional*, defaults to 1536): - Dimensionality of the "intermediate" (often named feed-forward) layer in decoder. - encoder_layerdrop (`float`, *optional*, defaults to 0.0): - The LayerDrop probability for the encoder. See the [LayerDrop paper](see https://arxiv.org/abs/1909.11556) - for more details. - decoder_layerdrop (`float`, *optional*, defaults to 0.0): - The LayerDrop probability for the decoder. See the [LayerDrop paper](see https://arxiv.org/abs/1909.11556) - for more details. - decoder_start_token_id (`int`, *optional*, defaults to 50257): - Corresponds to the "<|startoftranscript|>" token, which is automatically used when no `decoder_input_ids` - are provided to the `generate` function. It is used to guide the model`s generation process depending on - the task. + `inputs_ids` passed when calling [`MoonshineModel`]. + hidden_size (`int`, *optional*, defaults to 288): + Dimension of the hidden representations. + intermediate_size (`int`, *optional*): + Dimension of the MLP representations. + num_hidden_layers (`int`, *optional*, defaults to 6): + Number of hidden layers in the Transformer encoder and decoder. + num_attention_heads (`int`, *optional*, defaults to 8): + Number of attention heads for each attention layer in the Transformer encoder and decoder. + num_key_value_heads (`int`, *optional*): + This is the number of key_value heads that should be used to implement Grouped Query Attention. If + `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if + `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When + converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed + by meanpooling all the original heads within that group. For more details checkout [this + paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to + `num_attention_heads`. + encoder_hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`): + The non-linear activation function (function or string) in the encoder. + decoder_hidden_act (`str` or `function`, *optional*, defaults to `"silu"`): + The non-linear activation function (function or string) in the decoder. + max_position_embeddings (`int`, *optional*, defaults to 2048): + The maximum sequence length that this model might ever be used with. TODO: check this + layer_norm_eps (`float`, *optional*, defaults to 1e-5): + The epsilon used by the layer normalization layers. use_cache (`bool`, *optional*, defaults to `True`): Whether or not the model should return the last key/values attentions (not used by all models). - is_encoder_decoder (`bool`, *optional*, defaults to `True`): - Whether the model is used as an encoder/decoder or not. - activation_function (`str`, *optional*, defaults to `"gelu"`): - The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`, - `"relu"`, `"silu"` and `"gelu_new"` are supported. - d_model (`int`, *optional*, defaults to 384): - Dimensionality of the layers. - dropout (`float`, *optional*, defaults to 0.1): - The dropout probability for all fully connected layers in the embeddings, encoder, and pooler. + rope_theta (`float`, *optional*, defaults to 10000.0): + The base period of the RoPE embeddings. TODO: check this + partial_rotary_factor (`float`, *optional*, defaults to 0.5): + Percentage of the query and keys which will have rotary embedding. TODO: check this + ff_mult (`int`, *optional*, defaults to 4): + Factor by which to scale the intermediate size. + attention_bias (`bool`, *optional*, defaults to `False`): + Whether to use a bias in the query, key, value and output projection layers during self-attention. attention_dropout (`float`, *optional*, defaults to 0.0): The dropout ratio for the attention probabilities. - activation_dropout (`float`, *optional*, defaults to 0.0): - The dropout ratio for activations inside the fully connected layer. - init_std (`float`, *optional*, defaults to 0.02): - The standard deviation of the truncated_normal_initializer for initializing all weight matrices. - scale_embedding (`bool`, *optional*, defaults to False): - Scale embeddings by diving by sqrt(d_model). - max_source_positions (`int`, *optional*, defaults to 1500): - The maximum sequence length of log-mel filter-bank features that this model might ever be used with. - max_target_positions (`int`, *optional*, defaults to 448): - The maximum sequence length that this model might ever be used with. Typically set this to something large - just in case (e.g., 512 or 1024 or 2048). - pad_token_id (`int`, *optional*, defaults to 50256): - Padding token id. - bos_token_id (`int`, *optional*, defaults to 50256): - Begin of stream token id. - eos_token_id (`int`, *optional*, defaults to 50256): - End of stream token id. - suppress_tokens (`List[int]`, *optional*): - A list containing the non-speech tokens that will be used by the logit processor in the `generate` - function. NON_SPEECH_TOKENS and NON_SPEECH_TOKENS_MULTI each correspond to the `english-only` and the - `multilingual` model. - begin_suppress_tokens (`List[int]`, *optional*, defaults to `[220,50256]`): - A list containing tokens that will be supressed at the beginning of the sampling process. Initialized as - the token for `" "` (`blank_token_id`) and the `eos_token_id` - use_weighted_layer_sum (`bool`, *optional*, defaults to `False`): - Whether to use a weighted average of layer outputs with learned weights. Only relevant when using an - instance of [`MoonshineForAudioClassification`]. - classifier_proj_size (`int`, *optional*, defaults to 256): - Dimensionality of the projection before token mean-pooling for classification. Only relevant when using an - instance of [`MoonshineForAudioClassification`]. - apply_spec_augment (`bool`, *optional*, defaults to `False`): - Whether to apply *SpecAugment* data augmentation to the outputs of the feature encoder. For reference see - [SpecAugment: A Simple Data Augmentation Method for Automatic Speech - Recognition](https://arxiv.org/abs/1904.08779). - mask_time_prob (`float`, *optional*, defaults to 0.05): - Percentage (between 0 and 1) of all feature vectors along the time axis which will be masked. The masking - procecure generates `mask_time_prob*len(time_axis)/mask_time_length` independent masks over the axis. If - reasoning from the propability of each feature vector to be chosen as the start of the vector span to be - masked, *mask_time_prob* should be `prob_vector_start*mask_time_length`. Note that overlap may decrease the - actual percentage of masked vectors. This is only relevant if `apply_spec_augment == True`. - mask_time_length (`int`, *optional*, defaults to 10): - Length of vector span along the time axis. - mask_time_min_masks (`int`, *optional*, defaults to 2),: - The minimum number of masks of length `mask_feature_length` generated along the time axis, each time step, - irrespectively of `mask_feature_prob`. Only relevant if ''mask_time_prob*len(time_axis)/mask_time_length < - mask_time_min_masks'' - mask_feature_prob (`float`, *optional*, defaults to 0.0): - Percentage (between 0 and 1) of all feature vectors along the feature axis which will be masked. The - masking procecure generates `mask_feature_prob*len(feature_axis)/mask_time_length` independent masks over - the axis. If reasoning from the propability of each feature vector to be chosen as the start of the vector - span to be masked, *mask_feature_prob* should be `prob_vector_start*mask_feature_length`. Note that overlap - may decrease the actual percentage of masked vectors. This is only relevant if `apply_spec_augment is - True`. - mask_feature_length (`int`, *optional*, defaults to 10): - Length of vector span along the feature axis. - mask_feature_min_masks (`int`, *optional*, defaults to 0),: - The minimum number of masks of length `mask_feature_length` generated along the feature axis, each time - step, irrespectively of `mask_feature_prob`. Only relevant if - `mask_feature_prob*len(feature_axis)/mask_feature_length < mask_feature_min_masks`. - median_filter_width (`int`, *optional*, defaults to 7): - Width of the median filter used to smoothen to cross-attention outputs when computing token timestamps. - Should be an odd number. + qk_layernorm (`bool`, *optional*, defaults to `False`): + Whether or not to normalize the Queries and Keys after projecting the hidden states. + rope_scaling (`Dict`, *optional*): + Dictionary containing the scaling configuration for the RoPE embeddings. NOTE: if you apply new rope type + and you expect the model to work on longer `max_position_embeddings`, we recommend you to update this value + accordingly. + Expected contents: + `rope_type` (`str`): + The sub-variant of RoPE to use. Can be one of ['default', 'linear', 'dynamic', 'yarn', 'longrope', + 'llama3'], with 'default' being the original RoPE implementation. + `factor` (`float`, *optional*): + Used with all rope types except 'default'. The scaling factor to apply to the RoPE embeddings. In + most scaling types, a `factor` of x will enable the model to handle sequences of length x * + original maximum pre-trained length. + `original_max_position_embeddings` (`int`, *optional*): + Used with 'dynamic', 'longrope' and 'llama3'. The original max position embeddings used during + pretraining. + `attention_factor` (`float`, *optional*): + Used with 'yarn' and 'longrope'. The scaling factor to be applied on the attention + computation. If unspecified, it defaults to value recommended by the implementation, using the + `factor` field to infer the suggested value. + `beta_fast` (`float`, *optional*): + Only used with 'yarn'. Parameter to set the boundary for extrapolation (only) in the linear + ramp function. If unspecified, it defaults to 32. + `beta_slow` (`float`, *optional*): + Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear + ramp function. If unspecified, it defaults to 1. + `short_factor` (`List[float]`, *optional*): + Only used with 'longrope'. The scaling factor to be applied to short contexts (< + `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden + size divided by the number of attention heads divided by 2 + `long_factor` (`List[float]`, *optional*): + Only used with 'longrope'. The scaling factor to be applied to long contexts (< + `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden + size divided by the number of attention heads divided by 2 + `low_freq_factor` (`float`, *optional*): + Only used with 'llama3'. Scaling factor applied to low frequency components of the RoPE + `high_freq_factor` (`float`, *optional*): + Only used with 'llama3'. Scaling factor applied to high frequency components of the RoPE + bos_token_id (`int`, *optional*, defaults to 1): + Denotes beginning of sequences token id. + eos_token_id (`int`, *optional*, defaults to 2): + Denotes end of sequences token id. Example: ```python - >>> from transformers import MoonshineConfig, MoonshineModel + >>> from transformers import MoonshineModel, MoonshineConfig - >>> # Initializing a Moonshine tiny style configuration - >>> configuration = MoonshineConfig() + >>> # Initializing a Moonshine style configuration + >>> configuration = MoonshineConfig().from_pretrained("UsefulSensors/moonshine") - >>> # Initializing a model (with random weights) from the tiny style configuration + >>> # Initializing a model from the configuration >>> model = MoonshineModel(configuration) >>> # Accessing the model configuration @@ -130,117 +144,414 @@ class MoonshineConfig(PretrainedConfig): model_type = "moonshine" keys_to_ignore_at_inference = ["past_key_values"] - attribute_map = { - "num_key_value_heads": "encoder_attention_heads", - "num_attention_heads": "encoder_attention_heads", - "hidden_size": "d_model", - } def __init__( self, vocab_size=32768, - encoder_layers=6, - encoder_attention_heads=8, - decoder_layers=6, - decoder_attention_heads=8, - decoder_ffn_dim=1152, - encoder_ffn_dim=1152, - encoder_layerdrop=0.0, - decoder_layerdrop=0.0, - decoder_start_token_id=50257, + hidden_size=288, + intermediate_size=None, + num_hidden_layers=6, + num_attention_heads=8, + num_key_value_heads=None, + encoder_hidden_act="gelu", + decoder_hidden_act="silu", + max_position_embeddings=2048, + layer_norm_eps=1e-5, use_cache=True, - is_encoder_decoder=True, - activation_function="gelu", - d_model=288, - dropout=0.0, + rope_theta=10000.0, + partial_rotary_factor=0.5, + attention_bias=False, attention_dropout=0.0, - activation_dropout=0.0, - init_std=0.02, - scale_embedding=False, - max_source_positions=1500, - max_target_positions=448, - pad_token_id=50256, - bos_token_id=50256, - eos_token_id=50256, - suppress_tokens=None, - begin_suppress_tokens=[220, 50256], - use_weighted_layer_sum=False, - classifier_proj_size=256, - apply_spec_augment=False, - mask_time_prob=0.05, - mask_time_length=10, - mask_time_min_masks=2, - mask_feature_prob=0.0, - mask_feature_length=10, - mask_feature_min_masks=0, - median_filter_width=7, + qk_layernorm=False, + rope_scaling=None, + ff_mult=4, + bos_token_id=1, + eos_token_id=2, **kwargs, ): + self.vocab_size = vocab_size + self.hidden_size = hidden_size + self.intermediate_size = intermediate_size + self.num_hidden_layers = num_hidden_layers + self.num_attention_heads = num_attention_heads + + if num_key_value_heads is None: + num_key_value_heads = num_attention_heads + + self.num_key_value_heads = num_key_value_heads + self.encoder_hidden_act = encoder_hidden_act + self.decoder_hidden_act = decoder_hidden_act + self.max_position_embeddings = max_position_embeddings + self.layer_norm_eps = layer_norm_eps + self.use_cache = use_cache + self.rope_theta = rope_theta + self.partial_rotary_factor = partial_rotary_factor + + self.attention_bias = attention_bias + self.attention_dropout = attention_dropout + self.qk_layernorm = qk_layernorm + self.rope_scaling = rope_scaling + self.ff_mult = ff_mult + super().__init__( - pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, - is_encoder_decoder=is_encoder_decoder, - decoder_start_token_id=decoder_start_token_id, - suppress_tokens=suppress_tokens, - begin_suppress_tokens=begin_suppress_tokens, **kwargs, ) - self.vocab_size = vocab_size - self.d_model = d_model - self.encoder_layers = encoder_layers - self.encoder_attention_heads = encoder_attention_heads - self.decoder_layers = decoder_layers - self.decoder_attention_heads = decoder_attention_heads - self.decoder_ffn_dim = decoder_ffn_dim - self.encoder_ffn_dim = encoder_ffn_dim - self.dropout = dropout - self.attention_dropout = attention_dropout - self.activation_dropout = activation_dropout - self.activation_function = activation_function - self.init_std = init_std - self.encoder_layerdrop = encoder_layerdrop - self.decoder_layerdrop = decoder_layerdrop - self.use_cache = use_cache - self.num_hidden_layers = encoder_layers - self.scale_embedding = scale_embedding # scale factor will be sqrt(d_model) if True - self.max_source_positions = max_source_positions - self.max_target_positions = max_target_positions - - # Audio Classification-specific parameters. Feel free to ignore for other classes. - self.classifier_proj_size = classifier_proj_size - self.use_weighted_layer_sum = use_weighted_layer_sum - - # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 - self.apply_spec_augment = apply_spec_augment - self.mask_time_prob = mask_time_prob - self.mask_time_length = mask_time_length - self.mask_time_min_masks = mask_time_min_masks - self.mask_feature_prob = mask_feature_prob - self.mask_feature_length = mask_feature_length - self.mask_feature_min_masks = mask_feature_min_masks - - # draft - self.median_filter_width = median_filter_width - self.head_dim = self.d_model // self.encoder_attention_heads - self.max_position_embeddings = 2048 - self.rope_theta = 10000.0 - self.query_pre_attn_scalar = self.head_dim - self.attention_bias = True - self.sliding_window = 4096 - self.final_logit_softcapping = 30.0 - self.attn_logit_softcapping = None - self.final_logit_softcapping_type = None - - -class MoonshineAttention(Gemma2Attention): - pass +def rotate_every_two(x: torch.Tensor) -> torch.Tensor: + x1 = x[:, :, :, ::2] + x2 = x[:, :, :, 1::2] + x = torch.stack((-x2, x1), dim=-1) + return x.flatten(-2) # in einsum notation: rearrange(x, '... d j -> ... (d j)') + + +def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1): + """Applies Rotary Position Embedding to the query and key tensors. + + Args: + q (`torch.Tensor`): The query tensor. + k (`torch.Tensor`): The key tensor. + cos (`torch.Tensor`): The cosine part of the rotary embedding. + sin (`torch.Tensor`): The sine part of the rotary embedding. + position_ids (`torch.Tensor`, *optional*): + Deprecated and unused. + unsqueeze_dim (`int`, *optional*, defaults to 1): + The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and + sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note + that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and + k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes + cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have + the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2. + Returns: + `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding. + """ + cos = cos.unsqueeze(unsqueeze_dim) + sin = sin.unsqueeze(unsqueeze_dim) + + q_embed = (q * cos) + (rotate_every_two(q) * sin) + k_embed = (k * cos) + (rotate_every_two(k) * sin) + return q_embed, k_embed + + +class MoonshineRotaryEmbedding(PhiRotaryEmbedding): + @torch.no_grad() + def forward(self, x, position_ids): + if "dynamic" in self.rope_type: + self._dynamic_frequency_update(position_ids, device=x.device) + + # Core RoPE block + inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1) + position_ids_expanded = position_ids[:, None, :].float() + # Force float32 (see https://github.com/huggingface/transformers/pull/29285) + device_type = x.device.type + device_type = device_type if isinstance(device_type, str) and device_type != "mps" else "cpu" + with torch.autocast(device_type=device_type, enabled=False): + freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2) + emb = torch.stack((freqs, freqs), dim=-1) + emb = emb.flatten(-2) # in einsum notation: rearrange(x, '... d j -> ... (d j)') + cos = emb.cos() + sin = emb.sin() + + # Advanced RoPE types (e.g. yarn) apply a post-processing scaling factor, equivalent to scaling attention + cos = cos * self.attention_scaling + sin = sin * self.attention_scaling + + return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype) + + +class MoonshineNonGatedMLP(PhiMLP): + def __init__(self, config: MoonshineConfig, hidden_act: str): + config = copy.deepcopy(config) + config.hidden_act = hidden_act + if config.intermediate_size is None: + config.intermediate_size = config.hidden_size * config.ff_mult + super().__init__(config) + + +class MoonshineGatedMLP(MistralMLP): + def __init__(self, config: MoonshineConfig, hidden_act: str): + config = copy.deepcopy(config) + config.hidden_act = hidden_act + if config.intermediate_size is None: + config.intermediate_size = config.hidden_size * config.ff_mult * 2 + super().__init__(config) + self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=True) + self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False) + self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=True) + -class MoonshineFlashAttention2(Gemma2FlashAttention2): +class MoonshineMLP: + def __new__(cls, config: MoonshineConfig, hidden_act: str): + if hidden_act == "gelu": + return MoonshineNonGatedMLP(config, hidden_act) + elif hidden_act == "silu": + return MoonshineGatedMLP(config, hidden_act) + else: + raise ValueError(f"Unsupported activation function: {hidden_act}, please use 'gelu' or 'silu'") + + +class MoonshineAttention(PhiAttention): + def __init__(self, config: MoonshineConfig, layer_idx: Optional[int] = None): + super().__init__(config, layer_idx) + self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=config.attention_bias) + self.k_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config.attention_bias) + self.v_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config.attention_bias) + self.dense = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=config.attention_bias) + self.rotary_ndims = max(config.hidden_size // config.num_attention_heads // 2, 32) + + self.rotary_emb = MoonshineRotaryEmbedding( + dim=self.rotary_ndims, + max_position_embeddings=config.max_position_embeddings, + ) + + +class MoonshineFlashAttention2(PhiFlashAttention2): pass -class MoonshineSdpaAttention(Gemma2SdpaAttention): +class MoonshineSdpaAttention(PhiSdpaAttention): pass + + +MOONSHINE_ATTENTION_CLASSES = { + "eager": MoonshineAttention, + "flash_attention_2": MoonshineFlashAttention2, + "sdpa": MoonshineSdpaAttention, +} + + +class MoonshineEncoderLayer(LlamaDecoderLayer): + def __init__(self, config: MoonshineConfig, layer_idx: int): + super().__init__(config, layer_idx) + + self.mlp = MoonshineMLP(config, config.encoder_hidden_act) + self.input_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) + self.post_attention_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) + + +class MoonshineDecoderLayer(nn.Module): + def __init__(self, config: MoonshineConfig, layer_idx: int = None): + super().__init__() + self.hidden_size = config.hidden_size + + self.self_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation](config=config, layer_idx=layer_idx) + self.encoder_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation](config=config, layer_idx=layer_idx) + + self.mlp = MoonshineMLP(config, config.decoder_hidden_act) + self.input_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) + self.post_attention_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) + self.final_layer_norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) + + def forward( + self, + hidden_states: torch.Tensor, + attention_mask: Optional[torch.Tensor] = None, + encoder_hidden_states: Optional[torch.Tensor] = None, + encoder_attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + past_key_value: Optional[Cache] = None, + output_attentions: Optional[bool] = False, + use_cache: Optional[bool] = False, + cache_position: Optional[torch.LongTensor] = None, + position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 + **kwargs, + ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + """ + Args: + hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` + attention_mask (`torch.FloatTensor`, *optional*): + attention mask of size `(batch_size, sequence_length)` if flash attention is used or `(batch_size, 1, + query_sequence_length, key_sequence_length)` if default attention is used. + encoder_hidden_states (`torch.FloatTensor`): + cross attention input to the layer of shape `(batch, seq_len, embed_dim)` + encoder_attention_mask (`torch.FloatTensor`): encoder attention mask of size + `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values. + output_attentions (`bool`, *optional*): + Whether or not to return the attentions tensors of all attention layers. See `attentions` under + returned tensors for more detail. + 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`). + past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states + cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): + Indices depicting the position of the input sequence tokens in the sequence + position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, + with `head_dim` being the embedding dimension of each attention head. + kwargs (`dict`, *optional*): + Arbitrary kwargs to be ignored, used for FSDP and other methods that injects code + into the model + """ + residual = hidden_states + + hidden_states = self.input_layernorm(hidden_states) + + # Self Attention + hidden_states, self_attn_weights, present_key_value = self.self_attn( + hidden_states=hidden_states, + attention_mask=attention_mask, + position_ids=position_ids, + past_key_value=past_key_value, + output_attentions=output_attentions, + use_cache=use_cache, + cache_position=cache_position, + position_embeddings=position_embeddings, + **kwargs, + ) + hidden_states = residual + hidden_states + + # Cross-Attention Block + cross_attn_weights = None + if encoder_hidden_states is not None: + residual = hidden_states + hidden_states = self.post_attention_layernorm(hidden_states) + 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, + past_key_value=past_key_value, + output_attentions=output_attentions, + ) + hidden_states = residual + hidden_states + + # add cross-attn to positions 1 of present_key_value tuple + present_key_value = (present_key_value, cross_attn_present_key_value) + + # Fully Connected + residual = hidden_states + hidden_states = self.final_layer_norm(hidden_states) + hidden_states = self.mlp(hidden_states) + hidden_states = residual + hidden_states + + outputs = (hidden_states,) + + if output_attentions: + outputs += (self_attn_weights, cross_attn_weights) + + if use_cache: + outputs += (present_key_value,) + + return outputs + + +class MoonshineEncoder(PreTrainedModel): + """ + Transformer encoder consisting of *config.encoder_layers* self attention layers. Each layer is a + [`MoonshineEncoderLayer`]. + + Args: + config: MoonshineConfig + """ + + def __init__(self, config: MoonshineConfig): + super().__init__(config) + self.config = config + embed_dim = config.hidden_size + + self.conv1 = nn.Conv1d(1, embed_dim, kernel_size=127, stride=64, bias=False) + self.conv2 = nn.Conv1d(embed_dim, 2 * embed_dim, kernel_size=7, stride=3) + self.conv3 = nn.Conv1d(2 * embed_dim, embed_dim, kernel_size=3, stride=2) + self.groupnorm = nn.GroupNorm(num_groups=1, num_channels=embed_dim, eps=1e-5) + + self.rotary_emb = MoonshineRotaryEmbedding( + dim=max(config.hidden_size // config.num_attention_heads // 2, 32), + max_position_embeddings=config.max_position_embeddings, + ) + + self.layers = nn.ModuleList([MoonshineEncoderLayer(config, idx) for idx in range(config.num_hidden_layers)]) + self.layer_norm = nn.LayerNorm(embed_dim, eps=config.layer_norm_eps, bias=False) + + self.gradient_checkpointing = False + self.post_init() + + def _freeze_parameters(self): + for param in self.parameters(): + param.requires_grad = False + self._requires_grad = False + + def get_input_embeddings(self) -> nn.Module: + return self.conv1 + + def set_input_embeddings(self, value: nn.Module): + self.conv1 = value + + def forward( + self, + input_features, + attention_mask=None, + output_attentions=None, + output_hidden_states=None, + return_dict=None, + ): + r""" + Args: + input_features (`torch.LongTensor` of shape `(batch_size, 1, sequence_length)`): + Float values of the raw speech waveform. Raw speech waveform can be + obtained by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a + `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). + attention_mask (`torch.Tensor`)`, *optional*): + attention mask of size `(batch_size, sequence_length)` if flash attention is used or `(batch_size, 1, + output_attentions (`bool`, *optional*): + Whether or not to return the attentions tensors of all attention layers. See `attentions` under + returned tensors for more detail. + output_hidden_states (`bool`, *optional*): + Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors + for more detail. + return_dict (`bool`, *optional*): + Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. + """ + output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions + output_hidden_states = ( + output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states + ) + return_dict = return_dict if return_dict is not None else self.config.use_return_dict + + inputs_embeds = nn.functional.tanh(self.conv1(input_features)) + inputs_embeds = self.groupnorm(inputs_embeds) + inputs_embeds = nn.functional.gelu(self.conv2(inputs_embeds)) + inputs_embeds = nn.functional.gelu(self.conv3(inputs_embeds)) + inputs_embeds = inputs_embeds.permute(0, 2, 1) + hidden_states = inputs_embeds + + position_ids = torch.arange(inputs_embeds.shape[1], device=inputs_embeds.device).unsqueeze(0) + embed_pos = self.rotary_emb(hidden_states, position_ids) + + encoder_states = () if output_hidden_states else None + all_attentions = () if output_attentions else None + + for encoder_layer in self.layers: + if output_hidden_states: + encoder_states = encoder_states + (hidden_states,) + + if self.gradient_checkpointing and self.training: + layer_outputs = self._gradient_checkpointing_func( + encoder_layer.__call__, + hidden_states, + None, + output_attentions, + position_embeddings=embed_pos, + ) + else: + layer_outputs = encoder_layer( + hidden_states, + None, + output_attentions=output_attentions, + position_embeddings=embed_pos, + ) + + hidden_states = layer_outputs[0] + + if output_attentions: + all_attentions = all_attentions + (layer_outputs[1],) + + hidden_states = self.layer_norm(hidden_states) + if output_hidden_states: + encoder_states = encoder_states + (hidden_states,) + + if not return_dict: + return tuple(v for v in [hidden_states, encoder_states, all_attentions] if v is not None) + return BaseModelOutput( + last_hidden_state=hidden_states, hidden_states=encoder_states, attentions=all_attentions + ) \ No newline at end of file From 651725199ac5c71ff9c69c2b1af5d22b30eb1ec6 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Tue, 3 Dec 2024 19:36:21 +0100 Subject: [PATCH 03/39] full decoder forward --- .../models/moonshine/modular_moonshine.py | 335 +++++++++++++++++- 1 file changed, 325 insertions(+), 10 deletions(-) diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index 17d2df243f6258..3fe39c4d1fbf46 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -1,19 +1,25 @@ from ...configuration_utils import PretrainedConfig from ..phi.modeling_phi import PhiAttention, PhiFlashAttention2, PhiSdpaAttention, PhiMLP, PhiRotaryEmbedding -from ..llama.modeling_llama import LlamaDecoderLayer +from ..llama.modeling_llama import LlamaDecoderLayer, LlamaModel from ..mistral.modeling_mistral import MistralMLP from ..whisper.modeling_whisper import WhisperEncoder -from ...cache_utils import Cache, DynamicCache, StaticCache +from typing import List, Optional, Tuple, Union +from ...processing_utils import Unpack + +from ...cache_utils import Cache, DynamicCache, EncoderDecoderCache from ...modeling_outputs import ( BaseModelOutput, + BaseModelOutputWithPast, BaseModelOutputWithPastAndCrossAttentions, CausalLMOutputWithCrossAttentions, Seq2SeqLMOutput, Seq2SeqModelOutput, SequenceClassifierOutput, ) +from ...modeling_flash_attention_utils import FlashAttentionKwargs, _flash_attention_forward + from ...modeling_utils import PreTrainedModel import torch.nn as nn @@ -33,6 +39,11 @@ from ...activations import ACT2FN import copy +import math + +logger = logging.get_logger(__name__) + + class MoonshineConfig(PretrainedConfig): r""" This is the configuration class to store the configuration of a [`MoonshineModel`]. It is used to instantiate a Moonshine @@ -69,6 +80,8 @@ class MoonshineConfig(PretrainedConfig): The non-linear activation function (function or string) in the decoder. max_position_embeddings (`int`, *optional*, defaults to 2048): The maximum sequence length that this model might ever be used with. TODO: check this + initializer_range (`float`, *optional*, defaults to 0.02): + The standard deviation of the truncated_normal_initializer for initializing all weight matrices. layer_norm_eps (`float`, *optional*, defaults to 1e-5): The epsilon used by the layer normalization layers. use_cache (`bool`, *optional*, defaults to `True`): @@ -156,6 +169,7 @@ def __init__( encoder_hidden_act="gelu", decoder_hidden_act="silu", max_position_embeddings=2048, + initializer_range=0.02, layer_norm_eps=1e-5, use_cache=True, rope_theta=10000.0, @@ -182,6 +196,7 @@ def __init__( self.encoder_hidden_act = encoder_hidden_act self.decoder_hidden_act = decoder_hidden_act self.max_position_embeddings = max_position_embeddings + self.initializer_range = initializer_range self.layer_norm_eps = layer_norm_eps self.use_cache = use_cache self.rope_theta = rope_theta @@ -199,6 +214,16 @@ def __init__( **kwargs, ) +def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor: + """ + This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch, + num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim) + """ + batch, num_key_value_heads, slen, head_dim = hidden_states.shape + if n_rep == 1: + return hidden_states + hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim) + return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim) def rotate_every_two(x: torch.Tensor) -> torch.Tensor: x1 = x[:, :, :, ::2] @@ -270,16 +295,24 @@ def __init__(self, config: MoonshineConfig, hidden_act: str): super().__init__(config) -class MoonshineGatedMLP(MistralMLP): +class MoonshineGatedMLP(nn.Module): def __init__(self, config: MoonshineConfig, hidden_act: str): + super().__init__() config = copy.deepcopy(config) config.hidden_act = hidden_act if config.intermediate_size is None: config.intermediate_size = config.hidden_size * config.ff_mult * 2 - super().__init__(config) + self.hidden_size = config.hidden_size + self.intermediate_size = config.intermediate_size self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=True) - self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False) - self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=True) + self.down_proj = nn.Linear(self.intermediate_size // 2, self.hidden_size, bias=True) + self.act_fn = ACT2FN[config.hidden_act] + + def forward(self, hidden_state): + hidden_state = self.up_proj(hidden_state) + hidden_state, gate = hidden_state.chunk(2, dim=-1) + hidden_state = self.act_fn(gate) * hidden_state + return self.down_proj(hidden_state) class MoonshineMLP: @@ -305,6 +338,129 @@ def __init__(self, config: MoonshineConfig, layer_idx: Optional[int] = None): dim=self.rotary_ndims, max_position_embeddings=config.max_position_embeddings, ) + + def forward( + self, + hidden_states: torch.Tensor, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + past_key_value: Optional[Cache] = None, + key_value_states: Optional[torch.Tensor] = None, + output_attentions: bool = False, + use_cache: bool = False, + cache_position: Optional[torch.LongTensor] = None, + position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 + ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + bsz, q_len, _ = hidden_states.size() + + query_states = self.q_proj(hidden_states) + + # if key_value_states are provided this layer is used as a cross-attention layer + # for the decoder + is_cross_attention = key_value_states is not None + + if past_key_value is not None: + is_updated = past_key_value.is_updated.get(self.layer_idx) + if is_cross_attention: + # after the first generated id, we can subsequently re-use all key/value_states from cache + past_key_value.is_updated[self.layer_idx] = True + past_key_value = past_key_value.cross_attention_cache + else: + past_key_value = past_key_value.self_attention_cache + + # use key_value_states if cross attention + current_states = key_value_states if key_value_states is not None else hidden_states + if is_cross_attention and past_key_value and is_updated: + # reuse k,v, cross_attentions + key_states = past_key_value.key_cache[self.layer_idx] + value_states = past_key_value.value_cache[self.layer_idx] + else: + key_states = self.k_proj(current_states) + value_states = self.v_proj(current_states) + + if self.qk_layernorm: + query_states = self.q_layernorm(query_states) + key_states = self.k_layernorm(key_states) + + query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) + key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) + value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) + + if not is_cross_attention: + if position_embeddings is None: + logger.warning_once( + "The attention layers in this model are transitioning from computing the RoPE embeddings internally " + "through `position_ids` (2D tensor with the indexes of the tokens), to using externally computed " + "`position_embeddings` (Tuple of tensors, containing cos and sin). In v4.46 `position_ids` will be " + "removed and `position_embeddings` will be mandatory." + ) + cos, sin = self.rotary_emb(value_states, position_ids) + else: + cos, sin = position_embeddings + + # Partial rotary embedding + query_rot, query_pass = ( + query_states[..., : self.rotary_ndims], + query_states[..., self.rotary_ndims :], + ) + key_rot, key_pass = ( + key_states[..., : self.rotary_ndims], + key_states[..., self.rotary_ndims :], + ) + # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor] + query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin) + + # [batch_size, seq_length, num_heads, head_dim] + query_states = torch.cat((query_rot, query_pass), dim=-1) + key_states = torch.cat((key_rot, key_pass), dim=-1) + + if past_key_value is not None: + if not is_cross_attention: + cache_kwargs = { + "sin": sin, + "cos": cos, + "partial_rotation_size": self.rotary_ndims, + "cache_position": cache_position, + } + else: + cache_kwargs = { + "cache_position": cache_position, + } + key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs) + + key_states = repeat_kv(key_states, self.num_key_value_groups) + value_states = repeat_kv(value_states, self.num_key_value_groups) + + # Queries and keys upcast to fp32 is required by Moonshine-2 to avoid overflow + attn_weights = torch.matmul( + query_states.to(torch.float32), key_states.to(torch.float32).transpose(2, 3) + ) / math.sqrt(self.head_dim) + + if attention_mask is not None: + causal_mask = attention_mask[:, :, :, : key_states.shape[-2]] + attn_weights += causal_mask + + # upcast attention to fp32 + attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(value_states.dtype) + attn_weights = nn.functional.dropout(attn_weights, p=self.attention_dropout, training=self.training) + + attn_output = torch.matmul(attn_weights, value_states) + + if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim): + raise ValueError( + f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is" + f" {attn_output.size()}" + ) + + attn_output = attn_output.transpose(1, 2).contiguous() + attn_output = attn_output.reshape(bsz, q_len, self.hidden_size) + + attn_output = self.dense(attn_output) + + if not output_attentions: + attn_weights = None + + return attn_output, attn_weights, past_key_value class MoonshineFlashAttention2(PhiFlashAttention2): @@ -342,7 +498,7 @@ def __init__(self, config: MoonshineConfig, layer_idx: int = None): self.mlp = MoonshineMLP(config, config.decoder_hidden_act) self.input_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) self.post_attention_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) - self.final_layer_norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) + self.final_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) def forward( self, @@ -351,11 +507,13 @@ def forward( encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, + encoder_position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Cache] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 + encoder_position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, **kwargs, ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: """ @@ -380,6 +538,9 @@ def forward( position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. + encoder_position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + Tuple containing the cosine and sine positional embeddings of shape `(batch_size, encoder_seq_len, head_dim)`, + with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): Arbitrary kwargs to be ignored, used for FSDP and other methods that injects code into the model @@ -411,8 +572,11 @@ def forward( hidden_states=hidden_states, key_value_states=encoder_hidden_states, attention_mask=encoder_attention_mask, + position_ids=encoder_position_ids, past_key_value=past_key_value, output_attentions=output_attentions, + use_cache=use_cache, + position_embeddings=encoder_position_embeddings, ) hidden_states = residual + hidden_states @@ -421,7 +585,7 @@ def forward( # Fully Connected residual = hidden_states - hidden_states = self.final_layer_norm(hidden_states) + hidden_states = self.final_layernorm(hidden_states) hidden_states = self.mlp(hidden_states) hidden_states = residual + hidden_states @@ -435,7 +599,6 @@ def forward( return outputs - class MoonshineEncoder(PreTrainedModel): """ Transformer encoder consisting of *config.encoder_layers* self attention layers. Each layer is a @@ -554,4 +717,156 @@ def forward( return tuple(v for v in [hidden_states, encoder_states, all_attentions] if v is not None) return BaseModelOutput( last_hidden_state=hidden_states, hidden_states=encoder_states, attentions=all_attentions - ) \ No newline at end of file + ) + + +class MoonshineDecoder(LlamaModel): + def __init__(self, config: MoonshineConfig): + super().__init__(config) + self.norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) + self.rotary_emb = MoonshineRotaryEmbedding( + dim= max(config.hidden_size // config.num_attention_heads // 2, 32), + max_position_embeddings=config.max_position_embeddings, + ) + + def forward( + self, + input_ids: torch.LongTensor = None, + attention_mask: Optional[torch.Tensor] = None, + encoder_hidden_states: Optional[torch.FloatTensor] = None, + position_ids: Optional[torch.LongTensor] = None, + encoder_position_ids: Optional[torch.LongTensor] = 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, + output_hidden_states: Optional[bool] = None, + return_dict: Optional[bool] = None, + cache_position: Optional[torch.LongTensor] = None, + **flash_attn_kwargs: Unpack[FlashAttentionKwargs], + ) -> Union[Tuple, BaseModelOutputWithPast]: + output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions + output_hidden_states = ( + output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states + ) + 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 None) ^ (inputs_embeds is not None): + raise ValueError("You must specify exactly one of input_ids or inputs_embeds") + + 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) + + # kept for BC (non `Cache` `past_key_values` inputs) + return_legacy_cache = False + return_self_attention_cache = False + if use_cache or past_key_values is not None: + if isinstance(past_key_values, Cache) and not isinstance(past_key_values, EncoderDecoderCache): + return_self_attention_cache = True + past_key_values = EncoderDecoderCache(past_key_values, DynamicCache()) + elif not isinstance(past_key_values, EncoderDecoderCache): + return_legacy_cache = True + logger.warning_once( + "Passing a tuple of `past_key_values` is deprecated and will be removed in Transformers v4.43.0. " + "You should pass an instance of `EncoderDecoderCache` instead, e.g. " + "`past_key_values=EncoderDecoderCache.from_legacy_cache(past_key_values)`." + ) + past_key_values = EncoderDecoderCache.from_legacy_cache(past_key_values) + + 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 + inputs_embeds.shape[1], device=inputs_embeds.device + ) + if position_ids is None: + position_ids = cache_position.unsqueeze(0) + + if encoder_position_ids is None: + encoder_position_ids = torch.arange( + encoder_hidden_states.shape[1], device=encoder_hidden_states.device + ).unsqueeze(0) + + causal_mask = self._update_causal_mask( + attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions + ) + hidden_states = inputs_embeds + + # create position embeddings to be shared across the decoder layers + position_embeddings = self.rotary_emb(hidden_states, position_ids) + encoder_position_embeddings = self.rotary_emb(encoder_hidden_states, encoder_position_ids) + + # decoder layers + 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 + + for decoder_layer in self.layers: + if output_hidden_states: + all_hidden_states += (hidden_states,) + + if self.gradient_checkpointing and self.training: + layer_outputs = self._gradient_checkpointing_func( + decoder_layer.__call__, + hidden_states, + causal_mask, + encoder_hidden_states, + position_ids, + past_key_values, + output_attentions, + use_cache, + cache_position, + position_embeddings, + ) + else: + layer_outputs = decoder_layer( + hidden_states, + attention_mask=causal_mask, + encoder_hidden_states=encoder_hidden_states, + position_ids=position_ids, + encoder_position_ids=encoder_position_ids, + past_key_value=past_key_values, + output_attentions=output_attentions, + use_cache=use_cache, + cache_position=cache_position, + position_embeddings=position_embeddings, + encoder_position_embeddings=encoder_position_embeddings, + **flash_attn_kwargs, + ) + + hidden_states = layer_outputs[0] + + if output_attentions: + all_self_attns += (layer_outputs[1],) + + if encoder_hidden_states is not None: + all_cross_attentions += (layer_outputs[2],) + + hidden_states = self.norm(hidden_states) + + # add hidden states from the last decoder layer + if output_hidden_states: + all_hidden_states += (hidden_states,) + + next_cache = past_key_values if use_cache else None + if return_self_attention_cache: + next_cache = past_key_values.self_attention_cache + if return_legacy_cache: + next_cache = next_cache.to_legacy_cache() + + if not return_dict: + return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns, all_cross_attentions] if v is not None) + return BaseModelOutputWithPastAndCrossAttentions( + last_hidden_state=hidden_states, + past_key_values=next_cache, + hidden_states=all_hidden_states, + attentions=all_self_attns, + cross_attentions=all_cross_attentions, + ) From b0efed1e41f0c651ac8846db55b58ce395ea127a Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Thu, 12 Dec 2024 14:47:01 +0100 Subject: [PATCH 04/39] fix sdpa and FA2 --- .../models/moonshine/modular_moonshine.py | 315 +++++++++++++++++- 1 file changed, 312 insertions(+), 3 deletions(-) diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index 3fe39c4d1fbf46..737847b6eb9132 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -464,11 +464,274 @@ def forward( class MoonshineFlashAttention2(PhiFlashAttention2): - pass + def forward( + self, + hidden_states: torch.Tensor, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + past_key_value: Optional[Cache] = None, + key_value_states: Optional[torch.Tensor] = None, + output_attentions: bool = False, + use_cache: bool = False, + cache_position: Optional[torch.LongTensor] = None, + position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 + ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + bsz, q_len, _ = hidden_states.size() + + query_states = self.q_proj(hidden_states) + + # if key_value_states are provided this layer is used as a cross-attention layer + # for the decoder + is_cross_attention = key_value_states is not None + + if past_key_value is not None: + is_updated = past_key_value.is_updated.get(self.layer_idx) + if is_cross_attention: + # after the first generated id, we can subsequently re-use all key/value_states from cache + past_key_value.is_updated[self.layer_idx] = True + past_key_value = past_key_value.cross_attention_cache + else: + past_key_value = past_key_value.self_attention_cache + + # use key_value_states if cross attention + current_states = key_value_states if key_value_states is not None else hidden_states + if is_cross_attention and past_key_value and is_updated: + # reuse k,v, cross_attentions + key_states = past_key_value.key_cache[self.layer_idx] + value_states = past_key_value.value_cache[self.layer_idx] + else: + key_states = self.k_proj(current_states) + value_states = self.v_proj(current_states) + + if self.qk_layernorm: + query_states = self.q_layernorm(query_states) + key_states = self.k_layernorm(key_states) + + query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) + key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) + value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) + + if not is_cross_attention: + if position_embeddings is None: + logger.warning_once( + "The attention layers in this model are transitioning from computing the RoPE embeddings internally " + "through `position_ids` (2D tensor with the indexes of the tokens), to using externally computed " + "`position_embeddings` (Tuple of tensors, containing cos and sin). In v4.46 `position_ids` will be " + "removed and `position_embeddings` will be mandatory." + ) + cos, sin = self.rotary_emb(value_states, position_ids) + else: + cos, sin = position_embeddings + + # Partial rotary embedding + query_rot, query_pass = ( + query_states[..., : self.rotary_ndims], + query_states[..., self.rotary_ndims :], + ) + key_rot, key_pass = ( + key_states[..., : self.rotary_ndims], + key_states[..., self.rotary_ndims :], + ) + # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor] + query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin) + + # [batch_size, seq_length, num_heads, head_dim] + query_states = torch.cat((query_rot, query_pass), dim=-1) + key_states = torch.cat((key_rot, key_pass), dim=-1) + + if past_key_value is not None: + if not is_cross_attention: + cache_kwargs = { + "sin": sin, + "cos": cos, + "partial_rotation_size": self.rotary_ndims, + "cache_position": cache_position, + } + else: + cache_kwargs = { + "cache_position": cache_position, + } + key_states, value_states = past_key_value.update(key_states, value_states, 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. + query_states = query_states.transpose(1, 2) + key_states = key_states.transpose(1, 2) + value_states = value_states.transpose(1, 2) + + attn_dropout = self.attention_dropout if self.training else 0.0 + + # 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. + # This might slowdown training & inference so it is recommended to not cast the LayerNorms + # in fp32. + + if query_states.dtype == torch.float32: + if torch.is_autocast_enabled(): + target_dtype = torch.get_autocast_gpu_dtype() + # Handle the case where the model is quantized + elif hasattr(self.config, "_pre_quantization_dtype"): + target_dtype = self.config._pre_quantization_dtype + else: + target_dtype = self.q_proj.weight.dtype + + logger.warning_once( + f"The input hidden states seems to be silently casted in float32, this might be related to" + f" the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in" + f" {target_dtype}." + ) + + query_states = query_states.to(target_dtype) + key_states = key_states.to(target_dtype) + value_states = value_states.to(target_dtype) + + attn_output = _flash_attention_forward( + query_states, + key_states, + value_states, + attention_mask, + q_len, + position_ids=position_ids, + dropout=attn_dropout, + softmax_scale=None, + use_top_left_mask=self._flash_attn_uses_top_left_mask, + is_causal=self.is_causal, + ) + + attn_output = attn_output.reshape(bsz, q_len, self.hidden_size).contiguous() + attn_output = self.dense(attn_output) + + if not output_attentions: + attn_weights = None + + return attn_output, attn_weights, past_key_value class MoonshineSdpaAttention(PhiSdpaAttention): - pass + def forward( + self, + hidden_states: torch.Tensor, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + past_key_value: Optional[Cache] = None, + key_value_states: Optional[torch.Tensor] = None, + output_attentions: bool = False, + use_cache: bool = False, + cache_position: Optional[torch.LongTensor] = None, + position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 + ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + bsz, q_len, _ = hidden_states.size() + + query_states = self.q_proj(hidden_states) + + # if key_value_states are provided this layer is used as a cross-attention layer + # for the decoder + is_cross_attention = key_value_states is not None + + if past_key_value is not None: + is_updated = past_key_value.is_updated.get(self.layer_idx) + if is_cross_attention: + # after the first generated id, we can subsequently re-use all key/value_states from cache + past_key_value.is_updated[self.layer_idx] = True + past_key_value = past_key_value.cross_attention_cache + else: + past_key_value = past_key_value.self_attention_cache + + # use key_value_states if cross attention + current_states = key_value_states if key_value_states is not None else hidden_states + if is_cross_attention and past_key_value and is_updated: + # reuse k,v, cross_attentions + key_states = past_key_value.key_cache[self.layer_idx] + value_states = past_key_value.value_cache[self.layer_idx] + else: + key_states = self.k_proj(current_states) + value_states = self.v_proj(current_states) + + if self.qk_layernorm: + query_states = self.q_layernorm(query_states) + key_states = self.k_layernorm(key_states) + + query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) + key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) + value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) + + if not is_cross_attention: + if position_embeddings is None: + logger.warning_once( + "The attention layers in this model are transitioning from computing the RoPE embeddings internally " + "through `position_ids` (2D tensor with the indexes of the tokens), to using externally computed " + "`position_embeddings` (Tuple of tensors, containing cos and sin). In v4.46 `position_ids` will be " + "removed and `position_embeddings` will be mandatory." + ) + cos, sin = self.rotary_emb(value_states, position_ids) + else: + cos, sin = position_embeddings + + # Partial rotary embedding + query_rot, query_pass = ( + query_states[..., : self.rotary_ndims], + query_states[..., self.rotary_ndims :], + ) + key_rot, key_pass = ( + key_states[..., : self.rotary_ndims], + key_states[..., self.rotary_ndims :], + ) + # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor] + query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin) + + # [batch_size, seq_length, num_heads, head_dim] + query_states = torch.cat((query_rot, query_pass), dim=-1) + key_states = torch.cat((key_rot, key_pass), dim=-1) + + if past_key_value is not None: + if not is_cross_attention: + cache_kwargs = { + "sin": sin, + "cos": cos, + "partial_rotation_size": self.rotary_ndims, + "cache_position": cache_position, + } + else: + cache_kwargs = { + "cache_position": cache_position, + } + key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs) + + key_states = repeat_kv(key_states, self.num_key_value_groups) + value_states = repeat_kv(value_states, self.num_key_value_groups) + + causal_mask = attention_mask + if attention_mask is not None: + causal_mask = causal_mask[:, :, :, : key_states.shape[-2]] + + # SDPA with memory-efficient backend is broken in torch==2.1.2 when using non-contiguous inputs and a custom + # attn_mask, so we need to call `.contiguous()` here. This was fixed in torch==2.2.0. + # Reference: https://github.com/pytorch/pytorch/issues/112577 + if self.require_contiguous_qkv and query_states.device.type == "cuda" and attention_mask is not None: + query_states = query_states.contiguous() + key_states = key_states.contiguous() + value_states = value_states.contiguous() + + # 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 causal_mask is None and q_len > 1 else False + + attn_output = torch.nn.functional.scaled_dot_product_attention( + query_states, + key_states, + value_states, + attn_mask=causal_mask, + dropout_p=self.attention_dropout if self.training else 0.0, + is_causal=is_causal, + ) + + attn_output = attn_output.transpose(1, 2).contiguous() + attn_output = attn_output.reshape(bsz, q_len, self.hidden_size) + + attn_output = self.dense(attn_output) + + return attn_output, None, past_key_value MOONSHINE_ATTENTION_CLASSES = { @@ -598,8 +861,54 @@ def forward( outputs += (present_key_value,) return outputs + + +MOONSHINE_START_DOCSTRING = r""" + This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the + library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads + etc.) + + This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. + Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage + and behavior. -class MoonshineEncoder(PreTrainedModel): + Parameters: + config ([`MoonshineConfig`]): + Model configuration class with all the parameters of the model. Initializing with a config file does not + load the weights associated with the model, only the configuration. Check out the + [`~PreTrainedModel.from_pretrained`] method to load the model weights. +""" + + +@add_start_docstrings( + "The bare Moonshine Model outputting raw hidden-states without any specific head on top.", + MOONSHINE_START_DOCSTRING, +) +class MoonshinePreTrainedModel(PreTrainedModel): + config_class = MoonshineConfig + base_model_prefix = "model" + supports_gradient_checkpointing = True + _no_split_modules = ["MoonshineDecoderLayer"] + _skip_keys_device_placement = ["past_key_values"] + _supports_flash_attn_2 = True + _supports_sdpa = True + _supports_cache_class = True + _supports_quantized_cache = True + _supports_static_cache = True + + def _init_weights(self, module): + std = self.config.initializer_range + if isinstance(module, nn.Linear): + module.weight.data.normal_(mean=0.0, std=std) + if module.bias is not None: + module.bias.data.zero_() + elif isinstance(module, nn.Embedding): + module.weight.data.normal_(mean=0.0, std=std) + if module.padding_idx is not None: + module.weight.data[module.padding_idx].zero_() + + +class MoonshineEncoder(MoonshinePreTrainedModel): """ Transformer encoder consisting of *config.encoder_layers* self attention layers. Each layer is a [`MoonshineEncoderLayer`]. From b4d18f9e0c0e5a2047f9231236f2c2408dbb8e01 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Thu, 12 Dec 2024 15:01:15 +0100 Subject: [PATCH 05/39] fix sdpa and FA2 --- src/transformers/models/moonshine/modular_moonshine.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index 737847b6eb9132..f009bf81bf8f4b 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -326,7 +326,7 @@ def __new__(cls, config: MoonshineConfig, hidden_act: str): class MoonshineAttention(PhiAttention): - def __init__(self, config: MoonshineConfig, layer_idx: Optional[int] = None): + def __init__(self, config: MoonshineConfig, layer_idx: Optional[int] = None, is_causal: bool = False): super().__init__(config, layer_idx) self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=config.attention_bias) self.k_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config.attention_bias) @@ -338,6 +338,8 @@ def __init__(self, config: MoonshineConfig, layer_idx: Optional[int] = None): dim=self.rotary_ndims, max_position_embeddings=config.max_position_embeddings, ) + + self.is_causal = is_causal def forward( self, @@ -715,7 +717,7 @@ 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 causal_mask is None and q_len > 1 else False + is_causal = True if self.is_causal and causal_mask is None and q_len > 1 else False attn_output = torch.nn.functional.scaled_dot_product_attention( query_states, @@ -755,8 +757,8 @@ def __init__(self, config: MoonshineConfig, layer_idx: int = None): super().__init__() self.hidden_size = config.hidden_size - self.self_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation](config=config, layer_idx=layer_idx) - self.encoder_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation](config=config, layer_idx=layer_idx) + self.self_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation](config=config, layer_idx=layer_idx, is_causal=True) + self.encoder_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation](config=config, layer_idx=layer_idx, is_causal=True) self.mlp = MoonshineMLP(config, config.decoder_hidden_act) self.input_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) From b3777e0c6d6bd3a0dd892ab6ef6e0d29fc9b19b5 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Fri, 13 Dec 2024 12:31:14 +0100 Subject: [PATCH 06/39] moonshine model --- .../models/moonshine/modular_moonshine.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index f009bf81bf8f4b..59fb6d4a8a7b1d 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -2,7 +2,7 @@ from ..phi.modeling_phi import PhiAttention, PhiFlashAttention2, PhiSdpaAttention, PhiMLP, PhiRotaryEmbedding from ..llama.modeling_llama import LlamaDecoderLayer, LlamaModel from ..mistral.modeling_mistral import MistralMLP -from ..whisper.modeling_whisper import WhisperEncoder +from ..whisper.modeling_whisper import WhisperModel from typing import List, Optional, Tuple, Union from ...processing_utils import Unpack @@ -1181,3 +1181,12 @@ def forward( attentions=all_self_attns, cross_attentions=all_cross_attentions, ) + + +class MoonshineModel(WhisperModel): + def __init__(self, config: MoonshineConfig): + super().__init__(config) + self.encoder = MoonshineEncoder(config) + self.decoder = MoonshineDecoder(config) + + From e313ab563972bc1ed91a1c459409ebcb6d204fe0 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Fri, 13 Dec 2024 12:31:42 +0100 Subject: [PATCH 07/39] moonshine model forward --- .../models/moonshine/modular_moonshine.py | 381 +++++++++++++++++- 1 file changed, 378 insertions(+), 3 deletions(-) diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index 59fb6d4a8a7b1d..88c27063da0cfd 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -4,10 +4,13 @@ from ..mistral.modeling_mistral import MistralMLP from ..whisper.modeling_whisper import WhisperModel +from torch.nn import CrossEntropyLoss + from typing import List, Optional, Tuple, Union from ...processing_utils import Unpack from ...cache_utils import Cache, DynamicCache, EncoderDecoderCache +from ...generation import GenerationMixin from ...modeling_outputs import ( BaseModelOutput, @@ -43,6 +46,8 @@ logger = logging.get_logger(__name__) +_CONFIG_FOR_DOC = "MoonshineConfig" + class MoonshineConfig(PretrainedConfig): r""" @@ -84,8 +89,14 @@ class MoonshineConfig(PretrainedConfig): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. layer_norm_eps (`float`, *optional*, defaults to 1e-5): The epsilon used by the layer normalization layers. + decoder_start_token_id (`int`, *optional*, defaults to 1): + Corresponds to the "<|startoftranscript|>" token, which is automatically used when no `decoder_input_ids` + are provided to the `generate` function. It is used to guide the model`s generation process depending on + the task. use_cache (`bool`, *optional*, defaults to `True`): Whether or not the model should return the last key/values attentions (not used by all models). + is_encoder_decoder (`bool`, *optional*, defaults to `True`): + Whether the model is used as an encoder/decoder or not. rope_theta (`float`, *optional*, defaults to 10000.0): The base period of the RoPE embeddings. TODO: check this partial_rotary_factor (`float`, *optional*, defaults to 0.5): @@ -139,6 +150,35 @@ class MoonshineConfig(PretrainedConfig): Denotes beginning of sequences token id. eos_token_id (`int`, *optional*, defaults to 2): Denotes end of sequences token id. + apply_spec_augment (`bool`, *optional*, defaults to `False`): + Whether to apply *SpecAugment* data augmentation to the outputs of the feature encoder. For reference see + [SpecAugment: A Simple Data Augmentation Method for Automatic Speech + Recognition](https://arxiv.org/abs/1904.08779). + mask_time_prob (`float`, *optional*, defaults to 0.05): + Percentage (between 0 and 1) of all feature vectors along the time axis which will be masked. The masking + procecure generates `mask_time_prob*len(time_axis)/mask_time_length` independent masks over the axis. If + reasoning from the propability of each feature vector to be chosen as the start of the vector span to be + masked, *mask_time_prob* should be `prob_vector_start*mask_time_length`. Note that overlap may decrease the + actual percentage of masked vectors. This is only relevant if `apply_spec_augment == True`. + mask_time_length (`int`, *optional*, defaults to 10): + Length of vector span along the time axis. + mask_time_min_masks (`int`, *optional*, defaults to 2),: + The minimum number of masks of length `mask_feature_length` generated along the time axis, each time step, + irrespectively of `mask_feature_prob`. Only relevant if ''mask_time_prob*len(time_axis)/mask_time_length < + mask_time_min_masks'' + mask_feature_prob (`float`, *optional*, defaults to 0.0): + Percentage (between 0 and 1) of all feature vectors along the feature axis which will be masked. The + masking procecure generates `mask_feature_prob*len(feature_axis)/mask_time_length` independent masks over + the axis. If reasoning from the propability of each feature vector to be chosen as the start of the vector + span to be masked, *mask_feature_prob* should be `prob_vector_start*mask_feature_length`. Note that overlap + may decrease the actual percentage of masked vectors. This is only relevant if `apply_spec_augment is + True`. + mask_feature_length (`int`, *optional*, defaults to 10): + Length of vector span along the feature axis. + mask_feature_min_masks (`int`, *optional*, defaults to 0),: + The minimum number of masks of length `mask_feature_length` generated along the feature axis, each time + step, irrespectively of `mask_feature_prob`. Only relevant if + `mask_feature_prob*len(feature_axis)/mask_feature_length < mask_feature_min_masks`. Example: @@ -171,7 +211,9 @@ def __init__( max_position_embeddings=2048, initializer_range=0.02, layer_norm_eps=1e-5, + decoder_start_token_id=1, use_cache=True, + is_encoder_decoder=True, rope_theta=10000.0, partial_rotary_factor=0.5, attention_bias=False, @@ -181,6 +223,13 @@ def __init__( ff_mult=4, bos_token_id=1, eos_token_id=2, + apply_spec_augment=False, + mask_time_prob=0.05, + mask_time_length=10, + mask_time_min_masks=2, + mask_feature_prob=0.0, + mask_feature_length=10, + mask_feature_min_masks=0, **kwargs, ): self.vocab_size = vocab_size @@ -198,7 +247,9 @@ def __init__( self.max_position_embeddings = max_position_embeddings self.initializer_range = initializer_range self.layer_norm_eps = layer_norm_eps + self.decoder_start_token_id = decoder_start_token_id self.use_cache = use_cache + self.is_encoder_decoder = is_encoder_decoder self.rope_theta = rope_theta self.partial_rotary_factor = partial_rotary_factor @@ -208,12 +259,41 @@ def __init__( self.rope_scaling = rope_scaling self.ff_mult = ff_mult + # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 + self.apply_spec_augment = apply_spec_augment + self.mask_time_prob = mask_time_prob + self.mask_time_length = mask_time_length + self.mask_time_min_masks = mask_time_min_masks + self.mask_feature_prob = mask_feature_prob + self.mask_feature_length = mask_feature_length + self.mask_feature_min_masks = mask_feature_min_masks + super().__init__( bos_token_id=bos_token_id, eos_token_id=eos_token_id, + is_encoder_decoder=is_encoder_decoder, + decoder_start_token_id=decoder_start_token_id, **kwargs, ) + +# Copied from transformers.models.bart.modeling_bart.shift_tokens_right +def shift_tokens_right(input_ids: torch.Tensor, pad_token_id: int, decoder_start_token_id: int): + """ + Shift input ids one token to the right. + """ + shifted_input_ids = input_ids.new_zeros(input_ids.shape) + shifted_input_ids[:, 1:] = input_ids[:, :-1].clone() + shifted_input_ids[:, 0] = decoder_start_token_id + + if pad_token_id is None: + raise ValueError("self.model.config.pad_token_id has to be defined.") + # replace possible -100 values in labels by `pad_token_id` + shifted_input_ids.masked_fill_(shifted_input_ids == -100, pad_token_id) + + return shifted_input_ids + + def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor: """ This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch, @@ -225,6 +305,7 @@ def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor: hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim) return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim) + def rotate_every_two(x: torch.Tensor) -> torch.Tensor: x1 = x[:, :, :, ::2] x2 = x[:, :, :, 1::2] @@ -958,6 +1039,7 @@ def forward( output_attentions=None, output_hidden_states=None, return_dict=None, + **kwargs, ): r""" Args: @@ -1003,14 +1085,14 @@ def forward( layer_outputs = self._gradient_checkpointing_func( encoder_layer.__call__, hidden_states, - None, + attention_mask, output_attentions, position_embeddings=embed_pos, ) else: layer_outputs = encoder_layer( hidden_states, - None, + attention_mask, output_attentions=output_attentions, position_embeddings=embed_pos, ) @@ -1029,7 +1111,82 @@ def forward( return BaseModelOutput( last_hidden_state=hidden_states, hidden_states=encoder_states, attentions=all_attentions ) - + + +MOONSHINE_INPUTS_DOCSTRING = r""" + Args: + input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): + Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide + it. + + Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and + [`PreTrainedTokenizer.__call__`] for details. + + [What are input IDs?](../glossary#input-ids) + attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): + Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`: + + - 1 for tokens that are **not masked**, + - 0 for tokens that are **masked**. + + [What are attention masks?](../glossary#attention-mask) + + Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and + [`PreTrainedTokenizer.__call__`] for details. + + If `past_key_values` is used, optionally only the last `input_ids` have to be input (see + `past_key_values`). + + If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`] + and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more + information on the default strategy. + + - 1 indicates the head is **not masked**, + - 0 indicates the head is **masked**. + position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): + Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0, + config.n_positions - 1]`. + + [What are position IDs?](../glossary#position-ids) + 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, see our + [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache); + - 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)`. + inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): + 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. + 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`). + output_attentions (`bool`, *optional*): + Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned + tensors for more detail. + output_hidden_states (`bool`, *optional*): + Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for + 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. +""" + class MoonshineDecoder(LlamaModel): def __init__(self, config: MoonshineConfig): @@ -1189,4 +1346,222 @@ def __init__(self, config: MoonshineConfig): self.encoder = MoonshineEncoder(config) self.decoder = MoonshineDecoder(config) + def forward( + self, + input_features: Optional[torch.FloatTensor] = None, + attention_mask: Optional[torch.LongTensor] = None, + decoder_input_ids: Optional[torch.LongTensor] = None, + decoder_attention_mask: Optional[torch.LongTensor] = None, + encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[EncoderDecoderCache, Tuple[torch.FloatTensor]]] = None, + decoder_inputs_embeds: Optional[Tuple[torch.FloatTensor]] = None, + decoder_position_ids: Optional[Tuple[torch.LongTensor]] = None, + use_cache: Optional[bool] = None, + 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], Seq2SeqModelOutput]: + r""" + Returns: + + Example: + ```python + >>> import torch + >>> from transformers import AutoFeatureExtractor, WhisperModel + >>> from datasets import load_dataset + + >>> model = WhisperModel.from_pretrained("openai/whisper-base") + >>> feature_extractor = AutoFeatureExtractor.from_pretrained("openai/whisper-base") + >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") + >>> inputs = feature_extractor(ds[0]["audio"]["array"], return_tensors="pt") + >>> input_features = inputs.input_features + >>> decoder_input_ids = torch.tensor([[1, 1]]) * model.config.decoder_start_token_id + >>> last_hidden_state = model(input_features, decoder_input_ids=decoder_input_ids).last_hidden_state + >>> list(last_hidden_state.shape) + [1, 2, 512] + ```""" + output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions + output_hidden_states = ( + output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states + ) + 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 encoder_outputs is None: + input_features = self._mask_input_features(input_features, attention_mask=attention_mask) + + encoder_outputs = self.encoder( + input_features, + output_attentions=output_attentions, + output_hidden_states=output_hidden_states, + return_dict=return_dict, + ) + # If the user passed a tuple for encoder_outputs, we wrap it in a BaseModelOutput when return_dict=True + elif return_dict and not isinstance(encoder_outputs, BaseModelOutput): + encoder_outputs = BaseModelOutput( + last_hidden_state=encoder_outputs[0], + hidden_states=encoder_outputs[1] if len(encoder_outputs) > 1 else None, + attentions=encoder_outputs[2] if len(encoder_outputs) > 2 else None, + ) + + # decoder outputs consists of (dec_features, past_key_value, dec_hidden, dec_attn) + decoder_outputs = self.decoder( + input_ids=decoder_input_ids, + attention_mask=decoder_attention_mask, + encoder_hidden_states=encoder_outputs[0], + past_key_values=past_key_values, + inputs_embeds=decoder_inputs_embeds, + position_ids=decoder_position_ids, + use_cache=use_cache, + output_attentions=output_attentions, + output_hidden_states=output_hidden_states, + return_dict=return_dict, + cache_position=cache_position, + ) + + if not return_dict: + return decoder_outputs + encoder_outputs + + return Seq2SeqModelOutput( + last_hidden_state=decoder_outputs.last_hidden_state, + past_key_values=decoder_outputs.past_key_values, + decoder_hidden_states=decoder_outputs.hidden_states, + decoder_attentions=decoder_outputs.attentions, + cross_attentions=decoder_outputs.cross_attentions, + encoder_last_hidden_state=encoder_outputs.last_hidden_state, + encoder_hidden_states=encoder_outputs.hidden_states, + encoder_attentions=encoder_outputs.attentions, + ) + + +class MoonshineForConditionalGeneration(MoonshinePreTrainedModel, GenerationMixin): + _tied_weights_keys = ["proj_out.weight"] + + def __init__(self, config: MoonshineConfig): + super().__init__(config) + self.model = MoonshineModel(config) + self.proj_out = nn.Linear(config.hidden_size, config.vocab_size, bias=False) + + # Initialize weights and apply final processing + self.post_init() + + def get_encoder(self): + return self.model.get_encoder() + + def get_decoder(self): + return self.model.get_decoder() + + def get_output_embeddings(self): + return self.proj_out + + def set_output_embeddings(self, new_embeddings): + self.proj_out = new_embeddings + + def get_input_embeddings(self) -> nn.Module: + return self.model.get_input_embeddings() + + @add_start_docstrings_to_model_forward(MOONSHINE_INPUTS_DOCSTRING) + @replace_return_docstrings(output_type=Seq2SeqLMOutput, config_class=_CONFIG_FOR_DOC) + def forward( + self, + input_features: Optional[torch.FloatTensor] = None, + attention_mask: Optional[torch.LongTensor] = None, + decoder_input_ids: Optional[torch.LongTensor] = None, + decoder_attention_mask: Optional[torch.LongTensor] = None, + encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[EncoderDecoderCache, Tuple[torch.FloatTensor]]] = None, + decoder_inputs_embeds: Optional[Tuple[torch.FloatTensor]] = None, + decoder_position_ids: Optional[Tuple[torch.LongTensor]] = None, + labels: Optional[torch.LongTensor] = None, + use_cache: Optional[bool] = None, + 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], Seq2SeqLMOutput]: + r""" + labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): + Labels for computing the language modeling loss. Indices should either be in `[0, ..., config.vocab_size]` + or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored (masked), the loss is + only computed for the tokens with labels in `[0, ..., config.vocab_size]`. `sequence_length` should be smaller than or equal to `config.max_target_positions`. + + Returns: + + Example: + + ```python + >>> import torch + >>> from transformers import AutoProcessor, WhisperForConditionalGeneration + >>> from datasets import load_dataset + + >>> processor = AutoProcessor.from_pretrained("openai/whisper-tiny.en") + >>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny.en") + + >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") + + >>> inputs = processor(ds[0]["audio"]["array"], return_tensors="pt") + >>> input_features = inputs.input_features + + >>> generated_ids = model.generate(inputs=input_features) + + >>> transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] + >>> transcription + ' Mr. Quilter is the apostle of the middle classes, and we are glad to welcome his gospel.' + ```""" + return_dict = return_dict if return_dict is not None else self.config.use_return_dict + + if labels is not None: + if labels.shape[1] > self.max_target_positions: + raise ValueError( + f"Labels' sequence length {labels.shape[1]} cannot exceed the maximum allowed length of {self.max_target_positions} tokens." + ) + if decoder_input_ids is None and decoder_inputs_embeds is None: + decoder_input_ids = shift_tokens_right( + labels, self.config.pad_token_id, self.config.decoder_start_token_id + ) + + outputs = self.model( + input_features, + attention_mask=attention_mask, + decoder_input_ids=decoder_input_ids, + encoder_outputs=encoder_outputs, + decoder_attention_mask=decoder_attention_mask, + past_key_values=past_key_values, + decoder_inputs_embeds=decoder_inputs_embeds, + decoder_position_ids=decoder_position_ids, + use_cache=use_cache, + output_attentions=output_attentions, + output_hidden_states=output_hidden_states, + return_dict=return_dict, + cache_position=cache_position, + ) + lm_logits = self.proj_out(outputs[0]) + + loss = None + if labels is not None: + loss_fct = CrossEntropyLoss() + # move labels to correct device to enable PP + labels = labels.to(lm_logits.device) + loss = loss_fct(lm_logits.view(-1, self.config.vocab_size), labels.reshape(-1)) + + if not return_dict: + output = (lm_logits,) + outputs[1:] + return ((loss,) + output) if loss is not None else output + + return Seq2SeqLMOutput( + loss=loss, + logits=lm_logits, + past_key_values=outputs.past_key_values, + decoder_hidden_states=outputs.decoder_hidden_states, + decoder_attentions=outputs.decoder_attentions, + cross_attentions=outputs.cross_attentions, + encoder_last_hidden_state=outputs.encoder_last_hidden_state, + encoder_hidden_states=outputs.encoder_hidden_states, + encoder_attentions=outputs.encoder_attentions, + ) + + + + From 7a6935a641c9a3639e4c2e14660418198e4e6946 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Fri, 13 Dec 2024 14:47:05 +0100 Subject: [PATCH 08/39] fix attention with past_key_values --- .../models/moonshine/modular_moonshine.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index 88c27063da0cfd..29440447ba3eed 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -460,14 +460,14 @@ def forward( else: key_states = self.k_proj(current_states) value_states = self.v_proj(current_states) + key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) + value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) if self.qk_layernorm: query_states = self.q_layernorm(query_states) key_states = self.k_layernorm(key_states) query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) - key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) - value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) if not is_cross_attention: if position_embeddings is None: @@ -585,14 +585,14 @@ def forward( else: key_states = self.k_proj(current_states) value_states = self.v_proj(current_states) + key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) + value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) if self.qk_layernorm: query_states = self.q_layernorm(query_states) key_states = self.k_layernorm(key_states) query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) - key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) - value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) if not is_cross_attention: if position_embeddings is None: @@ -730,14 +730,14 @@ def forward( else: key_states = self.k_proj(current_states) value_states = self.v_proj(current_states) + key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) + value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) if self.qk_layernorm: query_states = self.q_layernorm(query_states) key_states = self.k_layernorm(key_states) query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) - key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) - value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) if not is_cross_attention: if position_embeddings is None: From 8fda4267f405f54f208c0a5f3fb1ac881373314a Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Fri, 13 Dec 2024 14:48:07 +0100 Subject: [PATCH 09/39] add MoonshineForConditionalGeneration --- .../models/moonshine/modular_moonshine.py | 174 +++++++++++------- 1 file changed, 110 insertions(+), 64 deletions(-) diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index 29440447ba3eed..be24c7837e8360 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -991,17 +991,11 @@ def _init_weights(self, module): module.weight.data[module.padding_idx].zero_() -class MoonshineEncoder(MoonshinePreTrainedModel): - """ - Transformer encoder consisting of *config.encoder_layers* self attention layers. Each layer is a - [`MoonshineEncoderLayer`]. - - Args: - config: MoonshineConfig - """ +class MoonshineEncoder(LlamaModel, MoonshinePreTrainedModel): + main_input_name = "input_features" def __init__(self, config: MoonshineConfig): - super().__init__(config) + MoonshinePreTrainedModel.__init__(self, config) self.config = config embed_dim = config.hidden_size @@ -1031,85 +1025,138 @@ def get_input_embeddings(self) -> nn.Module: def set_input_embeddings(self, value: nn.Module): self.conv1 = value - + + def preprocess(self, input_features: torch.FloatTensor): + input_features = input_features.unsqueeze(1) + inputs_embeds = nn.functional.tanh(self.conv1(input_features)) + inputs_embeds = self.groupnorm(inputs_embeds) + inputs_embeds = nn.functional.gelu(self.conv2(inputs_embeds)) + inputs_embeds = nn.functional.gelu(self.conv3(inputs_embeds)) + inputs_embeds = inputs_embeds.permute(0, 2, 1) + return inputs_embeds + def forward( self, - input_features, - attention_mask=None, - output_attentions=None, - output_hidden_states=None, - return_dict=None, - **kwargs, - ): - r""" - Args: - input_features (`torch.LongTensor` of shape `(batch_size, 1, sequence_length)`): - Float values of the raw speech waveform. Raw speech waveform can be - obtained by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a - `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). - attention_mask (`torch.Tensor`)`, *optional*): - attention mask of size `(batch_size, sequence_length)` if flash attention is used or `(batch_size, 1, - output_attentions (`bool`, *optional*): - Whether or not to return the attentions tensors of all attention layers. See `attentions` under - returned tensors for more detail. - output_hidden_states (`bool`, *optional*): - Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors - for more detail. - return_dict (`bool`, *optional*): - Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. - """ + input_features: Optional[torch.FloatTensor] = None, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = 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, + output_hidden_states: Optional[bool] = None, + return_dict: Optional[bool] = None, + cache_position: Optional[torch.LongTensor] = None, + **flash_attn_kwargs: Unpack[FlashAttentionKwargs], + ) -> Union[Tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) + 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 - inputs_embeds = nn.functional.tanh(self.conv1(input_features)) - inputs_embeds = self.groupnorm(inputs_embeds) - inputs_embeds = nn.functional.gelu(self.conv2(inputs_embeds)) - inputs_embeds = nn.functional.gelu(self.conv3(inputs_embeds)) - inputs_embeds = inputs_embeds.permute(0, 2, 1) + if (input_features is None) ^ (inputs_embeds is not None): + raise ValueError("You must specify exactly one of input_ids or inputs_embeds") + + 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.preprocess(input_features) + + # kept for BC (non `Cache` `past_key_values` inputs) + return_legacy_cache = False + if use_cache or past_key_values is not None: + if isinstance(past_key_values, Cache) and not isinstance(past_key_values, EncoderDecoderCache): + past_key_values = EncoderDecoderCache(past_key_values, DynamicCache()) + elif not isinstance(past_key_values, EncoderDecoderCache): + return_legacy_cache = True + logger.warning_once( + "Passing a tuple of `past_key_values` is deprecated and will be removed in Transformers v4.43.0. " + "You should pass an instance of `EncoderDecoderCache` instead, e.g. " + "`past_key_values=EncoderDecoderCache.from_legacy_cache(past_key_values)`." + ) + past_key_values = EncoderDecoderCache.from_legacy_cache(past_key_values) + + 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 + inputs_embeds.shape[1], device=inputs_embeds.device + ) + if position_ids is None: + position_ids = cache_position.unsqueeze(0) + + causal_mask = self._update_causal_mask( + attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions + ) hidden_states = inputs_embeds - - position_ids = torch.arange(inputs_embeds.shape[1], device=inputs_embeds.device).unsqueeze(0) - embed_pos = self.rotary_emb(hidden_states, position_ids) - encoder_states = () if output_hidden_states else None - all_attentions = () if output_attentions else None + # create position embeddings to be shared across the decoder layers + position_embeddings = self.rotary_emb(hidden_states, position_ids) + + # decoder layers + all_hidden_states = () if output_hidden_states else None + all_self_attns = () if output_attentions else None + next_decoder_cache = None - for encoder_layer in self.layers: + for decoder_layer in self.layers: if output_hidden_states: - encoder_states = encoder_states + (hidden_states,) - + all_hidden_states += (hidden_states,) + if self.gradient_checkpointing and self.training: layer_outputs = self._gradient_checkpointing_func( - encoder_layer.__call__, + decoder_layer.__call__, hidden_states, - attention_mask, + causal_mask, + position_ids, + past_key_values, output_attentions, - position_embeddings=embed_pos, + use_cache, + cache_position, + position_embeddings, ) else: - layer_outputs = encoder_layer( + layer_outputs = decoder_layer( hidden_states, - attention_mask, + attention_mask=causal_mask, + position_ids=position_ids, + past_key_value=past_key_values, output_attentions=output_attentions, - position_embeddings=embed_pos, + use_cache=use_cache, + cache_position=cache_position, + position_embeddings=position_embeddings, + **flash_attn_kwargs, ) hidden_states = layer_outputs[0] + if use_cache: + next_decoder_cache = layer_outputs[2 if output_attentions else 1] + if output_attentions: - all_attentions = all_attentions + (layer_outputs[1],) + all_self_attns += (layer_outputs[1],) hidden_states = self.layer_norm(hidden_states) + + # add hidden states from the last decoder layer if output_hidden_states: - encoder_states = encoder_states + (hidden_states,) + 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() if not return_dict: - return tuple(v for v in [hidden_states, encoder_states, all_attentions] if v is not None) - return BaseModelOutput( - last_hidden_state=hidden_states, hidden_states=encoder_states, attentions=all_attentions + return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None) + return BaseModelOutputWithPast( + last_hidden_state=hidden_states, + past_key_values=next_cache, + hidden_states=all_hidden_states, + attentions=all_self_attns, ) @@ -1461,6 +1508,10 @@ def set_output_embeddings(self, new_embeddings): def get_input_embeddings(self) -> nn.Module: return self.model.get_input_embeddings() + @property + def encoder(self): + return self.get_encoder() + @add_start_docstrings_to_model_forward(MOONSHINE_INPUTS_DOCSTRING) @replace_return_docstrings(output_type=Seq2SeqLMOutput, config_class=_CONFIG_FOR_DOC) def forward( @@ -1559,9 +1610,4 @@ def forward( encoder_last_hidden_state=outputs.encoder_last_hidden_state, encoder_hidden_states=outputs.encoder_hidden_states, encoder_attentions=outputs.encoder_attentions, - ) - - - - - + ) \ No newline at end of file From d0ed91720f46ef9d5b47e6110dddb2a3e71482b7 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Fri, 13 Dec 2024 18:42:07 +0100 Subject: [PATCH 10/39] fix cache handling and causality for cross attention --- .../moonshine/configuration_moonshine.py | 236 ++ .../moonshine/convert_usefulsensors_to_hf.py | 145 ++ .../models/moonshine/modeling_moonshine.py | 2078 +++++++++++++++++ .../models/moonshine/modular_moonshine.py | 26 +- 4 files changed, 2469 insertions(+), 16 deletions(-) create mode 100644 src/transformers/models/moonshine/configuration_moonshine.py create mode 100644 src/transformers/models/moonshine/convert_usefulsensors_to_hf.py create mode 100644 src/transformers/models/moonshine/modeling_moonshine.py diff --git a/src/transformers/models/moonshine/configuration_moonshine.py b/src/transformers/models/moonshine/configuration_moonshine.py new file mode 100644 index 00000000000000..44f043d6d3c3dd --- /dev/null +++ b/src/transformers/models/moonshine/configuration_moonshine.py @@ -0,0 +1,236 @@ +# 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 +# This file was automatically generated from src/transformers/models/moonshine/modular_moonshine.py. +# Do NOT edit this file manually as any edits will be overwritten by the generation of +# the file from the modular. If any change should be done, please apply the change to the +# modular_moonshine.py file directly. One of our CI enforces this. +# 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 + +from ...configuration_utils import PretrainedConfig + + +class MoonshineConfig(PretrainedConfig): + r""" + This is the configuration class to store the configuration of a [`MoonshineModel`]. It is used to instantiate a Moonshine + model according to the specified arguments, defining the model architecture. Instantiating a configuration with the + defaults will yield a similar configuration to that of the Moonshine + [UsefulSensors/moonshine](https://huggingface.co/UsefulSensors/moonshine). + + Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the + documentation from [`PretrainedConfig`] for more information. + + Args: + vocab_size (`int`, *optional*, defaults to 32768): + Vocabulary size of the Moonshine model. Defines the number of different tokens that can be represented by the + `inputs_ids` passed when calling [`MoonshineModel`]. + hidden_size (`int`, *optional*, defaults to 288): + Dimension of the hidden representations. + intermediate_size (`int`, *optional*): + Dimension of the MLP representations. + num_hidden_layers (`int`, *optional*, defaults to 6): + Number of hidden layers in the Transformer encoder and decoder. + num_attention_heads (`int`, *optional*, defaults to 8): + Number of attention heads for each attention layer in the Transformer encoder and decoder. + num_key_value_heads (`int`, *optional*): + This is the number of key_value heads that should be used to implement Grouped Query Attention. If + `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if + `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When + converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed + by meanpooling all the original heads within that group. For more details checkout [this + paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to + `num_attention_heads`. + encoder_hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`): + The non-linear activation function (function or string) in the encoder. + decoder_hidden_act (`str` or `function`, *optional*, defaults to `"silu"`): + The non-linear activation function (function or string) in the decoder. + max_position_embeddings (`int`, *optional*, defaults to 2048): + The maximum sequence length that this model might ever be used with. TODO: check this + initializer_range (`float`, *optional*, defaults to 0.02): + The standard deviation of the truncated_normal_initializer for initializing all weight matrices. + layer_norm_eps (`float`, *optional*, defaults to 1e-5): + The epsilon used by the layer normalization layers. + decoder_start_token_id (`int`, *optional*, defaults to 1): + Corresponds to the "<|startoftranscript|>" token, which is automatically used when no `decoder_input_ids` + are provided to the `generate` function. It is used to guide the model`s generation process depending on + the task. + use_cache (`bool`, *optional*, defaults to `True`): + Whether or not the model should return the last key/values attentions (not used by all models). + is_encoder_decoder (`bool`, *optional*, defaults to `True`): + Whether the model is used as an encoder/decoder or not. + rope_theta (`float`, *optional*, defaults to 10000.0): + The base period of the RoPE embeddings. TODO: check this + partial_rotary_factor (`float`, *optional*, defaults to 0.5): + Percentage of the query and keys which will have rotary embedding. TODO: check this + ff_mult (`int`, *optional*, defaults to 4): + Factor by which to scale the intermediate size. + attention_bias (`bool`, *optional*, defaults to `False`): + Whether to use a bias in the query, key, value and output projection layers during self-attention. + attention_dropout (`float`, *optional*, defaults to 0.0): + The dropout ratio for the attention probabilities. + qk_layernorm (`bool`, *optional*, defaults to `False`): + Whether or not to normalize the Queries and Keys after projecting the hidden states. + rope_scaling (`Dict`, *optional*): + Dictionary containing the scaling configuration for the RoPE embeddings. NOTE: if you apply new rope type + and you expect the model to work on longer `max_position_embeddings`, we recommend you to update this value + accordingly. + Expected contents: + `rope_type` (`str`): + The sub-variant of RoPE to use. Can be one of ['default', 'linear', 'dynamic', 'yarn', 'longrope', + 'llama3'], with 'default' being the original RoPE implementation. + `factor` (`float`, *optional*): + Used with all rope types except 'default'. The scaling factor to apply to the RoPE embeddings. In + most scaling types, a `factor` of x will enable the model to handle sequences of length x * + original maximum pre-trained length. + `original_max_position_embeddings` (`int`, *optional*): + Used with 'dynamic', 'longrope' and 'llama3'. The original max position embeddings used during + pretraining. + `attention_factor` (`float`, *optional*): + Used with 'yarn' and 'longrope'. The scaling factor to be applied on the attention + computation. If unspecified, it defaults to value recommended by the implementation, using the + `factor` field to infer the suggested value. + `beta_fast` (`float`, *optional*): + Only used with 'yarn'. Parameter to set the boundary for extrapolation (only) in the linear + ramp function. If unspecified, it defaults to 32. + `beta_slow` (`float`, *optional*): + Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear + ramp function. If unspecified, it defaults to 1. + `short_factor` (`List[float]`, *optional*): + Only used with 'longrope'. The scaling factor to be applied to short contexts (< + `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden + size divided by the number of attention heads divided by 2 + `long_factor` (`List[float]`, *optional*): + Only used with 'longrope'. The scaling factor to be applied to long contexts (< + `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden + size divided by the number of attention heads divided by 2 + `low_freq_factor` (`float`, *optional*): + Only used with 'llama3'. Scaling factor applied to low frequency components of the RoPE + `high_freq_factor` (`float`, *optional*): + Only used with 'llama3'. Scaling factor applied to high frequency components of the RoPE + bos_token_id (`int`, *optional*, defaults to 1): + Denotes beginning of sequences token id. + eos_token_id (`int`, *optional*, defaults to 2): + Denotes end of sequences token id. + apply_spec_augment (`bool`, *optional*, defaults to `False`): + Whether to apply *SpecAugment* data augmentation to the outputs of the feature encoder. For reference see + [SpecAugment: A Simple Data Augmentation Method for Automatic Speech + Recognition](https://arxiv.org/abs/1904.08779). + mask_time_prob (`float`, *optional*, defaults to 0.05): + Percentage (between 0 and 1) of all feature vectors along the time axis which will be masked. The masking + procecure generates `mask_time_prob*len(time_axis)/mask_time_length` independent masks over the axis. If + reasoning from the propability of each feature vector to be chosen as the start of the vector span to be + masked, *mask_time_prob* should be `prob_vector_start*mask_time_length`. Note that overlap may decrease the + actual percentage of masked vectors. This is only relevant if `apply_spec_augment == True`. + mask_time_length (`int`, *optional*, defaults to 10): + Length of vector span along the time axis. + mask_time_min_masks (`int`, *optional*, defaults to 2),: + The minimum number of masks of length `mask_feature_length` generated along the time axis, each time step, + irrespectively of `mask_feature_prob`. Only relevant if ''mask_time_prob*len(time_axis)/mask_time_length < + mask_time_min_masks'' + mask_feature_prob (`float`, *optional*, defaults to 0.0): + Percentage (between 0 and 1) of all feature vectors along the feature axis which will be masked. The + masking procecure generates `mask_feature_prob*len(feature_axis)/mask_time_length` independent masks over + the axis. If reasoning from the propability of each feature vector to be chosen as the start of the vector + span to be masked, *mask_feature_prob* should be `prob_vector_start*mask_feature_length`. Note that overlap + may decrease the actual percentage of masked vectors. This is only relevant if `apply_spec_augment is + True`. + mask_feature_length (`int`, *optional*, defaults to 10): + Length of vector span along the feature axis. + mask_feature_min_masks (`int`, *optional*, defaults to 0),: + The minimum number of masks of length `mask_feature_length` generated along the feature axis, each time + step, irrespectively of `mask_feature_prob`. Only relevant if + `mask_feature_prob*len(feature_axis)/mask_feature_length < mask_feature_min_masks`. + + Example: + + ```python + >>> from transformers import MoonshineModel, MoonshineConfig + + >>> # Initializing a Moonshine style configuration + >>> configuration = MoonshineConfig().from_pretrained("UsefulSensors/moonshine") + + >>> # Initializing a model from the configuration + >>> model = MoonshineModel(configuration) + + >>> # Accessing the model configuration + >>> configuration = model.config + ```""" + + model_type = "moonshine" + keys_to_ignore_at_inference = ["past_key_values"] + + def __init__( + self, + vocab_size=32768, + hidden_size=288, + intermediate_size=None, + num_hidden_layers=6, + num_attention_heads=8, + num_key_value_heads=None, + encoder_hidden_act="gelu", + decoder_hidden_act="silu", + max_position_embeddings=2048, + initializer_range=0.02, + layer_norm_eps=1e-5, + decoder_start_token_id=1, + use_cache=True, + is_encoder_decoder=True, + rope_theta=10000.0, + partial_rotary_factor=0.5, + attention_bias=False, + attention_dropout=0.0, + qk_layernorm=False, + rope_scaling=None, + ff_mult=4, + bos_token_id=1, + eos_token_id=2, + apply_spec_augment=False, + mask_time_prob=0.05, + mask_time_length=10, + mask_time_min_masks=2, + mask_feature_prob=0.0, + mask_feature_length=10, + mask_feature_min_masks=0, + **kwargs, + ): + self.vocab_size = vocab_size + self.hidden_size = hidden_size + self.intermediate_size = intermediate_size + self.num_hidden_layers = num_hidden_layers + self.num_attention_heads = num_attention_heads + + if num_key_value_heads is None: + num_key_value_heads = num_attention_heads + + self.num_key_value_heads = num_key_value_heads + self.encoder_hidden_act = encoder_hidden_act + self.decoder_hidden_act = decoder_hidden_act + self.max_position_embeddings = max_position_embeddings + self.initializer_range = initializer_range + self.layer_norm_eps = layer_norm_eps + self.decoder_start_token_id = decoder_start_token_id + self.use_cache = use_cache + self.is_encoder_decoder = is_encoder_decoder + self.rope_theta = rope_theta + self.partial_rotary_factor = partial_rotary_factor + + self.attention_bias = attention_bias + self.attention_dropout = attention_dropout + self.qk_layernorm = qk_layernorm + self.rope_scaling = rope_scaling + self.ff_mult = ff_mult + + # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 + self.apply_spec_augment = apply_spec_augment + self.mask_time_prob = mask_time_prob + self.mask_time_length = mask_time_length + self.mask_time_min_masks = mask_time_min_masks + self.mask_feature_prob = mask_feature_prob + self.mask_feature_length = mask_feature_length + self.mask_feature_min_masks = mask_feature_min_masks + + super().__init__( + bos_token_id=bos_token_id, + eos_token_id=eos_token_id, + is_encoder_decoder=is_encoder_decoder, + decoder_start_token_id=decoder_start_token_id, + **kwargs, + ) diff --git a/src/transformers/models/moonshine/convert_usefulsensors_to_hf.py b/src/transformers/models/moonshine/convert_usefulsensors_to_hf.py new file mode 100644 index 00000000000000..cf0010fd552ddd --- /dev/null +++ b/src/transformers/models/moonshine/convert_usefulsensors_to_hf.py @@ -0,0 +1,145 @@ +#!/usr/bin/env python +"""Converts a Moonshine model in Useful Sensors format to Hugging Face format.""" +# Copyright 2022 The HuggingFace Inc. team and the OpenAI team. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import argparse +from huggingface_hub import hf_hub_download + +import h5py +import torch +import numpy as np +import re + +from transformers.models.moonshine.modeling_moonshine import MoonshineConfig +from transformers.models.moonshine.modeling_moonshine import MoonshineDecoder + + +# Copied from https://github.com/usefulsensors/moonshine/blob/a1d77cc573b0471ac4602b86f67b3f48d67df1a9/moonshine/model.py +def _get_weights(model_name): + repo = "UsefulSensors/moonshine" + + return ( + hf_hub_download(repo, f"{x}.weights.h5", subfolder=model_name) + for x in ("preprocessor", "encoder", "decoder") + ) + + +def _read_h5_weights(group, current_key="", weights={}): + for key in group.keys(): + full_key = f"{current_key}.{key}" if current_key else key + if isinstance(group[key], h5py.Dataset): + w = np.array(group[key]) + w = torch.from_numpy(w) + if len(w.shape) > 1: + if len(w.shape) == 3: + hidden_size = max(list(w.shape)) + try: + w = w.reshape(hidden_size, hidden_size) + except RuntimeError: + # meaning its a conv layers + pass + w = w.transpose(0, -1) + weights[full_key] = w + else: + _read_h5_weights(group[key], full_key, weights) + return weights + + +def _convert_layer_names(name, gated_mlp=False): + name = re.sub(r'layers\.functional(?:_(\d+))?\.layers', lambda m: f'layers.{m.group(1) if m.group(1) else "0"}', name, count=1) + if gated_mlp: + name = re.sub(r"functional\.layers\.dense\.", "mlp.up_proj.", name) + name = re.sub(r"functional\.layers\.dense_1\.", "mlp.down_proj.", name) + else: + name = re.sub(r"functional\.layers\.sequential\.layers\.dense\.", "mlp.fc1.", name) + name = re.sub(r"functional\.layers\.sequential\.layers\.dense_1\.", "mlp.fc2.", name) + name = re.sub(r"layers\.sequential\.layers\.conv1d\.", "conv1.", name) + name = re.sub(r"layers\.sequential\.layers\.conv1d_1\.", "conv2.", name) + name = re.sub(r"layers\.sequential\.layers\.conv1d_2\.", "conv3.", name) + name = re.sub(r"layers\.sequential\.layers\.group_normalization\.", "groupnorm.", name) + name = re.sub(r"mha_with_rope\.key_dense", "self_attn.k_proj", name) + name = re.sub(r"mha_with_rope\.query_dense", "self_attn.q_proj", name) + name = re.sub(r"mha_with_rope\.value_dense", "self_attn.v_proj", name) + name = re.sub(r"mha_with_rope\.output_dense", "self_attn.dense", name) + name = re.sub(r"mha_precomputed_kv\.key_dense", "encoder_attn.k_proj", name) + name = re.sub(r"mha_precomputed_kv\.query_dense", "encoder_attn.q_proj", name) + name = re.sub(r"mha_precomputed_kv\.value_dense", "encoder_attn.v_proj", name) + name = re.sub(r"mha_precomputed_kv\.output_dense", "encoder_attn.dense", name) + name = re.sub(r"mha_causal_with_rope\.key_dense", "self_attn.k_proj", name) + name = re.sub(r"mha_causal_with_rope\.query_dense", "self_attn.q_proj", name) + name = re.sub(r"mha_causal_with_rope\.value_dense", "self_attn.v_proj", name) + name = re.sub(r"mha_causal_with_rope\.output_dense", "self_attn.dense", name) + name = re.sub(r"layer_normalization\.", "input_layernorm.", name) + name = re.sub(r"layer_normalization_1\.", "post_attention_layernorm.", name) + name = re.sub(r"layer_normalization_2\.", "final_layernorm.", name) + name = re.sub(r"vars\.0", "weight", name) + name = re.sub(r"vars\.1", "bias", name) + name = re.sub(r"layers\.reversible_embedding", "embed_tokens", name) + + return name + + +def _convert_weights(weights, encoder=True): + if "layers.rotary_embedding.vars.0" in weights: + weights.pop("layers.rotary_embedding.vars.0") + + converted_weights = {} + if encoder: + converted_weights["layer_norm.weight"] = weights.pop("layers.layer_normalization.vars.0") + else: + converted_weights["norm.weight"] = weights.pop("layers.layer_normalization.vars.0") + + for name, w in weights.items(): + if encoder: + new_name = _convert_layer_names(name) + else: + new_name = _convert_layer_names(name, gated_mlp=True) + converted_weights[new_name] = w + + return converted_weights + + +def convert_usefulsensors_moonshine_to_hf(model_name, pytorch_dump_folder_path): + preprocessor_weights_path, encoder_weights_path, decoder_weights_path = _get_weights(model_name) + + with h5py.File(preprocessor_weights_path, 'r') as f: + loaded_preprocessor_weights = _read_h5_weights(f, weights={}) + + with h5py.File(encoder_weights_path, 'r') as f: + loaded_encoder_weights = _read_h5_weights(f, weights={}) + + with h5py.File(decoder_weights_path, 'r') as f: + loaded_decoder_weights = _read_h5_weights(f, weights={}) + + encoder_state_dict = {**loaded_encoder_weights, **loaded_preprocessor_weights} + encoder_state_dict = _convert_weights(encoder_state_dict) + + converted_decoder_weights = _convert_weights(loaded_decoder_weights, encoder=False) + converted_decoder_weights['embed_tokens.weight'] = converted_decoder_weights['embed_tokens.weight'].T + + + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + # # Required parameters + parser.add_argument("--model_name", type=str, help="Path to the downloaded checkpoints") + parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") + args = parser.parse_args() + + convert_usefulsensors_moonshine_to_hf( + args.model_name, args.pytorch_dump_folder_path + ) + diff --git a/src/transformers/models/moonshine/modeling_moonshine.py b/src/transformers/models/moonshine/modeling_moonshine.py new file mode 100644 index 00000000000000..9f46520f40d86d --- /dev/null +++ b/src/transformers/models/moonshine/modeling_moonshine.py @@ -0,0 +1,2078 @@ +# 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 +# This file was automatically generated from src/transformers/models/moonshine/modular_moonshine.py. +# Do NOT edit this file manually as any edits will be overwritten by the generation of +# the file from the modular. If any change should be done, please apply the change to the +# modular_moonshine.py file directly. One of our CI enforces this. +# 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 + +import copy +import math +from typing import List, Optional, Tuple, Union + +import numpy as np +import torch +import torch.nn as nn +from packaging import version +from torch.nn import CrossEntropyLoss + +from ...activations import ACT2FN +from ...cache_utils import Cache, DynamicCache, EncoderDecoderCache, StaticCache +from ...generation import GenerationMixin +from ...modeling_attn_mask_utils import AttentionMaskConverter +from ...modeling_flash_attention_utils import FlashAttentionKwargs, _flash_attention_forward +from ...modeling_outputs import ( + BaseModelOutput, + BaseModelOutputWithPast, + BaseModelOutputWithPastAndCrossAttentions, + Seq2SeqLMOutput, + Seq2SeqModelOutput, +) +from ...modeling_rope_utils import ROPE_INIT_FUNCTIONS +from ...modeling_utils import PreTrainedModel +from ...processing_utils import Unpack +from ...utils import ( + add_start_docstrings, + add_start_docstrings_to_model_forward, + get_torch_version, + is_flash_attn_2_available, + is_flash_attn_greater_or_equal_2_10, + logging, + replace_return_docstrings, +) +from .configuration_moonshine import MoonshineConfig + + +if is_flash_attn_2_available(): + from ...modeling_flash_attention_utils import _flash_attention_forward + + +logger = logging.get_logger(__name__) + +_CONFIG_FOR_DOC = "MoonshineConfig" + + +class MoonshineRotaryEmbedding(nn.Module): + def __init__( + self, + dim=None, + max_position_embeddings=2048, + base=10000, + device=None, + scaling_factor=1.0, + rope_type="default", + config: Optional[MoonshineConfig] = None, + ): + super().__init__() + # TODO (joao): remove the `if` below, only used for BC + self.rope_kwargs = {} + if config is None: + logger.warning_once( + "`MoonshineRotaryEmbedding` can now be fully parameterized by passing the model config through the " + "`config` argument. All other arguments will be removed in v4.46" + ) + self.rope_kwargs = { + "rope_type": rope_type, + "factor": scaling_factor, + "dim": dim, + "base": base, + "max_position_embeddings": max_position_embeddings, + } + self.rope_type = rope_type + self.max_seq_len_cached = max_position_embeddings + self.original_max_seq_len = max_position_embeddings + else: + # BC: "rope_type" was originally "type" + if config.rope_scaling is not None: + self.rope_type = config.rope_scaling.get("rope_type", config.rope_scaling.get("type")) + else: + self.rope_type = "default" + self.max_seq_len_cached = config.max_position_embeddings + self.original_max_seq_len = config.max_position_embeddings + + self.config = config + self.rope_init_fn = ROPE_INIT_FUNCTIONS[self.rope_type] + + inv_freq, self.attention_scaling = self.rope_init_fn(self.config, device, **self.rope_kwargs) + self.register_buffer("inv_freq", inv_freq, persistent=False) + self.original_inv_freq = self.inv_freq + + def _dynamic_frequency_update(self, position_ids, device): + """ + dynamic RoPE layers should recompute `inv_freq` in the following situations: + 1 - growing beyond the cached sequence length (allow scaling) + 2 - the current sequence length is in the original scale (avoid losing precision with small sequences) + """ + seq_len = torch.max(position_ids) + 1 + if seq_len > self.max_seq_len_cached: # growth + inv_freq, self.attention_scaling = self.rope_init_fn( + self.config, device, seq_len=seq_len, **self.rope_kwargs + ) + self.register_buffer("inv_freq", inv_freq, persistent=False) # TODO joao: may break with compilation + self.max_seq_len_cached = seq_len + + if seq_len < self.original_max_seq_len and self.max_seq_len_cached > self.original_max_seq_len: # reset + self.register_buffer("inv_freq", self.original_inv_freq, persistent=False) + self.max_seq_len_cached = self.original_max_seq_len + + @torch.no_grad() + def forward(self, x, position_ids): + if "dynamic" in self.rope_type: + self._dynamic_frequency_update(position_ids, device=x.device) + + # Core RoPE block + inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1) + position_ids_expanded = position_ids[:, None, :].float() + # Force float32 (see https://github.com/huggingface/transformers/pull/29285) + device_type = x.device.type + device_type = device_type if isinstance(device_type, str) and device_type != "mps" else "cpu" + with torch.autocast(device_type=device_type, enabled=False): + freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2) + emb = torch.stack((freqs, freqs), dim=-1) + emb = emb.flatten(-2) # in einsum notation: rearrange(x, '... d j -> ... (d j)') + cos = emb.cos() + sin = emb.sin() + + # Advanced RoPE types (e.g. yarn) apply a post-processing scaling factor, equivalent to scaling attention + cos = cos * self.attention_scaling + sin = sin * self.attention_scaling + + return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype) + + +class MoonshineNonGatedMLP(nn.Module): + def __init__(self, config: MoonshineConfig, hidden_act: str): + super().__init__() + config = copy.deepcopy(config) + config.hidden_act = hidden_act + if config.intermediate_size is None: + config.intermediate_size = config.hidden_size * config.ff_mult + self.config = config + self.activation_fn = ACT2FN[config.hidden_act] + self.fc1 = nn.Linear(config.hidden_size, config.intermediate_size) + self.fc2 = nn.Linear(config.intermediate_size, config.hidden_size) + + def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: + hidden_states = self.fc1(hidden_states) + hidden_states = self.activation_fn(hidden_states) + hidden_states = self.fc2(hidden_states) + return hidden_states + + +class MoonshineGatedMLP(nn.Module): + def __init__(self, config: MoonshineConfig, hidden_act: str): + super().__init__() + config = copy.deepcopy(config) + config.hidden_act = hidden_act + if config.intermediate_size is None: + config.intermediate_size = config.hidden_size * config.ff_mult * 2 + self.hidden_size = config.hidden_size + self.intermediate_size = config.intermediate_size + self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=True) + self.down_proj = nn.Linear(self.intermediate_size // 2, self.hidden_size, bias=True) + self.act_fn = ACT2FN[config.hidden_act] + + def forward(self, hidden_state): + hidden_state = self.up_proj(hidden_state) + hidden_state, gate = hidden_state.chunk(2, dim=-1) + hidden_state = self.act_fn(gate) * hidden_state + return self.down_proj(hidden_state) + + +class MoonshineMLP: + def __new__(cls, config: MoonshineConfig, hidden_act: str): + if hidden_act == "gelu": + return MoonshineNonGatedMLP(config, hidden_act) + elif hidden_act == "silu": + return MoonshineGatedMLP(config, hidden_act) + else: + raise ValueError(f"Unsupported activation function: {hidden_act}, please use 'gelu' or 'silu'") + + +def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor: + """ + This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch, + num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim) + """ + batch, num_key_value_heads, slen, head_dim = hidden_states.shape + if n_rep == 1: + return hidden_states + hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim) + return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim) + + +def rotate_every_two(x: torch.Tensor) -> torch.Tensor: + x1 = x[:, :, :, ::2] + x2 = x[:, :, :, 1::2] + x = torch.stack((-x2, x1), dim=-1) + return x.flatten(-2) # in einsum notation: rearrange(x, '... d j -> ... (d j)') + + +def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1): + """Applies Rotary Position Embedding to the query and key tensors. + + Args: + q (`torch.Tensor`): The query tensor. + k (`torch.Tensor`): The key tensor. + cos (`torch.Tensor`): The cosine part of the rotary embedding. + sin (`torch.Tensor`): The sine part of the rotary embedding. + position_ids (`torch.Tensor`, *optional*): + Deprecated and unused. + unsqueeze_dim (`int`, *optional*, defaults to 1): + The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and + sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note + that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and + k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes + cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have + the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2. + Returns: + `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding. + """ + cos = cos.unsqueeze(unsqueeze_dim) + sin = sin.unsqueeze(unsqueeze_dim) + + q_embed = (q * cos) + (rotate_every_two(q) * sin) + k_embed = (k * cos) + (rotate_every_two(k) * sin) + return q_embed, k_embed + + +class MoonshineAttention(nn.Module): + """Multi-headed attention from 'Attention Is All You Need' paper""" + + def __init__(self, config: MoonshineConfig, layer_idx: Optional[int] = None, is_causal: bool = False): + super().__init__() + self.config = config + 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.attention_dropout = config.attention_dropout + self.hidden_size = config.hidden_size + self.num_heads = config.num_attention_heads + self.head_dim = self.hidden_size // self.num_heads + self.num_key_value_heads = config.num_key_value_heads + self.num_key_value_groups = self.num_heads // self.num_key_value_heads + self.rope_theta = config.rope_theta + self.rotary_ndims = max(config.hidden_size // config.num_attention_heads // 2, 32) + + self.is_causal = is_causal + + if (self.head_dim * self.num_heads) != self.hidden_size: + raise ValueError( + f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}" + f" and `num_heads`: {self.num_heads})." + ) + self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=config.attention_bias) + self.k_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config.attention_bias) + self.v_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config.attention_bias) + self.dense = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=config.attention_bias) + + self.qk_layernorm = config.qk_layernorm + if self.qk_layernorm: + self.q_layernorm = nn.LayerNorm( + config.hidden_size // self.num_heads, eps=config.layer_norm_eps, elementwise_affine=True + ) + self.k_layernorm = nn.LayerNorm( + config.hidden_size // self.num_heads, eps=config.layer_norm_eps, elementwise_affine=True + ) + + self.rotary_emb = MoonshineRotaryEmbedding( + dim=self.rotary_ndims, + max_position_embeddings=config.max_position_embeddings, + ) + + def forward( + self, + hidden_states: torch.Tensor, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + past_key_value: Optional[Cache] = None, + key_value_states: Optional[torch.Tensor] = None, + output_attentions: bool = False, + use_cache: bool = False, + cache_position: Optional[torch.LongTensor] = None, + position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 + ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + bsz, q_len, _ = hidden_states.size() + + query_states = self.q_proj(hidden_states) + + # if key_value_states are provided this layer is used as a cross-attention layer + # for the decoder + is_cross_attention = key_value_states is not None + + if past_key_value is not None: + is_updated = past_key_value.is_updated.get(self.layer_idx) + if is_cross_attention: + # after the first generated id, we can subsequently re-use all key/value_states from cache + past_key_value.is_updated[self.layer_idx] = True + past_key_value = past_key_value.cross_attention_cache + else: + past_key_value = past_key_value.self_attention_cache + + # use key_value_states if cross attention + current_states = key_value_states if key_value_states is not None else hidden_states + if is_cross_attention and past_key_value and is_updated: + # reuse k,v, cross_attentions + key_states = past_key_value.key_cache[self.layer_idx] + value_states = past_key_value.value_cache[self.layer_idx] + else: + key_states = self.k_proj(current_states) + value_states = self.v_proj(current_states) + key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) + value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) + if is_cross_attention and past_key_value is not None: + key_states, value_states = past_key_value.update( + key_states, value_states, self.layer_idx, {"cache_position": cache_position} + ) + + if self.qk_layernorm: + query_states = self.q_layernorm(query_states) + key_states = self.k_layernorm(key_states) + + query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) + + if not is_cross_attention: + if position_embeddings is None: + logger.warning_once( + "The attention layers in this model are transitioning from computing the RoPE embeddings internally " + "through `position_ids` (2D tensor with the indexes of the tokens), to using externally computed " + "`position_embeddings` (Tuple of tensors, containing cos and sin). In v4.46 `position_ids` will be " + "removed and `position_embeddings` will be mandatory." + ) + cos, sin = self.rotary_emb(value_states, position_ids) + else: + cos, sin = position_embeddings + + # Partial rotary embedding + query_rot, query_pass = ( + query_states[..., : self.rotary_ndims], + query_states[..., self.rotary_ndims :], + ) + key_rot, key_pass = ( + key_states[..., : self.rotary_ndims], + key_states[..., self.rotary_ndims :], + ) + # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor] + query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin) + + # [batch_size, seq_length, num_heads, head_dim] + query_states = torch.cat((query_rot, query_pass), dim=-1) + key_states = torch.cat((key_rot, key_pass), dim=-1) + + if past_key_value is not None: + if not is_cross_attention: + cache_kwargs = { + "sin": sin, + "cos": cos, + "partial_rotation_size": self.rotary_ndims, + "cache_position": cache_position, + } + key_states, value_states = past_key_value.update( + key_states, value_states, self.layer_idx, cache_kwargs + ) + + key_states = repeat_kv(key_states, self.num_key_value_groups) + value_states = repeat_kv(value_states, self.num_key_value_groups) + + # Queries and keys upcast to fp32 is required by Moonshine-2 to avoid overflow + attn_weights = torch.matmul( + query_states.to(torch.float32), key_states.to(torch.float32).transpose(2, 3) + ) / math.sqrt(self.head_dim) + + if attention_mask is not None: + causal_mask = attention_mask[:, :, :, : key_states.shape[-2]] + attn_weights += causal_mask + + # upcast attention to fp32 + attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(value_states.dtype) + attn_weights = nn.functional.dropout(attn_weights, p=self.attention_dropout, training=self.training) + + attn_output = torch.matmul(attn_weights, value_states) + + if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim): + raise ValueError( + f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is" + f" {attn_output.size()}" + ) + + attn_output = attn_output.transpose(1, 2).contiguous() + attn_output = attn_output.reshape(bsz, q_len, self.hidden_size) + + attn_output = self.dense(attn_output) + + if not output_attentions: + attn_weights = None + + return attn_output, attn_weights, past_key_value + + +class MoonshineFlashAttention2(MoonshineAttention): + """ + Moonshine flash attention module. This module inherits from `MoonshineAttention` as the weights of the module stays + untouched. The only required change would be on the forward pass where it needs to correctly call the public API of + flash attention and deal with padding tokens in case the input contains any of them. + """ + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + # TODO: Should be removed once Flash Attention for RoCm is bumped to 2.1. + # flash_attn<2.1 generates top-left aligned causal mask, while what is needed here is bottom-right alignement, that was made default for flash_attn>=2.1. This attribute is used to handle this difference. Reference: https://github.com/Dao-AILab/flash-attention/releases/tag/v2.1.0. + # 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 forward( + self, + hidden_states: torch.Tensor, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + past_key_value: Optional[Cache] = None, + key_value_states: Optional[torch.Tensor] = None, + output_attentions: bool = False, + use_cache: bool = False, + cache_position: Optional[torch.LongTensor] = None, + position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 + ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + bsz, q_len, _ = hidden_states.size() + + query_states = self.q_proj(hidden_states) + + # if key_value_states are provided this layer is used as a cross-attention layer + # for the decoder + is_cross_attention = key_value_states is not None + + if past_key_value is not None: + is_updated = past_key_value.is_updated.get(self.layer_idx) + if is_cross_attention: + # after the first generated id, we can subsequently re-use all key/value_states from cache + past_key_value.is_updated[self.layer_idx] = True + past_key_value = past_key_value.cross_attention_cache + else: + past_key_value = past_key_value.self_attention_cache + + # use key_value_states if cross attention + current_states = key_value_states if key_value_states is not None else hidden_states + if is_cross_attention and past_key_value and is_updated: + # reuse k,v, cross_attentions + key_states = past_key_value.key_cache[self.layer_idx] + value_states = past_key_value.value_cache[self.layer_idx] + else: + key_states = self.k_proj(current_states) + value_states = self.v_proj(current_states) + key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) + value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) + if is_cross_attention and past_key_value is not None: + key_states, value_states = past_key_value.update( + key_states, value_states, self.layer_idx, {"cache_position": cache_position} + ) + + if self.qk_layernorm: + query_states = self.q_layernorm(query_states) + key_states = self.k_layernorm(key_states) + + query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) + + if not is_cross_attention: + if position_embeddings is None: + logger.warning_once( + "The attention layers in this model are transitioning from computing the RoPE embeddings internally " + "through `position_ids` (2D tensor with the indexes of the tokens), to using externally computed " + "`position_embeddings` (Tuple of tensors, containing cos and sin). In v4.46 `position_ids` will be " + "removed and `position_embeddings` will be mandatory." + ) + cos, sin = self.rotary_emb(value_states, position_ids) + else: + cos, sin = position_embeddings + + # Partial rotary embedding + query_rot, query_pass = ( + query_states[..., : self.rotary_ndims], + query_states[..., self.rotary_ndims :], + ) + key_rot, key_pass = ( + key_states[..., : self.rotary_ndims], + key_states[..., self.rotary_ndims :], + ) + # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor] + query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin) + + # [batch_size, seq_length, num_heads, head_dim] + query_states = torch.cat((query_rot, query_pass), dim=-1) + key_states = torch.cat((key_rot, key_pass), dim=-1) + + if past_key_value is not None: + if not is_cross_attention: + cache_kwargs = { + "sin": sin, + "cos": cos, + "partial_rotation_size": self.rotary_ndims, + "cache_position": cache_position, + } + key_states, value_states = past_key_value.update( + key_states, value_states, 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. + query_states = query_states.transpose(1, 2) + key_states = key_states.transpose(1, 2) + value_states = value_states.transpose(1, 2) + + attn_dropout = self.attention_dropout if self.training else 0.0 + + # 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. + # This might slowdown training & inference so it is recommended to not cast the LayerNorms + # in fp32. + + if query_states.dtype == torch.float32: + if torch.is_autocast_enabled(): + target_dtype = torch.get_autocast_gpu_dtype() + # Handle the case where the model is quantized + elif hasattr(self.config, "_pre_quantization_dtype"): + target_dtype = self.config._pre_quantization_dtype + else: + target_dtype = self.q_proj.weight.dtype + + logger.warning_once( + f"The input hidden states seems to be silently casted in float32, this might be related to" + f" the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in" + f" {target_dtype}." + ) + + query_states = query_states.to(target_dtype) + key_states = key_states.to(target_dtype) + value_states = value_states.to(target_dtype) + + attn_output = _flash_attention_forward( + query_states, + key_states, + value_states, + attention_mask, + q_len, + position_ids=position_ids, + dropout=attn_dropout, + softmax_scale=None, + use_top_left_mask=self._flash_attn_uses_top_left_mask, + is_causal=self.is_causal, + ) + + attn_output = attn_output.reshape(bsz, q_len, self.hidden_size).contiguous() + attn_output = self.dense(attn_output) + + if not output_attentions: + attn_weights = None + + return attn_output, attn_weights, past_key_value + + +class MoonshineSdpaAttention(MoonshineAttention): + """ + SDPA attention module using torch.nn.functional.scaled_dot_product_attention. This module inherits from + `MoonshineAttention` as the weights of the module stays untouched. The only changes are on the forward pass to adapt to + SDPA API. + """ + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.require_contiguous_qkv = version.parse(get_torch_version()) < version.parse("2.2.0") + + # Adapted from MoonshineAttention.forward + def forward( + self, + hidden_states: torch.Tensor, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + past_key_value: Optional[Cache] = None, + key_value_states: Optional[torch.Tensor] = None, + output_attentions: bool = False, + use_cache: bool = False, + cache_position: Optional[torch.LongTensor] = None, + position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 + ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + bsz, q_len, _ = hidden_states.size() + + query_states = self.q_proj(hidden_states) + + # if key_value_states are provided this layer is used as a cross-attention layer + # for the decoder + is_cross_attention = key_value_states is not None + + if past_key_value is not None: + is_updated = past_key_value.is_updated.get(self.layer_idx) + if is_cross_attention: + # after the first generated id, we can subsequently re-use all key/value_states from cache + past_key_value.is_updated[self.layer_idx] = True + past_key_value = past_key_value.cross_attention_cache + else: + past_key_value = past_key_value.self_attention_cache + + # use key_value_states if cross attention + current_states = key_value_states if key_value_states is not None else hidden_states + if is_cross_attention and past_key_value and is_updated: + # reuse k,v, cross_attentions + key_states = past_key_value.key_cache[self.layer_idx] + value_states = past_key_value.value_cache[self.layer_idx] + else: + key_states = self.k_proj(current_states) + value_states = self.v_proj(current_states) + key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) + value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) + if is_cross_attention and past_key_value is not None: + key_states, value_states = past_key_value.update( + key_states, value_states, self.layer_idx, {"cache_position": cache_position} + ) + + if self.qk_layernorm: + query_states = self.q_layernorm(query_states) + key_states = self.k_layernorm(key_states) + + query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) + + if not is_cross_attention: + if position_embeddings is None: + logger.warning_once( + "The attention layers in this model are transitioning from computing the RoPE embeddings internally " + "through `position_ids` (2D tensor with the indexes of the tokens), to using externally computed " + "`position_embeddings` (Tuple of tensors, containing cos and sin). In v4.46 `position_ids` will be " + "removed and `position_embeddings` will be mandatory." + ) + cos, sin = self.rotary_emb(value_states, position_ids) + else: + cos, sin = position_embeddings + + # Partial rotary embedding + query_rot, query_pass = ( + query_states[..., : self.rotary_ndims], + query_states[..., self.rotary_ndims :], + ) + key_rot, key_pass = ( + key_states[..., : self.rotary_ndims], + key_states[..., self.rotary_ndims :], + ) + # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor] + query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin) + + # [batch_size, seq_length, num_heads, head_dim] + query_states = torch.cat((query_rot, query_pass), dim=-1) + key_states = torch.cat((key_rot, key_pass), dim=-1) + + if past_key_value is not None: + if not is_cross_attention: + cache_kwargs = { + "sin": sin, + "cos": cos, + "partial_rotation_size": self.rotary_ndims, + "cache_position": cache_position, + } + key_states, value_states = past_key_value.update( + key_states, value_states, self.layer_idx, cache_kwargs + ) + + key_states = repeat_kv(key_states, self.num_key_value_groups) + value_states = repeat_kv(value_states, self.num_key_value_groups) + + causal_mask = attention_mask + if attention_mask is not None: + causal_mask = causal_mask[:, :, :, : key_states.shape[-2]] + + # SDPA with memory-efficient backend is broken in torch==2.1.2 when using non-contiguous inputs and a custom + # attn_mask, so we need to call `.contiguous()` here. This was fixed in torch==2.2.0. + # Reference: https://github.com/pytorch/pytorch/issues/112577 + if self.require_contiguous_qkv and query_states.device.type == "cuda" and attention_mask is not None: + query_states = query_states.contiguous() + key_states = key_states.contiguous() + value_states = value_states.contiguous() + + # 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 self.is_causal and causal_mask is None and q_len > 1 else False + + attn_output = torch.nn.functional.scaled_dot_product_attention( + query_states, + key_states, + value_states, + attn_mask=causal_mask, + dropout_p=self.attention_dropout if self.training else 0.0, + is_causal=is_causal, + ) + + attn_output = attn_output.transpose(1, 2).contiguous() + attn_output = attn_output.reshape(bsz, q_len, self.hidden_size) + + attn_output = self.dense(attn_output) + + return attn_output, None, past_key_value + + +MOONSHINE_ATTENTION_CLASSES = { + "eager": MoonshineAttention, + "flash_attention_2": MoonshineFlashAttention2, + "sdpa": MoonshineSdpaAttention, +} + + +class MoonshineEncoderLayer(nn.Module): + def __init__(self, config: MoonshineConfig, layer_idx: int): + super().__init__() + self.hidden_size = config.hidden_size + + self.self_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation](config=config, layer_idx=layer_idx) + + self.mlp = MoonshineMLP(config, config.encoder_hidden_act) + self.input_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) + self.post_attention_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) + + def forward( + self, + hidden_states: torch.Tensor, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + past_key_value: Optional[Cache] = None, + output_attentions: Optional[bool] = False, + use_cache: Optional[bool] = False, + cache_position: Optional[torch.LongTensor] = None, + position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 + **kwargs, + ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + """ + Args: + hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` + attention_mask (`torch.FloatTensor`, *optional*): + attention mask of size `(batch_size, sequence_length)` if flash attention is used or `(batch_size, 1, + query_sequence_length, key_sequence_length)` if default attention is used. + output_attentions (`bool`, *optional*): + Whether or not to return the attentions tensors of all attention layers. See `attentions` under + returned tensors for more detail. + 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`). + past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states + cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): + Indices depicting the position of the input sequence tokens in the sequence + position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, + with `head_dim` being the embedding dimension of each attention head. + kwargs (`dict`, *optional*): + Arbitrary kwargs to be ignored, used for FSDP and other methods that injects code + into the model + """ + residual = hidden_states + + hidden_states = self.input_layernorm(hidden_states) + + # Self Attention + hidden_states, self_attn_weights, present_key_value = self.self_attn( + hidden_states=hidden_states, + attention_mask=attention_mask, + position_ids=position_ids, + past_key_value=past_key_value, + output_attentions=output_attentions, + use_cache=use_cache, + cache_position=cache_position, + position_embeddings=position_embeddings, + **kwargs, + ) + hidden_states = residual + hidden_states + + # Fully Connected + residual = hidden_states + hidden_states = self.post_attention_layernorm(hidden_states) + hidden_states = self.mlp(hidden_states) + hidden_states = residual + hidden_states + + outputs = (hidden_states,) + + if output_attentions: + outputs += (self_attn_weights,) + + if use_cache: + outputs += (present_key_value,) + + return outputs + + +class MoonshineDecoderLayer(nn.Module): + def __init__(self, config: MoonshineConfig, layer_idx: int = None): + super().__init__() + self.hidden_size = config.hidden_size + + self.self_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation]( + config=config, layer_idx=layer_idx, is_causal=True + ) + self.encoder_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation]( + config=config, layer_idx=layer_idx, is_causal=False + ) + + self.mlp = MoonshineMLP(config, config.decoder_hidden_act) + self.input_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) + self.post_attention_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) + self.final_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) + + def forward( + self, + hidden_states: torch.Tensor, + attention_mask: Optional[torch.Tensor] = None, + encoder_hidden_states: Optional[torch.Tensor] = None, + encoder_attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + encoder_position_ids: Optional[torch.LongTensor] = None, + past_key_value: Optional[Cache] = None, + output_attentions: Optional[bool] = False, + use_cache: Optional[bool] = False, + cache_position: Optional[torch.LongTensor] = None, + position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 + encoder_position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + **kwargs, + ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + """ + Args: + hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` + attention_mask (`torch.FloatTensor`, *optional*): + attention mask of size `(batch_size, sequence_length)` if flash attention is used or `(batch_size, 1, + query_sequence_length, key_sequence_length)` if default attention is used. + encoder_hidden_states (`torch.FloatTensor`): + cross attention input to the layer of shape `(batch, seq_len, embed_dim)` + encoder_attention_mask (`torch.FloatTensor`): encoder attention mask of size + `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values. + output_attentions (`bool`, *optional*): + Whether or not to return the attentions tensors of all attention layers. See `attentions` under + returned tensors for more detail. + 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`). + past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states + cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): + Indices depicting the position of the input sequence tokens in the sequence + position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, + with `head_dim` being the embedding dimension of each attention head. + encoder_position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + Tuple containing the cosine and sine positional embeddings of shape `(batch_size, encoder_seq_len, head_dim)`, + with `head_dim` being the embedding dimension of each attention head. + kwargs (`dict`, *optional*): + Arbitrary kwargs to be ignored, used for FSDP and other methods that injects code + into the model + """ + residual = hidden_states + + hidden_states = self.input_layernorm(hidden_states) + + # Self Attention + hidden_states, self_attn_weights, present_key_value = self.self_attn( + hidden_states=hidden_states, + attention_mask=attention_mask, + position_ids=position_ids, + past_key_value=past_key_value, + output_attentions=output_attentions, + use_cache=use_cache, + cache_position=cache_position, + position_embeddings=position_embeddings, + **kwargs, + ) + hidden_states = residual + hidden_states + + # Cross-Attention Block + cross_attn_weights = None + if encoder_hidden_states is not None: + residual = hidden_states + hidden_states = self.post_attention_layernorm(hidden_states) + 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, + position_ids=encoder_position_ids, + past_key_value=past_key_value, + output_attentions=output_attentions, + use_cache=use_cache, + position_embeddings=encoder_position_embeddings, + ) + hidden_states = residual + hidden_states + + # add cross-attn to positions 1 of present_key_value tuple + present_key_value = (present_key_value, cross_attn_present_key_value) + + # Fully Connected + residual = hidden_states + hidden_states = self.final_layernorm(hidden_states) + hidden_states = self.mlp(hidden_states) + hidden_states = residual + hidden_states + + outputs = (hidden_states,) + + if output_attentions: + outputs += (self_attn_weights, cross_attn_weights) + + if use_cache: + outputs += (present_key_value,) + + return outputs + + +MOONSHINE_START_DOCSTRING = r""" + This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the + library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads + etc.) + + This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. + Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage + and behavior. + + Parameters: + config ([`MoonshineConfig`]): + Model configuration class with all the parameters of the model. Initializing with a config file does not + load the weights associated with the model, only the configuration. Check out the + [`~PreTrainedModel.from_pretrained`] method to load the model weights. +""" + + +@add_start_docstrings( + "The bare Moonshine Model outputting raw hidden-states without any specific head on top.", + MOONSHINE_START_DOCSTRING, +) +class MoonshinePreTrainedModel(PreTrainedModel): + config_class = MoonshineConfig + base_model_prefix = "model" + supports_gradient_checkpointing = True + _no_split_modules = ["MoonshineDecoderLayer"] + _skip_keys_device_placement = ["past_key_values"] + _supports_flash_attn_2 = True + _supports_sdpa = True + _supports_cache_class = True + _supports_quantized_cache = True + _supports_static_cache = True + + def _init_weights(self, module): + std = self.config.initializer_range + if isinstance(module, nn.Linear): + module.weight.data.normal_(mean=0.0, std=std) + if module.bias is not None: + module.bias.data.zero_() + elif isinstance(module, nn.Embedding): + module.weight.data.normal_(mean=0.0, std=std) + if module.padding_idx is not None: + module.weight.data[module.padding_idx].zero_() + + +MOONSHINE_INPUTS_DOCSTRING = r""" + Args: + input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): + Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide + it. + + Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and + [`PreTrainedTokenizer.__call__`] for details. + + [What are input IDs?](../glossary#input-ids) + attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): + Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`: + + - 1 for tokens that are **not masked**, + - 0 for tokens that are **masked**. + + [What are attention masks?](../glossary#attention-mask) + + Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and + [`PreTrainedTokenizer.__call__`] for details. + + If `past_key_values` is used, optionally only the last `input_ids` have to be input (see + `past_key_values`). + + If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`] + and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more + information on the default strategy. + + - 1 indicates the head is **not masked**, + - 0 indicates the head is **masked**. + position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): + Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0, + config.n_positions - 1]`. + + [What are position IDs?](../glossary#position-ids) + 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, see our + [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache); + - 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)`. + inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): + 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. + 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`). + output_attentions (`bool`, *optional*): + Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned + tensors for more detail. + output_hidden_states (`bool`, *optional*): + Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for + 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. +""" + + +@add_start_docstrings( + "The bare Moonshine Model outputting raw hidden-states without any specific head on top.", + MOONSHINE_START_DOCSTRING, +) +class MoonshineEncoder(MoonshinePreTrainedModel): + """ + Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`MoonshineDecoderLayer`] + + Args: + config: MoonshineConfig + """ + + main_input_name = "input_features" + + def __init__(self, config: MoonshineConfig): + super().__init__(config) + self.config = config + embed_dim = config.hidden_size + + self.conv1 = nn.Conv1d(1, embed_dim, kernel_size=127, stride=64, bias=False) + self.conv2 = nn.Conv1d(embed_dim, 2 * embed_dim, kernel_size=7, stride=3) + self.conv3 = nn.Conv1d(2 * embed_dim, embed_dim, kernel_size=3, stride=2) + self.groupnorm = nn.GroupNorm(num_groups=1, num_channels=embed_dim, eps=1e-5) + + self.rotary_emb = MoonshineRotaryEmbedding( + dim=max(config.hidden_size // config.num_attention_heads // 2, 32), + max_position_embeddings=config.max_position_embeddings, + ) + + self.layers = nn.ModuleList([MoonshineEncoderLayer(config, idx) for idx in range(config.num_hidden_layers)]) + self.layer_norm = nn.LayerNorm(embed_dim, eps=config.layer_norm_eps, bias=False) + + self.gradient_checkpointing = False + self.post_init() + + def get_input_embeddings(self): + return self.conv1 + + def set_input_embeddings(self, value: nn.Module): + self.conv1 = value + + @add_start_docstrings_to_model_forward(MOONSHINE_INPUTS_DOCSTRING) + def forward( + self, + input_features: Optional[torch.FloatTensor] = None, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = 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, + output_hidden_states: Optional[bool] = None, + return_dict: Optional[bool] = None, + cache_position: Optional[torch.LongTensor] = None, + **flash_attn_kwargs: Unpack[FlashAttentionKwargs], + ) -> Union[Tuple, BaseModelOutputWithPast]: + output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions + output_hidden_states = ( + output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states + ) + 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_features is None) ^ (inputs_embeds is not None): + raise ValueError("You must specify exactly one of input_ids or inputs_embeds") + + 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.preprocess(input_features) + + # kept for BC (non `Cache` `past_key_values` inputs) + return_legacy_cache = False + if use_cache or past_key_values is not None: + if isinstance(past_key_values, Cache) and not isinstance(past_key_values, EncoderDecoderCache): + past_key_values = EncoderDecoderCache(past_key_values, DynamicCache()) + elif not isinstance(past_key_values, EncoderDecoderCache): + return_legacy_cache = True + logger.warning_once( + "Passing a tuple of `past_key_values` is deprecated and will be removed in Transformers v4.43.0. " + "You should pass an instance of `EncoderDecoderCache` instead, e.g. " + "`past_key_values=EncoderDecoderCache.from_legacy_cache(past_key_values)`." + ) + past_key_values = EncoderDecoderCache.from_legacy_cache(past_key_values) + + 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 + inputs_embeds.shape[1], device=inputs_embeds.device + ) + if position_ids is None: + position_ids = cache_position.unsqueeze(0) + + causal_mask = self._update_causal_mask( + attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions + ) + hidden_states = inputs_embeds + + # create position embeddings to be shared across the decoder layers + position_embeddings = self.rotary_emb(hidden_states, position_ids) + + # decoder layers + all_hidden_states = () if output_hidden_states else None + all_self_attns = () if output_attentions else None + next_decoder_cache = None + + for decoder_layer in self.layers: + if output_hidden_states: + all_hidden_states += (hidden_states,) + + if self.gradient_checkpointing and self.training: + layer_outputs = self._gradient_checkpointing_func( + decoder_layer.__call__, + hidden_states, + causal_mask, + position_ids, + past_key_values, + output_attentions, + use_cache, + cache_position, + position_embeddings, + ) + else: + layer_outputs = decoder_layer( + hidden_states, + attention_mask=causal_mask, + position_ids=position_ids, + past_key_value=past_key_values, + output_attentions=output_attentions, + use_cache=use_cache, + cache_position=cache_position, + position_embeddings=position_embeddings, + **flash_attn_kwargs, + ) + + hidden_states = layer_outputs[0] + + if use_cache: + next_decoder_cache = layer_outputs[2 if output_attentions else 1] + + if output_attentions: + all_self_attns += (layer_outputs[1],) + + hidden_states = self.layer_norm(hidden_states) + + # add hidden states from the last decoder layer + if output_hidden_states: + 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() + + 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( + last_hidden_state=hidden_states, + past_key_values=next_cache, + hidden_states=all_hidden_states, + attentions=all_self_attns, + ) + + def _update_causal_mask( + self, + attention_mask: torch.Tensor, + input_tensor: torch.Tensor, + cache_position: torch.Tensor, + past_key_values: Cache, + output_attentions: bool, + ): + 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 + sequence_length = input_tensor.shape[1] + if using_static_cache: + target_length = past_key_values.get_max_cache_shape() + else: + target_length = ( + attention_mask.shape[-1] + if isinstance(attention_mask, torch.Tensor) + else past_seen_tokens + sequence_length + 1 + ) + + # In case the provided `attention` mask is 2D, we generate a causal mask here (4D). + causal_mask = self._prepare_4d_causal_attention_mask_with_cache_position( + attention_mask, + sequence_length=sequence_length, + target_length=target_length, + dtype=dtype, + device=device, + cache_position=cache_position, + batch_size=input_tensor.shape[0], + ) + + 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 + min_dtype = torch.finfo(dtype).min + causal_mask = AttentionMaskConverter._unmask_unattended(causal_mask, min_dtype) + + return causal_mask + + @staticmethod + 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, + cache_position: torch.Tensor, + batch_size: int, + **kwargs, + ): + """ + 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. + 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: + min_dtype = torch.finfo(dtype).min + 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 _freeze_parameters(self): + for param in self.parameters(): + param.requires_grad = False + self._requires_grad = False + + def preprocess(self, input_features: torch.FloatTensor): + input_features = input_features.unsqueeze(1) + inputs_embeds = nn.functional.tanh(self.conv1(input_features)) + inputs_embeds = self.groupnorm(inputs_embeds) + inputs_embeds = nn.functional.gelu(self.conv2(inputs_embeds)) + inputs_embeds = nn.functional.gelu(self.conv3(inputs_embeds)) + inputs_embeds = inputs_embeds.permute(0, 2, 1) + return inputs_embeds + + +@add_start_docstrings( + "The bare Moonshine Model outputting raw hidden-states without any specific head on top.", + MOONSHINE_START_DOCSTRING, +) +class MoonshineDecoder(MoonshinePreTrainedModel): + """ + Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`MoonshineDecoderLayer`] + + Args: + config: MoonshineConfig + """ + + def __init__(self, config: MoonshineConfig): + super().__init__(config) + self.padding_idx = config.pad_token_id + self.vocab_size = config.vocab_size + + self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx) + self.layers = nn.ModuleList( + [MoonshineDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)] + ) + self.norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) + self.rotary_emb = MoonshineRotaryEmbedding( + dim=max(config.hidden_size // config.num_attention_heads // 2, 32), + max_position_embeddings=config.max_position_embeddings, + ) + self.gradient_checkpointing = False + + # Initialize weights and apply final processing + self.post_init() + + def get_input_embeddings(self): + return self.embed_tokens + + def set_input_embeddings(self, value): + self.embed_tokens = value + + @add_start_docstrings_to_model_forward(MOONSHINE_INPUTS_DOCSTRING) + def forward( + self, + input_ids: torch.LongTensor = None, + attention_mask: Optional[torch.Tensor] = None, + encoder_hidden_states: Optional[torch.FloatTensor] = None, + position_ids: Optional[torch.LongTensor] = None, + encoder_position_ids: Optional[torch.LongTensor] = 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, + output_hidden_states: Optional[bool] = None, + return_dict: Optional[bool] = None, + cache_position: Optional[torch.LongTensor] = None, + **flash_attn_kwargs: Unpack[FlashAttentionKwargs], + ) -> Union[Tuple, BaseModelOutputWithPast]: + output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions + output_hidden_states = ( + output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states + ) + 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 None) ^ (inputs_embeds is not None): + raise ValueError("You must specify exactly one of input_ids or inputs_embeds") + + 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) + + # kept for BC (non `Cache` `past_key_values` inputs) + return_legacy_cache = False + return_self_attention_cache = False + if use_cache or past_key_values is not None: + if isinstance(past_key_values, Cache) and not isinstance(past_key_values, EncoderDecoderCache): + return_self_attention_cache = True + past_key_values = EncoderDecoderCache(past_key_values, DynamicCache()) + elif not isinstance(past_key_values, EncoderDecoderCache): + return_legacy_cache = True + logger.warning_once( + "Passing a tuple of `past_key_values` is deprecated and will be removed in Transformers v4.43.0. " + "You should pass an instance of `EncoderDecoderCache` instead, e.g. " + "`past_key_values=EncoderDecoderCache.from_legacy_cache(past_key_values)`." + ) + past_key_values = EncoderDecoderCache.from_legacy_cache(past_key_values) + + 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 + inputs_embeds.shape[1], device=inputs_embeds.device + ) + if position_ids is None: + position_ids = cache_position.unsqueeze(0) + + if encoder_position_ids is None: + encoder_position_ids = torch.arange( + encoder_hidden_states.shape[1], device=encoder_hidden_states.device + ).unsqueeze(0) + + causal_mask = self._update_causal_mask( + attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions + ) + hidden_states = inputs_embeds + + # create position embeddings to be shared across the decoder layers + position_embeddings = self.rotary_emb(hidden_states, position_ids) + encoder_position_embeddings = self.rotary_emb(encoder_hidden_states, encoder_position_ids) + + # decoder layers + 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 + + for decoder_layer in self.layers: + if output_hidden_states: + all_hidden_states += (hidden_states,) + + if self.gradient_checkpointing and self.training: + layer_outputs = self._gradient_checkpointing_func( + decoder_layer.__call__, + hidden_states, + causal_mask, + encoder_hidden_states, + position_ids, + past_key_values, + output_attentions, + use_cache, + cache_position, + position_embeddings, + ) + else: + layer_outputs = decoder_layer( + hidden_states, + attention_mask=causal_mask, + encoder_hidden_states=encoder_hidden_states, + position_ids=position_ids, + encoder_position_ids=encoder_position_ids, + past_key_value=past_key_values, + output_attentions=output_attentions, + use_cache=use_cache, + cache_position=cache_position, + position_embeddings=position_embeddings, + encoder_position_embeddings=encoder_position_embeddings, + **flash_attn_kwargs, + ) + + hidden_states = layer_outputs[0] + + if output_attentions: + all_self_attns += (layer_outputs[1],) + + if encoder_hidden_states is not None: + all_cross_attentions += (layer_outputs[2],) + + hidden_states = self.norm(hidden_states) + + # add hidden states from the last decoder layer + if output_hidden_states: + all_hidden_states += (hidden_states,) + + next_cache = past_key_values if use_cache else None + if return_self_attention_cache: + next_cache = past_key_values.self_attention_cache + if return_legacy_cache: + next_cache = next_cache.to_legacy_cache() + + if not return_dict: + return tuple( + v + for v in [hidden_states, next_cache, all_hidden_states, all_self_attns, all_cross_attentions] + if v is not None + ) + return BaseModelOutputWithPastAndCrossAttentions( + last_hidden_state=hidden_states, + past_key_values=next_cache, + hidden_states=all_hidden_states, + attentions=all_self_attns, + cross_attentions=all_cross_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, + ): + 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 + sequence_length = input_tensor.shape[1] + if using_static_cache: + target_length = past_key_values.get_max_cache_shape() + else: + target_length = ( + attention_mask.shape[-1] + if isinstance(attention_mask, torch.Tensor) + else past_seen_tokens + sequence_length + 1 + ) + + # In case the provided `attention` mask is 2D, we generate a causal mask here (4D). + causal_mask = self._prepare_4d_causal_attention_mask_with_cache_position( + attention_mask, + sequence_length=sequence_length, + target_length=target_length, + dtype=dtype, + device=device, + cache_position=cache_position, + batch_size=input_tensor.shape[0], + ) + + 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 + min_dtype = torch.finfo(dtype).min + causal_mask = AttentionMaskConverter._unmask_unattended(causal_mask, min_dtype) + + return causal_mask + + @staticmethod + 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, + cache_position: torch.Tensor, + batch_size: int, + **kwargs, + ): + """ + 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. + 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: + min_dtype = torch.finfo(dtype).min + 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 _compute_mask_indices( + shape: Tuple[int, int], + mask_prob: float, + mask_length: int, + attention_mask: Optional[torch.LongTensor] = None, + min_masks: int = 0, +) -> np.ndarray: + """ + Computes random mask spans for a given shape. Used to implement [SpecAugment: A Simple Data Augmentation Method for + ASR](https://arxiv.org/abs/1904.08779). Note that this method is not optimized to run on TPU and should be run on + CPU as part of the preprocessing during training. + + Args: + shape: The shape for which to compute masks. This should be of a tuple of size 2 where + the first element is the batch size and the second element is the length of the axis to span. + mask_prob: The percentage of the whole axis (between 0 and 1) which will be masked. The number of + independently generated mask spans of length `mask_length` is computed by + `mask_prob*shape[1]/mask_length`. Note that due to overlaps, `mask_prob` is an upper bound and the + actual percentage will be smaller. + mask_length: size of the mask + min_masks: minimum number of masked spans + attention_mask: A (right-padded) attention mask which independently shortens the feature axis of + each batch dimension. + """ + batch_size, sequence_length = shape + + if mask_length < 1: + raise ValueError("`mask_length` has to be bigger than 0.") + + if mask_length > sequence_length: + raise ValueError( + f"`mask_length` has to be smaller than `sequence_length`, but got `mask_length`: {mask_length}" + f" and `sequence_length`: {sequence_length}`" + ) + + # epsilon is used for probabilistic rounding + epsilon = np.random.rand(1).item() + + def compute_num_masked_span(input_length): + """Given input length, compute how many spans should be masked""" + num_masked_span = int(mask_prob * input_length / mask_length + epsilon) + num_masked_span = max(num_masked_span, min_masks) + + # make sure num masked span <= sequence_length + if num_masked_span * mask_length > sequence_length: + num_masked_span = sequence_length // mask_length + + # make sure num_masked span is also <= input_length - (mask_length - 1) + if input_length - (mask_length - 1) < num_masked_span: + num_masked_span = max(input_length - (mask_length - 1), 0) + + return num_masked_span + + # compute number of masked spans in batch + input_lengths = ( + attention_mask.sum(-1).detach().tolist() + if attention_mask is not None + else [sequence_length for _ in range(batch_size)] + ) + + # SpecAugment mask to fill + spec_aug_mask = np.zeros((batch_size, sequence_length), dtype=bool) + spec_aug_mask_idxs = [] + + max_num_masked_span = compute_num_masked_span(sequence_length) + + if max_num_masked_span == 0: + return spec_aug_mask + + for input_length in input_lengths: + # compute num of masked spans for this input + num_masked_span = compute_num_masked_span(input_length) + + # get random indices to mask + spec_aug_mask_idx = np.random.choice( + np.arange(input_length - (mask_length - 1)), num_masked_span, replace=False + ) + + # pick first sampled index that will serve as a dummy index to pad vector + # to ensure same dimension for all batches due to probabilistic rounding + # Picking first sample just pads those vectors twice. + if len(spec_aug_mask_idx) == 0: + # this case can only happen if `input_length` is strictly smaller then + # `sequence_length` in which case the last token has to be a padding + # token which we can use as a dummy mask id + dummy_mask_idx = sequence_length - 1 + else: + dummy_mask_idx = spec_aug_mask_idx[0] + + spec_aug_mask_idx = np.concatenate( + [spec_aug_mask_idx, np.ones(max_num_masked_span - num_masked_span, dtype=np.int32) * dummy_mask_idx] + ) + spec_aug_mask_idxs.append(spec_aug_mask_idx) + + spec_aug_mask_idxs = np.array(spec_aug_mask_idxs) + + # expand masked indices to masked spans + spec_aug_mask_idxs = np.broadcast_to( + spec_aug_mask_idxs[:, :, None], (batch_size, max_num_masked_span, mask_length) + ) + spec_aug_mask_idxs = spec_aug_mask_idxs.reshape(batch_size, max_num_masked_span * mask_length) + + # add offset to the starting indexes so that indexes now create a span + offsets = np.arange(mask_length)[None, None, :] + offsets = np.broadcast_to(offsets, (batch_size, max_num_masked_span, mask_length)).reshape( + batch_size, max_num_masked_span * mask_length + ) + spec_aug_mask_idxs = spec_aug_mask_idxs + offsets + + # ensure that we cannot have indices larger than sequence_length + if spec_aug_mask_idxs.max() > sequence_length - 1: + spec_aug_mask_idxs[spec_aug_mask_idxs > sequence_length - 1] = sequence_length - 1 + + # scatter indices to mask + np.put_along_axis(spec_aug_mask, spec_aug_mask_idxs, 1, -1) + + return spec_aug_mask + + +@add_start_docstrings( + "The bare Moonshine Model outputting raw hidden-states without any specific head on top.", + MOONSHINE_START_DOCSTRING, +) +class MoonshineModel(MoonshinePreTrainedModel): + def __init__(self, config: MoonshineConfig): + super().__init__(config) + self.encoder = MoonshineEncoder(config) + self.decoder = MoonshineDecoder(config) + # Initialize weights and apply final processing + self.post_init() + + def get_input_embeddings(self): + return self.decoder.embed_tokens + + def set_input_embeddings(self, value): + self.decoder.embed_tokens = value + + def get_encoder(self): + return self.encoder + + def get_decoder(self): + return self.decoder + + def freeze_encoder(self): + """ + Calling this function will disable the gradient computation for the Moonshine encoder so that its parameters will + not be updated during training. + """ + self.encoder._freeze_parameters() + + def _mask_input_features( + self, + input_features: torch.FloatTensor, + attention_mask: Optional[torch.LongTensor] = None, + ): + """ + Masks extracted features along time axis and/or along feature axis according to + [SpecAugment](https://arxiv.org/abs/1904.08779). + """ + + # `config.apply_spec_augment` can set masking to False + if not getattr(self.config, "apply_spec_augment", True): + return input_features + + # generate indices & apply SpecAugment along time axis + batch_size, hidden_size, sequence_length = input_features.size() + + if self.config.mask_time_prob > 0 and self.training: + # generate indices & apply SpecAugment along time axis + mask_time_indices = _compute_mask_indices( + (batch_size, sequence_length), + mask_prob=self.config.mask_time_prob, + mask_length=self.config.mask_time_length, + attention_mask=attention_mask, + min_masks=self.config.mask_time_min_masks, + ) + mask_time_indices = torch.tensor(mask_time_indices, device=input_features.device, dtype=torch.bool) + mask_time_indices = mask_time_indices[:, None].expand(-1, hidden_size, -1) + input_features[mask_time_indices] = 0 + + if self.config.mask_feature_prob > 0 and self.training: + # generate indices & apply SpecAugment along feature axis + mask_feature_indices = _compute_mask_indices( + (batch_size, hidden_size), + mask_prob=self.config.mask_feature_prob, + mask_length=self.config.mask_feature_length, + min_masks=self.config.mask_feature_min_masks, + ) + mask_feature_indices = torch.tensor(mask_feature_indices, device=input_features.device, dtype=torch.bool) + input_features[mask_feature_indices] = 0 + + return input_features + + @add_start_docstrings_to_model_forward(MOONSHINE_INPUTS_DOCSTRING) + @replace_return_docstrings(output_type=Seq2SeqModelOutput, config_class=_CONFIG_FOR_DOC) + def forward( + self, + input_features: Optional[torch.FloatTensor] = None, + attention_mask: Optional[torch.LongTensor] = None, + decoder_input_ids: Optional[torch.LongTensor] = None, + decoder_attention_mask: Optional[torch.LongTensor] = None, + encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[EncoderDecoderCache, Tuple[torch.FloatTensor]]] = None, + decoder_inputs_embeds: Optional[Tuple[torch.FloatTensor]] = None, + decoder_position_ids: Optional[Tuple[torch.LongTensor]] = None, + use_cache: Optional[bool] = None, + 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], Seq2SeqModelOutput]: + r""" + Returns: + + Example: + Returns: + + Example: + + ```python + >>> import torch + >>> from transformers import AutoFeatureExtractor, WhisperModel + >>> from datasets import load_dataset + + >>> model = WhisperModel.from_pretrained("openai/whisper-base") + >>> feature_extractor = AutoFeatureExtractor.from_pretrained("openai/whisper-base") + >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") + >>> inputs = feature_extractor(ds[0]["audio"]["array"], return_tensors="pt") + >>> input_features = inputs.input_features + >>> decoder_input_ids = torch.tensor([[1, 1]]) * model.config.decoder_start_token_id + >>> last_hidden_state = model(input_features, decoder_input_ids=decoder_input_ids).last_hidden_state + >>> list(last_hidden_state.shape) + [1, 2, 512] + ```""" + output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions + output_hidden_states = ( + output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states + ) + 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 encoder_outputs is None: + input_features = self._mask_input_features(input_features, attention_mask=attention_mask) + + encoder_outputs = self.encoder( + input_features, + output_attentions=output_attentions, + output_hidden_states=output_hidden_states, + return_dict=return_dict, + ) + # If the user passed a tuple for encoder_outputs, we wrap it in a BaseModelOutput when return_dict=True + elif return_dict and not isinstance(encoder_outputs, BaseModelOutput): + encoder_outputs = BaseModelOutput( + last_hidden_state=encoder_outputs[0], + hidden_states=encoder_outputs[1] if len(encoder_outputs) > 1 else None, + attentions=encoder_outputs[2] if len(encoder_outputs) > 2 else None, + ) + + # decoder outputs consists of (dec_features, past_key_value, dec_hidden, dec_attn) + decoder_outputs = self.decoder( + input_ids=decoder_input_ids, + attention_mask=decoder_attention_mask, + encoder_hidden_states=encoder_outputs[0], + past_key_values=past_key_values, + inputs_embeds=decoder_inputs_embeds, + position_ids=decoder_position_ids, + use_cache=use_cache, + output_attentions=output_attentions, + output_hidden_states=output_hidden_states, + return_dict=return_dict, + cache_position=cache_position, + ) + + if not return_dict: + return decoder_outputs + encoder_outputs + + return Seq2SeqModelOutput( + last_hidden_state=decoder_outputs.last_hidden_state, + past_key_values=decoder_outputs.past_key_values, + decoder_hidden_states=decoder_outputs.hidden_states, + decoder_attentions=decoder_outputs.attentions, + cross_attentions=decoder_outputs.cross_attentions, + encoder_last_hidden_state=encoder_outputs.last_hidden_state, + encoder_hidden_states=encoder_outputs.hidden_states, + encoder_attentions=encoder_outputs.attentions, + ) + + +# Copied from transformers.models.bart.modeling_bart.shift_tokens_right +def shift_tokens_right(input_ids: torch.Tensor, pad_token_id: int, decoder_start_token_id: int): + """ + Shift input ids one token to the right. + """ + shifted_input_ids = input_ids.new_zeros(input_ids.shape) + shifted_input_ids[:, 1:] = input_ids[:, :-1].clone() + shifted_input_ids[:, 0] = decoder_start_token_id + + if pad_token_id is None: + raise ValueError("self.model.config.pad_token_id has to be defined.") + # replace possible -100 values in labels by `pad_token_id` + shifted_input_ids.masked_fill_(shifted_input_ids == -100, pad_token_id) + + return shifted_input_ids + + +class MoonshineForConditionalGeneration(MoonshinePreTrainedModel, GenerationMixin): + _tied_weights_keys = ["proj_out.weight"] + + def __init__(self, config: MoonshineConfig): + super().__init__(config) + self.model = MoonshineModel(config) + self.proj_out = nn.Linear(config.hidden_size, config.vocab_size, bias=False) + + # Initialize weights and apply final processing + self.post_init() + + def get_encoder(self): + return self.model.get_encoder() + + def get_decoder(self): + return self.model.get_decoder() + + def get_output_embeddings(self): + return self.proj_out + + def set_output_embeddings(self, new_embeddings): + self.proj_out = new_embeddings + + def get_input_embeddings(self) -> nn.Module: + return self.model.get_input_embeddings() + + @property + def encoder(self): + return self.get_encoder() + + @add_start_docstrings_to_model_forward(MOONSHINE_INPUTS_DOCSTRING) + @replace_return_docstrings(output_type=Seq2SeqLMOutput, config_class=_CONFIG_FOR_DOC) + def forward( + self, + input_features: Optional[torch.FloatTensor] = None, + attention_mask: Optional[torch.LongTensor] = None, + decoder_input_ids: Optional[torch.LongTensor] = None, + decoder_attention_mask: Optional[torch.LongTensor] = None, + encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[EncoderDecoderCache, Tuple[torch.FloatTensor]]] = None, + decoder_inputs_embeds: Optional[Tuple[torch.FloatTensor]] = None, + decoder_position_ids: Optional[Tuple[torch.LongTensor]] = None, + labels: Optional[torch.LongTensor] = None, + use_cache: Optional[bool] = None, + 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], Seq2SeqLMOutput]: + r""" + labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): + Labels for computing the language modeling loss. Indices should either be in `[0, ..., config.vocab_size]` + or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored (masked), the loss is + only computed for the tokens with labels in `[0, ..., config.vocab_size]`. `sequence_length` should be smaller than or equal to `config.max_target_positions`. + + Returns: + + Example: + + ```python + >>> import torch + >>> from transformers import AutoProcessor, WhisperForConditionalGeneration + >>> from datasets import load_dataset + + >>> processor = AutoProcessor.from_pretrained("openai/whisper-tiny.en") + >>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny.en") + + >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") + + >>> inputs = processor(ds[0]["audio"]["array"], return_tensors="pt") + >>> input_features = inputs.input_features + + >>> generated_ids = model.generate(inputs=input_features) + + >>> transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] + >>> transcription + ' Mr. Quilter is the apostle of the middle classes, and we are glad to welcome his gospel.' + ```""" + return_dict = return_dict if return_dict is not None else self.config.use_return_dict + + if labels is not None: + if labels.shape[1] > self.max_target_positions: + raise ValueError( + f"Labels' sequence length {labels.shape[1]} cannot exceed the maximum allowed length of {self.max_target_positions} tokens." + ) + if decoder_input_ids is None and decoder_inputs_embeds is None: + decoder_input_ids = shift_tokens_right( + labels, self.config.pad_token_id, self.config.decoder_start_token_id + ) + + outputs = self.model( + input_features, + attention_mask=attention_mask, + decoder_input_ids=decoder_input_ids, + encoder_outputs=encoder_outputs, + decoder_attention_mask=decoder_attention_mask, + past_key_values=past_key_values, + decoder_inputs_embeds=decoder_inputs_embeds, + decoder_position_ids=decoder_position_ids, + use_cache=use_cache, + output_attentions=output_attentions, + output_hidden_states=output_hidden_states, + return_dict=return_dict, + cache_position=cache_position, + ) + lm_logits = self.proj_out(outputs[0]) + + loss = None + if labels is not None: + loss_fct = CrossEntropyLoss() + # move labels to correct device to enable PP + labels = labels.to(lm_logits.device) + loss = loss_fct(lm_logits.view(-1, self.config.vocab_size), labels.reshape(-1)) + + if not return_dict: + output = (lm_logits,) + outputs[1:] + return ((loss,) + output) if loss is not None else output + + return Seq2SeqLMOutput( + loss=loss, + logits=lm_logits, + past_key_values=outputs.past_key_values, + decoder_hidden_states=outputs.decoder_hidden_states, + decoder_attentions=outputs.decoder_attentions, + cross_attentions=outputs.cross_attentions, + encoder_last_hidden_state=outputs.encoder_last_hidden_state, + encoder_hidden_states=outputs.encoder_hidden_states, + encoder_attentions=outputs.encoder_attentions, + ) diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index be24c7837e8360..bea8370b7313ea 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -462,6 +462,8 @@ def forward( value_states = self.v_proj(current_states) key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) + if is_cross_attention and past_key_value is not None: + key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, {"cache_position": cache_position}) if self.qk_layernorm: query_states = self.q_layernorm(query_states) @@ -505,11 +507,7 @@ def forward( "partial_rotation_size": self.rotary_ndims, "cache_position": cache_position, } - else: - cache_kwargs = { - "cache_position": cache_position, - } - key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs) + key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs) key_states = repeat_kv(key_states, self.num_key_value_groups) value_states = repeat_kv(value_states, self.num_key_value_groups) @@ -587,6 +585,8 @@ def forward( value_states = self.v_proj(current_states) key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) + if is_cross_attention and past_key_value is not None: + key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, {"cache_position": cache_position}) if self.qk_layernorm: query_states = self.q_layernorm(query_states) @@ -630,11 +630,7 @@ def forward( "partial_rotation_size": self.rotary_ndims, "cache_position": cache_position, } - else: - cache_kwargs = { - "cache_position": cache_position, - } - key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs) + key_states, value_states = past_key_value.update(key_states, value_states, 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. @@ -732,6 +728,8 @@ def forward( value_states = self.v_proj(current_states) key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) + if is_cross_attention and past_key_value is not None: + key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, {"cache_position": cache_position}) if self.qk_layernorm: query_states = self.q_layernorm(query_states) @@ -775,11 +773,7 @@ def forward( "partial_rotation_size": self.rotary_ndims, "cache_position": cache_position, } - else: - cache_kwargs = { - "cache_position": cache_position, - } - key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs) + key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs) key_states = repeat_kv(key_states, self.num_key_value_groups) value_states = repeat_kv(value_states, self.num_key_value_groups) @@ -839,7 +833,7 @@ def __init__(self, config: MoonshineConfig, layer_idx: int = None): self.hidden_size = config.hidden_size self.self_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation](config=config, layer_idx=layer_idx, is_causal=True) - self.encoder_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation](config=config, layer_idx=layer_idx, is_causal=True) + self.encoder_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation](config=config, layer_idx=layer_idx, is_causal=False) self.mlp = MoonshineMLP(config, config.decoder_hidden_act) self.input_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) From 461f210e659fa1089ada7cc39edd44926feeb1b2 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Sun, 15 Dec 2024 21:28:37 +0100 Subject: [PATCH 11/39] no causal attention mask for the encoder --- .../models/moonshine/modular_moonshine.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index bea8370b7313ea..3d2c62487cf30f 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -1032,7 +1032,6 @@ def preprocess(self, input_features: torch.FloatTensor): def forward( self, input_features: Optional[torch.FloatTensor] = None, - attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -1084,9 +1083,6 @@ def forward( if position_ids is None: position_ids = cache_position.unsqueeze(0) - causal_mask = self._update_causal_mask( - attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions - ) hidden_states = inputs_embeds # create position embeddings to be shared across the decoder layers @@ -1097,15 +1093,15 @@ def forward( all_self_attns = () if output_attentions else None next_decoder_cache = None - for decoder_layer in self.layers: + for encoder_layer in self.layers: if output_hidden_states: all_hidden_states += (hidden_states,) if self.gradient_checkpointing and self.training: layer_outputs = self._gradient_checkpointing_func( - decoder_layer.__call__, + encoder_layer.__call__, hidden_states, - causal_mask, + None, position_ids, past_key_values, output_attentions, @@ -1114,9 +1110,8 @@ def forward( position_embeddings, ) else: - layer_outputs = decoder_layer( + layer_outputs = encoder_layer( hidden_states, - attention_mask=causal_mask, position_ids=position_ids, past_key_value=past_key_values, output_attentions=output_attentions, From 22dbaae03b40ccbb0b01694de299c4591f69d9ba Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Sun, 15 Dec 2024 22:07:41 +0100 Subject: [PATCH 12/39] model addition (imports etc) --- docs/source/en/_toctree.yml | 2 + docs/source/en/model_doc/moonshine.md | 108 ++++++++++++++++++ src/transformers/__init__.py | 22 ++++ src/transformers/models/__init__.py | 1 + .../models/auto/configuration_auto.py | 2 + src/transformers/models/auto/modeling_auto.py | 6 + .../models/auto/tokenization_auto.py | 1 + src/transformers/models/moonshine/__init__.py | 13 ++- tests/models/moonshine/__init__.py | 0 9 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 docs/source/en/model_doc/moonshine.md create mode 100644 tests/models/moonshine/__init__.py diff --git a/docs/source/en/_toctree.yml b/docs/source/en/_toctree.yml index d800e40ecbd69d..66bcf4f849c804 100644 --- a/docs/source/en/_toctree.yml +++ b/docs/source/en/_toctree.yml @@ -490,6 +490,8 @@ title: mLUKE - local: model_doc/mobilebert title: MobileBERT + - local: model_doc/moonshine + title: moonshine - local: model_doc/mpnet title: MPNet - local: model_doc/mpt diff --git a/docs/source/en/model_doc/moonshine.md b/docs/source/en/model_doc/moonshine.md new file mode 100644 index 00000000000000..bf5f1255f03c2d --- /dev/null +++ b/docs/source/en/model_doc/moonshine.md @@ -0,0 +1,108 @@ + + +# moonshine + +# moonshine + +## Overview + +The moonshine model was proposed in []() by . + + +The abstract from the paper is the following: + +** + +Tips: + + + +This model was contributed by [INSERT YOUR HF USERNAME HERE](https://huggingface.co/). +The original code can be found [here](). + + +## MoonshineConfig + +[[autodoc]] MoonshineConfig + +## MoonshineTokenizer + +[[autodoc]] MoonshineTokenizer + - set_prefix_tokens + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + - batch_decode + - decode + - basic_normalize + - normalize + +## MoonshineTokenizerFast + +[[autodoc]] MoonshineTokenizerFast + - set_prefix_tokens + - build_inputs_with_special_tokens + - get_special_tokens_mask + - create_token_type_ids_from_sequences + - save_vocabulary + - batch_decode + - decode + - basic_normalize + - normalize + +## MoonshineFeatureExtractor + +[[autodoc]] MoonshineFeatureExtractor + - __call__ + +## MoonshineProcessor + +[[autodoc]] MoonshineProcessor + - __call__ + - from_pretrained + - save_pretrained + - batch_decode + - decode + + + + +## MoonshineModel + +[[autodoc]] MoonshineModel + - forward + - _mask_input_features + +## MoonshineForConditionalGeneration + +[[autodoc]] MoonshineForConditionalGeneration + - forward + - generate + +## MoonshineForCausalLM + +[[autodoc]] MoonshineForCausalLM + - forward + +## MoonshineForAudioClassification + +[[autodoc]] MoonshineForAudioClassification + - forward + + + diff --git a/src/transformers/__init__.py b/src/transformers/__init__.py index 36cc4449aec4a2..b651840412023a 100755 --- a/src/transformers/__init__.py +++ b/src/transformers/__init__.py @@ -463,6 +463,7 @@ "models.gpt_bigcode": ["GPTBigCodeConfig"], "models.gpt_neo": ["GPTNeoConfig"], "models.gpt_neox": ["GPTNeoXConfig"], + "models.moonshine": ["MoonshineConfig"], "models.gpt_neox_japanese": ["GPTNeoXJapaneseConfig"], "models.gpt_sw3": [], "models.gptj": ["GPTJConfig"], @@ -1081,6 +1082,7 @@ _import_structure["models.gemma"].append("GemmaTokenizerFast") _import_structure["models.gpt2"].append("GPT2TokenizerFast") _import_structure["models.gpt_neox"].append("GPTNeoXTokenizerFast") + _import_structure["models.moonshine"].append("MoonshineTokenizer") _import_structure["models.gpt_neox_japanese"].append("GPTNeoXJapaneseTokenizer") _import_structure["models.herbert"].append("HerbertTokenizerFast") _import_structure["models.layoutlm"].append("LayoutLMTokenizerFast") @@ -2360,6 +2362,16 @@ "GPTNeoXPreTrainedModel", ] ) + _import_structure["models.moonshine"].extend( + [ + "MoonshineForCausalLM", + "MoonshineForQuestionAnswering", + "MoonshineForSequenceClassification", + "MoonshineForTokenClassification", + "MoonshineModel", + "MoonshinePreTrainedModel", + ] + ) _import_structure["models.gpt_neox_japanese"].extend( [ "GPTNeoXJapaneseForCausalLM", @@ -5339,6 +5351,7 @@ ) from .models.gpt_neo import GPTNeoConfig from .models.gpt_neox import GPTNeoXConfig + from .models.moonshine import MoonshineConfig from .models.gpt_neox_japanese import ( GPTNeoXJapaneseConfig, ) @@ -6009,6 +6022,7 @@ from .models.gemma import GemmaTokenizerFast from .models.gpt2 import GPT2TokenizerFast from .models.gpt_neox import GPTNeoXTokenizerFast + from .models.moonshine import MoonshineTokenizer from .models.gpt_neox_japanese import GPTNeoXJapaneseTokenizer from .models.herbert import HerbertTokenizerFast from .models.layoutlm import LayoutLMTokenizerFast @@ -7102,6 +7116,14 @@ GPTNeoXModel, GPTNeoXPreTrainedModel, ) + from .models.moonshine import ( + MoonshineForCausalLM, + MoonshineForQuestionAnswering, + MoonshineForSequenceClassification, + MoonshineForTokenClassification, + MoonshineModel, + MoonshinePreTrainedModel, + ) from .models.gpt_neox_japanese import ( GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseModel, diff --git a/src/transformers/models/__init__.py b/src/transformers/models/__init__.py index 0d4b9f2f94de9b..ded8e359e30443 100644 --- a/src/transformers/models/__init__.py +++ b/src/transformers/models/__init__.py @@ -103,6 +103,7 @@ gpt_bigcode, gpt_neo, gpt_neox, + moonshine, gpt_neox_japanese, gpt_sw3, gptj, diff --git a/src/transformers/models/auto/configuration_auto.py b/src/transformers/models/auto/configuration_auto.py index 7f0182b50085c5..2696bda883a628 100644 --- a/src/transformers/models/auto/configuration_auto.py +++ b/src/transformers/models/auto/configuration_auto.py @@ -121,6 +121,7 @@ ("gpt_bigcode", "GPTBigCodeConfig"), ("gpt_neo", "GPTNeoConfig"), ("gpt_neox", "GPTNeoXConfig"), + ("moonshine", "MoonshineConfig"), ("gpt_neox_japanese", "GPTNeoXJapaneseConfig"), ("gptj", "GPTJConfig"), ("gptsan-japanese", "GPTSanJapaneseConfig"), @@ -425,6 +426,7 @@ ("gpt_bigcode", "GPTBigCode"), ("gpt_neo", "GPT Neo"), ("gpt_neox", "GPT NeoX"), + ("moonshine", "moonshine"), ("gpt_neox_japanese", "GPT NeoX Japanese"), ("gptj", "GPT-J"), ("gptsan-japanese", "GPTSAN-japanese"), diff --git a/src/transformers/models/auto/modeling_auto.py b/src/transformers/models/auto/modeling_auto.py index 5206972b72efde..b8258f972ff4b4 100644 --- a/src/transformers/models/auto/modeling_auto.py +++ b/src/transformers/models/auto/modeling_auto.py @@ -118,6 +118,7 @@ ("gpt_bigcode", "GPTBigCodeModel"), ("gpt_neo", "GPTNeoModel"), ("gpt_neox", "GPTNeoXModel"), + ("moonshine", "MoonshineModel"), ("gpt_neox_japanese", "GPTNeoXJapaneseModel"), ("gptj", "GPTJModel"), ("gptsan-japanese", "GPTSanJapaneseForConditionalGeneration"), @@ -408,6 +409,7 @@ ("gpt_bigcode", "GPTBigCodeForCausalLM"), ("gpt_neo", "GPTNeoForCausalLM"), ("gpt_neox", "GPTNeoXForCausalLM"), + ("moonshine", "MoonshineForCausalLM"), ("gpt_neox_japanese", "GPTNeoXJapaneseForCausalLM"), ("gptj", "GPTJForCausalLM"), ("gptsan-japanese", "GPTSanJapaneseForConditionalGeneration"), @@ -494,6 +496,7 @@ ("gpt_bigcode", "GPTBigCodeForCausalLM"), ("gpt_neo", "GPTNeoForCausalLM"), ("gpt_neox", "GPTNeoXForCausalLM"), + ("moonshine", "MoonshineForCausalLM"), ("gpt_neox_japanese", "GPTNeoXJapaneseForCausalLM"), ("gptj", "GPTJForCausalLM"), ("granite", "GraniteForCausalLM"), @@ -951,6 +954,7 @@ ("gpt_bigcode", "GPTBigCodeForSequenceClassification"), ("gpt_neo", "GPTNeoForSequenceClassification"), ("gpt_neox", "GPTNeoXForSequenceClassification"), + ("moonshine", "MoonshineForSequenceClassification"), ("gptj", "GPTJForSequenceClassification"), ("ibert", "IBertForSequenceClassification"), ("jamba", "JambaForSequenceClassification"), @@ -1039,6 +1043,7 @@ ("gpt2", "GPT2ForQuestionAnswering"), ("gpt_neo", "GPTNeoForQuestionAnswering"), ("gpt_neox", "GPTNeoXForQuestionAnswering"), + ("moonshine", "MoonshineForQuestionAnswering"), ("gptj", "GPTJForQuestionAnswering"), ("ibert", "IBertForQuestionAnswering"), ("layoutlmv2", "LayoutLMv2ForQuestionAnswering"), @@ -1142,6 +1147,7 @@ ("gpt_bigcode", "GPTBigCodeForTokenClassification"), ("gpt_neo", "GPTNeoForTokenClassification"), ("gpt_neox", "GPTNeoXForTokenClassification"), + ("moonshine", "MoonshineForTokenClassification"), ("ibert", "IBertForTokenClassification"), ("layoutlm", "LayoutLMForTokenClassification"), ("layoutlmv2", "LayoutLMv2ForTokenClassification"), diff --git a/src/transformers/models/auto/tokenization_auto.py b/src/transformers/models/auto/tokenization_auto.py index 4ed67df0e84b52..ea51f0c1302ab7 100644 --- a/src/transformers/models/auto/tokenization_auto.py +++ b/src/transformers/models/auto/tokenization_auto.py @@ -311,6 +311,7 @@ ("mluke", ("MLukeTokenizer" if is_sentencepiece_available() else None, None)), ("mobilebert", ("MobileBertTokenizer", "MobileBertTokenizerFast" if is_tokenizers_available() else None)), ("moshi", (None, "PreTrainedTokenizerFast" if is_tokenizers_available() else None)), + ("moonshine", (None, "PreTrainedTokenizerFast" if is_tokenizers_available() else None)), ("mpnet", ("MPNetTokenizer", "MPNetTokenizerFast" if is_tokenizers_available() else None)), ("mpt", (None, "GPTNeoXTokenizerFast" if is_tokenizers_available() else None)), ("mra", ("RobertaTokenizer", "RobertaTokenizerFast" if is_tokenizers_available() else None)), diff --git a/src/transformers/models/moonshine/__init__.py b/src/transformers/models/moonshine/__init__.py index d1c88ad1b9cb72..b99acc9a22315f 100644 --- a/src/transformers/models/moonshine/__init__.py +++ b/src/transformers/models/moonshine/__init__.py @@ -30,9 +30,13 @@ except OptionalDependencyNotAvailable: pass else: - _import_structure["modeling_gemma2"] = [ + _import_structure["modeling_moonshine"] = [ + "MoonshineForConditionalGeneration", + "MoonshineModel", + "MoonshinePreTrainedModel", ] + if TYPE_CHECKING: from .configuration_moonshine import MoonshineConfig @@ -42,7 +46,12 @@ except OptionalDependencyNotAvailable: pass else: - pass + from .modeling_moonshine import ( + MoonshineForConditionalGeneration, + MoonshineModel, + MoonshinePreTrainedModel, + ) + else: import sys diff --git a/tests/models/moonshine/__init__.py b/tests/models/moonshine/__init__.py new file mode 100644 index 00000000000000..e69de29bb2d1d6 From 72ba8c44a495d686596c8435ad65ea5bb928396a Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Sun, 15 Dec 2024 22:30:04 +0100 Subject: [PATCH 13/39] small nit --- src/transformers/models/moonshine/modular_moonshine.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index 3d2c62487cf30f..6d816249eca7f1 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -499,8 +499,7 @@ def forward( query_states = torch.cat((query_rot, query_pass), dim=-1) key_states = torch.cat((key_rot, key_pass), dim=-1) - if past_key_value is not None: - if not is_cross_attention: + if past_key_value is not None: cache_kwargs = { "sin": sin, "cos": cos, @@ -622,8 +621,7 @@ def forward( query_states = torch.cat((query_rot, query_pass), dim=-1) key_states = torch.cat((key_rot, key_pass), dim=-1) - if past_key_value is not None: - if not is_cross_attention: + if past_key_value is not None: cache_kwargs = { "sin": sin, "cos": cos, @@ -765,8 +763,7 @@ def forward( query_states = torch.cat((query_rot, query_pass), dim=-1) key_states = torch.cat((key_rot, key_pass), dim=-1) - if past_key_value is not None: - if not is_cross_attention: + if past_key_value is not None: cache_kwargs = { "sin": sin, "cos": cos, From f54850438390f082712760d37d487b7c9bdba168 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Mon, 16 Dec 2024 14:49:37 +0100 Subject: [PATCH 14/39] nits --- src/transformers/__init__.py | 20 +- src/transformers/models/__init__.py | 2 +- .../models/auto/configuration_auto.py | 4 +- src/transformers/models/auto/modeling_auto.py | 8 +- .../models/auto/processing_auto.py | 1 + .../models/auto/tokenization_auto.py | 2 +- .../moonshine/configuration_moonshine.py | 57 +--- .../models/moonshine/modeling_moonshine.py | 96 +++---- .../models/moonshine/modular_moonshine.py | 246 +++++++----------- 9 files changed, 158 insertions(+), 278 deletions(-) diff --git a/src/transformers/__init__.py b/src/transformers/__init__.py index b651840412023a..e89407c7eeaa10 100755 --- a/src/transformers/__init__.py +++ b/src/transformers/__init__.py @@ -5351,7 +5351,6 @@ ) from .models.gpt_neo import GPTNeoConfig from .models.gpt_neox import GPTNeoXConfig - from .models.moonshine import MoonshineConfig from .models.gpt_neox_japanese import ( GPTNeoXJapaneseConfig, ) @@ -5499,6 +5498,7 @@ from .models.mobilevitv2 import ( MobileViTV2Config, ) + from .models.moonshine import MoonshineConfig from .models.moshi import ( MoshiConfig, MoshiDepthConfig, @@ -6022,7 +6022,6 @@ from .models.gemma import GemmaTokenizerFast from .models.gpt2 import GPT2TokenizerFast from .models.gpt_neox import GPTNeoXTokenizerFast - from .models.moonshine import MoonshineTokenizer from .models.gpt_neox_japanese import GPTNeoXJapaneseTokenizer from .models.herbert import HerbertTokenizerFast from .models.layoutlm import LayoutLMTokenizerFast @@ -6037,6 +6036,7 @@ from .models.mbart import MBartTokenizerFast from .models.mbart50 import MBart50TokenizerFast from .models.mobilebert import MobileBertTokenizerFast + from .models.moonshine import MoonshineTokenizer from .models.mpnet import MPNetTokenizerFast from .models.mt5 import MT5TokenizerFast from .models.mvp import MvpTokenizerFast @@ -7116,14 +7116,6 @@ GPTNeoXModel, GPTNeoXPreTrainedModel, ) - from .models.moonshine import ( - MoonshineForCausalLM, - MoonshineForQuestionAnswering, - MoonshineForSequenceClassification, - MoonshineForTokenClassification, - MoonshineModel, - MoonshinePreTrainedModel, - ) from .models.gpt_neox_japanese import ( GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseModel, @@ -7463,6 +7455,14 @@ MobileViTV2Model, MobileViTV2PreTrainedModel, ) + from .models.moonshine import ( + MoonshineForCausalLM, + MoonshineForQuestionAnswering, + MoonshineForSequenceClassification, + MoonshineForTokenClassification, + MoonshineModel, + MoonshinePreTrainedModel, + ) from .models.moshi import ( MoshiForCausalLM, MoshiForConditionalGeneration, diff --git a/src/transformers/models/__init__.py b/src/transformers/models/__init__.py index ded8e359e30443..a95d4f348ab1c4 100644 --- a/src/transformers/models/__init__.py +++ b/src/transformers/models/__init__.py @@ -103,7 +103,6 @@ gpt_bigcode, gpt_neo, gpt_neox, - moonshine, gpt_neox_japanese, gpt_sw3, gptj, @@ -163,6 +162,7 @@ mobilenet_v2, mobilevit, mobilevitv2, + moonshine, moshi, mpnet, mpt, diff --git a/src/transformers/models/auto/configuration_auto.py b/src/transformers/models/auto/configuration_auto.py index 2696bda883a628..53457d1fb08b81 100644 --- a/src/transformers/models/auto/configuration_auto.py +++ b/src/transformers/models/auto/configuration_auto.py @@ -121,7 +121,6 @@ ("gpt_bigcode", "GPTBigCodeConfig"), ("gpt_neo", "GPTNeoConfig"), ("gpt_neox", "GPTNeoXConfig"), - ("moonshine", "MoonshineConfig"), ("gpt_neox_japanese", "GPTNeoXJapaneseConfig"), ("gptj", "GPTJConfig"), ("gptsan-japanese", "GPTSanJapaneseConfig"), @@ -181,6 +180,7 @@ ("mobilenet_v2", "MobileNetV2Config"), ("mobilevit", "MobileViTConfig"), ("mobilevitv2", "MobileViTV2Config"), + ("moonshine", "MoonshineConfig"), ("moshi", "MoshiConfig"), ("mpnet", "MPNetConfig"), ("mpt", "MptConfig"), @@ -426,7 +426,6 @@ ("gpt_bigcode", "GPTBigCode"), ("gpt_neo", "GPT Neo"), ("gpt_neox", "GPT NeoX"), - ("moonshine", "moonshine"), ("gpt_neox_japanese", "GPT NeoX Japanese"), ("gptj", "GPT-J"), ("gptsan-japanese", "GPTSAN-japanese"), @@ -496,6 +495,7 @@ ("mobilenet_v2", "MobileNetV2"), ("mobilevit", "MobileViT"), ("mobilevitv2", "MobileViTV2"), + ("moonshine", "moonshine"), ("moshi", "Moshi"), ("mpnet", "MPNet"), ("mpt", "MPT"), diff --git a/src/transformers/models/auto/modeling_auto.py b/src/transformers/models/auto/modeling_auto.py index b8258f972ff4b4..5cdcf88812ee03 100644 --- a/src/transformers/models/auto/modeling_auto.py +++ b/src/transformers/models/auto/modeling_auto.py @@ -118,7 +118,6 @@ ("gpt_bigcode", "GPTBigCodeModel"), ("gpt_neo", "GPTNeoModel"), ("gpt_neox", "GPTNeoXModel"), - ("moonshine", "MoonshineModel"), ("gpt_neox_japanese", "GPTNeoXJapaneseModel"), ("gptj", "GPTJModel"), ("gptsan-japanese", "GPTSanJapaneseForConditionalGeneration"), @@ -171,6 +170,7 @@ ("mobilenet_v2", "MobileNetV2Model"), ("mobilevit", "MobileViTModel"), ("mobilevitv2", "MobileViTV2Model"), + ("moonshine", "MoonshineModel"), ("moshi", "MoshiModel"), ("mpnet", "MPNetModel"), ("mpt", "MptModel"), @@ -409,7 +409,6 @@ ("gpt_bigcode", "GPTBigCodeForCausalLM"), ("gpt_neo", "GPTNeoForCausalLM"), ("gpt_neox", "GPTNeoXForCausalLM"), - ("moonshine", "MoonshineForCausalLM"), ("gpt_neox_japanese", "GPTNeoXJapaneseForCausalLM"), ("gptj", "GPTJForCausalLM"), ("gptsan-japanese", "GPTSanJapaneseForConditionalGeneration"), @@ -426,6 +425,7 @@ ("mega", "MegaForMaskedLM"), ("megatron-bert", "MegatronBertForCausalLM"), ("mobilebert", "MobileBertForMaskedLM"), + ("moonshine", "MoonshineForConditionalGeneration"), ("mpnet", "MPNetForMaskedLM"), ("mpt", "MptForCausalLM"), ("mra", "MraForMaskedLM"), @@ -496,7 +496,6 @@ ("gpt_bigcode", "GPTBigCodeForCausalLM"), ("gpt_neo", "GPTNeoForCausalLM"), ("gpt_neox", "GPTNeoXForCausalLM"), - ("moonshine", "MoonshineForCausalLM"), ("gpt_neox_japanese", "GPTNeoXJapaneseForCausalLM"), ("gptj", "GPTJForCausalLM"), ("granite", "GraniteForCausalLM"), @@ -954,7 +953,6 @@ ("gpt_bigcode", "GPTBigCodeForSequenceClassification"), ("gpt_neo", "GPTNeoForSequenceClassification"), ("gpt_neox", "GPTNeoXForSequenceClassification"), - ("moonshine", "MoonshineForSequenceClassification"), ("gptj", "GPTJForSequenceClassification"), ("ibert", "IBertForSequenceClassification"), ("jamba", "JambaForSequenceClassification"), @@ -1043,7 +1041,6 @@ ("gpt2", "GPT2ForQuestionAnswering"), ("gpt_neo", "GPTNeoForQuestionAnswering"), ("gpt_neox", "GPTNeoXForQuestionAnswering"), - ("moonshine", "MoonshineForQuestionAnswering"), ("gptj", "GPTJForQuestionAnswering"), ("ibert", "IBertForQuestionAnswering"), ("layoutlmv2", "LayoutLMv2ForQuestionAnswering"), @@ -1147,7 +1144,6 @@ ("gpt_bigcode", "GPTBigCodeForTokenClassification"), ("gpt_neo", "GPTNeoForTokenClassification"), ("gpt_neox", "GPTNeoXForTokenClassification"), - ("moonshine", "MoonshineForTokenClassification"), ("ibert", "IBertForTokenClassification"), ("layoutlm", "LayoutLMForTokenClassification"), ("layoutlmv2", "LayoutLMv2ForTokenClassification"), diff --git a/src/transformers/models/auto/processing_auto.py b/src/transformers/models/auto/processing_auto.py index c1f23bc1cb3f18..b4ceffec37c5b0 100644 --- a/src/transformers/models/auto/processing_auto.py +++ b/src/transformers/models/auto/processing_auto.py @@ -79,6 +79,7 @@ ("mctct", "MCTCTProcessor"), ("mgp-str", "MgpstrProcessor"), ("mllama", "MllamaProcessor"), + ("moonshine", "Wav2Vec2Processor"), ("oneformer", "OneFormerProcessor"), ("owlv2", "Owlv2Processor"), ("owlvit", "OwlViTProcessor"), diff --git a/src/transformers/models/auto/tokenization_auto.py b/src/transformers/models/auto/tokenization_auto.py index ea51f0c1302ab7..fc1fe2cc936868 100644 --- a/src/transformers/models/auto/tokenization_auto.py +++ b/src/transformers/models/auto/tokenization_auto.py @@ -310,8 +310,8 @@ ("mllama", ("LlamaTokenizer", "LlamaTokenizerFast" if is_tokenizers_available() else None)), ("mluke", ("MLukeTokenizer" if is_sentencepiece_available() else None, None)), ("mobilebert", ("MobileBertTokenizer", "MobileBertTokenizerFast" if is_tokenizers_available() else None)), - ("moshi", (None, "PreTrainedTokenizerFast" if is_tokenizers_available() else None)), ("moonshine", (None, "PreTrainedTokenizerFast" if is_tokenizers_available() else None)), + ("moshi", (None, "PreTrainedTokenizerFast" if is_tokenizers_available() else None)), ("mpnet", ("MPNetTokenizer", "MPNetTokenizerFast" if is_tokenizers_available() else None)), ("mpt", (None, "GPTNeoXTokenizerFast" if is_tokenizers_available() else None)), ("mra", ("RobertaTokenizer", "RobertaTokenizerFast" if is_tokenizers_available() else None)), diff --git a/src/transformers/models/moonshine/configuration_moonshine.py b/src/transformers/models/moonshine/configuration_moonshine.py index 44f043d6d3c3dd..a0a040d1d3d4c4 100644 --- a/src/transformers/models/moonshine/configuration_moonshine.py +++ b/src/transformers/models/moonshine/configuration_moonshine.py @@ -4,7 +4,6 @@ # the file from the modular. If any change should be done, please apply the change to the # modular_moonshine.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 - from ...configuration_utils import PretrainedConfig @@ -42,8 +41,6 @@ class MoonshineConfig(PretrainedConfig): The non-linear activation function (function or string) in the encoder. decoder_hidden_act (`str` or `function`, *optional*, defaults to `"silu"`): The non-linear activation function (function or string) in the decoder. - max_position_embeddings (`int`, *optional*, defaults to 2048): - The maximum sequence length that this model might ever be used with. TODO: check this initializer_range (`float`, *optional*, defaults to 0.02): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. layer_norm_eps (`float`, *optional*, defaults to 1e-5): @@ -56,10 +53,8 @@ class MoonshineConfig(PretrainedConfig): Whether or not the model should return the last key/values attentions (not used by all models). is_encoder_decoder (`bool`, *optional*, defaults to `True`): Whether the model is used as an encoder/decoder or not. - rope_theta (`float`, *optional*, defaults to 10000.0): - The base period of the RoPE embeddings. TODO: check this - partial_rotary_factor (`float`, *optional*, defaults to 0.5): - Percentage of the query and keys which will have rotary embedding. TODO: check this + min_rotary_ndims (`int`, *optional*, defaults to 32): + The minimum number of dimensions of the RoPE. ff_mult (`int`, *optional*, defaults to 4): Factor by which to scale the intermediate size. attention_bias (`bool`, *optional*, defaults to `False`): @@ -68,43 +63,6 @@ class MoonshineConfig(PretrainedConfig): The dropout ratio for the attention probabilities. qk_layernorm (`bool`, *optional*, defaults to `False`): Whether or not to normalize the Queries and Keys after projecting the hidden states. - rope_scaling (`Dict`, *optional*): - Dictionary containing the scaling configuration for the RoPE embeddings. NOTE: if you apply new rope type - and you expect the model to work on longer `max_position_embeddings`, we recommend you to update this value - accordingly. - Expected contents: - `rope_type` (`str`): - The sub-variant of RoPE to use. Can be one of ['default', 'linear', 'dynamic', 'yarn', 'longrope', - 'llama3'], with 'default' being the original RoPE implementation. - `factor` (`float`, *optional*): - Used with all rope types except 'default'. The scaling factor to apply to the RoPE embeddings. In - most scaling types, a `factor` of x will enable the model to handle sequences of length x * - original maximum pre-trained length. - `original_max_position_embeddings` (`int`, *optional*): - Used with 'dynamic', 'longrope' and 'llama3'. The original max position embeddings used during - pretraining. - `attention_factor` (`float`, *optional*): - Used with 'yarn' and 'longrope'. The scaling factor to be applied on the attention - computation. If unspecified, it defaults to value recommended by the implementation, using the - `factor` field to infer the suggested value. - `beta_fast` (`float`, *optional*): - Only used with 'yarn'. Parameter to set the boundary for extrapolation (only) in the linear - ramp function. If unspecified, it defaults to 32. - `beta_slow` (`float`, *optional*): - Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear - ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): - Only used with 'longrope'. The scaling factor to be applied to short contexts (< - `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden - size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): - Only used with 'longrope'. The scaling factor to be applied to long contexts (< - `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden - size divided by the number of attention heads divided by 2 - `low_freq_factor` (`float`, *optional*): - Only used with 'llama3'. Scaling factor applied to low frequency components of the RoPE - `high_freq_factor` (`float`, *optional*): - Only used with 'llama3'. Scaling factor applied to high frequency components of the RoPE bos_token_id (`int`, *optional*, defaults to 1): Denotes beginning of sequences token id. eos_token_id (`int`, *optional*, defaults to 2): @@ -167,18 +125,15 @@ def __init__( num_key_value_heads=None, encoder_hidden_act="gelu", decoder_hidden_act="silu", - max_position_embeddings=2048, initializer_range=0.02, layer_norm_eps=1e-5, decoder_start_token_id=1, use_cache=True, is_encoder_decoder=True, - rope_theta=10000.0, - partial_rotary_factor=0.5, + min_rotary_ndims=32, attention_bias=False, attention_dropout=0.0, qk_layernorm=False, - rope_scaling=None, ff_mult=4, bos_token_id=1, eos_token_id=2, @@ -203,19 +158,15 @@ def __init__( self.num_key_value_heads = num_key_value_heads self.encoder_hidden_act = encoder_hidden_act self.decoder_hidden_act = decoder_hidden_act - self.max_position_embeddings = max_position_embeddings self.initializer_range = initializer_range self.layer_norm_eps = layer_norm_eps self.decoder_start_token_id = decoder_start_token_id self.use_cache = use_cache self.is_encoder_decoder = is_encoder_decoder - self.rope_theta = rope_theta - self.partial_rotary_factor = partial_rotary_factor - + self.min_rotary_ndims = min_rotary_ndims self.attention_bias = attention_bias self.attention_dropout = attention_dropout self.qk_layernorm = qk_layernorm - self.rope_scaling = rope_scaling self.ff_mult = ff_mult # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 diff --git a/src/transformers/models/moonshine/modeling_moonshine.py b/src/transformers/models/moonshine/modeling_moonshine.py index 9f46520f40d86d..ab277c38566d1a 100644 --- a/src/transformers/models/moonshine/modeling_moonshine.py +++ b/src/transformers/models/moonshine/modeling_moonshine.py @@ -4,7 +4,6 @@ # the file from the modular. If any change should be done, please apply the change to the # modular_moonshine.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 - import copy import math from typing import List, Optional, Tuple, Union @@ -256,7 +255,8 @@ def __init__(self, config: MoonshineConfig, layer_idx: Optional[int] = None, is_ self.num_key_value_heads = config.num_key_value_heads self.num_key_value_groups = self.num_heads // self.num_key_value_heads self.rope_theta = config.rope_theta - self.rotary_ndims = max(config.hidden_size // config.num_attention_heads // 2, 32) + + self.rotary_ndims = max(config.hidden_size // config.num_attention_heads // 2, config.min_rotary_ndims) self.is_causal = is_causal @@ -278,11 +278,7 @@ def __init__(self, config: MoonshineConfig, layer_idx: Optional[int] = None, is_ self.k_layernorm = nn.LayerNorm( config.hidden_size // self.num_heads, eps=config.layer_norm_eps, elementwise_affine=True ) - - self.rotary_emb = MoonshineRotaryEmbedding( - dim=self.rotary_ndims, - max_position_embeddings=config.max_position_embeddings, - ) + self.rotary_emb = MoonshineRotaryEmbedding(dim=self.rotary_ndims) def forward( self, @@ -356,15 +352,14 @@ def forward( key_states[..., : self.rotary_ndims], key_states[..., self.rotary_ndims :], ) - # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor] + # [batch_size, seq_length, num_heads, self.rotary_ndims] query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin) # [batch_size, seq_length, num_heads, head_dim] query_states = torch.cat((query_rot, query_pass), dim=-1) key_states = torch.cat((key_rot, key_pass), dim=-1) - if past_key_value is not None: - if not is_cross_attention: + if past_key_value is not None: cache_kwargs = { "sin": sin, "cos": cos, @@ -497,15 +492,14 @@ def forward( key_states[..., : self.rotary_ndims], key_states[..., self.rotary_ndims :], ) - # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor] + # [batch_size, seq_length, num_heads, self.rotary_ndims] query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin) # [batch_size, seq_length, num_heads, head_dim] query_states = torch.cat((query_rot, query_pass), dim=-1) key_states = torch.cat((key_rot, key_pass), dim=-1) - if past_key_value is not None: - if not is_cross_attention: + if past_key_value is not None: cache_kwargs = { "sin": sin, "cos": cos, @@ -655,15 +649,14 @@ def forward( key_states[..., : self.rotary_ndims], key_states[..., self.rotary_ndims :], ) - # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor] + # [batch_size, seq_length, num_heads, self.rotary_ndims] query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin) # [batch_size, seq_length, num_heads, head_dim] query_states = torch.cat((query_rot, query_pass), dim=-1) key_states = torch.cat((key_rot, key_pass), dim=-1) - if past_key_value is not None: - if not is_cross_attention: + if past_key_value is not None: cache_kwargs = { "sin": sin, "cos": cos, @@ -1046,7 +1039,7 @@ class MoonshineEncoder(MoonshinePreTrainedModel): config: MoonshineConfig """ - main_input_name = "input_features" + main_input_name = "input_values" def __init__(self, config: MoonshineConfig): super().__init__(config) @@ -1059,8 +1052,7 @@ def __init__(self, config: MoonshineConfig): self.groupnorm = nn.GroupNorm(num_groups=1, num_channels=embed_dim, eps=1e-5) self.rotary_emb = MoonshineRotaryEmbedding( - dim=max(config.hidden_size // config.num_attention_heads // 2, 32), - max_position_embeddings=config.max_position_embeddings, + dim=max(config.hidden_size // config.num_attention_heads // 2, config.min_rotary_ndims) ) self.layers = nn.ModuleList([MoonshineEncoderLayer(config, idx) for idx in range(config.num_hidden_layers)]) @@ -1078,8 +1070,7 @@ def set_input_embeddings(self, value: nn.Module): @add_start_docstrings_to_model_forward(MOONSHINE_INPUTS_DOCSTRING) def forward( self, - input_features: Optional[torch.FloatTensor] = None, - attention_mask: Optional[torch.Tensor] = None, + input_values: Optional[torch.FloatTensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -1097,7 +1088,7 @@ 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_features is None) ^ (inputs_embeds is not None): + if (input_values is None) ^ (inputs_embeds is not None): raise ValueError("You must specify exactly one of input_ids or inputs_embeds") if self.gradient_checkpointing and self.training and use_cache: @@ -1107,7 +1098,7 @@ def forward( use_cache = False if inputs_embeds is None: - inputs_embeds = self.preprocess(input_features) + inputs_embeds = self.preprocess(input_values) # kept for BC (non `Cache` `past_key_values` inputs) return_legacy_cache = False @@ -1131,9 +1122,6 @@ def forward( if position_ids is None: position_ids = cache_position.unsqueeze(0) - causal_mask = self._update_causal_mask( - attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions - ) hidden_states = inputs_embeds # create position embeddings to be shared across the decoder layers @@ -1144,15 +1132,15 @@ def forward( all_self_attns = () if output_attentions else None next_decoder_cache = None - for decoder_layer in self.layers: + for encoder_layer in self.layers: if output_hidden_states: all_hidden_states += (hidden_states,) if self.gradient_checkpointing and self.training: layer_outputs = self._gradient_checkpointing_func( - decoder_layer.__call__, + encoder_layer.__call__, hidden_states, - causal_mask, + None, position_ids, past_key_values, output_attentions, @@ -1161,9 +1149,8 @@ def forward( position_embeddings, ) else: - layer_outputs = decoder_layer( + layer_outputs = encoder_layer( hidden_states, - attention_mask=causal_mask, position_ids=position_ids, past_key_value=past_key_values, output_attentions=output_attentions, @@ -1326,9 +1313,9 @@ def _freeze_parameters(self): param.requires_grad = False self._requires_grad = False - def preprocess(self, input_features: torch.FloatTensor): - input_features = input_features.unsqueeze(1) - inputs_embeds = nn.functional.tanh(self.conv1(input_features)) + def preprocess(self, input_values: torch.FloatTensor): + input_values = input_values.unsqueeze(1) + inputs_embeds = nn.functional.tanh(self.conv1(input_values)) inputs_embeds = self.groupnorm(inputs_embeds) inputs_embeds = nn.functional.gelu(self.conv2(inputs_embeds)) inputs_embeds = nn.functional.gelu(self.conv3(inputs_embeds)) @@ -1359,8 +1346,7 @@ def __init__(self, config: MoonshineConfig): ) self.norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) self.rotary_emb = MoonshineRotaryEmbedding( - dim=max(config.hidden_size // config.num_attention_heads // 2, 32), - max_position_embeddings=config.max_position_embeddings, + dim=max(config.hidden_size // config.num_attention_heads // 2, config.min_rotary_ndims) ) self.gradient_checkpointing = False @@ -1839,7 +1825,7 @@ def _mask_input_features( @replace_return_docstrings(output_type=Seq2SeqModelOutput, config_class=_CONFIG_FOR_DOC) def forward( self, - input_features: Optional[torch.FloatTensor] = None, + input_values: Optional[torch.FloatTensor] = None, attention_mask: Optional[torch.LongTensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.LongTensor] = None, @@ -1863,18 +1849,18 @@ def forward( ```python >>> import torch - >>> from transformers import AutoFeatureExtractor, WhisperModel + >>> from transformers import AutoFeatureExtractor, MoonshineModel >>> from datasets import load_dataset - >>> model = WhisperModel.from_pretrained("openai/whisper-base") - >>> feature_extractor = AutoFeatureExtractor.from_pretrained("openai/whisper-base") + >>> model = MoonshineModel.from_pretrained("UsefulSensors/moonshine-tiny") + >>> feature_extractor = AutoFeatureExtractor.from_pretrained("UsefulSensors/moonshine-tiny") >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") >>> inputs = feature_extractor(ds[0]["audio"]["array"], return_tensors="pt") - >>> input_features = inputs.input_features + >>> input_values = inputs.input_values >>> decoder_input_ids = torch.tensor([[1, 1]]) * model.config.decoder_start_token_id - >>> last_hidden_state = model(input_features, decoder_input_ids=decoder_input_ids).last_hidden_state + >>> last_hidden_state = model(input_values, decoder_input_ids=decoder_input_ids).last_hidden_state >>> list(last_hidden_state.shape) - [1, 2, 512] + [1, 2, 288] ```""" output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( @@ -1884,10 +1870,10 @@ def forward( return_dict = return_dict if return_dict is not None else self.config.use_return_dict if encoder_outputs is None: - input_features = self._mask_input_features(input_features, attention_mask=attention_mask) + input_values = self._mask_input_values(input_values, attention_mask=attention_mask) encoder_outputs = self.encoder( - input_features, + input_values, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, @@ -1981,7 +1967,7 @@ def encoder(self): @replace_return_docstrings(output_type=Seq2SeqLMOutput, config_class=_CONFIG_FOR_DOC) def forward( self, - input_features: Optional[torch.FloatTensor] = None, + input_values: Optional[torch.FloatTensor] = None, attention_mask: Optional[torch.LongTensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.LongTensor] = None, @@ -2000,7 +1986,7 @@ def forward( labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the language modeling loss. Indices should either be in `[0, ..., config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored (masked), the loss is - only computed for the tokens with labels in `[0, ..., config.vocab_size]`. `sequence_length` should be smaller than or equal to `config.max_target_positions`. + only computed for the tokens with labels in `[0, ..., config.vocab_size]`. Returns: @@ -2008,18 +1994,18 @@ def forward( ```python >>> import torch - >>> from transformers import AutoProcessor, WhisperForConditionalGeneration + >>> from transformers import AutoProcessor, MoonshineForConditionalGeneration >>> from datasets import load_dataset - >>> processor = AutoProcessor.from_pretrained("openai/whisper-tiny.en") - >>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny.en") + >>> processor = AutoProcessor.from_pretrained("UsefulSensors/moonshine") + >>> model = MoonshineForConditionalGeneration.from_pretrained("UsefulSensors/moonshine") >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") >>> inputs = processor(ds[0]["audio"]["array"], return_tensors="pt") - >>> input_features = inputs.input_features + >>> input_values = inputs.input_values - >>> generated_ids = model.generate(inputs=input_features) + >>> generated_ids = model.generate(input_values, max_new_tokens=100) >>> transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] >>> transcription @@ -2028,17 +2014,13 @@ def forward( return_dict = return_dict if return_dict is not None else self.config.use_return_dict if labels is not None: - if labels.shape[1] > self.max_target_positions: - raise ValueError( - f"Labels' sequence length {labels.shape[1]} cannot exceed the maximum allowed length of {self.max_target_positions} tokens." - ) if decoder_input_ids is None and decoder_inputs_embeds is None: decoder_input_ids = shift_tokens_right( labels, self.config.pad_token_id, self.config.decoder_start_token_id ) outputs = self.model( - input_features, + input_values, attention_mask=attention_mask, decoder_input_ids=decoder_input_ids, encoder_outputs=encoder_outputs, diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index 6d816249eca7f1..6aa9f96a36a3c8 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -1,48 +1,35 @@ -from ...configuration_utils import PretrainedConfig -from ..phi.modeling_phi import PhiAttention, PhiFlashAttention2, PhiSdpaAttention, PhiMLP, PhiRotaryEmbedding -from ..llama.modeling_llama import LlamaDecoderLayer, LlamaModel -from ..mistral.modeling_mistral import MistralMLP -from ..whisper.modeling_whisper import WhisperModel +import copy +import math +from typing import List, Optional, Tuple, Union +import torch +import torch.nn as nn from torch.nn import CrossEntropyLoss -from typing import List, Optional, Tuple, Union -from ...processing_utils import Unpack - +from ...activations import ACT2FN from ...cache_utils import Cache, DynamicCache, EncoderDecoderCache +from ...configuration_utils import PretrainedConfig from ...generation import GenerationMixin - +from ...modeling_flash_attention_utils import FlashAttentionKwargs, _flash_attention_forward from ...modeling_outputs import ( BaseModelOutput, BaseModelOutputWithPast, BaseModelOutputWithPastAndCrossAttentions, - CausalLMOutputWithCrossAttentions, Seq2SeqLMOutput, Seq2SeqModelOutput, - SequenceClassifierOutput, ) -from ...modeling_flash_attention_utils import FlashAttentionKwargs, _flash_attention_forward - from ...modeling_utils import PreTrainedModel - -import torch.nn as nn -import torch - +from ...processing_utils import Unpack from ...utils import ( add_start_docstrings, add_start_docstrings_to_model_forward, - is_flash_attn_2_available, - is_flash_attn_greater_or_equal_2_10, logging, replace_return_docstrings, ) +from ..llama.modeling_llama import LlamaDecoderLayer, LlamaModel +from ..phi.modeling_phi import PhiAttention, PhiFlashAttention2, PhiMLP, PhiRotaryEmbedding, PhiSdpaAttention +from ..whisper.modeling_whisper import WhisperModel -from typing import Optional, Tuple - -from ...activations import ACT2FN - -import copy -import math logger = logging.get_logger(__name__) @@ -83,8 +70,6 @@ class MoonshineConfig(PretrainedConfig): The non-linear activation function (function or string) in the encoder. decoder_hidden_act (`str` or `function`, *optional*, defaults to `"silu"`): The non-linear activation function (function or string) in the decoder. - max_position_embeddings (`int`, *optional*, defaults to 2048): - The maximum sequence length that this model might ever be used with. TODO: check this initializer_range (`float`, *optional*, defaults to 0.02): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. layer_norm_eps (`float`, *optional*, defaults to 1e-5): @@ -97,10 +82,8 @@ class MoonshineConfig(PretrainedConfig): Whether or not the model should return the last key/values attentions (not used by all models). is_encoder_decoder (`bool`, *optional*, defaults to `True`): Whether the model is used as an encoder/decoder or not. - rope_theta (`float`, *optional*, defaults to 10000.0): - The base period of the RoPE embeddings. TODO: check this - partial_rotary_factor (`float`, *optional*, defaults to 0.5): - Percentage of the query and keys which will have rotary embedding. TODO: check this + min_rotary_ndims (`int`, *optional*, defaults to 32): + The minimum number of dimensions of the RoPE. ff_mult (`int`, *optional*, defaults to 4): Factor by which to scale the intermediate size. attention_bias (`bool`, *optional*, defaults to `False`): @@ -109,43 +92,6 @@ class MoonshineConfig(PretrainedConfig): The dropout ratio for the attention probabilities. qk_layernorm (`bool`, *optional*, defaults to `False`): Whether or not to normalize the Queries and Keys after projecting the hidden states. - rope_scaling (`Dict`, *optional*): - Dictionary containing the scaling configuration for the RoPE embeddings. NOTE: if you apply new rope type - and you expect the model to work on longer `max_position_embeddings`, we recommend you to update this value - accordingly. - Expected contents: - `rope_type` (`str`): - The sub-variant of RoPE to use. Can be one of ['default', 'linear', 'dynamic', 'yarn', 'longrope', - 'llama3'], with 'default' being the original RoPE implementation. - `factor` (`float`, *optional*): - Used with all rope types except 'default'. The scaling factor to apply to the RoPE embeddings. In - most scaling types, a `factor` of x will enable the model to handle sequences of length x * - original maximum pre-trained length. - `original_max_position_embeddings` (`int`, *optional*): - Used with 'dynamic', 'longrope' and 'llama3'. The original max position embeddings used during - pretraining. - `attention_factor` (`float`, *optional*): - Used with 'yarn' and 'longrope'. The scaling factor to be applied on the attention - computation. If unspecified, it defaults to value recommended by the implementation, using the - `factor` field to infer the suggested value. - `beta_fast` (`float`, *optional*): - Only used with 'yarn'. Parameter to set the boundary for extrapolation (only) in the linear - ramp function. If unspecified, it defaults to 32. - `beta_slow` (`float`, *optional*): - Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear - ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): - Only used with 'longrope'. The scaling factor to be applied to short contexts (< - `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden - size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): - Only used with 'longrope'. The scaling factor to be applied to long contexts (< - `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden - size divided by the number of attention heads divided by 2 - `low_freq_factor` (`float`, *optional*): - Only used with 'llama3'. Scaling factor applied to low frequency components of the RoPE - `high_freq_factor` (`float`, *optional*): - Only used with 'llama3'. Scaling factor applied to high frequency components of the RoPE bos_token_id (`int`, *optional*, defaults to 1): Denotes beginning of sequences token id. eos_token_id (`int`, *optional*, defaults to 2): @@ -208,18 +154,15 @@ def __init__( num_key_value_heads=None, encoder_hidden_act="gelu", decoder_hidden_act="silu", - max_position_embeddings=2048, initializer_range=0.02, layer_norm_eps=1e-5, decoder_start_token_id=1, use_cache=True, is_encoder_decoder=True, - rope_theta=10000.0, - partial_rotary_factor=0.5, + min_rotary_ndims=32, attention_bias=False, attention_dropout=0.0, qk_layernorm=False, - rope_scaling=None, ff_mult=4, bos_token_id=1, eos_token_id=2, @@ -244,19 +187,15 @@ def __init__( self.num_key_value_heads = num_key_value_heads self.encoder_hidden_act = encoder_hidden_act self.decoder_hidden_act = decoder_hidden_act - self.max_position_embeddings = max_position_embeddings self.initializer_range = initializer_range self.layer_norm_eps = layer_norm_eps self.decoder_start_token_id = decoder_start_token_id self.use_cache = use_cache self.is_encoder_decoder = is_encoder_decoder - self.rope_theta = rope_theta - self.partial_rotary_factor = partial_rotary_factor - + self.min_rotary_ndims = min_rotary_ndims self.attention_bias = attention_bias self.attention_dropout = attention_dropout self.qk_layernorm = qk_layernorm - self.rope_scaling = rope_scaling self.ff_mult = ff_mult # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 @@ -356,7 +295,7 @@ def forward(self, x, position_ids): with torch.autocast(device_type=device_type, enabled=False): freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2) emb = torch.stack((freqs, freqs), dim=-1) - emb = emb.flatten(-2) # in einsum notation: rearrange(x, '... d j -> ... (d j)') + emb = emb.flatten(-2) # in einsum notation: rearrange(x, '... d j -> ... (d j)') cos = emb.cos() sin = emb.sin() @@ -394,11 +333,11 @@ def forward(self, hidden_state): hidden_state, gate = hidden_state.chunk(2, dim=-1) hidden_state = self.act_fn(gate) * hidden_state return self.down_proj(hidden_state) - + class MoonshineMLP: def __new__(cls, config: MoonshineConfig, hidden_act: str): - if hidden_act == "gelu": + if hidden_act == "gelu": return MoonshineNonGatedMLP(config, hidden_act) elif hidden_act == "silu": return MoonshineGatedMLP(config, hidden_act) @@ -413,15 +352,12 @@ def __init__(self, config: MoonshineConfig, layer_idx: Optional[int] = None, is_ self.k_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config.attention_bias) self.v_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config.attention_bias) self.dense = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=config.attention_bias) - self.rotary_ndims = max(config.hidden_size // config.num_attention_heads // 2, 32) - self.rotary_emb = MoonshineRotaryEmbedding( - dim=self.rotary_ndims, - max_position_embeddings=config.max_position_embeddings, - ) + self.rotary_ndims = max(config.hidden_size // config.num_attention_heads // 2, config.min_rotary_ndims) + self.rotary_emb = MoonshineRotaryEmbedding(dim=self.rotary_ndims) self.is_causal = is_causal - + def forward( self, hidden_states: torch.Tensor, @@ -437,7 +373,7 @@ def forward( bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states) - + # if key_value_states are provided this layer is used as a cross-attention layer # for the decoder is_cross_attention = key_value_states is not None @@ -463,7 +399,9 @@ def forward( key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) if is_cross_attention and past_key_value is not None: - key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, {"cache_position": cache_position}) + key_states, value_states = past_key_value.update( + key_states, value_states, self.layer_idx, {"cache_position": cache_position} + ) if self.qk_layernorm: query_states = self.q_layernorm(query_states) @@ -471,7 +409,7 @@ def forward( query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) - if not is_cross_attention: + if not is_cross_attention: if position_embeddings is None: logger.warning_once( "The attention layers in this model are transitioning from computing the RoPE embeddings internally " @@ -492,7 +430,7 @@ def forward( key_states[..., : self.rotary_ndims], key_states[..., self.rotary_ndims :], ) - # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor] + # [batch_size, seq_length, num_heads, self.rotary_ndims] query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin) # [batch_size, seq_length, num_heads, head_dim] @@ -506,7 +444,9 @@ def forward( "partial_rotation_size": self.rotary_ndims, "cache_position": cache_position, } - key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs) + key_states, value_states = past_key_value.update( + key_states, value_states, self.layer_idx, cache_kwargs + ) key_states = repeat_kv(key_states, self.num_key_value_groups) value_states = repeat_kv(value_states, self.num_key_value_groups) @@ -559,7 +499,7 @@ def forward( bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states) - + # if key_value_states are provided this layer is used as a cross-attention layer # for the decoder is_cross_attention = key_value_states is not None @@ -585,7 +525,9 @@ def forward( key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) if is_cross_attention and past_key_value is not None: - key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, {"cache_position": cache_position}) + key_states, value_states = past_key_value.update( + key_states, value_states, self.layer_idx, {"cache_position": cache_position} + ) if self.qk_layernorm: query_states = self.q_layernorm(query_states) @@ -593,7 +535,7 @@ def forward( query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) - if not is_cross_attention: + if not is_cross_attention: if position_embeddings is None: logger.warning_once( "The attention layers in this model are transitioning from computing the RoPE embeddings internally " @@ -614,7 +556,7 @@ def forward( key_states[..., : self.rotary_ndims], key_states[..., self.rotary_ndims :], ) - # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor] + # [batch_size, seq_length, num_heads, self.rotary_ndims] query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin) # [batch_size, seq_length, num_heads, head_dim] @@ -628,7 +570,9 @@ def forward( "partial_rotation_size": self.rotary_ndims, "cache_position": cache_position, } - key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs) + key_states, value_states = past_key_value.update( + key_states, value_states, 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. @@ -701,7 +645,7 @@ def forward( bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states) - + # if key_value_states are provided this layer is used as a cross-attention layer # for the decoder is_cross_attention = key_value_states is not None @@ -727,7 +671,9 @@ def forward( key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) if is_cross_attention and past_key_value is not None: - key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, {"cache_position": cache_position}) + key_states, value_states = past_key_value.update( + key_states, value_states, self.layer_idx, {"cache_position": cache_position} + ) if self.qk_layernorm: query_states = self.q_layernorm(query_states) @@ -735,7 +681,7 @@ def forward( query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) - if not is_cross_attention: + if not is_cross_attention: if position_embeddings is None: logger.warning_once( "The attention layers in this model are transitioning from computing the RoPE embeddings internally " @@ -756,7 +702,7 @@ def forward( key_states[..., : self.rotary_ndims], key_states[..., self.rotary_ndims :], ) - # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor] + # [batch_size, seq_length, num_heads, self.rotary_ndims] query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin) # [batch_size, seq_length, num_heads, head_dim] @@ -770,7 +716,9 @@ def forward( "partial_rotation_size": self.rotary_ndims, "cache_position": cache_position, } - key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs) + key_states, value_states = past_key_value.update( + key_states, value_states, self.layer_idx, cache_kwargs + ) key_states = repeat_kv(key_states, self.num_key_value_groups) value_states = repeat_kv(value_states, self.num_key_value_groups) @@ -818,7 +766,7 @@ def forward( class MoonshineEncoderLayer(LlamaDecoderLayer): def __init__(self, config: MoonshineConfig, layer_idx: int): super().__init__(config, layer_idx) - + self.mlp = MoonshineMLP(config, config.encoder_hidden_act) self.input_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) self.post_attention_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) @@ -829,10 +777,14 @@ def __init__(self, config: MoonshineConfig, layer_idx: int = None): super().__init__() self.hidden_size = config.hidden_size - self.self_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation](config=config, layer_idx=layer_idx, is_causal=True) - self.encoder_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation](config=config, layer_idx=layer_idx, is_causal=False) + self.self_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation]( + config=config, layer_idx=layer_idx, is_causal=True + ) + self.encoder_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation]( + config=config, layer_idx=layer_idx, is_causal=False + ) - self.mlp = MoonshineMLP(config, config.decoder_hidden_act) + self.mlp = MoonshineMLP(config, config.decoder_hidden_act) self.input_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) self.post_attention_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) self.final_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) @@ -935,7 +887,7 @@ def forward( outputs += (present_key_value,) return outputs - + MOONSHINE_START_DOCSTRING = r""" This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the @@ -983,7 +935,7 @@ def _init_weights(self, module): class MoonshineEncoder(LlamaModel, MoonshinePreTrainedModel): - main_input_name = "input_features" + main_input_name = "input_values" def __init__(self, config: MoonshineConfig): MoonshinePreTrainedModel.__init__(self, config) @@ -996,9 +948,8 @@ def __init__(self, config: MoonshineConfig): self.groupnorm = nn.GroupNorm(num_groups=1, num_channels=embed_dim, eps=1e-5) self.rotary_emb = MoonshineRotaryEmbedding( - dim=max(config.hidden_size // config.num_attention_heads // 2, 32), - max_position_embeddings=config.max_position_embeddings, - ) + dim=max(config.hidden_size // config.num_attention_heads // 2, config.min_rotary_ndims) + ) self.layers = nn.ModuleList([MoonshineEncoderLayer(config, idx) for idx in range(config.num_hidden_layers)]) self.layer_norm = nn.LayerNorm(embed_dim, eps=config.layer_norm_eps, bias=False) @@ -1016,19 +967,19 @@ def get_input_embeddings(self) -> nn.Module: def set_input_embeddings(self, value: nn.Module): self.conv1 = value - - def preprocess(self, input_features: torch.FloatTensor): - input_features = input_features.unsqueeze(1) - inputs_embeds = nn.functional.tanh(self.conv1(input_features)) + + def preprocess(self, input_values: torch.FloatTensor): + input_values = input_values.unsqueeze(1) + inputs_embeds = nn.functional.tanh(self.conv1(input_values)) inputs_embeds = self.groupnorm(inputs_embeds) inputs_embeds = nn.functional.gelu(self.conv2(inputs_embeds)) inputs_embeds = nn.functional.gelu(self.conv3(inputs_embeds)) - inputs_embeds = inputs_embeds.permute(0, 2, 1) + inputs_embeds = inputs_embeds.permute(0, 2, 1) return inputs_embeds - + def forward( self, - input_features: Optional[torch.FloatTensor] = None, + input_values: Optional[torch.FloatTensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -1046,7 +997,7 @@ 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_features is None) ^ (inputs_embeds is not None): + if (input_values is None) ^ (inputs_embeds is not None): raise ValueError("You must specify exactly one of input_ids or inputs_embeds") if self.gradient_checkpointing and self.training and use_cache: @@ -1056,7 +1007,7 @@ def forward( use_cache = False if inputs_embeds is None: - inputs_embeds = self.preprocess(input_features) + inputs_embeds = self.preprocess(input_values) # kept for BC (non `Cache` `past_key_values` inputs) return_legacy_cache = False @@ -1070,7 +1021,7 @@ def forward( "You should pass an instance of `EncoderDecoderCache` instead, e.g. " "`past_key_values=EncoderDecoderCache.from_legacy_cache(past_key_values)`." ) - past_key_values = EncoderDecoderCache.from_legacy_cache(past_key_values) + past_key_values = EncoderDecoderCache.from_legacy_cache(past_key_values) if cache_position is None: past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0 @@ -1226,8 +1177,7 @@ def __init__(self, config: MoonshineConfig): super().__init__(config) self.norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) self.rotary_emb = MoonshineRotaryEmbedding( - dim= max(config.hidden_size // config.num_attention_heads // 2, 32), - max_position_embeddings=config.max_position_embeddings, + dim=max(config.hidden_size // config.num_attention_heads // 2, config.min_rotary_ndims) ) def forward( @@ -1363,7 +1313,11 @@ def forward( next_cache = next_cache.to_legacy_cache() if not return_dict: - return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns, all_cross_attentions] if v is not None) + return tuple( + v + for v in [hidden_states, next_cache, all_hidden_states, all_self_attns, all_cross_attentions] + if v is not None + ) return BaseModelOutputWithPastAndCrossAttentions( last_hidden_state=hidden_states, past_key_values=next_cache, @@ -1371,7 +1325,7 @@ def forward( attentions=all_self_attns, cross_attentions=all_cross_attentions, ) - + class MoonshineModel(WhisperModel): def __init__(self, config: MoonshineConfig): @@ -1381,7 +1335,7 @@ def __init__(self, config: MoonshineConfig): def forward( self, - input_features: Optional[torch.FloatTensor] = None, + input_values: Optional[torch.FloatTensor] = None, attention_mask: Optional[torch.LongTensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.LongTensor] = None, @@ -1401,18 +1355,18 @@ def forward( Example: ```python >>> import torch - >>> from transformers import AutoFeatureExtractor, WhisperModel + >>> from transformers import AutoFeatureExtractor, MoonshineModel >>> from datasets import load_dataset - >>> model = WhisperModel.from_pretrained("openai/whisper-base") - >>> feature_extractor = AutoFeatureExtractor.from_pretrained("openai/whisper-base") + >>> model = MoonshineModel.from_pretrained("UsefulSensors/moonshine-tiny") + >>> feature_extractor = AutoFeatureExtractor.from_pretrained("UsefulSensors/moonshine-tiny") >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") >>> inputs = feature_extractor(ds[0]["audio"]["array"], return_tensors="pt") - >>> input_features = inputs.input_features + >>> input_values = inputs.input_values >>> decoder_input_ids = torch.tensor([[1, 1]]) * model.config.decoder_start_token_id - >>> last_hidden_state = model(input_features, decoder_input_ids=decoder_input_ids).last_hidden_state + >>> last_hidden_state = model(input_values, decoder_input_ids=decoder_input_ids).last_hidden_state >>> list(last_hidden_state.shape) - [1, 2, 512] + [1, 2, 288] ```""" output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( @@ -1422,10 +1376,10 @@ def forward( return_dict = return_dict if return_dict is not None else self.config.use_return_dict if encoder_outputs is None: - input_features = self._mask_input_features(input_features, attention_mask=attention_mask) + input_values = self._mask_input_values(input_values, attention_mask=attention_mask) encoder_outputs = self.encoder( - input_features, + input_values, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, @@ -1493,16 +1447,16 @@ def set_output_embeddings(self, new_embeddings): def get_input_embeddings(self) -> nn.Module: return self.model.get_input_embeddings() - + @property def encoder(self): return self.get_encoder() - + @add_start_docstrings_to_model_forward(MOONSHINE_INPUTS_DOCSTRING) @replace_return_docstrings(output_type=Seq2SeqLMOutput, config_class=_CONFIG_FOR_DOC) def forward( self, - input_features: Optional[torch.FloatTensor] = None, + input_values: Optional[torch.FloatTensor] = None, attention_mask: Optional[torch.LongTensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.LongTensor] = None, @@ -1521,7 +1475,7 @@ def forward( labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the language modeling loss. Indices should either be in `[0, ..., config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored (masked), the loss is - only computed for the tokens with labels in `[0, ..., config.vocab_size]`. `sequence_length` should be smaller than or equal to `config.max_target_positions`. + only computed for the tokens with labels in `[0, ..., config.vocab_size]`. Returns: @@ -1529,18 +1483,18 @@ def forward( ```python >>> import torch - >>> from transformers import AutoProcessor, WhisperForConditionalGeneration + >>> from transformers import AutoProcessor, MoonshineForConditionalGeneration >>> from datasets import load_dataset - >>> processor = AutoProcessor.from_pretrained("openai/whisper-tiny.en") - >>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny.en") + >>> processor = AutoProcessor.from_pretrained("UsefulSensors/moonshine") + >>> model = MoonshineForConditionalGeneration.from_pretrained("UsefulSensors/moonshine") >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") >>> inputs = processor(ds[0]["audio"]["array"], return_tensors="pt") - >>> input_features = inputs.input_features + >>> input_values = inputs.input_values - >>> generated_ids = model.generate(inputs=input_features) + >>> generated_ids = model.generate(input_values, max_new_tokens=100) >>> transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] >>> transcription @@ -1549,17 +1503,13 @@ def forward( return_dict = return_dict if return_dict is not None else self.config.use_return_dict if labels is not None: - if labels.shape[1] > self.max_target_positions: - raise ValueError( - f"Labels' sequence length {labels.shape[1]} cannot exceed the maximum allowed length of {self.max_target_positions} tokens." - ) if decoder_input_ids is None and decoder_inputs_embeds is None: decoder_input_ids = shift_tokens_right( labels, self.config.pad_token_id, self.config.decoder_start_token_id ) outputs = self.model( - input_features, + input_values, attention_mask=attention_mask, decoder_input_ids=decoder_input_ids, encoder_outputs=encoder_outputs, @@ -1596,4 +1546,4 @@ def forward( encoder_last_hidden_state=outputs.encoder_last_hidden_state, encoder_hidden_states=outputs.encoder_hidden_states, encoder_attentions=outputs.encoder_attentions, - ) \ No newline at end of file + ) From 3d52b1e8a6cee9f22ba2063d415b602c86faf593 Mon Sep 17 00:00:00 2001 From: eustlb <94853470+eustlb@users.noreply.github.com> Date: Mon, 16 Dec 2024 14:54:53 +0100 Subject: [PATCH 15/39] Update src/transformers/models/moonshine/convert_usefulsensors_to_hf.py Co-authored-by: Joshua Lochner --- .../moonshine/convert_usefulsensors_to_hf.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/transformers/models/moonshine/convert_usefulsensors_to_hf.py b/src/transformers/models/moonshine/convert_usefulsensors_to_hf.py index cf0010fd552ddd..c49ae161d11d00 100644 --- a/src/transformers/models/moonshine/convert_usefulsensors_to_hf.py +++ b/src/transformers/models/moonshine/convert_usefulsensors_to_hf.py @@ -129,6 +129,29 @@ def convert_usefulsensors_moonshine_to_hf(model_name, pytorch_dump_folder_path): converted_decoder_weights = _convert_weights(loaded_decoder_weights, encoder=False) converted_decoder_weights['embed_tokens.weight'] = converted_decoder_weights['embed_tokens.weight'].T + final_weights = {} + for k, v in encoder_state_dict.items(): + final_weights[f"model.encoder.{k}"] = v + + for k, v in converted_decoder_weights.items(): + final_weights[f"model.decoder.{k}"] = v + + if model_name == 'tiny': + config = MoonshineConfig() + elif model_name == 'base': + config = MoonshineConfig( + hidden_size=416, + num_hidden_layers=8, + num_attention_heads=8, + ) + else: + raise ValueError(f"Unknown model name {model_name}") + + final_weights['proj_out.weight'] = converted_decoder_weights['embed_tokens.weight'] + + model = MoonshineForConditionalGeneration(config) + model.load_state_dict(final_weights) + model.save_pretrained(pytorch_dump_folder_path) From 8f82a405c656c373e15496ce2526da440d47d110 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Mon, 16 Dec 2024 15:19:38 +0100 Subject: [PATCH 16/39] add rope_theta --- .../moonshine/configuration_moonshine.py | 4 ++ .../moonshine/convert_usefulsensors_to_hf.py | 45 +++++++++---------- .../models/moonshine/modular_moonshine.py | 4 ++ 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/transformers/models/moonshine/configuration_moonshine.py b/src/transformers/models/moonshine/configuration_moonshine.py index a0a040d1d3d4c4..05786abf846d60 100644 --- a/src/transformers/models/moonshine/configuration_moonshine.py +++ b/src/transformers/models/moonshine/configuration_moonshine.py @@ -55,6 +55,8 @@ class MoonshineConfig(PretrainedConfig): Whether the model is used as an encoder/decoder or not. min_rotary_ndims (`int`, *optional*, defaults to 32): The minimum number of dimensions of the RoPE. + rope_theta (`float`, *optional*, defaults to 10000.0): + The base period of the RoPE embeddings. ff_mult (`int`, *optional*, defaults to 4): Factor by which to scale the intermediate size. attention_bias (`bool`, *optional*, defaults to `False`): @@ -129,6 +131,7 @@ def __init__( layer_norm_eps=1e-5, decoder_start_token_id=1, use_cache=True, + rope_theta=10000.0, is_encoder_decoder=True, min_rotary_ndims=32, attention_bias=False, @@ -162,6 +165,7 @@ def __init__( self.layer_norm_eps = layer_norm_eps self.decoder_start_token_id = decoder_start_token_id self.use_cache = use_cache + self.rope_theta = rope_theta self.is_encoder_decoder = is_encoder_decoder self.min_rotary_ndims = min_rotary_ndims self.attention_bias = attention_bias diff --git a/src/transformers/models/moonshine/convert_usefulsensors_to_hf.py b/src/transformers/models/moonshine/convert_usefulsensors_to_hf.py index c49ae161d11d00..99fe729a31265e 100644 --- a/src/transformers/models/moonshine/convert_usefulsensors_to_hf.py +++ b/src/transformers/models/moonshine/convert_usefulsensors_to_hf.py @@ -15,15 +15,14 @@ # limitations under the License. import argparse -from huggingface_hub import hf_hub_download +import re import h5py -import torch import numpy as np -import re +import torch +from huggingface_hub import hf_hub_download -from transformers.models.moonshine.modeling_moonshine import MoonshineConfig -from transformers.models.moonshine.modeling_moonshine import MoonshineDecoder +from transformers.models.moonshine.modeling_moonshine import MoonshineConfig, MoonshineForConditionalGeneration # Copied from https://github.com/usefulsensors/moonshine/blob/a1d77cc573b0471ac4602b86f67b3f48d67df1a9/moonshine/model.py @@ -31,8 +30,7 @@ def _get_weights(model_name): repo = "UsefulSensors/moonshine" return ( - hf_hub_download(repo, f"{x}.weights.h5", subfolder=model_name) - for x in ("preprocessor", "encoder", "decoder") + hf_hub_download(repo, f"{x}.weights.h5", subfolder=model_name) for x in ("preprocessor", "encoder", "decoder") ) @@ -58,7 +56,12 @@ def _read_h5_weights(group, current_key="", weights={}): def _convert_layer_names(name, gated_mlp=False): - name = re.sub(r'layers\.functional(?:_(\d+))?\.layers', lambda m: f'layers.{m.group(1) if m.group(1) else "0"}', name, count=1) + name = re.sub( + r"layers\.functional(?:_(\d+))?\.layers", + lambda m: f'layers.{m.group(1) if m.group(1) else "0"}', + name, + count=1, + ) if gated_mlp: name = re.sub(r"functional\.layers\.dense\.", "mlp.up_proj.", name) name = re.sub(r"functional\.layers\.dense_1\.", "mlp.down_proj.", name) @@ -113,32 +116,32 @@ def _convert_weights(weights, encoder=True): def convert_usefulsensors_moonshine_to_hf(model_name, pytorch_dump_folder_path): preprocessor_weights_path, encoder_weights_path, decoder_weights_path = _get_weights(model_name) - - with h5py.File(preprocessor_weights_path, 'r') as f: + + with h5py.File(preprocessor_weights_path, "r") as f: loaded_preprocessor_weights = _read_h5_weights(f, weights={}) - with h5py.File(encoder_weights_path, 'r') as f: + with h5py.File(encoder_weights_path, "r") as f: loaded_encoder_weights = _read_h5_weights(f, weights={}) - with h5py.File(decoder_weights_path, 'r') as f: + with h5py.File(decoder_weights_path, "r") as f: loaded_decoder_weights = _read_h5_weights(f, weights={}) encoder_state_dict = {**loaded_encoder_weights, **loaded_preprocessor_weights} encoder_state_dict = _convert_weights(encoder_state_dict) converted_decoder_weights = _convert_weights(loaded_decoder_weights, encoder=False) - converted_decoder_weights['embed_tokens.weight'] = converted_decoder_weights['embed_tokens.weight'].T + converted_decoder_weights["embed_tokens.weight"] = converted_decoder_weights["embed_tokens.weight"].T final_weights = {} for k, v in encoder_state_dict.items(): final_weights[f"model.encoder.{k}"] = v - + for k, v in converted_decoder_weights.items(): final_weights[f"model.decoder.{k}"] = v - if model_name == 'tiny': + if model_name == "tiny": config = MoonshineConfig() - elif model_name == 'base': + elif model_name == "base": config = MoonshineConfig( hidden_size=416, num_hidden_layers=8, @@ -147,14 +150,13 @@ def convert_usefulsensors_moonshine_to_hf(model_name, pytorch_dump_folder_path): else: raise ValueError(f"Unknown model name {model_name}") - final_weights['proj_out.weight'] = converted_decoder_weights['embed_tokens.weight'] - + final_weights["proj_out.weight"] = converted_decoder_weights["embed_tokens.weight"] + model = MoonshineForConditionalGeneration(config) model.load_state_dict(final_weights) model.save_pretrained(pytorch_dump_folder_path) - if __name__ == "__main__": parser = argparse.ArgumentParser() # # Required parameters @@ -162,7 +164,4 @@ def convert_usefulsensors_moonshine_to_hf(model_name, pytorch_dump_folder_path): parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.") args = parser.parse_args() - convert_usefulsensors_moonshine_to_hf( - args.model_name, args.pytorch_dump_folder_path - ) - + convert_usefulsensors_moonshine_to_hf(args.model_name, args.pytorch_dump_folder_path) diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index 6aa9f96a36a3c8..597ae208c12586 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -84,6 +84,8 @@ class MoonshineConfig(PretrainedConfig): Whether the model is used as an encoder/decoder or not. min_rotary_ndims (`int`, *optional*, defaults to 32): The minimum number of dimensions of the RoPE. + rope_theta (`float`, *optional*, defaults to 10000.0): + The base period of the RoPE embeddings. ff_mult (`int`, *optional*, defaults to 4): Factor by which to scale the intermediate size. attention_bias (`bool`, *optional*, defaults to `False`): @@ -158,6 +160,7 @@ def __init__( layer_norm_eps=1e-5, decoder_start_token_id=1, use_cache=True, + rope_theta=10000.0, is_encoder_decoder=True, min_rotary_ndims=32, attention_bias=False, @@ -191,6 +194,7 @@ def __init__( self.layer_norm_eps = layer_norm_eps self.decoder_start_token_id = decoder_start_token_id self.use_cache = use_cache + self.rope_theta = rope_theta self.is_encoder_decoder = is_encoder_decoder self.min_rotary_ndims = min_rotary_ndims self.attention_bias = attention_bias From fc73b374ec4550561fa19d2f3dd3008e6bc93da5 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Mon, 16 Dec 2024 15:34:23 +0100 Subject: [PATCH 17/39] nits --- src/transformers/models/moonshine/modular_moonshine.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index 597ae208c12586..0808031c90a39d 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -41,7 +41,7 @@ class MoonshineConfig(PretrainedConfig): This is the configuration class to store the configuration of a [`MoonshineModel`]. It is used to instantiate a Moonshine model according to the specified arguments, defining the model architecture. Instantiating a configuration with the defaults will yield a similar configuration to that of the Moonshine - [UsefulSensors/moonshine](https://huggingface.co/UsefulSensors/moonshine). + [UsefulSensors/moonshine-tiny](https://huggingface.co/UsefulSensors/moonshine-tiny). Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information. @@ -134,7 +134,7 @@ class MoonshineConfig(PretrainedConfig): >>> from transformers import MoonshineModel, MoonshineConfig >>> # Initializing a Moonshine style configuration - >>> configuration = MoonshineConfig().from_pretrained("UsefulSensors/moonshine") + >>> configuration = MoonshineConfig().from_pretrained("UsefulSensors/moonshine-tiny") >>> # Initializing a model from the configuration >>> model = MoonshineModel(configuration) @@ -1490,8 +1490,8 @@ def forward( >>> from transformers import AutoProcessor, MoonshineForConditionalGeneration >>> from datasets import load_dataset - >>> processor = AutoProcessor.from_pretrained("UsefulSensors/moonshine") - >>> model = MoonshineForConditionalGeneration.from_pretrained("UsefulSensors/moonshine") + >>> processor = AutoProcessor.from_pretrained("UsefulSensors/moonshine-tiny") + >>> model = MoonshineForConditionalGeneration.from_pretrained("UsefulSensors/moonshine-tiny") >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") From aedccf5eb74f80b6616f6ffbf62bdeba3c81a0f1 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Mon, 16 Dec 2024 17:41:04 +0100 Subject: [PATCH 18/39] model doc --- docs/source/en/model_doc/moonshine.md | 64 ++++----------------------- 1 file changed, 8 insertions(+), 56 deletions(-) diff --git a/docs/source/en/model_doc/moonshine.md b/docs/source/en/model_doc/moonshine.md index bf5f1255f03c2d..a5025e6107510f 100644 --- a/docs/source/en/model_doc/moonshine.md +++ b/docs/source/en/model_doc/moonshine.md @@ -20,65 +20,27 @@ rendered properly in your Markdown viewer. ## Overview -The moonshine model was proposed in []() by . - +The moonshine model was proposed in [Moonshine: Speech Recognition for Live Transcription and Voice Commands +](https://arxiv.org/abs/2410.15608) by Nat Jeffries, Evan King, Manjunath Kudlur, Guy Nicholson, James Wang, Pete Warden. The abstract from the paper is the following: -** +This paper introduces Moonshine, a family of speech recognition models optimized for live transcription and voice command processing. Moonshine is based on an encoder-decoder transformer architecture and employs Rotary Position Embedding (RoPE) instead of traditional absolute position embeddings. The model is trained on speech segments of various lengths, but without using zero-padding, leading to greater efficiency for the encoder during inference time. When benchmarked against OpenAI's Whisper tiny-en, Moonshine Tiny demonstrates a 5x reduction in compute requirements for transcribing a 10-second speech segment while incurring no increase in word error rates across standard evaluation datasets. These results highlight Moonshine's potential for real-time and resource-constrained applications. Tips: - +- Moonshine improves upon Whisper's architecture: + 1. It uses SwiGLU activation instead of GELU in the decoder layers + 2. Most importantly, it replaces absolute position embeddings with Rotary Position Embeddings (RoPE). This allows Moonshine to handle audio inputs of any length, unlike Whisper which is restricted to fixed 30-second windows. -This model was contributed by [INSERT YOUR HF USERNAME HERE](https://huggingface.co/). -The original code can be found [here](). +This model was contributed by [Eustache Le Bihan (eustlb)](https://huggingface.co/eustlb). +The original code can be found [here](https://github.com/usefulsensors/moonshine). ## MoonshineConfig [[autodoc]] MoonshineConfig -## MoonshineTokenizer - -[[autodoc]] MoonshineTokenizer - - set_prefix_tokens - - build_inputs_with_special_tokens - - get_special_tokens_mask - - create_token_type_ids_from_sequences - - save_vocabulary - - batch_decode - - decode - - basic_normalize - - normalize - -## MoonshineTokenizerFast - -[[autodoc]] MoonshineTokenizerFast - - set_prefix_tokens - - build_inputs_with_special_tokens - - get_special_tokens_mask - - create_token_type_ids_from_sequences - - save_vocabulary - - batch_decode - - decode - - basic_normalize - - normalize - -## MoonshineFeatureExtractor - -[[autodoc]] MoonshineFeatureExtractor - - __call__ - -## MoonshineProcessor - -[[autodoc]] MoonshineProcessor - - __call__ - - from_pretrained - - save_pretrained - - batch_decode - - decode - @@ -94,15 +56,5 @@ The original code can be found [here](). - forward - generate -## MoonshineForCausalLM - -[[autodoc]] MoonshineForCausalLM - - forward - -## MoonshineForAudioClassification - -[[autodoc]] MoonshineForAudioClassification - - forward - From 095413386895c622bee891928f62c4305a0015b5 Mon Sep 17 00:00:00 2001 From: eustlb <94853470+eustlb@users.noreply.github.com> Date: Tue, 17 Dec 2024 10:52:49 +0100 Subject: [PATCH 19/39] Update src/transformers/models/auto/configuration_auto.py Co-authored-by: Joshua Lochner --- src/transformers/models/auto/configuration_auto.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformers/models/auto/configuration_auto.py b/src/transformers/models/auto/configuration_auto.py index 53457d1fb08b81..f9826e5d34675d 100644 --- a/src/transformers/models/auto/configuration_auto.py +++ b/src/transformers/models/auto/configuration_auto.py @@ -495,7 +495,7 @@ ("mobilenet_v2", "MobileNetV2"), ("mobilevit", "MobileViT"), ("mobilevitv2", "MobileViTV2"), - ("moonshine", "moonshine"), + ("moonshine", "Moonshine"), ("moshi", "Moshi"), ("mpnet", "MPNet"), ("mpt", "MPT"), From b1f0909d1573114b8a3da0169af40a56a4c377ad Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Tue, 17 Dec 2024 10:57:58 +0100 Subject: [PATCH 20/39] imports --- src/transformers/__init__.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/transformers/__init__.py b/src/transformers/__init__.py index e89407c7eeaa10..43547fef8c5e18 100755 --- a/src/transformers/__init__.py +++ b/src/transformers/__init__.py @@ -842,6 +842,7 @@ "WhisperProcessor", "WhisperTokenizer", ], + "models.moonshine": ["MoonshineConfig"], "models.x_clip": [ "XCLIPConfig", "XCLIPProcessor", @@ -3722,6 +3723,13 @@ "WhisperPreTrainedModel", ] ) + _import_structure["models.moonshine"].extend( + [ + "MoonshineForConditionalGeneration", + "MoonshineModel", + "MoonshinePreTrainedModel", + ] + ) _import_structure["models.x_clip"].extend( [ "XCLIPModel", @@ -5784,6 +5792,7 @@ WhisperProcessor, WhisperTokenizer, ) + from .models.moonshine import MoonshineConfig from .models.x_clip import ( XCLIPConfig, XCLIPProcessor, @@ -8163,6 +8172,11 @@ WhisperModel, WhisperPreTrainedModel, ) + from .models.moonshine import ( + MoonshineModel, + MoonshineForConditionalGeneration, + MoonshinePreTrainedModel, + ) from .models.x_clip import ( XCLIPModel, XCLIPPreTrainedModel, From f647a9ffc44eee6e673f8f1bf45dffa08ff08219 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Tue, 17 Dec 2024 11:00:04 +0100 Subject: [PATCH 21/39] add MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES --- src/transformers/models/auto/modeling_auto.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/transformers/models/auto/modeling_auto.py b/src/transformers/models/auto/modeling_auto.py index 5cdcf88812ee03..3a1d33b0b6caf4 100644 --- a/src/transformers/models/auto/modeling_auto.py +++ b/src/transformers/models/auto/modeling_auto.py @@ -915,6 +915,7 @@ ("speech_to_text", "Speech2TextForConditionalGeneration"), ("speecht5", "SpeechT5ForSpeechToText"), ("whisper", "WhisperForConditionalGeneration"), + ("moonshine", "MoonshineForConditionalGeneration"), ] ) From 2da255d64682fed25e833dba8ee2c9faf6b26702 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Tue, 17 Dec 2024 14:05:31 +0100 Subject: [PATCH 22/39] updates modular --- .../moonshine/configuration_moonshine.py | 4 +- .../models/moonshine/modeling_moonshine.py | 431 +++++++++--------- .../models/moonshine/modular_moonshine.py | 314 +++++++++---- 3 files changed, 453 insertions(+), 296 deletions(-) diff --git a/src/transformers/models/moonshine/configuration_moonshine.py b/src/transformers/models/moonshine/configuration_moonshine.py index 05786abf846d60..efa5063ef66c5f 100644 --- a/src/transformers/models/moonshine/configuration_moonshine.py +++ b/src/transformers/models/moonshine/configuration_moonshine.py @@ -12,7 +12,7 @@ class MoonshineConfig(PretrainedConfig): This is the configuration class to store the configuration of a [`MoonshineModel`]. It is used to instantiate a Moonshine model according to the specified arguments, defining the model architecture. Instantiating a configuration with the defaults will yield a similar configuration to that of the Moonshine - [UsefulSensors/moonshine](https://huggingface.co/UsefulSensors/moonshine). + [UsefulSensors/moonshine-tiny](https://huggingface.co/UsefulSensors/moonshine-tiny). Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information. @@ -105,7 +105,7 @@ class MoonshineConfig(PretrainedConfig): >>> from transformers import MoonshineModel, MoonshineConfig >>> # Initializing a Moonshine style configuration - >>> configuration = MoonshineConfig().from_pretrained("UsefulSensors/moonshine") + >>> configuration = MoonshineConfig().from_pretrained("UsefulSensors/moonshine-tiny") >>> # Initializing a model from the configuration >>> model = MoonshineModel(configuration) diff --git a/src/transformers/models/moonshine/modeling_moonshine.py b/src/transformers/models/moonshine/modeling_moonshine.py index ab277c38566d1a..ab17bc08edb70e 100644 --- a/src/transformers/models/moonshine/modeling_moonshine.py +++ b/src/transformers/models/moonshine/modeling_moonshine.py @@ -952,88 +952,13 @@ def _init_weights(self, module): module.weight.data[module.padding_idx].zero_() -MOONSHINE_INPUTS_DOCSTRING = r""" - Args: - input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): - Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide - it. - - Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and - [`PreTrainedTokenizer.__call__`] for details. - - [What are input IDs?](../glossary#input-ids) - attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): - Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`: - - - 1 for tokens that are **not masked**, - - 0 for tokens that are **masked**. - - [What are attention masks?](../glossary#attention-mask) - - Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and - [`PreTrainedTokenizer.__call__`] for details. - - If `past_key_values` is used, optionally only the last `input_ids` have to be input (see - `past_key_values`). - - If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`] - and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more - information on the default strategy. - - - 1 indicates the head is **not masked**, - - 0 indicates the head is **masked**. - position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): - Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0, - config.n_positions - 1]`. - - [What are position IDs?](../glossary#position-ids) - 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, see our - [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache); - - 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)`. - inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): - 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. - 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`). - output_attentions (`bool`, *optional*): - Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned - tensors for more detail. - output_hidden_states (`bool`, *optional*): - Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for - 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. -""" - - @add_start_docstrings( - "The bare Moonshine Model outputting raw hidden-states without any specific head on top.", + "The bare Moonshine encoder outputting raw hidden-states.", MOONSHINE_START_DOCSTRING, ) class MoonshineEncoder(MoonshinePreTrainedModel): """ - Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`MoonshineDecoderLayer`] + Transformer encoder consisting of *config.num_hidden_layers* layers. Each layer is a [`MoonshineEncoderLayer`] Args: config: MoonshineConfig @@ -1061,16 +986,30 @@ def __init__(self, config: MoonshineConfig): self.gradient_checkpointing = False self.post_init() - def get_input_embeddings(self): + def _freeze_parameters(self): + for param in self.parameters(): + param.requires_grad = False + self._requires_grad = False + + def get_input_embeddings(self) -> nn.Module: return self.conv1 def set_input_embeddings(self, value: nn.Module): self.conv1 = value - @add_start_docstrings_to_model_forward(MOONSHINE_INPUTS_DOCSTRING) + def preprocess(self, input_values: torch.FloatTensor): + input_values = input_values.unsqueeze(1) + inputs_embeds = nn.functional.tanh(self.conv1(input_values)) + inputs_embeds = self.groupnorm(inputs_embeds) + inputs_embeds = nn.functional.gelu(self.conv2(inputs_embeds)) + inputs_embeds = nn.functional.gelu(self.conv3(inputs_embeds)) + inputs_embeds = inputs_embeds.permute(0, 2, 1) + return inputs_embeds + def forward( self, input_values: Optional[torch.FloatTensor] = None, + attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -1081,6 +1020,58 @@ def forward( cache_position: Optional[torch.LongTensor] = None, **flash_attn_kwargs: Unpack[FlashAttentionKwargs], ) -> Union[Tuple, BaseModelOutputWithPast]: + r""" + Args: + input_values (`torch.FloatTensor` of shape `(batch_size, audio_length)`): + Float values of the raw speech waveform. Raw speech waveform can be + obtained by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a + `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into + `input_values`, the [`AutoFeatureExtractor`] should be used for padding + and conversion into a tensor of type `torch.FloatTensor`. + attention_mask (`torch.Tensor`)`, *optional*): + Moonshine does not support masking of the `input_values`, this argument is preserved for compatibility, + but it is not used. + position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): + Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0, + config.n_positions - 1]`. + + [What are position IDs?](../glossary#position-ids) + 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, see our + [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache); + - 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)`. + inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): + Optionally, instead of passing `input_values` you can choose to directly pass an embedded representation, where embedded + here refers to preprocessed input values that can be obtained by passing `input_values` to the encoder `preprocess` method. + use_cache (`bool`, *optional*): + If set to `True`, `past_key_values` key value states are returned (see `past_key_values`). + output_attentions (`bool`, *optional*): + Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned + tensors for more detail. + output_hidden_states (`bool`, *optional*): + Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for + 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. + """ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1089,7 +1080,7 @@ def forward( return_dict = return_dict if return_dict is not None else self.config.use_return_dict if (input_values is None) ^ (inputs_embeds is not None): - raise ValueError("You must specify exactly one of input_ids or inputs_embeds") + raise ValueError("You must specify exactly one of input_values or inputs_embeds") if self.gradient_checkpointing and self.training and use_cache: logger.warning_once( @@ -1130,7 +1121,7 @@ def forward( # decoder layers all_hidden_states = () if output_hidden_states else None all_self_attns = () if output_attentions else None - next_decoder_cache = None + next_encoder_cache = None for encoder_layer in self.layers: if output_hidden_states: @@ -1163,7 +1154,7 @@ def forward( hidden_states = layer_outputs[0] if use_cache: - next_decoder_cache = layer_outputs[2 if output_attentions else 1] + next_encoder_cache = layer_outputs[2 if output_attentions else 1] if output_attentions: all_self_attns += (layer_outputs[1],) @@ -1174,7 +1165,7 @@ def forward( if output_hidden_states: all_hidden_states += (hidden_states,) - next_cache = next_decoder_cache if use_cache else None + next_cache = next_encoder_cache if use_cache else None if return_legacy_cache: next_cache = next_cache.to_legacy_cache() @@ -1187,144 +1178,84 @@ def forward( attentions=all_self_attns, ) - def _update_causal_mask( - self, - attention_mask: torch.Tensor, - input_tensor: torch.Tensor, - cache_position: torch.Tensor, - past_key_values: Cache, - output_attentions: bool, - ): - 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) +MOONSHINE_INPUTS_DOCSTRING = r""" + Args: + input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): + Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide + it. - # 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 + Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and + [`PreTrainedTokenizer.__call__`] for details. - dtype, device = input_tensor.dtype, input_tensor.device - sequence_length = input_tensor.shape[1] - if using_static_cache: - target_length = past_key_values.get_max_cache_shape() - else: - target_length = ( - attention_mask.shape[-1] - if isinstance(attention_mask, torch.Tensor) - else past_seen_tokens + sequence_length + 1 - ) + [What are input IDs?](../glossary#input-ids) + attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): + Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`: - # In case the provided `attention` mask is 2D, we generate a causal mask here (4D). - causal_mask = self._prepare_4d_causal_attention_mask_with_cache_position( - attention_mask, - sequence_length=sequence_length, - target_length=target_length, - dtype=dtype, - device=device, - cache_position=cache_position, - batch_size=input_tensor.shape[0], - ) + - 1 for tokens that are **not masked**, + - 0 for tokens that are **masked**. - 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 - min_dtype = torch.finfo(dtype).min - causal_mask = AttentionMaskConverter._unmask_unattended(causal_mask, min_dtype) + [What are attention masks?](../glossary#attention-mask) - return causal_mask + Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and + [`PreTrainedTokenizer.__call__`] for details. - @staticmethod - 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, - cache_position: torch.Tensor, - batch_size: int, - **kwargs, - ): - """ - 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. + If `past_key_values` is used, optionally only the last `input_ids` have to be input (see + `past_key_values`). - 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. - 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: - min_dtype = torch.finfo(dtype).min - 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 - ) + If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`] + and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more + information on the default strategy. - return causal_mask + - 1 indicates the head is **not masked**, + - 0 indicates the head is **masked**. + position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): + Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0, + config.n_positions - 1]`. - def _freeze_parameters(self): - for param in self.parameters(): - param.requires_grad = False - self._requires_grad = False + [What are position IDs?](../glossary#position-ids) + 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`. - def preprocess(self, input_values: torch.FloatTensor): - input_values = input_values.unsqueeze(1) - inputs_embeds = nn.functional.tanh(self.conv1(input_values)) - inputs_embeds = self.groupnorm(inputs_embeds) - inputs_embeds = nn.functional.gelu(self.conv2(inputs_embeds)) - inputs_embeds = nn.functional.gelu(self.conv3(inputs_embeds)) - inputs_embeds = inputs_embeds.permute(0, 2, 1) - return inputs_embeds + Two formats are allowed: + - a [`~cache_utils.Cache`] instance, see our + [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache); + - 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)`. + inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): + 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. + 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`). + output_attentions (`bool`, *optional*): + Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned + tensors for more detail. + output_hidden_states (`bool`, *optional*): + Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for + 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. +""" @add_start_docstrings( - "The bare Moonshine Model outputting raw hidden-states without any specific head on top.", + "The bare Moonshine decoder outputting raw hidden-states without any specific head on top.", MOONSHINE_START_DOCSTRING, ) class MoonshineDecoder(MoonshinePreTrainedModel): @@ -1376,6 +1307,85 @@ def forward( cache_position: Optional[torch.LongTensor] = None, **flash_attn_kwargs: Unpack[FlashAttentionKwargs], ) -> Union[Tuple, BaseModelOutputWithPast]: + """ + Args: + input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): + Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide + it. + + Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and + [`PreTrainedTokenizer.__call__`] for details. + + [What are input IDs?](../glossary#input-ids) + attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): + Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`: + + - 1 for tokens that are **not masked**, + - 0 for tokens that are **masked**. + + [What are attention masks?](../glossary#attention-mask) + + Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and + [`PreTrainedTokenizer.__call__`] for details. + + If `past_key_values` is used, optionally only the last `input_ids` have to be input (see + `past_key_values`). + + If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`] + and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more + information on the default strategy. + + - 1 indicates the head is **not masked**, + - 0 indicates the head is **masked**. + encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, encoder_sequence_length, hidden_size)`, *optional*): + Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention + of the decoder. + position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): + Indices of positions of each input sequence tokens in the position embeddings. + + [What are position IDs?](../glossary#position-ids) + encoder_position_ids (`torch.LongTensor` of shape `(batch_size, encoder_sequence_length)`, *optional*): + Indices of positions of each encoder input's hidden states in the position embeddings. + + [What are position IDs?](../glossary#position-ids) + 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, see our + [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache); + - 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)`. + inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): + 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. + 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`). + output_attentions (`bool`, *optional*): + Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned + tensors for more detail. + output_hidden_states (`bool`, *optional*): + Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for + 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. + """ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1437,7 +1447,6 @@ 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 for decoder_layer in self.layers: if output_hidden_states: @@ -1870,7 +1879,7 @@ def forward( return_dict = return_dict if return_dict is not None else self.config.use_return_dict if encoder_outputs is None: - input_values = self._mask_input_values(input_values, attention_mask=attention_mask) + input_values = self._mask_input_features(input_values, attention_mask=attention_mask) encoder_outputs = self.encoder( input_values, @@ -1997,8 +2006,8 @@ def forward( >>> from transformers import AutoProcessor, MoonshineForConditionalGeneration >>> from datasets import load_dataset - >>> processor = AutoProcessor.from_pretrained("UsefulSensors/moonshine") - >>> model = MoonshineForConditionalGeneration.from_pretrained("UsefulSensors/moonshine") + >>> processor = AutoProcessor.from_pretrained("UsefulSensors/moonshine-tiny") + >>> model = MoonshineForConditionalGeneration.from_pretrained("UsefulSensors/moonshine-tiny") >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index 0808031c90a39d..1f718524f6658d 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -938,11 +938,97 @@ def _init_weights(self, module): module.weight.data[module.padding_idx].zero_() -class MoonshineEncoder(LlamaModel, MoonshinePreTrainedModel): +MOONSHINE_INPUTS_DOCSTRING = r""" + Args: + input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): + Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide + it. + + Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and + [`PreTrainedTokenizer.__call__`] for details. + + [What are input IDs?](../glossary#input-ids) + attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): + Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`: + + - 1 for tokens that are **not masked**, + - 0 for tokens that are **masked**. + + [What are attention masks?](../glossary#attention-mask) + + Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and + [`PreTrainedTokenizer.__call__`] for details. + + If `past_key_values` is used, optionally only the last `input_ids` have to be input (see + `past_key_values`). + + If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`] + and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more + information on the default strategy. + + - 1 indicates the head is **not masked**, + - 0 indicates the head is **masked**. + position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): + Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0, + config.n_positions - 1]`. + + [What are position IDs?](../glossary#position-ids) + 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, see our + [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache); + - 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)`. + inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): + 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. + 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`). + output_attentions (`bool`, *optional*): + Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned + tensors for more detail. + output_hidden_states (`bool`, *optional*): + Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for + 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. +""" + + +@add_start_docstrings( + "The bare Moonshine encoder outputting raw hidden-states.", + MOONSHINE_START_DOCSTRING, +) +class MoonshineEncoder(MoonshinePreTrainedModel): + """ + Transformer encoder consisting of *config.num_hidden_layers* layers. Each layer is a [`MoonshineEncoderLayer`] + + Args: + config: MoonshineConfig + """ + main_input_name = "input_values" def __init__(self, config: MoonshineConfig): - MoonshinePreTrainedModel.__init__(self, config) + super().__init__(config) self.config = config embed_dim = config.hidden_size @@ -984,6 +1070,7 @@ def preprocess(self, input_values: torch.FloatTensor): def forward( self, input_values: Optional[torch.FloatTensor] = None, + attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -994,6 +1081,58 @@ def forward( cache_position: Optional[torch.LongTensor] = None, **flash_attn_kwargs: Unpack[FlashAttentionKwargs], ) -> Union[Tuple, BaseModelOutputWithPast]: + r""" + Args: + input_values (`torch.FloatTensor` of shape `(batch_size, audio_length)`): + Float values of the raw speech waveform. Raw speech waveform can be + obtained by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a + `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into + `input_values`, the [`AutoFeatureExtractor`] should be used for padding + and conversion into a tensor of type `torch.FloatTensor`. + attention_mask (`torch.Tensor`)`, *optional*): + Moonshine does not support masking of the `input_values`, this argument is preserved for compatibility, + but it is not used. + position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): + Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0, + config.n_positions - 1]`. + + [What are position IDs?](../glossary#position-ids) + 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, see our + [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache); + - 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)`. + inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): + Optionally, instead of passing `input_values` you can choose to directly pass an embedded representation, where embedded + here refers to preprocessed input values that can be obtained by passing `input_values` to the encoder `preprocess` method. + use_cache (`bool`, *optional*): + If set to `True`, `past_key_values` key value states are returned (see `past_key_values`). + output_attentions (`bool`, *optional*): + Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned + tensors for more detail. + output_hidden_states (`bool`, *optional*): + Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for + 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. + """ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1002,7 +1141,7 @@ def forward( return_dict = return_dict if return_dict is not None else self.config.use_return_dict if (input_values is None) ^ (inputs_embeds is not None): - raise ValueError("You must specify exactly one of input_ids or inputs_embeds") + raise ValueError("You must specify exactly one of input_values or inputs_embeds") if self.gradient_checkpointing and self.training and use_cache: logger.warning_once( @@ -1043,7 +1182,7 @@ def forward( # decoder layers all_hidden_states = () if output_hidden_states else None all_self_attns = () if output_attentions else None - next_decoder_cache = None + next_encoder_cache = None for encoder_layer in self.layers: if output_hidden_states: @@ -1076,7 +1215,7 @@ def forward( hidden_states = layer_outputs[0] if use_cache: - next_decoder_cache = layer_outputs[2 if output_attentions else 1] + next_encoder_cache = layer_outputs[2 if output_attentions else 1] if output_attentions: all_self_attns += (layer_outputs[1],) @@ -1087,7 +1226,7 @@ def forward( if output_hidden_states: all_hidden_states += (hidden_states,) - next_cache = next_decoder_cache if use_cache else None + next_cache = next_encoder_cache if use_cache else None if return_legacy_cache: next_cache = next_cache.to_legacy_cache() @@ -1101,81 +1240,10 @@ def forward( ) -MOONSHINE_INPUTS_DOCSTRING = r""" - Args: - input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): - Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide - it. - - Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and - [`PreTrainedTokenizer.__call__`] for details. - - [What are input IDs?](../glossary#input-ids) - attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): - Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`: - - - 1 for tokens that are **not masked**, - - 0 for tokens that are **masked**. - - [What are attention masks?](../glossary#attention-mask) - - Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and - [`PreTrainedTokenizer.__call__`] for details. - - If `past_key_values` is used, optionally only the last `input_ids` have to be input (see - `past_key_values`). - - If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`] - and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more - information on the default strategy. - - - 1 indicates the head is **not masked**, - - 0 indicates the head is **masked**. - position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): - Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0, - config.n_positions - 1]`. - - [What are position IDs?](../glossary#position-ids) - 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, see our - [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache); - - 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)`. - inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): - 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. - 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`). - output_attentions (`bool`, *optional*): - Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned - tensors for more detail. - output_hidden_states (`bool`, *optional*): - Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for - 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. -""" - - +@add_start_docstrings( + "The bare Moonshine decoder outputting raw hidden-states without any specific head on top.", + MOONSHINE_START_DOCSTRING, +) class MoonshineDecoder(LlamaModel): def __init__(self, config: MoonshineConfig): super().__init__(config) @@ -1200,6 +1268,85 @@ def forward( cache_position: Optional[torch.LongTensor] = None, **flash_attn_kwargs: Unpack[FlashAttentionKwargs], ) -> Union[Tuple, BaseModelOutputWithPast]: + """ + Args: + input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): + Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide + it. + + Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and + [`PreTrainedTokenizer.__call__`] for details. + + [What are input IDs?](../glossary#input-ids) + attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): + Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`: + + - 1 for tokens that are **not masked**, + - 0 for tokens that are **masked**. + + [What are attention masks?](../glossary#attention-mask) + + Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and + [`PreTrainedTokenizer.__call__`] for details. + + If `past_key_values` is used, optionally only the last `input_ids` have to be input (see + `past_key_values`). + + If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`] + and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more + information on the default strategy. + + - 1 indicates the head is **not masked**, + - 0 indicates the head is **masked**. + encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, encoder_sequence_length, hidden_size)`, *optional*): + Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention + of the decoder. + position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): + Indices of positions of each input sequence tokens in the position embeddings. + + [What are position IDs?](../glossary#position-ids) + encoder_position_ids (`torch.LongTensor` of shape `(batch_size, encoder_sequence_length)`, *optional*): + Indices of positions of each encoder input's hidden states in the position embeddings. + + [What are position IDs?](../glossary#position-ids) + 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, see our + [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache); + - 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)`. + inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): + 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. + 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`). + output_attentions (`bool`, *optional*): + Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned + tensors for more detail. + output_hidden_states (`bool`, *optional*): + Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for + 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. + """ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1261,7 +1408,6 @@ 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 for decoder_layer in self.layers: if output_hidden_states: @@ -1337,6 +1483,8 @@ def __init__(self, config: MoonshineConfig): self.encoder = MoonshineEncoder(config) self.decoder = MoonshineDecoder(config) + @add_start_docstrings_to_model_forward(MOONSHINE_INPUTS_DOCSTRING) + @replace_return_docstrings(output_type=Seq2SeqModelOutput, config_class=_CONFIG_FOR_DOC) def forward( self, input_values: Optional[torch.FloatTensor] = None, @@ -1380,7 +1528,7 @@ def forward( return_dict = return_dict if return_dict is not None else self.config.use_return_dict if encoder_outputs is None: - input_values = self._mask_input_values(input_values, attention_mask=attention_mask) + input_values = self._mask_input_features(input_values, attention_mask=attention_mask) encoder_outputs = self.encoder( input_values, From 407cd3ed515eaf5a98513487c7bf563295d67a6a Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Tue, 17 Dec 2024 14:05:44 +0100 Subject: [PATCH 23/39] make --- src/transformers/__init__.py | 8 +------- src/transformers/models/auto/modeling_auto.py | 2 +- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/transformers/__init__.py b/src/transformers/__init__.py index 43547fef8c5e18..507454b0450931 100755 --- a/src/transformers/__init__.py +++ b/src/transformers/__init__.py @@ -463,7 +463,6 @@ "models.gpt_bigcode": ["GPTBigCodeConfig"], "models.gpt_neo": ["GPTNeoConfig"], "models.gpt_neox": ["GPTNeoXConfig"], - "models.moonshine": ["MoonshineConfig"], "models.gpt_neox_japanese": ["GPTNeoXJapaneseConfig"], "models.gpt_sw3": [], "models.gptj": ["GPTJConfig"], @@ -5792,7 +5791,6 @@ WhisperProcessor, WhisperTokenizer, ) - from .models.moonshine import MoonshineConfig from .models.x_clip import ( XCLIPConfig, XCLIPProcessor, @@ -7466,6 +7464,7 @@ ) from .models.moonshine import ( MoonshineForCausalLM, + MoonshineForConditionalGeneration, MoonshineForQuestionAnswering, MoonshineForSequenceClassification, MoonshineForTokenClassification, @@ -8172,11 +8171,6 @@ WhisperModel, WhisperPreTrainedModel, ) - from .models.moonshine import ( - MoonshineModel, - MoonshineForConditionalGeneration, - MoonshinePreTrainedModel, - ) from .models.x_clip import ( XCLIPModel, XCLIPPreTrainedModel, diff --git a/src/transformers/models/auto/modeling_auto.py b/src/transformers/models/auto/modeling_auto.py index 3a1d33b0b6caf4..ab9d02d7d3f23a 100644 --- a/src/transformers/models/auto/modeling_auto.py +++ b/src/transformers/models/auto/modeling_auto.py @@ -908,6 +908,7 @@ MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES = OrderedDict( [ + ("moonshine", "MoonshineForConditionalGeneration"), ("pop2piano", "Pop2PianoForConditionalGeneration"), ("seamless_m4t", "SeamlessM4TForSpeechToText"), ("seamless_m4t_v2", "SeamlessM4Tv2ForSpeechToText"), @@ -915,7 +916,6 @@ ("speech_to_text", "Speech2TextForConditionalGeneration"), ("speecht5", "SpeechT5ForSpeechToText"), ("whisper", "WhisperForConditionalGeneration"), - ("moonshine", "MoonshineForConditionalGeneration"), ] ) From b3da73da3228feb0347394d30b7183afde1538af Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Tue, 17 Dec 2024 14:19:41 +0100 Subject: [PATCH 24/39] make fix-copies --- docs/source/en/index.md | 1 + src/transformers/__init__.py | 20 ++---------------- .../moonshine/configuration_moonshine.py | 14 ++++++------- src/transformers/utils/dummy_pt_objects.py | 21 +++++++++++++++++++ 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/docs/source/en/index.md b/docs/source/en/index.md index 341cb417c7b8ac..3605379ee435d0 100644 --- a/docs/source/en/index.md +++ b/docs/source/en/index.md @@ -224,6 +224,7 @@ Flax), PyTorch, and/or TensorFlow. | [MobileNetV2](model_doc/mobilenet_v2) | ✅ | ❌ | ❌ | | [MobileViT](model_doc/mobilevit) | ✅ | ✅ | ❌ | | [MobileViTV2](model_doc/mobilevitv2) | ✅ | ❌ | ❌ | +| [Moonshine](model_doc/moonshine) | ✅ | ❌ | ❌ | | [Moshi](model_doc/moshi) | ✅ | ❌ | ❌ | | [MPNet](model_doc/mpnet) | ✅ | ✅ | ❌ | | [MPT](model_doc/mpt) | ✅ | ❌ | ❌ | diff --git a/src/transformers/__init__.py b/src/transformers/__init__.py index 507454b0450931..eae7b69b437e53 100755 --- a/src/transformers/__init__.py +++ b/src/transformers/__init__.py @@ -1082,7 +1082,6 @@ _import_structure["models.gemma"].append("GemmaTokenizerFast") _import_structure["models.gpt2"].append("GPT2TokenizerFast") _import_structure["models.gpt_neox"].append("GPTNeoXTokenizerFast") - _import_structure["models.moonshine"].append("MoonshineTokenizer") _import_structure["models.gpt_neox_japanese"].append("GPTNeoXJapaneseTokenizer") _import_structure["models.herbert"].append("HerbertTokenizerFast") _import_structure["models.layoutlm"].append("LayoutLMTokenizerFast") @@ -2364,10 +2363,7 @@ ) _import_structure["models.moonshine"].extend( [ - "MoonshineForCausalLM", - "MoonshineForQuestionAnswering", - "MoonshineForSequenceClassification", - "MoonshineForTokenClassification", + "MoonshineForConditionalGeneration", "MoonshineModel", "MoonshinePreTrainedModel", ] @@ -3722,13 +3718,6 @@ "WhisperPreTrainedModel", ] ) - _import_structure["models.moonshine"].extend( - [ - "MoonshineForConditionalGeneration", - "MoonshineModel", - "MoonshinePreTrainedModel", - ] - ) _import_structure["models.x_clip"].extend( [ "XCLIPModel", @@ -6043,7 +6032,6 @@ from .models.mbart import MBartTokenizerFast from .models.mbart50 import MBart50TokenizerFast from .models.mobilebert import MobileBertTokenizerFast - from .models.moonshine import MoonshineTokenizer from .models.mpnet import MPNetTokenizerFast from .models.mt5 import MT5TokenizerFast from .models.mvp import MvpTokenizerFast @@ -7463,11 +7451,7 @@ MobileViTV2PreTrainedModel, ) from .models.moonshine import ( - MoonshineForCausalLM, - MoonshineForConditionalGeneration, - MoonshineForQuestionAnswering, - MoonshineForSequenceClassification, - MoonshineForTokenClassification, + MoonshineForConditionalGeneration, MoonshineModel, MoonshinePreTrainedModel, ) diff --git a/src/transformers/models/moonshine/configuration_moonshine.py b/src/transformers/models/moonshine/configuration_moonshine.py index efa5063ef66c5f..344b8109dd4140 100644 --- a/src/transformers/models/moonshine/configuration_moonshine.py +++ b/src/transformers/models/moonshine/configuration_moonshine.py @@ -43,7 +43,7 @@ class MoonshineConfig(PretrainedConfig): The non-linear activation function (function or string) in the decoder. initializer_range (`float`, *optional*, defaults to 0.02): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. - layer_norm_eps (`float`, *optional*, defaults to 1e-5): + layer_norm_eps (`float`, *optional*, defaults to 1e-05): The epsilon used by the layer normalization layers. decoder_start_token_id (`int`, *optional*, defaults to 1): Corresponds to the "<|startoftranscript|>" token, which is automatically used when no `decoder_input_ids` @@ -51,20 +51,20 @@ class MoonshineConfig(PretrainedConfig): the task. use_cache (`bool`, *optional*, defaults to `True`): Whether or not the model should return the last key/values attentions (not used by all models). + rope_theta (`float`, *optional*, defaults to 10000.0): + The base period of the RoPE embeddings. is_encoder_decoder (`bool`, *optional*, defaults to `True`): Whether the model is used as an encoder/decoder or not. min_rotary_ndims (`int`, *optional*, defaults to 32): The minimum number of dimensions of the RoPE. - rope_theta (`float`, *optional*, defaults to 10000.0): - The base period of the RoPE embeddings. - ff_mult (`int`, *optional*, defaults to 4): - Factor by which to scale the intermediate size. attention_bias (`bool`, *optional*, defaults to `False`): Whether to use a bias in the query, key, value and output projection layers during self-attention. attention_dropout (`float`, *optional*, defaults to 0.0): The dropout ratio for the attention probabilities. qk_layernorm (`bool`, *optional*, defaults to `False`): Whether or not to normalize the Queries and Keys after projecting the hidden states. + ff_mult (`int`, *optional*, defaults to 4): + Factor by which to scale the intermediate size. bos_token_id (`int`, *optional*, defaults to 1): Denotes beginning of sequences token id. eos_token_id (`int`, *optional*, defaults to 2): @@ -81,10 +81,10 @@ class MoonshineConfig(PretrainedConfig): actual percentage of masked vectors. This is only relevant if `apply_spec_augment == True`. mask_time_length (`int`, *optional*, defaults to 10): Length of vector span along the time axis. - mask_time_min_masks (`int`, *optional*, defaults to 2),: The minimum number of masks of length `mask_feature_length` generated along the time axis, each time step, irrespectively of `mask_feature_prob`. Only relevant if ''mask_time_prob*len(time_axis)/mask_time_length < mask_time_min_masks'' + mask_time_min_masks (``, *optional*, defaults to 2): mask_feature_prob (`float`, *optional*, defaults to 0.0): Percentage (between 0 and 1) of all feature vectors along the feature axis which will be masked. The masking procecure generates `mask_feature_prob*len(feature_axis)/mask_time_length` independent masks over @@ -94,10 +94,10 @@ class MoonshineConfig(PretrainedConfig): True`. mask_feature_length (`int`, *optional*, defaults to 10): Length of vector span along the feature axis. - mask_feature_min_masks (`int`, *optional*, defaults to 0),: The minimum number of masks of length `mask_feature_length` generated along the feature axis, each time step, irrespectively of `mask_feature_prob`. Only relevant if `mask_feature_prob*len(feature_axis)/mask_feature_length < mask_feature_min_masks`. + mask_feature_min_masks (``, *optional*, defaults to 0): Example: diff --git a/src/transformers/utils/dummy_pt_objects.py b/src/transformers/utils/dummy_pt_objects.py index 3bf6d6eb288a9a..b37b82d148df55 100644 --- a/src/transformers/utils/dummy_pt_objects.py +++ b/src/transformers/utils/dummy_pt_objects.py @@ -6289,6 +6289,27 @@ def __init__(self, *args, **kwargs): requires_backends(self, ["torch"]) +class MoonshineForConditionalGeneration(metaclass=DummyObject): + _backends = ["torch"] + + def __init__(self, *args, **kwargs): + requires_backends(self, ["torch"]) + + +class MoonshineModel(metaclass=DummyObject): + _backends = ["torch"] + + def __init__(self, *args, **kwargs): + requires_backends(self, ["torch"]) + + +class MoonshinePreTrainedModel(metaclass=DummyObject): + _backends = ["torch"] + + def __init__(self, *args, **kwargs): + requires_backends(self, ["torch"]) + + class MoshiForCausalLM(metaclass=DummyObject): _backends = ["torch"] From 8cf195d87ea6dc82fd2435adfebb30af1a2ab1eb Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Tue, 17 Dec 2024 14:26:27 +0100 Subject: [PATCH 25/39] ruff check examples fix --- src/transformers/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformers/__init__.py b/src/transformers/__init__.py index eae7b69b437e53..88b5e29e090b69 100755 --- a/src/transformers/__init__.py +++ b/src/transformers/__init__.py @@ -7451,7 +7451,7 @@ MobileViTV2PreTrainedModel, ) from .models.moonshine import ( - MoonshineForConditionalGeneration, + MoonshineForConditionalGeneration, MoonshineModel, MoonshinePreTrainedModel, ) From f78db3d43d24fef0a8471fcfd6380851593d94d1 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Tue, 17 Dec 2024 14:43:39 +0100 Subject: [PATCH 26/39] fix check_modular_conversion --- .../models/moonshine/modular_moonshine.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index 1f718524f6658d..ddd2e864a46f47 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -72,7 +72,7 @@ class MoonshineConfig(PretrainedConfig): The non-linear activation function (function or string) in the decoder. initializer_range (`float`, *optional*, defaults to 0.02): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. - layer_norm_eps (`float`, *optional*, defaults to 1e-5): + layer_norm_eps (`float`, *optional*, defaults to 1e-05): The epsilon used by the layer normalization layers. decoder_start_token_id (`int`, *optional*, defaults to 1): Corresponds to the "<|startoftranscript|>" token, which is automatically used when no `decoder_input_ids` @@ -80,20 +80,20 @@ class MoonshineConfig(PretrainedConfig): the task. use_cache (`bool`, *optional*, defaults to `True`): Whether or not the model should return the last key/values attentions (not used by all models). + rope_theta (`float`, *optional*, defaults to 10000.0): + The base period of the RoPE embeddings. is_encoder_decoder (`bool`, *optional*, defaults to `True`): Whether the model is used as an encoder/decoder or not. min_rotary_ndims (`int`, *optional*, defaults to 32): The minimum number of dimensions of the RoPE. - rope_theta (`float`, *optional*, defaults to 10000.0): - The base period of the RoPE embeddings. - ff_mult (`int`, *optional*, defaults to 4): - Factor by which to scale the intermediate size. attention_bias (`bool`, *optional*, defaults to `False`): Whether to use a bias in the query, key, value and output projection layers during self-attention. attention_dropout (`float`, *optional*, defaults to 0.0): The dropout ratio for the attention probabilities. qk_layernorm (`bool`, *optional*, defaults to `False`): Whether or not to normalize the Queries and Keys after projecting the hidden states. + ff_mult (`int`, *optional*, defaults to 4): + Factor by which to scale the intermediate size. bos_token_id (`int`, *optional*, defaults to 1): Denotes beginning of sequences token id. eos_token_id (`int`, *optional*, defaults to 2): @@ -110,10 +110,10 @@ class MoonshineConfig(PretrainedConfig): actual percentage of masked vectors. This is only relevant if `apply_spec_augment == True`. mask_time_length (`int`, *optional*, defaults to 10): Length of vector span along the time axis. - mask_time_min_masks (`int`, *optional*, defaults to 2),: The minimum number of masks of length `mask_feature_length` generated along the time axis, each time step, irrespectively of `mask_feature_prob`. Only relevant if ''mask_time_prob*len(time_axis)/mask_time_length < mask_time_min_masks'' + mask_time_min_masks (``, *optional*, defaults to 2): mask_feature_prob (`float`, *optional*, defaults to 0.0): Percentage (between 0 and 1) of all feature vectors along the feature axis which will be masked. The masking procecure generates `mask_feature_prob*len(feature_axis)/mask_time_length` independent masks over @@ -123,10 +123,10 @@ class MoonshineConfig(PretrainedConfig): True`. mask_feature_length (`int`, *optional*, defaults to 10): Length of vector span along the feature axis. - mask_feature_min_masks (`int`, *optional*, defaults to 0),: The minimum number of masks of length `mask_feature_length` generated along the feature axis, each time step, irrespectively of `mask_feature_prob`. Only relevant if `mask_feature_prob*len(feature_axis)/mask_feature_length < mask_feature_min_masks`. + mask_feature_min_masks (``, *optional*, defaults to 0): Example: From b680f0d172b2b0b88b23b608c123fe977d95a20f Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Tue, 17 Dec 2024 15:45:55 +0100 Subject: [PATCH 27/39] nit --- src/transformers/__init__.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/transformers/__init__.py b/src/transformers/__init__.py index 2442c55b2df6b8..8280ee0b1d5050 100755 --- a/src/transformers/__init__.py +++ b/src/transformers/__init__.py @@ -605,6 +605,7 @@ "models.mobilenet_v2": ["MobileNetV2Config"], "models.mobilevit": ["MobileViTConfig"], "models.mobilevitv2": ["MobileViTV2Config"], + "models.moonshine": ["MoonshineConfig"], "models.moshi": [ "MoshiConfig", "MoshiDepthConfig", @@ -854,7 +855,6 @@ "WhisperProcessor", "WhisperTokenizer", ], - "models.moonshine": ["MoonshineConfig"], "models.x_clip": [ "XCLIPConfig", "XCLIPProcessor", @@ -2408,13 +2408,6 @@ "GPTNeoXPreTrainedModel", ] ) - _import_structure["models.moonshine"].extend( - [ - "MoonshineForConditionalGeneration", - "MoonshineModel", - "MoonshinePreTrainedModel", - ] - ) _import_structure["models.gpt_neox_japanese"].extend( [ "GPTNeoXJapaneseForCausalLM", @@ -2869,6 +2862,13 @@ "MobileViTV2PreTrainedModel", ] ) + _import_structure["models.moonshine"].extend( + [ + "MoonshineForConditionalGeneration", + "MoonshineModel", + "MoonshinePreTrainedModel", + ] + ) _import_structure["models.moshi"].extend( [ "MoshiForCausalLM", From 01a4ea17314eba70db07a527935a71be69436a78 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Tue, 17 Dec 2024 16:48:29 +0100 Subject: [PATCH 28/39] nits --- .../moonshine/configuration_moonshine.py | 10 ++++- .../models/moonshine/modeling_moonshine.py | 41 ++++++++----------- .../models/moonshine/modular_moonshine.py | 41 ++++++++++--------- 3 files changed, 48 insertions(+), 44 deletions(-) diff --git a/src/transformers/models/moonshine/configuration_moonshine.py b/src/transformers/models/moonshine/configuration_moonshine.py index 344b8109dd4140..ceca76d6b44651 100644 --- a/src/transformers/models/moonshine/configuration_moonshine.py +++ b/src/transformers/models/moonshine/configuration_moonshine.py @@ -84,7 +84,10 @@ class MoonshineConfig(PretrainedConfig): The minimum number of masks of length `mask_feature_length` generated along the time axis, each time step, irrespectively of `mask_feature_prob`. Only relevant if ''mask_time_prob*len(time_axis)/mask_time_length < mask_time_min_masks'' - mask_time_min_masks (``, *optional*, defaults to 2): + mask_time_min_masks (`int`, *optional*, defaults to 2): + The minimum number of masks of length `mask_feature_length` generated along the time axis, each time step, + irrespectively of `mask_feature_prob`. Only relevant if ''mask_time_prob*len(time_axis)/mask_time_length < + mask_time_min_masks'' mask_feature_prob (`float`, *optional*, defaults to 0.0): Percentage (between 0 and 1) of all feature vectors along the feature axis which will be masked. The masking procecure generates `mask_feature_prob*len(feature_axis)/mask_time_length` independent masks over @@ -97,7 +100,10 @@ class MoonshineConfig(PretrainedConfig): The minimum number of masks of length `mask_feature_length` generated along the feature axis, each time step, irrespectively of `mask_feature_prob`. Only relevant if `mask_feature_prob*len(feature_axis)/mask_feature_length < mask_feature_min_masks`. - mask_feature_min_masks (``, *optional*, defaults to 0): + mask_feature_min_masks (`int`, *optional*, defaults to 0): + The minimum number of masks of length `mask_feature_length` generated along the feature axis, each time + step, irrespectively of `mask_feature_prob`. Only relevant if + `mask_feature_prob*len(feature_axis)/mask_feature_length < mask_feature_min_masks`. Example: diff --git a/src/transformers/models/moonshine/modeling_moonshine.py b/src/transformers/models/moonshine/modeling_moonshine.py index ab17bc08edb70e..b0529a1e308c77 100644 --- a/src/transformers/models/moonshine/modeling_moonshine.py +++ b/src/transformers/models/moonshine/modeling_moonshine.py @@ -33,7 +33,6 @@ add_start_docstrings, add_start_docstrings_to_model_forward, get_torch_version, - is_flash_attn_2_available, is_flash_attn_greater_or_equal_2_10, logging, replace_return_docstrings, @@ -41,10 +40,6 @@ from .configuration_moonshine import MoonshineConfig -if is_flash_attn_2_available(): - from ...modeling_flash_attention_utils import _flash_attention_forward - - logger = logging.get_logger(__name__) _CONFIG_FOR_DOC = "MoonshineConfig" @@ -730,7 +725,7 @@ def forward( output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 + position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs, ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: """ @@ -1279,7 +1274,10 @@ def __init__(self, config: MoonshineConfig): self.rotary_emb = MoonshineRotaryEmbedding( dim=max(config.hidden_size // config.num_attention_heads // 2, config.min_rotary_ndims) ) + self.gradient_checkpointing = False + if getattr(config, "pretraining_tp", 1) != 1: + logger.warn("`pretraining_tp` is deprecated, please use `model.tensor_parallel` instead.") # Initialize weights and apply final processing self.post_init() @@ -1851,26 +1849,23 @@ def forward( r""" Returns: - Example: - Returns: - Example: ```python - >>> import torch - >>> from transformers import AutoFeatureExtractor, MoonshineModel - >>> from datasets import load_dataset - - >>> model = MoonshineModel.from_pretrained("UsefulSensors/moonshine-tiny") - >>> feature_extractor = AutoFeatureExtractor.from_pretrained("UsefulSensors/moonshine-tiny") - >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") - >>> inputs = feature_extractor(ds[0]["audio"]["array"], return_tensors="pt") - >>> input_values = inputs.input_values - >>> decoder_input_ids = torch.tensor([[1, 1]]) * model.config.decoder_start_token_id - >>> last_hidden_state = model(input_values, decoder_input_ids=decoder_input_ids).last_hidden_state - >>> list(last_hidden_state.shape) - [1, 2, 288] - ```""" + >>> import torch + >>> from transformers import AutoFeatureExtractor, MoonshineModel + >>> from datasets import load_dataset + + >>> model = MoonshineModel.from_pretrained("UsefulSensors/moonshine-tiny") + >>> feature_extractor = AutoFeatureExtractor.from_pretrained("UsefulSensors/moonshine-tiny") + >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") + >>> inputs = feature_extractor(ds[0]["audio"]["array"], return_tensors="pt") + >>> input_values = inputs.input_values + >>> decoder_input_ids = torch.tensor([[1, 1]]) * model.config.decoder_start_token_id + >>> last_hidden_state = model(input_values, decoder_input_ids=decoder_input_ids).last_hidden_state + >>> list(last_hidden_state.shape) + [1, 2, 288] + ```""" output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index ddd2e864a46f47..dfc07c943a5ace 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -113,7 +113,10 @@ class MoonshineConfig(PretrainedConfig): The minimum number of masks of length `mask_feature_length` generated along the time axis, each time step, irrespectively of `mask_feature_prob`. Only relevant if ''mask_time_prob*len(time_axis)/mask_time_length < mask_time_min_masks'' - mask_time_min_masks (``, *optional*, defaults to 2): + mask_time_min_masks (`int`, *optional*, defaults to 2): + The minimum number of masks of length `mask_feature_length` generated along the time axis, each time step, + irrespectively of `mask_feature_prob`. Only relevant if ''mask_time_prob*len(time_axis)/mask_time_length < + mask_time_min_masks'' mask_feature_prob (`float`, *optional*, defaults to 0.0): Percentage (between 0 and 1) of all feature vectors along the feature axis which will be masked. The masking procecure generates `mask_feature_prob*len(feature_axis)/mask_time_length` independent masks over @@ -126,7 +129,10 @@ class MoonshineConfig(PretrainedConfig): The minimum number of masks of length `mask_feature_length` generated along the feature axis, each time step, irrespectively of `mask_feature_prob`. Only relevant if `mask_feature_prob*len(feature_axis)/mask_feature_length < mask_feature_min_masks`. - mask_feature_min_masks (``, *optional*, defaults to 0): + mask_feature_min_masks (`int`, *optional*, defaults to 0): + The minimum number of masks of length `mask_feature_length` generated along the feature axis, each time + step, irrespectively of `mask_feature_prob`. Only relevant if + `mask_feature_prob*len(feature_axis)/mask_feature_length < mask_feature_min_masks`. Example: @@ -1502,24 +1508,21 @@ def forward( cache_position: Optional[torch.LongTensor] = None, ) -> Union[Tuple[torch.Tensor], Seq2SeqModelOutput]: r""" - Returns: + ```python + >>> import torch + >>> from transformers import AutoFeatureExtractor, MoonshineModel + >>> from datasets import load_dataset - Example: - ```python - >>> import torch - >>> from transformers import AutoFeatureExtractor, MoonshineModel - >>> from datasets import load_dataset - - >>> model = MoonshineModel.from_pretrained("UsefulSensors/moonshine-tiny") - >>> feature_extractor = AutoFeatureExtractor.from_pretrained("UsefulSensors/moonshine-tiny") - >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") - >>> inputs = feature_extractor(ds[0]["audio"]["array"], return_tensors="pt") - >>> input_values = inputs.input_values - >>> decoder_input_ids = torch.tensor([[1, 1]]) * model.config.decoder_start_token_id - >>> last_hidden_state = model(input_values, decoder_input_ids=decoder_input_ids).last_hidden_state - >>> list(last_hidden_state.shape) - [1, 2, 288] - ```""" + >>> model = MoonshineModel.from_pretrained("UsefulSensors/moonshine-tiny") + >>> feature_extractor = AutoFeatureExtractor.from_pretrained("UsefulSensors/moonshine-tiny") + >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") + >>> inputs = feature_extractor(ds[0]["audio"]["array"], return_tensors="pt") + >>> input_values = inputs.input_values + >>> decoder_input_ids = torch.tensor([[1, 1]]) * model.config.decoder_start_token_id + >>> last_hidden_state = model(input_values, decoder_input_ids=decoder_input_ids).last_hidden_state + >>> list(last_hidden_state.shape) + [1, 2, 288] + ```""" output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states From 5124cbd6b926573e852cc4056f8eab13e932efd1 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Tue, 17 Dec 2024 17:45:08 +0100 Subject: [PATCH 29/39] nits --- .../moonshine/configuration_moonshine.py | 4 ++-- .../models/moonshine/modeling_moonshine.py | 11 +++++----- .../models/moonshine/modular_moonshine.py | 21 +++++++------------ 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/transformers/models/moonshine/configuration_moonshine.py b/src/transformers/models/moonshine/configuration_moonshine.py index ceca76d6b44651..01c88fcff2694d 100644 --- a/src/transformers/models/moonshine/configuration_moonshine.py +++ b/src/transformers/models/moonshine/configuration_moonshine.py @@ -12,7 +12,7 @@ class MoonshineConfig(PretrainedConfig): This is the configuration class to store the configuration of a [`MoonshineModel`]. It is used to instantiate a Moonshine model according to the specified arguments, defining the model architecture. Instantiating a configuration with the defaults will yield a similar configuration to that of the Moonshine - [UsefulSensors/moonshine-tiny](https://huggingface.co/UsefulSensors/moonshine-tiny). + [eustlb/moonshine-tiny](https://huggingface.co/eustlb/moonshine-tiny). Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information. @@ -111,7 +111,7 @@ class MoonshineConfig(PretrainedConfig): >>> from transformers import MoonshineModel, MoonshineConfig >>> # Initializing a Moonshine style configuration - >>> configuration = MoonshineConfig().from_pretrained("UsefulSensors/moonshine-tiny") + >>> configuration = MoonshineConfig().from_pretrained("eustlb/moonshine-tiny") >>> # Initializing a model from the configuration >>> model = MoonshineModel(configuration) diff --git a/src/transformers/models/moonshine/modeling_moonshine.py b/src/transformers/models/moonshine/modeling_moonshine.py index b0529a1e308c77..6c47afffd04a11 100644 --- a/src/transformers/models/moonshine/modeling_moonshine.py +++ b/src/transformers/models/moonshine/modeling_moonshine.py @@ -1761,6 +1761,7 @@ def compute_num_masked_span(input_length): class MoonshineModel(MoonshinePreTrainedModel): def __init__(self, config: MoonshineConfig): super().__init__(config) + self.encoder = MoonshineEncoder(config) self.decoder = MoonshineDecoder(config) # Initialize weights and apply final processing @@ -1856,8 +1857,8 @@ def forward( >>> from transformers import AutoFeatureExtractor, MoonshineModel >>> from datasets import load_dataset - >>> model = MoonshineModel.from_pretrained("UsefulSensors/moonshine-tiny") - >>> feature_extractor = AutoFeatureExtractor.from_pretrained("UsefulSensors/moonshine-tiny") + >>> model = MoonshineModel.from_pretrained("eustlb/moonshine-tiny") + >>> feature_extractor = AutoFeatureExtractor.from_pretrained("eustlb/moonshine-tiny") >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") >>> inputs = feature_extractor(ds[0]["audio"]["array"], return_tensors="pt") >>> input_values = inputs.input_values @@ -2001,8 +2002,8 @@ def forward( >>> from transformers import AutoProcessor, MoonshineForConditionalGeneration >>> from datasets import load_dataset - >>> processor = AutoProcessor.from_pretrained("UsefulSensors/moonshine-tiny") - >>> model = MoonshineForConditionalGeneration.from_pretrained("UsefulSensors/moonshine-tiny") + >>> processor = AutoProcessor.from_pretrained("eustlb/moonshine-tiny") + >>> model = MoonshineForConditionalGeneration.from_pretrained("eustlb/moonshine-tiny") >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") @@ -2013,7 +2014,7 @@ def forward( >>> transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] >>> transcription - ' Mr. Quilter is the apostle of the middle classes, and we are glad to welcome his gospel.' + 'Mr. Quilter is the apostle of the middle classes, and we are glad to welcome his gospel.' ```""" return_dict = return_dict if return_dict is not None else self.config.use_return_dict diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index dfc07c943a5ace..4315f80113674b 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -41,7 +41,7 @@ class MoonshineConfig(PretrainedConfig): This is the configuration class to store the configuration of a [`MoonshineModel`]. It is used to instantiate a Moonshine model according to the specified arguments, defining the model architecture. Instantiating a configuration with the defaults will yield a similar configuration to that of the Moonshine - [UsefulSensors/moonshine-tiny](https://huggingface.co/UsefulSensors/moonshine-tiny). + [eustlb/moonshine-tiny](https://huggingface.co/eustlb/moonshine-tiny). Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information. @@ -140,7 +140,7 @@ class MoonshineConfig(PretrainedConfig): >>> from transformers import MoonshineModel, MoonshineConfig >>> # Initializing a Moonshine style configuration - >>> configuration = MoonshineConfig().from_pretrained("UsefulSensors/moonshine-tiny") + >>> configuration = MoonshineConfig().from_pretrained("eustlb/moonshine-tiny") >>> # Initializing a model from the configuration >>> model = MoonshineModel(configuration) @@ -1484,13 +1484,6 @@ def forward( class MoonshineModel(WhisperModel): - def __init__(self, config: MoonshineConfig): - super().__init__(config) - self.encoder = MoonshineEncoder(config) - self.decoder = MoonshineDecoder(config) - - @add_start_docstrings_to_model_forward(MOONSHINE_INPUTS_DOCSTRING) - @replace_return_docstrings(output_type=Seq2SeqModelOutput, config_class=_CONFIG_FOR_DOC) def forward( self, input_values: Optional[torch.FloatTensor] = None, @@ -1513,8 +1506,8 @@ def forward( >>> from transformers import AutoFeatureExtractor, MoonshineModel >>> from datasets import load_dataset - >>> model = MoonshineModel.from_pretrained("UsefulSensors/moonshine-tiny") - >>> feature_extractor = AutoFeatureExtractor.from_pretrained("UsefulSensors/moonshine-tiny") + >>> model = MoonshineModel.from_pretrained("eustlb/moonshine-tiny") + >>> feature_extractor = AutoFeatureExtractor.from_pretrained("eustlb/moonshine-tiny") >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") >>> inputs = feature_extractor(ds[0]["audio"]["array"], return_tensors="pt") >>> input_values = inputs.input_values @@ -1641,8 +1634,8 @@ def forward( >>> from transformers import AutoProcessor, MoonshineForConditionalGeneration >>> from datasets import load_dataset - >>> processor = AutoProcessor.from_pretrained("UsefulSensors/moonshine-tiny") - >>> model = MoonshineForConditionalGeneration.from_pretrained("UsefulSensors/moonshine-tiny") + >>> processor = AutoProcessor.from_pretrained("eustlb/moonshine-tiny") + >>> model = MoonshineForConditionalGeneration.from_pretrained("eustlb/moonshine-tiny") >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation") @@ -1653,7 +1646,7 @@ def forward( >>> transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] >>> transcription - ' Mr. Quilter is the apostle of the middle classes, and we are glad to welcome his gospel.' + 'Mr. Quilter is the apostle of the middle classes, and we are glad to welcome his gospel.' ```""" return_dict = return_dict if return_dict is not None else self.config.use_return_dict From a528bd38594e01641266610d4b0326df7377d19e Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Wed, 18 Dec 2024 14:58:19 +0100 Subject: [PATCH 30/39] copied from -> imports --- .../models/moonshine/modeling_moonshine.py | 1 - .../models/moonshine/modular_moonshine.py | 33 ++----------------- 2 files changed, 2 insertions(+), 32 deletions(-) diff --git a/src/transformers/models/moonshine/modeling_moonshine.py b/src/transformers/models/moonshine/modeling_moonshine.py index 6c47afffd04a11..03767a3a682ebb 100644 --- a/src/transformers/models/moonshine/modeling_moonshine.py +++ b/src/transformers/models/moonshine/modeling_moonshine.py @@ -1921,7 +1921,6 @@ def forward( ) -# Copied from transformers.models.bart.modeling_bart.shift_tokens_right def shift_tokens_right(input_ids: torch.Tensor, pad_token_id: int, decoder_start_token_id: int): """ Shift input ids one token to the right. diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index 4315f80113674b..37d8da747c51de 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -26,9 +26,9 @@ logging, replace_return_docstrings, ) -from ..llama.modeling_llama import LlamaDecoderLayer, LlamaModel +from ..llama.modeling_llama import LlamaDecoderLayer, LlamaModel, repeat_kv from ..phi.modeling_phi import PhiAttention, PhiFlashAttention2, PhiMLP, PhiRotaryEmbedding, PhiSdpaAttention -from ..whisper.modeling_whisper import WhisperModel +from ..whisper.modeling_whisper import WhisperModel, shift_tokens_right logger = logging.get_logger(__name__) @@ -226,35 +226,6 @@ def __init__( ) -# Copied from transformers.models.bart.modeling_bart.shift_tokens_right -def shift_tokens_right(input_ids: torch.Tensor, pad_token_id: int, decoder_start_token_id: int): - """ - Shift input ids one token to the right. - """ - shifted_input_ids = input_ids.new_zeros(input_ids.shape) - shifted_input_ids[:, 1:] = input_ids[:, :-1].clone() - shifted_input_ids[:, 0] = decoder_start_token_id - - if pad_token_id is None: - raise ValueError("self.model.config.pad_token_id has to be defined.") - # replace possible -100 values in labels by `pad_token_id` - shifted_input_ids.masked_fill_(shifted_input_ids == -100, pad_token_id) - - return shifted_input_ids - - -def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor: - """ - This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch, - num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim) - """ - batch, num_key_value_heads, slen, head_dim = hidden_states.shape - if n_rep == 1: - return hidden_states - hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim) - return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim) - - def rotate_every_two(x: torch.Tensor) -> torch.Tensor: x1 = x[:, :, :, ::2] x2 = x[:, :, :, 1::2] From 338c7c0a773209cb8a9e74fb811ddc2221890e26 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Fri, 20 Dec 2024 12:26:08 +0100 Subject: [PATCH 31/39] imports fix --- src/transformers/__init__.py | 3 +++ src/transformers/utils/dummy_pt_objects.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/src/transformers/__init__.py b/src/transformers/__init__.py index 7a6a0f58930f5e..1d3ae31b11d834 100755 --- a/src/transformers/__init__.py +++ b/src/transformers/__init__.py @@ -2877,6 +2877,8 @@ "MoonshineForConditionalGeneration", "MoonshineModel", "MoonshinePreTrainedModel", + ] + ) _import_structure["models.modernbert"].extend( [ "ModernBertForMaskedLM", @@ -7580,6 +7582,7 @@ MoonshineForConditionalGeneration, MoonshineModel, MoonshinePreTrainedModel, + ) from .models.modernbert import ( ModernBertForMaskedLM, ModernBertForSequenceClassification, diff --git a/src/transformers/utils/dummy_pt_objects.py b/src/transformers/utils/dummy_pt_objects.py index eaf1846e446788..3ee8bc0c14348d 100644 --- a/src/transformers/utils/dummy_pt_objects.py +++ b/src/transformers/utils/dummy_pt_objects.py @@ -6419,6 +6419,12 @@ def __init__(self, *args, **kwargs): class MoonshineForConditionalGeneration(metaclass=DummyObject): + _backends = ["torch"] + + def __init__(self, *args, **kwargs): + requires_backends(self, ["torch"]) + + class ModernBertForMaskedLM(metaclass=DummyObject): _backends = ["torch"] From 2ec366aabf8db663b4b6b8ab424f152897d42bd1 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Fri, 20 Dec 2024 15:30:20 +0100 Subject: [PATCH 32/39] integrate attention refacto --- docs/source/en/_toctree.yml | 4 +- src/transformers/__init__.py | 12 +- src/transformers/models/__init__.py | 2 +- .../models/auto/configuration_auto.py | 4 +- src/transformers/models/auto/modeling_auto.py | 2 +- .../models/auto/tokenization_auto.py | 2 +- .../moonshine/configuration_moonshine.py | 8 +- .../moonshine/convert_usefulsensors_to_hf.py | 6 +- .../models/moonshine/modeling_moonshine.py | 619 ++++-------------- .../models/moonshine/modular_moonshine.py | 543 +++------------ src/transformers/utils/dummy_pt_objects.py | 8 +- 11 files changed, 228 insertions(+), 982 deletions(-) diff --git a/docs/source/en/_toctree.yml b/docs/source/en/_toctree.yml index 2362331e2ad4a5..68874f99c2dc64 100644 --- a/docs/source/en/_toctree.yml +++ b/docs/source/en/_toctree.yml @@ -500,10 +500,10 @@ title: mLUKE - local: model_doc/mobilebert title: MobileBERT - - local: model_doc/moonshine - title: moonshine - local: model_doc/modernbert title: ModernBert + - local: model_doc/moonshine + title: moonshine - local: model_doc/mpnet title: MPNet - local: model_doc/mpt diff --git a/src/transformers/__init__.py b/src/transformers/__init__.py index 1d3ae31b11d834..4d3568bb3208be 100755 --- a/src/transformers/__init__.py +++ b/src/transformers/__init__.py @@ -5584,8 +5584,8 @@ from .models.mobilevitv2 import ( MobileViTV2Config, ) - from .models.moonshine import MoonshineConfig from .models.modernbert import ModernBertConfig + from .models.moonshine import MoonshineConfig from .models.moshi import ( MoshiConfig, MoshiDepthConfig, @@ -7578,11 +7578,6 @@ MobileViTV2Model, MobileViTV2PreTrainedModel, ) - from .models.moonshine import ( - MoonshineForConditionalGeneration, - MoonshineModel, - MoonshinePreTrainedModel, - ) from .models.modernbert import ( ModernBertForMaskedLM, ModernBertForSequenceClassification, @@ -7590,6 +7585,11 @@ ModernBertModel, ModernBertPreTrainedModel, ) + from .models.moonshine import ( + MoonshineForConditionalGeneration, + MoonshineModel, + MoonshinePreTrainedModel, + ) from .models.moshi import ( MoshiForCausalLM, MoshiForConditionalGeneration, diff --git a/src/transformers/models/__init__.py b/src/transformers/models/__init__.py index a24b065141c1f2..f576f6bc0c1fde 100644 --- a/src/transformers/models/__init__.py +++ b/src/transformers/models/__init__.py @@ -167,8 +167,8 @@ mobilenet_v2, mobilevit, mobilevitv2, - moonshine, modernbert, + moonshine, moshi, mpnet, mpt, diff --git a/src/transformers/models/auto/configuration_auto.py b/src/transformers/models/auto/configuration_auto.py index a585fb5c58211a..b5f9c57ab13d5a 100644 --- a/src/transformers/models/auto/configuration_auto.py +++ b/src/transformers/models/auto/configuration_auto.py @@ -187,8 +187,8 @@ ("mobilenet_v2", "MobileNetV2Config"), ("mobilevit", "MobileViTConfig"), ("mobilevitv2", "MobileViTV2Config"), - ("moonshine", "MoonshineConfig"), ("modernbert", "ModernBertConfig"), + ("moonshine", "MoonshineConfig"), ("moshi", "MoshiConfig"), ("mpnet", "MPNetConfig"), ("mpt", "MptConfig"), @@ -512,8 +512,8 @@ ("mobilenet_v2", "MobileNetV2"), ("mobilevit", "MobileViT"), ("mobilevitv2", "MobileViTV2"), - ("moonshine", "Moonshine"), ("modernbert", "ModernBERT"), + ("moonshine", "Moonshine"), ("moshi", "Moshi"), ("mpnet", "MPNet"), ("mpt", "MPT"), diff --git a/src/transformers/models/auto/modeling_auto.py b/src/transformers/models/auto/modeling_auto.py index 1bdeca6dc3c733..3dec215a02444b 100644 --- a/src/transformers/models/auto/modeling_auto.py +++ b/src/transformers/models/auto/modeling_auto.py @@ -176,8 +176,8 @@ ("mobilenet_v2", "MobileNetV2Model"), ("mobilevit", "MobileViTModel"), ("mobilevitv2", "MobileViTV2Model"), - ("moonshine", "MoonshineModel"), ("modernbert", "ModernBertModel"), + ("moonshine", "MoonshineModel"), ("moshi", "MoshiModel"), ("mpnet", "MPNetModel"), ("mpt", "MptModel"), diff --git a/src/transformers/models/auto/tokenization_auto.py b/src/transformers/models/auto/tokenization_auto.py index 1585bfb9bfc246..8eb246d7051989 100644 --- a/src/transformers/models/auto/tokenization_auto.py +++ b/src/transformers/models/auto/tokenization_auto.py @@ -313,8 +313,8 @@ ("mllama", ("LlamaTokenizer", "LlamaTokenizerFast" if is_tokenizers_available() else None)), ("mluke", ("MLukeTokenizer" if is_sentencepiece_available() else None, None)), ("mobilebert", ("MobileBertTokenizer", "MobileBertTokenizerFast" if is_tokenizers_available() else None)), - ("moonshine", (None, "PreTrainedTokenizerFast" if is_tokenizers_available() else None)), ("modernbert", (None, "PreTrainedTokenizerFast" if is_tokenizers_available() else None)), + ("moonshine", (None, "PreTrainedTokenizerFast" if is_tokenizers_available() else None)), ("moshi", (None, "PreTrainedTokenizerFast" if is_tokenizers_available() else None)), ("mpnet", ("MPNetTokenizer", "MPNetTokenizerFast" if is_tokenizers_available() else None)), ("mpt", (None, "GPTNeoXTokenizerFast" if is_tokenizers_available() else None)), diff --git a/src/transformers/models/moonshine/configuration_moonshine.py b/src/transformers/models/moonshine/configuration_moonshine.py index 01c88fcff2694d..2c1f429ca0757f 100644 --- a/src/transformers/models/moonshine/configuration_moonshine.py +++ b/src/transformers/models/moonshine/configuration_moonshine.py @@ -41,6 +41,8 @@ class MoonshineConfig(PretrainedConfig): The non-linear activation function (function or string) in the encoder. decoder_hidden_act (`str` or `function`, *optional*, defaults to `"silu"`): The non-linear activation function (function or string) in the decoder. + max_position_embeddings (`int`, *optional*, defaults to 2048): + The maximum sequence length that this model might ever be used with. initializer_range (`float`, *optional*, defaults to 0.02): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. layer_norm_eps (`float`, *optional*, defaults to 1e-05): @@ -61,8 +63,6 @@ class MoonshineConfig(PretrainedConfig): Whether to use a bias in the query, key, value and output projection layers during self-attention. attention_dropout (`float`, *optional*, defaults to 0.0): The dropout ratio for the attention probabilities. - qk_layernorm (`bool`, *optional*, defaults to `False`): - Whether or not to normalize the Queries and Keys after projecting the hidden states. ff_mult (`int`, *optional*, defaults to 4): Factor by which to scale the intermediate size. bos_token_id (`int`, *optional*, defaults to 1): @@ -133,6 +133,7 @@ def __init__( num_key_value_heads=None, encoder_hidden_act="gelu", decoder_hidden_act="silu", + max_position_embeddings=2048, initializer_range=0.02, layer_norm_eps=1e-5, decoder_start_token_id=1, @@ -142,7 +143,6 @@ def __init__( min_rotary_ndims=32, attention_bias=False, attention_dropout=0.0, - qk_layernorm=False, ff_mult=4, bos_token_id=1, eos_token_id=2, @@ -167,6 +167,7 @@ def __init__( self.num_key_value_heads = num_key_value_heads self.encoder_hidden_act = encoder_hidden_act self.decoder_hidden_act = decoder_hidden_act + self.max_position_embeddings = max_position_embeddings self.initializer_range = initializer_range self.layer_norm_eps = layer_norm_eps self.decoder_start_token_id = decoder_start_token_id @@ -176,7 +177,6 @@ def __init__( self.min_rotary_ndims = min_rotary_ndims self.attention_bias = attention_bias self.attention_dropout = attention_dropout - self.qk_layernorm = qk_layernorm self.ff_mult = ff_mult # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 diff --git a/src/transformers/models/moonshine/convert_usefulsensors_to_hf.py b/src/transformers/models/moonshine/convert_usefulsensors_to_hf.py index 99fe729a31265e..64833f9497b0f8 100644 --- a/src/transformers/models/moonshine/convert_usefulsensors_to_hf.py +++ b/src/transformers/models/moonshine/convert_usefulsensors_to_hf.py @@ -75,15 +75,15 @@ def _convert_layer_names(name, gated_mlp=False): name = re.sub(r"mha_with_rope\.key_dense", "self_attn.k_proj", name) name = re.sub(r"mha_with_rope\.query_dense", "self_attn.q_proj", name) name = re.sub(r"mha_with_rope\.value_dense", "self_attn.v_proj", name) - name = re.sub(r"mha_with_rope\.output_dense", "self_attn.dense", name) + name = re.sub(r"mha_with_rope\.output_dense", "self_attn.o_proj", name) name = re.sub(r"mha_precomputed_kv\.key_dense", "encoder_attn.k_proj", name) name = re.sub(r"mha_precomputed_kv\.query_dense", "encoder_attn.q_proj", name) name = re.sub(r"mha_precomputed_kv\.value_dense", "encoder_attn.v_proj", name) - name = re.sub(r"mha_precomputed_kv\.output_dense", "encoder_attn.dense", name) + name = re.sub(r"mha_precomputed_kv\.output_dense", "encoder_attn.o_proj", name) name = re.sub(r"mha_causal_with_rope\.key_dense", "self_attn.k_proj", name) name = re.sub(r"mha_causal_with_rope\.query_dense", "self_attn.q_proj", name) name = re.sub(r"mha_causal_with_rope\.value_dense", "self_attn.v_proj", name) - name = re.sub(r"mha_causal_with_rope\.output_dense", "self_attn.dense", name) + name = re.sub(r"mha_causal_with_rope\.output_dense", "self_attn.o_proj", name) name = re.sub(r"layer_normalization\.", "input_layernorm.", name) name = re.sub(r"layer_normalization_1\.", "post_attention_layernorm.", name) name = re.sub(r"layer_normalization_2\.", "final_layernorm.", name) diff --git a/src/transformers/models/moonshine/modeling_moonshine.py b/src/transformers/models/moonshine/modeling_moonshine.py index 03767a3a682ebb..91dd7fc4fcc7b7 100644 --- a/src/transformers/models/moonshine/modeling_moonshine.py +++ b/src/transformers/models/moonshine/modeling_moonshine.py @@ -5,20 +5,18 @@ # modular_moonshine.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 import copy -import math -from typing import List, Optional, Tuple, Union +from typing import Callable, Optional, Tuple, Union import numpy as np import torch import torch.nn as nn -from packaging import version from torch.nn import CrossEntropyLoss from ...activations import ACT2FN from ...cache_utils import Cache, DynamicCache, EncoderDecoderCache, StaticCache from ...generation import GenerationMixin from ...modeling_attn_mask_utils import AttentionMaskConverter -from ...modeling_flash_attention_utils import FlashAttentionKwargs, _flash_attention_forward +from ...modeling_flash_attention_utils import FlashAttentionKwargs from ...modeling_outputs import ( BaseModelOutput, BaseModelOutputWithPast, @@ -27,13 +25,11 @@ Seq2SeqModelOutput, ) from ...modeling_rope_utils import ROPE_INIT_FUNCTIONS -from ...modeling_utils import PreTrainedModel +from ...modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel from ...processing_utils import Unpack from ...utils import ( add_start_docstrings, add_start_docstrings_to_model_forward, - get_torch_version, - is_flash_attn_greater_or_equal_2_10, logging, replace_return_docstrings, ) @@ -46,6 +42,9 @@ class MoonshineRotaryEmbedding(nn.Module): + # Note: the forward pass of this RoPE is slightly different from Llama's, resulting in different `sin`/`cos` for + # the same parameterization. The differences are highlighted with a comment. + def __init__( self, dim=None, @@ -121,8 +120,7 @@ def forward(self, x, position_ids): device_type = device_type if isinstance(device_type, str) and device_type != "mps" else "cpu" with torch.autocast(device_type=device_type, enabled=False): freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2) - emb = torch.stack((freqs, freqs), dim=-1) - emb = emb.flatten(-2) # in einsum notation: rearrange(x, '... d j -> ... (d j)') + emb = torch.repeat_interleave(freqs, 2, dim=-1) # This line differs from Llama's implementation cos = emb.cos() sin = emb.sin() @@ -229,72 +227,77 @@ def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1): return q_embed, k_embed +def eager_attention_forward( + module: nn.Module, + query: torch.Tensor, + key: torch.Tensor, + value: torch.Tensor, + attention_mask: Optional[torch.Tensor], + scaling: float, + dropout: float = 0.0, + **kwargs, +): + key_states = repeat_kv(key, module.num_key_value_groups) + value_states = repeat_kv(value, module.num_key_value_groups) + + attn_weights = torch.matmul(query, key_states.transpose(2, 3)) * scaling + if attention_mask is not None: + causal_mask = attention_mask[:, :, :, : key_states.shape[-2]] + attn_weights = attn_weights + causal_mask + + attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query.dtype) + attn_weights = nn.functional.dropout(attn_weights, p=dropout, training=module.training) + attn_output = torch.matmul(attn_weights, value_states) + attn_output = attn_output.transpose(1, 2).contiguous() + + return attn_output, attn_weights + + class MoonshineAttention(nn.Module): """Multi-headed attention from 'Attention Is All You Need' paper""" - def __init__(self, config: MoonshineConfig, layer_idx: Optional[int] = None, is_causal: bool = False): + def __init__(self, config: MoonshineConfig, layer_idx: int, is_causal: bool): super().__init__() self.config = config 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.head_dim = getattr(config, "head_dim", config.hidden_size // config.num_attention_heads) + self.num_key_value_groups = config.num_attention_heads // config.num_key_value_heads + self.scaling = self.head_dim**-0.5 self.attention_dropout = config.attention_dropout - self.hidden_size = config.hidden_size - self.num_heads = config.num_attention_heads - self.head_dim = self.hidden_size // self.num_heads - self.num_key_value_heads = config.num_key_value_heads - self.num_key_value_groups = self.num_heads // self.num_key_value_heads - self.rope_theta = config.rope_theta - - self.rotary_ndims = max(config.hidden_size // config.num_attention_heads // 2, config.min_rotary_ndims) - self.is_causal = is_causal - if (self.head_dim * self.num_heads) != self.hidden_size: - raise ValueError( - f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}" - f" and `num_heads`: {self.num_heads})." - ) - self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=config.attention_bias) - self.k_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config.attention_bias) - self.v_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config.attention_bias) - self.dense = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=config.attention_bias) - - self.qk_layernorm = config.qk_layernorm - if self.qk_layernorm: - self.q_layernorm = nn.LayerNorm( - config.hidden_size // self.num_heads, eps=config.layer_norm_eps, elementwise_affine=True - ) - self.k_layernorm = nn.LayerNorm( - config.hidden_size // self.num_heads, eps=config.layer_norm_eps, elementwise_affine=True - ) - self.rotary_emb = MoonshineRotaryEmbedding(dim=self.rotary_ndims) + self.q_proj = nn.Linear( + config.hidden_size, config.num_attention_heads * self.head_dim, bias=config.attention_bias + ) + self.k_proj = nn.Linear( + config.hidden_size, config.num_key_value_heads * self.head_dim, bias=config.attention_bias + ) + self.v_proj = nn.Linear( + config.hidden_size, config.num_key_value_heads * self.head_dim, bias=config.attention_bias + ) + self.o_proj = nn.Linear( + config.num_attention_heads * self.head_dim, config.hidden_size, bias=config.attention_bias + ) + self.rotary_ndims = max(config.hidden_size // config.num_attention_heads // 2, config.min_rotary_ndims) + self.num_key_values_heads = config.num_key_value_heads def forward( self, hidden_states: torch.Tensor, + position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, - position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Cache] = None, - key_value_states: Optional[torch.Tensor] = None, - output_attentions: bool = False, - use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 + key_value_states: Optional[torch.Tensor] = None, + **kwargs: Unpack[FlashAttentionKwargs], ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: - bsz, q_len, _ = hidden_states.size() + bsz, q_len = hidden_states.shape[:-1] - query_states = self.q_proj(hidden_states) + query_states = ( + self.q_proj(hidden_states).view(bsz, q_len, self.config.num_key_value_heads, self.head_dim).transpose(1, 2) + ) - # if key_value_states are provided this layer is used as a cross-attention layer - # for the decoder is_cross_attention = key_value_states is not None - if past_key_value is not None: is_updated = past_key_value.is_updated.get(self.layer_idx) if is_cross_attention: @@ -307,176 +310,26 @@ def forward( # use key_value_states if cross attention current_states = key_value_states if key_value_states is not None else hidden_states if is_cross_attention and past_key_value and is_updated: - # reuse k,v, cross_attentions key_states = past_key_value.key_cache[self.layer_idx] value_states = past_key_value.value_cache[self.layer_idx] else: - key_states = self.k_proj(current_states) - value_states = self.v_proj(current_states) - key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) - value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) - if is_cross_attention and past_key_value is not None: - key_states, value_states = past_key_value.update( - key_states, value_states, self.layer_idx, {"cache_position": cache_position} - ) - - if self.qk_layernorm: - query_states = self.q_layernorm(query_states) - key_states = self.k_layernorm(key_states) - - query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) - - if not is_cross_attention: - if position_embeddings is None: - logger.warning_once( - "The attention layers in this model are transitioning from computing the RoPE embeddings internally " - "through `position_ids` (2D tensor with the indexes of the tokens), to using externally computed " - "`position_embeddings` (Tuple of tensors, containing cos and sin). In v4.46 `position_ids` will be " - "removed and `position_embeddings` will be mandatory." - ) - cos, sin = self.rotary_emb(value_states, position_ids) - else: - cos, sin = position_embeddings - - # Partial rotary embedding - query_rot, query_pass = ( - query_states[..., : self.rotary_ndims], - query_states[..., self.rotary_ndims :], - ) - key_rot, key_pass = ( - key_states[..., : self.rotary_ndims], - key_states[..., self.rotary_ndims :], + key_states = ( + self.k_proj(current_states) + .view(bsz, -1, self.config.num_key_value_heads, self.head_dim) + .transpose(1, 2) ) - # [batch_size, seq_length, num_heads, self.rotary_ndims] - query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin) - - # [batch_size, seq_length, num_heads, head_dim] - query_states = torch.cat((query_rot, query_pass), dim=-1) - key_states = torch.cat((key_rot, key_pass), dim=-1) - - if past_key_value is not None: - cache_kwargs = { - "sin": sin, - "cos": cos, - "partial_rotation_size": self.rotary_ndims, - "cache_position": cache_position, - } - key_states, value_states = past_key_value.update( - key_states, value_states, self.layer_idx, cache_kwargs - ) - - key_states = repeat_kv(key_states, self.num_key_value_groups) - value_states = repeat_kv(value_states, self.num_key_value_groups) - - # Queries and keys upcast to fp32 is required by Moonshine-2 to avoid overflow - attn_weights = torch.matmul( - query_states.to(torch.float32), key_states.to(torch.float32).transpose(2, 3) - ) / math.sqrt(self.head_dim) - - if attention_mask is not None: - causal_mask = attention_mask[:, :, :, : key_states.shape[-2]] - attn_weights += causal_mask - - # upcast attention to fp32 - attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(value_states.dtype) - attn_weights = nn.functional.dropout(attn_weights, p=self.attention_dropout, training=self.training) - - attn_output = torch.matmul(attn_weights, value_states) - - if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim): - raise ValueError( - f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is" - f" {attn_output.size()}" + value_states = ( + self.v_proj(current_states) + .view(bsz, -1, self.config.num_key_value_heads, self.head_dim) + .transpose(1, 2) ) - - attn_output = attn_output.transpose(1, 2).contiguous() - attn_output = attn_output.reshape(bsz, q_len, self.hidden_size) - - attn_output = self.dense(attn_output) - - if not output_attentions: - attn_weights = None - - return attn_output, attn_weights, past_key_value - - -class MoonshineFlashAttention2(MoonshineAttention): - """ - Moonshine flash attention module. This module inherits from `MoonshineAttention` as the weights of the module stays - untouched. The only required change would be on the forward pass where it needs to correctly call the public API of - flash attention and deal with padding tokens in case the input contains any of them. - """ - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - - # TODO: Should be removed once Flash Attention for RoCm is bumped to 2.1. - # flash_attn<2.1 generates top-left aligned causal mask, while what is needed here is bottom-right alignement, that was made default for flash_attn>=2.1. This attribute is used to handle this difference. Reference: https://github.com/Dao-AILab/flash-attention/releases/tag/v2.1.0. - # 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 forward( - self, - hidden_states: torch.Tensor, - attention_mask: Optional[torch.Tensor] = None, - position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Cache] = None, - key_value_states: Optional[torch.Tensor] = None, - output_attentions: bool = False, - use_cache: bool = False, - cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: - bsz, q_len, _ = hidden_states.size() - - query_states = self.q_proj(hidden_states) - - # if key_value_states are provided this layer is used as a cross-attention layer - # for the decoder - is_cross_attention = key_value_states is not None - - if past_key_value is not None: - is_updated = past_key_value.is_updated.get(self.layer_idx) - if is_cross_attention: - # after the first generated id, we can subsequently re-use all key/value_states from cache - past_key_value.is_updated[self.layer_idx] = True - past_key_value = past_key_value.cross_attention_cache - else: - past_key_value = past_key_value.self_attention_cache - - # use key_value_states if cross attention - current_states = key_value_states if key_value_states is not None else hidden_states - if is_cross_attention and past_key_value and is_updated: - # reuse k,v, cross_attentions - key_states = past_key_value.key_cache[self.layer_idx] - value_states = past_key_value.value_cache[self.layer_idx] - else: - key_states = self.k_proj(current_states) - value_states = self.v_proj(current_states) - key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) - value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) if is_cross_attention and past_key_value is not None: key_states, value_states = past_key_value.update( key_states, value_states, self.layer_idx, {"cache_position": cache_position} ) - if self.qk_layernorm: - query_states = self.q_layernorm(query_states) - key_states = self.k_layernorm(key_states) - - query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) - if not is_cross_attention: - if position_embeddings is None: - logger.warning_once( - "The attention layers in this model are transitioning from computing the RoPE embeddings internally " - "through `position_ids` (2D tensor with the indexes of the tokens), to using externally computed " - "`position_embeddings` (Tuple of tensors, containing cos and sin). In v4.46 `position_ids` will be " - "removed and `position_embeddings` will be mandatory." - ) - cos, sin = self.rotary_emb(value_states, position_ids) - else: - cos, sin = position_embeddings + cos, sin = position_embeddings # Partial rotary embedding query_rot, query_pass = ( @@ -487,7 +340,7 @@ def forward( key_states[..., : self.rotary_ndims], key_states[..., self.rotary_ndims :], ) - # [batch_size, seq_length, num_heads, self.rotary_ndims] + # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor] query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin) # [batch_size, seq_length, num_heads, head_dim] @@ -505,204 +358,32 @@ def forward( key_states, value_states, 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. - query_states = query_states.transpose(1, 2) - key_states = key_states.transpose(1, 2) - value_states = value_states.transpose(1, 2) - - attn_dropout = self.attention_dropout if self.training else 0.0 - - # 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. - # This might slowdown training & inference so it is recommended to not cast the LayerNorms - # in fp32. - - if query_states.dtype == torch.float32: - if torch.is_autocast_enabled(): - target_dtype = torch.get_autocast_gpu_dtype() - # Handle the case where the model is quantized - elif hasattr(self.config, "_pre_quantization_dtype"): - target_dtype = self.config._pre_quantization_dtype - else: - target_dtype = self.q_proj.weight.dtype - - logger.warning_once( - f"The input hidden states seems to be silently casted in float32, this might be related to" - f" the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in" - f" {target_dtype}." - ) - - query_states = query_states.to(target_dtype) - key_states = key_states.to(target_dtype) - value_states = value_states.to(target_dtype) - - attn_output = _flash_attention_forward( - query_states, - key_states, - value_states, - attention_mask, - q_len, - position_ids=position_ids, - dropout=attn_dropout, - softmax_scale=None, - use_top_left_mask=self._flash_attn_uses_top_left_mask, - is_causal=self.is_causal, - ) - - attn_output = attn_output.reshape(bsz, q_len, self.hidden_size).contiguous() - attn_output = self.dense(attn_output) - - if not output_attentions: - attn_weights = None - - return attn_output, attn_weights, past_key_value - - -class MoonshineSdpaAttention(MoonshineAttention): - """ - SDPA attention module using torch.nn.functional.scaled_dot_product_attention. This module inherits from - `MoonshineAttention` as the weights of the module stays untouched. The only changes are on the forward pass to adapt to - SDPA API. - """ - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.require_contiguous_qkv = version.parse(get_torch_version()) < version.parse("2.2.0") - - # Adapted from MoonshineAttention.forward - def forward( - self, - hidden_states: torch.Tensor, - attention_mask: Optional[torch.Tensor] = None, - position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Cache] = None, - key_value_states: Optional[torch.Tensor] = None, - output_attentions: bool = False, - use_cache: bool = False, - cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: - bsz, q_len, _ = hidden_states.size() - - query_states = self.q_proj(hidden_states) - - # if key_value_states are provided this layer is used as a cross-attention layer - # for the decoder - is_cross_attention = key_value_states is not None - - if past_key_value is not None: - is_updated = past_key_value.is_updated.get(self.layer_idx) - if is_cross_attention: - # after the first generated id, we can subsequently re-use all key/value_states from cache - past_key_value.is_updated[self.layer_idx] = True - past_key_value = past_key_value.cross_attention_cache - else: - past_key_value = past_key_value.self_attention_cache - - # use key_value_states if cross attention - current_states = key_value_states if key_value_states is not None else hidden_states - if is_cross_attention and past_key_value and is_updated: - # reuse k,v, cross_attentions - key_states = past_key_value.key_cache[self.layer_idx] - value_states = past_key_value.value_cache[self.layer_idx] - else: - key_states = self.k_proj(current_states) - value_states = self.v_proj(current_states) - key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) - value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) - if is_cross_attention and past_key_value is not None: - key_states, value_states = past_key_value.update( - key_states, value_states, self.layer_idx, {"cache_position": cache_position} - ) - - if self.qk_layernorm: - query_states = self.q_layernorm(query_states) - key_states = self.k_layernorm(key_states) - - query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) - - if not is_cross_attention: - if position_embeddings is None: + attention_interface: Callable = eager_attention_forward + if self.config._attn_implementation != "eager": + if self.config._attn_implementation == "sdpa" and kwargs.get("output_attentions", False): logger.warning_once( - "The attention layers in this model are transitioning from computing the RoPE embeddings internally " - "through `position_ids` (2D tensor with the indexes of the tokens), to using externally computed " - "`position_embeddings` (Tuple of tensors, containing cos and sin). In v4.46 `position_ids` will be " - "removed and `position_embeddings` will be mandatory." + "`torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to " + 'eager attention. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.' ) - cos, sin = self.rotary_emb(value_states, position_ids) else: - cos, sin = position_embeddings + attention_interface = ALL_ATTENTION_FUNCTIONS[self.config._attn_implementation] - # Partial rotary embedding - query_rot, query_pass = ( - query_states[..., : self.rotary_ndims], - query_states[..., self.rotary_ndims :], - ) - key_rot, key_pass = ( - key_states[..., : self.rotary_ndims], - key_states[..., self.rotary_ndims :], - ) - # [batch_size, seq_length, num_heads, self.rotary_ndims] - query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin) - - # [batch_size, seq_length, num_heads, head_dim] - query_states = torch.cat((query_rot, query_pass), dim=-1) - key_states = torch.cat((key_rot, key_pass), dim=-1) - - if past_key_value is not None: - cache_kwargs = { - "sin": sin, - "cos": cos, - "partial_rotation_size": self.rotary_ndims, - "cache_position": cache_position, - } - key_states, value_states = past_key_value.update( - key_states, value_states, self.layer_idx, cache_kwargs - ) - - key_states = repeat_kv(key_states, self.num_key_value_groups) - value_states = repeat_kv(value_states, self.num_key_value_groups) - - causal_mask = attention_mask - if attention_mask is not None: - causal_mask = causal_mask[:, :, :, : key_states.shape[-2]] - - # SDPA with memory-efficient backend is broken in torch==2.1.2 when using non-contiguous inputs and a custom - # attn_mask, so we need to call `.contiguous()` here. This was fixed in torch==2.2.0. - # Reference: https://github.com/pytorch/pytorch/issues/112577 - if self.require_contiguous_qkv and query_states.device.type == "cuda" and attention_mask is not None: - query_states = query_states.contiguous() - key_states = key_states.contiguous() - value_states = value_states.contiguous() - - # 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 self.is_causal and causal_mask is None and q_len > 1 else False - - attn_output = torch.nn.functional.scaled_dot_product_attention( + is_causal = True if self.is_causal and attention_mask is None and q_len > 1 else False + attn_output, attn_weights = attention_interface( + self, query_states, key_states, value_states, - attn_mask=causal_mask, - dropout_p=self.attention_dropout if self.training else 0.0, + attention_mask, + dropout=0.0 if not self.training else self.attention_dropout, + scaling=self.scaling, is_causal=is_causal, + **kwargs, ) - attn_output = attn_output.transpose(1, 2).contiguous() - attn_output = attn_output.reshape(bsz, q_len, self.hidden_size) - - attn_output = self.dense(attn_output) - - return attn_output, None, past_key_value - - -MOONSHINE_ATTENTION_CLASSES = { - "eager": MoonshineAttention, - "flash_attention_2": MoonshineFlashAttention2, - "sdpa": MoonshineSdpaAttention, -} + attn_output = attn_output.reshape(bsz, q_len, -1).contiguous() + attn_output = self.o_proj(attn_output) + return attn_output, attn_weights class MoonshineEncoderLayer(nn.Module): @@ -710,7 +391,7 @@ def __init__(self, config: MoonshineConfig, layer_idx: int): super().__init__() self.hidden_size = config.hidden_size - self.self_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation](config=config, layer_idx=layer_idx) + self.self_attn = MoonshineAttention(config=config, layer_idx=layer_idx, is_causal=False) self.mlp = MoonshineMLP(config, config.encoder_hidden_act) self.input_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) @@ -726,36 +407,14 @@ def forward( use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC - **kwargs, + **kwargs: Unpack[FlashAttentionKwargs], ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: - """ - Args: - hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` - attention_mask (`torch.FloatTensor`, *optional*): - attention mask of size `(batch_size, sequence_length)` if flash attention is used or `(batch_size, 1, - query_sequence_length, key_sequence_length)` if default attention is used. - output_attentions (`bool`, *optional*): - Whether or not to return the attentions tensors of all attention layers. See `attentions` under - returned tensors for more detail. - 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`). - past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states - cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): - Indices depicting the position of the input sequence tokens in the sequence - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): - Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, - with `head_dim` being the embedding dimension of each attention head. - kwargs (`dict`, *optional*): - Arbitrary kwargs to be ignored, used for FSDP and other methods that injects code - into the model - """ residual = hidden_states hidden_states = self.input_layernorm(hidden_states) # Self Attention - hidden_states, self_attn_weights, present_key_value = self.self_attn( + hidden_states, self_attn_weights = self.self_attn( hidden_states=hidden_states, attention_mask=attention_mask, position_ids=position_ids, @@ -775,13 +434,9 @@ def forward( hidden_states = residual + hidden_states outputs = (hidden_states,) - if output_attentions: outputs += (self_attn_weights,) - if use_cache: - outputs += (present_key_value,) - return outputs @@ -790,12 +445,8 @@ def __init__(self, config: MoonshineConfig, layer_idx: int = None): super().__init__() self.hidden_size = config.hidden_size - self.self_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation]( - config=config, layer_idx=layer_idx, is_causal=True - ) - self.encoder_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation]( - config=config, layer_idx=layer_idx, is_causal=False - ) + self.self_attn = MoonshineAttention(config=config, layer_idx=layer_idx, is_causal=True) + self.encoder_attn = MoonshineAttention(config=config, layer_idx=layer_idx, is_causal=False) self.mlp = MoonshineMLP(config, config.decoder_hidden_act) self.input_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) @@ -852,7 +503,7 @@ def forward( hidden_states = self.input_layernorm(hidden_states) # Self Attention - hidden_states, self_attn_weights, present_key_value = self.self_attn( + hidden_states, self_attn_weights = self.self_attn( hidden_states=hidden_states, attention_mask=attention_mask, position_ids=position_ids, @@ -870,7 +521,7 @@ def forward( if encoder_hidden_states is not None: residual = hidden_states hidden_states = self.post_attention_layernorm(hidden_states) - hidden_states, cross_attn_weights, cross_attn_present_key_value = self.encoder_attn( + hidden_states, cross_attn_weights = self.encoder_attn( hidden_states=hidden_states, key_value_states=encoder_hidden_states, attention_mask=encoder_attention_mask, @@ -882,9 +533,6 @@ def forward( ) hidden_states = residual + hidden_states - # add cross-attn to positions 1 of present_key_value tuple - present_key_value = (present_key_value, cross_attn_present_key_value) - # Fully Connected residual = hidden_states hidden_states = self.final_layernorm(hidden_states) @@ -896,9 +544,6 @@ def forward( if output_attentions: outputs += (self_attn_weights, cross_attn_weights) - if use_cache: - outputs += (present_key_value,) - return outputs @@ -1006,7 +651,7 @@ def forward( input_values: Optional[torch.FloatTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Cache] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1031,7 +676,7 @@ def forward( config.n_positions - 1]`. [What are position IDs?](../glossary#position-ids) - past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*): + past_key_values (`Cache`, *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`. @@ -1086,25 +731,17 @@ def forward( if inputs_embeds is None: inputs_embeds = self.preprocess(input_values) - # kept for BC (non `Cache` `past_key_values` inputs) - return_legacy_cache = False - if use_cache or past_key_values is not None: - if isinstance(past_key_values, Cache) and not isinstance(past_key_values, EncoderDecoderCache): - past_key_values = EncoderDecoderCache(past_key_values, DynamicCache()) - elif not isinstance(past_key_values, EncoderDecoderCache): - return_legacy_cache = True - logger.warning_once( - "Passing a tuple of `past_key_values` is deprecated and will be removed in Transformers v4.43.0. " - "You should pass an instance of `EncoderDecoderCache` instead, e.g. " - "`past_key_values=EncoderDecoderCache.from_legacy_cache(past_key_values)`." - ) - past_key_values = EncoderDecoderCache.from_legacy_cache(past_key_values) + if use_cache and past_key_values is None: + self_attention_cache = DynamicCache() + cross_attention_cache = DynamicCache() + past_key_values = EncoderDecoderCache(self_attention_cache, cross_attention_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 + inputs_embeds.shape[1], device=inputs_embeds.device ) + if position_ids is None: position_ids = cache_position.unsqueeze(0) @@ -1116,7 +753,6 @@ def forward( # decoder layers all_hidden_states = () if output_hidden_states else None all_self_attns = () if output_attentions else None - next_encoder_cache = None for encoder_layer in self.layers: if output_hidden_states: @@ -1148,9 +784,6 @@ def forward( hidden_states = layer_outputs[0] - if use_cache: - next_encoder_cache = layer_outputs[2 if output_attentions else 1] - if output_attentions: all_self_attns += (layer_outputs[1],) @@ -1160,18 +793,13 @@ def forward( if output_hidden_states: all_hidden_states += (hidden_states,) - next_cache = next_encoder_cache if use_cache else None - if return_legacy_cache: - next_cache = next_cache.to_legacy_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( + output = BaseModelOutputWithPast( last_hidden_state=hidden_states, - past_key_values=next_cache, + past_key_values=past_key_values if use_cache else None, hidden_states=all_hidden_states, attentions=all_self_attns, ) + return output if return_dict else output.to_tuple() MOONSHINE_INPUTS_DOCSTRING = r""" @@ -1274,10 +902,7 @@ def __init__(self, config: MoonshineConfig): self.rotary_emb = MoonshineRotaryEmbedding( dim=max(config.hidden_size // config.num_attention_heads // 2, config.min_rotary_ndims) ) - self.gradient_checkpointing = False - if getattr(config, "pretraining_tp", 1) != 1: - logger.warn("`pretraining_tp` is deprecated, please use `model.tensor_parallel` instead.") # Initialize weights and apply final processing self.post_init() @@ -1296,7 +921,7 @@ def forward( encoder_hidden_states: Optional[torch.FloatTensor] = None, position_ids: Optional[torch.LongTensor] = None, encoder_position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Cache] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1346,7 +971,7 @@ def forward( Indices of positions of each encoder input's hidden states in the position embeddings. [What are position IDs?](../glossary#position-ids) - past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*): + past_key_values (`Cache`, *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`. @@ -1403,27 +1028,17 @@ def forward( if inputs_embeds is None: inputs_embeds = self.embed_tokens(input_ids) - # kept for BC (non `Cache` `past_key_values` inputs) - return_legacy_cache = False - return_self_attention_cache = False - if use_cache or past_key_values is not None: - if isinstance(past_key_values, Cache) and not isinstance(past_key_values, EncoderDecoderCache): - return_self_attention_cache = True - past_key_values = EncoderDecoderCache(past_key_values, DynamicCache()) - elif not isinstance(past_key_values, EncoderDecoderCache): - return_legacy_cache = True - logger.warning_once( - "Passing a tuple of `past_key_values` is deprecated and will be removed in Transformers v4.43.0. " - "You should pass an instance of `EncoderDecoderCache` instead, e.g. " - "`past_key_values=EncoderDecoderCache.from_legacy_cache(past_key_values)`." - ) - past_key_values = EncoderDecoderCache.from_legacy_cache(past_key_values) + if use_cache and past_key_values is None: + self_attention_cache = DynamicCache() + cross_attention_cache = DynamicCache() + past_key_values = EncoderDecoderCache(self_attention_cache, cross_attention_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 + inputs_embeds.shape[1], device=inputs_embeds.device ) + if position_ids is None: position_ids = cache_position.unsqueeze(0) @@ -1435,6 +1050,7 @@ def forward( causal_mask = self._update_causal_mask( attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions ) + hidden_states = inputs_embeds # create position embeddings to be shared across the decoder layers @@ -1493,25 +1109,14 @@ def forward( if output_hidden_states: all_hidden_states += (hidden_states,) - next_cache = past_key_values if use_cache else None - if return_self_attention_cache: - next_cache = past_key_values.self_attention_cache - if return_legacy_cache: - next_cache = next_cache.to_legacy_cache() - - if not return_dict: - return tuple( - v - for v in [hidden_states, next_cache, all_hidden_states, all_self_attns, all_cross_attentions] - if v is not None - ) - return BaseModelOutputWithPastAndCrossAttentions( + output = BaseModelOutputWithPastAndCrossAttentions( last_hidden_state=hidden_states, - past_key_values=next_cache, + past_key_values=past_key_values if use_cache else None, hidden_states=all_hidden_states, attentions=all_self_attns, cross_attentions=all_cross_attentions, ) + return output if return_dict else output.to_tuple() def _update_causal_mask( self, diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index 37d8da747c51de..10b01e94270356 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -1,6 +1,5 @@ import copy -import math -from typing import List, Optional, Tuple, Union +from typing import Callable, Optional, Tuple, Union import torch import torch.nn as nn @@ -10,7 +9,7 @@ from ...cache_utils import Cache, DynamicCache, EncoderDecoderCache from ...configuration_utils import PretrainedConfig from ...generation import GenerationMixin -from ...modeling_flash_attention_utils import FlashAttentionKwargs, _flash_attention_forward +from ...modeling_flash_attention_utils import FlashAttentionKwargs from ...modeling_outputs import ( BaseModelOutput, BaseModelOutputWithPast, @@ -18,7 +17,7 @@ Seq2SeqLMOutput, Seq2SeqModelOutput, ) -from ...modeling_utils import PreTrainedModel +from ...modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel from ...processing_utils import Unpack from ...utils import ( add_start_docstrings, @@ -26,8 +25,9 @@ logging, replace_return_docstrings, ) -from ..llama.modeling_llama import LlamaDecoderLayer, LlamaModel, repeat_kv -from ..phi.modeling_phi import PhiAttention, PhiFlashAttention2, PhiMLP, PhiRotaryEmbedding, PhiSdpaAttention +from ..cohere.modeling_cohere import CohereRotaryEmbedding +from ..llama.modeling_llama import LlamaAttention, LlamaDecoderLayer, LlamaModel, repeat_kv +from ..phi.modeling_phi import PhiMLP from ..whisper.modeling_whisper import WhisperModel, shift_tokens_right @@ -70,6 +70,8 @@ class MoonshineConfig(PretrainedConfig): The non-linear activation function (function or string) in the encoder. decoder_hidden_act (`str` or `function`, *optional*, defaults to `"silu"`): The non-linear activation function (function or string) in the decoder. + max_position_embeddings (`int`, *optional*, defaults to 2048): + The maximum sequence length that this model might ever be used with. initializer_range (`float`, *optional*, defaults to 0.02): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. layer_norm_eps (`float`, *optional*, defaults to 1e-05): @@ -89,9 +91,7 @@ class MoonshineConfig(PretrainedConfig): attention_bias (`bool`, *optional*, defaults to `False`): Whether to use a bias in the query, key, value and output projection layers during self-attention. attention_dropout (`float`, *optional*, defaults to 0.0): - The dropout ratio for the attention probabilities. - qk_layernorm (`bool`, *optional*, defaults to `False`): - Whether or not to normalize the Queries and Keys after projecting the hidden states. + The dropout ratio for the attention probabilities. ff_mult (`int`, *optional*, defaults to 4): Factor by which to scale the intermediate size. bos_token_id (`int`, *optional*, defaults to 1): @@ -162,6 +162,7 @@ def __init__( num_key_value_heads=None, encoder_hidden_act="gelu", decoder_hidden_act="silu", + max_position_embeddings=2048, initializer_range=0.02, layer_norm_eps=1e-5, decoder_start_token_id=1, @@ -171,7 +172,6 @@ def __init__( min_rotary_ndims=32, attention_bias=False, attention_dropout=0.0, - qk_layernorm=False, ff_mult=4, bos_token_id=1, eos_token_id=2, @@ -196,6 +196,7 @@ def __init__( self.num_key_value_heads = num_key_value_heads self.encoder_hidden_act = encoder_hidden_act self.decoder_hidden_act = decoder_hidden_act + self.max_position_embeddings = max_position_embeddings self.initializer_range = initializer_range self.layer_norm_eps = layer_norm_eps self.decoder_start_token_id = decoder_start_token_id @@ -205,7 +206,6 @@ def __init__( self.min_rotary_ndims = min_rotary_ndims self.attention_bias = attention_bias self.attention_dropout = attention_dropout - self.qk_layernorm = qk_layernorm self.ff_mult = ff_mult # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779 @@ -261,30 +261,8 @@ def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1): return q_embed, k_embed -class MoonshineRotaryEmbedding(PhiRotaryEmbedding): - @torch.no_grad() - def forward(self, x, position_ids): - if "dynamic" in self.rope_type: - self._dynamic_frequency_update(position_ids, device=x.device) - - # Core RoPE block - inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1) - position_ids_expanded = position_ids[:, None, :].float() - # Force float32 (see https://github.com/huggingface/transformers/pull/29285) - device_type = x.device.type - device_type = device_type if isinstance(device_type, str) and device_type != "mps" else "cpu" - with torch.autocast(device_type=device_type, enabled=False): - freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2) - emb = torch.stack((freqs, freqs), dim=-1) - emb = emb.flatten(-2) # in einsum notation: rearrange(x, '... d j -> ... (d j)') - cos = emb.cos() - sin = emb.sin() - - # Advanced RoPE types (e.g. yarn) apply a post-processing scaling factor, equivalent to scaling attention - cos = cos * self.attention_scaling - sin = sin * self.attention_scaling - - return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype) +class MoonshineRotaryEmbedding(CohereRotaryEmbedding): + pass class MoonshineNonGatedMLP(PhiMLP): @@ -326,165 +304,53 @@ def __new__(cls, config: MoonshineConfig, hidden_act: str): raise ValueError(f"Unsupported activation function: {hidden_act}, please use 'gelu' or 'silu'") -class MoonshineAttention(PhiAttention): - def __init__(self, config: MoonshineConfig, layer_idx: Optional[int] = None, is_causal: bool = False): +def eager_attention_forward( + module: nn.Module, + query: torch.Tensor, + key: torch.Tensor, + value: torch.Tensor, + attention_mask: Optional[torch.Tensor], + scaling: float, + dropout: float = 0.0, + **kwargs, +): + key_states = repeat_kv(key, module.num_key_value_groups) + value_states = repeat_kv(value, module.num_key_value_groups) + + attn_weights = torch.matmul(query, key_states.transpose(2, 3)) * scaling + if attention_mask is not None: + causal_mask = attention_mask[:, :, :, : key_states.shape[-2]] + attn_weights = attn_weights + causal_mask + + attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query.dtype) + attn_weights = nn.functional.dropout(attn_weights, p=dropout, training=module.training) + attn_output = torch.matmul(attn_weights, value_states) + attn_output = attn_output.transpose(1, 2).contiguous() + + return attn_output, attn_weights + +class MoonshineAttention(LlamaAttention): + def __init__(self, config: MoonshineConfig, layer_idx: int, is_causal: bool): super().__init__(config, layer_idx) - self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=config.attention_bias) - self.k_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config.attention_bias) - self.v_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config.attention_bias) - self.dense = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=config.attention_bias) - self.rotary_ndims = max(config.hidden_size // config.num_attention_heads // 2, config.min_rotary_ndims) - self.rotary_emb = MoonshineRotaryEmbedding(dim=self.rotary_ndims) - self.is_causal = is_causal + self.num_key_values_heads = config.num_key_value_heads def forward( self, hidden_states: torch.Tensor, + position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, - position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Cache] = None, - key_value_states: Optional[torch.Tensor] = None, - output_attentions: bool = False, - use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: - bsz, q_len, _ = hidden_states.size() - - query_states = self.q_proj(hidden_states) - - # if key_value_states are provided this layer is used as a cross-attention layer - # for the decoder - is_cross_attention = key_value_states is not None - - if past_key_value is not None: - is_updated = past_key_value.is_updated.get(self.layer_idx) - if is_cross_attention: - # after the first generated id, we can subsequently re-use all key/value_states from cache - past_key_value.is_updated[self.layer_idx] = True - past_key_value = past_key_value.cross_attention_cache - else: - past_key_value = past_key_value.self_attention_cache - - # use key_value_states if cross attention - current_states = key_value_states if key_value_states is not None else hidden_states - if is_cross_attention and past_key_value and is_updated: - # reuse k,v, cross_attentions - key_states = past_key_value.key_cache[self.layer_idx] - value_states = past_key_value.value_cache[self.layer_idx] - else: - key_states = self.k_proj(current_states) - value_states = self.v_proj(current_states) - key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) - value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) - if is_cross_attention and past_key_value is not None: - key_states, value_states = past_key_value.update( - key_states, value_states, self.layer_idx, {"cache_position": cache_position} - ) - - if self.qk_layernorm: - query_states = self.q_layernorm(query_states) - key_states = self.k_layernorm(key_states) - - query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) - - if not is_cross_attention: - if position_embeddings is None: - logger.warning_once( - "The attention layers in this model are transitioning from computing the RoPE embeddings internally " - "through `position_ids` (2D tensor with the indexes of the tokens), to using externally computed " - "`position_embeddings` (Tuple of tensors, containing cos and sin). In v4.46 `position_ids` will be " - "removed and `position_embeddings` will be mandatory." - ) - cos, sin = self.rotary_emb(value_states, position_ids) - else: - cos, sin = position_embeddings - - # Partial rotary embedding - query_rot, query_pass = ( - query_states[..., : self.rotary_ndims], - query_states[..., self.rotary_ndims :], - ) - key_rot, key_pass = ( - key_states[..., : self.rotary_ndims], - key_states[..., self.rotary_ndims :], - ) - # [batch_size, seq_length, num_heads, self.rotary_ndims] - query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin) - - # [batch_size, seq_length, num_heads, head_dim] - query_states = torch.cat((query_rot, query_pass), dim=-1) - key_states = torch.cat((key_rot, key_pass), dim=-1) - - if past_key_value is not None: - cache_kwargs = { - "sin": sin, - "cos": cos, - "partial_rotation_size": self.rotary_ndims, - "cache_position": cache_position, - } - key_states, value_states = past_key_value.update( - key_states, value_states, self.layer_idx, cache_kwargs - ) - - key_states = repeat_kv(key_states, self.num_key_value_groups) - value_states = repeat_kv(value_states, self.num_key_value_groups) - - # Queries and keys upcast to fp32 is required by Moonshine-2 to avoid overflow - attn_weights = torch.matmul( - query_states.to(torch.float32), key_states.to(torch.float32).transpose(2, 3) - ) / math.sqrt(self.head_dim) - - if attention_mask is not None: - causal_mask = attention_mask[:, :, :, : key_states.shape[-2]] - attn_weights += causal_mask - - # upcast attention to fp32 - attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(value_states.dtype) - attn_weights = nn.functional.dropout(attn_weights, p=self.attention_dropout, training=self.training) - - attn_output = torch.matmul(attn_weights, value_states) - - if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim): - raise ValueError( - f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is" - f" {attn_output.size()}" - ) - - attn_output = attn_output.transpose(1, 2).contiguous() - attn_output = attn_output.reshape(bsz, q_len, self.hidden_size) - - attn_output = self.dense(attn_output) - - if not output_attentions: - attn_weights = None - - return attn_output, attn_weights, past_key_value - - -class MoonshineFlashAttention2(PhiFlashAttention2): - def forward( - self, - hidden_states: torch.Tensor, - attention_mask: Optional[torch.Tensor] = None, - position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Cache] = None, key_value_states: Optional[torch.Tensor] = None, - output_attentions: bool = False, - use_cache: bool = False, - cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 + **kwargs: Unpack[FlashAttentionKwargs], ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: - bsz, q_len, _ = hidden_states.size() + bsz, q_len = hidden_states.shape[:-1] - query_states = self.q_proj(hidden_states) + query_states = self.q_proj(hidden_states).view(bsz, q_len, self.config.num_key_value_heads, self.head_dim).transpose(1, 2) - # if key_value_states are provided this layer is used as a cross-attention layer - # for the decoder is_cross_attention = key_value_states is not None - if past_key_value is not None: is_updated = past_key_value.is_updated.get(self.layer_idx) if is_cross_attention: @@ -497,36 +363,18 @@ def forward( # use key_value_states if cross attention current_states = key_value_states if key_value_states is not None else hidden_states if is_cross_attention and past_key_value and is_updated: - # reuse k,v, cross_attentions key_states = past_key_value.key_cache[self.layer_idx] value_states = past_key_value.value_cache[self.layer_idx] else: - key_states = self.k_proj(current_states) - value_states = self.v_proj(current_states) - key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) - value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) + key_states = self.k_proj(current_states).view(bsz, -1, self.config.num_key_value_heads, self.head_dim).transpose(1, 2) + value_states = self.v_proj(current_states).view(bsz, -1, self.config.num_key_value_heads, self.head_dim).transpose(1, 2) if is_cross_attention and past_key_value is not None: key_states, value_states = past_key_value.update( key_states, value_states, self.layer_idx, {"cache_position": cache_position} ) - if self.qk_layernorm: - query_states = self.q_layernorm(query_states) - key_states = self.k_layernorm(key_states) - - query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) - if not is_cross_attention: - if position_embeddings is None: - logger.warning_once( - "The attention layers in this model are transitioning from computing the RoPE embeddings internally " - "through `position_ids` (2D tensor with the indexes of the tokens), to using externally computed " - "`position_embeddings` (Tuple of tensors, containing cos and sin). In v4.46 `position_ids` will be " - "removed and `position_embeddings` will be mandatory." - ) - cos, sin = self.rotary_emb(value_states, position_ids) - else: - cos, sin = position_embeddings + cos, sin = position_embeddings # Partial rotary embedding query_rot, query_pass = ( @@ -537,7 +385,7 @@ def forward( key_states[..., : self.rotary_ndims], key_states[..., self.rotary_ndims :], ) - # [batch_size, seq_length, num_heads, self.rotary_ndims] + # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor] query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin) # [batch_size, seq_length, num_heads, head_dim] @@ -555,199 +403,40 @@ def forward( key_states, value_states, 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. - query_states = query_states.transpose(1, 2) - key_states = key_states.transpose(1, 2) - value_states = value_states.transpose(1, 2) - - attn_dropout = self.attention_dropout if self.training else 0.0 - - # 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. - # This might slowdown training & inference so it is recommended to not cast the LayerNorms - # in fp32. - - if query_states.dtype == torch.float32: - if torch.is_autocast_enabled(): - target_dtype = torch.get_autocast_gpu_dtype() - # Handle the case where the model is quantized - elif hasattr(self.config, "_pre_quantization_dtype"): - target_dtype = self.config._pre_quantization_dtype - else: - target_dtype = self.q_proj.weight.dtype - - logger.warning_once( - f"The input hidden states seems to be silently casted in float32, this might be related to" - f" the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in" - f" {target_dtype}." - ) - - query_states = query_states.to(target_dtype) - key_states = key_states.to(target_dtype) - value_states = value_states.to(target_dtype) - - attn_output = _flash_attention_forward( - query_states, - key_states, - value_states, - attention_mask, - q_len, - position_ids=position_ids, - dropout=attn_dropout, - softmax_scale=None, - use_top_left_mask=self._flash_attn_uses_top_left_mask, - is_causal=self.is_causal, - ) - - attn_output = attn_output.reshape(bsz, q_len, self.hidden_size).contiguous() - attn_output = self.dense(attn_output) - - if not output_attentions: - attn_weights = None - - return attn_output, attn_weights, past_key_value - - -class MoonshineSdpaAttention(PhiSdpaAttention): - def forward( - self, - hidden_states: torch.Tensor, - attention_mask: Optional[torch.Tensor] = None, - position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Cache] = None, - key_value_states: Optional[torch.Tensor] = None, - output_attentions: bool = False, - use_cache: bool = False, - cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: - bsz, q_len, _ = hidden_states.size() - - query_states = self.q_proj(hidden_states) - - # if key_value_states are provided this layer is used as a cross-attention layer - # for the decoder - is_cross_attention = key_value_states is not None - - if past_key_value is not None: - is_updated = past_key_value.is_updated.get(self.layer_idx) - if is_cross_attention: - # after the first generated id, we can subsequently re-use all key/value_states from cache - past_key_value.is_updated[self.layer_idx] = True - past_key_value = past_key_value.cross_attention_cache - else: - past_key_value = past_key_value.self_attention_cache - - # use key_value_states if cross attention - current_states = key_value_states if key_value_states is not None else hidden_states - if is_cross_attention and past_key_value and is_updated: - # reuse k,v, cross_attentions - key_states = past_key_value.key_cache[self.layer_idx] - value_states = past_key_value.value_cache[self.layer_idx] - else: - key_states = self.k_proj(current_states) - value_states = self.v_proj(current_states) - key_states = key_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) - value_states = value_states.view(bsz, -1, self.num_key_value_heads, self.head_dim).transpose(1, 2) - if is_cross_attention and past_key_value is not None: - key_states, value_states = past_key_value.update( - key_states, value_states, self.layer_idx, {"cache_position": cache_position} - ) - - if self.qk_layernorm: - query_states = self.q_layernorm(query_states) - key_states = self.k_layernorm(key_states) - - query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) - - if not is_cross_attention: - if position_embeddings is None: + attention_interface: Callable = eager_attention_forward + if self.config._attn_implementation != "eager": + if self.config._attn_implementation == "sdpa" and kwargs.get("output_attentions", False): logger.warning_once( - "The attention layers in this model are transitioning from computing the RoPE embeddings internally " - "through `position_ids` (2D tensor with the indexes of the tokens), to using externally computed " - "`position_embeddings` (Tuple of tensors, containing cos and sin). In v4.46 `position_ids` will be " - "removed and `position_embeddings` will be mandatory." + "`torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to " + 'eager attention. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.' ) - cos, sin = self.rotary_emb(value_states, position_ids) else: - cos, sin = position_embeddings - - # Partial rotary embedding - query_rot, query_pass = ( - query_states[..., : self.rotary_ndims], - query_states[..., self.rotary_ndims :], - ) - key_rot, key_pass = ( - key_states[..., : self.rotary_ndims], - key_states[..., self.rotary_ndims :], - ) - # [batch_size, seq_length, num_heads, self.rotary_ndims] - query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin) - - # [batch_size, seq_length, num_heads, head_dim] - query_states = torch.cat((query_rot, query_pass), dim=-1) - key_states = torch.cat((key_rot, key_pass), dim=-1) - - if past_key_value is not None: - cache_kwargs = { - "sin": sin, - "cos": cos, - "partial_rotation_size": self.rotary_ndims, - "cache_position": cache_position, - } - key_states, value_states = past_key_value.update( - key_states, value_states, self.layer_idx, cache_kwargs - ) - - key_states = repeat_kv(key_states, self.num_key_value_groups) - value_states = repeat_kv(value_states, self.num_key_value_groups) - - causal_mask = attention_mask - if attention_mask is not None: - causal_mask = causal_mask[:, :, :, : key_states.shape[-2]] - - # SDPA with memory-efficient backend is broken in torch==2.1.2 when using non-contiguous inputs and a custom - # attn_mask, so we need to call `.contiguous()` here. This was fixed in torch==2.2.0. - # Reference: https://github.com/pytorch/pytorch/issues/112577 - if self.require_contiguous_qkv and query_states.device.type == "cuda" and attention_mask is not None: - query_states = query_states.contiguous() - key_states = key_states.contiguous() - value_states = value_states.contiguous() - - # 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 self.is_causal and causal_mask is None and q_len > 1 else False + attention_interface = ALL_ATTENTION_FUNCTIONS[self.config._attn_implementation] - attn_output = torch.nn.functional.scaled_dot_product_attention( + is_causal = True if self.is_causal and attention_mask is None and q_len > 1 else False + attn_output, attn_weights = attention_interface( + self, query_states, key_states, value_states, - attn_mask=causal_mask, - dropout_p=self.attention_dropout if self.training else 0.0, + attention_mask, + dropout=0.0 if not self.training else self.attention_dropout, + scaling=self.scaling, is_causal=is_causal, + **kwargs, ) - attn_output = attn_output.transpose(1, 2).contiguous() - attn_output = attn_output.reshape(bsz, q_len, self.hidden_size) - - attn_output = self.dense(attn_output) - - return attn_output, None, past_key_value - - -MOONSHINE_ATTENTION_CLASSES = { - "eager": MoonshineAttention, - "flash_attention_2": MoonshineFlashAttention2, - "sdpa": MoonshineSdpaAttention, -} + attn_output = attn_output.reshape(bsz, q_len, -1).contiguous() + attn_output = self.o_proj(attn_output) + return attn_output, attn_weights class MoonshineEncoderLayer(LlamaDecoderLayer): def __init__(self, config: MoonshineConfig, layer_idx: int): super().__init__(config, layer_idx) + self.self_attn = MoonshineAttention(config=config, layer_idx=layer_idx, is_causal=False) + self.mlp = MoonshineMLP(config, config.encoder_hidden_act) self.input_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) self.post_attention_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) @@ -758,12 +447,8 @@ def __init__(self, config: MoonshineConfig, layer_idx: int = None): super().__init__() self.hidden_size = config.hidden_size - self.self_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation]( - config=config, layer_idx=layer_idx, is_causal=True - ) - self.encoder_attn = MOONSHINE_ATTENTION_CLASSES[config._attn_implementation]( - config=config, layer_idx=layer_idx, is_causal=False - ) + self.self_attn = MoonshineAttention(config=config, layer_idx=layer_idx, is_causal=True) + self.encoder_attn = MoonshineAttention(config=config, layer_idx=layer_idx, is_causal=False) self.mlp = MoonshineMLP(config, config.decoder_hidden_act) self.input_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) @@ -820,7 +505,7 @@ def forward( hidden_states = self.input_layernorm(hidden_states) # Self Attention - hidden_states, self_attn_weights, present_key_value = self.self_attn( + hidden_states, self_attn_weights = self.self_attn( hidden_states=hidden_states, attention_mask=attention_mask, position_ids=position_ids, @@ -838,7 +523,7 @@ def forward( if encoder_hidden_states is not None: residual = hidden_states hidden_states = self.post_attention_layernorm(hidden_states) - hidden_states, cross_attn_weights, cross_attn_present_key_value = self.encoder_attn( + hidden_states, cross_attn_weights = self.encoder_attn( hidden_states=hidden_states, key_value_states=encoder_hidden_states, attention_mask=encoder_attention_mask, @@ -850,9 +535,6 @@ def forward( ) hidden_states = residual + hidden_states - # add cross-attn to positions 1 of present_key_value tuple - present_key_value = (present_key_value, cross_attn_present_key_value) - # Fully Connected residual = hidden_states hidden_states = self.final_layernorm(hidden_states) @@ -864,9 +546,6 @@ def forward( if output_attentions: outputs += (self_attn_weights, cross_attn_weights) - if use_cache: - outputs += (present_key_value,) - return outputs @@ -1049,7 +728,7 @@ def forward( input_values: Optional[torch.FloatTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Cache] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1074,7 +753,7 @@ def forward( config.n_positions - 1]`. [What are position IDs?](../glossary#position-ids) - past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*): + past_key_values (`Cache`, *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`. @@ -1129,25 +808,17 @@ def forward( if inputs_embeds is None: inputs_embeds = self.preprocess(input_values) - # kept for BC (non `Cache` `past_key_values` inputs) - return_legacy_cache = False - if use_cache or past_key_values is not None: - if isinstance(past_key_values, Cache) and not isinstance(past_key_values, EncoderDecoderCache): - past_key_values = EncoderDecoderCache(past_key_values, DynamicCache()) - elif not isinstance(past_key_values, EncoderDecoderCache): - return_legacy_cache = True - logger.warning_once( - "Passing a tuple of `past_key_values` is deprecated and will be removed in Transformers v4.43.0. " - "You should pass an instance of `EncoderDecoderCache` instead, e.g. " - "`past_key_values=EncoderDecoderCache.from_legacy_cache(past_key_values)`." - ) - past_key_values = EncoderDecoderCache.from_legacy_cache(past_key_values) + if use_cache and past_key_values is None: + self_attention_cache = DynamicCache() + cross_attention_cache = DynamicCache() + past_key_values = EncoderDecoderCache(self_attention_cache, cross_attention_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 + inputs_embeds.shape[1], device=inputs_embeds.device ) + if position_ids is None: position_ids = cache_position.unsqueeze(0) @@ -1159,7 +830,6 @@ def forward( # decoder layers all_hidden_states = () if output_hidden_states else None all_self_attns = () if output_attentions else None - next_encoder_cache = None for encoder_layer in self.layers: if output_hidden_states: @@ -1191,9 +861,6 @@ def forward( hidden_states = layer_outputs[0] - if use_cache: - next_encoder_cache = layer_outputs[2 if output_attentions else 1] - if output_attentions: all_self_attns += (layer_outputs[1],) @@ -1203,19 +870,13 @@ def forward( if output_hidden_states: all_hidden_states += (hidden_states,) - next_cache = next_encoder_cache if use_cache else None - if return_legacy_cache: - next_cache = next_cache.to_legacy_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( + output = BaseModelOutputWithPast( last_hidden_state=hidden_states, - past_key_values=next_cache, + past_key_values=past_key_values if use_cache else None, hidden_states=all_hidden_states, attentions=all_self_attns, ) - + return output if return_dict else output.to_tuple() @add_start_docstrings( "The bare Moonshine decoder outputting raw hidden-states without any specific head on top.", @@ -1236,7 +897,7 @@ def forward( encoder_hidden_states: Optional[torch.FloatTensor] = None, position_ids: Optional[torch.LongTensor] = None, encoder_position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Cache] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1286,7 +947,7 @@ def forward( Indices of positions of each encoder input's hidden states in the position embeddings. [What are position IDs?](../glossary#position-ids) - past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*): + past_key_values (`Cache`, *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`. @@ -1343,27 +1004,17 @@ def forward( if inputs_embeds is None: inputs_embeds = self.embed_tokens(input_ids) - # kept for BC (non `Cache` `past_key_values` inputs) - return_legacy_cache = False - return_self_attention_cache = False - if use_cache or past_key_values is not None: - if isinstance(past_key_values, Cache) and not isinstance(past_key_values, EncoderDecoderCache): - return_self_attention_cache = True - past_key_values = EncoderDecoderCache(past_key_values, DynamicCache()) - elif not isinstance(past_key_values, EncoderDecoderCache): - return_legacy_cache = True - logger.warning_once( - "Passing a tuple of `past_key_values` is deprecated and will be removed in Transformers v4.43.0. " - "You should pass an instance of `EncoderDecoderCache` instead, e.g. " - "`past_key_values=EncoderDecoderCache.from_legacy_cache(past_key_values)`." - ) - past_key_values = EncoderDecoderCache.from_legacy_cache(past_key_values) + if use_cache and past_key_values is None: + self_attention_cache = DynamicCache() + cross_attention_cache = DynamicCache() + past_key_values = EncoderDecoderCache(self_attention_cache, cross_attention_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 + inputs_embeds.shape[1], device=inputs_embeds.device ) + if position_ids is None: position_ids = cache_position.unsqueeze(0) @@ -1375,6 +1026,7 @@ def forward( causal_mask = self._update_causal_mask( attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions ) + hidden_states = inputs_embeds # create position embeddings to be shared across the decoder layers @@ -1433,25 +1085,14 @@ def forward( if output_hidden_states: all_hidden_states += (hidden_states,) - next_cache = past_key_values if use_cache else None - if return_self_attention_cache: - next_cache = past_key_values.self_attention_cache - if return_legacy_cache: - next_cache = next_cache.to_legacy_cache() - - if not return_dict: - return tuple( - v - for v in [hidden_states, next_cache, all_hidden_states, all_self_attns, all_cross_attentions] - if v is not None - ) - return BaseModelOutputWithPastAndCrossAttentions( + output = BaseModelOutputWithPastAndCrossAttentions( last_hidden_state=hidden_states, - past_key_values=next_cache, + past_key_values=past_key_values if use_cache else None, hidden_states=all_hidden_states, attentions=all_self_attns, cross_attentions=all_cross_attentions, ) + return output if return_dict else output.to_tuple() class MoonshineModel(WhisperModel): diff --git a/src/transformers/utils/dummy_pt_objects.py b/src/transformers/utils/dummy_pt_objects.py index 3ee8bc0c14348d..3d176246798667 100644 --- a/src/transformers/utils/dummy_pt_objects.py +++ b/src/transformers/utils/dummy_pt_objects.py @@ -6437,8 +6437,8 @@ class MoonshineModel(metaclass=DummyObject): def __init__(self, *args, **kwargs): requires_backends(self, ["torch"]) - - + + class ModernBertForSequenceClassification(metaclass=DummyObject): _backends = ["torch"] @@ -6451,8 +6451,8 @@ class MoonshinePreTrainedModel(metaclass=DummyObject): def __init__(self, *args, **kwargs): requires_backends(self, ["torch"]) - - + + class ModernBertForTokenClassification(metaclass=DummyObject): _backends = ["torch"] From 52618866a3ad30dcda66995d49996c6dbb1c1615 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Fri, 20 Dec 2024 15:52:02 +0100 Subject: [PATCH 33/39] modular edge case --- .../models/moonshine/modeling_moonshine.py | 29 ++++++++++++------- .../models/moonshine/modular_moonshine.py | 27 ++++++++++------- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/transformers/models/moonshine/modeling_moonshine.py b/src/transformers/models/moonshine/modeling_moonshine.py index 91dd7fc4fcc7b7..363316dbd6cb28 100644 --- a/src/transformers/models/moonshine/modeling_moonshine.py +++ b/src/transformers/models/moonshine/modeling_moonshine.py @@ -192,13 +192,18 @@ def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor: return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim) -def rotate_every_two(x: torch.Tensor) -> torch.Tensor: - x1 = x[:, :, :, ::2] - x2 = x[:, :, :, 1::2] - x = torch.stack((-x2, x1), dim=-1) - return x.flatten(-2) # in einsum notation: rearrange(x, '... d j -> ... (d j)') - - +# modular edge case: cannot import from Cohere's modeling file since it is call in the attention that inherits from LlamaAttention +# should be removed in the future +def rotate_half(x): + # Split and rotate. Note that this function is different from e.g. Llama. + x1 = x[..., ::2] + x2 = x[..., 1::2] + rot_x = torch.stack([-x2, x1], dim=-1).flatten(-2) + return rot_x + + +# modular edge case: cannot import from Cohere's modeling file since it is call in the attention that inherits from LlamaAttention +# should be removed in the future def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1): """Applies Rotary Position Embedding to the query and key tensors. @@ -219,12 +224,14 @@ def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1): Returns: `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding. """ + dtype = q.dtype + q = q.float() + k = k.float() cos = cos.unsqueeze(unsqueeze_dim) sin = sin.unsqueeze(unsqueeze_dim) - - q_embed = (q * cos) + (rotate_every_two(q) * sin) - k_embed = (k * cos) + (rotate_every_two(k) * sin) - return q_embed, k_embed + q_embed = (q * cos) + (rotate_half(q) * sin) + k_embed = (k * cos) + (rotate_half(k) * sin) + return q_embed.to(dtype=dtype), k_embed.to(dtype=dtype) def eager_attention_forward( diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index 10b01e94270356..e3e3f03bfd27b8 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -25,7 +25,7 @@ logging, replace_return_docstrings, ) -from ..cohere.modeling_cohere import CohereRotaryEmbedding +from ..cohere.modeling_cohere import apply_rotary_pos_emb, rotate_half, CohereRotaryEmbedding from ..llama.modeling_llama import LlamaAttention, LlamaDecoderLayer, LlamaModel, repeat_kv from ..phi.modeling_phi import PhiMLP from ..whisper.modeling_whisper import WhisperModel, shift_tokens_right @@ -226,13 +226,18 @@ def __init__( ) -def rotate_every_two(x: torch.Tensor) -> torch.Tensor: - x1 = x[:, :, :, ::2] - x2 = x[:, :, :, 1::2] - x = torch.stack((-x2, x1), dim=-1) - return x.flatten(-2) # in einsum notation: rearrange(x, '... d j -> ... (d j)') +# modular edge case: cannot import from Cohere's modeling file since it is call in the attention that inherits from LlamaAttention +# should be removed in the future +def rotate_half(x): + # Split and rotate. Note that this function is different from e.g. Llama. + x1 = x[..., ::2] + x2 = x[..., 1::2] + rot_x = torch.stack([-x2, x1], dim=-1).flatten(-2) + return rot_x +# modular edge case: cannot import from Cohere's modeling file since it is call in the attention that inherits from LlamaAttention +# should be removed in the future def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1): """Applies Rotary Position Embedding to the query and key tensors. @@ -253,12 +258,14 @@ def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1): Returns: `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding. """ + dtype = q.dtype + q = q.float() + k = k.float() cos = cos.unsqueeze(unsqueeze_dim) sin = sin.unsqueeze(unsqueeze_dim) - - q_embed = (q * cos) + (rotate_every_two(q) * sin) - k_embed = (k * cos) + (rotate_every_two(k) * sin) - return q_embed, k_embed + q_embed = (q * cos) + (rotate_half(q) * sin) + k_embed = (k * cos) + (rotate_half(k) * sin) + return q_embed.to(dtype=dtype), k_embed.to(dtype=dtype) class MoonshineRotaryEmbedding(CohereRotaryEmbedding): From 467b8e47eb50fe81087469ffd28e873f737d8c5e Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Thu, 2 Jan 2025 20:25:38 +0100 Subject: [PATCH 34/39] remove encoder --- .../models/moonshine/modular_moonshine.py | 68 +++---------------- 1 file changed, 8 insertions(+), 60 deletions(-) diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index e3e3f03bfd27b8..c3c38505b50801 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -734,14 +734,10 @@ def forward( self, input_values: Optional[torch.FloatTensor] = None, attention_mask: Optional[torch.Tensor] = None, - position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Cache] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - cache_position: Optional[torch.LongTensor] = None, **flash_attn_kwargs: Unpack[FlashAttentionKwargs], ) -> Union[Tuple, BaseModelOutputWithPast]: r""" @@ -755,29 +751,6 @@ def forward( attention_mask (`torch.Tensor`)`, *optional*): Moonshine does not support masking of the `input_values`, this argument is preserved for compatibility, but it is not used. - position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): - Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0, - config.n_positions - 1]`. - - [What are position IDs?](../glossary#position-ids) - past_key_values (`Cache`, *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, see our - [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache); - - 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)`. inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Optionally, instead of passing `input_values` you can choose to directly pass an embedded representation, where embedded here refers to preprocessed input values that can be obtained by passing `input_values` to the encoder `preprocess` method. @@ -791,50 +764,29 @@ def forward( 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. """ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) - 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_values is None) ^ (inputs_embeds is not None): raise ValueError("You must specify exactly one of input_values or inputs_embeds") - 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.preprocess(input_values) - if use_cache and past_key_values is None: - self_attention_cache = DynamicCache() - cross_attention_cache = DynamicCache() - past_key_values = EncoderDecoderCache(self_attention_cache, cross_attention_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 + inputs_embeds.shape[1], device=inputs_embeds.device - ) - - if position_ids is None: - position_ids = cache_position.unsqueeze(0) + position_ids = torch.arange( + 0, inputs_embeds.shape[1], device=inputs_embeds.device + ).unsqueeze(0) hidden_states = inputs_embeds # create position embeddings to be shared across the decoder layers position_embeddings = self.rotary_emb(hidden_states, position_ids) - # decoder layers + # encoder layers all_hidden_states = () if output_hidden_states else None all_self_attns = () if output_attentions else None @@ -848,20 +800,17 @@ def forward( hidden_states, None, position_ids, - past_key_values, + None, output_attentions, - use_cache, - cache_position, + False, + None, position_embeddings, ) else: layer_outputs = encoder_layer( hidden_states, position_ids=position_ids, - past_key_value=past_key_values, output_attentions=output_attentions, - use_cache=use_cache, - cache_position=cache_position, position_embeddings=position_embeddings, **flash_attn_kwargs, ) @@ -873,13 +822,12 @@ def forward( hidden_states = self.layer_norm(hidden_states) - # add hidden states from the last decoder layer + # add hidden states from the last encoder layer if output_hidden_states: all_hidden_states += (hidden_states,) output = BaseModelOutputWithPast( last_hidden_state=hidden_states, - past_key_values=past_key_values if use_cache else None, hidden_states=all_hidden_states, attentions=all_self_attns, ) From 445dfcbd09ffc0ffcd575c2b366b77587fde3ad8 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Thu, 2 Jan 2025 20:27:24 +0100 Subject: [PATCH 35/39] convolutions params in config --- .../models/moonshine/modular_moonshine.py | 46 +++++++++++++++++-- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index c3c38505b50801..c9c1242dd4a3fa 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -25,7 +25,7 @@ logging, replace_return_docstrings, ) -from ..cohere.modeling_cohere import apply_rotary_pos_emb, rotate_half, CohereRotaryEmbedding +from ..cohere.modeling_cohere import CohereRotaryEmbedding, apply_rotary_pos_emb, rotate_half from ..llama.modeling_llama import LlamaAttention, LlamaDecoderLayer, LlamaModel, repeat_kv from ..phi.modeling_phi import PhiMLP from ..whisper.modeling_whisper import WhisperModel, shift_tokens_right @@ -54,6 +54,18 @@ class MoonshineConfig(PretrainedConfig): Dimension of the hidden representations. intermediate_size (`int`, *optional*): Dimension of the MLP representations. + conv1_kernel_size (`int`, *optional*, defaults to 127): + Kernel size of the first convolutional layer. + conv1_stride (`int`, *optional*, defaults to 64): + Stride of the first convolutional layer. + conv2_kernel_size (`int`, *optional*, defaults to 7): + Kernel size of the second convolutional layer. + conv2_stride (`int`, *optional*, defaults to 3): + Stride of the second convolutional layer. + conv3_kernel_size (`int`, *optional*, defaults to 3): + Kernel size of the third convolutional layer. + conv3_stride (`int`, *optional*, defaults to 2): + Stride of the third convolutional layer. num_hidden_layers (`int`, *optional*, defaults to 6): Number of hidden layers in the Transformer encoder and decoder. num_attention_heads (`int`, *optional*, defaults to 8): @@ -157,6 +169,12 @@ def __init__( vocab_size=32768, hidden_size=288, intermediate_size=None, + conv1_kernel_size=127, + conv1_stride=64, + conv2_kernel_size=7, + conv2_stride=3, + conv3_kernel_size=3, + conv3_stride=2, num_hidden_layers=6, num_attention_heads=8, num_key_value_heads=None, @@ -187,6 +205,12 @@ def __init__( self.vocab_size = vocab_size self.hidden_size = hidden_size self.intermediate_size = intermediate_size + self.conv1_kernel_size = conv1_kernel_size + self.conv1_stride = conv1_stride + self.conv2_kernel_size = conv2_kernel_size + self.conv2_stride = conv2_stride + self.conv3_kernel_size = conv3_kernel_size + self.conv3_stride = conv3_stride self.num_hidden_layers = num_hidden_layers self.num_attention_heads = num_attention_heads @@ -580,6 +604,7 @@ def forward( class MoonshinePreTrainedModel(PreTrainedModel): config_class = MoonshineConfig base_model_prefix = "model" + main_input_name = "input_values" supports_gradient_checkpointing = True _no_split_modules = ["MoonshineDecoderLayer"] _skip_keys_device_placement = ["past_key_values"] @@ -591,7 +616,7 @@ class MoonshinePreTrainedModel(PreTrainedModel): def _init_weights(self, module): std = self.config.initializer_range - if isinstance(module, nn.Linear): + if isinstance(module, (nn.Linear, nn.Conv1d)): module.weight.data.normal_(mean=0.0, std=std) if module.bias is not None: module.bias.data.zero_() @@ -600,6 +625,16 @@ def _init_weights(self, module): if module.padding_idx is not None: module.weight.data[module.padding_idx].zero_() + def _get_feat_extract_output_lengths(self, input_lengths: torch.LongTensor): + """ + Computes the output length of the convolutional layers + """ + output_conv1_length = int((input_lengths - self.config.conv1_kernel_size) / self.config.conv1_stride + 1) + output_conv2_length = int((output_conv1_length - self.config.conv2_kernel_size) / self.config.conv2_stride + 1) + output_conv3_length = int((output_conv2_length - self.config.conv3_kernel_size) / self.config.conv3_stride + 1) + + return output_conv3_length + MOONSHINE_INPUTS_DOCSTRING = r""" Args: @@ -695,9 +730,9 @@ def __init__(self, config: MoonshineConfig): self.config = config embed_dim = config.hidden_size - self.conv1 = nn.Conv1d(1, embed_dim, kernel_size=127, stride=64, bias=False) - self.conv2 = nn.Conv1d(embed_dim, 2 * embed_dim, kernel_size=7, stride=3) - self.conv3 = nn.Conv1d(2 * embed_dim, embed_dim, kernel_size=3, stride=2) + self.conv1 = nn.Conv1d(1, embed_dim, kernel_size=config.conv1_kernel_size, stride=config.conv1_stride, bias=False) + self.conv2 = nn.Conv1d(embed_dim, 2 * embed_dim, kernel_size=config.conv2_kernel_size, stride=config.conv2_stride) + self.conv3 = nn.Conv1d(2 * embed_dim, embed_dim, kernel_size=config.conv3_kernel_size, stride=config.conv3_stride) self.groupnorm = nn.GroupNorm(num_groups=1, num_channels=embed_dim, eps=1e-5) self.rotary_emb = MoonshineRotaryEmbedding( @@ -838,6 +873,7 @@ def forward( MOONSHINE_START_DOCSTRING, ) class MoonshineDecoder(LlamaModel): + main_input_name = "input_ids" def __init__(self, config: MoonshineConfig): super().__init__(config) self.norm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps, bias=False) From a656e8cf054ee60176b3b998d3665881be0509e6 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Thu, 2 Jan 2025 20:28:16 +0100 Subject: [PATCH 36/39] run modular_model_converter --- .../moonshine/configuration_moonshine.py | 24 +++++ .../models/moonshine/modeling_moonshine.py | 93 ++++++------------- 2 files changed, 53 insertions(+), 64 deletions(-) diff --git a/src/transformers/models/moonshine/configuration_moonshine.py b/src/transformers/models/moonshine/configuration_moonshine.py index 2c1f429ca0757f..e8ffd48206802e 100644 --- a/src/transformers/models/moonshine/configuration_moonshine.py +++ b/src/transformers/models/moonshine/configuration_moonshine.py @@ -25,6 +25,18 @@ class MoonshineConfig(PretrainedConfig): Dimension of the hidden representations. intermediate_size (`int`, *optional*): Dimension of the MLP representations. + conv1_kernel_size (`int`, *optional*, defaults to 127): + Kernel size of the first convolutional layer. + conv1_stride (`int`, *optional*, defaults to 64): + Stride of the first convolutional layer. + conv2_kernel_size (`int`, *optional*, defaults to 7): + Kernel size of the second convolutional layer. + conv2_stride (`int`, *optional*, defaults to 3): + Stride of the second convolutional layer. + conv3_kernel_size (`int`, *optional*, defaults to 3): + Kernel size of the third convolutional layer. + conv3_stride (`int`, *optional*, defaults to 2): + Stride of the third convolutional layer. num_hidden_layers (`int`, *optional*, defaults to 6): Number of hidden layers in the Transformer encoder and decoder. num_attention_heads (`int`, *optional*, defaults to 8): @@ -128,6 +140,12 @@ def __init__( vocab_size=32768, hidden_size=288, intermediate_size=None, + conv1_kernel_size=127, + conv1_stride=64, + conv2_kernel_size=7, + conv2_stride=3, + conv3_kernel_size=3, + conv3_stride=2, num_hidden_layers=6, num_attention_heads=8, num_key_value_heads=None, @@ -158,6 +176,12 @@ def __init__( self.vocab_size = vocab_size self.hidden_size = hidden_size self.intermediate_size = intermediate_size + self.conv1_kernel_size = conv1_kernel_size + self.conv1_stride = conv1_stride + self.conv2_kernel_size = conv2_kernel_size + self.conv2_stride = conv2_stride + self.conv3_kernel_size = conv3_kernel_size + self.conv3_stride = conv3_stride self.num_hidden_layers = num_hidden_layers self.num_attention_heads = num_attention_heads diff --git a/src/transformers/models/moonshine/modeling_moonshine.py b/src/transformers/models/moonshine/modeling_moonshine.py index 363316dbd6cb28..20e388c9d48c30 100644 --- a/src/transformers/models/moonshine/modeling_moonshine.py +++ b/src/transformers/models/moonshine/modeling_moonshine.py @@ -578,6 +578,7 @@ def forward( class MoonshinePreTrainedModel(PreTrainedModel): config_class = MoonshineConfig base_model_prefix = "model" + main_input_name = "input_values" supports_gradient_checkpointing = True _no_split_modules = ["MoonshineDecoderLayer"] _skip_keys_device_placement = ["past_key_values"] @@ -589,7 +590,7 @@ class MoonshinePreTrainedModel(PreTrainedModel): def _init_weights(self, module): std = self.config.initializer_range - if isinstance(module, nn.Linear): + if isinstance(module, (nn.Linear, nn.Conv1d)): module.weight.data.normal_(mean=0.0, std=std) if module.bias is not None: module.bias.data.zero_() @@ -598,6 +599,16 @@ def _init_weights(self, module): if module.padding_idx is not None: module.weight.data[module.padding_idx].zero_() + def _get_feat_extract_output_lengths(self, input_lengths: torch.LongTensor): + """ + Computes the output length of the convolutional layers + """ + output_conv1_length = int((input_lengths - self.config.conv1_kernel_size) / self.config.conv1_stride + 1) + output_conv2_length = int((output_conv1_length - self.config.conv2_kernel_size) / self.config.conv2_stride + 1) + output_conv3_length = int((output_conv2_length - self.config.conv3_kernel_size) / self.config.conv3_stride + 1) + + return output_conv3_length + @add_start_docstrings( "The bare Moonshine encoder outputting raw hidden-states.", @@ -618,9 +629,15 @@ def __init__(self, config: MoonshineConfig): self.config = config embed_dim = config.hidden_size - self.conv1 = nn.Conv1d(1, embed_dim, kernel_size=127, stride=64, bias=False) - self.conv2 = nn.Conv1d(embed_dim, 2 * embed_dim, kernel_size=7, stride=3) - self.conv3 = nn.Conv1d(2 * embed_dim, embed_dim, kernel_size=3, stride=2) + self.conv1 = nn.Conv1d( + 1, embed_dim, kernel_size=config.conv1_kernel_size, stride=config.conv1_stride, bias=False + ) + self.conv2 = nn.Conv1d( + embed_dim, 2 * embed_dim, kernel_size=config.conv2_kernel_size, stride=config.conv2_stride + ) + self.conv3 = nn.Conv1d( + 2 * embed_dim, embed_dim, kernel_size=config.conv3_kernel_size, stride=config.conv3_stride + ) self.groupnorm = nn.GroupNorm(num_groups=1, num_channels=embed_dim, eps=1e-5) self.rotary_emb = MoonshineRotaryEmbedding( @@ -657,14 +674,10 @@ def forward( self, input_values: Optional[torch.FloatTensor] = None, attention_mask: Optional[torch.Tensor] = None, - position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Cache] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - cache_position: Optional[torch.LongTensor] = None, **flash_attn_kwargs: Unpack[FlashAttentionKwargs], ) -> Union[Tuple, BaseModelOutputWithPast]: r""" @@ -678,29 +691,6 @@ def forward( attention_mask (`torch.Tensor`)`, *optional*): Moonshine does not support masking of the `input_values`, this argument is preserved for compatibility, but it is not used. - position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): - Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0, - config.n_positions - 1]`. - - [What are position IDs?](../glossary#position-ids) - past_key_values (`Cache`, *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, see our - [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache); - - 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)`. inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Optionally, instead of passing `input_values` you can choose to directly pass an embedded representation, where embedded here refers to preprocessed input values that can be obtained by passing `input_values` to the encoder `preprocess` method. @@ -714,50 +704,27 @@ def forward( 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. """ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) - 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_values is None) ^ (inputs_embeds is not None): raise ValueError("You must specify exactly one of input_values or inputs_embeds") - 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.preprocess(input_values) - if use_cache and past_key_values is None: - self_attention_cache = DynamicCache() - cross_attention_cache = DynamicCache() - past_key_values = EncoderDecoderCache(self_attention_cache, cross_attention_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 + inputs_embeds.shape[1], device=inputs_embeds.device - ) - - if position_ids is None: - position_ids = cache_position.unsqueeze(0) + position_ids = torch.arange(0, inputs_embeds.shape[1], device=inputs_embeds.device).unsqueeze(0) hidden_states = inputs_embeds # create position embeddings to be shared across the decoder layers position_embeddings = self.rotary_emb(hidden_states, position_ids) - # decoder layers + # encoder layers all_hidden_states = () if output_hidden_states else None all_self_attns = () if output_attentions else None @@ -771,20 +738,17 @@ def forward( hidden_states, None, position_ids, - past_key_values, + None, output_attentions, - use_cache, - cache_position, + False, + None, position_embeddings, ) else: layer_outputs = encoder_layer( hidden_states, position_ids=position_ids, - past_key_value=past_key_values, output_attentions=output_attentions, - use_cache=use_cache, - cache_position=cache_position, position_embeddings=position_embeddings, **flash_attn_kwargs, ) @@ -796,13 +760,12 @@ def forward( hidden_states = self.layer_norm(hidden_states) - # add hidden states from the last decoder layer + # add hidden states from the last encoder layer if output_hidden_states: all_hidden_states += (hidden_states,) output = BaseModelOutputWithPast( last_hidden_state=hidden_states, - past_key_values=past_key_values if use_cache else None, hidden_states=all_hidden_states, attentions=all_self_attns, ) @@ -896,6 +859,8 @@ class MoonshineDecoder(MoonshinePreTrainedModel): config: MoonshineConfig """ + main_input_name = "input_ids" + def __init__(self, config: MoonshineConfig): super().__init__(config) self.padding_idx = config.pad_token_id From 65476342b0bd7a3806336359fadb251d762b6865 Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Thu, 2 Jan 2025 20:29:11 +0100 Subject: [PATCH 37/39] make --- .../models/moonshine/convert_usefulsensors_to_hf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformers/models/moonshine/convert_usefulsensors_to_hf.py b/src/transformers/models/moonshine/convert_usefulsensors_to_hf.py index 64833f9497b0f8..0455201ee58e3a 100644 --- a/src/transformers/models/moonshine/convert_usefulsensors_to_hf.py +++ b/src/transformers/models/moonshine/convert_usefulsensors_to_hf.py @@ -22,7 +22,7 @@ import torch from huggingface_hub import hf_hub_download -from transformers.models.moonshine.modeling_moonshine import MoonshineConfig, MoonshineForConditionalGeneration +from transformers.models.moonshine.modeling_moonshine_arch import MoonshineConfig, MoonshineForConditionalGeneration # Copied from https://github.com/usefulsensors/moonshine/blob/a1d77cc573b0471ac4602b86f67b3f48d67df1a9/moonshine/model.py From 9eec03bb48731eae29a18290fe28d0291c1e02b1 Mon Sep 17 00:00:00 2001 From: eustlb <94853470+eustlb@users.noreply.github.com> Date: Thu, 2 Jan 2025 20:30:00 +0100 Subject: [PATCH 38/39] Update docs/source/en/model_doc/moonshine.md Co-authored-by: Joshua Lochner --- docs/source/en/model_doc/moonshine.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/source/en/model_doc/moonshine.md b/docs/source/en/model_doc/moonshine.md index a5025e6107510f..166aab9985cce0 100644 --- a/docs/source/en/model_doc/moonshine.md +++ b/docs/source/en/model_doc/moonshine.md @@ -16,8 +16,6 @@ rendered properly in your Markdown viewer. # moonshine -# moonshine - ## Overview The moonshine model was proposed in [Moonshine: Speech Recognition for Live Transcription and Voice Commands From 53b9b9d1b0fe4ce3311dc2715737bcb640706acc Mon Sep 17 00:00:00 2001 From: Eustache Le Bihan Date: Thu, 2 Jan 2025 20:31:10 +0100 Subject: [PATCH 39/39] MoonshineModelTest --- .../moonshine/test_modeling_moonshine.py | 496 ++++++++++++++++++ 1 file changed, 496 insertions(+) create mode 100644 tests/models/moonshine/test_modeling_moonshine.py diff --git a/tests/models/moonshine/test_modeling_moonshine.py b/tests/models/moonshine/test_modeling_moonshine.py new file mode 100644 index 00000000000000..3c5c9d3f192d83 --- /dev/null +++ b/tests/models/moonshine/test_modeling_moonshine.py @@ -0,0 +1,496 @@ +# coding=utf-8 +# Copyright 2021 The HuggingFace Inc. team. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Testing suite for the PyTorch Moonshine model.""" + +import copy +import unittest + +from transformers import MoonshineConfig, is_torch_available +from transformers.testing_utils import require_torch, torch_device + +from ...test_configuration_common import ConfigTester +from ...test_modeling_common import ( + ModelTesterMixin, + floats_tensor, + random_attention_mask, +) +from ...test_pipeline_mixin import PipelineTesterMixin + + +if is_torch_available(): + import torch + + from transformers import ( + MoonshineForConditionalGeneration, + MoonshineModel, + ) + + +class MoonshineModelTester: + def __init__( + self, + parent, + batch_size=3, # need batch_size != num_hidden_layers + seq_length=60, + is_training=True, + use_labels=False, + vocab_size=147, + hidden_size=8, + conv1_kernel_size=7, + conv1_stride=3, + conv2_kernel_size=7, + conv2_stride=3, + conv3_kernel_size=3, + conv3_stride=2, + num_hidden_layers=2, + num_attention_heads=2, + num_key_value_heads=2, + min_rotary_ndims=4, + encoder_hidden_act="gelu", + decoder_hidden_act="silu", + attention_dropout=0.1, + intermediate_size=None, + ff_mult=2, + decoder_start_token_id=85, + bos_token_id=98, + eos_token_id=98, + pad_token_id=0, + ): + self.parent = parent + self.batch_size = batch_size + self.seq_length = seq_length + self.is_training = is_training + self.hidden_size = hidden_size + self.conv1_kernel_size = conv1_kernel_size + self.conv1_stride = conv1_stride + self.conv2_kernel_size = conv2_kernel_size + self.conv2_stride = conv2_stride + self.conv3_kernel_size = conv3_kernel_size + self.conv3_stride = conv3_stride + self.use_labels = use_labels + self.vocab_size = vocab_size + self.num_hidden_layers = num_hidden_layers + self.num_attention_heads = num_attention_heads + self.num_key_value_heads = num_key_value_heads + self.min_rotary_ndims = min_rotary_ndims + self.encoder_hidden_act = encoder_hidden_act + self.decoder_hidden_act = decoder_hidden_act + self.attention_dropout = attention_dropout + self.decoder_start_token_id = decoder_start_token_id + self.intermediate_size = intermediate_size + self.ff_mult = ff_mult + self.bos_token_id = bos_token_id + self.eos_token_id = eos_token_id + self.pad_token_id = pad_token_id + + def prepare_config_and_inputs(self): + input_values = floats_tensor([self.batch_size, self.seq_length], scale=1.0) + attention_mask = random_attention_mask([self.batch_size, self.seq_length]) + + decoder_input_ids = torch.tensor(self.batch_size * [[self.decoder_start_token_id]], device=torch_device) + decoder_attention_mask = decoder_input_ids.ne(self.pad_token_id) + + config = self.get_config() + + return config, input_values, attention_mask, decoder_input_ids, decoder_attention_mask + + def get_config(self): + return MoonshineConfig( + vocab_size=self.vocab_size, + hidden_size=self.hidden_size, + intermediate_size=self.intermediate_size, + num_hidden_layers=self.num_hidden_layers, + conv1_kernel_size=self.conv1_kernel_size, + conv1_stride=self.conv1_stride, + conv2_kernel_size=self.conv2_kernel_size, + conv2_stride=self.conv2_stride, + conv3_kernel_size=self.conv3_kernel_size, + conv3_stride=self.conv3_stride, + num_attention_heads=self.num_attention_heads, + num_key_value_heads=self.num_key_value_heads, + min_rotary_ndims=self.min_rotary_ndims, + encoder_hidden_act=self.encoder_hidden_act, + decoder_hidden_act=self.decoder_hidden_act, + decoder_start_token_id=self.decoder_start_token_id, + attention_dropout=self.attention_dropout, + ff_mult=self.ff_mult, + bos_token_id=self.bos_token_id, + eos_token_id=self.eos_token_id, + ) + + def create_and_check_model(self, config, input_values, attention_mask): + model = MoonshineModel(config=config) + model.to(torch_device) + model.eval() + result = model(input_values, attention_mask=attention_mask) + self.parent.assertEqual( + result.last_hidden_state.shape, (self.batch_size, self.output_seq_length, self.hidden_size) + ) + + def create_and_check_batch_inference(self, config, input_values, *args): + # test does not pass for models making use of `group_norm` + # check: https://github.com/pytorch/fairseq/issues/3227 + model = MoonshineModel(config=config) + model.to(torch_device) + model.eval() + + input_values = input_values[:3] + attention_mask = torch.ones(input_values.shape, device=torch_device, dtype=torch.bool) + + input_lengths = [input_values.shape[-1] // i for i in [4, 2, 1]] + + # pad input + for i in range(len(input_lengths)): + input_values[i, input_lengths[i] :] = 0.0 + attention_mask[i, input_lengths[i] :] = 0.0 + + batch_outputs = model(input_values, attention_mask=attention_mask).last_hidden_state + + for i in range(input_values.shape[0]): + input_slice = input_values[i : i + 1, : input_lengths[i]] + output = model(input_slice).last_hidden_state + + batch_output = batch_outputs[i : i + 1, : output.shape[1]] + self.parent.assertTrue(torch.allclose(output, batch_output, atol=1e-3)) + + def check_output_attentions(self, config, input_values, attention_mask): + model = MoonshineModel(config=config) + model.config.layerdrop = 1.0 + model.to(torch_device) + model.train() + + outputs = model(input_values, attention_mask=attention_mask, output_attentions=True) + self.parent.assertTrue(len(outputs.attentions) > 0) + + def prepare_config_and_inputs_for_common(self): + config, input_values, attention_mask, decoder_input_ids, decoder_attention_mask = self.prepare_config_and_inputs() + inputs_dict = {"input_values": input_values, "attention_mask": attention_mask, "decoder_input_ids": decoder_input_ids, "decoder_attention_mask": decoder_attention_mask} + return config, inputs_dict + + +@require_torch +class MoonshineModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase): + all_model_classes = ( + (MoonshineModel, MoonshineForConditionalGeneration) + if is_torch_available() + else () + ) + pipeline_model_mapping = ( + { + "automatic-speech-recognition": MoonshineForConditionalGeneration, + "feature-extraction": MoonshineModel, + } + if is_torch_available() + else {} + ) + test_pruning = False + test_headmasking = False + + def setUp(self): + self.model_tester = MoonshineModelTester(self) + self.config_tester = ConfigTester(self, config_class=MoonshineConfig) + + def test_config(self): + self.config_tester.run_common_tests() + + def test_attention_outputs(self): + config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common() + config.return_dict = True + + seq_len = getattr(self.model_tester, "seq_length", None) + decoder_seq_length = getattr(self.model_tester, "decoder_seq_length", 1) + encoder_seq_length = getattr(self.model_tester, "encoder_seq_length", seq_len) + decoder_key_length = getattr(self.model_tester, "decoder_key_length", 1) + encoder_key_length = getattr(self.model_tester, "key_length", encoder_seq_length) + + for model_class in self.all_model_classes: + inputs_dict["output_attentions"] = True + inputs_dict["output_hidden_states"] = False + config.return_dict = True + model = model_class(config) + model.to(torch_device) + model.eval() + + subsampled_encoder_seq_length = model._get_feat_extract_output_lengths(encoder_seq_length) + subsampled_encoder_key_length = model._get_feat_extract_output_lengths(encoder_key_length) + + with torch.no_grad(): + outputs = model(**self._prepare_for_class(inputs_dict, model_class)) + attentions = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions + self.assertEqual(len(attentions), self.model_tester.num_hidden_layers) + + # check that output_attentions also work using config + del inputs_dict["output_attentions"] + config.output_attentions = True + model = model_class(config) + model.to(torch_device) + model.eval() + with torch.no_grad(): + outputs = model(**self._prepare_for_class(inputs_dict, model_class)) + attentions = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions + self.assertEqual(len(attentions), self.model_tester.num_hidden_layers) + + self.assertListEqual( + list(attentions[0].shape[-3:]), + [self.model_tester.num_attention_heads, subsampled_encoder_seq_length, subsampled_encoder_key_length], + ) + out_len = len(outputs) + + correct_outlen = 5 + + # loss is at first position + if "labels" in inputs_dict: + correct_outlen += 1 # loss is added to beginning + if "past_key_values" in outputs: + correct_outlen += 1 # past_key_values have been returned + + self.assertEqual(out_len, correct_outlen) + + # decoder attentions + decoder_attentions = outputs.decoder_attentions + self.assertIsInstance(decoder_attentions, (list, tuple)) + self.assertEqual(len(decoder_attentions), self.model_tester.num_hidden_layers) + self.assertListEqual( + list(decoder_attentions[0].shape[-3:]), + [self.model_tester.num_attention_heads, decoder_seq_length, decoder_key_length], + ) + + # cross attentions + cross_attentions = outputs.cross_attentions + self.assertIsInstance(cross_attentions, (list, tuple)) + self.assertEqual(len(cross_attentions), self.model_tester.num_hidden_layers) + self.assertListEqual( + list(cross_attentions[0].shape[-3:]), + [ + self.model_tester.num_attention_heads, + decoder_seq_length, + subsampled_encoder_key_length, + ], + ) + + # Check attention is always last and order is fine + inputs_dict["output_attentions"] = True + inputs_dict["output_hidden_states"] = True + model = model_class(config) + model.to(torch_device) + model.eval() + with torch.no_grad(): + outputs = model(**self._prepare_for_class(inputs_dict, model_class)) + + added_hidden_states = 2 + self.assertEqual(out_len + added_hidden_states, len(outputs)) + + self_attentions = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions + + self.assertEqual(len(self_attentions), self.model_tester.num_hidden_layers) + self.assertListEqual( + list(self_attentions[0].shape[-3:]), + [self.model_tester.num_attention_heads, subsampled_encoder_seq_length, subsampled_encoder_key_length], + ) + + # Copied from tests.models.whisper.test_modeling_whisper.WhisperModelTest.test_hidden_states_output + def test_hidden_states_output(self): + def check_hidden_states_output(inputs_dict, config, model_class): + model = model_class(config) + model.to(torch_device) + model.eval() + + with torch.no_grad(): + outputs = model(**self._prepare_for_class(inputs_dict, model_class)) + + hidden_states = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states + + expected_num_layers = getattr( + self.model_tester, "expected_num_hidden_layers", self.model_tester.num_hidden_layers + 1 + ) + self.assertEqual(len(hidden_states), expected_num_layers) + + if hasattr(self.model_tester, "encoder_seq_length"): + seq_length = self.model_tester.encoder_seq_length + else: + seq_length = self.model_tester.seq_length + + subsampled_seq_length = model._get_feat_extract_output_lengths(seq_length) + + self.assertListEqual( + list(hidden_states[0].shape[-2:]), + [subsampled_seq_length, self.model_tester.hidden_size], + ) + + if config.is_encoder_decoder: + hidden_states = outputs.decoder_hidden_states + + self.assertIsInstance(hidden_states, (list, tuple)) + self.assertEqual(len(hidden_states), expected_num_layers) + + decoder_seq_length = getattr(self.model_tester, "decoder_seq_length", 1) + + self.assertListEqual( + list(hidden_states[0].shape[-2:]), + [decoder_seq_length, self.model_tester.hidden_size], + ) + + config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common() + + for model_class in self.all_model_classes: + inputs_dict["output_hidden_states"] = True + check_hidden_states_output(inputs_dict, config, model_class) + + # check that output_hidden_states also work using config + del inputs_dict["output_hidden_states"] + config.output_hidden_states = True + + check_hidden_states_output(inputs_dict, config, model_class) + + # Copied from tests.models.whisper.test_modeling_whisper.WhisperModelTest.test_inputs_embeds + def test_inputs_embeds(self): + config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common() + + for model_class in self.all_model_classes: + model = model_class(config) + model.to(torch_device) + model.eval() + + inputs = copy.deepcopy(self._prepare_for_class(inputs_dict, model_class)) + + decoder_input_ids = inputs.pop("decoder_input_ids", None) + inputs.pop("decoder_attention_mask", None) + + wte = model.get_input_embeddings() + inputs["decoder_inputs_embeds"] = wte(decoder_input_ids) + + with torch.no_grad(): + model(**inputs)[0] + + # Copied from tests.models.whisper.test_modeling_whisper.WhisperModelTest.test_resize_tokens_embeddings + def test_resize_tokens_embeddings(self): + ( + original_config, + inputs_dict, + ) = self.model_tester.prepare_config_and_inputs_for_common() + if not self.test_resize_embeddings: + self.skipTest(reason="test_resize_embeddings is False") + + for model_class in self.all_model_classes: + config = copy.deepcopy(original_config) + model = model_class(config) + model.to(torch_device) + + if self.model_tester.is_training is False: + model.eval() + + model_vocab_size = config.vocab_size + # Retrieve the embeddings and clone theme + model_embed = model.resize_token_embeddings(model_vocab_size) + cloned_embeddings = model_embed.weight.clone() + + # Check that resizing the token embeddings with a larger vocab size increases the model's vocab size + model_embed = model.resize_token_embeddings(model_vocab_size + 10) + self.assertEqual(model.config.vocab_size, model_vocab_size + 10) + # Check that it actually resizes the embeddings matrix + self.assertEqual(model_embed.weight.shape[0], cloned_embeddings.shape[0] + 10) + # Check that the model can still do a forward pass successfully (every parameter should be resized) + model(**self._prepare_for_class(inputs_dict, model_class)) + + # Check that resizing the token embeddings with a smaller vocab size decreases the model's vocab size + model_embed = model.resize_token_embeddings(model_vocab_size - 15) + self.assertEqual(model.config.vocab_size, model_vocab_size - 15) + # Check that it actually resizes the embeddings matrix + self.assertEqual(model_embed.weight.shape[0], cloned_embeddings.shape[0] - 15) + + # make sure that decoder_input_ids are resized + if "decoder_input_ids" in inputs_dict: + inputs_dict["decoder_input_ids"].clamp_(max=model_vocab_size - 15 - 1) + model(**self._prepare_for_class(inputs_dict, model_class)) + + # Check that adding and removing tokens has not modified the first part of the embedding matrix. + models_equal = True + for p1, p2 in zip(cloned_embeddings, model_embed.weight): + if p1.data.ne(p2.data).sum() > 0: + models_equal = False + + self.assertTrue(models_equal) + + # Copied from tests.models.whisper.test_modeling_whisper.WhisperModelTest.test_resize_embeddings_untied + def test_resize_embeddings_untied(self): + ( + original_config, + inputs_dict, + ) = self.model_tester.prepare_config_and_inputs_for_common() + if not self.test_resize_embeddings: + self.skipTest(reason="test_resize_embeddings is False") + + original_config.tie_word_embeddings = False + + # if model cannot untied embeddings -> leave test + if original_config.tie_word_embeddings: + self.skipTest(reason="Model cannot untie embeddings") + + for model_class in self.all_model_classes: + config = copy.deepcopy(original_config) + model = model_class(config).to(torch_device) + + # if no output embeddings -> leave test + if model.get_output_embeddings() is None: + continue + + # Check that resizing the token embeddings with a larger vocab size increases the model's vocab size + model_vocab_size = config.vocab_size + model.resize_token_embeddings(model_vocab_size + 10) + self.assertEqual(model.config.vocab_size, model_vocab_size + 10) + output_embeds = model.get_output_embeddings() + self.assertEqual(output_embeds.weight.shape[0], model_vocab_size + 10) + # Check bias if present + if output_embeds.bias is not None: + self.assertEqual(output_embeds.bias.shape[0], model_vocab_size + 10) + # Check that the model can still do a forward pass successfully (every parameter should be resized) + model(**self._prepare_for_class(inputs_dict, model_class)) + + # Check that resizing the token embeddings with a smaller vocab size decreases the model's vocab size + model.resize_token_embeddings(model_vocab_size - 15) + self.assertEqual(model.config.vocab_size, model_vocab_size - 15) + # Check that it actually resizes the embeddings matrix + output_embeds = model.get_output_embeddings() + self.assertEqual(output_embeds.weight.shape[0], model_vocab_size - 15) + # Check bias if present + if output_embeds.bias is not None: + self.assertEqual(output_embeds.bias.shape[0], model_vocab_size - 15) + # Check that the model can still do a forward pass successfully (every parameter should be resized) + if "decoder_input_ids" in inputs_dict: + inputs_dict["decoder_input_ids"].clamp_(max=model_vocab_size - 15 - 1) + # Check that the model can still do a forward pass successfully (every parameter should be resized) + model(**self._prepare_for_class(inputs_dict, model_class)) + + # training is not supported yet + @unittest.skip(reason="Training is not supported yet") + def test_training(self): + pass + + @unittest.skip(reason="Training is not supported yet") + def test_training_gradient_checkpointing(self): + pass + + @unittest.skip( + reason="This architecure seem to not compute gradients properly when using GC, check: https://github.com/huggingface/transformers/pull/27124" + ) + def test_training_gradient_checkpointing_use_reentrant(self): + pass + + @unittest.skip( + reason="This architecure seem to not compute gradients properly when using GC, check: https://github.com/huggingface/transformers/pull/27124" + ) + def test_training_gradient_checkpointing_use_reentrant_false(self): + pass +