



'''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__:   Optional[Any]								=			object()

# For specifying empty leaf dict `{}`
UpperCamelCase__:   Optional[int]								=			object()


def  snake_case_     (					_lowerCAmelCase  :						str   ,      _lowerCAmelCase  :						Optional[Any]  )      ->   Tuple:
							UpperCAmelCase :       Union[str, Any] 							=		tuple((re.compile(x + '''$'''  ) for x in qs)  )
							for i in range(len(_lowerCAmelCase  ) - len(_lowerCAmelCase  ) + 1  ):
														UpperCAmelCase :       Union[str, Any] 							=		[x.match(_lowerCAmelCase  ) for x, y in zip(_lowerCAmelCase   ,      ks[i:]  )]
														if matches and all(_lowerCAmelCase  ):
																					return True
							return False


def  snake_case_     (					_lowerCAmelCase  :						Optional[int]  )      ->   List[Any]:
							def replace(_lowerCAmelCase  :						Tuple   ,      _lowerCAmelCase  :						Optional[Any]  ):
														for rule, replacement in rules:
																					if _match(_lowerCAmelCase   ,      _lowerCAmelCase  ):
																												return replacement
														return val

							return replace


def  snake_case_     (					)      ->   List[str]:
							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  snake_case_     (					_lowerCAmelCase  :						Union[str, Any]  )      ->   Optional[Any]:
							UpperCAmelCase :       List[str] 							=		_get_partition_rules()
							UpperCAmelCase :       Dict 							=		_replacement_rules(_lowerCAmelCase  )
							UpperCAmelCase :       Dict 							=		{k: _unmatched for k in flatten_dict(_lowerCAmelCase  )}
							UpperCAmelCase :       Any 							=		{k: replace(_lowerCAmelCase   ,      _lowerCAmelCase  ) for k, v in initd.items()}
							assert _unmatched not in result.values(), "Incomplete partition spec."
							return freeze(unflatten_dict(_lowerCAmelCase  )  )










'''simple docstring'''






from __future__ import annotations


def  snake_case_     (					_lowerCAmelCase  :						list[int | float]   ,      _lowerCAmelCase  :						int   ,      _lowerCAmelCase  :						int  )      ->   int | float:
							if len(_lowerCAmelCase  ) == 0:
														raise ValueError('''find_max() arg is an empty sequence'''  )
							if (
							    left >= len(_lowerCAmelCase  )
							    or left < -len(_lowerCAmelCase  )
							    or right >= len(_lowerCAmelCase  )
							    or right < -len(_lowerCAmelCase  )
							):
														raise IndexError('''list index out of range'''  )
							if left == right:
														return nums[left]
							UpperCAmelCase :       List[Any] 							=		(left + right) >> 1  # the middle
							UpperCAmelCase :       Optional[Any] 							=		find_max(_lowerCAmelCase   ,      _lowerCAmelCase   ,      _lowerCAmelCase  )  # find max in range[left, mid]
							UpperCAmelCase :       Dict 							=		find_max(_lowerCAmelCase   ,      mid + 1   ,      _lowerCAmelCase  )  # find max in range[mid + 1, right]

							return left_max if left_max >= right_max else right_max


if __name__ == "__main__":
						import doctest

						doctest.testmod(verbose=True)





