Scikit-learn
human-activity-recognition
wearable
wrist
time-series
cpu
scikit-learn
WISP / src /wisp_fast /forest_kernels.c
Zipeng365's picture
wisp-har 0.2.0: accelerated implementation with speed (exact/fast) and HMM sequence (subject/recording) options
ac7f7cb verified
Raw History Blame Contribute Delete
1.59 kB
#include <math.h>
#include <stdint.h>
#include <string.h>
/* Read the dtype/strides verified by forest.py; never assume an sklearn C struct layout.
The table contains pointers to non-owning array views pinned by the Python plan.
f32 inputs, f64 comparisons and tree-order f64 sums match sklearn's serial path. */
static intptr_t read_index(uintptr_t p) {
intptr_t out;
memcpy(&out, (const void *)p, sizeof out);
return out;
}
static double read_double(uintptr_t p) {
double out;
memcpy(&out, (const void *)p, sizeof out);
return out;
}
#ifdef _WIN32
__declspec(dllexport)
#endif
void wisp_forest(const float *x, intptr_t n, intptr_t d,
const uintptr_t *specs, intptr_t nt, intptr_t k, double *out) {
for (intptr_t t = 0; t < nt; ++t) {
const uintptr_t *tree = specs + 8 * t;
const uintptr_t stride = tree[6];
for (intptr_t i = 0; i < n; ++i) {
intptr_t node = 0, left;
uintptr_t off = 0;
while ((left = read_index(tree[0] + off)) != -1) {
const float val = x[i*d + read_index(tree[2]+off)];
const int go_left = isnan(val) ? *(const uint8_t *)(tree[5]+off)
: (double)val <= read_double(tree[3]+off);
node = go_left ? left : read_index(tree[1]+off);
off = (uintptr_t)node * stride;
}
const double *leaf = (const double *)(tree[4] + (uintptr_t)node * tree[7]);
for (intptr_t j = 0; j < k; ++j) out[i*k+j] += leaf[j];
}
}
}