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







 """simple docstring"""


 def __init__( self		:   Tuple       , _snake_case		:   Optional[int]       , _snake_case		:   Optional[Any]=13       , _snake_case		:   Dict=7       , _snake_case		:   str=True       , _snake_case		:   Optional[int]=True       , _snake_case		:   Union[str, Any]=False       , _snake_case		:   int=True       , _snake_case		:   str=99       , _snake_case		:   List[str]=32       , _snake_case		:   Tuple=5       , _snake_case		:   Optional[Any]=4       , _snake_case		:   Union[str, Any]=37       , _snake_case		:   List[Any]="gelu"       , _snake_case		:   List[Any]=0.1       , _snake_case		:   int=0.1       , _snake_case		:   Union[str, Any]=512       , _snake_case		:   str=16       , _snake_case		:   Any=2       , _snake_case		:   Optional[int]=0.02       , _snake_case		:   str=3       , _snake_case		:   List[Any]=4       , _snake_case		:   Tuple=None       , )					->      Optional[int]:




    '''simple docstring'''






    a__  					=		parent
    a__  					=		batch_size
    a__  					=		seq_length
    a__  					=		is_training
    a__  					=		use_input_mask
    a__  					=		use_token_type_ids
    a__  					=		use_labels
    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__  					=		type_vocab_size
    a__  					=		type_sequence_label_size
    a__  					=		initializer_range
    a__  					=		num_labels
    a__  					=		num_choices
    a__  					=		scope


 def 							_lowerCAmelCase					( self		:   Union[str, Any]					)					->      List[Any]:




    '''simple docstring'''






    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]					)

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

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

    a__  					=		self.get_config()

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


 def 							_lowerCAmelCase					( self		:   Union[str, Any]					)					->      str:




    '''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[int]       , _snake_case		:   List[Any]       , _snake_case		:   Optional[int]       , _snake_case		:   Tuple       , _snake_case		:   Union[str, Any]       , _snake_case		:   Any       , _snake_case		:   Optional[int]       , _snake_case		:   Optional[int]					)					->      int:




    '''simple docstring'''






    a__  					=		OpenLlamaModel(config=_a					)
    model.to(_a					)
    model.eval()
    a__  					=		model(_a       , attention_mask=_a					)
    a__  					=		model(_a					)
    self.parent.assertEqual(result.last_hidden_state.shape       , (self.batch_size, self.seq_length, self.hidden_size)					)


 def 							_lowerCAmelCase					( self		:   Union[str, Any]       , _snake_case		:   List[str]       , _snake_case		:   int       , _snake_case		:   Dict       , _snake_case		:   List[str]       , _snake_case		:   Dict       , _snake_case		:   Union[str, Any]       , _snake_case		:   Dict       , _snake_case		:   int       , _snake_case		:   str       , )					->      int:




    '''simple docstring'''






    a__  					=		True
    a__  					=		OpenLlamaModel(_a					)
    model.to(_a					)
    model.eval()
    a__  					=		model(
        _a       , attention_mask=_a       , encoder_hidden_states=_a       , encoder_attention_mask=_a       , )
    a__  					=		model(
        _a       , attention_mask=_a       , encoder_hidden_states=_a       , )
    a__  					=		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		:   Dict       , _snake_case		:   int       , _snake_case		:   Optional[Any]       , _snake_case		:   Dict       , _snake_case		:   List[str]       , _snake_case		:   Tuple       , _snake_case		:   Tuple       , _snake_case		:   List[str]       , _snake_case		:   List[Any]       , _snake_case		:   List[Any]       , )					->      Optional[int]:




    '''simple docstring'''






    a__  					=		OpenLlamaForCausalLM(config=_a					)
    model.to(_a					)
    model.eval()
    a__  					=		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		:   Optional[int]       , _snake_case		:   Tuple       , _snake_case		:   Any       , _snake_case		:   int       , _snake_case		:   Tuple       , _snake_case		:   List[str]       , _snake_case		:   str       , _snake_case		:   Optional[int]       , _snake_case		:   str       , _snake_case		:   Any       , )					->      List[str]:




    '''simple docstring'''






    a__  					=		True
    a__  					=		True
    a__  					=		OpenLlamaForCausalLM(config=_a					)
    model.to(_a					)
    model.eval()

    # first forward pass
    a__  					=		model(
        _a       , attention_mask=_a       , encoder_hidden_states=_a       , encoder_attention_mask=_a       , use_cache=_a       , )
    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(
        _a       , attention_mask=_a       , encoder_hidden_states=_a       , encoder_attention_mask=_a       , output_hidden_states=_a       , )["hidden_states"][0]
    a__  					=		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__  					=		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(_a       , _a       , atol=1E-3					)					)



 def 							_lowerCAmelCase					( self		:   List[Any]					)					->      Union[str, Any]:




    '''simple docstring'''






    a__  					=		self.prepare_config_and_inputs()
    (
        a__       
    )  					=		config_and_inputs
    a__  					=		{"input_ids": input_ids, "attention_mask": input_mask}
    return config, inputs_dict

@require_torch
class SCREAMING_SNAKE_CASE  (     a_ ,   a_ ,   a_ ,   unittest.TestCase						):







 """simple docstring"""


 a_							:						Union[str, Any]	      =(
     (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
 )
 a_							:						str	      =(OpenLlamaForCausalLM,) if is_torch_available() else ()
 a_							:						int	      =(
     {
         "feature-extraction": OpenLlamaModel,
         "text-classification": OpenLlamaForSequenceClassification,
         "text-generation": OpenLlamaForCausalLM,
         "zero-shot": OpenLlamaForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 a_							:						Any	      =False
 a_							:						Optional[Any]	      =False


 def 							_lowerCAmelCase					( self		:   int					)					->      List[str]:




    '''simple docstring'''






    a__  					=		OpenLlamaModelTester(self					)
    a__  					=		ConfigTester(self       , config_class=_a       , hidden_size=37					)


 def 							_lowerCAmelCase					( self		:   List[str]					)					->      Union[str, Any]:




    '''simple docstring'''






    self.config_tester.run_common_tests()


 def 							_lowerCAmelCase					( self		:   Dict					)					->      List[str]:




    '''simple docstring'''






    a__  					=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*_a					)


 def 							_lowerCAmelCase					( self		:   List[Any]					)					->      int:




    '''simple docstring'''






    a__  					=		self.model_tester.prepare_config_and_inputs()
    for type in ["absolute", "relative_key", "relative_key_query"]:
       a__  					=		type
       self.model_tester.create_and_check_model(*_a					)


 def 							_lowerCAmelCase					( self		:   Dict					)					->      str:




    '''simple docstring'''






    a__  					=		self.model_tester.prepare_config_and_inputs_for_common()
    a__  					=		3
    a__  					=		input_dict["input_ids"]
    a__  					=		input_ids.ne(1					).to(_a					)
    a__  					=		ids_tensor([self.model_tester.batch_size]       , self.model_tester.type_sequence_label_size					)
    a__  					=		OpenLlamaForSequenceClassification(_a					)
    model.to(_a					)
    model.eval()
    a__  					=		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		:   str					)					->      str:




    '''simple docstring'''






    a__  					=		self.model_tester.prepare_config_and_inputs_for_common()
    a__  					=		3
    a__  					=		"single_label_classification"
    a__  					=		input_dict["input_ids"]
    a__  					=		input_ids.ne(1					).to(_a					)
    a__  					=		ids_tensor([self.model_tester.batch_size]       , self.model_tester.type_sequence_label_size					)
    a__  					=		OpenLlamaForSequenceClassification(_a					)
    model.to(_a					)
    model.eval()
    a__  					=		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		:   int					)					->      Optional[Any]:




    '''simple docstring'''






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




    '''simple docstring'''






    pass



 @parameterized.expand([('linear',), ('dynamic',)]					)
 def 							_lowerCAmelCase					( self		:   Union[str, Any]       , _snake_case		:   Dict					)					->      str:




    '''simple docstring'''






    a__  					=		self.model_tester.prepare_config_and_inputs_for_common()
    a__  					=		ids_tensor([1, 10]       , config.vocab_size					)
    a__  					=		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
    a__  					=		OpenLlamaModel(_a					)
    original_model.to(_a					)
    original_model.eval()
    a__  					=		original_model(_a					).last_hidden_state
    a__  					=		original_model(_a					).last_hidden_state

    set_seed(42					)  # Fixed seed at init time so the two models get the same random weights
    a__  					=		{"type": scaling_type, "factor": 10.0}
    a__  					=		OpenLlamaModel(_a					)
    scaled_model.to(_a					)
    scaled_model.eval()
    a__  					=		scaled_model(_a					).last_hidden_state
    a__  					=		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"""




# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_A     :			Optional[int]  						=			{
    """configuration_mgp_str""": ["""MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MgpstrConfig"""],
    """processing_mgp_str""": ["""MgpstrProcessor"""],
    """tokenization_mgp_str""": ["""MgpstrTokenizer"""],
}

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _A     :			Any  						=			[
       """MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST""",
       """MgpstrModel""",
       """MgpstrPreTrainedModel""",
       """MgpstrForSceneTextRecognition""",
   ]

if TYPE_CHECKING:
   from .configuration_mgp_str import MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP, MgpstrConfig
   from .processing_mgp_str import MgpstrProcessor
   from .tokenization_mgp_str import MgpstrTokenizer

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_mgp_str import (
          MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST,
          MgpstrForSceneTextRecognition,
          MgpstrModel,
          MgpstrPreTrainedModel,
      )
else:
   import sys

   _A     :			Tuple  						=			_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


