#include "ckernel_codegen.h"#include "ckernel_registry.h"#include "ckernel_kernel_specs.h"#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <string.h>Go to the source code of this file.
Functions | |
| static int | ck_buffer_should_alloc (const CKBufferSpec *spec) |
| static int | ck_buffer_uses_weight_dtype (const CKBufferSpec *spec) |
| void | ck_codegen_c_skeleton (const CKIRGraph *forward, const CKIRGraph *backward, FILE *out) |
| int | ck_codegen_emit_runtime (const CKIRGraph *forward, const char *path, CKEmitMode mode) |
| static const CKBufferSpec * | ck_find_buffer_spec (const char *name) |
| static const CKKernelSpec * | ck_find_kernel_spec (const char *name) |
| static const char * | ck_first_layer_buffer_name (void) |
| static int | ck_plan_step_enabled (const CKPlanStep *step, const CKIRGraph *cfg) |
| static const char * | ck_weight_dtype_expr (const CKBufferSpec *spec) |
| static void | emit_bump_bytes_assignment (FILE *out, const char *indent, const char *struct_prefix, const char *name, const CKDimToken *shape) |
| static void | emit_bump_bytes_assignment_weight_dtype (FILE *out, const char *indent, const char *struct_prefix, const char *name, const CKDimToken *shape, const char *dtype_expr) |
| static void | emit_dim_expr (FILE *out, CKDimKind dim) |
| static void | emit_global_aliases_to_layer (FILE *out) |
| static void | emit_global_allocations (FILE *out) |
| static void | emit_global_offset_fields (FILE *out) |
| static int | emit_kernel_manifest (const CKIRGraph *forward, const char *runtime_path) |
| static void | emit_layer_allocations (FILE *out) |
| static void | emit_layer_offsets_struct (FILE *out) |
| static void | emit_library_api (FILE *out, const CKIRGraph *forward) |
| static void | emit_model_struct (FILE *out) |
| static void | emit_offset_field (FILE *out, const char *name) |
| static int | emit_plan_sources (FILE *f, const CKPlanStep *plan, size_t plan_count, const CKIRGraph *cfg, const char **seen, size_t *seen_count, size_t seen_cap) |
| static int | emit_runtime_preamble (FILE *out) |
| static void | emit_sgd_update (FILE *out) |
| static void | emit_shape_expr (FILE *out, const CKDimToken *shape) |
| static void | emit_training_conditional_assignment (FILE *out, const char *indent, const char *struct_prefix, const char *name, const CKDimToken *shape) |
| static int | emit_unique_source (FILE *f, const char *path, const char **seen, size_t *seen_count, size_t seen_cap) |
| static void | emit_zero_grad (FILE *out) |
| static const char * | op_name (CKOpType op) |
|
static |
Definition at line 57 of file ckernel_codegen_v6.6.c.
References CK_ROLE_INPUT, and CKBufferSpec::role.
Referenced by ck_first_layer_buffer_name(), emit_global_allocations(), emit_global_offset_fields(), emit_layer_allocations(), and emit_layer_offsets_struct().
|
static |
Definition at line 62 of file ckernel_codegen_v6.6.c.
References CK_ROLE_WEIGHT, CKBufferSpec::name, and CKBufferSpec::role.
Referenced by emit_global_allocations(), and emit_layer_allocations().
Emit a C skeleton for forward + backward execution based on the IR.
This does not yet generate full pointer arithmetic or memory planning. It is intended as a starting point that:
Definition at line 613 of file ckernel_codegen_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_codegen_emit_runtime | ( | const CKIRGraph * | forward, |
| const char * | path, | ||
| CKEmitMode | mode | ||
| ) |
Emit a C runtime file that stitches kernels for the given forward IR.
| forward | The forward IR graph |
| path | Output file path |
| mode | CK_EMIT_STANDALONE for executable with main(), CK_EMIT_LIBRARY for shared object with API functions |
Returns 0 on success, non-zero on failure.
Definition at line 1439 of file ckernel_codegen_v6.6.c.
References CK_EMIT_STANDALONE, ck_first_layer_buffer_name(), ck_ir_validate_supported(), CKIRGraph::config, CKModelConfig::context_window, emit_global_aliases_to_layer(), emit_global_allocations(), emit_kernel_manifest(), emit_layer_allocations(), emit_layer_offsets_struct(), emit_library_api(), emit_model_struct(), emit_runtime_preamble(), emit_sgd_update(), emit_zero_grad(), CKModelConfig::hidden_size, CKModelConfig::intermediate_size, CKModelConfig::num_heads, CKModelConfig::num_kv_heads, CKModelConfig::num_layers, CKModelConfig::rms_norm_eps, CKModelConfig::rope_theta, and CKModelConfig::vocab_size.
Referenced by main().
|
static |
Definition at line 31 of file ckernel_codegen_v6.6.c.
References ck_decoder_buffer_count, and ck_decoder_buffers.
Referenced by emit_global_aliases_to_layer(), emit_global_allocations(), and emit_sgd_update().
|
static |
Definition at line 44 of file ckernel_codegen_v6.6.c.
References ck_kernel_spec_count, and ck_kernel_specs.
Referenced by emit_plan_sources().
|
static |
Definition at line 598 of file ckernel_codegen_v6.6.c.
References ck_buffer_should_alloc(), ck_decoder_buffer_count, ck_decoder_buffers, CK_SCOPE_LAYER, CKBufferSpec::name, and CKBufferSpec::scope.
Referenced by ck_codegen_emit_runtime().
|
static |
Definition at line 554 of file ckernel_codegen_v6.6.c.
References CKPlanStep::condition, CKIRGraph::config, and CKModelConfig::rope_theta.
Referenced by emit_plan_sources().
|
static |
Definition at line 80 of file ckernel_codegen_v6.6.c.
References CK_ROLE_WEIGHT, CK_SCOPE_GLOBAL, CK_SCOPE_LAYER, CKBufferSpec::name, CKBufferSpec::role, and CKBufferSpec::scope.
Referenced by emit_global_allocations(), and emit_layer_allocations().
|
static |
Definition at line 275 of file ckernel_codegen_v6.6.c.
References emit_shape_expr().
Referenced by emit_global_allocations(), and emit_layer_allocations().
|
static |
Definition at line 286 of file ckernel_codegen_v6.6.c.
References emit_shape_expr().
Referenced by emit_global_allocations(), and emit_layer_allocations().
|
static |
Definition at line 231 of file ckernel_codegen_v6.6.c.
References CK_DIM_ALIGNED_CTX, CK_DIM_ALIGNED_EMBED, CK_DIM_ALIGNED_HEAD, CK_DIM_ALIGNED_INTERMEDIATE, CK_DIM_EMBED, CK_DIM_END, CK_DIM_HEAD_DIM, CK_DIM_INTERMEDIATE, CK_DIM_NUM_HEADS, CK_DIM_NUM_KV_HEADS, CK_DIM_TOKENS, and CK_DIM_VOCAB.
Referenced by emit_shape_expr().
|
static |
Definition at line 398 of file ckernel_codegen_v6.6.c.
References CKBufferSpec::alias_of, ck_decoder_buffer_count, ck_decoder_buffers, ck_find_buffer_spec(), CK_SCOPE_GLOBAL, CK_SCOPE_LAYER, CKBufferSpec::name, and CKBufferSpec::scope.
Referenced by ck_codegen_emit_runtime().
|
static |
Definition at line 311 of file ckernel_codegen_v6.6.c.
References CKBufferSpec::alias_of, ck_buffer_should_alloc(), ck_buffer_uses_weight_dtype(), ck_decoder_buffer_count, ck_decoder_buffers, ck_find_buffer_spec(), CK_ROLE_GRAD, CK_SCOPE_GLOBAL, ck_weight_dtype_expr(), CKBufferSpec::condition, emit_bump_bytes_assignment(), emit_bump_bytes_assignment_weight_dtype(), emit_shape_expr(), emit_training_conditional_assignment(), CKBufferSpec::name, CKBufferSpec::role, CKBufferSpec::scope, and CKBufferSpec::shape.
Referenced by ck_codegen_emit_runtime().
|
static |
Definition at line 134 of file ckernel_codegen_v6.6.c.
References ck_buffer_should_alloc(), ck_decoder_buffer_count, ck_decoder_buffers, CK_SCOPE_GLOBAL, emit_offset_field(), CKBufferSpec::name, and CKBufferSpec::scope.
Referenced by emit_model_struct().
|
static |
Definition at line 820 of file ckernel_codegen_v6.6.c.
References ck_decoder_backward_plan, ck_decoder_backward_plan_count, ck_decoder_forward_plan, ck_decoder_forward_plan_count, ck_kernel_spec_count, CKERNEL_MAX_KERNEL_SOURCES, emit_plan_sources(), and emit_unique_source().
Referenced by ck_codegen_emit_runtime().
|
static |
Definition at line 363 of file ckernel_codegen_v6.6.c.
References ck_buffer_should_alloc(), ck_buffer_uses_weight_dtype(), ck_decoder_buffer_count, ck_decoder_buffers, CK_ROLE_GRAD, CK_SCOPE_LAYER, ck_weight_dtype_expr(), CKBufferSpec::condition, emit_bump_bytes_assignment(), emit_bump_bytes_assignment_weight_dtype(), emit_shape_expr(), emit_training_conditional_assignment(), CKBufferSpec::name, CKBufferSpec::role, CKBufferSpec::scope, and CKBufferSpec::shape.
Referenced by ck_codegen_emit_runtime().
|
static |
Definition at line 111 of file ckernel_codegen_v6.6.c.
References ck_buffer_should_alloc(), ck_decoder_buffer_count, ck_decoder_buffers, CK_SCOPE_LAYER, emit_offset_field(), CKBufferSpec::name, and CKBufferSpec::scope.
Referenced by ck_codegen_emit_runtime().
|
static |
Definition at line 916 of file ckernel_codegen_v6.6.c.
References CKIRGraph::config, CKModelConfig::context_window, CKModelConfig::hidden_size, CKModelConfig::intermediate_size, CKModelConfig::num_heads, CKModelConfig::num_kv_heads, CKModelConfig::num_layers, CKModelConfig::rms_norm_eps, CKModelConfig::rope_theta, and CKModelConfig::vocab_size.
Referenced by ck_codegen_emit_runtime().
|
static |
Definition at line 148 of file ckernel_codegen_v6.6.c.
References emit_global_offset_fields().
Referenced by ck_codegen_emit_runtime().
|
static |
Definition at line 106 of file ckernel_codegen_v6.6.c.
Referenced by emit_global_offset_fields(), and emit_layer_offsets_struct().
|
static |
Definition at line 568 of file ckernel_codegen_v6.6.c.
References ck_find_kernel_spec(), ck_plan_step_enabled(), CKERNEL_MAX_KERNEL_SOURCES, emit_unique_source(), CKPlanStep::kernel, and CKKernelSpec::sources.
Referenced by emit_kernel_manifest().
|
static |
Definition at line 758 of file ckernel_codegen_v6.6.c.
Referenced by ck_codegen_emit_runtime().
|
static |
Definition at line 459 of file ckernel_codegen_v6.6.c.
References CKBufferSpec::alias_of, ck_decoder_buffer_count, ck_decoder_buffers, ck_find_buffer_spec(), CK_ROLE_WEIGHT, CK_SCOPE_GLOBAL, CK_SCOPE_LAYER, emit_shape_expr(), CKBufferSpec::name, CKBufferSpec::role, CKBufferSpec::scope, and CKBufferSpec::shape.
Referenced by ck_codegen_emit_runtime().
|
static |
Definition at line 249 of file ckernel_codegen_v6.6.c.
References CK_DIM_END, and emit_dim_expr().
Referenced by emit_bump_bytes_assignment(), emit_bump_bytes_assignment_weight_dtype(), emit_global_allocations(), emit_layer_allocations(), emit_sgd_update(), emit_training_conditional_assignment(), and emit_zero_grad().
|
static |
Definition at line 299 of file ckernel_codegen_v6.6.c.
References emit_shape_expr().
Referenced by emit_global_allocations(), and emit_layer_allocations().
|
static |
Definition at line 530 of file ckernel_codegen_v6.6.c.
Referenced by emit_kernel_manifest(), and emit_plan_sources().
|
static |
Definition at line 419 of file ckernel_codegen_v6.6.c.
References ck_decoder_buffer_count, ck_decoder_buffers, CK_ROLE_GRAD, CK_SCOPE_GLOBAL, CK_SCOPE_LAYER, emit_shape_expr(), CKBufferSpec::name, CKBufferSpec::role, CKBufferSpec::scope, and CKBufferSpec::shape.
Referenced by ck_codegen_emit_runtime().
|
static |
Definition at line 10 of file ckernel_codegen_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_codegen_c_skeleton().