perf : separate functions in the API

ggml-ci
This commit is contained in:
Georgi Gerganov 2024-09-11 09:56:41 +03:00
parent fd46535314
commit f42de2426e
No known key found for this signature in database
GPG key ID: 449E073F9DC10735
20 changed files with 79 additions and 101 deletions

View file

@ -828,7 +828,7 @@ struct llama_init_result llama_init_from_gpt_params(gpt_params & params) {
} }
llama_kv_cache_clear(lctx); llama_kv_cache_clear(lctx);
llama_synchronize(lctx); llama_synchronize(lctx);
llama_perf_reset(lctx, LLAMA_PERF_TYPE_CONTEXT); llama_perf_reset_context(lctx);
} }
iparams.model = model; iparams.model = model;

View file

@ -257,10 +257,10 @@ void gpt_perf_print(const struct llama_context * ctx, const struct gpt_sampler *
// TODO: measure grammar performance // TODO: measure grammar performance
if (gsmpl) { if (gsmpl) {
llama_perf_print(gsmpl->chain, LLAMA_PERF_TYPE_SAMPLER_CHAIN); llama_perf_print_sampler(gsmpl->chain);
} }
if (ctx) { if (ctx) {
llama_perf_print(ctx, LLAMA_PERF_TYPE_CONTEXT); llama_perf_print_context(ctx);
} }
} }

View file

@ -209,7 +209,7 @@ int main(int argc, char ** argv) {
} }
LOG_TEE("\n"); LOG_TEE("\n");
llama_perf_print(ctx, LLAMA_PERF_TYPE_CONTEXT); llama_perf_print_context(ctx);
llama_batch_free(batch); llama_batch_free(batch);

View file

@ -200,8 +200,8 @@ let t_main_end = ggml_time_us()
print("decoded \(n_decode) tokens in \(String(format: "%.2f", Double(t_main_end - t_main_start) / 1_000_000.0)) s, speed: \(String(format: "%.2f", Double(n_decode) / (Double(t_main_end - t_main_start) / 1_000_000.0))) t/s\n\n") print("decoded \(n_decode) tokens in \(String(format: "%.2f", Double(t_main_end - t_main_start) / 1_000_000.0)) s, speed: \(String(format: "%.2f", Double(n_decode) / (Double(t_main_end - t_main_start) / 1_000_000.0))) t/s\n\n")
llama_perf_print(UnsafeRawPointer(context), LLAMA_PERF_TYPE_CONTEXT) llama_perf_print_sampler(smpl)
llama_perf_print(UnsafeRawPointer(smpl), LLAMA_PERF_TYPE_SAMPLER_CHAIN) llama_perf_print_context(context)
private func tokenize(text: String, add_bos: Bool) -> [llama_token] { private func tokenize(text: String, add_bos: Bool) -> [llama_token] {
let utf8Count = text.utf8.count let utf8Count = text.utf8.count

View file

@ -229,8 +229,8 @@ int main(int argc, char ** argv) {
__func__, n_decode, (t_main_end - t_main_start) / 1000000.0f, n_decode / ((t_main_end - t_main_start) / 1000000.0f)); __func__, n_decode, (t_main_end - t_main_start) / 1000000.0f, n_decode / ((t_main_end - t_main_start) / 1000000.0f));
LOG_TEE("\n"); LOG_TEE("\n");
llama_perf_print(smpl, LLAMA_PERF_TYPE_SAMPLER_CHAIN); llama_perf_print_sampler(smpl);
llama_perf_print(ctx, LLAMA_PERF_TYPE_CONTEXT); llama_perf_print_context(ctx);
fprintf(stderr, "\n"); fprintf(stderr, "\n");

View file

@ -308,7 +308,7 @@ int main(int argc, char ** argv) {
} }
LOG_TEE("\n"); LOG_TEE("\n");
llama_perf_print(ctx, LLAMA_PERF_TYPE_CONTEXT); llama_perf_print_context(ctx);
// clean up // clean up
llama_batch_free(batch); llama_batch_free(batch);

View file

@ -182,7 +182,7 @@ int main(int argc, char ** argv) {
} }
LOG_TEE("\n"); LOG_TEE("\n");
llama_perf_print(ctx, LLAMA_PERF_TYPE_CONTEXT); llama_perf_print_context(ctx);
llama_free(ctx); llama_free(ctx);
llama_free_model(model); llama_free_model(model);

View file

