




'''simple docstring'''





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

from .attention_flax import FlaxTransformeraDModel
from .resnet_flax import FlaxDownsampleaD, FlaxResnetBlockaD, FlaxUpsampleaD




class    __lowerCAmelCase       (							nn.Module):
       _a 					=						42
       _a 					=						42
       _a 					=						0.0
       _a 					=						1
       _a 					=						1
       _a 					=						True
       _a 					=						False
       _a 					=						False
       _a 					=						False
       _a 					=						jnp.floataa






       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         lowercase    :Tuple      	=						[]
         lowercase    :Union[str, Any]      	=						[]

         for i in range(self.num_layers				):
           lowercase    :Union[str, Any]      	=						self.in_channels if i == 0 else self.out_channels

           lowercase    :Optional[Any]      	=						FlaxResnetBlockaD(
               in_channels=__A       ,					out_channels=self.out_channels       ,					dropout_prob=self.dropout       ,					dtype=self.dtype       ,					)
           resnets.append(__A				)

           lowercase    :int      	=						FlaxTransformeraDModel(
               in_channels=self.out_channels       ,					n_heads=self.num_attention_heads       ,					d_head=self.out_channels // self.num_attention_heads       ,					depth=1       ,					use_linear_projection=self.use_linear_projection       ,					only_cross_attention=self.only_cross_attention       ,					use_memory_efficient_attention=self.use_memory_efficient_attention       ,					dtype=self.dtype       ,					)
           attentions.append(__A				)

         lowercase    :str      	=						resnets
         lowercase    :Optional[Any]      	=						attentions

         if self.add_downsample:
           lowercase    :Any      	=						FlaxDownsampleaD(self.out_channels       ,					dtype=self.dtype				)






       def __call__(   self:					Any       ,					_lowerCAmelCase:					Tuple       ,					_lowerCAmelCase:					Any       ,					_lowerCAmelCase:					Optional[Any]       ,					_lowerCAmelCase:					str=True				):
         lowercase    :List[Any]      	=						()

         for resnet, attn in zip(self.resnets       ,					self.attentions				):
           lowercase    :Optional[int]      	=						resnet(__A       ,					__A       ,					deterministic=__A				)
           lowercase    :Optional[int]      	=						attn(__A       ,					__A       ,					deterministic=__A				)
           output_states += (hidden_states,)

         if self.add_downsample:
           lowercase    :Dict      	=						self.downsamplers_a(__A				)
           output_states += (hidden_states,)

         return hidden_states, output_states







class    __lowerCAmelCase       (							nn.Module):
       _a 					=						42
       _a 					=						42
       _a 					=						0.0
       _a 					=						1
       _a 					=						True
       _a 					=						jnp.floataa






       def 					SCREAMING_SNAKE_CASE    (   self:					Union[str, Any]				):
         lowercase    :Union[str, Any]      	=						[]

         for i in range(self.num_layers				):
           lowercase    :Optional[int]      	=						self.in_channels if i == 0 else self.out_channels

           lowercase    :Dict      	=						FlaxResnetBlockaD(
               in_channels=__A       ,					out_channels=self.out_channels       ,					dropout_prob=self.dropout       ,					dtype=self.dtype       ,					)
           resnets.append(__A				)
         lowercase    :List[str]      	=						resnets

         if self.add_downsample:
           lowercase    :List[str]      	=						FlaxDownsampleaD(self.out_channels       ,					dtype=self.dtype				)






       def __call__(   self:					Optional[int]       ,					_lowerCAmelCase:					Tuple       ,					_lowerCAmelCase:					Dict       ,					_lowerCAmelCase:					Optional[int]=True				):
         lowercase    :Tuple      	=						()

         for resnet in self.resnets:
           lowercase    :str      	=						resnet(__A       ,					__A       ,					deterministic=__A				)
           output_states += (hidden_states,)

         if self.add_downsample:
           lowercase    :Optional[int]      	=						self.downsamplers_a(__A				)
           output_states += (hidden_states,)

         return hidden_states, output_states







class    __lowerCAmelCase       (							nn.Module):
       _a 					=						42
       _a 					=						42
       _a 					=						42
       _a 					=						0.0
       _a 					=						1
       _a 					=						1
       _a 					=						True
       _a 					=						False
       _a 					=						False
       _a 					=						False
       _a 					=						jnp.floataa






       def 					SCREAMING_SNAKE_CASE    (   self:					Any				):
         lowercase    :Optional[int]      	=						[]
         lowercase    :Any      	=						[]

         for i in range(self.num_layers				):
           lowercase    :Optional[Any]      	=						self.in_channels if (i == self.num_layers - 1) else self.out_channels
           lowercase    :int      	=						self.prev_output_channel if i == 0 else self.out_channels

           lowercase    :List[str]      	=						FlaxResnetBlockaD(
               in_channels=resnet_in_channels + res_skip_channels       ,					out_channels=self.out_channels       ,					dropout_prob=self.dropout       ,					dtype=self.dtype       ,					)
           resnets.append(__A				)

           lowercase    :int      	=						FlaxTransformeraDModel(
               in_channels=self.out_channels       ,					n_heads=self.num_attention_heads       ,					d_head=self.out_channels // self.num_attention_heads       ,					depth=1       ,					use_linear_projection=self.use_linear_projection       ,					only_cross_attention=self.only_cross_attention       ,					use_memory_efficient_attention=self.use_memory_efficient_attention       ,					dtype=self.dtype       ,					)
           attentions.append(__A				)

         lowercase    :str      	=						resnets
         lowercase    :Tuple      	=						attentions

         if self.add_upsample:
           lowercase    :Tuple      	=						FlaxUpsampleaD(self.out_channels       ,					dtype=self.dtype				)






       def __call__(   self:					Optional[Any]       ,					_lowerCAmelCase:					List[str]       ,					_lowerCAmelCase:					Dict       ,					_lowerCAmelCase:					List[str]       ,					_lowerCAmelCase:					List[Any]       ,					_lowerCAmelCase:					Dict=True				):
         for resnet, attn in zip(self.resnets       ,					self.attentions				):
           # pop res hidden states
           lowercase    :List[Any]      	=						res_hidden_states_tuple[-1]
           lowercase    :str      	=						res_hidden_states_tuple[:-1]
           lowercase    :Optional[Any]      	=						jnp.concatenate((hidden_states, res_hidden_states)       ,					axis=-1				)

           lowercase    :Any      	=						resnet(__A       ,					__A       ,					deterministic=__A				)
           lowercase    :List[Any]      	=						attn(__A       ,					__A       ,					deterministic=__A				)

         if self.add_upsample:
           lowercase    :List[str]      	=						self.upsamplers_a(__A				)

         return hidden_states







class    __lowerCAmelCase       (							nn.Module):
       _a 					=						42
       _a 					=						42
       _a 					=						42
       _a 					=						0.0
       _a 					=						1
       _a 					=						True
       _a 					=						jnp.floataa






       def 					SCREAMING_SNAKE_CASE    (   self:					Tuple				):
         lowercase    :Tuple      	=						[]

         for i in range(self.num_layers				):
           lowercase    :int      	=						self.in_channels if (i == self.num_layers - 1) else self.out_channels
           lowercase    :int      	=						self.prev_output_channel if i == 0 else self.out_channels

           lowercase    :Union[str, Any]      	=						FlaxResnetBlockaD(
               in_channels=resnet_in_channels + res_skip_channels       ,					out_channels=self.out_channels       ,					dropout_prob=self.dropout       ,					dtype=self.dtype       ,					)
           resnets.append(__A				)

         lowercase    :List[str]      	=						resnets

         if self.add_upsample:
           lowercase    :Tuple      	=						FlaxUpsampleaD(self.out_channels       ,					dtype=self.dtype				)






       def __call__(   self:					Union[str, Any]       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					Dict       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					Optional[int]=True				):
         for resnet in self.resnets:
           # pop res hidden states
           lowercase    :Dict      	=						res_hidden_states_tuple[-1]
           lowercase    :Optional[int]      	=						res_hidden_states_tuple[:-1]
           lowercase    :Tuple      	=						jnp.concatenate((hidden_states, res_hidden_states)       ,					axis=-1				)

           lowercase    :Dict      	=						resnet(__A       ,					__A       ,					deterministic=__A				)

         if self.add_upsample:
           lowercase    :List[Any]      	=						self.upsamplers_a(__A				)

         return hidden_states







class    __lowerCAmelCase       (							nn.Module):
       _a 					=						42
       _a 					=						0.0
       _a 					=						1
       _a 					=						1
       _a 					=						False
       _a 					=						False
       _a 					=						jnp.floataa






       def 					SCREAMING_SNAKE_CASE    (   self:					Tuple				):
         # there is always at least one resnet
         lowercase    :Any      	=						[
             FlaxResnetBlockaD(
                 in_channels=self.in_channels       ,					out_channels=self.in_channels       ,					dropout_prob=self.dropout       ,					dtype=self.dtype       ,					)
         ]

         lowercase    :Optional[Any]      	=						[]

         for _ in range(self.num_layers				):
           lowercase    :Tuple      	=						FlaxTransformeraDModel(
               in_channels=self.in_channels       ,					n_heads=self.num_attention_heads       ,					d_head=self.in_channels // self.num_attention_heads       ,					depth=1       ,					use_linear_projection=self.use_linear_projection       ,					use_memory_efficient_attention=self.use_memory_efficient_attention       ,					dtype=self.dtype       ,					)
           attentions.append(__A				)

           lowercase    :int      	=						FlaxResnetBlockaD(
               in_channels=self.in_channels       ,					out_channels=self.in_channels       ,					dropout_prob=self.dropout       ,					dtype=self.dtype       ,					)
           resnets.append(__A				)

         lowercase    :Dict      	=						resnets
         lowercase    :List[Any]      	=						attentions






       def __call__(   self:					List[str]       ,					_lowerCAmelCase:					str       ,					_lowerCAmelCase:					Union[str, Any]       ,					_lowerCAmelCase:					Union[str, Any]       ,					_lowerCAmelCase:					Optional[int]=True				):
         lowercase    :List[str]      	=						self.resnets[0](__A       ,					__A				)
         for attn, resnet in zip(self.attentions       ,					self.resnets[1:]				):
           lowercase    :Union[str, Any]      	=						attn(__A       ,					__A       ,					deterministic=__A				)
           lowercase    :Any      	=						resnet(__A       ,					__A       ,					deterministic=__A				)

         return hidden_states









def       UpperCAmelCase__  (     lowerCamelCase,			lowerCamelCase = False   ):
  if n == 2:
    return True
  if not n % 2 or n < 2:
    return False
  if n > 5 and n % 10 not in (1, 3, 7, 9):  # can quickly check last digit
    return False
  if n > 3317044064679887385961981 and not allow_probable:
    raise ValueError(
        "Warning: upper bound of deterministic test is exceeded. "
        "Pass allow_probable=True to allow probabilistic test. "
        "A return value of True indicates a probable prime."   )
  # array bounds provided by analysis
  lowercase    :Optional[Any]      	=						[
      2047,
      1373653,
      25326001,
      3215031751,
      2152302898747,
      3474749660383,
      341550071728321,
      1,
      3825123056546413051,
      1,
      1,
      318665857834031151167461,
      3317044064679887385961981,
  ]

  lowercase    :List[Any]      	=						[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]
  for idx, _p in enumerate(lowerCamelCase,			1   ):
    if n < _p:
      # then we have our last prime to check
      lowercase    :Tuple      	=						primes[:idx]
      break
  lowercase	,    lowercase    :str      	=						n - 1, 0
  # break up n -1 into a power of 2 (s) and
  # remaining odd component
  # essentially, solve for d * 2 ** s == n - 1
  while d % 2 == 0:
    d //= 2
    s += 1
  for prime in plist:
    lowercase    :Tuple      	=						False
    for r in range(lowerCamelCase   ):
      lowercase    :Any      	=						pow(lowerCamelCase,			d * 2**r,			lowerCamelCase   )
      # see article for analysis explanation for m
      if (r == 0 and m == 1) or ((m + 1) % n == 0):
        lowercase    :Any      	=						True
        # this loop will not determine compositeness
        break
    if pr:
      continue
    # if pr is False, then the above loop never evaluated to true,
    # and the n MUST be composite
    return False
  return True



def       UpperCAmelCase__  (     ):
  assert not miller_rabin(561   )
  assert miller_rabin(563   )
  # 2047

  assert not miller_rabin(838201   )
  assert miller_rabin(838207   )
  # 1_373_653

  assert not miller_rabin(17316001   )
  assert miller_rabin(17316017   )
  # 25_326_001

  assert not miller_rabin(3078386641   )
  assert miller_rabin(3078386653   )
  # 3_215_031_751

  assert not miller_rabin(1713045574801   )
  assert miller_rabin(1713045574819   )
  # 2_152_302_898_747

  assert not miller_rabin(2779799728307   )
  assert miller_rabin(2779799728327   )
  # 3_474_749_660_383

  assert not miller_rabin(113850023909441   )
  assert miller_rabin(113850023909527   )
  # 341_550_071_728_321

  assert not miller_rabin(1275041018848804351   )
  assert miller_rabin(1275041018848804391   )
  # 3_825_123_056_546_413_051

  assert not miller_rabin(79666464458507787791867   )
  assert miller_rabin(79666464458507787791951   )
  # 318_665_857_834_031_151_167_461

  assert not miller_rabin(552840677446647897660333   )
  assert miller_rabin(552840677446647897660359   )
  # 3_317_044_064_679_887_385_961_981
  # upper limit for probabilistic test


if __name__ == "__main__":
     test_miller_rabin()



