"""simple docstring"""


import math
from typing import Callable, List, Optional, Union

import numpy as np
import PIL
import torch
from PIL import Image
from transformers import CLIPTextModel, CLIPTokenizer

from diffusers.models import AutoencoderKL, UNetaDConditionModel
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_upscale import StableDiffusionUpscalePipeline
from diffusers.schedulers import DDIMScheduler, DDPMScheduler, LMSDiscreteScheduler, PNDMScheduler






def 			__UpperCAmelCase    (	lowercase      ,lowercase      ,lowercase=[]						):







  """simple docstring"""
  _UpperCAmelCase         =					size[0] - overlap_pixels * 2
  _UpperCAmelCase         =					size[1] - overlap_pixels * 2
  for letter in ["l", "r"]:
    if letter in remove_borders:
      size_x += overlap_pixels
  for letter in ["t", "b"]:
    if letter in remove_borders:
      size_y += overlap_pixels
  _UpperCAmelCase         =					np.ones((size_y, size_x)      ,dtype=np.uinta						) * 2_55
  _UpperCAmelCase         =					np.pad(lowerCAmelCase__      ,mode="""linear_ramp"""      ,pad_width=lowerCAmelCase__      ,end_values=0						)

  if "l" in remove_borders:
    _UpperCAmelCase         =					mask[:, overlap_pixels : mask.shape[1]]
  if "r" in remove_borders:
    _UpperCAmelCase         =					mask[:, 0 : mask.shape[1] - overlap_pixels]
  if "t" in remove_borders:
    _UpperCAmelCase         =					mask[overlap_pixels : mask.shape[0], :]
  if "b" in remove_borders:
    _UpperCAmelCase         =					mask[0 : mask.shape[0] - overlap_pixels, :]
  return mask






def 			__UpperCAmelCase    (	lowercase      ,lowercase      ,lowercase						):







  """simple docstring"""
  return max(lowerCAmelCase__      ,min(lowerCAmelCase__      ,lowerCAmelCase__						)						)






def 			__UpperCAmelCase    (	lowercase      ,lowercase      ,lowercase						):







  """simple docstring"""
  return (
      clamp(rect[0]      ,min[0]      ,max[0]						),
      clamp(rect[1]      ,min[1]      ,max[1]						),
      clamp(rect[2]      ,min[0]      ,max[0]						),
      clamp(rect[3]      ,min[1]      ,max[1]						),
  )






def 			__UpperCAmelCase    (	lowercase      ,lowercase      ,lowercase						):







  """simple docstring"""
  _UpperCAmelCase         =					list(lowerCAmelCase__						)
  rect[0] -= overlap
  rect[1] -= overlap
  rect[2] += overlap
  rect[3] += overlap
  _UpperCAmelCase         =					clamp_rect(lowerCAmelCase__      ,[0, 0]      ,[image_size[0], image_size[1]]						)
  return rect






def 			__UpperCAmelCase    (	lowercase      ,lowercase      ,lowercase      ,lowercase						):







  """simple docstring"""
  _UpperCAmelCase         =					Image.new("""RGB"""      ,(tile.size[0] + original_slice, tile.size[1])						)
  result.paste(
      original_image.resize((tile.size[0], tile.size[1])      ,Image.BICUBIC						).crop(
          (slice_x, 0, slice_x + original_slice, tile.size[1])						)      ,(0, 0)      ,)
  result.paste(lowerCAmelCase__      ,(original_slice, 0)						)
  return result






def 			__UpperCAmelCase    (	lowercase      ,lowercase						):







  """simple docstring"""
  _UpperCAmelCase         =					(original_image_slice * 4, 0, tile.size[0], tile.size[1])
  _UpperCAmelCase         =					tile.crop(lowerCAmelCase__						)
  return tile






def 			__UpperCAmelCase    (	lowercase      ,lowercase						):







  """simple docstring"""
  _UpperCAmelCase         =					n % d
  return n - divisor


