





import tempfile

import torch

from diffusers import (
    DEISMultistepScheduler,
    DPMSolverMultistepScheduler,
    DPMSolverSinglestepScheduler,
    UniPCMultistepScheduler,
)

from .test_schedulers import SchedulerCommonTest





class 					A_					(   __snake_case						):







       """simple docstring"""





       SCREAMING_SNAKE_CASE_   :			int  			=	(DPMSolverSinglestepScheduler,)
       SCREAMING_SNAKE_CASE_   :			Dict  			=	(('''num_inference_steps''', 2_5),)







       def  __UpperCAmelCase			(     self						:						str    ,**__A						:						int	)    ->       List[str]:
         _lowercase						    =			{
             'num_train_timesteps': 1000,
             'beta_start': 0.0001,
             'beta_end': 0.02,
             'beta_schedule': 'linear',
             'solver_order': 2,
             'prediction_type': 'epsilon',
             'thresholding': False,
             'sample_max_value': 1.0,
             'algorithm_type': 'dpmsolver++',
             'solver_type': 'midpoint',
             'lambda_min_clipped': -float('inf'	),
             'variance_type': None,
         }

         config.update(**a__	)
         return config







       def  __UpperCAmelCase			(     self						:						Any    ,__A						:						int=0    ,**__A						:						str	)    ->       Optional[int]:
         _lowercase						    =			dict(self.forward_default_kwargs	)
         _lowercase						    =			kwargs.pop('num_inference_steps'    ,a__	)
         _lowercase						    =			self.dummy_sample
         _lowercase						    =			0.1 * sample
         _lowercase						    =			[residual + 0.2, residual + 0.15, residual + 0.10]

         for scheduler_class in self.scheduler_classes:
           _lowercase						    =			self.get_scheduler_config(**a__	)
           _lowercase						    =			scheduler_class(**a__	)
           scheduler.set_timesteps(a__	)
           # copy over dummy past residuals
           _lowercase						    =			dummy_past_residuals[: scheduler.config.solver_order]

           with tempfile.TemporaryDirectory() as tmpdirname:
             scheduler.save_config(a__	)
             _lowercase						    =			scheduler_class.from_pretrained(a__	)
             new_scheduler.set_timesteps(a__	)
             # copy over dummy past residuals
             _lowercase						    =			dummy_past_residuals[: new_scheduler.config.solver_order]

           _lowercase    ,		_lowercase						    =			sample, sample
           for t in range(a__    ,time_step + scheduler.config.solver_order + 1	):
             _lowercase						    =			scheduler.step(a__    ,a__    ,a__    ,**a__	).prev_sample
             _lowercase						    =			new_scheduler.step(a__    ,a__    ,a__    ,**a__	).prev_sample

             assert torch.sum(torch.abs(output - new_output	)	) < 1e-5, "Scheduler outputs are not identical"







       def  __UpperCAmelCase			(     self						:						int	)    ->       List[str]:
         pass







       def  __UpperCAmelCase			(     self						:						Optional[Any]    ,__A						:						Optional[int]=0    ,**__A						:						Dict	)    ->       Dict:
         _lowercase						    =			dict(self.forward_default_kwargs	)
         _lowercase						    =			kwargs.pop('num_inference_steps'    ,a__	)
         _lowercase						    =			self.dummy_sample
         _lowercase						    =			0.1 * sample
         _lowercase						    =			[residual + 0.2, residual + 0.15, residual + 0.10]

         for scheduler_class in self.scheduler_classes:
           _lowercase						    =			self.get_scheduler_config()
           _lowercase						    =			scheduler_class(**a__	)
           scheduler.set_timesteps(a__	)

           # copy over dummy past residuals (must be after setting timesteps)
           _lowercase						    =			dummy_past_residuals[: scheduler.config.solver_order]

           with tempfile.TemporaryDirectory() as tmpdirname:
             scheduler.save_config(a__	)
             _lowercase						    =			scheduler_class.from_pretrained(a__	)
             # copy over dummy past residuals
             new_scheduler.set_timesteps(a__	)

             # copy over dummy past residual (must be after setting timesteps)
             _lowercase						    =			dummy_past_residuals[: new_scheduler.config.solver_order]

           _lowercase						    =			scheduler.step(a__    ,a__    ,a__    ,**a__	).prev_sample
           _lowercase						    =			new_scheduler.step(a__    ,a__    ,a__    ,**a__	).prev_sample

           assert torch.sum(torch.abs(output - new_output	)	) < 1e-5, "Scheduler outputs are not identical"







       def  __UpperCAmelCase			(     self						:						List[str]    ,__A						:						Tuple=None    ,**__A						:						Tuple	)    ->       List[Any]:
         if scheduler is None:
           _lowercase						    =			self.scheduler_classes[0]
           _lowercase						    =			self.get_scheduler_config(**a__	)
           _lowercase						    =			scheduler_class(**a__	)

         _lowercase						    =			self.scheduler_classes[0]
         _lowercase						    =			self.get_scheduler_config(**a__	)
         _lowercase						    =			scheduler_class(**a__	)

         _lowercase						    =			10
         _lowercase						    =			self.dummy_model()
         _lowercase						    =			self.dummy_sample_deter
         scheduler.set_timesteps(a__	)

         for i, t in enumerate(scheduler.timesteps	):
           _lowercase						    =			model(a__    ,a__	)
           _lowercase						    =			scheduler.step(a__    ,a__    ,a__	).prev_sample

         return sample







       def  __UpperCAmelCase			(     self						:						int	)    ->       Optional[Any]:
         _lowercase						    =			DPMSolverSinglestepScheduler(**self.get_scheduler_config()	)
         _lowercase						    =			50
         _lowercase						    =			self.dummy_model()
         _lowercase						    =			self.dummy_sample_deter
         scheduler.set_timesteps(a__	)

         # make sure that the first t is uneven
         for i, t in enumerate(scheduler.timesteps[3:]	):
           _lowercase						    =			model(a__    ,a__	)
           _lowercase						    =			scheduler.step(a__    ,a__    ,a__	).prev_sample

         _lowercase						    =			torch.mean(torch.abs(a__	)	)

         assert abs(result_mean.item() - 0.2574	) < 1e-3







       def  __UpperCAmelCase			(     self						:						Union[str, Any]	)    ->       Tuple:
         for timesteps in [25, 50, 100, 999, 1000]:
           self.check_over_configs(num_train_timesteps=a__	)







       def  __UpperCAmelCase			(     self						:						Union[str, Any]	)    ->       Any:
         # make sure that iterating over schedulers with same config names gives same results
         # for defaults
         _lowercase						    =			DPMSolverSinglestepScheduler(**self.get_scheduler_config()	)
         _lowercase						    =			self.full_loop(scheduler=a__	)
         _lowercase						    =			torch.mean(torch.abs(a__	)	)

         assert abs(result_mean.item() - 0.2791	) < 1e-3

         _lowercase						    =			DEISMultistepScheduler.from_config(scheduler.config	)
         _lowercase						    =			DPMSolverMultistepScheduler.from_config(scheduler.config	)
         _lowercase						    =			UniPCMultistepScheduler.from_config(scheduler.config	)
         _lowercase						    =			DPMSolverSinglestepScheduler.from_config(scheduler.config	)

         _lowercase						    =			self.full_loop(scheduler=a__	)
         _lowercase						    =			torch.mean(torch.abs(a__	)	)

         assert abs(result_mean.item() - 0.2791	) < 1e-3







       def  __UpperCAmelCase			(     self						:						Dict	)    ->       int:
         self.check_over_configs(thresholding=a__	)
         for order in [1, 2, 3]:
           for solver_type in ["midpoint", "heun"]:
             for threshold in [0.5, 1.0, 2.0]:
               for prediction_type in ["epsilon", "sample"]:
                 self.check_over_configs(
                     thresholding=a__    ,prediction_type=a__    ,sample_max_value=a__    ,algorithm_type='dpmsolver++'    ,solver_order=a__    ,solver_type=a__    ,)







       def  __UpperCAmelCase			(     self						:						Any	)    ->       Optional[Any]:
         for prediction_type in ["epsilon", "v_prediction"]:
           self.check_over_configs(prediction_type=a__	)







       def  __UpperCAmelCase			(     self						:						List[Any]	)    ->       List[str]:
         for algorithm_type in ["dpmsolver", "dpmsolver++"]:
           for solver_type in ["midpoint", "heun"]:
             for order in [1, 2, 3]:
               for prediction_type in ["epsilon", "sample"]:
                 self.check_over_configs(
                     solver_order=a__    ,solver_type=a__    ,prediction_type=a__    ,algorithm_type=a__    ,)
                 _lowercase						    =			self.full_loop(
                     solver_order=a__    ,solver_type=a__    ,prediction_type=a__    ,algorithm_type=a__    ,)
                 assert not torch.isnan(a__	).any(), "Samples have nan numbers"







       def  __UpperCAmelCase			(     self						:						Optional[Any]	)    ->       Dict:
         self.check_over_configs(lower_order_final=a__	)
         self.check_over_configs(lower_order_final=a__	)







       def  __UpperCAmelCase			(     self						:						List[Any]	)    ->       Dict:
         self.check_over_configs(lambda_min_clipped=-float('inf'	)	)
         self.check_over_configs(lambda_min_clipped=-5.1	)







       def  __UpperCAmelCase			(     self						:						Tuple	)    ->       Dict:
         self.check_over_configs(variance_type=a__	)
         self.check_over_configs(variance_type='learned_range'	)







       def  __UpperCAmelCase			(     self						:						Dict	)    ->       Any:
         for num_inference_steps in [1, 2, 3, 5, 10, 50, 100, 999, 1000]:
           self.check_over_forward(num_inference_steps=a__    ,time_step=0	)







       def  __UpperCAmelCase			(     self						:						Dict	)    ->       Union[str, Any]:
         _lowercase						    =			self.full_loop()
         _lowercase						    =			torch.mean(torch.abs(a__	)	)

         assert abs(result_mean.item() - 0.2791	) < 1e-3







       def  __UpperCAmelCase			(     self						:						List[str]	)    ->       int:
         _lowercase						    =			self.full_loop(use_karras_sigmas=a__	)
         _lowercase						    =			torch.mean(torch.abs(a__	)	)

         assert abs(result_mean.item() - 0.2248	) < 1e-3







       def  __UpperCAmelCase			(     self						:						Union[str, Any]	)    ->       Optional[Any]:
         _lowercase						    =			self.full_loop(prediction_type='v_prediction'	)
         _lowercase						    =			torch.mean(torch.abs(a__	)	)

         assert abs(result_mean.item() - 0.1453	) < 1e-3







       def  __UpperCAmelCase			(     self						:						List[Any]	)    ->       Tuple:
         _lowercase						    =			self.full_loop(prediction_type='v_prediction'    ,use_karras_sigmas=a__	)
         _lowercase						    =			torch.mean(torch.abs(a__	)	)

         assert abs(result_mean.item() - 0.0649	) < 1e-3







       def  __UpperCAmelCase			(     self						:						Optional[Any]	)    ->       Any:
         _lowercase						    =			self.scheduler_classes[0]
         _lowercase						    =			self.get_scheduler_config(thresholding=a__    ,dynamic_thresholding_ratio=0	)
         _lowercase						    =			scheduler_class(**a__	)

         _lowercase						    =			10
         _lowercase						    =			self.dummy_model()
         _lowercase						    =			self.dummy_sample_deter.half()
         scheduler.set_timesteps(a__	)

         for i, t in enumerate(scheduler.timesteps	):
           _lowercase						    =			model(a__    ,a__	)
           _lowercase						    =			scheduler.step(a__    ,a__    ,a__	).prev_sample

         assert sample.dtype == torch.floataa



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


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

