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

fix(grammar): the draft walker switched itself off after ~60 repetitions

Suliman Abdulrazzaqfd72ae3ab04f
diff --git a/c/grammar.h b/c/grammar.h
index 47486e62..be6ba6af 100644
--- a/c/grammar.h
+++ b/c/grammar.h
@@ -289,6 +289,12 @@ static int gr__normalize(Grammar *G, GrStack *k, GrState *out, int depth){
if(sy->t==GR_CLS) return gr__set_add(out,k);
if(depth>=GR_MAX_DEPTH) return 0; /* ricorsione sinistra / epsilon-ciclo */
t->s++; /* il chiamante riprende OLTRE il ref */
+ /* A reference that ends its alternate leaves the caller nothing to resume:
+ * pop the caller now instead of when the callee finishes. Kept on the stack,
+ * it cost one frame per repetition of x* / x+ (R ::= I R), so ~60 repetitions
+ * (the 60th byte of a JSON string, the 52nd NDJSON row) hit GR_MAX_DEPTH and
+ * switched the walker off for the rest of the output. */
+ if(t->s>=A->n) k->n--;
GrRule *C=&G->r[sy->ref];
for(int a=0;a<C->n;a++){
if(k->n>=GR_MAX_DEPTH) return 0;
diff --git a/c/tests/test_grammar.c b/c/tests/test_grammar.c
index 6603f74d..52073e74 100644
--- a/c/tests/test_grammar.c
+++ b/c/tests/test_grammar.c
@@ -143,6 +143,34 @@ int main(void){
CHECK(gr_forced(&S,buf,sizeof buf)==6 && !memcmp(buf,"\"id\":\"",6));
gr_free(&G);
+ /* repetition is not capped by GR_MAX_DEPTH: every repeat of x* / x+ used to keep
+ * one finished caller frame, so the walker switched itself off at the 52nd row of
+ * this NDJSON grammar and at the 62nd byte of the string below, and drafted
+ * nothing after */
+ CHECK(gr_parse(&G,
+ "root ::= riga+\n"
+ "riga ::= \"{\\\"id\\\":\\\"\" chiave \"\\\",\\\"fit_category\\\":\\\"\" cat \"\\\"}\" \"\\n\"\n"
+ "chiave ::= [a-z0-9-]+\n"
+ "cat ::= \"no_fit\" | \"partial_fit\" | \"good_fit\"\n")==0);
+ gr_state_init(&S,&G);
+ for(int r=0;r<200;r++){
+ char riga[96];
+ int len=snprintf(riga,sizeof riga,"{\"id\":\"ocds-%d\",\"fit_category\":\"good_fit\"}\n",r);
+ CHECK(feed(&S,riga)==len);
+ }
+ CHECK(S.alive && feed(&S,"{")==1);
+ CHECK(gr_forced(&S,buf,sizeof buf)==6 && !memcmp(buf,"\"id\":\"",6));
+ gr_free(&G);
+
+ CHECK(gr_parse(&G,"root ::= \"\\\"\" [a-z ]* \"\\\"\"")==0);
+ gr_state_init(&S,&G);
+ char lungo[402];
+ lungo[0]='\"'; memset(lungo+1,'a',400); lungo[401]=0;
+ CHECK(feed(&S,lungo)==401 && S.alive);
+ CHECK(feed(&S,"\"")==1);
+ CHECK(gr_admissible(&S,m,&end)==0 && end==1);
+ gr_free(&G);
+
puts("test_grammar: ok");
return 0;
}