
from __future__ import annotations

import unittest

from transformers import BlenderbotSmallConfig, BlenderbotSmallTokenizer, is_tf_available
from transformers.testing_utils import require_tf, require_tokenizers, slow
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
       import tensorflow as tf

       from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel





@require_tf
class 						_a   :

       """simple docstring"""





       snake_case				=BlenderbotSmallConfig
       snake_case				={}
       snake_case				='gelu'
       def __init__(					self       ,					_snake_case       ,					_snake_case=13       ,					_snake_case=7       ,					_snake_case=True       ,					_snake_case=False       ,					_snake_case=99       ,					_snake_case=32       ,					_snake_case=2       ,					_snake_case=4       ,					_snake_case=37       ,					_snake_case=0.1       ,					_snake_case=0.1       ,					_snake_case=20       ,					_snake_case=2       ,					_snake_case=1       ,					_snake_case=0       ,					):
        _UpperCAmelCase 				=parent
        _UpperCAmelCase 				=batch_size
        _UpperCAmelCase 				=seq_length
        _UpperCAmelCase 				=is_training
        _UpperCAmelCase 				=use_labels
        _UpperCAmelCase 				=vocab_size
        _UpperCAmelCase 				=hidden_size
        _UpperCAmelCase 				=num_hidden_layers
        _UpperCAmelCase 				=num_attention_heads
        _UpperCAmelCase 				=intermediate_size

        _UpperCAmelCase 				=hidden_dropout_prob
        _UpperCAmelCase 				=attention_probs_dropout_prob
        _UpperCAmelCase 				=max_position_embeddings
        _UpperCAmelCase 				=eos_token_id
        _UpperCAmelCase 				=pad_token_id
        _UpperCAmelCase 				=bos_token_id
       def     SCREAMING_SNAKE_CASE  (					self	):
        _UpperCAmelCase 				=ids_tensor([self.batch_size, self.seq_length - 1]       ,					self.vocab_size	)
        _UpperCAmelCase 				=tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size	)       ,					1	)
        _UpperCAmelCase 				=tf.concat([input_ids, eos_tensor]       ,					axis=1	)

        _UpperCAmelCase 				=ids_tensor([self.batch_size, self.seq_length]       ,					self.vocab_size	)

        _UpperCAmelCase 				=self.config_cls(
            vocab_size=self.vocab_size       ,					d_model=self.hidden_size       ,					encoder_layers=self.num_hidden_layers       ,					decoder_layers=self.num_hidden_layers       ,					encoder_attention_heads=self.num_attention_heads       ,					decoder_attention_heads=self.num_attention_heads       ,					encoder_ffn_dim=self.intermediate_size       ,					decoder_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_ids=[2]       ,					bos_token_id=self.bos_token_id       ,					pad_token_id=self.pad_token_id       ,					decoder_start_token_id=self.pad_token_id       ,					**self.config_updates       ,					)
        _UpperCAmelCase 				=prepare_blenderbot_small_inputs_dict(_snake_case       ,					_snake_case       ,					_snake_case	)
        return config, inputs_dict

       def     SCREAMING_SNAKE_CASE  (					self       ,					_snake_case       ,					_snake_case	):
        _UpperCAmelCase 				=TFBlenderbotSmallModel(config=_snake_case	).get_decoder()
        _UpperCAmelCase 				=inputs_dict["input_ids"]

        _UpperCAmelCase 				=input_ids[:1, :]
        _UpperCAmelCase 				=inputs_dict["attention_mask"][:1, :]
        _UpperCAmelCase 				=inputs_dict["head_mask"]
        _UpperCAmelCase 				=1

        # first forward pass
        _UpperCAmelCase 				=model(_snake_case       ,					attention_mask=_snake_case       ,					head_mask=_snake_case       ,					use_cache=_snake_case	)

        _UpperCAmelCase 				=outputs.to_tuple()

        # create hypothetical next token and extent to next_input_ids
        _UpperCAmelCase 				=ids_tensor((self.batch_size, 3)       ,					config.vocab_size	)
        _UpperCAmelCase 				=tf.cast(ids_tensor((self.batch_size, 3)       ,					2	)       ,					tf.inta	)

        # append to next input_ids and
        _UpperCAmelCase 				=tf.concat([input_ids, next_tokens]       ,					axis=-1	)
        _UpperCAmelCase 				=tf.concat([attention_mask, next_attn_mask]       ,					axis=-1	)

        _UpperCAmelCase 				=model(_snake_case       ,					attention_mask=_snake_case	)[0]
        _UpperCAmelCase 				=model(_snake_case       ,					attention_mask=_snake_case       ,					past_key_values=_snake_case	)[0]

        self.parent.assertEqual(next_tokens.shape[1]       ,					output_from_past.shape[1]	)

        # select random slice
        _UpperCAmelCase 				=int(ids_tensor((1,)       ,					output_from_past.shape[-1]	)	)
        _UpperCAmelCase 				=output_from_no_past[:, -3:, random_slice_idx]
        _UpperCAmelCase 				=output_from_past[:, :, random_slice_idx]

        # test that outputs are equal for slice
        tf.debugging.assert_near(_snake_case       ,					_snake_case       ,					rtol=1E-3	)







