← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
gelu_rows_erf_checked.c
Go to the documentation of this file.
1#include "ckernel_engine.h"
3
4#include <string.h>
5
6/* Checked row adapter for the existing FP64-erf/FP32-storage GELU arithmetic. */
7int gelu_rows_erf_checked_f32(const float *input, size_t input_elements,
8 size_t input_stride, float *output, size_t output_elements,
9 size_t output_stride, float *scratch, size_t scratch_elements,
10 size_t rows, size_t channels) {
11 size_t input_span, output_span, staged;
12 if (!ck_checked_span(rows, channels, input_stride, &input_span) ||
13 !ck_checked_span(rows, channels, output_stride, &output_span) ||
14 rows > SIZE_MAX / channels || rows * channels > SIZE_MAX / sizeof(float))
15 return -2;
16 staged = rows * channels;
17 if (input_elements < input_span || output_elements < output_span ||
18 scratch_elements < staged)
19 return -3;
20 if (!ck_checked_region(input, input_span) ||
21 !ck_checked_region(output, output_span) ||
22 !ck_checked_region(scratch, staged) ||
23 ck_checked_overlap(input, input_span, output, output_span) ||
24 ck_checked_overlap(input, input_span, scratch, staged) ||
25 ck_checked_overlap(output, output_span, scratch, staged))
26 return -1;
27 for (size_t r = 0; r < rows; ++r)
28 if (!ck_checked_finite(input + r * input_stride, channels)) return -4;
29 for (size_t r = 0; r < rows; ++r)
30 memcpy(scratch + r * channels, input + r * input_stride,
31 channels * sizeof(float));
32 gelu_erf_fp64_f32_inplace(scratch, staged);
33 if (!ck_checked_finite(scratch, staged)) return -4;
34 for (size_t r = 0; r < rows; ++r)
35 memcpy(output + r * output_stride, scratch + r * channels,
36 channels * sizeof(float));
37 return 0;
38}
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)
void gelu_erf_fp64_f32_inplace(float *data, size_t n)
int gelu_rows_erf_checked_f32(const float *input, size_t input_elements, size_t input_stride, float *output, size_t output_elements, size_t output_stride, float *scratch, size_t scratch_elements, size_t rows, size_t channels)