






import unittest

import numpy as np
import torch
from torch import nn
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModelWithProjection,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import KandinskyVaaPriorPipeline, PriorTransformer, UnCLIPScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()



class   lowerCamelCase				(SCREAMING_SNAKE_CASE_			,  unittest.TestCase      ):







		'''simple docstring'''



		_snake_case   :   int							=  KandinskyVaaPriorPipeline
		_snake_case   :   Optional[Any]							=  ['''prompt''']
		_snake_case   :   List[Any]							=  ['''prompt''', '''negative_prompt''']
		_snake_case   :   List[Any]							=  [
		    '''num_images_per_prompt''',
		    '''generator''',
		    '''num_inference_steps''',
		    '''latents''',
		    '''negative_prompt''',
		    '''guidance_scale''',
		    '''output_type''',
		    '''return_dict''',
		]
		_snake_case   :   str							=  False



		@property
		def  __UpperCAmelCase      (							self       )     ->							List[str]:
							return 3_2



		@property
		def  __UpperCAmelCase      (							self       )     ->							str:
							return 3_2



		@property
		def  __UpperCAmelCase      (							self       )     ->							Optional[int]:
							return self.time_input_dim



		@property
		def  __UpperCAmelCase      (							self       )     ->							int:
							return self.time_input_dim * 4



		@property
		def  __UpperCAmelCase      (							self       )     ->							Union[str, Any]:
							return 1_0_0



		@property
		def  __UpperCAmelCase      (							self       )     ->							List[str]:
							UpperCAmelCase_   :				Tuple						=				CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'       )
							return tokenizer



		@property
		def  __UpperCAmelCase      (							self       )     ->							Optional[int]:
							torch.manual_seed(0       )
							UpperCAmelCase_   :				Union[str, Any]						=				CLIPTextConfig(
							    bos_token_id=0    ,		eos_token_id=2    ,		hidden_size=self.text_embedder_hidden_size    ,		projection_dim=self.text_embedder_hidden_size    ,		intermediate_size=3_7    ,		layer_norm_eps=1E-05    ,		num_attention_heads=4    ,		num_hidden_layers=5    ,		pad_token_id=1    ,		vocab_size=1_0_0_0    ,		)
							return CLIPTextModelWithProjection(__a       )



		@property
		def  __UpperCAmelCase      (							self       )     ->							Any:
							torch.manual_seed(0       )

							UpperCAmelCase_   :				Any						=				{
							    'num_attention_heads': 2,
							    'attention_head_dim': 1_2,
							    'embedding_dim': self.text_embedder_hidden_size,
							    'num_layers': 1,
							}

							UpperCAmelCase_   :				List[Any]						=				PriorTransformer(**__a       )
							# clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0
							UpperCAmelCase_   :				Any						=				nn.Parameter(torch.ones(model.clip_std.shape       )       )
							return model



		@property
		def  __UpperCAmelCase      (							self       )     ->							List[str]:
							torch.manual_seed(0       )
							UpperCAmelCase_   :				Optional[Any]						=				CLIPVisionConfig(
							    hidden_size=self.text_embedder_hidden_size    ,		image_size=2_2_4    ,		projection_dim=self.text_embedder_hidden_size    ,		intermediate_size=3_7    ,		num_attention_heads=4    ,		num_channels=3    ,		num_hidden_layers=5    ,		patch_size=1_4    ,		)

							UpperCAmelCase_   :				Optional[Any]						=				CLIPVisionModelWithProjection(__a       )
							return model



		@property
		def  __UpperCAmelCase      (							self       )     ->							List[Any]:
							UpperCAmelCase_   :				Dict						=				CLIPImageProcessor(
							    crop_size=2_2_4    ,		do_center_crop=__a    ,		do_normalize=__a    ,		do_resize=__a    ,		image_mean=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]    ,		image_std=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]    ,		resample=3    ,		size=2_2_4    ,		)

							return image_processor



		def  __UpperCAmelCase      (							self       )     ->							Optional[int]:
							UpperCAmelCase_   :				Tuple						=				self.dummy_prior
							UpperCAmelCase_   :				List[str]						=				self.dummy_image_encoder
							UpperCAmelCase_   :				str						=				self.dummy_text_encoder
							UpperCAmelCase_   :				List[str]						=				self.dummy_tokenizer
							UpperCAmelCase_   :				List[str]						=				self.dummy_image_processor

							UpperCAmelCase_   :				Any						=				UnCLIPScheduler(
							    variance_type='fixed_small_log'    ,		prediction_type='sample'    ,		num_train_timesteps=1_0_0_0    ,		clip_sample=__a    ,		clip_sample_range=1_0.0    ,		)

							UpperCAmelCase_   :				str						=				{
							    'prior': prior,
							    'image_encoder': image_encoder,
							    'text_encoder': text_encoder,
							    'tokenizer': tokenizer,
							    'scheduler': scheduler,
							    'image_processor': image_processor,
							}

							return components



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase=0       )     ->							Any:
							if str(__a       ).startswith('mps'       ):
												UpperCAmelCase_   :				List[str]						=				torch.manual_seed(__a       )
							else:
												UpperCAmelCase_   :				List[str]						=				torch.Generator(device=__a       ).manual_seed(__a       )
							UpperCAmelCase_   :				List[Any]						=				{
							    'prompt': 'horse',
							    'generator': generator,
							    'guidance_scale': 4.0,
							    'num_inference_steps': 2,
							    'output_type': 'np',
							}
							return inputs



		def  __UpperCAmelCase      (							self       )     ->							Dict:
							UpperCAmelCase_   :				str						=				'cpu'

							UpperCAmelCase_   :				List[str]						=				self.get_dummy_components()

							UpperCAmelCase_   :				Tuple						=				self.pipeline_class(**__a       )
							UpperCAmelCase_   :				Optional[Any]						=				pipe.to(__a       )

							pipe.set_progress_bar_config(disable=__a       )

							UpperCAmelCase_   :				Optional[int]						=				pipe(**self.get_dummy_inputs(__a       )       )
							UpperCAmelCase_   :				List[str]						=				output.image_embeds

							UpperCAmelCase_   :				str						=				pipe(
							    **self.get_dummy_inputs(__a       )    ,		return_dict=__a    ,		)[0]

							UpperCAmelCase_   :				Union[str, Any]						=				image[0, -1_0:]
							UpperCAmelCase_   :				Any						=				image_from_tuple[0, -1_0:]

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

							UpperCAmelCase_   :				List[Any]						=				np.array(
							    [-0.05_32, 1.71_20, 0.36_56, -1.08_52, -0.89_46, -1.17_56, 0.43_48, 0.24_82, 0.51_46, -0.11_56]       )

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



		@skip_mps
		def  __UpperCAmelCase      (							self       )     ->							Optional[int]:
							UpperCAmelCase_   :				Union[str, Any]						=				torch_device == 'cpu'
							UpperCAmelCase_   :				Dict						=				True
							UpperCAmelCase_   :				Union[str, Any]						=				False

							self._test_inference_batch_single_identical(
							    test_max_difference=__a    ,		relax_max_difference=__a    ,		test_mean_pixel_difference=__a    ,		)



		@skip_mps
		def  __UpperCAmelCase      (							self       )     ->							Union[str, Any]:
							UpperCAmelCase_   :				Dict						=				torch_device == 'cpu'
							UpperCAmelCase_   :				Optional[Any]						=				False

							self._test_attention_slicing_forward_pass(
							    test_max_difference=__a    ,		test_mean_pixel_difference=__a    ,		)














from __future__ import annotations




def 					lowercase__				(					__snake_case   :							list[int]					,	__snake_case   :							int  ):


					'''simple docstring'''


					if len(__snake_case  ) < k or k < 0:
										raise ValueError('Invalid Input'  )
					UpperCAmelCase_   :				int						=						sum(array[:k]  )
					for i in range(len(__snake_case  ) - k  ):
										UpperCAmelCase_   :				List[Any]						=						current_sum - array[i] + array[i + k]
										UpperCAmelCase_   :				List[Any]						=						max(__snake_case					,	__snake_case  )
					return max_sum


if __name__ == "__main__":
		from doctest import testmod
		from random import randint

		testmod()
		__UpperCAmelCase     							=						[randint(-1000, 1000) for i in range(100)]
		__UpperCAmelCase     							=						randint(0, 110)
		print(F'The maximum sum of {k} consecutive elements is {max_sum_in_array(array,k)}')






