

"""simple docstring"""






from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...file_utils import TensorType, is_torch_available
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import logging


_lowerCAmelCase						=		logging.get_logger(__name__)

_lowerCAmelCase						=		{
    """facebook/blenderbot_small-90M""": """https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json""",
    # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small
}







class 	__UpperCamelCase    (    a__						):
  _UpperCAmelCase          =		"blenderbot-small"
  _UpperCAmelCase          =		["past_key_values"]
  _UpperCAmelCase          =		{"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}

  def __init__(		self    ,_A=5_0265    ,_A=512    ,_A=8    ,_A=2048    ,_A=16    ,_A=8    ,_A=2048    ,_A=16    ,_A=0.0    ,_A=0.0    ,_A=True    ,_A=True    ,_A="gelu"    ,_A=512    ,_A=0.1    ,_A=0.0    ,_A=0.0    ,_A=0.0_2    ,_A=1    ,_A=False    ,_A=0    ,_A=1    ,_A=2    ,_A=2    ,**_A    ,):
      '''simple docstring'''

      _lowerCAmelCase       :   List[Any]  =    vocab_size
      _lowerCAmelCase       :   Dict  =    max_position_embeddings
      _lowerCAmelCase       :   List[str]  =    d_model
      _lowerCAmelCase       :   List[Any]  =    encoder_ffn_dim
      _lowerCAmelCase       :   int  =    encoder_layers
      _lowerCAmelCase       :   List[str]  =    encoder_attention_heads
      _lowerCAmelCase       :   List[Any]  =    decoder_ffn_dim
      _lowerCAmelCase       :   Dict  =    decoder_layers
      _lowerCAmelCase       :   Tuple  =    decoder_attention_heads
      _lowerCAmelCase       :   str  =    dropout
      _lowerCAmelCase       :   Any  =    attention_dropout
      _lowerCAmelCase       :   str  =    activation_dropout
      _lowerCAmelCase       :   Dict  =    activation_function
      _lowerCAmelCase       :   Optional[Any]  =    init_std
      _lowerCAmelCase       :   Optional[Any]  =    encoder_layerdrop
      _lowerCAmelCase       :   Tuple  =    decoder_layerdrop
      _lowerCAmelCase       :   List[str]  =    use_cache
      _lowerCAmelCase       :   Union[str, Any]  =    encoder_layers
      _lowerCAmelCase       :   Union[str, Any]  =    scale_embedding  # scale factor will be sqrt(d_model) if True

      super().__init__(
          pad_token_id=_A    ,bos_token_id=_A    ,eos_token_id=_A    ,is_encoder_decoder=_A    ,decoder_start_token_id=_A    ,forced_eos_token_id=_A    ,**_A    ,)







class 	__UpperCamelCase    (    a__						):

  @property
  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      if self.task in ["default", "seq2seq-lm"]:
          _lowerCAmelCase       :   Union[str, Any]  =    OrderedDict(
              [
                  ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
                  ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
              ]      )

          if self.use_past:
              _lowerCAmelCase       :   str  =    {0: 'batch'}
              _lowerCAmelCase       :   Optional[Any]  =    {0: 'batch', 1: 'past_decoder_sequence + sequence'}
          else:
              _lowerCAmelCase       :   Optional[Any]  =    {0: 'batch', 1: 'decoder_sequence'}
              _lowerCAmelCase       :   Optional[int]  =    {0: 'batch', 1: 'decoder_sequence'}

          if self.use_past:
              self.fill_with_past_key_values_(_A    ,direction='inputs'      )
      elif self.task == "causal-lm":
          # TODO: figure this case out.
          _lowerCAmelCase       :   Optional[Any]  =    OrderedDict(
              [
                  ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
                  ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
              ]      )
          if self.use_past:
              _lowerCAmelCase,					_lowerCAmelCase       :   Any  =    self.num_layers
              for i in range(_A      ):
                  _lowerCAmelCase       :   List[Any]  =    {0: 'batch', 2: 'past_sequence + sequence'}
                  _lowerCAmelCase       :   int  =    {0: 'batch', 2: 'past_sequence + sequence'}
      else:
          _lowerCAmelCase       :   List[Any]  =    OrderedDict(
              [
                  ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
                  ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
                  ('decoder_input_ids', {0: 'batch', 1: 'decoder_sequence'}),
                  ('decoder_attention_mask', {0: 'batch', 1: 'decoder_sequence'}),
              ]      )

      return common_inputs

  @property
  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      if self.task in ["default", "seq2seq-lm"]:
          _lowerCAmelCase       :   Union[str, Any]  =    super().outputs
      else:
          _lowerCAmelCase       :   Optional[int]  =    super(_A    ,self      ).outputs
          if self.use_past:
              _lowerCAmelCase,					_lowerCAmelCase       :   Optional[Any]  =    self.num_layers
              for i in range(_A      ):
                  _lowerCAmelCase       :   Any  =    {0: 'batch', 2: 'past_sequence + sequence'}
                  _lowerCAmelCase       :   Tuple  =    {0: 'batch', 2: 'past_sequence + sequence'}
      return common_outputs

  def __lowerCamelCase		(		self    ,_A    ,_A = -1    ,_A = -1    ,_A = False    ,_A = None    ,):
      '''simple docstring'''

      _lowerCAmelCase       :   List[str]  =    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
          _A    ,_A    ,_A    ,_A    ,_A      )

      # Generate decoder inputs
      _lowerCAmelCase       :   List[str]  =    seq_length if not self.use_past else 1
      _lowerCAmelCase       :   List[str]  =    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
          _A    ,_A    ,_A    ,_A    ,_A      )
      _lowerCAmelCase       :   str  =    {F"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
      _lowerCAmelCase       :   Optional[int]  =    dict(**_A    ,**_A      )

      if self.use_past:
          if not is_torch_available():
              raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.'      )
          else:
              import torch
          _lowerCAmelCase,					_lowerCAmelCase       :   str  =    common_inputs['input_ids'].shape
          _lowerCAmelCase       :   Tuple  =    common_inputs['decoder_input_ids'].shape[1]
          _lowerCAmelCase,					_lowerCAmelCase       :   int  =    self.num_attention_heads
          _lowerCAmelCase       :   Any  =    (
              batch,
              num_encoder_attention_heads,
              encoder_seq_length,
              self._config.hidden_size // num_encoder_attention_heads,
          )
          _lowerCAmelCase       :   List[Any]  =    decoder_seq_length + 3
          _lowerCAmelCase       :   List[str]  =    (
              batch,
              num_decoder_attention_heads,
              decoder_past_length,
              self._config.hidden_size // num_decoder_attention_heads,
          )

          _lowerCAmelCase       :   Optional[Any]  =    torch.cat(
              [common_inputs['decoder_attention_mask'], torch.ones(_A    ,_A      )]    ,dim=1      )

          _lowerCAmelCase       :   str  =    []
          # If the number of encoder and decoder layers are present in the model configuration, both are considered
          _lowerCAmelCase,					_lowerCAmelCase       :   Optional[Any]  =    self.num_layers
          _lowerCAmelCase       :   List[Any]  =    min(_A    ,_A      )
          _lowerCAmelCase       :   str  =    max(_A    ,_A      ) - min_num_layers
          _lowerCAmelCase       :   Any  =    'encoder' if num_encoder_layers > num_decoder_layers else 'decoder'

          for _ in range(_A      ):
              common_inputs["past_key_values"].append(
                  (
                      torch.zeros(_A      ),
                      torch.zeros(_A      ),
                      torch.zeros(_A      ),
                      torch.zeros(_A      ),
                  )      )
          # TODO: test this.
          _lowerCAmelCase       :   Dict  =    encoder_shape if remaining_side_name == 'encoder' else decoder_shape
          for _ in range(_A    ,_A      ):
              common_inputs["past_key_values"].append((torch.zeros(_A      ), torch.zeros(_A      ))      )
      return common_inputs

  def __lowerCamelCase		(		self    ,_A    ,_A = -1    ,_A = -1    ,_A = False    ,_A = None    ,):
      '''simple docstring'''

      _lowerCAmelCase       :   Union[str, Any]  =    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
          _A    ,_A    ,_A    ,_A    ,_A      )

      if self.use_past:
          if not is_torch_available():
              raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.'      )
          else:
              import torch
          _lowerCAmelCase,					_lowerCAmelCase       :   List[Any]  =    common_inputs['input_ids'].shape
          # Not using the same length for past_key_values
          _lowerCAmelCase       :   str  =    seqlen + 2
          _lowerCAmelCase,					_lowerCAmelCase       :   Dict  =    self.num_layers
          _lowerCAmelCase,					_lowerCAmelCase       :   Any  =    self.num_attention_heads
          _lowerCAmelCase       :   Optional[int]  =    (
              batch,
              num_encoder_attention_heads,
              past_key_values_length,
              self._config.hidden_size // num_encoder_attention_heads,
          )

          _lowerCAmelCase       :   Optional[int]  =    common_inputs['attention_mask'].dtype
          _lowerCAmelCase       :   Dict  =    torch.cat(
              [common_inputs['attention_mask'], torch.ones(_A    ,_A    ,dtype=_A      )]    ,dim=1      )
          _lowerCAmelCase       :   str  =    [
              (torch.zeros(_A      ), torch.zeros(_A      )) for _ in range(_A      )
          ]
      return common_inputs

  def __lowerCamelCase		(		self    ,_A    ,_A = -1    ,_A = -1    ,_A = False    ,_A = None    ,):
      '''simple docstring'''

      _lowerCAmelCase       :   str  =    compute_effective_axis_dimension(
          _A    ,fixed_dimension=OnnxConfig.default_fixed_batch    ,num_token_to_add=0      )

      # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
      _lowerCAmelCase       :   Optional[int]  =    tokenizer.num_special_tokens_to_add(_A      )
      _lowerCAmelCase       :   str  =    compute_effective_axis_dimension(
          _A    ,fixed_dimension=OnnxConfig.default_fixed_sequence    ,num_token_to_add=_A      )

      # Generate dummy inputs according to compute batch and sequence
      _lowerCAmelCase       :   str  =    [' '.join([tokenizer.unk_token]      ) * seq_length] * batch_size
      _lowerCAmelCase       :   Tuple  =    dict(tokenizer(_A    ,return_tensors=_A      )      )
      return common_inputs

  def __lowerCamelCase		(		self    ,_A    ,_A = -1    ,_A = -1    ,_A = False    ,_A = None    ,):
      '''simple docstring'''

      if self.task in ["default", "seq2seq-lm"]:
          _lowerCAmelCase       :   Dict  =    self._generate_dummy_inputs_for_default_and_seqaseq_lm(
              _A    ,batch_size=_A    ,seq_length=_A    ,is_pair=_A    ,framework=_A      )

      elif self.task == "causal-lm":
          _lowerCAmelCase       :   Optional[Any]  =    self._generate_dummy_inputs_for_causal_lm(
              _A    ,batch_size=_A    ,seq_length=_A    ,is_pair=_A    ,framework=_A      )
      else:
          _lowerCAmelCase       :   str  =    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
              _A    ,batch_size=_A    ,seq_length=_A    ,is_pair=_A    ,framework=_A      )

      return common_inputs







  def __lowerCamelCase		(		self    ,_A    ,_A    ,_A    ,_A      ):
      '''simple docstring'''

      if self.task in ["default", "seq2seq-lm"]:
          _lowerCAmelCase       :   Optional[Any]  =    super()._flatten_past_key_values_(_A    ,_A    ,_A    ,_A      )
      else:
          _lowerCAmelCase       :   str  =    super(_A    ,self      )._flatten_past_key_values_(
              _A    ,_A    ,_A    ,_A      )








