

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






     '''simple docstring'''



     def __init__(						self					:	str	,     lowercase_					:	int	,     )		->      int:
      UpperCAmelCase			:      Any		   =					parent
      UpperCAmelCase			:      int		   =					13
      UpperCAmelCase			:      Any		   =					7
      UpperCAmelCase			:      int		   =					True
      UpperCAmelCase			:      Optional[Any]		   =					True
      UpperCAmelCase			:      Any		   =					True
      UpperCAmelCase			:      str		   =					True
      UpperCAmelCase			:      Dict		   =					True
      UpperCAmelCase			:      Any		   =					99
      UpperCAmelCase			:      Tuple		   =					32
      UpperCAmelCase			:      Dict		   =					2
      UpperCAmelCase			:      Union[str, Any]		   =					4
      UpperCAmelCase			:      Optional[Any]		   =					37
      UpperCAmelCase			:      List[Any]		   =					'gelu'
      UpperCAmelCase			:      List[str]		   =					0.1
      UpperCAmelCase			:      int		   =					0.1
      UpperCAmelCase			:      int		   =					512
      UpperCAmelCase			:      Dict		   =					16
      UpperCAmelCase			:      str		   =					2
      UpperCAmelCase			:      Any		   =					0.02
      UpperCAmelCase			:      int		   =					3
      UpperCAmelCase			:      Dict		   =					4
      UpperCAmelCase			:      Tuple		   =					None
      UpperCAmelCase			:      Optional[int]		   =					self.vocab_size - 1


     def   UpperCAmelCase_			(						self					:	Any			)		->      int:
      UpperCAmelCase			:      List[str]		   =					ids_tensor([self.batch_size, self.seq_length]	,     self.vocab_size			)

      UpperCAmelCase			:      Dict		   =					None
      if self.use_input_mask:
       UpperCAmelCase			:      int		   =					random_attention_mask([self.batch_size, self.seq_length]			)

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

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

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

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

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

      UpperCAmelCase			:      Optional[int]		   =					model(__lowerCamelCase			)

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


     def   UpperCAmelCase_			(						self					:	Union[str, Any]	,     lowercase_					:	Union[str, Any]	,     lowercase_					:	Optional[Any]	,     lowercase_					:	Optional[Any]	,     lowercase_					:	int	,     lowercase_					:	str	,     *lowercase_					:	List[str]			)		->      Optional[int]:
      UpperCAmelCase			:      str		   =					TFCTRLLMHeadModel(config=__lowerCamelCase			)
      UpperCAmelCase			:      Optional[Any]		   =					{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      UpperCAmelCase			:      List[Any]		   =					model(__lowerCamelCase			)
      self.parent.assertEqual(result.logits.shape	,     (self.batch_size, self.seq_length, self.vocab_size)			)


     def   UpperCAmelCase_			(						self					:	Optional[Any]	,     lowercase_					:	Union[str, Any]	,     lowercase_					:	Optional[Any]	,     lowercase_					:	Tuple	,     lowercase_					:	List[str]	,     lowercase_					:	Tuple	,     *lowercase_					:	Optional[int]			)		->      Tuple:
      UpperCAmelCase			:      Optional[int]		   =					self.num_labels
      UpperCAmelCase			:      Optional[int]		   =					ids_tensor([self.batch_size]	,     self.type_sequence_label_size			)
      UpperCAmelCase			:      Optional[int]		   =					{
          'input_ids': input_ids,
          'token_type_ids': token_type_ids,
          'labels': sequence_labels,
      }
      UpperCAmelCase			:      List[str]		   =					TFCTRLForSequenceClassification(__lowerCamelCase			)
      UpperCAmelCase			:      int		   =					model(__lowerCamelCase			)
      self.parent.assertEqual(result.logits.shape	,     (self.batch_size, self.num_labels)			)




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

      (
          (
          UpperCAmelCase						
      )   ,					(
          UpperCAmelCase						
      )   ,					(
          UpperCAmelCase						
      )   ,					(
          UpperCAmelCase						
      )   ,					(
          UpperCAmelCase						
      )   ,					(
          UpperCAmelCase						
      )   ,					(
          UpperCAmelCase						
      )   ,					(
          UpperCAmelCase						
      )   ,					(
          UpperCAmelCase						
      )   ,											
      )			:      Any		   =					config_and_inputs

      UpperCAmelCase			:      int		   =					{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
      return config, inputs_dict




@require_tf
class 			A_					(							_lowerCamelCase			,  _lowerCamelCase			,  unittest.TestCase				):






     '''simple docstring'''



     UpperCAmelCase_		:      Optional[Any]    						=    (TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
     UpperCAmelCase_		:      str    						=    (TFCTRLLMHeadModel,) if is_tf_available() else ()
     UpperCAmelCase_		:      Dict    						=    (
         {
             """feature-extraction""": TFCTRLModel,
             """text-classification""": TFCTRLForSequenceClassification,
             """text-generation""": TFCTRLLMHeadModel,
             """zero-shot""": TFCTRLForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     UpperCAmelCase_		:      Union[str, Any]    						=    False
     UpperCAmelCase_		:      Dict    						=    False


     def   UpperCAmelCase_			(						self					:	Optional[Any]	,     lowercase_					:	Any	,     lowercase_					:	int	,     lowercase_					:	Any	,     lowercase_					:	List[str]	,     lowercase_					:	Tuple			)		->      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					:	int			)		->      int:
      UpperCAmelCase			:      Tuple		   =					TFCTRLModelTester(self			)
      UpperCAmelCase			:      Dict		   =					ConfigTester(self	,     config_class=__lowerCamelCase	,     n_embd=37			)


     def   UpperCAmelCase_			(						self					:	Optional[Any]			)		->      Any:
      self.config_tester.run_common_tests()


     def   UpperCAmelCase_			(						self					:	List[Any]			)		->      Optional[Any]:
      UpperCAmelCase			:      Union[str, Any]		   =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_ctrl_model(*__lowerCamelCase			)


     def   UpperCAmelCase_			(						self					:	List[str]			)		->      List[Any]:
      UpperCAmelCase			:      int		   =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_ctrl_lm_head(*__lowerCamelCase			)


     def   UpperCAmelCase_			(						self					:	str			)		->      int:
      UpperCAmelCase			:      str		   =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_ctrl_for_sequence_classification(*__lowerCamelCase			)


     def   UpperCAmelCase_			(						self					:	Any			)		->      Any:
      UpperCAmelCase   ,					UpperCAmelCase			:      List[Any]		   =					self.model_tester.prepare_config_and_inputs_for_common()
      UpperCAmelCase			:      Any		   =					[TFCTRLLMHeadModel]
      UpperCAmelCase			:      Optional[int]		   =					[TFCTRLForSequenceClassification]

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




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




      for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase			:      Optional[Any]		   =					TFCTRLModel.from_pretrained(__lowerCamelCase			)
       self.assertIsNotNone(__lowerCamelCase			)




@require_tf
class 			A_					(							unittest.TestCase				):






     '''simple docstring'''



     @slow
     def   UpperCAmelCase_			(						self					:	Optional[int]			)		->      Union[str, Any]:
      UpperCAmelCase			:      List[str]		   =					TFCTRLLMHeadModel.from_pretrained('ctrl'			)
      UpperCAmelCase			:      List[Any]		   =					tf.convert_to_tensor([[11_859, 0, 1_611, 8]]	,     dtype=tf.intaa			)  # Legal the president is
      UpperCAmelCase			:      int		   =					[
          11_859,
          0,
          1_611,
          8,
          5,
          150,
          26_449,
          2,
          19,
          348,
          469,
          3,
          2_595,
          48,
          20_740,
          246_533,
          246_533,
          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			:      Tuple		   =					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) = }""")

