An Application of Storage-Optimal MatDot Codes for Coded Matrix Multiplication: Fast k-Nearest Neighbors Estimation
Utsav Sheth, Sanghamitra Dutta, Malhar Chaudhari, Haewon Jeong, Yaoqing Yang, Jukka Kohonen, Teemu Roos, Pulkit Grover
I INTRODUCTION
We consider the problem of finding the k nearest neighbors of a query point in a given high-dimensional dataset. To solve this problem efficiently, our goal is to speed up an existing algorithm by parallelizing it, and to make it resilient to stragglers . The k-nearest neighbor (k-NN) problem is often a first step used in a variety of real world applications including genomics , personalized search , network security , and web based recommendation systems .
In the era of Big Data, k-NN algorithms are often a bottleneck, as data and dimensionalities grow . There is a rich body of work on fast nearest neighbor retrieval. The existing work can be broadly classified into three categories. The first category of methods speeds up k-NN retrieval by reducing the space over which exact distance calculations are performed, by employing space partitioning data structures . The second category of techniques improves retrieval times by system level parallelism . Algorithms in the first and second categories are seldom scalable as they require that the whole dataset is held in (shared) memory for optimal performance. For large high-dimensional datasets, marshaling enough resources on a single system is challenging. The third category of methods aims to overcome this challenge by parallelizing storage and computation in a distributed setting. PANDA and DSI sharding are examples of data parallel implementations of distributed k-NN algorithms. While these techniques rely on specialized or high-performance hardware (e.g. Edison supercomputer for PANDA), the general trend in the distributed systems has been to use general purpose commodity systems . These approaches also do not address a more serious issue — the effects of node failures and slow nodes, or “stragglers”. Schroeder and Gibson observed as many as system failures per year at the Los Alamos National Laboratory. Dean et al. study a real Google service and observe that the slowest 5% of requests are responsible for half of the total 99th percentile latency.
Recently “Coded Computing” has been found to be very useful in combating stragglers and faults, by the efficient use of novel erasure-codes to create redundancy in computing. In this paper, we use one such coded computing technique called MatDot codes to speed up an approximate -NN algorithm called Multiple Random Projection Trees (MRPT) .
The problem of distributed matrix multiplication has always been of significant interest in the coded computing community . In our prior work , we proposed a novel coding technique called MatDot codes for the multiplication of two matrices (e.g. ) that outperform the recent Polynomial Codes with respect to recovery threshold, i.e., the number of worker nodes needed to wait for, out of the total nodes, under storage constraints. More specifically, for the problem of distributed matrix multiplication under the storage constraint that each node can only store a fixed fraction of each matrix, MatDot codes achieve the optimal recovery threshold of as compared to Polynomial codes that have a recovery threshold of .
We note that for matrix multiplication or matrix-vector product under the constraint that only fraction of each operand can be stored at each node, MatDot codes use vertical block-partitioning of the first matrix as compared to other existing strategies that use either horizontal partition or a combination of both (see for discussion on partitioning). Interestingly, as it turns out, the vertical partitioning of the first matrix is better suited for the coded MRPT problem formulation. This is because we are required to perform the multiplication or in a distributed fashion, where denotes a sub-matrix of consisting of the rows of indexed in a set , such that, is known in advance but the set becomes available only in the online phase (real-time).
This coded computing problem ensues from our broader goal in this work, which is to speed up MRPT by employing data and model parallelism and straggler-tolerant computing techniques. We differentiate here between data parallelism and model parallelism. In data parallelism, different nodes process different pieces of data, but each node performs all the computations relevant to the entire model. In model parallelism, the model itself is parallelized across nodes.
MRPT partitions the search space to retrieve approximate k nearest neighbors of the query . It uses a combination of random projection trees and voting to achieve fast queries and high accuracy. In this paper, we propose two enhancements to the MRPT algorithm by parallelizing it in a distributed setting. Our contributions are as follows:
We propose a distributed implementation of MRPT exploiting data parallelism that experimentally demonstrates faster queries than a single node implementation, even when using CPUs with lower clock speeds. Additionally, the cloud-based virtual machines we use in our experiments for parallel MRPT have a non-zero steal time, i.e., they may be required to wait while others are being served.
We formulate a coded computing problem for MRPT, and then apply coded matrix multiplication strategies, namely MatDot and Systematic MatDot codes to further reduce the query time for the model parallel architecture in a system that is prone to straggling.
The rest of the paper is organized as follows. In Section II we explain the MRPT algorithm and describe how it reduces the search-space through projections and voting. In Section III we introduce the Data Parallel Model Implementation of MRPT. In Section IV we introduce our proposed model parallel implementation of MRPT and then describe the application of MatDot codes and systematic MatDot codes in our model parallel architecture to achieve a lower recovery threshold under straggling. The model parallel architecture is ideal for applications where system components are unreliable and accuracy is important. In Section V we experimentally demonstrate the advantages of our approach. A conclusion is provided in Section VI.
II Preliminaries
This section briefly describes the two stages of the MRPT algorithm: (i) off-line index construction stage and (ii) on-line query stage. Assume that we are given a -dimensional dataset consisting of points, represented as a matrix . Given a query point , the problem of k-nearest neighbors involves finding a set of points such that and for each , , and the function is the distance function in the d-dimensional Euclidean space given by:
where and are two vectors in this space.
In the MRPT algorithm, a sparse -dimensional random projection vector is chosen, in which each entry is sampled from the following distribution:
Given a -dimensional query vector , the first step in the MRPT query stage is to generate a candidate set of indices (pruned data-point indices) such that .
For each Random Projection (RP) tree , at each level the query vector is projected onto the random vector for that level and then assigned a branch based on whether its value is greater than or less than the median of the projections of all other data-points with . This process is then repeated recursively until a leaf is reached.
Each tree had already partitioned the dataset into cells or leaves. For , let be defined as:
The candidate set of indices (pruned points) can then be finally chosen as follows:
Here, is a pre-configured parameter known as the voting threshold. Thus, the set denotes the set of indices for which at least trees have found the corresponding data-point in the same cell as .
II-B Coded Matrix Multiplication
Coded computing combines distributed numerical algorithms and error correcting codes (ECCs) to mitigate unreliable processors and randomness in their response time. In this work, we focus on coded matrix multiplication as matrix multiplication is the main bottleneck in MRPT algorithm. Coded computing has been used extensively for different computation objectives such as neural-network training, FFT, iterative computing, distributed regression, and convolutions (see for review).
System Model: We want to compute where and are -by- matrices. A master node distributes the computation to worker nodes. A worker node has limited memory/computing power, so each node can receive the -th fraction of matrices and . After completing its computation, a worker reports the result to a fusion node. Recovery threshold is defined as the worst-case number of workers needed to recover the final result.
Let be the number of workers needed to complete the computation if all worker nodes are reliable ( is different depending on how we split the matrix). In reality, some processors are significantly slower than the others due to queuing delays or random faults in the processor. Without any reliability measure to alleviate straggler problems, computation completion time would be dominated by few stragglers. Our aim is to use more than worker nodes by adding some redundancies, which are carefully designed by applying the ideas from coding theory, so that the whole computation can be resilient to stragglers.
MatDot codes: The matrix is split vertically into column blocks, and is split horizontally into row blocks:
where () are and dimensional submatrices, respectively.
The matrices and are then encoded as polynomials:
A master node distributes encoded matrices, and to the -th worker node (). Then the -th worker node computes the following product at :
and returns the result to the master node. Note that the coefficient of in is . Since is a polynomial of degree , its coefficients can be recovered by the master node as soon as it receives the values of at any distinct points. Hence the recovery threshold is . This is provably the optimal recovery threshold, when a worker node can store -th fraction of each input matrix .
Systematic MatDot codes: A code is called systematic if, for the first worker nodes, the output of the -th worker node is the product . We refer to the first worker nodes as systematic worker nodes. Having systematic nodes is useful because if all the systematic nodes complete their computation in time, there is no need for decoding. Systematic MatDot codes are achieved by applying different encoding polynomials. Let and where is defined as follows for :
Using these polynomials, the worst-case recovery threshold remains the same as non-systematic MatDot codes .
MatDot and Systematic MatDot codes use vertical partitioning of the first matrix which is well suited for the problem of coded MRPT as compared to strategies that use horizontal partitioning or a combination of horizontal and vertical partitioning for the first matrix. This is because in the coded MRPT formulation, we are required to perform the multiplication or in a distributed fashion, where denotes a sub-matrix of consisting of the rows of indexed in a set , such that the set is available only in the online phase (real-time).
III DATA PARALLEL MRPT
Now, we model MRPT as a problem in data parallelism and describe our first strategy to parallelize the algorithm. Consider a distributed computing cluster having a single master node and worker nodes as shown in Fig. 1.
Given a matrix X representing the set of data-points and a cluster with worker nodes, we randomly split vertically into disjoint, vertical partitions for .Thus, where each is of dimension
We distribute each partition across the worker nodes in the cluster such that the worker contains partition . Each worker then runs the MRPT index construction algorithm described in Section II-A to build a local MRPT set of trees from its partition of .
Given a query , the master node transmits to each worker node . Then uses the MRPT query algorithm described in Section II-A on its trees to determine the nearest neighbors of . We use the same voting threshold in all workers. After local voting, a local exact distance calculation step is performed to narrow down to data-points. Finally, each worker node then returns the indices of its set of nearest neighbors , from the partition of , to the master node along with their exact distances from the query . Then, the master node determines the final set of k nearest neighbors to from all the candidate data-point indices received from all the worker nodes, i.e., the indices of all the data-points in the set , by sorting the data-points based on their exact distances.
: Recall that (with ) is the set of the true k nearest neighbors of . We let be the set of the true nearest neighbors to that lie in worker . To achieve maximum accuracy, we must have and therefore the value of chosen for the system must be such that is much less than . In fact, a higher value of implies higher chance of containing the desired set , though it comes with increased communication cost from worker to master and increased computation at the master node.
IV MODEL PARALLEL MRPT
In this section we discuss a model parallel architecture for approximate k nearest neighbor search using MRPT. We then propose two enhancements to the model parallel architecture that apply coded distributed matrix multiplication techniques that achieve the optimal recovery threshold in a system that is prone to straggling.
Consider the d-dimensional dataset as before. In the model parallel architecture, given a query , we first find the possible candidate set of indices (pruned indices) S using the recursive algorithm described in Section II-A. Now the search space for the true nearest neighbors reduces to the set of data-points whose indices are in , i.e., .
To find the set , we compute the exact Euclidean distance from each data-point (for ) to the query point . Examining the terms constituting the Euclidean distance in (1), the Euclidean norm for each can be precomputed and the same can be done to get . We must now only compute the dot product to obtain the Euclidean distances from each to .
To do this, we first represent the data-points indexed in the set S as a matrix that contains only the data-points (columns of ) such that . The transpose of this matrix is the matrix that essentially denotes all the rows of the matrix indexed in .
Consider the column vector such that . Note that each element of the vector corresponds to the dot-product for some . The Euclidean distance from to can now be determined as all the terms in (1) are known to us, which includes the individual norms as well as the dot product .
The problem thus reduces to the following: compute the vector in a distributed computing cluster where is known in advance but the set of indices become available only in the online phase (real-time). Since the computation is the only stage of the algorithm that must be done at runtime (in the online stage) and scales linearly with , we now discuss several scalable strategies that compute vector in a distributed setting.
IV-B Uncoded Distributed Matrix-Vector Multiplication
We split into equal partitions as follows (see Fig. 2):
Now consider a cluster consisting of one master node and worker nodes as shown in Fig. 3. Each partition is distributed across the worker nodes such that worker stores the partition in advance (off-line).
Note that, if is partitioned using the strategy just discussed, the matrix also gets partitioned as follows:
In the online phase, we only split the query into equal partitions, (again see Fig. 2). The product can then be expressed as:
Given a query for which the k nearest neighbors must be determined, the master node first computes the possible candidate set S for from its MRPT index set of trees and then transmits the set S and partition of to worker node . For every S, each worker node only fetches the matrix from already stored in its memory. It then computes the product and returns the resulting vector to the master node. The master node can thus compute the vector by adding the results using (11), and determine the nearest neighbors using the exact distances.
IV-C Coded Distributed Matrix-Vector Multiplication using MatDot Codes
In order to successfully compute the vector using the strategy described in Section IV-B, the master node must wait for every worker node to successfully return the product . In a straggler-prone environment, this might cause unprecedented delays in computation. Thus, to avoid waiting for all nodes and be able to recover the matrix-vector product by only waiting for some out of all workers to finish, we will now apply the MatDot-based distributed matrix multiplication strategy .
We partition the matrix vertically again, but into partitions instead of as follows:
We then use the following encoding polynomial:
The rows of indexed in set actually represent the following polynomial:
We will be referring to this observation later.
Now, given a cluster with a master node and worker nodes, as shown in Fig. 4, each worker node is initialized with a different , using which it computes the polynomial () in (13). This encoding step can be performed off-line as is known in advance.
During the online stage, given a query for which the k nearest neighbors must be determined, the master node first partitions into parts: . We then use the following encoding polynomial:
As in Section IV-B, the master node first determines the candidate set S. It then transmits S and the encoded query obtained from (15) to worker . The worker then fetches only the matrix from its stored (recall (14)) which essentially denotes all the rows of indexed in . Then, it computes the product and returns the result to the master node.
The coefficient of in the polynomial turns out to be our desired desired matrix-vector product from the property of MatDot codes. We need to evaluate the polynomial at only distinct points so as to determine the coefficient for every power of . The master node must therefore wait for at least worker nodes following which it can determine the term using polynomial interpolation. We then follow the strategy of comparing the exact distances in Section IV-A to obtain the set of the k nearest neighbors to .
IV-D Coded Distributed Matrix-Vector Multiplication using Systematic MatDot Codes
In our prior work where we proposed MatDot Codes, we also introduced their systematic variant. Their advantage is that, while for MatDot Codes the recovery threshold is always , for systematic MatDot Codes one might sometimes only need nodes to finish, although is the worst-case value. In this section, we apply the systematic MatDot code to the MRPT problem.
Similar to the previous case, we first partition vertically into partitions, but then use a different encoding function:
Consider a cluster consisting of one master node and worker nodes. Worker is assigned a value \textit{\beta}_{i} using which it computes the polynomial in (16). This encoding is performed in advance, in the off-line stage. Interestingly, the workers turn out to be the systematic worker nodes, which contain uncoded partitions of .
In the online phase, given a query , the master node first partitions into partitions and then uses the following encoding function:
where is given by (17). The master node then transmits candidate set and to each worker node. Worker is responsible for computing the product . We first consider the case when the first m workers to successfully complete their computation are the systematic worker nodes. We can then obtain the vector as follows:
If the results of the first successful workers do not contain results from the systematic nodes, then the master interpolates the polynomial . It then computes this polynomial product at each {,,…,}. Finally, it computes the vector using (19). Note that in the ideal case, i.e., when all the systematic worker nodes finish first, we only needed nodes to finish as opposed to the worst-case recovery threshold of . We can now proceed with the steps of comparing exact distances (see Section IV-A) to retrieve the k nearest neighbors of .
V EXPERIMENTAL RESULTS
In this section, we evaluate the effectiveness of data and model parallel MRPT in terms of both accuracy and speed. All of our experiments were conducted on Amazon Elastic Compute Cloud instances .
The STL-10 dataset is a dataset of images each of dimension used in unsupervised image classification algorithms, while GIST is a popular dataset with and used in ANN algorithms. These datasets provide us with a good mix of dimensionality and number of datapoints to evaluate our proposed strategies. The MRPT parameters used for the experiments are provided in Table I.
We evaluate the accuracy of our implementation using recall defined as: , where is the query whose true k nearest neighbors is the set and is the set of k nearest neighbors returned by the algorithm.
For the single node MRPT baseline, we used a compute optimized c5.large instance with two 3GHz Intel Xeon Platinum processors and 4 GB of memory. For the data parallel and model parallel architectures, we used t2.medium instances with two 2.3 GHz Intel Broadwell processors and 4 GB of memory. All instances are provisioned with 40GB HDD secondary storage. Note that the hardware used for our baseline experiments is superior to that used to evaluate our parallel strategies. In all experiments we ran 500 queries sequentially with . We consider the average result of 50 runs for each experiment. The experiments were conducted in a cluster consisting of 1 master node and 16 worker nodes.
In the experiments with MatDot codes and systematic MatDot codes, we used encoding polynomials of degree 2.
All our experiments are conducted on systems with limited memory. Thus the MRPT algorithm has to use the disk to hold the index and the actual data points. In comparison, the data parallel and model parallel architectures use less memory and avoid disk penalties. In the data parallel architecture, we reduce the amount of points each worker must hold by a factor of P. Additionally, the MRPT indexes become smaller as each worker holds a smaller fraction of the dataset. In the model parallel architecture, the master node can discard the components for each point after tree construction; it only needs the index. At each worker node, the memory requirement is at least halved.
V-B Simulating Stragglers on Amazon Web Services
To demonstrate the effects of stragglers in the model parallel architecture, we sample the minimum time a worker must take to complete a matrix multiplication for a query from the shifted Exponential and Weibull distributions shown in (20) and (21) respectively.
Here, is the number of row vectors loaded at worker for matrix multiplication, is the shift parameter, is the shape parameter for the Weibull distribution, is the straggling parameter for , and . For our experiments, we set each , , and to some constants , , and to maintain homogeneity in minimum computation times across workers. The parameters chosen are as follows: Shifted Exponential (, ) and Weibull (, , ) for both the datasets. Note that, for model parallel MRPT, . We use a similar strategy to simulate straggling in data parallel MRPT with set to the average of for the set of test queries.
V-C Results
Our experimental results are provided in Tables II and also illustrated in Fig. 5. For completion, we also include our obtained recall values here: STL-10: Data Parallel (0.9632), Model Parallel (0.9648). GIST: Data Parallel (0.9430), Model Parallel (0.9350).
In all our experiments, both data and model parallel MRPT outperform the single node implementation. As shown in Fig. 5(a) and Fig. 5(b), data parallel MRPT is significantly faster than single node MRPT. This is due to the smaller size of the MRPT index and absence of disk penalties at workers. It can be seen that the data parallel strategy has better performance when compared to uncoded model parallel strategy because of its embarrassingly parallel design. Owing to this design, the data parallel strategy could scale linearly with the number of nodes. However, these scaling benefits in query execution come at a cost, as the random projection trees computed at each node do not contain all the data points and hence the candidate set generated by each node could contain lesser true positives. To offset this condition, we might have to lower the voting threshold in-order to generate a better candidate, while causing more communication overheads and hence lower query execution time. The note mentioned in Section III explains this case in more detail. For our experiments, we do not lower the voting threshold as the loss in recall for STL-10 and GIST is not significant.
The model parallel architecture results in a high communication cost as the candidate set has to be transmitted to each worker node. However, we find that if the MRPT parameters for a dataset are sufficiently tuned, Algorithm 1 will generate a smaller candidate set over which exact distance calculations must be performed. For our experiments, the algorithm was able to reduce the search space for STL-10 from 100000 datapoints to 3025 and for GIST from 1000000 to 14934 datapoints on average per query. Additionally, the model parallel strategy does not suffer from the accuracy related issues as compared to the data parallel architecture as the candidate set is generated using the entire dataset and not parts of it separately.
Both the strategies outperform baseline single node MRPT despite running on inferior hardware. The parallel strategies are not limited by memory constraints as in the case of single node MRPT. These strategies are therefore very useful when large datasets do not fit in memory. The coded model parallel strategy also makes the algorithm tolerant to slow nodes and failures in a distributed setting.
Fig. 5(c) and Fig. 5(d) show that model parallel MRPT outperforms single node MRPT. They also outperform data parallel MRPT under simulated straggling. This is of significance to real world systems where straggling may manifest as unreliable nodes or network delays. Fig. 5(e) and Fig. 5(f) show the benefits of coded matrix multiplication as opposed to the uncoded model parallel architecture under simulated straggling. Both MatDot codes and systematic MatDot codes are consistently faster than the uncoded approach. Fig. 5(e) also shows that systematic MatDot codes is able to outperform MatDot codes owing to its lower recovery threshold.
VI CONCLUSIONS
We proposed two approaches to parallelize the MRPT algorithm in a distributed setting. We also applied the MatDot code based distributed matrix multiplication strategy to reduce the recovery threshold in a system that is prone to stragglers. We showed that our parallelization strategies can achieve faster queries than the single node MRPT algorithm under limited memory. Our results demonstrate the benefits of applying MatDot code and systematic MatDot code to the model parallel architecture in a system with simulated stragglers. In our experiments we observed large floating point errors when inverting high degree Vandermonde matrices for polynomial interpolation. As future work, we will experiment with strategies to reduce the condition number of a Vandermonde matrix so that we can employ polynomials of higher degrees, and thus apply the MatDot code and systematic MatDot code with a larger number of worker nodes. Another possibility is to perform the computations in exact rational arithmetic. This would eliminate rounding errors, but the effect on runtime needs to be analyzed.
Acknowledgements
This work was supported by NSF CNS-1702694, the Academy of Finland under the WiFIUS program, the Academy of Finland COIN CoE and NSF CCF 1350314.