


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

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





def __UpperCAmelCase							(		__A			,    __A      )    ->       Dict:



       '''simple docstring'''





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





def __UpperCAmelCase							(		__A      )    ->       Union[str, Any]:



       '''simple docstring'''





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

       return replace





def __UpperCAmelCase							(		)    ->       str:



       '''simple docstring'''





       return [
           # embeddings
           (("transformer", "wpe", "embedding"), P("mp"			,    SCREAMING_SNAKE_CASE__      )),
           (("transformer", "wte", "embedding"), P("mp"			,    SCREAMING_SNAKE_CASE__      )),
           # atention
           (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(SCREAMING_SNAKE_CASE__			,    "mp"      )),
           (("attention", "out_proj", "kernel"), P("mp"			,    SCREAMING_SNAKE_CASE__      )),
           (("attention", "out_proj", "bias"), None),
           # mlp
           (("mlp", "c_fc", "kernel"), P(SCREAMING_SNAKE_CASE__			,    "mp"      )),
           (("mlp", "c_fc", "bias"), P("mp"      )),
           (("mlp", "c_proj", "kernel"), P("mp"			,    SCREAMING_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 __UpperCAmelCase							(		__A      )    ->       Optional[int]:



       '''simple docstring'''





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













'''simple docstring'''



__UpperCamelCase   :							List[Any]         =   """ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"""



def  __UpperCAmelCase						(					SCREAMING_SNAKE_CASE__:						bytes     )	->      bytes:

   """simple docstring"""
   # Make sure the supplied data is a bytes-like object
   if not isinstance(SCREAMING_SNAKE_CASE__,       SCREAMING_SNAKE_CASE__     ):
      __a					  =     f"""a bytes-like object is required, not '{data.__class__.__name__}'"""
      raise TypeError(SCREAMING_SNAKE_CASE__     )

   __a					  =     ''.join(bin(SCREAMING_SNAKE_CASE__     )[2:].zfill(8     ) for byte in data     )

   __a					  =     len(SCREAMING_SNAKE_CASE__     ) % 6 != 0

   if padding_needed:
      # The padding that will be added later
      __a					  =     b'=' * ((6 - len(SCREAMING_SNAKE_CASE__     ) % 6) // 2)

      # Append binary_stream with arbitrary binary digits (0's by default) to make its
      # length a multiple of 6.
      binary_stream += "0" * (6 - len(SCREAMING_SNAKE_CASE__     ) % 6)
   else:
      __a					  =     b''

   # Encode every 6 binary digits to their corresponding Base64 character
   return (
       "".join(
           B64_CHARSET[int(binary_stream[index : index + 6],       2     )]
           for index in range(0,       len(SCREAMING_SNAKE_CASE__     ),       6     )     ).encode()
       + padding
   )



def  __UpperCAmelCase						(					SCREAMING_SNAKE_CASE__:						str     )	->      bytes:

   """simple docstring"""
   # Make sure encoded_data is either a string or a bytes-like object
   if not isinstance(SCREAMING_SNAKE_CASE__,       SCREAMING_SNAKE_CASE__     ) and not isinstance(SCREAMING_SNAKE_CASE__,       SCREAMING_SNAKE_CASE__     ):
      __a					  =     (
          'argument should be a bytes-like object or ASCII string, '
          f"""not '{encoded_data.__class__.__name__}'"""
      )
      raise TypeError(SCREAMING_SNAKE_CASE__     )

   # In case encoded_data is a bytes-like object, make sure it contains only
   # ASCII characters so we convert it to a string object
   if isinstance(SCREAMING_SNAKE_CASE__,       SCREAMING_SNAKE_CASE__     ):
      try:
         __a					  =     encoded_data.decode('utf-8'     )
      except UnicodeDecodeError:
         raise ValueError('base64 encoded data should only contain ASCII characters'     )

   __a					  =     encoded_data.count('='     )

   # Check if the encoded string contains non base64 characters
   if padding:
      assert all(
          char in B64_CHARSET for char in encoded_data[:-padding]     ), "Invalid base64 character(s) found."
   else:
      assert all(
          char in B64_CHARSET for char in encoded_data     ), "Invalid base64 character(s) found."

   # Check the padding
   assert len(SCREAMING_SNAKE_CASE__     ) % 4 == 0 and padding < 3, "Incorrect padding"

   if padding:
      # Remove padding if there is one
      __a					  =     encoded_data[:-padding]

      __a					  =     ''.join(
          bin(B64_CHARSET.index(SCREAMING_SNAKE_CASE__     )     )[2:].zfill(6     ) for char in encoded_data     )[: -padding * 2]
   else:
      __a					  =     ''.join(
          bin(B64_CHARSET.index(SCREAMING_SNAKE_CASE__     )     )[2:].zfill(6     ) for char in encoded_data     )

   __a					  =     [
       int(binary_stream[index : index + 8],       2     )
       for index in range(0,       len(SCREAMING_SNAKE_CASE__     ),       8     )
   ]

   return bytes(SCREAMING_SNAKE_CASE__     )


if __name__ == "__main__":
 import doctest

 doctest.testmod()