





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      lowercase__							(							unittest.TestCase  ):







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







  def 	UpperCAmelCase__		(	self						):
       lowerCAmelCase_					:       Tuple    =		ids_tensor([self.batch_size, self.seq_length]    ,						self.vocab_size						)

       lowerCAmelCase_					:       int    =		None
       if self.use_attention_mask:
            lowerCAmelCase_					:       List[Any]    =		random_attention_mask([self.batch_size, self.seq_length]						)

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

       lowerCAmelCase_					:       Any    =		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=_lowercase    ,						initializer_range=self.initializer_range    ,						)

       return config, input_ids, token_type_ids, attention_mask







  def 	UpperCAmelCase__		(	self						):
       lowerCAmelCase_					:       Optional[Any]    =		self.prepare_config_and_inputs()
       lowerCAmelCase_					:       Optional[Any]    =		config_and_inputs
       lowerCAmelCase_					:       List[Any]    =		{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask}
       return config, inputs_dict





@require_flax
class      lowercase__							(							__lowercase     ,					unittest.TestCase  ):
  __UpperCamelCase	     =  (
      (
          FlaxAlbertModel,
          FlaxAlbertForPreTraining,
          FlaxAlbertForMaskedLM,
          FlaxAlbertForMultipleChoice,
          FlaxAlbertForQuestionAnswering,
          FlaxAlbertForSequenceClassification,
          FlaxAlbertForTokenClassification,
          FlaxAlbertForQuestionAnswering,
      )
      if is_flax_available()
      else ()
  )







  def 	UpperCAmelCase__		(	self						):
       lowerCAmelCase_					:       Optional[Any]    =		FlaxAlbertModelTester(self						)







  @slow
  def 	UpperCAmelCase__		(	self						):
       for model_class_name in self.all_model_classes:
            lowerCAmelCase_					:       int    =		model_class_name.from_pretrained("""albert-base-v2"""						)
            lowerCAmelCase_					:       List[Any]    =		model(np.ones((1, 1)						)						)
            self.assertIsNotNone(_lowercase						)





@require_flax
class      lowercase__							(							unittest.TestCase  ):







  @slow
  def 	UpperCAmelCase__		(	self						):
       lowerCAmelCase_					:       str    =		FlaxAlbertModel.from_pretrained("""albert-base-v2"""						)
       lowerCAmelCase_					:       int    =		np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]						)
       lowerCAmelCase_					:       int    =		np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
       lowerCAmelCase_					:       List[Any]    =		model(_lowercase    ,						attention_mask=_lowercase						)[0]
       lowerCAmelCase_					:       List[str]    =		(1, 11, 768)
       self.assertEqual(output.shape    ,						_lowercase						)
       lowerCAmelCase_					:       Any    =		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]    ,						_lowercase    ,						atol=1e-4						)						)







from collections import OrderedDict
from typing import Any, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...feature_extraction_utils import FeatureExtractionMixin
from ...onnx import OnnxConfig
from ...onnx.utils import compute_effective_axis_dimension
from ...tokenization_utils_base import PreTrainedTokenizerBase
from ...utils import TensorType, logging


UpperCAmelCase_					:   Optional[Any] 							=						logging.get_logger(__name__)

UpperCAmelCase_					:   Any 							=						{
    """deepmind/language-perceiver""": """https://huggingface.co/deepmind/language-perceiver/resolve/main/config.json""",
    # See all Perceiver models at https://huggingface.co/models?filter=perceiver
}





