

"""simple docstring"""






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       snake_case__  ( unittest.TestCase     ):







			def 			__lowerCAmelCase				(			self			: Dict  ):





								'''simple docstring'''



								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()







			def 			__lowerCAmelCase				(			self			: Any  ):





								'''simple docstring'''



								UpperCAmelCase			:   Any      =				StableDiffusionKDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4"  )
								UpperCAmelCase			:   List[str]      =				sd_pipe.to(lowercase  )
								sd_pipe.set_progress_bar_config(disable=lowercase  )

								sd_pipe.set_scheduler("sample_euler"  )

								UpperCAmelCase			:   List[str]      =				"A painting of a squirrel eating a burger"
								UpperCAmelCase			:   Any      =				torch.manual_seed(0  )
								UpperCAmelCase			:   List[Any]      =				sd_pipe([prompt]			,  generator=lowercase			,  guidance_scale=9.0			,  num_inference_steps=20			,  output_type="np"  )

								UpperCAmelCase			:   Tuple      =				output.images

								UpperCAmelCase			:   List[Any]      =				image[0, -3:, -3:, -1]

								assert image.shape == (1, 5_12, 5_12, 3)
								UpperCAmelCase			:   Optional[Any]      =				np.array([0.0_4_4_7, 0.0_4_9_2, 0.0_4_6_8, 0.0_4_0_8, 0.0_3_8_3, 0.0_4_0_8, 0.0_3_5_4, 0.0_3_8_0, 0.0_3_3_9]  )

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







			def 			__lowerCAmelCase				(			self			: str  ):





								'''simple docstring'''



								UpperCAmelCase			:   Tuple      =				StableDiffusionKDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base"  )
								UpperCAmelCase			:   str      =				sd_pipe.to(lowercase  )
								sd_pipe.set_progress_bar_config(disable=lowercase  )

								sd_pipe.set_scheduler("sample_euler"  )

								UpperCAmelCase			:   Optional[Any]      =				"A painting of a squirrel eating a burger"
								UpperCAmelCase			:   Union[str, Any]      =				torch.manual_seed(0  )
								UpperCAmelCase			:   Any      =				sd_pipe([prompt]			,  generator=lowercase			,  guidance_scale=9.0			,  num_inference_steps=20			,  output_type="np"  )

								UpperCAmelCase			:   Union[str, Any]      =				output.images

								UpperCAmelCase			:   Tuple      =				image[0, -3:, -3:, -1]

								assert image.shape == (1, 5_12, 5_12, 3)
								UpperCAmelCase			:   int      =				np.array([0.1_2_3_7, 0.1_3_2_0, 0.1_4_3_8, 0.1_3_5_9, 0.1_3_9_0, 0.1_1_3_2, 0.1_2_7_7, 0.1_1_7_5, 0.1_1_1_2]  )

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







			def 			__lowerCAmelCase				(			self			: int  ):





								'''simple docstring'''



								UpperCAmelCase			:   List[str]      =				StableDiffusionKDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base"  )
								UpperCAmelCase			:   Union[str, Any]      =				sd_pipe.to(lowercase  )
								sd_pipe.set_progress_bar_config(disable=lowercase  )

								sd_pipe.set_scheduler("sample_dpmpp_2m"  )

								UpperCAmelCase			:   Optional[int]      =				"A painting of a squirrel eating a burger"
								UpperCAmelCase			:   Optional[Any]      =				torch.manual_seed(0  )
								UpperCAmelCase			:   Optional[Any]      =				sd_pipe(
								    [prompt]			,  generator=lowercase			,  guidance_scale=7.5			,  num_inference_steps=15			,  output_type="np"			,  use_karras_sigmas=lowercase			,  )

								UpperCAmelCase			:   Union[str, Any]      =				output.images

								UpperCAmelCase			:   Any      =				image[0, -3:, -3:, -1]

								assert image.shape == (1, 5_12, 5_12, 3)
								UpperCAmelCase			:   Any      =				np.array(
								    [0.1_1_3_8_1_6_8_9, 0.1_2_1_1_2_9_2_1, 0.1_3_8_9_4_5_7, 0.1_2_5_4_9_6_0_6, 0.1_2_4_4_9_6_4, 0.1_0_8_3_1_5_1_7, 0.1_1_5_6_2_8_6_6, 0.1_0_8_6_7_8_1_6, 0.1_0_4_9_9_0_4_8]  )

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





