


import unittest

from transformers import MraConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_torch_available():
    import torch

    from transformers import (
        MraForMaskedLM,
        MraForMultipleChoice,
        MraForQuestionAnswering,
        MraForSequenceClassification,
        MraForTokenClassification,
        MraModel,
    )
    from transformers.models.mra.modeling_mra import MRA_PRETRAINED_MODEL_ARCHIVE_LIST


class     lowercase__  :






  def __init__(					self      :					int		,						_lowercase      :					Optional[int]		,						_lowercase      :					List[str]=2		,						_lowercase      :					Any=8		,						_lowercase      :					List[str]=True		,						_lowercase      :					Union[str, Any]=True		,						_lowercase      :					Optional[int]=True		,						_lowercase      :					Union[str, Any]=True		,						_lowercase      :					Optional[int]=99		,						_lowercase      :					str=16		,						_lowercase      :					List[Any]=5		,						_lowercase      :					str=2		,						_lowercase      :					str=36		,						_lowercase      :					Optional[Any]="gelu"		,						_lowercase      :					List[Any]=0.0		,						_lowercase      :					Union[str, Any]=0.0		,						_lowercase      :					Any=5_12		,						_lowercase      :					Optional[Any]=16		,						_lowercase      :					Any=2		,						_lowercase      :					Dict=0.0_2		,						_lowercase      :					str=3		,						_lowercase      :					str=4		,						_lowercase      :					Any=None		,						):




         """simple docstring"""







         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__										=     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 			_UpperCAmelCase  (					self      :					Dict ):




         """simple docstring"""







         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 			_UpperCAmelCase  (					self      :					List[str] ):




         """simple docstring"""







         return MraConfig(
             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		,						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 			_UpperCAmelCase  (					self      :					Optional[Any] ):




         """simple docstring"""







         UpperCAmelCase__										=     self.get_config()
         UpperCAmelCase__										=     3_00
         return config






  def 			_UpperCAmelCase  (					self      :					List[Any] ):




         """simple docstring"""







         (
             (
             UpperCAmelCase__
         )						,			(
             UpperCAmelCase__
         )						,			(
             UpperCAmelCase__
         )						,			(
             UpperCAmelCase__
         )						,			(
             UpperCAmelCase__
         )						,			(
             UpperCAmelCase__
         )						,			(
             UpperCAmelCase__
         )						,			
         )										=     self.prepare_config_and_inputs()

         UpperCAmelCase__										=     True
         UpperCAmelCase__										=     floats_tensor([self.batch_size, self.seq_length, self.hidden_size] )
         UpperCAmelCase__										=     ids_tensor([self.batch_size, self.seq_length]		,						vocab_size=2 )

         return (
             config,
             input_ids,
             token_type_ids,
             input_mask,
             sequence_labels,
             token_labels,
             choice_labels,
             encoder_hidden_states,
             encoder_attention_mask,
         )






  def 			_UpperCAmelCase  (					self      :					str		,						_lowercase      :					List[str]		,						_lowercase      :					Optional[Any]		,						_lowercase      :					Any		,						_lowercase      :					str		,						_lowercase      :					Dict		,						_lowercase      :					str		,						_lowercase      :					Optional[Any] ):




         """simple docstring"""







         UpperCAmelCase__										=     MraModel(config=_lowercase )
         model.to(_lowercase )
         model.eval()
         UpperCAmelCase__										=     model(_lowercase		,						attention_mask=_lowercase		,						token_type_ids=_lowercase )
         UpperCAmelCase__										=     model(_lowercase		,						token_type_ids=_lowercase )
         UpperCAmelCase__										=     model(_lowercase )
         self.parent.assertEqual(result.last_hidden_state.shape		,						(self.batch_size, self.seq_length, self.hidden_size) )






  def 			_UpperCAmelCase  (					self      :					List[str]		,						_lowercase      :					str		,						_lowercase      :					Tuple		,						_lowercase      :					List[str]		,						_lowercase      :					Any		,						_lowercase      :					Union[str, Any]		,						_lowercase      :					Optional[int]		,						_lowercase      :					Any		,						_lowercase      :					List[str]		,						_lowercase      :					List[Any]		,						):




         """simple docstring"""







         UpperCAmelCase__										=     True
         UpperCAmelCase__										=     MraModel(_lowercase )
         model.to(_lowercase )
         model.eval()
         UpperCAmelCase__										=     model(
             _lowercase		,						attention_mask=_lowercase		,						token_type_ids=_lowercase		,						encoder_hidden_states=_lowercase		,						encoder_attention_mask=_lowercase		,						)
         UpperCAmelCase__										=     model(
             _lowercase		,						attention_mask=_lowercase		,						token_type_ids=_lowercase		,						encoder_hidden_states=_lowercase		,						)
         UpperCAmelCase__										=     model(_lowercase		,						attention_mask=_lowercase		,						token_type_ids=_lowercase )
         self.parent.assertEqual(result.last_hidden_state.shape		,						(self.batch_size, self.seq_length, self.hidden_size) )






  def 			_UpperCAmelCase  (					self      :					Dict		,						_lowercase      :					Union[str, Any]		,						_lowercase      :					Optional[Any]		,						_lowercase      :					Any		,						_lowercase      :					Tuple		,						_lowercase      :					List[str]		,						_lowercase      :					str		,						_lowercase      :					Optional[Any] ):




         """simple docstring"""







         UpperCAmelCase__										=     MraForMaskedLM(config=_lowercase )
         model.to(_lowercase )
         model.eval()
         UpperCAmelCase__										=     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 			_UpperCAmelCase  (					self      :					List[str]		,						_lowercase      :					Union[str, Any]		,						_lowercase      :					str		,						_lowercase      :					Any		,						_lowercase      :					List[Any]		,						_lowercase      :					List[str]		,						_lowercase      :					str		,						_lowercase      :					int ):




         """simple docstring"""







         UpperCAmelCase__										=     MraForQuestionAnswering(config=_lowercase )
         model.to(_lowercase )
         model.eval()
         UpperCAmelCase__										=     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 			_UpperCAmelCase  (					self      :					List[Any]		,						_lowercase      :					Optional[int]		,						_lowercase      :					str		,						_lowercase      :					Dict		,						_lowercase      :					Optional[Any]		,						_lowercase      :					List[str]		,						_lowercase      :					Union[str, Any]		,						_lowercase      :					Any ):




         """simple docstring"""







         UpperCAmelCase__										=     self.num_labels
         UpperCAmelCase__										=     MraForSequenceClassification(_lowercase )
         model.to(_lowercase )
         model.eval()
         UpperCAmelCase__										=     model(_lowercase		,						attention_mask=_lowercase		,						token_type_ids=_lowercase		,						labels=_lowercase )
         self.parent.assertEqual(result.logits.shape		,						(self.batch_size, self.num_labels) )






  def 			_UpperCAmelCase  (					self      :					Optional[int]		,						_lowercase      :					Dict		,						_lowercase      :					List[str]		,						_lowercase      :					List[Any]		,						_lowercase      :					Union[str, Any]		,						_lowercase      :					Any		,						_lowercase      :					List[Any]		,						_lowercase      :					List[Any] ):




         """simple docstring"""







         UpperCAmelCase__										=     self.num_labels
         UpperCAmelCase__										=     MraForTokenClassification(config=_lowercase )
         model.to(_lowercase )
         model.eval()
         UpperCAmelCase__										=     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 			_UpperCAmelCase  (					self      :					Union[str, Any]		,						_lowercase      :					Union[str, Any]		,						_lowercase      :					Dict		,						_lowercase      :					List[Any]		,						_lowercase      :					List[Any]		,						_lowercase      :					int		,						_lowercase      :					str		,						_lowercase      :					List[Any] ):




         """simple docstring"""







         UpperCAmelCase__										=     self.num_choices
         UpperCAmelCase__										=     MraForMultipleChoice(config=_lowercase )
         model.to(_lowercase )
         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(
             _lowercase		,						attention_mask=_lowercase		,						token_type_ids=_lowercase		,						labels=_lowercase		,						)
         self.parent.assertEqual(result.logits.shape		,						(self.batch_size, self.num_choices) )






  def 			_UpperCAmelCase  (					self      :					List[str] ):




         """simple docstring"""







         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     lowercase__  (					__SCREAMING_SNAKE_CASE					,							unittest.TestCase		):
  A__=  (
      (
          MraModel,
          MraForMaskedLM,
          MraForMultipleChoice,
          MraForQuestionAnswering,
          MraForSequenceClassification,
          MraForTokenClassification,
      )
      if is_torch_available()
      else ()
  )
  A__=  False
  A__=  False
  A__=  False
  A__=  False

  A__=  ()






  def 			_UpperCAmelCase  (					self      :					Union[str, Any] ):




         """simple docstring"""







         UpperCAmelCase__										=     MraModelTester(self )
         UpperCAmelCase__										=     ConfigTester(self		,						config_class=_lowercase		,						hidden_size=37 )






  def 			_UpperCAmelCase  (					self      :					Dict ):




         """simple docstring"""







         self.config_tester.run_common_tests()






  def 			_UpperCAmelCase  (					self      :					Optional[Any] ):




         """simple docstring"""







         UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_lowercase )






  def 			_UpperCAmelCase  (					self      :					Optional[int] ):




         """simple docstring"""







         UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
                UpperCAmelCase__										=     type
                self.model_tester.create_and_check_model(*_lowercase )






  def 			_UpperCAmelCase  (					self      :					Tuple ):




         """simple docstring"""







         UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*_lowercase )






  def 			_UpperCAmelCase  (					self      :					Tuple ):




         """simple docstring"""







         UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_multiple_choice(*_lowercase )






  def 			_UpperCAmelCase  (					self      :					List[Any] ):




         """simple docstring"""







         UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*_lowercase )






  def 			_UpperCAmelCase  (					self      :					Tuple ):




         """simple docstring"""







         UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*_lowercase )






  def 			_UpperCAmelCase  (					self      :					str ):




         """simple docstring"""







         UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*_lowercase )






  @slow
  def 			_UpperCAmelCase  (					self      :					Union[str, Any] ):




         """simple docstring"""







         for model_name in MRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCAmelCase__										=     MraModel.from_pretrained(_lowercase )
                self.assertIsNotNone(_lowercase )






  @unittest.skip(reason="MRA does not output attentions" )
  def 			_UpperCAmelCase  (					self      :					Dict ):




         """simple docstring"""







         return




