


import unittest

import numpy as np
import timeout_decorator  # noqa

from transformers import OPTConfig, is_flax_available
from transformers.testing_utils import require_flax, require_sentencepiece, slow

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


if is_flax_available():
						import os

						# The slow tests are often failing with OOM error on GPU
						# This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed
						# but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html
						_UpperCAmelCase     :					Any       ="""platform"""

						import jax
						import jax.numpy as jnp

						from transformers import FlaxOPTForCausalLM, FlaxOPTModel, GPTaTokenizer



def      lowerCAmelCase	(						lowerCAmelCase_	,				lowerCAmelCase_	,				lowerCAmelCase_=None	,				lowerCAmelCase_=None	)->	int:
				if attention_mask is None:
								lowerCAmelCase_	: str      			=			np.where(input_ids != config.pad_token_id	,				1	,				0	)
				return {
				    "input_ids": input_ids,
				    "attention_mask": attention_mask,
				}







@require_flax
class 			snake_case__:







						'''simple docstring'''




						def __init__(			self				,  __lowercase				,  __lowercase=1_3				,  __lowercase=7				,  __lowercase=True				,  __lowercase=False				,  __lowercase=9_9				,  __lowercase=1_6				,  __lowercase=2				,  __lowercase=4				,  __lowercase=4				,  __lowercase="gelu"				,  __lowercase=0.1				,  __lowercase=0.1				,  __lowercase=2_0				,  __lowercase=2				,  __lowercase=1				,  __lowercase=0				,  __lowercase=1_6				,  __lowercase=1_6				,  __lowercase=0.02				,  )   ->      str:
										lowerCAmelCase_	: str      			=			parent
										lowerCAmelCase_	: Tuple      			=			batch_size
										lowerCAmelCase_	: Any      			=			seq_length
										lowerCAmelCase_	: Tuple      			=			is_training
										lowerCAmelCase_	: Tuple      			=			use_labels
										lowerCAmelCase_	: str      			=			vocab_size
										lowerCAmelCase_	: Union[str, Any]      			=			hidden_size
										lowerCAmelCase_	: Tuple      			=			num_hidden_layers
										lowerCAmelCase_	: Union[str, Any]      			=			num_attention_heads
										lowerCAmelCase_	: str      			=			intermediate_size
										lowerCAmelCase_	: Optional[Any]      			=			hidden_act
										lowerCAmelCase_	: List[Any]      			=			hidden_dropout_prob
										lowerCAmelCase_	: Optional[Any]      			=			attention_probs_dropout_prob
										lowerCAmelCase_	: int      			=			max_position_embeddings
										lowerCAmelCase_	: int      			=			eos_token_id
										lowerCAmelCase_	: Any      			=			pad_token_id
										lowerCAmelCase_	: Any      			=			bos_token_id
										lowerCAmelCase_	: Dict      			=			embed_dim
										lowerCAmelCase_	: Any      			=			word_embed_proj_dim
										lowerCAmelCase_	: List[str]      			=			initializer_range
										lowerCAmelCase_	: Union[str, Any]      			=			False




						def   lowercase_	(			self							)   ->      str:
										lowerCAmelCase_	: Optional[Any]      			=			np.clip(ids_tensor([self.batch_size, self.seq_length - 1]				,  self.vocab_size							)				,  3				,  self.vocab_size							)
										lowerCAmelCase_	: str      			=			np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1)				,  dtype=np.intaa							))				,  -1							)

										lowerCAmelCase_	: Union[str, Any]      			=			OPTConfig(
										    vocab_size=self.vocab_size				,  hidden_size=self.hidden_size				,  num_hidden_layers=self.num_hidden_layers				,  num_attention_heads=self.num_attention_heads				,  ffn_dim=self.intermediate_size				,  dropout=self.hidden_dropout_prob				,  attention_dropout=self.attention_probs_dropout_prob				,  max_position_embeddings=self.max_position_embeddings				,  eos_token_id=self.eos_token_id				,  bos_token_id=self.bos_token_id				,  pad_token_id=self.pad_token_id				,  embed_dim=self.embed_dim				,  is_encoder_decoder=__lowercase				,  word_embed_proj_dim=self.word_embed_proj_dim				,  initializer_range=self.initializer_range				,  use_cache=__lowercase				,  )
										lowerCAmelCase_	: str      			=			prepare_opt_inputs_dict(__lowercase				,  __lowercase							)
										return config, inputs_dict




						def   lowercase_	(			self							)   ->      List[Any]:
										lowerCAmelCase_			,					lowerCAmelCase_	: Union[str, Any]      			=			self.prepare_config_and_inputs()
										return config, inputs_dict




						def   lowercase_	(			self				,  __lowercase				,  __lowercase				,  __lowercase							)   ->      Optional[Any]:
										lowerCAmelCase_	: Tuple      			=			2_0
										lowerCAmelCase_	: Tuple      			=			model_class_name(__lowercase							)

										lowerCAmelCase_	: Any      			=			inputs_dict['''input_ids''']
										lowerCAmelCase_	: List[str]      			=			inputs_dict['''attention_mask''']

										lowerCAmelCase_	: List[str]      			=			model.init_cache(input_ids.shape[0]				,  __lowercase							)
										lowerCAmelCase_	: int      			=			jnp.ones((input_ids.shape[0], max_length)				,  dtype='''i4'''							)

										lowerCAmelCase_	: Tuple      			=			jnp.broadcast_to(
										    jnp.arange(input_ids.shape[-1] - 1							)[None, :]				,  (input_ids.shape[0], input_ids.shape[-1] - 1)				,  )
										lowerCAmelCase_	: Optional[int]      			=			model(
										    input_ids[:, :-1]				,  attention_mask=__lowercase				,  past_key_values=__lowercase				,  position_ids=__lowercase				,  )

										lowerCAmelCase_	: Union[str, Any]      			=			jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]				,  dtype='''i4'''							)
										lowerCAmelCase_	: Optional[Any]      			=			model(
										    input_ids[:, -1:]				,  attention_mask=__lowercase				,  past_key_values=outputs_cache.past_key_values				,  position_ids=__lowercase				,  )

										lowerCAmelCase_	: Any      			=			model(__lowercase							)

										lowerCAmelCase_	: int      			=			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   lowercase_	(			self				,  __lowercase				,  __lowercase				,  __lowercase							)   ->      List[Any]:
										lowerCAmelCase_	: List[Any]      			=			2_0
										lowerCAmelCase_	: List[str]      			=			model_class_name(__lowercase							)

										lowerCAmelCase_			,					lowerCAmelCase_	: str      			=			(
										    inputs_dict['''input_ids'''],
										    inputs_dict['''attention_mask'''],
										)

										lowerCAmelCase_	: Optional[Any]      			=			jnp.concatenate(
										    [
										        attention_mask,
										        jnp.zeros((attention_mask.shape[0], max_length - attention_mask.shape[1])							),
										    ]				,  axis=-1				,  )

										lowerCAmelCase_	: int      			=			model.init_cache(input_ids.shape[0]				,  __lowercase							)
										lowerCAmelCase_	: List[str]      			=			jnp.broadcast_to(
										    jnp.arange(input_ids.shape[-1] - 1							)[None, :]				,  (input_ids.shape[0], input_ids.shape[-1] - 1)				,  )

										lowerCAmelCase_	: int      			=			model(
										    input_ids[:, :-1]				,  attention_mask=__lowercase				,  past_key_values=__lowercase				,  position_ids=__lowercase				,  )
										lowerCAmelCase_	: Optional[int]      			=			jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]				,  dtype='''i4'''							)
										lowerCAmelCase_	: str      			=			model(
										    input_ids[:, -1:]				,  past_key_values=outputs_cache.past_key_values				,  attention_mask=__lowercase				,  position_ids=__lowercase				,  )

										lowerCAmelCase_	: Tuple      			=			model(__lowercase				,  attention_mask=__lowercase							)

										lowerCAmelCase_	: Any      			=			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 			snake_case__(							UpperCAmelCase__,  unittest.TestCase,  UpperCAmelCase__							):







						'''simple docstring'''




						SCREAMING_SNAKE_CASE__ :					int   =							(FlaxOPTModel, FlaxOPTForCausalLM) if is_flax_available() else ()
						SCREAMING_SNAKE_CASE__ :					Union[str, Any]   =							() if is_flax_available() else ()




						def   lowercase_	(			self							)   ->      Dict:
										lowerCAmelCase_	: Optional[Any]      			=			FlaxOPTModelTester(self							)




						def   lowercase_	(			self							)   ->      Tuple:
										lowerCAmelCase_			,					lowerCAmelCase_	: Optional[Any]      			=			self.model_tester.prepare_config_and_inputs()
										for model_class in self.all_model_classes:
														self.model_tester.check_use_cache_forward(__lowercase				,  __lowercase				,  __lowercase							)




						def   lowercase_	(			self							)   ->      Dict:
										lowerCAmelCase_			,					lowerCAmelCase_	: Union[str, Any]      			=			self.model_tester.prepare_config_and_inputs()
										for model_class in self.all_model_classes:
														self.model_tester.check_use_cache_forward_with_attn_mask(__lowercase				,  __lowercase				,  __lowercase							)




						@slow
						def   lowercase_	(			self							)   ->      int:
										for model_class_name in self.all_model_classes:
														lowerCAmelCase_	: Optional[int]      			=			model_class_name.from_pretrained('''facebook/opt-125m'''							)
														lowerCAmelCase_	: List[Any]      			=			np.ones((1, 1)							) * model.config.eos_token_id
														lowerCAmelCase_	: Dict      			=			model(__lowercase							)
														self.assertIsNotNone(__lowercase							)







