



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


				def __init__(							self      :  Tuple						, __UpperCamelCase      :  Union[str, Any]						, __UpperCamelCase      :  Any=14						, __UpperCamelCase      :  Dict=7						, __UpperCamelCase      :  List[Any]=True						, __UpperCamelCase      :  int=True						, __UpperCamelCase      :  Tuple=True						, __UpperCamelCase      :  List[Any]=True						, __UpperCamelCase      :  Union[str, Any]=True						, __UpperCamelCase      :  List[str]=99						, __UpperCamelCase      :  Optional[Any]=32						, __UpperCamelCase      :  List[Any]=5						, __UpperCamelCase      :  Union[str, Any]=4						, __UpperCamelCase      :  Dict=37						, __UpperCamelCase      :  str="gelu"						, __UpperCamelCase      :  str=0.1						, __UpperCamelCase      :  Any=0.1						, __UpperCamelCase      :  Any=512						, __UpperCamelCase      :  Optional[int]=16						, __UpperCamelCase      :  List[Any]=2						, __UpperCamelCase      :  int=0.0_2						, __UpperCamelCase      :  List[Any]=3						, __UpperCamelCase      :  Optional[int]=4						, __UpperCamelCase      :  Any=None						, )		->     Tuple:
											A      						=    parent
											A      						=    batch_size
											A      						=    seq_length
											A      						=    is_training
											A      						=    use_token_type_ids
											A      						=    use_input_mask
											A      						=    use_labels
											A      						=    use_mc_token_ids
											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
											A      						=    self.vocab_size - 1


				def __UpperCamelCase						(							self      :  Optional[int]       )		->     int:
											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
											if self.use_token_type_ids:
																		A      						=    ids_tensor([self.batch_size, self.seq_length]						, self.type_vocab_size       )

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

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

											A      						=    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 __UpperCamelCase						(							self      :  Union[str, Any]       )		->     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 __UpperCamelCase						(							self      :  Optional[int]						, __UpperCamelCase      :  Optional[int]						, __UpperCamelCase      :  Any						, __UpperCamelCase      :  List[str]						, __UpperCamelCase      :  Dict						, __UpperCamelCase      :  List[Any]						, *__UpperCamelCase      :  Optional[int]       )		->     List[str]:
											A      						=    CTRLModel(config=__UpperCamelCase       )
											model.to(__UpperCamelCase       )
											model.eval()

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

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


				def __UpperCamelCase						(							self      :  Union[str, Any]       )		->     Any:
											A      						=    self.prepare_config_and_inputs()

											(
											    (
											    A
											)						,   (
											    A
											)						,   (
											    A
											)						,   (
											    A
											)						,   (
											    A
											)						,   (
											    A
											)						,   (
											    A
											)						,   (
											    A
											)						,   (
											    A
											)						,   
											)      						=    config_and_inputs

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

											return config, inputs_dict


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





