




import tempfile
import unittest

import numpy as np

import transformers
from transformers import GPTaConfig, GPTaTokenizer, is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax, slow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
		import jax
		import jax.numpy as jnp

		from transformers.modeling_flax_pytorch_utils import (
		    convert_pytorch_state_dict_to_flax,
		    load_flax_weights_in_pytorch_model,
		)
		from transformers.models.gpta.modeling_flax_gpta import FlaxGPTaLMHeadModel, FlaxGPTaModel

if is_torch_available():
		import torch


class 				__UpperCamelCase      :





				'''simple docstring'''



				def __init__(			self							,			UpperCAmelCase_							,			UpperCAmelCase_=14							,			UpperCAmelCase_=7							,			UpperCAmelCase_=True							,			UpperCAmelCase_=True							,			UpperCAmelCase_=False							,			UpperCAmelCase_=True							,			UpperCAmelCase_=99							,			UpperCAmelCase_=32							,			UpperCAmelCase_=5							,			UpperCAmelCase_=4							,			UpperCAmelCase_=37							,			UpperCAmelCase_="gelu"							,			UpperCAmelCase_=0.1							,			UpperCAmelCase_=0.1							,			UpperCAmelCase_=5_12							,			UpperCAmelCase_=0.02							,			):
											lowerCAmelCase =		parent
											lowerCAmelCase =		batch_size
											lowerCAmelCase =		seq_length
											lowerCAmelCase =		is_training
											lowerCAmelCase =		use_input_mask
											lowerCAmelCase =		use_token_type_ids
											lowerCAmelCase =		use_labels
											lowerCAmelCase =		vocab_size
											lowerCAmelCase =		hidden_size
											lowerCAmelCase =		num_hidden_layers
											lowerCAmelCase =		num_attention_heads
											lowerCAmelCase =		intermediate_size
											lowerCAmelCase =		hidden_act
											lowerCAmelCase =		hidden_dropout_prob
											lowerCAmelCase =		attention_probs_dropout_prob
											lowerCAmelCase =		max_position_embeddings
											lowerCAmelCase =		initializer_range
											lowerCAmelCase =		None
											lowerCAmelCase =		vocab_size - 1
											lowerCAmelCase =		vocab_size - 1
											lowerCAmelCase =		vocab_size - 1



				def    __snake_case		(			self    ):
											lowerCAmelCase =		ids_tensor([self.batch_size, self.seq_length]							,			self.vocab_size    )

											lowerCAmelCase =		None
											if self.use_input_mask:
																		lowerCAmelCase =		random_attention_mask([self.batch_size, self.seq_length]    )

											lowerCAmelCase =		GPTaConfig(
											    vocab_size=self.vocab_size							,			n_embd=self.hidden_size							,			n_layer=self.num_hidden_layers							,			n_head=self.num_attention_heads							,			n_positions=self.max_position_embeddings							,			use_cache=UpperCAmelCase_							,			bos_token_id=self.bos_token_id							,			eos_token_id=self.eos_token_id							,			pad_token_id=self.pad_token_id							,			)

											return (config, input_ids, input_mask)



				def    __snake_case		(			self    ):
											lowerCAmelCase =		self.prepare_config_and_inputs()
											lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase =		config_and_inputs
											lowerCAmelCase =		{'''input_ids''': input_ids, '''attention_mask''': attention_mask}
											return config, inputs_dict



				def    __snake_case		(			self    ):
											lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase =		self.prepare_config_and_inputs()

											lowerCAmelCase =		floats_tensor([self.batch_size, self.seq_length, self.hidden_size]    )
											lowerCAmelCase =		ids_tensor([self.batch_size, self.seq_length]							,			vocab_size=2    )

											return (
											    config,
											    input_ids,
											    attention_mask,
											    encoder_hidden_states,
											    encoder_attention_mask,
											)



				def    __snake_case		(			self							,			UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_    ):
											lowerCAmelCase =		20
											lowerCAmelCase =		model_class_name(UpperCAmelCase_    )

											lowerCAmelCase =		model.init_cache(input_ids.shape[0]							,			UpperCAmelCase_    )
											lowerCAmelCase =		jnp.ones((input_ids.shape[0], max_decoder_length)							,			dtype='''i4'''    )

											lowerCAmelCase =		jnp.broadcast_to(
											    jnp.arange(input_ids.shape[-1] - 1    )[None, :]							,			(input_ids.shape[0], input_ids.shape[-1] - 1)    )
											lowerCAmelCase =		model(
											    input_ids[:, :-1]							,			attention_mask=UpperCAmelCase_							,			past_key_values=UpperCAmelCase_							,			position_ids=UpperCAmelCase_							,			)

											lowerCAmelCase =		jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]							,			dtype='''i4'''    )
											lowerCAmelCase =		model(
											    input_ids[:, -1:]							,			attention_mask=UpperCAmelCase_							,			past_key_values=outputs_cache.past_key_values							,			position_ids=UpperCAmelCase_							,			)

											lowerCAmelCase =		model(UpperCAmelCase_    )

											lowerCAmelCase =		np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])    )    )
											self.parent.assertTrue(diff < 1E-3							,			msg=F"""Max diff is {diff}"""    )






				def    __snake_case		(			self							,			UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_    ):
											lowerCAmelCase =		20
											lowerCAmelCase =		model_class_name(UpperCAmelCase_    )

											lowerCAmelCase =		jnp.concatenate(
											    [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1])    )]							,			axis=-1							,			)

											lowerCAmelCase =		model.init_cache(input_ids.shape[0]							,			UpperCAmelCase_    )
											lowerCAmelCase =		jnp.broadcast_to(
											    jnp.arange(input_ids.shape[-1] - 1    )[None, :]							,			(input_ids.shape[0], input_ids.shape[-1] - 1)    )

											lowerCAmelCase =		model(
											    input_ids[:, :-1]							,			attention_mask=UpperCAmelCase_							,			past_key_values=UpperCAmelCase_							,			position_ids=UpperCAmelCase_							,			)
											lowerCAmelCase =		jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]							,			dtype='''i4'''    )
											lowerCAmelCase =		model(
											    input_ids[:, -1:]							,			past_key_values=outputs_cache.past_key_values							,			attention_mask=UpperCAmelCase_							,			position_ids=UpperCAmelCase_							,			)

											lowerCAmelCase =		model(UpperCAmelCase_							,			attention_mask=UpperCAmelCase_    )

											lowerCAmelCase =		np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])    )    )
											self.parent.assertTrue(diff < 1E-3							,			msg=F"""Max diff is {diff}"""    )




