import gc
import unittest

from transformers import CTRLConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import (
       CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
       CTRLForSequenceClassification,
       CTRLLMHeadModel,
       CTRLModel,
   )



class __snake_case    :




   def __init__(   self      :    Optional[int]	,     _lowercase      :    Union[str, Any]	,     _lowercase      :    str=14	,     _lowercase      :    Optional[Any]=7	,     _lowercase      :    int=True	,     _lowercase      :    Tuple=True	,     _lowercase      :    Union[str, Any]=True	,     _lowercase      :    int=True	,     _lowercase      :    Any=True	,     _lowercase      :    Optional[Any]=99	,     _lowercase      :    List[str]=32	,     _lowercase      :    Any=5	,     _lowercase      :    Union[str, Any]=4	,     _lowercase      :    str=37	,     _lowercase      :    str="gelu"	,     _lowercase      :    Tuple=0.1	,     _lowercase      :    Dict=0.1	,     _lowercase      :    str=5_12	,     _lowercase      :    int=16	,     _lowercase      :    Union[str, Any]=2	,     _lowercase      :    Optional[Any]=0.02	,     _lowercase      :    str=3	,     _lowercase      :    Union[str, Any]=4	,     _lowercase      :    Dict=None	,     ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				parent
     SCREAMING_SNAKE_CASE__      							=				batch_size
     SCREAMING_SNAKE_CASE__      							=				seq_length
     SCREAMING_SNAKE_CASE__      							=				is_training
     SCREAMING_SNAKE_CASE__      							=				use_token_type_ids
     SCREAMING_SNAKE_CASE__      							=				use_input_mask
     SCREAMING_SNAKE_CASE__      							=				use_labels
     SCREAMING_SNAKE_CASE__      							=				use_mc_token_ids
     SCREAMING_SNAKE_CASE__      							=				vocab_size
     SCREAMING_SNAKE_CASE__      							=				hidden_size
     SCREAMING_SNAKE_CASE__      							=				num_hidden_layers
     SCREAMING_SNAKE_CASE__      							=				num_attention_heads
     SCREAMING_SNAKE_CASE__      							=				intermediate_size
     SCREAMING_SNAKE_CASE__      							=				hidden_act
     SCREAMING_SNAKE_CASE__      							=				hidden_dropout_prob
     SCREAMING_SNAKE_CASE__      							=				attention_probs_dropout_prob
     SCREAMING_SNAKE_CASE__      							=				max_position_embeddings
     SCREAMING_SNAKE_CASE__      							=				type_vocab_size
     SCREAMING_SNAKE_CASE__      							=				type_sequence_label_size
     SCREAMING_SNAKE_CASE__      							=				initializer_range
     SCREAMING_SNAKE_CASE__      							=				num_labels
     SCREAMING_SNAKE_CASE__      							=				num_choices
     SCREAMING_SNAKE_CASE__      							=				scope
     SCREAMING_SNAKE_CASE__      							=				self.vocab_size - 1




   def      __a						(   self      :    List[Any]				):



     """simple docstring"""






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

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

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

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

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

     SCREAMING_SNAKE_CASE__      							=				self.get_config()

     SCREAMING_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      __a						(   self      :    List[Any]				):



     """simple docstring"""






     return 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	,     )




   def      __a						(   self      :    Optional[Any]	,     _lowercase      :    List[str]	,     _lowercase      :    List[Any]	,     _lowercase      :    str	,     _lowercase      :    str	,     _lowercase      :    Tuple	,     *_lowercase      :    Optional[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				CTRLModel(config=_a				)
     model.to(_a				)
     model.eval()

     model(_a	,     token_type_ids=_a	,     head_mask=_a				)
     model(_a	,     token_type_ids=_a				)
     SCREAMING_SNAKE_CASE__      							=				model(_a				)
     self.parent.assertEqual(result.last_hidden_state.shape	,     (self.batch_size, self.seq_length, self.hidden_size)				)
     self.parent.assertEqual(len(result.past_key_values				)	,     config.n_layer				)




   def      __a						(   self      :    str	,     _lowercase      :    Optional[int]	,     _lowercase      :    int	,     _lowercase      :    Union[str, Any]	,     _lowercase      :    List[str]	,     _lowercase      :    Dict	,     *_lowercase      :    Optional[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				CTRLLMHeadModel(_a				)
     model.to(_a				)
     model.eval()

     SCREAMING_SNAKE_CASE__      							=				model(_a	,     token_type_ids=_a	,     labels=_a				)
     self.parent.assertEqual(result.loss.shape	,     ()				)
     self.parent.assertEqual(result.logits.shape	,     (self.batch_size, self.seq_length, self.vocab_size)				)




   def      __a						(   self      :    Any				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				self.prepare_config_and_inputs()

     (
         SCREAMING_SNAKE_CASE__     
     )      							=				config_and_inputs

     SCREAMING_SNAKE_CASE__      							=				{"input_ids": input_ids, "token_type_ids": token_type_ids, "head_mask": head_mask}

     return config, inputs_dict




   def      __a						(   self      :    Union[str, Any]	,     _lowercase      :    Union[str, Any]	,     _lowercase      :    Optional[int]	,     _lowercase      :    Tuple	,     _lowercase      :    Union[str, Any]	,     *_lowercase      :    Dict				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				self.num_labels
     SCREAMING_SNAKE_CASE__      							=				CTRLForSequenceClassification(_a				)
     model.to(_a				)
     model.eval()
     SCREAMING_SNAKE_CASE__      							=				ids_tensor([self.batch_size]	,     self.type_sequence_label_size				)
     SCREAMING_SNAKE_CASE__      							=				model(_a	,     token_type_ids=_a	,     labels=_a				)
     self.parent.assertEqual(result.logits.shape	,     (self.batch_size, self.num_labels)				)





@require_torch
class __snake_case    (						_a       ,		_a       ,		_a       ,		unittest.TestCase		):
   lowerCAmelCase_														=							(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
   lowerCAmelCase_														=							(CTRLLMHeadModel,) if is_torch_available() else ()
   lowerCAmelCase_														=							(
       {
           "feature-extraction": CTRLModel,
           "text-classification": CTRLForSequenceClassification,
           "text-generation": CTRLLMHeadModel,
           "zero-shot": CTRLForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   lowerCAmelCase_														=							True
   lowerCAmelCase_														=							False
   lowerCAmelCase_														=							False




   def      __a						(   self      :    Dict	,     _lowercase      :    Dict	,     _lowercase      :    int	,     _lowercase      :    List[Any]	,     _lowercase      :    Union[str, Any]	,     _lowercase      :    Optional[int]				):



     """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      :    List[str]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				CTRLModelTester(self				)
     SCREAMING_SNAKE_CASE__      							=				ConfigTester(self	,     config_class=_a	,     n_embd=37				)




   def      __a						(   self      :    Union[str, Any]				):



     """simple docstring"""






     super().tearDown()
     # clean-up as much as possible GPU memory occupied by PyTorch
     gc.collect()
     torch.cuda.empty_cache()




   def      __a						(   self      :    Union[str, Any]				):



     """simple docstring"""






     self.config_tester.run_common_tests()




   def      __a						(   self      :    Union[str, Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_ctrl_model(*_a				)




   def      __a						(   self      :    str				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_lm_head_model(*_a				)




   @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""				)
   def      __a						(   self      :    Optional[int]				):



     """simple docstring"""






     pass




   @slow
   def      __a						(   self      :    int				):



     """simple docstring"""






     for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       SCREAMING_SNAKE_CASE__      							=				CTRLModel.from_pretrained(_a				)
       self.assertIsNotNone(_a				)




   @unittest.skip("""The model doesn't support left padding"""				)  # and it's not used enough to be worth fixing :)
   def      __a						(   self      :    Optional[int]				):



     """simple docstring"""






     pass





@require_torch
class __snake_case    (						unittest.TestCase		):




   def      __a						(   self      :    Dict				):



     """simple docstring"""






     super().tearDown()
     # clean-up as much as possible GPU memory occupied by PyTorch
     gc.collect()
     torch.cuda.empty_cache()




   @slow
   def      __a						(   self      :    Any				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				CTRLLMHeadModel.from_pretrained("""ctrl"""				)
     model.to(_a				)
     SCREAMING_SNAKE_CASE__      							=				torch.tensor(
         [[1_18_59, 0, 16_11, 8]]	,     dtype=torch.long	,     device=_a				)  # Legal the president is
     SCREAMING_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

     SCREAMING_SNAKE_CASE__      							=				model.generate(_a	,     do_sample=_a				)
     self.assertListEqual(output_ids[0].tolist()	,     _a				)

import unittest

from .lib import (
    Matrix,
    Vector,
    axpy,
    square_zero_matrix,
    unit_basis_vector,
    zero_vector,
)







class __snake_case    (						unittest.TestCase		):




   def      __a						(   self      :    Dict				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3]				)
     self.assertEqual(x.component(0				)	,     1				)
     self.assertEqual(x.component(2				)	,     3				)
     SCREAMING_SNAKE_CASE__      							=				Vector()




   def      __a						(   self      :    int				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([0, 0, 0, 0, 0, 1]				)
     self.assertEqual(str(_lowercase				)	,     """(0,0,0,0,0,1)"""				)




   def      __a						(   self      :    Tuple				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3, 4]				)
     self.assertEqual(len(_lowercase				)	,     4				)




   def      __a						(   self      :    Optional[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3, 4, 5]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([1, -1, 1, -1, 2, -3, 4, -5]				)
     self.assertAlmostEqual(x.euclidean_length()	,     2.2_36	,     3				)
     self.assertAlmostEqual(y.euclidean_length()	,     7.4_16	,     3				)
     self.assertEqual(z.euclidean_length()	,     0				)
     self.assertAlmostEqual(w.euclidean_length()	,     7.6_16	,     3				)




   def      __a						(   self      :    Optional[int]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([1, 1, 1]				)
     self.assertEqual((x + y).component(0				)	,     2				)
     self.assertEqual((x + y).component(1				)	,     3				)
     self.assertEqual((x + y).component(2				)	,     4				)




   def      __a						(   self      :    Any				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([1, 1, 1]				)
     self.assertEqual((x - y).component(0				)	,     0				)
     self.assertEqual((x - y).component(1				)	,     1				)
     self.assertEqual((x - y).component(2				)	,     2				)




   def      __a						(   self      :    str				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([2, -1, 4]				)  # for test of dot product
     SCREAMING_SNAKE_CASE__      							=				Vector([1, -2, -1]				)
     self.assertEqual(str(x * 3.0				)	,     """(3.0,6.0,9.0)"""				)
     self.assertEqual((a * b)	,     0				)




   def      __a						(   self      :    Union[str, Any]				):



     """simple docstring"""






     self.assertEqual(str(zero_vector(10				)				).count("""0"""				)	,     10				)




   def      __a						(   self      :    str				):



     """simple docstring"""






     self.assertEqual(str(unit_basis_vector(3	,     1				)				)	,     """(0,1,0)"""				)




   def      __a						(   self      :    Any				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([1, 0, 1]				)
     self.assertEqual(str(axpy(2	,     _lowercase	,     _lowercase				)				)	,     """(3,4,7)"""				)




   def      __a						(   self      :    str				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 0, 0, 0, 0, 0]				)
     SCREAMING_SNAKE_CASE__      							=				x.copy()
     self.assertEqual(str(_lowercase				)	,     str(_lowercase				)				)




   def      __a						(   self      :    str				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 0, 0]				)
     x.change_component(0	,     0				)
     x.change_component(1	,     1				)
     self.assertEqual(str(_lowercase				)	,     """(0,1,0)"""				)




   def      __a						(   self      :    Optional[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     self.assertEqual("""|1,2,3|\n|2,4,5|\n|6,7,8|\n"""	,     str(_lowercase				)				)




   def      __a						(   self      :    List[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     SCREAMING_SNAKE_CASE__      							=				[[-3, -14, -10], [-5, -10, -5], [-2, -1, 0]]
     for x in range(a.height()				):
       for y in range(a.width()				):
         self.assertEqual(minors[x][y]	,     a.minor(_lowercase	,     _lowercase				)				)




   def      __a						(   self      :    Dict				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     SCREAMING_SNAKE_CASE__      							=				[[-3, 14, -10], [5, -10, 5], [-2, 1, 0]]
     for x in range(a.height()				):
       for y in range(a.width()				):
         self.assertEqual(cofactors[x][y]	,     a.cofactor(_lowercase	,     _lowercase				)				)




   def      __a						(   self      :    Union[str, Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     self.assertEqual(-5	,     a.determinant()				)




   def      __a						(   self      :    List[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]	,     3	,     3				)
     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3]				)
     self.assertEqual("""(14,32,50)"""	,     str(a * x				)				)
     self.assertEqual("""|2,4,6|\n|8,10,12|\n|14,16,18|\n"""	,     str(a * 2				)				)




   def      __a						(   self      :    Optional[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     a.change_component(0	,     2	,     5				)
     self.assertEqual("""|1,2,5|\n|2,4,5|\n|6,7,8|\n"""	,     str(_lowercase				)				)




   def      __a						(   self      :    Union[str, Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     self.assertEqual(7	,     a.component(2	,     1				)	,     0.01				)




   def      __a						(   self      :    List[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]	,     3	,     3				)
     self.assertEqual("""|2,4,10|\n|4,8,10|\n|12,14,18|\n"""	,     str(a + b				)				)




   def      __a						(   self      :    Optional[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]	,     3	,     3				)
     self.assertEqual("""|0,0,-4|\n|0,0,0|\n|0,0,-2|\n"""	,     str(a - b				)				)




   def      __a						(   self      :    Any				):



     """simple docstring"""






     self.assertEqual(
         """|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n"""	,     str(square_zero_matrix(5				)				)	,     )


if __name__ == "__main__":
   unittest.main()
