

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  __UpperCAmelCase					:



   def __init__(							self							:					Optional[Any],		__A							:					Any,		__A							:					Any=1_4,		__A							:					Union[str, Any]=7,		__A							:					List[str]=True,		__A							:					Optional[int]=True,		__A							:					Dict=True,		__A							:					str=True,		__A							:					str=True,		__A							:					Tuple=9_9,		__A							:					Any=3_2,		__A							:					Tuple=5,		__A							:					Optional[Any]=4,		__A							:					int=3_7,		__A							:					Optional[int]="gelu",		__A							:					List[str]=0.1,		__A							:					Optional[int]=0.1,		__A							:					Any=5_1_2,		__A							:					List[str]=1_6,		__A							:					Any=2,		__A							:					Any=0.0_2,		__A							:					Any=3,		__A							:					str=4,		__A							:					List[Any]=None,		):
     UpperCAmelCase	:							Tuple						=			parent
     UpperCAmelCase	:							Dict						=			batch_size
     UpperCAmelCase	:							str						=			seq_length
     UpperCAmelCase	:							Dict						=			is_training
     UpperCAmelCase	:							Dict						=			use_token_type_ids
     UpperCAmelCase	:							Union[str, Any]						=			use_input_mask
     UpperCAmelCase	:							List[Any]						=			use_labels
     UpperCAmelCase	:							List[str]						=			use_mc_token_ids
     UpperCAmelCase	:							Any						=			vocab_size
     UpperCAmelCase	:							List[Any]						=			hidden_size
     UpperCAmelCase	:							str						=			num_hidden_layers
     UpperCAmelCase	:							Any						=			num_attention_heads
     UpperCAmelCase	:							List[str]						=			intermediate_size
     UpperCAmelCase	:							List[str]						=			hidden_act
     UpperCAmelCase	:							Optional[Any]						=			hidden_dropout_prob
     UpperCAmelCase	:							int						=			attention_probs_dropout_prob
     UpperCAmelCase	:							Dict						=			max_position_embeddings
     UpperCAmelCase	:							Optional[Any]						=			type_vocab_size
     UpperCAmelCase	:							List[str]						=			type_sequence_label_size
     UpperCAmelCase	:							List[str]						=			initializer_range
     UpperCAmelCase	:							Dict						=			num_labels
     UpperCAmelCase	:							str						=			num_choices
     UpperCAmelCase	:							Any						=			scope
     UpperCAmelCase	:							str						=			self.vocab_size - 1



   def 	__magic_name__   (							self							:					int						):
     UpperCAmelCase	:							Optional[Any]						=			ids_tensor([self.batch_size, self.seq_length],		self.vocab_size						)

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

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

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

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

     UpperCAmelCase	:							List[Any]						=			self.get_config()

     UpperCAmelCase	:							Optional[Any]						=			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 	__magic_name__   (							self							:					List[str]						):
     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 	__magic_name__   (							self							:					Any,		__A							:					str,		__A							:					int,		__A							:					int,		__A							:					Optional[int],		__A							:					Any,		*__A							:					str						):
     UpperCAmelCase	:							Optional[int]						=			CTRLModel(config=__A						)
     model.to(__A						)
     model.eval()

     model(__A,		token_type_ids=__A,		head_mask=__A						)
     model(__A,		token_type_ids=__A						)
     UpperCAmelCase	:							Tuple						=			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 	__magic_name__   (							self							:					str,		__A							:					Dict,		__A							:					str,		__A							:					List[str],		__A							:					Optional[int],		__A							:					Union[str, Any],		*__A							:					Union[str, Any]						):
     UpperCAmelCase	:							List[str]						=			CTRLLMHeadModel(__A						)
     model.to(__A						)
     model.eval()

     UpperCAmelCase	:							str						=			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 	__magic_name__   (							self							:					Any						):
     UpperCAmelCase	:							List[str]						=			self.prepare_config_and_inputs()

     (
         UpperCAmelCase						
     )	:							Dict						=			config_and_inputs

     UpperCAmelCase	:							Any						=			{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask}

     return config, inputs_dict





   def 	__magic_name__   (							self							:					Union[str, Any],		__A							:					Tuple,		__A							:					List[str],		__A							:					List[str],		__A							:					Dict,		*__A							:					Optional[int]						):
     UpperCAmelCase	:							List[str]						=			self.num_labels
     UpperCAmelCase	:							int						=			CTRLForSequenceClassification(__A						)
     model.to(__A						)
     model.eval()
     UpperCAmelCase	:							List[Any]						=			ids_tensor([self.batch_size],		self.type_sequence_label_size						)
     UpperCAmelCase	:							List[str]						=			model(__A,		token_type_ids=__A,		labels=__A						)
     self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.num_labels)						)






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



   def 	__magic_name__   (							self							:					Tuple,		__A							:					List[Any],		__A							:					Optional[int],		__A							:					Optional[int],		__A							:					List[Any],		__A							:					Optional[Any]						):
     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 	__magic_name__   (							self							:					Optional[int]						):
     UpperCAmelCase	:							Union[str, Any]						=			CTRLModelTester(self						)
     UpperCAmelCase	:							Union[str, Any]						=			ConfigTester(self,		config_class=__A,		n_embd=3_7						)



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



   def 	__magic_name__   (							self							:					Dict						):
     self.config_tester.run_common_tests()



   def 	__magic_name__   (							self							:					Any						):
     UpperCAmelCase	:							str						=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_ctrl_model(*__A						)



   def 	__magic_name__   (							self							:					Dict						):
     UpperCAmelCase	:							Tuple						=			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 	__magic_name__   (							self							:					Union[str, Any]						):
     pass



   @slow
   def 	__magic_name__   (							self							:					Union[str, Any]						):
     for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase	:							Optional[int]						=			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 	__magic_name__   (							self							:					List[Any]						):
     pass






