char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
85a7d8677
char/aliceai-t5-moe
1import pytest 2from utils import * 3 4server = ServerPreset .tinyllama2 () 5 6 7SHORT_TEXT = """ 8Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 9Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 10Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 11""" .strip () 12 13LONG_TEXT = """ 14Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 15Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 16Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 17Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 18""" .strip () 19 20@ pytest . fixture ( autouse = True ) 21def create_server (): 22global server 23server = ServerPreset .tinyllama2 () 24server .n_ctx = 512 25server .n_slots = 2 26server .n_predict = 128 27 28 29def test_ctx_shift_enabled (): 30# the prompt is 226 tokens 31# the slot context is 512/2 = 256 tokens 32# 96 tokens are generated thanks to shifting the context when it gets full 33global server 34server .enable_ctx_shift = True 35server .start () 36res = server .make_request ("POST" ,"/completion" ,data = { 37"n_predict" :96 , 38"prompt" :SHORT_TEXT , 39 }) 40assert res .status_code == 200 41assert res .body ["timings" ]["prompt_n" ]== 226 42assert res .body ["timings" ]["predicted_n" ]== 96 43assert res .body ["truncated" ]is True 44 45 46@ pytest . mark . parametrize ( "n_predict,n_token_output,truncated" , [ 47( 64 , 64 , False ), 48( - 1 , 248 , True ), # 8 tokens prompt + 248 tokens generated = 256 tokens total 49]) 50def test_ctx_shift_disabled_short_prompt (n_predict :int ,n_token_output :int ,truncated :bool ): 51global server 52server .n_predict = - 1 53server .start () 54res = server .make_request ("POST" ,"/completion" ,data = { 55"n_predict" :n_predict , 56"prompt" :"Hi how are you" , 57 }) 58assert res .status_code == 200 59assert res .body ["timings" ]["predicted_n" ]== n_token_output 60assert res .body ["truncated" ]== truncated 61 62 63def test_ctx_shift_disabled_long_prompt (): 64global server 65server .start () 66res = server .make_request ("POST" ,"/completion" ,data = { 67"n_predict" :64 , 68"prompt" :LONG_TEXT , 69 }) 70assert res .status_code != 200 71assert "error" in res .body 72assert "exceeds the available context size" in res .body ["error" ]["message" ] 73 74def test_ctx_shift_disabled_stream (): 75global server 76server .start () 77res = server .make_stream_request ("POST" ,"/v1/completions" ,data = { 78"n_predict" :256 , 79"prompt" :"Once" , 80"stream" :True , 81 }) 82content = "" 83for data in res : 84choice = data ["choices" ][0 ] 85if choice ["finish_reason" ]== "length" : 86assert len (content )> 0 87else : 88assert choice ["finish_reason" ]is None 89content += choice ["text" ]