





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 					__lowerCAmelCase       (			_a     ):






			def __init__(self					,							__magic_name__					,							__magic_name__=13					,							__magic_name__=7					,							__magic_name__=True					,							__magic_name__=True					,							__magic_name__=False					,							__magic_name__=True					,							__magic_name__=99					,							__magic_name__=32					,							__magic_name__=5					,							__magic_name__=4					,							__magic_name__=64					,							__magic_name__="gelu"					,							__magic_name__=0.1					,							__magic_name__=0.1					,							__magic_name__=512					,							__magic_name__=16					,							__magic_name__=2					,							__magic_name__=0.02					,							__magic_name__=3					,							__magic_name__=4					,							__magic_name__=None					,							__magic_name__=2					,							__magic_name__=2					,							__magic_name__=2					,							__magic_name__=2					,							__magic_name__=4					,							__magic_name__=1					,							)     ->   List[str]:


							'''simple docstring'''



							snake_case_						:    Tuple				=  parent
							snake_case_						:    str				=  batch_size
							snake_case_						:    Tuple				=  seq_length
							snake_case_						:    Optional[Any]				=  is_training
							snake_case_						:    Tuple				=  use_input_mask
							snake_case_						:    int				=  use_token_type_ids
							snake_case_						:    List[Any]				=  use_labels
							snake_case_						:    Tuple				=  vocab_size
							snake_case_						:    Optional[int]				=  hidden_size
							snake_case_						:    Dict				=  num_hidden_layers
							snake_case_						:    List[str]				=  num_attention_heads
							snake_case_						:    Any				=  intermediate_size
							snake_case_						:    Dict				=  hidden_act
							snake_case_						:    int				=  hidden_dropout_prob
							snake_case_						:    List[Any]				=  attention_probs_dropout_prob
							snake_case_						:    Optional[int]				=  max_position_embeddings
							snake_case_						:    List[Any]				=  type_vocab_size
							snake_case_						:    Union[str, Any]				=  type_sequence_label_size
							snake_case_						:    Any				=  initializer_range
							snake_case_						:    List[str]				=  num_labels
							snake_case_						:    Dict				=  num_choices
							snake_case_						:    Any				=  scope
							snake_case_						:    Any				=  q_groups
							snake_case_						:    Union[str, Any]				=  k_groups
							snake_case_						:    Dict				=  v_groups
							snake_case_						:    int				=  post_attention_groups
							snake_case_						:    Union[str, Any]				=  intermediate_groups
							snake_case_						:    Union[str, Any]				=  output_groups






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


							'''simple docstring'''



							snake_case_						:    Tuple				=  ids_tensor([self.batch_size, self.seq_length]					,							self.vocab_size    )

							snake_case_						:    Dict				=  None
							if self.use_input_mask:
											snake_case_						:    Any				=  random_attention_mask([self.batch_size, self.seq_length]    )

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

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

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






			def 			lowerCamelCase  (self    )     ->   Optional[int]:


							'''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 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   List[str]:


							'''simple docstring'''



							snake_case_						:    Tuple				=  SqueezeBertModel(config=__magic_name__    )
							model.to(__magic_name__    )
							model.eval()
							snake_case_						:    List[str]				=  model(__magic_name__					,							__magic_name__    )
							snake_case_						:    Optional[int]				=  model(__magic_name__    )
							self.parent.assertEqual(result.last_hidden_state.shape					,							(self.batch_size, self.seq_length, self.hidden_size)    )






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


							'''simple docstring'''



							snake_case_						:    Union[str, Any]				=  SqueezeBertForMaskedLM(config=__magic_name__    )
							model.to(__magic_name__    )
							model.eval()
							snake_case_						:    Optional[int]				=  model(__magic_name__					,							attention_mask=__magic_name__					,							labels=__magic_name__    )
							self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.seq_length, self.vocab_size)    )






			def 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   Any:


							'''simple docstring'''



							snake_case_						:    Any				=  SqueezeBertForQuestionAnswering(config=__magic_name__    )
							model.to(__magic_name__    )
							model.eval()
							snake_case_						:    Tuple				=  model(
							    __magic_name__					,							attention_mask=__magic_name__					,							start_positions=__magic_name__					,							end_positions=__magic_name__    )
							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					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   Union[str, Any]:


							'''simple docstring'''



							snake_case_						:    Tuple				=  self.num_labels
							snake_case_						:    Tuple				=  SqueezeBertForSequenceClassification(__magic_name__    )
							model.to(__magic_name__    )
							model.eval()
							snake_case_						:    Optional[Any]				=  model(__magic_name__					,							attention_mask=__magic_name__					,							labels=__magic_name__    )
							self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.num_labels)    )






			def 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   List[Any]:


							'''simple docstring'''



							snake_case_						:    List[str]				=  self.num_labels
							snake_case_						:    Union[str, Any]				=  SqueezeBertForTokenClassification(config=__magic_name__    )
							model.to(__magic_name__    )
							model.eval()

							snake_case_						:    Any				=  model(__magic_name__					,							attention_mask=__magic_name__					,							labels=__magic_name__    )
							self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.seq_length, self.num_labels)    )






			def 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   List[str]:


							'''simple docstring'''



							snake_case_						:    Tuple				=  self.num_choices
							snake_case_						:    Tuple				=  SqueezeBertForMultipleChoice(config=__magic_name__    )
							model.to(__magic_name__    )
							model.eval()
							snake_case_						:    Union[str, Any]				=  input_ids.unsqueeze(1    ).expand(-1					,							self.num_choices					,							-1    ).contiguous()
							snake_case_						:    Dict				=  input_mask.unsqueeze(1    ).expand(-1					,							self.num_choices					,							-1    ).contiguous()
							snake_case_						:    Tuple				=  model(
							    __magic_name__					,							attention_mask=__magic_name__					,							labels=__magic_name__					,							)
							self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.num_choices)    )






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


							'''simple docstring'''



							snake_case_						:    Union[str, Any]				=  self.prepare_config_and_inputs()
							((snake_case_)			,							(snake_case_)			,							(snake_case_)			,							(snake_case_)			,							(snake_case_)			,							(snake_case_))						:    Any				=  config_and_inputs
							snake_case_						:    Any				=  {'''input_ids''': input_ids, '''attention_mask''': input_mask}
							return config, inputs_dict




@require_torch
class 					__lowerCAmelCase       (			_a,     _a,     unittest.TestCase     ):
			lowerCamelCase_     :     List[str]       			=      (
			    (
			        SqueezeBertModel,
			        SqueezeBertForMaskedLM,
			        SqueezeBertForMultipleChoice,
			        SqueezeBertForQuestionAnswering,
			        SqueezeBertForSequenceClassification,
			        SqueezeBertForTokenClassification,
			    )
			    if is_torch_available()
			    else None
			)
			lowerCamelCase_     :     Optional[int]       			=      (
			    {
			        '''feature-extraction''': SqueezeBertModel,
			        '''fill-mask''': SqueezeBertForMaskedLM,
			        '''question-answering''': SqueezeBertForQuestionAnswering,
			        '''text-classification''': SqueezeBertForSequenceClassification,
			        '''token-classification''': SqueezeBertForTokenClassification,
			        '''zero-shot''': SqueezeBertForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			lowerCamelCase_     :     str       			=      False
			lowerCamelCase_     :     Optional[int]       			=      True
			lowerCamelCase_     :     List[str]       			=      False






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


							'''simple docstring'''



							snake_case_						:    int				=  SqueezeBertModelTester(self    )
							snake_case_						:    Any				=  ConfigTester(self					,							config_class=__magic_name__					,							dim=37    )






			def 			lowerCamelCase  (self    )     ->   str:


							'''simple docstring'''



							self.config_tester.run_common_tests()






			def 			lowerCamelCase  (self    )     ->   str:


							'''simple docstring'''



							snake_case_						:    Any				=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_squeezebert_model(*__magic_name__    )






			def 			lowerCamelCase  (self    )     ->   Tuple:


							'''simple docstring'''



							snake_case_						:    List[Any]				=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_squeezebert_for_masked_lm(*__magic_name__    )






			def 			lowerCamelCase  (self    )     ->   Optional[int]:


							'''simple docstring'''



							snake_case_						:    List[str]				=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_squeezebert_for_question_answering(*__magic_name__    )






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


							'''simple docstring'''



							snake_case_						:    List[Any]				=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_squeezebert_for_sequence_classification(*__magic_name__    )






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


							'''simple docstring'''



							snake_case_						:    Union[str, Any]				=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_squeezebert_for_token_classification(*__magic_name__    )






			def 			lowerCamelCase  (self    )     ->   Tuple:


							'''simple docstring'''



							snake_case_						:    Optional[Any]				=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_squeezebert_for_multiple_choice(*__magic_name__    )






			@slow
			def 			lowerCamelCase  (self    )     ->   Tuple:


							'''simple docstring'''



							for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											snake_case_						:    Any				=  SqueezeBertModel.from_pretrained(__magic_name__    )
											self.assertIsNotNone(__magic_name__    )




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






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


							'''simple docstring'''



							snake_case_						:    Union[str, Any]				=  SqueezeBertForSequenceClassification.from_pretrained('''squeezebert/squeezebert-mnli'''    )

							snake_case_						:    Tuple				=  torch.tensor([[1, 2_9414, 232, 328, 740, 1140, 1_2695, 69, 13, 1588, 2]]    )
							snake_case_						:    Any				=  model(__magic_name__    )[0]
							snake_case_						:    Any				=  torch.Size((1, 3)    )
							self.assertEqual(output.shape					,							__magic_name__    )
							snake_case_						:    List[Any]				=  torch.tensor([[0.6_401, -0.0_349, -0.6_041]]    )
							self.assertTrue(torch.allclose(__magic_name__					,							__magic_name__					,							atol=1e-4    )    )







lowerCAmelCase_    =				{
    "joule": 1.0,
    "kilojoule": 1_0_0_0,
    "megajoule": 1_0_0_0_0_0_0,
    "gigajoule": 1_0_0_0_0_0_0_0_0_0,
    "wattsecond": 1.0,
    "watthour": 3_6_0_0,
    "kilowatthour": 3_6_0_0_0_0_0,
    "newtonmeter": 1.0,
    "calorie_nutr": 4_1_8_6.8,
    "kilocalorie_nutr": 4_1_8_6_8_0_0.0_0,
    "electronvolt": 1.602_176_634e-19,
    "britishthermalunit_it": 1_0_5_5.0_5_5_8_5,
    "footpound": 1.355_818,
}






def      lowerCamelCase_						(				_UpperCamelCase ,				_UpperCamelCase ,				_UpperCamelCase )		->						float:






				"""simple docstring"""


				if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION:
								snake_case_						:    str				=  (
								    f'''Incorrect \'from_type\' or \'to_type\' value: {from_type!r}, {to_type!r}\n'''
								    f'''Valid values are: {", ".join(_UpperCamelCase )}'''
								)
								raise ValueError(_UpperCamelCase )
				return value * ENERGY_CONVERSION[from_type] / ENERGY_CONVERSION[to_type]


if __name__ == "__main__":
	import doctest

	doctest.testmod()
