
"""simple docstring"""



import random
import unittest

import numpy as np

import transformers
from transformers import is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax


if is_flax_available():
	import os

	import jax.numpy as jnp
	from jax import jit

	from transformers import AutoTokenizer, FlaxAutoModelForCausalLM
	from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model

	_snake_case          =						'''0.12'''  # assumed parallelism: 8


if is_torch_available():
	import torch






def 	__snake_case  (    SCREAMING_SNAKE_CASE:					Tuple     ,  SCREAMING_SNAKE_CASE:					Union[str, Any]     ,  SCREAMING_SNAKE_CASE:					List[Any]=None						):






							"""simple docstring"""



							if rng is None:
														_lowerCAmelCase  	=			random.Random()

							_lowerCAmelCase  	=			1
							for dim in shape:
														total_dims *= dim

							_lowerCAmelCase  	=			[]
							for _ in range(SCREAMING_SNAKE_CASE						):
														values.append(rng.randint(0     ,  vocab_size - 1						)						)

							_lowerCAmelCase  	=			np.array(SCREAMING_SNAKE_CASE     ,  dtype=jnp.intaa						).reshape(SCREAMING_SNAKE_CASE						)

							return output






def 	__snake_case  (    SCREAMING_SNAKE_CASE:					List[str]     ,  SCREAMING_SNAKE_CASE:					Optional[int]=None						):






							"""simple docstring"""



							_lowerCAmelCase  	=			ids_tensor(SCREAMING_SNAKE_CASE     ,  vocab_size=2     ,  rng=SCREAMING_SNAKE_CASE						)
							# make sure that at least one token is attended to for each batch
							_lowerCAmelCase  	=			1
							return attn_mask
