
import copy
from collections import OrderedDict
from typing import Dict, Mapping

from packaging import version

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


__lowerCAmelCase    :	List[Any]						=						logging.get_logger(__name__)

__lowerCAmelCase    :	Dict						=						{
    "facebook/detr-resnet-50": "https://huggingface.co/facebook/detr-resnet-50/resolve/main/config.json",
    # See all DETR models at https://huggingface.co/models?filter=detr
}


class   __lowerCAmelCase		(		lowerCAmelCase_					):



      """simple docstring"""







      A__		:	Union[str, Any]         =			"detr"
      A__		:	List[str]         =			["past_key_values"]
      A__		:	Union[str, Any]         =			{
          "hidden_size": "d_model",
          "num_attention_heads": "encoder_attention_heads",
      }




      def __init__(					self						:				Any ,				_snake_case						:				Optional[Any]=True ,				_snake_case						:				List[str]=None ,				_snake_case						:				str=3 ,				_snake_case						:				Tuple=100 ,				_snake_case						:				List[Any]=6 ,				_snake_case						:				Dict=2048 ,				_snake_case						:				List[Any]=8 ,				_snake_case						:				List[Any]=6 ,				_snake_case						:				Union[str, Any]=2048 ,				_snake_case						:				Optional[Any]=8 ,				_snake_case						:				Union[str, Any]=0.0 ,				_snake_case						:				Tuple=0.0 ,				_snake_case						:				Optional[Any]=True ,				_snake_case						:				Tuple="relu" ,				_snake_case						:				Dict=256 ,				_snake_case						:				List[Any]=0.1 ,				_snake_case						:				Optional[Any]=0.0 ,				_snake_case						:				List[Any]=0.0 ,				_snake_case						:				int=0.02 ,				_snake_case						:				int=1.0 ,				_snake_case						:				int=False ,				_snake_case						:				str="sine" ,				_snake_case						:				Any="resnet50" ,				_snake_case						:				Any=True ,				_snake_case						:				str=False ,				_snake_case						:				Optional[int]=1 ,				_snake_case						:				str=5 ,				_snake_case						:				Any=2 ,				_snake_case						:				Any=1 ,				_snake_case						:				Optional[Any]=1 ,				_snake_case						:				Optional[Any]=5 ,				_snake_case						:				List[Any]=2 ,				_snake_case						:				Union[str, Any]=0.1 ,				**_snake_case						:				Union[str, Any] ,				):


          if backbone_config is not None and use_timm_backbone:
              raise ValueError('''You can\'t specify both `backbone_config` and `use_timm_backbone`.''' )

          if not use_timm_backbone:
              if backbone_config is None:
                  logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.''' )
                  __lowercase						:	int					=				CONFIG_MAPPING['''resnet'''](out_features=['''stage4'''] )
              elif isinstance(lowerCamelCase__ ,				lowerCamelCase__ ):
                  __lowercase						:	List[str]					=				backbone_config.get('''model_type''' )
                  __lowercase						:	Optional[int]					=				CONFIG_MAPPING[backbone_model_type]
                  __lowercase						:	Union[str, Any]					=				config_class.from_dict(lowerCamelCase__ )
              # set timm attributes to None
              __lowercase			,					__lowercase			,					__lowercase						:	Optional[Any]					=				None, None, None

          __lowercase						:	int					=				use_timm_backbone
          __lowercase						:	List[Any]					=				backbone_config
          __lowercase						:	Tuple					=				num_channels
          __lowercase						:	Dict					=				num_queries
          __lowercase						:	List[str]					=				d_model
          __lowercase						:	Tuple					=				encoder_ffn_dim
          __lowercase						:	Union[str, Any]					=				encoder_layers
          __lowercase						:	Any					=				encoder_attention_heads
          __lowercase						:	str					=				decoder_ffn_dim
          __lowercase						:	Optional[Any]					=				decoder_layers
          __lowercase						:	Optional[Any]					=				decoder_attention_heads
          __lowercase						:	Any					=				dropout
          __lowercase						:	Union[str, Any]					=				attention_dropout
          __lowercase						:	Union[str, Any]					=				activation_dropout
          __lowercase						:	Dict					=				activation_function
          __lowercase						:	List[Any]					=				init_std
          __lowercase						:	str					=				init_xavier_std
          __lowercase						:	str					=				encoder_layerdrop
          __lowercase						:	Tuple					=				decoder_layerdrop
          __lowercase						:	Optional[int]					=				encoder_layers
          __lowercase						:	Dict					=				auxiliary_loss
          __lowercase						:	List[Any]					=				position_embedding_type
          __lowercase						:	Any					=				backbone
          __lowercase						:	Dict					=				use_pretrained_backbone
          __lowercase						:	int					=				dilation
          # Hungarian matcher
          __lowercase						:	Dict					=				class_cost
          __lowercase						:	Optional[Any]					=				bbox_cost
          __lowercase						:	List[str]					=				giou_cost
          # Loss coefficients
          __lowercase						:	List[Any]					=				mask_loss_coefficient
          __lowercase						:	Optional[Any]					=				dice_loss_coefficient
          __lowercase						:	Dict					=				bbox_loss_coefficient
          __lowercase						:	Optional[int]					=				giou_loss_coefficient
          __lowercase						:	int					=				eos_coefficient
          super().__init__(is_encoder_decoder=lowerCamelCase__ ,				**lowerCamelCase__ )




      @property
      def 						snake_case_	(					self						:				List[str] ):
          return self.encoder_attention_heads




      @property
      def 						snake_case_	(					self						:				str ):
          return self.d_model




      @classmethod
      def 						snake_case_	(					cls						:				Dict ,				_snake_case						:				PretrainedConfig ,				**_snake_case						:				str ):
          return cls(backbone_config=lowerCamelCase__ ,				**lowerCamelCase__ )




      def 						snake_case_	(					self						:				str ):
          __lowercase						:	Union[str, Any]					=				copy.deepcopy(self.__dict__ )
          if output["backbone_config"] is not None:
              __lowercase						:	Union[str, Any]					=				self.backbone_config.to_dict()
          __lowercase						:	str					=				self.__class__.model_type
          return output






class   __lowerCAmelCase		(		lowerCAmelCase_					):



      """simple docstring"""







      A__		:	Union[str, Any]         =			version.parse('''1.11'''					)




      @property
      def 						snake_case_	(					self						:				Tuple ):
          return OrderedDict(
              [
                  ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
                  ('''pixel_mask''', {0: '''batch'''}),
              ] )




      @property
      def 						snake_case_	(					self						:				Optional[Any] ):
          return 1E-5




      @property
      def 						snake_case_	(					self						:				int ):
          return 12









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 		UpperCamelCase__ (      lowerCAmelCase_							,      unittest.TestCase   ):



     '''simple docstring'''

     __snake_case    :	List[str]			=			TextToVideoSDPipeline
     __snake_case    :	int			=			TEXT_TO_IMAGE_PARAMS
     __snake_case    :	Tuple			=			TEXT_TO_IMAGE_BATCH_PARAMS
     # No `output_type`.
     __snake_case    :	Dict			=			frozenset(
         [
             "num_inference_steps",
             "generator",
             "latents",
             "return_dict",
             "callback",
             "callback_steps",
         ]   )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Optional[int]       )     ->		Optional[int]:




           '''simple docstring'''


           torch.manual_seed(0       )
           SCREAMING_SNAKE_CASE		=						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						,)
           SCREAMING_SNAKE_CASE		=						DDIMScheduler(
               beta_start=0.00085						,beta_end=0.012						,beta_schedule="""scaled_linear"""						,clip_sample=lowerCamelCase__						,set_alpha_to_one=lowerCamelCase__						,)
           torch.manual_seed(0       )
           SCREAMING_SNAKE_CASE		=						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=128						,)
           torch.manual_seed(0       )
           SCREAMING_SNAKE_CASE		=						CLIPTextConfig(
               bos_token_id=0						,eos_token_id=2						,hidden_size=32						,intermediate_size=37						,layer_norm_eps=1e-0_5						,num_attention_heads=4						,num_hidden_layers=5						,pad_token_id=1						,vocab_size=1000						,hidden_act="""gelu"""						,projection_dim=512						,)
           SCREAMING_SNAKE_CASE		=						CLIPTextModel(lowerCamelCase__       )
           SCREAMING_SNAKE_CASE		=						CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""       )

           SCREAMING_SNAKE_CASE		=						{
               """unet""": unet,
               """scheduler""": scheduler,
               """vae""": vae,
               """text_encoder""": text_encoder,
               """tokenizer""": tokenizer,
           }
           return components






     def 				SCREAMING_SNAKE_CASE__  (							self						:    int						,lowerCamelCase__						:    Optional[Any]						,lowerCamelCase__						:    int=0       )     ->		List[Any]:




           '''simple docstring'''


           if str(lowerCamelCase__       ).startswith("""mps"""       ):
                 SCREAMING_SNAKE_CASE		=						torch.manual_seed(lowerCamelCase__       )
           else:
                 SCREAMING_SNAKE_CASE		=						torch.Generator(device=lowerCamelCase__       ).manual_seed(lowerCamelCase__       )
           SCREAMING_SNAKE_CASE		=						{
               """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 				SCREAMING_SNAKE_CASE__  (							self						:    Optional[Any]       )     ->		List[str]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						"""cpu"""  # ensure determinism for the device-dependent torch.Generator
           SCREAMING_SNAKE_CASE		=						self.get_dummy_components()
           SCREAMING_SNAKE_CASE		=						TextToVideoSDPipeline(**lowerCamelCase__       )
           SCREAMING_SNAKE_CASE		=						sd_pipe.to(lowerCamelCase__       )
           sd_pipe.set_progress_bar_config(disable=lowerCamelCase__       )

           SCREAMING_SNAKE_CASE		=						self.get_dummy_inputs(lowerCamelCase__       )
           SCREAMING_SNAKE_CASE		=						"""np"""
           SCREAMING_SNAKE_CASE		=						sd_pipe(**lowerCamelCase__       ).frames
           SCREAMING_SNAKE_CASE		=						frames[0][-3:, -3:, -1]

           assert frames[0].shape == (64, 64, 3)
           SCREAMING_SNAKE_CASE		=						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 				SCREAMING_SNAKE_CASE__  (							self						:    Dict       )     ->		int:




           '''simple docstring'''


           self._test_attention_slicing_forward_pass(test_mean_pixel_difference=lowerCamelCase__						,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 				SCREAMING_SNAKE_CASE__  (							self						:    Tuple       )     ->		Any:




           '''simple docstring'''


           self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=lowerCamelCase__						,expected_max_diff=1e-2       )






     @unittest.skip(reason="""Batching needs to be properly figured out first for this pipeline."""       )
     def 				SCREAMING_SNAKE_CASE__  (							self						:    Optional[Any]       )     ->		List[Any]:




           '''simple docstring'''


           pass






     @unittest.skip(reason="""Batching needs to be properly figured out first for this pipeline."""       )
     def 				SCREAMING_SNAKE_CASE__  (							self						:    Optional[Any]       )     ->		Any:




           '''simple docstring'''


           pass






     @unittest.skip(reason="""`num_images_per_prompt` argument is not supported for this pipeline."""       )
     def 				SCREAMING_SNAKE_CASE__  (							self						:    Optional[int]       )     ->		Tuple:




           '''simple docstring'''


           pass






     def 				SCREAMING_SNAKE_CASE__  (							self						:    List[str]       )     ->		Optional[Any]:




           '''simple docstring'''


           return super().test_progress_bar()

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



     '''simple docstring'''

     def 				SCREAMING_SNAKE_CASE__  (							self						:    Any       )     ->		Optional[int]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						load_numpy(
               """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video.npy"""       )

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

           SCREAMING_SNAKE_CASE		=						"""Spiderman is surfing"""
           SCREAMING_SNAKE_CASE		=						torch.Generator(device="""cpu"""       ).manual_seed(0       )

           SCREAMING_SNAKE_CASE		=						pipe(lowerCamelCase__						,generator=lowerCamelCase__						,num_inference_steps=25						,output_type="""pt"""       ).frames
           SCREAMING_SNAKE_CASE		=						video_frames.cpu().numpy()

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






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Any       )     ->		Any:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						load_numpy(
               """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video_2step.npy"""       )

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

           SCREAMING_SNAKE_CASE		=						"""Spiderman is surfing"""
           SCREAMING_SNAKE_CASE		=						torch.Generator(device="""cpu"""       ).manual_seed(0       )

           SCREAMING_SNAKE_CASE		=						pipe(lowerCamelCase__						,generator=lowerCamelCase__						,num_inference_steps=2						,output_type="""pt"""       ).frames
           SCREAMING_SNAKE_CASE		=						video_frames.cpu().numpy()

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


