
'''simple docstring'''

from __future__ import annotations

import unittest

import numpy as np

from transformers import OPTConfig, is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, slow

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 GPTaTokenizer, TFOPTForCausalLM, TFOPTModel


def 		_UpperCAmelCase (     _lowerCamelCase  :				List[Any]     ,					_lowerCamelCase  :				Union[str, Any]     ,					_lowerCamelCase  :				Any=None     ,					_lowerCamelCase  :				List[str]=None						)			->  Tuple:



  if attention_mask is None:
    _lowerCAmelCase				:     Optional[int]       =    tf.cast(tf.math.not_equal(A__     ,					config.pad_token_id						)     ,					tf.inta						)
  return {"input_ids": input_ids, "attention_mask": attention_mask}


@require_tf
class      a_     :
       __lowerCAmelCase			: Tuple			=					OPTConfig
       __lowerCAmelCase			: Optional[Any]			=					{}
       __lowerCAmelCase			: int			=					'gelu'




       def __init__(   self      ,      snake_case_      ,      snake_case_=1_3      ,      snake_case_=7      ,      snake_case_=True      ,      snake_case_=False      ,      snake_case_=9_9      ,      snake_case_=1_6      ,      snake_case_=2      ,      snake_case_=4      ,      snake_case_=4      ,      snake_case_="gelu"      ,      snake_case_=0.1      ,      snake_case_=0.1      ,      snake_case_=2_0      ,      snake_case_=2      ,      snake_case_=1      ,      snake_case_=0      ,      snake_case_=1_6      ,      snake_case_=1_6      ,      ):
         _lowerCAmelCase				:     Union[str, Any]       =    parent
         _lowerCAmelCase				:     List[Any]       =    batch_size
         _lowerCAmelCase				:     Optional[int]       =    seq_length
         _lowerCAmelCase				:     int       =    is_training
         _lowerCAmelCase				:     Optional[int]       =    use_labels
         _lowerCAmelCase				:     List[Any]       =    vocab_size
         _lowerCAmelCase				:     List[str]       =    hidden_size
         _lowerCAmelCase				:     Tuple       =    num_hidden_layers
         _lowerCAmelCase				:     Optional[int]       =    num_attention_heads
         _lowerCAmelCase				:     Optional[Any]       =    intermediate_size
         _lowerCAmelCase				:     Tuple       =    hidden_act
         _lowerCAmelCase				:     Optional[int]       =    hidden_dropout_prob
         _lowerCAmelCase				:     str       =    attention_probs_dropout_prob
         _lowerCAmelCase				:     str       =    max_position_embeddings
         _lowerCAmelCase				:     Optional[Any]       =    eos_token_id
         _lowerCAmelCase				:     Optional[int]       =    pad_token_id
         _lowerCAmelCase				:     int       =    bos_token_id
         _lowerCAmelCase				:     Any       =    embed_dim
         _lowerCAmelCase				:     List[Any]       =    word_embed_proj_dim
         _lowerCAmelCase				:     List[Any]       =    False




       def 							__UpperCamelCase				(   self     ):
         _lowerCAmelCase				:     Union[str, Any]       =    ids_tensor([self.batch_size, self.seq_length - 1]      ,      self.vocab_size     )
         _lowerCAmelCase				:     Any       =    tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size     )      ,      1     )
         _lowerCAmelCase				:     List[str]       =    tf.concat([input_ids, eos_tensor]      ,      axis=1     )

         _lowerCAmelCase				:     Optional[int]       =    self.config_cls(
             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      ,      word_embed_proj_dim=self.word_embed_proj_dim      ,      is_encoder_decoder=UpperCamelCase_      ,      **self.config_updates      ,      )
         _lowerCAmelCase				:     Any       =    prepare_opt_inputs_dict(UpperCamelCase_      ,      UpperCamelCase_     )
         return config, inputs_dict







       def 							__UpperCamelCase				(   self      ,      snake_case_      ,      snake_case_     ):
         _lowerCAmelCase				:     Union[str, Any]       =    TFOPTModel(config=UpperCamelCase_     )
         _lowerCAmelCase				:     int       =    inputs_dict["""input_ids"""]

         _lowerCAmelCase				:     List[str]       =    input_ids[:1, :]
         _lowerCAmelCase				:     Optional[int]       =    inputs_dict["""attention_mask"""][:1, :]
         _lowerCAmelCase				:     Any       =    1

         # first forward pass
         _lowerCAmelCase				:     Union[str, Any]       =    model(UpperCamelCase_      ,      attention_mask=UpperCamelCase_      ,      use_cache=UpperCamelCase_     )

         _lowerCAmelCase ,    _lowerCAmelCase				:     List[str]       =    outputs.to_tuple()

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

         # append to next input_ids and
         _lowerCAmelCase				:     Union[str, Any]       =    tf.concat([input_ids, next_tokens]      ,      axis=-1     )
         _lowerCAmelCase				:     str       =    tf.concat([attention_mask, next_attn_mask]      ,      axis=-1     )

         _lowerCAmelCase				:     List[str]       =    model(UpperCamelCase_      ,      attention_mask=UpperCamelCase_     )[0]
         _lowerCAmelCase				:     Optional[Any]       =    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
         _lowerCAmelCase				:     Union[str, Any]       =    int(ids_tensor((1,)      ,      output_from_past.shape[-1]     )     )
         _lowerCAmelCase				:     List[str]       =    output_from_no_past[:, -3:, random_slice_idx]
         _lowerCAmelCase				:     str       =    output_from_past[:, :, random_slice_idx]

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



