


'''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						(lowercase_     ):





      '''simple docstring'''






      lowerCAmelCase__					:     jnp.ndarray
      lowerCAmelCase__					:     jnp.ndarray






class   __lowerCAmelCase						(nn.Module     ):





      '''simple docstring'''






      lowerCAmelCase__					:     int
      lowerCAmelCase__					:     Tuple[int]  					=							(16, 32, 96, 256)
      lowerCAmelCase__					:     jnp.dtype  					=							jnp.floataa



      def       UpperCamelCase__		(self    :    Optional[Any] ):

             '''simple docstring'''







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

             lowercase__      			= []
             for i in range(len(self.block_out_channels ) - 1 ):
                    lowercase__      			= self.block_out_channels[i]
                    lowercase__      			= self.block_out_channels[i + 1]
                    lowercase__      			= nn.Conv(
                        UpperCamelCase   ,						kernel_size=(3, 3)   ,						padding=((1, 1), (1, 1))   ,						dtype=self.dtype   ,						)
                    blocks.append(UpperCamelCase )
                    lowercase__      			= nn.Conv(
                        UpperCamelCase   ,						kernel_size=(3, 3)   ,						strides=(2, 2)   ,						padding=((1, 1), (1, 1))   ,						dtype=self.dtype   ,						)
                    blocks.append(UpperCamelCase )
             lowercase__      			= blocks

             lowercase__      			= 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    :    int   ,						UpperCamelCase    :    Tuple ):

             '''simple docstring'''







             lowercase__      			= self.conv_in(UpperCamelCase )
             lowercase__      			= nn.silu(UpperCamelCase )

             for block in self.blocks:
                    lowercase__      			= block(UpperCamelCase )
                    lowercase__      			= nn.silu(UpperCamelCase )

             lowercase__      			= self.conv_out(UpperCamelCase )

             return embedding






@flax_register_to_config
class   __lowerCAmelCase						(nn.Module  ,     lowercase_  ,     lowercase_     ):





      '''simple docstring'''






      lowerCAmelCase__					:     int  					=							32
      lowerCAmelCase__					:     int  					=							4
      lowerCAmelCase__					:     Tuple[str]  					=							(
          "CrossAttnDownBlock2D",
          "CrossAttnDownBlock2D",
          "CrossAttnDownBlock2D",
          "DownBlock2D",
      )
      lowerCAmelCase__					:     Union[bool, Tuple[bool]]  					=							False
      lowerCAmelCase__					:     Tuple[int]  					=							(320, 640, 1280, 1280)
      lowerCAmelCase__					:     int  					=							2
      lowerCAmelCase__					:     Union[int, Tuple[int]]  					=							8
      lowerCAmelCase__					:     Optional[Union[int, Tuple[int]]]  					=							None
      lowerCAmelCase__					:     int  					=							1280
      lowerCAmelCase__					:     float  					=							0.0
      lowerCAmelCase__					:     bool  					=							False
      lowerCAmelCase__					:     jnp.dtype  					=							jnp.floataa
      lowerCAmelCase__					:     bool  					=							True
      lowerCAmelCase__					:     int  					=							0
      lowerCAmelCase__					:     str  					=							"rgb"
      lowerCAmelCase__					:     Tuple[int]  					=							(16, 32, 96, 256)



      def       UpperCamelCase__		(self    :    str   ,						UpperCamelCase    :    jax.random.KeyArray ):

             '''simple docstring'''







             lowercase__      			= (1, self.in_channels, self.sample_size, self.sample_size)
             lowercase__      			= jnp.zeros(UpperCamelCase   ,						dtype=jnp.floataa )
             lowercase__      			= jnp.ones((1,)   ,						dtype=jnp.intaa )
             lowercase__      			= jnp.zeros((1, 1, self.cross_attention_dim)   ,						dtype=jnp.floataa )
             lowercase__      			= (1, 3, self.sample_size * 8, self.sample_size * 8)
             lowercase__      			= jnp.zeros(UpperCamelCase   ,						dtype=jnp.floataa )

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

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



      def       UpperCamelCase__		(self    :    str ):

             '''simple docstring'''







             lowercase__      			= self.block_out_channels
             lowercase__      			= 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.
             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(UpperCamelCase   ,						dtype=self.dtype )

             lowercase__      			= FlaxControlNetConditioningEmbedding(
                 conditioning_embedding_channels=block_out_channels[0]   ,						block_out_channels=self.conditioning_embedding_out_channels   ,						)

             lowercase__      			= self.only_cross_attention
             if isinstance(UpperCamelCase   ,						UpperCamelCase ):
                    lowercase__      			= (only_cross_attention,) * len(self.down_block_types )

             if isinstance(UpperCamelCase   ,						UpperCamelCase ):
                    lowercase__      			= (num_attention_heads,) * len(self.down_block_types )

             # down
             lowercase__      			= []
             lowercase__      			= []

             lowercase__      			= block_out_channels[0]

             lowercase__      			= nn.Conv(
                 UpperCamelCase   ,						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(UpperCamelCase )

             for i, down_block_type in enumerate(self.down_block_types ):
                    lowercase__      			= output_channel
                    lowercase__      			= block_out_channels[i]
                    lowercase__      			= i == len(UpperCamelCase ) - 1

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

                    down_blocks.append(UpperCamelCase )

                    for _ in range(self.layers_per_block ):
                           lowercase__      			= nn.Conv(
                               UpperCamelCase   ,						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(UpperCamelCase )

                    if not is_final_block:
                           lowercase__      			= nn.Conv(
                               UpperCamelCase   ,						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(UpperCamelCase )

             lowercase__      			= down_blocks
             lowercase__      			= controlnet_down_blocks

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

             lowercase__      			= nn.Conv(
                 UpperCamelCase   ,						kernel_size=(1, 1)   ,						padding='''VALID'''   ,						kernel_init=nn.initializers.zeros_init()   ,						bias_init=nn.initializers.zeros_init()   ,						dtype=self.dtype   ,						)






      def __call__(self    :    str   ,						UpperCamelCase    :    List[Any]   ,						UpperCamelCase    :    Union[str, Any]   ,						UpperCamelCase    :    List[Any]   ,						UpperCamelCase    :    Any   ,						UpperCamelCase    :    float = 1.0   ,						UpperCamelCase    :    bool = True   ,						UpperCamelCase    :    bool = False   ,						):

             '''simple docstring'''







             lowercase__      			= self.controlnet_conditioning_channel_order
             if channel_order == "bgr":
                    lowercase__      			= jnp.flip(UpperCamelCase   ,						axis=1 )

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

             lowercase__      			= self.time_proj(UpperCamelCase )
             lowercase__      			= self.time_embedding(UpperCamelCase )

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

             lowercase__      			= jnp.transpose(UpperCamelCase   ,						(0, 2, 3, 1) )
             lowercase__      			= self.controlnet_cond_embedding(UpperCamelCase )
             sample += controlnet_cond

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

             # 4. mid
             lowercase__      			= self.mid_block(UpperCamelCase   ,						UpperCamelCase   ,						UpperCamelCase   ,						deterministic=not train )

             # 5. contronet blocks
             lowercase__      			= ()
             for down_block_res_sample, controlnet_block in zip(UpperCamelCase   ,						self.controlnet_down_blocks ):
                    lowercase__      			= controlnet_block(UpperCamelCase )
                    controlnet_down_block_res_samples += (down_block_res_sample,)

             lowercase__      			= controlnet_down_block_res_samples

             lowercase__      			= self.controlnet_mid_block(UpperCamelCase )

             # 6. scaling
             lowercase__      			= [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=UpperCamelCase   ,						mid_block_res_sample=UpperCamelCase )








'''simple docstring'''

import warnings

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


lowerCamelCase							:   str  						=	logging.get_logger(__name__)

lowerCamelCase							:   int  						=	{
    'RUCAIBox/mvp': 'https://huggingface.co/RUCAIBox/mvp/resolve/main/config.json',
}
class   __lowerCAmelCase						(lowercase_     ):





      '''simple docstring'''






      lowerCAmelCase__					:     Union[str, Any]  					=							"""mvp"""
      lowerCAmelCase__					:     Optional[Any]  					=							["""past_key_values"""]
      lowerCAmelCase__					:     List[str]  					=							{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}



      def __init__(self    :    Any   ,						UpperCamelCase    :    Optional[int]=50267   ,						UpperCamelCase    :    Tuple=1024   ,						UpperCamelCase    :    int=12   ,						UpperCamelCase    :    Tuple=4096   ,						UpperCamelCase    :    Dict=16   ,						UpperCamelCase    :    int=12   ,						UpperCamelCase    :    Optional[int]=4096   ,						UpperCamelCase    :    Optional[int]=16   ,						UpperCamelCase    :    Tuple=0.0   ,						UpperCamelCase    :    Tuple=0.0   ,						UpperCamelCase    :    List[Any]="gelu"   ,						UpperCamelCase    :    Union[str, Any]=1024   ,						UpperCamelCase    :    Optional[Any]=0.1   ,						UpperCamelCase    :    str=0.0   ,						UpperCamelCase    :    str=0.0   ,						UpperCamelCase    :    Optional[Any]=0.02   ,						UpperCamelCase    :    List[str]=0.0   ,						UpperCamelCase    :    List[str]=False   ,						UpperCamelCase    :    Optional[int]=True   ,						UpperCamelCase    :    Any=1   ,						UpperCamelCase    :    int=0   ,						UpperCamelCase    :    int=2   ,						UpperCamelCase    :    Any=True   ,						UpperCamelCase    :    Optional[Any]=2   ,						UpperCamelCase    :    Optional[Any]=2   ,						UpperCamelCase    :    Tuple=False   ,						UpperCamelCase    :    int=100   ,						UpperCamelCase    :    Optional[Any]=800   ,						**UpperCamelCase    :    str   ,						):

             '''simple docstring'''







             lowercase__      			= vocab_size
             lowercase__      			= max_position_embeddings
             lowercase__      			= d_model
             lowercase__      			= encoder_ffn_dim
             lowercase__      			= encoder_layers
             lowercase__      			= encoder_attention_heads
             lowercase__      			= decoder_ffn_dim
             lowercase__      			= decoder_layers
             lowercase__      			= decoder_attention_heads
             lowercase__      			= dropout
             lowercase__      			= attention_dropout
             lowercase__      			= activation_dropout
             lowercase__      			= activation_function
             lowercase__      			= init_std
             lowercase__      			= encoder_layerdrop
             lowercase__      			= decoder_layerdrop
             lowercase__      			= classifier_dropout
             lowercase__      			= use_cache
             lowercase__      			= encoder_layers
             lowercase__      			= scale_embedding  # scale factor will be sqrt(d_model) if True
             lowercase__      			= use_prompt
             lowercase__      			= prompt_length
             lowercase__      			= prompt_mid_dim

             super().__init__(
                 pad_token_id=UpperCamelCase   ,						bos_token_id=UpperCamelCase   ,						eos_token_id=UpperCamelCase   ,						is_encoder_decoder=UpperCamelCase   ,						decoder_start_token_id=UpperCamelCase   ,						forced_eos_token_id=UpperCamelCase   ,						**UpperCamelCase   ,						)

             if self.forced_bos_token_id is None and kwargs.get('''force_bos_token_to_be_generated'''   ,						UpperCamelCase ):
                    lowercase__      			= self.bos_token_id
                    warnings.warn(
                        f"Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. "
                        '''The config can simply be saved and uploaded again to be fixed.''' )




