


'''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
lowerCAmelCase    :     Union[str, Any]					=						object()

# For specifying empty leaf dict `{}`
lowerCAmelCase    :     Any					=						object()






def 			lowercase				(_A   ,						_A					):







       """simple docstring"""







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






def 			lowercase				(_A					):







       """simple docstring"""







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

       return replace






def 			lowercase				():







       """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 			lowercase				(_A					):







       """simple docstring"""







       _lowerCAmelCase    :							Any   			=       _get_partition_rules()
       _lowerCAmelCase    :							Tuple   			=       _replacement_rules(_A					)
       _lowerCAmelCase    :							Tuple   			=       {k: _unmatched for k in flatten_dict(_A					)}
       _lowerCAmelCase    :							List[Any]   			=       {k: replace(_A   ,						_A					) for k, v in initd.items()}
       assert _unmatched not in result.values(), "Incomplete partition spec."
       return freeze(unflatten_dict(_A					)					)





'''simple docstring'''







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_speech_available,
    is_torch_available,
)


lowerCAmelCase    :     int					=						{
    """configuration_trocr""": ["""TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TrOCRConfig"""],
    """processing_trocr""": ["""TrOCRProcessor"""],
}


try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       lowerCAmelCase    :     Optional[Any]					=						[
           """TROCR_PRETRAINED_MODEL_ARCHIVE_LIST""",
           """TrOCRForCausalLM""",
           """TrOCRPreTrainedModel""",
       ]


if TYPE_CHECKING:
       from .configuration_trocr import TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP, TrOCRConfig
       from .processing_trocr import TrOCRProcessor

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_trocr import TROCR_PRETRAINED_MODEL_ARCHIVE_LIST, TrOCRForCausalLM, TrOCRPreTrainedModel

else:
       import sys

       lowerCAmelCase    :     Any					=						_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

