import functools
import operator

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


UpperCAmelCase_								=							logging.get_logger(__name__)

UpperCAmelCase_								=							{
    'microsoft/unispeech-sat-base-100h-libri-ft': (
        'https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json'
    ),
    # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat
}





class      lowerCAmelCase						(					snake_case__			):
     _SCREAMING_SNAKE_CASE     :     int		="""unispeech-sat"""



     def __init__(					self				,      lowerCAmelCase__=32				,      lowerCAmelCase__=768				,      lowerCAmelCase__=12				,      lowerCAmelCase__=12				,      lowerCAmelCase__=3072				,      lowerCAmelCase__="gelu"				,      lowerCAmelCase__=0.1				,      lowerCAmelCase__=0.1				,      lowerCAmelCase__=0.1				,      lowerCAmelCase__=0.0				,      lowerCAmelCase__=0.0				,      lowerCAmelCase__=0.1				,      lowerCAmelCase__=0.1				,      lowerCAmelCase__=0.02				,      lowerCAmelCase__=1E-5				,      lowerCAmelCase__="group"				,      lowerCAmelCase__="gelu"				,      lowerCAmelCase__=(512, 512, 512, 512, 512, 512, 512)				,      lowerCAmelCase__=(5, 2, 2, 2, 2, 2, 2)				,      lowerCAmelCase__=(10, 3, 3, 3, 3, 2, 2)				,      lowerCAmelCase__=False				,      lowerCAmelCase__=128				,      lowerCAmelCase__=16				,      lowerCAmelCase__=False				,      lowerCAmelCase__=True				,      lowerCAmelCase__=0.05				,      lowerCAmelCase__=10				,      lowerCAmelCase__=2				,      lowerCAmelCase__=0.0				,      lowerCAmelCase__=10				,      lowerCAmelCase__=0				,      lowerCAmelCase__=320				,      lowerCAmelCase__=2				,      lowerCAmelCase__=0.1				,      lowerCAmelCase__=100				,      lowerCAmelCase__=256				,      lowerCAmelCase__=256				,      lowerCAmelCase__=0.1				,      lowerCAmelCase__="mean"				,      lowerCAmelCase__=False				,      lowerCAmelCase__=False				,      lowerCAmelCase__=256				,      lowerCAmelCase__=(512, 512, 512, 512, 1500)				,      lowerCAmelCase__=(5, 3, 3, 1, 1)				,      lowerCAmelCase__=(1, 2, 3, 1, 1)				,      lowerCAmelCase__=512				,      lowerCAmelCase__=0				,      lowerCAmelCase__=1				,      lowerCAmelCase__=2				,      lowerCAmelCase__=504				,      **lowerCAmelCase__				,      ):
            super().__init__(**UpperCAmelCase_				,      pad_token_id=UpperCAmelCase_				,      bos_token_id=UpperCAmelCase_				,      eos_token_id=UpperCAmelCase_       )
            _A=	hidden_size
            _A=	feat_extract_norm
            _A=	feat_extract_activation
            _A=	list(UpperCAmelCase_       )
            _A=	list(UpperCAmelCase_       )
            _A=	list(UpperCAmelCase_       )
            _A=	conv_bias
            _A=	num_conv_pos_embeddings
            _A=	num_conv_pos_embedding_groups
            _A=	len(self.conv_dim       )
            _A=	num_hidden_layers
            _A=	intermediate_size
            _A=	hidden_act
            _A=	num_attention_heads
            _A=	hidden_dropout
            _A=	attention_dropout
            _A=	activation_dropout
            _A=	feat_proj_dropout
            _A=	final_dropout
            _A=	layerdrop
            _A=	layer_norm_eps
            _A=	initializer_range
            _A=	vocab_size
            _A=	num_clusters
            _A=	do_stable_layer_norm
            _A=	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
            _A=	apply_spec_augment
            _A=	mask_time_prob
            _A=	mask_time_length
            _A=	mask_time_min_masks
            _A=	mask_feature_prob
            _A=	mask_feature_length
            _A=	mask_feature_min_masks

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

            # ctc loss
            _A=	ctc_loss_reduction
            _A=	ctc_zero_infinity

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

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





     @property
     def 			a__  (					self       ):
            return functools.reduce(operator.mul				,      self.conv_stride				,      1       )
import os
import tempfile
import unittest
import uuid
from pathlib import Path

