




"""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      UpperCAmelCase__		(      lowercase__     ):
		lowerCAmelCase_       :	jnp.ndarray
		lowerCAmelCase_       :	jnp.ndarray






class      UpperCAmelCase__		(      nn.Module     ):
		lowerCAmelCase_       :	int
		lowerCAmelCase_       :	Tuple[int] 							=     (16, 32, 96, 2_56)
		lowerCAmelCase_       :	jnp.dtype 							=     jnp.floataa



		def 					A_		( self  :    str				)			->						Optional[Any]:






									'''simple docstring'''

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

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

									A         =					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  :    List[Any]      ,							snake_case  :    str				)			->						Optional[Any]:






									'''simple docstring'''

									A         =					self.conv_in(snake_case				)
									A         =					nn.silu(snake_case				)

									for block in self.blocks:
																A         =					block(snake_case				)
																A         =					nn.silu(snake_case				)

									A         =					self.conv_out(snake_case				)

									return embedding






@flax_register_to_config
class      UpperCAmelCase__		(      nn.Module   ,lowercase__   ,lowercase__     ):
		lowerCAmelCase_       :	int 							=     32
		lowerCAmelCase_       :	int 							=     4
		lowerCAmelCase_       :	Tuple[str] 							=     (
		    "CrossAttnDownBlock2D",
		    "CrossAttnDownBlock2D",
		    "CrossAttnDownBlock2D",
		    "DownBlock2D",
		)
		lowerCAmelCase_       :	Union[bool, Tuple[bool]] 							=     False
		lowerCAmelCase_       :	Tuple[int] 							=     (3_20, 6_40, 12_80, 12_80)
		lowerCAmelCase_       :	int 							=     2
		lowerCAmelCase_       :	Union[int, Tuple[int]] 							=     8
		lowerCAmelCase_       :	Optional[Union[int, Tuple[int]]] 							=     None
		lowerCAmelCase_       :	int 							=     12_80
		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, 2_56)



		def 					A_		( self  :    Any      ,							snake_case  :    Optional[int]				)			->						Union[str, Any]:






									'''simple docstring'''

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

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

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



		def 					A_		( self  :    int				)			->						int:






									'''simple docstring'''

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

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

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

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

									A         =					self.only_cross_attention
									if isinstance(snake_case      ,							snake_case				):
																A         =					(only_cross_attention,) * len(self.down_block_types				)

									if isinstance(snake_case      ,							snake_case				):
																A         =					(num_attention_heads,) * len(self.down_block_types				)

									# down
									A         =					[]
									A         =					[]

									A         =					block_out_channels[0]

									A         =					nn.Conv(
									    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(snake_case				)

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

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

																down_blocks.append(snake_case				)

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

																if not is_final_block:
																							A         =					nn.Conv(
																							    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(snake_case				)

									A         =					down_blocks
									A         =					controlnet_down_blocks

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

									A         =					nn.Conv(
									    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  :    Union[str, Any]      ,							snake_case  :    int      ,							snake_case  :    List[str]      ,							snake_case  :    Dict      ,							snake_case  :    int      ,							snake_case  :    Dict = 1.0      ,							snake_case  :    Dict = True      ,							snake_case  :    Optional[Any] = False      ,							)			->						Optional[int]:






									'''simple docstring'''

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

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

									A         =					self.time_proj(snake_case				)
									A         =					self.time_embedding(snake_case				)

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

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

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

									# 4. mid
									A         =					self.mid_block(snake_case      ,							snake_case      ,							snake_case      ,							deterministic=not train				)

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

									A         =					controlnet_down_block_res_samples

									A         =					self.controlnet_mid_block(snake_case				)

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








"""simple docstring"""







# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


A    =			{'configuration_mra': ['MRA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MraConfig']}

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							A    =			[
							    'MRA_PRETRAINED_MODEL_ARCHIVE_LIST',
							    'MraForMaskedLM',
							    'MraForMultipleChoice',
							    'MraForQuestionAnswering',
							    'MraForSequenceClassification',
							    'MraForTokenClassification',
							    'MraLayer',
							    'MraModel',
							    'MraPreTrainedModel',
							]


if TYPE_CHECKING:
							from .configuration_mra import MRA_PRETRAINED_CONFIG_ARCHIVE_MAP, MraConfig

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_mra import (
														    MRA_PRETRAINED_MODEL_ARCHIVE_LIST,
														    MraForMaskedLM,
														    MraForMultipleChoice,
														    MraForQuestionAnswering,
														    MraForSequenceClassification,
														    MraForTokenClassification,
														    MraLayer,
														    MraModel,
														    MraPreTrainedModel,
														)
else:
							import sys

							A    =			_LazyModule(__name__, globals()['__file__'], _import_structure)



