





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     _UpperCamelCase     (a__   :Tuple   ,			a__   :Optional[Any]       ):






							"""simple docstring"""





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





def     _UpperCamelCase     (a__   :Optional[Any]       ):






							"""simple docstring"""





							def replace(a__   :Dict   ,			a__   :Union[str, Any]       ):
														for rule, replacement in rules:
																					if _match(a__   ,			a__       ):
																												return replacement
														return val

							return replace





def     _UpperCamelCase     ():






							"""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     _UpperCamelCase     (a__   :Optional[Any]       ):






							"""simple docstring"""





							UpperCamelCase__				     =			_get_partition_rules()
							UpperCamelCase__				     =			_replacement_rules(a__       )
							UpperCamelCase__				     =			{k: _unmatched for k in flatten_dict(a__       )}
							UpperCamelCase__				     =			{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 argparse import ArgumentParser

from .env import EnvironmentCommand





def     _UpperCamelCase     ():






							"""simple docstring"""





							UpperCamelCase__				     =			ArgumentParser("""Diffusers CLI tool"""   ,			usage="""diffusers-cli <command> [<args>]"""       )
							UpperCamelCase__				     =			parser.add_subparsers(help="""diffusers-cli command helpers"""       )

							# Register commands
							EnvironmentCommand.register_subcommand(a__       )

							# Let's go
							UpperCamelCase__				     =			parser.parse_args()

							if not hasattr(a__   ,			"""func"""       ):
														parser.print_help()
														exit(1       )

							# Run
							UpperCamelCase__				     =			args.func(a__       )
							service.run()


if __name__ == "__main__":
					main()




