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(schema): a nested array schema doubled the compiled grammar per level

Suliman Abdulrazzaqb7fcfd2a88bd
diff --git a/c/schema_gbnf.h b/c/schema_gbnf.h
index f64e1f0b..c89403c5 100644
--- a/c/schema_gbnf.h
+++ b/c/schema_gbnf.h
@@ -33,9 +33,13 @@
#define SGB_MAX_DEPTH 32
+/* an array item that holds an array, compiled once as rule jitem<id> after root */
+typedef struct { jval *sc; int depth; } SgbItemRule;
+
typedef struct {
char *s; size_t len, cap; /* output GBNF text */
int nrule; /* next composite rule id */
+ SgbItemRule *items; int items_cap; /* composite rule bodies, indexed by id */
int use_str, use_num, use_int; /* shared terminal rules actually referenced */
char err[160];
int fail;
@@ -142,23 +146,57 @@ static void sgb_object(SgbCtx *C, jval *sc, int depth){
sgb_put(C, " \"}\"");
}
+/* true when a schema node is an array, or an object with an array below it */
+static int sgb_has_array(jval *sc, int depth){
+ if (!sc || sc->t != J_OBJ || depth > SGB_MAX_DEPTH) return 0;
+ jval *ty = json_get(sc, "type");
+ if (!ty || ty->t != J_STR) return 0;
+ if (!strcmp(ty->str, "array")) return 1;
+ jval *props = json_get(sc, "properties");
+ if (strcmp(ty->str, "object") || !props || props->t != J_OBJ) return 0;
+ for (int i = 0; i < props->len; i++)
+ if (sgb_has_array(props->kids[i], depth + 1)) return 1;
+ return 0;
+}
+
+static void sgb_item(SgbCtx *C, jval *items, int depth, const char *rule){
+ if (rule[0]) sgb_put(C, rule); else sgb_value(C, items, depth);
+}
+
+/* An array names its item grammar twice (the first item, then each ","-item).
+ * Inlining an item that holds an array at both places doubled everything below
+ * it at every nesting level: 2^depth copies of the innermost item. Such an item
+ * is queued as one composite rule, emitted after the root rule, and referenced
+ * by name. Any other item is inlined as before and costs no extra rule. */
static void sgb_array(SgbCtx *C, jval *sc, int depth){
jval *items = json_get(sc, "items");
jval *mi = json_get(sc, "minItems");
int min1 = mi && mi->t == J_NUM && mi->num >= 1;
if (mi && mi->t == J_NUM && mi->num > 1){ sgb_fail(C, "minItems > 1"); return; }
if (!items){ sgb_fail(C, "array without items"); return; }
+ char rule[32] = "";
+ if (sgb_has_array(items, depth + 1)){
+ if (C->nrule == C->items_cap){
+ int nc = C->items_cap ? C->items_cap * 2 : 8;
+ SgbItemRule *ni = (SgbItemRule *)realloc(C->items, (size_t)nc * sizeof *ni);
+ if (!ni){ C->fail = 1; return; }
+ C->items = ni; C->items_cap = nc;
+ }
+ C->items[C->nrule].sc = items;
+ C->items[C->nrule].depth = depth + 1;
+ snprintf(rule, sizeof rule, "jitem%d", C->nrule++);
+ }
if (min1){
sgb_put(C, "\"[\" jws ");
- sgb_value(C, items, depth + 1);
+ sgb_item(C, items, depth + 1, rule);
sgb_put(C, " jws ( \",\" jws ");
- sgb_value(C, items, depth + 1);
+ sgb_item(C, items, depth + 1, rule);
sgb_put(C, " jws )* \"]\"");
} else {
sgb_put(C, "\"[\" jws ( ");
- sgb_value(C, items, depth + 1);
+ sgb_item(C, items, depth + 1, rule);
sgb_put(C, " jws ( \",\" jws ");
- sgb_value(C, items, depth + 1);
+ sgb_item(C, items, depth + 1, rule);
sgb_put(C, " jws )* )? \"]\"");
}
}
@@ -225,6 +263,15 @@ static char *schema_to_gbnf(const char *schema_json, char *err, int errsz){
sgb_put(&C, "root ::= jws ");
sgb_value(&C, sc, 0);
sgb_put(&C, " jws\n");
+ /* item rules queued by sgb_array; compiling one can queue the next (a deeper
+ * array), so nrule grows while this loop runs */
+ for (int i = 0; i < C.nrule && !C.fail; i++){
+ char head[40]; snprintf(head, sizeof head, "jitem%d ::= ", i);
+ sgb_put(&C, head);
+ sgb_value(&C, C.items[i].sc, C.items[i].depth);
+ sgb_put(&C, "\n");
+ }
+ free(C.items);
sgb_put(&C, "jws ::= ( \" \" | \"\\t\" | \"\\n\" | \"\\r\" )*\n");
if (C.use_str)
sgb_put(&C, "jstr ::= \"\\\"\" jchar* \"\\\"\"\n"
diff --git a/c/tests/test_schema_gbnf.c b/c/tests/test_schema_gbnf.c
index da857b4e..0f9a6c32 100644
--- a/c/tests/test_schema_gbnf.c
+++ b/c/tests/test_schema_gbnf.c
@@ -149,6 +149,68 @@ int main(void){
gr_free(&G);
}
+ /* 9. nested arrays: an item that holds an array is emitted once as a rule and
+ * referenced twice, so the GBNF grows linearly with nesting. Inlined at both
+ * places it doubled per level: 8 levels overflowed GR_MAX_RULES (no grammar
+ * at all) and a 567-byte schema of 22 levels compiled to 180 MB. */
+ {
+ char sc[1024], err[160];
+ char *flat = schema_to_gbnf("{\"type\":\"array\",\"items\":{\"type\":\"string\"}}", err, sizeof err);
+ CHECK(flat && !strstr(flat, "jitem")); /* a scalar item stays inline */
+ free(flat);
+ size_t len[17] = {0};
+ for (int depth = 1; depth <= 16; depth++){
+ sc[0] = 0;
+ for (int i = 0; i < depth; i++) strcat(sc, "{\"type\":\"array\",\"items\":");
+ strcat(sc, "{\"type\":\"string\"}");
+ for (int i = 0; i < depth; i++) strcat(sc, "}");
+ char *g = schema_to_gbnf(sc, err, sizeof err);
+ CHECK(g != NULL);
+ if (g) len[depth] = strlen(g);
+ free(g);
+ }
+ for (int depth = 2; depth <= 16; depth++) /* one rule line per level */
+ CHECK(len[depth] - len[depth - 1] < 100);
+ CHECK(len[16] < 4096);
+
+ Grammar G; GrState S;
+ unsigned char mask[32]; int can_end = 0;
+ const char *sc8 = "{\"type\":\"array\",\"items\":{\"type\":\"array\",\"items\":"
+ "{\"type\":\"array\",\"items\":{\"type\":\"array\",\"items\":"
+ "{\"type\":\"array\",\"items\":{\"type\":\"array\",\"items\":"
+ "{\"type\":\"array\",\"items\":{\"type\":\"array\",\"items\":"
+ "{\"type\":\"string\"}}}}}}}}}";
+ int ok = compile(sc8, &G, NULL, 0) == 0;
+ CHECK(ok);
+ if (ok){
+ gr_state_init(&S, &G);
+ const char *inst = "[[[[[[[[\"a\",\"b\"],[]]]]]]]]";
+ CHECK(walk(&S, inst) == (int)strlen(inst));
+ gr_admissible(&S, mask, &can_end);
+ CHECK(can_end == 1);
+ gr_state_init(&S, &G);
+ CHECK(walk(&S, "[[[[[[[\"a\"]]]]]]]") == 7); /* a string one level too shallow */
+ gr_free(&G);
+ }
+
+ /* array of objects that hold arrays: forced key spans survive the rule */
+ const char *nested = "{\"type\":\"array\",\"items\":{\"type\":\"object\",\"properties\":{"
+ "\"tags\":{\"type\":\"array\",\"items\":{\"enum\":[\"x\",\"y\"]}}},\"required\":[\"tags\"]}}";
+ ok = compile(nested, &G, NULL, 0) == 0;
+ CHECK(ok);
+ if (ok){
+ gr_state_init(&S, &G);
+ CHECK(walk(&S, "[{\"") == 3);
+ char f[64]; int n = gr_forced(&S, f, sizeof f);
+ CHECK(n > 0 && strncmp(f, "tags\"", 5) == 0);
+ const char *tail = "tags\":[\"x\",\"y\"]},{\"tags\":[]}]";
+ CHECK(walk(&S, tail) == (int)strlen(tail));
+ gr_admissible(&S, mask, &can_end);
+ CHECK(can_end == 1);
+ gr_free(&G);
+ }
+ }
+
if (fails){ printf("test_schema_gbnf: %d FAILED\n", fails); return 1; }
printf("test_schema_gbnf: OK\n");
return 0;