class      lowercase__    (							__A  ):
  __UpperCamelCase	     =  """perceiver"""







  def __init__(	self    ,						_lowercase=256    ,						_lowercase=1_280    ,						_lowercase=768    ,						_lowercase=1    ,						_lowercase=26    ,						_lowercase=8    ,						_lowercase=8    ,						_lowercase=None    ,						_lowercase=None    ,						_lowercase="kv"    ,						_lowercase=1    ,						_lowercase=1    ,						_lowercase="gelu"    ,						_lowercase=0.1    ,						_lowercase=0.02    ,						_lowercase=1e-12    ,						_lowercase=True    ,						_lowercase=262    ,						_lowercase=2_048    ,						_lowercase=56    ,						_lowercase=[368, 496]    ,						_lowercase=16    ,						_lowercase=1_920    ,						_lowercase=16    ,						_lowercase=[1, 16, 224, 224]    ,						**_lowercase    ,						):
       super().__init__(**_lowercase						)

       lowerCAmelCase_					:       Optional[int]    =		num_latents
       lowerCAmelCase_					:       List[str]    =		d_latents
       lowerCAmelCase_					:       int    =		d_model
       lowerCAmelCase_					:       Dict    =		num_blocks
       lowerCAmelCase_					:       Union[str, Any]    =		num_self_attends_per_block
       lowerCAmelCase_					:       List[str]    =		num_self_attention_heads
       lowerCAmelCase_					:       List[str]    =		num_cross_attention_heads
       lowerCAmelCase_					:       List[Any]    =		qk_channels
       lowerCAmelCase_					:       Optional[Any]    =		v_channels
       lowerCAmelCase_					:       Optional[Any]    =		cross_attention_shape_for_attention
       lowerCAmelCase_					:       Optional[int]    =		self_attention_widening_factor
       lowerCAmelCase_					:       int    =		cross_attention_widening_factor
       lowerCAmelCase_					:       Union[str, Any]    =		hidden_act
       lowerCAmelCase_					:       int    =		attention_probs_dropout_prob
       lowerCAmelCase_					:       int    =		initializer_range
       lowerCAmelCase_					:       Optional[Any]    =		layer_norm_eps
       lowerCAmelCase_					:       Optional[Any]    =		use_query_residual
       # masked language modeling attributes
       lowerCAmelCase_					:       Tuple    =		vocab_size
       lowerCAmelCase_					:       List[Any]    =		max_position_embeddings
       # image classification attributes
       lowerCAmelCase_					:       List[Any]    =		image_size
       # flow attributes
       lowerCAmelCase_					:       Dict    =		train_size
       # multimodal autoencoding attributes
       lowerCAmelCase_					:       Optional[Any]    =		num_frames
       lowerCAmelCase_					:       int    =		audio_samples_per_frame
       lowerCAmelCase_					:       Any    =		samples_per_patch
       lowerCAmelCase_					:       Any    =		output_shape





class      lowercase__    (							__A  ):







  @property
  def 	UpperCAmelCase__		(	self						):
       if self.task == "multiple-choice":
            lowerCAmelCase_					:       Optional[Any]    =		{0: """batch""", 1: """choice""", 2: """sequence"""}
       else:
            lowerCAmelCase_					:       Optional[int]    =		{0: """batch""", 1: """sequence"""}
       return OrderedDict(
           [
               ("""inputs""", dynamic_axis),
               ("""attention_mask""", dynamic_axis),
           ]						)







  @property
  def 	UpperCAmelCase__		(	self						):
       return 1e-4







  def 	UpperCAmelCase__		(	self    ,						_lowercase    ,						_lowercase = -1    ,						_lowercase = -1    ,						_lowercase = -1    ,						_lowercase = False    ,						_lowercase = None    ,						_lowercase = 3    ,						_lowercase = 40    ,						_lowercase = 40    ,						):
       # copied from `transformers.onnx.config.OnnxConfig` and slightly altered/simplified

       if isinstance(_lowercase    ,						_lowercase						):
            # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
            lowerCAmelCase_					:       Union[str, Any]    =		compute_effective_axis_dimension(
                _lowercase    ,						fixed_dimension=OnnxConfig.default_fixed_batch    ,						num_token_to_add=0						)
            # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
            lowerCAmelCase_					:       int    =		preprocessor.num_special_tokens_to_add(_lowercase						)
            lowerCAmelCase_					:       int    =		compute_effective_axis_dimension(
                _lowercase    ,						fixed_dimension=OnnxConfig.default_fixed_sequence    ,						num_token_to_add=_lowercase						)
            # Generate dummy inputs according to compute batch and sequence
            lowerCAmelCase_					:       Optional[int]    =		[""" """.join(["""a"""]						) * seq_length] * batch_size
            lowerCAmelCase_					:       Optional[Any]    =		dict(preprocessor(_lowercase    ,						return_tensors=_lowercase						)						)
            lowerCAmelCase_					:       Optional[Any]    =		inputs.pop("""input_ids"""						)
            return inputs
       elif isinstance(_lowercase    ,						_lowercase						) and preprocessor.model_input_names[0] == "pixel_values":
            # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
            lowerCAmelCase_					:       Any    =		compute_effective_axis_dimension(_lowercase    ,						fixed_dimension=OnnxConfig.default_fixed_batch						)
            lowerCAmelCase_					:       str    =		self._generate_dummy_images(_lowercase    ,						_lowercase    ,						_lowercase    ,						_lowercase						)
            lowerCAmelCase_					:       List[str]    =		dict(preprocessor(images=_lowercase    ,						return_tensors=_lowercase						)						)
            lowerCAmelCase_					:       Tuple    =		inputs.pop("""pixel_values"""						)
            return inputs
       else:
            raise ValueError(
                """Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor."""						)
