
"""simple docstring"""
import unittest

import numpy as np
import timeout_decorator  # noqa

from transformers import OPTConfig, is_flax_available
from transformers.testing_utils import require_flax, require_sentencepiece, slow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor


if is_flax_available():
    import os

    # The slow tests are often failing with OOM error on GPU
    # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed
    # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html
    _a   :      Optional[Any]       =				"""platform"""

    import jax
    import jax.numpy as jnp

    from transformers import FlaxOPTForCausalLM, FlaxOPTModel, GPTaTokenizer
def 					a__ ( a :					List[str]							,  a :					Dict							,  a :					List[Any]=None							,  a :					Dict=None		):

  """simple docstring"""







  if attention_mask is None:
    _snake_case  :    List[str]            = np.where(input_ids != config.pad_token_id							,  1							,  0		)
  return {
      "input_ids": input_ids,
      "attention_mask": attention_mask,
  }






@require_flax
class   _UpperCAmelCase					:
    def __init__(     self    ,     snake_case_    ,     snake_case_=13    ,     snake_case_=7    ,     snake_case_=True    ,     snake_case_=False    ,     snake_case_=99    ,     snake_case_=16    ,     snake_case_=2    ,     snake_case_=4    ,     snake_case_=4    ,     snake_case_="gelu"    ,     snake_case_=0.1    ,     snake_case_=0.1    ,     snake_case_=20    ,     snake_case_=2    ,     snake_case_=1    ,     snake_case_=0    ,     snake_case_=16    ,     snake_case_=16    ,     snake_case_=0.02    ,     ):
      _snake_case  :    List[Any]            = parent
      _snake_case  :    str            = batch_size
      _snake_case  :    List[Any]            = seq_length
      _snake_case  :    Union[str, Any]            = is_training
      _snake_case  :    Optional[Any]            = use_labels
      _snake_case  :    List[str]            = vocab_size
      _snake_case  :    int            = hidden_size
      _snake_case  :    List[Any]            = num_hidden_layers
      _snake_case  :    List[Any]            = num_attention_heads
      _snake_case  :    List[str]            = intermediate_size
      _snake_case  :    int            = hidden_act
      _snake_case  :    str            = hidden_dropout_prob
      _snake_case  :    Optional[Any]            = attention_probs_dropout_prob
      _snake_case  :    List[Any]            = max_position_embeddings
      _snake_case  :    str            = eos_token_id
      _snake_case  :    str            = pad_token_id
      _snake_case  :    Any            = bos_token_id
      _snake_case  :    int            = embed_dim
      _snake_case  :    Optional[Any]            = word_embed_proj_dim
      _snake_case  :    Any            = initializer_range
      _snake_case  :    List[Any]            = False
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    Tuple            = np.clip(ids_tensor([self.batch_size, self.seq_length - 1]    ,     self.vocab_size  )    ,     3    ,     self.vocab_size  )
      _snake_case  :    Union[str, Any]            = np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1)    ,     dtype=np.intaa  ))    ,     -1  )

      _snake_case  :    Tuple            = OPTConfig(
          vocab_size=self.vocab_size    ,     hidden_size=self.hidden_size    ,     num_hidden_layers=self.num_hidden_layers    ,     num_attention_heads=self.num_attention_heads    ,     ffn_dim=self.intermediate_size    ,     dropout=self.hidden_dropout_prob    ,     attention_dropout=self.attention_probs_dropout_prob    ,     max_position_embeddings=self.max_position_embeddings    ,     eos_token_id=self.eos_token_id    ,     bos_token_id=self.bos_token_id    ,     pad_token_id=self.pad_token_id    ,     embed_dim=self.embed_dim    ,     is_encoder_decoder=snake_case_    ,     word_embed_proj_dim=self.word_embed_proj_dim    ,     initializer_range=self.initializer_range    ,     use_cache=snake_case_    ,     )
      _snake_case  :    Optional[int]            = prepare_opt_inputs_dict(snake_case_    ,     snake_case_  )
      return config, inputs_dict
    def        lowerCamelCase__						(     self  ):
      _snake_case      ,       _snake_case  :    List[Any]            = self.prepare_config_and_inputs()
      return config, inputs_dict
    def        lowerCamelCase__						(     self    ,     snake_case_    ,     snake_case_    ,     snake_case_  ):
      _snake_case  :    str            = 20
      _snake_case  :    List[str]            = model_class_name(snake_case_  )

      _snake_case  :    Optional[Any]            = inputs_dict["input_ids"]
      _snake_case  :    str            = inputs_dict["attention_mask"]

      _snake_case  :    List[Any]            = model.init_cache(input_ids.shape[0]    ,     snake_case_  )
      _snake_case  :    int            = jnp.ones((input_ids.shape[0], max_length)    ,     dtype="i4"  )

      _snake_case  :    List[str]            = jnp.broadcast_to(
          jnp.arange(input_ids.shape[-1] - 1  )[None, :]    ,     (input_ids.shape[0], input_ids.shape[-1] - 1)    ,     )
      _snake_case  :    List[str]            = model(
          input_ids[:, :-1]    ,     attention_mask=snake_case_    ,     past_key_values=snake_case_    ,     position_ids=snake_case_    ,     )

      _snake_case  :    Dict            = jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]    ,     dtype="i4"  )
      _snake_case  :    Any            = model(
          input_ids[:, -1:]    ,     attention_mask=snake_case_    ,     past_key_values=outputs_cache.past_key_values    ,     position_ids=snake_case_    ,     )

      _snake_case  :    List[str]            = model(snake_case_  )

      _snake_case  :    int            = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])  )  )
      self.parent.assertTrue(diff < 1E-3    ,     msg=F'Max diff is {diff}'  )


    def        lowerCamelCase__						(     self    ,     snake_case_    ,     snake_case_    ,     snake_case_  ):
      _snake_case  :    Dict            = 20
      _snake_case  :    Any            = model_class_name(snake_case_  )

      _snake_case      ,       _snake_case  :    List[str]            = (
          inputs_dict["input_ids"],
          inputs_dict["attention_mask"],
      )

      _snake_case  :    Optional[Any]            = jnp.concatenate(
          [
              attention_mask,
              jnp.zeros((attention_mask.shape[0], max_length - attention_mask.shape[1])  ),
          ]    ,     axis=-1    ,     )

      _snake_case  :    List[str]            = model.init_cache(input_ids.shape[0]    ,     snake_case_  )
      _snake_case  :    Tuple            = jnp.broadcast_to(
          jnp.arange(input_ids.shape[-1] - 1  )[None, :]    ,     (input_ids.shape[0], input_ids.shape[-1] - 1)    ,     )

      _snake_case  :    Tuple            = model(
          input_ids[:, :-1]    ,     attention_mask=snake_case_    ,     past_key_values=snake_case_    ,     position_ids=snake_case_    ,     )
      _snake_case  :    Dict            = jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]    ,     dtype="i4"  )
      _snake_case  :    Dict            = model(
          input_ids[:, -1:]    ,     past_key_values=outputs_cache.past_key_values    ,     attention_mask=snake_case_    ,     position_ids=snake_case_    ,     )

      _snake_case  :    Tuple            = model(snake_case_    ,     attention_mask=snake_case_  )

      _snake_case  :    Optional[Any]            = np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])  )  )
      self.parent.assertTrue(diff < 1E-3    ,     msg=F'Max diff is {diff}'  )