@require_tf
class      a_     (__lowerCamelCase							,	__lowerCamelCase							,	unittest.TestCase			):
       __lowerCAmelCase			: List[Any]			=					(TFOPTModel, TFOPTForCausalLM) if is_tf_available() else ()
       __lowerCAmelCase			: Tuple			=					(TFOPTForCausalLM,) if is_tf_available() else ()
       __lowerCAmelCase			: str			=					(
           {'feature-extraction': TFOPTModel, 'text-generation': TFOPTForCausalLM} if is_tf_available() else {}
       )
       __lowerCAmelCase			: List[Any]			=					False
       __lowerCAmelCase			: Optional[int]			=					False
       __lowerCAmelCase			: int			=					False
       __lowerCAmelCase			: Any			=					1_0




       def 							__UpperCamelCase				(   self     ):
         _lowerCAmelCase				:     Dict       =    TFOPTModelTester(self     )
         _lowerCAmelCase				:     List[str]       =    ConfigTester(self      ,      config_class=UpperCamelCase_     )




       def 							__UpperCamelCase				(   self     ):
         self.config_tester.run_common_tests()




       def 							__UpperCamelCase				(   self     ):
         _lowerCAmelCase				:     Union[str, Any]       =    self.model_tester.prepare_config_and_inputs_for_common()
         self.model_tester.check_decoder_model_past_large_inputs(*UpperCamelCase_     )







       def 							__UpperCamelCase				(   self     ):
         _lowerCAmelCase ,    _lowerCAmelCase				:     Dict       =    self.model_tester.prepare_config_and_inputs_for_common()

         def _get_word_embedding_weight(snake_case_      ,      snake_case_     ):
           if hasattr(UpperCamelCase_      ,      """weight"""     ):
             return embedding_layer.weight
           else:
             # Here we build the word embeddings weights if not exists.
             # And then we retry to get the attribute once built.
             model.build()
             if hasattr(UpperCamelCase_      ,      """weight"""     ):
               return embedding_layer.weight
             else:
               return None

         for model_class in self.all_model_classes:
           for size in [config.vocab_size - 1_0, config.vocab_size + 1_0]:
             # build the embeddings
             _lowerCAmelCase				:     Optional[int]       =    model_class(config=UpperCamelCase_     )
             _lowerCAmelCase				:     List[Any]       =    _get_word_embedding_weight(UpperCamelCase_      ,      model.get_input_embeddings()     )
             _lowerCAmelCase				:     List[str]       =    _get_word_embedding_weight(UpperCamelCase_      ,      model.get_output_embeddings()     )

             # reshape the embeddings
             model.resize_token_embeddings(UpperCamelCase_     )
             _lowerCAmelCase				:     List[str]       =    _get_word_embedding_weight(UpperCamelCase_      ,      model.get_input_embeddings()     )
             _lowerCAmelCase				:     Any       =    _get_word_embedding_weight(UpperCamelCase_      ,      model.get_output_embeddings()     )

             # check that the resized embeddings size matches the desired size.
             _lowerCAmelCase				:     List[str]       =    size if size is not None else config.vocab_size

             self.assertEqual(new_input_embeddings.shape[0]      ,      UpperCamelCase_     )

             # check that weights remain the same after resizing
             _lowerCAmelCase				:     Optional[Any]       =    True
             for pa, pa in zip(old_input_embeddings.value()      ,      new_input_embeddings.value()     ):
               if tf.math.reduce_sum(tf.math.abs(pa - pa     )     ) > 0:
                 _lowerCAmelCase				:     Optional[Any]       =    False
             self.assertTrue(UpperCamelCase_     )

             if old_output_embeddings is not None and new_output_embeddings is not None:
               self.assertEqual(new_output_embeddings.shape[0]      ,      UpperCamelCase_     )

               _lowerCAmelCase				:     Union[str, Any]       =    True
               for pa, pa in zip(old_output_embeddings.value()      ,      new_output_embeddings.value()     ):
                 if tf.math.reduce_sum(tf.math.abs(pa - pa     )     ) > 0:
                   _lowerCAmelCase				:     Any       =    False
               self.assertTrue(UpperCamelCase_     )



