






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


snake_case     					=		logging.get_logger(__name__)

snake_case     					=		{
    """naver-clova-ix/donut-base""": """https://huggingface.co/naver-clova-ix/donut-base/resolve/main/config.json""",
    # See all Donut models at https://huggingface.co/models?filter=donut-swin
}


class 			SCREAMING_SNAKE_CASE      (      lowerCAmelCase       ):
     '''simple docstring'''


     UpperCamelCase_				:		Tuple								=						'''donut-swin'''

     UpperCamelCase_				:		List[str]								=						{
         '''num_attention_heads''': '''num_heads''',
         '''num_hidden_layers''': '''num_layers''',
     }


     def __init__(					self							:						List[Any]       ,			UpperCAmelCase_							:						Optional[int]=224       ,			UpperCAmelCase_							:						Any=4       ,			UpperCAmelCase_							:						Optional[Any]=3       ,			UpperCAmelCase_							:						Union[str, Any]=96       ,			UpperCAmelCase_							:						Union[str, Any]=[2, 2, 6, 2]       ,			UpperCAmelCase_							:						int=[3, 6, 12, 24]       ,			UpperCAmelCase_							:						Dict=7       ,			UpperCAmelCase_							:						Tuple=4.0       ,			UpperCAmelCase_							:						int=True       ,			UpperCAmelCase_							:						Optional[Any]=0.0       ,			UpperCAmelCase_							:						Tuple=0.0       ,			UpperCAmelCase_							:						Dict=0.1       ,			UpperCAmelCase_							:						int="gelu"       ,			UpperCAmelCase_							:						List[Any]=False       ,			UpperCAmelCase_							:						Optional[Any]=0.02       ,			UpperCAmelCase_							:						List[Any]=1E-5       ,			**UpperCAmelCase_							:						Any       ,			):
       super().__init__(**UpperCAmelCase_   )

       SCREAMING_SNAKE_CASE							:   int						    =     image_size
       SCREAMING_SNAKE_CASE							:   Optional[int]						    =     patch_size
       SCREAMING_SNAKE_CASE							:   int						    =     num_channels
       SCREAMING_SNAKE_CASE							:   int						    =     embed_dim
       SCREAMING_SNAKE_CASE							:   Union[str, Any]						    =     depths
       SCREAMING_SNAKE_CASE							:   Any						    =     len(UpperCAmelCase_   )
       SCREAMING_SNAKE_CASE							:   Dict						    =     num_heads
       SCREAMING_SNAKE_CASE							:   List[str]						    =     window_size
       SCREAMING_SNAKE_CASE							:   Optional[int]						    =     mlp_ratio
       SCREAMING_SNAKE_CASE							:   Union[str, Any]						    =     qkv_bias
       SCREAMING_SNAKE_CASE							:   Any						    =     hidden_dropout_prob
       SCREAMING_SNAKE_CASE							:   Any						    =     attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE							:   Dict						    =     drop_path_rate
       SCREAMING_SNAKE_CASE							:   Tuple						    =     hidden_act
       SCREAMING_SNAKE_CASE							:   List[Any]						    =     use_absolute_embeddings
       SCREAMING_SNAKE_CASE							:   str						    =     layer_norm_eps
       SCREAMING_SNAKE_CASE							:   Tuple						    =     initializer_range
       # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
       # this indicates the channel dimension after the last stage of the model
       SCREAMING_SNAKE_CASE							:   int						    =     int(embed_dim * 2 ** (len(UpperCAmelCase_   ) - 1)   )










import random
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    UNetaDConditionModel,
    VideoToVideoSDPipeline,
)
from diffusers.utils import floats_tensor, is_xformers_available, skip_mps
from diffusers.utils.testing_utils import enable_full_determinism, slow, torch_device

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()



@skip_mps
class 	__lowerCamelCase			(_a				,      unittest.TestCase						):
       _lowercase											=     VideoToVideoSDPipeline
       _lowercase											=     TEXT_GUIDED_IMAGE_VARIATION_PARAMS.union({"""video"""}						) - {"""image""", """width""", """height"""}
       _lowercase											=     TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"""video"""}						) - {"""image"""}
       _lowercase											=     PipelineTesterMixin.required_optional_params - {"""latents"""}
       _lowercase											=     False

       # No `output_type`.
       _lowercase											=     frozenset(
           [
               """num_inference_steps""",
               """generator""",
               """latents""",
               """return_dict""",
               """callback""",
               """callback_steps""",
           ]						)







       def 		snake_case_	(			self: List[str]					):






            '''simple docstring'''




            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.0_0_0_8_5,beta_end=0.0_1_2,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=128,)
            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=1000,hidden_act='gelu',projection_dim=512,)
            __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 		snake_case_	(			self: Union[str, Any],A_: Any,A_: Any=0					):






            '''simple docstring'''




            __UpperCamelCase =			floats_tensor((1, 3, 3, 32, 32),rng=random.Random(A_					)					).to(A_					)

            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',
                'video': video,
                'generator': generator,
                'num_inference_steps': 2,
                'guidance_scale': 6.0,
                'output_type': 'pt',
            }
            return inputs







       def 		snake_case_	(			self: Union[str, Any]					):






            '''simple docstring'''




            __UpperCamelCase =			'cpu'  # ensure determinism for the device-dependent torch.Generator
            __UpperCamelCase =			self.get_dummy_components()
            __UpperCamelCase =			VideoToVideoSDPipeline(**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 == (32, 32, 3)
            __UpperCamelCase =			np.array([106, 117, 113, 174, 137, 112, 148, 151, 131]					)

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







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






            '''simple docstring'''




            self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=A_,expected_max_diff=5E-3					)







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






            '''simple docstring'''




            pass







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






            '''simple docstring'''




            pass







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






            '''simple docstring'''




            pass







       def 		snake_case_	(			self: Any					):






            '''simple docstring'''




            return super().test_progress_bar()



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







       def 		snake_case_	(			self: Tuple					):






            '''simple docstring'''




            __UpperCamelCase =			VideoToVideoSDPipeline.from_pretrained('cerspense/zeroscope_v2_XL',torch_dtype=torch.floataa					)
            pipe.enable_model_cpu_offload()

            # 10 frames
            __UpperCamelCase =			torch.Generator(device='cpu'					).manual_seed(0					)
            __UpperCamelCase =			torch.randn((1, 10, 3, 1024, 576),generator=A_					)
            __UpperCamelCase =			video.to('cuda'					)

            __UpperCamelCase =			'Spiderman is surfing'

            __UpperCamelCase =			pipe(A_,video=A_,generator=A_,num_inference_steps=3,output_type='pt'					).frames

            __UpperCamelCase =			np.array([-1.0_4_5_8_9_8_4, -1.1_2_7_9_2_9_7, -0.9_6_6_3_0_8_6, -0.9_1_5_0_3_9_0_6, -0.7_5_0_9_7_6_5_6]					)
            assert np.abs(video_frames.cpu().numpy()[0, 0, 0, 0, -5:] - expected_array					).sum() < 1E-2


