

import unittest

from transformers import YosoConfig, 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
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch

  from transformers import (
      YosoForMaskedLM,
      YosoForMultipleChoice,
      YosoForQuestionAnswering,
      YosoForSequenceClassification,
      YosoForTokenClassification,
      YosoModel,
  )
  from transformers.models.yoso.modeling_yoso import YOSO_PRETRAINED_MODEL_ARCHIVE_LIST





class A__     :



 """simple docstring"""







 def __init__(		self			:						Optional[Any]  ,    A_			:						List[str]  ,    A_			:						Tuple=1_3  ,    A_			:						Optional[Any]=7  ,    A_			:						List[Any]=True  ,    A_			:						Optional[int]=True  ,    A_			:						int=True  ,    A_			:						Dict=True  ,    A_			:						int=9_9  ,    A_			:						Any=3_2  ,    A_			:						int=5  ,    A_			:						Dict=4  ,    A_			:						List[str]=3_7  ,    A_			:						Any="gelu"  ,    A_			:						int=0.1  ,    A_			:						List[Any]=0.1  ,    A_			:						int=5_1_2  ,    A_			:						Optional[int]=1_6  ,    A_			:						Union[str, Any]=2  ,    A_			:						List[Any]=0.02  ,    A_			:						Optional[int]=3  ,    A_			:						Any=4  ,    A_			:						Tuple=None  ,    ):






      '''simple docstring'''




      _lowerCAmelCase :       Optional[Any]	   =     parent
      _lowerCAmelCase :       Tuple	   =     batch_size
      _lowerCAmelCase :       Tuple	   =     seq_length
      _lowerCAmelCase :       Optional[int]	   =     is_training
      _lowerCAmelCase :       List[Any]	   =     use_input_mask
      _lowerCAmelCase :       List[str]	   =     use_token_type_ids
      _lowerCAmelCase :       Optional[Any]	   =     use_labels
      _lowerCAmelCase :       Optional[Any]	   =     vocab_size
      _lowerCAmelCase :       List[Any]	   =     hidden_size
      _lowerCAmelCase :       int	   =     num_hidden_layers
      _lowerCAmelCase :       Tuple	   =     num_attention_heads
      _lowerCAmelCase :       Optional[int]	   =     intermediate_size
      _lowerCAmelCase :       Union[str, Any]	   =     hidden_act
      _lowerCAmelCase :       Any	   =     hidden_dropout_prob
      _lowerCAmelCase :       Dict	   =     attention_probs_dropout_prob
      _lowerCAmelCase :       List[str]	   =     max_position_embeddings
      _lowerCAmelCase :       List[Any]	   =     type_vocab_size
      _lowerCAmelCase :       Union[str, Any]	   =     type_sequence_label_size
      _lowerCAmelCase :       str	   =     initializer_range
      _lowerCAmelCase :       List[Any]	   =     num_labels
      _lowerCAmelCase :       Dict	   =     num_choices
      _lowerCAmelCase :       List[str]	   =     scope

 def  __magic_name__							(		self			:						List[Any]							):






      '''simple docstring'''




      _lowerCAmelCase :       Any	   =     ids_tensor([self.batch_size, self.seq_length]  ,    self.vocab_size							)

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

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

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

      _lowerCAmelCase :       Any	   =     self.get_config()

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

 def  __magic_name__							(		self			:						Optional[Any]							):






      '''simple docstring'''




      return YosoConfig(
          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=A_  ,    initializer_range=self.initializer_range  ,    )

 def  __magic_name__							(		self			:						List[Any]							):






      '''simple docstring'''




      _lowerCAmelCase :       Dict	   =     self.get_config()
      _lowerCAmelCase :       Tuple	   =     3_0_0
      return config

 def  __magic_name__							(		self			:						List[str]							):






      '''simple docstring'''




      (
          (
          _lowerCAmelCase
      )   ,      (
          _lowerCAmelCase
      )   ,      (
          _lowerCAmelCase
      )   ,      (
          _lowerCAmelCase
      )   ,      (
          _lowerCAmelCase
      )   ,      (
          _lowerCAmelCase
      )   ,      (
          _lowerCAmelCase
      )   ,      
      ) :       str	   =     self.prepare_config_and_inputs()

      _lowerCAmelCase :       List[str]	   =     True
      _lowerCAmelCase :       str	   =     floats_tensor([self.batch_size, self.seq_length, self.hidden_size]							)
      _lowerCAmelCase :       Dict	   =     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  __magic_name__							(		self			:						Optional[Any]  ,    A_			:						str  ,    A_			:						Union[str, Any]  ,    A_			:						Union[str, Any]  ,    A_			:						List[Any]  ,    A_			:						Any  ,    A_			:						Any  ,    A_			:						int							):






      '''simple docstring'''




      _lowerCAmelCase :       Optional[int]	   =     YosoModel(config=A_							)
      model.to(A_							)
      model.eval()
      _lowerCAmelCase :       Optional[Any]	   =     model(A_  ,    attention_mask=A_  ,    token_type_ids=A_							)
      _lowerCAmelCase :       List[Any]	   =     model(A_  ,    token_type_ids=A_							)
      _lowerCAmelCase :       Union[str, Any]	   =     model(A_							)
      self.parent.assertEqual(result.last_hidden_state.shape  ,    (self.batch_size, self.seq_length, self.hidden_size)							)

 def  __magic_name__							(		self			:						List[str]  ,    A_			:						str  ,    A_			:						int  ,    A_			:						List[Any]  ,    A_			:						Dict  ,    A_			:						Dict  ,    A_			:						Tuple  ,    A_			:						Any  ,    A_			:						str  ,    A_			:						str  ,    ):






      '''simple docstring'''




      _lowerCAmelCase :       str	   =     True
      _lowerCAmelCase :       Union[str, Any]	   =     YosoModel(A_							)
      model.to(A_							)
      model.eval()
      _lowerCAmelCase :       List[str]	   =     model(
          A_  ,    attention_mask=A_  ,    token_type_ids=A_  ,    encoder_hidden_states=A_  ,    encoder_attention_mask=A_  ,    )
      _lowerCAmelCase :       List[str]	   =     model(
          A_  ,    attention_mask=A_  ,    token_type_ids=A_  ,    encoder_hidden_states=A_  ,    )
      _lowerCAmelCase :       Optional[Any]	   =     model(A_  ,    attention_mask=A_  ,    token_type_ids=A_							)
      self.parent.assertEqual(result.last_hidden_state.shape  ,    (self.batch_size, self.seq_length, self.hidden_size)							)

 def  __magic_name__							(		self			:						str  ,    A_			:						List[str]  ,    A_			:						Union[str, Any]  ,    A_			:						List[Any]  ,    A_			:						Optional[Any]  ,    A_			:						int  ,    A_			:						List[str]  ,    A_			:						List[str]							):






      '''simple docstring'''




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






      '''simple docstring'''




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






      '''simple docstring'''




      _lowerCAmelCase :       str	   =     self.num_labels
      _lowerCAmelCase :       Tuple	   =     YosoForSequenceClassification(A_							)
      model.to(A_							)
      model.eval()
      _lowerCAmelCase :       Tuple	   =     model(A_  ,    attention_mask=A_  ,    token_type_ids=A_  ,    labels=A_							)
      self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.num_labels)							)

 def  __magic_name__							(		self			:						List[str]  ,    A_			:						List[str]  ,    A_			:						int  ,    A_			:						Tuple  ,    A_			:						Dict  ,    A_			:						Optional[Any]  ,    A_			:						Optional[int]  ,    A_			:						Any							):






      '''simple docstring'''




      _lowerCAmelCase :       Any	   =     self.num_labels
      _lowerCAmelCase :       Tuple	   =     YosoForTokenClassification(config=A_							)
      model.to(A_							)
      model.eval()
      _lowerCAmelCase :       int	   =     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  __magic_name__							(		self			:						Tuple  ,    A_			:						Optional[Any]  ,    A_			:						Dict  ,    A_			:						List[Any]  ,    A_			:						int  ,    A_			:						Tuple  ,    A_			:						Optional[Any]  ,    A_			:						List[Any]							):






      '''simple docstring'''




      _lowerCAmelCase :       Dict	   =     self.num_choices
      _lowerCAmelCase :       Optional[Any]	   =     YosoForMultipleChoice(config=A_							)
      model.to(A_							)
      model.eval()
      _lowerCAmelCase :       Tuple	   =     input_ids.unsqueeze(1							).expand(-1  ,    self.num_choices  ,    -1							).contiguous()
      _lowerCAmelCase :       Optional[Any]	   =     token_type_ids.unsqueeze(1							).expand(-1  ,    self.num_choices  ,    -1							).contiguous()
      _lowerCAmelCase :       Tuple	   =     input_mask.unsqueeze(1							).expand(-1  ,    self.num_choices  ,    -1							).contiguous()
      _lowerCAmelCase :       List[str]	   =     model(
          A_  ,    attention_mask=A_  ,    token_type_ids=A_  ,    labels=A_  ,    )
      self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.num_choices)							)


 def  __magic_name__							(		self			:						List[Any]							):






      '''simple docstring'''




      _lowerCAmelCase :       Optional[Any]	   =     self.prepare_config_and_inputs()
      (
          (
          _lowerCAmelCase
      )   ,      (
          _lowerCAmelCase
      )   ,      (
          _lowerCAmelCase
      )   ,      (
          _lowerCAmelCase
      )   ,      (
          _lowerCAmelCase
      )   ,      (
          _lowerCAmelCase
      )   ,      (
          _lowerCAmelCase
      )   ,      
      ) :       Any	   =     config_and_inputs
      _lowerCAmelCase :       Optional[Any]	   =     {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
      return config, inputs_dict





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



 """simple docstring"""







 _lowercase  :       Union[str, Any]  			=    (
     (
         YosoModel,
         YosoForMaskedLM,
         YosoForMultipleChoice,
         YosoForQuestionAnswering,
         YosoForSequenceClassification,
         YosoForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 _lowercase  :       List[str]  			=    False
 _lowercase  :       int  			=    False
 _lowercase  :       int  			=    False

 _lowercase  :       str  			=    ()
 _lowercase  :       List[str]  			=    (
     {
         '''feature-extraction''': YosoModel,
         '''fill-mask''': YosoForMaskedLM,
         '''question-answering''': YosoForQuestionAnswering,
         '''text-classification''': YosoForSequenceClassification,
         '''token-classification''': YosoForTokenClassification,
         '''zero-shot''': YosoForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )

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






      '''simple docstring'''




      _lowerCAmelCase :       str	   =     YosoModelTester(self							)
      _lowerCAmelCase :       Union[str, Any]	   =     ConfigTester(self  ,    config_class=A_  ,    hidden_size=3_7							)

 def  __magic_name__							(		self			:						Optional[Any]							):






      '''simple docstring'''




      self.config_tester.run_common_tests()

 def  __magic_name__							(		self			:						Optional[int]							):






      '''simple docstring'''




      _lowerCAmelCase :       Tuple	   =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*A_							)

 def  __magic_name__							(		self			:						str							):






      '''simple docstring'''




      _lowerCAmelCase :       Tuple	   =     self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
           _lowerCAmelCase :       Any	   =     type
           self.model_tester.create_and_check_model(*A_							)

 def  __magic_name__							(		self			:						str							):






      '''simple docstring'''




      _lowerCAmelCase :       List[Any]	   =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*A_							)

 def  __magic_name__							(		self			:						List[Any]							):






      '''simple docstring'''




      _lowerCAmelCase :       Any	   =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_multiple_choice(*A_							)

 def  __magic_name__							(		self			:						Dict							):






      '''simple docstring'''




      _lowerCAmelCase :       List[str]	   =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*A_							)

 def  __magic_name__							(		self			:						int							):






      '''simple docstring'''




      _lowerCAmelCase :       Dict	   =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*A_							)

 def  __magic_name__							(		self			:						List[Any]							):






      '''simple docstring'''




      _lowerCAmelCase :       List[str]	   =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*A_							)

 @slow
 def  __magic_name__							(		self			:						int							):






      '''simple docstring'''




      for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _lowerCAmelCase :       Tuple	   =     YosoModel.from_pretrained(A_							)
           self.assertIsNotNone(A_							)


 def  __magic_name__							(		self			:						Optional[int]							):






      '''simple docstring'''




      return





