


'''simple docstring'''
import unittest

import numpy as np

from transformers import is_flax_available
from transformers.testing_utils import require_flax

from ..test_modeling_flax_common import ids_tensor


if is_flax_available():
		import jax
		import jax.numpy as jnp

		from transformers.generation import (
		    FlaxForcedBOSTokenLogitsProcessor,
		    FlaxForcedEOSTokenLogitsProcessor,
		    FlaxLogitsProcessorList,
		    FlaxMinLengthLogitsProcessor,
		    FlaxTemperatureLogitsWarper,
		    FlaxTopKLogitsWarper,
		    FlaxTopPLogitsWarper,
		)
@require_flax
class    lowercase	(							unittest.TestCase					):

		"""simple docstring"""

		def     lowerCAmelCase__				(		self    ,   UpperCamelCase_    ,   UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__   :List[str]												= jnp.ones((batch_size, length)							) / length
							return scores




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :Optional[Any]												= None
							UpperCamelCase__   :Tuple												= 20

							UpperCamelCase__   :Optional[int]												= self._get_uniform_logits(batch_size=2    ,   length=_a							)

							# tweak scores to not be uniform anymore
							UpperCamelCase__   :Optional[int]												= scores.at[1, 5].set((1 / length) + 0.1							)  # peak, 1st batch
							UpperCamelCase__   :Any												= scores.at[1, 10].set((1 / length) - 0.4							)  # valley, 1st batch

							# compute softmax
							UpperCamelCase__   :Optional[int]												= jax.nn.softmax(_a    ,   axis=-1							)

							UpperCamelCase__   :List[str]												= FlaxTemperatureLogitsWarper(temperature=0.5							)
							UpperCamelCase__   :List[Any]												= FlaxTemperatureLogitsWarper(temperature=1.3							)

							UpperCamelCase__   :List[str]												= jax.nn.softmax(temp_dist_warper_sharper(_a    ,   scores.copy()    ,   cur_len=_a							)    ,   axis=-1							)
							UpperCamelCase__   :Optional[Any]												= jax.nn.softmax(temp_dist_warper_smoother(_a    ,   scores.copy()    ,   cur_len=_a							)    ,   axis=-1							)

							# uniform distribution stays uniform
							self.assertTrue(jnp.allclose(probs[0, :]    ,   warped_prob_sharp[0, :]    ,   atol=1e-3							)							)
							self.assertTrue(jnp.allclose(probs[0, :]    ,   warped_prob_smooth[0, :]    ,   atol=1e-3							)							)

							# sharp peaks get higher, valleys get lower
							self.assertLess(probs[1, :].max()    ,   warped_prob_sharp[1, :].max()							)
							self.assertGreater(probs[1, :].min()    ,   warped_prob_sharp[1, :].min()							)

							# smooth peaks get lower, valleys get higher
							self.assertGreater(probs[1, :].max()    ,   warped_prob_smooth[1, :].max()							)
							self.assertLess(probs[1, :].min()    ,   warped_prob_smooth[1, :].min()							)




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :Union[str, Any]												= None
							UpperCamelCase__   :Optional[Any]												= 10
							UpperCamelCase__   :Union[str, Any]												= 2

							# create ramp distribution
							UpperCamelCase__   :List[Any]												= np.broadcast_to(np.arange(_a							)[None, :]    ,   (batch_size, vocab_size)							).copy()
							UpperCamelCase__   :Optional[Any]												= ramp_logits[1:, : vocab_size // 2] + vocab_size

							UpperCamelCase__   :List[Any]												= FlaxTopKLogitsWarper(3							)

							UpperCamelCase__   :Optional[Any]												= top_k_warp(_a    ,   _a    ,   cur_len=_a							)

							# check that correct tokens are filtered
							self.assertListEqual(jnp.isinf(scores[0]							).tolist()    ,   7 * [True] + 3 * [False]							)
							self.assertListEqual(jnp.isinf(scores[1]							).tolist()    ,   2 * [True] + 3 * [False] + 5 * [True]							)

							# check special case
							UpperCamelCase__   :Dict												= 5
							UpperCamelCase__   :Optional[Any]												= FlaxTopKLogitsWarper(top_k=1    ,   filter_value=0.0    ,   min_tokens_to_keep=3							)

							UpperCamelCase__   :Dict												= np.broadcast_to(np.arange(_a							)[None, :]    ,   (batch_size, length)							).copy()
							UpperCamelCase__   :str												= top_k_warp_safety_check(_a    ,   _a    ,   cur_len=_a							)

							# min_tokens overwrites k: 3 tokens are kept => 2 tokens are nullified
							self.assertListEqual((scores == 0.0).sum(axis=-1							).tolist()    ,   [2, 2]							)




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :List[Any]												= None
							UpperCamelCase__   :Optional[Any]												= 10
							UpperCamelCase__   :Optional[Any]												= 2

							# create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper)
							UpperCamelCase__   :List[str]												= np.log(np.array([[0.3, 0.1, 0.1, 0.5], [0.15, 0.3, 0.3, 0.25]]							)							)

							UpperCamelCase__   :Optional[Any]												= FlaxTopPLogitsWarper(0.8							)
							UpperCamelCase__   :Dict												= np.exp(top_p_warp(_a    ,   _a    ,   cur_len=_a							)							)

							# dist should be filtered to keep min num values so that sum is >= top_p
							# exp (-inf) => 0
							UpperCamelCase__   :int												= np.array([[0.3, 0.0, 0.0, 0.5], [0.0, 0.3, 0.3, 0.25]]							)
							self.assertTrue(np.allclose(_a    ,   _a    ,   atol=1e-3							)							)

							# check edge cases with negative and extreme logits
							UpperCamelCase__   :Optional[Any]												= np.broadcast_to(np.arange(_a							)[None, :]    ,   (batch_size, vocab_size)							).copy() - (
							    vocab_size // 2
							)

							# make ramp_logits more extreme
							UpperCamelCase__   :Optional[Any]												= ramp_logits[1] * 100.0

							# make sure at least 2 tokens are kept
							UpperCamelCase__   :Union[str, Any]												= FlaxTopPLogitsWarper(0.9    ,   min_tokens_to_keep=2    ,   filter_value=0.0							)
							UpperCamelCase__   :str												= top_p_warp(_a    ,   _a    ,   cur_len=_a							)

							# first batch should keep three tokens, second batch would keep only 1, but due to `min_tokens_to_keep=2` keeps 2.
							self.assertListEqual((filtered_dist != 0.0).sum(axis=-1							).tolist()    ,   [3, 2]							)




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :Any												= 20
							UpperCamelCase__   :List[Any]												= 4
							UpperCamelCase__   :Dict												= 0

							UpperCamelCase__   :Dict												= FlaxMinLengthLogitsProcessor(min_length=10    ,   eos_token_id=_a							)

							# check that min length is applied at length 5
							UpperCamelCase__   :Union[str, Any]												= ids_tensor((batch_size, 20)    ,   vocab_size=20							)
							UpperCamelCase__   :Any												= 5
							UpperCamelCase__   :Tuple												= self._get_uniform_logits(_a    ,   _a							)
							UpperCamelCase__   :Union[str, Any]												= min_dist_processor(_a    ,   _a    ,   cur_len=_a							)
							self.assertListEqual(scores_before_min_length[:, eos_token_id].tolist()    ,   4 * [-float('''inf'''							)]							)

							# check that min length is not applied anymore at length 15
							UpperCamelCase__   :Dict												= self._get_uniform_logits(_a    ,   _a							)
							UpperCamelCase__   :List[str]												= 15
							UpperCamelCase__   :str												= min_dist_processor(_a    ,   _a    ,   cur_len=_a							)
							self.assertFalse(jnp.isinf(_a							).any()							)




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :Union[str, Any]												= 20
							UpperCamelCase__   :Any												= 4
							UpperCamelCase__   :Union[str, Any]												= 0

							UpperCamelCase__   :Tuple												= FlaxForcedBOSTokenLogitsProcessor(bos_token_id=_a							)

							# check that all scores are -inf except the bos_token_id score
							UpperCamelCase__   :Optional[Any]												= ids_tensor((batch_size, 1)    ,   vocab_size=20							)
							UpperCamelCase__   :Optional[Any]												= 1
							UpperCamelCase__   :List[str]												= self._get_uniform_logits(_a    ,   _a							)
							UpperCamelCase__   :Optional[int]												= logits_processor(_a    ,   _a    ,   cur_len=_a							)
							self.assertTrue(jnp.isneginf(scores[:, bos_token_id + 1 :]							).all()							)
							self.assertListEqual(scores[:, bos_token_id].tolist()    ,   4 * [0]							)  # score for bos_token_id shold be zero

							# check that bos_token_id is not forced if current length is greater than 1
							UpperCamelCase__   :Optional[int]												= 3
							UpperCamelCase__   :List[Any]												= self._get_uniform_logits(_a    ,   _a							)
							UpperCamelCase__   :List[Any]												= logits_processor(_a    ,   _a    ,   cur_len=_a							)
							self.assertFalse(jnp.isinf(_a							).any()							)




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :List[str]												= 20
							UpperCamelCase__   :Optional[Any]												= 4
							UpperCamelCase__   :Any												= 0
							UpperCamelCase__   :Optional[Any]												= 5

							UpperCamelCase__   :Dict												= FlaxForcedEOSTokenLogitsProcessor(max_length=_a    ,   eos_token_id=_a							)

							# check that all scores are -inf except the eos_token_id when max_length is reached
							UpperCamelCase__   :Optional[int]												= ids_tensor((batch_size, 4)    ,   vocab_size=20							)
							UpperCamelCase__   :List[Any]												= 4
							UpperCamelCase__   :Optional[int]												= self._get_uniform_logits(_a    ,   _a							)
							UpperCamelCase__   :List[Any]												= logits_processor(_a    ,   _a    ,   cur_len=_a							)
							self.assertTrue(jnp.isneginf(scores[:, eos_token_id + 1 :]							).all()							)
							self.assertListEqual(scores[:, eos_token_id].tolist()    ,   4 * [0]							)  # score for eos_token_id should be zero

							# check that eos_token_id is not forced if max_length is not reached
							UpperCamelCase__   :List[str]												= 3
							UpperCamelCase__   :Union[str, Any]												= self._get_uniform_logits(_a    ,   _a							)
							UpperCamelCase__   :List[Any]												= logits_processor(_a    ,   _a    ,   cur_len=_a							)
							self.assertFalse(jnp.isinf(_a							).any()							)




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :Optional[int]												= 4
							UpperCamelCase__   :Optional[int]												= 10
							UpperCamelCase__   :str												= 15
							UpperCamelCase__   :Tuple												= 2
							UpperCamelCase__   :int												= 1
							UpperCamelCase__   :Dict												= 15

							# dummy input_ids and scores
							UpperCamelCase__   :Optional[Any]												= ids_tensor((batch_size, sequence_length)    ,   _a							)
							UpperCamelCase__   :Optional[Any]												= input_ids.copy()

							UpperCamelCase__   :Tuple												= self._get_uniform_logits(_a    ,   _a							)
							UpperCamelCase__   :Dict												= scores.copy()

							# instantiate all dist processors
							UpperCamelCase__   :Union[str, Any]												= FlaxTemperatureLogitsWarper(temperature=0.5							)
							UpperCamelCase__   :int												= FlaxTopKLogitsWarper(3							)
							UpperCamelCase__   :List[Any]												= FlaxTopPLogitsWarper(0.8							)

							# instantiate all logits processors
							UpperCamelCase__   :str												= FlaxMinLengthLogitsProcessor(min_length=10    ,   eos_token_id=_a							)
							UpperCamelCase__   :Any												= FlaxForcedBOSTokenLogitsProcessor(bos_token_id=_a							)
							UpperCamelCase__   :Optional[int]												= FlaxForcedEOSTokenLogitsProcessor(max_length=_a    ,   eos_token_id=_a							)

							UpperCamelCase__   :Optional[int]												= 10

							# no processor list
							UpperCamelCase__   :List[str]												= temp_dist_warp(_a    ,   _a    ,   cur_len=_a							)
							UpperCamelCase__   :str												= top_k_warp(_a    ,   _a    ,   cur_len=_a							)
							UpperCamelCase__   :Tuple												= top_p_warp(_a    ,   _a    ,   cur_len=_a							)
							UpperCamelCase__   :str												= min_dist_proc(_a    ,   _a    ,   cur_len=_a							)
							UpperCamelCase__   :Tuple												= bos_dist_proc(_a    ,   _a    ,   cur_len=_a							)
							UpperCamelCase__   :str												= eos_dist_proc(_a    ,   _a    ,   cur_len=_a							)

							# with processor list
							UpperCamelCase__   :List[str]												= FlaxLogitsProcessorList(
							    [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]							)
							UpperCamelCase__   :Tuple												= processor(_a    ,   _a    ,   cur_len=_a							)

							# scores should be equal
							self.assertTrue(jnp.allclose(_a    ,   _a    ,   atol=1e-3							)							)

							# input_ids should never be changed
							self.assertListEqual(input_ids.tolist()    ,   input_ids_comp.tolist()							)




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :int												= 4
							UpperCamelCase__   :Dict												= 10
							UpperCamelCase__   :str												= 15
							UpperCamelCase__   :List[Any]												= 2
							UpperCamelCase__   :List[Any]												= 1
							UpperCamelCase__   :Dict												= 15

							# dummy input_ids and scores
							UpperCamelCase__   :List[str]												= ids_tensor((batch_size, sequence_length)    ,   _a							)
							UpperCamelCase__   :int												= input_ids.copy()

							UpperCamelCase__   :Optional[int]												= self._get_uniform_logits(_a    ,   _a							)
							UpperCamelCase__   :Optional[int]												= scores.copy()

							# instantiate all dist processors
							UpperCamelCase__   :Dict												= FlaxTemperatureLogitsWarper(temperature=0.5							)
							UpperCamelCase__   :Optional[int]												= FlaxTopKLogitsWarper(3							)
							UpperCamelCase__   :Dict												= FlaxTopPLogitsWarper(0.8							)

							# instantiate all logits processors
							UpperCamelCase__   :Optional[Any]												= FlaxMinLengthLogitsProcessor(min_length=10    ,   eos_token_id=_a							)
							UpperCamelCase__   :Optional[int]												= FlaxForcedBOSTokenLogitsProcessor(bos_token_id=_a							)
							UpperCamelCase__   :Union[str, Any]												= FlaxForcedEOSTokenLogitsProcessor(max_length=_a    ,   eos_token_id=_a							)

							UpperCamelCase__   :List[Any]												= 10

							# no processor list
							def run_no_processor_list(UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_							):
												UpperCamelCase__   :List[str]												= temp_dist_warp(_a    ,   _a    ,   cur_len=_a							)
												UpperCamelCase__   :int												= top_k_warp(_a    ,   _a    ,   cur_len=_a							)
												UpperCamelCase__   :Any												= top_p_warp(_a    ,   _a    ,   cur_len=_a							)
												UpperCamelCase__   :str												= min_dist_proc(_a    ,   _a    ,   cur_len=_a							)
												UpperCamelCase__   :List[str]												= bos_dist_proc(_a    ,   _a    ,   cur_len=_a							)
												UpperCamelCase__   :Optional[int]												= eos_dist_proc(_a    ,   _a    ,   cur_len=_a							)
												return scores

							# with processor list
							def run_processor_list(UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_							):
												UpperCamelCase__   :Optional[int]												= FlaxLogitsProcessorList(
												    [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc]							)
												UpperCamelCase__   :int												= processor(_a    ,   _a    ,   cur_len=_a							)
												return scores

							UpperCamelCase__   :Optional[int]												= jax.jit(_a							)
							UpperCamelCase__   :List[str]												= jax.jit(_a							)

							UpperCamelCase__   :Optional[Any]												= jitted_run_no_processor_list(_a    ,   _a    ,   _a							)
							UpperCamelCase__   :Tuple												= jitted_run_processor_list(_a    ,   _a    ,   _a							)

							# scores should be equal
							self.assertTrue(jnp.allclose(_a    ,   _a    ,   atol=1e-3							)							)

							# input_ids should never be changed
							self.assertListEqual(input_ids.tolist()    ,   input_ids_comp.tolist()							)

def   lowerCAmelCase_						(					_snake_case      :  str      ,   _snake_case      :  str   )				->		bool:







	'''simple docstring'''


	__magic_name__		:		Union[str, Any]	   =     len(_snake_case   ) + 1
	__magic_name__		:		List[str]	   =     len(_snake_case   ) + 1

	# dp is a 2d matrix where dp[i][j] denotes whether prefix string of
	# length i of input_string matches with prefix string of length j of
	# given pattern.
	# "dp" stands for dynamic programming.
	__magic_name__		:		str	   =     [[0 for i in range(_snake_case   )] for j in range(_snake_case   )]

	# since string of zero length match pattern of zero length
	__magic_name__		:		Optional[int]	   =     1

	# since pattern of zero length will never match with string of non-zero length
	for i in range(1      ,   _snake_case   ):
		__magic_name__		:		Optional[int]	   =     0

	# since string of zero length will match with pattern where there
	# is at least one * alternatively
	for j in range(1      ,   _snake_case   ):
		__magic_name__		:		Union[str, Any]	   =     dp[0][j - 2] if pattern[j - 1] == "*" else 0

	# now using bottom-up approach to find for all remaining lengths
	for i in range(1      ,   _snake_case   ):
		for j in range(1      ,   _snake_case   ):
			if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == ".":
				__magic_name__		:		Optional[int]	   =     dp[i - 1][j - 1]

			elif pattern[j - 1] == "*":
				if dp[i][j - 2] == 1:
					__magic_name__		:		Optional[Any]	   =     1
				elif pattern[j - 2] in (input_string[i - 1], "."):
					__magic_name__		:		List[Any]	   =     dp[i - 1][j]
				else:
					__magic_name__		:		Union[str, Any]	   =     0
			else:
				__magic_name__		:		Dict	   =     0

	return bool(dp[-1][-1]   )


if __name__ == "__main__":
				import doctest

				doctest.testmod()
				# inputing the strings
				# input_string = input("input a string :")
				# pattern = input("input a pattern :")

				snake_case :		Optional[Any]							      =  "aab"
				snake_case :		List[str]							      =  "c*a*b"

				# using function to check whether given string matches the given pattern
				if match_pattern(input_string, pattern):
								print(F"{input_string} matches the given pattern {pattern}")
				else:
								print(F"{input_string} does not match with the given pattern {pattern}")
