





'''simple docstring'''



import gc
import random
import unittest

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

from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableDiffusionUpscalePipeline, UNetaDConditionModel
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()




class       __SCREAMING_SNAKE_CASE	(						unittest.TestCase			):



     '''simple docstring'''






     def 	UpperCamelCase(					self	):
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()

     @property
     def 	UpperCamelCase(					self	):
       _snake_case     		=				1
       _snake_case     		=				3
       _snake_case     		=				(32, 32)

       _snake_case     		=				floats_tensor((batch_size, num_channels) + sizes						,   rng=random.Random(0	)	).to(lowercase__	)
       return image

     @property
     def 	UpperCamelCase(					self	):
       torch.manual_seed(0	)
       _snake_case     		=				UNetaDConditionModel(
           block_out_channels=(32, 32, 64)						,   layers_per_block=2						,   sample_size=32						,   in_channels=7						,   out_channels=4						,   down_block_types=("DownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D")						,   up_block_types=("CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "UpBlock2D")						,   cross_attention_dim=32						,   attention_head_dim=8						,   use_linear_projection=lowercase__						,   only_cross_attention=(True, True, False)						,   num_class_embeds=100						,   )
       return model

     @property
     def 	UpperCamelCase(					self	):
       torch.manual_seed(0	)
       _snake_case     		=				AutoencoderKL(
           block_out_channels=[32, 32, 64]						,   in_channels=3						,   out_channels=3						,   down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D", "DownEncoderBlock2D"]						,   up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D"]						,   latent_channels=4						,   )
       return model

     @property
     def 	UpperCamelCase(					self	):
       torch.manual_seed(0	)
       _snake_case     		=				CLIPTextConfig(
           bos_token_id=0						,   eos_token_id=2						,   hidden_size=32						,   intermediate_size=37						,   layer_norm_eps=1e-05						,   num_attention_heads=4						,   num_hidden_layers=5						,   pad_token_id=1						,   vocab_size=1_000						,   hidden_act="gelu"						,   projection_dim=512						,   )
       return CLIPTextModel(lowercase__	)

     def 	UpperCamelCase(					self	):
       _snake_case     		=				'''cpu'''  # ensure determinism for the device-dependent torch.Generator
       _snake_case     		=				self.dummy_cond_unet_upscale
       _snake_case     		=				DDPMScheduler()
       _snake_case     		=				DDIMScheduler(prediction_type="v_prediction"	)
       _snake_case     		=				self.dummy_vae
       _snake_case     		=				self.dummy_text_encoder
       _snake_case     		=				CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"	)

       _snake_case     		=				self.dummy_image.cpu().permute(0						,   2						,   3						,   1	)[0]
       _snake_case     		=				Image.fromarray(np.uinta(lowercase__	)	).convert("RGB"	).resize((64, 64)	)

       # make sure here that pndm scheduler skips prk
       _snake_case     		=				StableDiffusionUpscalePipeline(
           unet=lowercase__						,   low_res_scheduler=lowercase__						,   scheduler=lowercase__						,   vae=lowercase__						,   text_encoder=lowercase__						,   tokenizer=lowercase__						,   max_noise_level=350						,   )
       _snake_case     		=				sd_pipe.to(lowercase__	)
       sd_pipe.set_progress_bar_config(disable=lowercase__	)

       _snake_case     		=				'''A painting of a squirrel eating a burger'''
       _snake_case     		=				torch.Generator(device=lowercase__	).manual_seed(0	)
       _snake_case     		=				sd_pipe(
           [prompt]						,   image=lowercase__						,   generator=lowercase__						,   guidance_scale=6.0						,   noise_level=20						,   num_inference_steps=2						,   output_type="np"						,   )

       _snake_case     		=				output.images

       _snake_case     		=				torch.Generator(device=lowercase__	).manual_seed(0	)
       _snake_case     		=				sd_pipe(
           [prompt]						,   image=lowercase__						,   generator=lowercase__						,   guidance_scale=6.0						,   noise_level=20						,   num_inference_steps=2						,   output_type="np"						,   return_dict=lowercase__						,   )[0]

       _snake_case     		=				image[0, -3:, -3:, -1]
       _snake_case     		=				image_from_tuple[0, -3:, -3:, -1]

       _snake_case     		=				low_res_image.size[0] * 4
       assert image.shape == (1, expected_height_width, expected_height_width, 3)
       _snake_case     		=				np.array([0.3113, 0.3910, 0.4272, 0.4859, 0.5061, 0.4652, 0.5362, 0.5715, 0.5661]	)

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

     def 	UpperCamelCase(					self	):
       _snake_case     		=				'''cpu'''  # ensure determinism for the device-dependent torch.Generator
       _snake_case     		=				self.dummy_cond_unet_upscale
       _snake_case     		=				DDPMScheduler()
       _snake_case     		=				DDIMScheduler(prediction_type="v_prediction"	)
       _snake_case     		=				self.dummy_vae
       _snake_case     		=				self.dummy_text_encoder
       _snake_case     		=				CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"	)

       _snake_case     		=				self.dummy_image.cpu().permute(0						,   2						,   3						,   1	)[0]
       _snake_case     		=				Image.fromarray(np.uinta(lowercase__	)	).convert("RGB"	).resize((64, 64)	)

       # make sure here that pndm scheduler skips prk
       _snake_case     		=				StableDiffusionUpscalePipeline(
           unet=lowercase__						,   low_res_scheduler=lowercase__						,   scheduler=lowercase__						,   vae=lowercase__						,   text_encoder=lowercase__						,   tokenizer=lowercase__						,   max_noise_level=350						,   )
       _snake_case     		=				sd_pipe.to(lowercase__	)
       sd_pipe.set_progress_bar_config(disable=lowercase__	)

       _snake_case     		=				'''A painting of a squirrel eating a burger'''
       _snake_case     		=				sd_pipe(
           2 * [prompt]						,   image=2 * [low_res_image]						,   guidance_scale=6.0						,   noise_level=20						,   num_inference_steps=2						,   output_type="np"						,   )
       _snake_case     		=				output.images
       assert image.shape[0] == 2

       _snake_case     		=				torch.Generator(device=lowercase__	).manual_seed(0	)
       _snake_case     		=				sd_pipe(
           [prompt]						,   image=lowercase__						,   generator=lowercase__						,   num_images_per_prompt=2						,   guidance_scale=6.0						,   noise_level=20						,   num_inference_steps=2						,   output_type="np"						,   )
       _snake_case     		=				output.images
       assert image.shape[0] == 2






     @unittest.skipIf(torch_device != "cuda"						,   "This test requires a GPU"	)
     def 	UpperCamelCase(					self	):
       _snake_case     		=				self.dummy_cond_unet_upscale
       _snake_case     		=				DDPMScheduler()
       _snake_case     		=				DDIMScheduler(prediction_type="v_prediction"	)
       _snake_case     		=				self.dummy_vae
       _snake_case     		=				self.dummy_text_encoder
       _snake_case     		=				CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"	)

       _snake_case     		=				self.dummy_image.cpu().permute(0						,   2						,   3						,   1	)[0]
       _snake_case     		=				Image.fromarray(np.uinta(lowercase__	)	).convert("RGB"	).resize((64, 64)	)

       # put models in fp16, except vae as it overflows in fp16
       _snake_case     		=				unet.half()
       _snake_case     		=				text_encoder.half()

       # make sure here that pndm scheduler skips prk
       _snake_case     		=				StableDiffusionUpscalePipeline(
           unet=lowercase__						,   low_res_scheduler=lowercase__						,   scheduler=lowercase__						,   vae=lowercase__						,   text_encoder=lowercase__						,   tokenizer=lowercase__						,   max_noise_level=350						,   )
       _snake_case     		=				sd_pipe.to(lowercase__	)
       sd_pipe.set_progress_bar_config(disable=lowercase__	)

       _snake_case     		=				'''A painting of a squirrel eating a burger'''
       _snake_case     		=				torch.manual_seed(0	)
       _snake_case     		=				sd_pipe(
           [prompt]						,   image=lowercase__						,   generator=lowercase__						,   num_inference_steps=2						,   output_type="np"						,   ).images

       _snake_case     		=				low_res_image.size[0] * 4
       assert image.shape == (1, expected_height_width, expected_height_width, 3)







@slow
@require_torch_gpu
class       __SCREAMING_SNAKE_CASE	(						unittest.TestCase			):



     '''simple docstring'''






     def 	UpperCamelCase(					self	):
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()

     def 	UpperCamelCase(					self	):
       _snake_case     		=				load_image(
           "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
           "/sd2-upscale/low_res_cat.png"	)
       _snake_case     		=				load_numpy(
           "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale"
           "/upsampled_cat.npy"	)

       _snake_case     		=				'''stabilityai/stable-diffusion-x4-upscaler'''
       _snake_case     		=				StableDiffusionUpscalePipeline.from_pretrained(lowercase__	)
       pipe.to(lowercase__	)
       pipe.set_progress_bar_config(disable=lowercase__	)
       pipe.enable_attention_slicing()

       _snake_case     		=				'''a cat sitting on a park bench'''

       _snake_case     		=				torch.manual_seed(0	)
       _snake_case     		=				pipe(
           prompt=lowercase__						,   image=lowercase__						,   generator=lowercase__						,   output_type="np"						,   )
       _snake_case     		=				output.images[0]

       assert image.shape == (512, 512, 3)
       assert np.abs(expected_image - image	).max() < 1e-3

     def 	UpperCamelCase(					self	):
       _snake_case     		=				load_image(
           "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
           "/sd2-upscale/low_res_cat.png"	)
       _snake_case     		=				load_numpy(
           "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale"
           "/upsampled_cat_fp16.npy"	)

       _snake_case     		=				'''stabilityai/stable-diffusion-x4-upscaler'''
       _snake_case     		=				StableDiffusionUpscalePipeline.from_pretrained(
           lowercase__						,   torch_dtype=torch.floataa						,   )
       pipe.to(lowercase__	)
       pipe.set_progress_bar_config(disable=lowercase__	)
       pipe.enable_attention_slicing()

       _snake_case     		=				'''a cat sitting on a park bench'''

       _snake_case     		=				torch.manual_seed(0	)
       _snake_case     		=				pipe(
           prompt=lowercase__						,   image=lowercase__						,   generator=lowercase__						,   output_type="np"						,   )
       _snake_case     		=				output.images[0]

       assert image.shape == (512, 512, 3)
       assert np.abs(expected_image - image	).max() < 5e-1






     def 	UpperCamelCase(					self	):
       torch.cuda.empty_cache()
       torch.cuda.reset_max_memory_allocated()
       torch.cuda.reset_peak_memory_stats()

       _snake_case     		=				load_image(
           "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
           "/sd2-upscale/low_res_cat.png"	)

       _snake_case     		=				'''stabilityai/stable-diffusion-x4-upscaler'''
       _snake_case     		=				StableDiffusionUpscalePipeline.from_pretrained(
           lowercase__						,   torch_dtype=torch.floataa						,   )
       pipe.to(lowercase__	)
       pipe.set_progress_bar_config(disable=lowercase__	)
       pipe.enable_attention_slicing(1	)
       pipe.enable_sequential_cpu_offload()

       _snake_case     		=				'''a cat sitting on a park bench'''

       _snake_case     		=				torch.manual_seed(0	)
       _snake_case     		=				pipe(
           prompt=lowercase__						,   image=lowercase__						,   generator=lowercase__						,   num_inference_steps=5						,   output_type="np"						,   )

       _snake_case     		=				torch.cuda.max_memory_allocated()
       # make sure that less than 2.9 GB is allocated
       assert mem_bytes < 2.9 * 10**9









'''simple docstring'''



from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__magic_name__   :						int    =			logging.get_logger(__name__)

__magic_name__   :						Tuple    =			{
    """xlm-mlm-en-2048""": """https://huggingface.co/xlm-mlm-en-2048/resolve/main/config.json""",
    """xlm-mlm-ende-1024""": """https://huggingface.co/xlm-mlm-ende-1024/resolve/main/config.json""",
    """xlm-mlm-enfr-1024""": """https://huggingface.co/xlm-mlm-enfr-1024/resolve/main/config.json""",
    """xlm-mlm-enro-1024""": """https://huggingface.co/xlm-mlm-enro-1024/resolve/main/config.json""",
    """xlm-mlm-tlm-xnli15-1024""": """https://huggingface.co/xlm-mlm-tlm-xnli15-1024/resolve/main/config.json""",
    """xlm-mlm-xnli15-1024""": """https://huggingface.co/xlm-mlm-xnli15-1024/resolve/main/config.json""",
    """xlm-clm-enfr-1024""": """https://huggingface.co/xlm-clm-enfr-1024/resolve/main/config.json""",
    """xlm-clm-ende-1024""": """https://huggingface.co/xlm-clm-ende-1024/resolve/main/config.json""",
    """xlm-mlm-17-1280""": """https://huggingface.co/xlm-mlm-17-1280/resolve/main/config.json""",
    """xlm-mlm-100-1280""": """https://huggingface.co/xlm-mlm-100-1280/resolve/main/config.json""",
}


class       __SCREAMING_SNAKE_CASE	(						__UpperCamelCase			):



     '''simple docstring'''






     UpperCAmelCase__       :						Union[str, Any]      =   '''xlm'''
     UpperCAmelCase__       :						Dict      =   {
         '''hidden_size''': '''emb_dim''',
         '''num_attention_heads''': '''n_heads''',
         '''num_hidden_layers''': '''n_layers''',
         '''n_words''': '''vocab_size''',  # For backward compatibility
     }

     def __init__(					self						,   lowerCamelCase=30_145						,   lowerCamelCase=2_048						,   lowerCamelCase=12						,   lowerCamelCase=16						,   lowerCamelCase=0.1						,   lowerCamelCase=0.1						,   lowerCamelCase=True						,   lowerCamelCase=False						,   lowerCamelCase=False						,   lowerCamelCase=False						,   lowerCamelCase=1						,   lowerCamelCase=True						,   lowerCamelCase=512						,   lowerCamelCase=2_048**-0.5						,   lowerCamelCase=1e-12						,   lowerCamelCase=0.02						,   lowerCamelCase=0						,   lowerCamelCase=1						,   lowerCamelCase=2						,   lowerCamelCase=3						,   lowerCamelCase=5						,   lowerCamelCase=True						,   lowerCamelCase="first"						,   lowerCamelCase=True						,   lowerCamelCase=None						,   lowerCamelCase=True						,   lowerCamelCase=0.1						,   lowerCamelCase=5						,   lowerCamelCase=5						,   lowerCamelCase=0						,   lowerCamelCase=0						,   lowerCamelCase=2						,   lowerCamelCase=0						,   **lowerCamelCase						,   ):
       _snake_case     		=				vocab_size
       _snake_case     		=				emb_dim
       _snake_case     		=				n_layers
       _snake_case     		=				n_heads
       _snake_case     		=				dropout
       _snake_case     		=				attention_dropout
       _snake_case     		=				gelu_activation
       _snake_case     		=				sinusoidal_embeddings
       _snake_case     		=				causal
       _snake_case     		=				asm
       _snake_case     		=				n_langs
       _snake_case     		=				use_lang_emb
       _snake_case     		=				layer_norm_eps
       _snake_case     		=				bos_index
       _snake_case     		=				eos_index
       _snake_case     		=				pad_index
       _snake_case     		=				unk_index
       _snake_case     		=				mask_index
       _snake_case     		=				is_encoder
       _snake_case     		=				max_position_embeddings
       _snake_case     		=				embed_init_std
       _snake_case     		=				init_std
       _snake_case     		=				summary_type
       _snake_case     		=				summary_use_proj
       _snake_case     		=				summary_activation
       _snake_case     		=				summary_proj_to_labels
       _snake_case     		=				summary_first_dropout
       _snake_case     		=				start_n_top
       _snake_case     		=				end_n_top
       _snake_case     		=				mask_token_id
       _snake_case     		=				lang_id

       if "n_words" in kwargs:
         _snake_case     		=				kwargs["n_words"]

       super().__init__(pad_token_id=lowerCamelCase						,   bos_token_id=lowerCamelCase						,   **lowerCamelCase	)







class       __SCREAMING_SNAKE_CASE	(						__UpperCamelCase			):



     '''simple docstring'''






     @property
     def 	UpperCamelCase(					self	):
       if self.task == "multiple-choice":
         _snake_case     		=				{0: "batch", 1: "choice", 2: "sequence"}
       else:
         _snake_case     		=				{0: "batch", 1: "sequence"}
       return OrderedDict(
           [
               ("input_ids", dynamic_axis),
               ("attention_mask", dynamic_axis),
               ("token_type_ids", dynamic_axis),
           ]	)


