
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

  lowercase  : Union[str, Any]						   =							"""0.12"""  # assumed parallelism: 8


if is_torch_available():
  import torch







def 						_snake_case(  SCREAMING_SNAKE_CASE__ ,     SCREAMING_SNAKE_CASE__ ,     SCREAMING_SNAKE_CASE__=None    )			->   List[str]:
       if rng is None:
              lowercase							:					Any	     =				random.Random()

       lowercase							:					Any	     =				1
       for dim in shape:
              total_dims *= dim

       lowercase							:					Optional[Any]	     =				[]
       for _ in range(SCREAMING_SNAKE_CASE__    ):
              values.append(rng.randint(0 ,     vocab_size - 1    )    )

       lowercase							:					str	     =				np.array(SCREAMING_SNAKE_CASE__ ,     dtype=jnp.intaa    ).reshape(SCREAMING_SNAKE_CASE__    )

       return output







def 						_snake_case(  SCREAMING_SNAKE_CASE__ ,     SCREAMING_SNAKE_CASE__=None    )			->   List[str]:
       lowercase							:					Any	     =				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
       lowercase							:					List[str]	     =				1
       return attn_mask

@require_flax
class __snake_case		:
    _a		:						Optional[Any]=    None
    _a		:						Any=    ()
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           lowercase     ,      lowercase							:					Optional[int]	     =				self.model_tester.prepare_config_and_inputs_for_common()

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

           lowercase							:					Optional[Any]	     =				jnp.ones_like(snake_case				)
           lowercase							:					Tuple	     =				attention_mask[:max_batch_size, :sequence_length]

           # generate max 5 tokens
           lowercase							:					List[str]	     =				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							:					Union[str, Any]	     =				config.eos_token_id
           return config, input_ids, attention_mask, max_length
    @is_pt_flax_cross_test
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           lowercase     ,      lowercase     ,      lowercase     ,      lowercase							:					Optional[int]	     =				self._get_input_ids_and_config()
           lowercase							:					Optional[int]	     =				False
           lowercase							:					int	     =				max_length
           lowercase							:					Union[str, Any]	     =				0

           for model_class in self.all_generative_model_classes:
                  lowercase							:					Dict	     =				model_class(snake_case				)

                  lowercase							:					Any	     =				model_class.__name__[4:]  # Skip the "Flax" at the beginning
                  lowercase							:					Optional[int]	     =				getattr(snake_case		,snake_case				)
                  lowercase							:					Optional[int]	     =				pt_model_class(snake_case				).eval()
                  lowercase							:					Optional[int]	     =				load_flax_weights_in_pytorch_model(snake_case		,flax_model.params				)

                  lowercase							:					Any	     =				flax_model.generate(snake_case				).sequences
                  lowercase							:					Optional[Any]	     =				pt_model.generate(torch.tensor(snake_case		,dtype=torch.long				)				)

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

                  self.assertListEqual(pt_generation_outputs.numpy().tolist()		,flax_generation_outputs.tolist()				)
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           lowercase     ,      lowercase     ,      lowercase     ,      lowercase							:					Optional[int]	     =				self._get_input_ids_and_config()
           lowercase							:					int	     =				False
           lowercase							:					str	     =				max_length

           for model_class in self.all_generative_model_classes:
                  lowercase							:					Optional[Any]	     =				model_class(snake_case				)

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

                  lowercase							:					str	     =				jit(model.generate				)
                  lowercase							:					Tuple	     =				jit_generate(snake_case				).sequences

                  self.assertListEqual(generation_outputs.tolist()		,jit_generation_outputs.tolist()				)
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           lowercase     ,      lowercase     ,      lowercase     ,      lowercase							:					Tuple	     =				self._get_input_ids_and_config()
           lowercase							:					List[Any]	     =				True
           lowercase							:					Optional[Any]	     =				max_length

           for model_class in self.all_generative_model_classes:
                  lowercase							:					Dict	     =				model_class(snake_case				)

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

                  lowercase							:					List[str]	     =				jit(model.generate				)
                  lowercase							:					Any	     =				jit_generate(snake_case				).sequences

                  self.assertListEqual(generation_outputs.tolist()		,jit_generation_outputs.tolist()				)
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







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

           for model_class in self.all_generative_model_classes:
                  lowercase							:					str	     =				model_class(snake_case				)

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

                  lowercase							:					List[Any]	     =				jit(model.generate				)
                  lowercase							:					List[str]	     =				jit_generate(snake_case				).sequences

                  self.assertListEqual(generation_outputs.tolist()		,jit_generation_outputs.tolist()				)
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           lowercase     ,      lowercase     ,      lowercase     ,      lowercase							:					int	     =				self._get_input_ids_and_config()
           lowercase							:					str	     =				False
           lowercase							:					int	     =				max_length
           lowercase							:					str	     =				2
           lowercase							:					int	     =				2

           for model_class in self.all_generative_model_classes:
                  lowercase							:					Any	     =				model_class(snake_case				)

                  lowercase							:					Any	     =				model.generate(snake_case				).sequences
                  self.assertEqual(generation_outputs.shape[0]		,input_ids.shape[0] * config.num_return_sequences				)
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







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

           for model_class in self.all_generative_model_classes:
                  lowercase							:					List[Any]	     =				model_class(snake_case				)

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

                  lowercase							:					Tuple	     =				jit(model.generate				)
                  lowercase							:					Any	     =				jit_generate(snake_case				).sequences

                  self.assertListEqual(generation_outputs.tolist()		,jit_generation_outputs.tolist()				)
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           lowercase     ,      lowercase     ,      lowercase     ,      lowercase							:					List[str]	     =				self._get_input_ids_and_config()
           lowercase							:					List[str]	     =				max_length
           lowercase							:					Dict	     =				1
           lowercase							:					Any	     =				8
           lowercase							:					Union[str, Any]	     =				9

           for model_class in self.all_generative_model_classes:
                  lowercase							:					List[str]	     =				model_class(snake_case				)

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

                  lowercase							:					Optional[Any]	     =				jit(model.generate				)
                  lowercase							:					str	     =				jit_generate(snake_case				).sequences

                  self.assertListEqual(generation_outputs.tolist()		,jit_generation_outputs.tolist()				)
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           lowercase     ,      lowercase     ,      lowercase     ,      lowercase							:					Union[str, Any]	     =				self._get_input_ids_and_config()
           lowercase							:					Any	     =				max_length
           lowercase							:					List[str]	     =				2
           lowercase							:					str	     =				1
           lowercase							:					Dict	     =				8
           lowercase							:					Any	     =				9

           for model_class in self.all_generative_model_classes:
                  lowercase							:					int	     =				model_class(snake_case				)

                  lowercase							:					Union[str, Any]	     =				model.generate(snake_case				).sequences
                  self.assertEqual(generation_outputs.shape[-1]		,snake_case				)

                  lowercase							:					str	     =				jit(model.generate				)
                  lowercase							:					str	     =				jit_generate(snake_case				).sequences

                  self.assertListEqual(generation_outputs.tolist()		,jit_generation_outputs.tolist()				)
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           lowercase     ,      lowercase     ,      lowercase     ,      lowercase							:					Optional[Any]	     =				self._get_input_ids_and_config()

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

           lowercase							:					Tuple	     =				False
           lowercase							:					List[str]	     =				max_length

           for model_class in self.all_generative_model_classes:
                  lowercase							:					Optional[Any]	     =				model_class(snake_case				)

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

                  lowercase							:					Optional[int]	     =				jit(model.generate				)
                  lowercase							:					Optional[int]	     =				jit_generate(snake_case		,attention_mask=snake_case				).sequences

                  self.assertListEqual(generation_outputs.tolist()		,jit_generation_outputs.tolist()				)
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           lowercase     ,      lowercase     ,      lowercase     ,      lowercase							:					List[Any]	     =				self._get_input_ids_and_config()

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

           lowercase							:					Any	     =				True
           lowercase							:					Dict	     =				max_length

           for model_class in self.all_generative_model_classes:
                  lowercase							:					List[str]	     =				model_class(snake_case				)

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

                  lowercase							:					str	     =				jit(model.generate				)
                  lowercase							:					Optional[int]	     =				jit_generate(snake_case		,attention_mask=snake_case				).sequences

                  self.assertListEqual(generation_outputs.tolist()		,jit_generation_outputs.tolist()				)
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           lowercase     ,      lowercase     ,      lowercase     ,      lowercase							:					List[Any]	     =				self._get_input_ids_and_config()

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

           lowercase							:					Dict	     =				2
           lowercase							:					Dict	     =				max_length

           for model_class in self.all_generative_model_classes:
                  lowercase							:					Dict	     =				model_class(snake_case				)

                  lowercase							:					Dict	     =				model.generate(snake_case		,attention_mask=snake_case				).sequences
                  self.assertEqual(generation_outputs.shape[-1]		,snake_case				)

                  lowercase							:					int	     =				jit(model.generate				)
                  lowercase							:					Optional[int]	     =				jit_generate(snake_case		,attention_mask=snake_case				).sequences

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






