
"""simple docstring"""



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_    :
    a   :					Optional[Any]			     =					BlenderbotSmallConfig
    a   :					Tuple			     =					{}
    a   :					int			     =					'gelu'







    def __init__(      self      :							str  ,       __UpperCamelCase      :							Optional[int]  ,       __UpperCamelCase      :							Dict=13  ,       __UpperCamelCase      :							str=7  ,       __UpperCamelCase      :							Optional[int]=True  ,       __UpperCamelCase      :							Optional[int]=False  ,       __UpperCamelCase      :							Tuple=99  ,       __UpperCamelCase      :							Dict=32  ,       __UpperCamelCase      :							str=2  ,       __UpperCamelCase      :							Tuple=4  ,       __UpperCamelCase      :							List[Any]=37  ,       __UpperCamelCase      :							Optional[int]=0.1  ,       __UpperCamelCase      :							List[Any]=0.1  ,       __UpperCamelCase      :							List[Any]=20  ,       __UpperCamelCase      :							Tuple=2  ,       __UpperCamelCase      :							Tuple=1  ,       __UpperCamelCase      :							Dict=0  ,       )		->List[Any]:






        '''simple docstring'''






        _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   _snake_case       (      self      :							List[str]  )		->Optional[Any]:






        '''simple docstring'''






        _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(__UpperCamelCase  ,       __UpperCamelCase  ,       __UpperCamelCase  )
        return config, inputs_dict







    def   _snake_case       (      self      :							Union[str, Any]  ,       __UpperCamelCase      :							Optional[int]  ,       __UpperCamelCase      :							Any  )		->Tuple:






        '''simple docstring'''






        _UpperCAmelCase        = TFBlenderbotSmallModel(config=__UpperCamelCase  ).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(__UpperCamelCase  ,       attention_mask=__UpperCamelCase  ,       head_mask=__UpperCamelCase  ,       use_cache=__UpperCamelCase  )

        _UpperCAmelCase     ,_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(__UpperCamelCase  ,       attention_mask=__UpperCamelCase  )[0]
        _UpperCAmelCase        = model(__UpperCamelCase  ,       attention_mask=__UpperCamelCase  ,       past_key_values=__UpperCamelCase  )[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(__UpperCamelCase  ,       __UpperCamelCase  ,       rtol=1e-3  )







def 			_UpperCamelCase						(    _A       ,   _A       ,   _A       ,   _A=None       ,   _A=None       ,   _A=None       ,   _A=None       ,   _A=None       ,   )     ->	int:





    """simple docstring"""

    if attention_mask is None:
        _UpperCAmelCase        = tf.cast(tf.math.not_equal(_A       ,   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						):
    a   :					List[str]			     =					(
        (TFBlenderbotSmallForConditionalGeneration, TFBlenderbotSmallModel) if is_tf_available() else ()
    )
    a   :					List[str]			     =					(TFBlenderbotSmallForConditionalGeneration,) if is_tf_available() else ()
    a   :					Optional[int]			     =					(
        {
            'conversational': TFBlenderbotSmallForConditionalGeneration,
            'feature-extraction': TFBlenderbotSmallModel,
            'summarization': TFBlenderbotSmallForConditionalGeneration,
            'text2text-generation': TFBlenderbotSmallForConditionalGeneration,
            'translation': TFBlenderbotSmallForConditionalGeneration,
        }
        if is_tf_available()
        else {}
    )
    a   :					str			     =					True
    a   :					Optional[Any]			     =					False
    a   :					Union[str, Any]			     =					False







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






        '''simple docstring'''






        _UpperCAmelCase        = TFBlenderbotSmallModelTester(self  )
        _UpperCAmelCase        = ConfigTester(self  ,       config_class=__UpperCamelCase  )







    def   _snake_case       (      self      :							Optional[Any]  )		->str:






        '''simple docstring'''






        self.config_tester.run_common_tests()







    def   _snake_case       (      self      :							Dict  )		->Any:






        '''simple docstring'''






        _UpperCAmelCase        = self.model_tester.prepare_config_and_inputs_for_common()
        self.model_tester.check_decoder_model_past_large_inputs(*__UpperCamelCase  )







@require_tokenizers
@require_tf
class 		a_    (			unittest.TestCase						):
    a   :					Optional[Any]			     =					[
        '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?'
    ]
    a   :					List[Any]			     =					'facebook/blenderbot_small-90M'







    @cached_property
    def   _snake_case       (      self      :							str  )		->List[str]:






        '''simple docstring'''






        return BlenderbotSmallTokenizer.from_pretrained("""facebook/blenderbot-90M"""  )







    @cached_property
    def   _snake_case       (      self      :							str  )		->Union[str, Any]:






        '''simple docstring'''






        _UpperCAmelCase        = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name  )
        return model







    @slow
    def   _snake_case       (      self      :							Any  )		->int:






        '''simple docstring'''






        _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=__UpperCamelCase  ,       )
        _UpperCAmelCase        = self.tokenizer.batch_decode(generated_ids.numpy()  ,       skip_special_tokens=__UpperCamelCase  )[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 gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionSAGPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()




class 		a_    (			_UpperCAmelCase  ,    _UpperCAmelCase  ,    unittest.TestCase						):
    a   :					Dict			     =					StableDiffusionSAGPipeline
    a   :					Any			     =					TEXT_TO_IMAGE_PARAMS
    a   :					Optional[Any]			     =					TEXT_TO_IMAGE_BATCH_PARAMS
    a   :					Optional[int]			     =					TEXT_TO_IMAGE_IMAGE_PARAMS
    a   :					Optional[int]			     =					TEXT_TO_IMAGE_IMAGE_PARAMS
    a   :					Tuple			     =					False







    def   _snake_case       (      self      :							List[Any]  )		->Tuple:






        '''simple docstring'''






        torch.manual_seed(0  )
        _UpperCAmelCase        = UNetaDConditionModel(
            block_out_channels=(32, 64)  ,       layers_per_block=2  ,       sample_size=32  ,       in_channels=4  ,       out_channels=4  ,       down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")  ,       up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")  ,       cross_attention_dim=32  ,       )
        _UpperCAmelCase        = DDIMScheduler(
            beta_start=0.0_0_0_8_5  ,       beta_end=0.0_1_2  ,       beta_schedule="""scaled_linear"""  ,       clip_sample=__UpperCamelCase  ,       set_alpha_to_one=__UpperCamelCase  ,       )
        torch.manual_seed(0  )
        _UpperCAmelCase        = AutoencoderKL(
            block_out_channels=[32, 64]  ,       in_channels=3  ,       out_channels=3  ,       down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]  ,       up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]  ,       latent_channels=4  ,       )
        torch.manual_seed(0  )
        _UpperCAmelCase        = CLIPTextConfig(
            bos_token_id=0  ,       eos_token_id=2  ,       hidden_size=32  ,       intermediate_size=37  ,       layer_norm_eps=1e-05  ,       num_attention_heads=4  ,       num_hidden_layers=5  ,       pad_token_id=1  ,       vocab_size=10_00  ,       )
        _UpperCAmelCase        = CLIPTextModel(__UpperCamelCase  )
        _UpperCAmelCase        = CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""  )

        _UpperCAmelCase        = {
            """unet""": unet,
            """scheduler""": scheduler,
            """vae""": vae,
            """text_encoder""": text_encoder,
            """tokenizer""": tokenizer,
            """safety_checker""": None,
            """feature_extractor""": None,
        }
        return components







    def   _snake_case       (      self      :							Union[str, Any]  ,       __UpperCamelCase      :							Union[str, Any]  ,       __UpperCamelCase      :							Any=0  )		->Any:






        '''simple docstring'''






        if str(__UpperCamelCase  ).startswith("""mps"""  ):
            _UpperCAmelCase        = torch.manual_seed(__UpperCamelCase  )
        else:
            _UpperCAmelCase        = torch.Generator(device=__UpperCamelCase  ).manual_seed(__UpperCamelCase  )
        _UpperCAmelCase        = {
            """prompt""": """.""",
            """generator""": generator,
            """num_inference_steps""": 2,
            """guidance_scale""": 1.0,
            """sag_scale""": 1.0,
            """output_type""": """numpy""",
        }
        return inputs







    def   _snake_case       (      self      :							Optional[Any]  )		->Optional[int]:






        '''simple docstring'''






        super().test_inference_batch_single_identical(expected_max_diff=3e-3  )







