#include "ckernel_ir.h"#include <stdbool.h>#include <stdio.h>#include <stdlib.h>#include <string.h>Go to the source code of this file.
Functions | |
| int | ck_build_decoder_backward_ir (const CKIRGraph *forward, CKIRGraph *backward) |
| int | ck_build_decoder_ir (const CKModelConfig *cfg, CKIRGraph *graph) |
| void | ck_ir_dump (const CKIRGraph *graph, FILE *out) |
| void | ck_ir_free (CKIRGraph *graph) |
| int | ck_ir_parse_json (const char *path, CKIRGraph *graph) |
| int | ck_ir_serialize_json (const CKIRGraph *graph, const char *path) |
| int | ck_model_config_from_hf_json (const char *path, CKModelConfig *cfg) |
| static int | find_object_range (const char *json, const char *key, const char **out_start, size_t *out_len) |
| static CKOpType | map_forward_to_backward (CKOpType op) |
| static const char * | op_name (CKOpType op) |
| static int | parse_float_field_any (const char *json, size_t len, const char *const *keys, float *out_value) |
| static int | parse_float_field_in_range (const char *json, size_t len, const char *key, float *out_value) |
| static int | parse_int_field (const char *json, const char *key, int *out_value) |
| static int | parse_int_field_any (const char *json, size_t len, const char *const *keys, int *out_value) |
| static int | parse_int_field_in_range (const char *json, size_t len, const char *key, int *out_value) |
| static CKOpType | parse_op (const char *s) |
Build a naive backward IR graph from a forward decoder IR.
This reverses the node order and maps each forward op to a corresponding *_BWD op type. This is an early skeleton useful for planning backprop codegen; it does not yet encode full gradient wiring.
Definition at line 456 of file ckernel_ir_v6.6.c.
References CKIRGraph::config, CKIRNode::id, CKIRNode::inputs, map_forward_to_backward(), CKIRNode::n_inputs, CKIRNode::n_outputs, CKIRGraph::nodes, CKIRGraph::num_nodes, and CKIRNode::op.
Referenced by main().
| int ck_build_decoder_ir | ( | const CKModelConfig * | cfg, |
| CKIRGraph * | graph | ||
| ) |
Build a simple decoder-only IR graph for the given config.
For now this constructs a canonical pattern per layer: RMSNorm -> QKV Linear -> Attention -> Add (residual) RMSNorm -> W1 Linear -> Split -> SwiGLU -> W2 Linear -> Add
The IR is suitable as a starting point for later fusion and codegen. Returns 0 on success, non-zero on failure.
Definition at line 305 of file ckernel_ir_v6.6.c.
References CK_OP_ADD, CK_OP_ATTENTION, CK_OP_LINEAR, CK_OP_LINEAR_QKV, CK_OP_RMSNORM, CK_OP_SPLIT, CK_OP_SWIGLU, CKIRGraph::config, CKIRNode::id, CKIRNode::inputs, CKKernelId::layer, CKIRNode::n_inputs, CKIRNode::n_outputs, CKKernelId::node, CKIRGraph::nodes, CKModelConfig::num_layers, CKIRGraph::num_nodes, CKIRNode::op, CKInputRef::out_index, and CKInputRef::producer.
Referenced by main().
| void ck_ir_dump | ( | const CKIRGraph * | graph, |
| FILE * | out | ||
| ) |
Dump a human-readable view of the IR to the given stream.
Definition at line 524 of file ckernel_ir_v6.6.c.
References CKIRGraph::config, CKModelConfig::context_window, CKModelConfig::hidden_size, CKIRNode::id, CKIRNode::inputs, CKModelConfig::intermediate_size, CKKernelId::layer, CKIRNode::n_inputs, CKIRNode::n_outputs, CKKernelId::node, CKIRGraph::nodes, CKModelConfig::num_heads, CKModelConfig::num_kv_heads, CKModelConfig::num_layers, CKIRGraph::num_nodes, CKIRNode::op, op_name(), CKInputRef::producer, CKModelConfig::rms_norm_eps, CKModelConfig::rope_theta, and CKModelConfig::vocab_size.
Referenced by main().
| void ck_ir_free | ( | CKIRGraph * | graph | ) |
Free any heap-allocated memory owned by the graph.
Definition at line 493 of file ckernel_ir_v6.6.c.
References CKIRGraph::nodes, and CKIRGraph::num_nodes.
Referenced by main().
| int ck_ir_parse_json | ( | const char * | path, |
| CKIRGraph * | graph | ||
| ) |
Parse a JSON IR map file (as produced by ck_ir_serialize_json) back into a CKIRGraph. This enables a two-stage pipeline:
config.json -> CKIRGraph -> ir.json ir.json -> CKIRGraph -> codegen / analysis
Returns 0 on success, non-zero on failure.
Definition at line 665 of file ckernel_ir_v6.6.c.
References CKIRGraph::config, CKModelConfig::context_window, CKModelConfig::hidden_size, CKIRNode::id, CKIRNode::inputs, CKModelConfig::intermediate_size, CKKernelId::layer, CKIRNode::n_inputs, CKIRNode::n_outputs, CKKernelId::node, CKIRGraph::nodes, CKModelConfig::num_heads, CKModelConfig::num_kv_heads, CKModelConfig::num_layers, CKIRGraph::num_nodes, CKIRNode::op, CKInputRef::out_index, parse_float_field_in_range(), parse_int_field(), parse_op(), CKInputRef::producer, CKModelConfig::rms_norm_eps, CKModelConfig::rope_theta, and CKModelConfig::vocab_size.
Referenced by main().
| int ck_ir_serialize_json | ( | const CKIRGraph * | graph, |
| const char * | path | ||
| ) |
Serialize a CKIRGraph to a simple JSON IR map file.
This is an explicit per-node, per-layer view of the decoder stack. It is intentionally low-level and mirrors ck_ir_dump(), but in a machine-readable format. Higher-level tooling can later reorganize this into header/block/ footer sections.
Returns 0 on success, non-zero on failure.
Definition at line 575 of file ckernel_ir_v6.6.c.
References CKIRGraph::config, CKModelConfig::context_window, CKModelConfig::hidden_size, CKIRNode::id, CKIRNode::inputs, CKModelConfig::intermediate_size, CKKernelId::layer, CKIRNode::n_inputs, CKIRNode::n_outputs, CKKernelId::node, CKIRGraph::nodes, CKModelConfig::num_heads, CKModelConfig::num_kv_heads, CKModelConfig::num_layers, CKIRGraph::num_nodes, CKIRNode::op, op_name(), CKInputRef::out_index, CKInputRef::producer, CKModelConfig::rms_norm_eps, CKModelConfig::rope_theta, and CKModelConfig::vocab_size.
Referenced by main().
| int ck_model_config_from_hf_json | ( | const char * | path, |
| CKModelConfig * | cfg | ||
| ) |
Parse a HuggingFace-style config.json into CKModelConfig.
This is a very small, dependency-free parser that looks for a handful of integer keys. If a "text_config" object is present, it will parse the fields from that object (so vision_config does not override text).
Keys recognized (with common fallbacks):
Returns 0 on success, non-zero on failure.
Definition at line 209 of file ckernel_ir_v6.6.c.
References CKModelConfig::context_window, find_object_range(), CKModelConfig::hidden_size, CKModelConfig::intermediate_size, CKModelConfig::num_heads, CKModelConfig::num_kv_heads, CKModelConfig::num_layers, parse_float_field_any(), parse_int_field_any(), CKModelConfig::rms_norm_eps, CKModelConfig::rope_theta, and CKModelConfig::vocab_size.
Referenced by main().
|
static |
Definition at line 139 of file ckernel_ir_v6.6.c.
References out_len, and start.
Referenced by ck_model_config_from_hf_json().
Definition at line 442 of file ckernel_ir_v6.6.c.
References CK_OP_ADD, CK_OP_ADD_BWD, CK_OP_ATTENTION, CK_OP_ATTENTION_BWD, CK_OP_LINEAR, CK_OP_LINEAR_BWD, CK_OP_LINEAR_QKV, CK_OP_LINEAR_QKV_BWD, CK_OP_RMSNORM, CK_OP_RMSNORM_BWD, CK_OP_SPLIT, CK_OP_SPLIT_BWD, CK_OP_SWIGLU, and CK_OP_SWIGLU_BWD.
Referenced by ck_build_decoder_backward_ir().
|
static |
Definition at line 503 of file ckernel_ir_v6.6.c.
References CK_OP_ADD, CK_OP_ADD_BWD, CK_OP_ATTENTION, CK_OP_ATTENTION_BWD, CK_OP_LINEAR, CK_OP_LINEAR_BWD, CK_OP_LINEAR_QKV, CK_OP_LINEAR_QKV_BWD, CK_OP_RMSNORM, CK_OP_RMSNORM_BWD, CK_OP_SPLIT, CK_OP_SPLIT_BWD, CK_OP_SWIGLU, and CK_OP_SWIGLU_BWD.
Referenced by ck_ir_dump(), and ck_ir_serialize_json().
|
static |
Definition at line 123 of file ckernel_ir_v6.6.c.
References parse_float_field_in_range().
Referenced by ck_model_config_from_hf_json().
|
static |
Definition at line 87 of file ckernel_ir_v6.6.c.
References end.
Referenced by ck_ir_parse_json(), and parse_float_field_any().
|
static |
|
static |
Definition at line 71 of file ckernel_ir_v6.6.c.
References parse_int_field_in_range().
Referenced by ck_model_config_from_hf_json().
|
static |
Definition at line 35 of file ckernel_ir_v6.6.c.
References end.
Referenced by parse_int_field_any().
|
static |
Definition at line 646 of file ckernel_ir_v6.6.c.
References CK_OP_ADD, CK_OP_ADD_BWD, CK_OP_ATTENTION, CK_OP_ATTENTION_BWD, CK_OP_LINEAR, CK_OP_LINEAR_BWD, CK_OP_LINEAR_QKV, CK_OP_LINEAR_QKV_BWD, CK_OP_RMSNORM, CK_OP_RMSNORM_BWD, CK_OP_SPLIT, CK_OP_SPLIT_BWD, CK_OP_SWIGLU, and CK_OP_SWIGLU_BWD.
Referenced by ck_ir_parse_json().