



'''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     ,UpperCAmelCase_     ,UpperCAmelCase_=13     ,UpperCAmelCase_=7     ,UpperCAmelCase_=True     ,UpperCAmelCase_=True     ,UpperCAmelCase_=False     ,UpperCAmelCase_=True     ,UpperCAmelCase_=99     ,UpperCAmelCase_=32     ,UpperCAmelCase_=5     ,UpperCAmelCase_=4     ,UpperCAmelCase_=37     ,UpperCAmelCase_="gelu"     ,UpperCAmelCase_=0.1     ,UpperCAmelCase_=0.1     ,UpperCAmelCase_=512     ,UpperCAmelCase_=16     ,UpperCAmelCase_=2     ,UpperCAmelCase_=0.02     ,UpperCAmelCase_=3     ,UpperCAmelCase_=4     ,UpperCAmelCase_=None     ,)  ->							List[Any]:
           lowercase__		       =						parent
           lowercase__		       =						batch_size
           lowercase__		       =						seq_length
           lowercase__		       =						is_training
           lowercase__		       =						use_input_mask
           lowercase__		       =						use_token_type_ids
           lowercase__		       =						use_labels
           lowercase__		       =						vocab_size
           lowercase__		       =						hidden_size
           lowercase__		       =						num_hidden_layers
           lowercase__		       =						num_attention_heads
           lowercase__		       =						intermediate_size
           lowercase__		       =						hidden_act
           lowercase__		       =						hidden_dropout_prob
           lowercase__		       =						attention_probs_dropout_prob
           lowercase__		       =						max_position_embeddings
           lowercase__		       =						type_vocab_size
           lowercase__		       =						type_sequence_label_size
           lowercase__		       =						initializer_range
           lowercase__		       =						num_labels
           lowercase__		       =						num_choices
           lowercase__		       =						scope



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

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

           lowercase__		       =						None
           if self.use_token_type_ids:
               lowercase__		       =						ids_tensor([self.batch_size, self.seq_length]     ,self.type_vocab_size      )

           lowercase__		       =						None
           lowercase__		       =						None
           lowercase__		       =						None
           if self.use_labels:
               lowercase__		       =						ids_tensor([self.batch_size]     ,self.type_sequence_label_size      )
               lowercase__		       =						ids_tensor([self.batch_size, self.seq_length]     ,self.num_labels      )
               lowercase__		       =						ids_tensor([self.batch_size]     ,self.num_choices      )

           lowercase__		       =						self.get_config()

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



       def 							_a      (				self      )  ->							Optional[int]:
           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=UpperCAmelCase_     ,initializer_range=self.initializer_range     ,use_stable_embedding=UpperCAmelCase_     ,)



       def 							_a      (				self     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_      )  ->							str:
           lowercase__		       =						OpenLlamaModel(config=UpperCAmelCase_      )
           model.to(UpperCAmelCase_      )
           model.eval()
           lowercase__		       =						model(UpperCAmelCase_     ,attention_mask=UpperCAmelCase_      )
           lowercase__		       =						model(UpperCAmelCase_      )
           self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, self.seq_length, self.hidden_size)      )



       def 							_a      (				self     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,)  ->							Optional[int]:
           lowercase__		       =						True
           lowercase__		       =						OpenLlamaModel(UpperCAmelCase_      )
           model.to(UpperCAmelCase_      )
           model.eval()
           lowercase__		       =						model(
               UpperCAmelCase_     ,attention_mask=UpperCAmelCase_     ,encoder_hidden_states=UpperCAmelCase_     ,encoder_attention_mask=UpperCAmelCase_     ,)
           lowercase__		       =						model(
               UpperCAmelCase_     ,attention_mask=UpperCAmelCase_     ,encoder_hidden_states=UpperCAmelCase_     ,)
           lowercase__		       =						model(UpperCAmelCase_     ,attention_mask=UpperCAmelCase_      )
           self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, self.seq_length, self.hidden_size)      )



       def 							_a      (				self     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,)  ->							List[Any]:
           lowercase__		       =						OpenLlamaForCausalLM(config=UpperCAmelCase_      )
           model.to(UpperCAmelCase_      )
           model.eval()
           lowercase__		       =						model(UpperCAmelCase_     ,attention_mask=UpperCAmelCase_     ,labels=UpperCAmelCase_      )
           self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.seq_length, self.vocab_size)      )



       def 							_a      (				self     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,UpperCAmelCase_     ,)  ->							Optional[int]:
           lowercase__		       =						True
           lowercase__		       =						True
           lowercase__		       =						OpenLlamaForCausalLM(config=UpperCAmelCase_      )
           model.to(UpperCAmelCase_      )
           model.eval()

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

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

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

           lowercase__		       =						model(
               UpperCAmelCase_     ,attention_mask=UpperCAmelCase_     ,encoder_hidden_states=UpperCAmelCase_     ,encoder_attention_mask=UpperCAmelCase_     ,output_hidden_states=UpperCAmelCase_     ,)["hidden_states"][0]
           lowercase__		       =						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
           lowercase__		       =						ids_tensor((1,)     ,output_from_past.shape[-1]      ).item()
           lowercase__		       =						output_from_no_past[:, -3:, random_slice_idx].detach()
           lowercase__		       =						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 							_a      (				self      )  ->							Dict:
           lowercase__		       =						self.prepare_config_and_inputs()
           (
               (
               lowercase__
           )       ,       (
               lowercase__
           )       ,       (
               lowercase__
           )       ,       (
               lowercase__
           )       ,       (
               lowercase__
           )       ,       (
               lowercase__
           )       ,       (
               lowercase__
           )       ,       
           )		       =						config_and_inputs
           lowercase__		       =						{"input_ids": input_ids, "attention_mask": input_mask}
           return config, inputs_dict





@require_torch
class 				snake_case (UpperCamelCase		,      UpperCamelCase		,      UpperCamelCase		,      unittest.TestCase   ):
       lowerCAmelCase__  :Tuple						   =					(
           (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
       )
       lowerCAmelCase__  :Union[str, Any]						   =					(OpenLlamaForCausalLM,) if is_torch_available() else ()
       lowerCAmelCase__  :Any						   =					(
           {
               "feature-extraction": OpenLlamaModel,
               "text-classification": OpenLlamaForSequenceClassification,
               "text-generation": OpenLlamaForCausalLM,
               "zero-shot": OpenLlamaForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       lowerCAmelCase__  :Optional[int]						   =					False
       lowerCAmelCase__  :Tuple						   =					False



       def 							_a      (				self      )  ->							int:
           lowercase__		       =						OpenLlamaModelTester(self      )
           lowercase__		       =						ConfigTester(self     ,config_class=UpperCAmelCase_     ,hidden_size=37      )



       def 							_a      (				self      )  ->							Optional[int]:
           self.config_tester.run_common_tests()



       def 							_a      (				self      )  ->							Optional[int]:
           lowercase__		       =						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCAmelCase_      )



       def 							_a      (				self      )  ->							str:
           lowercase__		       =						self.model_tester.prepare_config_and_inputs()
           for type in ["absolute", "relative_key", "relative_key_query"]:
               lowercase__		       =						type
               self.model_tester.create_and_check_model(*UpperCAmelCase_      )



       def 							_a      (				self      )  ->							str:
           lowercase__       ,       lowercase__		       =						self.model_tester.prepare_config_and_inputs_for_common()
           lowercase__		       =						3
           lowercase__		       =						input_dict["input_ids"]
           lowercase__		       =						input_ids.ne(1      ).to(UpperCAmelCase_      )
           lowercase__		       =						ids_tensor([self.model_tester.batch_size]     ,self.model_tester.type_sequence_label_size      )
           lowercase__		       =						OpenLlamaForSequenceClassification(UpperCAmelCase_      )
           model.to(UpperCAmelCase_      )
           model.eval()
           lowercase__		       =						model(UpperCAmelCase_     ,attention_mask=UpperCAmelCase_     ,labels=UpperCAmelCase_      )
           self.assertEqual(result.logits.shape     ,(self.model_tester.batch_size, self.model_tester.num_labels)      )



       def 							_a      (				self      )  ->							List[str]:
           lowercase__       ,       lowercase__		       =						self.model_tester.prepare_config_and_inputs_for_common()
           lowercase__		       =						3
           lowercase__		       =						"single_label_classification"
           lowercase__		       =						input_dict["input_ids"]
           lowercase__		       =						input_ids.ne(1      ).to(UpperCAmelCase_      )
           lowercase__		       =						ids_tensor([self.model_tester.batch_size]     ,self.model_tester.type_sequence_label_size      )
           lowercase__		       =						OpenLlamaForSequenceClassification(UpperCAmelCase_      )
           model.to(UpperCAmelCase_      )
           model.eval()
           lowercase__		       =						model(UpperCAmelCase_     ,attention_mask=UpperCAmelCase_     ,labels=UpperCAmelCase_      )
           self.assertEqual(result.logits.shape     ,(self.model_tester.batch_size, self.model_tester.num_labels)      )



       def 							_a      (				self      )  ->							Tuple:
           lowercase__       ,       lowercase__		       =						self.model_tester.prepare_config_and_inputs_for_common()
           lowercase__		       =						3
           lowercase__		       =						"multi_label_classification"
           lowercase__		       =						input_dict["input_ids"]
           lowercase__		       =						input_ids.ne(1      ).to(UpperCAmelCase_      )
           lowercase__		       =						ids_tensor(
               [self.model_tester.batch_size, config.num_labels]     ,self.model_tester.type_sequence_label_size      ).to(torch.float      )
           lowercase__		       =						OpenLlamaForSequenceClassification(UpperCAmelCase_      )
           model.to(UpperCAmelCase_      )
           model.eval()
           lowercase__		       =						model(UpperCAmelCase_     ,attention_mask=UpperCAmelCase_     ,labels=UpperCAmelCase_      )
           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 							_a      (				self      )  ->							List[Any]:
           pass







       @parameterized.expand([("linear",), ("dynamic",)]      )
       def 							_a      (				self     ,UpperCAmelCase_      )  ->							Dict:
           lowercase__       ,       lowercase__		       =						self.model_tester.prepare_config_and_inputs_for_common()
           lowercase__		       =						ids_tensor([1, 10]     ,config.vocab_size      )
           lowercase__		       =						ids_tensor([1, int(config.max_position_embeddings * 1.5      )]     ,config.vocab_size      )

           set_seed(42      )  # Fixed seed at init time so the two models get the same random weights
           lowercase__		       =						OpenLlamaModel(UpperCAmelCase_      )
           original_model.to(UpperCAmelCase_      )
           original_model.eval()
           lowercase__		       =						original_model(UpperCAmelCase_      ).last_hidden_state
           lowercase__		       =						original_model(UpperCAmelCase_      ).last_hidden_state

           set_seed(42      )  # Fixed seed at init time so the two models get the same random weights
           lowercase__		       =						{"type": scaling_type, "factor": 10.0}
           lowercase__		       =						OpenLlamaModel(UpperCAmelCase_      )
           scaled_model.to(UpperCAmelCase_      )
           scaled_model.eval()
           lowercase__		       =						scaled_model(UpperCAmelCase_      ).last_hidden_state
           lowercase__		       =						scaled_model(UpperCAmelCase_      ).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(UpperCAmelCase_     ,UpperCAmelCase_     ,atol=1E-5      )      )
           else:
               self.assertFalse(torch.allclose(UpperCAmelCase_     ,UpperCAmelCase_     ,atol=1E-5      )      )

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









'''simple docstring'''





from typing import List, Optional

from tokenizers import ByteLevelBPETokenizer

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_blenderbot_small import BlenderbotSmallTokenizer


SCREAMING_SNAKE_CASE__      					=     logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__      					=     {
    "vocab_file": "vocab.json",
    "merges_file": "merges.txt",
    "tokenizer_config_file": "tokenizer_config.json",
}

SCREAMING_SNAKE_CASE__      					=     {
    "vocab_file": {
        "facebook/blenderbot_small-90M": "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/vocab.json"
    },
    "merges_file": {
        "facebook/blenderbot_small-90M": "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/merges.txt"
    },
    "tokenizer_config_file": {
        "facebook/blenderbot_small-90M": (
            "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/tokenizer_config.json"
        )
    },
}

SCREAMING_SNAKE_CASE__      					=     {
    "facebook/blenderbot_small-90M": 512,
}


class 				snake_case (UpperCamelCase   ):
       lowerCAmelCase__  :Union[str, Any]						   =					VOCAB_FILES_NAMES
       lowerCAmelCase__  :Any						   =					PRETRAINED_VOCAB_FILES_MAP
       lowerCAmelCase__  :str						   =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       lowerCAmelCase__  :List[Any]						   =					BlenderbotSmallTokenizer



       def __init__(				self     ,UpperCAmelCase_=None     ,UpperCAmelCase_=None     ,UpperCAmelCase_="<|endoftext|>"     ,UpperCAmelCase_="<|endoftext|>"     ,UpperCAmelCase_="<|endoftext|>"     ,UpperCAmelCase_=False     ,UpperCAmelCase_=True     ,**UpperCAmelCase_     ,)  ->							Optional[Any]:
           super().__init__(
               ByteLevelBPETokenizer(
                   vocab=UpperCAmelCase_     ,merges=UpperCAmelCase_     ,add_prefix_space=UpperCAmelCase_     ,trim_offsets=UpperCAmelCase_     ,)     ,bos_token=UpperCAmelCase_     ,eos_token=UpperCAmelCase_     ,unk_token=UpperCAmelCase_     ,**UpperCAmelCase_     ,)
           lowercase__		       =						add_prefix_space



       def 							_a      (				self     ,UpperCAmelCase_     ,UpperCAmelCase_=None      )  ->							Optional[int]:
           lowercase__		       =						[self.bos_token_id] + token_ids_a + [self.eos_token_id]
           if token_ids_a is None:
               return output

           return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]







       def 							_a      (				self     ,UpperCAmelCase_     ,UpperCAmelCase_ = None      )  ->							List[int]:
           lowercase__		       =						[self.sep_token_id]
           lowercase__		       =						[self.cls_token_id]

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




