← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
rope_pairwise_rows_checked.c File Reference
#include "checked_float_buffer.h"
#include "bf16_utils.h"

Go to the source code of this file.

Functions

int rope_pairwise_rows_bf16_storage_checked (const float *input, size_t input_elements, const float *cosine, const float *sine, size_t table_elements, float *output, size_t output_elements, size_t tokens, size_t heads, size_t head_dim)
int rope_pairwise_rows_f32_checked (const float *input, size_t input_elements, const float *cosine, const float *sine, size_t table_elements, float *output, size_t output_elements, size_t tokens, size_t heads, size_t head_dim)
int rope_split_partial_position_f32_checked (const float *input, size_t input_elements, float *output, size_t output_elements, size_t heads, size_t head_dim, size_t rotary_dim, size_t capacity, size_t position, float freq_base)
int rope_split_rows_f32_checked (const float *input, size_t input_elements, const float *cosine, const float *sine, size_t table_elements, float *output, size_t output_elements, size_t tokens, size_t heads, size_t head_dim)
static int rotate_rows (const float *input, size_t input_elements, const float *cosine, const float *sine, size_t table_elements, float *output, size_t output_elements, size_t tokens, size_t heads, size_t head_dim, int bf16, int split_half)
static float rounded (float value, int bf16)

Function Documentation

◆ rope_pairwise_rows_bf16_storage_checked()

int rope_pairwise_rows_bf16_storage_checked ( const float * input,
size_t input_elements,
const float * cosine,
const float * sine,
size_t table_elements,
float * output,
size_t output_elements,
size_t tokens,
size_t heads,
size_t head_dim )

Definition at line 54 of file rope_pairwise_rows_checked.c.

57{
58 return rotate_rows(input, input_elements, cosine, sine, table_elements,
59 output, output_elements, tokens, heads, head_dim, 1, 0);
60}
static int rotate_rows(const float *input, size_t input_elements, const float *cosine, const float *sine, size_t table_elements, float *output, size_t output_elements, size_t tokens, size_t heads, size_t head_dim, int bf16, int split_half)

References rotate_rows().

◆ rope_pairwise_rows_f32_checked()

int rope_pairwise_rows_f32_checked ( const float * input,
size_t input_elements,
const float * cosine,
const float * sine,
size_t table_elements,
float * output,
size_t output_elements,
size_t tokens,
size_t heads,
size_t head_dim )

Definition at line 46 of file rope_pairwise_rows_checked.c.

49{
50 return rotate_rows(input, input_elements, cosine, sine, table_elements,
51 output, output_elements, tokens, heads, head_dim, 0, 0);
52}

References rotate_rows().

◆ rope_split_partial_position_f32_checked()

int rope_split_partial_position_f32_checked ( const float * input,
size_t input_elements,
float * output,
size_t output_elements,
size_t heads,
size_t head_dim,
size_t rotary_dim,
size_t capacity,
size_t position,
float freq_base )

Definition at line 70 of file rope_pairwise_rows_checked.c.

73{
74 size_t n;
75 if (!rotary_dim || (rotary_dim & 1) || rotary_dim > head_dim ||
76 position >= capacity || !isfinite(freq_base) || freq_base <= 1.0f ||
77 !ck_checked_span(heads, head_dim, head_dim, &n) ||
78 input_elements != n || output_elements != n) return -2;
79 if (!ck_checked_region(input, n) || !ck_checked_region(output, n) ||
80 ck_checked_overlap(input, n, output, n)) return -1;
81 if (!ck_checked_finite(input, n)) return -3;
82 const size_t half = rotary_dim / 2;
83 for (int publish = 0; publish < 2; ++publish)
84 for (size_t d = 0; d < half; ++d) {
85 const double angle = (double)position * pow((double)freq_base, -(double)d / half);
86 const float c = (float)cos(angle), s = (float)sin(angle);
87 for (size_t h = 0; h < heads; ++h) {
88 const size_t a = h * head_dim + d, b = a + half;
89 const float left = input[a] * c - input[b] * s;
90 const float right = input[b] * c + input[a] * s;
91 if (!isfinite(left) || !isfinite(right)) return -3;
92 if (publish) {
93 output[a] = left;
94 output[b] = right;
95 }
96 }
97 }
98 for (size_t h = 0; h < heads; ++h)
99 for (size_t d = rotary_dim; d < head_dim; ++d)
100 output[h * head_dim + d] = input[h * head_dim + d];
101 return 0;
102}
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)
static int publish(const int32_t *indices, float *dl, float *dr, float *scratch, size_t groups, size_t rows, size_t channels, size_t nl, size_t nr)
const char * left
Definition true_bpe.h:140
const char const char * right
Definition true_bpe.h:141

