





"""simple docstring"""



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 		__snake_case					(		unittest.TestCase     ):





 """simple docstring"""


 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




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

 @property
 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						Any						 =     1
     __A    :						Optional[Any]						 =     3
     __A    :						Optional[Any]						 =     (32, 32)

     __A    :						Tuple						 =     floats_tensor((batch_size, num_channels) + sizes					,						rng=random.Random(0  )  ).to(__lowerCamelCase  )
     return image

 @property
 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     torch.manual_seed(0  )
     __A    :						Any						 =     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 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     torch.manual_seed(0  )
     __A    :						Optional[Any]						 =     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 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     torch.manual_seed(0  )
     __A    :						Optional[Any]						 =     RobertaSeriesConfig(
         hidden_size=32					,						project_dim=32					,						intermediate_size=37					,						layer_norm_eps=1e-0_5					,						num_attention_heads=4					,						num_hidden_layers=5					,						pad_token_id=1					,						vocab_size=5006					,						)
     return RobertaSeriesModelWithTransformation(__lowerCamelCase  )

 @property
 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     def extract(*__lowerCamelCase					,						**__lowerCamelCase  ):






         class 		__snake_case					:





          """simple docstring"""


          def __init__(    self  ):



              '''simple docstring'''




              __A    :						Any						 =     torch.ones([0]  )







          def 					UpperCamelCase__(    self					,						__lowerCamelCase  ):



              '''simple docstring'''




              self.pixel_values.to(__lowerCamelCase  )
              return self






         return Out()

     return extract

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						Any						 =     '''cpu'''  # ensure determinism for the device-dependent torch.Generator
     __A    :						Optional[int]						 =     self.dummy_cond_unet
     __A    :						int						 =     PNDMScheduler(skip_prk_steps=__lowerCamelCase  )
     __A    :						Union[str, Any]						 =     self.dummy_vae
     __A    :						Optional[Any]						 =     self.dummy_text_encoder
     __A    :						int						 =     XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta'''  )
     __A    :						Dict						 =     77

     __A    :						int						 =     self.dummy_image.to(__lowerCamelCase  )
     __A    :						List[str]						 =     init_image / 2 + 0.5

     # make sure here that pndm scheduler skips prk
     __A    :						Optional[Any]						 =     AltDiffusionImgaImgPipeline(
         unet=__lowerCamelCase					,						scheduler=__lowerCamelCase					,						vae=__lowerCamelCase					,						text_encoder=__lowerCamelCase					,						tokenizer=__lowerCamelCase					,						safety_checker=__lowerCamelCase					,						feature_extractor=self.dummy_extractor					,						)
     __A    :						str						 =     VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor					,						do_normalize=__lowerCamelCase  )
     __A    :						List[str]						 =     alt_pipe.to(__lowerCamelCase  )
     alt_pipe.set_progress_bar_config(disable=__lowerCamelCase  )

     __A    :						Any						 =     '''A painting of a squirrel eating a burger'''
     __A    :						Tuple						 =     torch.Generator(device=__lowerCamelCase  ).manual_seed(0  )
     __A    :						List[str]						 =     alt_pipe(
         [prompt]					,						generator=__lowerCamelCase					,						guidance_scale=6.0					,						num_inference_steps=2					,						output_type='''np'''					,						image=__lowerCamelCase					,						)

     __A    :						List[Any]						 =     output.images

     __A    :						Union[str, Any]						 =     torch.Generator(device=__lowerCamelCase  ).manual_seed(0  )
     __A    :						Optional[Any]						 =     alt_pipe(
         [prompt]					,						generator=__lowerCamelCase					,						guidance_scale=6.0					,						num_inference_steps=2					,						output_type='''np'''					,						image=__lowerCamelCase					,						return_dict=__lowerCamelCase					,						)[0]

     __A    :						Optional[Any]						 =     image[0, -3:, -3:, -1]
     __A    :						Optional[int]						 =     image_from_tuple[0, -3:, -3:, -1]

     assert image.shape == (1, 32, 32, 3)
     __A    :						List[Any]						 =     np.array([0.4_4_2_7, 0.3_7_3_1, 0.4_2_4_9, 0.4_9_4_1, 0.4_5_4_6, 0.4_1_4_8, 0.4_1_9_3, 0.4_6_6_6, 0.4_4_9_9]  )

     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 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						Any						 =     self.dummy_cond_unet
     __A    :						Dict						 =     PNDMScheduler(skip_prk_steps=__lowerCamelCase  )
     __A    :						List[Any]						 =     self.dummy_vae
     __A    :						Optional[int]						 =     self.dummy_text_encoder
     __A    :						Tuple						 =     XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta'''  )
     __A    :						int						 =     77

     __A    :						List[Any]						 =     self.dummy_image.to(__lowerCamelCase  )

     # put models in fp16
     __A    :						Dict						 =     unet.half()
     __A    :						Tuple						 =     vae.half()
     __A    :						int						 =     bert.half()

     # make sure here that pndm scheduler skips prk
     __A    :						Dict						 =     AltDiffusionImgaImgPipeline(
         unet=__lowerCamelCase					,						scheduler=__lowerCamelCase					,						vae=__lowerCamelCase					,						text_encoder=__lowerCamelCase					,						tokenizer=__lowerCamelCase					,						safety_checker=__lowerCamelCase					,						feature_extractor=self.dummy_extractor					,						)
     __A    :						int						 =     VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor					,						do_normalize=__lowerCamelCase  )
     __A    :						str						 =     alt_pipe.to(__lowerCamelCase  )
     alt_pipe.set_progress_bar_config(disable=__lowerCamelCase  )

     __A    :						List[Any]						 =     '''A painting of a squirrel eating a burger'''
     __A    :						str						 =     torch.manual_seed(0  )
     __A    :						int						 =     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 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						Union[str, Any]						 =     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
     __A    :						str						 =     init_image.resize((760, 504)  )

     __A    :						List[Any]						 =     '''BAAI/AltDiffusion'''
     __A    :						List[str]						 =     AltDiffusionImgaImgPipeline.from_pretrained(
         __lowerCamelCase					,						safety_checker=__lowerCamelCase					,						)
     pipe.to(__lowerCamelCase  )
     pipe.set_progress_bar_config(disable=__lowerCamelCase  )
     pipe.enable_attention_slicing()

     __A    :						List[str]						 =     '''A fantasy landscape, trending on artstation'''

     __A    :						List[Any]						 =     torch.manual_seed(0  )
     __A    :						Tuple						 =     pipe(
         prompt=__lowerCamelCase					,						image=__lowerCamelCase					,						strength=0.7_5					,						guidance_scale=7.5					,						generator=__lowerCamelCase					,						output_type='''np'''					,						)
     __A    :						str						 =     output.images[0]

     __A    :						Optional[int]						 =     image[255:258, 383:386, -1]

     assert image.shape == (504, 760, 3)
     __A    :						Optional[Any]						 =     np.array([0.9_3_5_8, 0.9_3_9_7, 0.9_5_9_9, 0.9_9_0_1, 1.0_0_0_0, 1.0_0_0_0, 0.9_8_8_2, 1.0_0_0_0, 1.0_0_0_0]  )

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






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





 """simple docstring"""


 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




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







 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						str						 =     load_image(
         '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
         '''/img2img/sketch-mountains-input.jpg'''  )
     __A    :						Optional[int]						 =     init_image.resize((768, 512)  )
     __A    :						Optional[Any]						 =     load_numpy(
         '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy'''  )

     __A    :						Optional[int]						 =     '''BAAI/AltDiffusion'''
     __A    :						Dict						 =     AltDiffusionImgaImgPipeline.from_pretrained(
         __lowerCamelCase					,						safety_checker=__lowerCamelCase					,						)
     pipe.to(__lowerCamelCase  )
     pipe.set_progress_bar_config(disable=__lowerCamelCase  )
     pipe.enable_attention_slicing()

     __A    :						List[Any]						 =     '''A fantasy landscape, trending on artstation'''

     __A    :						int						 =     torch.manual_seed(0  )
     __A    :						Dict						 =     pipe(
         prompt=__lowerCamelCase					,						image=__lowerCamelCase					,						strength=0.7_5					,						guidance_scale=7.5					,						generator=__lowerCamelCase					,						output_type='''np'''					,						)
     __A    :						int						 =     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_ =			logging.get_logger(__name__)

