Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf : safer pointer handling + naming update
Browse files Browse the repository at this point in the history
ggml-ci
ggerganov committed Sep 12, 2024
1 parent f42de24 commit 7362f28
Showing 20 changed files with 71 additions and 63 deletions.
2 changes: 1 addition & 1 deletion common/common.cpp
Original file line number Diff line number Diff line change
@@ -828,7 +828,7 @@ struct llama_init_result llama_init_from_gpt_params(gpt_params & params) {
}
llama_kv_cache_clear(lctx);
llama_synchronize(lctx);
llama_perf_reset_context(lctx);
llama_perf_context_reset(lctx);
}

iparams.model = model;
4 changes: 2 additions & 2 deletions common/sampling.cpp
Original file line number Diff line number Diff line change
@@ -257,10 +257,10 @@ void gpt_perf_print(const struct llama_context * ctx, const struct gpt_sampler *
// TODO: measure grammar performance

if (gsmpl) {
llama_perf_print_sampler(gsmpl->chain);
llama_perf_sampler_print(gsmpl->chain);
}
if (ctx) {
llama_perf_print_context(ctx);
llama_perf_context_print(ctx);
}
}

2 changes: 1 addition & 1 deletion examples/batched-bench/batched-bench.cpp
Original file line number Diff line number Diff line change
@@ -209,7 +209,7 @@ int main(int argc, char ** argv) {
}

LOG_TEE("\n");
llama_perf_print_context(ctx);
llama_perf_context_print(ctx);

llama_batch_free(batch);

4 changes: 2 additions & 2 deletions examples/batched.swift/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -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")

llama_perf_print_sampler(smpl)
llama_perf_print_context(context)
llama_perf_sampler_print(smpl)
llama_perf_context_print(context)

private func tokenize(text: String, add_bos: Bool) -> [llama_token] {
let utf8Count = text.utf8.count
4 changes: 2 additions & 2 deletions examples/batched/batched.cpp
Original file line number Diff line number Diff line change
@@ -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));

LOG_TEE("\n");
llama_perf_print_sampler(smpl);
llama_perf_print_context(ctx);
llama_perf_sampler_print(smpl);
llama_perf_context_print(ctx);

fprintf(stderr, "\n");

2 changes: 1 addition & 1 deletion examples/embedding/embedding.cpp
Original file line number Diff line number Diff line change
@@ -308,7 +308,7 @@ int main(int argc, char ** argv) {
}

LOG_TEE("\n");
llama_perf_print_context(ctx);
llama_perf_context_print(ctx);

// clean up
llama_batch_free(batch);
2 changes: 1 addition & 1 deletion examples/eval-callback/eval-callback.cpp
Original file line number Diff line number Diff line change
@@ -182,7 +182,7 @@ int main(int argc, char ** argv) {
}

LOG_TEE("\n");
llama_perf_print_context(ctx);
llama_perf_context_print(ctx);

