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 : str      ,						A__ : Tuple      ,						A__ : Any=13      ,						A__ : Tuple=7      ,						A__ : List[str]=True      ,						A__ : Optional[Any]=True      ,						A__ : Tuple=False      ,						A__ : List[Any]=True      ,						A__ : Union[str, Any]=99      ,						A__ : Optional[Any]=32      ,						A__ : Tuple=5      ,						A__ : List[str]=4      ,						A__ : int=37      ,						A__ : Dict="gelu"      ,						A__ : Tuple=0.1      ,						A__ : Optional[Any]=0.1      ,						A__ : Tuple=5_12      ,						A__ : List[str]=16      ,						A__ : Any=2      ,						A__ : str=0.02      ,						A__ : Optional[int]=3      ,						A__ : Union[str, Any]=4      ,						A__ : int=None      ,						)				->  Optional[Any]:




         '''simple docstring'''




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



   def        UpperCAmelCase__							(					self : int )				->  Optional[Any]:




         '''simple docstring'''




         snake_case_  :      Any  	=							ids_tensor([self.batch_size, self.seq_length]      ,						self.vocab_size )

         snake_case_  :      Tuple  	=							None
         if self.use_input_mask:
               snake_case_  :      List[Any]  	=							random_attention_mask([self.batch_size, self.seq_length] )

         snake_case_  :      Tuple  	=							None
         if self.use_token_type_ids:
               snake_case_  :      List[str]  	=							ids_tensor([self.batch_size, self.seq_length]      ,						self.type_vocab_size )

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

         snake_case_  :      Any  	=							self.get_config()

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



   def        UpperCAmelCase__							(					self : Optional[int] )				->  Dict:




         '''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=snake_case__      ,						initializer_range=self.initializer_range      ,						use_stable_embedding=snake_case__      ,						)



   def        UpperCAmelCase__							(					self : Union[str, Any]      ,						A__ : Tuple      ,						A__ : Optional[Any]      ,						A__ : Optional[Any]      ,						A__ : Tuple      ,						A__ : Any      ,						A__ : Any      ,						A__ : int )				->  Optional[Any]:




         '''simple docstring'''




         snake_case_  :      Union[str, Any]  	=							OpenLlamaModel(config=snake_case__ )
         model.to(snake_case__ )
         model.eval()
         snake_case_  :      str  	=							model(snake_case__      ,						attention_mask=snake_case__ )
         snake_case_  :      int  	=							model(snake_case__ )
         self.parent.assertEqual(result.last_hidden_state.shape      ,						(self.batch_size, self.seq_length, self.hidden_size) )



   def        UpperCAmelCase__							(					self : Optional[int]      ,						A__ : Any      ,						A__ : str      ,						A__ : Union[str, Any]      ,						A__ : Tuple      ,						A__ : Optional[Any]      ,						A__ : Dict      ,						A__ : int      ,						A__ : Any      ,						A__ : Tuple      ,						)				->  List[str]:




         '''simple docstring'''




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



   def        UpperCAmelCase__							(					self : int      ,						A__ : Optional[int]      ,						A__ : List[str]      ,						A__ : int      ,						A__ : int      ,						A__ : str      ,						A__ : Dict      ,						A__ : Union[str, Any]      ,						A__ : List[str]      ,						A__ : Optional[Any]      ,						)				->  Optional[int]:




         '''simple docstring'''




         snake_case_  :      Dict  	=							OpenLlamaForCausalLM(config=snake_case__ )
         model.to(snake_case__ )
         model.eval()
         snake_case_  :      Optional[int]  	=							model(snake_case__      ,						attention_mask=snake_case__      ,						labels=snake_case__ )
         self.parent.assertEqual(result.logits.shape      ,						(self.batch_size, self.seq_length, self.vocab_size) )



   def        UpperCAmelCase__							(					self : Optional[Any]      ,						A__ : Optional[Any]      ,						A__ : Dict      ,						A__ : Any      ,						A__ : int      ,						A__ : Optional[Any]      ,						A__ : Union[str, Any]      ,						A__ : Dict      ,						A__ : str      ,						A__ : Union[str, Any]      ,						)				->  Optional[int]:




         '''simple docstring'''




         snake_case_  :      int  	=							True
         snake_case_  :      List[str]  	=							True
         snake_case_  :      Optional[Any]  	=							OpenLlamaForCausalLM(config=snake_case__ )
         model.to(snake_case__ )
         model.eval()

         # first forward pass
         snake_case_  :      List[str]  	=							model(
             snake_case__      ,						attention_mask=snake_case__      ,						encoder_hidden_states=snake_case__      ,						encoder_attention_mask=snake_case__      ,						use_cache=snake_case__      ,						)
         snake_case_  :      str  	=							outputs.past_key_values

         # create hypothetical multiple next token and extent to next_input_ids
         snake_case_  :      List[str]  	=							ids_tensor((self.batch_size, 3)      ,						config.vocab_size )
         snake_case_  :      Dict  	=							ids_tensor((self.batch_size, 3)      ,						vocab_size=2 )

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

         snake_case_  :      str  	=							model(
             snake_case__      ,						attention_mask=snake_case__      ,						encoder_hidden_states=snake_case__      ,						encoder_attention_mask=snake_case__      ,						output_hidden_states=snake_case__      ,						)["""hidden_states"""][0]
         snake_case_  :      Union[str, Any]  	=							model(
             snake_case__      ,						attention_mask=snake_case__      ,						encoder_hidden_states=snake_case__      ,						encoder_attention_mask=snake_case__      ,						past_key_values=snake_case__      ,						output_hidden_states=snake_case__      ,						)["""hidden_states"""][0]

         # select random slice
         snake_case_  :      List[str]  	=							ids_tensor((1,)      ,						output_from_past.shape[-1] ).item()
         snake_case_  :      Optional[int]  	=							output_from_no_past[:, -3:, random_slice_idx].detach()
         snake_case_  :      Union[str, Any]  	=							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(snake_case__      ,						snake_case__      ,						atol=1E-3 ) )



   def        UpperCAmelCase__							(					self : Optional[int] )				->  Any:




         '''simple docstring'''




         snake_case_  :      Union[str, Any]  	=							self.prepare_config_and_inputs()
         (
             snake_case_		
         )  :      str  	=							config_and_inputs
         snake_case_  :      List[Any]  	=							{"""input_ids""": input_ids, """attention_mask""": input_mask}
         return config, inputs_dict



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



   def        UpperCAmelCase__							(					self : str )				->  List[Any]:




         '''simple docstring'''




         snake_case_  :      Tuple  	=							OpenLlamaModelTester(self )
         snake_case_  :      Optional[int]  	=							ConfigTester(self      ,						config_class=snake_case__      ,						hidden_size=37 )



   def        UpperCAmelCase__							(					self : Any )				->  str:




         '''simple docstring'''




         self.config_tester.run_common_tests()



   def        UpperCAmelCase__							(					self : str )				->  Optional[int]:




         '''simple docstring'''




         snake_case_  :      int  	=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*snake_case__ )



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




         '''simple docstring'''




         snake_case_  :      List[str]  	=							self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
               snake_case_  :      List[str]  	=							type
               self.model_tester.create_and_check_model(*snake_case__ )



   def        UpperCAmelCase__							(					self : Any )				->  str:




         '''simple docstring'''




         snake_case_  :      Optional[Any]  	=							self.model_tester.prepare_config_and_inputs_for_common()
         snake_case_  :      Tuple  	=							3
         snake_case_  :      Dict  	=							input_dict["""input_ids"""]
         snake_case_  :      List[Any]  	=							input_ids.ne(1 ).to(snake_case__ )
         snake_case_  :      int  	=							ids_tensor([self.model_tester.batch_size]      ,						self.model_tester.type_sequence_label_size )
         snake_case_  :      Dict  	=							OpenLlamaForSequenceClassification(snake_case__ )
         model.to(snake_case__ )
         model.eval()
         snake_case_  :      int  	=							model(snake_case__      ,						attention_mask=snake_case__      ,						labels=snake_case__ )
         self.assertEqual(result.logits.shape      ,						(self.model_tester.batch_size, self.model_tester.num_labels) )



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




         '''simple docstring'''




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



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




         '''simple docstring'''




         snake_case_  :      List[Any]  	=							self.model_tester.prepare_config_and_inputs_for_common()
         snake_case_  :      Union[str, Any]  	=							3
         snake_case_  :      List[str]  	=							"""multi_label_classification"""
         snake_case_  :      str  	=							input_dict["""input_ids"""]
         snake_case_  :      Optional[int]  	=							input_ids.ne(1 ).to(snake_case__ )
         snake_case_  :      List[str]  	=							ids_tensor(
             [self.model_tester.batch_size, config.num_labels]      ,						self.model_tester.type_sequence_label_size ).to(torch.float )
         snake_case_  :      Dict  	=							OpenLlamaForSequenceClassification(snake_case__ )
         model.to(snake_case__ )
         model.eval()
         snake_case_  :      Union[str, Any]  	=							model(snake_case__      ,						attention_mask=snake_case__      ,						labels=snake_case__ )
         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        UpperCAmelCase__							(					self : Any )				->  str:




         '''simple docstring'''




         pass



   @parameterized.expand([("linear",), ("dynamic",)] )
   def        UpperCAmelCase__							(					self : Optional[int]      ,						A__ : Optional[Any] )				->  Dict:




         '''simple docstring'''




         snake_case_  :      str  	=							self.model_tester.prepare_config_and_inputs_for_common()
         snake_case_  :      List[str]  	=							ids_tensor([1, 10]      ,						config.vocab_size )
         snake_case_  :      str  	=							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
         snake_case_  :      List[str]  	=							OpenLlamaModel(snake_case__ )
         original_model.to(snake_case__ )
         original_model.eval()
         snake_case_  :      Union[str, Any]  	=							original_model(snake_case__ ).last_hidden_state
         snake_case_  :      Dict  	=							original_model(snake_case__ ).last_hidden_state

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

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










from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


UpperCAmelCase_							=				{
    """configuration_maskformer""": ["""MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MaskFormerConfig"""],
    """configuration_maskformer_swin""": ["""MaskFormerSwinConfig"""],
}

try:
							if not is_vision_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							UpperCAmelCase_							=				["""MaskFormerFeatureExtractor"""]
							UpperCAmelCase_							=				["""MaskFormerImageProcessor"""]


try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							UpperCAmelCase_							=				[
							    """MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """MaskFormerForInstanceSegmentation""",
							    """MaskFormerModel""",
							    """MaskFormerPreTrainedModel""",
							]
							UpperCAmelCase_							=				[
							    """MaskFormerSwinBackbone""",
							    """MaskFormerSwinModel""",
							    """MaskFormerSwinPreTrainedModel""",
							]

if TYPE_CHECKING:
							from .configuration_maskformer import MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskFormerConfig
							from .configuration_maskformer_swin import MaskFormerSwinConfig

							try:
														if not is_vision_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .feature_extraction_maskformer import MaskFormerFeatureExtractor
														from .image_processing_maskformer import MaskFormerImageProcessor
							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_maskformer import (
														    MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
														    MaskFormerForInstanceSegmentation,
														    MaskFormerModel,
														    MaskFormerPreTrainedModel,
														)
														from .modeling_maskformer_swin import (
														    MaskFormerSwinBackbone,
														    MaskFormerSwinModel,
														    MaskFormerSwinPreTrainedModel,
														)


else:
							import sys

							UpperCAmelCase_							=				_LazyModule(__name__, globals()["""__file__"""], _import_structure)