
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	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_=13	,      SCREAMING_SNAKE_CASE_=7	,      SCREAMING_SNAKE_CASE_=True	,      SCREAMING_SNAKE_CASE_=True	,      SCREAMING_SNAKE_CASE_=True	,      SCREAMING_SNAKE_CASE_=True	,      SCREAMING_SNAKE_CASE_=99	,      SCREAMING_SNAKE_CASE_=16	,      SCREAMING_SNAKE_CASE_=36	,      SCREAMING_SNAKE_CASE_=6	,      SCREAMING_SNAKE_CASE_=6	,      SCREAMING_SNAKE_CASE_=6	,      SCREAMING_SNAKE_CASE_=37	,      SCREAMING_SNAKE_CASE_="gelu"	,      SCREAMING_SNAKE_CASE_=0.1	,      SCREAMING_SNAKE_CASE_=0.1	,      SCREAMING_SNAKE_CASE_=5_12	,      SCREAMING_SNAKE_CASE_=16	,      SCREAMING_SNAKE_CASE_=2	,      SCREAMING_SNAKE_CASE_=0.02	,      SCREAMING_SNAKE_CASE_=3	,      SCREAMING_SNAKE_CASE_=4	,      SCREAMING_SNAKE_CASE_=None	,      ):
       UpperCamelCase__				=    parent
       UpperCamelCase__				=    batch_size
       UpperCamelCase__				=    seq_length
       UpperCamelCase__				=    is_training
       UpperCamelCase__				=    use_input_mask
       UpperCamelCase__				=    use_token_type_ids
       UpperCamelCase__				=    use_labels
       UpperCamelCase__				=    vocab_size
       UpperCamelCase__				=    embedding_size
       UpperCamelCase__				=    hidden_size
       UpperCamelCase__				=    num_hidden_layers
       UpperCamelCase__				=    num_hidden_groups
       UpperCamelCase__				=    num_attention_heads
       UpperCamelCase__				=    intermediate_size
       UpperCamelCase__				=    hidden_act
       UpperCamelCase__				=    hidden_dropout_prob
       UpperCamelCase__				=    attention_probs_dropout_prob
       UpperCamelCase__				=    max_position_embeddings
       UpperCamelCase__				=    type_vocab_size
       UpperCamelCase__				=    type_sequence_label_size
       UpperCamelCase__				=    initializer_range
       UpperCamelCase__				=    num_labels
       UpperCamelCase__				=    num_choices
       UpperCamelCase__				=    scope







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    ids_tensor([self.batch_size, self.seq_length]	,      self.vocab_size						)

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

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

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

       UpperCamelCase__				=    self.get_config()

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







  def 	UpperCAmelCase_ (self						):
       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	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						):
       UpperCamelCase__				=    AlbertModel(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       UpperCamelCase__				=    model(_lowerCAmelCase	,      attention_mask=_lowerCAmelCase	,      token_type_ids=_lowerCAmelCase						)
       UpperCamelCase__				=    model(_lowerCAmelCase	,      token_type_ids=_lowerCAmelCase						)
       UpperCamelCase__				=    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	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						):
       UpperCamelCase__				=    AlbertForPreTraining(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       UpperCamelCase__				=    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	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						):
       UpperCamelCase__				=    AlbertForMaskedLM(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       UpperCamelCase__				=    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	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						):
       UpperCamelCase__				=    AlbertForQuestionAnswering(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       UpperCamelCase__				=    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	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						):
       UpperCamelCase__				=    self.num_labels
       UpperCamelCase__				=    AlbertForSequenceClassification(_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       UpperCamelCase__				=    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	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						):
       UpperCamelCase__				=    self.num_labels
       UpperCamelCase__				=    AlbertForTokenClassification(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       UpperCamelCase__				=    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	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_						):
       UpperCamelCase__				=    self.num_choices
       UpperCamelCase__				=    AlbertForMultipleChoice(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       UpperCamelCase__				=    input_ids.unsqueeze(1						).expand(-1	,      self.num_choices	,      -1						).contiguous()
       UpperCamelCase__				=    token_type_ids.unsqueeze(1						).expand(-1	,      self.num_choices	,      -1						).contiguous()
       UpperCamelCase__				=    input_mask.unsqueeze(1						).expand(-1	,      self.num_choices	,      -1						).contiguous()
       UpperCamelCase__				=    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						):
       UpperCamelCase__				=    self.prepare_config_and_inputs()
       (
           UpperCamelCase__				
       )				=    config_and_inputs
       UpperCamelCase__				=    {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
       return config, inputs_dict






@require_torch
class      __A(    __UpperCamelCase    ,				__UpperCamelCase    ,				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	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_=False						):
       UpperCamelCase__				=    super()._prepare_for_class(_lowerCAmelCase	,      _lowerCAmelCase	,      return_labels=_lowerCAmelCase						)

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







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    AlbertModelTester(self						)
       UpperCamelCase__				=    ConfigTester(self	,      config_class=_lowerCAmelCase	,      hidden_size=37						)







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







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_lowerCAmelCase						)







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_pretraining(*_lowerCAmelCase						)







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*_lowerCAmelCase						)







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_multiple_choice(*_lowerCAmelCase						)







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*_lowerCAmelCase						)







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*_lowerCAmelCase						)







  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
            UpperCamelCase__				=    type
            self.model_tester.create_and_check_model(*_lowerCAmelCase						)







  @slow
  def 	UpperCAmelCase_ (self						):
       for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            UpperCamelCase__				=    AlbertModel.from_pretrained(_lowerCAmelCase						)
            self.assertIsNotNone(_lowerCAmelCase						)






