


'''simple docstring'''







import itertools
import random
import unittest

import numpy as np

from transformers import BatchFeature, SpeechTaFeatureExtractor
from transformers.testing_utils import require_torch
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_torch_available():
 import torch


SCREAMING_SNAKE_CASE_      =			random.Random()




def        __lowercase						(      __SCREAMING_SNAKE_CASE				,  __SCREAMING_SNAKE_CASE=1.0				,  __SCREAMING_SNAKE_CASE=None				,  __SCREAMING_SNAKE_CASE=None		)				->					Union[str, Any]:
     """simple docstring"""




     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


@require_torch
class  lowerCAmelCase_						(       unittest.TestCase      ):





  """simple docstring"""



  def __init__(					self     :							Any				,							SCREAMING_SNAKE_CASE__     :							Optional[int]				,							SCREAMING_SNAKE_CASE__     :							Optional[int]=7				,							SCREAMING_SNAKE_CASE__     :							Union[str, Any]=4_0_0				,							SCREAMING_SNAKE_CASE__     :							List[str]=2_0_0_0				,							SCREAMING_SNAKE_CASE__     :							Tuple=1				,							SCREAMING_SNAKE_CASE__     :							str=0.0				,							SCREAMING_SNAKE_CASE__     :							Optional[int]=1_6_0_0_0				,							SCREAMING_SNAKE_CASE__     :							int=True				,							SCREAMING_SNAKE_CASE__     :							Dict=8_0				,							SCREAMING_SNAKE_CASE__     :							str=1_6				,							SCREAMING_SNAKE_CASE__     :							Tuple=6_4				,							SCREAMING_SNAKE_CASE__     :							List[str]="hann_window"				,							SCREAMING_SNAKE_CASE__     :							Optional[Any]=8_0				,							SCREAMING_SNAKE_CASE__     :							Dict=7_6_0_0				,							SCREAMING_SNAKE_CASE__     :							str=1E-10				,							SCREAMING_SNAKE_CASE__     :							Tuple=True				,							):





       '''simple docstring'''

       __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       =   feature_size
       __a       =   padding_value
       __a       =   sampling_rate
       __a       =   do_normalize
       __a       =   num_mel_bins
       __a       =   hop_length
       __a       =   win_length
       __a       =   win_function
       __a       =   fmin
       __a       =   fmax
       __a       =   mel_floor
       __a       =   return_attention_mask

  def __a						(					self     :							List[Any]						):





       '''simple docstring'''

       return {
           "feature_size": self.feature_size,
           "padding_value": self.padding_value,
           "sampling_rate": self.sampling_rate,
           "do_normalize": self.do_normalize,
           "num_mel_bins": self.num_mel_bins,
           "hop_length": self.hop_length,
           "win_length": self.win_length,
           "win_function": self.win_function,
           "fmin": self.fmin,
           "fmax": self.fmax,
           "mel_floor": self.mel_floor,
           "return_attention_mask": self.return_attention_mask,
       }

  def __a						(					self     :							Any				,							SCREAMING_SNAKE_CASE__     :							Dict=False				,							SCREAMING_SNAKE_CASE__     :							str=False						):





       '''simple docstring'''

       def _flatten(SCREAMING_SNAKE_CASE__     :							Optional[int]						):
            return list(itertools.chain(*SCREAMING_SNAKE_CASE__						)						)

       if equal_length:
            __a       =   floats_list((self.batch_size, self.max_seq_length)						)
       else:
            # make sure that inputs increase in size
            __a       =   [
                _flatten(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(SCREAMING_SNAKE_CASE__						) for x in speech_inputs]

       return speech_inputs


  def __a						(					self     :							str				,							SCREAMING_SNAKE_CASE__     :							List[str]=False				,							SCREAMING_SNAKE_CASE__     :							Union[str, Any]=False						):





       '''simple docstring'''

       if equal_length:
            __a       =   [floats_list((self.max_seq_length, self.num_mel_bins)						) for _ in range(self.batch_size						)]
       else:
            # make sure that inputs increase in size
            __a       =   [
                floats_list((x, self.num_mel_bins)						)
                for x in range(self.min_seq_length				,							self.max_seq_length				,							self.seq_length_diff						)
            ]

       if numpify:
            __a       =   [np.asarray(SCREAMING_SNAKE_CASE__						) for x in speech_inputs]

       return speech_inputs


@require_torch
class  lowerCAmelCase_						(       snake_case__		,    unittest.TestCase      ):





  """simple docstring"""



  a_       :Optional[int]												=SpeechTaFeatureExtractor

  def __a						(					self     :							Optional[Any]						):





       '''simple docstring'''

       __a       =   SpeechTaFeatureExtractionTester(self						)

  def __a						(					self     :							Tuple				,							SCREAMING_SNAKE_CASE__     :							Any						):





       '''simple docstring'''

       self.assertTrue(np.all(np.mean(SCREAMING_SNAKE_CASE__				,							axis=0						) < 1E-3						)						)
       self.assertTrue(np.all(np.abs(np.var(SCREAMING_SNAKE_CASE__				,							axis=0						) - 1						) < 1E-3						)						)

  def __a						(					self     :							Any						):





       '''simple docstring'''

       __a       =   self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
       # create three inputs of length 800, 1000, and 1200
       __a       =   [floats_list((1, x)						)[0] for x in range(8_0_0				,							1_4_0_0				,							2_0_0						)]
       __a       =   [np.asarray(SCREAMING_SNAKE_CASE__						) for speech_input in speech_inputs]

       # Test not batched input
       __a       =   feat_extract(speech_inputs[0]				,							return_tensors="""np"""						).input_values
       __a       =   feat_extract(np_speech_inputs[0]				,							return_tensors="""np"""						).input_values
       self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							atol=1E-3						)						)

       # Test batched
       __a       =   feat_extract(SCREAMING_SNAKE_CASE__				,							return_tensors="""np"""						).input_values
       __a       =   feat_extract(SCREAMING_SNAKE_CASE__				,							return_tensors="""np"""						).input_values
       for enc_seq_a, enc_seq_a in zip(SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__						):
            self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							atol=1E-3						)						)

  def __a						(					self     :							List[str]						):





       '''simple docstring'''

       __a       =   self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
       __a       =   [floats_list((1, x)						)[0] for x in range(8_0_0				,							1_4_0_0				,							2_0_0						)]

       __a       =   ["""longest""", """max_length""", """do_not_pad"""]
       __a       =   [None, 1_6_0_0, None]
       for max_length, padding in zip(SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__						):
            __a       =   feat_extract(SCREAMING_SNAKE_CASE__				,							padding=SCREAMING_SNAKE_CASE__				,							max_length=SCREAMING_SNAKE_CASE__				,							return_tensors="""np"""						)
            __a       =   processed.input_values

            self._check_zero_mean_unit_variance(input_values[0][:8_0_0]						)
            self.assertTrue(input_values[0][8_0_0:].sum() < 1E-6						)
            self._check_zero_mean_unit_variance(input_values[1][:1_0_0_0]						)
            self.assertTrue(input_values[0][1_0_0_0:].sum() < 1E-6						)
            self._check_zero_mean_unit_variance(input_values[2][:1_2_0_0]						)

  def __a						(					self     :							Optional[int]						):





       '''simple docstring'''

       __a       =   self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
       __a       =   range(8_0_0				,							1_4_0_0				,							2_0_0						)
       __a       =   [floats_list((1, x)						)[0] for x in lengths]

       __a       =   ["""longest""", """max_length""", """do_not_pad"""]
       __a       =   [None, 1_6_0_0, None]

       for max_length, padding in zip(SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__						):
            __a       =   feat_extract(SCREAMING_SNAKE_CASE__				,							max_length=SCREAMING_SNAKE_CASE__				,							padding=SCREAMING_SNAKE_CASE__						)
            __a       =   processed.input_values

            self._check_zero_mean_unit_variance(input_values[0][:8_0_0]						)
            self._check_zero_mean_unit_variance(input_values[1][:1_0_0_0]						)
            self._check_zero_mean_unit_variance(input_values[2][:1_2_0_0]						)

  def __a						(					self     :							Tuple						):





       '''simple docstring'''

       __a       =   self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
       __a       =   [floats_list((1, x)						)[0] for x in range(8_0_0				,							1_4_0_0				,							2_0_0						)]
       __a       =   feat_extract(
           SCREAMING_SNAKE_CASE__				,							truncation=SCREAMING_SNAKE_CASE__				,							max_length=1_0_0_0				,							padding="""max_length"""				,							return_tensors="""np"""						)
       __a       =   processed.input_values

       self._check_zero_mean_unit_variance(input_values[0, :8_0_0]						)
       self._check_zero_mean_unit_variance(input_values[1]						)
       self._check_zero_mean_unit_variance(input_values[2]						)

  def __a						(					self     :							int						):





       '''simple docstring'''

       __a       =   self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
       __a       =   [floats_list((1, x)						)[0] for x in range(8_0_0				,							1_4_0_0				,							2_0_0						)]
       __a       =   feat_extract(
           SCREAMING_SNAKE_CASE__				,							truncation=SCREAMING_SNAKE_CASE__				,							max_length=1_0_0_0				,							padding="""longest"""				,							return_tensors="""np"""						)
       __a       =   processed.input_values

       self._check_zero_mean_unit_variance(input_values[0, :8_0_0]						)
       self._check_zero_mean_unit_variance(input_values[1, :1_0_0_0]						)
       self._check_zero_mean_unit_variance(input_values[2]						)

       # make sure that if max_length < longest -> then pad to max_length
       self.assertTrue(input_values.shape == (3, 1_0_0_0)						)

       __a       =   [floats_list((1, x)						)[0] for x in range(8_0_0				,							1_4_0_0				,							2_0_0						)]
       __a       =   feat_extract(
           SCREAMING_SNAKE_CASE__				,							truncation=SCREAMING_SNAKE_CASE__				,							max_length=2_0_0_0				,							padding="""longest"""				,							return_tensors="""np"""						)
       __a       =   processed.input_values

       self._check_zero_mean_unit_variance(input_values[0, :8_0_0]						)
       self._check_zero_mean_unit_variance(input_values[1, :1_0_0_0]						)
       self._check_zero_mean_unit_variance(input_values[2]						)

       # make sure that if max_length > longest -> then pad to longest
       self.assertTrue(input_values.shape == (3, 1_2_0_0)						)

  def __a						(					self     :							Dict						):





       '''simple docstring'''

       __a       =   self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
       __a       =   np.random.rand(1_0_0						).astype(np.floataa						)
       __a       =   np_speech_inputs.tolist()

       for inputs in [py_speech_inputs, np_speech_inputs]:
            __a       =   feature_extractor.pad([{"""input_values""": inputs}]				,							return_tensors="""np"""						)
            self.assertTrue(np_processed.input_values.dtype == np.floataa						)
            __a       =   feature_extractor.pad([{"""input_values""": inputs}]				,							return_tensors="""pt"""						)
            self.assertTrue(pt_processed.input_values.dtype == torch.floataa						)

  def __a						(					self     :							List[Any]						):





       '''simple docstring'''

       __a       =   self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
       # create three inputs of length 800, 1000, and 1200
       __a       =   [floats_list((1, x)						)[0] for x in range(8_0_0				,							1_4_0_0				,							2_0_0						)]
       __a       =   [np.asarray(SCREAMING_SNAKE_CASE__						) for speech_input in speech_inputs]

       # Test feature size
       __a       =   feature_extractor(audio_target=SCREAMING_SNAKE_CASE__				,							padding=SCREAMING_SNAKE_CASE__				,							return_tensors="""np"""						).input_values
       self.assertTrue(input_values.ndim == 3						)
       self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins						)

       # Test not batched input
       __a       =   feature_extractor(speech_inputs[0]				,							return_tensors="""np"""						).input_values
       __a       =   feature_extractor(np_speech_inputs[0]				,							return_tensors="""np"""						).input_values
       self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							atol=1E-3						)						)

       # Test batched
       __a       =   feature_extractor(SCREAMING_SNAKE_CASE__				,							return_tensors="""np"""						).input_values
       __a       =   feature_extractor(SCREAMING_SNAKE_CASE__				,							return_tensors="""np"""						).input_values
       for enc_seq_a, enc_seq_a in zip(SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__						):
            self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							atol=1E-3						)						)

       # Test 2-D numpy arrays are batched.
       __a       =   [floats_list((1, x)						)[0] for x in (8_0_0, 8_0_0, 8_0_0)]
       __a       =   np.asarray(SCREAMING_SNAKE_CASE__						)
       __a       =   feature_extractor(SCREAMING_SNAKE_CASE__				,							return_tensors="""np"""						).input_values
       __a       =   feature_extractor(SCREAMING_SNAKE_CASE__				,							return_tensors="""np"""						).input_values
       for enc_seq_a, enc_seq_a in zip(SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__						):
            self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							atol=1E-3						)						)

  def __a						(					self     :							int						):





       '''simple docstring'''

       __a       =   self.feat_extract_tester.prepare_inputs_for_target()
       __a       =   self.feature_extraction_class(**self.feat_extract_dict						)
       __a       =   feat_extract.model_input_names[0]

       __a       =   BatchFeature({input_name: speech_inputs}						)

       self.assertTrue(all(len(SCREAMING_SNAKE_CASE__						) == len(SCREAMING_SNAKE_CASE__						) for x, y in zip(SCREAMING_SNAKE_CASE__				,							processed_features[input_name]						)						)						)

       __a       =   self.feat_extract_tester.prepare_inputs_for_target(equal_length=SCREAMING_SNAKE_CASE__						)
       __a       =   BatchFeature({input_name: speech_inputs}				,							tensor_type="""np"""						)

       __a       =   processed_features[input_name]

       if len(batch_features_input.shape						) < 3:
            __a       =   batch_features_input[:, :, None]

       self.assertTrue(
           batch_features_input.shape
           == (self.feat_extract_tester.batch_size, len(speech_inputs[0]						), self.feat_extract_tester.num_mel_bins)						)

  @require_torch
  def __a						(					self     :							Any						):





       '''simple docstring'''

       __a       =   self.feat_extract_tester.prepare_inputs_for_target(equal_length=SCREAMING_SNAKE_CASE__						)
       __a       =   self.feature_extraction_class(**self.feat_extract_dict						)
       __a       =   feat_extract.model_input_names[0]

       __a       =   BatchFeature({input_name: speech_inputs}				,							tensor_type="""pt"""						)

       __a       =   processed_features[input_name]

       if len(batch_features_input.shape						) < 3:
            __a       =   batch_features_input[:, :, None]

       self.assertTrue(
           batch_features_input.shape
           == (self.feat_extract_tester.batch_size, len(speech_inputs[0]						), self.feat_extract_tester.num_mel_bins)						)

  @require_torch
  def __a						(					self     :							List[str]						):





       '''simple docstring'''

       __a       =   self.feature_extraction_class(**self.feat_extract_dict						)
       __a       =   self.feat_extract_tester.prepare_inputs_for_target()
       __a       =   feat_extract.model_input_names[0]

       __a       =   BatchFeature({input_name: speech_inputs}						)

       __a       =   feat_extract.num_mel_bins  # hack!

       __a       =   feat_extract.pad(SCREAMING_SNAKE_CASE__				,							padding="""longest"""				,							return_tensors="""np"""						)[input_name]
       __a       =   feat_extract.pad(SCREAMING_SNAKE_CASE__				,							padding="""longest"""				,							return_tensors="""pt"""						)[input_name]

       self.assertTrue(abs(input_np.astype(np.floataa						).sum() - input_pt.numpy().astype(np.floataa						).sum()						) < 1E-2						)

  def __a						(					self     :							Dict						):





       '''simple docstring'''

       __a       =   self.feat_extract_dict
       __a       =   True
       __a       =   self.feature_extraction_class(**SCREAMING_SNAKE_CASE__						)
       __a       =   self.feat_extract_tester.prepare_inputs_for_target()
       __a       =   [len(SCREAMING_SNAKE_CASE__						) for x in speech_inputs]
       __a       =   feat_extract.model_input_names[0]

       __a       =   BatchFeature({input_name: speech_inputs}						)

       __a       =   feat_extract.num_mel_bins  # hack!

       __a       =   feat_extract.pad(SCREAMING_SNAKE_CASE__				,							padding="""longest"""				,							return_tensors="""np"""						)
       self.assertIn("""attention_mask"""				,							SCREAMING_SNAKE_CASE__						)
       self.assertListEqual(list(processed.attention_mask.shape						)				,							list(processed[input_name].shape[:2]						)						)
       self.assertListEqual(processed.attention_mask.sum(-1						).tolist()				,							SCREAMING_SNAKE_CASE__						)

  def __a						(					self     :							List[Any]						):





       '''simple docstring'''

       __a       =   self.feat_extract_dict
       __a       =   True
       __a       =   self.feature_extraction_class(**SCREAMING_SNAKE_CASE__						)
       __a       =   self.feat_extract_tester.prepare_inputs_for_target()
       __a       =   [len(SCREAMING_SNAKE_CASE__						) for x in speech_inputs]
       __a       =   feat_extract.model_input_names[0]

       __a       =   BatchFeature({input_name: speech_inputs}						)
       __a       =   min(SCREAMING_SNAKE_CASE__						)

       __a       =   feat_extract.num_mel_bins  # hack!

       __a       =   feat_extract.pad(
           SCREAMING_SNAKE_CASE__				,							padding="""max_length"""				,							max_length=SCREAMING_SNAKE_CASE__				,							truncation=SCREAMING_SNAKE_CASE__				,							return_tensors="""np"""						)
       self.assertIn("""attention_mask"""				,							SCREAMING_SNAKE_CASE__						)
       self.assertListEqual(
           list(processed_pad.attention_mask.shape						)				,							[processed_pad[input_name].shape[0], max_length]						)
       self.assertListEqual(
           processed_pad.attention_mask[:, :max_length].sum(-1						).tolist()				,							[max_length for x in speech_inputs]						)

  def __a						(					self     :							Any				,							SCREAMING_SNAKE_CASE__     :							Dict						):





       '''simple docstring'''

       from datasets import load_dataset

       __a       =   load_dataset("""hf-internal-testing/librispeech_asr_dummy"""				,							"""clean"""				,							split="""validation"""						)
       # automatic decoding with librispeech
       __a       =   ds.sort("""id"""						).select(range(SCREAMING_SNAKE_CASE__						)						)[:num_samples]["""audio"""]

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

  def __a						(					self     :							int						):





       '''simple docstring'''

       __a       =   torch.tensor(
           [2.38_04E-03, 2.07_52E-03, 1.98_36E-03, 2.10_57E-03, 1.61_74E-03,
            3.05_18E-04, 9.15_53E-05, 3.35_69E-04, 9.76_56E-04, 1.83_11E-03,
            2.01_42E-03, 2.10_57E-03, 1.73_95E-03, 4.57_76E-04, -3.96_73E-04,
            4.57_76E-04, 1.00_71E-03, 9.15_53E-05, 4.88_28E-04, 1.15_97E-03,
            7.32_42E-04, 9.46_04E-04, 1.80_05E-03, 1.83_11E-03, 8.85_01E-04,
            4.27_25E-04, 4.88_28E-04, 7.32_42E-04, 1.09_86E-03, 2.10_57E-03]						)
       # fmt: on

       __a       =   self._load_datasamples(1						)
       __a       =   SpeechTaFeatureExtractor()
       __a       =   feature_extractor(SCREAMING_SNAKE_CASE__				,							return_tensors="""pt"""						).input_values
       self.assertEquals(input_values.shape				,							(1, 9_3_6_8_0)						)
       self.assertTrue(torch.allclose(input_values[0, :3_0]				,							SCREAMING_SNAKE_CASE__				,							atol=1E-6						)						)


  def __a						(					self     :							Optional[int]						):





       '''simple docstring'''

       __a       =   torch.tensor(
           [-2.6_8_7_0, -3.0_1_0_4, -3.1_3_5_6, -3.5_3_5_2, -3.0_0_4_4, -3.0_3_5_3, -3.4_7_1_9, -3.6_7_7_7,
            -3.1_5_2_0, -2.9_4_3_5, -2.6_5_5_3, -2.8_7_9_5, -2.9_9_4_4, -2.5_9_2_1, -3.0_2_7_9, -3.0_3_8_6,
            -3.0_8_6_4, -3.1_2_9_1, -3.2_3_5_3, -2.7_4_4_4, -2.6_8_3_1, -2.7_2_8_7, -3.1_7_6_1, -3.1_5_7_1,
            -3.2_7_2_6, -3.0_5_8_2, -3.1_0_0_7, -3.4_5_3_3, -3.4_6_9_5, -3.0_9_9_8]						)
       # fmt: on

       __a       =   self._load_datasamples(1						)
       __a       =   SpeechTaFeatureExtractor()
       __a       =   feature_extractor(audio_target=SCREAMING_SNAKE_CASE__				,							return_tensors="""pt"""						).input_values
       self.assertEquals(input_values.shape				,							(1, 3_6_6, 8_0)						)
       self.assertTrue(torch.allclose(input_values[0, 0, :3_0]				,							SCREAMING_SNAKE_CASE__				,							atol=1E-4						)						)










