


"""simple docstring"""







import inspect
import unittest

from transformers import YolosConfig
from transformers.testing_utils import 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, floats_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch
		from torch import nn

		from transformers import YolosForObjectDetection, YolosModel
		from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import AutoImageProcessor




class 			UpperCamelCase_     :

	"""simple docstring"""





	def __init__(			self      :    Any      ,      UpperCAmelCase__      :    Dict      ,      UpperCAmelCase__      :    List[str]=1_3      ,      UpperCAmelCase__      :    List[str]=[3_0, 3_0]      ,      UpperCAmelCase__      :    Any=2      ,      UpperCAmelCase__      :    Optional[int]=3      ,      UpperCAmelCase__      :    str=True      ,      UpperCAmelCase__      :    Tuple=True      ,      UpperCAmelCase__      :    str=3_2      ,      UpperCAmelCase__      :    Dict=5      ,      UpperCAmelCase__      :    Any=4      ,      UpperCAmelCase__      :    Union[str, Any]=3_7      ,      UpperCAmelCase__      :    str="gelu"      ,      UpperCAmelCase__      :    int=0.1      ,      UpperCAmelCase__      :    List[str]=0.1      ,      UpperCAmelCase__      :    Any=1_0      ,      UpperCAmelCase__      :    Optional[Any]=0.02      ,      UpperCAmelCase__      :    Any=3      ,      UpperCAmelCase__      :    int=None      ,      UpperCAmelCase__      :    Optional[Any]=8      ,      UpperCAmelCase__      :    str=1_0      ,      )	->     Optional[Any]:
						__SCREAMING_SNAKE_CASE						       =    parent
						__SCREAMING_SNAKE_CASE						       =    batch_size
						__SCREAMING_SNAKE_CASE						       =    image_size
						__SCREAMING_SNAKE_CASE						       =    patch_size
						__SCREAMING_SNAKE_CASE						       =    num_channels
						__SCREAMING_SNAKE_CASE						       =    is_training
						__SCREAMING_SNAKE_CASE						       =    use_labels
						__SCREAMING_SNAKE_CASE						       =    hidden_size
						__SCREAMING_SNAKE_CASE						       =    num_hidden_layers
						__SCREAMING_SNAKE_CASE						       =    num_attention_heads
						__SCREAMING_SNAKE_CASE						       =    intermediate_size
						__SCREAMING_SNAKE_CASE						       =    hidden_act
						__SCREAMING_SNAKE_CASE						       =    hidden_dropout_prob
						__SCREAMING_SNAKE_CASE						       =    attention_probs_dropout_prob
						__SCREAMING_SNAKE_CASE						       =    type_sequence_label_size
						__SCREAMING_SNAKE_CASE						       =    initializer_range
						__SCREAMING_SNAKE_CASE						       =    num_labels
						__SCREAMING_SNAKE_CASE						       =    scope
						__SCREAMING_SNAKE_CASE						       =    n_targets
						__SCREAMING_SNAKE_CASE						       =    num_detection_tokens
						# we set the expected sequence length (which is used in several tests)
						# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens
						__SCREAMING_SNAKE_CASE						       =    (image_size[1] // patch_size) * (image_size[0] // patch_size)
						__SCREAMING_SNAKE_CASE						       =    num_patches + 1 + self.num_detection_tokens




	def   UpperCAmelCase_				(			self      :    Any       )	->     Union[str, Any]:
						__SCREAMING_SNAKE_CASE						       =    floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]       )

						__SCREAMING_SNAKE_CASE						       =    None
						if self.use_labels:
											# labels is a list of Dict (each Dict being the labels for a given example in the batch)
											__SCREAMING_SNAKE_CASE						       =    []
											for i in range(self.batch_size       ):
																__SCREAMING_SNAKE_CASE						       =    {}
																__SCREAMING_SNAKE_CASE						       =    torch.randint(
																    high=self.num_labels      ,      size=(self.n_targets,)      ,      device=UpperCAmelCase__       )
																__SCREAMING_SNAKE_CASE						       =    torch.rand(self.n_targets      ,      4      ,      device=UpperCAmelCase__       )
																labels.append(UpperCAmelCase__       )

						__SCREAMING_SNAKE_CASE						       =    self.get_config()

						return config, pixel_values, labels




	def   UpperCAmelCase_				(			self      :    Optional[Any]       )	->     Dict:
						return YolosConfig(
						    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=UpperCAmelCase__      ,      initializer_range=self.initializer_range      ,      num_detection_tokens=self.num_detection_tokens      ,      num_labels=self.num_labels      ,      )




	def   UpperCAmelCase_				(			self      :    int      ,      UpperCAmelCase__      :    Tuple      ,      UpperCAmelCase__      :    List[Any]      ,      UpperCAmelCase__      :    Tuple       )	->     Optional[Any]:
						__SCREAMING_SNAKE_CASE						       =    YolosModel(config=UpperCAmelCase__       )
						model.to(UpperCAmelCase__       )
						model.eval()
						__SCREAMING_SNAKE_CASE						       =    model(UpperCAmelCase__       )
						self.parent.assertEqual(
						    result.last_hidden_state.shape      ,      (self.batch_size, self.expected_seq_len, self.hidden_size)       )




	def   UpperCAmelCase_				(			self      :    Any      ,      UpperCAmelCase__      :    str      ,      UpperCAmelCase__      :    Union[str, Any]      ,      UpperCAmelCase__      :    int       )	->     Union[str, Any]:
						__SCREAMING_SNAKE_CASE						       =    YolosForObjectDetection(UpperCAmelCase__       )
						model.to(UpperCAmelCase__       )
						model.eval()

						__SCREAMING_SNAKE_CASE						       =    model(pixel_values=UpperCAmelCase__       )
						__SCREAMING_SNAKE_CASE						       =    model(UpperCAmelCase__       )

						self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.num_detection_tokens, self.num_labels + 1)       )
						self.parent.assertEqual(result.pred_boxes.shape      ,      (self.batch_size, self.num_detection_tokens, 4)       )

						__SCREAMING_SNAKE_CASE						       =    model(pixel_values=UpperCAmelCase__      ,      labels=UpperCAmelCase__       )

						self.parent.assertEqual(result.loss.shape      ,      ()       )
						self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.num_detection_tokens, self.num_labels + 1)       )
						self.parent.assertEqual(result.pred_boxes.shape      ,      (self.batch_size, self.num_detection_tokens, 4)       )




	def   UpperCAmelCase_				(			self      :    List[str]       )	->     Tuple:
						__SCREAMING_SNAKE_CASE						       =    self.prepare_config_and_inputs()
						__SCREAMING_SNAKE_CASE       ,  __SCREAMING_SNAKE_CASE       ,  __SCREAMING_SNAKE_CASE						       =    config_and_inputs
						__SCREAMING_SNAKE_CASE						       =    {"pixel_values": pixel_values}
						return config, inputs_dict




