

import unittest

from transformers.testing_utils import is_torch_available, require_torch, tooslow

from ...generation.test_utils import torch_device
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers import (
          CpmAntConfig,
          CpmAntForCausalLM,
          CpmAntModel,
          CpmAntTokenizer,
      )


@require_torch
class      lowerCamelCase_ :




 def __init__(			self  ,  _UpperCAmelCase  ,  _UpperCAmelCase=2  ,  _UpperCAmelCase=8  ,  _UpperCAmelCase=True  ,  _UpperCAmelCase=False  ,  _UpperCAmelCase=False  ,  _UpperCAmelCase=False  ,  _UpperCAmelCase=False  ,  _UpperCAmelCase=99  ,  _UpperCAmelCase=32  ,  _UpperCAmelCase=3  ,  _UpperCAmelCase=4  ,  _UpperCAmelCase=37  ,  _UpperCAmelCase=32  ,  _UpperCAmelCase=128  ,  _UpperCAmelCase=8  ,  _UpperCAmelCase=8  ,  _UpperCAmelCase=8  ,  _UpperCAmelCase=1.0  ,  _UpperCAmelCase=True  ,  ):
   __UpperCAmelCase	:  Optional[Any]		=     parent
   __UpperCAmelCase	:  Dict		=     batch_size
   __UpperCAmelCase	:  Dict		=     seq_length
   __UpperCAmelCase	:  List[Any]		=     is_training
   __UpperCAmelCase	:  Any		=     use_token_type_ids
   __UpperCAmelCase	:  Any		=     use_input_mask
   __UpperCAmelCase	:  Any		=     use_labels
   __UpperCAmelCase	:  List[str]		=     use_mc_token_ids
   __UpperCAmelCase	:  Union[str, Any]		=     vocab_size
   __UpperCAmelCase	:  int		=     hidden_size
   __UpperCAmelCase	:  List[Any]		=     num_hidden_layers
   __UpperCAmelCase	:  Optional[Any]		=     num_attention_heads
   __UpperCAmelCase	:  List[Any]		=     intermediate_size
   __UpperCAmelCase	:  Any		=     num_buckets
   __UpperCAmelCase	:  str		=     max_distance
   __UpperCAmelCase	:  List[str]		=     prompt_length
   __UpperCAmelCase	:  int		=     prompt_types
   __UpperCAmelCase	:  Union[str, Any]		=     segment_types
   __UpperCAmelCase	:  Any		=     init_std
   __UpperCAmelCase	:  Optional[int]		=     return_dict




 def     a       (			self							):
   __UpperCAmelCase	:  Optional[Any]		=     {}
   __UpperCAmelCase	:  Any		=     ids_tensor([self.batch_size, self.seq_length]  ,  self.vocab_size							).type(torch.intaa							)
   __UpperCAmelCase	:  Union[str, Any]		=     False

   __UpperCAmelCase	:  List[str]		=     self.get_config()

   return (config, input_ids)




 def     a       (			self							):
   return CpmAntConfig(
       vocab_size=self.vocab_size  ,  hidden_size=self.hidden_size  ,  num_hidden_layers=self.num_hidden_layers  ,  num_attention_heads=self.num_attention_heads  ,  dim_ff=self.intermediate_size  ,  position_bias_num_buckets=self.num_buckets  ,  position_bias_max_distance=self.max_distance  ,  prompt_types=self.prompt_types  ,  prompt_length=self.prompt_length  ,  segment_types=self.segment_types  ,  use_cache=_UpperCAmelCase  ,  init_std=self.init_std  ,  return_dict=self.return_dict  ,  )




 def     a       (			self  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  *_UpperCAmelCase							):
   __UpperCAmelCase	:  List[Any]		=     CpmAntModel(config=_UpperCAmelCase							)
   model.to(_UpperCAmelCase							)
   model.eval()

   __UpperCAmelCase	:  Dict		=     model(**_UpperCAmelCase							).last_hidden_state

   self.parent.assertEqual(hidden_states.shape  ,  (self.batch_size, self.seq_length, config.hidden_size)							)




 def     a       (			self  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  *_UpperCAmelCase							):
   __UpperCAmelCase	:  List[str]		=     CpmAntForCausalLM(_UpperCAmelCase							)
   model.to(_UpperCAmelCase							)
   __UpperCAmelCase	:  Union[str, Any]		=     input_ids['input_ids'].to(_UpperCAmelCase							)
   model.eval()

   __UpperCAmelCase	:  Optional[Any]		=     model(**_UpperCAmelCase							)
   self.parent.assertEqual(
       model_output.logits.shape  ,  (self.batch_size, self.seq_length, config.vocab_size + config.prompt_types * config.prompt_length)  ,  )




 def     a       (			self							):
   __UpperCAmelCase,			__UpperCAmelCase	:  Any		=     self.prepare_config_and_inputs()
   return config, inputs_dict



