



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


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

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







def    _snake_case (       __snake_case					,  __snake_case					,  __snake_case=8    )     ->     Optional[Any]:






      '''simple docstring'''







      UpperCAmelCase_  :	Union[str, Any]				      =		height // scale_factor**2
      if height % scale_factor**2 != 0:
            new_height += 1
      UpperCAmelCase_  :	Optional[int]				      =		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_ (lowercase__  ):



       def __init__(     self     ,lowercase     ,lowercase     ,lowercase     ,):

             """simple docstring"""





             super().__init__()

             self.register_modules(
                 unet=__A     ,scheduler=__A     ,movq=__A     ,)
             UpperCAmelCase_  :	Any				      =		2 ** (len(self.movq.config.block_out_channels) - 1)



       def  A_   (     self     ,lowercase     ,lowercase     ,lowercase     ,lowercase     ,lowercase     ,lowercase):

             """simple docstring"""





             if latents is None:
                   UpperCAmelCase_  :	Tuple				      =		randn_tensor(__A     ,generator=__A     ,device=__A     ,dtype=__A)
             else:
                   if latents.shape != shape:
                         raise ValueError(F"""Unexpected latents shape, got {latents.shape}, expected {shape}""")
                   UpperCAmelCase_  :	int				      =		latents.to(__A)

             UpperCAmelCase_  :	Union[str, Any]				      =		latents * scheduler.init_noise_sigma
             return latents



       def  A_   (     self     ,lowercase=0):

             """simple docstring"""





             if is_accelerate_available():
                   from accelerate import cpu_offload
             else:
                   raise ImportError("Please install accelerate via `pip install accelerate`")

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

             UpperCAmelCase_  :	Optional[Any]				      =		[
                 self.unet,
                 self.movq,
             ]
             for cpu_offloaded_model in models:
                   if cpu_offloaded_model is not None:
                         cpu_offload(__A     ,__A)



       def  A_   (     self     ,lowercase=0):

             """simple docstring"""





             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.")

             UpperCAmelCase_  :	str				      =		torch.device(F"""cuda:{gpu_id}""")

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

             UpperCAmelCase_  :	Dict				      =		None
             for cpu_offloaded_model in [self.unet, self.movq]:
                   UpperCAmelCase_      ,      UpperCAmelCase_  :	Tuple				      =		cpu_offload_with_hook(__A     ,__A     ,prev_module_hook=__A)

             # We'll offload the last model manually.
             UpperCAmelCase_  :	Dict				      =		hook



       @property
       # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
       def  A_   (     self):

             """simple docstring"""





             if not hasattr(self.unet     ,"_hf_hook"):
                   return self.device
             for module in self.unet.modules():
                   if (
                       hasattr(__A     ,"_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(__A)
       def __call__(     self     ,lowercase     ,lowercase     ,lowercase = 512     ,lowercase = 512     ,lowercase = 100     ,lowercase = 4.0     ,lowercase = 1     ,lowercase = None     ,lowercase = None     ,lowercase = "pil"     ,lowercase = True     ,):

             """simple docstring"""





             UpperCAmelCase_  :	str				      =		self._execution_device

             UpperCAmelCase_  :	Union[str, Any]				      =		guidance_scale > 1.0

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

             if do_classifier_free_guidance:
                   UpperCAmelCase_  :	str				      =		image_embeds.repeat_interleave(__A     ,dim=0)
                   UpperCAmelCase_  :	Optional[int]				      =		negative_image_embeds.repeat_interleave(__A     ,dim=0)

             UpperCAmelCase_  :	int				      =		torch.cat([negative_image_embeds, image_embeds]     ,dim=0).to(dtype=self.unet.dtype     ,device=__A)

             self.scheduler.set_timesteps(__A     ,device=__A)
             UpperCAmelCase_  :	Optional[Any]				      =		self.scheduler.timesteps

             UpperCAmelCase_  :	Dict				      =		self.unet.config.in_channels

             UpperCAmelCase_      ,      UpperCAmelCase_  :	int				      =		downscale_height_and_width(__A     ,__A     ,self.movq_scale_factor)

             # create initial latent
             UpperCAmelCase_  :	Union[str, Any]				      =		self.prepare_latents(
                 (batch_size, num_channels_latents, height, width)     ,image_embeds.dtype     ,__A     ,__A     ,__A     ,self.scheduler     ,)

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

                   UpperCAmelCase_  :	List[str]				      =		{"image_embeds": image_embeds}
                   UpperCAmelCase_  :	Dict				      =		self.unet(
                       sample=__A     ,timestep=__A     ,encoder_hidden_states=__A     ,added_cond_kwargs=__A     ,return_dict=__A     ,)[0]

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

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

             if output_type == "pil":
                   UpperCAmelCase_  :	Any				      =		self.numpy_to_pil(__A)

             if not return_dict:
                   return (image,)

             return ImagePipelineOutput(images=__A)





import os
from argparse import ArgumentParser, Namespace

from ..data import SingleSentenceClassificationProcessor as Processor
from ..pipelines import TextClassificationPipeline
from ..utils import is_tf_available, is_torch_available, logging
from . import BaseTransformersCLICommand


if not is_tf_available() and not is_torch_available():
   raise RuntimeError('''At least one of PyTorch or TensorFlow 2.0+ should be installed to use CLI training''')

# TF training parameters
__lowerCamelCase         =			False
__lowerCamelCase         =			False







def    _snake_case (       __snake_case    )     ->     Any:






      '''simple docstring'''







      return TrainCommand(__snake_case    )





class   snake_case_ (lowercase__  ):




       """simple docstring"""






       @staticmethod
       def  A_   (     lowercase):

             """simple docstring"""





             UpperCAmelCase_  :	str				      =		parser.add_parser("train"     ,help="CLI tool to train a model on a task.")

             train_parser.add_argument(
                 "--train_data"     ,type=lowercase     ,required=lowercase     ,help="path to train (and optionally evaluation) dataset as a csv with tab separated labels and sentences."     ,)
             train_parser.add_argument(
                 "--column_label"     ,type=lowercase     ,default=0     ,help="Column of the dataset csv file with example labels.")
             train_parser.add_argument(
                 "--column_text"     ,type=lowercase     ,default=1     ,help="Column of the dataset csv file with example texts.")
             train_parser.add_argument(
                 "--column_id"     ,type=lowercase     ,default=2     ,help="Column of the dataset csv file with example ids.")
             train_parser.add_argument(
                 "--skip_first_row"     ,action="store_true"     ,help="Skip the first row of the csv file (headers).")

             train_parser.add_argument("--validation_data"     ,type=lowercase     ,default=""     ,help="path to validation dataset.")
             train_parser.add_argument(
                 "--validation_split"     ,type=lowercase     ,default=0.1     ,help="if validation dataset is not provided, fraction of train dataset to use as validation dataset."     ,)

             train_parser.add_argument("--output"     ,type=lowercase     ,default="./"     ,help="path to saved the trained model.")

             train_parser.add_argument(
                 "--task"     ,type=lowercase     ,default="text_classification"     ,help="Task to train the model on.")
             train_parser.add_argument(
                 "--model"     ,type=lowercase     ,default="bert-base-uncased"     ,help="Model's name or path to stored model.")
             train_parser.add_argument("--train_batch_size"     ,type=lowercase     ,default=32     ,help="Batch size for training.")
             train_parser.add_argument("--valid_batch_size"     ,type=lowercase     ,default=64     ,help="Batch size for validation.")
             train_parser.add_argument("--learning_rate"     ,type=lowercase     ,default=3E-5     ,help="Learning rate.")
             train_parser.add_argument("--adam_epsilon"     ,type=lowercase     ,default=1E-08     ,help="Epsilon for Adam optimizer.")
             train_parser.set_defaults(func=lowercase)



       def __init__(     self     ,lowercase):

             """simple docstring"""





             UpperCAmelCase_  :	Optional[Any]				      =		logging.get_logger("transformers-cli/training")

             UpperCAmelCase_  :	List[Any]				      =		"tf" if is_tf_available() else "torch"

             os.makedirs(args.output     ,exist_ok=lowercase)
             UpperCAmelCase_  :	Union[str, Any]				      =		args.output

             UpperCAmelCase_  :	Optional[int]				      =		args.column_label
             UpperCAmelCase_  :	Dict				      =		args.column_text
             UpperCAmelCase_  :	Optional[Any]				      =		args.column_id

             self.logger.info(F"""Loading {args.task} pipeline for {args.model}""")
             if args.task == "text_classification":
                   UpperCAmelCase_  :	List[Any]				      =		TextClassificationPipeline.from_pretrained(args.model)
             elif args.task == "token_classification":
                   raise NotImplementedError
             elif args.task == "question_answering":
                   raise NotImplementedError

             self.logger.info(F"""Loading dataset from {args.train_data}""")
             UpperCAmelCase_  :	str				      =		Processor.create_from_csv(
                 args.train_data     ,column_label=args.column_label     ,column_text=args.column_text     ,column_id=args.column_id     ,skip_first_row=args.skip_first_row     ,)
             UpperCAmelCase_  :	str				      =		None
             if args.validation_data:
                   self.logger.info(F"""Loading validation dataset from {args.validation_data}""")
                   UpperCAmelCase_  :	Union[str, Any]				      =		Processor.create_from_csv(
                       args.validation_data     ,column_label=args.column_label     ,column_text=args.column_text     ,column_id=args.column_id     ,skip_first_row=args.skip_first_row     ,)

             UpperCAmelCase_  :	List[str]				      =		args.validation_split
             UpperCAmelCase_  :	Tuple				      =		args.train_batch_size
             UpperCAmelCase_  :	Optional[int]				      =		args.valid_batch_size
             UpperCAmelCase_  :	Dict				      =		args.learning_rate
             UpperCAmelCase_  :	List[str]				      =		args.adam_epsilon



       def  A_   (     self):

             """simple docstring"""





             if self.framework == "tf":
                   return self.run_tf()
             return self.run_torch()



       def  A_   (     self):

             """simple docstring"""





             raise NotImplementedError



       def  A_   (     self):

             """simple docstring"""





             self.pipeline.fit(
                 self.train_dataset     ,validation_data=self.valid_dataset     ,validation_split=self.validation_split     ,learning_rate=self.learning_rate     ,adam_epsilon=self.adam_epsilon     ,train_batch_size=self.train_batch_size     ,valid_batch_size=self.valid_batch_size     ,)

             # Save trained pipeline
             self.pipeline.save_pretrained(self.output)
