File size: 8,060 Bytes
3f69d68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* x86_64 Linux only. Learned proposal, not a trainer or semantic validator.
 * Reuse reviewed bounded self-slot I/O. Legacy main/model are unreachable and
 * eliminated by --gc-sections; empty WEIGHTS_PATH satisfies its incbin only. */
#define main ln_unused_legacy_main
#include "nest.c"
#undef main
#if !defined(__linux__) || !defined(__x86_64__) || __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
#error "patch_nest requires little-endian x86_64 Linux"
#endif
#ifndef PATCH_WEIGHTS_PATH
#error "PATCH_WEIGHTS_PATH required"
#endif
__asm__(".pushsection .rodata\n.balign 4\n.global lp_w0\n.global lp_w1\nlp_w0:\n.incbin \"" STR(PATCH_WEIGHTS_PATH) "\"\nlp_w1:\n.popsection\n");
extern unsigned char lp_w0[], lp_w1[];
#define LP_COUNT 107994
/* Schema order is an ABI, mirrored by pack_patch.py. */
typedef struct { const float *ce,*c1w,*c1b,*c2w,*c2b,*cfw,*cfb,*be,*b1w,*b1b,*b2w,*b2b,*fw,*fb,*pw,*pb,*nw,*nb,*rw,*rb; float threshold; } PatchModel;
static const float *lp_take(const unsigned char **p, const unsigned char *end, size_t n) {
    if ((size_t)(end-*p) < n*4) die("truncated patch weights");
    const float *v=(const float *)*p;
    for(size_t i=0;i<n;i++) if(!isfinite(v[i])) die("nonfinite patch weights");
    *p+=n*4; return v;
}
static PatchModel lp_load(void) {
    const unsigned char *p=lp_w0,*end=lp_w1;
    if(end-p<32 || memcmp(p,"LNPATCH1",8)) die("missing patch weights"); p+=8;
    if(rd_u32(&p,end)!=1 || rd_u32(&p,end)!=96 || rd_u32(&p,end)!=256 || rd_u32(&p,end)!=LP_COUNT) die("unsupported patch weight schema");
    PatchModel m={0}; memcpy(&m.threshold,p,4);p+=4;
    if(!isfinite(m.threshold) || fabsf(m.threshold-.55f)>1e-6f || rd_u32(&p,end)!=0) die("unsupported confidence policy");
#define T(field,n) m.field=lp_take(&p,end,n)
    T(ce,257*24);T(c1w,48*24*5);T(c1b,48);T(c2w,48*48*5);T(c2b,48);
    T(cfw,64*288);T(cfb,64);T(be,257*24);T(b1w,64*24*9);T(b1b,64);
    T(b2w,64*64*9);T(b2b,64);T(fw,64*128);T(fb,64);T(pw,64);T(pb,1);
    T(nw,128);T(nb,1);T(rw,8*64);T(rb,8);
#undef T
    if(p!=end) die("unexpected patch weight bytes");return m;
}
/* Channel-major, PyTorch cross-correlation convention and exact GELU. */
static void lp_conv(const float *x,float *y,int length,int ni,int no,int kernel,const float *w,const float *bias) {
    for(int o=0;o<no;o++) for(int t=0;t<length;t++) {
        float s=bias[o];
        for(int i=0;i<ni;i++) for(int k=0;k<kernel;k++) {
            int at=t+k-kernel/2;
            if(at>=0 && at<length) s+=x[i*length+at]*w[(o*ni+i)*kernel+k];
        }
        y[o*length+t]=.5f*s*(1.f+erff(s*.7071067811865475f));
    }
}
static void lp_embed(const unsigned char *bytes,int n,int length,const float *w,float *out) {
    for(int i=0;i<length;i++) for(int c=0;c<24;c++) out[c*length+i]=w[(i<n ? (int)bytes[i]+1:0)*24+c];
}
static float lp_dot(const float *x,const float *w,int n,float b) { for(int i=0;i<n;i++) b+=x[i]*w[i];return b; }
typedef struct {int position, replacement, proposed; float confidence; const char *status;} PatchResult;
static PatchResult lp_predict(const PatchModel *m,const char *request,const unsigned char *bytes,int n) {
    size_t size=strlen(request);
    if(!size || size>96 || n<1 || n>256) die("expected 1..96 command bytes and 1..256 binary bytes");
    float ce[24*96],c1[48*96],c2[48*96],pooled[288],c[64];
    float be[24*256],b1[64*256],b2[64*256],h[256*64],joint[128],scores[257];
    lp_embed((const unsigned char*)request,(int)size,96,m->ce,ce);
    lp_conv(ce,c1,96,24,48,5,m->c1w,m->c1b);lp_conv(c1,c2,96,48,48,5,m->c2w,m->c2b);
    for(int k=0;k<48;k++) for(int bin=0;bin<6;bin++) {
        float v=-INFINITY; for(int t=bin*16;t<(bin+1)*16;t++) if(c2[k*96+t]>v) v=c2[k*96+t]; pooled[k*6+bin]=v;
    }
    for(int k=0;k<64;k++) c[k]=tanhf(lp_dot(pooled,m->cfw+k*288,288,m->cfb[k]));
    lp_embed(bytes,n,256,m->be,be);lp_conv(be,b1,256,24,64,9,m->b1w,m->b1b);lp_conv(b1,b2,256,64,64,9,m->b2w,m->b2b);
    for(int t=0;t<256;t++) {
        for(int k=0;k<64;k++) {joint[k]=b2[k*256+t];joint[k+64]=c[k];}
        for(int k=0;k<64;k++) h[t*64+k]=tanhf(lp_dot(joint,m->fw+k*128,128,m->fb[k]));
        scores[t]=t<n ? lp_dot(h+t*64,m->pw,64,m->pb[0]):-1e9f;
    }
    for(int k=0;k<64;k++) { float v=-INFINITY;for(int t=0;t<256;t++) if(b2[k*256+t]>v) v=b2[k*256+t]; joint[k]=v; joint[k+64]=c[k]; }
    scores[256]=lp_dot(joint,m->nw,128,m->nb[0]);
    int pos=0;for(int t=0;t<257;t++) {if(!isfinite(scores[t])) die("nonfinite inference");if(scores[t]>scores[pos]) pos=t;}
    float sum=0;for(int t=0;t<257;t++) sum+=expf(scores[t]-scores[pos]);
    PatchResult r={pos,0,0,1.f/sum,"none"};
    if(pos==256) return r;
    for(int bit=0;bit<8;bit++) {
        float logit=lp_dot(h+pos*64,m->rw+bit*64,64,m->rb[bit]);
        if(!isfinite(logit)) die("nonfinite inference");
        if(logit>=0) r.replacement |= 1<<bit;
    }
    if(r.confidence<m->threshold || pos>=n) {r.status="uncertain";return r;}
    for(int bit=0;bit<8;bit++) {float v=lp_dot(h+pos*64,m->rw+bit*64,64,m->rb[bit]);float p=1.f/(1.f+expf(-fabsf(v)));if(p<r.confidence)r.confidence=p;}
    if(r.confidence<m->threshold) {r.status="uncertain_byte";return r;}
    if(r.replacement==bytes[pos]) {r.status="no_change";return r;}
    r.proposed=1;r.status="unvalidated_candidate";return r;
}
/* Publish with link(2), not overwrite-capable rename: even a destination
 * created concurrently cannot be replaced. The reused copier stages privately. */