def 		_UpperCAmelCase (     _lowerCamelCase  :				Union[str, Any]						)			->  Any:
  return tf.constant(A__     ,					dtype=tf.intaa						)


@require_tf
class      a_     (unittest.TestCase			):
       __lowerCAmelCase			: Dict			=					9_9




       def 							__UpperCamelCase				(   self     ):
         _lowerCAmelCase				:     Optional[Any]       =    tf.ones((4, 1)      ,      dtype=tf.intaa     ) * 2
         _lowerCAmelCase				:     Optional[Any]       =    tf.concat([ids_tensor((4, 6)      ,      self.vocab_size - 3     ) + 3, eos_column_vector]      ,      axis=1     )
         _lowerCAmelCase				:     Optional[int]       =    input_ids.shape[0]
         _lowerCAmelCase				:     Tuple       =    OPTConfig(
             vocab_size=self.vocab_size      ,      hidden_size=2_4      ,      num_hidden_layers=2      ,      num_attention_heads=2      ,      ffn_dim=3_2      ,      max_position_embeddings=4_8      ,      eos_token_id=2      ,      pad_token_id=1      ,      bos_token_id=0      ,      )
         return config, input_ids, batch_size



@require_sentencepiece
@require_tf
class      a_     (unittest.TestCase			):




       @slow
       def 							__UpperCamelCase				(   self     ):
         _lowerCAmelCase				:     Optional[int]       =    TFOPTModel.from_pretrained("""facebook/opt-350m"""     )
         _lowerCAmelCase				:     str       =    _long_tensor([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]]     )
         _lowerCAmelCase				:     Dict       =    tf.not_equal(UpperCamelCase_      ,      model.config.pad_token_id     )
         with tf.GradientTape():
           _lowerCAmelCase				:     Dict       =    model(input_ids=UpperCamelCase_      ,      attention_mask=UpperCamelCase_     ).last_hidden_state
         _lowerCAmelCase				:     Tuple       =    (1, 1_1, 5_1_2)
         self.assertEqual(output.shape      ,      UpperCamelCase_     )
         _lowerCAmelCase				:     int       =    tf.constant(
             [[-0.2873, -1.9218, -0.3033], [-1.2710, -0.1338, -0.1902], [0.4095, 0.1214, -1.3121]]     )
         self.assertTrue(np.allclose(output[:, :3, :3]      ,      UpperCamelCase_      ,      atol=4E-3     )     )

         _lowerCAmelCase				:     Optional[int]       =    tf.function(UpperCamelCase_      ,      jit_compile=UpperCamelCase_     )
         _lowerCAmelCase				:     Tuple       =    xla_generate(UpperCamelCase_      ,      UpperCamelCase_     )[0]
         self.assertTrue(np.allclose(output[:, :3, :3]      ,      UpperCamelCase_      ,      atol=4E-2     )     )



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




       def 							__UpperCamelCase				(   self     ):
         super().setUp()
         _lowerCAmelCase				:     Union[str, Any]       =    """facebook/opt-350m"""







       def 							__UpperCamelCase				(   self     ):
         _lowerCAmelCase				:     Tuple       =    TFOPTForCausalLM.from_pretrained(self.path_model     )
         _lowerCAmelCase				:     Union[str, Any]       =    GPTaTokenizer.from_pretrained(self.path_model     )

         _lowerCAmelCase				:     Union[str, Any]       =    [
             """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
         _lowerCAmelCase				:     List[Any]       =    tokenizer(UpperCamelCase_      ,      return_tensors="""tf"""      ,      padding=UpperCamelCase_      ,      add_special_tokens=UpperCamelCase_     )
         _lowerCAmelCase				:     Dict       =    tf.math.reduce_mean(model(inputs.input_ids      ,      attention_mask=inputs.attention_mask     )[0]      ,      axis=-1     )
         _lowerCAmelCase				:     Optional[Any]       =    tf.constant(
             [
                 [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(np.allclose(UpperCamelCase_      ,      UpperCamelCase_      ,      atol=1E-4     )     )

         _lowerCAmelCase				:     int       =    tf.function(UpperCamelCase_      ,      jit_compile=UpperCamelCase_     )
         _lowerCAmelCase				:     List[str]       =    tf.math.reduce_mean(xla_generate(inputs.input_ids      ,      attention_mask=inputs.attention_mask     )[0]      ,      axis=-1     )
         self.assertTrue(np.allclose(UpperCamelCase_      ,      UpperCamelCase_      ,      atol=1E-4     )     )



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




       @property
       def 							__UpperCamelCase				(   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 							__UpperCamelCase				(   self     ):
         _lowerCAmelCase				:     str       =    """facebook/opt-125m"""

         _lowerCAmelCase				:     Optional[int]       =    [
             """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""",
         ]

         _lowerCAmelCase				:     Tuple       =    []
         _lowerCAmelCase				:     Dict       =    GPTaTokenizer.from_pretrained(UpperCamelCase_     )
         _lowerCAmelCase				:     Optional[int]       =    TFOPTForCausalLM.from_pretrained(UpperCamelCase_     )

         for prompt in self.prompts:
           _lowerCAmelCase				:     Any       =    tokenizer(UpperCamelCase_      ,      return_tensors="""tf"""     ).input_ids

           _lowerCAmelCase				:     Optional[Any]       =    model.generate(UpperCamelCase_      ,      max_length=1_0     )

           _lowerCAmelCase				:     Any       =    tokenizer.batch_decode(UpperCamelCase_      ,      skip_special_tokens=UpperCamelCase_     )
           predicted_outputs += generated_string

         self.assertListEqual(UpperCamelCase_      ,      UpperCamelCase_     )




       def 							__UpperCamelCase				(   self     ):
         _lowerCAmelCase				:     int       =    """facebook/opt-350m"""

         _lowerCAmelCase				:     str       =    GPTaTokenizer.from_pretrained(UpperCamelCase_     )
         _lowerCAmelCase				:     Any       =    TFOPTForCausalLM.from_pretrained(UpperCamelCase_     )

         _lowerCAmelCase				:     Dict       =    """left"""

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

         _lowerCAmelCase				:     Tuple       =    tokenizer(UpperCamelCase_      ,      return_tensors="""tf"""      ,      padding=UpperCamelCase_     )
         _lowerCAmelCase				:     Any       =    inputs["""input_ids"""]

         _lowerCAmelCase				:     int       =    model.generate(input_ids=UpperCamelCase_      ,      attention_mask=inputs["""attention_mask"""]     )

         _lowerCAmelCase				:     Tuple       =    tokenizer(sentences[0]      ,      return_tensors="""tf"""     ).input_ids
         _lowerCAmelCase				:     Dict       =    model.generate(input_ids=UpperCamelCase_     )

         _lowerCAmelCase				:     int       =    inputs_non_padded.shape[-1] - tf.math.reduce_sum(
             tf.cast(inputs["""attention_mask"""][-1]      ,      tf.intaa     )     )
         _lowerCAmelCase				:     Tuple       =    tokenizer(sentences[1]      ,      return_tensors="""tf"""     ).input_ids
         _lowerCAmelCase				:     Optional[int]       =    model.generate(input_ids=UpperCamelCase_      ,      max_length=model.config.max_length - num_paddings     )

         _lowerCAmelCase				:     List[Any]       =    tokenizer.batch_decode(UpperCamelCase_      ,      skip_special_tokens=UpperCamelCase_     )
         _lowerCAmelCase				:     str       =    tokenizer.decode(output_non_padded[0]      ,      skip_special_tokens=UpperCamelCase_     )
         _lowerCAmelCase				:     List[Any]       =    tokenizer.decode(output_padded[0]      ,      skip_special_tokens=UpperCamelCase_     )

         _lowerCAmelCase				:     Dict       =    [
             """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(UpperCamelCase_      ,      UpperCamelCase_     )
         self.assertListEqual(UpperCamelCase_      ,      [non_padded_sentence, padded_sentence]     )







       def 							__UpperCamelCase				(   self     ):
         _lowerCAmelCase				:     Dict       =    """facebook/opt-350m"""

         _lowerCAmelCase				:     str       =    [
             """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""",
         ]

         _lowerCAmelCase				:     Tuple       =    []
         _lowerCAmelCase				:     str       =    GPTaTokenizer.from_pretrained(UpperCamelCase_     )
         _lowerCAmelCase				:     Optional[int]       =    TFOPTForCausalLM.from_pretrained(UpperCamelCase_     )

         for prompt in self.prompts:
           _lowerCAmelCase				:     str       =    tokenizer(UpperCamelCase_      ,      return_tensors="""tf"""     ).input_ids

           _lowerCAmelCase				:     int       =    model.generate(UpperCamelCase_      ,      max_length=1_0     )

           _lowerCAmelCase				:     str       =    tokenizer.batch_decode(UpperCamelCase_      ,      skip_special_tokens=UpperCamelCase_     )
           predicted_outputs += generated_string

         self.assertListEqual(UpperCamelCase_      ,      UpperCamelCase_     )








from typing import List, Optional, Union

import torch

from ...models import UNetaDConditionModel, VQModel
from ...pipelines import DiffusionPipeline
from ...pipelines.pipeline_utils import ImagePipelineOutput
from ...schedulers import DDPMScheduler
from ...utils import (
    is_accelerate_available,
    is_accelerate_version,
    logging,
    randn_tensor,
    replace_example_docstring,
)


UpperCAmelCase_									=	logging.get_logger(__name__)  # pylint: disable=invalid-name

UpperCAmelCase_									=	'\n    Examples:\n        ```py\n        >>> import torch\n        >>> import numpy as np\n\n        >>> from diffusers import KandinskyV22PriorPipeline, KandinskyV22ControlnetPipeline\n        >>> from transformers import pipeline\n        >>> from diffusers.utils import load_image\n\n\n        >>> def make_hint(image, depth_estimator):\n        ...     image = depth_estimator(image)["depth"]\n        ...     image = np.array(image)\n        ...     image = image[:, :, None]\n        ...     image = np.concatenate([image, image, image], axis=2)\n        ...     detected_map = torch.from_numpy(image).float() / 255.0\n        ...     hint = detected_map.permute(2, 0, 1)\n        ...     return hint\n\n\n        >>> depth_estimator = pipeline("depth-estimation")\n\n        >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(\n        ...     "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16\n        ... )\n        >>> pipe_prior = pipe_prior.to("cuda")\n\n        >>> pipe = KandinskyV22ControlnetPipeline.from_pretrained(\n        ...     "kandinsky-community/kandinsky-2-2-controlnet-depth", torch_dtype=torch.float16\n        ... )\n        >>> pipe = pipe.to("cuda")\n\n\n        >>> img = load_image(\n        ...     "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"\n        ...     "/kandinsky/cat.png"\n        ... ).resize((768, 768))\n\n        >>> hint = make_hint(img, depth_estimator).unsqueeze(0).half().to("cuda")\n\n        >>> prompt = "A robot, 4k photo"\n        >>> negative_prior_prompt = "lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature"\n\n        >>> generator = torch.Generator(device="cuda").manual_seed(43)\n\n        >>> image_emb, zero_image_emb = pipe_prior(\n        ...     prompt=prompt, negative_prompt=negative_prior_prompt, generator=generator\n        ... ).to_tuple()\n\n        >>> images = pipe(\n        ...     image_embeds=image_emb,\n        ...     negative_image_embeds=zero_image_emb,\n        ...     hint=hint,\n        ...     num_inference_steps=50,\n        ...     generator=generator,\n        ...     height=768,\n        ...     width=768,\n        ... ).images\n\n        >>> images[0].save("robot_cat.png")\n        ```\n'




def 					lowerCamelCase__       (						A__       :					Optional[int]  ,			A__       :					Dict  ,			A__       :					Optional[int]=8	):





       '''simple docstring'''



       __lowerCamelCase   =						height // scale_factor**2
       if height % scale_factor**2 != 0:
              new_height += 1
       __lowerCamelCase   =						width // scale_factor**2
       if width % scale_factor**2 != 0:
              new_width += 1
       return new_height * scale_factor, new_width * scale_factor





class     lowerCamelCase__(					__lowerCamelCase):
      def __init__(				self: List[Any]    ,       UpperCamelCase_: UNetaDConditionModel    ,       UpperCamelCase_: DDPMScheduler    ,       UpperCamelCase_: VQModel    ,       ):
             super().__init__()

             self.register_modules(
                 unet=UpperCamelCase_    ,       scheduler=UpperCamelCase_    ,       movq=UpperCamelCase_    ,       )
             __lowerCamelCase   =						2 ** (len(self.movq.config.block_out_channels   ) - 1)
      def 			lowerCAmelCase__						(				self: int    ,       UpperCamelCase_: Any    ,       UpperCamelCase_: Tuple    ,       UpperCamelCase_: int    ,       UpperCamelCase_: Dict    ,       UpperCamelCase_: Dict    ,       UpperCamelCase_: int   ):
             if latents is None:
                    __lowerCamelCase   =						randn_tensor(UpperCamelCase_    ,       generator=UpperCamelCase_    ,       device=UpperCamelCase_    ,       dtype=UpperCamelCase_   )
             else:
                    if latents.shape != shape:
                           raise ValueError(F'Unexpected latents shape, got {latents.shape}, expected {shape}'   )
                    __lowerCamelCase   =						latents.to(UpperCamelCase_   )

             __lowerCamelCase   =						latents * scheduler.init_noise_sigma
             return latents
      def 			lowerCAmelCase__						(				self: Dict    ,       UpperCamelCase_: str=0   ):
             if is_accelerate_available():
                    from accelerate import cpu_offload
             else:
                    raise ImportError("""Please install accelerate via `pip install accelerate`"""   )

             __lowerCamelCase   =						torch.device(F'cuda:{gpu_id}'   )

             __lowerCamelCase   =						[
                 self.unet,
                 self.movq,
             ]
             for cpu_offloaded_model in models:
                    if cpu_offloaded_model is not None:
                           cpu_offload(UpperCamelCase_    ,       UpperCamelCase_   )
      def 			lowerCAmelCase__						(				self: List[str]    ,       UpperCamelCase_: Optional[int]=0   ):
             if is_accelerate_available() and is_accelerate_version(""">="""    ,       """0.17.0.dev0"""   ):
                    from accelerate import cpu_offload_with_hook
             else:
                    raise ImportError("""`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher."""   )

             __lowerCamelCase   =						torch.device(F'cuda:{gpu_id}'   )

             if self.device.type != "cpu":
                    self.to("""cpu"""    ,       silence_dtype_warnings=UpperCamelCase_   )
                    torch.cuda.empty_cache()  # otherwise we don't see the memory savings (but they probably exist)

             __lowerCamelCase   =						None
             for cpu_offloaded_model in [self.unet, self.movq]:
                    __lowerCamelCase,   __lowerCamelCase   =						cpu_offload_with_hook(UpperCamelCase_    ,       UpperCamelCase_    ,       prev_module_hook=UpperCamelCase_   )

             # We'll offload the last model manually.
             __lowerCamelCase   =						hook
      @property
      # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
      def 			lowerCAmelCase__						(				self: int   ):
             if not hasattr(self.unet    ,       """_hf_hook"""   ):
                    return self.device
             for module in self.unet.modules():
                    if (
                        hasattr(UpperCamelCase_    ,       """_hf_hook"""   )
                        and hasattr(module._hf_hook    ,       """execution_device"""   )
                        and module._hf_hook.execution_device is not None
                    ):
                           return torch.device(module._hf_hook.execution_device   )
             return self.device




      @torch.no_grad()
      @replace_example_docstring(UpperCamelCase_   )
      def __call__(				self: Tuple    ,       UpperCamelCase_: Union[torch.FloatTensor, List[torch.FloatTensor]]    ,       UpperCamelCase_: Union[torch.FloatTensor, List[torch.FloatTensor]]    ,       UpperCamelCase_: torch.FloatTensor    ,       UpperCamelCase_: int = 5_12    ,       UpperCamelCase_: int = 5_12    ,       UpperCamelCase_: int = 1_00    ,       UpperCamelCase_: float = 4.0    ,       UpperCamelCase_: int = 1    ,       UpperCamelCase_: Optional[Union[torch.Generator, List[torch.Generator]]] = None    ,       UpperCamelCase_: Optional[torch.FloatTensor] = None    ,       UpperCamelCase_: Optional[str] = "pil"    ,       UpperCamelCase_: bool = True    ,       ):
             __lowerCamelCase   =						self._execution_device

             __lowerCamelCase   =						guidance_scale > 1.0

             if isinstance(UpperCamelCase_    ,       UpperCamelCase_   ):
                    __lowerCamelCase   =						torch.cat(UpperCamelCase_    ,       dim=0   )
             if isinstance(UpperCamelCase_    ,       UpperCamelCase_   ):
                    __lowerCamelCase   =						torch.cat(UpperCamelCase_    ,       dim=0   )
             if isinstance(UpperCamelCase_    ,       UpperCamelCase_   ):
                    __lowerCamelCase   =						torch.cat(UpperCamelCase_    ,       dim=0   )

             __lowerCamelCase   =						image_embeds.shape[0] * num_images_per_prompt

             if do_classifier_free_guidance:
                    __lowerCamelCase   =						image_embeds.repeat_interleave(UpperCamelCase_    ,       dim=0   )
                    __lowerCamelCase   =						negative_image_embeds.repeat_interleave(UpperCamelCase_    ,       dim=0   )
                    __lowerCamelCase   =						hint.repeat_interleave(UpperCamelCase_    ,       dim=0   )

             __lowerCamelCase   =						torch.cat([negative_image_embeds, image_embeds]    ,       dim=0   ).to(dtype=self.unet.dtype    ,       device=UpperCamelCase_   )
             __lowerCamelCase   =						torch.cat([hint, hint]    ,       dim=0   ).to(dtype=self.unet.dtype    ,       device=UpperCamelCase_   )
             self.scheduler.set_timesteps(UpperCamelCase_    ,       device=UpperCamelCase_   )
             __lowerCamelCase   =						self.scheduler.timesteps

             __lowerCamelCase   =						self.movq.config.latent_channels

             __lowerCamelCase,   __lowerCamelCase   =						downscale_height_and_width(UpperCamelCase_    ,       UpperCamelCase_    ,       self.movq_scale_factor   )

             # create initial latent
             __lowerCamelCase   =						self.prepare_latents(
                 (batch_size, num_channels_latents, height, width)    ,       image_embeds.dtype    ,       UpperCamelCase_    ,       UpperCamelCase_    ,       UpperCamelCase_    ,       self.scheduler    ,       )

             for i, t in enumerate(self.progress_bar(UpperCamelCase_   )   ):
                    # expand the latents if we are doing classifier free guidance
                    __lowerCamelCase   =						torch.cat([latents] * 2   ) if do_classifier_free_guidance else latents

                    __lowerCamelCase   =						{"""image_embeds""": image_embeds, """hint""": hint}
                    __lowerCamelCase   =						self.unet(
                        sample=UpperCamelCase_    ,       timestep=UpperCamelCase_    ,       encoder_hidden_states=UpperCamelCase_    ,       added_cond_kwargs=UpperCamelCase_    ,       return_dict=UpperCamelCase_    ,       )[0]

                    if do_classifier_free_guidance:
                           __lowerCamelCase,   __lowerCamelCase   =						noise_pred.split(latents.shape[1]    ,       dim=1   )
                           __lowerCamelCase,   __lowerCamelCase   =						noise_pred.chunk(2   )
                           __lowerCamelCase,   __lowerCamelCase   =						variance_pred.chunk(2   )
                           __lowerCamelCase   =						noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
                           __lowerCamelCase   =						torch.cat([noise_pred, variance_pred_text]    ,       dim=1   )

                    if not (
                        hasattr(self.scheduler.config    ,       """variance_type"""   )
                        and self.scheduler.config.variance_type in ["learned", "learned_range"]
                    ):
                           __lowerCamelCase,   __lowerCamelCase   =						noise_pred.split(latents.shape[1]    ,       dim=1   )

                    # compute the previous noisy sample x_t -> x_t-1
                    __lowerCamelCase   =						self.scheduler.step(
                        UpperCamelCase_    ,       UpperCamelCase_    ,       UpperCamelCase_    ,       generator=UpperCamelCase_    ,       )[0]
             # post-processing
             __lowerCamelCase   =						self.movq.decode(UpperCamelCase_    ,       force_not_quantize=UpperCamelCase_   )["""sample"""]

             if output_type not in ["pt", "np", "pil"]:
                    raise ValueError(F'Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}'   )

             if output_type in ["np", "pil"]:
                    __lowerCamelCase   =						image * 0.5 + 0.5
                    __lowerCamelCase   =						image.clamp(0    ,       1   )
                    __lowerCamelCase   =						image.cpu().permute(0    ,       2    ,       3    ,       1   ).float().numpy()

             if output_type == "pil":
                    __lowerCamelCase   =						self.numpy_to_pil(UpperCamelCase_   )

             if not return_dict:
                    return (image,)

             return ImagePipelineOutput(images=UpperCamelCase_   )