@require_flax
class        _SCREAMING_SNAKE_CASE  :






				'''simple docstring'''



				SCREAMING_SNAKE_CASE_:							Tuple							=					None
				SCREAMING_SNAKE_CASE_:							List[Any]							=					()


				def 				__lowerCamelCase	(					self	:			str  )		->  Optional[Any]:







											"""simple docstring"""




											_lowerCAmelCase	,		_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs_for_common()

											# cut to half length & take max batch_size 3
											_lowerCAmelCase  	=			2
											_lowerCAmelCase  	=			inputs['input_ids'].shape[-1] // 2
											_lowerCAmelCase  	=			inputs['input_ids'][:max_batch_size, :sequence_length]

											_lowerCAmelCase  	=			jnp.ones_like(UpperCAmelCase_  )
											_lowerCAmelCase  	=			attention_mask[:max_batch_size, :sequence_length]

											# generate max 5 tokens
											_lowerCAmelCase  	=			input_ids.shape[-1] + 5
											if config.eos_token_id is not None and config.pad_token_id is None:
																		# hack to allow generate for models such as GPT2 as is done in `generate()`
																		_lowerCAmelCase  	=			config.eos_token_id
											return config, input_ids, attention_mask, max_length


				@is_pt_flax_cross_test
				def 				__lowerCamelCase	(					self	:			List[str]  )		->  Union[str, Any]:







											"""simple docstring"""




											_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase  	=			self._get_input_ids_and_config()
											_lowerCAmelCase  	=			False
											_lowerCAmelCase  	=			max_length
											_lowerCAmelCase  	=			0

											for model_class in self.all_generative_model_classes:
																		_lowerCAmelCase  	=			model_class(UpperCAmelCase_  )

																		_lowerCAmelCase  	=			model_class.__name__[4:]  # Skip the "Flax" at the beginning
																		_lowerCAmelCase  	=			getattr(UpperCAmelCase_				,     UpperCAmelCase_  )
																		_lowerCAmelCase  	=			pt_model_class(UpperCAmelCase_  ).eval()
																		_lowerCAmelCase  	=			load_flax_weights_in_pytorch_model(UpperCAmelCase_				,     flax_model.params  )

																		_lowerCAmelCase  	=			flax_model.generate(UpperCAmelCase_  ).sequences
																		_lowerCAmelCase  	=			pt_model.generate(torch.tensor(UpperCAmelCase_				,     dtype=torch.long  )  )

																		if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]:
																									_lowerCAmelCase  	=			flax_generation_outputs[:, : pt_generation_outputs.shape[-1]]

																		self.assertListEqual(pt_generation_outputs.numpy().tolist()				,     flax_generation_outputs.tolist()  )


				def 				__lowerCamelCase	(					self	:			List[str]  )		->  List[str]:







											"""simple docstring"""




											_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase  	=			self._get_input_ids_and_config()
											_lowerCAmelCase  	=			False
											_lowerCAmelCase  	=			max_length

											for model_class in self.all_generative_model_classes:
																		_lowerCAmelCase  	=			model_class(UpperCAmelCase_  )

																		_lowerCAmelCase  	=			model.generate(UpperCAmelCase_  ).sequences
																		self.assertEqual(generation_outputs.shape[-1]				,     UpperCAmelCase_  )

																		_lowerCAmelCase  	=			jit(model.generate  )
																		_lowerCAmelCase  	=			jit_generate(UpperCAmelCase_  ).sequences

																		self.assertListEqual(generation_outputs.tolist()				,     jit_generation_outputs.tolist()  )


				def 				__lowerCamelCase	(					self	:			Any  )		->  Optional[Any]:







											"""simple docstring"""




											_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase  	=			self._get_input_ids_and_config()
											_lowerCAmelCase  	=			True
											_lowerCAmelCase  	=			max_length

											for model_class in self.all_generative_model_classes:
																		_lowerCAmelCase  	=			model_class(UpperCAmelCase_  )

																		_lowerCAmelCase  	=			model.generate(UpperCAmelCase_  ).sequences
																		self.assertEqual(generation_outputs.shape[-1]				,     UpperCAmelCase_  )

																		_lowerCAmelCase  	=			jit(model.generate  )
																		_lowerCAmelCase  	=			jit_generate(UpperCAmelCase_  ).sequences

																		self.assertListEqual(generation_outputs.tolist()				,     jit_generation_outputs.tolist()  )


				def 				__lowerCamelCase	(					self	:			Union[str, Any]  )		->  List[Any]:







											"""simple docstring"""




											_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase  	=			self._get_input_ids_and_config()
											_lowerCAmelCase  	=			False
											_lowerCAmelCase  	=			max_length
											_lowerCAmelCase  	=			2

											for model_class in self.all_generative_model_classes:
																		_lowerCAmelCase  	=			model_class(UpperCAmelCase_  )

																		_lowerCAmelCase  	=			model.generate(UpperCAmelCase_  ).sequences
																		self.assertEqual(generation_outputs.shape[-1]				,     UpperCAmelCase_  )

																		_lowerCAmelCase  	=			jit(model.generate  )
																		_lowerCAmelCase  	=			jit_generate(UpperCAmelCase_  ).sequences

																		self.assertListEqual(generation_outputs.tolist()				,     jit_generation_outputs.tolist()  )


				def 				__lowerCamelCase	(					self	:			str  )		->  List[str]:







											"""simple docstring"""




											_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase  	=			self._get_input_ids_and_config()
											_lowerCAmelCase  	=			False
											_lowerCAmelCase  	=			max_length
											_lowerCAmelCase  	=			2
											_lowerCAmelCase  	=			2

											for model_class in self.all_generative_model_classes:
																		_lowerCAmelCase  	=			model_class(UpperCAmelCase_  )

																		_lowerCAmelCase  	=			model.generate(UpperCAmelCase_  ).sequences
																		self.assertEqual(generation_outputs.shape[0]				,     input_ids.shape[0] * config.num_return_sequences  )


				def 				__lowerCamelCase	(					self	:			int  )		->  Any:







											"""simple docstring"""




											_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase  	=			self._get_input_ids_and_config()
											_lowerCAmelCase  	=			True
											_lowerCAmelCase  	=			max_length
											_lowerCAmelCase  	=			0.8
											_lowerCAmelCase  	=			10
											_lowerCAmelCase  	=			0.3
											_lowerCAmelCase  	=			1
											_lowerCAmelCase  	=			8
											_lowerCAmelCase  	=			9

											for model_class in self.all_generative_model_classes:
																		_lowerCAmelCase  	=			model_class(UpperCAmelCase_  )

																		_lowerCAmelCase  	=			model.generate(UpperCAmelCase_  ).sequences
																		self.assertEqual(generation_outputs.shape[-1]				,     UpperCAmelCase_  )

																		_lowerCAmelCase  	=			jit(model.generate  )
																		_lowerCAmelCase  	=			jit_generate(UpperCAmelCase_  ).sequences

																		self.assertListEqual(generation_outputs.tolist()				,     jit_generation_outputs.tolist()  )


				def 				__lowerCamelCase	(					self	:			Optional[Any]  )		->  Optional[int]:







											"""simple docstring"""




											_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase  	=			self._get_input_ids_and_config()
											_lowerCAmelCase  	=			max_length
											_lowerCAmelCase  	=			1
											_lowerCAmelCase  	=			8
											_lowerCAmelCase  	=			9

											for model_class in self.all_generative_model_classes:
																		_lowerCAmelCase  	=			model_class(UpperCAmelCase_  )

																		_lowerCAmelCase  	=			model.generate(UpperCAmelCase_  ).sequences
																		self.assertEqual(generation_outputs.shape[-1]				,     UpperCAmelCase_  )

																		_lowerCAmelCase  	=			jit(model.generate  )
																		_lowerCAmelCase  	=			jit_generate(UpperCAmelCase_  ).sequences

																		self.assertListEqual(generation_outputs.tolist()				,     jit_generation_outputs.tolist()  )


				def 				__lowerCamelCase	(					self	:			List[Any]  )		->  List[str]:







											"""simple docstring"""




											_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase  	=			self._get_input_ids_and_config()
											_lowerCAmelCase  	=			max_length
											_lowerCAmelCase  	=			2
											_lowerCAmelCase  	=			1
											_lowerCAmelCase  	=			8
											_lowerCAmelCase  	=			9

											for model_class in self.all_generative_model_classes:
																		_lowerCAmelCase  	=			model_class(UpperCAmelCase_  )

																		_lowerCAmelCase  	=			model.generate(UpperCAmelCase_  ).sequences
																		self.assertEqual(generation_outputs.shape[-1]				,     UpperCAmelCase_  )

																		_lowerCAmelCase  	=			jit(model.generate  )
																		_lowerCAmelCase  	=			jit_generate(UpperCAmelCase_  ).sequences

																		self.assertListEqual(generation_outputs.tolist()				,     jit_generation_outputs.tolist()  )


				def 				__lowerCamelCase	(					self	:			Optional[int]  )		->  Optional[Any]:







											"""simple docstring"""




											_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase  	=			self._get_input_ids_and_config()

											# pad attention mask on the left
											_lowerCAmelCase  	=			attention_mask.at[(0, 0)].set(0  )

											_lowerCAmelCase  	=			False
											_lowerCAmelCase  	=			max_length

											for model_class in self.all_generative_model_classes:
																		_lowerCAmelCase  	=			model_class(UpperCAmelCase_  )

																		_lowerCAmelCase  	=			model.generate(UpperCAmelCase_				,     attention_mask=UpperCAmelCase_  ).sequences
																		self.assertEqual(generation_outputs.shape[-1]				,     UpperCAmelCase_  )

																		_lowerCAmelCase  	=			jit(model.generate  )
																		_lowerCAmelCase  	=			jit_generate(UpperCAmelCase_				,     attention_mask=UpperCAmelCase_  ).sequences

																		self.assertListEqual(generation_outputs.tolist()				,     jit_generation_outputs.tolist()  )


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







											"""simple docstring"""




											_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase  	=			self._get_input_ids_and_config()

											# pad attention mask on the left
											_lowerCAmelCase  	=			attention_mask.at[(0, 0)].set(0  )

											_lowerCAmelCase  	=			True
											_lowerCAmelCase  	=			max_length

											for model_class in self.all_generative_model_classes:
																		_lowerCAmelCase  	=			model_class(UpperCAmelCase_  )

																		_lowerCAmelCase  	=			model.generate(UpperCAmelCase_				,     attention_mask=UpperCAmelCase_  ).sequences
																		self.assertEqual(generation_outputs.shape[-1]				,     UpperCAmelCase_  )

																		_lowerCAmelCase  	=			jit(model.generate  )
																		_lowerCAmelCase  	=			jit_generate(UpperCAmelCase_				,     attention_mask=UpperCAmelCase_  ).sequences

																		self.assertListEqual(generation_outputs.tolist()				,     jit_generation_outputs.tolist()  )







				def 				__lowerCamelCase	(					self	:			Union[str, Any]  )		->  Optional[Any]:







											"""simple docstring"""




											_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase	,		_lowerCAmelCase  	=			self._get_input_ids_and_config()

											# pad attention mask on the left
											_lowerCAmelCase  	=			attention_mask.at[(0, 0)].set(0  )

											_lowerCAmelCase  	=			2
											_lowerCAmelCase  	=			max_length

											for model_class in self.all_generative_model_classes:
																		_lowerCAmelCase  	=			model_class(UpperCAmelCase_  )

																		_lowerCAmelCase  	=			model.generate(UpperCAmelCase_				,     attention_mask=UpperCAmelCase_  ).sequences
																		self.assertEqual(generation_outputs.shape[-1]				,     UpperCAmelCase_  )

																		_lowerCAmelCase  	=			jit(model.generate  )
																		_lowerCAmelCase  	=			jit_generate(UpperCAmelCase_				,     attention_mask=UpperCAmelCase_  ).sequences

																		self.assertListEqual(generation_outputs.tolist()				,     jit_generation_outputs.tolist()  )






