


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

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






def __UpperCamelCase   (			__lowerCamelCase		: Tuple      ,  __lowerCamelCase		: Tuple				)      ->     Union[str, Any]:







     '''simple docstring'''






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






def __UpperCamelCase   (			__lowerCamelCase		: int				)      ->     Union[str, Any]:







     '''simple docstring'''






     def replace(__lowerCamelCase		: Tuple      ,  __lowerCamelCase		: Any				):
          for rule, replacement in rules:
               if _match(__lowerCamelCase      ,  __lowerCamelCase				):
                    return replacement
          return val

     return replace






def __UpperCamelCase   (			)      ->     Tuple:







     '''simple docstring'''






     return [
         # embeddings
         (("transformer", "wpe", "embedding"), P("mp"      ,  __lowerCamelCase				)),
         (("transformer", "wte", "embedding"), P("mp"      ,  __lowerCamelCase				)),
         # atention
         (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(__lowerCamelCase      ,  "mp"				)),
         (("attention", "out_proj", "kernel"), P("mp"      ,  __lowerCamelCase				)),
         (("attention", "out_proj", "bias"), None),
         # mlp
         (("mlp", "c_fc", "kernel"), P(__lowerCamelCase      ,  "mp"				)),
         (("mlp", "c_fc", "bias"), P("mp"				)),
         (("mlp", "c_proj", "kernel"), P("mp"      ,  __lowerCamelCase				)),
         (("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   (			__lowerCamelCase		: Dict				)      ->     Tuple:







     '''simple docstring'''






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





'''simple docstring'''

import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin





class 						__SCREAMING_SNAKE_CASE					(      unittest.TestCase ,			lowerCamelCase__ ):






     def  a_					(			self		)       ->    Tuple:
          _a						=		load_tool("text-classification"		)
          self.tool.setup()
          _a						=		load_tool("text-classification"     ,							remote=__UpperCamelCase		)






     def  a_					(			self		)       ->    Optional[int]:
          _a						=		self.tool("That's quite cool"     ,							["positive", "negative"]		)
          self.assertEqual(__UpperCamelCase     ,							"positive"		)






     def  a_					(			self		)       ->    Union[str, Any]:
          _a						=		self.remote_tool("That's quite cool"     ,							["positive", "negative"]		)
          self.assertEqual(__UpperCamelCase     ,							"positive"		)






     def  a_					(			self		)       ->    Optional[int]:
          _a						=		self.tool(text="That's quite cool"     ,							labels=["positive", "negative"]		)
          self.assertEqual(__UpperCamelCase     ,							"positive"		)






     def  a_					(			self		)       ->    str:
          _a						=		self.remote_tool(text="That's quite cool"     ,							labels=["positive", "negative"]		)
          self.assertEqual(__UpperCamelCase     ,							"positive"		)

