gguf_create(): take flags to be able to overwrite files. Fixes #7.

This commit is contained in:
antirez
2024-01-09 16:32:10 +01:00
parent 81dbf8f8d2
commit 6eb4aeb2fb
3 changed files with 8 additions and 4 deletions

View File

@@ -196,7 +196,7 @@ void gguf_tools_split_mixtral(int *experts_id, const char *mixtral_filename, con
exit(1); exit(1);
} }
gguf_ctx *output = gguf_create(output_filename); gguf_ctx *output = gguf_create(output_filename, GGUF_NONE);
if (output == NULL) { if (output == NULL) {
perror("Opening the output file"); perror("Opening the output file");
exit(1); exit(1);

View File

@@ -428,14 +428,14 @@ void gguf_print_value(gguf_ctx *ctx, uint32_t type, union gguf_value *val, int f
* *
* On success the context with the file already loaded is returned, * On success the context with the file already loaded is returned,
* otherwise NULL is returned. */ * otherwise NULL is returned. */
gguf_ctx *gguf_create(const char *filename) { gguf_ctx *gguf_create(const char *filename, int flags) {
struct gguf_header hdr; struct gguf_header hdr;
memcpy(&hdr.magic,"GGUF",4); memcpy(&hdr.magic,"GGUF",4);
hdr.version = 3; hdr.version = 3;
hdr.tensor_count = 0; hdr.tensor_count = 0;
hdr.metadata_kv_count = 0; hdr.metadata_kv_count = 0;
FILE *fp = fopen(filename,"wx"); FILE *fp = fopen(filename, flags & GGUF_OVERWRITE ? "w" : "wx");
if (fp == NULL) return NULL; if (fp == NULL) return NULL;
if (fwrite(&hdr,1,sizeof(hdr),fp) != sizeof(hdr)) { if (fwrite(&hdr,1,sizeof(hdr),fp) != sizeof(hdr)) {
fclose(fp); fclose(fp);

View File

@@ -12,6 +12,10 @@
/* ============================ Enums and structures ======================== */ /* ============================ Enums and structures ======================== */
/* Flags that can be used in different functions with the same meaning. */
#define GGUF_NONE 0 // No flags.
#define GGUF_OVERWRITE (1<<0) // Overwrite the destination object.
enum gguf_tensor_type { enum gguf_tensor_type {
GGUF_TYPE_F32 = 0, GGUF_TYPE_F32 = 0,
GGUF_TYPE_F16 = 1, GGUF_TYPE_F16 = 1,
@@ -160,7 +164,7 @@ typedef struct {
/* =============================== Prototypes =============================== */ /* =============================== Prototypes =============================== */
gguf_ctx *gguf_init(const char *filename); gguf_ctx *gguf_init(const char *filename);
gguf_ctx *gguf_create(const char *filename); gguf_ctx *gguf_create(const char *filename, int flags);
int gguf_remap(gguf_ctx *ctx); int gguf_remap(gguf_ctx *ctx);
void gguf_rewind(gguf_ctx *ctx); void gguf_rewind(gguf_ctx *ctx);
void gguf_end(gguf_ctx *ctx); void gguf_end(gguf_ctx *ctx);