"""simple docstring"""






import gc
import unittest

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

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

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


enable_full_determinism()







class 	__UpperCamelCase    (    a__							,							unittest.TestCase						):
  _UpperCAmelCase          =		LDMTextToImagePipeline
  _UpperCAmelCase          =		TEXT_TO_IMAGE_PARAMS - {
      "negative_prompt",
      "negative_prompt_embeds",
      "cross_attention_kwargs",
      "prompt_embeds",
  }
  _UpperCAmelCase          =		PipelineTesterMixin.required_optional_params - {
      "num_images_per_prompt",
      "callback",
      "callback_steps",
  }
  _UpperCAmelCase          =		TEXT_TO_IMAGE_BATCH_PARAMS
  _UpperCAmelCase          =		False

  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      torch.manual_seed(0      )
      _lowerCAmelCase       :   Optional[int]  =    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    ,)
      _lowerCAmelCase       :   Union[str, Any]  =    DDIMScheduler(
          beta_start=0.0_0_0_8_5    ,beta_end=0.0_1_2    ,beta_schedule='scaled_linear'    ,clip_sample=_A    ,set_alpha_to_one=_A    ,)
      torch.manual_seed(0      )
      _lowerCAmelCase       :   Union[str, Any]  =    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      )
      _lowerCAmelCase       :   Dict  =    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=1000    ,)
      _lowerCAmelCase       :   Tuple  =    CLIPTextModel(_A      )
      _lowerCAmelCase       :   Any  =    CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'      )

      _lowerCAmelCase       :   List[str]  =    {
          'unet': unet,
          'scheduler': scheduler,
          'vqvae': vae,
          'bert': text_encoder,
          'tokenizer': tokenizer,
      }
      return components

  def __lowerCamelCase		(		self    ,_A    ,_A=0      ):
      '''simple docstring'''

      if str(_A      ).startswith('mps'      ):
          _lowerCAmelCase       :   int  =    torch.manual_seed(_A      )
      else:
          _lowerCAmelCase       :   Optional[Any]  =    torch.Generator(device=_A      ).manual_seed(_A      )
      _lowerCAmelCase       :   List[Any]  =    {
          'prompt': 'A painting of a squirrel eating a burger',
          'generator': generator,
          'num_inference_steps': 2,
          'guidance_scale': 6.0,
          'output_type': 'numpy',
      }
      return inputs







  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Optional[int]  =    'cpu'  # ensure determinism for the device-dependent torch.Generator

      _lowerCAmelCase       :   int  =    self.get_dummy_components()
      _lowerCAmelCase       :   str  =    LDMTextToImagePipeline(**_A      )
      pipe.to(_A      )
      pipe.set_progress_bar_config(disable=_A      )

      _lowerCAmelCase       :   List[Any]  =    self.get_dummy_inputs(_A      )
      _lowerCAmelCase       :   Any  =    pipe(**_A      ).images
      _lowerCAmelCase       :   Optional[int]  =    image[0, -3:, -3:, -1]

      assert image.shape == (1, 16, 16, 3)
      _lowerCAmelCase       :   Tuple  =    np.array([0.6_1_0_1, 0.6_1_5_6, 0.5_6_2_2, 0.4_8_9_5, 0.6_6_6_1, 0.3_8_0_4, 0.5_7_4_8, 0.6_1_3_6, 0.5_0_1_4]      )

      assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1E-3







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

  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

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

  def __lowerCamelCase		(		self    ,_A    ,_A=torch.floataa    ,_A=0      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Optional[Any]  =    torch.manual_seed(_A      )
      _lowerCAmelCase       :   Union[str, Any]  =    np.random.RandomState(_A      ).standard_normal((1, 4, 32, 32)      )
      _lowerCAmelCase       :   Optional[Any]  =    torch.from_numpy(_A      ).to(device=_A    ,dtype=_A      )
      _lowerCAmelCase       :   List[str]  =    {
          'prompt': 'A painting of a squirrel eating a burger',
          'latents': latents,
          'generator': generator,
          'num_inference_steps': 3,
          'guidance_scale': 6.0,
          'output_type': 'numpy',
      }
      return inputs







  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      _lowerCAmelCase       :   List[str]  =    LDMTextToImagePipeline.from_pretrained('CompVis/ldm-text2im-large-256'      ).to(_A      )
      pipe.set_progress_bar_config(disable=_A      )

      _lowerCAmelCase       :   Optional[Any]  =    self.get_inputs(_A      )
      _lowerCAmelCase       :   List[Any]  =    pipe(**_A      ).images
      _lowerCAmelCase       :   Dict  =    image[0, -3:, -3:, -1].flatten()

      assert image.shape == (1, 256, 256, 3)
      _lowerCAmelCase       :   str  =    np.array([0.5_1_8_2_5, 0.5_2_8_5_0, 0.5_2_5_4_3, 0.5_4_2_5_8, 0.5_2_3_0_4, 0.5_2_5_6_9, 0.5_4_3_6_3, 0.5_5_2_7_6, 0.5_6_8_7_8]      )
      _lowerCAmelCase       :   Dict  =    np.abs(expected_slice - image_slice      ).max()
      assert max_diff < 1E-3







@nightly
@require_torch_gpu
class 	__UpperCamelCase    (    unittest.TestCase						):

  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

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

  def __lowerCamelCase		(		self    ,_A    ,_A=torch.floataa    ,_A=0      ):
      '''simple docstring'''

      _lowerCAmelCase       :   List[str]  =    torch.manual_seed(_A      )
      _lowerCAmelCase       :   Optional[int]  =    np.random.RandomState(_A      ).standard_normal((1, 4, 32, 32)      )
      _lowerCAmelCase       :   List[Any]  =    torch.from_numpy(_A      ).to(device=_A    ,dtype=_A      )
      _lowerCAmelCase       :   int  =    {
          'prompt': 'A painting of a squirrel eating a burger',
          'latents': latents,
          'generator': generator,
          'num_inference_steps': 50,
          'guidance_scale': 6.0,
          'output_type': 'numpy',
      }
      return inputs







  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      _lowerCAmelCase       :   List[str]  =    LDMTextToImagePipeline.from_pretrained('CompVis/ldm-text2im-large-256'      ).to(_A      )
      pipe.set_progress_bar_config(disable=_A      )

      _lowerCAmelCase       :   str  =    self.get_inputs(_A      )
      _lowerCAmelCase       :   Union[str, Any]  =    pipe(**_A      ).images[0]

      _lowerCAmelCase       :   int  =    load_numpy(
          'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/ldm_text2img/ldm_large_256_ddim.npy'      )
      _lowerCAmelCase       :   List[str]  =    np.abs(expected_image - image      ).max()
      assert max_diff < 1E-3





