


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

# For specifying empty leaf dict `{}`
snake_case    :   int	    = object()
def     __lowerCamelCase						(			UpperCAmelCase_						:		Tuple			,  UpperCAmelCase_						:		List[str]						):




      """simple docstring"""





      a						:Optional[Any]										=	tuple((re.compile(x + '''$'''						) for x in qs)						)
      for i in range(len(UpperCAmelCase__						) - len(UpperCAmelCase__						) + 1						):
            a						:int										=	[x.match(UpperCAmelCase__						) for x, y in zip(UpperCAmelCase__			,  ks[i:]						)]
            if matches and all(UpperCAmelCase__						):
                  return True
      return False
def     __lowerCamelCase						(			UpperCAmelCase_						:		Optional[int]						):




      """simple docstring"""





      def replace(UpperCAmelCase_						:		Any			,  UpperCAmelCase_						:		List[Any]						):
            for rule, replacement in rules:
                  if _match(UpperCAmelCase__			,  UpperCAmelCase__						):
                        return replacement
            return val

      return replace
def     __lowerCamelCase						(			):




      """simple docstring"""





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




      """simple docstring"""





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









from ...utils import (
    OptionalDependencyNotAvailable,
    is_flax_available,
    is_torch_available,
    is_transformers_available,
)


try:
     if not (is_transformers_available() and is_torch_available()):
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     from ...utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
     from .multicontrolnet import MultiControlNetModel
     from .pipeline_controlnet import StableDiffusionControlNetPipeline
     from .pipeline_controlnet_imgaimg import StableDiffusionControlNetImgaImgPipeline
     from .pipeline_controlnet_inpaint import StableDiffusionControlNetInpaintPipeline


if is_transformers_available() and is_flax_available():
     from .pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline





