← Back to C-Kernel-Engine Docs Doxygen Source Documentation
ckernel_model_load_v4.c File Reference
#include "ckernel_model_load_v4.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Macros

#define COPY_CHUNK   (1 << 20)
 
#define MANIFEST_LINE_MAX   4096
 

Functions

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. More...
 
static unsigned long long parse_u64 (const char *s)
 
static int starts_with (const char *s, const char *prefix)
 

Macro Definition Documentation

◆ COPY_CHUNK

#define COPY_CHUNK   (1 << 20)

Definition at line 13 of file ckernel_model_load_v4.c.

◆ MANIFEST_LINE_MAX

#define MANIFEST_LINE_MAX   4096

Definition at line 12 of file ckernel_model_load_v4.c.

Function Documentation

◆ ck_load_weights_manifest_v4()

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.

The manifest format is line-based: name|dtype|file_offset|size|runtime_offset Offsets/sizes accept decimal or hex (0x...).

Parameters
baseBase pointer for the generated model buffer.
weights_pathPath to BUMPWGT4 weights file.
manifest_pathPath to weights_manifest.map emitted by build_ir_v4.py.
Returns
0 on success, non-zero on error.

Definition at line 24 of file ckernel_model_load_v4.c.

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 }
static unsigned long long parse_u64(const char *s)
#define COPY_CHUNK
#define MANIFEST_LINE_MAX

References COPY_CHUNK, MANIFEST_LINE_MAX, and parse_u64().

◆ parse_u64()

static unsigned long long parse_u64 ( const char *  s)
static

Definition at line 19 of file ckernel_model_load_v4.c.

19  {
20  if (!s) return 0;
21  return strtoull(s, NULL, 0);
22 }

Referenced by ck_load_weights_manifest_v4().

◆ starts_with()

static int starts_with ( const char *  s,
const char *  prefix 
)
static

Definition at line 15 of file ckernel_model_load_v4.c.

15  {
16  return strncmp(s, prefix, strlen(prefix)) == 0;
17 }