@require_flax
class __snake_case		(				unittest.TestCase					):
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           lowercase							:					str	     =				AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-bert"""				)
           lowercase							:					str	     =				FlaxAutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-bert-flax-only"""				)

           lowercase							:					Optional[int]	     =				"""Hello world"""
           lowercase							:					Tuple	     =				tokenizer(snake_case		,return_tensors="""np"""				).input_ids

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

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


from collections.abc import Callable

import numpy as np







def 						_snake_case(  SCREAMING_SNAKE_CASE__ ,     SCREAMING_SNAKE_CASE__ ,     SCREAMING_SNAKE_CASE__ ,     SCREAMING_SNAKE_CASE__ ,     SCREAMING_SNAKE_CASE__    )			->   np.array:
       lowercase							:					Optional[int]	     =				int(np.ceil((x_end - xa) / step_size    )    )
       lowercase							:					List[Any]	     =				np.zeros((n + 1,)    )
       lowercase							:					Optional[int]	     =				ya
       lowercase							:					Optional[int]	     =				xa

       for k in range(SCREAMING_SNAKE_CASE__    ):
              lowercase							:					str	     =				y[k] + step_size * ode_func(SCREAMING_SNAKE_CASE__ ,     y[k]    )
              lowercase							:					Union[str, Any]	     =				y[k] + (
                  (step_size / 2) * (ode_func(SCREAMING_SNAKE_CASE__ ,     y[k]    ) + ode_func(x + step_size ,     SCREAMING_SNAKE_CASE__    ))
              )
              x += step_size

       return y


if __name__ == "__main__":
  import doctest

  doctest.testmod()
