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






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






   def    __UpperCamelCase  (   self       :  int						)      ->		Tuple:
       lowercase__				:     Union[str, Any]					      =       ids_tensor([self.batch_size, self.seq_length]			,		self.vocab_size						)

       lowercase__				:     Dict					      =       None
       if self.use_input_mask:
           lowercase__				:     List[str]					      =       random_attention_mask([self.batch_size, self.seq_length]						)

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

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

       lowercase__				:     List[str]					      =       self.get_config()

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






   def    __UpperCamelCase  (   self       :  int						)      ->		Optional[Any]:
       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=lowercase_			,		initializer_range=self.initializer_range			,		)






   def    __UpperCamelCase  (   self       :  Any						)      ->		Optional[int]:
       lowercase__				:     Union[str, Any]					      =       self.get_config()
       lowercase__				:     List[Any]					      =       3_00
       return config






   def    __UpperCamelCase  (   self       :  str						)      ->		List[Any]:
       (
           lowercase__     
       )				:     Dict					      =       self.prepare_config_and_inputs()

       lowercase__				:     str					      =       True
       lowercase__				:     List[str]					      =       floats_tensor([self.batch_size, self.seq_length, self.hidden_size]						)
       lowercase__				:     Any					      =       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       :  Tuple			,		lowercase_       :  Union[str, Any]			,		lowercase_       :  List[Any]			,		lowercase_       :  List[Any]			,		lowercase_       :  Union[str, Any]			,		lowercase_       :  Tuple			,		lowercase_       :  Tuple			,		lowercase_       :  List[Any]						)      ->		Optional[Any]:
       lowercase__				:     int					      =       YosoModel(config=lowercase_						)
       model.to(lowercase_						)
       model.eval()
       lowercase__				:     str					      =       model(lowercase_			,		attention_mask=lowercase_			,		token_type_ids=lowercase_						)
       lowercase__				:     Union[str, Any]					      =       model(lowercase_			,		token_type_ids=lowercase_						)
       lowercase__				:     Dict					      =       model(lowercase_						)
       self.parent.assertEqual(result.last_hidden_state.shape			,		(self.batch_size, self.seq_length, self.hidden_size)						)






   def    __UpperCamelCase  (   self       :  str			,		lowercase_       :  Tuple			,		lowercase_       :  Dict			,		lowercase_       :  Union[str, Any]			,		lowercase_       :  Any			,		lowercase_       :  int			,		lowercase_       :  Optional[Any]			,		lowercase_       :  Tuple			,		lowercase_       :  List[str]			,		lowercase_       :  str			,		)      ->		str:
       lowercase__				:     List[str]					      =       True
       lowercase__				:     Union[str, Any]					      =       YosoModel(lowercase_						)
       model.to(lowercase_						)
       model.eval()
       lowercase__				:     List[str]					      =       model(
           lowercase_			,		attention_mask=lowercase_			,		token_type_ids=lowercase_			,		encoder_hidden_states=lowercase_			,		encoder_attention_mask=lowercase_			,		)
       lowercase__				:     str					      =       model(
           lowercase_			,		attention_mask=lowercase_			,		token_type_ids=lowercase_			,		encoder_hidden_states=lowercase_			,		)
       lowercase__				:     int					      =       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       :  List[str]			,		lowercase_       :  str			,		lowercase_       :  Optional[int]			,		lowercase_       :  int			,		lowercase_       :  str			,		lowercase_       :  Optional[int]			,		lowercase_       :  Any			,		lowercase_       :  int						)      ->		str:
       lowercase__				:     List[Any]					      =       YosoForMaskedLM(config=lowercase_						)
       model.to(lowercase_						)
       model.eval()
       lowercase__				:     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    __UpperCamelCase  (   self       :  Tuple			,		lowercase_       :  List[str]			,		lowercase_       :  List[str]			,		lowercase_       :  Optional[Any]			,		lowercase_       :  List[str]			,		lowercase_       :  Union[str, Any]			,		lowercase_       :  int			,		lowercase_       :  List[str]						)      ->		Union[str, Any]:
       lowercase__				:     List[Any]					      =       YosoForQuestionAnswering(config=lowercase_						)
       model.to(lowercase_						)
       model.eval()
       lowercase__				:     Dict					      =       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       :  Union[str, Any]			,		lowercase_       :  Any			,		lowercase_       :  Optional[Any]			,		lowercase_       :  List[str]			,		lowercase_       :  int			,		lowercase_       :  Optional[int]			,		lowercase_       :  str			,		lowercase_       :  Union[str, Any]						)      ->		Dict:
       lowercase__				:     int					      =       self.num_labels
       lowercase__				:     Dict					      =       YosoForSequenceClassification(lowercase_						)
       model.to(lowercase_						)
       model.eval()
       lowercase__				:     str					      =       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       :  List[str]			,		lowercase_       :  List[str]			,		lowercase_       :  Tuple			,		lowercase_       :  Optional[Any]			,		lowercase_       :  Optional[int]			,		lowercase_       :  List[Any]			,		lowercase_       :  Optional[Any]			,		lowercase_       :  Optional[Any]						)      ->		int:
       lowercase__				:     List[str]					      =       self.num_labels
       lowercase__				:     Union[str, Any]					      =       YosoForTokenClassification(config=lowercase_						)
       model.to(lowercase_						)
       model.eval()
       lowercase__				:     str					      =       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       :  Optional[int]			,		lowercase_       :  Optional[int]			,		lowercase_       :  int			,		lowercase_       :  Optional[int]			,		lowercase_       :  int			,		lowercase_       :  Union[str, Any]			,		lowercase_       :  Dict			,		lowercase_       :  int						)      ->		Union[str, Any]:
       lowercase__				:     Tuple					      =       self.num_choices
       lowercase__				:     Optional[Any]					      =       YosoForMultipleChoice(config=lowercase_						)
       model.to(lowercase_						)
       model.eval()
       lowercase__				:     Any					      =       input_ids.unsqueeze(1						).expand(-1			,		self.num_choices			,		-1						).contiguous()
       lowercase__				:     Dict					      =       token_type_ids.unsqueeze(1						).expand(-1			,		self.num_choices			,		-1						).contiguous()
       lowercase__				:     str					      =       input_mask.unsqueeze(1						).expand(-1			,		self.num_choices			,		-1						).contiguous()
       lowercase__				:     Optional[int]					      =       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]						)      ->		Dict:
       lowercase__				:     List[str]					      =       self.prepare_config_and_inputs()
       (
           lowercase__     
       )				:     int					      =       config_and_inputs
       lowercase__				:     List[Any]					      =       {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
       return config, inputs_dict



@require_torch
class 						snake_case_ (	__A						,__A						,unittest.TestCase ):
   __A			:	Union[str, Any]        =       (
       (
           YosoModel,
           YosoForMaskedLM,
           YosoForMultipleChoice,
           YosoForQuestionAnswering,
           YosoForSequenceClassification,
           YosoForTokenClassification,
       )
       if is_torch_available()
       else ()
   )
   __A			:	str        =       False
   __A			:	List[str]        =       False
   __A			:	Union[str, Any]        =       False

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






   def    __UpperCamelCase  (   self       :  List[str]						)      ->		Dict:
       lowercase__				:     Optional[int]					      =       YosoModelTester(self						)
       lowercase__				:     Any					      =       ConfigTester(self			,		config_class=lowercase_			,		hidden_size=37						)






   def    __UpperCamelCase  (   self       :  Optional[int]						)      ->		Optional[Any]:
       self.config_tester.run_common_tests()






   def    __UpperCamelCase  (   self       :  Dict						)      ->		Optional[Any]:
       lowercase__				:     Tuple					      =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*lowercase_						)






   def    __UpperCamelCase  (   self       :  List[str]						)      ->		List[Any]:
       lowercase__				:     str					      =       self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
           lowercase__				:     List[Any]					      =       type
           self.model_tester.create_and_check_model(*lowercase_						)






   def    __UpperCamelCase  (   self       :  List[str]						)      ->		Dict:
       lowercase__				:     int					      =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*lowercase_						)






   def    __UpperCamelCase  (   self       :  int						)      ->		Dict:
       lowercase__				:     Optional[int]					      =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_multiple_choice(*lowercase_						)






   def    __UpperCamelCase  (   self       :  Any						)      ->		List[Any]:
       lowercase__				:     List[str]					      =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*lowercase_						)






   def    __UpperCamelCase  (   self       :  List[Any]						)      ->		List[Any]:
       lowercase__				:     Optional[int]					      =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*lowercase_						)






   def    __UpperCamelCase  (   self       :  Dict						)      ->		Dict:
       lowercase__				:     int					      =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*lowercase_						)






   @slow
   def    __UpperCamelCase  (   self       :  Optional[int]						)      ->		List[Any]:
       for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowercase__				:     List[str]					      =       YosoModel.from_pretrained(lowercase_						)
           self.assertIsNotNone(lowercase_						)







   def    __UpperCamelCase  (   self       :  str						)      ->		List[Any]:
       return



