

import inspect
import unittest
from math import floor

from transformers import CvtConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import CvtForImageClassification, CvtModel
 from transformers.models.cvt.modeling_cvt import CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import AutoImageProcessor



class   lowercase_   (  __lowercase					):



      def 						UpperCamelCase_		(      self				:    Optional[Any]		)   ->					Union[str, Any]:
        _snake_case						=							self.config_class(**self.inputs_dict		)
        self.parent.assertTrue(hasattr(A__      , '''embed_dim'''		)		)
        self.parent.assertTrue(hasattr(A__      , '''num_heads'''		)		)



class   lowercase_   :



      def __init__(      self				:    str      , A__				:    Any      , A__				:    List[Any]=13      , A__				:    Optional[Any]=64      , A__				:    Union[str, Any]=3      , A__				:    Optional[Any]=[16, 48, 96]      , A__				:    List[str]=[1, 3, 6]      , A__				:    Union[str, Any]=[1, 2, 10]      , A__				:    Dict=[7, 3, 3]      , A__				:    Dict=[4, 2, 2]      , A__				:    List[Any]=[2, 1, 1]      , A__				:    Dict=[2, 2, 2]      , A__				:    Union[str, Any]=[False, False, True]      , A__				:    List[Any]=[0.0, 0.0, 0.0]      , A__				:    List[str]=0.02      , A__				:    Tuple=1e-12      , A__				:    Tuple=True      , A__				:    List[Any]=True      , A__				:    Optional[int]=2      , )   ->					str:
        _snake_case						=							parent
        _snake_case						=							batch_size
        _snake_case						=							image_size
        _snake_case						=							patch_sizes
        _snake_case						=							patch_stride
        _snake_case						=							patch_padding
        _snake_case						=							is_training
        _snake_case						=							use_labels
        _snake_case						=							num_labels
        _snake_case						=							num_channels
        _snake_case						=							embed_dim
        _snake_case						=							num_heads
        _snake_case						=							stride_kv
        _snake_case						=							depth
        _snake_case						=							cls_token
        _snake_case						=							attention_drop_rate
        _snake_case						=							initializer_range
        _snake_case						=							layer_norm_eps



      def 						UpperCamelCase_		(      self				:    Tuple		)   ->					int:
        _snake_case						=							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

        _snake_case						=							None
        if self.use_labels:
          _snake_case						=							ids_tensor([self.batch_size]      , self.num_labels		)

        _snake_case						=							self.get_config()
        return config, pixel_values, labels



      def 						UpperCamelCase_		(      self				:    int		)   ->					Optional[Any]:
        return CvtConfig(
            image_size=self.image_size      , num_labels=self.num_labels      , num_channels=self.num_channels      , embed_dim=self.embed_dim      , num_heads=self.num_heads      , patch_sizes=self.patch_sizes      , patch_padding=self.patch_padding      , patch_stride=self.patch_stride      , stride_kv=self.stride_kv      , depth=self.depth      , cls_token=self.cls_token      , attention_drop_rate=self.attention_drop_rate      , initializer_range=self.initializer_range      , )



      def 						UpperCamelCase_		(      self				:    Dict      , A__				:    Dict      , A__				:    Union[str, Any]      , A__				:    Union[str, Any]		)   ->					int:
        _snake_case						=							CvtModel(config=A__		)
        model.to(A__		)
        model.eval()
        _snake_case						=							model(A__		)
        _snake_case						=							(self.image_size, self.image_size)
        _snake_case,					_snake_case						=							image_size[0], image_size[1]
        for i in range(len(self.depth		)		):
          _snake_case						=							floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1		)
          _snake_case						=							floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1		)
        self.parent.assertEqual(result.last_hidden_state.shape      , (self.batch_size, self.embed_dim[-1], height, width)		)



      def 						UpperCamelCase_		(      self				:    str      , A__				:    Optional[int]      , A__				:    Optional[Any]      , A__				:    str		)   ->					Union[str, Any]:
        _snake_case						=							self.num_labels
        _snake_case						=							CvtForImageClassification(A__		)
        model.to(A__		)
        model.eval()
        _snake_case						=							model(A__      , labels=A__		)
        self.parent.assertEqual(result.logits.shape      , (self.batch_size, self.num_labels)		)







      def 						UpperCamelCase_		(      self				:    Tuple		)   ->					Optional[Any]:
        _snake_case						=							self.prepare_config_and_inputs()
        _snake_case,					_snake_case,					_snake_case						=							config_and_inputs
        _snake_case						=							{'''pixel_values''': pixel_values}
        return config, inputs_dict