@ -637,7 +637,7 @@ int main(int argc, char ** argv) {
g_collector.save_imatrix(); g_collector.save_imatrix();
LOG_TEE("\n"); LOG_TEE("\n");
llama_perf_print(ctx, LLAMA_PERF_TYPE_CONTEXT); llama_perf_print_context(ctx);
llama_free(ctx); llama_free(ctx);
llama_free_model(model); llama_free_model(model);

View file

@ -1630,7 +1630,7 @@ int main(int argc, char ** argv) {
fflush(p_err->fout); fflush(p_err->fout);
} }
llama_perf_print(ctx, LLAMA_PERF_TYPE_CONTEXT); llama_perf_print_context(ctx);
llama_free(ctx); llama_free(ctx);

View file

@ -308,7 +308,7 @@ int main(int argc, char ** argv) {
// process the prompt // process the prompt
process_prompt(ctx_llava, image_embed, &params, params.prompt); process_prompt(ctx_llava, image_embed, &params, params.prompt);
llama_perf_print(ctx_llava->ctx_llama, LLAMA_PERF_TYPE_CONTEXT); llama_perf_print_context(ctx_llava->ctx_llama);
llava_image_embed_free(image_embed); llava_image_embed_free(image_embed);
ctx_llava->model = NULL; ctx_llava->model = NULL;
llava_free(ctx_llava); llava_free(ctx_llava);
@ -325,7 +325,7 @@ int main(int argc, char ** argv) {
// process the prompt // process the prompt
process_prompt(ctx_llava, image_embed, &params, params.prompt); process_prompt(ctx_llava, image_embed, &params, params.prompt);
llama_perf_print(ctx_llava->ctx_llama, LLAMA_PERF_TYPE_CONTEXT); llama_perf_print_context(ctx_llava->ctx_llama);
llava_image_embed_free(image_embed); llava_image_embed_free(image_embed);
ctx_llava->model = NULL; ctx_llava->model = NULL;
llava_free(ctx_llava); llava_free(ctx_llava);

View file

@ -319,7 +319,7 @@ int main(int argc, char ** argv) {
} }
} }
printf("\n"); printf("\n");
llama_perf_print(ctx_llava->ctx_llama, LLAMA_PERF_TYPE_CONTEXT); llama_perf_print_context(ctx_llava->ctx_llama);
ctx_llava->model = NULL; ctx_llava->model = NULL;
llava_free(ctx_llava); llava_free(ctx_llava);

View file

@ -240,8 +240,7 @@ int main(int argc, char ** argv){
LOG_TEE("accept = %.3f%%\n", 100.0f * n_accept / n_drafted); LOG_TEE("accept = %.3f%%\n", 100.0f * n_accept / n_drafted);
LOG_TEE("\ntarget:\n\n"); LOG_TEE("\ntarget:\n\n");
llama_perf_print(smpl, LLAMA_PERF_TYPE_SAMPLER_CHAIN); gpt_perf_print(ctx, smpl);
llama_perf_print(ctx, LLAMA_PERF_TYPE_CONTEXT);
gpt_sampler_free(smpl); gpt_sampler_free(smpl);

View file

@ -415,7 +415,7 @@ int main(int argc, char ** argv) {
LOG_TEE("\n"); LOG_TEE("\n");
// TODO: print sampling/grammar timings for all clients // TODO: print sampling/grammar timings for all clients
llama_perf_print(ctx, LLAMA_PERF_TYPE_CONTEXT); llama_perf_print_context(ctx);
llama_batch_free(batch); llama_batch_free(batch);

View file

@ -256,7 +256,7 @@ int main(int argc, char ** argv) {
__func__, n_decode, (t_main_end - t_main_start) / 1000000.0f, n_decode / ((t_main_end - t_main_start) / 1000000.0f)); __func__, n_decode, (t_main_end - t_main_start) / 1000000.0f, n_decode / ((t_main_end - t_main_start) / 1000000.0f));
LOG_TEE("\n"); LOG_TEE("\n");
llama_perf_print(ctx, LLAMA_PERF_TYPE_CONTEXT); llama_perf_print_context(ctx);
fprintf(stderr, "\n"); fprintf(stderr, "\n");

View file

@ -2049,7 +2049,7 @@ int main(int argc, char ** argv) {
} }
LOG_TEE("\n"); LOG_TEE("\n");
llama_perf_print(ctx, LLAMA_PERF_TYPE_CONTEXT); llama_perf_print_context(ctx);
write_logfile(ctx, params, model, results); write_logfile(ctx, params, model, results);
llama_free(ctx); llama_free(ctx);

View file

@ -292,7 +292,7 @@ int main(int argc, char ** argv) {
} }
LOG_TEE("\n"); LOG_TEE("\n");
llama_perf_print(ctx, LLAMA_PERF_TYPE_CONTEXT); llama_perf_print_context(ctx);
// clean up // clean up
llama_batch_free(query_batch); llama_batch_free(query_batch);

View file