'''simple docstring'''







import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_gpu,
    require_vision,
    slow,
    torch_device,
)
from transformers.utils import cached_property, is_torch_available, is_vision_available

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 torch import nn

 from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel
 from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import ViTImageProcessor


class  lowerCAmelCase_						:





  """simple docstring"""



  def __init__(					self     :							Any				,							SCREAMING_SNAKE_CASE__     :							int				,							SCREAMING_SNAKE_CASE__     :							List[str]=1_3				,							SCREAMING_SNAKE_CASE__     :							int=3_0				,							SCREAMING_SNAKE_CASE__     :							List[str]=2				,							SCREAMING_SNAKE_CASE__     :							Union[str, Any]=3				,							SCREAMING_SNAKE_CASE__     :							List[str]=True				,							SCREAMING_SNAKE_CASE__     :							Dict=True				,							SCREAMING_SNAKE_CASE__     :							str=3_2				,							SCREAMING_SNAKE_CASE__     :							Tuple=5				,							SCREAMING_SNAKE_CASE__     :							List[Any]=4				,							SCREAMING_SNAKE_CASE__     :							Dict=3_7				,							SCREAMING_SNAKE_CASE__     :							Any="gelu"				,							SCREAMING_SNAKE_CASE__     :							Optional[Any]=0.1				,							SCREAMING_SNAKE_CASE__     :							Union[str, Any]=0.1				,							SCREAMING_SNAKE_CASE__     :							Union[str, Any]=1_0				,							SCREAMING_SNAKE_CASE__     :							str=0.0_2				,							SCREAMING_SNAKE_CASE__     :							Union[str, Any]=None				,							SCREAMING_SNAKE_CASE__     :							Any=2				,							):





       '''simple docstring'''

       __a       =   parent
       __a       =   batch_size
       __a       =   image_size
       __a       =   patch_size
       __a       =   num_channels
       __a       =   is_training
       __a       =   use_labels
       __a       =   hidden_size
       __a       =   num_hidden_layers
       __a       =   num_attention_heads
       __a       =   intermediate_size
       __a       =   hidden_act
       __a       =   hidden_dropout_prob
       __a       =   attention_probs_dropout_prob
       __a       =   type_sequence_label_size
       __a       =   initializer_range
       __a       =   scope
       __a       =   encoder_stride

       # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
       __a       =   (image_size // patch_size) ** 2
       __a       =   num_patches + 1

  def __a						(					self     :							Any						):





       '''simple docstring'''

       __a       =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

       __a       =   None
       if self.use_labels:
            __a       =   ids_tensor([self.batch_size]				,							self.type_sequence_label_size						)

       __a       =   self.get_config()

       return config, pixel_values, labels

  def __a						(					self     :							Optional[int]						):





       '''simple docstring'''

       return ViTConfig(
           image_size=self.image_size				,							patch_size=self.patch_size				,							num_channels=self.num_channels				,							hidden_size=self.hidden_size				,							num_hidden_layers=self.num_hidden_layers				,							num_attention_heads=self.num_attention_heads				,							intermediate_size=self.intermediate_size				,							hidden_act=self.hidden_act				,							hidden_dropout_prob=self.hidden_dropout_prob				,							attention_probs_dropout_prob=self.attention_probs_dropout_prob				,							is_decoder=SCREAMING_SNAKE_CASE__				,							initializer_range=self.initializer_range				,							encoder_stride=self.encoder_stride				,							)

  def __a						(					self     :							Dict				,							SCREAMING_SNAKE_CASE__     :							Optional[Any]				,							SCREAMING_SNAKE_CASE__     :							Any				,							SCREAMING_SNAKE_CASE__     :							Optional[Any]						):





       '''simple docstring'''

       __a       =   ViTModel(config=SCREAMING_SNAKE_CASE__						)
       model.to(SCREAMING_SNAKE_CASE__						)
       model.eval()
       __a       =   model(SCREAMING_SNAKE_CASE__						)
       self.parent.assertEqual(result.last_hidden_state.shape				,							(self.batch_size, self.seq_length, self.hidden_size)						)

  def __a						(					self     :							str				,							SCREAMING_SNAKE_CASE__     :							List[str]				,							SCREAMING_SNAKE_CASE__     :							Tuple				,							SCREAMING_SNAKE_CASE__     :							str						):





       '''simple docstring'''

       __a       =   ViTForMaskedImageModeling(config=SCREAMING_SNAKE_CASE__						)
       model.to(SCREAMING_SNAKE_CASE__						)
       model.eval()
       __a       =   model(SCREAMING_SNAKE_CASE__						)
       self.parent.assertEqual(
           result.reconstruction.shape				,							(self.batch_size, self.num_channels, self.image_size, self.image_size)						)

       # test greyscale images
       __a       =   1
       __a       =   ViTForMaskedImageModeling(SCREAMING_SNAKE_CASE__						)
       model.to(SCREAMING_SNAKE_CASE__						)
       model.eval()

       __a       =   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
       __a       =   model(SCREAMING_SNAKE_CASE__						)
       self.parent.assertEqual(result.reconstruction.shape				,							(self.batch_size, 1, self.image_size, self.image_size)						)

  def __a						(					self     :							str				,							SCREAMING_SNAKE_CASE__     :							Tuple				,							SCREAMING_SNAKE_CASE__     :							List[Any]				,							SCREAMING_SNAKE_CASE__     :							Dict						):





       '''simple docstring'''

       __a       =   self.type_sequence_label_size
       __a       =   ViTForImageClassification(SCREAMING_SNAKE_CASE__						)
       model.to(SCREAMING_SNAKE_CASE__						)
       model.eval()
       __a       =   model(SCREAMING_SNAKE_CASE__				,							labels=SCREAMING_SNAKE_CASE__						)
       self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.type_sequence_label_size)						)

       # test greyscale images
       __a       =   1
       __a       =   ViTForImageClassification(SCREAMING_SNAKE_CASE__						)
       model.to(SCREAMING_SNAKE_CASE__						)
       model.eval()

       __a       =   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
       __a       =   model(SCREAMING_SNAKE_CASE__						)
       self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.type_sequence_label_size)						)


  def __a						(					self     :							str						):





       '''simple docstring'''

       __a       =   self.prepare_config_and_inputs()
       (
           (
           __a
       ) ,						(
           __a
       ) ,						(
           __a
       ) ,						
       )       =   config_and_inputs
       __a       =   {"""pixel_values""": pixel_values}
       return config, inputs_dict


