


"""simple docstring"""





import gc
import random
import unittest

import numpy as np
import torch

from diffusers import DDIMScheduler, KandinskyVaaPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel
from diffusers.utils import floats_tensor, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()

class 			lowercase_	(			UpperCAmelCase_				,				unittest.TestCase    ):
      __magic_name__  :				int 							=					KandinskyVaaPipeline
      __magic_name__  :				Union[str, Any] 							=					[
          '''image_embeds''',
          '''negative_image_embeds''',
      ]
      __magic_name__  :				int 							=					['''image_embeds''', '''negative_image_embeds''']
      __magic_name__  :				str 							=					[
          '''generator''',
          '''height''',
          '''width''',
          '''latents''',
          '''guidance_scale''',
          '''num_inference_steps''',
          '''return_dict''',
          '''guidance_scale''',
          '''num_images_per_prompt''',
          '''output_type''',
          '''return_dict''',
      ]
      __magic_name__  :				str 							=					False







      @property
      def 					_lowerCAmelCase							(   self			:				int     ):
        return 3_2







      @property
      def 					_lowerCAmelCase							(   self			:				str     ):
        return 3_2







      @property
      def 					_lowerCAmelCase							(   self			:				Optional[int]     ):
        return self.time_input_dim







      @property
      def 					_lowerCAmelCase							(   self			:				Optional[int]     ):
        return self.time_input_dim * 4







      @property
      def 					_lowerCAmelCase							(   self			:				Optional[Any]     ):
        return 1_0_0







      @property
      def 					_lowerCAmelCase							(   self			:				str     ):
        torch.manual_seed(0     )

        lowerCAmelCase__     :     int										=       {
            """in_channels""": 4,
            # Out channels is double in channels because predicts mean and variance
            """out_channels""": 8,
            """addition_embed_type""": """image""",
            """down_block_types""": ("""ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D"""),
            """up_block_types""": ("""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""),
            """mid_block_type""": """UNetMidBlock2DSimpleCrossAttn""",
            """block_out_channels""": (self.block_out_channels_a, self.block_out_channels_a * 2),
            """layers_per_block""": 1,
            """encoder_hid_dim""": self.text_embedder_hidden_size,
            """encoder_hid_dim_type""": """image_proj""",
            """cross_attention_dim""": self.cross_attention_dim,
            """attention_head_dim""": 4,
            """resnet_time_scale_shift""": """scale_shift""",
            """class_embed_type""": None,
        }

        lowerCAmelCase__     :     Any										=       UNetaDConditionModel(**lowerCamelCase__     )
        return model







      @property
      def 					_lowerCAmelCase							(   self			:				Union[str, Any]     ):
        return {
            "block_out_channels": [3_2, 6_4],
            "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"],
            "in_channels": 3,
            "latent_channels": 4,
            "layers_per_block": 1,
            "norm_num_groups": 8,
            "norm_type": "spatial",
            "num_vq_embeddings": 1_2,
            "out_channels": 3,
            "up_block_types": [
                "AttnUpDecoderBlock2D",
                "UpDecoderBlock2D",
            ],
            "vq_embed_dim": 4,
        }







      @property
      def 					_lowerCAmelCase							(   self			:				int     ):
        torch.manual_seed(0     )
        lowerCAmelCase__     :     Tuple										=       VQModel(**self.dummy_movq_kwargs     )
        return model







      def 					_lowerCAmelCase							(   self			:				Tuple     ):
        lowerCAmelCase__     :     str										=       self.dummy_unet
        lowerCAmelCase__     :     Optional[int]										=       self.dummy_movq

        lowerCAmelCase__     :     List[str]										=       DDIMScheduler(
            num_train_timesteps=1_0_0_0		,	beta_schedule="linear"		,	beta_start=0.0_0085		,	beta_end=0.012		,	clip_sample=lowerCamelCase__		,	set_alpha_to_one=lowerCamelCase__		,	steps_offset=1		,	prediction_type="epsilon"		,	thresholding=lowerCamelCase__		,	)

        lowerCAmelCase__     :     str										=       {
            """unet""": unet,
            """scheduler""": scheduler,
            """movq""": movq,
        }
        return components







      def 					_lowerCAmelCase							(   self			:				Optional[int]		,	_lowercase			:				Optional[Any]		,	_lowercase			:				Any=0     ):
        lowerCAmelCase__     :     Tuple										=       floats_tensor((1, self.text_embedder_hidden_size)		,	rng=random.Random(lowerCamelCase__     )     ).to(lowerCamelCase__     )
        lowerCAmelCase__     :     Optional[int]										=       floats_tensor((1, self.text_embedder_hidden_size)		,	rng=random.Random(seed + 1     )     ).to(
            lowerCamelCase__     )
        if str(lowerCamelCase__     ).startswith("mps"     ):
          lowerCAmelCase__     :     Optional[int]										=       torch.manual_seed(lowerCamelCase__     )
        else:
          lowerCAmelCase__     :     int										=       torch.Generator(device=lowerCamelCase__     ).manual_seed(lowerCamelCase__     )
        lowerCAmelCase__     :     Dict										=       {
            """image_embeds""": image_embeds,
            """negative_image_embeds""": negative_image_embeds,
            """generator""": generator,
            """height""": 6_4,
            """width""": 6_4,
            """guidance_scale""": 4.0,
            """num_inference_steps""": 2,
            """output_type""": """np""",
        }
        return inputs







      def 					_lowerCAmelCase							(   self			:				Dict     ):
        lowerCAmelCase__     :     Dict										=       """cpu"""

        lowerCAmelCase__     :     Dict										=       self.get_dummy_components()

        lowerCAmelCase__     :     Union[str, Any]										=       self.pipeline_class(**lowerCamelCase__     )
        lowerCAmelCase__     :     int										=       pipe.to(lowerCamelCase__     )

        pipe.set_progress_bar_config(disable=lowerCamelCase__     )

        lowerCAmelCase__     :     List[Any]										=       pipe(**self.get_dummy_inputs(lowerCamelCase__     )     )
        lowerCAmelCase__     :     List[Any]										=       output.images

        lowerCAmelCase__     :     int										=       pipe(
            **self.get_dummy_inputs(lowerCamelCase__     )		,	return_dict=lowerCamelCase__		,	)[0]

        lowerCAmelCase__     :     List[str]										=       image[0, -3:, -3:, -1]
        lowerCAmelCase__     :     Tuple										=       image_from_tuple[0, -3:, -3:, -1]

        assert image.shape == (1, 6_4, 6_4, 3)

        lowerCAmelCase__     :     int										=       np.array(
            [0.623_7976, 1.0, 0.3644_1332, 1.0, 0.7063_9634, 0.2987_7186, 0.8565_2125, 0.521_6843, 0.5445_4046]     )

        assert (
            np.abs(image_slice.flatten() - expected_slice     ).max() < 1e-2
        ), f" expected_slice {expected_slice}, but got {image_slice.flatten()}"

        assert (
            np.abs(image_from_tuple_slice.flatten() - expected_slice     ).max() < 1e-2
        ), f" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}"

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







      def 					_lowerCAmelCase							(   self			:				Optional[int]     ):
        super().tearDown()
        gc.collect()
        torch.cuda.empty_cache()







      def 					_lowerCAmelCase							(   self			:				List[Any]     ):
        lowerCAmelCase__     :     int										=       load_numpy(
            "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
            "/kandinskyv22/kandinskyv22_text2img_cat_fp16.npy"     )

        lowerCAmelCase__     :     List[str]										=       KandinskyVaaPriorPipeline.from_pretrained(
            "kandinsky-community/kandinsky-2-2-prior"		,	torch_dtype=torch.floataa     )
        pipe_prior.to(lowerCamelCase__     )

        lowerCAmelCase__     :     int										=       KandinskyVaaPipeline.from_pretrained(
            "kandinsky-community/kandinsky-2-2-decoder"		,	torch_dtype=torch.floataa     )
        lowerCAmelCase__     :     Optional[int]										=       pipeline.to(lowerCamelCase__     )
        pipeline.set_progress_bar_config(disable=lowerCamelCase__     )

        lowerCAmelCase__     :     Optional[Any]										=       """red cat, 4k photo"""

        lowerCAmelCase__     :     Tuple										=       torch.Generator(device="cuda"     ).manual_seed(0     )
        lowerCAmelCase__     :     Union[str, Any]										=       pipe_prior(
            lowerCamelCase__		,	generator=lowerCamelCase__		,	num_inference_steps=5		,	negative_prompt=""		,	).to_tuple()

        lowerCAmelCase__     :     Any										=       torch.Generator(device="cuda"     ).manual_seed(0     )
        lowerCAmelCase__     :     Union[str, Any]										=       pipeline(
            image_embeds=lowerCamelCase__		,	negative_image_embeds=lowerCamelCase__		,	generator=lowerCamelCase__		,	num_inference_steps=1_0_0		,	output_type="np"		,	)

        lowerCAmelCase__     :     Any										=       output.images[0]

        assert image.shape == (5_1_2, 5_1_2, 3)

        assert_mean_pixel_difference(lowerCamelCase__		,	lowerCamelCase__     )








import unittest

from transformers import AutoTokenizer, FalconConfig, is_torch_available
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 (
           FalconForCausalLM,
           FalconForQuestionAnswering,
           FalconForSequenceClassification,
           FalconForTokenClassification,
           FalconModel,
       )







class 		_A						:

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

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

  def    a					(    self						:	List[str]			):
     """simple docstring"""

     __UpperCamelCase	:			Union[str, Any]					=					ids_tensor([self.batch_size, self.seq_length]   ,				self.vocab_size			)

     __UpperCamelCase	:			Tuple					=					None
     if self.use_input_mask:
        __UpperCamelCase	:			Optional[int]					=					random_attention_mask([self.batch_size, self.seq_length]			)

     __UpperCamelCase	:			Optional[Any]					=					None

     __UpperCamelCase	:			Optional[Any]					=					None
     __UpperCamelCase	:			List[Any]					=					None
     __UpperCamelCase	:			Dict					=					None
     if self.use_labels:
        __UpperCamelCase	:			List[Any]					=					ids_tensor([self.batch_size]   ,				self.type_sequence_label_size			)
        __UpperCamelCase	:			Tuple					=					ids_tensor([self.batch_size, self.seq_length]   ,				self.num_labels			)
        __UpperCamelCase	:			List[Any]					=					ids_tensor([self.batch_size]   ,				self.num_choices			)

     __UpperCamelCase	:			str					=					self.get_config()

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

  def    a					(    self						:	Dict			):
     """simple docstring"""

     return FalconConfig(
         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   ,				pad_token_id=1   ,				new_decoder_architecture=lowerCamelCase__   ,				)

  def    a					(    self						:	Dict   ,				lowerCamelCase__						:	List[Any]   ,				lowerCamelCase__						:	Union[str, Any]   ,				lowerCamelCase__						:	Optional[int]   ,				lowerCamelCase__						:	Any   ,				lowerCamelCase__						:	Dict   ,				lowerCamelCase__						:	Dict   ,				lowerCamelCase__						:	int			):
     """simple docstring"""

     __UpperCamelCase	:			str					=					FalconModel(config=lowerCamelCase__			)
     model.to(lowerCamelCase__			)
     model.eval()
     __UpperCamelCase	:			List[Any]					=					model(lowerCamelCase__   ,				attention_mask=lowerCamelCase__			)
     __UpperCamelCase	:			Tuple					=					model(lowerCamelCase__			)
     self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, self.seq_length, self.hidden_size)			)

  def    a					(    self						:	Any   ,				lowerCamelCase__						:	List[Any]   ,				lowerCamelCase__						:	Any   ,				lowerCamelCase__						:	Union[str, Any]   ,				lowerCamelCase__						:	Union[str, Any]   ,				lowerCamelCase__						:	Optional[Any]   ,				lowerCamelCase__						:	Union[str, Any]   ,				lowerCamelCase__						:	Dict   ,				lowerCamelCase__						:	List[Any]   ,				lowerCamelCase__						:	int   ,				):
     """simple docstring"""

     __UpperCamelCase	:			str					=					True
     __UpperCamelCase	:			List[str]					=					FalconModel(lowerCamelCase__			)
     model.to(lowerCamelCase__			)
     model.eval()
     __UpperCamelCase	:			Dict					=					model(
         lowerCamelCase__   ,				attention_mask=lowerCamelCase__   ,				encoder_hidden_states=lowerCamelCase__   ,				encoder_attention_mask=lowerCamelCase__   ,				)
     __UpperCamelCase	:			Any					=					model(
         lowerCamelCase__   ,				attention_mask=lowerCamelCase__   ,				encoder_hidden_states=lowerCamelCase__   ,				)
     __UpperCamelCase	:			Any					=					model(lowerCamelCase__   ,				attention_mask=lowerCamelCase__			)
     self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, self.seq_length, self.hidden_size)			)

  def    a					(    self						:	Dict   ,				lowerCamelCase__						:	List[str]   ,				lowerCamelCase__						:	Tuple   ,				lowerCamelCase__						:	Dict   ,				lowerCamelCase__						:	Dict   ,				lowerCamelCase__						:	str   ,				lowerCamelCase__						:	str   ,				lowerCamelCase__						:	Tuple   ,				lowerCamelCase__						:	Any   ,				lowerCamelCase__						:	int   ,				):
     """simple docstring"""

     __UpperCamelCase	:			int					=					FalconForCausalLM(config=lowerCamelCase__			)
     model.to(lowerCamelCase__			)
     model.eval()
     __UpperCamelCase	:			Optional[Any]					=					model(lowerCamelCase__   ,				attention_mask=lowerCamelCase__   ,				labels=lowerCamelCase__			)
     self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.seq_length, self.vocab_size)			)

  def    a					(    self						:	Optional[Any]   ,				lowerCamelCase__						:	Optional[int]   ,				lowerCamelCase__						:	Tuple   ,				lowerCamelCase__						:	Any   ,				lowerCamelCase__						:	str   ,				lowerCamelCase__						:	Union[str, Any]   ,				lowerCamelCase__						:	Dict   ,				lowerCamelCase__						:	Any   ,				lowerCamelCase__						:	int   ,				lowerCamelCase__						:	List[str]   ,				):
     """simple docstring"""

     __UpperCamelCase	:			List[str]					=					True
     __UpperCamelCase	:			List[Any]					=					True
     __UpperCamelCase	:			Dict					=					FalconForCausalLM(config=lowerCamelCase__			)
     model.to(lowerCamelCase__			)
     model.eval()

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

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

     # append to next input_ids and
     __UpperCamelCase	:			Tuple					=					torch.cat([input_ids, next_tokens]   ,				dim=-1			)
     __UpperCamelCase	:			Union[str, Any]					=					torch.cat([input_mask, next_mask]   ,				dim=-1			)

     __UpperCamelCase	:			Optional[int]					=					model(
         lowerCamelCase__   ,				attention_mask=lowerCamelCase__   ,				encoder_hidden_states=lowerCamelCase__   ,				encoder_attention_mask=lowerCamelCase__   ,				output_hidden_states=lowerCamelCase__   ,				)["""hidden_states"""][0]
     __UpperCamelCase	:			Union[str, Any]					=					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
     __UpperCamelCase	:			Dict					=					ids_tensor((1,)   ,				output_from_past.shape[-1]			).item()
     __UpperCamelCase	:			Dict					=					output_from_no_past[:, -3:, random_slice_idx].detach()
     __UpperCamelCase	:			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(lowerCamelCase__   ,				lowerCamelCase__   ,				atol=1e-3			)			)

  def    a					(    self						:	Union[str, Any]			):
     """simple docstring"""

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







@require_torch
class 		_A						( UpperCAmelCase_	,				UpperCAmelCase_	,				UpperCAmelCase_	,				unittest.TestCase ):
  lowercase_						:			Any						=					(
      (
          FalconModel,
          FalconForCausalLM,
          FalconForSequenceClassification,
          FalconForTokenClassification,
          FalconForQuestionAnswering,
      )
      if is_torch_available()
      else ()
  )
  lowercase_						:			List[str]						=					(FalconForCausalLM,) if is_torch_available() else ()
  lowercase_						:			Union[str, Any]						=					(
      {
          '''feature-extraction''': FalconModel,
          '''text-classification''': FalconForSequenceClassification,
          '''text-generation''': FalconForCausalLM,
          '''question-answering''': FalconForQuestionAnswering,
          '''token-classification''': FalconForTokenClassification,
          '''zero-shot''': FalconForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  lowercase_						:			Optional[int]						=					False
  lowercase_						:			Union[str, Any]						=					False

  def    a					(    self						:	List[str]			):
     """simple docstring"""

     __UpperCamelCase	:			Dict					=					FalconModelTester(self			)
     __UpperCamelCase	:			Dict					=					ConfigTester(self   ,				config_class=lowerCamelCase__   ,				hidden_size=37			)

  def    a					(    self						:	List[str]			):
     """simple docstring"""

     self.config_tester.run_common_tests()

  def    a					(    self						:	Union[str, Any]			):
     """simple docstring"""

     __UpperCamelCase	:			List[Any]					=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowerCamelCase__			)

  def    a					(    self						:	Optional[Any]			):
     """simple docstring"""

     __UpperCamelCase	,     *__UpperCamelCase	:			List[Any]					=					self.model_tester.prepare_config_and_inputs()
     for alibi in [True, False]:
        __UpperCamelCase	:			List[str]					=					alibi
        self.model_tester.create_and_check_model(lowerCamelCase__   ,				*lowerCamelCase__			)

  def    a					(    self						:	int			):
     """simple docstring"""

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

  def    a					(    self						:	Optional[Any]			):
     """simple docstring"""

     __UpperCamelCase	,     __UpperCamelCase	:			Any					=					self.model_tester.prepare_config_and_inputs_for_common()
     __UpperCamelCase	:			List[Any]					=					3
     __UpperCamelCase	:			List[Any]					=					"""single_label_classification"""
     __UpperCamelCase	:			Optional[Any]					=					input_dict["""input_ids"""]
     __UpperCamelCase	:			List[str]					=					input_ids.ne(1			).to(lowerCamelCase__			)
     __UpperCamelCase	:			Dict					=					ids_tensor([self.model_tester.batch_size]   ,				self.model_tester.type_sequence_label_size			)
     __UpperCamelCase	:			List[str]					=					FalconForSequenceClassification(lowerCamelCase__			)
     model.to(lowerCamelCase__			)
     model.eval()
     __UpperCamelCase	:			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    a					(    self						:	List[Any]			):
     """simple docstring"""

     __UpperCamelCase	,     __UpperCamelCase	:			List[Any]					=					self.model_tester.prepare_config_and_inputs_for_common()
     __UpperCamelCase	:			Any					=					input_dict["""input_ids"""]
     __UpperCamelCase	:			List[str]					=					FalconForCausalLM(lowerCamelCase__			)
     model.to(lowerCamelCase__			)
     model.eval()
     __UpperCamelCase	:			Optional[Any]					=					model(lowerCamelCase__   ,				use_cache=lowerCamelCase__			)
     __UpperCamelCase	:			Optional[int]					=					input_ids.shape[0]
     __UpperCamelCase	:			Dict					=					model._convert_to_rw_cache(result.past_key_values			)
     __UpperCamelCase	:			Optional[int]					=					model._convert_cache_to_standard_format(lowerCamelCase__   ,				lowerCamelCase__			)
     for layer in range(len(lowerCamelCase__			)			):
        for tensor_idx in range(2			):
           self.assertTrue(rw_cache[layer][tensor_idx].ndim == 3			)
           self.assertTrue(result.past_key_values[layer][tensor_idx].ndim == 4			)
           self.assertTrue(
               torch.all(result.past_key_values[layer][tensor_idx] == standard_cache[layer][tensor_idx]			)			)

  def    a					(    self						:	Tuple			):
     """simple docstring"""

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

  def    a					(    self						:	int			):
     """simple docstring"""

     for model_class in self.all_generative_model_classes:
        __UpperCamelCase	,     __UpperCamelCase	:			Union[str, Any]					=					self.model_tester.prepare_config_and_inputs_for_common()

        # If it doesn't support cache, pass the test
        if not hasattr(lowerCamelCase__   ,				"""use_cache"""			):
           return

        __UpperCamelCase	:			int					=					model_class(lowerCamelCase__			).to(lowerCamelCase__			)
        if "use_cache" not in inputs:
           __UpperCamelCase	:			Optional[Any]					=					True
        __UpperCamelCase	:			Any					=					model(**lowerCamelCase__			)

        # If "past_key_values" is not returned, pass the test (e.g. RWKV uses a different cache name and format)
        if "past_key_values" not in outputs:
           return

        __UpperCamelCase	:			List[str]					=					(
            getattr(lowerCamelCase__   ,				"""decoder_layers"""   ,				lowerCamelCase__			)
            or getattr(lowerCamelCase__   ,				"""num_decoder_layers"""   ,				lowerCamelCase__			)
            or config.num_hidden_layers
        )
        __UpperCamelCase	:			Optional[Any]					=					getattr(lowerCamelCase__   ,				"""num_kv_heads"""   ,				config.num_attention_heads			)
        __UpperCamelCase	:			str					=					getattr(lowerCamelCase__   ,				"""d_model"""   ,				config.hidden_size			)
        __UpperCamelCase	:			List[str]					=					embed_dim // num_attention_heads

        __UpperCamelCase	:			List[Any]					=					outputs["""past_key_values"""]
        self.assertEqual(len(lowerCamelCase__			)   ,				lowerCamelCase__			)

        __UpperCamelCase	,     __UpperCamelCase	:			Dict					=					inputs["""input_ids"""].shape
        for i in range(lowerCamelCase__			):
           if config.new_decoder_architecture:
              __UpperCamelCase	:			List[Any]					=					config.num_attention_heads
           elif config.multi_query:
              __UpperCamelCase	:			List[Any]					=					1
           self.assertEqual(len(past_kv[0]			)   ,				2			)  # K V for the decoder = 2
           self.assertEqual(
               past_kv[i][0].shape   ,				(batch_size, num_attention_heads, seq_length, per_head_embed_dim)			)
           self.assertEqual(
               past_kv[i][1].shape   ,				(batch_size, num_attention_heads, seq_length, per_head_embed_dim)			)







@require_torch
class 		_A						( unittest.TestCase ):

  @slow
  def    a					(    self						:	Optional[int]			):
     """simple docstring"""

     __UpperCamelCase	:			int					=					AutoTokenizer.from_pretrained("""Rocketknight1/falcon-rw-1b"""			)
     __UpperCamelCase	:			Optional[int]					=					FalconForCausalLM.from_pretrained("""Rocketknight1/falcon-rw-1b"""			)
     model.eval()
     model.to(lowerCamelCase__			)
     __UpperCamelCase	:			str					=					tokenizer("""My favorite food is"""   ,				return_tensors="""pt"""			).to(lowerCamelCase__			)

     __UpperCamelCase	:			Tuple					=					(
         """My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday."""
     )

     __UpperCamelCase	:			Tuple					=					model.generate(**lowerCamelCase__   ,				do_sample=lowerCamelCase__   ,				max_new_tokens=19			)
     __UpperCamelCase	:			Dict					=					tokenizer.batch_decode(lowerCamelCase__			)[0]

     self.assertEqual(lowerCamelCase__   ,				lowerCamelCase__			)

  @slow
  def    a					(    self						:	Dict			):
     """simple docstring"""

     for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
        __UpperCamelCase	:			Dict					=					AutoTokenizer.from_pretrained(lowerCamelCase__			)
        __UpperCamelCase	:			Union[str, Any]					=					FalconForCausalLM.from_pretrained(lowerCamelCase__			)
        model.eval()
        model.to(lowerCamelCase__			)
        __UpperCamelCase	:			Dict					=					tokenizer("""My favorite food is"""   ,				return_tensors="""pt"""			).to(lowerCamelCase__			)

        # We just test that these run without errors - the models are randomly initialized
        # and so the actual text outputs will be garbage
        model.generate(**lowerCamelCase__   ,				do_sample=lowerCamelCase__   ,				max_new_tokens=4			)
        model.generate(**lowerCamelCase__   ,				do_sample=lowerCamelCase__   ,				max_new_tokens=4			)
        model.generate(**lowerCamelCase__   ,				num_beams=2   ,				max_new_tokens=4			)

  @slow
  def    a					(    self						:	Tuple			):
     """simple docstring"""

     with torch.no_grad():
        for repo in [
            "Rocketknight1/falcon-rw-1b",
            "Rocketknight1/tiny-random-falcon-7b",
            "Rocketknight1/tiny-random-falcon-40b",
        ]:
           __UpperCamelCase	:			List[str]					=					AutoTokenizer.from_pretrained(lowerCamelCase__			)
           __UpperCamelCase	:			List[str]					=					FalconForCausalLM.from_pretrained(lowerCamelCase__			)
           model.eval()
           model.to(device=lowerCamelCase__			)
           __UpperCamelCase	:			Optional[int]					=					tokenizer("""My favorite food is"""   ,				return_tensors="""pt"""			).to(lowerCamelCase__			)

           # Test results are the same with and without cache
           __UpperCamelCase	:			Dict					=					model.generate(**lowerCamelCase__   ,				do_sample=lowerCamelCase__   ,				max_new_tokens=20   ,				use_cache=lowerCamelCase__			)
           __UpperCamelCase	:			int					=					model.generate(**lowerCamelCase__   ,				do_sample=lowerCamelCase__   ,				max_new_tokens=20   ,				use_cache=lowerCamelCase__			)
           self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0			)



