



"""simple docstring"""




import copy
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


_lowerCAmelCase							:   Tuple				=				logging.get_logger(__name__)

_lowerCAmelCase							:   Tuple				=				{
    "microsoft/conditional-detr-resnet-50": (
        "https://huggingface.co/microsoft/conditional-detr-resnet-50/resolve/main/config.json"
    ),
}






class       UpperCAmelCase_    (							_UpperCamelCase		):
      __SCREAMING_SNAKE_CASE		:  List[str]						=					'conditional_detr'
      __SCREAMING_SNAKE_CASE		:  Union[str, Any]						=					['past_key_values']
      __SCREAMING_SNAKE_CASE		:  Dict						=					{
          'hidden_size': 'd_model',
          'num_attention_heads': 'encoder_attention_heads',
      }



      def __init__(	self	:      Any	,      A	:      Optional[Any]=True	,      A	:      Union[str, Any]=None	,      A	:      List[Any]=3	,      A	:      Optional[Any]=3_0_0	,      A	:      Any=6	,      A	:      int=2_0_4_8	,      A	:      Any=8	,      A	:      Tuple=6	,      A	:      List[str]=2_0_4_8	,      A	:      Tuple=8	,      A	:      int=0.0	,      A	:      Any=0.0	,      A	:      Any=True	,      A	:      Optional[Any]="relu"	,      A	:      Optional[int]=2_5_6	,      A	:      Union[str, Any]=0.1	,      A	:      Optional[int]=0.0	,      A	:      Optional[int]=0.0	,      A	:      List[str]=0.02	,      A	:      Union[str, Any]=1.0	,      A	:      Dict=False	,      A	:      Optional[Any]="sine"	,      A	:      int="resnet50"	,      A	:      int=True	,      A	:      Any=False	,      A	:      Tuple=2	,      A	:      List[str]=5	,      A	:      Optional[Any]=2	,      A	:      List[Any]=1	,      A	:      List[Any]=1	,      A	:      Optional[Any]=2	,      A	:      Optional[Any]=5	,      A	:      Union[str, Any]=2	,      A	:      Dict=0.25	,      **A	:      Union[str, Any]	,      ):
           if backbone_config is not None and use_timm_backbone:
                raise ValueError("You can't specify both `backbone_config` and `use_timm_backbone`."		)

           if not use_timm_backbone:
                if backbone_config is None:
                     logger.info("`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."		)
                     _UpperCAmelCase     :      Optional[Any]														=       CONFIG_MAPPING["resnet"](out_features=["stage4"]		)
                elif isinstance(A	,      A		):
                     _UpperCAmelCase     :      int														=       backbone_config.get("model_type"		)
                     _UpperCAmelCase     :      Optional[Any]														=       CONFIG_MAPPING[backbone_model_type]
                     _UpperCAmelCase     :      str														=       config_class.from_dict(A		)

           _UpperCAmelCase     :      Tuple														=       use_timm_backbone
           _UpperCAmelCase     :      int														=       backbone_config
           _UpperCAmelCase     :      str														=       num_channels
           _UpperCAmelCase     :      Dict														=       num_queries
           _UpperCAmelCase     :      Dict														=       d_model
           _UpperCAmelCase     :      str														=       encoder_ffn_dim
           _UpperCAmelCase     :      Union[str, Any]														=       encoder_layers
           _UpperCAmelCase     :      Optional[Any]														=       encoder_attention_heads
           _UpperCAmelCase     :      Tuple														=       decoder_ffn_dim
           _UpperCAmelCase     :      Union[str, Any]														=       decoder_layers
           _UpperCAmelCase     :      Optional[Any]														=       decoder_attention_heads
           _UpperCAmelCase     :      List[Any]														=       dropout
           _UpperCAmelCase     :      Union[str, Any]														=       attention_dropout
           _UpperCAmelCase     :      Union[str, Any]														=       activation_dropout
           _UpperCAmelCase     :      Optional[int]														=       activation_function
           _UpperCAmelCase     :      List[Any]														=       init_std
           _UpperCAmelCase     :      List[str]														=       init_xavier_std
           _UpperCAmelCase     :      str														=       encoder_layerdrop
           _UpperCAmelCase     :      Union[str, Any]														=       decoder_layerdrop
           _UpperCAmelCase     :      Dict														=       encoder_layers
           _UpperCAmelCase     :      Dict														=       auxiliary_loss
           _UpperCAmelCase     :      Optional[int]														=       position_embedding_type
           _UpperCAmelCase     :      Tuple														=       backbone
           _UpperCAmelCase     :      Optional[int]														=       use_pretrained_backbone
           _UpperCAmelCase     :      Any														=       dilation
           # Hungarian matcher
           _UpperCAmelCase     :      str														=       class_cost
           _UpperCAmelCase     :      List[str]														=       bbox_cost
           _UpperCAmelCase     :      Any														=       giou_cost
           # Loss coefficients
           _UpperCAmelCase     :      Tuple														=       mask_loss_coefficient
           _UpperCAmelCase     :      str														=       dice_loss_coefficient
           _UpperCAmelCase     :      int														=       cls_loss_coefficient
           _UpperCAmelCase     :      str														=       bbox_loss_coefficient
           _UpperCAmelCase     :      Union[str, Any]														=       giou_loss_coefficient
           _UpperCAmelCase     :      Tuple														=       focal_alpha
           super().__init__(is_encoder_decoder=A	,      **A		)



      @property
      def 		snake_case_   (	self	:      Optional[int]		):
           return self.encoder_attention_heads



      @property
      def 		snake_case_   (	self	:      int		):
           return self.d_model



      def 		snake_case_   (	self	:      Optional[Any]		):
           _UpperCAmelCase     :      List[Any]														=       copy.deepcopy(self.__dict__		)
           if self.backbone_config is not None:
                _UpperCAmelCase     :      Optional[int]														=       self.backbone_config.to_dict()
           _UpperCAmelCase     :      List[str]														=       self.__class__.model_type
           return output







