


import gc
import random
import unittest

import numpy as np
import torch
from transformers import XLMRobertaTokenizer

from diffusers import (
    AltDiffusionImgaImgPipeline,
    AutoencoderKL,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.image_processor import VaeImageProcessor
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
    RobertaSeriesConfig,
    RobertaSeriesModelWithTransformation,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()







class        snake_case_   (unittest.TestCase      ):
					def  lowerCamelCase__(      self   :int      )					->    int:
												# clean up the VRAM after each test
												super().tearDown()
												gc.collect()
												torch.cuda.empty_cache()
					@property
					def  lowerCamelCase__(      self   :List[Any]      )					->    Tuple:
												a__             =     1
												a__             =     3
												a__             =     (32, 32)

												a__             =     floats_tensor((batch_size, num_channels) + sizes			,rng=random.Random(0      )      ).to(__snake_case      )
												return image
					@property
					def  lowerCamelCase__(      self   :Optional[int]      )					->    List[Any]:
												torch.manual_seed(0      )
												a__             =     UNetaDConditionModel(
												    block_out_channels=(32, 64)			,layers_per_block=2			,sample_size=32			,in_channels=4			,out_channels=4			,down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')			,up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')			,cross_attention_dim=32			,)
												return model
					@property
					def  lowerCamelCase__(      self   :int      )					->    str:
												torch.manual_seed(0      )
												a__             =     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			,)
												return model
					@property
					def  lowerCamelCase__(      self   :Optional[int]      )					->    Dict:
												torch.manual_seed(0      )
												a__             =     RobertaSeriesConfig(
												    hidden_size=32			,project_dim=32			,intermediate_size=37			,layer_norm_eps=1E-05			,num_attention_heads=4			,num_hidden_layers=5			,pad_token_id=1			,vocab_size=50_06			,)
												return RobertaSeriesModelWithTransformation(__snake_case      )
					@property
					def  lowerCamelCase__(      self   :Optional[int]      )					->    int:
												def extract(*__snake_case   :Any			,**__snake_case   :str      ):







																			class        snake_case_   :
																								def __init__(      self   :Any      )					->    int:
																															a__             =     torch.ones([0]      )



																								def  lowerCamelCase__(      self   :Any			,__snake_case   :Tuple      )					->    Optional[int]:
																															self.pixel_values.to(__snake_case      )
																															return self





																			return Out()

												return extract
					def  lowerCamelCase__(      self   :Union[str, Any]      )					->    List[str]:
												a__             =     'cpu'  # ensure determinism for the device-dependent torch.Generator
												a__             =     self.dummy_cond_unet
												a__             =     PNDMScheduler(skip_prk_steps=__snake_case      )
												a__             =     self.dummy_vae
												a__             =     self.dummy_text_encoder
												a__             =     XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta'      )
												a__             =     77

												a__             =     self.dummy_image.to(__snake_case      )
												a__             =     init_image / 2 + 0.5

												# make sure here that pndm scheduler skips prk
												a__             =     AltDiffusionImgaImgPipeline(
												    unet=__snake_case			,scheduler=__snake_case			,vae=__snake_case			,text_encoder=__snake_case			,tokenizer=__snake_case			,safety_checker=__snake_case			,feature_extractor=self.dummy_extractor			,)
												a__             =     VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor			,do_normalize=__snake_case      )
												a__             =     alt_pipe.to(__snake_case      )
												alt_pipe.set_progress_bar_config(disable=__snake_case      )

												a__             =     'A painting of a squirrel eating a burger'
												a__             =     torch.Generator(device=__snake_case      ).manual_seed(0      )
												a__             =     alt_pipe(
												    [prompt]			,generator=__snake_case			,guidance_scale=6.0			,num_inference_steps=2			,output_type='np'			,image=__snake_case			,)

												a__             =     output.images

												a__             =     torch.Generator(device=__snake_case      ).manual_seed(0      )
												a__             =     alt_pipe(
												    [prompt]			,generator=__snake_case			,guidance_scale=6.0			,num_inference_steps=2			,output_type='np'			,image=__snake_case			,return_dict=__snake_case			,)[0]

												a__             =     image[0, -3:, -3:, -1]
												a__             =     image_from_tuple[0, -3:, -3:, -1]

												assert image.shape == (1, 32, 32, 3)
												a__             =     np.array([0.44_27, 0.37_31, 0.42_49, 0.49_41, 0.45_46, 0.41_48, 0.41_93, 0.46_66, 0.44_99]      )

												assert np.abs(image_slice.flatten() - expected_slice      ).max() < 5E-3
												assert np.abs(image_from_tuple_slice.flatten() - expected_slice      ).max() < 5E-3
					@unittest.skipIf(torch_device != 'cuda'			,'This test requires a GPU'      )
					def  lowerCamelCase__(      self   :str      )					->    Tuple:
												a__             =     self.dummy_cond_unet
												a__             =     PNDMScheduler(skip_prk_steps=__snake_case      )
												a__             =     self.dummy_vae
												a__             =     self.dummy_text_encoder
												a__             =     XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta'      )
												a__             =     77

												a__             =     self.dummy_image.to(__snake_case      )

												# put models in fp16
												a__             =     unet.half()
												a__             =     vae.half()
												a__             =     bert.half()

												# make sure here that pndm scheduler skips prk
												a__             =     AltDiffusionImgaImgPipeline(
												    unet=__snake_case			,scheduler=__snake_case			,vae=__snake_case			,text_encoder=__snake_case			,tokenizer=__snake_case			,safety_checker=__snake_case			,feature_extractor=self.dummy_extractor			,)
												a__             =     VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor			,do_normalize=__snake_case      )
												a__             =     alt_pipe.to(__snake_case      )
												alt_pipe.set_progress_bar_config(disable=__snake_case      )

												a__             =     'A painting of a squirrel eating a burger'
												a__             =     torch.manual_seed(0      )
												a__             =     alt_pipe(
												    [prompt]			,generator=__snake_case			,num_inference_steps=2			,output_type='np'			,image=__snake_case			,).images

												assert image.shape == (1, 32, 32, 3)



					@unittest.skipIf(torch_device != 'cuda'			,'This test requires a GPU'      )
					def  lowerCamelCase__(      self   :str      )					->    Optional[int]:
												a__             =     load_image(
												    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
												    '/img2img/sketch-mountains-input.jpg'      )
												# resize to resolution that is divisible by 8 but not 16 or 32
												a__             =     init_image.resize((7_60, 5_04)      )

												a__             =     'BAAI/AltDiffusion'
												a__             =     AltDiffusionImgaImgPipeline.from_pretrained(
												    __snake_case			,safety_checker=__snake_case			,)
												pipe.to(__snake_case      )
												pipe.set_progress_bar_config(disable=__snake_case      )
												pipe.enable_attention_slicing()

												a__             =     'A fantasy landscape, trending on artstation'

												a__             =     torch.manual_seed(0      )
												a__             =     pipe(
												    prompt=__snake_case			,image=__snake_case			,strength=0.75			,guidance_scale=7.5			,generator=__snake_case			,output_type='np'			,)
												a__             =     output.images[0]

												a__             =     image[2_55:2_58, 3_83:3_86, -1]

												assert image.shape == (5_04, 7_60, 3)
												a__             =     np.array([0.93_58, 0.93_97, 0.95_99, 0.99_01, 1.00_00, 1.00_00, 0.98_82, 1.00_00, 1.00_00]      )

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







