
"""simple docstring"""



import functools
import operator

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


a					:	List[Any]							=		logging.get_logger(__name__)

a					:	Union[str, 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 		a_						( _UpperCAmelCase	):
    a      :				List[Any]     =				'sew-d'







    def __init__(			self			:       List[str]     ,					__UpperCamelCase			:       Dict=32     ,					__UpperCamelCase			:       List[Any]=7_68     ,					__UpperCamelCase			:       Dict=12     ,					__UpperCamelCase			:       Dict=12     ,					__UpperCamelCase			:       Optional[Any]=30_72     ,					__UpperCamelCase			:       str=2     ,					__UpperCamelCase			:       str=5_12     ,					__UpperCamelCase			:       Union[str, Any]=2_56     ,					__UpperCamelCase			:       Any=True     ,					__UpperCamelCase			:       List[str]=True     ,					__UpperCamelCase			:       Union[str, Any]=("p2c", "c2p")     ,					__UpperCamelCase			:       List[str]="layer_norm"     ,					__UpperCamelCase			:       Optional[int]="gelu_python"     ,					__UpperCamelCase			:       List[Any]=0.1     ,					__UpperCamelCase			:       Any=0.1     ,					__UpperCamelCase			:       List[Any]=0.1     ,					__UpperCamelCase			:       Union[str, Any]=0.0     ,					__UpperCamelCase			:       str=0.1     ,					__UpperCamelCase			:       Optional[Any]=0.0_2     ,					__UpperCamelCase			:       str=1e-7     ,					__UpperCamelCase			:       Optional[int]=1e-5     ,					__UpperCamelCase			:       List[Any]="group"     ,					__UpperCamelCase			:       List[Any]="gelu"     ,					__UpperCamelCase			:       int=(64, 1_28, 1_28, 1_28, 1_28, 2_56, 2_56, 2_56, 2_56, 5_12, 5_12, 5_12, 5_12)     ,					__UpperCamelCase			:       Any=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)     ,					__UpperCamelCase			:       Optional[Any]=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)     ,					__UpperCamelCase			:       Dict=False     ,					__UpperCamelCase			:       List[str]=1_28     ,					__UpperCamelCase			:       List[str]=16     ,					__UpperCamelCase			:       Optional[Any]=True     ,					__UpperCamelCase			:       Optional[Any]=0.0_5     ,					__UpperCamelCase			:       List[str]=10     ,					__UpperCamelCase			:       str=2     ,					__UpperCamelCase			:       Tuple=0.0     ,					__UpperCamelCase			:       List[str]=10     ,					__UpperCamelCase			:       Tuple=0     ,					__UpperCamelCase			:       Optional[int]="mean"     ,					__UpperCamelCase			:       List[Any]=False     ,					__UpperCamelCase			:       List[str]=False     ,					__UpperCamelCase			:       Tuple=2_56     ,					__UpperCamelCase			:       List[str]=0     ,					__UpperCamelCase			:       str=1     ,					__UpperCamelCase			:       List[Any]=2     ,					**__UpperCamelCase			:       Union[str, Any]     ,					)		->int:






        '''simple docstring'''






        super().__init__(**__UpperCamelCase     ,					pad_token_id=__UpperCamelCase     ,					bos_token_id=__UpperCamelCase     ,					eos_token_id=__UpperCamelCase     )
        _UpperCAmelCase          =   hidden_size
        _UpperCAmelCase          =   feat_extract_norm
        _UpperCAmelCase          =   feat_extract_activation
        _UpperCAmelCase          =   list(__UpperCamelCase     )
        _UpperCAmelCase          =   list(__UpperCamelCase     )
        _UpperCAmelCase          =   list(__UpperCamelCase     )
        _UpperCAmelCase          =   conv_bias
        _UpperCAmelCase          =   num_conv_pos_embeddings
        _UpperCAmelCase          =   num_conv_pos_embedding_groups
        _UpperCAmelCase          =   len(self.conv_dim     )
        _UpperCAmelCase          =   num_hidden_layers
        _UpperCAmelCase          =   intermediate_size
        _UpperCAmelCase          =   squeeze_factor
        _UpperCAmelCase          =   max_position_embeddings
        _UpperCAmelCase          =   position_buckets
        _UpperCAmelCase          =   share_att_key
        _UpperCAmelCase          =   relative_attention
        _UpperCAmelCase          =   norm_rel_ebd
        _UpperCAmelCase          =   list(__UpperCamelCase     )
        _UpperCAmelCase          =   hidden_act
        _UpperCAmelCase          =   num_attention_heads
        _UpperCAmelCase          =   hidden_dropout
        _UpperCAmelCase          =   attention_dropout
        _UpperCAmelCase          =   activation_dropout
        _UpperCAmelCase          =   feat_proj_dropout
        _UpperCAmelCase          =   final_dropout
        _UpperCAmelCase          =   layer_norm_eps
        _UpperCAmelCase          =   feature_layer_norm_eps
        _UpperCAmelCase          =   initializer_range
        _UpperCAmelCase          =   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
        _UpperCAmelCase          =   apply_spec_augment
        _UpperCAmelCase          =   mask_time_prob
        _UpperCAmelCase          =   mask_time_length
        _UpperCAmelCase          =   mask_time_min_masks
        _UpperCAmelCase          =   mask_feature_prob
        _UpperCAmelCase          =   mask_feature_length
        _UpperCAmelCase          =   mask_feature_min_masks

        # ctc loss
        _UpperCAmelCase          =   ctc_loss_reduction
        _UpperCAmelCase          =   ctc_zero_infinity

        # sequence classification
        _UpperCAmelCase          =   use_weighted_layer_sum
        _UpperCAmelCase          =   classifier_proj_size







    @property
    def      _snake_case					(			self			:       Any     )		->List[Any]:






        '''simple docstring'''






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