@require_sentencepiece
@require_flax
class 			snake_case__(							unittest.TestCase							):







						'''simple docstring'''




						@slow
						def   lowercase_	(			self							)   ->      List[str]:
										lowerCAmelCase_	: Optional[int]      			=			FlaxOPTModel.from_pretrained('''facebook/opt-350m'''							)
										lowerCAmelCase_	: Union[str, Any]      			=			jnp.array([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]]							)
										lowerCAmelCase_	: List[Any]      			=			model(input_ids=__lowercase							).last_hidden_state
										lowerCAmelCase_	: List[str]      			=			(1, 1_1, 5_1_2)
										self.assertEqual(output.shape				,  __lowercase							)
										lowerCAmelCase_	: Any      			=			jnp.array(
										    [[-0.28_67, -1.92_56, -0.30_62], [-1.27_11, -0.13_37, -0.18_97], [0.41_09, 0.11_87, -1.31_42]]							)
										self.assertTrue(jnp.allclose(output[:, :3, :3]				,  __lowercase				,  atol=4e-2							)							)







@require_flax
@slow
class 			snake_case__(							unittest.TestCase							):







						'''simple docstring'''




						def   lowercase_	(			self							)   ->      Union[str, Any]:
										super().setUp()
										lowerCAmelCase_	: Optional[int]      			=			'''facebook/opt-350m'''




						def   lowercase_	(			self							)   ->      str:
										lowerCAmelCase_	: Union[str, Any]      			=			FlaxOPTForCausalLM.from_pretrained(self.path_model							)
										lowerCAmelCase_	: Any      			=			GPTaTokenizer.from_pretrained(self.path_model							)

										lowerCAmelCase_	: Dict      			=			[
										    '''Today is a beautiful day and I want to''',
										    '''In the city of''',
										    '''Paris is the capital of France and''',
										    '''Computers and mobile phones have taken''',
										]
										# verify that prompt without BOS token is identical to Metaseq -> add_special_tokens=False
										lowerCAmelCase_	: int      			=			tokenizer(__lowercase				,  return_tensors='''jax'''				,  padding=__lowercase				,  add_special_tokens=__lowercase							)
										lowerCAmelCase_	: Tuple      			=			model(inputs.input_ids				,  attention_mask=inputs.attention_mask							)[0].mean(axis=-1							)
										lowerCAmelCase_	: List[str]      			=			jnp.array(
										    [
										        [1.38_51, -13.89_23, -10.52_29, -10.75_33, -0.23_09, -10.23_84, -0.53_65, -9.09_47, -5.16_70],
										        [-4.70_73, -10.62_76, -3.94_15, -21.52_42, -0.28_22, -0.28_22, -0.28_22, -0.28_22, -0.28_22],
										        [0.62_47, -3.42_29, -8.91_79, -1.42_97, -14.16_50, 1.41_46, -9.02_18, -0.27_03, -0.27_03],
										        [6.47_83, -1.99_13, -10.79_26, -2.33_36, 1.50_92, -0.99_74, -6.82_13, 1.34_77, 1.34_77],
										    ]							)
										self.assertTrue(jnp.allclose(__lowercase				,  __lowercase				,  atol=4e-2							)							)

										lowerCAmelCase_	: List[str]      			=			jax.jit(__lowercase							)
										lowerCAmelCase_	: int      			=			model(inputs.input_ids				,  attention_mask=inputs.attention_mask							)[0].mean(axis=-1							)
										self.assertTrue(jnp.allclose(__lowercase				,  __lowercase				,  atol=4e-2							)							)







