



"""simple docstring"""




import re

from flax.core.frozen_dict import freeze
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.experimental import PartitionSpec as P


# Sentinels
UpperCAmelCase_							:      Union[str, Any]      =  object()

# For specifying empty leaf dict `{}`
UpperCAmelCase_							:      Union[str, Any]      =  object()
def  UpperCamelCase						(       _A			:				Optional[int]			,							_A			:				Dict    )->       List[str]:





						"""simple docstring"""





						A__							=       tuple((re.compile(x + "$"    ) for x in qs)    )
						for i in range(len(lowerCamelCase__    ) - len(lowerCamelCase__    ) + 1    ):
												A__							=       [x.match(lowerCamelCase__    ) for x, y in zip(lowerCamelCase__			,							ks[i:]    )]
												if matches and all(lowerCamelCase__    ):
																		return True
						return False
def  UpperCamelCase						(       _A			:				List[str]    )->       str:





						"""simple docstring"""





						def replace(_A			:				Dict			,							_A			:				Any    ):
												for rule, replacement in rules:
																		if _match(lowerCamelCase__			,							lowerCamelCase__    ):
																								return replacement
												return val

						return replace
def  UpperCamelCase						(       )->       str:





						"""simple docstring"""





						return [
						    # embeddings
						    (("transformer", "wpe", "embedding"), P("mp"			,							lowerCamelCase__    )),
						    (("transformer", "wte", "embedding"), P("mp"			,							lowerCamelCase__    )),
						    # atention
						    (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(lowerCamelCase__			,							"mp"    )),
						    (("attention", "out_proj", "kernel"), P("mp"			,							lowerCamelCase__    )),
						    (("attention", "out_proj", "bias"), None),
						    # mlp
						    (("mlp", "c_fc", "kernel"), P(lowerCamelCase__			,							"mp"    )),
						    (("mlp", "c_fc", "bias"), P("mp"    )),
						    (("mlp", "c_proj", "kernel"), P("mp"			,							lowerCamelCase__    )),
						    (("mlp", "c_proj", "bias"), None),
						    # layer norms
						    ((r"ln_\d+", "bias"), None),
						    ((r"\d+", r"ln_\d+", "scale"), None),
						    (("ln_f", "bias"), None),
						    (("ln_f", "scale"), None),
						]
def  UpperCamelCase						(       _A			:				Union[str, Any]    )->       Any:





						"""simple docstring"""





						A__							=       _get_partition_rules()
						A__							=       _replacement_rules(lowerCamelCase__    )
						A__							=       {k: _unmatched for k in flatten_dict(lowerCamelCase__    )}
						A__							=       {k: replace(lowerCamelCase__			,							lowerCamelCase__    ) for k, v in initd.items()}
						assert _unmatched not in result.values(), "Incomplete partition spec."
						return freeze(unflatten_dict(lowerCamelCase__    )    )










from .dependency_versions_table import deps
from .utils.versions import require_version, require_version_core


# define which module versions we always want to check at run time
# (usually the ones defined in `install_requires` in setup.py)
#
# order specific notes:
# - tqdm must be checked before tokenizers

UpperCAmelCase_							:      List[Any]      =  [
    "python",
    "tqdm",
    "regex",
    "requests",
    "packaging",
    "filelock",
    "numpy",
    "tokenizers",
    "huggingface-hub",
    "safetensors",
    "accelerate",
    "pyyaml",
]

for pkg in pkgs_to_check_at_runtime:
						if pkg in deps:
												if pkg == "tokenizers":
																		# must be loaded here, or else tqdm check may fail
																		from .utils import is_tokenizers_available

																		if not is_tokenizers_available():
																								continue  # not required, check version only if installed
												elif pkg == "accelerate":
																		# must be loaded here, or else tqdm check may fail
																		from .utils import is_accelerate_available

																		# Maybe switch to is_torch_available in the future here so that Accelerate is hard dep of
																		# Transformers with PyTorch
																		if not is_accelerate_available():
																								continue  # not required, check version only if installed

												require_version_core(deps[pkg])
						else:
												raise ValueError(F'''can\'t find {pkg} in {deps.keys()}, check dependency_versions_table.py''')
def  UpperCamelCase						(       _A			:				List[Any]			,							_A			:				int=None    )->       Optional[int]:





						"""simple docstring"""





						require_version(deps[pkg]			,							_A    )





