





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


lowercase    : List[Any]    	=  logging.get_logger(__name__)

lowercase    : 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	  =       "perceiver"

     def __init__(      self,       A=256,       A=1280,       A=768,       A=1,       A=26,       A=8,       A=8,       A=None,       A=None,       A="kv",       A=1,       A=1,       A="gelu",       A=0.1,       A=0.02,       A=1E-1_2,       A=True,       A=262,       A=2048,       A=56,       A=[368, 496],       A=16,       A=1920,       A=16,       A=[1, 16, 224, 224],       **A,       ):





            """simple docstring"""

            super().__init__(**A      )

            _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      ):





            """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      ):





            """simple docstring"""

            return 1E-4



     def 	_UpperCamelCase     (      self,       A,       A = -1,       A = -1,       A = -1,       A = False,       A = None,       A = 3,       A = 40,       A = 40,       ):





            """simple docstring"""

            if isinstance(A,       A      ):
                   # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
                   _UpperCamelCase              =						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
                   _UpperCamelCase              =						preprocessor.num_special_tokens_to_add(A      )
                   _UpperCamelCase              =						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
                   _UpperCamelCase              =						[''' '''.join(['''a''']      ) * seq_length] * batch_size
                   _UpperCamelCase              =						dict(preprocessor(A,       return_tensors=A      )      )
                   _UpperCamelCase              =						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
                   _UpperCamelCase              =						compute_effective_axis_dimension(A,       fixed_dimension=OnnxConfig.default_fixed_batch      )
                   _UpperCamelCase              =						self._generate_dummy_images(A,       A,       A,       A      )
                   _UpperCamelCase              =						dict(preprocessor(images=A,       return_tensors=A      )      )
                   _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.'''      )













import gc
import unittest

from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline
from diffusers.utils import is_flax_available, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
       import jax
       import jax.numpy as jnp
       from flax.jax_utils import replicate
       from flax.training.common_utils import shard

@slow
@require_flax
class  __A(						unittest.TestCase ):

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            super().tearDown()
            gc.collect()

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase       ,		_UpperCamelCase              =						FlaxStableDiffusionPipeline.from_pretrained(
                '''stabilityai/stable-diffusion-2''',       revision='''bf16''',       dtype=jnp.bfloataa,       )

            _UpperCamelCase              =						'''A painting of a squirrel eating a burger'''
            _UpperCamelCase              =						jax.device_count()
            _UpperCamelCase              =						num_samples * [prompt]
            _UpperCamelCase              =						sd_pipe.prepare_inputs(A      )

            _UpperCamelCase              =						replicate(A      )
            _UpperCamelCase              =						shard(A      )

            _UpperCamelCase              =						jax.random.PRNGKey(0      )
            _UpperCamelCase              =						jax.random.split(A,       jax.device_count()      )

            _UpperCamelCase              =						sd_pipe(A,       A,       A,       num_inference_steps=25,       jit=A      )[0]
            assert images.shape == (jax.device_count(), 1, 768, 768, 3)

            _UpperCamelCase              =						images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]      )
            _UpperCamelCase              =						images[0, 253:256, 253:256, -1]

            _UpperCamelCase              =						jnp.asarray(jax.device_get(image_slice.flatten()      )      )
            _UpperCamelCase              =						jnp.array([0.4_238, 0.4_414, 0.4_395, 0.4_453, 0.4_629, 0.4_590, 0.4_531, 0.45_508, 0.4_512]      )
            print(F'''output_slice: {output_slice}'''      )
            assert jnp.abs(output_slice - expected_slice      ).max() < 1E-2



     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						'''stabilityai/stable-diffusion-2'''
            _UpperCamelCase       ,		_UpperCamelCase              =						FlaxDPMSolverMultistepScheduler.from_pretrained(A,       subfolder='''scheduler'''      )
            _UpperCamelCase       ,		_UpperCamelCase              =						FlaxStableDiffusionPipeline.from_pretrained(
                A,       scheduler=A,       revision='''bf16''',       dtype=jnp.bfloataa,       )
            _UpperCamelCase              =						scheduler_params

            _UpperCamelCase              =						'''A painting of a squirrel eating a burger'''
            _UpperCamelCase              =						jax.device_count()
            _UpperCamelCase              =						num_samples * [prompt]
            _UpperCamelCase              =						sd_pipe.prepare_inputs(A      )

            _UpperCamelCase              =						replicate(A      )
            _UpperCamelCase              =						shard(A      )

            _UpperCamelCase              =						jax.random.PRNGKey(0      )
            _UpperCamelCase              =						jax.random.split(A,       jax.device_count()      )

            _UpperCamelCase              =						sd_pipe(A,       A,       A,       num_inference_steps=25,       jit=A      )[0]
            assert images.shape == (jax.device_count(), 1, 768, 768, 3)

            _UpperCamelCase              =						images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]      )
            _UpperCamelCase              =						images[0, 253:256, 253:256, -1]

            _UpperCamelCase              =						jnp.asarray(jax.device_get(image_slice.flatten()      )      )
            _UpperCamelCase              =						jnp.array([0.4_336, 0.42_969, 0.4_453, 0.4_199, 0.4_297, 0.4_531, 0.4_434, 0.4_434, 0.4_297]      )
            print(F'''output_slice: {output_slice}'''      )
            assert jnp.abs(output_slice - expected_slice      ).max() < 1E-2






