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



 '''simple docstring'''

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

 def        __snake_case       (      self							:		int	)			->				Any:
        _UpperCamelCase  :							int	    =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

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

        _UpperCamelCase  :							Tuple	    =	self.get_config()

        return config, pixel_values, labels

 def        __snake_case       (      self							:		str	)			->				Union[str, Any]:
        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=lowerCamelCase							,      initializer_range=self.initializer_range							,      )

 def        __snake_case       (      self							:		Union[str, Any]							,      lowerCamelCase							:		Any							,      lowerCamelCase							:		Optional[Any]							,      lowerCamelCase							:		Optional[int]	)			->				Union[str, Any]:
        _UpperCamelCase  :							int	    =	TFConvNextModel(config=lowerCamelCase	)
        _UpperCamelCase  :							List[Any]	    =	model(lowerCamelCase							,      training=lowerCamelCase	)
        # 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        __snake_case       (      self							:		Union[str, Any]							,      lowerCamelCase							:		Dict							,      lowerCamelCase							:		List[Any]							,      lowerCamelCase							:		Any	)			->				Dict:
        _UpperCamelCase  :							Tuple	    =	self.type_sequence_label_size
        _UpperCamelCase  :							Optional[int]	    =	TFConvNextForImageClassification(lowerCamelCase	)
        _UpperCamelCase  :							Optional[Any]	    =	model(lowerCamelCase							,      labels=lowerCamelCase							,      training=lowerCamelCase	)
        self.parent.assertEqual(result.logits.shape							,      (self.batch_size, self.type_sequence_label_size)	)

 def        __snake_case       (      self							:		List[Any]	)			->				str:
        _UpperCamelCase  :							Tuple	    =	self.prepare_config_and_inputs()
        _UpperCamelCase    ,   _UpperCamelCase    ,   _UpperCamelCase  :							Optional[int]	    =	config_and_inputs
        _UpperCamelCase  :							Dict	    =	{'pixel_values': pixel_values}
        return config, inputs_dict







