← Back to C-Kernel-Engine Docs Doxygen Source Documentation
ckernel_ir_v2_demo.c
Go to the documentation of this file.
1 #include "ckernel_ir.h"
2 #include "ckernel_ir_v2.h"
3 #include "ckernel_ir_v2_lower.h"
4 #include "ckernel_codegen_v2.h"
5 
6 #include <stdio.h>
7 #include <string.h>
8 
9 static void print_usage(const char *argv0)
10 {
11  fprintf(stderr,
12  "Usage:\n"
13  " %s /path/to/config.json [--meta weights_meta.json] [--emit out.c] [--emit-lib]\n"
14  " %s --ir /path/to/ir_v2.json [--emit out.c] [--emit-lib]\n"
15  "\n"
16  "Options:\n"
17  " --emit out.c Write generated C code to file\n"
18  " --emit-lib Generate shared library API (no main)\n"
19  " --meta path Apply weights meta JSON (ties/quant info)\n"
20  " --lower mode Emit lowered IR JSON (prefill/decode/backward)\n"
21  " --lower-out path Write lowered IR JSON to path (default build/ir_v2_<mode>.json)\n",
22  argv0, argv0);
23 }
24 
25 int main(int argc, char **argv)
26 {
27  if (argc < 2) {
28  print_usage(argv[0]);
29  return 1;
30  }
31 
32  CKIRV2Graph graph = {0};
33 
34  const char *emit_path = NULL;
35  const char *meta_path = NULL;
36  const char *lower_mode_str = NULL;
37  const char *lower_out = NULL;
38  char lower_out_buf[128] = {0};
39  CKEmitMode emit_mode = CK_EMIT_STANDALONE;
40 
41  for (int i = 1; i < argc; ++i) {
42  if (strcmp(argv[i], "--emit") == 0 && i + 1 < argc) {
43  emit_path = argv[++i];
44  } else if (strcmp(argv[i], "--emit-lib") == 0) {
45  emit_mode = CK_EMIT_LIBRARY;
46  } else if (strcmp(argv[i], "--meta") == 0 && i + 1 < argc) {
47  meta_path = argv[++i];
48  } else if (strcmp(argv[i], "--lower") == 0 && i + 1 < argc) {
49  lower_mode_str = argv[++i];
50  } else if (strcmp(argv[i], "--lower-out") == 0 && i + 1 < argc) {
51  lower_out = argv[++i];
52  }
53  }
54 
55  if (strcmp(argv[1], "--ir") == 0) {
56  if (argc < 3) {
57  fprintf(stderr, "Missing IR v2 JSON path after --ir\n");
58  return 1;
59  }
60  const char *ir_path = argv[2];
61  if (ck_ir_v2_parse_json(ir_path, &graph) != 0) {
62  fprintf(stderr, "Failed to parse IR v2 JSON: %s\n", ir_path);
63  return 1;
64  }
65  } else {
66  const char *config_path = argv[1];
67  CKModelConfig cfg;
68  if (ck_model_config_from_hf_json(config_path, &cfg) != 0) {
69  fprintf(stderr, "Failed to parse config.json: %s\n", config_path);
70  return 1;
71  }
72  if (ck_ir_v2_build_decoder(&cfg, &graph) != 0) {
73  fprintf(stderr, "Failed to build IR v2 decoder\n");
74  return 1;
75  }
76  if (meta_path) {
77  if (ck_ir_v2_apply_meta(meta_path, &graph) != 0) {
78  fprintf(stderr, "Failed to apply weights meta: %s\n", meta_path);
79  ck_ir_v2_free(&graph);
80  return 1;
81  }
82  }
83  }
84 
85  printf("[ck_ir_v2_demo] buffers=%d nodes=%d layers=%d\n",
86  graph.num_buffers, graph.num_nodes, graph.config.num_layers);
87 
88  if (emit_path) {
89  const char *mode_str = (emit_mode == CK_EMIT_LIBRARY) ? "library" : "standalone";
90  if (ck_codegen_v2_emit_runtime(&graph, emit_path, emit_mode) == 0) {
91  fprintf(stderr, "[ck_ir_v2_demo] %s runtime written to %s\n", mode_str, emit_path);
92  } else {
93  fprintf(stderr, "[ck_ir_v2_demo] failed to write %s runtime to %s\n", mode_str, emit_path);
94  }
95  }
96 
97  if (lower_mode_str) {
98  CKIRV2LowerMode mode;
99  if (ck_ir_v2_lower_mode_from_string(lower_mode_str, &mode) != 0) {
100  fprintf(stderr, "[ck_ir_v2_demo] invalid lower mode: %s\n", lower_mode_str);
101  ck_ir_v2_free(&graph);
102  return 1;
103  }
104  if (!lower_out) {
105  snprintf(lower_out_buf, sizeof(lower_out_buf), "build/ir_v2_%s.json",
107  lower_out = lower_out_buf;
108  }
109  if (mode == CK_IR_V2_LOWER_BACKWARD) {
110  CKIRV2Graph backward = {0};
111  if (ck_ir_v2_build_decoder_backward(&graph, &backward) != 0) {
112  fprintf(stderr, "[ck_ir_v2_demo] failed to build backward graph\n");
113  ck_ir_v2_free(&graph);
114  return 1;
115  }
116  if (ck_ir_v2_lower_emit_json(&backward, mode, lower_out) == 0) {
117  fprintf(stderr, "[ck_ir_v2_demo] lowered IR v2 JSON written to %s\n", lower_out);
118  } else {
119  fprintf(stderr, "[ck_ir_v2_demo] failed to write lowered IR v2 JSON to %s\n", lower_out);
120  }
121  ck_ir_v2_free(&backward);
122  } else {
123  if (ck_ir_v2_lower_emit_json(&graph, mode, lower_out) == 0) {
124  fprintf(stderr, "[ck_ir_v2_demo] lowered IR v2 JSON written to %s\n", lower_out);
125  } else {
126  fprintf(stderr, "[ck_ir_v2_demo] failed to write lowered IR v2 JSON to %s\n", lower_out);
127  }
128  }
129  }
130 
131  if (strcmp(argv[1], "--ir") != 0) {
132  if (ck_ir_v2_serialize_json(&graph, "build/ir_v2.json") == 0) {
133  fprintf(stderr, "[ck_ir_v2_demo] IR v2 JSON written to build/ir_v2.json\n");
134  } else {
135  fprintf(stderr, "[ck_ir_v2_demo] Failed to write IR v2 JSON\n");
136  }
137  }
138 
139  ck_ir_v2_free(&graph);
140  return 0;
141 }
CKEmitMode
@ CK_EMIT_STANDALONE
@ CK_EMIT_LIBRARY
int ck_codegen_v2_emit_runtime(const CKIRV2Graph *graph, const char *path, CKEmitMode mode)
int ck_model_config_from_hf_json(const char *path, CKModelConfig *cfg)
Definition: ckernel_ir.c:209
void ck_ir_v2_free(CKIRV2Graph *graph)
Definition: ckernel_ir_v2.c:34
int ck_ir_v2_apply_meta(const char *path, CKIRV2Graph *graph)
int ck_ir_v2_serialize_json(const CKIRV2Graph *graph, const char *path)
int ck_ir_v2_build_decoder(const CKModelConfig *cfg, CKIRV2Graph *graph)
int ck_ir_v2_parse_json(const char *path, CKIRV2Graph *graph)
int ck_ir_v2_build_decoder_backward(const CKIRV2Graph *forward, CKIRV2Graph *backward)
int main(int argc, char **argv)
static void print_usage(const char *argv0)
const char * ck_ir_v2_lower_mode_name(CKIRV2LowerMode mode)
int ck_ir_v2_lower_emit_json(const CKIRV2Graph *input, CKIRV2LowerMode mode, const char *path)
CKIRV2LowerMode
@ CK_IR_V2_LOWER_BACKWARD
int ck_ir_v2_lower_mode_from_string(const char *name, CKIRV2LowerMode *out_mode)
CKModelConfig config
Definition: ckernel_ir_v2.h:56