





"""simple docstring"""






import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DPMSolverMultistepScheduler,
    TextToVideoSDPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import is_xformers_available, load_numpy, skip_mps, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()



@skip_mps
class 	lowerCAmelCase					(      lowercase__			,	unittest.TestCase							):
       lowerCAmelCase__						 =  TextToVideoSDPipeline
       lowerCAmelCase__						 =  TEXT_TO_IMAGE_PARAMS
       lowerCAmelCase__						 =  TEXT_TO_IMAGE_BATCH_PARAMS
       # No `output_type`.
       lowerCAmelCase__						 =  frozenset(
           [
               """num_inference_steps""",
               """generator""",
               """latents""",
               """return_dict""",
               """callback""",
               """callback_steps""",
           ]							)






       def 							__A (   self     ):
              torch.manual_seed(0     )
              _UpperCAmelCase     =     UNetaDConditionModel(
                  block_out_channels=(32, 64, 64, 64)    ,  layers_per_block=2    ,  sample_size=32    ,  in_channels=4    ,  out_channels=4    ,  down_block_types=('CrossAttnDownBlock3D', 'CrossAttnDownBlock3D', 'CrossAttnDownBlock3D', 'DownBlock3D')    ,  up_block_types=('UpBlock3D', 'CrossAttnUpBlock3D', 'CrossAttnUpBlock3D', 'CrossAttnUpBlock3D')    ,  cross_attention_dim=32    ,  attention_head_dim=4    ,  )
              _UpperCAmelCase     =     DDIMScheduler(
                  beta_start=0.00_085    ,  beta_end=0.012    ,  beta_schedule='scaled_linear'    ,  clip_sample=a__    ,  set_alpha_to_one=a__    ,  )
              torch.manual_seed(0     )
              _UpperCAmelCase     =     AutoencoderKL(
                  block_out_channels=[32, 64]    ,  in_channels=3    ,  out_channels=3    ,  down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']    ,  up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']    ,  latent_channels=4    ,  sample_size=1_28    ,  )
              torch.manual_seed(0     )
              _UpperCAmelCase     =     CLIPTextConfig(
                  bos_token_id=0    ,  eos_token_id=2    ,  hidden_size=32    ,  intermediate_size=37    ,  layer_norm_eps=1E-05    ,  num_attention_heads=4    ,  num_hidden_layers=5    ,  pad_token_id=1    ,  vocab_size=10_00    ,  hidden_act='gelu'    ,  projection_dim=5_12    ,  )
              _UpperCAmelCase     =     CLIPTextModel(a__     )
              _UpperCAmelCase     =     CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'     )

              _UpperCAmelCase     =     {
                  'unet': unet,
                  'scheduler': scheduler,
                  'vae': vae,
                  'text_encoder': text_encoder,
                  'tokenizer': tokenizer,
              }
              return components






       def 							__A (   self    ,  a__    ,  a__=0     ):
              if str(a__     ).startswith('mps'     ):
                     _UpperCAmelCase     =     torch.manual_seed(a__     )
              else:
                     _UpperCAmelCase     =     torch.Generator(device=a__     ).manual_seed(a__     )
              _UpperCAmelCase     =     {
                  'prompt': 'A painting of a squirrel eating a burger',
                  'generator': generator,
                  'num_inference_steps': 2,
                  'guidance_scale': 6.0,
                  'output_type': 'pt',
              }
              return inputs






       def 							__A (   self     ):
              _UpperCAmelCase     =     'cpu'  # ensure determinism for the device-dependent torch.Generator
              _UpperCAmelCase     =     self.get_dummy_components()
              _UpperCAmelCase     =     TextToVideoSDPipeline(**a__     )
              _UpperCAmelCase     =     sd_pipe.to(a__     )
              sd_pipe.set_progress_bar_config(disable=a__     )

              _UpperCAmelCase     =     self.get_dummy_inputs(a__     )
              _UpperCAmelCase     =     'np'
              _UpperCAmelCase     =     sd_pipe(**a__     ).frames
              _UpperCAmelCase     =     frames[0][-3:, -3:, -1]

              assert frames[0].shape == (64, 64, 3)
              _UpperCAmelCase     =     np.array([158.0, 160.0, 153.0, 125.0, 100.0, 121.0, 111.0, 93.0, 113.0]     )

              assert np.abs(image_slice.flatten() - expected_slice     ).max() < 1E-2






       def 							__A (   self     ):
              self._test_attention_slicing_forward_pass(test_mean_pixel_difference=a__    ,  expected_max_diff=3E-3     )






       @unittest.skipIf(
           torch_device != 'cuda' or not is_xformers_available()    ,  reason='XFormers attention is only available with CUDA and `xformers` installed'    ,  )
       def 							__A (   self     ):
              self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=a__    ,  expected_max_diff=1E-2     )






       @unittest.skip(reason='Batching needs to be properly figured out first for this pipeline.'     )
       def 							__A (   self     ):
              pass






       @unittest.skip(reason='Batching needs to be properly figured out first for this pipeline.'     )
       def 							__A (   self     ):
              pass






       @unittest.skip(reason='`num_images_per_prompt` argument is not supported for this pipeline.'     )
       def 							__A (   self     ):
              pass






       def 							__A (   self     ):
              return super().test_progress_bar()



