



import math


class 						lowercase    :
						'''simple docstring'''


						def       lowerCamelCase_				(  self    :				Any       ,	snake_case    :				list[list[float]]       ,	snake_case    :				list[int]	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     int						=				0.0
											SCREAMING_SNAKE_CASE							:     List[Any]						=				0.0
											for i in range(len(snake_case	)	):
																da += math.pow((sample[i] - weights[0][i])       ,	2	)
																da += math.pow((sample[i] - weights[1][i])       ,	2	)
																return 0 if da > da else 1
											return 0







						def       lowerCamelCase_				(  self    :				int       ,	snake_case    :				list[list[int | float]]       ,	snake_case    :				list[int]       ,	snake_case    :				int       ,	snake_case    :				float	):



											'''simple docstring'''


											for i in range(len(snake_case	)	):
																weights[j][i] += alpha * (sample[i] - weights[j][i])
											return weights


def        __a			(   )     ->     None:
					# Training Examples ( m, n )
					SCREAMING_SNAKE_CASE							:     Tuple						=				[[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]]

					# weight initialization ( n, C )
					SCREAMING_SNAKE_CASE							:     str						=				[[0.2, 0.6, 0.5, 0.9], [0.8, 0.4, 0.7, 0.3]]

					# training
					SCREAMING_SNAKE_CASE							:     int						=				SelfOrganizingMap()
					SCREAMING_SNAKE_CASE							:     List[Any]						=				3
					SCREAMING_SNAKE_CASE							:     Any						=				0.5

					for _ in range(_lowerCAmelCase   ):
										for j in range(len(_lowerCAmelCase   )   ):
															# training sample
															SCREAMING_SNAKE_CASE							:     int						=				training_samples[j]

															# Compute the winning vector
															SCREAMING_SNAKE_CASE							:     Dict						=				self_organizing_map.get_winner(_lowerCAmelCase      ,	_lowerCAmelCase   )

															# Update the winning vector
															SCREAMING_SNAKE_CASE							:     Dict						=				self_organizing_map.update(_lowerCAmelCase      ,	_lowerCAmelCase      ,	_lowerCAmelCase      ,	_lowerCAmelCase   )

    # classify test sample
					SCREAMING_SNAKE_CASE							:     int						=				[0, 0, 0, 1]
					SCREAMING_SNAKE_CASE							:     Optional[Any]						=				self_organizing_map.get_winner(_lowerCAmelCase      ,	_lowerCAmelCase   )

					# results
					print(F'''Clusters that the test sample belongs to : {winner}'''   )
					print(F'''Weights that have been trained : {weights}'''   )


# running the main() function
if __name__ == "__main__":
			main()






import gc
import unittest

from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline
from diffusers.utils import is_flax_available, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
 import jax
 import jax.numpy as jnp
 from flax.jax_utils import replicate
 from flax.training.common_utils import shard


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





     """simple docstring"""



     def     SCREAMING_SNAKE_CASE		(				self							:str	):





        '''simple docstring'''




        super().tearDown()
        gc.collect()



     def     SCREAMING_SNAKE_CASE		(				self							:int	):





        '''simple docstring'''




        A_   ,    A_       :					Optional[Any]        =      FlaxStableDiffusionPipeline.from_pretrained(
            "stabilityai/stable-diffusion-2"      ,   revision="bf16"      ,   dtype=jnp.bfloataa      ,   )

        A_       :					List[Any]        =      "A painting of a squirrel eating a burger"
        A_       :					int        =      jax.device_count()
        A_       :					Union[str, Any]        =      num_samples * [prompt]
        A_       :					Dict        =      sd_pipe.prepare_inputs(snake_case	)

        A_       :					List[Any]        =      replicate(snake_case	)
        A_       :					str        =      shard(snake_case	)

        A_       :					Any        =      jax.random.PRNGKey(0	)
        A_       :					Dict        =      jax.random.split(snake_case      ,   jax.device_count()	)

        A_       :					int        =      sd_pipe(snake_case      ,   snake_case      ,   snake_case      ,   num_inference_steps=25      ,   jit=snake_case	)[0]
        assert images.shape == (jax.device_count(), 1, 768, 768, 3)

        A_       :					List[str]        =      images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]	)
        A_       :					Union[str, Any]        =      images[0, 253:256, 253:256, -1]

        A_       :					Dict        =      jnp.asarray(jax.device_get(image_slice.flatten()	)	)
        A_       :					List[str]        =      jnp.array([0.4238, 0.4414, 0.4395, 0.4453, 0.4629, 0.4590, 0.4531, 0.45508, 0.4512]	)
        print(f"output_slice: {output_slice}"	)
        assert jnp.abs(output_slice - expected_slice	).max() < 1e-2





     def     SCREAMING_SNAKE_CASE		(				self							:Union[str, Any]	):





        '''simple docstring'''




        A_       :					str        =      "stabilityai/stable-diffusion-2"
        A_   ,    A_       :					Dict        =      FlaxDPMSolverMultistepScheduler.from_pretrained(snake_case      ,   subfolder="scheduler"	)
        A_   ,    A_       :					Any        =      FlaxStableDiffusionPipeline.from_pretrained(
            snake_case      ,   scheduler=snake_case      ,   revision="bf16"      ,   dtype=jnp.bfloataa      ,   )
        A_       :					Any        =      scheduler_params

        A_       :					Optional[Any]        =      "A painting of a squirrel eating a burger"
        A_       :					Dict        =      jax.device_count()
        A_       :					List[str]        =      num_samples * [prompt]
        A_       :					int        =      sd_pipe.prepare_inputs(snake_case	)

        A_       :					Optional[Any]        =      replicate(snake_case	)
        A_       :					Dict        =      shard(snake_case	)

        A_       :					Any        =      jax.random.PRNGKey(0	)
        A_       :					Union[str, Any]        =      jax.random.split(snake_case      ,   jax.device_count()	)

        A_       :					Union[str, Any]        =      sd_pipe(snake_case      ,   snake_case      ,   snake_case      ,   num_inference_steps=25      ,   jit=snake_case	)[0]
        assert images.shape == (jax.device_count(), 1, 768, 768, 3)

        A_       :					List[Any]        =      images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]	)
        A_       :					Union[str, Any]        =      images[0, 253:256, 253:256, -1]

        A_       :					Tuple        =      jnp.asarray(jax.device_get(image_slice.flatten()	)	)
        A_       :					str        =      jnp.array([0.4336, 0.42969, 0.4453, 0.4199, 0.4297, 0.4531, 0.4434, 0.4434, 0.4297]	)
        print(f"output_slice: {output_slice}"	)
        assert jnp.abs(output_slice - expected_slice	).max() < 1e-2
