





'''simple docstring'''




import heapq
import sys

import numpy as np

_A:					str							=				tuple[int, int]






class UpperCAmelCase							:



    def __init__(  self					):
     __UpperCAmelCase					=   []
     __UpperCAmelCase					=   set()



    def 					__lowerCamelCase						(  self					):
     if not self.empty():
      return self.elements[0][0]
     else:
      return float('inf'					)



    def 					__lowerCamelCase						(  self					):
     return len(self.elements					) == 0



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A					):
     if item not in self.set:
      heapq.heappush(self.elements     ,			(priority, item)					)
      self.set.add(__A					)
     else:
      # update
      # print("update", item)
      __UpperCAmelCase					=   []
      ((__UpperCAmelCase)					,    (__UpperCAmelCase))					=   heapq.heappop(self.elements					)
      while x != item:
       temp.append((pri, x)					)
       ((__UpperCAmelCase)					,    (__UpperCAmelCase))					=   heapq.heappop(self.elements					)
      temp.append((priority, item)					)
      for pro, xxx in temp:
       heapq.heappush(self.elements     ,			(pro, xxx)					)



    def 					__lowerCamelCase						(  self     ,			__A					):
     if item in self.set:
      self.set.remove(__A					)
      __UpperCAmelCase					=   []
      ((__UpperCAmelCase)					,    (__UpperCAmelCase))					=   heapq.heappop(self.elements					)
      while x != item:
       temp.append((pro, x)					)
       ((__UpperCAmelCase)					,    (__UpperCAmelCase))					=   heapq.heappop(self.elements					)
      for prito, yyy in temp:
       heapq.heappush(self.elements     ,			(prito, yyy)					)



    def 					__lowerCamelCase						(  self					):
     return self.elements[0][1]




    def 					__lowerCamelCase						(  self					):
     ((__UpperCAmelCase)					,    (__UpperCAmelCase))					=   heapq.heappop(self.elements					)
     self.set.remove(__A					)
     return (priority, item)







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase    )->     int:
 # euclidean distance
 __UpperCAmelCase					=   np.array(_lowerCAmelCase    )
 __UpperCAmelCase					=   np.array(_lowerCAmelCase    )
 return np.linalg.norm(a - b    )







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase    )->     Union[str, Any]:
 # integer division by time variable
 return consistent_heuristic(_lowerCAmelCase       ,    _lowerCAmelCase    ) // t







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase    )->     List[Any]:
 # manhattan distance
 return abs(p[0] - goal[0]    ) + abs(p[1] - goal[1]    )







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase    )->     List[Any]:
 __UpperCAmelCase					=   g_function[start] + Wa * heuristics[i](_lowerCAmelCase       ,    _lowerCAmelCase    )
 return ans







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase    )->     Union[str, Any]:
 __UpperCAmelCase					=   np.chararray((n, n)    )
 for i in range(_lowerCAmelCase    ):
  for j in range(_lowerCAmelCase    ):
   __UpperCAmelCase					=   '*'

 for i in range(_lowerCAmelCase    ):
  for j in range(_lowerCAmelCase    ):
   if (j, (n - 1) - i) in blocks:
    __UpperCAmelCase					=   '#'

 __UpperCAmelCase					=   '-'
 __UpperCAmelCase					=   back_pointer[goal]
 while x != start:
  ((__UpperCAmelCase)					,    (__UpperCAmelCase))					=   x
  # print(x)
  __UpperCAmelCase					=   '-'
  __UpperCAmelCase					=   back_pointer[x]
 __UpperCAmelCase					=   '-'

 for i in range(_lowerCAmelCase    ):
  for j in range(_lowerCAmelCase    ):
   if (i, j) == (0, n - 1):
    print(grid[i][j]       ,    end=' '    )
    print('<-- End position'       ,    end=' '    )
   else:
    print(grid[i][j]       ,    end=' '    )
  print()
 print('^'    )
 print('Start position'    )
 print()
 print('# is an obstacle'    )
 print('- is the path taken by algorithm'    )
 print('PATH TAKEN BY THE ALGORITHM IS:-'    )
 __UpperCAmelCase					=   back_pointer[goal]
 while x != start:
  print(_lowerCAmelCase       ,    end=' '    )
  __UpperCAmelCase					=   back_pointer[x]
 print(_lowerCAmelCase    )
 sys.exit()







