← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
grouped_weighted_rows.c File Reference
#include "ckernel_grouped_weighted_rows.h"
#include <float.h>
#include <math.h>
#include <stdint.h>

Go to the source code of this file.

Functions

int ck_grouped_softmax_weighted_rows_f32 (const float *options, size_t option_elements, size_t option_stride, const float *fields, size_t field_elements, size_t field_stride, const int32_t *group_counts, size_t count_elements, float *output, size_t output_elements, size_t output_stride, float *scratch, size_t scratch_elements, size_t groups, size_t option_rows, size_t channels)
static int overlaps (const void *left, size_t left_bytes, const void *right, size_t right_bytes)
static int span_fits (size_t rows, size_t stride, size_t width, size_t elements)
static int valid_region (const void *ptr, size_t elements, size_t bytes, size_t alignment)

Function Documentation

◆ ck_grouped_softmax_weighted_rows_f32()

int ck_grouped_softmax_weighted_rows_f32 ( const float * options,
size_t option_elements,
size_t option_stride,
const float * fields,
size_t field_elements,
size_t field_stride,
const int32_t * group_counts,
size_t count_elements,
float * output,
size_t output_elements,
size_t output_stride,
float * scratch,
size_t scratch_elements,
size_t groups,
size_t option_rows,
size_t channels )

Definition at line 26 of file grouped_weighted_rows.c.

32 {
33 if (!groups || !option_rows || !channels ||
34 groups > SIZE_MAX / channels ||
35 option_rows > SIZE_MAX - groups * channels ||
36 !span_fits(option_rows, option_stride, channels, option_elements) ||
37 !span_fits(groups, field_stride, channels, field_elements) ||
38 !span_fits(groups, output_stride, channels, output_elements) ||
39 count_elements < groups ||
40 scratch_elements < option_rows + groups * channels)
41 return -2;
42 if (!valid_region(options, option_elements, sizeof(float), _Alignof(float)) ||
43 !valid_region(fields, field_elements, sizeof(float), _Alignof(float)) ||
44 !valid_region(group_counts, count_elements, sizeof(int32_t), _Alignof(int32_t)) ||
45 !valid_region(output, output_elements, sizeof(float), _Alignof(float)) ||
46 !valid_region(scratch, scratch_elements, sizeof(float), _Alignof(float)))
47 return -1;
48 const size_t option_bytes = option_elements * sizeof(float);
49 const size_t field_bytes = field_elements * sizeof(float);
50 const size_t count_bytes = count_elements * sizeof(int32_t);
51 const size_t output_bytes = output_elements * sizeof(float);
52 const size_t scratch_bytes = scratch_elements * sizeof(float);
53 if (overlaps(output, output_bytes, options, option_bytes) ||
54 overlaps(output, output_bytes, fields, field_bytes) ||
55 overlaps(output, output_bytes, group_counts, count_bytes) ||
56 overlaps(output, output_bytes, scratch, scratch_bytes) ||
57 overlaps(scratch, scratch_bytes, options, option_bytes) ||
58 overlaps(scratch, scratch_bytes, fields, field_bytes) ||
59 overlaps(scratch, scratch_bytes, group_counts, count_bytes))
60 return -1;
61
62 size_t counted = 0;
63 for (size_t q = 0; q < groups; ++q) {
64 int32_t count = group_counts[q];
65 if (count <= 0 || (size_t)count > option_rows - counted)
66 return -2;
67 counted += (size_t)count;
68 for (size_t c = 0; c < channels; ++c)
69 if (!isfinite(fields[q * field_stride + c])) return -1;
70 }
71 if (counted != option_rows) return -2;
72 for (size_t r = 0; r < option_rows; ++r)
73 for (size_t c = 0; c < channels; ++c)
74 if (!isfinite(options[r * option_stride + c])) return -1;
75
76 /* Scratch holds all probabilities and staged results. No output is
77 * published until every group and output channel has passed validation. */
78 size_t first = 0;
79 const double scale = 1.0 / sqrt((double)channels);
80 for (size_t q = 0; q < groups; ++q) {
81 size_t count = (size_t)group_counts[q];
82 double maximum = -INFINITY;
83 for (size_t i = 0; i < count; ++i) {
84 double dot = 0.0;
85 for (size_t c = 0; c < channels; ++c) {
86 volatile double product =
87 (double)options[(first + i) * option_stride + c] *
88 (double)fields[q * field_stride + c];
89 dot += product;
90 }
91 double logit = dot * scale;
92 if (!isfinite(logit) || fabs(logit) > FLT_MAX) return -1;
93 scratch[first + i] = (float)logit;
94 if ((double)scratch[first + i] > maximum)
95 maximum = (double)scratch[first + i];
96 }
97 double denominator = 0.0;
98 for (size_t i = 0; i < count; ++i) {
99 double value = exp((double)scratch[first + i] - maximum);
100 if (!isfinite(value)) return -1;
101 scratch[first + i] = (float)value;
102 denominator += (double)scratch[first + i];
103 }
104 if (!isfinite(denominator) || denominator <= 0.0) return -1;
105 for (size_t i = 0; i < count; ++i)
106 scratch[first + i] = (float)((double)scratch[first + i] /
107 denominator);
108 for (size_t c = 0; c < channels; ++c) {
109 double sum = 0.0;
110 for (size_t i = 0; i < count; ++i) {
111 volatile double product = (double)scratch[first + i] *
112 (double)options[(first + i) * option_stride + c];
113 sum += product;
114 }
115 if (!isfinite(sum) || fabs(sum) > FLT_MAX) return -1;
116 scratch[option_rows + q * channels + c] = (float)sum;
117 }
118 first += count;
119 }
120 for (size_t q = 0; q < groups; ++q)
121 for (size_t c = 0; c < channels; ++c)
122 output[q * output_stride + c] =
123 scratch[option_rows + q * channels + c];
124 return 0;
125}
static double dot(const float *a, const float *b, size_t n)
static int product(size_t a, size_t b, size_t *out)
static int span_fits(size_t rows, size_t stride, size_t width, size_t elements)
static int valid_region(const void *ptr, size_t elements, size_t bytes, size_t alignment)
static int overlaps(const void *left, size_t left_bytes, const void *right, size_t right_bytes)
const uint8_t size_t count
Definition true_bpe.h:211

