



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  (						lowerCAmelCase__	,						unittest.TestCase ):
 lowerCAmelCase__							       =	VideoToVideoSDPipeline
 lowerCAmelCase__							       =	TEXT_GUIDED_IMAGE_VARIATION_PARAMS.union({"""video"""} ) - {"""image""", """width""", """height"""}
 lowerCAmelCase__							       =	TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"""video"""} ) - {"""image"""}
 lowerCAmelCase__							       =	PipelineTesterMixin.required_optional_params - {"""latents"""}
 lowerCAmelCase__							       =	False

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




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    torch.manual_seed(0					)
    __lowerCamelCase    				=     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						,					)
    __lowerCamelCase    				=     DDIMScheduler(
        beta_start=0.00_085						,					beta_end=0.012						,					beta_schedule='''scaled_linear'''						,					clip_sample=__UpperCAmelCase						,					set_alpha_to_one=__UpperCAmelCase						,					)
    torch.manual_seed(0					)
    __lowerCamelCase    				=     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					)
    __lowerCamelCase    				=     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						,					)
    __lowerCamelCase    				=     CLIPTextModel(__UpperCAmelCase					)
    __lowerCamelCase    				=     CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''					)

    __lowerCamelCase    				=     {
        '''unet''': unet,
        '''scheduler''': scheduler,
        '''vae''': vae,
        '''text_encoder''': text_encoder,
        '''tokenizer''': tokenizer,
    }
    return components




 def 							lowerCamelCase   (    self						,					__UpperCAmelCase						,					__UpperCAmelCase=0					):



    '''simple docstring'''
    # 3 frames
    __lowerCamelCase    				=     floats_tensor((1, 3, 3, 32, 32)						,					rng=random.Random(__UpperCAmelCase					)					).to(__UpperCAmelCase					)

    if str(__UpperCAmelCase					).startswith('''mps'''					):
       __lowerCamelCase    				=     torch.manual_seed(__UpperCAmelCase					)
    else:
       __lowerCamelCase    				=     torch.Generator(device=__UpperCAmelCase					).manual_seed(__UpperCAmelCase					)
    __lowerCamelCase    				=     {
        '''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 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __lowerCamelCase    				=     '''cpu'''  # ensure determinism for the device-dependent torch.Generator
    __lowerCamelCase    				=     self.get_dummy_components()
    __lowerCamelCase    				=     VideoToVideoSDPipeline(**__UpperCAmelCase					)
    __lowerCamelCase    				=     sd_pipe.to(__UpperCAmelCase					)
    sd_pipe.set_progress_bar_config(disable=__UpperCAmelCase					)

    __lowerCamelCase    				=     self.get_dummy_inputs(__UpperCAmelCase					)
    __lowerCamelCase    				=     '''np'''
    __lowerCamelCase    				=     sd_pipe(**__UpperCAmelCase					).frames
    __lowerCamelCase    				=     frames[0][-3:, -3:, -1]

    assert frames[0].shape == (32, 32, 3)
    __lowerCamelCase    				=     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 							lowerCamelCase   (    self					):



    '''simple docstring'''
    self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=__UpperCAmelCase						,					expected_max_diff=5E-3					)




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



    '''simple docstring'''
    pass




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



    '''simple docstring'''
    pass




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



    '''simple docstring'''
    pass




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    return super().test_progress_bar()



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




 def 							lowerCamelCase   (    self					):



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

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

    __lowerCamelCase    				=     '''Spiderman is surfing'''

    __lowerCamelCase    				=     pipe(__UpperCAmelCase						,					video=__UpperCAmelCase						,					generator=__UpperCAmelCase						,					num_inference_steps=3						,					output_type='''pt'''					).frames

    __lowerCamelCase    				=     np.array([-1.0_458_984, -1.1_279_297, -0.9_663_086, -0.91_503_906, -0.75_097_656]					)
    assert np.abs(video_frames.cpu().numpy()[0, 0, 0, 0, -5:] - expected_array					).sum() < 1E-2











import os
import re
import unicodedata
from shutil import copyfile
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import is_torch_available, logging


if is_torch_available():
  import torch


if TYPE_CHECKING:
  from transformers.pipelines.conversational import Conversation


a_								=       logging.get_logger(__name__)
a_								=       {"""vocab_file""": """spiece.model"""}

a_								=       {
    """vocab_file""": {
        """AI-Sweden/gpt-sw3-126m""": """https://huggingface.co/AI-Sweden/gpt-sw3-126m/resolve/main/spiece.model""",
        """AI-Sweden/gpt-sw3-350m""": """https://huggingface.co/AI-Sweden/gpt-sw3-350m/resolve/main/spiece.model""",
        """AI-Sweden/gpt-sw3-1.6b""": """https://huggingface.co/AI-Sweden/gpt-sw3-1.6b/resolve/main/spiece.model""",
        """AI-Sweden/gpt-sw3-6.7b""": """https://huggingface.co/AI-Sweden/gpt-sw3-6.7b/resolve/main/spiece.model""",
        """AI-Sweden/gpt-sw3-20b""": """https://huggingface.co/AI-Sweden/gpt-sw3-20b/resolve/main/spiece.model""",
    }
}

a_								=       {
    """AI-Sweden/gpt-sw3-126m""": 2_048,
    """AI-Sweden/gpt-sw3-350m""": 2_048,
    """AI-Sweden/gpt-sw3-1.6b""": 2_048,
    """AI-Sweden/gpt-sw3-6.7b""": 2_048,
    """AI-Sweden/gpt-sw3-20b""": 2_048,
}



class 			__lowerCAmelCase  (						lowerCAmelCase__ ):
 lowerCAmelCase__							       =	VOCAB_FILES_NAMES
 lowerCAmelCase__							       =	PRETRAINED_VOCAB_FILES_MAP
 lowerCAmelCase__							       =	PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 lowerCAmelCase__							       =	["""input_ids""", """attention_mask"""]




 def __init__(    self						,					__UpperCAmelCase						,					__UpperCAmelCase=False						,					__UpperCAmelCase=False						,					__UpperCAmelCase=False						,					__UpperCAmelCase=None						,					__UpperCAmelCase=None						,					__UpperCAmelCase=None						,					__UpperCAmelCase=None						,					__UpperCAmelCase = None						,					**__UpperCAmelCase						,					):



    '''simple docstring'''
    __lowerCamelCase    				=     {} if sp_model_kwargs is None else sp_model_kwargs

    __lowerCamelCase    				=     kwargs.get('''name_or_path'''					)
    if name_or_path is None:
       logger.warning(
           '''name_or_path not provided, will work for all GPTSw3 models except gpt-sw3-7b,'''
           ''' you are testing the model, this can safely be ignored'''					)
       __lowerCamelCase    				=     '''None'''

    # Default definitions for our 2 tokenizer versions, with None-checks to enable proper testing
    __lowerCamelCase    				=     '''<|endoftext|>''' if eos_token is None else eos_token
    __lowerCamelCase    				=     '''<unk>''' if unk_token is None else unk_token
    if "gpt-sw3-7b" in name_or_path:
       __lowerCamelCase    				=     unk_token if pad_token is None else pad_token
       __lowerCamelCase    				=     eos_token if bos_token is None else bos_token
    else:
       __lowerCamelCase    				=     '''<pad>''' if pad_token is None else pad_token
       __lowerCamelCase    				=     '''<s>''' if bos_token is None else bos_token

    super().__init__(
        do_lower_case=__UpperCAmelCase						,					remove_space=__UpperCAmelCase						,					keep_accents=__UpperCAmelCase						,					bos_token=__UpperCAmelCase						,					eos_token=__UpperCAmelCase						,					unk_token=__UpperCAmelCase						,					pad_token=__UpperCAmelCase						,					sp_model_kwargs=self.sp_model_kwargs						,					**__UpperCAmelCase						,					)

    __lowerCamelCase    				=     do_lower_case
    __lowerCamelCase    				=     remove_space
    __lowerCamelCase    				=     keep_accents
    __lowerCamelCase    				=     vocab_file

    __lowerCamelCase    				=     spm.SentencePieceProcessor(**self.sp_model_kwargs					)
    self.sp_model.Load(__UpperCAmelCase					)

    # Used for whitespace normalization in input texts
    # fmt : off
    __lowerCamelCase    				=     {''' ''', ''' ''', ''' ''', ''' ''', ''' ''', '''　''', ''' ''', ''' ''', ''' ''', ''' ''', '''￼''', ''''''}
    # fmt : on

    # Regular expression to remove non-printing characters (e.g. some unicode control chars) in preprocessing
    __lowerCamelCase    				=     re.compile(
        F"""[{"".join(map(__UpperCAmelCase						,					list(range(0						,					9					)					) + list(range(11						,					32					)					) + list(range(127						,					160					)					) + [160, 173, 8203]					)					)}]"""					)




 def __getstate__(    self					):



    '''simple docstring'''
    __lowerCamelCase    				=     self.__dict__.copy()
    __lowerCamelCase    				=     None
    return state




 def __setstate__(    self						,					__UpperCAmelCase					):



    '''simple docstring'''
    __lowerCamelCase    				=     d

    # for backward compatibility
    if not hasattr(self						,					'''sp_model_kwargs'''					):
       __lowerCamelCase    				=     {}

    __lowerCamelCase    				=     spm.SentencePieceProcessor(**self.sp_model_kwargs					)
    self.sp_model.Load(self.vocab_file					)




 @property
 # Copied from transformers.models.albert.tokenization_albert.AlbertTokenizer.vocab_size
 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    return len(self.sp_model					)




 def 							lowerCamelCase   (    self						,					__UpperCAmelCase					):



    '''simple docstring'''
    __lowerCamelCase    				=     self.non_printing_characters_re.sub(''''''						,					__UpperCAmelCase					)

    # Normalize whitespaces
    __lowerCamelCase    				=     ''''''.join([char if char not in self.whitespaces else ''' ''' for char in text]					)

    # NFC Unicode normalization
    __lowerCamelCase    				=     unicodedata.normalize('''NFC'''						,					__UpperCAmelCase					)
    return text




 def 							lowerCamelCase   (    self						,					__UpperCAmelCase						,					**__UpperCAmelCase					):



    '''simple docstring'''
    __lowerCamelCase    				=     self.preprocess_text(__UpperCAmelCase					)
    return self.sp_model.encode(__UpperCAmelCase						,					out_type=__UpperCAmelCase					)




 def 							lowerCamelCase   (    self						,					__UpperCAmelCase					):



    '''simple docstring'''
    return self.sp_model.PieceToId(__UpperCAmelCase					)




 def 							lowerCamelCase   (    self						,					__UpperCAmelCase					):



    '''simple docstring'''
    return self.sp_model.IdToPiece(__UpperCAmelCase					)




 @staticmethod
 def 							lowerCamelCase   (    __UpperCAmelCase					):



    '''simple docstring'''
    return out_string




 def 							lowerCamelCase   (    self						,					__UpperCAmelCase					):



    '''simple docstring'''
    __lowerCamelCase    				=     []
    __lowerCamelCase    				=     ''''''
    __lowerCamelCase    				=     False
    for token in tokens:
       # make sure that special tokens are not decoded using sentencepiece model
       if token in self.all_special_tokens:
          # TODO: Check if this is needed, as it ensures that decode(encode(doc)) != doc by adding extra whitespace in the decoded document
          if not prev_is_special:
             out_string += " "

          out_string += self.sp_model.decode(__UpperCAmelCase					) + token
          __lowerCamelCase    				=     True
          __lowerCamelCase    				=     []
       else:
          current_sub_tokens.append(__UpperCAmelCase					)
          __lowerCamelCase    				=     False
    out_string += self.sp_model.decode(__UpperCAmelCase					)

    return out_string




 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __lowerCamelCase    				=     {self.convert_ids_to_tokens(__UpperCAmelCase					): i for i in range(self.vocab_size					)}
    vocab.update(self.added_tokens_encoder					)
    return vocab




 def 							lowerCamelCase   (    self						,					__UpperCAmelCase						,					__UpperCAmelCase = None					):



    '''simple docstring'''
    if not os.path.isdir(__UpperCAmelCase					):
       logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""					)
       return
    __lowerCamelCase    				=     os.path.join(
        __UpperCAmelCase						,					(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']					)

    if os.path.abspath(self.vocab_file					) != os.path.abspath(__UpperCAmelCase					) and os.path.isfile(self.vocab_file					):
       copyfile(self.vocab_file						,					__UpperCAmelCase					)
    elif not os.path.isfile(self.vocab_file					):
       with open(__UpperCAmelCase						,					'''wb'''					) as fi:
          __lowerCamelCase    				=     self.sp_model.serialized_model_proto()
          fi.write(__UpperCAmelCase					)

    return (out_vocab_file,)




 def 							lowerCamelCase   (    self						,					__UpperCAmelCase						,					__UpperCAmelCase = False					):



    '''simple docstring'''

    if isinstance(__UpperCAmelCase						,					__UpperCAmelCase					):
       __lowerCamelCase    				=     self.preprocess_text(__UpperCAmelCase					)
       __lowerCamelCase    				=     self.sp_model.encode(__UpperCAmelCase					)
    else:
       __lowerCamelCase    				=     [self.preprocess_text(__UpperCAmelCase					) for t in text]
       __lowerCamelCase    				=     self.sp_model.encode(__UpperCAmelCase					)

    if return_tensors is True or return_tensors == "pt":
       __lowerCamelCase    				=     torch.tensor(__UpperCAmelCase					)

    return token_ids




 def 							lowerCamelCase   (    self						,					__UpperCAmelCase					):



    '''simple docstring'''
    return self.sp_model.decode(__UpperCAmelCase					)




 def 							lowerCamelCase   (    self						,					__UpperCAmelCase					):



    '''simple docstring'''
    __lowerCamelCase    				=     [F"""User: {text}""" if is_user else F"""Bot: {text}""" for is_user, text in conversation.iter_texts()]
    __lowerCamelCase    				=     (
        F"""{self.eos_token}{self.bos_token}""" + F"""{self.bos_token}""".join(__UpperCAmelCase					) + F"""{self.bos_token}Bot:"""
    )
    return self.encode(text=__UpperCAmelCase					)






