



import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionTextToImagePipeline
from diffusers.utils.testing_utils import nightly, require_torch_gpu, torch_device


_snake_case	=						False



class 						UpperCAmelCase_					( unittest.TestCase):
							pass



@nightly
@require_torch_gpu
class 						UpperCAmelCase_					( unittest.TestCase):







							def      snake_case__   (     self):

														'''simple docstring'''

														super().tearDown()
														gc.collect()
														torch.cuda.empty_cache()







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							Dict				     =					VersatileDiffusionTextToImagePipeline.from_pretrained("shi-labs/versatile-diffusion")
														# remove text_unet
														pipe.remove_unused_weights()
														pipe.to(__a)
														pipe.set_progress_bar_config(disable=__a)

														_lowerCAmelCase  :							str				     =					"A painting of a squirrel eating a burger "
														_lowerCAmelCase  :							Optional[Any]				     =					torch.manual_seed(0)
														_lowerCAmelCase  :							Any				     =					pipe(
														    prompt=__a,    generator=__a,    guidance_scale=7.5,    num_inference_steps=2,    output_type="numpy").images

														with tempfile.TemporaryDirectory() as tmpdirname:
																					pipe.save_pretrained(__a)
																					_lowerCAmelCase  :							List[str]				     =					VersatileDiffusionTextToImagePipeline.from_pretrained(__a)
														pipe.to(__a)
														pipe.set_progress_bar_config(disable=__a)

														_lowerCAmelCase  :							List[str]				     =					generator.manual_seed(0)
														_lowerCAmelCase  :							Any				     =					pipe(
														    prompt=__a,    generator=__a,    guidance_scale=7.5,    num_inference_steps=2,    output_type="numpy").images

														assert np.abs(image - new_image).sum() < 1E-5, "Models don't have the same forward pass"







							def      snake_case__   (     self):

														'''simple docstring'''

														_lowerCAmelCase  :							Tuple				     =					VersatileDiffusionTextToImagePipeline.from_pretrained(
														    "shi-labs/versatile-diffusion",    torch_dtype=torch.floataa)
														pipe.to(__a)
														pipe.set_progress_bar_config(disable=__a)

														_lowerCAmelCase  :							Optional[Any]				     =					"A painting of a squirrel eating a burger "
														_lowerCAmelCase  :							List[Any]				     =					torch.manual_seed(0)
														_lowerCAmelCase  :							Union[str, Any]				     =					pipe(
														    prompt=__a,    generator=__a,    guidance_scale=7.5,    num_inference_steps=50,    output_type="numpy").images

														_lowerCAmelCase  :							Optional[int]				     =					image[0, 253:256, 253:256, -1]

														assert image.shape == (1, 512, 512, 3)
														_lowerCAmelCase  :							Optional[Any]				     =					np.array([0.3_367, 0.3_169, 0.2_656, 0.3_870, 0.4_790, 0.3_796, 0.4_009, 0.4_878, 0.4_778])

														assert np.abs(image_slice.flatten() - 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


_snake_case	=						logging.get_logger(__name__)

_snake_case	=						{
    "microsoft/focalnet-tiny": "https://huggingface.co/microsoft/focalnet-tiny/resolve/main/config.json",
}



class 						UpperCAmelCase_					( a     ,      a):
							lowerCamelCase__         =	'focalnet'







							def __init__(     self,    __a=224,    __a=4,    __a=3,    __a=96,    __a=False,    __a=[192, 384, 768, 768],    __a=[2, 2, 6, 2],    __a=[2, 2, 2, 2],    __a=[3, 3, 3, 3],    __a="gelu",    __a=4.0,    __a=0.0,    __a=0.1,    __a=False,    __a=1E-4,    __a=False,    __a=False,    __a=False,    __a=0.02,    __a=1E-5,    __a=32,    __a=None,    __a=None,    **__a,    ):

														'''simple docstring'''

														super().__init__(**__a)

														_lowerCAmelCase  :							str				     =					image_size
														_lowerCAmelCase  :							List[str]				     =					patch_size
														_lowerCAmelCase  :							List[Any]				     =					num_channels
														_lowerCAmelCase  :							Tuple				     =					embed_dim
														_lowerCAmelCase  :							List[Any]				     =					use_conv_embed
														_lowerCAmelCase  :							Any				     =					hidden_sizes
														_lowerCAmelCase  :							Tuple				     =					depths
														_lowerCAmelCase  :							Dict				     =					focal_levels
														_lowerCAmelCase  :							Optional[Any]				     =					focal_windows
														_lowerCAmelCase  :							str				     =					hidden_act
														_lowerCAmelCase  :							Union[str, Any]				     =					mlp_ratio
														_lowerCAmelCase  :							Any				     =					hidden_dropout_prob
														_lowerCAmelCase  :							Dict				     =					drop_path_rate
														_lowerCAmelCase  :							str				     =					use_layerscale
														_lowerCAmelCase  :							str				     =					layerscale_value
														_lowerCAmelCase  :							Union[str, Any]				     =					use_post_layernorm
														_lowerCAmelCase  :							Optional[int]				     =					use_post_layernorm_in_modulation
														_lowerCAmelCase  :							str				     =					normalize_modulator
														_lowerCAmelCase  :							Any				     =					initializer_range
														_lowerCAmelCase  :							Union[str, Any]				     =					layer_norm_eps
														_lowerCAmelCase  :							Any				     =					encoder_stride
														_lowerCAmelCase  :							List[str]				     =					["stem"] + [f"stage{idx}" for idx in range(1,    len(self.depths) + 1)]
														_lowerCAmelCase     ,					_lowerCAmelCase  :							List[str]				     =					get_aligned_output_features_output_indices(
														    out_features=__a,    out_indices=__a,    stage_names=self.stage_names)
