
'''simple docstring'''


import warnings
from typing import List, Optional, Union

import numpy as np
import PIL
import torch
from PIL import Image

from .configuration_utils import ConfigMixin, register_to_config
from .utils import CONFIG_NAME, PIL_INTERPOLATION, deprecate




class     __snake_case			(    a__	):
		a_          =					CONFIG_NAME




		@register_to_config
		def __init__(  self					:Optional[int]			,    _SCREAMING_SNAKE_CASE					:bool = True			,    _SCREAMING_SNAKE_CASE					:int = 8			,    _SCREAMING_SNAKE_CASE					:str = "lanczos"			,    _SCREAMING_SNAKE_CASE					:bool = True			,    _SCREAMING_SNAKE_CASE					:bool = False			,    )					->			List[Any]:






								'''simple docstring'''






								super().__init__()




		@staticmethod
		def 	_snake_case	(  _SCREAMING_SNAKE_CASE					:np.ndarray)					->			Optional[int]:






								'''simple docstring'''






								if images.ndim == 3:
														UpperCamelCase_     :	Dict					     =     images[None, ...]
								UpperCamelCase_     :	Union[str, Any]					     =     (images * 2_5_5).round().astype("""uint8""")
								if images.shape[-1] == 1:
														# special case for grayscale (single channel) images
														UpperCamelCase_     :	int					     =     [Image.fromarray(image.squeeze()			,    mode="""L""") for image in images]
								else:
														UpperCamelCase_     :	Union[str, Any]					     =     [Image.fromarray(UpperCamelCase_) for image in images]

								return pil_images




		@staticmethod
		def 	_snake_case	(  _SCREAMING_SNAKE_CASE					:Union[List[PIL.Image.Image], PIL.Image.Image])					->			List[str]:






								'''simple docstring'''






								if not isinstance(UpperCamelCase_			,    UpperCamelCase_):
														UpperCamelCase_     :	int					     =     [images]
								UpperCamelCase_     :	str					     =     [np.array(UpperCamelCase_).astype(np.floataa) / 255.0 for image in images]
								UpperCamelCase_     :	Optional[Any]					     =     np.stack(UpperCamelCase_			,    axis=0)

								return images




		@staticmethod
		def 	_snake_case	(  _SCREAMING_SNAKE_CASE					:np.ndarray)					->			Optional[Any]:






								'''simple docstring'''






								if images.ndim == 3:
														UpperCamelCase_     :	Tuple					     =     images[..., None]

								UpperCamelCase_     :	int					     =     torch.from_numpy(images.transpose(0			,    3			,    1			,    2))
								return images




		@staticmethod
		def 	_snake_case	(  _SCREAMING_SNAKE_CASE					:torch.FloatTensor)					->			int:






								'''simple docstring'''






								UpperCamelCase_     :	Optional[Any]					     =     images.cpu().permute(0			,    2			,    3			,    1).float().numpy()
								return images




		@staticmethod
		def 	_snake_case	(  _SCREAMING_SNAKE_CASE					:Optional[int])					->			List[str]:






								'''simple docstring'''






								return 2.0 * images - 1.0




		@staticmethod
		def 	_snake_case	(  _SCREAMING_SNAKE_CASE					:Any)					->			Any:






								'''simple docstring'''






								return (images / 2 + 0.5).clamp(0			,    1)




		@staticmethod
		def 	_snake_case	(  _SCREAMING_SNAKE_CASE					:PIL.Image.Image)					->			Optional[int]:






								'''simple docstring'''






								UpperCamelCase_     :	str					     =     image.convert("""RGB""")
								return image




		def 	_snake_case	(  self					:Tuple			,    _SCREAMING_SNAKE_CASE					:PIL.Image.Image			,    _SCREAMING_SNAKE_CASE					:Optional[int] = None			,    _SCREAMING_SNAKE_CASE					:Optional[int] = None			,    )					->			Any:






								'''simple docstring'''






								if height is None:
														UpperCamelCase_     :	Dict					     =     image.height
								if width is None:
														UpperCamelCase_     :	str					     =     image.width

								UpperCamelCase_     :	Optional[Any]					     =     (
								    x - x % self.config.vae_scale_factor for x in (width, height)
								)  # resize to integer multiple of vae_scale_factor
								UpperCamelCase_     :	Optional[int]					     =     image.resize((width, height)			,    resample=PIL_INTERPOLATION[self.config.resample])
								return image




		def 	_snake_case	(  self					:Optional[Any]			,    _SCREAMING_SNAKE_CASE					:Union[torch.FloatTensor, PIL.Image.Image, np.ndarray]			,    _SCREAMING_SNAKE_CASE					:Optional[int] = None			,    _SCREAMING_SNAKE_CASE					:Optional[int] = None			,    )					->			List[str]:






								'''simple docstring'''






								UpperCamelCase_     :	List[Any]					     =     (PIL.Image.Image, np.ndarray, torch.Tensor)
								if isinstance(UpperCamelCase_			,    UpperCamelCase_):
														UpperCamelCase_     :	Optional[int]					     =     [image]
								elif not (isinstance(UpperCamelCase_			,    UpperCamelCase_) and all(isinstance(UpperCamelCase_			,    UpperCamelCase_) for i in image)):
														raise ValueError(
														    F"""Input is in incorrect format: {[type(UpperCamelCase_) for i in image]}. Currently, we only support {', '.join(UpperCamelCase_)}""")

								if isinstance(image[0]			,    PIL.Image.Image):
														if self.config.do_convert_rgb:
																				UpperCamelCase_     :	List[str]					     =     [self.convert_to_rgb(UpperCamelCase_) for i in image]
														if self.config.do_resize:
																				UpperCamelCase_     :	List[str]					     =     [self.resize(UpperCamelCase_			,    UpperCamelCase_			,    UpperCamelCase_) for i in image]
														UpperCamelCase_     :	Optional[Any]					     =     self.pil_to_numpy(UpperCamelCase_)  # to np
														UpperCamelCase_     :	Optional[int]					     =     self.numpy_to_pt(UpperCamelCase_)  # to pt

								elif isinstance(image[0]			,    np.ndarray):
														UpperCamelCase_     :	Optional[int]					     =     np.concatenate(UpperCamelCase_			,    axis=0) if image[0].ndim == 4 else np.stack(UpperCamelCase_			,    axis=0)
														UpperCamelCase_     :	List[Any]					     =     self.numpy_to_pt(UpperCamelCase_)
														UpperCamelCase_     :	List[str]					     =     image.shape
														if self.config.do_resize and (
														    height % self.config.vae_scale_factor != 0 or width % self.config.vae_scale_factor != 0
														):
																				raise ValueError(
																				    F"""Currently we only support resizing for PIL image - please resize your numpy array to be divisible by {self.config.vae_scale_factor}"""
																				    F"""currently the sizes are {height} and {width}. You can also pass a PIL image instead to use resize option in VAEImageProcessor""")

								elif isinstance(image[0]			,    torch.Tensor):
														UpperCamelCase_     :	Tuple					     =     torch.cat(UpperCamelCase_			,    axis=0) if image[0].ndim == 4 else torch.stack(UpperCamelCase_			,    axis=0)
														UpperCamelCase_     :	Tuple					     =     image.shape

														# don't need any preprocess if the image is latents
														if channel == 4:
																				return image

														if self.config.do_resize and (
														    height % self.config.vae_scale_factor != 0 or width % self.config.vae_scale_factor != 0
														):
																				raise ValueError(
																				    F"""Currently we only support resizing for PIL image - please resize your pytorch tensor to be divisible by {self.config.vae_scale_factor}"""
																				    F"""currently the sizes are {height} and {width}. You can also pass a PIL image instead to use resize option in VAEImageProcessor""")

        # expected range [0,1], normalize to [-1,1]
								UpperCamelCase_     :	Optional[Any]					     =     self.config.do_normalize
								if image.min() < 0:
														warnings.warn(
														    """Passing `image` as torch tensor with value range in [-1,1] is deprecated. The expected value range for image tensor is [0,1] """
														    F"""when passing as pytorch tensor or numpy Array. You passed `image` with value range [{image.min()},{image.max()}]"""			,    UpperCamelCase_			,    )
														UpperCamelCase_     :	Any					     =     False

								if do_normalize:
														UpperCamelCase_     :	str					     =     self.normalize(UpperCamelCase_)

								return image





		def 	_snake_case	(  self					:List[Any]			,    _SCREAMING_SNAKE_CASE					:torch.FloatTensor			,    _SCREAMING_SNAKE_CASE					:str = "pil"			,    _SCREAMING_SNAKE_CASE					:Optional[List[bool]] = None			,    )					->			Tuple:






								'''simple docstring'''






								if not isinstance(UpperCamelCase_			,    torch.Tensor):
														raise ValueError(
														    F"""Input for postprocessing is in incorrect format: {type(UpperCamelCase_)}. We only support pytorch tensor""")
								if output_type not in ["latent", "pt", "np", "pil"]:
														UpperCamelCase_     :	Union[str, Any]					     =     (
														    F"""the output_type {output_type} is outdated and has been set to `np`. Please make sure to set it to one of these instead: """
														    '''`pil`, `np`, `pt`, `latent`'''
														)
														deprecate("""Unsupported output_type"""			,    """1.0.0"""			,    UpperCamelCase_			,    standard_warn=UpperCamelCase_)
														UpperCamelCase_     :	Dict					     =     '''np'''

								if output_type == "latent":
														return image

								if do_denormalize is None:
														UpperCamelCase_     :	Union[str, Any]					     =     [self.config.do_normalize] * image.shape[0]

								UpperCamelCase_     :	str					     =     torch.stack(
								    [self.denormalize(image[i]) if do_denormalize[i] else image[i] for i in range(image.shape[0])])

								if output_type == "pt":
														return image

								UpperCamelCase_     :	Tuple					     =     self.pt_to_numpy(UpperCamelCase_)

								if output_type == "np":
														return image

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







