




"""simple docstring"""

import unittest

from transformers import MPNetConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
       import torch

       from transformers import (
           MPNetForMaskedLM,
           MPNetForMultipleChoice,
           MPNetForQuestionAnswering,
           MPNetForSequenceClassification,
           MPNetForTokenClassification,
           MPNetModel,
       )





class     lowercase_	:



 '''simple docstring'''

 def __init__(			self	:	List[Any]   ,							_UpperCAmelCase	:	Optional[int]   ,							_UpperCAmelCase	:	List[str]=13   ,							_UpperCAmelCase	:	Tuple=7   ,							_UpperCAmelCase	:	str=True   ,							_UpperCAmelCase	:	List[str]=True   ,							_UpperCAmelCase	:	Union[str, Any]=False   ,							_UpperCAmelCase	:	int=True   ,							_UpperCAmelCase	:	Optional[Any]=99   ,							_UpperCAmelCase	:	Any=64   ,							_UpperCAmelCase	:	Dict=5   ,							_UpperCAmelCase	:	List[str]=4   ,							_UpperCAmelCase	:	Union[str, Any]=64   ,							_UpperCAmelCase	:	Union[str, Any]="gelu"   ,							_UpperCAmelCase	:	Any=0.1   ,							_UpperCAmelCase	:	Optional[Any]=0.1   ,							_UpperCAmelCase	:	Any=512   ,							_UpperCAmelCase	:	Tuple=16   ,							_UpperCAmelCase	:	Any=2   ,							_UpperCAmelCase	:	Optional[Any]=0.02   ,							_UpperCAmelCase	:	str=3   ,							_UpperCAmelCase	:	str=4   ,							_UpperCAmelCase	:	str=None   ,							):
       _A        =       parent
       _A        =       batch_size
       _A        =       seq_length
       _A        =       is_training
       _A        =       use_input_mask
       _A        =       use_token_type_ids
       _A        =       use_labels
       _A        =       vocab_size
       _A        =       hidden_size
       _A        =       num_hidden_layers
       _A        =       num_attention_heads
       _A        =       intermediate_size
       _A        =       hidden_act
       _A        =       hidden_dropout_prob
       _A        =       attention_probs_dropout_prob
       _A        =       max_position_embeddings
       _A        =       type_vocab_size
       _A        =       type_sequence_label_size
       _A        =       initializer_range
       _A        =       num_labels
       _A        =       num_choices
       _A        =       scope

 def       lowerCAmelCase_							(			self	:	Dict		):
       return MPNetConfig.from_pretrained('microsoft/mpnet-base'		)

 def       lowerCAmelCase_							(			self	:	Dict		):
       _A        =       ids_tensor([self.batch_size, self.seq_length]   ,							self.vocab_size		)

       _A        =       None
       if self.use_input_mask:
             _A        =       random_attention_mask([self.batch_size, self.seq_length]		)

       _A        =       None
       _A        =       None
       _A        =       None
       if self.use_labels:
             _A        =       ids_tensor([self.batch_size]   ,							self.type_sequence_label_size		)
             _A        =       ids_tensor([self.batch_size, self.seq_length]   ,							self.num_labels		)
             _A        =       ids_tensor([self.batch_size]   ,							self.num_choices		)

       _A        =       self.get_config()
       return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels

 def       lowerCAmelCase_							(			self	:	Dict		):
       return MPNetConfig(
           vocab_size=self.vocab_size   ,							hidden_size=self.hidden_size   ,							num_hidden_layers=self.num_hidden_layers   ,							num_attention_heads=self.num_attention_heads   ,							intermediate_size=self.intermediate_size   ,							hidden_act=self.hidden_act   ,							hidden_dropout_prob=self.hidden_dropout_prob   ,							attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,							max_position_embeddings=self.max_position_embeddings   ,							initializer_range=self.initializer_range   ,							)

 def       lowerCAmelCase_							(			self	:	Optional[Any]   ,							_UpperCAmelCase	:	int   ,							_UpperCAmelCase	:	Optional[Any]   ,							_UpperCAmelCase	:	Optional[int]   ,							_UpperCAmelCase	:	str   ,							_UpperCAmelCase	:	Tuple   ,							_UpperCAmelCase	:	Optional[Any]		):
       _A        =       MPNetModel(config=_UpperCAmelCase		)
       model.to(_UpperCAmelCase		)
       model.eval()
       _A        =       model(_UpperCAmelCase   ,							_UpperCAmelCase		)
       _A        =       model(_UpperCAmelCase		)
       self.parent.assertEqual(result.last_hidden_state.shape   ,							(self.batch_size, self.seq_length, self.hidden_size)		)
       self.parent.assertEqual(result.pooler_output.shape   ,							(self.batch_size, self.hidden_size)		)

 def       lowerCAmelCase_							(			self	:	Any   ,							_UpperCAmelCase	:	Dict   ,							_UpperCAmelCase	:	List[str]   ,							_UpperCAmelCase	:	Any   ,							_UpperCAmelCase	:	int   ,							_UpperCAmelCase	:	Dict   ,							_UpperCAmelCase	:	int		):
       _A        =       MPNetForQuestionAnswering(config=_UpperCAmelCase		)
       model.to(_UpperCAmelCase		)
       model.eval()
       _A        =       model(
           _UpperCAmelCase   ,							attention_mask=_UpperCAmelCase   ,							start_positions=_UpperCAmelCase   ,							end_positions=_UpperCAmelCase   ,							)
       self.parent.assertEqual(result.start_logits.shape   ,							(self.batch_size, self.seq_length)		)
       self.parent.assertEqual(result.end_logits.shape   ,							(self.batch_size, self.seq_length)		)

 def       lowerCAmelCase_							(			self	:	Tuple   ,							_UpperCAmelCase	:	List[str]   ,							_UpperCAmelCase	:	str   ,							_UpperCAmelCase	:	str   ,							_UpperCAmelCase	:	Union[str, Any]   ,							_UpperCAmelCase	:	List[Any]   ,							_UpperCAmelCase	:	Dict		):
       _A        =       self.num_labels
       _A        =       MPNetForSequenceClassification(_UpperCAmelCase		)
       model.to(_UpperCAmelCase		)
       model.eval()
       _A        =       model(_UpperCAmelCase   ,							attention_mask=_UpperCAmelCase   ,							labels=_UpperCAmelCase		)
       self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.num_labels)		)

 def       lowerCAmelCase_							(			self	:	List[str]   ,							_UpperCAmelCase	:	str   ,							_UpperCAmelCase	:	Union[str, Any]   ,							_UpperCAmelCase	:	int   ,							_UpperCAmelCase	:	Optional[int]   ,							_UpperCAmelCase	:	int   ,							_UpperCAmelCase	:	int		):
       _A        =       self.num_choices
       _A        =       MPNetForMultipleChoice(config=_UpperCAmelCase		)
       model.to(_UpperCAmelCase		)
       model.eval()
       _A        =       input_ids.unsqueeze(1		).expand(-1   ,							self.num_choices   ,							-1		).contiguous()
       _A        =       input_mask.unsqueeze(1		).expand(-1   ,							self.num_choices   ,							-1		).contiguous()
       _A        =       model(
           _UpperCAmelCase   ,							attention_mask=_UpperCAmelCase   ,							labels=_UpperCAmelCase   ,							)
       self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.num_choices)		)

 def       lowerCAmelCase_							(			self	:	str   ,							_UpperCAmelCase	:	Tuple   ,							_UpperCAmelCase	:	List[str]   ,							_UpperCAmelCase	:	Optional[Any]   ,							_UpperCAmelCase	:	List[str]   ,							_UpperCAmelCase	:	List[str]   ,							_UpperCAmelCase	:	List[str]		):
       _A        =       self.num_labels
       _A        =       MPNetForTokenClassification(config=_UpperCAmelCase		)
       model.to(_UpperCAmelCase		)
       model.eval()
       _A        =       model(_UpperCAmelCase   ,							attention_mask=_UpperCAmelCase   ,							labels=_UpperCAmelCase		)
       self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.seq_length, self.num_labels)		)



 def       lowerCAmelCase_							(			self	:	Tuple		):
       _A        =       self.prepare_config_and_inputs()
       ((_A)       ,   (_A)       ,   (_A)       ,   (_A)       ,   (_A)       ,   (_A))        =       config_and_inputs
       _A        =       {'input_ids': input_ids, 'attention_mask': input_mask}
       return config, inputs_dict





