


import re

import jax.numpy as jnp
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.random import PRNGKey

from ..utils import logging


UpperCAmelCase__   	=							logging.get_logger(__name__)
def 				UpperCAmelCase_					(							__snake_case						)     ->			int:







 """simple docstring"""


 _lowercase							      =R"\w+[.]\d+"
 _lowercase							      =re.findall(__a     ,						__a						)
 for pat in pats:
  _lowercase							      =key.replace(__a     ,						'''_'''.join(pat.split('''.'''						)						)						)
 return key
def 				UpperCAmelCase_					(							__snake_case     ,						__snake_case     ,						__snake_case						)     ->			Any:







 """simple docstring"""


 _lowercase							      =pt_tuple_key[:-1] + ("scale",)
 if (
     any('''norm''' in str_ for str_ in pt_tuple_key						)
     and (pt_tuple_key[-1] == "bias")
     and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict)
     and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict)
 ):
  _lowercase							      =pt_tuple_key[:-1] + ("scale",)
  return renamed_pt_tuple_key, pt_tensor
 elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict:
  _lowercase							      =pt_tuple_key[:-1] + ("scale",)
  return renamed_pt_tuple_key, pt_tensor

 # embedding
 if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict:
  _lowercase							      =pt_tuple_key[:-1] + ("embedding",)
  return renamed_pt_tuple_key, pt_tensor

 # conv layer
 _lowercase							      =pt_tuple_key[:-1] + ("kernel",)
 if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4:
  _lowercase							      =pt_tensor.transpose(2     ,						3     ,						1     ,						0						)
  return renamed_pt_tuple_key, pt_tensor

 # linear layer
 _lowercase							      =pt_tuple_key[:-1] + ("kernel",)
 if pt_tuple_key[-1] == "weight":
  _lowercase							      =pt_tensor.T
  return renamed_pt_tuple_key, pt_tensor

 # old PyTorch layer norm weight
 _lowercase							      =pt_tuple_key[:-1] + ("weight",)
 if pt_tuple_key[-1] == "gamma":
  return renamed_pt_tuple_key, pt_tensor

 # old PyTorch layer norm bias
 _lowercase							      =pt_tuple_key[:-1] + ("bias",)
 if pt_tuple_key[-1] == "beta":
  return renamed_pt_tuple_key, pt_tensor

 return pt_tuple_key, pt_tensor



def 				UpperCAmelCase_					(							__snake_case     ,						__snake_case     ,						__snake_case=42						)     ->			Tuple:







 """simple docstring"""


 _lowercase							      ={k: v.numpy() for k, v in pt_state_dict.items()}

 # Step 2: Since the model is stateless, get random Flax params
 _lowercase							      =flax_model.init_weights(PRNGKey(__a						)						)

 _lowercase							      =flatten_dict(__a						)
 _lowercase							      ={}

 # Need to change some parameters name to match Flax names
 for pt_key, pt_tensor in pt_state_dict.items():
  _lowercase							      =rename_key(__a						)
  _lowercase							      =tuple(renamed_pt_key.split('''.'''						)						)

  # Correctly rename weight parameters
  _lowercase							      =rename_key_and_reshape_tensor(__a     ,						__a     ,						__a						)

  if flax_key in random_flax_state_dict:
   if flax_tensor.shape != random_flax_state_dict[flax_key].shape:
    raise ValueError(
        F"PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape "
        F"{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}."						)

    # also add unexpected weight so that warning is thrown
  _lowercase							      =jnp.asarray(__a						)

 return unflatten_dict(__a						)








from math import ceil, sqrt





def      lowerCAmelCase_ (       __a = 1000000		)     ->		int:







  """simple docstring"""





  lowerCamelCase__:      Optional[int]			  =0

  for outer_width in range(3	,    (limit // 4) + 2		):
    if outer_width**2 > limit:
      lowerCamelCase__:      Dict			  =max(ceil(sqrt(outer_width**2 - limit		)		)	,    1		)
    else:
      lowerCamelCase__:      str			  =1
    if (outer_width - hole_width_lower_bound) % 2:
      hole_width_lower_bound += 1

    answer += (outer_width - hole_width_lower_bound - 2) // 2 + 1

  return answer


if __name__ == "__main__":
     print(f'{solution() = }')

