test_generate.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import weakref
  2. from typing import List
  3. import pytest
  4. from aphrodite import LLM, RequestOutput, SamplingParams
  5. from ...conftest import cleanup
  6. MODEL_NAME = "facebook/opt-125m"
  7. PROMPTS = [
  8. "Hello, my name is",
  9. "The president of the United States is",
  10. "The capital of France is",
  11. "The future of AI is",
  12. ]
  13. TOKEN_IDS = [
  14. [0],
  15. [0, 1],
  16. [0, 2, 1],
  17. [0, 3, 1, 2],
  18. ]
  19. @pytest.fixture(scope="module")
  20. def llm():
  21. # pytest caches the fixture so we use weakref.proxy to
  22. # enable garbage collection
  23. llm = LLM(model=MODEL_NAME,
  24. max_num_batched_tokens=4096,
  25. tensor_parallel_size=1,
  26. gpu_memory_utilization=0.10,
  27. enforce_eager=True)
  28. with llm.deprecate_legacy_api():
  29. yield weakref.proxy(llm)
  30. del llm
  31. cleanup()
  32. def assert_outputs_equal(o1: List[RequestOutput], o2: List[RequestOutput]):
  33. assert [o.outputs for o in o1] == [o.outputs for o in o2]
  34. @pytest.mark.skip_global_cleanup
  35. @pytest.mark.parametrize('prompt', PROMPTS)
  36. def test_v1_v2_api_consistency_single_prompt_string(llm: LLM, prompt):
  37. sampling_params = SamplingParams(temperature=0.0, top_p=1.0)
  38. with pytest.warns(DeprecationWarning, match="'prompts'"):
  39. v1_output = llm.generate(prompts=prompt,
  40. sampling_params=sampling_params)
  41. v2_output = llm.generate(prompt, sampling_params=sampling_params)
  42. assert_outputs_equal(v1_output, v2_output)
  43. v2_output = llm.generate({"prompt": prompt},
  44. sampling_params=sampling_params)
  45. assert_outputs_equal(v1_output, v2_output)
  46. @pytest.mark.skip_global_cleanup
  47. @pytest.mark.parametrize('prompt_token_ids', TOKEN_IDS)
  48. def test_v1_v2_api_consistency_single_prompt_tokens(llm: LLM,
  49. prompt_token_ids):
  50. sampling_params = SamplingParams(temperature=0.0, top_p=1.0)
  51. with pytest.warns(DeprecationWarning, match="'prompt_token_ids'"):
  52. v1_output = llm.generate(prompt_token_ids=prompt_token_ids,
  53. sampling_params=sampling_params)
  54. v2_output = llm.generate({"prompt_token_ids": prompt_token_ids},
  55. sampling_params=sampling_params)
  56. assert_outputs_equal(v1_output, v2_output)
  57. @pytest.mark.skip_global_cleanup
  58. def test_v1_v2_api_consistency_multi_prompt_string(llm: LLM):
  59. sampling_params = SamplingParams(temperature=0.0, top_p=1.0)
  60. with pytest.warns(DeprecationWarning, match="'prompts'"):
  61. v1_output = llm.generate(prompts=PROMPTS,
  62. sampling_params=sampling_params)
  63. v2_output = llm.generate(PROMPTS, sampling_params=sampling_params)
  64. assert_outputs_equal(v1_output, v2_output)
  65. v2_output = llm.generate(
  66. [{
  67. "prompt": p
  68. } for p in PROMPTS],
  69. sampling_params=sampling_params,
  70. )
  71. assert_outputs_equal(v1_output, v2_output)
  72. @pytest.mark.skip_global_cleanup
  73. def test_v1_v2_api_consistency_multi_prompt_tokens(llm: LLM):
  74. sampling_params = SamplingParams(temperature=0.0, top_p=1.0)
  75. with pytest.warns(DeprecationWarning, match="'prompt_token_ids'"):
  76. v1_output = llm.generate(prompt_token_ids=TOKEN_IDS,
  77. sampling_params=sampling_params)
  78. v2_output = llm.generate(
  79. [{
  80. "prompt_token_ids": p
  81. } for p in TOKEN_IDS],
  82. sampling_params=sampling_params,
  83. )
  84. assert_outputs_equal(v1_output, v2_output)
  85. @pytest.mark.skip_global_cleanup
  86. def test_multiple_sampling_params(llm: LLM):
  87. sampling_params = [
  88. SamplingParams(temperature=0.01, top_p=0.95),
  89. SamplingParams(temperature=0.3, top_p=0.95),
  90. SamplingParams(temperature=0.7, top_p=0.95),
  91. SamplingParams(temperature=0.99, top_p=0.95),
  92. ]
  93. # Multiple SamplingParams should be matched with each prompt
  94. outputs = llm.generate(PROMPTS, sampling_params=sampling_params)
  95. assert len(PROMPTS) == len(outputs)
  96. # Exception raised, if the size of params does not match the size of prompts
  97. with pytest.raises(ValueError):
  98. outputs = llm.generate(PROMPTS, sampling_params=sampling_params[:3])
  99. # Single SamplingParams should be applied to every prompt
  100. single_sampling_params = SamplingParams(temperature=0.3, top_p=0.95)
  101. outputs = llm.generate(PROMPTS, sampling_params=single_sampling_params)
  102. assert len(PROMPTS) == len(outputs)
  103. # sampling_params is None, default params should be applied
  104. outputs = llm.generate(PROMPTS, sampling_params=None)
  105. assert len(PROMPTS) == len(outputs)
  106. def test_chat():
  107. llm = LLM(model="meta-llama/Meta-Llama-3-8B-Instruct")
  108. prompt1 = "Explain the concept of entropy."
  109. messages = [
  110. {
  111. "role": "system",
  112. "content": "You are a helpful assistant"
  113. },
  114. {
  115. "role": "user",
  116. "content": prompt1
  117. },
  118. ]
  119. outputs = llm.chat(messages)
  120. assert len(outputs) == 1