test_completion.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. import pytest
  2. import time
  3. from utils import *
  4. server = ServerPreset.tinyllama2()
  5. @pytest.fixture(scope="module", autouse=True)
  6. def create_server():
  7. global server
  8. server = ServerPreset.tinyllama2()
  9. @pytest.mark.parametrize("prompt,n_predict,re_content,n_prompt,n_predicted,truncated,return_tokens", [
  10. ("I believe the meaning of life is", 8, "(going|bed)+", 18, 8, False, False),
  11. ("Write a joke about AI from a very long prompt which will not be truncated", 256, "(princesses|everyone|kids|Anna|forest)+", 46, 64, False, True),
  12. ])
  13. def test_completion(prompt: str, n_predict: int, re_content: str, n_prompt: int, n_predicted: int, truncated: bool, return_tokens: bool):
  14. global server
  15. server.start()
  16. res = server.make_request("POST", "/completion", data={
  17. "n_predict": n_predict,
  18. "prompt": prompt,
  19. "return_tokens": return_tokens,
  20. })
  21. assert res.status_code == 200
  22. assert res.body["timings"]["prompt_n"] == n_prompt
  23. assert res.body["timings"]["predicted_n"] == n_predicted
  24. assert res.body["truncated"] == truncated
  25. assert type(res.body["has_new_line"]) == bool
  26. assert match_regex(re_content, res.body["content"])
  27. if return_tokens:
  28. assert len(res.body["tokens"]) > 0
  29. assert all(type(tok) == int for tok in res.body["tokens"])
  30. else:
  31. assert res.body["tokens"] == []
  32. @pytest.mark.parametrize("prompt,n_predict,re_content,n_prompt,n_predicted,truncated", [
  33. ("I believe the meaning of life is", 8, "(going|bed)+", 18, 8, False),
  34. ("Write a joke about AI from a very long prompt which will not be truncated", 256, "(princesses|everyone|kids|Anna|forest)+", 46, 64, False),
  35. ])
  36. def test_completion_stream(prompt: str, n_predict: int, re_content: str, n_prompt: int, n_predicted: int, truncated: bool):
  37. global server
  38. server.start()
  39. res = server.make_stream_request("POST", "/completion", data={
  40. "n_predict": n_predict,
  41. "prompt": prompt,
  42. "stream": True,
  43. })
  44. content = ""
  45. for data in res:
  46. assert "stop" in data and type(data["stop"]) == bool
  47. if data["stop"]:
  48. assert data["timings"]["prompt_n"] == n_prompt
  49. assert data["timings"]["predicted_n"] == n_predicted
  50. assert data["truncated"] == truncated
  51. assert data["stop_type"] == "limit"
  52. assert type(data["has_new_line"]) == bool
  53. assert "generation_settings" in data
  54. assert server.n_predict is not None
  55. assert data["generation_settings"]["n_predict"] == min(n_predict, server.n_predict)
  56. assert data["generation_settings"]["seed"] == server.seed
  57. assert match_regex(re_content, content)
  58. else:
  59. assert len(data["tokens"]) > 0
  60. assert all(type(tok) == int for tok in data["tokens"])
  61. content += data["content"]
  62. def test_completion_stream_vs_non_stream():
  63. global server
  64. server.start()
  65. res_stream = server.make_stream_request("POST", "/completion", data={
  66. "n_predict": 8,
  67. "prompt": "I believe the meaning of life is",
  68. "stream": True,
  69. })
  70. res_non_stream = server.make_request("POST", "/completion", data={
  71. "n_predict": 8,
  72. "prompt": "I believe the meaning of life is",
  73. })
  74. content_stream = ""
  75. for data in res_stream:
  76. content_stream += data["content"]
  77. assert content_stream == res_non_stream.body["content"]
  78. @pytest.mark.parametrize("n_slots", [1, 2])
  79. def test_consistent_result_same_seed(n_slots: int):
  80. global server
  81. server.n_slots = n_slots
  82. server.start()
  83. last_res = None
  84. for _ in range(4):
  85. res = server.make_request("POST", "/completion", data={
  86. "prompt": "I believe the meaning of life is",
  87. "seed": 42,
  88. "temperature": 1.0,
  89. "cache_prompt": False, # TODO: remove this once test_cache_vs_nocache_prompt is fixed
  90. })
  91. if last_res is not None:
  92. assert res.body["content"] == last_res.body["content"]
  93. last_res = res
  94. @pytest.mark.parametrize("n_slots", [1, 2])
  95. def test_different_result_different_seed(n_slots: int):
  96. global server
  97. server.n_slots = n_slots
  98. server.start()
  99. last_res = None
  100. for seed in range(4):
  101. res = server.make_request("POST", "/completion", data={
  102. "prompt": "I believe the meaning of life is",
  103. "seed": seed,
  104. "temperature": 1.0,
  105. "cache_prompt": False, # TODO: remove this once test_cache_vs_nocache_prompt is fixed
  106. })
  107. if last_res is not None:
  108. assert res.body["content"] != last_res.body["content"]
  109. last_res = res
  110. @pytest.mark.parametrize("n_batch", [16, 32])
  111. @pytest.mark.parametrize("temperature", [0.0, 1.0])
  112. def test_consistent_result_different_batch_size(n_batch: int, temperature: float):
  113. global server
  114. server.n_batch = n_batch
  115. server.start()
  116. last_res = None
  117. for _ in range(4):
  118. res = server.make_request("POST", "/completion", data={
  119. "prompt": "I believe the meaning of life is",
  120. "seed": 42,
  121. "temperature": temperature,
  122. "cache_prompt": False, # TODO: remove this once test_cache_vs_nocache_prompt is fixed
  123. })
  124. if last_res is not None:
  125. assert res.body["content"] == last_res.body["content"]
  126. last_res = res
  127. @pytest.mark.skip(reason="This test fails on linux, need to be fixed")
  128. def test_cache_vs_nocache_prompt():
  129. global server
  130. server.start()
  131. res_cache = server.make_request("POST", "/completion", data={
  132. "prompt": "I believe the meaning of life is",
  133. "seed": 42,
  134. "temperature": 1.0,
  135. "cache_prompt": True,
  136. })
  137. res_no_cache = server.make_request("POST", "/completion", data={
  138. "prompt": "I believe the meaning of life is",
  139. "seed": 42,
  140. "temperature": 1.0,
  141. "cache_prompt": False,
  142. })
  143. assert res_cache.body["content"] == res_no_cache.body["content"]
  144. def test_completion_with_tokens_input():
  145. global server
  146. server.temperature = 0.0
  147. server.start()
  148. prompt_str = "I believe the meaning of life is"
  149. res = server.make_request("POST", "/tokenize", data={
  150. "content": prompt_str,
  151. "add_special": True,
  152. })
  153. assert res.status_code == 200
  154. tokens = res.body["tokens"]
  155. # single completion
  156. res = server.make_request("POST", "/completion", data={
  157. "prompt": tokens,
  158. })
  159. assert res.status_code == 200
  160. assert type(res.body["content"]) == str
  161. # batch completion
  162. res = server.make_request("POST", "/completion", data={
  163. "prompt": [tokens, tokens],
  164. })
  165. assert res.status_code == 200
  166. assert type(res.body) == list
  167. assert len(res.body) == 2
  168. assert res.body[0]["content"] == res.body[1]["content"]
  169. # mixed string and tokens
  170. res = server.make_request("POST", "/completion", data={
  171. "prompt": [tokens, prompt_str],
  172. })
  173. assert res.status_code == 200
  174. assert type(res.body) == list
  175. assert len(res.body) == 2
  176. assert res.body[0]["content"] == res.body[1]["content"]
  177. # mixed string and tokens in one sequence
  178. res = server.make_request("POST", "/completion", data={
  179. "prompt": [1, 2, 3, 4, 5, 6, prompt_str, 7, 8, 9, 10, prompt_str],
  180. })
  181. assert res.status_code == 200
  182. assert type(res.body["content"]) == str
  183. @pytest.mark.parametrize("n_slots,n_requests", [
  184. (1, 3),
  185. (2, 2),
  186. (2, 4),
  187. (4, 2), # some slots must be idle
  188. (4, 6),
  189. ])
  190. def test_completion_parallel_slots(n_slots: int, n_requests: int):
  191. global server
  192. server.n_slots = n_slots
  193. server.temperature = 0.0
  194. server.start()
  195. PROMPTS = [
  196. ("Write a very long book.", "(very|special|big)+"),
  197. ("Write another a poem.", "(small|house)+"),
  198. ("What is LLM?", "(Dad|said)+"),
  199. ("The sky is blue and I love it.", "(climb|leaf)+"),
  200. ("Write another very long music lyrics.", "(friends|step|sky)+"),
  201. ("Write a very long joke.", "(cat|Whiskers)+"),
  202. ]
  203. def check_slots_status():
  204. should_all_slots_busy = n_requests >= n_slots
  205. time.sleep(0.1)
  206. res = server.make_request("GET", "/slots")
  207. n_busy = sum([1 for slot in res.body if slot["is_processing"]])
  208. if should_all_slots_busy:
  209. assert n_busy == n_slots
  210. else:
  211. assert n_busy <= n_slots
  212. tasks = []
  213. for i in range(n_requests):
  214. prompt, re_content = PROMPTS[i % len(PROMPTS)]
  215. tasks.append((server.make_request, ("POST", "/completion", {
  216. "prompt": prompt,
  217. "seed": 42,
  218. "temperature": 1.0,
  219. })))
  220. tasks.append((check_slots_status, ()))
  221. results = parallel_function_calls(tasks)
  222. # check results
  223. for i in range(n_requests):
  224. prompt, re_content = PROMPTS[i % len(PROMPTS)]
  225. res = results[i]
  226. assert res.status_code == 200
  227. assert type(res.body["content"]) == str
  228. assert len(res.body["content"]) > 10
  229. # FIXME: the result is not deterministic when using other slot than slot 0
  230. # assert match_regex(re_content, res.body["content"])
  231. def test_n_probs():
  232. global server
  233. server.start()
  234. res = server.make_request("POST", "/completion", data={
  235. "prompt": "I believe the meaning of life is",
  236. "n_probs": 10,
  237. "temperature": 0.0,
  238. "n_predict": 5,
  239. })
  240. assert res.status_code == 200
  241. assert "completion_probabilities" in res.body
  242. assert len(res.body["completion_probabilities"]) == 5
  243. for tok in res.body["completion_probabilities"]:
  244. assert "id" in tok and tok["id"] > 0
  245. assert "token" in tok and type(tok["token"]) == str
  246. assert "logprob" in tok and tok["logprob"] <= 0.0
  247. assert "bytes" in tok and type(tok["bytes"]) == list
  248. assert len(tok["top_logprobs"]) == 10
  249. for prob in tok["top_logprobs"]:
  250. assert "id" in prob and prob["id"] > 0
  251. assert "token" in prob and type(prob["token"]) == str
  252. assert "logprob" in prob and prob["logprob"] <= 0.0
  253. assert "bytes" in prob and type(prob["bytes"]) == list
  254. def test_n_probs_stream():
  255. global server
  256. server.start()
  257. res = server.make_stream_request("POST", "/completion", data={
  258. "prompt": "I believe the meaning of life is",
  259. "n_probs": 10,
  260. "temperature": 0.0,
  261. "n_predict": 5,
  262. "stream": True,
  263. })
  264. for data in res:
  265. if data["stop"] == False:
  266. assert "completion_probabilities" in data
  267. assert len(data["completion_probabilities"]) == 1
  268. for tok in data["completion_probabilities"]:
  269. assert "id" in tok and tok["id"] > 0
  270. assert "token" in tok and type(tok["token"]) == str
  271. assert "logprob" in tok and tok["logprob"] <= 0.0
  272. assert "bytes" in tok and type(tok["bytes"]) == list
  273. assert len(tok["top_logprobs"]) == 10
  274. for prob in tok["top_logprobs"]:
  275. assert "id" in prob and prob["id"] > 0
  276. assert "token" in prob and type(prob["token"]) == str
  277. assert "logprob" in prob and prob["logprob"] <= 0.0
  278. assert "bytes" in prob and type(prob["bytes"]) == list
  279. def test_n_probs_post_sampling():
  280. global server
  281. server.start()
  282. res = server.make_request("POST", "/completion", data={
  283. "prompt": "I believe the meaning of life is",
  284. "n_probs": 10,
  285. "temperature": 0.0,
  286. "n_predict": 5,
  287. "post_sampling_probs": True,
  288. })
  289. assert res.status_code == 200
  290. assert "completion_probabilities" in res.body
  291. assert len(res.body["completion_probabilities"]) == 5
  292. for tok in res.body["completion_probabilities"]:
  293. assert "id" in tok and tok["id"] > 0
  294. assert "token" in tok and type(tok["token"]) == str
  295. assert "prob" in tok and 0.0 < tok["prob"] <= 1.0
  296. assert "bytes" in tok and type(tok["bytes"]) == list
  297. assert len(tok["top_probs"]) == 10
  298. for prob in tok["top_probs"]:
  299. assert "id" in prob and prob["id"] > 0
  300. assert "token" in prob and type(prob["token"]) == str
  301. assert "prob" in prob and 0.0 <= prob["prob"] <= 1.0
  302. assert "bytes" in prob and type(prob["bytes"]) == list
  303. # because the test model usually output token with either 100% or 0% probability, we need to check all the top_probs
  304. assert any(prob["prob"] == 1.0 for prob in tok["top_probs"])