





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							:			Optional[Any]         =			object()

# For specifying empty leaf dict `{}`
__A							:			Union[str, Any]         =			object()





def 				__lowerCAmelCase(       _SCREAMING_SNAKE_CASE							,     _SCREAMING_SNAKE_CASE				)							-> Dict:




    """simple docstring"""




    _A     =							tuple((re.compile(x + '$'				) for x in qs)				)
    for i in range(len(_SCREAMING_SNAKE_CASE				) - len(_SCREAMING_SNAKE_CASE				) + 1				):
        _A     =							[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 				__lowerCAmelCase(       _SCREAMING_SNAKE_CASE				)							-> Optional[int]:




    """simple docstring"""




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

    return replace





def 				__lowerCAmelCase(       )							-> int:




    """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 				__lowerCAmelCase(       _SCREAMING_SNAKE_CASE				)							-> Optional[int]:




    """simple docstring"""




    _A     =							_get_partition_rules()
    _A     =							_replacement_rules(_SCREAMING_SNAKE_CASE				)
    _A     =							{k: _unmatched for k in flatten_dict(_SCREAMING_SNAKE_CASE				)}
    _A     =							{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"""

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
class   _UpperCAmelCase				(		SCREAMING_SNAKE_CASE_				):
      __SCREAMING_SNAKE_CASE :				List[Any]				   =						["image_processor", "tokenizer"]
      __SCREAMING_SNAKE_CASE :				Dict				   =						"AutoImageProcessor"
      __SCREAMING_SNAKE_CASE :				Optional[int]				   =						"AutoTokenizer"




      def __init__(				self ,							lowercase_ ,							lowercase_				)    ->       List[str]:
          super().__init__(lowercase_ ,							lowercase_				)
          UpperCAmelCase          =       self.image_processor




      def __call__(				self ,							lowercase_=None ,							lowercase_=None ,							lowercase_=None ,							**lowercase_				)    ->       Any:

          if text is None and images is None:
              raise ValueError('You have to specify either text or images. Both cannot be none.'				)

          if text is not None:
              UpperCAmelCase          =       self.tokenizer(lowercase_ ,							return_tensors=lowercase_ ,							**lowercase_				)

          if images is not None:
              UpperCAmelCase          =       self.image_processor(lowercase_ ,							return_tensors=lowercase_ ,							**lowercase_				)

          if text is not None and images is not None:
              UpperCAmelCase          =       image_features.pixel_values
              return encoding
          elif text is not None:
              return encoding
          else:
              return BatchEncoding(data=dict(**lowercase_				) ,							tensor_type=lowercase_				)




      def      a_   (				self ,							*lowercase_ ,							**lowercase_				)    ->       Optional[Any]:
          return self.tokenizer.batch_decode(*lowercase_ ,							**lowercase_				)




      def      a_   (				self ,							*lowercase_ ,							**lowercase_				)    ->       Optional[int]:
          return self.tokenizer.decode(*lowercase_ ,							**lowercase_				)




      @property
      def      a_   (				self				)    ->       Optional[int]:
          return ["input_ids", "attention_mask", "pixel_values"]



