





"""simple docstring"""




import gc
import unittest

from transformers import CTRLConfig, 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 (
         CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
         CTRLForSequenceClassification,
         CTRLLMHeadModel,
         CTRLModel,
     )



class     _UpperCAmelCase   :

  """simple docstring"""


  def __init__(    self			:		Any ,  lowerCAmelCase_			:		Optional[Any] ,  lowerCAmelCase_			:		Tuple=1_4 ,  lowerCAmelCase_			:		List[str]=7 ,  lowerCAmelCase_			:		int=True ,  lowerCAmelCase_			:		Union[str, Any]=True ,  lowerCAmelCase_			:		List[Any]=True ,  lowerCAmelCase_			:		Tuple=True ,  lowerCAmelCase_			:		int=True ,  lowerCAmelCase_			:		Optional[Any]=9_9 ,  lowerCAmelCase_			:		Union[str, Any]=3_2 ,  lowerCAmelCase_			:		List[Any]=5 ,  lowerCAmelCase_			:		int=4 ,  lowerCAmelCase_			:		List[str]=3_7 ,  lowerCAmelCase_			:		Tuple="gelu" ,  lowerCAmelCase_			:		Optional[Any]=0.1 ,  lowerCAmelCase_			:		Union[str, Any]=0.1 ,  lowerCAmelCase_			:		Tuple=5_1_2 ,  lowerCAmelCase_			:		str=1_6 ,  lowerCAmelCase_			:		Optional[Any]=2 ,  lowerCAmelCase_			:		int=0.02 ,  lowerCAmelCase_			:		str=3 ,  lowerCAmelCase_			:		List[str]=4 ,  lowerCAmelCase_			:		Optional[Any]=None ,  )							->						Any:
     __lowerCAmelCase							     =     parent
     __lowerCAmelCase							     =     batch_size
     __lowerCAmelCase							     =     seq_length
     __lowerCAmelCase							     =     is_training
     __lowerCAmelCase							     =     use_token_type_ids
     __lowerCAmelCase							     =     use_input_mask
     __lowerCAmelCase							     =     use_labels
     __lowerCAmelCase							     =     use_mc_token_ids
     __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
     __lowerCAmelCase							     =     self.vocab_size - 1






  def     lowercase		(    self			:		str					)							->						List[str]:
     __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
     if self.use_token_type_ids:
        __lowerCAmelCase							     =     ids_tensor([self.batch_size, self.seq_length] ,  self.type_vocab_size					)

     __lowerCAmelCase							     =     None
     if self.use_mc_token_ids:
        __lowerCAmelCase							     =     ids_tensor([self.batch_size, self.num_choices] ,  self.seq_length					)

     __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()

     __lowerCAmelCase							     =     ids_tensor([self.num_hidden_layers, self.num_attention_heads] ,  2					)

     return (
         config,
         input_ids,
         input_mask,
         head_mask,
         token_type_ids,
         mc_token_ids,
         sequence_labels,
         token_labels,
         choice_labels,
     )






  def     lowercase		(    self			:		Optional[Any]					)							->						Tuple:
     return CTRLConfig(
         vocab_size=self.vocab_size ,  n_embd=self.hidden_size ,  n_layer=self.num_hidden_layers ,  n_head=self.num_attention_heads ,  n_positions=self.max_position_embeddings ,  pad_token_id=self.pad_token_id ,  )






  def     lowercase		(    self			:		str ,  lowerCAmelCase_			:		Any ,  lowerCAmelCase_			:		Tuple ,  lowerCAmelCase_			:		List[str] ,  lowerCAmelCase_			:		Optional[int] ,  lowerCAmelCase_			:		Tuple ,  *lowerCAmelCase_			:		Optional[int]					)							->						Dict:
     __lowerCAmelCase							     =     CTRLModel(config=SCREAMING_SNAKE_CASE_					)
     model.to(SCREAMING_SNAKE_CASE_					)
     model.eval()

     model(SCREAMING_SNAKE_CASE_ ,  token_type_ids=SCREAMING_SNAKE_CASE_ ,  head_mask=SCREAMING_SNAKE_CASE_					)
     model(SCREAMING_SNAKE_CASE_ ,  token_type_ids=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)					)
     self.parent.assertEqual(len(result.past_key_values					) ,  config.n_layer					)






  def     lowercase		(    self			:		Any ,  lowerCAmelCase_			:		Any ,  lowerCAmelCase_			:		Optional[int] ,  lowerCAmelCase_			:		Optional[Any] ,  lowerCAmelCase_			:		Dict ,  lowerCAmelCase_			:		Optional[int] ,  *lowerCAmelCase_			:		str					)							->						Any:
     __lowerCAmelCase							     =     CTRLLMHeadModel(SCREAMING_SNAKE_CASE_					)
     model.to(SCREAMING_SNAKE_CASE_					)
     model.eval()

     __lowerCAmelCase							     =     model(SCREAMING_SNAKE_CASE_ ,  token_type_ids=SCREAMING_SNAKE_CASE_ ,  labels=SCREAMING_SNAKE_CASE_					)
     self.parent.assertEqual(result.loss.shape ,  ()					)
     self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.seq_length, self.vocab_size)					)






  def     lowercase		(    self			:		Optional[int]					)							->						Optional[int]:
     __lowerCAmelCase							     =     self.prepare_config_and_inputs()

     (
         __lowerCAmelCase							
     )							     =     config_and_inputs

     __lowerCAmelCase							     =     {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """head_mask""": head_mask}

     return config, inputs_dict






  def     lowercase		(    self			:		Union[str, Any] ,  lowerCAmelCase_			:		Optional[int] ,  lowerCAmelCase_			:		int ,  lowerCAmelCase_			:		str ,  lowerCAmelCase_			:		str ,  *lowerCAmelCase_			:		int					)							->						Optional[int]:
     __lowerCAmelCase							     =     self.num_labels
     __lowerCAmelCase							     =     CTRLForSequenceClassification(SCREAMING_SNAKE_CASE_					)
     model.to(SCREAMING_SNAKE_CASE_					)
     model.eval()
     __lowerCAmelCase							     =     ids_tensor([self.batch_size] ,  self.type_sequence_label_size					)
     __lowerCAmelCase							     =     model(SCREAMING_SNAKE_CASE_ ,  token_type_ids=SCREAMING_SNAKE_CASE_ ,  labels=SCREAMING_SNAKE_CASE_					)
     self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.num_labels)					)





