





import gc
import random
import unittest

import numpy as np
import torch
from transformers import XLMRobertaTokenizer

from diffusers import (
    AltDiffusionImgaImgPipeline,
    AutoencoderKL,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.image_processor import VaeImageProcessor
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
    RobertaSeriesConfig,
    RobertaSeriesModelWithTransformation,
)
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   A							(						unittest.TestCase    ):



      def 						lowerCAmelCase__     (						self:    Dict							)    ->     Union[str, Any]:
         '''simple docstring'''

         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()



      @property
      def 						lowerCAmelCase__     (						self:    Any							)    ->     Dict:
         '''simple docstring'''

         UpperCAmelCase_           =1
         UpperCAmelCase_           =3
         UpperCAmelCase_           =(32, 32)

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



      @property
      def 						lowerCAmelCase__     (						self:    List[str]							)    ->     Dict:
         '''simple docstring'''

         torch.manual_seed(0							)
         UpperCAmelCase_           =UNetaDConditionModel(
             block_out_channels=(32, 64)		,	layers_per_block=2		,	sample_size=32		,	in_channels=4		,	out_channels=4		,	down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")		,	up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")		,	cross_attention_dim=32		,	)
         return model



      @property
      def 						lowerCAmelCase__     (						self:    Optional[int]							)    ->     int:
         '''simple docstring'''

         torch.manual_seed(0							)
         UpperCAmelCase_           =AutoencoderKL(
             block_out_channels=[32, 64]		,	in_channels=3		,	out_channels=3		,	down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]		,	up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]		,	latent_channels=4		,	)
         return model



      @property
      def 						lowerCAmelCase__     (						self:    List[str]							)    ->     List[str]:
         '''simple docstring'''

         torch.manual_seed(0							)
         UpperCAmelCase_           =RobertaSeriesConfig(
             hidden_size=32		,	project_dim=32		,	intermediate_size=37		,	layer_norm_eps=1e-05		,	num_attention_heads=4		,	num_hidden_layers=5		,	pad_token_id=1		,	vocab_size=5006		,	)
         return RobertaSeriesModelWithTransformation(_lowerCAmelCase							)



      @property
      def 						lowerCAmelCase__     (						self:    Union[str, Any]							)    ->     Union[str, Any]:
         '''simple docstring'''

         def extract(*_lowerCAmelCase:    List[Any]		,	**_lowerCAmelCase:    int							):

            class   A							:



                  def __init__(						self:    List[str]							)    ->     Dict:
                     '''simple docstring'''

                     UpperCAmelCase_           =torch.ones([0]							)






                  def 						lowerCAmelCase__     (						self:    List[str]		,	_lowerCAmelCase:    str							)    ->     Any:
                     '''simple docstring'''

                     self.pixel_values.to(_lowerCAmelCase							)
                     return self

            return Out()

         return extract



      def 						lowerCAmelCase__     (						self:    Dict							)    ->     Optional[int]:
         '''simple docstring'''

         UpperCAmelCase_           ="cpu"  # ensure determinism for the device-dependent torch.Generator
         UpperCAmelCase_           =self.dummy_cond_unet
         UpperCAmelCase_           =PNDMScheduler(skip_prk_steps=_lowerCAmelCase							)
         UpperCAmelCase_           =self.dummy_vae
         UpperCAmelCase_           =self.dummy_text_encoder
         UpperCAmelCase_           =XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta"							)
         UpperCAmelCase_           =77

         UpperCAmelCase_           =self.dummy_image.to(_lowerCAmelCase							)
         UpperCAmelCase_           =init_image / 2 + 0.5

         # make sure here that pndm scheduler skips prk
         UpperCAmelCase_           =AltDiffusionImgaImgPipeline(
             unet=_lowerCAmelCase		,	scheduler=_lowerCAmelCase		,	vae=_lowerCAmelCase		,	text_encoder=_lowerCAmelCase		,	tokenizer=_lowerCAmelCase		,	safety_checker=_lowerCAmelCase		,	feature_extractor=self.dummy_extractor		,	)
         UpperCAmelCase_           =VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor		,	do_normalize=_lowerCAmelCase							)
         UpperCAmelCase_           =alt_pipe.to(_lowerCAmelCase							)
         alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase							)

         UpperCAmelCase_           ="A painting of a squirrel eating a burger"
         UpperCAmelCase_           =torch.Generator(device=_lowerCAmelCase							).manual_seed(0							)
         UpperCAmelCase_           =alt_pipe(
             [prompt]		,	generator=_lowerCAmelCase		,	guidance_scale=6.0		,	num_inference_steps=2		,	output_type="np"		,	image=_lowerCAmelCase		,	)

         UpperCAmelCase_           =output.images

         UpperCAmelCase_           =torch.Generator(device=_lowerCAmelCase							).manual_seed(0							)
         UpperCAmelCase_           =alt_pipe(
             [prompt]		,	generator=_lowerCAmelCase		,	guidance_scale=6.0		,	num_inference_steps=2		,	output_type="np"		,	image=_lowerCAmelCase		,	return_dict=_lowerCAmelCase		,	)[0]

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

         assert image.shape == (1, 32, 32, 3)
         UpperCAmelCase_           =np.array([0.44_27, 0.37_31, 0.42_49, 0.49_41, 0.45_46, 0.41_48, 0.41_93, 0.46_66, 0.44_99]							)

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



      @unittest.skipIf(torch_device != "cuda"		,	"This test requires a GPU"							)
      def 						lowerCAmelCase__     (						self:    List[str]							)    ->     Optional[Any]:
         '''simple docstring'''

         UpperCAmelCase_           =self.dummy_cond_unet
         UpperCAmelCase_           =PNDMScheduler(skip_prk_steps=_lowerCAmelCase							)
         UpperCAmelCase_           =self.dummy_vae
         UpperCAmelCase_           =self.dummy_text_encoder
         UpperCAmelCase_           =XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta"							)
         UpperCAmelCase_           =77

         UpperCAmelCase_           =self.dummy_image.to(_lowerCAmelCase							)

         # put models in fp16
         UpperCAmelCase_           =unet.half()
         UpperCAmelCase_           =vae.half()
         UpperCAmelCase_           =bert.half()

         # make sure here that pndm scheduler skips prk
         UpperCAmelCase_           =AltDiffusionImgaImgPipeline(
             unet=_lowerCAmelCase		,	scheduler=_lowerCAmelCase		,	vae=_lowerCAmelCase		,	text_encoder=_lowerCAmelCase		,	tokenizer=_lowerCAmelCase		,	safety_checker=_lowerCAmelCase		,	feature_extractor=self.dummy_extractor		,	)
         UpperCAmelCase_           =VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor		,	do_normalize=_lowerCAmelCase							)
         UpperCAmelCase_           =alt_pipe.to(_lowerCAmelCase							)
         alt_pipe.set_progress_bar_config(disable=_lowerCAmelCase							)

         UpperCAmelCase_           ="A painting of a squirrel eating a burger"
         UpperCAmelCase_           =torch.manual_seed(0							)
         UpperCAmelCase_           =alt_pipe(
             [prompt]		,	generator=_lowerCAmelCase		,	num_inference_steps=2		,	output_type="np"		,	image=_lowerCAmelCase		,	).images

         assert image.shape == (1, 32, 32, 3)






      @unittest.skipIf(torch_device != "cuda"		,	"This test requires a GPU"							)
      def 						lowerCAmelCase__     (						self:    List[str]							)    ->     Union[str, Any]:
         '''simple docstring'''

         UpperCAmelCase_           =load_image(
             "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
             "/img2img/sketch-mountains-input.jpg"							)
         # resize to resolution that is divisible by 8 but not 16 or 32
         UpperCAmelCase_           =init_image.resize((760, 504)							)

         UpperCAmelCase_           ="BAAI/AltDiffusion"
         UpperCAmelCase_           =AltDiffusionImgaImgPipeline.from_pretrained(
             _lowerCAmelCase		,	safety_checker=_lowerCAmelCase		,	)
         pipe.to(_lowerCAmelCase							)
         pipe.set_progress_bar_config(disable=_lowerCAmelCase							)
         pipe.enable_attention_slicing()

         UpperCAmelCase_           ="A fantasy landscape, trending on artstation"

         UpperCAmelCase_           =torch.manual_seed(0							)
         UpperCAmelCase_           =pipe(
             prompt=_lowerCAmelCase		,	image=_lowerCAmelCase		,	strength=0.75		,	guidance_scale=7.5		,	generator=_lowerCAmelCase		,	output_type="np"		,	)
         UpperCAmelCase_           =output.images[0]

         UpperCAmelCase_           =image[255:258, 383:386, -1]

         assert image.shape == (504, 760, 3)
         UpperCAmelCase_           =np.array([0.93_58, 0.93_97, 0.95_99, 0.99_01, 1.00_00, 1.00_00, 0.98_82, 1.00_00, 1.00_00]							)

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

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



      def 						lowerCAmelCase__     (						self:    Dict							)    ->     Optional[Any]:
         '''simple docstring'''

         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()






      def 						lowerCAmelCase__     (						self:    Dict							)    ->     Dict:
         '''simple docstring'''

         UpperCAmelCase_           =load_image(
             "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
             "/img2img/sketch-mountains-input.jpg"							)
         UpperCAmelCase_           =init_image.resize((768, 512)							)
         UpperCAmelCase_           =load_numpy(
             "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy"							)

         UpperCAmelCase_           ="BAAI/AltDiffusion"
         UpperCAmelCase_           =AltDiffusionImgaImgPipeline.from_pretrained(
             _lowerCAmelCase		,	safety_checker=_lowerCAmelCase		,	)
         pipe.to(_lowerCAmelCase							)
         pipe.set_progress_bar_config(disable=_lowerCAmelCase							)
         pipe.enable_attention_slicing()

         UpperCAmelCase_           ="A fantasy landscape, trending on artstation"

         UpperCAmelCase_           =torch.manual_seed(0							)
         UpperCAmelCase_           =pipe(
             prompt=_lowerCAmelCase		,	image=_lowerCAmelCase		,	strength=0.75		,	guidance_scale=7.5		,	generator=_lowerCAmelCase		,	output_type="np"		,	)
         UpperCAmelCase_           =output.images[0]

         assert image.shape == (512, 768, 3)
         # img2img is flaky across GPUs even in fp32, so using MAE here
         assert np.abs(expected_image - image							).max() < 1e-2











