



"""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	: List[str]						,  lowercase_	: Optional[int]						,  lowercase_	: Optional[Any]=13						,  lowercase_	: str=7						,  lowercase_	: int=True						,  lowercase_	: Dict=True						,  lowercase_	: str=True						,  lowercase_	: Optional[int]=True						,  lowercase_	: Union[str, Any]=99						,  lowercase_	: str=64						,  lowercase_	: List[Any]=32						,  lowercase_	: Any=5						,  lowercase_	: Union[str, Any]=4						,  lowercase_	: Tuple=37						,  lowercase_	: Dict="gelu"						,  lowercase_	: List[str]=0.1						,  lowercase_	: Optional[int]=0.1						,  lowercase_	: str=512						,  lowercase_	: Optional[int]=16						,  lowercase_	: int=2						,  lowercase_	: Optional[int]=0.02						,  lowercase_	: str=3						,  lowercase_	: Dict=4						,  lowercase_	: Union[str, Any]=None						,  ):
     snake_case_				:  Any       =			parent
     snake_case_				:  List[str]       =			batch_size
     snake_case_				:  List[str]       =			seq_length
     snake_case_				:  Union[str, Any]       =			is_training
     snake_case_				:  List[Any]       =			use_input_mask
     snake_case_				:  str       =			use_token_type_ids
     snake_case_				:  str       =			use_labels
     snake_case_				:  Optional[int]       =			vocab_size
     snake_case_				:  List[str]       =			hidden_size
     snake_case_				:  List[str]       =			embedding_size
     snake_case_				:  Dict       =			num_hidden_layers
     snake_case_				:  Dict       =			num_attention_heads
     snake_case_				:  Union[str, Any]       =			intermediate_size
     snake_case_				:  Any       =			hidden_act
     snake_case_				:  str       =			hidden_dropout_prob
     snake_case_				:  List[str]       =			attention_probs_dropout_prob
     snake_case_				:  Optional[int]       =			max_position_embeddings
     snake_case_				:  Optional[Any]       =			type_vocab_size
     snake_case_				:  int       =			type_sequence_label_size
     snake_case_				:  Any       =			initializer_range
     snake_case_				:  Optional[Any]       =			num_labels
     snake_case_				:  List[str]       =			num_choices
     snake_case_				:  List[Any]       =			scope

 def 		_snake_case      (  self	: str       ):
     snake_case_				:  List[str]       =			ids_tensor([self.batch_size, self.seq_length]						,  self.vocab_size       )

     snake_case_				:  Dict       =			None
     if self.use_input_mask:
         snake_case_				:  Optional[int]       =			random_attention_mask([self.batch_size, self.seq_length]       )

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

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

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

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

 def 		_snake_case      (  self	: List[str]       ):
     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=lowercase_						,  initializer_range=self.initializer_range						,  )

 def 		_snake_case      (  self	: Optional[Any]						,  lowercase_	: List[Any]						,  lowercase_	: Optional[Any]						,  lowercase_	: Tuple						,  lowercase_	: Union[str, Any]						,  lowercase_	: Any						,  lowercase_	: List[Any]						,  lowercase_	: Optional[int]       ):
     snake_case_				:  Optional[int]       =			MegatronBertModel(config=lowercase_       )
     model.to(lowercase_       )
     model.eval()
     snake_case_				:  Dict       =			model(lowercase_						,  attention_mask=lowercase_						,  token_type_ids=lowercase_       )
     snake_case_				:  Any       =			model(lowercase_						,  token_type_ids=lowercase_       )
     snake_case_				:  List[Any]       =			model(lowercase_       )

     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 		_snake_case      (  self	: Tuple						,  lowercase_	: Optional[Any]						,  lowercase_	: Optional[int]						,  lowercase_	: Tuple						,  lowercase_	: Union[str, Any]						,  lowercase_	: List[Any]						,  lowercase_	: str						,  lowercase_	: Dict       ):
     snake_case_				:  str       =			MegatronBertForMaskedLM(config=lowercase_       )
     model.to(lowercase_       )
     model.eval()
     snake_case_				:  Union[str, Any]       =			model(lowercase_						,  attention_mask=lowercase_						,  token_type_ids=lowercase_						,  labels=lowercase_       )
     self.parent.assertEqual(result.logits.shape						,  (self.batch_size, self.seq_length, self.vocab_size)       )

 def 		_snake_case      (  self	: Optional[Any]						,  lowercase_	: List[Any]						,  lowercase_	: int						,  lowercase_	: int						,  lowercase_	: Dict						,  lowercase_	: str						,  lowercase_	: Optional[Any]						,  lowercase_	: Dict       ):
     snake_case_				:  Any       =			MegatronBertForCausalLM(config=lowercase_       )
     model.to(lowercase_       )
     model.eval()
     snake_case_				:  Dict       =			model(lowercase_						,  attention_mask=lowercase_						,  token_type_ids=lowercase_						,  labels=lowercase_       )
     self.parent.assertEqual(result.logits.shape						,  (self.batch_size, self.seq_length, self.vocab_size)       )

 def 		_snake_case      (  self	: List[str]						,  lowercase_	: Optional[int]						,  lowercase_	: Any						,  lowercase_	: Optional[int]						,  lowercase_	: Tuple						,  lowercase_	: Dict						,  lowercase_	: List[str]						,  lowercase_	: str       ):
     snake_case_				:  Dict       =			MegatronBertForNextSentencePrediction(config=lowercase_       )
     model.to(lowercase_       )
     model.eval()
     snake_case_				:  Union[str, Any]       =			model(
         lowercase_						,  attention_mask=lowercase_						,  token_type_ids=lowercase_						,  labels=lowercase_						,  )
     self.parent.assertEqual(result.logits.shape						,  (self.batch_size, 2)       )

 def 		_snake_case      (  self	: Optional[int]						,  lowercase_	: List[Any]						,  lowercase_	: Optional[Any]						,  lowercase_	: int						,  lowercase_	: str						,  lowercase_	: int						,  lowercase_	: Tuple						,  lowercase_	: List[str]       ):
     snake_case_				:  Optional[int]       =			MegatronBertForPreTraining(config=lowercase_       )
     model.to(lowercase_       )
     model.eval()
     snake_case_				:  List[str]       =			model(
         lowercase_						,  attention_mask=lowercase_						,  token_type_ids=lowercase_						,  labels=lowercase_						,  next_sentence_label=lowercase_						,  )
     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 		_snake_case      (  self	: Dict						,  lowercase_	: Dict						,  lowercase_	: Union[str, Any]						,  lowercase_	: List[Any]						,  lowercase_	: Tuple						,  lowercase_	: Optional[Any]						,  lowercase_	: Optional[Any]						,  lowercase_	: Tuple       ):
     snake_case_				:  List[Any]       =			MegatronBertForQuestionAnswering(config=lowercase_       )
     model.to(lowercase_       )
     model.eval()
     snake_case_				:  str       =			model(
         lowercase_						,  attention_mask=lowercase_						,  token_type_ids=lowercase_						,  start_positions=lowercase_						,  end_positions=lowercase_						,  )
     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 		_snake_case      (  self	: List[Any]						,  lowercase_	: Any						,  lowercase_	: str						,  lowercase_	: List[Any]						,  lowercase_	: Any						,  lowercase_	: Union[str, Any]						,  lowercase_	: List[Any]						,  lowercase_	: int       ):
     snake_case_				:  int       =			self.num_labels
     snake_case_				:  Optional[Any]       =			MegatronBertForSequenceClassification(lowercase_       )
     model.to(lowercase_       )
     model.eval()
     snake_case_				:  Tuple       =			model(lowercase_						,  attention_mask=lowercase_						,  token_type_ids=lowercase_						,  labels=lowercase_       )
     self.parent.assertEqual(result.logits.shape						,  (self.batch_size, self.num_labels)       )

 def 		_snake_case      (  self	: List[Any]						,  lowercase_	: Dict						,  lowercase_	: int						,  lowercase_	: int						,  lowercase_	: List[str]						,  lowercase_	: str						,  lowercase_	: List[str]						,  lowercase_	: List[Any]       ):
     snake_case_				:  Any       =			self.num_labels
     snake_case_				:  str       =			MegatronBertForTokenClassification(config=lowercase_       )
     model.to(lowercase_       )
     model.eval()
     snake_case_				:  int       =			model(lowercase_						,  attention_mask=lowercase_						,  token_type_ids=lowercase_						,  labels=lowercase_       )
     self.parent.assertEqual(result.logits.shape						,  (self.batch_size, self.seq_length, self.num_labels)       )

 def 		_snake_case      (  self	: Union[str, Any]						,  lowercase_	: Optional[int]						,  lowercase_	: List[Any]						,  lowercase_	: Tuple						,  lowercase_	: str						,  lowercase_	: Optional[int]						,  lowercase_	: Tuple						,  lowercase_	: str       ):
     snake_case_				:  int       =			self.num_choices
     snake_case_				:  Optional[Any]       =			MegatronBertForMultipleChoice(config=lowercase_       )
     model.to(lowercase_       )
     model.eval()
     snake_case_				:  str       =			input_ids.unsqueeze(1       ).expand(-1						,  self.num_choices						,  -1       ).contiguous()
     snake_case_				:  int       =			token_type_ids.unsqueeze(1       ).expand(-1						,  self.num_choices						,  -1       ).contiguous()
     snake_case_				:  Dict       =			input_mask.unsqueeze(1       ).expand(-1						,  self.num_choices						,  -1       ).contiguous()
     snake_case_				:  Union[str, Any]       =			model(
         lowercase_						,  attention_mask=lowercase_						,  token_type_ids=lowercase_						,  labels=lowercase_						,  )
     self.parent.assertEqual(result.logits.shape						,  (self.batch_size, self.num_choices)       )






 def 		_snake_case      (  self	: List[str]       ):
     snake_case_				:  Dict       =			self.prepare_config_and_inputs()
     (
         (
         snake_case_
     ),			(
         snake_case_
     ),			(
         snake_case_
     ),			(
         snake_case_
     ),			(
         snake_case_
     ),			(
         snake_case_
     ),			(
         snake_case_
     ),			
     )				:  Union[str, Any]       =			config_and_inputs
     snake_case_				:  Tuple       =			{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
     return config, inputs_dict





@require_torch
class  _UpperCAmelCase (    lowerCAmelCase__					,  lowerCAmelCase__					,  unittest.TestCase):
 _lowerCAmelCase     :    Any	      =	(
     (
         MegatronBertModel,
         MegatronBertForMaskedLM,
         MegatronBertForCausalLM,
         MegatronBertForMultipleChoice,
         MegatronBertForNextSentencePrediction,
         MegatronBertForPreTraining,
         MegatronBertForQuestionAnswering,
         MegatronBertForSequenceClassification,
         MegatronBertForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 _lowerCAmelCase     :    List[str]	      =	(
     {
         """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 {}
 )
 _lowerCAmelCase     :    Any	      =	True
 # test_resize_embeddings = False
 _lowerCAmelCase     :    str	      =	False

 def 		_snake_case      (  self	: Optional[Any]						,  lowercase_	: List[Any]						,  lowercase_	: Any						,  lowercase_	: Union[str, Any]=False       ):
     snake_case_				:  Optional[Any]       =			super()._prepare_for_class(lowercase_						,  lowercase_						,  return_labels=lowercase_       )

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

 def 		_snake_case      (  self	: Dict       ):
     snake_case_				:  Any       =			MegatronBertModelTester(self       )
     snake_case_				:  Optional[int]       =			ConfigTester(self						,  config_class=lowercase_						,  hidden_size=37       )

 def 		_snake_case      (  self	: Optional[int]       ):
     self.config_tester.run_common_tests()

 def 		_snake_case      (  self	: str       ):
     snake_case_				:  Union[str, Any]       =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_model(*lowercase_       )

 def 		_snake_case      (  self	: List[Any]       ):
     snake_case_				:  Dict       =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_masked_lm(*lowercase_       )

 def 		_snake_case      (  self	: List[Any]       ):
     snake_case_				:  Optional[int]       =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_multiple_choice(*lowercase_       )

 def 		_snake_case      (  self	: Optional[Any]       ):
     snake_case_				:  Tuple       =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_next_sequence_prediction(*lowercase_       )

 def 		_snake_case      (  self	: List[Any]       ):
     snake_case_				:  List[Any]       =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_pretraining(*lowercase_       )

 def 		_snake_case      (  self	: Tuple       ):
     snake_case_				:  List[str]       =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_question_answering(*lowercase_       )

 def 		_snake_case      (  self	: Tuple       ):
     snake_case_				:  List[str]       =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_sequence_classification(*lowercase_       )






 def 		_snake_case      (  self	: Tuple       ):
     snake_case_				:  Tuple       =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_token_classification(*lowercase_       )





