






'''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 					lowercase							(				_lowerCamelCase   ):


    '''simple docstring'''

    def __init__(       self  :  str					,  __lowerCamelCase  :  int					,  )				->				int:

     '''simple docstring'''




     lowerCamelCase__	=  parent
     lowerCamelCase__	=  13
     lowerCamelCase__	=  7
     lowerCamelCase__	=  True
     lowerCamelCase__	=  True
     lowerCamelCase__	=  True
     lowerCamelCase__	=  True
     lowerCamelCase__	=  True
     lowerCamelCase__	=  99
     lowerCamelCase__	=  32
     lowerCamelCase__	=  2
     lowerCamelCase__	=  4
     lowerCamelCase__	=  37
     lowerCamelCase__	=  "gelu"
     lowerCamelCase__	=  0.1
     lowerCamelCase__	=  0.1
     lowerCamelCase__	=  512
     lowerCamelCase__	=  16
     lowerCamelCase__	=  2
     lowerCamelCase__	=  0.0_2
     lowerCamelCase__	=  3
     lowerCamelCase__	=  4
     lowerCamelCase__	=  None
     lowerCamelCase__	=  self.vocab_size - 1

    def  a__					(       self  :  Any  )				->				int:

     '''simple docstring'''




     lowerCamelCase__	=  ids_tensor([self.batch_size, self.seq_length]					,  self.vocab_size  )

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

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

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

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

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

     lowerCamelCase__	=  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  :  Dict					,  __lowerCamelCase  :  List[Any]					,  __lowerCamelCase  :  Optional[Any]					,  __lowerCamelCase  :  List[str]					,  __lowerCamelCase  :  Optional[Any]					,  __lowerCamelCase  :  List[str]					,  *__lowerCamelCase  :  Optional[Any]  )				->				List[Any]:

     '''simple docstring'''




     lowerCamelCase__	=  TFCTRLModel(config=__lowerCamelCase  )
     lowerCamelCase__	=  {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
     lowerCamelCase__	=  model(__lowerCamelCase  )

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

     lowerCamelCase__	=  model(__lowerCamelCase  )

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

    def  a__					(       self  :  Union[str, Any]					,  __lowerCamelCase  :  Union[str, Any]					,  __lowerCamelCase  :  Optional[Any]					,  __lowerCamelCase  :  Optional[Any]					,  __lowerCamelCase  :  int					,  __lowerCamelCase  :  str					,  *__lowerCamelCase  :  List[str]  )				->				Optional[int]:

     '''simple docstring'''




     lowerCamelCase__	=  TFCTRLLMHeadModel(config=__lowerCamelCase  )
     lowerCamelCase__	=  {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
     lowerCamelCase__	=  model(__lowerCamelCase  )
     self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.seq_length, self.vocab_size)  )

    def  a__					(       self  :  Optional[Any]					,  __lowerCamelCase  :  Union[str, Any]					,  __lowerCamelCase  :  Optional[Any]					,  __lowerCamelCase  :  Tuple					,  __lowerCamelCase  :  List[str]					,  __lowerCamelCase  :  Tuple					,  *__lowerCamelCase  :  Optional[int]  )				->				Tuple:

     '''simple docstring'''




     lowerCamelCase__	=  self.num_labels
     lowerCamelCase__	=  ids_tensor([self.batch_size]					,  self.type_sequence_label_size  )
     lowerCamelCase__	=  {
         "input_ids": input_ids,
         "token_type_ids": token_type_ids,
         "labels": sequence_labels,
     }
     lowerCamelCase__	=  TFCTRLForSequenceClassification(__lowerCamelCase  )
     lowerCamelCase__	=  model(__lowerCamelCase  )
     self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.num_labels)  )




    def  a__					(       self  :  Union[str, Any]  )				->				Union[str, Any]:

     '''simple docstring'''




     lowerCamelCase__	=  self.prepare_config_and_inputs()

     (
         (
         lowerCamelCase__
     )	,						(
         lowerCamelCase__
     )	,						(
         lowerCamelCase__
     )	,						(
         lowerCamelCase__
     )	,						(
         lowerCamelCase__
     )	,						(
         lowerCamelCase__
     )	,						(
         lowerCamelCase__
     )	,						(
         lowerCamelCase__
     )	,						(
         lowerCamelCase__
     )	,						
     )	=  config_and_inputs

     lowerCamelCase__	=  {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
     return config, inputs_dict
@require_tf
class 					lowercase							(				_lowerCamelCase			,							_lowerCamelCase			,							unittest.TestCase   ):


    '''simple docstring'''

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

    def  a__					(       self  :  Optional[Any]					,  __lowerCamelCase  :  Any					,  __lowerCamelCase  :  int					,  __lowerCamelCase  :  Any					,  __lowerCamelCase  :  List[str]					,  __lowerCamelCase  :  Tuple  )				->				Tuple:

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

     '''simple docstring'''




     lowerCamelCase__	=  TFCTRLModelTester(self  )
     lowerCamelCase__	=  ConfigTester(self					,  config_class=__lowerCamelCase					,  n_embd=37  )

    def  a__					(       self  :  Optional[Any]  )				->				Any:

     '''simple docstring'''




     self.config_tester.run_common_tests()

    def  a__					(       self  :  List[Any]  )				->				Optional[Any]:

     '''simple docstring'''




     lowerCamelCase__	=  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_ctrl_model(*__lowerCamelCase  )

    def  a__					(       self  :  List[str]  )				->				List[Any]:

     '''simple docstring'''




     lowerCamelCase__	=  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_ctrl_lm_head(*__lowerCamelCase  )

    def  a__					(       self  :  str  )				->				int:

     '''simple docstring'''




     lowerCamelCase__	=  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_ctrl_for_sequence_classification(*__lowerCamelCase  )

    def  a__					(       self  :  Any  )				->				Any:

     '''simple docstring'''




     lowerCamelCase__	,						lowerCamelCase__	=  self.model_tester.prepare_config_and_inputs_for_common()
     lowerCamelCase__	=  [TFCTRLLMHeadModel]
     lowerCamelCase__	=  [TFCTRLForSequenceClassification]

     for model_class in self.all_model_classes:
      lowerCamelCase__	=  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:
       lowerCamelCase__	=  model.get_output_embeddings()
       assert isinstance(__lowerCamelCase					,  tf.keras.layers.Layer  )
       lowerCamelCase__	=  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:
       lowerCamelCase__	=  model.get_output_embeddings()
       assert isinstance(__lowerCamelCase					,  tf.keras.layers.Layer  )
       lowerCamelCase__	=  model.get_bias()
       assert name is None
      else:
       lowerCamelCase__	=  model.get_output_embeddings()
       assert x is None
       lowerCamelCase__	=  model.get_bias()
       assert name is None




    @slow
    def  a__					(       self  :  Optional[Any]  )				->				Tuple:

     '''simple docstring'''




     for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      lowerCamelCase__	=  TFCTRLModel.from_pretrained(__lowerCamelCase  )
      self.assertIsNotNone(__lowerCamelCase  )
@require_tf
class 					lowercase							(				unittest.TestCase   ):


    '''simple docstring'''

    @slow
    def  a__					(       self  :  Optional[int]  )				->				Union[str, Any]:

     '''simple docstring'''




     lowerCamelCase__	=  TFCTRLLMHeadModel.from_pretrained("ctrl"  )
     lowerCamelCase__	=  tf.convert_to_tensor([[11859, 0, 1611, 8]]					,  dtype=tf.intaa  )  # Legal the president is
     lowerCamelCase__	=  [
         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

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









'''simple docstring'''





from math import cos, sin






def  lowerCamelCase_  (     lowercase__):
 lowerCamelCase__	=  float(lowercase__)
 return [[scaling_factor * int(x == y) for x in range(2)] for y in range(2)]






def  lowerCamelCase_  (     lowercase__):
 lowerCamelCase__	,						lowerCamelCase__	=  cos(lowercase__), sin(lowercase__)
 return [[c, -s], [s, c]]






def  lowerCamelCase_  (     lowercase__):
 lowerCamelCase__	,						lowerCamelCase__	=  cos(lowercase__), sin(lowercase__)
 lowerCamelCase__	=  c * s
 return [[c * c, cs], [cs, s * s]]






def  lowerCamelCase_  (     lowercase__):
 lowerCamelCase__	,						lowerCamelCase__	=  cos(lowercase__), sin(lowercase__)
 lowerCamelCase__	=  c * s
 return [[2 * c - 1, 2 * cs], [2 * cs, 2 * s - 1]]


print(F"""    {scaling(5) = }""")
print(F"""  {rotation(45) = }""")
print(F"""{projection(45) = }""")
print(F"""{reflection(45) = }""")