'''simple docstring'''




from collections import OrderedDict
from typing import Mapping

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


a__					:			str          =     logging.get_logger(__name__)

a__					:			Any          =     {
    '''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 				__snake_case     (					__magic_name__							):
	__lowerCAmelCase       						=  '''xlm'''
	__lowerCAmelCase       						=  {
	    '''hidden_size''': '''emb_dim''',
	    '''num_attention_heads''': '''n_heads''',
	    '''num_hidden_layers''': '''n_layers''',
	    '''n_words''': '''vocab_size''',  # For backward compatibility
	}





	def __init__(		self      ,       UpperCamelCase_=3_0145      ,       UpperCamelCase_=2048      ,       UpperCamelCase_=12      ,       UpperCamelCase_=16      ,       UpperCamelCase_=0.1      ,       UpperCamelCase_=0.1      ,       UpperCamelCase_=True      ,       UpperCamelCase_=False      ,       UpperCamelCase_=False      ,       UpperCamelCase_=False      ,       UpperCamelCase_=1      ,       UpperCamelCase_=True      ,       UpperCamelCase_=512      ,       UpperCamelCase_=2048**-0.5      ,       UpperCamelCase_=1E-1_2      ,       UpperCamelCase_=0.0_2      ,       UpperCamelCase_=0      ,       UpperCamelCase_=1      ,       UpperCamelCase_=2      ,       UpperCamelCase_=3      ,       UpperCamelCase_=5      ,       UpperCamelCase_=True      ,       UpperCamelCase_="first"      ,       UpperCamelCase_=True      ,       UpperCamelCase_=None      ,       UpperCamelCase_=True      ,       UpperCamelCase_=0.1      ,       UpperCamelCase_=5      ,       UpperCamelCase_=5      ,       UpperCamelCase_=0      ,       UpperCamelCase_=0      ,       UpperCamelCase_=2      ,       UpperCamelCase_=0      ,       **UpperCamelCase_      ,       )      ->		List[str]:
		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=UpperCamelCase_      ,       bos_token_id=UpperCamelCase_      ,       **UpperCamelCase_		)






class 				__snake_case     (					__magic_name__							):





	@property
	def   _snake_case   (		self		)      ->		Mapping[str, Mapping[int, str]]:
		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),
		    ]		)



