




import inspect
import unittest

from transformers import RegNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


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

       from transformers.models.regnet.modeling_flax_regnet import FlaxRegNetForImageClassification, FlaxRegNetModel

if is_vision_available():
       from PIL import Image

       from transformers import AutoImageProcessor





class     lowerCamelCase__(					unittest.TestCase):
      def __init__(				self: Dict    ,       UpperCamelCase_: List[str]    ,       UpperCamelCase_: Optional[int]=3    ,       UpperCamelCase_: Optional[int]=32    ,       UpperCamelCase_: Dict=3    ,       UpperCamelCase_: Optional[Any]=10    ,       UpperCamelCase_: Optional[int]=[10, 20, 30, 40]    ,       UpperCamelCase_: Dict=[1, 1, 2, 1]    ,       UpperCamelCase_: str=True    ,       UpperCamelCase_: Optional[int]=True    ,       UpperCamelCase_: Optional[Any]="relu"    ,       UpperCamelCase_: Optional[Any]=3    ,       UpperCamelCase_: List[str]=None    ,       ):
             __lowerCamelCase   =						parent
             __lowerCamelCase   =						batch_size
             __lowerCamelCase   =						image_size
             __lowerCamelCase   =						num_channels
             __lowerCamelCase   =						embeddings_size
             __lowerCamelCase   =						hidden_sizes
             __lowerCamelCase   =						depths
             __lowerCamelCase   =						is_training
             __lowerCamelCase   =						use_labels
             __lowerCamelCase   =						hidden_act
             __lowerCamelCase   =						num_labels
             __lowerCamelCase   =						scope
             __lowerCamelCase   =						len(UpperCamelCase_   )
      def 			lowerCAmelCase__						(				self: Any   ):
             __lowerCamelCase   =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

             __lowerCamelCase   =						self.get_config()

             return config, pixel_values
      def 			lowerCAmelCase__						(				self: Optional[Any]   ):
             return RegNetConfig(
                 num_channels=self.num_channels    ,       embeddings_size=self.embeddings_size    ,       hidden_sizes=self.hidden_sizes    ,       depths=self.depths    ,       hidden_act=self.hidden_act    ,       num_labels=self.num_labels    ,       image_size=self.image_size    ,       )
      def 			lowerCAmelCase__						(				self: int    ,       UpperCamelCase_: Optional[Any]    ,       UpperCamelCase_: Optional[Any]   ):
             __lowerCamelCase   =						FlaxRegNetModel(config=UpperCamelCase_   )
             __lowerCamelCase   =						model(UpperCamelCase_   )

             # Output shape (b, c, h, w)
             self.parent.assertEqual(
                 result.last_hidden_state.shape    ,       (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)    ,       )
      def 			lowerCAmelCase__						(				self: int    ,       UpperCamelCase_: List[str]    ,       UpperCamelCase_: Optional[Any]   ):
             __lowerCamelCase   =						self.num_labels
             __lowerCamelCase   =						FlaxRegNetForImageClassification(config=UpperCamelCase_   )
             __lowerCamelCase   =						model(UpperCamelCase_   )
             self.parent.assertEqual(result.logits.shape    ,       (self.batch_size, self.num_labels)   )




      def 			lowerCAmelCase__						(				self: int   ):
             __lowerCamelCase   =						self.prepare_config_and_inputs()
             __lowerCamelCase,   __lowerCamelCase   =						config_and_inputs
             __lowerCamelCase   =						{"""pixel_values""": pixel_values}
             return config, inputs_dict





