

import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_UpperCAmelCase		:   Any										=       logging.get_logger(__name__)

_UpperCAmelCase		:   Dict										=       {
    """facebook/wav2vec2-base-960h""": """https://huggingface.co/facebook/wav2vec2-base-960h/resolve/main/config.json""",
    # See all Wav2Vec2 models at https://huggingface.co/models?filter=wav2vec2
}







class  lowercase				(       SCREAMING_SNAKE_CASE_			):
				__SCREAMING_SNAKE_CASE					: List[str]       			=			'''wav2vec2'''




				def __init__(					self      ,   snake_case=32      ,   snake_case=768      ,   snake_case=12      ,   snake_case=12      ,   snake_case=3072      ,   snake_case="gelu"      ,   snake_case=0.1      ,   snake_case=0.1      ,   snake_case=0.1      ,   snake_case=0.0      ,   snake_case=0.0      ,   snake_case=0.1      ,   snake_case=0.1      ,   snake_case=0.02      ,   snake_case=1e-5      ,   snake_case="group"      ,   snake_case="gelu"      ,   snake_case=(512, 512, 512, 512, 512, 512, 512)      ,   snake_case=(5, 2, 2, 2, 2, 2, 2)      ,   snake_case=(10, 3, 3, 3, 3, 2, 2)      ,   snake_case=False      ,   snake_case=128      ,   snake_case=16      ,   snake_case=False      ,   snake_case=True      ,   snake_case=0.05      ,   snake_case=10      ,   snake_case=2      ,   snake_case=0.0      ,   snake_case=10      ,   snake_case=0      ,   snake_case=320      ,   snake_case=2      ,   snake_case=0.1      ,   snake_case=100      ,   snake_case=256      ,   snake_case=256      ,   snake_case=0.1      ,   snake_case="sum"      ,   snake_case=False      ,   snake_case=False      ,   snake_case=256      ,   snake_case=(512, 512, 512, 512, 1500)      ,   snake_case=(5, 3, 3, 1, 1)      ,   snake_case=(1, 2, 3, 1, 1)      ,   snake_case=512      ,   snake_case=0      ,   snake_case=1      ,   snake_case=2      ,   snake_case=False      ,   snake_case=3      ,   snake_case=2      ,   snake_case=3      ,   snake_case=None      ,   snake_case=None      ,   **snake_case      ,   ):
											super().__init__(**__UpperCAmelCase      ,   pad_token_id=__UpperCAmelCase      ,   bos_token_id=__UpperCAmelCase      ,   eos_token_id=__UpperCAmelCase       )
											snake_case_							=    hidden_size
											snake_case_							=    feat_extract_norm
											snake_case_							=    feat_extract_activation
											snake_case_							=    list(__UpperCAmelCase       )
											snake_case_							=    list(__UpperCAmelCase       )
											snake_case_							=    list(__UpperCAmelCase       )
											snake_case_							=    conv_bias
											snake_case_							=    num_conv_pos_embeddings
											snake_case_							=    num_conv_pos_embedding_groups
											snake_case_							=    len(self.conv_dim       )
											snake_case_							=    num_hidden_layers
											snake_case_							=    intermediate_size
											snake_case_							=    hidden_act
											snake_case_							=    num_attention_heads
											snake_case_							=    hidden_dropout
											snake_case_							=    attention_dropout
											snake_case_							=    activation_dropout
											snake_case_							=    feat_proj_dropout
											snake_case_							=    final_dropout
											snake_case_							=    layerdrop
											snake_case_							=    layer_norm_eps
											snake_case_							=    initializer_range
											snake_case_							=    vocab_size
											snake_case_							=    do_stable_layer_norm
											snake_case_							=    use_weighted_layer_sum

											if (
											    (len(self.conv_stride       ) != self.num_feat_extract_layers)
											    or (len(self.conv_kernel       ) != self.num_feat_extract_layers)
											    or (len(self.conv_dim       ) != self.num_feat_extract_layers)
											):
																		raise ValueError(
																		    'Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =='
																		    ' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ='
																		    F''' {len(self.conv_dim       )}`, `len(config.conv_stride) = {len(self.conv_stride       )}`,'''
																		    F''' `len(config.conv_kernel) = {len(self.conv_kernel       )}`.'''       )

											# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
											snake_case_							=    apply_spec_augment
											snake_case_							=    mask_time_prob
											snake_case_							=    mask_time_length
											snake_case_							=    mask_time_min_masks
											snake_case_							=    mask_feature_prob
											snake_case_							=    mask_feature_length
											snake_case_							=    mask_feature_min_masks

											# parameters for pretraining with codevector quantized representations
											snake_case_							=    num_codevectors_per_group
											snake_case_							=    num_codevector_groups
											snake_case_							=    contrastive_logits_temperature
											snake_case_							=    feat_quantizer_dropout
											snake_case_							=    num_negatives
											snake_case_							=    codevector_dim
											snake_case_							=    proj_codevector_dim
											snake_case_							=    diversity_loss_weight

											# ctc loss
											snake_case_							=    ctc_loss_reduction
											snake_case_							=    ctc_zero_infinity

											# adapter
											snake_case_							=    add_adapter
											snake_case_							=    adapter_kernel_size
											snake_case_							=    adapter_stride
											snake_case_							=    num_adapter_layers
											snake_case_							=    output_hidden_size or hidden_size
											snake_case_							=    adapter_attn_dim

											# SequenceClassification-specific parameter. Feel free to ignore for other classes.
											snake_case_							=    classifier_proj_size

											# XVector-specific parameters. Feel free to ignore for other classes.
											snake_case_							=    list(__UpperCAmelCase       )
											snake_case_							=    list(__UpperCAmelCase       )
											snake_case_							=    list(__UpperCAmelCase       )
											snake_case_							=    xvector_output_dim




				@property
				def     a							(					self       ):
											return functools.reduce(operator.mul      ,   self.conv_stride      ,   1       )









