



'''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 	SCREAMING_SNAKE_CASE__					( _UpperCamelCase       ):
	"""simple docstring"""


	A__     :     Tuple    =					42
	A__     :     Optional[Any]    =					42







class 	SCREAMING_SNAKE_CASE__					( nn.Module       ):
	"""simple docstring"""


	A__     :     Optional[int]    =					42
	A__     :     List[str]    =					(16, 32, 96, 256)
	A__     :     Union[str, Any]    =					jnp.floataa


	def   a__			(		self      )			->				Any:
				A:   Tuple								= nn.Conv(
				    self.block_out_channels[0]       , kernel_size=(3, 3)       , padding=((1, 1), (1, 1))       , dtype=self.dtype       , )

				A:   Dict								= []
				for i in range(len(self.block_out_channels      ) - 1      ):
							A:   str								= self.block_out_channels[i]
							A:   Optional[int]								= self.block_out_channels[i + 1]
							A:   int								= nn.Conv(
							    UpperCamelCase__       , kernel_size=(3, 3)       , padding=((1, 1), (1, 1))       , dtype=self.dtype       , )
							blocks.append(UpperCamelCase__      )
							A:   Tuple								= nn.Conv(
							    UpperCamelCase__       , kernel_size=(3, 3)       , strides=(2, 2)       , padding=((1, 1), (1, 1))       , dtype=self.dtype       , )
							blocks.append(UpperCamelCase__      )
				A:   Dict								= blocks

				A:   int								= 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       , A      )			->				str:
				A:   Any								= self.conv_in(UpperCamelCase__      )
				A:   Tuple								= nn.silu(UpperCamelCase__      )

				for block in self.blocks:
							A:   List[Any]								= block(UpperCamelCase__      )
							A:   Optional[Any]								= nn.silu(UpperCamelCase__      )

				A:   Optional[int]								= self.conv_out(UpperCamelCase__      )

				return embedding







@flax_register_to_config
class 	SCREAMING_SNAKE_CASE__					( nn.Module  ,      _UpperCamelCase  ,      _UpperCamelCase       ):
	"""simple docstring"""


	A__     :     Optional[int]    =					32
	A__     :     List[str]    =					4
	A__     :     Any    =					(
	    '''CrossAttnDownBlock2D''',
	    '''CrossAttnDownBlock2D''',
	    '''CrossAttnDownBlock2D''',
	    '''DownBlock2D''',
	)
	A__     :     List[Any]    =					False
	A__     :     Optional[int]    =					(320, 640, 1280, 1280)
	A__     :     Tuple    =					2
	A__     :     Optional[Any]    =					8
	A__     :     List[str]    =					None
	A__     :     List[str]    =					1280
	A__     :     Any    =					0.0
	A__     :     Optional[int]    =					False
	A__     :     int    =					jnp.floataa
	A__     :     Union[str, Any]    =					True
	A__     :     Tuple    =					0
	A__     :     Tuple    =					'''rgb'''
	A__     :     Dict    =					(16, 32, 96, 256)


	def   a__			(		self       , A      )			->				FrozenDict:
				# init input tensors
				A:   List[str]								= (1, self.in_channels, self.sample_size, self.sample_size)
				A:   Dict								= jnp.zeros(UpperCamelCase__       , dtype=jnp.floataa      )
				A:   Union[str, Any]								= jnp.ones((1,)       , dtype=jnp.intaa      )
				A:   List[Any]								= jnp.zeros((1, 1, self.cross_attention_dim)       , dtype=jnp.floataa      )
				A:   Optional[Any]								= (1, 3, self.sample_size * 8, self.sample_size * 8)
				A:   Union[str, Any]								= jnp.zeros(UpperCamelCase__       , dtype=jnp.floataa      )

				A				,		A:   List[str]								= jax.random.split(UpperCamelCase__      )
				A:   Any								= {"""params""": params_rng, """dropout""": dropout_rng}

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


	def   a__			(		self      )			->				Optional[Any]:
				A:   Any								= self.block_out_channels
				A:   Any								= 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.
				A:   Optional[Any]								= self.num_attention_heads or self.attention_head_dim

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

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

				A:   Optional[int]								= FlaxControlNetConditioningEmbedding(
				    conditioning_embedding_channels=block_out_channels[0]       , block_out_channels=self.conditioning_embedding_out_channels       , )

				A:   Optional[int]								= self.only_cross_attention
				if isinstance(UpperCamelCase__       , UpperCamelCase__      ):
							A:   Optional[Any]								= (only_cross_attention,) * len(self.down_block_types      )

				if isinstance(UpperCamelCase__       , UpperCamelCase__      ):
							A:   Optional[Any]								= (num_attention_heads,) * len(self.down_block_types      )

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

				A:   str								= block_out_channels[0]

				A:   Union[str, Any]								= 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      ):
							A:   int								= output_channel
							A:   Union[str, Any]								= block_out_channels[i]
							A:   List[Any]								= i == len(UpperCamelCase__      ) - 1

							if down_block_type == "CrossAttnDownBlock2D":
										A:   Tuple								= 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:
										A:   Optional[int]								= 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      ):
										A:   int								= 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:
										A:   List[Any]								= 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__      )

				A:   Union[str, Any]								= down_blocks
				A:   Union[str, Any]								= controlnet_down_blocks

				# mid
				A:   Tuple								= block_out_channels[-1]
				A:   Any								= FlaxUNetMidBlockaDCrossAttn(
				    in_channels=UpperCamelCase__       , dropout=self.dropout       , num_attention_heads=num_attention_heads[-1]       , use_linear_projection=self.use_linear_projection       , dtype=self.dtype       , )

				A:   Any								= 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       , A       , A       , A       , A       , A = 1.0       , A = True       , A = False       , )			->				Union[FlaxControlNetOutput, Tuple]:
				A:   Optional[Any]								= self.controlnet_conditioning_channel_order
				if channel_order == "bgr":
							A:   List[str]								= jnp.flip(UpperCamelCase__       , axis=1      )

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

				A:   Union[str, Any]								= self.time_proj(UpperCamelCase__      )
				A:   List[Any]								= self.time_embedding(UpperCamelCase__      )

				# 2. pre-process
				A:   Any								= jnp.transpose(UpperCamelCase__       , (0, 2, 3, 1)      )
				A:   List[Any]								= self.conv_in(UpperCamelCase__      )

				A:   Tuple								= jnp.transpose(UpperCamelCase__       , (0, 2, 3, 1)      )
				A:   str								= self.controlnet_cond_embedding(UpperCamelCase__      )
				sample += controlnet_cond

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

				# 4. mid
				A:   Dict								= self.mid_block(UpperCamelCase__       , UpperCamelCase__       , UpperCamelCase__       , deterministic=not train      )

				# 5. contronet blocks
				A:   int								= ()
				for down_block_res_sample, controlnet_block in zip(UpperCamelCase__       , self.controlnet_down_blocks      ):
							A:   str								= controlnet_block(UpperCamelCase__      )
							controlnet_down_block_res_samples += (down_block_res_sample,)

				A:   int								= controlnet_down_block_res_samples

				A:   int								= self.controlnet_mid_block(UpperCamelCase__      )

				# 6. scaling
				A:   Union[str, Any]								= [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'''






