← Back to C-Kernel-Engine Docs Doxygen Source Documentation
 
Loading...
Searching...
No Matches
rope_kernels_bf16.c
Go to the documentation of this file.
1/**
2 * @file rope_kernels_bf16.c
3 * @brief RoPE (Rotary Position Embedding) kernels for BF16
4 *
5 * CK-ENGINE KERNEL RULES:
6 * =======================
7 * 1. NO malloc/free - memory via bump allocator, pointers passed in
8 * 2. NO OpenMP - parallelization at orchestrator/codegen layer
9 * 3. API must define: inputs, outputs, workspace, and memory layouts
10 * 4. Pure computation - deterministic, no side effects
11 *
12 * After changes: make test && make llamacpp-parity-full
13 */
14
15#include <stdint.h>
16
17#include "bf16_utils.h"
18#include "ckernel_engine.h"
19
20/* Suppress false positive warnings about uninitialized variables */
21#pragma GCC diagnostic push
22#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
23
24/*
25 * BF16 RoPE forward with caller-provided scratch buffer.
26 * scratch: [num_heads * num_tokens * aligned_head_dim] floats
27 */
28void rope_forward_bf16(uint16_t *x,
29 const float *cos_cache,
30 const float *sin_cache,
31 int num_heads,
32 int num_tokens,
33 int head_dim,
34 int aligned_head_dim,
35 int pos_offset,
36 float *scratch)
37{
38 rope_forward_bf16_with_rotary_dim(x, cos_cache, sin_cache, num_heads, num_tokens,
39 head_dim, aligned_head_dim, pos_offset, head_dim, scratch);
40}
41
43 const float *cos_cache,
44 const float *sin_cache,
45 int num_heads,
46 int num_tokens,
47 int head_dim,
48 int aligned_head_dim,
49 int pos_offset,
50 int rotary_dim,
51 float *scratch)
52{
53 if (!scratch) return;
54
55 size_t total = (size_t)num_heads * (size_t)num_tokens * (size_t)aligned_head_dim;
56
57 bf16_tensor_to_float(x, scratch, total);
58 rope_forward_with_rotary_dim(scratch, cos_cache, sin_cache,
59 num_heads, num_tokens, head_dim, aligned_head_dim, pos_offset, rotary_dim);
60 float_tensor_to_bf16(scratch, x, total);
61}
62
63/*
64 * BF16 RoPE backward with caller-provided scratch buffers.
65 * scratch_d_out, scratch_d_x: each [num_heads * num_tokens * aligned_head_dim] floats
66 */
67void rope_backward_bf16(const uint16_t *d_out,
68 uint16_t *d_x,
69 const float *cos_cache,
70 const float *sin_cache,
71 int num_heads,
72 int num_tokens,
73 int head_dim,
74 int aligned_head_dim,
75 int pos_offset,
76 float *scratch_d_out,
77 float *scratch_d_x)
78{
79 if (!scratch_d_out || !scratch_d_x) return;
80
81 size_t total = (size_t)num_heads * (size_t)num_tokens * (size_t)aligned_head_dim;
82
83 bf16_tensor_to_float(d_out, scratch_d_out, total);
84 rope_backward(scratch_d_out, scratch_d_x, cos_cache, sin_cache,
85 num_heads, num_tokens, head_dim, aligned_head_dim, pos_offset);
86 float_tensor_to_bf16(scratch_d_x, d_x, total);
87}
88
89/*
90 * BF16 RoPE forward for Q and K with caller-provided scratch buffers.
91 * scratch_q: [num_heads * num_tokens * aligned_head_dim] floats
92 * scratch_k: [num_kv_heads * num_tokens * aligned_head_dim] floats
93 */
94void rope_forward_qk_bf16(uint16_t *q,
95 uint16_t *k,
96 const float *cos_cache,
97 const float *sin_cache,
98 int num_heads,
99 int num_kv_heads,
100 int num_tokens,
101 int head_dim,
102 int aligned_head_dim,
103 int pos_offset,
104 float *scratch_q,
105 float *scratch_k)
106{
107 rope_forward_qk_bf16_with_rotary_dim(q, k, cos_cache, sin_cache, num_heads, num_kv_heads,
108 num_tokens, head_dim, aligned_head_dim, pos_offset,
109 head_dim, scratch_q, scratch_k);
110}
111
113 uint16_t *k,
114 const float *cos_cache,
115 const float *sin_cache,
116 int num_heads,
117 int num_kv_heads,
118 int num_tokens,
119 int head_dim,
120 int aligned_head_dim,
121 int pos_offset,
122 int rotary_dim,
123 float *scratch_q,
124 float *scratch_k)
125{
126 if (!q || !k) return;
127
128 rope_forward_bf16_with_rotary_dim(q, cos_cache, sin_cache,
129 num_heads, num_tokens, head_dim, aligned_head_dim, pos_offset,
130 rotary_dim, scratch_q);
131 rope_forward_bf16_with_rotary_dim(k, cos_cache, sin_cache,
132 num_kv_heads, num_tokens, head_dim, aligned_head_dim, pos_offset,
133 rotary_dim, scratch_k);
134}
135
136/*
137 * BF16 RoPE backward for Q and K with caller-provided scratch buffers.
138 */
139void rope_backward_qk_bf16(const uint16_t *d_q_out,
140 const uint16_t *d_k_out,
141 uint16_t *d_q,
142 uint16_t *d_k,
143 const float *cos_cache,
144 const float *sin_cache,
145 int num_heads,
146 int num_kv_heads,
147 int num_tokens,
148 int head_dim,
149 int aligned_head_dim,
150 int pos_offset,
151 float *scratch_dq_out,
152 float *scratch_dq,
153 float *scratch_dk_out,
154 float *scratch_dk)
155{
156 if (!d_q_out || !d_k_out || !d_q || !d_k) return;
157
158 rope_backward_bf16(d_q_out, d_q, cos_cache, sin_cache,
159 num_heads, num_tokens, head_dim, aligned_head_dim, pos_offset,
160 scratch_dq_out, scratch_dq);
161 rope_backward_bf16(d_k_out, d_k, cos_cache, sin_cache,
162 num_kv_heads, num_tokens, head_dim, aligned_head_dim, pos_offset,
163 scratch_dk_out, scratch_dk);
164}
165
166#pragma GCC diagnostic pop
static void float_tensor_to_bf16(const float *src, uint16_t *dst, size_t count)
Definition bf16_utils.h:271
static void bf16_tensor_to_float(const uint16_t *src, float *dst, size_t count)
Definition bf16_utils.h:250
void rope_forward_with_rotary_dim(float *x, const float *cos_cache, const float *sin_cache, int num_heads, int num_tokens, int head_dim, int aligned_head_dim, int pos_offset, int rotary_dim)
void rope_backward(const float *d_out, float *d_x, const float *cos_cache, const float *sin_cache, int num_heads, int num_tokens, int head_dim, int aligned_head_dim, int pos_offset)
void rope_backward_bf16(const uint16_t *d_out, uint16_t *d_x, const float *cos_cache, const float *sin_cache, int num_heads, int num_tokens, int head_dim, int aligned_head_dim, int pos_offset, float *scratch_d_out, float *scratch_d_x)
void rope_backward_qk_bf16(const uint16_t *d_q_out, const uint16_t *d_k_out, uint16_t *d_q, uint16_t *d_k, const float *cos_cache, const float *sin_cache, int num_heads, int num_kv_heads, int num_tokens, int head_dim, int aligned_head_dim, int pos_offset, float *scratch_dq_out, float *scratch_dq, float *scratch_dk_out, float *scratch_dk)
void rope_forward_bf16(uint16_t *x, const float *cos_cache, const float *sin_cache, int num_heads, int num_tokens, int head_dim, int aligned_head_dim, int pos_offset, float *scratch)
void rope_forward_bf16_with_rotary_dim(uint16_t *x, const float *cos_cache, const float *sin_cache, int num_heads, int num_tokens, int head_dim, int aligned_head_dim, int pos_offset, int rotary_dim, float *scratch)
void rope_forward_qk_bf16(uint16_t *q, uint16_t *k, const float *cos_cache, const float *sin_cache, int num_heads, int num_kv_heads, int num_tokens, int head_dim, int aligned_head_dim, int pos_offset, float *scratch_q, float *scratch_k)
void rope_forward_qk_bf16_with_rotary_dim(uint16_t *q, uint16_t *k, const float *cos_cache, const float *sin_cache, int num_heads, int num_kv_heads, int num_tokens, int head_dim, int aligned_head_dim, int pos_offset, int rotary_dim, float *scratch_q, float *scratch_k)