← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
indexed_scale.c File Reference

Go to the source code of this file.

Functions

int ck_indexed_scale_f32 (const float *values, const int32_t *ids, const float *scales, float *output, size_t count, size_t scale_count)

Function Documentation

◆ ck_indexed_scale_f32()

int ck_indexed_scale_f32 ( const float * values,
const int32_t * ids,
const float * scales,
float * output,
size_t count,
size_t scale_count )

Definition at line 3 of file indexed_scale.c.

5 {
6 if (!count || !scale_count ||
7 !ck_checked_region(values, count) || !ck_checked_region(scales, scale_count) ||
8 !ck_checked_region(output, count) || !ids ||
9 (uintptr_t)ids % _Alignof(int32_t) != 0 ||
10 count > (UINTPTR_MAX - (uintptr_t)ids) / sizeof(*ids)) {
11 return -1;
12 }
13
14 uintptr_t output_begin = (uintptr_t)output;
15 uintptr_t ids_begin = (uintptr_t)ids;
16 if (ck_checked_overlap(values, count, output, count) ||
17 ck_checked_overlap(scales, scale_count, output, count) ||
18 (output_begin < ids_begin + count * sizeof(*ids) &&
19 ids_begin < output_begin + count * sizeof(*output))) {
20 return -1;
21 }
22
23 for (size_t i = 0; i < count; ++i) {
24 int32_t id = ids[i];
25 if (id < 0 || (size_t)id >= scale_count) {
26 return -2;
27 }
28 float product = values[i] * scales[id];
29 if (!isfinite(product)) {
30 return -3;
31 }
32 }
33 for (size_t i = 0; i < count; ++i) {
34 output[i] = values[i] * scales[ids[i]];
35 }
36 return 0;
37}
static int product(size_t a, size_t b, size_t *out)
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 int32_t * ids
Definition tokenizer.h:447
int32_t id
Definition tokenizer.h:317
const uint8_t size_t count
Definition true_bpe.h:211

References ck_checked_overlap(), ck_checked_region(), count, id, ids, and product().