






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 		_lowercase		(       unittest.TestCase	):

   def 		A							(      self			:       Union[str, Any]							)       ->				Optional[int]:


    """simple docstring"""






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

   def 		A							(      self			:       Tuple							)       ->				Optional[Any]:


    """simple docstring"""






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

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

    a						=		replicate(__lowerCAmelCase							)
    a						=		shard(__lowerCAmelCase							)

    a						=		jax.random.PRNGKey(0							)
    a						=		jax.random.split(__lowerCAmelCase							,   jax.device_count()							)

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

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

    a						=		jnp.asarray(jax.device_get(image_slice.flatten()							)							)
    a						=		jnp.array([0.4_2_3_8, 0.4_4_1_4, 0.4_3_9_5, 0.4_4_5_3, 0.4_6_2_9, 0.4_5_9_0, 0.4_5_3_1, 0.4_5_5_0_8, 0.4_5_1_2]							)
    print(f"""output_slice: {output_slice}"""							)
    assert jnp.abs(output_slice - expected_slice							).max() < 1E-2




   def 		A							(      self			:       List[Any]							)       ->				Dict:


    """simple docstring"""






    a						=		"stabilityai/stable-diffusion-2"
    a		,  a						=		FlaxDPMSolverMultistepScheduler.from_pretrained(__lowerCAmelCase							,   subfolder="scheduler"							)
    a		,  a						=		FlaxStableDiffusionPipeline.from_pretrained(
        __lowerCAmelCase							,   scheduler=__lowerCAmelCase							,   revision="bf16"							,   dtype=jnp.bfloataa							,   )
    a						=		scheduler_params

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

    a						=		replicate(__lowerCAmelCase							)
    a						=		shard(__lowerCAmelCase							)

    a						=		jax.random.PRNGKey(0							)
    a						=		jax.random.split(__lowerCAmelCase							,   jax.device_count()							)

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

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

    a						=		jnp.asarray(jax.device_get(image_slice.flatten()							)							)
    a						=		jnp.array([0.4_3_3_6, 0.4_2_9_6_9, 0.4_4_5_3, 0.4_1_9_9, 0.4_2_9_7, 0.4_5_3_1, 0.4_4_3_4, 0.4_4_3_4, 0.4_2_9_7]							)
    print(f"""output_slice: {output_slice}"""							)
    assert jnp.abs(output_slice - expected_slice							).max() < 1E-2






from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


A_		:     int        =     logging.get_logger(__name__)

A_		:     str        =     {
    '''microsoft/focalnet-tiny''': '''https://huggingface.co/microsoft/focalnet-tiny/resolve/main/config.json''',
}



class 		_lowercase		(       UpperCAmelCase__,   UpperCAmelCase__	):
   _UpperCAmelCase     	=			'''focalnet'''

   def __init__(      self			:       int							,   __lowerCAmelCase			:       Optional[Any]=224							,   __lowerCAmelCase			:       Any=4							,   __lowerCAmelCase			:       Any=3							,   __lowerCAmelCase			:       Tuple=96							,   __lowerCAmelCase			:       Dict=False							,   __lowerCAmelCase			:       Optional[int]=[192, 384, 768, 768]							,   __lowerCAmelCase			:       Union[str, Any]=[2, 2, 6, 2]							,   __lowerCAmelCase			:       Optional[int]=[2, 2, 2, 2]							,   __lowerCAmelCase			:       Union[str, Any]=[3, 3, 3, 3]							,   __lowerCAmelCase			:       str="gelu"							,   __lowerCAmelCase			:       Any=4.0							,   __lowerCAmelCase			:       Optional[int]=0.0							,   __lowerCAmelCase			:       Tuple=0.1							,   __lowerCAmelCase			:       str=False							,   __lowerCAmelCase			:       Optional[int]=1E-4							,   __lowerCAmelCase			:       Optional[Any]=False							,   __lowerCAmelCase			:       Union[str, Any]=False							,   __lowerCAmelCase			:       str=False							,   __lowerCAmelCase			:       Any=0.0_2							,   __lowerCAmelCase			:       str=1E-5							,   __lowerCAmelCase			:       Optional[Any]=32							,   __lowerCAmelCase			:       Dict=None							,   __lowerCAmelCase			:       str=None							,   **__lowerCAmelCase			:       Any							,   )       ->				List[str]:


    """simple docstring"""






    super().__init__(**__lowerCAmelCase							)

    a						=		image_size
    a						=		patch_size
    a						=		num_channels
    a						=		embed_dim
    a						=		use_conv_embed
    a						=		hidden_sizes
    a						=		depths
    a						=		focal_levels
    a						=		focal_windows
    a						=		hidden_act
    a						=		mlp_ratio
    a						=		hidden_dropout_prob
    a						=		drop_path_rate
    a						=		use_layerscale
    a						=		layerscale_value
    a						=		use_post_layernorm
    a						=		use_post_layernorm_in_modulation
    a						=		normalize_modulator
    a						=		initializer_range
    a						=		layer_norm_eps
    a						=		encoder_stride
    a						=		["stem"] + [f"""stage{idx}""" for idx in range(1							,   len(self.depths							) + 1							)]
    a		,  a						=		get_aligned_output_features_output_indices(
        out_features=__lowerCAmelCase							,   out_indices=__lowerCAmelCase							,   stage_names=self.stage_names							)

