"""simple docstring"""






from typing import TYPE_CHECKING

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


UpperCamelCase     					=     {
    'configuration_flava': [
        'FLAVA_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'FlavaConfig',
        'FlavaImageCodebookConfig',
        'FlavaImageConfig',
        'FlavaMultimodalConfig',
        'FlavaTextConfig',
    ],
}

try:
				if not is_vision_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCamelCase     					=     ['FlavaFeatureExtractor']
				UpperCamelCase     					=     ['FlavaImageProcessor']
				UpperCamelCase     					=     ['FlavaProcessor']

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCamelCase     					=     [
				    'FLAVA_PRETRAINED_MODEL_ARCHIVE_LIST',
				    'FlavaForPreTraining',
				    'FlavaImageCodebook',
				    'FlavaImageModel',
				    'FlavaModel',
				    'FlavaMultimodalModel',
				    'FlavaPreTrainedModel',
				    'FlavaTextModel',
				]

if TYPE_CHECKING:
				from .configuration_flava import (
				    FLAVA_PRETRAINED_CONFIG_ARCHIVE_MAP,
				    FlavaConfig,
				    FlavaImageCodebookConfig,
				    FlavaImageConfig,
				    FlavaMultimodalConfig,
				    FlavaTextConfig,
				)

				try:
								if not is_vision_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .feature_extraction_flava import FlavaFeatureExtractor
								from .image_processing_flava import FlavaImageProcessor
								from .processing_flava import FlavaProcessor

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_flava import (
								    FLAVA_PRETRAINED_MODEL_ARCHIVE_LIST,
								    FlavaForPreTraining,
								    FlavaImageCodebook,
								    FlavaImageModel,
								    FlavaModel,
								    FlavaMultimodalModel,
								    FlavaPreTrainedModel,
								    FlavaTextModel,
								)

else:
				import sys

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





"""simple docstring"""






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


	"""simple docstring"""







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







	def       A    (			self )    ->	Optional[int]:
							a_							:			int     		=					ids_tensor([self.batch_size, self.seq_length]       ,     self.vocab_size )

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

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

							a_							:			Dict     		=					None
							a_							:			Dict     		=					None
							a_							:			Union[str, Any]     		=					None
							if self.use_labels:
													a_							:			List[str]     		=					ids_tensor([self.batch_size]       ,     self.type_sequence_label_size )
													a_							:			List[Any]     		=					ids_tensor([self.batch_size, self.seq_length]       ,     self.num_labels )
													a_							:			List[Any]     		=					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       A    (			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=_SCREAMING_SNAKE_CASE       ,     initializer_range=self.initializer_range       ,     )







	def       A    (			self )    ->	Optional[Any]:
							a_							:			Optional[int]     		=					self.get_config()
							a_							:			List[Any]     		=					3_0_0
							return config







	def       A    (			self )    ->	int:
							(
							    (
							    a_
							)		,					(
							    a_
							)		,					(
							    a_
							)		,					(
							    a_
							)		,					(
							    a_
							)		,					(
							    a_
							)		,					(
							    a_
							)		,					
							)							:			Tuple     		=					self.prepare_config_and_inputs()

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







	def       A    (			self       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     )    ->	List[Any]:
							a_							:			int     		=					True
							a_							:			Optional[Any]     		=					YosoModel(_SCREAMING_SNAKE_CASE )
							model.to(_SCREAMING_SNAKE_CASE )
							model.eval()
							a_							:			Tuple     		=					model(
							    _SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE       ,     encoder_hidden_states=_SCREAMING_SNAKE_CASE       ,     encoder_attention_mask=_SCREAMING_SNAKE_CASE       ,     )
							a_							:			str     		=					model(
							    _SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE       ,     encoder_hidden_states=_SCREAMING_SNAKE_CASE       ,     )
							a_							:			str     		=					model(_SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE )
							self.parent.assertEqual(result.last_hidden_state.shape       ,     (self.batch_size, self.seq_length, self.hidden_size) )







	def       A    (			self       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE )    ->	List[str]:
							a_							:			Optional[int]     		=					YosoForMaskedLM(config=_SCREAMING_SNAKE_CASE )
							model.to(_SCREAMING_SNAKE_CASE )
							model.eval()
							a_							:			str     		=					model(_SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE       ,     labels=_SCREAMING_SNAKE_CASE )
							self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.seq_length, self.vocab_size) )







	def       A    (			self       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE )    ->	Optional[int]:
							a_							:			Tuple     		=					YosoForQuestionAnswering(config=_SCREAMING_SNAKE_CASE )
							model.to(_SCREAMING_SNAKE_CASE )
							model.eval()
							a_							:			Optional[int]     		=					model(
							    _SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE       ,     start_positions=_SCREAMING_SNAKE_CASE       ,     end_positions=_SCREAMING_SNAKE_CASE       ,     )
							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       A    (			self       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE )    ->	int:
							a_							:			int     		=					self.num_labels
							a_							:			Optional[int]     		=					YosoForSequenceClassification(_SCREAMING_SNAKE_CASE )
							model.to(_SCREAMING_SNAKE_CASE )
							model.eval()
							a_							:			Union[str, Any]     		=					model(_SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE       ,     labels=_SCREAMING_SNAKE_CASE )
							self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.num_labels) )







	def       A    (			self       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE )    ->	List[str]:
							a_							:			str     		=					self.num_labels
							a_							:			List[str]     		=					YosoForTokenClassification(config=_SCREAMING_SNAKE_CASE )
							model.to(_SCREAMING_SNAKE_CASE )
							model.eval()
							a_							:			Tuple     		=					model(_SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE       ,     labels=_SCREAMING_SNAKE_CASE )
							self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.seq_length, self.num_labels) )







	def       A    (			self       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE )    ->	Union[str, Any]:
							a_							:			Optional[int]     		=					self.num_choices
							a_							:			int     		=					YosoForMultipleChoice(config=_SCREAMING_SNAKE_CASE )
							model.to(_SCREAMING_SNAKE_CASE )
							model.eval()
							a_							:			Optional[Any]     		=					input_ids.unsqueeze(1 ).expand(-1       ,     self.num_choices       ,     -1 ).contiguous()
							a_							:			Optional[Any]     		=					token_type_ids.unsqueeze(1 ).expand(-1       ,     self.num_choices       ,     -1 ).contiguous()
							a_							:			Optional[int]     		=					input_mask.unsqueeze(1 ).expand(-1       ,     self.num_choices       ,     -1 ).contiguous()
							a_							:			Optional[int]     		=					model(
							    _SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE       ,     labels=_SCREAMING_SNAKE_CASE       ,     )
							self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.num_choices) )







	def       A    (			self )    ->	Tuple:
							a_							:			Optional[Any]     		=					self.prepare_config_and_inputs()
							(
							    (
							    a_
							)		,					(
							    a_
							)		,					(
							    a_
							)		,					(
							    a_
							)		,					(
							    a_
							)		,					(
							    a_
							)		,					(
							    a_
							)		,					
							)							:			Any     		=					config_and_inputs
							a_							:			Any     		=					{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
							return config, inputs_dict




@require_torch
class   UpperCAmelCase__				(    __lowerCamelCase,      __lowerCamelCase,      unittest.TestCase			):


	"""simple docstring"""
	lowerCAmelCase__    :					List[str]						      =							(
	    (
	        YosoModel,
	        YosoForMaskedLM,
	        YosoForMultipleChoice,
	        YosoForQuestionAnswering,
	        YosoForSequenceClassification,
	        YosoForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	lowerCAmelCase__    :					Any						      =							False
	lowerCAmelCase__    :					List[Any]						      =							False
	lowerCAmelCase__    :					Dict						      =							False

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







	def       A    (			self )    ->	Optional[Any]:
							a_							:			str     		=					YosoModelTester(self )
							a_							:			Any     		=					ConfigTester(self       ,     config_class=_SCREAMING_SNAKE_CASE       ,     hidden_size=3_7 )







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







	def       A    (			self )    ->	Optional[int]:
							a_							:			Dict     		=					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE )







	def       A    (			self )    ->	Union[str, Any]:
							a_							:			Any     		=					self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
													a_							:			Optional[Any]     		=					type
													self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE )







	def       A    (			self )    ->	Any:
							a_							:			str     		=					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*_SCREAMING_SNAKE_CASE )







	def       A    (			self )    ->	int:
							a_							:			str     		=					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*_SCREAMING_SNAKE_CASE )







	def       A    (			self )    ->	Union[str, Any]:
							a_							:			Tuple     		=					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE )







	def       A    (			self )    ->	Optional[Any]:
							a_							:			int     		=					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*_SCREAMING_SNAKE_CASE )







	def       A    (			self )    ->	Optional[Any]:
							a_							:			str     		=					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE )







	@slow
	def       A    (			self )    ->	int:
							for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													a_							:			Optional[int]     		=					YosoModel.from_pretrained(_SCREAMING_SNAKE_CASE )
													self.assertIsNotNone(_SCREAMING_SNAKE_CASE )







	def       A    (			self )    ->	Any:
							return




