




import inspect
import unittest

import numpy as np

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

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


if is_flax_available():
   import jax

   from transformers import FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel

if is_vision_available():
   from PIL import Image

   from transformers import BeitImageProcessor




class       a__	( unittest.TestCase				):







     def __init__(     self  ,		A  ,		A=100  ,		A=13  ,		A=30  ,		A=2  ,		A=3  ,		A=True  ,		A=True  ,		A=32  ,		A=5  ,		A=4  ,		A=37  ,		A="gelu"  ,		A=0.1  ,		A=0.1  ,		A=10  ,		A=0.0_2  ,		A=3  ,		)  ->   Any:



          '''simple docstring'''



          a     =				parent
          a     =				vocab_size
          a     =				batch_size
          a     =				image_size
          a     =				patch_size
          a     =				num_channels
          a     =				is_training
          a     =				use_labels
          a     =				hidden_size
          a     =				num_hidden_layers
          a     =				num_attention_heads
          a     =				intermediate_size
          a     =				hidden_act
          a     =				hidden_dropout_prob
          a     =				attention_probs_dropout_prob
          a     =				type_sequence_label_size
          a     =				initializer_range

          # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
          a     =				(image_size // patch_size) ** 2
          a     =				num_patches + 1







     def       lowerCAmelCase_    (     self			)  ->   List[str]:



          '''simple docstring'''



          a     =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

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

          a     =				BeitConfig(
              vocab_size=self.vocab_size  ,		image_size=self.image_size  ,		patch_size=self.patch_size  ,		num_channels=self.num_channels  ,		hidden_size=self.hidden_size  ,		num_hidden_layers=self.num_hidden_layers  ,		num_attention_heads=self.num_attention_heads  ,		intermediate_size=self.intermediate_size  ,		hidden_act=self.hidden_act  ,		hidden_dropout_prob=self.hidden_dropout_prob  ,		attention_probs_dropout_prob=self.attention_probs_dropout_prob  ,		is_decoder=A  ,		initializer_range=self.initializer_range  ,		)

          return config, pixel_values, labels







     def       lowerCAmelCase_    (     self  ,		A  ,		A  ,		A			)  ->   List[Any]:



          '''simple docstring'''



          a     =				FlaxBeitModel(config=A			)
          a     =				model(A			)
          self.parent.assertEqual(result.last_hidden_state.shape  ,		(self.batch_size, self.seq_length, self.hidden_size)			)







     def       lowerCAmelCase_    (     self  ,		A  ,		A  ,		A			)  ->   str:



          '''simple docstring'''



          a     =				FlaxBeitForMaskedImageModeling(config=A			)
          a     =				model(A			)
          self.parent.assertEqual(result.logits.shape  ,		(self.batch_size, self.seq_length - 1, self.vocab_size)			)







     def       lowerCAmelCase_    (     self  ,		A  ,		A  ,		A			)  ->   Dict:



          '''simple docstring'''



          a     =				self.type_sequence_label_size
          a     =				FlaxBeitForImageClassification(config=A			)
          a     =				model(A			)
          self.parent.assertEqual(result.logits.shape  ,		(self.batch_size, self.type_sequence_label_size)			)

          # test greyscale images
          a     =				1
          a     =				FlaxBeitForImageClassification(A			)

          a     =				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
          a     =				model(A			)







     def       lowerCAmelCase_    (     self			)  ->   Dict:



          '''simple docstring'''



          a     =				self.prepare_config_and_inputs()
          (
              (
              a
          )  ,				(
              a
          )  ,				(
              a
          )  ,				
          )     =				config_and_inputs
          a     =				{"pixel_values": pixel_values}
          return config, inputs_dict







@require_flax
class       a__	( UpperCamelCase__       , unittest.TestCase				):
     a		:      Optional[int]		 =     (
         (FlaxBeitModel, FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling) if is_flax_available() else ()
     )







     def       lowerCAmelCase_    (     self			)  ->   None:



          '''simple docstring'''



          a     =				FlaxBeitModelTester(self			)
          a     =				ConfigTester(self  ,		config_class=A  ,		has_text_modality=A  ,		hidden_size=37			)







     def       lowerCAmelCase_    (     self			)  ->   List[str]:



          '''simple docstring'''



          self.config_tester.run_common_tests()







     def       lowerCAmelCase_    (     self			)  ->   List[str]:



          '''simple docstring'''



          a  ,				a     =				self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
               a     =				model_class(A			)
               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]  ,		A			)







     def       lowerCAmelCase_    (     self			)  ->   Tuple:



          '''simple docstring'''



          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(A  ,		A			)
                    a     =				model_class(A			)

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

                    with self.subTest("JIT Enabled"			):
                         a     =				model_jitted(**A			).to_tuple()

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

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







     def       lowerCAmelCase_    (     self			)  ->   Any:



          '''simple docstring'''



          a     =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*A			)







     def       lowerCAmelCase_    (     self			)  ->   Optional[Any]:



          '''simple docstring'''



          a     =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*A			)







     def       lowerCAmelCase_    (     self			)  ->   Union[str, Any]:



          '''simple docstring'''



          a     =				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*A			)







     @slow
     def       lowerCAmelCase_    (     self			)  ->   Optional[int]:



          '''simple docstring'''



          for model_class_name in self.all_model_classes:
               a     =				model_class_name.from_pretrained("microsoft/beit-base-patch16-224"			)
               a     =				model(np.ones((1, 3, 224, 224)			)			)
               self.assertIsNotNone(A			)







