test_medusa_correctness.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_greedy_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": 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_mlp_e2e_greedy_correctness(baseline_llm_generator, test_llm_generator,
  58. batch_size: int, output_len: int):
  59. """Verify greedy equality with different batch size."""
  60. run_greedy_equality_correctness_test(baseline_llm_generator,
  61. test_llm_generator,
  62. batch_size,
  63. max_output_len=output_len,
  64. force_output_len=True)
  65. @pytest.mark.parametrize(
  66. "common_llm_kwargs",
  67. [{
  68. "block_size": 8,
  69. # 2 for small prompt, 256//8 for generated.
  70. "num_gpu_blocks_override": 2 + 256 // 8,
  71. "max_model_len": (2 + 256 // 8) * 8,
  72. # Skip cuda graph recording for fast test.
  73. "enforce_eager": True,
  74. # Required for spec decode.
  75. "use_v2_block_manager": True,
  76. # Precision
  77. "dtype": PRECISION,
  78. # Main model
  79. "model": MAIN_MODEL,
  80. }])
  81. @pytest.mark.parametrize("per_test_common_llm_kwargs", [{}])
  82. @pytest.mark.parametrize("baseline_llm_kwargs", [{}])
  83. @pytest.mark.parametrize("test_llm_kwargs", [
  84. {
  85. "speculative_model": SPEC_MODEL,
  86. "num_speculative_tokens": MAX_SPEC_TOKENS,
  87. },
  88. ])
  89. @pytest.mark.parametrize(
  90. "output_len",
  91. [
  92. # Use small output len for fast test.
  93. 128,
  94. ])
  95. @pytest.mark.parametrize("batch_size", [4])
  96. @pytest.mark.parametrize("seed", [1])
  97. def test_mlp_e2e_greedy_correctness_with_preemption(baseline_llm_generator,
  98. test_llm_generator,
  99. batch_size: int,
  100. output_len: int):
  101. """Verify greedy equality, even when some sequences are preempted mid-
  102. generation.
  103. """
  104. run_greedy_equality_correctness_test(baseline_llm_generator,
  105. test_llm_generator,
  106. batch_size,
  107. max_output_len=output_len,
  108. force_output_len=True)
  109. @pytest.mark.parametrize(
  110. "common_llm_kwargs",
  111. [{
  112. # Skip cuda graph recording for fast test.
  113. "enforce_eager": True,
  114. # Required for spec decode.
  115. "use_v2_block_manager": True,
  116. # Precision
  117. "dtype": PRECISION,
  118. # Main model
  119. "model": MAIN_MODEL,
  120. }])
  121. @pytest.mark.parametrize("per_test_common_llm_kwargs", [{}])
  122. @pytest.mark.parametrize("baseline_llm_kwargs", [{}])
  123. @pytest.mark.parametrize(
  124. "test_llm_kwargs",
  125. [
  126. {
  127. "speculative_model": SPEC_MODEL,
  128. "num_speculative_tokens": k,
  129. }
  130. # Try a range of num. speculative tokens
  131. for k in range(1, 1 + MAX_SPEC_TOKENS)
  132. ])
  133. @pytest.mark.parametrize("batch_size", [2])
  134. @pytest.mark.parametrize(
  135. "output_len",
  136. [
  137. # Use smaller output len for fast test.
  138. 32,
  139. ])
  140. @pytest.mark.parametrize("seed", [1])
  141. def test_mlp_different_k(baseline_llm_generator, test_llm_generator,
  142. batch_size: int, output_len: int):
  143. """Verify that mlp speculative decoding produces exact equality
  144. to without spec decode with different values of num_speculative_tokens.
  145. """
  146. run_greedy_equality_correctness_test(baseline_llm_generator,
  147. test_llm_generator,
  148. batch_size,
  149. max_output_len=output_len,
  150. force_output_len=True)
  151. @pytest.mark.parametrize(
  152. "common_llm_kwargs",
  153. [{
  154. # Skip cuda graph recording for fast test.
  155. "enforce_eager": True,
  156. # Required for spec decode.
  157. "use_v2_block_manager": True,
  158. # Precision
  159. "dtype": PRECISION,
  160. # Main model
  161. "model": MAIN_MODEL,
  162. }])
  163. @pytest.mark.parametrize("per_test_common_llm_kwargs", [{}])
  164. @pytest.mark.parametrize("baseline_llm_kwargs", [{}])
  165. @pytest.mark.parametrize("test_llm_kwargs",
  166. [{
  167. "speculative_model": SPEC_MODEL,
  168. "num_speculative_tokens": MAX_SPEC_TOKENS,
  169. "speculative_disable_by_batch_size": 4
  170. }])
  171. @pytest.mark.parametrize("batch_size", [1, 5])
  172. @pytest.mark.parametrize(
  173. "output_len",
  174. [
  175. # Use smaller output len for fast test.
  176. 32,
  177. ])
  178. @pytest.mark.parametrize("seed", [1])
  179. def test_mlp_disable_queue(baseline_llm_generator, test_llm_generator,
  180. batch_size: int, output_len: int):
  181. """Verify that mlp speculative decoding produces exact equality
  182. to without spec decode when speculation is disabled for large
  183. batch sizes.
  184. """
  185. run_greedy_equality_correctness_test(baseline_llm_generator,
  186. test_llm_generator,
  187. batch_size,
  188. max_output_len=output_len,
  189. force_output_len=True)
  190. if __name__ == "__main__":
  191. import pytest
  192. pytest.main([__file__])