

import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    EulerAncestralDiscreteScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPanoramaPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()







@skip_mps
class 						__magic_name__			(							lowerCamelCase__		,       lowerCamelCase__		,       unittest.TestCase							):

   '''simple docstring'''




   lowerCamelCase__ :	List[Any]    =   StableDiffusionPanoramaPipeline
   lowerCamelCase__ :	Optional[int]    =   TEXT_TO_IMAGE_PARAMS
   lowerCamelCase__ :	Optional[int]    =   TEXT_TO_IMAGE_BATCH_PARAMS
   lowerCamelCase__ :	Tuple    =   TEXT_TO_IMAGE_IMAGE_PARAMS
   lowerCamelCase__ :	int    =   TEXT_TO_IMAGE_IMAGE_PARAMS



   def  _UpperCAmelCase (				self       )							->    Any:






     """simple docstring"""






     torch.manual_seed(0       )
     a__  =UNetaDConditionModel(
         block_out_channels=(32, 64),    layers_per_block=1,    sample_size=32,    in_channels=4,    out_channels=4,    down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D'''),    up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D'''),    cross_attention_dim=32,    )
     a__  =DDIMScheduler()
     torch.manual_seed(0       )
     a__  =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,    )
     torch.manual_seed(0       )
     a__  =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=1000,    )
     a__  =CLIPTextModel(lowercase_       )
     a__  =CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''       )

     a__  ={
         '''unet''': unet,
         '''scheduler''': scheduler,
         '''vae''': vae,
         '''text_encoder''': text_encoder,
         '''tokenizer''': tokenizer,
         '''safety_checker''': None,
         '''feature_extractor''': None,
     }
     return components



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_=0       )							->    int:






     """simple docstring"""






     a__  =torch.manual_seed(lowercase_       )
     a__  ={
         '''prompt''': '''a photo of the dolomites''',
         '''generator''': generator,
         # Setting height and width to None to prevent OOMs on CPU.
         '''height''': None,
         '''width''': None,
         '''num_inference_steps''': 1,
         '''guidance_scale''': 6.0,
         '''output_type''': '''numpy''',
     }
     return inputs



   def  _UpperCAmelCase (				self       )							->    str:






     """simple docstring"""






     a__  ='''cpu'''  # ensure determinism for the device-dependent torch.Generator
     a__  =self.get_dummy_components()
     a__  =StableDiffusionPanoramaPipeline(**lowercase_       )
     a__  =sd_pipe.to(lowercase_       )
     sd_pipe.set_progress_bar_config(disable=lowercase_       )

     a__  =self.get_dummy_inputs(lowercase_       )
     a__  =sd_pipe(**lowercase_       ).images
     a__  =image[0, -3:, -3:, -1]
     assert image.shape == (1, 64, 64, 3)

     a__  =np.array([0.6186, 0.5374, 0.4915, 0.4135, 0.4114, 0.4563, 0.5128, 0.4977, 0.4757]       )

     assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1E-2



   def  _UpperCAmelCase (				self       )							->    List[Any]:






     """simple docstring"""






     super().test_inference_batch_consistent(batch_sizes=[1, 2]       )



   def  _UpperCAmelCase (				self       )							->    str:






     """simple docstring"""






     super().test_inference_batch_single_identical(batch_size=2,    expected_max_diff=3.25E-3       )



   def  _UpperCAmelCase (				self       )							->    Tuple:






     """simple docstring"""






     a__  ='''cpu'''  # ensure determinism for the device-dependent torch.Generator
     a__  =self.get_dummy_components()
     a__  =StableDiffusionPanoramaPipeline(**lowercase_       )
     a__  =sd_pipe.to(lowercase_       )
     sd_pipe.set_progress_bar_config(disable=lowercase_       )

     a__  =self.get_dummy_inputs(lowercase_       )
     a__  ='''french fries'''
     a__  =sd_pipe(**lowercase_,    negative_prompt=lowercase_       )
     a__  =output.images
     a__  =image[0, -3:, -3:, -1]

     assert image.shape == (1, 64, 64, 3)

     a__  =np.array([0.6187, 0.5375, 0.4915, 0.4136, 0.4114, 0.4563, 0.5128, 0.4976, 0.4757]       )

     assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1E-2



   def  _UpperCAmelCase (				self       )							->    Optional[Any]:






     """simple docstring"""






     a__  ='''cpu'''  # ensure determinism for the device-dependent torch.Generator
     a__  =self.get_dummy_components()
     a__  =StableDiffusionPanoramaPipeline(**lowercase_       )
     a__  =sd_pipe.to(lowercase_       )
     sd_pipe.set_progress_bar_config(disable=lowercase_       )

     a__  =self.get_dummy_inputs(lowercase_       )
     a__  =sd_pipe(**lowercase_,    view_batch_size=2       )
     a__  =output.images
     a__  =image[0, -3:, -3:, -1]

     assert image.shape == (1, 64, 64, 3)

     a__  =np.array([0.6187, 0.5375, 0.4915, 0.4136, 0.4114, 0.4563, 0.5128, 0.4976, 0.4757]       )

     assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1E-2



   def  _UpperCAmelCase (				self       )							->    Any:






     """simple docstring"""






     a__  ='''cpu'''  # ensure determinism for the device-dependent torch.Generator
     a__  =self.get_dummy_components()
     a__  =EulerAncestralDiscreteScheduler(
         beta_start=0.00085,    beta_end=0.012,    beta_schedule='''scaled_linear'''       )
     a__  =StableDiffusionPanoramaPipeline(**lowercase_       )
     a__  =sd_pipe.to(lowercase_       )
     sd_pipe.set_progress_bar_config(disable=lowercase_       )

     a__  =self.get_dummy_inputs(lowercase_       )
     a__  =sd_pipe(**lowercase_       ).images
     a__  =image[0, -3:, -3:, -1]

     assert image.shape == (1, 64, 64, 3)

     a__  =np.array([0.4024, 0.6510, 0.4901, 0.5378, 0.5813, 0.5622, 0.4795, 0.4467, 0.4952]       )

     assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1E-2



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






     """simple docstring"""






     a__  ='''cpu'''  # ensure determinism for the device-dependent torch.Generator
     a__  =self.get_dummy_components()
     a__  =PNDMScheduler(
         beta_start=0.00085,    beta_end=0.012,    beta_schedule='''scaled_linear''',    skip_prk_steps=lowercase_       )
     a__  =StableDiffusionPanoramaPipeline(**lowercase_       )
     a__  =sd_pipe.to(lowercase_       )
     sd_pipe.set_progress_bar_config(disable=lowercase_       )

     a__  =self.get_dummy_inputs(lowercase_       )
     a__  =sd_pipe(**lowercase_       ).images
     a__  =image[0, -3:, -3:, -1]

     assert image.shape == (1, 64, 64, 3)

     a__  =np.array([0.6391, 0.6291, 0.4861, 0.5134, 0.5552, 0.4578, 0.5032, 0.5023, 0.4539]       )

     assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1E-2







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

   '''simple docstring'''




   def  _UpperCAmelCase (				self       )							->    Any:






     """simple docstring"""






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



   def  _UpperCAmelCase (				self,    lowercase_=0       )							->    Dict:






     """simple docstring"""






     a__  =torch.manual_seed(lowercase_       )
     a__  ={
         '''prompt''': '''a photo of the dolomites''',
         '''generator''': generator,
         '''num_inference_steps''': 3,
         '''guidance_scale''': 7.5,
         '''output_type''': '''numpy''',
     }
     return inputs



   def  _UpperCAmelCase (				self       )							->    Dict:






     """simple docstring"""






     a__  ='''stabilityai/stable-diffusion-2-base'''
     a__  =DDIMScheduler.from_pretrained(lowercase_,    subfolder='''scheduler'''       )
     a__  =StableDiffusionPanoramaPipeline.from_pretrained(lowercase_,    scheduler=lowercase_,    safety_checker=lowercase_       )
     pipe.to(lowercase_       )
     pipe.set_progress_bar_config(disable=lowercase_       )
     pipe.enable_attention_slicing()

     a__  =self.get_inputs()
     a__  =pipe(**lowercase_       ).images
     a__  =image[0, -3:, -3:, -1].flatten()

     assert image.shape == (1, 512, 2048, 3)

     a__  =np.array(
         [
             0.36968392,
             0.27025372,
             0.32446766,
             0.28379387,
             0.36363274,
             0.30733347,
             0.27100027,
             0.27054125,
             0.25536096,
         ]       )

     assert np.abs(expected_slice - image_slice       ).max() < 1E-2



   def  _UpperCAmelCase (				self       )							->    Optional[int]:






     """simple docstring"""






     a__  =StableDiffusionPanoramaPipeline.from_pretrained(
         '''stabilityai/stable-diffusion-2-base''',    safety_checker=lowercase_       )
     a__  =LMSDiscreteScheduler.from_config(pipe.scheduler.config       )
     pipe.to(lowercase_       )
     pipe.set_progress_bar_config(disable=lowercase_       )
     pipe.enable_attention_slicing()

     a__  =self.get_inputs()
     a__  =pipe(**lowercase_       ).images
     a__  =image[0, -3:, -3:, -1].flatten()

     assert image.shape == (1, 512, 2048, 3)

     a__  =np.array(
         [
             [
                 0.0,
                 0.0,
                 0.0,
                 0.0,
                 0.0,
                 0.0,
                 0.0,
                 0.0,
                 0.0,
             ]
         ]       )

     assert np.abs(expected_slice - image_slice       ).max() < 1E-3



   def  _UpperCAmelCase (				self       )							->    Any:






     """simple docstring"""






     a__  =0

     def callback_fn(lowercase_,    lowercase_,    lowercase_       ) -> None:
       a__  =True
       nonlocal number_of_steps
       number_of_steps += 1
       if step == 1:
         a__  =latents.detach().cpu().numpy()
         assert latents.shape == (1, 4, 64, 256)
         a__  =latents[0, -3:, -3:, -1]

         a__  =np.array(
             [
                 0.18681869,
                 0.33907816,
                 0.5361276,
                 0.14432865,
                 -0.02856611,
                 -0.73941123,
                 0.23397987,
                 0.47322682,
                 -0.37823164,
             ]       )
         assert np.abs(latents_slice.flatten() - expected_slice       ).max() < 5E-2
       elif step == 2:
         a__  =latents.detach().cpu().numpy()
         assert latents.shape == (1, 4, 64, 256)
         a__  =latents[0, -3:, -3:, -1]

         a__  =np.array(
             [
                 0.18539645,
                 0.33987248,
                 0.5378559,
                 0.14437142,
                 -0.02455261,
                 -0.7338317,
                 0.23990755,
                 0.47356272,
                 -0.3786505,
             ]       )

         assert np.abs(latents_slice.flatten() - expected_slice       ).max() < 5E-2

     a__  =False

     a__  ='''stabilityai/stable-diffusion-2-base'''
     a__  =DDIMScheduler.from_pretrained(lowercase_,    subfolder='''scheduler'''       )
     a__  =StableDiffusionPanoramaPipeline.from_pretrained(lowercase_,    scheduler=lowercase_,    safety_checker=lowercase_       )
     a__  =pipe.to(lowercase_       )
     pipe.set_progress_bar_config(disable=lowercase_       )
     pipe.enable_attention_slicing()

     a__  =self.get_inputs()
     pipe(**lowercase_,    callback=lowercase_,    callback_steps=1       )
     assert callback_fn.has_been_called
     assert number_of_steps == 3



   def  _UpperCAmelCase (				self       )							->    Tuple:






     """simple docstring"""






     torch.cuda.empty_cache()
     torch.cuda.reset_max_memory_allocated()
     torch.cuda.reset_peak_memory_stats()

     a__  ='''stabilityai/stable-diffusion-2-base'''
     a__  =DDIMScheduler.from_pretrained(lowercase_,    subfolder='''scheduler'''       )
     a__  =StableDiffusionPanoramaPipeline.from_pretrained(lowercase_,    scheduler=lowercase_,    safety_checker=lowercase_       )
     a__  =pipe.to(lowercase_       )
     pipe.set_progress_bar_config(disable=lowercase_       )
     pipe.enable_attention_slicing(1       )
     pipe.enable_sequential_cpu_offload()

     a__  =self.get_inputs()
     a__  =pipe(**lowercase_       )

     a__  =torch.cuda.max_memory_allocated()
     # make sure that less than 5.2 GB is allocated
     assert mem_bytes < 5.5 * 10**9



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 						__magic_name__			:

   '''simple docstring'''




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






     """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  _UpperCAmelCase (				self       )							->    Optional[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  _UpperCAmelCase (				self       )							->    List[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=lowercase_,    initializer_range=self.initializer_range,    use_stable_embedding=lowercase_,    )



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_       )							->    List[str]:






     """simple docstring"""






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



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    )							->    Any:






     """simple docstring"""






     a__  =True
     a__  =OpenLlamaModel(lowercase_       )
     model.to(lowercase_       )
     model.eval()
     a__  =model(
         lowercase_,    attention_mask=lowercase_,    encoder_hidden_states=lowercase_,    encoder_attention_mask=lowercase_,    )
     a__  =model(
         lowercase_,    attention_mask=lowercase_,    encoder_hidden_states=lowercase_,    )
     a__  =model(lowercase_,    attention_mask=lowercase_       )
     self.parent.assertEqual(result.last_hidden_state.shape,    (self.batch_size, self.seq_length, self.hidden_size)       )



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    )							->    List[str]:






     """simple docstring"""






     a__  =OpenLlamaForCausalLM(config=lowercase_       )
     model.to(lowercase_       )
     model.eval()
     a__  =model(lowercase_,    attention_mask=lowercase_,    labels=lowercase_       )
     self.parent.assertEqual(result.logits.shape,    (self.batch_size, self.seq_length, self.vocab_size)       )



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    lowercase_,    )							->    List[Any]:






     """simple docstring"""






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

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



   def  _UpperCAmelCase (				self       )							->    Optional[int]:






     """simple docstring"""






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







@require_torch
class 						__magic_name__			(							lowerCamelCase__		,       lowerCamelCase__		,       lowerCamelCase__		,       unittest.TestCase							):

   '''simple docstring'''




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



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






     """simple docstring"""






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



   def  _UpperCAmelCase (				self       )							->    str:






     """simple docstring"""






     self.config_tester.run_common_tests()



   def  _UpperCAmelCase (				self       )							->    Optional[Any]:






     """simple docstring"""






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



   def  _UpperCAmelCase (				self       )							->    str:






     """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(*lowercase_       )



   def  _UpperCAmelCase (				self       )							->    int:






     """simple docstring"""






     a__,      a__  =self.model_tester.prepare_config_and_inputs_for_common()
     a__  =3
     a__  =input_dict['''input_ids''']
     a__  =input_ids.ne(1       ).to(lowercase_       )
     a__  =ids_tensor([self.model_tester.batch_size],    self.model_tester.type_sequence_label_size       )
     a__  =OpenLlamaForSequenceClassification(lowercase_       )
     model.to(lowercase_       )
     model.eval()
     a__  =model(lowercase_,    attention_mask=lowercase_,    labels=lowercase_       )
     self.assertEqual(result.logits.shape,    (self.model_tester.batch_size, self.model_tester.num_labels)       )



   def  _UpperCAmelCase (				self       )							->    Any:






     """simple docstring"""






     a__,      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(lowercase_       )
     a__  =ids_tensor([self.model_tester.batch_size],    self.model_tester.type_sequence_label_size       )
     a__  =OpenLlamaForSequenceClassification(lowercase_       )
     model.to(lowercase_       )
     model.eval()
     a__  =model(lowercase_,    attention_mask=lowercase_,    labels=lowercase_       )
     self.assertEqual(result.logits.shape,    (self.model_tester.batch_size, self.model_tester.num_labels)       )



   def  _UpperCAmelCase (				self       )							->    Optional[int]:






     """simple docstring"""






     a__,      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(lowercase_       )
     a__  =ids_tensor(
         [self.model_tester.batch_size, config.num_labels],    self.model_tester.type_sequence_label_size       ).to(torch.float       )
     a__  =OpenLlamaForSequenceClassification(lowercase_       )
     model.to(lowercase_       )
     model.eval()
     a__  =model(lowercase_,    attention_mask=lowercase_,    labels=lowercase_       )
     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       )							->    List[str]:






     """simple docstring"""






     pass



   @parameterized.expand([('''linear''',), ('''dynamic''',)]       )
   def  _UpperCAmelCase (				self,    lowercase_       )							->    Optional[Any]:






     """simple docstring"""






     a__,      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(lowercase_       )
     original_model.to(lowercase_       )
     original_model.eval()
     a__  =original_model(lowercase_       ).last_hidden_state
     a__  =original_model(lowercase_       ).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(lowercase_       )
     scaled_model.to(lowercase_       )
     scaled_model.eval()
     a__  =scaled_model(lowercase_       ).last_hidden_state
     a__  =scaled_model(lowercase_       ).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(lowercase_,    lowercase_,    atol=1E-5       )       )
     else:
       self.assertFalse(torch.allclose(lowercase_,    lowercase_,    atol=1E-5       )       )

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