From: Borislav Stanimirov Date: Sat, 24 Jun 2023 16:11:35 +0000 (+0300) Subject: build : fix compilation errors and warnigns when building with MSVC (#275) X-Git-Tag: upstream/0.0.1642~1392 X-Git-Url: https://git.djapps.eu/?a=commitdiff_plain;h=c1bb712a0485d0dfe7cbb51db4dc232f6cdb0f6d;p=pkg%2Fggml%2Fsources%2Fggml build : fix compilation errors and warnigns when building with MSVC (#275) --- diff --git a/.gitignore b/.gitignore index 3ae49ab9..7714dd6f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,12 @@ build/ build-debug/ build-*/ +out/ compile_commands.json +CMakeSettings.json +.vs/ +.vscode/ .exrc .cache diff --git a/examples/common.cpp b/examples/common.cpp index cf1769bd..d25e1ad5 100644 --- a/examples/common.cpp +++ b/examples/common.cpp @@ -17,6 +17,10 @@ #define M_PI 3.14159265358979323846 #endif +#if defined(_MSC_VER) +#pragma warning(disable: 4244 4267) // possible loss of data +#endif + bool gpt_params_parse(int argc, char ** argv, gpt_params & params) { for (int i = 1; i < argc; i++) { std::string arg = argv[i]; @@ -366,7 +370,7 @@ void test_gpt_tokenizer(gpt_vocab & vocab, const std::string & fpath_test){ } } - fprintf(stderr, "%s : %lu tests failed out of %lu tests.\n", __func__, n_fails, tests.size()); + fprintf(stderr, "%s : %zu tests failed out of %zu tests.\n", __func__, n_fails, tests.size()); } bool gpt_vocab_init(const std::string & fname, gpt_vocab & vocab) { diff --git a/examples/dolly-v2/main.cpp b/examples/dolly-v2/main.cpp index 3c9bd19a..85faa707 100644 --- a/examples/dolly-v2/main.cpp +++ b/examples/dolly-v2/main.cpp @@ -13,6 +13,10 @@ #include #include +#if defined(_MSC_VER) +#pragma warning(disable: 4244 4267) // possible loss of data +#endif + // default hparams (Dolly-V2 3B) struct dollyv2_hparams { int32_t n_vocab = 50254; // tokenizer.vocab_size diff --git a/examples/gpt-2/main.cpp b/examples/gpt-2/main.cpp index 080dc714..103bd388 100644 --- a/examples/gpt-2/main.cpp +++ b/examples/gpt-2/main.cpp @@ -12,6 +12,10 @@ #include #include +#if defined(_MSC_VER) +#pragma warning(disable: 4244 4267) // possible loss of data +#endif + // default hparams (GPT-2 117M) struct gpt2_hparams { int32_t n_vocab = 50257; diff --git a/examples/gpt-j/main.cpp b/examples/gpt-j/main.cpp index 93b1b6b1..3d956ffe 100644 --- a/examples/gpt-j/main.cpp +++ b/examples/gpt-j/main.cpp @@ -12,6 +12,11 @@ #include #include +#if defined(_MSC_VER) +#pragma warning(disable: 4244 4267) // possible loss of data +#endif + + // default hparams (GPT-J 6B) struct gptj_hparams { int32_t n_vocab = 50400; diff --git a/examples/gpt-neox/main.cpp b/examples/gpt-neox/main.cpp index 290cf206..1cd64a22 100644 --- a/examples/gpt-neox/main.cpp +++ b/examples/gpt-neox/main.cpp @@ -13,6 +13,10 @@ #include #include +#if defined(_MSC_VER) +#pragma warning(disable: 4244 4267) // possible loss of data +#endif + // default hparams (StableLM 3B) struct gpt_neox_hparams { int32_t n_vocab = 50257; diff --git a/examples/mnist/main-cpu.cpp b/examples/mnist/main-cpu.cpp index 22d12d05..b3cde658 100644 --- a/examples/mnist/main-cpu.cpp +++ b/examples/mnist/main-cpu.cpp @@ -20,6 +20,10 @@ #include #include +#if defined(_MSC_VER) +#pragma warning(disable: 4244 4267) // possible loss of data +#endif + // evaluate the MNIST compute graph // // - fname_cgraph: path to the compute graph diff --git a/examples/mnist/main.cpp b/examples/mnist/main.cpp index e4bf8f61..823616fc 100644 --- a/examples/mnist/main.cpp +++ b/examples/mnist/main.cpp @@ -11,6 +11,10 @@ #include #include +#if defined(_MSC_VER) +#pragma warning(disable: 4244 4267) // possible loss of data +#endif + // default hparams struct mnist_hparams { int32_t n_input = 784; diff --git a/examples/mpt/main.cpp b/examples/mpt/main.cpp index 0278b40f..e5903c3c 100644 --- a/examples/mpt/main.cpp +++ b/examples/mpt/main.cpp @@ -14,6 +14,10 @@ #include #include +#if defined(_MSC_VER) +#pragma warning(disable: 4244 4267) // possible loss of data +#endif + // no defaults for now struct mpt_hparams { int32_t d_model = 0; @@ -932,7 +936,7 @@ int main(int argc, char ** argv) { printf("%s: number of tokens in prompt = %zu\n", __func__, embd_inp.size()); for (size_t i = 0; i < embd_inp.size(); i++) { - printf("%s: token[%lu] = %6d\n", __func__, i, embd_inp[i]); + printf("%s: token[%zu] = %6d\n", __func__, i, embd_inp[i]); } printf("\n"); diff --git a/examples/replit/main.cpp b/examples/replit/main.cpp index 710f1eea..77a38be0 100644 --- a/examples/replit/main.cpp +++ b/examples/replit/main.cpp @@ -13,11 +13,28 @@ #include #include #include -#include #include #include #include +#if defined(_WIN32) +#define NOMINMAX +#include +bool is_stdin_terminal() { + auto in = GetStdHandle(STD_INPUT_HANDLE); + return GetFileType(in) == FILE_TYPE_CHAR; +} +#else +#include +bool is_stdin_terminal() { + return isatty(STDIN_FILENO); +} +#endif + +#if defined(_MSC_VER) +#pragma warning(disable: 4244 4267) // possible loss of data +#endif + using piece_t = std::pair; using piece_map_t = std::unordered_map; @@ -645,7 +662,7 @@ int main(int argc, char ** argv) { std::mt19937 rng(params.seed); if (params.prompt.empty()) { - if (!isatty(STDIN_FILENO)) { + if (!is_stdin_terminal()) { std::string line; while (std::getline(std::cin, line)) { params.prompt = params.prompt + "\n" + line; @@ -685,7 +702,7 @@ int main(int argc, char ** argv) { printf("%s: number of tokens in prompt = %zu\n", __func__, embd_inp.size()); for (int i = 0; i < embd_inp.size(); i++) { - printf("%s: token[%d] = %6lu\n", __func__, i, embd_inp[i]); + printf("%s: token[%d] = %6zu\n", __func__, i, embd_inp[i]); // vocab.id_to_token.at(embd_inp[i]).c_str() } printf("\n"); diff --git a/examples/starcoder/main.cpp b/examples/starcoder/main.cpp index 0d05d5e2..2016f897 100644 --- a/examples/starcoder/main.cpp +++ b/examples/starcoder/main.cpp @@ -12,6 +12,10 @@ #include #include +#if defined(_MSC_VER) +#pragma warning(disable: 4244 4267) // possible loss of data +#endif + // default hparams (GPT-2 117M) // https://huggingface.co/bigcode/gpt_bigcode-santacoder/blob/main/config.json struct starcoder_hparams { diff --git a/examples/whisper/main.cpp b/examples/whisper/main.cpp index 3e492084..1083512f 100644 --- a/examples/whisper/main.cpp +++ b/examples/whisper/main.cpp @@ -10,6 +10,10 @@ #include #include +#if defined(_MSC_VER) +#pragma warning(disable: 4244 4267) // possible loss of data +#endif + // Terminal color map. 10 colors grouped in ranges [0.0, 0.1, ..., 0.9] // Lowest is red, middle is yellow, highest is green. const std::vector k_colors = { diff --git a/examples/whisper/whisper.cpp b/examples/whisper/whisper.cpp index e28c63b6..89f36bfd 100644 --- a/examples/whisper/whisper.cpp +++ b/examples/whisper/whisper.cpp @@ -19,6 +19,10 @@ #include #include +#if defined(_MSC_VER) +#pragma warning(disable: 4244 4267) // possible loss of data +#endif + #if defined(GGML_BIG_ENDIAN) #include diff --git a/src/ggml.c b/src/ggml.c index ed5c2b0d..aa30e74c 100644 --- a/src/ggml.c +++ b/src/ggml.c @@ -1,5 +1,5 @@ -// Defines CLOCK_MONOTONIC on Linux -#define _GNU_SOURCE +#define _GNU_SOURCE // Defines CLOCK_MONOTONIC on Linux +#define _CRT_SECURE_NO_DEPRECATE // Disables ridiculous "unsafe" warnigns on Windows #include "ggml.h" diff --git a/tests/test-grad0.c b/tests/test-grad0.c index c8c2c0f7..b6371395 100644 --- a/tests/test-grad0.c +++ b/tests/test-grad0.c @@ -1,3 +1,4 @@ +#define _CRT_SECURE_NO_DEPRECATE // Disables ridiculous "unsafe" warnigns on Windows #include "ggml.h" #include @@ -5,6 +6,10 @@ #include #include +#if defined(_MSC_VER) +#pragma warning(disable: 4244 4267) // possible loss of data +#endif + #define MAX_NARGS 3 #undef MIN diff --git a/tests/test-mul-mat0.c b/tests/test-mul-mat0.c index 55047ed1..185df396 100644 --- a/tests/test-mul-mat0.c +++ b/tests/test-mul-mat0.c @@ -1,3 +1,4 @@ +#define _CRT_SECURE_NO_DEPRECATE // Disables ridiculous "unsafe" warnigns on Windows #include "ggml/ggml.h" #include @@ -6,6 +7,10 @@ #include #include +#if defined(_MSC_VER) +#pragma warning(disable: 4244 4267) // possible loss of data +#endif + #define MAX_NARGS 2 float frand() { diff --git a/tests/test-mul-mat2.c b/tests/test-mul-mat2.c index e6728a51..ad30492b 100644 --- a/tests/test-mul-mat2.c +++ b/tests/test-mul-mat2.c @@ -5,14 +5,12 @@ #include #include #include +#include #include #include #include -#include #include -#include - #if defined(__ARM_NEON) #include "arm_neon.h" #elif defined(__AVX__) || defined(__AVX2__) @@ -24,6 +22,12 @@ #define MIN(a, b) ((a) < (b) ? (a) : (b)) #endif +#if defined(_MSC_VER) +#pragma warning(disable: 4244 4267) // possible loss of data +#include +#define __builtin_popcountll __popcnt64 +#endif + const int M = 1280; const int N = 1536; const int K = 1280; @@ -54,12 +58,6 @@ float frand() { return (float) rand() / (float) RAND_MAX; } -uint64_t get_time_us() { - struct timeval tv; - gettimeofday(&tv, NULL); - return tv.tv_sec * 1000000 + tv.tv_usec; -} - #if defined(__AVX2__) // horizontally reduce 8 32-bit integers static inline uint32_t _mm256_hadd_epi32_gg(__m256i v) { @@ -255,8 +253,8 @@ void mul_mat_gq_1( s1[b + 1] = d1*(1 << b); } - m0[0] = -1ULL; - m1[0] = -1ULL; + m0[0] = 0-1ULL; + m1[0] = 0-1ULL; for (int s = 0; s < QK/gq_t_bits; ++s) { for (int b = 0; b < QB; b++) { @@ -2373,6 +2371,7 @@ void mul_mat_gq_6( int main(int argc, const char ** argv) { assert(sizeof(gq_quant_t)*8 == gq_t_bits); + ggml_time_init(); // needed to initialize f16 tables { @@ -2462,7 +2461,7 @@ int main(int argc, const char ** argv) { // convert fp32 -> gq { - const uint64_t t_start = get_time_us(); + const int64_t t_start = ggml_time_us(); if (method == 1) { quantize_1(src0, src0_gq, M, K); @@ -2494,7 +2493,7 @@ int main(int argc, const char ** argv) { quantize_6(src1, src1_gq, N, K); } - const uint64_t t_end = get_time_us(); + const int64_t t_end = ggml_time_us(); printf("convert time: %f ms / method = %d\n", (t_end - t_start) / 1000.0, method); } @@ -2504,8 +2503,8 @@ int main(int argc, const char ** argv) { const int nIter = 1; - const clock_t start = clock(); - const uint64_t start_us = get_time_us(); + const int64_t start = ggml_cycles(); + const int64_t start_us = ggml_time_us(); double iM = 1.0/M; double sum = 0.0f; @@ -2544,9 +2543,9 @@ int main(int argc, const char ** argv) { } { - const clock_t end = clock(); - const uint64_t end_us = get_time_us(); - printf("%s: elapsed ticks: %ld\n", __func__, end - start); + const int64_t end = ggml_cycles(); + const int64_t end_us = ggml_time_us(); + printf("%s: elapsed ticks: %" PRIu64 "\n", __func__, end - start); printf("%s: elapsed us: %d / %f ms\n", __func__, (int)(end_us - start_us), (end_us - start_us) / 1000.0 / nIter); } diff --git a/tests/test-vec0.c b/tests/test-vec0.c index f798eaaf..5e23f8eb 100644 --- a/tests/test-vec0.c +++ b/tests/test-vec0.c @@ -20,8 +20,11 @@ void mul_mat_vec_f32_0( dst[i] = sum; } } - -typedef float afloat __attribute__ ((__aligned__(32))); +#if defined(_MSC_VER) +typedef float __declspec(align(32)) afloat; +#else +typedef float afloat __attribute__((__aligned__(32))); +#endif void mul_mat_vec_f32_1( const afloat *restrict src0, const afloat *restrict src1, @@ -70,18 +73,24 @@ void mul_mat_vec_f32_2( for (unsigned i = 0; i < nrows; i++) { float sum = 0.0f; - const void * row = src0 + i*ncols*sizeof(float); - const void * col = src1; + const char * row = (const char*)src0 + i*ncols*sizeof(float); + const char * col = (const char*)src1; for (unsigned j = 0; j < ncols; j++) { sum += (*(float *)row) * (*(float *)col); row += sizeof(float); col += sizeof(float); } *(float *)d = sum; - d += sizeof(float); + d = (char*)d + sizeof(float); } } +#if defined(_MSC_VER) +void* aligned_alloc(size_t alignment, size_t size) { + return _aligned_malloc(size, alignment); +} +#endif + int main(int argc, const char ** argv) { //float * src0 = malloc(sizeof(float)*N*M); //float * src1 = malloc(sizeof(float)*M); @@ -91,12 +100,12 @@ int main(int argc, const char ** argv) { afloat * src1 = (float *)(aligned_alloc(32, sizeof(float)*M)); afloat * dst = (float *)(aligned_alloc(32, sizeof(float)*N)); - for (unsigned i = 0; i < N*M; i++) { - src0[i] = i; + for (int i = 0; i < N*M; i++) { + src0[i] = (afloat)i; } - for (unsigned i = 0; i < M; i++) { - src1[i] = i; + for (int i = 0; i < M; i++) { + src1[i] = (afloat)i; } const int nIter = 10; @@ -108,7 +117,7 @@ int main(int argc, const char ** argv) { //mul_mat_vec_f32_0(src0, src1, dst, N, M); mul_mat_vec_f32_1(src0, src1, dst, N, M); //mul_mat_vec_f32_2(src0, src1, dst, N, M); - for (unsigned i = 0; i < N; i++) { + for (int i = 0; i < N; i++) { sum += dst[i]; } } diff --git a/tests/test2.c b/tests/test2.c index 3b223412..839e3e6d 100644 --- a/tests/test2.c +++ b/tests/test2.c @@ -1,9 +1,14 @@ +#define _CRT_SECURE_NO_DEPRECATE // Disables ridiculous "unsafe" warnigns on Windows #include "ggml/ggml.h" #include #include #include +#if defined(_MSC_VER) +#pragma warning(disable: 4244 4267) // possible loss of data +#endif + bool is_close(float a, float b, float epsilon) { return fabs(a - b) < epsilon; } diff --git a/tests/test3.c b/tests/test3.c index a5ccdb7f..b92d6233 100644 --- a/tests/test3.c +++ b/tests/test3.c @@ -59,7 +59,7 @@ int main(int argc, const char ** argv) { l) ) ), - ggml_new_f32(ctx0, NP) + ggml_new_f32(ctx0, (float)NP) ), ggml_mul(ctx0, ggml_sum(ctx0, ggml_sqr(ctx0, x)),