TL;DR
Translating plain English into the newly standardized ISO/IEC 39075 graph query language (GPML) usually fails because public datasets ignore its subtle path-matching rules and test on overly simple graphs where sloppy queries accidentally get the right answer. This paper builds an end-to-end training and constrained-decoding stack that teaches tiny, local language models (350M and 1.3B parameters) to write precise, zero-hallucination GPML queries—running inside less than 400 MiB of RAM.
What Problem Does This Paper Address?
For years, querying graph databases meant speaking vendor-specific dialects like Cypher. Recently, ISO standardized the Graph Pattern Matching Language (GPML) inside GQL and SQL/PGQ. Instead of one rigid way to traverse edges, GPML lets you mix and match four path restrictors (WALK, TRAIL, ACYCLIC, SIMPLE—controlling whether nodes or edges can repeat) with four selectors (ALL, ANY, ALL SHORTEST, ANY SHORTEST), yielding 12 distinct behaviors on real-world graphs with loops and parallel edges.
Teaching Small Language Models (SLMs)—the kind small enough to run privately on-premise—to translate English into GPML hits three walls:
- No training data & "easy" test graphs: Public Text-to-Cypher datasets contain zero GPML keywords. Worse, if a benchmark graph is a simple tree or Directed Acyclic Graph (DAG), forgetting to write
ACYCLICorSHORTESTaccidentally returns the exact same answer because loops don't exist anyway! - Memorization & catastrophic forgetting: Fine-tuning a compact model on a fixed set of synthetic schemas causes it to memorize literal strings (like
code = "MAR-71") rather than reading the prompt, while ruining its general coding abilities. - Decoding quirks: Standard LLM repetition penalties punish models for legitimately reusing variable names across
MATCH,WHERE, andRETURNclauses, mutating valid identifiers into gibberish.
Methods
The authors tackle data, training, and inference from first principles:
- Combinatorial Infinite-Schema Synthesis: Instead of reusing a small pool of schemas, every single training row (~53k rows) gets a freshly generated schema from a pool of over $3 \times 10^7$ combinations, forcing the model to copy schema names from context rather than memory. Prompts are formatted byte-for-byte to match the target database's (
samyama-graph) live prompt pipeline. - Orthogonal Subspace Merging (KnOTS) & Step-GRPO: To add graph skills without erasing Python/Rust coding skills, the authors align the graph LoRA adapter and code LoRA adapter in a shared singular subspace using Cyclic Jacobi SVD before merging, followed by reinforcement learning (Step-GRPO) rewarded by live database execution.
- Dialect-Shift Distillation ($\beta_{\mathrm{anc}} = 0.0$): When distilling the 1.3B teacher into the 350M student, standard anchoring to the old student suppresses brand-new GPML keywords (to which the old student assigns near-zero probability). Dropping the anchor weight ($\beta_{\mathrm{anc}} = 0.0$) while keeping advantage-gated teacher guidance lets the tiny model adopt the new grammar.
- Dual-Mode Constrained Decoding: At runtime, the model can run in two ways without modifying the core database:
Mode A (In-Process) links directly to the database catalog in Rust using Schema-FSM v5.8 (a finite-state machine enforcing valid GPML syntax and schema topology) plus an MVCC execution gate. Mode B (Wire-Protocol Server) acts as a drop-in Ollama/OpenAI replacement using a 4-state BPE lexer mask that restricts quotes to words actually in the user's question and restricts labels/properties to the parsed schema.
Datasets
Because existing benchmarks fall prey to DAG degeneracy, the authors built two clean-room, Apache-2.0 benchmarks verified against both the SIGMOD 2022 GPML reference evaluator and samyama-graph:
- Samyama-GPML-Bench-100 (v2): 100 tasks across 10 GPML path families built specifically over witness multigraphs (containing cycles, self-loops, and parallel edges) so that naive "foil" queries omitting restrictors score 0.0%.
- Samyama-RealKG-NLQ-Bench-160: 160 end-to-end tasks across 8 enterprise domains (Clinical Trials, Biomed, FinRisk, PowerGrid, etc.).
They also evaluate against a 913-problem SHA-256 decontaminated suite including MBPP-Sanitized, HumanEval, Rustc-Exec-64, Cypher-Bench-100, Samyama-Ext-Bench-60, and the in-process Samyama-InProc-36 suite.
Key Insights and Ideas
- Acyclic Witness Degeneracy (Proposition 1): On simple DAGs, all 16 GPML path modes collapse to the exact same output. You must engineer cyclic multigraphs with diamond ties and self-loops to know if an LLM truly understands graph path semantics.
- Lower token loss $\neq$ better execution: Watching validation execution every 0.25 epochs revealed accuracy peaking at 1.25 epochs (95.83%) and dropping afterward, even as cross-entropy token loss kept smoothly falling. Quarter-epoch "slow-fitting" catches this sweet spot.
repetition_penaltybreaks query languages: A tiny penalty of1.05divides logits of repeated column names, causing BPE tokenizers to hallucinate spaces or mutated suffixes (Reservities). Settingrepetition_penalty = 1.0fixes this for free.- Non-empty rows can still be hallucinations: Checking
!rows.is_empty()isn't enough to verify a query. A contradictoryWHERE a.fuel = "Solar" AND a.fuel = "Hydro" RETURN count(a)happily returns one row:[0]. Catching zero-counts and unrequested join fanouts closed subtle false-positive traps.
Key Results
- Unconstrained GPML (Zero FSM):
Shoonya-Cypher-1.3B-v2.4jumps from 0.0% to 72.0% onGPML-Bench-50(64.0% onGPML-Bench-100 v2), while the tiny350M-v1.5reaches 54.0%. - Prompt Alignment Payoff: Training on the exact prose prompt format boosts unconstrained
RealKG-NLQ-160by 59.5$\times$ (0.62% $\rightarrow$ 36.88%) on 1.3B. - In-Process Perfection (Mode A): With Schema-FSM v5.8 and MVCC verification,
350M-v1.5solves 100.0% (36/36) ofSamyama-InProc-36at 64.0 tok/s inside just 390.3 MiB INT8 RAM, with 0.0% false-positive leaks. - Wire-Protocol Server (Mode B):
shoonya-nlq-serverhits 80.0% (1.3B) and 60.0% (350M) on unseen NLQPipeline GPML tasks while preserving general Python coding (33.46%on MBPP-Sanitized for 350M).
Limitations and Future Work
- Unconstrained vs. Constrained Gap: Without live catalog/lexer masking, raw zero-FSM OpenCypher accuracy on unseen complex schemas remains modest (20.0% on
Cypher-Bench-100and 36.88% onRealKG-NLQ-160for the 1.3B model), meaning production setups should always run Mode A or Mode B constraints. - Read-Only Scope: Both decoding modes strictly block mutating queries (
CREATE,MERGE,SET,DELETE) by design. - Unweighted Paths Only: Current benchmarks test hop-count shortest paths; supporting arbitrary weighted edge cost functions (
COST) is left for future work.
Summary updated