'''simple docstring'''





import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow


if is_torch_available():
 import torch

 from transformers import XLMRobertaModel






@require_sentencepiece
@require_tokenizers
@require_torch
class        lowerCAmelCase_(				unittest.TestCase     ):
  '''simple docstring'''




  @slow
  def 							UpperCAmelCase_	(    self      )    ->		Union[str, Any]:
      lowerCAmelCase__ :  Tuple							      =					XLMRobertaModel.from_pretrained("""xlm-roberta-base"""      )
      lowerCAmelCase__ :  Optional[int]							      =					torch.tensor([[0, 581, 1_0269, 83, 9_9942, 136, 6_0742, 23, 70, 8_0583, 1_8276, 2]]      )
      # The dog is cute and lives in the garden house

      lowerCAmelCase__ :  str							      =					torch.Size((1, 12, 768)      )  # batch_size, sequence_length, embedding_vector_dim
      lowerCAmelCase__ :  Dict							      =					torch.tensor(
          [[-0.0_1_0_1, 0.1_2_1_8, -0.0_8_0_3, 0.0_8_0_1, 0.1_3_2_7, 0.0_7_7_6, -0.1_2_1_5, 0.2_3_8_3, 0.3_3_3_8, 0.3_1_0_6, 0.0_3_0_0, 0.0_2_5_2]]      )
      #  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base')
      #  xlmr.eval()
      #  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
      with torch.no_grad():
          lowerCAmelCase__ :  Optional[int]							      =					model(__UpperCAmelCase      )["""last_hidden_state"""].detach()
      self.assertEqual(output.shape  ,__UpperCAmelCase      )
      # compare the actual values for a slice of last dim
      self.assertTrue(torch.allclose(output[:, :, -1]  ,__UpperCAmelCase  ,atol=1E-3      )      )






  @slow
  def 							UpperCAmelCase_	(    self      )    ->		int:
      lowerCAmelCase__ :  Union[str, Any]							      =					XLMRobertaModel.from_pretrained("""xlm-roberta-large"""      )
      lowerCAmelCase__ :  Optional[Any]							      =					torch.tensor([[0, 581, 1_0269, 83, 9_9942, 136, 6_0742, 23, 70, 8_0583, 1_8276, 2]]      )
      # The dog is cute and lives in the garden house

      lowerCAmelCase__ :  Dict							      =					torch.Size((1, 12, 1024)      )  # batch_size, sequence_length, embedding_vector_dim
      lowerCAmelCase__ :  Union[str, Any]							      =					torch.tensor(
          [[-0.0_6_9_9, -0.0_3_1_8, 0.0_7_0_5, -0.1_2_4_1, 0.0_9_9_9, -0.0_5_2_0, 0.1_0_0_4, -0.1_8_3_8, -0.4_7_0_4, 0.1_4_3_7, 0.0_8_2_1, 0.0_1_2_6]]      )
      #  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large')
      #  xlmr.eval()
      #  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
      with torch.no_grad():
          lowerCAmelCase__ :  Union[str, Any]							      =					model(__UpperCAmelCase      )["""last_hidden_state"""].detach()
      self.assertEqual(output.shape  ,__UpperCAmelCase      )
      # compare the actual values for a slice of last dim
      self.assertTrue(torch.allclose(output[:, :, -1]  ,__UpperCAmelCase  ,atol=1E-3      )      )


