





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 					A_					(   UpperCAmelCase						):







       """simple docstring"""





       SCREAMING_SNAKE_CASE_   :			jnp.ndarray





@flax_register_to_config
class 					A_					(   nn.Module       ,						UpperCAmelCase       ,						UpperCAmelCase						):







       """simple docstring"""





       SCREAMING_SNAKE_CASE_   :			int  			=	3_2
       SCREAMING_SNAKE_CASE_   :			int  			=	4
       SCREAMING_SNAKE_CASE_   :			int  			=	4
       SCREAMING_SNAKE_CASE_   :			Tuple[str]  			=	(
           "CrossAttnDownBlock2D",
           "CrossAttnDownBlock2D",
           "CrossAttnDownBlock2D",
           "DownBlock2D",
       )
       SCREAMING_SNAKE_CASE_   :			Tuple[str]  			=	("UpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D")
       SCREAMING_SNAKE_CASE_   :			Union[bool, Tuple[bool]]  			=	False
       SCREAMING_SNAKE_CASE_   :			Tuple[int]  			=	(3_2_0, 6_4_0, 1_2_8_0, 1_2_8_0)
       SCREAMING_SNAKE_CASE_   :			int  			=	2
       SCREAMING_SNAKE_CASE_   :			Union[int, Tuple[int]]  			=	8
       SCREAMING_SNAKE_CASE_   :			Optional[Union[int, Tuple[int]]]  			=	None
       SCREAMING_SNAKE_CASE_   :			int  			=	1_2_8_0
       SCREAMING_SNAKE_CASE_   :			float  			=	0.0
       SCREAMING_SNAKE_CASE_   :			bool  			=	False
       SCREAMING_SNAKE_CASE_   :			jnp.dtype  			=	jnp.floataa
       SCREAMING_SNAKE_CASE_   :			bool  			=	True
       SCREAMING_SNAKE_CASE_   :			int  			=	0
       SCREAMING_SNAKE_CASE_   :			bool  			=	False







       def  __UpperCAmelCase			(     self						:						Any    ,__A						:						jax.random.KeyArray	)    ->       FrozenDict:
         # init input tensors
         _lowercase						    =			(1, self.in_channels, self.sample_size, self.sample_size)
         _lowercase						    =			jnp.zeros(__A    ,dtype=jnp.floataa	)
         _lowercase						    =			jnp.ones((1,)    ,dtype=jnp.intaa	)
         _lowercase						    =			jnp.zeros((1, 1, self.cross_attention_dim)    ,dtype=jnp.floataa	)

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

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







       def  __UpperCAmelCase			(     self						:						List[str]	)    ->       Dict:
         _lowercase						    =			self.block_out_channels
         _lowercase						    =			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.
         _lowercase						    =			self.num_attention_heads or self.attention_head_dim

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

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

         _lowercase						    =			self.only_cross_attention
         if isinstance(__A    ,__A	):
           _lowercase						    =			(only_cross_attention,) * len(self.down_block_types	)

         if isinstance(__A    ,__A	):
           _lowercase						    =			(num_attention_heads,) * len(self.down_block_types	)

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

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

           down_blocks.append(__A	)
         _lowercase						    =			down_blocks

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

           _lowercase						    =			i == len(__A	) - 1

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

           up_blocks.append(__A	)
           _lowercase						    =			output_channel
         _lowercase						    =			up_blocks

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







       def __call__(     self						:						Union[str, Any]    ,__A						:						List[Any]    ,__A						:						Optional[Any]    ,__A						:						Tuple    ,__A						:						int=None    ,__A						:						Any=None    ,__A						:						bool = True    ,__A						:						bool = False    ,)    ->       Union[FlaxUNetaDConditionOutput, Tuple]:
         # 1. time
         if not isinstance(__A    ,jnp.ndarray	):
           _lowercase						    =			jnp.array([timesteps]    ,dtype=jnp.intaa	)
         elif isinstance(__A    ,jnp.ndarray	) and len(timesteps.shape	) == 0:
           _lowercase						    =			timesteps.astype(dtype=jnp.floataa	)
           _lowercase						    =			jnp.expand_dims(__A    ,0	)

         _lowercase						    =			self.time_proj(__A	)
         _lowercase						    =			self.time_embedding(__A	)

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

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

         if down_block_additional_residuals is not None:
           _lowercase						    =			()

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

           _lowercase						    =			new_down_block_res_samples

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

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

         if not return_dict:
           return (sample,)

         return FlaxUNetaDConditionOutput(sample=__A	)



'''simple docstring'''

from typing import Optional, Tuple

import jax
import jax.numpy as jnp
from flax import linen as nn
from flax.core.frozen_dict import FrozenDict
from transformers import CLIPConfig, FlaxPreTrainedModel
from transformers.models.clip.modeling_flax_clip import FlaxCLIPVisionModule

