DibaGuard / runtime /go /model.go
DibaAi's picture
DibaGuard: public licence-clean release (Dibachain)
5d97275 verified
Raw History Blame Contribute Delete
3.94 kB
// Package dibaguard is the Go reference runtime for DibaGuard tier-1 models: oblivious-tree ensembles (flows)
// and the hashed n-gram embedding-bag + MLP (HTTP). It reads the flat export written by
// dibaguard.parity.npz_to_flat (a .bin of little-endian arrays and a .bin.json header) and is proven against the
// Python runtime by the parity fixtures. No dependencies beyond the standard library.
package dibaguard
import (
"encoding/binary"
"encoding/json"
"fmt"
"math"
"os"
)
type arrayInfo struct {
Dtype string `json:"dtype"`
Shape []int `json:"shape"`
Offset int `json:"offset"`
NBytes int `json:"nbytes"`
}
// Flat is a loaded flat model file: raw bytes plus the header.
type Flat struct {
data []byte
arrays map[string]arrayInfo
Info map[string]any
}
// LoadFlat reads <path> and <path>.json.
func LoadFlat(path string) (*Flat, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
hdr, err := os.ReadFile(path + ".json")
if err != nil {
return nil, err
}
var raw map[string]json.RawMessage
if err := json.Unmarshal(hdr, &raw); err != nil {
return nil, err
}
f := &Flat{data: data, arrays: map[string]arrayInfo{}}
for k, v := range raw {
if k == "info" {
if err := json.Unmarshal(v, &f.Info); err != nil {
return nil, err
}
continue
}
var ai arrayInfo
if err := json.Unmarshal(v, &ai); err != nil {
return nil, err
}
f.arrays[k] = ai
}
return f, nil
}
func (f *Flat) bytesOf(name string) ([]byte, arrayInfo, error) {
ai, ok := f.arrays[name]
if !ok {
return nil, ai, fmt.Errorf("array %q missing", name)
}
return f.data[ai.Offset : ai.Offset+ai.NBytes], ai, nil
}
// Float32s returns the array as float32 regardless of its stored dtype (f4, f8, i1, i2, i4, i8).
func (f *Flat) Float32s(name string) ([]float32, []int, error) {
b, ai, err := f.bytesOf(name)
if err != nil {
return nil, nil, err
}
n := 1
for _, s := range ai.Shape {
n *= s
}
out := make([]float32, n)
switch ai.Dtype {
case "<f4":
for i := range out {
out[i] = math.Float32frombits(binary.LittleEndian.Uint32(b[4*i:]))
}
case "<f8":
for i := range out {
out[i] = float32(math.Float64frombits(binary.LittleEndian.Uint64(b[8*i:])))
}
case "|i1":
for i := range out {
out[i] = float32(int8(b[i]))
}
case "<i2":
for i := range out {
out[i] = float32(int16(binary.LittleEndian.Uint16(b[2*i:])))
}
case "<i4":
for i := range out {
out[i] = float32(int32(binary.LittleEndian.Uint32(b[4*i:])))
}
case "<i8":
for i := range out {
out[i] = float32(int64(binary.LittleEndian.Uint64(b[8*i:])))
}
default:
return nil, nil, fmt.Errorf("array %q: unsupported dtype %s", name, ai.Dtype)
}
return out, ai.Shape, nil
}
// Ints returns an integer array (i1/i2/i4/i8) as []int.
func (f *Flat) Ints(name string) ([]int, []int, error) {
b, ai, err := f.bytesOf(name)
if err != nil {
return nil, nil, err
}
n := 1
for _, s := range ai.Shape {
n *= s
}
out := make([]int, n)
switch ai.Dtype {
case "|i1":
for i := range out {
out[i] = int(int8(b[i]))
}
case "<i2":
for i := range out {
out[i] = int(int16(binary.LittleEndian.Uint16(b[2*i:])))
}
case "<i4":
for i := range out {
out[i] = int(int32(binary.LittleEndian.Uint32(b[4*i:])))
}
case "<i8":
for i := range out {
out[i] = int(int64(binary.LittleEndian.Uint64(b[8*i:])))
}
default:
return nil, nil, fmt.Errorf("array %q: not an integer dtype (%s)", name, ai.Dtype)
}
return out, ai.Shape, nil
}
// Int8s returns a |i1 array as []int8 (no copy conversion beyond the cast).
func (f *Flat) Int8s(name string) ([]int8, []int, error) {
b, ai, err := f.bytesOf(name)
if err != nil {
return nil, nil, err
}
if ai.Dtype != "|i1" {
return nil, nil, fmt.Errorf("array %q: dtype %s is not int8", name, ai.Dtype)
}
out := make([]int8, len(b))
for i := range b {
out[i] = int8(b[i])
}
return out, ai.Shape, nil
}