






"""simple docstring"""



import gc
import unittest

from parameterized import parameterized

from diffusers import FlaxUNetaDConditionModel
from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import load_hf_numpy, require_flax, slow


if is_flax_available():
						import jax
						import jax.numpy as jnp







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


			def 	UpperCAmelCase__						(      self     :  int   ,       snake_case__     :  List[str]   ,       snake_case__     :  List[str]					):
										return F"""gaussian_noise_s={seed}_shape={'_'.join([str(__SCREAMING_SNAKE_CASE					) for s in shape]					)}.npy"""


			def 	UpperCAmelCase__						(      self     :  List[Any]					):
										super().tearDown()
										gc.collect()


			def 	UpperCAmelCase__						(      self     :  Optional[int]   ,       snake_case__     :  Tuple=0   ,       snake_case__     :  List[Any]=(4, 4, 64, 64)   ,       snake_case__     :  List[Any]=False					):
										lowerCamelCase_       :							Any									=jnp.bfloataa if fpaa else jnp.floataa
										lowerCamelCase_       :							int									=jnp.array(load_hf_numpy(self.get_file_format(__SCREAMING_SNAKE_CASE   ,       __SCREAMING_SNAKE_CASE					)					)   ,       dtype=__SCREAMING_SNAKE_CASE					)
										return image


			def 	UpperCAmelCase__						(      self     :  List[Any]   ,       snake_case__     :  Tuple=False   ,       snake_case__     :  Union[str, Any]="CompVis/stable-diffusion-v1-4"					):
										lowerCamelCase_       :							Union[str, Any]									=jnp.bfloataa if fpaa else jnp.floataa
										lowerCamelCase_       :							Union[str, Any]									='''bf16''' if fpaa else None

										lowerCamelCase_       :							Dict									=FlaxUNetaDConditionModel.from_pretrained(
										    __SCREAMING_SNAKE_CASE   ,       subfolder="unet"   ,       dtype=__SCREAMING_SNAKE_CASE   ,       revision=__SCREAMING_SNAKE_CASE					)
										return model, params


			def 	UpperCAmelCase__						(      self     :  Dict   ,       snake_case__     :  str=0   ,       snake_case__     :  List[Any]=(4, 77, 768)   ,       snake_case__     :  Any=False					):
										lowerCamelCase_       :							Optional[Any]									=jnp.bfloataa if fpaa else jnp.floataa
										lowerCamelCase_       :							List[str]									=jnp.array(load_hf_numpy(self.get_file_format(__SCREAMING_SNAKE_CASE   ,       __SCREAMING_SNAKE_CASE					)					)   ,       dtype=__SCREAMING_SNAKE_CASE					)
										return hidden_states


			@parameterized.expand(
			    [
			        # fmt: off
			        [83, 4, [-0.2_323, -0.1_304, 0.0_813, -0.3_093, -0.0_919, -0.1_571, -0.1_125, -0.5_806]],
			        [17, 0.55, [-0.0_831, -0.2_443, 0.0_901, -0.0_919, 0.3_396, 0.0_103, -0.3_743, 0.0_701]],
			        [8, 0.89, [-0.4_863, 0.0_859, 0.0_875, -0.1_658, 0.9_199, -0.0_114, 0.4_839, 0.4_639]],
			        [3, 1000, [-0.5_649, 0.2_402, -0.5_518, 0.1_248, 1.1_328, -0.2_443, -0.0_325, -1.0_078]],
			        # fmt: on
			    ]					)
			def 	UpperCAmelCase__						(      self     :  Dict   ,       snake_case__     :  List[str]   ,       snake_case__     :  Optional[Any]   ,       snake_case__     :  Union[str, Any]					):
										lowerCamelCase_       :							Tuple									=self.get_unet_model(model_id="CompVis/stable-diffusion-v1-4"   ,       fpaa=__SCREAMING_SNAKE_CASE					)
										lowerCamelCase_       :							Any									=self.get_latents(__SCREAMING_SNAKE_CASE   ,       fpaa=__SCREAMING_SNAKE_CASE					)
										lowerCamelCase_       :							Optional[Any]									=self.get_encoder_hidden_states(__SCREAMING_SNAKE_CASE   ,       fpaa=__SCREAMING_SNAKE_CASE					)

										lowerCamelCase_       :							Optional[Any]									=model.apply(
										    {"params": params}   ,       __SCREAMING_SNAKE_CASE   ,       jnp.array(__SCREAMING_SNAKE_CASE   ,       dtype=jnp.intaa					)   ,       encoder_hidden_states=__SCREAMING_SNAKE_CASE   ,       ).sample

										assert sample.shape == latents.shape

										lowerCamelCase_       :							Dict									=jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())					)   ,       dtype=jnp.floataa					)
										lowerCamelCase_       :							Tuple									=jnp.array(__SCREAMING_SNAKE_CASE   ,       dtype=jnp.floataa					)

										# Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, in the same hardware
										assert jnp.allclose(__SCREAMING_SNAKE_CASE   ,       __SCREAMING_SNAKE_CASE   ,       atol=1E-2					)


			@parameterized.expand(
			    [
			        # fmt: off
			        [83, 4, [0.1_514, 0.0_807, 0.1_624, 0.1_016, -0.1_896, 0.0_263, 0.0_677, 0.2_310]],
			        [17, 0.55, [0.1_164, -0.0_216, 0.0_170, 0.1_589, -0.3_120, 0.1_005, -0.0_581, -0.1_458]],
			        [8, 0.89, [-0.1_758, -0.0_169, 0.1_004, -0.1_411, 0.1_312, 0.1_103, -0.1_996, 0.2_139]],
			        [3, 1000, [0.1_214, 0.0_352, -0.0_731, -0.1_562, -0.0_994, -0.0_906, -0.2_340, -0.0_539]],
			        # fmt: on
			    ]					)
			def 	UpperCAmelCase__						(      self     :  Tuple   ,       snake_case__     :  int   ,       snake_case__     :  List[str]   ,       snake_case__     :  Any					):
										lowerCamelCase_       :							Union[str, Any]									=self.get_unet_model(model_id="stabilityai/stable-diffusion-2"   ,       fpaa=__SCREAMING_SNAKE_CASE					)
										lowerCamelCase_       :							List[Any]									=self.get_latents(__SCREAMING_SNAKE_CASE   ,       shape=(4, 4, 96, 96)   ,       fpaa=__SCREAMING_SNAKE_CASE					)
										lowerCamelCase_       :							Optional[Any]									=self.get_encoder_hidden_states(__SCREAMING_SNAKE_CASE   ,       shape=(4, 77, 1024)   ,       fpaa=__SCREAMING_SNAKE_CASE					)

										lowerCamelCase_       :							Optional[Any]									=model.apply(
										    {"params": params}   ,       __SCREAMING_SNAKE_CASE   ,       jnp.array(__SCREAMING_SNAKE_CASE   ,       dtype=jnp.intaa					)   ,       encoder_hidden_states=__SCREAMING_SNAKE_CASE   ,       ).sample

										assert sample.shape == latents.shape

										lowerCamelCase_       :							Optional[int]									=jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())					)   ,       dtype=jnp.floataa					)
										lowerCamelCase_       :							List[Any]									=jnp.array(__SCREAMING_SNAKE_CASE   ,       dtype=jnp.floataa					)

										# Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, on the same hardware
										assert jnp.allclose(__SCREAMING_SNAKE_CASE   ,       __SCREAMING_SNAKE_CASE   ,       atol=1E-2					)





'''simple docstring'''







import unittest

from knapsack import greedy_knapsack as kp






class  lowerCAmelCase__   (  unittest.TestCase     ):






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     List[str]													=       [10, 20, 30, 40, 50, 60]
											lowercase_	:     Optional[Any]													=       [2, 4, 6, 8, 10, 12]
											lowercase_	:     Union[str, Any]													=       1_00
											self.assertEqual(kp.calc_profit(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)				,							2_10		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											self.assertRaisesRegex(__SCREAMING_SNAKE_CASE				,							'''max_weight must greater than zero.'''		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											self.assertRaisesRegex(__SCREAMING_SNAKE_CASE				,							'''Weight can not be negative.'''		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											self.assertRaisesRegex(__SCREAMING_SNAKE_CASE				,							'''Profit can not be negative.'''		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											self.assertRaisesRegex(__SCREAMING_SNAKE_CASE				,							'''max_weight must greater than zero.'''		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											self.assertRaisesRegex(
											    __SCREAMING_SNAKE_CASE				,							'''The length of profit and weight must be same.'''		)


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