a_ =			{
    """bert-base-uncased""": """https://huggingface.co/bert-base-uncased/resolve/main/config.json""",
    """bert-large-uncased""": """https://huggingface.co/bert-large-uncased/resolve/main/config.json""",
    """bert-base-cased""": """https://huggingface.co/bert-base-cased/resolve/main/config.json""",
    """bert-large-cased""": """https://huggingface.co/bert-large-cased/resolve/main/config.json""",
    """bert-base-multilingual-uncased""": """https://huggingface.co/bert-base-multilingual-uncased/resolve/main/config.json""",
    """bert-base-multilingual-cased""": """https://huggingface.co/bert-base-multilingual-cased/resolve/main/config.json""",
    """bert-base-chinese""": """https://huggingface.co/bert-base-chinese/resolve/main/config.json""",
    """bert-base-german-cased""": """https://huggingface.co/bert-base-german-cased/resolve/main/config.json""",
    """bert-large-uncased-whole-word-masking""": (
        """https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/config.json"""
    ),
    """bert-large-cased-whole-word-masking""": (
        """https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/config.json"""
    ),
    """bert-large-uncased-whole-word-masking-finetuned-squad""": (
        """https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/config.json"""
    ),
    """bert-large-cased-whole-word-masking-finetuned-squad""": (
        """https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/config.json"""
    ),
    """bert-base-cased-finetuned-mrpc""": """https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/config.json""",
    """bert-base-german-dbmdz-cased""": """https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/config.json""",
    """bert-base-german-dbmdz-uncased""": """https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/config.json""",
    """cl-tohoku/bert-base-japanese""": """https://huggingface.co/cl-tohoku/bert-base-japanese/resolve/main/config.json""",
    """cl-tohoku/bert-base-japanese-whole-word-masking""": (
        """https://huggingface.co/cl-tohoku/bert-base-japanese-whole-word-masking/resolve/main/config.json"""
    ),
    """cl-tohoku/bert-base-japanese-char""": (
        """https://huggingface.co/cl-tohoku/bert-base-japanese-char/resolve/main/config.json"""
    ),
    """cl-tohoku/bert-base-japanese-char-whole-word-masking""": (
        """https://huggingface.co/cl-tohoku/bert-base-japanese-char-whole-word-masking/resolve/main/config.json"""
    ),
    """TurkuNLP/bert-base-finnish-cased-v1""": (
        """https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/config.json"""
    ),
    """TurkuNLP/bert-base-finnish-uncased-v1""": (
        """https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/config.json"""
    ),
    """wietsedv/bert-base-dutch-cased""": """https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/config.json""",
    # See all BERT models at https://huggingface.co/models?filter=bert
}






