
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,
    FlaxCrossAttnUpBlockaD,
    FlaxDownBlockaD,
    FlaxUNetMidBlockaDCrossAttn,
    FlaxUpBlockaD,
)




@flax.struct.dataclass
class 		a_						( _UpperCAmelCase	):
    a      :				jnp.ndarray







@flax_register_to_config
class 		a_						( nn.Module     ,						_UpperCAmelCase     ,						_UpperCAmelCase	):
    a      :				int     =				32
    a      :				int     =				4
    a      :				int     =				4
    a      :				Tuple[str]     =				(
        "CrossAttnDownBlock2D",
        "CrossAttnDownBlock2D",
        "CrossAttnDownBlock2D",
        "DownBlock2D",
    )
    a      :				Tuple[str]     =				("UpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D")
    a      :				Union[bool, Tuple[bool]]     =				False
    a      :				Tuple[int]     =				(320, 640, 1280, 1280)
    a      :				int     =				2
    a      :				Union[int, Tuple[int]]     =				8
    a      :				Optional[Union[int, Tuple[int]]]     =				None
    a      :				int     =				1280
    a      :				float     =				0.0
    a      :				bool     =				False
    a      :				jnp.dtype     =				jnp.floataa
    a      :				bool     =				True
    a      :				int     =				0
    a      :				bool     =				False







    def      _snake_case					(			self			:       Tuple     ,					__UpperCamelCase			:       jax.random.KeyArray     )		->FrozenDict:






        '''simple docstring'''






        _UpperCAmelCase          =   (1, self.in_channels, self.sample_size, self.sample_size)
        _UpperCAmelCase          =   jnp.zeros(__UpperCamelCase     ,					dtype=jnp.floataa     )
        _UpperCAmelCase          =   jnp.ones((1,)     ,					dtype=jnp.intaa     )
        _UpperCAmelCase          =   jnp.zeros((1, 1, self.cross_attention_dim)     ,					dtype=jnp.floataa     )

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

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







    def      _snake_case					(			self			:       Union[str, Any]     )		->str:






        '''simple docstring'''






        _UpperCAmelCase          =   self.block_out_channels
        _UpperCAmelCase          =   block_out_channels[0] * 4

        if self.num_attention_heads is not None:
            raise ValueError(
                """At the moment it is not possible to define the number of attention heads via `num_attention_heads` because of a naming issue as described in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131. Passing `num_attention_heads` will only be supported in diffusers v0.19."""     )

        # 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(__UpperCamelCase     ,					dtype=self.dtype     )

        _UpperCAmelCase          =   self.only_cross_attention
        if isinstance(__UpperCamelCase     ,					__UpperCamelCase     ):
            _UpperCAmelCase          =   (only_cross_attention,) * len(self.down_block_types     )

        if isinstance(__UpperCamelCase     ,					__UpperCamelCase     ):
            _UpperCAmelCase          =   (num_attention_heads,) * len(self.down_block_types     )

        # down
        _UpperCAmelCase          =   []
        _UpperCAmelCase          =   block_out_channels[0]
        for i, down_block_type in enumerate(self.down_block_types     ):
            _UpperCAmelCase          =   output_channel
            _UpperCAmelCase          =   block_out_channels[i]
            _UpperCAmelCase          =   i == len(__UpperCamelCase     ) - 1

            if down_block_type == "CrossAttnDownBlock2D":
                _UpperCAmelCase          =   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]     ,					use_memory_efficient_attention=self.use_memory_efficient_attention     ,					dtype=self.dtype     ,					)
            else:
                _UpperCAmelCase          =   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     )
        _UpperCAmelCase          =   down_blocks

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

        # up
        _UpperCAmelCase          =   []
        _UpperCAmelCase          =   list(reversed(__UpperCamelCase     )     )
        _UpperCAmelCase          =   list(reversed(__UpperCamelCase     )     )
        _UpperCAmelCase          =   list(reversed(__UpperCamelCase     )     )
        _UpperCAmelCase          =   reversed_block_out_channels[0]
        for i, up_block_type in enumerate(self.up_block_types     ):
            _UpperCAmelCase          =   output_channel
            _UpperCAmelCase          =   reversed_block_out_channels[i]
            _UpperCAmelCase          =   reversed_block_out_channels[min(i + 1     ,					len(__UpperCamelCase     ) - 1     )]

            _UpperCAmelCase          =   i == len(__UpperCamelCase     ) - 1

            if up_block_type == "CrossAttnUpBlock2D":
                _UpperCAmelCase          =   FlaxCrossAttnUpBlockaD(
                    in_channels=__UpperCamelCase     ,					out_channels=__UpperCamelCase     ,					prev_output_channel=__UpperCamelCase     ,					num_layers=self.layers_per_block + 1     ,					num_attention_heads=reversed_num_attention_heads[i]     ,					add_upsample=not is_final_block     ,					dropout=self.dropout     ,					use_linear_projection=self.use_linear_projection     ,					only_cross_attention=only_cross_attention[i]     ,					use_memory_efficient_attention=self.use_memory_efficient_attention     ,					dtype=self.dtype     ,					)
            else:
                _UpperCAmelCase          =   FlaxUpBlockaD(
                    in_channels=__UpperCamelCase     ,					out_channels=__UpperCamelCase     ,					prev_output_channel=__UpperCamelCase     ,					num_layers=self.layers_per_block + 1     ,					add_upsample=not is_final_block     ,					dropout=self.dropout     ,					dtype=self.dtype     ,					)

            up_blocks.append(__UpperCamelCase     )
            _UpperCAmelCase          =   output_channel
        _UpperCAmelCase          =   up_blocks

        # out
        _UpperCAmelCase          =   nn.GroupNorm(num_groups=32     ,					epsilon=1e-5     )
        _UpperCAmelCase          =   nn.Conv(
            self.out_channels     ,					kernel_size=(3, 3)     ,					strides=(1, 1)     ,					padding=((1, 1), (1, 1))     ,					dtype=self.dtype     ,					)







    def __call__(			self			:       Tuple     ,					__UpperCamelCase			:       Union[str, Any]     ,					__UpperCamelCase			:       List[Any]     ,					__UpperCamelCase			:       Optional[Any]     ,					__UpperCamelCase			:       int=None     ,					__UpperCamelCase			:       Any=None     ,					__UpperCamelCase			:       bool = True     ,					__UpperCamelCase			:       bool = False     ,					)		->Union[FlaxUNetaDConditionOutput, Tuple]:






        '''simple docstring'''






        if not isinstance(__UpperCamelCase     ,					jnp.ndarray     ):
            _UpperCAmelCase          =   jnp.array([timesteps]     ,					dtype=jnp.intaa     )
        elif isinstance(__UpperCamelCase     ,					jnp.ndarray     ) and len(timesteps.shape     ) == 0:
            _UpperCAmelCase          =   timesteps.astype(dtype=jnp.floataa     )
            _UpperCAmelCase          =   jnp.expand_dims(__UpperCamelCase     ,					0     )

        _UpperCAmelCase          =   self.time_proj(__UpperCamelCase     )
        _UpperCAmelCase          =   self.time_embedding(__UpperCamelCase     )

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

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

        if down_block_additional_residuals is not None:
            _UpperCAmelCase          =   ()

            for down_block_res_sample, down_block_additional_residual in zip(
                __UpperCamelCase     ,					__UpperCamelCase     ):
                down_block_res_sample += down_block_additional_residual
                new_down_block_res_samples += (down_block_res_sample,)

            _UpperCAmelCase          =   new_down_block_res_samples

        # 4. mid
        _UpperCAmelCase          =   self.mid_block(__UpperCamelCase     ,					__UpperCamelCase     ,					__UpperCamelCase     ,					deterministic=not train     )

        if mid_block_additional_residual is not None:
            sample += mid_block_additional_residual

        # 5. up
        for up_block in self.up_blocks:
            _UpperCAmelCase          =   down_block_res_samples[-(self.layers_per_block + 1) :]
            _UpperCAmelCase          =   down_block_res_samples[: -(self.layers_per_block + 1)]
            if isinstance(__UpperCamelCase     ,					__UpperCamelCase     ):
                _UpperCAmelCase          =   up_block(
                    __UpperCamelCase     ,					temb=__UpperCamelCase     ,					encoder_hidden_states=__UpperCamelCase     ,					res_hidden_states_tuple=__UpperCamelCase     ,					deterministic=not train     ,					)
            else:
                _UpperCAmelCase          =   up_block(__UpperCamelCase     ,					temb=__UpperCamelCase     ,					res_hidden_states_tuple=__UpperCamelCase     ,					deterministic=not train     )

        # 6. post-process
        _UpperCAmelCase          =   self.conv_norm_out(__UpperCamelCase     )
        _UpperCAmelCase          =   nn.silu(__UpperCamelCase     )
        _UpperCAmelCase          =   self.conv_out(__UpperCamelCase     )
        _UpperCAmelCase          =   jnp.transpose(__UpperCamelCase     ,					(0, 3, 1, 2)     )

        if not return_dict:
            return (sample,)

        return FlaxUNetaDConditionOutput(sample=__UpperCamelCase     )

