






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

       UpperCamelCase    							=      '0.12'  # assumed parallelism: 8


if is_torch_available():
       import torch


def 				__lowerCamelCase     (							__lowerCAmelCase							:					Optional[Any]					,							__lowerCAmelCase							:					Union[str, Any]					,							__lowerCAmelCase							:					Optional[int]=None							)     ->  int:
   if rng is None:
      __UpperCamelCase	:			Dict					=					random.Random()

   __UpperCamelCase	:			Optional[Any]					=					1
   for dim in shape:
      total_dims *= dim

   __UpperCamelCase	:			List[str]					=					[]
   for _ in range(__lowerCAmelCase							):
      values.append(rng.randint(0					,							vocab_size - 1							)							)

   __UpperCamelCase	:			int					=					np.array(__lowerCAmelCase					,							dtype=jnp.intaa							).reshape(__lowerCAmelCase							)

   return output


def 				__lowerCamelCase     (							__lowerCAmelCase							:					Dict					,							__lowerCAmelCase							:					List[Any]=None							)     ->  List[str]:
   __UpperCamelCase	:			Union[str, Any]					=					ids_tensor(__lowerCAmelCase					,							vocab_size=2					,							rng=__lowerCAmelCase							)
   # make sure that at least one token is attended to for each batch
   __UpperCamelCase	:			int					=					1
   return attn_mask







