




'''simple docstring'''

from __future__ import annotations

import inspect
import unittest
from math import floor

import numpy as np

from transformers import CvtConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
							import tensorflow as tf

							from transformers import TFCvtForImageClassification, TFCvtModel
							from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor






class 						a				(   _lowerCamelCase     ):


				def   A_ (		self    :					str					):
									snake_case_									=				self.config_class(**self.inputs_dict					)
									self.parent.assertTrue(hasattr(lowercase_       ,				'''embed_dim'''					)					)
									self.parent.assertTrue(hasattr(lowercase_       ,				'''num_heads'''					)					)







class 						a				:


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


				def   A_ (		self    :					List[Any]					):
									snake_case_									=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

									snake_case_									=				None
									if self.use_labels:
														# create a random int32 tensor of given shape
														snake_case_									=				ids_tensor([self.batch_size]       ,				self.num_labels					)

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


				def   A_ (		self    :					Tuple					):
									return CvtConfig(
									    image_size=self.image_size       ,				num_labels=self.num_labels       ,				num_channels=self.num_channels       ,				embed_dim=self.embed_dim       ,				num_heads=self.num_heads       ,				patch_sizes=self.patch_sizes       ,				patch_padding=self.patch_padding       ,				patch_stride=self.patch_stride       ,				stride_kv=self.stride_kv       ,				depth=self.depth       ,				cls_token=self.cls_token       ,				attention_drop_rate=self.attention_drop_rate       ,				initializer_range=self.initializer_range       ,				)


				def   A_ (		self    :					int       ,				lowercase_    :					Union[str, Any]       ,				lowercase_    :					Any       ,				lowercase_    :					Any					):
									snake_case_									=				TFCvtModel(config=lowercase_					)
									snake_case_									=				model(lowercase_       ,				training=lowercase_					)
									snake_case_									=				(self.image_size, self.image_size)
									snake_case_			,snake_case_									=				image_size[0], image_size[1]
									for i in range(len(self.depth					)					):
														snake_case_									=				floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1					)
														snake_case_									=				floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1					)
									self.parent.assertEqual(result.last_hidden_state.shape       ,				(self.batch_size, self.embed_dim[-1], height, width)					)


				def   A_ (		self    :					Any       ,				lowercase_    :					Dict       ,				lowercase_    :					Optional[Any]       ,				lowercase_    :					Optional[Any]					):
									snake_case_									=				self.num_labels
									snake_case_									=				TFCvtForImageClassification(lowercase_					)
									snake_case_									=				model(lowercase_       ,				labels=lowercase_       ,				training=lowercase_					)
									self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.num_labels)					)


				def   A_ (		self    :					str					):
									snake_case_									=				self.prepare_config_and_inputs()
									snake_case_			,snake_case_			,snake_case_									=				config_and_inputs
									snake_case_									=				{'''pixel_values''': pixel_values}
									return config, inputs_dict







@require_tf
class 						a				(   _lowerCamelCase   ,     _lowerCamelCase   ,     unittest.TestCase     ):
				snake_case_      =		(TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
				snake_case_      =		(
				    {"feature-extraction": TFCvtModel, "image-classification": TFCvtForImageClassification}
				    if is_tf_available()
				    else {}
				)
				snake_case_      =		False
				snake_case_      =		False
				snake_case_      =		False
				snake_case_      =		False
				snake_case_      =		False


				def   A_ (		self    :					Dict					):
									snake_case_									=				TFCvtModelTester(self					)
									snake_case_									=				TFCvtConfigTester(self       ,				config_class=lowercase_       ,				has_text_modality=lowercase_       ,				hidden_size=37					)


				def   A_ (		self    :					List[Any]					):
									self.config_tester.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()


				@unittest.skip(reason='''Cvt does not output attentions'''					)
				def   A_ (		self    :					Any					):
									pass


				@unittest.skip(reason='''Cvt does not use inputs_embeds'''					)
				def   A_ (		self    :					Any					):
									pass


				@unittest.skip(reason='''Cvt does not support input and output embeddings'''					)
				def   A_ (		self    :					int					):
									pass


				@unittest.skipIf(
				    not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''					)					) == 0       ,				reason='''TF does not support backprop for grouped convolutions on CPU.'''       ,				)
				def   A_ (		self    :					List[Any]					):
									super().test_dataset_conversion()


				@unittest.skipIf(
				    not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''					)					) == 0       ,				reason='''TF does not support backprop for grouped convolutions on CPU.'''       ,				)
				@slow
				def   A_ (		self    :					Union[str, Any]					):
									super().test_keras_fit()


				@unittest.skip(reason='''Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8'''					)
				def   A_ (		self    :					Any					):
									snake_case_									=				tf.keras.mixed_precision.Policy('''mixed_float16'''					)
									tf.keras.mixed_precision.set_global_policy(lowercase_					)
									super().test_keras_fit()
									tf.keras.mixed_precision.set_global_policy('''float32'''					)


				def   A_ (		self    :					Tuple					):
									snake_case_			,snake_case_									=				self.model_tester.prepare_config_and_inputs_for_common()

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

														snake_case_									=				['''pixel_values''']
														self.assertListEqual(arg_names[:1]       ,				lowercase_					)


				def   A_ (		self    :					List[str]					):
									def check_hidden_states_output(lowercase_    :					Any       ,				lowercase_    :					Tuple       ,				lowercase_    :					int					):
														snake_case_									=				model_class(lowercase_					)

														snake_case_									=				model(**self._prepare_for_class(lowercase_       ,				lowercase_					)					)
														snake_case_									=				outputs.hidden_states

														snake_case_									=				len(self.model_tester.depth					)
														self.assertEqual(len(lowercase_					)       ,				lowercase_					)

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

									snake_case_			,snake_case_									=				self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
														snake_case_									=				True
														check_hidden_states_output(lowercase_       ,				lowercase_       ,				lowercase_					)

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

														check_hidden_states_output(lowercase_       ,				lowercase_       ,				lowercase_					)


				def   A_ (		self    :					str					):
									snake_case_									=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowercase_					)


				def   A_ (		self    :					List[str]					):
									snake_case_									=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*lowercase_					)


				@slow
				def   A_ (		self    :					Optional[int]					):
									for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														snake_case_									=				TFCvtModel.from_pretrained(lowercase_					)
														self.assertIsNotNone(lowercase_					)







