






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







		'''simple docstring'''



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



		def  __UpperCAmelCase      (							self       )     ->							Any:
							UpperCAmelCase_   :				Optional[Any]						=						ids_tensor([self.batch_size, self.seq_length]    ,		self.vocab_size       )

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

							UpperCAmelCase_   :				int						=						None
							if self.use_token_type_ids:
												UpperCAmelCase_   :				Optional[Any]						=						ids_tensor([self.batch_size, self.seq_length]    ,		self.type_vocab_size       )

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

							UpperCAmelCase_   :				Any						=						None
							UpperCAmelCase_   :				Optional[Any]						=						None
							UpperCAmelCase_   :				Any						=						None
							if self.use_labels:
												UpperCAmelCase_   :				Optional[Any]						=						ids_tensor([self.batch_size]    ,		self.type_sequence_label_size       )
												UpperCAmelCase_   :				Tuple						=						ids_tensor([self.batch_size, self.seq_length]    ,		self.num_labels       )
												UpperCAmelCase_   :				Optional[int]						=						ids_tensor([self.batch_size]    ,		self.num_choices       )

							UpperCAmelCase_   :				Any						=						self.get_config()

							UpperCAmelCase_   :				List[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       )     ->							Any:
							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    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		*_UpperCamelCase       )     ->							Optional[Any]:
							UpperCAmelCase_   :				Any						=						CTRLModel(config=_UpperCamelCase       )
							model.to(_UpperCamelCase       )
							model.eval()

							model(_UpperCamelCase    ,		token_type_ids=_UpperCamelCase    ,		head_mask=_UpperCamelCase       )
							model(_UpperCamelCase    ,		token_type_ids=_UpperCamelCase       )
							UpperCAmelCase_   :				Optional[int]						=						model(_UpperCamelCase       )
							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    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		*_UpperCamelCase       )     ->							Union[str, Any]:
							UpperCAmelCase_   :				Optional[int]						=						CTRLLMHeadModel(_UpperCamelCase       )
							model.to(_UpperCamelCase       )
							model.eval()

							UpperCAmelCase_   :				str						=						model(_UpperCamelCase    ,		token_type_ids=_UpperCamelCase    ,		labels=_UpperCamelCase       )
							self.parent.assertEqual(result.loss.shape    ,		()       )
							self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.seq_length, self.vocab_size)       )



		def  __UpperCAmelCase      (							self       )     ->							List[Any]:
							UpperCAmelCase_   :				Optional[int]						=						self.prepare_config_and_inputs()

							(
							    (
							    UpperCAmelCase_
							)	,  (
							    UpperCAmelCase_
							)	,  (
							    UpperCAmelCase_
							)	,  (
							    UpperCAmelCase_
							)	,  (
							    UpperCAmelCase_
							)	,  (
							    UpperCAmelCase_
							)	,  (
							    UpperCAmelCase_
							)	,  (
							    UpperCAmelCase_
							)	,  (
							    UpperCAmelCase_
							)	,  
							)   :				Union[str, Any]						=						config_and_inputs

							UpperCAmelCase_   :				List[str]						=						{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'head_mask': head_mask}

							return config, inputs_dict



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		*_UpperCamelCase       )     ->							Optional[int]:
							UpperCAmelCase_   :				Dict						=						self.num_labels
							UpperCAmelCase_   :				Tuple						=						CTRLForSequenceClassification(_UpperCamelCase       )
							model.to(_UpperCamelCase       )
							model.eval()
							UpperCAmelCase_   :				Any						=						ids_tensor([self.batch_size]    ,		self.type_sequence_label_size       )
							UpperCAmelCase_   :				int						=						model(_UpperCamelCase    ,		token_type_ids=_UpperCamelCase    ,		labels=_UpperCamelCase       )
							self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.num_labels)       )





@require_torch
class   lowerCamelCase				(_snake_case			,  _snake_case			,  _snake_case			,  unittest.TestCase      ):







		'''simple docstring'''



		_snake_case   :   Tuple							=  (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
		_snake_case   :   Optional[int]							=  (CTRLLMHeadModel,) if is_torch_available() else ()
		_snake_case   :   Union[str, Any]							=  (
		    {
		        '''feature-extraction''': CTRLModel,
		        '''text-classification''': CTRLForSequenceClassification,
		        '''text-generation''': CTRLLMHeadModel,
		        '''zero-shot''': CTRLForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)
		_snake_case   :   Dict							=  True
		_snake_case   :   Tuple							=  False
		_snake_case   :   int							=  False



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase       )     ->							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  __UpperCAmelCase      (							self       )     ->							Union[str, Any]:
							UpperCAmelCase_   :				str						=						CTRLModelTester(self       )
							UpperCAmelCase_   :				List[str]						=						ConfigTester(self    ,		config_class=_UpperCamelCase    ,		n_embd=3_7       )



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



		def  __UpperCAmelCase      (							self       )     ->							Union[str, Any]:
							self.config_tester.run_common_tests()



		def  __UpperCAmelCase      (							self       )     ->							Optional[int]:
							UpperCAmelCase_   :				Tuple						=						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_ctrl_model(*_UpperCamelCase       )



		def  __UpperCAmelCase      (							self       )     ->							Tuple:
							UpperCAmelCase_   :				List[Any]						=						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_lm_head_model(*_UpperCamelCase       )



		@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'       )
		def  __UpperCAmelCase      (							self       )     ->							List[str]:
							pass



		@slow
		def  __UpperCAmelCase      (							self       )     ->							int:
							for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												UpperCAmelCase_   :				Optional[Any]						=						CTRLModel.from_pretrained(_UpperCamelCase       )
												self.assertIsNotNone(_UpperCamelCase       )



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





@require_torch
class   lowerCamelCase				(unittest.TestCase      ):







		'''simple docstring'''



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



		@slow
		def  __UpperCAmelCase      (							self       )     ->							List[Any]:
							UpperCAmelCase_   :				Any						=						CTRLLMHeadModel.from_pretrained('ctrl'       )
							model.to(_UpperCamelCase       )
							UpperCAmelCase_   :				Optional[int]						=						torch.tensor(
							    [[1_1_8_5_9, 0, 1_6_1_1, 8]]    ,		dtype=torch.long    ,		device=_UpperCamelCase       )  # Legal the president is
							UpperCAmelCase_   :				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(_UpperCamelCase    ,		do_sample=_UpperCamelCase       )
							self.assertListEqual(output_ids[0].tolist()    ,		_UpperCamelCase       )














from __future__ import annotations




def 					lowercase__				(					__snake_case   :							list[int]					,	__snake_case   :							int  ):


					'''simple docstring'''


					if len(__snake_case  ) < k or k < 0:
										raise ValueError('Invalid Input'  )
					UpperCAmelCase_   :				int						=						sum(array[:k]  )
					for i in range(len(__snake_case  ) - k  ):
										UpperCAmelCase_   :				List[Any]						=						current_sum - array[i] + array[i + k]
										UpperCAmelCase_   :				List[Any]						=						max(__snake_case					,	__snake_case  )
					return max_sum


if __name__ == "__main__":
		from doctest import testmod
		from random import randint

		testmod()
		__UpperCAmelCase     							=						[randint(-1000, 1000) for i in range(100)]
		__UpperCAmelCase     							=						randint(0, 110)
		print(F'The maximum sum of {k} consecutive elements is {max_sum_in_array(array,k)}')