@require_torch
class 			UpperCamelCase_     (			UpperCamelCase      ,  UpperCamelCase      ,  unittest.TestCase):

	"""simple docstring"""





	snake_case__    :  int    =      (YolosModel, YolosForObjectDetection) if is_torch_available() else ()
	snake_case__    :  List[str]    =      (
	    {"feature-extraction": YolosModel, "object-detection": YolosForObjectDetection} if is_torch_available() else {}
	)

	snake_case__    :  Any    =      False
	snake_case__    :  Optional[int]    =      False
	snake_case__    :  List[Any]    =      False
	snake_case__    :  str    =      False




	def   UpperCAmelCase_				(			self      :    int      ,      UpperCAmelCase__      :    Tuple      ,      UpperCAmelCase__      :    List[str]      ,      UpperCAmelCase__      :    Tuple=False       )	->     Optional[int]:
						__SCREAMING_SNAKE_CASE						       =    super()._prepare_for_class(UpperCAmelCase__      ,      UpperCAmelCase__      ,      return_labels=UpperCAmelCase__       )

						if return_labels:
											if model_class.__name__ == "YolosForObjectDetection":
																__SCREAMING_SNAKE_CASE						       =    []
																for i in range(self.model_tester.batch_size       ):
																					__SCREAMING_SNAKE_CASE						       =    {}
																					__SCREAMING_SNAKE_CASE						       =    torch.ones(
																					    size=(self.model_tester.n_targets,)      ,      device=UpperCAmelCase__      ,      dtype=torch.long       )
																					__SCREAMING_SNAKE_CASE						       =    torch.ones(
																					    self.model_tester.n_targets      ,      4      ,      device=UpperCAmelCase__      ,      dtype=torch.float       )
																					labels.append(UpperCAmelCase__       )
																__SCREAMING_SNAKE_CASE						       =    labels

						return inputs_dict




	def   UpperCAmelCase_				(			self      :    Dict       )	->     Any:
						__SCREAMING_SNAKE_CASE						       =    YolosModelTester(self       )
						__SCREAMING_SNAKE_CASE						       =    ConfigTester(self      ,      config_class=UpperCAmelCase__      ,      has_text_modality=UpperCAmelCase__      ,      hidden_size=3_7       )




	def   UpperCAmelCase_				(			self      :    str       )	->     List[Any]:
						self.config_tester.run_common_tests()




	def   UpperCAmelCase_				(			self      :    Tuple       )	->     Optional[Any]:
						# YOLOS does not use inputs_embeds
						pass




	def   UpperCAmelCase_				(			self      :    Optional[int]       )	->     List[Any]:
						__SCREAMING_SNAKE_CASE       ,  __SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs_for_common()

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




	def   UpperCAmelCase_				(			self      :    List[Any]       )	->     Tuple:
						__SCREAMING_SNAKE_CASE       ,  __SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs_for_common()

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

											__SCREAMING_SNAKE_CASE						       =    ["pixel_values"]
											self.assertListEqual(arg_names[:1]      ,      UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Union[str, Any]       )	->     Any:
						__SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    List[Any]       )	->     Tuple:
						__SCREAMING_SNAKE_CASE       ,  __SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs_for_common()
						__SCREAMING_SNAKE_CASE						       =    True

						# in YOLOS, the seq_len is different
						__SCREAMING_SNAKE_CASE						       =    self.model_tester.expected_seq_len
						for model_class in self.all_model_classes:
											__SCREAMING_SNAKE_CASE						       =    True
											__SCREAMING_SNAKE_CASE						       =    False
											__SCREAMING_SNAKE_CASE						       =    True
											__SCREAMING_SNAKE_CASE						       =    model_class(UpperCAmelCase__       )
											model.to(UpperCAmelCase__       )
											model.eval()
											with torch.no_grad():
																__SCREAMING_SNAKE_CASE						       =    model(**self._prepare_for_class(UpperCAmelCase__      ,      UpperCAmelCase__       )       )
											__SCREAMING_SNAKE_CASE						       =    outputs.attentions
											self.assertEqual(len(UpperCAmelCase__       )      ,      self.model_tester.num_hidden_layers       )

											# check that output_attentions also work using config
											del inputs_dict["output_attentions"]
											__SCREAMING_SNAKE_CASE						       =    True
											__SCREAMING_SNAKE_CASE						       =    model_class(UpperCAmelCase__       )
											model.to(UpperCAmelCase__       )
											model.eval()
											with torch.no_grad():
																__SCREAMING_SNAKE_CASE						       =    model(**self._prepare_for_class(UpperCAmelCase__      ,      UpperCAmelCase__       )       )
											__SCREAMING_SNAKE_CASE						       =    outputs.attentions
											self.assertEqual(len(UpperCAmelCase__       )      ,      self.model_tester.num_hidden_layers       )

											self.assertListEqual(
											    list(attentions[0].shape[-3:]       )      ,      [self.model_tester.num_attention_heads, seq_len, seq_len]      ,      )
											__SCREAMING_SNAKE_CASE						       =    len(UpperCAmelCase__       )

											# Check attention is always last and order is fine
											__SCREAMING_SNAKE_CASE						       =    True
											__SCREAMING_SNAKE_CASE						       =    True
											__SCREAMING_SNAKE_CASE						       =    model_class(UpperCAmelCase__       )
											model.to(UpperCAmelCase__       )
											model.eval()
											with torch.no_grad():
																__SCREAMING_SNAKE_CASE						       =    model(**self._prepare_for_class(UpperCAmelCase__      ,      UpperCAmelCase__       )       )

											__SCREAMING_SNAKE_CASE						       =    1
											self.assertEqual(out_len + added_hidden_states      ,      len(UpperCAmelCase__       )       )

											__SCREAMING_SNAKE_CASE						       =    outputs.attentions

											self.assertEqual(len(UpperCAmelCase__       )      ,      self.model_tester.num_hidden_layers       )
											self.assertListEqual(
											    list(self_attentions[0].shape[-3:]       )      ,      [self.model_tester.num_attention_heads, seq_len, seq_len]      ,      )




	def   UpperCAmelCase_				(			self      :    Any       )	->     List[str]:
						def check_hidden_states_output(UpperCAmelCase__      :    Optional[int]      ,      UpperCAmelCase__      :    List[Any]      ,      UpperCAmelCase__      :    str       ):
											__SCREAMING_SNAKE_CASE						       =    model_class(UpperCAmelCase__       )
											model.to(UpperCAmelCase__       )
											model.eval()

											with torch.no_grad():
																__SCREAMING_SNAKE_CASE						       =    model(**self._prepare_for_class(UpperCAmelCase__      ,      UpperCAmelCase__       )       )

											__SCREAMING_SNAKE_CASE						       =    outputs.hidden_states

											__SCREAMING_SNAKE_CASE						       =    getattr(
											    self.model_tester      ,      "expected_num_hidden_layers"      ,      self.model_tester.num_hidden_layers + 1       )
											self.assertEqual(len(UpperCAmelCase__       )      ,      UpperCAmelCase__       )

											# YOLOS has a different seq_length
											__SCREAMING_SNAKE_CASE						       =    self.model_tester.expected_seq_len

											self.assertListEqual(
											    list(hidden_states[0].shape[-2:]       )      ,      [seq_length, self.model_tester.hidden_size]      ,      )

						__SCREAMING_SNAKE_CASE       ,  __SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
											__SCREAMING_SNAKE_CASE						       =    True
											check_hidden_states_output(UpperCAmelCase__      ,      UpperCAmelCase__      ,      UpperCAmelCase__       )

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

											check_hidden_states_output(UpperCAmelCase__      ,      UpperCAmelCase__      ,      UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Optional[Any]       )	->     Optional[int]:
						__SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_object_detection(*UpperCAmelCase__       )




	@slow
	def   UpperCAmelCase_				(			self      :    Any       )	->     Union[str, Any]:
						for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__SCREAMING_SNAKE_CASE						       =    YolosModel.from_pretrained(UpperCAmelCase__       )
											self.assertIsNotNone(UpperCAmelCase__       )




