




import unittest

from transformers.models.xlm_prophetnet.tokenization_xlm_prophetnet import SPIECE_UNDERLINE, XLMProphetNetTokenizer
from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow
from transformers.utils import cached_property

from ...test_tokenization_common import TokenizerTesterMixin


_lowerCAmelCase  :      str						=      get_tests_dir("fixtures/test_sentencepiece.model")
@require_sentencepiece
class 	_UpperCamelCase					( UpperCamelCase__		,				unittest.TestCase						):
      UpperCAmelCase_						=						XLMProphetNetTokenizer
      UpperCAmelCase_						=						False
      UpperCAmelCase_						=						True
      def 		UpperCAmelCase_  (	self      :Tuple					)   ->	List[Any]:
       super().setUp()

       # We have a SentencePiece fixture for testing
       UpperCAmelCase__							       =  XLMProphetNetTokenizer(__lowerCamelCase       ,					keep_accents=__lowerCamelCase					)
       tokenizer.save_pretrained(self.tmpdirname					)
      def 		UpperCAmelCase_  (	self      :Any					)   ->	Any:
       UpperCAmelCase__							       =  "[PAD]"
       UpperCAmelCase__							       =  0

       self.assertEqual(self.get_tokenizer()._convert_token_to_id(__lowerCamelCase					)       ,					__lowerCamelCase					)
       self.assertEqual(self.get_tokenizer()._convert_id_to_token(__lowerCamelCase					)       ,					__lowerCamelCase					)
      def 		UpperCAmelCase_  (	self      :Dict					)   ->	str:
       UpperCAmelCase__							       =  list(self.get_tokenizer().get_vocab().keys()					)

       self.assertEqual(vocab_keys[0]       ,					"[PAD]"					)
       self.assertEqual(vocab_keys[1]       ,					"[CLS]"					)
       self.assertEqual(vocab_keys[-1]       ,					"j"					)
       self.assertEqual(len(__lowerCamelCase					)       ,					1012					)
      def 		UpperCAmelCase_  (	self      :Dict					)   ->	str:
       self.assertEqual(self.get_tokenizer().vocab_size       ,					1012					)
      def 		UpperCAmelCase_  (	self      :Optional[Any]					)   ->	Dict:
       UpperCAmelCase__							       =  XLMProphetNetTokenizer(__lowerCamelCase       ,					keep_accents=__lowerCamelCase					)

       UpperCAmelCase__							       =  tokenizer.tokenize("This is a test"					)
       self.assertListEqual(__lowerCamelCase       ,					["▁This", "▁is", "▁a", "▁t", "est"]					)

       self.assertListEqual(
           tokenizer.convert_tokens_to_ids(__lowerCamelCase					)       ,					[value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]]       ,					)

       UpperCAmelCase__							       =  tokenizer.tokenize("I was born in 92000, and this is falsé."					)
       self.assertListEqual(
           __lowerCamelCase       ,					[
               SPIECE_UNDERLINE + "I",
               SPIECE_UNDERLINE + "was",
               SPIECE_UNDERLINE + "b",
               "or",
               "n",
               SPIECE_UNDERLINE + "in",
               SPIECE_UNDERLINE + "",
               "9",
               "2",
               "0",
               "0",
               "0",
               ",",
               SPIECE_UNDERLINE + "and",
               SPIECE_UNDERLINE + "this",
               SPIECE_UNDERLINE + "is",
               SPIECE_UNDERLINE + "f",
               "al",
               "s",
               "é",
               ".",
           ]       ,					)
       UpperCAmelCase__							       =  tokenizer.convert_tokens_to_ids(__lowerCamelCase					)
       self.assertListEqual(
           __lowerCamelCase       ,					[
               value + tokenizer.fairseq_offset
               for value in [8, 21, 84, 55, 24, 19, 7, -9, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, -9, 4]
           ]       ,					)

       UpperCAmelCase__							       =  tokenizer.convert_ids_to_tokens(__lowerCamelCase					)
       self.assertListEqual(
           __lowerCamelCase       ,					[
               SPIECE_UNDERLINE + "I",
               SPIECE_UNDERLINE + "was",
               SPIECE_UNDERLINE + "b",
               "or",
               "n",
               SPIECE_UNDERLINE + "in",
               SPIECE_UNDERLINE + "",
               "[UNK]",
               "2",
               "0",
               "0",
               "0",
               ",",
               SPIECE_UNDERLINE + "and",
               SPIECE_UNDERLINE + "this",
               SPIECE_UNDERLINE + "is",
               SPIECE_UNDERLINE + "f",
               "al",
               "s",
               "[UNK]",
               ".",
           ]       ,					)
      @cached_property
      def 		UpperCAmelCase_  (	self      :Tuple					)   ->	Optional[int]:
       return XLMProphetNetTokenizer.from_pretrained("microsoft/xprophetnet-large-wiki100-cased"					)
      @slow
      def 		UpperCAmelCase_  (	self      :List[Any]					)   ->	str:
       UpperCAmelCase__							       =  "Hello World!"
       UpperCAmelCase__							       =  [3_5389, 6672, 49, 2]
       self.assertListEqual(__lowerCamelCase       ,					self.big_tokenizer.encode(__lowerCamelCase					)					)







      @slow
      def 		UpperCAmelCase_  (	self      :Optional[int]					)   ->	List[Any]:
       # fmt: off
       UpperCAmelCase__							       =  {"input_ids": [[1_1073, 8_2783, 18, 26, 8_2783, 549, 5_1540, 248, 1_7209, 1301, 217, 20, 21_5186, 1325, 147, 1_7209, 1301, 217, 20, 5_6370, 53, 12_2020, 20, 1_6477, 27, 8_7355, 4548, 20, 4728, 7_8392, 17, 15_9969, 18, 26, 2_4491, 629, 15, 538, 2_2704, 5439, 15, 2788, 2_4491, 9885, 15, 4_3534, 605, 15, 814, 1_8403, 3_3200, 29, 15, 4_3534, 2_4458, 1_2410, 111, 2_4966, 8_3669, 9637, 14_4068, 26, 850, 2_2346, 27, 147, 2_4966, 8_3669, 8_3490, 26, 3_9113, 735, 27, 689, 656, 2800, 1339, 4600, 53, 12_2020, 11_5785, 34, 816, 1339, 4_6887, 18, 147, 5_3905, 1951, 4_2238, 4_1170, 1_7732, 834, 436, 15, 2_7523, 9_8733, 217, 147, 5542, 4981, 930, 1_7347, 16, 2], [2_0091, 629, 94, 8_2786, 58, 490, 20, 1528, 84, 5_3905, 344, 8_0592, 11_0128, 1_8822, 5267, 1306, 62, 15_2537, 308, 7997, 401, 12_4427, 549, 3_5442, 225, 109, 1_5055, 2_5748, 147, 7119, 4_3712, 34, 767, 13_5366, 18, 16, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [592, 6_3784, 11_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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}  # noqa: E501
       # fmt: on

       self.tokenizer_integration_test_util(
           expected_encoding=__lowerCamelCase       ,					model_name="microsoft/xprophetnet-large-wiki100-cased"       ,					revision="1acad1643ddd54a44df6a1b797ada8373685d90e"       ,					)












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     SCREAMING_SNAKE_CASE__ (						unittest.TestCase      ):



 def __init__(							self,__lowerCamelCase,__lowerCamelCase=3,__lowerCamelCase=32,__lowerCamelCase=3,__lowerCamelCase=10,__lowerCamelCase=[10, 20, 30, 40],__lowerCamelCase=[1, 1, 2, 1],__lowerCamelCase=True,__lowerCamelCase=True,__lowerCamelCase="relu",__lowerCamelCase=3,__lowerCamelCase=None,):
        A__											=						parent
        A__											=						batch_size
        A__											=						image_size
        A__											=						num_channels
        A__											=						embeddings_size
        A__											=						hidden_sizes
        A__											=						depths
        A__											=						is_training
        A__											=						use_labels
        A__											=						hidden_act
        A__											=						num_labels
        A__											=						scope
        A__											=						len(__lowerCamelCase )



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

        A__											=						self.get_config()

        return config, pixel_values



 def 				UpperCamelCase			(							self ):
        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 				UpperCamelCase			(							self,__lowerCamelCase,__lowerCamelCase ):
        A__											=						FlaxRegNetModel(config=__lowerCamelCase )
        A__											=						model(__lowerCamelCase )

        # 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 				UpperCamelCase			(							self,__lowerCamelCase,__lowerCamelCase ):
        A__											=						self.num_labels
        A__											=						FlaxRegNetForImageClassification(config=__lowerCamelCase )
        A__											=						model(__lowerCamelCase )
        self.parent.assertEqual(result.logits.shape,(self.batch_size, self.num_labels) )




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




@require_flax
class     SCREAMING_SNAKE_CASE__ (						UpperCamelCase__				,					unittest.TestCase      ):
 __SCREAMING_SNAKE_CASE							       =	(FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

 __SCREAMING_SNAKE_CASE							       =	False
 __SCREAMING_SNAKE_CASE							       =	False
 __SCREAMING_SNAKE_CASE							       =	False



 def 				UpperCamelCase			(							self ):
        A__											=						FlaxRegNetModelTester(self )
        A__											=						ConfigTester(self,config_class=__lowerCamelCase,has_text_modality=__lowerCamelCase )



 def 				UpperCamelCase			(							self ):
        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 				UpperCamelCase			(							self ):
        return



 def 				UpperCamelCase			(							self ):
        A__											=						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__lowerCamelCase )



 def 				UpperCamelCase			(							self ):
        A__											=						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase )



 @unittest.skip(reason='''RegNet does not use inputs_embeds''' )
 def 				UpperCamelCase			(							self ):
        pass



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



 def 				UpperCamelCase			(							self ):
        A__					,	A__											=						self.model_tester.prepare_config_and_inputs_for_common()

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

               A__											=						['''pixel_values''']
               self.assertListEqual(arg_names[:1],__lowerCamelCase )



 def 				UpperCamelCase			(							self ):
        def check_hidden_states_output(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase ):
               A__											=						model_class(__lowerCamelCase )

               A__											=						model(**self._prepare_for_class(__lowerCamelCase,__lowerCamelCase ) )

               A__											=						outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

               A__											=						self.model_tester.num_stages
               self.assertEqual(len(__lowerCamelCase ),expected_num_stages + 1 )

        A__					,	A__											=						self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
               A__											=						True
               check_hidden_states_output(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase )

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

               check_hidden_states_output(__lowerCamelCase,__lowerCamelCase,__lowerCamelCase )




 def 				UpperCamelCase			(							self ):
        A__					,	A__											=						self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
               with self.subTest(model_class.__name__ ):
                      A__											=						self._prepare_for_class(__lowerCamelCase,__lowerCamelCase )
                      A__											=						model_class(__lowerCamelCase )

                      @jax.jit
                      def model_jitted(__lowerCamelCase,**__lowerCamelCase ):
                             return model(pixel_values=__lowerCamelCase,**__lowerCamelCase )

                      with self.subTest('''JIT Enabled''' ):
                             A__											=						model_jitted(**__lowerCamelCase ).to_tuple()

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

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


def 	UpperCamelCase__(       )->Optional[int]:
       A__											=						Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''  )
       return image




@require_flax
class     SCREAMING_SNAKE_CASE__ (						unittest.TestCase      ):



 @cached_property
 def 				UpperCamelCase			(							self ):
        return AutoImageProcessor.from_pretrained('''facebook/regnet-y-040''' ) if is_vision_available() else None




 @slow
 def 				UpperCamelCase			(							self ):
        A__											=						FlaxRegNetForImageClassification.from_pretrained('''facebook/regnet-y-040''' )

        A__											=						self.default_image_processor
        A__											=						prepare_img()
        A__											=						image_processor(images=__lowerCamelCase,return_tensors='''np''' )

        A__											=						model(**__lowerCamelCase )

        # verify the logits
        A__											=						(1, 1000)
        self.assertEqual(outputs.logits.shape,__lowerCamelCase )

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

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





