← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
attention_rows_checked.c File Reference

Go to the source code of this file.

Functions

int scaled_head_l2_clip_checked_f32 (const float *input, size_t input_elements, const float *gamma, size_t gamma_elements, float *output, size_t output_elements, size_t channels, size_t rows, size_t heads, size_t dim, float cap, float epsilon)
int sigmoid_gate_rows_checked_f32 (const float *input, size_t input_elements, const float *gate, size_t gate_elements, float *output, size_t output_elements, size_t elements)

Function Documentation

◆ scaled_head_l2_clip_checked_f32()

int scaled_head_l2_clip_checked_f32 ( const float * input,
size_t input_elements,
const float * gamma,
size_t gamma_elements,
float * output,
size_t output_elements,
size_t channels,
size_t rows,
size_t heads,
size_t dim,
float cap,
float epsilon )

Definition at line 3 of file attention_rows_checked.c.

8{
9 if (!heads || !dim || heads > SIZE_MAX / dim ||
10 !isfinite(cap) || cap <= 0.0f || !isfinite(epsilon) || epsilon <= 0.0f) return -2;
11 const size_t width = heads * dim;
12 if (channels != width) return -2;
13 size_t active;
14 if (!ck_checked_span(rows, width, width, &active) ||
15 input_elements < active || output_elements < active || gamma_elements < width) return -2;
16 if (!ck_checked_region(input, active) || !ck_checked_region(output, active) ||
17 !ck_checked_region(gamma, width) ||
18 ck_checked_overlap(input, active, output, active) ||
19 ck_checked_overlap(gamma, width, output, active)) return -1;
20 if (!ck_checked_finite(gamma, width)) return -3;
21 for (size_t i = 0; i < active; ++i)
22 if (!isfinite(input[i] * gamma[i % width])) return -3;
23
24 for (size_t row = 0; row < rows; ++row) {
25 for (size_t h = 0; h < heads; ++h) {
26 const size_t col = h * dim;
27 const size_t start = row * width + col;
28 double squared_norm = 0.0;
29 for (size_t d = 0; d < dim; ++d) {
30 const float scaled = input[start + d] * gamma[col + d];
31 const volatile double squared = (double)scaled * scaled;
32 squared_norm += squared;
33 }
34 const double scale = fmin(1.0, cap / (sqrt(squared_norm) + epsilon));
35 for (size_t d = 0; d < dim; ++d) {
36 const float scaled = input[start + d] * gamma[col + d];
37 output[start + d] = (float)(scaled * scale);
38 }
39 }
40 }
41 return 0;
42}
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)
uint32_t start
Definition utf8.c:214

References ck_checked_finite(), ck_checked_overlap(), ck_checked_region(), ck_checked_span(), and start.

◆ sigmoid_gate_rows_checked_f32()

int sigmoid_gate_rows_checked_f32 ( const float * input,
size_t input_elements,
const float * gate,
size_t gate_elements,
float * output,
size_t output_elements,
size_t elements )

Definition at line 44 of file attention_rows_checked.c.

47{
48 if (!elements || input_elements < elements || gate_elements < elements ||
49 output_elements < elements) return -2;
50 if (!ck_checked_region(input, elements) || !ck_checked_region(gate, elements) ||
51 !ck_checked_region(output, elements) ||
52 ck_checked_overlap(input, elements, output, elements) ||
53 ck_checked_overlap(gate, elements, output, elements)) return -1;
54 if (!ck_checked_finite(input, elements) || !ck_checked_finite(gate, elements)) return -3;
55 for (size_t i = 0; i < elements; ++i) {
56 const float ex = expf(-fabsf(gate[i]));
57 const float sigmoid = (gate[i] < 0.0f ? ex : 1.0f) / (1.0f + ex);
58 output[i] = input[i] * sigmoid;
59 }
60 return 0;
61}

References ck_checked_finite(), ck_checked_overlap(), and ck_checked_region().