




'''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 							UpperCAmelCase_     (   self				)   ->		int:
      # clean up the VRAM after each test
      super().tearDown()
      gc.collect()
      torch.cuda.empty_cache()




  def 							UpperCAmelCase_     (   self				)   ->		Union[str, Any]:
      lowerCAmelCase__			:  Dict											= VersatileDiffusionTextToImagePipeline.from_pretrained("""shi-labs/versatile-diffusion"""				)
      # remove text_unet
      pipe.remove_unused_weights()
      pipe.to(__UpperCAmelCase				)
      pipe.set_progress_bar_config(disable=__UpperCAmelCase				)

      lowerCAmelCase__			:  Optional[int]											= """A painting of a squirrel eating a burger """
      lowerCAmelCase__			:  Optional[Any]											= torch.manual_seed(0				)
      lowerCAmelCase__			:  List[str]											= 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				)
          lowerCAmelCase__			:  Dict											= VersatileDiffusionTextToImagePipeline.from_pretrained(__UpperCAmelCase				)
      pipe.to(__UpperCAmelCase				)
      pipe.set_progress_bar_config(disable=__UpperCAmelCase				)

      lowerCAmelCase__			:  Tuple											= generator.manual_seed(0				)
      lowerCAmelCase__			:  str											= 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 							UpperCAmelCase_     (   self				)   ->		Dict:
      lowerCAmelCase__			:  Optional[int]											= VersatileDiffusionTextToImagePipeline.from_pretrained(
          """shi-labs/versatile-diffusion"""  ,torch_dtype=torch.floataa				)
      pipe.to(__UpperCAmelCase				)
      pipe.set_progress_bar_config(disable=__UpperCAmelCase				)

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

      lowerCAmelCase__			:  Dict											= image[0, 253:256, 253:256, -1]

      assert image.shape == (1, 512, 512, 3)
      lowerCAmelCase__			:  Optional[Any]											= np.array([0.3_3_6_7, 0.3_1_6_9, 0.2_6_5_6, 0.3_8_7_0, 0.4_7_9_0, 0.3_7_9_6, 0.4_0_0_9, 0.4_8_7_8, 0.4_7_7_8]				)

      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										=     {
    '''facebook/convnextv2-tiny-1k-224''': '''https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json''',
}






class 	lowerCAmelCase_(    SCREAMING_SNAKE_CASE_						,			SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Tuple           =				'''convnextv2'''




  def __init__(   self  ,__UpperCAmelCase=3  ,__UpperCAmelCase=4  ,__UpperCAmelCase=4  ,__UpperCAmelCase=None  ,__UpperCAmelCase=None  ,__UpperCAmelCase="gelu"  ,__UpperCAmelCase=0.0_2  ,__UpperCAmelCase=1E-12  ,__UpperCAmelCase=0.0  ,__UpperCAmelCase=224  ,__UpperCAmelCase=None  ,__UpperCAmelCase=None  ,**__UpperCAmelCase  ,)   ->		Union[str, Any]:
      super().__init__(**__UpperCAmelCase				)

      lowerCAmelCase__			:  int											= num_channels
      lowerCAmelCase__			:  List[Any]											= patch_size
      lowerCAmelCase__			:  Union[str, Any]											= num_stages
      lowerCAmelCase__			:  Tuple											= [96, 192, 384, 768] if hidden_sizes is None else hidden_sizes
      lowerCAmelCase__			:  str											= [3, 3, 9, 3] if depths is None else depths
      lowerCAmelCase__			:  Optional[Any]											= hidden_act
      lowerCAmelCase__			:  str											= initializer_range
      lowerCAmelCase__			:  List[str]											= layer_norm_eps
      lowerCAmelCase__			:  Dict											= drop_path_rate
      lowerCAmelCase__			:  int											= image_size
      lowerCAmelCase__			:  int											= ["""stem"""] + [F"""stage{idx}""" for idx in range(1  ,len(self.depths				) + 1				)]
      lowerCAmelCase__				,       lowerCAmelCase__			:  Tuple											= get_aligned_output_features_output_indices(
          out_features=__UpperCAmelCase  ,out_indices=__UpperCAmelCase  ,stage_names=self.stage_names				)