@require_torch
class 						snake_case_ (	unittest.TestCase ):






   @slow
   def    __UpperCamelCase  (   self       :  Optional[Any]						)      ->		str:
       lowercase__				:     Optional[Any]					      =       YosoModel.from_pretrained("uw-madison/yoso-4096"						)
       lowercase__				:     Tuple					      =       torch.tensor([[0, 1, 2, 3, 4, 5]]						)

       with torch.no_grad():
           lowercase__				:     int					      =       model(lowercase_						)[0]

       lowercase__				:     Any					      =       torch.Size((1, 6, 7_68)						)
       self.assertEqual(output.shape			,		lowercase_						)

       lowercase__				:     Dict					      =       torch.tensor(
           [[[-0.06_11, 0.12_42, 0.08_40], [0.02_80, -0.00_48, 0.11_25], [0.01_06, 0.02_26, 0.07_51]]]						)

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






   @slow
   def    __UpperCamelCase  (   self       :  Tuple						)      ->		Union[str, Any]:
       lowercase__				:     List[Any]					      =       YosoForMaskedLM.from_pretrained("uw-madison/yoso-4096"						)
       lowercase__				:     Dict					      =       torch.tensor([[0, 1, 2, 3, 4, 5]]						)

       with torch.no_grad():
           lowercase__				:     Optional[Any]					      =       model(lowercase_						)[0]

       lowercase__				:     Optional[Any]					      =       5_02_65

       lowercase__				:     Optional[int]					      =       torch.Size((1, 6, vocab_size)						)
       self.assertEqual(output.shape			,		lowercase_						)

       lowercase__				:     List[Any]					      =       torch.tensor(
           [[[-2.13_13, -3.72_85, -2.24_07], [-2.70_47, -3.33_14, -2.64_08], [0.06_29, -2.51_66, -0.33_56]]]						)

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







   @slow
   def    __UpperCamelCase  (   self       :  Optional[Any]						)      ->		Optional[int]:
       lowercase__				:     int					      =       YosoForMaskedLM.from_pretrained("uw-madison/yoso-4096"						)
       lowercase__				:     Union[str, Any]					      =       torch.arange(40_96						).unsqueeze(0						)

       with torch.no_grad():
           lowercase__				:     str					      =       model(lowercase_						)[0]

       lowercase__				:     List[str]					      =       5_02_65

       lowercase__				:     Dict					      =       torch.Size((1, 40_96, vocab_size)						)
       self.assertEqual(output.shape			,		lowercase_						)

       lowercase__				:     Optional[int]					      =       torch.tensor(
           [[[-2.39_14, -4.37_42, -5.09_56], [-4.09_88, -4.23_84, -7.04_06], [-3.14_27, -3.71_92, -6.68_00]]]						)

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




