
"""simple docstring"""
import functools
import operator

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


lowercase_       :						List[str]     					=	logging.get_logger(__name__)

lowercase_       :						int     					=	{
    '''microsoft/unispeech-large-1500h-cv''': (
        '''https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json'''
    ),
    # See all UniSpeech models at https://huggingface.co/models?filter=unispeech
}
class 			UpperCamelCase      (							__SCREAMING_SNAKE_CASE  ):
     A__      =			"""unispeech"""
     def __init__(     self  ,     snake_case__=32  ,     snake_case__=768  ,     snake_case__=12  ,     snake_case__=12  ,     snake_case__=3072  ,     snake_case__="gelu"  ,     snake_case__=0.1  ,     snake_case__=0.1  ,     snake_case__=0.1  ,     snake_case__=0.0  ,     snake_case__=0.0  ,     snake_case__=0.1  ,     snake_case__=0.1  ,     snake_case__=0.02  ,     snake_case__=1E-5  ,     snake_case__="group"  ,     snake_case__="gelu"  ,     snake_case__=(512, 512, 512, 512, 512, 512, 512)  ,     snake_case__=(5, 2, 2, 2, 2, 2, 2)  ,     snake_case__=(10, 3, 3, 3, 3, 2, 2)  ,     snake_case__=False  ,     snake_case__=128  ,     snake_case__=16  ,     snake_case__=False  ,     snake_case__=True  ,     snake_case__=0.05  ,     snake_case__=10  ,     snake_case__=2  ,     snake_case__=0.0  ,     snake_case__=10  ,     snake_case__=0  ,     snake_case__=320  ,     snake_case__=2  ,     snake_case__=0.1  ,     snake_case__=100  ,     snake_case__=256  ,     snake_case__=256  ,     snake_case__=0.1  ,     snake_case__="mean"  ,     snake_case__=False  ,     snake_case__=False  ,     snake_case__=256  ,     snake_case__=80  ,     snake_case__=0  ,     snake_case__=1  ,     snake_case__=2  ,     snake_case__=0.5  ,     **snake_case__  ,     ):




       """simple docstring"""


       super().__init__(**snake_case__  ,     pad_token_id=snake_case__  ,     bos_token_id=snake_case__  ,     eos_token_id=snake_case__				)
       _SCREAMING_SNAKE_CASE   :				Dict							    = hidden_size
       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = feat_extract_norm
       _SCREAMING_SNAKE_CASE   :				Tuple							    = feat_extract_activation
       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = list(snake_case__				)
       _SCREAMING_SNAKE_CASE   :				Tuple							    = list(snake_case__				)
       _SCREAMING_SNAKE_CASE   :				List[str]							    = list(snake_case__				)
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = conv_bias
       _SCREAMING_SNAKE_CASE   :				Dict							    = num_conv_pos_embeddings
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = num_conv_pos_embedding_groups
       _SCREAMING_SNAKE_CASE   :				Tuple							    = len(self.conv_dim				)
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = num_hidden_layers
       _SCREAMING_SNAKE_CASE   :				Any							    = intermediate_size
       _SCREAMING_SNAKE_CASE   :				Dict							    = hidden_act
       _SCREAMING_SNAKE_CASE   :				int							    = num_attention_heads
       _SCREAMING_SNAKE_CASE   :				Any							    = hidden_dropout
       _SCREAMING_SNAKE_CASE   :				List[str]							    = attention_dropout
       _SCREAMING_SNAKE_CASE   :				int							    = activation_dropout
       _SCREAMING_SNAKE_CASE   :				Tuple							    = feat_proj_dropout
       _SCREAMING_SNAKE_CASE   :				List[str]							    = final_dropout
       _SCREAMING_SNAKE_CASE   :				str							    = layerdrop
       _SCREAMING_SNAKE_CASE   :				Tuple							    = layer_norm_eps
       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = initializer_range
       _SCREAMING_SNAKE_CASE   :				int							    = num_ctc_classes
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = vocab_size
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = do_stable_layer_norm
       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = use_weighted_layer_sum
       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = 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   :				Any							    = apply_spec_augment
       _SCREAMING_SNAKE_CASE   :				List[str]							    = mask_time_prob
       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = mask_time_length
       _SCREAMING_SNAKE_CASE   :				Dict							    = mask_time_min_masks
       _SCREAMING_SNAKE_CASE   :				Any							    = mask_feature_prob
       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = mask_feature_length
       _SCREAMING_SNAKE_CASE   :				int							    = mask_feature_min_masks

       # parameters for pretraining with codevector quantized representations
       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = num_codevectors_per_group
       _SCREAMING_SNAKE_CASE   :				int							    = num_codevector_groups
       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = contrastive_logits_temperature
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = feat_quantizer_dropout
       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = num_negatives
       _SCREAMING_SNAKE_CASE   :				int							    = codevector_dim
       _SCREAMING_SNAKE_CASE   :				Tuple							    = proj_codevector_dim
       _SCREAMING_SNAKE_CASE   :				int							    = diversity_loss_weight

       # ctc loss
       _SCREAMING_SNAKE_CASE   :				Tuple							    = ctc_loss_reduction
       _SCREAMING_SNAKE_CASE   :				Tuple							    = ctc_zero_infinity

       # pretraining loss
       _SCREAMING_SNAKE_CASE   :				str							    = replace_prob




     @property
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


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







