← Back to C-Kernel-Engine Docs Doxygen Source Documentation
 
Loading...
Searching...
No Matches
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 */
23typedef 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)
33 int rotary_dim; // RoPE rotary dimensions (0 = use head_dim)
34 float rope_scaling_factor; // RoPE scaling factor (1.0 = no scaling)
35 char rope_scaling_type[16]; // RoPE scaling type: "none", "linear", "dynamic", "yarn"
37
55
56typedef struct {
57 uint16_t layer; // decoder layer index
58 uint16_t node; // kernel index within that layer
60
61typedef struct {
62 CKKernelId producer; // which kernel produced this input
63 uint8_t out_index; // which output slot (0 for most kernels)
65
66typedef struct {
69 CKInputRef inputs[4];
70 uint8_t n_inputs;
71 uint8_t n_outputs;
72 // For attention we may record heads; for now keep it simple.
73} CKIRNode;
74
75typedef struct {
78 CKIRNode *nodes; // heap-allocated array of length num_nodes
79} CKIRGraph;
80
81/**
82 * Parse a HuggingFace-style config.json into CKModelConfig.
83 *
84 * This is a very small, dependency-free parser that looks for a handful
85 * of integer keys. If a "text_config" object is present, it will parse
86 * the fields from that object (so vision_config does not override text).
87 *
88 * Keys recognized (with common fallbacks):
89 * - "num_hidden_layers" ("n_layer")
90 * - "hidden_size" ("n_embd", "d_model")
91 * - "intermediate_size" ("n_inner", "ffn_dim", "mlp_dim")
92 * - "num_attention_heads" ("n_head", "num_heads")
93 * - "num_key_value_heads" ("num_kv_heads") (optional; defaults to num_attention_heads)
94 * - "vocab_size" ("n_vocab") (optional)
95 * - "max_position_embeddings" ("n_positions", "context_length", "seq_len") (optional)
96 * - "rms_norm_eps" ("layer_norm_eps") (optional; defaults to 1e-5)
97 * - "rope_theta" (optional; 0 disables RoPE)
98 *
99 * Returns 0 on success, non-zero on failure.
100 */
101int ck_model_config_from_hf_json(const char *path, CKModelConfig *cfg);
102
103/**
104 * Build a simple decoder-only IR graph for the given config.
105 *
106 * For now this constructs a canonical pattern per layer:
107 * RMSNorm -> QKV Linear -> Attention -> Add (residual)
108 * RMSNorm -> W1 Linear -> Split -> SwiGLU -> W2 Linear -> Add
109 *
110 * The IR is suitable as a starting point for later fusion and codegen.
111 * Returns 0 on success, non-zero on failure.
112 */
113int ck_build_decoder_ir(const CKModelConfig *cfg, CKIRGraph *graph);
114
115/**
116 * Build a naive backward IR graph from a forward decoder IR.
117 *
118 * This reverses the node order and maps each forward op to a corresponding
119 * *_BWD op type. This is an early skeleton useful for planning backprop
120 * codegen; it does not yet encode full gradient wiring.
121 */
122int ck_build_decoder_backward_ir(const CKIRGraph *forward, CKIRGraph *backward);
123
124/**
125 * Free any heap-allocated memory owned by the graph.
126 */
127void ck_ir_free(CKIRGraph *graph);
128
129/**
130 * Dump a human-readable view of the IR to the given stream.
131 */
132void ck_ir_dump(const CKIRGraph *graph, FILE *out);
133
134/**
135 * Serialize a CKIRGraph to a simple JSON IR map file.
136 *
137 * This is an explicit per-node, per-layer view of the decoder stack. It is
138 * intentionally low-level and mirrors ck_ir_dump(), but in a machine-readable
139 * format. Higher-level tooling can later reorganize this into header/block/
140 * footer sections.
141 *
142 * Returns 0 on success, non-zero on failure.
143 */
144int ck_ir_serialize_json(const CKIRGraph *graph, const char *path);
145
146/**
147 * Parse a JSON IR map file (as produced by ck_ir_serialize_json) back into
148 * a CKIRGraph. This enables a two-stage pipeline:
149 *
150 * config.json -> CKIRGraph -> ir.json
151 * ir.json -> CKIRGraph -> codegen / analysis
152 *
153 * Returns 0 on success, non-zero on failure.
154 */
155int ck_ir_parse_json(const char *path, CKIRGraph *graph);
156
157#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:38
@ CK_OP_LINEAR_BWD
Definition ckernel_ir.h:51
@ CK_OP_SWIGLU
Definition ckernel_ir.h:45
@ CK_OP_RMSNORM_BWD
Definition ckernel_ir.h:47
@ CK_OP_SWIGLU_BWD
Definition ckernel_ir.h:53
@ CK_OP_ADD
Definition ckernel_ir.h:42
@ CK_OP_SPLIT
Definition ckernel_ir.h:44
@ CK_OP_LINEAR_QKV_BWD
Definition ckernel_ir.h:48
@ CK_OP_ATTENTION_BWD
Definition ckernel_ir.h:49
@ CK_OP_SPLIT_BWD
Definition ckernel_ir.h:52
@ CK_OP_LINEAR_QKV
Definition ckernel_ir.h:40
@ CK_OP_LINEAR
Definition ckernel_ir.h:43
@ CK_OP_RMSNORM
Definition ckernel_ir.h:39
@ CK_OP_ADD_BWD
Definition ckernel_ir.h:50
@ CK_OP_ATTENTION
Definition ckernel_ir.h:41
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:78
int num_nodes
Definition ckernel_ir.h:77
CKModelConfig config
Definition ckernel_ir.h:76
CKOpType op
Definition ckernel_ir.h:68
CKKernelId id
Definition ckernel_ir.h:67
uint8_t n_inputs
Definition ckernel_ir.h:70
uint8_t n_outputs
Definition ckernel_ir.h:71
CKKernelId producer
Definition ckernel_ir.h:62
uint8_t out_index
Definition ckernel_ir.h:63
uint16_t node
Definition ckernel_ir.h:58
uint16_t layer
Definition ckernel_ir.h:57
float rms_norm_eps
Definition ckernel_ir.h:31
float rope_scaling_factor
Definition ckernel_ir.h:34
float rope_theta
Definition ckernel_ir.h:32
int vocab_size
Definition true_bpe.h:193