@require_torch
class  lowerCAmelCase_						(       snake_case__		,    snake_case__		,    unittest.TestCase      ):





  """simple docstring"""



  a_       :str												=(
      (
          ViTModel,
          ViTForImageClassification,
          ViTForMaskedImageModeling,
      )
      if is_torch_available()
      else ()
  )
  a_       :Tuple												=(
      {"""feature-extraction""": ViTModel, """image-classification""": ViTForImageClassification}
      if is_torch_available()
      else {}
  )
  a_       :List[str]												=True

  a_       :str												=False
  a_       :Optional[int]												=False
  a_       :Tuple												=False

  def __a						(					self     :							str						):





       '''simple docstring'''

       __a       =   ViTModelTester(self						)
       __a       =   ConfigTester(self				,							config_class=SCREAMING_SNAKE_CASE__				,							has_text_modality=SCREAMING_SNAKE_CASE__				,							hidden_size=3_7						)

  def __a						(					self     :							Dict						):





       '''simple docstring'''

       self.config_tester.run_common_tests()

  @unittest.skip(reason="""ViT does not use inputs_embeds"""						)
  def __a						(					self     :							List[Any]						):





       '''simple docstring'''

       pass

  def __a						(					self     :							int						):





       '''simple docstring'''

       __a ,						__a       =   self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
            __a       =   model_class(SCREAMING_SNAKE_CASE__						)
            self.assertIsInstance(model.get_input_embeddings()				,							(nn.Module)						)
            __a       =   model.get_output_embeddings()
            self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE__				,							nn.Linear						)						)

  def __a						(					self     :							int						):





       '''simple docstring'''

       __a ,						__a       =   self.model_tester.prepare_config_and_inputs_for_common()

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

            __a       =   ["""pixel_values"""]
            self.assertListEqual(arg_names[:1]				,							SCREAMING_SNAKE_CASE__						)

  def __a						(					self     :							List[str]						):





       '''simple docstring'''

       __a       =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__						)

  def __a						(					self     :							Union[str, Any]						):





       '''simple docstring'''

       __a       =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*SCREAMING_SNAKE_CASE__						)

  def __a						(					self     :							Optional[Any]						):





       '''simple docstring'''

       __a       =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE__						)


  @slow
  def __a						(					self     :							List[str]						):





       '''simple docstring'''

       for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            __a       =   ViTModel.from_pretrained(SCREAMING_SNAKE_CASE__						)
            self.assertIsNotNone(SCREAMING_SNAKE_CASE__						)




