1
0

test_chunked_prefill_distributed.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """Compare the outputs of HF and distributed Aphrodite when using greedy
  2. sampling.
  3. Run:
  4. ```sh
  5. pytest test_chunked_prefill_distributed.py
  6. ```
  7. """
  8. import pytest
  9. from aphrodite.common.utils import cuda_device_count_stateless
  10. from ..models.utils import check_outputs_equal
  11. from ..utils import fork_new_process_for_each_test
  12. @pytest.mark.skipif(cuda_device_count_stateless() < 2,
  13. reason="Need at least 2 GPUs to run the test.")
  14. @pytest.mark.parametrize("model, distributed_executor_backend", [
  15. ("facebook/opt-125m", "ray"),
  16. ("meta-llama/Llama-2-7b-hf", "ray"),
  17. ("facebook/opt-125m", "mp"),
  18. ("meta-llama/Llama-2-7b-hf", "mp"),
  19. ])
  20. @fork_new_process_for_each_test
  21. def test_models(
  22. hf_runner,
  23. aphrodite_runner,
  24. example_prompts,
  25. model: str,
  26. distributed_executor_backend: str,
  27. ) -> None:
  28. dtype = "half"
  29. max_tokens = 5
  30. chunked_prefill_token_size = 16
  31. # Add a chunked prefill config.
  32. max_num_seqs = min(chunked_prefill_token_size, 256)
  33. assert chunked_prefill_token_size != -1
  34. enable_chunked_prefill = True
  35. max_num_batched_tokens = chunked_prefill_token_size
  36. # NOTE: take care of the order. run Aphrodite first, and then run HF.
  37. # Aphrodite needs a fresh new process without cuda initialization.
  38. # if we run HF first, the cuda initialization will be done and it
  39. # will hurt multiprocessing backend with fork method (the default method).
  40. with aphrodite_runner(
  41. model,
  42. dtype=dtype,
  43. tensor_parallel_size=2,
  44. max_num_seqs=max_num_seqs,
  45. enable_chunked_prefill=enable_chunked_prefill,
  46. max_num_batched_tokens=max_num_batched_tokens,
  47. distributed_executor_backend=distributed_executor_backend,
  48. ) as aphrodite_model:
  49. aphrodite_outputs = aphrodite_model.generate_greedy(
  50. example_prompts, max_tokens)
  51. with hf_runner(model, dtype=dtype) as hf_model:
  52. hf_outputs = hf_model.generate_greedy(example_prompts, max_tokens)
  53. check_outputs_equal(
  54. outputs_0_lst=hf_outputs,
  55. outputs_1_lst=aphrodite_outputs,
  56. name_0="hf",
  57. name_1="aphrodite",
  58. )