class     __snake_case			(    a__	):
		a_          =					CONFIG_NAME




		@register_to_config
		def __init__(  self					:Dict			,    _SCREAMING_SNAKE_CASE					:bool = True			,    _SCREAMING_SNAKE_CASE					:int = 8			,    _SCREAMING_SNAKE_CASE					:str = "lanczos"			,    _SCREAMING_SNAKE_CASE					:bool = True			,    )					->			List[str]:






								'''simple docstring'''






								super().__init__()




		@staticmethod
		def 	_snake_case	(  _SCREAMING_SNAKE_CASE					:Dict)					->			str:






								'''simple docstring'''






								if images.ndim == 3:
														UpperCamelCase_     :	Any					     =     images[None, ...]
								UpperCamelCase_     :	Optional[Any]					     =     (images * 2_5_5).round().astype("""uint8""")
								if images.shape[-1] == 1:
														# special case for grayscale (single channel) images
														UpperCamelCase_     :	Optional[int]					     =     [Image.fromarray(image.squeeze()			,    mode="""L""") for image in images]
								else:
														UpperCamelCase_     :	Tuple					     =     [Image.fromarray(image[:, :, :3]) for image in images]

								return pil_images




		@staticmethod
		def 	_snake_case	(  _SCREAMING_SNAKE_CASE					:List[str])					->			Optional[Any]:






								'''simple docstring'''






								return image[:, :, 1] * 2**8 + image[:, :, 2]




		def 	_snake_case	(  self					:List[str]			,    _SCREAMING_SNAKE_CASE					:List[Any])					->			Dict:






								'''simple docstring'''






								if images.ndim == 3:
														UpperCamelCase_     :	Optional[Any]					     =     images[None, ...]
								UpperCamelCase_     :	List[Any]					     =     images[:, :, :, 3:]
								if images.shape[-1] == 6:
														UpperCamelCase_     :	Dict					     =     (images_depth * 2_5_5).round().astype("""uint8""")
														UpperCamelCase_     :	Optional[int]					     =     [
														    Image.fromarray(self.rgblike_to_depthmap(UpperCamelCase_)			,    mode="""I;16""") for image_depth in images_depth
														]
								elif images.shape[-1] == 4:
														UpperCamelCase_     :	int					     =     (images_depth * 6_5_5_3_5.0).astype(np.uintaa)
														UpperCamelCase_     :	str					     =     [Image.fromarray(UpperCamelCase_			,    mode="""I;16""") for image_depth in images_depth]
								else:
														raise Exception("""Not supported""")

								return pil_images





		def 	_snake_case	(  self					:List[str]			,    _SCREAMING_SNAKE_CASE					:torch.FloatTensor			,    _SCREAMING_SNAKE_CASE					:str = "pil"			,    _SCREAMING_SNAKE_CASE					:Optional[List[bool]] = None			,    )					->			int:






								'''simple docstring'''






								if not isinstance(UpperCamelCase_			,    torch.Tensor):
														raise ValueError(
														    F"""Input for postprocessing is in incorrect format: {type(UpperCamelCase_)}. We only support pytorch tensor""")
								if output_type not in ["latent", "pt", "np", "pil"]:
														UpperCamelCase_     :	List[Any]					     =     (
														    F"""the output_type {output_type} is outdated and has been set to `np`. Please make sure to set it to one of these instead: """
														    '''`pil`, `np`, `pt`, `latent`'''
														)
														deprecate("""Unsupported output_type"""			,    """1.0.0"""			,    UpperCamelCase_			,    standard_warn=UpperCamelCase_)
														UpperCamelCase_     :	Union[str, Any]					     =     '''np'''

								if do_denormalize is None:
														UpperCamelCase_     :	int					     =     [self.config.do_normalize] * image.shape[0]

								UpperCamelCase_     :	Dict					     =     torch.stack(
								    [self.denormalize(image[i]) if do_denormalize[i] else image[i] for i in range(image.shape[0])])

								UpperCamelCase_     :	Optional[Any]					     =     self.pt_to_numpy(UpperCamelCase_)

								if output_type == "np":
														if image.shape[-1] == 6:
																				UpperCamelCase_     :	Dict					     =     np.stack([self.rgblike_to_depthmap(im[:, :, 3:]) for im in image]			,    axis=0)
														else:
																				UpperCamelCase_     :	str					     =     image[:, :, :, 3:]
														return image[:, :, :, :3], image_depth

								if output_type == "pil":
														return self.numpy_to_pil(UpperCamelCase_), self.numpy_to_depth(UpperCamelCase_)
								else:
														raise Exception(F"""This type {output_type} is not supported""")








