






"""simple docstring"""


import unittest

import numpy as np

from diffusers import LMSDiscreteScheduler, OnnxStableDiffusionInpaintPipeline
from diffusers.utils.testing_utils import (
    is_onnx_available,
    load_image,
    nightly,
    require_onnxruntime,
    require_torch_gpu,
)

from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin


if is_onnx_available():
    import onnxruntime as ort


class        a				(		lowercase__	,							unittest.TestCase   ):
    pass



@nightly
@require_onnxruntime
@require_torch_gpu
class        a				(		unittest.TestCase   ):


    @property
    def      lowerCAmelCase_  (					self	:  Dict					):
      return (
          "CUDAExecutionProvider",
          {
              "gpu_mem_limit": "15000000000",  # 15GB
              "arena_extend_strategy": "kSameAsRequested",
          },
      )


    @property
    def      lowerCAmelCase_  (					self	:  Dict					):
      _UpperCAmelCase						 =      ort.SessionOptions()
      _UpperCAmelCase						 =      False
      return options


    def      lowerCAmelCase_  (					self	:  str					):
      _UpperCAmelCase						 =      load_image(
          """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
          """/in_paint/overture-creations-5sI6fQgYIuo.png"""					)
      _UpperCAmelCase						 =      load_image(
          """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
          """/in_paint/overture-creations-5sI6fQgYIuo_mask.png"""					)
      _UpperCAmelCase						 =      OnnxStableDiffusionInpaintPipeline.from_pretrained(
          """runwayml/stable-diffusion-inpainting"""   ,				revision="""onnx"""   ,				safety_checker=__lowerCAmelCase   ,				feature_extractor=__lowerCAmelCase   ,				provider=self.gpu_provider   ,				sess_options=self.gpu_options   ,				)
      pipe.set_progress_bar_config(disable=__lowerCAmelCase					)

      _UpperCAmelCase						 =      "A red cat sitting on a park bench"

      _UpperCAmelCase						 =      np.random.RandomState(0					)
      _UpperCAmelCase						 =      pipe(
          prompt=__lowerCAmelCase   ,				image=__lowerCAmelCase   ,				mask_image=__lowerCAmelCase   ,				guidance_scale=7.5   ,				num_inference_steps=10   ,				generator=__lowerCAmelCase   ,				output_type="""np"""   ,				)
      _UpperCAmelCase						 =      output.images
      _UpperCAmelCase						 =      images[0, 255:258, 255:258, -1]

      assert images.shape == (1, 512, 512, 3)
      _UpperCAmelCase						 =      np.array([0.2_514, 0.3_007, 0.3_517, 0.1_790, 0.2_382, 0.3_167, 0.1_944, 0.2_273, 0.2_464]					)

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


    def      lowerCAmelCase_  (					self	:  List[Any]					):
      _UpperCAmelCase						 =      load_image(
          """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
          """/in_paint/overture-creations-5sI6fQgYIuo.png"""					)
      _UpperCAmelCase						 =      load_image(
          """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
          """/in_paint/overture-creations-5sI6fQgYIuo_mask.png"""					)
      _UpperCAmelCase						 =      LMSDiscreteScheduler.from_pretrained(
          """runwayml/stable-diffusion-inpainting"""   ,				subfolder="""scheduler"""   ,				revision="""onnx"""					)
      _UpperCAmelCase						 =      OnnxStableDiffusionInpaintPipeline.from_pretrained(
          """runwayml/stable-diffusion-inpainting"""   ,				revision="""onnx"""   ,				scheduler=__lowerCAmelCase   ,				safety_checker=__lowerCAmelCase   ,				feature_extractor=__lowerCAmelCase   ,				provider=self.gpu_provider   ,				sess_options=self.gpu_options   ,				)
      pipe.set_progress_bar_config(disable=__lowerCAmelCase					)

      _UpperCAmelCase						 =      "A red cat sitting on a park bench"

      _UpperCAmelCase						 =      np.random.RandomState(0					)
      _UpperCAmelCase						 =      pipe(
          prompt=__lowerCAmelCase   ,				image=__lowerCAmelCase   ,				mask_image=__lowerCAmelCase   ,				guidance_scale=7.5   ,				num_inference_steps=20   ,				generator=__lowerCAmelCase   ,				output_type="""np"""   ,				)
      _UpperCAmelCase						 =      output.images
      _UpperCAmelCase						 =      images[0, 255:258, 255:258, -1]

      assert images.shape == (1, 512, 512, 3)
      _UpperCAmelCase						 =      np.array([0.0_086, 0.0_077, 0.0_083, 0.0_093, 0.0_107, 0.0_139, 0.0_094, 0.0_097, 0.0_125]					)

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

