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



  '''simple docstring'''






  def __init__(self	:			Tuple						,			lowerCAmelCase_	:			List[Any]						,			) ->							List[Any]:
        snake_case_    							=				parent
        snake_case_    							=				13
        snake_case_    							=				7
        snake_case_    							=				True
        snake_case_    							=				True
        snake_case_    							=				True
        snake_case_    							=				True
        snake_case_    							=				True
        snake_case_    							=				99
        snake_case_    							=				32
        snake_case_    							=				2
        snake_case_    							=				4
        snake_case_    							=				37
        snake_case_    							=				"gelu"
        snake_case_    							=				0.1
        snake_case_    							=				0.1
        snake_case_    							=				5_12
        snake_case_    							=				16
        snake_case_    							=				2
        snake_case_    							=				0.02
        snake_case_    							=				3
        snake_case_    							=				4
        snake_case_    							=				None
        snake_case_    							=				self.vocab_size - 1

  def 		_lowerCamelCase						(self	:			int     ) ->							Tuple:
        snake_case_    							=				ids_tensor([self.batch_size, self.seq_length]						,			self.vocab_size     )

        snake_case_    							=				None
        if self.use_input_mask:
              snake_case_    							=				random_attention_mask([self.batch_size, self.seq_length]     )

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

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

        snake_case_    							=				None
        snake_case_    							=				None
        snake_case_    							=				None
        if self.use_labels:
              snake_case_    							=				ids_tensor([self.batch_size]						,			self.type_sequence_label_size     )
              snake_case_    							=				ids_tensor([self.batch_size, self.seq_length]						,			self.num_labels     )
              snake_case_    							=				ids_tensor([self.batch_size]						,			self.num_choices     )

        snake_case_    							=				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						,			)

        snake_case_    							=				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 		_lowerCamelCase						(self	:			str						,			lowerCAmelCase_	:			List[str]						,			lowerCAmelCase_	:			Optional[Any]						,			lowerCAmelCase_	:			List[str]						,			lowerCAmelCase_	:			int						,			lowerCAmelCase_	:			Tuple						,			*lowerCAmelCase_	:			Dict     ) ->							str:
        snake_case_    							=				TFCTRLModel(config=lowerCAmelCase_     )
        snake_case_    							=				{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
        snake_case_    							=				model(lowerCAmelCase_     )

        snake_case_    							=				[input_ids, None, input_mask]  # None is the input for 'past'
        snake_case_    							=				model(lowerCAmelCase_     )

        snake_case_    							=				model(lowerCAmelCase_     )

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

  def 		_lowerCamelCase						(self	:			Union[str, Any]						,			lowerCAmelCase_	:			List[str]						,			lowerCAmelCase_	:			Optional[Any]						,			lowerCAmelCase_	:			List[Any]						,			lowerCAmelCase_	:			Optional[int]						,			lowerCAmelCase_	:			Union[str, Any]						,			*lowerCAmelCase_	:			Union[str, Any]     ) ->							Dict:
        snake_case_    							=				TFCTRLLMHeadModel(config=lowerCAmelCase_     )
        snake_case_    							=				{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
        snake_case_    							=				model(lowerCAmelCase_     )
        self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.seq_length, self.vocab_size)     )

  def 		_lowerCamelCase						(self	:			List[str]						,			lowerCAmelCase_	:			Tuple						,			lowerCAmelCase_	:			Any						,			lowerCAmelCase_	:			str						,			lowerCAmelCase_	:			Tuple						,			lowerCAmelCase_	:			List[str]						,			*lowerCAmelCase_	:			List[Any]     ) ->							List[Any]:
        snake_case_    							=				self.num_labels
        snake_case_    							=				ids_tensor([self.batch_size]						,			self.type_sequence_label_size     )
        snake_case_    							=				{
            "input_ids": input_ids,
            "token_type_ids": token_type_ids,
            "labels": sequence_labels,
        }
        snake_case_    							=				TFCTRLForSequenceClassification(lowerCAmelCase_     )
        snake_case_    							=				model(lowerCAmelCase_     )
        self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.num_labels)     )


  def 		_lowerCamelCase						(self	:			List[str]     ) ->							Union[str, Any]:
        snake_case_    							=				self.prepare_config_and_inputs()

        (
            (
            snake_case_
        )							,						(
            snake_case_
        )							,						(
            snake_case_
        )							,						(
            snake_case_
        )							,						(
            snake_case_
        )							,						(
            snake_case_
        )							,						(
            snake_case_
        )							,						(
            snake_case_
        )							,						(
            snake_case_
        )							,						
        )    							=				config_and_inputs

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



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



  '''simple docstring'''






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

  def 		_lowerCamelCase						(self	:			Optional[int]						,			lowerCAmelCase_	:			Dict						,			lowerCAmelCase_	:			int						,			lowerCAmelCase_	:			Union[str, Any]						,			lowerCAmelCase_	:			Any						,			lowerCAmelCase_	:			Optional[int]     ) ->							int:
        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 		_lowerCamelCase						(self	:			Optional[int]     ) ->							str:
        snake_case_    							=				TFCTRLModelTester(self     )
        snake_case_    							=				ConfigTester(self						,			config_class=lowerCAmelCase_						,			n_embd=37     )

  def 		_lowerCamelCase						(self	:			Dict     ) ->							Dict:
        self.config_tester.run_common_tests()

  def 		_lowerCamelCase						(self	:			Dict     ) ->							Union[str, Any]:
        snake_case_    							=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_ctrl_model(*lowerCAmelCase_     )

  def 		_lowerCamelCase						(self	:			str     ) ->							List[str]:
        snake_case_    							=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_ctrl_lm_head(*lowerCAmelCase_     )

  def 		_lowerCamelCase						(self	:			str     ) ->							List[str]:
        snake_case_    							=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_ctrl_for_sequence_classification(*lowerCAmelCase_     )

  def 		_lowerCamelCase						(self	:			Union[str, Any]     ) ->							List[Any]:
        snake_case_							,						snake_case_    							=				self.model_tester.prepare_config_and_inputs_for_common()
        snake_case_    							=				[TFCTRLLMHeadModel]
        snake_case_    							=				[TFCTRLForSequenceClassification]

        for model_class in self.all_model_classes:
              snake_case_    							=				model_class(lowerCAmelCase_     )
              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:
                    snake_case_    							=				model.get_output_embeddings()
                    assert isinstance(lowerCAmelCase_						,			tf.keras.layers.Layer     )
                    snake_case_    							=				model.get_bias()
                    assert isinstance(lowerCAmelCase_						,			lowerCAmelCase_     )
                    for k, v in name.items():
                          assert isinstance(lowerCAmelCase_						,			tf.Variable     )
              elif model_class in list_other_models_with_output_ebd:
                    snake_case_    							=				model.get_output_embeddings()
                    assert isinstance(lowerCAmelCase_						,			tf.keras.layers.Layer     )
                    snake_case_    							=				model.get_bias()
                    assert name is None
              else:
                    snake_case_    							=				model.get_output_embeddings()
                    assert x is None
                    snake_case_    							=				model.get_bias()
                    assert name is None


  @slow
  def 		_lowerCamelCase						(self	:			Optional[Any]     ) ->							List[Any]:
        for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              snake_case_    							=				TFCTRLModel.from_pretrained(lowerCAmelCase_     )
              self.assertIsNotNone(lowerCAmelCase_     )



