CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
configuration_parakeet.py236 linesDownload Raw Back to parakeet
1# coding=utf-82# Copyright 2025 The HuggingFace Inc. team. All rights reserved.3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8#     http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.15"""Parakeet model configuration."""16 17from typing import Union18 19from ...configuration_utils import PretrainedConfig20from ...utils import logging21 22 23logger = logging.get_logger(__name__)24 25 26class ParakeetEncoderConfig(PretrainedConfig):27    r"""28    This is the configuration class to store the configuration of a [`ParakeetEncoder`]. It is used to instantiate a29    `ParakeetEncoder` model according to the specified arguments, defining the model architecture.30 31    Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the32    documentation from [`PretrainedConfig`] for more information.33 34    Args:35        hidden_size (`int`, *optional*, defaults to 1024):36            Dimension of the layers and the hidden states.37        num_hidden_layers (`int`, *optional*, defaults to 24):38            Number of hidden layers in the Transformer encoder.39        num_attention_heads (`int`, *optional*, defaults to 8):40            Number of attention heads for each attention layer in the Transformer encoder.41        intermediate_size (`int`, *optional*, defaults to 4096):42            Dimension of the "intermediate" (often named feed-forward) layer in the Transformer encoder.43        hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):44            The non-linear activation function (function or string) in the encoder and pooler.45        attention_bias (`bool`, *optional*, defaults to `True`):46            Whether to use bias in the attention layers.47        conv_kernel_size (`int`, *optional*, defaults to 9):48            The kernel size of the convolution layers in the Conformer block.49        subsampling_factor (`int`, *optional*, defaults to 8):50            The factor by which the input sequence is subsampled.51        subsampling_conv_channels (`int`, *optional*, defaults to 256):52            The number of channels in the subsampling convolution layers.53        num_mel_bins (`int`, *optional*, defaults to 80):54            Number of mel features.55        subsampling_conv_kernel_size (`int`, *optional*, defaults to 3):56            The kernel size of the subsampling convolution layers.57        subsampling_conv_stride (`int`, *optional*, defaults to 2):58            The stride of the subsampling convolution layers.59        dropout (`float`, *optional*, defaults to 0.1):60            The dropout ratio for all fully connected layers in the embeddings, encoder, and pooler.61        dropout_positions (`float`, *optional*, defaults to 0.0):62            The dropout ratio for the positions in the input sequence.63        layerdrop (`float`, *optional*, defaults to 0.1):64            The dropout ratio for the layers in the encoder.65        activation_dropout (`float`, *optional*, defaults to 0.1):66            The dropout ratio for activations inside the fully connected layer.67        attention_dropout (`float`, *optional*, defaults to 0.1):68            The dropout ratio for the attention layers.69        max_position_embeddings (`int`, *optional*, defaults to 5000):70            The maximum sequence length that this model might ever be used with.71        scale_input (`bool`, *optional*, defaults to `True`):72            Whether to scale the input embeddings.73        initializer_range (`float`, *optional*, defaults to 0.02):74            The standard deviation of the truncated_normal_initializer for initializing all weight matrices.75 76    Example:77        ```python78        >>> from transformers import ParakeetEncoderModel, ParakeetEncoderConfig79 80        >>> # Initializing a `ParakeetEncoder` configuration81        >>> configuration = ParakeetEncoderConfig()82 83        >>> # Initializing a model from the configuration84        >>> model = ParakeetEncoderModel(configuration)85 86        >>> # Accessing the model configuration87        >>> configuration = model.config88        ```89 90    This configuration class is based on the ParakeetEncoder architecture from NVIDIA NeMo. You can find more details91    and pre-trained models at [nvidia/parakeet-ctc-1.1b](https://huggingface.co/nvidia/parakeet-ctc-1.1b).92    """93 94    model_type = "parakeet_encoder"95    keys_to_ignore_at_inference = ["past_key_values"]96 97    def __init__(98        self,99        hidden_size=1024,100        num_hidden_layers=24,101        num_attention_heads=8,102        intermediate_size=4096,103        hidden_act="silu",104        attention_bias=True,105        conv_kernel_size=9,106        subsampling_factor=8,107        subsampling_conv_channels=256,108        num_mel_bins=80,109        subsampling_conv_kernel_size=3,110        subsampling_conv_stride=2,111        dropout=0.1,112        dropout_positions=0.0,113        layerdrop=0.1,114        activation_dropout=0.1,115        attention_dropout=0.1,116        max_position_embeddings=5000,117        scale_input=True,118        initializer_range=0.02,119        **kwargs,120    ):121        super().__init__(122            **kwargs,123        )124        self.hidden_size = hidden_size125        self.num_hidden_layers = num_hidden_layers126        self.num_attention_heads = num_attention_heads127        self.num_key_value_heads = num_attention_heads  # LlamaAttention compatibility128        self.intermediate_size = intermediate_size129        self.hidden_act = hidden_act130        self.attention_bias = attention_bias131 132        if (conv_kernel_size - 1) % 2 != 0:133            raise ValueError(f"conv_kernel_size must be odd, got {conv_kernel_size}")134        self.conv_kernel_size = conv_kernel_size135 136        self.subsampling_conv_kernel_size = subsampling_conv_kernel_size137        self.subsampling_conv_stride = subsampling_conv_stride138 139        self.subsampling_factor = subsampling_factor140        self.subsampling_conv_channels = subsampling_conv_channels141        self.num_mel_bins = num_mel_bins142 143        self.dropout = dropout144        self.dropout_positions = dropout_positions145        self.layerdrop = layerdrop146        self.activation_dropout = activation_dropout147        self.attention_dropout = attention_dropout148        self.max_position_embeddings = max_position_embeddings149        self.scale_input = scale_input150        self.initializer_range = initializer_range151 152 153class ParakeetCTCConfig(PretrainedConfig):154    r"""155    This is the configuration class to store the configuration of a [`ParakeetForCTC`]. It is used to instantiate a156    Parakeet CTC model according to the specified arguments, defining the model architecture.157 158    Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the159    documentation from [`PretrainedConfig`] for more information.160 161    Args:162            vocab_size (`int`, *optional*, defaults to 1025):163                Vocabulary size of the model.164            ctc_loss_reduction (`str`, *optional*, defaults to `"mean"`):165                Specifies the reduction to apply to the output of `torch.nn.CTCLoss`. Only relevant when training an166                instance of [`ParakeetForCTC`].167            ctc_zero_infinity (`bool`, *optional*, defaults to `True`):168                Whether to zero infinite losses and the associated gradients of `torch.nn.CTCLoss`. Infinite losses mainly169                occur when the inputs are too short to be aligned to the targets. Only relevant when training an instance170                of [`ParakeetForCTC`].171            encoder_config (`Union[dict, ParakeetEncoderConfig]`, *optional*):172                The config object or dictionary of the encoder.173            pad_token_id (`int`, *optional*, defaults to 1024):174                Padding token id. Also used as blank token id.175 176    Example:177        ```python178        >>> from transformers import ParakeetForCTC, ParakeetCTCConfig179 180        >>> # Initializing a Parakeet configuration181        >>> configuration = ParakeetCTCConfig()182 183        >>> # Initializing a model from the configuration184        >>> model = ParakeetForCTC(configuration)185 186        >>> # Accessing the model configuration187        >>> configuration = model.config188        ```189 190    This configuration class is based on the Parakeet CTC architecture from NVIDIA NeMo. You can find more details191    and pre-trained models at [nvidia/parakeet-ctc-1.1b](https://huggingface.co/nvidia/parakeet-ctc-1.1b).192    """193 194    model_type = "parakeet_ctc"195    sub_configs = {"encoder_config": ParakeetEncoderConfig}196 197    def __init__(198        self,199        vocab_size=1025,200        ctc_loss_reduction="mean",201        ctc_zero_infinity=True,202        encoder_config: Union[dict, ParakeetEncoderConfig] = None,203        pad_token_id=1024,204        **kwargs,205    ):206        self.vocab_size = vocab_size207        self.ctc_loss_reduction = ctc_loss_reduction208        self.ctc_zero_infinity = ctc_zero_infinity209 210        if isinstance(encoder_config, dict):211            self.encoder_config = ParakeetEncoderConfig(**encoder_config)212        elif encoder_config is None:213            self.encoder_config = ParakeetEncoderConfig()214 215        self.encoder_config = self.encoder_config216        self.initializer_range = self.encoder_config.initializer_range217 218        super().__init__(219            pad_token_id=pad_token_id,220            **kwargs,221        )222 223    @classmethod224    def from_encoder_config(cls, encoder_config: ParakeetEncoderConfig, **kwargs):225        r"""226        Instantiate a [`ParakeetCTCConfig`] (or a derived class) from parakeet encoder model configuration.227 228        Returns:229            [`ParakeetCTCConfig`]: An instance of a configuration object230        """231 232        return cls(encoder_config=encoder_config.to_dict(), **kwargs)233 234 235__all__ = ["ParakeetCTCConfig", "ParakeetEncoderConfig"]236 
Aluode/PerceptionLabPortable · CoolFace