


"""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

 __lowercase    					=      """0.12"""  # assumed parallelism: 8


if is_torch_available():
 import torch


def      lowercase			(				A_  ,   A_  ,   A_=None       )->      List[Any]:
  '''simple docstring'''







  if rng is None:
    a						:		Union[str, Any]      							=			random.Random()

  a						:		Tuple      							=			1
  for dim in shape:
    total_dims *= dim

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

  a						:		Dict      							=			np.array(A_  ,   dtype=jnp.intaa       ).reshape(A_       )

  return output


def      lowercase			(				A_  ,   A_=None       )->      List[str]:
  '''simple docstring'''







  a						:		Optional[int]      							=			ids_tensor(A_  ,   vocab_size=2  ,   rng=A_       )
  # make sure that at least one token is attended to for each batch
  a						:		Tuple      							=			1
  return attn_mask






@require_flax
class 						_A  :







    """simple docstring"""




    UpperCAmelCase       :							Dict	=       None
    UpperCAmelCase       :							Dict	=       ()






    def 	__snake_case							(					self				:    Optional[int]):
      a							,		a						:		int      							=			self.model_tester.prepare_config_and_inputs_for_common()

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

      a						:		str      							=			jnp.ones_like(__UpperCAmelCase)
      a						:		str      							=			attention_mask[:max_batch_size, :sequence_length]

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






    @is_pt_flax_cross_test
    def 	__snake_case							(					self				:    Optional[Any]):
      a							,		a							,		a							,		a						:		Optional[int]      							=			self._get_input_ids_and_config()
      a						:		Union[str, Any]      							=			False
      a						:		str      							=			max_length
      a						:		Dict      							=			0

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

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

        a						:		Tuple      							=			flax_model.generate(__UpperCAmelCase).sequences
        a						:		Tuple      							=			pt_model.generate(torch.tensor(__UpperCAmelCase				, dtype=torch.long))

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

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






    def 	__snake_case							(					self				:    Dict):
      a							,		a							,		a							,		a						:		Dict      							=			self._get_input_ids_and_config()
      a						:		Optional[Any]      							=			False
      a						:		Dict      							=			max_length

      for model_class in self.all_generative_model_classes:
        a						:		Optional[int]      							=			model_class(__UpperCAmelCase)

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

        a						:		str      							=			jit(model.generate)
        a						:		List[str]      							=			jit_generate(__UpperCAmelCase).sequences

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






    def 	__snake_case							(					self				:    Optional[int]):
      a							,		a							,		a							,		a						:		Dict      							=			self._get_input_ids_and_config()
      a						:		Union[str, Any]      							=			True
      a						:		List[Any]      							=			max_length

      for model_class in self.all_generative_model_classes:
        a						:		Dict      							=			model_class(__UpperCAmelCase)

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

        a						:		Optional[int]      							=			jit(model.generate)
        a						:		Tuple      							=			jit_generate(__UpperCAmelCase).sequences

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






    def 	__snake_case							(					self				:    Union[str, Any]):
      a							,		a							,		a							,		a						:		List[Any]      							=			self._get_input_ids_and_config()
      a						:		List[str]      							=			False
      a						:		str      							=			max_length
      a						:		List[Any]      							=			2

      for model_class in self.all_generative_model_classes:
        a						:		Tuple      							=			model_class(__UpperCAmelCase)

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

        a						:		List[Any]      							=			jit(model.generate)
        a						:		Optional[Any]      							=			jit_generate(__UpperCAmelCase).sequences

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






    def 	__snake_case							(					self				:    int):
      a							,		a							,		a							,		a						:		Any      							=			self._get_input_ids_and_config()
      a						:		int      							=			False
      a						:		str      							=			max_length
      a						:		str      							=			2
      a						:		Optional[int]      							=			2

      for model_class in self.all_generative_model_classes:
        a						:		str      							=			model_class(__UpperCAmelCase)

        a						:		str      							=			model.generate(__UpperCAmelCase).sequences
        self.assertEqual(generation_outputs.shape[0]				, input_ids.shape[0] * config.num_return_sequences)






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

      for model_class in self.all_generative_model_classes:
        a						:		int      							=			model_class(__UpperCAmelCase)

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

        a						:		Optional[Any]      							=			jit(model.generate)
        a						:		Any      							=			jit_generate(__UpperCAmelCase).sequences

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






    def 	__snake_case							(					self				:    Optional[int]):
      a							,		a							,		a							,		a						:		str      							=			self._get_input_ids_and_config()
      a						:		Tuple      							=			max_length
      a						:		int      							=			1
      a						:		int      							=			8
      a						:		List[Any]      							=			9

      for model_class in self.all_generative_model_classes:
        a						:		List[Any]      							=			model_class(__UpperCAmelCase)

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

        a						:		Any      							=			jit(model.generate)
        a						:		List[str]      							=			jit_generate(__UpperCAmelCase).sequences

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






    def 	__snake_case							(					self				:    Dict):
      a							,		a							,		a							,		a						:		int      							=			self._get_input_ids_and_config()
      a						:		Optional[Any]      							=			max_length
      a						:		Dict      							=			2
      a						:		Tuple      							=			1
      a						:		Optional[Any]      							=			8
      a						:		Dict      							=			9

      for model_class in self.all_generative_model_classes:
        a						:		Any      							=			model_class(__UpperCAmelCase)

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

        a						:		Dict      							=			jit(model.generate)
        a						:		Tuple      							=			jit_generate(__UpperCAmelCase).sequences

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






    def 	__snake_case							(					self				:    Optional[int]):
      a							,		a							,		a							,		a						:		List[Any]      							=			self._get_input_ids_and_config()

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

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

      for model_class in self.all_generative_model_classes:
        a						:		Any      							=			model_class(__UpperCAmelCase)

        a						:		Any      							=			model.generate(__UpperCAmelCase				, attention_mask=__UpperCAmelCase).sequences
        self.assertEqual(generation_outputs.shape[-1]				, __UpperCAmelCase)

        a						:		List[Any]      							=			jit(model.generate)
        a						:		Tuple      							=			jit_generate(__UpperCAmelCase				, attention_mask=__UpperCAmelCase).sequences

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






    def 	__snake_case							(					self				:    List[Any]):
      a							,		a							,		a							,		a						:		List[Any]      							=			self._get_input_ids_and_config()

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

      a						:		Any      							=			True
      a						:		Union[str, Any]      							=			max_length

      for model_class in self.all_generative_model_classes:
        a						:		Dict      							=			model_class(__UpperCAmelCase)

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

        a						:		Dict      							=			jit(model.generate)
        a						:		Dict      							=			jit_generate(__UpperCAmelCase				, attention_mask=__UpperCAmelCase).sequences

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






    def 	__snake_case							(					self				:    Dict):
      a							,		a							,		a							,		a						:		Any      							=			self._get_input_ids_and_config()

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

      a						:		str      							=			2
      a						:		Optional[int]      							=			max_length

      for model_class in self.all_generative_model_classes:
        a						:		List[str]      							=			model_class(__UpperCAmelCase)

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

        a						:		str      							=			jit(model.generate)
        a						:		Tuple      							=			jit_generate(__UpperCAmelCase				, attention_mask=__UpperCAmelCase).sequences

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






@require_flax
class 						_A  (  unittest.TestCase  ):







    """simple docstring"""




    def 	__snake_case							(					self				:    Any):
      a						:		List[Any]      							=			AutoTokenizer.from_pretrained("hf-internal-testing/tiny-bert")
      a						:		int      							=			FlaxAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-bert-flax-only")

      a						:		Union[str, Any]      							=			"Hello world"
      a						:		str      							=			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"):
        a						:		Tuple      							=			{"foo": "bar"}
        model.generate(__UpperCAmelCase				, **__UpperCAmelCase)










"""simple docstring"""







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


__lowercase    					=      {
    """configuration_rag""": ["""RagConfig"""],
    """retrieval_rag""": ["""RagRetriever"""],
    """tokenization_rag""": ["""RagTokenizer"""],
}

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __lowercase    					=      [
     """RagModel""",
     """RagPreTrainedModel""",
     """RagSequenceForGeneration""",
     """RagTokenForGeneration""",
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __lowercase    					=      [
     """TFRagModel""",
     """TFRagPreTrainedModel""",
     """TFRagSequenceForGeneration""",
     """TFRagTokenForGeneration""",
 ]


if TYPE_CHECKING:
 from .configuration_rag import RagConfig
 from .retrieval_rag import RagRetriever
 from .tokenization_rag import RagTokenizer

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_rag import RagModel, RagPreTrainedModel, RagSequenceForGeneration, RagTokenForGeneration

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_rag import (
      TFRagModel,
      TFRagPreTrainedModel,
      TFRagSequenceForGeneration,
      TFRagTokenForGeneration,
  )

else:
 import sys

 __lowercase    					=      _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)






