


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		:


					'''simple docstring'''






					def __init__(				self					, A__					, A__=13					, A__=30					, A__=2					, A__=3					, A__=True					, A__=True					, A__=32					, A__=2					, A__=4					, A__=37					, A__="gelu"					, A__=0.1					, A__=0.1					, A__=10					, A__=0.0_2					, A__=3					, A__=None					, ):
						A__							:					List[Any]       =	parent
						A__							:					Union[str, Any]       =	batch_size
						A__							:					Tuple       =	image_size
						A__							:					int       =	patch_size
						A__							:					str       =	num_channels
						A__							:					Optional[int]       =	is_training
						A__							:					Optional[Any]       =	use_labels
						A__							:					Any       =	hidden_size
						A__							:					Tuple       =	num_hidden_layers
						A__							:					Optional[int]       =	num_attention_heads
						A__							:					Any       =	intermediate_size
						A__							:					str       =	hidden_act
						A__							:					Dict       =	hidden_dropout_prob
						A__							:					Optional[int]       =	attention_probs_dropout_prob
						A__							:					Any       =	type_sequence_label_size
						A__							:					Optional[Any]       =	initializer_range
						A__							:					Dict       =	scope

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






					def 						__A    (				self    ):
						A__							:					Tuple       =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

						A__							:					str       =	None
						if self.use_labels:
							A__							:					Any       =	ids_tensor([self.batch_size]					, self.type_sequence_label_size    )

						A__							:					int       =	self.get_config()

						return config, pixel_values, labels






					def 						__A    (				self    ):
						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=lowercase_					, initializer_range=self.initializer_range					, )






					def 						__A    (				self					, A__					, A__					, A__    ):
						A__							:					Dict       =	TFViTModel(config=lowercase_    )
						A__							:					Dict       =	model(lowercase_					, training=lowercase_    )
						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__							:					Any       =	self.image_size // 2
						A__							:					Any       =	pixel_values[:, :, :image_size, :image_size]
						A__							:					Any       =	model(lowercase_					, interpolate_pos_encoding=lowercase_					, training=lowercase_    )
						A__							:					Optional[Any]       =	(image_size // self.patch_size) ** 2 + 1
						self.parent.assertEqual(result.last_hidden_state.shape					, (self.batch_size, seq_length, self.hidden_size)    )






					def 						__A    (				self					, A__					, A__					, A__    ):
						A__							:					List[str]       =	self.type_sequence_label_size
						A__							:					Optional[Any]       =	TFViTForImageClassification(lowercase_    )
						A__							:					Optional[int]       =	model(lowercase_					, labels=lowercase_					, training=lowercase_    )
						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__							:					int       =	self.image_size // 2
						A__							:					Optional[int]       =	pixel_values[:, :, :image_size, :image_size]
						A__							:					Optional[Any]       =	model(lowercase_					, interpolate_pos_encoding=lowercase_					, training=lowercase_    )
						self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.type_sequence_label_size)    )

						# test greyscale images
						A__							:					Dict       =	1
						A__							:					str       =	TFViTForImageClassification(lowercase_    )
						A__							:					Union[str, Any]       =	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
						A__							:					List[Any]       =	model(lowercase_    )
						self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.type_sequence_label_size)    )






					def 						__A    (				self    ):
						A__							:					Any       =	self.prepare_config_and_inputs()
						A__     ,     A__     ,     A__							:					List[str]       =	config_and_inputs
						A__							:					int       =	{"""pixel_values""": pixel_values}
						return config, inputs_dict