class       UpperCAmelCase_    (							_UpperCamelCase		):
      __SCREAMING_SNAKE_CASE		:  str						=					version.parse('1.11'		)



      @property
      def 		snake_case_   (	self	:      str		):
           return OrderedDict(
               [
                   ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
                   ("pixel_mask", {0: "batch"}),
               ]		)



      @property
      def 		snake_case_   (	self	:      Dict		):
           return 1e-5



      @property
      def 		snake_case_   (	self	:      List[str]		):
           return 1_2





"""simple docstring"""




import os
import tempfile
import unittest

import numpy as np

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


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

 from diffusers import FlaxDDIMScheduler, FlaxDiffusionPipeline, FlaxStableDiffusionPipeline






@require_flax
class       UpperCAmelCase_    (							unittest.TestCase		):



      def 		snake_case_   (	self	:      Optional[int]		):
           with tempfile.TemporaryDirectory() as tmpdirname:
                # pipeline has Flax weights
                _UpperCAmelCase     :      Union[str, Any]														=       FlaxDiffusionPipeline.from_pretrained(
                    "hf-internal-testing/tiny-stable-diffusion-pipe"	,      safety_checker=A	,      cache_dir=A		)

                _UpperCAmelCase     :      List[str]														=       [t[-1] for t in os.walk(os.path.join(A	,      os.listdir(A		)[0]	,      "snapshots"		)		)]
                _UpperCAmelCase     :      Union[str, Any]														=       [item for sublist in all_root_files for item in sublist]

                # None of the downloaded files should be a PyTorch file even if we have some here:
                # https://huggingface.co/hf-internal-testing/tiny-stable-diffusion-pipe/blob/main/unet/diffusion_pytorch_model.bin
                assert not any(f.endswith(".bin"		) for f in files		)







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



      def 		snake_case_   (	self	:      List[str]		):
           _UpperCAmelCase							,			_UpperCAmelCase     :      int														=       FlaxStableDiffusionPipeline.from_pretrained(
               "hf-internal-testing/tiny-stable-diffusion-pipe"	,      safety_checker=A		)

           _UpperCAmelCase     :      str														=       (
               "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
               " field, close up, split lighting, cinematic"
           )

           _UpperCAmelCase     :      int														=       jax.random.PRNGKey(0		)
           _UpperCAmelCase     :      List[str]														=       4

           _UpperCAmelCase     :      Any														=       jax.device_count()
           _UpperCAmelCase     :      Tuple														=       num_samples * [prompt]
           _UpperCAmelCase     :      Optional[int]														=       pipeline.prepare_inputs(A		)

           # shard inputs and rng
           _UpperCAmelCase     :      Union[str, Any]														=       replicate(A		)
           _UpperCAmelCase     :      List[Any]														=       jax.random.split(A	,      A		)
           _UpperCAmelCase     :      List[Any]														=       shard(A		)

           _UpperCAmelCase     :      Tuple														=       pipeline(A	,      A	,      A	,      A	,      jit=A		).images

           assert images.shape == (num_samples, 1, 6_4, 6_4, 3)
           if jax.device_count() == 8:
                assert np.abs(np.abs(images[0, 0, :2, :2, -2:]	,      dtype=np.floataa		).sum() - 4.1_514_745		) < 1e-3
                assert np.abs(np.abs(A	,      dtype=np.floataa		).sum() - 49_947.875		) < 5e-1

           _UpperCAmelCase     :      Optional[int]														=       pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:]		)		)		)
           assert len(A		) == num_samples



      def 		snake_case_   (	self	:      Union[str, Any]		):
           _UpperCAmelCase							,			_UpperCAmelCase     :      Optional[Any]														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="flax"	,      safety_checker=A		)

           _UpperCAmelCase     :      Dict														=       (
               "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
               " field, close up, split lighting, cinematic"
           )

           _UpperCAmelCase     :      Dict														=       jax.random.PRNGKey(0		)
           _UpperCAmelCase     :      str														=       5_0

           _UpperCAmelCase     :      List[str]														=       jax.device_count()
           _UpperCAmelCase     :      Any														=       num_samples * [prompt]
           _UpperCAmelCase     :      Dict														=       pipeline.prepare_inputs(A		)

           # shard inputs and rng
           _UpperCAmelCase     :      Dict														=       replicate(A		)
           _UpperCAmelCase     :      str														=       jax.random.split(A	,      A		)
           _UpperCAmelCase     :      Optional[Any]														=       shard(A		)

           _UpperCAmelCase     :      List[Any]														=       pipeline(A	,      A	,      A	,      A	,      jit=A		).images

           assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           if jax.device_count() == 8:
                assert np.abs((np.abs(images[0, 0, :2, :2, -2:]	,      dtype=np.floataa		).sum() - 0.05_652_401)		) < 1e-3
                assert np.abs((np.abs(A	,      dtype=np.floataa		).sum() - 2_383_808.2)		) < 5e-1



      def 		snake_case_   (	self	:      List[Any]		):
           _UpperCAmelCase							,			_UpperCAmelCase     :      List[Any]														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="bf16"	,      dtype=jnp.bfloataa	,      safety_checker=A		)

           _UpperCAmelCase     :      Any														=       (
               "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
               " field, close up, split lighting, cinematic"
           )

           _UpperCAmelCase     :      List[Any]														=       jax.random.PRNGKey(0		)
           _UpperCAmelCase     :      List[Any]														=       5_0

           _UpperCAmelCase     :      Union[str, Any]														=       jax.device_count()
           _UpperCAmelCase     :      Optional[int]														=       num_samples * [prompt]
           _UpperCAmelCase     :      Union[str, Any]														=       pipeline.prepare_inputs(A		)

           # shard inputs and rng
           _UpperCAmelCase     :      int														=       replicate(A		)
           _UpperCAmelCase     :      Optional[int]														=       jax.random.split(A	,      A		)
           _UpperCAmelCase     :      str														=       shard(A		)

           _UpperCAmelCase     :      Union[str, Any]														=       pipeline(A	,      A	,      A	,      A	,      jit=A		).images

           assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           if jax.device_count() == 8:
                assert np.abs((np.abs(images[0, 0, :2, :2, -2:]	,      dtype=np.floataa		).sum() - 0.04_003_906)		) < 1e-3
                assert np.abs((np.abs(A	,      dtype=np.floataa		).sum() - 2_373_516.75)		) < 5e-1



      def 		snake_case_   (	self	:      int		):
           _UpperCAmelCase							,			_UpperCAmelCase     :      Dict														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="bf16"	,      dtype=jnp.bfloataa		)

           _UpperCAmelCase     :      str														=       (
               "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
               " field, close up, split lighting, cinematic"
           )

           _UpperCAmelCase     :      Optional[int]														=       jax.random.PRNGKey(0		)
           _UpperCAmelCase     :      Dict														=       5_0

           _UpperCAmelCase     :      Union[str, Any]														=       jax.device_count()
           _UpperCAmelCase     :      str														=       num_samples * [prompt]
           _UpperCAmelCase     :      int														=       pipeline.prepare_inputs(A		)

           # shard inputs and rng
           _UpperCAmelCase     :      Optional[int]														=       replicate(A		)
           _UpperCAmelCase     :      Tuple														=       jax.random.split(A	,      A		)
           _UpperCAmelCase     :      Dict														=       shard(A		)

           _UpperCAmelCase     :      Optional[Any]														=       pipeline(A	,      A	,      A	,      A	,      jit=A		).images

           assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           if jax.device_count() == 8:
                assert np.abs((np.abs(images[0, 0, :2, :2, -2:]	,      dtype=np.floataa		).sum() - 0.04_003_906)		) < 1e-3
                assert np.abs((np.abs(A	,      dtype=np.floataa		).sum() - 2_373_516.75)		) < 5e-1



      def 		snake_case_   (	self	:      Optional[Any]		):
           _UpperCAmelCase     :      Dict														=       FlaxDDIMScheduler(
               beta_start=0.00_085	,      beta_end=0.012	,      beta_schedule="scaled_linear"	,      set_alpha_to_one=A	,      steps_offset=1	,      )

           _UpperCAmelCase							,			_UpperCAmelCase     :      Dict														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="bf16"	,      dtype=jnp.bfloataa	,      scheduler=A	,      safety_checker=A	,      )
           _UpperCAmelCase     :      Union[str, Any]														=       scheduler.create_state()

           _UpperCAmelCase     :      str														=       scheduler_state

           _UpperCAmelCase     :      Union[str, Any]														=       (
               "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
               " field, close up, split lighting, cinematic"
           )

           _UpperCAmelCase     :      str														=       jax.random.PRNGKey(0		)
           _UpperCAmelCase     :      List[str]														=       5_0

           _UpperCAmelCase     :      Optional[int]														=       jax.device_count()
           _UpperCAmelCase     :      int														=       num_samples * [prompt]
           _UpperCAmelCase     :      Optional[int]														=       pipeline.prepare_inputs(A		)

           # shard inputs and rng
           _UpperCAmelCase     :      List[Any]														=       replicate(A		)
           _UpperCAmelCase     :      Tuple														=       jax.random.split(A	,      A		)
           _UpperCAmelCase     :      Union[str, Any]														=       shard(A		)

           _UpperCAmelCase     :      Dict														=       pipeline(A	,      A	,      A	,      A	,      jit=A		).images

           assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           if jax.device_count() == 8:
                assert np.abs((np.abs(images[0, 0, :2, :2, -2:]	,      dtype=np.floataa		).sum() - 0.045_043_945)		) < 1e-3
                assert np.abs((np.abs(A	,      dtype=np.floataa		).sum() - 2_347_693.5)		) < 5e-1



      def 		snake_case_   (	self	:      int		):
           _UpperCAmelCase     :      Optional[Any]														=       (
               "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
               " field, close up, split lighting, cinematic"
           )

           _UpperCAmelCase     :      Optional[Any]														=       jax.device_count()
           _UpperCAmelCase     :      Union[str, Any]														=       num_samples * [prompt]
           _UpperCAmelCase     :      Optional[Any]														=       jax.random.split(jax.random.PRNGKey(0		)	,      A		)

           _UpperCAmelCase							,			_UpperCAmelCase     :      str														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="bf16"	,      dtype=jnp.bfloataa	,      safety_checker=A	,      )

           _UpperCAmelCase     :      int														=       replicate(A		)
           _UpperCAmelCase     :      Union[str, Any]														=       pipeline.prepare_inputs(A		)
           _UpperCAmelCase     :      int														=       shard(A		)
           _UpperCAmelCase     :      Dict														=       pipeline(A	,      A	,      A	,      jit=A		).images
           assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           _UpperCAmelCase     :      List[str]														=       images[2, 0, 2_5_6, 1_0:1_7, 1]

           # With memory efficient attention
           _UpperCAmelCase							,			_UpperCAmelCase     :      str														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="bf16"	,      dtype=jnp.bfloataa	,      safety_checker=A	,      use_memory_efficient_attention=A	,      )

           _UpperCAmelCase     :      Any														=       replicate(A		)
           _UpperCAmelCase     :      str														=       pipeline.prepare_inputs(A		)
           _UpperCAmelCase     :      Tuple														=       shard(A		)
           _UpperCAmelCase     :      Tuple														=       pipeline(A	,      A	,      A	,      jit=A		).images
           assert images_eff.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           _UpperCAmelCase     :      str														=       images[2, 0, 2_5_6, 1_0:1_7, 1]

           # I checked the results visually and they are very similar. However, I saw that the max diff is `1` and the `sum`
           # over the 8 images is exactly `256`, which is very suspicious. Testing a random slice for now.
           assert abs(slice_eff - slice		).max() < 1e-2
