






import gc
import random
import tempfile
import unittest

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

from diffusers import AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.semantic_stable_diffusion import SemanticStableDiffusionPipeline as StableDiffusionPipeline
from diffusers.utils import floats_tensor, nightly, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()

class      UpperCamelCase_							(				unittest.TestCase       ):




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




						@property
						def      snake_case__(    self       )			->						List[str]:
													_a  :    str			=						1
													_a  :    str			=						3
													_a  :    int			=						(32, 32)

													_a  :    Optional[int]			=						floats_tensor((batch_size, num_channels) + sizes		,	rng=random.Random(0       )       ).to(__SCREAMING_SNAKE_CASE       )
													return image




						@property
						def      snake_case__(    self       )			->						str:
													torch.manual_seed(0       )
													_a  :    Dict			=						UNetaDConditionModel(
													    block_out_channels=(32, 64)		,	layers_per_block=2		,	sample_size=32		,	in_channels=4		,	out_channels=4		,	down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')		,	up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')		,	cross_attention_dim=32		,	)
													return model




						@property
						def      snake_case__(    self       )			->						str:
													torch.manual_seed(0       )
													_a  :    Dict			=						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		,	)
													return model




						@property
						def      snake_case__(    self       )			->						Tuple:
													torch.manual_seed(0       )
													_a  :    List[Any]			=						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		,	)
													return CLIPTextModel(__SCREAMING_SNAKE_CASE       )




						@property
						def      snake_case__(    self       )			->						int:
													def extract(*lowercase		,	**lowercase       ):

																				class      UpperCamelCase_							:




																										def __init__(    self       )			->						Any:
																																	_a  :    Union[str, Any]			=						torch.ones([0]       )







																										def      snake_case__(    self		,	lowercase       )			->						List[str]:
																																	self.pixel_values.to(__SCREAMING_SNAKE_CASE       )
																																	return self

																				return Out()

													return extract




						def      snake_case__(    self       )			->						Union[str, Any]:
													_a  :    Dict			=						'cpu'  # ensure determinism for the device-dependent torch.Generator
													_a  :    Optional[int]			=						self.dummy_cond_unet
													_a  :    Optional[int]			=						DDIMScheduler(
													    beta_start=0.00085		,	beta_end=0.012		,	beta_schedule='''scaled_linear'''		,	clip_sample=__SCREAMING_SNAKE_CASE		,	set_alpha_to_one=__SCREAMING_SNAKE_CASE		,	)

													_a  :    Optional[Any]			=						self.dummy_vae
													_a  :    Optional[int]			=						self.dummy_text_encoder
													_a  :    List[str]			=						CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''       )

													# make sure here that pndm scheduler skips prk
													_a  :    Any			=						StableDiffusionPipeline(
													    unet=__SCREAMING_SNAKE_CASE		,	scheduler=__SCREAMING_SNAKE_CASE		,	vae=__SCREAMING_SNAKE_CASE		,	text_encoder=__SCREAMING_SNAKE_CASE		,	tokenizer=__SCREAMING_SNAKE_CASE		,	safety_checker=__SCREAMING_SNAKE_CASE		,	feature_extractor=self.dummy_extractor		,	)
													_a  :    Any			=						sd_pipe.to(__SCREAMING_SNAKE_CASE       )
													sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE       )

													_a  :    Dict			=						'A painting of a squirrel eating a burger'

													_a  :    Dict			=						torch.Generator(device=__SCREAMING_SNAKE_CASE       ).manual_seed(0       )
													_a  :    List[str]			=						sd_pipe([prompt]		,	generator=__SCREAMING_SNAKE_CASE		,	guidance_scale=6.0		,	num_inference_steps=2		,	output_type='''np'''       )
													_a  :    List[str]			=						output.images

													_a  :    Tuple			=						torch.Generator(device=__SCREAMING_SNAKE_CASE       ).manual_seed(0       )
													_a  :    Union[str, Any]			=						sd_pipe(
													    [prompt]		,	generator=__SCREAMING_SNAKE_CASE		,	guidance_scale=6.0		,	num_inference_steps=2		,	output_type='''np'''		,	return_dict=__SCREAMING_SNAKE_CASE		,	)[0]

													_a  :    Tuple			=						image[0, -3:, -3:, -1]
													_a  :    Union[str, Any]			=						image_from_tuple[0, -3:, -3:, -1]

													assert image.shape == (1, 64, 64, 3)
													_a  :    Optional[int]			=						np.array([0.5753, 0.6114, 0.5001, 0.5034, 0.5470, 0.4729, 0.4971, 0.4867, 0.4867]       )

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




						def      snake_case__(    self       )			->						Union[str, Any]:
													_a  :    Dict			=						'cpu'  # ensure determinism for the device-dependent torch.Generator
													_a  :    Any			=						self.dummy_cond_unet
													_a  :    int			=						PNDMScheduler(skip_prk_steps=__SCREAMING_SNAKE_CASE       )
													_a  :    List[str]			=						self.dummy_vae
													_a  :    int			=						self.dummy_text_encoder
													_a  :    int			=						CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''       )

													# make sure here that pndm scheduler skips prk
													_a  :    List[str]			=						StableDiffusionPipeline(
													    unet=__SCREAMING_SNAKE_CASE		,	scheduler=__SCREAMING_SNAKE_CASE		,	vae=__SCREAMING_SNAKE_CASE		,	text_encoder=__SCREAMING_SNAKE_CASE		,	tokenizer=__SCREAMING_SNAKE_CASE		,	safety_checker=__SCREAMING_SNAKE_CASE		,	feature_extractor=self.dummy_extractor		,	)
													_a  :    Any			=						sd_pipe.to(__SCREAMING_SNAKE_CASE       )
													sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE       )

													_a  :    Union[str, Any]			=						'A painting of a squirrel eating a burger'
													_a  :    List[Any]			=						torch.Generator(device=__SCREAMING_SNAKE_CASE       ).manual_seed(0       )
													_a  :    Optional[int]			=						sd_pipe([prompt]		,	generator=__SCREAMING_SNAKE_CASE		,	guidance_scale=6.0		,	num_inference_steps=2		,	output_type='''np'''       )

													_a  :    Tuple			=						output.images

													_a  :    int			=						torch.Generator(device=__SCREAMING_SNAKE_CASE       ).manual_seed(0       )
													_a  :    Union[str, Any]			=						sd_pipe(
													    [prompt]		,	generator=__SCREAMING_SNAKE_CASE		,	guidance_scale=6.0		,	num_inference_steps=2		,	output_type='''np'''		,	return_dict=__SCREAMING_SNAKE_CASE		,	)[0]

													_a  :    Optional[int]			=						image[0, -3:, -3:, -1]
													_a  :    List[Any]			=						image_from_tuple[0, -3:, -3:, -1]

													assert image.shape == (1, 64, 64, 3)
													_a  :    Tuple			=						np.array([0.5122, 0.5712, 0.4825, 0.5053, 0.5646, 0.4769, 0.5179, 0.4894, 0.4994]       )

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




						def      snake_case__(    self       )			->						int:
													_a  :    List[Any]			=						StableDiffusionPipeline.from_pretrained(
													    '''hf-internal-testing/tiny-stable-diffusion-lms-pipe'''		,	safety_checker=__SCREAMING_SNAKE_CASE       )
													assert isinstance(__SCREAMING_SNAKE_CASE		,	__SCREAMING_SNAKE_CASE       )
													assert isinstance(pipe.scheduler		,	__SCREAMING_SNAKE_CASE       )
													assert pipe.safety_checker is None

													_a  :    str			=						pipe('''example prompt'''		,	num_inference_steps=2       ).images[0]
													assert image is not None

													# check that there's no error when saving a pipeline with one of the models being None
													with tempfile.TemporaryDirectory() as tmpdirname:
																				pipe.save_pretrained(__SCREAMING_SNAKE_CASE       )
																				_a  :    Tuple			=						StableDiffusionPipeline.from_pretrained(__SCREAMING_SNAKE_CASE       )

													# sanity check that the pipeline still works
													assert pipe.safety_checker is None
													_a  :    Dict			=						pipe('''example prompt'''		,	num_inference_steps=2       ).images[0]
													assert image is not None







						@unittest.skipIf(torch_device != '''cuda'''		,	'''This test requires a GPU'''       )
						def      snake_case__(    self       )			->						str:
													_a  :    Any			=						self.dummy_cond_unet
													_a  :    List[str]			=						PNDMScheduler(skip_prk_steps=__SCREAMING_SNAKE_CASE       )
													_a  :    Tuple			=						self.dummy_vae
													_a  :    Tuple			=						self.dummy_text_encoder
													_a  :    Dict			=						CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''       )

													# put models in fp16
													_a  :    List[Any]			=						unet.half()
													_a  :    Union[str, Any]			=						vae.half()
													_a  :    Dict			=						bert.half()

													# make sure here that pndm scheduler skips prk
													_a  :    Any			=						StableDiffusionPipeline(
													    unet=__SCREAMING_SNAKE_CASE		,	scheduler=__SCREAMING_SNAKE_CASE		,	vae=__SCREAMING_SNAKE_CASE		,	text_encoder=__SCREAMING_SNAKE_CASE		,	tokenizer=__SCREAMING_SNAKE_CASE		,	safety_checker=__SCREAMING_SNAKE_CASE		,	feature_extractor=self.dummy_extractor		,	)
													_a  :    Dict			=						sd_pipe.to(__SCREAMING_SNAKE_CASE       )
													sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE       )

													_a  :    Any			=						'A painting of a squirrel eating a burger'
													_a  :    Optional[int]			=						sd_pipe([prompt]		,	num_inference_steps=2		,	output_type='''np'''       ).images

													assert image.shape == (1, 64, 64, 3)

