
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


__lowerCAmelCase				      =					{
    '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'},
}

__lowerCAmelCase				      =					[
    '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       a			(   )    ->Optional[int]:




			'''simple docstring'''


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

			SCREAMING_SNAKE_CASE    	=   parser.parse_args()

			# Sanitize args
			if args.model_name in DEFAULT_MODELS:
						SCREAMING_SNAKE_CASE    	=   DEFAULT_MODELS[args.model_name]
						SCREAMING_SNAKE_CASE    	=   defaults['''model''']
						if args.tokenizer_name is None:
									SCREAMING_SNAKE_CASE    	=   defaults.get('''tokenizer'''		,	args.model_name  )
						if args.is_causal is None:
									SCREAMING_SNAKE_CASE    	=   defaults['''is_causal''']
						if args.model_revision is None:
									SCREAMING_SNAKE_CASE    	=   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:
						SCREAMING_SNAKE_CASE    	=   args.model_name
			if args.model_revision is None:
						SCREAMING_SNAKE_CASE    	=   '''main'''

			return args


def       a			(   )    ->List[str]:




			'''simple docstring'''


			transformers.utils.logging.set_verbosity_error()
			SCREAMING_SNAKE_CASE    	=   parse_args()

			if args.torch_dtype is None:
						SCREAMING_SNAKE_CASE    	=   AutoConfig.from_pretrained(args.model_name  )
						SCREAMING_SNAKE_CASE    	=   getattr(a		,	'''torch_dtype'''		,	torch.floataa  )
			else:
						SCREAMING_SNAKE_CASE    	=   getattr(a		,	args.torch_dtype  )
			SCREAMING_SNAKE_CASE    	=   AutoModelForCausalLM if args.is_causal else AutoModelForSeqaSeqLM
			SCREAMING_SNAKE_CASE    	=   {
			    '''torch_dtype''': torch_dtype,
			    '''revision''': args.model_revision,
			}
			if args.disk_offload:
						SCREAMING_SNAKE_CASE    	=   '''tmp_offload'''
						SCREAMING_SNAKE_CASE    	=   True

			SCREAMING_SNAKE_CASE    	=   start_measure()
			SCREAMING_SNAKE_CASE    	=   model_cls.from_pretrained(args.model_name		,	device_map='''auto'''		,	**a  )
			SCREAMING_SNAKE_CASE    	=   end_measure(a  )
			log_measures(a		,	'''Model loading'''  )

			SCREAMING_SNAKE_CASE    	=   compute_module_sizes(a  )
			SCREAMING_SNAKE_CASE    	=   {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]
			SCREAMING_SNAKE_CASE    	=   '''\n'''.join([F"""- {device}: {size // 2**20}MiB""" for device, size in device_size.items()]  )
			print(F"""\nTheoretical use:\n{message}"""  )

			SCREAMING_SNAKE_CASE    	=   AutoTokenizer.from_pretrained(args.tokenizer_name  )

			SCREAMING_SNAKE_CASE    	=   start_measure()
			SCREAMING_SNAKE_CASE    	=   []
			SCREAMING_SNAKE_CASE    	=   []
			SCREAMING_SNAKE_CASE    	=   []
			for prompt in PROMPTS:
						SCREAMING_SNAKE_CASE    	=   tokenizer(a		,	return_tensors='''pt'''  ).to(0  )
						SCREAMING_SNAKE_CASE    	=   inputs['''input_ids'''][0].tolist()
						SCREAMING_SNAKE_CASE    	=   time.time()
						SCREAMING_SNAKE_CASE    	=   model.generate(inputs['''input_ids''']  )
						SCREAMING_SNAKE_CASE    	=   time.time()
						SCREAMING_SNAKE_CASE    	=   outputs[0].tolist()
						SCREAMING_SNAKE_CASE    	=   len(a  ) if outputs[: len(a  )] != tokens else len(a  ) - len(a  )
						SCREAMING_SNAKE_CASE    	=   after_generate - before_generate

						SCREAMING_SNAKE_CASE    	=   tokenizer.decode(a		,	skip_special_tokens=a  )
						texts_outs.append(a  )
						generation_times.append(a  )
						gen_tokens.append(a  )
						print(F"""Prompt: {prompt}\nGeneration {text_out}\nIn {generation_time:.2f}s for {num_gen_tokens} tokens\n"""  )

			SCREAMING_SNAKE_CASE    	=   end_measure(a  )
			log_measures(a		,	'''Model generation'''  )

			SCREAMING_SNAKE_CASE    	=   [gen / tok for gen, tok in zip(a		,	a  )]
			SCREAMING_SNAKE_CASE    	=   sum(a  ) / len(a  )
			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"""  )
			SCREAMING_SNAKE_CASE    	=   sum(generation_times_per_token[1:]  ) / (len(a  ) - 1)
			print(F"""Average time of generation per token (excluding the first): {avg_gen:.2f}s"""  )


if __name__ == "__main__":
							main()

import subprocess
from argparse import ArgumentParser
from typing import List, Union

from huggingface_hub.hf_api import HfFolder, create_repo, whoami
from requests.exceptions import HTTPError

from . import BaseTransformersCLICommand





class 						lowerCamelCase			(					__lowerCamelCase				):







	@staticmethod
	def    snake_case__    (    lowercase			:ArgumentParser )		->			Union[str, Any]:







				"""simple docstring"""






				SCREAMING_SNAKE_CASE    	=   parser.add_parser('''login'''      ,    help='''Log in using the same credentials as on huggingface.co''' )
				login_parser.set_defaults(func=lambda lowercase : LoginCommand(lowercase ) )
				SCREAMING_SNAKE_CASE    	=   parser.add_parser('''whoami'''      ,    help='''Find out which huggingface.co account you are logged in as.''' )
				whoami_parser.set_defaults(func=lambda lowercase : WhoamiCommand(lowercase ) )
				SCREAMING_SNAKE_CASE    	=   parser.add_parser('''logout'''      ,    help='''Log out''' )
				logout_parser.set_defaults(func=lambda lowercase : LogoutCommand(lowercase ) )

				# new system: git-based repo system
				SCREAMING_SNAKE_CASE    	=   parser.add_parser(
				    '''repo'''      ,    help='''Deprecated: use `huggingface-cli` instead. Commands to interact with your huggingface.co repos.'''      ,    )
				SCREAMING_SNAKE_CASE    	=   repo_parser.add_subparsers(
				    help='''Deprecated: use `huggingface-cli` instead. huggingface.co repos related commands''' )
				SCREAMING_SNAKE_CASE    	=   repo_subparsers.add_parser(
				    '''create'''      ,    help='''Deprecated: use `huggingface-cli` instead. Create a new repo on huggingface.co''' )
				repo_create_parser.add_argument(
				    '''name'''      ,    type=lowercase      ,    help='''Name for your model\'s repo. Will be namespaced under your username to build the model id.'''      ,    )
				repo_create_parser.add_argument('''--organization'''      ,    type=lowercase      ,    help='''Optional: organization namespace.''' )
				repo_create_parser.add_argument('''-y'''      ,    '''--yes'''      ,    action='''store_true'''      ,    help='''Optional: answer Yes to the prompt''' )
				repo_create_parser.set_defaults(func=lambda lowercase : RepoCreateCommand(lowercase ) )





class 						lowerCamelCase			:
	UpperCamelCase_     :				List[str]								=  '\u001b[1m'
	UpperCamelCase_     :				Union[str, Any]								=  '\u001b[31m'
	UpperCamelCase_     :				Any								=  '\u001b[90m'
	UpperCamelCase_     :				List[str]								=  '\u001b[0m'







	@classmethod
	def    snake_case__    (    cls			:List[Any]      ,    lowercase			:Any )		->			List[str]:







				"""simple docstring"""






				return f"""{cls._bold}{s}{cls._reset}"""







	@classmethod
	def    snake_case__    (    cls			:Optional[Any]      ,    lowercase			:str )		->			Tuple:







				"""simple docstring"""






				return f"""{cls._bold}{cls._red}{s}{cls._reset}"""







	@classmethod
	def    snake_case__    (    cls			:List[str]      ,    lowercase			:Any )		->			List[Any]:







				"""simple docstring"""






				return f"""{cls._gray}{s}{cls._reset}"""



def       a			(   a		,	a  )    ->str:




			'''simple docstring'''


			SCREAMING_SNAKE_CASE    	=   [max(len(str(a  )  ) for x in col  ) for col in zip(*a		,	a  )]
			SCREAMING_SNAKE_CASE    	=   ('''{{:{}}} ''' * len(a  )).format(*a  )
			SCREAMING_SNAKE_CASE    	=   []
			lines.append(row_format.format(*a  )  )
			lines.append(row_format.format(*['''-''' * w for w in col_widths]  )  )
			for row in rows:
						lines.append(row_format.format(*a  )  )
			return "\n".join(a  )





class 						lowerCamelCase			:







	def __init__(    self			:List[str]      ,    lowercase			:Optional[Any] )		->			Union[str, Any]:







				"""simple docstring"""






				SCREAMING_SNAKE_CASE    	=   args





class 						lowerCamelCase			(					__lowerCamelCase				):







	def    snake_case__    (    self			:List[str] )		->			Optional[Any]:







				"""simple docstring"""






				print(
				    ANSI.red(
				        '''ERROR! `huggingface-cli login` uses an outdated login mechanism '''
				        '''that is not compatible with the Hugging Face Hub backend anymore. '''
				        '''Please use `huggingface-cli login instead.''' ) )





class 						lowerCamelCase			(					__lowerCamelCase				):







	def    snake_case__    (    self			:List[Any] )		->			Any:







				"""simple docstring"""






				print(
				    ANSI.red(
				        '''WARNING! `transformers-cli whoami` is deprecated and will be removed in v5. Please use '''
				        '''`huggingface-cli whoami` instead.''' ) )
				SCREAMING_SNAKE_CASE    	=   HfFolder.get_token()
				if token is None:
							print('''Not logged in''' )
							exit()
				try:
							SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE    	=   whoami(lowercase )
							print(lowercase )
							if orgs:
										print(ANSI.bold('''orgs: ''' )      ,    ''','''.join(lowercase ) )
				except HTTPError as e:
							print(lowercase )
							print(ANSI.red(e.response.text ) )
							exit(1 )





class 						lowerCamelCase			(					__lowerCamelCase				):







	def    snake_case__    (    self			:str )		->			Union[str, Any]:







				"""simple docstring"""






				print(
				    ANSI.red(
				        '''ERROR! `transformers-cli logout` uses an outdated logout mechanism '''
				        '''that is not compatible with the Hugging Face Hub backend anymore. '''
				        '''Please use `huggingface-cli logout instead.''' ) )





class 						lowerCamelCase			(					__lowerCamelCase				):







	def    snake_case__    (    self			:int )		->			Optional[Any]:







				"""simple docstring"""






				print(
				    ANSI.red(
				        '''WARNING! Managing repositories through transformers-cli is deprecated. '''
				        '''Please use `huggingface-cli` instead.''' ) )
				SCREAMING_SNAKE_CASE    	=   HfFolder.get_token()
				if token is None:
							print('''Not logged in''' )
							exit(1 )
				try:
							SCREAMING_SNAKE_CASE    	=   subprocess.check_output(['''git''', '''--version'''] ).decode('''utf-8''' )
							print(ANSI.gray(stdout.strip() ) )
				except FileNotFoundError:
							print('''Looks like you do not have git installed, please install.''' )

				try:
							SCREAMING_SNAKE_CASE    	=   subprocess.check_output(['''git-lfs''', '''--version'''] ).decode('''utf-8''' )
							print(ANSI.gray(stdout.strip() ) )
				except FileNotFoundError:
							print(
							    ANSI.red(
							        '''Looks like you do not have git-lfs installed, please install.'''
							        ''' You can install from https://git-lfs.github.com/.'''
							        ''' Then run `git lfs install` (you only have to do this once).''' ) )
				print('''''' )

				SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE    	=   whoami(lowercase )
				SCREAMING_SNAKE_CASE    	=   self.args.organization if self.args.organization is not None else user
				SCREAMING_SNAKE_CASE    	=   f"""{namespace}/{self.args.name}"""
				print(f"""You are about to create {ANSI.bold(lowercase )}""" )

				if not self.args.yes:
							SCREAMING_SNAKE_CASE    	=   input('''Proceed? [Y/n] ''' ).lower()
							if not (choice == "" or choice == "y" or choice == "yes"):
										print('''Abort''' )
										exit()
				try:
							SCREAMING_SNAKE_CASE    	=   create_repo(lowercase      ,    name=self.args.name      ,    organization=self.args.organization )
				except HTTPError as e:
							print(lowercase )
							print(ANSI.red(e.response.text ) )
							exit(1 )
				print('''\nYour repo now lives at:''' )
				print(f"""  {ANSI.bold(lowercase )}""" )
				print('''\nYou can clone it locally with the command below, and commit/push as usual.''' )
				print(f"""\n  git clone {url}""" )
				print('''''' )