



"""simple docstring"""
import itertools
import os
from collections import Counter, defaultdict
from concurrent.futures import ThreadPoolExecutor, as_completed

import numpy as np

import datasets

from .execute import check_correctness


__SCREAMING_SNAKE_CASE  :						Optional[int]    							=      '''\
@misc{chen2021evaluating,
      title={Evaluating Large Language Models Trained on Code},
      author={Mark Chen and Jerry Tworek and Heewoo Jun and Qiming Yuan \
and Henrique Ponde de Oliveira Pinto and Jared Kaplan and Harri Edwards \
and Yuri Burda and Nicholas Joseph and Greg Brockman and Alex Ray \
and Raul Puri and Gretchen Krueger and Michael Petrov and Heidy Khlaaf \
and Girish Sastry and Pamela Mishkin and Brooke Chan and Scott Gray \
and Nick Ryder and Mikhail Pavlov and Alethea Power and Lukasz Kaiser \
and Mohammad Bavarian and Clemens Winter and Philippe Tillet \
and Felipe Petroski Such and Dave Cummings and Matthias Plappert \
and Fotios Chantzis and Elizabeth Barnes and Ariel Herbert-Voss \
and William Hebgen Guss and Alex Nichol and Alex Paino and Nikolas Tezak \
and Jie Tang and Igor Babuschkin and Suchir Balaji and Shantanu Jain \
and William Saunders and Christopher Hesse and Andrew N. Carr \
and Jan Leike and Josh Achiam and Vedant Misra and Evan Morikawa \
and Alec Radford and Matthew Knight and Miles Brundage and Mira Murati \
and Katie Mayer and Peter Welinder and Bob McGrew and Dario Amodei \
and Sam McCandlish and Ilya Sutskever and Wojciech Zaremba},
      year={2021},
      eprint={2107.03374},
      archivePrefix={arXiv},
      primaryClass={cs.LG}
}
'''

__SCREAMING_SNAKE_CASE  :						Any    							=      '''\
This metric implements the evaluation harness for the HumanEval problem solving dataset
described in the paper "Evaluating Large Language Models Trained on Code"
(https://arxiv.org/abs/2107.03374).
'''


__SCREAMING_SNAKE_CASE  :						int    							=      '''
Calculates how good are predictions given some references, using certain scores
Args:
    predictions: list of candidates to evaluate. Each candidates should be a list
        of strings with several code candidates to solve the problem.
    references: a list with a test for each prediction. Each test should evaluate the
        correctness of a code candidate.
    k: number of code candidates to consider in the evaluation (Default: [1, 10, 100])
    num_workers: number of workers used to evaluate the canidate programs (Default: 4).
    timeout:
Returns:
    pass_at_k: dict with pass rates for each k
    results: dict with granular results of each unittest
Examples:
    >>> code_eval = datasets.load_metric("code_eval")
    >>> test_cases = ["assert add(2,3)==5"]
    >>> candidates = [["def add(a,b): return a*b", "def add(a, b): return a+b"]]
    >>> pass_at_k, results = code_eval.compute(references=test_cases, predictions=candidates, k=[1, 2])
    >>> print(pass_at_k)
    {\'pass@1\': 0.5, \'pass@2\': 1.0}
'''


__SCREAMING_SNAKE_CASE  :						Optional[Any]    							=      '''
################################################################################
                                  !!!WARNING!!!
################################################################################
The "code_eval" metric executes untrusted model-generated code in Python.
Although it is highly unlikely that model-generated code will do something
overtly malicious in response to this test suite, model-generated code may act
destructively due to a lack of model capability or alignment.
Users are strongly encouraged to sandbox this evaluation suite so that it
does not perform destructive actions on their host or network. For more
information on how OpenAI sandboxes its code, see the paper "Evaluating Large
Language Models Trained on Code" (https://arxiv.org/abs/2107.03374).

Once you have read this disclaimer and taken appropriate precautions,
set the environment variable HF_ALLOW_CODE_EVAL="1". Within Python you can to this
with:

>>> import os
>>> os.environ["HF_ALLOW_CODE_EVAL"] = "1"

################################################################################\
'''

