





"""simple docstring"""



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   :





 """simple docstring"""


 def __init__(       self   ,		__lowerCamelCase   ,		__lowerCamelCase=14   ,		__lowerCamelCase=7   ,		__lowerCamelCase=True   ,		__lowerCamelCase=True   ,		__lowerCamelCase=True   ,		__lowerCamelCase=True   ,		__lowerCamelCase=True   ,		__lowerCamelCase=99   ,		__lowerCamelCase=32   ,		__lowerCamelCase=5   ,		__lowerCamelCase=4   ,		__lowerCamelCase=37   ,		__lowerCamelCase="gelu"   ,		__lowerCamelCase=0.1   ,		__lowerCamelCase=0.1   ,		__lowerCamelCase=512   ,		__lowerCamelCase=16   ,		__lowerCamelCase=2   ,		__lowerCamelCase=0.0_2   ,		__lowerCamelCase=3   ,		__lowerCamelCase=4   ,		__lowerCamelCase=None   ,		):



     '''simple docstring'''




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

 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     __A       :		Optional[int]							=	ids_tensor([self.batch_size, self.seq_length]   ,		self.vocab_size    )

     __A       :		Tuple							=	None
     if self.use_input_mask:
         __A       :		List[str]							=	random_attention_mask([self.batch_size, self.seq_length]    )

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

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

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

     __A       :		Optional[Any]							=	self.get_config()

     __A       :		Union[str, 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    UpperCamelCase__(       self    ):



     '''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   ,		__lowerCamelCase   ,		__lowerCamelCase   ,		__lowerCamelCase   ,		__lowerCamelCase   ,		__lowerCamelCase   ,		*__lowerCamelCase    ):



     '''simple docstring'''




     __A       :		Optional[int]							=	CTRLModel(config=__lowerCamelCase    )
     model.to(__lowerCamelCase    )
     model.eval()

     model(__lowerCamelCase   ,		token_type_ids=__lowerCamelCase   ,		head_mask=__lowerCamelCase    )
     model(__lowerCamelCase   ,		token_type_ids=__lowerCamelCase    )
     __A       :		str							=	model(__lowerCamelCase    )
     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   ,		__lowerCamelCase   ,		__lowerCamelCase   ,		__lowerCamelCase   ,		__lowerCamelCase   ,		__lowerCamelCase   ,		*__lowerCamelCase    ):



     '''simple docstring'''




     __A       :		Optional[int]							=	CTRLLMHeadModel(__lowerCamelCase    )
     model.to(__lowerCamelCase    )
     model.eval()

     __A       :		Union[str, Any]							=	model(__lowerCamelCase   ,		token_type_ids=__lowerCamelCase   ,		labels=__lowerCamelCase    )
     self.parent.assertEqual(result.loss.shape   ,		()    )
     self.parent.assertEqual(result.logits.shape   ,		(self.batch_size, self.seq_length, self.vocab_size)    )

 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     __A       :		List[str]							=	self.prepare_config_and_inputs()

     (
         (
         __A
     )					,			(
         __A
     )					,			(
         __A
     )					,			(
         __A
     )					,			(
         __A
     )					,			(
         __A
     )					,			(
         __A
     )					,			(
         __A
     )					,			(
         __A
     )					,			
     )       :		str							=	config_and_inputs

     __A       :		Tuple							=	{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask}

     return config, inputs_dict







 def    UpperCamelCase__(       self   ,		__lowerCamelCase   ,		__lowerCamelCase   ,		__lowerCamelCase   ,		__lowerCamelCase   ,		*__lowerCamelCase    ):



     '''simple docstring'''




     __A       :		Optional[Any]							=	self.num_labels
     __A       :		Union[str, Any]							=	CTRLForSequenceClassification(__lowerCamelCase    )
     model.to(__lowerCamelCase    )
     model.eval()
     __A       :		int							=	ids_tensor([self.batch_size]   ,		self.type_sequence_label_size    )
     __A       :		Optional[int]							=	model(__lowerCamelCase   ,		token_type_ids=__lowerCamelCase   ,		labels=__lowerCamelCase    )
     self.parent.assertEqual(result.logits.shape   ,		(self.batch_size, self.num_labels)    )






@require_torch
class 						__snake_case   (						SCREAMING_SNAKE_CASE__				,				SCREAMING_SNAKE_CASE__				,				SCREAMING_SNAKE_CASE__				,				unittest.TestCase	):





 """simple docstring"""


 _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    UpperCamelCase__(       self   ,		__lowerCamelCase   ,		__lowerCamelCase   ,		__lowerCamelCase   ,		__lowerCamelCase   ,		__lowerCamelCase    ):



     '''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    ):



     '''simple docstring'''




     __A       :		Tuple							=	CTRLModelTester(self    )
     __A       :		Optional[Any]							=	ConfigTester(self   ,		config_class=__lowerCamelCase   ,		n_embd=37    )

 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




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

 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     self.config_tester.run_common_tests()

 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     __A       :		List[str]							=	self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_ctrl_model(*__lowerCamelCase    )

 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     __A       :		str							=	self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_lm_head_model(*__lowerCamelCase    )

 @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''    )
 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     pass

 @slow
 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         __A       :		int							=	CTRLModel.from_pretrained(__lowerCamelCase    )
         self.assertIsNotNone(__lowerCamelCase    )







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



     '''simple docstring'''




     pass






@require_torch
class 						__snake_case   (						unittest.TestCase	):





 """simple docstring"""


 def    UpperCamelCase__(       self    ):



     '''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    ):



     '''simple docstring'''




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

     __A       :		Optional[Any]							=	model.generate(__lowerCamelCase   ,		do_sample=__lowerCamelCase    )
     self.assertListEqual(output_ids[0].tolist()   ,		__lowerCamelCase    )











"""simple docstring"""



from collections import defaultdict
from math import gcd







def 						__lowercase	(  snake_case_    :    int = 1500000		)  ->int:



    '''simple docstring'''







    __A       :		defaultdict							=	defaultdict(snake_case_		)
    __A       :		Any							=	2
    while 2 * euclid_m * (euclid_m + 1) <= limit:
        for euclid_n in range((euclid_m % 2) + 1							,snake_case_							,2		):
            if gcd(snake_case_							,snake_case_		) > 1:
                continue
            __A       :		List[Any]							=	2 * euclid_m * (euclid_m + euclid_n)
            for perimeter in range(snake_case_							,limit + 1							,snake_case_		):
                frequencies[perimeter] += 1
        euclid_m += 1
    return sum(1 for frequency in frequencies.values() if frequency == 1		)


if __name__ == "__main__":
       print(f'''{solution() = }''')




