char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
1d36b3670
char/aliceai-t5-moe
1import sse from 'k6/x/sse' 2import { check , sleep } from 'k6' 3import { SharedArray } from 'k6/data' 4import { Counter , Rate , Trend } from 'k6/metrics' 5import exec from 'k6/execution' ; 6 7// Server chat completions prefix 8const server_url = __ENV . SERVER_BENCH_URL ?__ENV . SERVER_BENCH_URL :'http://localhost:8080/v1' 9 10// Number of total prompts in the dataset - default 10m / 10 seconds/request * number of users 11const n_prompt = __ENV . SERVER_BENCH_N_PROMPTS ?parseInt ( __ENV . SERVER_BENCH_N_PROMPTS ) :600 / 10 * 8 12 13// Model name to request 14const model = __ENV . SERVER_BENCH_MODEL_ALIAS ?__ENV . SERVER_BENCH_MODEL_ALIAS :'my-model' 15 16// Dataset path 17const dataset_path = __ENV . SERVER_BENCH_DATASET ?__ENV . SERVER_BENCH_DATASET :'./ShareGPT_V3_unfiltered_cleaned_split.json' 18 19// Max tokens to predict 20const max_tokens = __ENV . SERVER_BENCH_MAX_TOKENS ?parseInt ( __ENV . SERVER_BENCH_MAX_TOKENS ) :512 21 22// Max prompt tokens 23const n_prompt_tokens = __ENV . SERVER_BENCH_MAX_PROMPT_TOKENS ?parseInt ( __ENV . SERVER_BENCH_MAX_PROMPT_TOKENS ) :1024 24 25// Max slot context 26const n_ctx_slot = __ENV . SERVER_BENCH_MAX_CONTEXT ?parseInt ( __ENV . SERVER_BENCH_MAX_CONTEXT ) :2048 27 28export function setup () { 29console . info ( `Benchmark config: server_url= ${ server_url } n_prompt= ${ n_prompt } model= ${ model } dataset_path= ${ dataset_path } max_tokens= ${ max_tokens } ` ) 30} 31 32const data = new SharedArray ( 'conversations' , function () { 33const tokenizer = ( message ) => message . split ( / [ \s ,'".? ] / ) 34 35return JSON . parse ( open ( dataset_path )) 36// Filter out the conversations with less than 2 turns. 37. filter ( data => data [ "conversations" ]. length >= 2 ) 38. filter ( data => data [ "conversations" ][ 0 ][ "from" ] === "human" ) 39. map ( data => { 40return { 41prompt :data [ "conversations" ][ 0 ][ "value" ], 42n_prompt_tokens :tokenizer ( data [ "conversations" ][ 0 ][ "value" ]). length , 43n_completion_tokens :tokenizer ( data [ "conversations" ][ 1 ][ "value" ]). length , 44} 45}) 46// Filter out too short sequences 47. filter ( conv => conv . n_prompt_tokens >= 4 && conv . n_completion_tokens >= 4 ) 48// Filter out too long sequences. 49. filter ( conv => conv . n_prompt_tokens <= n_prompt_tokens && conv . n_prompt_tokens + conv . n_completion_tokens <= n_ctx_slot ) 50// Keep only first n prompts 51. slice ( 0 , n_prompt ) 52}) 53 54const llamacpp_prompt_tokens = new Trend ( 'llamacpp_prompt_tokens' ) 55const llamacpp_completion_tokens = new Trend ( 'llamacpp_completion_tokens' ) 56 57const llamacpp_tokens_second = new Trend ( 'llamacpp_tokens_second' ) 58const llamacpp_prompt_processing_second = new Trend ( 'llamacpp_prompt_processing_second' ) 59const llamacpp_emit_first_token_second = new Trend ( 'llamacpp_emit_first_token_second' ) 60 61const llamacpp_prompt_tokens_total_counter = new Counter ( 'llamacpp_prompt_tokens_total_counter' ) 62const llamacpp_completion_tokens_total_counter = new Counter ( 'llamacpp_completion_tokens_total_counter' ) 63 64const llamacpp_completions_truncated_rate = new Rate ( 'llamacpp_completions_truncated_rate' ) 65const llamacpp_completions_stop_rate = new Rate ( 'llamacpp_completions_stop_rate' ) 66 67export const options = { 68thresholds :{ 69llamacpp_completions_truncated_rate :[ 70// more than 80% of truncated input will abort the test 71{ threshold :'rate < 0.8' , abortOnFail :true , delayAbortEval :'1m' }, 72], 73}, 74duration :'10m' , 75vus :8 , 76} 77 78export default function () { 79const conversation = data [ exec . scenario . iterationInInstance % data . length ] 80const payload = { 81"messages" :[ 82{ 83"role" :"system" , 84"content" :"You are ChatGPT, an AI assistant." , 85}, 86{ 87"role" :"user" , 88"content" :conversation . prompt , 89} 90], 91"model" :model , 92"stream" :true , 93"stream_options" :{ 94"include_usage" :true , // False to be supported in llama.cpp server 95}, 96"seed" :42 , 97"max_tokens" :max_tokens , 98"stop" :[ "<|im_end|>" ] // This is temporary for phi-2 base (i.e. not instructed) since the server expects that the model always to emit BOS 99} 100 101const params = { method :'POST' , body :JSON . stringify ( payload )}; 102 103const startTime = new Date () 104let promptEvalEndTime = null 105let prompt_tokens = 0 106let completions_tokens = 0 107let finish_reason = null 108const res = sse . open ( ` ${ server_url } /chat/completions` , params , function ( client ) { 109client . on ( 'event' , function ( event ) { 110if ( promptEvalEndTime == null ) { 111promptEvalEndTime = new Date () 112llamacpp_emit_first_token_second . add (( promptEvalEndTime - startTime ) / 1.e3 ) 113} 114 115if ( event . data === '[DONE]' || event . data === '' ) { 116return 117} 118 119let chunk = JSON . parse ( event . data ) 120 121if ( chunk . choices && chunk . choices . length > 0 ) { 122let choice = chunk . choices [ 0 ] 123if ( choice . finish_reason ) { 124finish_reason = choice . finish_reason 125} 126} 127 128if ( chunk . usage ) { 129prompt_tokens = chunk . usage . prompt_tokens 130llamacpp_prompt_tokens . add ( prompt_tokens ) 131llamacpp_prompt_tokens_total_counter . add ( prompt_tokens ) 132 133completions_tokens = chunk . usage . completion_tokens 134llamacpp_completion_tokens . add ( completions_tokens ) 135llamacpp_completion_tokens_total_counter . add ( completions_tokens ) 136} 137}) 138 139client . on ( 'error' , function ( e ) { 140console . log ( 'An unexpected error occurred: ' , e . error ()); 141throw e ; 142}) 143}) 144 145check ( res , { 'success completion' :( r ) => r . status === 200 }) 146 147const endTime = new Date () 148 149const promptEvalTime = promptEvalEndTime - startTime 150if ( promptEvalTime > 0 ) { 151llamacpp_prompt_processing_second . add ( prompt_tokens / ( promptEvalEndTime - startTime ) * 1.e3 ) 152} 153 154const completion_time = endTime - promptEvalEndTime 155if ( completions_tokens > 0 && completion_time > 0 ) { 156llamacpp_tokens_second . add ( completions_tokens / completion_time * 1.e3 ) 157} 158llamacpp_completions_truncated_rate . add ( finish_reason === 'length' ) 159llamacpp_completions_stop_rate . add ( finish_reason === 'stop' ) 160 161sleep ( 0.3 ) 162}