




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







 '''simple docstring'''





 def __init__(       self:	List[str],   a_:	List[str],   a_:	List[str]=13,   a_:	Dict=7,   a_:	List[Any]=True,   a_:	List[Any]=True,   a_:	Union[str, Any]=True,   a_:	Optional[Any]=True,   a_:	Optional[int]=99,   a_:	List[Any]=16,   a_:	Optional[Any]=36,   a_:	Optional[int]=6,   a_:	Union[str, Any]=6,   a_:	Optional[int]=6,   a_:	Dict=37,   a_:	List[Any]="gelu",   a_:	Tuple=0.1,   a_:	Optional[Any]=0.1,   a_:	List[Any]=512,   a_:	Dict=16,   a_:	Union[str, Any]=2,   a_:	Dict=0.02,   a_:	Optional[Any]=3,   a_:	Dict=4,   a_:	Dict=None,   ):




   '''simple docstring'''



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


 def 				UpperCamelCase_     (       self:	Dict	):




   '''simple docstring'''



   _snake_case							:	Optional[Any]			=						ids_tensor([self.batch_size, self.seq_length],   self.vocab_size	)

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

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

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

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

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


 def 				UpperCamelCase_     (       self:	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 				UpperCamelCase_     (       self:	Dict,   a_:	Optional[Any],   a_:	Dict,   a_:	int,   a_:	List[str],   a_:	List[str],   a_:	Any,   a_:	List[Any]	):




   '''simple docstring'''



   _snake_case							:	int			=						AlbertModel(config=__lowerCamelCase	)
   model.to(__lowerCamelCase	)
   model.eval()
   _snake_case							:	Any			=						model(__lowerCamelCase,   attention_mask=__lowerCamelCase,   token_type_ids=__lowerCamelCase	)
   _snake_case							:	Any			=						model(__lowerCamelCase,   token_type_ids=__lowerCamelCase	)
   _snake_case							:	Optional[int]			=						model(__lowerCamelCase	)
   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 				UpperCamelCase_     (       self:	Union[str, Any],   a_:	Union[str, Any],   a_:	Optional[Any],   a_:	Dict,   a_:	str,   a_:	Union[str, Any],   a_:	int,   a_:	Tuple	):




   '''simple docstring'''



   _snake_case							:	Any			=						AlbertForPreTraining(config=__lowerCamelCase	)
   model.to(__lowerCamelCase	)
   model.eval()
   _snake_case							:	Union[str, Any]			=						model(
       __lowerCamelCase,   attention_mask=__lowerCamelCase,   token_type_ids=__lowerCamelCase,   labels=__lowerCamelCase,   sentence_order_label=__lowerCamelCase,   )
   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 				UpperCamelCase_     (       self:	str,   a_:	str,   a_:	List[Any],   a_:	Any,   a_:	str,   a_:	List[Any],   a_:	Optional[int],   a_:	Tuple	):




   '''simple docstring'''



   _snake_case							:	Dict			=						AlbertForMaskedLM(config=__lowerCamelCase	)
   model.to(__lowerCamelCase	)
   model.eval()
   _snake_case							:	Tuple			=						model(__lowerCamelCase,   attention_mask=__lowerCamelCase,   token_type_ids=__lowerCamelCase,   labels=__lowerCamelCase	)
   self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.seq_length, self.vocab_size)	)


 def 				UpperCamelCase_     (       self:	Union[str, Any],   a_:	Optional[int],   a_:	List[Any],   a_:	Optional[Any],   a_:	Optional[Any],   a_:	Optional[Any],   a_:	Any,   a_:	int	):




   '''simple docstring'''



   _snake_case							:	str			=						AlbertForQuestionAnswering(config=__lowerCamelCase	)
   model.to(__lowerCamelCase	)
   model.eval()
   _snake_case							:	str			=						model(
       __lowerCamelCase,   attention_mask=__lowerCamelCase,   token_type_ids=__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 				UpperCamelCase_     (       self:	Optional[int],   a_:	Tuple,   a_:	Optional[Any],   a_:	Optional[Any],   a_:	Optional[int],   a_:	Optional[int],   a_:	Union[str, Any],   a_:	Dict	):




   '''simple docstring'''



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


 def 				UpperCamelCase_     (       self:	Dict,   a_:	Dict,   a_:	List[str],   a_:	List[str],   a_:	Any,   a_:	Dict,   a_:	Any,   a_:	List[Any]	):




   '''simple docstring'''



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


 def 				UpperCamelCase_     (       self:	Tuple,   a_:	Any,   a_:	Optional[Any],   a_:	List[Any],   a_:	List[Any],   a_:	Any,   a_:	Any,   a_:	Union[str, Any]	):




   '''simple docstring'''



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


 def 				UpperCamelCase_     (       self:	str	):




   '''simple docstring'''



   _snake_case							:	int			=						self.prepare_config_and_inputs()
   (
       _snake_case     
   )							:	Union[str, Any]			=						config_and_inputs
   _snake_case							:	str			=						{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
   return config, inputs_dict







@require_torch
class       lowercase(  lowercase__ ,    lowercase__ ,    unittest.TestCase				):







 '''simple docstring'''





 lowercase__				      =      (
     (
         AlbertModel,
         AlbertForPreTraining,
         AlbertForMaskedLM,
         AlbertForMultipleChoice,
         AlbertForSequenceClassification,
         AlbertForTokenClassification,
         AlbertForQuestionAnswering,
     )
     if is_torch_available()
     else ()
 )
 lowercase__				      =      (
     {
         "feature-extraction": AlbertModel,
         "fill-mask": AlbertForMaskedLM,
         "question-answering": AlbertForQuestionAnswering,
         "text-classification": AlbertForSequenceClassification,
         "token-classification": AlbertForTokenClassification,
         "zero-shot": AlbertForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 lowercase__				      =      True


 def 				UpperCamelCase_     (       self:	List[str],   a_:	int,   a_:	Tuple,   a_:	Dict=False	):




   '''simple docstring'''



   _snake_case							:	Any			=						super()._prepare_for_class(__lowerCamelCase,   __lowerCamelCase,   return_labels=__lowerCamelCase	)

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


 def 				UpperCamelCase_     (       self:	Optional[Any]	):




   '''simple docstring'''



   _snake_case							:	Optional[int]			=						AlbertModelTester(self	)
   _snake_case							:	Optional[Any]			=						ConfigTester(self,   config_class=__lowerCamelCase,   hidden_size=37	)


 def 				UpperCamelCase_     (       self:	Tuple	):




   '''simple docstring'''



   self.config_tester.run_common_tests()


 def 				UpperCamelCase_     (       self:	Dict	):




   '''simple docstring'''



   _snake_case							:	Optional[Any]			=						self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*__lowerCamelCase	)


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




   '''simple docstring'''



   _snake_case							:	Union[str, Any]			=						self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_pretraining(*__lowerCamelCase	)


 def 				UpperCamelCase_     (       self:	Any	):




   '''simple docstring'''



   _snake_case							:	int			=						self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_masked_lm(*__lowerCamelCase	)


 def 				UpperCamelCase_     (       self:	Any	):




   '''simple docstring'''



   _snake_case							:	List[Any]			=						self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_multiple_choice(*__lowerCamelCase	)


 def 				UpperCamelCase_     (       self:	Tuple	):




   '''simple docstring'''



   _snake_case							:	Optional[Any]			=						self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase	)


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




   '''simple docstring'''



   _snake_case							:	List[str]			=						self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_sequence_classification(*__lowerCamelCase	)


 def 				UpperCamelCase_     (       self:	Optional[Any]	):




   '''simple docstring'''



   _snake_case							:	List[str]			=						self.model_tester.prepare_config_and_inputs()
   for type in ["absolute", "relative_key", "relative_key_query"]:
     _snake_case							:	Dict			=						type
     self.model_tester.create_and_check_model(*__lowerCamelCase	)


 @slow
 def 				UpperCamelCase_     (       self:	Optional[Any]	):




   '''simple docstring'''



   for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     _snake_case							:	List[str]			=						AlbertModel.from_pretrained(__lowerCamelCase	)
     self.assertIsNotNone(__lowerCamelCase	)







@require_torch
class       lowercase(  unittest.TestCase				):







 '''simple docstring'''





 @slow
 def 				UpperCamelCase_     (       self:	Union[str, Any]	):




   '''simple docstring'''



   _snake_case							:	List[Any]			=						AlbertModel.from_pretrained("""albert-base-v2"""	)
   _snake_case							:	Any			=						torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]	)
   _snake_case							:	int			=						torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
   with torch.no_grad():
     _snake_case							:	List[Any]			=						model(__lowerCamelCase,   attention_mask=__lowerCamelCase	)[0]
   _snake_case							:	Tuple			=						torch.Size((1, 11, 768)	)
   self.assertEqual(output.shape,   __lowerCamelCase	)
   _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],   __lowerCamelCase,   atol=1E-4	)	)