class 				a (						__lowercase      ):


    def __init__(    self		:     str      , __lowerCAmelCase		:     AutoencoderKL      , __lowerCAmelCase		:     CLIPTextModel      , __lowerCAmelCase		:     CLIPTokenizer      , __lowerCAmelCase		:     UNetaDConditionModel      , __lowerCAmelCase		:     DDPMScheduler      , __lowerCAmelCase		:     Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]      , __lowerCAmelCase		:     int = 350      , ):
      super().__init__(
          vae=snake_case_      , text_encoder=snake_case_      , tokenizer=snake_case_      , unet=snake_case_      , low_res_scheduler=snake_case_      , scheduler=snake_case_      , max_noise_level=snake_case_      , )


    def    lowerCAmelCase_     (    self		:     Optional[int]      , __lowerCAmelCase		:     int      , __lowerCAmelCase		:     List[Any]      , __lowerCAmelCase		:     int      , __lowerCAmelCase		:     Union[str, Any]      , __lowerCAmelCase		:     Any      , __lowerCAmelCase		:     List[str]      , __lowerCAmelCase		:     List[str]      , **__lowerCAmelCase		:     Dict    ):
      torch.manual_seed(0    )
      _UpperCAmelCase         =					(
          min(image.size[0] - (tile_size + original_image_slice)      , x * tile_size    ),
          min(image.size[1] - (tile_size + original_image_slice)      , y * tile_size    ),
          min(image.size[0]      , (x + 1) * tile_size    ),
          min(image.size[1]      , (y + 1) * tile_size    ),
      )
      _UpperCAmelCase         =					add_overlap_rect(snake_case_      , snake_case_      , image.size    )
      _UpperCAmelCase         =					image.crop(snake_case_    )
      _UpperCAmelCase         =					((crop_rect[0] + ((crop_rect[2] - crop_rect[0]) / 2)) / image.size[0]) * tile.size[0]
      _UpperCAmelCase         =					translated_slice_x - (original_image_slice / 2)
      _UpperCAmelCase         =					max(0      , snake_case_    )
      _UpperCAmelCase         =					squeeze_tile(snake_case_      , snake_case_      , snake_case_      , snake_case_    )
      _UpperCAmelCase         =					to_input.size
      _UpperCAmelCase         =					to_input.resize((tile_size, tile_size)      , Image.BICUBIC    )
      _UpperCAmelCase         =					super(snake_case_      , self    ).__call__(image=snake_case_      , **snake_case_    ).images[0]
      _UpperCAmelCase         =					upscaled_tile.resize((orig_input_size[0] * 4, orig_input_size[1] * 4)      , Image.BICUBIC    )
      _UpperCAmelCase         =					unsqueeze_tile(snake_case_      , snake_case_    )
      _UpperCAmelCase         =					upscaled_tile.resize((tile.size[0] * 4, tile.size[1] * 4)      , Image.BICUBIC    )
      _UpperCAmelCase         =					[]
      if x == 0:
        remove_borders.append("""l"""    )
      elif crop_rect[2] == image.size[0]:
        remove_borders.append("""r"""    )
      if y == 0:
        remove_borders.append("""t"""    )
      elif crop_rect[3] == image.size[1]:
        remove_borders.append("""b"""    )
      _UpperCAmelCase         =					Image.fromarray(
          make_transparency_mask(
              (upscaled_tile.size[0], upscaled_tile.size[1])      , tile_border * 4      , remove_borders=snake_case_    )      , mode="""L"""      , )
      final_image.paste(
          snake_case_      , (crop_rect_with_overlap[0] * 4, crop_rect_with_overlap[1] * 4)      , snake_case_    )


    @torch.no_grad()
    def __call__(    self		:     Tuple      , __lowerCAmelCase		:     Union[str, List[str]]      , __lowerCAmelCase		:     Union[PIL.Image.Image, List[PIL.Image.Image]]      , __lowerCAmelCase		:     int = 75      , __lowerCAmelCase		:     float = 9.0      , __lowerCAmelCase		:     int = 50      , __lowerCAmelCase		:     Optional[Union[str, List[str]]] = None      , __lowerCAmelCase		:     Optional[int] = 1      , __lowerCAmelCase		:     float = 0.0      , __lowerCAmelCase		:     Optional[torch.Generator] = None      , __lowerCAmelCase		:     Optional[torch.FloatTensor] = None      , __lowerCAmelCase		:     Optional[Callable[[int, int, torch.FloatTensor], None]] = None      , __lowerCAmelCase		:     int = 1      , __lowerCAmelCase		:     int = 128      , __lowerCAmelCase		:     int = 32      , __lowerCAmelCase		:     int = 32      , ):
      _UpperCAmelCase         =					Image.new("""RGB"""      , (image.size[0] * 4, image.size[1] * 4)    )
      _UpperCAmelCase         =					math.ceil(image.size[0] / tile_size    )
      _UpperCAmelCase         =					math.ceil(image.size[1] / tile_size    )
      _UpperCAmelCase         =					tcx * tcy
      _UpperCAmelCase         =					0
      for y in range(snake_case_    ):
        for x in range(snake_case_    ):
          self._process_tile(
              snake_case_      , snake_case_      , snake_case_      , snake_case_      , snake_case_      , snake_case_      , snake_case_      , prompt=snake_case_      , num_inference_steps=snake_case_      , guidance_scale=snake_case_      , noise_level=snake_case_      , negative_prompt=snake_case_      , num_images_per_prompt=snake_case_      , eta=snake_case_      , generator=snake_case_      , latents=snake_case_      , )
          current_count += 1
          if callback is not None:
            callback({"""progress""": current_count / total_tile_count, """image""": final_image}    )
      return final_image






