


import inspect
import unittest

import numpy as np

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

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
		import jax

		from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel

class 						__a (							unittest.TestCase     ):
		def __init__(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE=13     ,       _SCREAMING_SNAKE_CASE=30     ,       _SCREAMING_SNAKE_CASE=2     ,       _SCREAMING_SNAKE_CASE=3     ,       _SCREAMING_SNAKE_CASE=True     ,       _SCREAMING_SNAKE_CASE=True     ,       _SCREAMING_SNAKE_CASE=32     ,       _SCREAMING_SNAKE_CASE=5     ,       _SCREAMING_SNAKE_CASE=4     ,       _SCREAMING_SNAKE_CASE=37     ,       _SCREAMING_SNAKE_CASE="gelu"     ,       _SCREAMING_SNAKE_CASE=0.1     ,       _SCREAMING_SNAKE_CASE=0.1     ,       _SCREAMING_SNAKE_CASE=10     ,       _SCREAMING_SNAKE_CASE=0.02     ,       )					->			int:




									"""simple docstring"""





									_UpperCAmelCase											=     parent
									_UpperCAmelCase											=     batch_size
									_UpperCAmelCase											=     image_size
									_UpperCAmelCase											=     patch_size
									_UpperCAmelCase											=     num_channels
									_UpperCAmelCase											=     is_training
									_UpperCAmelCase											=     use_labels
									_UpperCAmelCase											=     hidden_size
									_UpperCAmelCase											=     num_hidden_layers
									_UpperCAmelCase											=     num_attention_heads
									_UpperCAmelCase											=     intermediate_size
									_UpperCAmelCase											=     hidden_act
									_UpperCAmelCase											=     hidden_dropout_prob
									_UpperCAmelCase											=     attention_probs_dropout_prob
									_UpperCAmelCase											=     type_sequence_label_size
									_UpperCAmelCase											=     initializer_range

									# in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
									_UpperCAmelCase											=     (image_size // patch_size) ** 2
									_UpperCAmelCase											=     num_patches + 1
		def       UpperCAmelCase__							(     self		)					->			Optional[int]:




									"""simple docstring"""





									_UpperCAmelCase											=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

									_UpperCAmelCase											=     ViTConfig(
									    image_size=self.image_size     ,       patch_size=self.patch_size     ,       num_channels=self.num_channels     ,       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     ,       is_decoder=_SCREAMING_SNAKE_CASE     ,       initializer_range=self.initializer_range     ,       )

									return config, pixel_values
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			str:




									"""simple docstring"""





									_UpperCAmelCase											=     FlaxViTModel(config=_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     model(_SCREAMING_SNAKE_CASE		)
									# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
									_UpperCAmelCase											=     (self.image_size, self.image_size)
									_UpperCAmelCase											=     (self.patch_size, self.patch_size)
									_UpperCAmelCase											=     (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
									self.parent.assertEqual(result.last_hidden_state.shape     ,       (self.batch_size, num_patches + 1, self.hidden_size)		)
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			Any:




									"""simple docstring"""





									_UpperCAmelCase											=     self.type_sequence_label_size
									_UpperCAmelCase											=     FlaxViTForImageClassification(config=_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     model(_SCREAMING_SNAKE_CASE		)
									self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.type_sequence_label_size)		)

									# test greyscale images
									_UpperCAmelCase											=     1
									_UpperCAmelCase											=     FlaxViTForImageClassification(_SCREAMING_SNAKE_CASE		)

									_UpperCAmelCase											=     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
									_UpperCAmelCase											=     model(_SCREAMING_SNAKE_CASE		)






		def       UpperCAmelCase__							(     self		)					->			str:




									"""simple docstring"""





									_UpperCAmelCase											=     self.prepare_config_and_inputs()
									(
									    (
									    _UpperCAmelCase
									)						,			(
									    _UpperCAmelCase
									)						,			
									)											=     config_and_inputs
									_UpperCAmelCase											=     {'pixel_values': pixel_values}
									return config, inputs_dict



@require_flax
class 						__a (							UpperCAmelCase       , unittest.TestCase     ):
		_a      :    List[str]           =      (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()
		def       UpperCAmelCase__							(     self		)					->			None:




									"""simple docstring"""





									_UpperCAmelCase											=     FlaxViTModelTester(self		)
									_UpperCAmelCase											=     ConfigTester(self     ,       config_class=_SCREAMING_SNAKE_CASE     ,       has_text_modality=_SCREAMING_SNAKE_CASE     ,       hidden_size=37		)
		def       UpperCAmelCase__							(     self		)					->			Dict:




									"""simple docstring"""





									self.config_tester.run_common_tests()
		def       UpperCAmelCase__							(     self		)					->			Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			List[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			str:




									"""simple docstring"""





									_UpperCAmelCase						,			_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
																_UpperCAmelCase											=     model_class(_SCREAMING_SNAKE_CASE		)
																_UpperCAmelCase											=     inspect.signature(model.__call__		)
																# signature.parameters is an OrderedDict => so arg_names order is deterministic
																_UpperCAmelCase											=     [*signature.parameters.keys()]

																_UpperCAmelCase											=     ['pixel_values']
																self.assertListEqual(arg_names[:1]     ,       _SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			int:




									"""simple docstring"""





									_UpperCAmelCase						,			_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
																with self.subTest(model_class.__name__		):
																							_UpperCAmelCase											=     self._prepare_for_class(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)
																							_UpperCAmelCase											=     model_class(_SCREAMING_SNAKE_CASE		)

																							@jax.jit
																							def model_jitted(_SCREAMING_SNAKE_CASE     ,       **_SCREAMING_SNAKE_CASE		):
																														return model(pixel_values=_SCREAMING_SNAKE_CASE     ,       **_SCREAMING_SNAKE_CASE		)

																							with self.subTest('JIT Enabled'		):
																														_UpperCAmelCase											=     model_jitted(**_SCREAMING_SNAKE_CASE		).to_tuple()

																							with self.subTest('JIT Disabled'		):
																														with jax.disable_jit():
																																					_UpperCAmelCase											=     model_jitted(**_SCREAMING_SNAKE_CASE		).to_tuple()

																							self.assertEqual(len(_SCREAMING_SNAKE_CASE		)     ,       len(_SCREAMING_SNAKE_CASE		)		)
																							for jitted_output, output in zip(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		):
																														self.assertEqual(jitted_output.shape     ,       output.shape		)






		@slow
		def       UpperCAmelCase__							(     self		)					->			Dict:




									"""simple docstring"""





									for model_class_name in self.all_model_classes:
																_UpperCAmelCase											=     model_class_name.from_pretrained('google/vit-base-patch16-224'		)
																_UpperCAmelCase											=     model(np.ones((1, 3, 224, 224)		)		)
																self.assertIsNotNone(_SCREAMING_SNAKE_CASE		)




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


lowerCAmelCase__    :Dict       =				logging.get_logger(__name__)

lowerCAmelCase__    :Optional[int]       =				{
    '''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 						__a (							UpperCAmelCase     ):
		_a      :    Tuple           =      'perceiver'
		def __init__(     self     ,       _SCREAMING_SNAKE_CASE=256     ,       _SCREAMING_SNAKE_CASE=1280     ,       _SCREAMING_SNAKE_CASE=768     ,       _SCREAMING_SNAKE_CASE=1     ,       _SCREAMING_SNAKE_CASE=26     ,       _SCREAMING_SNAKE_CASE=8     ,       _SCREAMING_SNAKE_CASE=8     ,       _SCREAMING_SNAKE_CASE=None     ,       _SCREAMING_SNAKE_CASE=None     ,       _SCREAMING_SNAKE_CASE="kv"     ,       _SCREAMING_SNAKE_CASE=1     ,       _SCREAMING_SNAKE_CASE=1     ,       _SCREAMING_SNAKE_CASE="gelu"     ,       _SCREAMING_SNAKE_CASE=0.1     ,       _SCREAMING_SNAKE_CASE=0.02     ,       _SCREAMING_SNAKE_CASE=1e-1_2     ,       _SCREAMING_SNAKE_CASE=True     ,       _SCREAMING_SNAKE_CASE=262     ,       _SCREAMING_SNAKE_CASE=2048     ,       _SCREAMING_SNAKE_CASE=56     ,       _SCREAMING_SNAKE_CASE=[368, 496]     ,       _SCREAMING_SNAKE_CASE=16     ,       _SCREAMING_SNAKE_CASE=1920     ,       _SCREAMING_SNAKE_CASE=16     ,       _SCREAMING_SNAKE_CASE=[1, 16, 224, 224]     ,       **_SCREAMING_SNAKE_CASE     ,       )					->			Any:




									"""simple docstring"""





									super().__init__(**_SCREAMING_SNAKE_CASE		)

									_UpperCAmelCase											=     num_latents
									_UpperCAmelCase											=     d_latents
									_UpperCAmelCase											=     d_model
									_UpperCAmelCase											=     num_blocks
									_UpperCAmelCase											=     num_self_attends_per_block
									_UpperCAmelCase											=     num_self_attention_heads
									_UpperCAmelCase											=     num_cross_attention_heads
									_UpperCAmelCase											=     qk_channels
									_UpperCAmelCase											=     v_channels
									_UpperCAmelCase											=     cross_attention_shape_for_attention
									_UpperCAmelCase											=     self_attention_widening_factor
									_UpperCAmelCase											=     cross_attention_widening_factor
									_UpperCAmelCase											=     hidden_act
									_UpperCAmelCase											=     attention_probs_dropout_prob
									_UpperCAmelCase											=     initializer_range
									_UpperCAmelCase											=     layer_norm_eps
									_UpperCAmelCase											=     use_query_residual
									# masked language modeling attributes
									_UpperCAmelCase											=     vocab_size
									_UpperCAmelCase											=     max_position_embeddings
									# image classification attributes
									_UpperCAmelCase											=     image_size
									# flow attributes
									_UpperCAmelCase											=     train_size
									# multimodal autoencoding attributes
									_UpperCAmelCase											=     num_frames
									_UpperCAmelCase											=     audio_samples_per_frame
									_UpperCAmelCase											=     samples_per_patch
									_UpperCAmelCase											=     output_shape



class 						__a (							UpperCAmelCase     ):
		@property
		def       UpperCAmelCase__							(     self		)					->			Mapping[str, Mapping[int, str]]:




									"""simple docstring"""





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




									"""simple docstring"""





									return 1e-4






		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE = -1     ,       _SCREAMING_SNAKE_CASE = -1     ,       _SCREAMING_SNAKE_CASE = -1     ,       _SCREAMING_SNAKE_CASE = False     ,       _SCREAMING_SNAKE_CASE = None     ,       _SCREAMING_SNAKE_CASE = 3     ,       _SCREAMING_SNAKE_CASE = 40     ,       _SCREAMING_SNAKE_CASE = 40     ,       )					->			Mapping[str, Any]:




									"""simple docstring"""





									if isinstance(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		):
																# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
																_UpperCAmelCase											=     compute_effective_axis_dimension(
																    _SCREAMING_SNAKE_CASE     ,       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
																_UpperCAmelCase											=     preprocessor.num_special_tokens_to_add(_SCREAMING_SNAKE_CASE		)
																_UpperCAmelCase											=     compute_effective_axis_dimension(
																    _SCREAMING_SNAKE_CASE     ,       fixed_dimension=OnnxConfig.default_fixed_sequence     ,       num_token_to_add=_SCREAMING_SNAKE_CASE		)
																# Generate dummy inputs according to compute batch and sequence
																_UpperCAmelCase											=     [' '.join(['a']		) * seq_length] * batch_size
																_UpperCAmelCase											=     dict(preprocessor(_SCREAMING_SNAKE_CASE     ,       return_tensors=_SCREAMING_SNAKE_CASE		)		)
																_UpperCAmelCase											=     inputs.pop('input_ids'		)
																return inputs
									elif isinstance(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		) 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
																_UpperCAmelCase											=     compute_effective_axis_dimension(_SCREAMING_SNAKE_CASE     ,       fixed_dimension=OnnxConfig.default_fixed_batch		)
																_UpperCAmelCase											=     self._generate_dummy_images(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)
																_UpperCAmelCase											=     dict(preprocessor(images=_SCREAMING_SNAKE_CASE     ,       return_tensors=_SCREAMING_SNAKE_CASE		)		)
																_UpperCAmelCase											=     inputs.pop('pixel_values'		)
																return inputs
									else:
																raise ValueError(
																    'Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor.'		)