llama_free(ctx);
llama_free_model(model);
2 changes: 1 addition & 1 deletion examples/imatrix/imatrix.cpp
Original file line number Diff line number Diff line change
@@ -637,7 +637,7 @@ int main(int argc, char ** argv) {
g_collector.save_imatrix();

LOG_TEE("\n");
llama_perf_print_context(ctx);
llama_perf_context_print(ctx);

llama_free(ctx);
llama_free_model(model);
2 changes: 1 addition & 1 deletion examples/llama-bench/llama-bench.cpp
Original file line number Diff line number Diff line change
@@ -1630,7 +1630,7 @@ int main(int argc, char ** argv) {
fflush(p_err->fout);
}

llama_perf_print_context(ctx);
llama_perf_context_print(ctx);

llama_free(ctx);

4 changes: 2 additions & 2 deletions examples/llava/llava-cli.cpp
Original file line number Diff line number Diff line change
@@ -308,7 +308,7 @@ int main(int argc, char ** argv) {
// process the prompt
process_prompt(ctx_llava, image_embed, &params, params.prompt);

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

llama_perf_print_context(ctx_llava->ctx_llama);
llama_perf_context_print(ctx_llava->ctx_llama);
llava_image_embed_free(image_embed);
ctx_llava->model = NULL;
llava_free(ctx_llava);
2 changes: 1 addition & 1 deletion examples/llava/minicpmv-cli.cpp
Original file line number Diff line number Diff line change
@@ -319,7 +319,7 @@ int main(int argc, char ** argv) {
}
}
printf("\n");
llama_perf_print_context(ctx_llava->ctx_llama);
llama_perf_context_print(ctx_llava->ctx_llama);

ctx_llava->model = NULL;
llava_free(ctx_llava);
2 changes: 1 addition & 1 deletion examples/parallel/parallel.cpp
Original file line number Diff line number Diff line change
@@ -415,7 +415,7 @@ int main(int argc, char ** argv) {
LOG_TEE("\n");

// TODO: print sampling/grammar timings for all clients
llama_perf_print_context(ctx);
llama_perf_context_print(ctx);

llama_batch_free(batch);

2 changes: 1 addition & 1 deletion examples/passkey/passkey.cpp
Original file line number Diff line number Diff line change
@@ -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));

LOG_TEE("\n");
llama_perf_print_context(ctx);
llama_perf_context_print(ctx);

fprintf(stderr, "\n");

2 changes: 1 addition & 1 deletion examples/perplexity/perplexity.cpp
Original file line number Diff line number Diff line change
@@ -2049,7 +2049,7 @@ int main(int argc, char ** argv) {
}

LOG_TEE("\n");
llama_perf_print_context(ctx);
llama_perf_context_print(ctx);
write_logfile(ctx, params, model, results);

llama_free(ctx);
2 changes: 1 addition & 1 deletion examples/retrieval/retrieval.cpp
Original file line number Diff line number Diff line change
@@ -292,7 +292,7 @@ int main(int argc, char ** argv) {
}

LOG_TEE("\n");
llama_perf_print_context(ctx);
llama_perf_context_print(ctx);

// clean up
llama_batch_free(query_batch);
4 changes: 2 additions & 2 deletions examples/simple/simple.cpp
Original file line number Diff line number Diff line change
@@ -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));

LOG_TEE("\n");
llama_perf_print_sampler(smpl);
llama_perf_print_context(ctx);
llama_perf_sampler_print(smpl);
llama_perf_context_print(ctx);

fprintf(stderr, "\n");