@require_flax
@slow
class 			snake_case__(							unittest.TestCase							):







						'''simple docstring'''




						@property
						def   lowercase_	(			self							)   ->      Dict:
										return [
										    "Today is a beautiful day and I want",
										    "In the city of",
										    "Paris is the capital of France and",
										    "Computers and mobile phones have taken",
										]




						def   lowercase_	(			self							)   ->      str:
										lowerCAmelCase_	: Dict      			=			'''facebook/opt-125m'''

										lowerCAmelCase_	: List[Any]      			=			[
										    '''Today is a beautiful day and I want to''',
										    '''In the city of New York, the city''',
										    '''Paris is the capital of France and the capital''',
										    '''Computers and mobile phones have taken over the''',
										]

										lowerCAmelCase_	: Dict      			=			[]

										lowerCAmelCase_	: Any      			=			FlaxOPTForCausalLM.from_pretrained(__lowercase							)
										lowerCAmelCase_	: Optional[int]      			=			GPTaTokenizer.from_pretrained(__lowercase							)

										for prompt in self.prompts:
														lowerCAmelCase_	: List[Any]      			=			tokenizer(__lowercase				,  return_tensors='''jax'''							).input_ids

														lowerCAmelCase_	: Optional[int]      			=			model.generate(__lowercase				,  max_length=1_0							)
														lowerCAmelCase_	: Tuple      			=			generated_ids[0]

														lowerCAmelCase_	: List[Any]      			=			tokenizer.batch_decode(__lowercase				,  skip_special_tokens=__lowercase							)
														predicted_outputs += generated_string

										self.assertListEqual(__lowercase				,  __lowercase							)




						def   lowercase_	(			self							)   ->      Optional[Any]:
										lowerCAmelCase_	: int      			=			'''facebook/opt-350m'''

										lowerCAmelCase_	: Optional[int]      			=			[
										    '''Today is a beautiful day and I want to''',
										    '''In the city of San Francisco, the city''',
										    '''Paris is the capital of France and the capital''',
										    '''Computers and mobile phones have taken over the''',
										]

										lowerCAmelCase_	: Optional[Any]      			=			[]
										lowerCAmelCase_	: int      			=			FlaxOPTForCausalLM.from_pretrained(__lowercase							)
										lowerCAmelCase_	: List[Any]      			=			GPTaTokenizer.from_pretrained(__lowercase							)

										for prompt in self.prompts:
														lowerCAmelCase_	: List[str]      			=			tokenizer(__lowercase				,  return_tensors='''jax'''							).input_ids

														lowerCAmelCase_	: Optional[Any]      			=			model.generate(__lowercase				,  max_length=1_0							)
														lowerCAmelCase_	: Optional[Any]      			=			generated_ids[0]

														lowerCAmelCase_	: Any      			=			tokenizer.batch_decode(__lowercase				,  skip_special_tokens=__lowercase							)
														predicted_outputs += generated_string

										self.assertListEqual(__lowercase				,  __lowercase							)




						def   lowercase_	(			self							)   ->      Any:
										lowerCAmelCase_	: Union[str, Any]      			=			'''facebook/opt-125m'''
										lowerCAmelCase_	: Optional[Any]      			=			[
										    '''Today is a beautiful day and I want to thank''',
										    '''In the city of Rome Canaver Canaver Canaver Canaver''',
										]
										lowerCAmelCase_	: Optional[int]      			=			FlaxOPTForCausalLM.from_pretrained(__lowercase							)
										lowerCAmelCase_	: Tuple      			=			GPTaTokenizer.from_pretrained(__lowercase							)
										lowerCAmelCase_	: str      			=			tokenizer(
										    [
										        '''Today is a beautiful day and I want to''',
										        '''In the city of''',
										    ]				,  return_tensors='''jax'''				,  padding=__lowercase				,  )

										lowerCAmelCase_	: str      			=			jax.jit(model.generate							)

										lowerCAmelCase_	: List[Any]      			=			jit_generate(inputs['''input_ids''']				,  attention_mask=inputs['''attention_mask''']							).sequences

										lowerCAmelCase_	: Optional[Any]      			=			tokenizer.batch_decode(__lowercase				,  skip_special_tokens=__lowercase							)

										self.assertIsNotNone(__lowercase				,  __lowercase							)




						def   lowercase_	(			self							)   ->      Optional[Any]:
										lowerCAmelCase_	: Union[str, Any]      			=			'''facebook/opt-350m'''

										lowerCAmelCase_	: Dict      			=			GPTaTokenizer.from_pretrained(__lowercase							)
										lowerCAmelCase_	: Dict      			=			FlaxOPTForCausalLM.from_pretrained(__lowercase							)

										lowerCAmelCase_	: Optional[Any]      			=			'''left'''

										# use different length sentences to test batching
										lowerCAmelCase_	: List[Any]      			=			[
										    '''Hello, my dog is a little''',
										    '''Today, I''',
										]

										lowerCAmelCase_	: str      			=			tokenizer(__lowercase				,  return_tensors='''jax'''				,  padding=__lowercase							)
										lowerCAmelCase_	: Tuple      			=			inputs['''input_ids''']

										lowerCAmelCase_	: List[str]      			=			model.generate(input_ids=__lowercase				,  attention_mask=inputs['''attention_mask''']				,  trace=__lowercase							)

										lowerCAmelCase_	: List[str]      			=			tokenizer(sentences[0]				,  return_tensors='''jax'''							).input_ids
										lowerCAmelCase_	: Optional[Any]      			=			model.generate(input_ids=__lowercase							)

										lowerCAmelCase_	: Optional[int]      			=			inputs_non_padded.shape[-1] - inputs['''attention_mask'''][-1].sum()
										lowerCAmelCase_	: Tuple      			=			tokenizer(sentences[1]				,  return_tensors='''jax'''							).input_ids
										lowerCAmelCase_	: int      			=			model.generate(input_ids=__lowercase				,  max_length=model.config.max_length - num_paddings							)

										lowerCAmelCase_	: List[Any]      			=			tokenizer.batch_decode(outputs[0]				,  skip_special_tokens=__lowercase							)
										lowerCAmelCase_	: str      			=			tokenizer.decode(output_non_padded[0][0]				,  skip_special_tokens=__lowercase							)
										lowerCAmelCase_	: Optional[Any]      			=			tokenizer.decode(output_padded[0][0]				,  skip_special_tokens=__lowercase							)

										lowerCAmelCase_	: List[str]      			=			[
										    '''Hello, my dog is a little bit of a dork.\nI\'m a little bit''',
										    '''Today, I was in the middle of a conversation with a friend about the''',
										]
										self.assertListEqual(__lowercase				,  __lowercase							)
										self.assertListEqual(__lowercase				,  [non_padded_sentence, padded_sentence]							)



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 			snake_case__:







						'''simple docstring'''




						def __init__(			self				,  __lowercase				,  __lowercase=1_3				,  __lowercase=3_2				,  __lowercase=3				,  __lowercase=4				,  __lowercase=[1_0, 2_0, 3_0, 4_0]				,  __lowercase=[2, 2, 3, 2]				,  __lowercase=True				,  __lowercase=True				,  __lowercase=3_7				,  __lowercase="gelu"				,  __lowercase=1_0				,  __lowercase=0.02				,  __lowercase=3				,  __lowercase=None				,  )   ->      List[str]:
										lowerCAmelCase_	: List[Any]      			=			parent
										lowerCAmelCase_	: int      			=			batch_size
										lowerCAmelCase_	: Optional[int]      			=			image_size
										lowerCAmelCase_	: Dict      			=			num_channels
										lowerCAmelCase_	: Dict      			=			num_stages
										lowerCAmelCase_	: Union[str, Any]      			=			hidden_sizes
										lowerCAmelCase_	: List[Any]      			=			depths
										lowerCAmelCase_	: str      			=			is_training
										lowerCAmelCase_	: Optional[int]      			=			use_labels
										lowerCAmelCase_	: List[str]      			=			intermediate_size
										lowerCAmelCase_	: int      			=			hidden_act
										lowerCAmelCase_	: Any      			=			type_sequence_label_size
										lowerCAmelCase_	: List[Any]      			=			initializer_range
										lowerCAmelCase_	: Union[str, Any]      			=			scope




						def   lowercase_	(			self							)   ->      List[Any]:
										lowerCAmelCase_	: str      			=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

										lowerCAmelCase_	: str      			=			self.get_config()

										return config, pixel_values, labels




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




						def   lowercase_	(			self				,  __lowercase				,  __lowercase				,  __lowercase							)   ->      str:
										lowerCAmelCase_	: List[str]      			=			TFConvNextModel(config=__lowercase							)
										lowerCAmelCase_	: str      			=			model(__lowercase				,  training=__lowercase							)
										# 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 // 3_2, self.image_size // 3_2)				,  )




						def   lowercase_	(			self				,  __lowercase				,  __lowercase				,  __lowercase							)   ->      Optional[int]:
										lowerCAmelCase_	: int      			=			self.type_sequence_label_size
										lowerCAmelCase_	: Dict      			=			TFConvNextForImageClassification(__lowercase							)
										lowerCAmelCase_	: Tuple      			=			model(__lowercase				,  labels=__lowercase				,  training=__lowercase							)
										self.parent.assertEqual(result.logits.shape				,  (self.batch_size, self.type_sequence_label_size)							)




						def   lowercase_	(			self							)   ->      Optional[Any]:
										lowerCAmelCase_	: Optional[Any]      			=			self.prepare_config_and_inputs()
										lowerCAmelCase_			,					lowerCAmelCase_			,					lowerCAmelCase_	: Optional[int]      			=			config_and_inputs
										lowerCAmelCase_	: Optional[int]      			=			{'''pixel_values''': pixel_values}
										return config, inputs_dict







