

'''simple docstring'''





from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
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 TFViTForImageClassification, TFViTModel


if is_vision_available():
							from PIL import Image

							from transformers import ViTImageProcessor
class        A  :





						def __init__(	self	,    snake_case_	,    snake_case_=1_3	,    snake_case_=3_0	,    snake_case_=2	,    snake_case_=3	,    snake_case_=True	,    snake_case_=True	,    snake_case_=3_2	,    snake_case_=2	,    snake_case_=4	,    snake_case_=3_7	,    snake_case_="gelu"	,    snake_case_=0.1	,    snake_case_=0.1	,    snake_case_=1_0	,    snake_case_=0.02	,    snake_case_=3	,    snake_case_=None	,    )			->							Optional[Any]:
										_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

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





						def __lowerCAmelCase   (	self			)			->							Optional[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 __lowerCAmelCase   (	self			)			->							Optional[Any]:
										return ViTConfig(
										    image_size=self.image_size	,    patch_size=self.patch_size	,    num_channels=self.num_channels	,    hidden_size=self.hidden_size	,    num_hidden_layers=self.num_hidden_layers	,    num_attention_heads=self.num_attention_heads	,    intermediate_size=self.intermediate_size	,    hidden_act=self.hidden_act	,    hidden_dropout_prob=self.hidden_dropout_prob	,    attention_probs_dropout_prob=self.attention_probs_dropout_prob	,    is_decoder=snake_case_	,    initializer_range=self.initializer_range	,    )





						def __lowerCAmelCase   (	self	,    snake_case_	,    snake_case_	,    snake_case_			)			->							str:
										_a        =   TFViTModel(config=snake_case_			)
										_a        =   model(snake_case_	,    training=snake_case_			)
										self.parent.assertEqual(result.last_hidden_state.shape	,    (self.batch_size, self.seq_length, self.hidden_size)			)

										# Test with an image with different size than the one specified in config.
										_a        =   self.image_size // 2
										_a        =   pixel_values[:, :, :image_size, :image_size]
										_a        =   model(snake_case_	,    interpolate_pos_encoding=snake_case_	,    training=snake_case_			)
										_a        =   (image_size // self.patch_size) ** 2 + 1
										self.parent.assertEqual(result.last_hidden_state.shape	,    (self.batch_size, seq_length, self.hidden_size)			)





						def __lowerCAmelCase   (	self	,    snake_case_	,    snake_case_	,    snake_case_			)			->							Optional[Any]:
										_a        =   self.type_sequence_label_size
										_a        =   TFViTForImageClassification(snake_case_			)
										_a        =   model(snake_case_	,    labels=snake_case_	,    training=snake_case_			)
										self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.type_sequence_label_size)			)

										# Test with an image with different size than the one specified in config.
										_a        =   self.image_size // 2
										_a        =   pixel_values[:, :, :image_size, :image_size]
										_a        =   model(snake_case_	,    interpolate_pos_encoding=snake_case_	,    training=snake_case_			)
										self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.type_sequence_label_size)			)

										# test greyscale images
										_a        =   1
										_a        =   TFViTForImageClassification(snake_case_			)
										_a        =   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
										_a        =   model(snake_case_			)
										self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.type_sequence_label_size)			)





						def __lowerCAmelCase   (	self			)			->							Tuple:
										_a        =   self.prepare_config_and_inputs()
										_a        =   config_and_inputs
										_a        =   {'pixel_values': pixel_values}
										return config, inputs_dict



@require_tf
class        A  (	a  ,    a  ,    unittest.TestCase ):
						__UpperCAmelCase				:	str   =   (TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
						__UpperCAmelCase				:	List[str]   =   (
						    {"""feature-extraction""": TFViTModel, """image-classification""": TFViTForImageClassification}
						    if is_tf_available()
						    else {}
						)

						__UpperCAmelCase				:	Union[str, Any]   =   False
						__UpperCAmelCase				:	Optional[Any]   =   False
						__UpperCAmelCase				:	Union[str, Any]   =   False





						def __lowerCAmelCase   (	self			)			->							int:
										_a        =   TFViTModelTester(self			)
										_a        =   ConfigTester(self	,    config_class=snake_case_	,    has_text_modality=snake_case_	,    hidden_size=3_7			)





						def __lowerCAmelCase   (	self			)			->							str:
										self.config_tester.run_common_tests()





						@unittest.skip(reason="ViT does not use inputs_embeds"			)
						def __lowerCAmelCase   (	self			)			->							Dict:
										pass





						@unittest.skip(reason="ViT does not use inputs_embeds"			)
						def __lowerCAmelCase   (	self			)			->							Optional[int]:
										pass





						def __lowerCAmelCase   (	self			)			->							List[str]:
										_a        =   self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
														_a        =   model_class(snake_case_			)
														self.assertIsInstance(model.get_input_embeddings()	,    (tf.keras.layers.Layer)			)
														_a        =   model.get_output_embeddings()
														self.assertTrue(x is None or isinstance(snake_case_	,    tf.keras.layers.Layer			)			)





						def __lowerCAmelCase   (	self			)			->							List[Any]:
										_a        =   self.model_tester.prepare_config_and_inputs_for_common()

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

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





						def __lowerCAmelCase   (	self			)			->							Any:
										_a        =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*snake_case_			)





						def __lowerCAmelCase   (	self			)			->							Tuple:
										_a        =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*snake_case_			)





						@slow
						def __lowerCAmelCase   (	self			)			->							Union[str, Any]:
										_a        =   TFViTModel.from_pretrained("google/vit-base-patch16-224"			)
										self.assertIsNotNone(snake_case_			)



def 					_lowercase						(   ):
				_a        =   Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
				return image




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





						@cached_property
						def __lowerCAmelCase   (	self			)			->							Union[str, Any]:
										return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224"			) if is_vision_available() else None





						@slow
						def __lowerCAmelCase   (	self			)			->							Optional[int]:
										_a        =   TFViTForImageClassification.from_pretrained("google/vit-base-patch16-224"			)

										_a        =   self.default_image_processor
										_a        =   prepare_img()
										_a        =   image_processor(images=snake_case_	,    return_tensors="tf"			)

										# forward pass
										_a        =   model(**snake_case_			)

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

										_a        =   tf.constant([-0.2_744, 0.8_215, -0.0_836]			)

										tf.debugging.assert_near(outputs.logits[0, :3]	,    snake_case_	,    atol=1E-4			)











"""simple docstring"""






import re







def 	_snake_case     (							lowercase__						):

				if len(re.findall('[ATCG]'				,       lowercase__						)						) != len(lowercase__						):
								raise ValueError('Invalid Strand'						)

				return dna.translate(dna.maketrans('ATCG'				,       'TAGC'						)						)


if __name__ == "__main__":
			import doctest

			doctest.testmod()