



'''simple docstring'''




import functools
import operator

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


UpperCamelCase__ :			str												=				logging.get_logger(__name__)

UpperCamelCase__ :			Dict												=				{
    '''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						(							lowerCamelCase__					):


			'''simple docstring'''

			_A   :	Any    					=	'''sew-d'''

			def __init__(							self  :				List[str]							,  lowerCAmelCase__  :				List[str]=3_2							,  lowerCAmelCase__  :				List[str]=7_6_8							,  lowerCAmelCase__  :				Tuple=1_2							,  lowerCAmelCase__  :				int=1_2							,  lowerCAmelCase__  :				int=3_0_7_2							,  lowerCAmelCase__  :				Optional[Any]=2							,  lowerCAmelCase__  :				List[str]=5_1_2							,  lowerCAmelCase__  :				Optional[int]=2_5_6							,  lowerCAmelCase__  :				Any=True							,  lowerCAmelCase__  :				List[str]=True							,  lowerCAmelCase__  :				Tuple=("p2c", "c2p")							,  lowerCAmelCase__  :				Dict="layer_norm"							,  lowerCAmelCase__  :				Tuple="gelu_python"							,  lowerCAmelCase__  :				Optional[Any]=0.1							,  lowerCAmelCase__  :				Optional[int]=0.1							,  lowerCAmelCase__  :				int=0.1							,  lowerCAmelCase__  :				Optional[int]=0.0							,  lowerCAmelCase__  :				Union[str, Any]=0.1							,  lowerCAmelCase__  :				Tuple=0.02							,  lowerCAmelCase__  :				Tuple=1E-7							,  lowerCAmelCase__  :				List[str]=1E-5							,  lowerCAmelCase__  :				Optional[int]="group"							,  lowerCAmelCase__  :				str="gelu"							,  lowerCAmelCase__  :				Tuple=(6_4, 1_2_8, 1_2_8, 1_2_8, 1_2_8, 2_5_6, 2_5_6, 2_5_6, 2_5_6, 5_1_2, 5_1_2, 5_1_2, 5_1_2)							,  lowerCAmelCase__  :				int=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)							,  lowerCAmelCase__  :				Tuple=(1_0, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)							,  lowerCAmelCase__  :				Optional[Any]=False							,  lowerCAmelCase__  :				Any=1_2_8							,  lowerCAmelCase__  :				Optional[Any]=1_6							,  lowerCAmelCase__  :				List[str]=True							,  lowerCAmelCase__  :				str=0.05							,  lowerCAmelCase__  :				Any=1_0							,  lowerCAmelCase__  :				Tuple=2							,  lowerCAmelCase__  :				Union[str, Any]=0.0							,  lowerCAmelCase__  :				Tuple=1_0							,  lowerCAmelCase__  :				Tuple=0							,  lowerCAmelCase__  :				Any="mean"							,  lowerCAmelCase__  :				Union[str, Any]=False							,  lowerCAmelCase__  :				List[str]=False							,  lowerCAmelCase__  :				Dict=2_5_6							,  lowerCAmelCase__  :				List[Any]=0							,  lowerCAmelCase__  :				List[Any]=1							,  lowerCAmelCase__  :				Optional[Any]=2							,  **lowerCAmelCase__  :				Optional[Any]							,  ):





					"""simple docstring"""






					super().__init__(**lowerCAmelCase__							,  pad_token_id=lowerCAmelCase__							,  bos_token_id=lowerCAmelCase__							,  eos_token_id=lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	Any					 =	hidden_size
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	feat_extract_norm
					__SCREAMING_SNAKE_CASE :	Tuple					 =	feat_extract_activation
					__SCREAMING_SNAKE_CASE :	int					 =	list(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	int					 =	list(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	list(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	str					 =	conv_bias
					__SCREAMING_SNAKE_CASE :	str					 =	num_conv_pos_embeddings
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	num_conv_pos_embedding_groups
					__SCREAMING_SNAKE_CASE :	int					 =	len(self.conv_dim		)
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	num_hidden_layers
					__SCREAMING_SNAKE_CASE :	Any					 =	intermediate_size
					__SCREAMING_SNAKE_CASE :	Any					 =	squeeze_factor
					__SCREAMING_SNAKE_CASE :	Any					 =	max_position_embeddings
					__SCREAMING_SNAKE_CASE :	str					 =	position_buckets
					__SCREAMING_SNAKE_CASE :	Tuple					 =	share_att_key
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	relative_attention
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	norm_rel_ebd
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	list(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	hidden_act
					__SCREAMING_SNAKE_CASE :	Tuple					 =	num_attention_heads
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	hidden_dropout
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	attention_dropout
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	activation_dropout
					__SCREAMING_SNAKE_CASE :	int					 =	feat_proj_dropout
					__SCREAMING_SNAKE_CASE :	str					 =	final_dropout
					__SCREAMING_SNAKE_CASE :	Tuple					 =	layer_norm_eps
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	feature_layer_norm_eps
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	initializer_range
					__SCREAMING_SNAKE_CASE :	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
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	apply_spec_augment
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	mask_time_prob
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	mask_time_length
					__SCREAMING_SNAKE_CASE :	Tuple					 =	mask_time_min_masks
					__SCREAMING_SNAKE_CASE :	Any					 =	mask_feature_prob
					__SCREAMING_SNAKE_CASE :	Any					 =	mask_feature_length
					__SCREAMING_SNAKE_CASE :	Any					 =	mask_feature_min_masks

					# ctc loss
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	ctc_loss_reduction
					__SCREAMING_SNAKE_CASE :	Dict					 =	ctc_zero_infinity

					# sequence classification
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	use_weighted_layer_sum
					__SCREAMING_SNAKE_CASE :	int					 =	classifier_proj_size



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





					"""simple docstring"""






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






'''simple docstring'''




import torch

from diffusers import KDPMaDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest






class 		_UpperCamelCase						(							lowerCamelCase__					):


			'''simple docstring'''

			_A   :	Optional[int]    					=	(KDPMaDiscreteScheduler,)
			_A   :	Dict    					=	10

			def UpperCamelCase__      (							self  :				Optional[int]							,  **lowerCAmelCase__  :				List[Any]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Any					 =	{
					    """num_train_timesteps""": 1_1_0_0,
					    """beta_start""": 0.00_01,
					    """beta_end""": 0.02,
					    """beta_schedule""": """linear""",
					}

					config.update(**lowerCAmelCase__		)
					return config

			def UpperCamelCase__      (							self  :				int		):





					"""simple docstring"""






					for timesteps in [1_0, 5_0, 1_0_0, 1_0_0_0]:
							self.check_over_configs(num_train_timesteps=lowerCAmelCase__		)

			def UpperCamelCase__      (							self  :				str		):





					"""simple docstring"""






					for beta_start, beta_end in zip([0.0_00_01, 0.00_01, 0.0_01]							,  [0.00_02, 0.0_02, 0.02]		):
							self.check_over_configs(beta_start=lowerCAmelCase__							,  beta_end=lowerCAmelCase__		)

			def UpperCamelCase__      (							self  :				Tuple		):





					"""simple docstring"""






					for schedule in ["linear", "scaled_linear"]:
							self.check_over_configs(beta_schedule=lowerCAmelCase__		)

			def UpperCamelCase__      (							self  :				int		):





					"""simple docstring"""






					for prediction_type in ["epsilon", "v_prediction"]:
							self.check_over_configs(prediction_type=lowerCAmelCase__		)

			def UpperCamelCase__      (							self  :				List[str]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Any					 =	self.scheduler_classes[0]
					__SCREAMING_SNAKE_CASE :	Tuple					 =	self.get_scheduler_config(prediction_type="""v_prediction"""		)
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	scheduler_class(**lowerCAmelCase__		)

					scheduler.set_timesteps(self.num_inference_steps		)

					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	self.dummy_model()
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	self.dummy_sample_deter * scheduler.init_noise_sigma
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	sample.to(lowerCAmelCase__		)

					for i, t in enumerate(scheduler.timesteps		):
							__SCREAMING_SNAKE_CASE :	List[Any]					 =	scheduler.scale_model_input(lowerCAmelCase__							,  lowerCAmelCase__		)

							__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	model(lowerCAmelCase__							,  lowerCAmelCase__		)

							__SCREAMING_SNAKE_CASE :	Dict					 =	scheduler.step(lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__		)
							__SCREAMING_SNAKE_CASE :	List[str]					 =	output.prev_sample

					__SCREAMING_SNAKE_CASE :	List[Any]					 =	torch.sum(torch.abs(lowerCAmelCase__		)		)
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	torch.mean(torch.abs(lowerCAmelCase__		)		)

					if torch_device in ["cpu", "mps"]:
							assert abs(result_sum.item() - 4.69_34E-07		) < 1E-2
							assert abs(result_mean.item() - 6.11_12E-10		) < 1E-3
					else:
							# CUDA
							assert abs(result_sum.item() - 4.6_93_42_86_50_17_09_72E-07		) < 1E-2
							assert abs(result_mean.item() - 0.00_02		) < 1E-3

			def UpperCamelCase__      (							self  :				Optional[int]		):





					"""simple docstring"""






					if torch_device == "mps":
							return
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	self.scheduler_classes[0]
					__SCREAMING_SNAKE_CASE :	str					 =	self.get_scheduler_config()
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	scheduler_class(**lowerCAmelCase__		)

					scheduler.set_timesteps(self.num_inference_steps		)

					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	self.dummy_model()
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	self.dummy_sample_deter * scheduler.init_noise_sigma
					__SCREAMING_SNAKE_CASE :	str					 =	sample.to(lowerCAmelCase__		)

					for i, t in enumerate(scheduler.timesteps		):
							__SCREAMING_SNAKE_CASE :	Tuple					 =	scheduler.scale_model_input(lowerCAmelCase__							,  lowerCAmelCase__		)

							__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	model(lowerCAmelCase__							,  lowerCAmelCase__		)

							__SCREAMING_SNAKE_CASE :	Any					 =	scheduler.step(lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__		)
							__SCREAMING_SNAKE_CASE :	List[Any]					 =	output.prev_sample

					__SCREAMING_SNAKE_CASE :	int					 =	torch.sum(torch.abs(lowerCAmelCase__		)		)
					__SCREAMING_SNAKE_CASE :	int					 =	torch.mean(torch.abs(lowerCAmelCase__		)		)

					if torch_device in ["cpu", "mps"]:
							assert abs(result_sum.item() - 20.41_25		) < 1E-2
							assert abs(result_mean.item() - 0.02_66		) < 1E-3
					else:
							# CUDA
							assert abs(result_sum.item() - 20.41_25		) < 1E-2
							assert abs(result_mean.item() - 0.02_66		) < 1E-3



			def UpperCamelCase__      (							self  :				List[str]		):





					"""simple docstring"""






					if torch_device == "mps":
							return
					__SCREAMING_SNAKE_CASE :	Dict					 =	self.scheduler_classes[0]
					__SCREAMING_SNAKE_CASE :	Dict					 =	self.get_scheduler_config()
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	scheduler_class(**lowerCAmelCase__		)

					scheduler.set_timesteps(self.num_inference_steps							,  device=lowerCAmelCase__		)

					__SCREAMING_SNAKE_CASE :	List[Any]					 =	self.dummy_model()
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	self.dummy_sample_deter.to(lowerCAmelCase__		) * scheduler.init_noise_sigma

					for t in scheduler.timesteps:
							__SCREAMING_SNAKE_CASE :	Dict					 =	scheduler.scale_model_input(lowerCAmelCase__							,  lowerCAmelCase__		)

							__SCREAMING_SNAKE_CASE :	Any					 =	model(lowerCAmelCase__							,  lowerCAmelCase__		)

							__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	scheduler.step(lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__		)
							__SCREAMING_SNAKE_CASE :	str					 =	output.prev_sample

					__SCREAMING_SNAKE_CASE :	Dict					 =	torch.sum(torch.abs(lowerCAmelCase__		)		)
					__SCREAMING_SNAKE_CASE :	int					 =	torch.mean(torch.abs(lowerCAmelCase__		)		)

					if str(lowerCAmelCase__		).startswith("""cpu"""		):
							# The following sum varies between 148 and 156 on mps. Why?
							assert abs(result_sum.item() - 20.41_25		) < 1E-2
							assert abs(result_mean.item() - 0.02_66		) < 1E-3
					else:
							# CUDA
							assert abs(result_sum.item() - 20.41_25		) < 1E-2
							assert abs(result_mean.item() - 0.02_66		) < 1E-3