test_phi.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from typing import List
  2. import aphrodite
  3. from aphrodite.lora.request import LoRARequest
  4. MODEL_PATH = "microsoft/phi-2"
  5. PROMPT_TEMPLATE = "### Instruct: {sql_prompt}\n\n### Context: {context}\n\n### Output:" # noqa: E501
  6. def do_sample(llm: aphrodite.LLM, lora_path: str, lora_id: int) -> List[str]:
  7. prompts = [
  8. PROMPT_TEMPLATE.format(
  9. sql_prompt=
  10. "Which catalog publisher has published the most catalogs?",
  11. context="CREATE TABLE catalogs (catalog_publisher VARCHAR);"),
  12. PROMPT_TEMPLATE.format(
  13. sql_prompt=
  14. "Which trip started from the station with the largest dock count? Give me the trip id.", # noqa: E501
  15. context=
  16. "CREATE TABLE trip (id VARCHAR, start_station_id VARCHAR); CREATE TABLE station (id VARCHAR, dock_count VARCHAR);" # noqa: E501
  17. ),
  18. PROMPT_TEMPLATE.format(
  19. sql_prompt=
  20. "How many marine species are found in the Southern Ocean?", # noqa: E501
  21. context=
  22. "CREATE TABLE marine_species (name VARCHAR(50), common_name VARCHAR(50), location VARCHAR(50));" # noqa: E501
  23. ),
  24. ]
  25. sampling_params = aphrodite.SamplingParams(temperature=0,
  26. max_tokens=64,
  27. stop="### End")
  28. outputs = llm.generate(
  29. prompts,
  30. sampling_params,
  31. lora_request=LoRARequest(str(lora_id), lora_id, lora_path)
  32. if lora_id else None,
  33. )
  34. # Print the outputs.
  35. generated_texts: List[str] = []
  36. for output in outputs:
  37. prompt = output.prompt
  38. generated_text = output.outputs[0].text.strip()
  39. generated_texts.append(generated_text)
  40. print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
  41. return generated_texts
  42. def test_phi2_lora(phi2_lora_files):
  43. # We enable enforce_eager=True here to reduce VRAM usage for lora-test CI,
  44. # Otherwise, the lora-test will fail due to CUDA OOM.
  45. llm = aphrodite.LLM(MODEL_PATH,
  46. max_model_len=1024,
  47. enable_lora=True,
  48. max_loras=2,
  49. enforce_eager=True)
  50. expected_lora_output = [
  51. "SELECT catalog_publisher, COUNT(*) as num_catalogs FROM catalogs GROUP BY catalog_publisher ORDER BY num_catalogs DESC LIMIT 1;", # noqa: E501
  52. "SELECT trip.id FROM trip JOIN station ON trip.start_station_id = station.id WHERE station.dock_count = (SELECT MAX(dock_count) FROM station);", # noqa: E501
  53. "SELECT COUNT(*) FROM marine_species WHERE location = 'Southern Ocean';", # noqa: E501
  54. ]
  55. output1 = do_sample(llm, phi2_lora_files, lora_id=1)
  56. for i in range(len(expected_lora_output)):
  57. assert output1[i].startswith(expected_lora_output[i])
  58. output2 = do_sample(llm, phi2_lora_files, lora_id=2)
  59. for i in range(len(expected_lora_output)):
  60. assert output2[i].startswith(expected_lora_output[i])