






"""simple docstring"""
import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, XLMRobertaTokenizer

from diffusers import AltDiffusionPipeline, AutoencoderKL, DDIMScheduler, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
    RobertaSeriesConfig,
    RobertaSeriesModelWithTransformation,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

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


enable_full_determinism()







class     UpperCamelCase_ (__A       ,		__A       ,		__A       ,		unittest.TestCase  ):
	__magic_name__			   =       AltDiffusionPipeline
	__magic_name__			   =       TEXT_TO_IMAGE_PARAMS
	__magic_name__			   =       TEXT_TO_IMAGE_BATCH_PARAMS
	__magic_name__			   =       TEXT_TO_IMAGE_IMAGE_PARAMS
	__magic_name__			   =       TEXT_TO_IMAGE_IMAGE_PARAMS





	def   _SCREAMING_SNAKE_CASE   (   self      :				str		)		->    Optional[Any]:
				torch.manual_seed(0		)
				UpperCAmelCase_  :				int      =      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			,			)
				UpperCAmelCase_  :				Any      =      DDIMScheduler(
				    beta_start=0.0_0_0_8_5			,			beta_end=0.0_1_2			,			beta_schedule="scaled_linear"			,			clip_sample=lowerCAmelCase_			,			set_alpha_to_one=lowerCAmelCase_			,			)
				torch.manual_seed(0		)
				UpperCAmelCase_  :				List[str]      =      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			,			)

				# TODO: address the non-deterministic text encoder (fails for save-load tests)
				# torch.manual_seed(0)
				# text_encoder_config = RobertaSeriesConfig(
				#     hidden_size=32,
				#     project_dim=32,
				#     intermediate_size=37,
				#     layer_norm_eps=1e-05,
				#     num_attention_heads=4,
				#     num_hidden_layers=5,
				#     vocab_size=5002,
				# )
				# text_encoder = RobertaSeriesModelWithTransformation(text_encoder_config)

				torch.manual_seed(0		)
				UpperCAmelCase_  :				str      =      CLIPTextConfig(
				    bos_token_id=0			,			eos_token_id=2			,			hidden_size=32			,			projection_dim=32			,			intermediate_size=37			,			layer_norm_eps=1e-05			,			num_attention_heads=4			,			num_hidden_layers=5			,			pad_token_id=1			,			vocab_size=5_002			,			)
				UpperCAmelCase_  :				List[str]      =      CLIPTextModel(lowerCAmelCase_		)

				UpperCAmelCase_  :				Any      =      XLMRobertaTokenizer.from_pretrained("hf-internal-testing/tiny-xlm-roberta"		)
				UpperCAmelCase_  :				Any      =      77

				UpperCAmelCase_  :				List[Any]      =      {
				    "unet": unet,
				    "scheduler": scheduler,
				    "vae": vae,
				    "text_encoder": text_encoder,
				    "tokenizer": tokenizer,
				    "safety_checker": None,
				    "feature_extractor": None,
				}
				return components





	def   _SCREAMING_SNAKE_CASE   (   self      :				Union[str, Any]			,			lowerCAmelCase_      :				str			,			lowerCAmelCase_      :				Optional[Any]=0		)		->    Optional[int]:
				if str(lowerCAmelCase_		).startswith("mps"		):
							UpperCAmelCase_  :				Union[str, Any]      =      torch.manual_seed(lowerCAmelCase_		)
				else:
							UpperCAmelCase_  :				Tuple      =      torch.Generator(device=lowerCAmelCase_		).manual_seed(lowerCAmelCase_		)
				UpperCAmelCase_  :				Tuple      =      {
				    "prompt": "A painting of a squirrel eating a burger",
				    "generator": generator,
				    "num_inference_steps": 2,
				    "guidance_scale": 6.0,
				    "output_type": "numpy",
				}
				return inputs





	def   _SCREAMING_SNAKE_CASE   (   self      :				Any		)		->    Optional[Any]:
				super().test_attention_slicing_forward_pass(expected_max_diff=3e-3		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				Tuple		)		->    Dict:
				super().test_inference_batch_single_identical(expected_max_diff=3e-3		)





	def   _SCREAMING_SNAKE_CASE   (   self      :				Tuple		)		->    Optional[int]:
				UpperCAmelCase_  :				Tuple      =      "cpu"  # ensure determinism for the device-dependent torch.Generator

				UpperCAmelCase_  :				List[Any]      =      self.get_dummy_components()
				torch.manual_seed(0		)
				UpperCAmelCase_  :				Union[str, Any]      =      RobertaSeriesConfig(
				    hidden_size=32			,			project_dim=32			,			intermediate_size=37			,			layer_norm_eps=1e-05			,			num_attention_heads=4			,			num_hidden_layers=5			,			vocab_size=5_002			,			)
				# TODO: remove after fixing the non-deterministic text encoder
				UpperCAmelCase_  :				str      =      RobertaSeriesModelWithTransformation(lowerCAmelCase_		)
				UpperCAmelCase_  :				Union[str, Any]      =      text_encoder

				UpperCAmelCase_  :				Optional[Any]      =      AltDiffusionPipeline(**lowerCAmelCase_		)
				UpperCAmelCase_  :				Tuple      =      alt_pipe.to(lowerCAmelCase_		)
				alt_pipe.set_progress_bar_config(disable=lowerCAmelCase_		)

				UpperCAmelCase_  :				Optional[int]      =      self.get_dummy_inputs(lowerCAmelCase_		)
				UpperCAmelCase_  :				Optional[int]      =      "A photo of an astronaut"
				UpperCAmelCase_  :				int      =      alt_pipe(**lowerCAmelCase_		)
				UpperCAmelCase_  :				Tuple      =      output.images
				UpperCAmelCase_  :				Tuple      =      image[0, -3:, -3:, -1]

				assert image.shape == (1, 64, 64, 3)
				UpperCAmelCase_  :				Optional[Any]      =      np.array(
				    [0.5_7_4_8_1_6_2, 0.6_0_4_4_7_1_4_5, 0.4_8_8_2_1_2_1_7, 0.5_0_1_0_0_6_3_6, 0.5_4_3_1_1_8_5, 0.4_5_7_6_3_6_8_3, 0.4_9_6_5_7_6_9_6, 0.4_8_1_3_2_7_3_3, 0.4_7_5_7_3_0_9_3]		)

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





	def   _SCREAMING_SNAKE_CASE   (   self      :				Union[str, Any]		)		->    int:
				UpperCAmelCase_  :				List[str]      =      "cpu"  # ensure determinism for the device-dependent torch.Generator

				UpperCAmelCase_  :				Dict      =      self.get_dummy_components()
				UpperCAmelCase_  :				List[Any]      =      PNDMScheduler(skip_prk_steps=lowerCAmelCase_		)
				torch.manual_seed(0		)
				UpperCAmelCase_  :				Tuple      =      RobertaSeriesConfig(
				    hidden_size=32			,			project_dim=32			,			intermediate_size=37			,			layer_norm_eps=1e-05			,			num_attention_heads=4			,			num_hidden_layers=5			,			vocab_size=5_002			,			)
				# TODO: remove after fixing the non-deterministic text encoder
				UpperCAmelCase_  :				Tuple      =      RobertaSeriesModelWithTransformation(lowerCAmelCase_		)
				UpperCAmelCase_  :				List[Any]      =      text_encoder
				UpperCAmelCase_  :				List[str]      =      AltDiffusionPipeline(**lowerCAmelCase_		)
				UpperCAmelCase_  :				Optional[int]      =      alt_pipe.to(lowerCAmelCase_		)
				alt_pipe.set_progress_bar_config(disable=lowerCAmelCase_		)

				UpperCAmelCase_  :				str      =      self.get_dummy_inputs(lowerCAmelCase_		)
				UpperCAmelCase_  :				Dict      =      alt_pipe(**lowerCAmelCase_		)
				UpperCAmelCase_  :				Optional[int]      =      output.images
				UpperCAmelCase_  :				str      =      image[0, -3:, -3:, -1]

				assert image.shape == (1, 64, 64, 3)
				UpperCAmelCase_  :				int      =      np.array(
				    [0.5_1_6_0_5_0_9_3, 0.5_7_0_7_2_4_1, 0.4_7_3_6_5_5_0_7, 0.5_0_5_7_8_8_8_6, 0.5_6_3_3_8_7_7, 0.4_6_4_2_5_0_3, 0.5_1_8_2_0_8_1, 0.4_8_7_6_3_4_8_4, 0.4_9_0_8_4_2_3_7]		)

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







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





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





	def   _SCREAMING_SNAKE_CASE   (   self      :				Optional[int]		)		->    Optional[Any]:
				# make sure here that pndm scheduler skips prk
				UpperCAmelCase_  :				Optional[Any]      =      AltDiffusionPipeline.from_pretrained("BAAI/AltDiffusion"			,			safety_checker=lowerCAmelCase_		)
				UpperCAmelCase_  :				Dict      =      alt_pipe.to(lowerCAmelCase_		)
				alt_pipe.set_progress_bar_config(disable=lowerCAmelCase_		)

				UpperCAmelCase_  :				Union[str, Any]      =      "A painting of a squirrel eating a burger"
				UpperCAmelCase_  :				Optional[int]      =      torch.manual_seed(0		)
				UpperCAmelCase_  :				List[str]      =      alt_pipe([prompt]			,			generator=lowerCAmelCase_			,			guidance_scale=6.0			,			num_inference_steps=20			,			output_type="np"		)

				UpperCAmelCase_  :				List[Any]      =      output.images

				UpperCAmelCase_  :				List[str]      =      image[0, -3:, -3:, -1]

				assert image.shape == (1, 512, 512, 3)
				UpperCAmelCase_  :				Union[str, Any]      =      np.array([0.1_0_1_0, 0.0_8_0_0, 0.0_7_9_4, 0.0_8_8_5, 0.0_8_4_3, 0.0_7_6_2, 0.0_7_6_9, 0.0_7_2_9, 0.0_5_8_6]		)

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





	def   _SCREAMING_SNAKE_CASE   (   self      :				Tuple		)		->    Dict:
				UpperCAmelCase_  :				Optional[Any]      =      DDIMScheduler.from_pretrained("BAAI/AltDiffusion"			,			subfolder="scheduler"		)

				UpperCAmelCase_  :				Any      =      AltDiffusionPipeline.from_pretrained("BAAI/AltDiffusion"			,			scheduler=lowerCAmelCase_			,			safety_checker=lowerCAmelCase_		)
				UpperCAmelCase_  :				Union[str, Any]      =      alt_pipe.to(lowerCAmelCase_		)
				alt_pipe.set_progress_bar_config(disable=lowerCAmelCase_		)

				UpperCAmelCase_  :				Optional[int]      =      "A painting of a squirrel eating a burger"
				UpperCAmelCase_  :				Optional[int]      =      torch.manual_seed(0		)

				UpperCAmelCase_  :				Dict      =      alt_pipe([prompt]			,			generator=lowerCAmelCase_			,			num_inference_steps=2			,			output_type="numpy"		)
				UpperCAmelCase_  :				str      =      output.images

				UpperCAmelCase_  :				str      =      image[0, -3:, -3:, -1]

				assert image.shape == (1, 512, 512, 3)
				UpperCAmelCase_  :				Dict      =      np.array([0.4_0_1_9, 0.4_0_5_2, 0.3_8_1_0, 0.4_1_1_9, 0.3_9_1_6, 0.3_9_8_2, 0.4_6_5_1, 0.4_1_9_5, 0.5_3_2_3]		)

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











