


'''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


_lowerCAmelCase	:		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'},
}

_lowerCAmelCase	:		Union[str, 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       __UpperCamelCase     (     )     ->   Optional[int]:




	"""simple docstring"""





	lowerCAmelCase       :     Any   				=			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'					)

	lowerCAmelCase       :     int   				=			parser.parse_args()

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

	return args

def       __UpperCamelCase     (     )     ->   Tuple:




	"""simple docstring"""





	transformers.utils.logging.set_verbosity_error()
	lowerCAmelCase       :     str   				=			parse_args()

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

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

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

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

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

		lowerCAmelCase       :     Optional[int]   				=			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"					)

	lowerCAmelCase       :     Tuple   				=			end_measure(_A					)
	log_measures(_A       , 'Model generation'					)

	lowerCAmelCase       :     str   				=			[gen / tok for gen, tok in zip(_A       , _A					)]
	lowerCAmelCase       :     Optional[int]   				=			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"					)
	lowerCAmelCase       :     Union[str, Any]   				=			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()




'''simple docstring'''




import math
from collections.abc import Generator

def       __UpperCamelCase     (     _A     :					int					)     ->   Generator[int, None, None]:




	"""simple docstring"""





	lowerCAmelCase       :     Generator   				=			(i for i in range(1       , (max_n + 1)					))
	for i in (n for n in numbers if n > 1):
		for j in range(2       , _A					):
			if (i % j) == 0:
				break
		else:
			yield i

def       __UpperCamelCase     (     _A     :					int					)     ->   Generator[int, None, None]:




	"""simple docstring"""





	lowerCAmelCase       :     Generator   				=			(i for i in range(1       , (max_n + 1)					))
	for i in (n for n in numbers if n > 1):
		# only need to check for factors up to sqrt(i)
		lowerCAmelCase       :     List[str]   				=			int(math.sqrt(_A					)					) + 1
		for j in range(2       , _A					):
			if (i % j) == 0:
				break
		else:
			yield i

def       __UpperCamelCase     (     _A     :					int					)     ->   Generator[int, None, None]:




	"""simple docstring"""





	lowerCAmelCase       :     Generator   				=			(i for i in range(1       , (max_n + 1)       , 2					))
	# It's useless to test even numbers as they will not be prime
	if max_n > 2:
		yield 2  # Because 2 will not be tested, it's necessary to yield it now
	for i in (n for n in numbers if n > 1):
		lowerCAmelCase       :     int   				=			int(math.sqrt(_A					)					) + 1
		for j in range(3       , _A       , 2					):
			# As we removed the even numbers, we don't need them now
			if (i % j) == 0:
				break
		else:
			yield i

def       __UpperCamelCase     (     )     ->   Dict:




	"""simple docstring"""





	from timeit import timeit

	lowerCAmelCase       :     Tuple   				=			'from __main__ import slow_primes, primes, fast_primes'
	print(timeit('slow_primes(1_000_000_000_000)'       , setup=_A       , number=1_00_00_00					)					)
	print(timeit('primes(1_000_000_000_000)'       , setup=_A       , number=1_00_00_00					)					)
	print(timeit('fast_primes(1_000_000_000_000)'       , setup=_A       , number=1_00_00_00					)					)


if __name__ == "__main__":
				_lowerCAmelCase	:		Optional[Any]		  =						int(input('Calculate primes up to:\n>> ').strip())
				for ret in primes(number):
								print(ret)
				benchmark()
