diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-09-29 14:38:02 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-09-29 14:38:02 +0200 |
| commit | 91c6aa07597462705a7f30ee5b97e2c7943b5616 (patch) | |
| tree | b16f686279a1f1b0691a6c0d65a544084fcc1c1e /liblangdet/safetensors.c | |
| parent | 6f98fc8cf3f7cc4b9d523d16492a07d3e2aea8c9 (diff) | |
| download | langdet-91c6aa07597462705a7f30ee5b97e2c7943b5616.tar.gz langdet-91c6aa07597462705a7f30ee5b97e2c7943b5616.zip | |
Remove unused checkpoint, tokenizer, and attention compatibility paths
Diffstat (limited to 'liblangdet/safetensors.c')
| -rw-r--r-- | liblangdet/safetensors.c | 466 |
1 files changed, 54 insertions, 412 deletions
diff --git a/liblangdet/safetensors.c b/liblangdet/safetensors.c index cfba76e..546f51d 100644 --- a/liblangdet/safetensors.c +++ b/liblangdet/safetensors.c @@ -1,267 +1,68 @@ #include "safetensors.h" -#include <dirent.h> +#include <yyjson.h> + #include <fcntl.h> -#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/mman.h> #include <sys/stat.h> #include <unistd.h> -/* Minimal JSON parser for safetensors header */ -/* TODO: consider replacing with yyjson */ - -static void skip_whitespace(const char **p) { - while (**p == ' ' || **p == '\n' || **p == '\r' || **p == '\t') - (*p)++; -} - -static int parse_string(const char **p, char *out, size_t max_len) { - skip_whitespace(p); - if (**p != '"') - return -1; - (*p)++; - size_t i = 0; - while (**p && **p != '"' && i < max_len - 1) { - if (**p == '\\') { - (*p)++; - if (**p == 'n') - out[i++] = '\n'; - else if (**p == 't') - out[i++] = '\t'; - else if (**p == '"') - out[i++] = '"'; - else if (**p == '\\') - out[i++] = '\\'; - else - out[i++] = **p; - } else { - out[i++] = **p; - } - (*p)++; - } - out[i] = '\0'; - if (**p != '"') - return -1; - (*p)++; - return 0; -} - -static int parse_int(const char **p, int64_t *out) { - skip_whitespace(p); - int64_t val = 0; - int neg = 0; - if (**p == '-') { - neg = 1; - (*p)++; - } - if (**p < '0' || **p > '9') - return -1; - while (**p >= '0' && **p <= '9') { - val = val * 10 + (**p - '0'); - (*p)++; - } - *out = neg ? -val : val; - return 0; -} - -static safetensor_dtype_t parse_dtype(const char *s) { - if (strcmp(s, "F32") == 0) - return DTYPE_F32; - if (strcmp(s, "F16") == 0) - return DTYPE_F16; - if (strcmp(s, "BF16") == 0) - return DTYPE_BF16; - if (strcmp(s, "I32") == 0) - return DTYPE_I32; - if (strcmp(s, "I64") == 0) - return DTYPE_I64; - if (strcmp(s, "BOOL") == 0) - return DTYPE_BOOL; - return DTYPE_UNKNOWN; -} - -static int parse_tensor_entry(const char **p, safetensor_t *t) { - skip_whitespace(p); - if (**p != '{') - return -1; - (*p)++; - - t->dtype = DTYPE_UNKNOWN; - t->ndim = 0; - t->data_offset = 0; - t->data_size = 0; - - while (**p && **p != '}') { - skip_whitespace(p); - if (**p == ',') { - (*p)++; - continue; - } - - char key[64]; - if (parse_string(p, key, sizeof(key)) != 0) - return -1; - skip_whitespace(p); - if (**p != ':') - return -1; - (*p)++; - skip_whitespace(p); - - if (strcmp(key, "dtype") == 0) { - char dtype_str[32]; - if (parse_string(p, dtype_str, sizeof(dtype_str)) != 0) - return -1; - t->dtype = parse_dtype(dtype_str); - } else if (strcmp(key, "shape") == 0) { - if (**p != '[') - return -1; - (*p)++; - t->ndim = 0; - while (**p && **p != ']') { - skip_whitespace(p); - if (**p == ',') { - (*p)++; - continue; - } - if (t->ndim >= 8) - return -1; - if (parse_int(p, &t->shape[t->ndim]) != 0) - return -1; - t->ndim++; - } - if (**p != ']') - return -1; - (*p)++; - } else if (strcmp(key, "data_offsets") == 0) { - if (**p != '[') - return -1; - (*p)++; - skip_whitespace(p); - int64_t start_i; - if (parse_int(p, &start_i) != 0) - return -1; - skip_whitespace(p); - if (**p != ',') - return -1; - (*p)++; - skip_whitespace(p); - int64_t end_i; - if (parse_int(p, &end_i) != 0) - return -1; - if (start_i < 0 || end_i < start_i) - return -1; - t->data_offset = (size_t)start_i; - t->data_size = (size_t)(end_i - start_i); - skip_whitespace(p); - if (**p != ']') - return -1; - (*p)++; - } else { - /* Skip unknown value */ - if (**p == '"') { - (*p)++; - while (**p && **p != '"') { - if (**p == '\\') - (*p)++; - if (**p) - (*p)++; - } - if (**p == '"') - (*p)++; - } else if (**p == '[') { - int depth = 1; - (*p)++; - while (**p && depth > 0) { - if (**p == '[') - depth++; - else if (**p == ']') - depth--; - (*p)++; - } - } else if (**p == '{') { - int depth = 1; - (*p)++; - while (**p && depth > 0) { - if (**p == '{') - depth++; - else if (**p == '}') - depth--; - (*p)++; - } - } else { - while (**p && **p != ',' && **p != '}') - (*p)++; - } - } - } - if (**p != '}') - return -1; - (*p)++; - return 0; -} - static int parse_header(safetensors_file_t *sf) { - const char *p = sf->header_json; - skip_whitespace(&p); - if (*p != '{') + yyjson_doc *doc = yyjson_read((char *)sf->data + 8, sf->header_size, 0); + if (!doc) return -1; - p++; - - sf->num_tensors = 0; - - while (*p && *p != '}') { - skip_whitespace(&p); - if (*p == ',') { - p++; - continue; - } - if (*p == '}') - break; + yyjson_val *root = yyjson_doc_get_root(doc); + if (!yyjson_is_obj(root)) + goto fail; - char name[256]; - if (parse_string(&p, name, sizeof(name)) != 0) - return -1; - skip_whitespace(&p); - if (*p != ':') - return -1; - p++; - - if (strcmp(name, "__metadata__") == 0) { - skip_whitespace(&p); - if (*p == '{') { - int depth = 1; - p++; - while (*p && depth > 0) { - if (*p == '{') - depth++; - else if (*p == '}') - depth--; - p++; - } - } + size_t index, count; + yyjson_val *key, *entry; + yyjson_obj_foreach(root, index, count, key, entry) { + if (yyjson_equals_str(key, "__metadata__")) continue; - } + if (!yyjson_is_obj(entry) || sf->num_tensors >= SAFETENSORS_MAX_TENSORS) + goto fail; + safetensor_t *tensor = &sf->tensors[sf->num_tensors]; + size_t name_len = yyjson_get_len(key); + if (name_len >= sizeof(tensor->name)) + goto fail; + memcpy(tensor->name, yyjson_get_str(key), name_len + 1); - if (sf->num_tensors >= SAFETENSORS_MAX_TENSORS) { - fprintf(stderr, "safetensors: too many tensors in %s (max %d)\n", - sf->path ? sf->path : "<unknown>", SAFETENSORS_MAX_TENSORS); - return -1; + yyjson_val *dtype = yyjson_obj_get(entry, "dtype"); + yyjson_val *shape = yyjson_obj_get(entry, "shape"); + yyjson_val *offsets = yyjson_obj_get(entry, "data_offsets"); + if (!yyjson_is_str(dtype) || !yyjson_is_arr(shape) || + yyjson_arr_size(shape) > sizeof(tensor->shape) / sizeof(tensor->shape[0]) || + !yyjson_is_arr(offsets) || yyjson_arr_size(offsets) != 2) + goto fail; + tensor->dtype = yyjson_equals_str(dtype, "F32") ? DTYPE_F32 : DTYPE_UNKNOWN; + tensor->ndim = (int)yyjson_arr_size(shape); + for (int i = 0; i < tensor->ndim; i++) { + yyjson_val *dim = yyjson_arr_get(shape, (size_t)i); + if (!yyjson_is_uint(dim) || yyjson_get_uint(dim) > INT64_MAX) + goto fail; + tensor->shape[i] = (int64_t)yyjson_get_uint(dim); } - - safetensor_t *t = &sf->tensors[sf->num_tensors]; - snprintf(t->name, sizeof(t->name), "%s", name); - if (parse_tensor_entry(&p, t) != 0) - return -1; + yyjson_val *start = yyjson_arr_get(offsets, 0); + yyjson_val *end = yyjson_arr_get(offsets, 1); + if (!yyjson_is_uint(start) || !yyjson_is_uint(end)) + goto fail; + uint64_t first = yyjson_get_uint(start), last = yyjson_get_uint(end); + if (first > last || last > sf->file_size - 8 - sf->header_size) + goto fail; + tensor->data_offset = (size_t)first; + tensor->data_size = (size_t)(last - first); sf->num_tensors++; } - if (*p != '}') - return -1; + yyjson_doc_free(doc); return 0; -} -/* Single file operations */ +fail: + yyjson_doc_free(doc); + return -1; +} safetensors_file_t *safetensors_open(const char *path) { int fd = open(path, O_RDONLY); @@ -269,17 +70,12 @@ safetensors_file_t *safetensors_open(const char *path) { return NULL; struct stat st; - if (fstat(fd, &st) < 0) { + if (fstat(fd, &st) < 0 || st.st_size < 8) { close(fd); return NULL; } size_t file_size = (size_t)st.st_size; - if (file_size < 8) { - close(fd); - return NULL; - } - void *data = mmap(NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0); close(fd); if (data == MAP_FAILED) @@ -298,24 +94,13 @@ safetensors_file_t *safetensors_open(const char *path) { return NULL; } - sf->path = strdup(path); sf->data = data; sf->file_size = file_size; sf->header_size = (size_t)header_size; - - sf->header_json = malloc(header_size + 1); - if (!sf->header_json) { - safetensors_close(sf); - return NULL; - } - memcpy(sf->header_json, (char *)data + 8, header_size); - sf->header_json[header_size] = '\0'; - if (parse_header(sf) != 0) { safetensors_close(sf); return NULL; } - return sf; } @@ -324,160 +109,17 @@ void safetensors_close(safetensors_file_t *sf) { return; if (sf->data) munmap(sf->data, sf->file_size); - free(sf->path); - free(sf->header_json); free(sf); } -const void *safetensors_data(const safetensors_file_t *sf, const safetensor_t *t) { - return (const char *)sf->data + 8 + sf->header_size + t->data_offset; -} - -/* Multi-shard operations */ - -static int cmp_shard_names(const void *a, const void *b) { - const char *sa = (const char *)a; - const char *sb = (const char *)b; - return strcmp(sa, sb); -} - -static int parse_shard_name(const char *name, int *idx, int *total) { - int i = 0, n = 0, consumed = 0; - if (sscanf(name, "model-%d-of-%d.safetensors%n", &i, &n, &consumed) != 2) - return -1; - if (name[consumed] != '\0' || i <= 0 || n <= 0 || i > n) - return -1; - *idx = i; - *total = n; - return 0; -} - -multi_safetensors_t *multi_safetensors_open(const char *model_dir) { - multi_safetensors_t *ms = calloc(1, sizeof(multi_safetensors_t)); - if (!ms) - return NULL; - - char path[4096]; - - /* Try single file first */ - snprintf(path, sizeof(path), "%s/model.safetensors", model_dir); - safetensors_file_t *sf = safetensors_open(path); - if (sf) { - ms->shards[0] = sf; - ms->num_shards = 1; - return ms; - } - - /* Scan directory for shard files */ - DIR *dir = opendir(model_dir); - if (!dir) { - free(ms); - return NULL; - } - - struct dirent *entry; - char shard_names[SAFETENSORS_MAX_SHARDS][256]; - int seen[SAFETENSORS_MAX_SHARDS] = {0}; - int n_shards = 0; - int expected_total = 0; - - while ((entry = readdir(dir)) != NULL) { - if (strncmp(entry->d_name, "model-", 6) == 0 && - strstr(entry->d_name, ".safetensors") != NULL) { - int shard_idx = 0, shard_total = 0; - if (parse_shard_name(entry->d_name, &shard_idx, &shard_total) != 0) { - fprintf(stderr, "multi_safetensors_open: bad shard name: %s\n", entry->d_name); - closedir(dir); - free(ms); - return NULL; - } - if (shard_total > SAFETENSORS_MAX_SHARDS) { - fprintf(stderr, "multi_safetensors_open: too many shards (%d > %d)\n", shard_total, - SAFETENSORS_MAX_SHARDS); - closedir(dir); - free(ms); - return NULL; - } - if (expected_total == 0) { - expected_total = shard_total; - } else if (expected_total != shard_total) { - fprintf(stderr, "multi_safetensors_open: inconsistent shard total in %s\n", - entry->d_name); - closedir(dir); - free(ms); - return NULL; - } - if (seen[shard_idx - 1]) { - fprintf(stderr, "multi_safetensors_open: duplicate shard index %d\n", shard_idx); - closedir(dir); - free(ms); - return NULL; - } - seen[shard_idx - 1] = 1; - - snprintf(shard_names[n_shards], sizeof(shard_names[n_shards]), "%s", entry->d_name); - n_shards++; - } - } - closedir(dir); - - if (n_shards == 0) { - fprintf(stderr, "multi_safetensors_open: no safetensors files in %s\n", model_dir); - free(ms); - return NULL; - } - if (expected_total != n_shards) { - fprintf(stderr, "multi_safetensors_open: expected %d shard(s), found %d in %s\n", - expected_total, n_shards, model_dir); - free(ms); - return NULL; - } - for (int i = 0; i < expected_total; i++) { - if (!seen[i]) { - fprintf(stderr, "multi_safetensors_open: missing shard %d of %d in %s\n", i + 1, - expected_total, model_dir); - free(ms); - return NULL; - } - } - - /* Sort shard names to ensure consistent ordering */ - qsort(shard_names, n_shards, sizeof(shard_names[0]), cmp_shard_names); - - /* Open each shard */ - for (int i = 0; i < n_shards; i++) { - snprintf(path, sizeof(path), "%s/%s", model_dir, shard_names[i]); - ms->shards[i] = safetensors_open(path); - if (!ms->shards[i]) { - fprintf(stderr, "multi_safetensors_open: failed to open %s\n", path); - multi_safetensors_close(ms); - return NULL; - } - } - ms->num_shards = n_shards; - return ms; -} - -void multi_safetensors_close(multi_safetensors_t *ms) { - if (!ms) - return; - for (int i = 0; i < ms->num_shards; i++) { - safetensors_close(ms->shards[i]); +const safetensor_t *safetensors_find(const safetensors_file_t *sf, const char *name) { + for (int i = 0; i < sf->num_tensors; i++) { + if (strcmp(sf->tensors[i].name, name) == 0) + return &sf->tensors[i]; } - free(ms); + return NULL; } -const safetensor_t * -multi_safetensors_find(const multi_safetensors_t *ms, const char *name, safetensors_file_t **out_sf) { - for (int s = 0; s < ms->num_shards; s++) { - safetensors_file_t *sf = ms->shards[s]; - for (int i = 0; i < sf->num_tensors; i++) { - if (strcmp(sf->tensors[i].name, name) == 0) { - if (out_sf) - *out_sf = sf; - return &sf->tensors[i]; - } - } - } - return NULL; +const void *safetensors_data(const safetensors_file_t *sf, const safetensor_t *t) { + return (const char *)sf->data + 8 + sf->header_size + t->data_offset; } |