def        __lowercase						(      )				->					int:
     """simple docstring"""




     __a       =   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""		)
     return image



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





  """simple docstring"""



  @cached_property
  def __a						(					self     :							str						):





       '''simple docstring'''

       return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224"""						) if is_vision_available() else None

  @slow
  def __a						(					self     :							Dict						):





       '''simple docstring'''

       __a       =   ViTForImageClassification.from_pretrained("""google/vit-base-patch16-224"""						).to(SCREAMING_SNAKE_CASE__						)

       __a       =   self.default_image_processor
       __a       =   prepare_img()
       __a       =   image_processor(images=SCREAMING_SNAKE_CASE__				,							return_tensors="""pt"""						).to(SCREAMING_SNAKE_CASE__						)

       # forward pass
       with torch.no_grad():
            __a       =   model(**SCREAMING_SNAKE_CASE__						)

       # verify the logits
       __a       =   torch.Size((1, 1_0_0_0)						)
       self.assertEqual(outputs.logits.shape				,							SCREAMING_SNAKE_CASE__						)

       __a       =   torch.tensor([-0.2_7_4_4, 0.8_2_1_5, -0.0_8_3_6]						).to(SCREAMING_SNAKE_CASE__						)

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

  @slow
  def __a						(					self     :							int						):





       '''simple docstring'''

       __a       =   ViTModel.from_pretrained("""facebook/dino-vits8"""						).to(SCREAMING_SNAKE_CASE__						)

       __a       =   ViTImageProcessor.from_pretrained("""facebook/dino-vits8"""				,							size=4_8_0						)
       __a       =   prepare_img()
       __a       =   image_processor(images=SCREAMING_SNAKE_CASE__				,							return_tensors="""pt"""						)
       __a       =   inputs.pixel_values.to(SCREAMING_SNAKE_CASE__						)

       # forward pass
       with torch.no_grad():
            __a       =   model(SCREAMING_SNAKE_CASE__				,							interpolate_pos_encoding=SCREAMING_SNAKE_CASE__						)

       # verify the logits
       __a       =   torch.Size((1, 3_6_0_1, 3_8_4)						)
       self.assertEqual(outputs.last_hidden_state.shape				,							SCREAMING_SNAKE_CASE__						)

       __a       =   torch.tensor(
           [[4.2_3_4_0, 4.3_9_0_6, -6.6_6_9_2], [4.5_4_6_3, 1.8_9_2_8, -6.7_2_5_7], [4.4_4_2_9, 0.8_4_9_6, -5.8_5_8_5]]						).to(SCREAMING_SNAKE_CASE__						)

       self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]				,							SCREAMING_SNAKE_CASE__				,							atol=1E-4						)						)


  @slow
  @require_accelerate
  @require_torch_gpu
  def __a						(					self     :							Optional[Any]						):





       '''simple docstring'''

       __a       =   ViTModel.from_pretrained("""facebook/dino-vits8"""				,							torch_dtype=torch.floataa				,							device_map="""auto"""						)
       __a       =   self.default_image_processor

       __a       =   prepare_img()
       __a       =   image_processor(images=SCREAMING_SNAKE_CASE__				,							return_tensors="""pt"""						)
       __a       =   inputs.pixel_values.to(SCREAMING_SNAKE_CASE__						)

       # forward pass to make sure inference works in fp16
       with torch.no_grad():
            __a       =   model(SCREAMING_SNAKE_CASE__						)






