test_baichuan.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. from typing import List
  2. import pytest
  3. import aphrodite
  4. from aphrodite.lora.request import LoRARequest
  5. from .conftest import cleanup
  6. MODEL_PATH = "baichuan-inc/Baichuan-7B"
  7. PROMPT_TEMPLATE = """I want you to act as a SQL terminal in front of an example database, you need only to return the sql command to me.Below is an instruction that describes a task, Write a response that appropriately completes the request.\n"\n##Instruction:\nconcert_singer contains tables such as stadium, singer, concert, singer_in_concert. Table stadium has columns such as Stadium_ID, Location, Name, Capacity, Highest, Lowest, Average. Stadium_ID is the primary key.\nTable singer has columns such as Singer_ID, Name, Country, Song_Name, Song_release_year, Age, Is_male. Singer_ID is the primary key.\nTable concert has columns such as concert_ID, concert_Name, Theme, Stadium_ID, Year. concert_ID is the primary key.\nTable singer_in_concert has columns such as concert_ID, Singer_ID. concert_ID is the primary key.\nThe Stadium_ID of concert is the foreign key of Stadium_ID of stadium.\nThe Singer_ID of singer_in_concert is the foreign key of Singer_ID of singer.\nThe concert_ID of singer_in_concert is the foreign key of concert_ID of concert.\n\n###Input:\n{query}\n\n###Response:""" # noqa: E501
  8. def do_sample(llm: aphrodite.LLM, lora_path: str, lora_id: int) -> List[str]:
  9. prompts = [
  10. PROMPT_TEMPLATE.format(query="How many singers do we have?"),
  11. PROMPT_TEMPLATE.format(
  12. query=
  13. "What is the average, minimum, and maximum age of all singers from France?" # noqa: E501
  14. ),
  15. PROMPT_TEMPLATE.format(
  16. query=
  17. "Show name, country, age for all singers ordered by age from the oldest to the youngest." # noqa: E501
  18. ),
  19. ]
  20. print(prompts)
  21. sampling_params = aphrodite.SamplingParams(temperature=0, max_tokens=256)
  22. outputs = llm.generate(
  23. prompts,
  24. sampling_params,
  25. lora_request=LoRARequest(str(lora_id), lora_id, lora_path)
  26. if lora_id else None)
  27. # Print the outputs.
  28. generated_texts: List[str] = []
  29. for output in outputs:
  30. prompt = output.prompt
  31. generated_text = output.outputs[0].text.strip()
  32. generated_texts.append(generated_text)
  33. print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
  34. return generated_texts
  35. def test_baichuan_lora(baichuan_lora_files):
  36. llm = aphrodite.LLM(MODEL_PATH,
  37. max_model_len=1024,
  38. enable_lora=True,
  39. max_loras=4,
  40. max_lora_rank=64,
  41. trust_remote_code=True)
  42. expected_lora_output = [
  43. "SELECT count(*) FROM singer",
  44. "SELECT avg(age) , min(age) , max(age) FROM singer WHERE Country = 'France'", # noqa: E501
  45. "SELECT name , country , age FROM singer ORDER BY age ASC",
  46. ]
  47. output1 = do_sample(llm, baichuan_lora_files, lora_id=1)
  48. for i in range(len(expected_lora_output)):
  49. assert output1[i] == expected_lora_output[i]
  50. output2 = do_sample(llm, baichuan_lora_files, lora_id=2)
  51. for i in range(len(expected_lora_output)):
  52. assert output2[i] == expected_lora_output[i]
  53. @pytest.mark.skip("Requires multiple GPUs")
  54. @pytest.mark.parametrize("fully_sharded", [True, False])
  55. def test_baichuan_tensor_parallel_equality(baichuan_lora_files, fully_sharded):
  56. # Cannot use as it will initialize torch.cuda too early...
  57. # if torch.cuda.device_count() < 4:
  58. # pytest.skip(f"Not enough GPUs for tensor parallelism {4}")
  59. llm_tp1 = aphrodite.LLM(MODEL_PATH,
  60. enable_lora=True,
  61. max_num_seqs=16,
  62. max_loras=4,
  63. max_lora_rank=64,
  64. tensor_parallel_size=1,
  65. trust_remote_code=True,
  66. fully_sharded_loras=fully_sharded)
  67. output_tp1 = do_sample(llm_tp1, baichuan_lora_files, lora_id=1)
  68. del llm_tp1
  69. cleanup()
  70. llm_tp2 = aphrodite.LLM(MODEL_PATH,
  71. enable_lora=True,
  72. max_num_seqs=16,
  73. max_loras=4,
  74. max_lora_rank=64,
  75. tensor_parallel_size=2,
  76. trust_remote_code=True,
  77. fully_sharded_loras=fully_sharded)
  78. output_tp2 = do_sample(llm_tp2, baichuan_lora_files, lora_id=2)
  79. del llm_tp2
  80. cleanup()
  81. assert output_tp1 == output_tp2
  82. llm_tp4 = aphrodite.LLM(MODEL_PATH,
  83. enable_lora=True,
  84. max_num_seqs=16,
  85. max_loras=4,
  86. max_lora_rank=64,
  87. tensor_parallel_size=4,
  88. trust_remote_code=True,
  89. fully_sharded_loras=fully_sharded)
  90. output_tp4 = do_sample(llm_tp4, baichuan_lora_files, lora_id=2)
  91. del llm_tp4
  92. cleanup()
  93. assert output_tp1 == output_tp4