@slow
@require_torch_gpu
class        snake_case_   (unittest.TestCase      ):
					def  lowerCamelCase__(      self   :List[Any]      )					->    Tuple:
												# clean up the VRAM after each test
												super().tearDown()
												gc.collect()
												torch.cuda.empty_cache()



					def  lowerCamelCase__(      self   :Tuple      )					->    Optional[int]:
												a__             =     load_image(
												    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
												    '/img2img/sketch-mountains-input.jpg'      )
												a__             =     init_image.resize((7_68, 5_12)      )
												a__             =     load_numpy(
												    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy'      )

												a__             =     'BAAI/AltDiffusion'
												a__             =     AltDiffusionImgaImgPipeline.from_pretrained(
												    __snake_case			,safety_checker=__snake_case			,)
												pipe.to(__snake_case      )
												pipe.set_progress_bar_config(disable=__snake_case      )
												pipe.enable_attention_slicing()

												a__             =     'A fantasy landscape, trending on artstation'

												a__             =     torch.manual_seed(0      )
												a__             =     pipe(
												    prompt=__snake_case			,image=__snake_case			,strength=0.75			,guidance_scale=7.5			,generator=__snake_case			,output_type='np'			,)
												a__             =     output.images[0]

												assert image.shape == (5_12, 7_68, 3)
												# img2img is flaky across GPUs even in fp32, so using MAE here
												assert np.abs(expected_image - image      ).max() < 1E-2








import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_distilbert import DistilBertTokenizer


snake_case							:       Dict     			=      logging.get_logger(__name__)

snake_case							:       Dict     			=      {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''}

snake_case							:       List[Any]     			=      {
    '''vocab_file''': {
        '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/vocab.txt''',
        '''distilbert-base-uncased-distilled-squad''': (
            '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/vocab.txt'''
        ),
        '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/vocab.txt''',
        '''distilbert-base-cased-distilled-squad''': (
            '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/vocab.txt'''
        ),
        '''distilbert-base-german-cased''': '''https://huggingface.co/distilbert-base-german-cased/resolve/main/vocab.txt''',
        '''distilbert-base-multilingual-cased''': (
            '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/vocab.txt'''
        ),
    },
    '''tokenizer_file''': {
        '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/tokenizer.json''',
        '''distilbert-base-uncased-distilled-squad''': (
            '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/tokenizer.json'''
        ),
        '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/tokenizer.json''',
        '''distilbert-base-cased-distilled-squad''': (
            '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json'''
        ),
        '''distilbert-base-german-cased''': (
            '''https://huggingface.co/distilbert-base-german-cased/resolve/main/tokenizer.json'''
        ),
        '''distilbert-base-multilingual-cased''': (
            '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/tokenizer.json'''
        ),
    },
}

snake_case							:       int     			=      {
    '''distilbert-base-uncased''': 5_12,
    '''distilbert-base-uncased-distilled-squad''': 5_12,
    '''distilbert-base-cased''': 5_12,
    '''distilbert-base-cased-distilled-squad''': 5_12,
    '''distilbert-base-german-cased''': 5_12,
    '''distilbert-base-multilingual-cased''': 5_12,
}


snake_case							:       Union[str, Any]     			=      {
    '''distilbert-base-uncased''': {'''do_lower_case''': True},
    '''distilbert-base-uncased-distilled-squad''': {'''do_lower_case''': True},
    '''distilbert-base-cased''': {'''do_lower_case''': False},
    '''distilbert-base-cased-distilled-squad''': {'''do_lower_case''': False},
    '''distilbert-base-german-cased''': {'''do_lower_case''': False},
    '''distilbert-base-multilingual-cased''': {'''do_lower_case''': False},
}







class        snake_case_   (lowerCamelCase_      ):
					UpperCAmelCase__					:    Any          =			VOCAB_FILES_NAMES
					UpperCAmelCase__					:    str          =			PRETRAINED_VOCAB_FILES_MAP
					UpperCAmelCase__					:    int          =			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
					UpperCAmelCase__					:    Tuple          =			PRETRAINED_INIT_CONFIGURATION
					UpperCAmelCase__					:    Optional[Any]          =			['''input_ids''', '''attention_mask''']
					UpperCAmelCase__					:    Optional[int]          =			DistilBertTokenizer
					def __init__(      self   :Dict			,__snake_case   :Dict=None			,__snake_case   :Optional[Any]=None			,__snake_case   :Optional[Any]=True			,__snake_case   :List[Any]="[UNK]"			,__snake_case   :str="[SEP]"			,__snake_case   :List[Any]="[PAD]"			,__snake_case   :Tuple="[CLS]"			,__snake_case   :Optional[int]="[MASK]"			,__snake_case   :Dict=True			,__snake_case   :Dict=None			,**__snake_case   :List[Any]			,)					->    Optional[int]:
												super().__init__(
												    __snake_case			,tokenizer_file=__snake_case			,do_lower_case=__snake_case			,unk_token=__snake_case			,sep_token=__snake_case			,pad_token=__snake_case			,cls_token=__snake_case			,mask_token=__snake_case			,tokenize_chinese_chars=__snake_case			,strip_accents=__snake_case			,**__snake_case			,)

												a__             =     json.loads(self.backend_tokenizer.normalizer.__getstate__()      )
												if (
												    normalizer_state.get('lowercase'			,__snake_case      ) != do_lower_case
												    or normalizer_state.get('strip_accents'			,__snake_case      ) != strip_accents
												    or normalizer_state.get('handle_chinese_chars'			,__snake_case      ) != tokenize_chinese_chars
												):
																			a__             =     getattr(__snake_case			,normalizer_state.pop('type'      )      )
																			a__             =     do_lower_case
																			a__             =     strip_accents
																			a__             =     tokenize_chinese_chars
																			a__             =     normalizer_class(**__snake_case      )

												a__             =     do_lower_case
					def  lowerCamelCase__(      self   :Any			,__snake_case   :List[str]			,__snake_case   :int=None      )					->    Dict:
												a__             =     [self.cls_token_id] + token_ids_a + [self.sep_token_id]

												if token_ids_a:
																			output += token_ids_a + [self.sep_token_id]

												return output
					def  lowerCamelCase__(      self   :List[str]			,__snake_case   :List[int]			,__snake_case   :Optional[List[int]] = None      )					->    List[int]:
												a__             =     [self.sep_token_id]
												a__             =     [self.cls_token_id]
												if token_ids_a is None:
																			return len(cls + token_ids_a + sep      ) * [0]
												return len(cls + token_ids_a + sep      ) * [0] + len(token_ids_a + sep      ) * [1]



					def  lowerCamelCase__(      self   :Union[str, Any]			,__snake_case   :str			,__snake_case   :Optional[str] = None      )					->    Tuple[str]:
												a__             =     self._tokenizer.model.save(__snake_case			,name=__snake_case      )
												return tuple(__snake_case      )