@require_torch
class   UpperCAmelCase__				(    unittest.TestCase			):


	"""simple docstring"""







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

							with torch.no_grad():
													a_							:			Tuple     		=					model(_SCREAMING_SNAKE_CASE )[0]

							a_							:			int     		=					torch.Size((1, 6, 7_6_8) )
							self.assertEqual(output.shape       ,     _SCREAMING_SNAKE_CASE )

							a_							:			List[str]     		=					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]       ,     _SCREAMING_SNAKE_CASE       ,     atol=1E-4 ) )







	@slow
	def       A    (			self )    ->	Optional[int]:
							a_							:			int     		=					YosoForMaskedLM.from_pretrained("uw-madison/yoso-4096" )
							a_							:			Tuple     		=					torch.tensor([[0, 1, 2, 3, 4, 5]] )

							with torch.no_grad():
													a_							:			Any     		=					model(_SCREAMING_SNAKE_CASE )[0]

							a_							:			List[str]     		=					5_0_2_6_5

							a_							:			Union[str, Any]     		=					torch.Size((1, 6, vocab_size) )
							self.assertEqual(output.shape       ,     _SCREAMING_SNAKE_CASE )

							a_							:			Optional[Any]     		=					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]       ,     _SCREAMING_SNAKE_CASE       ,     atol=1E-4 ) )







	@slow
	def       A    (			self )    ->	int:
							a_							:			List[str]     		=					YosoForMaskedLM.from_pretrained("uw-madison/yoso-4096" )
							a_							:			Dict     		=					torch.arange(4_0_9_6 ).unsqueeze(0 )

							with torch.no_grad():
													a_							:			Tuple     		=					model(_SCREAMING_SNAKE_CASE )[0]

							a_							:			str     		=					5_0_2_6_5

							a_							:			List[Any]     		=					torch.Size((1, 4_0_9_6, vocab_size) )
							self.assertEqual(output.shape       ,     _SCREAMING_SNAKE_CASE )

							a_							:			str     		=					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]       ,     _SCREAMING_SNAKE_CASE       ,     atol=1E-4 ) )




