test_chunked_prefill.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. """Compare the outputs of HF and Aphrodite when using greedy sampling.
  2. It tests chunked prefill. Chunked prefill can be enabled by
  3. enable_chunked_prefill=True. If prefill size exceeds max_num_batched_tokens,
  4. prefill requests are chunked.
  5. Run `pytest tests/models/test_chunked_prefill.py`.
  6. """
  7. import pytest
  8. from ..models.utils import check_logprobs_close, check_outputs_equal
  9. MODELS = [
  10. "facebook/opt-125m",
  11. "meta-llama/Llama-2-7b-hf",
  12. ]
  13. E5M2_KV_MODELS = [
  14. "facebook/opt-125m",
  15. "meta-llama/Llama-2-7b-chat-hf",
  16. ]
  17. E4M3_KV_MODELS = [
  18. "meta-llama/Llama-2-7b-chat-hf", "nm-testing/Qwen2-1.5B-Instruct-FP8-K-V",
  19. "nm-testing/TinyLlama-1.1B-compressed-tensors-kv-cache-scheme"
  20. ]
  21. KV_CACHE_QUANTIZATION_PATHS = {
  22. "meta-llama/Llama-2-7b-chat-hf":
  23. "./tests/fp8_kv/llama2-7b-fp8-kv/kv_cache_scales.json"
  24. }
  25. @pytest.mark.parametrize("model", MODELS)
  26. @pytest.mark.parametrize("dtype", ["half"])
  27. @pytest.mark.parametrize("max_tokens", [32])
  28. @pytest.mark.parametrize("chunked_prefill_token_size", [1, 4, 16])
  29. @pytest.mark.parametrize("enforce_eager", [False, True])
  30. # NOTE: Increasing this in this suite will fail CI because we currently cannot
  31. # reset distributed env properly. Use a value > 1 just when you test.
  32. @pytest.mark.parametrize("tensor_parallel_size", [1])
  33. def test_models(
  34. hf_runner,
  35. aphrodite_runner,
  36. example_prompts,
  37. model: str,
  38. dtype: str,
  39. max_tokens: int,
  40. chunked_prefill_token_size: int,
  41. enforce_eager: bool,
  42. tensor_parallel_size: int,
  43. ) -> None:
  44. """
  45. Checks exact match decode between huggingface model and aphrodite runner
  46. with chunked prefill.
  47. """
  48. max_num_seqs = chunked_prefill_token_size
  49. max_num_batched_tokens = chunked_prefill_token_size
  50. with hf_runner(model, dtype=dtype) as hf_model:
  51. hf_outputs = hf_model.generate_greedy(example_prompts, max_tokens)
  52. with aphrodite_runner(
  53. model,
  54. dtype=dtype,
  55. max_num_batched_tokens=max_num_batched_tokens,
  56. enable_chunked_prefill=True,
  57. tensor_parallel_size=tensor_parallel_size,
  58. enforce_eager=enforce_eager,
  59. max_num_seqs=max_num_seqs,
  60. ) as aphrodite_model:
  61. aphrodite_outputs = aphrodite_model.generate_greedy(example_prompts,
  62. max_tokens)
  63. check_outputs_equal(
  64. outputs_0_lst=hf_outputs,
  65. outputs_1_lst=aphrodite_outputs,
  66. name_0="hf",
  67. name_1="aphrodite",
  68. )
  69. @pytest.mark.parametrize("kv_cache_dtype,model",
  70. [("fp8_e5m2", m)
  71. for m in E5M2_KV_MODELS] + [("fp8_e4m3", m)
  72. for m in E4M3_KV_MODELS])
  73. # Due to low-precision numerical divergence, we only test logprob of 4 tokens
  74. @pytest.mark.parametrize("max_tokens", [4])
  75. @pytest.mark.parametrize("chunked_prefill_token_size", [1, 4, 16])
  76. @pytest.mark.parametrize("enforce_eager", [False, True])
  77. # NOTE: Increasing this in this suite will fail CI because we currently cannot
  78. # reset distributed env properly. Use a value > 1 just when you test.
  79. @pytest.mark.parametrize("tensor_parallel_size", [1])
  80. def test_models_with_fp8_kv_cache(
  81. aphrodite_runner,
  82. example_prompts,
  83. kv_cache_dtype: str,
  84. model: str,
  85. max_tokens: int,
  86. chunked_prefill_token_size: int,
  87. enforce_eager: bool,
  88. tensor_parallel_size: int,
  89. ) -> None:
  90. """
  91. Only checks log probs match between chunked-prefill and
  92. non-chunked-prefill version of Aphrodite model runner.
  93. This test is used when there is discrepancy in kernels
  94. / numerics (e.g. when using lower-precision types like FP8).
  95. """
  96. NUM_LOG_PROBS = 8
  97. if model == "facebook/opt-125m":
  98. pytest.skip(
  99. "#7378: CUDA illegal memory access (undiagnosed) facebook/opt-125m"
  100. )
  101. max_num_seqs = chunked_prefill_token_size
  102. max_num_batched_tokens = chunked_prefill_token_size
  103. extra_kwargs = {}
  104. if model in KV_CACHE_QUANTIZATION_PATHS:
  105. extra_kwargs["quantization_param_path"] = KV_CACHE_QUANTIZATION_PATHS[
  106. model]
  107. with aphrodite_runner(
  108. model,
  109. tensor_parallel_size=tensor_parallel_size,
  110. enforce_eager=enforce_eager,
  111. max_num_seqs=max_num_seqs,
  112. kv_cache_dtype=kv_cache_dtype,
  113. **extra_kwargs,
  114. ) as aphrodite_model:
  115. no_chunked_prefill_outputs = aphrodite_model.generate_greedy_logprobs(
  116. example_prompts, max_tokens, NUM_LOG_PROBS)
  117. with aphrodite_runner(
  118. model,
  119. max_num_batched_tokens=max_num_batched_tokens,
  120. enable_chunked_prefill=True,
  121. tensor_parallel_size=tensor_parallel_size,
  122. enforce_eager=enforce_eager,
  123. max_num_seqs=max_num_seqs,
  124. kv_cache_dtype=kv_cache_dtype,
  125. **extra_kwargs,
  126. ) as aphrodite_model:
  127. chunked_prefill_outputs = aphrodite_model.generate_greedy_logprobs(
  128. example_prompts, max_tokens, NUM_LOG_PROBS)
  129. check_logprobs_close(
  130. outputs_0_lst=no_chunked_prefill_outputs,
  131. outputs_1_lst=chunked_prefill_outputs,
  132. name_0="no_chunked_prefill",
  133. name_1="chunked_prefill",
  134. )