← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
alignment_attention_dtw_f32.c
Go to the documentation of this file.
1/* Generic attention-to-token-frame alignment. Caller owns every buffer.
2 * Selected heads are already packed into [head, token, frame] storage.
3 * The normalization/median/DTW order matches the pinned Whisper reference;
4 * no model identity or tokenizer semantics enter this operation. */
5#include <math.h>
6#include <stddef.h>
7#include <stdint.h>
8
9static int mul_size(size_t a, size_t b, size_t *out) {
10 if (a && b > SIZE_MAX / a) return 0;
11 *out = a * b;
12 return 1;
13}
14
15static int overlaps(const void *a, size_t na, const void *b, size_t nb) {
16 uintptr_t x = (uintptr_t)a, y = (uintptr_t)b;
17 if (na > UINTPTR_MAX - x || nb > UINTPTR_MAX - y) return 1;
18 return x < y + nb && y < x + na;
19}
20
21static size_t reflect_index(int64_t index, size_t width) {
22 const int64_t last = (int64_t)width - 1;
23 while (index < 0 || index > last) {
24 if (index < 0) index = -index;
25 if (index > last) index = 2 * last - index;
26 }
27 return (size_t)index;
28}
29
30/* Return 0 on success. A rejected call never changes token_frames.
31 * score/mean/cost/trace capacities are element counts, not bytes. */
33 const float *attention, size_t attention_elements,
34 size_t heads, size_t tokens, size_t frames,
35 size_t token_stride, size_t head_stride, size_t median_width,
36 float *score_scratch, size_t score_elements,
37 float *mean_scratch, size_t mean_elements,
38 float *cost_scratch, size_t cost_elements,
39 uint8_t *trace_scratch, size_t trace_elements,
40 int32_t *token_frames, size_t token_capacity)
41{
42 size_t matrix, cells, input_required, row_required, bytes_matrix;
43 if (!attention || !score_scratch || !mean_scratch || !cost_scratch ||
44 !trace_scratch || !token_frames || !heads || !tokens || !frames ||
45 tokens > INT32_MAX || frames > INT32_MAX ||
46 frames > INT64_MAX / 2 || !median_width || !(median_width & 1) ||
47 median_width > 31 || token_stride < frames ||
48 !mul_size(tokens, frames, &matrix) ||
49 !mul_size(tokens + 1, frames + 1, &cells) ||
50 !mul_size(heads - 1, head_stride, &input_required) ||
51 !mul_size(tokens - 1, token_stride, &row_required) ||
52 row_required > SIZE_MAX - frames ||
53 input_required > SIZE_MAX - row_required - frames ||
54 !mul_size(matrix, sizeof(float), &bytes_matrix)) return -1;
55 input_required += row_required + frames;
56 if (head_stride < row_required + frames || attention_elements < input_required ||
57 score_elements < matrix || mean_elements < matrix ||
58 cost_elements < cells || trace_elements < cells ||
59 token_capacity < tokens) return -2;
60 size_t input_bytes, cost_bytes, output_bytes;
61 if (!mul_size(attention_elements, sizeof(float), &input_bytes) ||
62 !mul_size(cost_elements, sizeof(float), &cost_bytes) ||
63 !mul_size(token_capacity, sizeof(int32_t), &output_bytes)) return -1;
64 const void *ptrs[] = {attention, score_scratch, mean_scratch,
65 cost_scratch, trace_scratch, token_frames};
66 const size_t lengths[] = {input_bytes, bytes_matrix, bytes_matrix,
67 cost_bytes, trace_elements, output_bytes};
68 for (size_t i = 0; i < 6; ++i)
69 for (size_t j = i + 1; j < 6; ++j)
70 if (overlaps(ptrs[i], lengths[i], ptrs[j], lengths[j])) return -3;
71 for (size_t h = 0; h < heads; ++h)
72 for (size_t t = 0; t < tokens; ++t)
73 for (size_t f = 0; f < frames; ++f)
74 if (!isfinite(attention[h * head_stride + t * token_stride + f]))
75 return -4;
76
77 for (size_t n = 0; n < matrix; ++n) mean_scratch[n] = 0.0f;
78 const int filter = frames > median_width / 2;
79 for (size_t h = 0; h < heads; ++h) {
80 const float *head = attention + h * head_stride;
81 for (size_t f = 0; f < frames; ++f) {
82 double mean = 0.0, variance = 0.0;
83 for (size_t t = 0; t < tokens; ++t)
84 mean += head[t * token_stride + f];
85 mean /= (double)tokens;
86 for (size_t t = 0; t < tokens; ++t) {
87 double delta = (double)head[t * token_stride + f] - mean;
88 variance += delta * delta;
89 }
90 variance /= (double)tokens;
91 if (!(variance > 0.0) || !isfinite(variance)) return -5;
92 const double inverse = 1.0 / sqrt(variance);
93 for (size_t t = 0; t < tokens; ++t)
94 score_scratch[t * frames + f] =
95 (float)(((double)head[t * token_stride + f] - mean) * inverse);
96 }
97 for (size_t t = 0; t < tokens; ++t) {
98 for (size_t f = 0; f < frames; ++f) {
99 float value;
100 if (!filter) value = score_scratch[t * frames + f];
101 else {
102 float window[31];
103 const int64_t half = (int64_t)(median_width / 2);
104 for (size_t k = 0; k < median_width; ++k) {
105 size_t source = reflect_index((int64_t)f + (int64_t)k - half, frames);
106 float item = score_scratch[t * frames + source];
107 size_t m = k;
108 while (m && window[m - 1] > item) {
109 window[m] = window[m - 1];
110 --m;
111 }
112 window[m] = item;
113 }
114 value = window[median_width / 2];
115 }
116 mean_scratch[t * frames + f] += value / (float)heads;
117 }
118 }
119 }
120 for (size_t n = 0; n < matrix; ++n)
121 if (!isfinite(mean_scratch[n])) return -5;
122
123 const size_t stride = frames + 1;
124 for (size_t n = 0; n < cells; ++n) {
125 cost_scratch[n] = INFINITY;
126 trace_scratch[n] = 255;
127 }
128 cost_scratch[0] = 0.0f;
129 for (size_t f = 1; f <= frames; ++f) {
130 for (size_t t = 1; t <= tokens; ++t) {
131 const float diagonal = cost_scratch[(t - 1) * stride + f - 1];
132 const float vertical = cost_scratch[(t - 1) * stride + f];
133 const float horizontal = cost_scratch[t * stride + f - 1];
134 uint8_t direction;
135 float prior;
136 if (diagonal < vertical && diagonal < horizontal) {
137 direction = 0; prior = diagonal;
138 } else if (vertical < diagonal && vertical < horizontal) {
139 direction = 1; prior = vertical;
140 } else {
141 direction = 2; prior = horizontal;
142 }
143 const size_t cell = t * stride + f;
144 cost_scratch[cell] = -mean_scratch[(t - 1) * frames + f - 1] + prior;
145 trace_scratch[cell] = direction;
146 }
147 }
148 if (!isfinite(cost_scratch[tokens * stride + frames])) return -5;
149 for (size_t t = 0; t < tokens; ++t) token_frames[t] = -1;
150 size_t t = tokens, f = frames;
151 while (t || f) {
152 if (t) token_frames[t - 1] = (int32_t)(f ? f - 1 : 0);
153 uint8_t direction = t && f ? trace_scratch[t * stride + f] :
154 t ? 1 : 2;
155 if (direction == 0) { --t; --f; }
156 else if (direction == 1) --t;
157 else --f;
158 }
159 return 0;
160}
static int overlaps(const void *a, size_t na, const void *b, size_t nb)
static size_t reflect_index(int64_t index, size_t width)
static int mul_size(size_t a, size_t b, size_t *out)
int alignment_attention_dtw_f32(const float *attention, size_t attention_elements, size_t heads, size_t tokens, size_t frames, size_t token_stride, size_t head_stride, size_t median_width, float *score_scratch, size_t score_elements, float *mean_scratch, size_t mean_elements, float *cost_scratch, size_t cost_elements, uint8_t *trace_scratch, size_t trace_elements, int32_t *token_frames, size_t token_capacity)
static int token_frames(const float *logits, size_t bins, float speed, size_t max_duration_per_token, int32_t *frames)