@require_tf
class 			snake_case__(							UpperCAmelCase__,  UpperCAmelCase__,  unittest.TestCase							):







						'''simple docstring'''




						SCREAMING_SNAKE_CASE__ :					Optional[Any]   =							(TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
						SCREAMING_SNAKE_CASE__ :					Dict   =							(
						    {"""feature-extraction""": TFConvNextModel, """image-classification""": TFConvNextForImageClassification}
						    if is_tf_available()
						    else {}
						)

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




						def   lowercase_	(			self							)   ->      str:
										lowerCAmelCase_	: Optional[int]      			=			TFConvNextModelTester(self							)
										lowerCAmelCase_	: Dict      			=			ConfigTester(
										    self				,  config_class=__lowercase				,  has_text_modality=__lowercase				,  hidden_size=3_7				,  )




						@unittest.skip(reason='''ConvNext does not use inputs_embeds'''							)
						def   lowercase_	(			self							)   ->      str:
										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   lowercase_	(			self							)   ->      Union[str, Any]:
										super().test_keras_fit()




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




						def   lowercase_	(			self							)   ->      Dict:
										lowerCAmelCase_			,					lowerCAmelCase_	: Dict      			=			self.model_tester.prepare_config_and_inputs_for_common()

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

														lowerCAmelCase_	: int      			=			['''pixel_values''']
														self.assertListEqual(arg_names[:1]				,  __lowercase							)




						def   lowercase_	(			self							)   ->      List[str]:
										lowerCAmelCase_	: Any      			=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*__lowercase							)




						@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   lowercase_	(			self							)   ->      List[Any]:
										super().test_dataset_conversion()




						def   lowercase_	(			self							)   ->      List[Any]:
										def check_hidden_states_output(__lowercase				,  __lowercase				,  __lowercase							):
														lowerCAmelCase_	: Union[str, Any]      			=			model_class(__lowercase							)

														lowerCAmelCase_	: str      			=			model(**self._prepare_for_class(__lowercase				,  __lowercase							)							)
														lowerCAmelCase_	: Optional[int]      			=			outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

														lowerCAmelCase_	: Any      			=			self.model_tester.num_stages
														self.assertEqual(len(__lowercase							)				,  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_	: List[Any]      			=			self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
														lowerCAmelCase_	: Tuple      			=			True
														check_hidden_states_output(__lowercase				,  __lowercase				,  __lowercase							)

														# check that output_hidden_states also work using config
														del inputs_dict["output_hidden_states"]
														lowerCAmelCase_	: str      			=			True

														check_hidden_states_output(__lowercase				,  __lowercase				,  __lowercase							)




						def   lowercase_	(			self							)   ->      Optional[int]:
										lowerCAmelCase_			,					lowerCAmelCase_	: str      			=			self.model_tester.prepare_config_and_inputs_for_common()

										def check_equivalence(__lowercase				,  __lowercase				,  __lowercase				,  __lowercase={}							):
														lowerCAmelCase_	: str      			=			model(__lowercase				,  return_dict=__lowercase				,  **__lowercase							)
														lowerCAmelCase_	: int      			=			model(__lowercase				,  return_dict=__lowercase				,  **__lowercase							).to_tuple()

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

																		recursive_check(__lowercase				,  __lowercase							)

										for model_class in self.all_model_classes:
														lowerCAmelCase_	: Optional[int]      			=			model_class(__lowercase							)

														lowerCAmelCase_	: Optional[int]      			=			self._prepare_for_class(__lowercase				,  __lowercase							)
														lowerCAmelCase_	: Tuple      			=			self._prepare_for_class(__lowercase				,  __lowercase							)
														check_equivalence(__lowercase				,  __lowercase				,  __lowercase							)

														lowerCAmelCase_	: Any      			=			self._prepare_for_class(__lowercase				,  __lowercase				,  return_labels=__lowercase							)
														lowerCAmelCase_	: List[str]      			=			self._prepare_for_class(__lowercase				,  __lowercase				,  return_labels=__lowercase							)
														check_equivalence(__lowercase				,  __lowercase				,  __lowercase							)

														lowerCAmelCase_	: List[str]      			=			self._prepare_for_class(__lowercase				,  __lowercase							)
														lowerCAmelCase_	: int      			=			self._prepare_for_class(__lowercase				,  __lowercase							)
														check_equivalence(__lowercase				,  __lowercase				,  __lowercase				,  {'''output_hidden_states''': True}							)

														lowerCAmelCase_	: Any      			=			self._prepare_for_class(__lowercase				,  __lowercase				,  return_labels=__lowercase							)
														lowerCAmelCase_	: Optional[int]      			=			self._prepare_for_class(__lowercase				,  __lowercase				,  return_labels=__lowercase							)
														check_equivalence(__lowercase				,  __lowercase				,  __lowercase				,  {'''output_hidden_states''': True}							)




						def   lowercase_	(			self							)   ->      Optional[Any]:
										lowerCAmelCase_	: Dict      			=			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*__lowercase							)




						@slow
						def   lowercase_	(			self							)   ->      Union[str, Any]:
										lowerCAmelCase_	: Union[str, Any]      			=			TFConvNextModel.from_pretrained('''facebook/convnext-tiny-224'''							)
										self.assertIsNotNone(__lowercase							)






