




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



				'''simple docstring'''





				def __init__(      self							:   Tuple      ,							__magic_name__							:   List[Any]      ,							__magic_name__							:   str=13      ,							__magic_name__							:   Dict=7      ,							__magic_name__							:   Union[str, Any]=True      ,							__magic_name__							:   Union[str, Any]=True      ,							__magic_name__							:   Dict=False      ,							__magic_name__							:   List[str]=True      ,							__magic_name__							:   Tuple=99      ,							__magic_name__							:   Union[str, Any]=32      ,							__magic_name__							:   Tuple=5      ,							__magic_name__							:   str=4      ,							__magic_name__							:   Optional[int]=64      ,							__magic_name__							:   Any="gelu"      ,							__magic_name__							:   List[str]=0.1      ,							__magic_name__							:   Optional[int]=0.1      ,							__magic_name__							:   Union[str, Any]=512      ,							__magic_name__							:   Optional[int]=16      ,							__magic_name__							:   Tuple=2      ,							__magic_name__							:   Dict=0.02      ,							__magic_name__							:   Tuple=3      ,							__magic_name__							:   List[str]=4      ,							__magic_name__							:   List[Any]=None      ,							__magic_name__							:   str=2      ,							__magic_name__							:   List[Any]=2      ,							__magic_name__							:   Any=2      ,							__magic_name__							:   List[str]=2      ,							__magic_name__							:   Dict=4      ,							__magic_name__							:   str=1      ,							)							->			Dict:
											lowerCamelCase_			:    Tuple   						=      parent
											lowerCamelCase_			:    List[Any]   						=      batch_size
											lowerCamelCase_			:    Optional[int]   						=      seq_length
											lowerCamelCase_			:    Dict   						=      is_training
											lowerCamelCase_			:    List[Any]   						=      use_input_mask
											lowerCamelCase_			:    List[str]   						=      use_token_type_ids
											lowerCamelCase_			:    int   						=      use_labels
											lowerCamelCase_			:    int   						=      vocab_size
											lowerCamelCase_			:    List[str]   						=      hidden_size
											lowerCamelCase_			:    List[str]   						=      num_hidden_layers
											lowerCamelCase_			:    Dict   						=      num_attention_heads
											lowerCamelCase_			:    Any   						=      intermediate_size
											lowerCamelCase_			:    Tuple   						=      hidden_act
											lowerCamelCase_			:    int   						=      hidden_dropout_prob
											lowerCamelCase_			:    List[str]   						=      attention_probs_dropout_prob
											lowerCamelCase_			:    Any   						=      max_position_embeddings
											lowerCamelCase_			:    str   						=      type_vocab_size
											lowerCamelCase_			:    List[Any]   						=      type_sequence_label_size
											lowerCamelCase_			:    Dict   						=      initializer_range
											lowerCamelCase_			:    Optional[int]   						=      num_labels
											lowerCamelCase_			:    int   						=      num_choices
											lowerCamelCase_			:    int   						=      scope
											lowerCamelCase_			:    List[Any]   						=      q_groups
											lowerCamelCase_			:    Tuple   						=      k_groups
											lowerCamelCase_			:    Union[str, Any]   						=      v_groups
											lowerCamelCase_			:    Any   						=      post_attention_groups
											lowerCamelCase_			:    Dict   						=      intermediate_groups
											lowerCamelCase_			:    Dict   						=      output_groups





				def    __SCREAMING_SNAKE_CASE     (      self							:   int							)							->			Dict:
											lowerCamelCase_			:    int   						=      ids_tensor([self.batch_size, self.seq_length]      ,							self.vocab_size							)

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

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

											lowerCamelCase_			:    Union[str, Any]   						=      self.get_config()

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





				def    __SCREAMING_SNAKE_CASE     (      self							:   List[str]							)							->			Any:
											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    __SCREAMING_SNAKE_CASE     (      self							:   List[str]      ,							__magic_name__							:   Optional[int]      ,							__magic_name__							:   Optional[Any]      ,							__magic_name__							:   str      ,							__magic_name__							:   Optional[Any]      ,							__magic_name__							:   Union[str, Any]      ,							__magic_name__							:   Union[str, Any]							)							->			Union[str, Any]:
											lowerCamelCase_			:    List[str]   						=      SqueezeBertModel(config=_UpperCAmelCase							)
											model.to(_UpperCAmelCase							)
											model.eval()
											lowerCamelCase_			:    Union[str, Any]   						=      model(_UpperCAmelCase      ,							_UpperCAmelCase							)
											lowerCamelCase_			:    Any   						=      model(_UpperCAmelCase							)
											self.parent.assertEqual(result.last_hidden_state.shape      ,							(self.batch_size, self.seq_length, self.hidden_size)							)





				def    __SCREAMING_SNAKE_CASE     (      self							:   List[str]      ,							__magic_name__							:   Optional[Any]      ,							__magic_name__							:   Tuple      ,							__magic_name__							:   Optional[int]      ,							__magic_name__							:   List[Any]      ,							__magic_name__							:   int      ,							__magic_name__							:   Optional[Any]							)							->			Optional[int]:
											lowerCamelCase_			:    Dict   						=      SqueezeBertForMaskedLM(config=_UpperCAmelCase							)
											model.to(_UpperCAmelCase							)
											model.eval()
											lowerCamelCase_			:    str   						=      model(_UpperCAmelCase      ,							attention_mask=_UpperCAmelCase      ,							labels=_UpperCAmelCase							)
											self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.seq_length, self.vocab_size)							)





				def    __SCREAMING_SNAKE_CASE     (      self							:   Optional[Any]      ,							__magic_name__							:   Optional[Any]      ,							__magic_name__							:   Any      ,							__magic_name__							:   Any      ,							__magic_name__							:   Any      ,							__magic_name__							:   Tuple      ,							__magic_name__							:   str							)							->			Tuple:
											lowerCamelCase_			:    Union[str, Any]   						=      SqueezeBertForQuestionAnswering(config=_UpperCAmelCase							)
											model.to(_UpperCAmelCase							)
											model.eval()
											lowerCamelCase_			:    List[Any]   						=      model(
											    _UpperCAmelCase      ,							attention_mask=_UpperCAmelCase      ,							start_positions=_UpperCAmelCase      ,							end_positions=_UpperCAmelCase							)
											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    __SCREAMING_SNAKE_CASE     (      self							:   Tuple      ,							__magic_name__							:   List[str]      ,							__magic_name__							:   str      ,							__magic_name__							:   List[str]      ,							__magic_name__							:   int      ,							__magic_name__							:   List[Any]      ,							__magic_name__							:   int							)							->			Optional[int]:
											lowerCamelCase_			:    Dict   						=      self.num_labels
											lowerCamelCase_			:    List[str]   						=      SqueezeBertForSequenceClassification(_UpperCAmelCase							)
											model.to(_UpperCAmelCase							)
											model.eval()
											lowerCamelCase_			:    Tuple   						=      model(_UpperCAmelCase      ,							attention_mask=_UpperCAmelCase      ,							labels=_UpperCAmelCase							)
											self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.num_labels)							)





				def    __SCREAMING_SNAKE_CASE     (      self							:   Tuple      ,							__magic_name__							:   int      ,							__magic_name__							:   Any      ,							__magic_name__							:   Any      ,							__magic_name__							:   List[Any]      ,							__magic_name__							:   List[Any]      ,							__magic_name__							:   Union[str, Any]							)							->			Optional[int]:
											lowerCamelCase_			:    Optional[int]   						=      self.num_labels
											lowerCamelCase_			:    int   						=      SqueezeBertForTokenClassification(config=_UpperCAmelCase							)
											model.to(_UpperCAmelCase							)
											model.eval()

											lowerCamelCase_			:    List[str]   						=      model(_UpperCAmelCase      ,							attention_mask=_UpperCAmelCase      ,							labels=_UpperCAmelCase							)
											self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.seq_length, self.num_labels)							)





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





				def    __SCREAMING_SNAKE_CASE     (      self							:   List[Any]							)							->			Union[str, Any]:
											lowerCamelCase_			:    Optional[int]   						=      self.prepare_config_and_inputs()
											((lowerCamelCase_)						,						(lowerCamelCase_)						,						(lowerCamelCase_)						,						(lowerCamelCase_)						,						(lowerCamelCase_)						,						(lowerCamelCase_))			:    Optional[int]   						=      config_and_inputs
											lowerCamelCase_			:    Optional[int]   						=      {"input_ids": input_ids, "attention_mask": input_mask}
											return config, inputs_dict





