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




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


UpperCAmelCase__			:  Dict					     =     {"""configuration_hubert""": ["""HUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """HubertConfig"""]}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase__			:  Union[str, Any]					     =     [
      """HUBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """HubertForCTC""",
      """HubertForSequenceClassification""",
      """HubertModel""",
      """HubertPreTrainedModel""",
  ]


try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase__			:  List[Any]					     =     [
      """TF_HUBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """TFHubertForCTC""",
      """TFHubertModel""",
      """TFHubertPreTrainedModel""",
  ]

if TYPE_CHECKING:
  from .configuration_hubert import HUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, HubertConfig

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_hubert import (
        HUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
        HubertForCTC,
        HubertForSequenceClassification,
        HubertModel,
        HubertPreTrainedModel,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_hubert import (
        TF_HUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFHubertForCTC,
        TFHubertModel,
        TFHubertPreTrainedModel,
    )


else:
  import sys

  UpperCAmelCase__			:  str					     =     _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)