static void lp_emit(const char *src, const char *out, const unsigned char *bytes, int n) {
    struct stat s;
    if (lstat(out, &s) == 0 || errno != ENOENT)
        die("output must be a new path");
    size_t size = strlen(out);
    char *tmp = malloc(size + sizeof ".lppublish.XXXXXX");
    if (!tmp) die("out of memory");
    memcpy(tmp, out, size);
    strcpy(tmp + size, ".lppublish.XXXXXX");
    int fd = mkstemp(tmp);
    if (fd < 0) die("cannot stage candidate");
    if (close(fd)) {
        unlink(tmp);
        free(tmp);
        die("cannot close staging file");
    }
    stage_executable(src, tmp, "rv32", bytes, n, 1, 0);
    int failed = link(tmp, out);
    unlink(tmp);
    free(tmp);
    if (failed) die("candidate publish failed; destination was not replaced");
}

#include "responses_http.h"

int main(int argc, char **argv) {
    PatchModel model = lp_load(); /* Fail closed even for init/dump. */
    if (argc == 3 && !strcmp(argv[1], "--serve"))
        return rh_serve(&model, argv[2]);
    /* Open the running inode, even if its original pathname was replaced or
     * unlinked. Both read_self and stage_executable open this same image. */
    const char *src = "/proc/self/exe";
    char arch[16];
    unsigned char bytes[256];
    if (argc == 5 && !strcmp(argv[1], "--init") && !strcmp(argv[3], "-o")) {
        size_t n;
        unsigned char *raw = read_file(argv[2], &n, 256);
        if (!n || n % 4) die("seed must be nonempty aligned raw RV32");
        lp_emit(src, argv[4], raw, (int)n);
        free(raw);
        return 0;
    }
    int n = read_self(src, arch, bytes);
    if (!n || n % 4 || strcmp(arch, "rv32"))
        die("initialize a bounded raw RV32 slot first");
    if (argc == 2 && !strcmp(argv[1], "--dump-self")) {
        if (fwrite(bytes, 1, (size_t)n, stdout) != (size_t)n)
            die("stdout write failed");
        return 0;
    }
    int inspect = argc == 3 && !strcmp(argv[1], "--inspect");
    int propose = argc == 5 && !strcmp(argv[1], "--propose") && !strcmp(argv[3], "-o");
    if (!inspect && !propose)
        die("usage: --serve PORT | --init raw.rv32 -o new | --inspect 'request' | --propose 'request' -o new | --dump-self");
    PatchResult r = lp_predict(&model, argv[2], bytes, n);
    printf("{\"position\":%d,\"replacement\":%d,\"confidence\":%.9g,\"proposed\":%s,\"status\":\"%s\"}\n",
           r.position, r.replacement, r.confidence, r.proposed ? "true" : "false", r.status);
    if (propose && r.proposed) {
        bytes[r.position] = (unsigned char)r.replacement;
        lp_emit(src, argv[4], bytes, n);
    }
    return propose && !r.proposed ? 3 : 0;
}