import unittest

from datasets import load_dataset

from transformers import load_tool

from .test_tools_common import ToolTesterMixin



class 						snake_case_ (	unittest.TestCase						,__A ):






   def    __UpperCamelCase  (   self       :  Optional[int]						)      ->		List[Any]:
       lowercase__				:     List[Any]					      =       load_tool("document-question-answering"						)
       self.tool.setup()
       lowercase__				:     Optional[int]					      =       load_tool("document-question-answering"			,		remote=lowercase_						)






   def    __UpperCamelCase  (   self       :  int						)      ->		Optional[int]:
       lowercase__				:     List[Any]					      =       load_dataset("hf-internal-testing/example-documents"			,		split="test"						)
       lowercase__				:     List[Any]					      =       dataset[0]["image"]

       lowercase__				:     int					      =       self.tool(lowercase_			,		"When is the coffee break?"						)
       self.assertEqual(lowercase_			,		"11-14 to 11:39 a.m."						)






   def    __UpperCamelCase  (   self       :  Optional[Any]						)      ->		List[Any]:
       lowercase__				:     str					      =       load_dataset("hf-internal-testing/example-documents"			,		split="test"						)
       lowercase__				:     str					      =       dataset[0]["image"]

       lowercase__				:     List[str]					      =       self.remote_tool(lowercase_			,		"When is the coffee break?"						)
       self.assertEqual(lowercase_			,		"11-14 to 11:39 a.m."						)






   def    __UpperCamelCase  (   self       :  List[Any]						)      ->		Dict:
       lowercase__				:     List[str]					      =       load_dataset("hf-internal-testing/example-documents"			,		split="test"						)
       lowercase__				:     List[str]					      =       dataset[0]["image"]

       self.tool(document=lowercase_			,		question="When is the coffee break?"						)







   def    __UpperCamelCase  (   self       :  str						)      ->		Optional[Any]:
       lowercase__				:     Optional[Any]					      =       load_dataset("hf-internal-testing/example-documents"			,		split="test"						)
       lowercase__				:     int					      =       dataset[0]["image"]

       lowercase__				:     Tuple					      =       self.remote_tool(document=lowercase_			,		question="When is the coffee break?"						)
       self.assertEqual(lowercase_			,		"11-14 to 11:39 a.m."						)



