
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_      :					Union[str, 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_      :					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    (  ):



	'''simple docstring'''



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

	UpperCAmelCase:					List[Any] 						=     parser.parse_args()

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

	return args



def      __UpperCAmelCase    (  ):



	'''simple docstring'''



	transformers.utils.logging.set_verbosity_error()
	UpperCAmelCase:					Optional[Any] 						=     parse_args()

	if args.torch_dtype is None:
		UpperCAmelCase:					Union[str, Any] 						=     AutoConfig.from_pretrained(args.model_name				)
		UpperCAmelCase:					int 						=     getattr(__lowerCAmelCase				,					"torch_dtype"				,					torch.floataa				)
	else:
		UpperCAmelCase:					int 						=     getattr(__lowerCAmelCase				,					args.torch_dtype				)
	UpperCAmelCase:					str 						=     AutoModelForCausalLM if args.is_causal else AutoModelForSeqaSeqLM
	UpperCAmelCase:					List[str] 						=     {
	    "torch_dtype": torch_dtype,
	    "revision": args.model_revision,
	}
	if args.disk_offload:
		UpperCAmelCase:					Dict 						=     "tmp_offload"
		UpperCAmelCase:					Tuple 						=     True

	UpperCAmelCase:					Optional[Any] 						=     start_measure()
	UpperCAmelCase:					Any 						=     model_cls.from_pretrained(args.model_name				,					device_map="auto"				,					**__lowerCAmelCase				)
	UpperCAmelCase:					Dict 						=     end_measure(__lowerCAmelCase				)
	log_measures(__lowerCAmelCase				,					"Model loading"				)

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

	UpperCAmelCase:					Union[str, Any] 						=     AutoTokenizer.from_pretrained(args.tokenizer_name				)

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

		UpperCAmelCase:					Optional[int] 						=     tokenizer.decode(__lowerCAmelCase				,					skip_special_tokens=__lowerCAmelCase				)
		texts_outs.append(__lowerCAmelCase				)
		generation_times.append(__lowerCAmelCase				)
		gen_tokens.append(__lowerCAmelCase				)
		print(F'Prompt: {prompt}\nGeneration {text_out}\nIn {generation_time:.2f}s for {num_gen_tokens} tokens\n'				)

	UpperCAmelCase:					Optional[int] 						=     end_measure(__lowerCAmelCase				)
	log_measures(__lowerCAmelCase				,					"Model generation"				)

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


if __name__ == "__main__":
	main()








import json
import os
import unittest

from transformers.models.gpt_neox_japanese.tokenization_gpt_neox_japanese import (
    VOCAB_FILES_NAMES,
    GPTNeoXJapaneseTokenizer,
)
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin





@require_tokenizers
class __lowerCamelCase					( snake_case__	,			unittest.TestCase):





	"""simple docstring"""






	UpperCamelCase__								= GPTNeoXJapaneseTokenizer
	UpperCamelCase__								= False
	UpperCamelCase__								= {"do_clean_text": False, "add_prefix_space": False}

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							super().setUp()

							_UpperCAmelCase							   =     [
							    'こん',
							    'こんに',
							    'にちは',
							    'ばんは',
							    '世界,㔺界',
							    '、',
							    '。',
							    '<BR>',
							    '<SP>',
							    '<TAB>',
							    '<URL>',
							    '<EMAIL>',
							    '<TEL>',
							    '<DATE>',
							    '<PRICE>',
							    '<BLOCK>',
							    '<KIGOU>',
							    '<U2000U2BFF>',
							    '<|emoji1|>',
							    '<unk>',
							    '<|startoftext|>',
							    '<|endoftext|>',
							]
							_UpperCAmelCase							   =     {'emoji': {'\ud83d\ude00': '<|emoji1|>'}, 'emoji_inv': {'<|emoji1|>': '\ud83d\ude00'}}  # 😀
							_UpperCAmelCase							   =     {'unk_token': '<unk>'}

							_UpperCAmelCase							   =     os.path.join(self.tmpdirname			,       VOCAB_FILES_NAMES['vocab_file']       )
							_UpperCAmelCase							   =     os.path.join(self.tmpdirname			,       VOCAB_FILES_NAMES['emoji_file']       )
							with open(self.vocab_file			,       'w'			,       encoding='utf-8'       ) as vocab_writer:
													vocab_writer.write(''.join([x + '\n' for x in vocab_tokens]       )       )
							with open(self.emoji_file			,       'w'       ) as emoji_writer:
													emoji_writer.write(json.dumps(UpperCAmelCase       )       )

	def 			UpperCamelCase       (				self			,       **UpperCAmelCase       ):
							"""simple docstring"""







							kwargs.update(self.special_tokens_map       )
							return GPTNeoXJapaneseTokenizer.from_pretrained(self.tmpdirname			,       **UpperCAmelCase       )

	def 			UpperCamelCase       (				self			,       UpperCAmelCase       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     'こんにちは、世界。 \nこんばんは、㔺界。😀'
							_UpperCAmelCase							   =     'こんにちは、世界。 \nこんばんは、世界。😀'
							return input_text, output_text

	def 			UpperCamelCase       (				self			,       UpperCAmelCase       ):
							"""simple docstring"""







							_UpperCAmelCase   ,					_UpperCAmelCase							   =     self.get_input_output_texts(UpperCAmelCase       )
							_UpperCAmelCase							   =     tokenizer.encode(UpperCAmelCase			,       add_special_tokens=UpperCAmelCase       )
							_UpperCAmelCase							   =     tokenizer.decode(UpperCAmelCase			,       clean_up_tokenization_spaces=UpperCAmelCase       )
							return text, ids

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							pass  # TODO add if relevant

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							pass  # TODO add if relevant

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							pass  # TODO add if relevant

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     self.get_tokenizer()

							# Testing tokenization
							_UpperCAmelCase							   =     'こんにちは、世界。　こんばんは、㔺界。'
							_UpperCAmelCase							   =     ['こん', 'にちは', '、', '世界', '。', '<SP>', 'こん', 'ばんは', '、', '㔺界', '。']
							_UpperCAmelCase							   =     tokenizer.tokenize(UpperCAmelCase       )
							self.assertListEqual(UpperCAmelCase			,       UpperCAmelCase       )

							# Testing conversion to ids without special tokens
							_UpperCAmelCase							   =     [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6]
							_UpperCAmelCase							   =     tokenizer.convert_tokens_to_ids(UpperCAmelCase       )
							self.assertListEqual(UpperCAmelCase			,       UpperCAmelCase       )

							# Testing conversion to ids with special tokens
							_UpperCAmelCase							   =     tokens + [tokenizer.unk_token]
							_UpperCAmelCase							   =     [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6, 19]
							_UpperCAmelCase							   =     tokenizer.convert_tokens_to_ids(UpperCAmelCase       )
							self.assertListEqual(UpperCAmelCase			,       UpperCAmelCase       )

	@slow
	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     self.tokenizer_class.from_pretrained('abeja/gpt-neox-japanese-2.7b'       )

							_UpperCAmelCase							   =     tokenizer.encode('ありがとう。'			,       add_special_tokens=UpperCAmelCase       )
							_UpperCAmelCase							   =     tokenizer.encode('どういたしまして。'			,       add_special_tokens=UpperCAmelCase       )

							_UpperCAmelCase							   =     tokenizer.build_inputs_with_special_tokens(UpperCAmelCase       )
							_UpperCAmelCase							   =     tokenizer.build_inputs_with_special_tokens(UpperCAmelCase			,       UpperCAmelCase       )

							assert encoded_sentence == ids_a
							assert encoded_pair == ids_a + ids_a

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							pass

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							pass
