






"""simple docstring"""





import inspect
import unittest

from transformers import DPTConfig
from transformers.file_utils import is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

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, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel
    from transformers.models.dpt.modeling_dpt import DPT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import DPTImageProcessor


class      _lowercase :


      def __init__(    self				:	Optional[int]							,   a				:	List[str]							,   a				:	str=2							,   a				:	Dict=3_2							,   a				:	Tuple=1_6							,   a				:	str=3							,   a				:	Union[str, Any]=True							,   a				:	Union[str, Any]=True							,   a				:	List[str]=3_2							,   a				:	Union[str, Any]=4							,   a				:	Optional[int]=[0, 1, 2, 3]							,   a				:	Dict=4							,   a				:	Optional[Any]=3_7							,   a				:	Tuple="gelu"							,   a				:	Optional[Any]=0.1							,   a				:	List[str]=0.1							,   a				:	Tuple=0.0_2							,   a				:	Any=3							,   a				:	Optional[int]=[1, 3_8_4, 2_4, 2_4]							,   a				:	int=True							,   a				:	Any=None							,   ):





            """simple docstring"""







            __snake_case      :    str  			=parent
            __snake_case      :    int  			=batch_size
            __snake_case      :    Optional[int]  			=image_size
            __snake_case      :    Optional[Any]  			=patch_size
            __snake_case      :    Optional[int]  			=num_channels
            __snake_case      :    List[str]  			=is_training
            __snake_case      :    Optional[Any]  			=use_labels
            __snake_case      :    Optional[int]  			=hidden_size
            __snake_case      :    str  			=num_hidden_layers
            __snake_case      :    Optional[Any]  			=backbone_out_indices
            __snake_case      :    Optional[int]  			=num_attention_heads
            __snake_case      :    List[Any]  			=intermediate_size
            __snake_case      :    Union[str, Any]  			=hidden_act
            __snake_case      :    List[Any]  			=hidden_dropout_prob
            __snake_case      :    Any  			=attention_probs_dropout_prob
            __snake_case      :    Any  			=initializer_range
            __snake_case      :    Union[str, Any]  			=num_labels
            __snake_case      :    int  			=backbone_featmap_shape
            __snake_case      :    Optional[int]  			=scope
            __snake_case      :    Union[str, Any]  			=is_hybrid
            # sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token)
            __snake_case      :    str  			=(image_size // patch_size) ** 2
            __snake_case      :    List[Any]  			=num_patches + 1


      def _UpperCamelCase						(    self				:	Dict						):





            """simple docstring"""







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

            __snake_case      :    Optional[Any]  			=None
            if self.use_labels:
                  __snake_case      :    str  			=ids_tensor([self.batch_size, self.image_size, self.image_size]							,   self.num_labels						)

            __snake_case      :    List[Any]  			=self.get_config()

            return config, pixel_values, labels


      def _UpperCamelCase						(    self				:	Optional[Any]						):





            """simple docstring"""







            __snake_case      :    Tuple  			={
                '''global_padding''': '''same''',
                '''layer_type''': '''bottleneck''',
                '''depths''': [3, 4, 9],
                '''out_features''': ['''stage1''', '''stage2''', '''stage3'''],
                '''embedding_dynamic_padding''': True,
                '''hidden_sizes''': [9_6, 1_9_2, 3_8_4, 7_6_8],
                '''num_groups''': 2,
            }

            return DPTConfig(
                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							,   backbone_out_indices=self.backbone_out_indices							,   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							,   is_hybrid=self.is_hybrid							,   backbone_config=a							,   backbone_featmap_shape=self.backbone_featmap_shape							,   )


      def _UpperCamelCase						(    self				:	List[Any]							,   a				:	Dict							,   a				:	Optional[Any]							,   a				:	int						):





            """simple docstring"""







            __snake_case      :    Tuple  			=DPTModel(config=a						)
            model.to(a						)
            model.eval()
            __snake_case      :    List[str]  			=model(a						)
            self.parent.assertEqual(result.last_hidden_state.shape							,   (self.batch_size, self.seq_length, self.hidden_size)						)


      def _UpperCamelCase						(    self				:	Optional[Any]							,   a				:	Optional[int]							,   a				:	List[Any]							,   a				:	List[str]						):





            """simple docstring"""







            __snake_case      :    Dict  			=self.num_labels
            __snake_case      :    Optional[Any]  			=DPTForDepthEstimation(a						)
            model.to(a						)
            model.eval()
            __snake_case      :    Any  			=model(a						)
            self.parent.assertEqual(result.predicted_depth.shape							,   (self.batch_size, self.image_size, self.image_size)						)


      def _UpperCamelCase						(    self				:	int							,   a				:	str							,   a				:	Union[str, Any]							,   a				:	Optional[int]						):





            """simple docstring"""







            __snake_case      :    str  			=self.num_labels
            __snake_case      :    Dict  			=DPTForSemanticSegmentation(a						)
            model.to(a						)
            model.eval()
            __snake_case      :    Optional[Any]  			=model(a							,   labels=a						)
            self.parent.assertEqual(
                result.logits.shape							,   (self.batch_size, self.num_labels, self.image_size, self.image_size)						)







      def _UpperCamelCase						(    self				:	Tuple						):





            """simple docstring"""







            __snake_case      :    int  			=self.prepare_config_and_inputs()
            __snake_case			,				__snake_case			,				__snake_case      :    List[str]  			=config_and_inputs
            __snake_case      :    Tuple  			={'''pixel_values''': pixel_values}
            return config, inputs_dict



@require_torch
class      _lowercase (     lowerCAmelCase		,  lowerCAmelCase		,  unittest.TestCase						):
      _a   :     str								=   (DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else ()
      _a   :     Dict								=   (
          {
              '''depth-estimation''': DPTForDepthEstimation,
              '''feature-extraction''': DPTModel,
              '''image-segmentation''': DPTForSemanticSegmentation,
          }
          if is_torch_available()
          else {}
      )

      _a   :     Optional[int]								=   False
      _a   :     List[Any]								=   False
      _a   :     int								=   False


      def _UpperCamelCase						(    self				:	int						):





            """simple docstring"""







            __snake_case      :    Any  			=DPTModelTester(self						)
            __snake_case      :    int  			=ConfigTester(self							,   config_class=a							,   has_text_modality=a							,   hidden_size=3_7						)


      def _UpperCamelCase						(    self				:	int						):





            """simple docstring"""







            self.config_tester.run_common_tests()


      @unittest.skip(reason='''DPT does not use inputs_embeds'''						)
      def _UpperCamelCase						(    self				:	Tuple						):





            """simple docstring"""







            pass


      def _UpperCamelCase						(    self				:	Any						):





            """simple docstring"""







            __snake_case			,				__snake_case      :    Union[str, Any]  			=self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                  __snake_case      :    List[Any]  			=model_class(a						)
                  self.assertIsInstance(model.get_input_embeddings()							,   (nn.Module)						)
                  __snake_case      :    List[str]  			=model.get_output_embeddings()
                  self.assertTrue(x is None or isinstance(a							,   nn.Linear						)						)


      def _UpperCamelCase						(    self				:	int						):





            """simple docstring"""







            __snake_case			,				__snake_case      :    Dict  			=self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                  __snake_case      :    Union[str, Any]  			=model_class(a						)
                  __snake_case      :    Optional[Any]  			=inspect.signature(model.forward						)
                  # signature.parameters is an OrderedDict => so arg_names order is deterministic
                  __snake_case      :    List[Any]  			=[*signature.parameters.keys()]

                  __snake_case      :    Dict  			=['''pixel_values''']
                  self.assertListEqual(arg_names[:1]							,   a						)


      def _UpperCamelCase						(    self				:	List[Any]						):





            """simple docstring"""







            __snake_case      :    List[str]  			=self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*a						)


      def _UpperCamelCase						(    self				:	List[Any]						):





            """simple docstring"""







            __snake_case      :    Optional[int]  			=self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_depth_estimation(*a						)


      def _UpperCamelCase						(    self				:	Optional[Any]						):





            """simple docstring"""







            __snake_case      :    List[Any]  			=self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_semantic_segmentation(*a						)


      def _UpperCamelCase						(    self				:	Dict						):





            """simple docstring"""







            for model_class in self.all_model_classes:
                  if model_class.__name__ == "DPTForDepthEstimation":
                        continue

                  __snake_case			,				__snake_case      :    Union[str, Any]  			=self.model_tester.prepare_config_and_inputs_for_common()
                  __snake_case      :    List[Any]  			=True

                  if model_class in get_values(a						):
                        continue

                  __snake_case      :    Tuple  			=model_class(a						)
                  model.to(a						)
                  model.train()
                  __snake_case      :    List[str]  			=self._prepare_for_class(a							,   a							,   return_labels=a						)
                  __snake_case      :    int  			=model(**a						).loss
                  loss.backward()


      def _UpperCamelCase						(    self				:	str						):





            """simple docstring"""







            for model_class in self.all_model_classes:
                  if model_class.__name__ == "DPTForDepthEstimation":
                        continue

                  __snake_case			,				__snake_case      :    Tuple  			=self.model_tester.prepare_config_and_inputs_for_common()
                  __snake_case      :    int  			=False
                  __snake_case      :    str  			=True

                  if model_class in get_values(a						) or not model_class.supports_gradient_checkpointing:
                        continue
                  __snake_case      :    List[Any]  			=model_class(a						)
                  model.to(a						)
                  model.gradient_checkpointing_enable()
                  model.train()
                  __snake_case      :    List[Any]  			=self._prepare_for_class(a							,   a							,   return_labels=a						)
                  __snake_case      :    Any  			=model(**a						).loss
                  loss.backward()


      def _UpperCamelCase						(    self				:	Tuple						):





            """simple docstring"""







            __snake_case			,				__snake_case      :    Optional[Any]  			=self.model_tester.prepare_config_and_inputs_for_common()

            __snake_case      :    List[str]  			=_config_zero_init(a						)
            for model_class in self.all_model_classes:
                  __snake_case      :    List[str]  			=model_class(config=a						)
                  # Skip the check for the backbone
                  __snake_case      :    List[Any]  			=[]
                  for name, module in model.named_modules():
                        if module.__class__.__name__ == "DPTViTHybridEmbeddings":
                              __snake_case      :    Any  			=[f'''{name}.{key}''' for key in module.state_dict().keys()]
                              break

                  for name, param in model.named_parameters():
                        if param.requires_grad:
                              if name in backbone_params:
                                    continue
                              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'''							,   )


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





            """simple docstring"""







            pass


      @slow
      def _UpperCamelCase						(    self				:	Optional[Any]						):





            """simple docstring"""







            for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[1:]:
                  __snake_case      :    Dict  			=DPTModel.from_pretrained(a						)
                  self.assertIsNotNone(a						)







      def _UpperCamelCase						(    self				:	List[Any]						):





            """simple docstring"""







            __snake_case			,				__snake_case      :    int  			=self.model_tester.prepare_config_and_inputs_for_common()
            __snake_case      :    Tuple  			='''add'''
            with self.assertRaises(a						):
                  __snake_case      :    List[Any]  			=DPTForDepthEstimation(a						)



def     __lowercase					(    )					->					Any:
      __snake_case      :    Any  			=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
      return image


@require_torch
@require_vision
@slow
class      _lowercase (     unittest.TestCase						):


      def _UpperCamelCase						(    self				:	Union[str, Any]						):





            """simple docstring"""







            __snake_case      :    Tuple  			=DPTImageProcessor.from_pretrained('''Intel/dpt-hybrid-midas'''						)
            __snake_case      :    Dict  			=DPTForDepthEstimation.from_pretrained('''Intel/dpt-hybrid-midas'''						).to(a						)

            __snake_case      :    List[Any]  			=prepare_img()
            __snake_case      :    Optional[Any]  			=image_processor(images=a							,   return_tensors='''pt'''						).to(a						)

            # forward pass
            with torch.no_grad():
                  __snake_case      :    Union[str, Any]  			=model(**a						)
                  __snake_case      :    Union[str, Any]  			=outputs.predicted_depth

            # verify the predicted depth
            __snake_case      :    List[Any]  			=torch.Size((1, 3_8_4, 3_8_4)						)
            self.assertEqual(predicted_depth.shape							,   a						)

            __snake_case      :    Union[str, Any]  			=torch.tensor(
                [[[5.6_4_3_7, 5.6_1_4_6, 5.6_5_1_1], [5.4_3_7_1, 5.5_6_4_9, 5.5_9_5_8], [5.5_2_1_5, 5.5_1_8_4, 5.5_2_9_3]]]						).to(a						)

            self.assertTrue(torch.allclose(outputs.predicted_depth[:3, :3, :3] / 1_0_0							,   a							,   atol=1e-4						)						)










"""simple docstring"""





import unittest

from transformers import BertGenerationConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import BertGenerationDecoder, BertGenerationEncoder


class      _lowercase :


      def __init__(    self				:	Optional[Any]							,   a				:	List[str]							,   a				:	List[Any]=1_3							,   a				:	Any=7							,   a				:	List[str]=True							,   a				:	int=True							,   a				:	Optional[int]=9_9							,   a				:	List[Any]=3_2							,   a				:	Optional[int]=5							,   a				:	List[Any]=4							,   a				:	Dict=3_7							,   a				:	List[str]="gelu"							,   a				:	Union[str, Any]=0.1							,   a				:	str=0.1							,   a				:	List[str]=5_0							,   a				:	Tuple=0.0_2							,   a				:	Union[str, Any]=True							,   a				:	int=None							,   ):





            """simple docstring"""







            __snake_case      :    Optional[int]  			=parent
            __snake_case      :    Dict  			=batch_size
            __snake_case      :    Union[str, Any]  			=seq_length
            __snake_case      :    Optional[Any]  			=is_training
            __snake_case      :    Any  			=use_input_mask
            __snake_case      :    Union[str, Any]  			=vocab_size
            __snake_case      :    Union[str, Any]  			=hidden_size
            __snake_case      :    int  			=num_hidden_layers
            __snake_case      :    Optional[int]  			=num_attention_heads
            __snake_case      :    int  			=intermediate_size
            __snake_case      :    str  			=hidden_act
            __snake_case      :    str  			=hidden_dropout_prob
            __snake_case      :    Tuple  			=attention_probs_dropout_prob
            __snake_case      :    Tuple  			=max_position_embeddings
            __snake_case      :    Optional[Any]  			=initializer_range
            __snake_case      :    List[str]  			=use_labels
            __snake_case      :    List[Any]  			=scope


      def _UpperCamelCase						(    self				:	List[Any]						):





            """simple docstring"""







            __snake_case      :    str  			=ids_tensor([self.batch_size, self.seq_length]							,   self.vocab_size						)

            __snake_case      :    Optional[int]  			=None
            if self.use_input_mask:
                  __snake_case      :    Optional[Any]  			=random_attention_mask([self.batch_size, self.seq_length]						)

            if self.use_labels:
                  __snake_case      :    int  			=ids_tensor([self.batch_size, self.seq_length]							,   self.vocab_size						)

            __snake_case      :    Any  			=self.get_config()

            return config, input_ids, input_mask, token_labels


      def _UpperCamelCase						(    self				:	Union[str, Any]						):





            """simple docstring"""







            return BertGenerationConfig(
                vocab_size=self.vocab_size							,   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							,   max_position_embeddings=self.max_position_embeddings							,   is_decoder=a							,   initializer_range=self.initializer_range							,   )


      def _UpperCamelCase						(    self				:	Union[str, Any]						):





            """simple docstring"""







            (
                (
                __snake_case
            )			,				(
                __snake_case
            )			,				(
                __snake_case
            )			,				(
                __snake_case
            )			,				
            )      :    Dict  			=self.prepare_config_and_inputs()

            __snake_case      :    Dict  			=True
            __snake_case      :    int  			=floats_tensor([self.batch_size, self.seq_length, self.hidden_size]						)
            __snake_case      :    Union[str, Any]  			=ids_tensor([self.batch_size, self.seq_length]							,   vocab_size=2						)

            return (
                config,
                input_ids,
                input_mask,
                token_labels,
                encoder_hidden_states,
                encoder_attention_mask,
            )


      def _UpperCamelCase						(    self				:	Optional[Any]							,   a				:	Union[str, Any]							,   a				:	Optional[int]							,   a				:	int							,   a				:	Tuple							,   **a				:	Any							,   ):





            """simple docstring"""







            __snake_case      :    List[Any]  			=BertGenerationEncoder(config=a						)
            model.to(a						)
            model.eval()
            __snake_case      :    Dict  			=model(a							,   attention_mask=a						)
            __snake_case      :    Tuple  			=model(a						)
            self.parent.assertEqual(result.last_hidden_state.shape							,   (self.batch_size, self.seq_length, self.hidden_size)						)


      def _UpperCamelCase						(    self				:	Tuple							,   a				:	Union[str, Any]							,   a				:	Optional[Any]							,   a				:	str							,   a				:	List[Any]							,   a				:	Optional[int]							,   a				:	Optional[Any]							,   **a				:	int							,   ):





            """simple docstring"""







            __snake_case      :    Any  			=True
            __snake_case      :    List[str]  			=BertGenerationEncoder(config=a						)
            model.to(a						)
            model.eval()
            __snake_case      :    Union[str, Any]  			=model(
                a							,   attention_mask=a							,   encoder_hidden_states=a							,   encoder_attention_mask=a							,   )
            __snake_case      :    str  			=model(
                a							,   attention_mask=a							,   encoder_hidden_states=a							,   )
            self.parent.assertEqual(result.last_hidden_state.shape							,   (self.batch_size, self.seq_length, self.hidden_size)						)


      def _UpperCamelCase						(    self				:	Any							,   a				:	List[str]							,   a				:	Dict							,   a				:	Optional[Any]							,   a				:	Tuple							,   a				:	Dict							,   a				:	Tuple							,   **a				:	Tuple							,   ):





            """simple docstring"""







            __snake_case      :    List[Any]  			=True
            __snake_case      :    Any  			=True
            __snake_case      :    int  			=BertGenerationDecoder(config=a						).to(a						).eval()

            # first forward pass
            __snake_case      :    List[str]  			=model(
                a							,   attention_mask=a							,   encoder_hidden_states=a							,   encoder_attention_mask=a							,   use_cache=a							,   )
            __snake_case      :    Optional[int]  			=outputs.past_key_values

            # create hypothetical multiple next token and extent to next_input_ids
            __snake_case      :    List[str]  			=ids_tensor((self.batch_size, 3)							,   config.vocab_size						)
            __snake_case      :    List[Any]  			=ids_tensor((self.batch_size, 3)							,   vocab_size=2						)

            # append to next input_ids and
            __snake_case      :    Tuple  			=torch.cat([input_ids, next_tokens]							,   dim=-1						)
            __snake_case      :    List[Any]  			=torch.cat([input_mask, next_mask]							,   dim=-1						)

            __snake_case      :    Tuple  			=model(
                a							,   attention_mask=a							,   encoder_hidden_states=a							,   encoder_attention_mask=a							,   output_hidden_states=a							,   )['''hidden_states'''][0]
            __snake_case      :    Any  			=model(
                a							,   attention_mask=a							,   encoder_hidden_states=a							,   encoder_attention_mask=a							,   past_key_values=a							,   output_hidden_states=a							,   )['''hidden_states'''][0]

            # select random slice
            __snake_case      :    int  			=ids_tensor((1,)							,   output_from_past.shape[-1]						).item()
            __snake_case      :    int  			=output_from_no_past[:, -3:, random_slice_idx].detach()
            __snake_case      :    int  			=output_from_past[:, :, random_slice_idx].detach()

            self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]						)

            # test that outputs are equal for slice
            self.parent.assertTrue(torch.allclose(a							,   a							,   atol=1e-3						)						)


      def _UpperCamelCase						(    self				:	Optional[Any]							,   a				:	List[Any]							,   a				:	Dict							,   a				:	Optional[Any]							,   a				:	List[str]							,   *a				:	Dict							,   ):





            """simple docstring"""







            __snake_case      :    List[str]  			=BertGenerationDecoder(a						)
            model.to(a						)
            model.eval()
            __snake_case      :    Any  			=model(a							,   attention_mask=a							,   labels=a						)
            self.parent.assertEqual(result.logits.shape							,   (self.batch_size, self.seq_length, self.vocab_size)						)







      def _UpperCamelCase						(    self				:	List[Any]						):





            """simple docstring"""







            __snake_case			,				__snake_case			,				__snake_case			,				__snake_case      :    Any  			=self.prepare_config_and_inputs()
            __snake_case      :    List[Any]  			={'''input_ids''': input_ids, '''attention_mask''': input_mask}
            return config, inputs_dict



@require_torch
class      _lowercase (     lowerCAmelCase		,  lowerCAmelCase		,  lowerCAmelCase		,  unittest.TestCase						):
      _a   :     Union[str, Any]								=   (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
      _a   :     List[Any]								=   (BertGenerationDecoder,) if is_torch_available() else ()
      _a   :     Union[str, Any]								=   (
          {'''feature-extraction''': BertGenerationEncoder, '''text-generation''': BertGenerationDecoder}
          if is_torch_available()
          else {}
      )


      def _UpperCamelCase						(    self				:	List[str]						):





            """simple docstring"""







            __snake_case      :    int  			=BertGenerationEncoderTester(self						)
            __snake_case      :    Union[str, Any]  			=ConfigTester(self							,   config_class=a							,   hidden_size=3_7						)


      def _UpperCamelCase						(    self				:	str						):





            """simple docstring"""







            self.config_tester.run_common_tests()


      def _UpperCamelCase						(    self				:	Union[str, Any]						):





            """simple docstring"""







            __snake_case      :    str  			=self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*a						)


      def _UpperCamelCase						(    self				:	Optional[Any]						):





            """simple docstring"""







            __snake_case			,				__snake_case			,				__snake_case			,				__snake_case      :    Tuple  			=self.model_tester.prepare_config_and_inputs()
            __snake_case      :    int  			='''bert'''
            self.model_tester.create_and_check_model(a							,   a							,   a							,   a						)


      def _UpperCamelCase						(    self				:	Dict						):





            """simple docstring"""







            __snake_case      :    List[str]  			=self.model_tester.prepare_config_and_inputs_for_decoder()
            self.model_tester.create_and_check_model_as_decoder(*a						)


      def _UpperCamelCase						(    self				:	Tuple						):





            """simple docstring"""







            __snake_case      :    Dict  			=self.model_tester.prepare_config_and_inputs_for_decoder()
            self.model_tester.create_and_check_decoder_model_past_large_inputs(*a						)


      def _UpperCamelCase						(    self				:	List[str]						):





            """simple docstring"""







            (
                (
                __snake_case
            )			,				(
                __snake_case
            )			,				(
                __snake_case
            )			,				(
                __snake_case
            )			,				(
                __snake_case
            )			,				(
                __snake_case
            )			,				
            )      :    Union[str, Any]  			=self.model_tester.prepare_config_and_inputs_for_decoder()

            __snake_case      :    Tuple  			=None

            self.model_tester.create_and_check_model_as_decoder(
                a							,   a							,   a							,   a							,   a							,   a							,   )


      def _UpperCamelCase						(    self				:	List[Any]						):





            """simple docstring"""







            __snake_case      :    Tuple  			=self.model_tester.prepare_config_and_inputs_for_decoder()
            self.model_tester.create_and_check_for_causal_lm(*a						)







      @slow
      def _UpperCamelCase						(    self				:	Tuple						):





            """simple docstring"""







            __snake_case      :    Optional[int]  			=BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''						)
            self.assertIsNotNone(a						)



@require_torch
class      _lowercase (     unittest.TestCase						):


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





            """simple docstring"""







            __snake_case      :    List[Any]  			=BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''						)
            __snake_case      :    Any  			=torch.tensor([[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_2_6, 3_8_9_9, 2_0_0_3, 1_0_1_4_0, 1_0_2]]						)
            with torch.no_grad():
                  __snake_case      :    str  			=model(a						)[0]
            __snake_case      :    List[str]  			=torch.Size([1, 8, 1_0_2_4]						)
            self.assertEqual(output.shape							,   a						)
            __snake_case      :    int  			=torch.tensor(
                [[[0.1_7_7_5, 0.0_0_8_3, -0.0_3_2_1], [1.6_0_0_2, 0.1_2_8_7, 0.3_9_1_2], [2.1_4_7_3, 0.5_7_9_1, 0.6_0_6_6]]]						)
            self.assertTrue(torch.allclose(output[:, :3, :3]							,   a							,   atol=1e-4						)						)



@require_torch
class      _lowercase (     unittest.TestCase						):


      @slow
      def _UpperCamelCase						(    self				:	Any						):





            """simple docstring"""







            __snake_case      :    int  			=BertGenerationDecoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''						)
            __snake_case      :    Dict  			=torch.tensor([[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_2_6, 3_8_9_9, 2_0_0_3, 1_0_1_4_0, 1_0_2]]						)
            with torch.no_grad():
                  __snake_case      :    List[str]  			=model(a						)[0]
            __snake_case      :    str  			=torch.Size([1, 8, 5_0_3_5_8]						)
            self.assertEqual(output.shape							,   a						)
            __snake_case      :    Optional[int]  			=torch.tensor(
                [[[-0.5_7_8_8, -2.5_9_9_4, -3.7_0_5_4], [0.0_4_3_8, 4.7_9_9_7, 1.8_7_9_5], [1.5_8_6_2, 6.6_4_0_9, 4.4_6_3_8]]]						)
            self.assertTrue(torch.allclose(output[:, :3, :3]							,   a							,   atol=1e-4						)						)


