


'''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  lowerCAmelCase_						:





  """simple docstring"""



  def __init__(					self     :							Any				,							SCREAMING_SNAKE_CASE__     :							List[Any]				,							SCREAMING_SNAKE_CASE__     :							int=3				,							SCREAMING_SNAKE_CASE__     :							Optional[Any]=7				,							SCREAMING_SNAKE_CASE__     :							Any=True				,							SCREAMING_SNAKE_CASE__     :							Tuple=True				,							SCREAMING_SNAKE_CASE__     :							int=False				,							SCREAMING_SNAKE_CASE__     :							str=True				,							SCREAMING_SNAKE_CASE__     :							Optional[int]=9_9				,							SCREAMING_SNAKE_CASE__     :							Optional[int]=3_2				,							SCREAMING_SNAKE_CASE__     :							List[str]=5				,							SCREAMING_SNAKE_CASE__     :							List[Any]=4				,							SCREAMING_SNAKE_CASE__     :							List[Any]=3_7				,							SCREAMING_SNAKE_CASE__     :							Union[str, Any]="gelu"				,							SCREAMING_SNAKE_CASE__     :							List[Any]=0.1				,							SCREAMING_SNAKE_CASE__     :							Tuple=0.1				,							SCREAMING_SNAKE_CASE__     :							Optional[int]=5_1_2				,							SCREAMING_SNAKE_CASE__     :							Optional[int]=1_6				,							SCREAMING_SNAKE_CASE__     :							str=2				,							SCREAMING_SNAKE_CASE__     :							List[Any]=0.0_2				,							SCREAMING_SNAKE_CASE__     :							int=3				,							SCREAMING_SNAKE_CASE__     :							Dict=4				,							SCREAMING_SNAKE_CASE__     :							List[Any]=None				,							):





       '''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 __a						(					self     :							List[str]						):





       '''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

       __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 __a						(					self     :							List[Any]						):





       '''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=SCREAMING_SNAKE_CASE__				,							initializer_range=self.initializer_range				,							pad_token_id=1				,							new_decoder_architecture=SCREAMING_SNAKE_CASE__				,							)

  def __a						(					self     :							List[str]				,							SCREAMING_SNAKE_CASE__     :							int				,							SCREAMING_SNAKE_CASE__     :							List[str]				,							SCREAMING_SNAKE_CASE__     :							Optional[int]				,							SCREAMING_SNAKE_CASE__     :							str				,							SCREAMING_SNAKE_CASE__     :							List[Any]				,							SCREAMING_SNAKE_CASE__     :							Optional[int]				,							SCREAMING_SNAKE_CASE__     :							Optional[Any]						):





       '''simple docstring'''

       __a       =   FalconModel(config=SCREAMING_SNAKE_CASE__						)
       model.to(SCREAMING_SNAKE_CASE__						)
       model.eval()
       __a       =   model(SCREAMING_SNAKE_CASE__				,							attention_mask=SCREAMING_SNAKE_CASE__						)
       __a       =   model(SCREAMING_SNAKE_CASE__						)
       self.parent.assertEqual(result.last_hidden_state.shape				,							(self.batch_size, self.seq_length, self.hidden_size)						)

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





       '''simple docstring'''

       __a       =   True
       __a       =   FalconModel(SCREAMING_SNAKE_CASE__						)
       model.to(SCREAMING_SNAKE_CASE__						)
       model.eval()
       __a       =   model(
           SCREAMING_SNAKE_CASE__				,							attention_mask=SCREAMING_SNAKE_CASE__				,							encoder_hidden_states=SCREAMING_SNAKE_CASE__				,							encoder_attention_mask=SCREAMING_SNAKE_CASE__				,							)
       __a       =   model(
           SCREAMING_SNAKE_CASE__				,							attention_mask=SCREAMING_SNAKE_CASE__				,							encoder_hidden_states=SCREAMING_SNAKE_CASE__				,							)
       __a       =   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 __a						(					self     :							str				,							SCREAMING_SNAKE_CASE__     :							List[str]				,							SCREAMING_SNAKE_CASE__     :							Dict				,							SCREAMING_SNAKE_CASE__     :							Union[str, Any]				,							SCREAMING_SNAKE_CASE__     :							Optional[Any]				,							SCREAMING_SNAKE_CASE__     :							Optional[Any]				,							SCREAMING_SNAKE_CASE__     :							Any				,							SCREAMING_SNAKE_CASE__     :							str				,							SCREAMING_SNAKE_CASE__     :							Dict				,							SCREAMING_SNAKE_CASE__     :							List[Any]				,							):





       '''simple docstring'''

       __a       =   FalconForCausalLM(config=SCREAMING_SNAKE_CASE__						)
       model.to(SCREAMING_SNAKE_CASE__						)
       model.eval()
       __a       =   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 __a						(					self     :							Any				,							SCREAMING_SNAKE_CASE__     :							str				,							SCREAMING_SNAKE_CASE__     :							Optional[Any]				,							SCREAMING_SNAKE_CASE__     :							Optional[Any]				,							SCREAMING_SNAKE_CASE__     :							Tuple				,							SCREAMING_SNAKE_CASE__     :							int				,							SCREAMING_SNAKE_CASE__     :							Optional[Any]				,							SCREAMING_SNAKE_CASE__     :							int				,							SCREAMING_SNAKE_CASE__     :							Any				,							SCREAMING_SNAKE_CASE__     :							Any				,							):





       '''simple docstring'''

       __a       =   True
       __a       =   True
       __a       =   FalconForCausalLM(config=SCREAMING_SNAKE_CASE__						)
       model.to(SCREAMING_SNAKE_CASE__						)
       model.eval()

       # first forward pass
       __a       =   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__				,							)
       __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(
           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]
       __a       =   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
       __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(SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							atol=1E-3						)						)


  def __a						(					self     :							List[str]						):





       '''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  lowerCAmelCase_						(       snake_case__		,    snake_case__		,    snake_case__		,    unittest.TestCase      ):





  """simple docstring"""



  a_       :str												=(
      (
          FalconModel,
          FalconForCausalLM,
          FalconForSequenceClassification,
          FalconForTokenClassification,
          FalconForQuestionAnswering,
      )
      if is_torch_available()
      else ()
  )
  a_       :List[str]												=(FalconForCausalLM,) if is_torch_available() else ()
  a_       :int												=(
      {
          """feature-extraction""": FalconModel,
          """text-classification""": FalconForSequenceClassification,
          """text-generation""": FalconForCausalLM,
          """question-answering""": FalconForQuestionAnswering,
          """token-classification""": FalconForTokenClassification,
          """zero-shot""": FalconForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  a_       :Dict												=False
  a_       :Tuple												=False

  def __a						(					self     :							Dict						):





       '''simple docstring'''

       __a       =   FalconModelTester(self						)
       __a       =   ConfigTester(self				,							config_class=SCREAMING_SNAKE_CASE__				,							hidden_size=3_7						)

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





       '''simple docstring'''

       self.config_tester.run_common_tests()

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





       '''simple docstring'''

       __a       =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__						)

  def __a						(					self     :							Optional[Any]						):





       '''simple docstring'''

       __a ,						*__a       =   self.model_tester.prepare_config_and_inputs()
       for alibi in [True, False]:
            __a       =   alibi
            self.model_tester.create_and_check_model(SCREAMING_SNAKE_CASE__				,							*SCREAMING_SNAKE_CASE__						)

  def __a						(					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(SCREAMING_SNAKE_CASE__						)
       __a       =   ids_tensor([self.model_tester.batch_size]				,							self.model_tester.type_sequence_label_size						)
       __a       =   FalconForSequenceClassification(SCREAMING_SNAKE_CASE__						)
       model.to(SCREAMING_SNAKE_CASE__						)
       model.eval()
       __a       =   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 __a						(					self     :							Optional[int]						):





       '''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(SCREAMING_SNAKE_CASE__						)
       __a       =   ids_tensor([self.model_tester.batch_size]				,							self.model_tester.type_sequence_label_size						)
       __a       =   FalconForSequenceClassification(SCREAMING_SNAKE_CASE__						)
       model.to(SCREAMING_SNAKE_CASE__						)
       model.eval()
       __a       =   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 __a						(					self     :							Any						):





       '''simple docstring'''

       __a ,						__a       =   self.model_tester.prepare_config_and_inputs_for_common()
       __a       =   input_dict["""input_ids"""]
       __a       =   FalconForCausalLM(SCREAMING_SNAKE_CASE__						)
       model.to(SCREAMING_SNAKE_CASE__						)
       model.eval()
       __a       =   model(SCREAMING_SNAKE_CASE__				,							use_cache=SCREAMING_SNAKE_CASE__						)
       __a       =   input_ids.shape[0]
       __a       =   model._convert_to_rw_cache(result.past_key_values						)
       __a       =   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 __a						(					self     :							Union[str, Any]						):





       '''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(SCREAMING_SNAKE_CASE__						)
       __a       =   ids_tensor(
           [self.model_tester.batch_size, config.num_labels]				,							self.model_tester.type_sequence_label_size						).to(torch.float						)
       __a       =   FalconForSequenceClassification(SCREAMING_SNAKE_CASE__						)
       model.to(SCREAMING_SNAKE_CASE__						)
       model.eval()
       __a       =   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 __a						(					self     :							List[str]						):





       '''simple docstring'''

       for model_class in self.all_generative_model_classes:
            __a ,						__a       =   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

            __a       =   model_class(SCREAMING_SNAKE_CASE__						).to(SCREAMING_SNAKE_CASE__						)
            if "use_cache" not in inputs:
                 __a       =   True
            __a       =   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

            __a       =   (
                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
            )
            __a       =   getattr(SCREAMING_SNAKE_CASE__				,							"""num_kv_heads"""				,							config.num_attention_heads						)
            __a       =   getattr(SCREAMING_SNAKE_CASE__				,							"""d_model"""				,							config.hidden_size						)
            __a       =   embed_dim // num_attention_heads

            __a       =   outputs["""past_key_values"""]
            self.assertEqual(len(SCREAMING_SNAKE_CASE__						)				,							SCREAMING_SNAKE_CASE__						)

            __a ,						__a       =   inputs["""input_ids"""].shape
            for i in range(SCREAMING_SNAKE_CASE__						):
                 if config.new_decoder_architecture:
                      __a       =   config.num_attention_heads
                 elif config.multi_query:
                      __a       =   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  lowerCAmelCase_						(       unittest.TestCase      ):





  """simple docstring"""



  @slow
  def __a						(					self     :							int						):





       '''simple docstring'''

       __a       =   AutoTokenizer.from_pretrained("""Rocketknight1/falcon-rw-1b"""						)
       __a       =   FalconForCausalLM.from_pretrained("""Rocketknight1/falcon-rw-1b"""						)
       model.eval()
       model.to(SCREAMING_SNAKE_CASE__						)
       __a       =   tokenizer("""My favorite food is"""				,							return_tensors="""pt"""						).to(SCREAMING_SNAKE_CASE__						)

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

       __a       =   model.generate(**SCREAMING_SNAKE_CASE__				,							do_sample=SCREAMING_SNAKE_CASE__				,							max_new_tokens=1_9						)
       __a       =   tokenizer.batch_decode(SCREAMING_SNAKE_CASE__						)[0]

       self.assertEqual(SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__						)

  @slow
  def __a						(					self     :							Tuple						):





       '''simple docstring'''

       for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
            __a       =   AutoTokenizer.from_pretrained(SCREAMING_SNAKE_CASE__						)
            __a       =   FalconForCausalLM.from_pretrained(SCREAMING_SNAKE_CASE__						)
            model.eval()
            model.to(SCREAMING_SNAKE_CASE__						)
            __a       =   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 __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",
            ]:
                 __a       =   AutoTokenizer.from_pretrained(SCREAMING_SNAKE_CASE__						)
                 __a       =   FalconForCausalLM.from_pretrained(SCREAMING_SNAKE_CASE__						)
                 model.eval()
                 model.to(device=SCREAMING_SNAKE_CASE__						)
                 __a       =   tokenizer("""My favorite food is"""				,							return_tensors="""pt"""						).to(SCREAMING_SNAKE_CASE__						)

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










'''simple docstring'''







import gc
import unittest

import numpy as np
import torch
from torch.backends.cuda import sdp_kernel

from diffusers import (
    CMStochasticIterativeScheduler,
    ConsistencyModelPipeline,
    UNetaDModel,
)
from diffusers.utils import randn_tensor, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_a, require_torch_gpu

from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


class  lowerCAmelCase_						(       snake_case__		,    unittest.TestCase      ):





  """simple docstring"""



  a_       :Optional[int]												=ConsistencyModelPipeline
  a_       :List[Any]												=UNCONDITIONAL_IMAGE_GENERATION_PARAMS
  a_       :Optional[int]												=UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS

  # Override required_optional_params to remove num_images_per_prompt
  a_       :Optional[Any]												=frozenset(
      [
          """num_inference_steps""",
          """generator""",
          """latents""",
          """output_type""",
          """return_dict""",
          """callback""",
          """callback_steps""",
      ]      )

  @property
  def __a						(					self     :							Dict						):





       '''simple docstring'''

       __a       =   UNetaDModel.from_pretrained(
           """diffusers/consistency-models-test"""				,							subfolder="""test_unet"""				,							)
       return unet

  @property
  def __a						(					self     :							Tuple						):





       '''simple docstring'''

       __a       =   UNetaDModel.from_pretrained(
           """diffusers/consistency-models-test"""				,							subfolder="""test_unet_class_cond"""				,							)
       return unet

  def __a						(					self     :							str				,							SCREAMING_SNAKE_CASE__     :							int=False						):





       '''simple docstring'''

       if class_cond:
            __a       =   self.dummy_cond_unet
       else:
            __a       =   self.dummy_uncond_unet

       # Default to CM multistep sampler
       __a       =   CMStochasticIterativeScheduler(
           num_train_timesteps=4_0				,							sigma_min=0.0_0_2				,							sigma_max=8_0.0				,							)

       __a       =   {
           """unet""": unet,
           """scheduler""": scheduler,
       }

       return components

  def __a						(					self     :							Union[str, Any]				,							SCREAMING_SNAKE_CASE__     :							Dict				,							SCREAMING_SNAKE_CASE__     :							int=0						):





       '''simple docstring'''

       if str(SCREAMING_SNAKE_CASE__						).startswith("""mps"""						):
            __a       =   torch.manual_seed(SCREAMING_SNAKE_CASE__						)
       else:
            __a       =   torch.Generator(device=SCREAMING_SNAKE_CASE__						).manual_seed(SCREAMING_SNAKE_CASE__						)

       __a       =   {
           """batch_size""": 1,
           """num_inference_steps""": None,
           """timesteps""": [2_2, 0],
           """generator""": generator,
           """output_type""": """np""",
       }

       return inputs

  def __a						(					self     :							str						):





       '''simple docstring'''

       __a       =   """cpu"""  # ensure determinism for the device-dependent torch.Generator
       __a       =   self.get_dummy_components()
       __a       =   ConsistencyModelPipeline(**SCREAMING_SNAKE_CASE__						)
       __a       =   pipe.to(SCREAMING_SNAKE_CASE__						)
       pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__						)

       __a       =   self.get_dummy_inputs(SCREAMING_SNAKE_CASE__						)
       __a       =   pipe(**SCREAMING_SNAKE_CASE__						).images
       assert image.shape == (1, 3_2, 3_2, 3)

       __a       =   image[0, -3:, -3:, -1]
       __a       =   np.array([0.3_5_7_2, 0.6_2_7_3, 0.4_0_3_1, 0.3_9_6_1, 0.4_3_2_1, 0.5_7_3_0, 0.5_2_6_6, 0.4_7_8_0, 0.5_0_0_4]						)

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

  def __a						(					self     :							int						):





       '''simple docstring'''

       __a       =   """cpu"""  # ensure determinism for the device-dependent torch.Generator
       __a       =   self.get_dummy_components(class_cond=SCREAMING_SNAKE_CASE__						)
       __a       =   ConsistencyModelPipeline(**SCREAMING_SNAKE_CASE__						)
       __a       =   pipe.to(SCREAMING_SNAKE_CASE__						)
       pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__						)

       __a       =   self.get_dummy_inputs(SCREAMING_SNAKE_CASE__						)
       __a       =   0
       __a       =   pipe(**SCREAMING_SNAKE_CASE__						).images
       assert image.shape == (1, 3_2, 3_2, 3)

       __a       =   image[0, -3:, -3:, -1]
       __a       =   np.array([0.3_5_7_2, 0.6_2_7_3, 0.4_0_3_1, 0.3_9_6_1, 0.4_3_2_1, 0.5_7_3_0, 0.5_2_6_6, 0.4_7_8_0, 0.5_0_0_4]						)

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

  def __a						(					self     :							str						):





       '''simple docstring'''

       __a       =   """cpu"""  # ensure determinism for the device-dependent torch.Generator
       __a       =   self.get_dummy_components()
       __a       =   ConsistencyModelPipeline(**SCREAMING_SNAKE_CASE__						)
       __a       =   pipe.to(SCREAMING_SNAKE_CASE__						)
       pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__						)

       __a       =   self.get_dummy_inputs(SCREAMING_SNAKE_CASE__						)
       __a       =   1
       __a       =   None
       __a       =   pipe(**SCREAMING_SNAKE_CASE__						).images
       assert image.shape == (1, 3_2, 3_2, 3)

       __a       =   image[0, -3:, -3:, -1]
       __a       =   np.array([0.5_0_0_4, 0.5_0_0_4, 0.4_9_9_4, 0.5_0_0_8, 0.4_9_7_6, 0.5_0_1_8, 0.4_9_9_0, 0.4_9_8_2, 0.4_9_8_7]						)

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


  def __a						(					self     :							List[Any]						):





       '''simple docstring'''

       __a       =   """cpu"""  # ensure determinism for the device-dependent torch.Generator
       __a       =   self.get_dummy_components(class_cond=SCREAMING_SNAKE_CASE__						)
       __a       =   ConsistencyModelPipeline(**SCREAMING_SNAKE_CASE__						)
       __a       =   pipe.to(SCREAMING_SNAKE_CASE__						)
       pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__						)

       __a       =   self.get_dummy_inputs(SCREAMING_SNAKE_CASE__						)
       __a       =   1
       __a       =   None
       __a       =   0
       __a       =   pipe(**SCREAMING_SNAKE_CASE__						).images
       assert image.shape == (1, 3_2, 3_2, 3)

       __a       =   image[0, -3:, -3:, -1]
       __a       =   np.array([0.5_0_0_4, 0.5_0_0_4, 0.4_9_9_4, 0.5_0_0_8, 0.4_9_7_6, 0.5_0_1_8, 0.4_9_9_0, 0.4_9_8_2, 0.4_9_8_7]						)

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


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





  """simple docstring"""



  def __a						(					self     :							int						):





       '''simple docstring'''

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

  def __a						(					self     :							Union[str, Any]				,							SCREAMING_SNAKE_CASE__     :							Dict=0				,							SCREAMING_SNAKE_CASE__     :							str=False				,							SCREAMING_SNAKE_CASE__     :							int="cpu"				,							SCREAMING_SNAKE_CASE__     :							Union[str, Any]=torch.floataa				,							SCREAMING_SNAKE_CASE__     :							Union[str, Any]=(1, 3, 6_4, 6_4)						):





       '''simple docstring'''

       __a       =   torch.manual_seed(SCREAMING_SNAKE_CASE__						)

       __a       =   {
           """num_inference_steps""": None,
           """timesteps""": [2_2, 0],
           """class_labels""": 0,
           """generator""": generator,
           """output_type""": """np""",
       }

       if get_fixed_latents:
            __a       =   self.get_fixed_latents(seed=SCREAMING_SNAKE_CASE__				,							device=SCREAMING_SNAKE_CASE__				,							dtype=SCREAMING_SNAKE_CASE__				,							shape=SCREAMING_SNAKE_CASE__						)
            __a       =   latents

       return inputs

  def __a						(					self     :							Optional[int]				,							SCREAMING_SNAKE_CASE__     :							str=0				,							SCREAMING_SNAKE_CASE__     :							List[str]="cpu"				,							SCREAMING_SNAKE_CASE__     :							List[str]=torch.floataa				,							SCREAMING_SNAKE_CASE__     :							List[str]=(1, 3, 6_4, 6_4)						):





       '''simple docstring'''

       if type(SCREAMING_SNAKE_CASE__						) == str:
            __a       =   torch.device(SCREAMING_SNAKE_CASE__						)
       __a       =   torch.Generator(device=SCREAMING_SNAKE_CASE__						).manual_seed(SCREAMING_SNAKE_CASE__						)
       __a       =   randn_tensor(SCREAMING_SNAKE_CASE__				,							generator=SCREAMING_SNAKE_CASE__				,							device=SCREAMING_SNAKE_CASE__				,							dtype=SCREAMING_SNAKE_CASE__						)
       return latents

  def __a						(					self     :							str						):





       '''simple docstring'''

       __a       =   UNetaDModel.from_pretrained("""diffusers/consistency_models"""				,							subfolder="""diffusers_cd_imagenet64_l2"""						)
       __a       =   CMStochasticIterativeScheduler(
           num_train_timesteps=4_0				,							sigma_min=0.0_0_2				,							sigma_max=8_0.0				,							)
       __a       =   ConsistencyModelPipeline(unet=SCREAMING_SNAKE_CASE__				,							scheduler=SCREAMING_SNAKE_CASE__						)
       pipe.to(torch_device=SCREAMING_SNAKE_CASE__						)
       pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__						)

       __a       =   self.get_inputs()
       __a       =   pipe(**SCREAMING_SNAKE_CASE__						).images
       assert image.shape == (1, 6_4, 6_4, 3)

       __a       =   image[0, -3:, -3:, -1]

       __a       =   np.array([0.0_8_8_8, 0.0_8_8_1, 0.0_6_6_6, 0.0_4_7_9, 0.0_2_9_2, 0.0_1_9_5, 0.0_2_0_1, 0.0_1_6_3, 0.0_2_5_4]						)

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

  def __a						(					self     :							List[Any]						):





       '''simple docstring'''

       __a       =   UNetaDModel.from_pretrained("""diffusers/consistency_models"""				,							subfolder="""diffusers_cd_imagenet64_l2"""						)
       __a       =   CMStochasticIterativeScheduler(
           num_train_timesteps=4_0				,							sigma_min=0.0_0_2				,							sigma_max=8_0.0				,							)
       __a       =   ConsistencyModelPipeline(unet=SCREAMING_SNAKE_CASE__				,							scheduler=SCREAMING_SNAKE_CASE__						)
       pipe.to(torch_device=SCREAMING_SNAKE_CASE__						)
       pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__						)

       __a       =   self.get_inputs()
       __a       =   1
       __a       =   None
       __a       =   pipe(**SCREAMING_SNAKE_CASE__						).images
       assert image.shape == (1, 6_4, 6_4, 3)

       __a       =   image[0, -3:, -3:, -1]

       __a       =   np.array([0.0_3_4_0, 0.0_1_5_2, 0.0_0_6_3, 0.0_2_6_7, 0.0_2_2_1, 0.0_1_0_7, 0.0_4_1_6, 0.0_1_8_6, 0.0_2_1_7]						)

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

  @require_torch_a
  def __a						(					self     :							Tuple						):





       '''simple docstring'''

       __a       =   UNetaDModel.from_pretrained("""diffusers/consistency_models"""				,							subfolder="""diffusers_cd_imagenet64_l2"""						)
       __a       =   CMStochasticIterativeScheduler(
           num_train_timesteps=4_0				,							sigma_min=0.0_0_2				,							sigma_max=8_0.0				,							)
       __a       =   ConsistencyModelPipeline(unet=SCREAMING_SNAKE_CASE__				,							scheduler=SCREAMING_SNAKE_CASE__						)
       pipe.to(torch_device=SCREAMING_SNAKE_CASE__				,							torch_dtype=torch.floataa						)
       pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__						)

       __a       =   self.get_inputs(get_fixed_latents=SCREAMING_SNAKE_CASE__				,							device=SCREAMING_SNAKE_CASE__						)
       # Ensure usage of flash attention in torch 2.0
       with sdp_kernel(enable_flash=SCREAMING_SNAKE_CASE__				,							enable_math=SCREAMING_SNAKE_CASE__				,							enable_mem_efficient=SCREAMING_SNAKE_CASE__						):
            __a       =   pipe(**SCREAMING_SNAKE_CASE__						).images
       assert image.shape == (1, 6_4, 6_4, 3)

       __a       =   image[0, -3:, -3:, -1]

       __a       =   np.array([0.1_8_7_5, 0.1_4_2_8, 0.1_2_8_9, 0.2_1_5_1, 0.2_0_9_2, 0.1_4_7_7, 0.1_8_7_7, 0.1_6_4_1, 0.1_3_5_3]						)

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


  @require_torch_a
  def __a						(					self     :							Optional[Any]						):





       '''simple docstring'''

       __a       =   UNetaDModel.from_pretrained("""diffusers/consistency_models"""				,							subfolder="""diffusers_cd_imagenet64_l2"""						)
       __a       =   CMStochasticIterativeScheduler(
           num_train_timesteps=4_0				,							sigma_min=0.0_0_2				,							sigma_max=8_0.0				,							)
       __a       =   ConsistencyModelPipeline(unet=SCREAMING_SNAKE_CASE__				,							scheduler=SCREAMING_SNAKE_CASE__						)
       pipe.to(torch_device=SCREAMING_SNAKE_CASE__				,							torch_dtype=torch.floataa						)
       pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__						)

       __a       =   self.get_inputs(get_fixed_latents=SCREAMING_SNAKE_CASE__				,							device=SCREAMING_SNAKE_CASE__						)
       __a       =   1
       __a       =   None
       # Ensure usage of flash attention in torch 2.0
       with sdp_kernel(enable_flash=SCREAMING_SNAKE_CASE__				,							enable_math=SCREAMING_SNAKE_CASE__				,							enable_mem_efficient=SCREAMING_SNAKE_CASE__						):
            __a       =   pipe(**SCREAMING_SNAKE_CASE__						).images
       assert image.shape == (1, 6_4, 6_4, 3)

       __a       =   image[0, -3:, -3:, -1]

       __a       =   np.array([0.1_6_6_3, 0.1_9_4_8, 0.2_2_7_5, 0.1_6_8_0, 0.1_2_0_4, 0.1_2_4_5, 0.1_8_5_8, 0.1_3_3_8, 0.2_0_9_5]						)

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






