
"""simple docstring"""





import gc
import unittest

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

from diffusers import PriorTransformer, UnCLIPPipeline, UnCLIPScheduler, UNetaDConditionModel, UNetaDModel
from diffusers.pipelines.unclip.text_proj import UnCLIPTextProjModel
from diffusers.utils import load_numpy, nightly, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()


class A_			(lowercase__       ,unittest.TestCase   ):




  '''simple docstring'''
  SCREAMING_SNAKE_CASE__    :		Any						  =	UnCLIPPipeline
  SCREAMING_SNAKE_CASE__    :		List[Any]						  =	TEXT_TO_IMAGE_PARAMS - {
      """negative_prompt""",
      """height""",
      """width""",
      """negative_prompt_embeds""",
      """guidance_scale""",
      """prompt_embeds""",
      """cross_attention_kwargs""",
  }
  SCREAMING_SNAKE_CASE__    :		List[str]						  =	TEXT_TO_IMAGE_BATCH_PARAMS
  SCREAMING_SNAKE_CASE__    :		Union[str, Any]						  =	[
      """generator""",
      """return_dict""",
      """prior_num_inference_steps""",
      """decoder_num_inference_steps""",
      """super_res_num_inference_steps""",
  ]
  SCREAMING_SNAKE_CASE__    :		Union[str, Any]						  =	False







  @property
  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     return 32







  @property
  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     return 32







  @property
  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     return self.time_input_dim







  @property
  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     return self.time_input_dim * 4







  @property
  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     return 100







  @property
  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     UpperCAmelCase_      :      int      =					CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"						)
     return tokenizer







  @property
  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     torch.manual_seed(0						)
     UpperCAmelCase_      :      Tuple      =					CLIPTextConfig(
         bos_token_id=0       ,     eos_token_id=2       ,     hidden_size=self.text_embedder_hidden_size       ,     projection_dim=self.text_embedder_hidden_size       ,     intermediate_size=37       ,     layer_norm_eps=1E-0_5       ,     num_attention_heads=4       ,     num_hidden_layers=5       ,     pad_token_id=1       ,     vocab_size=1000       ,     )
     return CLIPTextModelWithProjection(lowercase_						)







  @property
  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     torch.manual_seed(0						)

     UpperCAmelCase_      :      Union[str, Any]      =					{
         "num_attention_heads": 2,
         "attention_head_dim": 12,
         "embedding_dim": self.text_embedder_hidden_size,
         "num_layers": 1,
     }

     UpperCAmelCase_      :      List[Any]      =					PriorTransformer(**lowercase_						)
     return model







  @property
  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     torch.manual_seed(0						)

     UpperCAmelCase_      :      Any      =					{
         "clip_embeddings_dim": self.text_embedder_hidden_size,
         "time_embed_dim": self.time_embed_dim,
         "cross_attention_dim": self.cross_attention_dim,
     }

     UpperCAmelCase_      :      int      =					UnCLIPTextProjModel(**lowercase_						)
     return model







  @property
  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     torch.manual_seed(0						)

     UpperCAmelCase_      :      int      =					{
         "sample_size": 32,
         # RGB in channels
         "in_channels": 3,
         # Out channels is double in channels because predicts mean and variance
         "out_channels": 6,
         "down_block_types": ("ResnetDownsampleBlock2D", "SimpleCrossAttnDownBlock2D"),
         "up_block_types": ("SimpleCrossAttnUpBlock2D", "ResnetUpsampleBlock2D"),
         "mid_block_type": "UNetMidBlock2DSimpleCrossAttn",
         "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2),
         "layers_per_block": 1,
         "cross_attention_dim": self.cross_attention_dim,
         "attention_head_dim": 4,
         "resnet_time_scale_shift": "scale_shift",
         "class_embed_type": "identity",
     }

     UpperCAmelCase_      :      Tuple      =					UNetaDConditionModel(**lowercase_						)
     return model







  @property
  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     return {
         "sample_size": 64,
         "layers_per_block": 1,
         "down_block_types": ("ResnetDownsampleBlock2D", "ResnetDownsampleBlock2D"),
         "up_block_types": ("ResnetUpsampleBlock2D", "ResnetUpsampleBlock2D"),
         "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2),
         "in_channels": 6,
         "out_channels": 3,
     }







  @property
  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     torch.manual_seed(0						)

     UpperCAmelCase_      :      Tuple      =					UNetaDModel(**self.dummy_super_res_kwargs						)
     return model







  @property
  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     torch.manual_seed(1						)

     UpperCAmelCase_      :      List[Any]      =					UNetaDModel(**self.dummy_super_res_kwargs						)
     return model







  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     UpperCAmelCase_      :      Any      =					self.dummy_prior
     UpperCAmelCase_      :      Any      =					self.dummy_decoder
     UpperCAmelCase_      :      List[str]      =					self.dummy_text_proj
     UpperCAmelCase_      :      str      =					self.dummy_text_encoder
     UpperCAmelCase_      :      List[Any]      =					self.dummy_tokenizer
     UpperCAmelCase_      :      List[Any]      =					self.dummy_super_res_first
     UpperCAmelCase_      :      str      =					self.dummy_super_res_last

     UpperCAmelCase_      :      Any      =					UnCLIPScheduler(
         variance_type="fixed_small_log"       ,     prediction_type="sample"       ,     num_train_timesteps=1000       ,     clip_sample_range=5.0       ,     )

     UpperCAmelCase_      :      List[str]      =					UnCLIPScheduler(
         variance_type="learned_range"       ,     prediction_type="epsilon"       ,     num_train_timesteps=1000       ,     )

     UpperCAmelCase_      :      int      =					UnCLIPScheduler(
         variance_type="fixed_small_log"       ,     prediction_type="epsilon"       ,     num_train_timesteps=1000       ,     )

     UpperCAmelCase_      :      Union[str, Any]      =					{
         "prior": prior,
         "decoder": decoder,
         "text_proj": text_proj,
         "text_encoder": text_encoder,
         "tokenizer": tokenizer,
         "super_res_first": super_res_first,
         "super_res_last": super_res_last,
         "prior_scheduler": prior_scheduler,
         "decoder_scheduler": decoder_scheduler,
         "super_res_scheduler": super_res_scheduler,
     }

     return components







  def  UpperCamelCase__      (	self       ,     lowercase_       ,     lowercase_=0						):

     """simple docstring"""
     if str(lowercase_						).startswith("mps"						):
        UpperCAmelCase_      :      List[str]      =					torch.manual_seed(lowercase_						)
     else:
        UpperCAmelCase_      :      Optional[int]      =					torch.Generator(device=lowercase_						).manual_seed(lowercase_						)
     UpperCAmelCase_      :      Union[str, Any]      =					{
         "prompt": "horse",
         "generator": generator,
         "prior_num_inference_steps": 2,
         "decoder_num_inference_steps": 2,
         "super_res_num_inference_steps": 2,
         "output_type": "numpy",
     }
     return inputs







  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     UpperCAmelCase_      :      List[str]      =					"cpu"

     UpperCAmelCase_      :      List[str]      =					self.get_dummy_components()

     UpperCAmelCase_      :      Union[str, Any]      =					self.pipeline_class(**lowercase_						)
     UpperCAmelCase_      :      Optional[int]      =					pipe.to(lowercase_						)

     pipe.set_progress_bar_config(disable=lowercase_						)

     UpperCAmelCase_      :      int      =					pipe(**self.get_dummy_inputs(lowercase_						)						)
     UpperCAmelCase_      :      Dict      =					output.images

     UpperCAmelCase_      :      Optional[int]      =					pipe(
         **self.get_dummy_inputs(lowercase_						)       ,     return_dict=lowercase_       ,     )[0]

     UpperCAmelCase_      :      Dict      =					image[0, -3:, -3:, -1]
     UpperCAmelCase_      :      Any      =					image_from_tuple[0, -3:, -3:, -1]

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

     UpperCAmelCase_      :      Dict      =					np.array(
         [
             0.99_97,
             0.99_88,
             0.00_28,
             0.99_97,
             0.99_84,
             0.99_65,
             0.00_29,
             0.99_86,
             0.00_25,
         ]						)

     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  UpperCamelCase__      (	self						):

     """simple docstring"""
     UpperCAmelCase_      :      str      =					torch.device("cpu"						)


     class A_			:




       '''simple docstring'''
       SCREAMING_SNAKE_CASE__    :		Any						  =	1




     UpperCAmelCase_      :      Dict      =					self.get_dummy_components()

     UpperCAmelCase_      :      Optional[Any]      =					self.pipeline_class(**lowercase_						)
     UpperCAmelCase_      :      List[Any]      =					pipe.to(lowercase_						)

     UpperCAmelCase_      :      Union[str, Any]      =					components["prior"]
     UpperCAmelCase_      :      List[Any]      =					components["decoder"]
     UpperCAmelCase_      :      Any      =					components["super_res_first"]
     UpperCAmelCase_      :      Optional[int]      =					components["tokenizer"]
     UpperCAmelCase_      :      Any      =					components["text_encoder"]

     UpperCAmelCase_      :      str      =					torch.Generator(device=lowercase_						).manual_seed(0						)
     UpperCAmelCase_      :      Union[str, Any]      =					prior.dtype
     UpperCAmelCase_      :      Dict      =					1

     UpperCAmelCase_      :      List[Any]      =					(batch_size, prior.config.embedding_dim)
     UpperCAmelCase_      :      Dict      =					pipe.prepare_latents(
         lowercase_       ,     dtype=lowercase_       ,     device=lowercase_       ,     generator=lowercase_       ,     latents=lowercase_       ,     scheduler=DummyScheduler()						)
     UpperCAmelCase_      :      Optional[Any]      =					(batch_size, decoder.config.in_channels, decoder.config.sample_size, decoder.config.sample_size)
     UpperCAmelCase_      :      Tuple      =					pipe.prepare_latents(
         lowercase_       ,     dtype=lowercase_       ,     device=lowercase_       ,     generator=lowercase_       ,     latents=lowercase_       ,     scheduler=DummyScheduler()						)

     UpperCAmelCase_      :      str      =					(
         batch_size,
         super_res_first.config.in_channels // 2,
         super_res_first.config.sample_size,
         super_res_first.config.sample_size,
     )
     UpperCAmelCase_      :      Union[str, Any]      =					pipe.prepare_latents(
         lowercase_       ,     dtype=lowercase_       ,     device=lowercase_       ,     generator=lowercase_       ,     latents=lowercase_       ,     scheduler=DummyScheduler()						)

     pipe.set_progress_bar_config(disable=lowercase_						)

     UpperCAmelCase_      :      Any      =					"this is a prompt example"

     UpperCAmelCase_      :      List[Any]      =					torch.Generator(device=lowercase_						).manual_seed(0						)
     UpperCAmelCase_      :      Optional[int]      =					pipe(
         [prompt]       ,     generator=lowercase_       ,     prior_num_inference_steps=2       ,     decoder_num_inference_steps=2       ,     super_res_num_inference_steps=2       ,     prior_latents=lowercase_       ,     decoder_latents=lowercase_       ,     super_res_latents=lowercase_       ,     output_type="np"       ,     )
     UpperCAmelCase_      :      Dict      =					output.images

     UpperCAmelCase_      :      Optional[Any]      =					tokenizer(
         lowercase_       ,     padding="max_length"       ,     max_length=tokenizer.model_max_length       ,     return_tensors="pt"       ,     )
     UpperCAmelCase_      :      str      =					text_encoder(text_inputs.input_ids						)
     UpperCAmelCase_      :      Dict      =					text_inputs.attention_mask

     UpperCAmelCase_      :      Any      =					torch.Generator(device=lowercase_						).manual_seed(0						)
     UpperCAmelCase_      :      List[Any]      =					pipe(
         generator=lowercase_       ,     prior_num_inference_steps=2       ,     decoder_num_inference_steps=2       ,     super_res_num_inference_steps=2       ,     prior_latents=lowercase_       ,     decoder_latents=lowercase_       ,     super_res_latents=lowercase_       ,     text_model_output=lowercase_       ,     text_attention_mask=lowercase_       ,     output_type="np"       ,     )[0]

     # make sure passing text embeddings manually is identical
     assert np.abs(image - image_from_text						).max() < 1E-4







  @skip_mps
  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     UpperCAmelCase_      :      Dict      =					torch_device == "cpu"

     self._test_attention_slicing_forward_pass(test_max_difference=lowercase_       ,     expected_max_diff=0.01						)







  @skip_mps
  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     UpperCAmelCase_      :      Any      =					torch_device == "cpu"
     UpperCAmelCase_      :      Tuple      =					True
     UpperCAmelCase_      :      List[str]      =					[
         "prior_num_inference_steps",
         "decoder_num_inference_steps",
         "super_res_num_inference_steps",
     ]

     self._test_inference_batch_single_identical(
         test_max_difference=lowercase_       ,     relax_max_difference=lowercase_       ,     additional_params_copy_to_batched_inputs=lowercase_       ,     )







  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     UpperCAmelCase_      :      int      =					[
         "prior_num_inference_steps",
         "decoder_num_inference_steps",
         "super_res_num_inference_steps",
     ]

     if torch_device == "mps":
        # TODO: MPS errors with larger batch sizes
        UpperCAmelCase_      :      List[str]      =					[2, 3]
        self._test_inference_batch_consistent(
            batch_sizes=lowercase_       ,     additional_params_copy_to_batched_inputs=lowercase_       ,     )
     else:
        self._test_inference_batch_consistent(
            additional_params_copy_to_batched_inputs=lowercase_						)







  @skip_mps
  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     return super().test_dict_tuple_outputs_equivalent()







  @skip_mps
  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     return super().test_save_load_local()







  @skip_mps
  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     return super().test_save_load_optional_components()




