


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       __magic_name__	(     __lowerCAmelCase					,				unittest.TestCase):
     A:		str			=      VideoToVideoSDPipeline
     A:		int			=      TEXT_GUIDED_IMAGE_VARIATION_PARAMS.union({"video"}) - {"image", "width", "height"}
     A:		str			=      TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"video"}) - {"image"}
     A:		Any			=      PipelineTesterMixin.required_optional_params - {"latents"}
     A:		List[Any]			=      False

     # No `output_type`.
     A:		List[Any]			=      frozenset(
         [
             "num_inference_steps",
             "generator",
             "latents",
             "return_dict",
             "callback",
             "callback_steps",
         ])


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





      '''simple docstring'''






      torch.manual_seed(0			)
      UpperCamelCase__					:    int          =   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_0085							,	beta_end=0.012							,	beta_schedule='''scaled_linear'''							,	clip_sample=lowerCamelCase__							,	set_alpha_to_one=lowerCamelCase__							,	)
      torch.manual_seed(0			)
      UpperCamelCase__					:    Optional[Any]          =   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__					:    Dict          =   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							,	)
      UpperCamelCase__					:    int          =   CLIPTextModel(lowerCamelCase__			)
      UpperCamelCase__					:    Union[str, Any]          =   CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''			)

      UpperCamelCase__					:    Optional[int]          =   {
          '''unet''': unet,
          '''scheduler''': scheduler,
          '''vae''': vae,
          '''text_encoder''': text_encoder,
          '''tokenizer''': tokenizer,
      }
      return components


     def      UpperCAmelCase__	(      self							:     List[Any]							,	lowerCamelCase__							:     Any							,	lowerCamelCase__							:     str=0			)  ->      str:





      '''simple docstring'''






      UpperCamelCase__					:    str          =   floats_tensor((1, 3, 3, 32, 32)							,	rng=random.Random(lowerCamelCase__			)			).to(lowerCamelCase__			)

      if str(lowerCamelCase__			).startswith('''mps'''			):
       UpperCamelCase__					:    List[str]          =   torch.manual_seed(lowerCamelCase__			)
      else:
       UpperCamelCase__					:    List[str]          =   torch.Generator(device=lowerCamelCase__			).manual_seed(lowerCamelCase__			)
      UpperCamelCase__					:    Tuple          =   {
          '''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      UpperCAmelCase__	(      self							:     List[str]			)  ->      Tuple:





      '''simple docstring'''






      UpperCamelCase__					:    List[str]          =   '''cpu'''  # ensure determinism for the device-dependent torch.Generator
      UpperCamelCase__					:    List[Any]          =   self.get_dummy_components()
      UpperCamelCase__					:    Any          =   VideoToVideoSDPipeline(**lowerCamelCase__			)
      UpperCamelCase__					:    Optional[int]          =   sd_pipe.to(lowerCamelCase__			)
      sd_pipe.set_progress_bar_config(disable=lowerCamelCase__			)

      UpperCamelCase__					:    Any          =   self.get_dummy_inputs(lowerCamelCase__			)
      UpperCamelCase__					:    str          =   '''np'''
      UpperCamelCase__					:    Any          =   sd_pipe(**lowerCamelCase__			).frames
      UpperCamelCase__					:    Dict          =   frames[0][-3:, -3:, -1]

      assert frames[0].shape == (32, 32, 3)
      UpperCamelCase__					:    Any          =   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      UpperCAmelCase__	(      self							:     Dict			)  ->      str:





      '''simple docstring'''






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


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





      '''simple docstring'''






      pass


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





      '''simple docstring'''






      pass


     @unittest.skip(reason='''`num_images_per_prompt` argument is not supported for this pipeline.'''			)
     def      UpperCAmelCase__	(      self							:     Any			)  ->      Dict:





      '''simple docstring'''






      pass


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





      '''simple docstring'''






      return super().test_progress_bar()



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


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





      '''simple docstring'''






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

      # 10 frames
      UpperCamelCase__					:    Dict          =   torch.Generator(device='''cpu'''			).manual_seed(0			)
      UpperCamelCase__					:    Optional[int]          =   torch.randn((1, 10, 3, 1024, 576)							,	generator=lowerCamelCase__			)
      UpperCamelCase__					:    List[Any]          =   video.to('''cuda'''			)

      UpperCamelCase__					:    Optional[int]          =   '''Spiderman is surfing'''

      UpperCamelCase__					:    Dict          =   pipe(lowerCamelCase__							,	video=lowerCamelCase__							,	generator=lowerCamelCase__							,	num_inference_steps=3							,	output_type='''pt'''			).frames

      UpperCamelCase__					:    Any          =   np.array([-1.045_8984, -1.127_9297, -0.966_3086, -0.9150_3906, -0.7509_7656]			)
      assert np.abs(video_frames.cpu().numpy()[0, 0, 0, 0, -5:] - expected_array			).sum() < 1E-2






import unittest

from transformers import BarthezTokenizer, BarthezTokenizerFast, BatchEncoding
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow

from ...test_tokenization_common import TokenizerTesterMixin

@require_tokenizers
@require_sentencepiece
@slow  # see https://github.com/huggingface/transformers/issues/11457
class       __magic_name__	(     __lowerCAmelCase					,				unittest.TestCase):
     A:		Optional[int]			=      BarthezTokenizer
     A:		Optional[int]			=      BarthezTokenizerFast
     A:		str			=      True
     A:		Optional[int]			=      True


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





      '''simple docstring'''






      super().setUp()

      UpperCamelCase__					:    Optional[Any]          =   BarthezTokenizerFast.from_pretrained('''moussaKam/mbarthez'''			)
      tokenizer.save_pretrained(self.tmpdirname			)
      tokenizer.save_pretrained(self.tmpdirname							,	legacy_format=lowerCamelCase__			)
      UpperCamelCase__					:    int          =   tokenizer


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





      '''simple docstring'''






      UpperCamelCase__					:    Optional[int]          =   '''<pad>'''
      UpperCamelCase__					:    List[str]          =   1

      self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowerCamelCase__			)							,	lowerCamelCase__			)
      self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowerCamelCase__			)							,	lowerCamelCase__			)


     def      UpperCAmelCase__	(      self							:     int			)  ->      Tuple:





      '''simple docstring'''






      UpperCamelCase__					:    int          =   list(self.get_tokenizer().get_vocab().keys()			)

      self.assertEqual(vocab_keys[0]							,	'''<s>'''			)
      self.assertEqual(vocab_keys[1]							,	'''<pad>'''			)
      self.assertEqual(vocab_keys[-1]							,	'''<mask>'''			)
      self.assertEqual(len(lowerCamelCase__			)							,	101122			)


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





      '''simple docstring'''






      self.assertEqual(self.get_tokenizer().vocab_size							,	101122			)


     @require_torch
     def      UpperCAmelCase__	(      self							:     Dict			)  ->      Tuple:





      '''simple docstring'''






      UpperCamelCase__					:    Dict          =   ['''A long paragraph for summarization.''', '''Another paragraph for summarization.''']
      UpperCamelCase__					:    Any          =   [0, 57, 3018, 70307, 91, 2]

      UpperCamelCase__					:    Optional[int]          =   self.tokenizer(
          lowerCamelCase__							,	max_length=len(lowerCamelCase__			)							,	padding=lowerCamelCase__							,	truncation=lowerCamelCase__							,	return_tensors='''pt'''			)
      self.assertIsInstance(lowerCamelCase__							,	lowerCamelCase__			)

      self.assertEqual((2, 6)							,	batch.input_ids.shape			)
      self.assertEqual((2, 6)							,	batch.attention_mask.shape			)
      UpperCamelCase__					:    List[Any]          =   batch.input_ids.tolist()[0]
      self.assertListEqual(lowerCamelCase__							,	lowerCamelCase__			)


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





      '''simple docstring'''






      if not self.test_rust_tokenizer:
       return

      UpperCamelCase__					:    List[Any]          =   self.get_tokenizer()
      UpperCamelCase__					:    Optional[int]          =   self.get_rust_tokenizer()

      UpperCamelCase__					:    Any          =   '''I was born in 92000, and this is falsé.'''

      UpperCamelCase__					:    List[str]          =   tokenizer.tokenize(lowerCamelCase__			)
      UpperCamelCase__					:    Tuple          =   rust_tokenizer.tokenize(lowerCamelCase__			)
      self.assertListEqual(lowerCamelCase__							,	lowerCamelCase__			)

      UpperCamelCase__					:    Tuple          =   tokenizer.encode(lowerCamelCase__							,	add_special_tokens=lowerCamelCase__			)
      UpperCamelCase__					:    str          =   rust_tokenizer.encode(lowerCamelCase__							,	add_special_tokens=lowerCamelCase__			)
      self.assertListEqual(lowerCamelCase__							,	lowerCamelCase__			)

      UpperCamelCase__					:    Tuple          =   self.get_rust_tokenizer()
      UpperCamelCase__					:    Optional[int]          =   tokenizer.encode(lowerCamelCase__			)
      UpperCamelCase__					:    str          =   rust_tokenizer.encode(lowerCamelCase__			)
      self.assertListEqual(lowerCamelCase__							,	lowerCamelCase__			)


     @slow
     def      UpperCAmelCase__	(      self							:     List[str]			)  ->      Dict:





      '''simple docstring'''






      UpperCamelCase__					:    Optional[int]          =   {'''input_ids''': [[0, 490, 14328, 4507, 354, 47, 43669, 95, 25, 78117, 20215, 19779, 190, 22, 400, 4, 35343, 80310, 603, 86, 24937, 105, 33438, 94762, 196, 39642, 7, 15, 15933, 173, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 10534, 87, 25, 66, 3358, 196, 55289, 8, 82961, 81, 2204, 75203, 7, 15, 763, 12956, 216, 178, 14328, 9595, 1377, 69693, 7, 448, 71021, 196, 18106, 1437, 13974, 108, 9083, 4, 49315, 7, 39, 86, 1326, 2793, 46333, 4, 448, 196, 74588, 7, 49315, 7, 39, 21, 822, 38470, 74, 21, 66723, 62480, 8, 22050, 5, 2]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}  # noqa: E501
      # fmt: on

      # moussaKam/mbarthez is a french model. So we also use french texts.
      UpperCamelCase__					:    int          =   [
          '''Le transformeur est un modèle d\'apprentissage profond introduit en 2017, '''
          '''utilisé principalement dans le domaine du traitement automatique des langues (TAL).''',
          '''À l\'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus '''
          '''pour gérer des données séquentielles, telles que le langage naturel, pour des tâches '''
          '''telles que la traduction et la synthèse de texte.''',
      ]

      self.tokenizer_integration_test_util(
          expected_encoding=lowerCamelCase__							,	model_name='''moussaKam/mbarthez'''							,	revision='''c2e4ecbca5e3cd2c37fe1ac285ca4fbdf1366fb6'''							,	sequences=lowerCamelCase__							,	)


