



'''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 							lowercase__	(							lowercase   ):
	lowercase__  				=			42
	lowercase__  				=			42






class 							lowercase__	(							nn.Module   ):
	lowercase__  				=			42
	lowercase__  				=			(16, 32, 96, 2_56)
	lowercase__  				=			jnp.floataa





	def 	UpperCamelCase_  ( self					:				Tuple ):







							'''simple docstring'''
							_UpperCamelCase					:		List[str] 				=   nn.Conv(
							    self.block_out_channels[0] ,kernel_size=(3, 3) ,padding=((1, 1), (1, 1)) ,dtype=self.dtype ,)

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

							_UpperCamelCase					:		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					:				List[Any] ,lowerCamelCase__					:				Tuple ):







							'''simple docstring'''
							_UpperCamelCase					:		Optional[Any] 				=   self.conv_in(lowerCamelCase__ )
							_UpperCamelCase					:		List[Any] 				=   nn.silu(lowerCamelCase__ )

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

							_UpperCamelCase					:		List[str] 				=   self.conv_out(lowerCamelCase__ )

							return embedding






@flax_register_to_config
class 							lowercase__	(							nn.Module						,					lowercase						,					lowercase   ):
	lowercase__  				=			32
	lowercase__  				=			4
	lowercase__  				=			(
	    "CrossAttnDownBlock2D",
	    "CrossAttnDownBlock2D",
	    "CrossAttnDownBlock2D",
	    "DownBlock2D",
	)
	lowercase__  				=			False
	lowercase__  				=			(3_20, 6_40, 12_80, 12_80)
	lowercase__  				=			2
	lowercase__  				=			8
	lowercase__  				=			None
	lowercase__  				=			12_80
	lowercase__  				=			0.0
	lowercase__  				=			False
	lowercase__  				=			jnp.floataa
	lowercase__  				=			True
	lowercase__  				=			0
	lowercase__  				=			"rgb"
	lowercase__  				=			(16, 32, 96, 2_56)





	def 	UpperCamelCase_  ( self					:				List[Any] ,lowerCamelCase__					:				jax.random.KeyArray ):







							'''simple docstring'''
							# init input tensors
							_UpperCamelCase					:		Optional[Any] 				=   (1, self.in_channels, self.sample_size, self.sample_size)
							_UpperCamelCase					:		Tuple 				=   jnp.zeros(lowerCamelCase__ ,dtype=jnp.floataa )
							_UpperCamelCase					:		Optional[int] 				=   jnp.ones((1,) ,dtype=jnp.intaa )
							_UpperCamelCase					:		Any 				=   jnp.zeros((1, 1, self.cross_attention_dim) ,dtype=jnp.floataa )
							_UpperCamelCase					:		Tuple 				=   (1, 3, self.sample_size * 8, self.sample_size * 8)
							_UpperCamelCase					:		Union[str, Any] 				=   jnp.zeros(lowerCamelCase__ ,dtype=jnp.floataa )

							_UpperCamelCase				,			_UpperCamelCase					:		Optional[int] 				=   jax.random.split(lowerCamelCase__ )
							_UpperCamelCase					:		Optional[Any] 				=   {'params': params_rng, 'dropout': dropout_rng}

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





	def 	UpperCamelCase_  ( self					:				Optional[int] ):







							'''simple docstring'''
							_UpperCamelCase					:		Union[str, Any] 				=   self.block_out_channels
							_UpperCamelCase					:		Optional[int] 				=   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					:		Dict 				=   self.num_attention_heads or self.attention_head_dim

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

							# time
							_UpperCamelCase					:		Union[str, Any] 				=   FlaxTimesteps(
							    block_out_channels[0] ,flip_sin_to_cos=self.flip_sin_to_cos ,freq_shift=self.config.freq_shift )
							_UpperCamelCase					:		List[Any] 				=   FlaxTimestepEmbedding(lowerCamelCase__ ,dtype=self.dtype )

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

							_UpperCamelCase					:		Tuple 				=   self.only_cross_attention
							if isinstance(lowerCamelCase__ ,lowerCamelCase__ ):
													_UpperCamelCase					:		int 				=   (only_cross_attention,) * len(self.down_block_types )

							if isinstance(lowerCamelCase__ ,lowerCamelCase__ ):
													_UpperCamelCase					:		List[str] 				=   (num_attention_heads,) * len(self.down_block_types )

							# down
							_UpperCamelCase					:		List[Any] 				=   []
							_UpperCamelCase					:		Union[str, Any] 				=   []

							_UpperCamelCase					:		Dict 				=   block_out_channels[0]

							_UpperCamelCase					:		int 				=   nn.Conv(
							    lowerCamelCase__ ,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(lowerCamelCase__ )

							for i, down_block_type in enumerate(self.down_block_types ):
													_UpperCamelCase					:		Dict 				=   output_channel
													_UpperCamelCase					:		List[Any] 				=   block_out_channels[i]
													_UpperCamelCase					:		Any 				=   i == len(lowerCamelCase__ ) - 1

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

													down_blocks.append(lowerCamelCase__ )

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

													if not is_final_block:
																			_UpperCamelCase					:		List[Any] 				=   nn.Conv(
																			    lowerCamelCase__ ,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(lowerCamelCase__ )

							_UpperCamelCase					:		Optional[Any] 				=   down_blocks
							_UpperCamelCase					:		List[str] 				=   controlnet_down_blocks

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

							_UpperCamelCase					:		Any 				=   nn.Conv(
							    lowerCamelCase__ ,kernel_size=(1, 1) ,padding='VALID' ,kernel_init=nn.initializers.zeros_init() ,bias_init=nn.initializers.zeros_init() ,dtype=self.dtype ,)





	def __call__( self					:				int ,lowerCamelCase__					:				Union[str, Any] ,lowerCamelCase__					:				Optional[int] ,lowerCamelCase__					:				int ,lowerCamelCase__					:				str ,lowerCamelCase__					:				float = 1.0 ,lowerCamelCase__					:				bool = True ,lowerCamelCase__					:				bool = False ,):







							'''simple docstring'''
							_UpperCamelCase					:		Any 				=   self.controlnet_conditioning_channel_order
							if channel_order == "bgr":
													_UpperCamelCase					:		Optional[Any] 				=   jnp.flip(lowerCamelCase__ ,axis=1 )

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

							_UpperCamelCase					:		Any 				=   self.time_proj(lowerCamelCase__ )
							_UpperCamelCase					:		Dict 				=   self.time_embedding(lowerCamelCase__ )

							# 2. pre-process
							_UpperCamelCase					:		List[Any] 				=   jnp.transpose(lowerCamelCase__ ,(0, 2, 3, 1) )
							_UpperCamelCase					:		Optional[int] 				=   self.conv_in(lowerCamelCase__ )

							_UpperCamelCase					:		Optional[int] 				=   jnp.transpose(lowerCamelCase__ ,(0, 2, 3, 1) )
							_UpperCamelCase					:		Tuple 				=   self.controlnet_cond_embedding(lowerCamelCase__ )
							sample += controlnet_cond

							# 3. down
							_UpperCamelCase					:		Any 				=   (sample,)
							for down_block in self.down_blocks:
													if isinstance(lowerCamelCase__ ,lowerCamelCase__ ):
																			_UpperCamelCase				,			_UpperCamelCase					:		Union[str, Any] 				=   down_block(lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__ ,deterministic=not train )
													else:
																			_UpperCamelCase				,			_UpperCamelCase					:		str 				=   down_block(lowerCamelCase__ ,lowerCamelCase__ ,deterministic=not train )
													down_block_res_samples += res_samples

							# 4. mid
							_UpperCamelCase					:		Optional[int] 				=   self.mid_block(lowerCamelCase__ ,lowerCamelCase__ ,lowerCamelCase__ ,deterministic=not train )

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

							_UpperCamelCase					:		Optional[int] 				=   controlnet_down_block_res_samples

							_UpperCamelCase					:		Tuple 				=   self.controlnet_mid_block(lowerCamelCase__ )

							# 6. scaling
							_UpperCamelCase					:		Dict 				=   [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=lowerCamelCase__ ,mid_block_res_sample=lowerCamelCase__ )






"""simple docstring"""







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


a__				:							str						=     {
    '''configuration_roformer''': ['''ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''RoFormerConfig''', '''RoFormerOnnxConfig'''],
    '''tokenization_roformer''': ['''RoFormerTokenizer'''],
}

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a__				:							int						=     ['''RoFormerTokenizerFast''']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a__				:							int						=     [
		    '''ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''RoFormerForCausalLM''',
		    '''RoFormerForMaskedLM''',
		    '''RoFormerForMultipleChoice''',
		    '''RoFormerForQuestionAnswering''',
		    '''RoFormerForSequenceClassification''',
		    '''RoFormerForTokenClassification''',
		    '''RoFormerLayer''',
		    '''RoFormerModel''',
		    '''RoFormerPreTrainedModel''',
		    '''load_tf_weights_in_roformer''',
		]


try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a__				:							List[Any]						=     [
		    '''TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''TFRoFormerForCausalLM''',
		    '''TFRoFormerForMaskedLM''',
		    '''TFRoFormerForMultipleChoice''',
		    '''TFRoFormerForQuestionAnswering''',
		    '''TFRoFormerForSequenceClassification''',
		    '''TFRoFormerForTokenClassification''',
		    '''TFRoFormerLayer''',
		    '''TFRoFormerModel''',
		    '''TFRoFormerPreTrainedModel''',
		]


try:
		if not is_flax_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a__				:							Tuple						=     [
		    '''FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''FlaxRoFormerForMaskedLM''',
		    '''FlaxRoFormerForMultipleChoice''',
		    '''FlaxRoFormerForQuestionAnswering''',
		    '''FlaxRoFormerForSequenceClassification''',
		    '''FlaxRoFormerForTokenClassification''',
		    '''FlaxRoFormerModel''',
		    '''FlaxRoFormerPreTrainedModel''',
		]


if TYPE_CHECKING:
		from .configuration_roformer import ROFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, RoFormerConfig, RoFormerOnnxConfig
		from .tokenization_roformer import RoFormerTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_roformer_fast import RoFormerTokenizerFast

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_roformer import (
				    ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
				    RoFormerForCausalLM,
				    RoFormerForMaskedLM,
				    RoFormerForMultipleChoice,
				    RoFormerForQuestionAnswering,
				    RoFormerForSequenceClassification,
				    RoFormerForTokenClassification,
				    RoFormerLayer,
				    RoFormerModel,
				    RoFormerPreTrainedModel,
				    load_tf_weights_in_roformer,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_roformer import (
				    TF_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFRoFormerForCausalLM,
				    TFRoFormerForMaskedLM,
				    TFRoFormerForMultipleChoice,
				    TFRoFormerForQuestionAnswering,
				    TFRoFormerForSequenceClassification,
				    TFRoFormerForTokenClassification,
				    TFRoFormerLayer,
				    TFRoFormerModel,
				    TFRoFormerPreTrainedModel,
				)

		try:
				if not is_flax_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_flax_roformer import (
				    FLAX_ROFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
				    FlaxRoFormerForMaskedLM,
				    FlaxRoFormerForMultipleChoice,
				    FlaxRoFormerForQuestionAnswering,
				    FlaxRoFormerForSequenceClassification,
				    FlaxRoFormerForTokenClassification,
				    FlaxRoFormerModel,
				    FlaxRoFormerPreTrainedModel,
				)


else:
		import sys

		a__				:							int						=     _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