@require_flax
class 				__UpperCamelCase      (			__UpperCAmelCase  ,       __UpperCAmelCase  ,       unittest.TestCase	):





				'''simple docstring'''



				__a				:			int					    =(FlaxGPTaModel, FlaxGPTaLMHeadModel) if is_flax_available() else ()
				__a				:			Dict					    =(FlaxGPTaLMHeadModel,) if is_flax_available() else ()



				def    __snake_case		(			self    ):
											lowerCAmelCase =		FlaxGPTaModelTester(self    )



				def    __snake_case		(			self    ):
											for model_class_name in self.all_model_classes:
																		lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase =		self.model_tester.prepare_config_and_inputs()
																		self.model_tester.check_use_cache_forward(UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_    )



				def    __snake_case		(			self    ):
											for model_class_name in self.all_model_classes:
																		lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase =		self.model_tester.prepare_config_and_inputs()
																		self.model_tester.check_use_cache_forward_with_attn_mask(
																		    UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_    )



				@slow
				def    __snake_case		(			self    ):
											lowerCAmelCase =		GPTaTokenizer.from_pretrained('''gpt2'''							,			pad_token='''</s>'''							,			padding_side='''left'''    )
											lowerCAmelCase =		tokenizer(['''Hello this is a long string''', '''Hey''']							,			return_tensors='''np'''							,			padding=UpperCAmelCase_							,			truncation=UpperCAmelCase_    )

											lowerCAmelCase =		FlaxGPTaLMHeadModel.from_pretrained('''gpt2'''    )
											lowerCAmelCase =		False
											lowerCAmelCase =		model.config.eos_token_id

											lowerCAmelCase =		jax.jit(model.generate    )

											lowerCAmelCase =		jit_generate(inputs['''input_ids''']							,			attention_mask=inputs['''attention_mask''']    ).sequences

											lowerCAmelCase =		tokenizer.batch_decode(UpperCAmelCase_							,			skip_special_tokens=UpperCAmelCase_    )

											lowerCAmelCase =		[
											    '''Hello this is a long string of words. I\'m going to start with the first one.\n''',
											    '''Hey, I\'m not sure if I\'m going to be able to do''',
											]

											self.assertListEqual(UpperCAmelCase_							,			UpperCAmelCase_    )



				@is_pt_flax_cross_test
				def    __snake_case		(			self    ):
											lowerCAmelCase ,  lowerCAmelCase =		self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		with self.subTest(model_class.__name__    ):
																									# prepare inputs
																									lowerCAmelCase =		self._prepare_for_class(UpperCAmelCase_							,			UpperCAmelCase_    )
																									lowerCAmelCase =		{k: torch.tensor(v.tolist()    ) for k, v in prepared_inputs_dict.items()}

																									# load corresponding PyTorch class
																									lowerCAmelCase =		model_class.__name__[4:]  # Skip the "Flax" at the beginning
																									lowerCAmelCase =		getattr(UpperCAmelCase_							,			UpperCAmelCase_    )

																									lowerCAmelCase ,  lowerCAmelCase =		pt_inputs['''input_ids'''].shape
																									lowerCAmelCase =		np.random.randint(0							,			seq_length - 1							,			size=(batch_size,)    )
																									for batch_idx, start_index in enumerate(UpperCAmelCase_    ):
																																lowerCAmelCase =		0
																																lowerCAmelCase =		1
																																lowerCAmelCase =		0
																																lowerCAmelCase =		1
																									lowerCAmelCase =		pt_model_class(UpperCAmelCase_    ).eval()
																									lowerCAmelCase =		model_class(UpperCAmelCase_							,			dtype=jnp.floataa    )

																									lowerCAmelCase =		convert_pytorch_state_dict_to_flax(pt_model.state_dict()							,			UpperCAmelCase_    )
																									lowerCAmelCase =		fx_state

																									with torch.no_grad():
																																lowerCAmelCase =		pt_model(**UpperCAmelCase_    ).to_tuple()

																									lowerCAmelCase =		fx_model(**UpperCAmelCase_    ).to_tuple()
																									self.assertEqual(len(UpperCAmelCase_    )							,			len(UpperCAmelCase_    )							,			'''Output lengths differ between Flax and PyTorch'''    )
																									for fx_output, pt_output in zip(UpperCAmelCase_							,			UpperCAmelCase_    ):
																																self.assert_almost_equals(fx_output[:, -1]							,			pt_output[:, -1].numpy()							,			4E-2    )

																									with tempfile.TemporaryDirectory() as tmpdirname:
																																pt_model.save_pretrained(UpperCAmelCase_    )
																																lowerCAmelCase =		model_class.from_pretrained(UpperCAmelCase_							,			from_pt=UpperCAmelCase_    )

																									lowerCAmelCase =		fx_model_loaded(**UpperCAmelCase_    ).to_tuple()
																									self.assertEqual(
																									    len(UpperCAmelCase_    )							,			len(UpperCAmelCase_    )							,			'''Output lengths differ between Flax and PyTorch'''    )
																									for fx_output_loaded, pt_output in zip(UpperCAmelCase_							,			UpperCAmelCase_    ):
																																self.assert_almost_equals(fx_output_loaded[:, -1]							,			pt_output[:, -1].numpy()							,			4E-2    )



				@is_pt_flax_cross_test
				def    __snake_case		(			self    ):
											lowerCAmelCase ,  lowerCAmelCase =		self.model_tester.prepare_config_and_inputs_for_common()
											for model_class in self.all_model_classes:
																		with self.subTest(model_class.__name__    ):
																									# prepare inputs
																									lowerCAmelCase =		self._prepare_for_class(UpperCAmelCase_							,			UpperCAmelCase_    )
																									lowerCAmelCase =		{k: torch.tensor(v.tolist()    ) for k, v in prepared_inputs_dict.items()}

																									# load corresponding PyTorch class
																									lowerCAmelCase =		model_class.__name__[4:]  # Skip the "Flax" at the beginning
																									lowerCAmelCase =		getattr(UpperCAmelCase_							,			UpperCAmelCase_    )

																									lowerCAmelCase =		pt_model_class(UpperCAmelCase_    ).eval()
																									lowerCAmelCase =		model_class(UpperCAmelCase_							,			dtype=jnp.floataa    )

																									lowerCAmelCase =		load_flax_weights_in_pytorch_model(UpperCAmelCase_							,			fx_model.params    )
																									lowerCAmelCase ,  lowerCAmelCase =		pt_inputs['''input_ids'''].shape
																									lowerCAmelCase =		np.random.randint(0							,			seq_length - 1							,			size=(batch_size,)    )
																									for batch_idx, start_index in enumerate(UpperCAmelCase_    ):
																																lowerCAmelCase =		0
																																lowerCAmelCase =		1
																																lowerCAmelCase =		0
																																lowerCAmelCase =		1

																									# make sure weights are tied in PyTorch
																									pt_model.tie_weights()

																									with torch.no_grad():
																																lowerCAmelCase =		pt_model(**UpperCAmelCase_    ).to_tuple()

																									lowerCAmelCase =		fx_model(**UpperCAmelCase_    ).to_tuple()
																									self.assertEqual(len(UpperCAmelCase_    )							,			len(UpperCAmelCase_    )							,			'''Output lengths differ between Flax and PyTorch'''    )
																									for fx_output, pt_output in zip(UpperCAmelCase_							,			UpperCAmelCase_    ):
																																self.assert_almost_equals(fx_output[:, -1]							,			pt_output[:, -1].numpy()							,			4E-2    )

																									with tempfile.TemporaryDirectory() as tmpdirname:
																																fx_model.save_pretrained(UpperCAmelCase_    )
																																lowerCAmelCase =		pt_model_class.from_pretrained(UpperCAmelCase_							,			from_flax=UpperCAmelCase_    )

																									with torch.no_grad():
																																lowerCAmelCase =		pt_model_loaded(**UpperCAmelCase_    ).to_tuple()

																									self.assertEqual(
																									    len(UpperCAmelCase_    )							,			len(UpperCAmelCase_    )							,			'''Output lengths differ between Flax and PyTorch'''    )
																									for fx_output, pt_output in zip(UpperCAmelCase_							,			UpperCAmelCase_    ):
																																self.assert_almost_equals(fx_output[:, -1]							,			pt_output[:, -1].numpy()							,			4E-2    )






				@slow
				def    __snake_case		(			self    ):
											for model_class_name in self.all_model_classes:
																		lowerCAmelCase =		model_class_name.from_pretrained('''gpt2'''							,			from_pt=UpperCAmelCase_    )
																		lowerCAmelCase =		model(np.ones((1, 1)    )    )
																		self.assertIsNotNone(UpperCAmelCase_    )









