12 #define MANIFEST_LINE_MAX 4096
13 #define COPY_CHUNK (1 << 20)
16 return strncmp(s, prefix, strlen(prefix)) == 0;
19 static unsigned long long parse_u64(
const char *s) {
21 return strtoull(s, NULL, 0);
25 const char *weights_path,
26 const char *manifest_path)
28 if (!base || !weights_path || !manifest_path) {
29 fprintf(stderr,
"ck_load_weights_manifest_v4: invalid arguments\n");
33 FILE *wf = fopen(weights_path,
"rb");
35 fprintf(stderr,
"ck_load_weights_manifest_v4: failed to open %s: %s\n",
36 weights_path, strerror(errno));
41 if (fread(magic, 1, 8, wf) != 8 || memcmp(magic,
"BUMPWGT4", 8) != 0) {
42 fprintf(stderr,
"ck_load_weights_manifest_v4: invalid BUMPWGT4 magic\n");
47 FILE *mf = fopen(manifest_path,
"r");
49 fprintf(stderr,
"ck_load_weights_manifest_v4: failed to open %s: %s\n",
50 manifest_path, strerror(errno));
58 fprintf(stderr,
"ck_load_weights_manifest_v4: malloc failed\n");
64 while (fgets(line,
sizeof(line), mf)) {
65 if (line[0] ==
'#' || line[0] ==
'\n') {
68 line[strcspn(line,
"\r\n")] =
'\0';
70 char *name = strtok(line,
"|");
71 char *dtype = strtok(NULL,
"|");
72 char *file_off = strtok(NULL,
"|");
73 char *size_str = strtok(NULL,
"|");
74 char *rt_off = strtok(NULL,
"|");
76 if (!name || !dtype || !file_off || !size_str || !rt_off) {
77 fprintf(stderr,
"ck_load_weights_manifest_v4: malformed line\n");
87 unsigned long long file_offset =
parse_u64(file_off);
88 unsigned long long size =
parse_u64(size_str);
89 unsigned long long runtime_offset =
parse_u64(rt_off);
91 if (fseek(wf, (
long)file_offset, SEEK_SET) != 0) {
92 fprintf(stderr,
"ck_load_weights_manifest_v4: fseek failed\n");
99 unsigned char *dst = (
unsigned char *)base + runtime_offset;
100 unsigned long long remaining = size;
102 while (remaining > 0) {
104 size_t n = fread(buf, 1, take, wf);
106 fprintf(stderr,
"ck_load_weights_manifest_v4: short read\n");
112 memcpy(dst, buf, take);
int ck_load_weights_manifest_v4(void *base, const char *weights_path, const char *manifest_path)
Load BUMPWGT4 weights into a v4 model buffer using a manifest map.
static unsigned long long parse_u64(const char *s)
static int starts_with(const char *s, const char *prefix)
#define MANIFEST_LINE_MAX