




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_					:					str       						=			[
    "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		()  -> Dict:







     """simple docstring"""


     UpperCAmelCase_		=				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'''			)

     UpperCAmelCase_		=				parser.parse_args()

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

     return args


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







     """simple docstring"""


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

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

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

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

     UpperCAmelCase_		=				AutoTokenizer.from_pretrained(args.tokenizer_name			)

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

          UpperCAmelCase_		=				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"""			)

     UpperCAmelCase_		=				end_measure(__A			)
     log_measures(__A  ,							'''Model generation'''			)

     UpperCAmelCase_		=				[gen / tok for gen, tok in zip(__A  ,							__A			)]
     UpperCAmelCase_		=				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"""			)
     UpperCAmelCase_		=				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 unittest

import numpy as np
from datasets import load_dataset

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import SegformerImageProcessor







class __snake_case     (     unittest.TestCase						):







   def __init__( self						:		List[Any]   ,		_snake_case						:		List[str]   ,		_snake_case						:		Any=7   ,		_snake_case						:		Any=3   ,		_snake_case						:		str=30   ,		_snake_case						:		str=400   ,		_snake_case						:		Any=True   ,		_snake_case						:		Dict=None   ,		_snake_case						:		List[str]=True   ,		_snake_case						:		Union[str, Any]=[0.5, 0.5, 0.5]   ,		_snake_case						:		Tuple=[0.5, 0.5, 0.5]   ,		_snake_case						:		str=False   ,		):

        """simple docstring"""


        UpperCAmelCase_		=				size if size is not None else {'''height''': 30, '''width''': 30}
        UpperCAmelCase_		=				parent
        UpperCAmelCase_		=				batch_size
        UpperCAmelCase_		=				num_channels
        UpperCAmelCase_		=				min_resolution
        UpperCAmelCase_		=				max_resolution
        UpperCAmelCase_		=				do_resize
        UpperCAmelCase_		=				size
        UpperCAmelCase_		=				do_normalize
        UpperCAmelCase_		=				image_mean
        UpperCAmelCase_		=				image_std
        UpperCAmelCase_		=				do_reduce_labels







   def lowerCamelCase		( self						:		Tuple):

        """simple docstring"""


        return {
            "do_resize": self.do_resize,
            "size": self.size,
            "do_normalize": self.do_normalize,
            "image_mean": self.image_mean,
            "image_std": self.image_std,
            "do_reduce_labels": self.do_reduce_labels,
        }







def   A		()  -> List[str]:







     """simple docstring"""


     UpperCAmelCase_		=				load_dataset('''hf-internal-testing/fixtures_ade20k'''  ,							split='''test'''			)

     UpperCAmelCase_		=				Image.open(dataset[0]['''file''']			)
     UpperCAmelCase_		=				Image.open(dataset[1]['''file''']			)

     return image, map


def   A		()  -> str:







     """simple docstring"""


     UpperCAmelCase_		=				load_dataset('''hf-internal-testing/fixtures_ade20k'''  ,							split='''test'''			)

     UpperCAmelCase_		=				Image.open(dataset[0]['''file''']			)
     UpperCAmelCase_		=				Image.open(dataset[1]['''file''']			)
     UpperCAmelCase_		=				Image.open(dataset[2]['''file''']			)
     UpperCAmelCase_		=				Image.open(dataset[3]['''file''']			)

     return [imagea, imagea], [mapa, mapa]







@require_torch
@require_vision
class __snake_case     (     a					,    unittest.TestCase						):
   UpperCAmelCase__    :       int		=    SegformerImageProcessor if is_vision_available() else None







   def lowerCamelCase		( self						:		Tuple):

        """simple docstring"""


        UpperCAmelCase_		=				SegformerImageProcessingTester(self)







   @property
   def lowerCamelCase		( self						:		int):

        """simple docstring"""


        return self.image_processor_tester.prepare_image_processor_dict()







   def lowerCamelCase		( self						:		List[Any]):

        """simple docstring"""


        UpperCAmelCase_		=				self.image_processing_class(**self.image_processor_dict)
        self.assertTrue(hasattr(_snake_case   ,		'''do_resize'''))
        self.assertTrue(hasattr(_snake_case   ,		'''size'''))
        self.assertTrue(hasattr(_snake_case   ,		'''do_normalize'''))
        self.assertTrue(hasattr(_snake_case   ,		'''image_mean'''))
        self.assertTrue(hasattr(_snake_case   ,		'''image_std'''))
        self.assertTrue(hasattr(_snake_case   ,		'''do_reduce_labels'''))







   def lowerCamelCase		( self						:		Optional[int]):

        """simple docstring"""


        UpperCAmelCase_		=				self.image_processing_class.from_dict(self.image_processor_dict)
        self.assertEqual(image_processor.size   ,		{'''height''': 30, '''width''': 30})
        self.assertEqual(image_processor.do_reduce_labels   ,		_snake_case)

        UpperCAmelCase_		=				self.image_processing_class.from_dict(self.image_processor_dict   ,		size=42   ,		reduce_labels=_snake_case)
        self.assertEqual(image_processor.size   ,		{'''height''': 42, '''width''': 42})
        self.assertEqual(image_processor.do_reduce_labels   ,		_snake_case)







   def lowerCamelCase		( self						:		Dict):

        """simple docstring"""


        pass







   def lowerCamelCase		( self						:		Dict):

        """simple docstring"""


        UpperCAmelCase_		=				self.image_processing_class(**self.image_processor_dict)
        # create random PIL images
        UpperCAmelCase_		=				prepare_image_inputs(self.image_processor_tester   ,		equal_resolution=_snake_case)
        for image in image_inputs:
             self.assertIsInstance(_snake_case   ,		Image.Image)

        # Test not batched input
        UpperCAmelCase_		=				image_processing(image_inputs[0]   ,		return_tensors='''pt''').pixel_values
        self.assertEqual(
            encoded_images.shape   ,		(
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size['''height'''],
                self.image_processor_tester.size['''width'''],
            )   ,		)

        # Test batched
        UpperCAmelCase_		=				image_processing(_snake_case   ,		return_tensors='''pt''').pixel_values
        self.assertEqual(
            encoded_images.shape   ,		(
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size['''height'''],
                self.image_processor_tester.size['''width'''],
            )   ,		)







   def lowerCamelCase		( self						:		Optional[int]):

        """simple docstring"""


        UpperCAmelCase_		=				self.image_processing_class(**self.image_processor_dict)
        # create random numpy tensors
        UpperCAmelCase_		=				prepare_image_inputs(self.image_processor_tester   ,		equal_resolution=_snake_case   ,		numpify=_snake_case)
        for image in image_inputs:
             self.assertIsInstance(_snake_case   ,		np.ndarray)

        # Test not batched input
        UpperCAmelCase_		=				image_processing(image_inputs[0]   ,		return_tensors='''pt''').pixel_values
        self.assertEqual(
            encoded_images.shape   ,		(
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size['''height'''],
                self.image_processor_tester.size['''width'''],
            )   ,		)

        # Test batched
        UpperCAmelCase_		=				image_processing(_snake_case   ,		return_tensors='''pt''').pixel_values
        self.assertEqual(
            encoded_images.shape   ,		(
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size['''height'''],
                self.image_processor_tester.size['''width'''],
            )   ,		)







   def lowerCamelCase		( self						:		Union[str, Any]):

        """simple docstring"""


        UpperCAmelCase_		=				self.image_processing_class(**self.image_processor_dict)
        # create random PyTorch tensors
        UpperCAmelCase_		=				prepare_image_inputs(self.image_processor_tester   ,		equal_resolution=_snake_case   ,		torchify=_snake_case)
        for image in image_inputs:
             self.assertIsInstance(_snake_case   ,		torch.Tensor)

        # Test not batched input
        UpperCAmelCase_		=				image_processing(image_inputs[0]   ,		return_tensors='''pt''').pixel_values
        self.assertEqual(
            encoded_images.shape   ,		(
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size['''height'''],
                self.image_processor_tester.size['''width'''],
            )   ,		)

        # Test batched
        UpperCAmelCase_		=				image_processing(_snake_case   ,		return_tensors='''pt''').pixel_values
        self.assertEqual(
            encoded_images.shape   ,		(
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size['''height'''],
                self.image_processor_tester.size['''width'''],
            )   ,		)







   def lowerCamelCase		( self						:		str):

        """simple docstring"""


        UpperCAmelCase_		=				self.image_processing_class(**self.image_processor_dict)
        # create random PyTorch tensors
        UpperCAmelCase_		=				prepare_image_inputs(self.image_processor_tester   ,		equal_resolution=_snake_case   ,		torchify=_snake_case)
        UpperCAmelCase_		=				[]
        for image in image_inputs:
             self.assertIsInstance(_snake_case   ,		torch.Tensor)
             maps.append(torch.zeros(image.shape[-2:]).long())

        # Test not batched input
        UpperCAmelCase_		=				image_processing(image_inputs[0]   ,		maps[0]   ,		return_tensors='''pt''')
        self.assertEqual(
            encoding['''pixel_values'''].shape   ,		(
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size['''height'''],
                self.image_processor_tester.size['''width'''],
            )   ,		)
        self.assertEqual(
            encoding['''labels'''].shape   ,		(
                1,
                self.image_processor_tester.size['''height'''],
                self.image_processor_tester.size['''width'''],
            )   ,		)
        self.assertEqual(encoding['''labels'''].dtype   ,		torch.long)
        self.assertTrue(encoding['''labels'''].min().item() >= 0)
        self.assertTrue(encoding['''labels'''].max().item() <= 255)

        # Test batched
        UpperCAmelCase_		=				image_processing(_snake_case   ,		_snake_case   ,		return_tensors='''pt''')
        self.assertEqual(
            encoding['''pixel_values'''].shape   ,		(
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size['''height'''],
                self.image_processor_tester.size['''width'''],
            )   ,		)
        self.assertEqual(
            encoding['''labels'''].shape   ,		(
                self.image_processor_tester.batch_size,
                self.image_processor_tester.size['''height'''],
                self.image_processor_tester.size['''width'''],
            )   ,		)
        self.assertEqual(encoding['''labels'''].dtype   ,		torch.long)
        self.assertTrue(encoding['''labels'''].min().item() >= 0)
        self.assertTrue(encoding['''labels'''].max().item() <= 255)

        # Test not batched input (PIL images)
        UpperCAmelCase_	,			UpperCAmelCase_		=				prepare_semantic_single_inputs()

        UpperCAmelCase_		=				image_processing(_snake_case   ,		_snake_case   ,		return_tensors='''pt''')
        self.assertEqual(
            encoding['''pixel_values'''].shape   ,		(
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size['''height'''],
                self.image_processor_tester.size['''width'''],
            )   ,		)
        self.assertEqual(
            encoding['''labels'''].shape   ,		(
                1,
                self.image_processor_tester.size['''height'''],
                self.image_processor_tester.size['''width'''],
            )   ,		)
        self.assertEqual(encoding['''labels'''].dtype   ,		torch.long)
        self.assertTrue(encoding['''labels'''].min().item() >= 0)
        self.assertTrue(encoding['''labels'''].max().item() <= 255)

        # Test batched input (PIL images)
        UpperCAmelCase_	,			UpperCAmelCase_		=				prepare_semantic_batch_inputs()

        UpperCAmelCase_		=				image_processing(_snake_case   ,		_snake_case   ,		return_tensors='''pt''')
        self.assertEqual(
            encoding['''pixel_values'''].shape   ,		(
                2,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size['''height'''],
                self.image_processor_tester.size['''width'''],
            )   ,		)
        self.assertEqual(
            encoding['''labels'''].shape   ,		(
                2,
                self.image_processor_tester.size['''height'''],
                self.image_processor_tester.size['''width'''],
            )   ,		)
        self.assertEqual(encoding['''labels'''].dtype   ,		torch.long)
        self.assertTrue(encoding['''labels'''].min().item() >= 0)
        self.assertTrue(encoding['''labels'''].max().item() <= 255)







   def lowerCamelCase		( self						:		Optional[Any]):

        """simple docstring"""


        UpperCAmelCase_		=				self.image_processing_class(**self.image_processor_dict)

        # ADE20k has 150 classes, and the background is included, so labels should be between 0 and 150
        UpperCAmelCase_	,			UpperCAmelCase_		=				prepare_semantic_single_inputs()
        UpperCAmelCase_		=				image_processing(_snake_case   ,		_snake_case   ,		return_tensors='''pt''')
        self.assertTrue(encoding['''labels'''].min().item() >= 0)
        self.assertTrue(encoding['''labels'''].max().item() <= 150)

        UpperCAmelCase_		=				True
        UpperCAmelCase_		=				image_processing(_snake_case   ,		_snake_case   ,		return_tensors='''pt''')
        self.assertTrue(encoding['''labels'''].min().item() >= 0)
        self.assertTrue(encoding['''labels'''].max().item() <= 255)






