
"""simple docstring"""
import unittest

from transformers import AlbertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import 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 (
          MODEL_FOR_PRETRAINING_MAPPING,
          AlbertForMaskedLM,
          AlbertForMultipleChoice,
          AlbertForPreTraining,
          AlbertForQuestionAnswering,
          AlbertForSequenceClassification,
          AlbertForTokenClassification,
          AlbertModel,
      )
      from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST
class 			UpperCamelCase      :
     def __init__(     self  ,     snake_case__  ,     snake_case__=13  ,     snake_case__=7  ,     snake_case__=True  ,     snake_case__=True  ,     snake_case__=True  ,     snake_case__=True  ,     snake_case__=99  ,     snake_case__=16  ,     snake_case__=36  ,     snake_case__=6  ,     snake_case__=6  ,     snake_case__=6  ,     snake_case__=37  ,     snake_case__="gelu"  ,     snake_case__=0.1  ,     snake_case__=0.1  ,     snake_case__=512  ,     snake_case__=16  ,     snake_case__=2  ,     snake_case__=0.02  ,     snake_case__=3  ,     snake_case__=4  ,     snake_case__=None  ,     ):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				List[str]							    = parent
       _SCREAMING_SNAKE_CASE   :				int							    = batch_size
       _SCREAMING_SNAKE_CASE   :				str							    = seq_length
       _SCREAMING_SNAKE_CASE   :				Dict							    = is_training
       _SCREAMING_SNAKE_CASE   :				Dict							    = use_input_mask
       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = use_token_type_ids
       _SCREAMING_SNAKE_CASE   :				str							    = use_labels
       _SCREAMING_SNAKE_CASE   :				str							    = vocab_size
       _SCREAMING_SNAKE_CASE   :				Dict							    = embedding_size
       _SCREAMING_SNAKE_CASE   :				Tuple							    = hidden_size
       _SCREAMING_SNAKE_CASE   :				Tuple							    = num_hidden_layers
       _SCREAMING_SNAKE_CASE   :				str							    = num_hidden_groups
       _SCREAMING_SNAKE_CASE   :				Dict							    = num_attention_heads
       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = intermediate_size
       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = hidden_act
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = hidden_dropout_prob
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = attention_probs_dropout_prob
       _SCREAMING_SNAKE_CASE   :				int							    = max_position_embeddings
       _SCREAMING_SNAKE_CASE   :				List[str]							    = type_vocab_size
       _SCREAMING_SNAKE_CASE   :				Any							    = type_sequence_label_size
       _SCREAMING_SNAKE_CASE   :				Dict							    = initializer_range
       _SCREAMING_SNAKE_CASE   :				Dict							    = num_labels
       _SCREAMING_SNAKE_CASE   :				int							    = num_choices
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = scope
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Dict							    = ids_tensor([self.batch_size, self.seq_length]  ,     self.vocab_size				)

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

       _SCREAMING_SNAKE_CASE   :				Dict							    = None
       if self.use_token_type_ids:
         _SCREAMING_SNAKE_CASE   :				Any							    = ids_tensor([self.batch_size, self.seq_length]  ,     self.type_vocab_size				)

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

       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = self.get_config()

       return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       return AlbertConfig(
           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  ,     hidden_dropout_prob=self.hidden_dropout_prob  ,     attention_probs_dropout_prob=self.attention_probs_dropout_prob  ,     max_position_embeddings=self.max_position_embeddings  ,     type_vocab_size=self.type_vocab_size  ,     initializer_range=self.initializer_range  ,     num_hidden_groups=self.num_hidden_groups  ,     )
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = AlbertModel(config=snake_case__				)
       model.to(snake_case__				)
       model.eval()
       _SCREAMING_SNAKE_CASE   :				Any							    = model(snake_case__  ,     attention_mask=snake_case__  ,     token_type_ids=snake_case__				)
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = model(snake_case__  ,     token_type_ids=snake_case__				)
       _SCREAMING_SNAKE_CASE   :				str							    = model(snake_case__				)
       self.parent.assertEqual(result.last_hidden_state.shape  ,     (self.batch_size, self.seq_length, self.hidden_size)				)
       self.parent.assertEqual(result.pooler_output.shape  ,     (self.batch_size, self.hidden_size)				)
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Dict							    = AlbertForPreTraining(config=snake_case__				)
       model.to(snake_case__				)
       model.eval()
       _SCREAMING_SNAKE_CASE   :				List[str]							    = model(
           snake_case__  ,     attention_mask=snake_case__  ,     token_type_ids=snake_case__  ,     labels=snake_case__  ,     sentence_order_label=snake_case__  ,     )
       self.parent.assertEqual(result.prediction_logits.shape  ,     (self.batch_size, self.seq_length, self.vocab_size)				)
       self.parent.assertEqual(result.sop_logits.shape  ,     (self.batch_size, config.num_labels)				)
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = AlbertForMaskedLM(config=snake_case__				)
       model.to(snake_case__				)
       model.eval()
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = model(snake_case__  ,     attention_mask=snake_case__  ,     token_type_ids=snake_case__  ,     labels=snake_case__				)
       self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.seq_length, self.vocab_size)				)
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				List[Any]							    = AlbertForQuestionAnswering(config=snake_case__				)
       model.to(snake_case__				)
       model.eval()
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = model(
           snake_case__  ,     attention_mask=snake_case__  ,     token_type_ids=snake_case__  ,     start_positions=snake_case__  ,     end_positions=snake_case__  ,     )
       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  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = self.num_labels
       _SCREAMING_SNAKE_CASE   :				Tuple							    = AlbertForSequenceClassification(snake_case__				)
       model.to(snake_case__				)
       model.eval()
       _SCREAMING_SNAKE_CASE   :				Any							    = model(snake_case__  ,     attention_mask=snake_case__  ,     token_type_ids=snake_case__  ,     labels=snake_case__				)
       self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.num_labels)				)
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				str							    = self.num_labels
       _SCREAMING_SNAKE_CASE   :				int							    = AlbertForTokenClassification(config=snake_case__				)
       model.to(snake_case__				)
       model.eval()
       _SCREAMING_SNAKE_CASE   :				Tuple							    = model(snake_case__  ,     attention_mask=snake_case__  ,     token_type_ids=snake_case__  ,     labels=snake_case__				)
       self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.seq_length, self.num_labels)				)
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				int							    = self.num_choices
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = AlbertForMultipleChoice(config=snake_case__				)
       model.to(snake_case__				)
       model.eval()
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = input_ids.unsqueeze(1				).expand(-1  ,     self.num_choices  ,     -1				).contiguous()
       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = token_type_ids.unsqueeze(1				).expand(-1  ,     self.num_choices  ,     -1				).contiguous()
       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = input_mask.unsqueeze(1				).expand(-1  ,     self.num_choices  ,     -1				).contiguous()
       _SCREAMING_SNAKE_CASE   :				Tuple							    = model(
           snake_case__  ,     attention_mask=snake_case__  ,     token_type_ids=snake_case__  ,     labels=snake_case__  ,     )
       self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.num_choices)				)




     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Tuple							    = self.prepare_config_and_inputs()
       (
           (
           _SCREAMING_SNAKE_CASE
       )    ,		(
           _SCREAMING_SNAKE_CASE
       )    ,		(
           _SCREAMING_SNAKE_CASE
       )    ,		(
           _SCREAMING_SNAKE_CASE
       )    ,		(
           _SCREAMING_SNAKE_CASE
       )    ,		(
           _SCREAMING_SNAKE_CASE
       )    ,		(
           _SCREAMING_SNAKE_CASE
       )    ,		
       )   :				List[str]							    = config_and_inputs
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
       return config, inputs_dict