"""simple docstring"""



import argparse
import os
import re

import packaging.version


a					:	str							=		'''examples/'''
a					:	List[str]							=		{
    '''examples''': (re.compile(r'''^check_min_version\("[^"]+"\)\s*$''', re.MULTILINE), '''check_min_version("VERSION")\n'''),
    '''init''': (re.compile(r'''^__version__\s+=\s+"([^"]+)"\s*$''', re.MULTILINE), '''__version__ = "VERSION"\n'''),
    '''setup''': (re.compile(r'''^(\s*)version\s*=\s*"[^"]+",''', re.MULTILINE), r'''\1version="VERSION",'''),
    '''doc''': (re.compile(r'''^(\s*)release\s*=\s*"[^"]+"$''', re.MULTILINE), '''release = "VERSION"\n'''),
}
a					:	Tuple							=		{
    '''init''': '''src/diffusers/__init__.py''',
    '''setup''': '''setup.py''',
}
a					:	List[str]							=		'''README.md'''


def  _UpperCamelCase	(						_A					,      _A					,      _A      )    ->  Dict:





    """simple docstring"""

    with open(_A					,      """r"""					,      encoding="""utf-8"""					,      newline="""\n"""      ) as f:
        _UpperCAmelCase          =   f.read()
    _UpperCAmelCase    ,_UpperCAmelCase          =   REPLACE_PATTERNS[pattern]
    _UpperCAmelCase          =   replace.replace("""VERSION"""					,      _A      )
    _UpperCAmelCase          =   re_pattern.sub(_A					,      _A      )
    with open(_A					,      """w"""					,      encoding="""utf-8"""					,      newline="""\n"""      ) as f:
        f.write(_A      )


def  _UpperCamelCase	(						_A      )    ->  Union[str, Any]:





    """simple docstring"""

    for folder, directories, fnames in os.walk(_A      ):
        # Removing some of the folders with non-actively maintained examples from the walk
        if "research_projects" in directories:
            directories.remove("""research_projects"""      )
        if "legacy" in directories:
            directories.remove("""legacy"""      )
        for fname in fnames:
            if fname.endswith(""".py"""      ):
                update_version_in_file(os.path.join(_A					,      _A      )					,      _A					,      pattern="""examples"""      )


def  _UpperCamelCase	(						_A					,      _A=False      )    ->  int:





    """simple docstring"""

    for pattern, fname in REPLACE_FILES.items():
        update_version_in_file(_A					,      _A					,      _A      )
    if not patch:
        update_version_in_examples(_A      )


def  _UpperCamelCase	(						)    ->  Any:





    """simple docstring"""

    _UpperCAmelCase          =   """🤗 Transformers currently provides the following architectures"""
    _UpperCAmelCase          =   """1. Want to contribute a new model?"""
    with open(_A					,      """r"""					,      encoding="""utf-8"""					,      newline="""\n"""      ) as f:
        _UpperCAmelCase          =   f.readlines()

    # Find the start of the list.
    _UpperCAmelCase          =   0
    while not lines[start_index].startswith(_start_prompt      ):
        start_index += 1
    start_index += 1

    _UpperCAmelCase          =   start_index
    # Update the lines in the model list.
    while not lines[index].startswith(_end_prompt      ):
        if lines[index].startswith("""1."""      ):
            _UpperCAmelCase          =   lines[index].replace(
                """https://huggingface.co/docs/diffusers/main/model_doc"""					,      """https://huggingface.co/docs/diffusers/model_doc"""					,      )
        index += 1

    with open(_A					,      """w"""					,      encoding="""utf-8"""					,      newline="""\n"""      ) as f:
        f.writelines(_A      )


