'''simple docstring'''

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







		"""simple docstring"""
		lowercase   :      Dict       =							KandinskyVaaPriorPipeline
		lowercase   :      Dict       =							["""prompt"""]
		lowercase   :      Dict       =							["""prompt""", """negative_prompt"""]
		lowercase   :      Dict       =							[
		    """num_images_per_prompt""",
		    """generator""",
		    """num_inference_steps""",
		    """latents""",
		    """negative_prompt""",
		    """guidance_scale""",
		    """output_type""",
		    """return_dict""",
		]
		lowercase   :      List[Any]       =							False






		@property
		def 		UpperCamelCase__       (   self				) ->     Dict:
								return 32






		@property
		def 		UpperCamelCase__       (   self				) ->     Tuple:
								return 32






		@property
		def 		UpperCamelCase__       (   self				) ->     List[Any]:
								return self.time_input_dim






		@property
		def 		UpperCamelCase__       (   self				) ->     str:
								return self.time_input_dim * 4






		@property
		def 		UpperCamelCase__       (   self				) ->     str:
								return 100






		@property
		def 		UpperCamelCase__       (   self				) ->     Tuple:
								_lowerCAmelCase										=CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'				)
								return tokenizer






		@property
		def 		UpperCamelCase__       (   self				) ->     List[str]:
								torch.manual_seed(0				)
								_lowerCAmelCase										=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=37		,						layer_norm_eps=1E-05		,						num_attention_heads=4		,						num_hidden_layers=5		,						pad_token_id=1		,						vocab_size=1000		,						)
								return CLIPTextModelWithProjection(__A				)






		@property
		def 		UpperCamelCase__       (   self				) ->     Dict:
								torch.manual_seed(0				)

								_lowerCAmelCase										={
								    'num_attention_heads': 2,
								    'attention_head_dim': 12,
								    'embedding_dim': self.text_embedder_hidden_size,
								    'num_layers': 1,
								}

								_lowerCAmelCase										=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
								_lowerCAmelCase										=nn.Parameter(torch.ones(model.clip_std.shape				)				)
								return model






		@property
		def 		UpperCamelCase__       (   self				) ->     List[str]:
								torch.manual_seed(0				)
								_lowerCAmelCase										=CLIPVisionConfig(
								    hidden_size=self.text_embedder_hidden_size		,						image_size=224		,						projection_dim=self.text_embedder_hidden_size		,						intermediate_size=37		,						num_attention_heads=4		,						num_channels=3		,						num_hidden_layers=5		,						patch_size=14		,						)

								_lowerCAmelCase										=CLIPVisionModelWithProjection(__A				)
								return model






		@property
		def 		UpperCamelCase__       (   self				) ->     Optional[Any]:
								_lowerCAmelCase										=CLIPImageProcessor(
								    crop_size=224		,						do_center_crop=__A		,						do_normalize=__A		,						do_resize=__A		,						image_mean=[0.48_145_466, 0.4_578_275, 0.40_821_073]		,						image_std=[0.26_862_954, 0.26_130_258, 0.27_577_711]		,						resample=3		,						size=224		,						)

								return image_processor






		def 		UpperCamelCase__       (   self				) ->     List[Any]:
								_lowerCAmelCase										=self.dummy_prior
								_lowerCAmelCase										=self.dummy_image_encoder
								_lowerCAmelCase										=self.dummy_text_encoder
								_lowerCAmelCase										=self.dummy_tokenizer
								_lowerCAmelCase										=self.dummy_image_processor

								_lowerCAmelCase										=UnCLIPScheduler(
								    variance_type='fixed_small_log'		,						prediction_type='sample'		,						num_train_timesteps=1000		,						clip_sample=__A		,						clip_sample_range=10.0		,						)

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

								return components






		def 		UpperCamelCase__       (   self		,						__A		,						__A=0				) ->     Dict:
								if str(__A				).startswith('mps'				):
														_lowerCAmelCase										=torch.manual_seed(__A				)
								else:
														_lowerCAmelCase										=torch.Generator(device=__A				).manual_seed(__A				)
								_lowerCAmelCase										={
								    'prompt': 'horse',
								    'generator': generator,
								    'guidance_scale': 4.0,
								    'num_inference_steps': 2,
								    'output_type': 'np',
								}
								return inputs






		def 		UpperCamelCase__       (   self				) ->     int:
								_lowerCAmelCase										='cpu'

								_lowerCAmelCase										=self.get_dummy_components()

								_lowerCAmelCase										=self.pipeline_class(**__A				)
								_lowerCAmelCase										=pipe.to(__A				)

								pipe.set_progress_bar_config(disable=__A				)

								_lowerCAmelCase										=pipe(**self.get_dummy_inputs(__A				)				)
								_lowerCAmelCase										=output.image_embeds

								_lowerCAmelCase										=pipe(
								    **self.get_dummy_inputs(__A				)		,						return_dict=__A		,						)[0]

								_lowerCAmelCase										=image[0, -10:]
								_lowerCAmelCase										=image_from_tuple[0, -10:]

								assert image.shape == (1, 32)

								_lowerCAmelCase										=np.array(
								    [-0.0_532, 1.7_120, 0.3_656, -1.0_852, -0.8_946, -1.1_756, 0.4_348, 0.2_482, 0.5_146, -0.1_156]				)

								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[Any]:
								_lowerCAmelCase										=torch_device == 'cpu'
								_lowerCAmelCase										=True
								_lowerCAmelCase										=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				) ->     int:
								_lowerCAmelCase										=torch_device == 'cpu'
								_lowerCAmelCase										=False

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






'''simple docstring'''

import unittest

from knapsack import knapsack as k
class   SCREAMING_SNAKE_CASE			(						unittest.TestCase):







		"""simple docstring"""






		def 		UpperCamelCase__       (   self				) ->     Optional[Any]:
								_lowerCAmelCase										=0
								_lowerCAmelCase										=[0]
								_lowerCAmelCase										=[0]
								_lowerCAmelCase										=len(__A				)
								self.assertEqual(k.knapsack(__A		,						__A		,						__A		,						__A				)		,						0				)

								_lowerCAmelCase										=[60]
								_lowerCAmelCase										=[10]
								_lowerCAmelCase										=len(__A				)
								self.assertEqual(k.knapsack(__A		,						__A		,						__A		,						__A				)		,						0				)






		def 		UpperCamelCase__       (   self				) ->     Tuple:
								_lowerCAmelCase										=3
								_lowerCAmelCase										=[1, 2, 3]
								_lowerCAmelCase										=[3, 2, 1]
								_lowerCAmelCase										=len(__A				)
								self.assertEqual(k.knapsack(__A		,						__A		,						__A		,						__A				)		,						5				)






		def 		UpperCamelCase__       (   self				) ->     Union[str, Any]:
								_lowerCAmelCase										=50
								_lowerCAmelCase										=[60, 100, 120]
								_lowerCAmelCase										=[10, 20, 30]
								_lowerCAmelCase										=len(__A				)
								self.assertEqual(k.knapsack(__A		,						__A		,						__A		,						__A				)		,						220				)


if __name__ == "__main__":
							unittest.main()





