← Back to C-Kernel-Engine Docs Doxygen Source Documentation
ckernel_codegen_v2_schedule.c
Go to the documentation of this file.
2 
3 #include <stdio.h>
4 
5 static const char *ck_codegen_v2_dtype_name(CKDataType dtype)
6 {
7  switch (dtype) {
8  case CK_DT_FP32:
9  return "fp32";
10  case CK_DT_BF16:
11  return "bf16";
12  case CK_DT_FP16:
13  return "fp16";
14  case CK_DT_Q4_0:
15  return "q4_0";
16  case CK_DT_Q4_K:
17  return "q4_k";
18  case CK_DT_Q6_K:
19  return "q6_k";
20  case CK_DT_Q8_0:
21  return "q8_0";
22  default:
23  return "unknown";
24  }
25 }
26 
27 static void emit_schedule_block(FILE *out,
28  const CKIRV2Graph *graph,
29  const char *func_name,
30  const char *label,
31  const char *runtime_sym)
32 {
33  fprintf(out, "void %s(void) {\n", func_name);
34  fprintf(out, " /* %s schedule (no-op placeholders). */\n", label);
35  for (int i = 0; i < graph->num_nodes; ++i) {
36  const CKIRV2Node *node = &graph->nodes[i];
37  fprintf(out,
38  " /* node %d: layer=%d op=%s kernel=%s dtype=%s */\n",
39  i,
40  (int)node->layer,
41  node->op ? node->op : "none",
42  node->kernel ? node->kernel : "none",
44  for (int b = 0; b < node->n_bindings; ++b) {
45  const CKIRV2Binding *bind = &node->bindings[b];
46  const char *buf_name = "unknown";
47  if (bind->buffer >= 0 && bind->buffer < graph->num_buffers) {
48  buf_name = graph->buffers[bind->buffer].name ? graph->buffers[bind->buffer].name : "unnamed";
49  }
50  fprintf(out,
51  " /* bind: %s -> %s */\n",
52  bind->arg ? bind->arg : "arg",
53  buf_name);
54  }
55  fprintf(out, " ck_v2_dispatch_node(%d);\n", i);
56  }
57  fprintf(out, " (void)%s;\n", runtime_sym ? runtime_sym : "ck_v2_runtime");
58  fprintf(out, "}\n\n");
59 }
60 
62  const CKIRV2Graph *graph,
63  const char *prefill_runtime,
64  const char *decode_runtime,
65  const char *backward_runtime)
66 {
67  emit_schedule_block(out, graph, "ck_v2_run_prefill", "prefill",
68  prefill_runtime ? prefill_runtime : "ck_v2_prefill_runtime");
69  emit_schedule_block(out, graph, "ck_v2_run_decode", "decode",
70  decode_runtime ? decode_runtime : "ck_v2_decode_runtime");
71  emit_schedule_block(out, graph, "ck_v2_run_backward", "backward",
72  backward_runtime ? backward_runtime : "ck_v2_backward_runtime");
73 
74  fprintf(out,
75  "void ck_v2_run_forward(void) {\n"
76  " ck_v2_run_prefill();\n"
77  "}\n\n");
78 }
static const char * ck_codegen_v2_dtype_name(CKDataType dtype)
void ck_codegen_v2_emit_schedule(FILE *out, const CKIRV2Graph *graph, const char *prefill_runtime, const char *decode_runtime, const char *backward_runtime)
static void emit_schedule_block(FILE *out, const CKIRV2Graph *graph, const char *func_name, const char *label, const char *runtime_sym)
CKDataType
Supported data types in C-Kernel-Engine.
Definition: ckernel_dtype.h:27
@ CK_DT_Q4_K
Definition: ckernel_dtype.h:40
@ CK_DT_Q4_0
Definition: ckernel_dtype.h:38
@ CK_DT_Q8_0
Definition: ckernel_dtype.h:42
@ CK_DT_FP32
Definition: ckernel_dtype.h:29
@ CK_DT_FP16
Definition: ckernel_dtype.h:31
@ CK_DT_Q6_K
Definition: ckernel_dtype.h:41
@ CK_DT_BF16
Definition: ckernel_dtype.h:30
int32_t buffer
Definition: ckernel_ir_v2.h:37
CKIRV2Node * nodes
Definition: ckernel_ir_v2.h:64
CKIRV2Buffer * buffers
Definition: ckernel_ir_v2.h:62
uint8_t n_bindings
Definition: ckernel_ir_v2.h:48
uint16_t layer
Definition: ckernel_ir_v2.h:45
CKIRV2Binding bindings[24]
Definition: ckernel_ir_v2.h:47
CKDataType kernel_dtype
Definition: ckernel_ir_v2.h:43
char * kernel
Definition: ckernel_ir_v2.h:42