


'''simple docstring'''




import torch

from diffusers import KDPMaDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest

class 		UpperCAmelCase_      (  __lowercase		):
     lowerCamelCase				:	str		=     (KDPMaDiscreteScheduler,)
     lowerCamelCase				:	Optional[int]		=     10





     def 							__UpperCAmelCase      ( self       :      List[Any]    ,      **UpperCAmelCase__       :      Optional[int]    )				->  Any:
        lowerCAmelCase     =			{
            'num_train_timesteps': 1_1_0_0,
            'beta_start': 0.0_001,
            'beta_end': 0.02,
            'beta_schedule': 'linear',
        }

        config.update(**UpperCAmelCase__    )
        return config





     def 							__UpperCAmelCase      ( self       :      List[str]    )				->  Tuple:
        for timesteps in [1_0, 5_0, 1_0_0, 1_0_0_0]:
           self.check_over_configs(num_train_timesteps=UpperCAmelCase__    )





     def 							__UpperCAmelCase      ( self       :      Dict    )				->  Optional[Any]:
        for beta_start, beta_end in zip([0.00_001, 0.0_001, 0.001]    ,      [0.0_002, 0.002, 0.02]    ):
           self.check_over_configs(beta_start=UpperCAmelCase__    ,      beta_end=UpperCAmelCase__    )





     def 							__UpperCAmelCase      ( self       :      Optional[Any]    )				->  int:
        for schedule in ["linear", "scaled_linear"]:
           self.check_over_configs(beta_schedule=UpperCAmelCase__    )





     def 							__UpperCAmelCase      ( self       :      Union[str, Any]    )				->  List[str]:
        for prediction_type in ["epsilon", "v_prediction"]:
           self.check_over_configs(prediction_type=UpperCAmelCase__    )





     def 							__UpperCAmelCase      ( self       :      int    )				->  Dict:
        lowerCAmelCase     =			self.scheduler_classes[0]
        lowerCAmelCase     =			self.get_scheduler_config(prediction_type='v_prediction'    )
        lowerCAmelCase     =			scheduler_class(**UpperCAmelCase__    )

        scheduler.set_timesteps(self.num_inference_steps    )

        lowerCAmelCase     =			self.dummy_model()
        lowerCAmelCase     =			self.dummy_sample_deter * scheduler.init_noise_sigma
        lowerCAmelCase     =			sample.to(UpperCAmelCase__    )

        for i, t in enumerate(scheduler.timesteps    ):
           lowerCAmelCase     =			scheduler.scale_model_input(UpperCAmelCase__    ,      UpperCAmelCase__    )

           lowerCAmelCase     =			model(UpperCAmelCase__    ,      UpperCAmelCase__    )

           lowerCAmelCase     =			scheduler.step(UpperCAmelCase__    ,      UpperCAmelCase__    ,      UpperCAmelCase__    )
           lowerCAmelCase     =			output.prev_sample

        lowerCAmelCase     =			torch.sum(torch.abs(UpperCAmelCase__    )    )
        lowerCAmelCase     =			torch.mean(torch.abs(UpperCAmelCase__    )    )

        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.0_002    ) < 1E-3





     def 							__UpperCAmelCase      ( self       :      Tuple    )				->  Dict:
        if torch_device == "mps":
           return
        lowerCAmelCase     =			self.scheduler_classes[0]
        lowerCAmelCase     =			self.get_scheduler_config()
        lowerCAmelCase     =			scheduler_class(**UpperCAmelCase__    )

        scheduler.set_timesteps(self.num_inference_steps    )

        lowerCAmelCase     =			self.dummy_model()
        lowerCAmelCase     =			self.dummy_sample_deter * scheduler.init_noise_sigma
        lowerCAmelCase     =			sample.to(UpperCAmelCase__    )

        for i, t in enumerate(scheduler.timesteps    ):
           lowerCAmelCase     =			scheduler.scale_model_input(UpperCAmelCase__    ,      UpperCAmelCase__    )

           lowerCAmelCase     =			model(UpperCAmelCase__    ,      UpperCAmelCase__    )

           lowerCAmelCase     =			scheduler.step(UpperCAmelCase__    ,      UpperCAmelCase__    ,      UpperCAmelCase__    )
           lowerCAmelCase     =			output.prev_sample

        lowerCAmelCase     =			torch.sum(torch.abs(UpperCAmelCase__    )    )
        lowerCAmelCase     =			torch.mean(torch.abs(UpperCAmelCase__    )    )

        if torch_device in ["cpu", "mps"]:
           assert abs(result_sum.item() - 20.4_125    ) < 1E-2
           assert abs(result_mean.item() - 0.0_266    ) < 1E-3
        else:
           # CUDA
           assert abs(result_sum.item() - 20.4_125    ) < 1E-2
           assert abs(result_mean.item() - 0.0_266    ) < 1E-3





     def 							__UpperCAmelCase      ( self       :      List[Any]    )				->  Optional[Any]:
        if torch_device == "mps":
           return
        lowerCAmelCase     =			self.scheduler_classes[0]
        lowerCAmelCase     =			self.get_scheduler_config()
        lowerCAmelCase     =			scheduler_class(**UpperCAmelCase__    )

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

        lowerCAmelCase     =			self.dummy_model()
        lowerCAmelCase     =			self.dummy_sample_deter.to(UpperCAmelCase__    ) * scheduler.init_noise_sigma

        for t in scheduler.timesteps:
           lowerCAmelCase     =			scheduler.scale_model_input(UpperCAmelCase__    ,      UpperCAmelCase__    )

           lowerCAmelCase     =			model(UpperCAmelCase__    ,      UpperCAmelCase__    )

           lowerCAmelCase     =			scheduler.step(UpperCAmelCase__    ,      UpperCAmelCase__    ,      UpperCAmelCase__    )
           lowerCAmelCase     =			output.prev_sample

        lowerCAmelCase     =			torch.sum(torch.abs(UpperCAmelCase__    )    )
        lowerCAmelCase     =			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() - 20.4_125    ) < 1E-2
           assert abs(result_mean.item() - 0.0_266    ) < 1E-3
        else:
           # CUDA
           assert abs(result_sum.item() - 20.4_125    ) < 1E-2
           assert abs(result_mean.item() - 0.0_266    ) < 1E-3