@require_torch
class     _UpperCAmelCase   (   _UpperCAmelCase		,   _UpperCAmelCase		,   _UpperCAmelCase		,   unittest.TestCase   ):

  """simple docstring"""


  a_      =			(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
  a_      =			(CTRLLMHeadModel,) if is_torch_available() else ()
  a_      =			(
      {
          'feature-extraction': CTRLModel,
          'text-classification': CTRLForSequenceClassification,
          'text-generation': CTRLLMHeadModel,
          'zero-shot': CTRLForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  a_      =			True
  a_      =			False
  a_      =			False






  def     lowercase		(    self			:		Union[str, Any] ,  lowerCAmelCase_			:		Tuple ,  lowerCAmelCase_			:		Dict ,  lowerCAmelCase_			:		Union[str, Any] ,  lowerCAmelCase_			:		Union[str, Any] ,  lowerCAmelCase_			:		Optional[int]					)							->						Tuple:
     if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
        # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
        # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny
        # config could not be created.
        return True

     return False






  def     lowercase		(    self			:		int					)							->						List[str]:
     __lowerCAmelCase							     =     CTRLModelTester(self					)
     __lowerCAmelCase							     =     ConfigTester(self ,  config_class=SCREAMING_SNAKE_CASE_ ,  n_embd=3_7					)






  def     lowercase		(    self			:		Optional[Any]					)							->						Dict:
     super().tearDown()
     # clean-up as much as possible GPU memory occupied by PyTorch
     gc.collect()
     torch.cuda.empty_cache()






  def     lowercase		(    self			:		List[Any]					)							->						List[str]:
     self.config_tester.run_common_tests()






  def     lowercase		(    self			:		str					)							->						Optional[int]:
     __lowerCAmelCase							     =     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_ctrl_model(*SCREAMING_SNAKE_CASE_					)






  def     lowercase		(    self			:		Tuple					)							->						Any:
     __lowerCAmelCase							     =     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_lm_head_model(*SCREAMING_SNAKE_CASE_					)






  @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'					)
  def     lowercase		(    self			:		List[str]					)							->						Optional[Any]:
     pass






  @slow
  def     lowercase		(    self			:		str					)							->						Any:
     for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        __lowerCAmelCase							     =     CTRLModel.from_pretrained(SCREAMING_SNAKE_CASE_					)
        self.assertIsNotNone(SCREAMING_SNAKE_CASE_					)






  @unittest.skip('The model doesn\'t support left padding'					)  # and it's not used enough to be worth fixing :)
  def     lowercase		(    self			:		Tuple					)							->						int:
     pass





@require_torch
class     _UpperCAmelCase   (   unittest.TestCase   ):

  """simple docstring"""


  def     lowercase		(    self			:		Union[str, Any]					)							->						Optional[Any]:
     super().tearDown()
     # clean-up as much as possible GPU memory occupied by PyTorch
     gc.collect()
     torch.cuda.empty_cache()






  @slow
  def     lowercase		(    self			:		List[Any]					)							->						Tuple:
     __lowerCAmelCase							     =     CTRLLMHeadModel.from_pretrained('ctrl'					)
     model.to(SCREAMING_SNAKE_CASE_					)
     __lowerCAmelCase							     =     torch.tensor(
         [[1_1_8_5_9, 0, 1_6_1_1, 8]] ,  dtype=torch.long ,  device=SCREAMING_SNAKE_CASE_					)  # Legal the president is
     __lowerCAmelCase							     =     [
         1_1_8_5_9,
         0,
         1_6_1_1,
         8,
         5,
         1_5_0,
         2_6_4_4_9,
         2,
         1_9,
         3_4_8,
         4_6_9,
         3,
         2_5_9_5,
         4_8,
         2_0_7_4_0,
         2_4_6_5_3_3,
         2_4_6_5_3_3,
         1_9,
         3_0,
         5,
     ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

     __lowerCAmelCase							     =     model.generate(SCREAMING_SNAKE_CASE_ ,  do_sample=SCREAMING_SNAKE_CASE_					)
     self.assertListEqual(output_ids[0].tolist() ,  SCREAMING_SNAKE_CASE_					)








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     _UpperCAmelCase   (   unittest.TestCase   ):

  """simple docstring"""


  def     lowercase		(    self			:		List[str]					)							->						int:
     # clean up the VRAM after each test
     super().tearDown()
     gc.collect()
     torch.cuda.empty_cache()






  @property
  def     lowercase		(    self			:		Union[str, Any]					)							->						Any:
     torch.manual_seed(0					)
     __lowerCAmelCase							     =     UNetaDModel(
         sample_size=(3_2, 6_4) ,  in_channels=1 ,  out_channels=1 ,  layers_per_block=2 ,  block_out_channels=(1_2_8, 1_2_8) ,  down_block_types=('AttnDownBlock2D', 'DownBlock2D') ,  up_block_types=('UpBlock2D', 'AttnUpBlock2D') ,  )
     return model






  @property
  def     lowercase		(    self			:		Optional[Any]					)							->						List[Any]:
     torch.manual_seed(0					)
     __lowerCAmelCase							     =     UNetaDConditionModel(
         sample_size=(6_4, 3_2) ,  in_channels=1 ,  out_channels=1 ,  layers_per_block=2 ,  block_out_channels=(1_2_8, 1_2_8) ,  down_block_types=('CrossAttnDownBlock2D', 'DownBlock2D') ,  up_block_types=('UpBlock2D', 'CrossAttnUpBlock2D') ,  cross_attention_dim=1_0 ,  )
     return model






  @property
  def     lowercase		(    self			:		Dict					)							->						Optional[Any]:
     torch.manual_seed(0					)
     __lowerCAmelCase							     =     AutoencoderKL(
         sample_size=(1_2_8, 6_4) ,  in_channels=1 ,  out_channels=1 ,  latent_channels=1 ,  layers_per_block=2 ,  block_out_channels=(1_2_8, 1_2_8) ,  down_block_types=('DownEncoderBlock2D', 'DownEncoderBlock2D') ,  up_block_types=('UpDecoderBlock2D', 'UpDecoderBlock2D') ,  )
     __lowerCAmelCase							     =     UNetaDModel(
         sample_size=(6_4, 3_2) ,  in_channels=1 ,  out_channels=1 ,  layers_per_block=2 ,  block_out_channels=(1_2_8, 1_2_8) ,  down_block_types=('AttnDownBlock2D', 'DownBlock2D') ,  up_block_types=('UpBlock2D', 'AttnUpBlock2D') ,  )
     return vqvae, unet






  @slow
  def     lowercase		(    self			:		Dict					)							->						Optional[Any]:
     __lowerCAmelCase							     =     'cpu'  # ensure determinism for the device-dependent torch.Generator
     __lowerCAmelCase							     =     Mel(
         x_res=self.dummy_unet.config.sample_size[1] ,  y_res=self.dummy_unet.config.sample_size[0] ,  )

     __lowerCAmelCase							     =     DDPMScheduler()
     __lowerCAmelCase							     =     AudioDiffusionPipeline(vqvae=lowerCAmelCase_ ,  unet=self.dummy_unet ,  mel=lowerCAmelCase_ ,  scheduler=lowerCAmelCase_					)
     __lowerCAmelCase							     =     pipe.to(lowerCAmelCase_					)
     pipe.set_progress_bar_config(disable=lowerCAmelCase_					)

     __lowerCAmelCase							     =     torch.Generator(device=lowerCAmelCase_					).manual_seed(4_2					)
     __lowerCAmelCase							     =     pipe(generator=lowerCAmelCase_ ,  steps=4					)
     __lowerCAmelCase							     =     output.audios[0]
     __lowerCAmelCase							     =     output.images[0]

     __lowerCAmelCase							     =     torch.Generator(device=lowerCAmelCase_					).manual_seed(4_2					)
     __lowerCAmelCase							     =     pipe(generator=lowerCAmelCase_ ,  steps=4 ,  return_dict=lowerCAmelCase_					)
     __lowerCAmelCase							     =     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]
     )
     __lowerCAmelCase							     =     np.frombuffer(image.tobytes() ,  dtype='uint8'					)[:1_0]
     __lowerCAmelCase							     =     np.frombuffer(image_from_tuple.tobytes() ,  dtype='uint8'					)[:1_0]
     __lowerCAmelCase							     =     np.array([6_9, 2_5_5, 2_5_5, 2_5_5, 0, 0, 7_7, 1_8_1, 1_2, 1_2_7]					)

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

     __lowerCAmelCase							     =     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] ,  )

     __lowerCAmelCase							     =     DDIMScheduler()
     __lowerCAmelCase							     =     self.dummy_vqvae_and_unet
     __lowerCAmelCase							     =     AudioDiffusionPipeline(
         vqvae=self.dummy_vqvae_and_unet[0] ,  unet=dummy_vqvae_and_unet[1] ,  mel=lowerCAmelCase_ ,  scheduler=lowerCAmelCase_					)
     __lowerCAmelCase							     =     pipe.to(lowerCAmelCase_					)
     pipe.set_progress_bar_config(disable=lowerCAmelCase_					)

     np.random.seed(0					)
     __lowerCAmelCase							     =     np.random.uniform(-1 ,  1 ,  ((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,)					)
     __lowerCAmelCase							     =     torch.Generator(device=lowerCAmelCase_					).manual_seed(4_2					)
     __lowerCAmelCase							     =     pipe(raw_audio=lowerCAmelCase_ ,  generator=lowerCAmelCase_ ,  start_step=5 ,  steps=1_0					)
     __lowerCAmelCase							     =     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]
     )
     __lowerCAmelCase							     =     np.frombuffer(image.tobytes() ,  dtype='uint8'					)[:1_0]
     __lowerCAmelCase							     =     np.array([1_2_0, 1_1_7, 1_1_0, 1_0_9, 1_3_8, 1_6_7, 1_3_8, 1_4_8, 1_3_2, 1_2_1]					)

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

     __lowerCAmelCase							     =     self.dummy_unet_condition
     __lowerCAmelCase							     =     AudioDiffusionPipeline(
         vqvae=self.dummy_vqvae_and_unet[0] ,  unet=lowerCAmelCase_ ,  mel=lowerCAmelCase_ ,  scheduler=lowerCAmelCase_					)
     __lowerCAmelCase							     =     pipe.to(lowerCAmelCase_					)
     pipe.set_progress_bar_config(disable=lowerCAmelCase_					)

     np.random.seed(0					)
     __lowerCAmelCase							     =     torch.rand((1, 1, 1_0)					)
     __lowerCAmelCase							     =     pipe(generator=lowerCAmelCase_ ,  encoding=lowerCAmelCase_					)
     __lowerCAmelCase							     =     output.images[0]
     __lowerCAmelCase							     =     np.frombuffer(image.tobytes() ,  dtype='uint8'					)[:1_0]
     __lowerCAmelCase							     =     np.array([1_0_7, 1_0_3, 1_2_0, 1_2_7, 1_4_2, 1_2_2, 1_1_3, 1_2_2, 9_7, 1_1_1]					)

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





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

  """simple docstring"""


  def     lowercase		(    self			:		List[Any]					)							->						Optional[int]:
     # clean up the VRAM after each test
     super().tearDown()
     gc.collect()
     torch.cuda.empty_cache()






  def     lowercase		(    self			:		Union[str, Any]					)							->						Optional[int]:
     __lowerCAmelCase							     =     torch_device

     __lowerCAmelCase							     =     DiffusionPipeline.from_pretrained('teticio/audio-diffusion-ddim-256'					)
     __lowerCAmelCase							     =     pipe.to(lowerCAmelCase_					)
     pipe.set_progress_bar_config(disable=lowerCAmelCase_					)

     __lowerCAmelCase							     =     torch.Generator(device=lowerCAmelCase_					).manual_seed(4_2					)
     __lowerCAmelCase							     =     pipe(generator=lowerCAmelCase_					)
     __lowerCAmelCase							     =     output.audios[0]
     __lowerCAmelCase							     =     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]
     __lowerCAmelCase							     =     np.frombuffer(image.tobytes() ,  dtype='uint8'					)[:1_0]
     __lowerCAmelCase							     =     np.array([1_5_1, 1_6_7, 1_5_4, 1_4_4, 1_2_2, 1_3_4, 1_2_1, 1_0_5, 7_0, 2_6]					)

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

