]> git.djapps.eu Git - pkg/ggml/sources/ggml/commitdiff
build : fix compilation errors and warnigns when building with MSVC (#275)
authorBorislav Stanimirov <redacted>
Sat, 24 Jun 2023 16:11:35 +0000 (19:11 +0300)
committerGitHub <redacted>
Sat, 24 Jun 2023 16:11:35 +0000 (19:11 +0300)
20 files changed:
.gitignore
examples/common.cpp
examples/dolly-v2/main.cpp
examples/gpt-2/main.cpp
examples/gpt-j/main.cpp
examples/gpt-neox/main.cpp
examples/mnist/main-cpu.cpp
examples/mnist/main.cpp
examples/mpt/main.cpp
examples/replit/main.cpp
examples/starcoder/main.cpp
examples/whisper/main.cpp
examples/whisper/whisper.cpp
src/ggml.c
tests/test-grad0.c
tests/test-mul-mat0.c
tests/test-mul-mat2.c
tests/test-vec0.c
tests/test2.c
tests/test3.c

index 3ae49ab9c11653ae4a7aa7edc48ac86dd7290f62..7714dd6febb9149e7a0570f6a75b1e35c1041bfc 100644 (file)
@@ -1,8 +1,12 @@
 build/
 build-debug/
 build-*/
+out/
 
 compile_commands.json
+CMakeSettings.json
+.vs/
+.vscode/
 
 .exrc
 .cache
index cf1769bdbaa57591fec07de3832d6fe4b79a9411..d25e1ad5bcf5a91ac45256c0cc5523b43c3ed9c3 100644 (file)
 #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) {
index 3c9bd19a4e66b38cfcbac358b96121de5fea766b..85faa707625e94b943ebe88211ebde0c812b6e2c 100644 (file)
 #include <string>
 #include <vector>
 
+#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
index 080dc714e85357d604abca4c09303f3d86edae95..103bd388a95f4b5de66f7fc1e5a59c71a600322e 100644 (file)
 #include <string>
 #include <vector>
 
+#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;
index 93b1b6b1e0760423286e36061335104a334113a7..3d956ffe876c34d0bfb1591bfa1bd7c372e9b84d 100644 (file)
 #include <string>
 #include <vector>
 
+#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;
index 290cf20625fd64a9ee55578a39bf84422756c90c..1cd64a227fe76e9207955030a0f1c376ba27f159 100644 (file)
 #include <string>
 #include <vector>
 
+#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;
index 22d12d0557bbffbffe03d06c8b0797389ee0d721..b3cde65818394acd3f37ba4629c35dc670540568 100644 (file)
 #include <fstream>
 #include <vector>
 
+#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
index e4bf8f61c7a3dcfc2b2c425307029fcbb31d3fb5..823616fc8d6a162eadcfbe3369e94cb718c9ffc4 100644 (file)
 #include <vector>
 #include <algorithm>
 
+#if defined(_MSC_VER)
+#pragma warning(disable: 4244 4267) // possible loss of data
+#endif
+
 // default hparams
 struct mnist_hparams {
     int32_t n_input   = 784;
index 0278b40f0563c655394b3dc7be7e08aa72541269..e5903c3c31bb4efdcf0f339b083ac974a6b6ccf5 100644 (file)
 #include <utility>
 #include <vector>
 
+#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");
 
index 710f1eea7483ead3139d1039287dc866a4037d78..77a38be0cb9466199ae1469e836f2862c616ef6a 100644 (file)
 #include <map>
 #include <stdint.h>
 #include <string>
-#include <unistd.h>
 #include <unordered_map>
 #include <utility>
 #include <vector>
 
+#if defined(_WIN32)
+#define NOMINMAX
+#include <Windows.h>
+bool is_stdin_terminal() {
+    auto in = GetStdHandle(STD_INPUT_HANDLE);
+    return GetFileType(in) == FILE_TYPE_CHAR;
+}
+#else
+#include <unistd.h>
+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<std::size_t, float>;
 using piece_map_t = std::unordered_map<std::string, piece_t>;
 
@@ -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");
index 0d05d5e25f94fd374975d32a61f777d8b54dae1e..2016f8974c8dc8bf5979cd5d69e2e8a5687c0cd7 100644 (file)
 #include <string>
 #include <vector>
 
+#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 {
index 3e4920849a182d82bc90fb7bfcf5c87ff5107b59..1083512fc97032068318b3387d693e96376e6233 100644 (file)
 #include <vector>
 #include <cstring>
 
+#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<std::string> k_colors = {
index e28c63b60d250e4ea8ada3cdf49f2fb161b40158..89f36bfd0d0bdb6f3bb75b24256379dbfe8fd263 100644 (file)
 #include <regex>
 #include <random>
 
+#if defined(_MSC_VER)
+#pragma warning(disable: 4244 4267) // possible loss of data
+#endif
+
 #if defined(GGML_BIG_ENDIAN)
 #include <bit>
 
index ed5c2b0d01dc13146d2a76d6d4bbf844c72330a5..aa30e74c24455be8b915a6d8dc6d9429e9be5872 100644 (file)
@@ -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"
 
index c8c2c0f717e320ff699190134d559b7420e6feac..b6371395239339772694af90a13c6d8b27a17212 100644 (file)
@@ -1,3 +1,4 @@
+#define _CRT_SECURE_NO_DEPRECATE // Disables ridiculous "unsafe" warnigns on Windows
 #include "ggml.h"
 
 #include <math.h>
@@ -5,6 +6,10 @@
 #include <stdlib.h>
 #include <assert.h>
 
+#if defined(_MSC_VER)
+#pragma warning(disable: 4244 4267) // possible loss of data
+#endif
+
 #define MAX_NARGS 3
 
 #undef MIN
index 55047ed104b3045114350335babd64e614006ef3..185df39651fd82432b1c3cec0ff9a072b434096d 100644 (file)
@@ -1,3 +1,4 @@
+#define _CRT_SECURE_NO_DEPRECATE // Disables ridiculous "unsafe" warnigns on Windows
 #include "ggml/ggml.h"
 
 #include <math.h>
@@ -6,6 +7,10 @@
 #include <assert.h>
 #include <inttypes.h>
 
+#if defined(_MSC_VER)
+#pragma warning(disable: 4244 4267) // possible loss of data
+#endif
+
 #define MAX_NARGS 2
 
 float frand() {
index e6728a511cc18c44c847d4472ce4ff258550afb2..ad30492b478f27115d82c83f27b50f145688d2a9 100644 (file)
@@ -5,14 +5,12 @@
 #include <float.h>
 #include <stdint.h>
 #include <stdio.h>
+#include <inttypes.h>
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
-#include <time.h>
 #include <math.h>
 
-#include <sys/time.h>
-
 #if defined(__ARM_NEON)
 #include "arm_neon.h"
 #elif defined(__AVX__) || defined(__AVX2__)
 #define MIN(a, b) ((a) < (b) ? (a) : (b))
 #endif
 
+#if defined(_MSC_VER)
+#pragma warning(disable: 4244 4267) // possible loss of data
+#include <intrin.h>
+#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);
     }
 
index f798eaaf9a210cabc53f16621efdc57cb805edeb..5e23f8ebab62404beccdb4c9e0ada861e56a308a 100644 (file)
@@ -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];
         }
     }
index 3b223412731f6050d997e4fc0ba669fca3736c49..839e3e6de91d6b874606e0637d34925ec6ff6dda 100644 (file)
@@ -1,9 +1,14 @@
+#define _CRT_SECURE_NO_DEPRECATE // Disables ridiculous "unsafe" warnigns on Windows
 #include "ggml/ggml.h"
 
 #include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
 
+#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;
 }
index a5ccdb7f2ba33a53d0fe02fb23bde33dd187587e..b92d6233dcaadbbce8db236e69ce124e0e59e154 100644 (file)
@@ -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)),