Skip to content

Qwen3-0.6B inference (12 KB), bare-metal bootable OS (22 KB), and from-scratch Mini-GPT trainer with live Braille TUI (32 KB) in 100% pure x86-64 NASM assembly. Zero C, zero libc, zero dependencies.

2.2 KB
Raw Blame
"""Symmetric depth-four gradient boosted trees with one shared split per level."""
import numpy as np

ABI=np.dtype([("feature","u1",(32,4)),("threshold","i1",(32,4)),
              ("leaves","<i2",(32,16)),("ntrees","<u4")])
# Additional head leaf banks use the same split planes as the deployed risk table.
SCALE=256


def sigmoid(v):
    return 1/(1+np.exp(-np.clip(v,-50,50)))


def leaf_ids(a,x,t):
    leaf=np.zeros(len(x),dtype=int)
    for d in range(4):
        leaf |= (x[:,a["feature"][t,d]]>a["threshold"][t,d]).astype(int)<<d
    return leaf


def fit(x,y):
    # Aggregate identical frozen connect vectors; this also gives duplicates equal weight.
    values, inverse, count=np.unique(x,axis=0,return_inverse=True,return_counts=True)
    target=np.stack([np.bincount(inverse,weights=head)/count for head in y.T],axis=1)
    candidates=[(i,int(v)) for i in range(32) for v in np.unique(values[:,i])[:-1]]
    planes=np.array([values[:,i]>v for i,v in candidates],dtype=np.int32)
    a=np.zeros((),dtype=ABI)
    a["ntrees"]=32
    leaves=np.zeros((32,16,4),np.int16)
    logits=np.zeros_like(target)
    for t in range(32):
        p=sigmoid(logits)
        grad=(target-p)*count[:,None]
        hess=(p*(1-p))*count[:,None]
        leaf=np.zeros(len(values),dtype=int)
        for d in range(4):
            best=-np.inf
            for j,plane in enumerate(planes):
                child=leaf+(plane<<d)
                g=np.stack([np.bincount(child,weights=v,minlength=16) for v in grad.T],axis=1)
                h=np.stack([np.bincount(child,weights=v,minlength=16) for v in hess.T],axis=1)
                score=(g**2/(h+2)).sum()
                if score>best:
                    best=score
                    chosen=j
            i,th=candidates[chosen]
            a["feature"][t,d]=i
            a["threshold"][t,d]=th
            leaf+=planes[chosen]<<d
        for k in range(4):
            g=np.bincount(leaf,weights=grad[:,k],minlength=16)
            h=np.bincount(leaf,weights=hess[:,k],minlength=16)
            leaves[t,:,k]=np.rint(SCALE*.25*g/(h+2))
        logits+=leaves[t,leaf]/SCALE
    a["leaves"]=leaves[:,:,0]
    return a,leaves


def predict(a,leaves,x):
    raw=sum(leaves[t,leaf_ids(a,x,t)].astype(np.int32) for t in range(32))
    return raw/SCALE