Abstract
This paper presents a purely declarative framework for machine learning (ML) implemented in Datalog. By introducing formal semantics for recursive aggregates through the Pre-Computable Cardinality (PCC) property, the authors decouple high-level model specification from distributed system execution concerns. Built as an extension of BigDatalog on Apache Spark, the system achieves significant performance gains—often 2x to 4x faster than Spark’s MLlib—and demonstrates superior scalability on high-dimensional sparse datasets where traditional imperative frameworks like PyTorch encounter memory limitations.
Introduction
Scaling machine learning algorithms on distributed platforms such as Hadoop or Spark typically requires developers to manage intricate implementation details, including data partitioning, communication protocols, and iterative state management. While libraries like MLlib provide some abstraction, they still impose an imperative "how-to" programming style that tightly couples the algorithm's logic with the underlying system's execution model.
The authors advocate for a "what-to" approach using Datalog, a declarative logic programming language. Historically, Datalog has been highly effective for recursive graph queries but has struggled with machine learning. The primary obstacle is that ML requires performing mathematical aggregates (like SUM or AVG) within recursive loops. Standard Datalog semantics often forbid this because it can lead to non-monotonic behavior—where adding more data might invalidate a previous result—making it impossible to reach a stable solution (a fixpoint).
Methods and Formal Semantics
The researchers propose a framework that treats ML training as a recursive Datalog query, supported by three key pillars:
1. The PCC Property
They introduce the Pre-Computable Cardinality (PCC) property. They prove that if the number of elements being aggregated is known before a recursive step begins—a condition met by most ML training iterations—the aggregates become monotonic. This provides the necessary mathematical foundation to safely use SUM, COUNT, and AVG inside recursive loops.
2. BigDatalog on Spark
The system is implemented as an extension of BigDatalog, a shared-nothing Datalog engine running on Apache Spark. This allows the framework to leverage Spark’s distributed execution capabilities while maintaining Datalog’s succinct syntax.
3. Optimized Recursion and Data Flow
The paper introduces a Mutual Recursion Operator (MRO) to handle complex ML structures where multiple relations (e.g., the model, the gradient, and the predictions) depend on each other simultaneously. Additionally, "smart-replica" optimizations were developed to minimize expensive data shuffles by pre-partitioning base tables in multiple ways.
Evaluation and Key Insights
The framework was evaluated using four large-scale public datasets from the LIBSVM benchmark, focusing on high-dimensional sparse data: URL (2.4M instances), KDD10 (19.3M instances), KDD12 (149.6M instances), and Webspam (350K instances).
ML as Logic
The core insight is that the iterative nature of Gradient Descent is essentially a recursive logic problem. By expressing model updates as recursive rules, the engine can automatically optimize data flow. Because ML iterations typically occur over a fixed set of training data, the "non-monotonic" danger of aggregates disappears, allowing for strictly declarative formal semantics.
Performance and Scalability
- Speed: The Datalog-based system was 2x to 4x faster than Spark’s MLlib and significantly outperformed SystemML across various linear models, including Linear Regression, Logistic Regression, and SVM.
- Memory Efficiency: On the massive KDD12 dataset, the Datalog approach successfully completed training while PyTorch ran out of memory (OOM). The Datalog engine's join optimizations proved more memory-efficient for high-dimensional sparse data than the parameter-server model used by PyTorch.
- Succinctness: Complex ML workflows were expressed in just a few Datalog rules, which are significantly shorter and easier to reason about than equivalent imperative code in Scala or Python.
Conclusion and Future Work
While the current system excels at linear models, the authors acknowledge that deep learning requires further research into efficient matrix-multiplication optimizations within Datalog. Future work includes extending the compiler to generate code for GPUs/TPUs and developing a more familiar DataFrame API to improve usability for data scientists accustomed to Python-based workflows. By bridging the gap between logic and learning, this work demonstrates that declarative programming can offer both formal rigor and industry-leading performance.
Summary updated