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,
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,
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]))
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;
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);
97 for (
size_t t = 0; t < tokens; ++t) {
98 for (
size_t f = 0; f < frames; ++f) {
100 if (!filter) value = score_scratch[t * frames + f];
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];
108 while (m && window[m - 1] > item) {
109 window[m] = window[m - 1];
114 value = window[median_width / 2];
116 mean_scratch[t * frames + f] += value / (float)heads;
120 for (
size_t n = 0; n < matrix; ++n)
121 if (!isfinite(mean_scratch[n]))
return -5;
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;
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];
136 if (diagonal < vertical && diagonal < horizontal) {
137 direction = 0; prior = diagonal;
138 }
else if (vertical < diagonal && vertical < horizontal) {
139 direction = 1; prior = vertical;
141 direction = 2; prior = horizontal;
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;
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;
153 uint8_t direction = t && f ? trace_scratch[t * stride + f] :
155 if (direction == 0) { --t; --f; }
156 else if (direction == 1) --t;
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)