← Back to C-Kernel-Engine Docs Doxygen Source Documentation
 
Loading...
Searching...
No Matches
ckernel_ir_demo.c
Go to the documentation of this file.
1#include "ckernel_ir.h"
2#include "ckernel_codegen.h"
3
4#include <stdio.h>
5#include <string.h>
6
7int main(int argc, char **argv)
8{
9 if (argc < 2) {
10 fprintf(stderr,
11 "Usage:\n"
12 " %s /path/to/config.json [--emit out.c] [--emit-lib] # parse config, dump + codegen\n"
13 " %s --ir /path/to/ir.json [--emit out.c] [--emit-lib] # parse IR JSON, dump + codegen\n"
14 "\n"
15 "Options:\n"
16 " --emit out.c Write generated C code to file\n"
17 " --emit-lib Generate shared library API (no main)\n"
18 " Without --emit-lib: generates standalone executable with main()\n",
19 argv[0], argv[0]);
20 return 1;
21 }
22
23 CKIRGraph graph = {0};
24 CKIRGraph bwd = {0};
25
26 const char *emit_path = NULL;
28
29 for (int i = 1; i < argc; ++i) {
30 if (strcmp(argv[i], "--emit") == 0 && i + 1 < argc) {
31 emit_path = argv[++i];
32 } else if (strcmp(argv[i], "--emit-lib") == 0) {
33 emit_mode = CK_EMIT_LIBRARY;
34 }
35 }
36
37 if (strcmp(argv[1], "--ir") == 0) {
38 if (argc < 3) {
39 fprintf(stderr, "Missing IR JSON path after --ir\n");
40 return 1;
41 }
42 const char *ir_path = argv[2];
43 if (ck_ir_parse_json(ir_path, &graph) != 0) {
44 fprintf(stderr, "Failed to parse IR JSON: %s\n", ir_path);
45 return 1;
46 }
47 if (ck_build_decoder_backward_ir(&graph, &bwd) != 0) {
48 fprintf(stderr, "Failed to build backward IR from IR JSON\n");
49 ck_ir_free(&graph);
50 return 1;
51 }
52 } else {
53 const char *config_path = argv[1];
54 CKModelConfig cfg;
55 if (ck_model_config_from_hf_json(config_path, &cfg) != 0) {
56 fprintf(stderr, "Failed to parse config.json: %s\n", config_path);
57 return 1;
58 }
59
60 if (ck_build_decoder_ir(&cfg, &graph) != 0) {
61 fprintf(stderr, "Failed to build decoder IR\n");
62 return 1;
63 }
64
65 if (ck_build_decoder_backward_ir(&graph, &bwd) != 0) {
66 fprintf(stderr, "Failed to build backward IR\n");
67 ck_ir_free(&graph);
68 return 1;
69 }
70 }
71
72 printf("=== Forward IR ===\n");
73 ck_ir_dump(&graph, stdout);
74 printf("\n=== Backward IR (skeleton) ===\n");
75 ck_ir_dump(&bwd, stdout);
76
77 printf("\n=== Generated C Skeleton ===\n");
78 ck_codegen_c_skeleton(&graph, &bwd, stdout);
79
80 if (emit_path) {
81 const char *mode_str = (emit_mode == CK_EMIT_LIBRARY) ? "library" : "standalone";
82 if (ck_codegen_emit_runtime(&graph, emit_path, emit_mode) == 0) {
83 fprintf(stderr, "\n[ck_ir_demo] %s runtime written to %s\n", mode_str, emit_path);
84 } else {
85 fprintf(stderr, "\n[ck_ir_demo] failed to write %s runtime to %s\n", mode_str, emit_path);
86 }
87 }
88
89 // If we came from config.json, also emit a JSON IR map for tooling.
90 if (strcmp(argv[1], "--ir") != 0) {
91 if (ck_ir_serialize_json(&graph, "build/ir.json") == 0) {
92 fprintf(stderr, "\n[ck_ir_demo] JSON IR written to build/ir.json\n");
93 } else {
94 fprintf(stderr, "\n[ck_ir_demo] Failed to write JSON IR to build/ir.json\n");
95 }
96 }
97
98 ck_ir_free(&graph);
99 ck_ir_free(&bwd);
100 return 0;
101}
int ck_codegen_emit_runtime(const CKIRGraph *forward, const char *path, CKEmitMode mode)
CKEmitMode
@ CK_EMIT_STANDALONE
@ CK_EMIT_LIBRARY
void ck_codegen_c_skeleton(const CKIRGraph *forward, const CKIRGraph *backward, FILE *out)
int ck_build_decoder_ir(const CKModelConfig *cfg, CKIRGraph *graph)
Definition ckernel_ir.c:305
int ck_ir_serialize_json(const CKIRGraph *graph, const char *path)
Definition ckernel_ir.c:575
int ck_build_decoder_backward_ir(const CKIRGraph *forward, CKIRGraph *backward)
Definition ckernel_ir.c:456
void ck_ir_dump(const CKIRGraph *graph, FILE *out)
Definition ckernel_ir.c:524
int ck_model_config_from_hf_json(const char *path, CKModelConfig *cfg)
Definition ckernel_ir.c:209
int ck_ir_parse_json(const char *path, CKIRGraph *graph)
Definition ckernel_ir.c:665
void ck_ir_free(CKIRGraph *graph)
Definition ckernel_ir.c:493
int main(int argc, char **argv)