
"""simple docstring"""




import functools
import math

import flax.linen as nn
import jax
import jax.numpy as jnp



def 						__magic_name__		(				UpperCamelCase    :		Tuple       ,		UpperCamelCase    :		Dict       ,		UpperCamelCase    :		Optional[Any]       ,		UpperCamelCase    :		List[str]       ,		UpperCamelCase    :		int = 4096    )    ->	Optional[int]:
    a__    = key.shape[-3:]
    a__    = value.shape[-1]
    a__    = min(UpperCamelCase__       ,		UpperCamelCase__    )
    a__    = query / jnp.sqrt(UpperCamelCase__    )

    @functools.partial(jax.checkpoint       ,		prevent_cse=UpperCamelCase__    )
    def summarize_chunk(UpperCamelCase    :		List[str]       ,		UpperCamelCase    :		Union[str, Any]       ,		UpperCamelCase    :		List[str]    ):
        a__    = jnp.einsum('...qhd,...khd->...qhk'       ,		UpperCamelCase__       ,		UpperCamelCase__       ,		precision=UpperCamelCase__    )

        a__    = jnp.max(UpperCamelCase__       ,		axis=-1       ,		keepdims=UpperCamelCase__    )
        a__    = jax.lax.stop_gradient(UpperCamelCase__    )
        a__    = jnp.exp(attn_weights - max_score    )

        a__    = jnp.einsum('...vhf,...qhv->...qhf'       ,		UpperCamelCase__       ,		UpperCamelCase__       ,		precision=UpperCamelCase__    )
        a__    = jnp.einsum('...qhk->...qh'       ,		UpperCamelCase__    )

        return (exp_values, exp_weights.sum(axis=-1    ), max_score)

    def chunk_scanner(UpperCamelCase    :		Optional[int]    ):
        # julienne key array
        a__    = jax.lax.dynamic_slice(
            operand=UpperCamelCase__       ,		start_indices=[0] * (key.ndim - 3) + [chunk_idx, 0, 0]       ,		slice_sizes=list(key.shape[:-3]    ) + [key_chunk_size, num_heads, k_features]       ,		)

        # julienne value array
        a__    = jax.lax.dynamic_slice(
            operand=UpperCamelCase__       ,		start_indices=[0] * (value.ndim - 3) + [chunk_idx, 0, 0]       ,		slice_sizes=list(value.shape[:-3]    ) + [key_chunk_size, num_heads, v_features]       ,		)

        return summarize_chunk(UpperCamelCase__       ,		UpperCamelCase__       ,		UpperCamelCase__    )

    a__    = jax.lax.map(f=UpperCamelCase__       ,		xs=jnp.arange(0       ,		UpperCamelCase__       ,		UpperCamelCase__    )    )

    a__    = jnp.max(UpperCamelCase__       ,		axis=0       ,		keepdims=UpperCamelCase__    )
    a__    = jnp.exp(chunk_max - global_max    )

    chunk_values *= jnp.expand_dims(UpperCamelCase__       ,		axis=-1    )
    chunk_weights *= max_diffs

    a__    = chunk_values.sum(axis=0    )
    a__    = jnp.expand_dims(UpperCamelCase__       ,		-1    ).sum(axis=0    )

    return all_values / all_weights



def 						__magic_name__		(				UpperCamelCase    :		Dict       ,		UpperCamelCase    :		Union[str, Any]       ,		UpperCamelCase    :		Any       ,		UpperCamelCase    :		Any=jax.lax.Precision.HIGHEST       ,		UpperCamelCase    :		int = 1024       ,		UpperCamelCase    :		int = 4096    )    ->	Dict:
    a__    = query.shape[-3:]

    def chunk_scanner(UpperCamelCase    :		Union[str, Any]       ,		UpperCamelCase    :		Optional[Any]    ):
        # julienne query array
        a__    = jax.lax.dynamic_slice(
            operand=UpperCamelCase__       ,		start_indices=([0] * (query.ndim - 3)) + [chunk_idx, 0, 0]       ,		slice_sizes=list(query.shape[:-3]    ) + [min(UpperCamelCase__       ,		UpperCamelCase__    ), num_heads, q_features]       ,		)

        return (
            chunk_idx + query_chunk_size,  # unused ignore it
            _query_chunk_attention(
                query=UpperCamelCase__       ,		key=UpperCamelCase__       ,		value=UpperCamelCase__       ,		precision=UpperCamelCase__       ,		key_chunk_size=UpperCamelCase__    ),
        )

    a__    = jax.lax.scan(
        f=UpperCamelCase__       ,		init=0       ,		xs=UpperCamelCase__       ,		length=math.ceil(num_q / query_chunk_size    )    )

    return jnp.concatenate(UpperCamelCase__       ,		axis=-3    )  # fuse the chunked result back







