10
#include <unordered_map>
20
#pragma warning(disable: 4244 4267)
23
static void ggml_graph_compute_helper(std::vector<uint8_t> & buf, ggml_cgraph * graph, int n_threads) {
24
struct ggml_cplan plan = ggml_graph_plan(graph, n_threads, nullptr);
26
if (plan.work_size > 0) {
27
buf.resize(plan.work_size);
28
plan.work_data = buf.data();
31
ggml_graph_compute(graph, &plan);
34
static float tensor_sum_elements(const ggml_tensor * tensor) {
36
if (tensor->type == GGML_TYPE_F32) {
37
for (int j = 0; j < tensor->ne[1]; j++) {
38
for (int k = 0; k < tensor->ne[0]; k++) {
39
sum += ((float *) tensor->data)[j*tensor->ne[0] + k];
46
static void tensor_dump(const ggml_tensor * tensor, const char * name) {
47
printf("%15s: type = %i (%5s) ne = %5" PRIi64 " x %5" PRIi64 " x %5" PRIi64 ", nb = (%5zi, %5zi, %5zi) - ", name,
48
tensor->type, ggml_type_name(tensor->type),
49
tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->nb[0], tensor->nb[1], tensor->nb[2]);
50
float sum = tensor_sum_elements(tensor);
51
printf("Sum of tensor %s is %6.2f\n", name, sum);
54
#define TENSOR_DUMP(tensor) tensor_dump(tensor, #tensor)
56
struct benchmark_params_struct {
58
int32_t n_iterations = 10;
61
static void print_usage(int , char ** argv, struct benchmark_params_struct params) {
62
fprintf(stderr, "usage: %s [options]\n", argv[0]);
63
fprintf(stderr, "\n");
64
fprintf(stderr, "options:\n");
65
fprintf(stderr, " -h, --help show this help message and exit\n");
66
fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);
67
fprintf(stderr, " -i N, --iter N number of iterations to use during computation (default: %d)\n", params.n_iterations);
68
fprintf(stderr, "\n");
71
int main(int argc, char ** argv) {
72
struct benchmark_params_struct benchmark_params;
74
bool invalid_param = false;
76
for (int i = 1; i < argc; i++) {
79
if (arg == "-t" || arg == "--threads") {
84
benchmark_params.n_threads = std::stoi(argv[i]);
85
} else if (arg == "-i" || arg == "--iter") {
90
benchmark_params.n_iterations = std::stoi(argv[i]);
91
} else if (arg == "-h" || arg == "--help") {
92
print_usage(argc, argv, benchmark_params);
97
fprintf(stderr, "error: invalid parameter for argument: %s\n", arg.c_str());
98
print_usage(argc, argv, benchmark_params);
103
printf("Starting Test\n");
106
struct ggml_context * ctx;
110
#undef VERBOSE_DEBUGGING
111
#ifndef VERBOSE_DEBUGGING
112
const int sizey = 4096;
113
const int sizex = 11008;
114
const int sizez = 128;
118
const int sizex = (8*32);
129
const ggml_type qtype = GGML_TYPE_Q4_1;
132
ctx_size += ggml_row_size(GGML_TYPE_F32, sizex*sizey);
133
ctx_size += ggml_row_size(GGML_TYPE_F32, sizex*sizey);
134
ctx_size += ggml_row_size(GGML_TYPE_F32, sizex*sizez);
135
ctx_size += ggml_row_size(qtype, sizex*sizey);
136
ctx_size += ggml_row_size(qtype, sizex*sizey);
137
ctx_size += ggml_row_size(GGML_TYPE_F32, sizex*sizey);
138
ctx_size += ggml_row_size(GGML_TYPE_F32, sizex*sizey);
139
ctx_size += 1024*1024*16;
141
printf("Allocating Memory of size %zi bytes, %zi MB\n",ctx_size, (ctx_size/1024/1024));
143
struct ggml_init_params params = {
149
ctx = ggml_init(params);
151
fprintf(stderr, "%s: ggml_init() failed\n", __func__);
156
printf("Creating new tensors\n");
158
struct ggml_tensor * m11 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, sizex, sizey);
159
ggml_set_f32(m11, 1.0f);
162
struct ggml_tensor * m12 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, sizex, sizey);
163
ggml_set_f32(m12, 1.5f);
166
struct ggml_tensor * m2 = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, sizex, sizez);
167
ggml_set_f32(m2, 2.0f);
169
printf("\n------ Test 1 - Matrix Mult via F32 code\n");
171
struct ggml_tensor * m11xm2 = ggml_mul_mat(ctx, m11, m2);
174
struct ggml_cgraph * gf = ggml_new_graph(ctx);
175
ggml_build_forward_expand(gf, m11xm2);
177
printf("n_threads=%i\n", benchmark_params.n_threads);
182
std::vector<uint8_t> work_buffer;
184
ggml_graph_compute_helper(work_buffer, gf, benchmark_params.n_threads);
186
TENSOR_DUMP(ggml_graph_node(gf, 0));
188
printf("\n------ Test 2 - Matrix Mult via %s code\n", ggml_type_name(qtype));
190
int32_t nelements = sizex*sizey;
194
struct ggml_tensor * q11 = ggml_new_tensor_2d(ctx, qtype, sizex, sizey);
195
ggml_quantize_chunk(qtype, (const float *) m11->data, q11->data, 0, nelements/m11->ne[0], m11->ne[0], nullptr);
199
struct ggml_tensor * q31 = ggml_mul_mat(ctx, q11, m2);
202
struct ggml_cgraph * gf31 = ggml_new_graph(ctx);
203
ggml_build_forward_expand(gf31, q31);
207
struct ggml_tensor * q12 = ggml_new_tensor_2d(ctx, qtype, sizex, sizey);
208
ggml_quantize_chunk(qtype, (const float *) m12->data, q12->data, 0, nelements/m12->ne[0], m12->ne[0], nullptr);
211
struct ggml_tensor * q32 = ggml_mul_mat(ctx, q12, m2);
214
struct ggml_cgraph * gf32 = ggml_new_graph(ctx);
215
ggml_build_forward_expand(gf32, q32);
216
printf("n_threads=%i\n", benchmark_params.n_threads);
218
const int dimx = sizex;
219
const int dimy = sizey;
220
const int dimz = sizez;
221
long long int flops_per_dot_product = dimy + dimy;
222
long long int flops_per_matrix = flops_per_dot_product * dimx * dimz; ;
223
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);
227
float sum_of_F32_reference = tensor_sum_elements(ggml_graph_node(gf, 0));
229
printf("Iteration;NThreads; SizeX; SizeY; SizeZ; Required_FLOPS; Elapsed_u_Seconds; gigaFLOPS\n");
230
printf("=====================================================================================\n");
232
double gflops_sum = 0;
233
for (int i=0;i<benchmark_params.n_iterations ;i++) {
235
long long int start = ggml_time_us();
237
ggml_graph_compute_helper(work_buffer, gf31, benchmark_params.n_threads);
239
long long int stop = ggml_time_us();
240
long long int usec = stop-start;
241
double gflops = (double)(flops_per_matrix)/usec/1000.0;
242
gflops_sum += gflops;
243
printf("%9i;%8i;%6i;%6i;%6i;%15lli;%18lli;%10.2f\n",
245
benchmark_params.n_threads,
246
sizex, sizey, sizez, flops_per_matrix,
249
#ifdef VERBOSE_DEBUGGING
250
TENSOR_DUMP("res",gf31.nodes[0])
255
float sum_of_Q4_result = tensor_sum_elements(ggml_graph_node(gf31, 0));
256
float delta = std::abs(sum_of_Q4_result - sum_of_F32_reference);
257
float allowed_delta = (sum_of_F32_reference) / 1000 / 1000;
259
if (delta > allowed_delta) {
260
printf("\nABORT - ERROR in Matrix Multiplication result - expected %6.2f, got %6.2f (delta %6.2f > allowed_delta %6.2f)\n",
261
sum_of_F32_reference,
270
ggml_graph_compute_helper(work_buffer, gf32, benchmark_params.n_threads);
273
printf("Average%78.2f\n",gflops_sum/((double)benchmark_params.n_iterations));
274
printf("=====================================================================================\n");