def 					lowerCamelCase__		(				_lowerCamelCase			,		_lowerCamelCase			,		_lowerCamelCase			,		_lowerCamelCase=None			,		_lowerCamelCase=None			,		_lowerCamelCase=None			,		_lowerCamelCase=None			,		_lowerCamelCase=None			,		)						->Optional[int]:

 if attention_mask is None:
  _UpperCAmelCase 				=tf.cast(tf.math.not_equal(SCREAMING_SNAKE_CASE__			,		config.pad_token_id  )			,		tf.inta  )
 if decoder_attention_mask is None:
  _UpperCAmelCase 				=tf.concat(
      [
          tf.ones(decoder_input_ids[:, :1].shape			,		dtype=tf.inta  ),
          tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]			,		config.pad_token_id  )			,		tf.inta  ),
      ]			,		axis=-1			,		)
 if head_mask is None:
  _UpperCAmelCase 				=tf.ones((config.encoder_layers, config.encoder_attention_heads)  )
 if decoder_head_mask is None:
  _UpperCAmelCase 				=tf.ones((config.decoder_layers, config.decoder_attention_heads)  )
 if cross_attn_head_mask is None:
  _UpperCAmelCase 				=tf.ones((config.decoder_layers, config.decoder_attention_heads)  )
 return {
     "input_ids": input_ids,
     "decoder_input_ids": decoder_input_ids,
     "attention_mask": attention_mask,
     "decoder_attention_mask": decoder_attention_mask,
     "head_mask": head_mask,
     "decoder_head_mask": decoder_head_mask,
     "cross_attn_head_mask": cross_attn_head_mask,
 }





@require_tf
class 						_a   (						_UpperCamelCase ,      _UpperCamelCase ,      unittest.TestCase       ):

       """simple docstring"""





       snake_case				=(
           (TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel) if is_tf_available() else ()
       )
       snake_case				=(TFBlenderbotSmallForConditionalGeneration,) if is_tf_available() else ()
       snake_case				=(
           {
               'conversational': TFBlenderbotSmallForConditionalGeneration,
               'feature-extraction': TFBlenderbotSmallModel,
               'summarization': TFBlenderbotSmallForConditionalGeneration,
               'text2text-generation': TFBlenderbotSmallForConditionalGeneration,
               'translation': TFBlenderbotSmallForConditionalGeneration,
           }
           if is_tf_available()
           else {}
       )
       snake_case				=True
       snake_case				=False
       snake_case				=False
       def     SCREAMING_SNAKE_CASE  (					self	):
        _UpperCAmelCase 				=TFBlenderbotSmallModelTester(self	)
        _UpperCAmelCase 				=ConfigTester(self       ,					config_class=_snake_case	)
       def     SCREAMING_SNAKE_CASE  (					self	):
        self.config_tester.run_common_tests()

       def     SCREAMING_SNAKE_CASE  (					self	):
        _UpperCAmelCase 				=self.model_tester.prepare_config_and_inputs_for_common()
        self.model_tester.check_decoder_model_past_large_inputs(*_snake_case	)







