Skip to content

Run frontier MoE models on hardware you already own β€” pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

feat(engine): add a logit-row status layer, evidence digests, and a checked ablation scoring mode

monotophicc3d080552fce
diff --git a/c/Makefile b/c/Makefile
index fe37e6bb..087bc29b 100644
--- a/c/Makefile
+++ b/c/Makefile
@@ -765,7 +765,7 @@ $(file >.build-config,$(BUILD_CONFIG))
endif
.build-config: ;
-colibri$(EXE): colibri.c oracle.h pin_pool.h cli_args.h st.h uring.h json.h tok.h tok_unicode.h compat.h grammar.h quant.h idot.h sample.h kv_persist.h telemetry.h route_trace.h omp_tune.h kv_fp8.h kv_tq.h abl.h backend_cuda.h backend_metal.h backend_vulkan.h decode_batch.h edge_adapters.h edge_runtime.h edge_tok_internal.h schema_gbnf.h segment_adapter_internal.h segment_adapters.h segment_runtime.h tier.h $(CUDA_OBJ) $(METAL_OBJ) $(VK_OBJ) $(VK_SPV) .build-config
+colibri$(EXE): colibri.c oracle.h pin_pool.h cli_args.h st.h uring.h json.h tok.h tok_unicode.h compat.h grammar.h quant.h idot.h sample.h kv_persist.h telemetry.h route_trace.h omp_tune.h kv_fp8.h kv_tq.h abl.h backend_cuda.h backend_metal.h backend_vulkan.h decode_batch.h edge_adapters.h edge_runtime.h edge_tok_internal.h evidence_digest.h schema_gbnf.h segment_adapter_internal.h segment_adapters.h segment_runtime.h tier.h $(CUDA_OBJ) $(METAL_OBJ) $(VK_OBJ) $(VK_SPV) .build-config
$(CC) $(CFLAGS) colibri.c $(CUDA_OBJ) $(METAL_OBJ) $(VK_OBJ) -o colibri$(EXE) $(LDFLAGS)
# Vulkan backend object (plain C + vulkan headers) and its SPIR-V shaders.
@@ -1404,6 +1404,20 @@ tests/test_rope_invfreq$(EXE): tests/test_rope_invfreq.c colibri.c oracle.h st.h
tests/test_grammar_cache$(EXE): tests/test_grammar_cache.c colibri.c oracle.h st.h uring.h json.h tok.h tok_unicode.h compat.h grammar.h tier.h quant.h idot.h sample.h kv_persist.h telemetry.h route_trace.h
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
+# The classified logit-row reduction in sample.h and the digest in
+# evidence_digest.h: no model/weights, published SHA-256 vectors, an
+# agreement check against the plain logprob_target the sampling path uses
+# on rows a float subtraction cannot mis-round, and a double-precision
+# check against a second double computation on rows that do.
+tests/test_logprob_status$(EXE): tests/test_logprob_status.c colibri.c oracle.h sample.h evidence_digest.h st.h uring.h json.h tok.h tok_unicode.h compat.h grammar.h tier.h quant.h kv_persist.h telemetry.h route_trace.h
+ $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
+
+# The ablation mode's manifest loader, evidence writer and dispatch contract.
+# The adapter build bypasses only the model computation, so the parser/writer
+# path is exercised for real with no model and no weights.
+tests/test_ablate_mode$(EXE): tests/test_ablate_mode.c colibri.c oracle.h sample.h evidence_digest.h abl.h st.h uring.h json.h tok.h tok_unicode.h compat.h grammar.h tier.h quant.h kv_persist.h telemetry.h route_trace.h
+ $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
+
# Standalone: drives a faithful miniature of moe()'s routing+accumulate and links
# the SAME abl.h the engine links -- no model/weights needed (the ablation-logic gate).
tests/test_ablate$(EXE): tests/test_ablate.c abl.h
diff --git a/c/colibri.c b/c/colibri.c
index a96ccba4..a5ac503a 100644
--- a/c/colibri.c
+++ b/c/colibri.c
@@ -24,6 +24,8 @@
#include <math.h>
#include <time.h>
#include <limits.h>
+#include <stdarg.h> /* the ablation writer forwards a format list */
+#include <inttypes.h> /* fixed-width parsing and printing in the ablation manifest */
#include <pthread.h> /* thread I/O del PILOTA */
#include <stdatomic.h> /* PIPE ready-flags/job queue + PILOT_REAL cross-layer handshake */
#include <sched.h> /* sched_yield: PIPE spin / PILOT barrier */
@@ -69,6 +71,7 @@
#include "tier.h"
#include "grammar.h" /* metodo F: draft grammaticali (#48) */
#include "abl.h" /* per-expert causal-ablation harness β€” inert unless g_abl.mode set (ABLATE_SCORE=<manifest>) */
+#include "evidence_digest.h" /* SHA-256 over the bytes an evidence mode consumed */
#include "schema_gbnf.h" /* SCHEMA=: JSON-Schema -> GBNF for method F */
#include "decode_batch.h"
#include "pin_pool.h" /* piu scatti annidati dello stato */
@@ -173,6 +176,7 @@ typedef struct {
int index_topk, index_nh, index_hd; /* DSA lightning indexer */
int8_t idx_type[128]; /* per layer: 1=full (calcola), 0=shared (riusa) */
float eps, theta, attn_scale, routed_scale;
+ char config_sha256[65]; /* digest of the loaded config.json bytes */
} Cfg;
/* tensore [O,I] in uno di tre formati:
@@ -1677,7 +1681,7 @@ static char* cfg_slurp(const char *path){
if((long)got!=n || memchr(b,'\0',got)){ free(b); return NULL; }
b[got]=0; return b;
}
-static jval* cfg_root(const char *snap, char **arena){
+static jval *cfg_root(const char *snap, char **arena, char config_sha256[65]){
char p[2048]; snprintf(p,sizeof(p),"%s/config.json",snap);
FILE *f=fopen(p,"rb"); if(!f){perror(p);exit(1);}
fseek(f,0,SEEK_END); long n=ftell(f); fseek(f,0,SEEK_SET);
@@ -1688,11 +1692,17 @@ static jval* cfg_root(const char *snap, char **arena){
char *b=malloc((size_t)n+1); if(!b){ fprintf(stderr,"OOM reading %s (%ld bytes)\n",p,n); exit(1); }
size_t got=fread(b,1,(size_t)n,f); b[got]=0; fclose(f);
if((long)got!=n) fprintf(stderr,"warning: short read on %s (%ld of %ld)\n",p,(long)got,n);
+ /* Bind the exact bytes that were loaded, for callers that record them. */
+ if(config_sha256) evidence_sha256_hex(b,got,config_sha256);
return json_parse(b,arena);
}
static int gi(jval*r,const char*k){ jval*v=json_get(r,k); return v?(int)v->num:0; }
static void load_cfg(Cfg *c, const char *snap){
- char *ar=NULL; jval *r=cfg_root(snap,&ar);
+ /* The digest costs a hash pass over config.json on every load; only the
+ * ABLATE_SCORE evidence path needs to name its config input, so it is the
+ * only caller that pays for it. c->config_sha256 stays unset otherwise. */
+ char *sha_out = getenv("ABLATE_SCORE") ? c->config_sha256 : NULL;
+ char *ar=NULL; jval *r=cfg_root(snap,&ar,sha_out);
c->hidden=gi(r,"hidden_size"); c->n_layers=gi(r,"num_hidden_layers");
c->n_heads=gi(r,"num_attention_heads"); c->n_experts=gi(r,"n_routed_experts");
c->topk=gi(r,"num_experts_per_tok"); c->moe_inter=gi(r,"moe_intermediate_size");
@@ -7975,7 +7985,7 @@ static void run_score(Model *m, const char *snap, const char *path){
* prefissato (eval_glm.py post-#194) passa INTATTO. SCORE_PREFIX=0 -> comportamento nudo. */
int pfx[2]={-1,-1}, pfx_on=0;
if(!getenv("SCORE_PREFIX")||atoi(getenv("SCORE_PREFIX"))){
- char *ar=NULL; jval *r=cfg_root(snap,&ar);
+ char *ar=NULL; jval *r=cfg_root(snap,&ar,NULL); /* the prefix probe needs no digest */
jval *mt=json_get(r,"model_type");
if(mt_is_glm(mt?mt->str:NULL)){
char tkp[2048]; snprintf(tkp,sizeof(tkp),"%s/tokenizer.json",snap);
@@ -8037,88 +8047,754 @@ static void run_score(Model *m, const char *snap, const char *path){
* ncells = # ablated cells (0 for baseline)
* L E A = layer, expert, swap-target (A=-1 unless mode 3), one triple per cell
* t_* = token ids (host pre-tokenised, prefix included if the model needs it)
- * abl_reset() before each item makes the ablation PER-ITEM (an item's spec can
+ * The whole manifest is strictly validated and SHA-256 bound before output;
+ * empty/malformed/duplicate-ID or zero-target inputs fail closed. abl_reset()
+ * before each item makes the ablation PER-ITEM (an item's spec can
* never leak into the next). NLL/margin/correctness are exact; top-K is for a
* paired approximate next-token KL on the host. */
#define ABL_LOGIT_TOPK 32
-static void run_ablate_score(Model *m, const char *path){
+/* Upper bound on one manifest item's declared token count. A teacher-forced
+ * ablation item is a prompt plus a short continuation; a million tokens is
+ * orders of magnitude above anything a study uses, and it keeps a hostile or
+ * corrupt manifest from asking the loader for an arbitrary allocation. */
+#define ABLATE_MAX_ITEM_TOKENS (1<<20)
+
+static int logit_topk_count(int V, int requested){
+ if(V<=0 || requested<=0) return 0;
+ int k=requested;
+ if(k>ABL_LOGIT_TOPK) k=ABL_LOGIT_TOPK;
+ if(k>V) k=V;
+ return k;
+}
+
+static void logprob_refusal(const char *surface, unsigned long long owner,
+ int64_t position, const char *field,
+ LogprobStatus status){
+ fprintf(stderr,
+ "[numeric] INCOMPLETE: surface=%s owner=%llu position=%" PRId64
+ " field=%s class=%s\n",
+ surface,owner,position,field,logprob_status_name(status));
+}
+
+/* For finite rows, select real vocabulary entries without a numeric sentinel.
+ * Filling empty slots in encounter order and replacing only on strict greater-
+ * than preserves the historical first-minimum-SLOT behavior and unsorted slot
+ * order. This is deliberately not a lowest-token-id tie rule: [0,0,1] at
+ * k=2 emits token ids [2,1].
+ *
+ * Exceptional rows are rejected by the classified numeric-status layer before
+ * this finite-only selector is reached. */
+static int logit_topk_select(const float *lo, int V, int requested,
+ int *tk_id, float *tk_val,
+ int *status_out){
+ if(status_out) *status_out=0;
+ if(!lo || !tk_id || !tk_val || V<=0) return 0;
+ int k=logit_topk_count(V,requested);
+ if(k==0) return 0;
+ int finite=1;
+ for(int i=0;i<V;i++) if(!isfinite(lo[i])){ finite=0; break; }
+ if(!finite){
+ LogprobRow row;
+ LogprobStatus status=logprob_row_checked(lo,V,&row);
+ if(status_out) *status_out=(int)status;
+ return 0;
+ }
+
+ for(int j=0;j<k;j++) tk_id[j]=-1;
+ for(int i=0;i<V;i++){
+ int slot=-1;
+ for(int j=0;j<k;j++) if(tk_id[j]<0){ slot=j; break; }
+ if(slot<0){
+ int mn=0;
+ for(int j=1;j<k;j++) if(tk_val[j]<tk_val[mn]) mn=j;
+ if(!(lo[i]>tk_val[mn])) continue;
+ slot=mn;
+ }
+ tk_id[slot]=i;
+ tk_val[slot]=lo[i];
+ }
+ return k;
+}
+
+typedef struct {
+ char digest[65];
+ char config_sha256[65];
+ int64_t items;
+ int64_t targets;
+ int maxT;
+ int n_layers;
+ int first_dense;
+ int n_experts;
+} AblateManifestInfo;
+
+/* Parse one canonical signed-64 decimal, independent of host ``long`` width.
+ * The ABLATE manifest is intentionally narrower than generic strtoimax input:
+ * no plus, leading zero, -0, tabs, CR, or trailing whitespace. A canonical
+ * byte stream then has one stable digest. */
+static int ablate_manifest_i64(const char **cursor, const char *end,
+ int64_t *out){
+ const char *p=*cursor;
+ if(p>=end) return -1;
+ int negative=(*p=='-');
+ if(negative && ++p>=end) return -1;
+ if(*p<'0' || *p>'9') return -1;
+ if(*p=='0' && p+1<end && p[1]>='0' && p[1]<='9') return -1;
+ if(negative && *p=='0') return -1;
+ errno=0; char *after=NULL;
+ intmax_t value=strtoimax(*cursor,&after,10);
+ if(errno==ERANGE || after==*cursor || after>end ||
+ value<INT64_MIN || value>INT64_MAX) return -1;
+ *cursor=after; *out=(int64_t)value;
+ return 0;
+}
+
+static int ablate_manifest_space(const char **cursor, const char *end){
+ if(*cursor>=end || **cursor!=' ') return -1;
+ (*cursor)++;
+ return 0;
+}
+
+/* One manifest item id together with the line it was read from. The duplicate
+ * check sorts these, so the line has to travel with the id: reporting the last
+ * line of the file instead would point a reader at the wrong record. */
+typedef struct {
+ int64_t item;
+ int64_t line;
+} AblateItemRef;
+
+static int ablate_item_ref_compare(const void *av, const void *bv){
+ const AblateItemRef *a=(const AblateItemRef *)av, *b=(const AblateItemRef *)bv;
+ if(a->item!=b->item) return (a->item>b->item)-(a->item<b->item);
+ return (a->line>b->line)-(a->line<b->line);
+}
+
+static int ablate_count_add(int64_t *total, int64_t increment){
+ if(!total || *total<0 || increment<0 || *total>INT64_MAX-increment)
+ return -1;
+ *total+=increment;
+ return 0;
+}
+
+typedef struct {
+ int64_t item;
+ int T;
+ int n_prompt;
+ int mode;
+ int ncells;
+ int layers[ABL_MAX_CELLS];
+ int experts[ABL_MAX_CELLS];
+ int applied[ABL_MAX_CELLS];
+ int *tokens;
+} AblateManifestItem;
+
+typedef struct {
+ AblateManifestInfo info;
+ AblateManifestItem *items;
+ size_t count;
+} AblateManifest;
+
+static int evidence_sha256_text_valid(const char *text){
+ if(!text) return 0;
+ for(int i=0;i<64;i++)
+ if(!((text[i]>='0' && text[i]<='9') ||
+ (text[i]>='a' && text[i]<='f'))) return 0;
+ return text[64]==0;
+}
+
+static void ablate_manifest_free(AblateManifest *manifest){
+ if(!manifest) return;
+ for(size_t i=0;i<manifest->count;i++) free(manifest->items[i].tokens);
+ free(manifest->items);
+ memset(manifest,0,sizeof(*manifest));
+}
+
+/* Parse and freeze the complete positive denominator before any output or
+ * model work. The digest and the owned parsed rows come from these same bytes;
+ * execution never rereads the caller-owned FILE. */
+/* Name the precondition that failed, or NULL when they all hold. A refusal
+ * with no reason on stderr is indistinguishable from a crash to whoever runs
+ * the mode, so every caller of this prints what it returns. */
+static const char *ablate_precondition_reason(FILE *f, const Cfg *c){
+ if(!f) return "no manifest stream";
+ if(!c) return "no loaded model configuration";
+ if(c->vocab<=0 || c->vocab>(1<<24)) return "vocabulary size out of range (1..16777216)";
+ if(c->n_layers<=0 || c->n_layers>128) return "layer count out of range (1..128)";
+ if(c->first_dense<0 || c->first_dense>c->n_layers)
+ return "first routed layer out of range (0..layer count)";
+ if(c->n_experts<=0 || c->n_experts>4096) return "expert count out of range (1..4096)";
+ if(!evidence_sha256_text_valid(c->config_sha256))
+ return "the loaded config.json was not digested, so evidence could not name it";
+ return NULL;
+}
+
+static int ablate_manifest_load(FILE *f, const Cfg *c,
+ AblateManifest *manifest){
+ if(!manifest){
+ fprintf(stderr,"[ablate] INCOMPLETE: no manifest destination\n");
+ return 1;
+ }
+ memset(manifest,0,sizeof(*manifest));
+ const char *reason=ablate_precondition_reason(f,c);
+ if(reason){
+ fprintf(stderr,"[ablate] INCOMPLETE: %s\n",reason);
+ return 1;
+ }
+ int V=c->vocab;
+ clearerr(f); rewind(f);
+ EvidenceSha256 hash;
+ evidence_sha256_init(&hash);
+ static const char domain[]="coli-ablate-manifest/2\n";
+ evidence_sha256_update(&hash,domain,sizeof(domain)-1);
+ AblateItemRef *item_ids=NULL;
+ size_t item_cap=0, item_count=0, row_cap=0;
+ int64_t target_count=0;
+ int maxT=1, rc=0;
+ char *line=NULL; size_t cap=0; ssize_t length; int64_t line_no=0, bad_line=0;
+ int named=0; /* a specific reason was already printed */
+ while((length=getline(&line,&cap,f))>0){
+ line_no++;
+ /* Accept the two framings a host editor produces without meaning to:
+ * a CRLF line ending, and a last line with no terminator at all. The
+ * record is normalised to its bare text here and the digest below is
+ * taken over the normalised bytes, so the same manifest content always
+ * produces the same digest however it was saved. Anything else -- a
+ * stray carriage return inside a record, an embedded NUL, an empty
+ * line -- is still refused. */
+ size_t len=(size_t)length;
+ if(line[len-1]=='\n') len--;
+ if(len && line[len-1]=='\r') len--;
+ if(len==0 || memchr(line,'\r',len) || memchr(line,'\0',len)){
+ rc=1; break;
+ }
+ const char *record_end=line+len, *p=line;
+ int64_t item,T,np,mode,nc;
+ if(ablate_manifest_i64(&p,record_end,&item)<0 ||
+ ablate_manifest_space(&p,record_end)<0 ||
+ ablate_manifest_i64(&p,record_end,&T)<0 ||
+ ablate_manifest_space(&p,record_end)<0 ||
+ ablate_manifest_i64(&p,record_end,&np)<0 ||
+ ablate_manifest_space(&p,record_end)<0 ||
+ ablate_manifest_i64(&p,record_end,&mode)<0 ||
+ ablate_manifest_space(&p,record_end)<0 ||
+ ablate_manifest_i64(&p,record_end,&nc)<0){
+ rc=1; break;
+ }
+ /* A manifest is host input: an item may not ask for an unbounded
+ * allocation by declaring an enormous length. The cap is far above any
+ * teacher-forced item a study runs -- the engine also allocates
+ * maxT * hidden floats for the prefill -- and it bounds the token array
+ * this loop is about to allocate to a few megabytes. */
+ if(T>ABLATE_MAX_ITEM_TOKENS){
+ fprintf(stderr,"[ablate] INCOMPLETE: line %" PRId64 " declares %" PRId64
+ " tokens, above the %d-token limit for one item\n",
+ line_no,T,ABLATE_MAX_ITEM_TOKENS);
+ named=1; rc=1; bad_line=line_no; break;
+ }
+ if(item<0 || T<2 || T>INT_MAX || np<1 || np>=T ||
+ mode<0 || mode>3 || nc<0 || nc>ABL_MAX_CELLS ||
+ (mode==0 ? nc!=0 : nc==0)){
+ rc=1; break;
+ }
+ AblateManifestItem row={0};
+ row.item=item; row.T=(int)T; row.n_prompt=(int)np;
+ row.mode=(int)mode; row.ncells=(int)nc;
+ for(int64_t i=0;i<nc;i++){
+ int64_t L,E,A;
+ if(ablate_manifest_space(&p,record_end)<0 ||
+ ablate_manifest_i64(&p,record_end,&L)<0 ||
+ ablate_manifest_space(&p,record_end)<0 ||
+ ablate_manifest_i64(&p,record_end,&E)<0 ||
+ ablate_manifest_space(&p,record_end)<0 ||
+ ablate_manifest_i64(&p,record_end,&A)<0 ||
+ L<c->first_dense || L>=c->n_layers ||
+ E<0 || E>=c->n_experts ||
+ A<INT_MIN || A>INT_MAX ||
+ (mode==3 ? (A<0 || A>=c->n_experts || A==E) : A!=-1)){
+ rc=1; break;
+ }
+ for(int64_t j=0;j<i;j++)
+ if(row.layers[j]==(int)L && row.experts[j]==(int)E){
+ rc=1; break;
+ }
+ if(rc) break;
+ row.layers[i]=(int)L; row.experts[i]=(int)E;
+ row.applied[i]=(int)A;
+ }
+ if(!rc && (uintmax_t)T>(uintmax_t)(SIZE_MAX/sizeof(*row.tokens))) rc=1;
+ if(!rc){ row.tokens=malloc((size_t)T*sizeof(*row.tokens)); if(!row.tokens) rc=1; }
+ for(int64_t i=0;i<T && !rc;i++){
+ int64_t token;
+ if(ablate_manifest_space(&p,record_end)<0 ||
+ ablate_manifest_i64(&p,record_end,&token)<0 ||
+ token<0 || token>=V) rc=1;
+ else row.tokens[i]=(int)token;
+ }
+ if(rc || p!=record_end) { free(row.tokens); rc=1; break; }
+ if(item_count==item_cap){
+ size_t next=item_cap ? item_cap*2 : 32;
+ if(next<item_cap || next>SIZE_MAX/sizeof(*item_ids)){
+ free(row.tokens); rc=1; break;
+ }
+ AblateItemRef *grown=realloc(item_ids,next*sizeof(*item_ids));
+ if(!grown){ free(row.tokens); rc=1; break; }
+ item_ids=grown; item_cap=next;
+ }
+ if(item_count==row_cap){
+ size_t next=row_cap ? row_cap*2 : 32;
+ if(next<row_cap || next>SIZE_MAX/sizeof(*manifest->items)){
+ free(row.tokens); rc=1; break;
+ }
+ AblateManifestItem *grown=realloc(
+ manifest->items,next*sizeof(*manifest->items));
+ if(!grown){ free(row.tokens); rc=1; break; }
+ manifest->items=grown; row_cap=next;
+ }
+ item_ids[item_count]=(AblateItemRef){item,line_no};
+ item_count++;
+ manifest->items[manifest->count++]=row;
+ int64_t row_targets=T-np;
+ if(ablate_count_add(&target_count,row_targets)<0){ rc=1; break; }
+ if(T>maxT) maxT=(int)T;
+ evidence_sha256_update(&hash,line,len);
+ evidence_sha256_update(&hash,"\n",1);
+ }
+ if(ferror(f) || item_count==0 || target_count<=0 ||
+ (uintmax_t)item_count>(uintmax_t)INT64_MAX) rc=1;
+ if(!rc){
+ qsort(item_ids,item_count,sizeof(*item_ids),ablate_item_ref_compare);
+ for(size_t i=1;i<item_count;i++)
+ if(item_ids[i].item==item_ids[i-1].item){
+ /* Sorted by id then line, so this entry is the later of the
+ * two: the record that repeats an id already used. */
+ bad_line=item_ids[i].line;
+ rc=1; break;
+ }
+ }
+ if(rc && !named){
+ int64_t at=bad_line ? bad_line : (line_no ? line_no : 1);
+ fprintf(stderr,"[ablate] invalid canonical manifest at line %" PRId64 "\n",at);
+ }
+ if(!rc){
+ unsigned char raw[32]; static const char hex[]="0123456789abcdef";
+ evidence_sha256_final(&hash,raw);
+ for(int i=0;i<32;i++){
+ manifest->info.digest[2*i]=hex[raw[i]>>4];
+ manifest->info.digest[2*i+1]=hex[raw[i]&15];
+ }
+ manifest->info.digest[64]=0;
+ memcpy(manifest->info.config_sha256,c->config_sha256,65);
+ manifest->info.items=(int64_t)item_count;
+ manifest->info.targets=target_count;
+ manifest->info.maxT=maxT;
+ manifest->info.n_layers=c->n_layers;
+ manifest->info.first_dense=c->first_dense;
+ manifest->info.n_experts=c->n_experts;
+ }
+ free(line); free(item_ids); clearerr(f); rewind(f);
+ if(rc) ablate_manifest_free(manifest);
+ return rc;
+}
+
+/* The writer used to be a small vtable (ctx + vprintf/puts/flush/close
+ * function pointers) so a test double could stand in for a real file. It
+ * only ever had one implementation -- a plain FILE* -- so the indirection
+ * bought nothing; a test that wants an isolated destination opens a real
+ * tmpfile and passes it in like any other caller. */
+static int ablate_writer_printf(FILE *f, const char *fmt, ...){
+ va_list ap; va_start(ap,fmt);
+ int n=vfprintf(f,fmt,ap);
+ va_end(ap);
+ return n;
+}
+
+static int ablate_writer_puts(FILE *f, const char *text){
+ return fputs(text,f)==EOF ? -1 : 0;
+}
+
+static FILE *ablate_writer_open(const char *path){
+ /* Evidence output is new-file-only. O_EXCL makes the path-name decision
+ * atomic: an exact/normalised source path, hard link, symbolic link, or any
+ * other existing destination is refused before a byte can be truncated.
+ * The caller keeps the already-open manifest descriptor live through this
+ * operation, so a newly created inode cannot identify that source. */
+ int fd=open(path,O_WRONLY|O_CREAT|O_EXCL|COMPAT_O_BINARY,0666);
+ if(fd<0) return NULL;
+ FILE *f=fdopen(fd,"wb");
+ if(!f){
+ int saved=errno;
+ close(fd);
+ errno=saved;
+ return NULL;
+ }
+ return f;
+}
+
+/* Always attempt close even if flush failed, and preserve either failure. */
+static int ablate_writer_finish(FILE *f){
+ if(!f) return -1;
+ int bad=(fflush(f)!=0);
+ if(fclose(f)!=0) bad=1;
+ return bad ? -1 : 0;
+}
+
+typedef int (*AblateOutputOpenFn)(void *ctx, FILE **w,
+ const char *path);
+typedef int (*AblateOutputBodyFn)(void *ctx, FILE *of);
+typedef struct {
+ void *ctx;
+ AblateOutputOpenFn open_fn;
+ AblateOutputBodyFn body_fn;
+ const AblateManifestInfo *manifest;
+ int64_t *completed_items;
+ int64_t *completed_targets;
+} AblateOutputRun;
+
+static int ablate_file_open_run(void *ctx, FILE **w,
+ const char *path){
+ (void)ctx;
+ *w=ablate_writer_open(path);
+ return *w ? 0 : -1;
+}
+
+static int ablate_header_line(FILE *of, int V,
+ const AblateManifestInfo *manifest){
+ if(!manifest) return -1;
+ int topk=logit_topk_count(V,ABL_LOGIT_TOPK);
+ return ablate_writer_printf(of,
+ "{\"t\":\"hdr\",\"schema\":\"coli-ablate/2\",\"vocab\":%d,\"topk\":%d,"
+ "\"n_layers\":%d,\"first_dense\":%d,\"n_experts\":%d,"
+ "\"config_sha256\":\"%s\","
+ "\"manifest_sha256\":\"%s\",\"expected_items\":%" PRId64
+ ",\"expected_targets\":%" PRId64 "}\n",
+ V,topk,manifest->n_layers,manifest->first_dense,
+ manifest->n_experts,manifest->config_sha256,manifest->digest,
+ manifest->items,manifest->targets)<0 ? -1 : 0;
+}
+
+static int ablate_done_line(FILE *of,
+ const AblateManifestInfo *manifest,
+ int64_t completed_items,
+ int64_t completed_targets){
+ if(!manifest) return -1;
+ return ablate_writer_printf(of,
+ "{\"t\":\"done\",\"manifest_sha256\":\"%s\","
+ "\"completed_items\":%" PRId64
+ ",\"completed_targets\":%" PRId64 "}\n",
+ manifest->digest,completed_items,completed_targets)<0 ? -1 : 0;
+}
+
+/* Requested-output orchestration shared by the real ABLATE driver and its
+ * model-free caller-chain gate. A missing ABLATE_OUT remains optional; once
+ * a path is requested, every open/header/body/finalize failure is a failed
+ * mode result. Finalize always attempts close after flush. */
+static int ablate_output_run(AblateOutputRun *run, const char *outp, int V){
+ if(!run || !run->body_fn || !run->manifest ||
+ !run->completed_items || !run->completed_targets ||
+ run->manifest->items<=0 || run->manifest->targets<=0){
+ fprintf(stderr,"[ablate] INCOMPLETE: nothing to run "
+ "(the manifest produced no items or targets)\n");
+ return 1;
+ }
+ *run->completed_items=0; *run->completed_targets=0;
+ FILE *writer=NULL, *of=NULL;
+ int rc=0;
+ if(outp){
+ if(!run->open_fn || run->open_fn(run->ctx,&writer,outp)<0){
+ fprintf(stderr,
+ "[ablate] INCOMPLETE: ABLATE_OUT must be a new file: %s\n",
+ outp);
+ return 1;
+ }
+ of=writer;
+ if(ablate_header_line(of,V,run->manifest)<0){
+ fprintf(stderr,"[ablate] cannot write requested ABLATE_OUT=%s\n",outp);
+ rc=1;
+ }
+ }
+ if(!rc && run->body_fn(run->ctx,of)!=0) rc=1;
+ if(!rc && (*run->completed_items!=run->manifest->items ||
+ *run->completed_targets!=run->manifest->targets)){
+ fprintf(stderr,"[ablate] completion denominator mismatch: "
+ "%" PRId64 "/%" PRId64 " items, "
+ "%" PRId64 "/%" PRId64 " targets\n",
+ *run->completed_items,run->manifest->items,
+ *run->completed_targets,run->manifest->targets);
+ rc=1;
+ }
+ if(!rc && of && ablate_done_line(of,run->manifest,
+ *run->completed_items,*run->completed_targets)<0) rc=1;
+ if(of && ablate_writer_finish(of)<0) rc=1;
+ if(rc && outp)
+ fprintf(stderr,"[ablate] requested ABLATE_OUT=%s is incomplete\n",outp);
+ return rc;
+}
+
+static int ablate_item_line(FILE *of, int64_t item, int64_t mode,
+ int64_t nc, int64_t T, int64_t np,
+ const int *Ls, const int *Es,
+ const int *As){
+ if(ablate_writer_printf(of,
+ "{\"t\":\"ah\",\"item\":%" PRId64
+ ",\"mode\":%" PRId64 ",\"ncells\":%" PRId64
+ ",\"T\":%" PRId64 ",\"n_prompt\":%" PRId64
+ ",\"cells\":[",
+ item,mode,nc,T,np)<0) return -1;
+ for(int i=0;i<(int)nc;i++)
+ if(ablate_writer_printf(of,"%s[%d,%d,%d]",i?",":"",Ls[i],Es[i],As[i])<0)
+ return -1;
+ return ablate_writer_puts(of,"]}\n");
+}
+
+/* Keep the ABLATE numeric schema in one callable formatter so the exact
+ * production emission can be round-trip tested without loading a model.
+ * Raw diagnostic logits retain their existing compact precision; nll, logZ,
+ * and the requested top-k numeric fields are the precision-corrected sites. */
+static int ablate_logit_line(FILE *of, int64_t item, int64_t pos,
+ int gold,
+ double nll, float glogit, float molo, float mgn,
+ int am, float amlogit, double logZ, int corr,
+ int topk, const int *tk_id, const float *tk_val){
+ int n=ablate_writer_printf(of,"{\"t\":\"lg\",\"item\":%" PRId64
+ ",\"pos\":%" PRId64 ",\"gold\":%d,\"nll\":%.17g,"
+ "\"glogit\":%.6g,\"molo\":%.6g,\"mgn\":%.6g,\"am\":%d,\"amlogit\":%.6g,"
+ "\"logZ\":%.17g,\"corr\":%d,\"tk\":[",
+ item,pos,gold,nll,(double)glogit,(double)molo,(double)mgn,
+ am,(double)amlogit,logZ,corr);
+ if(n<0) return -1;
+ for(int k=0;k<topk;k++)
+ if(ablate_writer_printf(of,"%s[%d,%.17g]",k?",":"",tk_id[k],(double)tk_val[k])<0) return -1;
+ return ablate_writer_puts(of,"]}\n");
+}
+
+/* This is the actual production row-calculation/selection/format seam used by
+ * run_ablate_score and by the model-free formula gate. */
+static int ablate_logit_record(FILE *of, int64_t item, int64_t pos,
+ int gold,
+ const float *lo, int V){
+ if(!of) return 0; /* ABLATE_OUT was not requested: no work */
+ if(!lo || V<=0 || gold<0 || gold>=V){
+ fprintf(stderr,"[ablate] INCOMPLETE: item %" PRId64 " position %" PRId64
+ " has no readable logit row\n",item,pos);
+ return -1;
+ }
+ LogprobRow lpr;
+ LogprobStatus status=logprob_row_checked(lo,V,&lpr);
+ if(status!=LOGPROB_FINITE){
+ logprob_refusal("ABLATE",(unsigned long long)item,pos,"row",status);
+ return -1;
+ }
+ float mx=lpr.max;
+ double target_lp=0;
+ status=logprob_from_row_checked(lo,gold,&lpr,&target_lp);
+ if(status!=LOGPROB_FINITE){
+ logprob_refusal("ABLATE",(unsigned long long)item,pos,"target",status);
+ return -1;
+ }
+ /* The classified read above decides whether this position is reportable at
+ * all. The reported value itself is the subtraction done wholly in double,
+ * which is what this mode has always written: taking the difference in
+ * float first would round away up to an ulp of the largest logit, and on a
+ * widely spread row that is a visible amount. */
+ double gnll=lpr.logZ-(double)lo[gold];
+ if(!isfinite(gnll)){
+ logprob_refusal("ABLATE",(unsigned long long)item,pos,"nll",
+ LOGPROB_FINITE_OVERFLOW);
+ return -1;
+ }
+ /* V==1 has no competitor and retains the historical finite sentinel.
+ * For every real competitor set, seed from an actual non-gold logit so a
+ * finite value below -1e30 cannot be hidden and margin overflow is refused. */
+ float molo=-1e30f;
+ if(V>=2){
+ int competitor=gold==0?1:0;
+ molo=lo[competitor];
+ for(int i=0;i<V;i++) if(i!=gold && lo[i]>molo) molo=lo[i];
+ }
+ float mgn=lo[gold]-molo;
+ if(!isfinite(mgn)){
+ logprob_refusal("ABLATE",(unsigned long long)item,pos,"margin",
+ LOGPROB_FINITE_OVERFLOW);
+ return -1;
+ }
+ int tk_id[ABL_LOGIT_TOPK]; float tk_val[ABL_LOGIT_TOPK];
+ int topk_status=0;
+ int topk=logit_topk_select(lo,V,ABL_LOGIT_TOPK,tk_id,tk_val,&topk_status);
+ if(topk!=logit_topk_count(V,ABL_LOGIT_TOPK)){
+ logprob_refusal("ABLATE",(unsigned long long)item,pos,"topk",
+ topk_status ? (LogprobStatus)topk_status : LOGPROB_INVALID);
+ return -1;
+ }
+ return ablate_logit_line(of,item,pos,gold,gnll,lo[gold],molo,mgn,
+ lpr.argmax,mx,lpr.logZ,lpr.argmax==gold,
+ topk,tk_id,tk_val);
+}
+
+typedef int (*AblateRowOutputFn)(void *ctx, FILE *of, int64_t pos);
+
+/* One item's actual row-caller and per-item flush chain. With no requested
+ * output it performs no logprob work, matching the historical optional path. */
+static int ablate_item_output(FILE *of, int64_t first, int64_t end,
+ AblateRowOutputFn row_fn, void *ctx){
+ if(!of) return 0;
+ if(!row_fn){
+ fprintf(stderr,"[ablate] INCOMPLETE: no row reader for the requested output\n");
+ return 1;
+ }
+ for(int64_t pos=first;pos<end;pos++)
+ if(row_fn(ctx,of,pos)<0) return 1;
+ return fflush(of)==0 ? 0 : 1;
+}
+
+typedef struct {
+ Model *m;
+ float *x;
+ float *row;
+ float *lo;
+ const int *ids;
+ int64_t item;
+ int D;
+ int V;
+} AblateModelRows;
+
+#ifdef COLI_TEST_ABLATE_ADAPTERS
+/* Compile-only state for the model-free adapter gate. The product build has
+ * no runtime test mode: the gate borrows a manifest, bypasses only model math,
+ * and enters the same production parser/writer/status adapters. */
+typedef struct {
+ FILE *borrowed_manifest;
+ void (*after_manifest_load)(FILE *manifest);
+ int bypass_model_compute;
+ int force_body_rc;
+ int override_outp;
+ const char *outp;
+ int observed_first_token;
+ const float *forced_row; /* NULL: use the default -i row */
+} AblateAdapterTestState;
+static AblateAdapterTestState g_ablate_adapter_test;
+#define ABLATE_MODEL_COMPUTE_ENABLED (!g_ablate_adapter_test.bypass_model_compute)
+#else
+#define ABLATE_MODEL_COMPUTE_ENABLED 1
+#endif
+
+static int ablate_model_row_output(void *ctx, FILE *of, int64_t pos){
+ AblateModelRows *rows=(AblateModelRows *)ctx;
+ Model *m=rows->m; Cfg *c=&m->c;
+ if(ABLATE_MODEL_COMPUTE_ENABLED){
+ rmsnorm(rows->row, rows->x+(int64_t)pos*rows->D,
+ m->final_norm, rows->D, c->eps);
+ matmul_qt(rows->lo, rows->row, &m->lm_head, 1);
+ }
+ return ablate_logit_record(of,rows->item,pos,rows->ids[pos+1],
+ rows->lo,rows->V);
+}
+
+typedef struct {
+ Model *m;
+ const AblateManifest *manifest;
+ int64_t completed_items;
+ int64_t completed_targets;
+} AblateModelRun;
+
+static int ablate_model_output_body(void *ctx, FILE *of){
+ AblateModelRun *run=(AblateModelRun *)ctx;
+ Model *m=run->m;
+ if(!run->manifest || run->manifest->count==0){
+ fprintf(stderr,"[ablate] INCOMPLETE: the manifest holds no items\n");
+ return 1;
+ }
Cfg *c=&m->c; int D=c->hidden, V=c->vocab;
- FILE *f=fopen(path,"rb"); if(!f){perror(path);exit(1);}
- const char *outp=getenv("ABLATE_OUT");
- FILE *of = outp ? fopen(outp,"wb") : NULL;
- if(outp && !of){ fprintf(stderr,"[ablate] cannot open ABLATE_OUT=%s\n",outp); }
- if(of) fprintf(of,"{\"t\":\"hdr\",\"schema\":\"coli-ablate/1\",\"vocab\":%d,\"topk\":%d}\n",V,ABL_LOGIT_TOPK);
- int maxT=1; { char *ln=NULL; size_t cp=0;
- while(getline(&ln,&cp,f)>0){ long id,T; char *e;
- id=strtol(ln,&e,10); if(e==ln) continue; T=strtol(e,&e,10);
- if(T>maxT) maxT=(int)T; (void)id; }
- free(ln); }
- kv_alloc(m,maxT);
+ int maxT=run->manifest->info.maxT;
+ run->completed_items=0; run->completed_targets=0;
+ if(ABLATE_MODEL_COMPUTE_ENABLED) kv_alloc(m,maxT);
float *x=falloc((int64_t)maxT*D), *lo=falloc(V), *row=falloc(D);
- int *ids=malloc((size_t)maxT*sizeof(int));
- int tk_id[ABL_LOGIT_TOPK]; float tk_val[ABL_LOGIT_TOPK];
- rewind(f); char *ln=NULL; size_t cp=0; int nreq=0; double t0=now_s();
- while(getline(&ln,&cp,f)>0){
- char *p=ln, *e;
- long item=strtol(p,&e,10); if(e==p) continue; p=e; /* blank line */
- long T=strtol(p,&e,10); if(e==p){ fprintf(stderr,"[ablate] bad T\n"); continue; } p=e;
- long np=strtol(p,&e,10); if(e==p){ fprintf(stderr,"[ablate] bad n_prompt\n"); continue; } p=e;
- long mode=strtol(p,&e,10); if(e==p){ fprintf(stderr,"[ablate] bad mode\n"); continue; } p=e;
- long nc=strtol(p,&e,10); if(e==p){ fprintf(stderr,"[ablate] bad ncells\n"); continue; } p=e;
- int Ls[ABL_MAX_CELLS], Es[ABL_MAX_CELLS], As[ABL_MAX_CELLS];
- int bad=0; long ncc = nc<0?0:(nc>ABL_MAX_CELLS?ABL_MAX_CELLS:nc);
- for(long i=0;i<nc;i++){ /* read every triple; keep first ABL_MAX_CELLS */
- long L=strtol(p,&e,10); if(e==p){bad=1;break;} p=e;
- long E=strtol(p,&e,10); if(e==p){bad=1;break;} p=e;
- long A=strtol(p,&e,10); if(e==p){bad=1;break;} p=e;
- if(i<ncc){ Ls[i]=(int)L; Es[i]=(int)E; As[i]=(int)A; }
- }
- bad = bad || (T<1 || T>maxT || np<0 || np>T || mode<0 || mode>3);
- for(long i=0;i<T && !bad;i++){ long v=strtol(p,&e,10); if(e==p){bad=1;break;} p=e;
- if(v<0 || v>=V) bad=1; else ids[i]=(int)v; }
- if(bad){ fprintf(stderr,"[ablate] ERR item %ld (bad field/token)\n",item); continue; }
+#ifdef COLI_TEST_ABLATE_ADAPTERS
+ if(g_ablate_adapter_test.bypass_model_compute){
+ if(g_ablate_adapter_test.forced_row)
+ for(int i=0;i<V;i++) lo[i]=g_ablate_adapter_test.forced_row[i];
+ else
+ for(int i=0;i<V;i++) lo[i]=(float)-i;
+ }
+#endif
+ size_t nreq=0; int rc=0; double t0=now_s();
+ for(size_t index=0;index<run->manifest->count;index++){
+ const AblateManifestItem *item=&run->manifest->items[index];
+#ifdef COLI_TEST_ABLATE_ADAPTERS
+ g_ablate_adapter_test.observed_first_token=item->tokens[0];
+#endif
/* PER-ITEM RESET then configure this item's ablation (no cross-item leak). */
abl_reset(&g_abl);
- abl_set_item(&g_abl, (int)mode, Ls, Es, (mode==3?As:NULL), (int)ncc);
- if(of){
- fprintf(of,"{\"t\":\"ah\",\"item\":%ld,\"mode\":%ld,\"ncells\":%ld,\"T\":%ld,\"n_prompt\":%ld,\"cells\":[",
- item, mode, ncc, T, np);
- for(int i=0;i<(int)ncc;i++) fprintf(of,"%s[%d,%d,%d]", i?",":"", Ls[i],Es[i],As[i]);
- fprintf(of,"]}\n");
+ abl_set_item(&g_abl,item->mode,item->layers,item->experts,
+ item->mode==3?item->applied:NULL,item->ncells);
+ if(of && ablate_item_line(of,item->item,item->mode,item->ncells,
+ item->T,item->n_prompt,item->layers,item->experts,
+ item->applied)<0){ rc=1; break; }
+ if(ABLATE_MODEL_COMPUTE_ENABLED){
+ for(int s=0;s<item->T;s++)
+ embed_row(m,item->tokens[s],x+(int64_t)s*D);
+ layers_forward(m,x,item->T,0); /* ONE prefill; moe() applies g_abl */
}
- for(int s=0;s<T;s++) embed_row(m, ids[s], x+(int64_t)s*D);
- layers_forward(m,x,(int)T,0); /* ONE prefill; moe() applies g_abl */
/* FINAL-logit read-out at every target position pos in [np-1, T-1). */
- if(of) for(long pos=(np>0?np-1:0); pos<T-1; pos++){
- rmsnorm(row, x+(int64_t)pos*D, m->final_norm, D, c->eps);
- matmul_qt(lo, row, &m->lm_head, 1);
- int gold=ids[pos+1];
- float mx=lo[0]; int am=0;
- for(int i=1;i<V;i++){ if(lo[i]>mx){mx=lo[i];am=i;} }
- double se=0; for(int i=0;i<V;i++) se+=exp((double)lo[i]-mx);
- double logZ=(double)mx+log(se);
- double gnll=logZ-(double)lo[gold]; /* -log p(gold) */
- float molo=-1e30f; for(int i=0;i<V;i++){ if(i!=gold && lo[i]>molo) molo=lo[i]; }
- float mgn=lo[gold]-molo; /* gold-vs-best-competitor logit margin */
- for(int k=0;k<ABL_LOGIT_TOPK;k++){ tk_id[k]=-1; tk_val[k]=-1e30f; }
- for(int i=0;i<V;i++){ float v=lo[i];
- int mn=0; for(int k=1;k<ABL_LOGIT_TOPK;k++) if(tk_val[k]<tk_val[mn]) mn=k;
- if(v>tk_val[mn]){ tk_val[mn]=v; tk_id[mn]=i; } }
- fprintf(of,"{\"t\":\"lg\",\"item\":%ld,\"pos\":%ld,\"gold\":%d,\"nll\":%.6f,"
- "\"glogit\":%.6g,\"molo\":%.6g,\"mgn\":%.6g,\"am\":%d,\"amlogit\":%.6g,"
- "\"logZ\":%.6f,\"corr\":%d,\"tk\":[",
- item, pos, gold, gnll, (double)lo[gold], (double)molo, (double)mgn,
- am, (double)mx, logZ, (am==gold)?1:0);
- for(int k=0;k<ABL_LOGIT_TOPK;k++) fprintf(of,"%s[%d,%.5g]", k?",":"", tk_id[k], (double)tk_val[k]);
- fprintf(of,"]}\n");
- }
- if(of) fflush(of);
- if(++nreq%8==0) fprintf(stderr,"[ablate %d item | %.1fs | RSS %.2f GB | hit %.0f%%]\n",
+ AblateModelRows rows={m,x,row,lo,item->tokens,item->item,D,V};
+ if(ablate_item_output(of,item->n_prompt-1,item->T-1,
+ ablate_model_row_output,&rows)!=0){ rc=1; break; }
+ run->completed_items++;
+ run->completed_targets+=item->T-item->n_prompt;
+ if(++nreq%8==0) fprintf(stderr,"[ablate %zu item | %.1fs | RSS %.2f GB | hit %.0f%%]\n",
nreq, now_s()-t0, rss_gb(), (m->hits+m->miss)?100.0*m->hits/(m->hits+m->miss):0.0);
}
abl_reset(&g_abl); /* leave the engine in the OFF state */
- if(of){ fflush(of); fclose(of); }
- free(ln); free(ids); free(x); free(lo); free(row); fclose(f);
+ free(x); free(lo); free(row);
+#ifdef COLI_TEST_ABLATE_ADAPTERS
+ if(g_ablate_adapter_test.force_body_rc) rc=1;
+#endif
+ return rc;
+}
+
+static int run_ablate_score(Model *m, const char *path){
+ FILE *f=NULL; int own_manifest=1;
+#ifdef COLI_TEST_ABLATE_ADAPTERS
+ if(g_ablate_adapter_test.borrowed_manifest){
+ f=g_ablate_adapter_test.borrowed_manifest; own_manifest=0;
+ clearerr(f); rewind(f);
+ }else
+#endif
+ { f=fopen(path,"rb"); if(!f){perror(path);exit(1);} }
+ AblateManifest manifest;
+ if(ablate_manifest_load(f,&m->c,&manifest)!=0){
+ if(own_manifest) fclose(f);
+ return 1;
+ }
+#ifdef COLI_TEST_ABLATE_ADAPTERS
+ if(!own_manifest && g_ablate_adapter_test.after_manifest_load)
+ g_ablate_adapter_test.after_manifest_load(f);
+#endif
+ AblateModelRun model_run={m,&manifest,0,0};
+ AblateOutputRun output_run={
+ &model_run,ablate_file_open_run,ablate_model_output_body,
+ &manifest.info,&model_run.completed_items,&model_run.completed_targets,
+ };
+ const char *outp=getenv("ABLATE_OUT");
+#ifdef COLI_TEST_ABLATE_ADAPTERS
+ if(g_ablate_adapter_test.override_outp) outp=g_ablate_adapter_test.outp;
+#endif
+ int rc=ablate_output_run(&output_run,outp,m->c.vocab);
+ /* Keep the manifest identity open until the atomic new-output decision and
+ * all evidence writes finish. Borrowed test streams remain caller-owned. */
+ if(own_manifest && fclose(f)!=0){
+ fprintf(stderr,"[ablate] INCOMPLETE: the manifest could not be closed cleanly\n");
+ rc=1;
+ }
+ ablate_manifest_free(&manifest);
+ return rc;
+}
+
+static int ablate_model_mode_run(Model *m, const char *path){
+ if(!path){
+ fprintf(stderr,"[ablate] INCOMPLETE: the ablation mode was entered "
+ "without a manifest to run\n");
+ return 1;
+ }
+ return run_ablate_score(m,path)==0 ? 0 : 1;
}
static int generate(Model *m, const int *prompt, int np, int n_new, int *out, int *finite){
@@ -11031,6 +11707,18 @@ static int coli_env_on(const char *name)
strcmp(v,"off")==0 || strcmp(v,"no")==0);
}
+/* One process-status mapping for the ablation branch: run the mode, dump the
+ * optional expert histogram, and return the mode's own result as the process
+ * status instead of falling through into the ordinary decode. */
+#define ABLATE_MAIN_RETURN(model_,path_,stats_) do { \
+ Model *const ablate_main_model=(model_); \
+ const char *const ablate_main_path=(path_); \
+ const char *const ablate_main_stats=(stats_); \
+ int ablate_main_rc=ablate_model_mode_run(ablate_main_model,ablate_main_path); \
+ if(ablate_main_stats) stats_dump(ablate_main_model,ablate_main_stats); \
+ return ablate_main_rc; \
+} while(0)
+
#ifndef COLIBRI_NO_MAIN
int main(int argc, char **argv){
int strict=coli_env_on("ORACLE_STRICT");
@@ -11797,7 +12485,9 @@ int main(int argc, char **argv){
* ablation sweep with per-target-position final-logit read-out (ABLATE_OUT=
* <file>). Precedes SCORE. Optional ROUTE_TRACE=<file> records the
* post-ablation router trace. */
- if(getenv("ABLATE_SCORE")){ run_ablate_score(&m, getenv("ABLATE_SCORE")); if(stats) stats_dump(&m,stats); return 0; }
+ if(getenv("ABLATE_SCORE")){
+ ABLATE_MAIN_RETURN(&m,getenv("ABLATE_SCORE"),stats);
+ }
/* modo scoring per benchmark: SCORE=<requests.txt> -> log-likelihood per riga */
if(getenv("SCORE")){ run_score(&m, snap, getenv("SCORE")); if(stats) stats_dump(&m,stats); return 0; }
diff --git a/c/evidence_digest.h b/c/evidence_digest.h
new file mode 100644
index 00000000..1ec76e87
--- /dev/null
+++ b/c/evidence_digest.h
@@ -0,0 +1,120 @@
+/* evidence_digest.h β€” SHA-256 over the exact bytes an evidence-producing mode
+ * consumed. Header-only: all functions are static β€” include from the engine.
+ *
+ * Diagnostic modes of the engine write artifacts that a separate offline tool
+ * checks. For that check to mean anything, the artifact has to name the inputs
+ * it was produced from in a way the tool can recompute independently: the exact
+ * config.json bytes that were loaded, and the exact manifest bytes that were
+ * run. A digest over those byte ranges does that, and a short self-contained
+ * implementation keeps it from becoming a build dependency of the whole engine
+ * on a crypto library it otherwise never needs. This is an integrity aid for
+ * reproducibility, not a security boundary. */
+#ifndef EVIDENCE_DIGEST_H
+#define EVIDENCE_DIGEST_H
+
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+
+typedef struct {
+ uint32_t h[8];
+ uint64_t bits;
+ unsigned char block[64];
+ size_t used;
+} EvidenceSha256;
+
+static uint32_t evidence_rotr32(uint32_t x, unsigned n){
+ return (x>>n)|(x<<(32-n));
+}
+
+static void evidence_sha256_block(EvidenceSha256 *s, const unsigned char *p){
+ static const uint32_t k[64]={
+ 0x428a2f98u,0x71374491u,0xb5c0fbcfu,0xe9b5dba5u,
+ 0x3956c25bu,0x59f111f1u,0x923f82a4u,0xab1c5ed5u,
+ 0xd807aa98u,0x12835b01u,0x243185beu,0x550c7dc3u,
+ 0x72be5d74u,0x80deb1feu,0x9bdc06a7u,0xc19bf174u,
+ 0xe49b69c1u,0xefbe4786u,0x0fc19dc6u,0x240ca1ccu,
+ 0x2de92c6fu,0x4a7484aau,0x5cb0a9dcu,0x76f988dau,
+ 0x983e5152u,0xa831c66du,0xb00327c8u,0xbf597fc7u,
+ 0xc6e00bf3u,0xd5a79147u,0x06ca6351u,0x14292967u,
+ 0x27b70a85u,0x2e1b2138u,0x4d2c6dfcu,0x53380d13u,
+ 0x650a7354u,0x766a0abbu,0x81c2c92eu,0x92722c85u,
+ 0xa2bfe8a1u,0xa81a664bu,0xc24b8b70u,0xc76c51a3u,
+ 0xd192e819u,0xd6990624u,0xf40e3585u,0x106aa070u,
+ 0x19a4c116u,0x1e376c08u,0x2748774cu,0x34b0bcb5u,
+ 0x391c0cb3u,0x4ed8aa4au,0x5b9cca4fu,0x682e6ff3u,
+ 0x748f82eeu,0x78a5636fu,0x84c87814u,0x8cc70208u,
+ 0x90befffau,0xa4506cebu,0xbef9a3f7u,0xc67178f2u,
+ };
+ uint32_t w[64];
+ for(int i=0;i<16;i++)
+ w[i]=((uint32_t)p[4*i]<<24)|((uint32_t)p[4*i+1]<<16)|
+ ((uint32_t)p[4*i+2]<<8)|(uint32_t)p[4*i+3];
+ for(int i=16;i<64;i++){
+ uint32_t s0=evidence_rotr32(w[i-15],7)^evidence_rotr32(w[i-15],18)^(w[i-15]>>3);
+ uint32_t s1=evidence_rotr32(w[i-2],17)^evidence_rotr32(w[i-2],19)^(w[i-2]>>10);
+ w[i]=w[i-16]+s0+w[i-7]+s1;
+ }
+ uint32_t a=s->h[0],b=s->h[1],c=s->h[2],d=s->h[3];
+ uint32_t e=s->h[4],f=s->h[5],g=s->h[6],h=s->h[7];
+ for(int i=0;i<64;i++){
+ uint32_t S1=evidence_rotr32(e,6)^evidence_rotr32(e,11)^evidence_rotr32(e,25);
+ uint32_t ch=(e&f)^((~e)&g);
+ uint32_t t1=h+S1+ch+k[i]+w[i];
+ uint32_t S0=evidence_rotr32(a,2)^evidence_rotr32(a,13)^evidence_rotr32(a,22);
+ uint32_t maj=(a&b)^(a&c)^(b&c), t2=S0+maj;
+ h=g; g=f; f=e; e=d+t1; d=c; c=b; b=a; a=t1+t2;
+ }
+ s->h[0]+=a; s->h[1]+=b; s->h[2]+=c; s->h[3]+=d;
+ s->h[4]+=e; s->h[5]+=f; s->h[6]+=g; s->h[7]+=h;
+}
+
+static void evidence_sha256_init(EvidenceSha256 *s){
+ *s=(EvidenceSha256){{
+ 0x6a09e667u,0xbb67ae85u,0x3c6ef372u,0xa54ff53au,
+ 0x510e527fu,0x9b05688cu,0x1f83d9abu,0x5be0cd19u,
+ },0,{0},0};
+}
+
+/* Streaming update: a caller can hash a file line by line as it validates it,
+ * so the digest covers exactly the bytes that were accepted. */
+static void evidence_sha256_update(EvidenceSha256 *s, const void *data, size_t len){
+ const unsigned char *p=(const unsigned char *)data;
+ s->bits+=(uint64_t)len*8u;
+ while(len){
+ size_t take=64-s->used; if(take>len) take=len;
+ memcpy(s->block+s->used,p,take); s->used+=take; p+=take; len-=take;
+ if(s->used==64){ evidence_sha256_block(s,s->block); s->used=0; }
+ }
+}
+
+static void evidence_sha256_final(EvidenceSha256 *s, unsigned char out[32]){
+ uint64_t bits=s->bits;
+ s->block[s->used++]=0x80;
+ if(s->used>56){
+ memset(s->block+s->used,0,64-s->used);
+ evidence_sha256_block(s,s->block); s->used=0;
+ }
+ memset(s->block+s->used,0,56-s->used);
+ for(int i=0;i<8;i++) s->block[63-i]=(unsigned char)(bits>>(8*i));
+ evidence_sha256_block(s,s->block);
+ for(int i=0;i<8;i++){
+ out[4*i]=(unsigned char)(s->h[i]>>24);
+ out[4*i+1]=(unsigned char)(s->h[i]>>16);
+ out[4*i+2]=(unsigned char)(s->h[i]>>8);
+ out[4*i+3]=(unsigned char)s->h[i];
+ }
+}
+
+/* One-shot digest as the 64 lowercase hex characters an artifact carries,
+ * NUL-terminated so it can be written straight into a text record. */
+static void evidence_sha256_hex(const void *data, size_t len, char out[65]){
+ static const char hex[]="0123456789abcdef";
+ EvidenceSha256 s; unsigned char raw[32];
+ evidence_sha256_init(&s); evidence_sha256_update(&s,data,len);
+ evidence_sha256_final(&s,raw);
+ for(int i=0;i<32;i++){ out[2*i]=hex[raw[i]>>4]; out[2*i+1]=hex[raw[i]&15]; }
+ out[64]=0;
+}
+
+#endif /* EVIDENCE_DIGEST_H */
diff --git a/c/sample.h b/c/sample.h
index 332e13e7..1b206f95 100644
--- a/c/sample.h
+++ b/c/sample.h
@@ -175,6 +175,130 @@ static void stops_arm_tok(const Cfg *c, int tok_eos, Tok *T){
}
static void stops_arm(const Cfg *c, int tok_eos){ stops_arm_tok(c, tok_eos, NULL); }
+/* ---- classified log-prob row reduction ----------------------------------- */
+/* These three are `static inline` rather than plain `static`: they are a
+ * header-only facility that different engine modes pull in as they need it,
+ * and a translation unit that includes this header without calling all of
+ * them should not have to explain itself to -Wunused-function.
+ *
+ * The plain logprob_target() below answers "what is the log-probability of this
+ * token", which is all a sampling loop needs. Evidence-producing modes need a
+ * second answer as well: whether the logit row was numerically sound at all, so
+ * that a run can refuse a position instead of writing a NaN into an artifact a
+ * reader cannot distinguish from a real value. The row reduction and the
+ * per-target subtraction are kept separately reusable because a caller often
+ * reduces a row once and then reads several targets out of it. The
+ * per-target subtraction promotes the float logit to double before
+ * subtracting the row's own double logZ, so its value carries the double
+ * arithmetic's own rounding (a few ulp); logprob_target() below still takes the float-scale subtraction
+ * the sampling path has always used, so the two need not agree past a
+ * float's own precision on a widely spread row. */
+typedef enum {
+ LOGPROB_FINITE=0, /* row reduced normally; the value is usable */
+ LOGPROB_NAN, /* at least one logit was NaN */
+ LOGPROB_POS_INF, /* at least one logit was +infinity */
+ LOGPROB_NEG_INF, /* at least one logit was -infinity */
+ LOGPROB_ALL_NONFINITE, /* no logit in the row was finite */
+ LOGPROB_FINITE_OVERFLOW, /* every logit was finite, the reduction was not */
+ LOGPROB_INVALID, /* no row was supplied, or it had no entries */
+} LogprobStatus;
+
+static inline const char *logprob_status_name(LogprobStatus status){
+ switch(status){
+ case LOGPROB_FINITE: return "FINITE";
+ case LOGPROB_NAN: return "NAN";
+ case LOGPROB_POS_INF: return "POS_INF";
+ case LOGPROB_NEG_INF: return "NEG_INF";
+ case LOGPROB_ALL_NONFINITE: return "ALL_NONFINITE";
+ case LOGPROB_FINITE_OVERFLOW: return "FINITE_OVERFLOW";
+ default: return "INVALID";
+ }
+}
+
+typedef struct {
+ float max; /* largest logit in the row */
+ double logse; /* log of the shifted exponential sum */
+ double logZ; /* max + logse: the row's log partition function */
+ int argmax; /* index of the largest logit */
+ LogprobStatus status;
+} LogprobRow;
+
+/* Reduce one logit row, reporting why it failed rather than only that it did.
+ * No partition function is computed for a row that is not entirely finite.
+ *
+ * The classification is a fixed precedence, not the order the values appear in.
+ * A row with no finite entry at all is ALL_NONFINITE whatever it contains,
+ * because the shape of such a row, not one value in it, is what a reader needs.
+ * Otherwise a NaN outranks an infinity, and a positive infinity outranks a
+ * negative one: a NaN cannot arise from saturation, so it points at a different
+ * defect, and a positive infinity is what actually destroys the reduction. */
+static inline LogprobStatus logprob_row_checked(const float *lo, int V,
+ LogprobRow *out){
+ LogprobRow r={0,0,0,0,LOGPROB_INVALID};
+ if(!lo || V<=0){ if(out) *out=r; return r.status; }
+ int finite_count=0, saw_nan=0, saw_pos_inf=0, saw_neg_inf=0;
+ for(int i=0;i<V;i++){
+ if(isfinite(lo[i])) finite_count++;
+ else if(isnan(lo[i])) saw_nan=1;
+ else if(lo[i]>0) saw_pos_inf=1;
+ else saw_neg_inf=1;
+ }
+ if(finite_count!=V){
+ r.status=finite_count==0 ? LOGPROB_ALL_NONFINITE :
+ saw_nan ? LOGPROB_NAN :
+ saw_pos_inf ? LOGPROB_POS_INF :
+ saw_neg_inf ? LOGPROB_NEG_INF : LOGPROB_INVALID;
+ if(out) *out=r;
+ return r.status;
+ }
+ r.max=lo[0]; r.argmax=0;
+ for(int i=1;i<V;i++) if(lo[i]>r.max){ r.max=lo[i]; r.argmax=i; }
+ double se=0;
+ for(int i=0;i<V;i++){
+ double term=exp((double)lo[i]-(double)r.max);
+ if(!isfinite(term) || !isfinite(se+term)){
+ r.status=LOGPROB_FINITE_OVERFLOW;
+ if(out) *out=r;
+ return r.status;
+ }
+ se+=term;
+ }
+ r.logse=log(se);
+ r.logZ=(double)r.max+r.logse;
+ r.status=(isfinite(r.logse) && isfinite(r.logZ)) ?
+ LOGPROB_FINITE : LOGPROB_FINITE_OVERFLOW;
+ if(out) *out=r;
+ return r.status;
+}
+
+/* Read one target out of an already reduced row. A row that did not reduce
+ * cleanly propagates its own status, so a caller can report the original
+ * cause rather than a generic failure.
+ *
+ * Every return path writes *out, and only LOGPROB_FINITE leaves a usable value
+ * there. A missing row, a missing logit vector or a negative target is
+ * INVALID rather than a propagated row status: those are caller mistakes, not
+ * properties of the data, and returning FINITE for them would hand the caller
+ * an uninitialised number that looks like a measurement. (This function has
+ * no vocabulary size to check a target against from above; the caller is
+ * responsible for bounding it, same as `ablate_logit_record` does.) */
+static inline LogprobStatus logprob_from_row_checked(const float *lo, int target,
+ const LogprobRow *r,
+ double *out){
+ if(out) *out=NAN;
+ if(!lo || !r || target<0) return LOGPROB_INVALID;
+ if(r->status!=LOGPROB_FINITE) return r->status;
+ /* The subtraction is done wholly in double: the float logit is promoted
+ * BEFORE subtracting the row's own double logZ (max+logse). Taking it in
+ * float first rounds away up to an ulp of the row maximum -- on a logit-
+ * scale row that is about 2e-6, visible in every digit a %.17g consumer
+ * reads past the seventh. */
+ double value=(double)lo[target]-r->logZ;
+ if(!isfinite(value)) return LOGPROB_FINITE_OVERFLOW;
+ if(out) *out=value;
+ return LOGPROB_FINITE;
+}
+
/* ---- log-prob of a target token given the logit vector ------------------- */
static double logprob_target(const float *lo, int V, int target, int *am){
float mx = lo[0]; int best = 0;
diff --git a/c/tests/test_ablate_mode.c b/c/tests/test_ablate_mode.c
new file mode 100644
index 00000000..39adb4f7
--- /dev/null
+++ b/c/tests/test_ablate_mode.c
@@ -0,0 +1,620 @@
+/* test_ablate_mode.c β€” the ablation scoring mode's manifest loader, evidence
+ * writer and dispatch contract, with no model and no weights.
+ *
+ * The mode's real work is one teacher-forced prefill per manifest item, which
+ * needs a loaded model. Everything around that prefill -- accepting or
+ * refusing a manifest, binding it by digest, opening the output exactly once,
+ * writing the records an offline reader parses, and reporting completion --
+ * runs without any model math at all. The adapter build bypasses only the
+ * model computation and enters the same production parser, writer and
+ * formatter, so those parts can be pinned on any machine.
+ *
+ * Required properties:
+ * P1 ROUND TRIP β€” a well-formed manifest produces a complete evidence
+ * artifact: a header naming the config and manifest digests, one item
+ * record per item, one logit record per target position, and a done
+ * record whose counts equal the header's expectations.
+ * P2 DIGEST BINDING β€” the manifest digest in the artifact is a digest of the
+ * manifest's own bytes under a domain prefix, so an artifact cannot be
+ * re-attached to a different manifest.
+ * P3 REFUSAL β€” malformed manifests are refused rather than partly run, and
+ * each refusal leaves no evidence file behind.
+ * P4 DISPATCH CONTRACT β€” the mode reports failure and writes nothing when it
+ * is handed no manifest path, which is the value the engine's environment
+ * lookup yields when the mode was not requested.
+ * P5 NEW FILE ONLY β€” an output path that already exists is refused, never
+ * truncated.
+ * P6 LENIENT FRAMING β€” the two line endings a host editor produces without
+ * meaning to (a CRLF terminator, and a last line with no terminator) are
+ * accepted, and all three framings of the same content bind to the same
+ * manifest digest.
+ * P7 NAMED REFUSALS β€” every refusal says why on the error stream; a silent
+ * non-zero exit is indistinguishable from a crash, and a refusal that
+ * names a line names the record at fault rather than the last one read.
+ * P8 BOUNDED INPUT β€” a manifest may not ask the loader for an unbounded
+ * allocation by declaring an enormous item length.
+ * Exit 0 = all pass.
+ *
+ * With "--emit-round-trip <dir> [lf|crlf|unterminated]" it instead writes one
+ * config, one manifest in the named framing and the artifact produced from
+ * them into <dir> and exits, so a checker written independently of this engine
+ * can validate real producer output -- in each framing the engine accepts.
+ */
+#define COLI_TEST_ABLATE_ADAPTERS 1
+#define main coli_glm_main_unused
+#include "../colibri.c"
+#undef main
+#undef COLI_TEST_ABLATE_ADAPTERS
+
+static int fails = 0;
+#define CHECK(cond,msg) do{ if(!(cond)){ printf(" FAIL: %s\n", msg); fails++; } \
+ else printf(" ok: %s\n", msg); }while(0)
+
+/* Process-unique so two concurrent test runs in the same CWD never collide. */
+static char MANIFEST_PATH[64];
+static char EVIDENCE_PATH[64];
+static char STDERR_PATH[64];
+
+static void temp_paths_init(void){
+ long pid=(long)getpid();
+ snprintf(MANIFEST_PATH,sizeof(MANIFEST_PATH),"tmp_test_ablate_manifest.%ld.txt",pid);
+ snprintf(EVIDENCE_PATH,sizeof(EVIDENCE_PATH),"tmp_test_ablate_evidence.%ld.jsonl",pid);
+ snprintf(STDERR_PATH,sizeof(STDERR_PATH),"tmp_test_ablate_stderr.%ld.txt",pid);
+}
+
+/* An eight-expert, four-layer, 64-token vocabulary is enough for every bound
+ * the loader checks, and small enough to write by hand. The digest is taken
+ * over the same bytes a reader would find in the config file, so an external
+ * checker can recompute it. */
+static const char CONFIG_JSON[]=
+ "{\"hidden_size\":8,\"num_hidden_layers\":4,\"n_routed_experts\":8,"
+ "\"first_k_dense_replace\":1,\"vocab_size\":64}\n";
+
+static void cfg_init(Cfg *c){
+ memset(c,0,sizeof(*c));
+ c->hidden=8; c->vocab=64; c->n_layers=4; c->first_dense=1; c->n_experts=8;
+ evidence_sha256_hex(CONFIG_JSON,sizeof(CONFIG_JSON)-1,c->config_sha256);
+}
+
+static int file_write(const char *path, const char *body){
+ FILE *f=fopen(path,"wb");
+ if(!f) return -1;
+ size_t want=strlen(body);
+ size_t got=fwrite(body,1,want,f);
+ return (fclose(f)==0 && got==want) ? 0 : -1;
+}
+
+static void manifest_write(const char *body){
+ (void)file_write(MANIFEST_PATH,body);
+}
+
+/* The manifest used for the round trip, shared with the emitted artifact so an
+ * external checker sees exactly what this test checks. */
+static const char ROUND_TRIP_MANIFEST[]=
+ "0 3 2 0 0 1 2 3\n"
+ "1 4 2 1 1 2 3 -1 4 5 6 7\n";
+
+static char *slurp(const char *path, size_t *len_out){
+ FILE *f=fopen(path,"rb");
+ if(!f) return NULL;
+ fseek(f,0,SEEK_END); long n=ftell(f); fseek(f,0,SEEK_SET);
+ if(n<0){ fclose(f); return NULL; }
+ char *b=malloc((size_t)n+1);
+ if(!b){ fclose(f); return NULL; }
+ size_t got=fread(b,1,(size_t)n,f); fclose(f);
+ b[got]=0;
+ if(len_out) *len_out=got;
+ return b;
+}
+
+/* Run the mode with model computation bypassed. */
+static int run_mode(Cfg *c, const char *manifest_path, const char *out_path){
+ static Model m;
+ memset(&m,0,sizeof(m));
+ m.c=*c;
+ remove(out_path);
+ memset(&g_ablate_adapter_test,0,sizeof(g_ablate_adapter_test));
+ g_ablate_adapter_test.bypass_model_compute=1;
+ g_ablate_adapter_test.override_outp=1;
+ g_ablate_adapter_test.outp=out_path;
+ int rc=ablate_model_mode_run(&m,manifest_path);
+ memset(&g_ablate_adapter_test,0,sizeof(g_ablate_adapter_test));
+ return rc;
+}
+
+static int count_occurrences(const char *hay, const char *needle){
+ int n=0;
+ for(const char *p=strstr(hay,needle); p; p=strstr(p+1,needle)) n++;
+ return n;
+}
+
+static void t_round_trip(Cfg *c){
+ printf("P1 round trip and P2 digest binding\n");
+ /* item T n_prompt mode ncells (L E A)... t_0..t_{T-1} */
+ const char *body=ROUND_TRIP_MANIFEST;
+ manifest_write(body);
+ CHECK(run_mode(c,MANIFEST_PATH,EVIDENCE_PATH)==0, "a well-formed manifest runs to completion");
+
+ size_t len=0;
+ char *text=slurp(EVIDENCE_PATH,&len);
+ CHECK(text!=NULL, "the requested evidence file exists");
+ if(!text) return;
+
+ CHECK(count_occurrences(text,"\"t\":\"hdr\"")==1, "exactly one header record");
+ CHECK(count_occurrences(text,"\"t\":\"ah\"")==2, "one item record per manifest item");
+ CHECK(count_occurrences(text,"\"t\":\"lg\"")==3, "one logit record per target position");
+ CHECK(count_occurrences(text,"\"t\":\"done\"")==1, "exactly one done record");
+ CHECK(strstr(text,"\"schema\":\"coli-ablate/2\"")!=NULL, "the header names the schema");
+ CHECK(strstr(text,"\"expected_items\":2")!=NULL, "the header expects both items");
+ CHECK(strstr(text,"\"expected_targets\":3")!=NULL, "the header expects all three targets");
+ CHECK(strstr(text,"\"completed_items\":2")!=NULL, "the done record completed both items");
+ CHECK(strstr(text,"\"completed_targets\":3")!=NULL, "the done record completed all targets");
+ CHECK(strstr(text,c->config_sha256)!=NULL, "the header carries the loaded config digest");
+ CHECK(strstr(text,"\"cells\":[[2,3,-1]]")!=NULL, "the ablated cell is recorded as given");
+
+ /* P2: recompute the manifest digest the way an offline reader would. */
+ static const char domain[]="coli-ablate-manifest/2\n";
+ EvidenceSha256 h; unsigned char raw[32]; char expect[65];
+ static const char hex[]="0123456789abcdef";
+ evidence_sha256_init(&h);
+ evidence_sha256_update(&h,domain,sizeof(domain)-1);
+ evidence_sha256_update(&h,body,strlen(body));
+ evidence_sha256_final(&h,raw);
+ for(int i=0;i<32;i++){ expect[2*i]=hex[raw[i]>>4]; expect[2*i+1]=hex[raw[i]&15]; }
+ expect[64]=0;
+ CHECK(count_occurrences(text,expect)==2,
+ "both records carry a digest of the manifest bytes under the domain prefix");
+
+ free(text);
+ remove(EVIDENCE_PATH);
+}
+
+static void t_refusal(Cfg *c){
+ printf("P3 malformed manifests are refused\n");
+ static const struct { const char *body; const char *why; } bad[] = {
+ { "", "an empty manifest" },
+ { "\n", "a manifest that is only a line terminator" },
+ { "0 3 2 0 0 1 2 3\n\n", "a manifest with an empty line in it" },
+ { "0 3 2 0\r 0 1 2 3\n", "a carriage return inside a record" },
+ { "00 3 2 0 0 1 2 3\n", "a leading zero in a field" },
+ { "0 3 2 0 0 1 2 3 4\n", "a token count that exceeds the declared length" },
+ { "0 3 2 0 0 1 2\n", "a token count below the declared length" },
+ { "0 1 1 0 0 5\n", "a sequence with no target position" },
+ { "0 3 3 0 0 1 2 3\n", "a prompt that leaves no target position" },
+ { "0 3 2 4 1 2 3 -1 1 2 3\n", "an unknown ablation mode" },
+ { "0 3 2 1 0 1 2 3\n", "an ablating mode with no cells" },
+ { "0 3 2 0 1 2 3 -1 1 2 3\n", "a baseline item with cells" },
+ { "0 3 2 1 1 0 3 -1 1 2 3\n", "a layer below the first routed layer" },
+ { "0 3 2 1 1 2 99 -1 1 2 3\n", "an expert beyond the model's expert count" },
+ { "0 3 2 3 1 2 3 3 1 2 3\n", "a swap whose target is the ablated expert" },
+ { "0 3 2 1 1 2 3 0 1 2 3\n", "a swap target on a non-swap mode" },
+ { "0 3 2 1 2 2 3 -1 2 3 -1 1 2 3\n", "a duplicate cell within one item" },
+ { "0 3 2 0 0 1 2 999\n", "a token beyond the vocabulary" },
+ { "0 3 2 0 0 1 2 64\n", "a token exactly at the vocab size -- one past the "
+ "last valid id (999 above is not a boundary test: it is caught the same "
+ "way whether the bound is >=V or the off-by-one >V)" },
+ { "99999999999999999999999999 3 2 0 0 1 2 3\n",
+ "an item id with enough digits to overflow 64-bit parsing (ERANGE) -- "
+ "the item field has no upper-bound range check of its own, so an "
+ "overflow silently clamped to INTMAX_MAX would otherwise be accepted "
+ "as an enormous-but-'valid' item id" },
+ { "0 3 2 0 0 1 2 3\n0 3 2 0 0 1 2 3\n", "a duplicate item id" },
+ };
+ for(unsigned i=0;i<sizeof(bad)/sizeof(bad[0]);i++){
+ manifest_write(bad[i].body);
+ int rc=run_mode(c,MANIFEST_PATH,EVIDENCE_PATH);
+ char *text=slurp(EVIDENCE_PATH,NULL);
+ CHECK(rc==1 && text==NULL, bad[i].why);
+ free(text);
+ remove(EVIDENCE_PATH);
+ }
+}
+
+/* The vocab bound above (P3) proves the exact boundary (V) is refused; this
+ * proves the check is not off by one in the OTHER direction either -- the
+ * true last valid id (V-1) must still be accepted, or a fix for one off-by-one
+ * could silently introduce the opposite one. */
+static void t_token_boundary_accepts_the_last_valid_id(Cfg *c){
+ printf("P3c the largest valid token id (vocab-1) is still accepted\n");
+ manifest_write("0 3 2 0 0 1 2 63\n");
+ int rc=run_mode(c,MANIFEST_PATH,EVIDENCE_PATH);
+ char *text=slurp(EVIDENCE_PATH,NULL);
+ CHECK(rc==0 && text!=NULL, "token id 63 (vocab-1, this fixture's vocab=64) runs to completion");
+ free(text);
+ remove(EVIDENCE_PATH);
+}
+
+static void t_dispatch_contract(Cfg *c){
+ printf("P4 the mode is not entered without a manifest path\n");
+ static Model m;
+ memset(&m,0,sizeof(m));
+ m.c=*c;
+ remove(EVIDENCE_PATH);
+ memset(&g_ablate_adapter_test,0,sizeof(g_ablate_adapter_test));
+ g_ablate_adapter_test.bypass_model_compute=1;
+ g_ablate_adapter_test.override_outp=1;
+ g_ablate_adapter_test.outp=EVIDENCE_PATH;
+ /* NULL is what the engine's environment lookup yields when the mode was
+ * not requested; the mode entry must refuse it and touch nothing. */
+ int rc=ablate_model_mode_run(&m,NULL);
+ char *text=slurp(EVIDENCE_PATH,NULL);
+ CHECK(rc==1, "no manifest path is a failed mode result");
+ CHECK(text==NULL, "no manifest path writes no evidence file");
+ free(text);
+ memset(&g_ablate_adapter_test,0,sizeof(g_ablate_adapter_test));
+ remove(EVIDENCE_PATH);
+}
+
+static void t_new_file_only(Cfg *c){
+ printf("P5 an existing output path is refused\n");
+ manifest_write("0 3 2 0 0 1 2 3\n");
+ FILE *f=fopen(EVIDENCE_PATH,"wb");
+ if(f){ fputs("do not truncate me\n",f); fclose(f); }
+ static Model m;
+ memset(&m,0,sizeof(m));
+ m.c=*c;
+ memset(&g_ablate_adapter_test,0,sizeof(g_ablate_adapter_test));
+ g_ablate_adapter_test.bypass_model_compute=1;
+ g_ablate_adapter_test.override_outp=1;
+ g_ablate_adapter_test.outp=EVIDENCE_PATH;
+ int rc=ablate_model_mode_run(&m,MANIFEST_PATH);
+ memset(&g_ablate_adapter_test,0,sizeof(g_ablate_adapter_test));
+ CHECK(rc==1, "an existing output path is a failed mode result");
+ char *text=slurp(EVIDENCE_PATH,NULL);
+ CHECK(text && strcmp(text,"do not truncate me\n")==0, "the existing file is left intact");
+ free(text);
+ remove(EVIDENCE_PATH);
+}
+
+/* Copy the 64 digest characters that follow the named key, or return 0. */
+static int digest_of(const char *text, const char *key, char out[65]){
+ const char *at=strstr(text,key);
+ if(!at) return 0;
+ at+=strlen(key);
+ for(int i=0;i<64;i++){
+ if(!at[i]) return 0;
+ out[i]=at[i];
+ }
+ out[64]=0;
+ return 1;
+}
+
+static int manifest_digest_of_run(Cfg *c, const char *body, char out[65]){
+ manifest_write(body);
+ if(run_mode(c,MANIFEST_PATH,EVIDENCE_PATH)!=0) return 0;
+ char *text=slurp(EVIDENCE_PATH,NULL);
+ int ok=text && digest_of(text,"\"manifest_sha256\":\"",out);
+ free(text);
+ remove(EVIDENCE_PATH);
+ return ok;
+}
+
+static void t_lenient_framing(Cfg *c){
+ printf("P6 the framings a host editor produces are accepted\n");
+ char canonical[65], crlf[65], unterminated[65];
+ CHECK(manifest_digest_of_run(c,"0 3 2 0 0 1 2 3\n",canonical),
+ "a canonical manifest runs");
+ CHECK(manifest_digest_of_run(c,"0 3 2 0 0 1 2 3\r\n",crlf),
+ "a manifest with CRLF line endings runs");
+ CHECK(manifest_digest_of_run(c,"0 3 2 0 0 1 2 3",unterminated),
+ "a manifest whose last line has no terminator runs");
+ /* Known answer, pinned identically in tests/test_check_ablate_evidence.py:
+ * SHA-256 of "coli-ablate-manifest/2\n" followed by the canonical record.
+ * Pinning the literal on both sides is what makes the two implementations
+ * of the canonical rule checkable against each other rather than only
+ * against themselves. */
+ CHECK(strcmp(canonical,
+ "c63a48c375b14ca60f26c7e3c5dd36b5929ffaf669a45511c93deee6e8bbd5ed")==0,
+ "the canonical digest matches the value the offline checker computes");
+ CHECK(strcmp(canonical,crlf)==0,
+ "a CRLF manifest binds to the same digest as its canonical form");
+ CHECK(strcmp(canonical,unterminated)==0,
+ "an unterminated manifest binds to the same digest as its canonical form");
+
+ /* Both leniencies at once, across more than one record. */
+ char mixed[65];
+ CHECK(manifest_digest_of_run(c,"0 3 2 0 0 1 2 3\r\n1 3 2 0 0 4 5 6",mixed),
+ "CRLF endings and a missing final terminator together run");
+ char plain[65];
+ CHECK(manifest_digest_of_run(c,"0 3 2 0 0 1 2 3\n1 3 2 0 0 4 5 6\n",plain),
+ "the same two records in canonical form run");
+ CHECK(strcmp(mixed,plain)==0, "the mixed framing binds to the canonical digest");
+}
+
+/* Refusals must say why. stderr is redirected to a file for the duration and
+ * left there: everything this test reports goes to stdout. */
+
+static long stderr_bytes_of(Cfg *c, const char *manifest_body, void (*damage)(Cfg *)){
+ Cfg local=*c;
+ if(damage) damage(&local);
+ manifest_write(manifest_body);
+ if(!freopen(STDERR_PATH,"w",stderr)) return -1;
+ int rc=run_mode(&local,MANIFEST_PATH,EVIDENCE_PATH);
+ fflush(stderr);
+ size_t len=0;
+ char *text=slurp(STDERR_PATH,&len);
+ free(text);
+ remove(EVIDENCE_PATH);
+ return rc==1 ? (long)len : -1;
+}
+
+static void damage_unhashed_config(Cfg *c){ memset(c->config_sha256,0,65); }
+static void damage_short_digest(Cfg *c){ c->config_sha256[7]='Z'; }
+static void damage_layer_count(Cfg *c){ c->n_layers=129; }
+static void damage_expert_count(Cfg *c){ c->n_experts=0; }
+static void damage_vocab(Cfg *c){ c->vocab=(1<<24)+1; }
+static void damage_first_dense(Cfg *c){ c->first_dense=c->n_layers+1; }
+
+static void t_named_refusals(Cfg *c){
+ printf("P7 every refusal says why on the error stream\n");
+ static const char good[]="0 3 2 0 0 1 2 3\n";
+ static const struct { void (*damage)(Cfg *); const char *body; const char *why; } cases[] = {
+ { damage_unhashed_config, good, "an unhashed config is refused with a reason" },
+ { damage_short_digest, good, "a malformed config digest is refused with a reason" },
+ { damage_layer_count, good, "too many layers is refused with a reason" },
+ { damage_expert_count, good, "no experts is refused with a reason" },
+ { damage_vocab, good, "an implausible vocabulary is refused with a reason" },
+ { damage_first_dense, good, "a first routed layer past the end is refused with a reason" },
+ { NULL, "0 3 2 0 0 1 2 999\n", "a malformed manifest is refused with a reason" },
+ { NULL, "", "an empty manifest is refused with a reason" },
+ };
+ for(unsigned i=0;i<sizeof(cases)/sizeof(cases[0]);i++){
+ long bytes=stderr_bytes_of(c,cases[i].body,cases[i].damage);
+ CHECK(bytes>0, cases[i].why);
+ }
+ remove(STDERR_PATH);
+}
+
+/* Run once with stderr captured, and hand the captured text back. */
+static char *refusal_text_of(Cfg *c, const char *manifest_body){
+ manifest_write(manifest_body);
+ if(!freopen(STDERR_PATH,"w",stderr)) return NULL;
+ int rc=run_mode(c,MANIFEST_PATH,EVIDENCE_PATH);
+ fflush(stderr);
+ remove(EVIDENCE_PATH);
+ if(rc!=1) return NULL;
+ return slurp(STDERR_PATH,NULL);
+}
+
+static void t_refusal_names_the_offender(Cfg *c){
+ printf("P7b a refusal names the record at fault\n");
+ /* Three good records, then a fourth repeating the first record's id, then
+ * a fifth good one. The duplicate check runs after the whole file is read,
+ * so naming the last line read would report line 5 here. */
+ char *text=refusal_text_of(c,
+ "7 3 2 0 0 1 2 3\n"
+ "8 3 2 0 0 1 2 3\n"
+ "9 3 2 0 0 1 2 3\n"
+ "7 3 2 0 0 4 5 6\n"
+ "10 3 2 0 0 1 2 3\n");
+ CHECK(text!=NULL, "a duplicate item id is refused");
+ CHECK(text && strstr(text,"at line 4")!=NULL,
+ "the duplicate names the line that repeats the id, not the last line");
+ CHECK(text && strstr(text,"at line 5")==NULL,
+ "the duplicate does not name the last line of the file");
+ free(text);
+
+ /* A malformed record in the middle names itself, not the end of file. */
+ text=refusal_text_of(c,
+ "0 3 2 0 0 1 2 3\n"
+ "nonsense\n"
+ "2 3 2 0 0 1 2 3\n");
+ CHECK(text && strstr(text,"at line 2")!=NULL,
+ "a malformed record names its own line");
+ free(text);
+ remove(STDERR_PATH);
+}
+
+static void t_bounded_item_length(Cfg *c){
+ printf("P8 an absurd item length is refused, not allocated\n");
+ char *text=refusal_text_of(c,"0 9000000000000000000 2 0 0 1 2 3\n");
+ CHECK(text!=NULL, "an item length near the 64-bit ceiling is refused");
+ CHECK(text && strstr(text,"above the")!=NULL &&
+ strstr(text,"limit for one item")!=NULL,
+ "the refusal names the per-item token limit");
+ free(text);
+
+ char body[64];
+ snprintf(body,sizeof(body),"0 %d 2 0 0 1 2 3\n",ABLATE_MAX_ITEM_TOKENS+1);
+ text=refusal_text_of(c,body);
+ CHECK(text!=NULL, "one token past the documented limit is refused");
+ CHECK(text && strstr(text,"above the")!=NULL, "and names the limit");
+ free(text);
+ remove(STDERR_PATH);
+}
+
+/* nll must come from a subtraction done wholly in double
+ * (logZ - lo[gold]), not from a float-rounded intermediate. A row this
+ * wide (max 1.0e7, gold logit 0.25, everything else far below the max)
+ * makes the two formulas disagree: the float subtraction lo[gold]-max
+ * rounds gold's contribution away entirely (its ulp near 1.0e7 is 1.0,
+ * and 0.25 is under half that), while the all-double path keeps it. */
+static void t_nll_pin(Cfg *c){
+ printf("nll is a double-precision reduction, not a float intermediate\n");
+ int V=c->vocab; /* 64, from CONFIG_JSON */
+ float *row=malloc(sizeof(float)*(size_t)V);
+ for(int i=0;i<V;i++) row[i]=-1.0e7f;
+ row[0]=1.0e7f; /* the row's max */
+ row[2]=0.25f; /* the gold logit -- deliberately tiny
+ * next to the max */
+
+ /* item 0: T=3, n_prompt=2, tokens 1,5,2 -- the one target position is
+ * pos=1, whose gold token is tokens[2]=2. */
+ manifest_write("0 3 2 0 0 1 5 2\n");
+ static Model m;
+ memset(&m,0,sizeof(m));
+ m.c=*c;
+ remove(EVIDENCE_PATH);
+ memset(&g_ablate_adapter_test,0,sizeof(g_ablate_adapter_test));
+ g_ablate_adapter_test.bypass_model_compute=1;
+ g_ablate_adapter_test.override_outp=1;
+ g_ablate_adapter_test.outp=EVIDENCE_PATH;
+ g_ablate_adapter_test.forced_row=row;
+ int rc=ablate_model_mode_run(&m,MANIFEST_PATH);
+ memset(&g_ablate_adapter_test,0,sizeof(g_ablate_adapter_test));
+ CHECK(rc==0, "the wide-spread row runs to completion");
+
+ char *text=slurp(EVIDENCE_PATH,NULL);
+ CHECK(text!=NULL, "the evidence file exists");
+ if(text){
+ const char *key="\"nll\":";
+ const char *at=strstr(text,key);
+ CHECK(at!=NULL, "the logit record carries an nll field");
+ if(at){
+ double got=strtod(at+strlen(key),NULL);
+ /* dev's formula, replicated here in double from the row: */
+ double max=row[0];
+ double se=0;
+ for(int i=0;i<V;i++) se+=exp((double)row[i]-max);
+ double expect=(max+log(se))-(double)row[2];
+ CHECK(got==expect,
+ "nll matches dev's double-precision formula exactly");
+ }
+ }
+ free(text);
+ remove(EVIDENCE_PATH);
+ free(row);
+}
+
+/* config_sha256 must be bound through the production load path
+ * (cfg_root, which load_cfg calls), not filled in by a test double that
+ * hashes the literal separately. cfg_root takes the digest as an
+ * out-parameter, so the hashing branch is exercised directly through
+ * that parameter rather than by round-tripping the ABLATE_SCORE
+ * environment variable through setenv/getenv (the pairing is not
+ * reliable on every platform's C runtime -- see load_cfg's caller
+ * below for the one assertion that still goes through load_cfg itself,
+ * which needs no environment variable at all). */
+static void t_load_cfg_digest_binding(Cfg *unused){
+ (void)unused;
+ printf("load_cfg binds config_sha256 to the loaded config.json bytes\n");
+ /* load_cfg validates the full GLM config shape (n_group==1, every CKR
+ * bound), unlike cfg_init above which sets Cfg fields directly -- so
+ * this fixture, unlike CONFIG_JSON, must be a config.json load_cfg
+ * actually accepts. */
+ static const char CFG_BODY[]=
+ "{\"hidden_size\":64,\"num_hidden_layers\":2,\"num_attention_heads\":4,"
+ "\"n_routed_experts\":8,\"num_experts_per_tok\":2,"
+ "\"moe_intermediate_size\":32,\"intermediate_size\":64,"
+ "\"first_k_dense_replace\":1,\"q_lora_rank\":0,\"kv_lora_rank\":16,"
+ "\"qk_nope_head_dim\":8,\"qk_rope_head_dim\":8,\"v_head_dim\":8,"
+ "\"n_shared_experts\":1,\"vocab_size\":200,\"n_group\":1,"
+ "\"topk_group\":1,\"rope_theta\":10000.0}\n";
+ /* shasum -a 256 of the exact CFG_BODY bytes above, computed independently
+ * of this codebase. */
+ static const char PINNED_DIGEST[]=
+ "3fc4425efb033c287f49e5db866946faf9672609e8916218ec6aacb8d4d530cf";
+ CHECK(strlen(PINNED_DIGEST)==64, "the pinned literal is 64 hex characters");
+
+ char dir[]="tmp_test_ablate_cfg_XXXXXX";
+ if(!mkdtemp(dir)){ CHECK(0, "the config test directory could not be created"); return; }
+ char path[512]; snprintf(path,sizeof(path),"%s/config.json",dir);
+ CHECK(file_write(path,CFG_BODY)==0, "the temp config.json was written");
+
+ /* load_cfg's own default call (no environment variable touched at
+ * all) must leave config_sha256 unset -- this is the production
+ * entry point every real caller uses. */
+ Cfg c1; memset(&c1,0,sizeof(c1));
+ load_cfg(&c1,dir);
+ CHECK(c1.config_sha256[0]==0,
+ "the default load path leaves config_sha256 unset");
+
+ /* The hashing branch itself: called directly through cfg_root's
+ * digest out-parameter, the same function load_cfg calls when its
+ * caller asks for a digest. */
+ char *ar=NULL; char got_digest[65]; memset(got_digest,0,sizeof(got_digest));
+ jval *r=cfg_root(dir,&ar,got_digest);
+ CHECK(r!=NULL, "cfg_root parses the temp config.json");
+
+ /* Independently computed SHA-256 of the exact file bytes. */
+ EvidenceSha256 h; unsigned char raw[32]; char expect[65];
+ static const char hex[]="0123456789abcdef";
+ evidence_sha256_init(&h);
+ evidence_sha256_update(&h,CFG_BODY,sizeof(CFG_BODY)-1);
+ evidence_sha256_final(&h,raw);
+ for(int i=0;i<32;i++){ expect[2*i]=hex[raw[i]>>4]; expect[2*i+1]=hex[raw[i]&15]; }
+ expect[64]=0;
+ CHECK(strcmp(got_digest,expect)==0,
+ "cfg_root's digest matches an independently computed SHA-256 of the bytes");
+ /* And against a literal computed outside this codebase, so a change to
+ * both the fixture and the hash function in the same wrong direction
+ * cannot pass unnoticed. */
+ CHECK(strcmp(got_digest,PINNED_DIGEST)==0,
+ "cfg_root's digest matches the pinned literal for this fixture");
+
+ free(ar);
+ remove(path);
+ rmdir(dir);
+}
+
+/* Write one config, one manifest and the artifact produced from them into a
+ * directory, for an independently written checker to validate. */
+/* The round-trip manifest re-framed the way a host editor might have saved it.
+ * The engine accepts all three and binds them to the same digest. */
+static int reframe(const char *canonical, const char *framing,
+ char *dst, size_t cap){
+ size_t out=0;
+ for(size_t i=0;canonical[i];i++){
+ if(canonical[i]=='\n'){
+ if(strcmp(framing,"crlf")==0){
+ if(out+2>=cap) return -1;
+ dst[out++]='\r';
+ }else if(strcmp(framing,"unterminated")==0 && canonical[i+1]==0){
+ break; /* drop the final terminator */
+ }
+ if(out+1>=cap) return -1;
+ dst[out++]='\n';
+ continue;
+ }
+ if(out+1>=cap) return -1;
+ dst[out++]=canonical[i];
+ }
+ dst[out]=0;
+ return 0;
+}
+
+static int emit_round_trip(Cfg *c, const char *dir, const char *framing){
+ char config_path[1024], manifest_path[1024], evidence_path[1024];
+ if(snprintf(config_path,sizeof(config_path),"%s/config.json",dir)>=(int)sizeof(config_path) ||
+ snprintf(manifest_path,sizeof(manifest_path),"%s/manifest.txt",dir)>=(int)sizeof(manifest_path) ||
+ snprintf(evidence_path,sizeof(evidence_path),"%s/evidence.jsonl",dir)>=(int)sizeof(evidence_path)){
+ fprintf(stderr,"test_ablate_mode: directory name is too long\n");
+ return 1;
+ }
+ char framed[4096];
+ if(reframe(ROUND_TRIP_MANIFEST,framing,framed,sizeof(framed))!=0){
+ fprintf(stderr,"test_ablate_mode: unknown or oversized framing %s\n",framing);
+ return 1;
+ }
+ if(file_write(config_path,CONFIG_JSON)!=0 ||
+ file_write(manifest_path,framed)!=0){
+ fprintf(stderr,"test_ablate_mode: cannot write into %s\n",dir);
+ return 1;
+ }
+ int rc=run_mode(c,manifest_path,evidence_path);
+ if(rc!=0) fprintf(stderr,"test_ablate_mode: the ablation mode failed\n");
+ return rc;
+}
+
+int main(int argc, char **argv){
+ temp_paths_init();
+ Cfg c; cfg_init(&c);
+ if((argc==3 || argc==4) && strcmp(argv[1],"--emit-round-trip")==0)
+ return emit_round_trip(&c,argv[2],argc==4?argv[3]:"lf");
+ printf("test_ablate_mode\n");
+ t_round_trip(&c);
+ t_refusal(&c);
+ t_token_boundary_accepts_the_last_valid_id(&c);
+ t_dispatch_contract(&c);
+ t_new_file_only(&c);
+ t_lenient_framing(&c);
+ t_named_refusals(&c);
+ t_refusal_names_the_offender(&c);
+ t_bounded_item_length(&c);
+ t_nll_pin(&c);
+ t_load_cfg_digest_binding(&c);
+ remove(MANIFEST_PATH);
+ remove(EVIDENCE_PATH);
+ printf(fails ? "FAILED (%d)\n" : "PASSED (%d failures)\n", fails);
+ return fails ? 1 : 0;
+}
diff --git a/c/tests/test_ablate_mode_gate.py b/c/tests/test_ablate_mode_gate.py
new file mode 100644
index 00000000..cc7b33ef
--- /dev/null
+++ b/c/tests/test_ablate_mode_gate.py
@@ -0,0 +1,303 @@
+"""The ablation scoring mode must stay unreachable unless it is asked for, and
+its output must satisfy a checker written independently of the engine.
+
+Two different risks are covered here, and neither is covered anywhere else.
+
+The first is that the mode becomes reachable on a normal run. Ablation is a
+diagnostic path: it replaces the whole decode with a teacher-forced sweep and
+returns its own process status. If its dispatch ever ran unconditionally,
+every ordinary invocation would stop doing what it was asked to do. That
+branch sits after the model is loaded, and this repository ships no weights, so
+no test can reach it by running the engine; what can be checked, exactly and
+mechanically, is that the engine contains no path into the mode other than its
+environment variable. These checks read `colibri.c` for that reason, and they
+fail if the guard is removed, weakened, or bypassed by a second call site.
+
+The second is that the engine and the offline checker drift apart. The mode's
+whole purpose is to produce an artifact that `tools/check_ablate_evidence.py`
+can validate; a producer change that the checker would reject is a defect even
+if the engine is self-consistent. The last check runs the real producer and
+the real checker against each other, once per manifest framing the engine
+accepts, because the framings are exactly where the two could disagree: the
+engine normalises them before hashing, and the checker has to reproduce that
+normalisation rather than hash the file as it sits on disk.
+"""
+
+import pathlib
+import re
+import subprocess
+import sys
+import tempfile
+import unittest
+
+HERE = pathlib.Path(__file__).resolve().parent
+ENGINE = HERE.parent / "colibri.c"
+VALIDATOR = HERE.parent / "tools" / "check_ablate_evidence.py"
+PRODUCER = HERE / "test_ablate_mode"
+
+ENV_GUARD = 'if(getenv("ABLATE_SCORE")){'
+ADAPTER_DEFINE = "COLI_TEST_ABLATE_ADAPTERS"
+ADAPTER_STATE = "g_ablate_adapter_test"
+ADAPTER_STATE_RE = re.compile(r"\b" + re.escape(ADAPTER_STATE) + r"\b")
+
+
+def _engine_lines():
+ return ENGINE.read_text(encoding="utf-8").splitlines()
+
+
+def _strip_comments(lines):
+ """Return *lines* with the text of `//` and `/* ... */` comments (including
+ comments that span multiple lines) blanked out to spaces.
+
+ Line count and column positions are preserved so callers can keep using
+ 0-based line numbers from the original file. Without this, a reference
+ to `g_ablate_adapter_test` inside a comment reads as a real use -- a
+ single stray comment could satisfy the "population is nonzero" anchor
+ below even after every real reference was deleted.
+ """
+ out_lines = []
+ in_block = False
+ for line in lines:
+ out = []
+ i, n = 0, len(line)
+ while i < n:
+ if in_block:
+ end = line.find("*/", i)
+ if end == -1:
+ out.append(" " * (n - i))
+ i = n
+ else:
+ out.append(" " * (end + 2 - i))
+ i = end + 2
+ in_block = False
+ continue
+ line_comment = line.find("//", i)
+ block_comment = line.find("/*", i)
+ if line_comment == -1 and block_comment == -1:
+ out.append(line[i:])
+ i = n
+ elif block_comment == -1 or (line_comment != -1 and line_comment < block_comment):
+ out.append(line[i:line_comment])
+ out.append(" " * (n - line_comment))
+ i = n
+ else:
+ out.append(line[i:block_comment])
+ i = block_comment + 2
+ in_block = True
+ out_lines.append("".join(out))
+ return out_lines
+
+
+def _adapter_state_uses(lines):
+ """0-based line numbers with a code (non-comment) reference to the
+ adapter-only test state."""
+ code_lines = _strip_comments(lines)
+ return [number for number, line in enumerate(code_lines) if ADAPTER_STATE_RE.search(line)]
+
+
+def _adapter_only_lines(lines):
+ """Line numbers (0-based) that the compiler only sees when the model-free
+ adapter build is selected.
+
+ Tracks `#if`/`#ifdef`/`#ifndef`/`#elif`/`#else`/`#endif` nesting depth.
+ A block opened with `#ifdef COLI_TEST_ABLATE_ADAPTERS` (or an equivalent
+ `#if` testing the define) puts its own arm -- up to the first `#elif` or
+ `#else` at that same depth -- into the adapter-only region; that
+ `#elif`/`#else` is the PRODUCT arm and ends the region immediately,
+ it does not extend it. A block opened with `#ifndef
+ COLI_TEST_ABLATE_ADAPTERS` is the mirror image: its own arm runs whenever
+ the adapter harness is *not* selected, i.e. it IS the product arm, so the
+ adapter-only region only begins at that block's `#else` (there is no
+ inverted equivalent of `#elif` handled here, since `#ifndef` takes no
+ condition to test one against). Nesting inside an active region is
+ unaffected: every line stays adapter-only until the region's own
+ `#elif`/`#else`/`#endif` is reached.
+ """
+ guarded = set()
+ depth = 0
+ adapter_depth = None
+ pending_ifndef_depth = None
+ for number, line in enumerate(lines):
+ stripped = line.strip()
+ if stripped.startswith("#if"):
+ depth += 1
+ if ADAPTER_DEFINE in stripped:
+ if stripped.startswith("#ifndef"):
+ if pending_ifndef_depth is None:
+ pending_ifndef_depth = depth
+ elif adapter_depth is None:
+ adapter_depth = depth
+ elif stripped.startswith("#elif") or stripped.startswith("#else"):
+ if adapter_depth is not None and depth == adapter_depth:
+ # The #ifdef ADAPTER_DEFINE arm ends here; #elif/#else is a
+ # product arm regardless of what it itself tests.
+ adapter_depth = None
+ elif (
+ pending_ifndef_depth is not None
+ and depth == pending_ifndef_depth
+ and stripped.startswith("#else")
+ ):
+ # The #ifndef ADAPTER_DEFINE arm (the product arm) ends here;
+ # its #else is the adapter-only arm.
+ adapter_depth = depth
+ pending_ifndef_depth = None
+ elif stripped.startswith("#endif"):
+ if adapter_depth is not None and depth == adapter_depth:
+ adapter_depth = None
+ if pending_ifndef_depth is not None and depth == pending_ifndef_depth:
+ pending_ifndef_depth = None
+ depth = max(0, depth - 1)
+ elif adapter_depth is not None:
+ guarded.add(number)
+ return guarded
+
+
+class AblationModeEntryTest(unittest.TestCase):
+ def test_the_mode_has_exactly_one_product_entry_and_it_is_the_guard(self):
+ lines = _engine_lines()
+ adapter_only = _adapter_only_lines(lines)
+ uses = [
+ number
+ for number, line in enumerate(lines)
+ if "ABLATE_MAIN_RETURN(" in line and not line.lstrip().startswith("#define")
+ ]
+ self.assertEqual(len(uses), 1, "expected exactly one entry into the ablation mode")
+ product = [number for number in uses if number not in adapter_only]
+ self.assertEqual(len(product), 1, "the ablation mode has more than one product entry")
+
+ guard = lines[product[0] - 1].strip()
+ self.assertEqual(
+ guard,
+ ENV_GUARD,
+ "the product entry to the ablation mode is not guarded by its "
+ "environment variable; an unguarded dispatch would replace every "
+ "ordinary run with a diagnostic sweep",
+ )
+
+ def test_every_adapter_state_reference_is_compile_gated(self):
+ """The model-free adapter build is a test harness, not a product mode.
+
+ Its state exists only so the parser/writer/status path can be driven
+ with no weights; if any reference to it escaped the
+ COLI_TEST_ABLATE_ADAPTERS gate, the shipped engine would carry a
+ runtime test mode -- state that a normal run could observe or branch
+ on. Counting the gated references and requiring that count to be the
+ whole population fails both ways it can break: a reference moved out of
+ the gate, and the gate itself removed (which empties the gated set).
+ """
+ lines = _engine_lines()
+ adapter_only = _adapter_only_lines(lines)
+ uses = _adapter_state_uses(lines)
+ self.assertGreater(
+ len(uses), 0,
+ f"{ADAPTER_STATE} is gone from the engine; this check no longer "
+ f"guards anything and must be retired or re-pointed",
+ )
+ gated = [number for number in uses if number in adapter_only]
+ self.assertEqual(
+ len(gated), len(uses),
+ f"{ADAPTER_STATE} is referenced outside the {ADAPTER_DEFINE} gate "
+ f"at engine lines "
+ f"{[number + 1 for number in uses if number not in adapter_only]}; "
+ f"the product build must carry no adapter state",
+ )
+
+ def test_no_second_path_reaches_the_mode(self):
+ lines = _engine_lines()
+ entry = [line for line in lines if "ablate_model_mode_run(" in line]
+ self.assertEqual(
+ [line for line in entry if line.lstrip().startswith("static int ablate_model_mode_run")],
+ [line for line in entry if "static int" in line],
+ "ablate_model_mode_run is declared more than once",
+ )
+ callers = [
+ line
+ for line in entry
+ if "static int" not in line and not line.strip().startswith("*")
+ ]
+ self.assertEqual(
+ len(callers), 1,
+ "ablate_model_mode_run is called from somewhere other than the guard macro",
+ )
+ self.assertIn("ablate_main_rc=ablate_model_mode_run", callers[0].replace(" ", ""))
+
+ runners = [
+ number
+ for number, line in enumerate(lines)
+ if "run_ablate_score(" in line and "static int run_ablate_score" not in line
+ ]
+ self.assertEqual(len(runners), 1, "run_ablate_score is called from more than one place")
+ enclosing = ""
+ for number in range(runners[0], -1, -1):
+ stripped = lines[number].strip()
+ if stripped.startswith("static ") and stripped.endswith("{"):
+ enclosing = stripped
+ break
+ self.assertIn(
+ "ablate_model_mode_run",
+ enclosing,
+ "run_ablate_score is called from outside the mode implementation",
+ )
+
+
+class AblationEvidenceRoundTripTest(unittest.TestCase):
+ FRAMINGS = ("lf", "crlf", "unterminated")
+
+ def test_producer_output_passes_the_offline_checker(self):
+ if not PRODUCER.exists():
+ reason = (
+ f"{PRODUCER.name} is not built, so real producer output was NOT "
+ f"checked against the offline checker this run; build it with "
+ f"`make {PRODUCER.relative_to(HERE.parent)}` and re-run"
+ )
+ # A silent skip here reads as a pass, and the thing being skipped is
+ # the only check that the engine and the checker still agree. Say so
+ # on the console as well as in the unittest result.
+ print(f"SKIP: {reason}", file=sys.stderr, flush=True)
+ self.skipTest(reason)
+ digests = {}
+ for framing in self.FRAMINGS:
+ with self.subTest(framing=framing), \
+ tempfile.TemporaryDirectory() as directory:
+ emitted = subprocess.run(
+ [str(PRODUCER), "--emit-round-trip", directory, framing],
+ capture_output=True,
+ text=True,
+ )
+ self.assertEqual(emitted.returncode, 0, emitted.stderr)
+ checked = subprocess.run(
+ [
+ sys.executable,
+ str(VALIDATOR),
+ "--config",
+ f"{directory}/config.json",
+ f"{directory}/manifest.txt",
+ f"{directory}/evidence.jsonl",
+ ],
+ capture_output=True,
+ text=True,
+ )
+ self.assertEqual(
+ checked.returncode,
+ 0,
+ f"the offline checker rejected real producer output "
+ f"({framing}):\n{checked.stdout}{checked.stderr}",
+ )
+ self.assertIn("PASS", checked.stdout)
+ digests[framing] = checked.stdout.split("manifest=")[1].split()[0]
+ # The framings must really differ on disk, or this proves nothing.
+ raw = pathlib.Path(directory, "manifest.txt").read_bytes()
+ if framing == "crlf":
+ self.assertIn(b"\r\n", raw)
+ elif framing == "unterminated":
+ self.assertFalse(raw.endswith(b"\n"))
+ else:
+ self.assertTrue(raw.endswith(b"\n"))
+ self.assertNotIn(b"\r", raw)
+ self.assertEqual(
+ len(set(digests.values())), 1,
+ f"the framings bound different manifest digests: {digests}")
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/c/tests/test_logprob_status.c b/c/tests/test_logprob_status.c
new file mode 100644
index 00000000..ca155ee2
--- /dev/null
+++ b/c/tests/test_logprob_status.c
@@ -0,0 +1,339 @@
+/* test_logprob_status.c β€” the classified logit-row reduction in sample.h and
+ * the evidence digest in evidence_digest.h.
+ *
+ * Both are foundations for engine modes that write artifacts an offline tool
+ * re-checks, so both need to be right for inputs the sampling path never sees:
+ * rows containing NaN or an infinity, empty rows, and a target whose distance
+ * from the row maximum does not fit in a float. A wrong answer there is worse
+ * than a crash, because it is written into a file and read back later as if it
+ * were a measurement.
+ *
+ * Required properties:
+ * P1 AGREEMENT β€” on a row whose logits sit close enough together that a
+ * float-scale subtraction cannot lose precision, the classified path
+ * returns exactly the value and argmax flag the plain logprob_target()
+ * returns. The two paths are not required to agree past a float's own
+ * precision on a widely spread row: logprob_target() keeps its
+ * historical float-scale subtraction, while the classified path
+ * promotes to double first (P1B).
+ * P1B PRECISION β€” the classified path's value is accurate to double
+ * precision: it matches, bit for bit, a reference computed
+ * a second time in this file by promoting the row's logit to double
+ * before subtracting, on rows spanning ordinary, widely spread,
+ * subnormal and full-double-precision inputs. This is the property
+ * P1's exact test rows are too narrow to exercise.
+ * P2 CLASSIFICATION β€” each exceptional row shape reports its own cause, not a
+ * generic failure, no partition function is invented for it, and the
+ * precedence between the causes is the documented one.
+ * P3 PROPAGATION β€” reading a target out of a row that did not reduce cleanly
+ * reports the row's original cause, a caller mistake is reported as such
+ * rather than as a usable value, and every refusal leaves a defined
+ * number behind rather than whatever the caller's variable held.
+ * P4 DIGEST β€” the digest matches published SHA-256 values, including the
+ * lengths that exercise the block boundary and both padding branches, and
+ * the streaming form agrees with the one-shot form.
+ * Exit 0 = all pass.
+ */
+#include <float.h>
+#define main coli_glm_main_unused
+#include "../colibri.c"
+#undef main
+#include "../evidence_digest.h"
+
+static int fails = 0;
+#define CHECK(cond,msg) do{ if(!(cond)){ printf(" FAIL: %s\n", msg); fails++; } \
+ else printf(" ok: %s\n", msg); }while(0)
+
+static int close_enough(double a, double b){
+ double d = a - b;
+ if(d < 0) d = -d;
+ return d <= 1e-12;
+}
+
+/* Tight tolerance for P1B: the classified path's value against an
+ * second double computation should agree to a few ulp; the relative bound leaves no room
+ * for a float-scale rounding to sneak back in while tolerating, at most,
+ * a difference in the last bit of a double reduction. */
+static int close_enough_precise(double a, double b){
+ /* a few units in the last place, relative to the reference magnitude */
+ double tol = 16.0 * DBL_EPSILON * fabs(b) + 1e-300;
+ return fabs(a - b) <= tol;
+}
+
+/* P1: on a row a float subtraction cannot mis-round, the classified path
+ * agrees with the plain path exactly. */
+static void t_agreement(void){
+ printf("P1 agreement with the plain path\n");
+ static const float rows[3][5] = {
+ { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f },
+ { 1.0f, 2.0f, 3.0f, -1.0f, 0.5f },
+ { -30.0f, -31.0f, -29.5f, -100.0f, -29.75f },
+ };
+ for(int r=0;r<3;r++){
+ LogprobRow row;
+ CHECK(logprob_row_checked(rows[r],5,&row)==LOGPROB_FINITE,
+ "finite row reduces to FINITE");
+ for(int target=0;target<5;target++){
+ double classified = 0;
+ CHECK(logprob_from_row_checked(rows[r],target,&row,&classified)==LOGPROB_FINITE,
+ "finite row yields a finite target value");
+ int plain_argmax = 0;
+ double plain = logprob_target(rows[r],5,target,&plain_argmax);
+ CHECK(close_enough(classified,plain),
+ "classified value equals the plain logprob_target value");
+ CHECK(plain_argmax==(row.argmax==target),
+ "classified argmax flag equals the plain argmax flag");
+ }
+ }
+ /* A two-entry uniform row has a hand-checkable answer: log(1/2). */
+ static const float uniform[2] = { 0.0f, 0.0f };
+ LogprobRow row;
+ logprob_row_checked(uniform,2,&row);
+ double value = 0;
+ logprob_from_row_checked(uniform,0,&row,&value);
+ CHECK(close_enough(value,-log(2.0)), "uniform pair gives log(1/2)");
+ CHECK(close_enough(row.logZ,log(2.0)), "uniform pair has logZ = log(2)");
+ CHECK(row.argmax==0, "uniform pair takes the first maximum");
+}
+
+/* A second double computation for P1B (same formula as the code under test, so it
+ * detects float-first rounding, not ulp-level error): re-finds the row's maximum and
+ * log-sum-exp from scratch, entirely in double, without touching the
+ * LogprobRow the function under test already reduced. This is not the same
+ * code path as logprob_from_row_checked() -- it recomputes the row instead
+ * of reading r->max/r->logZ -- so it cannot agree with a wrong answer by
+ * sharing a mistake. */
+static double reference_double(const float *lo, int V, int target){
+ double mx = (double)lo[0];
+ for(int i=1;i<V;i++){ double v=(double)lo[i]; if(v>mx) mx=v; }
+ double se = 0;
+ for(int i=0;i<V;i++) se += exp((double)lo[i]-mx);
+ return (double)lo[target] - (mx + log(se));
+}
+
+static void check_row_precise(const char *name, const float *lo, int V,
+ const int *targets, int ntargets){
+ LogprobRow row;
+ CHECK(logprob_row_checked(lo,V,&row)==LOGPROB_FINITE, name);
+ for(int k=0;k<ntargets;k++){
+ int target = targets[k];
+ double value = 0;
+ CHECK(logprob_from_row_checked(lo,target,&row,&value)==LOGPROB_FINITE,
+ "the target reads back finite");
+ double ref = reference_double(lo,V,target);
+ CHECK(close_enough_precise(value,ref),
+ "the classified path's value agrees with the second double computation to a few ulp");
+ }
+}
+
+/* P1B: the classified path agrees with a second double computation (to a few units in
+ * the last place; ~13 significant digits on the widest rows) on rows P1's own
+ * exact test rows are too narrow to tell apart from a float-rounded answer.
+ * Table covers: an ordinary tightly-spread row; a wide-spread row mirroring
+ * test_ablate_mode.c's t_nll_pin (max 1.0e7, one target's contribution under
+ * half a float ulp at that magnitude); a moderately spread row whose correct
+ * value needs digits past a float's own seven to state; and a subnormal row,
+ * where the magnitudes are so small a float-first subtraction cannot lose
+ * anything, so this also checks the fix introduces no new problem there. */
+static void t_precision(void){
+ printf("P1B classified path agrees with a second double computation to a few ulp\n");
+
+ static const float ordinary[4] = { 2.0f, -1.5f, 0.25f, -3.0f };
+ int t_ordinary[4] = { 0, 1, 2, 3 };
+ check_row_precise("ordinary row reduces to FINITE", ordinary, 4, t_ordinary, 4);
+
+ /* Mirrors test_ablate_mode.c's t_nll_pin: max 1.0e7 at index 0, the
+ * target of interest at index 2 with a logit of 0.25 -- a contribution
+ * the float subtraction lo[2]-max rounds away entirely, since the ulp
+ * near 1.0e7 is 1.0 and 0.25 is under half of it. */
+ static float wide_spread[64];
+ for(int i=0;i<64;i++) wide_spread[i] = -1.0e7f;
+ wide_spread[0] = 1.0e7f;
+ wide_spread[2] = 0.25f;
+ int t_wide[5] = { 0, 1, 2, 5, 63 };
+ check_row_precise("wide-spread row reduces to FINITE", wide_spread, 64, t_wide, 5);
+
+ /* A moderately spread row: max in the tens of thousands, targets with
+ * fractional digits a float at that magnitude cannot all hold. */
+ static const float moderate[5] =
+ { 10000.0f, 0.123456789f, -5000.5f, 9999.99f, 3.14159265f };
+ int t_moderate[5] = { 0, 1, 2, 3, 4 };
+ check_row_precise("moderate-spread row reduces to FINITE", moderate, 5, t_moderate, 5);
+
+ /* Subnormal logits: values this small carry no ulp large enough for a
+ * float-first subtraction to lose, so this is a robustness check as much
+ * as a precision one. */
+ static const float subnormal[3] =
+ { 1e-45f, 1e-45f*3.0f, 1e-45f*7.0f };
+ int t_subnormal[3] = { 0, 1, 2 };
+ check_row_precise("subnormal row reduces to FINITE", subnormal, 3, t_subnormal, 3);
+}
+
+/* P2: every exceptional row shape names its own cause. */
+static void t_classification(void){
+ printf("P2 exceptional rows are classified\n");
+ LogprobRow row;
+
+ float nan_row[3] = { 1.0f, NAN, 3.0f };
+ CHECK(logprob_row_checked(nan_row,3,&row)==LOGPROB_NAN, "NaN present -> NAN");
+ CHECK(row.status==LOGPROB_NAN, "the returned row carries the same status");
+ CHECK(row.logZ==0 && row.max==0, "no partition function is invented");
+
+ float pos_row[3] = { 1.0f, INFINITY, 3.0f };
+ CHECK(logprob_row_checked(pos_row,3,&row)==LOGPROB_POS_INF, "+inf present -> POS_INF");
+
+ float neg_row[3] = { 1.0f, -INFINITY, 3.0f };
+ CHECK(logprob_row_checked(neg_row,3,&row)==LOGPROB_NEG_INF, "-inf present -> NEG_INF");
+
+ /* Precedence, stated as a fixed order rather than the order of appearance.
+ * Each row below contains more than one exceptional class, so a change to
+ * the ranking changes an answer here. */
+ float mixed_row[3] = { NAN, INFINITY, -INFINITY };
+ CHECK(logprob_row_checked(mixed_row,3,&row)==LOGPROB_ALL_NONFINITE,
+ "no finite entry at all -> ALL_NONFINITE, whatever the classes are");
+
+ float finite_nan_pos[3] = { 1.0f, NAN, INFINITY };
+ CHECK(logprob_row_checked(finite_nan_pos,3,&row)==LOGPROB_NAN,
+ "a NaN outranks a positive infinity");
+ float finite_pos_nan[3] = { 1.0f, INFINITY, NAN };
+ CHECK(logprob_row_checked(finite_pos_nan,3,&row)==LOGPROB_NAN,
+ "a NaN outranks a positive infinity that appears before it");
+ float finite_nan_neg[3] = { 1.0f, NAN, -INFINITY };
+ CHECK(logprob_row_checked(finite_nan_neg,3,&row)==LOGPROB_NAN,
+ "a NaN outranks a negative infinity");
+ float finite_pos_neg[3] = { 1.0f, INFINITY, -INFINITY };
+ CHECK(logprob_row_checked(finite_pos_neg,3,&row)==LOGPROB_POS_INF,
+ "a positive infinity outranks a negative one");
+ float finite_neg_pos[3] = { 1.0f, -INFINITY, INFINITY };
+ CHECK(logprob_row_checked(finite_neg_pos,3,&row)==LOGPROB_POS_INF,
+ "a positive infinity outranks a negative one that appears first");
+ float finite_neg[3] = { 1.0f, 2.0f, -INFINITY };
+ CHECK(logprob_row_checked(finite_neg,3,&row)==LOGPROB_NEG_INF,
+ "a negative infinity alongside finite entries -> NEG_INF");
+ float all_nan[2] = { NAN, NAN };
+ CHECK(logprob_row_checked(all_nan,2,&row)==LOGPROB_ALL_NONFINITE,
+ "a row of NaN alone is still ALL_NONFINITE");
+
+ CHECK(logprob_row_checked(NULL,3,&row)==LOGPROB_INVALID, "no row -> INVALID");
+ CHECK(logprob_row_checked(nan_row,0,&row)==LOGPROB_INVALID, "empty row -> INVALID");
+ CHECK(logprob_row_checked(nan_row,-1,&row)==LOGPROB_INVALID, "negative length -> INVALID");
+ CHECK(logprob_row_checked(nan_row,3,NULL)==LOGPROB_NAN,
+ "the status is returned even with no output row");
+
+ CHECK(strcmp(logprob_status_name(LOGPROB_FINITE),"FINITE")==0, "FINITE names itself");
+ CHECK(strcmp(logprob_status_name(LOGPROB_NAN),"NAN")==0, "NAN names itself");
+ CHECK(strcmp(logprob_status_name(LOGPROB_POS_INF),"POS_INF")==0, "POS_INF names itself");
+ CHECK(strcmp(logprob_status_name(LOGPROB_NEG_INF),"NEG_INF")==0, "NEG_INF names itself");
+ CHECK(strcmp(logprob_status_name(LOGPROB_ALL_NONFINITE),"ALL_NONFINITE")==0,
+ "ALL_NONFINITE names itself");
+ CHECK(strcmp(logprob_status_name(LOGPROB_FINITE_OVERFLOW),"FINITE_OVERFLOW")==0,
+ "FINITE_OVERFLOW names itself");
+ CHECK(strcmp(logprob_status_name(LOGPROB_INVALID),"INVALID")==0, "INVALID names itself");
+}
+
+/* P3: a target read out of a bad row reports the row's cause. A spread that
+ * would overflow a float subtraction no longer refuses the target, because
+ * the classified path takes that subtraction in double: it is pinned below
+ * as a status change, not silently absorbed into "still finite". */
+static void t_propagation(void){
+ printf("P3 target reads propagate the row's cause\n");
+ LogprobRow row;
+ float nan_row[3] = { 1.0f, NAN, 3.0f };
+ logprob_row_checked(nan_row,3,&row);
+ double value = 12345.0;
+ CHECK(logprob_from_row_checked(nan_row,0,&row,&value)==LOGPROB_NAN,
+ "a NaN row propagates NAN to the target read");
+ CHECK(isnan(value), "a refused target read leaves a defined value behind");
+
+ CHECK(logprob_from_row_checked(nan_row,0,NULL,&value)==LOGPROB_INVALID,
+ "no row -> INVALID");
+
+ /* A caller mistake on a perfectly good row must not read back as a usable
+ * value: the earlier form returned FINITE here and never wrote the output,
+ * so the caller consumed whatever its own variable happened to hold. */
+ static const float good_row[3] = { 1.0f, 2.0f, 3.0f };
+ LogprobRow good;
+ CHECK(logprob_row_checked(good_row,3,&good)==LOGPROB_FINITE, "the good row reduces");
+ value = 12345.0;
+ CHECK(logprob_from_row_checked(NULL,0,&good,&value)==LOGPROB_INVALID,
+ "no logit vector on a good row -> INVALID, never FINITE");
+ CHECK(isnan(value), "no logit vector leaves a defined value behind");
+ value = 12345.0;
+ CHECK(logprob_from_row_checked(good_row,-1,&good,&value)==LOGPROB_INVALID,
+ "a negative target on a good row -> INVALID, never FINITE");
+ CHECK(isnan(value), "a negative target leaves a defined value behind");
+
+ logprob_row_checked(nan_row,3,&row);
+ CHECK(logprob_from_row_checked(NULL,0,&row,&value)==LOGPROB_INVALID,
+ "no logit vector is a caller mistake even when the row is bad");
+
+ /* The widest representable spread: a float subtraction between these two
+ * values would saturate to infinity even though every logit in the row
+ * is finite. The classified path subtracts in double, so it does not:
+ * this is a real, disclosed widening of what the layer accepts, pinned
+ * here so it cannot regress silently in either direction. */
+ float wide[2] = { FLT_MAX, -FLT_MAX };
+ CHECK(logprob_row_checked(wide,2,&row)==LOGPROB_FINITE, "an extreme finite row still reduces");
+ CHECK(logprob_from_row_checked(wide,1,&row,&value)==LOGPROB_FINITE,
+ "a target the float subtraction could not hold -> FINITE, not refused, under the double subtraction");
+ CHECK(value==-2.0*(double)FLT_MAX,
+ "the saturated target's value is the exact double difference, not an infinity");
+ CHECK(logprob_from_row_checked(wide,0,&row,&value)==LOGPROB_FINITE,
+ "the maximum itself still reads back finite");
+ CHECK(close_enough(value,0.0), "the maximum of a saturated row has log-probability 0");
+
+ logprob_row_checked(nan_row,0,&row);
+ CHECK(logprob_from_row_checked(nan_row,-1,&row,&value)==LOGPROB_INVALID,
+ "a negative target on an invalid row -> INVALID");
+}
+
+/* P4: published SHA-256 answers, chosen to cover the block boundary and both
+ * padding branches (a message whose tail leaves no room for the length field
+ * needs an extra block). */
+static void t_digest(void){
+ printf("P4 evidence digest matches published SHA-256 values\n");
+ static const struct { const char *text; const char *hex; } vectors[] = {
+ { "", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" },
+ { "abc", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" },
+ { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
+ "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" },
+ { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno",
+ "2ff100b36c386c65a1afc462ad53e25479bec9498ed00aa5a04de584bc25301b" },
+ };
+ char out[65];
+ for(unsigned v=0;v<sizeof(vectors)/sizeof(vectors[0]);v++){
+ evidence_sha256_hex(vectors[v].text,strlen(vectors[v].text),out);
+ CHECK(strcmp(out,vectors[v].hex)==0, "one-shot digest matches the published value");
+ CHECK(strlen(out)==64, "the digest is 64 characters and NUL-terminated");
+ }
+
+ /* 65 bytes: one full block plus a remainder, fed one byte at a time, so a
+ * mishandled carry between updates would show up here and not above. */
+ char sixty_five[66];
+ memset(sixty_five,'a',65); sixty_five[65]=0;
+ EvidenceSha256 stream;
+ unsigned char raw[32];
+ evidence_sha256_init(&stream);
+ for(int i=0;i<65;i++) evidence_sha256_update(&stream,sixty_five+i,1);
+ evidence_sha256_final(&stream,raw);
+ char streamed[65];
+ static const char hex[]="0123456789abcdef";
+ for(int i=0;i<32;i++){ streamed[2*i]=hex[raw[i]>>4]; streamed[2*i+1]=hex[raw[i]&15]; }
+ streamed[64]=0;
+ CHECK(strcmp(streamed,
+ "635361c48bb9eab14198e76ea8ab7f1a41685d6ad62aa9146d301d4f17eb0ae0")==0,
+ "a byte-at-a-time stream matches the published value");
+ evidence_sha256_hex(sixty_five,65,out);
+ CHECK(strcmp(streamed,out)==0, "the streaming and one-shot forms agree");
+}
+
+int main(void){
+ printf("test_logprob_status\n");
+ t_agreement();
+ t_precision();
+ t_classification();
+ t_propagation();
+ t_digest();
+ printf(fails ? "FAILED (%d)\n" : "PASSED (%d failures)\n", fails);
+ return fails ? 1 : 0;
+}