'''simple docstring'''




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


__snake_case    						=logging.get_logger(__name__)

__snake_case    						={
    """facebook/s2t-wav2vec2-large-en-de""": (
        """https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json"""
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2
}

class 		UpperCAmelCase_      (  __lowercase		):
     lowerCamelCase				:	Union[str, Any]		=     '''speech_to_text_2'''
     lowerCamelCase				:	Any		=     ['''past_key_values''']
     lowerCamelCase				:	Optional[Any]		=     {'''num_attention_heads''': '''decoder_attention_heads''', '''hidden_size''': '''d_model'''}





     def __init__( self       :      Optional[int]    ,      UpperCAmelCase__       :      Optional[Any]=1_0_0_0_0    ,      UpperCAmelCase__       :      int=6    ,      UpperCAmelCase__       :      Optional[Any]=2_0_4_8    ,      UpperCAmelCase__       :      Union[str, Any]=4    ,      UpperCAmelCase__       :      List[Any]=0.0    ,      UpperCAmelCase__       :      str=True    ,      UpperCAmelCase__       :      str="relu"    ,      UpperCAmelCase__       :      Any=2_5_6    ,      UpperCAmelCase__       :      Optional[Any]=0.1    ,      UpperCAmelCase__       :      Any=0.0    ,      UpperCAmelCase__       :      Tuple=0.0    ,      UpperCAmelCase__       :      List[Any]=0.02    ,      UpperCAmelCase__       :      List[Any]=2    ,      UpperCAmelCase__       :      str=True    ,      UpperCAmelCase__       :      List[str]=1    ,      UpperCAmelCase__       :      Any=0    ,      UpperCAmelCase__       :      Dict=2    ,      UpperCAmelCase__       :      int=1_0_2_4    ,      **UpperCAmelCase__       :      Optional[Any]    ,      )				->  Dict:
        lowerCAmelCase     =			vocab_size
        lowerCAmelCase     =			d_model
        lowerCAmelCase     =			decoder_ffn_dim
        lowerCAmelCase     =			decoder_layers
        lowerCAmelCase     =			decoder_attention_heads
        lowerCAmelCase     =			dropout
        lowerCAmelCase     =			attention_dropout
        lowerCAmelCase     =			activation_dropout
        lowerCAmelCase     =			activation_function
        lowerCAmelCase     =			init_std
        lowerCAmelCase     =			decoder_layerdrop
        lowerCAmelCase     =			use_cache
        lowerCAmelCase     =			decoder_layers
        lowerCAmelCase     =			scale_embedding  # scale factor will be sqrt(d_model) if True
        lowerCAmelCase     =			max_target_positions

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





