






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

     SCREAMING_SNAKE_CASE__     =       '0.12'  # assumed parallelism: 8


if is_torch_available():
     import torch







def    lowercase__			(				__UpperCamelCase		,						__UpperCamelCase		,						__UpperCamelCase=None   )->			List[Any]:




      if rng is None:
            UpperCamelCase      = random.Random()

      UpperCamelCase      = 1
      for dim in shape:
            total_dims *= dim

      UpperCamelCase      = []
      for _ in range(_a   ):
            values.append(rng.randint(0		,						vocab_size - 1   )   )

      UpperCamelCase      = np.array(_a		,						dtype=jnp.intaa   ).reshape(_a   )

      return output







def    lowercase__			(				__UpperCamelCase		,						__UpperCamelCase=None   )->			Any:
      UpperCamelCase      = ids_tensor(_a		,						vocab_size=2		,						rng=_a   )
      # make sure that at least one token is attended to for each batch
      UpperCamelCase      = 1
      return attn_mask






@require_flax
class     a_						:
  lowercase							=				None
  lowercase							=				()

  def   A__						(				self				)  ->				Optional[Any]:



        """simple docstring"""






        UpperCamelCase      = self.model_tester.prepare_config_and_inputs_for_common()

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

        UpperCamelCase      = jnp.ones_like(lowerCamelCase_				)
        UpperCamelCase      = attention_mask[:max_batch_size, :sequence_length]

        # generate max 5 tokens
        UpperCamelCase      = 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      = config.eos_token_id
        return config, input_ids, attention_mask, max_length

  @is_pt_flax_cross_test
  def   A__						(				self				)  ->				Optional[int]:



        """simple docstring"""






        UpperCamelCase      = self._get_input_ids_and_config()
        UpperCamelCase      = False
        UpperCamelCase      = max_length
        UpperCamelCase      = 0

        for model_class in self.all_generative_model_classes:
              UpperCamelCase      = model_class(lowerCamelCase_				)

              UpperCamelCase      = model_class.__name__[4:]  # Skip the "Flax" at the beginning
              UpperCamelCase      = getattr(lowerCamelCase_      ,		lowerCamelCase_				)
              UpperCamelCase      = pt_model_class(lowerCamelCase_				).eval()
              UpperCamelCase      = load_flax_weights_in_pytorch_model(lowerCamelCase_      ,		flax_model.params				)

              UpperCamelCase      = flax_model.generate(lowerCamelCase_				).sequences
              UpperCamelCase      = pt_model.generate(torch.tensor(lowerCamelCase_      ,		dtype=torch.long				)				)

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

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

  def   A__						(				self				)  ->				int:



        """simple docstring"""






        UpperCamelCase      = self._get_input_ids_and_config()
        UpperCamelCase      = False
        UpperCamelCase      = max_length

        for model_class in self.all_generative_model_classes:
              UpperCamelCase      = model_class(lowerCamelCase_				)

              UpperCamelCase      = model.generate(lowerCamelCase_				).sequences
              self.assertEqual(generation_outputs.shape[-1]      ,		lowerCamelCase_				)

              UpperCamelCase      = jit(model.generate				)
              UpperCamelCase      = jit_generate(lowerCamelCase_				).sequences

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

  def   A__						(				self				)  ->				str:



        """simple docstring"""






        UpperCamelCase      = self._get_input_ids_and_config()
        UpperCamelCase      = True
        UpperCamelCase      = max_length

        for model_class in self.all_generative_model_classes:
              UpperCamelCase      = model_class(lowerCamelCase_				)

              UpperCamelCase      = model.generate(lowerCamelCase_				).sequences
              self.assertEqual(generation_outputs.shape[-1]      ,		lowerCamelCase_				)

              UpperCamelCase      = jit(model.generate				)
              UpperCamelCase      = jit_generate(lowerCamelCase_				).sequences

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

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



        """simple docstring"""






        UpperCamelCase      = self._get_input_ids_and_config()
        UpperCamelCase      = False
        UpperCamelCase      = max_length
        UpperCamelCase      = 2

        for model_class in self.all_generative_model_classes:
              UpperCamelCase      = model_class(lowerCamelCase_				)

              UpperCamelCase      = model.generate(lowerCamelCase_				).sequences
              self.assertEqual(generation_outputs.shape[-1]      ,		lowerCamelCase_				)

              UpperCamelCase      = jit(model.generate				)
              UpperCamelCase      = jit_generate(lowerCamelCase_				).sequences

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

  def   A__						(				self				)  ->				Any:



        """simple docstring"""






        UpperCamelCase      = self._get_input_ids_and_config()
        UpperCamelCase      = False
        UpperCamelCase      = max_length
        UpperCamelCase      = 2
        UpperCamelCase      = 2

        for model_class in self.all_generative_model_classes:
              UpperCamelCase      = model_class(lowerCamelCase_				)

              UpperCamelCase      = model.generate(lowerCamelCase_				).sequences
              self.assertEqual(generation_outputs.shape[0]      ,		input_ids.shape[0] * config.num_return_sequences				)

  def   A__						(				self				)  ->				int:



        """simple docstring"""






        UpperCamelCase      = self._get_input_ids_and_config()
        UpperCamelCase      = True
        UpperCamelCase      = max_length
        UpperCamelCase      = 0.8
        UpperCamelCase      = 10
        UpperCamelCase      = 0.3
        UpperCamelCase      = 1
        UpperCamelCase      = 8
        UpperCamelCase      = 9

        for model_class in self.all_generative_model_classes:
              UpperCamelCase      = model_class(lowerCamelCase_				)

              UpperCamelCase      = model.generate(lowerCamelCase_				).sequences
              self.assertEqual(generation_outputs.shape[-1]      ,		lowerCamelCase_				)

              UpperCamelCase      = jit(model.generate				)
              UpperCamelCase      = jit_generate(lowerCamelCase_				).sequences

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

  def   A__						(				self				)  ->				str:



        """simple docstring"""






        UpperCamelCase      = self._get_input_ids_and_config()
        UpperCamelCase      = max_length
        UpperCamelCase      = 1
        UpperCamelCase      = 8
        UpperCamelCase      = 9

        for model_class in self.all_generative_model_classes:
              UpperCamelCase      = model_class(lowerCamelCase_				)

              UpperCamelCase      = model.generate(lowerCamelCase_				).sequences
              self.assertEqual(generation_outputs.shape[-1]      ,		lowerCamelCase_				)

              UpperCamelCase      = jit(model.generate				)
              UpperCamelCase      = jit_generate(lowerCamelCase_				).sequences

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

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



        """simple docstring"""






        UpperCamelCase      = self._get_input_ids_and_config()
        UpperCamelCase      = max_length
        UpperCamelCase      = 2
        UpperCamelCase      = 1
        UpperCamelCase      = 8
        UpperCamelCase      = 9

        for model_class in self.all_generative_model_classes:
              UpperCamelCase      = model_class(lowerCamelCase_				)

              UpperCamelCase      = model.generate(lowerCamelCase_				).sequences
              self.assertEqual(generation_outputs.shape[-1]      ,		lowerCamelCase_				)

              UpperCamelCase      = jit(model.generate				)
              UpperCamelCase      = jit_generate(lowerCamelCase_				).sequences

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

  def   A__						(				self				)  ->				Any:



        """simple docstring"""






        UpperCamelCase      = self._get_input_ids_and_config()

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

        UpperCamelCase      = False
        UpperCamelCase      = max_length

        for model_class in self.all_generative_model_classes:
              UpperCamelCase      = model_class(lowerCamelCase_				)

              UpperCamelCase      = model.generate(lowerCamelCase_      ,		attention_mask=lowerCamelCase_				).sequences
              self.assertEqual(generation_outputs.shape[-1]      ,		lowerCamelCase_				)

              UpperCamelCase      = jit(model.generate				)
              UpperCamelCase      = jit_generate(lowerCamelCase_      ,		attention_mask=lowerCamelCase_				).sequences

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

  def   A__						(				self				)  ->				Union[str, Any]:



        """simple docstring"""






        UpperCamelCase      = self._get_input_ids_and_config()

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

        UpperCamelCase      = True
        UpperCamelCase      = max_length

        for model_class in self.all_generative_model_classes:
              UpperCamelCase      = model_class(lowerCamelCase_				)

              UpperCamelCase      = model.generate(lowerCamelCase_      ,		attention_mask=lowerCamelCase_				).sequences
              self.assertEqual(generation_outputs.shape[-1]      ,		lowerCamelCase_				)

              UpperCamelCase      = jit(model.generate				)
              UpperCamelCase      = jit_generate(lowerCamelCase_      ,		attention_mask=lowerCamelCase_				).sequences

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


  def   A__						(				self				)  ->				List[Any]:



        """simple docstring"""






        UpperCamelCase      = self._get_input_ids_and_config()

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

        UpperCamelCase      = 2
        UpperCamelCase      = max_length

        for model_class in self.all_generative_model_classes:
              UpperCamelCase      = model_class(lowerCamelCase_				)

              UpperCamelCase      = model.generate(lowerCamelCase_      ,		attention_mask=lowerCamelCase_				).sequences
              self.assertEqual(generation_outputs.shape[-1]      ,		lowerCamelCase_				)

              UpperCamelCase      = jit(model.generate				)
              UpperCamelCase      = 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				)  ->				Optional[Any]:



        """simple docstring"""






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

        UpperCamelCase      = """Hello world"""
        UpperCamelCase      = 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      = {"""foo""": """bar"""}
              model.generate(lowerCamelCase_      ,		**lowerCamelCase_				)








