code: def timing_cuda(
      model,
      num_batches,
      input_ids,
      masks,
      is_decoder,
      generation_config=None,
  ):
      if is_decoder:
          model.generation_config.eos_token_id = None
  
      torch.cuda.reset_peak_memory_stats(device)
      torch.cuda.synchronize()
  
      # We need NOT call torch.cuda.empty_cache() here as it appears to negate the warmup.
  
      latencies = []
      for _ in tqdm(range(num_batches)):
          start_event = torch.cuda.Event(enable_timing=True)
          end_event = torch.cuda.Event(enable_timing=True)
          torch.cuda.synchronize()
          start_event.record()
  
          if is_decoder:
              _ = model.generate(
                  input_ids,
                  attention_mask=masks,
                  generation_config=generation_config,
              )
          else:
              _ = model(input_ids, masks)
          end_event.record()
          torch.cuda.synchronize()
  
          latency_ms = start_event.elapsed_time(end_event)
          print(
              f"\nLatency per token: {latency_ms / generation_config.min_new_tokens:.3f} ms"
          )
          latencies.append(latency_ms)
  
      max_memory = torch.cuda.max_memory_allocated(device)
  
      return np.mean(latencies), max_memory
  
file_path: optimum/tests/benchmark/benchmark_bettertransformer.py
imports: import argparse
  import numpy
  import pandas
  import torch
  from tqdm import tqdm
  from transformers import AutoModel, AutoModelForCausalLM, AutoModelForSeq2SeqLM, AutoTokenizer, GenerationConfig
  from optimum.bettertransformer import BetterTransformer
  from optimum.exporters import TasksManager
label_desc: Use image node_id 3 for calling a global function with example usage: timing_cuda(model, num_batches, input_ids, masks, is_decoder, generation_config) and returns: np,  max_memory