




'''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       (						_a       ):
							lowercase_							     =							42



@flax_register_to_config
class      A       (						nn.Module	,_a	,_a       ):
							lowercase_							     =							32
							lowercase_							     =							4
							lowercase_							     =							4
							lowercase_							     =							(
							    'CrossAttnDownBlock2D',
							    'CrossAttnDownBlock2D',
							    'CrossAttnDownBlock2D',
							    'DownBlock2D',
							)
							lowercase_							     =							('UpBlock2D', 'CrossAttnUpBlock2D', 'CrossAttnUpBlock2D', 'CrossAttnUpBlock2D')
							lowercase_							     =							False
							lowercase_							     =							(320, 640, 1280, 1280)
							lowercase_							     =							2
							lowercase_							     =							8
							lowercase_							     =							None
							lowercase_							     =							1280
							lowercase_							     =							0.0
							lowercase_							     =							False
							lowercase_							     =							jnp.floataa
							lowercase_							     =							True
							lowercase_							     =							0
							lowercase_							     =							False







							def   __lowerCAmelCase     (    self			:    Union[str, Any]				,     lowerCAmelCase_			:    Optional[Any]   )     ->				FrozenDict:


														"""simple docstring"""


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

														_a	,				_a      	=							jax.random.split(__a   )
														_a      	=							{'''params''': params_rng, '''dropout''': dropout_rng}

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







							def   __lowerCAmelCase     (    self			:    List[Any]   )     ->				str:


														"""simple docstring"""


														_a      	=							self.block_out_channels
														_a      	=							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.
														_a      	=							self.num_attention_heads or self.attention_head_dim

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

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

														_a      	=							self.only_cross_attention
														if isinstance(__a				,     __a   ):
																					_a      	=							(only_cross_attention,) * len(self.down_block_types   )

														if isinstance(__a				,     __a   ):
																					_a      	=							(num_attention_heads,) * len(self.down_block_types   )

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

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

														# mid
														_a      	=							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
														_a      	=							[]
														_a      	=							list(reversed(__a   )   )
														_a      	=							list(reversed(__a   )   )
														_a      	=							list(reversed(__a   )   )
														_a      	=							reversed_block_out_channels[0]
														for i, up_block_type in enumerate(self.up_block_types   ):
																					_a      	=							output_channel
																					_a      	=							reversed_block_out_channels[i]
																					_a      	=							reversed_block_out_channels[min(i + 1				,     len(__a   ) - 1   )]

																					_a      	=							i == len(__a   ) - 1

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

																					up_blocks.append(__a   )
																					_a      	=							output_channel
														_a      	=							up_blocks

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







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


														"""simple docstring"""


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

														_a      	=							self.time_proj(__a   )
														_a      	=							self.time_embedding(__a   )

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

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

														if down_block_additional_residuals is not None:
																					_a      	=							()

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

																					_a      	=							new_down_block_res_samples

														# 4. mid
														_a      	=							self.mid_block(__a				,     __a				,     __a				,     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:
																					_a      	=							down_block_res_samples[-(self.layers_per_block + 1) :]
																					_a      	=							down_block_res_samples[: -(self.layers_per_block + 1)]
																					if isinstance(__a				,     __a   ):
																												_a      	=							up_block(
																												    __a				,     temb=__a				,     encoder_hidden_states=__a				,     res_hidden_states_tuple=__a				,     deterministic=not train				,     )
																					else:
																												_a      	=							up_block(__a				,     temb=__a				,     res_hidden_states_tuple=__a				,     deterministic=not train   )

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

														if not return_dict:
																					return (sample,)

														return FlaxUNetaDConditionOutput(sample=__a   )



"""simple docstring"""





from datasets.utils.patching import _PatchedModuleObj, patch_submodule

from . import _test_patching



def  lowerCamelCase__						(     )				->						List[str]:


					"""simple docstring"""





					import os as original_os
					from os import path as original_path
					from os import rename as original_rename
					from os.path import dirname as original_dirname
					from os.path import join as original_join

					assert _test_patching.os is original_os
					assert _test_patching.path is original_path
					assert _test_patching.join is original_join

					assert _test_patching.renamed_os is original_os
					assert _test_patching.renamed_path is original_path
					assert _test_patching.renamed_join is original_join

					_UpperCamelCase									=    '''__test_patch_submodule_mock__'''
					with patch_submodule(_test_patching,	'''os.path.join''',	__snake_case      ):
										# Every way to access os.path.join must be patched, and the rest must stay untouched

										# check os.path.join
										assert isinstance(_test_patching.os,	_PatchedModuleObj      )
										assert isinstance(_test_patching.os.path,	_PatchedModuleObj      )
										assert _test_patching.os.path.join is mock

										# check path.join
										assert isinstance(_test_patching.path,	_PatchedModuleObj      )
										assert _test_patching.path.join is mock

										# check join
										assert _test_patching.join is mock

										# check that the other attributes are untouched
										assert _test_patching.os.rename is original_rename
										assert _test_patching.path.dirname is original_dirname
										assert _test_patching.os.path.dirname is original_dirname

										# Even renamed modules or objects must be patched

										# check renamed_os.path.join
										assert isinstance(_test_patching.renamed_os,	_PatchedModuleObj      )
										assert isinstance(_test_patching.renamed_os.path,	_PatchedModuleObj      )
										assert _test_patching.renamed_os.path.join is mock

										# check renamed_path.join
										assert isinstance(_test_patching.renamed_path,	_PatchedModuleObj      )
										assert _test_patching.renamed_path.join is mock

										# check renamed_join
										assert _test_patching.renamed_join is mock

										# check that the other attributes are untouched
										assert _test_patching.renamed_os.rename is original_rename
										assert _test_patching.renamed_path.dirname is original_dirname
										assert _test_patching.renamed_os.path.dirname is original_dirname

					# check that everthing is back to normal when the patch is over

					assert _test_patching.os is original_os
					assert _test_patching.path is original_path
					assert _test_patching.join is original_join

					assert _test_patching.renamed_os is original_os
					assert _test_patching.renamed_path is original_path
					assert _test_patching.renamed_join is original_join



def  lowerCamelCase__						(     )				->						List[str]:


					"""simple docstring"""





					assert _test_patching.open is open

					_UpperCamelCase									=    '''__test_patch_submodule_builtin_mock__'''
					# _test_patching has "open" in its globals
					assert _test_patching.open is open
					with patch_submodule(_test_patching,	'''open''',	__snake_case      ):
										assert _test_patching.open is mock

					# check that everthing is back to normal when the patch is over

					assert _test_patching.open is open



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


					"""simple docstring"""





					_UpperCamelCase									=    '''__test_patch_submodule_missing_mock__'''
					with patch_submodule(_test_patching,	'''pandas.read_csv''',	__snake_case      ):
										pass



def  lowerCamelCase__						(     )				->						Dict:


					"""simple docstring"""





					_UpperCamelCase									=    '''__test_patch_submodule_missing_builtin_mock__'''
					# _test_patching doesn't have "len" in its globals
					assert getattr(_test_patching,	'''len''',	__snake_case      ) is None
					with patch_submodule(_test_patching,	'''len''',	__snake_case      ):
										assert _test_patching.len is mock
					assert _test_patching.len is len



def  lowerCamelCase__						(     )				->						Tuple:


					"""simple docstring"""





					_UpperCamelCase									=    '''__test_patch_submodule_start_and_stop_mock__'''
					_UpperCamelCase									=    patch_submodule(_test_patching,	'''open''',	__snake_case      )
					assert _test_patching.open is open
					patch.start()
					assert _test_patching.open is mock
					patch.stop()
					assert _test_patching.open is open



def  lowerCamelCase__						(     )				->						Optional[int]:


					"""simple docstring"""





					from os import rename as original_rename
					from os.path import dirname as original_dirname
					from os.path import join as original_join

					_UpperCamelCase									=    '''__test_patch_submodule_successive_join__'''
					_UpperCamelCase									=    '''__test_patch_submodule_successive_dirname__'''
					_UpperCamelCase									=    '''__test_patch_submodule_successive_rename__'''
					assert _test_patching.os.path.join is original_join
					assert _test_patching.os.path.dirname is original_dirname
					assert _test_patching.os.rename is original_rename

					with patch_submodule(_test_patching,	'''os.path.join''',	__snake_case      ):
										with patch_submodule(_test_patching,	'''os.rename''',	__snake_case      ):
															with patch_submodule(_test_patching,	'''os.path.dirname''',	__snake_case      ):
																				assert _test_patching.os.path.join is mock_join
																				assert _test_patching.os.path.dirname is mock_dirname
																				assert _test_patching.os.rename is mock_rename

    # try another order
					with patch_submodule(_test_patching,	'''os.rename''',	__snake_case      ):
										with patch_submodule(_test_patching,	'''os.path.join''',	__snake_case      ):
															with patch_submodule(_test_patching,	'''os.path.dirname''',	__snake_case      ):
																				assert _test_patching.os.path.join is mock_join
																				assert _test_patching.os.path.dirname is mock_dirname
																				assert _test_patching.os.rename is mock_rename

					assert _test_patching.os.path.join is original_join
					assert _test_patching.os.path.dirname is original_dirname
					assert _test_patching.os.rename is original_rename



def  lowerCamelCase__						(     )				->						str:


					"""simple docstring"""





					_UpperCamelCase									=    '''__test_patch_submodule_doesnt_exist_mock__'''
					with patch_submodule(_test_patching,	'''__module_that_doesn_exist__.__attribute_that_doesn_exist__''',	__snake_case      ):
										pass
					with patch_submodule(_test_patching,	'''os.__attribute_that_doesn_exist__''',	__snake_case      ):
										pass