"""simple docstring"""
import gc
import unittest

import numpy as np
import torch

from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, DPMSolverMultistepScheduler, TransformeraDModel
from diffusers.utils import is_xformers_available, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS,
    CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()
class 			UpperCamelCase      (							__SCREAMING_SNAKE_CASE      ,       unittest.TestCase  ):
     A__      =			DiTPipeline
     A__      =			CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS
     A__      =			PipelineTesterMixin.required_optional_params - {
         """latents""",
         """num_images_per_prompt""",
         """callback""",
         """callback_steps""",
     }
     A__      =			CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS
     A__      =			False
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       torch.manual_seed(0				)
       _SCREAMING_SNAKE_CASE   :				List[str]							    = TransformeraDModel(
           sample_size=16  ,     num_layers=2  ,     patch_size=4  ,     attention_head_dim=8  ,     num_attention_heads=2  ,     in_channels=4  ,     out_channels=8  ,     attention_bias=snake_case__  ,     activation_fn="gelu-approximate"  ,     num_embeds_ada_norm=1000  ,     norm_type="ada_norm_zero"  ,     norm_elementwise_affine=snake_case__  ,     )
       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = AutoencoderKL()
       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = DDIMScheduler()
       _SCREAMING_SNAKE_CASE   :				Dict							    = {"transformer": transformer.eval(), "vae": vae.eval(), "scheduler": scheduler}
       return components
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__  ,     snake_case__=0				):




       """simple docstring"""


       if str(snake_case__				).startswith("mps"				):
         _SCREAMING_SNAKE_CASE   :				Tuple							    = torch.manual_seed(snake_case__				)
       else:
         _SCREAMING_SNAKE_CASE   :				List[Any]							    = torch.Generator(device=snake_case__				).manual_seed(snake_case__				)
       _SCREAMING_SNAKE_CASE   :				List[str]							    = {
           "class_labels": [1],
           "generator": generator,
           "num_inference_steps": 2,
           "output_type": "numpy",
       }
       return inputs
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Dict							    = "cpu"

       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = self.get_dummy_components()
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = self.pipeline_class(**snake_case__				)
       pipe.to(snake_case__				)
       pipe.set_progress_bar_config(disable=snake_case__				)

       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = self.get_dummy_inputs(snake_case__				)
       _SCREAMING_SNAKE_CASE   :				List[str]							    = pipe(**snake_case__				).images
       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = image[0, -3:, -3:, -1]

       self.assertEqual(image.shape  ,     (1, 16, 16, 3)				)
       _SCREAMING_SNAKE_CASE   :				List[str]							    = np.array([0.2_946, 0.6_601, 0.4_329, 0.3_296, 0.4_144, 0.5_319, 0.7_273, 0.5_013, 0.4_457]				)
       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = np.abs(image_slice.flatten() - expected_slice				).max()
       self.assertLessEqual(snake_case__  ,     1E-3				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       self._test_inference_batch_single_identical(relax_max_difference=snake_case__  ,     expected_max_diff=1E-3				)




     @unittest.skipIf(
         torch_device != "cuda" or not is_xformers_available()  ,     reason="XFormers attention is only available with CUDA and `xformers` installed"  ,     )
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3				)
@require_torch_gpu
@slow
class 			UpperCamelCase      (							unittest.TestCase  ):
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				str							    = torch.manual_seed(0				)

       _SCREAMING_SNAKE_CASE   :				str							    = DiTPipeline.from_pretrained("facebook/DiT-XL-2-256"				)
       pipe.to("cuda"				)

       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = ["vase", "umbrella", "white shark", "white wolf"]
       _SCREAMING_SNAKE_CASE   :				Any							    = pipe.get_label_ids(snake_case__				)

       _SCREAMING_SNAKE_CASE   :				List[str]							    = pipe(snake_case__  ,     generator=snake_case__  ,     num_inference_steps=40  ,     output_type="np"				).images

       for word, image in zip(snake_case__  ,     snake_case__				):
         _SCREAMING_SNAKE_CASE   :				int							    = load_numpy(
             F'''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/dit/{word}.npy'''				)
         assert np.abs((expected_image - image).max()				) < 1E-2




     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Any							    = DiTPipeline.from_pretrained("facebook/DiT-XL-2-512"				)
       _SCREAMING_SNAKE_CASE   :				Dict							    = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config				)
       pipe.to("cuda"				)

       _SCREAMING_SNAKE_CASE   :				List[Any]							    = ["vase", "umbrella"]
       _SCREAMING_SNAKE_CASE   :				int							    = pipe.get_label_ids(snake_case__				)

       _SCREAMING_SNAKE_CASE   :				str							    = torch.manual_seed(0				)
       _SCREAMING_SNAKE_CASE   :				Tuple							    = pipe(snake_case__  ,     generator=snake_case__  ,     num_inference_steps=25  ,     output_type="np"				).images

       for word, image in zip(snake_case__  ,     snake_case__				):
         _SCREAMING_SNAKE_CASE   :				Tuple							    = load_numpy(
             "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
             F'''/dit/{word}_512.npy'''				)

         assert np.abs((expected_image - image).max()				) < 1E-1



