



'''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,
    FlaxDownBlockaD,
    FlaxUNetMidBlockaDCrossAttn,
)





@flax.struct.dataclass
class 				lowerCAmelCase_						(			lowerCamelCase__		):
    __UpperCAmelCase				=			42
    __UpperCAmelCase				=			42





class 				lowerCAmelCase_						(			nn.Module		):
    __UpperCAmelCase				=			42
    __UpperCAmelCase				=			(16, 32, 96, 256)
    __UpperCAmelCase				=			jnp.floataa





    def 	__snake_case  (							self							:							int      ):



       '''simple docstring'''







       snake_case       :				int		    =nn.Conv(
           self.block_out_channels[0],							kernel_size=(3, 3),							padding=((1, 1), (1, 1)),							dtype=self.dtype,							)

       snake_case       :				List[str]		    =[]
       for i in range(len(self.block_out_channels      ) - 1      ):
          snake_case       :				List[str]		    =self.block_out_channels[i]
          snake_case       :				Union[str, Any]		    =self.block_out_channels[i + 1]
          snake_case       :				Any		    =nn.Conv(
              SCREAMING_SNAKE_CASE__,							kernel_size=(3, 3),							padding=((1, 1), (1, 1)),							dtype=self.dtype,							)
          blocks.append(SCREAMING_SNAKE_CASE__      )
          snake_case       :				int		    =nn.Conv(
              SCREAMING_SNAKE_CASE__,							kernel_size=(3, 3),							strides=(2, 2),							padding=((1, 1), (1, 1)),							dtype=self.dtype,							)
          blocks.append(SCREAMING_SNAKE_CASE__      )
       snake_case       :				List[Any]		    =blocks

       snake_case       :				List[str]		    =nn.Conv(
           self.conditioning_embedding_channels,							kernel_size=(3, 3),							padding=((1, 1), (1, 1)),							kernel_init=nn.initializers.zeros_init(),							bias_init=nn.initializers.zeros_init(),							dtype=self.dtype,							)






    def __call__(							self							:							Optional[int],							_snake_case							:							Tuple      ):



       '''simple docstring'''







       snake_case       :				Tuple		    =self.conv_in(SCREAMING_SNAKE_CASE__      )
       snake_case       :				Optional[int]		    =nn.silu(SCREAMING_SNAKE_CASE__      )

       for block in self.blocks:
          snake_case       :				List[str]		    =block(SCREAMING_SNAKE_CASE__      )
          snake_case       :				Dict		    =nn.silu(SCREAMING_SNAKE_CASE__      )

       snake_case       :				List[str]		    =self.conv_out(SCREAMING_SNAKE_CASE__      )

       return embedding





@flax_register_to_config
class 				lowerCAmelCase_						(			nn.Module    ,     lowerCamelCase__    ,     lowerCamelCase__		):
    __UpperCAmelCase				=			32
    __UpperCAmelCase				=			4
    __UpperCAmelCase				=			(
        'CrossAttnDownBlock2D',
        'CrossAttnDownBlock2D',
        'CrossAttnDownBlock2D',
        'DownBlock2D',
    )
    __UpperCAmelCase				=			False
    __UpperCAmelCase				=			(320, 640, 1280, 1280)
    __UpperCAmelCase				=			2
    __UpperCAmelCase				=			8
    __UpperCAmelCase				=			None
    __UpperCAmelCase				=			1280
    __UpperCAmelCase				=			0.0
    __UpperCAmelCase				=			False
    __UpperCAmelCase				=			jnp.floataa
    __UpperCAmelCase				=			True
    __UpperCAmelCase				=			0
    __UpperCAmelCase				=			'rgb'
    __UpperCAmelCase				=			(16, 32, 96, 256)





    def 	__snake_case  (							self							:							List[Any],							_snake_case							:							Optional[Any]      ):



       '''simple docstring'''







       snake_case       :				List[Any]		    =(1, self.in_channels, self.sample_size, self.sample_size)
       snake_case       :				Any		    =jnp.zeros(SCREAMING_SNAKE_CASE__,							dtype=jnp.floataa      )
       snake_case       :				List[str]		    =jnp.ones((1,),							dtype=jnp.intaa      )
       snake_case       :				Tuple		    =jnp.zeros((1, 1, self.cross_attention_dim),							dtype=jnp.floataa      )
       snake_case       :				List[str]		    =(1, 3, self.sample_size * 8, self.sample_size * 8)
       snake_case       :				Dict		    =jnp.zeros(SCREAMING_SNAKE_CASE__,							dtype=jnp.floataa      )

       snake_case       :				Tuple		    =jax.random.split(SCREAMING_SNAKE_CASE__      )
       snake_case       :				Union[str, Any]		    ={'''params''': params_rng, '''dropout''': dropout_rng}

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





    def 	__snake_case  (							self							:							List[Any]      ):



       '''simple docstring'''







       snake_case       :				Union[str, Any]		    =self.block_out_channels
       snake_case       :				Tuple		    =block_out_channels[0] * 4

       # 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       :				int		    =self.num_attention_heads or self.attention_head_dim

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

       # time
       snake_case       :				Optional[Any]		    =FlaxTimesteps(
           block_out_channels[0],							flip_sin_to_cos=self.flip_sin_to_cos,							freq_shift=self.config.freq_shift      )
       snake_case       :				Any		    =FlaxTimestepEmbedding(SCREAMING_SNAKE_CASE__,							dtype=self.dtype      )

       snake_case       :				str		    =FlaxControlNetConditioningEmbedding(
           conditioning_embedding_channels=block_out_channels[0],							block_out_channels=self.conditioning_embedding_out_channels,							)

       snake_case       :				Dict		    =self.only_cross_attention
       if isinstance(SCREAMING_SNAKE_CASE__,							SCREAMING_SNAKE_CASE__      ):
          snake_case       :				str		    =(only_cross_attention,) * len(self.down_block_types      )

       if isinstance(SCREAMING_SNAKE_CASE__,							SCREAMING_SNAKE_CASE__      ):
          snake_case       :				Union[str, Any]		    =(num_attention_heads,) * len(self.down_block_types      )

       # down
       snake_case       :				Tuple		    =[]
       snake_case       :				Optional[int]		    =[]

       snake_case       :				str		    =block_out_channels[0]

       snake_case       :				Tuple		    =nn.Conv(
           SCREAMING_SNAKE_CASE__,							kernel_size=(1, 1),							padding='''VALID''',							kernel_init=nn.initializers.zeros_init(),							bias_init=nn.initializers.zeros_init(),							dtype=self.dtype,							)
       controlnet_down_blocks.append(SCREAMING_SNAKE_CASE__      )

       for i, down_block_type in enumerate(self.down_block_types      ):
          snake_case       :				List[str]		    =output_channel
          snake_case       :				str		    =block_out_channels[i]
          snake_case       :				List[Any]		    =i == len(SCREAMING_SNAKE_CASE__      ) - 1

          if down_block_type == "CrossAttnDownBlock2D":
             snake_case       :				int		    =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],							dtype=self.dtype,							)
          else:
             snake_case       :				Tuple		    =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__      )

          for _ in range(self.layers_per_block      ):
             snake_case       :				Dict		    =nn.Conv(
                 SCREAMING_SNAKE_CASE__,							kernel_size=(1, 1),							padding='''VALID''',							kernel_init=nn.initializers.zeros_init(),							bias_init=nn.initializers.zeros_init(),							dtype=self.dtype,							)
             controlnet_down_blocks.append(SCREAMING_SNAKE_CASE__      )

          if not is_final_block:
             snake_case       :				Union[str, Any]		    =nn.Conv(
                 SCREAMING_SNAKE_CASE__,							kernel_size=(1, 1),							padding='''VALID''',							kernel_init=nn.initializers.zeros_init(),							bias_init=nn.initializers.zeros_init(),							dtype=self.dtype,							)
             controlnet_down_blocks.append(SCREAMING_SNAKE_CASE__      )

       snake_case       :				int		    =down_blocks
       snake_case       :				List[Any]		    =controlnet_down_blocks

       # mid
       snake_case       :				Union[str, Any]		    =block_out_channels[-1]
       snake_case       :				Optional[Any]		    =FlaxUNetMidBlockaDCrossAttn(
           in_channels=SCREAMING_SNAKE_CASE__,							dropout=self.dropout,							num_attention_heads=num_attention_heads[-1],							use_linear_projection=self.use_linear_projection,							dtype=self.dtype,							)

       snake_case       :				Tuple		    =nn.Conv(
           SCREAMING_SNAKE_CASE__,							kernel_size=(1, 1),							padding='''VALID''',							kernel_init=nn.initializers.zeros_init(),							bias_init=nn.initializers.zeros_init(),							dtype=self.dtype,							)






    def __call__(							self							:							Optional[int],							_snake_case							:							Tuple,							_snake_case							:							Optional[Any],							_snake_case							:							int,							_snake_case							:							Dict,							_snake_case							:							Optional[Any] = 1.0,							_snake_case							:							List[Any] = True,							_snake_case							:							List[str] = False,							):



       '''simple docstring'''







       snake_case       :				List[Any]		    =self.controlnet_conditioning_channel_order
       if channel_order == "bgr":
          snake_case       :				Union[str, Any]		    =jnp.flip(SCREAMING_SNAKE_CASE__,							axis=1      )

       # 1. time
       if not isinstance(SCREAMING_SNAKE_CASE__,							jnp.ndarray      ):
          snake_case       :				Any		    =jnp.array([timesteps],							dtype=jnp.intaa      )
       elif isinstance(SCREAMING_SNAKE_CASE__,							jnp.ndarray      ) and len(timesteps.shape      ) == 0:
          snake_case       :				Any		    =timesteps.astype(dtype=jnp.floataa      )
          snake_case       :				str		    =jnp.expand_dims(SCREAMING_SNAKE_CASE__,							0      )

       snake_case       :				Any		    =self.time_proj(SCREAMING_SNAKE_CASE__      )
       snake_case       :				Optional[Any]		    =self.time_embedding(SCREAMING_SNAKE_CASE__      )

       # 2. pre-process
       snake_case       :				Optional[int]		    =jnp.transpose(SCREAMING_SNAKE_CASE__,							(0, 2, 3, 1)      )
       snake_case       :				Tuple		    =self.conv_in(SCREAMING_SNAKE_CASE__      )

       snake_case       :				str		    =jnp.transpose(SCREAMING_SNAKE_CASE__,							(0, 2, 3, 1)      )
       snake_case       :				List[Any]		    =self.controlnet_cond_embedding(SCREAMING_SNAKE_CASE__      )
       sample += controlnet_cond

       # 3. down
       snake_case       :				Optional[int]		    =(sample,)
       for down_block in self.down_blocks:
          if isinstance(SCREAMING_SNAKE_CASE__,							SCREAMING_SNAKE_CASE__      ):
             snake_case       :				Dict		    =down_block(SCREAMING_SNAKE_CASE__,							SCREAMING_SNAKE_CASE__,							SCREAMING_SNAKE_CASE__,							deterministic=not train      )
          else:
             snake_case       :				Tuple		    =down_block(SCREAMING_SNAKE_CASE__,							SCREAMING_SNAKE_CASE__,							deterministic=not train      )
          down_block_res_samples += res_samples

       # 4. mid
       snake_case       :				Dict		    =self.mid_block(SCREAMING_SNAKE_CASE__,							SCREAMING_SNAKE_CASE__,							SCREAMING_SNAKE_CASE__,							deterministic=not train      )

       # 5. contronet blocks
       snake_case       :				Union[str, Any]		    =()
       for down_block_res_sample, controlnet_block in zip(SCREAMING_SNAKE_CASE__,							self.controlnet_down_blocks      ):
          snake_case       :				Tuple		    =controlnet_block(SCREAMING_SNAKE_CASE__      )
          controlnet_down_block_res_samples += (down_block_res_sample,)

       snake_case       :				List[str]		    =controlnet_down_block_res_samples

       snake_case       :				Tuple		    =self.controlnet_mid_block(SCREAMING_SNAKE_CASE__      )

       # 6. scaling
       snake_case       :				Optional[int]		    =[sample * conditioning_scale for sample in down_block_res_samples]
       mid_block_res_sample *= conditioning_scale

       if not return_dict:
          return (down_block_res_samples, mid_block_res_sample)

       return FlaxControlNetOutput(
           down_block_res_samples=SCREAMING_SNAKE_CASE__,							mid_block_res_sample=SCREAMING_SNAKE_CASE__      )









"""simple docstring"""






from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCamelCase__				:str			   =	logging.get_logger(__name__)

UpperCamelCase__				:Optional[int]			   =	{
    """microsoft/markuplm-base""": """https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json""",
    """microsoft/markuplm-large""": """https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json""",
}






class      A(  lowerCamelCase__		):

		"""simple docstring"""





		A    =							"markuplm"
		def __init__( self					,				SCREAMING_SNAKE_CASE__=3_05_22					,				SCREAMING_SNAKE_CASE__=7_68					,				SCREAMING_SNAKE_CASE__=12					,				SCREAMING_SNAKE_CASE__=12					,				SCREAMING_SNAKE_CASE__=30_72					,				SCREAMING_SNAKE_CASE__="gelu"					,				SCREAMING_SNAKE_CASE__=0.1					,				SCREAMING_SNAKE_CASE__=0.1					,				SCREAMING_SNAKE_CASE__=5_12					,				SCREAMING_SNAKE_CASE__=2					,				SCREAMING_SNAKE_CASE__=0.0_2					,				SCREAMING_SNAKE_CASE__=1E-12					,				SCREAMING_SNAKE_CASE__=0					,				SCREAMING_SNAKE_CASE__=0					,				SCREAMING_SNAKE_CASE__=2					,				SCREAMING_SNAKE_CASE__=2_56					,				SCREAMING_SNAKE_CASE__=10_24					,				SCREAMING_SNAKE_CASE__=2_16					,				SCREAMING_SNAKE_CASE__=10_01					,				SCREAMING_SNAKE_CASE__=32					,				SCREAMING_SNAKE_CASE__=50					,				SCREAMING_SNAKE_CASE__="absolute"					,				SCREAMING_SNAKE_CASE__=True					,				SCREAMING_SNAKE_CASE__=None					,				**SCREAMING_SNAKE_CASE__					,				)      ->	Optional[Any]:

					"""simple docstring"""



					super().__init__(
					    pad_token_id=SCREAMING_SNAKE_CASE__					,				bos_token_id=SCREAMING_SNAKE_CASE__					,				eos_token_id=SCREAMING_SNAKE_CASE__					,				**SCREAMING_SNAKE_CASE__					,				)
					_UpperCamelCase					:Any					     =	vocab_size
					_UpperCamelCase					:Union[str, Any]					     =	hidden_size
					_UpperCamelCase					:List[Any]					     =	num_hidden_layers
					_UpperCamelCase					:int					     =	num_attention_heads
					_UpperCamelCase					:Tuple					     =	hidden_act
					_UpperCamelCase					:str					     =	intermediate_size
					_UpperCamelCase					:Optional[int]					     =	hidden_dropout_prob
					_UpperCamelCase					:Any					     =	attention_probs_dropout_prob
					_UpperCamelCase					:Union[str, Any]					     =	max_position_embeddings
					_UpperCamelCase					:Dict					     =	type_vocab_size
					_UpperCamelCase					:int					     =	initializer_range
					_UpperCamelCase					:Optional[int]					     =	layer_norm_eps
					_UpperCamelCase					:Any					     =	position_embedding_type
					_UpperCamelCase					:Any					     =	use_cache
					_UpperCamelCase					:List[str]					     =	classifier_dropout
					# additional properties
					_UpperCamelCase					:Union[str, Any]					     =	max_depth
					_UpperCamelCase					:Union[str, Any]					     =	max_xpath_tag_unit_embeddings
					_UpperCamelCase					:Optional[Any]					     =	max_xpath_subs_unit_embeddings
					_UpperCamelCase					:int					     =	tag_pad_id
					_UpperCamelCase					:str					     =	subs_pad_id
					_UpperCamelCase					:List[str]					     =	xpath_unit_hidden_size