def 			__UpperCAmelCase    (	):







  """simple docstring"""
  # Run a demo
  _UpperCAmelCase         =					'''stabilityai/stable-diffusion-x4-upscaler'''
  _UpperCAmelCase         =					StableDiffusionTiledUpscalePipeline.from_pretrained(lowerCAmelCase__      ,revision="""fp16"""      ,torch_dtype=torch.floataa						)
  _UpperCAmelCase         =					pipe.to("""cuda"""						)
  _UpperCAmelCase         =					Image.open("""../../docs/source/imgs/diffusers_library.jpg"""						)

  def callback(lowercase						):
    print(f'''progress: {obj["progress"]:.4f}'''						)
    obj["image"].save("""diffusers_library_progress.jpg"""						)

  _UpperCAmelCase         =					pipe(image=lowerCAmelCase__      ,prompt="""Black font, white background, vector"""      ,noise_level=40      ,callback=lowerCAmelCase__						)
  final_image.save("""diffusers_library.jpg"""						)


if __name__ == "__main__":
    main()


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 		UpperCamelCase__  (       unittest.TestCase    ):
   def       lowerCAmelCase			(self :					Optional[Any]   ):
    # clean up the VRAM after each test
    super().tearDown()
    gc.collect()
   def       lowerCAmelCase			(self :					int   ):
    __a				,       __a :  Optional[Any]						=  FlaxStableDiffusionPipeline.from_pretrained(
        '''stabilityai/stable-diffusion-2'''					,				revision='''bf16'''					,				dtype=jnp.bfloataa					,				)

    __a :  Any						=  '''A painting of a squirrel eating a burger'''
    __a :  Dict						=  jax.device_count()
    __a :  Optional[int]						=  num_samples * [prompt]
    __a :  Optional[Any]						=  sd_pipe.prepare_inputs(snake_case_   )

    __a :  Optional[Any]						=  replicate(snake_case_   )
    __a :  Optional[int]						=  shard(snake_case_   )

    __a :  int						=  jax.random.PRNGKey(0   )
    __a :  str						=  jax.random.split(snake_case_					,				jax.device_count()   )

    __a :  int						=  sd_pipe(snake_case_					,				snake_case_					,				snake_case_					,				num_inference_steps=2_5					,				jit=snake_case_   )[0]
    assert images.shape == (jax.device_count(), 1, 7_6_8, 7_6_8, 3)

    __a :  Optional[int]						=  images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]   )
    __a :  List[Any]						=  images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

    __a :  Dict						=  jnp.asarray(jax.device_get(image_slice.flatten()   )   )
    __a :  str						=  jnp.array([0.4238, 0.4414, 0.4395, 0.4453, 0.4629, 0.4590, 0.4531, 0.4_5508, 0.4512]   )
    print(f"output_slice: {output_slice}"   )
    assert jnp.abs(output_slice - expected_slice   ).max() < 1E-2







   def       lowerCAmelCase			(self :					Tuple   ):
    __a :  Optional[Any]						=  '''stabilityai/stable-diffusion-2'''
    __a				,       __a :  Dict						=  FlaxDPMSolverMultistepScheduler.from_pretrained(snake_case_					,				subfolder='''scheduler'''   )
    __a				,       __a :  List[Any]						=  FlaxStableDiffusionPipeline.from_pretrained(
        snake_case_					,				scheduler=snake_case_					,				revision='''bf16'''					,				dtype=jnp.bfloataa					,				)
    __a :  Union[str, Any]						=  scheduler_params

    __a :  List[Any]						=  '''A painting of a squirrel eating a burger'''
    __a :  Any						=  jax.device_count()
    __a :  Any						=  num_samples * [prompt]
    __a :  List[Any]						=  sd_pipe.prepare_inputs(snake_case_   )

    __a :  Tuple						=  replicate(snake_case_   )
    __a :  Dict						=  shard(snake_case_   )

    __a :  Dict						=  jax.random.PRNGKey(0   )
    __a :  Dict						=  jax.random.split(snake_case_					,				jax.device_count()   )

    __a :  str						=  sd_pipe(snake_case_					,				snake_case_					,				snake_case_					,				num_inference_steps=2_5					,				jit=snake_case_   )[0]
    assert images.shape == (jax.device_count(), 1, 7_6_8, 7_6_8, 3)

    __a :  List[Any]						=  images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]   )
    __a :  Any						=  images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

    __a :  List[Any]						=  jnp.asarray(jax.device_get(image_slice.flatten()   )   )
    __a :  List[Any]						=  jnp.array([0.4336, 0.4_2969, 0.4453, 0.4199, 0.4297, 0.4531, 0.4434, 0.4434, 0.4297]   )
    print(f"output_slice: {output_slice}"   )
    assert jnp.abs(output_slice - expected_slice   ).max() < 1E-2