@require_flax
class   _UpperCAmelCase					(       _snake_case		,       unittest.TestCase		,       _snake_case):
    __lowercase				:			Optional[int]	=       (FlaxOPTModel, FlaxOPTForCausalLM) if is_flax_available() else ()
    __lowercase				:			int	=       () if is_flax_available() else ()
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    Tuple            = FlaxOPTModelTester(self  )
    def        lowerCamelCase__						(     self  ):
      _snake_case      ,       _snake_case  :    Tuple            = self.model_tester.prepare_config_and_inputs()
      for model_class in self.all_model_classes:
        self.model_tester.check_use_cache_forward(snake_case_    ,     snake_case_    ,     snake_case_  )
    def        lowerCamelCase__						(     self  ):
      _snake_case      ,       _snake_case  :    str            = self.model_tester.prepare_config_and_inputs()
      for model_class in self.all_model_classes:
        self.model_tester.check_use_cache_forward_with_attn_mask(snake_case_    ,     snake_case_    ,     snake_case_  )


    @slow
    def        lowerCamelCase__						(     self  ):
      for model_class_name in self.all_model_classes:
        _snake_case  :    Optional[Any]            = model_class_name.from_pretrained("facebook/opt-125m"  )
        _snake_case  :    Union[str, Any]            = np.ones((1, 1)  ) * model.config.eos_token_id
        _snake_case  :    str            = model(snake_case_  )
        self.assertIsNotNone(snake_case_  )