@require_tf
class  __lowerCAmelCase	(							a_,						a_,						unittest.TestCase    ):



 '''simple docstring'''
 _lowerCamelCase    :						List[str]					     =			(TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
 _lowerCamelCase    :						Any					     =			(
     {'feature-extraction': TFConvNextModel, 'image-classification': TFConvNextForImageClassification}
     if is_tf_available()
     else {}
 )

 _lowerCamelCase    :						Any					     =			False
 _lowerCamelCase    :						int					     =			False
 _lowerCamelCase    :						Optional[int]					     =			False
 _lowerCamelCase    :						Optional[int]					     =			False
 _lowerCamelCase    :						Optional[Any]					     =			False

 def        __snake_case       (      self							:		int	)			->				int:
        _UpperCamelCase  :							int	    =	TFConvNextModelTester(self	)
        _UpperCamelCase  :							Optional[int]	    =	ConfigTester(
            self							,      config_class=lowerCamelCase							,      has_text_modality=lowerCamelCase							,      hidden_size=3_7							,      )

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

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

 def        __snake_case       (      self							:		List[Any]	)			->				Union[str, Any]:
        _UpperCamelCase    ,   _UpperCamelCase  :							Optional[int]	    =	self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
               _UpperCamelCase  :							List[Any]	    =	model_class(lowerCamelCase	)
               _UpperCamelCase  :							Tuple	    =	inspect.signature(model.call	)
               # signature.parameters is an OrderedDict => so arg_names order is deterministic
               _UpperCamelCase  :							Optional[Any]	    =	[*signature.parameters.keys()]

               _UpperCamelCase  :							Optional[int]	    =	['pixel_values']
               self.assertListEqual(arg_names[:1]							,      lowerCamelCase	)

 def        __snake_case       (      self							:		List[Any]	)			->				List[Any]:
        _UpperCamelCase  :							Optional[Any]	    =	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCamelCase	)

 @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							:		Tuple	)			->				str:
        super().test_dataset_conversion()

 def        __snake_case       (      self							:		Tuple	)			->				Any:
        def check_hidden_states_output(lowerCamelCase							:		Union[str, Any]							,      lowerCamelCase							:		Dict							,      lowerCamelCase							:		List[str]	):
               _UpperCamelCase  :							Tuple	    =	model_class(lowerCamelCase	)

               _UpperCamelCase  :							Optional[Any]	    =	model(**self._prepare_for_class(lowerCamelCase							,      lowerCamelCase	)	)
               _UpperCamelCase  :							Optional[int]	    =	outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

               _UpperCamelCase  :							List[str]	    =	self.model_tester.num_stages
               self.assertEqual(len(lowerCamelCase	)							,      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]							,      )

        _UpperCamelCase    ,   _UpperCamelCase  :							List[Any]	    =	self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
               _UpperCamelCase  :							List[str]	    =	True
               check_hidden_states_output(lowerCamelCase							,      lowerCamelCase							,      lowerCamelCase	)

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

               check_hidden_states_output(lowerCamelCase							,      lowerCamelCase							,      lowerCamelCase	)

 def        __snake_case       (      self							:		List[str]	)			->				Optional[int]:
        _UpperCamelCase    ,   _UpperCamelCase  :							Union[str, Any]	    =	self.model_tester.prepare_config_and_inputs_for_common()

        def check_equivalence(lowerCamelCase							:		Optional[Any]							,      lowerCamelCase							:		Optional[int]							,      lowerCamelCase							:		List[Any]							,      lowerCamelCase							:		Tuple={}	):
               _UpperCamelCase  :							str	    =	model(lowerCamelCase							,      return_dict=lowerCamelCase							,      **lowerCamelCase	)
               _UpperCamelCase  :							str	    =	model(lowerCamelCase							,      return_dict=lowerCamelCase							,      **lowerCamelCase	).to_tuple()

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

                      recursive_check(lowerCamelCase							,      lowerCamelCase	)

        for model_class in self.all_model_classes:
               _UpperCamelCase  :							str	    =	model_class(lowerCamelCase	)

               _UpperCamelCase  :							Optional[Any]	    =	self._prepare_for_class(lowerCamelCase							,      lowerCamelCase	)
               _UpperCamelCase  :							int	    =	self._prepare_for_class(lowerCamelCase							,      lowerCamelCase	)
               check_equivalence(lowerCamelCase							,      lowerCamelCase							,      lowerCamelCase	)

               _UpperCamelCase  :							str	    =	self._prepare_for_class(lowerCamelCase							,      lowerCamelCase							,      return_labels=lowerCamelCase	)
               _UpperCamelCase  :							Union[str, Any]	    =	self._prepare_for_class(lowerCamelCase							,      lowerCamelCase							,      return_labels=lowerCamelCase	)
               check_equivalence(lowerCamelCase							,      lowerCamelCase							,      lowerCamelCase	)

               _UpperCamelCase  :							Optional[int]	    =	self._prepare_for_class(lowerCamelCase							,      lowerCamelCase	)
               _UpperCamelCase  :							Union[str, Any]	    =	self._prepare_for_class(lowerCamelCase							,      lowerCamelCase	)
               check_equivalence(lowerCamelCase							,      lowerCamelCase							,      lowerCamelCase							,      {'output_hidden_states': True}	)

               _UpperCamelCase  :							Dict	    =	self._prepare_for_class(lowerCamelCase							,      lowerCamelCase							,      return_labels=lowerCamelCase	)
               _UpperCamelCase  :							str	    =	self._prepare_for_class(lowerCamelCase							,      lowerCamelCase							,      return_labels=lowerCamelCase	)
               check_equivalence(lowerCamelCase							,      lowerCamelCase							,      lowerCamelCase							,      {'output_hidden_states': True}	)

 def        __snake_case       (      self							:		Dict	)			->				List[str]:
        _UpperCamelCase  :							Any	    =	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*lowerCamelCase	)

 @slow
 def        __snake_case       (      self							:		Dict	)			->				Optional[int]:
        _UpperCamelCase  :							Dict	    =	TFConvNextModel.from_pretrained('facebook/convnext-tiny-224'	)
        self.assertIsNotNone(lowerCamelCase	)