__SCREAMING_SNAKE_CASE  :						Union[str, Any]    							=      '''The MIT License

Copyright (c) OpenAI (https://openai.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.'''



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





					'''simple docstring'''



					def 			snake_case__      (    self   ):
									return datasets.MetricInfo(
									    # This is the description that will appear on the metrics page.
									    description=_DESCRIPTION				,       citation=_CITATION				,       inputs_description=_KWARGS_DESCRIPTION				,       features=datasets.Features(
									        {
									            '''predictions''': datasets.Sequence(datasets.Value('''string'''   )   ),
									            '''references''': datasets.Value('''string'''   ),
									        }   )				,       homepage='''https://github.com/openai/human-eval'''				,       codebase_urls=['''https://github.com/openai/human-eval''']				,       reference_urls=['''https://github.com/openai/human-eval''']				,       license=_LICENSE				,       )





					def 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__=[1, 1_0, 1_0_0]				,       lowerCamelCase__=4				,       lowerCamelCase__=3.0   ):

									if os.getenv('''HF_ALLOW_CODE_EVAL'''				,       0   ) != "1":
													raise ValueError(_WARNING   )

									if os.name == "nt":
													raise NotImplementedError('''This metric is currently not supported on Windows.'''   )

									with ThreadPoolExecutor(max_workers=lowerCamelCase__   ) as executor:
													_lowerCamelCase        =				[]
													_lowerCamelCase        =				Counter()
													_lowerCamelCase        =				0
													_lowerCamelCase        =				defaultdict(lowerCamelCase__   )

													for task_id, (candidates, test_case) in enumerate(zip(lowerCamelCase__				,       lowerCamelCase__   )   ):
																	for candidate in candidates:
																					_lowerCamelCase        =				candidate + '''\n''' + test_case
																					_lowerCamelCase        =				(test_program, timeout, task_id, completion_id[task_id])
																					_lowerCamelCase        =				executor.submit(lowerCamelCase__				,       *lowerCamelCase__   )
																					futures.append(lowerCamelCase__   )
																					completion_id[task_id] += 1
																					n_samples += 1

													for future in as_completed(lowerCamelCase__   ):
																	_lowerCamelCase        =				future.result()
																	results[result["task_id"]].append((result['''completion_id'''], result)   )

									_lowerCamelCase  ,     _lowerCamelCase        =				[], []
									for result in results.values():
													result.sort()
													_lowerCamelCase        =				[r[1]['''passed'''] for r in result]
													total.append(len(lowerCamelCase__   )   )
													correct.append(sum(lowerCamelCase__   )   )
									_lowerCamelCase        =				np.array(lowerCamelCase__   )
									_lowerCamelCase        =				np.array(lowerCamelCase__   )

									_lowerCamelCase        =				k
									_lowerCamelCase        =				{F"""pass@{k}""": estimate_pass_at_k(lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__   ).mean() for k in ks if (total >= k).all()}

									return pass_at_k, results



def        lowerCAmelCase_(   lowercase_     :							Optional[int]						,   lowercase_     :							str						,   lowercase_     :							List[Any]							)	->       Tuple:

				def estimator(lowercase_     :							int						,   lowercase_     :							int						,   lowercase_     :							int							) -> float:
								if n - c < k:
												return 1.0
								return 1.0 - np.prod(1.0 - k / np.arange(n - c + 1						,   n + 1							)							)

				if isinstance(lowercase_						,   lowercase_							):
								_lowerCamelCase        =				itertools.repeat(lowercase_						,   len(lowercase_							)							)
				else:
								assert len(lowercase_							) == len(lowercase_							)
								_lowerCamelCase        =				iter(lowercase_							)

				return np.array([estimator(int(lowercase_							)						,   int(lowercase_							)						,   lowercase_							) for n, c in zip(lowercase_						,   lowercase_							)]							)





"""simple docstring"""
from __future__ import annotations

import copy
import inspect
import json
import math
import os
import tempfile
import unittest
from importlib import import_module

import numpy as np

from transformers import ViTMAEConfig
from transformers.file_utils import cached_property, is_tf_available, is_vision_available
from transformers.testing_utils import require_tf, require_vision, slow

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 TFViTMAEForPreTraining, TFViTMAEModel


