






"""simple docstring"""


# coding=utf-8
# Copyright 2023 The HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# this script dumps information about the environment

import os
import platform
import sys


_UpperCamelCase       :					int									=		'3'

print('Python version:', sys.version)

print('OS platform:', platform.platform())
print('OS architecture:', platform.machine())

try:
   import torch

   print('Torch version:', torch.__version__)
   print('Cuda available:', torch.cuda.is_available())
   print('Cuda version:', torch.version.cuda)
   print('CuDNN version:', torch.backends.cudnn.version())
   print('Number of GPUs available:', torch.cuda.device_count())
except ImportError:
   print('Torch version:', None)

try:
   import transformers

   print('transformers version:', transformers.__version__)
except ImportError:
   print('transformers version:', None)












"""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 		snake_case						:





    def __init__(							self			:      Optional[int]       ,	A			:      Optional[Any]       ,	A			:      Tuple=1_3       ,	A			:      Optional[int]=7       ,	A			:      Tuple=True       ,	A			:      Any=True       ,	A			:      Union[str, Any]=True       ,	A			:      Any=True       ,	A			:      List[Any]=9_9       ,	A			:      Optional[int]=1_6       ,	A			:      Tuple=3_6       ,	A			:      str=6       ,	A			:      Tuple=6       ,	A			:      Optional[Any]=6       ,	A			:      Any=3_7       ,	A			:      int="gelu"       ,	A			:      Optional[int]=0.1       ,	A			:      Dict=0.1       ,	A			:      Union[str, Any]=5_1_2       ,	A			:      int=1_6       ,	A			:      int=2       ,	A			:      Tuple=0.02       ,	A			:      Optional[Any]=3       ,	A			:      str=4       ,	A			:      Tuple=None       ,	):


      '''simple docstring'''




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





    def        lowerCamelCase__   (							self			:      Tuple    ):


      '''simple docstring'''




      a  :    List[str]       =     ids_tensor([self.batch_size, self.seq_length]       ,	self.vocab_size    )

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

      a  :    Union[str, Any]       =     None
      if self.use_token_type_ids:
        a  :    Tuple       =     ids_tensor([self.batch_size, self.seq_length]       ,	self.type_vocab_size    )

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

      a  :    int       =     self.get_config()

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





    def        lowerCamelCase__   (							self			:      List[str]    ):


      '''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        lowerCamelCase__   (							self			:      Optional[int]       ,	A			:      Tuple       ,	A			:      Optional[Any]       ,	A			:      Optional[int]       ,	A			:      Dict       ,	A			:      List[str]       ,	A			:      Optional[Any]       ,	A			:      str    ):


      '''simple docstring'''




      a  :    Dict       =     AlbertModel(config=A    )
      model.to(A    )
      model.eval()
      a  :    List[str]       =     model(A       ,	attention_mask=A       ,	token_type_ids=A    )
      a  :    str       =     model(A       ,	token_type_ids=A    )
      a  :    int       =     model(A    )
      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        lowerCamelCase__   (							self			:      List[Any]       ,	A			:      int       ,	A			:      Any       ,	A			:      List[str]       ,	A			:      List[str]       ,	A			:      Optional[int]       ,	A			:      Tuple       ,	A			:      Optional[int]    ):


      '''simple docstring'''




      a  :    int       =     AlbertForPreTraining(config=A    )
      model.to(A    )
      model.eval()
      a  :    Dict       =     model(
          A       ,	attention_mask=A       ,	token_type_ids=A       ,	labels=A       ,	sentence_order_label=A       ,	)
      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        lowerCamelCase__   (							self			:      int       ,	A			:      Tuple       ,	A			:      Any       ,	A			:      Dict       ,	A			:      Optional[int]       ,	A			:      Dict       ,	A			:      int       ,	A			:      str    ):


      '''simple docstring'''




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





    def        lowerCamelCase__   (							self			:      int       ,	A			:      Union[str, Any]       ,	A			:      int       ,	A			:      Dict       ,	A			:      Tuple       ,	A			:      str       ,	A			:      List[Any]       ,	A			:      str    ):


      '''simple docstring'''




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


      '''simple docstring'''




      a  :    Optional[int]       =     self.num_labels
      a  :    Optional[Any]       =     AlbertForSequenceClassification(A    )
      model.to(A    )
      model.eval()
      a  :    str       =     model(A       ,	attention_mask=A       ,	token_type_ids=A       ,	labels=A    )
      self.parent.assertEqual(result.logits.shape       ,	(self.batch_size, self.num_labels)    )





    def        lowerCamelCase__   (							self			:      Optional[Any]       ,	A			:      List[Any]       ,	A			:      Optional[int]       ,	A			:      List[Any]       ,	A			:      str       ,	A			:      Dict       ,	A			:      Optional[Any]       ,	A			:      Any    ):


      '''simple docstring'''




      a  :    Optional[int]       =     self.num_labels
      a  :    str       =     AlbertForTokenClassification(config=A    )
      model.to(A    )
      model.eval()
      a  :    List[str]       =     model(A       ,	attention_mask=A       ,	token_type_ids=A       ,	labels=A    )
      self.parent.assertEqual(result.logits.shape       ,	(self.batch_size, self.seq_length, self.num_labels)    )





    def        lowerCamelCase__   (							self			:      Dict       ,	A			:      Optional[int]       ,	A			:      int       ,	A			:      int       ,	A			:      List[Any]       ,	A			:      List[str]       ,	A			:      Optional[Any]       ,	A			:      str    ):


      '''simple docstring'''




      a  :    List[str]       =     self.num_choices
      a  :    str       =     AlbertForMultipleChoice(config=A    )
      model.to(A    )
      model.eval()
      a  :    List[str]       =     input_ids.unsqueeze(1    ).expand(-1       ,	self.num_choices       ,	-1    ).contiguous()
      a  :    str       =     token_type_ids.unsqueeze(1    ).expand(-1       ,	self.num_choices       ,	-1    ).contiguous()
      a  :    str       =     input_mask.unsqueeze(1    ).expand(-1       ,	self.num_choices       ,	-1    ).contiguous()
      a  :    Union[str, Any]       =     model(
          A       ,	attention_mask=A       ,	token_type_ids=A       ,	labels=A       ,	)
      self.parent.assertEqual(result.logits.shape       ,	(self.batch_size, self.num_choices)    )





    def        lowerCamelCase__   (							self			:      str    ):


      '''simple docstring'''




      a  :    Tuple       =     self.prepare_config_and_inputs()
      (
          (
          a
      ),  (
          a
      ),  (
          a
      ),  (
          a
      ),  (
          a
      ),  (
          a
      ),  (
          a
      ),  
      )  :    Dict       =     config_and_inputs
      a  :    List[Any]       =     {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
      return config, inputs_dict






@require_torch
class 		snake_case						(						UpperCAmelCase     ,				UpperCAmelCase     ,				unittest.TestCase ):
    __magic_name__					  =  (
        (
            AlbertModel,
            AlbertForPreTraining,
            AlbertForMaskedLM,
            AlbertForMultipleChoice,
            AlbertForSequenceClassification,
            AlbertForTokenClassification,
            AlbertForQuestionAnswering,
        )
        if is_torch_available()
        else ()
    )
    __magic_name__					  =  (
        {
            '''feature-extraction''': AlbertModel,
            '''fill-mask''': AlbertForMaskedLM,
            '''question-answering''': AlbertForQuestionAnswering,
            '''text-classification''': AlbertForSequenceClassification,
            '''token-classification''': AlbertForTokenClassification,
            '''zero-shot''': AlbertForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    __magic_name__					  =  True





    def        lowerCamelCase__   (							self			:      Union[str, Any]       ,	A			:      List[str]       ,	A			:      Tuple       ,	A			:      Tuple=False    ):


      '''simple docstring'''




      a  :    List[Any]       =     super()._prepare_for_class(A       ,	A       ,	return_labels=A    )

      if return_labels:
        if model_class in get_values(A    ):
          a  :    Union[str, Any]       =     torch.zeros(
              (self.model_tester.batch_size, self.model_tester.seq_length)       ,	dtype=torch.long       ,	device=A    )
          a  :    Any       =     torch.zeros(
              self.model_tester.batch_size       ,	dtype=torch.long       ,	device=A    )
      return inputs_dict





    def        lowerCamelCase__   (							self			:      Any    ):


      '''simple docstring'''




      a  :    int       =     AlbertModelTester(self    )
      a  :    List[Any]       =     ConfigTester(self       ,	config_class=A       ,	hidden_size=3_7    )





    def        lowerCamelCase__   (							self			:      Union[str, Any]    ):


      '''simple docstring'''




      self.config_tester.run_common_tests()





    def        lowerCamelCase__   (							self			:      Tuple    ):


      '''simple docstring'''




      a  :    Tuple       =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*A    )





    def        lowerCamelCase__   (							self			:      int    ):


      '''simple docstring'''




      a  :    Tuple       =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_pretraining(*A    )





    def        lowerCamelCase__   (							self			:      Dict    ):


      '''simple docstring'''




      a  :    Optional[int]       =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*A    )





    def        lowerCamelCase__   (							self			:      int    ):


      '''simple docstring'''




      a  :    int       =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_multiple_choice(*A    )





    def        lowerCamelCase__   (							self			:      Tuple    ):


      '''simple docstring'''




      a  :    List[str]       =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*A    )





    def        lowerCamelCase__   (							self			:      int    ):


      '''simple docstring'''




      a  :    Tuple       =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*A    )





    def        lowerCamelCase__   (							self			:      int    ):


      '''simple docstring'''




      a  :    Optional[int]       =     self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
        a  :    Optional[Any]       =     type
        self.model_tester.create_and_check_model(*A    )





    @slow
    def        lowerCamelCase__   (							self			:      Any    ):


      '''simple docstring'''




      for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        a  :    Tuple       =     AlbertModel.from_pretrained(A    )
        self.assertIsNotNone(A    )






@require_torch
class 		snake_case						(						unittest.TestCase ):





    @slow
    def        lowerCamelCase__   (							self			:      List[Any]    ):


      '''simple docstring'''




      a  :    List[Any]       =     AlbertModel.from_pretrained('albert-base-v2'    )
      a  :    Union[str, Any]       =     torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]    )
      a  :    str       =     torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]    )
      with torch.no_grad():
        a  :    Optional[Any]       =     model(A       ,	attention_mask=A    )[0]
      a  :    List[Any]       =     torch.Size((1, 1_1, 7_6_8)    )
      self.assertEqual(output.shape       ,	A    )
      a  :    int       =     torch.tensor(
          [[[-0.65_13, 1.50_35, -0.27_66], [-0.65_15, 1.50_46, -0.27_80], [-0.65_12, 1.50_49, -0.27_84]]]    )

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




