





import tempfile
import unittest

import numpy as np
import torch

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor



class 				__snake_case  (     unittest.TestCase						):


    '''simple docstring'''







    def    lowercase_				(					self      ,							A_ = False    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= 10

        if only_cross_attention:
            SCREAMING_SNAKE_CASE__       							= 12
        else:
            # when only cross attention is not set, the cross attention dim must be the same as the query dim
            SCREAMING_SNAKE_CASE__       							= query_dim

        return {
            "query_dim": query_dim,
            "cross_attention_dim": cross_attention_dim,
            "heads": 2,
            "dim_head": 4,
            "added_kv_proj_dim": 6,
            "norm_num_groups": 1,
            "only_cross_attention": only_cross_attention,
            "processor": AttnAddedKVProcessor(),
        }



    def    lowercase_				(					self      ,							A_      ,							A_    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= 2

        SCREAMING_SNAKE_CASE__       							= torch.rand(A_      ,							A_      ,							3      ,							2    )
        SCREAMING_SNAKE_CASE__       							= torch.rand(A_      ,							4      ,							A_    )
        SCREAMING_SNAKE_CASE__       							= None

        return {
            "hidden_states": hidden_states,
            "encoder_hidden_states": encoder_hidden_states,
            "attention_mask": attention_mask,
        }




    def    lowercase_				(					self    ):





        '''simple docstring'''

        torch.manual_seed(0    )

        SCREAMING_SNAKE_CASE__       							= self.get_constructor_arguments(only_cross_attention=A_    )
        SCREAMING_SNAKE_CASE__       							= Attention(**A_    )

        self.assertTrue(attn.to_k is not None    )
        self.assertTrue(attn.to_v is not None    )

        SCREAMING_SNAKE_CASE__       							= self.get_forward_arguments(
            query_dim=constructor_args['''query_dim''']      ,							added_kv_proj_dim=constructor_args['''added_kv_proj_dim''']    )

        SCREAMING_SNAKE_CASE__       							= attn(**A_    )

        # only self attention

        torch.manual_seed(0    )

        SCREAMING_SNAKE_CASE__       							= self.get_constructor_arguments(only_cross_attention=A_    )
        SCREAMING_SNAKE_CASE__       							= Attention(**A_    )

        self.assertTrue(attn.to_k is None    )
        self.assertTrue(attn.to_v is None    )

        SCREAMING_SNAKE_CASE__       							= self.get_forward_arguments(
            query_dim=constructor_args['''query_dim''']      ,							added_kv_proj_dim=constructor_args['''added_kv_proj_dim''']    )

        SCREAMING_SNAKE_CASE__       							= attn(**A_    )

        self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()    )





class 				__snake_case  (     unittest.TestCase						):


    '''simple docstring'''







    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= DiffusionPipeline.from_pretrained('''hf-internal-testing/tiny-stable-diffusion-pipe'''      ,							safety_checker=A_    )

        SCREAMING_SNAKE_CASE__       							= pipe(
            '''foo'''      ,							num_inference_steps=2      ,							generator=torch.Generator('''cpu'''    ).manual_seed(0    )      ,							output_type='''np'''      ,							).images

        # the initial conversion succeeds
        SCREAMING_SNAKE_CASE__       							= DiffusionPipeline.from_pretrained(
            '''hf-internal-testing/tiny-stable-diffusion-pipe'''      ,							device_map='''sequential'''      ,							safety_checker=A_    )

        SCREAMING_SNAKE_CASE__       							= pipe(
            '''foo'''      ,							num_inference_steps=2      ,							generator=torch.Generator('''cpu'''    ).manual_seed(0    )      ,							output_type='''np'''      ,							).images

        with tempfile.TemporaryDirectory() as tmpdir:
            # save the converted model
            pipe.save_pretrained(A_    )

            # can also load the converted weights
            SCREAMING_SNAKE_CASE__       							= DiffusionPipeline.from_pretrained(A_      ,							device_map='''sequential'''      ,							safety_checker=A_    )

        SCREAMING_SNAKE_CASE__       							= pipe(
            '''foo'''      ,							num_inference_steps=2      ,							generator=torch.Generator('''cpu'''    ).manual_seed(0    )      ,							output_type='''np'''      ,							).images

        self.assertTrue(np.allclose(A_      ,							A_      ,							atol=1E-5    )    )
        self.assertTrue(np.allclose(A_      ,							A_      ,							atol=1E-5    )    )







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    )
