GigaAM-v4-c / src /ga_weights.c
eschmidbauer's picture
streaming runtime and conversion pipeline for GigaAM-v4
5b65ff7
Raw History Blame Contribute Delete
7.8 kB
#include "ga_weights.h"
#include "ga_half.h"
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#define GA_MAGIC "GIGAAMB1"
#define GA_VERSION 1u
enum { GA_F32 = 0, GA_F16 = 1, GA_I32 = 2, GA_U8 = 3, GA_I8 = 4 };
typedef struct ga_header {
char magic[8];
uint32_t version;
uint32_t count;
} ga_header;
void ga_weights_fail(ga_weights *w, const char *fmt, ...)
{
va_list args;
if (w->err[0])
return;
va_start(args, fmt);
vsnprintf(w->err, sizeof(w->err), fmt, args);
va_end(args);
}
int ga_weights_failed(const ga_weights *w)
{
return w->err[0] != 0;
}
#define GA_HUGE_PAGE ((size_t)2 << 20)
/* Large blocks are streamed every step: ask for huge pages where the OS offers them. */
static void *alloc_zeroed(size_t bytes)
{
#if defined(MADV_HUGEPAGE)
void *block = NULL;
if (bytes >= GA_HUGE_PAGE) {
size_t rounded = (bytes + GA_HUGE_PAGE - 1) / GA_HUGE_PAGE * GA_HUGE_PAGE;
if (posix_memalign(&block, GA_HUGE_PAGE, rounded) != 0)
return NULL;
madvise(block, rounded, MADV_HUGEPAGE);
memset(block, 0, bytes);
return block;
}
#endif
return calloc(1, bytes ? bytes : 1);
}
void *ga_weights_alloc(ga_weights *w, size_t bytes)
{
void *block = alloc_zeroed(bytes);
void **owned = block ? realloc(w->owned, (w->n_owned + 1) * sizeof(void *)) : NULL;
if (!owned) {
free(block);
ga_weights_fail(w, "out of memory");
return NULL;
}
w->owned = owned;
w->owned[w->n_owned++] = block;
return block;
}
const ga_entry *ga_weights_find(const ga_weights *w, const char *name)
{
for (uint32_t i = 0; i < w->count; i++)
if (strncmp(w->entries[i].name, name, sizeof(w->entries[i].name)) == 0)
return &w->entries[i];
return NULL;
}
static size_t entry_count(const ga_entry *e)
{
size_t count = 1;
for (uint32_t i = 0; i < e->ndim; i++)
count *= e->dims[i];
return count;
}
static const float *float_data(ga_weights *w, const ga_entry *e)
{
const uint8_t *data = (const uint8_t *)w->map + e->offset;
size_t count = entry_count(e);
float *wide;
if (e->dtype == GA_F32)
return (const float *)data;
if (e->dtype != GA_F16) {
ga_weights_fail(w, "%s: not a float tensor", e->name);
return NULL;
}
wide = ga_weights_alloc(w, count * sizeof(float));
if (!wide)
return NULL;
for (size_t i = 0; i < count; i++) {
uint16_t half;
memcpy(&half, data + 2 * i, sizeof(half));
wide[i] = ga_half_to_float(half);
}
return wide;
}
static const ga_entry *find_named(ga_weights *w, char *name, size_t size, const char *fmt,
va_list args)
{
const ga_entry *e;
vsnprintf(name, size, fmt, args);
e = ga_weights_find(w, name);
if (!e)
ga_weights_fail(w, "missing tensor %s", name);
return e;
}
static const ga_entry *find_shaped(ga_weights *w, int rows, int cols, const char *fmt,
va_list args)
{
char name[128];
const ga_entry *e = find_named(w, name, sizeof(name), fmt, args);
if (!e)
return NULL;
if (e->ndim != (cols ? 2u : 1u) || e->dims[0] != (uint32_t)rows ||
(cols && e->dims[1] != (uint32_t)cols)) {
ga_weights_fail(w, "%s: expected shape [%d,%d]", name, rows, cols);
return NULL;
}
return e;
}
const float *ga_weights_mat(ga_weights *w, int rows, int cols, const char *fmt, ...)
{
const ga_entry *e;
va_list args;
va_start(args, fmt);
e = find_shaped(w, rows, cols, fmt, args);
va_end(args);
return e ? float_data(w, e) : NULL;
}
const uint16_t *ga_weights_mat_f16(ga_weights *w, int rows, int cols, const char *fmt, ...)
{
const ga_entry *e;
va_list args;
va_start(args, fmt);
e = find_shaped(w, rows, cols, fmt, args);
va_end(args);
if (!e || e->dtype != GA_F16)
return NULL;
return (const uint16_t *)((const uint8_t *)w->map + e->offset);
}
const int8_t *ga_weights_mat_i8(ga_weights *w, int rows, int cols, const char *fmt, ...)
{
const ga_entry *e;
va_list args;
va_start(args, fmt);
e = find_shaped(w, rows, cols, fmt, args);
va_end(args);
if (!e || e->dtype != GA_I8)
return NULL;
return (const int8_t *)((const uint8_t *)w->map + e->offset);
}
const float *ga_weights_flat(ga_weights *w, size_t count, const char *fmt, ...)
{
char name[128];
const ga_entry *e;
va_list args;
va_start(args, fmt);
e = find_named(w, name, sizeof(name), fmt, args);
va_end(args);
if (!e)
return NULL;
if (entry_count(e) != count) {
ga_weights_fail(w, "%s: expected %zu elements", name, count);
return NULL;
}
return float_data(w, e);
}
const int32_t *ga_weights_i32(ga_weights *w, const char *name, int *count)
{
const ga_entry *e = ga_weights_find(w, name);
if (!e || e->dtype != GA_I32) {
ga_weights_fail(w, "missing int tensor %s", name);
*count = 0;
return NULL;
}
*count = (int)entry_count(e);
return (const int32_t *)((const uint8_t *)w->map + e->offset);
}
const uint8_t *ga_weights_u8(ga_weights *w, const char *name, size_t *count)
{
const ga_entry *e = ga_weights_find(w, name);
if (!e || e->dtype != GA_U8) {
ga_weights_fail(w, "missing byte tensor %s", name);
*count = 0;
return NULL;
}
*count = entry_count(e);
return (const uint8_t *)w->map + e->offset;
}
static int validate(ga_weights *w)
{
const ga_header *header = w->map;
static const size_t width[] = {4, 2, 4, 1, 1};
if (w->size < sizeof(*header) || memcmp(header->magic, GA_MAGIC, 8) != 0 ||
header->version != GA_VERSION) {
ga_weights_fail(w, "not a .gigaam v%u file", GA_VERSION);
return -1;
}
if ((w->size - sizeof(*header)) / sizeof(ga_entry) < header->count) {
ga_weights_fail(w, "truncated tensor table");
return -1;
}
w->count = header->count;
w->entries = (const ga_entry *)(header + 1);
for (uint32_t i = 0; i < w->count; i++) {
const ga_entry *e = &w->entries[i];
if (e->dtype > GA_I8 || e->ndim > 4 || e->name[sizeof(e->name) - 1] ||
e->offset > w->size || e->nbytes > w->size - e->offset ||
entry_count(e) * width[e->dtype] != e->nbytes) {
ga_weights_fail(w, "corrupt tensor entry %u", i);
return -1;
}
}
return 0;
}
int ga_weights_open(ga_weights *w, const char *path)
{
struct stat info;
int fd;
memset(w, 0, sizeof(*w));
fd = open(path, O_RDONLY);
if (fd < 0 || fstat(fd, &info) != 0) {
ga_weights_fail(w, "cannot open %s", path);
if (fd >= 0)
close(fd);
return -1;
}
w->size = (size_t)info.st_size;
w->map = mmap(NULL, w->size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
if (w->map == MAP_FAILED) {
w->map = NULL;
ga_weights_fail(w, "cannot map %s", path);
return -1;
}
w->mapped = 1;
return validate(w);
}
int ga_weights_open_memory(ga_weights *w, const void *data, size_t size)
{
memset(w, 0, sizeof(*w));
if (!data) {
ga_weights_fail(w, "no model data");
return -1;
}
w->map = (void *)data;
w->size = size;
return validate(w);
}
void ga_weights_close(ga_weights *w)
{
for (size_t i = 0; i < w->n_owned; i++)
free(w->owned[i]);
free(w->owned);
if (w->map && w->mapped)
munmap(w->map, w->size);
w->map = NULL;
w->owned = NULL;
w->n_owned = 0;
w->count = 0;
}