code: 
  
  
  
  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
  
  
  __UpperCAmelCase      = {
      '''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'''},
  }
  
  __UpperCAmelCase      = [
      '''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			(     )			->  str:
     UpperCamelCase					:						Tuple					=			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'			)
  
     UpperCamelCase					:						Optional[int]					=			parser.parse_args()
  
     # Sanitize args
     if args.model_name in DEFAULT_MODELS:
        UpperCamelCase					:						int					=			DEFAULT_MODELS[args.model_name]
        UpperCamelCase					:						Dict					=			defaults['model']
        if args.tokenizer_name is None:
           UpperCamelCase					:						List[Any]					=			defaults.get('tokenizer' ,  args.model_name			)
        if args.is_causal is None:
           UpperCamelCase					:						List[Any]					=			defaults['is_causal']
        if args.model_revision is None:
           UpperCamelCase					:						Dict					=			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					:						Union[str, Any]					=			args.model_name
     if args.model_revision is None:
        UpperCamelCase					:						Optional[int]					=			'main'
  
     return args
  def 	UpperCamelCase			(     )			->  List[str]:
     transformers.utils.logging.set_verbosity_error()
     UpperCamelCase					:						Optional[Any]					=			parse_args()
  
     if args.torch_dtype is None:
        UpperCamelCase					:						List[Any]					=			AutoConfig.from_pretrained(args.model_name			)
        UpperCamelCase					:						str					=			getattr(snake_case__ ,  'torch_dtype' ,  torch.floataa			)
     else:
        UpperCamelCase					:						Dict					=			getattr(snake_case__ ,  args.torch_dtype			)
     UpperCamelCase					:						Optional[int]					=			AutoModelForCausalLM if args.is_causal else AutoModelForSeqaSeqLM
     UpperCamelCase					:						List[Any]					=			{
         'torch_dtype': torch_dtype,
         'revision': args.model_revision,
     }
     if args.disk_offload:
        UpperCamelCase					:						str					=			'tmp_offload'
        UpperCamelCase					:						Any					=			True
  
     UpperCamelCase					:						Optional[int]					=			start_measure()
     UpperCamelCase					:						str					=			model_cls.from_pretrained(args.model_name ,  device_map='auto' ,  **snake_case__			)
     UpperCamelCase					:						str					=			end_measure(snake_case__			)
     log_measures(snake_case__ ,  'Model loading'			)
  
     UpperCamelCase					:						Union[str, Any]					=			compute_module_sizes(snake_case__			)
     UpperCamelCase					:						List[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]
     UpperCamelCase					:						Dict					=			'\n'.join([F"""- {device}: {size // 2**20}MiB""" for device, size in device_size.items()]			)
     print(F"""\nTheoretical use:\n{message}"""			)
  
     UpperCamelCase					:						Dict					=			AutoTokenizer.from_pretrained(args.tokenizer_name			)
  
     UpperCamelCase					:						List[Any]					=			start_measure()
     UpperCamelCase					:						List[str]					=			[]
     UpperCamelCase					:						int					=			[]
     UpperCamelCase					:						Optional[Any]					=			[]
     for prompt in PROMPTS:
        UpperCamelCase					:						Tuple					=			tokenizer(snake_case__ ,  return_tensors='pt'			).to(0			)
        UpperCamelCase					:						Optional[int]					=			inputs['input_ids'][0].tolist()
        UpperCamelCase					:						List[Any]					=			time.time()
        UpperCamelCase					:						Optional[int]					=			model.generate(inputs['input_ids']			)
        UpperCamelCase					:						Tuple					=			time.time()
        UpperCamelCase					:						Union[str, Any]					=			outputs[0].tolist()
        UpperCamelCase					:						int					=			len(snake_case__			) if outputs[: len(snake_case__			)] != tokens else len(snake_case__			) - len(snake_case__			)
        UpperCamelCase					:						List[Any]					=			after_generate - before_generate
  
        UpperCamelCase					:						Optional[int]					=			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"""			)
  
     UpperCamelCase					:						Optional[int]					=			end_measure(snake_case__			)
     log_measures(snake_case__ ,  'Model generation'			)
  
     UpperCamelCase					:						Dict					=			[gen / tok for gen, tok in zip(snake_case__ ,  snake_case__			)]
     UpperCamelCase					:						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"""			)
     UpperCamelCase					:						Optional[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()
  
  
  
  
style_context: 
  
  
  
  import unittest
  
  import numpy as np
  
  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 ViltImageProcessor
  
  
  
  class        lowerCAmelCase_	(						unittest.TestCase							):
         def __init__(    self,    SCREAMING_SNAKE_CASE_,    SCREAMING_SNAKE_CASE_=7,    SCREAMING_SNAKE_CASE_=3,    SCREAMING_SNAKE_CASE_=18,    SCREAMING_SNAKE_CASE_=30,    SCREAMING_SNAKE_CASE_=400,    SCREAMING_SNAKE_CASE_=True,    SCREAMING_SNAKE_CASE_=None,    SCREAMING_SNAKE_CASE_=2,    SCREAMING_SNAKE_CASE_=True,    SCREAMING_SNAKE_CASE_=[0.5, 0.5, 0.5],    SCREAMING_SNAKE_CASE_=[0.5, 0.5, 0.5],    )  ->	List[Any]:
            UpperCamelCase					:						Optional[int]					=			size if size is not None else {'shortest_edge': 30}
            UpperCamelCase					:						Dict					=			parent
            UpperCamelCase					:						str					=			batch_size
            UpperCamelCase					:						str					=			num_channels
            UpperCamelCase					:						Optional[int]					=			image_size
            UpperCamelCase					:						List[Any]					=			min_resolution
            UpperCamelCase					:						Tuple					=			max_resolution
            UpperCamelCase					:						Tuple					=			do_resize
            UpperCamelCase					:						Any					=			size
            UpperCamelCase					:						int					=			size_divisor
            UpperCamelCase					:						str					=			do_normalize
            UpperCamelCase					:						Optional[int]					=			image_mean
            UpperCamelCase					:						List[str]					=			image_std
         def       snake_case_ (    self )  ->	List[Any]:
            return {
                "image_mean": self.image_mean,
                "image_std": self.image_std,
                "do_normalize": self.do_normalize,
                "do_resize": self.do_resize,
                "size": self.size,
                "size_divisor": self.size_divisor,
            }
  
  
  
  
  
  
         def       snake_case_ (    self,    SCREAMING_SNAKE_CASE_,    SCREAMING_SNAKE_CASE_=False )  ->	Union[str, Any]:
            if not batched:
               UpperCamelCase					:						Optional[Any]					=			self.size['shortest_edge']
               UpperCamelCase					:						int					=			image_inputs[0]
               if isinstance(SCREAMING_SNAKE_CASE_,    Image.Image ):
                  UpperCamelCase			,  UpperCamelCase					:						List[Any]					=			image.size
               else:
                  UpperCamelCase			,  UpperCamelCase					:						List[Any]					=			image.shape[1], image.shape[2]
               UpperCamelCase					:						Any					=			size / min(SCREAMING_SNAKE_CASE_,    SCREAMING_SNAKE_CASE_ )
               if h < w:
                  UpperCamelCase			,  UpperCamelCase					:						Optional[int]					=			size, scale * w
               else:
                  UpperCamelCase			,  UpperCamelCase					:						List[str]					=			scale * h, size
  
               UpperCamelCase					:						int					=			int((1333 / 800) * size )
               if max(SCREAMING_SNAKE_CASE_,    SCREAMING_SNAKE_CASE_ ) > max_size:
                  UpperCamelCase					:						Any					=			max_size / max(SCREAMING_SNAKE_CASE_,    SCREAMING_SNAKE_CASE_ )
                  UpperCamelCase					:						List[Any]					=			newh * scale
                  UpperCamelCase					:						Optional[int]					=			neww * scale
  
               UpperCamelCase			,  UpperCamelCase					:						List[Any]					=			int(newh + 0.5 ), int(neww + 0.5 )
               UpperCamelCase			,  UpperCamelCase					:						int					=			(
                   newh // self.size_divisor * self.size_divisor,
                   neww // self.size_divisor * self.size_divisor,
               )
  
            else:
               UpperCamelCase					:						Dict					=			[]
               for image in image_inputs:
                  UpperCamelCase			,  UpperCamelCase					:						Union[str, Any]					=			self.get_expected_values([image] )
                  expected_values.append((expected_height, expected_width) )
               UpperCamelCase					:						List[str]					=			max(SCREAMING_SNAKE_CASE_,    key=lambda SCREAMING_SNAKE_CASE_ : item[0] )[0]
               UpperCamelCase					:						List[str]					=			max(SCREAMING_SNAKE_CASE_,    key=lambda SCREAMING_SNAKE_CASE_ : item[1] )[1]
  
            return expected_height, expected_width
  
  
  
  
  
  
  @require_torch
  @require_vision
  class        lowerCAmelCase_	(						a__ ,					unittest.TestCase							):
         UpperCAmelCase__       :      List[Any]       =						ViltImageProcessor if is_vision_available() else None
         def       snake_case_ (    self )  ->	Union[str, Any]:
            UpperCamelCase					:						Dict					=			ViltImageProcessingTester(self )
         @property
         def       snake_case_ (    self )  ->	Dict:
            return self.image_processor_tester.prepare_image_processor_dict()
         def       snake_case_ (    self )  ->	Union[str, Any]:
            UpperCamelCase					:						List[str]					=			self.image_processing_class(**self.image_processor_dict )
            self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_,    'image_mean' ) )
            self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_,    'image_std' ) )
            self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_,    'do_normalize' ) )
            self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_,    'do_resize' ) )
            self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_,    'size' ) )
            self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_,    'size_divisor' ) )
         def       snake_case_ (    self )  ->	List[Any]:
            UpperCamelCase					:						Union[str, Any]					=			self.image_processing_class.from_dict(self.image_processor_dict )
            self.assertEqual(image_processor.size,    {'shortest_edge': 30} )
  
            UpperCamelCase					:						Union[str, Any]					=			self.image_processing_class.from_dict(self.image_processor_dict,    size=42 )
            self.assertEqual(image_processor.size,    {'shortest_edge': 42} )
         def       snake_case_ (    self )  ->	Dict:
            pass
         def       snake_case_ (    self )  ->	str:
            # Initialize image_processing
            UpperCamelCase					:						int					=			self.image_processing_class(**self.image_processor_dict )
            # create random PIL images
            UpperCamelCase					:						Any					=			prepare_image_inputs(self.image_processor_tester,    equal_resolution=SCREAMING_SNAKE_CASE_ )
            for image in image_inputs:
               self.assertIsInstance(SCREAMING_SNAKE_CASE_,    Image.Image )
  
            # Test not batched input
            UpperCamelCase					:						int					=			image_processing(image_inputs[0],    return_tensors='pt' ).pixel_values
  
            UpperCamelCase			,  UpperCamelCase					:						Dict					=			self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_ )
            self.assertEqual(
                encoded_images.shape,    (1, self.image_processor_tester.num_channels, expected_height, expected_width),    )
  
            # Test batched
            UpperCamelCase					:						str					=			image_processing(SCREAMING_SNAKE_CASE_,    return_tensors='pt' ).pixel_values
  
            UpperCamelCase			,  UpperCamelCase					:						Tuple					=			self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_,    batched=SCREAMING_SNAKE_CASE_ )
            self.assertEqual(
                encoded_images.shape,    (
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    expected_height,
                    expected_width,
                ),    )
         def       snake_case_ (    self )  ->	List[str]:
            # Initialize image_processing
            UpperCamelCase					:						Optional[int]					=			self.image_processing_class(**self.image_processor_dict )
            # create random numpy tensors
            UpperCamelCase					:						Optional[Any]					=			prepare_image_inputs(self.image_processor_tester,    equal_resolution=SCREAMING_SNAKE_CASE_,    numpify=SCREAMING_SNAKE_CASE_ )
            for image in image_inputs:
               self.assertIsInstance(SCREAMING_SNAKE_CASE_,    np.ndarray )
  
            # Test not batched input
            UpperCamelCase					:						Optional[Any]					=			image_processing(image_inputs[0],    return_tensors='pt' ).pixel_values
  
            UpperCamelCase			,  UpperCamelCase					:						str					=			self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_ )
            self.assertEqual(
                encoded_images.shape,    (1, self.image_processor_tester.num_channels, expected_height, expected_width),    )
  
            # Test batched
            UpperCamelCase					:						Dict					=			image_processing(SCREAMING_SNAKE_CASE_,    return_tensors='pt' ).pixel_values
  
            UpperCamelCase			,  UpperCamelCase					:						Optional[Any]					=			self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_,    batched=SCREAMING_SNAKE_CASE_ )
            self.assertEqual(
                encoded_images.shape,    (
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    expected_height,
                    expected_width,
                ),    )
  
  
  
  
  
  
         def       snake_case_ (    self )  ->	Optional[Any]:
            # Initialize image_processing
            UpperCamelCase					:						Optional[int]					=			self.image_processing_class(**self.image_processor_dict )
            # create random PyTorch tensors
            UpperCamelCase					:						Dict					=			prepare_image_inputs(self.image_processor_tester,    equal_resolution=SCREAMING_SNAKE_CASE_,    torchify=SCREAMING_SNAKE_CASE_ )
            for image in image_inputs:
               self.assertIsInstance(SCREAMING_SNAKE_CASE_,    torch.Tensor )
  
            # Test not batched input
            UpperCamelCase					:						List[str]					=			image_processing(image_inputs[0],    return_tensors='pt' ).pixel_values
  
            UpperCamelCase			,  UpperCamelCase					:						Union[str, Any]					=			self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_ )
            self.assertEqual(
                encoded_images.shape,    (1, self.image_processor_tester.num_channels, expected_height, expected_width),    )
  
            # Test batched
            UpperCamelCase					:						List[Any]					=			image_processing(SCREAMING_SNAKE_CASE_,    return_tensors='pt' ).pixel_values
  
            UpperCamelCase			,  UpperCamelCase					:						Dict					=			self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_,    batched=SCREAMING_SNAKE_CASE_ )
            self.assertEqual(
                encoded_images.shape,    (
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    expected_height,
                    expected_width,
                ),    )
  
  
  
  