"""simple docstring"""






from __future__ import annotations

snake_case_   :   str        =       list[list[int]]

# assigning initial values to the grid
snake_case_   :   Matrix        =       [
    [3, 0, 6, 5, 0, 8, 4, 0, 0],
    [5, 2, 0, 0, 0, 0, 0, 0, 0],
    [0, 8, 7, 0, 0, 0, 0, 3, 1],
    [0, 0, 3, 0, 1, 0, 0, 8, 0],
    [9, 0, 0, 8, 6, 3, 0, 0, 5],
    [0, 5, 0, 0, 9, 0, 6, 0, 0],
    [1, 3, 0, 0, 0, 0, 2, 5, 0],
    [0, 0, 0, 0, 0, 0, 0, 7, 4],
    [0, 0, 5, 2, 0, 6, 3, 0, 0],
]

# a grid with no solution
snake_case_   :   Matrix        =       [
    [5, 0, 6, 5, 0, 8, 4, 0, 3],
    [5, 2, 0, 0, 0, 0, 0, 0, 2],
    [1, 8, 7, 0, 0, 0, 0, 3, 1],
    [0, 0, 3, 0, 1, 0, 0, 8, 0],
    [9, 0, 0, 8, 6, 3, 0, 0, 5],
    [0, 5, 0, 0, 9, 0, 6, 0, 0],
    [1, 3, 0, 0, 0, 0, 2, 5, 0],
    [0, 0, 0, 0, 0, 0, 0, 7, 4],
    [0, 0, 5, 2, 0, 6, 3, 0, 0],
]



def   lowercase_	(    _lowercase       :		Matrix							,      _lowercase       :		int							,      _lowercase       :		int							,      _lowercase       :		int	):




					'''simple docstring'''






					for i in range(9	):
										if grid[row][i] == n or grid[i][column] == n:
															return False

					for i in range(3	):
										for j in range(3	):
															if grid[(row - row % 3) + i][(column - column % 3) + j] == n:
																				return False

					return True



def   lowercase_	(    _lowercase       :		Matrix	):




					'''simple docstring'''






					for i in range(9	):
										for j in range(9	):
															if grid[i][j] == 0:
																				return i, j
					return None



def   lowercase_	(    _lowercase       :		Matrix	):




					'''simple docstring'''






					if location := find_empty_location(_lowercase	):
										UpperCAmelCase      ,							UpperCAmelCase			:   List[str]      =				location
					else:
										# If the location is ``None``, then the grid is solved.
										return grid

					for digit in range(1							,      10	):
										if is_safe(_lowercase							,      _lowercase							,      _lowercase							,      _lowercase	):
															UpperCAmelCase			:   int      =				digit

															if sudoku(_lowercase	) is not None:
																				return grid

															UpperCAmelCase			:   Optional[Any]      =				0

					return None



def   lowercase_	(    _lowercase       :		Matrix	):




					'''simple docstring'''






					for row in grid:
										for cell in row:
															print(_lowercase							,      end=" "	)
										print()


if __name__ == "__main__":
	# make a copy of grid so that you can compare with the unmodified grid
	for example_grid in (initial_grid, no_solution):
		print("""\nExample grid:\n""" + """=""" * 2_0)
		print_solution(example_grid)
		print("""\nExample grid solution:""")
		snake_case_   :   Dict        =       sudoku(example_grid)
		if solution is not None:
			print_solution(solution)
		else:
			print("""Cannot find a solution.""")


