




"""simple docstring"""

import inspect
import unittest

from datasets import load_dataset
from packaging import version

from transformers import BeitConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_gpu, 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, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
       import torch
       from torch import nn

       from transformers import (
           MODEL_MAPPING,
           BeitForImageClassification,
           BeitForMaskedImageModeling,
           BeitForSemanticSegmentation,
           BeitModel,
       )
       from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
       import PIL
       from PIL import Image

       from transformers import BeitImageProcessor



class     lowercase_	:



 '''simple docstring'''

 def __init__(			self	:	Any   ,							_UpperCAmelCase	:	List[str]   ,							_UpperCAmelCase	:	Optional[int]=100   ,							_UpperCAmelCase	:	Any=13   ,							_UpperCAmelCase	:	Optional[int]=30   ,							_UpperCAmelCase	:	Tuple=2   ,							_UpperCAmelCase	:	int=3   ,							_UpperCAmelCase	:	Tuple=True   ,							_UpperCAmelCase	:	Optional[Any]=True   ,							_UpperCAmelCase	:	List[Any]=32   ,							_UpperCAmelCase	:	Dict=4   ,							_UpperCAmelCase	:	Dict=4   ,							_UpperCAmelCase	:	Optional[int]=37   ,							_UpperCAmelCase	:	str="gelu"   ,							_UpperCAmelCase	:	Dict=0.1   ,							_UpperCAmelCase	:	Union[str, Any]=0.1   ,							_UpperCAmelCase	:	Dict=10   ,							_UpperCAmelCase	:	List[str]=0.02   ,							_UpperCAmelCase	:	Optional[Any]=3   ,							_UpperCAmelCase	:	List[str]=None   ,							_UpperCAmelCase	:	Dict=[0, 1, 2, 3]   ,							):
       _A        =       parent
       _A        =       100
       _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
       _A        =       scope
       _A        =       out_indices
       _A        =       num_labels

       # 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	:	Dict		):
       _A        =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

       _A        =       None
       _A        =       None
       if self.use_labels:
             _A        =       ids_tensor([self.batch_size]   ,							self.type_sequence_label_size		)
             _A        =       ids_tensor([self.batch_size, self.image_size, self.image_size]   ,							self.num_labels		)

       _A        =       self.get_config()

       return config, pixel_values, labels, pixel_labels

 def       lowerCAmelCase_							(			self	:	Tuple		):
       return 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=UpperCamelCase__   ,							initializer_range=self.initializer_range   ,							out_indices=self.out_indices   ,							)

 def       lowerCAmelCase_							(			self	:	Optional[Any]   ,							_UpperCAmelCase	:	Tuple   ,							_UpperCAmelCase	:	Tuple   ,							_UpperCAmelCase	:	Tuple   ,							_UpperCAmelCase	:	Union[str, Any]		):
       _A        =       BeitModel(config=UpperCamelCase__		)
       model.to(UpperCamelCase__		)
       model.eval()
       _A        =       model(UpperCamelCase__		)
       self.parent.assertEqual(result.last_hidden_state.shape   ,							(self.batch_size, self.seq_length, self.hidden_size)		)

 def       lowerCAmelCase_							(			self	:	Any   ,							_UpperCAmelCase	:	Union[str, Any]   ,							_UpperCAmelCase	:	List[Any]   ,							_UpperCAmelCase	:	Union[str, Any]   ,							_UpperCAmelCase	:	List[str]		):
       _A        =       BeitForMaskedImageModeling(config=UpperCamelCase__		)
       model.to(UpperCamelCase__		)
       model.eval()
       _A        =       model(UpperCamelCase__		)
       self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.seq_length - 1, self.vocab_size)		)

 def       lowerCAmelCase_							(			self	:	Any   ,							_UpperCAmelCase	:	Optional[Any]   ,							_UpperCAmelCase	:	List[Any]   ,							_UpperCAmelCase	:	Tuple   ,							_UpperCAmelCase	:	Dict		):
       _A        =       self.type_sequence_label_size
       _A        =       BeitForImageClassification(UpperCamelCase__		)
       model.to(UpperCamelCase__		)
       model.eval()
       _A        =       model(UpperCamelCase__   ,							labels=UpperCamelCase__		)
       self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.type_sequence_label_size)		)

       # test greyscale images
       _A        =       1
       _A        =       BeitForImageClassification(UpperCamelCase__		)
       model.to(UpperCamelCase__		)
       model.eval()

       _A        =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
       _A        =       model(UpperCamelCase__   ,							labels=UpperCamelCase__		)
       self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.type_sequence_label_size)		)

 def       lowerCAmelCase_							(			self	:	str   ,							_UpperCAmelCase	:	Dict   ,							_UpperCAmelCase	:	Optional[Any]   ,							_UpperCAmelCase	:	Optional[Any]   ,							_UpperCAmelCase	:	Union[str, Any]		):
       _A        =       self.num_labels
       _A        =       BeitForSemanticSegmentation(UpperCamelCase__		)
       model.to(UpperCamelCase__		)
       model.eval()
       _A        =       model(UpperCamelCase__		)
       self.parent.assertEqual(
           result.logits.shape   ,							(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)		)
       _A        =       model(UpperCamelCase__   ,							labels=UpperCamelCase__		)
       self.parent.assertEqual(
           result.logits.shape   ,							(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)		)



 def       lowerCAmelCase_							(			self	:	str		):
       _A        =       self.prepare_config_and_inputs()
       _A       ,   _A       ,   _A       ,   _A        =       config_and_inputs
       _A        =       {'pixel_values': pixel_values}
       return config, inputs_dict