from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import ConvNextConfig
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 TFConvNextForImageClassification, TFConvNextModel


if is_vision_available():
		from PIL import Image

		from transformers import ConvNextImageProcessor


class 				__UpperCamelCase      :





				'''simple docstring'''



				def __init__(			self							,			UpperCAmelCase_							,			UpperCAmelCase_=13							,			UpperCAmelCase_=32							,			UpperCAmelCase_=3							,			UpperCAmelCase_=4							,			UpperCAmelCase_=[10, 20, 30, 40]							,			UpperCAmelCase_=[2, 2, 3, 2]							,			UpperCAmelCase_=True							,			UpperCAmelCase_=True							,			UpperCAmelCase_=37							,			UpperCAmelCase_="gelu"							,			UpperCAmelCase_=10							,			UpperCAmelCase_=0.02							,			UpperCAmelCase_=3							,			UpperCAmelCase_=None							,			):
											lowerCAmelCase =		parent
											lowerCAmelCase =		batch_size
											lowerCAmelCase =		image_size
											lowerCAmelCase =		num_channels
											lowerCAmelCase =		num_stages
											lowerCAmelCase =		hidden_sizes
											lowerCAmelCase =		depths
											lowerCAmelCase =		is_training
											lowerCAmelCase =		use_labels
											lowerCAmelCase =		intermediate_size
											lowerCAmelCase =		hidden_act
											lowerCAmelCase =		type_sequence_label_size
											lowerCAmelCase =		initializer_range
											lowerCAmelCase =		scope



				def    __snake_case		(			self    ):
											lowerCAmelCase =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

											lowerCAmelCase =		None
											if self.use_labels:
																		lowerCAmelCase =		ids_tensor([self.batch_size]							,			self.type_sequence_label_size    )

											lowerCAmelCase =		self.get_config()

											return config, pixel_values, labels



				def    __snake_case		(			self    ):
											return ConvNextConfig(
											    num_channels=self.num_channels							,			hidden_sizes=self.hidden_sizes							,			depths=self.depths							,			num_stages=self.num_stages							,			hidden_act=self.hidden_act							,			is_decoder=UpperCAmelCase_							,			initializer_range=self.initializer_range							,			)



				def    __snake_case		(			self							,			UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_    ):
											lowerCAmelCase =		TFConvNextModel(config=UpperCAmelCase_    )
											lowerCAmelCase =		model(UpperCAmelCase_							,			training=UpperCAmelCase_    )
											# expected last hidden states: B, C, H // 32, W // 32
											self.parent.assertEqual(
											    result.last_hidden_state.shape							,			(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)							,			)



				def    __snake_case		(			self							,			UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_    ):
											lowerCAmelCase =		self.type_sequence_label_size
											lowerCAmelCase =		TFConvNextForImageClassification(UpperCAmelCase_    )
											lowerCAmelCase =		model(UpperCAmelCase_							,			labels=UpperCAmelCase_							,			training=UpperCAmelCase_    )
											self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.type_sequence_label_size)    )






				def    __snake_case		(			self    ):
											lowerCAmelCase =		self.prepare_config_and_inputs()
											lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase =		config_and_inputs
											lowerCAmelCase =		{'''pixel_values''': pixel_values}
											return config, inputs_dict




