



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


						def __init__( self						:  Union[str, Any]      ,       _a						:  Optional[Any]      ,       _a						:  Union[str, Any]=1_3      ,       _a						:  Optional[int]=7      ,       _a						:  int=True      ,       _a						:  Dict=True      ,       _a						:  Optional[int]=True      ,       _a						:  Optional[Any]=True      ,       _a						:  Tuple=9_9      ,       _a						:  Tuple=3_2      ,       _a						:  Optional[Any]=5      ,       _a						:  Dict=4      ,       _a						:  List[Any]=3_7      ,       _a						:  List[Any]="gelu"      ,       _a						:  List[str]=0.1      ,       _a						:  Tuple=0.1      ,       _a						:  Optional[Any]=5_1_2      ,       _a						:  Optional[Any]=1_6      ,       _a						:  Dict=2      ,       _a						:  str=0.0_2      ,       _a						:  Any=3      ,       _a						:  Union[str, Any]=4      ,       _a						:  Any=None      ,       ):
													a__:       Optional[int]											=parent
													a__:       int											=batch_size
													a__:       Optional[int]											=seq_length
													a__:       int											=is_training
													a__:       Optional[int]											=use_input_mask
													a__:       Optional[int]											=use_token_type_ids
													a__:       Dict											=use_labels
													a__:       Optional[Any]											=vocab_size
													a__:       List[str]											=hidden_size
													a__:       Union[str, Any]											=num_hidden_layers
													a__:       Tuple											=num_attention_heads
													a__:       List[Any]											=intermediate_size
													a__:       Tuple											=hidden_act
													a__:       Optional[Any]											=hidden_dropout_prob
													a__:       Any											=attention_probs_dropout_prob
													a__:       Any											=max_position_embeddings
													a__:       Union[str, Any]											=type_vocab_size
													a__:       Optional[int]											=type_sequence_label_size
													a__:       int											=initializer_range
													a__:       Optional[int]											=num_labels
													a__:       int											=num_choices
													a__:       int											=scope


						def   _lowerCamelCase			( self						:  str			):
													a__:       List[str]											=ids_tensor([self.batch_size, self.seq_length]      ,       self.vocab_size			)

													a__:       Optional[int]											=None
													if self.use_input_mask:
																				a__:       Optional[int]											=random_attention_mask([self.batch_size, self.seq_length]			)

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

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

													a__:       str											=self.get_config()

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


						def   _lowerCamelCase			( self						:  List[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=_a      ,       initializer_range=self.initializer_range      ,       )


						def   _lowerCamelCase			( self						:  str			):
													a__:       str											=self.get_config()
													a__:       Any											=3_0_0
													return config


						def   _lowerCamelCase			( self						:  Tuple			):
													(
													    (
													    a__
													)							,			(
													    a__
													)							,			(
													    a__
													)							,			(
													    a__
													)							,			(
													    a__
													)							,			(
													    a__
													)							,			(
													    a__
													)							,			
													):       Union[str, Any]											=self.prepare_config_and_inputs()

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


						def   _lowerCamelCase			( self						:  Tuple      ,       _a						:  List[Any]      ,       _a						:  int      ,       _a						:  List[Any]      ,       _a						:  Optional[Any]      ,       _a						:  Optional[Any]      ,       _a						:  Tuple      ,       _a						:  Union[str, Any]      ,       _a						:  Union[str, Any]      ,       _a						:  int      ,       ):
													a__:       Tuple											=True
													a__:       Tuple											=YosoModel(_a			)
													model.to(_a			)
													model.eval()
													a__:       Optional[int]											=model(
													    _a      ,       attention_mask=_a      ,       token_type_ids=_a      ,       encoder_hidden_states=_a      ,       encoder_attention_mask=_a      ,       )
													a__:       List[str]											=model(
													    _a      ,       attention_mask=_a      ,       token_type_ids=_a      ,       encoder_hidden_states=_a      ,       )
													a__:       Optional[int]											=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   _lowerCamelCase			( self						:  Optional[Any]      ,       _a						:  Union[str, Any]      ,       _a						:  Any      ,       _a						:  Any      ,       _a						:  Any      ,       _a						:  Union[str, Any]      ,       _a						:  str      ,       _a						:  Optional[int]			):
													a__:       Dict											=YosoForMaskedLM(config=_a			)
													model.to(_a			)
													model.eval()
													a__:       str											=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						:  Optional[Any]      ,       _a						:  List[Any]      ,       _a						:  str      ,       _a						:  Optional[Any]      ,       _a						:  List[Any]      ,       _a						:  Union[str, Any]      ,       _a						:  Optional[int]      ,       _a						:  Any			):
													a__:       Optional[Any]											=YosoForQuestionAnswering(config=_a			)
													model.to(_a			)
													model.eval()
													a__:       Optional[int]											=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						:  Tuple      ,       _a						:  Dict      ,       _a						:  List[str]      ,       _a						:  Tuple      ,       _a						:  Union[str, Any]      ,       _a						:  str      ,       _a						:  Tuple      ,       _a						:  Optional[int]			):
													a__:       List[str]											=self.num_labels
													a__:       Tuple											=YosoForSequenceClassification(_a			)
													model.to(_a			)
													model.eval()
													a__:       List[str]											=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						:  List[Any]      ,       _a						:  str      ,       _a						:  List[str]      ,       _a						:  Any      ,       _a						:  int      ,       _a						:  List[Any]      ,       _a						:  Optional[int]      ,       _a						:  int			):
													a__:       int											=self.num_labels
													a__:       int											=YosoForTokenClassification(config=_a			)
													model.to(_a			)
													model.eval()
													a__:       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.num_labels)			)


						def   _lowerCamelCase			( self						:  Optional[Any]      ,       _a						:  Union[str, Any]      ,       _a						:  str      ,       _a						:  Any      ,       _a						:  Optional[Any]      ,       _a						:  Optional[Any]      ,       _a						:  Optional[Any]      ,       _a						:  Tuple			):
													a__:       List[str]											=self.num_choices
													a__:       Optional[int]											=YosoForMultipleChoice(config=_a			)
													model.to(_a			)
													model.eval()
													a__:       str											=input_ids.unsqueeze(1			).expand(-1      ,       self.num_choices      ,       -1			).contiguous()
													a__:       Dict											=token_type_ids.unsqueeze(1			).expand(-1      ,       self.num_choices      ,       -1			).contiguous()
													a__:       Tuple											=input_mask.unsqueeze(1			).expand(-1      ,       self.num_choices      ,       -1			).contiguous()
													a__:       int											=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						:  Dict			):
													a__:       List[Any]											=self.prepare_config_and_inputs()
													(
													    (
													    a__
													)							,			(
													    a__
													)							,			(
													    a__
													)							,			(
													    a__
													)							,			(
													    a__
													)							,			(
													    a__
													)							,			(
													    a__
													)							,			
													):       Union[str, Any]											=config_and_inputs
													a__:       Tuple											={"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   ):
						_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   _lowerCamelCase			( self						:  Optional[Any]			):
													a__:       str											=YosoModelTester(self			)
													a__:       Tuple											=ConfigTester(self      ,       config_class=_a      ,       hidden_size=3_7			)


						def   _lowerCamelCase			( self						:  Tuple			):
													self.config_tester.run_common_tests()


						def   _lowerCamelCase			( self						:  List[str]			):
													a__:       Tuple											=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*_a			)


						def   _lowerCamelCase			( self						:  List[Any]			):
													a__:       List[Any]											=self.model_tester.prepare_config_and_inputs()
													for type in ["absolute", "relative_key", "relative_key_query"]:
																				a__:       Optional[int]											=type
																				self.model_tester.create_and_check_model(*_a			)


						def   _lowerCamelCase			( self						:  str			):
													a__:       Optional[Any]											=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_masked_lm(*_a			)


						def   _lowerCamelCase			( self						:  str			):
													a__:       Union[str, Any]											=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_multiple_choice(*_a			)


						def   _lowerCamelCase			( self						:  Dict			):
													a__:       Optional[int]											=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_question_answering(*_a			)


						def   _lowerCamelCase			( self						:  List[Any]			):
													a__:       Any											=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_sequence_classification(*_a			)


						def   _lowerCamelCase			( self						:  Dict			):
													a__:       Any											=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_token_classification(*_a			)


						@slow
						def   _lowerCamelCase			( self						:  List[Any]			):
													for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				a__:       List[str]											=YosoModel.from_pretrained(_a			)
																				self.assertIsNotNone(_a			)







						def   _lowerCamelCase			( self						:  List[str]			):
													return


