


import itertools
import os
import random
import tempfile
import unittest

import numpy as np

from transformers import TvltFeatureExtractor, is_datasets_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_torch_available():
	import torch

if is_datasets_available():
	from datasets import load_dataset

_lowerCAmelCase:						Optional[Any]   				=       random.Random()






def   _lowercase(					__a				:						int    ,     __a				:						str=1.0    ,     __a				:						Any=None    ,     __a				:						str=None     ):





							if rng is None:
														a__      =global_rng

							a__      =[]
							for batch_idx in range(shape[0]     ):
														values.append([]     )
														for _ in range(shape[1]     ):
																					values[-1].append(rng.random() * scale     )

							return values




class       lowercase_					(unittest.TestCase   ):





				def __init__(				self       ,  lowercase_       ,  lowercase_=7       ,  lowercase_=400       ,  lowercase_=2000       ,  lowercase_=2048       ,  lowercase_=128       ,  lowercase_=1       ,  lowercase_=512       ,  lowercase_=30       ,  lowercase_=44100       ,  )  -> str:
											a__      =parent
											a__      =batch_size
											a__      =min_seq_length
											a__      =max_seq_length
											a__      =(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
											a__      =spectrogram_length
											a__      =feature_size
											a__      =num_audio_channels
											a__      =hop_length
											a__      =chunk_length
											a__      =sampling_rate





				def 						__UpperCamelCase    (				self)  -> int:
											return {
											    "spectrogram_length": self.spectrogram_length,
											    "feature_size": self.feature_size,
											    "num_audio_channels": self.num_audio_channels,
											    "hop_length": self.hop_length,
											    "chunk_length": self.chunk_length,
											    "sampling_rate": self.sampling_rate,
											}





				def 						__UpperCamelCase    (				self       ,  lowercase_=False       ,  lowercase_=False)  -> Optional[int]:
											def _flatten(lowercase_):
																		return list(itertools.chain(*_lowerCamelCase))

											if equal_length:
																		a__      =[floats_list((self.max_seq_length, self.feature_size)) for _ in range(self.batch_size)]
											else:
																		# make sure that inputs increase in size
																		a__      =[
																		    floats_list((x, self.feature_size))
																		    for x in range(self.min_seq_length       ,  self.max_seq_length       ,  self.seq_length_diff)
																		]
											if numpify:
																		a__      =[np.asarray(_lowerCamelCase) for x in speech_inputs]
											return speech_inputs







@require_torch
@require_torchaudio
class       lowercase_					(_snake_case      ,					unittest.TestCase   ):
				snake_case	    =TvltFeatureExtractor





				def 						__UpperCamelCase    (				self)  -> Dict:
											a__      =TvltFeatureExtractionTester(self)





				def 						__UpperCamelCase    (				self)  -> List[Any]:
											a__      =self.feature_extraction_class(**self.feat_extract_dict)
											self.assertTrue(hasattr(_lowerCamelCase       ,  'spectrogram_length'))
											self.assertTrue(hasattr(_lowerCamelCase       ,  'feature_size'))
											self.assertTrue(hasattr(_lowerCamelCase       ,  'num_audio_channels'))
											self.assertTrue(hasattr(_lowerCamelCase       ,  'hop_length'))
											self.assertTrue(hasattr(_lowerCamelCase       ,  'chunk_length'))
											self.assertTrue(hasattr(_lowerCamelCase       ,  'sampling_rate'))





				def 						__UpperCamelCase    (				self)  -> Dict:
											a__      =self.feature_extraction_class(**self.feat_extract_dict)

											with tempfile.TemporaryDirectory() as tmpdirname:
																		a__      =feat_extract_first.save_pretrained(_lowerCamelCase)[0]
																		check_json_file_has_correct_format(_lowerCamelCase)
																		a__      =self.feature_extraction_class.from_pretrained(_lowerCamelCase)

											a__      =feat_extract_first.to_dict()
											a__      =feat_extract_second.to_dict()
											a__      =dict_first.pop('mel_filters')
											a__      =dict_second.pop('mel_filters')
											self.assertTrue(np.allclose(_lowerCamelCase       ,  _lowerCamelCase))
											self.assertEqual(_lowerCamelCase       ,  _lowerCamelCase)





				def 						__UpperCamelCase    (				self)  -> Tuple:
											a__      =self.feature_extraction_class(**self.feat_extract_dict)

											with tempfile.TemporaryDirectory() as tmpdirname:
																		a__      =os.path.join(_lowerCamelCase       ,  'feat_extract.json')
																		feat_extract_first.to_json_file(_lowerCamelCase)
																		a__      =self.feature_extraction_class.from_json_file(_lowerCamelCase)

											a__      =feat_extract_first.to_dict()
											a__      =feat_extract_second.to_dict()
											a__      =dict_first.pop('mel_filters')
											a__      =dict_second.pop('mel_filters')
											self.assertTrue(np.allclose(_lowerCamelCase       ,  _lowerCamelCase))
											self.assertEqual(_lowerCamelCase       ,  _lowerCamelCase)





				def 						__UpperCamelCase    (				self)  -> Optional[int]:
											# Initialize feature_extractor
											a__      =self.feature_extraction_class(**self.feat_extract_dict)

											# create three inputs of length 800, 1000, and 1200
											a__      =[floats_list((1, x))[0] for x in range(800       ,  1400       ,  200)]
											a__      =[np.asarray(_lowerCamelCase) for speech_input in speech_inputs]

											# Test not batched input
											a__      =feature_extractor(np_speech_inputs[0]       ,  return_tensors='np'       ,  sampling_rate=44100).audio_values

											self.assertTrue(encoded_audios.ndim == 4)
											self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size)
											self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length)
											self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels)

											# Test batched
											a__      =feature_extractor(_lowerCamelCase       ,  return_tensors='np'       ,  sampling_rate=44100).audio_values

											self.assertTrue(encoded_audios.ndim == 4)
											self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size)
											self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length)
											self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels)

											# Test audio masking
											a__      =feature_extractor(
											    _lowerCamelCase       ,  return_tensors='np'       ,  sampling_rate=44100       ,  mask_audio=_lowerCamelCase).audio_values

											self.assertTrue(encoded_audios.ndim == 4)
											self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size)
											self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length)
											self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels)

											# Test 2-D numpy arrays are batched.
											a__      =[floats_list((1, x))[0] for x in (800, 800, 800)]
											a__      =np.asarray(_lowerCamelCase)
											a__      =feature_extractor(_lowerCamelCase       ,  return_tensors='np'       ,  sampling_rate=44100).audio_values
											self.assertTrue(encoded_audios.ndim == 4)
											self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size)
											self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length)
											self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels)





				def 						__UpperCamelCase    (				self       ,  lowercase_)  -> str:
											a__      =load_dataset('hf-internal-testing/librispeech_asr_dummy'       ,  'clean'       ,  split='validation')
											# automatic decoding with librispeech
											a__      =ds.sort('id').select(range(_lowerCamelCase))[:num_samples]['''audio''']

											return [x["array"] for x in speech_samples]





				def 						__UpperCamelCase    (				self)  -> List[Any]:
											a__      =self._load_datasamples(1)
											a__      =TvltFeatureExtractor()
											a__      =feature_extractor(_lowerCamelCase       ,  return_tensors='pt').audio_values

											self.assertEquals(audio_values.shape       ,  (1, 1, 192, 128))

											a__      =torch.tensor([[-0.30_32, -0.27_08], [-0.44_34, -0.40_07]])
											self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2]       ,  _lowerCamelCase       ,  atol=1e-4))





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


