


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


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

snake_case							:       Optional[int]										=	'''
    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 							__lowerCamelCase		(					UpperCAmelCase_		:						Optional[int]					,    UpperCAmelCase_		:						str					,    UpperCAmelCase_		:						Dict=8		):




      """simple docstring"""





      a		:Tuple									=			height // scale_factor**2
      if height % scale_factor**2 != 0:
            new_height += 1
      a		:Union[str, Any]									=			width // scale_factor**2
      if width % scale_factor**2 != 0:
            new_width += 1
      return new_height * scale_factor, new_width * scale_factor







class 						_snake_case    ( _snake_case				):




 def __init__(						self   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    ):
       super().__init__()

       self.register_modules(
           unet=_lowerCamelCase   ,    scheduler=_lowerCamelCase   ,    movq=_lowerCamelCase   ,    )
       a		:int									=			2 ** (len(self.movq.config.block_out_channels       ) - 1)




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase       ):
       if latents is None:
             a		:Optional[Any]									=			randn_tensor(_lowerCamelCase   ,    generator=_lowerCamelCase   ,    device=_lowerCamelCase   ,    dtype=_lowerCamelCase       )
       else:
             if latents.shape != shape:
                   raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {shape}'''       )
             a		:Dict									=			latents.to(_lowerCamelCase       )

       a		:Dict									=			latents * scheduler.init_noise_sigma
       return latents




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

       a		:List[str]									=			torch.device(F'''cuda:{gpu_id}'''       )

       a		:Union[str, Any]									=			[
           self.unet,
           self.movq,
       ]
       for cpu_offloaded_model in models:
             if cpu_offloaded_model is not None:
                   cpu_offload(_lowerCamelCase   ,    _lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase=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		:str									=			torch.device(F'''cuda:{gpu_id}'''       )

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

       a		:Union[str, Any]									=			None
       for cpu_offloaded_model in [self.unet, self.movq]:
             a				,							a		:Union[str, Any]									=			cpu_offload_with_hook(_lowerCamelCase   ,    _lowerCamelCase   ,    prev_module_hook=_lowerCamelCase       )

       # We'll offload the last model manually.
       a		:str									=			hook




 @property
 # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       if not hasattr(self.unet   ,    '''_hf_hook'''       ):
             return self.device
       for module in self.unet.modules():
             if (
                 hasattr(_lowerCamelCase   ,    '''_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(_lowerCamelCase       )
 def __call__(						self   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase = 512   ,    _lowerCamelCase = 512   ,    _lowerCamelCase = 100   ,    _lowerCamelCase = 4.0   ,    _lowerCamelCase = 1   ,    _lowerCamelCase = None   ,    _lowerCamelCase = None   ,    _lowerCamelCase = "pil"   ,    _lowerCamelCase = True   ,    ):
       a		:Dict									=			self._execution_device

       a		:Any									=			guidance_scale > 1.0

       if isinstance(_lowerCamelCase   ,    _lowerCamelCase       ):
             a		:Tuple									=			torch.cat(_lowerCamelCase   ,    dim=0       )
       a		:Optional[Any]									=			image_embeds.shape[0] * num_images_per_prompt
       if isinstance(_lowerCamelCase   ,    _lowerCamelCase       ):
             a		:Any									=			torch.cat(_lowerCamelCase   ,    dim=0       )

       if do_classifier_free_guidance:
             a		:Union[str, Any]									=			image_embeds.repeat_interleave(_lowerCamelCase   ,    dim=0       )
             a		:Any									=			negative_image_embeds.repeat_interleave(_lowerCamelCase   ,    dim=0       )

       a		:Optional[Any]									=			torch.cat([negative_image_embeds, image_embeds]   ,    dim=0       ).to(dtype=self.unet.dtype   ,    device=_lowerCamelCase       )

       self.scheduler.set_timesteps(_lowerCamelCase   ,    device=_lowerCamelCase       )
       a		:Union[str, Any]									=			self.scheduler.timesteps

       a		:Optional[int]									=			self.unet.config.in_channels

       a				,							a		:Dict									=			downscale_height_and_width(_lowerCamelCase   ,    _lowerCamelCase   ,    self.movq_scale_factor       )

       # create initial latent
       a		:str									=			self.prepare_latents(
           (batch_size, num_channels_latents, height, width)   ,    image_embeds.dtype   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    self.scheduler   ,    )

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

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

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

             # compute the previous noisy sample x_t -> x_t-1
             a		:Union[str, Any]									=			self.scheduler.step(
                 _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    generator=_lowerCamelCase   ,    )[0]
       # post-processing
       a		:Optional[Any]									=			self.movq.decode(_lowerCamelCase   ,    force_not_quantize=_lowerCamelCase       )['''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		:Optional[int]									=			image * 0.5 + 0.5
             a		:Optional[int]									=			image.clamp(0   ,    1       )
             a		:Union[str, Any]									=			image.cpu().permute(0   ,    2   ,    3   ,    1       ).float().numpy()

       if output_type == "pil":
             a		:Union[str, Any]									=			self.numpy_to_pil(_lowerCamelCase       )

       if not return_dict:
             return (image,)

       return ImagePipelineOutput(images=_lowerCamelCase       )









import unittest

import numpy as np

from transformers import DistilBertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
     import jax.numpy as jnp

     from transformers.models.distilbert.modeling_flax_distilbert import (
         FlaxDistilBertForMaskedLM,
         FlaxDistilBertForMultipleChoice,
         FlaxDistilBertForQuestionAnswering,
         FlaxDistilBertForSequenceClassification,
         FlaxDistilBertForTokenClassification,
         FlaxDistilBertModel,
     )







class 						_snake_case    ( unittest.TestCase				):




 def __init__(						self   ,    _lowerCamelCase   ,    _lowerCamelCase=13   ,    _lowerCamelCase=7   ,    _lowerCamelCase=True   ,    _lowerCamelCase=True   ,    _lowerCamelCase=True   ,    _lowerCamelCase=True   ,    _lowerCamelCase=99   ,    _lowerCamelCase=32   ,    _lowerCamelCase=5   ,    _lowerCamelCase=4   ,    _lowerCamelCase=37   ,    _lowerCamelCase="gelu"   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=512   ,    _lowerCamelCase=16   ,    _lowerCamelCase=2   ,    _lowerCamelCase=0.02   ,    _lowerCamelCase=4   ,    ):
       a		:Optional[Any]									=			parent
       a		:str									=			batch_size
       a		:Tuple									=			seq_length
       a		:List[Any]									=			is_training
       a		:Optional[int]									=			use_attention_mask
       a		:List[str]									=			use_token_type_ids
       a		:str									=			use_labels
       a		:Optional[Any]									=			vocab_size
       a		:Optional[int]									=			hidden_size
       a		:Tuple									=			num_hidden_layers
       a		:Union[str, Any]									=			num_attention_heads
       a		:int									=			intermediate_size
       a		:int									=			hidden_act
       a		:int									=			hidden_dropout_prob
       a		:Union[str, Any]									=			attention_probs_dropout_prob
       a		:str									=			max_position_embeddings
       a		:Dict									=			type_vocab_size
       a		:str									=			type_sequence_label_size
       a		:List[str]									=			initializer_range
       a		:Optional[Any]									=			num_choices




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Tuple									=			ids_tensor([self.batch_size, self.seq_length]   ,    self.vocab_size       )

       a		:Any									=			None
       if self.use_attention_mask:
             a		:Any									=			random_attention_mask([self.batch_size, self.seq_length]       )

       a		:Any									=			DistilBertConfig(
           vocab_size=self.vocab_size   ,    dim=self.hidden_size   ,    n_layers=self.num_hidden_layers   ,    n_heads=self.num_attention_heads   ,    hidden_dim=self.intermediate_size   ,    hidden_act=self.hidden_act   ,    dropout=self.hidden_dropout_prob   ,    attention_dropout=self.attention_probs_dropout_prob   ,    max_position_embeddings=self.max_position_embeddings   ,    initializer_range=self.initializer_range   ,    tie_weights_=_lowerCamelCase   ,    )

       return config, input_ids, attention_mask




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:List[str]									=			self.prepare_config_and_inputs()
       a				,							a				,							a		:str									=			config_and_inputs
       a		:List[Any]									=			{'''input_ids''': input_ids, '''attention_mask''': attention_mask}
       return config, inputs_dict







@require_flax
class 						_snake_case    ( _snake_case		,       unittest.TestCase				):
 SCREAMING_SNAKE_CASE__ =			(
     (
         FlaxDistilBertModel,
         FlaxDistilBertForMaskedLM,
         FlaxDistilBertForMultipleChoice,
         FlaxDistilBertForQuestionAnswering,
         FlaxDistilBertForSequenceClassification,
         FlaxDistilBertForTokenClassification,
         FlaxDistilBertForQuestionAnswering,
     )
     if is_flax_available()
     else ()
 )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:List[Any]									=			FlaxDistilBertModelTester(self       )




 @slow
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       for model_class_name in self.all_model_classes:
             a		:int									=			model_class_name.from_pretrained('''distilbert-base-uncased'''       )
             a		:List[str]									=			model(np.ones((1, 1)       )       )
             self.assertIsNotNone(_lowerCamelCase       )







@require_flax
class 						_snake_case    ( unittest.TestCase				):




 @slow
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:List[str]									=			FlaxDistilBertModel.from_pretrained('''distilbert-base-uncased'''       )
       a		:Optional[Any]									=			np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]       )
       a		:List[Any]									=			np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]       )
       a		:List[str]									=			model(_lowerCamelCase   ,    attention_mask=_lowerCamelCase       )[0]
       a		:Union[str, Any]									=			(1, 11, 768)
       self.assertEqual(output.shape   ,    _lowerCamelCase       )
       a		:int									=			np.array([[[-0.1639, 0.3299, 0.1648], [-0.1746, 0.3289, 0.1710], [-0.1884, 0.3357, 0.1810]]]       )

       self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]   ,    _lowerCamelCase   ,    atol=1e-4       )       )





