


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

# For specifying empty leaf dict `{}`
UpperCAmelCase					:  List[str]			=						object()






def      __lowerCamelCase		(				lowerCamelCase__						:		List[Any]   ,				lowerCamelCase__						:		Optional[int]	):





			'''simple docstring'''




			lowerCamelCase  						=   tuple((re.compile(x + """$"""	) for x in qs)	)
			for i in range(len(__snake_case	) - len(__snake_case	) + 1	):
						lowerCamelCase  						=   [x.match(__snake_case	) for x, y in zip(__snake_case   ,				ks[i:]	)]
						if matches and all(__snake_case	):
									return True
			return False






def      __lowerCamelCase		(				lowerCamelCase__						:		Optional[Any]	):





			'''simple docstring'''




			def replace(lowerCamelCase__						:		List[Any]   ,				lowerCamelCase__						:		Optional[Any]	):
						for rule, replacement in rules:
									if _match(__snake_case   ,				__snake_case	):
												return replacement
						return val

			return replace






def      __lowerCamelCase		(				):





			'''simple docstring'''




			return [
			    # embeddings
			    (("transformer", "wpe", "embedding"), P("""mp"""   ,				__snake_case	)),
			    (("transformer", "wte", "embedding"), P("""mp"""   ,				__snake_case	)),
			    # atention
			    (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(__snake_case   ,				"""mp"""	)),
			    (("attention", "out_proj", "kernel"), P("""mp"""   ,				__snake_case	)),
			    (("attention", "out_proj", "bias"), None),
			    # mlp
			    (("mlp", "c_fc", "kernel"), P(__snake_case   ,				"""mp"""	)),
			    (("mlp", "c_fc", "bias"), P("""mp"""	)),
			    (("mlp", "c_proj", "kernel"), P("""mp"""   ,				__snake_case	)),
			    (("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		(				lowerCamelCase__						:		List[Any]	):





			'''simple docstring'''




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









def      lowercase__      (     __snake_case    :							int     ,       __snake_case    :							int  ):


					'''simple docstring'''


					if a < 0 or b < 0:
										raise ValueError('the value of both inputs must be positive'  )

					UpperCAmelCase_    :      Tuple            =						str(bin(__snake_case  )  )[2:]  # remove the leading "0b"
					UpperCAmelCase_    :      Union[str, Any]            =						str(bin(__snake_case  )  )[2:]  # remove the leading "0b"

					UpperCAmelCase_    :      List[Any]            =						max(len(__snake_case  )     ,       len(__snake_case  )  )

					return "0b" + "".join(
					    str(int(char_a == '1' and char_b == '1'  )  )
					    for char_a, char_b in zip(a_binary.zfill(__snake_case  )     ,       b_binary.zfill(__snake_case  )  )  )


if __name__ == "__main__":
		import doctest

		doctest.testmod()






