configuration_whisper.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # Copyright 2022 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. """Whisper model configuration"""
  15. from huggingface_hub.dataclasses import strict
  16. from ...configuration_utils import PreTrainedConfig
  17. from ...utils import auto_docstring
  18. # fmt: off
  19. NON_SPEECH_TOKENS = [
  20. 1, 2, 7, 8, 9, 10, 14, 25,
  21. 26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
  22. 63, 90, 91, 92, 93, 357, 366, 438, 532, 685,
  23. 705, 796, 930, 1058, 1220, 1267, 1279, 1303, 1343, 1377,
  24. 1391, 1635, 1782, 1875, 2162, 2361, 2488, 3467, 4008, 4211,
  25. 4600, 4808, 5299, 5855, 6329, 7203, 9609, 9959, 10563, 10786,
  26. 11420, 11709, 11907, 13163, 13697, 13700, 14808, 15306, 16410, 16791,
  27. 17992, 19203, 19510, 20724, 22305, 22935, 27007, 30109, 30420, 33409,
  28. 34949, 40283, 40493, 40549, 47282, 49146, 50257, 50359, 50360, 50361
  29. ]
  30. NON_SPEECH_TOKENS_MULTI = [
  31. 1, 2, 7, 8, 9, 10, 14, 25,
  32. 26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
  33. 63, 90, 91, 92, 93, 359, 503, 522, 542, 873,
  34. 893, 902, 918, 922, 931, 1350, 1853, 1982, 2460, 2627,
  35. 3246, 3253, 3268, 3536, 3846, 3961, 4183, 4667, 6585, 6647,
  36. 7273, 9061, 9383, 10428, 10929, 11938, 12033, 12331, 12562, 13793,
  37. 14157, 14635, 15265, 15618, 16553, 16604, 18362, 18956, 20075, 21675,
  38. 22520, 26130, 26161, 26435, 28279, 29464, 31650, 32302, 32470, 36865,
  39. 42863, 47425, 49870, 50254, 50258, 50360, 50361, 50362
  40. ]
  41. # fmt: on
  42. @auto_docstring(checkpoint="openai/whisper-tiny")
  43. @strict
  44. class WhisperConfig(PreTrainedConfig):
  45. r"""
  46. max_source_positions (`int`, *optional*, defaults to 1500):
  47. The maximum sequence length of log-mel filter-bank features that this model might ever be used with.
  48. max_target_positions (`int`, *optional*, defaults to 448):
  49. The maximum sequence length that this model might ever be used with. Typically set this to something large
  50. just in case (e.g., 512 or 1024 or 2048).
  51. suppress_tokens (`list[int]`, *optional*):
  52. A list containing the non-speech tokens that will be used by the logit processor in the `generate`
  53. function. NON_SPEECH_TOKENS and NON_SPEECH_TOKENS_MULTI each correspond to the `english-only` and the
  54. `multilingual` model.
  55. begin_suppress_tokens (`list[int]`, *optional*, defaults to `[220,50256]`):
  56. A list containing tokens that will be suppressed at the beginning of the sampling process. Initialized as
  57. the token for `" "` (`blank_token_id`) and the `eos_token_id`
  58. use_weighted_layer_sum (`bool`, *optional*, defaults to `False`):
  59. Whether to use a weighted average of layer outputs with learned weights. Only relevant when using an
  60. instance of [`WhisperForAudioClassification`].
  61. classifier_proj_size (`int`, *optional*, defaults to 256):
  62. Dimensionality of the projection before token mean-pooling for classification. Only relevant when using an
  63. instance of [`WhisperForAudioClassification`].
  64. apply_spec_augment (`bool`, *optional*, defaults to `False`):
  65. Whether to apply *SpecAugment* data augmentation to the outputs of the feature encoder. For reference see
  66. [SpecAugment: A Simple Data Augmentation Method for Automatic Speech
  67. Recognition](https://huggingface.co/papers/1904.08779).
  68. mask_time_prob (`float`, *optional*, defaults to 0.05):
  69. Percentage (between 0 and 1) of all feature vectors along the time axis which will be masked. The masking
  70. procedure generates `mask_time_prob*len(time_axis)/mask_time_length` independent masks over the axis. If
  71. reasoning from the probability of each feature vector to be chosen as the start of the vector span to be
  72. masked, *mask_time_prob* should be `prob_vector_start*mask_time_length`. Note that overlap may decrease the
  73. actual percentage of masked vectors. This is only relevant if `apply_spec_augment == True`.
  74. mask_time_length (`int`, *optional*, defaults to 10):
  75. Length of vector span along the time axis.
  76. mask_time_min_masks (`int`, *optional*, defaults to 2),:
  77. The minimum number of masks of length `mask_feature_length` generated along the time axis, each time step,
  78. irrespectively of `mask_feature_prob`. Only relevant if ''mask_time_prob*len(time_axis)/mask_time_length <
  79. mask_time_min_masks''
  80. mask_feature_prob (`float`, *optional*, defaults to 0.0):
  81. Percentage (between 0 and 1) of all feature vectors along the feature axis which will be masked. The
  82. masking procedure generates `mask_feature_prob*len(feature_axis)/mask_time_length` independent masks over
  83. the axis. If reasoning from the probability of each feature vector to be chosen as the start of the vector
  84. span to be masked, *mask_feature_prob* should be `prob_vector_start*mask_feature_length`. Note that overlap
  85. may decrease the actual percentage of masked vectors. This is only relevant if `apply_spec_augment is
  86. True`.
  87. mask_feature_length (`int`, *optional*, defaults to 10):
  88. Length of vector span along the feature axis.
  89. mask_feature_min_masks (`int`, *optional*, defaults to 0):
  90. The minimum number of masks of length `mask_feature_length` generated along the feature axis, each time
  91. step, irrespectively of `mask_feature_prob`. Only relevant if
  92. `mask_feature_prob*len(feature_axis)/mask_feature_length < mask_feature_min_masks`.
  93. median_filter_width (`int`, *optional*, defaults to 7):
  94. Width of the median filter used to smoothen to cross-attention outputs when computing token timestamps.
  95. Should be an odd number.
  96. Example:
  97. ```python
  98. >>> from transformers import WhisperConfig, WhisperModel
  99. >>> # Initializing a Whisper tiny style configuration
  100. >>> configuration = WhisperConfig()
  101. >>> # Initializing a model (with random weights) from the tiny style configuration
  102. >>> model = WhisperModel(configuration)
  103. >>> # Accessing the model configuration
  104. >>> configuration = model.config
  105. ```"""
  106. model_type = "whisper"
  107. keys_to_ignore_at_inference = ["past_key_values"]
  108. attribute_map = {
  109. "num_key_value_heads": "encoder_attention_heads",
  110. "num_attention_heads": "encoder_attention_heads",
  111. "hidden_size": "d_model",
  112. "num_hidden_layers": "encoder_layers",
  113. }
  114. vocab_size: int = 51865
  115. num_mel_bins: int = 80
  116. encoder_layers: int = 4
  117. encoder_attention_heads: int = 6
  118. decoder_layers: int = 4
  119. decoder_attention_heads: int = 6
  120. decoder_ffn_dim: int = 1536
  121. encoder_ffn_dim: int = 1536
  122. encoder_layerdrop: float | int = 0.0
  123. decoder_layerdrop: float | int = 0.0
  124. decoder_start_token_id: int = 50257
  125. use_cache: bool = True
  126. is_encoder_decoder: bool = True
  127. activation_function: str = "gelu"
  128. d_model: int = 384
  129. dropout: float | int = 0.0
  130. attention_dropout: float | int = 0.0
  131. activation_dropout: float | int = 0.0
  132. init_std: float = 0.02
  133. scale_embedding: bool = False
  134. max_source_positions: int = 1500
  135. max_target_positions: int = 448
  136. pad_token_id: int | None = 50256
  137. bos_token_id: int | None = 50256
  138. eos_token_id: int | list[int] | None = 50256
  139. suppress_tokens: list | None = None
  140. begin_suppress_tokens: list[int] | tuple[int, ...] | None = (220, 50256)
  141. use_weighted_layer_sum: bool = False
  142. classifier_proj_size: int = 256
  143. apply_spec_augment: bool = False
  144. mask_time_prob: float | int = 0.05
  145. mask_time_length: int = 10
  146. mask_time_min_masks: int = 2
  147. mask_feature_prob: float | int = 0.0
  148. mask_feature_length: int = 10
  149. mask_feature_min_masks: int = 0
  150. median_filter_width: int = 7
  151. tie_word_embeddings: bool = True
  152. __all__ = ["WhisperConfig"]