def SCREAMING_SNAKE_CASE					(  ):
       _UpperCamelCase  :							Any	    =	Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'					)
       return image





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



 '''simple docstring'''

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

 @slow
 def        __snake_case       (      self							:		Union[str, Any]	)			->				Tuple:
        _UpperCamelCase  :							List[str]	    =	TFConvNextForImageClassification.from_pretrained('facebook/convnext-tiny-224'	)

        _UpperCamelCase  :							List[str]	    =	self.default_image_processor
        _UpperCamelCase  :							Dict	    =	prepare_img()
        _UpperCamelCase  :							Tuple	    =	image_processor(images=lowerCamelCase							,      return_tensors='tf'	)

        # forward pass
        _UpperCamelCase  :							Union[str, Any]	    =	model(**lowerCamelCase	)

        # verify the logits
        _UpperCamelCase  :							Optional[Any]	    =	tf.TensorShape((1, 1_0_0_0)	)
        self.assertEqual(outputs.logits.shape							,      lowerCamelCase	)

        _UpperCamelCase  :							str	    =	tf.constant([-0.0_2_6_0, -0.4_7_3_9, 0.1_9_1_1]	)

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




from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCAmelCase      		=					logging.get_logger(__name__)

UpperCAmelCase      		=					{
    '''flaubert/flaubert_small_cased''': '''https://huggingface.co/flaubert/flaubert_small_cased/resolve/main/config.json''',
    '''flaubert/flaubert_base_uncased''': '''https://huggingface.co/flaubert/flaubert_base_uncased/resolve/main/config.json''',
    '''flaubert/flaubert_base_cased''': '''https://huggingface.co/flaubert/flaubert_base_cased/resolve/main/config.json''',
    '''flaubert/flaubert_large_cased''': '''https://huggingface.co/flaubert/flaubert_large_cased/resolve/main/config.json''',
}