def _lowerCAmelCase     (      _lowerCAmelCase    )->     Optional[int]:
 if p[0] < 0 or p[0] > n - 1:
  return False
 if p[1] < 0 or p[1] > n - 1:
  return False
 return True







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    )->     Tuple:
 for itera in range(_lowerCAmelCase    ):
  open_list[itera].remove_element(_lowerCAmelCase    )
 # print("s", s)
 # print("j", j)
 ((__UpperCAmelCase)					,    (__UpperCAmelCase))					=   s
 __UpperCAmelCase					=   (x - 1, y)
 __UpperCAmelCase					=   (x + 1, y)
 __UpperCAmelCase					=   (x, y + 1)
 __UpperCAmelCase					=   (x, y - 1)

 for neighbours in [left, right, up, down]:
  if neighbours not in blocks:
   if valid(_lowerCAmelCase    ) and neighbours not in visited:
    # print("neighbour", neighbours)
    visited.add(_lowerCAmelCase    )
    __UpperCAmelCase					=   -1
    __UpperCAmelCase					=   float('inf'    )

   if valid(_lowerCAmelCase    ) and g_function[neighbours] > g_function[s] + 1:
    __UpperCAmelCase					=   g_function[s] + 1
    __UpperCAmelCase					=   s
    if neighbours not in close_list_anchor:
     open_list[0].put(_lowerCAmelCase       ,    key(_lowerCAmelCase       ,    0       ,    _lowerCAmelCase       ,    _lowerCAmelCase    )    )
     if neighbours not in close_list_inad:
      for var in range(1       ,    _lowerCAmelCase    ):
       if key(_lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase    ) <= Wa * key(
           _lowerCAmelCase       ,    0       ,    _lowerCAmelCase       ,    _lowerCAmelCase    ):
        open_list[j].put(
            _lowerCAmelCase       ,    key(_lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase    )    )







def _lowerCAmelCase     (      )->     int:
 __UpperCAmelCase					=   []
 for x in range(1       ,    5    ):
  for y in range(1       ,    6    ):
   some_list.append((x, y)    )

 for x in range(15       ,    20    ):
  some_list.append((x, 17)    )

 for x in range(10       ,    19    ):
  for y in range(1       ,    15    ):
   some_list.append((x, y)    )

    # L block
 for x in range(1       ,    4    ):
  for y in range(12       ,    19    ):
   some_list.append((x, y)    )
 for x in range(3       ,    13    ):
  for y in range(16       ,    19    ):
   some_list.append((x, y)    )
 return some_list


_A:					Dict							=				{0: consistent_heuristic, 1: heuristic_a, 2: heuristic_a}

_A:					Optional[int]							=				[
    (0, 1),
    (1, 1),
    (2, 1),
    (3, 1),
    (4, 1),
    (5, 1),
    (6, 1),
    (7, 1),
    (8, 1),
    (9, 1),
    (10, 1),
    (11, 1),
    (12, 1),
    (13, 1),
    (14, 1),
    (15, 1),
    (16, 1),
    (17, 1),
    (18, 1),
    (19, 1),
]
_A:					List[Any]							=				make_common_ground()


_A:					List[str]							=				blocks_blk
# hyper parameters
_A:					Optional[Any]							=				1
_A:					str							=				1
_A:					Optional[Any]							=				20
_A:					Dict							=				3  # one consistent and two other inconsistent

# start and end destination
_A:					int							=				(0, 0)
_A:					List[Any]							=				(n - 1, n - 1)