@require_torch
class   lowercase_   (  __lowercase       ,							__lowercase       ,							unittest.TestCase					):
      UpperCamelCase_      :		int										=    (CvtModel, CvtForImageClassification) if is_torch_available() else ()
      UpperCamelCase_      :		List[str]										=    (
          {"feature-extraction": CvtModel, "image-classification": CvtForImageClassification}
          if is_torch_available()
          else {}
      )

      UpperCamelCase_      :		Any										=    False
      UpperCamelCase_      :		Union[str, Any]										=    False
      UpperCamelCase_      :		Any										=    False
      UpperCamelCase_      :		List[str]										=    False
      UpperCamelCase_      :		Optional[int]										=    False



      def 						UpperCamelCase_		(      self				:    Optional[int]		)   ->					Any:
        _snake_case						=							CvtModelTester(self		)
        _snake_case						=							ConfigTester(self      , config_class=A__      , has_text_modality=A__      , hidden_size=37		)



      def 						UpperCamelCase_		(      self				:    List[Any]		)   ->					Tuple:
        self.create_and_test_config_common_properties()
        self.config_tester.create_and_test_config_to_json_string()
        self.config_tester.create_and_test_config_to_json_file()
        self.config_tester.create_and_test_config_from_and_save_pretrained()
        self.config_tester.create_and_test_config_with_num_labels()
        self.config_tester.check_config_can_be_init_without_params()
        self.config_tester.check_config_arguments_init()



      def 						UpperCamelCase_		(      self				:    Dict		)   ->					str:
        return



      @unittest.skip(reason='''Cvt does not output attentions'''		)
      def 						UpperCamelCase_		(      self				:    Dict		)   ->					int:
        pass



      @unittest.skip(reason='''Cvt does not use inputs_embeds'''		)
      def 						UpperCamelCase_		(      self				:    int		)   ->					Optional[Any]:
        pass



      @unittest.skip(reason='''Cvt does not support input and output embeddings'''		)
      def 						UpperCamelCase_		(      self				:    Optional[int]		)   ->					Optional[int]:
        pass



      def 						UpperCamelCase_		(      self				:    List[str]		)   ->					Union[str, Any]:
        _snake_case,					_snake_case						=							self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
          _snake_case						=							model_class(A__		)
          _snake_case						=							inspect.signature(model.forward		)
          # signature.parameters is an OrderedDict => so arg_names order is deterministic
          _snake_case						=							[*signature.parameters.keys()]

          _snake_case						=							['''pixel_values''']
          self.assertListEqual(arg_names[:1]      , A__		)



      def 						UpperCamelCase_		(      self				:    str		)   ->					Dict:
        _snake_case						=							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*A__		)



      def 						UpperCamelCase_		(      self				:    str		)   ->					Optional[int]:
        def check_hidden_states_output(A__				:    Tuple      , A__				:    int      , A__				:    Any		):
          _snake_case						=							model_class(A__		)
          model.to(A__		)
          model.eval()

          with torch.no_grad():
            _snake_case						=							model(**self._prepare_for_class(A__      , A__		)		)

          _snake_case						=							outputs.hidden_states

          _snake_case						=							len(self.model_tester.depth		)
          self.assertEqual(len(A__		)      , A__		)

          # verify the first hidden states (first block)
          self.assertListEqual(
              list(hidden_states[0].shape[-3:]		)      , [
                  self.model_tester.embed_dim[0],
                  self.model_tester.image_size // 4,
                  self.model_tester.image_size // 4,
              ]      , )

        _snake_case,					_snake_case						=							self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
          _snake_case						=							True
          check_hidden_states_output(A__      , A__      , A__		)

          # check that output_hidden_states also work using config
          del inputs_dict["output_hidden_states"]
          _snake_case						=							True

          check_hidden_states_output(A__      , A__      , A__		)



      def 						UpperCamelCase_		(      self				:    Tuple		)   ->					Tuple:
        _snake_case						=							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*A__		)



      @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''		)
      def 						UpperCamelCase_		(      self				:    Dict		)   ->					int:
        pass







      @slow
      def 						UpperCamelCase_		(      self				:    Dict		)   ->					Optional[int]:
        for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          _snake_case						=							CvtModel.from_pretrained(A__		)
          self.assertIsNotNone(A__		)



def 				snake_case_()							->     List[str]:



  """simple docstring"""






  _snake_case						=							Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''					)
  return image



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



      @cached_property
      def 						UpperCamelCase_		(      self				:    List[str]		)   ->					List[Any]:
        return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)







      @slow
      def 						UpperCamelCase_		(      self				:    str		)   ->					str:
        _snake_case						=							CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		).to(A__		)

        _snake_case						=							self.default_image_processor
        _snake_case						=							prepare_img()
        _snake_case						=							image_processor(images=A__      , return_tensors='''pt'''		).to(A__		)

        # forward pass
        with torch.no_grad():
          _snake_case						=							model(**A__		)

        # verify the logits
        _snake_case						=							torch.Size((1, 1000)		)
        self.assertEqual(outputs.logits.shape      , A__		)

        _snake_case						=							torch.tensor([0.9285, 0.9015, -0.3150]		).to(A__		)

        self.assertTrue(torch.allclose(outputs.logits[0, :3]      , A__      , atol=1e-4		)		)




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

