From dc9b9f3272b2cd4522bcf44512c16381dea06ea7 Mon Sep 17 00:00:00 2001 From: netrunnereve <139727413+netrunnereve@users.noreply.github.com> Date: Sat, 29 Jul 2023 13:55:53 -0400 Subject: [PATCH 1/6] fix hellaswag print format, cast away warning in test-double-float --- examples/common.cpp | 2 +- tests/test-double-float.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/common.cpp b/examples/common.cpp index fe7308b1787eb..5a2cbaf8dd4b8 100644 --- a/examples/common.cpp +++ b/examples/common.cpp @@ -566,7 +566,7 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) { fprintf(stdout, " --temp N temperature (default: %.1f)\n", (double)params.temp); fprintf(stdout, " --perplexity compute perplexity over each ctx window of the prompt\n"); fprintf(stdout, " --hellaswag compute HellaSwag score over random tasks from datafile supplied with -f\n"); - fprintf(stdout, " --hellaswag-tasks N number of tasks to use when computing the HellaSwag score (default: %d)\n", params.hellaswag_tasks); + fprintf(stdout, " --hellaswag-tasks N number of tasks to use when computing the HellaSwag score (default: %zu)\n", params.hellaswag_tasks); fprintf(stdout, " --keep N number of tokens to keep from the initial prompt (default: %d, -1 = all)\n", params.n_keep); fprintf(stdout, " --chunks N max number of chunks to process (default: %d, -1 = all)\n", params.n_chunks); if (llama_mlock_supported()) { diff --git a/tests/test-double-float.c b/tests/test-double-float.c index 89dafc9f278dc..cd7bbf666d76a 100644 --- a/tests/test-double-float.c +++ b/tests/test-double-float.c @@ -32,7 +32,8 @@ inline static float silu_float(float x) { int main(void) { uint32_t x = UINT32_MAX; do { - float f = *(float *)&x; + float *y = (float *)&x; + float f = *y; assert(!isfinite(f) || (round_orig(f) == round_float(f))); } while (x--); From 49580fe816a21c7df4400168089193a5867a842f Mon Sep 17 00:00:00 2001 From: netrunnereve <139727413+netrunnereve@users.noreply.github.com> Date: Sat, 29 Jul 2023 14:36:33 -0400 Subject: [PATCH 2/6] c++11 cannot use designated initializers --- tests/test-grad0.c | 9 ++++----- tests/test-opt.c | 10 +++++----- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/tests/test-grad0.c b/tests/test-grad0.c index 6d312216d58af..da2f3dfc6445b 100644 --- a/tests/test-grad0.c +++ b/tests/test-grad0.c @@ -372,11 +372,10 @@ bool check_mat_mul( #define NUM_PERMUTATIONS (4*3*2*1) int main(int argc, const char ** argv) { - struct ggml_init_params params = { - .mem_size = 128*1024*1024, - .mem_buffer = NULL, - .no_alloc = false, - }; + struct ggml_init_params params; + params.mem_size = 128*1024*1024; + params.mem_buffer = NULL; + params.no_alloc = false; int64_t ne[4]; diff --git a/tests/test-opt.c b/tests/test-opt.c index 4eef62bcfb96b..c831a091ad06a 100644 --- a/tests/test-opt.c +++ b/tests/test-opt.c @@ -118,11 +118,11 @@ void set_element(struct ggml_tensor * t, int idx, float value) { } int main(void) { - struct ggml_init_params params = { - .mem_size = 1024*1024*1024, - .mem_buffer = NULL, - .no_alloc = false, - }; + struct ggml_init_params params; + params.mem_size = 1024*1024*1024; + params.mem_buffer = NULL; + params.no_alloc = false; + struct ggml_context * ctx = ggml_init(params); int64_t ne1[4] = {4, 128, 1, 1}; From 0b206788dcceeb700e9090b871a736750dd260c1 Mon Sep 17 00:00:00 2001 From: netrunnereve <139727413+netrunnereve@users.noreply.github.com> Date: Sat, 29 Jul 2023 15:12:05 -0400 Subject: [PATCH 3/6] add static to test-grad0.c internal functions --- tests/test-grad0.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/test-grad0.c b/tests/test-grad0.c index da2f3dfc6445b..1148bd2c90ab4 100644 --- a/tests/test-grad0.c +++ b/tests/test-grad0.c @@ -47,16 +47,16 @@ #define GGML_PRINT(...) printf(__VA_ARGS__) -float frand(void) { +static float frand(void) { return (float)rand()/(float)RAND_MAX; } -int irand(int n) { +static int irand(int n) { if (n == 0) return 0; return rand()%n; } -void get_random_dims(int64_t * dims, int ndims) { +static void get_random_dims(int64_t * dims, int ndims) { dims[0] = dims[1] = dims[2] = dims[3] = 1; for (int i = 0; i < ndims; i++) { @@ -64,7 +64,7 @@ void get_random_dims(int64_t * dims, int ndims) { } } -struct ggml_tensor * get_random_tensor_f32( +static struct ggml_tensor * get_random_tensor_f32( struct ggml_context * ctx0, int ndims, int64_t ne[], @@ -112,7 +112,7 @@ struct ggml_tensor * get_random_tensor_f32( return result; } -struct ggml_tensor * get_random_tensor_f16( +static struct ggml_tensor * get_random_tensor_f16( struct ggml_context * ctx0, int ndims, int64_t ne[], @@ -160,7 +160,7 @@ struct ggml_tensor * get_random_tensor_f16( return result; } -struct ggml_tensor * get_random_tensor_i32( +static struct ggml_tensor * get_random_tensor_i32( struct ggml_context * ctx0, int ndims, int64_t ne[], @@ -208,7 +208,7 @@ struct ggml_tensor * get_random_tensor_i32( return result; } -void print_elements(const char* label, const struct ggml_tensor * t) { +static void print_elements(const char* label, const struct ggml_tensor * t) { if (!t) { printf("%s: %s = null\n", __func__, label); return; @@ -228,7 +228,7 @@ void print_elements(const char* label, const struct ggml_tensor * t) { } -bool check_gradient( +static bool check_gradient( const char * op_name, struct ggml_context * ctx0, struct ggml_tensor * x[], @@ -310,7 +310,7 @@ bool check_gradient( } // TODO: clean-up this .. -bool check_mat_mul( +static bool check_mat_mul( const struct ggml_tensor * y, const struct ggml_tensor * x0, const struct ggml_tensor * x1) { From eab8335e3320bed4294a4ee9c28118b116ebc2e6 Mon Sep 17 00:00:00 2001 From: netrunnereve <139727413+netrunnereve@users.noreply.github.com> Date: Sun, 30 Jul 2023 23:12:25 -0400 Subject: [PATCH 4/6] use memcpy in test-double-float.c --- tests/test-double-float.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test-double-float.c b/tests/test-double-float.c index cd7bbf666d76a..fe04ef2d28c52 100644 --- a/tests/test-double-float.c +++ b/tests/test-double-float.c @@ -7,6 +7,7 @@ #include #include #include +#include #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdouble-promotion" @@ -32,8 +33,8 @@ inline static float silu_float(float x) { int main(void) { uint32_t x = UINT32_MAX; do { - float *y = (float *)&x; - float f = *y; + float f; + memcpy(&f, &x, sizeof(x)); assert(!isfinite(f) || (round_orig(f) == round_float(f))); } while (x--); From 92e60dba8bf6472a16d823e6f3ceb845c07cf89b Mon Sep 17 00:00:00 2001 From: netrunnereve <139727413+netrunnereve@users.noreply.github.com> Date: Mon, 31 Jul 2023 20:59:43 -0400 Subject: [PATCH 5/6] port c tests to c++ --- Makefile | 6 +++--- scripts/sync-ggml.sh | 4 ++-- tests/CMakeLists.txt | 6 +++--- tests/{test-double-float.c => test-double-float.cpp} | 10 +++++----- tests/{test-grad0.c => test-grad0.cpp} | 8 ++++---- tests/{test-opt.c => test-opt.cpp} | 8 ++++---- 6 files changed, 21 insertions(+), 21 deletions(-) rename tests/{test-double-float.c => test-double-float.cpp} (91%) rename tests/{test-grad0.c => test-grad0.cpp} (99%) rename tests/{test-opt.c => test-opt.cpp} (98%) diff --git a/Makefile b/Makefile index 2035c525338a7..ab78f05fec84e 100644 --- a/Makefile +++ b/Makefile @@ -395,13 +395,13 @@ benchmark-matmult: examples/benchmark/benchmark-matmult.cpp build-info.h ggml.o vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS) $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS) -tests/test-double-float: tests/test-double-float.c build-info.h ggml.o llama.o common.o $(OBJS) +tests/test-double-float: tests/test-double-float.cpp build-info.h ggml.o llama.o common.o $(OBJS) $(CXX) $(CXXFLAGS) $(filter-out %.txt,$^) -o $@ $(LDFLAGS) -tests/test-grad0: tests/test-grad0.c build-info.h ggml.o llama.o common.o $(OBJS) +tests/test-grad0: tests/test-grad0.cpp build-info.h ggml.o llama.o common.o $(OBJS) $(CXX) $(CXXFLAGS) $(filter-out %.txt,$^) -o $@ $(LDFLAGS) -tests/test-opt: tests/test-opt.c build-info.h ggml.o llama.o common.o $(OBJS) +tests/test-opt: tests/test-opt.cpp build-info.h ggml.o llama.o common.o $(OBJS) $(CXX) $(CXXFLAGS) $(filter-out %.txt,$^) -o $@ $(LDFLAGS) tests/test-quantize-fns: tests/test-quantize-fns.cpp build-info.h ggml.o llama.o common.o $(OBJS) diff --git a/scripts/sync-ggml.sh b/scripts/sync-ggml.sh index 02ea6ec15241b..3d13e852a4d24 100755 --- a/scripts/sync-ggml.sh +++ b/scripts/sync-ggml.sh @@ -10,5 +10,5 @@ cp -rpv ../ggml/src/ggml-metal.m ./ggml-metal.m cp -rpv ../ggml/src/ggml-metal.metal ./ggml-metal.metal cp -rpv ../ggml/include/ggml/ggml.h ./ggml.h -cp -rpv ../ggml/tests/test-opt.c ./tests/test-opt.c -cp -rpv ../ggml/tests/test-grad0.c ./tests/test-grad0.c +cp -rpv ../ggml/tests/test-opt.cpp ./tests/test-opt.cpp +cp -rpv ../ggml/tests/test-grad0.cpp ./tests/test-grad0.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 11ec6c7252f46..1a40edbec58c4 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,10 +6,10 @@ function(llama_add_test source) add_test(NAME ${TEST_TARGET} COMMAND $ ${ARGN}) endfunction() -# llama_add_test(test-double-float.c) # SLOW +# llama_add_test(test-double-float.cpp) # SLOW llama_add_test(test-quantize-fns.cpp) llama_add_test(test-quantize-perf.cpp) llama_add_test(test-sampling.cpp) llama_add_test(test-tokenizer-0.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../models/ggml-vocab.bin) -llama_add_test(test-grad0.c) # SLOW -# llama_add_test(test-opt.c) # SLOW +llama_add_test(test-grad0.cpp) # SLOW +# llama_add_test(test-opt.cpp) # SLOW diff --git a/tests/test-double-float.c b/tests/test-double-float.cpp similarity index 91% rename from tests/test-double-float.c rename to tests/test-double-float.cpp index fe04ef2d28c52..b506f273fee9f 100644 --- a/tests/test-double-float.c +++ b/tests/test-double-float.cpp @@ -3,11 +3,11 @@ // This is done by checking all finite (non-NaN, non-infinite) floats. #undef NDEBUG -#include +#include #include -#include -#include -#include +#include +#include +#include #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdouble-promotion" @@ -35,7 +35,7 @@ int main(void) { do { float f; memcpy(&f, &x, sizeof(x)); - assert(!isfinite(f) || (round_orig(f) == round_float(f))); + assert(!std::isfinite(f) || (round_orig(f) == round_float(f))); } while (x--); #ifdef __F16C__ diff --git a/tests/test-grad0.c b/tests/test-grad0.cpp similarity index 99% rename from tests/test-grad0.c rename to tests/test-grad0.cpp index 1148bd2c90ab4..78b668ad33252 100644 --- a/tests/test-grad0.c +++ b/tests/test-grad0.cpp @@ -1,10 +1,10 @@ #define _CRT_SECURE_NO_DEPRECATE // Disables ridiculous "unsafe" warnigns on Windows #include "ggml.h" -#include -#include -#include -#include +#include +#include +#include +#include #if defined(_MSC_VER) #pragma warning(disable: 4244 4267) // possible loss of data diff --git a/tests/test-opt.c b/tests/test-opt.cpp similarity index 98% rename from tests/test-opt.c rename to tests/test-opt.cpp index c831a091ad06a..e3671402ec3b4 100644 --- a/tests/test-opt.c +++ b/tests/test-opt.cpp @@ -1,9 +1,9 @@ #include "ggml.h" -#include -#include -#include -#include +#include +#include +#include +#include #define MAX_NARGS 2 From e23ba19da12054236a1c8ac04fe49b5cce1d090c Mon Sep 17 00:00:00 2001 From: netrunnereve <139727413+netrunnereve@users.noreply.github.com> Date: Tue, 1 Aug 2023 17:53:14 -0400 Subject: [PATCH 6/6] use initializer list for ggml_init_params --- tests/test-grad0.cpp | 9 +++++---- tests/test-opt.cpp | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/test-grad0.cpp b/tests/test-grad0.cpp index 78b668ad33252..75a698d73dc67 100644 --- a/tests/test-grad0.cpp +++ b/tests/test-grad0.cpp @@ -372,10 +372,11 @@ static bool check_mat_mul( #define NUM_PERMUTATIONS (4*3*2*1) int main(int argc, const char ** argv) { - struct ggml_init_params params; - params.mem_size = 128*1024*1024; - params.mem_buffer = NULL; - params.no_alloc = false; + struct ggml_init_params params = { + /* .mem_size = */ 128*1024*1024, + /* .mem_buffer = */ NULL, + /* .no_alloc = */ false, + }; int64_t ne[4]; diff --git a/tests/test-opt.cpp b/tests/test-opt.cpp index e3671402ec3b4..8ab240202a585 100644 --- a/tests/test-opt.cpp +++ b/tests/test-opt.cpp @@ -118,10 +118,11 @@ void set_element(struct ggml_tensor * t, int idx, float value) { } int main(void) { - struct ggml_init_params params; - params.mem_size = 1024*1024*1024; - params.mem_buffer = NULL; - params.no_alloc = false; + struct ggml_init_params params = { + /* .mem_size = */ 1024*1024*1024, + /* .mem_buffer = */ NULL, + /* .no_alloc = */ false, + }; struct ggml_context * ctx = ggml_init(params);