def 		__magic_name__    (	)		->					List[str]:

					'''simple docstring'''

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






@require_tf
@require_vision
class 						a				(   unittest.TestCase     ):


				@cached_property
				def   A_ (		self    :					Any					):
									return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]					)


				@slow
				def   A_ (		self    :					Optional[Any]					):
									snake_case_									=				TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]					)

									snake_case_									=				self.default_image_processor
									snake_case_									=				prepare_img()
									snake_case_									=				image_processor(images=lowercase_       ,				return_tensors='''tf'''					)

									# forward pass
									snake_case_									=				model(**lowercase_					)

									# verify the logits
									snake_case_									=				tf.TensorShape((1, 1000)					)
									self.assertEqual(outputs.logits.shape       ,				lowercase_					)

									snake_case_									=				tf.constant([0.9285, 0.9015, -0.3150]					)
									self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()       ,				lowercase_       ,				atol=1e-4					)					)







'''simple docstring'''

from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


a  :				str									=  logging.get_logger(__name__)

a  :				str									=  {
    'google/bigbird-roberta-base': 'https://huggingface.co/google/bigbird-roberta-base/resolve/main/config.json',
    'google/bigbird-roberta-large': 'https://huggingface.co/google/bigbird-roberta-large/resolve/main/config.json',
    'google/bigbird-base-trivia-itc': 'https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/config.json',
    # See all BigBird models at https://huggingface.co/models?filter=big_bird
}






class 						a				(   _lowerCamelCase     ):
				snake_case_      =		"big_bird"


				def __init__(		self    :					Union[str, Any]       ,				lowercase_    :					List[Any]=5_0358       ,				lowercase_    :					Tuple=768       ,				lowercase_    :					Dict=12       ,				lowercase_    :					str=12       ,				lowercase_    :					Tuple=3072       ,				lowercase_    :					Any="gelu_new"       ,				lowercase_    :					Optional[Any]=0.1       ,				lowercase_    :					List[Any]=0.1       ,				lowercase_    :					List[Any]=4096       ,				lowercase_    :					List[Any]=2       ,				lowercase_    :					List[str]=0.02       ,				lowercase_    :					Optional[int]=1e-12       ,				lowercase_    :					Tuple=True       ,				lowercase_    :					Tuple=0       ,				lowercase_    :					str=1       ,				lowercase_    :					Union[str, Any]=2       ,				lowercase_    :					Optional[Any]=66       ,				lowercase_    :					Optional[int]="block_sparse"       ,				lowercase_    :					Any=True       ,				lowercase_    :					List[str]=False       ,				lowercase_    :					Any=64       ,				lowercase_    :					Tuple=3       ,				lowercase_    :					Tuple=None       ,				**lowercase_    :					Tuple       ,				):
									super().__init__(
									    pad_token_id=lowercase_       ,				bos_token_id=lowercase_       ,				eos_token_id=lowercase_       ,				sep_token_id=lowercase_       ,				**lowercase_       ,				)

									snake_case_									=				vocab_size
									snake_case_									=				max_position_embeddings
									snake_case_									=				hidden_size
									snake_case_									=				num_hidden_layers
									snake_case_									=				num_attention_heads
									snake_case_									=				intermediate_size
									snake_case_									=				hidden_act
									snake_case_									=				hidden_dropout_prob
									snake_case_									=				attention_probs_dropout_prob
									snake_case_									=				initializer_range
									snake_case_									=				type_vocab_size
									snake_case_									=				layer_norm_eps
									snake_case_									=				use_cache

									snake_case_									=				rescale_embeddings
									snake_case_									=				attention_type
									snake_case_									=				use_bias
									snake_case_									=				block_size
									snake_case_									=				num_random_blocks
									snake_case_									=				classifier_dropout







class 						a				(   _lowerCamelCase     ):


				@property
				def   A_ (		self    :					str					):
									if self.task == "multiple-choice":
														snake_case_									=				{0: '''batch''', 1: '''choice''', 2: '''sequence'''}
									else:
														snake_case_									=				{0: '''batch''', 1: '''sequence'''}
									return OrderedDict(
									    [
									        ('''input_ids''', dynamic_axis),
									        ('''attention_mask''', dynamic_axis),
									    ]					)