@require_tf
class 					_a		(__magic_name__	,     __magic_name__	,     unittest.TestCase       ):


					'''simple docstring'''






					UpperCAmelCase__:     List[str]													= (TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
					UpperCAmelCase__:     Dict													= (
					    {'''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 						__A    (				self    ):
						A__							:					str       =	TFViTModelTester(self    )
						A__							:					Optional[Any]       =	ConfigTester(self					, config_class=lowercase_					, has_text_modality=lowercase_					, hidden_size=37    )






					def 						__A    (				self    ):
						self.config_tester.run_common_tests()






					@unittest.skip(reason="""ViT does not use inputs_embeds"""    )
					def 						__A    (				self    ):
						pass






					@unittest.skip(reason="""ViT does not use inputs_embeds"""    )
					def 						__A    (				self    ):
						pass






					def 						__A    (				self    ):
						A__     ,     A__							:					List[str]       =	self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
							A__							:					Any       =	model_class(lowercase_    )
							self.assertIsInstance(model.get_input_embeddings()					, (tf.keras.layers.Layer)    )
							A__							:					List[str]       =	model.get_output_embeddings()
							self.assertTrue(x is None or isinstance(lowercase_					, tf.keras.layers.Layer    )    )






					def 						__A    (				self    ):
						A__     ,     A__							:					Dict       =	self.model_tester.prepare_config_and_inputs_for_common()

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

							A__							:					int       =	["""pixel_values"""]
							self.assertListEqual(arg_names[:1]					, lowercase_    )






					def 						__A    (				self    ):
						A__							:					Dict       =	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowercase_    )






					def 						__A    (				self    ):
						A__							:					str       =	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*lowercase_    )






					@slow
					def 						__A    (				self    ):
						A__							:					Optional[Any]       =	TFViTModel.from_pretrained("""google/vit-base-patch16-224"""    )
						self.assertIsNotNone(lowercase_    )



def 		UpperCamelCase      ()  ->   Dict:
	A__							:					List[Any]       =	Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
	return image



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


					'''simple docstring'''






					@cached_property
					def 						__A    (				self    ):
						return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224"""    ) if is_vision_available() else None






					@slow
					def 						__A    (				self    ):
						A__							:					Dict       =	TFViTForImageClassification.from_pretrained("""google/vit-base-patch16-224"""    )

						A__							:					List[str]       =	self.default_image_processor
						A__							:					List[str]       =	prepare_img()
						A__							:					Any       =	image_processor(images=lowercase_					, return_tensors="""tf"""    )

						# forward pass
						A__							:					str       =	model(**lowercase_    )

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

						A__							:					Union[str, Any]       =	tf.constant([-0.2_7_4_4, 0.8_2_1_5, -0.0_8_3_6]    )

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






import os
import tempfile
from functools import partial
from unittest import TestCase
from unittest.mock import patch

import datasets
import datasets.config

from .utils import require_beam



class 					_a		(datasets.BeamBasedBuilder       ):


					'''simple docstring'''






					def 						__A    (				self    ):
						return datasets.DatasetInfo(
						    features=datasets.Features({"""content""": datasets.Value("""string"""    )}    )					, supervised_keys=A__					, )






					def 						__A    (				self					, A__					, A__    ):
						return [datasets.SplitGenerator(name=datasets.Split.TRAIN					, gen_kwargs={"""examples""": get_test_dummy_examples()}    )]






					def 						__A    (				self					, A__					, A__    ):
						import apache_beam as beam

						return pipeline | "Load Examples" >> beam.Create(A__    )



class 					_a		(datasets.BeamBasedBuilder       ):


					'''simple docstring'''






					def 						__A    (				self    ):
						return datasets.DatasetInfo(
						    features=datasets.Features({"""a""": datasets.Sequence({"""b""": datasets.Value("""string"""    )}    )}    )					, supervised_keys=A__					, )






					def 						__A    (				self					, A__					, A__    ):
						return [
						    datasets.SplitGenerator(name=datasets.Split.TRAIN					, gen_kwargs={"""examples""": get_test_nested_examples()}    )
						]






					def 						__A    (				self					, A__					, A__    ):
						import apache_beam as beam

						return pipeline | "Load Examples" >> beam.Create(A__    )



def 		UpperCamelCase      ()  ->   Dict:
	return [(i, {"content": content}) for i, content in enumerate(["""foo""", """bar""", """foobar"""]      )]

def 		UpperCamelCase      ()  ->   Tuple:
	return [(i, {"a": {"b": [content]}}) for i, content in enumerate(["""foo""", """bar""", """foobar"""]      )]



class 					_a		(__magic_name__       ):


					'''simple docstring'''






					@require_beam
					def 						__A    (				self    ):
						A__							:					Dict       =	len(get_test_dummy_examples()    )
						with tempfile.TemporaryDirectory() as tmp_cache_dir:
							A__							:					int       =	DummyBeamDataset(cache_dir=A__					, beam_runner="""DirectRunner"""    )
							builder.download_and_prepare()
							self.assertTrue(
							    os.path.exists(
							        os.path.join(A__					, builder.name					, """default"""					, """0.0.0"""					, F"""{builder.name}-train.arrow"""    )    )    )
							self.assertDictEqual(builder.info.features					, datasets.Features({"""content""": datasets.Value("""string"""    )}    )    )
							A__							:					int       =	builder.as_dataset()
							self.assertEqual(dset["""train"""].num_rows					, A__    )
							self.assertEqual(dset["""train"""].info.splits["""train"""].num_examples					, A__    )
							self.assertDictEqual(dset["""train"""][0]					, get_test_dummy_examples()[0][1]    )
							self.assertDictEqual(
							    dset["""train"""][expected_num_examples - 1]					, get_test_dummy_examples()[expected_num_examples - 1][1]    )
							self.assertTrue(
							    os.path.exists(os.path.join(A__					, builder.name					, """default"""					, """0.0.0"""					, """dataset_info.json"""    )    )    )
							del dset






					@require_beam
					def 						__A    (				self    ):
						import apache_beam as beam

						A__							:					int       =	beam.io.parquetio.WriteToParquet

						A__							:					List[str]       =	len(get_test_dummy_examples()    )
						with tempfile.TemporaryDirectory() as tmp_cache_dir:
							A__							:					str       =	DummyBeamDataset(cache_dir=A__					, beam_runner="""DirectRunner"""    )
							with patch("""apache_beam.io.parquetio.WriteToParquet"""    ) as write_parquet_mock:
								A__							:					Optional[Any]       =	partial(A__					, num_shards=2    )
								builder.download_and_prepare()
							self.assertTrue(
							    os.path.exists(
							        os.path.join(
							            A__					, builder.name					, """default"""					, """0.0.0"""					, F"""{builder.name}-train-00000-of-00002.arrow"""    )    )    )
							self.assertTrue(
							    os.path.exists(
							        os.path.join(
							            A__					, builder.name					, """default"""					, """0.0.0"""					, F"""{builder.name}-train-00000-of-00002.arrow"""    )    )    )
							self.assertDictEqual(builder.info.features					, datasets.Features({"""content""": datasets.Value("""string"""    )}    )    )
							A__							:					Optional[int]       =	builder.as_dataset()
							self.assertEqual(dset["""train"""].num_rows					, A__    )
							self.assertEqual(dset["""train"""].info.splits["""train"""].num_examples					, A__    )
							# Order is not preserved when sharding, so we just check that all the elements are there
							self.assertListEqual(sorted(dset["""train"""]["""content"""]    )					, sorted(["""foo""", """bar""", """foobar"""]    )    )
							self.assertTrue(
							    os.path.exists(os.path.join(A__					, builder.name					, """default"""					, """0.0.0"""					, """dataset_info.json"""    )    )    )
							del dset






					@require_beam
					def 						__A    (				self    ):
						with tempfile.TemporaryDirectory() as tmp_cache_dir:
							A__							:					int       =	DummyBeamDataset(cache_dir=A__    )
							self.assertRaises(datasets.builder.MissingBeamOptions					, builder.download_and_prepare    )






					@require_beam
					def 						__A    (				self    ):
						A__							:					List[Any]       =	len(get_test_nested_examples()    )
						with tempfile.TemporaryDirectory() as tmp_cache_dir:
							A__							:					Optional[int]       =	NestedBeamDataset(cache_dir=A__					, beam_runner="""DirectRunner"""    )
							builder.download_and_prepare()
							self.assertTrue(
							    os.path.exists(
							        os.path.join(A__					, builder.name					, """default"""					, """0.0.0"""					, F"""{builder.name}-train.arrow"""    )    )    )
							self.assertDictEqual(
							    builder.info.features					, datasets.Features({"""a""": datasets.Sequence({"""b""": datasets.Value("""string"""    )}    )}    )    )
							A__							:					Optional[int]       =	builder.as_dataset()
							self.assertEqual(dset["""train"""].num_rows					, A__    )
							self.assertEqual(dset["""train"""].info.splits["""train"""].num_examples					, A__    )
							self.assertDictEqual(dset["""train"""][0]					, get_test_nested_examples()[0][1]    )
							self.assertDictEqual(
							    dset["""train"""][expected_num_examples - 1]					, get_test_nested_examples()[expected_num_examples - 1][1]    )
							self.assertTrue(
							    os.path.exists(os.path.join(A__					, builder.name					, """default"""					, """0.0.0"""					, """dataset_info.json"""    )    )    )
							del dset



