




"""simple docstring"""






from typing import Optional, Tuple, Union

import flax
import flax.linen as nn
import jax
import jax.numpy as jnp
from flax.core.frozen_dict import FrozenDict

from ..configuration_utils import ConfigMixin, flax_register_to_config
from ..utils import BaseOutput
from .embeddings_flax import FlaxTimestepEmbedding, FlaxTimesteps
from .modeling_flax_utils import FlaxModelMixin
from .unet_ad_blocks_flax import (
    FlaxCrossAttnDownBlockaD,
    FlaxCrossAttnUpBlockaD,
    FlaxDownBlockaD,
    FlaxUNetMidBlockaDCrossAttn,
    FlaxUpBlockaD,
)


@flax.struct.dataclass
class     __lowerCamelCase  (					a__				):

     '''simple docstring'''
     A_       :	Optional[Any]    =		42







@flax_register_to_config
class     __lowerCamelCase  (					nn.Module  ,					a__  ,					a__				):

     '''simple docstring'''
     A_       :	Dict    =		32
     A_       :	List[str]    =		4
     A_       :	List[Any]    =		4
     A_       :	Dict    =		(
         'CrossAttnDownBlock2D',
         'CrossAttnDownBlock2D',
         'CrossAttnDownBlock2D',
         'DownBlock2D',
     )
     A_       :	Tuple    =		('UpBlock2D', 'CrossAttnUpBlock2D', 'CrossAttnUpBlock2D', 'CrossAttnUpBlock2D')
     A_       :	Tuple    =		False
     A_       :	Dict    =		(320, 640, 1_280, 1_280)
     A_       :	Optional[Any]    =		2
     A_       :	List[Any]    =		8
     A_       :	Union[str, Any]    =		None
     A_       :	Optional[int]    =		1_280
     A_       :	str    =		0.0
     A_       :	Tuple    =		False
     A_       :	Any    =		jnp.floataa
     A_       :	str    =		True
     A_       :	List[str]    =		0
     A_       :	Optional[int]    =		False


     def 							_UpperCAmelCase      (      self    ,				__UpperCAmelCase							)       ->							Optional[Any]:
          _a    	=				(1, self.in_channels, self.sample_size, self.sample_size)
          _a    	=				jnp.zeros(__lowerCAmelCase    ,				dtype=jnp.floataa							)
          _a    	=				jnp.ones((1,)    ,				dtype=jnp.intaa							)
          _a    	=				jnp.zeros((1, 1, self.cross_attention_dim)    ,				dtype=jnp.floataa							)

          _a	,      _a    	=				jax.random.split(__lowerCAmelCase							)
          _a    	=				{'''params''': params_rng, '''dropout''': dropout_rng}

          return self.init(__lowerCAmelCase    ,				__lowerCAmelCase    ,				__lowerCAmelCase    ,				__lowerCAmelCase							)["params"]


     def 							_UpperCAmelCase      (      self							)       ->							Union[str, Any]:
          _a    	=				self.block_out_channels
          _a    	=				block_out_channels[0] * 4

          if self.num_attention_heads is not None:
               raise ValueError(
                   '''At the moment it is not possible to define the number of attention heads via `num_attention_heads` because of a naming issue as described in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131. Passing `num_attention_heads` will only be supported in diffusers v0.19.'''							)

          # If `num_attention_heads` is not defined (which is the case for most models)
          # it will default to `attention_head_dim`. This looks weird upon first reading it and it is.
          # The reason for this behavior is to correct for incorrectly named variables that were introduced
          # when this library was created. The incorrect naming was only discovered much later in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131
          # Changing `attention_head_dim` to `num_attention_heads` for 40,000+ configurations is too backwards breaking
          # which is why we correct for the naming here.
          _a    	=				self.num_attention_heads or self.attention_head_dim

          # input
          _a    	=				nn.Conv(
              block_out_channels[0]    ,				kernel_size=(3, 3)    ,				strides=(1, 1)    ,				padding=((1, 1), (1, 1))    ,				dtype=self.dtype    ,				)

          # time
          _a    	=				FlaxTimesteps(
              block_out_channels[0]    ,				flip_sin_to_cos=self.flip_sin_to_cos    ,				freq_shift=self.config.freq_shift							)
          _a    	=				FlaxTimestepEmbedding(__lowerCAmelCase    ,				dtype=self.dtype							)

          _a    	=				self.only_cross_attention
          if isinstance(__lowerCAmelCase    ,				__lowerCAmelCase							):
               _a    	=				(only_cross_attention,) * len(self.down_block_types							)

          if isinstance(__lowerCAmelCase    ,				__lowerCAmelCase							):
               _a    	=				(num_attention_heads,) * len(self.down_block_types							)

          # down
          _a    	=				[]
          _a    	=				block_out_channels[0]
          for i, down_block_type in enumerate(self.down_block_types							):
               _a    	=				output_channel
               _a    	=				block_out_channels[i]
               _a    	=				i == len(__lowerCAmelCase							) - 1

               if down_block_type == "CrossAttnDownBlock2D":
                    _a    	=				FlaxCrossAttnDownBlockaD(
                        in_channels=__lowerCAmelCase    ,				out_channels=__lowerCAmelCase    ,				dropout=self.dropout    ,				num_layers=self.layers_per_block    ,				num_attention_heads=num_attention_heads[i]    ,				add_downsample=not is_final_block    ,				use_linear_projection=self.use_linear_projection    ,				only_cross_attention=only_cross_attention[i]    ,				use_memory_efficient_attention=self.use_memory_efficient_attention    ,				dtype=self.dtype    ,				)
               else:
                    _a    	=				FlaxDownBlockaD(
                        in_channels=__lowerCAmelCase    ,				out_channels=__lowerCAmelCase    ,				dropout=self.dropout    ,				num_layers=self.layers_per_block    ,				add_downsample=not is_final_block    ,				dtype=self.dtype    ,				)

               down_blocks.append(__lowerCAmelCase							)
          _a    	=				down_blocks

          # mid
          _a    	=				FlaxUNetMidBlockaDCrossAttn(
              in_channels=block_out_channels[-1]    ,				dropout=self.dropout    ,				num_attention_heads=num_attention_heads[-1]    ,				use_linear_projection=self.use_linear_projection    ,				use_memory_efficient_attention=self.use_memory_efficient_attention    ,				dtype=self.dtype    ,				)

          # up
          _a    	=				[]
          _a    	=				list(reversed(__lowerCAmelCase							)							)
          _a    	=				list(reversed(__lowerCAmelCase							)							)
          _a    	=				list(reversed(__lowerCAmelCase							)							)
          _a    	=				reversed_block_out_channels[0]
          for i, up_block_type in enumerate(self.up_block_types							):
               _a    	=				output_channel
               _a    	=				reversed_block_out_channels[i]
               _a    	=				reversed_block_out_channels[min(i + 1    ,				len(__lowerCAmelCase							) - 1							)]

               _a    	=				i == len(__lowerCAmelCase							) - 1

               if up_block_type == "CrossAttnUpBlock2D":
                    _a    	=				FlaxCrossAttnUpBlockaD(
                        in_channels=__lowerCAmelCase    ,				out_channels=__lowerCAmelCase    ,				prev_output_channel=__lowerCAmelCase    ,				num_layers=self.layers_per_block + 1    ,				num_attention_heads=reversed_num_attention_heads[i]    ,				add_upsample=not is_final_block    ,				dropout=self.dropout    ,				use_linear_projection=self.use_linear_projection    ,				only_cross_attention=only_cross_attention[i]    ,				use_memory_efficient_attention=self.use_memory_efficient_attention    ,				dtype=self.dtype    ,				)
               else:
                    _a    	=				FlaxUpBlockaD(
                        in_channels=__lowerCAmelCase    ,				out_channels=__lowerCAmelCase    ,				prev_output_channel=__lowerCAmelCase    ,				num_layers=self.layers_per_block + 1    ,				add_upsample=not is_final_block    ,				dropout=self.dropout    ,				dtype=self.dtype    ,				)

               up_blocks.append(__lowerCAmelCase							)
               _a    	=				output_channel
          _a    	=				up_blocks

          # out
          _a    	=				nn.GroupNorm(num_groups=32    ,				epsilon=1e-5							)
          _a    	=				nn.Conv(
              self.out_channels    ,				kernel_size=(3, 3)    ,				strides=(1, 1)    ,				padding=((1, 1), (1, 1))    ,				dtype=self.dtype    ,				)





     def __call__(      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase=None    ,				__UpperCAmelCase=None    ,				__UpperCAmelCase = True    ,				__UpperCAmelCase = False    ,				)       ->							List[str]:







          if not isinstance(__lowerCAmelCase    ,				jnp.ndarray							):
               _a    	=				jnp.array([timesteps]    ,				dtype=jnp.intaa							)
          elif isinstance(__lowerCAmelCase    ,				jnp.ndarray							) and len(timesteps.shape							) == 0:
               _a    	=				timesteps.astype(dtype=jnp.floataa							)
               _a    	=				jnp.expand_dims(__lowerCAmelCase    ,				0							)

          _a    	=				self.time_proj(__lowerCAmelCase							)
          _a    	=				self.time_embedding(__lowerCAmelCase							)

          # 2. pre-process
          _a    	=				jnp.transpose(__lowerCAmelCase    ,				(0, 2, 3, 1)							)
          _a    	=				self.conv_in(__lowerCAmelCase							)

          # 3. down
          _a    	=				(sample,)
          for down_block in self.down_blocks:
               if isinstance(__lowerCAmelCase    ,				__lowerCAmelCase							):
                    _a	,      _a    	=				down_block(__lowerCAmelCase    ,				__lowerCAmelCase    ,				__lowerCAmelCase    ,				deterministic=not train							)
               else:
                    _a	,      _a    	=				down_block(__lowerCAmelCase    ,				__lowerCAmelCase    ,				deterministic=not train							)
               down_block_res_samples += res_samples

          if down_block_additional_residuals is not None:
               _a    	=				()

               for down_block_res_sample, down_block_additional_residual in zip(
                   __lowerCAmelCase    ,				__lowerCAmelCase							):
                    down_block_res_sample += down_block_additional_residual
                    new_down_block_res_samples += (down_block_res_sample,)

               _a    	=				new_down_block_res_samples

          # 4. mid
          _a    	=				self.mid_block(__lowerCAmelCase    ,				__lowerCAmelCase    ,				__lowerCAmelCase    ,				deterministic=not train							)

          if mid_block_additional_residual is not None:
               sample += mid_block_additional_residual

          # 5. up
          for up_block in self.up_blocks:
               _a    	=				down_block_res_samples[-(self.layers_per_block + 1) :]
               _a    	=				down_block_res_samples[: -(self.layers_per_block + 1)]
               if isinstance(__lowerCAmelCase    ,				__lowerCAmelCase							):
                    _a    	=				up_block(
                        __lowerCAmelCase    ,				temb=__lowerCAmelCase    ,				encoder_hidden_states=__lowerCAmelCase    ,				res_hidden_states_tuple=__lowerCAmelCase    ,				deterministic=not train    ,				)
               else:
                    _a    	=				up_block(__lowerCAmelCase    ,				temb=__lowerCAmelCase    ,				res_hidden_states_tuple=__lowerCAmelCase    ,				deterministic=not train							)

        # 6. post-process
          _a    	=				self.conv_norm_out(__lowerCAmelCase							)
          _a    	=				nn.silu(__lowerCAmelCase							)
          _a    	=				self.conv_out(__lowerCAmelCase							)
          _a    	=				jnp.transpose(__lowerCAmelCase    ,				(0, 3, 1, 2)							)

          if not return_dict:
               return (sample,)

          return FlaxUNetaDConditionOutput(sample=__lowerCAmelCase							)



