--- license: mit language: - en tags: - math - symbolic-math - parser - neuro-symbolic - arithmetic - prefix-notation library_name: pytorch pipeline_tag: text-generation --- # Vidai: Neural Mathematical Parsing with Deterministic Computation > **Work in Progress**: This is an early release demonstrating a novel architecture. The model requires more diverse training data to handle complex expressions reliably. We welcome contributions and feedback. ## Why This Architecture Transformers excel at sequence-to-sequence transformation and pattern recognition. Vidai uses them for exactly that: converting mathematical notation into structured tree representations. We don't ask the model to "learn" arithmetic facts like 7 × 8 = 56 from text tokens alone. **The key insight**: Mathematical expressions have inherent tree structure. `x^2 + 3*y` is not just a sequence of characters; it's a tree where `+` is the root with `x^2` and `3*y` as children. Vidai's encoder learns to recognize this structure and output prefix notation (`+ ** x 2 * 3 y`), which explicitly encodes the tree. **The architecture**: - **ContextEncoder**: Processes input characters with tree-depth embeddings, helping the model understand expression structure - **SymbolicParserDecoder**: Generates prefix notation tokens representing the parsed tree - **SymPy Bridge**: Converts prefix notation to symbolic expressions for deterministic evaluation This separation means the transformer does what it's good at (pattern recognition, sequence transformation) while symbolic engines handle what they're good at (exact computation). **Read more**: [Teaching Machines Arithmetic: The Vidai Journey](https://rajeshrs.in/blog/ai-explorations/posts/2026-01-04-vidai-teaching-machines-arithmetic/) ## Current Status Vidai (Tamil for "answer") is a neuro-symbolic system that parses mathematical notation into prefix notation trees, then delegates computation to symbolic engines (SymPy). ## Model Description - **Architecture**: Encoder-decoder transformer (44.6M parameters) - **Input**: Character-level ASCII (vocabulary size 256) - **Output**: Prefix notation tokens (vocabulary size 64) - **Computation**: Deterministic via SymPy (0 learned parameters) | Component | Parameters | |-----------|------------| | ContextEncoder | 19M | | SymbolicParserDecoder | 25M | | TreeComputeModule | 0 | ## Usage ```python import torch from vidai.model.tree_compute import TreeComputeTransformer, TreeComputeConfig from vidai.data.tokenizer import ParserTokenizer # Load model ckpt = torch.load('parser_v4_best.pt', map_location='cpu', weights_only=False) config = TreeComputeConfig(**ckpt['config']['model_config']) model = TreeComputeTransformer(config) model.load_state_dict(ckpt['model_state_dict']) model.eval() # Parse expression tokenizer = ParserTokenizer() text = "x^2 + 3*y" input_ids = torch.tensor([tokenizer.encode_input(text)]) input_mask = (input_ids != 0).bool() with torch.no_grad(): output_ids = model.parse(input_ids, input_mask, max_len=64) prefix = tokenizer.decode_output(output_ids[0].tolist()) print(f"{text} -> {prefix}") # Output: x^2 + 3*y -> + ** x 2 * 3 y ``` ## CLI Usage ```bash pip install vidai vidai parse "x^2 + 3*y" vidai parse "3 + 5 * 2" --eval vidai parse "x^2 + y" --eval x=3 y=4 ``` ## Training Data Trained on 2M synthetic examples with mixed notation formats: - 30% explicit parentheses with spaces - 15% explicit parentheses without spaces - 20% unparenthesized with spaces - 10% unparenthesized without spaces - 25% edge cases (Unicode, negatives, trig functions) ## Evaluation Results | Category | Accuracy | |----------|----------| | Basic arithmetic | 100% | | **Parenthesized expressions** | **100%** | | Trig functions (sin, cos, tan) | 100% | | Unicode sqrt | 100% | | Left associativity | 100% | | Operator precedence (no parens) | 86% | | Extended variables (r, c, d, v, g) | 91% | | **Overall on targeted patterns** | **90.8%** | ## Important: Use Parentheses for Reliable Results **For 100% accuracy, always use explicit parentheses around:** 1. **Function calls**: `(sqrt(16)) + 2` not `sqrt(16) + 2` 2. **Exponents in expressions**: `(x^2) + y` not `x^2 + y` 3. **Complex expressions**: `((a + b)) * (c - d)` not `a + b * c - d` **Examples:** | Expression | Reliable? | Recommendation | |------------|-----------|----------------| | `sqrt(16) + 2^3` | ❌ May fail | `(sqrt(16)) + (2^3)` | | `x^2 - 3*y + pi` | ❌ May fail | `((x^2) - (3*y)) + pi` | | `(sqrt(16)) + (2^3)` | ✅ Works | Already parenthesized | | `3 + 5` | ✅ Works | Simple expressions are fine | ## Current Limitations > **This model needs more training data.** The architecture is sound, but the training dataset lacks diversity. | What Works | What Doesn't (Yet) | |------------|-------------------| | Simple expressions: `3 + 5 * 2` | Deep nesting: `sqrt(sqrt(sqrt(x)))` | | Parenthesized: `(x^2) + (3*y)` | Long polynomials (4+ terms) | | Single functions: `sin(x)`, `sqrt(16)` | Chained functions: `log(exp(sqrt(x)))` | | Basic precedence | Complex precedence without parens | **Known Issues:** - Unparenthesized expressions fail ~14% of the time - Function calls without outer parens: `sqrt(x) + y` may be misparsed - Deep nesting (3+ levels) often fails - Long expressions get truncated - Capital letters not in vocabulary ## What's Needed: Training Data The architecture can handle complex expressions, but needs: 1. **More diverse synthetic data** - especially deeply nested functions 2. **Edge cases** - operator precedence without parentheses 3. **Real-world expressions** - from textbooks, papers, code **Contributing**: If you'd like to help improve Vidai, see our [GitHub repository](https://github.com/aiexplorations/vidai) for data generation scripts. ## Model Files - `parser_v4_best.pt` - Best V4 checkpoint (pre-training) - `finetune_v1_step3500.pt` - Fine-tuned on targeted patterns - `config.json` - Model configuration - `tokenizer.json` - Output vocabulary ## Citation ```bibtex @software{vidai2026, author = {Sampathkumar, Rajesh}, title = {Vidai: Neural Mathematical Parsing with Deterministic Computation}, year = {2026}, url = {https://github.com/aiexplorations/vidai} } ``` ## License MIT License