






import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


snake_case     					=		logging.get_logger(__name__)

snake_case     					=		{
    """BAAI/AltCLIP""": """https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json""",
    # See all AltCLIP models at https://huggingface.co/models?filter=altclip
}


class 			SCREAMING_SNAKE_CASE      (      lowerCAmelCase       ):
     '''simple docstring'''


     UpperCamelCase_				:		List[str]								=						'''altclip_text_model'''


     def __init__(					self							:						Optional[int]       ,			UpperCAmelCase_							:						List[Any]=25_0002       ,			UpperCAmelCase_							:						Any=1024       ,			UpperCAmelCase_							:						Optional[int]=24       ,			UpperCAmelCase_							:						str=16       ,			UpperCAmelCase_							:						Dict=4096       ,			UpperCAmelCase_							:						Tuple="gelu"       ,			UpperCAmelCase_							:						Optional[int]=0.1       ,			UpperCAmelCase_							:						int=0.1       ,			UpperCAmelCase_							:						int=514       ,			UpperCAmelCase_							:						str=1       ,			UpperCAmelCase_							:						List[str]=0.02       ,			UpperCAmelCase_							:						Dict=0.02       ,			UpperCAmelCase_							:						Tuple=1E-05       ,			UpperCAmelCase_							:						Tuple=1       ,			UpperCAmelCase_							:						List[str]=0       ,			UpperCAmelCase_							:						Optional[int]=2       ,			UpperCAmelCase_							:						int="absolute"       ,			UpperCAmelCase_							:						Optional[int]=True       ,			UpperCAmelCase_							:						Tuple=768       ,			**UpperCAmelCase_							:						Any       ,			):
       super().__init__(pad_token_id=UpperCAmelCase_       ,			bos_token_id=UpperCAmelCase_       ,			eos_token_id=UpperCAmelCase_       ,			**UpperCAmelCase_   )

       SCREAMING_SNAKE_CASE							:   str						    =     vocab_size
       SCREAMING_SNAKE_CASE							:   Any						    =     hidden_size
       SCREAMING_SNAKE_CASE							:   List[Any]						    =     num_hidden_layers
       SCREAMING_SNAKE_CASE							:   Dict						    =     num_attention_heads
       SCREAMING_SNAKE_CASE							:   Union[str, Any]						    =     hidden_act
       SCREAMING_SNAKE_CASE							:   Optional[Any]						    =     intermediate_size
       SCREAMING_SNAKE_CASE							:   Tuple						    =     hidden_dropout_prob
       SCREAMING_SNAKE_CASE							:   List[Any]						    =     attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE							:   Optional[int]						    =     max_position_embeddings
       SCREAMING_SNAKE_CASE							:   Optional[int]						    =     type_vocab_size
       SCREAMING_SNAKE_CASE							:   Any						    =     initializer_range
       SCREAMING_SNAKE_CASE							:   Tuple						    =     initializer_factor
       SCREAMING_SNAKE_CASE							:   Any						    =     layer_norm_eps
       SCREAMING_SNAKE_CASE							:   List[Any]						    =     position_embedding_type
       SCREAMING_SNAKE_CASE							:   Union[str, Any]						    =     use_cache
       SCREAMING_SNAKE_CASE							:   int						    =     project_dim