@nightly
class A_			(unittest.TestCase   ):




  '''simple docstring'''







  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     super().tearDown()
     gc.collect()
     torch.cuda.empty_cache()







  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     UpperCAmelCase_      :      Any      =					load_numpy(
         "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
         "/unclip/karlo_v1_alpha_horse_cpu.npy"						)

     UpperCAmelCase_      :      List[Any]      =					UnCLIPPipeline.from_pretrained("kakaobrain/karlo-v1-alpha"						)
     pipeline.set_progress_bar_config(disable=lowercase_						)

     UpperCAmelCase_      :      List[Any]      =					torch.manual_seed(0						)
     UpperCAmelCase_      :      Optional[int]      =					pipeline(
         "horse"       ,     num_images_per_prompt=1       ,     generator=lowercase_       ,     output_type="np"       ,     )

     UpperCAmelCase_      :      Any      =					output.images[0]

     assert image.shape == (256, 256, 3)
     assert np.abs(expected_image - image						).max() < 1E-1




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




  '''simple docstring'''







  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     super().tearDown()
     gc.collect()
     torch.cuda.empty_cache()







  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     UpperCAmelCase_      :      Union[str, Any]      =					load_numpy(
         "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
         "/unclip/karlo_v1_alpha_horse_fp16.npy"						)

     UpperCAmelCase_      :      Optional[int]      =					UnCLIPPipeline.from_pretrained("kakaobrain/karlo-v1-alpha"       ,     torch_dtype=torch.floataa						)
     UpperCAmelCase_      :      List[Any]      =					pipeline.to(lowercase_						)
     pipeline.set_progress_bar_config(disable=lowercase_						)

     UpperCAmelCase_      :      Dict      =					torch.Generator(device="cpu"						).manual_seed(0						)
     UpperCAmelCase_      :      Union[str, Any]      =					pipeline(
         "horse"       ,     generator=lowercase_       ,     output_type="np"       ,     )

     UpperCAmelCase_      :      List[Any]      =					output.images[0]

     assert image.shape == (256, 256, 3)

     assert_mean_pixel_difference(lowercase_       ,     lowercase_						)







  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     torch.cuda.empty_cache()
     torch.cuda.reset_max_memory_allocated()
     torch.cuda.reset_peak_memory_stats()

     UpperCAmelCase_      :      Any      =					UnCLIPPipeline.from_pretrained("kakaobrain/karlo-v1-alpha"       ,     torch_dtype=torch.floataa						)
     UpperCAmelCase_      :      Dict      =					pipe.to(lowercase_						)
     pipe.set_progress_bar_config(disable=lowercase_						)
     pipe.enable_attention_slicing()
     pipe.enable_sequential_cpu_offload()

     UpperCAmelCase_      :      Optional[Any]      =					pipe(
         "horse"       ,     num_images_per_prompt=1       ,     prior_num_inference_steps=2       ,     decoder_num_inference_steps=2       ,     super_res_num_inference_steps=2       ,     output_type="np"       ,     )

     UpperCAmelCase_      :      Tuple      =					torch.cuda.max_memory_allocated()
     # make sure that less than 7 GB is allocated
     assert mem_bytes < 7 * 10**9




