diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-09-13 20:03:39 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-09-13 20:13:28 +0200 |
| commit | 64c3a1b528a88b4ae21bf3cbcff2537200477806 (patch) | |
| tree | 85da00748d7bbb5036aeb5a7a4b60b5abf75aeea /src/weights.h | |
| download | pyseg-64c3a1b528a88b4ae21bf3cbcff2537200477806.tar.gz pyseg-64c3a1b528a88b4ae21bf3cbcff2537200477806.zip | |
Init
Diffstat (limited to 'src/weights.h')
| -rw-r--r-- | src/weights.h | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/weights.h b/src/weights.h new file mode 100644 index 0000000..f32cdfb --- /dev/null +++ b/src/weights.h @@ -0,0 +1,49 @@ +/* Access to the packed weight blob produced by tools/export_weights.py. + * + * The blob is already in the layout the kernels read, so loading it is a table + * walk that hands out pointers: no transposing, no unpacking, no allocation + * for the weights themselves. That holds both for the copy compiled into the + * binary and for a file, which is mapped read-only and shared between every + * process on the host that maps it. */ + +#ifndef PYSEG_WEIGHTS_H +#define PYSEG_WEIGHTS_H + +#include <stddef.h> + +#define PYSEG_ALIGN64 __attribute__((aligned(64))) + +/* Every weight matrix is [rows, window] row-major, matching pyseg_kernels. */ +typedef struct { + const float *ih; /* [512, input] */ + const float *hh; /* [512, 128] */ + const float *bias; /* [512], the two PyTorch bias vectors already summed */ +} pyseg_lstm_weights; + +typedef struct { + const float *wavnorm; /* [2]: InstanceNorm1d scale then shift */ + const float *sinc_w; /* [80, 256]: 251 taps then five zero columns */ + const float *norm_w[3]; /* [80], [60], [60] */ + const float *norm_b[3]; + const float *conv_w[2]; /* [60, 400] and [60, 300] */ + const float *conv_b[2]; + pyseg_lstm_weights lstm[4][2]; /* [layer][0 = forward, 1 = backward] */ + const float *lin_w[2]; /* [128, 256] and [128, 128] */ + const float *lin_b[2]; + const float *cls_w; /* [7, 128] */ + const float *cls_b; +} pyseg_weights; + +/* Returns 0 on success. verify recomputes the per-tensor checksum. */ +int pyseg_weights_parse(const void *blob, size_t len, int verify, pyseg_weights *out); + +/* Maps path read-only and parses it. On success the caller must keep the + * mapping alive for as long as the weights are used. */ +int pyseg_weights_map(const char *path, int verify, pyseg_weights *out, void **map, size_t *map_len); + +void pyseg_weights_unmap(void *map, size_t len); + +/* The blob compiled into this binary, or NULL when built without one. */ +const void *pyseg_weights_embedded(size_t *len); + +#endif /* PYSEG_WEIGHTS_H */ |
