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



    def    _lowercase		(self       :       Union[str, Any]					):
         # clean up the VRAM after each test
         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()



    @property
    def    _lowercase		(self       :       Any					):
         UpperCAmelCase_			       =  1
         UpperCAmelCase_			       =  3
         UpperCAmelCase_			       =  (32, 32)

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



    @property
    def    _lowercase		(self       :       List[Any]					):
         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    _lowercase		(self       :       int					):
         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    _lowercase		(self       :       Optional[Any]					):
         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(__a					)



    @property
    def    _lowercase		(self       :       Optional[Any]					):
         def extract(*__a       :       Any  ,     **__a       :       Any					):




              class 			__A :



                  def __init__(self       :       Union[str, Any]					):
                       UpperCAmelCase_			       =  torch.ones([0]					)






                  def    _lowercase		(self       :       Tuple  ,     __a       :       str					):
                       self.pixel_values.to(__a					)
                       return self




              return Out()

         return extract



    def    _lowercase		(self       :       Tuple					):
         UpperCAmelCase_			       =  "cpu"  # ensure determinism for the device-dependent torch.Generator
         UpperCAmelCase_			       =  self.dummy_cond_unet
         UpperCAmelCase_			       =  PNDMScheduler(skip_prk_steps=__a					)
         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(__a					)
         UpperCAmelCase_			       =  init_image / 2 + 0.5

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

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

         UpperCAmelCase_			       =  output.images

         UpperCAmelCase_			       =  torch.Generator(device=__a					).manual_seed(0					)
         UpperCAmelCase_			       =  alt_pipe(
             [prompt]  ,     generator=__a  ,     guidance_scale=6.0  ,     num_inference_steps=2  ,     output_type="np"  ,     image=__a  ,     return_dict=__a  ,     )[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    _lowercase		(self       :       str					):
         UpperCAmelCase_			       =  self.dummy_cond_unet
         UpperCAmelCase_			       =  PNDMScheduler(skip_prk_steps=__a					)
         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(__a					)

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

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

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

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






    @unittest.skipIf(torch_device != "cuda"  ,     "This test requires a GPU"					)
    def    _lowercase		(self       :       Tuple					):
         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(
             __a  ,     safety_checker=__a  ,     )
         pipe.to(__a					)
         pipe.set_progress_bar_config(disable=__a					)
         pipe.enable_attention_slicing()

         UpperCAmelCase_			       =  "A fantasy landscape, trending on artstation"

         UpperCAmelCase_			       =  torch.manual_seed(0					)
         UpperCAmelCase_			       =  pipe(
             prompt=__a  ,     image=__a  ,     strength=0.75  ,     guidance_scale=7.5  ,     generator=__a  ,     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    _lowercase		(self       :       Dict					):
         # clean up the VRAM after each test
         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()






    def    _lowercase		(self       :       Optional[int]					):
         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(
             __a  ,     safety_checker=__a  ,     )
         pipe.to(__a					)
         pipe.set_progress_bar_config(disable=__a					)
         pipe.enable_attention_slicing()

         UpperCAmelCase_			       =  "A fantasy landscape, trending on artstation"

         UpperCAmelCase_			       =  torch.manual_seed(0					)
         UpperCAmelCase_			       =  pipe(
             prompt=__a  ,     image=__a  ,     strength=0.75  ,     guidance_scale=7.5  ,     generator=__a  ,     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








from collections import OrderedDict
from typing import Mapping

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


__snake_case   							=						logging.get_logger(__name__)

__snake_case   							=						{
    '''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 	__lowerCamelCase			(_a						):
       _lowercase											=     """bert"""







       def __init__(			self: Any,A_: Dict=3_0522,A_: Optional[Any]=768,A_: Union[str, Any]=12,A_: List[Any]=12,A_: Optional[int]=3072,A_: Union[str, Any]="gelu",A_: List[str]=0.1,A_: Dict=0.1,A_: Optional[int]=512,A_: Optional[Any]=2,A_: Union[str, Any]=0.0_2,A_: List[Any]=1E-12,A_: Optional[int]=0,A_: List[Any]="absolute",A_: str=True,A_: Union[str, Any]=None,**A_: int,):






            '''simple docstring'''




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

            __UpperCamelCase =			vocab_size
            __UpperCamelCase =			hidden_size
            __UpperCamelCase =			num_hidden_layers
            __UpperCamelCase =			num_attention_heads
            __UpperCamelCase =			hidden_act
            __UpperCamelCase =			intermediate_size
            __UpperCamelCase =			hidden_dropout_prob
            __UpperCamelCase =			attention_probs_dropout_prob
            __UpperCamelCase =			max_position_embeddings
            __UpperCamelCase =			type_vocab_size
            __UpperCamelCase =			initializer_range
            __UpperCamelCase =			layer_norm_eps
            __UpperCamelCase =			position_embedding_type
            __UpperCamelCase =			use_cache
            __UpperCamelCase =			classifier_dropout



class 	__lowerCamelCase			(_a						):







       @property
       def 		snake_case_	(			self: Optional[int]					):






            '''simple docstring'''




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