"""simple docstring"""



import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel

from diffusers import DDIMScheduler, LDMPipeline, UNetaDModel, VQModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()




class 		a_						( unittest.TestCase	):







    @property
    def      _snake_case					(			self			:       Dict     )		->int:






        '''simple docstring'''






        torch.manual_seed(0     )
        _UpperCAmelCase          =   UNetaDModel(
            block_out_channels=(32, 64)     ,					layers_per_block=2     ,					sample_size=32     ,					in_channels=3     ,					out_channels=3     ,					down_block_types=("""DownBlock2D""", """AttnDownBlock2D""")     ,					up_block_types=("""AttnUpBlock2D""", """UpBlock2D""")     ,					)
        return model







    @property
    def      _snake_case					(			self			:       Optional[Any]     )		->str:






        '''simple docstring'''






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







    @property
    def      _snake_case					(			self			:       List[Any]     )		->Optional[int]:






        '''simple docstring'''






        torch.manual_seed(0     )
        _UpperCAmelCase          =   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=10_00     ,					)
        return CLIPTextModel(__UpperCamelCase     )







    def      _snake_case					(			self			:       List[str]     )		->Optional[Any]:






        '''simple docstring'''






        _UpperCAmelCase          =   self.dummy_uncond_unet
        _UpperCAmelCase          =   DDIMScheduler()
        _UpperCAmelCase          =   self.dummy_vq_model

        _UpperCAmelCase          =   LDMPipeline(unet=__UpperCamelCase     ,					vqvae=__UpperCamelCase     ,					scheduler=__UpperCamelCase     )
        ldm.to(__UpperCamelCase     )
        ldm.set_progress_bar_config(disable=__UpperCamelCase     )

        _UpperCAmelCase          =   torch.manual_seed(0     )
        _UpperCAmelCase          =   ldm(generator=__UpperCamelCase     ,					num_inference_steps=2     ,					output_type="""numpy"""     ).images

        _UpperCAmelCase          =   torch.manual_seed(0     )
        _UpperCAmelCase          =   ldm(generator=__UpperCamelCase     ,					num_inference_steps=2     ,					output_type="""numpy"""     ,					return_dict=__UpperCamelCase     )[0]

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

        assert image.shape == (1, 64, 64, 3)
        _UpperCAmelCase          =   np.array([0.8_5_1_2, 0.8_1_8, 0.6_4_1_1, 0.6_8_0_8, 0.4_4_6_5, 0.5_6_1_8, 0.4_6, 0.6_2_3_1, 0.5_1_7_2]     )
        _UpperCAmelCase          =   1e-2 if torch_device != """mps""" else 3e-2

        assert np.abs(image_slice.flatten() - expected_slice     ).max() < tolerance
        assert np.abs(image_from_tuple_slice.flatten() - expected_slice     ).max() < tolerance







@slow
@require_torch
class 		a_						( unittest.TestCase	):







    def      _snake_case					(			self			:       List[Any]     )		->Optional[Any]:






        '''simple docstring'''






        _UpperCAmelCase          =   LDMPipeline.from_pretrained("""CompVis/ldm-celebahq-256"""     )
        ldm.to(__UpperCamelCase     )
        ldm.set_progress_bar_config(disable=__UpperCamelCase     )

        _UpperCAmelCase          =   torch.manual_seed(0     )
        _UpperCAmelCase          =   ldm(generator=__UpperCamelCase     ,					num_inference_steps=5     ,					output_type="""numpy"""     ).images

        _UpperCAmelCase          =   image[0, -3:, -3:, -1]

        assert image.shape == (1, 2_56, 2_56, 3)
        _UpperCAmelCase          =   np.array([0.4_3_9_9, 0.4_4_9_7_5, 0.4_6_8_2_5, 0.4_7_4, 0.4_3_5_9, 0.4_5_8_1, 0.4_5_0_9_5, 0.4_3_4_1, 0.4_4_4_7]     )
        _UpperCAmelCase          =   1e-2 if torch_device != """mps""" else 3e-2

        assert np.abs(image_slice.flatten() - expected_slice     ).max() < tolerance