References ck_checked_finite(), ck_checked_overlap(), ck_checked_region(), ck_checked_span(), left, publish(), and right.

◆ rope_split_rows_f32_checked()

int rope_split_rows_f32_checked ( const float * input,
size_t input_elements,
const float * cosine,
const float * sine,
size_t table_elements,
float * output,
size_t output_elements,
size_t tokens,
size_t heads,
size_t head_dim )

Definition at line 62 of file rope_pairwise_rows_checked.c.

65{
66 return rotate_rows(input, input_elements, cosine, sine, table_elements,
67 output, output_elements, tokens, heads, head_dim, 0, 1);
68}

References rotate_rows().

◆ rotate_rows()

int rotate_rows ( const float * input,
size_t input_elements,
const float * cosine,
const float * sine,
size_t table_elements,
float * output,
size_t output_elements,
size_t tokens,
size_t heads,
size_t head_dim,
int bf16,
int split_half )
static

Definition at line 10 of file rope_pairwise_rows_checked.c.

14{
15 size_t ni, nt;
16 if (!head_dim || (head_dim & 1) || !heads || heads > SIZE_MAX / head_dim ||
17 !ck_checked_span(tokens, heads * head_dim, heads * head_dim, &ni) ||
18 !ck_checked_span(tokens, head_dim / 2, head_dim / 2, &nt) ||
19 input_elements < ni || output_elements < ni || table_elements < nt) return -2;
20 if (!ck_checked_region(input, ni) || !ck_checked_region(output, ni) ||
21 !ck_checked_region(cosine, nt) || !ck_checked_region(sine, nt) ||
22 ck_checked_overlap(input, ni, output, ni) ||
23 ck_checked_overlap(cosine, nt, output, ni) ||
24 ck_checked_overlap(sine, nt, output, ni)) return -1;
25 if (!ck_checked_finite(input, ni) || !ck_checked_finite(cosine, nt) ||
26 !ck_checked_finite(sine, nt)) return -3;
27 for (int pass = 0; pass < 2; ++pass)
28 for (size_t t = 0; t < tokens; ++t)
29 for (size_t h = 0; h < heads; ++h)
30 for (size_t d = 0; d < head_dim / 2; ++d) {
31 size_t at = (t * heads + h) * head_dim + (split_half ? d : 2 * d);
32 size_t other = at + (split_half ? head_dim / 2 : 1);
33 size_t phase = t * (head_dim / 2) + d;
34 float a = input[at], b = input[other];
35 float c = cosine[phase], s = sine[phase];
36 if (bf16 && (rounded(a, 1) != a || rounded(b, 1) != b ||
37 rounded(c, 1) != c || rounded(s, 1) != s)) return -3;
38 float left = rounded(rounded(a * c, bf16) - rounded(b * s, bf16), bf16);
39 float right = rounded(rounded(b * c, bf16) + rounded(a * s, bf16), bf16);
40 if (!isfinite(left) || !isfinite(right)) return -3;
41 if (pass) { output[at] = left; output[other] = right; }
42 }
43 return 0;
44}
static float rounded(float value, int bf16)

References ck_checked_finite(), ck_checked_overlap(), ck_checked_region(), ck_checked_span(), left, right, and rounded().

Referenced by rope_pairwise_rows_bf16_storage_checked(), rope_pairwise_rows_f32_checked(), and rope_split_rows_f32_checked().

◆ rounded()

float rounded ( float value,
int bf16 )
static

Definition at line 4 of file rope_pairwise_rows_checked.c.

4 {
5 return bf16 ? bf16_to_float(float_to_bf16(value)) : value;
6}
static uint16_t float_to_bf16(float f)
Definition bf16_utils.h:90
static float bf16_to_float(uint16_t v)
Definition bf16_utils.h:38

References bf16_to_float(), and float_to_bf16().

Referenced by rotate_rows().