


"""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     __lowerCamelCase  :

     '''simple docstring'''


     def __init__(      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase=13    ,				__UpperCAmelCase=7    ,				__UpperCAmelCase=True    ,				__UpperCAmelCase=True    ,				__UpperCAmelCase=99    ,				__UpperCAmelCase=32    ,				__UpperCAmelCase=5    ,				__UpperCAmelCase=4    ,				__UpperCAmelCase=37    ,				__UpperCAmelCase="gelu"    ,				__UpperCAmelCase=0.1    ,				__UpperCAmelCase=0.1    ,				__UpperCAmelCase=50    ,				__UpperCAmelCase=0.02    ,				__UpperCAmelCase=True    ,				__UpperCAmelCase=None    ,				)       ->							List[Any]:
          _a    	=				parent
          _a    	=				batch_size
          _a    	=				seq_length
          _a    	=				is_training
          _a    	=				use_input_mask
          _a    	=				vocab_size
          _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    	=				max_position_embeddings
          _a    	=				initializer_range
          _a    	=				use_labels
          _a    	=				scope


     def 							_UpperCAmelCase      (      self							)       ->							str:
          _a    	=				ids_tensor([self.batch_size, self.seq_length]    ,				self.vocab_size							)

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

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

          _a    	=				self.get_config()

          return config, input_ids, input_mask, token_labels


     def 							_UpperCAmelCase      (      self							)       ->							Dict:
          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=__UpperCAmelCase    ,				initializer_range=self.initializer_range    ,				)


     def 							_UpperCAmelCase      (      self							)       ->							int:
          (
              (
              _a
          )	,      (
              _a
          )	,      (
              _a
          )	,      (
              _a
          )	,      
          )    	=				self.prepare_config_and_inputs()

          _a    	=				True
          _a    	=				floats_tensor([self.batch_size, self.seq_length, self.hidden_size]							)
          _a    	=				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    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				**__UpperCAmelCase    ,				)       ->							Union[str, Any]:
          _a    	=				BertGenerationEncoder(config=__UpperCAmelCase							)
          model.to(__UpperCAmelCase							)
          model.eval()
          _a    	=				model(__UpperCAmelCase    ,				attention_mask=__UpperCAmelCase							)
          _a    	=				model(__UpperCAmelCase							)
          self.parent.assertEqual(result.last_hidden_state.shape    ,				(self.batch_size, self.seq_length, self.hidden_size)							)


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				**__UpperCAmelCase    ,				)       ->							List[str]:
          _a    	=				True
          _a    	=				BertGenerationEncoder(config=__UpperCAmelCase							)
          model.to(__UpperCAmelCase							)
          model.eval()
          _a    	=				model(
              __UpperCAmelCase    ,				attention_mask=__UpperCAmelCase    ,				encoder_hidden_states=__UpperCAmelCase    ,				encoder_attention_mask=__UpperCAmelCase    ,				)
          _a    	=				model(
              __UpperCAmelCase    ,				attention_mask=__UpperCAmelCase    ,				encoder_hidden_states=__UpperCAmelCase    ,				)
          self.parent.assertEqual(result.last_hidden_state.shape    ,				(self.batch_size, self.seq_length, self.hidden_size)							)


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				**__UpperCAmelCase    ,				)       ->							Union[str, Any]:
          _a    	=				True
          _a    	=				True
          _a    	=				BertGenerationDecoder(config=__UpperCAmelCase							).to(__UpperCAmelCase							).eval()

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

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

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

          _a    	=				model(
              __UpperCAmelCase    ,				attention_mask=__UpperCAmelCase    ,				encoder_hidden_states=__UpperCAmelCase    ,				encoder_attention_mask=__UpperCAmelCase    ,				output_hidden_states=__UpperCAmelCase    ,				)['''hidden_states'''][0]
          _a    	=				model(
              __UpperCAmelCase    ,				attention_mask=__UpperCAmelCase    ,				encoder_hidden_states=__UpperCAmelCase    ,				encoder_attention_mask=__UpperCAmelCase    ,				past_key_values=__UpperCAmelCase    ,				output_hidden_states=__UpperCAmelCase    ,				)['''hidden_states'''][0]

          # select random slice
          _a    	=				ids_tensor((1,)    ,				output_from_past.shape[-1]							).item()
          _a    	=				output_from_no_past[:, -3:, random_slice_idx].detach()
          _a    	=				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(__UpperCAmelCase    ,				__UpperCAmelCase    ,				atol=1e-3							)							)


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				*__UpperCAmelCase    ,				)       ->							Any:
          _a    	=				BertGenerationDecoder(__UpperCAmelCase							)
          model.to(__UpperCAmelCase							)
          model.eval()
          _a    	=				model(__UpperCAmelCase    ,				attention_mask=__UpperCAmelCase    ,				labels=__UpperCAmelCase							)
          self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.seq_length, self.vocab_size)							)





     def 							_UpperCAmelCase      (      self							)       ->							List[Any]:
          _a	,      _a	,      _a	,      _a    	=				self.prepare_config_and_inputs()
          _a    	=				{'''input_ids''': input_ids, '''attention_mask''': input_mask}
          return config, inputs_dict







@require_torch
class     __lowerCamelCase  (					a__  ,					a__  ,					a__  ,					unittest.TestCase				):

     '''simple docstring'''
     A_       :	Union[str, Any]    =		(BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
     A_       :	Any    =		(BertGenerationDecoder,) if is_torch_available() else ()
     A_       :	Tuple    =		(
         {'feature-extraction': BertGenerationEncoder, 'text-generation': BertGenerationDecoder}
         if is_torch_available()
         else {}
     )


     def 							_UpperCAmelCase      (      self							)       ->							Union[str, Any]:
          _a    	=				BertGenerationEncoderTester(self							)
          _a    	=				ConfigTester(self    ,				config_class=__UpperCAmelCase    ,				hidden_size=37							)


     def 							_UpperCAmelCase      (      self							)       ->							List[Any]:
          self.config_tester.run_common_tests()


     def 							_UpperCAmelCase      (      self							)       ->							str:
          _a    	=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__UpperCAmelCase							)


     def 							_UpperCAmelCase      (      self							)       ->							List[Any]:
          _a	,      _a	,      _a	,      _a    	=				self.model_tester.prepare_config_and_inputs()
          _a    	=				'''bert'''
          self.model_tester.create_and_check_model(__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase							)


     def 							_UpperCAmelCase      (      self							)       ->							int:
          _a    	=				self.model_tester.prepare_config_and_inputs_for_decoder()
          self.model_tester.create_and_check_model_as_decoder(*__UpperCAmelCase							)


     def 							_UpperCAmelCase      (      self							)       ->							Optional[int]:
          _a    	=				self.model_tester.prepare_config_and_inputs_for_decoder()
          self.model_tester.create_and_check_decoder_model_past_large_inputs(*__UpperCAmelCase							)


     def 							_UpperCAmelCase      (      self							)       ->							Union[str, Any]:
          # This regression test was failing with PyTorch < 1.3
          (
              (
              _a
          )	,      (
              _a
          )	,      (
              _a
          )	,      (
              _a
          )	,      (
              _a
          )	,      (
              _a
          )	,      
          )    	=				self.model_tester.prepare_config_and_inputs_for_decoder()

          _a    	=				None

          self.model_tester.create_and_check_model_as_decoder(
              __UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				)


     def 							_UpperCAmelCase      (      self							)       ->							Optional[int]:
          _a    	=				self.model_tester.prepare_config_and_inputs_for_decoder()
          self.model_tester.create_and_check_for_causal_lm(*__UpperCAmelCase							)





     @slow
     def 							_UpperCAmelCase      (      self							)       ->							Dict:
          _a    	=				BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''							)
          self.assertIsNotNone(__UpperCAmelCase							)







@require_torch
class     __lowerCamelCase  (					unittest.TestCase				):

     '''simple docstring'''


     @slow
     def 							_UpperCAmelCase      (      self							)       ->							str:
          _a    	=				BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''							)
          _a    	=				torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 10140, 102]]							)
          with torch.no_grad():
               _a    	=				model(__UpperCAmelCase							)[0]
          _a    	=				torch.Size([1, 8, 1024]							)
          self.assertEqual(output.shape    ,				__UpperCAmelCase							)
          _a    	=				torch.tensor(
              [[[0.1775, 0.0083, -0.0321], [1.6002, 0.1287, 0.3912], [2.1473, 0.5791, 0.6066]]]							)
          self.assertTrue(torch.allclose(output[:, :3, :3]    ,				__UpperCAmelCase    ,				atol=1e-4							)							)







@require_torch
class     __lowerCamelCase  (					unittest.TestCase				):

     '''simple docstring'''


     @slow
     def 							_UpperCAmelCase      (      self							)       ->							Union[str, Any]:
          _a    	=				BertGenerationDecoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''							)
          _a    	=				torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 10140, 102]]							)
          with torch.no_grad():
               _a    	=				model(__UpperCAmelCase							)[0]
          _a    	=				torch.Size([1, 8, 50358]							)
          self.assertEqual(output.shape    ,				__UpperCAmelCase							)
          _a    	=				torch.tensor(
              [[[-0.5788, -2.5994, -3.7054], [0.0438, 4.7997, 1.8795], [1.5862, 6.6409, 4.4638]]]							)
          self.assertTrue(torch.allclose(output[:, :3, :3]    ,				__UpperCAmelCase    ,				atol=1e-4							)							)



"""simple docstring"""






import warnings
from typing import Dict, List, Optional, Tuple

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


__snake_case		=       logging.get_logger(__name__)


class     __lowerCamelCase  (					a__				):

     '''simple docstring'''
     A_       :	Any    =		['input_ids', 'attention_mask']


     def __init__(      self    ,				__UpperCAmelCase="</s>"    ,				__UpperCAmelCase="<unk>"    ,				__UpperCAmelCase="<pad>"    ,				__UpperCAmelCase=125    ,				__UpperCAmelCase=None    ,				**__UpperCAmelCase    ,				)       ->							None:
          # Add extra_ids to the special token list
          if extra_ids > 0 and additional_special_tokens is None:
               _a    	=				[F'<extra_id_{i}>' for i in range(__UpperCAmelCase							)]
          elif extra_ids > 0 and additional_special_tokens is not None:
               # Check that we have the right number of extra_id special tokens
               _a    	=				len(set(filter(lambda __UpperCAmelCase							: bool('''extra_id''' in str(__UpperCAmelCase							)							)    ,				__UpperCAmelCase							)							)							)
               if extra_tokens != extra_ids:
                    raise ValueError(
                        F'Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are'
                        ''' provided to ByT5Tokenizer. In this case the additional_special_tokens must include the'''
                        ''' extra_ids tokens'''							)

          _a    	=				AddedToken(__UpperCAmelCase    ,				lstrip=__UpperCAmelCase    ,				rstrip=__UpperCAmelCase							) if isinstance(__UpperCAmelCase    ,				__UpperCAmelCase							) else pad_token
          _a    	=				AddedToken(__UpperCAmelCase    ,				lstrip=__UpperCAmelCase    ,				rstrip=__UpperCAmelCase							) if isinstance(__UpperCAmelCase    ,				__UpperCAmelCase							) else eos_token
          _a    	=				AddedToken(__UpperCAmelCase    ,				lstrip=__UpperCAmelCase    ,				rstrip=__UpperCAmelCase							) if isinstance(__UpperCAmelCase    ,				__UpperCAmelCase							) else unk_token

          super().__init__(
              eos_token=__UpperCAmelCase    ,				unk_token=__UpperCAmelCase    ,				pad_token=__UpperCAmelCase    ,				extra_ids=__UpperCAmelCase    ,				additional_special_tokens=__UpperCAmelCase    ,				**__UpperCAmelCase    ,				)

          _a    	=				extra_ids

          _a    	=				2**8  # utf is 8 bits

          # define special tokens dict
          _a    	=				{
              self.pad_token: 0,
              self.eos_token: 1,
              self.unk_token: 2,
          }
          _a    	=				len(self.special_tokens_encoder							)
          _a    	=				len(__UpperCAmelCase							)
          for i, token in enumerate(__UpperCAmelCase							):
               _a    	=				self.vocab_size + i - n
          _a    	=				{v: k for k, v in self.special_tokens_encoder.items()}


     @property
     def 							_UpperCAmelCase      (      self							)       ->							Dict:
          return self._utf_vocab_size + self._num_special_tokens + self._extra_ids


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase = None    ,				__UpperCAmelCase = False							)       ->							List[int]:
          if already_has_special_tokens:
               return super().get_special_tokens_mask(
                   token_ids_a=__UpperCAmelCase    ,				token_ids_a=__UpperCAmelCase    ,				already_has_special_tokens=__UpperCAmelCase							)

          # normal case: some special tokens
          if token_ids_a is None:
               return ([0] * len(__UpperCAmelCase							)) + [1]
          return ([0] * len(__UpperCAmelCase							)) + [1] + ([0] * len(__UpperCAmelCase							)) + [1]


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase							)       ->							List[int]:
          if len(__UpperCAmelCase							) > 0 and token_ids[-1] == self.eos_token_id:
               warnings.warn(
                   F'This sequence already has {self.eos_token}. In future versions this behavior may lead to duplicated'
                   ''' eos tokens being added.'''							)
               return token_ids
          else:
               return token_ids + [self.eos_token_id]


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase = None							)       ->							List[int]:
          _a    	=				[self.eos_token_id]

          if token_ids_a is None:
               return len(token_ids_a + eos							) * [0]
          return len(token_ids_a + eos + token_ids_a + eos							) * [0]


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase = None							)       ->							List[int]:
          _a    	=				self._add_eos_if_not_present(__UpperCAmelCase							)
          if token_ids_a is None:
               return token_ids_a
          else:
               _a    	=				self._add_eos_if_not_present(__UpperCAmelCase							)
               return token_ids_a + token_ids_a


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase							)       ->							List[str]:
          _a    	=				[chr(__UpperCAmelCase							) for i in text.encode('''utf-8'''							)]
          return tokens


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase							)       ->							Union[str, Any]:
          if token in self.special_tokens_encoder:
               _a    	=				self.special_tokens_encoder[token]
          elif token in self.added_tokens_encoder:
               _a    	=				self.added_tokens_encoder[token]
          elif len(__UpperCAmelCase							) != 1:
               _a    	=				self.unk_token_id
          else:
               _a    	=				ord(__UpperCAmelCase							) + self._num_special_tokens
          return token_id


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase							)       ->							List[Any]:
          if index in self.special_tokens_decoder:
               _a    	=				self.special_tokens_decoder[index]
          else:
               _a    	=				chr(index - self._num_special_tokens							)
          return token


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase							)       ->							Any:
          _a    	=				B''''''
          for token in tokens:
               if token in self.special_tokens_decoder:
                    _a    	=				self.special_tokens_decoder[token].encode('''utf-8'''							)
               elif token in self.added_tokens_decoder:
                    _a    	=				self.special_tokens_decoder[token].encode('''utf-8'''							)
               elif token in self.special_tokens_encoder:
                    _a    	=				token.encode('''utf-8'''							)
               elif token in self.added_tokens_encoder:
                    _a    	=				token.encode('''utf-8'''							)
               else:
                    _a    	=				bytes([ord(__UpperCAmelCase							)]							)
               bstring += tok_string
          _a    	=				bstring.decode('''utf-8'''    ,				errors='''ignore'''							)
          return string





     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase = None							)       ->							Tuple[str]:
          return ()