


"""simple docstring"""







import inspect
import unittest

from transformers import ViTHybridConfig
from transformers.testing_utils import require_accelerate, require_torch, 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, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
       import torch
       from torch import nn

       from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel
       from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
       from PIL import Image






class 				__A    :






 """simple docstring"""




 def __init__(		self			,   __A			,   __A=13			,   __A=64			,   __A=2			,   __A=3			,   __A=True			,   __A=True			,   __A=32			,   __A=5			,   __A=4			,   __A=37			,   __A="gelu"			,   __A=0.1			,   __A=0.1			,   __A=10			,   __A=0.02			,   __A=[1, 16, 4, 4]			,   __A=None			,   ) ->					Dict:
     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					=backbone_featmap_shape

     # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
     # the number of patches is based on the feature map of the backbone, which by default uses an output stride
     # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size
     a					=(self.image_size // 32) ** 2
     a					=num_patches + 1


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Union[str, Any]:
     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 				SCREAMING_SNAKE_CASE							(		self						) ->					Optional[Any]:
     a					={
         '''global_padding''': '''same''',
         '''layer_type''': '''bottleneck''',
         '''depths''': [3, 4, 9],
         '''out_features''': ['''stage1''', '''stage2''', '''stage3'''],
         '''embedding_dynamic_padding''': True,
         '''hidden_sizes''': [4, 8, 16, 32],
         '''num_groups''': 2,
     }

     return ViTHybridConfig(
         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=__A			,   initializer_range=self.initializer_range			,   backbone_featmap_shape=self.backbone_featmap_shape			,   backbone_config=__A			,   )


 def 				SCREAMING_SNAKE_CASE							(		self			,   __A			,   __A			,   __A						) ->					int:
     a					=ViTHybridModel(config=__A						)
     model.to(__A						)
     model.eval()
     a					=model(__A						)
     self.parent.assertEqual(result.last_hidden_state.shape			,   (self.batch_size, self.seq_length, self.hidden_size)						)


 def 				SCREAMING_SNAKE_CASE							(		self			,   __A			,   __A			,   __A						) ->					int:
     a					=self.type_sequence_label_size
     a					=ViTHybridForImageClassification(__A						)
     model.to(__A						)
     model.eval()
     a					=model(__A			,   labels=__A						)
     self.parent.assertEqual(result.logits.shape			,   (self.batch_size, self.type_sequence_label_size)						)





 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Union[str, Any]:
     a					=self.prepare_config_and_inputs()
     a		,				a		,				a					=config_and_inputs
     a					={'''pixel_values''': pixel_values}
     return config, inputs_dict






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






 """simple docstring"""




 __lowerCAmelCase			 =  (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
 __lowerCAmelCase			 =  (
     {"feature-extraction": ViTHybridModel, "image-classification": ViTHybridForImageClassification}
     if is_torch_available()
     else {}
 )
 __lowerCAmelCase			 =  False
 __lowerCAmelCase			 =  False
 __lowerCAmelCase			 =  False


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Tuple:
     a					=ViTHybridModelTester(self						)
     a					=ConfigTester(self			,   config_class=__A			,   has_text_modality=__A			,   hidden_size=37						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					int:
     self.config_tester.run_common_tests()


 @unittest.skip(reason='''ViT does not use inputs_embeds'''						)
 def 				SCREAMING_SNAKE_CASE							(		self						) ->					List[Any]:
     pass


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Union[str, Any]:
     a		,				a					=self.model_tester.prepare_config_and_inputs_for_common()

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


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					int:
     a		,				a					=self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         a					=model_class(__A						)
         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]			,   __A						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					int:
     a					=self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__A						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Any:
     a					=self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*__A						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Optional[int]:
     a		,				a					=self.model_tester.prepare_config_and_inputs_for_common()

     a					=_config_zero_init(__A						)
     for model_class in self.all_model_classes:
         a					=model_class(config=__A						)
         # Skip the check for the backbone
         for name, module in model.named_modules():
             if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
                 a					=[f'''{name}.{key}''' for key in module.state_dict().keys()]
                 break

         for name, param in model.named_parameters():
             if param.requires_grad:
                 if name in backbone_params:
                     continue
                 self.assertIn(
                     ((param.data.mean() * 1E9).round() / 1E9).item()			,   [0.0, 1.0]			,   msg=f'''Parameter {name} of model {model_class} seems not properly initialized'''			,   )





 @slow
 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Any:
     for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         a					=ViTHybridModel.from_pretrained(__A						)
         self.assertIsNotNone(__A						)

def _A						(						):







    """simple docstring"""
    a					=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
    return image






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






 """simple docstring"""




 @cached_property
 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Optional[Any]:
     return (
         ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]						)
         if is_vision_available()
         else None
     )


 @slow
 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Optional[Any]:
     a					=ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]						).to(
         __A						)

     a					=self.default_image_processor
     a					=prepare_img()
     a					=image_processor(images=__A			,   return_tensors='''pt'''						).to(__A						)

     # forward pass
     with torch.no_grad():
         a					=model(**__A						)

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

     a					=torch.tensor([-1.9_090, -0.4_993, -0.2_389]						).to(__A						)

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





 @slow
 @require_accelerate
 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Union[str, Any]:
     a					=ViTHybridImageProcessor.from_pretrained('''google/vit-hybrid-base-bit-384'''						)
     a					=ViTHybridForImageClassification.from_pretrained('''google/vit-hybrid-base-bit-384'''			,   device_map='''auto'''						)

     a					=prepare_img()

     a					=image_processor(images=__A			,   return_tensors='''pt'''						)
     a					=model(**__A						)
     a					=outputs.logits
     # model predicts one of the 1000 ImageNet classes
     a					=logits.argmax(-1						).item()

     self.assertTrue(model.config.idalabel[predicted_class_idx]			,   '''tabby, tabby cat'''						)