_A:					str							=				1







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase    )->     Any:
 __UpperCAmelCase					=   {start: 0, goal: float('inf'    )}
 __UpperCAmelCase					=   {start: -1, goal: -1}
 __UpperCAmelCase					=   []
 __UpperCAmelCase					=   set()

 for i in range(_lowerCAmelCase    ):
  open_list.append(PriorityQueue()    )
  open_list[i].put(_lowerCAmelCase       ,    key(_lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase    )    )

 __UpperCAmelCase					=   []
 __UpperCAmelCase					=   []
 while open_list[0].minkey() < float('inf'    ):
  for i in range(1       ,    _lowerCAmelCase    ):
   # print(open_list[0].minkey(), open_list[i].minkey())
   if open_list[i].minkey() <= Wa * open_list[0].minkey():
    global t
    t += 1
    if g_function[goal] <= open_list[i].minkey():
     if g_function[goal] < float('inf'    ):
      do_something(_lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase    )
    else:
     __UpperCAmelCase					,    __UpperCAmelCase					=   open_list[i].top_show()
     visited.add(_lowerCAmelCase    )
     expand_state(
         _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    )
     close_list_inad.append(_lowerCAmelCase    )
   else:
    if g_function[goal] <= open_list[0].minkey():
     if g_function[goal] < float('inf'    ):
      do_something(_lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase    )
    else:
     __UpperCAmelCase					=   open_list[0].top_show()
     visited.add(_lowerCAmelCase    )
     expand_state(
         _lowerCAmelCase       ,    0       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    )
     close_list_anchor.append(_lowerCAmelCase    )
 print('No path found to goal'    )
 print()
 for i in range(n - 1       ,    -1       ,    -1    ):
  for j in range(_lowerCAmelCase    ):
   if (j, i) in blocks:
    print('#'       ,    end=' '    )
   elif (j, i) in back_pointer:
    if (j, i) == (n - 1, n - 1):
     print('*'       ,    end=' '    )
    else:
     print('-'       ,    end=' '    )
   else:
    print('*'       ,    end=' '    )
   if (j, i) == (n - 1, n - 1):
    print('<-- End position'       ,    end=' '    )
  print()
 print('^'    )
 print('Start position'    )
 print()
 print('# is an obstacle'    )
 print('- is the path taken by algorithm'    )


if __name__ == "__main__":
    multi_a_star(start, goal, n_heuristic)