@require_torch
class        lowerCAmelCase__					(  _lowerCamelCase ,				_lowerCamelCase ,				_lowerCamelCase ,				unittest.TestCase       ):
				A_   :     Optional[Any]								=     (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
				A_   :     Optional[Any]								=     (CTRLLMHeadModel,) if is_torch_available() else ()
				A_   :     Optional[int]								=     (
				    {
				        'feature-extraction': CTRLModel,
				        'text-classification': CTRLForSequenceClassification,
				        'text-generation': CTRLLMHeadModel,
				        'zero-shot': CTRLForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				A_   :     str								=     True
				A_   :     Dict								=     False
				A_   :     Any								=     False


				def __UpperCamelCase						(							self      :  Optional[Any]						, __UpperCamelCase      :  Optional[int]						, __UpperCamelCase      :  Tuple						, __UpperCamelCase      :  Tuple						, __UpperCamelCase      :  List[Any]						, __UpperCamelCase      :  Tuple       )		->     List[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 __UpperCamelCase						(							self      :  List[Any]       )		->     Any:
											A      						=    CTRLModelTester(self       )
											A      						=    ConfigTester(self						, config_class=__UpperCamelCase						, n_embd=37       )


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


				def __UpperCamelCase						(							self      :  int       )		->     int:
											self.config_tester.run_common_tests()


				def __UpperCamelCase						(							self      :  Optional[int]       )		->     Optional[Any]:
											A      						=    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_ctrl_model(*__UpperCamelCase       )


				def __UpperCamelCase						(							self      :  Optional[Any]       )		->     Optional[int]:
											A      						=    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_lm_head_model(*__UpperCamelCase       )


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


				@slow
				def __UpperCamelCase						(							self      :  Any       )		->     Any:
											for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		A      						=    CTRLModel.from_pretrained(__UpperCamelCase       )
																		self.assertIsNotNone(__UpperCamelCase       )


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





@require_torch
class        lowerCAmelCase__					(  unittest.TestCase       ):


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


				@slow
				def __UpperCamelCase						(							self      :  Tuple       )		->     Optional[int]:
											A      						=    CTRLLMHeadModel.from_pretrained('ctrl'       )
											model.to(__UpperCamelCase       )
											A      						=    torch.tensor(
											    [[11_859, 0, 1_611, 8]]						, dtype=torch.long						, device=__UpperCamelCase       )  # Legal the president is
											A      						=    [
											    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

											A      						=    model.generate(__UpperCamelCase						, do_sample=__UpperCamelCase       )
											self.assertListEqual(output_ids[0].tolist()						, __UpperCamelCase       )




import random
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    UNetaDConditionModel,
    VideoToVideoSDPipeline,
)
from diffusers.utils import floats_tensor, is_xformers_available, skip_mps
from diffusers.utils.testing_utils import enable_full_determinism, slow, torch_device

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()



@skip_mps
class        lowerCAmelCase__					(  _lowerCamelCase ,				unittest.TestCase       ):
				A_   :     int								=     VideoToVideoSDPipeline
				A_   :     Optional[Any]								=     TEXT_GUIDED_IMAGE_VARIATION_PARAMS.union({'video'}       ) - {'image', 'width', 'height'}
				A_   :     List[str]								=     TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({'video'}       ) - {'image'}
				A_   :     Tuple								=     PipelineTesterMixin.required_optional_params - {'latents'}
				A_   :     Dict								=     False

				# No `output_type`.
				A_   :     str								=     frozenset(
				    [
				        'num_inference_steps',
				        'generator',
				        'latents',
				        'return_dict',
				        'callback',
				        'callback_steps',
				    ]       )


				def __UpperCamelCase						(							self      :  Optional[int]       )		->     List[Any]:
											torch.manual_seed(0       )
											A      						=    UNetaDConditionModel(
											    block_out_channels=(32, 64, 64, 64)						, layers_per_block=2						, sample_size=32						, in_channels=4						, out_channels=4						, down_block_types=('CrossAttnDownBlock3D', 'CrossAttnDownBlock3D', 'CrossAttnDownBlock3D', 'DownBlock3D')						, up_block_types=('UpBlock3D', 'CrossAttnUpBlock3D', 'CrossAttnUpBlock3D', 'CrossAttnUpBlock3D')						, cross_attention_dim=32						, attention_head_dim=4						, )
											A      						=    DDIMScheduler(
											    beta_start=0.0_0_0_8_5						, beta_end=0.0_1_2						, beta_schedule='scaled_linear'						, clip_sample=__UpperCamelCase						, set_alpha_to_one=__UpperCamelCase						, )
											torch.manual_seed(0       )
											A      						=    AutoencoderKL(
											    block_out_channels=[32, 64]						, in_channels=3						, out_channels=3						, down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']						, up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']						, latent_channels=4						, sample_size=128						, )
											torch.manual_seed(0       )
											A      						=    CLIPTextConfig(
											    bos_token_id=0						, eos_token_id=2						, hidden_size=32						, intermediate_size=37						, layer_norm_eps=1e-05						, num_attention_heads=4						, num_hidden_layers=5						, pad_token_id=1						, vocab_size=1_000						, hidden_act='gelu'						, projection_dim=512						, )
											A      						=    CLIPTextModel(__UpperCamelCase       )
											A      						=    CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'       )

											A      						=    {
											    'unet': unet,
											    'scheduler': scheduler,
											    'vae': vae,
											    'text_encoder': text_encoder,
											    'tokenizer': tokenizer,
											}
											return components


				def __UpperCamelCase						(							self      :  Optional[Any]						, __UpperCamelCase      :  Any						, __UpperCamelCase      :  str=0       )		->     Optional[int]:
											# 3 frames
											A      						=    floats_tensor((1, 3, 3, 32, 32)						, rng=random.Random(__UpperCamelCase       )       ).to(__UpperCamelCase       )

											if str(__UpperCamelCase       ).startswith('mps'       ):
																		A      						=    torch.manual_seed(__UpperCamelCase       )
											else:
																		A      						=    torch.Generator(device=__UpperCamelCase       ).manual_seed(__UpperCamelCase       )
											A      						=    {
											    'prompt': 'A painting of a squirrel eating a burger',
											    'video': video,
											    'generator': generator,
											    'num_inference_steps': 2,
											    'guidance_scale': 6.0,
											    'output_type': 'pt',
											}
											return inputs


				def __UpperCamelCase						(							self      :  Optional[int]       )		->     str:
											A      						=    'cpu'  # ensure determinism for the device-dependent torch.Generator
											A      						=    self.get_dummy_components()
											A      						=    VideoToVideoSDPipeline(**__UpperCamelCase       )
											A      						=    sd_pipe.to(__UpperCamelCase       )
											sd_pipe.set_progress_bar_config(disable=__UpperCamelCase       )

											A      						=    self.get_dummy_inputs(__UpperCamelCase       )
											A      						=    'np'
											A      						=    sd_pipe(**__UpperCamelCase       ).frames
											A      						=    frames[0][-3:, -3:, -1]

											assert frames[0].shape == (32, 32, 3)
											A      						=    np.array([106, 117, 113, 174, 137, 112, 148, 151, 131]       )

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


				@unittest.skipIf(
				    torch_device != 'cuda' or not is_xformers_available()						, reason='XFormers attention is only available with CUDA and `xformers` installed'						, )
				def __UpperCamelCase						(							self      :  int       )		->     Tuple:
											self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=__UpperCamelCase						, expected_max_diff=5e-3       )


				@unittest.skip(reason='Batching needs to be properly figured out first for this pipeline.'       )
				def __UpperCamelCase						(							self      :  Optional[int]       )		->     Any:
											pass


				@unittest.skip(reason='Batching needs to be properly figured out first for this pipeline.'       )
				def __UpperCamelCase						(							self      :  str       )		->     Any:
											pass


				@unittest.skip(reason='`num_images_per_prompt` argument is not supported for this pipeline.'       )
				def __UpperCamelCase						(							self      :  Union[str, Any]       )		->     Optional[Any]:
											pass


				def __UpperCamelCase						(							self      :  Any       )		->     Any:
											return super().test_progress_bar()





@slow
@skip_mps
class        lowerCAmelCase__					(  unittest.TestCase       ):


				def __UpperCamelCase						(							self      :  Optional[Any]       )		->     Dict:
											A      						=    VideoToVideoSDPipeline.from_pretrained('cerspense/zeroscope_v2_XL'						, torch_dtype=torch.floataa       )
											pipe.enable_model_cpu_offload()

											# 10 frames
											A      						=    torch.Generator(device='cpu'       ).manual_seed(0       )
											A      						=    torch.randn((1, 10, 3, 1_024, 576)						, generator=__UpperCamelCase       )
											A      						=    video.to('cuda'       )

											A      						=    'Spiderman is surfing'

											A      						=    pipe(__UpperCamelCase						, video=__UpperCamelCase						, generator=__UpperCamelCase						, num_inference_steps=3						, output_type='pt'       ).frames

											A      						=    np.array([-1.0_4_5_8_9_8_4, -1.1_2_7_9_2_9_7, -0.9_6_6_3_0_8_6, -0.9_1_5_0_3_9_0_6, -0.7_5_0_9_7_6_5_6]       )
											assert np.abs(video_frames.cpu().numpy()[0, 0, 0, 0, -5:] - expected_array       ).sum() < 1e-2