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_     (   UpperCAmelCase_						,	unittest.TestCase ):






    '''simple docstring'''






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







    def 			UpperCamelCase__   (	self)       ->	Optional[Any]:
        torch.manual_seed(0)
        __UpperCamelCase     :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 , )
        __UpperCamelCase     :Optional[int]			  =		DDIMScheduler(
            beta_start=0.0_00_85 , beta_end=0.0_12 , beta_schedule='''scaled_linear''' , clip_sample=__lowercase , set_alpha_to_one=__lowercase , )
        torch.manual_seed(0)
        __UpperCamelCase     :Optional[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)
        __UpperCamelCase     :Optional[int]			  =		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=1_000 , hidden_act='''gelu''' , projection_dim=512 , )
        __UpperCamelCase     :Optional[Any]			  =		CLIPTextModel(__lowercase)
        __UpperCamelCase     :Optional[int]			  =		CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''')

        __UpperCamelCase     :Union[str, Any]			  =		{
            '''unet''': unet,
            '''scheduler''': scheduler,
            '''vae''': vae,
            '''text_encoder''': text_encoder,
            '''tokenizer''': tokenizer,
        }
        return components







    def 			UpperCamelCase__   (	self , __lowercase , __lowercase=0)       ->	Optional[int]:
        if str(__lowercase).startswith('''mps'''):
            __UpperCamelCase     :List[Any]			  =		torch.manual_seed(__lowercase)
        else:
            __UpperCamelCase     :Tuple			  =		torch.Generator(device=__lowercase).manual_seed(__lowercase)
        __UpperCamelCase     :Dict			  =		{
            '''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)       ->	Optional[Any]:
        __UpperCamelCase     :int			  =		'''cpu'''  # ensure determinism for the device-dependent torch.Generator
        __UpperCamelCase     :Optional[int]			  =		self.get_dummy_components()
        __UpperCamelCase     :Dict			  =		TextToVideoSDPipeline(**__lowercase)
        __UpperCamelCase     :Any			  =		sd_pipe.to(__lowercase)
        sd_pipe.set_progress_bar_config(disable=__lowercase)

        __UpperCamelCase     :Optional[Any]			  =		self.get_dummy_inputs(__lowercase)
        __UpperCamelCase     :int			  =		'''np'''
        __UpperCamelCase     :List[str]			  =		sd_pipe(**__lowercase).frames
        __UpperCamelCase     :Optional[Any]			  =		frames[0][-3:, -3:, -1]

        assert frames[0].shape == (64, 64, 3)
        __UpperCamelCase     :str			  =		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)       ->	Tuple:
        self._test_attention_slicing_forward_pass(test_mean_pixel_difference=__lowercase , 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)       ->	Optional[int]:
        self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=__lowercase , expected_max_diff=1E-2)







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







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







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







    def 			UpperCamelCase__   (	self)       ->	List[str]:
        return super().test_progress_bar()


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






    '''simple docstring'''






    def 			UpperCamelCase__   (	self)       ->	Dict:
        __UpperCamelCase     :Union[str, Any]			  =		load_numpy(
            '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video.npy''')

        __UpperCamelCase     :List[str]			  =		TextToVideoSDPipeline.from_pretrained('''damo-vilab/text-to-video-ms-1.7b''')
        __UpperCamelCase     :Union[str, Any]			  =		DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
        __UpperCamelCase     :str			  =		pipe.to('''cuda''')

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

        __UpperCamelCase     :List[Any]			  =		pipe(__lowercase , generator=__lowercase , num_inference_steps=25 , output_type='''pt''').frames
        __UpperCamelCase     :Optional[int]			  =		video_frames.cpu().numpy()

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







    def 			UpperCamelCase__   (	self)       ->	int:
        __UpperCamelCase     :str			  =		load_numpy(
            '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video_2step.npy''')

        __UpperCamelCase     :Union[str, Any]			  =		TextToVideoSDPipeline.from_pretrained('''damo-vilab/text-to-video-ms-1.7b''')
        __UpperCamelCase     :str			  =		pipe.to('''cuda''')

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

        __UpperCamelCase     :List[Any]			  =		pipe(__lowercase , generator=__lowercase , num_inference_steps=2 , output_type='''pt''').frames
        __UpperCamelCase     :Optional[Any]			  =		video_frames.cpu().numpy()

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








import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import MgpstrTokenizer
from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES
from transformers.testing_utils import require_torch, require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_torch_available, is_vision_available


if is_torch_available():
 import torch


if is_vision_available():
 from PIL import Image

 from transformers import MgpstrProcessor, ViTImageProcessor