"""simple docstring"""


import os
import sys
import unittest


A_      			=       os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, '''utils'''))

import check_dummies  # noqa: E402
from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init  # noqa: E402


# Align TRANSFORMERS_PATH in check_dummies with the current path
A_      			=       os.path.join(git_repo_path, '''src''', '''diffusers''')






class       lowercase(  unittest.TestCase				):







 '''simple docstring'''





 def 				UpperCamelCase_     (       self:	Tuple	):




   '''simple docstring'''



   _snake_case							:	Optional[int]			=						find_backend("""    if not is_torch_available():"""	)
   self.assertEqual(a_,   """torch"""	)

   # backend_with_underscore = find_backend("    if not is_tensorflow_text_available():")
   # self.assertEqual(backend_with_underscore, "tensorflow_text")

   _snake_case							:	Any			=						find_backend("""    if not (is_torch_available() and is_transformers_available()):"""	)
   self.assertEqual(a_,   """torch_and_transformers"""	)

   # double_backend_with_underscore = find_backend(
   #    "    if not (is_sentencepiece_available() and is_tensorflow_text_available()):"
   # )
   # self.assertEqual(double_backend_with_underscore, "sentencepiece_and_tensorflow_text")

   _snake_case							:	Union[str, Any]			=						find_backend(
       """    if not (is_torch_available() and is_transformers_available() and is_onnx_available()):"""	)
   self.assertEqual(a_,   """torch_and_transformers_and_onnx"""	)


 def 				UpperCamelCase_     (       self:	List[Any]	):




   '''simple docstring'''



   _snake_case							:	Dict			=						read_init()
   # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
   self.assertIn("""torch""",   a_	)
   self.assertIn("""torch_and_transformers""",   a_	)
   self.assertIn("""flax_and_transformers""",   a_	)
   self.assertIn("""torch_and_transformers_and_onnx""",   a_	)

   # Likewise, we can't assert on the exact content of a key
   self.assertIn("""UNet2DModel""",   objects["""torch"""]	)
   self.assertIn("""FlaxUNet2DConditionModel""",   objects["""flax"""]	)
   self.assertIn("""StableDiffusionPipeline""",   objects["""torch_and_transformers"""]	)
   self.assertIn("""FlaxStableDiffusionPipeline""",   objects["""flax_and_transformers"""]	)
   self.assertIn("""LMSDiscreteScheduler""",   objects["""torch_and_scipy"""]	)
   self.assertIn("""OnnxStableDiffusionPipeline""",   objects["""torch_and_transformers_and_onnx"""]	)


 def 				UpperCamelCase_     (       self:	List[str]	):




   '''simple docstring'''



   _snake_case							:	Optional[Any]			=						create_dummy_object("""CONSTANT""",   """'torch'"""	)
   self.assertEqual(a_,   """\nCONSTANT = None\n"""	)

   _snake_case							:	Optional[int]			=						create_dummy_object("""function""",   """'torch'"""	)
   self.assertEqual(
       a_,   """\ndef function(*args, **kwargs):\n    requires_backends(function, 'torch')\n"""	)

   _snake_case							:	List[Any]			=						"""
class FakeClass(metaclass=DummyObject):
    _backends = 'torch'

    def __init__(self, *args, **kwargs):
        requires_backends(self, 'torch')

    @classmethod
    def from_config(cls, *args, **kwargs):
        requires_backends(cls, 'torch')

    @classmethod
    def from_pretrained(cls, *args, **kwargs):
        requires_backends(cls, 'torch')
"""
   _snake_case							:	Union[str, Any]			=						create_dummy_object("""FakeClass""",   """'torch'"""	)
   self.assertEqual(a_,   a_	)


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




   '''simple docstring'''



   _snake_case							:	Union[str, Any]			=						"""# This file is autogenerated by the command `make fix-copies`, do not edit.
from ..utils import DummyObject, requires_backends


CONSTANT = None


def function(*args, **kwargs):
    requires_backends(function, [\"torch\"])


class FakeClass(metaclass=DummyObject):
    _backends = [\"torch\"]

    def __init__(self, *args, **kwargs):
        requires_backends(self, [\"torch\"])

    @classmethod
    def from_config(cls, *args, **kwargs):
        requires_backends(cls, [\"torch\"])

    @classmethod
    def from_pretrained(cls, *args, **kwargs):
        requires_backends(cls, [\"torch\"])
"""
   _snake_case							:	List[Any]			=						create_dummy_files({"""torch""": ["""CONSTANT""", """function""", """FakeClass"""]}	)
   self.assertEqual(dummy_files["""torch"""],   a_	)


