test_encode.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import weakref
  2. from typing import List
  3. import pytest
  4. from aphrodite import LLM, EmbeddingRequestOutput, PoolingParams
  5. from ...conftest import cleanup
  6. MODEL_NAME = "intfloat/e5-mistral-7b-instruct"
  7. PROMPTS = [
  8. "Hello, my name is",
  9. "The president of the United States is",
  10. "The capital of France is",
  11. "The future of AI is",
  12. ]
  13. TOKEN_IDS = [
  14. # Using ID={0, 1, 2, 3} results in NaN values,
  15. # so we add this offset of 1000
  16. [1000],
  17. [1000, 1001],
  18. [1000, 1002, 1001],
  19. [1000, 1003, 1001, 1002],
  20. ]
  21. @pytest.fixture(scope="module")
  22. def llm():
  23. # pytest caches the fixture so we use weakref.proxy to
  24. # enable garbage collection
  25. llm = LLM(model=MODEL_NAME,
  26. max_num_batched_tokens=32768,
  27. tensor_parallel_size=1,
  28. gpu_memory_utilization=0.75,
  29. enforce_eager=True)
  30. with llm.deprecate_legacy_api():
  31. yield weakref.proxy(llm)
  32. del llm
  33. cleanup()
  34. def assert_outputs_equal(o1: List[EmbeddingRequestOutput],
  35. o2: List[EmbeddingRequestOutput]):
  36. assert [o.outputs for o in o1] == [o.outputs for o in o2]
  37. @pytest.mark.skip_global_cleanup
  38. @pytest.mark.parametrize('prompt', PROMPTS)
  39. def test_v1_v2_api_consistency_single_prompt_string(llm: LLM, prompt):
  40. pooling_params = PoolingParams()
  41. with pytest.warns(DeprecationWarning, match="'prompts'"):
  42. v1_output = llm.encode(prompts=prompt, pooling_params=pooling_params)
  43. v2_output = llm.encode(prompt, pooling_params=pooling_params)
  44. assert_outputs_equal(v1_output, v2_output)
  45. v2_output = llm.encode({"prompt": prompt}, pooling_params=pooling_params)
  46. assert_outputs_equal(v1_output, v2_output)
  47. @pytest.mark.skip_global_cleanup
  48. @pytest.mark.parametrize('prompt_token_ids', TOKEN_IDS)
  49. def test_v1_v2_api_consistency_single_prompt_tokens(llm: LLM,
  50. prompt_token_ids):
  51. pooling_params = PoolingParams()
  52. with pytest.warns(DeprecationWarning, match="'prompt_token_ids'"):
  53. v1_output = llm.encode(prompt_token_ids=prompt_token_ids,
  54. pooling_params=pooling_params)
  55. v2_output = llm.encode({"prompt_token_ids": prompt_token_ids},
  56. pooling_params=pooling_params)
  57. assert_outputs_equal(v1_output, v2_output)
  58. @pytest.mark.skip_global_cleanup
  59. def test_v1_v2_api_consistency_multi_prompt_string(llm: LLM):
  60. pooling_params = PoolingParams()
  61. with pytest.warns(DeprecationWarning, match="'prompts'"):
  62. v1_output = llm.encode(prompts=PROMPTS, pooling_params=pooling_params)
  63. v2_output = llm.encode(PROMPTS, pooling_params=pooling_params)
  64. assert_outputs_equal(v1_output, v2_output)
  65. v2_output = llm.encode(
  66. [{
  67. "prompt": p
  68. } for p in PROMPTS],
  69. pooling_params=pooling_params,
  70. )
  71. assert_outputs_equal(v1_output, v2_output)
  72. @pytest.mark.skip_global_cleanup
  73. def test_v1_v2_api_consistency_multi_prompt_tokens(llm: LLM):
  74. pooling_params = PoolingParams()
  75. with pytest.warns(DeprecationWarning, match="'prompt_token_ids'"):
  76. v1_output = llm.encode(prompt_token_ids=TOKEN_IDS,
  77. pooling_params=pooling_params)
  78. v2_output = llm.encode(
  79. [{
  80. "prompt_token_ids": p
  81. } for p in TOKEN_IDS],
  82. pooling_params=pooling_params,
  83. )
  84. assert_outputs_equal(v1_output, v2_output)
  85. @pytest.mark.skip_global_cleanup
  86. def test_multiple_pooling_params(llm: LLM):
  87. pooling_params = [
  88. PoolingParams(),
  89. PoolingParams(),
  90. PoolingParams(),
  91. PoolingParams(),
  92. ]
  93. # Multiple PoolingParams should be matched with each prompt
  94. outputs = llm.encode(PROMPTS, pooling_params=pooling_params)
  95. assert len(PROMPTS) == len(outputs)
  96. # Exception raised, if the size of params does not match the size of prompts
  97. with pytest.raises(ValueError):
  98. outputs = llm.encode(PROMPTS, pooling_params=pooling_params[:3])
  99. # Single PoolingParams should be applied to every prompt
  100. single_pooling_params = PoolingParams()
  101. outputs = llm.encode(PROMPTS, pooling_params=single_pooling_params)
  102. assert len(PROMPTS) == len(outputs)
  103. # pooling_params is None, default params should be applied
  104. outputs = llm.encode(PROMPTS, pooling_params=None)
  105. assert len(PROMPTS) == len(outputs)