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

Go to the source code of this file.

Functions

int ck_grouped_cross_entropy_f32 (const float *logits, size_t logits_elements, const int32_t *counts, size_t count_elements, const int32_t *targets, size_t target_elements, float *d_logits, size_t gradient_elements, float *loss, size_t loss_elements, float *scratch, size_t scratch_elements, size_t groups, size_t options)
int ck_prefix_cross_entropy_f32 (const float *logits, size_t logits_elements, const int32_t *counts, size_t count_elements, const int32_t *targets, size_t target_elements, float *d_logits, size_t gradient_elements, float *loss, size_t loss_elements, float *scratch, size_t scratch_elements, size_t groups, size_t columns)
static int grouped_cross_entropy_f32 (const float *logits, size_t logits_elements, const int32_t *counts, size_t count_elements, const int32_t *targets, size_t target_elements, float *d_logits, size_t gradient_elements, float *loss, size_t loss_elements, float *scratch, size_t scratch_elements, size_t groups, size_t options, size_t row_width)

Function Documentation

◆ ck_grouped_cross_entropy_f32()

int ck_grouped_cross_entropy_f32 ( const float * logits,
size_t logits_elements,
const int32_t * counts,
size_t count_elements,
const int32_t * targets,
size_t target_elements,
float * d_logits,
size_t gradient_elements,
float * loss,
size_t loss_elements,
float * scratch,
size_t scratch_elements,
size_t groups,
size_t options )

Definition at line 71 of file grouped_cross_entropy.c.

78 {
79 return grouped_cross_entropy_f32(logits, logits_elements, counts, count_elements,
80 targets, target_elements, d_logits, gradient_elements, loss, loss_elements,
81 scratch, scratch_elements, groups, options, 0);
82}
static int grouped_cross_entropy_f32(const float *logits, size_t logits_elements, const int32_t *counts, size_t count_elements, const int32_t *targets, size_t target_elements, float *d_logits, size_t gradient_elements, float *loss, size_t loss_elements, float *scratch, size_t scratch_elements, size_t groups, size_t options, size_t row_width)

References grouped_cross_entropy_f32().

◆ ck_prefix_cross_entropy_f32()

int ck_prefix_cross_entropy_f32 ( const float * logits,
size_t logits_elements,
const int32_t * counts,
size_t count_elements,
const int32_t * targets,
size_t target_elements,
float * d_logits,
size_t gradient_elements,
float * loss,
size_t loss_elements,
float * scratch,
size_t scratch_elements,
size_t groups,
size_t columns )

Definition at line 84 of file grouped_cross_entropy.c.

91 {
92 if (!columns || groups > SIZE_MAX / columns) return -2;
93 return grouped_cross_entropy_f32(logits, logits_elements, counts, count_elements,
94 targets, target_elements, d_logits, gradient_elements, loss, loss_elements,
95 scratch, scratch_elements, groups, groups * columns, columns);
96}

References grouped_cross_entropy_f32().

◆ grouped_cross_entropy_f32()

int grouped_cross_entropy_f32 ( const float * logits,
size_t logits_elements,
const int32_t * counts,
size_t count_elements,
const int32_t * targets,
size_t target_elements,
float * d_logits,
size_t gradient_elements,
float * loss,
size_t loss_elements,
float * scratch,
size_t scratch_elements,
size_t groups,
size_t options,
size_t row_width )
static

Definition at line 8 of file grouped_cross_entropy.c.

15 {
16 if (!groups || !options || groups > options || options == SIZE_MAX ||
17 options > SIZE_MAX / sizeof(float) - 1 || logits_elements < options ||
18 gradient_elements < options || count_elements < groups || target_elements < groups ||
19 !loss_elements || scratch_elements < options + 1) return -2;
20 const void *ptrs[] = {logits, counts, targets, d_logits, loss, scratch};
21 size_t sizes[] = {options, groups, groups, options, 1, options+1};
22 for (size_t i=0; i<6; ++i) if (!ck_checked_region(ptrs[i], sizes[i])) return -1;
23 for (size_t i=3; i<6; ++i) for (size_t j=0; j<i; ++j)
24 if (ck_checked_overlap(ptrs[i], sizes[i], ptrs[j], sizes[j])) return -1;
25 size_t start = 0;
26 for (size_t group = 0; group < groups; ++group) {
27 int32_t count = counts[group];
28 size_t available = row_width ? row_width : options - start;
29 if (count <= 0 || (size_t)count > available ||
30 targets[group] < 0 || targets[group] >= count) return -2;
31 start += row_width ? row_width : (size_t)count;
32 }
33 if (start != options) return -3;
34 start = 0;
35 for (size_t group = 0; group < groups; ++group) {
36 size_t count = (size_t)counts[group];
37 if (!ck_checked_finite(logits + start, count)) return -3;
38 start += row_width ? row_width : count;
39 }
40
41 double accumulated_loss = 0.0;
42 start = 0;
43 for (size_t group = 0; group < groups; ++group) {
44 size_t count = (size_t)counts[group];
45 size_t width = row_width ? row_width : count;
46 const float *scores = logits + start;
47 float *gradient = scratch + start;
48 size_t target = (size_t)targets[group];
49 double maximum = scores[0];
50 for (size_t i = 1; i < count; ++i)
51 if ((double)scores[i] > maximum) maximum = scores[i];
52 double sum = 0.0;
53 for (size_t i = 0; i < count; ++i) sum += exp((double)scores[i] - maximum);
54 /* Subtract before adding log(sum), avoiding cancellation for large offsets. */
55 double group_loss = (maximum - (double)scores[target]) + log(sum);
56 accumulated_loss += group_loss / (double)groups;
57 if (!isfinite(accumulated_loss) || accumulated_loss > FLT_MAX) return -4;
58 for (size_t i = 0; i < count; ++i) {
59 double probability = exp((double)scores[i] - maximum) / sum;
60 gradient[i] = (float)((probability - (i == target ? 1.0 : 0.0)) / (double)groups);
61 }
62 for (size_t i = count; i < width; ++i) gradient[i] = 0.0f;
63 start += width;
64 }
65 scratch[options]=(float)accumulated_loss;
66 memcpy(d_logits,scratch,options*sizeof(float));
67 loss[0]=scratch[options];
68 return 0;
69}
static int ck_checked_finite(const float *ptr, size_t n)
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)
const uint8_t size_t count
Definition true_bpe.h:211
uint32_t start
Definition utf8.c:214

References ck_checked_finite(), ck_checked_overlap(), ck_checked_region(), count, and start.

Referenced by ck_grouped_cross_entropy_f32(), and ck_prefix_cross_entropy_f32().