@require_tokenizers
@require_tf
class 						_a   (						unittest.TestCase       ):

       """simple docstring"""





       snake_case				=[
           'Social anxiety\nWow, I am never shy. Do you have anxiety?\nYes. I end up sweating and blushing and feel like '
           '  i\'m going to throw up.\nand why is that?'
       ]
       snake_case				='facebook/blenderbot_small-90M'
       @cached_property
       def     SCREAMING_SNAKE_CASE  (					self	):
        # use "old" tokenizer here because of bug when downloading new tokenizer
        return BlenderbotSmallTokenizer.from_pretrained("facebook/blenderbot-90M"	)
       @cached_property
       def     SCREAMING_SNAKE_CASE  (					self	):
        _UpperCAmelCase 				=TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name	)
        return model

       @slow
       def     SCREAMING_SNAKE_CASE  (					self	):
        _UpperCAmelCase 				=self.tokenizer(self.src_text       ,					return_tensors="tf"	)
        _UpperCAmelCase 				=self.model.generate(
            model_inputs.input_ids       ,					attention_mask=model_inputs.attention_mask       ,					num_beams=2       ,					use_cache=_snake_case       ,					)
        _UpperCAmelCase 				=self.tokenizer.batch_decode(generated_ids.numpy()       ,					skip_special_tokens=_snake_case	)[0]
        assert generated_words in (
            "i don't know. i just feel like i'm going to throw up. it's not fun.",
            "i'm not sure. i just feel like i've been feeling like i have to be in a certain place",
            "i'm not sure. i just feel like i've been in a bad situation.",
        )










"""simple docstring"""




import os
import tempfile
import unittest

import numpy as np

from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import require_flax, slow


if is_flax_available():
 import jax
 import jax.numpy as jnp
 from flax.jax_utils import replicate
 from flax.training.common_utils import shard

 from diffusers import FlaxDDIMScheduler, FlaxDiffusionPipeline, FlaxStableDiffusionPipeline






@require_flax
class       UpperCAmelCase_    (							unittest.TestCase		):



      def 		snake_case_   (	self	:      Optional[int]		):
           with tempfile.TemporaryDirectory() as tmpdirname:
                # pipeline has Flax weights
                _UpperCAmelCase     :      Union[str, Any]														=       FlaxDiffusionPipeline.from_pretrained(
                    "hf-internal-testing/tiny-stable-diffusion-pipe"	,      safety_checker=A	,      cache_dir=A		)

                _UpperCAmelCase     :      List[str]														=       [t[-1] for t in os.walk(os.path.join(A	,      os.listdir(A		)[0]	,      "snapshots"		)		)]
                _UpperCAmelCase     :      Union[str, Any]														=       [item for sublist in all_root_files for item in sublist]

                # None of the downloaded files should be a PyTorch file even if we have some here:
                # https://huggingface.co/hf-internal-testing/tiny-stable-diffusion-pipe/blob/main/unet/diffusion_pytorch_model.bin
                assert not any(f.endswith(".bin"		) for f in files		)