@slow
@require_torch_gpu
class 		a_    (			unittest.TestCase						):







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






        '''simple docstring'''






        super().tearDown()
        gc.collect()
        torch.cuda.empty_cache()







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






        '''simple docstring'''






        _UpperCAmelCase        = StableDiffusionSAGPipeline.from_pretrained("""CompVis/stable-diffusion-v1-4"""  )
        _UpperCAmelCase        = sag_pipe.to(__UpperCamelCase  )
        sag_pipe.set_progress_bar_config(disable=__UpperCamelCase  )

        _UpperCAmelCase        = """."""
        _UpperCAmelCase        = torch.manual_seed(0  )
        _UpperCAmelCase        = sag_pipe(
            [prompt]  ,       generator=__UpperCamelCase  ,       guidance_scale=7.5  ,       sag_scale=1.0  ,       num_inference_steps=20  ,       output_type="""np"""  )

        _UpperCAmelCase        = output.images

        _UpperCAmelCase        = image[0, -3:, -3:, -1]

        assert image.shape == (1, 5_12, 5_12, 3)
        _UpperCAmelCase        = np.array([0.1_5_6_8, 0.1_7_3_8, 0.1_6_9_5, 0.1_6_9_3, 0.1_5_0_7, 0.1_7_0_5, 0.1_5_4_7, 0.1_7_5_1, 0.1_9_4_9]  )

        assert np.abs(image_slice.flatten() - expected_slice  ).max() < 5e-2







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






        '''simple docstring'''






        _UpperCAmelCase        = StableDiffusionSAGPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""  )
        _UpperCAmelCase        = sag_pipe.to(__UpperCamelCase  )
        sag_pipe.set_progress_bar_config(disable=__UpperCamelCase  )

        _UpperCAmelCase        = """."""
        _UpperCAmelCase        = torch.manual_seed(0  )
        _UpperCAmelCase        = sag_pipe(
            [prompt]  ,       generator=__UpperCamelCase  ,       guidance_scale=7.5  ,       sag_scale=1.0  ,       num_inference_steps=20  ,       output_type="""np"""  )

        _UpperCAmelCase        = output.images

        _UpperCAmelCase        = image[0, -3:, -3:, -1]

        assert image.shape == (1, 5_12, 5_12, 3)
        _UpperCAmelCase        = np.array([0.3_4_5_9, 0.2_8_7_6, 0.2_5_3_7, 0.3_0_0_2, 0.2_6_7_1, 0.2_1_6_0, 0.3_0_2_6, 0.2_2_6_2, 0.2_3_7_1]  )

        assert np.abs(image_slice.flatten() - expected_slice  ).max() < 5e-2







    def   _snake_case       (      self      :							str  )		->str:






        '''simple docstring'''






        _UpperCAmelCase        = StableDiffusionSAGPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""  )
        _UpperCAmelCase        = sag_pipe.to(__UpperCamelCase  )
        sag_pipe.set_progress_bar_config(disable=__UpperCamelCase  )

        _UpperCAmelCase        = """."""
        _UpperCAmelCase        = torch.manual_seed(0  )
        _UpperCAmelCase        = sag_pipe(
            [prompt]  ,       width=7_68  ,       height=5_12  ,       generator=__UpperCamelCase  ,       guidance_scale=7.5  ,       sag_scale=1.0  ,       num_inference_steps=20  ,       output_type="""np"""  ,       )

        _UpperCAmelCase        = output.images

        assert image.shape == (1, 5_12, 7_68, 3)