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

Go to the source code of this file.

Functions

int ck_repetition_penalty_last_row_checked_f32 (const float *values, size_t values_elements, size_t rows, size_t channels, size_t row_stride, const int32_t *history, size_t history_elements, size_t history_count, size_t penalty_numerator, size_t penalty_denominator, float *output, size_t output_elements)
static int overlaps (uintptr_t a, size_t a_bytes, uintptr_t b, size_t b_bytes)

Function Documentation

◆ ck_repetition_penalty_last_row_checked_f32()

int ck_repetition_penalty_last_row_checked_f32 ( const float * values,
size_t values_elements,
size_t rows,
size_t channels,
size_t row_stride,
const int32_t * history,
size_t history_elements,
size_t history_count,
size_t penalty_numerator,
size_t penalty_denominator,
float * output,
size_t output_elements )

Definition at line 11 of file repetition_penalty_checked.c.

17{
18 if (!rows || !channels || channels > INT32_MAX || row_stride < channels ||
19 !penalty_numerator || !penalty_denominator ||
20 penalty_numerator > UINT32_MAX || penalty_denominator > UINT32_MAX ||
21 history_count > history_elements || output_elements < channels ||
22 rows - 1 > (SIZE_MAX - channels) / row_stride)
23 return -2;
24 const size_t values_span = (rows - 1) * row_stride + channels;
25 if (values_span > SIZE_MAX / sizeof(float) ||
26 history_count > SIZE_MAX / sizeof(int32_t) ||
27 channels > SIZE_MAX / sizeof(float) || values_elements < values_span)
28 return -2;
29 const float penalty = (float)penalty_numerator / (float)penalty_denominator;
30 if (!isfinite(penalty) || penalty <= 0.0f)
31 return -2;
32 const size_t values_bytes = values_span * sizeof(float);
33 const size_t history_bytes = history_count * sizeof(int32_t);
34 const size_t output_bytes = channels * sizeof(float);
35 const uintptr_t v = (uintptr_t)values, h = (uintptr_t)history;
36 const uintptr_t o = (uintptr_t)output;
37 if (!values || !output || (history_count && !history) ||
38 v % _Alignof(float) || o % _Alignof(float) ||
39 (history_count && h % _Alignof(int32_t)) ||
40 v > UINTPTR_MAX - values_bytes || o > UINTPTR_MAX - output_bytes ||
41 (history_count && h > UINTPTR_MAX - history_bytes) ||
42 overlaps(v, values_bytes, o, output_bytes) ||
43 (history_count && overlaps(h, history_bytes, o, output_bytes)))
44 return -1;
45 for (size_t i = 0; i < history_count; ++i)
46 if (history[i] < 0 || (size_t)history[i] >= channels)
47 return -2;
48 const float *last = values + (rows - 1) * row_stride;
49 /* Check the whole output before writing any element. */
50 for (size_t j = 0; j < channels; ++j) {
51 if (!isfinite(last[j])) return -3;
52 int seen = 0;
53 for (size_t i = 0; i < history_count; ++i)
54 if ((size_t)history[i] == j) { seen = 1; break; }
55 const float adjusted = seen ?
56 (last[j] < 0.0f ? last[j] * penalty : last[j] / penalty) : last[j];
57 if (!isfinite(adjusted)) return -3;
58 }
59 for (size_t j = 0; j < channels; ++j) {
60 int seen = 0;
61 for (size_t i = 0; i < history_count; ++i)
62 if ((size_t)history[i] == j) { seen = 1; break; }
63 output[j] = seen ?
64 (last[j] < 0.0f ? last[j] * penalty : last[j] / penalty) : last[j];
65 }
66 return 0;
67}
static int overlaps(uintptr_t a, size_t a_bytes, uintptr_t b, size_t b_bytes)

References overlaps().

◆ overlaps()

int overlaps ( uintptr_t a,
size_t a_bytes,
uintptr_t b,
size_t b_bytes )
static

Definition at line 6 of file repetition_penalty_checked.c.

7{
8 return a < b + b_bytes && b < a + a_bytes;
9}

Referenced by ck_repetition_penalty_last_row_checked_f32().