@require_torch
class     lowercase_	(					__a      ,     __a      ,     unittest.TestCase							):



 '''simple docstring'''
 UpperCAmelCase   :					int				  =     (
     (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
     if is_torch_available()
     else ()
 )
 UpperCAmelCase   :					str				  =     (
     {
         '''feature-extraction''': BeitModel,
         '''image-classification''': BeitForImageClassification,
         '''image-segmentation''': BeitForSemanticSegmentation,
     }
     if is_torch_available()
     else {}
 )

 UpperCAmelCase   :					int				  =     False
 UpperCAmelCase   :					Tuple				  =     False
 UpperCAmelCase   :					str				  =     False

 def       lowerCAmelCase_							(			self	:	Tuple		):
       _A        =       BeitModelTester(self		)
       _A        =       ConfigTester(self   ,							config_class=UpperCamelCase__   ,							has_text_modality=UpperCamelCase__   ,							hidden_size=37		)

 def       lowerCAmelCase_							(			self	:	int		):
       self.config_tester.run_common_tests()

 @unittest.skip(reason='BEiT does not use inputs_embeds'		)
 def       lowerCAmelCase_							(			self	:	List[str]		):
       pass

 @require_torch_multi_gpu
 @unittest.skip(reason='BEiT has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'		)
 def       lowerCAmelCase_							(			self	:	Optional[int]		):
       pass

 def       lowerCAmelCase_							(			self	:	str		):
       _A       ,   _A        =       self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
             _A        =       model_class(UpperCamelCase__		)
             self.assertIsInstance(model.get_input_embeddings()   ,							(nn.Module)		)
             _A        =       model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(UpperCamelCase__   ,							nn.Linear		)		)

 def       lowerCAmelCase_							(			self	:	Dict		):
       _A       ,   _A        =       self.model_tester.prepare_config_and_inputs_for_common()

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

             _A        =       ['pixel_values']
             self.assertListEqual(arg_names[:1]   ,							UpperCamelCase__		)

 def       lowerCAmelCase_							(			self	:	Any		):
       _A        =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*UpperCamelCase__		)

 def       lowerCAmelCase_							(			self	:	Union[str, Any]		):
       _A        =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase__		)

 def       lowerCAmelCase_							(			self	:	List[str]		):
       _A        =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__		)

 def       lowerCAmelCase_							(			self	:	int		):
       _A        =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_semantic_segmentation(*UpperCamelCase__		)

 def       lowerCAmelCase_							(			self	:	Optional[Any]		):



       if not self.model_tester.is_training:
             return

       _A       ,   _A        =       self.model_tester.prepare_config_and_inputs_for_common()
       _A        =       True

       for model_class in self.all_model_classes:
             # we don't test BeitForMaskedImageModeling
             if model_class in [*get_values(UpperCamelCase__		), BeitForMaskedImageModeling]:
                   continue

             _A        =       model_class(UpperCamelCase__		)
             model.to(UpperCamelCase__		)
             model.train()
             _A        =       self._prepare_for_class(UpperCamelCase__   ,							UpperCamelCase__   ,							return_labels=UpperCamelCase__		)
             _A        =       model(**UpperCamelCase__		).loss
             loss.backward()

 def       lowerCAmelCase_							(			self	:	List[str]		):
       _A       ,   _A        =       self.model_tester.prepare_config_and_inputs_for_common()
       if not self.model_tester.is_training:
             return

       _A        =       False
       _A        =       True

       for model_class in self.all_model_classes:
             # we don't test BeitForMaskedImageModeling
             if (
                 model_class in [*get_values(UpperCamelCase__		), BeitForMaskedImageModeling]
                 or not model_class.supports_gradient_checkpointing
             ):
                   continue

             _A        =       model_class(UpperCamelCase__		)
             model.gradient_checkpointing_enable()
             model.to(UpperCamelCase__		)
             model.train()
             _A        =       self._prepare_for_class(UpperCamelCase__   ,							UpperCamelCase__   ,							return_labels=UpperCamelCase__		)
             _A        =       model(**UpperCamelCase__		).loss
             loss.backward()

 def       lowerCAmelCase_							(			self	:	Union[str, Any]		):
       _A       ,   _A        =       self.model_tester.prepare_config_and_inputs_for_common()

       _A        =       _config_zero_init(UpperCamelCase__		)
       for model_class in self.all_model_classes:
             _A        =       model_class(config=UpperCamelCase__		)
             for name, param in model.named_parameters():
                   # we skip lambda parameters as these require special initial values
                   # determined by config.layer_scale_init_value
                   if "lambda" in name:
                         continue
                   if param.requires_grad:
                         self.assertIn(
                             ((param.data.mean() * 1E9).round() / 1E9).item()   ,							[0.0, 1.0]   ,							msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''   ,							)



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



       for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _A        =       BeitModel.from_pretrained(UpperCamelCase__		)
             self.assertIsNotNone(UpperCamelCase__		)






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






      '''simple docstring'''


      _A        =       Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'							)
      return image