@require_torch
class 					snake_case_   (     __lowerCAmelCase   ,	__lowerCAmelCase   ,	unittest.TestCase	):



				'''simple docstring'''
				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    __SCREAMING_SNAKE_CASE     (      self							:   str							)							->			Tuple:
											lowerCamelCase_			:    int   						=      SqueezeBertModelTester(self							)
											lowerCamelCase_			:    Optional[Any]   						=      ConfigTester(self      ,							config_class=_UpperCAmelCase      ,							dim=37							)





				def    __SCREAMING_SNAKE_CASE     (      self							:   Dict							)							->			str:
											self.config_tester.run_common_tests()





				def    __SCREAMING_SNAKE_CASE     (      self							:   Tuple							)							->			Optional[Any]:
											lowerCamelCase_			:    Any   						=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_squeezebert_model(*_UpperCAmelCase							)





				def    __SCREAMING_SNAKE_CASE     (      self							:   int							)							->			Tuple:
											lowerCamelCase_			:    str   						=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_squeezebert_for_masked_lm(*_UpperCAmelCase							)





				def    __SCREAMING_SNAKE_CASE     (      self							:   Dict							)							->			List[Any]:
											lowerCamelCase_			:    str   						=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_squeezebert_for_question_answering(*_UpperCAmelCase							)





				def    __SCREAMING_SNAKE_CASE     (      self							:   Any							)							->			List[Any]:
											lowerCamelCase_			:    Tuple   						=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_squeezebert_for_sequence_classification(*_UpperCAmelCase							)





				def    __SCREAMING_SNAKE_CASE     (      self							:   Tuple							)							->			Union[str, Any]:
											lowerCamelCase_			:    List[str]   						=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_squeezebert_for_token_classification(*_UpperCAmelCase							)





				def    __SCREAMING_SNAKE_CASE     (      self							:   Tuple							)							->			List[Any]:
											lowerCamelCase_			:    Optional[int]   						=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_squeezebert_for_multiple_choice(*_UpperCAmelCase							)





				@slow
				def    __SCREAMING_SNAKE_CASE     (      self							:   List[Any]							)							->			List[str]:
											for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		lowerCamelCase_			:    Tuple   						=      SqueezeBertModel.from_pretrained(_UpperCAmelCase							)
																		self.assertIsNotNone(_UpperCAmelCase							)





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



				'''simple docstring'''





				@slow
				def    __SCREAMING_SNAKE_CASE     (      self							:   Tuple							)							->			Dict:
											lowerCamelCase_			:    Any   						=      SqueezeBertForSequenceClassification.from_pretrained("squeezebert/squeezebert-mnli"							)

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







