

import unittest

import numpy as np

from transformers import AlbertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
       import jax.numpy as jnp

       from transformers.models.albert.modeling_flax_albert import (
           FlaxAlbertForMaskedLM,
           FlaxAlbertForMultipleChoice,
           FlaxAlbertForPreTraining,
           FlaxAlbertForQuestionAnswering,
           FlaxAlbertForSequenceClassification,
           FlaxAlbertForTokenClassification,
           FlaxAlbertModel,
       )






class  UpperCAmelCase__	( unittest.TestCase ):






  """simple docstring"""


  def __init__(							self		:	str			,     __lowerCamelCase		:	Optional[Any]			,     __lowerCamelCase		:	List[Any]=13			,     __lowerCamelCase		:	Optional[int]=7			,     __lowerCamelCase		:	Tuple=True			,     __lowerCamelCase		:	Any=True			,     __lowerCamelCase		:	Union[str, Any]=True			,     __lowerCamelCase		:	Union[str, Any]=True			,     __lowerCamelCase		:	Tuple=99			,     __lowerCamelCase		:	List[str]=32			,     __lowerCamelCase		:	int=5			,     __lowerCamelCase		:	Tuple=4			,     __lowerCamelCase		:	str=37			,     __lowerCamelCase		:	List[Any]="gelu"			,     __lowerCamelCase		:	int=0.1			,     __lowerCamelCase		:	Optional[int]=0.1			,     __lowerCamelCase		:	Dict=512			,     __lowerCamelCase		:	List[str]=16			,     __lowerCamelCase		:	Optional[int]=2			,     __lowerCamelCase		:	List[str]=0.02			,     __lowerCamelCase		:	List[Any]=4			,     )	->							Optional[Any]:
       SCREAMING_SNAKE_CASE__					=					parent
       SCREAMING_SNAKE_CASE__					=					batch_size
       SCREAMING_SNAKE_CASE__					=					seq_length
       SCREAMING_SNAKE_CASE__					=					is_training
       SCREAMING_SNAKE_CASE__					=					use_attention_mask
       SCREAMING_SNAKE_CASE__					=					use_token_type_ids
       SCREAMING_SNAKE_CASE__					=					use_labels
       SCREAMING_SNAKE_CASE__					=					vocab_size
       SCREAMING_SNAKE_CASE__					=					hidden_size
       SCREAMING_SNAKE_CASE__					=					num_hidden_layers
       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_choices






  def    lowercase_	(							self		:	Optional[Any]      )	->							int:
       SCREAMING_SNAKE_CASE__					=					ids_tensor([self.batch_size, self.seq_length]			,     self.vocab_size      )

       SCREAMING_SNAKE_CASE__					=					None
       if self.use_attention_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__					=					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			,     is_decoder=__lowerCamelCase			,     initializer_range=self.initializer_range			,     )

       return config, input_ids, token_type_ids, attention_mask







  def    lowercase_	(							self		:	int      )	->							List[Any]:
       SCREAMING_SNAKE_CASE__					=					self.prepare_config_and_inputs()
       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''': attention_mask}
       return config, inputs_dict






@require_flax
class  UpperCAmelCase__	( A__		,  unittest.TestCase ):






  """simple docstring"""


  a     =      (
      (
          FlaxAlbertModel,
          FlaxAlbertForPreTraining,
          FlaxAlbertForMaskedLM,
          FlaxAlbertForMultipleChoice,
          FlaxAlbertForQuestionAnswering,
          FlaxAlbertForSequenceClassification,
          FlaxAlbertForTokenClassification,
          FlaxAlbertForQuestionAnswering,
      )
      if is_flax_available()
      else ()
  )






  def    lowercase_	(							self		:	Tuple      )	->							Tuple:
       SCREAMING_SNAKE_CASE__					=					FlaxAlbertModelTester(self      )







  @slow
  def    lowercase_	(							self		:	str      )	->							Optional[Any]:
       for model_class_name in self.all_model_classes:
            SCREAMING_SNAKE_CASE__					=					model_class_name.from_pretrained('''albert-base-v2'''      )
            SCREAMING_SNAKE_CASE__					=					model(np.ones((1, 1)      )      )
            self.assertIsNotNone(__lowerCamelCase      )






@require_flax
class  UpperCAmelCase__	( unittest.TestCase ):






  """simple docstring"""


  @slow
  def    lowercase_	(							self		:	Any      )	->							Dict:
       SCREAMING_SNAKE_CASE__					=					FlaxAlbertModel.from_pretrained('''albert-base-v2'''      )
       SCREAMING_SNAKE_CASE__					=					np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]      )
       SCREAMING_SNAKE_CASE__					=					np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]      )
       SCREAMING_SNAKE_CASE__					=					model(__lowerCamelCase			,     attention_mask=__lowerCamelCase      )[0]
       SCREAMING_SNAKE_CASE__					=					(1, 11, 768)
       self.assertEqual(output.shape			,     __lowerCamelCase      )
       SCREAMING_SNAKE_CASE__					=					np.array(
           [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]]      )

       self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]			,     __lowerCamelCase			,     atol=1e-4      )      )





import math
import time

from transformers import Trainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


if is_torch_tpu_available(check_device=False):
       import torch_xla.core.xla_model as xm
       import torch_xla.debug.metrics as met






class  UpperCAmelCase__	( A__ ):






  """simple docstring"""


  def __init__(							self		:	Tuple			,     *__lowerCamelCase		:	List[str]			,     __lowerCamelCase		:	Any=None			,     __lowerCamelCase		:	Optional[int]=None			,     **__lowerCamelCase		:	Optional[int]      )	->							List[str]:
       super().__init__(*__lowerCamelCase			,     **__lowerCamelCase      )
       SCREAMING_SNAKE_CASE__					=					eval_examples
       SCREAMING_SNAKE_CASE__					=					post_process_function






  def    lowercase_	(							self		:	Optional[Any]			,     __lowerCamelCase		:	Dict=None			,     __lowerCamelCase		:	int=None			,     __lowerCamelCase		:	str=None			,     __lowerCamelCase		:	str = "eval"      )	->							Optional[Any]:
       SCREAMING_SNAKE_CASE__					=					self.eval_dataset if eval_dataset is None else eval_dataset
       SCREAMING_SNAKE_CASE__					=					self.get_eval_dataloader(__lowerCamelCase      )
       SCREAMING_SNAKE_CASE__					=					self.eval_examples if eval_examples is None else eval_examples

       # Temporarily disable metric computation, we will do it in the loop here.
       SCREAMING_SNAKE_CASE__					=					self.compute_metrics
       SCREAMING_SNAKE_CASE__					=					None
       SCREAMING_SNAKE_CASE__					=					self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
       SCREAMING_SNAKE_CASE__					=					time.time()
       try:
            SCREAMING_SNAKE_CASE__					=					eval_loop(
                __lowerCamelCase			,     description='''Evaluation'''			,     prediction_loss_only=True if compute_metrics is None else None			,     ignore_keys=__lowerCamelCase			,     metric_key_prefix=__lowerCamelCase			,     )
       finally:
            SCREAMING_SNAKE_CASE__					=					compute_metrics
       SCREAMING_SNAKE_CASE__					=					self.args.eval_batch_size * self.args.world_size
       if f'''{metric_key_prefix}_jit_compilation_time''' in output.metrics:
            start_time += output.metrics[f'''{metric_key_prefix}_jit_compilation_time''']
       output.metrics.update(
           speed_metrics(
               __lowerCamelCase			,     __lowerCamelCase			,     num_samples=output.num_samples			,     num_steps=math.ceil(output.num_samples / total_batch_size      )			,     )      )
       if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
            # Only the main node write the results by default
            SCREAMING_SNAKE_CASE__					=					self.post_process_function(__lowerCamelCase			,     __lowerCamelCase			,     output.predictions      )
            SCREAMING_SNAKE_CASE__					=					self.compute_metrics(__lowerCamelCase      )

            # Prefix all keys with metric_key_prefix + '_'
            for key in list(metrics.keys()      ):
                 if not key.startswith(f'''{metric_key_prefix}_'''      ):
                      SCREAMING_SNAKE_CASE__					=					metrics.pop(__lowerCamelCase      )
            metrics.update(output.metrics      )
       else:
            SCREAMING_SNAKE_CASE__					=					output.metrics

       if self.args.should_log:
            # Only the main node log the results by default
            self.log(__lowerCamelCase      )

       if self.args.tpu_metrics_debug or self.args.debug:
            # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
            xm.master_print(met.metrics_report()      )

       SCREAMING_SNAKE_CASE__					=					self.callback_handler.on_evaluate(self.args			,     self.state			,     self.control			,     __lowerCamelCase      )
       return metrics







  def    lowercase_	(							self		:	Dict			,     __lowerCamelCase		:	Dict			,     __lowerCamelCase		:	str			,     __lowerCamelCase		:	str=None			,     __lowerCamelCase		:	str = "test"      )	->							Any:
       SCREAMING_SNAKE_CASE__					=					self.get_test_dataloader(__lowerCamelCase      )

       # Temporarily disable metric computation, we will do it in the loop here.
       SCREAMING_SNAKE_CASE__					=					self.compute_metrics
       SCREAMING_SNAKE_CASE__					=					None
       SCREAMING_SNAKE_CASE__					=					self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
       SCREAMING_SNAKE_CASE__					=					time.time()
       try:
            SCREAMING_SNAKE_CASE__					=					eval_loop(
                __lowerCamelCase			,     description='''Prediction'''			,     prediction_loss_only=True if compute_metrics is None else None			,     ignore_keys=__lowerCamelCase			,     metric_key_prefix=__lowerCamelCase			,     )
       finally:
            SCREAMING_SNAKE_CASE__					=					compute_metrics
       SCREAMING_SNAKE_CASE__					=					self.args.eval_batch_size * self.args.world_size
       if f'''{metric_key_prefix}_jit_compilation_time''' in output.metrics:
            start_time += output.metrics[f'''{metric_key_prefix}_jit_compilation_time''']
       output.metrics.update(
           speed_metrics(
               __lowerCamelCase			,     __lowerCamelCase			,     num_samples=output.num_samples			,     num_steps=math.ceil(output.num_samples / total_batch_size      )			,     )      )

       if self.post_process_function is None or self.compute_metrics is None:
            return output

       SCREAMING_SNAKE_CASE__					=					self.post_process_function(__lowerCamelCase			,     __lowerCamelCase			,     output.predictions			,     '''predict'''      )
       SCREAMING_SNAKE_CASE__					=					self.compute_metrics(__lowerCamelCase      )

       # Prefix all keys with metric_key_prefix + '_'
       for key in list(metrics.keys()      ):
            if not key.startswith(f'''{metric_key_prefix}_'''      ):
                 SCREAMING_SNAKE_CASE__					=					metrics.pop(__lowerCamelCase      )
       metrics.update(output.metrics      )
       return PredictionOutput(predictions=predictions.predictions			,     label_ids=predictions.label_ids			,     metrics=__lowerCamelCase      )