@require_flax
class 		_A						:
  lowercase_						:			int						=					None
  lowercase_						:			Dict						=					()

  def    a					(    self						:	Dict			):
     """simple docstring"""

     __UpperCamelCase	,     __UpperCamelCase	:			Any					=					self.model_tester.prepare_config_and_inputs_for_common()

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

     __UpperCamelCase	:			Union[str, Any]					=					jnp.ones_like(lowerCamelCase__			)
     __UpperCamelCase	:			Any					=					attention_mask[:max_batch_size, :sequence_length]

     # generate max 5 tokens
     __UpperCamelCase	:			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()`
        __UpperCamelCase	:			str					=					config.eos_token_id
     return config, input_ids, attention_mask, max_length

  @is_pt_flax_cross_test
  def    a					(    self						:	int			):
     """simple docstring"""

     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	:			Optional[int]					=					self._get_input_ids_and_config()
     __UpperCamelCase	:			List[str]					=					False
     __UpperCamelCase	:			Dict					=					max_length
     __UpperCamelCase	:			Optional[int]					=					0

     for model_class in self.all_generative_model_classes:
        __UpperCamelCase	:			Any					=					model_class(lowerCamelCase__			)

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

        __UpperCamelCase	:			Any					=					flax_model.generate(lowerCamelCase__			).sequences
        __UpperCamelCase	:			str					=					pt_model.generate(torch.tensor(lowerCamelCase__   ,				dtype=torch.long			)			)

        if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]:
           __UpperCamelCase	:			Optional[int]					=					flax_generation_outputs[:, : pt_generation_outputs.shape[-1]]

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

  def    a					(    self						:	str			):
     """simple docstring"""

     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	:			Optional[Any]					=					self._get_input_ids_and_config()
     __UpperCamelCase	:			Optional[Any]					=					False
     __UpperCamelCase	:			int					=					max_length

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

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

        __UpperCamelCase	:			List[str]					=					jit(model.generate			)
        __UpperCamelCase	:			Optional[Any]					=					jit_generate(lowerCamelCase__			).sequences

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

  def    a					(    self						:	Tuple			):
     """simple docstring"""

     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	:			str					=					self._get_input_ids_and_config()
     __UpperCamelCase	:			Optional[Any]					=					True
     __UpperCamelCase	:			List[str]					=					max_length

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

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

        __UpperCamelCase	:			Union[str, Any]					=					jit(model.generate			)
        __UpperCamelCase	:			Any					=					jit_generate(lowerCamelCase__			).sequences

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

  def    a					(    self						:	Optional[Any]			):
     """simple docstring"""

     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	:			Optional[Any]					=					self._get_input_ids_and_config()
     __UpperCamelCase	:			Any					=					False
     __UpperCamelCase	:			Optional[Any]					=					max_length
     __UpperCamelCase	:			Tuple					=					2

     for model_class in self.all_generative_model_classes:
        __UpperCamelCase	:			Any					=					model_class(lowerCamelCase__			)

        __UpperCamelCase	:			Dict					=					model.generate(lowerCamelCase__			).sequences
        self.assertEqual(generation_outputs.shape[-1]   ,				lowerCamelCase__			)

        __UpperCamelCase	:			str					=					jit(model.generate			)
        __UpperCamelCase	:			int					=					jit_generate(lowerCamelCase__			).sequences

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

  def    a					(    self						:	str			):
     """simple docstring"""

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

     for model_class in self.all_generative_model_classes:
        __UpperCamelCase	:			List[Any]					=					model_class(lowerCamelCase__			)

        __UpperCamelCase	:			Optional[int]					=					model.generate(lowerCamelCase__			).sequences
        self.assertEqual(generation_outputs.shape[0]   ,				input_ids.shape[0] * config.num_return_sequences			)

  def    a					(    self						:	str			):
     """simple docstring"""

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

     for model_class in self.all_generative_model_classes:
        __UpperCamelCase	:			Optional[int]					=					model_class(lowerCamelCase__			)

        __UpperCamelCase	:			Optional[Any]					=					model.generate(lowerCamelCase__			).sequences
        self.assertEqual(generation_outputs.shape[-1]   ,				lowerCamelCase__			)

        __UpperCamelCase	:			Dict					=					jit(model.generate			)
        __UpperCamelCase	:			Any					=					jit_generate(lowerCamelCase__			).sequences

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

  def    a					(    self						:	Tuple			):
     """simple docstring"""

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

     for model_class in self.all_generative_model_classes:
        __UpperCamelCase	:			str					=					model_class(lowerCamelCase__			)

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

        __UpperCamelCase	:			str					=					jit(model.generate			)
        __UpperCamelCase	:			List[str]					=					jit_generate(lowerCamelCase__			).sequences

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

  def    a					(    self						:	List[Any]			):
     """simple docstring"""

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

     for model_class in self.all_generative_model_classes:
        __UpperCamelCase	:			Tuple					=					model_class(lowerCamelCase__			)

        __UpperCamelCase	:			int					=					model.generate(lowerCamelCase__			).sequences
        self.assertEqual(generation_outputs.shape[-1]   ,				lowerCamelCase__			)

        __UpperCamelCase	:			Tuple					=					jit(model.generate			)
        __UpperCamelCase	:			List[Any]					=					jit_generate(lowerCamelCase__			).sequences

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

  def    a					(    self						:	List[str]			):
     """simple docstring"""

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

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

     __UpperCamelCase	:			List[str]					=					False
     __UpperCamelCase	:			str					=					max_length

     for model_class in self.all_generative_model_classes:
        __UpperCamelCase	:			Optional[int]					=					model_class(lowerCamelCase__			)

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

        __UpperCamelCase	:			str					=					jit(model.generate			)
        __UpperCamelCase	:			int					=					jit_generate(lowerCamelCase__   ,				attention_mask=lowerCamelCase__			).sequences

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

  def    a					(    self						:	int			):
     """simple docstring"""

     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	:			List[str]					=					self._get_input_ids_and_config()

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

     __UpperCamelCase	:			Union[str, Any]					=					True
     __UpperCamelCase	:			List[str]					=					max_length

     for model_class in self.all_generative_model_classes:
        __UpperCamelCase	:			str					=					model_class(lowerCamelCase__			)

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

        __UpperCamelCase	:			Dict					=					jit(model.generate			)
        __UpperCamelCase	:			List[str]					=					jit_generate(lowerCamelCase__   ,				attention_mask=lowerCamelCase__			).sequences

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

  def    a					(    self						:	List[Any]			):
     """simple docstring"""

     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	:			str					=					self._get_input_ids_and_config()

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

     __UpperCamelCase	:			int					=					2
     __UpperCamelCase	:			List[Any]					=					max_length

     for model_class in self.all_generative_model_classes:
        __UpperCamelCase	:			Optional[int]					=					model_class(lowerCamelCase__			)

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

        __UpperCamelCase	:			Optional[Any]					=					jit(model.generate			)
        __UpperCamelCase	:			int					=					jit_generate(lowerCamelCase__   ,				attention_mask=lowerCamelCase__			).sequences

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







@require_flax
class 		_A						( unittest.TestCase ):

  def    a					(    self						:	List[Any]			):
     """simple docstring"""

     __UpperCamelCase	:			str					=					AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-bert"""			)
     __UpperCamelCase	:			Union[str, Any]					=					FlaxAutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-bert-flax-only"""			)

     __UpperCamelCase	:			Optional[int]					=					"""Hello world"""
     __UpperCamelCase	:			Dict					=					tokenizer(lowerCamelCase__   ,				return_tensors="""np"""			).input_ids

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

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











from __future__ import annotations


def 				__lowerCamelCase     (							__lowerCAmelCase							:					str					,							__lowerCAmelCase							:					Tuple					,							__lowerCAmelCase							:					int					,							__lowerCAmelCase							:					Tuple							)     ->  Optional[Any]:  # noqa: E741
   while r - l > 1:
      __UpperCamelCase	:			int					=					(l + r) // 2
      if v[m] >= key:
         __UpperCamelCase	:			Dict					=					m
      else:
         __UpperCamelCase	:			Optional[Any]					=					m  # noqa: E741
   return r


def 				__lowerCamelCase     (							__lowerCAmelCase							:					list[int]							)     ->  int:
   if len(__lowerCAmelCase							) == 0:
      return 0

   __UpperCamelCase	:			Optional[Any]					=					[0] * len(__lowerCAmelCase							)
   __UpperCamelCase	:			Optional[int]					=					1

   __UpperCamelCase	:			Union[str, Any]					=					v[0]

   for i in range(1					,							len(__lowerCAmelCase							)							):
      if v[i] < tail[0]:
         __UpperCamelCase	:			Dict					=					v[i]
      elif v[i] > tail[length - 1]:
         __UpperCamelCase	:			Optional[int]					=					v[i]
         length += 1
      else:
         __UpperCamelCase	:			List[Any]					=					v[i]

   return length


if __name__ == "__main__":
       import doctest

       doctest.testmod()



