



'''simple docstring'''






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


 """simple docstring"""


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







 @property
 def _snake_case					(				self				)						->  List[str]:
        _UpperCAmelCase    :		str     	=					1
        _UpperCAmelCase    :		List[Any]     	=					3
        _UpperCAmelCase    :		Optional[Any]     	=					(32, 32)

        _UpperCAmelCase    :		str     	=					floats_tensor((batch_size, num_channels) + sizes    ,rng=random.Random(0				)				).to(a_				)
        return image







 @property
 def _snake_case					(				self				)						->  Any:
        torch.manual_seed(0				)
        _UpperCAmelCase    :		Optional[Any]     	=					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				)						->  Dict:
        torch.manual_seed(0				)
        _UpperCAmelCase    :		Any     	=					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				)						->  Dict:
        torch.manual_seed(0				)
        _UpperCAmelCase    :		Optional[int]     	=					CLIPTextConfig(
            bos_token_id=0    ,eos_token_id=2    ,hidden_size=32    ,intermediate_size=37    ,layer_norm_eps=1E-0_5    ,num_attention_heads=4    ,num_hidden_layers=5    ,pad_token_id=1    ,vocab_size=1_000    ,)
        return CLIPTextModel(a_				)







 @property
 def _snake_case					(				self				)						->  List[Any]:
        def extract(*a_    ,**a_				):



               class 				lowercase			:


                """simple docstring"""


                def __init__(				self				)						->  Dict:
                       _UpperCAmelCase    :		Optional[int]     	=					torch.ones([0]				)







                def _snake_case					(				self    ,a_				)						->  str:
                       self.pixel_values.to(a_				)
                       return self







               return Out()

        return extract







 def _snake_case					(				self				)						->  str:
        _UpperCAmelCase    :		Tuple     	=					"""cpu"""  # ensure determinism for the device-dependent torch.Generator
        _UpperCAmelCase    :		Optional[Any]     	=					self.dummy_cond_unet
        _UpperCAmelCase    :		List[str]     	=					DDIMScheduler(
            beta_start=0.0_0085    ,beta_end=0.012    ,beta_schedule="""scaled_linear"""    ,clip_sample=a_    ,set_alpha_to_one=a_    ,)

        _UpperCAmelCase    :		Union[str, Any]     	=					self.dummy_vae
        _UpperCAmelCase    :		str     	=					self.dummy_text_encoder
        _UpperCAmelCase    :		Any     	=					CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""				)

        # make sure here that pndm scheduler skips prk
        _UpperCAmelCase    :		int     	=					StableDiffusionPipeline(
            unet=a_    ,scheduler=a_    ,vae=a_    ,text_encoder=a_    ,tokenizer=a_    ,safety_checker=a_    ,feature_extractor=self.dummy_extractor    ,)
        _UpperCAmelCase    :		Optional[int]     	=					sd_pipe.to(a_				)
        sd_pipe.set_progress_bar_config(disable=a_				)

        _UpperCAmelCase    :		List[Any]     	=					"""A painting of a squirrel eating a burger"""

        _UpperCAmelCase    :		Any     	=					torch.Generator(device=a_				).manual_seed(0				)
        _UpperCAmelCase    :		Any     	=					sd_pipe([prompt]    ,generator=a_    ,guidance_scale=6.0    ,num_inference_steps=2    ,output_type="""np"""				)
        _UpperCAmelCase    :		Optional[Any]     	=					output.images

        _UpperCAmelCase    :		Optional[Any]     	=					torch.Generator(device=a_				).manual_seed(0				)
        _UpperCAmelCase    :		Optional[int]     	=					sd_pipe(
            [prompt]    ,generator=a_    ,guidance_scale=6.0    ,num_inference_steps=2    ,output_type="""np"""    ,return_dict=a_    ,)[0]

        _UpperCAmelCase    :		Union[str, Any]     	=					image[0, -3:, -3:, -1]
        _UpperCAmelCase    :		List[Any]     	=					image_from_tuple[0, -3:, -3:, -1]

        assert image.shape == (1, 64, 64, 3)
        _UpperCAmelCase    :		Dict     	=					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				)						->  Dict:
        _UpperCAmelCase    :		Tuple     	=					"""cpu"""  # ensure determinism for the device-dependent torch.Generator
        _UpperCAmelCase    :		Optional[Any]     	=					self.dummy_cond_unet
        _UpperCAmelCase    :		int     	=					PNDMScheduler(skip_prk_steps=a_				)
        _UpperCAmelCase    :		Any     	=					self.dummy_vae
        _UpperCAmelCase    :		List[str]     	=					self.dummy_text_encoder
        _UpperCAmelCase    :		List[str]     	=					CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""				)

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

        _UpperCAmelCase    :		int     	=					"""A painting of a squirrel eating a burger"""
        _UpperCAmelCase    :		Optional[int]     	=					torch.Generator(device=a_				).manual_seed(0				)
        _UpperCAmelCase    :		int     	=					sd_pipe([prompt]    ,generator=a_    ,guidance_scale=6.0    ,num_inference_steps=2    ,output_type="""np"""				)

        _UpperCAmelCase    :		List[str]     	=					output.images

        _UpperCAmelCase    :		Optional[int]     	=					torch.Generator(device=a_				).manual_seed(0				)
        _UpperCAmelCase    :		Union[str, Any]     	=					sd_pipe(
            [prompt]    ,generator=a_    ,guidance_scale=6.0    ,num_inference_steps=2    ,output_type="""np"""    ,return_dict=a_    ,)[0]

        _UpperCAmelCase    :		List[str]     	=					image[0, -3:, -3:, -1]
        _UpperCAmelCase    :		Optional[Any]     	=					image_from_tuple[0, -3:, -3:, -1]

        assert image.shape == (1, 64, 64, 3)
        _UpperCAmelCase    :		Optional[int]     	=					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				)						->  List[str]:
        _UpperCAmelCase    :		Optional[Any]     	=					StableDiffusionPipeline.from_pretrained(
            """hf-internal-testing/tiny-stable-diffusion-lms-pipe"""    ,safety_checker=a_				)
        assert isinstance(a_    ,a_				)
        assert isinstance(pipe.scheduler    ,a_				)
        assert pipe.safety_checker is None

        _UpperCAmelCase    :		Optional[Any]     	=					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(a_				)
               _UpperCAmelCase    :		Union[str, Any]     	=					StableDiffusionPipeline.from_pretrained(a_				)

        # sanity check that the pipeline still works
        assert pipe.safety_checker is None
        _UpperCAmelCase    :		Tuple     	=					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:
        _UpperCAmelCase    :		Any     	=					self.dummy_cond_unet
        _UpperCAmelCase    :		Optional[Any]     	=					PNDMScheduler(skip_prk_steps=a_				)
        _UpperCAmelCase    :		Dict     	=					self.dummy_vae
        _UpperCAmelCase    :		Dict     	=					self.dummy_text_encoder
        _UpperCAmelCase    :		Optional[int]     	=					CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""				)

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

        # make sure here that pndm scheduler skips prk
        _UpperCAmelCase    :		int     	=					StableDiffusionPipeline(
            unet=a_    ,scheduler=a_    ,vae=a_    ,text_encoder=a_    ,tokenizer=a_    ,safety_checker=a_    ,feature_extractor=self.dummy_extractor    ,)
        _UpperCAmelCase    :		Optional[int]     	=					sd_pipe.to(a_				)
        sd_pipe.set_progress_bar_config(disable=a_				)

        _UpperCAmelCase    :		int     	=					"""A painting of a squirrel eating a burger"""
        _UpperCAmelCase    :		str     	=					sd_pipe([prompt]    ,num_inference_steps=2    ,output_type="""np"""				).images

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







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


 """simple docstring"""


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







 def _snake_case					(				self				)						->  Union[str, Any]:
        _UpperCAmelCase    :		int     	=					"""cuda"""
        _UpperCAmelCase    :		List[str]     	=					StableDiffusionPipeline.from_pretrained("""runwayml/stable-diffusion-v1-5"""				)
        _UpperCAmelCase    :		Optional[int]     	=					pipe.to(a_				)
        pipe.set_progress_bar_config(disable=a_				)

        _UpperCAmelCase    :		Any     	=					"""a photo of a cat"""
        _UpperCAmelCase    :		int     	=					{
            """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,
        }

        _UpperCAmelCase    :		Optional[int]     	=					3
        _UpperCAmelCase    :		str     	=					7

        # no sega enabled
        _UpperCAmelCase    :		int     	=					torch.Generator(a_				)
        generator.manual_seed(a_				)
        _UpperCAmelCase    :		str     	=					pipe(
            [prompt]    ,generator=a_    ,guidance_scale=a_    ,num_inference_steps=50    ,output_type="""np"""    ,width=512    ,height=512    ,)

        _UpperCAmelCase    :		str     	=					output.images
        _UpperCAmelCase    :		Optional[Any]     	=					image[0, -3:, -3:, -1]
        _UpperCAmelCase    :		Tuple     	=					[
            0.3467_3113,
            0.3849_2733,
            0.3759_7352,
            0.3408_6335,
            0.3565_0748,
            0.3557_9205,
            0.338_4763,
            0.3434_0236,
            0.357_3271,
        ]

        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(a_				)
        _UpperCAmelCase    :		Optional[Any]     	=					pipe(
            [prompt]    ,generator=a_    ,guidance_scale=a_    ,num_inference_steps=50    ,output_type="""np"""    ,width=512    ,height=512    ,**a_    ,)

        _UpperCAmelCase    :		List[str]     	=					output.images
        _UpperCAmelCase    :		Dict     	=					image[0, -3:, -3:, -1]
        _UpperCAmelCase    :		Optional[int]     	=					[
            0.4188_7826,
            0.3772_8766,
            0.3013_8272,
            0.4141_6335,
            0.4166_4985,
            0.3628_3392,
            0.3619_1246,
            0.4336_4465,
            0.4300_1732,
        ]

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

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







 def _snake_case					(				self				)						->  int:
        _UpperCAmelCase    :		List[Any]     	=					"""cuda"""
        _UpperCAmelCase    :		Union[str, Any]     	=					StableDiffusionPipeline.from_pretrained("""runwayml/stable-diffusion-v1-5"""				)
        _UpperCAmelCase    :		Optional[Any]     	=					pipe.to(a_				)
        pipe.set_progress_bar_config(disable=a_				)

        _UpperCAmelCase    :		int     	=					"""an image of a crowded boulevard, realistic, 4k"""
        _UpperCAmelCase    :		List[Any]     	=					{
            """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,
        }

        _UpperCAmelCase    :		str     	=					9
        _UpperCAmelCase    :		Optional[Any]     	=					7

        # no sega enabled
        _UpperCAmelCase    :		Tuple     	=					torch.Generator(a_				)
        generator.manual_seed(a_				)
        _UpperCAmelCase    :		List[Any]     	=					pipe(
            [prompt]    ,generator=a_    ,guidance_scale=a_    ,num_inference_steps=50    ,output_type="""np"""    ,width=512    ,height=512    ,)

        _UpperCAmelCase    :		List[Any]     	=					output.images
        _UpperCAmelCase    :		Any     	=					image[0, -3:, -3:, -1]
        _UpperCAmelCase    :		str     	=					[
            0.4349_7998,
            0.9181_4065,
            0.754_0739,
            0.5558_0205,
            0.846_7265,
            0.538_9691,
            0.6257_4506,
            0.5889_7763,
            0.5092_6757,
        ]

        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(a_				)
        _UpperCAmelCase    :		Dict     	=					pipe(
            [prompt]    ,generator=a_    ,guidance_scale=a_    ,num_inference_steps=50    ,output_type="""np"""    ,width=512    ,height=512    ,**a_    ,)

        _UpperCAmelCase    :		int     	=					output.images
        _UpperCAmelCase    :		List[str]     	=					image[0, -3:, -3:, -1]
        _UpperCAmelCase    :		Optional[int]     	=					[
            0.308_9719,
            0.3050_0144,
            0.2901_6042,
            0.3063_0964,
            0.32_5687,
            0.2941_9225,
            0.290_8091,
            0.2872_3598,
            0.2769_6294,
        ]

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

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







 def _snake_case					(				self				)						->  List[str]:
        _UpperCAmelCase    :		List[str]     	=					"""cuda"""
        _UpperCAmelCase    :		Dict     	=					StableDiffusionPipeline.from_pretrained("""runwayml/stable-diffusion-v1-5"""				)
        _UpperCAmelCase    :		Tuple     	=					pipe.to(a_				)
        pipe.set_progress_bar_config(disable=a_				)

        _UpperCAmelCase    :		List[Any]     	=					"""a castle next to a river"""
        _UpperCAmelCase    :		Tuple     	=					{
            """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,
        }

        _UpperCAmelCase    :		List[Any]     	=					48
        _UpperCAmelCase    :		str     	=					7

        # no sega enabled
        _UpperCAmelCase    :		str     	=					torch.Generator(a_				)
        generator.manual_seed(a_				)
        _UpperCAmelCase    :		Dict     	=					pipe(
            [prompt]    ,generator=a_    ,guidance_scale=a_    ,num_inference_steps=50    ,output_type="""np"""    ,width=512    ,height=512    ,)

        _UpperCAmelCase    :		Optional[Any]     	=					output.images
        _UpperCAmelCase    :		Union[str, Any]     	=					image[0, -3:, -3:, -1]
        _UpperCAmelCase    :		List[Any]     	=					[
            0.7516_3555,
            0.7603_7145,
            0.6_1785,
            0.918_9673,
            0.862_7701,
            0.8518_9694,
            0.851_2813,
            0.8701_2076,
            0.831_2857,
        ]

        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(a_				)
        _UpperCAmelCase    :		Dict     	=					pipe(
            [prompt]    ,generator=a_    ,guidance_scale=a_    ,num_inference_steps=50    ,output_type="""np"""    ,width=512    ,height=512    ,**a_    ,)

        _UpperCAmelCase    :		Dict     	=					output.images
        _UpperCAmelCase    :		Optional[int]     	=					image[0, -3:, -3:, -1]
        _UpperCAmelCase    :		List[str]     	=					[
            0.7355_3365,
            0.753_7271,
            0.7434_1905,
            0.6648_0356,
            0.647_2925,
            0.6303_9416,
            0.6481_2905,
            0.674_9717,
            0.651_7102,
        ]

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

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







 def _snake_case					(				self				)						->  Tuple:
        _UpperCAmelCase    :		Union[str, Any]     	=					"""cuda"""
        _UpperCAmelCase    :		List[str]     	=					StableDiffusionPipeline.from_pretrained("""runwayml/stable-diffusion-v1-5"""    ,torch_dtype=torch.floataa				)
        _UpperCAmelCase    :		List[str]     	=					pipe.to(a_				)
        pipe.set_progress_bar_config(disable=a_				)

        _UpperCAmelCase    :		Union[str, Any]     	=					"""a photo of a cat"""
        _UpperCAmelCase    :		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,
        }

        _UpperCAmelCase    :		Any     	=					3
        _UpperCAmelCase    :		Dict     	=					7

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

        _UpperCAmelCase    :		str     	=					output.images
        _UpperCAmelCase    :		str     	=					image[0, -3:, -3:, -1]
        _UpperCAmelCase    :		List[str]     	=					[
            0.3488_7695,
            0.387_6953,
            0.375,
            0.3442_3828,
            0.358_1543,
            0.3571_7773,
            0.338_3789,
            0.3457_0312,
            0.35_9375,
        ]

        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(a_				)
        _UpperCAmelCase    :		List[str]     	=					pipe(
            [prompt]    ,generator=a_    ,guidance_scale=a_    ,num_inference_steps=50    ,output_type="""np"""    ,width=512    ,height=512    ,**a_    ,)

        _UpperCAmelCase    :		List[str]     	=					output.images
        _UpperCAmelCase    :		Tuple     	=					image[0, -3:, -3:, -1]
        _UpperCAmelCase    :		Union[str, Any]     	=					[
            0.4228_5156,
            0.3691_4062,
            0.2907_7148,
            0.4204_1016,
            0.4191_8945,
            0.3549_8047,
            0.361_8164,
            0.442_3828,
            0.4311_5234,
        ]

        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   UpperCamelCase					(       a_       ):







    """simple docstring"""





    def __init__(					self     :						Dict							,							UpperCAmelCase_     :						List[str]							,							):


         """simple docstring"""







         a					:							Any							= parent
         a					:							Tuple							= 1_3
         a					:							Any							= 7
         a					:							Any							= True
         a					:							Optional[Any]							= True
         a					:							Optional[Any]							= True
         a					:							Dict							= True
         a					:							List[str]							= True
         a					:							Dict							= 9_9
         a					:							Tuple							= 3_2
         a					:							Optional[int]							= 2
         a					:							str							= 4
         a					:							str							= 3_7
         a					:							Union[str, Any]							= 'gelu'
         a					:							Any							= 0.1
         a					:							Any							= 0.1
         a					:							List[Any]							= 5_1_2
         a					:							Any							= 1_6
         a					:							int							= 2
         a					:							Dict							= 0.02
         a					:							Optional[int]							= 3
         a					:							Union[str, Any]							= 4
         a					:							int							= None
         a					:							Tuple							= self.vocab_size - 1





    def    SCREAMING_SNAKE_CASE_     (					self     :						int):


         """simple docstring"""







         a					:							Optional[int]							= ids_tensor([self.batch_size, self.seq_length]							,							self.vocab_size)

         a					:							List[str]							= None
         if self.use_input_mask:
              a					:							Optional[Any]							= random_attention_mask([self.batch_size, self.seq_length])

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

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

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

         a					:							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							,							)

         a					:							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    SCREAMING_SNAKE_CASE_     (					self     :						Union[str, Any]							,							UpperCAmelCase_     :						Optional[Any]							,							UpperCAmelCase_     :						Optional[Any]							,							UpperCAmelCase_     :						Dict							,							UpperCAmelCase_     :						List[Any]							,							UpperCAmelCase_     :						str							,							*UpperCAmelCase_     :						List[str]):


         """simple docstring"""







         a					:							Union[str, Any]							= TFCTRLModel(config=UpperCAmelCase_)
         a					:							Dict							= {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
         a					:							Tuple							= model(UpperCAmelCase_)

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

         a					:							List[str]							= model(UpperCAmelCase_)

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





    def    SCREAMING_SNAKE_CASE_     (					self     :						List[str]							,							UpperCAmelCase_     :						Optional[Any]							,							UpperCAmelCase_     :						List[str]							,							UpperCAmelCase_     :						Dict							,							UpperCAmelCase_     :						Tuple							,							UpperCAmelCase_     :						Dict							,							*UpperCAmelCase_     :						str):


         """simple docstring"""







         a					:							Optional[int]							= TFCTRLLMHeadModel(config=UpperCAmelCase_)
         a					:							Tuple							= {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
         a					:							int							= model(UpperCAmelCase_)
         self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.seq_length, self.vocab_size))





    def    SCREAMING_SNAKE_CASE_     (					self     :						Tuple							,							UpperCAmelCase_     :						str							,							UpperCAmelCase_     :						Optional[Any]							,							UpperCAmelCase_     :						int							,							UpperCAmelCase_     :						Tuple							,							UpperCAmelCase_     :						Optional[Any]							,							*UpperCAmelCase_     :						List[str]):


         """simple docstring"""







         a					:							Tuple							= self.num_labels
         a					:							List[str]							= ids_tensor([self.batch_size]							,							self.type_sequence_label_size)
         a					:							int							= {
             'input_ids': input_ids,
             'token_type_ids': token_type_ids,
             'labels': sequence_labels,
         }
         a					:							str							= TFCTRLForSequenceClassification(UpperCAmelCase_)
         a					:							Union[str, Any]							= model(UpperCAmelCase_)
         self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.num_labels))







    def    SCREAMING_SNAKE_CASE_     (					self     :						int):


         """simple docstring"""







         a					:							Optional[int]							= self.prepare_config_and_inputs()

         (
             (
             a
         )							,			(
             a
         )							,			(
             a
         )							,			(
             a
         )							,			(
             a
         )							,			(
             a
         )							,			(
             a
         )							,			(
             a
         )							,			(
             a
         )							,			
         )					:							int							= config_and_inputs

         a					:							Tuple							= {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
         return config, inputs_dict






@require_tf
class   UpperCamelCase					(       a_    ,     a_    ,     unittest.TestCase       ):







    """simple docstring"""





    A     :    str				=			(TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
    A     :    Optional[int]				=			(TFCTRLLMHeadModel,) if is_tf_available() else ()
    A     :    str				=			(
        {
            "feature-extraction": TFCTRLModel,
            "text-classification": TFCTRLForSequenceClassification,
            "text-generation": TFCTRLLMHeadModel,
            "zero-shot": TFCTRLForSequenceClassification,
        }
        if is_tf_available()
        else {}
    )
    A     :    int				=			False
    A     :    Optional[Any]				=			False





    def    SCREAMING_SNAKE_CASE_     (					self     :						Union[str, Any]							,							UpperCAmelCase_     :						Dict							,							UpperCAmelCase_     :						Any							,							UpperCAmelCase_     :						Optional[Any]							,							UpperCAmelCase_     :						Tuple							,							UpperCAmelCase_     :						Optional[Any]):


         """simple docstring"""







         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    SCREAMING_SNAKE_CASE_     (					self     :						Optional[Any]):


         """simple docstring"""







         a					:							Any							= TFCTRLModelTester(self)
         a					:							Dict							= ConfigTester(self							,							config_class=UpperCAmelCase_							,							n_embd=3_7)





    def    SCREAMING_SNAKE_CASE_     (					self     :						List[Any]):


         """simple docstring"""







         self.config_tester.run_common_tests()





    def    SCREAMING_SNAKE_CASE_     (					self     :						str):


         """simple docstring"""







         a					:							Optional[Any]							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_ctrl_model(*UpperCAmelCase_)





    def    SCREAMING_SNAKE_CASE_     (					self     :						Dict):


         """simple docstring"""







         a					:							List[Any]							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_ctrl_lm_head(*UpperCAmelCase_)





    def    SCREAMING_SNAKE_CASE_     (					self     :						List[str]):


         """simple docstring"""







         a					:							str							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_ctrl_for_sequence_classification(*UpperCAmelCase_)





    def    SCREAMING_SNAKE_CASE_     (					self     :						Optional[Any]):


         """simple docstring"""







         a							,			a					:							Optional[Any]							= self.model_tester.prepare_config_and_inputs_for_common()
         a					:							int							= [TFCTRLLMHeadModel]
         a					:							int							= [TFCTRLForSequenceClassification]

         for model_class in self.all_model_classes:
              a					:							int							= model_class(UpperCAmelCase_)
              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:
                   a					:							Optional[Any]							= model.get_output_embeddings()
                   assert isinstance(UpperCAmelCase_							,							tf.keras.layers.Layer)
                   a					:							int							= model.get_bias()
                   assert isinstance(UpperCAmelCase_							,							UpperCAmelCase_)
                   for k, v in name.items():
                        assert isinstance(UpperCAmelCase_							,							tf.Variable)
              elif model_class in list_other_models_with_output_ebd:
                   a					:							List[str]							= model.get_output_embeddings()
                   assert isinstance(UpperCAmelCase_							,							tf.keras.layers.Layer)
                   a					:							List[Any]							= model.get_bias()
                   assert name is None
              else:
                   a					:							List[str]							= model.get_output_embeddings()
                   assert x is None
                   a					:							Optional[Any]							= model.get_bias()
                   assert name is None







    @slow
    def    SCREAMING_SNAKE_CASE_     (					self     :						Dict):


         """simple docstring"""







         for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              a					:							Dict							= TFCTRLModel.from_pretrained(UpperCAmelCase_)
              self.assertIsNotNone(UpperCAmelCase_)






@require_tf
class   UpperCamelCase					(       unittest.TestCase       ):







    """simple docstring"""





    @slow
    def    SCREAMING_SNAKE_CASE_     (					self     :						Tuple):


         """simple docstring"""







         a					:							List[Any]							= TFCTRLLMHeadModel.from_pretrained('ctrl')
         a					:							Union[str, Any]							= tf.convert_to_tensor([[1_1_8_5_9, 0, 1_6_1_1, 8]]							,							dtype=tf.intaa)  # Legal the president is
         a					:							List[Any]							= [
             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

         a					:							List[str]							= model.generate(UpperCAmelCase_							,							do_sample=UpperCAmelCase_)
         self.assertListEqual(output_ids[0].numpy().tolist()							,							UpperCAmelCase_)

