Skip to content

Commit 5addd62

Browse files
committed
A few fixes that make it compile on Mingw64.
Mostly taken from ggml-org/llama.cpp#22 Some might be unnecessary, this is the first version I managed to run.
1 parent 4fc96cb commit 5addd62

File tree

4 files changed

+46
-21
lines changed

4 files changed

+46
-21
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ endif
3232

3333
CFLAGS = -I. -O3 -DNDEBUG -std=c11 -fPIC
3434
CXXFLAGS = -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC
35-
LDFLAGS =
35+
LDFLAGS = -static
3636

3737
# OS specific
3838
# TODO: support Windows
@@ -48,6 +48,10 @@ ifeq ($(UNAME_S),FreeBSD)
4848
CFLAGS += -pthread
4949
CXXFLAGS += -pthread
5050
endif
51+
ifeq ($(UNAME_S),NetBSD)
52+
CFLAGS += -pthread
53+
CXXFLAGS += -pthread
54+
endif
5155
ifeq ($(UNAME_S),Haiku)
5256
CFLAGS += -pthread
5357
CXXFLAGS += -pthread

ggml.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#if defined(_MSC_VER) || defined(__MINGW32__)
44
#include <malloc.h> // using malloc.h with MSC/MINGW
5-
#elif !defined(__FreeBSD__)
5+
#elif !defined(__FreeBSD__) && !defined(__NetBSD__)
66
#include <alloca.h>
77
#endif
88

@@ -364,7 +364,7 @@ static const size_t CACHE_LINE_SIZE_F32 = CACHE_LINE_SIZE/sizeof(float);
364364
#if __AVX2__
365365
// Unpack 32 4-bit fields into 32 bytes
366366
// The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
367-
inline __m256i bytesFromNibbles( const uint8_t* rsi )
367+
static inline __m256i bytesFromNibbles( const uint8_t* rsi )
368368
{
369369
// Load 16 bytes from memory
370370
__m128i tmp = _mm_loadu_si128( ( const __m128i* )rsi );
@@ -381,7 +381,7 @@ inline __m256i bytesFromNibbles( const uint8_t* rsi )
381381
return bytes;
382382
}
383383

