




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       _A				(		__lowercase     ):






		def __init__(    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE=13						,		_SCREAMING_SNAKE_CASE=7						,		_SCREAMING_SNAKE_CASE=True						,		_SCREAMING_SNAKE_CASE=True						,		_SCREAMING_SNAKE_CASE=False						,		_SCREAMING_SNAKE_CASE=True						,		_SCREAMING_SNAKE_CASE=99						,		_SCREAMING_SNAKE_CASE=32						,		_SCREAMING_SNAKE_CASE=5						,		_SCREAMING_SNAKE_CASE=4						,		_SCREAMING_SNAKE_CASE=64						,		_SCREAMING_SNAKE_CASE="gelu"						,		_SCREAMING_SNAKE_CASE=0.1						,		_SCREAMING_SNAKE_CASE=0.1						,		_SCREAMING_SNAKE_CASE=512						,		_SCREAMING_SNAKE_CASE=16						,		_SCREAMING_SNAKE_CASE=2						,		_SCREAMING_SNAKE_CASE=0.02						,		_SCREAMING_SNAKE_CASE=3						,		_SCREAMING_SNAKE_CASE=4						,		_SCREAMING_SNAKE_CASE=None						,		_SCREAMING_SNAKE_CASE=2						,		_SCREAMING_SNAKE_CASE=2						,		_SCREAMING_SNAKE_CASE=2						,		_SCREAMING_SNAKE_CASE=2						,		_SCREAMING_SNAKE_CASE=4						,		_SCREAMING_SNAKE_CASE=1						,		):
								_UpperCAmelCase      =			parent
								_UpperCAmelCase      =			batch_size
								_UpperCAmelCase      =			seq_length
								_UpperCAmelCase      =			is_training
								_UpperCAmelCase      =			use_input_mask
								_UpperCAmelCase      =			use_token_type_ids
								_UpperCAmelCase      =			use_labels
								_UpperCAmelCase      =			vocab_size
								_UpperCAmelCase      =			hidden_size
								_UpperCAmelCase      =			num_hidden_layers
								_UpperCAmelCase      =			num_attention_heads
								_UpperCAmelCase      =			intermediate_size
								_UpperCAmelCase      =			hidden_act
								_UpperCAmelCase      =			hidden_dropout_prob
								_UpperCAmelCase      =			attention_probs_dropout_prob
								_UpperCAmelCase      =			max_position_embeddings
								_UpperCAmelCase      =			type_vocab_size
								_UpperCAmelCase      =			type_sequence_label_size
								_UpperCAmelCase      =			initializer_range
								_UpperCAmelCase      =			num_labels
								_UpperCAmelCase      =			num_choices
								_UpperCAmelCase      =			scope
								_UpperCAmelCase      =			q_groups
								_UpperCAmelCase      =			k_groups
								_UpperCAmelCase      =			v_groups
								_UpperCAmelCase      =			post_attention_groups
								_UpperCAmelCase      =			intermediate_groups
								_UpperCAmelCase      =			output_groups






		def 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			ids_tensor([self.batch_size, self.seq_length]						,		self.vocab_size			)

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

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

								_UpperCAmelCase      =			self.get_config()

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






		def 				UpperCAmelCase (    self			):
								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 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			):
								_UpperCAmelCase      =			SqueezeBertModel(config=_SCREAMING_SNAKE_CASE			)
								model.to(_SCREAMING_SNAKE_CASE			)
								model.eval()
								_UpperCAmelCase      =			model(_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			)
								_UpperCAmelCase      =			model(_SCREAMING_SNAKE_CASE			)
								self.parent.assertEqual(result.last_hidden_state.shape						,		(self.batch_size, self.seq_length, self.hidden_size)			)






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			):
								_UpperCAmelCase      =			SqueezeBertForMaskedLM(config=_SCREAMING_SNAKE_CASE			)
								model.to(_SCREAMING_SNAKE_CASE			)
								model.eval()
								_UpperCAmelCase      =			model(_SCREAMING_SNAKE_CASE						,		attention_mask=_SCREAMING_SNAKE_CASE						,		labels=_SCREAMING_SNAKE_CASE			)
								self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.seq_length, self.vocab_size)			)






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			):
								_UpperCAmelCase      =			SqueezeBertForQuestionAnswering(config=_SCREAMING_SNAKE_CASE			)
								model.to(_SCREAMING_SNAKE_CASE			)
								model.eval()
								_UpperCAmelCase      =			model(
								    _SCREAMING_SNAKE_CASE						,		attention_mask=_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 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			):
								_UpperCAmelCase      =			self.num_labels
								_UpperCAmelCase      =			SqueezeBertForSequenceClassification(_SCREAMING_SNAKE_CASE			)
								model.to(_SCREAMING_SNAKE_CASE			)
								model.eval()
								_UpperCAmelCase      =			model(_SCREAMING_SNAKE_CASE						,		attention_mask=_SCREAMING_SNAKE_CASE						,		labels=_SCREAMING_SNAKE_CASE			)
								self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.num_labels)			)






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			):
								_UpperCAmelCase      =			self.num_labels
								_UpperCAmelCase      =			SqueezeBertForTokenClassification(config=_SCREAMING_SNAKE_CASE			)
								model.to(_SCREAMING_SNAKE_CASE			)
								model.eval()

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






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			):
								_UpperCAmelCase      =			self.num_choices
								_UpperCAmelCase      =			SqueezeBertForMultipleChoice(config=_SCREAMING_SNAKE_CASE			)
								model.to(_SCREAMING_SNAKE_CASE			)
								model.eval()
								_UpperCAmelCase      =			input_ids.unsqueeze(1			).expand(-1						,		self.num_choices						,		-1			).contiguous()
								_UpperCAmelCase      =			input_mask.unsqueeze(1			).expand(-1						,		self.num_choices						,		-1			).contiguous()
								_UpperCAmelCase      =			model(
								    _SCREAMING_SNAKE_CASE						,		attention_mask=_SCREAMING_SNAKE_CASE						,		labels=_SCREAMING_SNAKE_CASE						,		)
								self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.num_choices)			)






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




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






		def 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			SqueezeBertModelTester(self			)
								_UpperCAmelCase      =			ConfigTester(self						,		config_class=_SCREAMING_SNAKE_CASE						,		dim=37			)






		def 				UpperCAmelCase (    self			):
								self.config_tester.run_common_tests()






		def 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_squeezebert_model(*_SCREAMING_SNAKE_CASE			)






		def 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_squeezebert_for_masked_lm(*_SCREAMING_SNAKE_CASE			)






		def 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_squeezebert_for_question_answering(*_SCREAMING_SNAKE_CASE			)






		def 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_squeezebert_for_sequence_classification(*_SCREAMING_SNAKE_CASE			)






		def 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_squeezebert_for_token_classification(*_SCREAMING_SNAKE_CASE			)






		def 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_squeezebert_for_multiple_choice(*_SCREAMING_SNAKE_CASE			)






		@slow
		def 				UpperCAmelCase (    self			):
								for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_UpperCAmelCase      =			SqueezeBertModel.from_pretrained(_SCREAMING_SNAKE_CASE			)
														self.assertIsNotNone(_SCREAMING_SNAKE_CASE			)




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






		@slow
		def 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			SqueezeBertForSequenceClassification.from_pretrained("""squeezebert/squeezebert-mnli"""			)

								_UpperCAmelCase      =			torch.tensor([[1, 2_9414, 232, 328, 740, 1140, 1_2695, 69, 13, 1588, 2]]			)
								_UpperCAmelCase      =			model(_SCREAMING_SNAKE_CASE			)[0]
								_UpperCAmelCase      =			torch.Size((1, 3)			)
								self.assertEqual(output.shape						,		_SCREAMING_SNAKE_CASE			)
								_UpperCAmelCase      =			torch.tensor([[0.6401, -0.0349, -0.6041]]			)
								self.assertTrue(torch.allclose(_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		atol=1e-4			)			)





import math




def        _SCREAMING_SNAKE_CASE      (		snake_case			,     snake_case       )						->				float:
						if (
						    not isinstance(snake_case			,     (int, float)       )
						    or power_factor < -1
						    or power_factor > 1
						):
												raise ValueError("""power_factor must be a valid float value between -1 and 1."""       )
						return apparent_power * power_factor




def        _SCREAMING_SNAKE_CASE      (		snake_case			,     snake_case       )						->				float:
						if (
						    not isinstance(snake_case			,     (int, float)       )
						    or power_factor < -1
						    or power_factor > 1
						):
												raise ValueError("""power_factor must be a valid float value between -1 and 1."""       )
						return apparent_power * math.sqrt(1 - power_factor**2       )


if __name__ == "__main__":
				import doctest

				doctest.testmod()