


'''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       :      Dict       ,   SCREAMING_SNAKE_CASE_       :      Optional[Any]       ,   SCREAMING_SNAKE_CASE_       :      Union[str, Any]=13       ,   SCREAMING_SNAKE_CASE_       :      List[Any]=7       ,   SCREAMING_SNAKE_CASE_       :      str=True       ,   SCREAMING_SNAKE_CASE_       :      str=True       ,   SCREAMING_SNAKE_CASE_       :      Tuple=99       ,   SCREAMING_SNAKE_CASE_       :      Dict=32       ,   SCREAMING_SNAKE_CASE_       :      str=5       ,   SCREAMING_SNAKE_CASE_       :      Dict=4       ,   SCREAMING_SNAKE_CASE_       :      Union[str, Any]=37       ,   SCREAMING_SNAKE_CASE_       :      Any="gelu"       ,   SCREAMING_SNAKE_CASE_       :      Tuple=0.1       ,   SCREAMING_SNAKE_CASE_       :      Dict=0.1       ,   SCREAMING_SNAKE_CASE_       :      Optional[int]=50       ,   SCREAMING_SNAKE_CASE_       :      str=0.0_2       ,   SCREAMING_SNAKE_CASE_       :      Optional[Any]=True       ,   SCREAMING_SNAKE_CASE_       :      Optional[int]=None       ,   ) ->							Dict:
        __snake_case           =						parent
        __snake_case           =						batch_size
        __snake_case           =						seq_length
        __snake_case           =						is_training
        __snake_case           =						use_input_mask
        __snake_case           =						vocab_size
        __snake_case           =						hidden_size
        __snake_case           =						num_hidden_layers
        __snake_case           =						num_attention_heads
        __snake_case           =						intermediate_size
        __snake_case           =						hidden_act
        __snake_case           =						hidden_dropout_prob
        __snake_case           =						attention_probs_dropout_prob
        __snake_case           =						max_position_embeddings
        __snake_case           =						initializer_range
        __snake_case           =						use_labels
        __snake_case           =						scope
   def a  (						self       :      str      ) ->							Tuple:
        __snake_case           =						ids_tensor([self.batch_size, self.seq_length]       ,   self.vocab_size      )

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

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

        __snake_case           =						self.get_config()

        return config, input_ids, input_mask, token_labels
   def a  (						self       :      List[Any]      ) ->							Optional[int]:
        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=SCREAMING_SNAKE_CASE_       ,   initializer_range=self.initializer_range       ,   )
   def a  (						self       :      int      ) ->							Dict:
        (
            (
            __snake_case
        )      ,       (
            __snake_case
        )      ,       (
            __snake_case
        )      ,       (
            __snake_case
        )      ,       
        )           =						self.prepare_config_and_inputs()

        __snake_case           =						True
        __snake_case           =						floats_tensor([self.batch_size, self.seq_length, self.hidden_size]      )
        __snake_case           =						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 a  (						self       :      List[Any]       ,   SCREAMING_SNAKE_CASE_       :      Any       ,   SCREAMING_SNAKE_CASE_       :      str       ,   SCREAMING_SNAKE_CASE_       :      Tuple       ,   SCREAMING_SNAKE_CASE_       :      Optional[Any]       ,   **SCREAMING_SNAKE_CASE_       :      int       ,   ) ->							Optional[Any]:
        __snake_case           =						BertGenerationEncoder(config=SCREAMING_SNAKE_CASE_      )
        model.to(SCREAMING_SNAKE_CASE_      )
        model.eval()
        __snake_case           =						model(SCREAMING_SNAKE_CASE_       ,   attention_mask=SCREAMING_SNAKE_CASE_      )
        __snake_case           =						model(SCREAMING_SNAKE_CASE_      )
        self.parent.assertEqual(result.last_hidden_state.shape       ,   (self.batch_size, self.seq_length, self.hidden_size)      )
   def a  (						self       :      Union[str, Any]       ,   SCREAMING_SNAKE_CASE_       :      Tuple       ,   SCREAMING_SNAKE_CASE_       :      int       ,   SCREAMING_SNAKE_CASE_       :      Optional[Any]       ,   SCREAMING_SNAKE_CASE_       :      List[str]       ,   SCREAMING_SNAKE_CASE_       :      Tuple       ,   SCREAMING_SNAKE_CASE_       :      Optional[int]       ,   **SCREAMING_SNAKE_CASE_       :      Dict       ,   ) ->							List[Any]:
        __snake_case           =						True
        __snake_case           =						BertGenerationEncoder(config=SCREAMING_SNAKE_CASE_      )
        model.to(SCREAMING_SNAKE_CASE_      )
        model.eval()
        __snake_case           =						model(
            SCREAMING_SNAKE_CASE_       ,   attention_mask=SCREAMING_SNAKE_CASE_       ,   encoder_hidden_states=SCREAMING_SNAKE_CASE_       ,   encoder_attention_mask=SCREAMING_SNAKE_CASE_       ,   )
        __snake_case           =						model(
            SCREAMING_SNAKE_CASE_       ,   attention_mask=SCREAMING_SNAKE_CASE_       ,   encoder_hidden_states=SCREAMING_SNAKE_CASE_       ,   )
        self.parent.assertEqual(result.last_hidden_state.shape       ,   (self.batch_size, self.seq_length, self.hidden_size)      )
   def a  (						self       :      List[Any]       ,   SCREAMING_SNAKE_CASE_       :      Tuple       ,   SCREAMING_SNAKE_CASE_       :      List[str]       ,   SCREAMING_SNAKE_CASE_       :      int       ,   SCREAMING_SNAKE_CASE_       :      int       ,   SCREAMING_SNAKE_CASE_       :      Optional[Any]       ,   SCREAMING_SNAKE_CASE_       :      Optional[int]       ,   **SCREAMING_SNAKE_CASE_       :      Union[str, Any]       ,   ) ->							Union[str, Any]:
        __snake_case           =						True
        __snake_case           =						True
        __snake_case           =						BertGenerationDecoder(config=SCREAMING_SNAKE_CASE_      ).to(SCREAMING_SNAKE_CASE_      ).eval()

        # first forward pass
        __snake_case           =						model(
            SCREAMING_SNAKE_CASE_       ,   attention_mask=SCREAMING_SNAKE_CASE_       ,   encoder_hidden_states=SCREAMING_SNAKE_CASE_       ,   encoder_attention_mask=SCREAMING_SNAKE_CASE_       ,   use_cache=SCREAMING_SNAKE_CASE_       ,   )
        __snake_case           =						outputs.past_key_values

        # create hypothetical multiple next token and extent to next_input_ids
        __snake_case           =						ids_tensor((self.batch_size, 3)       ,   config.vocab_size      )
        __snake_case           =						ids_tensor((self.batch_size, 3)       ,   vocab_size=2      )

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

        __snake_case           =						model(
            SCREAMING_SNAKE_CASE_       ,   attention_mask=SCREAMING_SNAKE_CASE_       ,   encoder_hidden_states=SCREAMING_SNAKE_CASE_       ,   encoder_attention_mask=SCREAMING_SNAKE_CASE_       ,   output_hidden_states=SCREAMING_SNAKE_CASE_       ,   )['hidden_states'][0]
        __snake_case           =						model(
            SCREAMING_SNAKE_CASE_       ,   attention_mask=SCREAMING_SNAKE_CASE_       ,   encoder_hidden_states=SCREAMING_SNAKE_CASE_       ,   encoder_attention_mask=SCREAMING_SNAKE_CASE_       ,   past_key_values=SCREAMING_SNAKE_CASE_       ,   output_hidden_states=SCREAMING_SNAKE_CASE_       ,   )['hidden_states'][0]

        # select random slice
        __snake_case           =						ids_tensor((1,)       ,   output_from_past.shape[-1]      ).item()
        __snake_case           =						output_from_no_past[:, -3:, random_slice_idx].detach()
        __snake_case           =						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(SCREAMING_SNAKE_CASE_       ,   SCREAMING_SNAKE_CASE_       ,   atol=1e-3      )      )
   def a  (						self       :      int       ,   SCREAMING_SNAKE_CASE_       :      Dict       ,   SCREAMING_SNAKE_CASE_       :      str       ,   SCREAMING_SNAKE_CASE_       :      List[str]       ,   SCREAMING_SNAKE_CASE_       :      Any       ,   *SCREAMING_SNAKE_CASE_       :      List[Any]       ,   ) ->							Any:
        __snake_case           =						BertGenerationDecoder(SCREAMING_SNAKE_CASE_      )
        model.to(SCREAMING_SNAKE_CASE_      )
        model.eval()
        __snake_case           =						model(SCREAMING_SNAKE_CASE_       ,   attention_mask=SCREAMING_SNAKE_CASE_       ,   labels=SCREAMING_SNAKE_CASE_      )
        self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.seq_length, self.vocab_size)      )





   def a  (						self       :      str      ) ->							Union[str, Any]:
        __snake_case      ,       __snake_case      ,       __snake_case      ,       __snake_case           =						self.prepare_config_and_inputs()
        __snake_case           =						{'input_ids': input_ids, 'attention_mask': input_mask}
        return config, inputs_dict




@require_torch
class 			_lowercase (	__lowercase						,					__lowercase						,					__lowercase						,					unittest.TestCase							):
   _SCREAMING_SNAKE_CASE  :  Optional[int]          =	(BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
   _SCREAMING_SNAKE_CASE  :  Dict          =	(BertGenerationDecoder,) if is_torch_available() else ()
   _SCREAMING_SNAKE_CASE  :  str          =	(
       {"feature-extraction": BertGenerationEncoder, "text-generation": BertGenerationDecoder}
       if is_torch_available()
       else {}
   )
   def a  (						self       :      int      ) ->							Union[str, Any]:
        __snake_case           =						BertGenerationEncoderTester(self      )
        __snake_case           =						ConfigTester(self       ,   config_class=SCREAMING_SNAKE_CASE_       ,   hidden_size=37      )
   def a  (						self       :      Any      ) ->							Optional[int]:
        self.config_tester.run_common_tests()
   def a  (						self       :      List[Any]      ) ->							int:
        __snake_case           =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_      )
   def a  (						self       :      Dict      ) ->							Optional[Any]:
        __snake_case      ,       __snake_case      ,       __snake_case      ,       __snake_case           =						self.model_tester.prepare_config_and_inputs()
        __snake_case           =						'bert'
        self.model_tester.create_and_check_model(SCREAMING_SNAKE_CASE_       ,   SCREAMING_SNAKE_CASE_       ,   SCREAMING_SNAKE_CASE_       ,   SCREAMING_SNAKE_CASE_      )
   def a  (						self       :      Union[str, Any]      ) ->							Union[str, Any]:
        __snake_case           =						self.model_tester.prepare_config_and_inputs_for_decoder()
        self.model_tester.create_and_check_model_as_decoder(*SCREAMING_SNAKE_CASE_      )
   def a  (						self       :      Dict      ) ->							Optional[Any]:
        __snake_case           =						self.model_tester.prepare_config_and_inputs_for_decoder()
        self.model_tester.create_and_check_decoder_model_past_large_inputs(*SCREAMING_SNAKE_CASE_      )
   def a  (						self       :      int      ) ->							Union[str, Any]:
        # This regression test was failing with PyTorch < 1.3
        (
            (
            __snake_case
        )      ,       (
            __snake_case
        )      ,       (
            __snake_case
        )      ,       (
            __snake_case
        )      ,       (
            __snake_case
        )      ,       (
            __snake_case
        )      ,       
        )           =						self.model_tester.prepare_config_and_inputs_for_decoder()

        __snake_case           =						None

        self.model_tester.create_and_check_model_as_decoder(
            SCREAMING_SNAKE_CASE_       ,   SCREAMING_SNAKE_CASE_       ,   SCREAMING_SNAKE_CASE_       ,   SCREAMING_SNAKE_CASE_       ,   SCREAMING_SNAKE_CASE_       ,   SCREAMING_SNAKE_CASE_       ,   )
   def a  (						self       :      Optional[int]      ) ->							Optional[Any]:
        __snake_case           =						self.model_tester.prepare_config_and_inputs_for_decoder()
        self.model_tester.create_and_check_for_causal_lm(*SCREAMING_SNAKE_CASE_      )





   @slow
   def a  (						self       :      Optional[Any]      ) ->							Any:
        __snake_case           =						BertGenerationEncoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder'      )
        self.assertIsNotNone(SCREAMING_SNAKE_CASE_      )




@require_torch
class 			_lowercase (	unittest.TestCase							):
   @slow
   def a  (						self       :      Optional[int]      ) ->							Optional[Any]:
        __snake_case           =						BertGenerationEncoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder'      )
        __snake_case           =						torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 1_0140, 102]]      )
        with torch.no_grad():
             __snake_case           =						model(SCREAMING_SNAKE_CASE_      )[0]
        __snake_case           =						torch.Size([1, 8, 1024]      )
        self.assertEqual(output.shape       ,   SCREAMING_SNAKE_CASE_      )
        __snake_case           =						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]       ,   SCREAMING_SNAKE_CASE_       ,   atol=1e-4      )      )




@require_torch
class 			_lowercase (	unittest.TestCase							):
   @slow
   def a  (						self       :      List[Any]      ) ->							Any:
        __snake_case           =						BertGenerationDecoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder'      )
        __snake_case           =						torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 1_0140, 102]]      )
        with torch.no_grad():
             __snake_case           =						model(SCREAMING_SNAKE_CASE_      )[0]
        __snake_case           =						torch.Size([1, 8, 5_0358]      )
        self.assertEqual(output.shape       ,   SCREAMING_SNAKE_CASE_      )
        __snake_case           =						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]       ,   SCREAMING_SNAKE_CASE_       ,   atol=1e-4      )      )











"""simple docstring"""





from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow


if is_tf_available():
							import numpy as np
							import tensorflow as tf

							from transformers import TFXLMRobertaModel
@require_tf
@require_sentencepiece
@require_tokenizers
class 		SCREAMING_SNAKE_CASE_				(     unittest.TestCase ):
				"""simple docstring"""




				@slow
				def        __lowercase	(  self	:Union[str, Any]      ):
											__lowerCamelCase   :   List[Any]       =TFXLMRobertaModel.from_pretrained('''jplu/tf-xlm-roberta-base'''      )

											__lowerCamelCase   :   Dict       ={
											    '''input_ids''': tf.convert_to_tensor([[0, 2646, 1_0269, 83, 9_9942, 2]]    ,     dtype=tf.intaa      ),  # "My dog is cute"
											    '''attention_mask''': tf.convert_to_tensor([[1, 1, 1, 1, 1, 1]]    ,     dtype=tf.intaa      ),
											}

											__lowerCamelCase   :   Optional[int]       =model(__lowercase      )['''last_hidden_state''']
											__lowerCamelCase   :   str       =tf.TensorShape((1, 6, 768)      )
											self.assertEqual(output.shape    ,     __lowercase      )
											# compare the actual values for a slice.
											__lowerCamelCase   :   str       =tf.convert_to_tensor(
											    [
											        [
											            [0.0681762, 0.10894451, 0.06772504],
											            [-0.06423668, 0.02366615, 0.04329344],
											            [-0.06057295, 0.09974135, -0.00070584],
											        ]
											    ]    ,     dtype=tf.floataa    ,     )

											self.assertTrue(np.allclose(output[:, :3, :3].numpy()    ,     expected_slice.numpy()    ,     atol=1e-4      )      )