class 		__snake_case					(		SCREAMING_SNAKE_CASE__     ):





 """simple docstring"""


 _lowerCamelCase        =     """bert"""

 def __init__(    self					,						__lowerCamelCase=3_0522					,						__lowerCamelCase=768					,						__lowerCamelCase=12					,						__lowerCamelCase=12					,						__lowerCamelCase=3072					,						__lowerCamelCase="gelu"					,						__lowerCamelCase=0.1					,						__lowerCamelCase=0.1					,						__lowerCamelCase=512					,						__lowerCamelCase=2					,						__lowerCamelCase=0.0_2					,						__lowerCamelCase=1e-1_2					,						__lowerCamelCase=0					,						__lowerCamelCase="absolute"					,						__lowerCamelCase=True					,						__lowerCamelCase=None					,						**__lowerCamelCase					,						):



     '''simple docstring'''




     super().__init__(pad_token_id=__lowerCamelCase					,						**__lowerCamelCase  )

     __A    :						Dict						 =     vocab_size
     __A    :						Any						 =     hidden_size
     __A    :						str						 =     num_hidden_layers
     __A    :						int						 =     num_attention_heads
     __A    :						Optional[int]						 =     hidden_act
     __A    :						List[Any]						 =     intermediate_size
     __A    :						Tuple						 =     hidden_dropout_prob
     __A    :						Optional[int]						 =     attention_probs_dropout_prob
     __A    :						Optional[int]						 =     max_position_embeddings
     __A    :						Optional[Any]						 =     type_vocab_size
     __A    :						Optional[Any]						 =     initializer_range
     __A    :						Dict						 =     layer_norm_eps
     __A    :						Any						 =     position_embedding_type
     __A    :						Optional[int]						 =     use_cache
     __A    :						str						 =     classifier_dropout






class 		__snake_case					(		SCREAMING_SNAKE_CASE__     ):





 """simple docstring"""


 @property
 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     if self.task == "multiple-choice":
         __A    :						str						 =     {0: '''batch''', 1: '''choice''', 2: '''sequence'''}
     else:
         __A    :						Tuple						 =     {0: '''batch''', 1: '''sequence'''}
     return OrderedDict(
         [
             ('''input_ids''', dynamic_axis),
             ('''attention_mask''', dynamic_axis),
             ('''token_type_ids''', dynamic_axis),
         ]  )




