





"""simple docstring"""



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


a_			=      logging.get_logger(__name__)







def 		__lowercase					(       snake_case_  :  int						)       ->str:



    '''simple docstring'''







    __A   :						Union[str, Any]									=					r'''\w+[.]\d+'''
    __A   :						List[Any]									=					re.findall(snake_case_    ,snake_case_						)
    for pat in pats:
        __A   :						Dict									=					key.replace(snake_case_    ,'''_'''.join(pat.split('''.'''						)						)						)
    return key







def 		__lowercase					(       snake_case_  :  List[Any]    ,snake_case_  :  List[Any]    ,snake_case_  :  List[Any]						)       ->Optional[Any]:



    '''simple docstring'''







    __A   :						List[Any]									=					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)
    ):
        __A   :						Optional[int]									=					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:
        __A   :						Any									=					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:
        __A   :						Tuple									=					pt_tuple_key[:-1] + ('''embedding''',)
        return renamed_pt_tuple_key, pt_tensor

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

    # linear layer
    __A   :						List[Any]									=					pt_tuple_key[:-1] + ('''kernel''',)
    if pt_tuple_key[-1] == "weight":
        __A   :						Dict									=					pt_tensor.T
        return renamed_pt_tuple_key, pt_tensor

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

    # old PyTorch layer norm bias
    __A   :						Optional[Any]									=					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 		__lowercase					(       snake_case_  :  Optional[int]    ,snake_case_  :  int    ,snake_case_  :  Union[str, Any]=42						)       ->Optional[Any]:



    '''simple docstring'''







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

    # Step 2: Since the model is stateless, get random Flax params
    __A   :						List[Any]									=					flax_model.init_weights(PRNGKey(snake_case_						)						)

    __A   :						Dict									=					flatten_dict(snake_case_						)
    __A   :						int									=					{}

    # Need to change some parameters name to match Flax names
    for pt_key, pt_tensor in pt_state_dict.items():
        __A   :						List[str]									=					rename_key(snake_case_						)
        __A   :						Dict									=					tuple(renamed_pt_key.split('''.'''						)						)

        # Correctly rename weight parameters
        __A			,	__A   :						Optional[int]									=					rename_key_and_reshape_tensor(snake_case_    ,snake_case_    ,snake_case_						)

        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
        __A   :						Any									=					jnp.asarray(snake_case_						)

    return unflatten_dict(snake_case_						)











"""simple docstring"""



from math import factorial







def 		__lowercase					(       snake_case_  :  int    ,snake_case_  :  int						)       ->int:



    '''simple docstring'''







    if n < k or k < 0:
        raise ValueError('''Please enter positive integers for n and k where n >= k'''						)
    return factorial(snake_case_						) // (factorial(snake_case_						) * factorial(n - k						))


if __name__ == "__main__":
       print(
           """The number of five-card hands possible from a standard""",
           f'''fifty-two card deck is: {combinations(52, 5)}\n''',
       )

       print(
           """If a class of 40 students must be arranged into groups of""",
           f'''4 for group projects, there are {combinations(40, 4)} ways''',
           """to arrange them.\n""",
       )

       print(
           """If 10 teams are competing in a Formula One race, there""",
           f'''are {combinations(10, 3)} ways that first, second and''',
           """third place can be awarded.""",
       )




