



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

# For specifying empty leaf dict `{}`
__lowerCamelCase         =			object()







def    _snake_case (       __snake_case					,  __snake_case    )     ->     List[str]:






      '''simple docstring'''







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







def    _snake_case (       __snake_case    )     ->     Optional[int]:






      '''simple docstring'''







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

      return replace







def    _snake_case (       )     ->     Dict:






      '''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    _snake_case (       __snake_case    )     ->     Any:






      '''simple docstring'''







      UpperCAmelCase_  :	Optional[Any]				      =		_get_partition_rules()
      UpperCAmelCase_  :	Dict				      =		_replacement_rules(__UpperCamelCase    )
      UpperCAmelCase_  :	Optional[int]				      =		{k: _unmatched for k in flatten_dict(__UpperCamelCase    )}
      UpperCAmelCase_  :	Tuple				      =		{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 abc import ABC, abstractmethod
from typing import Optional, Union

from .. import Dataset, DatasetDict, Features, IterableDataset, IterableDatasetDict, NamedSplit
from ..utils.typing import NestedDataStructureLike, PathLike


class   snake_case_ (lowercase__  ):




       """simple docstring"""






       def __init__(     self     ,lowercase = None     ,lowercase = None     ,lowercase = None     ,lowercase = None     ,lowercase = False     ,lowercase = False     ,lowercase = None     ,**lowercase     ,):

             """simple docstring"""





             UpperCAmelCase_  :	Optional[Any]				      =		path_or_paths
             UpperCAmelCase_  :	Optional[int]				      =		split if split or isinstance(lowercase     ,lowercase) else "train"
             UpperCAmelCase_  :	Optional[int]				      =		features
             UpperCAmelCase_  :	Dict				      =		cache_dir
             UpperCAmelCase_  :	int				      =		keep_in_memory
             UpperCAmelCase_  :	Tuple				      =		streaming
             UpperCAmelCase_  :	Union[str, Any]				      =		num_proc
             UpperCAmelCase_  :	Union[str, Any]				      =		kwargs



       @abstractmethod
       def  A_   (     self):

             """simple docstring"""





             pass


class   snake_case_ (lowercase__  ):




       """simple docstring"""






       def __init__(     self     ,lowercase = None     ,lowercase = None     ,lowercase = False     ,lowercase = False     ,lowercase = None     ,**lowercase     ,):

             """simple docstring"""





             UpperCAmelCase_  :	List[Any]				      =		features
             UpperCAmelCase_  :	List[Any]				      =		cache_dir
             UpperCAmelCase_  :	List[str]				      =		keep_in_memory
             UpperCAmelCase_  :	Optional[int]				      =		streaming
             UpperCAmelCase_  :	Dict				      =		num_proc
             UpperCAmelCase_  :	str				      =		kwargs



       @abstractmethod
       def  A_   (     self):

             """simple docstring"""





             pass
