






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 							snake_case_       (    __UpperCamelCase		,							unittest.TestCase      ):

						"""simple docstring"""



						snake_case__        =      TextToVideoSDPipeline
						snake_case__        =      TEXT_TO_IMAGE_PARAMS
						snake_case__        =      TEXT_TO_IMAGE_BATCH_PARAMS
						# No `output_type`.
						snake_case__        =      frozenset(
						    [
						        """num_inference_steps""",
						        """generator""",
						        """latents""",
						        """return_dict""",
						        """callback""",
						        """callback_steps""",
						    ]      )



						def        UpperCAmelCase__					(self:					List[str]   )    ->     int:




											'''simple docstring'''



											torch.manual_seed(0   )
											__a					:						List[str]      =					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 ,					)
											__a					:						List[str]      =					DDIMScheduler(
											    beta_start=0.0_00_85 ,					beta_end=0.0_12 ,					beta_schedule="scaled_linear" ,					clip_sample=__UpperCAmelCase ,					set_alpha_to_one=__UpperCAmelCase ,					)
											torch.manual_seed(0   )
											__a					:						int      =					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   )
											__a					:						List[str]      =					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 ,					)
											__a					:						List[Any]      =					CLIPTextModel(__UpperCAmelCase   )
											__a					:						List[str]      =					CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"   )

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



						def        UpperCAmelCase__					(self:					str ,					__UpperCAmelCase:					Any ,					__UpperCAmelCase:					Optional[int]=0   )    ->     Tuple:




											'''simple docstring'''



											if str(__UpperCAmelCase   ).startswith("mps"   ):
																__a					:						Any      =					torch.manual_seed(__UpperCAmelCase   )
											else:
																__a					:						Any      =					torch.Generator(device=__UpperCAmelCase   ).manual_seed(__UpperCAmelCase   )
											__a					:						str      =					{
											    "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        UpperCAmelCase__					(self:					Dict   )    ->     Union[str, Any]:




											'''simple docstring'''



											__a					:						Dict      =					"cpu"  # ensure determinism for the device-dependent torch.Generator
											__a					:						str      =					self.get_dummy_components()
											__a					:						Optional[Any]      =					TextToVideoSDPipeline(**__UpperCAmelCase   )
											__a					:						Union[str, Any]      =					sd_pipe.to(__UpperCAmelCase   )
											sd_pipe.set_progress_bar_config(disable=__UpperCAmelCase   )

											__a					:						Dict      =					self.get_dummy_inputs(__UpperCAmelCase   )
											__a					:						List[Any]      =					"np"
											__a					:						str      =					sd_pipe(**__UpperCAmelCase   ).frames
											__a					:						str      =					frames[0][-3:, -3:, -1]

											assert frames[0].shape == (64, 64, 3)
											__a					:						Tuple      =					np.array([1_58.0, 1_60.0, 1_53.0, 1_25.0, 1_00.0, 1_21.0, 1_11.0, 93.0, 1_13.0]   )

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



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




											'''simple docstring'''



											self._test_attention_slicing_forward_pass(test_mean_pixel_difference=__UpperCAmelCase ,					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        UpperCAmelCase__					(self:					Any   )    ->     Optional[Any]:




											'''simple docstring'''



											self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=__UpperCAmelCase ,					expected_max_diff=1E-2   )



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




											'''simple docstring'''



											pass



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




											'''simple docstring'''



											pass



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




											'''simple docstring'''



											pass



						def        UpperCAmelCase__					(self:					Union[str, Any]   )    ->     List[Any]:




											'''simple docstring'''



											return super().test_progress_bar()




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

						"""simple docstring"""



						def        UpperCAmelCase__					(self:					Tuple   )    ->     int:




											'''simple docstring'''



											__a					:						List[str]      =					load_numpy(
											    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video.npy"   )

											__a					:						Dict      =					TextToVideoSDPipeline.from_pretrained("damo-vilab/text-to-video-ms-1.7b"   )
											__a					:						str      =					DPMSolverMultistepScheduler.from_config(pipe.scheduler.config   )
											__a					:						Optional[Any]      =					pipe.to("cuda"   )

											__a					:						Union[str, Any]      =					"Spiderman is surfing"
											__a					:						Any      =					torch.Generator(device="cpu"   ).manual_seed(0   )

											__a					:						Optional[int]      =					pipe(__UpperCAmelCase ,					generator=__UpperCAmelCase ,					num_inference_steps=25 ,					output_type="pt"   ).frames
											__a					:						Any      =					video_frames.cpu().numpy()

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



						def        UpperCAmelCase__					(self:					List[str]   )    ->     int:




											'''simple docstring'''



											__a					:						Union[str, Any]      =					load_numpy(
											    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video_2step.npy"   )

											__a					:						Dict      =					TextToVideoSDPipeline.from_pretrained("damo-vilab/text-to-video-ms-1.7b"   )
											__a					:						Tuple      =					pipe.to("cuda"   )

											__a					:						int      =					"Spiderman is surfing"
											__a					:						Any      =					torch.Generator(device="cpu"   ).manual_seed(0   )

											__a					:						Union[str, Any]      =					pipe(__UpperCAmelCase ,					generator=__UpperCAmelCase ,					num_inference_steps=2 ,					output_type="pt"   ).frames
											__a					:						Tuple      =					video_frames.cpu().numpy()

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













import unittest

import numpy as np

from transformers import RobertaConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
			from transformers.models.roberta.modeling_flax_roberta import (
			    FlaxRobertaForCausalLM,
			    FlaxRobertaForMaskedLM,
			    FlaxRobertaForMultipleChoice,
			    FlaxRobertaForQuestionAnswering,
			    FlaxRobertaForSequenceClassification,
			    FlaxRobertaForTokenClassification,
			    FlaxRobertaModel,
			)




class 							snake_case_       (    unittest.TestCase      ):

						"""simple docstring"""



						def __init__(self:					Optional[int] ,					__UpperCAmelCase:					str ,					__UpperCAmelCase:					List[str]=13 ,					__UpperCAmelCase:					Any=7 ,					__UpperCAmelCase:					List[str]=True ,					__UpperCAmelCase:					Optional[int]=True ,					__UpperCAmelCase:					Dict=True ,					__UpperCAmelCase:					Optional[Any]=True ,					__UpperCAmelCase:					Optional[int]=99 ,					__UpperCAmelCase:					Optional[Any]=32 ,					__UpperCAmelCase:					int=5 ,					__UpperCAmelCase:					Dict=4 ,					__UpperCAmelCase:					Optional[int]=37 ,					__UpperCAmelCase:					int="gelu" ,					__UpperCAmelCase:					Tuple=0.1 ,					__UpperCAmelCase:					Any=0.1 ,					__UpperCAmelCase:					Union[str, Any]=512 ,					__UpperCAmelCase:					Optional[Any]=16 ,					__UpperCAmelCase:					List[Any]=2 ,					__UpperCAmelCase:					str=0.02 ,					__UpperCAmelCase:					int=4 ,					)    ->     str:




											'''simple docstring'''



											__a					:						Tuple      =					parent
											__a					:						int      =					batch_size
											__a					:						Optional[int]      =					seq_length
											__a					:						List[Any]      =					is_training
											__a					:						Tuple      =					use_attention_mask
											__a					:						Optional[int]      =					use_token_type_ids
											__a					:						Tuple      =					use_labels
											__a					:						str      =					vocab_size
											__a					:						Union[str, Any]      =					hidden_size
											__a					:						List[str]      =					num_hidden_layers
											__a					:						Optional[int]      =					num_attention_heads
											__a					:						Any      =					intermediate_size
											__a					:						Any      =					hidden_act
											__a					:						List[str]      =					hidden_dropout_prob
											__a					:						Dict      =					attention_probs_dropout_prob
											__a					:						Tuple      =					max_position_embeddings
											__a					:						Optional[int]      =					type_vocab_size
											__a					:						Tuple      =					type_sequence_label_size
											__a					:						List[Any]      =					initializer_range
											__a					:						int      =					num_choices



						def        UpperCAmelCase__					(self:					Tuple   )    ->     List[Any]:




											'''simple docstring'''



											__a					:						List[Any]      =					ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size   )

											__a					:						Dict      =					None
											if self.use_attention_mask:
																__a					:						List[Any]      =					random_attention_mask([self.batch_size, self.seq_length]   )

											__a					:						Optional[int]      =					None
											if self.use_token_type_ids:
																__a					:						Dict      =					ids_tensor([self.batch_size, self.seq_length] ,					self.type_vocab_size   )

											__a					:						Dict      =					RobertaConfig(
											    vocab_size=self.vocab_size ,					hidden_size=self.hidden_size ,					num_hidden_layers=self.num_hidden_layers ,					num_attention_heads=self.num_attention_heads ,					intermediate_size=self.intermediate_size ,					hidden_act=self.hidden_act ,					hidden_dropout_prob=self.hidden_dropout_prob ,					attention_probs_dropout_prob=self.attention_probs_dropout_prob ,					max_position_embeddings=self.max_position_embeddings ,					type_vocab_size=self.type_vocab_size ,					is_decoder=__UpperCAmelCase ,					initializer_range=self.initializer_range ,					)

											return config, input_ids, token_type_ids, attention_mask



						def        UpperCAmelCase__					(self:					Optional[Any]   )    ->     Union[str, Any]:




											'''simple docstring'''



											__a					:						Union[str, Any]      =					self.prepare_config_and_inputs()
											__a      ,			__a      ,			__a      ,			__a					:						Dict      =					config_and_inputs
											__a					:						str      =					{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask}
											return config, inputs_dict



						def        UpperCAmelCase__					(self:					Dict   )    ->     str:




											'''simple docstring'''



											__a					:						Optional[Any]      =					self.prepare_config_and_inputs()
											__a      ,			__a      ,			__a      ,			__a					:						Tuple      =					config_and_inputs

											__a					:						int      =					True
											__a					:						List[str]      =					floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
											__a					:						List[Any]      =					ids_tensor([self.batch_size, self.seq_length] ,					vocab_size=2   )

											return (
											    config,
											    input_ids,
											    token_type_ids,
											    encoder_hidden_states,
											    encoder_attention_mask,
											)




@require_flax
class 							snake_case_       (    __UpperCamelCase		,							unittest.TestCase      ):

						"""simple docstring"""



						snake_case__        =      True

						snake_case__        =      (
						    (
						        FlaxRobertaModel,
						        FlaxRobertaForCausalLM,
						        FlaxRobertaForMaskedLM,
						        FlaxRobertaForSequenceClassification,
						        FlaxRobertaForTokenClassification,
						        FlaxRobertaForMultipleChoice,
						        FlaxRobertaForQuestionAnswering,
						    )
						    if is_flax_available()
						    else ()
						)



						def        UpperCAmelCase__					(self:					Dict   )    ->     Union[str, Any]:




											'''simple docstring'''



											__a					:						Tuple      =					FlaxRobertaModelTester(self   )



						@slow
						def        UpperCAmelCase__					(self:					Optional[Any]   )    ->     List[Any]:




											'''simple docstring'''



											for model_class_name in self.all_model_classes:
																__a					:						int      =					model_class_name.from_pretrained("roberta-base" ,					from_pt=__UpperCAmelCase   )
																__a					:						Tuple      =					model(np.ones((1, 1)   )   )
																self.assertIsNotNone(__UpperCAmelCase   )





