← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
causal_depthwise_residual_checked.c File Reference
#include "checked_float_buffer.h"
#include <float.h>
#include <string.h>

Go to the source code of this file.

Typedefs

typedef double(* EvaluateStatic) (const float *, const float *, const float *, size_t, size_t, size_t, size_t, double)

Functions

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)
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)
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)
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 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)
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)
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)
static void store_history (float *history, const float *input, size_t tokens, size_t taps, size_t rows, size_t state)

Typedef Documentation

◆ EvaluateStatic

typedef double(* EvaluateStatic) (const float *, const float *, const float *, size_t, size_t, size_t, size_t, double)

Definition at line 42 of file causal_depthwise_residual_checked.c.

Function Documentation

◆ causal_depthwise_residual()

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

Definition at line 56 of file causal_depthwise_residual_checked.c.

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}
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)
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)
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)

References ck_checked_finite(), ck_checked_overlap(), ck_checked_region(), ck_checked_span(), evaluate(), evaluate_fp32(), and store_history().

Referenced by ck_causal_depthwise_residual_checked_f32(), and ck_causal_depthwise_residual_checked_ordered_f32().

◆ causal_dynamic_depthwise_residual_checked_f32()

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 )

Definition at line 144 of file causal_depthwise_residual_checked.c.

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 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)

References ck_checked_finite(), ck_checked_overlap(), ck_checked_region(), ck_checked_span(), evaluate_dynamic(), and store_history().

◆ ck_causal_depthwise_residual_checked_f32()

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 )

Definition at line 96 of file causal_depthwise_residual_checked.c.

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}
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)

References causal_depthwise_residual().

◆ ck_causal_depthwise_residual_checked_ordered_f32()

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 )

Definition at line 106 of file causal_depthwise_residual_checked.c.

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}

References causal_depthwise_residual().

◆ evaluate()

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 )
static

Definition at line 7 of file causal_depthwise_residual_checked.c.

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}
static int product(size_t a, size_t b, size_t *out)
const char * token
Definition tokenizer.h:308

References product(), and token.

Referenced by causal_depthwise_residual().

◆ evaluate_dynamic()

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 )
static

Definition at line 117 of file causal_depthwise_residual_checked.c.

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}

References product(), and token.

Referenced by causal_dynamic_depthwise_residual_checked_f32().

◆ evaluate_fp32()

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 )
static

Definition at line 25 of file causal_depthwise_residual_checked.c.

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}

References product(), and token.

Referenced by causal_depthwise_residual().

◆ store_history()

void store_history ( float * history,
const float * input,
size_t tokens,
size_t taps,
size_t rows,
size_t state )
static

Definition at line 45 of file causal_depthwise_residual_checked.c.

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}

Referenced by causal_depthwise_residual(), and causal_dynamic_depthwise_residual_checked_f32().