





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 ,							)					->       Dict:


							'''simple docstring'''



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






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


							'''simple docstring'''



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

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

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

							snake_case_						:   Tuple           =  self.get_config()

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






			def       lowerCamelCase  (self   )					->       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       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__   )					->       Union[str, Any]:


							'''simple docstring'''



							snake_case_						:   Any           =  SqueezeBertModel(config=__magic_name__   )
							model.to(__magic_name__   )
							model.eval()
							snake_case_						:   Optional[int]           =  model(__magic_name__ ,							__magic_name__   )
							snake_case_						:   str           =  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_						:   Dict           =  SqueezeBertForMaskedLM(config=__magic_name__   )
							model.to(__magic_name__   )
							model.eval()
							snake_case_						:   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__   )					->       Dict:


							'''simple docstring'''



							snake_case_						:   Tuple           =  SqueezeBertForQuestionAnswering(config=__magic_name__   )
							model.to(__magic_name__   )
							model.eval()
							snake_case_						:   List[str]           =  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__   )					->       List[str]:


							'''simple docstring'''



							snake_case_						:   Any           =  self.num_labels
							snake_case_						:   Tuple           =  SqueezeBertForSequenceClassification(__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.num_labels)   )






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


							'''simple docstring'''



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

							snake_case_						:   Union[str, 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__   )					->       int:


							'''simple docstring'''



							snake_case_						:   List[Any]           =  self.num_choices
							snake_case_						:   Dict           =  SqueezeBertForMultipleChoice(config=__magic_name__   )
							model.to(__magic_name__   )
							model.eval()
							snake_case_						:   Dict           =  input_ids.unsqueeze(1   ).expand(-1 ,							self.num_choices ,							-1   ).contiguous()
							snake_case_						:   List[Any]           =  input_mask.unsqueeze(1   ).expand(-1 ,							self.num_choices ,							-1   ).contiguous()
							snake_case_						:   str           =  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   )					->       List[str]:


							'''simple docstring'''



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




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






			def       lowerCamelCase  (self   )					->       Any:


							'''simple docstring'''



							snake_case_						:   List[str]           =  SqueezeBertModelTester(self   )
							snake_case_						:   Tuple           =  ConfigTester(self ,							config_class=__magic_name__ ,							dim=37   )






			def       lowerCamelCase  (self   )					->       Tuple:


							'''simple docstring'''



							self.config_tester.run_common_tests()






			def       lowerCamelCase  (self   )					->       Dict:


							'''simple docstring'''



							snake_case_						:   int           =  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_						:   Optional[int]           =  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   )					->       int:


							'''simple docstring'''



							snake_case_						:   Optional[int]           =  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_squeezebert_for_sequence_classification(*__magic_name__   )






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


							'''simple docstring'''



							snake_case_						:   Optional[int]           =  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_squeezebert_for_token_classification(*__magic_name__   )






			def       lowerCamelCase  (self   )					->       Dict:


							'''simple docstring'''



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






			@slow
			def       lowerCamelCase  (self   )					->       List[str]:


							'''simple docstring'''



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




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






			@slow
			def       lowerCamelCase  (self   )					->       Any:


							'''simple docstring'''



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

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







from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase_    =    logging.get_logger(__name__)

lowerCAmelCase_    =    {
    '''microsoft/biogpt''': '''https://huggingface.co/microsoft/biogpt/resolve/main/config.json''',
    # See all BioGPT models at https://huggingface.co/models?filter=biogpt
}



class  __lowerCAmelCase	(      _a					):
			lowerCamelCase_					:					Any	      =				'''biogpt'''






			def __init__(self ,							__magic_name__=4_2384 ,							__magic_name__=1024 ,							__magic_name__=24 ,							__magic_name__=16 ,							__magic_name__=4096 ,							__magic_name__="gelu" ,							__magic_name__=0.1 ,							__magic_name__=0.1 ,							__magic_name__=1024 ,							__magic_name__=0.02 ,							__magic_name__=1e-12 ,							__magic_name__=True ,							__magic_name__=True ,							__magic_name__=0.0 ,							__magic_name__=0.0 ,							__magic_name__=1 ,							__magic_name__=0 ,							__magic_name__=2 ,							**__magic_name__ ,							)					->       List[str]:


							'''simple docstring'''



							snake_case_						:   List[str]           =  vocab_size
							snake_case_						:   Dict           =  max_position_embeddings
							snake_case_						:   Optional[int]           =  hidden_size
							snake_case_						:   List[Any]           =  num_hidden_layers
							snake_case_						:   List[str]           =  num_attention_heads
							snake_case_						:   int           =  intermediate_size
							snake_case_						:   List[Any]           =  hidden_act
							snake_case_						:   List[Any]           =  hidden_dropout_prob
							snake_case_						:   Optional[int]           =  attention_probs_dropout_prob
							snake_case_						:   Optional[int]           =  initializer_range
							snake_case_						:   Optional[int]           =  layer_norm_eps
							snake_case_						:   str           =  scale_embedding
							snake_case_						:   Optional[Any]           =  use_cache
							snake_case_						:   Optional[Any]           =  layerdrop
							snake_case_						:   Optional[Any]           =  activation_dropout
							super().__init__(pad_token_id=__magic_name__ ,							bos_token_id=__magic_name__ ,							eos_token_id=__magic_name__ ,							**__magic_name__   )
