
"""simple docstring"""

from typing import Optional

import torch
from PIL import Image
from tqdm.auto import tqdm
from transformers import CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, DiffusionPipeline, UNetaDConditionModel
from diffusers.image_processor import VaeImageProcessor
from diffusers.utils import (
    deprecate,
)

class __lowerCAmelCase						( __SCREAMING_SNAKE_CASE     ):





     def __init__(      self      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase = 0.9_3      ,	__UpperCAmelCase = True      ,	):

      '''simple docstring'''




      __UpperCamelCase							    =			mixing_coeff
      __UpperCamelCase							    =			leapfrog_steps

      super().__init__()
      self.register_modules(
          vae=__UpperCAmelCase      ,	text_encoder=__UpperCAmelCase      ,	tokenizer=__UpperCAmelCase      ,	unet=__UpperCAmelCase      ,	scheduler=__UpperCAmelCase      ,	)

      __UpperCamelCase							    =			2 ** (len(self.vae.config.block_out_channels ) - 1)
      __UpperCamelCase							    =			VaeImageProcessor(vae_scale_factor=self.vae_scale_factor )





     def 		UpperCAmelCase   (      self      ,	__UpperCAmelCase      ,	__UpperCAmelCase = None      ,	__UpperCAmelCase = False ):

      '''simple docstring'''




      __UpperCamelCase							    =			self.tokenizer(
          __UpperCAmelCase      ,	padding='max_length'      ,	max_length=self.tokenizer.model_max_length      ,	truncation=__UpperCAmelCase      ,	return_tensors='pt'      ,	)

      __UpperCamelCase							    =			self.text_encoder(text_inputs.input_ids.to(self.device ) ).last_hidden_state

      __UpperCamelCase							    =			prompt_embeds.to(dtype=self.text_encoder.dtype      ,	device=self.device )

      if do_classifier_free_guidance:
       __UpperCamelCase							    =			'' if negative_prompt is None else negative_prompt

       __UpperCamelCase							    =			self.tokenizer(
           __UpperCAmelCase      ,	padding='max_length'      ,	max_length=self.tokenizer.model_max_length      ,	truncation=__UpperCAmelCase      ,	return_tensors='pt'      ,	)

       __UpperCamelCase							    =			self.text_encoder(uncond_input.input_ids.to(self.device ) ).last_hidden_state

       __UpperCamelCase							    =			torch.cat([negative_prompt_embeds, prompt_embeds] )

      return prompt_embeds





     def 		UpperCAmelCase   (      self      ,	__UpperCAmelCase      ,	__UpperCAmelCase ):

      '''simple docstring'''




      __UpperCamelCase							    =			self.mixing_coeff * x + (1 - self.mixing_coeff) * y
      __UpperCamelCase							    =			self.mixing_coeff * y + (1 - self.mixing_coeff) * x

      return [x, y]





     def 		UpperCAmelCase   (      self      ,	__UpperCAmelCase      ,	__UpperCAmelCase ):

      '''simple docstring'''




      __UpperCamelCase							    =			(y - (1 - self.mixing_coeff) * x) / self.mixing_coeff
      __UpperCamelCase							    =			(x - (1 - self.mixing_coeff) * y) / self.mixing_coeff

      return [x, y]





     def 		UpperCAmelCase   (      self      ,	__UpperCAmelCase ):

      '''simple docstring'''




      __UpperCamelCase							    =			int(__UpperCAmelCase )

      __UpperCamelCase							    =			self.scheduler.alphas_cumprod[t] if t >= 0 else self.scheduler.final_alpha_cumprod

      return alpha_prod, 1 - alpha_prod





     def 		UpperCAmelCase   (      self      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	):

      '''simple docstring'''




      __UpperCamelCase							    =			timestep - self.scheduler.config.num_train_timesteps / self.scheduler.num_inference_steps

      __UpperCamelCase    ,      __UpperCamelCase							    =			self._get_alpha_and_beta(__UpperCAmelCase )
      __UpperCamelCase    ,      __UpperCamelCase							    =			self._get_alpha_and_beta(__UpperCAmelCase )

      __UpperCamelCase							    =			(alpha_prod_t_prev / alpha_prod_t) ** 0.5
      __UpperCamelCase							    =			-a_t * (beta_prod_t**0.5) + beta_prod_t_prev**0.5

      __UpperCamelCase							    =			(base - b_t * model_output) / a_t

      return model_input, next_model_input.to(base.dtype )





     def 		UpperCAmelCase   (      self      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	):

      '''simple docstring'''




      __UpperCamelCase							    =			timestep - self.scheduler.config.num_train_timesteps / self.scheduler.num_inference_steps

      __UpperCamelCase    ,      __UpperCamelCase							    =			self._get_alpha_and_beta(__UpperCAmelCase )
      __UpperCamelCase    ,      __UpperCamelCase							    =			self._get_alpha_and_beta(__UpperCAmelCase )

      __UpperCamelCase							    =			(alpha_prod_t_prev / alpha_prod_t) ** 0.5
      __UpperCamelCase							    =			-a_t * (beta_prod_t**0.5) + beta_prod_t_prev**0.5
      __UpperCamelCase							    =			a_t * base + b_t * model_output

      return model_input, next_model_input.to(base.dtype )





     @torch.no_grad()
     def 		UpperCAmelCase   (      self      ,	__UpperCAmelCase ):

      '''simple docstring'''




      __UpperCamelCase							    =			1 / self.vae.config.scaling_factor * latents
      __UpperCamelCase							    =			self.vae.decode(__UpperCAmelCase ).sample
      __UpperCamelCase							    =			(image / 2 + 0.5).clamp(0      ,	1 )
      return image





     @torch.no_grad()
     def 		UpperCAmelCase   (      self      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase = None      ,	):

      '''simple docstring'''




      __UpperCamelCase							    =			guidance_scale > 1.0

      __UpperCamelCase							    =			image.to(device=self.device      ,	dtype=text_embeds.dtype )
      __UpperCamelCase							    =			self.vae.encode(__UpperCAmelCase ).latent_dist.sample(__UpperCAmelCase )

      __UpperCamelCase							    =			self.vae.config.scaling_factor * latent

      __UpperCamelCase							    =			[latent.clone(), latent.clone()]

      for i, t in tqdm(enumerate(__UpperCAmelCase )      ,	total=len(__UpperCAmelCase ) ):
       __UpperCamelCase							    =			self.noise_mixing_layer(x=coupled_latents[0]      ,	y=coupled_latents[1] )

       # j - model_input index, k - base index
       for j in range(2 ):
        __UpperCamelCase							    =			j ^ 1

        if self.leapfrog_steps:
         if i % 2 == 0:
          __UpperCamelCase    ,      __UpperCamelCase							    =			j, k

        __UpperCamelCase							    =			coupled_latents[j]
        __UpperCamelCase							    =			coupled_latents[k]

        __UpperCamelCase							    =			torch.cat([model_input] * 2 ) if do_classifier_free_guidance else model_input

        __UpperCamelCase							    =			self.unet(__UpperCAmelCase      ,	__UpperCAmelCase      ,	encoder_hidden_states=__UpperCAmelCase ).sample

        if do_classifier_free_guidance:
         __UpperCamelCase    ,      __UpperCamelCase							    =			noise_pred.chunk(2 )
         __UpperCamelCase							    =			noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

        __UpperCamelCase    ,      __UpperCamelCase							    =			self.noise_step(
            base=__UpperCAmelCase      ,	model_input=__UpperCAmelCase      ,	model_output=__UpperCAmelCase      ,	timestep=__UpperCAmelCase      ,	)

        __UpperCamelCase							    =			model_input

      return coupled_latents







     @torch.no_grad()
     def __call__(      self      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase = 3.0      ,	__UpperCAmelCase = 50      ,	__UpperCAmelCase = 0.8      ,	__UpperCAmelCase = None      ,	__UpperCAmelCase = None      ,	__UpperCAmelCase = "pil"      ,	):

      '''simple docstring'''




      __UpperCamelCase							    =			guidance_scale > 1.0

      __UpperCamelCase							    =			self.image_processor.preprocess(__UpperCAmelCase )

      __UpperCamelCase							    =			self._encode_prompt(__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase )
      __UpperCamelCase							    =			self._encode_prompt(__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase )

      self.scheduler.set_timesteps(__UpperCAmelCase      ,	self.device )

      __UpperCamelCase							    =			num_inference_steps - int(num_inference_steps * strength )
      __UpperCamelCase							    =			self.scheduler.timesteps[t_limit:]
      __UpperCamelCase							    =			fwd_timesteps.flip(0 )

      __UpperCamelCase							    =			self.prepare_latents(__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase )

      for i, t in tqdm(enumerate(__UpperCAmelCase )      ,	total=len(__UpperCAmelCase ) ):
       # j - model_input index, k - base index
       for k in range(2 ):
        __UpperCamelCase							    =			k ^ 1

        if self.leapfrog_steps:
         if i % 2 == 1:
          __UpperCamelCase    ,      __UpperCamelCase							    =			j, k

        __UpperCamelCase							    =			coupled_latents[j]
        __UpperCamelCase							    =			coupled_latents[k]

        __UpperCamelCase							    =			torch.cat([model_input] * 2 ) if do_classifier_free_guidance else model_input

        __UpperCamelCase							    =			self.unet(__UpperCAmelCase      ,	__UpperCAmelCase      ,	encoder_hidden_states=__UpperCAmelCase ).sample

        if do_classifier_free_guidance:
         __UpperCamelCase    ,      __UpperCamelCase							    =			noise_pred.chunk(2 )
         __UpperCamelCase							    =			noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

        __UpperCamelCase    ,      __UpperCamelCase							    =			self.denoise_step(
            base=__UpperCAmelCase      ,	model_input=__UpperCAmelCase      ,	model_output=__UpperCAmelCase      ,	timestep=__UpperCAmelCase      ,	)

        __UpperCamelCase							    =			model_input

       __UpperCamelCase							    =			self.denoise_mixing_layer(x=coupled_latents[0]      ,	y=coupled_latents[1] )

      # either one is fine
      __UpperCamelCase							    =			coupled_latents[0]

      if output_type not in ["latent", "pt", "np", "pil"]:
       __UpperCamelCase							    =			(
           F'the output_type {output_type} is outdated. Please make sure to set it to one of these instead: '
           '`pil`, `np`, `pt`, `latent`'
       )
       deprecate('Unsupported output_type'      ,	'1.0.0'      ,	__UpperCAmelCase      ,	standard_warn=__UpperCAmelCase )
       __UpperCamelCase							    =			'np'

      if output_type == "latent":
       __UpperCamelCase							    =			final_latent
      else:
       __UpperCamelCase							    =			self.decode_latents(__UpperCAmelCase )
       __UpperCamelCase							    =			self.image_processor.postprocess(__UpperCAmelCase      ,	output_type=__UpperCAmelCase )

      return image





"""simple docstring"""

UpperCamelCase      :							int          =					1_0**8






def 				A		(							snake_case :int						) ->      bool:
 if n % 1_0 == 0:
  return False
 __UpperCamelCase							    =			str(snake_case						)
 return s == s[::-1]






def 				A		(							) ->      int:
 __UpperCamelCase							    =			set()
 __UpperCamelCase							    =			1
 __UpperCamelCase							    =			5
 while sum_squares < LIMIT:
  __UpperCamelCase							    =			first_square + 1
  while sum_squares < LIMIT:
   if is_palindrome(snake_case						):
    answer.add(snake_case						)
   last_square += 1
   sum_squares += last_square**2
  first_square += 1
  __UpperCamelCase							    =			first_square**2 + (first_square + 1) ** 2

 return sum(snake_case						)


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