def  _UpperCamelCase	(						)    ->  Optional[int]:





    """simple docstring"""

    with open(REPLACE_FILES["""init"""]					,      """r"""      ) as f:
        _UpperCAmelCase          =   f.read()
    _UpperCAmelCase          =   REPLACE_PATTERNS["""init"""][0].search(_A      ).groups()[0]
    return packaging.version.parse(_A      )


def  _UpperCamelCase	(						_A=False      )    ->  List[Any]:





    """simple docstring"""

    _UpperCAmelCase          =   get_version()
    if patch and default_version.is_devrelease:
        raise ValueError("""Can't create a patch version from the dev branch, checkout a released version!"""      )
    if default_version.is_devrelease:
        _UpperCAmelCase          =   default_version.base_version
    elif patch:
        _UpperCAmelCase          =   F"""{default_version.major}.{default_version.minor}.{default_version.micro + 1}"""
    else:
        _UpperCAmelCase          =   F"""{default_version.major}.{default_version.minor + 1}.0"""

    # Now let's ask nicely if that's the right one.
    _UpperCAmelCase          =   input(F"""Which version are you releasing? [{default_version}]"""      )
    if len(_A      ) == 0:
        _UpperCAmelCase          =   default_version

    print(F"""Updating version to {version}."""      )
    global_version_update(_A					,      patch=_A      )


def  _UpperCamelCase	(						)    ->  List[Any]:





    """simple docstring"""

    _UpperCAmelCase          =   get_version()
    _UpperCAmelCase          =   F"""{current_version.major}.{current_version.minor + 1}.0.dev0"""
    _UpperCAmelCase          =   current_version.base_version

    # Check with the user we got that right.
    _UpperCAmelCase          =   input(F"""Which version are we developing now? [{dev_version}]"""      )
    if len(_A      ) == 0:
        _UpperCAmelCase          =   dev_version

    print(F"""Updating version to {version}."""      )
    global_version_update(_A      )


#    print("Cleaning main README, don't forget to run `make fix-copies`.")
#    clean_main_ref_in_model_list()


if __name__ == "__main__":
 a					:	Dict							=		argparse.ArgumentParser()
 parser.add_argument('''--post_release''', action='''store_true''', help='''Whether this is pre or post release.''')
 parser.add_argument('''--patch''', action='''store_true''', help='''Whether or not this is a patch release.''')
 a					:	Tuple							=		parser.parse_args()
 if not args.post_release:
  pre_release_work(patch=args.patch)
 elif args.patch:
  print('''Nothing to do after a patch :-)''')
 else:
  post_release_work()