@slow
@skip_mps
class 	lowerCAmelCase					(      unittest.TestCase							):






       def 							__A (   self     ):
              _UpperCAmelCase     =     load_numpy(
                  'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video.npy'     )

              _UpperCAmelCase     =     TextToVideoSDPipeline.from_pretrained('damo-vilab/text-to-video-ms-1.7b'     )
              _UpperCAmelCase     =     DPMSolverMultistepScheduler.from_config(pipe.scheduler.config     )
              _UpperCAmelCase     =     pipe.to('cuda'     )

              _UpperCAmelCase     =     'Spiderman is surfing'
              _UpperCAmelCase     =     torch.Generator(device='cpu'     ).manual_seed(0     )

              _UpperCAmelCase     =     pipe(a__    ,  generator=a__    ,  num_inference_steps=25    ,  output_type='pt'     ).frames
              _UpperCAmelCase     =     video_frames.cpu().numpy()

              assert np.abs(expected_video - video     ).mean() < 5E-2






       def 							__A (   self     ):
              _UpperCAmelCase     =     load_numpy(
                  'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video_2step.npy'     )

              _UpperCAmelCase     =     TextToVideoSDPipeline.from_pretrained('damo-vilab/text-to-video-ms-1.7b'     )
              _UpperCAmelCase     =     pipe.to('cuda'     )

              _UpperCAmelCase     =     'Spiderman is surfing'
              _UpperCAmelCase     =     torch.Generator(device='cpu'     ).manual_seed(0     )

              _UpperCAmelCase     =     pipe(a__    ,  generator=a__    ,  num_inference_steps=2    ,  output_type='pt'     ).frames
              _UpperCAmelCase     =     video_frames.cpu().numpy()

              assert np.abs(expected_video - video     ).mean() < 5E-2













"""simple docstring"""






from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


if TYPE_CHECKING:
    from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType


lowerCAmelCase_			=							logging.get_logger(__name__)

lowerCAmelCase_			=							{
    '''microsoft/deberta-v2-xlarge''': '''https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json''',
    '''microsoft/deberta-v2-xxlarge''': '''https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json''',
    '''microsoft/deberta-v2-xlarge-mnli''': (
        '''https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json'''
    ),
    '''microsoft/deberta-v2-xxlarge-mnli''': (
        '''https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json'''
    ),
}



class 	lowerCAmelCase					(      snake_case							):
       lowerCAmelCase__						 =  """deberta-v2"""






       def __init__(   self    ,  a__=12_81_00    ,  a__=15_36    ,  a__=24    ,  a__=24    ,  a__=61_44    ,  a__="gelu"    ,  a__=0.1    ,  a__=0.1    ,  a__=5_12    ,  a__=0    ,  a__=0.02    ,  a__=1E-7    ,  a__=False    ,  a__=-1    ,  a__=0    ,  a__=True    ,  a__=None    ,  a__=0    ,  a__="gelu"    ,  **a__    ,  ):
              super().__init__(**a__     )

              _UpperCAmelCase     =     hidden_size
              _UpperCAmelCase     =     num_hidden_layers
              _UpperCAmelCase     =     num_attention_heads
              _UpperCAmelCase     =     intermediate_size
              _UpperCAmelCase     =     hidden_act
              _UpperCAmelCase     =     hidden_dropout_prob
              _UpperCAmelCase     =     attention_probs_dropout_prob
              _UpperCAmelCase     =     max_position_embeddings
              _UpperCAmelCase     =     type_vocab_size
              _UpperCAmelCase     =     initializer_range
              _UpperCAmelCase     =     relative_attention
              _UpperCAmelCase     =     max_relative_positions
              _UpperCAmelCase     =     pad_token_id
              _UpperCAmelCase     =     position_biased_input

              # Backwards compatibility
              if type(a__     ) == str:
                     _UpperCAmelCase     =     [x.strip() for x in pos_att_type.lower().split('|'     )]

              _UpperCAmelCase     =     pos_att_type
              _UpperCAmelCase     =     vocab_size
              _UpperCAmelCase     =     layer_norm_eps

              _UpperCAmelCase     =     kwargs.get('pooler_hidden_size'    ,  a__     )
              _UpperCAmelCase     =     pooler_dropout
              _UpperCAmelCase     =     pooler_hidden_act



class 	lowerCAmelCase					(      snake_case							):






       @property
       def 							__A (   self     ):
              if self.task == "multiple-choice":
                     _UpperCAmelCase     =     {0: 'batch', 1: 'choice', 2: 'sequence'}
              else:
                     _UpperCAmelCase     =     {0: 'batch', 1: 'sequence'}
              if self._config.type_vocab_size > 0:
                     return OrderedDict(
                         [('input_ids', dynamic_axis), ('attention_mask', dynamic_axis), ('token_type_ids', dynamic_axis)]     )
              else:
                     return OrderedDict([('input_ids', dynamic_axis), ('attention_mask', dynamic_axis)]     )






       @property
       def 							__A (   self     ):
              return 12






       def 							__A (   self    ,  a__    ,  a__ = -1    ,  a__ = -1    ,  a__ = -1    ,  a__ = False    ,  a__ = None    ,  a__ = 3    ,  a__ = 40    ,  a__ = 40    ,  a__ = None    ,  ):
              _UpperCAmelCase     =     super().generate_dummy_inputs(preprocessor=a__    ,  framework=a__     )
              if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs:
                     del dummy_inputs["token_type_ids"]
              return dummy_inputs






