





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

  """simple docstring"""







  def __init__( self  ,    UpperCAmelCase  ,    UpperCAmelCase=13  ,    UpperCAmelCase=7  ,    UpperCAmelCase=True  ,    UpperCAmelCase=True  ,    UpperCAmelCase=True  ,    UpperCAmelCase=True  ,    UpperCAmelCase=99  ,    UpperCAmelCase=32  ,    UpperCAmelCase=5  ,    UpperCAmelCase=4  ,    UpperCAmelCase=37  ,    UpperCAmelCase="gelu"  ,    UpperCAmelCase=0.1  ,    UpperCAmelCase=0.1  ,    UpperCAmelCase=5_12  ,    UpperCAmelCase=16  ,    UpperCAmelCase=2  ,    UpperCAmelCase=0.02  ,    UpperCAmelCase=3  ,    UpperCAmelCase=4  ,    UpperCAmelCase=None  ,    )-> Optional[Any]:
     __A				 =       parent
     __A				 =       batch_size
     __A				 =       seq_length
     __A				 =       is_training
     __A				 =       use_input_mask
     __A				 =       use_token_type_ids
     __A				 =       use_labels
     __A				 =       vocab_size
     __A				 =       hidden_size
     __A				 =       num_hidden_layers
     __A				 =       num_attention_heads
     __A				 =       intermediate_size
     __A				 =       hidden_act
     __A				 =       hidden_dropout_prob
     __A				 =       attention_probs_dropout_prob
     __A				 =       max_position_embeddings
     __A				 =       type_vocab_size
     __A				 =       type_sequence_label_size
     __A				 =       initializer_range
     __A				 =       num_labels
     __A				 =       num_choices
     __A				 =       scope

  def 		SCREAMING_SNAKE_CASE__	( self     )-> Dict:
     __A				 =       ids_tensor([self.batch_size, self.seq_length]  ,    self.vocab_size     )

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

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

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

     __A				 =       self.get_config()

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

  def 		SCREAMING_SNAKE_CASE__	( self     )-> Dict:
     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=UpperCAmelCase  ,    initializer_range=self.initializer_range  ,    )

  def 		SCREAMING_SNAKE_CASE__	( self     )-> Tuple:
     __A				 =       self.get_config()
     __A				 =       3_00
     return config

  def 		SCREAMING_SNAKE_CASE__	( self     )-> List[str]:
     (
         (
         __A
     ) ,						(
         __A
     ) ,						(
         __A
     ) ,						(
         __A
     ) ,						(
         __A
     ) ,						(
         __A
     ) ,						(
         __A
     ) ,						
     )				 =       self.prepare_config_and_inputs()

     __A				 =       True
     __A				 =       floats_tensor([self.batch_size, self.seq_length, self.hidden_size]     )
     __A				 =       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 		SCREAMING_SNAKE_CASE__	( self  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase     )-> int:
     __A				 =       YosoModel(config=UpperCAmelCase     )
     model.to(UpperCAmelCase     )
     model.eval()
     __A				 =       model(UpperCAmelCase  ,    attention_mask=UpperCAmelCase  ,    token_type_ids=UpperCAmelCase     )
     __A				 =       model(UpperCAmelCase  ,    token_type_ids=UpperCAmelCase     )
     __A				 =       model(UpperCAmelCase     )
     self.parent.assertEqual(result.last_hidden_state.shape  ,    (self.batch_size, self.seq_length, self.hidden_size)     )

  def 		SCREAMING_SNAKE_CASE__	( self  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    )-> Union[str, Any]:
     __A				 =       True
     __A				 =       YosoModel(UpperCAmelCase     )
     model.to(UpperCAmelCase     )
     model.eval()
     __A				 =       model(
         UpperCAmelCase  ,    attention_mask=UpperCAmelCase  ,    token_type_ids=UpperCAmelCase  ,    encoder_hidden_states=UpperCAmelCase  ,    encoder_attention_mask=UpperCAmelCase  ,    )
     __A				 =       model(
         UpperCAmelCase  ,    attention_mask=UpperCAmelCase  ,    token_type_ids=UpperCAmelCase  ,    encoder_hidden_states=UpperCAmelCase  ,    )
     __A				 =       model(UpperCAmelCase  ,    attention_mask=UpperCAmelCase  ,    token_type_ids=UpperCAmelCase     )
     self.parent.assertEqual(result.last_hidden_state.shape  ,    (self.batch_size, self.seq_length, self.hidden_size)     )

  def 		SCREAMING_SNAKE_CASE__	( self  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase     )-> List[Any]:
     __A				 =       YosoForMaskedLM(config=UpperCAmelCase     )
     model.to(UpperCAmelCase     )
     model.eval()
     __A				 =       model(UpperCAmelCase  ,    attention_mask=UpperCAmelCase  ,    token_type_ids=UpperCAmelCase  ,    labels=UpperCAmelCase     )
     self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.seq_length, self.vocab_size)     )

  def 		SCREAMING_SNAKE_CASE__	( self  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase     )-> Optional[int]:
     __A				 =       YosoForQuestionAnswering(config=UpperCAmelCase     )
     model.to(UpperCAmelCase     )
     model.eval()
     __A				 =       model(
         UpperCAmelCase  ,    attention_mask=UpperCAmelCase  ,    token_type_ids=UpperCAmelCase  ,    start_positions=UpperCAmelCase  ,    end_positions=UpperCAmelCase  ,    )
     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 		SCREAMING_SNAKE_CASE__	( self  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase     )-> Optional[int]:
     __A				 =       self.num_labels
     __A				 =       YosoForSequenceClassification(UpperCAmelCase     )
     model.to(UpperCAmelCase     )
     model.eval()
     __A				 =       model(UpperCAmelCase  ,    attention_mask=UpperCAmelCase  ,    token_type_ids=UpperCAmelCase  ,    labels=UpperCAmelCase     )
     self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.num_labels)     )

  def 		SCREAMING_SNAKE_CASE__	( self  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase     )-> Dict:
     __A				 =       self.num_labels
     __A				 =       YosoForTokenClassification(config=UpperCAmelCase     )
     model.to(UpperCAmelCase     )
     model.eval()
     __A				 =       model(UpperCAmelCase  ,    attention_mask=UpperCAmelCase  ,    token_type_ids=UpperCAmelCase  ,    labels=UpperCAmelCase     )
     self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.seq_length, self.num_labels)     )

  def 		SCREAMING_SNAKE_CASE__	( self  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase  ,    UpperCAmelCase     )-> Tuple:
     __A				 =       self.num_choices
     __A				 =       YosoForMultipleChoice(config=UpperCAmelCase     )
     model.to(UpperCAmelCase     )
     model.eval()
     __A				 =       input_ids.unsqueeze(1     ).expand(-1  ,    self.num_choices  ,    -1     ).contiguous()
     __A				 =       token_type_ids.unsqueeze(1     ).expand(-1  ,    self.num_choices  ,    -1     ).contiguous()
     __A				 =       input_mask.unsqueeze(1     ).expand(-1  ,    self.num_choices  ,    -1     ).contiguous()
     __A				 =       model(
         UpperCAmelCase  ,    attention_mask=UpperCAmelCase  ,    token_type_ids=UpperCAmelCase  ,    labels=UpperCAmelCase  ,    )
     self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.num_choices)     )





  def 		SCREAMING_SNAKE_CASE__	( self     )-> Union[str, Any]:
     __A				 =       self.prepare_config_and_inputs()
     (
         (
         __A
     ) ,						(
         __A
     ) ,						(
         __A
     ) ,						(
         __A
     ) ,						(
         __A
     ) ,						(
         __A
     ) ,						(
         __A
     ) ,						
     )				 =       config_and_inputs
     __A				 =       {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
     return config, inputs_dict







@require_torch
class 						_lowerCAmelCase(						_a		,   _a		,   unittest.TestCase):

  """simple docstring"""







  lowerCamelCase__      					=   (
      (
          YosoModel,
          YosoForMaskedLM,
          YosoForMultipleChoice,
          YosoForQuestionAnswering,
          YosoForSequenceClassification,
          YosoForTokenClassification,
      )
      if is_torch_available()
      else ()
  )
  lowerCamelCase__      					=   False
  lowerCamelCase__      					=   False
  lowerCamelCase__      					=   False

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

  def 		SCREAMING_SNAKE_CASE__	( self     )-> Union[str, Any]:
     __A				 =       YosoModelTester(self     )
     __A				 =       ConfigTester(self  ,    config_class=UpperCAmelCase  ,    hidden_size=37     )

  def 		SCREAMING_SNAKE_CASE__	( self     )-> int:
     self.config_tester.run_common_tests()

  def 		SCREAMING_SNAKE_CASE__	( self     )-> Optional[Any]:
     __A				 =       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*UpperCAmelCase     )

  def 		SCREAMING_SNAKE_CASE__	( self     )-> Any:
     __A				 =       self.model_tester.prepare_config_and_inputs()
     for type in ["absolute", "relative_key", "relative_key_query"]:
        __A				 =       type
        self.model_tester.create_and_check_model(*UpperCAmelCase     )

  def 		SCREAMING_SNAKE_CASE__	( self     )-> Any:
     __A				 =       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*UpperCAmelCase     )

  def 		SCREAMING_SNAKE_CASE__	( self     )-> int:
     __A				 =       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_multiple_choice(*UpperCAmelCase     )

  def 		SCREAMING_SNAKE_CASE__	( self     )-> int:
     __A				 =       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase     )

  def 		SCREAMING_SNAKE_CASE__	( self     )-> Any:
     __A				 =       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*UpperCAmelCase     )

  def 		SCREAMING_SNAKE_CASE__	( self     )-> List[Any]:
     __A				 =       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase     )

  @slow
  def 		SCREAMING_SNAKE_CASE__	( self     )-> Tuple:
     for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        __A				 =       YosoModel.from_pretrained(UpperCAmelCase     )
        self.assertIsNotNone(UpperCAmelCase     )





  def 		SCREAMING_SNAKE_CASE__	( self     )-> Any:
     return