snake_case							:       List[Any]										=	logging.get_logger(__name__)

snake_case							:       Dict										=	{
    '''uclanlp/visualbert-vqa''': '''https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json''',
    '''uclanlp/visualbert-vqa-pre''': '''https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json''',
    '''uclanlp/visualbert-vqa-coco-pre''': (
        '''https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json'''
    ),
    '''uclanlp/visualbert-vcr''': '''https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json''',
    '''uclanlp/visualbert-vcr-pre''': '''https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json''',
    '''uclanlp/visualbert-vcr-coco-pre''': (
        '''https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json'''
    ),
    '''uclanlp/visualbert-nlvr2''': '''https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json''',
    '''uclanlp/visualbert-nlvr2-pre''': '''https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json''',
    '''uclanlp/visualbert-nlvr2-coco-pre''': (
        '''https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json'''
    )
    # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert
}







class 						_snake_case    ( _snake_case				):
 SCREAMING_SNAKE_CASE__ =			'visual_bert'




 def __init__(						self   ,    _lowerCamelCase=3_0522   ,    _lowerCamelCase=768   ,    _lowerCamelCase=512   ,    _lowerCamelCase=12   ,    _lowerCamelCase=12   ,    _lowerCamelCase=3072   ,    _lowerCamelCase="gelu"   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=512   ,    _lowerCamelCase=2   ,    _lowerCamelCase=0.02   ,    _lowerCamelCase=1e-12   ,    _lowerCamelCase=False   ,    _lowerCamelCase=True   ,    _lowerCamelCase=1   ,    _lowerCamelCase=0   ,    _lowerCamelCase=2   ,    **_lowerCamelCase   ,    ):
       super().__init__(pad_token_id=_lowerCamelCase   ,    bos_token_id=_lowerCamelCase   ,    eos_token_id=_lowerCamelCase   ,    **_lowerCamelCase       )

       a		:Any									=			vocab_size
       a		:str									=			max_position_embeddings
       a		:str									=			hidden_size
       a		:List[Any]									=			visual_embedding_dim
       a		:str									=			num_hidden_layers
       a		:Optional[int]									=			num_attention_heads
       a		:int									=			intermediate_size
       a		:int									=			hidden_act
       a		:Union[str, Any]									=			hidden_dropout_prob
       a		:List[Any]									=			attention_probs_dropout_prob
       a		:int									=			initializer_range
       a		:List[Any]									=			type_vocab_size
       a		:str									=			layer_norm_eps
       a		:Optional[int]									=			bypass_transformer
       a		:str									=			special_visual_initialize





