



'''simple docstring'''




import math
import os
import unittest

from transformers import MegatronBertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

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 (
							    MODEL_FOR_PRETRAINING_MAPPING,
							    MegatronBertForCausalLM,
							    MegatronBertForMaskedLM,
							    MegatronBertForMultipleChoice,
							    MegatronBertForNextSentencePrediction,
							    MegatronBertForPreTraining,
							    MegatronBertForQuestionAnswering,
							    MegatronBertForSequenceClassification,
							    MegatronBertForTokenClassification,
							    MegatronBertModel,
							)

class     UpperCamelCase_				:





	def __init__(						self	,	A	,	A=13	,	A=7	,	A=True	,	A=True	,	A=True	,	A=True	,	A=99	,	A=64	,	A=32	,	A=5	,	A=4	,	A=37	,	A="gelu"	,	A=0.1	,	A=0.1	,	A=512	,	A=16	,	A=2	,	A=0.0_2	,	A=3	,	A=4	,	A=None	,	)    ->    Dict:
								UpperCAmelCase  :	Union[str, Any]							  =	parent
								UpperCAmelCase  :	List[Any]							  =	batch_size
								UpperCAmelCase  :	Optional[int]							  =	seq_length
								UpperCAmelCase  :	int							  =	is_training
								UpperCAmelCase  :	List[str]							  =	use_input_mask
								UpperCAmelCase  :	Any							  =	use_token_type_ids
								UpperCAmelCase  :	Dict							  =	use_labels
								UpperCAmelCase  :	str							  =	vocab_size
								UpperCAmelCase  :	Union[str, Any]							  =	hidden_size
								UpperCAmelCase  :	str							  =	embedding_size
								UpperCAmelCase  :	Dict							  =	num_hidden_layers
								UpperCAmelCase  :	List[Any]							  =	num_attention_heads
								UpperCAmelCase  :	Optional[int]							  =	intermediate_size
								UpperCAmelCase  :	str							  =	hidden_act
								UpperCAmelCase  :	str							  =	hidden_dropout_prob
								UpperCAmelCase  :	Dict							  =	attention_probs_dropout_prob
								UpperCAmelCase  :	Any							  =	max_position_embeddings
								UpperCAmelCase  :	str							  =	type_vocab_size
								UpperCAmelCase  :	Tuple							  =	type_sequence_label_size
								UpperCAmelCase  :	Optional[Any]							  =	initializer_range
								UpperCAmelCase  :	Any							  =	num_labels
								UpperCAmelCase  :	Union[str, Any]							  =	num_choices
								UpperCAmelCase  :	Dict							  =	scope





	def 				_lowercase(						self					)    ->    Any:
								UpperCAmelCase  :	Union[str, Any]							  =	ids_tensor([self.batch_size, self.seq_length]	,	self.vocab_size					)

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

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

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

								UpperCAmelCase  :	Any							  =	self.get_config()

								return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels





	def 				_lowercase(						self					)    ->    Tuple:
								return MegatronBertConfig(
								    vocab_size=self.vocab_size	,	hidden_size=self.hidden_size	,	num_hidden_layers=self.num_hidden_layers	,	num_attention_heads=self.num_attention_heads	,	intermediate_size=self.intermediate_size	,	embedding_size=self.embedding_size	,	hidden_act=self.hidden_act	,	hidden_dropout_prob=self.hidden_dropout_prob	,	attention_probs_dropout_prob=self.attention_probs_dropout_prob	,	max_position_embeddings=self.max_position_embeddings	,	type_vocab_size=self.type_vocab_size	,	is_decoder=A	,	initializer_range=self.initializer_range	,	)





	def 				_lowercase(						self	,	A	,	A	,	A	,	A	,	A	,	A	,	A					)    ->    str:
								UpperCAmelCase  :	Any							  =	MegatronBertModel(config=A					)
								model.to(A					)
								model.eval()
								UpperCAmelCase  :	List[str]							  =	model(A	,	attention_mask=A	,	token_type_ids=A					)
								UpperCAmelCase  :	Union[str, Any]							  =	model(A	,	token_type_ids=A					)
								UpperCAmelCase  :	Tuple							  =	model(A					)

								self.parent.assertEqual(result.last_hidden_state.shape	,	(self.batch_size, self.seq_length, self.hidden_size)					)
								self.parent.assertEqual(result.pooler_output.shape	,	(self.batch_size, self.hidden_size)					)





	def 				_lowercase(						self	,	A	,	A	,	A	,	A	,	A	,	A	,	A					)    ->    Dict:
								UpperCAmelCase  :	Dict							  =	MegatronBertForMaskedLM(config=A					)
								model.to(A					)
								model.eval()
								UpperCAmelCase  :	Optional[Any]							  =	model(A	,	attention_mask=A	,	token_type_ids=A	,	labels=A					)
								self.parent.assertEqual(result.logits.shape	,	(self.batch_size, self.seq_length, self.vocab_size)					)





	def 				_lowercase(						self	,	A	,	A	,	A	,	A	,	A	,	A	,	A					)    ->    int:
								UpperCAmelCase  :	List[Any]							  =	MegatronBertForCausalLM(config=A					)
								model.to(A					)
								model.eval()
								UpperCAmelCase  :	Union[str, Any]							  =	model(A	,	attention_mask=A	,	token_type_ids=A	,	labels=A					)
								self.parent.assertEqual(result.logits.shape	,	(self.batch_size, self.seq_length, self.vocab_size)					)





	def 				_lowercase(						self	,	A	,	A	,	A	,	A	,	A	,	A	,	A					)    ->    Tuple:
								UpperCAmelCase  :	Dict							  =	MegatronBertForNextSentencePrediction(config=A					)
								model.to(A					)
								model.eval()
								UpperCAmelCase  :	List[Any]							  =	model(
								    A	,	attention_mask=A	,	token_type_ids=A	,	labels=A	,	)
								self.parent.assertEqual(result.logits.shape	,	(self.batch_size, 2)					)





	def 				_lowercase(						self	,	A	,	A	,	A	,	A	,	A	,	A	,	A					)    ->    Tuple:
								UpperCAmelCase  :	List[Any]							  =	MegatronBertForPreTraining(config=A					)
								model.to(A					)
								model.eval()
								UpperCAmelCase  :	Union[str, Any]							  =	model(
								    A	,	attention_mask=A	,	token_type_ids=A	,	labels=A	,	next_sentence_label=A	,	)
								self.parent.assertEqual(result.prediction_logits.shape	,	(self.batch_size, self.seq_length, self.vocab_size)					)
								self.parent.assertEqual(result.seq_relationship_logits.shape	,	(self.batch_size, 2)					)





	def 				_lowercase(						self	,	A	,	A	,	A	,	A	,	A	,	A	,	A					)    ->    List[str]:
								UpperCAmelCase  :	List[str]							  =	MegatronBertForQuestionAnswering(config=A					)
								model.to(A					)
								model.eval()
								UpperCAmelCase  :	Any							  =	model(
								    A	,	attention_mask=A	,	token_type_ids=A	,	start_positions=A	,	end_positions=A	,	)
								self.parent.assertEqual(result.start_logits.shape	,	(self.batch_size, self.seq_length)					)
								self.parent.assertEqual(result.end_logits.shape	,	(self.batch_size, self.seq_length)					)





	def 				_lowercase(						self	,	A	,	A	,	A	,	A	,	A	,	A	,	A					)    ->    Tuple:
								UpperCAmelCase  :	Any							  =	self.num_labels
								UpperCAmelCase  :	Dict							  =	MegatronBertForSequenceClassification(A					)
								model.to(A					)
								model.eval()
								UpperCAmelCase  :	Optional[Any]							  =	model(A	,	attention_mask=A	,	token_type_ids=A	,	labels=A					)
								self.parent.assertEqual(result.logits.shape	,	(self.batch_size, self.num_labels)					)





	def 				_lowercase(						self	,	A	,	A	,	A	,	A	,	A	,	A	,	A					)    ->    Tuple:
								UpperCAmelCase  :	Tuple							  =	self.num_labels
								UpperCAmelCase  :	Optional[Any]							  =	MegatronBertForTokenClassification(config=A					)
								model.to(A					)
								model.eval()
								UpperCAmelCase  :	Union[str, Any]							  =	model(A	,	attention_mask=A	,	token_type_ids=A	,	labels=A					)
								self.parent.assertEqual(result.logits.shape	,	(self.batch_size, self.seq_length, self.num_labels)					)





	def 				_lowercase(						self	,	A	,	A	,	A	,	A	,	A	,	A	,	A					)    ->    str:
								UpperCAmelCase  :	Dict							  =	self.num_choices
								UpperCAmelCase  :	str							  =	MegatronBertForMultipleChoice(config=A					)
								model.to(A					)
								model.eval()
								UpperCAmelCase  :	Dict							  =	input_ids.unsqueeze(1					).expand(-1	,	self.num_choices	,	-1					).contiguous()
								UpperCAmelCase  :	str							  =	token_type_ids.unsqueeze(1					).expand(-1	,	self.num_choices	,	-1					).contiguous()
								UpperCAmelCase  :	str							  =	input_mask.unsqueeze(1					).expand(-1	,	self.num_choices	,	-1					).contiguous()
								UpperCAmelCase  :	Any							  =	model(
								    A	,	attention_mask=A	,	token_type_ids=A	,	labels=A	,	)
								self.parent.assertEqual(result.logits.shape	,	(self.batch_size, self.num_choices)					)






	def 				_lowercase(						self					)    ->    List[Any]:
								UpperCAmelCase  :	int							  =	self.prepare_config_and_inputs()
								(
								    (
								    UpperCAmelCase					
								)  ,  (
								    UpperCAmelCase					
								)  ,  (
								    UpperCAmelCase					
								)  ,  (
								    UpperCAmelCase					
								)  ,  (
								    UpperCAmelCase					
								)  ,  (
								    UpperCAmelCase					
								)  ,  (
								    UpperCAmelCase					
								)  ,  					
								)  :	Any							  =	config_and_inputs
								UpperCAmelCase  :	int							  =	{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
								return config, inputs_dict




@require_torch
class     UpperCamelCase_				(							lowercase_   ,  lowercase_   ,  unittest.TestCase    ):
	lowercase    =				(
	    (
	        MegatronBertModel,
	        MegatronBertForMaskedLM,
	        MegatronBertForCausalLM,
	        MegatronBertForMultipleChoice,
	        MegatronBertForNextSentencePrediction,
	        MegatronBertForPreTraining,
	        MegatronBertForQuestionAnswering,
	        MegatronBertForSequenceClassification,
	        MegatronBertForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	lowercase    =				(
	    {
	        '''feature-extraction''': MegatronBertModel,
	        '''fill-mask''': MegatronBertForMaskedLM,
	        '''question-answering''': MegatronBertForQuestionAnswering,
	        '''text-classification''': MegatronBertForSequenceClassification,
	        '''text-generation''': MegatronBertForCausalLM,
	        '''token-classification''': MegatronBertForTokenClassification,
	        '''zero-shot''': MegatronBertForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	lowercase    =				True
	# test_resize_embeddings = False
	lowercase    =				False





	def 				_lowercase(						self	,	A	,	A	,	A=False					)    ->    Dict:
								UpperCAmelCase  :	Dict							  =	super()._prepare_for_class(A	,	A	,	return_labels=A					)

								if return_labels:
															if model_class in get_values(A					):
																						UpperCAmelCase  :	Optional[Any]							  =	torch.zeros(
																						    (self.model_tester.batch_size, self.model_tester.seq_length)	,	dtype=torch.long	,	device=A					)
																						UpperCAmelCase  :	Optional[int]							  =	torch.zeros(
																						    self.model_tester.batch_size	,	dtype=torch.long	,	device=A					)
								return inputs_dict





	def 				_lowercase(						self					)    ->    Union[str, Any]:
								UpperCAmelCase  :	Optional[Any]							  =	MegatronBertModelTester(self					)
								UpperCAmelCase  :	Union[str, Any]							  =	ConfigTester(self	,	config_class=A	,	hidden_size=37					)





	def 				_lowercase(						self					)    ->    List[str]:
								self.config_tester.run_common_tests()





	def 				_lowercase(						self					)    ->    Any:
								UpperCAmelCase  :	int							  =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_megatron_bert_model(*A					)





	def 				_lowercase(						self					)    ->    List[str]:
								UpperCAmelCase  :	Optional[int]							  =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_megatron_bert_for_masked_lm(*A					)





	def 				_lowercase(						self					)    ->    str:
								UpperCAmelCase  :	Any							  =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_megatron_bert_for_multiple_choice(*A					)





	def 				_lowercase(						self					)    ->    Any:
								UpperCAmelCase  :	Dict							  =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_megatron_bert_for_next_sequence_prediction(*A					)





	def 				_lowercase(						self					)    ->    Dict:
								UpperCAmelCase  :	Optional[Any]							  =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_megatron_bert_for_pretraining(*A					)





	def 				_lowercase(						self					)    ->    Tuple:
								UpperCAmelCase  :	str							  =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_megatron_bert_for_question_answering(*A					)





	def 				_lowercase(						self					)    ->    Tuple:
								UpperCAmelCase  :	Dict							  =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_megatron_bert_for_sequence_classification(*A					)






	def 				_lowercase(						self					)    ->    Any:
								UpperCAmelCase  :	List[str]							  =	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_megatron_bert_for_token_classification(*A					)




def    __lowerCamelCase						(      _lowercase	)   ->    Optional[int]:
							return torch.tensor(
							    UpperCamelCase__			,       dtype=torch.long			,       device=UpperCamelCase__			,       )


a     :    Any 						=					1E-4


@require_torch
@require_sentencepiece
@require_tokenizers
class     UpperCamelCase_				(							unittest.TestCase    ):





	@slow
	@unittest.skip("""Model is not available."""					)
	def 				_lowercase(						self					)    ->    Any:
								UpperCAmelCase  :	Dict							  =	"""nvidia/megatron-bert-uncased-345m"""
								if "MYDIR" in os.environ:
															UpperCAmelCase  :	str							  =	os.path.join(os.environ["""MYDIR"""]	,	A					)
								UpperCAmelCase  :	Any							  =	MegatronBertModel.from_pretrained(A					)
								model.to(A					)
								model.half()
								UpperCAmelCase  :	Union[str, Any]							  =	_long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]					)
								with torch.no_grad():
															UpperCAmelCase  :	Any							  =	model(A					)[0]
								UpperCAmelCase  :	List[str]							  =	torch.Size((1, 9, 1024)					)
								self.assertEqual(output.shape	,	A					)

								UpperCAmelCase  :	int							  =	[-0.6_0_4_0, -0.2_5_1_7, -0.1_0_2_5, 0.3_4_2_0, -0.6_7_5_8, -0.0_0_1_7, -0.1_0_8_9, -0.1_9_9_0, 0.5_7_2_8]
								for ii in range(3					):
															for jj in range(3					):
																						UpperCAmelCase  :	Optional[Any]							  =	output[0, ii, jj]
																						UpperCAmelCase  :	Union[str, Any]							  =	expected[3 * ii + jj]
																						UpperCAmelCase  :	Tuple							  =	"""ii={} jj={} a={} b={}""".format(A	,	A	,	A	,	A					)
																						self.assertTrue(math.isclose(A	,	A	,	rel_tol=A	,	abs_tol=A					)	,	msg=A					)






'''simple docstring'''




from datetime import datetime as dt
import os

from github import Github


a     :    int      						=					[
    """good first issue""",
    """good second issue""",
    """good difficult issue""",
    """feature request""",
    """new model""",
    """wip""",
]



def    __lowerCamelCase						(      )   ->    Dict:
							UpperCAmelCase  :	str							  =	Github(os.environ["""GITHUB_TOKEN"""]	)
							UpperCAmelCase  :	Dict							  =	g.get_repo("""huggingface/transformers"""	)
							UpperCAmelCase  :	int							  =	repo.get_issues(state="""open"""	)

							for issue in open_issues:
														UpperCAmelCase  :	Optional[int]							  =	sorted([comment for comment in issue.get_comments()]			,       key=lambda _lowercase	: i.created_at			,       reverse=_lowercase	)
														UpperCAmelCase  :	Any							  =	comments[0] if len(_lowercase	) > 0 else None
														if (
														    last_comment is not None
														    and last_comment.user.login == "github-actions[bot]"
														    and (dt.utcnow() - issue.updated_at).days > 7
														    and (dt.utcnow() - issue.created_at).days >= 3_0
														    and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels()	)
														):
																					# print(f"Would close issue {issue.number} since it has been 7 days of inactivity since bot mention.")
																					issue.edit(state="""closed"""	)
														elif (
														    (dt.utcnow() - issue.updated_at).days > 2_3
														    and (dt.utcnow() - issue.created_at).days >= 3_0
														    and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels()	)
														):
																					# print(f"Would add stale comment to {issue.number}")
																					issue.create_comment(
																					    """This issue has been automatically marked as stale because it has not had """
																					    """recent activity. If you think this still needs to be addressed """
																					    """please comment on this thread.\n\nPlease note that issues that do not follow the """
																					    """[contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) """
																					    """are likely to be ignored."""	)


if __name__ == "__main__":
							main()

