


"""simple docstring"""






import unittest

from parameterized import parameterized

from transformers import LlamaConfig, is_torch_available, set_seed
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, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch

  from transformers import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaTokenizer




class 							A_      :


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







     '''simple docstring'''







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


  def 							_lowercase					(			self:						Dict    ):







     '''simple docstring'''







     _lowerCamelCase   :				Tuple							=     ids_tensor([self.batch_size, self.seq_length]							,self.vocab_size    )

     _lowerCamelCase   :				str							=     None
     if self.use_input_mask:
        _lowerCamelCase   :				Dict							=     random_attention_mask([self.batch_size, self.seq_length]    )

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

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

     _lowerCamelCase   :				Dict							=     self.get_config()

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


  def 							_lowercase					(			self:						List[str]    ):







     '''simple docstring'''







     return LlamaConfig(
         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=__lowerCAmelCase							,initializer_range=self.initializer_range							,)


  def 							_lowercase					(			self:						int							,__lowerCAmelCase:						Tuple							,__lowerCAmelCase:						Union[str, Any]							,__lowerCAmelCase:						int							,__lowerCAmelCase:						Tuple							,__lowerCAmelCase:						List[str]							,__lowerCAmelCase:						List[Any]							,__lowerCAmelCase:						Dict    ):







     '''simple docstring'''







     _lowerCamelCase   :				Optional[int]							=     LlamaModel(config=__lowerCAmelCase    )
     model.to(__lowerCAmelCase    )
     model.eval()
     _lowerCamelCase   :				int							=     model(__lowerCAmelCase							,attention_mask=__lowerCAmelCase    )
     _lowerCamelCase   :				Dict							=     model(__lowerCAmelCase    )
     self.parent.assertEqual(result.last_hidden_state.shape							,(self.batch_size, self.seq_length, self.hidden_size)    )


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







     '''simple docstring'''







     _lowerCamelCase   :				Union[str, Any]							=     True
     _lowerCamelCase   :				List[Any]							=     LlamaModel(__lowerCAmelCase    )
     model.to(__lowerCAmelCase    )
     model.eval()
     _lowerCamelCase   :				Tuple							=     model(
         __lowerCAmelCase							,attention_mask=__lowerCAmelCase							,encoder_hidden_states=__lowerCAmelCase							,encoder_attention_mask=__lowerCAmelCase							,)
     _lowerCamelCase   :				List[str]							=     model(
         __lowerCAmelCase							,attention_mask=__lowerCAmelCase							,encoder_hidden_states=__lowerCAmelCase							,)
     _lowerCamelCase   :				Dict							=     model(__lowerCAmelCase							,attention_mask=__lowerCAmelCase    )
     self.parent.assertEqual(result.last_hidden_state.shape							,(self.batch_size, self.seq_length, self.hidden_size)    )


  def 							_lowercase					(			self:						Optional[Any]							,__lowerCAmelCase:						Tuple							,__lowerCAmelCase:						Optional[Any]							,__lowerCAmelCase:						List[Any]							,__lowerCAmelCase:						Optional[int]							,__lowerCAmelCase:						List[Any]							,__lowerCAmelCase:						Union[str, Any]							,__lowerCAmelCase:						Tuple							,__lowerCAmelCase:						Any							,__lowerCAmelCase:						List[str]							,):







     '''simple docstring'''







     _lowerCamelCase   :				Tuple							=     LlamaForCausalLM(config=__lowerCAmelCase    )
     model.to(__lowerCAmelCase    )
     model.eval()
     _lowerCamelCase   :				Any							=     model(__lowerCAmelCase							,attention_mask=__lowerCAmelCase							,labels=__lowerCAmelCase    )
     self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length, self.vocab_size)    )


  def 							_lowercase					(			self:						Union[str, Any]							,__lowerCAmelCase:						int							,__lowerCAmelCase:						Union[str, Any]							,__lowerCAmelCase:						List[str]							,__lowerCAmelCase:						Dict							,__lowerCAmelCase:						Union[str, Any]							,__lowerCAmelCase:						Optional[int]							,__lowerCAmelCase:						Union[str, Any]							,__lowerCAmelCase:						Any							,__lowerCAmelCase:						Union[str, Any]							,):







     '''simple docstring'''







     _lowerCamelCase   :				Union[str, Any]							=     True
     _lowerCamelCase   :				Dict							=     True
     _lowerCamelCase   :				int							=     LlamaForCausalLM(config=__lowerCAmelCase    )
     model.to(__lowerCAmelCase    )
     model.eval()

     # first forward pass
     _lowerCamelCase   :				Any							=     model(
         __lowerCAmelCase							,attention_mask=__lowerCAmelCase							,encoder_hidden_states=__lowerCAmelCase							,encoder_attention_mask=__lowerCAmelCase							,use_cache=__lowerCAmelCase							,)
     _lowerCamelCase   :				Union[str, Any]							=     outputs.past_key_values

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

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

     _lowerCamelCase   :				Tuple							=     model(
         __lowerCAmelCase							,attention_mask=__lowerCAmelCase							,encoder_hidden_states=__lowerCAmelCase							,encoder_attention_mask=__lowerCAmelCase							,output_hidden_states=__lowerCAmelCase							,)["hidden_states"][0]
     _lowerCamelCase   :				List[str]							=     model(
         __lowerCAmelCase							,attention_mask=__lowerCAmelCase							,encoder_hidden_states=__lowerCAmelCase							,encoder_attention_mask=__lowerCAmelCase							,past_key_values=__lowerCAmelCase							,output_hidden_states=__lowerCAmelCase							,)["hidden_states"][0]

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






  def 							_lowercase					(			self:						Optional[Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Optional[Any]							=     self.prepare_config_and_inputs()
     (
         (
         _lowerCamelCase
     ),					(
         _lowerCamelCase
     ),					(
         _lowerCamelCase
     ),					(
         _lowerCamelCase
     ),					(
         _lowerCamelCase
     ),					(
         _lowerCamelCase
     ),					(
         _lowerCamelCase
     ),					
     )   :				Dict							=     config_and_inputs
     _lowerCamelCase   :				Dict							=     {"input_ids": input_ids, "attention_mask": input_mask}
     return config, inputs_dict




@require_torch
class 							A_      (       _a     ,	_a     ,	_a     ,	unittest.TestCase ):
  lowerCAmelCase__      			=	(LlamaModel, LlamaForCausalLM, LlamaForSequenceClassification) if is_torch_available() else ()
  lowerCAmelCase__      			=	(LlamaForCausalLM,) if is_torch_available() else ()
  lowerCAmelCase__      			=	(
      {
          'feature-extraction': LlamaModel,
          'text-classification': LlamaForSequenceClassification,
          'text-generation': LlamaForCausalLM,
          'zero-shot': LlamaForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  lowerCAmelCase__      			=	False
  lowerCAmelCase__      			=	False


  def 							_lowercase					(			self:						str    ):







     '''simple docstring'''







     _lowerCamelCase   :				List[str]							=     LlamaModelTester(self    )
     _lowerCamelCase   :				Optional[Any]							=     ConfigTester(self							,config_class=__lowerCAmelCase							,hidden_size=37    )


  def 							_lowercase					(			self:						Union[str, Any]    ):







     '''simple docstring'''







     self.config_tester.run_common_tests()


  def 							_lowercase					(			self:						Optional[int]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Optional[int]							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__lowerCAmelCase    )


  def 							_lowercase					(			self:						List[str]    ):







     '''simple docstring'''







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


  def 							_lowercase					(			self:						List[Any]    ):







     '''simple docstring'''







     _lowerCamelCase,					_lowerCamelCase   :				str							=     self.model_tester.prepare_config_and_inputs_for_common()
     _lowerCamelCase   :				List[Any]							=     3
     _lowerCamelCase   :				Optional[Any]							=     input_dict["input_ids"]
     _lowerCamelCase   :				List[str]							=     input_ids.ne(1    ).to(__lowerCAmelCase    )
     _lowerCamelCase   :				Any							=     ids_tensor([self.model_tester.batch_size]							,self.model_tester.type_sequence_label_size    )
     _lowerCamelCase   :				int							=     LlamaForSequenceClassification(__lowerCAmelCase    )
     model.to(__lowerCAmelCase    )
     model.eval()
     _lowerCamelCase   :				Any							=     model(__lowerCAmelCase							,attention_mask=__lowerCAmelCase							,labels=__lowerCAmelCase    )
     self.assertEqual(result.logits.shape							,(self.model_tester.batch_size, self.model_tester.num_labels)    )


  def 							_lowercase					(			self:						str    ):







     '''simple docstring'''







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


  def 							_lowercase					(			self:						Optional[int]    ):







     '''simple docstring'''







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


  @unittest.skip("LLaMA buffers include complex numbers, which breaks this test"    )
  def 							_lowercase					(			self:						Any    ):







     '''simple docstring'''







     pass






  @parameterized.expand([("linear",), ("dynamic",)]    )
  def 							_lowercase					(			self:						Dict							,__lowerCAmelCase:						Optional[Any]    ):







     '''simple docstring'''







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

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

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




@require_torch
class 							A_      (       unittest.TestCase ):


  @unittest.skip("Logits are not exactly the same, once we fix the instabalities somehow, will update!"    )
  @slow
  def 							_lowercase					(			self:						Optional[int]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Tuple							=     [1, 306, 4_658, 278, 6_593, 310, 2_834, 338]
     _lowerCamelCase   :				List[Any]							=     LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-7b-hf"							,device_map="auto"    )
     _lowerCamelCase   :				List[str]							=     model(torch.tensor([input_ids]    )    )
     # Expected mean on dim = -1
     _lowerCamelCase   :				Any							=     torch.tensor([[-6.65_50, -4.12_27, -4.98_59, -3.24_06, 0.82_62, -3.00_33, 1.29_64, -3.36_99]]    )
     torch.testing.assert_close(out.mean(-1    )							,__lowerCAmelCase							,atol=1e-2							,rtol=1e-2    )
     # slicing logits[0, 0, 0:30]
     # fmt: off
     _lowerCamelCase   :				Dict							=     torch.tensor([-12.82_81, -7.44_53, -0.46_39, -8.06_25, -7.25_00, -8.00_00, -6.48_83, -7.76_95, -7.84_38, -7.03_12, -6.21_88, -7.13_28, -1.84_96, 1.99_61, -8.62_50, -6.72_27, -12.82_81, -6.94_92, -7.07_42, -7.78_52, -7.58_20, -7.90_62, -6.93_75, -7.98_05, -8.34_38, -8.15_62, -8.04_69, -7.62_50, -7.74_22, -7.33_98,]    )
     # fmt: on
     torch.testing.assert_close(out[0, 0, :30]							,__lowerCAmelCase							,atol=1e-5							,rtol=1e-5    )


  @unittest.skip("Logits are not exactly the same, once we fix the instabalities somehow, will update!"    )
  @slow
  def 							_lowercase					(			self:						int    ):







     '''simple docstring'''







     _lowerCamelCase   :				List[str]							=     [1, 306, 4_658, 278, 6_593, 310, 2_834, 338]
     _lowerCamelCase   :				Dict							=     LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-13b-hf"							,device_map="auto"    )
     _lowerCamelCase   :				Dict							=     model(torch.tensor(__lowerCAmelCase    )    )
     # Expected mean on dim = -1
     _lowerCamelCase   :				List[str]							=     torch.tensor([[-2.06_22, -1.27_94, -1.16_38, -0.97_88, -1.46_03, -1.02_38, -1.78_93, -1.44_11]]    )
     torch.testing.assert_close(out.mean(-1    )							,__lowerCAmelCase							,atol=1e-2							,rtol=1e-2    )
     # slicing logits[0, 0, 0:30]
     # fmt: off
     _lowerCamelCase   :				Union[str, Any]							=     torch.tensor([-8.14_06, -8.05_47, 2.74_61, -1.23_44, -0.14_48, -1.82_62, -1.00_20, -1.81_54, -1.68_95, -1.85_16, -2.35_74, -0.92_77, 3.75_98, 6.57_42, -1.29_98, -0.11_77, -8.14_06, -2.96_88, -2.91_99, -3.16_99, -3.52_54, -2.35_55, -2.79_88, -3.41_41, -2.82_62, -4.51_95, -3.33_79, -3.31_64, -2.78_32, -3.02_73]    )
     # fmt: on
     torch.testing.assert_close(out[0, 0, :30]							,__lowerCAmelCase							,atol=1e-5							,rtol=1e-5    )


  @unittest.skip("Logits are not exactly the same, once we fix the instabalities somehow, will update!"    )
  @slow
  def 							_lowercase					(			self:						Optional[int]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Optional[int]							=     [1, 306, 4_658, 278, 6_593, 310, 2_834, 338]
     _lowerCamelCase   :				Union[str, Any]							=     LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-13b-chat-hf"							,device_map="auto"    )
     _lowerCamelCase   :				List[Any]							=     model(torch.tensor(__lowerCAmelCase    )    )
     # Expected mean on dim = -1
     _lowerCamelCase   :				Union[str, Any]							=     torch.tensor([[-0.85_62, -1.85_20, -0.75_51, -0.41_62, -1.51_61, -1.20_38, -2.48_23, -2.32_54]]    )
     torch.testing.assert_close(out.mean(-1    )							,__lowerCAmelCase							,atol=1e-2							,rtol=1e-2    )
     # slicing logits[0, 0, 0:30]
     # fmt: off
     _lowerCamelCase   :				Any							=     torch.tensor([-2.22_27, 4.88_28, 0.90_23, -0.45_78, -0.78_71, -0.10_33, -0.62_21, -0.57_86, -0.78_03, -1.06_74, -1.29_20, -0.15_70, 0.80_08, 2.07_23, -0.94_97, 0.27_71, -2.22_27, -0.76_12, -1.43_46, -1.20_61, -1.64_26, -0.30_00, -0.71_39, -1.19_34, -1.86_91, -1.69_73, -1.59_47, -1.27_05, -0.35_23, -0.55_13]    )
     # fmt: on
     torch.testing.assert_close(out.mean(-1    )							,__lowerCAmelCase							,atol=1e-2							,rtol=1e-2    )


  @unittest.skip(
      "Logits are not exactly the same, once we fix the instabalities somehow, will update! Also it is gonna be a `too_slow` test"    )
  @slow
  def 							_lowercase					(			self:						Tuple    ):







     '''simple docstring'''







     _lowerCamelCase   :				str							=     [1, 306, 4_658, 278, 6_593, 310, 2_834, 338]
     _lowerCamelCase   :				List[Any]							=     LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-70b-hf"							,device_map="auto"    )
     _lowerCamelCase   :				Dict							=     model(torch.tensor(__lowerCAmelCase    )    )

     _lowerCamelCase   :				Any							=     torch.tensor(
         [[-4.23_27, -3.33_60, -4.66_65, -4.76_31, -1.81_80, -3.41_70, -1.42_11, -3.18_10]]							,dtype=torch.floataa    )
     torch.testing.assert_close(out.mean(-1    )							,__lowerCAmelCase							,atol=1e-2							,rtol=1e-2    )
     # fmt: off
     _lowerCamelCase   :				str							=     torch.tensor([-9.49_22, -3.95_51, 1.79_98, -5.67_58, -5.10_55, -5.89_84, -4.83_20, -6.80_86, -6.53_91, -5.61_72, -5.58_20, -5.53_52, 1.78_81, 3.62_89, -6.51_17, -3.47_85, -9.50_00, -6.03_52, -6.81_25, -6.01_95, -6.68_36, -5.47_27, -6.28_12, -6.03_91, -7.33_98, -7.42_97, -7.48_44, -6.58_20, -5.87_89, -5.53_12]    )
     # fmt: on
     torch.testing.assert_close(out[0, 0, :30]							,__lowerCAmelCase							,atol=1e-5							,rtol=1e-5    )






  @unittest.skip("Model is curently gated"    )
  @slow
  def 							_lowercase					(			self:						Dict    ):







     '''simple docstring'''







     _lowerCamelCase   :				Optional[int]							=     "Simply put, the theory of relativity states that 1) the laws of physics are the same everywhere in the universe and 2) the passage of time and the length of objects can vary depending on the observer\'s frame of reference.\n\nThe first part of the theory, that the laws of physics are the same everywhere, is known as the \"princi"
     _lowerCamelCase   :				Tuple							=     "Simply put, the theory of relativity states that "
     _lowerCamelCase   :				Tuple							=     LlamaTokenizer.from_pretrained("meta-llama/Llama-2-13b-chat-hf"    )
     _lowerCamelCase   :				List[str]							=     tokenizer.encode(__lowerCAmelCase							,return_tensors="pt"    )
     _lowerCamelCase   :				Optional[Any]							=     LlamaForCausalLM.from_pretrained(
         "meta-llama/Llama-2-13b-chat-hf"							,device_map="sequential"							,use_safetensors=__lowerCAmelCase    )

     # greedy generation outputs
     _lowerCamelCase   :				Optional[Any]							=     model.generate(__lowerCAmelCase							,max_new_tokens=64							,top_p=__lowerCAmelCase							,temperature=1							,do_sample=__lowerCAmelCase    )
     _lowerCamelCase   :				Union[str, Any]							=     tokenizer.decode(generated_ids[0]							,skip_special_tokens=__lowerCAmelCase    )
     self.assertEqual(__lowerCAmelCase							,__lowerCAmelCase    )

import gc
import random
import tempfile
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMInverseScheduler,
    DDIMScheduler,
    DPMSolverMultistepInverseScheduler,
    DPMSolverMultistepScheduler,
    StableDiffusionDiffEditPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import load_image, slow
from diffusers.utils.testing_utils import enable_full_determinism, floats_tensor, require_torch_gpu, torch_device

from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()






class     UpperCAmelCase						(		A_			,A_			,unittest.TestCase					):
   A__   :						Dict			=	StableDiffusionDiffEditPipeline
   A__   :						str			=	TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"height", "width", "image"} | {"image_latents"}
   A__   :						List[Any]			=	TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS - {"image"} | {"image_latents"}
   A__   :						Optional[Any]			=	frozenset(
       []					)  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
   A__   :						int			=	frozenset([]					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   Tuple					)						->      Optional[Any]:


          '''simple docstring'''


          torch.manual_seed(0					)
          snake_case			:  Dict     							= UNetaDConditionModel(
              block_out_channels=(32, 64)       ,		layers_per_block=2       ,		sample_size=32       ,		in_channels=4       ,		out_channels=4       ,		down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")       ,		up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")       ,		cross_attention_dim=32       ,		attention_head_dim=(2, 4)       ,		use_linear_projection=snake_case__       ,		)
          snake_case			:  str     							= DDIMScheduler(
              beta_start=0.00085       ,		beta_end=0.012       ,		beta_schedule="scaled_linear"       ,		clip_sample=snake_case__       ,		set_alpha_to_one=snake_case__       ,		)
          snake_case			:  List[Any]     							= DDIMInverseScheduler(
              beta_start=0.00085       ,		beta_end=0.012       ,		beta_schedule="scaled_linear"       ,		clip_sample=snake_case__       ,		set_alpha_to_zero=snake_case__       ,		)
          torch.manual_seed(0					)
          snake_case			:  Any     							= AutoencoderKL(
              block_out_channels=[32, 64]       ,		in_channels=3       ,		out_channels=3       ,		down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]       ,		up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]       ,		latent_channels=4       ,		sample_size=1_28       ,		)
          torch.manual_seed(0					)
          snake_case			:  Union[str, Any]     							= CLIPTextConfig(
              bos_token_id=0       ,		eos_token_id=2       ,		hidden_size=32       ,		intermediate_size=37       ,		layer_norm_eps=1e-05       ,		num_attention_heads=4       ,		num_hidden_layers=5       ,		pad_token_id=1       ,		vocab_size=10_00       ,		hidden_act="gelu"       ,		projection_dim=5_12       ,		)
          snake_case			:  str     							= CLIPTextModel(snake_case__					)
          snake_case			:  Optional[Any]     							= CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"					)

          snake_case			:  List[str]     							= {
              "unet": unet,
              "scheduler": scheduler,
              "inverse_scheduler": inverse_scheduler,
              "vae": vae,
              "text_encoder": text_encoder,
              "tokenizer": tokenizer,
              "safety_checker": None,
              "feature_extractor": None,
          }

          return components



   def 							_SCREAMING_SNAKE_CASE  (self						:   List[str]       ,		snake_case__						:   Dict       ,		snake_case__						:   Union[str, Any]=0					)						->      Optional[Any]:


          '''simple docstring'''


          snake_case			:  List[Any]     							= floats_tensor((1, 16, 16)       ,		rng=random.Random(snake_case__					)					).to(snake_case__					)
          snake_case			:  List[Any]     							= floats_tensor((1, 2, 4, 16, 16)       ,		rng=random.Random(snake_case__					)					).to(snake_case__					)
          if str(snake_case__					).startswith("mps"					):
                 snake_case			:  Any     							= torch.manual_seed(snake_case__					)
          else:
                 snake_case			:  Optional[Any]     							= torch.Generator(device=snake_case__					).manual_seed(snake_case__					)
          snake_case			:  Union[str, Any]     							= {
              "prompt": "a dog and a newt",
              "mask_image": mask,
              "image_latents": latents,
              "generator": generator,
              "num_inference_steps": 2,
              "inpaint_strength": 1.0,
              "guidance_scale": 6.0,
              "output_type": "numpy",
          }

          return inputs



   def 							_SCREAMING_SNAKE_CASE  (self						:   Tuple       ,		snake_case__						:   Optional[int]       ,		snake_case__						:   Optional[int]=0					)						->      Tuple:


          '''simple docstring'''


          snake_case			:  Tuple     							= floats_tensor((1, 3, 32, 32)       ,		rng=random.Random(snake_case__					)					).to(snake_case__					)
          snake_case			:  Optional[Any]     							= image.cpu().permute(0       ,		2       ,		3       ,		1					)[0]
          snake_case			:  Any     							= Image.fromarray(np.uinta(snake_case__					)					).convert("RGB"					)
          if str(snake_case__					).startswith("mps"					):
                 snake_case			:  int     							= torch.manual_seed(snake_case__					)
          else:
                 snake_case			:  Optional[int]     							= torch.Generator(device=snake_case__					).manual_seed(snake_case__					)
          snake_case			:  Any     							= {
              "image": image,
              "source_prompt": "a cat and a frog",
              "target_prompt": "a dog and a newt",
              "generator": generator,
              "num_inference_steps": 2,
              "num_maps_per_mask": 2,
              "mask_encode_strength": 1.0,
              "guidance_scale": 6.0,
              "output_type": "numpy",
          }

          return inputs



   def 							_SCREAMING_SNAKE_CASE  (self						:   Any       ,		snake_case__						:   Optional[Any]       ,		snake_case__						:   Tuple=0					)						->      Any:


          '''simple docstring'''


          snake_case			:  Optional[Any]     							= floats_tensor((1, 3, 32, 32)       ,		rng=random.Random(snake_case__					)					).to(snake_case__					)
          snake_case			:  Tuple     							= image.cpu().permute(0       ,		2       ,		3       ,		1					)[0]
          snake_case			:  List[Any]     							= Image.fromarray(np.uinta(snake_case__					)					).convert("RGB"					)
          if str(snake_case__					).startswith("mps"					):
                 snake_case			:  str     							= torch.manual_seed(snake_case__					)
          else:
                 snake_case			:  int     							= torch.Generator(device=snake_case__					).manual_seed(snake_case__					)
          snake_case			:  List[str]     							= {
              "image": image,
              "prompt": "a cat and a frog",
              "generator": generator,
              "num_inference_steps": 2,
              "inpaint_strength": 1.0,
              "guidance_scale": 6.0,
              "decode_latents": True,
              "output_type": "numpy",
          }
          return inputs



   def 							_SCREAMING_SNAKE_CASE  (self						:   List[str]					)						->      str:


          '''simple docstring'''


          if not hasattr(self.pipeline_class       ,		"_optional_components"					):
                 return

          snake_case			:  int     							= self.get_dummy_components()
          snake_case			:  Optional[int]     							= self.pipeline_class(**snake_case__					)
          pipe.to(snake_case__					)
          pipe.set_progress_bar_config(disable=snake_case__					)

          # set all optional components to None and update pipeline config accordingly
          for optional_component in pipe._optional_components:
                 setattr(snake_case__       ,		snake_case__       ,		snake_case__					)
          pipe.register_modules(**{optional_component: None for optional_component in pipe._optional_components}					)

          snake_case			:  Dict     							= self.get_dummy_inputs(snake_case__					)
          snake_case			:  int     							= pipe(**snake_case__					)[0]

          with tempfile.TemporaryDirectory() as tmpdir:
                 pipe.save_pretrained(snake_case__					)
                 snake_case			:  Any     							= self.pipeline_class.from_pretrained(snake_case__					)
                 pipe_loaded.to(snake_case__					)
                 pipe_loaded.set_progress_bar_config(disable=snake_case__					)

          for optional_component in pipe._optional_components:
                 self.assertTrue(
                     getattr(snake_case__       ,		snake_case__					) is None       ,		f"""`{optional_component}` did not stay set to None after loading."""       ,		)

          snake_case			:  Optional[int]     							= self.get_dummy_inputs(snake_case__					)
          snake_case			:  int     							= pipe_loaded(**snake_case__					)[0]

          snake_case			:  int     							= np.abs(output - output_loaded					).max()
          self.assertLess(snake_case__       ,		1e-4					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   str					)						->      int:


          '''simple docstring'''


          snake_case			:  Union[str, Any]     							= "cpu"

          snake_case			:  int     							= self.get_dummy_components()
          snake_case			:  Any     							= self.pipeline_class(**snake_case__					)
          pipe.to(snake_case__					)
          pipe.set_progress_bar_config(disable=snake_case__					)

          snake_case			:  Any     							= self.get_dummy_mask_inputs(snake_case__					)
          snake_case			:  Union[str, Any]     							= pipe.generate_mask(**snake_case__					)
          snake_case			:  List[Any]     							= mask[0, -3:, -3:]

          self.assertEqual(mask.shape       ,		(1, 16, 16)					)
          snake_case			:  int     							= np.array([0] * 9					)
          snake_case			:  str     							= np.abs(mask_slice.flatten() - expected_slice					).max()
          self.assertLessEqual(snake_case__       ,		1e-3					)
          self.assertEqual(mask[0, -3, -4]       ,		0					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   str					)						->      str:


          '''simple docstring'''


          snake_case			:  Any     							= "cpu"

          snake_case			:  List[Any]     							= self.get_dummy_components()
          snake_case			:  Optional[int]     							= self.pipeline_class(**snake_case__					)
          pipe.to(snake_case__					)
          pipe.set_progress_bar_config(disable=snake_case__					)

          snake_case			:  List[str]     							= self.get_dummy_inversion_inputs(snake_case__					)
          snake_case			:  List[str]     							= pipe.invert(**snake_case__					).images
          snake_case			:  Optional[Any]     							= image[0, -1, -3:, -3:]

          self.assertEqual(image.shape       ,		(2, 32, 32, 3)					)
          snake_case			:  int     							= np.array(
              [0.5150, 0.5134, 0.5043, 0.5376, 0.4694, 0.51050, 0.5015, 0.4407, 0.4799]       ,		)
          snake_case			:  Optional[Any]     							= np.abs(image_slice.flatten() - expected_slice					).max()
          self.assertLessEqual(snake_case__       ,		1e-3					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   int					)						->      Any:


          '''simple docstring'''


          super().test_inference_batch_single_identical(expected_max_diff=5e-3					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   str					)						->      Any:


          '''simple docstring'''


          snake_case			:  Any     							= "cpu"

          snake_case			:  int     							= self.get_dummy_components()

          snake_case			:  Tuple     							= {"beta_start": 0.00085, "beta_end": 0.012, "beta_schedule": "scaled_linear"}
          snake_case			:  int     							= DPMSolverMultistepScheduler(**snake_case__					)
          snake_case			:  Optional[Any]     							= DPMSolverMultistepInverseScheduler(**snake_case__					)

          snake_case			:  Any     							= self.pipeline_class(**snake_case__					)
          pipe.to(snake_case__					)
          pipe.set_progress_bar_config(disable=snake_case__					)

          snake_case			:  Tuple     							= self.get_dummy_inversion_inputs(snake_case__					)
          snake_case			:  Optional[int]     							= pipe.invert(**snake_case__					).images
          snake_case			:  Optional[Any]     							= image[0, -1, -3:, -3:]

          self.assertEqual(image.shape       ,		(2, 32, 32, 3)					)
          snake_case			:  int     							= np.array(
              [0.5150, 0.5134, 0.5043, 0.5376, 0.4694, 0.51050, 0.5015, 0.4407, 0.4799]       ,		)
          snake_case			:  Any     							= np.abs(image_slice.flatten() - expected_slice					).max()
          self.assertLessEqual(snake_case__       ,		1e-3					)






@require_torch_gpu
@slow
class     UpperCAmelCase						(		unittest.TestCase					):



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


          '''simple docstring'''


          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()



   @classmethod
   def 							_SCREAMING_SNAKE_CASE  (cls						:   Optional[int]					)						->      Optional[int]:


          '''simple docstring'''


          snake_case			:  Tuple     							= load_image(
              "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/diffedit/fruit.png"					)

          snake_case			:  Tuple     							= raw_image.convert("RGB"					).resize((7_68, 7_68)					)

          snake_case			:  Tuple     							= raw_image



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


          '''simple docstring'''


          snake_case			:  Optional[int]     							= torch.manual_seed(0					)

          snake_case			:  List[str]     							= StableDiffusionDiffEditPipeline.from_pretrained(
              "stabilityai/stable-diffusion-2-1"       ,		safety_checker=snake_case__       ,		torch_dtype=torch.floataa					)
          snake_case			:  int     							= DDIMScheduler.from_config(pipe.scheduler.config					)
          snake_case			:  Dict     							= DDIMInverseScheduler.from_config(pipe.scheduler.config					)
          pipe.enable_model_cpu_offload()
          pipe.set_progress_bar_config(disable=snake_case__					)

          snake_case			:  Union[str, Any]     							= "a bowl of fruit"
          snake_case			:  int     							= "a bowl of pears"

          snake_case			:  List[Any]     							= pipe.generate_mask(
              image=self.raw_image       ,		source_prompt=snake_case__       ,		target_prompt=snake_case__       ,		generator=snake_case__       ,		)

          snake_case			:  Optional[int]     							= pipe.invert(
              prompt=snake_case__       ,		image=self.raw_image       ,		inpaint_strength=0.7       ,		generator=snake_case__					).latents

          snake_case			:  List[str]     							= pipe(
              prompt=snake_case__       ,		mask_image=snake_case__       ,		image_latents=snake_case__       ,		generator=snake_case__       ,		negative_prompt=snake_case__       ,		inpaint_strength=0.7       ,		output_type="numpy"       ,		).images[0]

          snake_case			:  Dict     							= (
              np.array(
                  load_image(
                      "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
                      "/diffedit/pears.png"					).resize((7_68, 7_68)					)					)
              / 2_55
          )
          assert np.abs((expected_image - image).max()					) < 5e-1



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


          '''simple docstring'''


          snake_case			:  int     							= torch.manual_seed(0					)

          snake_case			:  Tuple     							= StableDiffusionDiffEditPipeline.from_pretrained(
              "stabilityai/stable-diffusion-2-1"       ,		safety_checker=snake_case__       ,		torch_dtype=torch.floataa					)
          snake_case			:  Union[str, Any]     							= DPMSolverMultistepScheduler.from_config(pipe.scheduler.config					)
          snake_case			:  Optional[int]     							= DPMSolverMultistepInverseScheduler.from_config(pipe.scheduler.config					)
          pipe.enable_model_cpu_offload()
          pipe.set_progress_bar_config(disable=snake_case__					)

          snake_case			:  str     							= "a bowl of fruit"
          snake_case			:  Optional[int]     							= "a bowl of pears"

          snake_case			:  Optional[Any]     							= pipe.generate_mask(
              image=self.raw_image       ,		source_prompt=snake_case__       ,		target_prompt=snake_case__       ,		generator=snake_case__       ,		)

          snake_case			:  List[str]     							= pipe.invert(
              prompt=snake_case__       ,		image=self.raw_image       ,		inpaint_strength=0.7       ,		generator=snake_case__       ,		num_inference_steps=25       ,		).latents

          snake_case			:  Dict     							= pipe(
              prompt=snake_case__       ,		mask_image=snake_case__       ,		image_latents=snake_case__       ,		generator=snake_case__       ,		negative_prompt=snake_case__       ,		inpaint_strength=0.7       ,		num_inference_steps=25       ,		output_type="numpy"       ,		).images[0]

          snake_case			:  int     							= (
              np.array(
                  load_image(
                      "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
                      "/diffedit/pears.png"					).resize((7_68, 7_68)					)					)
              / 2_55
          )
          assert np.abs((expected_image - image).max()					) < 5e-1