"""simple docstring"""


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


UpperCAmelCase__  		=						logging.get_logger(__name__)

UpperCAmelCase__  		=						{
    """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        a				(		lowerCAmelCase_	,							lowerCAmelCase_   ):
    _snake_case     :  Union[str, Any]     							=							'nat'

    _snake_case     :  List[str]     							=							{
        'num_attention_heads': 'num_heads',
        'num_hidden_layers': 'num_layers',
    }


    def __init__(					self	:  List[Any]   ,				__lowerCAmelCase	:  int=4   ,				__lowerCAmelCase	:  Union[str, Any]=3   ,				__lowerCAmelCase	:  Dict=64   ,				__lowerCAmelCase	:  int=[3, 4, 6, 5]   ,				__lowerCAmelCase	:  List[str]=[2, 4, 8, 16]   ,				__lowerCAmelCase	:  Tuple=7   ,				__lowerCAmelCase	:  List[str]=3.0   ,				__lowerCAmelCase	:  int=True   ,				__lowerCAmelCase	:  int=0.0   ,				__lowerCAmelCase	:  Tuple=0.0   ,				__lowerCAmelCase	:  Any=0.1   ,				__lowerCAmelCase	:  List[str]="gelu"   ,				__lowerCAmelCase	:  List[Any]=0.02   ,				__lowerCAmelCase	:  Union[str, Any]=1e-5   ,				__lowerCAmelCase	:  List[Any]=0.0   ,				__lowerCAmelCase	:  Tuple=None   ,				__lowerCAmelCase	:  Optional[int]=None   ,				**__lowerCAmelCase	:  Optional[Any]   ,				):
      super().__init__(**__lowerCAmelCase					)

      _UpperCAmelCase						 =      patch_size
      _UpperCAmelCase						 =      num_channels
      _UpperCAmelCase						 =      embed_dim
      _UpperCAmelCase						 =      depths
      _UpperCAmelCase						 =      len(__lowerCAmelCase					)
      _UpperCAmelCase						 =      num_heads
      _UpperCAmelCase						 =      kernel_size
      _UpperCAmelCase						 =      mlp_ratio
      _UpperCAmelCase						 =      qkv_bias
      _UpperCAmelCase						 =      hidden_dropout_prob
      _UpperCAmelCase						 =      attention_probs_dropout_prob
      _UpperCAmelCase						 =      drop_path_rate
      _UpperCAmelCase						 =      hidden_act
      _UpperCAmelCase						 =      layer_norm_eps
      _UpperCAmelCase						 =      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
      _UpperCAmelCase						 =      int(embed_dim * 2 ** (len(__lowerCAmelCase					) - 1)					)
      _UpperCAmelCase						 =      layer_scale_init_value
      _UpperCAmelCase						 =      ["""stem"""] + [f'''stage{idx}''' for idx in range(1   ,				len(__lowerCAmelCase					) + 1					)]
      _UpperCAmelCase ,				_UpperCAmelCase						 =      get_aligned_output_features_output_indices(
          out_features=__lowerCAmelCase   ,				out_indices=__lowerCAmelCase   ,				stage_names=self.stage_names					)
