← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
causal_depthwise_residual_checked.c
Go to the documentation of this file.
2#include <float.h>
3#include <string.h>
4
5/* Ordered FP64 arithmetic on FP32 input storage. Weight taps and history run
6 * oldest first. The history contains pre-convolution rows, never outputs. */
7static double evaluate(const float *input, const float *history, const float *weight,
8 size_t token, size_t channel, size_t channels, size_t taps,
9 double divisor)
10{
11 const float *filter = weight + channel * taps;
12 double sum = 0.0;
13 for (size_t tap = 0; tap < taps; ++tap) {
14 const size_t row = token + tap;
15 const float value = row < taps - 1
16 ? history[row * channels + channel]
17 : input[(row - (taps - 1)) * channels + channel];
18 /* Keep the same multiply and add sequence in every build. */
19 const volatile double product = (double)value * (double)filter[tap];
20 sum += product;
21 }
22 return (sum + (double)input[token * channels + channel]) / divisor;
23}
24
25static double evaluate_fp32(const float *input, const float *history, const float *weight,
26 size_t token, size_t channel, size_t channels, size_t taps,
27 double multiplier)
28{
29 const float *filter = weight + channel * taps;
30 float sum = 0.0f;
31 for (size_t tap = 0; tap < taps; ++tap) {
32 const size_t row = token + tap;
33 const float value = row < taps - 1
34 ? history[row * channels + channel]
35 : input[(row - (taps - 1)) * channels + channel];
36 const volatile float product = value * filter[tap];
37 sum += product;
38 }
39 return (sum + input[token * channels + channel]) * (float)multiplier;
40}
41
42typedef double (*EvaluateStatic)(const float *, const float *, const float *,
43 size_t, size_t, size_t, size_t, double);
44
45static void store_history(float *history, const float *input, size_t tokens, size_t taps,
46 size_t rows, size_t state)
47{
48 if (tokens >= taps - 1) {
49 memcpy(history, input + rows - state, state * sizeof(float));
50 } else {
51 memmove(history, history + rows, (state - rows) * sizeof(float));
52 memcpy(history + state - rows, input, rows * sizeof(float));
53 }
54}
55
57 const float *input, size_t input_elements, const float *weight, size_t weight_elements,
58 float *history, size_t history_elements, float *output, size_t output_elements,
59 size_t tokens, size_t channels, size_t taps, size_t history_rows,
60 int ordered_fp32)
61{
62 if (taps < 2 || taps > 256 || history_rows != taps - 1 || tokens > SIZE_MAX - taps) return -2;
63 size_t rows, weights, state;
64 if (!ck_checked_span(tokens, channels, channels, &rows) ||
65 !ck_checked_span(channels, taps, taps, &weights) ||
66 !ck_checked_span(taps - 1, channels, channels, &state) ||
67 rows > input_elements || weights > weight_elements ||
68 state > history_elements || rows > output_elements) return -2;
69
70 if (!ck_checked_region(input, rows) || !ck_checked_region(weight, weights) ||
71 !ck_checked_region(history, state) || !ck_checked_region(output, rows) ||
72 ck_checked_overlap(history, state, input, rows) ||
73 ck_checked_overlap(history, state, weight, weights) ||
74 ck_checked_overlap(history, state, output, rows) ||
75 ck_checked_overlap(output, rows, input, rows) ||
76 ck_checked_overlap(output, rows, weight, weights)) return -1;
77 if (!ck_checked_finite(input, rows) || !ck_checked_finite(weight, weights) ||
78 !ck_checked_finite(history, state)) return -3;
79
80 const EvaluateStatic compute = ordered_fp32 ? evaluate_fp32 : evaluate;
81 const double scale = ordered_fp32 ? (double)(1.0f / sqrtf((float)taps + 1.0f))
82 : sqrt((double)taps + 1.0);
83 for (size_t t = 0; t < tokens; ++t)
84 for (size_t c = 0; c < channels; ++c) {
85 const double value = compute(input, history, weight, t, c, channels, taps, scale);
86 if (!isfinite(value) || fabs(value) > FLT_MAX) return -3;
87 }
88 for (size_t t = 0; t < tokens; ++t)
89 for (size_t c = 0; c < channels; ++c)
90 output[t * channels + c] = (float)compute(input, history, weight, t, c, channels, taps, scale);
91
92 store_history(history, input, tokens, taps, rows, state);
93 return 0;
94}
95
97 const float *input, size_t input_elements, const float *weight, size_t weight_elements,
98 float *history, size_t history_elements, float *output, size_t output_elements,
99 size_t tokens, size_t channels, size_t taps, size_t history_rows)
100{
101 return causal_depthwise_residual(input, input_elements, weight, weight_elements, history,
102 history_elements, output, output_elements,
103 tokens, channels, taps, history_rows, 0);
104}
105
107 const float *input, size_t input_elements, const float *weight, size_t weight_elements,
108 float *history, size_t history_elements, float *output, size_t output_elements,
109 size_t tokens, size_t channels, size_t taps, size_t history_rows)
110{
111 return causal_depthwise_residual(input, input_elements, weight, weight_elements, history,
112 history_elements, output, output_elements,
113 tokens, channels, taps, history_rows, 1);
114}
115
116/* GGUF static taps are oldest first; per-row dynamic taps start at lag zero. */
117static double evaluate_dynamic(const float *input, const float *history, const float *weight,
118 const float *dynamic, size_t token, size_t channel, size_t channels,
119 size_t heads, size_t taps, double divisor)
120{
121 const float *filter = weight + channel * taps;
122 const size_t head = channel / (channels / heads);
123 const float *delta = dynamic + (token * heads + head) * taps;
124 double static_sum = 0.0, dynamic_sum = 0.0;
125 for (size_t tap = 0; tap < taps; ++tap) {
126 const size_t row = token + tap;
127 const float value = row < taps - 1
128 ? history[row * channels + channel]
129 : input[(row - (taps - 1)) * channels + channel];
130 const volatile double product = (double)value * (double)filter[tap];
131 static_sum += product;
132 }
133 for (size_t lag = 0; lag < taps; ++lag) {
134 const size_t row = token + taps - 1 - lag;
135 const float value = row < taps - 1
136 ? history[row * channels + channel]
137 : input[(row - (taps - 1)) * channels + channel];
138 const volatile double product = (double)value * (double)delta[lag];
139 dynamic_sum += product;
140 }
141 return (static_sum + dynamic_sum + (double)input[token * channels + channel]) / divisor;
142}
143
145 const float *input, size_t input_elements, const float *weight, size_t weight_elements,
146 const float *dynamic, size_t dynamic_elements, float *history, size_t history_elements,
147 float *output, size_t output_elements, size_t tokens, size_t channels, size_t heads,
148 size_t taps, size_t history_rows, size_t dynamic_width)
149{
150 if (!heads || channels % heads || taps < 2 || taps > 256 ||
151 history_rows != taps - 1 || tokens > SIZE_MAX - taps || heads > SIZE_MAX / taps ||
152 dynamic_width != heads * taps) return -2;
153
154 size_t rows, weights, state, dynamic_span;
155 if (!ck_checked_span(tokens, channels, channels, &rows) ||
156 !ck_checked_span(channels, taps, taps, &weights) ||
157 !ck_checked_span(history_rows, channels, channels, &state) ||
158 !ck_checked_span(tokens, dynamic_width, dynamic_width, &dynamic_span) ||
159 input_elements < rows || output_elements < rows || weight_elements < weights ||
160 history_elements < state || dynamic_elements < dynamic_span) return -2;
161
162 if (!ck_checked_region(history, state) || !ck_checked_region(output, rows) ||
163 ck_checked_overlap(history, state, output, rows)) return -1;
164 const float *reads[] = {input, weight, dynamic};
165 const size_t sizes[] = {rows, weights, dynamic_span};
166 for (size_t i = 0; i < sizeof(reads) / sizeof(reads[0]); ++i) {
167 if (!ck_checked_region(reads[i], sizes[i]) ||
168 ck_checked_overlap(reads[i], sizes[i], history, state) ||
169 ck_checked_overlap(reads[i], sizes[i], output, rows)) return -1;
170 if (!ck_checked_finite(reads[i], sizes[i])) return -3;
171 }
172 if (!ck_checked_finite(history, state)) return -3;
173
174 const double divisor = sqrt(2.0 * (double)taps + 1.0);
175 for (size_t t = 0; t < tokens; ++t)
176 for (size_t c = 0; c < channels; ++c)
177 if (fabs(evaluate_dynamic(input, history, weight, dynamic, t, c, channels, heads, taps, divisor)) > FLT_MAX)
178 return -3;
179 for (size_t t = 0; t < tokens; ++t)
180 for (size_t c = 0; c < channels; ++c)
181 output[t * channels + c] =
182 (float)evaluate_dynamic(input, history, weight, dynamic, t, c, channels, heads, taps, divisor);
183
184 store_history(history, input, tokens, taps, rows, state);
185 return 0;
186}
static int product(size_t a, size_t b, size_t *out)
int ck_causal_depthwise_residual_checked_ordered_f32(const float *input, size_t input_elements, const float *weight, size_t weight_elements, float *history, size_t history_elements, float *output, size_t output_elements, size_t tokens, size_t channels, size_t taps, size_t history_rows)
static int causal_depthwise_residual(const float *input, size_t input_elements, const float *weight, size_t weight_elements, float *history, size_t history_elements, float *output, size_t output_elements, size_t tokens, size_t channels, size_t taps, size_t history_rows, int ordered_fp32)
static double evaluate(const float *input, const float *history, const float *weight, size_t token, size_t channel, size_t channels, size_t taps, double divisor)
int causal_dynamic_depthwise_residual_checked_f32(const float *input, size_t input_elements, const float *weight, size_t weight_elements, const float *dynamic, size_t dynamic_elements, float *history, size_t history_elements, float *output, size_t output_elements, size_t tokens, size_t channels, size_t heads, size_t taps, size_t history_rows, size_t dynamic_width)
static void store_history(float *history, const float *input, size_t tokens, size_t taps, size_t rows, size_t state)
static double evaluate_fp32(const float *input, const float *history, const float *weight, size_t token, size_t channel, size_t channels, size_t taps, double multiplier)
int ck_causal_depthwise_residual_checked_f32(const float *input, size_t input_elements, const float *weight, size_t weight_elements, float *history, size_t history_elements, float *output, size_t output_elements, size_t tokens, size_t channels, size_t taps, size_t history_rows)
static double evaluate_dynamic(const float *input, const float *history, const float *weight, const float *dynamic, size_t token, size_t channel, size_t channels, size_t heads, size_t taps, double divisor)
double(* EvaluateStatic)(const float *, const float *, const float *, size_t, size_t, size_t, size_t, double)
static int ck_checked_finite(const float *ptr, size_t n)
static int ck_checked_span(size_t rows, size_t width, size_t stride, size_t *span)
static int ck_checked_region(const float *ptr, size_t n)
static int ck_checked_overlap(const float *a, size_t na, const float *b, size_t nb)
const char * token
Definition tokenizer.h:308