hashing.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import cProfile
  2. import pstats
  3. from aphrodite import LLM, SamplingParams
  4. from aphrodite.common.utils import FlexibleArgumentParser
  5. # A very long prompt, total number of tokens is about 15k.
  6. LONG_PROMPT = ["You are an expert in large language models, aren't you?"
  7. ] * 1000
  8. LONG_PROMPT = ' '.join(LONG_PROMPT)
  9. def main(args):
  10. llm = LLM(
  11. model=args.model,
  12. enforce_eager=True,
  13. enable_prefix_caching=True,
  14. tensor_parallel_size=args.tensor_parallel_size,
  15. use_v2_block_manager=args.use_v2_block_manager,
  16. )
  17. sampling_params = SamplingParams(temperature=0, max_tokens=args.output_len)
  18. profiler = cProfile.Profile()
  19. print("------warm up------")
  20. for i in range(3):
  21. output = llm.generate(LONG_PROMPT, sampling_params)
  22. print(output[0].outputs[0].text)
  23. print("------start generating------")
  24. for i in range(3):
  25. profiler.runctx('llm.generate(LONG_PROMPT, sampling_params)',
  26. globals(), locals())
  27. # analyze the runtime of hashing function
  28. stats = pstats.Stats(profiler)
  29. stats.sort_stats('cumulative')
  30. total_time = 0
  31. total_calls = 0
  32. for func in stats.stats:
  33. if 'hash_of_block' in func[2]:
  34. total_time = stats.stats[func][3]
  35. total_calls = stats.stats[func][0]
  36. percentage = (total_time / stats.total_tt) * 100
  37. print(f"Hashing took {total_time:.2f} seconds,"
  38. f"{percentage:.2f}% of the total runtime.")
  39. if __name__ == "__main__":
  40. parser = FlexibleArgumentParser(
  41. description='Benchmark the performance of hashing function in'
  42. 'automatic prefix caching.')
  43. parser.add_argument('--model', type=str, default='lmsys/longchat-7b-16k')
  44. parser.add_argument('--tensor-parallel-size', '-tp', type=int, default=1)
  45. parser.add_argument('--output-len', type=int, default=10)
  46. parser.add_argument('--enable-prefix-caching',
  47. action='store_true',
  48. help='enable prefix caching')
  49. parser.add_argument('--use-v2-block-manager',
  50. action='store_true',
  51. help='Use BlockSpaceMangerV2')
  52. args = parser.parse_args()
  53. main(args)