@require_flax
class        _SCREAMING_SNAKE_CASE  (  unittest.TestCase							):






				'''simple docstring'''



				def 				__lowerCamelCase	(					self	:			Tuple  )		->  str:







											"""simple docstring"""




											_lowerCAmelCase  	=			AutoTokenizer.from_pretrained('hf-internal-testing/tiny-bert'  )
											_lowerCAmelCase  	=			FlaxAutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-bert-flax-only'  )

											_lowerCAmelCase  	=			'Hello world'
											_lowerCAmelCase  	=			tokenizer(UpperCAmelCase_				,     return_tensors='np'  ).input_ids

											# typos are quickly detected (the correct argument is `do_sample`)
											with self.assertRaisesRegex(UpperCAmelCase_				,     'do_samples'  ):
																		model.generate(UpperCAmelCase_				,     do_samples=UpperCAmelCase_  )

											# arbitrary arguments that will not be used anywhere are also not accepted
											with self.assertRaisesRegex(UpperCAmelCase_				,     'foo'  ):
																		_lowerCAmelCase  	=			{'foo': 'bar'}
																		model.generate(UpperCAmelCase_				,     **UpperCAmelCase_  )








"""simple docstring"""



def 	__snake_case  (    SCREAMING_SNAKE_CASE:					str						):






							"""simple docstring"""



							_lowerCAmelCase  	=			[0 for i in range(len(SCREAMING_SNAKE_CASE						)						)]

							# initialize interval's left pointer and right pointer
							_lowerCAmelCase	,		_lowerCAmelCase  	=			0, 0

							for i in range(1     ,  len(SCREAMING_SNAKE_CASE						)						):
														# case when current index is inside the interval
														if i <= right_pointer:
																					_lowerCAmelCase  	=			min(right_pointer - i + 1     ,  z_result[i - left_pointer]						)
																					_lowerCAmelCase  	=			min_edge

														while go_next(SCREAMING_SNAKE_CASE     ,  SCREAMING_SNAKE_CASE     ,  SCREAMING_SNAKE_CASE						):
																					z_result[i] += 1

														# if new index's result gives us more right interval,
														# we've to update left_pointer and right_pointer
														if i + z_result[i] - 1 > right_pointer:
																					_lowerCAmelCase	,		_lowerCAmelCase  	=			i, i + z_result[i] - 1

							return z_result






def 	__snake_case  (    SCREAMING_SNAKE_CASE:					int     ,  SCREAMING_SNAKE_CASE:					list[int]     ,  SCREAMING_SNAKE_CASE:					str						):






							"""simple docstring"""



							return i + z_result[i] < len(SCREAMING_SNAKE_CASE						) and s[z_result[i]] == s[i + z_result[i]]






def 	__snake_case  (    SCREAMING_SNAKE_CASE:					str     ,  SCREAMING_SNAKE_CASE:					str						):






							"""simple docstring"""



							_lowerCAmelCase  	=			0
							# concatenate 'pattern' and 'input_str' and call z_function
							# with concatenated string
							_lowerCAmelCase  	=			z_function(pattern + input_str						)

							for val in z_result:
														# if value is greater then length of the pattern string
														# that means this index is starting position of substring
														# which is equal to pattern string
														if val >= len(SCREAMING_SNAKE_CASE						):
																					answer += 1

							return answer


if __name__ == "__main__":
	import doctest

	doctest.testmod()






