





'''simple docstring'''






import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
						import tensorflow as tf




class 				UpperCAmelCase  (    TensorFormatter[Mapping, '''tf.Tensor''', Mapping]     ):




						def __init__(    self    :				Any    ,    __snake_case    :				Dict=None    ,    **__snake_case    :				Any						)       ->   Dict:
												super().__init__(features=__snake_case						)
												_lowerCAmelCase							  =							tf_tensor_kwargs
												import tensorflow as tf  # noqa: import tf at initialization




						def        lowercase__					(    self    :				Optional[int]    ,    __snake_case    :				Union[str, Any]						)       ->   int:
												import tensorflow as tf

												if isinstance(__snake_case    ,    __snake_case						) and column:
																		if all(
																		    isinstance(__snake_case    ,    tf.Tensor						) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column						):
																								return tf.stack(__snake_case						)
																		elif all(
																		    isinstance(__snake_case    ,    (tf.Tensor, tf.RaggedTensor)						) and x.ndim == 1 and x.dtype == column[0].dtype
																		    for x in column						):
																								# only rag 1-D tensors, otherwise some dimensions become ragged even though they were consolidated
																								return tf.ragged.stack(__snake_case						)

												return column




						def        lowercase__					(    self    :				List[str]    ,    __snake_case    :				Union[str, Any]						)       ->   Any:
												import tensorflow as tf

												if value is None:
																		return value

												_lowerCAmelCase							  =							{}

												if isinstance(__snake_case    ,    (np.number, np.ndarray)						) and np.issubdtype(value.dtype    ,    np.integer						):
																		_lowerCAmelCase							  =							{"""dtype""": tf.intaa}
												elif isinstance(__snake_case    ,    (np.number, np.ndarray)						) and np.issubdtype(value.dtype    ,    np.floating						):
																		_lowerCAmelCase							  =							{"""dtype""": tf.floataa}
												elif config.PIL_AVAILABLE and "PIL" in sys.modules:
																		import PIL.Image

																		if isinstance(__snake_case    ,    PIL.Image.Image						):
																								_lowerCAmelCase							  =							np.asarray(__snake_case						)

												return tf.convert_to_tensor(__snake_case    ,    **{**default_dtype, **self.tf_tensor_kwargs}						)




						def        lowercase__					(    self    :				Dict    ,    __snake_case    :				Optional[int]						)       ->   List[Any]:
												import tensorflow as tf

												# support for torch, tf, jax etc.
												if config.TORCH_AVAILABLE and "torch" in sys.modules:
																		import torch

																		if isinstance(__snake_case    ,    torch.Tensor						):
																								return self._tensorize(data_struct.detach().cpu().numpy()[()]						)
												if hasattr(__snake_case    ,    """__array__"""						) and not isinstance(__snake_case    ,    tf.Tensor						):
																		_lowerCAmelCase							  =							data_struct.__array__()
												# support for nested types like struct of list of struct
												if isinstance(__snake_case    ,    np.ndarray						):
																		if data_struct.dtype == object:  # tf tensors cannot be instantied from an array of objects
																								return self._consolidate([self.recursive_tensorize(__snake_case						) for substruct in data_struct]						)
												elif isinstance(__snake_case    ,    (list, tuple)						):
																		return self._consolidate([self.recursive_tensorize(__snake_case						) for substruct in data_struct]						)
												return self._tensorize(__snake_case						)




						def        lowercase__					(    self    :				Tuple    ,    __snake_case    :				dict						)       ->   Tuple:
												return map_nested(self._recursive_tensorize    ,    __snake_case    ,    map_list=__snake_case						)




						def        lowercase__					(    self    :				int    ,    __snake_case    :				pa.Table						)       ->   Mapping:
												_lowerCAmelCase							  =							self.numpy_arrow_extractor().extract_row(__snake_case						)
												_lowerCAmelCase							  =							self.python_features_decoder.decode_row(__snake_case						)
												return self.recursive_tensorize(__snake_case						)




						def        lowercase__					(    self    :				Union[str, Any]    ,    __snake_case    :				pa.Table						)       ->   "tf.Tensor":
												_lowerCAmelCase							  =							self.numpy_arrow_extractor().extract_column(__snake_case						)
												_lowerCAmelCase							  =							self.python_features_decoder.decode_column(__snake_case    ,    pa_table.column_names[0]						)
												_lowerCAmelCase							  =							self.recursive_tensorize(__snake_case						)
												_lowerCAmelCase							  =							self._consolidate(__snake_case						)
												return column




						def        lowercase__					(    self    :				Tuple    ,    __snake_case    :				pa.Table						)       ->   Mapping:
												_lowerCAmelCase							  =							self.numpy_arrow_extractor().extract_batch(__snake_case						)
												_lowerCAmelCase							  =							self.python_features_decoder.decode_batch(__snake_case						)
												_lowerCAmelCase							  =							self.recursive_tensorize(__snake_case						)
												for column_name in batch:
																		_lowerCAmelCase							  =							self._consolidate(batch[column_name]						)
												return batch













'''simple docstring'''