from . import (
    albert,
    align,
    altclip,
    audio_spectrogram_transformer,
    auto,
    autoformer,
    bark,
    bart,
    barthez,
    bartpho,
    beit,
    bert,
    bert_generation,
    bert_japanese,
    bertweet,
    big_bird,
    bigbird_pegasus,
    biogpt,
    bit,
    blenderbot,
    blenderbot_small,
    blip,
    blip_a,
    bloom,
    bridgetower,
    byta,
    camembert,
    canine,
    chinese_clip,
    clap,
    clip,
    clipseg,
    codegen,
    conditional_detr,
    convbert,
    convnext,
    convnextva,
    cpm,
    cpmant,
    ctrl,
    cvt,
    dataavec,
    deberta,
    deberta_va,
    decision_transformer,
    deformable_detr,
    deit,
    deprecated,
    deta,
    detr,
    dialogpt,
    dinat,
    distilbert,
    dit,
    donut,
    dpr,
    dpt,
    efficientformer,
    efficientnet,
    electra,
    encodec,
    encoder_decoder,
    ernie,
    ernie_m,
    esm,
    falcon,
    flaubert,
    flava,
    fnet,
    focalnet,
    fsmt,
    funnel,
    git,
    glpn,
    gpta,
    gpt_bigcode,
    gpt_neo,
    gpt_neox,
    gpt_neox_japanese,
    gpt_swa,
    gptj,
    gptsan_japanese,
    graphormer,
    groupvit,
    herbert,
    hubert,
    ibert,
    imagegpt,
    informer,
    instructblip,
    jukebox,
    layoutlm,
    layoutlmva,
    layoutlmva,
    layoutxlm,
    led,
    levit,
    lilt,
    llama,
    longformer,
    longta,
    luke,
    lxmert,
    mam_aaa,
    marian,
    markuplm,
    maskaformer,
    maskformer,
    mbart,
    mbartaa,
    mega,
    megatron_bert,
    megatron_gpta,
    mgp_str,
    mluke,
    mobilebert,
    mobilenet_va,
    mobilenet_va,
    mobilevit,
    mobilevitva,
    mpnet,
    mra,
    mta,
    musicgen,
    mvp,
    nat,
    nezha,
    nllb,
    nllb_moe,
    nystromformer,
    oneformer,
    open_llama,
    openai,
    opt,
    owlvit,
    pegasus,
    pegasus_x,
    perceiver,
    phobert,
    pixastruct,
    plbart,
    poolformer,
    prophetnet,
    qdqbert,
    rag,
    realm,
    reformer,
    regnet,
    rembert,
    resnet,
    roberta,
    roberta_prelayernorm,
    roc_bert,
    roformer,
    rwkv,
    sam,
    segformer,
    sew,
    sew_d,
    speech_encoder_decoder,
    speech_to_text,
    speech_to_text_a,
    speechta,
    splinter,
    squeezebert,
    swiftformer,
    swin,
    swinasr,
    swinva,
    switch_transformers,
    ta,
    table_transformer,
    tapas,
    time_series_transformer,
    timesformer,
    timm_backbone,
    transfo_xl,
    trocr,
    tvlt,
    umta,
    unispeech,
    unispeech_sat,
    upernet,
    videomae,
    vilt,
    vision_encoder_decoder,
    vision_text_dual_encoder,
    visual_bert,
    vit,
    vit_hybrid,
    vit_mae,
    vit_msn,
    vivit,
    wavaveca,
    wavaveca_conformer,
    wavaveca_phoneme,
    wavaveca_with_lm,
    wavlm,
    whisper,
    x_clip,
    xglm,
    xlm,
    xlm_prophetnet,
    xlm_roberta,
    xlm_roberta_xl,
    xlnet,
    xmod,
    yolos,
    yoso,
)



