mirror of
https://github.com/ggerganov/llama.cpp.git
synced 2024-12-25 19:04:35 +00:00
42c76d1358
* Introduce ggml_compute_threadpool - OpenMP functional: check - Vanilla ggml functional: Check - ggml w/threadpool functional: Check - OpenMP no regression: No glaring problems - Vanilla ggml no regression: No glaring problems - ggml w/threadpool no regression: No glaring problems * Minor fixes * fixed use after release bug * fixed a harmless race condition * Fix Android bulid issue * fix more race conditions * fix deadlock for cases where cgraph.n_nodes == 1 and fix --poll case * threadpool: use cpu_get_num_math to set the default number of threadpool threads This way we avoid using E-Cores and Hyperthreaded siblings. * bench: create fresh threadpool for each test For benchmarking it's better to start a fresh pool for each test with the exact number of threads needed for that test. Having larger pools is suboptimal (causes more load, etc). * atomics: always use stdatomics with clang and use relaxed memory order when polling in ggml_barrier This also removes sched_yield() calls from ggml_barrier() to match OpenMP behavior. * threadpool: make polling the default to match openmp behavior All command line args now allow for setting poll to 0 (false). * threadpool: do not wakeup threads in already paused threadpool * fix potential race condition in check_for_work * threadpool: do not create two threadpools if their params are identical * threadpool: reduce pause/resume/wakeup overhead in common cases We now start threadpool in paused state only if we have two. The resume is now implicit (ie new work) which allows for reduced locking and context-switch overhead. * threadpool: add support for hybrid polling poll params (--poll, ...) now specify "polling level", i.e. how aggresively we poll before waiting on cond.var. poll=0 means no polling, 1 means poll for 128K rounds then wait, 2 for 256K rounds, ... The default value of 50 (ie 50x128K rounds) seems like a decent default across modern platforms. We can tune this further as things evolve. * threadpool: reduce the number of barrier required New work is now indicated with an atomic counter that is incremented for each new graph that needs to be computed. This removes the need for extra barrier for clearing the "new_work" and removes the special case for trivial graphs. * threadpool: remove special-casing for disposable threadpools With the efficient hybrid polling there is no need to make disposable pools any different. This simplifies the overall logic and reduces branching. Include n_threads in debug print for disposable threadpool. Declare pause and stop flags as atomic_bool This doesn't actually generate any memory barriers and simply informs the thread sanitizer that these flags can be written & read by different threads without locking. * threadpool: do not clear barrier counters between graphs computes (fixes race with small graphs) This fixes the race condition with very small graphs where the main thread happens to start a new graph while the workers are just about to exit from barriers. * threadpool: use relaxed order for chunk sync Full memory barrier is an overkill for this since each thread works on different chunk * threadpool: remove abort_callback from threadpool state * threadpool: better naming for thread/cpumask releated functions * threadpool: consistent use of int type for n_threads params * threadpool: add support for ggml_threadpool_params_default/init Also removes the need for explicit mask_specified param. all-zero cpumask means use default (usually inherited) cpu affinity mask. * threadpool: move typedef into ggml.h * threadpool: fix apply_priority() function name * threadpool: fix swift wrapper errors due to n_threads int type cleanup * threadpool: enable --cpu-mask and other threadpool related options only if threadpool is enabled * threadpool: replace checks for compute_thread ret code with proper status check * threadpool: simplify threadpool init logic and fix main thread affinity application Most of the init code is now exactly the same between threadpool and openmp. * threadpool: update threadpool resume/pause function names * threadpool: enable openmp by default for now * threadpool: don't forget to free workers state when omp is enabled * threadpool: avoid updating process priority on the platforms that do not require it On Windows we need to change overall process priority class in order to set thread priorities, but on Linux, Mac, etc we do not need to touch the overall process settings. * threadpool: update calling thread prio and affinity only at start/resume This avoids extra syscalls for each graph_compute() * llama-bench: turn threadpool params into vectors, add output headers, etc * llama-bench: add support for cool off between tests --delay This helps for long running tests on platforms that are thermally limited (phones, laptops, etc). --delay (disabled by default) introduces the sleep for N seconds before starting each test. * threadpool: move process priority setting into the apps (bench and cli) This avoids changing the overall process priority on Windows for the apps that use ggml/llama.cpp directy. * threadpool: move all pause/resume logic into ggml * threadpool: futher api cleanup and prep for future refactoring All threadpool related functions and structs use ggml_threadpool prefix. * threadpool: minor indent fixes * threadpool: improve setprioty error message * Update examples/llama-bench/llama-bench.cpp Co-authored-by: slaren <slarengh@gmail.com> * threadpool: fix indent in set_threadpool call * use int32_t for n_thread type in public llama.cpp API * threadpool: use _new and _free instead of _create and _release * fix two more public APIs to use int32_t for n_threads * build: set _GNU_SOURCE for Adroid --------- Co-authored-by: Max Krasnyansky <quic_maxk@quicinc.com> Co-authored-by: fmz <quic_fzaghlou@quic.com> Co-authored-by: Max Krasnyansky <max.krasnyansky@gmail.com> Co-authored-by: slaren <slarengh@gmail.com>
276 lines
9.6 KiB
C++
276 lines
9.6 KiB
C++
#include "common.h"
|
|
#include "ggml.h"
|
|
|
|
#include <locale.h>
|
|
#include <assert.h>
|
|
#include <math.h>
|
|
#include <cstring>
|
|
#include <cstdio>
|
|
#include <cinttypes>
|
|
#include <unordered_map>
|
|
#include <queue>
|
|
#include <string.h>
|
|
#include <cassert>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <iterator>
|
|
#include <algorithm>
|
|
|
|
#if defined(_MSC_VER)
|
|
#pragma warning(disable: 4244 4267) // possible loss of data
|
|
#endif
|
|
|
|
static void ggml_graph_compute_helper(std::vector<uint8_t> & buf, ggml_cgraph * graph, int n_threads) {
|
|
struct ggml_cplan plan = ggml_graph_plan(graph, n_threads, nullptr);
|
|
|
|
if (plan.work_size > 0) {
|
|
buf.resize(plan.work_size);
|
|
plan.work_data = buf.data();
|
|
}
|
|
|
|
ggml_graph_compute(graph, &plan);
|
|
}
|
|
|
|
static float tensor_sum_elements(const ggml_tensor * tensor) {
|
|
double sum = 0;
|
|
if (tensor->type == GGML_TYPE_F32) {
|
|
for (int j = 0; j < tensor->ne[1]; j++) {
|
|
for (int k = 0; k < tensor->ne[0]; k++) {
|
|
sum += ((float *) tensor->data)[j*tensor->ne[0] + k];
|
|
}
|
|
}
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
static void tensor_dump(const ggml_tensor * tensor, const char * name) {
|
|
printf("%15s: type = %i (%5s) ne = %5" PRIi64 " x %5" PRIi64 " x %5" PRIi64 ", nb = (%5zi, %5zi, %5zi) - ", name,
|
|
tensor->type, ggml_type_name(tensor->type),
|
|
tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->nb[0], tensor->nb[1], tensor->nb[2]);
|
|
float sum = tensor_sum_elements(tensor);
|
|
printf("Sum of tensor %s is %6.2f\n", name, sum);
|
|
}
|
|
|
|
#define TENSOR_DUMP(tensor) tensor_dump(tensor, #tensor)
|
|
|
|
struct benchmark_params_struct {
|
|
int n_threads = 1;
|
|
int32_t n_iterations = 10;
|
|
};
|
|
|
|
static void print_usage(int /*argc*/, char ** argv, struct benchmark_params_struct params) {
|
|
fprintf(stderr, "usage: %s [options]\n", argv[0]);
|
|
fprintf(stderr, "\n");
|
|
fprintf(stderr, "options:\n");
|
|
fprintf(stderr, " -h, --help show this help message and exit\n");
|
|
fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);
|
|
fprintf(stderr, " -i N, --iter N number of iterations to use during computation (default: %d)\n", params.n_iterations);
|
|
fprintf(stderr, "\n");
|
|
}
|
|
|
|
int main(int argc, char ** argv) {
|
|
struct benchmark_params_struct benchmark_params;
|
|
|
|
bool invalid_param = false;
|
|
std::string arg;
|
|
for (int i = 1; i < argc; i++) {
|
|
arg = argv[i];
|
|
|
|
if (arg == "-t" || arg == "--threads") {
|
|
if (++i >= argc) {
|
|
invalid_param = true;
|
|
break;
|
|
}
|
|
benchmark_params.n_threads = std::stoi(argv[i]);
|
|
} else if (arg == "-i" || arg == "--iter") {
|
|
if (++i >= argc) {
|
|
invalid_param = true;
|
|
break;
|
|
}
|
|
benchmark_params.n_iterations = std::stoi(argv[i]);
|
|
} else if (arg == "-h" || arg == "--help") {
|
|
print_usage(argc, argv, benchmark_params);
|
|
exit(0);
|
|
}
|
|
}
|
|
if (invalid_param) {
|
|
fprintf(stderr, "error: invalid parameter for argument: %s\n", arg.c_str());
|
|
print_usage(argc, argv, benchmark_params);
|
|
exit(1);
|
|
}
|
|
|
|
print_build_info();
|
|
printf("Starting Test\n");
|
|
|
|
// create the ggml context
|
|
struct ggml_context * ctx;
|
|
//const int sizex = 4096;
|
|
//const int sizey = 11008;
|
|
|
|
#undef VERBOSE_DEBUGGING
|
|
#ifndef VERBOSE_DEBUGGING
|
|
const int sizey = 4096;
|
|
const int sizex = 11008;
|
|
const int sizez = 128;
|
|
#else
|
|
/* Working - let's increase size */
|
|
const int sizey = 1;
|
|
const int sizex = (8*32);
|
|
const int sizez = 1;
|
|
|
|
/*const int sizey = 1;
|
|
const int sizex = 3*(8*32);
|
|
const int sizez = 1;*/
|
|
#endif
|
|
|
|
//printf("Memsize required = %i\n", sizex*sizex);
|
|
|
|
// TODO: perform the bench for all types or for a user specified type
|
|
const ggml_type qtype = GGML_TYPE_Q4_1;
|
|
|
|
size_t ctx_size = 0;
|
|
ctx_size += ggml_row_size(GGML_TYPE_F32, sizex*sizey);
|
|
ctx_size += ggml_row_size(GGML_TYPE_F32, sizex*sizey);
|
|
ctx_size += ggml_row_size(GGML_TYPE_F32, sizex*sizez);
|
|
ctx_size += ggml_row_size(qtype, sizex*sizey);
|
|
ctx_size += ggml_row_size(qtype, sizex*sizey);
|
|
ctx_size += ggml_row_size(GGML_TYPE_F32, sizex*sizey); // BLAS
|
|
ctx_size += ggml_row_size(GGML_TYPE_F32, sizex*sizey); // BLAS
|
|
ctx_size += 1024*1024*16;
|
|
|
|
printf("Allocating Memory of size %zi bytes, %zi MB\n",ctx_size, (ctx_size/1024/1024));
|
|
|
|
struct ggml_init_params params = {
|
|
/*.mem_size =*/ ctx_size,
|
|
/*.mem_buffer =*/ NULL,
|
|
/* no_alloc =*/ 0
|
|
};
|
|
|
|
ctx = ggml_init(params);
|
|
if (!ctx) {
|
|
fprintf(stderr, "%s: ggml_init() failed\n", __func__);
|
|
return 1;
|
|
}
|
|
|
|
|
|
printf("Creating new tensors\n");
|
|
// printf("Creating new tensor m1\n");
|
|
struct ggml_tensor * m11 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, sizex, sizey);
|
|
ggml_set_f32(m11, 1.0f);
|
|
|
|
// printf("Creating new tensor m1\n");
|
|
struct ggml_tensor * m12 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, sizex, sizey);
|
|
ggml_set_f32(m12, 1.5f);
|
|
|
|
// printf("Creating new tensor m2\n");
|
|
struct ggml_tensor * m2 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, sizex, sizez);
|
|
ggml_set_f32(m2, 2.0f);
|
|
|
|
printf("\n------ Test 1 - Matrix Mult via F32 code\n");
|
|
// printf("Creating new tensor m11xm2\n");
|
|
struct ggml_tensor * m11xm2 = ggml_mul_mat(ctx, m11, m2);
|
|
|
|
// printf("Creating compute graph\n");
|
|
struct ggml_cgraph * gf = ggml_new_graph(ctx);
|
|
ggml_build_forward_expand(gf, m11xm2);
|
|
|
|
printf("n_threads=%i\n", benchmark_params.n_threads);
|
|
|
|
TENSOR_DUMP(m11);
|
|
TENSOR_DUMP(m2);
|
|
|
|
std::vector<uint8_t> work_buffer;
|
|
|
|
ggml_graph_compute_helper(work_buffer, gf, benchmark_params.n_threads);
|
|
|
|
TENSOR_DUMP(gf->nodes[0]);
|
|
|
|
printf("\n------ Test 2 - Matrix Mult via %s code\n", ggml_type_name(qtype));
|
|
|
|
int32_t nelements = sizex*sizey;
|
|
|
|
// Set up a the benchmark matrices
|
|
// printf("Creating new tensor q11 & Running quantize\n");
|
|
struct ggml_tensor * q11 = ggml_new_tensor_2d(ctx, qtype, sizex, sizey);
|
|
ggml_quantize_chunk(qtype, (const float *) m11->data, q11->data, 0, nelements/m11->ne[0], m11->ne[0], nullptr);
|
|
|
|
// Set up a the compute graph
|
|
// printf("Creating new tensor q31\n");
|
|
struct ggml_tensor * q31 = ggml_mul_mat(ctx, q11, m2);
|
|
|
|
// printf("Creating compute graph\n");
|
|
struct ggml_cgraph * gf31 = ggml_new_graph(ctx);
|
|
ggml_build_forward_expand(gf31, q31);
|
|
|
|
// Set up a second graph computation to make sure we override the CPU cache lines
|
|
// printf("Creating new tensor q12 & Running quantize\n");
|
|
struct ggml_tensor * q12 = ggml_new_tensor_2d(ctx, qtype, sizex, sizey);
|
|
ggml_quantize_chunk(qtype, (const float *) m12->data, q12->data, 0, nelements/m12->ne[0], m12->ne[0], nullptr);
|
|
|
|
// printf("Creating new tensor q32\n");
|
|
struct ggml_tensor * q32 = ggml_mul_mat(ctx, q12, m2);
|
|
|
|
//printf("Creating compute graph\n");
|
|
struct ggml_cgraph * gf32 = ggml_new_graph(ctx);
|
|
ggml_build_forward_expand(gf32, q32);
|
|
printf("n_threads=%i\n", benchmark_params.n_threads);
|
|
|
|
const int dimx = sizex;
|
|
const int dimy = sizey;
|
|
const int dimz = sizez;
|
|
long long int flops_per_dot_product = dimy + dimy;
|
|
long long int flops_per_matrix = flops_per_dot_product * dimx * dimz; ;
|
|
printf("Matrix Multiplication of (%i,%i,%i) x (%i,%i,%i) - about %6.2f gFLOPS\n\n", sizex, sizey, 1, sizex, sizez, 1, 1.0f*flops_per_matrix / 1000 / 1000 / 1000);
|
|
|
|
|
|
// Let's use the F32 result from above as a reference for the quantized multiplication
|
|
float sum_of_F32_reference = tensor_sum_elements(gf->nodes[0]);
|
|
|
|
printf("Iteration;NThreads; SizeX; SizeY; SizeZ; Required_FLOPS; Elapsed_u_Seconds; gigaFLOPS\n");
|
|
printf("=====================================================================================\n");
|
|
|
|
double gflops_sum = 0;
|
|
for (int i=0;i<benchmark_params.n_iterations ;i++) {
|
|
|
|
long long int start = ggml_time_us();
|
|
//printf("Running ggml_graph_compute\n");
|
|
ggml_graph_compute_helper(work_buffer, gf31, benchmark_params.n_threads);
|
|
|
|
long long int stop = ggml_time_us();
|
|
long long int usec = stop-start;
|
|
double gflops = (double)(flops_per_matrix)/usec/1000.0;
|
|
gflops_sum += gflops;
|
|
printf("%9i;%8i;%6i;%6i;%6i;%15lli;%18lli;%10.2f\n",
|
|
i,
|
|
benchmark_params.n_threads,
|
|
sizex, sizey, sizez, flops_per_matrix,
|
|
usec,gflops);
|
|
|
|
#ifdef VERBOSE_DEBUGGING
|
|
TENSOR_DUMP("res",gf31.nodes[0])
|
|
#endif
|
|
|
|
// Check that the matrix multiplication result is in the right ballpark
|
|
// We cannot use the exact value from the F32 multiplication because the quantizuation will be slightly different
|
|
float sum_of_Q4_result = tensor_sum_elements(gf31->nodes[0]);
|
|
float delta = std::abs(sum_of_Q4_result - sum_of_F32_reference);
|
|
float allowed_delta = (sum_of_F32_reference) / 1000 / 1000; // Let's accept an epsilon of 10^-6
|
|
|
|
if (delta > allowed_delta) {
|
|
printf("\nABORT - ERROR in Matrix Multiplication result - expected %6.2f, got %6.2f (delta %6.2f > allowed_delta %6.2f)\n",
|
|
sum_of_F32_reference,
|
|
sum_of_Q4_result,
|
|
delta,
|
|
allowed_delta
|
|
);
|
|
exit(0);
|
|
}
|
|
|
|
// Running a different graph computation to make sure we override the CPU cache lines
|
|
ggml_graph_compute_helper(work_buffer, gf32, benchmark_params.n_threads);
|
|
}
|
|
printf("\n");
|
|
printf("Average%78.2f\n",gflops_sum/((double)benchmark_params.n_iterations));
|
|
printf("=====================================================================================\n");
|
|
}
|