test_ctx_shift.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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(autouse=True)
  11. def create_server():
  12. global server
  13. server = ServerPreset.tinyllama2()
  14. server.n_ctx = 512
  15. server.n_slots = 2
  16. server.n_predict = 128
  17. def test_ctx_shift_enabled():
  18. # the prompt is 301 tokens
  19. # the slot context is 512/2 = 256 tokens
  20. # the prompt is truncated to keep the last (301 - 256/2) = 173 tokens
  21. # 96 tokens are generated thanks to shifting the context when it gets full
  22. global server
  23. server.enable_ctx_shift = True
  24. server.start()
  25. res = server.make_request("POST", "/completion", data={
  26. "n_predict": 96,
  27. "prompt": LONG_TEXT,
  28. })
  29. assert res.status_code == 200
  30. assert res.body["timings"]["prompt_n"] == 173
  31. assert res.body["timings"]["predicted_n"] == 96
  32. assert res.body["truncated"] is True
  33. @pytest.mark.parametrize("n_predict,n_token_output,truncated", [
  34. (64, 64, False),
  35. (-1, 120, True),
  36. ])
  37. def test_ctx_shift_disabled_short_prompt(n_predict: int, n_token_output: int, truncated: bool):
  38. global server
  39. server.n_predict = -1
  40. server.start()
  41. res = server.make_request("POST", "/completion", data={
  42. "n_predict": n_predict,
  43. "prompt": "Hi how are you",
  44. })
  45. assert res.status_code == 200
  46. assert res.body["timings"]["predicted_n"] == n_token_output
  47. assert res.body["truncated"] == truncated
  48. def test_ctx_shift_disabled_long_prompt():
  49. global server
  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"]
  58. def test_ctx_shift_disabled_stream():
  59. global server
  60. server.start()
  61. res = server.make_stream_request("POST", "/v1/completions", data={
  62. "n_predict": 256,
  63. "prompt": "Once",
  64. "stream": True,
  65. })
  66. content = ""
  67. for data in res:
  68. choice = data["choices"][0]
  69. if choice["finish_reason"] == "length":
  70. assert len(content) > 0
  71. else:
  72. assert choice["finish_reason"] is None
  73. content += choice["text"]