
import tempfile
import unittest

import torch

from diffusers import UNetaDConditionModel
from diffusers.training_utils import EMAModel
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps, torch_device


enable_full_determinism()


class   __lowerCAmelCase		(		unittest.TestCase ):



      """simple docstring"""







      A__		:     List[Any]       				=      '''hf-internal-testing/tiny-stable-diffusion-pipe'''
      A__		:     List[Any]       				=      1
      A__		:     int       				=      7_7
      A__		:     Optional[Any]       				=      3_2
      A__		:     Any       				=      4
      A__		:     Optional[int]       				=      6_4
      A__		:     List[Any]       				=      torch.manual_seed(0 )




      def 						snake_case_     ( self						:    int			,    _snake_case						:    Tuple=0.99_99			):
          __lowercase						:     Dict		      =    UNetaDConditionModel.from_pretrained(self.model_id			,    subfolder='''unet'''			)
          __lowercase						:     Optional[Any]		      =    unet.to(_snake_case			)
          __lowercase						:     Union[str, Any]		      =    EMAModel(unet.parameters()			,    decay=_snake_case			,    model_cls=_snake_case			,    model_config=unet.config			)
          return unet, ema_unet




      def 						snake_case_     ( self						:    List[str]			):
          __lowercase						:     Tuple		      =    torch.randn(
              self.batch_size			,    self.num_in_channels			,    self.latent_height			,    self.latent_width			,    generator=self.generator			).to(_snake_case			)
          __lowercase						:     int		      =    torch.randint(0			,    1000			,    size=(self.batch_size,)			,    generator=self.generator			).to(_snake_case			)
          __lowercase						:     Optional[int]		      =    torch.randn(
              self.batch_size			,    self.prompt_length			,    self.text_encoder_hidden_dim			,    generator=self.generator			).to(_snake_case			)
          return noisy_latents, timesteps, encoder_hidden_states




      def 						snake_case_     ( self						:    Dict			,    _snake_case						:    Dict			):
          __lowercase						:     Union[str, Any]		      =    {}
          for k, param in unet.state_dict().items():
              __lowercase						:     Dict		      =    torch.randn_like(_snake_case			) + (param * torch.randn_like(_snake_case			))
              updated_state_dict.update({k: updated_param}			)
          unet.load_state_dict(_snake_case			)
          return unet




      def 						snake_case_     ( self						:    Union[str, Any]			):
          __lowercase      , __lowercase						:     Any		      =    self.get_models()
          # Take the first (hypothetical) EMA step.
          ema_unet.step(unet.parameters()			)
          assert ema_unet.optimization_step == 1

          # Take two more.
          for _ in range(2			):
              ema_unet.step(unet.parameters()			)
          assert ema_unet.optimization_step == 3




      def 						snake_case_     ( self						:    Union[str, Any]			):
          __lowercase      , __lowercase						:     Tuple		      =    self.get_models()
          # Since the `unet` is not being updated (i.e., backprop'd)
          # there won't be any difference between the `params` of `unet`
          # and `ema_unet` even if we call `ema_unet.step(unet.parameters())`.
          ema_unet.step(unet.parameters()			)
          __lowercase						:     List[Any]		      =    list(unet.parameters()			)
          for s_param, param in zip(ema_unet.shadow_params			,    _snake_case			):
              assert torch.allclose(_snake_case			,    _snake_case			)

          # The above holds true even if we call `ema.step()` multiple times since
          # `unet` params are still not being updated.
          for _ in range(4			):
              ema_unet.step(unet.parameters()			)
          for s_param, param in zip(ema_unet.shadow_params			,    _snake_case			):
              assert torch.allclose(_snake_case			,    _snake_case			)




      def 						snake_case_     ( self						:    Optional[int]			):
          __lowercase      , __lowercase						:     Optional[Any]		      =    self.get_models()
          # Here we simulate the parameter updates for `unet`. Since there might
          # be some parameters which are initialized to zero we take extra care to
          # initialize their values to something non-zero before the multiplication.
          __lowercase						:     Union[str, Any]		      =    self.simulate_backprop(_snake_case			)

          # Take the EMA step.
          ema_unet.step(unet_pseudo_updated_step_one.parameters()			)

          # Now the EMA'd parameters won't be equal to the original model parameters.
          __lowercase						:     Optional[Any]		      =    list(unet_pseudo_updated_step_one.parameters()			)
          for s_param, param in zip(ema_unet.shadow_params			,    _snake_case			):
              assert ~torch.allclose(_snake_case			,    _snake_case			)

          # Ensure this is the case when we take multiple EMA steps.
          for _ in range(4			):
              ema_unet.step(unet.parameters()			)
          for s_param, param in zip(ema_unet.shadow_params			,    _snake_case			):
              assert ~torch.allclose(_snake_case			,    _snake_case			)




      def 						snake_case_     ( self						:    List[Any]			):
          # If we call EMA step after a backpropagation consecutively for two times,
          # the shadow params from those two steps should be different.
          __lowercase      , __lowercase						:     Any		      =    self.get_models()

          # First backprop + EMA
          __lowercase						:     Union[str, Any]		      =    self.simulate_backprop(_snake_case			)
          ema_unet.step(unet_step_one.parameters()			)
          __lowercase						:     List[Any]		      =    ema_unet.shadow_params

          # Second backprop + EMA
          __lowercase						:     Optional[int]		      =    self.simulate_backprop(_snake_case			)
          ema_unet.step(unet_step_two.parameters()			)
          __lowercase						:     Optional[Any]		      =    ema_unet.shadow_params

          for step_one, step_two in zip(_snake_case			,    _snake_case			):
              assert ~torch.allclose(_snake_case			,    _snake_case			)




      def 						snake_case_     ( self						:    Union[str, Any]			):
          # If there's no decay even if there are backprops, EMA steps
          # won't take any effect i.e., the shadow params would remain the
          # same.
          __lowercase      , __lowercase						:     Optional[Any]		      =    self.get_models(decay=0.0			)
          __lowercase						:     Dict		      =    self.simulate_backprop(_snake_case			)
          ema_unet.step(unet_step_one.parameters()			)
          __lowercase						:     Optional[Any]		      =    ema_unet.shadow_params

          __lowercase						:     Dict		      =    self.simulate_backprop(_snake_case			)
          ema_unet.step(unet_step_two.parameters()			)
          __lowercase						:     Dict		      =    ema_unet.shadow_params

          for step_one, step_two in zip(_snake_case			,    _snake_case			):
              assert torch.allclose(_snake_case			,    _snake_case			)




      @skip_mps
      def 						snake_case_     ( self						:    Optional[Any]			):
          __lowercase      , __lowercase						:     List[Any]		      =    self.get_models()
          __lowercase      , __lowercase      , __lowercase						:     Tuple		      =    self.get_dummy_inputs()

          with tempfile.TemporaryDirectory() as tmpdir:
              ema_unet.save_pretrained(_snake_case			)
              __lowercase						:     int		      =    UNetaDConditionModel.from_pretrained(_snake_case			,    model_cls=_snake_case			)
              __lowercase						:     List[Any]		      =    loaded_unet.to(unet.device			)

          # Since no EMA step has been performed the outputs should match.
          __lowercase						:     List[str]		      =    unet(_snake_case			,    _snake_case			,    _snake_case			).sample
          __lowercase						:     List[str]		      =    loaded_unet(_snake_case			,    _snake_case			,    _snake_case			).sample

          assert torch.allclose(_snake_case			,    _snake_case			,    atol=1E-4			)




from __future__ import annotations

import math
from itertools import permutations


def       UpperCAmelCase_					(  __lowerCAmelCase		)	->  bool:

    if 1 < number < 4:
        # 2 and 3 are primes
        return True
    elif number < 2 or number % 2 == 0 or number % 3 == 0:
        # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
        return False

    # All primes number are in format of 6k +/- 1
    for i in range(5		,   int(math.sqrt(__lowerCAmelCase		) + 1		)		,   6		):
        if number % i == 0 or number % (i + 2) == 0:
            return False
    return True


def       UpperCAmelCase_					(  __lowerCAmelCase = 7		)	->  int:
    __lowercase						:     Union[str, Any]		      =    ''''''.join(str(__lowerCAmelCase		) for i in range(1		,   n + 1		)		)
    __lowercase						:     Dict		      =    [int(''''''.join(__lowerCAmelCase		)		) for i in permutations(__lowerCAmelCase		,   __lowerCAmelCase		)]
    __lowercase						:     int		      =    [num for num in perm_list if is_prime(__lowerCAmelCase		)]
    return max(__lowerCAmelCase		) if pandigitals else 0


if __name__ == "__main__":
 print(F'{solution() = }')


