|
| 1 | +import pytest |
| 2 | +from utils import * |
| 3 | + |
| 4 | +server = ServerPreset.tinyllama2() |
| 5 | + |
| 6 | +@pytest.fixture(scope="module", autouse=True) |
| 7 | +def create_server(): |
| 8 | + global server |
| 9 | + server = ServerPreset.tinyllama2() |
| 10 | + server.slot_save_path = "./tmp" |
| 11 | + |
| 12 | + |
| 13 | +def test_slot_save_restore(): |
| 14 | + global server |
| 15 | + server.start() |
| 16 | + |
| 17 | + # First prompt in slot 1 should be fully processed |
| 18 | + res = server.make_request("POST", "/completion", data={ |
| 19 | + "prompt": "What is the capital of France?", |
| 20 | + "id_slot": 1, |
| 21 | + "cache_prompt": True, |
| 22 | + }) |
| 23 | + assert res.status_code == 200 |
| 24 | + assert match_regex("(Lily|cake)+", res.body["content"]) |
| 25 | + assert res.body["timings"]["prompt_n"] == 21 # all tokens are processed |
| 26 | + |
| 27 | + # Save state of slot 1 |
| 28 | + res = server.make_request("POST", "/slots/1?action=save", data={ |
| 29 | + "filename": "slot1.bin", |
| 30 | + }) |
| 31 | + assert res.status_code == 200 |
| 32 | + assert res.body["n_saved"] == 84 |
| 33 | + |
| 34 | + # Since we have cache, this should only process the last tokens |
| 35 | + res = server.make_request("POST", "/completion", data={ |
| 36 | + "prompt": "What is the capital of Germany?", |
| 37 | + "id_slot": 1, |
| 38 | + "cache_prompt": True, |
| 39 | + }) |
| 40 | + assert res.status_code == 200 |
| 41 | + assert match_regex("(Jack|said)+", res.body["content"]) |
| 42 | + assert res.body["timings"]["prompt_n"] == 6 # only different part is processed |
| 43 | + |
| 44 | + # Loading the saved cache into slot 0 |
| 45 | + res = server.make_request("POST", "/slots/0?action=restore", data={ |
| 46 | + "filename": "slot1.bin", |
| 47 | + }) |
| 48 | + assert res.status_code == 200 |
| 49 | + assert res.body["n_restored"] == 84 |
| 50 | + |
| 51 | + # Since we have cache, slot 0 should only process the last tokens |
| 52 | + res = server.make_request("POST", "/completion", data={ |
| 53 | + "prompt": "What is the capital of Germany?", |
| 54 | + "id_slot": 0, |
| 55 | + "cache_prompt": True, |
| 56 | + }) |
| 57 | + assert res.status_code == 200 |
| 58 | + assert match_regex("(Jack|said)+", res.body["content"]) |
| 59 | + assert res.body["timings"]["prompt_n"] == 6 # only different part is processed |
| 60 | + |
| 61 | + # For verification that slot 1 was not corrupted during slot 0 load, same thing should work |
| 62 | + res = server.make_request("POST", "/completion", data={ |
| 63 | + "prompt": "What is the capital of Germany?", |
| 64 | + "id_slot": 1, |
| 65 | + "cache_prompt": True, |
| 66 | + }) |
| 67 | + assert res.status_code == 200 |
| 68 | + assert match_regex("(Jack|said)+", res.body["content"]) |
| 69 | + assert res.body["timings"]["prompt_n"] == 1 |
| 70 | + |
| 71 | + |
| 72 | +def test_slot_erase(): |
| 73 | + global server |
| 74 | + server.start() |
| 75 | + |
| 76 | + res = server.make_request("POST", "/completion", data={ |
| 77 | + "prompt": "What is the capital of France?", |
| 78 | + "id_slot": 1, |
| 79 | + "cache_prompt": True, |
| 80 | + }) |
| 81 | + assert res.status_code == 200 |
| 82 | + assert match_regex("(Lily|cake)+", res.body["content"]) |
| 83 | + assert res.body["timings"]["prompt_n"] == 21 # all tokens are processed |
| 84 | + |
| 85 | + # erase slot 1 |
| 86 | + res = server.make_request("POST", "/slots/1?action=erase") |
| 87 | + assert res.status_code == 200 |
| 88 | + |
| 89 | + # re-run the same prompt, it should process all tokens again |
| 90 | + res = server.make_request("POST", "/completion", data={ |
| 91 | + "prompt": "What is the capital of France?", |
| 92 | + "id_slot": 1, |
| 93 | + "cache_prompt": True, |
| 94 | + }) |
| 95 | + assert res.status_code == 200 |
| 96 | + assert match_regex("(Lily|cake)+", res.body["content"]) |
| 97 | + assert res.body["timings"]["prompt_n"] == 21 # all tokens are processed |
0 commit comments