configuration_parakeet.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # Copyright 2025 The HuggingFace Inc. team. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Parakeet model configuration."""
  15. from huggingface_hub.dataclasses import strict
  16. from ...configuration_utils import PreTrainedConfig
  17. from ...utils import auto_docstring
  18. @auto_docstring(checkpoint="nvidia/parakeet-ctc-1.1b")
  19. @strict
  20. class ParakeetEncoderConfig(PreTrainedConfig):
  21. r"""
  22. convolution_bias (`bool`, *optional*, defaults to `True`):
  23. Whether to use bias in convolutions of the conformer's convolution module.
  24. conv_kernel_size (`int`, *optional*, defaults to 9):
  25. The kernel size of the convolution layers in the Conformer block.
  26. subsampling_factor (`int`, *optional*, defaults to 8):
  27. The factor by which the input sequence is subsampled.
  28. subsampling_conv_channels (`int`, *optional*, defaults to 256):
  29. The number of channels in the subsampling convolution layers.
  30. num_mel_bins (`int`, *optional*, defaults to 80):
  31. Number of mel features.
  32. subsampling_conv_kernel_size (`int`, *optional*, defaults to 3):
  33. The kernel size of the subsampling convolution layers.
  34. subsampling_conv_stride (`int`, *optional*, defaults to 2):
  35. The stride of the subsampling convolution layers.
  36. dropout_positions (`float`, *optional*, defaults to 0.0):
  37. The dropout ratio for the positions in the input sequence.
  38. scale_input (`bool`, *optional*, defaults to `True`):
  39. Whether to scale the input embeddings.
  40. Example:
  41. ```python
  42. >>> from transformers import ParakeetEncoderModel, ParakeetEncoderConfig
  43. >>> # Initializing a `ParakeetEncoder` configuration
  44. >>> configuration = ParakeetEncoderConfig()
  45. >>> # Initializing a model from the configuration
  46. >>> model = ParakeetEncoderModel(configuration)
  47. >>> # Accessing the model configuration
  48. >>> configuration = model.config
  49. ```
  50. This configuration class is based on the ParakeetEncoder architecture from NVIDIA NeMo. You can find more details
  51. and pre-trained models at [nvidia/parakeet-ctc-1.1b](https://huggingface.co/nvidia/parakeet-ctc-1.1b).
  52. """
  53. model_type = "parakeet_encoder"
  54. keys_to_ignore_at_inference = ["past_key_values"]
  55. hidden_size: int = 1024
  56. num_hidden_layers: int = 24
  57. num_attention_heads: int = 8
  58. intermediate_size: int = 4096
  59. hidden_act: str = "silu"
  60. attention_bias: bool = True
  61. convolution_bias: bool = True
  62. conv_kernel_size: int = 9
  63. subsampling_factor: int = 8
  64. subsampling_conv_channels: int = 256
  65. num_mel_bins: int = 80
  66. subsampling_conv_kernel_size: int = 3
  67. subsampling_conv_stride: int = 2
  68. dropout: float | int = 0.1
  69. dropout_positions: float | int = 0.0
  70. layerdrop: float | int = 0.1
  71. activation_dropout: float | int = 0.1
  72. attention_dropout: float | int = 0.1
  73. max_position_embeddings: int = 5000
  74. scale_input: bool = True
  75. initializer_range: float = 0.02
  76. def __post_init__(self, **kwargs):
  77. self.num_key_value_heads = self.num_attention_heads
  78. super().__post_init__(**kwargs)
  79. @auto_docstring(checkpoint="nvidia/parakeet-ctc-1.1b")
  80. @strict
  81. class ParakeetCTCConfig(PreTrainedConfig):
  82. r"""
  83. ctc_loss_reduction (`str`, *optional*, defaults to `"mean"`):
  84. Specifies the reduction to apply to the output of `torch.nn.CTCLoss`. Only relevant when training an
  85. instance of [`ParakeetForCTC`].
  86. ctc_zero_infinity (`bool`, *optional*, defaults to `True`):
  87. Whether to zero infinite losses and the associated gradients of `torch.nn.CTCLoss`. Infinite losses mainly
  88. occur when the inputs are too short to be aligned to the targets. Only relevant when training an instance
  89. of [`ParakeetForCTC`].
  90. encoder_config (`Union[dict, ParakeetEncoderConfig]`, *optional*):
  91. The config object or dictionary of the encoder.
  92. Example:
  93. ```python
  94. >>> from transformers import ParakeetForCTC, ParakeetCTCConfig
  95. >>> # Initializing a Parakeet configuration
  96. >>> configuration = ParakeetCTCConfig()
  97. >>> # Initializing a model from the configuration
  98. >>> model = ParakeetForCTC(configuration)
  99. >>> # Accessing the model configuration
  100. >>> configuration = model.config
  101. ```
  102. """
  103. model_type = "parakeet_ctc"
  104. sub_configs = {"encoder_config": ParakeetEncoderConfig}
  105. vocab_size: int = 1025
  106. ctc_loss_reduction: str = "mean"
  107. ctc_zero_infinity: bool = True
  108. encoder_config: dict | PreTrainedConfig | None = None
  109. pad_token_id: int | None = 1024
  110. def __post_init__(self, **kwargs):
  111. if isinstance(self.encoder_config, dict):
  112. self.encoder_config = ParakeetEncoderConfig(**self.encoder_config)
  113. elif self.encoder_config is None:
  114. self.encoder_config = ParakeetEncoderConfig()
  115. self.initializer_range = self.encoder_config.initializer_range
  116. super().__post_init__(**kwargs)
  117. __all__ = ["ParakeetCTCConfig", "ParakeetEncoderConfig"]