@require_torch
class     lowercase__  (					unittest.TestCase		):






  @slow
  def 			_UpperCAmelCase  (					self      :					int ):




         """simple docstring"""







         UpperCAmelCase__										=     MraModel.from_pretrained("uw-madison/mra-base-512-4" )
         UpperCAmelCase__										=     torch.arange(2_56 ).unsqueeze(0 )

         with torch.no_grad():
                UpperCAmelCase__										=     model(_lowercase )[0]

         UpperCAmelCase__										=     torch.Size((1, 2_56, 7_68) )
         self.assertEqual(output.shape		,						_lowercase )

         UpperCAmelCase__										=     torch.tensor(
             [[[-0.0_1_4_0, 0.0_8_3_0, -0.0_3_8_1], [0.1_5_4_6, 0.1_4_0_2, 0.0_2_2_0], [0.1_1_6_2, 0.0_8_5_1, 0.0_1_6_5]]] )

         self.assertTrue(torch.allclose(output[:, :3, :3]		,						_lowercase		,						atol=1E-4 ) )






  @slow
  def 			_UpperCAmelCase  (					self      :					Union[str, Any] ):




         """simple docstring"""







         UpperCAmelCase__										=     MraForMaskedLM.from_pretrained("uw-madison/mra-base-512-4" )
         UpperCAmelCase__										=     torch.arange(2_56 ).unsqueeze(0 )

         with torch.no_grad():
                UpperCAmelCase__										=     model(_lowercase )[0]

         UpperCAmelCase__										=     5_02_65

         UpperCAmelCase__										=     torch.Size((1, 2_56, vocab_size) )
         self.assertEqual(output.shape		,						_lowercase )

         UpperCAmelCase__										=     torch.tensor(
             [[[9.2_5_9_5, -3.6_0_3_8, 1_1.8_8_1_9], [9.3_8_6_9, -3.2_6_9_3, 1_1.0_9_5_6], [1_1.8_5_2_4, -3.4_9_3_8, 1_3.1_2_1_0]]] )

         self.assertTrue(torch.allclose(output[:, :3, :3]		,						_lowercase		,						atol=1E-4 ) )






  @slow
  def 			_UpperCAmelCase  (					self      :					List[Any] ):




         """simple docstring"""







         UpperCAmelCase__										=     MraForMaskedLM.from_pretrained("uw-madison/mra-base-4096-8-d3" )
         UpperCAmelCase__										=     torch.arange(40_96 ).unsqueeze(0 )

         with torch.no_grad():
                UpperCAmelCase__										=     model(_lowercase )[0]

         UpperCAmelCase__										=     5_02_65

         UpperCAmelCase__										=     torch.Size((1, 40_96, vocab_size) )
         self.assertEqual(output.shape		,						_lowercase )

         UpperCAmelCase__										=     torch.tensor(
             [[[5.4_7_8_9, -2.3_5_6_4, 7.5_0_6_4], [7.9_0_6_7, -1.3_3_6_9, 9.9_6_6_8], [9.0_7_1_2, -1.8_1_0_6, 7.0_3_8_0]]] )

         self.assertTrue(torch.allclose(output[:, :3, :3]		,						_lowercase		,						atol=1E-4 ) )