@require_torch
class 						_lowerCAmelCase(						unittest.TestCase):

  """simple docstring"""







  @slow
  def 		SCREAMING_SNAKE_CASE__	( self     )-> Dict:
     __A				 =       YosoModel.from_pretrained('''uw-madison/yoso-4096'''     )
     __A				 =       torch.tensor([[0, 1, 2, 3, 4, 5]]     )

     with torch.no_grad():
        __A				 =       model(UpperCAmelCase     )[0]

     __A				 =       torch.Size((1, 6, 7_68)     )
     self.assertEqual(output.shape  ,    UpperCAmelCase     )

     __A				 =       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]  ,    UpperCAmelCase  ,    atol=1e-4     )     )

  @slow
  def 		SCREAMING_SNAKE_CASE__	( self     )-> Union[str, Any]:
     __A				 =       YosoForMaskedLM.from_pretrained('''uw-madison/yoso-4096'''     )
     __A				 =       torch.tensor([[0, 1, 2, 3, 4, 5]]     )

     with torch.no_grad():
        __A				 =       model(UpperCAmelCase     )[0]

     __A				 =       5_02_65

     __A				 =       torch.Size((1, 6, vocab_size)     )
     self.assertEqual(output.shape  ,    UpperCAmelCase     )

     __A				 =       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]  ,    UpperCAmelCase  ,    atol=1e-4     )     )





  @slow
  def 		SCREAMING_SNAKE_CASE__	( self     )-> Optional[int]:
     __A				 =       YosoForMaskedLM.from_pretrained('''uw-madison/yoso-4096'''     )
     __A				 =       torch.arange(40_96     ).unsqueeze(0     )

     with torch.no_grad():
        __A				 =       model(UpperCAmelCase     )[0]

     __A				 =       5_02_65

     __A				 =       torch.Size((1, 40_96, vocab_size)     )
     self.assertEqual(output.shape  ,    UpperCAmelCase     )

     __A				 =       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]  ,    UpperCAmelCase  ,    atol=1e-4     )     )