@require_torch
class     lowercase_	(					__lowerCAmelCase      ,     __lowerCAmelCase      ,     unittest.TestCase							):



 '''simple docstring'''
 UpperCAmelCase   :					Tuple				  =     (
     (
         MPNetForMaskedLM,
         MPNetForMultipleChoice,
         MPNetForQuestionAnswering,
         MPNetForSequenceClassification,
         MPNetForTokenClassification,
         MPNetModel,
     )
     if is_torch_available()
     else ()
 )
 UpperCAmelCase   :					Union[str, Any]				  =     (
     {
         '''feature-extraction''': MPNetModel,
         '''fill-mask''': MPNetForMaskedLM,
         '''question-answering''': MPNetForQuestionAnswering,
         '''text-classification''': MPNetForSequenceClassification,
         '''token-classification''': MPNetForTokenClassification,
         '''zero-shot''': MPNetForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 UpperCAmelCase   :					Dict				  =     False
 UpperCAmelCase   :					List[str]				  =     True

 def       lowerCAmelCase_							(			self	:	List[Any]		):
       _A        =       MPNetModelTester(self		)
       _A        =       ConfigTester(self   ,							config_class=_UpperCAmelCase   ,							hidden_size=37		)

 def       lowerCAmelCase_							(			self	:	int		):
       self.config_tester.run_common_tests()

 def       lowerCAmelCase_							(			self	:	Union[str, Any]		):
       _A        =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_model(*_UpperCAmelCase		)

 def       lowerCAmelCase_							(			self	:	Optional[int]		):
       _A        =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_sequence_classification(*_UpperCAmelCase		)

 def       lowerCAmelCase_							(			self	:	Optional[Any]		):
       _A        =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_multiple_choice(*_UpperCAmelCase		)

 def       lowerCAmelCase_							(			self	:	List[Any]		):
       _A        =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_token_classification(*_UpperCAmelCase		)



 def       lowerCAmelCase_							(			self	:	List[str]		):
       _A        =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mpnet_for_question_answering(*_UpperCAmelCase		)





@require_torch
class     lowercase_	(					unittest.TestCase							):



 '''simple docstring'''

 @slow
 def       lowerCAmelCase_							(			self	:	int		):
       _A        =       MPNetModel.from_pretrained('microsoft/mpnet-base'		)
       _A        =       torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]		)
       _A        =       model(_UpperCAmelCase		)[0]
       _A        =       torch.Size((1, 11, 768)		)
       self.assertEqual(output.shape   ,							_UpperCAmelCase		)
       _A        =       torch.tensor(
           [[[-0.0550, 0.1943, -0.0740], [-0.0562, 0.2211, -0.0579], [-0.0437, 0.3337, -0.0641]]]		)
       # compare the actual values for a slice.
       self.assertTrue(torch.allclose(output[:, :3, :3]   ,							_UpperCAmelCase   ,							atol=1E-4		)		)







