




"""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     __magic_name__							(       _UpperCAmelCase				):






				'''simple docstring'''
				__UpperCamelCase			  =						42






@flax_register_to_config
class     __magic_name__							(       nn.Module						,					_UpperCAmelCase						,					_UpperCAmelCase				):






				'''simple docstring'''
				__UpperCamelCase			  =						32
				__UpperCamelCase			  =						4
				__UpperCamelCase			  =						4
				__UpperCamelCase			  =						(
				    "CrossAttnDownBlock2D",
				    "CrossAttnDownBlock2D",
				    "CrossAttnDownBlock2D",
				    "DownBlock2D",
				)
				__UpperCamelCase			  =						("UpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D")
				__UpperCamelCase			  =						False
				__UpperCamelCase			  =						(3_20, 6_40, 12_80, 12_80)
				__UpperCamelCase			  =						2
				__UpperCamelCase			  =						8
				__UpperCamelCase			  =						None
				__UpperCamelCase			  =						12_80
				__UpperCamelCase			  =						0.0
				__UpperCamelCase			  =						False
				__UpperCamelCase			  =						jnp.floataa
				__UpperCamelCase			  =						True
				__UpperCamelCase			  =						0
				__UpperCamelCase			  =						False





				def    _lowerCAmelCase				(       self		, _a ):





							"""simple docstring"""
							lowerCamelCase			      =					(1, self.in_channels, self.sample_size, self.sample_size)
							lowerCamelCase			      =					jnp.zeros(lowercase_		, dtype=jnp.floataa )
							lowerCamelCase			      =					jnp.ones((1,)		, dtype=jnp.intaa )
							lowerCamelCase			      =					jnp.zeros((1, 1, self.cross_attention_dim)		, dtype=jnp.floataa )

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

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





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					self.block_out_channels
							lowerCamelCase			      =					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.
							lowerCamelCase			      =					self.num_attention_heads or self.attention_head_dim

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

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

							lowerCamelCase			      =					self.only_cross_attention
							if isinstance(lowercase_		, lowercase_ ):
										lowerCamelCase			      =					(only_cross_attention,) * len(self.down_block_types )

							if isinstance(lowercase_		, lowercase_ ):
										lowerCamelCase			      =					(num_attention_heads,) * len(self.down_block_types )

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

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

										down_blocks.append(lowercase_ )
							lowerCamelCase			      =					down_blocks

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

										lowerCamelCase			      =					i == len(lowercase_ ) - 1

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

										up_blocks.append(lowercase_ )
										lowerCamelCase			      =					output_channel
							lowerCamelCase			      =					up_blocks

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





				def __call__(       self		, _a		, _a		, _a		, _a=None		, _a=None		, _a = True		, _a = False		, ):





							"""simple docstring"""







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

							lowerCamelCase			      =					self.time_proj(lowercase_ )
							lowerCamelCase			      =					self.time_embedding(lowercase_ )

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

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

							if down_block_additional_residuals is not None:
										lowerCamelCase			      =					()

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

										lowerCamelCase			      =					new_down_block_res_samples

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

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

							if not return_dict:
										return (sample,)

							return FlaxUNetaDConditionOutput(sample=lowercase_ )






from timeit import timeit
def 						_snake_case(    SCREAMING_SNAKE_CASE__			:	int      )							->      int:





    '''simple docstring'''






    if number < 0:
        raise ValueError('the value of input must not be negative'      )
    A__          =   0
    while number:
        number &= number - 1
        result += 1
    return result
def 						_snake_case(    SCREAMING_SNAKE_CASE__			:	int      )							->      int:





    '''simple docstring'''






    if number < 0:
        raise ValueError('the value of input must not be negative'      )
    A__          =   0
    while number:
        if number % 2 == 1:
            result += 1
        number >>= 1
    return result
def 						_snake_case(    )							->      None:





    '''simple docstring'''






    def do_benchmark(SCREAMING_SNAKE_CASE__			:	int      ) -> None:
        A__          =   'import __main__ as z'
        print(f'Benchmark when {number = }:'      )
        print(f'{get_set_bits_count_using_modulo_operator(SCREAMING_SNAKE_CASE__      ) = }'      )
        A__          =   timeit('z.get_set_bits_count_using_modulo_operator(25)'    ,			setup=SCREAMING_SNAKE_CASE__      )
        print(f'timeit() runs in {timing} seconds'      )
        print(f'{get_set_bits_count_using_brian_kernighans_algorithm(SCREAMING_SNAKE_CASE__      ) = }'      )
        A__          =   timeit(
            'z.get_set_bits_count_using_brian_kernighans_algorithm(25)'    ,			setup=SCREAMING_SNAKE_CASE__    ,			)
        print(f'timeit() runs in {timing} seconds'      )

    for number in (25, 37, 58, 0):
        do_benchmark(SCREAMING_SNAKE_CASE__      )
        print()


if __name__ == "__main__":
     import doctest

     doctest.testmod()
     benchmark()