@ -154,8 +154,8 @@ int main(int argc, char ** argv) {
__func__, n_decode, (t_main_end - t_main_start) / 1000000.0f, n_decode / ((t_main_end - t_main_start) / 1000000.0f)); __func__, n_decode, (t_main_end - t_main_start) / 1000000.0f, n_decode / ((t_main_end - t_main_start) / 1000000.0f));
LOG_TEE("\n"); LOG_TEE("\n");
llama_perf_print(smpl, LLAMA_PERF_TYPE_SAMPLER_CHAIN); llama_perf_print_sampler(smpl);
llama_perf_print(ctx, LLAMA_PERF_TYPE_CONTEXT); llama_perf_print_context(ctx);
fprintf(stderr, "\n"); fprintf(stderr, "\n");

View file

@ -616,7 +616,7 @@ int main(int argc, char ** argv) {
LOG_TEE("\ndraft:\n\n"); LOG_TEE("\ndraft:\n\n");
// TODO: print sampling/grammar timings for all drafts // TODO: print sampling/grammar timings for all drafts
llama_perf_print(ctx_dft, LLAMA_PERF_TYPE_CONTEXT); llama_perf_print_context(ctx_dft);
LOG_TEE("\ntarget:\n\n"); LOG_TEE("\ntarget:\n\n");
gpt_perf_print(ctx_tgt, smpl); gpt_perf_print(ctx_tgt, smpl);

View file

@ -1169,9 +1169,7 @@ extern "C" {
// NOTE: Used by llama.cpp examples, avoid using in third-party apps. Instead, do your own performance measurements. // NOTE: Used by llama.cpp examples, avoid using in third-party apps. Instead, do your own performance measurements.
// //
// performance timing information struct llama_perf_data_context {
struct llama_perf_data {
// llama_context
double t_start_ms; double t_start_ms;
double t_load_ms; double t_load_ms;
double t_p_eval_ms; double t_p_eval_ms;
@ -1179,22 +1177,22 @@ extern "C" {
int32_t n_p_eval; int32_t n_p_eval;
int32_t n_eval; int32_t n_eval;
};
// llama_sampler_chain struct llama_perf_data_sampler {
double t_sample_ms; double t_sample_ms;
int32_t n_sample; int32_t n_sample;
}; };
enum llama_perf_type { LLAMA_API struct llama_perf_data_context llama_perf_context(const struct llama_context * ctx);
LLAMA_PERF_TYPE_CONTEXT = 0, LLAMA_API struct llama_perf_data_sampler llama_perf_sampler(const struct llama_sampler * chain);
LLAMA_PERF_TYPE_SAMPLER_CHAIN = 1,
};
LLAMA_API struct llama_perf_data llama_perf_get(const void * ctx, enum llama_perf_type type); LLAMA_API void llama_perf_print_context(const struct llama_context * ctx);
LLAMA_API void llama_perf_print_sampler(const struct llama_sampler * chain);
LLAMA_API void llama_perf_print(const void * ctx, enum llama_perf_type type); LLAMA_API void llama_perf_reset_context(struct llama_context * ctx);
LLAMA_API void llama_perf_reset( void * ctx, enum llama_perf_type type); LLAMA_API void llama_perf_reset_sampler(struct llama_sampler * chain);
LLAMA_API void llama_perf_dump_yaml(FILE * stream, const struct llama_context * ctx); LLAMA_API void llama_perf_dump_yaml(FILE * stream, const struct llama_context * ctx);

View file

@ -20687,45 +20687,40 @@ const char * llama_print_system_info(void) {
return s.c_str(); return s.c_str();
} }
llama_perf_data llama_perf_get(const void * ctx, enum llama_perf_type type) { struct llama_perf_data_context llama_perf_context(const struct llama_context * ctx) {
llama_perf_data data = {}; struct llama_perf_data_context data = {};
if (ctx == nullptr) { if (ctx == nullptr) {
return data; return data;
} }
switch (type) { data.t_start_ms = 1e-3 * ctx->t_start_us;
case LLAMA_PERF_TYPE_CONTEXT: data.t_load_ms = 1e-3 * ctx->t_load_us;
{ data.t_p_eval_ms = 1e-3 * ctx->t_p_eval_us;
const auto * p = (const struct llama_context *) ctx; data.t_eval_ms = 1e-3 * ctx->t_eval_us;
data.n_p_eval = std::max(1, ctx->n_p_eval);
data.t_start_ms = 1e-3 * p->t_start_us; data.n_eval = std::max(1, ctx->n_eval);
data.t_load_ms = 1e-3 * p->t_load_us;
data.t_p_eval_ms = 1e-3 * p->t_p_eval_us;
data.t_eval_ms = 1e-3 * p->t_eval_us;
data.n_p_eval = std::max(1, p->n_p_eval);
data.n_eval = std::max(1, p->n_eval);
} break;
case LLAMA_PERF_TYPE_SAMPLER_CHAIN:
{
const auto * smpl = (const struct llama_sampler *) ctx;
const auto * p = (const struct llama_sampler_chain *) smpl->ctx;
data.t_sample_ms = 1e-3 * p->t_sample_us;
data.n_sample = std::max(0, p->n_sample);
} break;
default:
GGML_ABORT("invalid perf type");
}
return data; return data;
} }
void llama_perf_print(const void * ctx, enum llama_perf_type type) { struct llama_perf_data_sampler llama_perf_sampler(const struct llama_sampler * chain) {
switch (type) { struct llama_perf_data_sampler data = {};
case LLAMA_PERF_TYPE_CONTEXT:
{ if (chain == nullptr) {
const auto data = llama_perf_get(ctx, type); return data;
}
const auto * p = (const struct llama_sampler_chain *) chain->ctx;
data.t_sample_ms = 1e-3 * p->t_sample_us;
data.n_sample = std::max(0, p->n_sample);
return data;
}
void llama_perf_print_context(const struct llama_context * ctx) {
const auto data = llama_perf_context(ctx);
const double t_end_ms = 1e-3 * ggml_time_us(); const double t_end_ms = 1e-3 * ggml_time_us();
@ -20735,39 +20730,25 @@ void llama_perf_print(const void * ctx, enum llama_perf_type type) {
LLAMA_LOG_INFO("%s: eval time = %10.2f ms / %5d runs (%8.2f ms per token, %8.2f tokens per second)\n", LLAMA_LOG_INFO("%s: eval time = %10.2f ms / %5d runs (%8.2f ms per token, %8.2f tokens per second)\n",
__func__, data.t_eval_ms, data.n_eval, data.t_eval_ms / data.n_eval, 1e3 / data.t_eval_ms * data.n_eval); __func__, data.t_eval_ms, data.n_eval, data.t_eval_ms / data.n_eval, 1e3 / data.t_eval_ms * data.n_eval);
LLAMA_LOG_INFO("%s: total time = %10.2f ms / %5d tokens\n", __func__, (t_end_ms - data.t_start_ms), (data.n_p_eval + data.n_eval)); LLAMA_LOG_INFO("%s: total time = %10.2f ms / %5d tokens\n", __func__, (t_end_ms - data.t_start_ms), (data.n_p_eval + data.n_eval));
} break; }
case LLAMA_PERF_TYPE_SAMPLER_CHAIN:
{ void llama_perf_print_sampler(const struct llama_sampler * chain) {
const auto data = llama_perf_get(ctx, type); const auto data = llama_perf_sampler(chain);
LLAMA_LOG_INFO("%s: sampling time = %10.2f ms / %5d runs (%8.2f ms per token, %8.2f tokens per second)\n", LLAMA_LOG_INFO("%s: sampling time = %10.2f ms / %5d runs (%8.2f ms per token, %8.2f tokens per second)\n",
__func__, data.t_sample_ms, data.n_sample, data.t_sample_ms / data.n_sample, 1e3 / data.t_sample_ms * data.n_sample); __func__, data.t_sample_ms, data.n_sample, data.t_sample_ms / data.n_sample, 1e3 / data.t_sample_ms * data.n_sample);
} break;
default:
GGML_ABORT("invalid perf type");
}
} }
void llama_perf_reset(void * ctx, enum llama_perf_type type) { void llama_perf_reset_context(struct llama_context * ctx) {
switch (type) { ctx->t_start_us = ggml_time_us();
case LLAMA_PERF_TYPE_CONTEXT: ctx->t_eval_us = ctx->n_eval = 0;
{ ctx->t_p_eval_us = ctx->n_p_eval = 0;
auto * p = (struct llama_context *) ctx; }
p->t_start_us = ggml_time_us(); void llama_perf_reset_sampler(struct llama_sampler * chain) {
p->t_eval_us = p->n_eval = 0; auto * p = (struct llama_sampler_chain *) chain->ctx;
p->t_p_eval_us = p->n_p_eval = 0;
} break;
case LLAMA_PERF_TYPE_SAMPLER_CHAIN:
{
auto * smpl = (struct llama_sampler *) ctx;
auto * p = (struct llama_sampler_chain *) smpl->ctx;
p->t_sample_us = p->n_sample = 0; p->t_sample_us = p->n_sample = 0;
} break;
default:
GGML_ABORT("invalid perf type");
}
} }
void llama_perf_dump_yaml(FILE * stream, const llama_context * ctx) { void llama_perf_dump_yaml(FILE * stream, const llama_context * ctx) {