2 changes: 1 addition & 1 deletion examples/speculative/speculative.cpp
Original file line number Diff line number Diff line change
@@ -616,7 +616,7 @@ int main(int argc, char ** argv) {

LOG_TEE("\ndraft:\n\n");
// TODO: print sampling/grammar timings for all drafts
llama_perf_print_context(ctx_dft);
llama_perf_context_print(ctx_dft);

LOG_TEE("\ntarget:\n\n");
gpt_perf_print(ctx_tgt, smpl);
18 changes: 9 additions & 9 deletions include/llama.h
Original file line number Diff line number Diff line change
@@ -1169,7 +1169,7 @@ extern "C" {
// NOTE: Used by llama.cpp examples, avoid using in third-party apps. Instead, do your own performance measurements.
//

struct llama_perf_data_context {
struct llama_perf_context_data {
double t_start_ms;
double t_load_ms;
double t_p_eval_ms;
@@ -1179,20 +1179,20 @@ extern "C" {
int32_t n_eval;
};

struct llama_perf_data_sampler {
struct llama_perf_sampler_data {
double t_sample_ms;

int32_t n_sample;
};

LLAMA_API struct llama_perf_data_context llama_perf_context(const struct llama_context * ctx);
LLAMA_API struct llama_perf_data_sampler llama_perf_sampler(const struct llama_sampler * chain);
LLAMA_API struct llama_perf_context_data llama_perf_context (const struct llama_context * ctx);
LLAMA_API void llama_perf_context_print(const struct llama_context * ctx);
LLAMA_API void llama_perf_context_reset( struct llama_context * ctx);

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_reset_context(struct llama_context * ctx);
LLAMA_API void llama_perf_reset_sampler(struct llama_sampler * chain);
// NOTE: the following work only with samplers constructed via llama_sampler_chain_init
LLAMA_API struct llama_perf_sampler_data llama_perf_sampler (const struct llama_sampler * chain);
LLAMA_API void llama_perf_sampler_print(const struct llama_sampler * chain);
LLAMA_API void llama_perf_sampler_reset( struct llama_sampler * chain);

LLAMA_API void llama_perf_dump_yaml(FILE * stream, const struct llama_context * ctx);

36 changes: 36 additions & 0 deletions src/llama-sampling.cpp
Original file line number Diff line number Diff line change
@@ -1599,3 +1599,39 @@ struct llama_sampler * llama_sampler_init_logit_bias(
},
};
}

// perf

struct llama_perf_sampler_data llama_perf_sampler(const struct llama_sampler * chain) {
struct llama_perf_sampler_data data = {};

if (chain == nullptr || chain->iface != &llama_sampler_chain_i) {
// TODO: return empty data, or GGML_ABORT() ?
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_sampler_print(const struct llama_sampler * chain) {
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",
__func__, data.t_sample_ms, data.n_sample, data.t_sample_ms / data.n_sample, 1e3 / data.t_sample_ms * data.n_sample);
}

void llama_perf_sampler_reset(struct llama_sampler * chain) {
if (chain == nullptr || chain->iface != &llama_sampler_chain_i) {
// TODO: return empty data, or GGML_ABORT() ?
return;
}

auto * p = (struct llama_sampler_chain *) chain->ctx;

p->t_sample_us = p->n_sample = 0;
}
36 changes: 4 additions & 32 deletions src/llama.cpp
Original file line number Diff line number Diff line change
@@ -20687,8 +20687,8 @@ const char * llama_print_system_info(void) {
return s.c_str();
}

struct llama_perf_data_context llama_perf_context(const struct llama_context * ctx) {
struct llama_perf_data_context data = {};
struct llama_perf_context_data llama_perf_context(const struct llama_context * ctx) {
struct llama_perf_context_data data = {};

if (ctx == nullptr) {
return data;
@@ -20704,22 +20704,7 @@ struct llama_perf_data_context llama_perf_context(const struct llama_context * c
return data;
}

struct llama_perf_data_sampler llama_perf_sampler(const struct llama_sampler * chain) {
struct llama_perf_data_sampler data = {};

if (chain == nullptr) {
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) {
void llama_perf_context_print(const struct llama_context * ctx) {
const auto data = llama_perf_context(ctx);

const double t_end_ms = 1e-3 * ggml_time_us();
@@ -20732,25 +20717,12 @@ void llama_perf_print_context(const struct llama_context * ctx) {
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));
}

void llama_perf_print_sampler(const struct llama_sampler * chain) {
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",
__func__, data.t_sample_ms, data.n_sample, data.t_sample_ms / data.n_sample, 1e3 / data.t_sample_ms * data.n_sample);
}

void llama_perf_reset_context(struct llama_context * ctx) {
void llama_perf_context_reset(struct llama_context * ctx) {
ctx->t_start_us = ggml_time_us();
ctx->t_eval_us = ctx->n_eval = 0;
ctx->t_p_eval_us = ctx->n_p_eval = 0;
}

void llama_perf_reset_sampler(struct llama_sampler * chain) {
auto * p = (struct llama_sampler_chain *) chain->ctx;

p->t_sample_us = p->n_sample = 0;
}

void llama_perf_dump_yaml(FILE * stream, const llama_context * ctx) {
fprintf(stream, "\n");
fprintf(stream, "###########\n");

0 comments on commit 7362f28

Please sign in to comment.