384-
inline __m128i packNibbles( __m256i bytes )
384+
static inline __m128i packNibbles( __m256i bytes )
385385
{
386386
// Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
387387
const __m256i lowByte = _mm256_set1_epi16( 0xFF );
@@ -407,8 +407,8 @@ void quantize_row_q4_0(const float * restrict x, void * restrict y, int k) {
407407
const int nb = k / QK;
408408
const size_t bs = sizeof(float) + QK/2;
409409

410-
uint8_t * restrict pd = (uint8_t *) (y + 0*bs);
411-
uint8_t * restrict pb = (uint8_t *) (y + 0*bs + sizeof(float));
410+
uint8_t * restrict pd = ((uint8_t *)y + 0*bs);
411+
uint8_t * restrict pb = ((uint8_t *)y + 0*bs + sizeof(float));
412412

413413
uint8_t pp[QK/2];
414414

@@ -654,8 +654,8 @@ void dequantize_row_q4_0(const void * restrict x, float * restrict y, int k) {
654654
const int nb = k / QK;
655655
const size_t bs = sizeof(float) + QK/2;
656656

657-
const uint8_t * restrict pd = (const uint8_t *) (x + 0*bs);
658-
const uint8_t * restrict pb = (const uint8_t *) (x + 0*bs + sizeof(float));
657+
const uint8_t * restrict pd = ((const uint8_t *)x + 0*bs);
658+
const uint8_t * restrict pb = ((const uint8_t *)x + 0*bs + sizeof(float));
659659

660660
// scalar
661661
for (int i = 0; i < nb; i++) {
@@ -1301,11 +1301,11 @@ inline static void ggml_vec_dot_q4_0(const int n, float * restrict s, const void
13011301

13021302
const size_t bs = sizeof(float) + QK/2;
13031303

1304-
const uint8_t * restrict pd0 = (const uint8_t *) (x + 0*bs);
1305-
const uint8_t * restrict pd1 = (const uint8_t *) (y + 0*bs);
1304+
const uint8_t * restrict pd0 = ((const uint8_t *)x + 0*bs);
1305+
const uint8_t * restrict pd1 = ((const uint8_t *)y + 0*bs);
13061306

1307-
const uint8_t * restrict pb0 = (const uint8_t *) (x + 0*bs + sizeof(float));
1308-
const uint8_t * restrict pb1 = (const uint8_t *) (y + 0*bs + sizeof(float));
1307+
const uint8_t * restrict pb0 = ((const uint8_t *)x + 0*bs + sizeof(float));
1308+
const uint8_t * restrict pb1 = ((const uint8_t *)y + 0*bs + sizeof(float));
13091309

13101310
float sumf = 0.0;
13111311

@@ -1731,8 +1731,8 @@ inline static void ggml_vec_mad_q4_0(const int n, float * restrict y, void * res
17311731
const int nb = n / QK;
17321732
const size_t bs = sizeof(float) + QK/2;
17331733

1734-
const uint8_t * restrict pd = (const uint8_t *) (x + 0*bs);
1735-
const uint8_t * restrict pb = (const uint8_t *) (x + 0*bs + sizeof(float));
1734+
const uint8_t * restrict pd = ((const uint8_t *)x + 0*bs);
1735+
const uint8_t * restrict pb = ((const uint8_t *)x + 0*bs + sizeof(float));
17361736

17371737
#if __ARM_NEON
17381738
#if QK == 32

main.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
#include <string>
1212
#include <vector>
1313

14+
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
15+
#include <signal.h>
16+
#include <unistd.h>
17+
#elif defined (_WIN32)
18+
#include <signal.h>
19+
#endif
20+
1421
struct bloom_hparams {
1522
int32_t n_vocab = 32000;
1623
int32_t n_ctx = 512; // this is provided as user input?
@@ -212,8 +219,8 @@ bool bloom_model_load(const std::string & fname, bloom_model & model, gpt_vocab
212219
// create the ggml context
213220
{
214221
struct ggml_init_params params = {
215-
.mem_size = ctx_size,
216-
.mem_buffer = NULL,
222+
/*.mem_size =*/ ctx_size,
223+
/*.mem_buffer =*/ NULL,
217224
};
218225

219226
model.ctx = ggml_init(params);
@@ -566,7 +573,8 @@ bool bloom_eval(
566573
};
567574

568575
struct ggml_context * ctx0 = ggml_init(params);
569-
struct ggml_cgraph gf = { .n_threads = n_threads };
576+
ggml_cgraph gf = {};
577+
gf.n_threads = n_threads;
570578

571579
struct ggml_tensor * embd = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, N);
572580
memcpy(embd->data, embd_inp.data(), N*ggml_element_size(embd));
@@ -763,6 +771,7 @@ bool bloom_eval(
763771
}
764772

765773
int main(int argc, char ** argv) {
774+
ggml_time_init();
766775
const int64_t t_main_start_us = ggml_time_us();
767776

768777
gpt_params params;

utils.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
#include <cstring>
55
#include <fstream>
66
#include <regex>
7+
#include <iostream>
8+
#include <iterator>
9+
#include <string>
10+
#include <math.h>
11+
12+
#if defined(_MSC_VER) || defined(__MINGW32__)
13+
#include <malloc.h> // using malloc.h with MSC/MINGW
14+
#elif !defined(__FreeBSD__) && !defined(__NetBSD__)
15+
#include <alloca.h>
16+
#endif
717

818
bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
919
for (int i = 1; i < argc; i++) {
@@ -487,7 +497,8 @@ size_t ggml_quantize_q4_0(float * src, void * dst, int n, int k, int qk, int64_t
487497

488498
assert(k % qk == 0);
489499

490-
uint8_t pp[qk/2];
500+
const size_t pp_size = qk / 2;
501+
uint8_t *pp = static_cast<uint8_t*>(alloca(pp_size));
491502

492503
char * pdst = (char *) dst;
493504

@@ -526,7 +537,7 @@ size_t ggml_quantize_q4_0(float * src, void * dst, int n, int k, int qk, int64_t
526537
pp[l/2] = vi0 | (vi1 << 4);
527538
}
528539

529-
memcpy(pb, pp, sizeof(pp));
540+
memcpy(pb, pp, pp_size);
530541
pb += bs;
531542
}
532543
}
@@ -541,7 +552,8 @@ size_t ggml_quantize_q4_1(float * src, void * dst, int n, int k, int qk, int64_t
541552

542553
assert(k % qk == 0);
543554

544-
uint8_t pp[qk/2];
555+
const size_t pp_size = qk / 2;
556+
uint8_t *pp = static_cast<uint8_t*>(alloca(pp_size));
545557

546558
char * pdst = (char *) dst;
547559

@@ -585,7 +597,7 @@ size_t ggml_quantize_q4_1(float * src, void * dst, int n, int k, int qk, int64_t
585597
pp[l/2] = vi0 | (vi1 << 4);
586598
}
587599

588-
memcpy(pb + i*qk/2, pp, sizeof(pp));
600+
memcpy(pb + i*qk/2, pp, pp_size);
589601
}
590602
}
591603
}

0 commit comments

Comments
 (0)