4static float rounded(
float value,
int bf16) {
10static int rotate_rows(
const float *input,
size_t input_elements,
11 const float *cosine,
const float *sine,
size_t table_elements,
12 float *output,
size_t output_elements,
size_t tokens,
size_t heads,
13 size_t head_dim,
int bf16,
int split_half)
16 if (!head_dim || (head_dim & 1) || !heads || heads > SIZE_MAX / head_dim ||
19 input_elements < ni || output_elements < ni || table_elements < nt)
return -2;
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];
40 if (!isfinite(
left) || !isfinite(
right))
return -3;
41 if (pass) { output[at] =
left; output[other] =
right; }
47 const float *cosine,
const float *sine,
size_t table_elements,
48 float *output,
size_t output_elements,
size_t tokens,
size_t heads,
size_t head_dim)
50 return rotate_rows(input, input_elements, cosine, sine, table_elements,
51 output, output_elements, tokens, heads, head_dim, 0, 0);
55 const float *cosine,
const float *sine,
size_t table_elements,
56 float *output,
size_t output_elements,
size_t tokens,
size_t heads,
size_t head_dim)
58 return rotate_rows(input, input_elements, cosine, sine, table_elements,
59 output, output_elements, tokens, heads, head_dim, 1, 0);
63 const float *cosine,
const float *sine,
size_t table_elements,
64 float *output,
size_t output_elements,
size_t tokens,
size_t heads,
size_t head_dim)
66 return rotate_rows(input, input_elements, cosine, sine, table_elements,
67 output, output_elements, tokens, heads, head_dim, 0, 1);
71 float *output,
size_t output_elements,
size_t heads,
size_t head_dim,
72 size_t rotary_dim,
size_t capacity,
size_t position,
float freq_base)
75 if (!rotary_dim || (rotary_dim & 1) || rotary_dim > head_dim ||
76 position >= capacity || !isfinite(freq_base) || freq_base <= 1.0f ||
78 input_elements != n || output_elements != n)
return -2;
82 const size_t half = rotary_dim / 2;
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;
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];
static uint16_t float_to_bf16(float f)
static float bf16_to_float(uint16_t v)
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)
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)
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)
static float rounded(float value, int bf16)
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_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_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)
const char const char * right