




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



  def __init__(			self		,     __lowerCAmelCase		,     ):







      '''simple docstring'''


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



  def     a_							(			self ):







      '''simple docstring'''


      _a				:							Any							       =      ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size )

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

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

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

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

      _a				:							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		,     )

      _a				:							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     a_							(			self		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     *__lowerCAmelCase ):







      '''simple docstring'''


      _a				:							str							       =      TFCTRLModel(config=lowerCamelCase_ )
      _a				:							Optional[Any]							       =      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
      _a				:							Tuple							       =      model(lowerCamelCase_ )

      _a				:							Optional[int]							       =      [input_ids, None, input_mask]  # None is the input for 'past'
      _a				:							Optional[int]							       =      model(lowerCamelCase_ )

      _a				:							Tuple							       =      model(lowerCamelCase_ )

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



  def     a_							(			self		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     *__lowerCAmelCase ):







      '''simple docstring'''


      _a				:							Optional[int]							       =      TFCTRLLMHeadModel(config=lowerCamelCase_ )
      _a				:							int							       =      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
      _a				:							str							       =      model(lowerCamelCase_ )
      self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.vocab_size) )



  def     a_							(			self		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     *__lowerCAmelCase ):







      '''simple docstring'''


      _a				:							Tuple							       =      self.num_labels
      _a				:							Union[str, Any]							       =      ids_tensor([self.batch_size]		,     self.type_sequence_label_size )
      _a				:							Optional[int]							       =      {
          """input_ids""": input_ids,
          """token_type_ids""": token_type_ids,
          """labels""": sequence_labels,
      }
      _a				:							Optional[Any]							       =      TFCTRLForSequenceClassification(lowerCamelCase_ )
      _a				:							Optional[Any]							       =      model(lowerCamelCase_ )
      self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_labels) )



  def     a_							(			self ):







      '''simple docstring'''


      _a				:							Tuple							       =      self.prepare_config_and_inputs()

      (
          (
          _a							
      )       , (
          _a							
      )       , (
          _a							
      )       , (
          _a							
      )       , (
          _a							
      )       , (
          _a							
      )       , (
          _a							
      )       , (
          _a							
      )       , (
          _a							
      )       , 							
      )				:							Optional[Any]							       =      config_and_inputs

      _a				:							Optional[Any]							       =      {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
      return config, inputs_dict






@require_tf
class      UpperCamelCase       (	lowerCamelCase_      ,		lowerCamelCase_      ,		unittest.TestCase       ):
  a     :Tuple          =					(TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
  a     :Union[str, Any]          =					(TFCTRLLMHeadModel,) if is_tf_available() else ()
  a     :Optional[Any]          =					(
      {
          """feature-extraction""": TFCTRLModel,
          """text-classification""": TFCTRLForSequenceClassification,
          """text-generation""": TFCTRLLMHeadModel,
          """zero-shot""": TFCTRLForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )
  a     :Optional[int]          =					False
  a     :Any          =					False



  def     a_							(			self		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase		,     __lowerCAmelCase ):







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







      '''simple docstring'''


      _a				:							Any							       =      TFCTRLModelTester(self )
      _a				:							int							       =      ConfigTester(self		,     config_class=lowerCamelCase_		,     n_embd=37 )



  def     a_							(			self ):







      '''simple docstring'''


      self.config_tester.run_common_tests()



  def     a_							(			self ):







      '''simple docstring'''


      _a				:							List[str]							       =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_ctrl_model(*lowerCamelCase_ )



  def     a_							(			self ):







      '''simple docstring'''


      _a				:							Optional[Any]							       =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_ctrl_lm_head(*lowerCamelCase_ )



  def     a_							(			self ):







      '''simple docstring'''


      _a				:							Optional[Any]							       =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_ctrl_for_sequence_classification(*lowerCamelCase_ )



  def     a_							(			self ):







      '''simple docstring'''


      _a       , _a				:							str							       =      self.model_tester.prepare_config_and_inputs_for_common()
      _a				:							int							       =      [TFCTRLLMHeadModel]
      _a				:							Dict							       =      [TFCTRLForSequenceClassification]

      for model_class in self.all_model_classes:
          _a				:							Optional[Any]							       =      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:
              _a				:							Any							       =      model.get_output_embeddings()
              assert isinstance(lowerCamelCase_		,     tf.keras.layers.Layer )
              _a				:							Dict							       =      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:
              _a				:							List[Any]							       =      model.get_output_embeddings()
              assert isinstance(lowerCamelCase_		,     tf.keras.layers.Layer )
              _a				:							Optional[int]							       =      model.get_bias()
              assert name is None
          else:
              _a				:							int							       =      model.get_output_embeddings()
              assert x is None
              _a				:							Tuple							       =      model.get_bias()
              assert name is None



  @slow
  def     a_							(			self ):







      '''simple docstring'''


      for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          _a				:							Union[str, Any]							       =      TFCTRLModel.from_pretrained(lowerCamelCase_ )
          self.assertIsNotNone(lowerCamelCase_ )






@require_tf
class      UpperCamelCase       (	unittest.TestCase       ):



  @slow
  def     a_							(			self ):







      '''simple docstring'''


      _a				:							Dict							       =      TFCTRLLMHeadModel.from_pretrained("""ctrl""" )
      _a				:							str							       =      tf.convert_to_tensor([[1_1859, 0, 1611, 8]]		,     dtype=tf.intaa )  # Legal the president is
      _a				:							Optional[Any]							       =      [
          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

      _a				:							Optional[Any]							       =      model.generate(lowerCamelCase_		,     do_sample=lowerCamelCase_ )
      self.assertListEqual(output_ids[0].numpy().tolist()		,     lowerCamelCase_ )






from __future__ import annotations

__SCREAMING_SNAKE_CASE	:							Union[str, Any]           =  [2, 1, 4, 5, 6, 0, 8, 9, 1, 2, 0, 6, 4, 2, 0, 6, 5, 3, 2, 7]


class        __lowerCamelCase      :


				"""simple docstring"""







				def __init__(				self     :    Dict	,							lowerCamelCase_     :    int					):
								_lowerCAmelCase  						=-1
								_lowerCAmelCase  						=-1
								_lowerCAmelCase  						=[-1] * length
								_lowerCAmelCase  						=None
								_lowerCAmelCase  						=None






				def __repr__(				self     :    Tuple					):
								return F"Node(min_value={self.minn} max_value={self.maxx})"

def 		snake_case_   (				lowercase__     :    list[int]     ):






				'''simple docstring'''






				_lowerCAmelCase  						=Node(len(lowercase__     )     )
				_lowerCAmelCase						,							_lowerCAmelCase  						=min(lowercase__     ), max(lowercase__     )
				# Leaf node case where the node contains only one unique value
				if root.minn == root.maxx:
								return root
				_lowerCAmelCase  						=(root.minn + root.maxx) // 2

				_lowerCAmelCase  						=[]
				_lowerCAmelCase  						=[]

				for index, num in enumerate(lowercase__     ):
								if num <= pivot:
												left_arr.append(lowercase__     )
								else:
												right_arr.append(lowercase__     )
								_lowerCAmelCase  						=len(lowercase__     )
				_lowerCAmelCase  						=build_tree(lowercase__     )
				_lowerCAmelCase  						=build_tree(lowercase__     )
				return root

def 		snake_case_   (				lowercase__     :    Node | None       ,     lowercase__     :    int       ,     lowercase__     :    int     ):






				'''simple docstring'''






				if index < 0 or node is None:
								return 0
				# Leaf node cases
				if node.minn == node.maxx:
								return index + 1 if node.minn == num else 0
				_lowerCAmelCase  						=(node.minn + node.maxx) // 2
				if num <= pivot:
								# go the left subtree and map index to the left subtree
								return rank_till_index(node.left       ,     lowercase__       ,     node.map_left[index] - 1     )
				else:
								# go to the right subtree and map index to the right subtree
								return rank_till_index(node.right       ,     lowercase__       ,     index - node.map_left[index]     )

def 		snake_case_   (				lowercase__     :    Node | None       ,     lowercase__     :    int       ,     lowercase__     :    int       ,     lowercase__     :    int     ):






				'''simple docstring'''






				if start > end:
								return 0
				_lowerCAmelCase  						=rank_till_index(lowercase__       ,     lowercase__       ,     lowercase__     )
				_lowerCAmelCase  						=rank_till_index(lowercase__       ,     lowercase__       ,     start - 1     )
				return rank_till_end - rank_before_start

def 		snake_case_   (				lowercase__     :    Node | None       ,     lowercase__     :    int       ,     lowercase__     :    int       ,     lowercase__     :    int     ):






				'''simple docstring'''






				if index > (end - start) or start > end or node is None:
								return -1
				# Leaf node case
				if node.minn == node.maxx:
								return node.minn
				# Number of elements in the left subtree in interval [start, end]
				_lowerCAmelCase  						=node.map_left[end] - (
				    node.map_left[start - 1] if start else 0
				)
				if num_elements_in_left_tree > index:
								return quantile(
								    node.left       ,     lowercase__       ,     (node.map_left[start - 1] if start else 0)       ,     node.map_left[end] - 1       ,     )
				else:
								return quantile(
								    node.right       ,     index - num_elements_in_left_tree       ,     start - (node.map_left[start - 1] if start else 0)       ,     end - node.map_left[end]       ,     )

def 		snake_case_   (				lowercase__     :    Node | None       ,     lowercase__     :    int       ,     lowercase__     :    int       ,     lowercase__     :    int       ,     lowercase__     :    int     ):






				'''simple docstring'''






				if (
				    start > end
				    or node is None
				    or start_num > end_num
				    or node.minn > end_num
				    or node.maxx < start_num
				):
								return 0
				if start_num <= node.minn and node.maxx <= end_num:
								return end - start + 1
				_lowerCAmelCase  						=range_counting(
				    node.left       ,     (node.map_left[start - 1] if start else 0)       ,     node.map_left[end] - 1       ,     lowercase__       ,     lowercase__       ,     )
				_lowerCAmelCase  						=range_counting(
				    node.right       ,     start - (node.map_left[start - 1] if start else 0)       ,     end - node.map_left[end]       ,     lowercase__       ,     lowercase__       ,     )
				return left + right


if __name__ == "__main__":
		import doctest

		doctest.testmod()