def  SCREAMING_SNAKE_CASE							(       )      ->					Any:
     a     =				Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
     return image





@require_vision
@require_flax
class       a__	( unittest.TestCase				):







     @cached_property
     def       lowerCAmelCase_    (     self			)  ->   Any:



          '''simple docstring'''



          return BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224"			) if is_vision_available() else None







     @slow
     def       lowerCAmelCase_    (     self			)  ->   Union[str, Any]:



          '''simple docstring'''



          a     =				FlaxBeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k"			)

          a     =				self.default_image_processor
          a     =				prepare_img()
          a     =				image_processor(images=A  ,		return_tensors="np"			).pixel_values

          # prepare bool_masked_pos
          a     =				np.ones((1, 196)  ,		dtype=A			)

          # forward pass
          a     =				model(pixel_values=A  ,		bool_masked_pos=A			)
          a     =				outputs.logits

          # verify the logits
          a     =				(1, 196, 8192)
          self.assertEqual(logits.shape  ,		A			)

          a     =				np.array(
              [[-3.2_4_3_7, 0.5_0_7_2, -1_3.9_1_7_4], [-3.2_4_5_6, 0.4_9_4_8, -1_3.9_4_0_1], [-3.2_0_3_3, 0.5_1_2_1, -1_3.8_5_5_0]]			)

          self.assertTrue(np.allclose(logits[bool_masked_pos][:3, :3]  ,		A  ,		atol=1e-2			)			)







     @slow
     def       lowerCAmelCase_    (     self			)  ->   str:



          '''simple docstring'''



          a     =				FlaxBeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224"			)

          a     =				self.default_image_processor
          a     =				prepare_img()
          a     =				image_processor(images=A  ,		return_tensors="np"			)

          # forward pass
          a     =				model(**A			)
          a     =				outputs.logits

          # verify the logits
          a     =				(1, 1000)
          self.assertEqual(logits.shape  ,		A			)

          a     =				np.array([-1.2_3_8_5, -1.0_9_8_7, -1.0_1_0_8]			)

          self.assertTrue(np.allclose(logits[0, :3]  ,		A  ,		atol=1e-4			)			)

          a     =				281
          self.assertEqual(logits.argmax(-1			).item()  ,		A			)







     @slow
     def       lowerCAmelCase_    (     self			)  ->   int:



          '''simple docstring'''



          a     =				FlaxBeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k"			)

          a     =				self.default_image_processor
          a     =				prepare_img()
          a     =				image_processor(images=A  ,		return_tensors="np"			)

          # forward pass
          a     =				model(**A			)
          a     =				outputs.logits

          # verify the logits
          a     =				(1, 21841)
          self.assertEqual(logits.shape  ,		A			)

          a     =				np.array([1.6_8_8_1, -0.2_7_8_7, 0.5_9_0_1]			)

          self.assertTrue(np.allclose(logits[0, :3]  ,		A  ,		atol=1e-4			)			)

          a     =				2396
          self.assertEqual(logits.argmax(-1			).item()  ,		A			)







import numpy as np
from nltk.translate import meteor_score

import datasets
from datasets.config import importlib_metadata, version


lowercase__				:      Optional[int]	   = version.parse(importlib_metadata.version("nltk"))
if NLTK_VERSION >= version.Version("3.6.4"):
   from nltk import word_tokenize