@require_torch
class 			UpperCamelCase      (							__SCREAMING_SNAKE_CASE      ,       __SCREAMING_SNAKE_CASE      ,       unittest.TestCase  ):
     A__      =			(
         (
             AlbertModel,
             AlbertForPreTraining,
             AlbertForMaskedLM,
             AlbertForMultipleChoice,
             AlbertForSequenceClassification,
             AlbertForTokenClassification,
             AlbertForQuestionAnswering,
         )
         if is_torch_available()
         else ()
     )
     A__      =			(
         {
             """feature-extraction""": AlbertModel,
             """fill-mask""": AlbertForMaskedLM,
             """question-answering""": AlbertForQuestionAnswering,
             """text-classification""": AlbertForSequenceClassification,
             """token-classification""": AlbertForTokenClassification,
             """zero-shot""": AlbertForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     A__      =			True
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__  ,     snake_case__  ,     snake_case__=False				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				List[Any]							    = super()._prepare_for_class(snake_case__  ,     snake_case__  ,     return_labels=snake_case__				)

       if return_labels:
         if model_class in get_values(snake_case__				):
           _SCREAMING_SNAKE_CASE   :				str							    = torch.zeros(
               (self.model_tester.batch_size, self.model_tester.seq_length)  ,     dtype=torch.long  ,     device=snake_case__				)
           _SCREAMING_SNAKE_CASE   :				Any							    = torch.zeros(
               self.model_tester.batch_size  ,     dtype=torch.long  ,     device=snake_case__				)
       return inputs_dict
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = AlbertModelTester(self				)
       _SCREAMING_SNAKE_CASE   :				Any							    = ConfigTester(self  ,     config_class=snake_case__  ,     hidden_size=37				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


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




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Any							    = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*snake_case__				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				List[str]							    = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_pretraining(*snake_case__				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Tuple							    = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*snake_case__				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Any							    = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_multiple_choice(*snake_case__				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				int							    = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*snake_case__				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				List[str]							    = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*snake_case__				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				List[str]							    = self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
         _SCREAMING_SNAKE_CASE   :				Tuple							    = type
         self.model_tester.create_and_check_model(*snake_case__				)




     @slow
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _SCREAMING_SNAKE_CASE   :				List[Any]							    = AlbertModel.from_pretrained(snake_case__				)
         self.assertIsNotNone(snake_case__				)
@require_torch
class 			UpperCamelCase      (							unittest.TestCase  ):
     @slow
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				List[Any]							    = AlbertModel.from_pretrained("albert-base-v2"				)
       _SCREAMING_SNAKE_CASE   :				str							    = torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]				)
       _SCREAMING_SNAKE_CASE   :				str							    = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
       with torch.no_grad():
         _SCREAMING_SNAKE_CASE   :				str							    = model(snake_case__  ,     attention_mask=snake_case__				)[0]
       _SCREAMING_SNAKE_CASE   :				List[str]							    = torch.Size((1, 11, 768)				)
       self.assertEqual(output.shape  ,     snake_case__				)
       _SCREAMING_SNAKE_CASE   :				Dict							    = torch.tensor(
           [[[-0.6_513, 1.5_035, -0.2_766], [-0.6_515, 1.5_046, -0.2_780], [-0.6_512, 1.5_049, -0.2_784]]]				)

       self.assertTrue(torch.allclose(output[:, 1:4, 1:4]  ,     snake_case__  ,     atol=1E-4				)				)





"""simple docstring"""
from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow


if is_tf_available():
      import numpy as np
      import tensorflow as tf

      from transformers import TFCamembertModel
@require_tf
@require_sentencepiece
@require_tokenizers
class 			UpperCamelCase      (							unittest.TestCase  ):
     @slow
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				str							    = TFCamembertModel.from_pretrained("jplu/tf-camembert-base"				)

       _SCREAMING_SNAKE_CASE   :				str							    = tf.convert_to_tensor(
           [[5, 121, 11, 660, 16, 730, 25543, 110, 83, 6]]  ,     dtype=tf.intaa  ,     )  # J'aime le camembert !"

       _SCREAMING_SNAKE_CASE   :				str							    = model(snake_case__				)["last_hidden_state"]
       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = tf.TensorShape((1, 10, 768)				)
       self.assertEqual(output.shape  ,     snake_case__				)
       # compare the actual values for a slice.
       _SCREAMING_SNAKE_CASE   :				int							    = tf.convert_to_tensor(
           [[[-0.0_254, 0.0_235, 0.1_027], [0.0_606, -0.1_811, -0.0_418], [-0.1_561, -0.1_127, 0.2_687]]]  ,     dtype=tf.floataa  ,     )
       # camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0')
       # camembert.eval()
       # expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach()

       self.assertTrue(np.allclose(output[:, :3, :3].numpy()  ,     expected_slice.numpy()  ,     atol=1E-4				)				)