def        UpperCAmelCase__	():




					'''simple docstring'''


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




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

	"""simple docstring"""





	@cached_property
	def   UpperCAmelCase_				(			self      :    List[str]       )	->     int:
						return AutoImageProcessor.from_pretrained("hustvl/yolos-small"       ) if is_vision_available() else None




	@slow
	def   UpperCAmelCase_				(			self      :    Tuple       )	->     Any:
						__SCREAMING_SNAKE_CASE						       =    YolosForObjectDetection.from_pretrained("hustvl/yolos-small"       ).to(UpperCAmelCase__       )

						__SCREAMING_SNAKE_CASE						       =    self.default_image_processor
						__SCREAMING_SNAKE_CASE						       =    prepare_img()
						__SCREAMING_SNAKE_CASE						       =    image_processor(images=UpperCAmelCase__      ,      return_tensors="pt"       ).to(UpperCAmelCase__       )

						# forward pass
						with torch.no_grad():
											__SCREAMING_SNAKE_CASE						       =    model(inputs.pixel_values       )

						# verify outputs
						__SCREAMING_SNAKE_CASE						       =    torch.Size((1, 1_0_0, 9_2)       )
						self.assertEqual(outputs.logits.shape      ,      UpperCAmelCase__       )

						__SCREAMING_SNAKE_CASE						       =    torch.tensor(
						    [[-24.0_248, -10.3_024, -14.8_290], [-42.0_392, -16.8_200, -27.4_334], [-27.2_743, -11.8_154, -18.7_148]]      ,      device=UpperCAmelCase__      ,      )
						__SCREAMING_SNAKE_CASE						       =    torch.tensor(
						    [[0.2_559, 0.5_455, 0.4_706], [0.2_989, 0.7_279, 0.1_875], [0.7_732, 0.4_017, 0.4_462]]      ,      device=UpperCAmelCase__       )
						self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]      ,      UpperCAmelCase__      ,      atol=1E-4       )       )
						self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3]      ,      UpperCAmelCase__      ,      atol=1E-4       )       )

						# verify postprocessing
						__SCREAMING_SNAKE_CASE						       =    image_processor.post_process_object_detection(
						    UpperCAmelCase__      ,      threshold=0.3      ,      target_sizes=[image.size[::-1]]       )[0]
						__SCREAMING_SNAKE_CASE						       =    torch.tensor([0.9_994, 0.9_790, 0.9_964, 0.9_972, 0.9_861]       ).to(UpperCAmelCase__       )
						__SCREAMING_SNAKE_CASE						       =    [7_5, 7_5, 1_7, 6_3, 1_7]
						__SCREAMING_SNAKE_CASE						       =    torch.tensor([335.0_609, 79.3_848, 375.4_216, 187.2_495]       ).to(UpperCAmelCase__       )

						self.assertEqual(len(results["scores"]       )      ,      5       )
						self.assertTrue(torch.allclose(results["scores"]      ,      UpperCAmelCase__      ,      atol=1E-4       )       )
						self.assertSequenceEqual(results["labels"].tolist()      ,      UpperCAmelCase__       )
						self.assertTrue(torch.allclose(results["boxes"][0, :]      ,      UpperCAmelCase__       )       )