class 						lowercase(nn.Module					):
     __snake_case:  int  				=    42
     __snake_case:  List[str]  				=    8
     __snake_case:  Optional[Any]  				=    64
     __snake_case:  Optional[Any]  				=    0.0
     __snake_case:  Optional[Any]  				=    False
     __snake_case:  Optional[Any]  				=    jnp.floataa



     def  lowercase__(       self      )		->    Dict:







         """simple docstring"""



         a__    = self.dim_head * self.heads
         a__    = self.dim_head**-0.5

         # Weights were exported with old names {to_q, to_k, to_v, to_out}
         a__    = nn.Dense(__lowerCamelCase    ,							use_bias=__lowerCamelCase    ,							dtype=self.dtype    ,							name='to_q'      )
         a__    = nn.Dense(__lowerCamelCase    ,							use_bias=__lowerCamelCase    ,							dtype=self.dtype    ,							name='to_k'      )
         a__    = nn.Dense(__lowerCamelCase    ,							use_bias=__lowerCamelCase    ,							dtype=self.dtype    ,							name='to_v'      )

         a__    = nn.Dense(self.query_dim    ,							dtype=self.dtype    ,							name='to_out_0'      )
         a__    = nn.Dropout(rate=self.dropout      )



     def  lowercase__(       self    ,							__SCREAMING_SNAKE_CASE      )		->    Optional[int]:







         """simple docstring"""



         a__    = tensor.shape
         a__    = self.heads
         a__    = tensor.reshape(__lowerCamelCase    ,							__lowerCamelCase    ,							__lowerCamelCase    ,							dim // head_size      )
         a__    = jnp.transpose(__lowerCamelCase    ,							(0, 2, 1, 3)      )
         a__    = tensor.reshape(batch_size * head_size    ,							__lowerCamelCase    ,							dim // head_size      )
         return tensor



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







         """simple docstring"""



         a__    = tensor.shape
         a__    = self.heads
         a__    = tensor.reshape(batch_size // head_size    ,							__lowerCamelCase    ,							__lowerCamelCase    ,							__lowerCamelCase      )
         a__    = jnp.transpose(__lowerCamelCase    ,							(0, 2, 1, 3)      )
         a__    = tensor.reshape(batch_size // head_size    ,							__lowerCamelCase    ,							dim * head_size      )
         return tensor




     def __call__(       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE=None    ,							__SCREAMING_SNAKE_CASE=True      )		->    Any:







         """simple docstring"""



         a__    = hidden_states if context is None else context

         a__    = self.query(__lowerCamelCase      )
         a__    = self.key(__lowerCamelCase      )
         a__    = self.value(__lowerCamelCase      )

         a__    = self.reshape_heads_to_batch_dim(__lowerCamelCase      )
         a__    = self.reshape_heads_to_batch_dim(__lowerCamelCase      )
         a__    = self.reshape_heads_to_batch_dim(__lowerCamelCase      )

         if self.use_memory_efficient_attention:
             a__    = query_states.transpose(1    ,							0    ,							2      )
             a__    = key_states.transpose(1    ,							0    ,							2      )
             a__    = value_states.transpose(1    ,							0    ,							2      )

             # this if statement create a chunk size for each layer of the unet
             # the chunk size is equal to the query_length dimension of the deepest layer of the unet

             a__    = query_states.shape[-3]
             if flatten_latent_dim % 6_4 == 0:
                 a__    = int(flatten_latent_dim / 6_4      )
             elif flatten_latent_dim % 1_6 == 0:
                 a__    = int(flatten_latent_dim / 1_6      )
             elif flatten_latent_dim % 4 == 0:
                 a__    = int(flatten_latent_dim / 4      )
             else:
                 a__    = int(__lowerCamelCase      )

             a__    = jax_memory_efficient_attention(
                 __lowerCamelCase    ,							__lowerCamelCase    ,							__lowerCamelCase    ,							query_chunk_size=__lowerCamelCase    ,							key_chunk_size=4_0_9_6 * 4      )

             a__    = hidden_states.transpose(1    ,							0    ,							2      )
         else:
             # compute attentions
             a__    = jnp.einsum('b i d, b j d->b i j'    ,							__lowerCamelCase    ,							__lowerCamelCase      )
             a__    = attention_scores * self.scale
             a__    = nn.softmax(__lowerCamelCase    ,							axis=2      )

             # attend to values
             a__    = jnp.einsum('b i j, b j d -> b i d'    ,							__lowerCamelCase    ,							__lowerCamelCase      )

         a__    = self.reshape_batch_dim_to_heads(__lowerCamelCase      )
         a__    = self.proj_attn(__lowerCamelCase      )
         return self.dropout_layer(__lowerCamelCase    ,							deterministic=__lowerCamelCase      )







class 						lowercase(nn.Module					):
     __snake_case:  int  				=    42
     __snake_case:  int  				=    42
     __snake_case:  Any  				=    42
     __snake_case:  Tuple  				=    0.0
     __snake_case:  List[Any]  				=    False
     __snake_case:  Union[str, Any]  				=    jnp.floataa
     __snake_case:  Optional[Any]  				=    False



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







         """simple docstring"""



         a__    = FlaxAttention(
             self.dim    ,							self.n_heads    ,							self.d_head    ,							self.dropout    ,							self.use_memory_efficient_attention    ,							dtype=self.dtype      )
         # cross attention
         a__    = FlaxAttention(
             self.dim    ,							self.n_heads    ,							self.d_head    ,							self.dropout    ,							self.use_memory_efficient_attention    ,							dtype=self.dtype      )
         a__    = FlaxFeedForward(dim=self.dim    ,							dropout=self.dropout    ,							dtype=self.dtype      )
         a__    = nn.LayerNorm(epsilon=1e-5    ,							dtype=self.dtype      )
         a__    = nn.LayerNorm(epsilon=1e-5    ,							dtype=self.dtype      )
         a__    = nn.LayerNorm(epsilon=1e-5    ,							dtype=self.dtype      )
         a__    = nn.Dropout(rate=self.dropout      )




     def __call__(       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE=True      )		->    Optional[int]:







         """simple docstring"""



         a__    = hidden_states
         if self.only_cross_attention:
             a__    = self.attna(self.norma(__lowerCamelCase      )    ,							__lowerCamelCase    ,							deterministic=__lowerCamelCase      )
         else:
             a__    = self.attna(self.norma(__lowerCamelCase      )    ,							deterministic=__lowerCamelCase      )
         a__    = hidden_states + residual

         # cross attention
         a__    = hidden_states
         a__    = self.attna(self.norma(__lowerCamelCase      )    ,							__lowerCamelCase    ,							deterministic=__lowerCamelCase      )
         a__    = hidden_states + residual

         # feed forward
         a__    = hidden_states
         a__    = self.ff(self.norma(__lowerCamelCase      )    ,							deterministic=__lowerCamelCase      )
         a__    = hidden_states + residual

         return self.dropout_layer(__lowerCamelCase    ,							deterministic=__lowerCamelCase      )







class 						lowercase(nn.Module					):
     __snake_case:  Tuple  				=    42
     __snake_case:  Union[str, Any]  				=    42
     __snake_case:  List[str]  				=    42
     __snake_case:  int  				=    1
     __snake_case:  List[str]  				=    0.0
     __snake_case:  List[Any]  				=    False
     __snake_case:  List[Any]  				=    False
     __snake_case:  Optional[Any]  				=    jnp.floataa
     __snake_case:  Tuple  				=    False



     def  lowercase__(       self      )		->    Tuple:







         """simple docstring"""



         a__    = nn.GroupNorm(num_groups=3_2    ,							epsilon=1e-5      )

         a__    = self.n_heads * self.d_head
         if self.use_linear_projection:
             a__    = nn.Dense(__lowerCamelCase    ,							dtype=self.dtype      )
         else:
             a__    = nn.Conv(
                 __lowerCamelCase    ,							kernel_size=(1, 1)    ,							strides=(1, 1)    ,							padding='VALID'    ,							dtype=self.dtype    ,							)

         a__    = [
             FlaxBasicTransformerBlock(
                 __lowerCamelCase    ,							self.n_heads    ,							self.d_head    ,							dropout=self.dropout    ,							only_cross_attention=self.only_cross_attention    ,							dtype=self.dtype    ,							use_memory_efficient_attention=self.use_memory_efficient_attention    ,							)
             for _ in range(self.depth      )
         ]

         if self.use_linear_projection:
             a__    = nn.Dense(__lowerCamelCase    ,							dtype=self.dtype      )
         else:
             a__    = nn.Conv(
                 __lowerCamelCase    ,							kernel_size=(1, 1)    ,							strides=(1, 1)    ,							padding='VALID'    ,							dtype=self.dtype    ,							)

         a__    = nn.Dropout(rate=self.dropout      )




     def __call__(       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE=True      )		->    Tuple:







         """simple docstring"""



         a__    = hidden_states.shape
         a__    = hidden_states
         a__    = self.norm(__lowerCamelCase      )
         if self.use_linear_projection:
             a__    = hidden_states.reshape(__lowerCamelCase    ,							height * width    ,							__lowerCamelCase      )
             a__    = self.proj_in(__lowerCamelCase      )
         else:
             a__    = self.proj_in(__lowerCamelCase      )
             a__    = hidden_states.reshape(__lowerCamelCase    ,							height * width    ,							__lowerCamelCase      )

         for transformer_block in self.transformer_blocks:
             a__    = transformer_block(__lowerCamelCase    ,							__lowerCamelCase    ,							deterministic=__lowerCamelCase      )

         if self.use_linear_projection:
             a__    = self.proj_out(__lowerCamelCase      )
             a__    = hidden_states.reshape(__lowerCamelCase    ,							__lowerCamelCase    ,							__lowerCamelCase    ,							__lowerCamelCase      )
         else:
             a__    = hidden_states.reshape(__lowerCamelCase    ,							__lowerCamelCase    ,							__lowerCamelCase    ,							__lowerCamelCase      )
             a__    = self.proj_out(__lowerCamelCase      )

         a__    = hidden_states + residual
         return self.dropout_layer(__lowerCamelCase    ,							deterministic=__lowerCamelCase      )







class 						lowercase(nn.Module					):
     __snake_case:  int  				=    42
     __snake_case:  List[Any]  				=    0.0
     __snake_case:  List[Any]  				=    jnp.floataa



     def  lowercase__(       self      )		->    Tuple:







         """simple docstring"""



         a__    = FlaxGEGLU(self.dim    ,							self.dropout    ,							self.dtype      )
         a__    = nn.Dense(self.dim    ,							dtype=self.dtype      )




     def __call__(       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE=True      )		->    int:







         """simple docstring"""



         a__    = self.net_a(__lowerCamelCase    ,							deterministic=__lowerCamelCase      )
         a__    = self.net_a(__lowerCamelCase      )
         return hidden_states







class 						lowercase(nn.Module					):
     __snake_case:  Optional[Any]  				=    42
     __snake_case:  Union[str, Any]  				=    0.0
     __snake_case:  Union[str, Any]  				=    jnp.floataa



     def  lowercase__(       self      )		->    str:







         """simple docstring"""



         a__    = self.dim * 4
         a__    = nn.Dense(inner_dim * 2    ,							dtype=self.dtype      )
         a__    = nn.Dropout(rate=self.dropout      )




     def __call__(       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE=True      )		->    Any:







         """simple docstring"""



         a__    = self.proj(__lowerCamelCase      )
         a__    = jnp.split(__lowerCamelCase    ,							2    ,							axis=2      )
         return self.dropout_layer(hidden_linear * nn.gelu(__lowerCamelCase      )    ,							deterministic=__lowerCamelCase      )




"""simple docstring"""




a		:					str    	=					(1, 1_53, 3_70, 3_71, 16_34, 24_67_80_51, 1_15_13_22_19_01_87_63_99_25_65_09_55_97_97_39_71_52_24_01)
a		:					tuple    	=					(-1_53, -1, 0, 1.2, 2_00, "A", [], {}, None)



def 						__magic_name__		(				UpperCamelCase    :		int    )    ->	bool:
    if not isinstance(UpperCamelCase       ,		UpperCamelCase    ) or n < 1:
        return False

    # Initialization of sum and number of digits.
    a__    = 0
    a__    = 0
    a__    = n
    # Calculation of digits of the number
    while temp > 0:
        number_of_digits += 1
        temp //= 10
    # Dividing number into separate digits and find Armstrong number
    a__    = n
    while temp > 0:
        a__    = temp % 10
        total += rem**number_of_digits
        temp //= 10
    return n == total



def 						__magic_name__		(				UpperCamelCase    :		int    )    ->	bool:
    if not isinstance(UpperCamelCase       ,		UpperCamelCase    ) or n < 1:
        return False

    # Init a "histogram" of the digits
    a__    = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    a__    = 0
    a__    = 0
    a__    = n
    while temp > 0:
        a__  ,    a__    = divmod(UpperCamelCase       ,		10    )
        digit_histogram[rem] += 1
        digit_total += 1

    for cnt, i in zip(UpperCamelCase       ,		range(len(UpperCamelCase    )    )    ):
        total += cnt * i**digit_total

    return n == total



def 						__magic_name__		(				UpperCamelCase    :		int    )    ->	bool:
    if not isinstance(UpperCamelCase       ,		UpperCamelCase    ) or n < 1:
        return False
    a__    = len(str(UpperCamelCase    )    )  # the power that all digits will be raised to
    # check if sum of each digit multiplied expo times is equal to number
    return n == sum(int(UpperCamelCase    ) ** expo for i in str(UpperCamelCase    )    )



def 						__magic_name__		(				)    ->	List[Any]:
    a__    = int(input('Enter an integer to see if it is an Armstrong number: '    ).strip()    )
    print(f'{num} is {"" if armstrong_number(UpperCamelCase    ) else "not "}an Armstrong number.'    )
    print(f'{num} is {"" if narcissistic_number(UpperCamelCase    ) else "not "}an Armstrong number.'    )
    print(f'{num} is {"" if pluperfect_number(UpperCamelCase    ) else "not "}an Armstrong number.'    )


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    main()


