


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







    """simple docstring"""

    def __init__(			self     ,   __snake_case     ,   __snake_case=1_4     ,   __snake_case=7     ,   __snake_case=True     ,   __snake_case=True     ,   __snake_case=True     ,   __snake_case=True     ,   __snake_case=True     ,   __snake_case=9_9     ,   __snake_case=3_2     ,   __snake_case=5     ,   __snake_case=4     ,   __snake_case=3_7     ,   __snake_case="gelu"     ,   __snake_case=0.1     ,   __snake_case=0.1     ,   __snake_case=5_1_2     ,   __snake_case=1_6     ,   __snake_case=2     ,   __snake_case=0.02     ,   __snake_case=3     ,   __snake_case=4     ,   __snake_case=None     ,   ):
         snake_case											=							parent
         snake_case											=							batch_size
         snake_case											=							seq_length
         snake_case											=							is_training
         snake_case											=							use_token_type_ids
         snake_case											=							use_input_mask
         snake_case											=							use_labels
         snake_case											=							use_mc_token_ids
         snake_case											=							vocab_size
         snake_case											=							hidden_size
         snake_case											=							num_hidden_layers
         snake_case											=							num_attention_heads
         snake_case											=							intermediate_size
         snake_case											=							hidden_act
         snake_case											=							hidden_dropout_prob
         snake_case											=							attention_probs_dropout_prob
         snake_case											=							max_position_embeddings
         snake_case											=							type_vocab_size
         snake_case											=							type_sequence_label_size
         snake_case											=							initializer_range
         snake_case											=							num_labels
         snake_case											=							num_choices
         snake_case											=							scope
         snake_case											=							self.vocab_size - 1
    def 						a_     (			self       ):
         snake_case											=							ids_tensor([self.batch_size, self.seq_length]     ,   self.vocab_size       )

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

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

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

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

         snake_case											=							self.get_config()

         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       ):
         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     ,   __snake_case     ,   __snake_case     ,   __snake_case     ,   __snake_case     ,   __snake_case     ,   *__snake_case       ):
         snake_case											=							CTRLModel(config=__snake_case       )
         model.to(__snake_case       )
         model.eval()

         model(__snake_case     ,   token_type_ids=__snake_case     ,   head_mask=__snake_case       )
         model(__snake_case     ,   token_type_ids=__snake_case       )
         snake_case											=							model(__snake_case       )
         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     ,   __snake_case     ,   __snake_case     ,   __snake_case     ,   __snake_case     ,   __snake_case     ,   *__snake_case       ):
         snake_case											=							CTRLLMHeadModel(__snake_case       )
         model.to(__snake_case       )
         model.eval()

         snake_case											=							model(__snake_case     ,   token_type_ids=__snake_case     ,   labels=__snake_case       )
         self.parent.assertEqual(result.loss.shape     ,   ()       )
         self.parent.assertEqual(result.logits.shape     ,   (self.batch_size, self.seq_length, self.vocab_size)       )
    def 						a_     (			self       ):
         snake_case											=							self.prepare_config_and_inputs()

         (
             (
             snake_case
         )					,    (
             snake_case
         )					,    (
             snake_case
         )					,    (
             snake_case
         )					,    (
             snake_case
         )					,    (
             snake_case
         )					,    (
             snake_case
         )					,    (
             snake_case
         )					,    (
             snake_case
         )					,    
         )											=							config_and_inputs

         snake_case											=							{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask}

         return config, inputs_dict




    def 						a_     (			self     ,   __snake_case     ,   __snake_case     ,   __snake_case     ,   __snake_case     ,   *__snake_case       ):
         snake_case											=							self.num_labels
         snake_case											=							CTRLForSequenceClassification(__snake_case       )
         model.to(__snake_case       )
         model.eval()
         snake_case											=							ids_tensor([self.batch_size]     ,   self.type_sequence_label_size       )
         snake_case											=							model(__snake_case     ,   token_type_ids=__snake_case     ,   labels=__snake_case       )
         self.parent.assertEqual(result.logits.shape     ,   (self.batch_size, self.num_labels)       )