class 			SCREAMING_SNAKE_CASE      (      lowerCAmelCase       ):
     '''simple docstring'''


     UpperCamelCase_				:		Tuple								=						'''altclip_vision_model'''


     def __init__(					self							:						str       ,			UpperCAmelCase_							:						Union[str, Any]=768       ,			UpperCAmelCase_							:						List[Any]=3072       ,			UpperCAmelCase_							:						List[Any]=512       ,			UpperCAmelCase_							:						Dict=12       ,			UpperCAmelCase_							:						Union[str, Any]=12       ,			UpperCAmelCase_							:						Optional[int]=3       ,			UpperCAmelCase_							:						Optional[Any]=224       ,			UpperCAmelCase_							:						Optional[int]=32       ,			UpperCAmelCase_							:						Any="quick_gelu"       ,			UpperCAmelCase_							:						Optional[int]=1E-5       ,			UpperCAmelCase_							:						Tuple=0.0       ,			UpperCAmelCase_							:						Any=0.02       ,			UpperCAmelCase_							:						Any=1.0       ,			**UpperCAmelCase_							:						Any       ,			):
       super().__init__(**UpperCAmelCase_   )

       SCREAMING_SNAKE_CASE							:   int						    =     hidden_size
       SCREAMING_SNAKE_CASE							:   List[Any]						    =     intermediate_size
       SCREAMING_SNAKE_CASE							:   Tuple						    =     projection_dim
       SCREAMING_SNAKE_CASE							:   Optional[Any]						    =     num_hidden_layers
       SCREAMING_SNAKE_CASE							:   List[Any]						    =     num_attention_heads
       SCREAMING_SNAKE_CASE							:   Any						    =     num_channels
       SCREAMING_SNAKE_CASE							:   List[Any]						    =     patch_size
       SCREAMING_SNAKE_CASE							:   str						    =     image_size
       SCREAMING_SNAKE_CASE							:   List[Any]						    =     initializer_range
       SCREAMING_SNAKE_CASE							:   Any						    =     initializer_factor
       SCREAMING_SNAKE_CASE							:   Any						    =     attention_dropout
       SCREAMING_SNAKE_CASE							:   Tuple						    =     layer_norm_eps
       SCREAMING_SNAKE_CASE							:   Optional[int]						    =     hidden_act






     @classmethod
     def 					_A							(					cls							:						Any       ,			UpperCAmelCase_							:						Union[str, os.PathLike]       ,			**UpperCAmelCase_							:						str   ):
       cls._set_token_in_kwargs(UpperCAmelCase_   )

       SCREAMING_SNAKE_CASE    ,	SCREAMING_SNAKE_CASE							:   Any						    =     cls.get_config_dict(UpperCAmelCase_       ,			**UpperCAmelCase_   )

       # get the vision config dict if we are loading from AltCLIPConfig
       if config_dict.get("model_type"   ) == "altclip":
         SCREAMING_SNAKE_CASE							:   Union[str, Any]						    =     config_dict["vision_config"]

       if "model_type" in config_dict and hasattr(cls       ,			"model_type"   ) and config_dict["model_type"] != cls.model_type:
         logger.warning(
             f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
             f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''   )

       return cls.from_dict(UpperCAmelCase_       ,			**UpperCAmelCase_   )