'''simple docstring'''

import functools
import logging
import os
import sys
import threading
from logging import (
    CRITICAL,  # NOQA
    DEBUG,  # NOQA
    ERROR,  # NOQA
    FATAL,  # NOQA
    INFO,  # NOQA
    NOTSET,  # NOQA
    WARN,  # NOQA
    WARNING,  # NOQA
)
from typing import Optional

import huggingface_hub.utils as hf_hub_utils
from tqdm import auto as tqdm_lib


SCREAMING_SNAKE_CASE__     =       threading.Lock()
SCREAMING_SNAKE_CASE__     =       None

SCREAMING_SNAKE_CASE__     =       {
    'debug': logging.DEBUG,
    'info': logging.INFO,
    'warning': logging.WARNING,
    'error': logging.ERROR,
    'critical': logging.CRITICAL,
}

SCREAMING_SNAKE_CASE__     =       logging.WARNING

SCREAMING_SNAKE_CASE__     =       True







def    lowercase__			(				)->			Optional[int]:
      UpperCamelCase      = os.getenv("""TRANSFORMERS_VERBOSITY"""		,						__UpperCamelCase   )
      if env_level_str:
            if env_level_str in log_levels:
                  return log_levels[env_level_str]
            else:
                  logging.getLogger().warning(
                      F"Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, "
                      F"has to be one of: { ', '.join(log_levels.keys()   ) }"   )
      return _default_log_level