_lowercase							:   List[str]						=						{
    "speechbrain/m-ctc-t-large": "https://huggingface.co/speechbrain/m-ctc-t-large/resolve/main/config.json",
    # See all M-CTC-T models at https://huggingface.co/models?filter=mctct
}






class    _UpperCamelCase     (       __snake_case					):

				"""simple docstring"""







				lowerCAmelCase					=			'mctct'

				def __init__(    self					, a__=8065					, a__=1536					, a__=36					, a__=6144					, a__=4					, a__=384					, a__=920					, a__=1e-5					, a__=0.3					, a__="relu"					, a__=0.02					, a__=0.3					, a__=0.3					, a__=1					, a__=0					, a__=2					, a__=1					, a__=0.3					, a__=1					, a__=(7,)					, a__=(3,)					, a__=80					, a__=1					, a__=None					, a__="sum"					, a__=False					, **a__					, )						->					Optional[Any]:
					super().__init__(**a__					, pad_token_id=a__					, bos_token_id=a__					, eos_token_id=a__ )
					A             =					vocab_size
					A             =					hidden_size
					A             =					num_hidden_layers
					A             =					intermediate_size
					A             =					num_attention_heads
					A             =					attention_head_dim
					A             =					max_position_embeddings
					A             =					layer_norm_eps
					A             =					layerdrop
					A             =					hidden_act
					A             =					initializer_range
					A             =					hidden_dropout_prob
					A             =					attention_probs_dropout_prob
					A             =					pad_token_id
					A             =					bos_token_id
					A             =					eos_token_id
					A             =					conv_glu_dim
					A             =					conv_dropout
					A             =					num_conv_layers
					A             =					input_feat_per_channel
					A             =					input_channels
					A             =					conv_channels
					A             =					ctc_loss_reduction
					A             =					ctc_zero_infinity

					# prevents config testing fail with exporting to json
					A             =					list(a__ )
					A             =					list(a__ )

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