import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin


_UpperCamelCase   :							str     =	"""
Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning.

In March 2021, Hugging Face raised $40 million in a Series B funding round.[3]

On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5]
"""







class 						_lowerCAmelCase(						unittest.TestCase		,   _a):

  """simple docstring"""







  def 		SCREAMING_SNAKE_CASE__	( self     )-> int:
     __A				 =       load_tool('''summarization'''     )
     self.tool.setup()
     __A				 =       load_tool('''summarization'''  ,    remote=UpperCAmelCase     )

  def 		SCREAMING_SNAKE_CASE__	( self     )-> List[str]:
     __A				 =       self.tool(UpperCAmelCase     )
     self.assertEqual(
         UpperCAmelCase  ,    '''Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf. In March 2021, Hugging Face raised $40 million in a Series B funding round. On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model. In 2022, the workshop concluded with the announcement of BLOOM.'''  ,    )

  def 		SCREAMING_SNAKE_CASE__	( self     )-> Dict:
     __A				 =       self.remote_tool(UpperCAmelCase     )
     self.assertEqual(
         UpperCAmelCase  ,    '''Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf. In March 2021, Hugging Face raised $40 million in a Series B funding round. On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model. In 2022, the workshop concluded with the announcement of BLOOM.'''  ,    )

  def 		SCREAMING_SNAKE_CASE__	( self     )-> Union[str, Any]:
     __A				 =       self.tool(text=UpperCAmelCase     )
     self.assertEqual(
         UpperCAmelCase  ,    '''Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf. In March 2021, Hugging Face raised $40 million in a Series B funding round. On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model. In 2022, the workshop concluded with the announcement of BLOOM.'''  ,    )





  def 		SCREAMING_SNAKE_CASE__	( self     )-> Optional[Any]:
     __A				 =       self.remote_tool(text=UpperCAmelCase     )
     self.assertEqual(
         UpperCAmelCase  ,    '''Hugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf. In March 2021, Hugging Face raised $40 million in a Series B funding round. On April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model. In 2022, the workshop concluded with the announcement of BLOOM.'''  ,    )