"""simple docstring"""





import argparse
import time

import torch
import transformers
from measures_util import end_measure, log_measures, start_measure
from transformers import AutoConfig, AutoModelForCausalLM, AutoModelForSeqaSeqLM, AutoTokenizer

from accelerate.utils import compute_module_sizes


snake_case__		:  Optional[Any]     					=					{
    '''gpt-j-6b''': {'''is_causal''': True, '''model''': '''sgugger/sharded-gpt-j-6B''', '''tokenizer''': '''EleutherAI/gpt-j-6B'''},
    '''gpt-neox''': {'''is_causal''': True, '''model''': '''EleutherAI/gpt-neox-20b'''},
    '''opt''': {'''is_causal''': True, '''model''': '''facebook/opt-30b'''},
    '''T0pp''': {'''is_causal''': False, '''model''': '''bigscience/T0pp''', '''model_revision''': '''sharded'''},
}

snake_case__		:  Any     					=					[
    '''Hello, my name is''',
    '''Are unicorns real? Unicorns are''',
    '''For the first time in several years,''',
    '''My name is Julien and I am''',
    '''The goal of life is''',
    '''Whenever I\'m sad, I like to''',
]


def   _snake_case    (    ):
      lowerCAmelCase       :						int							= argparse.ArgumentParser(description='''Run and time generations on a big model using Accelerate.'''      )
      parser.add_argument('''model_name'''			, type=_snake_case			, default=_snake_case			, help='''The name of the model to try.'''      )
      parser.add_argument(
          '''--tokenizer_name'''			, type=_snake_case			, default=_snake_case			, help='''The name of the tokenizer (if different from the model.'''      )
      parser.add_argument('''--is_causal'''			, type=_snake_case			, default=_snake_case			, help='''Whether or not the model is causal.'''      )
      parser.add_argument(
          '''--model_revision'''			, type=_snake_case			, default=_snake_case			, help='''The revision to use for the model checkpoint.'''      )
      parser.add_argument('''--torch_dtype'''			, type=_snake_case			, default=_snake_case			, help='''The dtype for the model.'''      )
      parser.add_argument('''--disk_offload'''			, action='''store_true'''      )

      lowerCAmelCase       :						Dict							= parser.parse_args()

      # Sanitize args
      if args.model_name in DEFAULT_MODELS:
            lowerCAmelCase       :						Optional[int]							= DEFAULT_MODELS[args.model_name]
            lowerCAmelCase       :						Union[str, Any]							= defaults['''model''']
            if args.tokenizer_name is None:
                  lowerCAmelCase       :						Union[str, Any]							= defaults.get('''tokenizer'''			, args.model_name      )
            if args.is_causal is None:
                  lowerCAmelCase       :						List[Any]							= defaults['''is_causal''']
            if args.model_revision is None:
                  lowerCAmelCase       :						Tuple							= defaults.get('''model_revision'''			, '''main'''      )

      if args.is_causal is None:
            raise ValueError('''Could not infer the default for `--is_causal`, pass either True or False for it.'''      )
      if args.tokenizer_name is None:
            lowerCAmelCase       :						Union[str, Any]							= args.model_name
      if args.model_revision is None:
            lowerCAmelCase       :						List[Any]							= '''main'''

      return args