__A      		=     random.Random()



def 				snake_case_(_UpperCamelCase			,	_UpperCamelCase=1.0			,	_UpperCamelCase=None			,	_UpperCamelCase=None					)							->     Optional[int]:



  """simple docstring"""






  if rng is None:
    _snake_case						=							global_rng

  _snake_case						=							[]
  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				:    List[Any]      , A__				:    List[Any]      , A__				:    int=7      , A__				:    Tuple=400      , A__				:    int=2000      , A__				:    Any=2048      , A__				:    List[Any]=128      , A__				:    Optional[int]=1      , A__				:    Optional[Any]=512      , A__				:    Any=30      , A__				:    Any=44100      , )   ->					int:
        _snake_case						=							parent
        _snake_case						=							batch_size
        _snake_case						=							min_seq_length
        _snake_case						=							max_seq_length
        _snake_case						=							(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
        _snake_case						=							spectrogram_length
        _snake_case						=							feature_size
        _snake_case						=							num_audio_channels
        _snake_case						=							hop_length
        _snake_case						=							chunk_length
        _snake_case						=							sampling_rate



      def 						UpperCamelCase_		(      self				:    str		)   ->					Optional[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				:    Any      , A__				:    Any=False      , A__				:    List[str]=False		)   ->					Tuple:
        def _flatten(A__				:    List[str]		):
          return list(itertools.chain(*A__		)		)

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



@require_torch
@require_torchaudio
class   lowercase_   (  __lowercase       ,							unittest.TestCase					):
      UpperCamelCase_      :		Optional[int]										=    TvltFeatureExtractor



      def 						UpperCamelCase_		(      self				:    Dict		)   ->					List[str]:
        _snake_case						=							TvltFeatureExtractionTester(self		)



      def 						UpperCamelCase_		(      self				:    int		)   ->					Optional[int]:
        _snake_case						=							self.feature_extraction_class(**self.feat_extract_dict		)
        self.assertTrue(hasattr(A__      , '''spectrogram_length'''		)		)
        self.assertTrue(hasattr(A__      , '''feature_size'''		)		)
        self.assertTrue(hasattr(A__      , '''num_audio_channels'''		)		)
        self.assertTrue(hasattr(A__      , '''hop_length'''		)		)
        self.assertTrue(hasattr(A__      , '''chunk_length'''		)		)
        self.assertTrue(hasattr(A__      , '''sampling_rate'''		)		)



      def 						UpperCamelCase_		(      self				:    Any		)   ->					Union[str, Any]:
        _snake_case						=							self.feature_extraction_class(**self.feat_extract_dict		)

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

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



      def 						UpperCamelCase_		(      self				:    int		)   ->					Union[str, Any]:
        _snake_case						=							self.feature_extraction_class(**self.feat_extract_dict		)

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

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



      def 						UpperCamelCase_		(      self				:    Union[str, Any]		)   ->					Any:
        # Initialize feature_extractor
        _snake_case						=							self.feature_extraction_class(**self.feat_extract_dict		)

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

        # Test not batched input
        _snake_case						=							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
        _snake_case						=							feature_extractor(A__      , 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
        _snake_case						=							feature_extractor(
            A__      , return_tensors='''np'''      , sampling_rate=44100      , mask_audio=A__		).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.
        _snake_case						=							[floats_list((1, x)		)[0] for x in (800, 800, 800)]
        _snake_case						=							np.asarray(A__		)
        _snake_case						=							feature_extractor(A__      , 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				:    Optional[Any]      , A__				:    Any		)   ->					Optional[int]:
        _snake_case						=							load_dataset('''hf-internal-testing/librispeech_asr_dummy'''      , '''clean'''      , split='''validation'''		)
        # automatic decoding with librispeech
        _snake_case						=							ds.sort('''id'''		).select(range(A__		)		)[:num_samples]['''audio''']

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







      def 						UpperCamelCase_		(      self				:    List[str]		)   ->					Optional[Any]:
        _snake_case						=							self._load_datasamples(1		)
        _snake_case						=							TvltFeatureExtractor()
        _snake_case						=							feature_extractor(A__      , return_tensors='''pt'''		).audio_values

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

        _snake_case						=							torch.tensor([[-0.3032, -0.2708], [-0.4434, -0.4007]]		)
        self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2]      , A__      , atol=1e-4		)		)