def __lowercase    (							_a					):
    return torch.tensor(
        _a	,    dtype=torch.long	,    device=_a	,    )


lowercase__					:						Dict												=			1e-4






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

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

     snake_case_				:  List[str]       =			[-0.60_40, -0.25_17, -0.10_25, 0.34_20, -0.67_58, -0.00_17, -0.10_89, -0.19_90, 0.57_28]
     for ii in range(3       ):
         for jj in range(3       ):
             snake_case_				:  Union[str, Any]       =			output[0, ii, jj]
             snake_case_				:  str       =			expected[3 * ii + jj]
             snake_case_				:  List[Any]       =			'''ii={} jj={} a={} b={}'''.format(lowercase_						,  lowercase_						,  lowercase_						,  lowercase_       )
             self.assertTrue(math.isclose(lowercase_						,  lowercase_						,  rel_tol=lowercase_						,  abs_tol=lowercase_       )						,  msg=lowercase_       )










"""simple docstring"""

import unittest

from transformers import is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device


if is_torch_available():
     import torch

     from transformers import AutoModelForImageClassification

if is_vision_available():
     from transformers import AutoImageProcessor




@require_torch
@require_vision
class  _UpperCAmelCase (    unittest.TestCase):

 @slow
 def 		_snake_case      (  self	: List[Any]       ):
     snake_case_				:  List[Any]       =			AutoImageProcessor.from_pretrained('''microsoft/dit-base-finetuned-rvlcdip'''       )
     snake_case_				:  List[Any]       =			AutoModelForImageClassification.from_pretrained('''microsoft/dit-base-finetuned-rvlcdip'''       )
     model.to(lowercase_       )

     from datasets import load_dataset

     snake_case_				:  str       =			load_dataset('''nielsr/rvlcdip-demo'''       )

     snake_case_				:  Tuple       =			dataset['''train'''][0]['''image'''].convert('''RGB'''       )

     snake_case_				:  Union[str, Any]       =			image_processor(lowercase_						,  return_tensors='''pt'''       ).to(lowercase_       )

     # forward pass
     with torch.no_grad():
         snake_case_				:  List[Any]       =			model(**lowercase_       )
         snake_case_				:  int       =			outputs.logits

     snake_case_				:  Any       =			torch.Size((1, 16)       )
     self.assertEqual(logits.shape						,  lowercase_       )

     snake_case_				:  Any       =			torch.tensor(
         [-0.41_58, -0.40_92, -0.43_47]						,  device=lowercase_						,  dtype=torch.float						,  )
     self.assertTrue(torch.allclose(logits[0, :3]						,  lowercase_						,  atol=1E-4       )       )





