





"""simple docstring"""


import gc
import unittest

import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DDPMScheduler,
    PriorTransformer,
    StableUnCLIPPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion.stable_unclip_image_normalizer import StableUnCLIPImageNormalizer
from diffusers.utils.testing_utils import enable_full_determinism, load_numpy, require_torch_gpu, slow, torch_device

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import (
    PipelineKarrasSchedulerTesterMixin,
    PipelineLatentTesterMixin,
    PipelineTesterMixin,
    assert_mean_pixel_difference,
)


enable_full_determinism()






class 							lowercase__			(   __lowerCAmelCase					,     __lowerCAmelCase					,     __lowerCAmelCase					,     unittest.TestCase	):

			"""simple docstring"""
			__lowerCAmelCase     :				Optional[int]										=  StableUnCLIPPipeline
			__lowerCAmelCase     :				List[Any]										=  TEXT_TO_IMAGE_PARAMS
			__lowerCAmelCase     :				int										=  TEXT_TO_IMAGE_BATCH_PARAMS
			__lowerCAmelCase     :				Union[str, Any]										=  TEXT_TO_IMAGE_IMAGE_PARAMS
			__lowerCAmelCase     :				Union[str, Any]										=  TEXT_TO_IMAGE_IMAGE_PARAMS

			# TODO(will) Expected attn_bias.stride(1) == 0 to be true, but got false
			__lowerCAmelCase     :				Dict										=  False







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		Any				   =			3_2
									UpperCamelCase				:		Optional[Any]				   =			embedder_hidden_size

									# prior components

									torch.manual_seed(0    )
									UpperCamelCase				:		Any				   =			CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""    )

									torch.manual_seed(0    )
									UpperCamelCase				:		Optional[Any]				   =			CLIPTextModelWithProjection(
									    CLIPTextConfig(
									        bos_token_id=0						,   eos_token_id=2						,   hidden_size=_UpperCAmelCase						,   projection_dim=_UpperCAmelCase						,   intermediate_size=3_7						,   layer_norm_eps=1e-0_5						,   num_attention_heads=4						,   num_hidden_layers=5						,   pad_token_id=1						,   vocab_size=1_0_0_0						,   )    )

									torch.manual_seed(0    )
									UpperCamelCase				:		int				   =			PriorTransformer(
									    num_attention_heads=2						,   attention_head_dim=1_2						,   embedding_dim=_UpperCAmelCase						,   num_layers=1						,   )

									torch.manual_seed(0    )
									UpperCamelCase				:		Tuple				   =			DDPMScheduler(
									    variance_type="""fixed_small_log"""						,   prediction_type="""sample"""						,   num_train_timesteps=1_0_0_0						,   clip_sample=_UpperCAmelCase						,   clip_sample_range=5.0						,   beta_schedule="""squaredcos_cap_v2"""						,   )

									# regular denoising components

									torch.manual_seed(0    )
									UpperCamelCase				:		Optional[int]				   =			StableUnCLIPImageNormalizer(embedding_dim=_UpperCAmelCase    )
									UpperCamelCase				:		List[Any]				   =			DDPMScheduler(beta_schedule="""squaredcos_cap_v2"""    )

									torch.manual_seed(0    )
									UpperCamelCase				:		Optional[int]				   =			CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""    )

									torch.manual_seed(0    )
									UpperCamelCase				:		List[str]				   =			CLIPTextModel(
									    CLIPTextConfig(
									        bos_token_id=0						,   eos_token_id=2						,   hidden_size=_UpperCAmelCase						,   projection_dim=3_2						,   intermediate_size=3_7						,   layer_norm_eps=1e-0_5						,   num_attention_heads=4						,   num_hidden_layers=5						,   pad_token_id=1						,   vocab_size=1_0_0_0						,   )    )

									torch.manual_seed(0    )
									UpperCamelCase				:		List[Any]				   =			UNetaDConditionModel(
									    sample_size=3_2						,   in_channels=4						,   out_channels=4						,   down_block_types=("""CrossAttnDownBlock2D""", """DownBlock2D""")						,   up_block_types=("""UpBlock2D""", """CrossAttnUpBlock2D""")						,   block_out_channels=(3_2, 6_4)						,   attention_head_dim=(2, 4)						,   class_embed_type="""projection"""						,   projection_class_embeddings_input_dim=embedder_projection_dim * 2						,   cross_attention_dim=_UpperCAmelCase						,   layers_per_block=1						,   upcast_attention=_UpperCAmelCase						,   use_linear_projection=_UpperCAmelCase						,   )

									torch.manual_seed(0    )
									UpperCamelCase				:		str				   =			DDIMScheduler(
									    beta_schedule="""scaled_linear"""						,   beta_start=0.0_00_85						,   beta_end=0.0_12						,   prediction_type="""v_prediction"""						,   set_alpha_to_one=_UpperCAmelCase						,   steps_offset=1						,   )

									torch.manual_seed(0    )
									UpperCamelCase				:		Union[str, Any]				   =			AutoencoderKL()

									UpperCamelCase				:		Tuple				   =			{
									    # prior components
									    """prior_tokenizer""": prior_tokenizer,
									    """prior_text_encoder""": prior_text_encoder,
									    """prior""": prior,
									    """prior_scheduler""": prior_scheduler,
									    # image noising components
									    """image_normalizer""": image_normalizer,
									    """image_noising_scheduler""": image_noising_scheduler,
									    # regular denoising components
									    """tokenizer""": tokenizer,
									    """text_encoder""": text_encoder,
									    """unet""": unet,
									    """scheduler""": scheduler,
									    """vae""": vae,
									}

									return components







			def   _a      (      self						,   _A						,   _A=0    ):






									'''simple docstring'''







									if str(_UpperCAmelCase    ).startswith("""mps"""    ):
															UpperCamelCase				:		Optional[Any]				   =			torch.manual_seed(_UpperCAmelCase    )
									else:
															UpperCamelCase				:		Tuple				   =			torch.Generator(device=_UpperCAmelCase    ).manual_seed(_UpperCAmelCase    )
									UpperCamelCase				:		Union[str, Any]				   =			{
									    """prompt""": """A painting of a squirrel eating a burger""",
									    """generator""": generator,
									    """num_inference_steps""": 2,
									    """prior_num_inference_steps""": 2,
									    """output_type""": """numpy""",
									}
									return inputs







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		Tuple				   =			torch_device == """cpu"""

									self._test_attention_slicing_forward_pass(test_max_difference=_UpperCAmelCase    )







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		Union[str, Any]				   =			torch_device in ["""cpu""", """mps"""]

									self._test_inference_batch_single_identical(test_max_difference=_UpperCAmelCase    )






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

			"""simple docstring"""







			def   _a      (      self    ):






									'''simple docstring'''







									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		Any				   =			load_numpy(
									    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_l_anime_turtle_fp16.npy"""    )

									UpperCamelCase				:		Any				   =			StableUnCLIPPipeline.from_pretrained("""fusing/stable-unclip-2-1-l"""						,   torch_dtype=torch.floataa    )
									pipe.to(_UpperCAmelCase    )
									pipe.set_progress_bar_config(disable=_UpperCAmelCase    )
									# stable unclip will oom when integration tests are run on a V100,
									# so turn on memory savings
									pipe.enable_attention_slicing()
									pipe.enable_sequential_cpu_offload()

									UpperCamelCase				:		int				   =			torch.Generator(device="""cpu"""    ).manual_seed(0    )
									UpperCamelCase				:		Any				   =			pipe("""anime turle"""						,   generator=_UpperCAmelCase						,   output_type="""np"""    )

									UpperCamelCase				:		Any				   =			output.images[0]

									assert image.shape == (7_6_8, 7_6_8, 3)

									assert_mean_pixel_difference(_UpperCAmelCase						,   _UpperCAmelCase    )







			def   _a      (      self    ):






									'''simple docstring'''







									torch.cuda.empty_cache()
									torch.cuda.reset_max_memory_allocated()
									torch.cuda.reset_peak_memory_stats()

									UpperCamelCase				:		Tuple				   =			StableUnCLIPPipeline.from_pretrained("""fusing/stable-unclip-2-1-l"""						,   torch_dtype=torch.floataa    )
									UpperCamelCase				:		List[Any]				   =			pipe.to(_UpperCAmelCase    )
									pipe.set_progress_bar_config(disable=_UpperCAmelCase    )
									pipe.enable_attention_slicing()
									pipe.enable_sequential_cpu_offload()

									UpperCamelCase				:		Tuple				   =			pipe(
									    """anime turtle"""						,   prior_num_inference_steps=2						,   num_inference_steps=2						,   output_type="""np"""						,   )

									UpperCamelCase				:		Tuple				   =			torch.cuda.max_memory_allocated()
									# make sure that less than 7 GB is allocated
									assert mem_bytes < 7 * 1_0**9









"""simple docstring"""

from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


a							      =     logging.get_logger(__name__)

a							      =     {
    '''facebook/xmod-base''': '''https://huggingface.co/facebook/xmod-base/resolve/main/config.json''',
    '''facebook/xmod-large-prenorm''': '''https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json''',
    '''facebook/xmod-base-13-125k''': '''https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json''',
    '''facebook/xmod-base-30-125k''': '''https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json''',
    '''facebook/xmod-base-30-195k''': '''https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json''',
    '''facebook/xmod-base-60-125k''': '''https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json''',
    '''facebook/xmod-base-60-265k''': '''https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json''',
    '''facebook/xmod-base-75-125k''': '''https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json''',
    '''facebook/xmod-base-75-269k''': '''https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json''',
}





class 							lowercase_			(	__lowerCAmelCase				):



 '''simple docstring'''
 UpperCAmelCase       :	Optional[Any]     =      '''xmod'''

 def __init__(					self			:			str       ,				_UpperCAmelCase			:			Optional[Any]=30_522       ,				_UpperCAmelCase			:			Any=768       ,				_UpperCAmelCase			:			int=12       ,				_UpperCAmelCase			:			List[str]=12       ,				_UpperCAmelCase			:			Dict=3_072       ,				_UpperCAmelCase			:			Union[str, Any]="gelu"       ,				_UpperCAmelCase			:			Optional[int]=0.1       ,				_UpperCAmelCase			:			Tuple=0.1       ,				_UpperCAmelCase			:			List[str]=512       ,				_UpperCAmelCase			:			Dict=2       ,				_UpperCAmelCase			:			Optional[int]=0.02       ,				_UpperCAmelCase			:			Any=1E-1_2       ,				_UpperCAmelCase			:			Tuple=1       ,				_UpperCAmelCase			:			int=0       ,				_UpperCAmelCase			:			List[Any]=2       ,				_UpperCAmelCase			:			List[str]="absolute"       ,				_UpperCAmelCase			:			Tuple=True       ,				_UpperCAmelCase			:			Tuple=None       ,				_UpperCAmelCase			:			int=False       ,				_UpperCAmelCase			:			Union[str, Any]=2       ,				_UpperCAmelCase			:			List[Any]=False       ,				_UpperCAmelCase			:			Dict=True       ,				_UpperCAmelCase			:			List[str]=True       ,				_UpperCAmelCase			:			Tuple=("en_XX",)       ,				_UpperCAmelCase			:			List[str]=None       ,				**_UpperCAmelCase			:			Optional[Any]       ,				):
       super().__init__(pad_token_id=_UpperCAmelCase       ,				bos_token_id=_UpperCAmelCase       ,				eos_token_id=_UpperCAmelCase       ,				**_UpperCAmelCase						)

       _A     =  vocab_size
       _A     =  hidden_size
       _A     =  num_hidden_layers
       _A     =  num_attention_heads
       _A     =  hidden_act
       _A     =  intermediate_size
       _A     =  hidden_dropout_prob
       _A     =  attention_probs_dropout_prob
       _A     =  max_position_embeddings
       _A     =  type_vocab_size
       _A     =  initializer_range
       _A     =  layer_norm_eps
       _A     =  position_embedding_type
       _A     =  use_cache
       _A     =  classifier_dropout
       _A     =  pre_norm
       _A     =  adapter_reduction_factor
       _A     =  adapter_layer_norm
       _A     =  adapter_reuse_layer_norm
       _A     =  ln_before_adapter
       _A     =  list(_UpperCAmelCase						)
       _A     =  default_language





class 							lowercase_			(	__lowerCAmelCase				):



 '''simple docstring'''

 @property
 def      lowerCAmelCase_				(					self			:			Dict						):
       if self.task == "multiple-choice":
             _A     =  {0: 'batch', 1: 'choice', 2: 'sequence'}
       else:
             _A     =  {0: 'batch', 1: 'sequence'}
       return OrderedDict(
           [
               ('input_ids', dynamic_axis),
               ('attention_mask', dynamic_axis),
           ]						)

