





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-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/config.json''',
    # See all Blenderbot models at https://huggingface.co/models?filter=blenderbot
}




class 	_UpperCAmelCase						( A_       ):
					__A        = "blenderbot"
					__A        = ["past_key_values"]
					__A        = {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}




					def __init__(    self     ,   __lowerCAmelCase=8_008     ,   __lowerCAmelCase=128     ,   __lowerCAmelCase=2     ,   __lowerCAmelCase=10_240     ,   __lowerCAmelCase=32     ,   __lowerCAmelCase=24     ,   __lowerCAmelCase=10_240     ,   __lowerCAmelCase=32     ,   __lowerCAmelCase=0.0     ,   __lowerCAmelCase=0.0     ,   __lowerCAmelCase=True     ,   __lowerCAmelCase=True     ,   __lowerCAmelCase="gelu"     ,   __lowerCAmelCase=2_560     ,   __lowerCAmelCase=0.1     ,   __lowerCAmelCase=0.0     ,   __lowerCAmelCase=0.0     ,   __lowerCAmelCase=0.0_2     ,   __lowerCAmelCase=1     ,   __lowerCAmelCase=False     ,   __lowerCAmelCase=0     ,   __lowerCAmelCase=1     ,   __lowerCAmelCase=2     ,   __lowerCAmelCase=3     ,   __lowerCAmelCase=2     ,   **__lowerCAmelCase     ,   )    ->	Optional[int]:
							UpperCamelCase__								=						vocab_size
							UpperCamelCase__								=						max_position_embeddings
							UpperCamelCase__								=						d_model
							UpperCamelCase__								=						encoder_ffn_dim
							UpperCamelCase__								=						encoder_layers
							UpperCamelCase__								=						encoder_attention_heads
							UpperCamelCase__								=						decoder_ffn_dim
							UpperCamelCase__								=						decoder_layers
							UpperCamelCase__								=						decoder_attention_heads
							UpperCamelCase__								=						dropout
							UpperCamelCase__								=						attention_dropout
							UpperCamelCase__								=						activation_dropout
							UpperCamelCase__								=						activation_function
							UpperCamelCase__								=						init_std
							UpperCamelCase__								=						encoder_layerdrop
							UpperCamelCase__								=						decoder_layerdrop
							UpperCamelCase__								=						use_cache
							UpperCamelCase__								=						encoder_layers
							UpperCamelCase__								=						scale_embedding  # scale factor will be sqrt(d_model) if True

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




class 	_UpperCAmelCase						( A_       ):




					@property
					def 						snake_case_		(    self							)    ->	Optional[Any]:



							if self.task in ["default", "seq2seq-lm"]:
									UpperCamelCase__								=						OrderedDict(
									    [
									        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
									        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
									    ]							)
									if self.use_past:
											UpperCamelCase__								=						{0: 'batch'}
											UpperCamelCase__								=						{0: 'batch', 1: 'past_decoder_sequence + sequence'}
									else:
											UpperCamelCase__								=						{0: 'batch', 1: 'decoder_sequence'}
											UpperCamelCase__								=						{0: 'batch', 1: 'decoder_sequence'}
									if self.use_past:
											self.fill_with_past_key_values_(__lowerCamelCase     ,   direction="""inputs"""							)
							elif self.task == "causal-lm":
									UpperCamelCase__								=						OrderedDict(
									    [
									        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
									        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
									    ]							)
									if self.use_past:
											UpperCamelCase__								=						self.num_layers
											for i in range(__lowerCamelCase							):
													UpperCamelCase__								=						{0: 'batch', 2: 'past_sequence + sequence'}
													UpperCamelCase__								=						{0: 'batch', 2: 'past_sequence + sequence'}
							else:
									UpperCamelCase__								=						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
					# Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs
					def 						snake_case_		(    self							)    ->	int:



							if self.task in ["default", "seq2seq-lm"]:
									UpperCamelCase__								=						super().outputs
							else:
									UpperCamelCase__								=						super(__lowerCamelCase     ,   self							).outputs
									if self.use_past:
											UpperCamelCase__								=						self.num_layers
											for i in range(__lowerCamelCase							):
													UpperCamelCase__								=						{0: 'batch', 2: 'past_sequence + sequence'}
													UpperCamelCase__								=						{0: 'batch', 2: 'past_sequence + sequence'}
							return common_outputs




					def 						snake_case_		(    self     ,   __lowerCAmelCase     ,   __lowerCAmelCase = -1     ,   __lowerCAmelCase = -1     ,   __lowerCAmelCase = False     ,   __lowerCAmelCase = None     ,   )    ->	Dict:
							UpperCamelCase__								=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
							    __lowerCamelCase     ,   __lowerCamelCase     ,   __lowerCamelCase     ,   __lowerCamelCase     ,   __lowerCamelCase							)
							# Generate decoder inputs
							UpperCamelCase__								=						seq_length if not self.use_past else 1
							UpperCamelCase__								=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
							    __lowerCamelCase     ,   __lowerCamelCase     ,   __lowerCamelCase     ,   __lowerCamelCase     ,   __lowerCamelCase							)
							UpperCamelCase__								=						{F"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
							UpperCamelCase__								=						dict(**__lowerCamelCase     ,   **__lowerCamelCase							)

							if self.use_past:
									if not is_torch_available():
											raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""							)
									else:
											import torch
									UpperCamelCase__								=						common_inputs['input_ids'].shape
									UpperCamelCase__								=						common_inputs['decoder_input_ids'].shape[1]
									UpperCamelCase__								=						self.num_attention_heads
									UpperCamelCase__								=						(
									    batch,
									    num_encoder_attention_heads,
									    encoder_seq_length,
									    self._config.hidden_size // num_encoder_attention_heads,
									)
									UpperCamelCase__								=						decoder_seq_length
									UpperCamelCase__								=						(
									    batch,
									    num_decoder_attention_heads,
									    decoder_past_length,
									    self._config.hidden_size // num_decoder_attention_heads,
									)
									UpperCamelCase__								=						torch.cat(
									    [common_inputs["""decoder_attention_mask"""], torch.ones(__lowerCamelCase     ,   __lowerCamelCase							)]     ,   dim=1							)
									UpperCamelCase__								=						[]
									UpperCamelCase__								=						self.num_layers

									for _ in range(__lowerCamelCase							):
											common_inputs["past_key_values"].append(
											    (
											        torch.zeros(__lowerCamelCase							),
											        torch.zeros(__lowerCamelCase							),
											        torch.zeros(__lowerCamelCase							),
											        torch.zeros(__lowerCamelCase							),
											    )							)
							return common_inputs




					def 						snake_case_		(    self     ,   __lowerCAmelCase     ,   __lowerCAmelCase = -1     ,   __lowerCAmelCase = -1     ,   __lowerCAmelCase = False     ,   __lowerCAmelCase = None     ,   )    ->	str:
							UpperCamelCase__								=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
							    __lowerCamelCase     ,   __lowerCamelCase     ,   __lowerCamelCase     ,   __lowerCamelCase     ,   __lowerCamelCase							)

							if self.use_past:
									if not is_torch_available():
											raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""							)
									else:
											import torch
									UpperCamelCase__								=						common_inputs['input_ids'].shape
									UpperCamelCase__								=						seqlen
									UpperCamelCase__								=						self.num_layers
									UpperCamelCase__								=						self.num_attention_heads
									UpperCamelCase__								=						(
									    batch,
									    num_encoder_attention_heads,
									    past_key_values_length,
									    self._config.hidden_size // num_encoder_attention_heads,
									)
									UpperCamelCase__								=						common_inputs['attention_mask'].dtype
									UpperCamelCase__								=						torch.cat(
									    [common_inputs["""attention_mask"""], torch.ones(__lowerCamelCase     ,   __lowerCamelCase     ,   dtype=__lowerCamelCase							)]     ,   dim=1							)
									UpperCamelCase__								=						[
									    (torch.zeros(__lowerCamelCase							), torch.zeros(__lowerCamelCase							)) for _ in range(__lowerCamelCase							)
									]
							return common_inputs




					def 						snake_case_		(    self     ,   __lowerCAmelCase     ,   __lowerCAmelCase = -1     ,   __lowerCAmelCase = -1     ,   __lowerCAmelCase = False     ,   __lowerCAmelCase = None     ,   )    ->	Dict:
							UpperCamelCase__								=						compute_effective_axis_dimension(
							    __lowerCamelCase     ,   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
							UpperCamelCase__								=						tokenizer.num_special_tokens_to_add(__lowerCamelCase							)
							UpperCamelCase__								=						compute_effective_axis_dimension(
							    __lowerCamelCase     ,   fixed_dimension=OnnxConfig.default_fixed_sequence     ,   num_token_to_add=__lowerCamelCase							)

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




					def 						snake_case_		(    self     ,   __lowerCAmelCase     ,   __lowerCAmelCase = -1     ,   __lowerCAmelCase = -1     ,   __lowerCAmelCase = False     ,   __lowerCAmelCase = None     ,   )    ->	int:



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

							elif self.task == "causal-lm":
									UpperCamelCase__								=						self._generate_dummy_inputs_for_causal_lm(
									    __lowerCamelCase     ,   batch_size=__lowerCamelCase     ,   seq_length=__lowerCamelCase     ,   is_pair=__lowerCamelCase     ,   framework=__lowerCamelCase							)
							else:
									UpperCamelCase__								=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
									    __lowerCamelCase     ,   batch_size=__lowerCamelCase     ,   seq_length=__lowerCamelCase     ,   is_pair=__lowerCamelCase     ,   framework=__lowerCamelCase							)

							return common_inputs




					def 						snake_case_		(    self     ,   __lowerCAmelCase     ,   __lowerCAmelCase     ,   __lowerCAmelCase     ,   __lowerCAmelCase							)    ->	Dict:



							if self.task in ["default", "seq2seq-lm"]:
									UpperCamelCase__								=						super()._flatten_past_key_values_(__lowerCamelCase     ,   __lowerCamelCase     ,   __lowerCamelCase     ,   __lowerCamelCase							)
							else:
									UpperCamelCase__								=						super(__lowerCamelCase     ,   self							)._flatten_past_key_values_(
									    __lowerCamelCase     ,   __lowerCamelCase     ,   __lowerCamelCase     ,   __lowerCamelCase							)




					def 						snake_case_		(    self     ,   __lowerCAmelCase     ,   __lowerCAmelCase							)    ->	int:



							if direction not in ["inputs", "outputs"]:
									raise ValueError(F"""direction must either be \"inputs\" or \"outputs\", but {direction} was given"""							)

							UpperCamelCase__								=						'past_key_values' if direction == 'inputs' else 'present'
							UpperCamelCase__								=						self.num_layers

							UpperCamelCase__								=						'past_encoder_sequence'
							UpperCamelCase__								=						'past_decoder_sequence' if direction == 'inputs' else 'past_decoder_sequence + sequence'

							for i in range(__lowerCamelCase							):
									UpperCamelCase__								=						{0: 'batch', 2: decoder_sequence}
									UpperCamelCase__								=						{0: 'batch', 2: decoder_sequence}
									UpperCamelCase__								=						{0: 'batch', 2: encoder_sequence}
									UpperCamelCase__								=						{0: 'batch', 2: encoder_sequence}






"""simple docstring"""







import random
import unittest

import torch

from diffusers import IFImgaImgPipeline
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 							__snake_case(					A_				,	A_				,	unittest.TestCase	):


    '''simple docstring'''





    _UpperCAmelCase       =			IFImgaImgPipeline
    _UpperCAmelCase       =			TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"width", "height"}
    _UpperCAmelCase       =			TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
    _UpperCAmelCase       =			PipelineTesterMixin.required_optional_params - {"latents"}
    def 						_a							(  self	):




         '''simple docstring'''



         return self._get_dummy_components()
    def 						_a							(  self					,       __lowerCamelCase					,       __lowerCamelCase=0	):




         '''simple docstring'''



         if str(__lowerCamelCase	).startswith('mps'	):
              __A					:					Tuple								=		torch.manual_seed(__lowerCamelCase	)
         else:
              __A					:					int								=		torch.Generator(device=__lowerCamelCase	).manual_seed(__lowerCamelCase	)

         __A					:					str								=		floats_tensor((1, 3, 32, 32)					,       rng=random.Random(__lowerCamelCase	)	).to(__lowerCamelCase	)

         __A					:					str								=		{
             'prompt': 'A painting of a squirrel eating a burger',
             'image': image,
             'generator': generator,
             'num_inference_steps': 2,
             'output_type': 'numpy',
         }

         return inputs
    def 						_a							(  self	):




         '''simple docstring'''



         self._test_save_load_optional_components()
    @unittest.skipIf(
        torch_device != 'cuda' or not is_xformers_available()					,       reason='XFormers attention is only available with CUDA and `xformers` installed'					,       )
    def 						_a							(  self	):




         '''simple docstring'''



         self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3	)
    @unittest.skipIf(torch_device != 'cuda'					,       reason='float16 requires CUDA'	)
    def 						_a							(  self	):




         '''simple docstring'''



         super().test_save_load_floataa(expected_max_diff=1e-1	)
    @unittest.skipIf(torch_device != 'cuda'					,       reason='float16 requires CUDA'	)
    def 						_a							(  self	):




         '''simple docstring'''



         super().test_floataa_inference(expected_max_diff=1e-1	)
    def 						_a							(  self	):




         '''simple docstring'''



         self._test_attention_slicing_forward_pass(expected_max_diff=1e-2	)
    def 						_a							(  self	):




         '''simple docstring'''



         self._test_save_load_local()

    def 						_a							(  self	):




         '''simple docstring'''



         self._test_inference_batch_single_identical(
             expected_max_diff=1e-2					,       )