class 			SCREAMING_SNAKE_CASE      (      lowerCAmelCase       ):
     '''simple docstring'''


     UpperCamelCase_				:		List[str]								=						'''altclip'''
     UpperCamelCase_				:		List[Any]								=						True


     def __init__(					self							:						str       ,			UpperCAmelCase_							:						Dict=None       ,			UpperCAmelCase_							:						Dict=None       ,			UpperCAmelCase_							:						Any=768       ,			UpperCAmelCase_							:						str=2.6_592       ,			**UpperCAmelCase_							:						Optional[Any]   ):
       # If `_config_dict` exist, we use them for the backward compatibility.
       # We pop out these 2 attributes before calling `super().__init__` to avoid them being saved (which causes a lot
       # of confusion!).
       SCREAMING_SNAKE_CASE							:   Optional[int]						    =     kwargs.pop("text_config_dict"       ,			UpperCAmelCase_   )
       SCREAMING_SNAKE_CASE							:   Union[str, Any]						    =     kwargs.pop("vision_config_dict"       ,			UpperCAmelCase_   )

       super().__init__(**UpperCAmelCase_   )

       # Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in
       # `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most
       # cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`.
       if text_config_dict is not None:
         if text_config is None:
           SCREAMING_SNAKE_CASE							:   Optional[Any]						    =     {}

         # This is the complete result when using `text_config_dict`.
         SCREAMING_SNAKE_CASE							:   Union[str, Any]						    =     AltCLIPTextConfig(**UpperCAmelCase_   ).to_dict()

         # Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different.
         for key, value in _text_config_dict.items():
           if key in text_config and value != text_config[key] and key not in ["transformers_version"]:
             # If specified in `text_config_dict`
             if key in text_config_dict:
               SCREAMING_SNAKE_CASE							:   int						    =     (
                   f'''`{key}` is found in both `text_config_dict` and `text_config` but with different values. '''
                   f'''The value `text_config_dict["{key}"]` will be used instead.'''
               )
             # If inferred from default argument values (just to be super careful)
             else:
               SCREAMING_SNAKE_CASE							:   Any						    =     (
                   f'''`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The '''
                   f'''value `text_config["{key}"]` will be overriden.'''
               )
             logger.warning(UpperCAmelCase_   )

            # Update all values in `text_config` with the ones in `_text_config_dict`.
         text_config.update(_text_config_dict   )

       if vision_config_dict is not None:
         if vision_config is None:
           SCREAMING_SNAKE_CASE							:   Dict						    =     {}

         # This is the complete result when using `vision_config_dict`.
         SCREAMING_SNAKE_CASE							:   Any						    =     AltCLIPVisionConfig(**UpperCAmelCase_   ).to_dict()
         # convert keys to string instead of integer
         if "id2label" in _vision_config_dict:
           SCREAMING_SNAKE_CASE							:   List[str]						    =     {
               str(UpperCAmelCase_   ): value for key, value in _vision_config_dict["id2label"].items()
           }

         # Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different.
         for key, value in _vision_config_dict.items():
           if key in vision_config and value != vision_config[key] and key not in ["transformers_version"]:
             # If specified in `vision_config_dict`
             if key in vision_config_dict:
               SCREAMING_SNAKE_CASE							:   Union[str, Any]						    =     (
                   f'''`{key}` is found in both `vision_config_dict` and `vision_config` but with different '''
                   f'''values. The value `vision_config_dict["{key}"]` will be used instead.'''
               )
             # If inferred from default argument values (just to be super careful)
             else:
               SCREAMING_SNAKE_CASE							:   Tuple						    =     (
                   f'''`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. '''
                   f'''The value `vision_config["{key}"]` will be overriden.'''
               )
             logger.warning(UpperCAmelCase_   )

            # Update all values in `vision_config` with the ones in `_vision_config_dict`.
         vision_config.update(_vision_config_dict   )

       if text_config is None:
         SCREAMING_SNAKE_CASE							:   Tuple						    =     {}
         logger.info("`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values."   )

       if vision_config is None:
         SCREAMING_SNAKE_CASE							:   Dict						    =     {}
         logger.info("`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values."   )

       SCREAMING_SNAKE_CASE							:   Optional[int]						    =     AltCLIPTextConfig(**UpperCAmelCase_   )
       SCREAMING_SNAKE_CASE							:   Tuple						    =     AltCLIPVisionConfig(**UpperCAmelCase_   )

       SCREAMING_SNAKE_CASE							:   Dict						    =     projection_dim
       SCREAMING_SNAKE_CASE							:   Union[str, Any]						    =     logit_scale_init_value
       SCREAMING_SNAKE_CASE							:   Optional[Any]						    =     1.0


     @classmethod
     def 					_A							(					cls							:						Union[str, Any]       ,			UpperCAmelCase_							:						AltCLIPTextConfig       ,			UpperCAmelCase_							:						AltCLIPVisionConfig       ,			**UpperCAmelCase_							:						int   ):
       return cls(text_config=text_config.to_dict()       ,			vision_config=vision_config.to_dict()       ,			**UpperCAmelCase_   )






     def 					_A							(					self							:						Optional[int]   ):
       SCREAMING_SNAKE_CASE							:   Dict						    =     copy.deepcopy(self.__dict__   )
       SCREAMING_SNAKE_CASE							:   int						    =     self.text_config.to_dict()
       SCREAMING_SNAKE_CASE							:   Optional[int]						    =     self.vision_config.to_dict()
       SCREAMING_SNAKE_CASE							:   Dict						    =     self.__class__.model_type
       return output









