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


_lowerCamelCase					:   str							=		logging.get_logger(__name__)

_lowerCamelCase					:   Tuple							=		{
    '''facebook/s2t-small-librispeech-asr''': (
        '''https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/config.json'''
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech_to_text
}


class 						lowercase    (     a     ):
							lowercase__						:       Any								=							"""speech_to_text"""
							lowercase__						:       Tuple								=							["""past_key_values"""]
							lowercase__						:       Union[str, Any]								=							{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}




							def __init__(						self   :					str	,							_UpperCamelCase   :					Tuple=10_000	,							_UpperCamelCase   :					Any=12	,							_UpperCamelCase   :					Dict=2_048	,							_UpperCamelCase   :					Optional[Any]=4	,							_UpperCamelCase   :					Any=6	,							_UpperCamelCase   :					Optional[Any]=2_048	,							_UpperCamelCase   :					List[str]=4	,							_UpperCamelCase   :					Optional[Any]=0.0	,							_UpperCamelCase   :					List[Any]=0.0	,							_UpperCamelCase   :					Union[str, Any]=True	,							_UpperCamelCase   :					int=True	,							_UpperCamelCase   :					List[Any]="relu"	,							_UpperCamelCase   :					List[str]=256	,							_UpperCamelCase   :					List[Any]=0.1	,							_UpperCamelCase   :					Dict=0.0	,							_UpperCamelCase   :					str=0.0	,							_UpperCamelCase   :					List[str]=0.0_2	,							_UpperCamelCase   :					Any=2	,							_UpperCamelCase   :					List[str]=True	,							_UpperCamelCase   :					List[str]=1	,							_UpperCamelCase   :					List[str]=0	,							_UpperCamelCase   :					Optional[Any]=2	,							_UpperCamelCase   :					Optional[int]=6_000	,							_UpperCamelCase   :					Dict=1_024	,							_UpperCamelCase   :					Dict=2	,							_UpperCamelCase   :					List[Any]=(5, 5)	,							_UpperCamelCase   :					str=1_024	,							_UpperCamelCase   :					Dict=80	,							_UpperCamelCase   :					int=1	,							**_UpperCamelCase   :					Tuple	,							)					->				Optional[Any]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE    			=       vocab_size
												SCREAMING_SNAKE_CASE    			=       d_model
												SCREAMING_SNAKE_CASE    			=       encoder_ffn_dim
												SCREAMING_SNAKE_CASE    			=       encoder_layers
												SCREAMING_SNAKE_CASE    			=       encoder_attention_heads
												SCREAMING_SNAKE_CASE    			=       decoder_ffn_dim
												SCREAMING_SNAKE_CASE    			=       decoder_layers
												SCREAMING_SNAKE_CASE    			=       decoder_attention_heads
												SCREAMING_SNAKE_CASE    			=       dropout
												SCREAMING_SNAKE_CASE    			=       attention_dropout
												SCREAMING_SNAKE_CASE    			=       activation_dropout
												SCREAMING_SNAKE_CASE    			=       activation_function
												SCREAMING_SNAKE_CASE    			=       init_std
												SCREAMING_SNAKE_CASE    			=       encoder_layerdrop
												SCREAMING_SNAKE_CASE    			=       decoder_layerdrop
												SCREAMING_SNAKE_CASE    			=       use_cache
												SCREAMING_SNAKE_CASE    			=       encoder_layers
												SCREAMING_SNAKE_CASE    			=       scale_embedding  # scale factor will be sqrt(d_model) if True
												SCREAMING_SNAKE_CASE    			=       max_source_positions
												SCREAMING_SNAKE_CASE    			=       max_target_positions
												SCREAMING_SNAKE_CASE    			=       num_conv_layers
												SCREAMING_SNAKE_CASE    			=       list(_UpperCamelCase      )
												SCREAMING_SNAKE_CASE    			=       conv_channels
												SCREAMING_SNAKE_CASE    			=       input_feat_per_channel
												SCREAMING_SNAKE_CASE    			=       input_channels

												if len(self.conv_kernel_sizes      ) != self.num_conv_layers:
																	raise ValueError(
																	    "Configuration for convolutional module is incorrect. "
																	    "It is required that `len(config.conv_kernel_sizes)` == `config.num_conv_layers` "
																	    F"but is `len(config.conv_kernel_sizes) = {len(self.conv_kernel_sizes      )}`, "
																	    F"`config.num_conv_layers = {self.num_conv_layers}`."      )

												super().__init__(
												    pad_token_id=_UpperCamelCase	,							bos_token_id=_UpperCamelCase	,							eos_token_id=_UpperCamelCase	,							is_encoder_decoder=_UpperCamelCase	,							decoder_start_token_id=_UpperCamelCase	,							**_UpperCamelCase	,							)





import torch

from diffusers import KDPMaDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest


class 						lowercase    (     a     ):
							lowercase__						:       Tuple								=							(KDPMaDiscreteScheduler,)
							lowercase__						:       Optional[int]								=							10




							def 	__snake_case(						self   :					Optional[Any]	,							**_UpperCamelCase   :					List[Any]      )					->				Tuple:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE    			=       {
												    "num_train_timesteps": 1_100,
												    "beta_start": 0.0_0_0_1,
												    "beta_end": 0.0_2,
												    "beta_schedule": "linear",
												}

												config.update(**_UpperCamelCase      )
												return config




							def 	__snake_case(						self   :					int      )					->				List[Any]:



												'''simple docstring'''

												for timesteps in [10, 50, 100, 1_000]:
																	self.check_over_configs(num_train_timesteps=_UpperCamelCase      )




							def 	__snake_case(						self   :					Union[str, Any]      )					->				Union[str, Any]:



												'''simple docstring'''

												for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1]	,							[0.0_0_0_2, 0.0_0_2, 0.0_2]      ):
																	self.check_over_configs(beta_start=_UpperCamelCase	,							beta_end=_UpperCamelCase      )




							def 	__snake_case(						self   :					Union[str, Any]      )					->				str:



												'''simple docstring'''

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




							def 	__snake_case(						self   :					Optional[int]      )					->				int:



												'''simple docstring'''

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




							def 	__snake_case(						self   :					Optional[int]      )					->				Any:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE    			=       self.scheduler_classes[0]
												SCREAMING_SNAKE_CASE    			=       self.get_scheduler_config(prediction_type="v_prediction"      )
												SCREAMING_SNAKE_CASE    			=       scheduler_class(**_UpperCamelCase      )

												scheduler.set_timesteps(self.num_inference_steps      )

												SCREAMING_SNAKE_CASE    			=       self.dummy_model()
												SCREAMING_SNAKE_CASE    			=       self.dummy_sample_deter * scheduler.init_noise_sigma
												SCREAMING_SNAKE_CASE    			=       sample.to(_UpperCamelCase      )

												for i, t in enumerate(scheduler.timesteps      ):
																	SCREAMING_SNAKE_CASE    			=       scheduler.scale_model_input(_UpperCamelCase	,							_UpperCamelCase      )

																	SCREAMING_SNAKE_CASE    			=       model(_UpperCamelCase	,							_UpperCamelCase      )

																	SCREAMING_SNAKE_CASE    			=       scheduler.step(_UpperCamelCase	,							_UpperCamelCase	,							_UpperCamelCase      )
																	SCREAMING_SNAKE_CASE    			=       output.prev_sample

												SCREAMING_SNAKE_CASE    			=       torch.sum(torch.abs(_UpperCamelCase      )      )
												SCREAMING_SNAKE_CASE    			=       torch.mean(torch.abs(_UpperCamelCase      )      )

												if torch_device in ["cpu", "mps"]:
																	assert abs(result_sum.item() - 4.6_934e-07      ) < 1e-2
																	assert abs(result_mean.item() - 6.1_112e-10      ) < 1e-3
												else:
																	# CUDA
																	assert abs(result_sum.item() - 4.693_428_650_170_972e-07      ) < 1e-2
																	assert abs(result_mean.item() - 0.0_0_0_2      ) < 1e-3




							def 	__snake_case(						self   :					Dict      )					->				int:



												'''simple docstring'''

												if torch_device == "mps":
																	return
												SCREAMING_SNAKE_CASE    			=       self.scheduler_classes[0]
												SCREAMING_SNAKE_CASE    			=       self.get_scheduler_config()
												SCREAMING_SNAKE_CASE    			=       scheduler_class(**_UpperCamelCase      )

												scheduler.set_timesteps(self.num_inference_steps      )

												SCREAMING_SNAKE_CASE    			=       self.dummy_model()
												SCREAMING_SNAKE_CASE    			=       self.dummy_sample_deter * scheduler.init_noise_sigma
												SCREAMING_SNAKE_CASE    			=       sample.to(_UpperCamelCase      )

												for i, t in enumerate(scheduler.timesteps      ):
																	SCREAMING_SNAKE_CASE    			=       scheduler.scale_model_input(_UpperCamelCase	,							_UpperCamelCase      )

																	SCREAMING_SNAKE_CASE    			=       model(_UpperCamelCase	,							_UpperCamelCase      )

																	SCREAMING_SNAKE_CASE    			=       scheduler.step(_UpperCamelCase	,							_UpperCamelCase	,							_UpperCamelCase      )
																	SCREAMING_SNAKE_CASE    			=       output.prev_sample

												SCREAMING_SNAKE_CASE    			=       torch.sum(torch.abs(_UpperCamelCase      )      )
												SCREAMING_SNAKE_CASE    			=       torch.mean(torch.abs(_UpperCamelCase      )      )

												if torch_device in ["cpu", "mps"]:
																	assert abs(result_sum.item() - 2_0.4_1_2_5      ) < 1e-2
																	assert abs(result_mean.item() - 0.0_2_6_6      ) < 1e-3
												else:
																	# CUDA
																	assert abs(result_sum.item() - 2_0.4_1_2_5      ) < 1e-2
																	assert abs(result_mean.item() - 0.0_2_6_6      ) < 1e-3





							def 	__snake_case(						self   :					Tuple      )					->				str:



												'''simple docstring'''

												if torch_device == "mps":
																	return
												SCREAMING_SNAKE_CASE    			=       self.scheduler_classes[0]
												SCREAMING_SNAKE_CASE    			=       self.get_scheduler_config()
												SCREAMING_SNAKE_CASE    			=       scheduler_class(**_UpperCamelCase      )

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

												SCREAMING_SNAKE_CASE    			=       self.dummy_model()
												SCREAMING_SNAKE_CASE    			=       self.dummy_sample_deter.to(_UpperCamelCase      ) * scheduler.init_noise_sigma

												for t in scheduler.timesteps:
																	SCREAMING_SNAKE_CASE    			=       scheduler.scale_model_input(_UpperCamelCase	,							_UpperCamelCase      )

																	SCREAMING_SNAKE_CASE    			=       model(_UpperCamelCase	,							_UpperCamelCase      )

																	SCREAMING_SNAKE_CASE    			=       scheduler.step(_UpperCamelCase	,							_UpperCamelCase	,							_UpperCamelCase      )
																	SCREAMING_SNAKE_CASE    			=       output.prev_sample

												SCREAMING_SNAKE_CASE    			=       torch.sum(torch.abs(_UpperCamelCase      )      )
												SCREAMING_SNAKE_CASE    			=       torch.mean(torch.abs(_UpperCamelCase      )      )

												if str(_UpperCamelCase      ).startswith("cpu"      ):
																	# The following sum varies between 148 and 156 on mps. Why?
																	assert abs(result_sum.item() - 2_0.4_1_2_5      ) < 1e-2
																	assert abs(result_mean.item() - 0.0_2_6_6      ) < 1e-3
												else:
																	# CUDA
																	assert abs(result_sum.item() - 2_0.4_1_2_5      ) < 1e-2
																	assert abs(result_mean.item() - 0.0_2_6_6      ) < 1e-3




