




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 			lowerCamelCase_				:


 '''simple docstring'''







 def __init__(				self    :						Optional[Any]							,							_lowerCAmelCase    :						Union[str, Any]							,							_lowerCAmelCase    :						List[Any]=14							,							_lowerCAmelCase    :						int=7							,							_lowerCAmelCase    :						Union[str, Any]=True							,							_lowerCAmelCase    :						Dict=True							,							_lowerCAmelCase    :						Optional[int]=True							,							_lowerCAmelCase    :						Optional[Any]=True							,							_lowerCAmelCase    :						Any=True							,							_lowerCAmelCase    :						List[Any]=99							,							_lowerCAmelCase    :						Dict=32							,							_lowerCAmelCase    :						Optional[int]=5							,							_lowerCAmelCase    :						int=4							,							_lowerCAmelCase    :						Union[str, Any]=37							,							_lowerCAmelCase    :						List[str]="gelu"							,							_lowerCAmelCase    :						Any=0.1							,							_lowerCAmelCase    :						str=0.1							,							_lowerCAmelCase    :						Tuple=512							,							_lowerCAmelCase    :						Dict=16							,							_lowerCAmelCase    :						Any=2							,							_lowerCAmelCase    :						Tuple=0.02							,							_lowerCAmelCase    :						List[Any]=3							,							_lowerCAmelCase    :						Union[str, Any]=4							,							_lowerCAmelCase    :						List[str]=None							,							):
       SCREAMING_SNAKE_CASE_      		=     parent
       SCREAMING_SNAKE_CASE_      		=     batch_size
       SCREAMING_SNAKE_CASE_      		=     seq_length
       SCREAMING_SNAKE_CASE_      		=     is_training
       SCREAMING_SNAKE_CASE_      		=     use_token_type_ids
       SCREAMING_SNAKE_CASE_      		=     use_input_mask
       SCREAMING_SNAKE_CASE_      		=     use_labels
       SCREAMING_SNAKE_CASE_      		=     use_mc_token_ids
       SCREAMING_SNAKE_CASE_      		=     vocab_size
       SCREAMING_SNAKE_CASE_      		=     hidden_size
       SCREAMING_SNAKE_CASE_      		=     num_hidden_layers
       SCREAMING_SNAKE_CASE_      		=     num_attention_heads
       SCREAMING_SNAKE_CASE_      		=     intermediate_size
       SCREAMING_SNAKE_CASE_      		=     hidden_act
       SCREAMING_SNAKE_CASE_      		=     hidden_dropout_prob
       SCREAMING_SNAKE_CASE_      		=     attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE_      		=     max_position_embeddings
       SCREAMING_SNAKE_CASE_      		=     type_vocab_size
       SCREAMING_SNAKE_CASE_      		=     type_sequence_label_size
       SCREAMING_SNAKE_CASE_      		=     initializer_range
       SCREAMING_SNAKE_CASE_      		=     num_labels
       SCREAMING_SNAKE_CASE_      		=     num_choices
       SCREAMING_SNAKE_CASE_      		=     scope
       SCREAMING_SNAKE_CASE_      		=     self.vocab_size - 1



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

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

       SCREAMING_SNAKE_CASE_      		=     None
       if self.use_token_type_ids:
             SCREAMING_SNAKE_CASE_      		=     ids_tensor([self.batch_size, self.seq_length]							,							self.type_vocab_size		)

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

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

       SCREAMING_SNAKE_CASE_      		=     self.get_config()

       SCREAMING_SNAKE_CASE_      		=     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  lowerCAmelCase_    (				self    :						Optional[int]		):
       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  lowerCAmelCase_    (				self    :						str							,							_lowerCAmelCase    :						str							,							_lowerCAmelCase    :						Optional[int]							,							_lowerCAmelCase    :						List[str]							,							_lowerCAmelCase    :						str							,							_lowerCAmelCase    :						Optional[int]							,							*_lowerCAmelCase    :						int		):
       SCREAMING_SNAKE_CASE_      		=     CTRLModel(config=_lowerCAmelCase		)
       model.to(_lowerCAmelCase		)
       model.eval()

       model(_lowerCAmelCase							,							token_type_ids=_lowerCAmelCase							,							head_mask=_lowerCAmelCase		)
       model(_lowerCAmelCase							,							token_type_ids=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase		)
       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  lowerCAmelCase_    (				self    :						str							,							_lowerCAmelCase    :						Any							,							_lowerCAmelCase    :						Any							,							_lowerCAmelCase    :						int							,							_lowerCAmelCase    :						Union[str, Any]							,							_lowerCAmelCase    :						Dict							,							*_lowerCAmelCase    :						Union[str, Any]		):
       SCREAMING_SNAKE_CASE_      		=     CTRLLMHeadModel(_lowerCAmelCase		)
       model.to(_lowerCAmelCase		)
       model.eval()

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



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

       (
           (
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					
       )      		=     config_and_inputs

       SCREAMING_SNAKE_CASE_      		=     {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'head_mask': head_mask}

       return config, inputs_dict




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





@require_torch
class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE			,  _SCREAMING_SNAKE_CASE			,  _SCREAMING_SNAKE_CASE			,  unittest.TestCase   ):


 '''simple docstring'''







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



 def  lowerCAmelCase_    (				self    :						Dict							,							_lowerCAmelCase    :						List[str]							,							_lowerCAmelCase    :						Union[str, Any]							,							_lowerCAmelCase    :						str							,							_lowerCAmelCase    :						Optional[Any]							,							_lowerCAmelCase    :						Optional[Any]		):
       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  lowerCAmelCase_    (				self    :						Tuple		):
       SCREAMING_SNAKE_CASE_      		=     CTRLModelTester(self		)
       SCREAMING_SNAKE_CASE_      		=     ConfigTester(self							,							config_class=_lowerCAmelCase							,							n_embd=37		)



 def  lowerCAmelCase_    (				self    :						int		):
       super().tearDown()
       # clean-up as much as possible GPU memory occupied by PyTorch
       gc.collect()
       torch.cuda.empty_cache()



 def  lowerCAmelCase_    (				self    :						Optional[Any]		):
       self.config_tester.run_common_tests()



 def  lowerCAmelCase_    (				self    :						List[str]		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_ctrl_model(*_lowerCAmelCase		)



 def  lowerCAmelCase_    (				self    :						List[str]		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_lm_head_model(*_lowerCAmelCase		)



 @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'		)
 def  lowerCAmelCase_    (				self    :						int		):
       pass



 @slow
 def  lowerCAmelCase_    (				self    :						str		):
       for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             SCREAMING_SNAKE_CASE_      		=     CTRLModel.from_pretrained(_lowerCAmelCase		)
             self.assertIsNotNone(_lowerCAmelCase		)




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





@require_torch
class 			lowerCamelCase_				( unittest.TestCase   ):


 '''simple docstring'''







 def  lowerCAmelCase_    (				self    :						str		):
       super().tearDown()
       # clean-up as much as possible GPU memory occupied by PyTorch
       gc.collect()
       torch.cuda.empty_cache()




 @slow
 def  lowerCAmelCase_    (				self    :						int		):
       SCREAMING_SNAKE_CASE_      		=     CTRLLMHeadModel.from_pretrained('ctrl'		)
       model.to(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     torch.tensor(
           [[11_859, 0, 1_611, 8]]							,							dtype=torch.long							,							device=_lowerCAmelCase		)  # Legal the president is
       SCREAMING_SNAKE_CASE_      		=     [
           11_859,
           0,
           1_611,
           8,
           5,
           150,
           26_449,
           2,
           19,
           348,
           469,
           3,
           2_595,
           48,
           20_740,
           246_533,
           246_533,
           19,
           30,
           5,
       ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

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

"""simple docstring"""





from math import acos, sin
from typing import List, Tuple, Union

import numpy as np
import torch
from PIL import Image

from ...models import AutoencoderKL, UNetaDConditionModel
from ...schedulers import DDIMScheduler, DDPMScheduler
from ...utils import randn_tensor
from ..pipeline_utils import AudioPipelineOutput, BaseOutput, DiffusionPipeline, ImagePipelineOutput
from .mel import Mel






class        _UpperCAmelCase(       lowerCamelCase      ):
		lowercase__         = ['vqvae']



		def __init__(   self							,						__a							,						__a							,						__a							,						__a							,						)			->			List[str]:






							'''simple docstring'''
							super().__init__()
							self.register_modules(unet=__a							,						scheduler=__a							,						mel=__a							,						vqvae=__a)



		def 		UpperCAmelCase				(   self)			->			int:






							'''simple docstring'''
							return 50 if isinstance(self.scheduler							,						__a) else 10_00



		@torch.no_grad()
		def __call__(   self							,						__a = 1							,						__a = None							,						__a = None							,						__a = 0							,						__a = 0							,						__a = None							,						__a = None							,						__a = 0							,						__a = 0							,						__a = None							,						__a = 0							,						__a = None							,						__a = None							,						__a=True							,						)			->			Union[
		    Union[AudioPipelineOutput, ImagePipelineOutput],
		    Tuple[List[Image.Image], Tuple[int, List[np.ndarray]]],
		]:






							'''simple docstring'''
							_UpperCamelCase									=    steps or self.get_default_steps()
							self.scheduler.set_timesteps(__a)
							_UpperCamelCase									=    step_generator or generator
							# For backwards compatibility
							if type(self.unet.config.sample_size) == int:
												_UpperCamelCase									=    (self.unet.config.sample_size, self.unet.config.sample_size)
							if noise is None:
												_UpperCamelCase									=    randn_tensor(
												    (
												        batch_size,
												        self.unet.config.in_channels,
												        self.unet.config.sample_size[0],
												        self.unet.config.sample_size[1],
												    )							,						generator=__a							,						device=self.device							,						)
							_UpperCamelCase									=    noise
							_UpperCamelCase									=    None

							if audio_file is not None or raw_audio is not None:
												self.mel.load_audio(__a							,						__a)
												_UpperCamelCase									=    self.mel.audio_slice_to_image(__a)
												_UpperCamelCase									=    np.frombuffer(input_image.tobytes()							,						dtype='''uint8''').reshape(
												    (input_image.height, input_image.width))
												_UpperCamelCase									=    (input_image / 2_55) * 2 - 1
												_UpperCamelCase									=    torch.tensor(input_image[np.newaxis, :, :]							,						dtype=torch.float).to(self.device)

												if self.vqvae is not None:
																	_UpperCamelCase									=    self.vqvae.encode(torch.unsqueeze(__a							,						0)).latent_dist.sample(
																	    generator=__a)[0]
																	_UpperCamelCase									=    self.vqvae.config.scaling_factor * input_images

												if start_step > 0:
																	_UpperCamelCase									=    self.scheduler.add_noise(__a							,						__a							,						self.scheduler.timesteps[start_step - 1])

												_UpperCamelCase									=    (
												    self.unet.config.sample_size[1] * self.mel.get_sample_rate() / self.mel.x_res / self.mel.hop_length
												)
												_UpperCamelCase									=    int(mask_start_secs * pixels_per_second)
												_UpperCamelCase									=    int(mask_end_secs * pixels_per_second)
												_UpperCamelCase									=    self.scheduler.add_noise(__a							,						__a							,						torch.tensor(self.scheduler.timesteps[start_step:]))

							for step, t in enumerate(self.progress_bar(self.scheduler.timesteps[start_step:])):
												if isinstance(self.unet							,						__a):
																	_UpperCamelCase									=    self.unet(__a							,						__a							,						__a)['''sample''']
												else:
																	_UpperCamelCase									=    self.unet(__a							,						__a)['''sample''']

												if isinstance(self.scheduler							,						__a):
																	_UpperCamelCase									=    self.scheduler.step(
																	    model_output=__a							,						timestep=__a							,						sample=__a							,						eta=__a							,						generator=__a							,						)['''prev_sample''']
												else:
																	_UpperCamelCase									=    self.scheduler.step(
																	    model_output=__a							,						timestep=__a							,						sample=__a							,						generator=__a							,						)['''prev_sample''']

												if mask is not None:
																	if mask_start > 0:
																						_UpperCamelCase									=    mask[:, step, :, :mask_start]
																	if mask_end > 0:
																						_UpperCamelCase									=    mask[:, step, :, -mask_end:]

							if self.vqvae is not None:
												# 0.18215 was scaling factor used in training to ensure unit variance
												_UpperCamelCase									=    1 / self.vqvae.config.scaling_factor * images
												_UpperCamelCase									=    self.vqvae.decode(__a)['''sample''']

							_UpperCamelCase									=    (images / 2 + 0.5).clamp(0							,						1)
							_UpperCamelCase									=    images.cpu().permute(0							,						2							,						3							,						1).numpy()
							_UpperCamelCase									=    (images * 2_55).round().astype('''uint8''')
							_UpperCamelCase									=    list(
							    (Image.fromarray(_[:, :, 0]) for _ in images)
							    if images.shape[3] == 1
							    else (Image.fromarray(__a							,						mode='''RGB''').convert('''L''') for _ in images))

							_UpperCamelCase									=    [self.mel.image_to_audio(__a) for _ in images]
							if not return_dict:
												return images, (self.mel.get_sample_rate(), audios)

							return BaseOutput(**AudioPipelineOutput(np.array(__a)[:, np.newaxis, :])							,						**ImagePipelineOutput(__a))



		@torch.no_grad()
		def 		UpperCAmelCase				(   self							,						__a							,						__a = 50)			->			np.ndarray:






							'''simple docstring'''
							assert isinstance(self.scheduler							,						__a)
							self.scheduler.set_timesteps(__a)
							_UpperCamelCase									=    np.array(
							    [np.frombuffer(image.tobytes()							,						dtype='''uint8''').reshape((1, image.height, image.width)) for image in images])
							_UpperCamelCase									=    (sample / 2_55) * 2 - 1
							_UpperCamelCase									=    torch.Tensor(__a).to(self.device)

							for t in self.progress_bar(torch.flip(self.scheduler.timesteps							,						(0,))):
												_UpperCamelCase									=    t - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps
												_UpperCamelCase									=    self.scheduler.alphas_cumprod[t]
												_UpperCamelCase									=    (
												    self.scheduler.alphas_cumprod[prev_timestep]
												    if prev_timestep >= 0
												    else self.scheduler.final_alpha_cumprod
												)
												_UpperCamelCase									=    1 - alpha_prod_t
												_UpperCamelCase									=    self.unet(__a							,						__a)['''sample''']
												_UpperCamelCase									=    (1 - alpha_prod_t_prev) ** 0.5 * model_output
												_UpperCamelCase									=    (sample - pred_sample_direction) * alpha_prod_t_prev ** (-0.5)
												_UpperCamelCase									=    sample * alpha_prod_t ** 0.5 + beta_prod_t ** 0.5 * model_output

							return sample




		@staticmethod
		def 		UpperCAmelCase				(   __a							,						__a							,						__a)			->			torch.Tensor:






							'''simple docstring'''
							_UpperCamelCase									=    acos(torch.dot(torch.flatten(__a)							,						torch.flatten(__a)) / torch.norm(__a) / torch.norm(__a))
							return sin((1 - alpha) * theta) * xa / sin(__a) + sin(alpha * theta) * xa / sin(__a)