"""simple docstring"""







import inspect
import unittest

import numpy as np

from transformers import ViTConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
		import jax

		from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel




class 			UpperCamelCase_     (			unittest.TestCase):

	"""simple docstring"""





	def __init__(			self      :    Optional[Any]      ,      UpperCAmelCase__      :    Optional[Any]      ,      UpperCAmelCase__      :    Tuple=1_3      ,      UpperCAmelCase__      :    Optional[int]=3_0      ,      UpperCAmelCase__      :    Any=2      ,      UpperCAmelCase__      :    List[str]=3      ,      UpperCAmelCase__      :    Optional[Any]=True      ,      UpperCAmelCase__      :    Optional[int]=True      ,      UpperCAmelCase__      :    int=3_2      ,      UpperCAmelCase__      :    Any=5      ,      UpperCAmelCase__      :    Union[str, Any]=4      ,      UpperCAmelCase__      :    List[str]=3_7      ,      UpperCAmelCase__      :    Tuple="gelu"      ,      UpperCAmelCase__      :    List[str]=0.1      ,      UpperCAmelCase__      :    str=0.1      ,      UpperCAmelCase__      :    Any=1_0      ,      UpperCAmelCase__      :    str=0.02      ,      )	->     Tuple:
						__SCREAMING_SNAKE_CASE						       =    parent
						__SCREAMING_SNAKE_CASE						       =    batch_size
						__SCREAMING_SNAKE_CASE						       =    image_size
						__SCREAMING_SNAKE_CASE						       =    patch_size
						__SCREAMING_SNAKE_CASE						       =    num_channels
						__SCREAMING_SNAKE_CASE						       =    is_training
						__SCREAMING_SNAKE_CASE						       =    use_labels
						__SCREAMING_SNAKE_CASE						       =    hidden_size
						__SCREAMING_SNAKE_CASE						       =    num_hidden_layers
						__SCREAMING_SNAKE_CASE						       =    num_attention_heads
						__SCREAMING_SNAKE_CASE						       =    intermediate_size
						__SCREAMING_SNAKE_CASE						       =    hidden_act
						__SCREAMING_SNAKE_CASE						       =    hidden_dropout_prob
						__SCREAMING_SNAKE_CASE						       =    attention_probs_dropout_prob
						__SCREAMING_SNAKE_CASE						       =    type_sequence_label_size
						__SCREAMING_SNAKE_CASE						       =    initializer_range

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




	def   UpperCAmelCase_				(			self      :    Union[str, Any]       )	->     Tuple:
						__SCREAMING_SNAKE_CASE						       =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

						__SCREAMING_SNAKE_CASE						       =    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=UpperCAmelCase__      ,      initializer_range=self.initializer_range      ,      )

						return config, pixel_values




	def   UpperCAmelCase_				(			self      :    Tuple      ,      UpperCAmelCase__      :    str      ,      UpperCAmelCase__      :    List[str]       )	->     Any:
						__SCREAMING_SNAKE_CASE						       =    FlaxViTModel(config=UpperCAmelCase__       )
						__SCREAMING_SNAKE_CASE						       =    model(UpperCAmelCase__       )
						# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
						__SCREAMING_SNAKE_CASE						       =    (self.image_size, self.image_size)
						__SCREAMING_SNAKE_CASE						       =    (self.patch_size, self.patch_size)
						__SCREAMING_SNAKE_CASE						       =    (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
						self.parent.assertEqual(result.last_hidden_state.shape      ,      (self.batch_size, num_patches + 1, self.hidden_size)       )




	def   UpperCAmelCase_				(			self      :    str      ,      UpperCAmelCase__      :    List[str]      ,      UpperCAmelCase__      :    Tuple       )	->     Tuple:
						__SCREAMING_SNAKE_CASE						       =    self.type_sequence_label_size
						__SCREAMING_SNAKE_CASE						       =    FlaxViTForImageClassification(config=UpperCAmelCase__       )
						__SCREAMING_SNAKE_CASE						       =    model(UpperCAmelCase__       )
						self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.type_sequence_label_size)       )

						# test greyscale images
						__SCREAMING_SNAKE_CASE						       =    1
						__SCREAMING_SNAKE_CASE						       =    FlaxViTForImageClassification(UpperCAmelCase__       )

						__SCREAMING_SNAKE_CASE						       =    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
						__SCREAMING_SNAKE_CASE						       =    model(UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Union[str, Any]       )	->     List[Any]:
						__SCREAMING_SNAKE_CASE						       =    self.prepare_config_and_inputs()
						(
						    (
						    __SCREAMING_SNAKE_CASE
						)       ,  (
						    __SCREAMING_SNAKE_CASE
						)       ,  
						)						       =    config_and_inputs
						__SCREAMING_SNAKE_CASE						       =    {"pixel_values": pixel_values}
						return config, inputs_dict