References count, dot(), overlaps(), product(), span_fits(), and valid_region().

Referenced by ck_grouped_weighted_rows_backward_f32().

◆ overlaps()

int overlaps ( const void * left,
size_t left_bytes,
const void * right,
size_t right_bytes )
static

Definition at line 20 of file grouped_weighted_rows.c.

21 {
22 uintptr_t a = (uintptr_t)left, b = (uintptr_t)right;
23 return a < b + right_bytes && b < a + left_bytes;
24}
const char * left
Definition true_bpe.h:140
const char const char * right
Definition true_bpe.h:141

References left, and right.

Referenced by ck_grouped_softmax_weighted_rows_f32().

◆ span_fits()

int span_fits ( size_t rows,
size_t stride,
size_t width,
size_t elements )
static

Definition at line 7 of file grouped_weighted_rows.c.

7 {
8 return rows && width && stride >= width &&
9 rows - 1 <= (SIZE_MAX - width) / stride &&
10 (rows - 1) * stride + width <= elements;
11}

Referenced by ck_grouped_softmax_weighted_rows_f32().

◆ valid_region()

int valid_region ( const void * ptr,
size_t elements,
size_t bytes,
size_t alignment )
static

Definition at line 13 of file grouped_weighted_rows.c.

14 {
15 return ptr && (uintptr_t)ptr % alignment == 0 &&
16 elements <= SIZE_MAX / bytes &&
17 (uintptr_t)ptr <= UINTPTR_MAX - elements * bytes;
18}

Referenced by ck_grouped_softmax_weighted_rows_f32().