@require_torch
@require_vision
class   __lowercase      (unittest.TestCase ):
 _UpperCamelCase			  =  ViTImageProcessor if is_vision_available() else None
 @property
 def 	UpperCamelCase__  (							self					)					->str:



       '''simple docstring'''





       return self.image_processor_tester.prepare_image_processor_dict()
 def 	UpperCamelCase__  (							self					)					->str:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					(3, 32, 128)
       __lowerCAmelCase					:						List[str]					=					tempfile.mkdtemp()

       # fmt: off
       __lowerCAmelCase					:						List[str]					=					['''[GO]''', '''[s]''', '''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''a''', '''b''', '''c''', '''d''', '''e''', '''f''', '''g''', '''h''', '''i''', '''j''', '''k''', '''l''', '''m''', '''n''', '''o''', '''p''', '''q''', '''r''', '''s''', '''t''', '''u''', '''v''', '''w''', '''x''', '''y''', '''z''']
       # fmt: on
       __lowerCAmelCase					:						Optional[int]					=					dict(zip(A_      ,							range(len(A_					)					)					)					)

       __lowerCAmelCase					:						Tuple					=					os.path.join(self.tmpdirname      ,							VOCAB_FILES_NAMES['''vocab_file''']					)
       with open(self.vocab_file      ,							'''w'''      ,							encoding='''utf-8'''					) as fp:
             fp.write(json.dumps(A_					) + '''\n'''					)

       __lowerCAmelCase					:						Union[str, Any]					=					{
           '''do_normalize''': False,
           '''do_resize''': True,
           '''image_processor_type''': '''ViTImageProcessor''',
           '''resample''': 3,
           '''size''': {'''height''': 32, '''width''': 128},
       }
       __lowerCAmelCase					:						Optional[Any]					=					os.path.join(self.tmpdirname      ,							A_					)
       with open(self.image_processor_file      ,							'''w'''      ,							encoding='''utf-8'''					) as fp:
             json.dump(A_      ,							A_					)
 def 	UpperCamelCase__  (							self      ,							**A_					)					->Tuple:



       '''simple docstring'''





       return MgpstrTokenizer.from_pretrained(self.tmpdirname      ,							**A_					)
 def 	UpperCamelCase__  (							self      ,							**A_					)					->Tuple:



       '''simple docstring'''





       return ViTImageProcessor.from_pretrained(self.tmpdirname      ,							**A_					)
 def 	UpperCamelCase__  (							self					)					->Optional[int]:



       '''simple docstring'''





       shutil.rmtree(self.tmpdirname					)
 def 	UpperCamelCase__  (							self					)					->Optional[Any]:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					np.random.randint(255      ,							size=(3, 30, 400)      ,							dtype=np.uinta					)

       __lowerCAmelCase					:						str					=					Image.fromarray(np.moveaxis(A_      ,							0      ,							-1					)					)

       return image_input
 def 	UpperCamelCase__  (							self					)					->Any:



       '''simple docstring'''





       __lowerCAmelCase					:						Dict					=					self.get_tokenizer()
       __lowerCAmelCase					:						List[Any]					=					self.get_image_processor()

       __lowerCAmelCase					:						List[Any]					=					MgpstrProcessor(tokenizer=A_      ,							image_processor=A_					)
       processor.save_pretrained(self.tmpdirname					)
       __lowerCAmelCase					:						Union[str, Any]					=					MgpstrProcessor.from_pretrained(self.tmpdirname      ,							use_fast=A_					)

       self.assertEqual(processor.char_tokenizer.get_vocab()      ,							tokenizer.get_vocab()					)
       self.assertIsInstance(processor.char_tokenizer      ,							A_					)

       self.assertEqual(processor.image_processor.to_json_string()      ,							image_processor.to_json_string()					)
       self.assertIsInstance(processor.image_processor      ,							A_					)
 def 	UpperCamelCase__  (							self					)					->Optional[int]:



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					self.get_tokenizer()
       __lowerCAmelCase					:						Union[str, Any]					=					self.get_image_processor()

       __lowerCAmelCase					:						List[Any]					=					MgpstrProcessor(tokenizer=A_      ,							image_processor=A_					)
       processor.save_pretrained(self.tmpdirname					)

       __lowerCAmelCase					:						List[Any]					=					self.get_tokenizer(bos_token='''(BOS)'''      ,							eos_token='''(EOS)'''					)
       __lowerCAmelCase					:						int					=					self.get_image_processor(do_normalize=A_      ,							padding_value=1.0					)

       __lowerCAmelCase					:						int					=					MgpstrProcessor.from_pretrained(
           self.tmpdirname      ,							bos_token='''(BOS)'''      ,							eos_token='''(EOS)'''      ,							do_normalize=A_      ,							padding_value=1.0					)

       self.assertEqual(processor.char_tokenizer.get_vocab()      ,							tokenizer_add_kwargs.get_vocab()					)
       self.assertIsInstance(processor.char_tokenizer      ,							A_					)

       self.assertEqual(processor.image_processor.to_json_string()      ,							image_processor_add_kwargs.to_json_string()					)
       self.assertIsInstance(processor.image_processor      ,							A_					)
 def 	UpperCamelCase__  (							self					)					->List[str]:



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					self.get_image_processor()
       __lowerCAmelCase					:						Optional[Any]					=					self.get_tokenizer()

       __lowerCAmelCase					:						int					=					MgpstrProcessor(tokenizer=A_      ,							image_processor=A_					)

       __lowerCAmelCase					:						Optional[int]					=					self.prepare_image_inputs()

       __lowerCAmelCase					:						Optional[Any]					=					image_processor(A_      ,							return_tensors='''np'''					)
       __lowerCAmelCase					:						Tuple					=					processor(images=A_      ,							return_tensors='''np'''					)

       for key in input_image_proc.keys():
             self.assertAlmostEqual(input_image_proc[key].sum()      ,							input_processor[key].sum()      ,							delta=1e-2					)
 def 	UpperCamelCase__  (							self					)					->str:



       '''simple docstring'''





       __lowerCAmelCase					:						str					=					self.get_image_processor()
       __lowerCAmelCase					:						Union[str, Any]					=					self.get_tokenizer()

       __lowerCAmelCase					:						Optional[Any]					=					MgpstrProcessor(tokenizer=A_      ,							image_processor=A_					)

       __lowerCAmelCase					:						Any					=					'''test'''

       __lowerCAmelCase					:						Dict					=					processor(text=A_					)

       __lowerCAmelCase					:						str					=					tokenizer(A_					)
       for key in encoded_tok.keys():
             self.assertListEqual(encoded_tok[key]      ,							encoded_processor[key]					)
 def 	UpperCamelCase__  (							self					)					->Optional[int]:



       '''simple docstring'''





       __lowerCAmelCase					:						Dict					=					self.get_image_processor()
       __lowerCAmelCase					:						Any					=					self.get_tokenizer()

       __lowerCAmelCase					:						str					=					MgpstrProcessor(tokenizer=A_      ,							image_processor=A_					)

       __lowerCAmelCase					:						List[Any]					=					'''test'''
       __lowerCAmelCase					:						int					=					self.prepare_image_inputs()

       __lowerCAmelCase					:						int					=					processor(text=A_      ,							images=A_					)

       self.assertListEqual(list(inputs.keys()					)      ,							['''pixel_values''', '''labels''']					)

       # test if it raises when no input is passed
       with pytest.raises(A_					):
             processor()
 def 	UpperCamelCase__  (							self					)					->Union[str, Any]:



       '''simple docstring'''





       __lowerCAmelCase					:						List[Any]					=					self.get_image_processor()
       __lowerCAmelCase					:						int					=					self.get_tokenizer()

       __lowerCAmelCase					:						Any					=					MgpstrProcessor(tokenizer=A_      ,							image_processor=A_					)

       __lowerCAmelCase					:						List[str]					=					[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9], [3, 4, 3, 1, 1, 8, 9]]

       __lowerCAmelCase					:						Optional[int]					=					processor.char_decode(A_					)
       __lowerCAmelCase					:						Tuple					=					tokenizer.batch_decode(A_					)
       __lowerCAmelCase					:						Any					=					[seq.replace(''' '''      ,							''''''					) for seq in decoded_tok]

       self.assertListEqual(A_      ,							A_					)
 def 	UpperCamelCase__  (							self					)					->Any:



       '''simple docstring'''





       __lowerCAmelCase					:						str					=					self.get_image_processor()
       __lowerCAmelCase					:						Any					=					self.get_tokenizer()

       __lowerCAmelCase					:						int					=					MgpstrProcessor(tokenizer=A_      ,							image_processor=A_					)

       __lowerCAmelCase					:						Union[str, Any]					=					None
       __lowerCAmelCase					:						Optional[Any]					=					self.prepare_image_inputs()

       __lowerCAmelCase					:						List[Any]					=					processor(text=A_      ,							images=A_					)

       self.assertListEqual(list(inputs.keys()					)      ,							processor.model_input_names					)





 def 	UpperCamelCase__  (							self					)					->List[str]:



       '''simple docstring'''





       __lowerCAmelCase					:						List[str]					=					self.get_image_processor()
       __lowerCAmelCase					:						List[str]					=					self.get_tokenizer()

       __lowerCAmelCase					:						Any					=					MgpstrProcessor(tokenizer=A_      ,							image_processor=A_					)

       __lowerCAmelCase					:						List[Any]					=					torch.randn(1      ,							27      ,							38					)
       __lowerCAmelCase					:						Optional[int]					=					torch.randn(1      ,							27      ,							5_0257					)
       __lowerCAmelCase					:						Optional[Any]					=					torch.randn(1      ,							27      ,							3_0522					)

       __lowerCAmelCase					:						List[str]					=					processor.batch_decode([char_input, bpe_input, wp_input]					)

       self.assertListEqual(list(results.keys()					)      ,							['''generated_text''', '''scores''', '''char_preds''', '''bpe_preds''', '''wp_preds''']					)


