




"""simple docstring"""

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



 '''simple docstring'''

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

 def      lowerCAmelCase_				(					self			:			int						):
       _A     =  ids_tensor([self.batch_size, self.seq_length]       ,				self.vocab_size						)

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

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

       _A     =  self.get_config()

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

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

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

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

 def      lowerCAmelCase_				(					self			:			Tuple       ,				_UpperCAmelCase			:			int       ,				_UpperCAmelCase			:			Any       ,				_UpperCAmelCase			:			Any       ,				_UpperCAmelCase			:			List[Any]       ,				_UpperCAmelCase			:			List[Any]       ,				_UpperCAmelCase			:			Union[str, Any]						):
       _A     =  self.num_labels
       _A     =  SqueezeBertForTokenClassification(config=_UpperCAmelCase						)
       model.to(_UpperCAmelCase						)
       model.eval()

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

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



 def      lowerCAmelCase_				(					self			:			List[Any]						):
       _A     =  self.prepare_config_and_inputs()
       ((_A)  ,       (_A)  ,       (_A)  ,       (_A)  ,       (_A)  ,       (_A))     =  config_and_inputs
       _A     =  {'input_ids': input_ids, 'attention_mask': input_mask}
       return config, inputs_dict





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



 '''simple docstring'''
 UpperCAmelCase       :	Dict     =      (
     (
         SqueezeBertModel,
         SqueezeBertForMaskedLM,
         SqueezeBertForMultipleChoice,
         SqueezeBertForQuestionAnswering,
         SqueezeBertForSequenceClassification,
         SqueezeBertForTokenClassification,
     )
     if is_torch_available()
     else None
 )
 UpperCAmelCase       :	List[str]     =      (
     {
         '''feature-extraction''': SqueezeBertModel,
         '''fill-mask''': SqueezeBertForMaskedLM,
         '''question-answering''': SqueezeBertForQuestionAnswering,
         '''text-classification''': SqueezeBertForSequenceClassification,
         '''token-classification''': SqueezeBertForTokenClassification,
         '''zero-shot''': SqueezeBertForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 UpperCAmelCase       :	int     =      False
 UpperCAmelCase       :	Union[str, Any]     =      True
 UpperCAmelCase       :	Tuple     =      False

 def      lowerCAmelCase_				(					self			:			str						):
       _A     =  SqueezeBertModelTester(self						)
       _A     =  ConfigTester(self       ,				config_class=_UpperCAmelCase       ,				dim=37						)

 def      lowerCAmelCase_				(					self			:			Dict						):
       self.config_tester.run_common_tests()

 def      lowerCAmelCase_				(					self			:			Tuple						):
       _A     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_model(*_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			int						):
       _A     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_for_masked_lm(*_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			Dict						):
       _A     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_for_question_answering(*_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			Any						):
       _A     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_for_sequence_classification(*_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			Tuple						):
       _A     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_for_token_classification(*_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			Tuple						):
       _A     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_for_multiple_choice(*_UpperCAmelCase						)



 @slow
 def      lowerCAmelCase_				(					self			:			List[Any]						):
       for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _A     =  SqueezeBertModel.from_pretrained(_UpperCAmelCase						)
             self.assertIsNotNone(_UpperCAmelCase						)





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



 '''simple docstring'''

 @slow
 def      lowerCAmelCase_				(					self			:			Tuple						):
       _A     =  SqueezeBertForSequenceClassification.from_pretrained('squeezebert/squeezebert-mnli'						)

       _A     =  torch.tensor([[1, 29_414, 232, 328, 740, 1_140, 12_695, 69, 13, 1_588, 2]]						)
       _A     =  model(_UpperCAmelCase						)[0]
       _A     =  torch.Size((1, 3)						)
       self.assertEqual(output.shape       ,				_UpperCAmelCase						)
       _A     =  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						)

