test_medusa_correctness.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. """This docstring details important information on the testing methodology.
  2. Most of the tests rely on "greedy equality", where we expect the output of
  3. speculative decoding on a sequence to exactly match the output of normal non-
  4. speculative decoding.
  5. Since speculative decoding with rejection sampling guarantees that the output
  6. distribution matches the target model's output distribution (up to hardware
  7. numerics, see https://arxiv.org/pdf/2302.01318.pdf), we can expect greedy
  8. equality.
  9. However, we still need to verify below scenario could be passed:
  10. * Batch size 1 greedy equality
  11. * Batch size >1 greedy equality
  12. * Test greedy equality under preemption
  13. * Test greedy equality under various number of speculative tokens.
  14. With those tests, we can say at least, Medusa would not break the
  15. correctess for the target model outputs.
  16. """
  17. import pytest
  18. from .conftest import run_equality_correctness_test
  19. # main model
  20. # lmsys/vicuna-7b-v1.3 was to be used but it's causing
  21. # OOM in CI pipeline, so using a smaller model.
  22. MAIN_MODEL = "JackFram/llama-68m"
  23. # speculative model
  24. SPEC_MODEL = "abhigoyal/vllm-medusa-llama-68m-random"
  25. # max. number of speculative tokens: this corresponds to
  26. # num_heads in the config.json of the speculator model.
  27. MAX_SPEC_TOKENS = 5
  28. # precision
  29. PRECISION = "float32"
  30. @pytest.mark.parametrize(
  31. "common_llm_kwargs",
  32. [{
  33. # Skip cuda graph recording for fast test.
  34. "enforce_eager": True,
  35. # Required for spec decode.
  36. "use_v2_block_manager": True,
  37. # Print spec metrics.
  38. "disable_log_stats": False,
  39. # Precision
  40. "dtype": PRECISION,
  41. # Main model
  42. "model_name": MAIN_MODEL,
  43. }])
  44. @pytest.mark.parametrize("per_test_common_llm_kwargs", [{}])
  45. @pytest.mark.parametrize("baseline_llm_kwargs", [{}])
  46. @pytest.mark.parametrize("test_llm_kwargs", [
  47. {
  48. "speculative_model": SPEC_MODEL,
  49. "num_speculative_tokens": MAX_SPEC_TOKENS,
  50. },
  51. ])
  52. @pytest.mark.parametrize("output_len", [
  53. 128,
  54. ])
  55. @pytest.mark.parametrize("batch_size", [1, 32])
  56. @pytest.mark.parametrize("seed", [1])
  57. def test_medusa_e2e_greedy_correctness(aphrodite_runner, common_llm_kwargs,
  58. per_test_common_llm_kwargs,
  59. baseline_llm_kwargs, test_llm_kwargs,
  60. batch_size: int, output_len: int,
  61. seed: int):
  62. """Verify greedy equality with different batch size."""
  63. run_equality_correctness_test(aphrodite_runner,
  64. common_llm_kwargs,
  65. per_test_common_llm_kwargs,
  66. baseline_llm_kwargs,
  67. test_llm_kwargs,
  68. batch_size,
  69. max_output_len=output_len,
  70. seed=seed,
  71. temperature=0.0)
  72. @pytest.mark.parametrize(
  73. "common_llm_kwargs",
  74. [{
  75. "enforce_eager": False,
  76. # Required for spec decode.
  77. "use_v2_block_manager": True,
  78. # Print spec metrics.
  79. "disable_log_stats": False,
  80. # Precision
  81. "dtype": PRECISION,
  82. # Main model
  83. "model_name": MAIN_MODEL,
  84. }])
  85. @pytest.mark.parametrize("per_test_common_llm_kwargs", [{}])
  86. @pytest.mark.parametrize("baseline_llm_kwargs", [{}])
  87. @pytest.mark.parametrize("test_llm_kwargs", [
  88. {
  89. "speculative_model": SPEC_MODEL,
  90. "num_speculative_tokens": MAX_SPEC_TOKENS,
  91. },
  92. ])
  93. @pytest.mark.parametrize("output_len", [
  94. 128,
  95. ])
  96. @pytest.mark.parametrize("batch_size", [1, 32])
  97. @pytest.mark.parametrize("seed", [1])
  98. def test_medusa_e2e_greedy_correctness_cuda_graph(
  99. aphrodite_runner, common_llm_kwargs, per_test_common_llm_kwargs,
  100. baseline_llm_kwargs, test_llm_kwargs, batch_size: int, output_len: int,
  101. seed: int):
  102. """Verify greedy equality with cuda graph enabled and different
  103. batch sizes."""
  104. run_equality_correctness_test(aphrodite_runner,
  105. common_llm_kwargs,
  106. per_test_common_llm_kwargs,
  107. baseline_llm_kwargs,
  108. test_llm_kwargs,
  109. batch_size,
  110. max_output_len=output_len,
  111. seed=seed,
  112. temperature=0.0)
  113. @pytest.mark.parametrize(
  114. "common_llm_kwargs",
  115. [{
  116. "block_size": 8,
  117. # 2 for small prompt, 256//8 for generated.
  118. "num_gpu_blocks_override": 2 + 256 // 8,
  119. "max_model_len": (2 + 256 // 8) * 8,
  120. # Skip cuda graph recording for fast test.
  121. "enforce_eager": True,
  122. # Required for spec decode.
  123. "use_v2_block_manager": True,
  124. # Precision
  125. "dtype": PRECISION,
  126. # Main model
  127. "model_name": MAIN_MODEL,
  128. }])
  129. @pytest.mark.parametrize("per_test_common_llm_kwargs", [{}])
  130. @pytest.mark.parametrize("baseline_llm_kwargs", [{}])
  131. @pytest.mark.parametrize("test_llm_kwargs", [
  132. {
  133. "speculative_model": SPEC_MODEL,
  134. "num_speculative_tokens": MAX_SPEC_TOKENS,
  135. },
  136. ])
  137. @pytest.mark.parametrize(
  138. "output_len",
  139. [
  140. # Use small output len for fast test.
  141. 128,
  142. ])
  143. @pytest.mark.parametrize("batch_size", [4])
  144. @pytest.mark.parametrize("seed", [1])
  145. def test_medusa_e2e_greedy_correctness_with_preemption(
  146. aphrodite_runner, common_llm_kwargs, per_test_common_llm_kwargs,
  147. baseline_llm_kwargs, test_llm_kwargs, batch_size: int, output_len: int,
  148. seed: int):
  149. """Verify greedy equality, even when some sequences are preempted mid-
  150. generation.
  151. """
  152. run_equality_correctness_test(aphrodite_runner,
  153. common_llm_kwargs,
  154. per_test_common_llm_kwargs,
  155. baseline_llm_kwargs,
  156. test_llm_kwargs,
  157. batch_size,
  158. max_output_len=output_len,
  159. seed=seed,
  160. temperature=0.0)
  161. @pytest.mark.parametrize(
  162. "common_llm_kwargs",
  163. [{
  164. # Skip cuda graph recording for fast test.
  165. "enforce_eager": True,
  166. # Required for spec decode.
  167. "use_v2_block_manager": True,
  168. # Precision
  169. "dtype": PRECISION,
  170. # Main model
  171. "model_name": MAIN_MODEL,
  172. }])
  173. @pytest.mark.parametrize("per_test_common_llm_kwargs", [{}])
  174. @pytest.mark.parametrize("baseline_llm_kwargs", [{}])
  175. @pytest.mark.parametrize(
  176. "test_llm_kwargs",
  177. [
  178. {
  179. "speculative_model": SPEC_MODEL,
  180. "num_speculative_tokens": k,
  181. }
  182. # Try a range of num. speculative tokens
  183. for k in range(1, 1 + MAX_SPEC_TOKENS)
  184. ])
  185. @pytest.mark.parametrize("batch_size", [2])
  186. @pytest.mark.parametrize(
  187. "output_len",
  188. [
  189. # Use smaller output len for fast test.
  190. 32,
  191. ])
  192. @pytest.mark.parametrize("seed", [1])
  193. def test_medusa_different_k(aphrodite_runner, common_llm_kwargs,
  194. per_test_common_llm_kwargs, baseline_llm_kwargs,
  195. test_llm_kwargs, batch_size: int, output_len: int,
  196. seed: int):
  197. """Verify that medusa speculative decoding produces exact equality
  198. to without spec decode with different values of num_speculative_tokens.
  199. """
  200. run_equality_correctness_test(aphrodite_runner,
  201. common_llm_kwargs,
  202. per_test_common_llm_kwargs,
  203. baseline_llm_kwargs,
  204. test_llm_kwargs,
  205. batch_size,
  206. max_output_len=output_len,
  207. seed=seed,
  208. temperature=0.0)
  209. @pytest.mark.parametrize(
  210. "common_llm_kwargs",
  211. [{
  212. # Skip cuda graph recording for fast test.
  213. "enforce_eager": True,
  214. # Required for spec decode.
  215. "use_v2_block_manager": True,
  216. # Precision
  217. "dtype": PRECISION,
  218. # Main model
  219. "model_name": MAIN_MODEL,
  220. }])
  221. @pytest.mark.parametrize("per_test_common_llm_kwargs", [{}])
  222. @pytest.mark.parametrize("baseline_llm_kwargs", [{}])
  223. @pytest.mark.parametrize("test_llm_kwargs",
  224. [{
  225. "speculative_model": SPEC_MODEL,
  226. "num_speculative_tokens": MAX_SPEC_TOKENS,
  227. "speculative_disable_by_batch_size": 4
  228. }])
  229. @pytest.mark.parametrize("batch_size", [1, 5])
  230. @pytest.mark.parametrize(
  231. "output_len",
  232. [
  233. # Use smaller output len for fast test.
  234. 32,
  235. ])
  236. @pytest.mark.parametrize("seed", [1])
  237. def test_medusa_disable_queue(aphrodite_runner, common_llm_kwargs,
  238. per_test_common_llm_kwargs, baseline_llm_kwargs,
  239. test_llm_kwargs, batch_size: int,
  240. output_len: int, seed: int):
  241. """Verify that medusa speculative decoding produces exact equality
  242. to without spec decode when speculation is disabled for large
  243. batch sizes.
  244. """
  245. run_equality_correctness_test(aphrodite_runner,
  246. common_llm_kwargs,
  247. per_test_common_llm_kwargs,
  248. baseline_llm_kwargs,
  249. test_llm_kwargs,
  250. batch_size,
  251. max_output_len=output_len,
  252. seed=seed,
  253. temperature=0.0)
  254. if __name__ == "__main__":
  255. import pytest
  256. pytest.main([__file__])