Introduction
Cognitive scientists utilize computational models to simulate mental processes, aiming to understand human behavior and inform artificial intelligence. However, human behavior is inherently stochastic, requiring these models to be executed hundreds of thousands of times to generate statistically significant distributions of outcomes.
The current landscape of cognitive modeling is dominated by Python-based frameworks such as PsyNeuLink, PyTorch, and NEURON. While Python facilitates rapid prototyping, its dynamic nature introduces substantial performance bottlenecks. Existing solutions like general-purpose JIT compilers often fail to optimize across framework boundaries or handle the massive memory overhead of complex simulations. This paper introduces Distill, a domain-specific compiler designed to eliminate these inefficiencies by leveraging LLVM to bridge the gap between high-level modeling and low-level performance.
The Distill Compilation Pipeline
Distill leverages the fact that while cognitive models are constructed dynamically, their execution graph is typically static. The pipeline consists of several key stages:
- Type and Shape Inference: By utilizing the initial "sanitization" pass of the modeling framework, Distill statically determines the types and shapes of all variables.
- Static Conversion: Dynamic Python structures are converted into efficient, statically-defined LLVM structs and arrays.
- Unified Intermediate Representation (IR): Computations from disparate libraries (NumPy, PyTorch, etc.) are lowered into a single LLVM IR, enabling cross-framework optimizations.
- Auto-Parallelization: By bypassing the Python Global Interpreter Lock (GIL), Distill enables multi-threaded CPU execution and GPU offloading via LLVM’s NVPTX backend.
Experimental Evaluation
The authors evaluated Distill across a suite of standard cognitive models, including the Necker Cube perception model, Botvinick Stroop conflict models, and a complex Predator-Prey agent simulation.
Distill achieved an average speedup of 26x, with a peak performance gain of 778x on the Botvinick Stroop model. Notably, the Predator-Prey XL model, which was unrunnable in standard CPython (failing to finish within 24 hours), completed in just 4.4 seconds using Distill. Furthermore, Distill's automatic parallelization provided an additional 6.3x speedup on NVIDIA GPUs over the serial compiled version.
Static Analysis for Model Optimization
A unique capability of Distill is its ability to perform "science" at the compiler level. By extending LLVM's Value Range Propagation (VRP) to floating-point operations, Distill can analytically deduce model parameter bounds and convergence times without executing the code.
In the Predator-Prey model, Distill used this "adaptive mesh refinement" to find optimal attention parameters in just 7 rounds of compiler analysis, effectively replacing hundreds of thousands of traditional simulation runs. This demonstrates that the compiler can do more than just accelerate code; it can actively participate in the scientific discovery process by analytically exploring the parameter space.
Limitations and Future Work
While Distill shows immense promise, GPU performance was constrained by memory pressure from replicating Pseudo-Random Number Generator (PRNG) states to ensure stochastic reproducibility. Future work involves validating GPU-friendly PRNGs with cognitive scientists to ensure scientific integrity. Additionally, the team plans to implement advanced "clone detection" to automatically simplify mathematically equivalent model components, further reducing computational requirements and enhancing the scalability of cognitive simulations.
Summary updated