
'''simple docstring'''
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


__lowerCAmelCase												=	False


class       _lowerCAmelCase   (   unittest.TestCase				):







							'''simple docstring'''

							pass


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







							'''simple docstring'''

							def 	lowercase							(self  )					->       Dict:
														# clean up the VRAM after each test
														super().tearDown()
														gc.collect()
														torch.cuda.empty_cache()

							def 	lowercase							(self  )					->       Optional[Any]:
														_snake_case       =  VersatileDiffusionTextToImagePipeline.from_pretrained("""shi-labs/versatile-diffusion"""  )
														# remove text_unet
														pipe.remove_unused_weights()
														pipe.to(UpperCAmelCase  )
														pipe.set_progress_bar_config(disable=UpperCAmelCase  )

														_snake_case       =  """A painting of a squirrel eating a burger """
														_snake_case       =  torch.manual_seed(0  )
														_snake_case       =  pipe(
														    prompt=UpperCAmelCase  ,  generator=UpperCAmelCase  ,  guidance_scale=7.5  ,  num_inference_steps=2  ,  output_type="""numpy"""  ).images

														with tempfile.TemporaryDirectory() as tmpdirname:
																					pipe.save_pretrained(UpperCAmelCase  )
																					_snake_case       =  VersatileDiffusionTextToImagePipeline.from_pretrained(UpperCAmelCase  )
														pipe.to(UpperCAmelCase  )
														pipe.set_progress_bar_config(disable=UpperCAmelCase  )

														_snake_case       =  generator.manual_seed(0  )
														_snake_case       =  pipe(
														    prompt=UpperCAmelCase  ,  generator=UpperCAmelCase  ,  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 	lowercase							(self  )					->       List[Any]:
														_snake_case       =  VersatileDiffusionTextToImagePipeline.from_pretrained(
														    """shi-labs/versatile-diffusion"""  ,  torch_dtype=torch.floataa  )
														pipe.to(UpperCAmelCase  )
														pipe.set_progress_bar_config(disable=UpperCAmelCase  )

														_snake_case       =  """A painting of a squirrel eating a burger """
														_snake_case       =  torch.manual_seed(0  )
														_snake_case       =  pipe(
														    prompt=UpperCAmelCase  ,  generator=UpperCAmelCase  ,  guidance_scale=7.5  ,  num_inference_steps=50  ,  output_type="""numpy"""  ).images

														_snake_case       =  image[0, 253:256, 253:256, -1]

														assert image.shape == (1, 512, 512, 3)
														_snake_case       =  np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778]  )

														assert np.abs(image_slice.flatten() - expected_slice  ).max() < 1e-2

'''simple docstring'''
from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


__lowerCAmelCase												=	logging.get_logger(__name__)

__lowerCAmelCase												=	{
    'shi-labs/nat-mini-in1k-224': 'https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json',
    # See all Nat models at https://huggingface.co/models?filter=nat
}


class       _lowerCAmelCase   (   __snake_case					,							__snake_case				):







							'''simple docstring'''

							lowerCAmelCase_ 						=						"nat"

							lowerCAmelCase_ 						=						{
							    "num_attention_heads": "num_heads",
							    "num_hidden_layers": "num_layers",
							}

							def __init__(self  ,  UpperCAmelCase=4  ,  UpperCAmelCase=3  ,  UpperCAmelCase=64  ,  UpperCAmelCase=[3, 4, 6, 5]  ,  UpperCAmelCase=[2, 4, 8, 16]  ,  UpperCAmelCase=7  ,  UpperCAmelCase=3.0  ,  UpperCAmelCase=True  ,  UpperCAmelCase=0.0  ,  UpperCAmelCase=0.0  ,  UpperCAmelCase=0.1  ,  UpperCAmelCase="gelu"  ,  UpperCAmelCase=0.02  ,  UpperCAmelCase=1e-5  ,  UpperCAmelCase=0.0  ,  UpperCAmelCase=None  ,  UpperCAmelCase=None  ,  **UpperCAmelCase  ,  )					->       str:
														super().__init__(**UpperCAmelCase  )

														_snake_case       =  patch_size
														_snake_case       =  num_channels
														_snake_case       =  embed_dim
														_snake_case       =  depths
														_snake_case       =  len(UpperCAmelCase  )
														_snake_case       =  num_heads
														_snake_case       =  kernel_size
														_snake_case       =  mlp_ratio
														_snake_case       =  qkv_bias
														_snake_case       =  hidden_dropout_prob
														_snake_case       =  attention_probs_dropout_prob
														_snake_case       =  drop_path_rate
														_snake_case       =  hidden_act
														_snake_case       =  layer_norm_eps
														_snake_case       =  initializer_range
														# we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel
														# this indicates the channel dimension after the last stage of the model
														_snake_case       =  int(embed_dim * 2 ** (len(UpperCAmelCase  ) - 1)  )
														_snake_case       =  layer_scale_init_value
														_snake_case       =  ["""stem"""] + [f"""stage{idx}""" for idx in range(1  ,  len(UpperCAmelCase  ) + 1  )]
														_snake_case,							_snake_case       =  get_aligned_output_features_output_indices(
														    out_features=UpperCAmelCase  ,  out_indices=UpperCAmelCase  ,  stage_names=self.stage_names  )