cached_prefix_inference.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from time import time
  2. from aphrodite import LLM, SamplingParams
  3. # Common prefix.
  4. prefix = (
  5. "You are an expert school principal, skilled in effectively managing "
  6. "faculty and staff. Draft 10-15 questions for a potential first grade "
  7. "Head Teacher for my K-12, all-girls', independent school that emphasizes "
  8. "community, joyful discovery, and life-long learning. The candidate is "
  9. "coming in for a first-round panel interview for a 8th grade Math "
  10. "teaching role. They have 5 years of previous teaching experience "
  11. "as an assistant teacher at a co-ed, public school with experience "
  12. "in middle school math teaching. Based on these information, fulfill "
  13. "the following paragraph: ")
  14. # Sample prompts.
  15. prompts = [
  16. "Once upon a time,",
  17. "In a galaxy far, far away,",
  18. "The quick brown fox jumps over the lazy dog.",
  19. "The meaning of life is",
  20. ]
  21. generating_prompts = [prefix + prompt for prompt in prompts]
  22. # Create a sampling params object.
  23. sampling_params = SamplingParams(temperature=0.0)
  24. # Create an LLM.
  25. regular_llm = LLM(model="NousResearch/Meta-Llama-3.1-8B-Instruct")
  26. prefix_cached_llm = LLM(model="NousResearch/Meta-Llama-3.1-8B-Instruct",
  27. enable_prefix_caching=True,
  28. gpu_memory_utilization=0.4)
  29. print("Results without `enable_prefix_caching`")
  30. # Generate texts from the prompts. The output is a list of RequestOutput objects
  31. # that contain the prompt, generated text, and other information.
  32. start_time_regular = time()
  33. outputs = regular_llm.generate(generating_prompts, sampling_params)
  34. duration_regular = time() - start_time_regular
  35. regular_generated_texts = []
  36. # Print the outputs.
  37. for output in outputs:
  38. prompt = output.prompt
  39. generated_text = output.outputs[0].text
  40. regular_generated_texts.append(generated_text)
  41. print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
  42. print("-" * 80)
  43. # Warmup so that the shared prompt's KV cache is computed.
  44. prefix_cached_llm.generate(generating_prompts[0], sampling_params)
  45. # Generate with prefix caching.
  46. start_time_cached = time()
  47. outputs = prefix_cached_llm.generate(generating_prompts, sampling_params)
  48. duration_cached = time() - start_time_cached
  49. print("Results with `enable_prefix_caching`")
  50. cached_generated_texts = []
  51. # Print the outputs. You should see the same outputs as before.
  52. for output in outputs:
  53. prompt = output.prompt
  54. generated_text = output.outputs[0].text
  55. cached_generated_texts.append(generated_text)
  56. print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
  57. print("-" * 80)
  58. # Compare the results and display the speedup
  59. generated_same = all([
  60. regular_generated_texts[i] == cached_generated_texts[i]
  61. for i in range(len(prompts))
  62. ])
  63. print(f"Generated answers are the same: {generated_same}")
  64. speedup = round(duration_regular / duration_cached, 2)
  65. print(f"Speed up of cached generation compared to the regular is: {speedup}")