← Back to C-Kernel-Engine Docs Doxygen Source Documentation
ckernel_model_load_v4.c
Go to the documentation of this file.
1 /*
2  * ckernel_model_load_v4.c - Load BUMPWGT4 weights using a manifest map.
3  */
4 
6 
7 #include <errno.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 
12 #define MANIFEST_LINE_MAX 4096
13 #define COPY_CHUNK (1 << 20)
14 
15 static int starts_with(const char *s, const char *prefix) {
16  return strncmp(s, prefix, strlen(prefix)) == 0;
17 }
18 
19 static unsigned long long parse_u64(const char *s) {
20  if (!s) return 0;
21  return strtoull(s, NULL, 0);
22 }
23 
25  const char *weights_path,
26  const char *manifest_path)
27 {
28  if (!base || !weights_path || !manifest_path) {
29  fprintf(stderr, "ck_load_weights_manifest_v4: invalid arguments\n");
30  return -1;
31  }
32 
33  FILE *wf = fopen(weights_path, "rb");
34  if (!wf) {
35  fprintf(stderr, "ck_load_weights_manifest_v4: failed to open %s: %s\n",
36  weights_path, strerror(errno));
37  return -1;
38  }
39 
40  char magic[8] = {0};
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");
43  fclose(wf);
44  return -1;
45  }
46 
47  FILE *mf = fopen(manifest_path, "r");
48  if (!mf) {
49  fprintf(stderr, "ck_load_weights_manifest_v4: failed to open %s: %s\n",
50  manifest_path, strerror(errno));
51  fclose(wf);
52  return -1;
53  }
54 
55  char line[MANIFEST_LINE_MAX];
56  unsigned char *buf = malloc(COPY_CHUNK);
57  if (!buf) {
58  fprintf(stderr, "ck_load_weights_manifest_v4: malloc failed\n");
59  fclose(mf);
60  fclose(wf);
61  return -1;
62  }
63 
64  while (fgets(line, sizeof(line), mf)) {
65  if (line[0] == '#' || line[0] == '\n') {
66  continue;
67  }
68  line[strcspn(line, "\r\n")] = '\0';
69 
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, "|");
75 
76  if (!name || !dtype || !file_off || !size_str || !rt_off) {
77  fprintf(stderr, "ck_load_weights_manifest_v4: malformed line\n");
78  free(buf);
79  fclose(mf);
80  fclose(wf);
81  return -1;
82  }
83 
84  (void)name;
85  (void)dtype;
86 
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);
90 
91  if (fseek(wf, (long)file_offset, SEEK_SET) != 0) {
92  fprintf(stderr, "ck_load_weights_manifest_v4: fseek failed\n");
93  free(buf);
94  fclose(mf);
95  fclose(wf);
96  return -1;
97  }
98 
99  unsigned char *dst = (unsigned char *)base + runtime_offset;
100  unsigned long long remaining = size;
101 
102  while (remaining > 0) {
103  size_t take = remaining > COPY_CHUNK ? COPY_CHUNK : (size_t)remaining;
104  size_t n = fread(buf, 1, take, wf);
105  if (n != take) {
106  fprintf(stderr, "ck_load_weights_manifest_v4: short read\n");
107  free(buf);
108  fclose(mf);
109  fclose(wf);
110  return -1;
111  }
112  memcpy(dst, buf, take);
113  dst += take;
114  remaining -= take;
115  }
116  }
117 
118  free(buf);
119  fclose(mf);
120  fclose(wf);
121  return 0;
122 }
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 COPY_CHUNK
#define MANIFEST_LINE_MAX