lowercase__				:      int	   = "\\n@inproceedings{banarjee2005,\n  title     = {{METEOR}: An Automatic Metric for {MT} Evaluation with Improved Correlation with Human Judgments},\n  author    = {Banerjee, Satanjeev  and Lavie, Alon},\n  booktitle = {Proceedings of the {ACL} Workshop on Intrinsic and Extrinsic Evaluation Measures for Machine Translation and/or Summarization},\n  month     = jun,\n  year      = {2005},\n  address   = {Ann Arbor, Michigan},\n  publisher = {Association for Computational Linguistics},\n  url       = {https://www.aclweb.org/anthology/W05-0909},\n  pages     = {65--72},\n}\n"

lowercase__				:      str	   = "\\nMETEOR, an automatic metric for machine translation evaluation\nthat is based on a generalized concept of unigram matching between the\nmachine-produced translation and human-produced reference translations.\nUnigrams can be matched based on their surface forms, stemmed forms,\nand meanings; furthermore, METEOR can be easily extended to include more\nadvanced matching strategies. Once all generalized unigram matches\nbetween the two strings have been found, METEOR computes a score for\nthis matching using a combination of unigram-precision, unigram-recall, and\na measure of fragmentation that is designed to directly capture how\nwell-ordered the matched words in the machine translation are in relation\nto the reference.\n\nMETEOR gets an R correlation value of 0.347 with human evaluation on the Arabic\ndata and 0.331 on the Chinese data. This is shown to be an improvement on\nusing simply unigram-precision, unigram-recall and their harmonic F1\ncombination.\n"

lowercase__				:      Tuple	   = "\nComputes METEOR score of translated segments against one or more references.\nArgs:\n    predictions: list of predictions to score. Each prediction\n        should be a string with tokens separated by spaces.\n    references: list of reference for each prediction. Each\n        reference should be a string with tokens separated by spaces.\n    alpha: Parameter for controlling relative weights of precision and recall. default: 0.9\n    beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3\n    gamma: Relative weight assigned to fragmentation penalty. default: 0.5\nReturns:\n    'meteor': meteor score.\nExamples:\n\n    >>> meteor = datasets.load_metric('meteor')\n    >>> predictions = [\"It is a guide to action which ensures that the military always obeys the commands of the party\"]\n    >>> references = [\"It is a guide to action that ensures that the military will forever heed Party commands\"]\n    >>> results = meteor.compute(predictions=predictions, references=references)\n    >>> print(round(results[\"meteor\"], 4))\n    0.6944\n"




@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION       , _KWARGS_DESCRIPTION				)
class       a__	( datasets.Metric				):







     def       lowerCAmelCase_    (     self			)  ->   List[Any]:



          '''simple docstring'''



          return datasets.MetricInfo(
              description=_DESCRIPTION  ,		citation=_CITATION  ,		inputs_description=_KWARGS_DESCRIPTION  ,		features=datasets.Features(
                  {
                      "predictions": datasets.Value("string"  ,		id="sequence"			),
                      "references": datasets.Value("string"  ,		id="sequence"			),
                  }			)  ,		codebase_urls=["https://github.com/nltk/nltk/blob/develop/nltk/translate/meteor_score.py"]  ,		reference_urls=[
                  "https://www.nltk.org/api/nltk.translate.html#module-nltk.translate.meteor_score",
                  "https://en.wikipedia.org/wiki/METEOR",
              ]  ,		)







     def       lowerCAmelCase_    (     self  ,		A			)  ->   List[str]:



          '''simple docstring'''



          import nltk

          nltk.download("wordnet"			)
          if NLTK_VERSION >= version.Version("3.6.5"			):
               nltk.download("punkt"			)
          if NLTK_VERSION >= version.Version("3.6.6"			):
               nltk.download("omw-1.4"			)







     def       lowerCAmelCase_    (     self  ,		A  ,		A  ,		A=0.9  ,		A=3  ,		A=0.5			)  ->   Tuple:



          '''simple docstring'''



          if NLTK_VERSION >= version.Version("3.6.5"			):
               a     =				[
                   meteor_score.single_meteor_score(
                       word_tokenize(A			)  ,		word_tokenize(A			)  ,		alpha=A  ,		beta=A  ,		gamma=A			)
                   for ref, pred in zip(A  ,		A			)
               ]
          else:
               a     =				[
                   meteor_score.single_meteor_score(A  ,		A  ,		alpha=A  ,		beta=A  ,		gamma=A			)
                   for ref, pred in zip(A  ,		A			)
               ]

          return {"meteor": np.mean(A			)}

