


import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()
@slow
@require_torch_gpu
class 		__magic_name__						(unittest.TestCase				):





     '''simple docstring'''



     def 	SCREAMING_SNAKE_CASE__						(					self:int   ):
       # clean up the VRAM after each test
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()

     def 	SCREAMING_SNAKE_CASE__						(					self:List[str]   ):
       snake_case__						= StableDiffusionKDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4'''   )
       snake_case__						= sd_pipe.to(_a   )
       sd_pipe.set_progress_bar_config(disable=_a   )

       sd_pipe.set_scheduler('''sample_euler'''   )

       snake_case__						= '''A painting of a squirrel eating a burger'''
       snake_case__						= torch.manual_seed(0   )
       snake_case__						= sd_pipe([prompt]     ,		generator=_a     ,		guidance_scale=9.0     ,		num_inference_steps=20     ,		output_type='''np'''   )

       snake_case__						= output.images

       snake_case__						= image[0, -3:, -3:, -1]

       assert image.shape == (1, 5_12, 5_12, 3)
       snake_case__						= np.array([0.0447, 0.0492, 0.0468, 0.0408, 0.0383, 0.0408, 0.0354, 0.0380, 0.0339]   )

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

     def 	SCREAMING_SNAKE_CASE__						(					self:Tuple   ):
       snake_case__						= StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base'''   )
       snake_case__						= sd_pipe.to(_a   )
       sd_pipe.set_progress_bar_config(disable=_a   )

       sd_pipe.set_scheduler('''sample_euler'''   )

       snake_case__						= '''A painting of a squirrel eating a burger'''
       snake_case__						= torch.manual_seed(0   )
       snake_case__						= sd_pipe([prompt]     ,		generator=_a     ,		guidance_scale=9.0     ,		num_inference_steps=20     ,		output_type='''np'''   )

       snake_case__						= output.images

       snake_case__						= image[0, -3:, -3:, -1]

       assert image.shape == (1, 5_12, 5_12, 3)
       snake_case__						= np.array([0.1237, 0.1320, 0.1438, 0.1359, 0.1390, 0.1132, 0.1277, 0.1175, 0.1112]   )

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







     def 	SCREAMING_SNAKE_CASE__						(					self:List[str]   ):
       snake_case__						= StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base'''   )
       snake_case__						= sd_pipe.to(_a   )
       sd_pipe.set_progress_bar_config(disable=_a   )

       sd_pipe.set_scheduler('''sample_dpmpp_2m'''   )

       snake_case__						= '''A painting of a squirrel eating a burger'''
       snake_case__						= torch.manual_seed(0   )
       snake_case__						= sd_pipe(
           [prompt]     ,		generator=_a     ,		guidance_scale=7.5     ,		num_inference_steps=15     ,		output_type='''np'''     ,		use_karras_sigmas=_a     ,		)

       snake_case__						= output.images

       snake_case__						= image[0, -3:, -3:, -1]

       assert image.shape == (1, 5_12, 5_12, 3)
       snake_case__						= np.array(
           [0.11381689, 0.12112921, 0.1389457, 0.12549606, 0.1244964, 0.10831517, 0.11562866, 0.10867816, 0.10499048]   )

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





import math
class 		__magic_name__						:





     '''simple docstring'''



     def 	SCREAMING_SNAKE_CASE__						(					self:Optional[int]     ,		_a:list[list[float]]     ,		_a:list[int]   ):
       snake_case__						= 0.0
       snake_case__						= 0.0
       for i in range(len(_a   )   ):
         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 	SCREAMING_SNAKE_CASE__						(					self:Tuple     ,		_a:list[list[int | float]]     ,		_a:list[int]     ,		_a:int     ,		_a:float   ):
       for i in range(len(_a   )   ):
         weights[j][i] += alpha * (sample[i] - weights[j][i])
       return weights







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

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

  # training
  snake_case__						= SelfOrganizingMap()
  snake_case__						= 3
  snake_case__						= 0.5

  for _ in range(__lowerCAmelCase				):
    for j in range(len(__lowerCAmelCase				)				):
      # training sample
      snake_case__						= training_samples[j]

      # Compute the winning vector
      snake_case__						= self_organizing_map.get_winner(__lowerCAmelCase							,   __lowerCAmelCase				)

      # Update the winning vector
      snake_case__						= self_organizing_map.update(__lowerCAmelCase							,   __lowerCAmelCase							,   __lowerCAmelCase							,   __lowerCAmelCase				)

    # classify test sample
  snake_case__						= [0, 0, 0, 1]
  snake_case__						= 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()

