




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



 '''simple docstring'''


 def __init__(       self      :						List[str]		,							A_      :						int		,							A_      :						int=13		,							A_      :						List[Any]=7		,							A_      :						List[Any]=True		,							A_      :						Union[str, Any]=True		,							A_      :						Optional[Any]=True		,							A_      :						str=True		,							A_      :						Optional[int]=99		,							A_      :						List[Any]=64		,							A_      :						int=32		,							A_      :						List[str]=5		,							A_      :						Union[str, Any]=4		,							A_      :						List[Any]=37		,							A_      :						List[str]="gelu"		,							A_      :						Dict=0.1		,							A_      :						List[Any]=0.1		,							A_      :						int=512		,							A_      :						Dict=16		,							A_      :						List[Any]=2		,							A_      :						Optional[Any]=0.02		,							A_      :						Dict=3		,							A_      :						Optional[int]=4		,							A_      :						str=None		,							)     ->			Optional[int]:

       """simple docstring"""






       lowerCamelCase_	  =					parent
       lowerCamelCase_	  =					batch_size
       lowerCamelCase_	  =					seq_length
       lowerCamelCase_	  =					is_training
       lowerCamelCase_	  =					use_input_mask
       lowerCamelCase_	  =					use_token_type_ids
       lowerCamelCase_	  =					use_labels
       lowerCamelCase_	  =					vocab_size
       lowerCamelCase_	  =					hidden_size
       lowerCamelCase_	  =					embedding_size
       lowerCamelCase_	  =					num_hidden_layers
       lowerCamelCase_	  =					num_attention_heads
       lowerCamelCase_	  =					intermediate_size
       lowerCamelCase_	  =					hidden_act
       lowerCamelCase_	  =					hidden_dropout_prob
       lowerCamelCase_	  =					attention_probs_dropout_prob
       lowerCamelCase_	  =					max_position_embeddings
       lowerCamelCase_	  =					type_vocab_size
       lowerCamelCase_	  =					type_sequence_label_size
       lowerCamelCase_	  =					initializer_range
       lowerCamelCase_	  =					num_labels
       lowerCamelCase_	  =					num_choices
       lowerCamelCase_	  =					scope

 def        a__				(       self      :						Any						)     ->			List[Any]:

       """simple docstring"""






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

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

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

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

       lowerCamelCase_	  =					self.get_config()

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

 def        a__				(       self      :						Any						)     ->			Tuple:

       """simple docstring"""






       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        a__				(       self      :						List[Any]		,							A_      :						int		,							A_      :						str		,							A_      :						Union[str, Any]		,							A_      :						int		,							A_      :						Union[str, Any]		,							A_      :						Union[str, Any]		,							A_      :						str						)     ->			int:

       """simple docstring"""






       lowerCamelCase_	  =					MegatronBertModel(config=A_						)
       model.to(A_						)
       model.eval()
       lowerCamelCase_	  =					model(A_		,							attention_mask=A_		,							token_type_ids=A_						)
       lowerCamelCase_	  =					model(A_		,							token_type_ids=A_						)
       lowerCamelCase_	  =					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        a__				(       self      :						Any		,							A_      :						Optional[Any]		,							A_      :						Optional[Any]		,							A_      :						List[str]		,							A_      :						List[Any]		,							A_      :						Optional[Any]		,							A_      :						List[str]		,							A_      :						Union[str, Any]						)     ->			Tuple:

       """simple docstring"""






       lowerCamelCase_	  =					MegatronBertForMaskedLM(config=A_						)
       model.to(A_						)
       model.eval()
       lowerCamelCase_	  =					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        a__				(       self      :						Tuple		,							A_      :						Dict		,							A_      :						str		,							A_      :						Optional[int]		,							A_      :						Optional[int]		,							A_      :						Optional[int]		,							A_      :						Optional[Any]		,							A_      :						str						)     ->			Any:

       """simple docstring"""






       lowerCamelCase_	  =					MegatronBertForCausalLM(config=A_						)
       model.to(A_						)
       model.eval()
       lowerCamelCase_	  =					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        a__				(       self      :						Tuple		,							A_      :						str		,							A_      :						Any		,							A_      :						Optional[int]		,							A_      :						List[Any]		,							A_      :						Optional[int]		,							A_      :						Optional[Any]		,							A_      :						Optional[int]						)     ->			Union[str, Any]:

       """simple docstring"""






       lowerCamelCase_	  =					MegatronBertForNextSentencePrediction(config=A_						)
       model.to(A_						)
       model.eval()
       lowerCamelCase_	  =					model(
           A_		,							attention_mask=A_		,							token_type_ids=A_		,							labels=A_		,							)
       self.parent.assertEqual(result.logits.shape		,							(self.batch_size, 2)						)

 def        a__				(       self      :						Tuple		,							A_      :						Tuple		,							A_      :						int		,							A_      :						Optional[int]		,							A_      :						List[Any]		,							A_      :						List[str]		,							A_      :						str		,							A_      :						Tuple						)     ->			Optional[Any]:

       """simple docstring"""






       lowerCamelCase_	  =					MegatronBertForPreTraining(config=A_						)
       model.to(A_						)
       model.eval()
       lowerCamelCase_	  =					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        a__				(       self      :						List[Any]		,							A_      :						str		,							A_      :						Tuple		,							A_      :						Union[str, Any]		,							A_      :						int		,							A_      :						Any		,							A_      :						Dict		,							A_      :						Union[str, Any]						)     ->			str:

       """simple docstring"""






       lowerCamelCase_	  =					MegatronBertForQuestionAnswering(config=A_						)
       model.to(A_						)
       model.eval()
       lowerCamelCase_	  =					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        a__				(       self      :						List[str]		,							A_      :						List[str]		,							A_      :						Dict		,							A_      :						str		,							A_      :						Optional[int]		,							A_      :						Dict		,							A_      :						Dict		,							A_      :						List[str]						)     ->			Any:

       """simple docstring"""






       lowerCamelCase_	  =					self.num_labels
       lowerCamelCase_	  =					MegatronBertForSequenceClassification(A_						)
       model.to(A_						)
       model.eval()
       lowerCamelCase_	  =					model(A_		,							attention_mask=A_		,							token_type_ids=A_		,							labels=A_						)
       self.parent.assertEqual(result.logits.shape		,							(self.batch_size, self.num_labels)						)

 def        a__				(       self      :						Dict		,							A_      :						Union[str, Any]		,							A_      :						List[Any]		,							A_      :						Dict		,							A_      :						Dict		,							A_      :						Any		,							A_      :						Optional[int]		,							A_      :						Optional[Any]						)     ->			Optional[Any]:

       """simple docstring"""






       lowerCamelCase_	  =					self.num_labels
       lowerCamelCase_	  =					MegatronBertForTokenClassification(config=A_						)
       model.to(A_						)
       model.eval()
       lowerCamelCase_	  =					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        a__				(       self      :						List[Any]		,							A_      :						Optional[int]		,							A_      :						str		,							A_      :						Any		,							A_      :						Tuple		,							A_      :						Optional[int]		,							A_      :						List[str]		,							A_      :						int						)     ->			int:

       """simple docstring"""






       lowerCamelCase_	  =					self.num_choices
       lowerCamelCase_	  =					MegatronBertForMultipleChoice(config=A_						)
       model.to(A_						)
       model.eval()
       lowerCamelCase_	  =					input_ids.unsqueeze(1						).expand(-1		,							self.num_choices		,							-1						).contiguous()
       lowerCamelCase_	  =					token_type_ids.unsqueeze(1						).expand(-1		,							self.num_choices		,							-1						).contiguous()
       lowerCamelCase_	  =					input_mask.unsqueeze(1						).expand(-1		,							self.num_choices		,							-1						).contiguous()
       lowerCamelCase_	  =					model(
           A_		,							attention_mask=A_		,							token_type_ids=A_		,							labels=A_		,							)
       self.parent.assertEqual(result.logits.shape		,							(self.batch_size, self.num_choices)						)





 def        a__				(       self      :						List[str]						)     ->			Union[str, Any]:

       """simple docstring"""






       lowerCamelCase_	  =					self.prepare_config_and_inputs()
       (
           (
           lowerCamelCase_
       )  ,       (
           lowerCamelCase_
       )  ,       (
           lowerCamelCase_
       )  ,       (
           lowerCamelCase_
       )  ,       (
           lowerCamelCase_
       )  ,       (
           lowerCamelCase_
       )  ,       (
           lowerCamelCase_
       )  ,       
       )	  =					config_and_inputs
       lowerCamelCase_	  =					{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
       return config, inputs_dict







@require_torch
class 						A(		UpperCamelCase							,   UpperCamelCase							,   unittest.TestCase     ):



 '''simple docstring'''


 UpperCamelCase		     =		(
     (
         MegatronBertModel,
         MegatronBertForMaskedLM,
         MegatronBertForCausalLM,
         MegatronBertForMultipleChoice,
         MegatronBertForNextSentencePrediction,
         MegatronBertForPreTraining,
         MegatronBertForQuestionAnswering,
         MegatronBertForSequenceClassification,
         MegatronBertForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 UpperCamelCase		     =		(
     {
         '''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 {}
 )
 UpperCamelCase		     =		True
 # test_resize_embeddings = False
 UpperCamelCase		     =		False

 def        a__				(       self      :						Any		,							A_      :						List[Any]		,							A_      :						Any		,							A_      :						Union[str, Any]=False						)     ->			List[Any]:

       """simple docstring"""






       lowerCamelCase_	  =					super()._prepare_for_class(A_		,							A_		,							return_labels=A_						)

       if return_labels:
             if model_class in get_values(A_						):
                   lowerCamelCase_	  =					torch.zeros(
                       (self.model_tester.batch_size, self.model_tester.seq_length)		,							dtype=torch.long		,							device=A_						)
                   lowerCamelCase_	  =					torch.zeros(
                       self.model_tester.batch_size		,							dtype=torch.long		,							device=A_						)
       return inputs_dict

 def        a__				(       self      :						Union[str, Any]						)     ->			Dict:

       """simple docstring"""






       lowerCamelCase_	  =					MegatronBertModelTester(self						)
       lowerCamelCase_	  =					ConfigTester(self		,							config_class=A_		,							hidden_size=37						)

 def        a__				(       self      :						Optional[Any]						)     ->			List[str]:

       """simple docstring"""






       self.config_tester.run_common_tests()

 def        a__				(       self      :						str						)     ->			Union[str, Any]:

       """simple docstring"""






       lowerCamelCase_	  =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_model(*A_						)

 def        a__				(       self      :						Optional[int]						)     ->			Optional[int]:

       """simple docstring"""






       lowerCamelCase_	  =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_masked_lm(*A_						)

 def        a__				(       self      :						Optional[Any]						)     ->			Dict:

       """simple docstring"""






       lowerCamelCase_	  =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_multiple_choice(*A_						)

 def        a__				(       self      :						Union[str, Any]						)     ->			Optional[int]:

       """simple docstring"""






       lowerCamelCase_	  =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_next_sequence_prediction(*A_						)

 def        a__				(       self      :						Any						)     ->			Dict:

       """simple docstring"""






       lowerCamelCase_	  =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_pretraining(*A_						)

 def        a__				(       self      :						List[str]						)     ->			str:

       """simple docstring"""






       lowerCamelCase_	  =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_question_answering(*A_						)

 def        a__				(       self      :						List[str]						)     ->			List[Any]:

       """simple docstring"""






       lowerCamelCase_	  =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_sequence_classification(*A_						)





 def        a__				(       self      :						Tuple						)     ->			Optional[int]:

       """simple docstring"""






       lowerCamelCase_	  =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_token_classification(*A_						)

def     _SCREAMING_SNAKE_CASE    (							lowercase			: List[Any]  ):


      '''simple docstring'''



      return torch.tensor(
          lowercase			,							dtype=torch.long			,							device=lowercase			,							)


lowerCamelCase       :	Union[str, Any]									=     1e-4







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



 '''simple docstring'''


 @slow
 @unittest.skip('Model is not available.'						)
 def        a__				(       self      :						Tuple						)     ->			Optional[int]:

       """simple docstring"""






       lowerCamelCase_	  =					'nvidia/megatron-bert-uncased-345m'
       if "MYDIR" in os.environ:
             lowerCamelCase_	  =					os.path.join(os.environ['MYDIR']		,							A_						)
       lowerCamelCase_	  =					MegatronBertModel.from_pretrained(A_						)
       model.to(A_						)
       model.half()
       lowerCamelCase_	  =					_long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]						)
       with torch.no_grad():
             lowerCamelCase_	  =					model(A_						)[0]
       lowerCamelCase_	  =					torch.Size((1, 9, 1024)						)
       self.assertEqual(output.shape		,							A_						)

       lowerCamelCase_	  =					[-0.6040, -0.2517, -0.1025, 0.3420, -0.6758, -0.0017, -0.1089, -0.1990, 0.5728]
       for ii in range(3						):
             for jj in range(3						):
                   lowerCamelCase_	  =					output[0, ii, jj]
                   lowerCamelCase_	  =					expected[3 * ii + jj]
                   lowerCamelCase_	  =					'ii={} jj={} a={} b={}'.format(A_		,							A_		,							A_		,							A_						)
                   self.assertTrue(math.isclose(A_		,							A_		,							rel_tol=A_		,							abs_tol=A_						)		,							msg=A_						)









import cva
import numpy as np







class 						A:



 '''simple docstring'''


 def __init__(       self      :						int		,							A_      :						float		,							A_      :						int						)     ->			List[Any]:

       """simple docstring"""






       if k in (0.04, 0.06):
             lowerCamelCase_	  =					k
             lowerCamelCase_	  =					window_size
       else:
             raise ValueError('invalid k value'						)

 def __str__(       self      :						str						)     ->			str:

       """simple docstring"""






       return str(self.k						)





 def        a__				(       self      :						Any		,							A_      :						str						)     ->			tuple[cva.Mat, list[list[int]]]:

       """simple docstring"""






       lowerCamelCase_	  =					cva.imread(A_		,							0						)
       lowerCamelCase_  ,       lowerCamelCase_	  =					img.shape
       lowerCamelCase_	  =					[]
       lowerCamelCase_	  =					img.copy()
       lowerCamelCase_	  =					cva.cvtColor(A_		,							cva.COLOR_GRAY2RGB						)
       lowerCamelCase_  ,       lowerCamelCase_	  =					np.gradient(A_						)
       lowerCamelCase_	  =					dx**2
       lowerCamelCase_	  =					dy**2
       lowerCamelCase_	  =					dx * dy
       lowerCamelCase_	  =					0.04
       lowerCamelCase_	  =					self.window_size // 2
       for y in range(A_		,							h - offset						):
             for x in range(A_		,							w - offset						):
                   lowerCamelCase_	  =					ixx[
                       y - offset : y + offset + 1, x - offset : x + offset + 1
                   ].sum()
                   lowerCamelCase_	  =					iyy[
                       y - offset : y + offset + 1, x - offset : x + offset + 1
                   ].sum()
                   lowerCamelCase_	  =					ixy[
                       y - offset : y + offset + 1, x - offset : x + offset + 1
                   ].sum()

                   lowerCamelCase_	  =					(wxx * wyy) - (wxy**2)
                   lowerCamelCase_	  =					wxx + wyy
                   lowerCamelCase_	  =					det - k * (trace**2)
                   # Can change the value
                   if r > 0.5:
                         corner_list.append([x, y, r]						)
                         color_img.itemset((y, x, 0)		,							0						)
                         color_img.itemset((y, x, 1)		,							0						)
                         color_img.itemset((y, x, 2)		,							255						)
       return color_img, corner_list


if __name__ == "__main__":
 lowerCamelCase       :	Optional[int]									=     HarrisCorner(0.04, 3)
 lowerCamelCase					,					lowerCamelCase       :	Optional[int]									=     edge_detect.detect("path_to_image")
 cva.imwrite("detect.png", color_img)