@require_torch
class     A__   (       snake_case__			,   snake_case__			,   snake_case__			,   unittest.TestCase      ):







    """simple docstring"""

    __magic_name__         =						(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
    __magic_name__         =						(CTRLLMHeadModel,) if is_torch_available() else ()
    __magic_name__         =						(
        {
            'feature-extraction': CTRLModel,
            'text-classification': CTRLForSequenceClassification,
            'text-generation': CTRLLMHeadModel,
            'zero-shot': CTRLForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    __magic_name__         =						True
    __magic_name__         =						False
    __magic_name__         =						False
    def 						a_     (			self     ,   __snake_case     ,   __snake_case     ,   __snake_case     ,   __snake_case     ,   __snake_case       ):
         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       ):
         snake_case											=							CTRLModelTester(self       )
         snake_case											=							ConfigTester(self     ,   config_class=__snake_case     ,   n_embd=3_7       )
    def 						a_     (			self       ):
         super().tearDown()
         # clean-up as much as possible GPU memory occupied by PyTorch
         gc.collect()
         torch.cuda.empty_cache()
    def 						a_     (			self       ):
         self.config_tester.run_common_tests()
    def 						a_     (			self       ):
         snake_case											=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_ctrl_model(*__snake_case       )
    def 						a_     (			self       ):
         snake_case											=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_lm_head_model(*__snake_case       )
    @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''       )
    def 						a_     (			self       ):
         pass
    @slow
    def 						a_     (			self       ):
         for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              snake_case											=							CTRLModel.from_pretrained(__snake_case       )
              self.assertIsNotNone(__snake_case       )




    @unittest.skip('''The model doesn\'t support left padding'''       )  # and it's not used enough to be worth fixing :)
    def 						a_     (			self       ):
         pass






@require_torch
class     A__   (       unittest.TestCase      ):







    """simple docstring"""

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




    @slow
    def 						a_     (			self       ):
         snake_case											=							CTRLLMHeadModel.from_pretrained('''ctrl'''       )
         model.to(__snake_case       )
         snake_case											=							torch.tensor(
             [[1_1_8_5_9, 0, 1_6_1_1, 8]]     ,   dtype=torch.long     ,   device=__snake_case       )  # Legal the president is
         snake_case											=							[
             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

         snake_case											=							model.generate(__snake_case     ,   do_sample=__snake_case       )
         self.assertListEqual(output_ids[0].tolist()     ,   __snake_case       )








from functools import lru_cache
def      UpperCAmelCase__		(UpperCamelCase_							):



     """simple docstring"""






     snake_case											=							2
     snake_case											=							set()
     while i * i <= n:
          if n % i:
               i += 1
          else:
               n //= i
               factors.add(UpperCamelCase_							)
     if n > 1:
          factors.add(UpperCamelCase_							)
     return factors
@lru_cache
def      UpperCAmelCase__		(UpperCamelCase_							):



     """simple docstring"""






     return len(unique_prime_factors(UpperCamelCase_							)							)
def      UpperCAmelCase__		(UpperCamelCase_							):



     """simple docstring"""






     return len(set(UpperCamelCase_							)							) in (0, 1)
def      UpperCAmelCase__		(UpperCamelCase_							):



     """simple docstring"""






     snake_case											=							2

     while True:
          # Increment each value of a generated range
          snake_case											=							[base + i for i in range(UpperCamelCase_							)]

          # Run elements through out unique_prime_factors function
          # Append our target number to the end.
          snake_case											=							[upf_len(UpperCamelCase_							) for x in group]
          checker.append(UpperCamelCase_							)

          # If all numbers in the list are equal, return the group variable.
          if equality(UpperCamelCase_							):
               return group

          # Increment our base variable by 1
          base += 1
def      UpperCAmelCase__		(UpperCamelCase_ = 4							):



     """simple docstring"""






     snake_case											=							run(UpperCamelCase_							)
     return results[0] if len(UpperCamelCase_							) else None


if __name__ == "__main__":
 print(solution())