@require_tf
class 			lowerCAmelCase__ (							unittest.TestCase							):



  '''simple docstring'''






  @slow
  def 		_lowerCamelCase						(self	:			Optional[Any]     ) ->							Tuple:
        snake_case_    							=				TFCTRLLMHeadModel.from_pretrained("ctrl"     )
        snake_case_    							=				tf.convert_to_tensor([[1_18_59, 0, 16_11, 8]]						,			dtype=tf.intaa     )  # Legal the president is
        snake_case_    							=				[
            1_18_59,
            0,
            16_11,
            8,
            5,
            1_50,
            2_64_49,
            2,
            19,
            3_48,
            4_69,
            3,
            25_95,
            48,
            2_07_40,
            24_65_33,
            24_65_33,
            19,
            30,
            5,
        ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

        snake_case_    							=				model.generate(lowerCAmelCase_						,			do_sample=lowerCAmelCase_     )
        self.assertListEqual(output_ids[0].numpy().tolist()						,			lowerCAmelCase_     )






import os

from packaging import version

from .. import __version__
from .accelerate_utils import apply_forward_hook
from .constants import (
    CONFIG_NAME,
    DEPRECATED_REVISION_ARGS,
    DIFFUSERS_CACHE,
    DIFFUSERS_DYNAMIC_MODULE_NAME,
    FLAX_WEIGHTS_NAME,
    HF_MODULES_CACHE,
    HUGGINGFACE_CO_RESOLVE_ENDPOINT,
    ONNX_EXTERNAL_WEIGHTS_NAME,
    ONNX_WEIGHTS_NAME,
    SAFETENSORS_WEIGHTS_NAME,
    TEXT_ENCODER_ATTN_MODULE,
    WEIGHTS_NAME,
)
from .deprecation_utils import deprecate
from .doc_utils import replace_example_docstring
from .dynamic_modules_utils import get_class_from_dynamic_module
from .hub_utils import (
    HF_HUB_OFFLINE,
    _add_variant,
    _get_model_file,
    extract_commit_hash,
    http_user_agent,
)
from .import_utils import (
    BACKENDS_MAPPING,
    ENV_VARS_TRUE_AND_AUTO_VALUES,
    ENV_VARS_TRUE_VALUES,
    USE_JAX,
    USE_TF,
    USE_TORCH,
    DummyObject,
    OptionalDependencyNotAvailable,
    is_accelerate_available,
    is_accelerate_version,
    is_bsa_available,
    is_flax_available,
    is_ftfy_available,
    is_inflect_available,
    is_invisible_watermark_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_librosa_available,
    is_note_seq_available,
    is_omegaconf_available,
    is_onnx_available,
    is_safetensors_available,
    is_scipy_available,
    is_tensorboard_available,
    is_tf_available,
    is_torch_available,
    is_torch_version,
    is_torchsde_available,
    is_transformers_available,
    is_transformers_version,
    is_unidecode_available,
    is_wandb_available,
    is_xformers_available,
    requires_backends,
)
from .logging import get_logger
from .outputs import BaseOutput
from .pil_utils import PIL_INTERPOLATION, numpy_to_pil, pt_to_pil
from .torch_utils import is_compiled_module, randn_tensor


if is_torch_available():
		from .testing_utils import (
		    floats_tensor,
		    load_hf_numpy,
		    load_image,
		    load_numpy,
		    load_pt,
		    nightly,
		    parse_flag_from_env,
		    print_tensor_test,
		    require_torch_a,
		    require_torch_gpu,
		    skip_mps,
		    slow,
		    torch_all_close,
		    torch_device,
		)
		from .torch_utils import maybe_allow_in_graph

from .testing_utils import export_to_gif, export_to_video


__magic_name__       =     get_logger(__name__)





def 		__snake_case						(    _UpperCAmelCase				):
					"""simple docstring"""






					if version.parse(_UpperCAmelCase				) < version.parse(_UpperCAmelCase				):
										if "dev" in min_version:
															lowercase						   =       (
															    'This example requires a source install from HuggingFace diffusers (see '
															    '`https://huggingface.co/docs/diffusers/installation#install-from-source`),'
															)
										else:
															lowercase						   =       f"""This example requires a minimum version of {min_version},"""
										error_message += f""" but the version found is {__version__}.\n"""
										raise ImportError(_UpperCAmelCase				)