class  __lowerCAmelCase	(							a_    ):



 '''simple docstring'''
 _lowerCamelCase    :						List[Any]					     =			'flaubert'
 _lowerCamelCase    :						List[str]					     =			{
     'hidden_size': 'emb_dim',
     'num_attention_heads': 'n_heads',
     'num_hidden_layers': 'n_layers',
     'n_words': 'vocab_size',  # For backward compatibility
 }

 def __init__(      self							:		List[Any]							,      lowerCamelCase							:		Tuple=False							,      lowerCamelCase							:		str=0.0							,      lowerCamelCase							:		int=3_0_1_4_5							,      lowerCamelCase							:		List[str]=2_0_4_8							,      lowerCamelCase							:		List[str]=1_2							,      lowerCamelCase							:		Dict=1_6							,      lowerCamelCase							:		Any=0.1							,      lowerCamelCase							:		List[str]=0.1							,      lowerCamelCase							:		List[str]=True							,      lowerCamelCase							:		Any=False							,      lowerCamelCase							:		Optional[int]=False							,      lowerCamelCase							:		int=False							,      lowerCamelCase							:		Optional[Any]=1							,      lowerCamelCase							:		int=True							,      lowerCamelCase							:		str=5_1_2							,      lowerCamelCase							:		Tuple=2_0_4_8**-0.5							,      lowerCamelCase							:		str=1e-12							,      lowerCamelCase							:		Any=0.0_2							,      lowerCamelCase							:		Dict=0							,      lowerCamelCase							:		Union[str, Any]=1							,      lowerCamelCase							:		List[Any]=2							,      lowerCamelCase							:		Union[str, Any]=3							,      lowerCamelCase							:		List[Any]=5							,      lowerCamelCase							:		int=True							,      lowerCamelCase							:		Dict="first"							,      lowerCamelCase							:		Optional[int]=True							,      lowerCamelCase							:		Any=None							,      lowerCamelCase							:		Optional[int]=True							,      lowerCamelCase							:		List[str]=0.1							,      lowerCamelCase							:		List[Any]=5							,      lowerCamelCase							:		Optional[Any]=5							,      lowerCamelCase							:		str=0							,      lowerCamelCase							:		Optional[int]=0							,      lowerCamelCase							:		List[str]=2							,      lowerCamelCase							:		Any=0							,      **lowerCamelCase							:		Tuple							,      )			->				Any:
        _UpperCamelCase  :							Optional[Any]	    =	pre_norm
        _UpperCamelCase  :							Optional[int]	    =	layerdrop
        _UpperCamelCase  :							Dict	    =	vocab_size
        _UpperCamelCase  :							Dict	    =	emb_dim
        _UpperCamelCase  :							int	    =	n_layers
        _UpperCamelCase  :							Tuple	    =	n_heads
        _UpperCamelCase  :							Tuple	    =	dropout
        _UpperCamelCase  :							Any	    =	attention_dropout
        _UpperCamelCase  :							Union[str, Any]	    =	gelu_activation
        _UpperCamelCase  :							Dict	    =	sinusoidal_embeddings
        _UpperCamelCase  :							str	    =	causal
        _UpperCamelCase  :							str	    =	asm
        _UpperCamelCase  :							Tuple	    =	n_langs
        _UpperCamelCase  :							Optional[int]	    =	use_lang_emb
        _UpperCamelCase  :							Optional[Any]	    =	layer_norm_eps
        _UpperCamelCase  :							Optional[Any]	    =	bos_index
        _UpperCamelCase  :							Dict	    =	eos_index
        _UpperCamelCase  :							Union[str, Any]	    =	pad_index
        _UpperCamelCase  :							Union[str, Any]	    =	unk_index
        _UpperCamelCase  :							Optional[Any]	    =	mask_index
        _UpperCamelCase  :							Any	    =	is_encoder
        _UpperCamelCase  :							List[str]	    =	max_position_embeddings
        _UpperCamelCase  :							Union[str, Any]	    =	embed_init_std
        _UpperCamelCase  :							Optional[int]	    =	init_std
        _UpperCamelCase  :							Any	    =	summary_type
        _UpperCamelCase  :							Any	    =	summary_use_proj
        _UpperCamelCase  :							Optional[int]	    =	summary_activation
        _UpperCamelCase  :							Union[str, Any]	    =	summary_proj_to_labels
        _UpperCamelCase  :							Dict	    =	summary_first_dropout
        _UpperCamelCase  :							Union[str, Any]	    =	start_n_top
        _UpperCamelCase  :							Any	    =	end_n_top
        _UpperCamelCase  :							List[Any]	    =	mask_token_id
        _UpperCamelCase  :							str	    =	lang_id

        if "n_words" in kwargs:
               _UpperCamelCase  :							Tuple	    =	kwargs['n_words']

        super().__init__(pad_token_id=lowerCamelCase							,      bos_token_id=lowerCamelCase							,      **lowerCamelCase	)







class  __lowerCAmelCase	(							a_    ):



 '''simple docstring'''

 @property
 def        __snake_case       (      self							:		str	)			->				Mapping[str, Mapping[int, str]]:
        if self.task == "multiple-choice":
               _UpperCamelCase  :							int	    =	{0: 'batch', 1: 'choice', 2: 'sequence'}
        else:
               _UpperCamelCase  :							Dict	    =	{0: 'batch', 1: 'sequence'}
        return OrderedDict(
            [
                ('input_ids', dynamic_axis),
                ('attention_mask', dynamic_axis),
            ]	)



