






"""simple docstring"""


import unittest

from parameterized import parameterized

from transformers import OpenLlamaConfig, is_torch_available, set_seed
from transformers.testing_utils import require_torch, torch_device

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


if is_torch_available():
   import torch

   from transformers import OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel






class 						snake_case  :





    def __init__(							self     : Any    ,		A     : str    ,		A     : Tuple=1_3    ,		A     : List[str]=7    ,		A     : Dict=True    ,		A     : List[str]=True    ,		A     : Tuple=False    ,		A     : str=True    ,		A     : Optional[Any]=9_9    ,		A     : Optional[int]=3_2    ,		A     : Dict=5    ,		A     : Union[str, Any]=4    ,		A     : Union[str, Any]=3_7    ,		A     : List[str]="gelu"    ,		A     : Tuple=0.1    ,		A     : str=0.1    ,		A     : Optional[Any]=5_1_2    ,		A     : List[Any]=1_6    ,		A     : Union[str, Any]=2    ,		A     : List[str]=0.02    ,		A     : str=3    ,		A     : Any=4    ,		A     : Dict=None    ,		):


      '''simple docstring'''




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





    def     lowerCamelCase__				(							self     : Any      ):


      '''simple docstring'''




      a					:      Dict    =       ids_tensor([self.batch_size, self.seq_length]    ,		self.vocab_size      )

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

      a					:      Tuple    =       None
      if self.use_token_type_ids:
        a					:      Union[str, Any]    =       ids_tensor([self.batch_size, self.seq_length]    ,		self.type_vocab_size      )

      a					:      Dict    =       None
      a					:      Union[str, Any]    =       None
      a					:      Optional[Any]    =       None
      if self.use_labels:
        a					:      Any    =       ids_tensor([self.batch_size]    ,		self.type_sequence_label_size      )
        a					:      Optional[int]    =       ids_tensor([self.batch_size, self.seq_length]    ,		self.num_labels      )
        a					:      Any    =       ids_tensor([self.batch_size]    ,		self.num_choices      )

      a					:      int    =       self.get_config()

      return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels





    def     lowerCamelCase__				(							self     : int      ):


      '''simple docstring'''




      return OpenLlamaConfig(
          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    ,		type_vocab_size=self.type_vocab_size    ,		is_decoder=_A    ,		initializer_range=self.initializer_range    ,		use_stable_embedding=_A    ,		)





    def     lowerCamelCase__				(							self     : Optional[Any]    ,		A     : List[str]    ,		A     : Optional[int]    ,		A     : Dict    ,		A     : Optional[int]    ,		A     : Union[str, Any]    ,		A     : int    ,		A     : Dict      ):


      '''simple docstring'''




      a					:      int    =       OpenLlamaModel(config=_A      )
      model.to(_A      )
      model.eval()
      a					:      List[Any]    =       model(_A    ,		attention_mask=_A      )
      a					:      int    =       model(_A      )
      self.parent.assertEqual(result.last_hidden_state.shape    ,		(self.batch_size, self.seq_length, self.hidden_size)      )





    def     lowerCamelCase__				(							self     : int    ,		A     : str    ,		A     : int    ,		A     : Optional[int]    ,		A     : Optional[Any]    ,		A     : int    ,		A     : int    ,		A     : str    ,		A     : Optional[int]    ,		A     : Any    ,		):


      '''simple docstring'''




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





    def     lowerCamelCase__				(							self     : str    ,		A     : Tuple    ,		A     : Dict    ,		A     : Optional[int]    ,		A     : Tuple    ,		A     : Optional[Any]    ,		A     : Union[str, Any]    ,		A     : Tuple    ,		A     : str    ,		A     : List[str]    ,		):


      '''simple docstring'''




      a					:      Dict    =       OpenLlamaForCausalLM(config=_A      )
      model.to(_A      )
      model.eval()
      a					:      int    =       model(_A    ,		attention_mask=_A    ,		labels=_A      )
      self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.seq_length, self.vocab_size)      )





    def     lowerCamelCase__				(							self     : int    ,		A     : str    ,		A     : Dict    ,		A     : str    ,		A     : Optional[Any]    ,		A     : str    ,		A     : Tuple    ,		A     : str    ,		A     : List[str]    ,		A     : Optional[Any]    ,		):


      '''simple docstring'''




      a					:      str    =       True
      a					:      Tuple    =       True
      a					:      List[str]    =       OpenLlamaForCausalLM(config=_A      )
      model.to(_A      )
      model.eval()

      # first forward pass
      a					:      Tuple    =       model(
          _A    ,		attention_mask=_A    ,		encoder_hidden_states=_A    ,		encoder_attention_mask=_A    ,		use_cache=_A    ,		)
      a					:      Dict    =       outputs.past_key_values

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

      # append to next input_ids and
      a					:      Any    =       torch.cat([input_ids, next_tokens]    ,		dim=-1      )
      a					:      Optional[int]    =       torch.cat([input_mask, next_mask]    ,		dim=-1      )

      a					:      Any    =       model(
          _A    ,		attention_mask=_A    ,		encoder_hidden_states=_A    ,		encoder_attention_mask=_A    ,		output_hidden_states=_A    ,		)['hidden_states'][0]
      a					:      Optional[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
      a					:      str    =       ids_tensor((1,)    ,		output_from_past.shape[-1]      ).item()
      a					:      str    =       output_from_no_past[:, -3:, random_slice_idx].detach()
      a					:      Optional[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     lowerCamelCase__				(							self     : Union[str, Any]      ):


      '''simple docstring'''




      a					:      int    =       self.prepare_config_and_inputs()
      (
          a
      )					:      int    =       config_and_inputs
      a					:      Optional[Any]    =       {'input_ids': input_ids, 'attention_mask': input_mask}
      return config, inputs_dict






@require_torch
class 						snake_case  (  snake_case_       ,			snake_case_       ,			snake_case_       ,			unittest.TestCase	):
    __magic_name__   					=					(
        (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
    )
    __magic_name__   					=					(OpenLlamaForCausalLM,) if is_torch_available() else ()
    __magic_name__   					=					(
        {
            "feature-extraction": OpenLlamaModel,
            "text-classification": OpenLlamaForSequenceClassification,
            "text-generation": OpenLlamaForCausalLM,
            "zero-shot": OpenLlamaForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    __magic_name__   					=					False
    __magic_name__   					=					False





    def     lowerCamelCase__				(							self     : Optional[int]      ):


      '''simple docstring'''




      a					:      Tuple    =       OpenLlamaModelTester(self      )
      a					:      str    =       ConfigTester(self    ,		config_class=_A    ,		hidden_size=3_7      )





    def     lowerCamelCase__				(							self     : Tuple      ):


      '''simple docstring'''




      self.config_tester.run_common_tests()





    def     lowerCamelCase__				(							self     : Optional[Any]      ):


      '''simple docstring'''




      a					:      int    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*_A      )





    def     lowerCamelCase__				(							self     : Any      ):


      '''simple docstring'''




      a					:      Optional[int]    =       self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
        a					:      Any    =       type
        self.model_tester.create_and_check_model(*_A      )





    def     lowerCamelCase__				(							self     : Optional[Any]      ):


      '''simple docstring'''




      a					:      Tuple    =       self.model_tester.prepare_config_and_inputs_for_common()
      a					:      List[Any]    =       3
      a					:      List[str]    =       input_dict['input_ids']
      a					:      List[str]    =       input_ids.ne(1      ).to(_A      )
      a					:      Optional[Any]    =       ids_tensor([self.model_tester.batch_size]    ,		self.model_tester.type_sequence_label_size      )
      a					:      str    =       OpenLlamaForSequenceClassification(_A      )
      model.to(_A      )
      model.eval()
      a					:      Optional[Any]    =       model(_A    ,		attention_mask=_A    ,		labels=_A      )
      self.assertEqual(result.logits.shape    ,		(self.model_tester.batch_size, self.model_tester.num_labels)      )





    def     lowerCamelCase__				(							self     : Any      ):


      '''simple docstring'''




      a					:      str    =       self.model_tester.prepare_config_and_inputs_for_common()
      a					:      List[str]    =       3
      a					:      List[Any]    =       'single_label_classification'
      a					:      int    =       input_dict['input_ids']
      a					:      str    =       input_ids.ne(1      ).to(_A      )
      a					:      Tuple    =       ids_tensor([self.model_tester.batch_size]    ,		self.model_tester.type_sequence_label_size      )
      a					:      str    =       OpenLlamaForSequenceClassification(_A      )
      model.to(_A      )
      model.eval()
      a					:      Union[str, Any]    =       model(_A    ,		attention_mask=_A    ,		labels=_A      )
      self.assertEqual(result.logits.shape    ,		(self.model_tester.batch_size, self.model_tester.num_labels)      )





    def     lowerCamelCase__				(							self     : Optional[int]      ):


      '''simple docstring'''




      a					:      List[str]    =       self.model_tester.prepare_config_and_inputs_for_common()
      a					:      Optional[int]    =       3
      a					:      List[Any]    =       'multi_label_classification'
      a					:      Union[str, Any]    =       input_dict['input_ids']
      a					:      List[str]    =       input_ids.ne(1      ).to(_A      )
      a					:      str    =       ids_tensor(
          [self.model_tester.batch_size, config.num_labels]    ,		self.model_tester.type_sequence_label_size      ).to(torch.float      )
      a					:      Union[str, Any]    =       OpenLlamaForSequenceClassification(_A      )
      model.to(_A      )
      model.eval()
      a					:      Union[str, Any]    =       model(_A    ,		attention_mask=_A    ,		labels=_A      )
      self.assertEqual(result.logits.shape    ,		(self.model_tester.batch_size, self.model_tester.num_labels)      )





    @unittest.skip('Open-Llama buffers include complex numbers, which breaks this test'      )
    def     lowerCamelCase__				(							self     : List[str]      ):


      '''simple docstring'''




      pass





    @parameterized.expand([('linear',), ('dynamic',)]      )
    def     lowerCamelCase__				(							self     : Optional[Any]    ,		A     : Tuple      ):


      '''simple docstring'''




      a					:      Dict    =       self.model_tester.prepare_config_and_inputs_for_common()
      a					:      str    =       ids_tensor([1, 1_0]    ,		config.vocab_size      )
      a					:      Optional[Any]    =       ids_tensor([1, int(config.max_position_embeddings * 1.5      )]    ,		config.vocab_size      )

      set_seed(4_2      )  # Fixed seed at init time so the two models get the same random weights
      a					:      List[Any]    =       OpenLlamaModel(_A      )
      original_model.to(_A      )
      original_model.eval()
      a					:      str    =       original_model(_A      ).last_hidden_state
      a					:      List[Any]    =       original_model(_A      ).last_hidden_state

      set_seed(4_2      )  # Fixed seed at init time so the two models get the same random weights
      a					:      Optional[Any]    =       {'type': scaling_type, 'factor': 1_0.0}
      a					:      Dict    =       OpenLlamaModel(_A      )
      scaled_model.to(_A      )
      scaled_model.eval()
      a					:      Dict    =       scaled_model(_A      ).last_hidden_state
      a					:      str    =       scaled_model(_A      ).last_hidden_state

      # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original
      # maximum sequence length, so the outputs for the short input should match.
      if scaling_type == "dynamic":
        self.assertTrue(torch.allclose(_A    ,		_A    ,		atol=1E-5      )      )
      else:
        self.assertFalse(torch.allclose(_A    ,		_A    ,		atol=1E-5      )      )

      # The output should be different for long inputs
      self.assertFalse(torch.allclose(_A    ,		_A    ,		atol=1E-5      )      )












"""simple docstring"""


import os
import unittest

from transformers.models.bartpho.tokenization_bartpho import VOCAB_FILES_NAMES, BartphoTokenizer
from transformers.testing_utils import get_tests_dir

from ...test_tokenization_common import TokenizerTesterMixin


_UpperCamelCase    :   int   			=						get_tests_dir('fixtures/test_sentencepiece_bpe.model')






class 						snake_case  (  UpperCAmelCase       ,			unittest.TestCase	):
    __magic_name__   					=					BartphoTokenizer
    __magic_name__   					=					False
    __magic_name__   					=					True





    def     lowerCamelCase__				(							self     : List[Any]      ):


      '''simple docstring'''




      super().setUp()

      a					:      Any    =       ['▁This', '▁is', '▁a', '▁t', 'est']
      a					:      List[Any]    =       dict(zip(A    ,		range(len(A      )      )      )      )
      a					:      int    =       {'unk_token': '<unk>'}

      a					:      Tuple    =       os.path.join(self.tmpdirname    ,		VOCAB_FILES_NAMES['monolingual_vocab_file']      )
      with open(self.monolingual_vocab_file    ,		'w'    ,		encoding='utf-8'      ) as fp:
        for token in vocab_tokens:
          fp.write(F'''{token} {vocab_tokens[token]}\n'''      )

      a					:      Optional[int]    =       BartphoTokenizer(A    ,		self.monolingual_vocab_file    ,		**self.special_tokens_map      )
      tokenizer.save_pretrained(self.tmpdirname      )





    def     lowerCamelCase__				(							self     : Dict    ,		**A     : str      ):


      '''simple docstring'''




      kwargs.update(self.special_tokens_map      )
      return BartphoTokenizer.from_pretrained(self.tmpdirname    ,		**A      )





    def     lowerCamelCase__				(							self     : Optional[int]    ,		A     : Dict      ):


      '''simple docstring'''




      a					:      Tuple    =       'This is a là test'
      a					:      List[Any]    =       'This is a<unk><unk> test'
      return input_text, output_text





    def     lowerCamelCase__				(							self     : Optional[Any]      ):


      '''simple docstring'''




      a					:      Tuple    =       BartphoTokenizer(A    ,		self.monolingual_vocab_file    ,		**self.special_tokens_map      )
      a					:      int    =       'This is a là test'
      a					:      int    =       '▁This ▁is ▁a ▁l à ▁t est'.split()
      a					:      str    =       tokenizer.tokenize(A      )
      self.assertListEqual(A    ,		A      )

      a					:      Union[str, Any]    =       tokens + [tokenizer.unk_token]
      a					:      Dict    =       [4, 5, 6, 3, 3, 7, 8, 3]
      self.assertListEqual(tokenizer.convert_tokens_to_ids(A      )    ,		A      )




