Abstract
We explore different approaches to integrating a simple convolutional neural network (CNN) with the Lucene search engine in a multi-stage ranking architecture. Our models are trained using the PyTorch deep learning toolkit, which is implemented in C/C++ with a Python frontend. One obvious integration strategy is to expose the neural network directly as a service. For this, we use Apache Thrift, a software framework for building scalable cross-language services. In exploring alternative architectures, we observe that once trained, the feedforward evaluation of neural networks is quite straightforward. Therefore, we can extract the parameters of a trained CNN from PyTorch and import the model into Java, taking advantage of the Java Deeplearning4J library for feedforward evaluation. This has the advantage that the entire end-to-end system can be implemented in Java. As a third approach, we can extract the neural network from PyTorch and "compile" it into a C++ program that exposes a Thrift service. We evaluate these alternatives in terms of performance (latency and throughput) as well as ease of integration. Experiments show that feedforward evaluation of the convolutional neural network is significantly slower in Java, while the performance of the compiled C++ network does not consistently beat the PyTorch implementation.
Introduction
In modern information retrieval, multi-stage ranking architectures have become standard. Typically, an initial stage uses an efficient engine like Lucene with BM25 to retrieve a candidate set of documents. A subsequent stage employs complex neural models to rerank these candidates. A significant engineering challenge arises because Lucene is Java-based, while state-of-the-art deep learning frameworks like PyTorch are primarily C++/Python-based. This study investigates various strategies to bridge this gap, balancing performance metrics like latency and throughput against ease of integration.
Model Architecture
The core of our reranking system is a "Siamese" Convolutional Neural Network (CNN) designed for answer selection. This architecture processes a query and a candidate document in parallel through identical sub-networks to generate vector representations, which are then compared to determine relevance.
Integration Strategies
We implemented and compared three distinct integration approaches:
- PyTorch + Apache Thrift: The model remains in its native PyTorch environment and is exposed as a microservice via Apache Thrift. This allows the Java-based Lucene client to communicate with the model over a network protocol.
- Deeplearning4J (DL4J): To achieve a unified Java codebase, model weights trained in PyTorch are exported and loaded into DL4J. This enables native Java feedforward evaluation within the same process as Lucene.
- C++ "Compiled" Network: We developed a tool to "compile" the trained network into a standalone C++ program using the Blaze math library. This program is then exposed as a service via Thrift, aiming to leverage aggressive compiler optimizations.
Experimental Results
The models were evaluated using the TrecQA dataset. Performance was measured in terms of throughput (Queries Per Second, QPS) and latency (p50 and p99).
Throughput Analysis
Our experiments revealed a significant performance gap between the native Java implementation and the C++/Python-based solutions. As shown in the table below, DL4J was consistently 2x to 6x slower than PyTorch or the C++ Generator across different hardware configurations.
Latency and Service Overhead
While there were concerns regarding the overhead of network serialization in the Thrift-based approaches, the study found this overhead to be minimal (approximately 6-7% for PyTorch). On a desktop machine, the PyTorch service achieved a median latency of 0.83ms with a throughput of ~1150 QPS.
Discussion and Conclusion
The results highlight a "maturity gap" in Java-native deep learning libraries compared to established frameworks like PyTorch. Despite the theoretical benefits of a unified Java environment, the performance penalty of DL4J was substantial. Furthermore, manually compiling networks into C++ did not yield consistent gains over the highly optimized PyTorch backend.
We conclude that for real-time document reranking, a microservices approach using a high-performance RPC framework like Apache Thrift to wrap a PyTorch model provides the best trade-off between performance and engineering simplicity.
Summary updated