@require_sentencepiece
@require_flax
class   _UpperCAmelCase					(       unittest.TestCase):
    @slow
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    Tuple            = FlaxOPTModel.from_pretrained("facebook/opt-350m"  )
      _snake_case  :    Any            = jnp.array([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]  )
      _snake_case  :    Optional[int]            = model(input_ids=snake_case_  ).last_hidden_state
      _snake_case  :    Optional[Any]            = (1, 11, 5_12)
      self.assertEqual(output.shape    ,     snake_case_  )
      _snake_case  :    int            = jnp.array(
          [[-0.2867, -1.9256, -0.3062], [-1.2711, -0.1337, -0.1897], [0.4109, 0.1187, -1.3142]]  )
      self.assertTrue(jnp.allclose(output[:, :3, :3]    ,     snake_case_    ,     atol=4E-2  )  )






@require_flax
@slow
class   _UpperCAmelCase					(       unittest.TestCase):
    def        lowerCamelCase__						(     self  ):
      super().setUp()
      _snake_case  :    List[Any]            = "facebook/opt-350m"


    def        lowerCamelCase__						(     self  ):
      _snake_case  :    Optional[Any]            = FlaxOPTForCausalLM.from_pretrained(self.path_model  )
      _snake_case  :    Optional[Any]            = GPTaTokenizer.from_pretrained(self.path_model  )

      _snake_case  :    str            = [
          "Today is a beautiful day and I want to",
          "In the city of",
          "Paris is the capital of France and",
          "Computers and mobile phones have taken",
      ]
      # verify that prompt without BOS token is identical to Metaseq -> add_special_tokens=False
      _snake_case  :    Tuple            = tokenizer(snake_case_    ,     return_tensors="jax"    ,     padding=snake_case_    ,     add_special_tokens=snake_case_  )
      _snake_case  :    int            = model(inputs.input_ids    ,     attention_mask=inputs.attention_mask  )[0].mean(axis=-1  )
      _snake_case  :    int            = jnp.array(
          [
              [1.3851, -13.8923, -10.5229, -10.7533, -0.2309, -10.2384, -0.5365, -9.0947, -5.1670],
              [-4.7073, -10.6276, -3.9415, -21.5242, -0.2822, -0.2822, -0.2822, -0.2822, -0.2822],
              [0.6247, -3.4229, -8.9179, -1.4297, -14.1650, 1.4146, -9.0218, -0.2703, -0.2703],
              [6.4783, -1.9913, -10.7926, -2.3336, 1.5092, -0.9974, -6.8213, 1.3477, 1.3477],
          ]  )
      self.assertTrue(jnp.allclose(snake_case_    ,     snake_case_    ,     atol=4E-2  )  )

      _snake_case  :    int            = jax.jit(snake_case_  )
      _snake_case  :    List[str]            = model(inputs.input_ids    ,     attention_mask=inputs.attention_mask  )[0].mean(axis=-1  )
      self.assertTrue(jnp.allclose(snake_case_    ,     snake_case_    ,     atol=4E-2  )  )