@require_torch
@require_vision
class     lowercase_	(					unittest.TestCase							):



 '''simple docstring'''

 @cached_property
 def       lowerCAmelCase_							(			self	:	Union[str, Any]		):
       return BeitImageProcessor.from_pretrained('microsoft/beit-base-patch16-224'		) if is_vision_available() else None

 @slow
 def       lowerCAmelCase_							(			self	:	Optional[Any]		):
       _A        =       BeitForMaskedImageModeling.from_pretrained('microsoft/beit-base-patch16-224-pt22k'		).to(UpperCamelCase__		)

       _A        =       self.default_image_processor
       _A        =       prepare_img()
       _A        =       image_processor(images=UpperCamelCase__   ,							return_tensors='pt'		).pixel_values.to(UpperCamelCase__		)

       # prepare bool_masked_pos
       _A        =       torch.ones((1, 196)   ,							dtype=torch.bool		).to(UpperCamelCase__		)

       # forward pass
       with torch.no_grad():
             _A        =       model(pixel_values=UpperCamelCase__   ,							bool_masked_pos=UpperCamelCase__		)
       _A        =       outputs.logits

       # verify the logits
       _A        =       torch.Size((1, 196, 8_192)		)
       self.assertEqual(logits.shape   ,							UpperCamelCase__		)

       _A        =       torch.tensor(
           [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]]		).to(UpperCamelCase__		)

       self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3]   ,							UpperCamelCase__   ,							atol=1E-2		)		)

 @slow
 def       lowerCAmelCase_							(			self	:	Any		):
       _A        =       BeitForImageClassification.from_pretrained('microsoft/beit-base-patch16-224'		).to(UpperCamelCase__		)

       _A        =       self.default_image_processor
       _A        =       prepare_img()
       _A        =       image_processor(images=UpperCamelCase__   ,							return_tensors='pt'		).to(UpperCamelCase__		)

       # forward pass
       with torch.no_grad():
             _A        =       model(**UpperCamelCase__		)
       _A        =       outputs.logits

       # verify the logits
       _A        =       torch.Size((1, 1_000)		)
       self.assertEqual(logits.shape   ,							UpperCamelCase__		)

       _A        =       torch.tensor([-1.2385, -1.0987, -1.0108]		).to(UpperCamelCase__		)

       self.assertTrue(torch.allclose(logits[0, :3]   ,							UpperCamelCase__   ,							atol=1E-4		)		)

       _A        =       281
       self.assertEqual(logits.argmax(-1		).item()   ,							UpperCamelCase__		)

 @slow
 def       lowerCAmelCase_							(			self	:	Union[str, Any]		):
       _A        =       BeitForImageClassification.from_pretrained('microsoft/beit-large-patch16-224-pt22k-ft22k'		).to(
           UpperCamelCase__		)

       _A        =       self.default_image_processor
       _A        =       prepare_img()
       _A        =       image_processor(images=UpperCamelCase__   ,							return_tensors='pt'		).to(UpperCamelCase__		)

       # forward pass
       with torch.no_grad():
             _A        =       model(**UpperCamelCase__		)
       _A        =       outputs.logits

       # verify the logits
       _A        =       torch.Size((1, 21_841)		)
       self.assertEqual(logits.shape   ,							UpperCamelCase__		)

       _A        =       torch.tensor([1.6881, -0.2787, 0.5901]		).to(UpperCamelCase__		)

       self.assertTrue(torch.allclose(logits[0, :3]   ,							UpperCamelCase__   ,							atol=1E-4		)		)

       _A        =       2_396
       self.assertEqual(logits.argmax(-1		).item()   ,							UpperCamelCase__		)

 @slow
 def       lowerCAmelCase_							(			self	:	int		):
       _A        =       BeitForSemanticSegmentation.from_pretrained('microsoft/beit-base-finetuned-ade-640-640'		)
       _A        =       model.to(UpperCamelCase__		)

       _A        =       BeitImageProcessor(do_resize=UpperCamelCase__   ,							size=640   ,							do_center_crop=UpperCamelCase__		)

       _A        =       load_dataset('hf-internal-testing/fixtures_ade20k'   ,							split='test'		)
       _A        =       Image.open(ds[0]['file']		)
       _A        =       image_processor(images=UpperCamelCase__   ,							return_tensors='pt'		).to(UpperCamelCase__		)

       # forward pass
       with torch.no_grad():
             _A        =       model(**UpperCamelCase__		)
       _A        =       outputs.logits

       # verify the logits
       _A        =       torch.Size((1, 150, 160, 160)		)
       self.assertEqual(logits.shape   ,							UpperCamelCase__		)

       _A        =       version.parse(PIL.__version__		) < version.parse('9.0.0'		)

       if is_pillow_less_than_a:
             _A        =       torch.tensor(
                 [
                     [[-4.9225, -2.3954, -3.0522], [-2.8822, -1.0046, -1.7561], [-2.9549, -1.3228, -2.1347]],
                     [[-5.8168, -3.4129, -4.0778], [-3.8651, -2.2214, -3.0277], [-3.8356, -2.4643, -3.3535]],
                     [[-0.0078, 3.9952, 4.0754], [2.9856, 4.6944, 5.0035], [3.2413, 4.7813, 4.9969]],
                 ]   ,							device=UpperCamelCase__   ,							)
       else:
             _A        =       torch.tensor(
                 [
                     [[-4.8960, -2.3688, -3.0355], [-2.8478, -0.9836, -1.7418], [-2.9449, -1.3332, -2.1456]],
                     [[-5.8081, -3.4124, -4.1006], [-3.8561, -2.2081, -3.0323], [-3.8365, -2.4601, -3.3669]],
                     [[-0.0309, 3.9868, 4.0540], [2.9640, 4.6877, 4.9976], [3.2081, 4.7690, 4.9942]],
                 ]   ,							device=UpperCamelCase__   ,							)

       self.assertTrue(torch.allclose(logits[0, :3, :3, :3]   ,							UpperCamelCase__   ,							atol=1E-4		)		)



 @slow
 def       lowerCAmelCase_							(			self	:	Dict		):
       _A        =       BeitForSemanticSegmentation.from_pretrained('microsoft/beit-base-finetuned-ade-640-640'		)
       _A        =       model.to(UpperCamelCase__		)

       _A        =       BeitImageProcessor(do_resize=UpperCamelCase__   ,							size=640   ,							do_center_crop=UpperCamelCase__		)

       _A        =       load_dataset('hf-internal-testing/fixtures_ade20k'   ,							split='test'		)
       _A        =       Image.open(ds[0]['file']		)
       _A        =       image_processor(images=UpperCamelCase__   ,							return_tensors='pt'		).to(UpperCamelCase__		)

       # forward pass
       with torch.no_grad():
             _A        =       model(**UpperCamelCase__		)

       _A        =       outputs.logits.detach().cpu()

       _A        =       image_processor.post_process_semantic_segmentation(outputs=UpperCamelCase__   ,							target_sizes=[(500, 300)]		)
       _A        =       torch.Size((500, 300)		)
       self.assertEqual(segmentation[0].shape   ,							UpperCamelCase__		)

       _A        =       image_processor.post_process_semantic_segmentation(outputs=UpperCamelCase__		)
       _A        =       torch.Size((160, 160)		)
       self.assertEqual(segmentation[0].shape   ,							UpperCamelCase__		)