if is_vision_available():
			from PIL import Image

			from transformers import ViTImageProcessor


class 							lowerCamelCase_:





					'''simple docstring'''



					def __init__(    self				,       lowerCamelCase__				,       lowerCamelCase__=1_3				,       lowerCamelCase__=3_0				,       lowerCamelCase__=2				,       lowerCamelCase__=3				,       lowerCamelCase__=True				,       lowerCamelCase__=True				,       lowerCamelCase__=3_2				,       lowerCamelCase__=2				,       lowerCamelCase__=4				,       lowerCamelCase__=3_7				,       lowerCamelCase__="gelu"				,       lowerCamelCase__=0.1				,       lowerCamelCase__=0.1				,       lowerCamelCase__=1_0				,       lowerCamelCase__=0.0_2				,       lowerCamelCase__=3				,       lowerCamelCase__=0.6				,       lowerCamelCase__=None				,       ):
									_lowerCamelCase        =				parent
									_lowerCamelCase        =				batch_size
									_lowerCamelCase        =				image_size
									_lowerCamelCase        =				patch_size
									_lowerCamelCase        =				num_channels
									_lowerCamelCase        =				is_training
									_lowerCamelCase        =				use_labels
									_lowerCamelCase        =				hidden_size
									_lowerCamelCase        =				num_hidden_layers
									_lowerCamelCase        =				num_attention_heads
									_lowerCamelCase        =				intermediate_size
									_lowerCamelCase        =				hidden_act
									_lowerCamelCase        =				hidden_dropout_prob
									_lowerCamelCase        =				attention_probs_dropout_prob
									_lowerCamelCase        =				type_sequence_label_size
									_lowerCamelCase        =				initializer_range
									_lowerCamelCase        =				mask_ratio
									_lowerCamelCase        =				scope

									# in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above
									# (we add 1 for the [CLS] token)
									_lowerCamelCase        =				(image_size // patch_size) ** 2
									_lowerCamelCase        =				int(math.ceil((1 - mask_ratio) * (num_patches + 1)   )   )

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

									_lowerCamelCase        =				self.get_config()

									return config, pixel_values, labels

					def 			snake_case__      (    self   ):
									return ViTMAEConfig(
									    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				,       decoder_hidden_size=self.hidden_size				,       decoder_num_hidden_layers=self.num_hidden_layers				,       decoder_num_attention_heads=self.num_attention_heads				,       decoder_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=lowerCamelCase__				,       initializer_range=self.initializer_range				,       mask_ratio=self.mask_ratio				,       )

					def 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__   ):
									_lowerCamelCase        =				TFViTMAEModel(config=lowerCamelCase__   )
									_lowerCamelCase        =				model(lowerCamelCase__				,       training=lowerCamelCase__   )
									self.parent.assertEqual(result.last_hidden_state.shape				,       (self.batch_size, self.seq_length, self.hidden_size)   )

					def 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__   ):
									_lowerCamelCase        =				TFViTMAEForPreTraining(lowerCamelCase__   )
									_lowerCamelCase        =				model(lowerCamelCase__				,       training=lowerCamelCase__   )
									# expected sequence length = num_patches
									_lowerCamelCase        =				(self.image_size // self.patch_size) ** 2
									_lowerCamelCase        =				self.patch_size**2 * self.num_channels
									self.parent.assertEqual(result.logits.shape				,       (self.batch_size, num_patches, expected_num_channels)   )

									# test greyscale images
									_lowerCamelCase        =				1
									_lowerCamelCase        =				TFViTMAEForPreTraining(lowerCamelCase__   )

									_lowerCamelCase        =				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
									_lowerCamelCase        =				model(lowerCamelCase__				,       training=lowerCamelCase__   )
									_lowerCamelCase        =				self.patch_size**2
									self.parent.assertEqual(result.logits.shape				,       (self.batch_size, num_patches, expected_num_channels)   )





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


@require_tf
class 							lowerCamelCase_(   A__,				A__,				unittest.TestCase    ):





					'''simple docstring'''



					lowercase__   :       Optional[Any]										= (TFViTMAEModel, TFViTMAEForPreTraining) if is_tf_available() else ()
					lowercase__   :       Dict										= {'feature-extraction': TFViTMAEModel} if is_tf_available() else {}

					lowercase__   :       Optional[Any]										= False
					lowercase__   :       Union[str, Any]										= False
					lowercase__   :       str										= False
					lowercase__   :       List[str]										= False

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				TFViTMAEModelTester(self   )
									_lowerCamelCase        =				ConfigTester(self				,       config_class=lowerCamelCase__				,       has_text_modality=lowerCamelCase__				,       hidden_size=3_7   )

					def 			snake_case__      (    self   ):
									self.config_tester.run_common_tests()

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

					def 			snake_case__      (    self   ):
									_lowerCamelCase  ,     _lowerCamelCase        =				self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
													_lowerCamelCase        =				model_class(lowerCamelCase__   )
													self.assertIsInstance(model.get_input_embeddings()				,       (tf.keras.layers.Layer)   )
													_lowerCamelCase        =				model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(lowerCamelCase__				,       tf.keras.layers.Layer   )   )

					def 			snake_case__      (    self   ):
									_lowerCamelCase  ,     _lowerCamelCase        =				self.model_tester.prepare_config_and_inputs_for_common()

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

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowerCamelCase__   )

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_pretraining(*lowerCamelCase__   )

					def 			snake_case__      (    self   ):
									# make the mask reproducible
									np.random.seed(2   )

									_lowerCamelCase  ,     _lowerCamelCase        =				self.model_tester.prepare_config_and_inputs_for_common()

									_lowerCamelCase        =				int((config.image_size // config.patch_size) ** 2   )
									_lowerCamelCase        =				np.random.uniform(size=(self.model_tester.batch_size, num_patches)   )

									for model_class in self.all_model_classes:
													_lowerCamelCase        =				model_class(lowerCamelCase__   )
													_lowerCamelCase        =				self._prepare_for_class(lowerCamelCase__				,       lowerCamelCase__   )

													_lowerCamelCase        =				model(lowerCamelCase__				,       noise=lowerCamelCase__   )

													_lowerCamelCase        =				copy.deepcopy(self._prepare_for_class(lowerCamelCase__				,       lowerCamelCase__   )   )
													_lowerCamelCase        =				model(**lowerCamelCase__				,       noise=lowerCamelCase__   )
													_lowerCamelCase        =				outputs_dict[0].numpy()
													_lowerCamelCase        =				outputs_keywords[0].numpy()

													self.assertLess(np.sum(np.abs(output_dict - output_keywords   )   )				,       1e-6   )

					def 			snake_case__      (    self   ):
									# make the mask reproducible
									np.random.seed(2   )

									_lowerCamelCase  ,     _lowerCamelCase        =				self.model_tester.prepare_config_and_inputs_for_common()

									_lowerCamelCase        =				int((config.image_size // config.patch_size) ** 2   )
									_lowerCamelCase        =				np.random.uniform(size=(self.model_tester.batch_size, num_patches)   )

									def prepare_numpy_arrays(lowerCamelCase__   ):
													_lowerCamelCase        =				{}
													for k, v in inputs_dict.items():
																	if tf.is_tensor(lowerCamelCase__   ):
																					_lowerCamelCase        =				v.numpy()
																	else:
																					_lowerCamelCase        =				np.array(lowerCamelCase__   )

													return inputs_np_dict

									for model_class in self.all_model_classes:
													_lowerCamelCase        =				model_class(lowerCamelCase__   )

													_lowerCamelCase        =				self._prepare_for_class(lowerCamelCase__				,       lowerCamelCase__   )
													_lowerCamelCase        =				prepare_numpy_arrays(lowerCamelCase__   )

													_lowerCamelCase        =				model(lowerCamelCase__				,       noise=lowerCamelCase__   )
													_lowerCamelCase        =				model(**lowerCamelCase__				,       noise=lowerCamelCase__   )
													self.assert_outputs_same(lowerCamelCase__				,       lowerCamelCase__   )

					def 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__   ):
									# make masks reproducible
									np.random.seed(2   )

									_lowerCamelCase        =				int((tf_model.config.image_size // tf_model.config.patch_size) ** 2   )
									_lowerCamelCase        =				np.random.uniform(size=(self.model_tester.batch_size, num_patches)   )
									_lowerCamelCase        =				tf.constant(lowerCamelCase__   )

									# Add `noise` argument.
									# PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument
									_lowerCamelCase        =				tf_noise

									super().check_pt_tf_models(lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__   )

					def 			snake_case__      (    self   ):
									# make mask reproducible
									np.random.seed(2   )

									_lowerCamelCase  ,     _lowerCamelCase        =				self.model_tester.prepare_config_and_inputs_for_common()

									_lowerCamelCase        =				{
									    module_member
									    for model_class in self.all_model_classes
									    for module in (import_module(model_class.__module__   ),)
									    for module_member_name in dir(lowerCamelCase__   )
									    if module_member_name.endswith('''MainLayer'''   )
									    # This condition is required, since `modeling_tf_clip.py` has 3 classes whose names end with `MainLayer`.
									    and module_member_name[: -len('''MainLayer'''   )] == model_class.__name__[: -len('''Model'''   )]
									    for module_member in (getattr(lowerCamelCase__				,       lowerCamelCase__   ),)
									    if isinstance(lowerCamelCase__				,       lowerCamelCase__   )
									    and tf.keras.layers.Layer in module_member.__bases__
									    and getattr(lowerCamelCase__				,       '''_keras_serializable'''				,       lowerCamelCase__   )
									}

									_lowerCamelCase        =				int((config.image_size // config.patch_size) ** 2   )
									_lowerCamelCase        =				np.random.uniform(size=(self.model_tester.batch_size, num_patches)   )
									_lowerCamelCase        =				tf.convert_to_tensor(lowerCamelCase__   )
									inputs_dict.update({'''noise''': noise}   )

									for main_layer_class in tf_main_layer_classes:
													_lowerCamelCase        =				main_layer_class(lowerCamelCase__   )

													_lowerCamelCase        =				{
													    name: tf.keras.Input(tensor.shape[1:]				,       dtype=tensor.dtype   ) for name, tensor in inputs_dict.items()
													}

													_lowerCamelCase        =				tf.keras.Model(lowerCamelCase__				,       outputs=main_layer(lowerCamelCase__   )   )
													_lowerCamelCase        =				model(lowerCamelCase__   )

													with tempfile.TemporaryDirectory() as tmpdirname:
																	_lowerCamelCase        =				os.path.join(lowerCamelCase__				,       '''keras_model.h5'''   )
																	model.save(lowerCamelCase__   )
																	_lowerCamelCase        =				tf.keras.models.load_model(
																	    lowerCamelCase__				,       custom_objects={main_layer_class.__name__: main_layer_class}   )
																	assert isinstance(lowerCamelCase__				,       tf.keras.Model   )
																	_lowerCamelCase        =				model(lowerCamelCase__   )
																	self.assert_outputs_same(lowerCamelCase__				,       lowerCamelCase__   )

					@slow
					def 			snake_case__      (    self   ):
									# make mask reproducible
									np.random.seed(2   )

									_lowerCamelCase  ,     _lowerCamelCase        =				self.model_tester.prepare_config_and_inputs_for_common()

									_lowerCamelCase        =				int((config.image_size // config.patch_size) ** 2   )
									_lowerCamelCase        =				np.random.uniform(size=(self.model_tester.batch_size, num_patches)   )

									for model_class in self.all_model_classes:
													_lowerCamelCase        =				model_class(lowerCamelCase__   )
													_lowerCamelCase        =				self._prepare_for_class(lowerCamelCase__				,       lowerCamelCase__   )
													_lowerCamelCase        =				model(lowerCamelCase__				,       noise=lowerCamelCase__   )

													if model_class.__name__ == "TFViTMAEModel":
																	_lowerCamelCase        =				outputs.last_hidden_state.numpy()
																	_lowerCamelCase        =				0
													else:
																	_lowerCamelCase        =				outputs.logits.numpy()
																	_lowerCamelCase        =				0

													with tempfile.TemporaryDirectory() as tmpdirname:
																	model.save_pretrained(lowerCamelCase__				,       saved_model=lowerCamelCase__   )
																	_lowerCamelCase        =				model_class.from_pretrained(lowerCamelCase__   )
																	_lowerCamelCase        =				model(lowerCamelCase__				,       noise=lowerCamelCase__   )

																	if model_class.__name__ == "TFViTMAEModel":
																					_lowerCamelCase        =				after_outputs['''last_hidden_state'''].numpy()
																					_lowerCamelCase        =				0
																	else:
																					_lowerCamelCase        =				after_outputs['''logits'''].numpy()
																					_lowerCamelCase        =				0

																	_lowerCamelCase        =				np.amax(np.abs(out_a - out_a   )   )
																	self.assertLessEqual(lowerCamelCase__				,       1e-5   )

					def 			snake_case__      (    self   ):
									# make mask reproducible
									np.random.seed(2   )

									_lowerCamelCase  ,     _lowerCamelCase        =				self.model_tester.prepare_config_and_inputs_for_common()

									_lowerCamelCase        =				int((config.image_size // config.patch_size) ** 2   )
									_lowerCamelCase        =				np.random.uniform(size=(self.model_tester.batch_size, num_patches)   )

									for model_class in self.all_model_classes:
													_lowerCamelCase        =				model_class(lowerCamelCase__   )
													_lowerCamelCase        =				self._prepare_for_class(lowerCamelCase__				,       lowerCamelCase__   )

													_lowerCamelCase        =				model(lowerCamelCase__				,       noise=lowerCamelCase__   )
													_lowerCamelCase        =				model.get_config()
													# make sure that returned config is jsonifiable, which is required by keras
													json.dumps(lowerCamelCase__   )
													_lowerCamelCase        =				model_class.from_config(model.get_config()   )
													# make sure it also accepts a normal config
													_lowerCamelCase        =				model_class.from_config(model.config   )
													_lowerCamelCase        =				new_model(lowerCamelCase__   )  # Build model
													new_model.set_weights(model.get_weights()   )
													_lowerCamelCase        =				new_model(lowerCamelCase__				,       noise=lowerCamelCase__   )

													self.assert_outputs_same(lowerCamelCase__				,       lowerCamelCase__   )

					@unittest.skip(
					    reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load
    to get deterministic results.'''   )
					def 			snake_case__      (    self   ):
									pass

					@unittest.skip(reason='''ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load'''   )
					def 			snake_case__      (    self   ):
									pass





					@slow
					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				TFViTMAEModel.from_pretrained('''google/vit-base-patch16-224'''   )
									self.assertIsNotNone(lowerCamelCase__   )



def        lowerCAmelCase_(   )	->       List[Any]:
				_lowerCamelCase        =				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   ):
									return ViTImageProcessor.from_pretrained('''facebook/vit-mae-base'''   ) if is_vision_available() else None





					@slow
					def 			snake_case__      (    self   ):
									# make random mask reproducible across the PT and TF model
									np.random.seed(2   )

									_lowerCamelCase        =				TFViTMAEForPreTraining.from_pretrained('''facebook/vit-mae-base'''   )

									_lowerCamelCase        =				self.default_image_processor
									_lowerCamelCase        =				prepare_img()
									_lowerCamelCase        =				image_processor(images=lowerCamelCase__				,       return_tensors='''tf'''   )

									# prepare a noise vector that will be also used for testing the TF model
									# (this way we can ensure that the PT and TF models operate on the same inputs)
									_lowerCamelCase        =				ViTMAEConfig()
									_lowerCamelCase        =				int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2   )
									_lowerCamelCase        =				np.random.uniform(size=(1, num_patches)   )

									# forward pass
									_lowerCamelCase        =				model(**lowerCamelCase__				,       noise=lowerCamelCase__   )

									# verify the logits
									_lowerCamelCase        =				tf.convert_to_tensor([1, 1_9_6, 7_6_8]   )
									self.assertEqual(outputs.logits.shape				,       lowerCamelCase__   )

									_lowerCamelCase        =				tf.convert_to_tensor(
									    [[-0.0_5_4_8, -1.7_0_2_3, -0.9_3_2_5], [0.3_7_2_1, -0.5_6_7_0, -0.2_2_3_3], [0.8_2_3_5, -1.3_8_7_8, -0.3_5_2_4]]   )

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