





'''simple docstring'''






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


A_		:  Tuple		 =						logging.get_logger(__name__)

A_		:  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 					__snake_case  (					__SCREAMING_SNAKE_CASE							):






   '''simple docstring'''





   lowerCamelCase__    							=      '''perceiver'''

   def __init__(							self	,		__SCREAMING_SNAKE_CASE=2_5_6	,		__SCREAMING_SNAKE_CASE=1_2_8_0	,		__SCREAMING_SNAKE_CASE=7_6_8	,		__SCREAMING_SNAKE_CASE=1	,		__SCREAMING_SNAKE_CASE=2_6	,		__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=2_6_2	,		__SCREAMING_SNAKE_CASE=2_0_4_8	,		__SCREAMING_SNAKE_CASE=5_6	,		__SCREAMING_SNAKE_CASE=[3_6_8, 4_9_6]	,		__SCREAMING_SNAKE_CASE=1_6	,		__SCREAMING_SNAKE_CASE=1_9_2_0	,		__SCREAMING_SNAKE_CASE=1_6	,		__SCREAMING_SNAKE_CASE=[1, 1_6, 2_2_4, 2_2_4]	,		**__SCREAMING_SNAKE_CASE	,		):
    super().__init__(**__SCREAMING_SNAKE_CASE       )

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



class 					__snake_case  (					__SCREAMING_SNAKE_CASE							):






   '''simple docstring'''





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

   @property
   def 		__UpperCamelCase						(							self       ):
    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 = 4_0	,		__SCREAMING_SNAKE_CASE = 4_0	,		):
    # copied from `transformers.onnx.config.OnnxConfig` and slightly altered/simplified

    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
     snake_case__     :					Any    =  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
     snake_case__     :					str    =  preprocessor.num_special_tokens_to_add(__SCREAMING_SNAKE_CASE       )
     snake_case__     :					Optional[int]    =  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
     snake_case__     :					Any    =  [""" """.join(["""a"""]       ) * seq_length] * batch_size
     snake_case__     :					Tuple    =  dict(preprocessor(__SCREAMING_SNAKE_CASE	,		return_tensors=__SCREAMING_SNAKE_CASE       )       )
     snake_case__     :					Dict    =  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
     snake_case__     :					Optional[int]    =  compute_effective_axis_dimension(__SCREAMING_SNAKE_CASE	,		fixed_dimension=OnnxConfig.default_fixed_batch       )
     snake_case__     :					Any    =  self._generate_dummy_images(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       )
     snake_case__     :					str    =  dict(preprocessor(images=__SCREAMING_SNAKE_CASE	,		return_tensors=__SCREAMING_SNAKE_CASE       )       )
     snake_case__     :					List[str]    =  inputs.pop("""pixel_values"""       )
     return inputs
    else:
     raise ValueError(
         """Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor."""       )





'''simple docstring'''

import contextlib
import importlib
import io
import unittest

import transformers

# Try to import everything from transformers to ensure every object can be loaded.
from transformers import *  # noqa F406
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, require_tf, require_torch
from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available


if is_torch_available():
 from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification

if is_tf_available():
 from transformers import TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification

if is_flax_available():
 from transformers import FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification


A_     :						Optional[Any]    							=DUMMY_UNKNOWN_IDENTIFIER
# An actual model hosted on huggingface.co

A_     :						Tuple    							='''main'''
# Default branch name
A_     :						Dict    							='''f2c752cfc5c0ab6f4bdec59acea69eefbee381c2'''
# One particular commit (not the top of `main`)
A_     :						int    							='''aaaaaaa'''
# This commit does not exist, so we should 404.

A_     :						List[str]    							='''d9e9f15bc825e4b2c9249e9578f884bbcb5e3684'''
# Sha-1 of config.json on the top of `main`, for checking purposes
A_     :						List[str]    							='''4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3'''



@contextlib.contextmanager
def snake_case_      (					)			->					Tuple:
   print('''Welcome!''')
   yield
   print('''Bye!''')



@contextlib.contextmanager
def snake_case_      (					)			->					str:
   print('''Bonjour!''')
   yield
   print('''Au revoir!''')


class     __UpperCAmelCase       ( unittest.TestCase		):





  def 						UpperCAmelCase_	(	self ):
     # If the spec is missing, importlib would not be able to import the module dynamically.
     assert transformers.__spec__ is not None
     assert importlib.util.find_spec('''transformers''' ) is not None






class     __UpperCAmelCase       ( unittest.TestCase		):





  @unittest.mock.patch('''sys.stdout'''    ,     new_callable=io.StringIO )
  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase ):
     with ContextManagers([] ):
        print('''Transformers are awesome!''' )
     # The print statement adds a new line at the end of the output
     self.assertEqual(mock_stdout.getvalue()    ,     '''Transformers are awesome!\n''' )





  @unittest.mock.patch('''sys.stdout'''    ,     new_callable=io.StringIO )
  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase ):
     with ContextManagers([context_en()] ):
        print('''Transformers are awesome!''' )
     # The output should be wrapped with an English welcome and goodbye
     self.assertEqual(mock_stdout.getvalue()    ,     '''Welcome!\nTransformers are awesome!\nBye!\n''' )





  @unittest.mock.patch('''sys.stdout'''    ,     new_callable=io.StringIO )
  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase ):
     with ContextManagers([context_fr(), context_en()] ):
        print('''Transformers are awesome!''' )
     # The output should be wrapped with an English and French welcome and goodbye
     self.assertEqual(mock_stdout.getvalue()    ,     '''Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n''' )





  @require_torch
  def 						UpperCAmelCase_	(	self ):
     self.assertEqual(find_labels(_lowerCamelCase )    ,     ['''labels'''] )
     self.assertEqual(find_labels(_lowerCamelCase )    ,     ['''labels''', '''next_sentence_label'''] )
     self.assertEqual(find_labels(_lowerCamelCase )    ,     ['''start_positions''', '''end_positions'''] )


     class     __UpperCAmelCase       ( __a		):
       pass






     self.assertEqual(find_labels(_lowerCamelCase )    ,     ['''labels'''] )





  @require_tf
  def 						UpperCAmelCase_	(	self ):
     self.assertEqual(find_labels(_lowerCamelCase )    ,     ['''labels'''] )
     self.assertEqual(find_labels(_lowerCamelCase )    ,     ['''labels''', '''next_sentence_label'''] )
     self.assertEqual(find_labels(_lowerCamelCase )    ,     ['''start_positions''', '''end_positions'''] )


     class     __UpperCAmelCase       ( __a		):
       pass






     self.assertEqual(find_labels(_lowerCamelCase )    ,     ['''labels'''] )







  @require_flax
  def 						UpperCAmelCase_	(	self ):
     # Flax models don't have labels
     self.assertEqual(find_labels(_lowerCamelCase )    ,     [] )
     self.assertEqual(find_labels(_lowerCamelCase )    ,     [] )
     self.assertEqual(find_labels(_lowerCamelCase )    ,     [] )


     class     __UpperCAmelCase       ( __a		):
       pass






     self.assertEqual(find_labels(_lowerCamelCase )    ,     [] )