"""simple docstring"""
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


lowerCamelCase_       			=   {
    '''configuration_funnel''': ['''FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''FunnelConfig'''],
    '''convert_funnel_original_tf_checkpoint_to_pytorch''': [],
    '''tokenization_funnel''': ['''FunnelTokenizer'''],
}

try:
			if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			lowerCamelCase_       			=   ['''FunnelTokenizerFast''']

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			lowerCamelCase_       			=   [
			    '''FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST''',
			    '''FunnelBaseModel''',
			    '''FunnelForMaskedLM''',
			    '''FunnelForMultipleChoice''',
			    '''FunnelForPreTraining''',
			    '''FunnelForQuestionAnswering''',
			    '''FunnelForSequenceClassification''',
			    '''FunnelForTokenClassification''',
			    '''FunnelModel''',
			    '''FunnelPreTrainedModel''',
			    '''load_tf_weights_in_funnel''',
			]

try:
			if not is_tf_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			lowerCamelCase_       			=   [
			    '''TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST''',
			    '''TFFunnelBaseModel''',
			    '''TFFunnelForMaskedLM''',
			    '''TFFunnelForMultipleChoice''',
			    '''TFFunnelForPreTraining''',
			    '''TFFunnelForQuestionAnswering''',
			    '''TFFunnelForSequenceClassification''',
			    '''TFFunnelForTokenClassification''',
			    '''TFFunnelModel''',
			    '''TFFunnelPreTrainedModel''',
			]


if TYPE_CHECKING:
			from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig
			from .tokenization_funnel import FunnelTokenizer

			try:
						if not is_tokenizers_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .tokenization_funnel_fast import FunnelTokenizerFast

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_funnel import (
						    FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST,
						    FunnelBaseModel,
						    FunnelForMaskedLM,
						    FunnelForMultipleChoice,
						    FunnelForPreTraining,
						    FunnelForQuestionAnswering,
						    FunnelForSequenceClassification,
						    FunnelForTokenClassification,
						    FunnelModel,
						    FunnelPreTrainedModel,
						    load_tf_weights_in_funnel,
						)

			try:
						if not is_tf_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_tf_funnel import (
						    TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST,
						    TFFunnelBaseModel,
						    TFFunnelForMaskedLM,
						    TFFunnelForMultipleChoice,
						    TFFunnelForPreTraining,
						    TFFunnelForQuestionAnswering,
						    TFFunnelForSequenceClassification,
						    TFFunnelForTokenClassification,
						    TFFunnelModel,
						    TFFunnelPreTrainedModel,
						)

else:
			import sys

			lowerCamelCase_       			=   _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)



