17 memset(buf, 0,
sizeof(*buf));
31 memset(node, 0,
sizeof(*node));
46 for (
int i = 0; i < graph->
num_nodes; ++i) {
51 memset(graph, 0,
sizeof(*graph));
116 return "aligned_embed";
120 return "aligned_head";
124 return "num_kv_heads";
126 return "aligned_ctx";
128 return "intermediate";
130 return "aligned_intermediate";
158 size_t aligned_embed;
160 size_t aligned_intermediate;
161 size_t aligned_context;
169 return (n + align - 1) & ~(align - 1);
174 size_t bytes = elems * elem_bytes;
176 return bytes / elem_bytes;
180 size_t alignment_bytes,
181 CKIRV2AlignInfo *align)
186 memset(align, 0,
sizeof(*align));
190 if (alignment_bytes == 0) {
193 size_t elem_bytes =
sizeof(float);
198 elem_bytes, alignment_bytes);
203 const CKIRV2AlignInfo *align,
209 if (tokens_override >= 0) {
210 return (
size_t)tokens_override;
216 return align ? align->aligned_embed : 0;
220 return align ? align->aligned_head : 0;
226 return align ? align->aligned_context : 0;
230 return align ? align->aligned_intermediate : 0;
250 fprintf(out,
"{\"dim\":\"%s\",\"dim_id\":%d,\"mult\":%d,\"div\":%d}",
263 const CKIRV2AlignInfo *align,
274 size_t mult = (size_t)(shape[i].mult > 0 ? shape[i].mult : 1);
275 size_t div = (size_t)(shape[i].div > 0 ? shape[i].div : 1);
276 size_t resolved = (div == 0) ? 0 : (dim * mult / div);
280 fprintf(out,
"%zu", resolved);
288 const CKIRV2AlignInfo *align,
291 fprintf(out,
" \"dimensions\": [\n");
305 size_t dim_count =
sizeof(dims) /
sizeof(dims[0]);
306 for (
size_t i = 0; i < dim_count; ++i) {
310 " {\"id\": %d, \"name\": \"%s\", \"value\": %zu}%s\n",
314 (i + 1 == dim_count) ?
"" :
",");
316 fprintf(out,
" ],\n");
325 return "activations";
340 fprintf(out,
" \"memory_plan\": {\n");
342 fprintf(out,
" \"total_bytes\": {\n");
346 fprintf(out,
" },\n");
347 fprintf(out,
" \"buffers\": [\n");
353 " {\"name\": \"%s\", \"arena\": \"%s\", \"offset_bytes\": %zu, \"size_bytes\": %zu, \"enabled\": %s}%s\n",
358 enabled ?
"true" :
"false",
361 fprintf(out,
" ]\n");
362 fprintf(out,
" },\n");
369 int base_context_window,
372 if (!graph || !path) {
375 FILE *out = fopen(path,
"wb");
377 fprintf(stderr,
"ck_ir_v2_serialize_json: failed to open %s: %s\n",
378 path, strerror(errno));
382 CKIRV2AlignInfo align = {0};
386 fprintf(out,
" \"version\": 2,\n");
387 fprintf(out,
" \"notes\": [\n");
388 fprintf(out,
" \"shape.dim uses symbolic names; see dimensions for resolved values\",\n");
390 " \"resolved_shape applies mult/div using alignment_bytes=%d and elem_bytes=4\",\n",
392 fprintf(out,
" \"kernel is the selected impl; kernel_dtype records dtype selection\"\n");
393 fprintf(out,
" ],\n");
394 fprintf(out,
" \"config\": {\n");
398 fprintf(out,
" \"num_attention_heads\": %d,\n", graph->
config.
num_heads);
404 fprintf(out,
" },\n");
408 fprintf(out,
" \"meta\": {\n");
409 fprintf(out,
" \"has_pos_emb\": %s,\n", graph->
has_pos_emb ?
"true" :
"false");
411 fprintf(out,
" \"tie_word_embeddings\": null,\n");
413 fprintf(out,
" \"tie_word_embeddings\": %s,\n", graph->
tie_word_embeddings ?
"true" :
"false");
416 fprintf(out,
" \"fused_qkv\": null,\n");
418 fprintf(out,
" \"fused_qkv\": %s,\n", graph->
fused_qkv ?
"true" :
"false");
421 fprintf(out,
" \"gated_mlp\": null\n");
423 fprintf(out,
" \"gated_mlp\": %s\n", graph->
gated_mlp ?
"true" :
"false");
425 fprintf(out,
" },\n");
428 int training = (mode && strcmp(mode,
"backward") == 0);
430 fprintf(out,
" \"lowering\": {\n");
431 fprintf(out,
" \"mode\": \"%s\",\n", mode ? mode :
"unknown");
432 fprintf(out,
" \"training\": %s,\n", training ?
"true" :
"false");
433 fprintf(out,
" \"tokens\": %d", tokens);
434 if (base_context_window >= 0) {
435 fprintf(out,
",\n \"base_context_window\": %d\n", base_context_window);
439 fprintf(out,
" },\n");
443 fprintf(out,
" \"buffers\": [\n");
446 fprintf(out,
" {\n");
447 fprintf(out,
" \"name\": \"%s\",\n", buf->
name ? buf->
name :
"");
451 fprintf(out,
" \"optional\": %d,\n", buf->
optional ? 1 : 0);
452 fprintf(out,
" \"shape\": ");
455 fprintf(out,
" \"resolved_shape\": ");
460 fprintf(out,
" \"alias_of\": \"%s\",\n", buf->
alias_of);
462 fprintf(out,
" \"alias_of\": null,\n");
465 fprintf(out,
" \"condition\": \"%s\"\n", buf->
condition);
467 fprintf(out,
" \"condition\": null\n");
469 fprintf(out,
" }%s\n", (i + 1 == graph->
num_buffers) ?
"" :
",");
471 fprintf(out,
" ],\n");
473 fprintf(out,
" \"nodes\": [\n");
474 for (
int i = 0; i < graph->
num_nodes; ++i) {
476 fprintf(out,
" {\n");
477 fprintf(out,
" \"layer\": %d,\n", (
int)node->
layer);
478 fprintf(out,
" \"op\": \"%s\",\n", node->
op ? node->
op :
"");
479 fprintf(out,
" \"kernel\": \"%s\",\n", node->
kernel ? node->
kernel :
"");
480 fprintf(out,
" \"kernel_variant\": \"%s\",\n", node->
kernel ? node->
kernel :
"");
482 fprintf(out,
" \"flags\": %u,\n", (
unsigned)node->
flags);
484 fprintf(out,
" \"condition\": \"%s\",\n", node->
condition);
486 fprintf(out,
" \"condition\": null,\n");
488 fprintf(out,
" \"bindings\": [\n");
491 const char *buf_name =
"";
496 " {\"arg\": \"%s\", \"buffer\": \"%s\"}%s\n",
497 bind->
arg ? bind->
arg :
"",
501 fprintf(out,
" ]\n");
502 fprintf(out,
" }%s\n", (i + 1 == graph->
num_nodes) ?
"" :
",");
504 fprintf(out,
" ]\n");
520 int base_context_window,
536 const char *cur =
start + 1;
537 while (cur <
end && *cur !=
'"') {
538 if (*cur ==
'\\' && (cur + 1) <
end) {
544 if (cur >=
end || *cur !=
'"') {
547 size_t len = (size_t)(cur - (
start + 1));
548 char *buf = (
char *)malloc(len + 1);
552 memcpy(buf,
start + 1, len);
560 if (!cur || cur >=
end || *cur !=
'"') {
565 if (*cur ==
'\\' && (cur + 1) <
end) {
579 const char **obj_start,
580 const char **obj_end)
583 const char *
start = NULL;
584 for (
const char *p = cur; p <
end; ++p) {
599 if (depth == 0 &&
start) {
612 if (!open || open >=
end || *open !=
'[') {
616 for (
const char *p = open; p <
end; ++p) {
641 size_t key_len = strlen(key);
642 const char *cur = json;
643 while (cur + key_len <
end) {
644 if (memcmp(cur, key, key_len) == 0) {
661 const char *colon = strchr(p,
':');
662 if (!colon || colon >=
end) {
666 if (sscanf(colon + 1,
"%d", &value) != 1) {
682 const char *colon = strchr(p,
':');
683 if (!colon || colon >=
end) {
686 const char *cur = colon + 1;
687 while (cur <
end && (*cur ==
' ' || *cur ==
'\t' || *cur ==
'\n' || *cur ==
'\r')) {
690 if (cur + 4 <=
end && memcmp(cur,
"true", 4) == 0) {
694 if (cur + 5 <=
end && memcmp(cur,
"false", 5) == 0) {
710 const char *colon = strchr(p,
':');
711 if (!colon || colon >=
end) {
715 if (sscanf(colon + 1,
"%f", &value) != 1) {
731 const char *colon = strchr(p,
':');
732 if (!colon || colon >=
end) {
735 const char *cur = colon + 1;
736 while (cur <
end && (*cur ==
' ' || *cur ==
'\t' || *cur ==
'\n' || *cur ==
'\r')) {
742 if (strncmp(cur,
"null", 4) == 0) {
775 if (strcmp(s,
"fp32") == 0)
return CK_DT_FP32;
776 if (strcmp(s,
"bf16") == 0)
return CK_DT_BF16;
777 if (strcmp(s,
"fp16") == 0)
return CK_DT_FP16;
778 if (strcmp(s,
"q4_0") == 0)
return CK_DT_Q4_0;
779 if (strcmp(s,
"q4_k") == 0)
return CK_DT_Q4_K;
780 if (strcmp(s,
"q6_k") == 0)
return CK_DT_Q6_K;
781 if (strcmp(s,
"q8_0") == 0)
return CK_DT_Q8_0;
788 char *dim_str = NULL;
819 shape_out[i].
mult = 0;
820 shape_out[i].
div = 0;
827 const char *open = strchr(
start,
'[');
829 if (!open || !close) {
833 const char *cur = open;
834 const char *sstart = NULL;
835 const char *send = NULL;
847 shape_out[idx].
dim = dim;
848 shape_out[idx].
mult = mult;
849 shape_out[idx].
div = div;
852 return (idx > 0) ? 0 : -1;
870 if (!graph || !name) {
889 const char *open = strchr(
start,
'[');
891 if (!open || !close) {
896 const char *cur = open;
897 const char *obj_start = NULL;
898 const char *obj_end = NULL;
912 while (idx < count &&
957 buffers[idx++] = buf;
974 const char *open = strchr(
start,
'[');
976 if (!open || !close || close > obj_end) {
980 const char *cur = open;
981 const char *bstart = NULL;
982 const char *bend = NULL;
1013 const char *open = strchr(
start,
'[');
1015 if (!open || !close) {
1020 const char *cur = open;
1021 const char *obj_start = NULL;
1022 const char *obj_end = NULL;
1036 while (idx < count &&
1041 char *kernel = NULL;
1042 char *kernel_variant = NULL;
1043 char *kernel_dtype = NULL;
1056 if (!kernel && kernel_variant) {
1057 kernel = kernel_variant;
1058 kernel_variant = NULL;
1065 node.
layer = (uint16_t)layer;
1066 node.
flags = (uint8_t)flags;
1071 if (kernel_variant) {
1072 free(kernel_variant);
1085 for (
int j = 0; j < idx; ++j) {
1092 nodes[idx++] = node;
1095 graph->
nodes = nodes;
1102 if (!path || !graph) {
1105 FILE *f = fopen(path,
"rb");
1107 perror(
"ck_ir_v2_parse_json: fopen");
1110 if (fseek(f, 0, SEEK_END) != 0) {
1114 long len = ftell(f);
1119 if (fseek(f, 0, SEEK_SET) != 0) {
1123 char *buf = (
char *)malloc((
size_t)len + 1);
1128 size_t nread = fread(buf, 1, (
size_t)len, f);
1133 const char *
end = buf + nread;
1155 const char *brace = strchr(meta_key,
'{');
1156 const char *obj_start = NULL;
1157 const char *obj_end = NULL;
CKDataType
Supported data types in C-Kernel-Engine.
static void ck_ir_v2_resolve_align(const CKModelConfig *cfg, size_t alignment_bytes, CKIRV2AlignInfo *align)
static const char * ck_ir_v2_dim_name(CKDimKind dim)
static void ck_ir_v2_emit_resolved_shape(FILE *out, const CKModelConfig *cfg, const CKIRV2AlignInfo *align, const CKDimToken *shape, int tokens_override)
static const char * ck_ir_v2_mem_arena_name(CKMemArenaKind arena)
static CKDimKind ck_ir_v2_dim_kind_from_name(const char *name)
static void ck_ir_v2_emit_dimensions(FILE *out, const CKModelConfig *cfg, const CKIRV2AlignInfo *align, int tokens_override)
static const char * ck_ir_v2_dtype_name(CKDataType dtype)
static size_t ck_ir_v2_resolve_dim_value(const CKModelConfig *cfg, const CKIRV2AlignInfo *align, CKDimKind dim, int tokens_override)
static const char * ck_ir_v2_scope_name(CKBufferScope scope)
static int ck_ir_v2_serialize_json_internal(const CKIRV2Graph *graph, const CKMemPlan *plan, const char *mode, int tokens_override, int base_context_window, const char *path)
static int ck_ir_v2_parse_string(const char *start, const char *end, char **out_str)
static void ck_ir_v2_free_node(CKIRV2Node *node)
static void ck_ir_v2_free_buffer(CKIRV2Buffer *buf)
static int ck_ir_v2_parse_buffers(const char *json, const char *end, CKIRV2Graph *graph)
static const char * ck_ir_v2_role_name(CKBufferRole role)
static int ck_ir_v2_parse_shape(const char *obj_start, const char *obj_end, CKDimToken *shape_out)
static void ck_ir_v2_emit_memory_plan(FILE *out, const CKIRV2Graph *graph, const CKMemPlan *plan)
static const char * ck_ir_v2_next_object(const char *cur, const char *end, const char **obj_start, const char **obj_end)
static const char * ck_ir_v2_find_array_end(const char *open, const char *end)
static CKDimKind ck_ir_v2_parse_dim_kind(const char *obj_start, const char *obj_end)
void ck_ir_v2_free(CKIRV2Graph *graph)
int ck_ir_v2_serialize_json(const CKIRV2Graph *graph, const char *path)
static size_t ck_ir_v2_align_up_elems(size_t elems, size_t elem_bytes, size_t align_bytes)
int ck_ir_v2_parse_json(const char *path, CKIRV2Graph *graph)
static const char * ck_ir_v2_find_key(const char *json, const char *key, const char *end)
static int ck_ir_v2_parse_bindings(const char *obj_start, const char *obj_end, CKIRV2Graph *graph, CKIRV2Node *node)
static const CKBufferSpec * ck_ir_v2_find_buffer_spec(const char *name)
static CKBufferRole ck_ir_v2_parse_role(const char *s)
static int ck_ir_v2_parse_bool(const char *json, const char *key, const char *end, int *out_val)
static int ck_ir_v2_parse_int(const char *json, const char *key, const char *end, int *out_val)
static int ck_ir_v2_parse_nodes(const char *json, const char *end, CKIRV2Graph *graph)
static int ck_ir_v2_parse_string_field(const char *json, const char *key, const char *end, char **out_str)
static CKDataType ck_ir_v2_parse_dtype(const char *s)
static int ck_ir_v2_find_buffer_index(const CKIRV2Graph *graph, const char *name)
static const char * ck_ir_v2_skip_string(const char *cur, const char *end)
static int ck_ir_v2_parse_float(const char *json, const char *key, const char *end, float *out_val)
static CKBufferScope ck_ir_v2_parse_scope(const char *s)
static size_t ck_ir_v2_align_up_bytes(size_t n, size_t align)
static int ck_ir_v2_emit_shape(FILE *out, const CKDimToken *shape)
int ck_ir_v2_serialize_json_with_plan(const CKIRV2Graph *graph, const CKMemPlan *plan, const char *mode, int tokens_override, int base_context_window, const char *path)
#define CK_IR_V2_MAX_DIMS
#define CK_IR_V2_MAX_BINDINGS
@ CK_DIM_ALIGNED_INTERMEDIATE
const CKBufferSpec ck_decoder_buffers[]
const size_t ck_decoder_buffer_count
@ CK_MEM_ARENA_ACTIVATIONS
#define CK_MEM_PLAN_DEFAULT_ALIGN
CKIRV2Binding bindings[24]
size_t total_bytes[CK_MEM_ARENA_COUNT]