

"""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			=					object()

# For specifying empty leaf dict `{}`
__UpperCAmelCase			=					object()
def 	lowerCAmelCase    (       __UpperCamelCase				,     __UpperCamelCase   ):
 '''simple docstring'''





 UpperCAmelCase__       :	Any        =       tuple((re.compile(x + """$"""   ) for x in qs)   )
 for i in range(len(__a   ) - len(__a   ) + 1   ):
  UpperCAmelCase__       :	Tuple        =       [x.match(__a   ) for x, y in zip(__a				,     ks[i:]   )]
  if matches and all(__a   ):
   return True
 return False
def 	lowerCAmelCase    (       __UpperCamelCase   ):
 '''simple docstring'''





 def replace(__UpperCamelCase				,     __UpperCamelCase   ):
  for rule, replacement in rules:
   if _match(__a				,     __a   ):
    return replacement
  return val

 return replace
def 	lowerCAmelCase    (       ):
 '''simple docstring'''





 return [
     # embeddings
     (("transformer", "wpe", "embedding"), P("""mp"""				,     __a   )),
     (("transformer", "wte", "embedding"), P("""mp"""				,     __a   )),
     # atention
     (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(__a				,     """mp"""   )),
     (("attention", "out_proj", "kernel"), P("""mp"""				,     __a   )),
     (("attention", "out_proj", "bias"), None),
     # mlp
     (("mlp", "c_fc", "kernel"), P(__a				,     """mp"""   )),
     (("mlp", "c_fc", "bias"), P("""mp"""   )),
     (("mlp", "c_proj", "kernel"), P("""mp"""				,     __a   )),
     (("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 	lowerCAmelCase    (       __UpperCamelCase   ):
 '''simple docstring'''





 UpperCAmelCase__       :	int        =       _get_partition_rules()
 UpperCAmelCase__       :	Optional[int]        =       _replacement_rules(__a   )
 UpperCAmelCase__       :	Optional[int]        =       {k: _unmatched for k in flatten_dict(__a   )}
 UpperCAmelCase__       :	Dict        =       {k: replace(__a				,     __a   ) for k, v in initd.items()}
 assert _unmatched not in result.values(), "Incomplete partition spec."
 return freeze(unflatten_dict(__a   )   )









from importlib import import_module

from .logging import get_logger


_lowerCAmelCase:						str   				=       get_logger(__name__)




class       lowercase_					:





				def __init__(				self       ,  lowercase_       ,  lowercase_=None)  -> Tuple:
											a__      =attrs or []
											if module is not None:
																		for key in module.__dict__:
																									if key in attrs or not key.startswith('__'):
																																setattr(self       ,  lowercase_       ,  getattr(lowercase_       ,  lowercase_))
											a__      =module._original_module if isinstance(lowercase_       ,  _PatchedModuleObj) else module







class       lowercase_					:
				snake_case	    =[]





				def __init__(				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_=None)  -> List[str]:
											a__      =obj
											a__      =target
											a__      =new
											a__      =target.split('.')[0]
											a__      ={}
											a__      =attrs or []





				def __enter__(				self)  -> Optional[int]:
											*a__    ,     a__      =self.target.split('.')

											# Patch modules:
											# it's used to patch attributes of submodules like "os.path.join";
											# in this case we need to patch "os" and "os.path"

											for i in range(len(lowercase_)):
																		try:
																									a__      =import_module('.'.join(submodules[: i + 1]))
																		except ModuleNotFoundError:
																									continue
																		# We iterate over all the globals in self.obj in case we find "os" or "os.path"
																		for attr in self.obj.__dir__():
																									a__      =getattr(self.obj       ,  lowercase_)
																									# We don't check for the name of the global, but rather if its value *is* "os" or "os.path".
																									# This allows to patch renamed modules like "from os import path as ospath".
																									if obj_attr is submodule or (
																									    (isinstance(lowercase_       ,  _PatchedModuleObj) and obj_attr._original_module is submodule)
																									):
																																a__      =obj_attr
																																# patch at top level
																																setattr(self.obj       ,  lowercase_       ,  _PatchedModuleObj(lowercase_       ,  attrs=self.attrs))
																																a__      =getattr(self.obj       ,  lowercase_)
																																# construct lower levels patches
																																for key in submodules[i + 1 :]:
																																							setattr(lowercase_       ,  lowercase_       ,  _PatchedModuleObj(getattr(lowercase_       ,  lowercase_       ,  lowercase_)       ,  attrs=self.attrs))
																																							a__      =getattr(lowercase_       ,  lowercase_)
																																# finally set the target attribute
																																setattr(lowercase_       ,  lowercase_       ,  self.new)

        # Patch attribute itself:
        # it's used for builtins like "open",
        # and also to patch "os.path.join" we may also need to patch "join"
        # itself if it was imported as "from os.path import join".

											if submodules:  # if it's an attribute of a submodule like "os.path.join"
																		try:
																									a__      =getattr(import_module('.'.join(lowercase_))       ,  lowercase_)
																		except (AttributeError, ModuleNotFoundError):
																									return
																		# We iterate over all the globals in self.obj in case we find "os.path.join"
																		for attr in self.obj.__dir__():
																									# We don't check for the name of the global, but rather if its value *is* "os.path.join".
																									# This allows to patch renamed attributes like "from os.path import join as pjoin".
																									if getattr(self.obj       ,  lowercase_) is attr_value:
																																a__      =getattr(self.obj       ,  lowercase_)
																																setattr(self.obj       ,  lowercase_       ,  self.new)
											elif target_attr in globals()["__builtins__"]:  # if it'a s builtin like "open"
																		a__      =globals()['__builtins__'][target_attr]
																		setattr(self.obj       ,  lowercase_       ,  self.new)
											else:
																		raise RuntimeError(F"""Tried to patch attribute {target_attr} instead of a submodule.""")





				def __exit__(				self       ,  *lowercase_)  -> str:
											for attr in list(self.original):
																		setattr(self.obj       ,  lowercase_       ,  self.original.pop(lowercase_))





				def 						__UpperCamelCase    (				self)  -> Any:
											self.__enter__()
											self._active_patches.append(self)





				def 						__UpperCamelCase    (				self)  -> Union[str, Any]:
											try:
																		self._active_patches.remove(self)
											except ValueError:
																		# If the patch hasn't been started this will fail
																		return None

											return self.__exit__()

