





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


_UpperCAmelCase				:       Any	   =				logging.get_logger(__name__)

_UpperCAmelCase				:       str	   =				{
    """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    lowerCAmelCase							(					__UpperCamelCase			):
 UpperCAmelCase__													=		"""speech_to_text"""
 UpperCAmelCase__													=		["""past_key_values"""]
 UpperCAmelCase__													=		{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}
 def __init__(      self  :      List[Any]			,     UpperCAmelCase  :      str=10000			,     UpperCAmelCase  :      int=12			,     UpperCAmelCase  :      Optional[int]=2048			,     UpperCAmelCase  :      Optional[int]=4			,     UpperCAmelCase  :      List[Any]=6			,     UpperCAmelCase  :      Dict=2048			,     UpperCAmelCase  :      List[str]=4			,     UpperCAmelCase  :      Optional[int]=0.0			,     UpperCAmelCase  :      Optional[int]=0.0			,     UpperCAmelCase  :      Tuple=True			,     UpperCAmelCase  :      Dict=True			,     UpperCAmelCase  :      List[str]="relu"			,     UpperCAmelCase  :      Any=256			,     UpperCAmelCase  :      int=0.1			,     UpperCAmelCase  :      Tuple=0.0			,     UpperCAmelCase  :      List[str]=0.0			,     UpperCAmelCase  :      Tuple=0.0_2			,     UpperCAmelCase  :      Any=2			,     UpperCAmelCase  :      Optional[Any]=True			,     UpperCAmelCase  :      int=1			,     UpperCAmelCase  :      int=0			,     UpperCAmelCase  :      int=2			,     UpperCAmelCase  :      int=6000			,     UpperCAmelCase  :      Optional[Any]=1024			,     UpperCAmelCase  :      Any=2			,     UpperCAmelCase  :      int=(5, 5)			,     UpperCAmelCase  :      int=1024			,     UpperCAmelCase  :      Optional[int]=80			,     UpperCAmelCase  :      Tuple=1			,     **UpperCAmelCase  :      Dict			,     )			->	Any:
     lowerCamelCase__    :      Optional[int]					 =		vocab_size
     lowerCamelCase__    :      List[str]					 =		d_model
     lowerCamelCase__    :      int					 =		encoder_ffn_dim
     lowerCamelCase__    :      Optional[Any]					 =		encoder_layers
     lowerCamelCase__    :      Tuple					 =		encoder_attention_heads
     lowerCamelCase__    :      str					 =		decoder_ffn_dim
     lowerCamelCase__    :      Optional[Any]					 =		decoder_layers
     lowerCamelCase__    :      Dict					 =		decoder_attention_heads
     lowerCamelCase__    :      int					 =		dropout
     lowerCamelCase__    :      str					 =		attention_dropout
     lowerCamelCase__    :      Dict					 =		activation_dropout
     lowerCamelCase__    :      int					 =		activation_function
     lowerCamelCase__    :      Tuple					 =		init_std
     lowerCamelCase__    :      Optional[int]					 =		encoder_layerdrop
     lowerCamelCase__    :      Dict					 =		decoder_layerdrop
     lowerCamelCase__    :      Union[str, Any]					 =		use_cache
     lowerCamelCase__    :      Optional[Any]					 =		encoder_layers
     lowerCamelCase__    :      Any					 =		scale_embedding  # scale factor will be sqrt(d_model) if True
     lowerCamelCase__    :      Optional[int]					 =		max_source_positions
     lowerCamelCase__    :      Optional[int]					 =		max_target_positions
     lowerCamelCase__    :      int					 =		num_conv_layers
     lowerCamelCase__    :      Union[str, Any]					 =		list(UpperCAmelCase	)
     lowerCamelCase__    :      int					 =		conv_channels
     lowerCamelCase__    :      Dict					 =		input_feat_per_channel
     lowerCamelCase__    :      int					 =		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			,     )










'''simple docstring'''






import tempfile

import torch

from diffusers import IPNDMScheduler

from .test_schedulers import SchedulerCommonTest



class 							lowercase       (  _lowerCamelCase				):


 """simple docstring"""


 UpperCAmelCase							=  (IPNDMScheduler,)
 UpperCAmelCase							=  (("""num_inference_steps""", 50),)







 def _snake_case (							self  ,**a_							)		->   Optional[int]:
        _UpperCAmelCase  :						str			     = {"""num_train_timesteps""": 1_000}
        config.update(**a_							)
        return config







 def _snake_case (							self  ,a_=0  ,**a_							)		->   List[str]:
        _UpperCAmelCase  :						Any			     = dict(self.forward_default_kwargs							)
        _UpperCAmelCase  :						Any			     = kwargs.pop("""num_inference_steps"""  ,a_							)
        _UpperCAmelCase  :						Union[str, Any]			     = self.dummy_sample
        _UpperCAmelCase  :						Union[str, Any]			     = 0.1 * sample
        _UpperCAmelCase  :						List[str]			     = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]

        for scheduler_class in self.scheduler_classes:
               _UpperCAmelCase  :						str			     = self.get_scheduler_config(**a_							)
               _UpperCAmelCase  :						List[str]			     = scheduler_class(**a_							)
               scheduler.set_timesteps(a_							)
               # copy over dummy past residuals
               _UpperCAmelCase  :						str			     = dummy_past_residuals[:]

               if time_step is None:
                      _UpperCAmelCase  :						List[str]			     = scheduler.timesteps[len(scheduler.timesteps							) // 2]

               with tempfile.TemporaryDirectory() as tmpdirname:
                      scheduler.save_config(a_							)
                      _UpperCAmelCase  :						List[str]			     = scheduler_class.from_pretrained(a_							)
                      new_scheduler.set_timesteps(a_							)
                      # copy over dummy past residuals
                      _UpperCAmelCase  :						Tuple			     = dummy_past_residuals[:]

               _UpperCAmelCase  :						Dict			     = scheduler.step(a_  ,a_  ,a_  ,**a_							).prev_sample
               _UpperCAmelCase  :						Any			     = new_scheduler.step(a_  ,a_  ,a_  ,**a_							).prev_sample

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

               _UpperCAmelCase  :						str			     = scheduler.step(a_  ,a_  ,a_  ,**a_							).prev_sample
               _UpperCAmelCase  :						Any			     = 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 _snake_case (							self							)		->   Any:
        pass







 def _snake_case (							self  ,a_=0  ,**a_							)		->   Dict:
        _UpperCAmelCase  :						str			     = dict(self.forward_default_kwargs							)
        _UpperCAmelCase  :						Union[str, Any]			     = kwargs.pop("""num_inference_steps"""  ,a_							)
        _UpperCAmelCase  :						Optional[int]			     = self.dummy_sample
        _UpperCAmelCase  :						Tuple			     = 0.1 * sample
        _UpperCAmelCase  :						Optional[Any]			     = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]

        for scheduler_class in self.scheduler_classes:
               _UpperCAmelCase  :						Optional[Any]			     = self.get_scheduler_config()
               _UpperCAmelCase  :						Dict			     = scheduler_class(**a_							)
               scheduler.set_timesteps(a_							)

               # copy over dummy past residuals (must be after setting timesteps)
               _UpperCAmelCase  :						Any			     = dummy_past_residuals[:]

               if time_step is None:
                      _UpperCAmelCase  :						Optional[int]			     = scheduler.timesteps[len(scheduler.timesteps							) // 2]

               with tempfile.TemporaryDirectory() as tmpdirname:
                      scheduler.save_config(a_							)
                      _UpperCAmelCase  :						str			     = 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)
                      _UpperCAmelCase  :						Dict			     = dummy_past_residuals[:]

               _UpperCAmelCase  :						Tuple			     = scheduler.step(a_  ,a_  ,a_  ,**a_							).prev_sample
               _UpperCAmelCase  :						str			     = new_scheduler.step(a_  ,a_  ,a_  ,**a_							).prev_sample

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

               _UpperCAmelCase  :						str			     = scheduler.step(a_  ,a_  ,a_  ,**a_							).prev_sample
               _UpperCAmelCase  :						str			     = 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 _snake_case (							self  ,**a_							)		->   List[Any]:
        _UpperCAmelCase  :						int			     = self.scheduler_classes[0]
        _UpperCAmelCase  :						List[str]			     = self.get_scheduler_config(**a_							)
        _UpperCAmelCase  :						List[Any]			     = scheduler_class(**a_							)

        _UpperCAmelCase  :						List[Any]			     = 10
        _UpperCAmelCase  :						Dict			     = self.dummy_model()
        _UpperCAmelCase  :						Dict			     = self.dummy_sample_deter
        scheduler.set_timesteps(a_							)

        for i, t in enumerate(scheduler.timesteps							):
               _UpperCAmelCase  :						Optional[Any]			     = model(a_  ,a_							)
               _UpperCAmelCase  :						Tuple			     = scheduler.step(a_  ,a_  ,a_							).prev_sample

        for i, t in enumerate(scheduler.timesteps							):
               _UpperCAmelCase  :						List[str]			     = model(a_  ,a_							)
               _UpperCAmelCase  :						int			     = scheduler.step(a_  ,a_  ,a_							).prev_sample

        return sample







 def _snake_case (							self							)		->   Optional[Any]:
        _UpperCAmelCase  :						int			     = dict(self.forward_default_kwargs							)

        _UpperCAmelCase  :						Optional[Any]			     = kwargs.pop("""num_inference_steps"""  ,a_							)

        for scheduler_class in self.scheduler_classes:
               _UpperCAmelCase  :						int			     = self.get_scheduler_config()
               _UpperCAmelCase  :						List[str]			     = scheduler_class(**a_							)

               _UpperCAmelCase  :						Optional[int]			     = self.dummy_sample
               _UpperCAmelCase  :						Tuple			     = 0.1 * sample

               if num_inference_steps is not None and hasattr(a_  ,"""set_timesteps"""							):
                      scheduler.set_timesteps(a_							)
               elif num_inference_steps is not None and not hasattr(a_  ,"""set_timesteps"""							):
                      _UpperCAmelCase  :						Optional[Any]			     = num_inference_steps

               # copy over dummy past residuals (must be done after set_timesteps)
               _UpperCAmelCase  :						Union[str, Any]			     = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]
               _UpperCAmelCase  :						Optional[int]			     = dummy_past_residuals[:]

               _UpperCAmelCase  :						Tuple			     = scheduler.timesteps[5]
               _UpperCAmelCase  :						Optional[Any]			     = scheduler.timesteps[6]

               _UpperCAmelCase  :						Optional[Any]			     = scheduler.step(a_  ,a_  ,a_  ,**a_							).prev_sample
               _UpperCAmelCase  :						Optional[int]			     = scheduler.step(a_  ,a_  ,a_  ,**a_							).prev_sample

               self.assertEqual(output_a.shape  ,sample.shape							)
               self.assertEqual(output_a.shape  ,output_a.shape							)

               _UpperCAmelCase  :						Dict			     = scheduler.step(a_  ,a_  ,a_  ,**a_							).prev_sample
               _UpperCAmelCase  :						Any			     = scheduler.step(a_  ,a_  ,a_  ,**a_							).prev_sample

               self.assertEqual(output_a.shape  ,sample.shape							)
               self.assertEqual(output_a.shape  ,output_a.shape							)







 def _snake_case (							self							)		->   List[Any]:
        for timesteps in [100, 1_000]:
               self.check_over_configs(num_train_timesteps=a_  ,time_step=a_							)







 def _snake_case (							self							)		->   int:
        for t, num_inference_steps in zip([1, 5, 10]  ,[10, 50, 100]							):
               self.check_over_forward(num_inference_steps=a_  ,time_step=a_							)







 def _snake_case (							self							)		->   Optional[Any]:
        _UpperCAmelCase  :						Union[str, Any]			     = self.full_loop()
        _UpperCAmelCase  :						Any			     = torch.mean(torch.abs(a_							)							)

        assert abs(result_mean.item() - 2_540_529							) < 10



