3#if defined(__ELF__) && (defined(__GNUC__) || defined(__clang__))
5#define CK_DEPTH_OPTIONAL_POOL 1
6#pragma weak ck_get_num_threads
7#pragma weak ck_threadpool_global
8#pragma weak ck_threadpool_in_dispatch
9#pragma weak ck_threadpool_n_threads
10#pragma weak ck_threadpool_parallel_for_n
13static double rms_scale(
const float *x,
size_t width,
double epsilon)
16 for (
size_t c = 0; c < width; ++c) sum += (
double)x[c] * x[c];
17 return 1.0 / sqrt(sum / (
double)width + epsilon);
20struct depth_mix_rows {
21 const float *bank, *current, *query;
29 const struct depth_mix_rows *work = opaque;
30 const float *bank = work->bank, *current = work->current, *query = work->query;
31 float *output = work->output;
32 const size_t states = work->states, width = work->width;
33 const float epsilon = work->epsilon;
34 const double temperature = 1.0 / sqrt((
double)width);
35 for (
size_t r = begin; r <
end; ++r) {
36 double scale[65], logits[65], max_logit = -INFINITY;
37 for (
size_t s = 0; s <= states; ++s) {
38 const float *x = s == states ? current + r * width : bank + (r * states + s) * width;
41 for (
size_t c = 0; c < width; ++c) {
42 const float normalized = (float)(x[c] * scale[s]);
43 dot += (double)query[c] * normalized;
45 logits[s] =
dot * temperature;
46 max_logit = fmax(max_logit, logits[s]);
48 double denominator = 0.0;
49 for (
size_t s = 0; s <= states; ++s) {
50 logits[s] = exp(logits[s] - max_logit);
51 denominator += logits[s];
53 for (
size_t c = 0; c < width; ++c) {
55 for (
size_t s = 0; s <= states; ++s) {
56 const float *x = s == states ? current + r * width : bank + (r * states + s) * width;
57 const float normalized = (float)(x[c] * scale[s]);
58 sum += logits[s] * normalized;
60 output[r * width + c] = (float)(sum / denominator);
66 const float *current,
size_t current_elements,
67 const float *query,
size_t query_elements,
68 float *output,
size_t output_elements,
69 size_t rows,
size_t states,
size_t width,
float epsilon)
71 size_t present, prior;
72 if (!states || states > 64 || !isfinite(epsilon) || epsilon <= 0.0f ||
74 present > SIZE_MAX / states ||
75 (prior = present * states) > SIZE_MAX /
sizeof(
float) ||
76 bank_elements < prior || current_elements < present ||
77 output_elements < present || query_elements < width)
return -2;
86 struct depth_mix_rows work = {
87 .bank = bank, .current = current, .query = query, .output = output,
88 .states = states, .width = width, .epsilon = epsilon,
90#ifdef CK_DEPTH_OPTIONAL_POOL
static double dot(const float *a, const float *b, size_t n)
static int ck_checked_finite(const float *ptr, size_t n)
static int ck_checked_span(size_t rows, size_t width, size_t stride, size_t *span)
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)
static void ck_checked_parallel_for(size_t count, int grain, ck_checked_range_fn fn, void *args)
void ck_threadpool_parallel_for_n(ck_threadpool_t *pool, int active_threads, int begin, int end, int grain_size, ck_range_fn_t fn, void *args)
ck_threadpool_t * ck_threadpool_global(void)
int ck_threadpool_in_dispatch(const ck_threadpool_t *pool)
int ck_threadpool_n_threads(const ck_threadpool_t *pool)
int ck_get_num_threads(void)
int depth_residual_mix_checked_f32(const float *bank, size_t bank_elements, const float *current, size_t current_elements, const float *query, size_t query_elements, float *output, size_t output_elements, size_t rows, size_t states, size_t width, float epsilon)
static double rms_scale(const float *x, size_t width, double epsilon)
static void mix_depth_rows(size_t begin, size_t end, void *opaque)