@require_flax
class 			UpperCamelCase_     (			UpperCamelCase      ,  unittest.TestCase):

	"""simple docstring"""





	snake_case__    :  List[str]    =      (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()




	def   UpperCAmelCase_				(			self      :    int       )	->     None:
						__SCREAMING_SNAKE_CASE						       =    FlaxViTModelTester(self       )
						__SCREAMING_SNAKE_CASE						       =    ConfigTester(self      ,      config_class=UpperCAmelCase__      ,      has_text_modality=UpperCAmelCase__      ,      hidden_size=3_7       )




	def   UpperCAmelCase_				(			self      :    int       )	->     Union[str, Any]:
						self.config_tester.run_common_tests()




	def   UpperCAmelCase_				(			self      :    Tuple       )	->     List[str]:
						__SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Dict       )	->     Dict:
						__SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Tuple       )	->     Any:
						__SCREAMING_SNAKE_CASE       ,  __SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs_for_common()

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

											__SCREAMING_SNAKE_CASE						       =    ["pixel_values"]
											self.assertListEqual(arg_names[:1]      ,      UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Any       )	->     List[Any]:
						__SCREAMING_SNAKE_CASE       ,  __SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
											with self.subTest(model_class.__name__       ):
																__SCREAMING_SNAKE_CASE						       =    self._prepare_for_class(UpperCAmelCase__      ,      UpperCAmelCase__       )
																__SCREAMING_SNAKE_CASE						       =    model_class(UpperCAmelCase__       )

																@jax.jit
																def model_jitted(UpperCAmelCase__      :    Optional[int]      ,      **UpperCAmelCase__      :    Any       ):
																					return model(pixel_values=UpperCAmelCase__      ,      **UpperCAmelCase__       )

																with self.subTest("JIT Enabled"       ):
																					__SCREAMING_SNAKE_CASE						       =    model_jitted(**UpperCAmelCase__       ).to_tuple()

																with self.subTest("JIT Disabled"       ):
																					with jax.disable_jit():
																										__SCREAMING_SNAKE_CASE						       =    model_jitted(**UpperCAmelCase__       ).to_tuple()

																self.assertEqual(len(UpperCAmelCase__       )      ,      len(UpperCAmelCase__       )       )
																for jitted_output, output in zip(UpperCAmelCase__      ,      UpperCAmelCase__       ):
																					self.assertEqual(jitted_output.shape      ,      output.shape       )




	@slow
	def   UpperCAmelCase_				(			self      :    int       )	->     Union[str, Any]:
						for model_class_name in self.all_model_classes:
											__SCREAMING_SNAKE_CASE						       =    model_class_name.from_pretrained("google/vit-base-patch16-224"       )
											__SCREAMING_SNAKE_CASE						       =    model(np.ones((1, 3, 2_2_4, 2_2_4)       )       )
											self.assertIsNotNone(UpperCAmelCase__       )
