


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

import os
import tempfile
import unittest
import uuid
from pathlib import Path

from transformers.testing_utils import get_tests_dir, require_soundfile, require_torch, require_vision
from transformers.tools.agent_types import AgentAudio, AgentImage, AgentText
from transformers.utils import is_soundfile_availble, is_torch_available, is_vision_available


if is_torch_available():
     import torch

if is_soundfile_availble():
     import soundfile as sf

if is_vision_available():
     from PIL import Image




def __lowercase			(							_a=""    ):
    snake_case_ :     List[str]				=	tempfile.mkdtemp()
    return os.path.join(_a		,			str(uuid.uuida()    ) + suffix    )




@require_soundfile
@require_torch
class        _UpperCAmelCase       (			unittest.TestCase):

 def 						_snake_case					(     self		:       str				):
     snake_case_ :     int				=	torch.rand(12  ,     dtype=torch.floataa				) - 0.5
     snake_case_ :     Optional[int]				=	AgentAudio(lowercase_				)
     snake_case_ :     List[str]				=	str(agent_type.to_string()				)

     # Ensure that the tensor and the agent_type's tensor are the same
     self.assertTrue(torch.allclose(lowercase_  ,     agent_type.to_raw()  ,     atol=1E-4				)				)

     del agent_type

     # Ensure the path remains even after the object deletion
     self.assertTrue(os.path.exists(lowercase_				)				)

     # Ensure that the file contains the same value as the original tensor
     snake_case_,	snake_case_ :     int				=	sf.read(lowercase_				)
     self.assertTrue(torch.allclose(lowercase_  ,     torch.tensor(lowercase_				)  ,     atol=1E-4				)				)






 def 						_snake_case					(     self		:       Optional[int]				):
     snake_case_ :     Any				=	torch.rand(12  ,     dtype=torch.floataa				) - 0.5
     snake_case_ :     List[str]				=	get_new_path(suffix='''.wav'''				)
     sf.write(lowercase_  ,     lowercase_  ,     16000				)

     snake_case_ :     Tuple				=	AgentAudio(lowercase_				)

     self.assertTrue(torch.allclose(lowercase_  ,     agent_type.to_raw()  ,     atol=1E-4				)				)
     self.assertEqual(agent_type.to_string()  ,     lowercase_				)





@require_vision
@require_torch
class        _UpperCAmelCase       (			unittest.TestCase):

 def 						_snake_case					(     self		:       Tuple				):
     snake_case_ :     List[Any]				=	torch.randint(0  ,     256  ,     (64, 64, 3)				)
     snake_case_ :     str				=	AgentImage(lowercase_				)
     snake_case_ :     Union[str, Any]				=	str(agent_type.to_string()				)

     # Ensure that the tensor and the agent_type's tensor are the same
     self.assertTrue(torch.allclose(lowercase_  ,     agent_type._tensor  ,     atol=1E-4				)				)

     self.assertIsInstance(agent_type.to_raw()  ,     Image.Image				)

     # Ensure the path remains even after the object deletion
     del agent_type
     self.assertTrue(os.path.exists(lowercase_				)				)

 def 						_snake_case					(     self		:       str				):
     snake_case_ :     Any				=	Path(get_tests_dir('''fixtures/tests_samples/COCO'''				)				) / '''000000039769.png'''
     snake_case_ :     Optional[int]				=	Image.open(lowercase_				)
     snake_case_ :     Tuple				=	AgentImage(lowercase_				)

     self.assertTrue(path.samefile(agent_type.to_string()				)				)
     self.assertTrue(image == agent_type.to_raw()				)

     # Ensure the path remains even after the object deletion
     del agent_type
     self.assertTrue(os.path.exists(lowercase_				)				)






 def 						_snake_case					(     self		:       str				):
     snake_case_ :     int				=	Path(get_tests_dir('''fixtures/tests_samples/COCO'''				)				) / '''000000039769.png'''
     snake_case_ :     Dict				=	Image.open(lowercase_				)
     snake_case_ :     List[str]				=	AgentImage(lowercase_				)

     self.assertFalse(path.samefile(agent_type.to_string()				)				)
     self.assertTrue(image == agent_type.to_raw()				)

     # Ensure the path remains even after the object deletion
     del agent_type
     self.assertTrue(os.path.exists(lowercase_				)				)





class        _UpperCAmelCase       (			unittest.TestCase):

 def 						_snake_case					(     self		:       Any				):
     snake_case_ :     Tuple				=	'''Hey!'''
     snake_case_ :     Optional[Any]				=	AgentText(lowercase_				)

     self.assertEqual(lowercase_  ,     agent_type.to_string()				)
     self.assertEqual(lowercase_  ,     agent_type.to_raw()				)
     self.assertEqual(lowercase_  ,     lowercase_				)