"""simple docstring"""






import math
import random
from typing import Any

from .hill_climbing import SearchProblem
def       A_   (   _lowerCAmelCase     :					Dict,     _lowerCAmelCase     :					bool = True,     _lowerCAmelCase     :					float = math.inf,     _lowerCAmelCase     :					float = -math.inf,     _lowerCAmelCase     :					float = math.inf,     _lowerCAmelCase     :					float = -math.inf,     _lowerCAmelCase     :					bool = False,     _lowerCAmelCase     :					float = 1_00,     _lowerCAmelCase     :					float = 0.0_1,     _lowerCAmelCase     :					float = 1,     ):






     """simple docstring"""

     _a    	=				False
     _a    	=				search_prob
     _a    	=				start_temperate
     _a    	=				[]
     _a    	=				0
     _a    	=				None

     while not search_end:
          _a    	=				current_state.score()
          if best_state is None or current_score > best_state.score():
               _a    	=				current_state
          scores.append(_lowerCAmelCase       )
          iterations += 1
          _a    	=				None
          _a    	=				current_state.get_neighbors()
          while (
              next_state is None and neighbors
          ):  # till we do not find a neighbor that we can move to
               _a    	=				random.randint(0,     len(_lowerCAmelCase       ) - 1       )  # picking a random neighbor
               _a    	=				neighbors.pop(_lowerCAmelCase       )
               _a    	=				picked_neighbor.score() - current_score

               if (
                   picked_neighbor.x > max_x
                   or picked_neighbor.x < min_x
                   or picked_neighbor.y > max_y
                   or picked_neighbor.y < min_y
               ):
                    continue  # neighbor outside our bounds

               if not find_max:
                    _a    	=				change * -1  # in case we are finding minimum
               if change > 0:  # improves the solution
                    _a    	=				picked_neighbor
               else:
                    _a    	=				(math.e) ** (
                        change / current_temp
                    )  # probability generation function
                    if random.random() < probability:  # random number within probability
                         _a    	=				picked_neighbor
          _a    	=				current_temp - (current_temp * rate_of_decrease)

          if current_temp < threshold_temp or next_state is None:
               # temperature below threshold, or could not find a suitable neighbor
               _a    	=				True
          else:
               _a    	=				next_state

     if visualization:
          from matplotlib import pyplot as plt

          plt.plot(range(_lowerCAmelCase       ),     _lowerCAmelCase       )
          plt.xlabel('''Iterations'''       )
          plt.ylabel('''Function values'''       )
          plt.show()
     return best_state


