
"""simple docstring"""
from typing import Dict, List, Optional, Tuple, Union

import torch

from ...models import AutoencoderKL, TransformeraDModel
from ...schedulers import KarrasDiffusionSchedulers
from ...utils import randn_tensor
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput
class       lowerCAmelCase_		(				A__ ):



							'''simple docstring'''





							def __init__( self    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_ = None    ,				)       ->  int:
													super().__init__()
													self.register_modules(transformer=snake_case_    ,				vae=snake_case_    ,				scheduler=snake_case_				)

													# create a imagenet -> id dictionary for easier use
													__lowerCAmelCase     =  {}
													if idalabel is not None:
																			for key, value in idalabel.items():
																									for label in value.split(""","""				):
																															__lowerCAmelCase     =  int(snake_case_				)
																			__lowerCAmelCase     =  dict(sorted(self.labels.items()				)				)




							def    A__						( self    ,				snake_case_				)       ->  List[int]:

													if not isinstance(snake_case_    ,				snake_case_				):
																			__lowerCAmelCase     =  list(snake_case_				)

													for l in label:
																			if l not in self.labels:
																									raise ValueError(
																									    f"""{l} does not exist. Please make sure to select one of the following labels: \n {self.labels}."""				)

													return [self.labels[l] for l in label]




							@torch.no_grad()
							def __call__( self    ,				snake_case_    ,				snake_case_ = 4.0    ,				snake_case_ = None    ,				snake_case_ = 50    ,				snake_case_ = "pil"    ,				snake_case_ = True    ,				)       ->  Union[ImagePipelineOutput, Tuple]:
													__lowerCAmelCase     =  len(snake_case_				)
													__lowerCAmelCase     =  self.transformer.config.sample_size
													__lowerCAmelCase     =  self.transformer.config.in_channels

													__lowerCAmelCase     =  randn_tensor(
													    shape=(batch_size, latent_channels, latent_size, latent_size)    ,				generator=snake_case_    ,				device=self.device    ,				dtype=self.transformer.dtype    ,				)
													__lowerCAmelCase     =  torch.cat([latents] * 2				) if guidance_scale > 1 else latents

													__lowerCAmelCase     =  torch.tensor(snake_case_    ,				device=self.device				).reshape(-1				)
													__lowerCAmelCase     =  torch.tensor([1_000] * batch_size    ,				device=self.device				)
													__lowerCAmelCase     =  torch.cat([class_labels, class_null]    ,				0				) if guidance_scale > 1 else class_labels

													# set step values
													self.scheduler.set_timesteps(snake_case_				)

													for t in self.progress_bar(self.scheduler.timesteps				):
																			if guidance_scale > 1:
																									__lowerCAmelCase     =  latent_model_input[: len(snake_case_				) // 2]
																									__lowerCAmelCase     =  torch.cat([half, half]    ,				dim=0				)
																			__lowerCAmelCase     =  self.scheduler.scale_model_input(snake_case_    ,				snake_case_				)

																			__lowerCAmelCase     =  t
																			if not torch.is_tensor(snake_case_				):
																									# TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can
																									# This would be a good case for the `match` statement (Python 3.10+)
																									__lowerCAmelCase     =  latent_model_input.device.type == """mps"""
																									if isinstance(snake_case_    ,				snake_case_				):
																															__lowerCAmelCase     =  torch.floataa if is_mps else torch.floataa
																									else:
																															__lowerCAmelCase     =  torch.intaa if is_mps else torch.intaa
																									__lowerCAmelCase     =  torch.tensor([timesteps]    ,				dtype=snake_case_    ,				device=latent_model_input.device				)
																			elif len(timesteps.shape				) == 0:
																									__lowerCAmelCase     =  timesteps[None].to(latent_model_input.device				)
																			# broadcast to batch dimension in a way that's compatible with ONNX/Core ML
																			__lowerCAmelCase     =  timesteps.expand(latent_model_input.shape[0]				)
																			# predict noise model_output
																			__lowerCAmelCase     =  self.transformer(
																			    snake_case_    ,				timestep=snake_case_    ,				class_labels=snake_case_				).sample

																			# perform guidance
																			if guidance_scale > 1:
																									__lowerCAmelCase   ,     __lowerCAmelCase     =  noise_pred[:, :latent_channels], noise_pred[:, latent_channels:]
																									__lowerCAmelCase   ,     __lowerCAmelCase     =  torch.split(snake_case_    ,				len(snake_case_				) // 2    ,				dim=0				)

																									__lowerCAmelCase     =  uncond_eps + guidance_scale * (cond_eps - uncond_eps)
																									__lowerCAmelCase     =  torch.cat([half_eps, half_eps]    ,				dim=0				)

																									__lowerCAmelCase     =  torch.cat([eps, rest]    ,				dim=1				)

																			# learned sigma
																			if self.transformer.config.out_channels // 2 == latent_channels:
																									__lowerCAmelCase   ,     __lowerCAmelCase     =  torch.split(snake_case_    ,				snake_case_    ,				dim=1				)
																			else:
																									__lowerCAmelCase     =  noise_pred

																			# compute previous image: x_t -> x_t-1
																			__lowerCAmelCase     =  self.scheduler.step(snake_case_    ,				snake_case_    ,				snake_case_				).prev_sample

													if guidance_scale > 1:
																			__lowerCAmelCase   ,     __lowerCAmelCase     =  latent_model_input.chunk(2    ,				dim=0				)
													else:
																			__lowerCAmelCase     =  latent_model_input

													__lowerCAmelCase     =  1 / self.vae.config.scaling_factor * latents
													__lowerCAmelCase     =  self.vae.decode(snake_case_				).sample

													__lowerCAmelCase     =  (samples / 2 + 0.5).clamp(0    ,				1				)

													# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
													__lowerCAmelCase     =  samples.cpu().permute(0    ,				2    ,				3    ,				1				).float().numpy()

													if output_type == "pil":
																			__lowerCAmelCase     =  self.numpy_to_pil(snake_case_				)

													if not return_dict:
																			return (samples,)

													return ImagePipelineOutput(images=snake_case_				)


"""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())
