
"""simple docstring"""
import gc
import unittest

import numpy as np
import torch
from torch.backends.cuda import sdp_kernel

from diffusers import (
    CMStochasticIterativeScheduler,
    ConsistencyModelPipeline,
    UNetaDModel,
)
from diffusers.utils import randn_tensor, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_a, require_torch_gpu

from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()
class       lowerCAmelCase_		(				A__				,				unittest.TestCase ):



							'''simple docstring'''





							_snake_case						      =		ConsistencyModelPipeline
							_snake_case						      =		UNCONDITIONAL_IMAGE_GENERATION_PARAMS
							_snake_case						      =		UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS

							# Override required_optional_params to remove num_images_per_prompt
							_snake_case						      =		frozenset(
							    [
							        '''num_inference_steps''',
							        '''generator''',
							        '''latents''',
							        '''output_type''',
							        '''return_dict''',
							        '''callback''',
							        '''callback_steps''',
							    ] )




							@property
							def    A__						( self				)       ->  Union[str, Any]:
													__lowerCAmelCase     =  UNetaDModel.from_pretrained(
													    """diffusers/consistency-models-test"""    ,				subfolder="""test_unet"""    ,				)
													return unet




							@property
							def    A__						( self				)       ->  int:
													__lowerCAmelCase     =  UNetaDModel.from_pretrained(
													    """diffusers/consistency-models-test"""    ,				subfolder="""test_unet_class_cond"""    ,				)
													return unet




							def    A__						( self    ,				snake_case_=False				)       ->  List[str]:
													if class_cond:
																			__lowerCAmelCase     =  self.dummy_cond_unet
													else:
																			__lowerCAmelCase     =  self.dummy_uncond_unet

													# Default to CM multistep sampler
													__lowerCAmelCase     =  CMStochasticIterativeScheduler(
													    num_train_timesteps=40    ,				sigma_min=0.002    ,				sigma_max=80.0    ,				)

													__lowerCAmelCase     =  {
													    """unet""": unet,
													    """scheduler""": scheduler,
													}

													return components




							def    A__						( self    ,				snake_case_    ,				snake_case_=0				)       ->  Optional[Any]:
													if str(snake_case_				).startswith("""mps"""				):
																			__lowerCAmelCase     =  torch.manual_seed(snake_case_				)
													else:
																			__lowerCAmelCase     =  torch.Generator(device=snake_case_				).manual_seed(snake_case_				)

													__lowerCAmelCase     =  {
													    """batch_size""": 1,
													    """num_inference_steps""": None,
													    """timesteps""": [22, 0],
													    """generator""": generator,
													    """output_type""": """np""",
													}

													return inputs




							def    A__						( self				)       ->  Tuple:
													__lowerCAmelCase     =  """cpu"""  # ensure determinism for the device-dependent torch.Generator
													__lowerCAmelCase     =  self.get_dummy_components()
													__lowerCAmelCase     =  ConsistencyModelPipeline(**snake_case_				)
													__lowerCAmelCase     =  pipe.to(snake_case_				)
													pipe.set_progress_bar_config(disable=snake_case_				)

													__lowerCAmelCase     =  self.get_dummy_inputs(snake_case_				)
													__lowerCAmelCase     =  pipe(**snake_case_				).images
													assert image.shape == (1, 32, 32, 3)

													__lowerCAmelCase     =  image[0, -3:, -3:, -1]
													__lowerCAmelCase     =  np.array([0.3_572, 0.6_273, 0.4_031, 0.3_961, 0.4_321, 0.5_730, 0.5_266, 0.4_780, 0.5_004]				)

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




							def    A__						( self				)       ->  Any:
													__lowerCAmelCase     =  """cpu"""  # ensure determinism for the device-dependent torch.Generator
													__lowerCAmelCase     =  self.get_dummy_components(class_cond=snake_case_				)
													__lowerCAmelCase     =  ConsistencyModelPipeline(**snake_case_				)
													__lowerCAmelCase     =  pipe.to(snake_case_				)
													pipe.set_progress_bar_config(disable=snake_case_				)

													__lowerCAmelCase     =  self.get_dummy_inputs(snake_case_				)
													__lowerCAmelCase     =  0
													__lowerCAmelCase     =  pipe(**snake_case_				).images
													assert image.shape == (1, 32, 32, 3)

													__lowerCAmelCase     =  image[0, -3:, -3:, -1]
													__lowerCAmelCase     =  np.array([0.3_572, 0.6_273, 0.4_031, 0.3_961, 0.4_321, 0.5_730, 0.5_266, 0.4_780, 0.5_004]				)

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




							def    A__						( self				)       ->  List[str]:
													__lowerCAmelCase     =  """cpu"""  # ensure determinism for the device-dependent torch.Generator
													__lowerCAmelCase     =  self.get_dummy_components()
													__lowerCAmelCase     =  ConsistencyModelPipeline(**snake_case_				)
													__lowerCAmelCase     =  pipe.to(snake_case_				)
													pipe.set_progress_bar_config(disable=snake_case_				)

													__lowerCAmelCase     =  self.get_dummy_inputs(snake_case_				)
													__lowerCAmelCase     =  1
													__lowerCAmelCase     =  None
													__lowerCAmelCase     =  pipe(**snake_case_				).images
													assert image.shape == (1, 32, 32, 3)

													__lowerCAmelCase     =  image[0, -3:, -3:, -1]
													__lowerCAmelCase     =  np.array([0.5_004, 0.5_004, 0.4_994, 0.5_008, 0.4_976, 0.5_018, 0.4_990, 0.4_982, 0.4_987]				)

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




							def    A__						( self				)       ->  Dict:
													__lowerCAmelCase     =  """cpu"""  # ensure determinism for the device-dependent torch.Generator
													__lowerCAmelCase     =  self.get_dummy_components(class_cond=snake_case_				)
													__lowerCAmelCase     =  ConsistencyModelPipeline(**snake_case_				)
													__lowerCAmelCase     =  pipe.to(snake_case_				)
													pipe.set_progress_bar_config(disable=snake_case_				)

													__lowerCAmelCase     =  self.get_dummy_inputs(snake_case_				)
													__lowerCAmelCase     =  1
													__lowerCAmelCase     =  None
													__lowerCAmelCase     =  0
													__lowerCAmelCase     =  pipe(**snake_case_				).images
													assert image.shape == (1, 32, 32, 3)

													__lowerCAmelCase     =  image[0, -3:, -3:, -1]
													__lowerCAmelCase     =  np.array([0.5_004, 0.5_004, 0.4_994, 0.5_008, 0.4_976, 0.5_018, 0.4_990, 0.4_982, 0.4_987]				)

													assert np.abs(image_slice.flatten() - expected_slice				).max() < 1e-3
