






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

	lowerCamelCase			:			List[str]     =							"""0.12"""  # assumed parallelism: 8


if is_torch_available():
	import torch
def    snake_case_		(  lowerCAmelCase_							:    int							,							lowerCAmelCase_							:    int							,							lowerCAmelCase_							:    List[Any]=None   ):
							if rng is None:
														__lowercase   :				Union[str, Any]						      =  random.Random()

							__lowercase   :				List[Any]						      =  1
							for dim in shape:
														total_dims *= dim

							__lowercase   :				List[Any]						      =  []
							for _ in range(__snake_case   ):
														values.append(rng.randint(0							,							vocab_size - 1   )   )

							__lowercase   :				Union[str, Any]						      =  np.array(__snake_case							,							dtype=jnp.intaa   ).reshape(__snake_case   )

							return output
def    snake_case_		(  lowerCAmelCase_							:    int							,							lowerCAmelCase_							:    Optional[Any]=None   ):
							__lowercase   :				str						      =  ids_tensor(__snake_case							,							vocab_size=2							,							rng=__snake_case   )
							# make sure that at least one token is attended to for each batch
							__lowercase   :				Union[str, Any]						      =  1
							return attn_mask

@require_flax
class 		lowerCAmelCase  :

					'''simple docstring'''

					_A    :      str					       =   None
					_A    :      List[str]					       =   ()



					def    lowerCAmelCase					(	self					:						Any				)      ->	str:


												"""simple docstring"""

												__lowercase   :				Optional[Any]						      =  self.model_tester.prepare_config_and_inputs_for_common()

												# cut to half length & take max batch_size 3
												__lowercase   :				Optional[Any]						      =  2
												__lowercase   :				Dict						      =  inputs["input_ids"].shape[-1] // 2
												__lowercase   :				Optional[int]						      =  inputs["input_ids"][:max_batch_size, :sequence_length]

												__lowercase   :				Optional[int]						      =  jnp.ones_like(_a				)
												__lowercase   :				int						      =  attention_mask[:max_batch_size, :sequence_length]

												# generate max 5 tokens
												__lowercase   :				Optional[int]						      =  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()`
																			__lowercase   :				Any						      =  config.eos_token_id
												return config, input_ids, attention_mask, max_length



					@is_pt_flax_cross_test
					def    lowerCAmelCase					(	self					:						str				)      ->	Tuple:


												"""simple docstring"""

												__lowercase   :				Union[str, Any]						      =  self._get_input_ids_and_config()
												__lowercase   :				List[str]						      =  False
												__lowercase   :				Any						      =  max_length
												__lowercase   :				Any						      =  0

												for model_class in self.all_generative_model_classes:
																			__lowercase   :				int						      =  model_class(_a				)

																			__lowercase   :				Optional[Any]						      =  model_class.__name__[4:]  # Skip the "Flax" at the beginning
																			__lowercase   :				List[Any]						      =  getattr(_a		,  _a				)
																			__lowercase   :				Any						      =  pt_model_class(_a				).eval()
																			__lowercase   :				Union[str, Any]						      =  load_flax_weights_in_pytorch_model(_a		,  flax_model.params				)

																			__lowercase   :				Optional[Any]						      =  flax_model.generate(_a				).sequences
																			__lowercase   :				Union[str, Any]						      =  pt_model.generate(torch.tensor(_a		,  dtype=torch.long				)				)

																			if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]:
																										__lowercase   :				List[str]						      =  flax_generation_outputs[:, : pt_generation_outputs.shape[-1]]

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



					def    lowerCAmelCase					(	self					:						Dict				)      ->	Any:


												"""simple docstring"""

												__lowercase   :				str						      =  self._get_input_ids_and_config()
												__lowercase   :				Union[str, Any]						      =  False
												__lowercase   :				List[str]						      =  max_length

												for model_class in self.all_generative_model_classes:
																			__lowercase   :				int						      =  model_class(_a				)

																			__lowercase   :				List[str]						      =  model.generate(_a				).sequences
																			self.assertEqual(generation_outputs.shape[-1]		,  _a				)

																			__lowercase   :				str						      =  jit(model.generate				)
																			__lowercase   :				List[Any]						      =  jit_generate(_a				).sequences

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



					def    lowerCAmelCase					(	self					:						List[str]				)      ->	Dict:


												"""simple docstring"""

												__lowercase   :				Optional[int]						      =  self._get_input_ids_and_config()
												__lowercase   :				int						      =  True
												__lowercase   :				int						      =  max_length

												for model_class in self.all_generative_model_classes:
																			__lowercase   :				int						      =  model_class(_a				)

																			__lowercase   :				List[str]						      =  model.generate(_a				).sequences
																			self.assertEqual(generation_outputs.shape[-1]		,  _a				)

																			__lowercase   :				str						      =  jit(model.generate				)
																			__lowercase   :				Any						      =  jit_generate(_a				).sequences

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



					def    lowerCAmelCase					(	self					:						Any				)      ->	Any:


												"""simple docstring"""

												__lowercase   :				Optional[int]						      =  self._get_input_ids_and_config()
												__lowercase   :				Optional[int]						      =  False
												__lowercase   :				Tuple						      =  max_length
												__lowercase   :				Optional[int]						      =  2

												for model_class in self.all_generative_model_classes:
																			__lowercase   :				Dict						      =  model_class(_a				)

																			__lowercase   :				Tuple						      =  model.generate(_a				).sequences
																			self.assertEqual(generation_outputs.shape[-1]		,  _a				)

																			__lowercase   :				Union[str, Any]						      =  jit(model.generate				)
																			__lowercase   :				Dict						      =  jit_generate(_a				).sequences

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



					def    lowerCAmelCase					(	self					:						Optional[int]				)      ->	Tuple:


												"""simple docstring"""

												__lowercase   :				Union[str, Any]						      =  self._get_input_ids_and_config()
												__lowercase   :				int						      =  False
												__lowercase   :				Optional[int]						      =  max_length
												__lowercase   :				Dict						      =  2
												__lowercase   :				List[str]						      =  2

												for model_class in self.all_generative_model_classes:
																			__lowercase   :				Optional[int]						      =  model_class(_a				)

																			__lowercase   :				Dict						      =  model.generate(_a				).sequences
																			self.assertEqual(generation_outputs.shape[0]		,  input_ids.shape[0] * config.num_return_sequences				)



					def    lowerCAmelCase					(	self					:						Optional[int]				)      ->	int:


												"""simple docstring"""

												__lowercase   :				Tuple						      =  self._get_input_ids_and_config()
												__lowercase   :				List[str]						      =  True
												__lowercase   :				int						      =  max_length
												__lowercase   :				Union[str, Any]						      =  0.8
												__lowercase   :				List[str]						      =  10
												__lowercase   :				str						      =  0.3
												__lowercase   :				Optional[int]						      =  1
												__lowercase   :				int						      =  8
												__lowercase   :				List[str]						      =  9

												for model_class in self.all_generative_model_classes:
																			__lowercase   :				Union[str, Any]						      =  model_class(_a				)

																			__lowercase   :				Any						      =  model.generate(_a				).sequences
																			self.assertEqual(generation_outputs.shape[-1]		,  _a				)

																			__lowercase   :				List[str]						      =  jit(model.generate				)
																			__lowercase   :				int						      =  jit_generate(_a				).sequences

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



					def    lowerCAmelCase					(	self					:						Dict				)      ->	List[Any]:


												"""simple docstring"""

												__lowercase   :				Optional[int]						      =  self._get_input_ids_and_config()
												__lowercase   :				Optional[int]						      =  max_length
												__lowercase   :				Optional[Any]						      =  1
												__lowercase   :				Union[str, Any]						      =  8
												__lowercase   :				Union[str, Any]						      =  9

												for model_class in self.all_generative_model_classes:
																			__lowercase   :				int						      =  model_class(_a				)

																			__lowercase   :				Any						      =  model.generate(_a				).sequences
																			self.assertEqual(generation_outputs.shape[-1]		,  _a				)

																			__lowercase   :				str						      =  jit(model.generate				)
																			__lowercase   :				Optional[Any]						      =  jit_generate(_a				).sequences

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



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


												"""simple docstring"""

												__lowercase   :				List[str]						      =  self._get_input_ids_and_config()
												__lowercase   :				Tuple						      =  max_length
												__lowercase   :				Any						      =  2
												__lowercase   :				Optional[int]						      =  1
												__lowercase   :				int						      =  8
												__lowercase   :				str						      =  9

												for model_class in self.all_generative_model_classes:
																			__lowercase   :				Dict						      =  model_class(_a				)

																			__lowercase   :				str						      =  model.generate(_a				).sequences
																			self.assertEqual(generation_outputs.shape[-1]		,  _a				)

																			__lowercase   :				Optional[int]						      =  jit(model.generate				)
																			__lowercase   :				int						      =  jit_generate(_a				).sequences

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



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


												"""simple docstring"""

												__lowercase   :				List[str]						      =  self._get_input_ids_and_config()

												# pad attention mask on the left
												__lowercase   :				Optional[int]						      =  attention_mask.at[(0, 0)].set(0				)

												__lowercase   :				Union[str, Any]						      =  False
												__lowercase   :				Optional[int]						      =  max_length

												for model_class in self.all_generative_model_classes:
																			__lowercase   :				Dict						      =  model_class(_a				)

																			__lowercase   :				Tuple						      =  model.generate(_a		,  attention_mask=_a				).sequences
																			self.assertEqual(generation_outputs.shape[-1]		,  _a				)

																			__lowercase   :				int						      =  jit(model.generate				)
																			__lowercase   :				str						      =  jit_generate(_a		,  attention_mask=_a				).sequences

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



					def    lowerCAmelCase					(	self					:						Dict				)      ->	Tuple:


												"""simple docstring"""

												__lowercase   :				Union[str, Any]						      =  self._get_input_ids_and_config()

												# pad attention mask on the left
												__lowercase   :				List[Any]						      =  attention_mask.at[(0, 0)].set(0				)

												__lowercase   :				Union[str, Any]						      =  True
												__lowercase   :				Tuple						      =  max_length

												for model_class in self.all_generative_model_classes:
																			__lowercase   :				Optional[Any]						      =  model_class(_a				)

																			__lowercase   :				List[Any]						      =  model.generate(_a		,  attention_mask=_a				).sequences
																			self.assertEqual(generation_outputs.shape[-1]		,  _a				)

																			__lowercase   :				Optional[Any]						      =  jit(model.generate				)
																			__lowercase   :				List[Any]						      =  jit_generate(_a		,  attention_mask=_a				).sequences

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



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


												"""simple docstring"""

												__lowercase   :				Tuple						      =  self._get_input_ids_and_config()

												# pad attention mask on the left
												__lowercase   :				Union[str, Any]						      =  attention_mask.at[(0, 0)].set(0				)

												__lowercase   :				Optional[Any]						      =  2
												__lowercase   :				Tuple						      =  max_length

												for model_class in self.all_generative_model_classes:
																			__lowercase   :				Dict						      =  model_class(_a				)

																			__lowercase   :				int						      =  model.generate(_a		,  attention_mask=_a				).sequences
																			self.assertEqual(generation_outputs.shape[-1]		,  _a				)

																			__lowercase   :				List[str]						      =  jit(model.generate				)
																			__lowercase   :				List[str]						      =  jit_generate(_a		,  attention_mask=_a				).sequences

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







@require_flax
class 		lowerCAmelCase  (			unittest.TestCase      ):

					'''simple docstring'''

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


												"""simple docstring"""

												__lowercase   :				List[Any]						      =  AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-bert"""				)
												__lowercase   :				Optional[int]						      =  FlaxAutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-bert-flax-only"""				)

												__lowercase   :				Optional[Any]						      =  "Hello world"
												__lowercase   :				Optional[int]						      =  tokenizer(_a		,  return_tensors="""np"""				).input_ids

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

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

"""simple docstring"""




def     __magic_name__      ( __snake_case							:			int	,					__snake_case							:			int    )					->							Any:
       if b == 0:
              return 1
       if (b % 2) == 0:
              return actual_power(__snake_case	,					int(b / 2    )    ) * actual_power(__snake_case	,					int(b / 2    )    )
       else:
              return a * actual_power(__snake_case	,					int(b / 2    )    ) * actual_power(__snake_case	,					int(b / 2    )    )


def     __magic_name__      ( __snake_case							:			int	,					__snake_case							:			int    )					->							float:
       if b < 0:
              return 1 / actual_power(__snake_case	,					__snake_case    )
       return actual_power(__snake_case	,					__snake_case    )


if __name__ == "__main__":
   print(power(-2, -3))


