← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
decoder_features_sequence.c
Go to the documentation of this file.
2#include <math.h>
3#include <stdint.h>
4#include <string.h>
5
6static int span(const void *p, size_t n, uintptr_t *lo, uintptr_t *hi) {
7 if (!p || !n) return 0;
8 *lo = (uintptr_t)p;
9 if (n > UINTPTR_MAX - *lo) return 0;
10 *hi = *lo + n;
11 return 1;
12}
13
15 const void *opaque, size_t context_bytes,
16 const int32_t *ids, size_t id_elements, size_t tokens, size_t channels,
17 float *output, size_t output_elements,
18 float *scratch, size_t scratch_elements) {
19 if (!opaque || context_bytes != sizeof(CKDecoderFeatures) ||
20 (uintptr_t)opaque % _Alignof(CKDecoderFeatures) ||
21 !tokens || !channels || tokens > SIZE_MAX / channels)
22 return -1;
23 const size_t elements = tokens * channels;
24 if (elements > SIZE_MAX / sizeof(float) || tokens > SIZE_MAX / sizeof(int32_t) ||
25 id_elements < tokens || output_elements < elements || scratch_elements < elements)
26 return -2;
27 uintptr_t lo[5], hi[5];
28 if (!span(opaque, sizeof(CKDecoderFeatures), &lo[0], &hi[0]) ||
29 !span(ids, tokens * sizeof(int32_t), &lo[1], &hi[1]) ||
30 !span(output, elements * sizeof(float), &lo[2], &hi[2]) ||
31 !span(scratch, elements * sizeof(float), &lo[3], &hi[3]) ||
32 (uintptr_t)ids % _Alignof(int32_t) ||
33 (uintptr_t)output % _Alignof(float) || (uintptr_t)scratch % _Alignof(float))
34 return -2;
35 const CKDecoderFeatures *ctx = (const CKDecoderFeatures *)opaque;
36 if (ctx->abi_version != 1 || ctx->channels != channels ||
37 ctx->feature_elements < channels || !ctx->decode || !ctx->reset ||
38 !ctx->active_tokens || !ctx->context_window || !ctx->vocabulary_size ||
39 !span(ctx->features, channels * sizeof(float), &lo[4], &hi[4]) ||
40 (uintptr_t)ctx->features % _Alignof(float))
41 return -1;
42 /* The live feature row is writable by decode, so it must not overlap IDs
43 or descriptor storage either. Keep all five declared regions disjoint. */
44 for (size_t i = 0; i < 5; ++i)
45 for (size_t j = i + 1; j < 5; ++j)
46 if (lo[i] < hi[j] && lo[j] < hi[i]) return -2;
47 const int capacity = ctx->context_window();
48 const int vocabulary = ctx->vocabulary_size();
49 if (capacity <= 0 || vocabulary <= 0 || tokens > (size_t)capacity) return -2;
50 for (size_t row = 0; row < tokens; ++row)
51 if (ids[row] < 0 || ids[row] >= vocabulary) return -1;
52 ctx->reset();
53 int status = ctx->active_tokens() == 0 ? 0 : -3;
54 for (size_t row = 0; status == 0 && row < tokens; ++row) {
55 if (ctx->should_cancel && ctx->should_cancel(ctx->cancel_context)) {
56 status = -5;
57 break;
58 }
59 if (ctx->decode(ids[row], NULL) != 0 ||
60 ctx->active_tokens() != (int)(row + 1)) {
61 status = -3;
62 break;
63 }
64 for (size_t col = 0; col < channels; ++col) {
65 const float value = ctx->features[col];
66 if (!isfinite(value)) {
67 status = -4;
68 break;
69 }
70 scratch[row * channels + col] = value;
71 }
72 }
73 ctx->reset();
74 if (ctx->active_tokens() != 0) status = -3;
75 if (!status && ctx->should_cancel && ctx->should_cancel(ctx->cancel_context)) status = -5;
76 if (status) return status;
77 memcpy(output, scratch, elements * sizeof(float));
78 return 0;
79}
static int span(const void *p, size_t n, uintptr_t *lo, uintptr_t *hi)
int decoder_features_sequence_checked_f32(const void *opaque, size_t context_bytes, const int32_t *ids, size_t id_elements, size_t tokens, size_t channels, float *output, size_t output_elements, float *scratch, size_t scratch_elements)
int(* should_cancel)(void *context)
int(* decode)(int32_t token, float *logits)
int(* vocabulary_size)(void)
int(* context_window)(void)
const int32_t * ids
Definition tokenizer.h:447