def    lowercase__			(				)->			str:
      return __name__.split("""."""   )[0]







def    lowercase__			(				)->			logging.Logger:
      return logging.getLogger(_get_library_name()   )







def    lowercase__			(				)->			None:
      global _default_handler

      with _lock:
            if _default_handler:
                  # This library has already configured the library root logger.
                  return
            UpperCamelCase      = logging.StreamHandler()  # Set sys.stderr as stream.
            UpperCamelCase      = sys.stderr.flush

            # Apply our default configuration to the library root logger.
            UpperCamelCase      = _get_library_root_logger()
            library_root_logger.addHandler(_default_handler   )
            library_root_logger.setLevel(_get_default_logging_level()   )
            UpperCamelCase      = False







def    lowercase__			(				)->			None:
      global _default_handler

      with _lock:
            if not _default_handler:
                  return

            UpperCamelCase      = _get_library_root_logger()
            library_root_logger.removeHandler(_default_handler   )
            library_root_logger.setLevel(logging.NOTSET   )
            UpperCamelCase      = None







def    lowercase__			(				)->			Tuple:
      return log_levels







def    lowercase__			(				__UpperCamelCase = None   )->			logging.Logger:

      if name is None:
            UpperCamelCase      = _get_library_name()

      _configure_library_root_logger()
      return logging.getLogger(__UpperCamelCase   )







def    lowercase__			(				)->			int:
      _configure_library_root_logger()
      return _get_library_root_logger().getEffectiveLevel()







def    lowercase__			(				__UpperCamelCase   )->			None:
      _configure_library_root_logger()
      _get_library_root_logger().setLevel(__UpperCamelCase   )







def    lowercase__			(				)->			Tuple:
      return set_verbosity(__UpperCamelCase   )







def    lowercase__			(				)->			Union[str, Any]:
      return set_verbosity(__UpperCamelCase   )







def    lowercase__			(				)->			Optional[int]:
      return set_verbosity(__UpperCamelCase   )







def    lowercase__			(				)->			Tuple:
      return set_verbosity(__UpperCamelCase   )







def    lowercase__			(				)->			None:
      _configure_library_root_logger()

      assert _default_handler is not None
      _get_library_root_logger().removeHandler(_default_handler   )