if __name__ == "__main__":
   def       A_   (   _lowerCAmelCase     :					List[str],     _lowerCAmelCase     :					Dict       ):






        """simple docstring"""

        return (x**2) + (y**2)

   # starting the problem with initial coordinates (12, 47)
   __snake_case		=       SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa)
   __snake_case		=       simulated_annealing(
       prob, find_max=False, max_x=100, min_x=5, max_y=50, min_y=-5, visualization=True
   )
   print(
       '''The minimum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 '''
       f'and 50 > y > - 5 found via hill climbing: {local_min.score()}'
   )

   # starting the problem with initial coordinates (12, 47)
   __snake_case		=       SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa)
   __snake_case		=       simulated_annealing(
       prob, find_max=True, max_x=100, min_x=5, max_y=50, min_y=-5, visualization=True
   )
   print(
       '''The maximum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 '''
       f'and 50 > y > - 5 found via hill climbing: {local_min.score()}'
   )
   def       A_   (   _lowerCAmelCase     :					Optional[Any],     _lowerCAmelCase     :					Optional[int]       ):






        """simple docstring"""

        return (3 * x**2) - (6 * y)

   __snake_case		=       SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa)
   __snake_case		=       simulated_annealing(prob, find_max=False, visualization=True)
   print(
       '''The minimum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: '''
       f'{local_min.score()}'
   )

   __snake_case		=       SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa)
   __snake_case		=       simulated_annealing(prob, find_max=True, visualization=True)

   print(
       '''The maximum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: '''
       f'{local_min.score()}'
   )