@require_flax
class     lowerCamelCase__(					__lowerCamelCase ,     unittest.TestCase):
      UpperCAmelCase__      :	Optional[int]    		=   (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

      UpperCAmelCase__      :	str    		=   False
      UpperCAmelCase__      :	Optional[int]    		=   False
      UpperCAmelCase__      :	List[Any]    		=   False
      def 			lowerCAmelCase__						(				self: Union[str, Any]   ):
             __lowerCamelCase   =						FlaxRegNetModelTester(self   )
             __lowerCamelCase   =						ConfigTester(self    ,       config_class=UpperCamelCase_    ,       has_text_modality=UpperCamelCase_   )
      def 			lowerCAmelCase__						(				self: Optional[Any]   ):
             self.create_and_test_config_common_properties()
             self.config_tester.create_and_test_config_to_json_string()
             self.config_tester.create_and_test_config_to_json_file()
             self.config_tester.create_and_test_config_from_and_save_pretrained()
             self.config_tester.create_and_test_config_with_num_labels()
             self.config_tester.check_config_can_be_init_without_params()
             self.config_tester.check_config_arguments_init()
      def 			lowerCAmelCase__						(				self: Optional[Any]   ):
             return
      def 			lowerCAmelCase__						(				self: int   ):
             __lowerCamelCase   =						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*UpperCamelCase_   )
      def 			lowerCAmelCase__						(				self: Any   ):
             __lowerCamelCase   =						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_classification(*UpperCamelCase_   )
      @unittest.skip(reason="""RegNet does not use inputs_embeds"""   )
      def 			lowerCAmelCase__						(				self: Union[str, Any]   ):
             pass
      @unittest.skip(reason="""RegNet does not support input and output embeddings"""   )
      def 			lowerCAmelCase__						(				self: Optional[Any]   ):
             pass
      def 			lowerCAmelCase__						(				self: Tuple   ):
             __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 			lowerCAmelCase__						(				self: Optional[Any]   ):
             def check_hidden_states_output(UpperCamelCase_: List[str]    ,       UpperCamelCase_: Any    ,       UpperCamelCase_: str   ):
                    __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   )

             __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 			lowerCAmelCase__						(				self: Optional[int]   ):
             __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__   ):
                           __lowerCamelCase   =						self._prepare_for_class(UpperCamelCase_    ,       UpperCamelCase_   )
                           __lowerCamelCase   =						model_class(UpperCamelCase_   )

                           @jax.jit
                           def model_jitted(UpperCamelCase_: List[Any]    ,       **UpperCamelCase_: Tuple   ):
                                  return model(pixel_values=UpperCamelCase_    ,       **UpperCamelCase_   )

                           with self.subTest("""JIT Enabled"""   ):
                                  __lowerCamelCase   =						model_jitted(**UpperCamelCase_   ).to_tuple()

                           with self.subTest("""JIT Disabled"""   ):
                                  with jax.disable_jit():
                                         __lowerCamelCase   =						model_jitted(**UpperCamelCase_   ).to_tuple()

                           self.assertEqual(len(UpperCamelCase_   )    ,       len(UpperCamelCase_   )   )
                           for jitted_output, output in zip(UpperCamelCase_    ,       UpperCamelCase_   ):
                                  self.assertEqual(jitted_output.shape    ,       output.shape   )




def 					lowerCamelCase__       (						):





       '''simple docstring'''



       __lowerCamelCase   =						Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""	)
       return image





@require_flax
class     lowerCamelCase__(					unittest.TestCase):
      @cached_property
      def 			lowerCAmelCase__						(				self: List[Any]   ):
             return AutoImageProcessor.from_pretrained("""facebook/regnet-y-040"""   ) if is_vision_available() else None




      @slow
      def 			lowerCAmelCase__						(				self: str   ):
             __lowerCamelCase   =						FlaxRegNetForImageClassification.from_pretrained("""facebook/regnet-y-040"""   )

             __lowerCamelCase   =						self.default_image_processor
             __lowerCamelCase   =						prepare_img()
             __lowerCamelCase   =						image_processor(images=UpperCamelCase_    ,       return_tensors="""np"""   )

             __lowerCamelCase   =						model(**UpperCamelCase_   )

             # verify the logits
             __lowerCamelCase   =						(1, 10_00)
             self.assertEqual(outputs.logits.shape    ,       UpperCamelCase_   )

             __lowerCamelCase   =						jnp.array([-0.4180, -1.5051, -3.4836]   )

             self.assertTrue(jnp.allclose(outputs.logits[0, :3]    ,       UpperCamelCase_    ,       atol=1E-4   )   )







from transformers import DistilBertTokenizer, DistilBertTokenizerFast
from transformers.testing_utils import require_tokenizers, slow

from ..bert.test_tokenization_bert import BertTokenizationTest





@require_tokenizers
class     lowerCamelCase__(					__lowerCamelCase):
      UpperCAmelCase__      :	Dict    		=   DistilBertTokenizer
      UpperCAmelCase__      :	Dict    		=   DistilBertTokenizerFast
      UpperCAmelCase__      :	Tuple    		=   True
      @slow
      def 			lowerCAmelCase__						(				self: Tuple   ):
             __lowerCamelCase   =						DistilBertTokenizer.from_pretrained("""distilbert-base-uncased"""   )

             __lowerCamelCase   =						tokenizer.encode("""sequence builders"""    ,       add_special_tokens=UpperCamelCase_   )
             __lowerCamelCase   =						tokenizer.encode("""multi-sequence build"""    ,       add_special_tokens=UpperCamelCase_   )

             __lowerCamelCase   =						tokenizer.build_inputs_with_special_tokens(UpperCamelCase_   )
             __lowerCamelCase   =						tokenizer.build_inputs_with_special_tokens(UpperCamelCase_    ,       UpperCamelCase_   )

             assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id]
             assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [
                 tokenizer.sep_token_id
             ]

