4 const float *gamma,
size_t gamma_elements,
5 float *output,
size_t output_elements,
6 size_t channels,
size_t rows,
size_t heads,
size_t dim,
7 float cap,
float epsilon)
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;
15 input_elements < active || output_elements < active || gamma_elements < width)
return -2;
21 for (
size_t i = 0; i < active; ++i)
22 if (!isfinite(input[i] * gamma[i % width]))
return -3;
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;
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);
45 const float *gate,
size_t gate_elements,
46 float *output,
size_t output_elements,
size_t elements)
48 if (!elements || input_elements < elements || gate_elements < elements ||
49 output_elements < elements)
return -2;
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;
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)
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)