@require_torch
class      lowerCamelCase_ (			_lowerCAmelCase	,				_lowerCAmelCase	,				unittest.TestCase):
 _lowerCAmelCase									=	(CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
 _lowerCAmelCase									=	(
     {'''feature-extraction''': CpmAntModel, '''text-generation''': CpmAntForCausalLM} if is_torch_available() else {}
 )

 _lowerCAmelCase									=	False
 _lowerCAmelCase									=	False
 _lowerCAmelCase									=	False
 _lowerCAmelCase									=	False
 _lowerCAmelCase									=	False




 def     a       (			self							):
   __UpperCAmelCase	:  Any		=     CpmAntModelTester(self							)
   __UpperCAmelCase	:  int		=     ConfigTester(self  ,  config_class=_UpperCAmelCase							)




 def     a       (			self							):
   self.config_tester.create_and_test_config_common_properties()
   self.config_tester.create_and_test_config_to_json_string()
   self.config_tester.create_and_test_config_to_json_file()
   self.config_tester.create_and_test_config_from_and_save_pretrained()
   self.config_tester.check_config_can_be_init_without_params()
   self.config_tester.check_config_arguments_init()




 def     a       (			self							):
   unittest.skip('CPMAnt doesn\'t support input_embeds.'							)(self.test_inputs_embeds							)




 def     a       (			self							):
   unittest.skip(
       'CPMAnt doesn\'t support retain grad in hidden_states or attentions, because prompt management will peel off the output.hidden_states from graph.\
                 So is attentions. We strongly recommand you use loss to tune model.'							)(self.test_retain_grad_hidden_states_attentions							)




 def     a       (			self							):
   __UpperCAmelCase,			__UpperCAmelCase	:  str		=     self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_cpmant_model(_UpperCAmelCase  ,  _UpperCAmelCase							)




 def     a       (			self							):
   __UpperCAmelCase,			__UpperCAmelCase	:  Tuple		=     self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_lm_head_model(_UpperCAmelCase  ,  _UpperCAmelCase							)



@require_torch
class      lowerCamelCase_ (			unittest.TestCase):




 @tooslow
 def     a       (			self							):
   __UpperCAmelCase	:  Union[str, Any]		=     '今天天气真好！'
   __UpperCAmelCase	:  Dict		=     'openbmb/cpm-ant-10b'
   __UpperCAmelCase	:  List[Any]		=     CpmAntModel.from_pretrained(_UpperCAmelCase							)
   __UpperCAmelCase	:  Optional[int]		=     CpmAntTokenizer.from_pretrained(_UpperCAmelCase							)
   __UpperCAmelCase	:  Any		=     tokenizer(_UpperCAmelCase  ,  return_tensors='pt'							)
   __UpperCAmelCase	:  Optional[int]		=     model(**_UpperCAmelCase							).last_hidden_state

   __UpperCAmelCase	:  Tuple		=     torch.tensor(
       [[[6.1_7_0_8, 5.9_2_4_4, 1.0_8_3_5], [6.5_2_0_7, 6.2_8_9_3, -1_1.3_3_2_4], [-1.0_1_0_7, -0.0_5_7_6, -5.9_5_7_7]]]  ,  )
   self.assertTrue(torch.allclose(hidden_states[:, :3, :3]  ,  _UpperCAmelCase  ,  atol=1E-2							)							)



@require_torch
class      lowerCamelCase_ (			unittest.TestCase):




 @tooslow
 def     a       (			self							):
   __UpperCAmelCase	:  Any		=     '今天天气真好！'
   __UpperCAmelCase	:  Any		=     'openbmb/cpm-ant-10b'
   __UpperCAmelCase	:  List[Any]		=     CpmAntForCausalLM.from_pretrained(_UpperCAmelCase							)
   __UpperCAmelCase	:  Any		=     CpmAntTokenizer.from_pretrained(_UpperCAmelCase							)
   __UpperCAmelCase	:  Optional[Any]		=     tokenizer(_UpperCAmelCase  ,  return_tensors='pt'							)
   __UpperCAmelCase	:  List[str]		=     model(**_UpperCAmelCase							).logits

   __UpperCAmelCase	:  int		=     torch.tensor(
       [[[-6.4_2_6_7, -6.4_0_8_3, -6.3_9_5_8], [-5.8_8_0_2, -5.9_4_4_7, -5.7_8_1_1], [-5.3_8_9_6, -5.4_8_2_0, -5.4_2_9_5]]]  ,  )
   self.assertTrue(torch.allclose(hidden_states[:, :3, :3]  ,  _UpperCAmelCase  ,  atol=1E-2							)							)




 @tooslow
 def     a       (			self							):
   __UpperCAmelCase	:  Any		=     'openbmb/cpm-ant-10b'
   __UpperCAmelCase	:  Any		=     CpmAntForCausalLM.from_pretrained(_UpperCAmelCase							)
   __UpperCAmelCase	:  str		=     CpmAntTokenizer.from_pretrained(_UpperCAmelCase							)
   __UpperCAmelCase	:  Dict		=     '今天天气不错，'
   __UpperCAmelCase	:  List[Any]		=     '今天天气不错，阳光明媚，我和妈妈一起去超市买东西。\n在超市里，我看到了一个很好玩的玩具，它的名字叫“机器人”。它有一个圆圆的脑袋，两只圆圆的眼睛，还有一个圆圆的'
   __UpperCAmelCase	:  Optional[Any]		=     tokenizer(_UpperCAmelCase  ,  return_tensors='pt'							)
   __UpperCAmelCase	:  Any		=     model.generate(**_UpperCAmelCase							)
   __UpperCAmelCase	:  Optional[Any]		=     tokenizer.batch_decode(_UpperCAmelCase							)
   self.assertEqual(_UpperCAmelCase  ,  _UpperCAmelCase							)




 @tooslow
 def     a       (			self							):
   __UpperCAmelCase	:  List[Any]		=     'openbmb/cpm-ant-10b'
   __UpperCAmelCase	:  Any		=     CpmAntForCausalLM.from_pretrained(_UpperCAmelCase							)
   __UpperCAmelCase	:  Any		=     CpmAntTokenizer.from_pretrained(_UpperCAmelCase							)
   __UpperCAmelCase	:  List[str]		=     ['今天天气不错，', '新年快乐，万事如意！']
   __UpperCAmelCase	:  str		=     [
       '今天天气不错，阳光明媚，我和妈妈一起去超市买东西。\n在超市里，我看到了一个很好玩的玩具，它的名字叫“机器人”。它有一个圆圆的脑袋，两只圆圆的眼睛，还有一个圆圆的',
       '新年快乐，万事如意！在这辞旧迎新的美好时刻，我谨代表《农村新技术》杂志社全体同仁，向一直以来关心、支持《农村新技术》杂志发展的各级领导、各界朋友和广大读者致以最诚挚的',
   ]
   __UpperCAmelCase	:  List[Any]		=     tokenizer(_UpperCAmelCase  ,  return_tensors='pt'  ,  padding=_UpperCAmelCase							)
   __UpperCAmelCase	:  List[str]		=     model.generate(**_UpperCAmelCase							)
   __UpperCAmelCase	:  Optional[Any]		=     tokenizer.batch_decode(_UpperCAmelCase							)
   self.assertEqual(_UpperCAmelCase  ,  _UpperCAmelCase							)








import types
from typing import List, Optional, Tuple, Union

import torch
from transformers import CLIPTextModelWithProjection, CLIPTokenizer
from transformers.models.clip.modeling_clip import CLIPTextModelOutput

from diffusers.models import PriorTransformer
from diffusers.pipelines import DiffusionPipeline, StableDiffusionImageVariationPipeline
from diffusers.schedulers import UnCLIPScheduler
from diffusers.utils import logging, randn_tensor


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





def  _snake_case		(self					:     Optional[Any]    ,       snake_case_					:     Optional[Any]    ,       snake_case_					:     Dict    ,       snake_case_					:     Optional[int]    ,       snake_case_					:     Optional[Any]  )							->		Union[str, Any]:
  __UpperCAmelCase	:  Any		=     image.to(device=snake_case_  )
  __UpperCAmelCase	:  Tuple		=     image  # take image as image_embeddings
  __UpperCAmelCase	:  str		=     image_embeddings.unsqueeze(1  )

  # duplicate image embeddings for each generation per prompt, using mps friendly method
  __UpperCAmelCase,			__UpperCAmelCase,			__UpperCAmelCase	:  int		=     image_embeddings.shape
  __UpperCAmelCase	:  int		=     image_embeddings.repeat(1    ,       snake_case_    ,       1  )
  __UpperCAmelCase	:  Optional[Any]		=     image_embeddings.view(bs_embed * num_images_per_prompt    ,       snake_case_    ,       -1  )

  if do_classifier_free_guidance:
    __UpperCAmelCase	:  List[Any]		=     torch.zeros_like(snake_case_  )

    # For classifier free guidance, we need to do two forward passes.
    # Here we concatenate the unconditional and text embeddings into a single batch
    # to avoid doing two forward passes
    __UpperCAmelCase	:  Union[str, Any]		=     torch.cat([uncond_embeddings, image_embeddings]  )

  return image_embeddings







class      lowerCamelCase_ (			_lowerCAmelCase):




 def __init__(			self  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase = None  ,  ):
   super().__init__()

   __UpperCAmelCase	:  List[Any]		=     {'image_encoder': None} if decoder_pipe_kwargs is None else decoder_pipe_kwargs

   __UpperCAmelCase	:  Optional[Any]		=     decoder_pipe_kwargs.get('torch_dtype'  ,  _UpperCAmelCase							) or prior.dtype

   __UpperCAmelCase	:  str		=     StableDiffusionImageVariationPipeline.from_pretrained(
       'lambdalabs/sd-image-variations-diffusers'  ,  **_UpperCAmelCase							)

   # replace `_encode_image` method
   __UpperCAmelCase	:  Dict		=     types.MethodType(_encode_image  ,  self.decoder_pipe							)

   self.register_modules(
       prior=_UpperCAmelCase  ,  tokenizer=_UpperCAmelCase  ,  text_encoder=_UpperCAmelCase  ,  prior_scheduler=_UpperCAmelCase  ,  )




 def     a       (			self  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = None  ,  ):
   if text_model_output is None:
     __UpperCAmelCase	:  Any		=     len(_UpperCAmelCase							) if isinstance(_UpperCAmelCase  ,  _UpperCAmelCase							) else 1
     # get prompt text embeddings
     __UpperCAmelCase	:  str		=     self.tokenizer(
         _UpperCAmelCase  ,  padding='max_length'  ,  max_length=self.tokenizer.model_max_length  ,  return_tensors='pt'  ,  )
     __UpperCAmelCase	:  int		=     text_inputs.input_ids
     __UpperCAmelCase	:  str		=     text_inputs.attention_mask.bool().to(_UpperCAmelCase							)

     if text_input_ids.shape[-1] > self.tokenizer.model_max_length:
       __UpperCAmelCase	:  Any		=     self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :]							)
       logger.warning(
           'The following part of your input was truncated because CLIP can only handle sequences up to'
           F" {self.tokenizer.model_max_length} tokens: {removed_text}"							)
       __UpperCAmelCase	:  int		=     text_input_ids[:, : self.tokenizer.model_max_length]

     __UpperCAmelCase	:  Any		=     self.text_encoder(text_input_ids.to(_UpperCAmelCase							)							)

     __UpperCAmelCase	:  str		=     text_encoder_output.text_embeds
     __UpperCAmelCase	:  str		=     text_encoder_output.last_hidden_state

   else:
     __UpperCAmelCase	:  str		=     text_model_output[0].shape[0]
     __UpperCAmelCase,			__UpperCAmelCase	:  Any		=     text_model_output[0], text_model_output[1]
     __UpperCAmelCase	:  Optional[Any]		=     text_attention_mask

   __UpperCAmelCase	:  Optional[Any]		=     text_embeddings.repeat_interleave(_UpperCAmelCase  ,  dim=0							)
   __UpperCAmelCase	:  Tuple		=     text_encoder_hidden_states.repeat_interleave(_UpperCAmelCase  ,  dim=0							)
   __UpperCAmelCase	:  Optional[Any]		=     text_mask.repeat_interleave(_UpperCAmelCase  ,  dim=0							)

   if do_classifier_free_guidance:
     __UpperCAmelCase	:  int		=     [''] * batch_size

     __UpperCAmelCase	:  Optional[int]		=     self.tokenizer(
         _UpperCAmelCase  ,  padding='max_length'  ,  max_length=self.tokenizer.model_max_length  ,  truncation=_UpperCAmelCase  ,  return_tensors='pt'  ,  )
     __UpperCAmelCase	:  List[str]		=     uncond_input.attention_mask.bool().to(_UpperCAmelCase							)
     __UpperCAmelCase	:  int		=     self.text_encoder(uncond_input.input_ids.to(_UpperCAmelCase							)							)

     __UpperCAmelCase	:  Optional[Any]		=     uncond_embeddings_text_encoder_output.text_embeds
     __UpperCAmelCase	:  List[str]		=     uncond_embeddings_text_encoder_output.last_hidden_state

     # duplicate unconditional embeddings for each generation per prompt, using mps friendly method

     __UpperCAmelCase	:  List[str]		=     uncond_embeddings.shape[1]
     __UpperCAmelCase	:  Tuple		=     uncond_embeddings.repeat(1  ,  _UpperCAmelCase							)
     __UpperCAmelCase	:  List[str]		=     uncond_embeddings.view(batch_size * num_images_per_prompt  ,  _UpperCAmelCase							)

     __UpperCAmelCase	:  Tuple		=     uncond_text_encoder_hidden_states.shape[1]
     __UpperCAmelCase	:  Union[str, Any]		=     uncond_text_encoder_hidden_states.repeat(1  ,  _UpperCAmelCase  ,  1							)
     __UpperCAmelCase	:  Any		=     uncond_text_encoder_hidden_states.view(
         batch_size * num_images_per_prompt  ,  _UpperCAmelCase  ,  -1							)
     __UpperCAmelCase	:  int		=     uncond_text_mask.repeat_interleave(_UpperCAmelCase  ,  dim=0							)

     # done duplicates

     # For classifier free guidance, we need to do two forward passes.
     # Here we concatenate the unconditional and text embeddings into a single batch
     # to avoid doing two forward passes
     __UpperCAmelCase	:  List[str]		=     torch.cat([uncond_embeddings, text_embeddings]							)
     __UpperCAmelCase	:  str		=     torch.cat([uncond_text_encoder_hidden_states, text_encoder_hidden_states]							)

     __UpperCAmelCase	:  Any		=     torch.cat([uncond_text_mask, text_mask]							)

   return text_embeddings, text_encoder_hidden_states, text_mask




 @property
 def     a       (			self							):
   if self.device != torch.device('meta'							) or not hasattr(self.prior  ,  '_hf_hook'							):
     return self.device
   for module in self.prior.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




 def     a       (			self  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase							):
   if latents is None:
     __UpperCAmelCase	:  Any		=     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}"							)
     __UpperCAmelCase	:  Optional[int]		=     latents.to(_UpperCAmelCase							)

   __UpperCAmelCase	:  str		=     latents * scheduler.init_noise_sigma
   return latents




 def     a       (			self  ,  _UpperCAmelCase = None							):
   self.decoder_pipe.to(_UpperCAmelCase							)
   super().to(_UpperCAmelCase							)




 @torch.no_grad()
 def __call__(			self  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = 1  ,  _UpperCAmelCase = 25  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = 4.0  ,  _UpperCAmelCase = 8.0  ,  _UpperCAmelCase = 50  ,  _UpperCAmelCase = 1  ,  _UpperCAmelCase = 0.0  ,  _UpperCAmelCase = "pil"  ,  _UpperCAmelCase = True  ,  ):
   if prompt is not None:
     if isinstance(_UpperCAmelCase  ,  _UpperCAmelCase							):
       __UpperCAmelCase	:  List[Any]		=     1
     elif isinstance(_UpperCAmelCase  ,  _UpperCAmelCase							):
       __UpperCAmelCase	:  Tuple		=     len(_UpperCAmelCase							)
     else:
       raise ValueError(F"`prompt` has to be of type `str` or `list` but is {type(_UpperCAmelCase							)}"							)
   else:
     __UpperCAmelCase	:  Tuple		=     text_model_output[0].shape[0]

   __UpperCAmelCase	:  Any		=     self._execution_device

   __UpperCAmelCase	:  Any		=     batch_size * num_images_per_prompt

   __UpperCAmelCase	:  List[str]		=     prior_guidance_scale > 1.0 or decoder_guidance_scale > 1.0

   __UpperCAmelCase,			__UpperCAmelCase,			__UpperCAmelCase	:  Optional[Any]		=     self._encode_prompt(
       _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase							)

   # prior

   self.prior_scheduler.set_timesteps(_UpperCAmelCase  ,  device=_UpperCAmelCase							)
   __UpperCAmelCase	:  Dict		=     self.prior_scheduler.timesteps

   __UpperCAmelCase	:  Tuple		=     self.prior.config.embedding_dim

   __UpperCAmelCase	:  Dict		=     self.prepare_latents(
       (batch_size, embedding_dim)  ,  text_embeddings.dtype  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  self.prior_scheduler  ,  )

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

     __UpperCAmelCase	:  Union[str, Any]		=     self.prior(
         _UpperCAmelCase  ,  timestep=_UpperCAmelCase  ,  proj_embedding=_UpperCAmelCase  ,  encoder_hidden_states=_UpperCAmelCase  ,  attention_mask=_UpperCAmelCase  ,  ).predicted_image_embedding

     if do_classifier_free_guidance:
       __UpperCAmelCase,			__UpperCAmelCase	:  List[str]		=     predicted_image_embedding.chunk(2							)
       __UpperCAmelCase	:  Optional[Any]		=     predicted_image_embedding_uncond + prior_guidance_scale * (
           predicted_image_embedding_text - predicted_image_embedding_uncond
       )

     if i + 1 == prior_timesteps_tensor.shape[0]:
       __UpperCAmelCase	:  int		=     None
     else:
       __UpperCAmelCase	:  Any		=     prior_timesteps_tensor[i + 1]

     __UpperCAmelCase	:  Union[str, Any]		=     self.prior_scheduler.step(
         _UpperCAmelCase  ,  timestep=_UpperCAmelCase  ,  sample=_UpperCAmelCase  ,  generator=_UpperCAmelCase  ,  prev_timestep=_UpperCAmelCase  ,  ).prev_sample

   __UpperCAmelCase	:  Union[str, Any]		=     self.prior.post_process_latents(_UpperCAmelCase							)

   __UpperCAmelCase	:  Any		=     prior_latents

   __UpperCAmelCase	:  List[Any]		=     self.decoder_pipe(
       image=_UpperCAmelCase  ,  height=_UpperCAmelCase  ,  width=_UpperCAmelCase  ,  num_inference_steps=_UpperCAmelCase  ,  guidance_scale=_UpperCAmelCase  ,  generator=_UpperCAmelCase  ,  output_type=_UpperCAmelCase  ,  return_dict=_UpperCAmelCase  ,  num_images_per_prompt=_UpperCAmelCase  ,  eta=_UpperCAmelCase  ,  )
   return output





