






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      :    Union[str, Any]	,   __lowercase      :    Optional[Any]	,   )							->   List[Any]:
        __UpperCAmelCase							:			str     =						parent
        __UpperCAmelCase							:			Any     =						13
        __UpperCAmelCase							:			Union[str, Any]     =						7
        __UpperCAmelCase							:			Tuple     =						True
        __UpperCAmelCase							:			Any     =						True
        __UpperCAmelCase							:			Optional[int]     =						True
        __UpperCAmelCase							:			Optional[int]     =						True
        __UpperCAmelCase							:			List[Any]     =						True
        __UpperCAmelCase							:			Any     =						99
        __UpperCAmelCase							:			str     =						32
        __UpperCAmelCase							:			Optional[Any]     =						2
        __UpperCAmelCase							:			Any     =						4
        __UpperCAmelCase							:			Union[str, Any]     =						37
        __UpperCAmelCase							:			Optional[int]     =						"""gelu"""
        __UpperCAmelCase							:			str     =						0.1
        __UpperCAmelCase							:			Optional[int]     =						0.1
        __UpperCAmelCase							:			Union[str, Any]     =						512
        __UpperCAmelCase							:			Dict     =						16
        __UpperCAmelCase							:			Optional[int]     =						2
        __UpperCAmelCase							:			List[str]     =						0.02
        __UpperCAmelCase							:			Optional[Any]     =						3
        __UpperCAmelCase							:			str     =						4
        __UpperCAmelCase							:			Dict     =						None
        __UpperCAmelCase							:			Any     =						self.vocab_size - 1
      def 			UpperCAmelCase   (   self      :    List[Any]      )							->   str:
        __UpperCAmelCase							:			Union[str, Any]     =						ids_tensor([self.batch_size, self.seq_length]	,   self.vocab_size      )

        __UpperCAmelCase							:			Any     =						None
        if self.use_input_mask:
          __UpperCAmelCase							:			Union[str, Any]     =						random_attention_mask([self.batch_size, self.seq_length]      )

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

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

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

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

        __UpperCAmelCase							:			List[str]     =						[input_ids, None, input_mask]  # None is the input for 'past'
        __UpperCAmelCase							:			Dict     =						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      :    str	,   __lowercase      :    List[Any]	,   __lowercase      :    str	,   __lowercase      :    Union[str, Any]	,   __lowercase      :    int	,   __lowercase      :    Dict	,   *__lowercase      :    Optional[Any]      )							->   Dict:
        __UpperCAmelCase							:			Any     =						TFCTRLLMHeadModel(config=__lowercase      )
        __UpperCAmelCase							:			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      :    int	,   __lowercase      :    List[Any]	,   __lowercase      :    Dict	,   __lowercase      :    Tuple	,   __lowercase      :    Dict	,   __lowercase      :    List[str]	,   *__lowercase      :    Union[str, Any]      )							->   Optional[Any]:
        __UpperCAmelCase							:			Union[str, Any]     =						self.num_labels
        __UpperCAmelCase							:			Dict     =						ids_tensor([self.batch_size]	,   self.type_sequence_label_size      )
        __UpperCAmelCase							:			str     =						{
            """input_ids""": input_ids,
            """token_type_ids""": token_type_ids,
            """labels""": sequence_labels,
        }
        __UpperCAmelCase							:			Optional[Any]     =						TFCTRLForSequenceClassification(__lowercase      )
        __UpperCAmelCase							:			List[str]     =						model(__lowercase      )
        self.parent.assertEqual(result.logits.shape	,   (self.batch_size, self.num_labels)      )




      def 			UpperCAmelCase   (   self      :    Optional[Any]      )							->   Union[str, Any]:
        __UpperCAmelCase							:			Any     =						self.prepare_config_and_inputs()

        (
            (
            __UpperCAmelCase
        )   ,				(
            __UpperCAmelCase
        )   ,				(
            __UpperCAmelCase
        )   ,				(
            __UpperCAmelCase
        )   ,				(
            __UpperCAmelCase
        )   ,				(
            __UpperCAmelCase
        )   ,				(
            __UpperCAmelCase
        )   ,				(
            __UpperCAmelCase
        )   ,				(
            __UpperCAmelCase
        )   ,				
        )							:			List[str]     =						config_and_inputs

        __UpperCAmelCase							:			Tuple     =						{"""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"""


      a						:				List[str]							 =			(TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
      a						:				Union[str, Any]							 =			(TFCTRLLMHeadModel,) if is_tf_available() else ()
      a						:				List[Any]							 =			(
          {
              'feature-extraction': TFCTRLModel,
              'text-classification': TFCTRLForSequenceClassification,
              'text-generation': TFCTRLLMHeadModel,
              'zero-shot': TFCTRLForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      a						:				Union[str, Any]							 =			False
      a						:				str							 =			False
      def 			UpperCAmelCase   (   self      :    Tuple	,   __lowercase      :    Optional[Any]	,   __lowercase      :    Union[str, Any]	,   __lowercase      :    Any	,   __lowercase      :    List[str]	,   __lowercase      :    Union[str, Any]      )							->   Tuple:
        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      :    Optional[Any]      )							->   Optional[int]:
        __UpperCAmelCase							:			List[Any]     =						TFCTRLModelTester(self      )
        __UpperCAmelCase							:			Optional[Any]     =						ConfigTester(self	,   config_class=__lowercase	,   n_embd=37      )
      def 			UpperCAmelCase   (   self      :    List[str]      )							->   Optional[Any]:
        self.config_tester.run_common_tests()
      def 			UpperCAmelCase   (   self      :    Tuple      )							->   Optional[int]:
        __UpperCAmelCase							:			str     =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_ctrl_model(*__lowercase      )
      def 			UpperCAmelCase   (   self      :    Any      )							->   int:
        __UpperCAmelCase							:			Optional[Any]     =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_ctrl_lm_head(*__lowercase      )
      def 			UpperCAmelCase   (   self      :    Union[str, Any]      )							->   Dict:
        __UpperCAmelCase							:			str     =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_ctrl_for_sequence_classification(*__lowercase      )
      def 			UpperCAmelCase   (   self      :    Optional[int]      )							->   int:
        __UpperCAmelCase   ,				__UpperCAmelCase							:			str     =						self.model_tester.prepare_config_and_inputs_for_common()
        __UpperCAmelCase							:			Dict     =						[TFCTRLLMHeadModel]
        __UpperCAmelCase							:			str     =						[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							:			Union[str, Any]     =						model.get_output_embeddings()
            assert isinstance(__lowercase	,   tf.keras.layers.Layer      )
            __UpperCAmelCase							:			Optional[int]     =						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							:			Optional[int]     =						model.get_output_embeddings()
            assert isinstance(__lowercase	,   tf.keras.layers.Layer      )
            __UpperCAmelCase							:			Tuple     =						model.get_bias()
            assert name is None
          else:
            __UpperCAmelCase							:			Tuple     =						model.get_output_embeddings()
            assert x is None
            __UpperCAmelCase							:			Optional[Any]     =						model.get_bias()
            assert name is None




      @slow
      def 			UpperCAmelCase   (   self      :    str      )							->   Optional[Any]:
        for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          __UpperCAmelCase							:			Dict     =						TFCTRLModel.from_pretrained(__lowercase      )
          self.assertIsNotNone(__lowercase      )


@require_tf
class 					a					(				unittest.TestCase     ):



      """simple docstring"""


      @slow
      def 			UpperCAmelCase   (   self      :    int      )							->   Union[str, Any]:
        __UpperCAmelCase							:			int     =						TFCTRLLMHeadModel.from_pretrained("""ctrl"""      )
        __UpperCAmelCase							:			int     =						tf.convert_to_tensor([[11859, 0, 1611, 8]]	,   dtype=tf.intaa      )  # Legal the president is
        __UpperCAmelCase							:			List[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

        __UpperCAmelCase							:			Union[str, Any]     =						model.generate(__lowercase	,   do_sample=__lowercase      )
        self.assertListEqual(output_ids[0].numpy().tolist()	,   __lowercase      )










import enum
import inspect
import warnings
from functools import wraps
from typing import Callable, Optional

from .logging import get_logger


a	: Any      =				set()
a	: List[Any]      =				get_logger(__name__)






def        lowerCamelCase__   (					__lowerCamelCase    :  Optional[str] = None	):

  def decorator(__lowerCamelCase    :  Callable	):
    global _emitted_deprecation_warnings

    if inspect.isclass(__lowerCamelCase	):
      __UpperCAmelCase							:			Optional[Any]     =						deprecated_class_or_function.__init__
      __UpperCAmelCase							:			int     =						deprecated_class_or_function.__name__
    else:
      __UpperCAmelCase							:			Any     =						deprecated_class_or_function
      __UpperCAmelCase							:			Any     =						deprecated_function.__name__
      # Support deprecating __init__ class method: class name instead
      __UpperCAmelCase							:			List[str]     =						name if name != """__init__""" else deprecated_function.__qualname__.split("""."""	)[-2]

    __UpperCAmelCase							:			Union[str, Any]     =						(
        f"""{name} is deprecated and will be removed in the next major version of datasets.""" + f""" {help_message}"""
        if help_message
        else """"""
    )

    @wraps(__lowerCamelCase	)
    def wrapper(*__lowerCamelCase    :  List[str]						,	**__lowerCamelCase    :  Tuple	):
      __UpperCAmelCase							:			Optional[Any]     =						hash(__lowerCamelCase	)
      if func_hash not in _emitted_deprecation_warnings:
        warnings.warn(__lowerCamelCase						,	category=__lowerCamelCase						,	stacklevel=2	)
        _emitted_deprecation_warnings.add(__lowerCamelCase	)
      return deprecated_function(*__lowerCamelCase						,	**__lowerCamelCase	)

    __UpperCAmelCase							:			List[str]     =						"""deprecated"""

    if inspect.isclass(__lowerCamelCase	):
      __UpperCAmelCase							:			int     =						wrapper
      return deprecated_class_or_function
    else:
      return wrapper

  return decorator
class 					a					(				enum.EnumMeta     ):



      """simple docstring"""


      def __getattribute__(   cls      :    Optional[int]	,   __lowercase      :    List[Any]      )							->   str:
        __UpperCAmelCase							:			List[Any]     =						super().__getattribute__(__lowercase      )
        if isinstance(__lowercase	,   enum.Enum      ) and obj._on_access:
          obj._on_access()
        return obj
      def __getitem__(   cls      :    Union[str, Any]	,   __lowercase      :    Optional[int]      )							->   Optional[int]:
        __UpperCAmelCase							:			Union[str, Any]     =						super().__getitem__(__lowercase      )
        if member._on_access:
          member._on_access()
        return member




      def __call__(   cls      :    List[str]	,   __lowercase      :    Dict	,   __lowercase      :    List[Any]=None	,   *, __lowercase      :    Dict=None	,   __lowercase      :    Any=None	,   __lowercase      :    Optional[Any]=None	,   __lowercase      :    Dict=1      )							->   List[Any]:
        __UpperCAmelCase							:			Union[str, Any]     =						super().__call__(__lowercase	,   __lowercase	,   module=__lowercase	,   qualname=__lowercase	,   type=__lowercase	,   start=__lowercase      )
        if isinstance(__lowercase	,   enum.Enum      ) and obj._on_access:
          obj._on_access()
        return obj


class 					a					(				enum.Enum							, metaclass=lowercase__     ):



      """simple docstring"""


      def __new__(   cls      :    Dict	,   __lowercase      :    Tuple      )							->   Optional[Any]:
        __UpperCAmelCase							:			List[Any]     =						object.__new__(cls      )
        __UpperCAmelCase							:			Any     =						value
        __UpperCAmelCase							:			int     =						member.deprecate
        return member
      @property
      def 			UpperCAmelCase   (   self      :    Optional[Any]      )							->   Dict:
        return ""




      def 			UpperCAmelCase   (   self      :    str      )							->   Dict:
        __UpperCAmelCase							:			Optional[int]     =						f""" {self.help_message}""" if self.help_message else """"""
        warnings.warn(
            f"""'{self.__objclass__.__name__}' is deprecated and will be removed in the next major version of datasets."""
            + help_message	,   __lowercase	,   stacklevel=3	,   )