"""simple docstring"""

import logging
from dataclasses import dataclass, field
from pathlib import Path
from typing import Optional, Union

from .generation.configuration_utils import GenerationConfig
from .training_args import TrainingArguments
from .utils import add_start_docstrings


a         =      logging.getLogger(__name__)





@dataclass
@add_start_docstrings(TrainingArguments.__doc__							)
class     lowercase_	(					__lowerCAmelCase							):



 '''simple docstring'''
 UpperCAmelCase   :					bool				  =     field(default=__lowerCAmelCase      ,     metadata={'''help''': '''Whether to use SortishSampler or not.'''}							)
 UpperCAmelCase   :					bool				  =     field(
     default=__lowerCAmelCase      ,     metadata={'''help''': '''Whether to use generate to calculate generative metrics (ROUGE, BLEU).'''}							)
 UpperCAmelCase   :					Optional[int]				  =     field(
     default=__lowerCAmelCase      ,     metadata={
         '''help''': (
             '''The `max_length` to use on each evaluation loop when `predict_with_generate=True`. Will default '''
             '''to the `max_length` value of the model configuration.'''
         )
     }      ,     )
 UpperCAmelCase   :					Optional[int]				  =     field(
     default=__lowerCAmelCase      ,     metadata={
         '''help''': (
             '''The `num_beams` to use on each evaluation loop when `predict_with_generate=True`. Will default '''
             '''to the `num_beams` value of the model configuration.'''
         )
     }      ,     )
 UpperCAmelCase   :					Optional[Union[str, Path, GenerationConfig]]				  =     field(
     default=__lowerCAmelCase      ,     metadata={
         '''help''': '''Model id, file path or url pointing to a GenerationConfig json file, to use during prediction.'''
     }      ,     )

 def       lowerCAmelCase_							(			self	:	int		):
       _A        =       super().to_dict()
       for k, v in d.items():
             if isinstance(_UpperCAmelCase   ,							_UpperCAmelCase		):
                   _A        =       v.to_dict()
       return d

