← Back to C-Kernel-Engine Docs Doxygen Source Documentation
ckernel_ir.h
Go to the documentation of this file.
1 #ifndef CKERNEL_IR_H
2 #define CKERNEL_IR_H
3 
4 #include <stdint.h>
5 #include <stdio.h>
6 
7 /**
8  * Minimal HF-style model config extracted from config.json.
9  *
10  * Design note:
11  * - The long-term IR structure follows a "header / block / footer" pattern,
12  * inspired by the Antsand HMVC website generator:
13  * - header: one-time ops before the decoder stack (embeddings, pos-enc, etc.)
14  * - block : per-layer ops (RMSNorm, attention, MLP/SwiGLU, residual adds)
15  * - footer: one-time ops after the stack (final norm, LM head / weight-tying, loss)
16  * - In code this will eventually become a double-array layout (layers x ops)
17  * for the block section, plus separate header/footer op lists. The current
18  * CKIRGraph is the primitive building block for that higher-level IR.
19  *
20  * This header focuses on the basic decoder-only transformer parameters and
21  * node-level IR that higher layers (codegen, memory planner, etc.) will build on.
22  */
23 typedef struct {
24  int num_layers; // num_hidden_layers
25  int hidden_size; // hidden_size / d_model
26  int intermediate_size; // intermediate_size (MLP)
27  int num_heads; // num_attention_heads
28  int num_kv_heads; // num_key_value_heads (GQA)
29  int vocab_size; // vocab_size (if available)
30  int context_window; // max positions / context length (if available)
31  float rms_norm_eps; // RMSNorm epsilon (if available)
32  float rope_theta; // RoPE base (0 disables RoPE)
34 
35 typedef enum {
43  /* Backward ops (mirrors forward ops) */
52 
53 typedef struct {
54  uint16_t layer; // decoder layer index
55  uint16_t node; // kernel index within that layer
56 } CKKernelId;
57 
58 typedef struct {
59  CKKernelId producer; // which kernel produced this input
60  uint8_t out_index; // which output slot (0 for most kernels)
61 } CKInputRef;
62 
63 typedef struct {
66  CKInputRef inputs[4];
67  uint8_t n_inputs;
68  uint8_t n_outputs;
69  // For attention we may record heads; for now keep it simple.
70 } CKIRNode;
71 
72 typedef struct {
74  int num_nodes;
75  CKIRNode *nodes; // heap-allocated array of length num_nodes
76 } CKIRGraph;
77 
78 /**
79  * Parse a HuggingFace-style config.json into CKModelConfig.
80  *
81  * This is a very small, dependency-free parser that looks for a handful
82  * of integer keys. If a "text_config" object is present, it will parse
83  * the fields from that object (so vision_config does not override text).
84  *
85  * Keys recognized (with common fallbacks):
86  * - "num_hidden_layers" ("n_layer")
87  * - "hidden_size" ("n_embd", "d_model")
88  * - "intermediate_size" ("n_inner", "ffn_dim", "mlp_dim")
89  * - "num_attention_heads" ("n_head", "num_heads")
90  * - "num_key_value_heads" ("num_kv_heads") (optional; defaults to num_attention_heads)
91  * - "vocab_size" ("n_vocab") (optional)
92  * - "max_position_embeddings" ("n_positions", "context_length", "seq_len") (optional)
93  * - "rms_norm_eps" ("layer_norm_eps") (optional; defaults to 1e-5)
94  * - "rope_theta" (optional; 0 disables RoPE)
95  *
96  * Returns 0 on success, non-zero on failure.
97  */
98 int ck_model_config_from_hf_json(const char *path, CKModelConfig *cfg);
99 
100 /**
101  * Build a simple decoder-only IR graph for the given config.
102  *
103  * For now this constructs a canonical pattern per layer:
104  * RMSNorm -> QKV Linear -> Attention -> Add (residual)
105  * RMSNorm -> W1 Linear -> Split -> SwiGLU -> W2 Linear -> Add
106  *
107  * The IR is suitable as a starting point for later fusion and codegen.
108  * Returns 0 on success, non-zero on failure.
109  */
110 int ck_build_decoder_ir(const CKModelConfig *cfg, CKIRGraph *graph);
111 
112 /**
113  * Build a naive backward IR graph from a forward decoder IR.
114  *
115  * This reverses the node order and maps each forward op to a corresponding
116  * *_BWD op type. This is an early skeleton useful for planning backprop
117  * codegen; it does not yet encode full gradient wiring.
118  */
119 int ck_build_decoder_backward_ir(const CKIRGraph *forward, CKIRGraph *backward);
120 
121 /**
122  * Free any heap-allocated memory owned by the graph.
123  */
124 void ck_ir_free(CKIRGraph *graph);
125 
126 /**
127  * Dump a human-readable view of the IR to the given stream.
128  */
129 void ck_ir_dump(const CKIRGraph *graph, FILE *out);
130 
131 /**
132  * Serialize a CKIRGraph to a simple JSON IR map file.
133  *
134  * This is an explicit per-node, per-layer view of the decoder stack. It is
135  * intentionally low-level and mirrors ck_ir_dump(), but in a machine-readable
136  * format. Higher-level tooling can later reorganize this into header/block/
137  * footer sections.
138  *
139  * Returns 0 on success, non-zero on failure.
140  */
141 int ck_ir_serialize_json(const CKIRGraph *graph, const char *path);
142 
143 /**
144  * Parse a JSON IR map file (as produced by ck_ir_serialize_json) back into
145  * a CKIRGraph. This enables a two-stage pipeline:
146  *
147  * config.json -> CKIRGraph -> ir.json
148  * ir.json -> CKIRGraph -> codegen / analysis
149  *
150  * Returns 0 on success, non-zero on failure.
151  */
152 int ck_ir_parse_json(const char *path, CKIRGraph *graph);
153 
154 #endif /* CKERNEL_IR_H */
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
CKOpType
Definition: ckernel_ir.h:35
@ CK_OP_LINEAR_BWD
Definition: ckernel_ir.h:48
@ CK_OP_SWIGLU
Definition: ckernel_ir.h:42
@ CK_OP_RMSNORM_BWD
Definition: ckernel_ir.h:44
@ CK_OP_SWIGLU_BWD
Definition: ckernel_ir.h:50
@ CK_OP_ADD
Definition: ckernel_ir.h:39
@ CK_OP_SPLIT
Definition: ckernel_ir.h:41
@ CK_OP_LINEAR_QKV_BWD
Definition: ckernel_ir.h:45
@ CK_OP_ATTENTION_BWD
Definition: ckernel_ir.h:46
@ CK_OP_SPLIT_BWD
Definition: ckernel_ir.h:49
@ CK_OP_LINEAR_QKV
Definition: ckernel_ir.h:37
@ CK_OP_LINEAR
Definition: ckernel_ir.h:40
@ CK_OP_RMSNORM
Definition: ckernel_ir.h:36
@ CK_OP_ADD_BWD
Definition: ckernel_ir.h:47
@ CK_OP_ATTENTION
Definition: ckernel_ir.h:38
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
CKIRNode * nodes
Definition: ckernel_ir.h:75
int num_nodes
Definition: ckernel_ir.h:74
CKModelConfig config
Definition: ckernel_ir.h:73
CKOpType op
Definition: ckernel_ir.h:65
CKKernelId id
Definition: ckernel_ir.h:64
uint8_t n_inputs
Definition: ckernel_ir.h:67
uint8_t n_outputs
Definition: ckernel_ir.h:68
CKKernelId producer
Definition: ckernel_ir.h:59
uint8_t out_index
Definition: ckernel_ir.h:60
uint16_t node
Definition: ckernel_ir.h:55
uint16_t layer
Definition: ckernel_ir.h:54
int context_window
Definition: ckernel_ir.h:30
float rms_norm_eps
Definition: ckernel_ir.h:31
float rope_theta
Definition: ckernel_ir.h:32
int vocab_size
Definition: true_bpe.h:185