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,
11 const float *filter = weight + channel * taps;
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];
19 const volatile double product = (double)value * (
double)filter[tap];
22 return (sum + (
double)input[
token * channels + channel]) / divisor;
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,
29 const float *filter = weight + channel * taps;
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];
39 return (sum + input[
token * channels + channel]) * (float)multiplier;
43 size_t, size_t, size_t, size_t, double);
45static void store_history(
float *history,
const float *input,
size_t tokens,
size_t taps,
46 size_t rows,
size_t state)
48 if (tokens >= taps - 1) {
49 memcpy(history, input + rows - state, state *
sizeof(
float));
51 memmove(history, history + rows, (state - rows) *
sizeof(
float));
52 memcpy(history + state - rows, input, rows *
sizeof(
float));
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,
62 if (taps < 2 || taps > 256 || history_rows != taps - 1 || tokens > SIZE_MAX - taps)
return -2;
63 size_t rows, weights, state;
67 rows > input_elements || weights > weight_elements ||
68 state > history_elements || rows > output_elements)
return -2;
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;
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);
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)
102 history_elements, output, output_elements,
103 tokens, channels, taps, history_rows, 0);
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)
112 history_elements, output, output_elements,
113 tokens, channels, taps, history_rows, 1);
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)
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];
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];
141 return (static_sum + dynamic_sum + (
double)input[
token * channels + channel]) / divisor;
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)
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;
154 size_t rows, weights, state, dynamic_span;
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;
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) {
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)
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);
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)