'''simple docstring'''

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		):






				"""simple docstring"""



				def   a__					(    self       )     ->							int:
											# clean up the VRAM after each test
											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()



				@property
				def   a__					(    self       )     ->							Optional[int]:
											SCREAMING_SNAKE_CASE_				=				1
											SCREAMING_SNAKE_CASE_				=				3
											SCREAMING_SNAKE_CASE_				=				(32, 32)

											SCREAMING_SNAKE_CASE_				=				floats_tensor((batch_size, num_channels) + sizes,     rng=random.Random(0       )       ).to(_lowercase       )
											return image



				@property
				def   a__					(    self       )     ->							Any:
											torch.manual_seed(0       )
											SCREAMING_SNAKE_CASE_				=				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   a__					(    self       )     ->							Tuple:
											torch.manual_seed(0       )
											SCREAMING_SNAKE_CASE_				=				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   a__					(    self       )     ->							Tuple:
											torch.manual_seed(0       )
											SCREAMING_SNAKE_CASE_				=				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=5006,     )
											return RobertaSeriesModelWithTransformation(_lowercase       )



				@property
				def   a__					(    self       )     ->							List[str]:
											def extract(*_lowercase,     **_lowercase       ):






																		class    snake_case     :






																						"""simple docstring"""



																						def __init__(    self       )     ->							Dict:
																													SCREAMING_SNAKE_CASE_				=				torch.ones([0]       )





																						def   a__					(    self,     _lowercase       )     ->							List[str]:
																													self.pixel_values.to(_lowercase       )
																													return self


																		return Out()

											return extract



				def   a__					(    self       )     ->							Tuple:
											SCREAMING_SNAKE_CASE_				=				'cpu'  # ensure determinism for the device-dependent torch.Generator
											SCREAMING_SNAKE_CASE_				=				self.dummy_cond_unet
											SCREAMING_SNAKE_CASE_				=				PNDMScheduler(skip_prk_steps=_lowercase       )
											SCREAMING_SNAKE_CASE_				=				self.dummy_vae
											SCREAMING_SNAKE_CASE_				=				self.dummy_text_encoder
											SCREAMING_SNAKE_CASE_				=				XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta'       )
											SCREAMING_SNAKE_CASE_				=				77

											SCREAMING_SNAKE_CASE_				=				self.dummy_image.to(_lowercase       )
											SCREAMING_SNAKE_CASE_				=				init_image / 2 + 0.5

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

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

											SCREAMING_SNAKE_CASE_				=				output.images

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

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

											assert image.shape == (1, 32, 32, 3)
											SCREAMING_SNAKE_CASE_				=				np.array([0.4_427, 0.3_731, 0.4_249, 0.4_941, 0.4_546, 0.4_148, 0.4_193, 0.4_666, 0.4_499]       )

											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   a__					(    self       )     ->							str:
											SCREAMING_SNAKE_CASE_				=				self.dummy_cond_unet
											SCREAMING_SNAKE_CASE_				=				PNDMScheduler(skip_prk_steps=_lowercase       )
											SCREAMING_SNAKE_CASE_				=				self.dummy_vae
											SCREAMING_SNAKE_CASE_				=				self.dummy_text_encoder
											SCREAMING_SNAKE_CASE_				=				XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta'       )
											SCREAMING_SNAKE_CASE_				=				77

											SCREAMING_SNAKE_CASE_				=				self.dummy_image.to(_lowercase       )

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

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

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

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





				@unittest.skipIf(torch_device != 'cuda',     'This test requires a GPU'       )
				def   a__					(    self       )     ->							Optional[Any]:
											SCREAMING_SNAKE_CASE_				=				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
											SCREAMING_SNAKE_CASE_				=				init_image.resize((760, 504)       )

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

											SCREAMING_SNAKE_CASE_				=				'A fantasy landscape, trending on artstation'

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

											SCREAMING_SNAKE_CASE_				=				image[255:258, 383:386, -1]

											assert image.shape == (504, 760, 3)
											SCREAMING_SNAKE_CASE_				=				np.array([0.9_358, 0.9_397, 0.9_599, 0.9_901, 1.0_000, 1.0_000, 0.9_882, 1.0_000, 1.0_000]       )

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






@slow
@require_torch_gpu
class    snake_case     (      unittest.TestCase		):






				"""simple docstring"""



				def   a__					(    self       )     ->							List[Any]:
											# clean up the VRAM after each test
											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()





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

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

											SCREAMING_SNAKE_CASE_				=				'A fantasy landscape, trending on artstation'

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

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