@slow
@require_torch_gpu
class       lowerCAmelCase_		(				unittest.TestCase ):



							'''simple docstring'''





							def    A__						( self				)       ->  int:
													super().tearDown()
													gc.collect()
													torch.cuda.empty_cache()




							def    A__						( self    ,				snake_case_=0    ,				snake_case_=False    ,				snake_case_="cpu"    ,				snake_case_=torch.floataa    ,				snake_case_=(1, 3, 64, 64)				)       ->  Dict:
													__lowerCAmelCase     =  torch.manual_seed(snake_case_				)

													__lowerCAmelCase     =  {
													    """num_inference_steps""": None,
													    """timesteps""": [22, 0],
													    """class_labels""": 0,
													    """generator""": generator,
													    """output_type""": """np""",
													}

													if get_fixed_latents:
																			__lowerCAmelCase     =  self.get_fixed_latents(seed=snake_case_    ,				device=snake_case_    ,				dtype=snake_case_    ,				shape=snake_case_				)
																			__lowerCAmelCase     =  latents

													return inputs




							def    A__						( self    ,				snake_case_=0    ,				snake_case_="cpu"    ,				snake_case_=torch.floataa    ,				snake_case_=(1, 3, 64, 64)				)       ->  Any:
													if type(snake_case_				) == str:
																			__lowerCAmelCase     =  torch.device(snake_case_				)
													__lowerCAmelCase     =  torch.Generator(device=snake_case_				).manual_seed(snake_case_				)
													__lowerCAmelCase     =  randn_tensor(snake_case_    ,				generator=snake_case_    ,				device=snake_case_    ,				dtype=snake_case_				)
													return latents




							def    A__						( self				)       ->  Tuple:
													__lowerCAmelCase     =  UNetaDModel.from_pretrained("""diffusers/consistency_models"""    ,				subfolder="""diffusers_cd_imagenet64_l2"""				)
													__lowerCAmelCase     =  CMStochasticIterativeScheduler(
													    num_train_timesteps=40    ,				sigma_min=0.002    ,				sigma_max=80.0    ,				)
													__lowerCAmelCase     =  ConsistencyModelPipeline(unet=snake_case_    ,				scheduler=snake_case_				)
													pipe.to(torch_device=snake_case_				)
													pipe.set_progress_bar_config(disable=snake_case_				)

													__lowerCAmelCase     =  self.get_inputs()
													__lowerCAmelCase     =  pipe(**snake_case_				).images
													assert image.shape == (1, 64, 64, 3)

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

													__lowerCAmelCase     =  np.array([0.0_888, 0.0_881, 0.0_666, 0.0_479, 0.0_292, 0.0_195, 0.0_201, 0.0_163, 0.0_254]				)

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




							def    A__						( self				)       ->  List[str]:
													__lowerCAmelCase     =  UNetaDModel.from_pretrained("""diffusers/consistency_models"""    ,				subfolder="""diffusers_cd_imagenet64_l2"""				)
													__lowerCAmelCase     =  CMStochasticIterativeScheduler(
													    num_train_timesteps=40    ,				sigma_min=0.002    ,				sigma_max=80.0    ,				)
													__lowerCAmelCase     =  ConsistencyModelPipeline(unet=snake_case_    ,				scheduler=snake_case_				)
													pipe.to(torch_device=snake_case_				)
													pipe.set_progress_bar_config(disable=snake_case_				)

													__lowerCAmelCase     =  self.get_inputs()
													__lowerCAmelCase     =  1
													__lowerCAmelCase     =  None
													__lowerCAmelCase     =  pipe(**snake_case_				).images
													assert image.shape == (1, 64, 64, 3)

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

													__lowerCAmelCase     =  np.array([0.0_340, 0.0_152, 0.0_063, 0.0_267, 0.0_221, 0.0_107, 0.0_416, 0.0_186, 0.0_217]				)

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




							@require_torch_a
							def    A__						( self				)       ->  Tuple:
													__lowerCAmelCase     =  UNetaDModel.from_pretrained("""diffusers/consistency_models"""    ,				subfolder="""diffusers_cd_imagenet64_l2"""				)
													__lowerCAmelCase     =  CMStochasticIterativeScheduler(
													    num_train_timesteps=40    ,				sigma_min=0.002    ,				sigma_max=80.0    ,				)
													__lowerCAmelCase     =  ConsistencyModelPipeline(unet=snake_case_    ,				scheduler=snake_case_				)
													pipe.to(torch_device=snake_case_    ,				torch_dtype=torch.floataa				)
													pipe.set_progress_bar_config(disable=snake_case_				)

													__lowerCAmelCase     =  self.get_inputs(get_fixed_latents=snake_case_    ,				device=snake_case_				)
													# Ensure usage of flash attention in torch 2.0
													with sdp_kernel(enable_flash=snake_case_    ,				enable_math=snake_case_    ,				enable_mem_efficient=snake_case_				):
																			__lowerCAmelCase     =  pipe(**snake_case_				).images
													assert image.shape == (1, 64, 64, 3)

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

													__lowerCAmelCase     =  np.array([0.1_875, 0.1_428, 0.1_289, 0.2_151, 0.2_092, 0.1_477, 0.1_877, 0.1_641, 0.1_353]				)

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




							@require_torch_a
							def    A__						( self				)       ->  Union[str, Any]:
													__lowerCAmelCase     =  UNetaDModel.from_pretrained("""diffusers/consistency_models"""    ,				subfolder="""diffusers_cd_imagenet64_l2"""				)
													__lowerCAmelCase     =  CMStochasticIterativeScheduler(
													    num_train_timesteps=40    ,				sigma_min=0.002    ,				sigma_max=80.0    ,				)
													__lowerCAmelCase     =  ConsistencyModelPipeline(unet=snake_case_    ,				scheduler=snake_case_				)
													pipe.to(torch_device=snake_case_    ,				torch_dtype=torch.floataa				)
													pipe.set_progress_bar_config(disable=snake_case_				)

													__lowerCAmelCase     =  self.get_inputs(get_fixed_latents=snake_case_    ,				device=snake_case_				)
													__lowerCAmelCase     =  1
													__lowerCAmelCase     =  None
													# Ensure usage of flash attention in torch 2.0
													with sdp_kernel(enable_flash=snake_case_    ,				enable_math=snake_case_    ,				enable_mem_efficient=snake_case_				):
																			__lowerCAmelCase     =  pipe(**snake_case_				).images
													assert image.shape == (1, 64, 64, 3)

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

													__lowerCAmelCase     =  np.array([0.1_663, 0.1_948, 0.2_275, 0.1_680, 0.1_204, 0.1_245, 0.1_858, 0.1_338, 0.2_095]				)

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


