




'''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 (			__lowerCAmelCase):
       __lowercase	:   jnp.ndarray





@flax_register_to_config
class      lowerCAmelCase (			nn.Module					, __lowerCAmelCase					, __lowerCAmelCase):
       __lowercase	:   int										=      32
       __lowercase	:   int										=      4
       __lowercase	:   int										=      4
       __lowercase	:   Tuple[str]										=      (
           "CrossAttnDownBlock2D",
           "CrossAttnDownBlock2D",
           "CrossAttnDownBlock2D",
           "DownBlock2D",
       )
       __lowercase	:   Tuple[str]										=      ("UpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D")
       __lowercase	:   Union[bool, Tuple[bool]]										=      False
       __lowercase	:   Tuple[int]										=      (320, 640, 1280, 1280)
       __lowercase	:   int										=      2
       __lowercase	:   Union[int, Tuple[int]]										=      8
       __lowercase	:   Optional[Union[int, Tuple[int]]]										=      None
       __lowercase	:   int										=      1280
       __lowercase	:   float										=      0.0
       __lowercase	:   bool										=      False
       __lowercase	:   jnp.dtype										=      jnp.floataa
       __lowercase	:   bool										=      True
       __lowercase	:   int										=      0
       __lowercase	:   bool										=      False


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE	)   ->	FrozenDict:



            '''simple docstring'''




            __snake_case            =	(1, self.in_channels, self.sample_size, self.sample_size)
            __snake_case            =	jnp.zeros(__SCREAMING_SNAKE_CASE     ,	dtype=jnp.floataa	)
            __snake_case            =	jnp.ones((1,)     ,	dtype=jnp.intaa	)
            __snake_case            =	jnp.zeros((1, 1, self.cross_attention_dim)     ,	dtype=jnp.floataa	)

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

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


       def    lowerCAmelCase   ( self	)   ->	Optional[Any]:



            '''simple docstring'''




            __snake_case            =	self.block_out_channels
            __snake_case            =	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.
            __snake_case            =	self.num_attention_heads or self.attention_head_dim

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

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

            __snake_case            =	self.only_cross_attention
            if isinstance(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	):
                 __snake_case            =	(only_cross_attention,) * len(self.down_block_types	)

            if isinstance(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	):
                 __snake_case            =	(num_attention_heads,) * len(self.down_block_types	)

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

                 if down_block_type == "CrossAttnDownBlock2D":
                      __snake_case            =	FlaxCrossAttnDownBlockaD(
                          in_channels=__SCREAMING_SNAKE_CASE     ,	out_channels=__SCREAMING_SNAKE_CASE     ,	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:
                      __snake_case            =	FlaxDownBlockaD(
                          in_channels=__SCREAMING_SNAKE_CASE     ,	out_channels=__SCREAMING_SNAKE_CASE     ,	dropout=self.dropout     ,	num_layers=self.layers_per_block     ,	add_downsample=not is_final_block     ,	dtype=self.dtype     ,	)

                 down_blocks.append(__SCREAMING_SNAKE_CASE	)
            __snake_case            =	down_blocks

            # mid
            __snake_case            =	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
            __snake_case            =	[]
            __snake_case            =	list(reversed(__SCREAMING_SNAKE_CASE	)	)
            __snake_case            =	list(reversed(__SCREAMING_SNAKE_CASE	)	)
            __snake_case            =	list(reversed(__SCREAMING_SNAKE_CASE	)	)
            __snake_case            =	reversed_block_out_channels[0]
            for i, up_block_type in enumerate(self.up_block_types	):
                 __snake_case            =	output_channel
                 __snake_case            =	reversed_block_out_channels[i]
                 __snake_case            =	reversed_block_out_channels[min(i + 1     ,	len(__SCREAMING_SNAKE_CASE	) - 1	)]

                 __snake_case            =	i == len(__SCREAMING_SNAKE_CASE	) - 1

                 if up_block_type == "CrossAttnUpBlock2D":
                      __snake_case            =	FlaxCrossAttnUpBlockaD(
                          in_channels=__SCREAMING_SNAKE_CASE     ,	out_channels=__SCREAMING_SNAKE_CASE     ,	prev_output_channel=__SCREAMING_SNAKE_CASE     ,	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:
                      __snake_case            =	FlaxUpBlockaD(
                          in_channels=__SCREAMING_SNAKE_CASE     ,	out_channels=__SCREAMING_SNAKE_CASE     ,	prev_output_channel=__SCREAMING_SNAKE_CASE     ,	num_layers=self.layers_per_block + 1     ,	add_upsample=not is_final_block     ,	dropout=self.dropout     ,	dtype=self.dtype     ,	)

                 up_blocks.append(__SCREAMING_SNAKE_CASE	)
                 __snake_case            =	output_channel
            __snake_case            =	up_blocks

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




       def __call__( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE=None     ,	__SCREAMING_SNAKE_CASE=None     ,	__SCREAMING_SNAKE_CASE = True     ,	__SCREAMING_SNAKE_CASE = False     ,	)   ->	Union[FlaxUNetaDConditionOutput, Tuple]:



            '''simple docstring'''




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

            __snake_case            =	self.time_proj(__SCREAMING_SNAKE_CASE	)
            __snake_case            =	self.time_embedding(__SCREAMING_SNAKE_CASE	)

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

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

            if down_block_additional_residuals is not None:
                 __snake_case            =	()

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

                 __snake_case            =	new_down_block_res_samples

            # 4. mid
            __snake_case            =	self.mid_block(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	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:
                 __snake_case            =	down_block_res_samples[-(self.layers_per_block + 1) :]
                 __snake_case            =	down_block_res_samples[: -(self.layers_per_block + 1)]
                 if isinstance(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	):
                      __snake_case            =	up_block(
                          __SCREAMING_SNAKE_CASE     ,	temb=__SCREAMING_SNAKE_CASE     ,	encoder_hidden_states=__SCREAMING_SNAKE_CASE     ,	res_hidden_states_tuple=__SCREAMING_SNAKE_CASE     ,	deterministic=not train     ,	)
                 else:
                      __snake_case            =	up_block(__SCREAMING_SNAKE_CASE     ,	temb=__SCREAMING_SNAKE_CASE     ,	res_hidden_states_tuple=__SCREAMING_SNAKE_CASE     ,	deterministic=not train	)

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

            if not return_dict:
                 return (sample,)

            return FlaxUNetaDConditionOutput(sample=__SCREAMING_SNAKE_CASE	)













'''simple docstring'''




import random



def 		__A		(  lowerCamelCase_	):


		"""simple docstring"""



		SCREAMING_SNAKE_CASE :				str		=			num - 1
		SCREAMING_SNAKE_CASE :				Union[str, Any]		=			0

		while s % 2 == 0:
				SCREAMING_SNAKE_CASE :				List[str]		=			s // 2
				t += 1

		for _ in range(5	):
				SCREAMING_SNAKE_CASE :				List[Any]		=			random.randrange(2			,			num - 1	)
				SCREAMING_SNAKE_CASE :				List[Any]		=			pow(lowerCamelCase_			,			lowerCamelCase_			,			lowerCamelCase_	)
				if v != 1:
						SCREAMING_SNAKE_CASE :				Tuple		=			0
						while v != (num - 1):
								if i == t - 1:
										return False
								else:
										SCREAMING_SNAKE_CASE :				Any		=			i + 1
										SCREAMING_SNAKE_CASE :				str		=			(v**2) % num
		return True



def 		__A		(  lowerCamelCase_	):


		"""simple docstring"""



		if num < 2:
				return False

		SCREAMING_SNAKE_CASE :				List[str]		=			[
		    2,
		    3,
		    5,
		    7,
		    11,
		    13,
		    17,
		    19,
		    23,
		    29,
		    31,
		    37,
		    41,
		    43,
		    47,
		    53,
		    59,
		    61,
		    67,
		    71,
		    73,
		    79,
		    83,
		    89,
		    97,
		    1_01,
		    1_03,
		    1_07,
		    1_09,
		    1_13,
		    1_27,
		    1_31,
		    1_37,
		    1_39,
		    1_49,
		    1_51,
		    1_57,
		    1_63,
		    1_67,
		    1_73,
		    1_79,
		    1_81,
		    1_91,
		    1_93,
		    1_97,
		    1_99,
		    2_11,
		    2_23,
		    2_27,
		    2_29,
		    2_33,
		    2_39,
		    2_41,
		    2_51,
		    2_57,
		    2_63,
		    2_69,
		    2_71,
		    2_77,
		    2_81,
		    2_83,
		    2_93,
		    3_07,
		    3_11,
		    3_13,
		    3_17,
		    3_31,
		    3_37,
		    3_47,
		    3_49,
		    3_53,
		    3_59,
		    3_67,
		    3_73,
		    3_79,
		    3_83,
		    3_89,
		    3_97,
		    4_01,
		    4_09,
		    4_19,
		    4_21,
		    4_31,
		    4_33,
		    4_39,
		    4_43,
		    4_49,
		    4_57,
		    4_61,
		    4_63,
		    4_67,
		    4_79,
		    4_87,
		    4_91,
		    4_99,
		    5_03,
		    5_09,
		    5_21,
		    5_23,
		    5_41,
		    5_47,
		    5_57,
		    5_63,
		    5_69,
		    5_71,
		    5_77,
		    5_87,
		    5_93,
		    5_99,
		    6_01,
		    6_07,
		    6_13,
		    6_17,
		    6_19,
		    6_31,
		    6_41,
		    6_43,
		    6_47,
		    6_53,
		    6_59,
		    6_61,
		    6_73,
		    6_77,
		    6_83,
		    6_91,
		    7_01,
		    7_09,
		    7_19,
		    7_27,
		    7_33,
		    7_39,
		    7_43,
		    7_51,
		    7_57,
		    7_61,
		    7_69,
		    7_73,
		    7_87,
		    7_97,
		    8_09,
		    8_11,
		    8_21,
		    8_23,
		    8_27,
		    8_29,
		    8_39,
		    8_53,
		    8_57,
		    8_59,
		    8_63,
		    8_77,
		    8_81,
		    8_83,
		    8_87,
		    9_07,
		    9_11,
		    9_19,
		    9_29,
		    9_37,
		    9_41,
		    9_47,
		    9_53,
		    9_67,
		    9_71,
		    9_77,
		    9_83,
		    9_91,
		    9_97,
		]

		if num in low_primes:
				return True

		for prime in low_primes:
				if (num % prime) == 0:
						return False

		return rabin_miller(lowerCamelCase_	)



def 		__A		(  lowerCamelCase_ = 10_24	):


		"""simple docstring"""



		while True:
				SCREAMING_SNAKE_CASE :				Optional[Any]		=			random.randrange(2 ** (keysize - 1)			,			2 ** (keysize)	)
				if is_prime_low_num(lowerCamelCase_	):
						return num


if __name__ == "__main__":
							__UpperCAmelCase            =       generate_large_prime()
							print(("""Prime number:""", num))
							print(("""is_prime_low_num:""", is_prime_low_num(num)))



