test_ctx_shift.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import pytest
  2. from utils import *
  3. server = ServerPreset.tinyllama2()
  4. LONG_TEXT = """
  5. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  6. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
  7. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
  8. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  9. """.strip()
  10. @pytest.fixture(scope="module", autouse=True)
  11. def create_server():
  12. global server
  13. server = ServerPreset.tinyllama2()
  14. server.n_ctx = 256
  15. server.n_slots = 2
  16. def test_ctx_shift_enabled():
  17. # the prompt is 301 tokens
  18. # the slot context is 256/2 = 128 tokens
  19. # the prompt is truncated to keep the last 109 tokens
  20. # 64 tokens are generated thanks to shifting the context when it gets full
  21. global server
  22. server.start()
  23. res = server.make_request("POST", "/completion", data={
  24. "n_predict": 64,
  25. "prompt": LONG_TEXT,
  26. })
  27. assert res.status_code == 200
  28. assert res.body["timings"]["prompt_n"] == 109
  29. assert res.body["timings"]["predicted_n"] == 64
  30. assert res.body["truncated"] is True
  31. @pytest.mark.parametrize("n_predict,n_token_output,truncated", [
  32. (64, 64, False),
  33. (-1, 120, True),
  34. ])
  35. def test_ctx_shift_disabled_short_prompt(n_predict: int, n_token_output: int, truncated: bool):
  36. global server
  37. server.disable_ctx_shift = True
  38. server.n_predict = -1
  39. server.start()
  40. res = server.make_request("POST", "/completion", data={
  41. "n_predict": n_predict,
  42. "prompt": "Hi how are you",
  43. })
  44. assert res.status_code == 200
  45. assert res.body["timings"]["predicted_n"] == n_token_output
  46. assert res.body["truncated"] == truncated
  47. def test_ctx_shift_disabled_long_prompt():
  48. global server
  49. server.disable_ctx_shift = True
  50. server.start()
  51. res = server.make_request("POST", "/completion", data={
  52. "n_predict": 64,
  53. "prompt": LONG_TEXT,
  54. })
  55. assert res.status_code != 200
  56. assert "error" in res.body
  57. assert "exceeds the available context size" in res.body["error"]["message"]