
'''simple docstring'''



import functools
import operator

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


_lowercase          =						logging.get_logger(__name__)

_lowercase          =						{
    """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     UpperCAmelCase_	(						_SCREAMING_SNAKE_CASE					):






						'''simple docstring'''






						_lowercase						: List[Any]										=  '''sew-d'''




						def __init__(   self       ,  _lowercase=32       ,  _lowercase=768       ,  _lowercase=12       ,  _lowercase=12       ,  _lowercase=3_072       ,  _lowercase=2       ,  _lowercase=512       ,  _lowercase=256       ,  _lowercase=True       ,  _lowercase=True       ,  _lowercase=("p2c", "c2p")       ,  _lowercase="layer_norm"       ,  _lowercase="gelu_python"       ,  _lowercase=0.1       ,  _lowercase=0.1       ,  _lowercase=0.1       ,  _lowercase=0.0       ,  _lowercase=0.1       ,  _lowercase=0.02       ,  _lowercase=1e-7       ,  _lowercase=1e-5       ,  _lowercase="group"       ,  _lowercase="gelu"       ,  _lowercase=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)       ,  _lowercase=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)       ,  _lowercase=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)       ,  _lowercase=False       ,  _lowercase=128       ,  _lowercase=16       ,  _lowercase=True       ,  _lowercase=0.05       ,  _lowercase=10       ,  _lowercase=2       ,  _lowercase=0.0       ,  _lowercase=10       ,  _lowercase=0       ,  _lowercase="mean"       ,  _lowercase=False       ,  _lowercase=False       ,  _lowercase=256       ,  _lowercase=0       ,  _lowercase=1       ,  _lowercase=2       ,  **_lowercase       ,  ):

												"""simple docstring"""



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

												# ctc loss
												_lowerCAmelCase    		=					ctc_loss_reduction
												_lowerCAmelCase    		=					ctc_zero_infinity

												# sequence classification
												_lowerCAmelCase    		=					use_weighted_layer_sum
												_lowerCAmelCase    		=					classifier_proj_size




						@property
						def    _lowercase      (   self ):

												"""simple docstring"""



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





import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


_lowerCamelCase    :		Any		      =    False


class 						lowercase    (							unittest.TestCase):
						'''simple docstring'''


						pass


@nightly
@require_torch_gpu
class 						lowercase    (							unittest.TestCase):
						'''simple docstring'''


						def       lowerCamelCase_				(  self    :				List[Any]	):



											'''simple docstring'''


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







						def       lowerCamelCase_				(  self    :				List[str]	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     Dict						=				VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion'       ,	torch_dtype=torch.floataa	)
											pipe.to(snake_case	)
											pipe.set_progress_bar_config(disable=snake_case	)

											SCREAMING_SNAKE_CASE							:     Dict						=				load_image(
											    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'	)

											SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				torch.manual_seed(0	)
											SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				pipe.dual_guided(
											    prompt='first prompt'       ,	image=snake_case       ,	text_to_image_strength=0.75       ,	generator=snake_case       ,	guidance_scale=7.5       ,	num_inference_steps=2       ,	output_type='numpy'       ,	).images

											with tempfile.TemporaryDirectory() as tmpdirname:
																pipe.save_pretrained(snake_case	)
																SCREAMING_SNAKE_CASE							:     Any						=				VersatileDiffusionPipeline.from_pretrained(snake_case       ,	torch_dtype=torch.floataa	)
											pipe.to(snake_case	)
											pipe.set_progress_bar_config(disable=snake_case	)

											SCREAMING_SNAKE_CASE							:     List[str]						=				generator.manual_seed(0	)
											SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				pipe.dual_guided(
											    prompt='first prompt'       ,	image=snake_case       ,	text_to_image_strength=0.75       ,	generator=snake_case       ,	guidance_scale=7.5       ,	num_inference_steps=2       ,	output_type='numpy'       ,	).images

											assert np.abs(image - new_image	).sum() < 1E-5, "Models don't have the same forward pass"







						def       lowerCamelCase_				(  self    :				Tuple	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     Dict						=				VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion'       ,	torch_dtype=torch.floataa	)
											pipe.to(snake_case	)
											pipe.set_progress_bar_config(disable=snake_case	)

											SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				'cyberpunk 2077'
											SCREAMING_SNAKE_CASE							:     Optional[int]						=				load_image(
											    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'	)
											SCREAMING_SNAKE_CASE							:     List[Any]						=				torch.manual_seed(0	)
											SCREAMING_SNAKE_CASE							:     Any						=				pipe.dual_guided(
											    prompt=snake_case       ,	image=snake_case       ,	text_to_image_strength=0.75       ,	generator=snake_case       ,	guidance_scale=7.5       ,	num_inference_steps=50       ,	output_type='numpy'       ,	).images

											SCREAMING_SNAKE_CASE							:     List[Any]						=				image[0, 253:256, 253:256, -1]

											assert image.shape == (1, 512, 512, 3)
											SCREAMING_SNAKE_CASE							:     Tuple						=				np.array([0.1448, 0.1619, 0.1741, 0.1086, 0.1147, 0.1128, 0.1199, 0.1165, 0.1001]	)

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

											SCREAMING_SNAKE_CASE							:     Any						=				'A painting of a squirrel eating a burger '
											SCREAMING_SNAKE_CASE							:     Any						=				torch.manual_seed(0	)
											SCREAMING_SNAKE_CASE							:     Tuple						=				pipe.text_to_image(
											    prompt=snake_case       ,	generator=snake_case       ,	guidance_scale=7.5       ,	num_inference_steps=50       ,	output_type='numpy'	).images

											SCREAMING_SNAKE_CASE							:     Dict						=				image[0, 253:256, 253:256, -1]

											assert image.shape == (1, 512, 512, 3)
											SCREAMING_SNAKE_CASE							:     List[Any]						=				np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778]	)

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

											SCREAMING_SNAKE_CASE							:     Optional[Any]						=				pipe.image_variation(snake_case       ,	generator=snake_case       ,	output_type='numpy'	).images

											SCREAMING_SNAKE_CASE							:     Optional[int]						=				image[0, 253:256, 253:256, -1]

											assert image.shape == (1, 512, 512, 3)
											SCREAMING_SNAKE_CASE							:     Optional[int]						=				np.array([0.3076, 0.3123, 0.3284, 0.3782, 0.3770, 0.3894, 0.4297, 0.4331, 0.4456]	)

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