
'''simple docstring'''

import math
from dataclasses import dataclass
from typing import List, Optional, Tuple, Union

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, logging, randn_tensor
from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin


__UpperCAmelCase												=   logging.get_logger(__name__)  # pylint: disable=invalid-name


@dataclass
# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->EulerAncestralDiscrete
class 		_a     (							SCREAMING_SNAKE_CASE  ):







   """simple docstring"""




   A		= 42
   A		= None



def        SCREAMING_SNAKE_CASE_  (	snake_case_				:	List[str]   ,     snake_case_				:	List[Any]=0.999   ,     snake_case_				:	int="cosine"   ,     )       ->	List[str]:
 if alpha_transform_type == "cosine":

  def alpha_bar_fn(snake_case_				:	List[Any]							):
   return math.cos((t + 0.008) / 1.008 * math.pi / 2							) ** 2

 elif alpha_transform_type == "exp":

  def alpha_bar_fn(snake_case_				:	Dict							):
   return math.exp(t * -12.0							)

 else:
  raise ValueError(f"""Unsupported alpha_tranform_type: {alpha_transform_type}"""							)

 SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		[]
 for i in range(snake_case_							):
  SCREAMING_SNAKE_CASE  :  Optional[Any]								=		i / num_diffusion_timesteps
  SCREAMING_SNAKE_CASE  :  List[str]								=		(i + 1) / num_diffusion_timesteps
  betas.append(min(1 - alpha_bar_fn(snake_case_							) / alpha_bar_fn(snake_case_							)   ,     snake_case_							)							)
 return torch.tensor(snake_case_   ,     dtype=torch.floataa							)