"""simple docstring"""







import itertools
import os
import random
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers import is_speech_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_speech_available():
       from transformers import WhisperFeatureExtractor

if is_torch_available():
       import torch

lowerCamelCase_       :       Any      						=  random.Random()
def _A						(						lowercase	,   lowercase=1.0	,   lowercase=None	,   lowercase=None		):







    """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
@require_torchaudio
class 				__A    (	unittest.TestCase      ):






 """simple docstring"""




 def __init__(		self			,   __A			,   __A=7			,   __A=400			,   __A=2000			,   __A=10			,   __A=160			,   __A=8			,   __A=0.0			,   __A=4000			,   __A=False			,   __A=True			,   ) ->					Optional[Any]:
     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					=padding_value
     a					=sampling_rate
     a					=return_attention_mask
     a					=do_normalize
     a					=feature_size
     a					=chunk_length
     a					=hop_length


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					str:
     return {
         "feature_size": self.feature_size,
         "hop_length": self.hop_length,
         "chunk_length": self.chunk_length,
         "padding_value": self.padding_value,
         "sampling_rate": self.sampling_rate,
         "return_attention_mask": self.return_attention_mask,
         "do_normalize": self.do_normalize,
     }





 def 				SCREAMING_SNAKE_CASE							(		self			,   __A=False			,   __A=False						) ->					str:
     def _flatten(__A						):
         return list(itertools.chain(*__A						)						)

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






@require_torch
@require_torchaudio
class 				__A    (	_SCREAMING_SNAKE_CASE,   unittest.TestCase      ):






 """simple docstring"""




 __lowerCAmelCase			 =  WhisperFeatureExtractor if is_speech_available() else None


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Dict:
     a					=WhisperFeatureExtractionTester(self						)


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

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

     a					=feat_extract_first.to_dict()
     a					=feat_extract_second.to_dict()
     a					=feat_extract_first.mel_filters
     a					=feat_extract_second.mel_filters
     self.assertTrue(np.allclose(__A			,   __A						)						)
     self.assertEqual(__A			,   __A						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					str:
     a					=self.feature_extraction_class(**self.feat_extract_dict						)

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

     a					=feat_extract_first.to_dict()
     a					=feat_extract_second.to_dict()
     a					=feat_extract_first.mel_filters
     a					=feat_extract_second.mel_filters
     self.assertTrue(np.allclose(__A			,   __A						)						)
     self.assertEqual(__A			,   __A						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Optional[int]:
     # Tests that all call wrap to encode_plus and batch_encode_plus
     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(800			,   1400			,   200						)]
     a					=[np.asarray(__A						) for speech_input in speech_inputs]

     # Test feature size
     a					=feature_extractor(__A			,   padding='''max_length'''			,   return_tensors='''np'''						).input_features
     self.assertTrue(input_features.ndim == 3						)
     self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames						)
     self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size						)

     # Test not batched input
     a					=feature_extractor(speech_inputs[0]			,   return_tensors='''np'''						).input_features
     a					=feature_extractor(np_speech_inputs[0]			,   return_tensors='''np'''						).input_features
     self.assertTrue(np.allclose(__A			,   __A			,   atol=1E-3						)						)

     # Test batched
     a					=feature_extractor(__A			,   return_tensors='''np'''						).input_features
     a					=feature_extractor(__A			,   return_tensors='''np'''						).input_features
     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.
     a					=[floats_list((1, x)						)[0] for x in (800, 800, 800)]
     a					=np.asarray(__A						)
     a					=feature_extractor(__A			,   return_tensors='''np'''						).input_features
     a					=feature_extractor(__A			,   return_tensors='''np'''						).input_features
     for enc_seq_a, enc_seq_a in zip(__A			,   __A						):
         self.assertTrue(np.allclose(__A			,   __A			,   atol=1E-3						)						)

     # Test truncation required
     a					=[floats_list((1, x)						)[0] for x in range(200			,   (feature_extractor.n_samples + 500)			,   200						)]
     a					=[np.asarray(__A						) for speech_input in speech_inputs]

     a					=[x[: feature_extractor.n_samples] for x in speech_inputs]
     a					=[np.asarray(__A						) for speech_input in speech_inputs_truncated]

     a					=feature_extractor(__A			,   return_tensors='''np'''						).input_features
     a					=feature_extractor(__A			,   return_tensors='''np'''						).input_features
     for enc_seq_a, enc_seq_a in zip(__A			,   __A						):
         self.assertTrue(np.allclose(__A			,   __A			,   atol=1E-3						)						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Tuple:
     import torch

     a					=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
     a					=np.random.rand(100			,   32						).astype(np.floataa						)
     a					=np_speech_inputs.tolist()

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


 def 				SCREAMING_SNAKE_CASE							(		self			,   __A						) ->					Dict:
     a					=load_dataset('''hf-internal-testing/librispeech_asr_dummy'''			,   '''clean'''			,   split='''validation'''						)
     # automatic decoding with librispeech
     a					=ds.sort('''id'''						).select(range(__A						)						)[:num_samples]['''audio''']

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


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Any:
     # fmt: off
     a					=torch.tensor(
         [
             0.1_193, -0.0_946, -0.1_098, -0.0_196, 0.0_225, -0.0_690, -0.1_736, 0.0_951,
             0.0_971, -0.0_817, -0.0_702, 0.0_162, 0.0_260, 0.0_017, -0.0_192, -0.1_678,
             0.0_709, -0.1_867, -0.0_655, -0.0_274, -0.0_234, -0.1_884, -0.0_516, -0.0_554,
             -0.0_274, -0.1_425, -0.1_423, 0.0_837, 0.0_377, -0.0_854
         ]						)
     # fmt: on

     a					=self._load_datasamples(1						)
     a					=WhisperFeatureExtractor()
     a					=feature_extractor(__A			,   return_tensors='''pt'''						).input_features
     self.assertEqual(input_features.shape			,   (1, 80, 3000)						)
     self.assertTrue(torch.allclose(input_features[0, 0, :30]			,   __A			,   atol=1E-4						)						)





 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Union[str, Any]:
     a					=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
     a					=self._load_datasamples(1						)[0]
     a					=((audio - audio.min()) / (audio.max() - audio.min())) * 6_5535  # Rescale to [0, 65535] to show issue
     a					=feat_extract.zero_mean_unit_var_norm([audio]			,   attention_mask=__A						)[0]

     self.assertTrue(np.all(np.mean(__A						) < 1E-3						)						)
     self.assertTrue(np.all(np.abs(np.var(__A						) - 1						) < 1E-3						)						)