



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





				'''simple docstring'''





				def __init__(							self  :  List[str]  ,       _lowerCAmelCase  :  Optional[int]  ,       _lowerCAmelCase  :  Dict=1_4  ,       _lowerCAmelCase  :  Any=7  ,       _lowerCAmelCase  :  Optional[int]=True  ,       _lowerCAmelCase  :  Optional[int]=True  ,       _lowerCAmelCase  :  Optional[Any]=True  ,       _lowerCAmelCase  :  str=True  ,       _lowerCAmelCase  :  Union[str, Any]=True  ,       _lowerCAmelCase  :  Optional[Any]=9_9  ,       _lowerCAmelCase  :  Optional[Any]=3_2  ,       _lowerCAmelCase  :  Tuple=5  ,       _lowerCAmelCase  :  str=4  ,       _lowerCAmelCase  :  str=3_7  ,       _lowerCAmelCase  :  int="gelu"  ,       _lowerCAmelCase  :  Tuple=0.1  ,       _lowerCAmelCase  :  Tuple=0.1  ,       _lowerCAmelCase  :  List[str]=5_1_2  ,       _lowerCAmelCase  :  List[str]=1_6  ,       _lowerCAmelCase  :  Optional[int]=2  ,       _lowerCAmelCase  :  List[Any]=0.02  ,       _lowerCAmelCase  :  List[Any]=3  ,       _lowerCAmelCase  :  Any=4  ,       _lowerCAmelCase  :  Optional[Any]=None  ,       ):







											'''simple docstring'''

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

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







											'''simple docstring'''

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

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

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

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

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

											__lowercase       	=self.get_config()

											__lowercase       	=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 				__lowerCamelCase     (							self  :  str):







											'''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 				__lowerCamelCase     (							self  :  List[str]  ,       _lowerCAmelCase  :  str  ,       _lowerCAmelCase  :  List[str]  ,       _lowerCAmelCase  :  Optional[Any]  ,       _lowerCAmelCase  :  List[Any]  ,       _lowerCAmelCase  :  str  ,       *_lowerCAmelCase  :  str):







											'''simple docstring'''

											__lowercase       	=CTRLModel(config=_lowerCAmelCase)
											model.to(_lowerCAmelCase)
											model.eval()

											model(_lowerCAmelCase  ,       token_type_ids=_lowerCAmelCase  ,       head_mask=_lowerCAmelCase)
											model(_lowerCAmelCase  ,       token_type_ids=_lowerCAmelCase)
											__lowercase       	=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 				__lowerCamelCase     (							self  :  Any  ,       _lowerCAmelCase  :  Optional[int]  ,       _lowerCAmelCase  :  List[Any]  ,       _lowerCAmelCase  :  Dict  ,       _lowerCAmelCase  :  Any  ,       _lowerCAmelCase  :  Optional[Any]  ,       *_lowerCAmelCase  :  List[Any]):







											'''simple docstring'''

											__lowercase       	=CTRLLMHeadModel(_lowerCAmelCase)
											model.to(_lowerCAmelCase)
											model.eval()

											__lowercase       	=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 				__lowerCamelCase     (							self  :  int):







											'''simple docstring'''

											__lowercase       	=self.prepare_config_and_inputs()

											(
											    (
											    __lowercase
											)	,				(
											    __lowercase
											)	,				(
											    __lowercase
											)	,				(
											    __lowercase
											)	,				(
											    __lowercase
											)	,				(
											    __lowercase
											)	,				(
											    __lowercase
											)	,				(
											    __lowercase
											)	,				(
											    __lowercase
											)	,				
											)       	=config_and_inputs

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

											return config, inputs_dict

				def 				__lowerCamelCase     (							self  :  List[str]  ,       _lowerCAmelCase  :  Optional[int]  ,       _lowerCAmelCase  :  Optional[int]  ,       _lowerCAmelCase  :  str  ,       _lowerCAmelCase  :  Dict  ,       *_lowerCAmelCase  :  Optional[int]):







											'''simple docstring'''

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





@require_torch
class _UpperCamelCase		(     A      ,       A      ,       A      ,       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 				__lowerCamelCase     (							self  :  int  ,       _lowerCAmelCase  :  Tuple  ,       _lowerCAmelCase  :  str  ,       _lowerCAmelCase  :  List[Any]  ,       _lowerCAmelCase  :  List[str]  ,       _lowerCAmelCase  :  List[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 				__lowerCamelCase     (							self  :  List[str]):







											'''simple docstring'''

											__lowercase       	=CTRLModelTester(self)
											__lowercase       	=ConfigTester(self  ,       config_class=_lowerCAmelCase  ,       n_embd=3_7)

				def 				__lowerCamelCase     (							self  :  Optional[int]):







											'''simple docstring'''

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

				def 				__lowerCamelCase     (							self  :  Optional[int]):







											'''simple docstring'''

											self.config_tester.run_common_tests()

				def 				__lowerCamelCase     (							self  :  int):







											'''simple docstring'''

											__lowercase       	=self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_ctrl_model(*_lowerCAmelCase)

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







											'''simple docstring'''

											__lowercase       	=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 				__lowerCamelCase     (							self  :  Dict):







											'''simple docstring'''

											pass

				@slow
				def 				__lowerCamelCase     (							self  :  Union[str, Any]):







											'''simple docstring'''

											for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		__lowercase       	=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 				__lowerCamelCase     (							self  :  int):







											'''simple docstring'''

											pass





@require_torch
class _UpperCamelCase		(     unittest.TestCase		):





				'''simple docstring'''





				def 				__lowerCamelCase     (							self  :  str):







											'''simple docstring'''

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

				@slow
				def 				__lowerCamelCase     (							self  :  Tuple):







											'''simple docstring'''

											__lowercase       	=CTRLLMHeadModel.from_pretrained('ctrl')
											model.to(_lowerCAmelCase)
											__lowercase       	=torch.tensor(
											    [[1_1_8_5_9, 0, 1_6_1_1, 8]]  ,       dtype=torch.long  ,       device=_lowerCAmelCase)  # Legal the president is
											__lowercase       	=[
											    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

											__lowercase       	=model.generate(_lowerCAmelCase  ,       do_sample=_lowerCAmelCase)
											self.assertListEqual(output_ids[0].tolist()  ,       _lowerCAmelCase)








'''simple docstring'''



from decimal import Decimal, getcontext
from math import ceil, factorial



def   _A       (   _lowerCAmelCase					):



							"""simple docstring"""



							if not isinstance(_lowerCAmelCase  ,   _lowerCAmelCase					):
														raise TypeError('Undefined for non-integers'					)
							elif precision < 1:
														raise ValueError('Undefined for non-natural numbers'					)

							__lowercase       	=precision
							__lowercase       	=ceil(precision / 14					)
							__lowercase       	=426_880 * Decimal(10_005					).sqrt()
							__lowercase       	=1
							__lowercase       	=13_591_409
							__lowercase       	=Decimal(_lowerCAmelCase					)
							for k in range(1  ,   _lowerCAmelCase					):
														__lowercase       	=factorial(6 * k					) // (factorial(3 * k					) * factorial(_lowerCAmelCase					) ** 3)
														linear_term += 545_140_134
														exponential_term *= -262_537_412_640_768_000
														partial_sum += Decimal(multinomial_term * linear_term					) / exponential_term
							return str(constant_term / partial_sum					)[:-1]


if __name__ == "__main__":
		lowerCamelCase	 =       50
		print(f"The first {n} digits of pi is: {pi(n)}")



