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    (     UpperCAmelCase__			):






    '''simple docstring'''




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




    def      _a    (    self ):
       lowerCAmelCase_:							List[str]       =	ids_tensor([self.batch_size, self.seq_length]       ,						self.vocab_size )

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

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

       lowerCAmelCase_:							List[Any]       =	self.get_config()

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




    def      _a    (    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      _a    (    self       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__ ):
       lowerCAmelCase_:							str       =	SqueezeBertModel(config=lowerCamelCase__ )
       model.to(lowerCamelCase__ )
       model.eval()
       lowerCAmelCase_:							Dict       =	model(lowerCamelCase__       ,						lowerCamelCase__ )
       lowerCAmelCase_:							int       =	model(lowerCamelCase__ )
       self.parent.assertEqual(result.last_hidden_state.shape       ,						(self.batch_size, self.seq_length, self.hidden_size) )




    def      _a    (    self       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__ ):
       lowerCAmelCase_:							Union[str, Any]       =	SqueezeBertForMaskedLM(config=lowerCamelCase__ )
       model.to(lowerCamelCase__ )
       model.eval()
       lowerCAmelCase_:							Any       =	model(lowerCamelCase__       ,						attention_mask=lowerCamelCase__       ,						labels=lowerCamelCase__ )
       self.parent.assertEqual(result.logits.shape       ,						(self.batch_size, self.seq_length, self.vocab_size) )




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




    def      _a    (    self       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__       ,						lowerCamelCase__ ):
       lowerCAmelCase_:							Any       =	self.num_labels
       lowerCAmelCase_:							str       =	SqueezeBertForTokenClassification(config=lowerCamelCase__ )
       model.to(lowerCamelCase__ )
       model.eval()

       lowerCAmelCase_:							Optional[int]       =	model(lowerCamelCase__       ,						attention_mask=lowerCamelCase__       ,						labels=lowerCamelCase__ )
       self.parent.assertEqual(result.logits.shape       ,						(self.batch_size, self.seq_length, self.num_labels) )




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




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





@require_torch
class 					_lowercase    (     UpperCAmelCase__ ,  UpperCAmelCase__ ,  unittest.TestCase			):






    '''simple docstring'''
    SCREAMING_SNAKE_CASE:				Union[str, Any]          =    (
        (
            SqueezeBertModel,
            SqueezeBertForMaskedLM,
            SqueezeBertForMultipleChoice,
            SqueezeBertForQuestionAnswering,
            SqueezeBertForSequenceClassification,
            SqueezeBertForTokenClassification,
        )
        if is_torch_available()
        else None
    )
    SCREAMING_SNAKE_CASE:				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 {}
    )
    SCREAMING_SNAKE_CASE:				Union[str, Any]          =    False
    SCREAMING_SNAKE_CASE:				int          =    True
    SCREAMING_SNAKE_CASE:				Dict          =    False




    def      _a    (    self ):
       lowerCAmelCase_:							Tuple       =	SqueezeBertModelTester(self )
       lowerCAmelCase_:							Any       =	ConfigTester(self       ,						config_class=lowerCamelCase__       ,						dim=37 )




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




    def      _a    (    self ):
       lowerCAmelCase_:							int       =	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_model(*lowerCamelCase__ )




    def      _a    (    self ):
       lowerCAmelCase_:							Optional[int]       =	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_for_masked_lm(*lowerCamelCase__ )




    def      _a    (    self ):
       lowerCAmelCase_:							Optional[int]       =	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_for_question_answering(*lowerCamelCase__ )




    def      _a    (    self ):
       lowerCAmelCase_:							Tuple       =	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_for_sequence_classification(*lowerCamelCase__ )




    def      _a    (    self ):
       lowerCAmelCase_:							Dict       =	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_for_token_classification(*lowerCamelCase__ )




    def      _a    (    self ):
       lowerCAmelCase_:							List[str]       =	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_squeezebert_for_multiple_choice(*lowerCamelCase__ )




    @slow
    def      _a    (    self ):
       for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowerCAmelCase_:							Optional[int]       =	SqueezeBertModel.from_pretrained(lowerCamelCase__ )
          self.assertIsNotNone(lowerCamelCase__ )





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






    '''simple docstring'''




    @slow
    def      _a    (    self ):
       lowerCAmelCase_:							int       =	SqueezeBertForSequenceClassification.from_pretrained("squeezebert/squeezebert-mnli" )

       lowerCAmelCase_:							List[str]       =	torch.tensor([[1, 29_414, 232, 328, 740, 1_140, 12_695, 69, 13, 1_588, 2]] )
       lowerCAmelCase_:							Dict       =	model(lowerCamelCase__ )[0]
       lowerCAmelCase_:							Any       =	torch.Size((1, 3) )
       self.assertEqual(output.shape       ,						lowerCamelCase__ )
       lowerCAmelCase_:							List[str]       =	torch.tensor([[0.6_4_0_1, -0.0_3_4_9, -0.6_0_4_1]] )
       self.assertTrue(torch.allclose(lowerCamelCase__       ,						lowerCamelCase__       ,						atol=1E-4 ) )
from ...configuration_utils import PretrainedConfig
from ...utils import logging


a  :      List[Any]            =    logging.get_logger(__name__)

a  :      Union[str, Any]            =    {
    """bigcode/gpt_bigcode-santacoder""": """https://huggingface.co/bigcode/gpt_bigcode-santacoder/resolve/main/config.json""",
}

class 					_lowercase    (     UpperCAmelCase__			):






    '''simple docstring'''
    SCREAMING_SNAKE_CASE:				List[str]          =    'gpt_bigcode'
    SCREAMING_SNAKE_CASE:				Union[str, Any]          =    ['past_key_values']
    SCREAMING_SNAKE_CASE:				Tuple          =    {
        'hidden_size': 'n_embd',
        'max_position_embeddings': 'n_positions',
        'num_attention_heads': 'n_head',
        'num_hidden_layers': 'n_layer',
    }




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

       lowerCAmelCase_:							Union[str, Any]       =	bos_token_id
       lowerCAmelCase_:							List[str]       =	eos_token_id

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