@slow
@require_flax
class       UpperCAmelCase_    (							unittest.TestCase		):



      def 		snake_case_   (	self	:      List[str]		):
           _UpperCAmelCase							,			_UpperCAmelCase     :      int														=       FlaxStableDiffusionPipeline.from_pretrained(
               "hf-internal-testing/tiny-stable-diffusion-pipe"	,      safety_checker=A		)

           _UpperCAmelCase     :      str														=       (
               "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
               " field, close up, split lighting, cinematic"
           )

           _UpperCAmelCase     :      int														=       jax.random.PRNGKey(0		)
           _UpperCAmelCase     :      List[str]														=       4

           _UpperCAmelCase     :      Any														=       jax.device_count()
           _UpperCAmelCase     :      Tuple														=       num_samples * [prompt]
           _UpperCAmelCase     :      Optional[int]														=       pipeline.prepare_inputs(A		)

           # shard inputs and rng
           _UpperCAmelCase     :      Union[str, Any]														=       replicate(A		)
           _UpperCAmelCase     :      List[Any]														=       jax.random.split(A	,      A		)
           _UpperCAmelCase     :      List[Any]														=       shard(A		)

           _UpperCAmelCase     :      Tuple														=       pipeline(A	,      A	,      A	,      A	,      jit=A		).images

           assert images.shape == (num_samples, 1, 6_4, 6_4, 3)
           if jax.device_count() == 8:
                assert np.abs(np.abs(images[0, 0, :2, :2, -2:]	,      dtype=np.floataa		).sum() - 4.1_514_745		) < 1e-3
                assert np.abs(np.abs(A	,      dtype=np.floataa		).sum() - 49_947.875		) < 5e-1

           _UpperCAmelCase     :      Optional[int]														=       pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:]		)		)		)
           assert len(A		) == num_samples



      def 		snake_case_   (	self	:      Union[str, Any]		):
           _UpperCAmelCase							,			_UpperCAmelCase     :      Optional[Any]														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="flax"	,      safety_checker=A		)

           _UpperCAmelCase     :      Dict														=       (
               "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
               " field, close up, split lighting, cinematic"
           )

           _UpperCAmelCase     :      Dict														=       jax.random.PRNGKey(0		)
           _UpperCAmelCase     :      str														=       5_0

           _UpperCAmelCase     :      List[str]														=       jax.device_count()
           _UpperCAmelCase     :      Any														=       num_samples * [prompt]
           _UpperCAmelCase     :      Dict														=       pipeline.prepare_inputs(A		)

           # shard inputs and rng
           _UpperCAmelCase     :      Dict														=       replicate(A		)
           _UpperCAmelCase     :      str														=       jax.random.split(A	,      A		)
           _UpperCAmelCase     :      Optional[Any]														=       shard(A		)

           _UpperCAmelCase     :      List[Any]														=       pipeline(A	,      A	,      A	,      A	,      jit=A		).images

           assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           if jax.device_count() == 8:
                assert np.abs((np.abs(images[0, 0, :2, :2, -2:]	,      dtype=np.floataa		).sum() - 0.05_652_401)		) < 1e-3
                assert np.abs((np.abs(A	,      dtype=np.floataa		).sum() - 2_383_808.2)		) < 5e-1



      def 		snake_case_   (	self	:      List[Any]		):
           _UpperCAmelCase							,			_UpperCAmelCase     :      List[Any]														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="bf16"	,      dtype=jnp.bfloataa	,      safety_checker=A		)

           _UpperCAmelCase     :      Any														=       (
               "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
               " field, close up, split lighting, cinematic"
           )

           _UpperCAmelCase     :      List[Any]														=       jax.random.PRNGKey(0		)
           _UpperCAmelCase     :      List[Any]														=       5_0

           _UpperCAmelCase     :      Union[str, Any]														=       jax.device_count()
           _UpperCAmelCase     :      Optional[int]														=       num_samples * [prompt]
           _UpperCAmelCase     :      Union[str, Any]														=       pipeline.prepare_inputs(A		)

           # shard inputs and rng
           _UpperCAmelCase     :      int														=       replicate(A		)
           _UpperCAmelCase     :      Optional[int]														=       jax.random.split(A	,      A		)
           _UpperCAmelCase     :      str														=       shard(A		)

           _UpperCAmelCase     :      Union[str, Any]														=       pipeline(A	,      A	,      A	,      A	,      jit=A		).images

           assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           if jax.device_count() == 8:
                assert np.abs((np.abs(images[0, 0, :2, :2, -2:]	,      dtype=np.floataa		).sum() - 0.04_003_906)		) < 1e-3
                assert np.abs((np.abs(A	,      dtype=np.floataa		).sum() - 2_373_516.75)		) < 5e-1



      def 		snake_case_   (	self	:      int		):
           _UpperCAmelCase							,			_UpperCAmelCase     :      Dict														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="bf16"	,      dtype=jnp.bfloataa		)

           _UpperCAmelCase     :      str														=       (
               "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
               " field, close up, split lighting, cinematic"
           )

           _UpperCAmelCase     :      Optional[int]														=       jax.random.PRNGKey(0		)
           _UpperCAmelCase     :      Dict														=       5_0

           _UpperCAmelCase     :      Union[str, Any]														=       jax.device_count()
           _UpperCAmelCase     :      str														=       num_samples * [prompt]
           _UpperCAmelCase     :      int														=       pipeline.prepare_inputs(A		)

           # shard inputs and rng
           _UpperCAmelCase     :      Optional[int]														=       replicate(A		)
           _UpperCAmelCase     :      Tuple														=       jax.random.split(A	,      A		)
           _UpperCAmelCase     :      Dict														=       shard(A		)

           _UpperCAmelCase     :      Optional[Any]														=       pipeline(A	,      A	,      A	,      A	,      jit=A		).images

           assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           if jax.device_count() == 8:
                assert np.abs((np.abs(images[0, 0, :2, :2, -2:]	,      dtype=np.floataa		).sum() - 0.04_003_906)		) < 1e-3
                assert np.abs((np.abs(A	,      dtype=np.floataa		).sum() - 2_373_516.75)		) < 5e-1



      def 		snake_case_   (	self	:      Optional[Any]		):
           _UpperCAmelCase     :      Dict														=       FlaxDDIMScheduler(
               beta_start=0.00_085	,      beta_end=0.012	,      beta_schedule="scaled_linear"	,      set_alpha_to_one=A	,      steps_offset=1	,      )

           _UpperCAmelCase							,			_UpperCAmelCase     :      Dict														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="bf16"	,      dtype=jnp.bfloataa	,      scheduler=A	,      safety_checker=A	,      )
           _UpperCAmelCase     :      Union[str, Any]														=       scheduler.create_state()

           _UpperCAmelCase     :      str														=       scheduler_state

           _UpperCAmelCase     :      Union[str, Any]														=       (
               "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
               " field, close up, split lighting, cinematic"
           )

           _UpperCAmelCase     :      str														=       jax.random.PRNGKey(0		)
           _UpperCAmelCase     :      List[str]														=       5_0

           _UpperCAmelCase     :      Optional[int]														=       jax.device_count()
           _UpperCAmelCase     :      int														=       num_samples * [prompt]
           _UpperCAmelCase     :      Optional[int]														=       pipeline.prepare_inputs(A		)

           # shard inputs and rng
           _UpperCAmelCase     :      List[Any]														=       replicate(A		)
           _UpperCAmelCase     :      Tuple														=       jax.random.split(A	,      A		)
           _UpperCAmelCase     :      Union[str, Any]														=       shard(A		)

           _UpperCAmelCase     :      Dict														=       pipeline(A	,      A	,      A	,      A	,      jit=A		).images

           assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           if jax.device_count() == 8:
                assert np.abs((np.abs(images[0, 0, :2, :2, -2:]	,      dtype=np.floataa		).sum() - 0.045_043_945)		) < 1e-3
                assert np.abs((np.abs(A	,      dtype=np.floataa		).sum() - 2_347_693.5)		) < 5e-1



      def 		snake_case_   (	self	:      int		):
           _UpperCAmelCase     :      Optional[Any]														=       (
               "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
               " field, close up, split lighting, cinematic"
           )

           _UpperCAmelCase     :      Optional[Any]														=       jax.device_count()
           _UpperCAmelCase     :      Union[str, Any]														=       num_samples * [prompt]
           _UpperCAmelCase     :      Optional[Any]														=       jax.random.split(jax.random.PRNGKey(0		)	,      A		)

           _UpperCAmelCase							,			_UpperCAmelCase     :      str														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="bf16"	,      dtype=jnp.bfloataa	,      safety_checker=A	,      )

           _UpperCAmelCase     :      int														=       replicate(A		)
           _UpperCAmelCase     :      Union[str, Any]														=       pipeline.prepare_inputs(A		)
           _UpperCAmelCase     :      int														=       shard(A		)
           _UpperCAmelCase     :      Dict														=       pipeline(A	,      A	,      A	,      jit=A		).images
           assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           _UpperCAmelCase     :      List[str]														=       images[2, 0, 2_5_6, 1_0:1_7, 1]

           # With memory efficient attention
           _UpperCAmelCase							,			_UpperCAmelCase     :      str														=       FlaxStableDiffusionPipeline.from_pretrained(
               "CompVis/stable-diffusion-v1-4"	,      revision="bf16"	,      dtype=jnp.bfloataa	,      safety_checker=A	,      use_memory_efficient_attention=A	,      )

           _UpperCAmelCase     :      Any														=       replicate(A		)
           _UpperCAmelCase     :      str														=       pipeline.prepare_inputs(A		)
           _UpperCAmelCase     :      Tuple														=       shard(A		)
           _UpperCAmelCase     :      Tuple														=       pipeline(A	,      A	,      A	,      jit=A		).images
           assert images_eff.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
           _UpperCAmelCase     :      str														=       images[2, 0, 2_5_6, 1_0:1_7, 1]

           # I checked the results visually and they are very similar. However, I saw that the max diff is `1` and the `sum`
           # over the 8 images is exactly `256`, which is very suspicious. Testing a random slice for now.
           assert abs(slice_eff - slice		).max() < 1e-2
