test_logprobs.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. from itertools import cycle
  2. import pytest
  3. from aphrodite import SamplingParams
  4. from .conftest import run_logprob_correctness_test
  5. @pytest.mark.parametrize(
  6. "common_llm_kwargs",
  7. [{
  8. "model_name": "JackFram/llama-68m",
  9. # Skip cuda graph recording for fast test.
  10. "enforce_eager": True,
  11. # Required for spec decode.
  12. "use_v2_block_manager": True,
  13. }])
  14. @pytest.mark.parametrize("per_test_common_llm_kwargs", [{}])
  15. @pytest.mark.parametrize("baseline_llm_kwargs", [{}])
  16. @pytest.mark.parametrize("test_llm_kwargs",
  17. [{
  18. "speculative_model": "JackFram/llama-160m",
  19. "num_speculative_tokens": 3,
  20. "disable_logprobs_during_spec_decoding": False,
  21. }])
  22. @pytest.mark.parametrize("batch_size", [8])
  23. @pytest.mark.parametrize(
  24. "output_len",
  25. [
  26. # Use smaller output len for fast test.
  27. 7,
  28. ])
  29. @pytest.mark.parametrize("seed", [1])
  30. @pytest.mark.parametrize("logprobs", [1, 6])
  31. def test_logprobs_equality(aphrodite_runner, common_llm_kwargs,
  32. per_test_common_llm_kwargs, baseline_llm_kwargs,
  33. test_llm_kwargs, batch_size: int, output_len: int,
  34. seed: int, logprobs: int):
  35. """Verify output logprobs are equal with and without speculative decoding.
  36. """
  37. run_logprob_correctness_test(aphrodite_runner,
  38. common_llm_kwargs,
  39. per_test_common_llm_kwargs,
  40. baseline_llm_kwargs,
  41. test_llm_kwargs,
  42. batch_size,
  43. output_len,
  44. seed,
  45. temperature=0.0,
  46. logprobs=logprobs)
  47. @pytest.mark.parametrize(
  48. "common_llm_kwargs",
  49. [{
  50. "model_name": "JackFram/llama-68m",
  51. # Skip cuda graph recording for fast test.
  52. "enforce_eager": True,
  53. # Required for spec decode.
  54. "use_v2_block_manager": True
  55. }])
  56. @pytest.mark.parametrize("per_test_common_llm_kwargs", [{}])
  57. @pytest.mark.parametrize("baseline_llm_kwargs", [{}])
  58. @pytest.mark.parametrize("test_llm_kwargs",
  59. [{
  60. "speculative_model": "JackFram/llama-160m",
  61. "num_speculative_tokens": 3,
  62. "disable_logprobs_during_spec_decoding": False,
  63. }, {
  64. "speculative_model": "JackFram/llama-160m",
  65. "num_speculative_tokens": 6,
  66. "disable_logprobs_during_spec_decoding": False,
  67. }])
  68. @pytest.mark.parametrize("batch_size", [8])
  69. @pytest.mark.parametrize(
  70. "output_len",
  71. [
  72. # Use smaller output len for fast test.
  73. 32,
  74. ])
  75. @pytest.mark.parametrize("seed", [1])
  76. @pytest.mark.parametrize("logprobs", [1, 6])
  77. def test_logprobs_different_k(aphrodite_runner, common_llm_kwargs,
  78. per_test_common_llm_kwargs, baseline_llm_kwargs,
  79. test_llm_kwargs, batch_size: int,
  80. output_len: int, seed: int, logprobs: int):
  81. """Veriy logprob greedy equality with different speculation lens.
  82. """
  83. run_logprob_correctness_test(aphrodite_runner,
  84. common_llm_kwargs,
  85. per_test_common_llm_kwargs,
  86. baseline_llm_kwargs,
  87. test_llm_kwargs,
  88. batch_size,
  89. output_len,
  90. seed,
  91. temperature=0.0,
  92. logprobs=logprobs)
  93. @pytest.mark.parametrize(
  94. "common_llm_kwargs",
  95. [{
  96. "model_name": "JackFram/llama-68m",
  97. # Skip cuda graph recording for fast test.
  98. "enforce_eager": True,
  99. # Required for spec decode.
  100. "use_v2_block_manager": True
  101. }])
  102. @pytest.mark.parametrize("per_test_common_llm_kwargs", [{}])
  103. @pytest.mark.parametrize("baseline_llm_kwargs", [{}])
  104. @pytest.mark.parametrize(
  105. "test_llm_kwargs",
  106. [{
  107. "speculative_model": "JackFram/llama-160m",
  108. "num_speculative_tokens": 3,
  109. "disable_logprobs_during_spec_decoding": False,
  110. # Artificially limit the draft model max model len; this forces
  111. # Aphrodite to skip speculation once the sequences grow beyond 32-k
  112. # tokens.
  113. "speculative_max_model_len": 32,
  114. }])
  115. @pytest.mark.parametrize("batch_size", [8])
  116. @pytest.mark.parametrize(
  117. "output_len",
  118. [
  119. # Use smaller output len for fast test.
  120. 32,
  121. ])
  122. @pytest.mark.parametrize("seed", [1])
  123. @pytest.mark.parametrize("logprobs", [1])
  124. def test_logprobs_when_skip_speculation(aphrodite_runner, common_llm_kwargs,
  125. per_test_common_llm_kwargs,
  126. baseline_llm_kwargs, test_llm_kwargs,
  127. batch_size: int, output_len: int,
  128. seed: int, logprobs: int):
  129. """Verify logprobs greedy equality when some sequences skip speculation.
  130. """
  131. run_logprob_correctness_test(aphrodite_runner,
  132. common_llm_kwargs,
  133. per_test_common_llm_kwargs,
  134. baseline_llm_kwargs,
  135. test_llm_kwargs,
  136. batch_size,
  137. output_len,
  138. seed,
  139. temperature=0.0,
  140. logprobs=logprobs)
  141. @pytest.mark.parametrize(
  142. "common_llm_kwargs",
  143. [{
  144. "model_name": "JackFram/llama-68m",
  145. # Skip cuda graph recording for fast test.
  146. "enforce_eager": True,
  147. # Required for spec decode.
  148. "use_v2_block_manager": True
  149. }])
  150. @pytest.mark.parametrize("per_test_common_llm_kwargs", [{}])
  151. @pytest.mark.parametrize("baseline_llm_kwargs", [{}])
  152. @pytest.mark.parametrize("test_llm_kwargs",
  153. [{
  154. "speculative_model": "JackFram/llama-160m",
  155. "num_speculative_tokens": 3,
  156. "disable_logprobs_during_spec_decoding": False,
  157. }])
  158. @pytest.mark.parametrize("batch_size", [1])
  159. @pytest.mark.parametrize(
  160. "output_len",
  161. [
  162. # Use smaller output len for fast test.
  163. 32,
  164. ])
  165. @pytest.mark.parametrize("seed", [1])
  166. @pytest.mark.parametrize("logprobs", [6])
  167. def test_logprobs_temp_1(aphrodite_runner, common_llm_kwargs,
  168. per_test_common_llm_kwargs, baseline_llm_kwargs,
  169. test_llm_kwargs, batch_size: int, output_len: int,
  170. seed: int, logprobs: int):
  171. """Verify at least one logprob result has num_logprobs+1, which tests the
  172. case where the sampled token is not in top-k logprobs.
  173. Ideally, this test should validate equality with non-spec by getting
  174. logprobs. This is left as future improvement.
  175. """
  176. temperature = 1.0
  177. prompts = [
  178. "Hello, my name is",
  179. "The president of the United States is",
  180. "The capital of France is",
  181. "The future of AI is",
  182. "San Francisco is know for its",
  183. "Facebook was created in 2004 by",
  184. "Curious George is a",
  185. "Python 3.11 brings improvements to its",
  186. ]
  187. prompts = [prompt for prompt, _ in zip(cycle(prompts), range(batch_size))]
  188. sampling_params = SamplingParams(
  189. max_tokens=output_len,
  190. ignore_eos=True,
  191. temperature=temperature,
  192. logprobs=logprobs,
  193. )
  194. sd_args = {
  195. **common_llm_kwargs,
  196. **per_test_common_llm_kwargs,
  197. **test_llm_kwargs,
  198. }
  199. with aphrodite_runner(**sd_args) as aphrodite_model:
  200. sd_outputs = aphrodite_model.generate_w_logprobs(prompts,
  201. sampling_params)
  202. num_returned_logprobs = [
  203. len(seq_logprobs) for seq_logprobs in sd_outputs[-1]
  204. ]
  205. # Assert one of the returned logprobs has > num_logprobs (indicating the
  206. # sampled token is not in top-k).
  207. assert any(
  208. [num_returned > logprobs for num_returned in num_returned_logprobs])
  209. @pytest.mark.parametrize(
  210. "common_llm_kwargs",
  211. [{
  212. "model_name": "JackFram/llama-160m",
  213. # Skip cuda graph recording for fast test.
  214. "enforce_eager": True,
  215. # Required for spec decode.
  216. "use_v2_block_manager": True,
  217. }])
  218. @pytest.mark.parametrize("per_test_common_llm_kwargs", [{}])
  219. @pytest.mark.parametrize("baseline_llm_kwargs", [{}])
  220. @pytest.mark.parametrize("test_llm_kwargs",
  221. [{
  222. "speculative_model": "JackFram/llama-68m",
  223. "num_speculative_tokens": 3,
  224. "disable_logprobs_during_spec_decoding": True,
  225. }])
  226. @pytest.mark.parametrize("seed", [1])
  227. @pytest.mark.parametrize("batch_size", [4])
  228. @pytest.mark.parametrize(
  229. "output_len",
  230. [
  231. # Use smaller output len for fast test.
  232. 32,
  233. ])
  234. @pytest.mark.parametrize("logprobs", [0])
  235. def test_logprobs_disabled(aphrodite_runner, common_llm_kwargs,
  236. per_test_common_llm_kwargs, baseline_llm_kwargs,
  237. test_llm_kwargs, batch_size: int, output_len: int,
  238. seed: int, logprobs: int):
  239. """Check the behavior when logprobs are disabled.
  240. Token choices should match with the base model.
  241. """
  242. run_logprob_correctness_test(aphrodite_runner,
  243. common_llm_kwargs,
  244. per_test_common_llm_kwargs,
  245. baseline_llm_kwargs,
  246. test_llm_kwargs,
  247. batch_size,
  248. output_len,
  249. seed,
  250. temperature=0.0,
  251. logprobs=logprobs)