from transformers.testing_utils import get_tests_dir, require_soundfile, require_torch, require_vision
from transformers.tools.agent_types import AgentAudio, AgentImage, AgentText
from transformers.utils import is_soundfile_availble, is_torch_available, is_vision_available


if is_torch_available():
      import torch

if is_soundfile_availble():
      import soundfile as sf

if is_vision_available():
      from PIL import Image

def 							UpperCamelCase				(    lowerCAmelCase_=""   )		->						str:




       '''simple docstring'''






       _A=	tempfile.mkdtemp()
       return os.path.join(lowerCAmelCase_       ,					str(uuid.uuida()   ) + suffix   )





@require_soundfile
@require_torch
class      lowerCAmelCase						(					unittest.TestCase			):



     def 			a__  (					self       ):
            _A=	torch.rand(12				,      dtype=torch.floataa       ) - 0.5
            _A=	AgentAudio(lowerCAmelCase__       )
            _A=	str(agent_type.to_string()       )

            # Ensure that the tensor and the agent_type's tensor are the same
            self.assertTrue(torch.allclose(lowerCAmelCase__				,      agent_type.to_raw()				,      atol=1E-4       )       )

            del agent_type

            # Ensure the path remains even after the object deletion
            self.assertTrue(os.path.exists(lowerCAmelCase__       )       )

            # Ensure that the file contains the same value as the original tensor
            _A,			_A=	sf.read(lowerCAmelCase__       )
            self.assertTrue(torch.allclose(lowerCAmelCase__				,      torch.tensor(lowerCAmelCase__       )				,      atol=1E-4       )       )





     def 			a__  (					self       ):
            _A=	torch.rand(12				,      dtype=torch.floataa       ) - 0.5
            _A=	get_new_path(suffix='.wav'       )
            sf.write(lowerCAmelCase__				,      lowerCAmelCase__				,      16000       )

            _A=	AgentAudio(lowerCAmelCase__       )

            self.assertTrue(torch.allclose(lowerCAmelCase__				,      agent_type.to_raw()				,      atol=1E-4       )       )
            self.assertEqual(agent_type.to_string()				,      lowerCAmelCase__       )





@require_vision
@require_torch
class      lowerCAmelCase						(					unittest.TestCase			):



     def 			a__  (					self       ):
            _A=	torch.randint(0				,      256				,      (64, 64, 3)       )
            _A=	AgentImage(lowerCAmelCase__       )
            _A=	str(agent_type.to_string()       )

            # Ensure that the tensor and the agent_type's tensor are the same
            self.assertTrue(torch.allclose(lowerCAmelCase__				,      agent_type._tensor				,      atol=1E-4       )       )

            self.assertIsInstance(agent_type.to_raw()				,      Image.Image       )

            # Ensure the path remains even after the object deletion
            del agent_type
            self.assertTrue(os.path.exists(lowerCAmelCase__       )       )



     def 			a__  (					self       ):
            _A=	Path(get_tests_dir('fixtures/tests_samples/COCO'       )       ) / '000000039769.png'
            _A=	Image.open(lowerCAmelCase__       )
            _A=	AgentImage(lowerCAmelCase__       )

            self.assertTrue(path.samefile(agent_type.to_string()       )       )
            self.assertTrue(image == agent_type.to_raw()       )

            # Ensure the path remains even after the object deletion
            del agent_type
            self.assertTrue(os.path.exists(lowerCAmelCase__       )       )





     def 			a__  (					self       ):
            _A=	Path(get_tests_dir('fixtures/tests_samples/COCO'       )       ) / '000000039769.png'
            _A=	Image.open(lowerCAmelCase__       )
            _A=	AgentImage(lowerCAmelCase__       )

            self.assertFalse(path.samefile(agent_type.to_string()       )       )
            self.assertTrue(image == agent_type.to_raw()       )

            # Ensure the path remains even after the object deletion
            del agent_type
            self.assertTrue(os.path.exists(lowerCAmelCase__       )       )





class      lowerCAmelCase						(					unittest.TestCase			):



     def 			a__  (					self       ):
            _A=	'Hey!'
            _A=	AgentText(lowerCAmelCase__       )

            self.assertEqual(lowerCAmelCase__				,      agent_type.to_string()       )
            self.assertEqual(lowerCAmelCase__				,      agent_type.to_raw()       )
            self.assertEqual(lowerCAmelCase__				,      lowerCAmelCase__       )