@require_flax
@slow
class   _UpperCAmelCase					(       unittest.TestCase):
    @property
    def        lowerCamelCase__						(     self  ):
      return [
          "Today is a beautiful day and I want",
          "In the city of",
          "Paris is the capital of France and",
          "Computers and mobile phones have taken",
      ]
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    Union[str, Any]            = "facebook/opt-125m"

      _snake_case  :    Tuple            = [
          "Today is a beautiful day and I want to",
          "In the city of New York, the city",
          "Paris is the capital of France and the capital",
          "Computers and mobile phones have taken over the",
      ]

      _snake_case  :    Tuple            = []

      _snake_case  :    int            = FlaxOPTForCausalLM.from_pretrained(snake_case_  )
      _snake_case  :    Any            = GPTaTokenizer.from_pretrained(snake_case_  )

      for prompt in self.prompts:
        _snake_case  :    Tuple            = tokenizer(snake_case_    ,     return_tensors="jax"  ).input_ids

        _snake_case  :    List[Any]            = model.generate(snake_case_    ,     max_length=10  )
        _snake_case  :    Tuple            = generated_ids[0]

        _snake_case  :    Tuple            = tokenizer.batch_decode(snake_case_    ,     skip_special_tokens=snake_case_  )
        predicted_outputs += generated_string

      self.assertListEqual(snake_case_    ,     snake_case_  )
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    List[str]            = "facebook/opt-350m"

      _snake_case  :    Any            = [
          "Today is a beautiful day and I want to",
          "In the city of San Francisco, the city",
          "Paris is the capital of France and the capital",
          "Computers and mobile phones have taken over the",
      ]

      _snake_case  :    Dict            = []
      _snake_case  :    Optional[int]            = FlaxOPTForCausalLM.from_pretrained(snake_case_  )
      _snake_case  :    Tuple            = GPTaTokenizer.from_pretrained(snake_case_  )

      for prompt in self.prompts:
        _snake_case  :    Any            = tokenizer(snake_case_    ,     return_tensors="jax"  ).input_ids

        _snake_case  :    Tuple            = model.generate(snake_case_    ,     max_length=10  )
        _snake_case  :    Optional[Any]            = generated_ids[0]

        _snake_case  :    Optional[Any]            = tokenizer.batch_decode(snake_case_    ,     skip_special_tokens=snake_case_  )
        predicted_outputs += generated_string

      self.assertListEqual(snake_case_    ,     snake_case_  )
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    Union[str, Any]            = "facebook/opt-125m"
      _snake_case  :    Union[str, Any]            = [
          "Today is a beautiful day and I want to thank",
          "In the city of Rome Canaver Canaver Canaver Canaver",
      ]
      _snake_case  :    Optional[int]            = FlaxOPTForCausalLM.from_pretrained(snake_case_  )
      _snake_case  :    Optional[int]            = GPTaTokenizer.from_pretrained(snake_case_  )
      _snake_case  :    Any            = tokenizer(
          [
              "Today is a beautiful day and I want to",
              "In the city of",
          ]    ,     return_tensors="jax"    ,     padding=snake_case_    ,     )

      _snake_case  :    Optional[Any]            = jax.jit(model.generate  )

      _snake_case  :    Union[str, Any]            = jit_generate(inputs["input_ids"]    ,     attention_mask=inputs["attention_mask"]  ).sequences

      _snake_case  :    Optional[int]            = tokenizer.batch_decode(snake_case_    ,     skip_special_tokens=snake_case_  )

      self.assertIsNotNone(snake_case_    ,     snake_case_  )


    def        lowerCamelCase__						(     self  ):
      _snake_case  :    int            = "facebook/opt-350m"

      _snake_case  :    int            = GPTaTokenizer.from_pretrained(snake_case_  )
      _snake_case  :    List[str]            = FlaxOPTForCausalLM.from_pretrained(snake_case_  )

      _snake_case  :    List[Any]            = "left"

      # use different length sentences to test batching
      _snake_case  :    Optional[Any]            = [
          "Hello, my dog is a little",
          "Today, I",
      ]

      _snake_case  :    int            = tokenizer(snake_case_    ,     return_tensors="jax"    ,     padding=snake_case_  )
      _snake_case  :    Union[str, Any]            = inputs["input_ids"]

      _snake_case  :    Dict            = model.generate(input_ids=snake_case_    ,     attention_mask=inputs["attention_mask"]    ,     trace=snake_case_  )

      _snake_case  :    List[Any]            = tokenizer(sentences[0]    ,     return_tensors="jax"  ).input_ids
      _snake_case  :    List[Any]            = model.generate(input_ids=snake_case_  )

      _snake_case  :    Dict            = inputs_non_padded.shape[-1] - inputs["attention_mask"][-1].sum()
      _snake_case  :    Optional[Any]            = tokenizer(sentences[1]    ,     return_tensors="jax"  ).input_ids
      _snake_case  :    Optional[Any]            = model.generate(input_ids=snake_case_    ,     max_length=model.config.max_length - num_paddings  )

      _snake_case  :    Union[str, Any]            = tokenizer.batch_decode(outputs[0]    ,     skip_special_tokens=snake_case_  )
      _snake_case  :    Tuple            = tokenizer.decode(output_non_padded[0][0]    ,     skip_special_tokens=snake_case_  )
      _snake_case  :    int            = tokenizer.decode(output_padded[0][0]    ,     skip_special_tokens=snake_case_  )

      _snake_case  :    Any            = [
          "Hello, my dog is a little bit of a dork.\nI'm a little bit",
          "Today, I was in the middle of a conversation with a friend about the",
      ]
      self.assertListEqual(snake_case_    ,     snake_case_  )
      self.assertListEqual(snake_case_    ,     [non_padded_sentence, padded_sentence]  )