@require_tf
class 				__UpperCamelCase      (			__UpperCAmelCase  ,       __UpperCAmelCase  ,       unittest.TestCase	):





				'''simple docstring'''



				__a				:			Union[str, Any]					    =(TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
				__a				:			Optional[int]					    =(
				    {"""feature-extraction""": TFConvNextModel, """image-classification""": TFConvNextForImageClassification}
				    if is_tf_available()
				    else {}
				)

				__a				:			Any					    =False
				__a				:			int					    =False
				__a				:			Union[str, Any]					    =False
				__a				:			Optional[Any]					    =False
				__a				:			List[Any]					    =False



				def    __snake_case		(			self    ):
											lowerCAmelCase =		TFConvNextModelTester(self    )
											lowerCAmelCase =		ConfigTester(
											    self							,			config_class=UpperCAmelCase_							,			has_text_modality=UpperCAmelCase_							,			hidden_size=37							,			)



				@unittest.skip(reason='''ConvNext does not use inputs_embeds'''    )
				def    __snake_case		(			self    ):
											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.'''							,			)
				@slow
				def    __snake_case		(			self    ):
											super().test_keras_fit()



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



				def    __snake_case		(			self    ):
											lowerCAmelCase ,  lowerCAmelCase =		self.model_tester.prepare_config_and_inputs_for_common()

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

																		lowerCAmelCase =		['''pixel_values''']
																		self.assertListEqual(arg_names[:1]							,			UpperCAmelCase_    )



				def    __snake_case		(			self    ):
											lowerCAmelCase =		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*UpperCAmelCase_    )



				@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    __snake_case		(			self    ):
											super().test_dataset_conversion()



				def    __snake_case		(			self    ):
											def check_hidden_states_output(UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_    ):
																		lowerCAmelCase =		model_class(UpperCAmelCase_    )

																		lowerCAmelCase =		model(**self._prepare_for_class(UpperCAmelCase_							,			UpperCAmelCase_    )    )
																		lowerCAmelCase =		outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																		lowerCAmelCase =		self.model_tester.num_stages
																		self.assertEqual(len(UpperCAmelCase_    )							,			expected_num_stages + 1    )

																		# ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
																		self.assertListEqual(
																		    list(hidden_states[0].shape[-2:]    )							,			[self.model_tester.image_size // 4, self.model_tester.image_size // 4]							,			)

											lowerCAmelCase ,  lowerCAmelCase =		self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		lowerCAmelCase =		True
																		check_hidden_states_output(UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_    )

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

																		check_hidden_states_output(UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_    )



				def    __snake_case		(			self    ):
											lowerCAmelCase ,  lowerCAmelCase =		self.model_tester.prepare_config_and_inputs_for_common()

											def check_equivalence(UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_={}    ):
																		lowerCAmelCase =		model(UpperCAmelCase_							,			return_dict=UpperCAmelCase_							,			**UpperCAmelCase_    )
																		lowerCAmelCase =		model(UpperCAmelCase_							,			return_dict=UpperCAmelCase_							,			**UpperCAmelCase_    ).to_tuple()

																		def recursive_check(UpperCAmelCase_							,			UpperCAmelCase_    ):
																									if isinstance(UpperCAmelCase_							,			(List, Tuple)    ):
																																for tuple_iterable_value, dict_iterable_value in zip(UpperCAmelCase_							,			UpperCAmelCase_    ):
																																							recursive_check(UpperCAmelCase_							,			UpperCAmelCase_    )
																									elif tuple_object is None:
																																return
																									else:
																																self.assertTrue(
																																    all(tf.equal(UpperCAmelCase_							,			UpperCAmelCase_    )    )							,			msg=(
																																        '''Tuple and dict output are not equal. Difference:'''
																																        F""" {tf.math.reduce_max(tf.abs(tuple_object - dict_object    )    )}"""
																																    )							,			)

																									recursive_check(UpperCAmelCase_							,			UpperCAmelCase_    )

											for model_class in self.all_model_classes:
																		lowerCAmelCase =		model_class(UpperCAmelCase_    )

																		lowerCAmelCase =		self._prepare_for_class(UpperCAmelCase_							,			UpperCAmelCase_    )
																		lowerCAmelCase =		self._prepare_for_class(UpperCAmelCase_							,			UpperCAmelCase_    )
																		check_equivalence(UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_    )

																		lowerCAmelCase =		self._prepare_for_class(UpperCAmelCase_							,			UpperCAmelCase_							,			return_labels=UpperCAmelCase_    )
																		lowerCAmelCase =		self._prepare_for_class(UpperCAmelCase_							,			UpperCAmelCase_							,			return_labels=UpperCAmelCase_    )
																		check_equivalence(UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_    )

																		lowerCAmelCase =		self._prepare_for_class(UpperCAmelCase_							,			UpperCAmelCase_    )
																		lowerCAmelCase =		self._prepare_for_class(UpperCAmelCase_							,			UpperCAmelCase_    )
																		check_equivalence(UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_							,			{'''output_hidden_states''': True}    )

																		lowerCAmelCase =		self._prepare_for_class(UpperCAmelCase_							,			UpperCAmelCase_							,			return_labels=UpperCAmelCase_    )
																		lowerCAmelCase =		self._prepare_for_class(UpperCAmelCase_							,			UpperCAmelCase_							,			return_labels=UpperCAmelCase_    )
																		check_equivalence(UpperCAmelCase_							,			UpperCAmelCase_							,			UpperCAmelCase_							,			{'''output_hidden_states''': True}    )



				def    __snake_case		(			self    ):
											lowerCAmelCase =		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_    )






				@slow
				def    __snake_case		(			self    ):
											lowerCAmelCase =		TFConvNextModel.from_pretrained('''facebook/convnext-tiny-224'''    )
											self.assertIsNotNone(UpperCAmelCase_    )




def    UpperCAmelCase							(  ):
							lowerCAmelCase =		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
							return image


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





				'''simple docstring'''



				@cached_property
				def    __snake_case		(			self    ):
											return ConvNextImageProcessor.from_pretrained('''facebook/convnext-tiny-224'''    ) if is_vision_available() else None






				@slow
				def    __snake_case		(			self    ):
											lowerCAmelCase =		TFConvNextForImageClassification.from_pretrained('''facebook/convnext-tiny-224'''    )

											lowerCAmelCase =		self.default_image_processor
											lowerCAmelCase =		prepare_img()
											lowerCAmelCase =		image_processor(images=UpperCAmelCase_							,			return_tensors='''tf'''    )

											# forward pass
											lowerCAmelCase =		model(**UpperCAmelCase_    )

											# verify the logits
											lowerCAmelCase =		tf.TensorShape((1, 10_00)    )
											self.assertEqual(outputs.logits.shape							,			UpperCAmelCase_    )

											lowerCAmelCase =		tf.constant([-0.0260, -0.4739, 0.1911]    )

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



