

import unittest

from parameterized import parameterized

from transformers import AutoTokenizer, GPTNeoXConfig, is_torch_available, set_seed
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import (
        GPTNeoXForCausalLM,
        GPTNeoXForQuestionAnswering,
        GPTNeoXForSequenceClassification,
        GPTNeoXForTokenClassification,
        GPTNeoXModel,
    )
class  _A		:

  def __init__(				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE=13 ,					_SCREAMING_SNAKE_CASE=7 ,					_SCREAMING_SNAKE_CASE=True ,					_SCREAMING_SNAKE_CASE=True ,					_SCREAMING_SNAKE_CASE=True ,					_SCREAMING_SNAKE_CASE=True ,					_SCREAMING_SNAKE_CASE=99 ,					_SCREAMING_SNAKE_CASE=64 ,					_SCREAMING_SNAKE_CASE=5 ,					_SCREAMING_SNAKE_CASE=4 ,					_SCREAMING_SNAKE_CASE=37 ,					_SCREAMING_SNAKE_CASE="gelu" ,					_SCREAMING_SNAKE_CASE=0.1 ,					_SCREAMING_SNAKE_CASE=0.1 ,					_SCREAMING_SNAKE_CASE=512 ,					_SCREAMING_SNAKE_CASE=16 ,					_SCREAMING_SNAKE_CASE=2 ,					_SCREAMING_SNAKE_CASE=0.02 ,					_SCREAMING_SNAKE_CASE=3 ,					_SCREAMING_SNAKE_CASE=4 ,					_SCREAMING_SNAKE_CASE=None ,					):





        """simple docstring"""







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

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size					)

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

        SCREAMING_SNAKE_CASE_				:  int           =     None
        if self.use_labels:
              SCREAMING_SNAKE_CASE_				:  str           =     ids_tensor([self.batch_size, self.seq_length] ,					self.num_labels					)

        SCREAMING_SNAKE_CASE_				:  Tuple           =     self.get_config()

        return config, input_ids, input_mask, token_labels

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        return GPTNeoXConfig(
            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=A_ ,					initializer_range=self.initializer_range ,					pad_token_id=self.pad_token_id ,					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  List[str]           =     self.prepare_config_and_inputs()

        SCREAMING_SNAKE_CASE_				:  Dict           =     True

        return config, input_ids, input_mask, token_labels

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     GPTNeoXModel(config=A_					)
        model.to(A_					)
        model.eval()
        SCREAMING_SNAKE_CASE_				:  Any           =     model(A_ ,					attention_mask=A_					)
        SCREAMING_SNAKE_CASE_				:  Dict           =     model(A_					)
        self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.hidden_size)					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Dict           =     True
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     GPTNeoXModel(A_					)
        model.to(A_					)
        model.eval()
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     model(A_ ,					attention_mask=A_					)
        self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.hidden_size)					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







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

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     self.num_labels
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     GPTNeoXForQuestionAnswering(A_					)
        model.to(A_					)
        model.eval()
        SCREAMING_SNAKE_CASE_				:  int           =     model(A_ ,					attention_mask=A_					)
        self.parent.assertEqual(result.start_logits.shape ,					(self.batch_size, self.seq_length)					)
        self.parent.assertEqual(result.end_logits.shape ,					(self.batch_size, self.seq_length)					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Dict           =     self.num_labels
        SCREAMING_SNAKE_CASE_				:  List[str]           =     GPTNeoXForSequenceClassification(A_					)
        model.to(A_					)
        model.eval()
        SCREAMING_SNAKE_CASE_				:  str           =     ids_tensor([self.batch_size] ,					self.type_sequence_label_size					)
        SCREAMING_SNAKE_CASE_				:  List[str]           =     model(A_ ,					attention_mask=A_ ,					labels=A_					)
        self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.num_labels)					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     self.num_labels
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     GPTNeoXForTokenClassification(A_					)
        model.to(A_					)
        model.eval()
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     model(A_ ,					attention_mask=A_ ,					labels=A_					)
        self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length, self.num_labels)					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  int           =     True
        SCREAMING_SNAKE_CASE_				:  Tuple           =     GPTNeoXForCausalLM(config=A_					)
        model.to(A_					)
        model.eval()

        # first forward pass
        SCREAMING_SNAKE_CASE_				:  str           =     model(A_ ,					attention_mask=A_ ,					use_cache=A_					)
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     outputs.past_key_values

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

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

        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     model(A_ ,					attention_mask=A_ ,					output_hidden_states=A_					)
        SCREAMING_SNAKE_CASE_				:  Any           =     output_from_no_past["hidden_states"][0]
        SCREAMING_SNAKE_CASE_				:  List[str]           =     model(
            A_ ,					attention_mask=A_ ,					past_key_values=A_ ,					output_hidden_states=A_ ,					)["hidden_states"][0]

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



  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     self.prepare_config_and_inputs()
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     config_and_inputs
        SCREAMING_SNAKE_CASE_				:  List[str]           =     {"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    :      str       =  (
      (
          GPTNeoXModel,
          GPTNeoXForCausalLM,
          GPTNeoXForQuestionAnswering,
          GPTNeoXForSequenceClassification,
          GPTNeoXForTokenClassification,
      )
      if is_torch_available()
      else ()
  )
  SCREAMING_SNAKE_CASE    :      str       =  (GPTNeoXForCausalLM,) if is_torch_available() else ()
  SCREAMING_SNAKE_CASE    :      Optional[Any]       =  (
      {
          'feature-extraction': GPTNeoXModel,
          'question-answering': GPTNeoXForQuestionAnswering,
          'text-classification': GPTNeoXForSequenceClassification,
          'text-generation': GPTNeoXForCausalLM,
          'token-classification': GPTNeoXForTokenClassification,
          'zero-shot': GPTNeoXForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  SCREAMING_SNAKE_CASE    :      Any       =  False
  SCREAMING_SNAKE_CASE    :      List[str]       =  False
  SCREAMING_SNAKE_CASE    :      Union[str, Any]       =  False
  SCREAMING_SNAKE_CASE    :      Dict       =  False

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     GPTNeoXModelTester(self					)
        SCREAMING_SNAKE_CASE_				:  Any           =     ConfigTester(self ,					config_class=A_ ,					hidden_size=64 ,					num_attention_heads=8					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        self.config_tester.run_common_tests()

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  str           =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(A_ ,					A_ ,					A_					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  List[Any]           =     self.model_tester.prepare_config_and_inputs_for_decoder()
        self.model_tester.create_and_check_model_as_decoder(A_ ,					A_ ,					A_					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Dict           =     self.model_tester.prepare_config_and_inputs_for_decoder()

        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     None

        self.model_tester.create_and_check_model_as_decoder(A_ ,					A_ ,					A_					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  int           =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_decoder_model_past_large_inputs(A_ ,					A_ ,					A_					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Dict           =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_causal_lm(*A_					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  List[str]           =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*A_					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*A_					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  int           =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*A_					)

  @unittest.skip(reason='Feed forward chunking is not implemented'					)
  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        pass



  @parameterized.expand([('linear',), ('dynamic',)]					)
  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  List[Any]           =     self.model_tester.prepare_config_and_inputs_for_common()
        SCREAMING_SNAKE_CASE_				:  Tuple           =     ids_tensor([1, 10] ,					config.vocab_size					)
        SCREAMING_SNAKE_CASE_				:  List[str]           =     ids_tensor([1, int(config.max_position_embeddings * 1.5					)] ,					config.vocab_size					)

        set_seed(42					)  # Fixed seed at init time so the two models get the same random weights
        SCREAMING_SNAKE_CASE_				:  Dict           =     GPTNeoXModel(A_					)
        original_model.to(A_					)
        original_model.eval()
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     original_model(A_					).last_hidden_state
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     original_model(A_					).last_hidden_state

        set_seed(42					)  # Fixed seed at init time so the two models get the same random weights
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     {"type": scaling_type, "factor": 10.0}
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     GPTNeoXModel(A_					)
        scaled_model.to(A_					)
        scaled_model.eval()
        SCREAMING_SNAKE_CASE_				:  Dict           =     scaled_model(A_					).last_hidden_state
        SCREAMING_SNAKE_CASE_				:  Any           =     scaled_model(A_					).last_hidden_state

        # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original
        # maximum sequence length, so the outputs for the short input should match.
        if scaling_type == "dynamic":
              self.assertTrue(torch.allclose(A_ ,					A_ ,					atol=1e-5					)					)
        else:
              self.assertFalse(torch.allclose(A_ ,					A_ ,					atol=1e-5					)					)

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

@require_torch
class  _A		(  unittest.TestCase):

  @slow
  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     AutoTokenizer.from_pretrained('EleutherAI/pythia-410m-deduped'					)
        for checkpointing in [True, False]:
              SCREAMING_SNAKE_CASE_				:  str           =     GPTNeoXForCausalLM.from_pretrained('EleutherAI/pythia-410m-deduped'					)

              if checkpointing:
                    model.gradient_checkpointing_enable()
              else:
                    model.gradient_checkpointing_disable()
              model.to(A_					)

              SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     tokenizer('My favorite food is' ,					return_tensors='pt'					).to(A_					)
              # The hub repo. is updated on 2023-04-04, resulting in poor outputs.
              # See: https://github.com/huggingface/transformers/pull/24193
              SCREAMING_SNAKE_CASE_				:  Optional[int]           =     "My favorite food is a good old-fashioned, old-fashioned, old-fashioned.\n\nI'm not sure"

              SCREAMING_SNAKE_CASE_				:  List[Any]           =     model.generate(**A_ ,					do_sample=A_ ,					max_new_tokens=20					)
              SCREAMING_SNAKE_CASE_				:  int           =     tokenizer.batch_decode(A_					)[0]

              self.assertEqual(A_ ,					A_					)









import gc
import unittest

import numpy as np
import torch

from diffusers import (
    AudioDiffusionPipeline,
    AutoencoderKL,
    DDIMScheduler,
    DDPMScheduler,
    DiffusionPipeline,
    Mel,
    UNetaDConditionModel,
    UNetaDModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()
class  _A		(  unittest.TestCase):

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







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

  @property
  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        torch.manual_seed(0					)
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     UNetaDModel(
            sample_size=(32, 64) ,					in_channels=1 ,					out_channels=1 ,					layers_per_block=2 ,					block_out_channels=(128, 128) ,					down_block_types=('AttnDownBlock2D', 'DownBlock2D') ,					up_block_types=('UpBlock2D', 'AttnUpBlock2D') ,					)
        return model

  @property
  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        torch.manual_seed(0					)
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     UNetaDConditionModel(
            sample_size=(64, 32) ,					in_channels=1 ,					out_channels=1 ,					layers_per_block=2 ,					block_out_channels=(128, 128) ,					down_block_types=('CrossAttnDownBlock2D', 'DownBlock2D') ,					up_block_types=('UpBlock2D', 'CrossAttnUpBlock2D') ,					cross_attention_dim=10 ,					)
        return model

  @property
  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        torch.manual_seed(0					)
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     AutoencoderKL(
            sample_size=(128, 64) ,					in_channels=1 ,					out_channels=1 ,					latent_channels=1 ,					layers_per_block=2 ,					block_out_channels=(128, 128) ,					down_block_types=('DownEncoderBlock2D', 'DownEncoderBlock2D') ,					up_block_types=('UpDecoderBlock2D', 'UpDecoderBlock2D') ,					)
        SCREAMING_SNAKE_CASE_				:  Dict           =     UNetaDModel(
            sample_size=(64, 32) ,					in_channels=1 ,					out_channels=1 ,					layers_per_block=2 ,					block_out_channels=(128, 128) ,					down_block_types=('AttnDownBlock2D', 'DownBlock2D') ,					up_block_types=('UpBlock2D', 'AttnUpBlock2D') ,					)
        return vqvae, unet



  @slow
  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     'cpu'  # ensure determinism for the device-dependent torch.Generator
        SCREAMING_SNAKE_CASE_				:  Any           =     Mel(
            x_res=self.dummy_unet.config.sample_size[1] ,					y_res=self.dummy_unet.config.sample_size[0] ,					)

        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     DDPMScheduler()
        SCREAMING_SNAKE_CASE_				:  str           =     AudioDiffusionPipeline(vqvae=_SCREAMING_SNAKE_CASE ,					unet=self.dummy_unet ,					mel=_SCREAMING_SNAKE_CASE ,					scheduler=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  Dict           =     pipe.to(_SCREAMING_SNAKE_CASE					)
        pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE					)

        SCREAMING_SNAKE_CASE_				:  Dict           =     torch.Generator(device=_SCREAMING_SNAKE_CASE					).manual_seed(42					)
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     pipe(generator=_SCREAMING_SNAKE_CASE ,					steps=4					)
        SCREAMING_SNAKE_CASE_				:  Any           =     output.audios[0]
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     output.images[0]

        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     torch.Generator(device=_SCREAMING_SNAKE_CASE					).manual_seed(42					)
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     pipe(generator=_SCREAMING_SNAKE_CASE ,					steps=4 ,					return_dict=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     output[0][0]

        assert audio.shape == (1, (self.dummy_unet.config.sample_size[1] - 1) * mel.hop_length)
        assert (
            image.height == self.dummy_unet.config.sample_size[0]
            and image.width == self.dummy_unet.config.sample_size[1]
        )
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     np.frombuffer(image.tobytes() ,					dtype='uint8'					)[:10]
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     np.frombuffer(image_from_tuple.tobytes() ,					dtype='uint8'					)[:10]
        SCREAMING_SNAKE_CASE_				:  Any           =     np.array([69, 255, 255, 255, 0, 0, 77, 181, 12, 127]					)

        assert np.abs(image_slice.flatten() - expected_slice					).max() == 0
        assert np.abs(image_from_tuple_slice.flatten() - expected_slice					).max() == 0

        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     Mel(
            x_res=self.dummy_vqvae_and_unet[0].config.sample_size[1] ,					y_res=self.dummy_vqvae_and_unet[0].config.sample_size[0] ,					)

        SCREAMING_SNAKE_CASE_				:  str           =     DDIMScheduler()
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     self.dummy_vqvae_and_unet
        SCREAMING_SNAKE_CASE_				:  int           =     AudioDiffusionPipeline(
            vqvae=self.dummy_vqvae_and_unet[0] ,					unet=dummy_vqvae_and_unet[1] ,					mel=_SCREAMING_SNAKE_CASE ,					scheduler=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  str           =     pipe.to(_SCREAMING_SNAKE_CASE					)
        pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE					)

        np.random.seed(0					)
        SCREAMING_SNAKE_CASE_				:  Dict           =     np.random.uniform(-1 ,					1 ,					((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,)					)
        SCREAMING_SNAKE_CASE_				:  str           =     torch.Generator(device=_SCREAMING_SNAKE_CASE					).manual_seed(42					)
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     pipe(raw_audio=_SCREAMING_SNAKE_CASE ,					generator=_SCREAMING_SNAKE_CASE ,					start_step=5 ,					steps=10					)
        SCREAMING_SNAKE_CASE_				:  int           =     output.images[0]

        assert (
            image.height == self.dummy_vqvae_and_unet[0].config.sample_size[0]
            and image.width == self.dummy_vqvae_and_unet[0].config.sample_size[1]
        )
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     np.frombuffer(image.tobytes() ,					dtype='uint8'					)[:10]
        SCREAMING_SNAKE_CASE_				:  int           =     np.array([120, 117, 110, 109, 138, 167, 138, 148, 132, 121]					)

        assert np.abs(image_slice.flatten() - expected_slice					).max() == 0

        SCREAMING_SNAKE_CASE_				:  Dict           =     self.dummy_unet_condition
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     AudioDiffusionPipeline(
            vqvae=self.dummy_vqvae_and_unet[0] ,					unet=_SCREAMING_SNAKE_CASE ,					mel=_SCREAMING_SNAKE_CASE ,					scheduler=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  Dict           =     pipe.to(_SCREAMING_SNAKE_CASE					)
        pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE					)

        np.random.seed(0					)
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     torch.rand((1, 1, 10)					)
        SCREAMING_SNAKE_CASE_				:  Any           =     pipe(generator=_SCREAMING_SNAKE_CASE ,					encoding=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     output.images[0]
        SCREAMING_SNAKE_CASE_				:  Dict           =     np.frombuffer(image.tobytes() ,					dtype='uint8'					)[:10]
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     np.array([107, 103, 120, 127, 142, 122, 113, 122, 97, 111]					)

        assert np.abs(image_slice.flatten() - expected_slice					).max() == 0

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

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







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



  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     torch_device

        SCREAMING_SNAKE_CASE_				:  str           =     DiffusionPipeline.from_pretrained('teticio/audio-diffusion-ddim-256'					)
        SCREAMING_SNAKE_CASE_				:  Dict           =     pipe.to(_SCREAMING_SNAKE_CASE					)
        pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE					)

        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     torch.Generator(device=_SCREAMING_SNAKE_CASE					).manual_seed(42					)
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     pipe(generator=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  Any           =     output.audios[0]
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     output.images[0]

        assert audio.shape == (1, (pipe.unet.config.sample_size[1] - 1) * pipe.mel.hop_length)
        assert image.height == pipe.unet.config.sample_size[0] and image.width == pipe.unet.config.sample_size[1]
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     np.frombuffer(image.tobytes() ,					dtype='uint8'					)[:10]
        SCREAMING_SNAKE_CASE_				:  Any           =     np.array([151, 167, 154, 144, 122, 134, 121, 105, 70, 26]					)

        assert np.abs(image_slice.flatten() - expected_slice					).max() == 0