def    lowercase__			(				)->			None:
      _configure_library_root_logger()

      assert _default_handler is not None
      _get_library_root_logger().addHandler(_default_handler   )







def    lowercase__			(				__UpperCamelCase   )->			None:
      _configure_library_root_logger()

      assert handler is not None
      _get_library_root_logger().addHandler(__UpperCamelCase   )







def    lowercase__			(				__UpperCamelCase   )->			None:
      _configure_library_root_logger()

      assert handler is not None and handler not in _get_library_root_logger().handlers
      _get_library_root_logger().removeHandler(__UpperCamelCase   )







def    lowercase__			(				)->			None:
      _configure_library_root_logger()
      UpperCamelCase      = False







def    lowercase__			(				)->			None:
      _configure_library_root_logger()
      UpperCamelCase      = True







def    lowercase__			(				)->			None:
      UpperCamelCase      = _get_library_root_logger().handlers

      for handler in handlers:
            UpperCamelCase      = logging.Formatter("""[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s"""   )
            handler.setFormatter(__UpperCamelCase   )







def    lowercase__			(				)->			None:
      UpperCamelCase      = _get_library_root_logger().handlers

      for handler in handlers:
            handler.setFormatter(__UpperCamelCase   )







def    lowercase__			(				self		,						*__UpperCamelCase		,						**__UpperCamelCase   )->			Tuple:
      UpperCamelCase      = os.getenv("""TRANSFORMERS_NO_ADVISORY_WARNINGS"""		,						__UpperCamelCase   )
      if no_advisory_warnings:
            return
      self.warning(*__UpperCamelCase		,						**__UpperCamelCase   )


SCREAMING_SNAKE_CASE__     =       warning_advice







@functools.lru_cache(__UpperCamelCase   )
def    lowercase__			(				self		,						*__UpperCamelCase		,						**__UpperCamelCase   )->			Optional[Any]:
      self.warning(*__UpperCamelCase		,						**__UpperCamelCase   )


SCREAMING_SNAKE_CASE__     =       warning_once






class     a_						:

  def __init__(				self      ,		*_SCREAMING_SNAKE_CASE      ,		**_SCREAMING_SNAKE_CASE				)  ->				Optional[int]:  # pylint: disable=unused-argument



        """simple docstring"""






        UpperCamelCase      = args[0] if args else None

  def __iter__(				self				)  ->				List[Any]:



        """simple docstring"""






        return iter(self._iterator				)

  def __getattr__(				self      ,		_SCREAMING_SNAKE_CASE				)  ->				List[Any]:



        """simple docstring"""






        def empty_fn(*_SCREAMING_SNAKE_CASE      ,		**_SCREAMING_SNAKE_CASE				):  # pylint: disable=unused-argument
              return

        return empty_fn

  def __enter__(				self				)  ->				Dict:



        """simple docstring"""






        return self


  def __exit__(				self      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE				)  ->				List[Any]:



        """simple docstring"""






        return






class     a_						:

  def __call__(				self      ,		*_SCREAMING_SNAKE_CASE      ,		**_SCREAMING_SNAKE_CASE				)  ->				str:



        """simple docstring"""






        if _tqdm_active:
              return tqdm_lib.tqdm(*_SCREAMING_SNAKE_CASE      ,		**_SCREAMING_SNAKE_CASE				)
        else:
              return EmptyTqdm(*_SCREAMING_SNAKE_CASE      ,		**_SCREAMING_SNAKE_CASE				)

  def   A__						(				self      ,		*_SCREAMING_SNAKE_CASE      ,		**_SCREAMING_SNAKE_CASE				)  ->				Optional[Any]:



        """simple docstring"""






        UpperCamelCase      = None
        if _tqdm_active:
              return tqdm_lib.tqdm.set_lock(*_SCREAMING_SNAKE_CASE      ,		**_SCREAMING_SNAKE_CASE				)


  def   A__						(				self				)  ->				Union[str, Any]:



        """simple docstring"""






        if _tqdm_active:
              return tqdm_lib.tqdm.get_lock()


SCREAMING_SNAKE_CASE__     =       _tqdm_cls()







def    lowercase__			(				)->			bool:
      global _tqdm_active
      return bool(_tqdm_active   )







def    lowercase__			(				)->			Optional[Any]:
      global _tqdm_active
      UpperCamelCase      = True
      hf_hub_utils.enable_progress_bars()







def    lowercase__			(				)->			str:
      global _tqdm_active
      UpperCamelCase      = False
      hf_hub_utils.disable_progress_bars()