"""simple docstring"""

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


a							      =     logging.get_logger(__name__)

a							      =     {
    '''bigcode/gpt_bigcode-santacoder''': '''https://huggingface.co/bigcode/gpt_bigcode-santacoder/resolve/main/config.json''',
}





class 							lowercase_			(	__lowerCAmelCase				):



 '''simple docstring'''
 UpperCAmelCase       :	int     =      '''gpt_bigcode'''
 UpperCAmelCase       :	str     =      ['''past_key_values''']
 UpperCAmelCase       :	Dict     =      {
     '''hidden_size''': '''n_embd''',
     '''max_position_embeddings''': '''n_positions''',
     '''num_attention_heads''': '''n_head''',
     '''num_hidden_layers''': '''n_layer''',
 }

 def __init__(					self			:			Tuple       ,				_UpperCAmelCase			:			Dict=50_257       ,				_UpperCAmelCase			:			List[Any]=1_024       ,				_UpperCAmelCase			:			Any=768       ,				_UpperCAmelCase			:			int=12       ,				_UpperCAmelCase			:			Any=12       ,				_UpperCAmelCase			:			Tuple=None       ,				_UpperCAmelCase			:			str="gelu_pytorch_tanh"       ,				_UpperCAmelCase			:			str=0.1       ,				_UpperCAmelCase			:			Optional[Any]=0.1       ,				_UpperCAmelCase			:			Tuple=0.1       ,				_UpperCAmelCase			:			List[Any]=1E-5       ,				_UpperCAmelCase			:			List[Any]=0.02       ,				_UpperCAmelCase			:			Optional[Any]=True       ,				_UpperCAmelCase			:			Dict=True       ,				_UpperCAmelCase			:			List[Any]=50_256       ,				_UpperCAmelCase			:			Dict=50_256       ,				_UpperCAmelCase			:			int=True       ,				_UpperCAmelCase			:			Union[str, Any]=True       ,				_UpperCAmelCase			:			Any=True       ,				**_UpperCAmelCase			:			Any       ,				):
       _A     =  vocab_size
       _A     =  n_positions
       _A     =  n_embd
       _A     =  n_layer
       _A     =  n_head
       _A     =  n_inner
       _A     =  activation_function
       _A     =  resid_pdrop
       _A     =  embd_pdrop
       _A     =  attn_pdrop
       _A     =  layer_norm_epsilon
       _A     =  initializer_range
       _A     =  scale_attn_weights
       _A     =  use_cache
       _A     =  attention_softmax_in_fpaa
       _A     =  scale_attention_softmax_in_fpaa
       _A     =  multi_query

       _A     =  bos_token_id
       _A     =  eos_token_id

       super().__init__(bos_token_id=_UpperCAmelCase       ,				eos_token_id=_UpperCAmelCase       ,				**_UpperCAmelCase						)