"""simple docstring"""
import random
import unittest

import torch

from diffusers import IFSuperResolutionPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin






@skip_mps
class   _UpperCAmelCase					(       _snake_case		,       _snake_case		,       unittest.TestCase):
    __lowercase				:			List[str]	=       IFSuperResolutionPipeline
    __lowercase				:			Optional[Any]	=       TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""width""", """height"""}
    __lowercase				:			List[Any]	=       TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
    __lowercase				:			Optional[Any]	=       PipelineTesterMixin.required_optional_params - {"""latents"""}
    def        lowerCamelCase__						(     self  ):
      return self._get_superresolution_dummy_components()
    def        lowerCamelCase__						(     self    ,     snake_case_    ,     snake_case_=0  ):
      if str(snake_case_  ).startswith("mps"  ):
        _snake_case  :    Tuple            = torch.manual_seed(snake_case_  )
      else:
        _snake_case  :    Tuple            = torch.Generator(device=snake_case_  ).manual_seed(snake_case_  )

      _snake_case  :    List[str]            = floats_tensor((1, 3, 32, 32)    ,     rng=random.Random(snake_case_  )  ).to(snake_case_  )

      _snake_case  :    List[str]            = {
          "prompt": "A painting of a squirrel eating a burger",
          "image": image,
          "generator": generator,
          "num_inference_steps": 2,
          "output_type": "numpy",
      }

      return inputs
    @unittest.skipIf(
        torch_device != "cuda" or not is_xformers_available()    ,     reason="XFormers attention is only available with CUDA and `xformers` installed"    ,     )
    def        lowerCamelCase__						(     self  ):
      self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3  )
    def        lowerCamelCase__						(     self  ):
      self._test_save_load_optional_components()
    @unittest.skipIf(torch_device != "cuda"    ,     reason="float16 requires CUDA"  )
    def        lowerCamelCase__						(     self  ):
      # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
      super().test_save_load_floataa(expected_max_diff=1E-1  )
    def        lowerCamelCase__						(     self  ):
      self._test_attention_slicing_forward_pass(expected_max_diff=1E-2  )
    def        lowerCamelCase__						(     self  ):
      self._test_save_load_local()


    def        lowerCamelCase__						(     self  ):
      self._test_inference_batch_single_identical(
          expected_max_diff=1E-2    ,     )

