



# Imports
import numpy as np



class  lowerCamelCase__    :




		'''simple docstring'''



		def __init__(self	,__lowerCamelCase=None	,__lowerCamelCase=None	,__lowerCamelCase=None	,__lowerCamelCase=None	,__lowerCamelCase=None				)  ->    Optional[int]:

						"""simple docstring"""






						self.set_matricies(red=__lowerCamelCase	,green=__lowerCamelCase	,blue=__lowerCamelCase	,red_edge=__lowerCamelCase	,nir=__lowerCamelCase				)



		def 	lowerCAmelCase__   (self	,__lowerCamelCase=None	,__lowerCamelCase=None	,__lowerCamelCase=None	,__lowerCamelCase=None	,__lowerCamelCase=None				)  ->    List[str]:

						"""simple docstring"""






						if red is not None:
										lowerCAmelCase__  :       str				=  red
						if green is not None:
										lowerCAmelCase__  :       Optional[Any]				=  green
						if blue is not None:
										lowerCAmelCase__  :       Dict				=  blue
						if red_edge is not None:
										lowerCAmelCase__  :       List[Any]				=  red_edge
						if nir is not None:
										lowerCAmelCase__  :       Any				=  nir
						return True



		def 	lowerCAmelCase__   (self	,__lowerCamelCase=""	,__lowerCamelCase=None	,__lowerCamelCase=None	,__lowerCamelCase=None	,__lowerCamelCase=None	,__lowerCamelCase=None				)  ->    Optional[int]:

						"""simple docstring"""






						self.set_matricies(red=__lowerCamelCase	,green=__lowerCamelCase	,blue=__lowerCamelCase	,red_edge=__lowerCamelCase	,nir=__lowerCamelCase				)
						lowerCAmelCase__  :       Optional[int]				=  {
						    '''ARVI2''': self.arvaa,
						    '''CCCI''': self.ccci,
						    '''CVI''': self.cvi,
						    '''GLI''': self.gli,
						    '''NDVI''': self.ndvi,
						    '''BNDVI''': self.bndvi,
						    '''redEdgeNDVI''': self.red_edge_ndvi,
						    '''GNDVI''': self.gndvi,
						    '''GBNDVI''': self.gbndvi,
						    '''GRNDVI''': self.grndvi,
						    '''RBNDVI''': self.rbndvi,
						    '''PNDVI''': self.pndvi,
						    '''ATSAVI''': self.atsavi,
						    '''BWDRVI''': self.bwdrvi,
						    '''CIgreen''': self.ci_green,
						    '''CIrededge''': self.ci_rededge,
						    '''CI''': self.ci,
						    '''CTVI''': self.ctvi,
						    '''GDVI''': self.gdvi,
						    '''EVI''': self.evi,
						    '''GEMI''': self.gemi,
						    '''GOSAVI''': self.gosavi,
						    '''GSAVI''': self.gsavi,
						    '''Hue''': self.hue,
						    '''IVI''': self.ivi,
						    '''IPVI''': self.ipvi,
						    '''I''': self.i,
						    '''RVI''': self.rvi,
						    '''MRVI''': self.mrvi,
						    '''MSAVI''': self.m_savi,
						    '''NormG''': self.norm_g,
						    '''NormNIR''': self.norm_nir,
						    '''NormR''': self.norm_r,
						    '''NGRDI''': self.ngrdi,
						    '''RI''': self.ri,
						    '''S''': self.s,
						    '''IF''': self._if,
						    '''DVI''': self.dvi,
						    '''TVI''': self.tvi,
						    '''NDRE''': self.ndre,
						}

						try:
										return funcs[index]()
						except KeyError:
										print('''Index not in the list!'''				)
										return False



		def 	lowerCAmelCase__   (self				)  ->    int:

						"""simple docstring"""






						return -0.18 + (1.17 * ((self.nir - self.red) / (self.nir + self.red)))



		def 	lowerCAmelCase__   (self				)  ->    Dict:

						"""simple docstring"""






						return ((self.nir - self.redEdge) / (self.nir + self.redEdge)) / (
						    (self.nir - self.red) / (self.nir + self.red)
						)



		def 	lowerCAmelCase__   (self				)  ->    str:

						"""simple docstring"""






						return self.nir * (self.red / (self.green**2))



		def 	lowerCAmelCase__   (self				)  ->    Union[str, Any]:

						"""simple docstring"""






						return (2 * self.green - self.red - self.blue) / (
						    2 * self.green + self.red + self.blue
						)



		def 	lowerCAmelCase__   (self				)  ->    int:

						"""simple docstring"""






						return (self.nir - self.red) / (self.nir + self.red)



		def 	lowerCAmelCase__   (self				)  ->    Union[str, Any]:

						"""simple docstring"""






						return (self.nir - self.blue) / (self.nir + self.blue)



		def 	lowerCAmelCase__   (self				)  ->    str:

						"""simple docstring"""






						return (self.redEdge - self.red) / (self.redEdge + self.red)



		def 	lowerCAmelCase__   (self				)  ->    List[str]:

						"""simple docstring"""






						return (self.nir - self.green) / (self.nir + self.green)



		def 	lowerCAmelCase__   (self				)  ->    Any:

						"""simple docstring"""






						return (self.nir - (self.green + self.blue)) / (
						    self.nir + (self.green + self.blue)
						)



		def 	lowerCAmelCase__   (self				)  ->    Optional[Any]:

						"""simple docstring"""






						return (self.nir - (self.green + self.red)) / (
						    self.nir + (self.green + self.red)
						)



		def 	lowerCAmelCase__   (self				)  ->    Optional[int]:

						"""simple docstring"""






						return (self.nir - (self.blue + self.red)) / (self.nir + (self.blue + self.red))



		def 	lowerCAmelCase__   (self				)  ->    int:

						"""simple docstring"""






						return (self.nir - (self.green + self.red + self.blue)) / (
						    self.nir + (self.green + self.red + self.blue)
						)



		def 	lowerCAmelCase__   (self	,__lowerCamelCase=0.08	,__lowerCamelCase=1.22	,__lowerCamelCase=0.03				)  ->    Union[str, Any]:

						"""simple docstring"""






						return a * (
						    (self.nir - a * self.red - b)
						    / (a * self.nir + self.red - a * b + x * (1 + a**2))
						)



		def 	lowerCAmelCase__   (self				)  ->    Union[str, Any]:

						"""simple docstring"""






						return (0.1 * self.nir - self.blue) / (0.1 * self.nir + self.blue)



		def 	lowerCAmelCase__   (self				)  ->    Any:

						"""simple docstring"""






						return (self.nir / self.green) - 1



		def 	lowerCAmelCase__   (self				)  ->    int:

						"""simple docstring"""






						return (self.nir / self.redEdge) - 1



		def 	lowerCAmelCase__   (self				)  ->    List[str]:

						"""simple docstring"""






						return (self.red - self.blue) / self.red



		def 	lowerCAmelCase__   (self				)  ->    Any:

						"""simple docstring"""






						lowerCAmelCase__  :       str				=  self.ndvi()
						return ((ndvi + 0.5) / (abs(ndvi + 0.5				))) * (abs(ndvi + 0.5				) ** (1 / 2))



		def 	lowerCAmelCase__   (self				)  ->    Optional[int]:

						"""simple docstring"""






						return self.nir - self.green



		def 	lowerCAmelCase__   (self				)  ->    Optional[Any]:

						"""simple docstring"""






						return 2.5 * (
						    (self.nir - self.red) / (self.nir + 6 * self.red - 7.5 * self.blue + 1)
						)



		def 	lowerCAmelCase__   (self				)  ->    Tuple:

						"""simple docstring"""






						lowerCAmelCase__  :       int				=  (2 * (self.nir**2 - self.red**2) + 1.5 * self.nir + 0.5 * self.red) / (
						    self.nir + self.red + 0.5
						)
						return n * (1 - 0.25 * n) - (self.red - 0.125) / (1 - self.red)



		def 	lowerCAmelCase__   (self	,__lowerCamelCase=0.16				)  ->    Union[str, Any]:

						"""simple docstring"""






						return (self.nir - self.green) / (self.nir + self.green + y)



		def 	lowerCAmelCase__   (self	,__lowerCamelCase=0.5				)  ->    str:

						"""simple docstring"""






						return ((self.nir - self.green) / (self.nir + self.green + n)) * (1 + n)



		def 	lowerCAmelCase__   (self				)  ->    Optional[int]:

						"""simple docstring"""






						return np.arctan(
						    ((2 * self.red - self.green - self.blue) / 30.5) * (self.green - self.blue)				)



		def 	lowerCAmelCase__   (self	,__lowerCamelCase=None	,__lowerCamelCase=None				)  ->    str:

						"""simple docstring"""






						return (self.nir - b) / (a * self.red)



		def 	lowerCAmelCase__   (self				)  ->    str:

						"""simple docstring"""






						return (self.nir / ((self.nir + self.red) / 2)) * (self.ndvi() + 1)



		def 	lowerCAmelCase__   (self				)  ->    Tuple:

						"""simple docstring"""






						return (self.red + self.green + self.blue) / 30.5



		def 	lowerCAmelCase__   (self				)  ->    Dict:

						"""simple docstring"""






						return self.nir / self.red



		def 	lowerCAmelCase__   (self				)  ->    List[Any]:

						"""simple docstring"""






						return (self.rvi() - 1) / (self.rvi() + 1)



		def 	lowerCAmelCase__   (self				)  ->    List[Any]:

						"""simple docstring"""






						return (
						    (2 * self.nir + 1)
						    - ((2 * self.nir + 1) ** 2 - 8 * (self.nir - self.red)) ** (1 / 2)
						) / 2



		def 	lowerCAmelCase__   (self				)  ->    Optional[Any]:

						"""simple docstring"""






						return self.green / (self.nir + self.red + self.green)



		def 	lowerCAmelCase__   (self				)  ->    str:

						"""simple docstring"""






						return self.nir / (self.nir + self.red + self.green)



		def 	lowerCAmelCase__   (self				)  ->    int:

						"""simple docstring"""






						return self.red / (self.nir + self.red + self.green)



		def 	lowerCAmelCase__   (self				)  ->    str:

						"""simple docstring"""






						return (self.green - self.red) / (self.green + self.red)



		def 	lowerCAmelCase__   (self				)  ->    Dict:

						"""simple docstring"""






						return (self.red - self.green) / (self.red + self.green)



		def 	lowerCAmelCase__   (self				)  ->    Optional[Any]:

						"""simple docstring"""






						lowerCAmelCase__  :       List[Any]				=  np.max([np.max(self.red				), np.max(self.green				), np.max(self.blue				)]				)
						lowerCAmelCase__  :       Optional[Any]				=  np.min([np.min(self.red				), np.min(self.green				), np.min(self.blue				)]				)
						return (max_value - min_value) / max_value



		def 	lowerCAmelCase__   (self				)  ->    Optional[int]:

						"""simple docstring"""






						return (2 * self.red - self.green - self.blue) / (self.green - self.blue)



		def 	lowerCAmelCase__   (self				)  ->    Optional[int]:

						"""simple docstring"""






						return self.nir / self.red



		def 	lowerCAmelCase__   (self				)  ->    int:

						"""simple docstring"""






						return (self.ndvi() + 0.5) ** (1 / 2)



		def 	lowerCAmelCase__   (self				)  ->    str:

						"""simple docstring"""






						return (self.nir - self.redEdge) / (self.nir + self.redEdge)






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  lowerCamelCase__    (							lowerCamelCase__   ,       lowerCamelCase__   ,       lowerCamelCase__   ,       unittest.TestCase):




		'''simple docstring'''



		snake_case_			=AltDiffusionPipeline
		snake_case_			=TEXT_TO_IMAGE_PARAMS
		snake_case_			=TEXT_TO_IMAGE_BATCH_PARAMS
		snake_case_			=TEXT_TO_IMAGE_IMAGE_PARAMS
		snake_case_			=TEXT_TO_IMAGE_IMAGE_PARAMS



		def 	lowerCAmelCase__   (self				)  ->    Dict:

						"""simple docstring"""






						torch.manual_seed(0				)
						lowerCAmelCase__  :       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	,)
						lowerCAmelCase__  :       List[str]				=  DDIMScheduler(
						    beta_start=0.0_0085	,beta_end=0.012	,beta_schedule='''scaled_linear'''	,clip_sample=__lowerCamelCase	,set_alpha_to_one=__lowerCamelCase	,)
						torch.manual_seed(0				)
						lowerCAmelCase__  :       Optional[Any]				=  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				)
						lowerCAmelCase__  :       Dict				=  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=50_02	,)
						lowerCAmelCase__  :       int				=  CLIPTextModel(__lowerCamelCase				)

						lowerCAmelCase__  :       str				=  XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta'''				)
						lowerCAmelCase__  :       Union[str, Any]				=  77

						lowerCAmelCase__  :       str				=  {
						    '''unet''': unet,
						    '''scheduler''': scheduler,
						    '''vae''': vae,
						    '''text_encoder''': text_encoder,
						    '''tokenizer''': tokenizer,
						    '''safety_checker''': None,
						    '''feature_extractor''': None,
						}
						return components



		def 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase=0				)  ->    Optional[Any]:

						"""simple docstring"""






						if str(__lowerCamelCase				).startswith('''mps'''				):
										lowerCAmelCase__  :       Tuple				=  torch.manual_seed(__lowerCamelCase				)
						else:
										lowerCAmelCase__  :       Any				=  torch.Generator(device=__lowerCamelCase				).manual_seed(__lowerCamelCase				)
						lowerCAmelCase__  :       Optional[Any]				=  {
						    '''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 	lowerCAmelCase__   (self				)  ->    str:

						"""simple docstring"""






						super().test_attention_slicing_forward_pass(expected_max_diff=3e-3				)



		def 	lowerCAmelCase__   (self				)  ->    Tuple:

						"""simple docstring"""






						super().test_inference_batch_single_identical(expected_max_diff=3e-3				)



		def 	lowerCAmelCase__   (self				)  ->    Tuple:

						"""simple docstring"""






						lowerCAmelCase__  :       List[str]				=  '''cpu'''  # ensure determinism for the device-dependent torch.Generator

						lowerCAmelCase__  :       int				=  self.get_dummy_components()
						torch.manual_seed(0				)
						lowerCAmelCase__  :       str				=  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=50_02	,)
						# TODO: remove after fixing the non-deterministic text encoder
						lowerCAmelCase__  :       Any				=  RobertaSeriesModelWithTransformation(__lowerCamelCase				)
						lowerCAmelCase__  :       List[str]				=  text_encoder

						lowerCAmelCase__  :       List[Any]				=  AltDiffusionPipeline(**__lowerCamelCase				)
						lowerCAmelCase__  :       Optional[Any]				=  alt_pipe.to(__lowerCamelCase				)
						alt_pipe.set_progress_bar_config(disable=__lowerCamelCase				)

						lowerCAmelCase__  :       List[str]				=  self.get_dummy_inputs(__lowerCamelCase				)
						lowerCAmelCase__  :       str				=  '''A photo of an astronaut'''
						lowerCAmelCase__  :       Any				=  alt_pipe(**__lowerCamelCase				)
						lowerCAmelCase__  :       int				=  output.images
						lowerCAmelCase__  :       List[Any]				=  image[0, -3:, -3:, -1]

						assert image.shape == (1, 64, 64, 3)
						lowerCAmelCase__  :       Optional[Any]				=  np.array(
						    [0.574_8162, 0.6044_7145, 0.4882_1217, 0.5010_0636, 0.543_1185, 0.4576_3683, 0.4965_7696, 0.4813_2733, 0.4757_3093]				)

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



		def 	lowerCAmelCase__   (self				)  ->    Optional[int]:

						"""simple docstring"""






						lowerCAmelCase__  :       List[Any]				=  '''cpu'''  # ensure determinism for the device-dependent torch.Generator

						lowerCAmelCase__  :       str				=  self.get_dummy_components()
						lowerCAmelCase__  :       Optional[Any]				=  PNDMScheduler(skip_prk_steps=__lowerCamelCase				)
						torch.manual_seed(0				)
						lowerCAmelCase__  :       int				=  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=50_02	,)
						# TODO: remove after fixing the non-deterministic text encoder
						lowerCAmelCase__  :       Tuple				=  RobertaSeriesModelWithTransformation(__lowerCamelCase				)
						lowerCAmelCase__  :       List[str]				=  text_encoder
						lowerCAmelCase__  :       List[Any]				=  AltDiffusionPipeline(**__lowerCamelCase				)
						lowerCAmelCase__  :       str				=  alt_pipe.to(__lowerCamelCase				)
						alt_pipe.set_progress_bar_config(disable=__lowerCamelCase				)

						lowerCAmelCase__  :       Union[str, Any]				=  self.get_dummy_inputs(__lowerCamelCase				)
						lowerCAmelCase__  :       Optional[int]				=  alt_pipe(**__lowerCamelCase				)
						lowerCAmelCase__  :       Optional[int]				=  output.images
						lowerCAmelCase__  :       str				=  image[0, -3:, -3:, -1]

						assert image.shape == (1, 64, 64, 3)
						lowerCAmelCase__  :       Union[str, Any]				=  np.array(
						    [0.5160_5093, 0.570_7241, 0.4736_5507, 0.5057_8886, 0.563_3877, 0.464_2503, 0.518_2081, 0.4876_3484, 0.4908_4237]				)

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



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




		'''simple docstring'''



		def 	lowerCAmelCase__   (self				)  ->    List[Any]:

						"""simple docstring"""






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



		def 	lowerCAmelCase__   (self				)  ->    List[str]:

						"""simple docstring"""






						lowerCAmelCase__  :       Union[str, Any]				=  AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion'''	,safety_checker=__lowerCamelCase				)
						lowerCAmelCase__  :       List[Any]				=  alt_pipe.to(__lowerCamelCase				)
						alt_pipe.set_progress_bar_config(disable=__lowerCamelCase				)

						lowerCAmelCase__  :       Any				=  '''A painting of a squirrel eating a burger'''
						lowerCAmelCase__  :       List[Any]				=  torch.manual_seed(0				)
						lowerCAmelCase__  :       str				=  alt_pipe([prompt]	,generator=__lowerCamelCase	,guidance_scale=6.0	,num_inference_steps=20	,output_type='''np'''				)

						lowerCAmelCase__  :       str				=  output.images

						lowerCAmelCase__  :       int				=  image[0, -3:, -3:, -1]

						assert image.shape == (1, 5_12, 5_12, 3)
						lowerCAmelCase__  :       Dict				=  np.array([0.1010, 0.0800, 0.0794, 0.0885, 0.0843, 0.0762, 0.0769, 0.0729, 0.0586]				)

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



		def 	lowerCAmelCase__   (self				)  ->    Any:

						"""simple docstring"""






						lowerCAmelCase__  :       List[Any]				=  DDIMScheduler.from_pretrained('''BAAI/AltDiffusion'''	,subfolder='''scheduler'''				)

						lowerCAmelCase__  :       List[str]				=  AltDiffusionPipeline.from_pretrained('''BAAI/AltDiffusion'''	,scheduler=__lowerCamelCase	,safety_checker=__lowerCamelCase				)
						lowerCAmelCase__  :       Union[str, Any]				=  alt_pipe.to(__lowerCamelCase				)
						alt_pipe.set_progress_bar_config(disable=__lowerCamelCase				)

						lowerCAmelCase__  :       List[Any]				=  '''A painting of a squirrel eating a burger'''
						lowerCAmelCase__  :       Optional[Any]				=  torch.manual_seed(0				)

						lowerCAmelCase__  :       List[str]				=  alt_pipe([prompt]	,generator=__lowerCamelCase	,num_inference_steps=2	,output_type='''numpy'''				)
						lowerCAmelCase__  :       List[str]				=  output.images

						lowerCAmelCase__  :       Optional[int]				=  image[0, -3:, -3:, -1]

						assert image.shape == (1, 5_12, 5_12, 3)
						lowerCAmelCase__  :       List[Any]				=  np.array([0.4019, 0.4052, 0.3810, 0.4119, 0.3916, 0.3982, 0.4651, 0.4195, 0.5323]				)

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

