






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




       def 					a_					(					self      : Union[str, Any]					):


            """simple docstring"""







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




       @property
       def 					a_					(					self      : Tuple					):


            """simple docstring"""







            __lowerCamelCase						:       str				   =				1
            __lowerCamelCase						:       List[Any]				   =				3
            __lowerCamelCase						:       Tuple				   =				(32, 32)

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




       @property
       def 					a_					(					self      : Union[str, Any]					):


            """simple docstring"""







            torch.manual_seed(0					)
            __lowerCamelCase						:       List[str]				   =				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 					a_					(					self      : str					):


            """simple docstring"""







            torch.manual_seed(0					)
            __lowerCamelCase						:       List[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 					a_					(					self      : List[str]					):


            """simple docstring"""







            torch.manual_seed(0					)
            __lowerCamelCase						:       Dict				   =				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(A__					)




       @property
       def 					a_					(					self      : List[str]					):


            """simple docstring"""







            def extract(*A__      : List[str]	,    **A__      : Optional[Any]					):







                 class 	SCREAMING_SNAKE_CASE	:




                        def __init__(					self      : Dict					):


                             """simple docstring"""







                             __lowerCamelCase						:       Tuple				   =				torch.ones([0]					)




                        def 					a_					(					self      : Optional[int]	,    A__      : Dict					):


                             """simple docstring"""







                             self.pixel_values.to(A__					)
                             return self

                 return Out()

            return extract




       def 					a_					(					self      : Any					):


            """simple docstring"""







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

            __lowerCamelCase						:       int				   =				self.dummy_image.to(A__					)
            __lowerCamelCase						:       Optional[Any]				   =				init_image / 2 + 0.5

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

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

            __lowerCamelCase						:       Tuple				   =				output.images

            __lowerCamelCase						:       int				   =				torch.Generator(device=A__					).manual_seed(0					)
            __lowerCamelCase						:       str				   =				alt_pipe(
                [prompt]	,    generator=A__	,    guidance_scale=6.0	,    num_inference_steps=2	,    output_type="""np"""	,    image=A__	,    return_dict=A__	,    )[0]

            __lowerCamelCase						:       int				   =				image[0, -3:, -3:, -1]
            __lowerCamelCase						:       Dict				   =				image_from_tuple[0, -3:, -3:, -1]

            assert image.shape == (1, 32, 32, 3)
            __lowerCamelCase						:       int				   =				np.array([0.4427, 0.3731, 0.4249, 0.4941, 0.4546, 0.4148, 0.4193, 0.4666, 0.4499]					)

            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 					a_					(					self      : Union[str, Any]					):


            """simple docstring"""







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

            __lowerCamelCase						:       int				   =				self.dummy_image.to(A__					)

            # put models in fp16
            __lowerCamelCase						:       List[str]				   =				unet.half()
            __lowerCamelCase						:       Optional[Any]				   =				vae.half()
            __lowerCamelCase						:       Tuple				   =				bert.half()

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

            __lowerCamelCase						:       Optional[int]				   =				"""A painting of a squirrel eating a burger"""
            __lowerCamelCase						:       int				   =				torch.manual_seed(0					)
            __lowerCamelCase						:       Optional[Any]				   =				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 					a_					(					self      : str					):


            """simple docstring"""







            __lowerCamelCase						:       Optional[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
            __lowerCamelCase						:       str				   =				init_image.resize((760, 504)					)

            __lowerCamelCase						:       Any				   =				"""BAAI/AltDiffusion"""
            __lowerCamelCase						:       str				   =				AltDiffusionImgaImgPipeline.from_pretrained(
                A__	,    safety_checker=A__	,    )
            pipe.to(A__					)
            pipe.set_progress_bar_config(disable=A__					)
            pipe.enable_attention_slicing()

            __lowerCamelCase						:       Dict				   =				"""A fantasy landscape, trending on artstation"""

            __lowerCamelCase						:       str				   =				torch.manual_seed(0					)
            __lowerCamelCase						:       List[str]				   =				pipe(
                prompt=A__	,    image=A__	,    strength=0.75	,    guidance_scale=7.5	,    generator=A__	,    output_type="""np"""	,    )
            __lowerCamelCase						:       Optional[Any]				   =				output.images[0]

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

            assert image.shape == (504, 760, 3)
            __lowerCamelCase						:       Dict				   =				np.array([0.9358, 0.9397, 0.9599, 0.9901, 1.0000, 1.0000, 0.9882, 1.0000, 1.0000]					)

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







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




       def 					a_					(					self      : Any					):


            """simple docstring"""







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




       def 					a_					(					self      : Union[str, Any]					):


            """simple docstring"""







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

            __lowerCamelCase						:       Dict				   =				"""BAAI/AltDiffusion"""
            __lowerCamelCase						:       List[str]				   =				AltDiffusionImgaImgPipeline.from_pretrained(
                A__	,    safety_checker=A__	,    )
            pipe.to(A__					)
            pipe.set_progress_bar_config(disable=A__					)
            pipe.enable_attention_slicing()

            __lowerCamelCase						:       int				   =				"""A fantasy landscape, trending on artstation"""

            __lowerCamelCase						:       Optional[Any]				   =				torch.manual_seed(0					)
            __lowerCamelCase						:       str				   =				pipe(
                prompt=A__	,    image=A__	,    strength=0.75	,    guidance_scale=7.5	,    generator=A__	,    output_type="""np"""	,    )
            __lowerCamelCase						:       List[Any]				   =				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'''

import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase__						:Optional[int]          =			logging.get_logger(__name__)

UpperCAmelCase__						:List[Any]          =			{
    """asapp/sew-d-tiny-100k""": """https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json""",
    # See all SEW-D models at https://huggingface.co/models?filter=sew-d
}







class 	SCREAMING_SNAKE_CASE	(      lowerCAmelCase_  ):
       snake_case__    :     Dict   		=			'sew-d'




       def __init__(					self      : Tuple	,    A__      : Optional[int]=32	,    A__      : Optional[int]=768	,    A__      : Any=12	,    A__      : List[str]=12	,    A__      : List[Any]=3072	,    A__      : str=2	,    A__      : Dict=512	,    A__      : Optional[Any]=256	,    A__      : Optional[Any]=True	,    A__      : Any=True	,    A__      : List[str]=("p2c", "c2p")	,    A__      : List[Any]="layer_norm"	,    A__      : Union[str, Any]="gelu_python"	,    A__      : int=0.1	,    A__      : Dict=0.1	,    A__      : int=0.1	,    A__      : Dict=0.0	,    A__      : Optional[Any]=0.1	,    A__      : Dict=0.02	,    A__      : Dict=1e-7	,    A__      : List[Any]=1e-5	,    A__      : Any="group"	,    A__      : Any="gelu"	,    A__      : Dict=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)	,    A__      : Dict=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)	,    A__      : str=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)	,    A__      : Optional[Any]=False	,    A__      : Any=128	,    A__      : Optional[Any]=16	,    A__      : Union[str, Any]=True	,    A__      : Any=0.05	,    A__      : List[str]=10	,    A__      : Union[str, Any]=2	,    A__      : Dict=0.0	,    A__      : str=10	,    A__      : Tuple=0	,    A__      : Any="mean"	,    A__      : Optional[int]=False	,    A__      : int=False	,    A__      : List[str]=256	,    A__      : Union[str, Any]=0	,    A__      : int=1	,    A__      : Optional[Any]=2	,    **A__      : List[Any]	,    ):


            """simple docstring"""







            super().__init__(**A__	,    pad_token_id=A__	,    bos_token_id=A__	,    eos_token_id=A__					)
            __lowerCamelCase						:       List[str]				   =				hidden_size
            __lowerCamelCase						:       Tuple				   =				feat_extract_norm
            __lowerCamelCase						:       Tuple				   =				feat_extract_activation
            __lowerCamelCase						:       List[Any]				   =				list(A__					)
            __lowerCamelCase						:       int				   =				list(A__					)
            __lowerCamelCase						:       Optional[Any]				   =				list(A__					)
            __lowerCamelCase						:       Tuple				   =				conv_bias
            __lowerCamelCase						:       List[str]				   =				num_conv_pos_embeddings
            __lowerCamelCase						:       Tuple				   =				num_conv_pos_embedding_groups
            __lowerCamelCase						:       int				   =				len(self.conv_dim					)
            __lowerCamelCase						:       Optional[int]				   =				num_hidden_layers
            __lowerCamelCase						:       Dict				   =				intermediate_size
            __lowerCamelCase						:       Optional[int]				   =				squeeze_factor
            __lowerCamelCase						:       Union[str, Any]				   =				max_position_embeddings
            __lowerCamelCase						:       int				   =				position_buckets
            __lowerCamelCase						:       Tuple				   =				share_att_key
            __lowerCamelCase						:       Any				   =				relative_attention
            __lowerCamelCase						:       Any				   =				norm_rel_ebd
            __lowerCamelCase						:       Dict				   =				list(A__					)
            __lowerCamelCase						:       Optional[Any]				   =				hidden_act
            __lowerCamelCase						:       List[str]				   =				num_attention_heads
            __lowerCamelCase						:       List[str]				   =				hidden_dropout
            __lowerCamelCase						:       Union[str, Any]				   =				attention_dropout
            __lowerCamelCase						:       Tuple				   =				activation_dropout
            __lowerCamelCase						:       Union[str, Any]				   =				feat_proj_dropout
            __lowerCamelCase						:       Union[str, Any]				   =				final_dropout
            __lowerCamelCase						:       List[str]				   =				layer_norm_eps
            __lowerCamelCase						:       Tuple				   =				feature_layer_norm_eps
            __lowerCamelCase						:       Union[str, Any]				   =				initializer_range
            __lowerCamelCase						:       List[str]				   =				vocab_size

            if (
                (len(self.conv_stride					) != self.num_feat_extract_layers)
                or (len(self.conv_kernel					) != self.num_feat_extract_layers)
                or (len(self.conv_dim					) != self.num_feat_extract_layers)
            ):
                 raise ValueError(
                     """Configuration for convolutional layers is incorrect."""
                     """It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,"""
                     f"but is `len(config.conv_dim) = {len(self.conv_dim					)}`, `len(config.conv_stride)"
                     f"= {len(self.conv_stride					)}`, `len(config.conv_kernel) = {len(self.conv_kernel					)}`."					)

            # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
            __lowerCamelCase						:       Any				   =				apply_spec_augment
            __lowerCamelCase						:       str				   =				mask_time_prob
            __lowerCamelCase						:       Tuple				   =				mask_time_length
            __lowerCamelCase						:       Optional[int]				   =				mask_time_min_masks
            __lowerCamelCase						:       Dict				   =				mask_feature_prob
            __lowerCamelCase						:       Optional[Any]				   =				mask_feature_length
            __lowerCamelCase						:       str				   =				mask_feature_min_masks

            # ctc loss
            __lowerCamelCase						:       Any				   =				ctc_loss_reduction
            __lowerCamelCase						:       str				   =				ctc_zero_infinity

            # sequence classification
            __lowerCamelCase						:       Dict				   =				use_weighted_layer_sum
            __lowerCamelCase						:       List[Any]				   =				classifier_proj_size




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


            """simple docstring"""







            return functools.reduce(operator.mul	,    self.conv_stride	,    1					)



