






"""simple docstring"""





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     :							Optional[int]       ,						__SCREAMING_SNAKE_CASE     :							Dict       ,						__SCREAMING_SNAKE_CASE     :							Dict=3       ,						__SCREAMING_SNAKE_CASE     :							Optional[int]=7       ,						__SCREAMING_SNAKE_CASE     :							Any=True       ,						__SCREAMING_SNAKE_CASE     :							str=True       ,						__SCREAMING_SNAKE_CASE     :							Optional[Any]=False       ,						__SCREAMING_SNAKE_CASE     :							Union[str, Any]=True       ,						__SCREAMING_SNAKE_CASE     :							Optional[int]=99       ,						__SCREAMING_SNAKE_CASE     :							Optional[int]=32       ,						__SCREAMING_SNAKE_CASE     :							Optional[int]=5       ,						__SCREAMING_SNAKE_CASE     :							Tuple=4       ,						__SCREAMING_SNAKE_CASE     :							Dict=37       ,						__SCREAMING_SNAKE_CASE     :							Optional[Any]="gelu"       ,						__SCREAMING_SNAKE_CASE     :							Union[str, Any]=0.1       ,						__SCREAMING_SNAKE_CASE     :							List[str]=0.1       ,						__SCREAMING_SNAKE_CASE     :							Union[str, Any]=512       ,						__SCREAMING_SNAKE_CASE     :							List[Any]=16       ,						__SCREAMING_SNAKE_CASE     :							Optional[Any]=2       ,						__SCREAMING_SNAKE_CASE     :							Tuple=0.02       ,						__SCREAMING_SNAKE_CASE     :							str=3       ,						__SCREAMING_SNAKE_CASE     :							Dict=4       ,						__SCREAMING_SNAKE_CASE     :							int=None       ,						)     ->		List[str]:
      lowerCamelCase_         =						parent
      lowerCamelCase_         =						batch_size
      lowerCamelCase_         =						seq_length
      lowerCamelCase_         =						is_training
      lowerCamelCase_         =						use_input_mask
      lowerCamelCase_         =						use_token_type_ids
      lowerCamelCase_         =						use_labels
      lowerCamelCase_         =						vocab_size
      lowerCamelCase_         =						hidden_size
      lowerCamelCase_         =						num_hidden_layers
      lowerCamelCase_         =						num_attention_heads
      lowerCamelCase_         =						intermediate_size
      lowerCamelCase_         =						hidden_act
      lowerCamelCase_         =						hidden_dropout_prob
      lowerCamelCase_         =						attention_probs_dropout_prob
      lowerCamelCase_         =						max_position_embeddings
      lowerCamelCase_         =						type_vocab_size
      lowerCamelCase_         =						type_sequence_label_size
      lowerCamelCase_         =						initializer_range
      lowerCamelCase_         =						num_labels
      lowerCamelCase_         =						num_choices
      lowerCamelCase_         =						scope






    def    UpperCamelCase       (       self     :							Optional[Any]					)     ->		Optional[Any]:
      lowerCamelCase_         =						ids_tensor([self.batch_size, self.seq_length]       ,						self.vocab_size					)

      lowerCamelCase_         =						None
      if self.use_input_mask:
        lowerCamelCase_         =						random_attention_mask([self.batch_size, self.seq_length]					)

      lowerCamelCase_         =						None

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

      lowerCamelCase_         =						self.get_config()

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






    def    UpperCamelCase       (       self     :							Union[str, Any]					)     ->		Any:
      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=__SCREAMING_SNAKE_CASE       ,						initializer_range=self.initializer_range       ,						pad_token_id=1       ,						new_decoder_architecture=__SCREAMING_SNAKE_CASE       ,						)






    def    UpperCamelCase       (       self     :							Union[str, Any]       ,						__SCREAMING_SNAKE_CASE     :							Dict       ,						__SCREAMING_SNAKE_CASE     :							Union[str, Any]       ,						__SCREAMING_SNAKE_CASE     :							List[Any]       ,						__SCREAMING_SNAKE_CASE     :							Any       ,						__SCREAMING_SNAKE_CASE     :							int       ,						__SCREAMING_SNAKE_CASE     :							List[Any]       ,						__SCREAMING_SNAKE_CASE     :							List[str]					)     ->		Optional[int]:
      lowerCamelCase_         =						FalconModel(config=__SCREAMING_SNAKE_CASE					)
      model.to(__SCREAMING_SNAKE_CASE					)
      model.eval()
      lowerCamelCase_         =						model(__SCREAMING_SNAKE_CASE       ,						attention_mask=__SCREAMING_SNAKE_CASE					)
      lowerCamelCase_         =						model(__SCREAMING_SNAKE_CASE					)
      self.parent.assertEqual(result.last_hidden_state.shape       ,						(self.batch_size, self.seq_length, self.hidden_size)					)






    def    UpperCamelCase       (       self     :							Union[str, Any]       ,						__SCREAMING_SNAKE_CASE     :							Tuple       ,						__SCREAMING_SNAKE_CASE     :							Optional[Any]       ,						__SCREAMING_SNAKE_CASE     :							Tuple       ,						__SCREAMING_SNAKE_CASE     :							str       ,						__SCREAMING_SNAKE_CASE     :							str       ,						__SCREAMING_SNAKE_CASE     :							Dict       ,						__SCREAMING_SNAKE_CASE     :							Optional[Any]       ,						__SCREAMING_SNAKE_CASE     :							List[Any]       ,						__SCREAMING_SNAKE_CASE     :							str       ,						)     ->		List[Any]:
      lowerCamelCase_         =						True
      lowerCamelCase_         =						FalconModel(__SCREAMING_SNAKE_CASE					)
      model.to(__SCREAMING_SNAKE_CASE					)
      model.eval()
      lowerCamelCase_         =						model(
          __SCREAMING_SNAKE_CASE       ,						attention_mask=__SCREAMING_SNAKE_CASE       ,						encoder_hidden_states=__SCREAMING_SNAKE_CASE       ,						encoder_attention_mask=__SCREAMING_SNAKE_CASE       ,						)
      lowerCamelCase_         =						model(
          __SCREAMING_SNAKE_CASE       ,						attention_mask=__SCREAMING_SNAKE_CASE       ,						encoder_hidden_states=__SCREAMING_SNAKE_CASE       ,						)
      lowerCamelCase_         =						model(__SCREAMING_SNAKE_CASE       ,						attention_mask=__SCREAMING_SNAKE_CASE					)
      self.parent.assertEqual(result.last_hidden_state.shape       ,						(self.batch_size, self.seq_length, self.hidden_size)					)






    def    UpperCamelCase       (       self     :							int       ,						__SCREAMING_SNAKE_CASE     :							Union[str, Any]       ,						__SCREAMING_SNAKE_CASE     :							Union[str, Any]       ,						__SCREAMING_SNAKE_CASE     :							Optional[Any]       ,						__SCREAMING_SNAKE_CASE     :							List[Any]       ,						__SCREAMING_SNAKE_CASE     :							int       ,						__SCREAMING_SNAKE_CASE     :							List[str]       ,						__SCREAMING_SNAKE_CASE     :							Union[str, Any]       ,						__SCREAMING_SNAKE_CASE     :							Any       ,						__SCREAMING_SNAKE_CASE     :							Union[str, Any]       ,						)     ->		Union[str, Any]:
      lowerCamelCase_         =						FalconForCausalLM(config=__SCREAMING_SNAKE_CASE					)
      model.to(__SCREAMING_SNAKE_CASE					)
      model.eval()
      lowerCamelCase_         =						model(__SCREAMING_SNAKE_CASE       ,						attention_mask=__SCREAMING_SNAKE_CASE       ,						labels=__SCREAMING_SNAKE_CASE					)
      self.parent.assertEqual(result.logits.shape       ,						(self.batch_size, self.seq_length, self.vocab_size)					)






    def    UpperCamelCase       (       self     :							int       ,						__SCREAMING_SNAKE_CASE     :							Dict       ,						__SCREAMING_SNAKE_CASE     :							Optional[Any]       ,						__SCREAMING_SNAKE_CASE     :							List[Any]       ,						__SCREAMING_SNAKE_CASE     :							Dict       ,						__SCREAMING_SNAKE_CASE     :							Dict       ,						__SCREAMING_SNAKE_CASE     :							Optional[int]       ,						__SCREAMING_SNAKE_CASE     :							List[Any]       ,						__SCREAMING_SNAKE_CASE     :							Optional[Any]       ,						__SCREAMING_SNAKE_CASE     :							Tuple       ,						)     ->		Optional[int]:
      lowerCamelCase_         =						True
      lowerCamelCase_         =						True
      lowerCamelCase_         =						FalconForCausalLM(config=__SCREAMING_SNAKE_CASE					)
      model.to(__SCREAMING_SNAKE_CASE					)
      model.eval()

      # first forward pass
      lowerCamelCase_         =						model(
          __SCREAMING_SNAKE_CASE       ,						attention_mask=__SCREAMING_SNAKE_CASE       ,						encoder_hidden_states=__SCREAMING_SNAKE_CASE       ,						encoder_attention_mask=__SCREAMING_SNAKE_CASE       ,						use_cache=__SCREAMING_SNAKE_CASE       ,						)
      lowerCamelCase_         =						outputs.past_key_values

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

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

      lowerCamelCase_         =						model(
          __SCREAMING_SNAKE_CASE       ,						attention_mask=__SCREAMING_SNAKE_CASE       ,						encoder_hidden_states=__SCREAMING_SNAKE_CASE       ,						encoder_attention_mask=__SCREAMING_SNAKE_CASE       ,						output_hidden_states=__SCREAMING_SNAKE_CASE       ,						)['hidden_states'][0]
      lowerCamelCase_         =						model(
          __SCREAMING_SNAKE_CASE       ,						attention_mask=__SCREAMING_SNAKE_CASE       ,						encoder_hidden_states=__SCREAMING_SNAKE_CASE       ,						encoder_attention_mask=__SCREAMING_SNAKE_CASE       ,						past_key_values=__SCREAMING_SNAKE_CASE       ,						output_hidden_states=__SCREAMING_SNAKE_CASE       ,						)['hidden_states'][0]

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






    def    UpperCamelCase       (       self     :							Dict					)     ->		Optional[Any]:
      lowerCamelCase_         =						self.prepare_config_and_inputs()
      (
          (
          lowerCamelCase_
      )     ,	(
          lowerCamelCase_
      )     ,	(
          lowerCamelCase_
      )     ,	(
          lowerCamelCase_
      )     ,	(
          lowerCamelCase_
      )     ,	(
          lowerCamelCase_
      )     ,	(
          lowerCamelCase_
      )     ,	
      )         =						config_and_inputs
      lowerCamelCase_         =						{'input_ids': input_ids, 'attention_mask': input_mask}
      return config, inputs_dict






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






    def    UpperCamelCase       (       self     :							int					)     ->		Optional[int]:
      lowerCamelCase_         =						FalconModelTester(self					)
      lowerCamelCase_         =						ConfigTester(self       ,						config_class=__SCREAMING_SNAKE_CASE       ,						hidden_size=37					)






    def    UpperCamelCase       (       self     :							List[str]					)     ->		Union[str, Any]:
      self.config_tester.run_common_tests()






    def    UpperCamelCase       (       self     :							str					)     ->		Optional[int]:
      lowerCamelCase_         =						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE					)






    def    UpperCamelCase       (       self     :							Dict					)     ->		Dict:
      lowerCamelCase_     ,	*lowerCamelCase_         =						self.model_tester.prepare_config_and_inputs()
      for alibi in [True, False]:
        lowerCamelCase_         =						alibi
        self.model_tester.create_and_check_model(__SCREAMING_SNAKE_CASE       ,						*__SCREAMING_SNAKE_CASE					)






    def    UpperCamelCase       (       self     :							str					)     ->		List[str]:
      lowerCamelCase_     ,	lowerCamelCase_         =						self.model_tester.prepare_config_and_inputs_for_common()
      lowerCamelCase_         =						3
      lowerCamelCase_         =						input_dict['input_ids']
      lowerCamelCase_         =						input_ids.ne(1					).to(__SCREAMING_SNAKE_CASE					)
      lowerCamelCase_         =						ids_tensor([self.model_tester.batch_size]       ,						self.model_tester.type_sequence_label_size					)
      lowerCamelCase_         =						FalconForSequenceClassification(__SCREAMING_SNAKE_CASE					)
      model.to(__SCREAMING_SNAKE_CASE					)
      model.eval()
      lowerCamelCase_         =						model(__SCREAMING_SNAKE_CASE       ,						attention_mask=__SCREAMING_SNAKE_CASE       ,						labels=__SCREAMING_SNAKE_CASE					)
      self.assertEqual(result.logits.shape       ,						(self.model_tester.batch_size, self.model_tester.num_labels)					)






    def    UpperCamelCase       (       self     :							Dict					)     ->		int:
      lowerCamelCase_     ,	lowerCamelCase_         =						self.model_tester.prepare_config_and_inputs_for_common()
      lowerCamelCase_         =						3
      lowerCamelCase_         =						'single_label_classification'
      lowerCamelCase_         =						input_dict['input_ids']
      lowerCamelCase_         =						input_ids.ne(1					).to(__SCREAMING_SNAKE_CASE					)
      lowerCamelCase_         =						ids_tensor([self.model_tester.batch_size]       ,						self.model_tester.type_sequence_label_size					)
      lowerCamelCase_         =						FalconForSequenceClassification(__SCREAMING_SNAKE_CASE					)
      model.to(__SCREAMING_SNAKE_CASE					)
      model.eval()
      lowerCamelCase_         =						model(__SCREAMING_SNAKE_CASE       ,						attention_mask=__SCREAMING_SNAKE_CASE       ,						labels=__SCREAMING_SNAKE_CASE					)
      self.assertEqual(result.logits.shape       ,						(self.model_tester.batch_size, self.model_tester.num_labels)					)






    def    UpperCamelCase       (       self     :							Tuple					)     ->		Optional[int]:
      lowerCamelCase_     ,	lowerCamelCase_         =						self.model_tester.prepare_config_and_inputs_for_common()
      lowerCamelCase_         =						input_dict['input_ids']
      lowerCamelCase_         =						FalconForCausalLM(__SCREAMING_SNAKE_CASE					)
      model.to(__SCREAMING_SNAKE_CASE					)
      model.eval()
      lowerCamelCase_         =						model(__SCREAMING_SNAKE_CASE       ,						use_cache=__SCREAMING_SNAKE_CASE					)
      lowerCamelCase_         =						input_ids.shape[0]
      lowerCamelCase_         =						model._convert_to_rw_cache(result.past_key_values					)
      lowerCamelCase_         =						model._convert_cache_to_standard_format(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE					)
      for layer in range(len(__SCREAMING_SNAKE_CASE					)					):
        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    UpperCamelCase       (       self     :							List[Any]					)     ->		str:
      lowerCamelCase_     ,	lowerCamelCase_         =						self.model_tester.prepare_config_and_inputs_for_common()
      lowerCamelCase_         =						3
      lowerCamelCase_         =						'multi_label_classification'
      lowerCamelCase_         =						input_dict['input_ids']
      lowerCamelCase_         =						input_ids.ne(1					).to(__SCREAMING_SNAKE_CASE					)
      lowerCamelCase_         =						ids_tensor(
          [self.model_tester.batch_size, config.num_labels]       ,						self.model_tester.type_sequence_label_size					).to(torch.float					)
      lowerCamelCase_         =						FalconForSequenceClassification(__SCREAMING_SNAKE_CASE					)
      model.to(__SCREAMING_SNAKE_CASE					)
      model.eval()
      lowerCamelCase_         =						model(__SCREAMING_SNAKE_CASE       ,						attention_mask=__SCREAMING_SNAKE_CASE       ,						labels=__SCREAMING_SNAKE_CASE					)
      self.assertEqual(result.logits.shape       ,						(self.model_tester.batch_size, self.model_tester.num_labels)					)






    def    UpperCamelCase       (       self     :							Dict					)     ->		Optional[Any]:
      # Falcon can have different numbers of KV-heads than the number of query heads, so we need
      # to override this test to use the right head counts.
      for model_class in self.all_generative_model_classes:
        lowerCamelCase_     ,	lowerCamelCase_         =						self.model_tester.prepare_config_and_inputs_for_common()

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

        lowerCamelCase_         =						model_class(__SCREAMING_SNAKE_CASE					).to(__SCREAMING_SNAKE_CASE					)
        if "use_cache" not in inputs:
          lowerCamelCase_         =						True
        lowerCamelCase_         =						model(**__SCREAMING_SNAKE_CASE					)

        # 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

        lowerCamelCase_         =						(
            getattr(__SCREAMING_SNAKE_CASE       ,						'decoder_layers'       ,						__SCREAMING_SNAKE_CASE					)
            or getattr(__SCREAMING_SNAKE_CASE       ,						'num_decoder_layers'       ,						__SCREAMING_SNAKE_CASE					)
            or config.num_hidden_layers
        )
        lowerCamelCase_         =						getattr(__SCREAMING_SNAKE_CASE       ,						'num_kv_heads'       ,						config.num_attention_heads					)
        lowerCamelCase_         =						getattr(__SCREAMING_SNAKE_CASE       ,						'd_model'       ,						config.hidden_size					)
        lowerCamelCase_         =						embed_dim // num_attention_heads

        lowerCamelCase_         =						outputs['past_key_values']
        self.assertEqual(len(__SCREAMING_SNAKE_CASE					)       ,						__SCREAMING_SNAKE_CASE					)

        lowerCamelCase_     ,	lowerCamelCase_         =						inputs['input_ids'].shape
        for i in range(__SCREAMING_SNAKE_CASE					):
          if config.new_decoder_architecture:
            lowerCamelCase_         =						config.num_attention_heads
          elif config.multi_query:
            lowerCamelCase_         =						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    UpperCamelCase       (       self     :							Optional[int]					)     ->		Any:
      lowerCamelCase_         =						AutoTokenizer.from_pretrained('Rocketknight1/falcon-rw-1b'					)
      lowerCamelCase_         =						FalconForCausalLM.from_pretrained('Rocketknight1/falcon-rw-1b'					)
      model.eval()
      model.to(__SCREAMING_SNAKE_CASE					)
      lowerCamelCase_         =						tokenizer('My favorite food is'       ,						return_tensors='pt'					).to(__SCREAMING_SNAKE_CASE					)

      lowerCamelCase_         =						(
          'My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday.'
      )

      lowerCamelCase_         =						model.generate(**__SCREAMING_SNAKE_CASE       ,						do_sample=__SCREAMING_SNAKE_CASE       ,						max_new_tokens=19					)
      lowerCamelCase_         =						tokenizer.batch_decode(__SCREAMING_SNAKE_CASE					)[0]

      self.assertEqual(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE					)






    @slow
    def    UpperCamelCase       (       self     :							Any					)     ->		List[str]:
      # The big models are way too big for the CI, so we use tiny random models that resemble their
      # architectures but with much smaller and fewer layers
      for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
        lowerCamelCase_         =						AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE					)
        lowerCamelCase_         =						FalconForCausalLM.from_pretrained(__SCREAMING_SNAKE_CASE					)
        model.eval()
        model.to(__SCREAMING_SNAKE_CASE					)
        lowerCamelCase_         =						tokenizer('My favorite food is'       ,						return_tensors='pt'					).to(__SCREAMING_SNAKE_CASE					)

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






    @slow
    def    UpperCamelCase       (       self     :							Union[str, Any]					)     ->		Union[str, Any]:
      # The big models are way too big for the CI, so we use tiny random models that resemble their
      # architectures but with much smaller and fewer layers
      with torch.no_grad():
        for repo in [
            "Rocketknight1/falcon-rw-1b",
            "Rocketknight1/tiny-random-falcon-7b",
            "Rocketknight1/tiny-random-falcon-40b",
        ]:
          lowerCamelCase_         =						AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE					)
          lowerCamelCase_         =						FalconForCausalLM.from_pretrained(__SCREAMING_SNAKE_CASE					)
          model.eval()
          model.to(device=__SCREAMING_SNAKE_CASE					)
          lowerCamelCase_         =						tokenizer('My favorite food is'       ,						return_tensors='pt'					).to(__SCREAMING_SNAKE_CASE					)

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