"""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_			(lowercase__   ):




  '''simple docstring'''







  def __init__(	self       ,     lowercase_       ,     ):

     """simple docstring"""
     UpperCAmelCase_      :      List[str]      =					parent
     UpperCAmelCase_      :      List[Any]      =					13
     UpperCAmelCase_      :      Dict      =					7
     UpperCAmelCase_      :      Optional[int]      =					True
     UpperCAmelCase_      :      List[Any]      =					True
     UpperCAmelCase_      :      Optional[int]      =					True
     UpperCAmelCase_      :      int      =					True
     UpperCAmelCase_      :      Any      =					True
     UpperCAmelCase_      :      Any      =					99
     UpperCAmelCase_      :      Dict      =					32
     UpperCAmelCase_      :      Dict      =					2
     UpperCAmelCase_      :      str      =					4
     UpperCAmelCase_      :      int      =					37
     UpperCAmelCase_      :      Union[str, Any]      =					"gelu"
     UpperCAmelCase_      :      List[str]      =					0.1
     UpperCAmelCase_      :      List[str]      =					0.1
     UpperCAmelCase_      :      int      =					512
     UpperCAmelCase_      :      str      =					16
     UpperCAmelCase_      :      Union[str, Any]      =					2
     UpperCAmelCase_      :      Dict      =					0.02
     UpperCAmelCase_      :      Tuple      =					3
     UpperCAmelCase_      :      Tuple      =					4
     UpperCAmelCase_      :      int      =					None
     UpperCAmelCase_      :      Dict      =					self.vocab_size - 1







  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     UpperCAmelCase_      :      Dict      =					ids_tensor([self.batch_size, self.seq_length]       ,     self.vocab_size						)

     UpperCAmelCase_      :      Optional[int]      =					None
     if self.use_input_mask:
        UpperCAmelCase_      :      Optional[Any]      =					random_attention_mask([self.batch_size, self.seq_length]						)

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

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

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

     UpperCAmelCase_      :      str      =					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       ,     )

     UpperCAmelCase_      :      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  UpperCamelCase__      (	self       ,     lowercase_       ,     lowercase_       ,     lowercase_       ,     lowercase_       ,     lowercase_       ,     *lowercase_						):

     """simple docstring"""
     UpperCAmelCase_      :      int      =					TFCTRLModel(config=lowercase_						)
     UpperCAmelCase_      :      Optional[Any]      =					{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
     UpperCAmelCase_      :      Union[str, Any]      =					model(lowercase_						)

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

     UpperCAmelCase_      :      str      =					model(lowercase_						)

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







  def  UpperCamelCase__      (	self       ,     lowercase_       ,     lowercase_       ,     lowercase_       ,     lowercase_       ,     lowercase_       ,     *lowercase_						):

     """simple docstring"""
     UpperCAmelCase_      :      Optional[int]      =					TFCTRLLMHeadModel(config=lowercase_						)
     UpperCAmelCase_      :      List[Any]      =					{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
     UpperCAmelCase_      :      List[str]      =					model(lowercase_						)
     self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.seq_length, self.vocab_size)						)







  def  UpperCamelCase__      (	self       ,     lowercase_       ,     lowercase_       ,     lowercase_       ,     lowercase_       ,     lowercase_       ,     *lowercase_						):

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







  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     UpperCAmelCase_      :      Dict      =					self.prepare_config_and_inputs()

     (
         (
         UpperCAmelCase_
     )  ,     (
         UpperCAmelCase_
     )  ,     (
         UpperCAmelCase_
     )  ,     (
         UpperCAmelCase_
     )  ,     (
         UpperCAmelCase_
     )  ,     (
         UpperCAmelCase_
     )  ,     (
         UpperCAmelCase_
     )  ,     (
         UpperCAmelCase_
     )  ,     (
         UpperCAmelCase_
     )  ,     
     )      :      Tuple      =					config_and_inputs

     UpperCAmelCase_      :      int      =					{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
     return config, inputs_dict




@require_tf
class A_			(lowercase__       ,lowercase__       ,unittest.TestCase   ):




  '''simple docstring'''
  SCREAMING_SNAKE_CASE__    :		Dict						  =	(TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
  SCREAMING_SNAKE_CASE__    :		str						  =	(TFCTRLLMHeadModel,) if is_tf_available() else ()
  SCREAMING_SNAKE_CASE__    :		Any						  =	(
      {
          """feature-extraction""": TFCTRLModel,
          """text-classification""": TFCTRLForSequenceClassification,
          """text-generation""": TFCTRLLMHeadModel,
          """zero-shot""": TFCTRLForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )
  SCREAMING_SNAKE_CASE__    :		Dict						  =	False
  SCREAMING_SNAKE_CASE__    :		List[Any]						  =	False







  def  UpperCamelCase__      (	self       ,     lowercase_       ,     lowercase_       ,     lowercase_       ,     lowercase_       ,     lowercase_						):

     """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  UpperCamelCase__      (	self						):

     """simple docstring"""
     UpperCAmelCase_      :      str      =					TFCTRLModelTester(self						)
     UpperCAmelCase_      :      Dict      =					ConfigTester(self       ,     config_class=lowercase_       ,     n_embd=37						)







  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     self.config_tester.run_common_tests()







  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     UpperCAmelCase_      :      str      =					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_ctrl_model(*lowercase_						)







  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     UpperCAmelCase_      :      Union[str, Any]      =					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_ctrl_lm_head(*lowercase_						)







  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     UpperCAmelCase_      :      Optional[Any]      =					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_ctrl_for_sequence_classification(*lowercase_						)







  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     UpperCAmelCase_  ,     UpperCAmelCase_      :      List[Any]      =					self.model_tester.prepare_config_and_inputs_for_common()
     UpperCAmelCase_      :      Tuple      =					[TFCTRLLMHeadModel]
     UpperCAmelCase_      :      Dict      =					[TFCTRLForSequenceClassification]

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







  @slow
  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCAmelCase_      :      List[Any]      =					TFCTRLModel.from_pretrained(lowercase_						)
        self.assertIsNotNone(lowercase_						)




@require_tf
class A_			(unittest.TestCase   ):




  '''simple docstring'''







  @slow
  def  UpperCamelCase__      (	self						):

     """simple docstring"""
     UpperCAmelCase_      :      Union[str, Any]      =					TFCTRLLMHeadModel.from_pretrained("ctrl"						)
     UpperCAmelCase_      :      Union[str, Any]      =					tf.convert_to_tensor([[1_1859, 0, 1611, 8]]       ,     dtype=tf.intaa						)  # Legal the president is
     UpperCAmelCase_      :      int      =					[
         1_1859,
         0,
         1611,
         8,
         5,
         150,
         2_6449,
         2,
         19,
         348,
         469,
         3,
         2595,
         48,
         2_0740,
         24_6533,
         24_6533,
         19,
         30,
         5,
     ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

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


