




'''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,
    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, 1_280, 1_280)
       __a				:     int   				=					2
       __a				:     Union[int, Tuple[int]]   				=					8
       __a				:     Optional[Union[int, Tuple[int]]]   				=					None
       __a				:     int   				=					1_280
       __a				:     float   				=					0.0
       __a				:     bool   				=					False
       __a				:     jnp.dtype   				=					jnp.floataa
       __a				:     bool   				=					True
       __a				:     int   				=					0
       __a				:     bool   				=					False
       def     A      (	self						:	Dict   ,				lowercase						:	jax.random.KeyArray				):



           '''simple docstring'''

           UpperCAmelCase			       =		(1, self.in_channels, self.sample_size, self.sample_size)
           UpperCAmelCase			       =		jnp.zeros(SCREAMING_SNAKE_CASE__   ,				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(SCREAMING_SNAKE_CASE__				)
           UpperCAmelCase			       =		{'''params''': params_rng, '''dropout''': dropout_rng}

           return self.init(SCREAMING_SNAKE_CASE__   ,				SCREAMING_SNAKE_CASE__   ,				SCREAMING_SNAKE_CASE__   ,				SCREAMING_SNAKE_CASE__				)["params"]
       def     A      (	self						:	int				):



           '''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(SCREAMING_SNAKE_CASE__   ,				dtype=self.dtype				)

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

           if isinstance(SCREAMING_SNAKE_CASE__   ,				SCREAMING_SNAKE_CASE__				):
               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(SCREAMING_SNAKE_CASE__				) - 1

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

               down_blocks.append(SCREAMING_SNAKE_CASE__				)
           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(SCREAMING_SNAKE_CASE__				)				)
           UpperCAmelCase			       =		list(reversed(SCREAMING_SNAKE_CASE__				)				)
           UpperCAmelCase			       =		list(reversed(SCREAMING_SNAKE_CASE__				)				)
           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(SCREAMING_SNAKE_CASE__				) - 1				)]

               UpperCAmelCase			       =		i == len(SCREAMING_SNAKE_CASE__				) - 1

               if up_block_type == "CrossAttnUpBlock2D":
                   UpperCAmelCase			       =		FlaxCrossAttnUpBlockaD(
                       in_channels=SCREAMING_SNAKE_CASE__   ,				out_channels=SCREAMING_SNAKE_CASE__   ,				prev_output_channel=SCREAMING_SNAKE_CASE__   ,				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=SCREAMING_SNAKE_CASE__   ,				out_channels=SCREAMING_SNAKE_CASE__   ,				prev_output_channel=SCREAMING_SNAKE_CASE__   ,				num_layers=self.layers_per_block + 1   ,				add_upsample=not is_final_block   ,				dropout=self.dropout   ,				dtype=self.dtype   ,				)

               up_blocks.append(SCREAMING_SNAKE_CASE__				)
               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						:	Dict   ,				lowercase						:	int   ,				lowercase						:	Tuple   ,				lowercase						:	Union[str, Any]   ,				lowercase						:	Optional[Any]=None   ,				lowercase						:	List[str]=None   ,				lowercase						:	bool = True   ,				lowercase						:	bool = False   ,				):



           '''simple docstring'''

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

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

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

           # 3. down
           UpperCAmelCase			       =		(sample,)
           for down_block in self.down_blocks:
               if isinstance(SCREAMING_SNAKE_CASE__   ,				SCREAMING_SNAKE_CASE__				):
                   UpperCAmelCase       ,     UpperCAmelCase			       =		down_block(SCREAMING_SNAKE_CASE__   ,				SCREAMING_SNAKE_CASE__   ,				SCREAMING_SNAKE_CASE__   ,				deterministic=not train				)
               else:
                   UpperCAmelCase       ,     UpperCAmelCase			       =		down_block(SCREAMING_SNAKE_CASE__   ,				SCREAMING_SNAKE_CASE__   ,				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(
                   SCREAMING_SNAKE_CASE__   ,				SCREAMING_SNAKE_CASE__				):
                   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(SCREAMING_SNAKE_CASE__   ,				SCREAMING_SNAKE_CASE__   ,				SCREAMING_SNAKE_CASE__   ,				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(SCREAMING_SNAKE_CASE__   ,				SCREAMING_SNAKE_CASE__				):
                   UpperCAmelCase			       =		up_block(
                       SCREAMING_SNAKE_CASE__   ,				temb=SCREAMING_SNAKE_CASE__   ,				encoder_hidden_states=SCREAMING_SNAKE_CASE__   ,				res_hidden_states_tuple=SCREAMING_SNAKE_CASE__   ,				deterministic=not train   ,				)
               else:
                   UpperCAmelCase			       =		up_block(SCREAMING_SNAKE_CASE__   ,				temb=SCREAMING_SNAKE_CASE__   ,				res_hidden_states_tuple=SCREAMING_SNAKE_CASE__   ,				deterministic=not train				)

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

           if not return_dict:
               return (sample,)

           return FlaxUNetaDConditionOutput(sample=SCREAMING_SNAKE_CASE__				)





'''simple docstring'''

import re

from filelock import FileLock


try:
 import nltk

 _A			:      int					      =				True
except (ImportError, ModuleNotFoundError):
 _A			:      Optional[Any]					      =				False

if NLTK_AVAILABLE:
 with FileLock('''.lock''') as lock:
  nltk.download('''punkt''', quiet=True)







def      UpperCamelCase_						(					snake_case_       :       str      )					->     str:






 '''simple docstring'''

 re.sub("""<n>"""						,		""""""						,		snake_case_      )  # remove pegasus newline char
 assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)"
 return "\n".join(nltk.sent_tokenize(snake_case_      )      )






