





import unittest

from transformers import SqueezeBertConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import (
	    SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
	    SqueezeBertForMaskedLM,
	    SqueezeBertForMultipleChoice,
	    SqueezeBertForQuestionAnswering,
	    SqueezeBertForSequenceClassification,
	    SqueezeBertForTokenClassification,
	    SqueezeBertModel,
	)




class   UpperCamelCase_       (     UpperCamelCase__    ):


							def __init__(							self    :Optional[int]							,				__A    :Any							,				__A    :Union[str, Any]=13							,				__A    :List[Any]=7							,				__A    :List[str]=True							,				__A    :List[Any]=True							,				__A    :str=False							,				__A    :Union[str, Any]=True							,				__A    :Any=99							,				__A    :List[Any]=32							,				__A    :int=5							,				__A    :Union[str, Any]=4							,				__A    :Tuple=64							,				__A    :Tuple="gelu"							,				__A    :List[Any]=0.1							,				__A    :Optional[Any]=0.1							,				__A    :int=512							,				__A    :int=16							,				__A    :Optional[int]=2							,				__A    :Tuple=0.0_2							,				__A    :Union[str, Any]=3							,				__A    :Optional[int]=4							,				__A    :List[str]=None							,				__A    :Any=2							,				__A    :Optional[Any]=2							,				__A    :Tuple=2							,				__A    :Optional[Any]=2							,				__A    :Optional[Any]=4							,				__A    :Dict=1							,				)   ->   Dict:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      parent
									SCREAMING_SNAKE_CASE__			=      batch_size
									SCREAMING_SNAKE_CASE__			=      seq_length
									SCREAMING_SNAKE_CASE__			=      is_training
									SCREAMING_SNAKE_CASE__			=      use_input_mask
									SCREAMING_SNAKE_CASE__			=      use_token_type_ids
									SCREAMING_SNAKE_CASE__			=      use_labels
									SCREAMING_SNAKE_CASE__			=      vocab_size
									SCREAMING_SNAKE_CASE__			=      hidden_size
									SCREAMING_SNAKE_CASE__			=      num_hidden_layers
									SCREAMING_SNAKE_CASE__			=      num_attention_heads
									SCREAMING_SNAKE_CASE__			=      intermediate_size
									SCREAMING_SNAKE_CASE__			=      hidden_act
									SCREAMING_SNAKE_CASE__			=      hidden_dropout_prob
									SCREAMING_SNAKE_CASE__			=      attention_probs_dropout_prob
									SCREAMING_SNAKE_CASE__			=      max_position_embeddings
									SCREAMING_SNAKE_CASE__			=      type_vocab_size
									SCREAMING_SNAKE_CASE__			=      type_sequence_label_size
									SCREAMING_SNAKE_CASE__			=      initializer_range
									SCREAMING_SNAKE_CASE__			=      num_labels
									SCREAMING_SNAKE_CASE__			=      num_choices
									SCREAMING_SNAKE_CASE__			=      scope
									SCREAMING_SNAKE_CASE__			=      q_groups
									SCREAMING_SNAKE_CASE__			=      k_groups
									SCREAMING_SNAKE_CASE__			=      v_groups
									SCREAMING_SNAKE_CASE__			=      post_attention_groups
									SCREAMING_SNAKE_CASE__			=      intermediate_groups
									SCREAMING_SNAKE_CASE__			=      output_groups


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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      ids_tensor([self.batch_size, self.seq_length]							,				self.vocab_size		)

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

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

									SCREAMING_SNAKE_CASE__			=      self.get_config()

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


							def      _snake_case  (							self    :Any		)   ->   Optional[Any]:



									"""simple docstring"""



									return SqueezeBertConfig(
									    embedding_size=self.hidden_size							,				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							,				attention_probs_dropout_prob=self.hidden_dropout_prob							,				attention_dropout=self.attention_probs_dropout_prob							,				max_position_embeddings=self.max_position_embeddings							,				initializer_range=self.initializer_range							,				q_groups=self.q_groups							,				k_groups=self.k_groups							,				v_groups=self.v_groups							,				post_attention_groups=self.post_attention_groups							,				intermediate_groups=self.intermediate_groups							,				output_groups=self.output_groups							,				)


							def      _snake_case  (							self    :Tuple							,				__A    :Dict							,				__A    :Union[str, Any]							,				__A    :Any							,				__A    :Union[str, Any]							,				__A    :Optional[int]							,				__A    :Optional[Any]		)   ->   Any:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      SqueezeBertModel(config=__A		)
									model.to(__A		)
									model.eval()
									SCREAMING_SNAKE_CASE__			=      model(__A							,				__A		)
									SCREAMING_SNAKE_CASE__			=      model(__A		)
									self.parent.assertEqual(result.last_hidden_state.shape							,				(self.batch_size, self.seq_length, self.hidden_size)		)


							def      _snake_case  (							self    :int							,				__A    :Dict							,				__A    :Optional[Any]							,				__A    :List[Any]							,				__A    :str							,				__A    :str							,				__A    :int		)   ->   Tuple:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      SqueezeBertForMaskedLM(config=__A		)
									model.to(__A		)
									model.eval()
									SCREAMING_SNAKE_CASE__			=      model(__A							,				attention_mask=__A							,				labels=__A		)
									self.parent.assertEqual(result.logits.shape							,				(self.batch_size, self.seq_length, self.vocab_size)		)


							def      _snake_case  (							self    :int							,				__A    :str							,				__A    :Optional[Any]							,				__A    :Tuple							,				__A    :Optional[Any]							,				__A    :Optional[Any]							,				__A    :str		)   ->   int:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      SqueezeBertForQuestionAnswering(config=__A		)
									model.to(__A		)
									model.eval()
									SCREAMING_SNAKE_CASE__			=      model(
									    __A							,				attention_mask=__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      _snake_case  (							self    :int							,				__A    :Optional[Any]							,				__A    :int							,				__A    :str							,				__A    :int							,				__A    :List[Any]							,				__A    :str		)   ->   List[Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      self.num_labels
									SCREAMING_SNAKE_CASE__			=      SqueezeBertForSequenceClassification(__A		)
									model.to(__A		)
									model.eval()
									SCREAMING_SNAKE_CASE__			=      model(__A							,				attention_mask=__A							,				labels=__A		)
									self.parent.assertEqual(result.logits.shape							,				(self.batch_size, self.num_labels)		)


							def      _snake_case  (							self    :Union[str, Any]							,				__A    :Tuple							,				__A    :List[Any]							,				__A    :int							,				__A    :List[str]							,				__A    :Any							,				__A    :Optional[Any]		)   ->   int:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      self.num_labels
									SCREAMING_SNAKE_CASE__			=      SqueezeBertForTokenClassification(config=__A		)
									model.to(__A		)
									model.eval()

									SCREAMING_SNAKE_CASE__			=      model(__A							,				attention_mask=__A							,				labels=__A		)
									self.parent.assertEqual(result.logits.shape							,				(self.batch_size, self.seq_length, self.num_labels)		)


							def      _snake_case  (							self    :Any							,				__A    :Any							,				__A    :List[str]							,				__A    :List[Any]							,				__A    :int							,				__A    :str							,				__A    :Optional[int]		)   ->   Optional[Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      self.num_choices
									SCREAMING_SNAKE_CASE__			=      SqueezeBertForMultipleChoice(config=__A		)
									model.to(__A		)
									model.eval()
									SCREAMING_SNAKE_CASE__			=      input_ids.unsqueeze(1		).expand(-1							,				self.num_choices							,				-1		).contiguous()
									SCREAMING_SNAKE_CASE__			=      input_mask.unsqueeze(1		).expand(-1							,				self.num_choices							,				-1		).contiguous()
									SCREAMING_SNAKE_CASE__			=      model(
									    __A							,				attention_mask=__A							,				labels=__A							,				)
									self.parent.assertEqual(result.logits.shape							,				(self.batch_size, self.num_choices)		)


							def      _snake_case  (							self    :Optional[int]		)   ->   List[str]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      self.prepare_config_and_inputs()
									((SCREAMING_SNAKE_CASE__)	,						(SCREAMING_SNAKE_CASE__)	,						(SCREAMING_SNAKE_CASE__)	,						(SCREAMING_SNAKE_CASE__)	,						(SCREAMING_SNAKE_CASE__)	,						(SCREAMING_SNAKE_CASE__))			=      config_and_inputs
									SCREAMING_SNAKE_CASE__			=      {"""input_ids""": input_ids, """attention_mask""": input_mask}
									return config, inputs_dict




@require_torch
class   UpperCamelCase_       (     UpperCamelCase__				,  UpperCamelCase__				,  unittest.TestCase    ):
							lowerCamelCase_           =					(
							    (
							        SqueezeBertModel,
							        SqueezeBertForMaskedLM,
							        SqueezeBertForMultipleChoice,
							        SqueezeBertForQuestionAnswering,
							        SqueezeBertForSequenceClassification,
							        SqueezeBertForTokenClassification,
							    )
							    if is_torch_available()
							    else None
							)
							lowerCamelCase_           =					(
							    {
							        "feature-extraction": SqueezeBertModel,
							        "fill-mask": SqueezeBertForMaskedLM,
							        "question-answering": SqueezeBertForQuestionAnswering,
							        "text-classification": SqueezeBertForSequenceClassification,
							        "token-classification": SqueezeBertForTokenClassification,
							        "zero-shot": SqueezeBertForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							lowerCamelCase_           =					False
							lowerCamelCase_           =					True
							lowerCamelCase_           =					False


							def      _snake_case  (							self    :List[Any]		)   ->   Any:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      SqueezeBertModelTester(self		)
									SCREAMING_SNAKE_CASE__			=      ConfigTester(self							,				config_class=__A							,				dim=37		)


							def      _snake_case  (							self    :List[Any]		)   ->   int:



									"""simple docstring"""



									self.config_tester.run_common_tests()


							def      _snake_case  (							self    :Dict		)   ->   List[Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_squeezebert_model(*__A		)


							def      _snake_case  (							self    :List[str]		)   ->   List[str]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_squeezebert_for_masked_lm(*__A		)


							def      _snake_case  (							self    :List[str]		)   ->   str:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_squeezebert_for_question_answering(*__A		)


							def      _snake_case  (							self    :List[Any]		)   ->   Tuple:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_squeezebert_for_sequence_classification(*__A		)


							def      _snake_case  (							self    :Union[str, Any]		)   ->   str:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_squeezebert_for_token_classification(*__A		)


							def      _snake_case  (							self    :Any		)   ->   Any:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_squeezebert_for_multiple_choice(*__A		)


							@slow
							def      _snake_case  (							self    :Dict		)   ->   Union[str, Any]:



									"""simple docstring"""



									for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											SCREAMING_SNAKE_CASE__			=      SqueezeBertModel.from_pretrained(__A		)
											self.assertIsNotNone(__A		)




@require_sentencepiece
@require_tokenizers
@require_torch
class   UpperCamelCase_       (     unittest.TestCase    ):


							@slow
							def      _snake_case  (							self    :str		)   ->   str:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      SqueezeBertForSequenceClassification.from_pretrained("""squeezebert/squeezebert-mnli"""		)

									SCREAMING_SNAKE_CASE__			=      torch.tensor([[1, 2_9414, 232, 328, 740, 1140, 1_2695, 69, 13, 1588, 2]]		)
									SCREAMING_SNAKE_CASE__			=      model(__A		)[0]
									SCREAMING_SNAKE_CASE__			=      torch.Size((1, 3)		)
									self.assertEqual(output.shape							,				__A		)
									SCREAMING_SNAKE_CASE__			=      torch.tensor([[0.6_4_0_1, -0.0_3_4_9, -0.6_0_4_1]]		)
									self.assertTrue(torch.allclose(__A							,				__A							,				atol=1E-4		)		)






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


_lowerCamelCase   				=			{
    'configuration_layoutlmv3': [
        'LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'LayoutLMv3Config',
        'LayoutLMv3OnnxConfig',
    ],
    'processing_layoutlmv3': ['LayoutLMv3Processor'],
    'tokenization_layoutlmv3': ['LayoutLMv3Tokenizer'],
}

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

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_lowerCamelCase   				=			[
	    'LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST',
	    'LayoutLMv3ForQuestionAnswering',
	    'LayoutLMv3ForSequenceClassification',
	    'LayoutLMv3ForTokenClassification',
	    'LayoutLMv3Model',
	    'LayoutLMv3PreTrainedModel',
	]

try:
	if not is_tf_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_lowerCamelCase   				=			[
	    'TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST',
	    'TFLayoutLMv3ForQuestionAnswering',
	    'TFLayoutLMv3ForSequenceClassification',
	    'TFLayoutLMv3ForTokenClassification',
	    'TFLayoutLMv3Model',
	    'TFLayoutLMv3PreTrainedModel',
	]

try:
	if not is_vision_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_lowerCamelCase   				=			['LayoutLMv3FeatureExtractor']
	_lowerCamelCase   				=			['LayoutLMv3ImageProcessor']


if TYPE_CHECKING:
	from .configuration_layoutlmva import (
	    LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP,
	    LayoutLMvaConfig,
	    LayoutLMvaOnnxConfig,
	)
	from .processing_layoutlmva import LayoutLMvaProcessor
	from .tokenization_layoutlmva import LayoutLMvaTokenizer

	try:
		if not is_tokenizers_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_layoutlmva import (
		    LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
		    LayoutLMvaForQuestionAnswering,
		    LayoutLMvaForSequenceClassification,
		    LayoutLMvaForTokenClassification,
		    LayoutLMvaModel,
		    LayoutLMvaPreTrainedModel,
		)

	try:
		if not is_tf_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_tf_layoutlmva import (
		    TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFLayoutLMvaForQuestionAnswering,
		    TFLayoutLMvaForSequenceClassification,
		    TFLayoutLMvaForTokenClassification,
		    TFLayoutLMvaModel,
		    TFLayoutLMvaPreTrainedModel,
		)

	try:
		if not is_vision_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor
		from .image_processing_layoutlmva import LayoutLMvaImageProcessor

else:
	import sys

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