'''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     ,			):
     __UpperCAmelCase					=   parent
     __UpperCAmelCase					=   batch_size
     __UpperCAmelCase					=   seq_length
     __UpperCAmelCase					=   is_training
     __UpperCAmelCase					=   use_input_mask
     __UpperCAmelCase					=   use_token_type_ids
     __UpperCAmelCase					=   use_labels
     __UpperCAmelCase					=   vocab_size
     __UpperCAmelCase					=   hidden_size
     __UpperCAmelCase					=   embedding_size
     __UpperCAmelCase					=   num_hidden_layers
     __UpperCAmelCase					=   num_attention_heads
     __UpperCAmelCase					=   intermediate_size
     __UpperCAmelCase					=   hidden_act
     __UpperCAmelCase					=   hidden_dropout_prob
     __UpperCAmelCase					=   attention_probs_dropout_prob
     __UpperCAmelCase					=   max_position_embeddings
     __UpperCAmelCase					=   type_vocab_size
     __UpperCAmelCase					=   type_sequence_label_size
     __UpperCAmelCase					=   initializer_range
     __UpperCAmelCase					=   num_labels
     __UpperCAmelCase					=   num_choices
     __UpperCAmelCase					=   scope



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   ids_tensor([self.batch_size, self.seq_length]     ,			self.vocab_size					)

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

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

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

     __UpperCAmelCase					=   self.get_config()

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



    def 					__lowerCamelCase						(  self					):
     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 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   MegatronBertModel(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   model(__A     ,			attention_mask=__A     ,			token_type_ids=__A					)
     __UpperCAmelCase					=   model(__A     ,			token_type_ids=__A					)
     __UpperCAmelCase					=   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 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   MegatronBertForMaskedLM(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   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 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   MegatronBertForCausalLM(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   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 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   MegatronBertForNextSentencePrediction(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   model(
         __A     ,			attention_mask=__A     ,			token_type_ids=__A     ,			labels=__A     ,			)
     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, 2)					)



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   MegatronBertForPreTraining(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   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 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   MegatronBertForQuestionAnswering(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   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 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   self.num_labels
     __UpperCAmelCase					=   MegatronBertForSequenceClassification(__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   model(__A     ,			attention_mask=__A     ,			token_type_ids=__A     ,			labels=__A					)
     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.num_labels)					)



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   self.num_labels
     __UpperCAmelCase					=   MegatronBertForTokenClassification(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   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 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A     ,			__A					):
     __UpperCAmelCase					=   self.num_choices
     __UpperCAmelCase					=   MegatronBertForMultipleChoice(config=__A					)
     model.to(__A					)
     model.eval()
     __UpperCAmelCase					=   input_ids.unsqueeze(1					).expand(-1     ,			self.num_choices     ,			-1					).contiguous()
     __UpperCAmelCase					=   token_type_ids.unsqueeze(1					).expand(-1     ,			self.num_choices     ,			-1					).contiguous()
     __UpperCAmelCase					=   input_mask.unsqueeze(1					).expand(-1     ,			self.num_choices     ,			-1					).contiguous()
     __UpperCAmelCase					=   model(
         __A     ,			attention_mask=__A     ,			token_type_ids=__A     ,			labels=__A     ,			)
     self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.num_choices)					)




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






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



    def 					__lowerCamelCase						(  self     ,			__A     ,			__A     ,			__A=False					):
     __UpperCAmelCase					=   super()._prepare_for_class(__A     ,			__A     ,			return_labels=__A					)

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



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   MegatronBertModelTester(self					)
     __UpperCAmelCase					=   ConfigTester(self     ,			config_class=__A     ,			hidden_size=37					)



    def 					__lowerCamelCase						(  self					):
     self.config_tester.run_common_tests()



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_model(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_masked_lm(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_multiple_choice(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_next_sequence_prediction(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_pretraining(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_question_answering(*__A					)



    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_sequence_classification(*__A					)




    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_megatron_bert_for_token_classification(*__A					)







def _lowerCAmelCase     (      _lowerCAmelCase    )->     Dict:
 return torch.tensor(
     _lowerCAmelCase       ,    dtype=torch.long       ,    device=_lowerCAmelCase       ,    )


_A:					Any							=				1E-4






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



    @slow
    @unittest.skip('Model is not available.'					)
    def 					__lowerCamelCase						(  self					):
     __UpperCAmelCase					=   'nvidia/megatron-bert-uncased-345m'
     if "MYDIR" in os.environ:
      __UpperCAmelCase					=   os.path.join(os.environ['MYDIR']     ,			__A					)
     __UpperCAmelCase					=   MegatronBertModel.from_pretrained(__A					)
     model.to(__A					)
     model.half()
     __UpperCAmelCase					=   _long_tensor([[101, 7_110, 1_005, 1_056, 2_023, 11_333, 17_413, 1_029, 102]]					)
     with torch.no_grad():
      __UpperCAmelCase					=   model(__A					)[0]
     __UpperCAmelCase					=   torch.Size((1, 9, 1_024)					)
     self.assertEqual(output.shape     ,			__A					)

     __UpperCAmelCase					=   [-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					=   output[0, ii, jj]
       __UpperCAmelCase					=   expected[3 * ii + jj]
       __UpperCAmelCase					=   'ii={} jj={} a={} b={}'.format(__A     ,			__A     ,			__A     ,			__A					)
       self.assertTrue(math.isclose(__A     ,			__A     ,			rel_tol=__A     ,			abs_tol=__A					)     ,			msg=__A					)





