/* 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;i1e-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=0 && at96 || 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]=tpw,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<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(pthreshold) {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; }