

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   __lowercase      :
 def __init__(							self      ,							A_      ,							A_=14      ,							A_=7      ,							A_=True      ,							A_=True      ,							A_=True      ,							A_=True      ,							A_=True      ,							A_=99      ,							A_=32      ,							A_=5      ,							A_=4      ,							A_=37      ,							A_="gelu"      ,							A_=0.1      ,							A_=0.1      ,							A_=512      ,							A_=16      ,							A_=2      ,							A_=0.02      ,							A_=3      ,							A_=4      ,							A_=None      ,							)					->Any:



       '''simple docstring'''





       __lowerCAmelCase					:						Optional[int]					=					parent
       __lowerCAmelCase					:						Union[str, Any]					=					batch_size
       __lowerCAmelCase					:						Dict					=					seq_length
       __lowerCAmelCase					:						str					=					is_training
       __lowerCAmelCase					:						List[str]					=					use_token_type_ids
       __lowerCAmelCase					:						Optional[Any]					=					use_input_mask
       __lowerCAmelCase					:						Union[str, Any]					=					use_labels
       __lowerCAmelCase					:						List[Any]					=					use_mc_token_ids
       __lowerCAmelCase					:						Tuple					=					vocab_size
       __lowerCAmelCase					:						str					=					hidden_size
       __lowerCAmelCase					:						str					=					num_hidden_layers
       __lowerCAmelCase					:						int					=					num_attention_heads
       __lowerCAmelCase					:						Union[str, Any]					=					intermediate_size
       __lowerCAmelCase					:						str					=					hidden_act
       __lowerCAmelCase					:						Union[str, Any]					=					hidden_dropout_prob
       __lowerCAmelCase					:						Tuple					=					attention_probs_dropout_prob
       __lowerCAmelCase					:						Optional[Any]					=					max_position_embeddings
       __lowerCAmelCase					:						Union[str, Any]					=					type_vocab_size
       __lowerCAmelCase					:						List[str]					=					type_sequence_label_size
       __lowerCAmelCase					:						str					=					initializer_range
       __lowerCAmelCase					:						Any					=					num_labels
       __lowerCAmelCase					:						Union[str, Any]					=					num_choices
       __lowerCAmelCase					:						Union[str, Any]					=					scope
       __lowerCAmelCase					:						Optional[int]					=					self.vocab_size - 1
 def 	UpperCamelCase__  (							self					)					->int:



       '''simple docstring'''





       __lowerCAmelCase					:						List[str]					=					ids_tensor([self.batch_size, self.seq_length]      ,							self.vocab_size					)

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

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

       __lowerCAmelCase					:						Optional[Any]					=					None
       if self.use_mc_token_ids:
             __lowerCAmelCase					:						int					=					ids_tensor([self.batch_size, self.num_choices]      ,							self.seq_length					)

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

       __lowerCAmelCase					:						List[str]					=					self.get_config()

       __lowerCAmelCase					:						int					=					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:



       '''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 	UpperCamelCase__  (							self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							*A_					)					->Optional[Any]:



       '''simple docstring'''





       __lowerCAmelCase					:						List[str]					=					CTRLModel(config=A_					)
       model.to(A_					)
       model.eval()

       model(A_      ,							token_type_ids=A_      ,							head_mask=A_					)
       model(A_      ,							token_type_ids=A_					)
       __lowerCAmelCase					:						str					=					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 	UpperCamelCase__  (							self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							*A_					)					->Dict:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					CTRLLMHeadModel(A_					)
       model.to(A_					)
       model.eval()

       __lowerCAmelCase					:						Any					=					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 	UpperCamelCase__  (							self					)					->int:



       '''simple docstring'''





       __lowerCAmelCase					:						List[Any]					=					self.prepare_config_and_inputs()

       (
           (
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							
       )					:						Tuple					=					config_and_inputs

       __lowerCAmelCase					:						Optional[Any]					=					{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask}

       return config, inputs_dict





 def 	UpperCamelCase__  (							self      ,							A_      ,							A_      ,							A_      ,							A_      ,							*A_					)					->str:



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					self.num_labels
       __lowerCAmelCase					:						str					=					CTRLForSequenceClassification(A_					)
       model.to(A_					)
       model.eval()
       __lowerCAmelCase					:						Optional[Any]					=					ids_tensor([self.batch_size]      ,							self.type_sequence_label_size					)
       __lowerCAmelCase					:						Dict					=					model(A_      ,							token_type_ids=A_      ,							labels=A_					)
       self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.num_labels)					)







@require_torch
class   __lowercase      (_UpperCAmelCase		,   _UpperCAmelCase		,   _UpperCAmelCase		,   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 	UpperCamelCase__  (							self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_					)					->Union[str, Any]:



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



       '''simple docstring'''





       __lowerCAmelCase					:						int					=					CTRLModelTester(self					)
       __lowerCAmelCase					:						str					=					ConfigTester(self      ,							config_class=A_      ,							n_embd=37					)
 def 	UpperCamelCase__  (							self					)					->Tuple:



       '''simple docstring'''





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



       '''simple docstring'''





       self.config_tester.run_common_tests()
 def 	UpperCamelCase__  (							self					)					->Tuple:



       '''simple docstring'''





       __lowerCAmelCase					:						Dict					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_ctrl_model(*A_					)
 def 	UpperCamelCase__  (							self					)					->int:



       '''simple docstring'''





       __lowerCAmelCase					:						Dict					=					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 	UpperCamelCase__  (							self					)					->Any:



       '''simple docstring'''





       pass
 @slow
 def 	UpperCamelCase__  (							self					)					->Dict:



       '''simple docstring'''





       for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             __lowerCAmelCase					:						Any					=					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 	UpperCamelCase__  (							self					)					->str:



       '''simple docstring'''





       pass







@require_torch
class   __lowercase      (unittest.TestCase ):
 def 	UpperCamelCase__  (							self					)					->Any:



       '''simple docstring'''





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





 @slow
 def 	UpperCamelCase__  (							self					)					->str:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					CTRLLMHeadModel.from_pretrained('''ctrl'''					)
       model.to(A_					)
       __lowerCAmelCase					:						Dict					=					torch.tensor(
           [[1_1859, 0, 1611, 8]]      ,							dtype=torch.long      ,							device=A_					)  # Legal the president is
       __lowerCAmelCase					:						List[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

       __lowerCAmelCase					:						Union[str, Any]					=					model.generate(A_      ,							do_sample=A_					)
       self.assertListEqual(output_ids[0].tolist()      ,							A_					)





def 		_lowercase (     lowercase__						):
      if not all(x.isalpha() for x in string						):
            raise ValueError('''String must only contain alphabetic characters.'''						)

      __lowerCAmelCase					:						int					=					sorted(string.lower()						)
      return len(lowercase__						) == len(set(lowercase__						)						)


if __name__ == "__main__":
 _UpperCamelCase 			=		input("Enter a string ").strip()

 _UpperCamelCase 			=		is_isogram(input_str)
 print(F"{input_str} is {'an' if isogram else 'not an'} isogram.")