def   _snake_case    (    ):
      transformers.utils.logging.set_verbosity_error()
      lowerCAmelCase       :						Union[str, Any]							= parse_args()

      if args.torch_dtype is None:
            lowerCAmelCase       :						List[str]							= AutoConfig.from_pretrained(args.model_name      )
            lowerCAmelCase       :						Optional[Any]							= getattr(_snake_case			, '''torch_dtype'''			, torch.floataa      )
      else:
            lowerCAmelCase       :						Optional[int]							= getattr(_snake_case			, args.torch_dtype      )
      lowerCAmelCase       :						Tuple							= AutoModelForCausalLM if args.is_causal else AutoModelForSeqaSeqLM
      lowerCAmelCase       :						str							= {
          '''torch_dtype''': torch_dtype,
          '''revision''': args.model_revision,
      }
      if args.disk_offload:
            lowerCAmelCase       :						Optional[int]							= '''tmp_offload'''
            lowerCAmelCase       :						Optional[int]							= True

      lowerCAmelCase       :						str							= start_measure()
      lowerCAmelCase       :						List[str]							= model_cls.from_pretrained(args.model_name			, device_map='''auto'''			, **_snake_case      )
      lowerCAmelCase       :						Tuple							= end_measure(_snake_case      )
      log_measures(_snake_case			, '''Model loading'''      )

      lowerCAmelCase       :						str							= compute_module_sizes(_snake_case      )
      lowerCAmelCase       :						Tuple							= {v: 0 for v in model.hf_device_map.values()}
      for module, device in model.hf_device_map.items():
            device_size[device] += module_sizes[module]
      lowerCAmelCase       :						int							= '''\n'''.join([f'''- {device}: {size // 2**20}MiB''' for device, size in device_size.items()]      )
      print(f'''\nTheoretical use:\n{message}'''      )

      lowerCAmelCase       :						int							= AutoTokenizer.from_pretrained(args.tokenizer_name      )

      lowerCAmelCase       :						List[str]							= start_measure()
      lowerCAmelCase       :						Union[str, Any]							= []
      lowerCAmelCase       :						int							= []
      lowerCAmelCase       :						Dict							= []
      for prompt in PROMPTS:
            lowerCAmelCase       :						str							= tokenizer(_snake_case			, return_tensors='''pt'''      ).to(0      )
            lowerCAmelCase       :						int							= inputs['''input_ids'''][0].tolist()
            lowerCAmelCase       :						Optional[Any]							= time.time()
            lowerCAmelCase       :						Optional[int]							= model.generate(inputs['''input_ids''']      )
            lowerCAmelCase       :						Any							= time.time()
            lowerCAmelCase       :						str							= outputs[0].tolist()
            lowerCAmelCase       :						int							= len(_snake_case      ) if outputs[: len(_snake_case      )] != tokens else len(_snake_case      ) - len(_snake_case      )
            lowerCAmelCase       :						Tuple							= after_generate - before_generate

            lowerCAmelCase       :						Tuple							= tokenizer.decode(_snake_case			, skip_special_tokens=_snake_case      )
            texts_outs.append(_snake_case      )
            generation_times.append(_snake_case      )
            gen_tokens.append(_snake_case      )
            print(f'''Prompt: {prompt}\nGeneration {text_out}\nIn {generation_time:.2f}s for {num_gen_tokens} tokens\n'''      )

      lowerCAmelCase       :						List[Any]							= end_measure(_snake_case      )
      log_measures(_snake_case			, '''Model generation'''      )

      lowerCAmelCase       :						str							= [gen / tok for gen, tok in zip(_snake_case			, _snake_case      )]
      lowerCAmelCase       :						Any							= sum(_snake_case      ) / len(_snake_case      )
      print(f'''Average time of generation per token: {avg_gen:.2f}s'''      )
      print(f'''First generation (avg time per token): {generation_times_per_token[0]:.2f}s'''      )
      lowerCAmelCase       :						List[Any]							= sum(generation_times_per_token[1:]      ) / (len(_snake_case      ) - 1)
      print(f'''Average time of generation per token (excluding the first): {avg_gen:.2f}s'''      )


if __name__ == "__main__":
 main()