@nightly
@require_torch_gpu
class      UpperCamelCase_							(				unittest.TestCase       ):




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




						def      snake_case__(    self       )			->						Optional[Any]:
													_a  :    int			=						'cuda'
													_a  :    Tuple			=						StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5'''       )
													_a  :    str			=						pipe.to(__SCREAMING_SNAKE_CASE       )
													pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE       )

													_a  :    Dict			=						'a photo of a cat'
													_a  :    List[Any]			=						{
													    'editing_prompt': ['sunglasses'],
													    'reverse_editing_direction': [False],
													    'edit_warmup_steps': 10,
													    'edit_guidance_scale': 6,
													    'edit_threshold': 0.95,
													    'edit_momentum_scale': 0.5,
													    'edit_mom_beta': 0.6,
													}

													_a  :    int			=						3
													_a  :    Union[str, Any]			=						7

													# no sega enabled
													_a  :    Optional[Any]			=						torch.Generator(__SCREAMING_SNAKE_CASE       )
													generator.manual_seed(__SCREAMING_SNAKE_CASE       )
													_a  :    Union[str, Any]			=						pipe(
													    [prompt]		,	generator=__SCREAMING_SNAKE_CASE		,	guidance_scale=__SCREAMING_SNAKE_CASE		,	num_inference_steps=50		,	output_type='''np'''		,	width=512		,	height=512		,	)

													_a  :    List[str]			=						output.images
													_a  :    Any			=						image[0, -3:, -3:, -1]
													_a  :    Optional[Any]			=						[
													    0.34673113,
													    0.38492733,
													    0.37597352,
													    0.34086335,
													    0.35650748,
													    0.35579205,
													    0.3384763,
													    0.34340236,
													    0.3573271,
													]

													assert image.shape == (1, 512, 512, 3)

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

													# with sega enabled
													# generator = torch.manual_seed(seed)
													generator.manual_seed(__SCREAMING_SNAKE_CASE       )
													_a  :    Dict			=						pipe(
													    [prompt]		,	generator=__SCREAMING_SNAKE_CASE		,	guidance_scale=__SCREAMING_SNAKE_CASE		,	num_inference_steps=50		,	output_type='''np'''		,	width=512		,	height=512		,	**__SCREAMING_SNAKE_CASE		,	)

													_a  :    Dict			=						output.images
													_a  :    Tuple			=						image[0, -3:, -3:, -1]
													_a  :    List[str]			=						[
													    0.41887826,
													    0.37728766,
													    0.30138272,
													    0.41416335,
													    0.41664985,
													    0.36283392,
													    0.36191246,
													    0.43364465,
													    0.43001732,
													]

													assert image.shape == (1, 512, 512, 3)

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




						def      snake_case__(    self       )			->						Tuple:
													_a  :    Union[str, Any]			=						'cuda'
													_a  :    Union[str, Any]			=						StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5'''       )
													_a  :    List[Any]			=						pipe.to(__SCREAMING_SNAKE_CASE       )
													pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE       )

													_a  :    Optional[int]			=						'an image of a crowded boulevard, realistic, 4k'
													_a  :    Tuple			=						{
													    'editing_prompt': 'crowd, crowded, people',
													    'reverse_editing_direction': True,
													    'edit_warmup_steps': 10,
													    'edit_guidance_scale': 8.3,
													    'edit_threshold': 0.9,
													    'edit_momentum_scale': 0.5,
													    'edit_mom_beta': 0.6,
													}

													_a  :    Union[str, Any]			=						9
													_a  :    Optional[Any]			=						7

													# no sega enabled
													_a  :    Optional[Any]			=						torch.Generator(__SCREAMING_SNAKE_CASE       )
													generator.manual_seed(__SCREAMING_SNAKE_CASE       )
													_a  :    Dict			=						pipe(
													    [prompt]		,	generator=__SCREAMING_SNAKE_CASE		,	guidance_scale=__SCREAMING_SNAKE_CASE		,	num_inference_steps=50		,	output_type='''np'''		,	width=512		,	height=512		,	)

													_a  :    Any			=						output.images
													_a  :    Any			=						image[0, -3:, -3:, -1]
													_a  :    Optional[int]			=						[
													    0.43497998,
													    0.91814065,
													    0.7540739,
													    0.55580205,
													    0.8467265,
													    0.5389691,
													    0.62574506,
													    0.58897763,
													    0.50926757,
													]

													assert image.shape == (1, 512, 512, 3)

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

													# with sega enabled
													# generator = torch.manual_seed(seed)
													generator.manual_seed(__SCREAMING_SNAKE_CASE       )
													_a  :    Any			=						pipe(
													    [prompt]		,	generator=__SCREAMING_SNAKE_CASE		,	guidance_scale=__SCREAMING_SNAKE_CASE		,	num_inference_steps=50		,	output_type='''np'''		,	width=512		,	height=512		,	**__SCREAMING_SNAKE_CASE		,	)

													_a  :    List[Any]			=						output.images
													_a  :    Dict			=						image[0, -3:, -3:, -1]
													_a  :    List[Any]			=						[
													    0.3089719,
													    0.30500144,
													    0.29016042,
													    0.30630964,
													    0.325687,
													    0.29419225,
													    0.2908091,
													    0.28723598,
													    0.27696294,
													]

													assert image.shape == (1, 512, 512, 3)

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




						def      snake_case__(    self       )			->						Optional[Any]:
													_a  :    str			=						'cuda'
													_a  :    List[str]			=						StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5'''       )
													_a  :    Union[str, Any]			=						pipe.to(__SCREAMING_SNAKE_CASE       )
													pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE       )

													_a  :    Any			=						'a castle next to a river'
													_a  :    str			=						{
													    'editing_prompt': ['boat on a river, boat', 'monet, impression, sunrise'],
													    'reverse_editing_direction': False,
													    'edit_warmup_steps': [15, 18],
													    'edit_guidance_scale': 6,
													    'edit_threshold': [0.9, 0.8],
													    'edit_momentum_scale': 0.5,
													    'edit_mom_beta': 0.6,
													}

													_a  :    Optional[Any]			=						48
													_a  :    Dict			=						7

													# no sega enabled
													_a  :    str			=						torch.Generator(__SCREAMING_SNAKE_CASE       )
													generator.manual_seed(__SCREAMING_SNAKE_CASE       )
													_a  :    Union[str, Any]			=						pipe(
													    [prompt]		,	generator=__SCREAMING_SNAKE_CASE		,	guidance_scale=__SCREAMING_SNAKE_CASE		,	num_inference_steps=50		,	output_type='''np'''		,	width=512		,	height=512		,	)

													_a  :    Any			=						output.images
													_a  :    str			=						image[0, -3:, -3:, -1]
													_a  :    Union[str, Any]			=						[
													    0.75163555,
													    0.76037145,
													    0.61785,
													    0.9189673,
													    0.8627701,
													    0.85189694,
													    0.8512813,
													    0.87012076,
													    0.8312857,
													]

													assert image.shape == (1, 512, 512, 3)

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

													# with sega enabled
													# generator = torch.manual_seed(seed)
													generator.manual_seed(__SCREAMING_SNAKE_CASE       )
													_a  :    Union[str, Any]			=						pipe(
													    [prompt]		,	generator=__SCREAMING_SNAKE_CASE		,	guidance_scale=__SCREAMING_SNAKE_CASE		,	num_inference_steps=50		,	output_type='''np'''		,	width=512		,	height=512		,	**__SCREAMING_SNAKE_CASE		,	)

													_a  :    Union[str, Any]			=						output.images
													_a  :    Optional[Any]			=						image[0, -3:, -3:, -1]
													_a  :    Optional[Any]			=						[
													    0.73553365,
													    0.7537271,
													    0.74341905,
													    0.66480356,
													    0.6472925,
													    0.63039416,
													    0.64812905,
													    0.6749717,
													    0.6517102,
													]

													assert image.shape == (1, 512, 512, 3)

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







						def      snake_case__(    self       )			->						List[Any]:
													_a  :    Any			=						'cuda'
													_a  :    Tuple			=						StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5'''		,	torch_dtype=torch.floataa       )
													_a  :    List[str]			=						pipe.to(__SCREAMING_SNAKE_CASE       )
													pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE       )

													_a  :    List[str]			=						'a photo of a cat'
													_a  :    Tuple			=						{
													    'editing_prompt': ['sunglasses'],
													    'reverse_editing_direction': [False],
													    'edit_warmup_steps': 10,
													    'edit_guidance_scale': 6,
													    'edit_threshold': 0.95,
													    'edit_momentum_scale': 0.5,
													    'edit_mom_beta': 0.6,
													}

													_a  :    Optional[int]			=						3
													_a  :    Optional[int]			=						7

													# no sega enabled
													_a  :    int			=						torch.Generator(__SCREAMING_SNAKE_CASE       )
													generator.manual_seed(__SCREAMING_SNAKE_CASE       )
													_a  :    Any			=						pipe(
													    [prompt]		,	generator=__SCREAMING_SNAKE_CASE		,	guidance_scale=__SCREAMING_SNAKE_CASE		,	num_inference_steps=50		,	output_type='''np'''		,	width=512		,	height=512		,	)

													_a  :    Dict			=						output.images
													_a  :    Tuple			=						image[0, -3:, -3:, -1]
													_a  :    Any			=						[
													    0.34887695,
													    0.3876953,
													    0.375,
													    0.34423828,
													    0.3581543,
													    0.35717773,
													    0.3383789,
													    0.34570312,
													    0.359375,
													]

													assert image.shape == (1, 512, 512, 3)

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

													# with sega enabled
													# generator = torch.manual_seed(seed)
													generator.manual_seed(__SCREAMING_SNAKE_CASE       )
													_a  :    str			=						pipe(
													    [prompt]		,	generator=__SCREAMING_SNAKE_CASE		,	guidance_scale=__SCREAMING_SNAKE_CASE		,	num_inference_steps=50		,	output_type='''np'''		,	width=512		,	height=512		,	**__SCREAMING_SNAKE_CASE		,	)

													_a  :    str			=						output.images
													_a  :    Dict			=						image[0, -3:, -3:, -1]
													_a  :    Union[str, Any]			=						[
													    0.42285156,
													    0.36914062,
													    0.29077148,
													    0.42041016,
													    0.41918945,
													    0.35498047,
													    0.3618164,
													    0.4423828,
													    0.43115234,
													]

													assert image.shape == (1, 512, 512, 3)

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

