From e513abe37e5994873a6d3f8a1adcb1bcfc6a8b40 Mon Sep 17 00:00:00 2001 From: chenqiny Date: Sun, 15 Oct 2023 23:59:53 +0800 Subject: [PATCH] always use "GGUF" as beginng of GGUF file --- ggml.c | 8 +------- ggml.h | 14 ++++++++++++-- gguf-py/gguf/gguf.py | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/ggml.c b/ggml.c index 04b88c98a..6d1776ca4 100644 --- a/ggml.c +++ b/ggml.c @@ -20916,13 +20916,7 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p gguf_fread_el(file, &magic, sizeof(magic), &offset); if (magic != GGUF_MAGIC) { - if (magic == GGUF_WRONG_ENIAN_MAGIC) - { - fprintf(stderr, "Endianess of the GGUF file and platform do not match.%s: invalid magic number %08x.\n", __func__, magic); - } - else { - fprintf(stderr, "%s: invalid magic number %08x\n", __func__, magic); - } + fprintf(stderr, "%s: invalid magic number %08x\n", __func__, magic); fclose(file); return NULL; } diff --git a/ggml.h b/ggml.h index fdd8e31be..10ae3c033 100644 --- a/ggml.h +++ b/ggml.h @@ -231,8 +231,18 @@ #define GGML_EXIT_SUCCESS 0 #define GGML_EXIT_ABORTED 1 -#define GGUF_MAGIC 0x46554747 // "GGUF" -#define GGUF_WRONG_ENIAN_MAGIC 0x47475546 +#if defined(__linux__) + #include + #if BYTE_ORDER == LITTLE_ENDIAN + #define GGUF_MAGIC 0x46554747 + #elif BYTE_ORDER == BIG_ENDIAN + #define GGUF_MAGIC 0x47475546 + #endif +#else + // Use little endian magic uint_32 value + #define GGUF_MAGIC 0x46554747 +#endif + #define GGUF_VERSION 3 #define GGUF_DEFAULT_ALIGNMENT 32 diff --git a/gguf-py/gguf/gguf.py b/gguf-py/gguf/gguf.py index 748d59343..a5f92dd6d 100644 --- a/gguf-py/gguf/gguf.py +++ b/gguf-py/gguf/gguf.py @@ -652,7 +652,7 @@ class GGUFWriter: print(f"This gguf file is for {endianess_str} only") def write_header_to_file(self): - self.fout.write(struct.pack(f"{self.pack_prefix}I", GGUF_MAGIC)) + self.fout.write(struct.pack("