@require_torch
class      __A(    unittest.TestCase			):

  """simple docstring"""







  @slow
  def 	UpperCAmelCase_ (self						):
       UpperCamelCase__				=    AlbertModel.from_pretrained("""albert-base-v2"""						)
       UpperCamelCase__				=    torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]						)
       UpperCamelCase__				=    torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
       with torch.no_grad():
            UpperCamelCase__				=    model(_lowerCAmelCase	,      attention_mask=_lowerCAmelCase						)[0]
       UpperCamelCase__				=    torch.Size((1, 11, 7_68)						)
       self.assertEqual(output.shape	,      _lowerCAmelCase						)
       UpperCamelCase__				=    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]	,      _lowerCAmelCase	,      atol=1E-4						)						)







import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.metrics import mean_absolute_error, mean_squared_error
from sklearn.model_selection import train_test_split
from xgboost import XGBRegressor






def 						__magic_name__			(   __a      :	dict						):



     '''simple docstring'''




     return (data["data"], data["target"])






def 						__magic_name__			(   __a      :	np.ndarray	,		__a      :	np.ndarray	,		__a      :	np.ndarray						):



     '''simple docstring'''




     UpperCamelCase__				=    XGBRegressor(verbosity=0	,		random_state=42						)
     xgb.fit(__a	,		__a						)
     # Predict target for test data
     UpperCamelCase__				=    xgb.predict(__a						)
     UpperCamelCase__				=    predictions.reshape(len(__a						)	,		1						)
     return predictions






def 						__magic_name__			(   ):



     '''simple docstring'''




     UpperCamelCase__				=    fetch_california_housing()
     UpperCamelCase__		,	UpperCamelCase__				=    data_handling(__a						)
     UpperCamelCase__		,	UpperCamelCase__		,	UpperCamelCase__		,	UpperCamelCase__				=    train_test_split(
         __a	,		__a	,		test_size=0.25	,		random_state=1						)
     UpperCamelCase__				=    xgboost(__a	,		__a	,		__a						)
     # Error printing
     print(f"Mean Absolute Error : {mean_absolute_error(__a	,		__a						)}"						)
     print(f"Mean Square Error  : {mean_squared_error(__a	,		__a						)}"						)


if __name__ == "__main__":
    import doctest

    doctest.testmod(verbose=True)
    main()





