import torch

from diffusers import HeunDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest



class 				__snake_case  (     __UpperCAmelCase						):


    '''simple docstring'''







    lowerCamelCase__						:		List[str]							=  (HeunDiscreteScheduler,)
    lowerCamelCase__						:		List[Any]							=  1_0



    def    lowercase_				(					self      ,							**A_    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= {
            '''num_train_timesteps''': 11_00,
            '''beta_start''': 0.0001,
            '''beta_end''': 0.02,
            '''beta_schedule''': '''linear''',
        }

        config.update(**lowercase_    )
        return config



    def    lowercase_				(					self    ):





        '''simple docstring'''

        for timesteps in [10, 50, 1_00, 10_00]:
            self.check_over_configs(num_train_timesteps=lowercase_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        for beta_start, beta_end in zip([0.00001, 0.0001, 0.001]      ,							[0.0002, 0.002, 0.02]    ):
            self.check_over_configs(beta_start=lowercase_      ,							beta_end=lowercase_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

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



    def    lowercase_				(					self    ):





        '''simple docstring'''

        for clip_sample_range in [1.0, 2.0, 3.0]:
            self.check_over_configs(clip_sample_range=lowercase_      ,							clip_sample=lowercase_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

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



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.scheduler_classes[0]
        SCREAMING_SNAKE_CASE__       							= self.get_scheduler_config()
        SCREAMING_SNAKE_CASE__       							= scheduler_class(**lowercase_    )

        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(lowercase_    )

        for i, t in enumerate(scheduler.timesteps    ):
            SCREAMING_SNAKE_CASE__       							= scheduler.scale_model_input(lowercase_      ,							lowercase_    )

            SCREAMING_SNAKE_CASE__       							= model(lowercase_      ,							lowercase_    )

            SCREAMING_SNAKE_CASE__       							= scheduler.step(lowercase_      ,							lowercase_      ,							lowercase_    )
            SCREAMING_SNAKE_CASE__       							= output.prev_sample

        SCREAMING_SNAKE_CASE__       							= torch.sum(torch.abs(lowercase_    )    )
        SCREAMING_SNAKE_CASE__       							= torch.mean(torch.abs(lowercase_    )    )

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



    def    lowercase_				(					self    ):





        '''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(**lowercase_    )

        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(lowercase_    )

        for i, t in enumerate(scheduler.timesteps    ):
            SCREAMING_SNAKE_CASE__       							= scheduler.scale_model_input(lowercase_      ,							lowercase_    )

            SCREAMING_SNAKE_CASE__       							= model(lowercase_      ,							lowercase_    )

            SCREAMING_SNAKE_CASE__       							= scheduler.step(lowercase_      ,							lowercase_      ,							lowercase_    )
            SCREAMING_SNAKE_CASE__       							= output.prev_sample

        SCREAMING_SNAKE_CASE__       							= torch.sum(torch.abs(lowercase_    )    )
        SCREAMING_SNAKE_CASE__       							= torch.mean(torch.abs(lowercase_    )    )

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



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.scheduler_classes[0]
        SCREAMING_SNAKE_CASE__       							= self.get_scheduler_config()
        SCREAMING_SNAKE_CASE__       							= scheduler_class(**lowercase_    )

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

        SCREAMING_SNAKE_CASE__       							= self.dummy_model()
        SCREAMING_SNAKE_CASE__       							= self.dummy_sample_deter.to(lowercase_    ) * scheduler.init_noise_sigma

        for t in scheduler.timesteps:
            SCREAMING_SNAKE_CASE__       							= scheduler.scale_model_input(lowercase_      ,							lowercase_    )

            SCREAMING_SNAKE_CASE__       							= model(lowercase_      ,							lowercase_    )

            SCREAMING_SNAKE_CASE__       							= scheduler.step(lowercase_      ,							lowercase_      ,							lowercase_    )
            SCREAMING_SNAKE_CASE__       							= output.prev_sample

        SCREAMING_SNAKE_CASE__       							= torch.sum(torch.abs(lowercase_    )    )
        SCREAMING_SNAKE_CASE__       							= torch.mean(torch.abs(lowercase_    )    )

        if str(lowercase_    ).startswith('''cpu'''    ):
            # The following sum varies between 148 and 156 on mps. Why?
            assert abs(result_sum.item() - 0.1233    ) < 1E-2
            assert abs(result_mean.item() - 0.0002    ) < 1E-3
        elif str(lowercase_    ).startswith('''mps'''    ):
            # Larger tolerance on mps
            assert abs(result_mean.item() - 0.0002    ) < 1E-2
        else:
            # CUDA
            assert abs(result_sum.item() - 0.1233    ) < 1E-2
            assert abs(result_mean.item() - 0.0002    ) < 1E-3




    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.scheduler_classes[0]
        SCREAMING_SNAKE_CASE__       							= self.get_scheduler_config()
        SCREAMING_SNAKE_CASE__       							= scheduler_class(**lowercase_      ,							use_karras_sigmas=lowercase_    )

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

        SCREAMING_SNAKE_CASE__       							= self.dummy_model()
        SCREAMING_SNAKE_CASE__       							= self.dummy_sample_deter.to(lowercase_    ) * scheduler.init_noise_sigma
        SCREAMING_SNAKE_CASE__       							= sample.to(lowercase_    )

        for t in scheduler.timesteps:
            SCREAMING_SNAKE_CASE__       							= scheduler.scale_model_input(lowercase_      ,							lowercase_    )

            SCREAMING_SNAKE_CASE__       							= model(lowercase_      ,							lowercase_    )

            SCREAMING_SNAKE_CASE__       							= scheduler.step(lowercase_      ,							lowercase_      ,							lowercase_    )
            SCREAMING_SNAKE_CASE__       							= output.prev_sample

        SCREAMING_SNAKE_CASE__       							= torch.sum(torch.abs(lowercase_    )    )
        SCREAMING_SNAKE_CASE__       							= torch.mean(torch.abs(lowercase_    )    )

        assert abs(result_sum.item() - 0.00015    ) < 1E-2
        assert abs(result_mean.item() - 1.9_869_554_535_034_695E-07    ) < 1E-2







import functools
import operator

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


_A				:							Any							= logging.get_logger(__name__)

_A				:							Any							= {
    """facebook/hubert-base-ls960""": """https://huggingface.co/facebook/hubert-base-ls960/resolve/main/config.json""",
    # See all Hubert models at https://huggingface.co/models?filter=hubert
}



class 				__snake_case  (     __SCREAMING_SNAKE_CASE						):


    '''simple docstring'''







    lowerCamelCase__						:		int							=  """hubert"""



    def __init__(					self      ,							A_=32      ,							A_=7_68      ,							A_=12      ,							A_=12      ,							A_=30_72      ,							A_="gelu"      ,							A_=0.1      ,							A_=0.1      ,							A_=0.1      ,							A_=True      ,							A_=0.0      ,							A_=0.1      ,							A_=0.1      ,							A_=0.02      ,							A_=1E-5      ,							A_="group"      ,							A_="gelu"      ,							A_=(5_12, 5_12, 5_12, 5_12, 5_12, 5_12, 5_12)      ,							A_=(5, 2, 2, 2, 2, 2, 2)      ,							A_=(10, 3, 3, 3, 3, 2, 2)      ,							A_=False      ,							A_=1_28      ,							A_=16      ,							A_=False      ,							A_=True      ,							A_=0.05      ,							A_=10      ,							A_=2      ,							A_=0.0      ,							A_=10      ,							A_=0      ,							A_="sum"      ,							A_=False      ,							A_=False      ,							A_=2_56      ,							A_=0      ,							A_=1      ,							A_=2      ,							**A_      ,							):





        '''simple docstring'''

        super().__init__(**A_      ,							pad_token_id=A_      ,							bos_token_id=A_      ,							eos_token_id=A_    )
        SCREAMING_SNAKE_CASE__       							= hidden_size
        SCREAMING_SNAKE_CASE__       							= feat_extract_norm
        SCREAMING_SNAKE_CASE__       							= feat_extract_activation
        SCREAMING_SNAKE_CASE__       							= list(A_    )
        SCREAMING_SNAKE_CASE__       							= list(A_    )
        SCREAMING_SNAKE_CASE__       							= list(A_    )
        SCREAMING_SNAKE_CASE__       							= conv_bias
        SCREAMING_SNAKE_CASE__       							= num_conv_pos_embeddings
        SCREAMING_SNAKE_CASE__       							= num_conv_pos_embedding_groups
        SCREAMING_SNAKE_CASE__       							= len(self.conv_dim    )
        SCREAMING_SNAKE_CASE__       							= num_hidden_layers
        SCREAMING_SNAKE_CASE__       							= intermediate_size
        SCREAMING_SNAKE_CASE__       							= hidden_act
        SCREAMING_SNAKE_CASE__       							= num_attention_heads
        SCREAMING_SNAKE_CASE__       							= hidden_dropout
        SCREAMING_SNAKE_CASE__       							= attention_dropout
        SCREAMING_SNAKE_CASE__       							= activation_dropout
        SCREAMING_SNAKE_CASE__       							= feat_proj_layer_norm
        SCREAMING_SNAKE_CASE__       							= feat_proj_dropout
        SCREAMING_SNAKE_CASE__       							= final_dropout
        SCREAMING_SNAKE_CASE__       							= layerdrop
        SCREAMING_SNAKE_CASE__       							= layer_norm_eps
        SCREAMING_SNAKE_CASE__       							= initializer_range
        SCREAMING_SNAKE_CASE__       							= vocab_size
        SCREAMING_SNAKE_CASE__       							= do_stable_layer_norm
        SCREAMING_SNAKE_CASE__       							= use_weighted_layer_sum
        SCREAMING_SNAKE_CASE__       							= classifier_proj_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)`, but is `len(config.conv_dim) ='''
                f''' {len(self.conv_dim    )}`, `len(config.conv_stride) = {len(self.conv_stride    )}`,'''
                f''' `len(config.conv_kernel) = {len(self.conv_kernel    )}`.'''    )

        # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
        SCREAMING_SNAKE_CASE__       							= apply_spec_augment
        SCREAMING_SNAKE_CASE__       							= mask_time_prob
        SCREAMING_SNAKE_CASE__       							= mask_time_length
        SCREAMING_SNAKE_CASE__       							= mask_time_min_masks
        SCREAMING_SNAKE_CASE__       							= mask_feature_prob
        SCREAMING_SNAKE_CASE__       							= mask_feature_length
        SCREAMING_SNAKE_CASE__       							= mask_feature_min_masks

        # ctc loss
        SCREAMING_SNAKE_CASE__       							= ctc_loss_reduction
        SCREAMING_SNAKE_CASE__       							= ctc_zero_infinity




    @property
    def    lowercase_				(					self    ):





        '''simple docstring'''

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