@require_torch
class  __UpperCAmelCase					(	unittest.TestCase						):



   def 	__magic_name__   (							self							:					Union[str, Any]						):
     super().tearDown()
     # clean-up as much as possible GPU memory occupied by PyTorch
     gc.collect()
     torch.cuda.empty_cache()





   @slow
   def 	__magic_name__   (							self							:					Tuple						):
     UpperCAmelCase	:							Optional[Any]						=			CTRLLMHeadModel.from_pretrained('''ctrl'''						)
     model.to(__A						)
     UpperCAmelCase	:							Union[str, Any]						=			torch.tensor(
         [[1_1_8_5_9, 0, 1_6_1_1, 8]],		dtype=torch.long,		device=__A						)  # Legal the president is
     UpperCAmelCase	:							Union[str, Any]						=			[
         1_1_8_5_9,
         0,
         1_6_1_1,
         8,
         5,
         1_5_0,
         2_6_4_4_9,
         2,
         1_9,
         3_4_8,
         4_6_9,
         3,
         2_5_9_5,
         4_8,
         2_0_7_4_0,
         2_4_6_5_3_3,
         2_4_6_5_3_3,
         1_9,
         3_0,
         5,
     ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

     UpperCAmelCase	:							str						=			model.generate(__A,		do_sample=__A						)
     self.assertListEqual(output_ids[0].tolist(),		__A						)








def       a__					(		UpperCAmelCase							:      int    ,			UpperCAmelCase							:      int      )  ->						str:
  if a < 0 or b < 0:
    raise ValueError('''the value of both inputs must be positive'''      )
  UpperCAmelCase	:							List[Any]						=			str(bin(UpperCAmelCase      )      )[2:]  # remove the leading "0b"
  UpperCAmelCase	:							List[str]						=			str(bin(UpperCAmelCase      )      )[2:]
  UpperCAmelCase	:							Optional[Any]						=			max(len(UpperCAmelCase      )    ,			len(UpperCAmelCase      )      )
  return "0b" + "".join(
      str(int('''1''' in (char_a, char_b)      )      )
      for char_a, char_b in zip(a_binary.zfill(UpperCAmelCase      )    ,			b_binary.zfill(UpperCAmelCase      )      )      )


if __name__ == "__main__":
     import doctest

     doctest.testmod()





