
'''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  _a					:





       """simple docstring"""




       def __init__(				self							,				A__							,				A__=13							,				A__=7							,				A__=True							,				A__=True							,				A__=True							,				A__=True							,				A__=99							,				A__=16							,				A__=36							,				A__=6							,				A__=6							,				A__=6							,				A__=37							,				A__="gelu"							,				A__=0.1							,				A__=0.1							,				A__=5_12							,				A__=16							,				A__=2							,				A__=0.02							,				A__=3							,				A__=4							,				A__=None							,				)     ->      str:
          _SCREAMING_SNAKE_CASE  			=					parent
          _SCREAMING_SNAKE_CASE  			=					batch_size
          _SCREAMING_SNAKE_CASE  			=					seq_length
          _SCREAMING_SNAKE_CASE  			=					is_training
          _SCREAMING_SNAKE_CASE  			=					use_input_mask
          _SCREAMING_SNAKE_CASE  			=					use_token_type_ids
          _SCREAMING_SNAKE_CASE  			=					use_labels
          _SCREAMING_SNAKE_CASE  			=					vocab_size
          _SCREAMING_SNAKE_CASE  			=					embedding_size
          _SCREAMING_SNAKE_CASE  			=					hidden_size
          _SCREAMING_SNAKE_CASE  			=					num_hidden_layers
          _SCREAMING_SNAKE_CASE  			=					num_hidden_groups
          _SCREAMING_SNAKE_CASE  			=					num_attention_heads
          _SCREAMING_SNAKE_CASE  			=					intermediate_size
          _SCREAMING_SNAKE_CASE  			=					hidden_act
          _SCREAMING_SNAKE_CASE  			=					hidden_dropout_prob
          _SCREAMING_SNAKE_CASE  			=					attention_probs_dropout_prob
          _SCREAMING_SNAKE_CASE  			=					max_position_embeddings
          _SCREAMING_SNAKE_CASE  			=					type_vocab_size
          _SCREAMING_SNAKE_CASE  			=					type_sequence_label_size
          _SCREAMING_SNAKE_CASE  			=					initializer_range
          _SCREAMING_SNAKE_CASE  			=					num_labels
          _SCREAMING_SNAKE_CASE  			=					num_choices
          _SCREAMING_SNAKE_CASE  			=					scope






       def 							UpperCamelCase					(				self   )     ->      Tuple:
          _SCREAMING_SNAKE_CASE  			=					ids_tensor([self.batch_size, self.seq_length]							,				self.vocab_size   )

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

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

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

          _SCREAMING_SNAKE_CASE  			=					self.get_config()

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






       def 							UpperCamelCase					(				self   )     ->      Optional[Any]:
          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							,				A__							,				A__							,				A__							,				A__							,				A__							,				A__							,				A__   )     ->      Any:
          _SCREAMING_SNAKE_CASE  			=					AlbertModel(config=a_   )
          model.to(a_   )
          model.eval()
          _SCREAMING_SNAKE_CASE  			=					model(a_							,				attention_mask=a_							,				token_type_ids=a_   )
          _SCREAMING_SNAKE_CASE  			=					model(a_							,				token_type_ids=a_   )
          _SCREAMING_SNAKE_CASE  			=					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 							UpperCamelCase					(				self							,				A__							,				A__							,				A__							,				A__							,				A__							,				A__							,				A__   )     ->      Dict:
          _SCREAMING_SNAKE_CASE  			=					AlbertForPreTraining(config=a_   )
          model.to(a_   )
          model.eval()
          _SCREAMING_SNAKE_CASE  			=					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 							UpperCamelCase					(				self							,				A__							,				A__							,				A__							,				A__							,				A__							,				A__							,				A__   )     ->      Optional[int]:
          _SCREAMING_SNAKE_CASE  			=					AlbertForMaskedLM(config=a_   )
          model.to(a_   )
          model.eval()
          _SCREAMING_SNAKE_CASE  			=					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 							UpperCamelCase					(				self							,				A__							,				A__							,				A__							,				A__							,				A__							,				A__							,				A__   )     ->      Any:
          _SCREAMING_SNAKE_CASE  			=					AlbertForQuestionAnswering(config=a_   )
          model.to(a_   )
          model.eval()
          _SCREAMING_SNAKE_CASE  			=					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 							UpperCamelCase					(				self							,				A__							,				A__							,				A__							,				A__							,				A__							,				A__							,				A__   )     ->      str:
          _SCREAMING_SNAKE_CASE  			=					self.num_labels
          _SCREAMING_SNAKE_CASE  			=					AlbertForSequenceClassification(a_   )
          model.to(a_   )
          model.eval()
          _SCREAMING_SNAKE_CASE  			=					model(a_							,				attention_mask=a_							,				token_type_ids=a_							,				labels=a_   )
          self.parent.assertEqual(result.logits.shape							,				(self.batch_size, self.num_labels)   )






       def 							UpperCamelCase					(				self							,				A__							,				A__							,				A__							,				A__							,				A__							,				A__							,				A__   )     ->      Optional[int]:
          _SCREAMING_SNAKE_CASE  			=					self.num_labels
          _SCREAMING_SNAKE_CASE  			=					AlbertForTokenClassification(config=a_   )
          model.to(a_   )
          model.eval()
          _SCREAMING_SNAKE_CASE  			=					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 							UpperCamelCase					(				self							,				A__							,				A__							,				A__							,				A__							,				A__							,				A__							,				A__   )     ->      Optional[Any]:
          _SCREAMING_SNAKE_CASE  			=					self.num_choices
          _SCREAMING_SNAKE_CASE  			=					AlbertForMultipleChoice(config=a_   )
          model.to(a_   )
          model.eval()
          _SCREAMING_SNAKE_CASE  			=					input_ids.unsqueeze(1   ).expand(-1							,				self.num_choices							,				-1   ).contiguous()
          _SCREAMING_SNAKE_CASE  			=					token_type_ids.unsqueeze(1   ).expand(-1							,				self.num_choices							,				-1   ).contiguous()
          _SCREAMING_SNAKE_CASE  			=					input_mask.unsqueeze(1   ).expand(-1							,				self.num_choices							,				-1   ).contiguous()
          _SCREAMING_SNAKE_CASE  			=					model(
              a_							,				attention_mask=a_							,				token_type_ids=a_							,				labels=a_							,				)
          self.parent.assertEqual(result.logits.shape							,				(self.batch_size, self.num_choices)   )






       def 							UpperCamelCase					(				self   )     ->      List[Any]:
          _SCREAMING_SNAKE_CASE  			=					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   
          )			,				   
          )  			=					config_and_inputs
          _SCREAMING_SNAKE_CASE  			=					{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
          return config, inputs_dict






@require_torch
class  _a					(_lowerCamelCase	,       _lowerCamelCase	,       unittest.TestCase):





       """simple docstring"""




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






       def 							UpperCamelCase					(				self							,				A__							,				A__							,				A__=False   )     ->      Optional[Any]:
          _SCREAMING_SNAKE_CASE  			=					super()._prepare_for_class(a_							,				a_							,				return_labels=a_   )

          if return_labels:
             if model_class in get_values(a_   ):
                _SCREAMING_SNAKE_CASE  			=					torch.zeros(
                    (self.model_tester.batch_size, self.model_tester.seq_length)							,				dtype=torch.long							,				device=a_   )
                _SCREAMING_SNAKE_CASE  			=					torch.zeros(
                    self.model_tester.batch_size							,				dtype=torch.long							,				device=a_   )
          return inputs_dict






       def 							UpperCamelCase					(				self   )     ->      Optional[Any]:
          _SCREAMING_SNAKE_CASE  			=					AlbertModelTester(self   )
          _SCREAMING_SNAKE_CASE  			=					ConfigTester(self							,				config_class=a_							,				hidden_size=37   )






       def 							UpperCamelCase					(				self   )     ->      Union[str, Any]:
          self.config_tester.run_common_tests()






       def 							UpperCamelCase					(				self   )     ->      str:
          _SCREAMING_SNAKE_CASE  			=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*a_   )






       def 							UpperCamelCase					(				self   )     ->      Dict:
          _SCREAMING_SNAKE_CASE  			=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_pretraining(*a_   )






       def 							UpperCamelCase					(				self   )     ->      str:
          _SCREAMING_SNAKE_CASE  			=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*a_   )






       def 							UpperCamelCase					(				self   )     ->      Tuple:
          _SCREAMING_SNAKE_CASE  			=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_multiple_choice(*a_   )






       def 							UpperCamelCase					(				self   )     ->      Union[str, Any]:
          _SCREAMING_SNAKE_CASE  			=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*a_   )






       def 							UpperCamelCase					(				self   )     ->      Optional[Any]:
          _SCREAMING_SNAKE_CASE  			=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*a_   )






       def 							UpperCamelCase					(				self   )     ->      str:
          _SCREAMING_SNAKE_CASE  			=					self.model_tester.prepare_config_and_inputs()
          for type in ["absolute", "relative_key", "relative_key_query"]:
             _SCREAMING_SNAKE_CASE  			=					type
             self.model_tester.create_and_check_model(*a_   )






       @slow
       def 							UpperCamelCase					(				self   )     ->      Dict:
          for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _SCREAMING_SNAKE_CASE  			=					AlbertModel.from_pretrained(a_   )
             self.assertIsNotNone(a_   )






@require_torch
class  _a					(unittest.TestCase):





       """simple docstring"""




       @slow
       def 							UpperCamelCase					(				self   )     ->      List[Any]:
          _SCREAMING_SNAKE_CASE  			=					AlbertModel.from_pretrained("""albert-base-v2"""   )
          _SCREAMING_SNAKE_CASE  			=					torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]   )
          _SCREAMING_SNAKE_CASE  			=					torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
          with torch.no_grad():
             _SCREAMING_SNAKE_CASE  			=					model(a_							,				attention_mask=a_   )[0]
          _SCREAMING_SNAKE_CASE  			=					torch.Size((1, 11, 7_68)   )
          self.assertEqual(output.shape							,				a_   )
          _SCREAMING_SNAKE_CASE  			=					torch.tensor(
              [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]]   )

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









'''simple docstring'''




import subprocess
import sys

from transformers import BertConfig, BertModel, BertTokenizer, pipeline
from transformers.testing_utils import TestCasePlus, require_torch



class 							lowercase_						(		a			):

  '''simple docstring'''





  @require_torch
  def     snake_case_      (  self  )				->					Optional[Any]:


    """simple docstring"""
    # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
    # `transformers` is loaded, and it's too late for inside pytest - so we are changing it
    # while running an external program

    # python one-liner segments

    # this must be loaded before socket.socket is monkey-patched
    UpperCAmelCase    				=						'\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        '

    UpperCAmelCase    				=						'\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n        '

    UpperCAmelCase    				=						'\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")\nsocket.socket = offline_socket\n        '

    # Force fetching the files so that we can use the cache
    UpperCAmelCase    				=						'hf-internal-testing/tiny-random-bert'
    BertConfig.from_pretrained(a_  )
    BertModel.from_pretrained(a_  )
    BertTokenizer.from_pretrained(a_  )
    pipeline(task='fill-mask' ,			model=a_  )

    # baseline - just load from_pretrained with normal network
    UpperCAmelCase    				=						[sys.executable, '-c', '\n'.join([load, run, mock]  )]

    # should succeed
    UpperCAmelCase    				=						self.get_env()
    # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
    UpperCAmelCase    				=						'1'
    UpperCAmelCase    				=						subprocess.run(a_ ,			env=a_ ,			check=a_ ,			capture_output=a_  )
    self.assertEqual(result.returncode ,			0 ,			result.stderr  )
    self.assertIn('success' ,			result.stdout.decode()  )
  @require_torch
  def     snake_case_      (  self  )				->					Union[str, Any]:


    """simple docstring"""
    # python one-liner segments
    # this must be loaded before socket.socket is monkey-patched
    UpperCAmelCase    				=						'\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        '

    UpperCAmelCase    				=						'\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n        '

    UpperCAmelCase    				=						'\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")\nsocket.socket = offline_socket\n        '

    # Force fetching the files so that we can use the cache
    UpperCAmelCase    				=						'hf-internal-testing/tiny-random-bert'
    BertConfig.from_pretrained(a_  )
    BertModel.from_pretrained(a_  )
    BertTokenizer.from_pretrained(a_  )
    pipeline(task='fill-mask' ,			model=a_  )

    # baseline - just load from_pretrained with normal network
    UpperCAmelCase    				=						[sys.executable, '-c', '\n'.join([load, run, mock]  )]

    # should succeed
    UpperCAmelCase    				=						self.get_env()
    UpperCAmelCase    				=						subprocess.run(a_ ,			env=a_ ,			check=a_ ,			capture_output=a_  )
    self.assertEqual(result.returncode ,			0 ,			result.stderr  )
    self.assertIn('success' ,			result.stdout.decode()  )
  @require_torch
  def     snake_case_      (  self  )				->					List[str]:


    """simple docstring"""
    # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
    # `transformers` is loaded, and it's too late for inside pytest - so we are changing it
    # while running an external program

    # python one-liner segments

    # this must be loaded before socket.socket is monkey-patched
    UpperCAmelCase    				=						'\nfrom transformers import BertConfig, BertModel, BertTokenizer\n        '

    UpperCAmelCase    				=						'\nmname = "hf-internal-testing/tiny-random-bert-sharded"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint("success")\n        '

    UpperCAmelCase    				=						'\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")\nsocket.socket = offline_socket\n        '

    # baseline - just load from_pretrained with normal network
    UpperCAmelCase    				=						[sys.executable, '-c', '\n'.join([load, run]  )]

    # should succeed
    UpperCAmelCase    				=						self.get_env()
    UpperCAmelCase    				=						subprocess.run(a_ ,			env=a_ ,			check=a_ ,			capture_output=a_  )
    self.assertEqual(result.returncode ,			0 ,			result.stderr  )
    self.assertIn('success' ,			result.stdout.decode()  )

    # next emulate no network
    UpperCAmelCase    				=						[sys.executable, '-c', '\n'.join([load, mock, run]  )]

    # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this.
    # env["TRANSFORMERS_OFFLINE"] = "0"
    # result = subprocess.run(cmd, env=env, check=False, capture_output=True)
    # self.assertEqual(result.returncode, 1, result.stderr)

    # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
    UpperCAmelCase    				=						'1'
    UpperCAmelCase    				=						subprocess.run(a_ ,			env=a_ ,			check=a_ ,			capture_output=a_  )
    self.assertEqual(result.returncode ,			0 ,			result.stderr  )
    self.assertIn('success' ,			result.stdout.decode()  )
  @require_torch
  def     snake_case_      (  self  )				->					Optional[Any]:


    """simple docstring"""
    UpperCAmelCase    				=						'\nfrom transformers import pipeline\n        '
    UpperCAmelCase    				=						'\nmname = "hf-internal-testing/tiny-random-bert"\npipe = pipeline(model=mname)\n        '

    UpperCAmelCase    				=						'\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")\nsocket.socket = offline_socket\n        '
    UpperCAmelCase    				=						self.get_env()
    UpperCAmelCase    				=						'1'
    UpperCAmelCase    				=						[sys.executable, '-c', '\n'.join([load, mock, run]  )]
    UpperCAmelCase    				=						subprocess.run(a_ ,			env=a_ ,			check=a_ ,			capture_output=a_  )
    self.assertEqual(result.returncode ,			1 ,			result.stderr  )
    self.assertIn(
        'You cannot infer task automatically within `pipeline` when using offline mode' ,			result.stderr.decode().replace('\n' ,			''  ) ,			)





  @require_torch
  def     snake_case_      (  self  )				->					str:


    """simple docstring"""
    UpperCAmelCase    				=						'\nfrom transformers import AutoModel\n        '
    UpperCAmelCase    				=						'\nmname = "hf-internal-testing/test_dynamic_model"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint("success")\n        '

    # baseline - just load from_pretrained with normal network
    UpperCAmelCase    				=						[sys.executable, '-c', '\n'.join([load, run]  )]

    # should succeed
    UpperCAmelCase    				=						self.get_env()
    UpperCAmelCase    				=						subprocess.run(a_ ,			env=a_ ,			check=a_ ,			capture_output=a_  )
    self.assertEqual(result.returncode ,			0 ,			result.stderr  )
    self.assertIn('success' ,			result.stdout.decode()  )

    # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
    UpperCAmelCase    				=						'1'
    UpperCAmelCase    				=						subprocess.run(a_ ,			env=a_ ,			check=a_ ,			capture_output=a_  )
    self.assertEqual(result.returncode ,			0 ,			result.stderr  )
    self.assertIn('success' ,			result.stdout.decode()  )


