10#define CK_ENABLE_LLAMA_CPP_PARITY 1
14#include <ggml-backend.h>
15#include <ggml-alloc.h>
18#define RTLD_DEFAULT ((void *)0)
27typedef struct ggml_tensor *(*ck_ggml_new_tensor_1d_fn)(
struct ggml_context *,
enum ggml_type, int64_t);
28typedef struct ggml_tensor *(*ck_ggml_new_tensor_2d_fn)(
struct ggml_context *,
enum ggml_type, int64_t, int64_t);
29typedef struct ggml_tensor *(*ck_ggml_view_3d_fn)(
struct ggml_context *,
struct ggml_tensor *, int64_t, int64_t, int64_t, size_t, size_t, size_t);
30typedef struct ggml_tensor *(*ck_ggml_permute_fn)(
struct ggml_context *,
struct ggml_tensor *, int, int, int, int);
31typedef struct ggml_tensor *(*ck_ggml_cont_fn)(
struct ggml_context *,
struct ggml_tensor *);
32typedef struct ggml_tensor *(*ck_ggml_cont_2d_fn)(
struct ggml_context *,
struct ggml_tensor *, int64_t, int64_t);
33typedef struct ggml_tensor *(*ck_ggml_mul_mat_graph_fn)(
struct ggml_context *,
struct ggml_tensor *,
struct ggml_tensor *);
34typedef struct ggml_tensor *(*ck_ggml_soft_max_ext_fn)(
struct ggml_context *,
struct ggml_tensor *,
struct ggml_tensor *, float, float);
35typedef struct ggml_cgraph *(*ck_ggml_new_graph_fn)(
struct ggml_context *);
73 const char *v = getenv(
"CK_STRICT_ATTN_DUMP");
74 return v && v[0] && strcmp(v,
"0") != 0;
79 const char *v = getenv(
"CK_STRICT_ATTN_DUMP_META");
80 return v && v[0] && strcmp(v,
"0") != 0;
85 const char *v = getenv(
"CK_STRICT_ATTN_DUMP_LAYER");
90 long parsed = strtol(v, &
end, 10);
91 if (
end == v || (
end && *
end !=
'\0') || parsed < 0 || parsed > INT32_MAX) {
95 *layer_id_out = (int) parsed;
102 int exact_layer = -1;
106 return layer_id == exact_layer;
113 int aligned_head_dim)
115 return ((
size_t) h * (
size_t) num_tokens + (
size_t) t) * (size_t) aligned_head_dim +
132 const uint8_t *base = (
const uint8_t *) t->data;
134 i0 * (size_t) t->nb[0] +
135 i1 * (
size_t) t->nb[1] +
136 i2 * (size_t) t->nb[2] +
137 i3 * (
size_t) t->nb[3];
138 return *(
const float *) (base + off);
143 const struct ggml_tensor *t)
145 const char *dir = getenv(
"CK_PARITY_DIR");
147 !dir || !dir[0] || !name || !name[0] || !t || !t->data ||
155 const int64_t ne0 = t->ne[0] > 0 ? t->ne[0] : 1;
156 const int64_t ne1 = t->ne[1] > 0 ? t->ne[1] : 1;
157 const int64_t ne2 = t->ne[2] > 0 ? t->ne[2] : 1;
158 const int64_t ne3 = t->ne[3] > 0 ? t->ne[3] : 1;
159 const size_t elem_count =
160 (size_t) ne0 * (
size_t) ne1 * (size_t) ne2 * (
size_t) ne3;
161 if (elem_count == 0) {
164 const size_t nbytes = elem_count *
sizeof(float);
166 const uint8_t *base = NULL;
168 if (t->buffer != NULL) {
170 if (!ggml_backend_tensor_get_fn) {
173 host = (
float *) malloc(nbytes);
177 ggml_backend_tensor_get_fn(t, host, 0, nbytes);
178 base = (
const uint8_t *) host;
179 }
else if (t->data != NULL) {
180 base = (
const uint8_t *) t->data;
188 snprintf(path,
sizeof(path),
"%s/%s", dir,
"strict_internal.bin");
189 FILE *f = fopen(path,
"ab");
195 ck_attention_oracle_dump_header_t h;
196 memset(&h, 0,
sizeof(h));
199 h.layer_id = layer_id;
200 strncpy(h.op_name, name,
sizeof(h.op_name) - 1);
203 h.shape[0] = (int64_t) elem_count;
204 h.elem_count = (uint32_t) elem_count;
207 fwrite(&h,
sizeof(h), 1, f);
208 for (int64_t i3 = 0; i3 < ne3; ++i3) {
209 for (int64_t i2 = 0; i2 < ne2; ++i2) {
210 for (int64_t i1 = 0; i1 < ne1; ++i1) {
211 for (int64_t i0 = 0; i0 < ne0; ++i0) {
213 (size_t) i0 * (
size_t) t->nb[0] +
214 (size_t) i1 * (
size_t) t->nb[1] +
215 (size_t) i2 * (
size_t) t->nb[2] +
216 (size_t) i3 * (
size_t) t->nb[3];
217 const float v = *(
const float *) (base + off);
218 fwrite(&v,
sizeof(v), 1, f);
229 const struct ggml_tensor *t)
231 const char *dir = getenv(
"CK_PARITY_DIR");
233 !dir || !dir[0] || !name || !name[0] || !t ||
239 snprintf(path,
sizeof(path),
"%s/%s", dir,
"meta.jsonl");
240 FILE *f = fopen(path,
"a");
247 "{\"name\":\"%s\",\"layer_id\":%d,\"type\":%d,"
248 "\"ne\":[%lld,%lld,%lld,%lld],"
249 "\"nb\":[%zu,%zu,%zu,%zu]}\n",
253 (
long long) t->ne[0], (
long long) t->ne[1], (
long long) t->ne[2], (
long long) t->ne[3],
254 (
size_t) t->nb[0], (
size_t) t->nb[1], (
size_t) t->nb[2], (
size_t) t->nb[3]);
260 static int tried = 0;
271 static int tried = 0;
282 static int tried = 0;
293 static int tried = 0;
304 static int tried = 0;
315 static int tried = 0;
326 static int tried = 0;
337 static int tried = 0;
348 static int tried = 0;
359 static int tried = 0;
370 static int tried = 0;
381 static int tried = 0;
392 static int tried = 0;
403 static int tried = 0;
414 static int tried = 0;
425 static int tried = 0;
436 static int tried = 0;
447 static int tried = 0;
458 static int tried = 0;
469 static int tried = 0;
480 static int tried = 0;
491 static int tried = 0;
502 static int tried = 0;
513 static int tried = 0;
524 static int tried = 0;
535 static int tried = 0;
550 int aligned_head_dim,
553 const char *disable_env = getenv(
"CK_STRICT_DISABLE_REGULAR_ATTN_ORACLE");
554 if (disable_env && disable_env[0] && strcmp(disable_env,
"0") != 0) {
573 if (!ggml_cpu_init_fn || !ggml_init_fn || !ggml_free_fn ||
574 !ggml_new_tensor_1d_fn || !ggml_new_tensor_2d_fn ||
575 !ggml_view_3d_fn || !ggml_permute_fn || !ggml_cont_fn || !ggml_cont_2d_fn ||
576 !ggml_mul_mat_fn || !ggml_soft_max_ext_fn || !ggml_new_graph_fn ||
577 !ggml_build_forward_expand_fn || !ggml_graph_compute_with_ctx_fn ||
578 !ggml_set_input_fn) {
584 const size_t row_bytes = (size_t) aligned_head_dim *
sizeof(
float);
585 const size_t tensor_bytes = (size_t) num_tokens * row_bytes;
586 const size_t kq_bytes = (size_t) num_tokens * (
size_t) num_tokens *
sizeof(float);
587 const size_t mem_size = (size_t) 128 * 1024 * 1024 + tensor_bytes * 3 + kq_bytes * 2;
594 struct ggml_context *ctx = ggml_init_fn(params);
600 struct ggml_tensor *q_base =
601 ggml_new_tensor_1d_fn(ctx,
GGML_TYPE_F32, (int64_t) num_tokens * (int64_t) aligned_head_dim);
602 struct ggml_tensor *k_base =
603 ggml_new_tensor_1d_fn(ctx,
GGML_TYPE_F32, (int64_t) num_tokens * (int64_t) aligned_head_dim);
604 struct ggml_tensor *v_base =
605 ggml_new_tensor_1d_fn(ctx,
GGML_TYPE_F32, (int64_t) num_tokens * (int64_t) aligned_head_dim);
606 if (!q_base || !k_base || !v_base) {
611 q_base->data = (
void *) q_head;
612 k_base->data = (
void *) k_head;
613 v_base->data = (
void *) v_head;
614 ggml_set_input_fn(q_base);
615 ggml_set_input_fn(k_base);
616 ggml_set_input_fn(v_base);
618 struct ggml_tensor *q_cur = ggml_view_3d_fn(ctx, q_base, head_dim, 1, num_tokens, row_bytes, row_bytes, 0);
619 struct ggml_tensor *k_cur = ggml_view_3d_fn(ctx, k_base, head_dim, 1, num_tokens, row_bytes, row_bytes, 0);
620 struct ggml_tensor *v_cur = ggml_view_3d_fn(ctx, v_base, head_dim, 1, num_tokens, row_bytes, row_bytes, 0);
621 if (!q_cur || !k_cur || !v_cur) {
626 struct ggml_tensor *q = ggml_permute_fn(ctx, q_cur, 0, 2, 1, 3);
627 struct ggml_tensor *k = ggml_permute_fn(ctx, k_cur, 0, 2, 1, 3);
628 struct ggml_tensor *v = ggml_permute_fn(ctx, v_cur, 1, 2, 0, 3);
629 if (!q || !k || !v) {
634 v = ggml_cont_fn(ctx, v);
635 struct ggml_tensor *kq_scores = ggml_mul_mat_fn(ctx, k, q);
636 struct ggml_tensor *kq_scores_dump =
638 ? ggml_cont_fn(ctx, kq_scores)
640 struct ggml_tensor *kq = kq_scores ?
641 ggml_soft_max_ext_fn(ctx, kq_scores, NULL, scale, 0.0f) : NULL;
642 struct ggml_tensor *kqv = kq ? ggml_mul_mat_fn(ctx, v, kq) : NULL;
643 struct ggml_tensor *cur = kqv ? ggml_permute_fn(ctx, kqv, 0, 2, 1, 3) : NULL;
645 cur = ggml_cont_2d_fn(ctx, cur, cur->ne[0] * cur->ne[1], cur->ne[2] * cur->ne[3]);
652 struct ggml_cgraph *gf = ggml_new_graph_fn(ctx);
657 if (kq_scores_dump) {
658 ggml_build_forward_expand_fn(gf, kq_scores_dump);
660 ggml_build_forward_expand_fn(gf, cur);
667 const float *src = (
const float *) cur->data;
668 for (
int t = 0; t < num_tokens; ++t) {
669 memcpy(out_head + (
size_t) t * (
size_t) aligned_head_dim,
670 src + (
size_t) t * (
size_t) head_dim,
671 (
size_t) head_dim *
sizeof(
float));
672 for (
int d = head_dim; d < aligned_head_dim; ++d) {
673 out_head[(size_t) t * (
size_t) aligned_head_dim + (size_t) d] = 0.0f;
691 int aligned_head_dim,
692 int kv_stride_tokens,
695 const char *disable_env = getenv(
"CK_STRICT_DISABLE_MULTIHEAD_ATTN_ORACLE");
696 if (disable_env && disable_env[0] && strcmp(disable_env,
"0") != 0) {
699 if (!q || !k || !v || !output) {
702 if (num_heads <= 0 || num_tokens <= 0 || head_dim <= 0) {
705 if (num_heads != num_kv_heads) {
708 if (aligned_head_dim < head_dim) {
711 if (kv_stride_tokens != num_tokens) {
732 if (!ggml_cpu_init_fn || !ggml_init_fn || !ggml_free_fn ||
733 !ggml_new_tensor_1d_fn || !ggml_new_tensor_2d_fn ||
734 !ggml_view_3d_fn || !ggml_permute_fn || !ggml_cont_fn || !ggml_cont_2d_fn ||
735 !ggml_mul_mat_fn || !ggml_soft_max_ext_fn || !ggml_new_graph_fn ||
736 !ggml_build_forward_expand_fn || !ggml_graph_compute_with_ctx_fn ||
737 !ggml_set_input_fn) {
743 const size_t row_bytes = (size_t) aligned_head_dim *
sizeof(
float);
744 const size_t head_bytes = (size_t) num_tokens * row_bytes;
745 const size_t tensor_bytes = (size_t) num_heads * head_bytes;
746 const size_t packed_row_bytes = (size_t) num_heads * (
size_t) head_dim *
sizeof(float);
747 const size_t fused_qkv_row_bytes = (size_t) 3 * packed_row_bytes;
748 const size_t score_bytes =
749 (size_t) num_heads * (
size_t) num_tokens * (size_t) num_tokens *
sizeof(
float);
750 const size_t mem_size =
751 (size_t) 1024 * 1024 * 1024 + tensor_bytes * 4 + score_bytes * 2;
758 struct ggml_context *ctx = ggml_init_fn(params);
764 struct ggml_tensor *qkv_base = ggml_new_tensor_2d_fn(
765 ctx,
GGML_TYPE_F32, 3 * (int64_t) num_heads * (int64_t) head_dim, num_tokens);
770 ggml_set_input_fn(qkv_base);
772 const size_t embd_elems = (size_t) num_heads * (
size_t) head_dim;
773 const size_t qkv_pack_elems = (size_t) num_tokens * 3 * embd_elems;
774 float *qkv_pack = (
float *) malloc(qkv_pack_elems *
sizeof(
float));
779 for (
int t = 0; t < num_tokens; ++t) {
780 float *q_tok = qkv_pack + (size_t) t * 3 * embd_elems;
781 float *k_tok = q_tok + embd_elems;
782 float *v_tok = k_tok + embd_elems;
783 for (
int h = 0; h < num_heads; ++h) {
784 memcpy(q_tok + (
size_t) h * (
size_t) head_dim,
786 (
size_t) head_dim *
sizeof(
float));
787 memcpy(k_tok + (
size_t) h * (
size_t) head_dim,
789 (
size_t) head_dim *
sizeof(
float));
790 memcpy(v_tok + (
size_t) h * (
size_t) head_dim,
792 (
size_t) head_dim *
sizeof(
float));
795 qkv_base->data = qkv_pack;
797 struct ggml_tensor *q_cur = ggml_view_3d_fn(ctx,
802 (
size_t) head_dim *
sizeof(
float),
805 struct ggml_tensor *k_cur = ggml_view_3d_fn(ctx,
810 (
size_t) head_dim *
sizeof(
float),
813 struct ggml_tensor *v_cur = ggml_view_3d_fn(ctx,
818 (
size_t) head_dim *
sizeof(
float),
820 2 * packed_row_bytes);
821 if (!q_cur || !k_cur || !v_cur) {
827 struct ggml_cgraph *gf = ggml_new_graph_fn(ctx);
834 ggml_build_forward_expand_fn(gf, q_cur);
835 ggml_build_forward_expand_fn(gf, k_cur);
836 ggml_build_forward_expand_fn(gf, v_cur);
838 struct ggml_tensor *q_perm = ggml_permute_fn(ctx, q_cur, 0, 2, 1, 3);
839 struct ggml_tensor *k_perm = ggml_permute_fn(ctx, k_cur, 0, 2, 1, 3);
840 struct ggml_tensor *v_perm = ggml_permute_fn(ctx, v_cur, 1, 2, 0, 3);
841 if (!q_perm || !k_perm || !v_perm) {
847 v_perm = ggml_cont_fn(ctx, v_perm);
848 struct ggml_tensor *kq_scores = v_perm ? ggml_mul_mat_fn(ctx, k_perm, q_perm) : NULL;
849 struct ggml_tensor *kq_scores_dump =
851 ? ggml_cont_fn(ctx, kq_scores)
853 struct ggml_tensor *kq_softmax = kq_scores ?
854 ggml_soft_max_ext_fn(ctx, kq_scores, NULL, scale, 0.0f) : NULL;
855 struct ggml_tensor *kqv = kq_softmax ? ggml_mul_mat_fn(ctx, v_perm, kq_softmax) : NULL;
856 struct ggml_tensor *cur = kqv ? ggml_permute_fn(ctx, kqv, 0, 2, 1, 3) : NULL;
858 cur = ggml_cont_2d_fn(ctx, cur, cur->ne[0] * cur->ne[1], cur->ne[2] * cur->ne[3]);
866 if (kq_scores_dump) {
867 ggml_build_forward_expand_fn(gf, kq_scores_dump);
869 ggml_build_forward_expand_fn(gf, cur);
891 const size_t cur_elems = (size_t) cur->ne[0] * (
size_t) cur->ne[1] * (size_t) cur->ne[2] * (
size_t) cur->ne[3];
892 float *cur_host = (
float *) malloc(cur_elems *
sizeof(
float));
898 memcpy(cur_host, cur->data, cur_elems *
sizeof(
float));
899 const float *src = cur_host;
901 const size_t token_width = (size_t) num_heads * (
size_t) head_dim;
902 for (
int t = 0; t < num_tokens; ++t) {
903 const float *token_src = src + (size_t) t * token_width;
904 for (
int h = 0; h < num_heads; ++h) {
906 memcpy(dst, token_src + (
size_t) h * (
size_t) head_dim, (
size_t) head_dim *
sizeof(
float));
907 for (
int d = head_dim; d < aligned_head_dim; ++d) {
static ck_ggml_backend_free_fn ck_resolve_ggml_backend_free(void)
static ck_ggml_backend_tensor_set_fn ck_resolve_ggml_backend_tensor_set(void)
static ck_ggml_cont_fn ck_resolve_ggml_cont(void)
static int ck_attention_oracle_should_dump_layer(int layer_id)
static size_t ck_attention_oracle_qkv_index(int h, int t, int d, int num_tokens, int aligned_head_dim)
static ck_ggml_backend_sched_alloc_graph_fn ck_resolve_ggml_backend_sched_alloc_graph(void)
void(* ck_ggml_backend_free_fn)(ggml_backend_t)
void(* ck_ggml_backend_tensor_get_fn)(const struct ggml_tensor *, void *, size_t, size_t)
static ck_ggml_mul_mat_graph_fn ck_resolve_ggml_mul_mat_graph(void)
void(* ck_ggml_cpu_init_fn)(void)
void(* ck_ggml_backend_sched_reset_fn)(ggml_backend_sched_t)
struct ggml_tensor *(* ck_ggml_view_3d_fn)(struct ggml_context *, struct ggml_tensor *, int64_t, int64_t, int64_t, size_t, size_t, size_t)
struct ggml_tensor *(* ck_ggml_new_tensor_2d_fn)(struct ggml_context *, enum ggml_type, int64_t, int64_t)
struct ggml_cgraph *(* ck_ggml_new_graph_fn)(struct ggml_context *)
void(* ck_ggml_set_input_fn)(struct ggml_tensor *)
ggml_backend_buffer_type_t(* ck_ggml_backend_get_default_buffer_type_fn)(ggml_backend_t)
static ck_ggml_view_3d_fn ck_resolve_ggml_view_3d(void)
ggml_backend_t(* ck_ggml_backend_init_by_type_fn)(enum ggml_backend_dev_type, const char *)
void(* ck_ggml_backend_sched_free_fn)(ggml_backend_sched_t)
static ck_ggml_new_tensor_1d_fn ck_resolve_ggml_new_tensor_1d(void)
static ck_ggml_backend_get_default_buffer_type_fn ck_resolve_ggml_backend_get_default_buffer_type(void)
enum ggml_status(* ck_ggml_graph_compute_with_ctx_fn)(struct ggml_context *, struct ggml_cgraph *, int)
static ck_ggml_new_tensor_2d_fn ck_resolve_ggml_new_tensor_2d(void)
static float ck_attention_oracle_tensor_f32_at(const struct ggml_tensor *t, size_t i0, size_t i1, size_t i2, size_t i3)
struct ggml_tensor *(* ck_ggml_permute_fn)(struct ggml_context *, struct ggml_tensor *, int, int, int, int)
static int ck_attention_oracle_dump_layer_id(void)
static void ck_attention_oracle_dump_tensor(const char *name, int layer_id, const struct ggml_tensor *t)
enum ggml_status(* ck_ggml_backend_sched_graph_compute_fn)(ggml_backend_sched_t, struct ggml_cgraph *)
static ck_ggml_build_forward_expand_fn ck_resolve_ggml_build_forward_expand(void)
struct ggml_tensor *(* ck_ggml_cont_2d_fn)(struct ggml_context *, struct ggml_tensor *, int64_t, int64_t)
bool(* ck_ggml_backend_sched_alloc_graph_fn)(ggml_backend_sched_t, struct ggml_cgraph *)
static int ck_attention_oracle_dump_enabled(void)
struct ggml_tensor *(* ck_ggml_cont_fn)(struct ggml_context *, struct ggml_tensor *)
void(* ck_ggml_backend_cpu_set_n_threads_fn)(ggml_backend_t, int)
static int ck_attention_oracle_exact_dump_layer(int *layer_id_out)
void(* ck_ggml_free_fn)(struct ggml_context *)
static int ck_attention_oracle_meta_dump_enabled(void)
static ck_ggml_backend_init_by_type_fn ck_resolve_ggml_backend_init_by_type(void)
void(* ck_ggml_build_forward_expand_fn)(struct ggml_cgraph *, struct ggml_tensor *)
struct ggml_tensor *(* ck_ggml_mul_mat_graph_fn)(struct ggml_context *, struct ggml_tensor *, struct ggml_tensor *)
static ck_ggml_graph_compute_with_ctx_fn ck_resolve_ggml_graph_compute_with_ctx(void)
static ck_ggml_backend_sched_new_fn ck_resolve_ggml_backend_sched_new(void)
static ck_ggml_cont_2d_fn ck_resolve_ggml_cont_2d(void)
static void ck_attention_oracle_dump_meta(const char *name, int layer_id, const struct ggml_tensor *t)
static ck_ggml_set_input_fn ck_resolve_ggml_set_input(void)
static ck_ggml_backend_sched_free_fn ck_resolve_ggml_backend_sched_free(void)
static ck_ggml_backend_sched_reset_fn ck_resolve_ggml_backend_sched_reset(void)
int ck_attention_head_full_ggml_graph_oracle_regular(const float *q_head, const float *k_head, const float *v_head, float *out_head, int num_tokens, int head_dim, int aligned_head_dim, float scale)
static ck_ggml_new_graph_fn ck_resolve_ggml_new_graph(void)
static ck_ggml_free_fn ck_resolve_ggml_free(void)
static const uint32_t ck_attention_oracle_version
struct ggml_tensor *(* ck_ggml_new_tensor_1d_fn)(struct ggml_context *, enum ggml_type, int64_t)
struct ggml_context *(* ck_ggml_init_fn)(struct ggml_init_params)
int ck_attention_full_ggml_graph_oracle_multihead(const float *q, const float *k, const float *v, float *output, int num_heads, int num_kv_heads, int num_tokens, int head_dim, int aligned_head_dim, int kv_stride_tokens, float scale)
static ck_ggml_backend_sched_graph_compute_fn ck_resolve_ggml_backend_sched_graph_compute(void)
static ck_ggml_backend_tensor_get_fn ck_resolve_ggml_backend_tensor_get(void)
static int ck_attention_oracle_layer_seq
static ck_ggml_backend_cpu_set_n_threads_fn ck_resolve_ggml_backend_cpu_set_n_threads(void)
struct ggml_tensor *(* ck_ggml_soft_max_ext_fn)(struct ggml_context *, struct ggml_tensor *, struct ggml_tensor *, float, float)
static ck_ggml_init_fn ck_resolve_ggml_init(void)
static ck_ggml_cpu_init_fn ck_resolve_ggml_cpu_init(void)
static const char ck_attention_oracle_magic[8]
static ck_ggml_soft_max_ext_fn ck_resolve_ggml_soft_max_ext(void)
ggml_backend_sched_t(* ck_ggml_backend_sched_new_fn)(ggml_backend_t *, ggml_backend_buffer_type_t *, int, size_t, bool, bool)
void(* ck_ggml_backend_tensor_set_fn)(struct ggml_tensor *, const void *, size_t, size_t)
static ck_ggml_permute_fn ck_resolve_ggml_permute(void)
static const char * op_name(CKOpType op)
void ck_strict_store_next_gemm_a(const float *data, size_t elems)
__attribute__((visibility("default"))) CKTokenizer *ck_tokenizer_create(CKTokenizerType type)