
'''simple docstring'''
import inspect
import math
import tempfile
import unittest

import numpy as np

from transformers import ViTMAEConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch
     from torch import nn

     from transformers import ViTMAEForPreTraining, ViTMAEModel
     from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     from PIL import Image

     from transformers import ViTImageProcessor







class  __A				:
 def __init__(   self			:    Optional[Any]				,   lowerCamelCase			:    str				,   lowerCamelCase			:    int=13				,   lowerCamelCase			:    Dict=30				,   lowerCamelCase			:    int=2				,   lowerCamelCase			:    Optional[Any]=3				,   lowerCamelCase			:    Dict=True				,   lowerCamelCase			:    Any=True				,   lowerCamelCase			:    Union[str, Any]=32				,   lowerCamelCase			:    List[str]=5				,   lowerCamelCase			:    Optional[int]=4				,   lowerCamelCase			:    List[Any]=37				,   lowerCamelCase			:    List[Any]="gelu"				,   lowerCamelCase			:    str=0.1				,   lowerCamelCase			:    Optional[int]=0.1				,   lowerCamelCase			:    List[Any]=10				,   lowerCamelCase			:    List[str]=0.02				,   lowerCamelCase			:    str=3				,   lowerCamelCase			:    Optional[Any]=0.6				,   lowerCamelCase			:    Dict=None				,   ):



   """simple docstring"""





   __A		:     int												=		parent
   __A		:     List[str]												=		batch_size
   __A		:     Tuple												=		image_size
   __A		:     List[str]												=		patch_size
   __A		:     Optional[Any]												=		num_channels
   __A		:     Optional[Any]												=		is_training
   __A		:     Any												=		use_labels
   __A		:     Union[str, Any]												=		hidden_size
   __A		:     Tuple												=		num_hidden_layers
   __A		:     Optional[int]												=		num_attention_heads
   __A		:     Union[str, Any]												=		intermediate_size
   __A		:     Union[str, Any]												=		hidden_act
   __A		:     int												=		hidden_dropout_prob
   __A		:     Any												=		attention_probs_dropout_prob
   __A		:     str												=		type_sequence_label_size
   __A		:     Optional[int]												=		initializer_range
   __A		:     List[Any]												=		mask_ratio
   __A		:     Dict												=		scope

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



   """simple docstring"""





   __A		:     Optional[int]												=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

   __A		:     int												=		None
   if self.use_labels:
     __A		:     Optional[int]												=		ids_tensor([self.batch_size]				,   self.type_sequence_label_size       )

   __A		:     Tuple												=		self.get_config()

   return config, pixel_values, labels
 def 		lowercase_(   self			:    Dict       ):



   """simple docstring"""





   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				,   hidden_act=self.hidden_act				,   hidden_dropout_prob=self.hidden_dropout_prob				,   attention_probs_dropout_prob=self.attention_probs_dropout_prob				,   is_decoder=UpperCAmelCase__				,   initializer_range=self.initializer_range				,   mask_ratio=self.mask_ratio				,   )
 def 		lowercase_(   self			:    Any				,   lowerCamelCase			:    Any				,   lowerCamelCase			:    Union[str, Any]				,   lowerCamelCase			:    Any       ):



   """simple docstring"""





   __A		:     str												=		ViTMAEModel(config=UpperCAmelCase__       )
   model.to(UpperCAmelCase__       )
   model.eval()
   __A		:     Dict												=		model(UpperCAmelCase__       )
   self.parent.assertEqual(result.last_hidden_state.shape				,   (self.batch_size, self.seq_length, self.hidden_size)       )
 def 		lowercase_(   self			:    List[str]				,   lowerCamelCase			:    List[Any]				,   lowerCamelCase			:    List[Any]				,   lowerCamelCase			:    Tuple       ):



   """simple docstring"""





   __A		:     Dict												=		ViTMAEForPreTraining(UpperCAmelCase__       )
   model.to(UpperCAmelCase__       )
   model.eval()
   __A		:     Any												=		model(UpperCAmelCase__       )
   __A		:     str												=		(self.image_size // self.patch_size) ** 2
   __A		:     List[Any]												=		self.patch_size**2 * self.num_channels
   self.parent.assertEqual(result.logits.shape				,   (self.batch_size, num_patches, expected_num_channels)       )

   # test greyscale images
   __A		:     str												=		1
   __A		:     Any												=		ViTMAEForPreTraining(UpperCAmelCase__       )
   model.to(UpperCAmelCase__       )
   model.eval()
   __A		:     Tuple												=		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
   __A		:     List[str]												=		model(UpperCAmelCase__       )
   __A		:     int												=		self.patch_size**2
   self.parent.assertEqual(result.logits.shape				,   (self.batch_size, num_patches, expected_num_channels)       )






 def 		lowercase_(   self			:    Optional[int]       ):



   """simple docstring"""





   __A		:     Optional[int]												=		self.prepare_config_and_inputs()
   __A		:     int												=		config_and_inputs
   __A		:     List[Any]												=		{'''pixel_values''': pixel_values}
   return config, inputs_dict







@require_torch
class  __A				( _SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						unittest.TestCase			):
 lowerCamelCase				       =(ViTMAEModel, ViTMAEForPreTraining) if is_torch_available() else ()
 lowerCamelCase				       ={'feature-extraction': ViTMAEModel} if is_torch_available() else {}

 lowerCamelCase				       =False
 lowerCamelCase				       =False
 lowerCamelCase				       =False
 lowerCamelCase				       =False
 def 		lowercase_(   self			:    Tuple       ):



   """simple docstring"""





   __A		:     Union[str, Any]												=		ViTMAEModelTester(self       )
   __A		:     Tuple												=		ConfigTester(self				,   config_class=UpperCAmelCase__				,   has_text_modality=UpperCAmelCase__				,   hidden_size=37       )
 def 		lowercase_(   self			:    Tuple       ):



   """simple docstring"""





   self.config_tester.run_common_tests()
 @unittest.skip(reason="""ViTMAE does not use inputs_embeds"""       )
 def 		lowercase_(   self			:    Union[str, Any]       ):



   """simple docstring"""





   pass
 def 		lowercase_(   self			:    Tuple       ):



   """simple docstring"""





   __A		:     Tuple												=		self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
     __A		:     List[Any]												=		model_class(UpperCAmelCase__       )
     self.assertIsInstance(model.get_input_embeddings()				,   (nn.Module)       )
     __A		:     str												=		model.get_output_embeddings()
     self.assertTrue(x is None or isinstance(UpperCAmelCase__				,   nn.Linear       )       )
 def 		lowercase_(   self			:    Optional[int]       ):



   """simple docstring"""





   __A		:     str												=		self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
     __A		:     Tuple												=		model_class(UpperCAmelCase__       )
     __A		:     int												=		inspect.signature(model.forward       )
     # signature.parameters is an OrderedDict => so arg_names order is deterministic
     __A		:     List[str]												=		[*signature.parameters.keys()]

     __A		:     Any												=		['''pixel_values''']
     self.assertListEqual(arg_names[:1]				,   UpperCAmelCase__       )
 def 		lowercase_(   self			:    Dict       ):



   """simple docstring"""





   __A		:     str												=		self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*UpperCAmelCase__       )
 def 		lowercase_(   self			:    List[Any]       ):



   """simple docstring"""





   __A		:     Union[str, Any]												=		self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_pretraining(*UpperCAmelCase__       )
 def 		lowercase_(   self			:    List[str]				,   lowerCamelCase			:    Optional[Any]				,   lowerCamelCase			:    Dict				,   lowerCamelCase			:    Tuple       ):



   """simple docstring"""





   np.random.seed(2       )

   __A		:     Optional[Any]												=		int((pt_model.config.image_size // pt_model.config.patch_size) ** 2       )
   __A		:     Any												=		np.random.uniform(size=(self.model_tester.batch_size, num_patches)       )
   __A		:     List[str]												=		torch.from_numpy(UpperCAmelCase__       )

   # Add `noise` argument.
   # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument
   __A		:     Optional[Any]												=		pt_noise

   super().check_pt_tf_models(UpperCAmelCase__				,   UpperCAmelCase__				,   UpperCAmelCase__       )
 def 		lowercase_(   self			:    Tuple       ):



   """simple docstring"""





   __A		:     List[str]												=		self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
     __A		:     Optional[int]												=		model_class(UpperCAmelCase__       )
     model.to(UpperCAmelCase__       )
     model.eval()
     # make random mask reproducible
     torch.manual_seed(2       )
     with torch.no_grad():
       __A		:     List[Any]												=		model(**self._prepare_for_class(UpperCAmelCase__				,   UpperCAmelCase__       )       )

     __A		:     Optional[int]												=		outputs[0].cpu().numpy()
     __A		:     List[Any]												=		0

     with tempfile.TemporaryDirectory() as tmpdirname:
       model.save_pretrained(UpperCAmelCase__       )
       __A		:     int												=		model_class.from_pretrained(UpperCAmelCase__       )
       model.to(UpperCAmelCase__       )
       # make random mask reproducible
       torch.manual_seed(2       )
       with torch.no_grad():
         __A		:     str												=		model(**self._prepare_for_class(UpperCAmelCase__				,   UpperCAmelCase__       )       )

       # Make sure we don't have nans
       __A		:     Union[str, Any]												=		after_outputs[0].cpu().numpy()
       __A		:     Tuple												=		0
       __A		:     Optional[Any]												=		np.amax(np.abs(out_a - out_a       )       )
       self.assertLessEqual(UpperCAmelCase__				,   1e-5       )
 @unittest.skip(
     reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load
    to get deterministic results."""       )
 def 		lowercase_(   self			:    Dict       ):



   """simple docstring"""





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



   """simple docstring"""





   pass
 @unittest.skip(
     reason="""ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load
    to get deterministic results."""       )
 def 		lowercase_(   self			:    Optional[int]       ):



   """simple docstring"""





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



   """simple docstring"""





   pass
 @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""       )
 def 		lowercase_(   self			:    Dict       ):



   """simple docstring"""





   pass






 @slow
 def 		lowercase_(   self			:    Optional[int]       ):



   """simple docstring"""





   for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     __A		:     Dict												=		ViTMAEModel.from_pretrained(UpperCAmelCase__       )
     self.assertIsNotNone(UpperCAmelCase__       )







def        A_     (						)				->       List[Any]:





  """simple docstring"""

  __A		:     List[Any]												=		Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
  return image







@require_torch
@require_vision
class  __A				( unittest.TestCase			):
 @cached_property
 def 		lowercase_(   self			:    List[str]       ):



   """simple docstring"""





   return ViTImageProcessor.from_pretrained("""facebook/vit-mae-base"""       ) if is_vision_available() else None






 @slow
 def 		lowercase_(   self			:    List[Any]       ):



   """simple docstring"""





   np.random.seed(2       )

   __A		:     str												=		ViTMAEForPreTraining.from_pretrained("""facebook/vit-mae-base"""       ).to(UpperCAmelCase__       )

   __A		:     Any												=		self.default_image_processor
   __A		:     List[Any]												=		prepare_img()
   __A		:     int												=		image_processor(images=UpperCAmelCase__				,   return_tensors="""pt"""       ).to(UpperCAmelCase__       )

   # 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)
   __A		:     Dict												=		ViTMAEConfig()
   __A		:     Union[str, Any]												=		int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2       )
   __A		:     Tuple												=		np.random.uniform(size=(1, num_patches)       )

   # forward pass
   with torch.no_grad():
     __A		:     List[str]												=		model(**UpperCAmelCase__				,   noise=torch.from_numpy(UpperCAmelCase__       ).to(device=UpperCAmelCase__       )       )

   # verify the logits
   __A		:     Optional[Any]												=		torch.Size((1, 1_96, 7_68)       )
   self.assertEqual(outputs.logits.shape				,   UpperCAmelCase__       )

   __A		:     List[str]												=		torch.tensor(
       [[-0.0_548, -1.7_023, -0.9_325], [0.3_721, -0.5_670, -0.2_233], [0.8_235, -1.3_878, -0.3_524]]       )

   self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]				,   expected_slice.to(UpperCAmelCase__       )				,   atol=1e-4       )       )





'''simple docstring'''
A__   :		int 		=8.3_1_4_4_6_2  # Unit - J mol-1 K-1







def        A_     (						__SCREAMING_SNAKE_CASE						:  float			,     __SCREAMING_SNAKE_CASE						:  float			,     __SCREAMING_SNAKE_CASE						:  float )				->       float:





  """simple docstring"""

  if moles < 0 or kelvin < 0 or volume < 0:
    raise ValueError("""Invalid inputs. Enter positive value.""" )
  return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume







def        A_     (						__SCREAMING_SNAKE_CASE						:  float			,     __SCREAMING_SNAKE_CASE						:  float			,     __SCREAMING_SNAKE_CASE						:  float )				->       float:





  """simple docstring"""

  if moles < 0 or kelvin < 0 or pressure < 0:
    raise ValueError("""Invalid inputs. Enter positive value.""" )
  return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure


if __name__ == "__main__":
     from doctest import testmod

     testmod()