def _SCREAMING_SNAKE_CASE (A    ,					A    ,					A=1E-12			)			->			str:






       """simple docstring"""







       lowercase__      =					jnp.divide(emb_a.T    ,					jnp.clip(jnp.linalg.norm(A    ,					axis=1			)    ,					a_min=A			)			).T
       lowercase__      =					jnp.divide(emb_a.T    ,					jnp.clip(jnp.linalg.norm(A    ,					axis=1			)    ,					a_min=A			)			).T
       return jnp.matmul(A    ,					norm_emb_a.T			)
class     __lowerCAmelCase		(nn.Module	):





      '''simple docstring'''






      lowerCAmelCase__   :	CLIPConfig
      lowerCAmelCase__   :	jnp.dtype       =				jnp.floataa



      def  UpperCamelCase__						(self       :       Union[str, Any]					):

             '''simple docstring'''







             lowercase__      =					FlaxCLIPVisionModule(self.config.vision_config					)
             lowercase__      =					nn.Dense(self.config.projection_dim  ,		use_bias=UpperCamelCase  ,		dtype=self.dtype					)

             lowercase__      =					self.param('''concept_embeds'''  ,		jax.nn.initializers.ones  ,		(17, self.config.projection_dim)					)
             lowercase__      =					self.param(
                 '''special_care_embeds'''  ,		jax.nn.initializers.ones  ,		(3, self.config.projection_dim)					)

             lowercase__      =					self.param('''concept_embeds_weights'''  ,		jax.nn.initializers.ones  ,		(17,)					)
             lowercase__      =					self.param('''special_care_embeds_weights'''  ,		jax.nn.initializers.ones  ,		(3,)					)






      def __call__(self       :       Union[str, Any]  ,		UpperCamelCase       :       Tuple					):

             '''simple docstring'''







             lowercase__      =					self.vision_model(UpperCamelCase					)[1]
             lowercase__      =					self.visual_projection(UpperCamelCase					)

             lowercase__      =					jax_cosine_distance(UpperCamelCase  ,		self.special_care_embeds					)
             lowercase__      =					jax_cosine_distance(UpperCamelCase  ,		self.concept_embeds					)

             # increase this value to create a stronger `nfsw` filter
             # at the cost of increasing the possibility of filtering benign image inputs
             lowercase__      =					0.0

             lowercase__      =					special_cos_dist - self.special_care_embeds_weights[None, :] + adjustment
             lowercase__      =					jnp.round(UpperCamelCase  ,		3					)
             lowercase__      =					jnp.any(special_scores > 0  ,		axis=1  ,		keepdims=UpperCamelCase					)
             # Use a lower threshold if an image has any special care concept
             lowercase__      =					is_special_care * 0.01

             lowercase__      =					cos_dist - self.concept_embeds_weights[None, :] + special_adjustment
             lowercase__      =					jnp.round(UpperCamelCase  ,		3					)
             lowercase__      =					jnp.any(concept_scores > 0  ,		axis=1					)

             return has_nsfw_concepts






class     __lowerCAmelCase		(lowercase_	):





      '''simple docstring'''






      lowerCAmelCase__   :	Union[str, Any]       =				CLIPConfig
      lowerCAmelCase__   :	Optional[int]       =				"""clip_input"""
      lowerCAmelCase__   :	Any       =				FlaxStableDiffusionSafetyCheckerModule



      def __init__(self       :       Optional[int]  ,		UpperCamelCase       :       CLIPConfig  ,		UpperCamelCase       :       Optional[Tuple] = None  ,		UpperCamelCase       :       int = 0  ,		UpperCamelCase       :       jnp.dtype = jnp.floataa  ,		UpperCamelCase       :       bool = True  ,		**UpperCamelCase       :       List[Any]  ,		):

             '''simple docstring'''







             if input_shape is None:
                    lowercase__      =					(1, 224, 224, 3)
             lowercase__      =					self.module_class(config=UpperCamelCase  ,		dtype=UpperCamelCase  ,		**UpperCamelCase					)
             super().__init__(UpperCamelCase  ,		UpperCamelCase  ,		input_shape=UpperCamelCase  ,		seed=UpperCamelCase  ,		dtype=UpperCamelCase  ,		_do_init=_do_init					)



      def  UpperCamelCase__						(self       :       Any  ,		UpperCamelCase       :       jax.random.KeyArray  ,		UpperCamelCase       :       Tuple  ,		UpperCamelCase       :       FrozenDict = None					):

             '''simple docstring'''







             lowercase__      =					jax.random.normal(UpperCamelCase  ,		UpperCamelCase					)

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

             lowercase__      =					self.module.init(UpperCamelCase  ,		UpperCamelCase					)['''params''']

             return random_params






      def __call__(self       :       Optional[Any]  ,		UpperCamelCase       :       Tuple  ,		UpperCamelCase       :       dict = None  ,		):

             '''simple docstring'''







             lowercase__      =					jnp.transpose(UpperCamelCase  ,		(0, 2, 3, 1)					)

             return self.module.apply(
                 {'''params''': params or self.params}  ,		jnp.array(UpperCamelCase  ,		dtype=jnp.floataa					)  ,		rngs={}  ,		)