"""simple docstring"""
import os

def  lowercase     ():
						with open(os.path.dirname(_lowerCAmelCase						) + """/grid.txt"""						) as f:
												__lowerCAmelCase     =  []  # noqa: E741
												for _ in range(20						):
																		l.append([int(_lowerCAmelCase						) for x in f.readline().split()]						)

												__lowerCAmelCase     =  0

												# right
												for i in range(20						):
																		for j in range(17						):
																								__lowerCAmelCase     =  l[i][j] * l[i][j + 1] * l[i][j + 2] * l[i][j + 3]
																								if temp > maximum:
																														__lowerCAmelCase     =  temp

        # down
												for i in range(17						):
																		for j in range(20						):
																								__lowerCAmelCase     =  l[i][j] * l[i + 1][j] * l[i + 2][j] * l[i + 3][j]
																								if temp > maximum:
																														__lowerCAmelCase     =  temp

        # diagonal 1
												for i in range(17						):
																		for j in range(17						):
																								__lowerCAmelCase     =  l[i][j] * l[i + 1][j + 1] * l[i + 2][j + 2] * l[i + 3][j + 3]
																								if temp > maximum:
																														__lowerCAmelCase     =  temp

        # diagonal 2
												for i in range(17						):
																		for j in range(3 ,    20						):
																								__lowerCAmelCase     =  l[i][j] * l[i + 1][j - 1] * l[i + 2][j - 2] * l[i + 3][j - 3]
																								if temp > maximum:
																														__lowerCAmelCase     =  temp
												return maximum


if __name__ == "__main__":
			print(solution())