@require_torch
class A__     (			unittest.TestCase			):



 """simple docstring"""







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






      '''simple docstring'''




      _lowerCAmelCase :       Union[str, Any]	   =     YosoModel.from_pretrained("uw-madison/yoso-4096"							)
      _lowerCAmelCase :       str	   =     torch.tensor([[0, 1, 2, 3, 4, 5]]							)

      with torch.no_grad():
           _lowerCAmelCase :       List[str]	   =     model(A_							)[0]

      _lowerCAmelCase :       str	   =     torch.Size((1, 6, 7_6_8)							)
      self.assertEqual(output.shape  ,    A_							)

      _lowerCAmelCase :       Union[str, Any]	   =     torch.tensor(
          [[[-0.0611, 0.1242, 0.0840], [0.0280, -0.0048, 0.1125], [0.0106, 0.0226, 0.0751]]]							)

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

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






      '''simple docstring'''




      _lowerCAmelCase :       Any	   =     YosoForMaskedLM.from_pretrained("uw-madison/yoso-4096"							)
      _lowerCAmelCase :       Optional[int]	   =     torch.tensor([[0, 1, 2, 3, 4, 5]]							)

      with torch.no_grad():
           _lowerCAmelCase :       Union[str, Any]	   =     model(A_							)[0]

      _lowerCAmelCase :       Optional[Any]	   =     5_0_2_6_5

      _lowerCAmelCase :       str	   =     torch.Size((1, 6, vocab_size)							)
      self.assertEqual(output.shape  ,    A_							)

      _lowerCAmelCase :       str	   =     torch.tensor(
          [[[-2.1313, -3.7285, -2.2407], [-2.7047, -3.3314, -2.6408], [0.0629, -2.5166, -0.3356]]]							)

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


 @slow
 def  __magic_name__							(		self			:						Tuple							):






      '''simple docstring'''




      _lowerCAmelCase :       str	   =     YosoForMaskedLM.from_pretrained("uw-madison/yoso-4096"							)
      _lowerCAmelCase :       List[Any]	   =     torch.arange(4_0_9_6							).unsqueeze(0							)

      with torch.no_grad():
           _lowerCAmelCase :       Dict	   =     model(A_							)[0]

      _lowerCAmelCase :       Tuple	   =     5_0_2_6_5

      _lowerCAmelCase :       Union[str, Any]	   =     torch.Size((1, 4_0_9_6, vocab_size)							)
      self.assertEqual(output.shape  ,    A_							)

      _lowerCAmelCase :       List[str]	   =     torch.tensor(
          [[[-2.3914, -4.3742, -5.0956], [-4.0988, -4.2384, -7.0406], [-3.1427, -3.7192, -6.6800]]]							)

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









def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   Optional[int]:

     """simple docstring"""







     if power < 0:
          return -1
     base %= mod
     _lowerCAmelCase :       Tuple	   =     1

     while power > 0:
          if power & 1:
               _lowerCAmelCase :       Optional[Any]	   =     (result * base) % mod
          _lowerCAmelCase :       int	   =     power >> 1
          _lowerCAmelCase :       Optional[int]	   =     (base * base) % mod

     return result







def 						_snake_case		(     )							->   Optional[int]:

     """simple docstring"""







     print(modular_exponential(3		,				200		,				13					)					)


if __name__ == "__main__":
  import doctest

  doctest.testmod()

  main()






