






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


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

SCREAMING_SNAKE_CASE__      :			Tuple	=		{
    '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 						lowerCamelCase_				(      lowerCamelCase ):
				a__					       =  '''perceiver'''







				def __init__(						self       ,					__lowerCAmelCase=2_5_6       ,					__lowerCAmelCase=1_2_8_0       ,					__lowerCAmelCase=7_6_8       ,					__lowerCAmelCase=1       ,					__lowerCAmelCase=2_6       ,					__lowerCAmelCase=8       ,					__lowerCAmelCase=8       ,					__lowerCAmelCase=None       ,					__lowerCAmelCase=None       ,					__lowerCAmelCase="kv"       ,					__lowerCAmelCase=1       ,					__lowerCAmelCase=1       ,					__lowerCAmelCase="gelu"       ,					__lowerCAmelCase=0.1       ,					__lowerCAmelCase=0.02       ,					__lowerCAmelCase=1E-12       ,					__lowerCAmelCase=True       ,					__lowerCAmelCase=2_6_2       ,					__lowerCAmelCase=2_0_4_8       ,					__lowerCAmelCase=5_6       ,					__lowerCAmelCase=[3_6_8, 4_9_6]       ,					__lowerCAmelCase=1_6       ,					__lowerCAmelCase=1_9_2_0       ,					__lowerCAmelCase=1_6       ,					__lowerCAmelCase=[1, 1_6, 2_2_4, 2_2_4]       ,					**__lowerCAmelCase       ,					):
								"""simple docstring"""
								super().__init__(**__A     )

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







class 						lowerCamelCase_				(      lowerCamelCase ):







				@property
				def 			A	(						self     ):
								"""simple docstring"""
								if self.task == "multiple-choice":
												__magic_name__		:str        =      {0: "batch", 1: "choice", 2: "sequence"}
								else:
												__magic_name__		:List[Any]        =      {0: "batch", 1: "sequence"}
								return OrderedDict(
								    [
								        ('''inputs''', dynamic_axis),
								        ('''attention_mask''', dynamic_axis),
								    ]     )







				@property
				def 			A	(						self     ):
								"""simple docstring"""
								return 1E-4







				def 			A	(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase = -1       ,					__lowerCAmelCase = -1       ,					__lowerCAmelCase = -1       ,					__lowerCAmelCase = False       ,					__lowerCAmelCase = None       ,					__lowerCAmelCase = 3       ,					__lowerCAmelCase = 4_0       ,					__lowerCAmelCase = 4_0       ,					):
								"""simple docstring"""
								# copied from `transformers.onnx.config.OnnxConfig` and slightly altered/simplified

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














import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel

from diffusers import DDIMScheduler, LDMPipeline, UNetaDModel, VQModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()
class 						lowerCamelCase_				(      unittest.TestCase ):







				@property
				def 			A	(						self     ):
								"""simple docstring"""
								torch.manual_seed(0     )
								__magic_name__		:Dict        =      UNetaDModel(
								    block_out_channels=(3_2, 6_4)       ,					layers_per_block=2       ,					sample_size=3_2       ,					in_channels=3       ,					out_channels=3       ,					down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')       ,					up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')       ,					)
								return model







				@property
				def 			A	(						self     ):
								"""simple docstring"""
								torch.manual_seed(0     )
								__magic_name__		:List[str]        =      VQModel(
								    block_out_channels=[3_2, 6_4]       ,					in_channels=3       ,					out_channels=3       ,					down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']       ,					up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']       ,					latent_channels=3       ,					)
								return model







				@property
				def 			A	(						self     ):
								"""simple docstring"""
								torch.manual_seed(0     )
								__magic_name__		:int        =      CLIPTextConfig(
								    bos_token_id=0       ,					eos_token_id=2       ,					hidden_size=3_2       ,					intermediate_size=3_7       ,					layer_norm_eps=1E-05       ,					num_attention_heads=4       ,					num_hidden_layers=5       ,					pad_token_id=1       ,					vocab_size=1_0_0_0       ,					)
								return CLIPTextModel(__lowerCAmelCase     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:Optional[Any]        =      self.dummy_uncond_unet
								__magic_name__		:Optional[int]        =      DDIMScheduler()
								__magic_name__		:List[str]        =      self.dummy_vq_model

								__magic_name__		:Tuple        =      LDMPipeline(unet=__lowerCAmelCase       ,					vqvae=__lowerCAmelCase       ,					scheduler=__lowerCAmelCase     )
								ldm.to(__lowerCAmelCase     )
								ldm.set_progress_bar_config(disable=__lowerCAmelCase     )

								__magic_name__		:List[Any]        =      torch.manual_seed(0     )
								__magic_name__		:List[str]        =      ldm(generator=__lowerCAmelCase       ,					num_inference_steps=2       ,					output_type='''numpy'''     ).images

								__magic_name__		:List[Any]        =      torch.manual_seed(0     )
								__magic_name__		:Any        =      ldm(generator=__lowerCAmelCase       ,					num_inference_steps=2       ,					output_type='''numpy'''       ,					return_dict=__lowerCAmelCase     )[0]

								__magic_name__		:Any        =      image[0, -3:, -3:, -1]
								__magic_name__		:Dict        =      image_from_tuple[0, -3:, -3:, -1]

								assert image.shape == (1, 6_4, 6_4, 3)
								__magic_name__		:Union[str, Any]        =      np.array([0.8512, 0.818, 0.6411, 0.6808, 0.4465, 0.5618, 0.46, 0.6231, 0.5172]     )
								__magic_name__		:Any        =      1E-2 if torch_device != '''mps''' else 3E-2

								assert np.abs(image_slice.flatten() - expected_slice     ).max() < tolerance
								assert np.abs(image_from_tuple_slice.flatten() - expected_slice     ).max() < tolerance







@slow
@require_torch
class 						lowerCamelCase_				(      unittest.TestCase ):







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:Tuple        =      LDMPipeline.from_pretrained('''CompVis/ldm-celebahq-256'''     )
								ldm.to(__lowerCAmelCase     )
								ldm.set_progress_bar_config(disable=__lowerCAmelCase     )

								__magic_name__		:Optional[Any]        =      torch.manual_seed(0     )
								__magic_name__		:Optional[int]        =      ldm(generator=__lowerCAmelCase       ,					num_inference_steps=5       ,					output_type='''numpy'''     ).images

								__magic_name__		:Optional[Any]        =      image[0, -3:, -3:, -1]

								assert image.shape == (1, 2_5_6, 2_5_6, 3)
								__magic_name__		:List[str]        =      np.array([0.4399, 0.44975, 0.46825, 0.474, 0.4359, 0.4581, 0.45095, 0.4341, 0.4447]     )
								__magic_name__		:Tuple        =      1E-2 if torch_device != '''mps''' else 3E-2

								assert np.abs(image_slice.flatten() - expected_slice     ).max() < tolerance






