Abstract
Finding errors in machine learning applications requires a thorough exploration of their behavior over data. Existing approaches used by practitioners are often ad-hoc and lack the abstractions needed to scale this process. We present TorchQL, a programming framework to evaluate and improve the correctness of machine learning applications. TorchQL allows users to write queries to specify and check integrity constraints over machine learning models and datasets. It seamlessly integrates relational algebra with functional programming to allow for highly expressive queries using only eight intuitive operators. We evaluate TorchQL on diverse use-cases including finding critical temporal inconsistencies in objects detected across video frames in autonomous driving, finding data imputation errors in time-series medical records, finding data labeling errors in real-world images, and evaluating biases and constraining outputs of language models. Our experiments show that TorchQL enables up to 13x faster query executions than baselines like Pandas and MongoDB, and up to 40% shorter queries than native Python. We also conduct a user study and find that TorchQL is natural enough for developers familiar with Python to specify complex integrity constraints.
Introduction
Machine learning models often fail in ways that standard performance metrics, such as accuracy or mean Average Precision (mAP), do not capture. For instance, a vision model might exhibit high overall accuracy but fail a "common sense" test by detecting a pedestrian in two consecutive frames and then completely losing them in the third. Identifying these "integrity violations" is currently a significant challenge for practitioners, who often resort to writing ad-hoc Python scripts that are difficult to scale, optimize, and reuse. Existing database tools like Pandas or MongoDB frequently struggle with the complex, unstructured data types common in ML, such as tensors and bounding boxes, or require rigid schemas that hinder rapid iteration.
The TorchQL Framework
TorchQL is a domain-specific language designed to bridge the gap between relational algebra and functional programming. Unlike traditional SQL, TorchQL treats arbitrary Python objects as first-class citizens, allowing users to query directly over PyTorch tensors or custom class instances without the need for complex data transformation.
The framework is built around eight intuitive operators—including join, filter, project, and flatten—that enable the construction of complex queries. Most of these operators are powered by User-Defined Functions (UDFs), allowing developers to drop any Python function, including calls to other ML models, directly into a query.
Under the hood, TorchQL optimizes execution using hash-based joins to achieve $O(M+N)$ complexity, significantly outperforming the $O(MN)$ complexity of naive nested loops. It also integrates natively with PyTorch’s Dataset and DataLoader for efficient batch processing.
Applications and Insights
The central insight of TorchQL is that many ML failures are violations of logical properties. By expressing these properties as queries, developers can automate the search for "needles in the haystack" of massive datasets.
Beyond debugging, TorchQL supports Interactive Refinement. Because queries are concise, developers can use them interactively, starting with broad checks (e.g., "changing counts of people") and refining them into specific "object permanence" constraints. Furthermore, TorchQL can be used for Constrained Generation at runtime. By wrapping Large Language Model (LLM) calls in a query that filters for specific constraints—such as regex patterns or data types—it can significantly improve the reliability of model outputs.
Evaluation and Results
The researchers evaluated TorchQL across five distinct domains, including temporal inconsistencies in Cityscapes video frames (15,000 frames), data imputation errors in Physionet-2012 medical records, and adjective-profession biases in LLMs using the Alpaca dataset.
Key results include:
- Performance: TorchQL achieved up to 13x faster query execution than baselines like Pandas and MongoDB, particularly for complex joins over large video datasets.
- Conciseness: Queries were up to 40% shorter in terms of token count compared to equivalent native Python code.
- Error Discovery: In the Cityscapes dataset, a simple TorchQL query identified 71 objects missed by a state-of-the-art model with 62.83% precision.
- LLM Steering: When used to constrain LLM outputs, TorchQL was up to 11x faster than LMQL while achieving higher task accuracy on reasoning benchmarks.
- Usability: In a user study, developers familiar with Python were able to learn the system and write complex constraints in under an hour.
Conclusion and Future Work
TorchQL demonstrates that many ML debugging tasks can be effectively framed as database query problems. While currently limited by in-memory execution and single-core query processing, future work aims to incorporate automated query synthesis using LLMs and support for recursive operators to allow for more advanced optimizations.
Summary updated