"""simple docstring"""





from typing import Any, Callable, Dict, List, Optional, Union

import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker


_SCREAMING_SNAKE_CASE					:				Union[str, Any]					    =						'''CompVis/stable-diffusion-v1-1'''
_SCREAMING_SNAKE_CASE					:				Optional[Any]					    =						'''CompVis/stable-diffusion-v1-2'''
_SCREAMING_SNAKE_CASE					:				int					    =						'''CompVis/stable-diffusion-v1-3'''
_SCREAMING_SNAKE_CASE					:				str					    =						'''CompVis/stable-diffusion-v1-4'''






class      a						(					__snake_case		):






    def __init__(       self     :							int       ,						__SCREAMING_SNAKE_CASE     :							AutoencoderKL       ,						__SCREAMING_SNAKE_CASE     :							CLIPTextModel       ,						__SCREAMING_SNAKE_CASE     :							CLIPTokenizer       ,						__SCREAMING_SNAKE_CASE     :							UNetaDConditionModel       ,						__SCREAMING_SNAKE_CASE     :							Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]       ,						__SCREAMING_SNAKE_CASE     :							StableDiffusionSafetyChecker       ,						__SCREAMING_SNAKE_CASE     :							CLIPImageProcessor       ,						__SCREAMING_SNAKE_CASE     :							bool = True       ,						)     ->		List[str]:
      super()._init_()

      lowerCamelCase_         =						StableDiffusionPipeline.from_pretrained(__SCREAMING_SNAKE_CASE					)
      lowerCamelCase_         =						StableDiffusionPipeline.from_pretrained(__SCREAMING_SNAKE_CASE					)
      lowerCamelCase_         =						StableDiffusionPipeline.from_pretrained(__SCREAMING_SNAKE_CASE					)
      lowerCamelCase_         =						StableDiffusionPipeline(
          vae=__SCREAMING_SNAKE_CASE       ,						text_encoder=__SCREAMING_SNAKE_CASE       ,						tokenizer=__SCREAMING_SNAKE_CASE       ,						unet=__SCREAMING_SNAKE_CASE       ,						scheduler=__SCREAMING_SNAKE_CASE       ,						safety_checker=__SCREAMING_SNAKE_CASE       ,						feature_extractor=__SCREAMING_SNAKE_CASE       ,						requires_safety_checker=__SCREAMING_SNAKE_CASE       ,						)

      self.register_modules(pipelinea=self.pipea       ,						pipelinea=self.pipea       ,						pipelinea=self.pipea       ,						pipelinea=self.pipea					)






    @property
    def    UpperCamelCase       (       self     :							List[str]					)     ->		Dict[str, Any]:
      return {k: getattr(self       ,						__SCREAMING_SNAKE_CASE					) for k in self.config.keys() if not k.startswith('_'					)}






    def    UpperCamelCase       (       self     :							int       ,						__SCREAMING_SNAKE_CASE     :							Optional[Union[str, int]] = "auto"					)     ->		Any:
      if slice_size == "auto":
        # half the attention head size is usually a good trade-off between
        # speed and memory
        lowerCamelCase_         =						self.unet.config.attention_head_dim // 2
      self.unet.set_attention_slice(__SCREAMING_SNAKE_CASE					)






    def    UpperCamelCase       (       self     :							Any					)     ->		List[Any]:
      self.enable_attention_slicing(__SCREAMING_SNAKE_CASE					)






    @torch.no_grad()
    def    UpperCamelCase       (       self     :							Any       ,						__SCREAMING_SNAKE_CASE     :							Union[str, List[str]]       ,						__SCREAMING_SNAKE_CASE     :							int = 512       ,						__SCREAMING_SNAKE_CASE     :							int = 512       ,						__SCREAMING_SNAKE_CASE     :							int = 50       ,						__SCREAMING_SNAKE_CASE     :							float = 7.5       ,						__SCREAMING_SNAKE_CASE     :							Optional[Union[str, List[str]]] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[int] = 1       ,						__SCREAMING_SNAKE_CASE     :							float = 0.0       ,						__SCREAMING_SNAKE_CASE     :							Optional[torch.Generator] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[torch.FloatTensor] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[str] = "pil"       ,						__SCREAMING_SNAKE_CASE     :							bool = True       ,						__SCREAMING_SNAKE_CASE     :							Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,						__SCREAMING_SNAKE_CASE     :							int = 1       ,						**__SCREAMING_SNAKE_CASE     :							int       ,						)     ->		Tuple:
      return self.pipea(
          prompt=__SCREAMING_SNAKE_CASE       ,						height=__SCREAMING_SNAKE_CASE       ,						width=__SCREAMING_SNAKE_CASE       ,						num_inference_steps=__SCREAMING_SNAKE_CASE       ,						guidance_scale=__SCREAMING_SNAKE_CASE       ,						negative_prompt=__SCREAMING_SNAKE_CASE       ,						num_images_per_prompt=__SCREAMING_SNAKE_CASE       ,						eta=__SCREAMING_SNAKE_CASE       ,						generator=__SCREAMING_SNAKE_CASE       ,						latents=__SCREAMING_SNAKE_CASE       ,						output_type=__SCREAMING_SNAKE_CASE       ,						return_dict=__SCREAMING_SNAKE_CASE       ,						callback=__SCREAMING_SNAKE_CASE       ,						callback_steps=__SCREAMING_SNAKE_CASE       ,						**__SCREAMING_SNAKE_CASE       ,						)






    @torch.no_grad()
    def    UpperCamelCase       (       self     :							List[str]       ,						__SCREAMING_SNAKE_CASE     :							Union[str, List[str]]       ,						__SCREAMING_SNAKE_CASE     :							int = 512       ,						__SCREAMING_SNAKE_CASE     :							int = 512       ,						__SCREAMING_SNAKE_CASE     :							int = 50       ,						__SCREAMING_SNAKE_CASE     :							float = 7.5       ,						__SCREAMING_SNAKE_CASE     :							Optional[Union[str, List[str]]] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[int] = 1       ,						__SCREAMING_SNAKE_CASE     :							float = 0.0       ,						__SCREAMING_SNAKE_CASE     :							Optional[torch.Generator] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[torch.FloatTensor] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[str] = "pil"       ,						__SCREAMING_SNAKE_CASE     :							bool = True       ,						__SCREAMING_SNAKE_CASE     :							Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,						__SCREAMING_SNAKE_CASE     :							int = 1       ,						**__SCREAMING_SNAKE_CASE     :							List[str]       ,						)     ->		Optional[int]:
      return self.pipea(
          prompt=__SCREAMING_SNAKE_CASE       ,						height=__SCREAMING_SNAKE_CASE       ,						width=__SCREAMING_SNAKE_CASE       ,						num_inference_steps=__SCREAMING_SNAKE_CASE       ,						guidance_scale=__SCREAMING_SNAKE_CASE       ,						negative_prompt=__SCREAMING_SNAKE_CASE       ,						num_images_per_prompt=__SCREAMING_SNAKE_CASE       ,						eta=__SCREAMING_SNAKE_CASE       ,						generator=__SCREAMING_SNAKE_CASE       ,						latents=__SCREAMING_SNAKE_CASE       ,						output_type=__SCREAMING_SNAKE_CASE       ,						return_dict=__SCREAMING_SNAKE_CASE       ,						callback=__SCREAMING_SNAKE_CASE       ,						callback_steps=__SCREAMING_SNAKE_CASE       ,						**__SCREAMING_SNAKE_CASE       ,						)






    @torch.no_grad()
    def    UpperCamelCase       (       self     :							Optional[Any]       ,						__SCREAMING_SNAKE_CASE     :							Union[str, List[str]]       ,						__SCREAMING_SNAKE_CASE     :							int = 512       ,						__SCREAMING_SNAKE_CASE     :							int = 512       ,						__SCREAMING_SNAKE_CASE     :							int = 50       ,						__SCREAMING_SNAKE_CASE     :							float = 7.5       ,						__SCREAMING_SNAKE_CASE     :							Optional[Union[str, List[str]]] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[int] = 1       ,						__SCREAMING_SNAKE_CASE     :							float = 0.0       ,						__SCREAMING_SNAKE_CASE     :							Optional[torch.Generator] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[torch.FloatTensor] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[str] = "pil"       ,						__SCREAMING_SNAKE_CASE     :							bool = True       ,						__SCREAMING_SNAKE_CASE     :							Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,						__SCREAMING_SNAKE_CASE     :							int = 1       ,						**__SCREAMING_SNAKE_CASE     :							Optional[int]       ,						)     ->		Tuple:
      return self.pipea(
          prompt=__SCREAMING_SNAKE_CASE       ,						height=__SCREAMING_SNAKE_CASE       ,						width=__SCREAMING_SNAKE_CASE       ,						num_inference_steps=__SCREAMING_SNAKE_CASE       ,						guidance_scale=__SCREAMING_SNAKE_CASE       ,						negative_prompt=__SCREAMING_SNAKE_CASE       ,						num_images_per_prompt=__SCREAMING_SNAKE_CASE       ,						eta=__SCREAMING_SNAKE_CASE       ,						generator=__SCREAMING_SNAKE_CASE       ,						latents=__SCREAMING_SNAKE_CASE       ,						output_type=__SCREAMING_SNAKE_CASE       ,						return_dict=__SCREAMING_SNAKE_CASE       ,						callback=__SCREAMING_SNAKE_CASE       ,						callback_steps=__SCREAMING_SNAKE_CASE       ,						**__SCREAMING_SNAKE_CASE       ,						)






    @torch.no_grad()
    def    UpperCamelCase       (       self     :							Tuple       ,						__SCREAMING_SNAKE_CASE     :							Union[str, List[str]]       ,						__SCREAMING_SNAKE_CASE     :							int = 512       ,						__SCREAMING_SNAKE_CASE     :							int = 512       ,						__SCREAMING_SNAKE_CASE     :							int = 50       ,						__SCREAMING_SNAKE_CASE     :							float = 7.5       ,						__SCREAMING_SNAKE_CASE     :							Optional[Union[str, List[str]]] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[int] = 1       ,						__SCREAMING_SNAKE_CASE     :							float = 0.0       ,						__SCREAMING_SNAKE_CASE     :							Optional[torch.Generator] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[torch.FloatTensor] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[str] = "pil"       ,						__SCREAMING_SNAKE_CASE     :							bool = True       ,						__SCREAMING_SNAKE_CASE     :							Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,						__SCREAMING_SNAKE_CASE     :							int = 1       ,						**__SCREAMING_SNAKE_CASE     :							Tuple       ,						)     ->		Tuple:
      return self.pipea(
          prompt=__SCREAMING_SNAKE_CASE       ,						height=__SCREAMING_SNAKE_CASE       ,						width=__SCREAMING_SNAKE_CASE       ,						num_inference_steps=__SCREAMING_SNAKE_CASE       ,						guidance_scale=__SCREAMING_SNAKE_CASE       ,						negative_prompt=__SCREAMING_SNAKE_CASE       ,						num_images_per_prompt=__SCREAMING_SNAKE_CASE       ,						eta=__SCREAMING_SNAKE_CASE       ,						generator=__SCREAMING_SNAKE_CASE       ,						latents=__SCREAMING_SNAKE_CASE       ,						output_type=__SCREAMING_SNAKE_CASE       ,						return_dict=__SCREAMING_SNAKE_CASE       ,						callback=__SCREAMING_SNAKE_CASE       ,						callback_steps=__SCREAMING_SNAKE_CASE       ,						**__SCREAMING_SNAKE_CASE       ,						)






    @torch.no_grad()
    def    UpperCamelCase       (       self     :							Dict       ,						__SCREAMING_SNAKE_CASE     :							Union[str, List[str]]       ,						__SCREAMING_SNAKE_CASE     :							int = 512       ,						__SCREAMING_SNAKE_CASE     :							int = 512       ,						__SCREAMING_SNAKE_CASE     :							int = 50       ,						__SCREAMING_SNAKE_CASE     :							float = 7.5       ,						__SCREAMING_SNAKE_CASE     :							Optional[Union[str, List[str]]] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[int] = 1       ,						__SCREAMING_SNAKE_CASE     :							float = 0.0       ,						__SCREAMING_SNAKE_CASE     :							Optional[torch.Generator] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[torch.FloatTensor] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[str] = "pil"       ,						__SCREAMING_SNAKE_CASE     :							bool = True       ,						__SCREAMING_SNAKE_CASE     :							Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,						__SCREAMING_SNAKE_CASE     :							int = 1       ,						**__SCREAMING_SNAKE_CASE     :							int       ,						)     ->		str:
      lowerCamelCase_         =						'cuda' if torch.cuda.is_available() else 'cpu'
      self.to(__SCREAMING_SNAKE_CASE					)

      # Checks if the height and width are divisible by 8 or not
      if height % 8 != 0 or width % 8 != 0:
        raise ValueError(F'''`height` and `width` must be divisible by 8 but are {height} and {width}.'''					)

      # Get first result from Stable Diffusion Checkpoint v1.1
      lowerCamelCase_         =						self.textaimg_sda_a(
          prompt=__SCREAMING_SNAKE_CASE       ,						height=__SCREAMING_SNAKE_CASE       ,						width=__SCREAMING_SNAKE_CASE       ,						num_inference_steps=__SCREAMING_SNAKE_CASE       ,						guidance_scale=__SCREAMING_SNAKE_CASE       ,						negative_prompt=__SCREAMING_SNAKE_CASE       ,						num_images_per_prompt=__SCREAMING_SNAKE_CASE       ,						eta=__SCREAMING_SNAKE_CASE       ,						generator=__SCREAMING_SNAKE_CASE       ,						latents=__SCREAMING_SNAKE_CASE       ,						output_type=__SCREAMING_SNAKE_CASE       ,						return_dict=__SCREAMING_SNAKE_CASE       ,						callback=__SCREAMING_SNAKE_CASE       ,						callback_steps=__SCREAMING_SNAKE_CASE       ,						**__SCREAMING_SNAKE_CASE       ,						)

      # Get first result from Stable Diffusion Checkpoint v1.2
      lowerCamelCase_         =						self.textaimg_sda_a(
          prompt=__SCREAMING_SNAKE_CASE       ,						height=__SCREAMING_SNAKE_CASE       ,						width=__SCREAMING_SNAKE_CASE       ,						num_inference_steps=__SCREAMING_SNAKE_CASE       ,						guidance_scale=__SCREAMING_SNAKE_CASE       ,						negative_prompt=__SCREAMING_SNAKE_CASE       ,						num_images_per_prompt=__SCREAMING_SNAKE_CASE       ,						eta=__SCREAMING_SNAKE_CASE       ,						generator=__SCREAMING_SNAKE_CASE       ,						latents=__SCREAMING_SNAKE_CASE       ,						output_type=__SCREAMING_SNAKE_CASE       ,						return_dict=__SCREAMING_SNAKE_CASE       ,						callback=__SCREAMING_SNAKE_CASE       ,						callback_steps=__SCREAMING_SNAKE_CASE       ,						**__SCREAMING_SNAKE_CASE       ,						)

      # Get first result from Stable Diffusion Checkpoint v1.3
      lowerCamelCase_         =						self.textaimg_sda_a(
          prompt=__SCREAMING_SNAKE_CASE       ,						height=__SCREAMING_SNAKE_CASE       ,						width=__SCREAMING_SNAKE_CASE       ,						num_inference_steps=__SCREAMING_SNAKE_CASE       ,						guidance_scale=__SCREAMING_SNAKE_CASE       ,						negative_prompt=__SCREAMING_SNAKE_CASE       ,						num_images_per_prompt=__SCREAMING_SNAKE_CASE       ,						eta=__SCREAMING_SNAKE_CASE       ,						generator=__SCREAMING_SNAKE_CASE       ,						latents=__SCREAMING_SNAKE_CASE       ,						output_type=__SCREAMING_SNAKE_CASE       ,						return_dict=__SCREAMING_SNAKE_CASE       ,						callback=__SCREAMING_SNAKE_CASE       ,						callback_steps=__SCREAMING_SNAKE_CASE       ,						**__SCREAMING_SNAKE_CASE       ,						)

      # Get first result from Stable Diffusion Checkpoint v1.4
      lowerCamelCase_         =						self.textaimg_sda_a(
          prompt=__SCREAMING_SNAKE_CASE       ,						height=__SCREAMING_SNAKE_CASE       ,						width=__SCREAMING_SNAKE_CASE       ,						num_inference_steps=__SCREAMING_SNAKE_CASE       ,						guidance_scale=__SCREAMING_SNAKE_CASE       ,						negative_prompt=__SCREAMING_SNAKE_CASE       ,						num_images_per_prompt=__SCREAMING_SNAKE_CASE       ,						eta=__SCREAMING_SNAKE_CASE       ,						generator=__SCREAMING_SNAKE_CASE       ,						latents=__SCREAMING_SNAKE_CASE       ,						output_type=__SCREAMING_SNAKE_CASE       ,						return_dict=__SCREAMING_SNAKE_CASE       ,						callback=__SCREAMING_SNAKE_CASE       ,						callback_steps=__SCREAMING_SNAKE_CASE       ,						**__SCREAMING_SNAKE_CASE       ,						)

      # Get all result images into a single list and pass it via StableDiffusionPipelineOutput for final result
      return StableDiffusionPipelineOutput([resa[0], resa[0], resa[0], resa[0]]					)