class 		_a     (							SCREAMING_SNAKE_CASE			,    SCREAMING_SNAKE_CASE  ):







   """simple docstring"""




   A		= [e.name for e in KarrasDiffusionSchedulers]
   A		= 1






   @register_to_config
   def __init__(       self    ,__SCREAMING_SNAKE_CASE = 1000    ,__SCREAMING_SNAKE_CASE = 0.0001    ,__SCREAMING_SNAKE_CASE = 0.02    ,__SCREAMING_SNAKE_CASE = "linear"    ,__SCREAMING_SNAKE_CASE = None    ,__SCREAMING_SNAKE_CASE = "epsilon"    ,__SCREAMING_SNAKE_CASE = "linspace"    ,__SCREAMING_SNAKE_CASE = 0    ,):
    if trained_betas is not None:
     SCREAMING_SNAKE_CASE  :  Optional[int]								=		torch.tensor(__SCREAMING_SNAKE_CASE    ,dtype=torch.floataa      )
    elif beta_schedule == "linear":
     SCREAMING_SNAKE_CASE  :  Any								=		torch.linspace(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,dtype=torch.floataa      )
    elif beta_schedule == "scaled_linear":
     # this schedule is very specific to the latent diffusion model.
     SCREAMING_SNAKE_CASE  :  str								=		(
         torch.linspace(beta_start**0.5    ,beta_end**0.5    ,__SCREAMING_SNAKE_CASE    ,dtype=torch.floataa      ) ** 2
     )
    elif beta_schedule == "squaredcos_cap_v2":
     # Glide cosine schedule
     SCREAMING_SNAKE_CASE  :  str								=		betas_for_alpha_bar(__SCREAMING_SNAKE_CASE      )
    else:
     raise NotImplementedError(f"""{beta_schedule} does is not implemented for {self.__class__}"""      )

    SCREAMING_SNAKE_CASE  :  int								=		1.0 - self.betas
    SCREAMING_SNAKE_CASE  :  Optional[Any]								=		torch.cumprod(self.alphas    ,dim=0      )

    SCREAMING_SNAKE_CASE  :  List[str]								=		np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5      )
    SCREAMING_SNAKE_CASE  :  int								=		np.concatenate([sigmas[::-1], [0.0]]      ).astype(np.floataa      )
    SCREAMING_SNAKE_CASE  :  Tuple								=		torch.from_numpy(__SCREAMING_SNAKE_CASE      )

    # setable values
    SCREAMING_SNAKE_CASE  :  int								=		None
    SCREAMING_SNAKE_CASE  :  Any								=		np.linspace(0    ,num_train_timesteps - 1    ,__SCREAMING_SNAKE_CASE    ,dtype=__SCREAMING_SNAKE_CASE      )[::-1].copy()
    SCREAMING_SNAKE_CASE  :  str								=		torch.from_numpy(__SCREAMING_SNAKE_CASE      )
    SCREAMING_SNAKE_CASE  :  int								=		False






   @property
   def 					__a			(       self      ):
    # standard deviation of the initial noise distribution
    if self.config.timestep_spacing in ["linspace", "trailing"]:
     return self.sigmas.max()

    return (self.sigmas.max() ** 2 + 1) ** 0.5






   def 					__a			(       self    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      ):
    if isinstance(__SCREAMING_SNAKE_CASE    ,torch.Tensor      ):
     SCREAMING_SNAKE_CASE  :  int								=		timestep.to(self.timesteps.device      )
    SCREAMING_SNAKE_CASE  :  int								=		(self.timesteps == timestep).nonzero().item()
    SCREAMING_SNAKE_CASE  :  int								=		self.sigmas[step_index]
    SCREAMING_SNAKE_CASE  :  str								=		sample / ((sigma**2 + 1) ** 0.5)
    SCREAMING_SNAKE_CASE  :  Dict								=		True
    return sample






   def 					__a			(       self    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE = None      ):
    SCREAMING_SNAKE_CASE  :  Any								=		num_inference_steps

    # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891
    if self.config.timestep_spacing == "linspace":
     SCREAMING_SNAKE_CASE  :  Any								=		np.linspace(0    ,self.config.num_train_timesteps - 1    ,__SCREAMING_SNAKE_CASE    ,dtype=__SCREAMING_SNAKE_CASE      )[
         ::-1
     ].copy()
    elif self.config.timestep_spacing == "leading":
     SCREAMING_SNAKE_CASE  :  Tuple								=		self.config.num_train_timesteps // self.num_inference_steps
     # creates integer timesteps by multiplying by ratio
     # casting to int to avoid issues when num_inference_step is power of 3
     SCREAMING_SNAKE_CASE  :  Dict								=		(np.arange(0    ,__SCREAMING_SNAKE_CASE      ) * step_ratio).round()[::-1].copy().astype(__SCREAMING_SNAKE_CASE      )
     timesteps += self.config.steps_offset
    elif self.config.timestep_spacing == "trailing":
     SCREAMING_SNAKE_CASE  :  Tuple								=		self.config.num_train_timesteps / self.num_inference_steps
     # creates integer timesteps by multiplying by ratio
     # casting to int to avoid issues when num_inference_step is power of 3
     SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		(np.arange(self.config.num_train_timesteps    ,0    ,-step_ratio      )).round().copy().astype(__SCREAMING_SNAKE_CASE      )
     timesteps -= 1
    else:
     raise ValueError(
         f"""{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'."""      )

    SCREAMING_SNAKE_CASE  :  List[Any]								=		np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5      )
    SCREAMING_SNAKE_CASE  :  List[str]								=		np.interp(__SCREAMING_SNAKE_CASE    ,np.arange(0    ,len(__SCREAMING_SNAKE_CASE      )      )    ,__SCREAMING_SNAKE_CASE      )
    SCREAMING_SNAKE_CASE  :  List[str]								=		np.concatenate([sigmas, [0.0]]      ).astype(np.floataa      )
    SCREAMING_SNAKE_CASE  :  str								=		torch.from_numpy(__SCREAMING_SNAKE_CASE      ).to(device=__SCREAMING_SNAKE_CASE      )
    if str(__SCREAMING_SNAKE_CASE      ).startswith('mps'      ):
     # mps does not support float64
     SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		torch.from_numpy(__SCREAMING_SNAKE_CASE      ).to(__SCREAMING_SNAKE_CASE    ,dtype=torch.floataa      )
    else:
     SCREAMING_SNAKE_CASE  :  Optional[int]								=		torch.from_numpy(__SCREAMING_SNAKE_CASE      ).to(device=__SCREAMING_SNAKE_CASE      )






   def 					__a			(       self    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE = None    ,__SCREAMING_SNAKE_CASE = True    ,):

    if (
        isinstance(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      )
        or isinstance(__SCREAMING_SNAKE_CASE    ,torch.IntTensor      )
        or isinstance(__SCREAMING_SNAKE_CASE    ,torch.LongTensor      )
    ):
     raise ValueError(
         (
             'Passing integer indices (e.g. from `enumerate(timesteps)`) as timesteps to'
             ' `EulerDiscreteScheduler.step()` is not supported. Make sure to pass'
             ' one of the `scheduler.timesteps` as a timestep.'
         )    ,)

    if not self.is_scale_input_called:
     logger.warning(
         'The `scale_model_input` function should be called before `step` to ensure correct denoising. '
         'See `StableDiffusionPipeline` for a usage example.'      )

    if isinstance(__SCREAMING_SNAKE_CASE    ,torch.Tensor      ):
     SCREAMING_SNAKE_CASE  :  List[Any]								=		timestep.to(self.timesteps.device      )

    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		(self.timesteps == timestep).nonzero().item()
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		self.sigmas[step_index]

    # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
    if self.config.prediction_type == "epsilon":
     SCREAMING_SNAKE_CASE  :  Tuple								=		sample - sigma * model_output
    elif self.config.prediction_type == "v_prediction":
     # * c_out + input * c_skip
     SCREAMING_SNAKE_CASE  :  List[Any]								=		model_output * (-sigma / (sigma**2 + 1) ** 0.5) + (sample / (sigma**2 + 1))
    elif self.config.prediction_type == "sample":
     raise NotImplementedError('prediction_type not implemented yet: sample'      )
    else:
     raise ValueError(
         f"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`"""      )

    SCREAMING_SNAKE_CASE  :  Tuple								=		self.sigmas[step_index]
    SCREAMING_SNAKE_CASE  :  Any								=		self.sigmas[step_index + 1]
    SCREAMING_SNAKE_CASE  :  Tuple								=		(sigma_to**2 * (sigma_from**2 - sigma_to**2) / sigma_from**2) ** 0.5
    SCREAMING_SNAKE_CASE  :  Tuple								=		(sigma_to**2 - sigma_up**2) ** 0.5

    # 2. Convert to an ODE derivative
    SCREAMING_SNAKE_CASE  :  Optional[int]								=		(sample - pred_original_sample) / sigma

    SCREAMING_SNAKE_CASE  :  str								=		sigma_down - sigma

    SCREAMING_SNAKE_CASE  :  Any								=		sample + derivative * dt

    SCREAMING_SNAKE_CASE  :  Tuple								=		model_output.device
    SCREAMING_SNAKE_CASE  :  Optional[int]								=		randn_tensor(model_output.shape    ,dtype=model_output.dtype    ,device=__SCREAMING_SNAKE_CASE    ,generator=__SCREAMING_SNAKE_CASE      )

    SCREAMING_SNAKE_CASE  :  List[str]								=		prev_sample + noise * sigma_up

    if not return_dict:
     return (prev_sample,)

    return EulerAncestralDiscreteSchedulerOutput(
        prev_sample=__SCREAMING_SNAKE_CASE    ,pred_original_sample=__SCREAMING_SNAKE_CASE      )






   def 					__a			(       self    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,):
    # Make sure sigmas and timesteps have the same device and dtype as original_samples
    SCREAMING_SNAKE_CASE  :  Dict								=		self.sigmas.to(device=original_samples.device    ,dtype=original_samples.dtype      )
    if original_samples.device.type == "mps" and torch.is_floating_point(__SCREAMING_SNAKE_CASE      ):
     # mps does not support float64
     SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		self.timesteps.to(original_samples.device    ,dtype=torch.floataa      )
     SCREAMING_SNAKE_CASE  :  Dict								=		timesteps.to(original_samples.device    ,dtype=torch.floataa      )
    else:
     SCREAMING_SNAKE_CASE  :  List[Any]								=		self.timesteps.to(original_samples.device      )
     SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		timesteps.to(original_samples.device      )

    SCREAMING_SNAKE_CASE  :  List[str]								=		[(schedule_timesteps == t).nonzero().item() for t in timesteps]

    SCREAMING_SNAKE_CASE  :  str								=		sigmas[step_indices].flatten()
    while len(sigma.shape      ) < len(original_samples.shape      ):
     SCREAMING_SNAKE_CASE  :  Dict								=		sigma.unsqueeze(-1      )

    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		original_samples + noise * sigma
    return noisy_samples






   def __len__(       self      ):
    return self.config.num_train_timesteps




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