'''simple docstring'''

from __future__ import annotations

import unittest

from transformers import CTRLConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import tensorflow as tf

   from transformers.models.ctrl.modeling_tf_ctrl import (
       TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
       TFCTRLForSequenceClassification,
       TFCTRLLMHeadModel,
       TFCTRLModel,
   )


class 		_a     (							SCREAMING_SNAKE_CASE  ):







   """simple docstring"""




   def __init__(       self    ,__SCREAMING_SNAKE_CASE    ,):
    SCREAMING_SNAKE_CASE  :  Dict								=		parent
    SCREAMING_SNAKE_CASE  :  int								=		13
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		7
    SCREAMING_SNAKE_CASE  :  List[Any]								=		True
    SCREAMING_SNAKE_CASE  :  Dict								=		True
    SCREAMING_SNAKE_CASE  :  Optional[int]								=		True
    SCREAMING_SNAKE_CASE  :  int								=		True
    SCREAMING_SNAKE_CASE  :  str								=		True
    SCREAMING_SNAKE_CASE  :  int								=		99
    SCREAMING_SNAKE_CASE  :  List[str]								=		32
    SCREAMING_SNAKE_CASE  :  List[str]								=		2
    SCREAMING_SNAKE_CASE  :  Optional[Any]								=		4
    SCREAMING_SNAKE_CASE  :  Tuple								=		37
    SCREAMING_SNAKE_CASE  :  str								=		'gelu'
    SCREAMING_SNAKE_CASE  :  Tuple								=		0.1
    SCREAMING_SNAKE_CASE  :  Any								=		0.1
    SCREAMING_SNAKE_CASE  :  List[str]								=		512
    SCREAMING_SNAKE_CASE  :  List[Any]								=		16
    SCREAMING_SNAKE_CASE  :  List[Any]								=		2
    SCREAMING_SNAKE_CASE  :  Any								=		0.02
    SCREAMING_SNAKE_CASE  :  List[str]								=		3
    SCREAMING_SNAKE_CASE  :  Any								=		4
    SCREAMING_SNAKE_CASE  :  List[Any]								=		None
    SCREAMING_SNAKE_CASE  :  Optional[int]								=		self.vocab_size - 1






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  Any								=		ids_tensor([self.batch_size, self.seq_length]    ,self.vocab_size      )

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

    SCREAMING_SNAKE_CASE  :  List[Any]								=		None
    if self.use_token_type_ids:
     SCREAMING_SNAKE_CASE  :  str								=		ids_tensor([self.batch_size, self.seq_length]    ,self.type_vocab_size      )

    SCREAMING_SNAKE_CASE  :  int								=		None
    if self.use_mc_token_ids:
     SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		ids_tensor([self.batch_size, self.num_choices]    ,self.seq_length      )

    SCREAMING_SNAKE_CASE  :  Optional[int]								=		None
    SCREAMING_SNAKE_CASE  :  List[Any]								=		None
    SCREAMING_SNAKE_CASE  :  str								=		None
    if self.use_labels:
     SCREAMING_SNAKE_CASE  :  Any								=		ids_tensor([self.batch_size]    ,self.type_sequence_label_size      )
     SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		ids_tensor([self.batch_size, self.seq_length]    ,self.num_labels      )
     SCREAMING_SNAKE_CASE  :  str								=		ids_tensor([self.batch_size]    ,self.num_choices      )

    SCREAMING_SNAKE_CASE  :  Dict								=		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    ,)

    SCREAMING_SNAKE_CASE  :  Tuple								=		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 					__a			(       self    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,*__SCREAMING_SNAKE_CASE      ):
    SCREAMING_SNAKE_CASE  :  int								=		TFCTRLModel(config=__SCREAMING_SNAKE_CASE      )
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		model(__SCREAMING_SNAKE_CASE      )

    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		[input_ids, None, input_mask]  # None is the input for 'past'
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		model(__SCREAMING_SNAKE_CASE      )

    SCREAMING_SNAKE_CASE  :  Tuple								=		model(__SCREAMING_SNAKE_CASE      )

    self.parent.assertEqual(result.last_hidden_state.shape    ,(self.batch_size, self.seq_length, self.hidden_size)      )






   def 					__a			(       self    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,*__SCREAMING_SNAKE_CASE      ):
    SCREAMING_SNAKE_CASE  :  Optional[int]								=		TFCTRLLMHeadModel(config=__SCREAMING_SNAKE_CASE      )
    SCREAMING_SNAKE_CASE  :  Any								=		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
    SCREAMING_SNAKE_CASE  :  Any								=		model(__SCREAMING_SNAKE_CASE      )
    self.parent.assertEqual(result.logits.shape    ,(self.batch_size, self.seq_length, self.vocab_size)      )






   def 					__a			(       self    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,*__SCREAMING_SNAKE_CASE      ):
    SCREAMING_SNAKE_CASE  :  Tuple								=		self.num_labels
    SCREAMING_SNAKE_CASE  :  Any								=		ids_tensor([self.batch_size]    ,self.type_sequence_label_size      )
    SCREAMING_SNAKE_CASE  :  Dict								=		{
        'input_ids': input_ids,
        'token_type_ids': token_type_ids,
        'labels': sequence_labels,
    }
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		TFCTRLForSequenceClassification(__SCREAMING_SNAKE_CASE      )
    SCREAMING_SNAKE_CASE  :  str								=		model(__SCREAMING_SNAKE_CASE      )
    self.parent.assertEqual(result.logits.shape    ,(self.batch_size, self.num_labels)      )






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  str								=		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
    )				, 
    )  :  List[Any]								=		config_and_inputs

    SCREAMING_SNAKE_CASE  :  int								=		{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
    return config, inputs_dict



@require_tf
class 		_a     (							SCREAMING_SNAKE_CASE			,    SCREAMING_SNAKE_CASE			,    unittest.TestCase  ):







   """simple docstring"""




   A		= (TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
   A		= (TFCTRLLMHeadModel,) if is_tf_available() else ()
   A		= (
       {
           'feature-extraction': TFCTRLModel,
           'text-classification': TFCTRLForSequenceClassification,
           'text-generation': TFCTRLLMHeadModel,
           'zero-shot': TFCTRLForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )
   A		= False
   A		= False






   def 					__a			(       self    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      ):
    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 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  List[Any]								=		TFCTRLModelTester(self      )
    SCREAMING_SNAKE_CASE  :  Dict								=		ConfigTester(self    ,config_class=__SCREAMING_SNAKE_CASE    ,n_embd=37      )






   def 					__a			(       self      ):
    self.config_tester.run_common_tests()






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  Tuple								=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_ctrl_model(*__SCREAMING_SNAKE_CASE      )






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  str								=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_ctrl_lm_head(*__SCREAMING_SNAKE_CASE      )






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  Optional[Any]								=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_ctrl_for_sequence_classification(*__SCREAMING_SNAKE_CASE      )






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE  :  Tuple								=		self.model_tester.prepare_config_and_inputs_for_common()
    SCREAMING_SNAKE_CASE  :  Any								=		[TFCTRLLMHeadModel]
    SCREAMING_SNAKE_CASE  :  str								=		[TFCTRLForSequenceClassification]

    for model_class in self.all_model_classes:
     SCREAMING_SNAKE_CASE  :  List[str]								=		model_class(__SCREAMING_SNAKE_CASE      )
     model.build()  # may be needed for the get_bias() call below
     assert isinstance(model.get_input_embeddings()    ,tf.keras.layers.Layer      )

     if model_class in list_lm_models:
      SCREAMING_SNAKE_CASE  :  Dict								=		model.get_output_embeddings()
      assert isinstance(__SCREAMING_SNAKE_CASE    ,tf.keras.layers.Layer      )
      SCREAMING_SNAKE_CASE  :  Any								=		model.get_bias()
      assert isinstance(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      )
      for k, v in name.items():
       assert isinstance(__SCREAMING_SNAKE_CASE    ,tf.Variable      )
     elif model_class in list_other_models_with_output_ebd:
      SCREAMING_SNAKE_CASE  :  Any								=		model.get_output_embeddings()
      assert isinstance(__SCREAMING_SNAKE_CASE    ,tf.keras.layers.Layer      )
      SCREAMING_SNAKE_CASE  :  str								=		model.get_bias()
      assert name is None
     else:
      SCREAMING_SNAKE_CASE  :  List[Any]								=		model.get_output_embeddings()
      assert x is None
      SCREAMING_SNAKE_CASE  :  int								=		model.get_bias()
      assert name is None






   @slow
   def 					__a			(       self      ):
    for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     SCREAMING_SNAKE_CASE  :  List[str]								=		TFCTRLModel.from_pretrained(__SCREAMING_SNAKE_CASE      )
     self.assertIsNotNone(__SCREAMING_SNAKE_CASE      )



@require_tf
class 		_a     (							unittest.TestCase  ):







   """simple docstring"""




   @slow
   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		TFCTRLLMHeadModel.from_pretrained('ctrl'      )
    SCREAMING_SNAKE_CASE  :  Any								=		tf.convert_to_tensor([[11859, 0, 1611, 8]]    ,dtype=tf.intaa      )  # Legal the president is
    SCREAMING_SNAKE_CASE  :  Optional[Any]								=		[
        11859,
        0,
        1611,
        8,
        5,
        150,
        26449,
        2,
        19,
        348,
        469,
        3,
        2595,
        48,
        20740,
        246533,
        246533,
        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  :  Union[str, Any]								=		model.generate(__SCREAMING_SNAKE_CASE    ,do_sample=__SCREAMING_SNAKE_CASE      )
    self.assertListEqual(output_ids[0].numpy().tolist()    ,__SCREAMING_SNAKE_CASE      )