@require_torch
class 	lowerCamelCase__ (		unittest.TestCase   ):


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

													with torch.no_grad():
																				a__:       Any											=model(_a			)[0]

													a__:       Union[str, Any]											=torch.Size((1, 6, 7_6_8)			)
													self.assertEqual(output.shape      ,       _a			)

													a__:       int											=torch.tensor(
													    [[[-0.0_6_1_1, 0.1_2_4_2, 0.0_8_4_0], [0.0_2_8_0, -0.0_0_4_8, 0.1_1_2_5], [0.0_1_0_6, 0.0_2_2_6, 0.0_7_5_1]]]			)

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


						@slow
						def   _lowerCamelCase			( self						:  str			):
													a__:       Union[str, Any]											=YosoForMaskedLM.from_pretrained("uw-madison/yoso-4096"			)
													a__:       Optional[Any]											=torch.tensor([[0, 1, 2, 3, 4, 5]]			)

													with torch.no_grad():
																				a__:       Optional[int]											=model(_a			)[0]

													a__:       List[Any]											=5_0_2_6_5

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

													a__:       int											=torch.tensor(
													    [[[-2.1_3_1_3, -3.7_2_8_5, -2.2_4_0_7], [-2.7_0_4_7, -3.3_3_1_4, -2.6_4_0_8], [0.0_6_2_9, -2.5_1_6_6, -0.3_3_5_6]]]			)

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







						@slow
						def   _lowerCamelCase			( self						:  int			):
													a__:       List[Any]											=YosoForMaskedLM.from_pretrained("uw-madison/yoso-4096"			)
													a__:       Optional[int]											=torch.arange(4_0_9_6			).unsqueeze(0			)

													with torch.no_grad():
																				a__:       Optional[int]											=model(_a			)[0]

													a__:       Tuple											=5_0_2_6_5

													a__:       Tuple											=torch.Size((1, 4_0_9_6, vocab_size)			)
													self.assertEqual(output.shape      ,       _a			)

													a__:       Optional[int]											=torch.tensor(
													    [[[-2.3_9_1_4, -4.3_7_4_2, -5.0_9_5_6], [-4.0_9_8_8, -4.2_3_8_4, -7.0_4_0_6], [-3.1_4_2_7, -3.7_1_9_2, -6.6_8_0_0]]]			)

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










from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


__UpperCAmelCase						  =     {
    '''configuration_splinter''': ['''SPLINTER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''SplinterConfig'''],
    '''tokenization_splinter''': ['''SplinterTokenizer'''],
}

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__UpperCAmelCase						  =     ['''SplinterTokenizerFast''']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__UpperCAmelCase						  =     [
		    '''SPLINTER_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''SplinterForQuestionAnswering''',
		    '''SplinterForPreTraining''',
		    '''SplinterLayer''',
		    '''SplinterModel''',
		    '''SplinterPreTrainedModel''',
		]


if TYPE_CHECKING:
		from .configuration_splinter import SPLINTER_PRETRAINED_CONFIG_ARCHIVE_MAP, SplinterConfig
		from .tokenization_splinter import SplinterTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_splinter_fast import SplinterTokenizerFast

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_splinter import (
				    SPLINTER_PRETRAINED_MODEL_ARCHIVE_LIST,
				    SplinterForPreTraining,
				    SplinterForQuestionAnswering,
				    SplinterLayer,
				    SplinterModel,
				    SplinterPreTrainedModel,
				)


else:
		import sys

		__UpperCAmelCase						  =     _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)





