always use "GGUF" as beginng of GGUF file

This commit is contained in:
chenqiny 2023-10-15 23:59:53 +08:00
parent 7fc0250d15
commit e513abe37e
3 changed files with 14 additions and 10 deletions

8
ggml.c
View file

@ -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); gguf_fread_el(file, &magic, sizeof(magic), &offset);
if (magic != GGUF_MAGIC) { if (magic != GGUF_MAGIC) {
if (magic == GGUF_WRONG_ENIAN_MAGIC) fprintf(stderr, "%s: invalid magic number %08x\n", __func__, 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);
}
fclose(file); fclose(file);
return NULL; return NULL;
} }

14
ggml.h
View file

@ -231,8 +231,18 @@
#define GGML_EXIT_SUCCESS 0 #define GGML_EXIT_SUCCESS 0
#define GGML_EXIT_ABORTED 1 #define GGML_EXIT_ABORTED 1
#define GGUF_MAGIC 0x46554747 // "GGUF" #if defined(__linux__)
#define GGUF_WRONG_ENIAN_MAGIC 0x47475546 #include <endian.h>
#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_VERSION 3
#define GGUF_DEFAULT_ALIGNMENT 32 #define GGUF_DEFAULT_ALIGNMENT 32

View file

@ -652,7 +652,7 @@ class GGUFWriter:
print(f"This gguf file is for {endianess_str} only") print(f"This gguf file is for {endianess_str} only")
def write_header_to_file(self): def write_header_to_file(self):
self.fout.write(struct.pack(f"{self.pack_prefix}I", GGUF_MAGIC)) self.fout.write(struct.pack("<I", GGUF_MAGIC))
self.fout.write(struct.pack(f"{self.pack_prefix}I", GGUF_VERSION)) self.fout.write(struct.pack(f"{self.pack_prefix}I", GGUF_VERSION))
self.fout.write(struct.pack(f"{self.pack_prefix}Q", self.ti_data_count)) self.fout.write(struct.pack(f"{self.pack_prefix}Q", self.ti_data_count))
self.fout.write(struct.pack(f"{self.pack_prefix}Q", self.kv_data_count)) self.fout.write(struct.pack(f"{self.pack_prefix}Q", self.kv_data_count))