from math import atan, cos, radians, sin, tan

from .haversine_distance import haversine_distance

A      = 6378137.0
A      = 6356752.314245
A      = 637_8137





def __UpperCAmelCase       (		__A ,						__A ,						__A ,						__A			)						->					float:



       '''simple docstring'''





       UpperCAmelCase__										=     (AXIS_A - AXIS_B) / AXIS_A
       # Parametric latitudes
       # https://en.wikipedia.org/wiki/Latitude#Parametric_(or_reduced)_latitude
       UpperCAmelCase__										=     atan((1 - flattening) * tan(radians(__A			)			)			)
       UpperCAmelCase__										=     atan((1 - flattening) * tan(radians(__A			)			)			)

       # Compute central angle between two points
       # using haversine theta. sigma =  haversine_distance / equatorial radius
       UpperCAmelCase__										=     haversine_distance(__A ,						__A ,						__A ,						__A			) / EQUATORIAL_RADIUS

       # Intermediate P and Q values
       UpperCAmelCase__										=     (b_lata + b_lata) / 2
       UpperCAmelCase__										=     (b_lata - b_lata) / 2

       # Intermediate X value
       # X = (sigma - sin(sigma)) * sin^2Pcos^2Q / cos^2(sigma/2)
       UpperCAmelCase__										=     (sin(__A			) ** 2) * (cos(__A			) ** 2)
       UpperCAmelCase__										=     cos(sigma / 2			) ** 2
       UpperCAmelCase__										=     (sigma - sin(__A			)) * (x_numerator / x_demonimator)

       # Intermediate Y value
       # Y = (sigma + sin(sigma)) * cos^2Psin^2Q / sin^2(sigma/2)
       UpperCAmelCase__										=     (cos(__A			) ** 2) * (sin(__A			) ** 2)
       UpperCAmelCase__										=     sin(sigma / 2			) ** 2
       UpperCAmelCase__										=     (sigma + sin(__A			)) * (y_numerator / y_denominator)

       return EQUATORIAL_RADIUS * (sigma - ((flattening / 2) * (x_value + y_value)))


if __name__ == "__main__":
    import doctest

    doctest.testmod()





