



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



						"""simple docstring"""
						__UpperCamelCase		    =							42
						__UpperCamelCase		    =							42







class 				A_      (					nn.Module    ):



						"""simple docstring"""
						__UpperCamelCase		    =							42
						__UpperCamelCase		    =							(16, 32, 96, 2_56)
						__UpperCamelCase		    =							jnp.floataa






						def      UpperCAmelCase__					( self			:str )    ->      List[Any]:
												UpperCAmelCase					=			nn.Conv(
												    self.block_out_channels[0]       ,  kernel_size=(3, 3)       ,  padding=((1, 1), (1, 1))       ,  dtype=self.dtype       ,  )

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

												UpperCAmelCase					=			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]       ,  lowercase_			:List[str] )    ->      Optional[Any]:
												UpperCAmelCase					=			self.conv_in(lowercase_ )
												UpperCAmelCase					=			nn.silu(lowercase_ )

												for block in self.blocks:
																		UpperCAmelCase					=			block(lowercase_ )
																		UpperCAmelCase					=			nn.silu(lowercase_ )

												UpperCAmelCase					=			self.conv_out(lowercase_ )

												return embedding







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



						"""simple docstring"""
						__UpperCamelCase		    =							32
						__UpperCamelCase		    =							4
						__UpperCamelCase		    =							(
						    "CrossAttnDownBlock2D",
						    "CrossAttnDownBlock2D",
						    "CrossAttnDownBlock2D",
						    "DownBlock2D",
						)
						__UpperCamelCase		    =							False
						__UpperCamelCase		    =							(3_20, 6_40, 12_80, 12_80)
						__UpperCamelCase		    =							2
						__UpperCamelCase		    =							8
						__UpperCamelCase		    =							None
						__UpperCamelCase		    =							12_80
						__UpperCamelCase		    =							0.0
						__UpperCamelCase		    =							False
						__UpperCamelCase		    =							jnp.floataa
						__UpperCamelCase		    =							True
						__UpperCamelCase		    =							0
						__UpperCamelCase		    =							"rgb"
						__UpperCamelCase		    =							(16, 32, 96, 2_56)






						def      UpperCAmelCase__					( self			:int       ,  lowercase_			:jax.random.KeyArray )    ->      int:
												# init input tensors
												UpperCAmelCase					=			(1, self.in_channels, self.sample_size, self.sample_size)
												UpperCAmelCase					=			jnp.zeros(lowercase_       ,  dtype=jnp.floataa )
												UpperCAmelCase					=			jnp.ones((1,)       ,  dtype=jnp.intaa )
												UpperCAmelCase					=			jnp.zeros((1, 1, self.cross_attention_dim)       ,  dtype=jnp.floataa )
												UpperCAmelCase					=			(1, 3, self.sample_size * 8, self.sample_size * 8)
												UpperCAmelCase					=			jnp.zeros(lowercase_       ,  dtype=jnp.floataa )

												UpperCAmelCase					=			jax.random.split(lowercase_ )
												UpperCAmelCase					=			{"""params""": params_rng, """dropout""": dropout_rng}

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






						def      UpperCAmelCase__					( self			:Tuple )    ->      Union[str, Any]:
												UpperCAmelCase					=			self.block_out_channels
												UpperCAmelCase					=			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.
												UpperCAmelCase					=			self.num_attention_heads or self.attention_head_dim

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

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

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

												UpperCAmelCase					=			self.only_cross_attention
												if isinstance(lowercase_       ,  lowercase_ ):
																		UpperCAmelCase					=			(only_cross_attention,) * len(self.down_block_types )

												if isinstance(lowercase_       ,  lowercase_ ):
																		UpperCAmelCase					=			(num_attention_heads,) * len(self.down_block_types )

												# down
												UpperCAmelCase					=			[]
												UpperCAmelCase					=			[]

												UpperCAmelCase					=			block_out_channels[0]

												UpperCAmelCase					=			nn.Conv(
												    lowercase_       ,  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(lowercase_ )

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

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

																		down_blocks.append(lowercase_ )

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

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

												UpperCAmelCase					=			down_blocks
												UpperCAmelCase					=			controlnet_down_blocks

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

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






						def __call__( self			:List[str]       ,  lowercase_			:Union[str, Any]       ,  lowercase_			:List[str]       ,  lowercase_			:List[str]       ,  lowercase_			:int       ,  lowercase_			:float = 1.0       ,  lowercase_			:bool = True       ,  lowercase_			:bool = False       ,  )    ->      Optional[Any]:
												UpperCAmelCase					=			self.controlnet_conditioning_channel_order
												if channel_order == "bgr":
																		UpperCAmelCase					=			jnp.flip(lowercase_       ,  axis=1 )

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

												UpperCAmelCase					=			self.time_proj(lowercase_ )
												UpperCAmelCase					=			self.time_embedding(lowercase_ )

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

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

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

												# 4. mid
												UpperCAmelCase					=			self.mid_block(lowercase_       ,  lowercase_       ,  lowercase_       ,  deterministic=not train )

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

												UpperCAmelCase					=			controlnet_down_block_res_samples

												UpperCAmelCase					=			self.controlnet_mid_block(lowercase_ )

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




"""simple docstring"""


# Copyright 2022 The HuggingFace Team and The OpenBMB 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


snake_case_    					=	{
    """configuration_cpmant""": ["""CPMANT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """CpmAntConfig"""],
    """tokenization_cpmant""": ["""CpmAntTokenizer"""],
}

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				snake_case_    					=	[
				    """CPMANT_PRETRAINED_MODEL_ARCHIVE_LIST""",
				    """CpmAntForCausalLM""",
				    """CpmAntModel""",
				    """CpmAntPreTrainedModel""",
				]


if TYPE_CHECKING:
				from .configuration_cpmant import CPMANT_PRETRAINED_CONFIG_ARCHIVE_MAP, CpmAntConfig
				from .tokenization_cpmant import CpmAntTokenizer

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_cpmant import (
								    CPMANT_PRETRAINED_MODEL_ARCHIVE_LIST,
								    CpmAntForCausalLM,
								    CpmAntModel,
								    CpmAntPreTrainedModel,
								)


else:
				import sys

				snake_case_    					=	_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)





