|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2022 Google AI and 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 at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# 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 and |
| 14 | +# limitations under the License. |
| 15 | +# ============================================================================ |
| 16 | +""" Audio Spectogram Transformer (AST) model configuration""" |
| 17 | + |
| 18 | + |
| 19 | +from mindnlp.utils import logging |
| 20 | +from ...configuration_utils import PretrainedConfig |
| 21 | + |
| 22 | + |
| 23 | +logger = logging.get_logger(__name__) |
| 24 | + |
| 25 | +AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP = { |
| 26 | + "MIT/ast-finetuned-audioset-10-10-0.4593": ( |
| 27 | + "https://huggingface.co/MIT/ast-finetuned-audioset-10-10-0.4593/resolve/main/config.json" |
| 28 | + ), |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +class ASTConfig(PretrainedConfig): |
| 33 | + r""" |
| 34 | + This is the configuration class to store the configuration of a [`ASTModel`]. It is used to instantiate an AST |
| 35 | + model according to the specified arguments, defining the model architecture. Instantiating a configuration with the |
| 36 | + defaults will yield a similar configuration to that of the AST |
| 37 | + [MIT/ast-finetuned-audioset-10-10-0.4593](https://huggingface.co/MIT/ast-finetuned-audioset-10-10-0.4593) |
| 38 | + architecture. |
| 39 | +
|
| 40 | + Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the |
| 41 | + documentation from [`PretrainedConfig`] for more information. |
| 42 | +
|
| 43 | + Args: |
| 44 | + hidden_size (`int`, *optional*, defaults to 768): |
| 45 | + Dimensionality of the encoder layers and the pooler layer. |
| 46 | + num_hidden_layers (`int`, *optional*, defaults to 12): |
| 47 | + Number of hidden layers in the Transformer encoder. |
| 48 | + num_attention_heads (`int`, *optional*, defaults to 12): |
| 49 | + Number of attention heads for each attention layer in the Transformer encoder. |
| 50 | + intermediate_size (`int`, *optional*, defaults to 3072): |
| 51 | + Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder. |
| 52 | + hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`): |
| 53 | + The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`, |
| 54 | + `"relu"`, `"selu"` and `"gelu_new"` are supported. |
| 55 | + hidden_dropout_prob (`float`, *optional*, defaults to 0.0): |
| 56 | + The dropout probability for all fully connected layers in the embeddings, encoder, and pooler. |
| 57 | + attention_probs_dropout_prob (`float`, *optional*, defaults to 0.0): |
| 58 | + The dropout ratio for the attention probabilities. |
| 59 | + initializer_range (`float`, *optional*, defaults to 0.02): |
| 60 | + The standard deviation of the truncated_normal_initializer for initializing all weight matrices. |
| 61 | + layer_norm_eps (`float`, *optional*, defaults to 1e-12): |
| 62 | + The epsilon used by the layer normalization layers. |
| 63 | + patch_size (`int`, *optional*, defaults to 16): |
| 64 | + The size (resolution) of each patch. |
| 65 | + qkv_bias (`bool`, *optional*, defaults to `True`): |
| 66 | + Whether to add a bias to the queries, keys and values. |
| 67 | + frequency_stride (`int`, *optional*, defaults to 10): |
| 68 | + Frequency stride to use when patchifying the spectrograms. |
| 69 | + time_stride (`int`, *optional*, defaults to 10): |
| 70 | + Temporal stride to use when patchifying the spectrograms. |
| 71 | + max_length (`int`, *optional*, defaults to 1024): |
| 72 | + Temporal dimension of the spectrograms. |
| 73 | + num_mel_bins (`int`, *optional*, defaults to 128): |
| 74 | + Frequency dimension of the spectrograms (number of Mel-frequency bins). |
| 75 | +
|
| 76 | + Example: |
| 77 | +
|
| 78 | + ```python |
| 79 | + >>> from transformers import ASTConfig, ASTModel |
| 80 | +
|
| 81 | + >>> # Initializing a AST MIT/ast-finetuned-audioset-10-10-0.4593 style configuration |
| 82 | + >>> configuration = ASTConfig() |
| 83 | +
|
| 84 | + >>> # Initializing a model (with random weights) from the MIT/ast-finetuned-audioset-10-10-0.4593 style configuration |
| 85 | + >>> model = ASTModel(configuration) |
| 86 | +
|
| 87 | + >>> # Accessing the model configuration |
| 88 | + >>> configuration = model.config |
| 89 | + ```""" |
| 90 | + |
| 91 | + model_type = "audio-spectrogram-transformer" |
| 92 | + |
| 93 | + def __init__( |
| 94 | + self, |
| 95 | + hidden_size=768, |
| 96 | + num_hidden_layers=12, |
| 97 | + num_attention_heads=12, |
| 98 | + intermediate_size=3072, |
| 99 | + hidden_act="gelu", |
| 100 | + hidden_dropout_prob=0.0, |
| 101 | + attention_probs_dropout_prob=0.0, |
| 102 | + initializer_range=0.02, |
| 103 | + layer_norm_eps=1e-12, |
| 104 | + patch_size=16, |
| 105 | + qkv_bias=True, |
| 106 | + frequency_stride=10, |
| 107 | + time_stride=10, |
| 108 | + max_length=1024, |
| 109 | + num_mel_bins=128, |
| 110 | + **kwargs, |
| 111 | + ): |
| 112 | + super().__init__(**kwargs) |
| 113 | + |
| 114 | + self.hidden_size = hidden_size |
| 115 | + self.num_hidden_layers = num_hidden_layers |
| 116 | + self.num_attention_heads = num_attention_heads |
| 117 | + self.intermediate_size = intermediate_size |
| 118 | + self.hidden_act = hidden_act |
| 119 | + self.hidden_dropout_prob = hidden_dropout_prob |
| 120 | + self.attention_probs_dropout_prob = attention_probs_dropout_prob |
| 121 | + self.initializer_range = initializer_range |
| 122 | + self.layer_norm_eps = layer_norm_eps |
| 123 | + self.patch_size = patch_size |
| 124 | + self.qkv_bias = qkv_bias |
| 125 | + self.frequency_stride = frequency_stride |
| 126 | + self.time_stride = time_stride |
| 127 | + self.max_length = max_length |
| 128 | + self.num_mel_bins = num_mel_bins |
| 129 | + |
| 130 | +__all__ = [ |
| 131 | + "AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", |
| 132 | + "ASTConfig", |
| 133 | +] |
0 commit comments