import unittest

import numpy as np

from transformers import ElectraConfig, 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():
      from transformers.models.electra.modeling_flax_electra import (
          FlaxElectraForCausalLM,
          FlaxElectraForMaskedLM,
          FlaxElectraForMultipleChoice,
          FlaxElectraForPreTraining,
          FlaxElectraForQuestionAnswering,
          FlaxElectraForSequenceClassification,
          FlaxElectraForTokenClassification,
          FlaxElectraModel,
      )




class 				UpperCAmelCase  (    unittest.TestCase     ):




      def __init__(    self    :				str    ,    __snake_case    :				str    ,    __snake_case    :				str=13    ,    __snake_case    :				Optional[Any]=7    ,    __snake_case    :				str=True    ,    __snake_case    :				Any=True    ,    __snake_case    :				List[str]=True    ,    __snake_case    :				Any=True    ,    __snake_case    :				List[str]=99    ,    __snake_case    :				Dict=24    ,    __snake_case    :				Any=32    ,    __snake_case    :				List[str]=5    ,    __snake_case    :				Any=4    ,    __snake_case    :				Optional[Any]=37    ,    __snake_case    :				Union[str, Any]="gelu"    ,    __snake_case    :				Dict=0.1    ,    __snake_case    :				int=0.1    ,    __snake_case    :				Dict=5_12    ,    __snake_case    :				List[Any]=16    ,    __snake_case    :				Dict=2    ,    __snake_case    :				str=0.02    ,    __snake_case    :				Any=4    ,    )       ->   Union[str, Any]:
            _lowerCAmelCase							  =							parent
            _lowerCAmelCase							  =							batch_size
            _lowerCAmelCase							  =							seq_length
            _lowerCAmelCase							  =							is_training
            _lowerCAmelCase							  =							use_attention_mask
            _lowerCAmelCase							  =							use_token_type_ids
            _lowerCAmelCase							  =							use_labels
            _lowerCAmelCase							  =							vocab_size
            _lowerCAmelCase							  =							hidden_size
            _lowerCAmelCase							  =							embedding_size
            _lowerCAmelCase							  =							num_hidden_layers
            _lowerCAmelCase							  =							num_attention_heads
            _lowerCAmelCase							  =							intermediate_size
            _lowerCAmelCase							  =							hidden_act
            _lowerCAmelCase							  =							hidden_dropout_prob
            _lowerCAmelCase							  =							attention_probs_dropout_prob
            _lowerCAmelCase							  =							max_position_embeddings
            _lowerCAmelCase							  =							type_vocab_size
            _lowerCAmelCase							  =							type_sequence_label_size
            _lowerCAmelCase							  =							initializer_range
            _lowerCAmelCase							  =							num_choices




      def        lowercase__					(    self    :				Optional[Any]						)       ->   Optional[Any]:
            _lowerCAmelCase							  =							ids_tensor([self.batch_size, self.seq_length]    ,    self.vocab_size						)

            _lowerCAmelCase							  =							None
            if self.use_attention_mask:
                  _lowerCAmelCase							  =							random_attention_mask([self.batch_size, self.seq_length]						)

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

            _lowerCAmelCase							  =							ElectraConfig(
                vocab_size=self.vocab_size    ,    hidden_size=self.hidden_size    ,    embedding_size=self.embedding_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    ,    )

            return config, input_ids, token_type_ids, attention_mask




      def        lowercase__					(    self    :				Any						)       ->   Optional[int]:
            _lowerCAmelCase							  =							self.prepare_config_and_inputs()
            _lowerCAmelCase  ,							_lowerCAmelCase  ,							_lowerCAmelCase  ,							_lowerCAmelCase							  =							config_and_inputs
            _lowerCAmelCase							  =							{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask}
            return config, inputs_dict




@require_flax
class 				UpperCAmelCase  (    snake_case_							,	unittest.TestCase     ):
      _lowercase:    str		  =     True

      _lowercase:    Optional[Any]		  =     (
          (
              FlaxElectraModel,
              FlaxElectraForCausalLM,
              FlaxElectraForMaskedLM,
              FlaxElectraForPreTraining,
              FlaxElectraForTokenClassification,
              FlaxElectraForQuestionAnswering,
              FlaxElectraForMultipleChoice,
              FlaxElectraForSequenceClassification,
          )
          if is_flax_available()
          else ()
      )




      def        lowercase__					(    self    :				Tuple						)       ->   Optional[int]:
            _lowerCAmelCase							  =							FlaxElectraModelTester(self						)




      @slow
      def        lowercase__					(    self    :				List[Any]						)       ->   str:
            for model_class_name in self.all_model_classes:
                  if model_class_name == FlaxElectraForMaskedLM:
                        _lowerCAmelCase							  =							model_class_name.from_pretrained("""google/electra-small-generator"""						)
                  else:
                        _lowerCAmelCase							  =							model_class_name.from_pretrained("""google/electra-small-discriminator"""						)
                  _lowerCAmelCase							  =							model(np.ones((1, 1)						)						)
                  self.assertIsNotNone(__snake_case						)






