




'''simple docstring'''


import argparse
from collections import defaultdict

import yaml


_snake_case : int  				=						'docs/source/en/_toctree.yml'
def 						snake_case_       (UpperCamelCase			:    List[Any] ):





							'''simple docstring'''


							_a      	=							defaultdict(UpperCamelCase )
							for doc in model_doc:
														counts[doc["local"]] += 1
							_a      	=							[key for key, value in counts.items() if value > 1]

							_a      	=							[]
							for duplicate_key in duplicates:
														_a      	=							list({doc['''title'''] for doc in model_doc if doc['''local'''] == duplicate_key} )
														if len(UpperCamelCase ) > 1:
																					raise ValueError(
																					    f'{duplicate_key} is present several times in the documentation table of content at '
																					    '''`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the '''
																					    '''others.''' )
														# Only add this once
														new_doc.append({'''local''': duplicate_key, '''title''': titles[0]} )

							# Add none duplicate-keys
							new_doc.extend([doc for doc in model_doc if counts[doc['''local''']] == 1] )

							# Sort
							return sorted(UpperCamelCase					,		key=lambda UpperCamelCase : s["title"].lower() )
def 						snake_case_       (UpperCamelCase			:    Any=False ):





							'''simple docstring'''


							with open(UpperCamelCase					,		encoding='''utf-8''' ) as f:
														_a      	=							yaml.safe_load(f.read() )

							# Get to the API doc
							_a      	=							0
							while content[api_idx]["title"] != "API":
														api_idx += 1
							_a      	=							content[api_idx]['''sections''']

							# Then to the model doc
							_a      	=							0
							while api_doc[model_idx]["title"] != "Models":
														model_idx += 1

							_a      	=							api_doc[model_idx]['''sections''']

							_a      	=							[(idx, section) for idx, section in enumerate(UpperCamelCase ) if '''sections''' in section]
							_a      	=							False
							for idx, modality_doc in modalities_docs:
														_a      	=							modality_doc['''sections''']
														_a      	=							clean_model_doc_toc(UpperCamelCase )

														if old_modality_doc != new_modality_doc:
																					_a      	=							True
																					if overwrite:
																												_a      	=							new_modality_doc

							if diff:
														if overwrite:
																					_a      	=							model_doc
																					_a      	=							api_doc
																					with open(UpperCamelCase					,		'''w'''					,		encoding='''utf-8''' ) as f:
																												f.write(yaml.dump(UpperCamelCase					,		allow_unicode=UpperCamelCase ) )
														else:
																					raise ValueError(
																					    '''The model doc part of the table of content is not properly sorted, run `make style` to fix this.''' )


if __name__ == "__main__":
				_snake_case : str  				=						argparse.ArgumentParser()
				parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.')
				_snake_case : Optional[int]  				=						parser.parse_args()

				check_model_doc(args.fix_and_overwrite)








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     snake_case_		(   a							):






 '''simple docstring'''





 __UpperCamelCase       =      42
 __UpperCamelCase       =      42

class     snake_case_		(   nn.Module							):






 '''simple docstring'''





 __UpperCamelCase       =      42
 __UpperCamelCase       =      (1_6, 3_2, 9_6, 2_5_6)
 __UpperCamelCase       =      jnp.floataa

 def  __UpperCAmelCase						(  self		)	->       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(
           A_,		kernel_size=(3, 3),		padding=((1, 1), (1, 1)),		dtype=self.dtype,		)
       blocks.append(A_		)
       UpperCAmelCase__						=nn.Conv(
           A_,		kernel_size=(3, 3),		strides=(2, 2),		padding=((1, 1), (1, 1)),		dtype=self.dtype,		)
       blocks.append(A_		)
    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,		A_		)	->       Any:
    UpperCAmelCase__						=self.conv_in(A_		)
    UpperCAmelCase__						=nn.silu(A_		)

    for block in self.blocks:
       UpperCAmelCase__						=block(A_		)
       UpperCAmelCase__						=nn.silu(A_		)

    UpperCAmelCase__						=self.conv_out(A_		)

    return embedding

@flax_register_to_config
class     snake_case_		(   nn.Module,		a,		a							):






 '''simple docstring'''





 __UpperCamelCase       =      3_2
 __UpperCamelCase       =      4
 __UpperCamelCase       =      (
     "CrossAttnDownBlock2D",
     "CrossAttnDownBlock2D",
     "CrossAttnDownBlock2D",
     "DownBlock2D",
 )
 __UpperCamelCase       =      False
 __UpperCamelCase       =      (3_2_0, 6_4_0, 1_2_8_0, 1_2_8_0)
 __UpperCamelCase       =      2
 __UpperCamelCase       =      8
 __UpperCamelCase       =      None
 __UpperCamelCase       =      1_2_8_0
 __UpperCamelCase       =      0.0
 __UpperCamelCase       =      False
 __UpperCamelCase       =      jnp.floataa
 __UpperCamelCase       =      True
 __UpperCamelCase       =      0
 __UpperCamelCase       =      "rgb"
 __UpperCamelCase       =      (1_6, 3_2, 9_6, 2_5_6)

 def  __UpperCAmelCase						(  self,		A_		)	->       FrozenDict:
    # init input tensors
    UpperCAmelCase__						=(1, self.in_channels, self.sample_size, self.sample_size)
    UpperCAmelCase__						=jnp.zeros(A_,		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(A_,		dtype=jnp.floataa		)

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

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

 def  __UpperCAmelCase						(  self		)	->       int:
    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(A_,		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(A_,		A_		):
       UpperCAmelCase__						=(only_cross_attention,) * len(self.down_block_types		)

    if isinstance(A_,		A_		):
       UpperCAmelCase__						=(num_attention_heads,) * len(self.down_block_types		)

    # down
    UpperCAmelCase__						=[]
    UpperCAmelCase__						=[]

    UpperCAmelCase__						=block_out_channels[0]

    UpperCAmelCase__						=nn.Conv(
        A_,		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(A_		)

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

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

       down_blocks.append(A_		)

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

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

    UpperCAmelCase__						=down_blocks
    UpperCAmelCase__						=controlnet_down_blocks

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

    UpperCAmelCase__						=nn.Conv(
        A_,		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]:
    UpperCAmelCase__						=self.controlnet_conditioning_channel_order
    if channel_order == "bgr":
       UpperCAmelCase__						=jnp.flip(A_,		axis=1		)

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

    UpperCAmelCase__						=self.time_proj(A_		)
    UpperCAmelCase__						=self.time_embedding(A_		)

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

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

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

    # 4. mid
    UpperCAmelCase__						=self.mid_block(A_,		A_,		A_,		deterministic=not train		)

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

    UpperCAmelCase__						=controlnet_down_block_res_samples

    UpperCAmelCase__						=self.controlnet_mid_block(A_		)

    # 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=A_,		mid_block_res_sample=A_		)



