





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


_lowerCamelCase			       =      random.Random()




def    __UpperCAmelCase(      lowercase_ ,    lowercase_=1.0 ,    lowercase_=None ,    lowercase_=None      ):
       if rng is None:
              _lowerCamelCase							:     List[str]	=		global_rng

       _lowerCamelCase							:     int	=		[]
       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 			__A       (      unittest.TestCase  ):




      """simple docstring"""







      def __init__(		self      ,							a__      ,							a__=7      ,							a__=400      ,							a__=2000      ,							a__=1      ,							a__=0.0      ,							a__=1_6000      ,							a__=True      ,							a__=80      ,							a__=16      ,							a__=64      ,							a__="hann_window"      ,							a__=80      ,							a__=7600      ,							a__=1e-10      ,							a__=True      ,							):



             """simple docstring"""





             _lowerCamelCase							:     str	=		parent
             _lowerCamelCase							:     Union[str, Any]	=		batch_size
             _lowerCamelCase							:     int	=		min_seq_length
             _lowerCamelCase							:     int	=		max_seq_length
             _lowerCamelCase							:     int	=		(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
             _lowerCamelCase							:     List[str]	=		feature_size
             _lowerCamelCase							:     Union[str, Any]	=		padding_value
             _lowerCamelCase							:     Dict	=		sampling_rate
             _lowerCamelCase							:     Tuple	=		do_normalize
             _lowerCamelCase							:     List[Any]	=		num_mel_bins
             _lowerCamelCase							:     Optional[Any]	=		hop_length
             _lowerCamelCase							:     Tuple	=		win_length
             _lowerCamelCase							:     Optional[Any]	=		win_function
             _lowerCamelCase							:     List[Any]	=		fmin
             _lowerCamelCase							:     Dict	=		fmax
             _lowerCamelCase							:     Optional[int]	=		mel_floor
             _lowerCamelCase							:     List[str]	=		return_attention_mask


      def 				__snake_case			(		self):



             """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 				__snake_case			(		self      ,							a__=False      ,							a__=False):



             """simple docstring"""





             def _flatten(a__):
                    return list(itertools.chain(*a__))

             if equal_length:
                    _lowerCamelCase							:     Optional[int]	=		floats_list((self.batch_size, self.max_seq_length))
             else:
                    # make sure that inputs increase in size
                    _lowerCamelCase							:     Any	=		[
                        _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:
                    _lowerCamelCase							:     Any	=		[np.asarray(a__) for x in speech_inputs]

             return speech_inputs



      def 				__snake_case			(		self      ,							a__=False      ,							a__=False):



             """simple docstring"""





             if equal_length:
                    _lowerCamelCase							:     Optional[int]	=		[floats_list((self.max_seq_length, self.num_mel_bins)) for _ in range(self.batch_size)]
             else:
                    # make sure that inputs increase in size
                    _lowerCamelCase							:     int	=		[
                        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:
                    _lowerCamelCase							:     int	=		[np.asarray(a__) for x in speech_inputs]

             return speech_inputs







@require_torch
class 			__A       (      lowerCamelCase__ ,unittest.TestCase  ):




      """simple docstring"""







      UpperCAmelCase__						=      SpeechTaFeatureExtractor


      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     Any	=		SpeechTaFeatureExtractionTester(self)


      def 				__snake_case			(		self      ,							a__):



             """simple docstring"""





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


      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     Dict	=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
             # create three inputs of length 800, 1000, and 1200
             _lowerCamelCase							:     str	=		[floats_list((1, x))[0] for x in range(800      ,							1400      ,							200)]
             _lowerCamelCase							:     Any	=		[np.asarray(a__) for speech_input in speech_inputs]

             # Test not batched input
             _lowerCamelCase							:     Dict	=		feat_extract(speech_inputs[0]      ,							return_tensors='''np''').input_values
             _lowerCamelCase							:     Optional[Any]	=		feat_extract(np_speech_inputs[0]      ,							return_tensors='''np''').input_values
             self.assertTrue(np.allclose(a__      ,							a__      ,							atol=1e-3))

             # Test batched
             _lowerCamelCase							:     Union[str, Any]	=		feat_extract(a__      ,							return_tensors='''np''').input_values
             _lowerCamelCase							:     Optional[Any]	=		feat_extract(a__      ,							return_tensors='''np''').input_values
             for enc_seq_a, enc_seq_a in zip(a__      ,							a__):
                    self.assertTrue(np.allclose(a__      ,							a__      ,							atol=1e-3))


      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     Optional[Any]	=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
             _lowerCamelCase							:     Any	=		[floats_list((1, x))[0] for x in range(800      ,							1400      ,							200)]

             _lowerCamelCase							:     Tuple	=		['''longest''', '''max_length''', '''do_not_pad''']
             _lowerCamelCase							:     Optional[int]	=		[None, 1600, None]
             for max_length, padding in zip(a__      ,							a__):
                    _lowerCamelCase							:     List[Any]	=		feat_extract(a__      ,							padding=a__      ,							max_length=a__      ,							return_tensors='''np''')
                    _lowerCamelCase							:     Dict	=		processed.input_values

                    self._check_zero_mean_unit_variance(input_values[0][:800])
                    self.assertTrue(input_values[0][800:].sum() < 1e-6)
                    self._check_zero_mean_unit_variance(input_values[1][:1000])
                    self.assertTrue(input_values[0][1000:].sum() < 1e-6)
                    self._check_zero_mean_unit_variance(input_values[2][:1200])


      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     Optional[Any]	=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
             _lowerCamelCase							:     int	=		range(800      ,							1400      ,							200)
             _lowerCamelCase							:     str	=		[floats_list((1, x))[0] for x in lengths]

             _lowerCamelCase							:     List[str]	=		['''longest''', '''max_length''', '''do_not_pad''']
             _lowerCamelCase							:     Optional[int]	=		[None, 1600, None]

             for max_length, padding in zip(a__      ,							a__):
                    _lowerCamelCase							:     List[str]	=		feat_extract(a__      ,							max_length=a__      ,							padding=a__)
                    _lowerCamelCase							:     Dict	=		processed.input_values

                    self._check_zero_mean_unit_variance(input_values[0][:800])
                    self._check_zero_mean_unit_variance(input_values[1][:1000])
                    self._check_zero_mean_unit_variance(input_values[2][:1200])


      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     Dict	=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
             _lowerCamelCase							:     List[str]	=		[floats_list((1, x))[0] for x in range(800      ,							1400      ,							200)]
             _lowerCamelCase							:     Union[str, Any]	=		feat_extract(
                 a__      ,							truncation=a__      ,							max_length=1000      ,							padding='''max_length'''      ,							return_tensors='''np''')
             _lowerCamelCase							:     str	=		processed.input_values

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


      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     Tuple	=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
             _lowerCamelCase							:     List[Any]	=		[floats_list((1, x))[0] for x in range(800      ,							1400      ,							200)]
             _lowerCamelCase							:     Dict	=		feat_extract(
                 a__      ,							truncation=a__      ,							max_length=1000      ,							padding='''longest'''      ,							return_tensors='''np''')
             _lowerCamelCase							:     str	=		processed.input_values

             self._check_zero_mean_unit_variance(input_values[0, :800])
             self._check_zero_mean_unit_variance(input_values[1, :1000])
             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, 1000))

             _lowerCamelCase							:     List[str]	=		[floats_list((1, x))[0] for x in range(800      ,							1400      ,							200)]
             _lowerCamelCase							:     str	=		feat_extract(
                 a__      ,							truncation=a__      ,							max_length=2000      ,							padding='''longest'''      ,							return_tensors='''np''')
             _lowerCamelCase							:     Optional[int]	=		processed.input_values

             self._check_zero_mean_unit_variance(input_values[0, :800])
             self._check_zero_mean_unit_variance(input_values[1, :1000])
             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, 1200))


      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     Any	=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
             _lowerCamelCase							:     List[str]	=		np.random.rand(100).astype(np.floataa)
             _lowerCamelCase							:     str	=		np_speech_inputs.tolist()

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


      def 				__snake_case			(		self):



             """simple docstring"""





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

             # Test feature size
             _lowerCamelCase							:     Dict	=		feature_extractor(audio_target=a__      ,							padding=a__      ,							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
             _lowerCamelCase							:     Any	=		feature_extractor(speech_inputs[0]      ,							return_tensors='''np''').input_values
             _lowerCamelCase							:     Union[str, Any]	=		feature_extractor(np_speech_inputs[0]      ,							return_tensors='''np''').input_values
             self.assertTrue(np.allclose(a__      ,							a__      ,							atol=1e-3))

             # Test batched
             _lowerCamelCase							:     Optional[Any]	=		feature_extractor(a__      ,							return_tensors='''np''').input_values
             _lowerCamelCase							:     str	=		feature_extractor(a__      ,							return_tensors='''np''').input_values
             for enc_seq_a, enc_seq_a in zip(a__      ,							a__):
                    self.assertTrue(np.allclose(a__      ,							a__      ,							atol=1e-3))

             # Test 2-D numpy arrays are batched.
             _lowerCamelCase							:     Optional[Any]	=		[floats_list((1, x))[0] for x in (800, 800, 800)]
             _lowerCamelCase							:     Optional[Any]	=		np.asarray(a__)
             _lowerCamelCase							:     Union[str, Any]	=		feature_extractor(a__      ,							return_tensors='''np''').input_values
             _lowerCamelCase							:     Optional[Any]	=		feature_extractor(a__      ,							return_tensors='''np''').input_values
             for enc_seq_a, enc_seq_a in zip(a__      ,							a__):
                    self.assertTrue(np.allclose(a__      ,							a__      ,							atol=1e-3))


      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     Optional[Any]	=		self.feat_extract_tester.prepare_inputs_for_target()
             _lowerCamelCase							:     Dict	=		self.feature_extraction_class(**self.feat_extract_dict)
             _lowerCamelCase							:     List[Any]	=		feat_extract.model_input_names[0]

             _lowerCamelCase							:     Union[str, Any]	=		BatchFeature({input_name: speech_inputs})

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

             _lowerCamelCase							:     Tuple	=		self.feat_extract_tester.prepare_inputs_for_target(equal_length=a__)
             _lowerCamelCase							:     Union[str, Any]	=		BatchFeature({input_name: speech_inputs}      ,							tensor_type='''np''')

             _lowerCamelCase							:     List[str]	=		processed_features[input_name]

             if len(batch_features_input.shape) < 3:
                    _lowerCamelCase							:     Dict	=		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 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     int	=		self.feat_extract_tester.prepare_inputs_for_target(equal_length=a__)
             _lowerCamelCase							:     Optional[int]	=		self.feature_extraction_class(**self.feat_extract_dict)
             _lowerCamelCase							:     Any	=		feat_extract.model_input_names[0]

             _lowerCamelCase							:     Tuple	=		BatchFeature({input_name: speech_inputs}      ,							tensor_type='''pt''')

             _lowerCamelCase							:     str	=		processed_features[input_name]

             if len(batch_features_input.shape) < 3:
                    _lowerCamelCase							:     int	=		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 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     Union[str, Any]	=		self.feature_extraction_class(**self.feat_extract_dict)
             _lowerCamelCase							:     int	=		self.feat_extract_tester.prepare_inputs_for_target()
             _lowerCamelCase							:     int	=		feat_extract.model_input_names[0]

             _lowerCamelCase							:     Dict	=		BatchFeature({input_name: speech_inputs})

             _lowerCamelCase							:     List[str]	=		feat_extract.num_mel_bins  # hack!

             _lowerCamelCase							:     List[str]	=		feat_extract.pad(a__      ,							padding='''longest'''      ,							return_tensors='''np''')[input_name]
             _lowerCamelCase							:     Any	=		feat_extract.pad(a__      ,							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 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     Optional[Any]	=		self.feat_extract_dict
             _lowerCamelCase							:     Tuple	=		True
             _lowerCamelCase							:     List[Any]	=		self.feature_extraction_class(**a__)
             _lowerCamelCase							:     Optional[int]	=		self.feat_extract_tester.prepare_inputs_for_target()
             _lowerCamelCase							:     List[str]	=		[len(a__) for x in speech_inputs]
             _lowerCamelCase							:     Dict	=		feat_extract.model_input_names[0]

             _lowerCamelCase							:     List[Any]	=		BatchFeature({input_name: speech_inputs})

             _lowerCamelCase							:     Union[str, Any]	=		feat_extract.num_mel_bins  # hack!

             _lowerCamelCase							:     str	=		feat_extract.pad(a__      ,							padding='''longest'''      ,							return_tensors='''np''')
             self.assertIn('''attention_mask'''      ,							a__)
             self.assertListEqual(list(processed.attention_mask.shape)      ,							list(processed[input_name].shape[:2]))
             self.assertListEqual(processed.attention_mask.sum(-1).tolist()      ,							a__)


      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     int	=		self.feat_extract_dict
             _lowerCamelCase							:     Optional[int]	=		True
             _lowerCamelCase							:     Optional[Any]	=		self.feature_extraction_class(**a__)
             _lowerCamelCase							:     List[str]	=		self.feat_extract_tester.prepare_inputs_for_target()
             _lowerCamelCase							:     str	=		[len(a__) for x in speech_inputs]
             _lowerCamelCase							:     Dict	=		feat_extract.model_input_names[0]

             _lowerCamelCase							:     int	=		BatchFeature({input_name: speech_inputs})
             _lowerCamelCase							:     Tuple	=		min(a__)

             _lowerCamelCase							:     Union[str, Any]	=		feat_extract.num_mel_bins  # hack!

             _lowerCamelCase							:     str	=		feat_extract.pad(
                 a__      ,							padding='''max_length'''      ,							max_length=a__      ,							truncation=a__      ,							return_tensors='''np''')
             self.assertIn('''attention_mask'''      ,							a__)
             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 				__snake_case			(		self      ,							a__):



             """simple docstring"""





             from datasets import load_dataset

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

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


      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     Optional[int]	=		torch.tensor(
                 [2.3804e-03, 2.0752e-03, 1.9836e-03, 2.1057e-03, 1.6174e-03,
                  3.0518e-04, 9.1553e-05, 3.3569e-04, 9.7656e-04, 1.8311e-03,
                  2.0142e-03, 2.1057e-03, 1.7395e-03, 4.5776e-04, -3.9673e-04,
                  4.5776e-04, 1.0071e-03, 9.1553e-05, 4.8828e-04, 1.1597e-03,
                  7.3242e-04, 9.4604e-04, 1.8005e-03, 1.8311e-03, 8.8501e-04,
                  4.2725e-04, 4.8828e-04, 7.3242e-04, 1.0986e-03, 2.1057e-03])
             # fmt: on

             _lowerCamelCase							:     Optional[int]	=		self._load_datasamples(1)
             _lowerCamelCase							:     List[str]	=		SpeechTaFeatureExtractor()
             _lowerCamelCase							:     List[Any]	=		feature_extractor(a__      ,							return_tensors='''pt''').input_values
             self.assertEquals(input_values.shape      ,							(1, 9_3680))
             self.assertTrue(torch.allclose(input_values[0, :30]      ,							a__      ,							atol=1e-6))



      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     List[str]	=		torch.tensor(
                 [-2.6870, -3.0104, -3.1356, -3.5352, -3.0044, -3.0353, -3.4719, -3.6777,
                  -3.1520, -2.9435, -2.6553, -2.8795, -2.9944, -2.5921, -3.0279, -3.0386,
                  -3.0864, -3.1291, -3.2353, -2.7444, -2.6831, -2.7287, -3.1761, -3.1571,
                  -3.2726, -3.0582, -3.1007, -3.4533, -3.4695, -3.0998])
             # fmt: on

             _lowerCamelCase							:     List[Any]	=		self._load_datasamples(1)
             _lowerCamelCase							:     List[str]	=		SpeechTaFeatureExtractor()
             _lowerCamelCase							:     Union[str, Any]	=		feature_extractor(audio_target=a__      ,							return_tensors='''pt''').input_values
             self.assertEquals(input_values.shape      ,							(1, 366, 80))
             self.assertTrue(torch.allclose(input_values[0, 0, :30]      ,							a__      ,							atol=1e-4))








import inspect
import unittest
import warnings
from math import ceil, floor

from transformers import LevitConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.models.auto import get_values
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 (
           MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
           MODEL_MAPPING,
           LevitForImageClassification,
           LevitForImageClassificationWithTeacher,
           LevitModel,
       )
       from transformers.models.levit.modeling_levit import LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
       from PIL import Image

       from transformers import LevitImageProcessor







class 			__A       (      lowerCamelCase__  ):




      """simple docstring"""







      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     Optional[Any]	=		self.config_class(**self.inputs_dict)
             self.parent.assertTrue(hasattr(a__      ,							'''hidden_sizes'''))
             self.parent.assertTrue(hasattr(a__      ,							'''num_attention_heads'''))







class 			__A       :




      """simple docstring"""







      def __init__(		self      ,							a__      ,							a__=13      ,							a__=64      ,							a__=3      ,							a__=3      ,							a__=2      ,							a__=1      ,							a__=16      ,							a__=[128, 256, 384]      ,							a__=[4, 6, 8]      ,							a__=[2, 3, 4]      ,							a__=[16, 16, 16]      ,							a__=0      ,							a__=[2, 2, 2]      ,							a__=[2, 2, 2]      ,							a__=0.02      ,							a__=True      ,							a__=True      ,							a__=2      ,							):



             """simple docstring"""





             _lowerCamelCase							:     Optional[Any]	=		parent
             _lowerCamelCase							:     Union[str, Any]	=		batch_size
             _lowerCamelCase							:     Dict	=		image_size
             _lowerCamelCase							:     Dict	=		num_channels
             _lowerCamelCase							:     Any	=		kernel_size
             _lowerCamelCase							:     int	=		stride
             _lowerCamelCase							:     Dict	=		padding
             _lowerCamelCase							:     Union[str, Any]	=		hidden_sizes
             _lowerCamelCase							:     Optional[Any]	=		num_attention_heads
             _lowerCamelCase							:     Any	=		depths
             _lowerCamelCase							:     List[Any]	=		key_dim
             _lowerCamelCase							:     Optional[Any]	=		drop_path_rate
             _lowerCamelCase							:     Union[str, Any]	=		patch_size
             _lowerCamelCase							:     int	=		attention_ratio
             _lowerCamelCase							:     Any	=		mlp_ratio
             _lowerCamelCase							:     List[str]	=		initializer_range
             _lowerCamelCase							:     Dict	=		[
                 ['''Subsample''', key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2],
                 ['''Subsample''', key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2],
             ]
             _lowerCamelCase							:     str	=		is_training
             _lowerCamelCase							:     Tuple	=		use_labels
             _lowerCamelCase							:     List[Any]	=		num_labels
             _lowerCamelCase							:     Union[str, Any]	=		initializer_range


      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     int	=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

             _lowerCamelCase							:     str	=		None
             if self.use_labels:
                    _lowerCamelCase							:     List[str]	=		ids_tensor([self.batch_size]      ,							self.num_labels)

             _lowerCamelCase							:     List[Any]	=		self.get_config()
             return config, pixel_values, labels


      def 				__snake_case			(		self):



             """simple docstring"""





             return LevitConfig(
                 image_size=self.image_size      ,							num_channels=self.num_channels      ,							kernel_size=self.kernel_size      ,							stride=self.stride      ,							padding=self.padding      ,							patch_size=self.patch_size      ,							hidden_sizes=self.hidden_sizes      ,							num_attention_heads=self.num_attention_heads      ,							depths=self.depths      ,							key_dim=self.key_dim      ,							drop_path_rate=self.drop_path_rate      ,							mlp_ratio=self.mlp_ratio      ,							attention_ratio=self.attention_ratio      ,							initializer_range=self.initializer_range      ,							down_ops=self.down_ops      ,							)


      def 				__snake_case			(		self      ,							a__      ,							a__      ,							a__):



             """simple docstring"""





             _lowerCamelCase							:     Optional[Any]	=		LevitModel(config=a__)
             model.to(a__)
             model.eval()
             _lowerCamelCase							:     List[str]	=		model(a__)
             _lowerCamelCase							:     List[str]	=		(self.image_size, self.image_size)
             _lowerCamelCase,  _lowerCamelCase							:     Optional[int]	=		image_size[0], image_size[1]
             for _ in range(4):
                    _lowerCamelCase							:     int	=		floor(((height + 2 * self.padding - self.kernel_size) / self.stride) + 1)
                    _lowerCamelCase							:     Dict	=		floor(((width + 2 * self.padding - self.kernel_size) / self.stride) + 1)
             self.parent.assertEqual(
                 result.last_hidden_state.shape      ,							(self.batch_size, ceil(height / 4) * ceil(width / 4), self.hidden_sizes[-1])      ,							)


      def 				__snake_case			(		self      ,							a__      ,							a__      ,							a__):



             """simple docstring"""





             _lowerCamelCase							:     Optional[int]	=		self.num_labels
             _lowerCamelCase							:     Dict	=		LevitForImageClassification(a__)
             model.to(a__)
             model.eval()
             _lowerCamelCase							:     List[Any]	=		model(a__      ,							labels=a__)
             self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.num_labels))



      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     int	=		self.prepare_config_and_inputs()
             _lowerCamelCase,  _lowerCamelCase,  _lowerCamelCase							:     Union[str, Any]	=		config_and_inputs
             _lowerCamelCase							:     List[str]	=		{'''pixel_values''': pixel_values}
             return config, inputs_dict







@require_torch
class 			__A       (      lowerCamelCase__ ,lowerCamelCase__ ,unittest.TestCase  ):




      """simple docstring"""







      UpperCAmelCase__						=      (
          (LevitModel, LevitForImageClassification, LevitForImageClassificationWithTeacher)
          if is_torch_available()
          else ()
      )
      UpperCAmelCase__						=      (
          {
              """feature-extraction""": LevitModel,
              """image-classification""": (LevitForImageClassification, LevitForImageClassificationWithTeacher),
          }
          if is_torch_available()
          else {}
      )

      UpperCAmelCase__						=      False
      UpperCAmelCase__						=      False
      UpperCAmelCase__						=      False
      UpperCAmelCase__						=      False
      UpperCAmelCase__						=      False


      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     Union[str, Any]	=		LevitModelTester(self)
             _lowerCamelCase							:     Tuple	=		ConfigTester(self      ,							config_class=a__      ,							has_text_modality=a__      ,							hidden_size=37)


      def 				__snake_case			(		self):



             """simple docstring"""





             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 				__snake_case			(		self):



             """simple docstring"""





             return


      @unittest.skip(reason='''Levit does not use inputs_embeds''')
      def 				__snake_case			(		self):



             """simple docstring"""





             pass


      @unittest.skip(reason='''Levit does not support input and output embeddings''')
      def 				__snake_case			(		self):



             """simple docstring"""





             pass


      @unittest.skip(reason='''Levit does not output attentions''')
      def 				__snake_case			(		self):



             """simple docstring"""





             pass


      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase,  _lowerCamelCase							:     Any	=		self.model_tester.prepare_config_and_inputs_for_common()

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

                    _lowerCamelCase							:     List[Any]	=		['''pixel_values''']
                    self.assertListEqual(arg_names[:1]      ,							a__)


      def 				__snake_case			(		self):



             """simple docstring"""





             def check_hidden_states_output(a__      ,							a__      ,							a__):
                    _lowerCamelCase							:     List[Any]	=		model_class(a__)
                    model.to(a__)
                    model.eval()

                    with torch.no_grad():
                           _lowerCamelCase							:     Any	=		model(**self._prepare_for_class(a__      ,							a__))

                    _lowerCamelCase							:     int	=		outputs.hidden_states

                    _lowerCamelCase							:     List[str]	=		len(self.model_tester.depths) + 1
                    self.assertEqual(len(a__)      ,							a__)

                    _lowerCamelCase							:     Tuple	=		(self.model_tester.image_size, self.model_tester.image_size)
                    _lowerCamelCase,  _lowerCamelCase							:     Tuple	=		image_size[0], image_size[1]
                    for _ in range(4):
                           _lowerCamelCase							:     Optional[Any]	=		floor(
                               (
                                   (height + 2 * self.model_tester.padding - self.model_tester.kernel_size)
                                   / self.model_tester.stride
                               )
                               + 1)
                           _lowerCamelCase							:     List[Any]	=		floor(
                               (
                                   (width + 2 * self.model_tester.padding - self.model_tester.kernel_size)
                                   / self.model_tester.stride
                               )
                               + 1)
                    # verify the first hidden states (first block)
                    self.assertListEqual(
                        list(hidden_states[0].shape[-2:])      ,							[
                            height * width,
                            self.model_tester.hidden_sizes[0],
                        ]      ,							)

             _lowerCamelCase,  _lowerCamelCase							:     Optional[int]	=		self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    _lowerCamelCase							:     List[Any]	=		True
                    check_hidden_states_output(a__      ,							a__      ,							a__)

                    # check that output_hidden_states also work using config
                    del inputs_dict["output_hidden_states"]
                    _lowerCamelCase							:     List[str]	=		True

                    check_hidden_states_output(a__      ,							a__      ,							a__)


      @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''')
      def 				__snake_case			(		self):



             """simple docstring"""





             pass


      def 				__snake_case			(		self      ,							a__      ,							a__      ,							a__=False):



             """simple docstring"""





             _lowerCamelCase							:     Dict	=		super()._prepare_for_class(a__      ,							a__      ,							return_labels=a__)

             if return_labels:
                    if model_class.__name__ == "LevitForImageClassificationWithTeacher":
                           del inputs_dict["labels"]

             return inputs_dict


      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     Tuple	=		self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*a__)


      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     List[str]	=		self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_classification(*a__)


      def 				__snake_case			(		self):



             """simple docstring"""





             if not self.model_tester.is_training:
                    return

             _lowerCamelCase,  _lowerCamelCase							:     List[Any]	=		self.model_tester.prepare_config_and_inputs_for_common()
             _lowerCamelCase							:     Optional[Any]	=		True

             for model_class in self.all_model_classes:
                    # LevitForImageClassificationWithTeacher supports inference-only
                    if (
                        model_class in get_values(a__)
                        or model_class.__name__ == "LevitForImageClassificationWithTeacher"
                    ):
                           continue
                    _lowerCamelCase							:     Optional[Any]	=		model_class(a__)
                    model.to(a__)
                    model.train()
                    _lowerCamelCase							:     Any	=		self._prepare_for_class(a__      ,							a__      ,							return_labels=a__)
                    _lowerCamelCase							:     Tuple	=		model(**a__).loss
                    loss.backward()


      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase,  _lowerCamelCase							:     Union[str, Any]	=		self.model_tester.prepare_config_and_inputs_for_common()
             if not self.model_tester.is_training:
                    return

             _lowerCamelCase							:     Union[str, Any]	=		False
             _lowerCamelCase							:     List[str]	=		True

             for model_class in self.all_model_classes:
                    if model_class in get_values(a__) or not model_class.supports_gradient_checkpointing:
                           continue
                    # LevitForImageClassificationWithTeacher supports inference-only
                    if model_class.__name__ == "LevitForImageClassificationWithTeacher":
                           continue
                    _lowerCamelCase							:     List[Any]	=		model_class(a__)
                    model.gradient_checkpointing_enable()
                    model.to(a__)
                    model.train()
                    _lowerCamelCase							:     int	=		self._prepare_for_class(a__      ,							a__      ,							return_labels=a__)
                    _lowerCamelCase							:     Any	=		model(**a__).loss
                    loss.backward()


      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase,  _lowerCamelCase							:     Tuple	=		self.model_tester.prepare_config_and_inputs_for_common()

             _lowerCamelCase							:     Dict	=		[
                 {'''title''': '''multi_label_classification''', '''num_labels''': 2, '''dtype''': torch.float},
                 {'''title''': '''single_label_classification''', '''num_labels''': 1, '''dtype''': torch.long},
                 {'''title''': '''regression''', '''num_labels''': 1, '''dtype''': torch.float},
             ]

             for model_class in self.all_model_classes:
                    if (
                        model_class
                        not in [
                            *get_values(a__),
                        ]
                        or model_class.__name__ == "LevitForImageClassificationWithTeacher"
                    ):
                           continue

                    for problem_type in problem_types:
                           with self.subTest(msg=F"""Testing {model_class} with {problem_type['title']}"""):
                                  _lowerCamelCase							:     Tuple	=		problem_type['''title''']
                                  _lowerCamelCase							:     Tuple	=		problem_type['''num_labels''']

                                  _lowerCamelCase							:     int	=		model_class(a__)
                                  model.to(a__)
                                  model.train()

                                  _lowerCamelCase							:     int	=		self._prepare_for_class(a__      ,							a__      ,							return_labels=a__)

                                  if problem_type["num_labels"] > 1:
                                         _lowerCamelCase							:     int	=		inputs['''labels'''].unsqueeze(1).repeat(1      ,							problem_type['''num_labels'''])

                                  _lowerCamelCase							:     Tuple	=		inputs['''labels'''].to(problem_type['''dtype'''])

                                  # This tests that we do not trigger the warning form PyTorch "Using a target size that is different
                                  # to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
                                  # they have the same size." which is a symptom something in wrong for the regression problem.
                                  # See https://github.com/huggingface/transformers/issues/11780
                                  with warnings.catch_warnings(record=a__) as warning_list:
                                         _lowerCamelCase							:     Any	=		model(**a__).loss
                                  for w in warning_list:
                                         if "Using a target size that is different to the input size" in str(w.message):
                                                raise ValueError(
                                                    F"""Something is going wrong in the regression problem: intercepted {w.message}""")

                                  loss.backward()



      @slow
      def 				__snake_case			(		self):



             """simple docstring"""





             for model_name in LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    _lowerCamelCase							:     int	=		LevitModel.from_pretrained(a__)
                    self.assertIsNotNone(a__)




def    __UpperCAmelCase(      ):
       _lowerCamelCase							:     int	=		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
       return image







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




      """simple docstring"""







      @cached_property
      def 				__snake_case			(		self):



             """simple docstring"""





             return LevitImageProcessor.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0])



      @slow
      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     Any	=		LevitForImageClassificationWithTeacher.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]).to(
                 a__)

             _lowerCamelCase							:     int	=		self.default_image_processor
             _lowerCamelCase							:     int	=		prepare_img()
             _lowerCamelCase							:     str	=		image_processor(images=a__      ,							return_tensors='''pt''').to(a__)

             # forward pass
             with torch.no_grad():
                    _lowerCamelCase							:     int	=		model(**a__)

             # verify the logits
             _lowerCamelCase							:     List[str]	=		torch.Size((1, 1000))
             self.assertEqual(outputs.logits.shape      ,							a__)

             _lowerCamelCase							:     Optional[Any]	=		torch.tensor([1.0448, -0.3745, -1.8317]).to(a__)

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