def      lowerCAmelCase	(						)->	Optional[Any]:
				lowerCAmelCase_	: Dict      			=			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
				return image







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







						'''simple docstring'''




						@cached_property
						def   lowercase_	(			self							)   ->      Union[str, Any]:
										return ConvNextImageProcessor.from_pretrained('''facebook/convnext-tiny-224'''							) if is_vision_available() else None




						@slow
						def   lowercase_	(			self							)   ->      Optional[Any]:
										lowerCAmelCase_	: Union[str, Any]      			=			TFConvNextForImageClassification.from_pretrained('''facebook/convnext-tiny-224'''							)

										lowerCAmelCase_	: int      			=			self.default_image_processor
										lowerCAmelCase_	: Dict      			=			prepare_img()
										lowerCAmelCase_	: Optional[Any]      			=			image_processor(images=__lowercase				,  return_tensors='''tf'''							)

										# forward pass
										lowerCAmelCase_	: Any      			=			model(**__lowercase							)

										# verify the logits
										lowerCAmelCase_	: Union[str, Any]      			=			tf.TensorShape((1, 1_0_0_0)							)
										self.assertEqual(outputs.logits.shape				,  __lowercase							)

										lowerCAmelCase_	: List[str]      			=			tf.constant([-0.02_60, -0.47_39, 0.19_11]							)

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