"""simple docstring"""

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,
)


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

a         =      '''
    Examples:
        ```py
        >>> from diffusers import KandinskyV22Pipeline, KandinskyV22PriorPipeline
        >>> import torch

        >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-prior")
        >>> pipe_prior.to("cuda")
        >>> prompt = "red cat, 4k photo"
        >>> out = pipe_prior(prompt)
        >>> image_emb = out.image_embeds
        >>> zero_image_emb = out.negative_image_embeds
        >>> pipe = KandinskyV22Pipeline.from_pretrained("kandinsky-community/kandinsky-2-2-decoder")
        >>> pipe.to("cuda")
        >>> image = pipe(
        ...     image_embeds=image_emb,
        ...     negative_image_embeds=zero_image_emb,
        ...     height=768,
        ...     width=768,
        ...     num_inference_steps=50,
        ... ).images
        >>> image[0].save("cat.png")
        ```
'''






def 						_snake_case					( _snake_case							: Dict    ,  _snake_case							: List[Any]    ,  _snake_case							: List[Any]=8							)   -> Optional[int]:






      '''simple docstring'''


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







class     lowercase_	(					__lowerCAmelCase							):



 '''simple docstring'''

 def __init__(			self	:	Dict   ,							_UpperCAmelCase	:	UNetaDConditionModel   ,							_UpperCAmelCase	:	DDPMScheduler   ,							_UpperCAmelCase	:	VQModel   ,							):
       super().__init__()

       self.register_modules(
           unet=_UpperCAmelCase   ,							scheduler=_UpperCAmelCase   ,							movq=_UpperCAmelCase   ,							)
       _A        =       2 ** (len(self.movq.config.block_out_channels		) - 1)

 def       lowerCAmelCase_							(			self	:	Any   ,							_UpperCAmelCase	:	str   ,							_UpperCAmelCase	:	Tuple   ,							_UpperCAmelCase	:	Optional[Any]   ,							_UpperCAmelCase	:	Dict   ,							_UpperCAmelCase	:	List[str]   ,							_UpperCAmelCase	:	int		):
       if latents is None:
             _A        =       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}'''		)
             _A        =       latents.to(_UpperCAmelCase		)

       _A        =       latents * scheduler.init_noise_sigma
       return latents

 def       lowerCAmelCase_							(			self	:	Tuple   ,							_UpperCAmelCase	:	Tuple=0		):
       if is_accelerate_available():
             from accelerate import cpu_offload
       else:
             raise ImportError('Please install accelerate via `pip install accelerate`'		)

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

       _A        =       [
           self.unet,
           self.movq,
       ]
       for cpu_offloaded_model in models:
             if cpu_offloaded_model is not None:
                   cpu_offload(_UpperCAmelCase   ,							_UpperCAmelCase		)

 def       lowerCAmelCase_							(			self	:	Union[str, Any]   ,							_UpperCAmelCase	:	Union[str, Any]=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.'		)

       _A        =       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)

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

       # We'll offload the last model manually.
       _A        =       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	:	Union[str, Any]   ,							_UpperCAmelCase	:	Union[torch.FloatTensor, List[torch.FloatTensor]]   ,							_UpperCAmelCase	:	Union[torch.FloatTensor, List[torch.FloatTensor]]   ,							_UpperCAmelCase	:	int = 512   ,							_UpperCAmelCase	:	int = 512   ,							_UpperCAmelCase	:	int = 100   ,							_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   ,							):
       _A        =       self._execution_device

       _A        =       guidance_scale > 1.0

       if isinstance(_UpperCAmelCase   ,							_UpperCAmelCase		):
             _A        =       torch.cat(_UpperCAmelCase   ,							dim=0		)
       _A        =       image_embeds.shape[0] * num_images_per_prompt
       if isinstance(_UpperCAmelCase   ,							_UpperCAmelCase		):
             _A        =       torch.cat(_UpperCAmelCase   ,							dim=0		)

       if do_classifier_free_guidance:
             _A        =       image_embeds.repeat_interleave(_UpperCAmelCase   ,							dim=0		)
             _A        =       negative_image_embeds.repeat_interleave(_UpperCAmelCase   ,							dim=0		)

       _A        =       torch.cat([negative_image_embeds, image_embeds]   ,							dim=0		).to(dtype=self.unet.dtype   ,							device=_UpperCAmelCase		)

       self.scheduler.set_timesteps(_UpperCAmelCase   ,							device=_UpperCAmelCase		)
       _A        =       self.scheduler.timesteps

       _A        =       self.unet.config.in_channels

       _A       ,   _A        =       downscale_height_and_width(_UpperCAmelCase   ,							_UpperCAmelCase   ,							self.movq_scale_factor		)

       # create initial latent
       _A        =       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
             _A        =       torch.cat([latents] * 2		) if do_classifier_free_guidance else latents

             _A        =       {'image_embeds': image_embeds}
             _A        =       self.unet(
                 sample=_UpperCAmelCase   ,							timestep=_UpperCAmelCase   ,							encoder_hidden_states=_UpperCAmelCase   ,							added_cond_kwargs=_UpperCAmelCase   ,							return_dict=_UpperCAmelCase   ,							)[0]

             if do_classifier_free_guidance:
                   _A       ,   _A        =       noise_pred.split(latents.shape[1]   ,							dim=1		)
                   _A       ,   _A        =       noise_pred.chunk(2		)
                   _A       ,   _A        =       variance_pred.chunk(2		)
                   _A        =       noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
                   _A        =       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"]
             ):
                   _A       ,   _A        =       noise_pred.split(latents.shape[1]   ,							dim=1		)

             # compute the previous noisy sample x_t -> x_t-1
             _A        =       self.scheduler.step(
                 _UpperCAmelCase   ,							_UpperCAmelCase   ,							_UpperCAmelCase   ,							generator=_UpperCAmelCase   ,							)[0]
       # post-processing
       _A        =       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"]:
             _A        =       image * 0.5 + 0.5
             _A        =       image.clamp(0   ,							1		)
             _A        =       image.cpu().permute(0   ,							2   ,							3   ,							1		).float().numpy()

       if output_type == "pil":
             _A        =       self.numpy_to_pil(_UpperCAmelCase		)

       if not return_dict:
             return (image,)

       return ImagePipelineOutput(images=_UpperCAmelCase		)

