Gradient Coding
Rashish Tandon, Qi Lei, Alexandros G. Dimakis, Nikos Karampatziakis
Introduction
We propose a novel coding theoretic framework for mitigating stragglers in distributed learning. The central idea can be seen through the simple example of Figure 1: Consider synchronous Gradient Descent (GD) on three workers (,,). The baseline vanilla system is shown in the left figure and operates as follows: The three workers have different partitions of the labeled data stored locally (,,) and all share the current model. Worker 1 computes the gradient of the model on examples in partition , denoted by . Similarly, Workers 2 and 3 compute and . The three gradient vectors are then communicated to a central node (called the master/aggregator) which computes the full gradient by summing these vectors and updates the model with a gradient step. The new model is then sent to the workers and the system moves to the next round (where the same examples or other labeled examples, say ,,, will be used in the same way).
The problem is that sometimes worker nodes can be stragglers (Li et al., 2014; Ho et al., 2013; Dean et al., 2012) i.e. delay significantly in computing and communicating gradient vectors to the master. This is especially pronounced for cheaper virtual machines in the cloud. For example on t2.micro machines on Amazon EC2, as can be seen in Figure 2: some machines can be slower in computing and communicating gradients compared to typical performance.
First, we discuss one way to resolve this problem if we replicate some data across machines by considering the placement in Fig.1 (b) but without coding. As can be seen, in Fig. 1 (b) each example is replicated two times using a specific placement policy. Each worker is assigned to compute two gradients on the two examples they have for this round. For example, will compute vectors and . Now let’s assume that is the straggler. If we use control messages, can notify the master that they are done. Subsequently, if feedback is used, the master can ask to send and and to send . These feedback control messages can be much smaller than the actual gradient vectors but are still a system complication that can cause delays. However, feedback makes it possible for a centralized node to coordinate the workers, thereby avoiding stragglers. One can also reduce network communication further by simply asking to send the sum of two gradient vectors instead of sending both. The master can then create the global gradient on this batch by summing these two vectors. Unfortunately, which linear combination must be sent depends on who is the straggler: If was the straggler then should be sending and sending so that their sum is the global gradient .
In this paper we show that feedback and coordination is not necessary: every worker can send a single linear combination of gradient vectors without knowing who the straggler will be. The main coding theoretic question we investigate is how to design these linear combinations so that any two (or any fixed number generally) contain the vector in their span. In our example, in Fig. 1(b), sends , sends and sends . The reader can verify that can obtain the vector from any two out of these three vectors. For instance, . We call this idea gradient coding.
We consider this problem in the general setting of machines and any stragglers. We first establish a lower bound: to compute gradients on all the data in the presence of any stragglers, each partition must be replicated times across machines. We propose two placement and gradient coding schemes that match this optimal replication factor. We further consider a partial straggler setting, wherein we assume that a straggler can compute gradients at a fraction of the speed of others, and show how our scheme can be adapted to such scenarios. All proofs can be found in the appendix.
We also compare our scheme with the popular ignoring the stragglers approach (Chen et al., 2016): simply doing a gradient step when most workers are done. We see that while ignoring the stragglers is faster, this loses some data which can hurt the generalization error. This can be especially pronounced in supervised learning with unbalanced labels or heavily unbalanced features since a few examples may contain critical, previously unseen information.
In Figure 2, we show the average time required for 50 t2.micro Amazon EC2 instances to communicate gradients to a single master machine (a c3.8xlarge instance). We observe that a few worker machines incurred a communication delay of up to the typical behavior. Interestingly, throughout the timescale of our experiments (a few hours), the straggling behavior was consistent in the same machines.
We have also experimented extensively with other Amazon EC2 instances: Our finding is that cheaper instance types have significantly higher variability in performance. This is especially true for t2 type instance which on AWS are described as having Burstable Performance. Fortunately, these machines have very low cost.
The choices of the number and type of workers used in training big models ultimately depends on total cost and time needed until deployment. The main message of this paper is that going for very low-cost instances and using coding to mitigate stragglers, may be a sensible choice for some learning problems.
2 Related Work
The slow machine problem is the Achilles heel of many distributed learning systems that run in modern cloud environments. Recognizing that, some recent work has advocated asynchronous approaches (Li et al., 2014; Ho et al., 2013; Mitliagkas et al., 2016) to learning. While asynchronous updates are a valid way to avoid slow machines, they do give up many other desirable properties, including faster convergence rates, amenability to analysis, and ease of reproducibility and debugging.
Attacking the straggling problem in synchronous machine learning algorithms has surprisingly not received much attention in the literature. There do exist general systems solutions such as speculative execution Zaharia et al. (2008) but we believe that approaches tailored to machine learning can be vastly more efficient. In Chen et al. (2016) the authors use synchronous minibatch SGD and request a small number of additional worker machines so that they have an adequate minibatch size even when some machines are slow. However, this approach does not handle well machines that are consistently slow and the data on those machines might never participate in training. In Narayanamurthy et al. (2013) the authors describe an approach for dealing with failed machines by approximating the loss function in the failed partitions with a linear approximation at the last iterate before they failed. Since the linear approximation is only valid at a small neighborhood of the model parameters, this approach can only work if failed data partitions are restored fairly quickly.
The work of Lee et al. (2015) is the closest in spirit to our work, using coding theory and treating stragglers as erasures in the transmission of the computed results. However, we focus on codes for recovering the batch gradient of any loss function while Lee et al. (2015) and the more recent work of Dutta et al. (2016) describe techniques for mitigating stragglers in two different distributed applications: data shuffling and matrix multiplication. We also mention Li et al. (2016a), which investigates a generalized view of the coding ideas in Lee et al. (2015), showing that their solution is a single operating point in a general scheme of trading off latency of computation to the load of communication. Further closely related work has shown how coding can be used for distributed MapReduce, as well as a similar communication and computation tradeoff (Li et al., 2015, 2016b). All these prior works develop novel coding techniques, but do not code across gradient vectors in the way we are proposing in this paper.
Preliminaries
where is a gradient-based optimizer, which also depends on . Several methods such as gradient descent, accelerated gradient, conditional gradient (Frank-Wolfe), proximal methods, LBFGS, and bundle methods fit in this framework. However, if the number of samples, , is large, a computational bottleneck in the above update step is the computation of the gradient, , whose computation can be distributed.
2 The General Setup
We can generalize the scheme in Figure 1(b) to workers and data partitions by setting up a system of linear equations:
Then, worker transmits . Note that to transmit , only needs to compute the partial gradients on the partitions in . Now, each row of is associated with a specific failure/straggler scenario, to which tolerance is desired. In particular, any row , with support , corresponds to the scenario where the worker indices in are alive/non-stragglers. Also, by the construction in Eq. (3), we have:
where denotes the element of the row . Thus, the entries of encode a linear combination which, when taken over the transmitted gradients of the alive/non-straggler workers, , would yield the full gradient.
Going back to the example in Fig. 1(b), the corresponding and matrices under the above generalization are:
with . It is easy to check that . Also, since every row of here has exactly one zero, we say that this scheme is robust to any one straggler.
In general, we shall seek schemes, through the construction of , which are robust to any stragglers.
The rest of this paper is organized as follows. In Section 3 we provide two schemes applicable to any number of workers , under the assumption that stragglers can be arbitrarily slow to the extent of total failure. In Section 4, we relax this assumption to the case of worker slowdown (with known slowdown factor), instead of failure, and show how our constructions can be appended to be more effective. Finally, in Section 5 we present results of empirical tests using our proposed distribution schemes on Amazon EC2.
Full Stragglers
In this section, we consider schemes robust to any stragglers, given workers (with ). We assume that any straggler is (what we call) a full straggler i.e. it can be arbitrarily slow to the extent of complete failure. We show how to construct the matrices and , with , such that the scheme is robust to any full stragglers.
Consider any such scheme . Since every row of represents a set of non-straggler workers, all possible sets over of size must be supports in the rows of . Thus i.e. the total number of failure scenarios is the number of ways to choose stragglers out of workers. Now, since each row of represents a linear span over some rows of , and since we require , this leads us to the following condition on :
Consider any scheme robust to any stragglers, given workers (with ). Then we require that for every subset :
where is the span of vectors.
The B-Span condition above ensures that the all s vector lies in the span of any rows of . This is of course necessary. However, it is also sufficient. In particular, given a satisfying Condition 1, we can construct such that , and has the support structure discussed above. The construction of is described in Algorithm 1 (in MATLAB syntax), and we have the following lemma.
Based on Lemma 1, to obtain a scheme robust to any stragglers, we only need to furnish a satisfying Condition 1. A trivial that works is , the all ones matrix. However, this is wasteful since it implies that each worker gets all the partitions and computes the full gradient. Our goal is to construct satisfying Condition 1 while also being as sparse as possible in each row. In this regard, we have the following theorem, which gives a lower bound on the number of non-zeros in any row of .
Consider any scheme robust to any stragglers, given workers (with ) and partitions. Then, if all rows of have the same number of non-zeros, we must have: for any .
Theorem 1 implies that any scheme that assigns the same amount of data to all the workers must assign at least fraction of the data to each worker. Since this fraction is independent of , for the remainder of this paper we shall assume that i.e. the number of partitions is the same as the number of workers. In this case, we want to be a square matrix satisfying Condition 1, with each row having at least non-zeros. In the sequel, we demonstrate two constructions for which satisfy Condition 1 and achieve the density lower bound.
In this section, we provide a construction for that works by replicating the task done by a subset of the workers. We note that this construction is only applicable when the number of workers, , is a multiple of , where is the number of stragglers we seek tolerance to. In this case, the construction is as follows:
We divide the workers into groups of size .
In each group, we divide all the data equally and disjointly, assigning partitions to each worker
All the groups are replicas of each other
When finished computing, every worker transmits the sum of its partial gradients
Thus, the first worker in the group gets the first partitions, the second worker gets the second partitions, and so on. Then, is simply replicated copies of :
where for each , .
It is easy to see that this construction can yield robustness to any stragglers. Since any particular partition of data is replicated over workers, any stragglers would leave at least one non-straggler worker to process it. We have the following theorem.
Consider constructed as in Eq. (9), for a given number of workers and stragglers . Then, satisfies the B-Span condition (Condition 1). Consequently, the scheme , with constructed using Algorithm 1, is robust to any stragglers.
The construction of matches the density lower bound in Theorem 1 and, the above theorem shows that the scheme , with constructed from Algorithm 1, is robust to stragglers.
2 Cyclic Repetition Scheme
In this section we provide an alternate construction for which also matches the lower bound in Theorem 1 and satisfies Condition 1. However, in contrast to construction in the previous section, this construction does not require to be divisible by . Here, instead of assigning disjoint collections of partitions, we consider a cyclic assignment of partitions to the workers. We construct a with the following support structure:
where indicates non-zero entries in . So, the first row of has its first entries assigned as non-zero. As we move down the rows, the positions of the non-zero entries shift one step to the right, and cycle around until the last row.
Consider constructed using the randomized construction in Algorithm 2, for a given number of workers and stragglers . Then, with probability , satisfies the B-Span condition (Condition 1). Consequently, the scheme , with constructed using Algorithm 1, is robust to any stragglers.
Partial Stragglers
In this section, we revisit our earlier assumption of full stragglers. Under a full straggler assumption, Theorem 1 shows that any non-straggler worker must incur an -factor overhead in computation, if we want to attain tolerance to any stragglers. This may be prohibitively huge in many situations. One way to mitigate this is by allowing at least some work to be done also by the straggling workers. Therefore, in this section, we consider a more plausible scenario of slow workers, but assume a known slowdown factor. We say that a straggler is an -partial straggler (with ) if it is at most slower than any non-straggler. This means that if a non-straggler completes a task in time , an -partial straggler would require at most time to complete it. Now, we augment our previous schemes (in Section 3.1 and Section 3.2) to be robust to any stragglers, assuming that any straggler is an -partial straggler.
Note that our earlier constructions are still applicable: a scheme , with or , would still provide robustness to partial stragglers. However, given that no machine is slower than a factor of , a more efficient scheme is possible by exploiting at least some computation on every machine. Our basic idea is to couple our earlier schemes with a naive distribution scheme, but on different parts of the data. We split the data into a naive component, and a coded component. The key is to do the split such that whenever an -partial straggler is done processing its naive partitions, a non-straggler would be done processing both its naive and coded partitions.
In general, for any , our two-stage scheme works as follows:
We split the data into equal-sized partitions — of which partitions are coded components, and the rest are naive components
Each worker gets naive partitions, distributed disjointly.
Each worker gets coded partitions, distributed according to an distribution scheme robust to stragglers (e.g. with or )
Any worker, , first processes all its naive partitions and sends the sum of their gradients to the aggregator. It then processes its coded partitions, and sends a linear combination, as per the distribution scheme.
Note that each worker now has to send two partial gradients (instead of one, as in earlier schemes). However, a speedup gained in processing a smaller fraction of the data may mitigate this overhead in communication, since each non-straggler only has to process a fraction of the data, as opposed to a fraction in full straggler schemes. Thus, when computation is the bottleneck, adopting a partial stragglers scheme may not hurt the overall efficiency. On the other hand, when communication is the bottleneck (and if a overhead is prohibitive), a full straggler scheme may be a better choice even with its (s+1)-factor overhead in computation for the non-straggler workers.
Fig. 4 illustrates our two-stage strategy for . We see that each non-straggler gets fraction of the data, instead of a fraction (for e.g. in Fig 1(b)).
Experiments
In this section, we present experimental results on Amazon EC2, comparing our proposed gradient coding schemes with baseline approaches. We compare our approaches against: the naive scheme, where the data is divided uniformly across all workers without replication and the aggregator waits for all workers to send their gradients, and the ignoring stragglers scheme where the data is divided as in the naive scheme, however the aggregator performs an update step after any workers have successfully sent their gradient.
We implemented all methods in python using MPI4py (Dalcin et al., 2011), an open source MPI implementation. Based on the method being considered, each worker loads a certain number of partitions of the data into memory before starting the iterations. In iteration the aggregator sends the latest model to all the workers (using Isend()). Each worker receives the model (using Irecv()) and starts a gradient computation. Once finished, it sends its gradient(s) back to the aggregator. When sufficiently many workers have returned with their gradients, the aggregator computes the overall gradient, performs a descent step, and moves on to the next iteration.
Our experiments were performed using two different worker instance types on Amazon EC2: m1.small and t2.micro — these are very small, very low-cost EC2 instances. We also observed that our system was often bottlenecked by the number of incoming connections i.e. all workers trying to talk to the master concurrently. For that reason, and to mitigate this additional overhead to some degree, we used a larger master instance of c3.8xlarge in our experiments.
We ran the various approaches to train logistic regression models, a well-understood convex problem that is widely used in practice. Moreover, Logistic regression models are often expanded by including interaction terms that are often one-hot encoded for categorical features. This can lead to 100’s of thousands of parameters (or more) in the trained models. To train the logistic regression models for using our proposed scheme (or the naive scheme), we used Nesterov’s Accelerated Gradient descent with a constant learning rate, where the constant was chosen optimally from a range. Note that other optimizers such as LBFGS would have also been applicable here since we obtain the full gradient in our schemes. For the ignoring s stragglers approach, we used gradient descent with a learning rate of (which is typical for SGD), where and were also chosen optimally in a range. We did not use NAG here since it is unstable to noisy gradients. While we do not present any empirical results, we refer the reader to Devolder et al. (2014) for a theoretical and empirical analysis of the effect of noisy gradients in NAG. Thus another advantage of our schemes over ignoring s stragglers is that the latter cannot be combined with NAG because errors may quickly accumulate and eventually cause the method to diverge.
2 Results
In this experiment, we also artificially added delays to random workers in each iteration (using time.sleep()). Figure 5 presents the results of our experiments with and stragglers, on a cluster of m1.small machines. As expected, the baseline naive scheme that waits for the stragglers has poorer performance as the delay increases. The Cyclic and Fractional schemes were designed for one straggler in Figure 5(a) and for two stragglers in Figure 5(b). Therefore, we expect that these two schemes would not be influenced at all by the delay of the stragglers (up to some variance due to implementation overheads). The partial straggler schemes were designed for various . Recall that for partial straggler schemes, denotes the slowdown factor.
Real Dataset: Next, we trained a logistic regression model on the Amazon Employee Access dataset from Kaggle https://www.kaggle.com/c/amazon-employee-access-challenge. We used training samples, and a model dimension of (after one-hot encoding with interaction terms). These experiments were run on t2.micro instances on Amazon EC2.
In Figure 7 we show the Generalization AUC of our method (FracRep and CycRep) versus ignoring stragglers (IgnoreStragg). As can be seen, Gradient coding achieved significantly better generalization error. We emphasize that the results in figures 6 and 7 do not use any artificial straggling, only the natural delays introduced by the EC2 cluster.
How is this stark difference possible? When stragglers were ignored we were, at best, receiving a stochastic gradient (when random machines are straggling in each iteration). As alluded to earlier, in this case the best we could do as an optimization algorithm is to run gradient descent as it is robust to noise. When using gradient coding however, we could retrieve the full gradient which gave us access to faster optimization algorithms. In Figure 7 we used Nesterov’s Accelerated Gradient (NAG).
Another advantage of using full gradients is that we can guarantee that we are training on the same distribution as the one the training set was drawn from. This is not true for the approach that ignores stragglers. If a particular machine is more likely to be a straggler, samples on that machine will likely be underrepresented in the final model, unless particular countermeasures are deployed. There may even be inherent reasons why a particular sample will systematically be excluded when we ignore stragglers. For example, in structured models such as linear-chain CRFs, the computation of the gradient is proportional to the length of the sequence. Therefore, extraordinarily long examples can be ignored very frequently.
Conclusion
In this paper, we have experimented with various gradient coding ideas on Amazon EC2 instances. This is a complex trade-off space between model sizes, number of samples, worker configurations, and number of workers. Our proposed schemes create computation overheads while keeping communication the same.
The benefit of this additional computation is fault-tolerance: we are able to recover full gradients, even if machines do not deliver their assigned work, or are slow in doing so. Moreover, our partial straggler schemes provide fault tolerance while allowing all machines to do partial work. They however require an extra round of communication. An interesting open problem here is whether partial work on all machines is possible without this extra round of communication. Another open question under our framework is that of approximate gradient coding: can we get a vector that is close to the true gradient, with lesser computation overheads ? Ignoring stragglers does give the approximate gradient in a sense. However, is it possible to have a better approximation with on little computation overheads (relative to gradient coding) ?
For several model-cluster configurations that we tested, communication was the bottleneck and hence the additional computation’s effect on iteration times was negligible. This is the regime where gradient coding is most useful. However, this design space needs further exploration, that is also varying as different architectures change the parameter landscape. Overall, we believe that gradient coding is an interesting idea to add in the distributed large-scale learning arsenal.
Acknowledgements
This research has been supported by NSF Grants CCF 1344364, 1407278, 1422549, 1618689 and ARO YIP W911NF-14-1-0258.
References
Appendix - Proofs
Therefore, by construction, we have: , and the scheme is robust to any stragglers.
2 Proof of Theorem 1
Now, it is easy to see that the degree of the worker is .
Also, for any partition , its degree must be at least . If its degree is or less, then consider the scenario where all its neighbors are stragglers. In this case, there is no non-straggler worker with access to , which contradicts robustness to any stragglers.
Based on the above discussion, and using the fact that the sum of degrees of the workers in the bipartite graph must be the same as the sum of degrees of partitions, we get:
Since we assume all workers get access to the same number of partitions, this gives:
3 Proof of Theorem 2
Consider groups of partitions as follows:
Fix some set . Based on our construction, it is easy to observe that for any group , there exists some index in , say , such that the corresponding row in , has all s at partitions in and s elsewhere. This is because there are rows of that correspond in this way to (one in each block ), and so at least one would survive in the set of cardinality . Now, it is trivial to see that:
we have .
Finally, since the above holds for any set , we get that satisfies Condition 1. The remainder of the theorem follows from Lemma 1.
4 Proof of Theorem 3
Consider the subspace given by the null space of the random matrix (constructed in Algorithm 2):
Note that has different random values ( for each column), since its last column is simply the negative sum of its previous columns. Now, we have the following Lemma listing some properties of and .
Any columns of are linearly independent with probability
, where is the all-ones vector
For , let denote the set . Then, corresponds to the support of the row of in our construction, as also given by the support structure in Eq. (3.2).
Recall that we denote the row of by . By our construction, we have:
Consider the row of constructed using Algorithm 2 (also shown in Eq. 7.4). Then,
Every element of is non-zero with probability 1
For any subset , , the set of vectors is linearly independent with probability 1
Now, using Lemma 3, we can conclude that for any subset , , and . Consequently, from Lemma 2, since and , this implies that:
and, . Taking union bound over every shows that satisfies Condition 1. The remainder of the theorem follows from Lemma 1.
Consider any subset , such that . Then, all the elements of are independent, and is a polynomial in the elements of . Consequently, since every element is drawn from a continuous probability distribution (in particular, Gaussian), the set is a zero measure set. So, , and thus the columns of are linearly independent with probability 1.
where we let . The elements of are independent, so using the same argument as above, we again have . Finally, taking a union bound over all sets of cardinality shows that any columns of are linearly independent.
Since any columns in are linearly independent, this implies that . Since the subspace is simply the null space of , we have .
Finally, since (by construction), we have and thus .
4.2 Proof of Lemma 3
Now, if possible, let for some , . Then, since , we have:
Consequently, the set of columns is linearly dependent which contradicts having any columns being linearly independent (in Lemma 2). Therefore, we must have every element of being non-zero.
Now, consider any subset . We shall show that the matrix (corresponding to the rows of with indices in ) has rank with probability . Consequently, the set of vectors would be linearly independent. To show this, we consider some columns of , say given by the set , and denote the sub-matrix of columns by . Then, it suffices to show that . Now, by the construction in Algorithm 2, we have: , for some polynomials and in the entries of . Therefore, if we can show that there exists at least one with and , then under a choice of i.i.d. standard Gaussian entries of , we would have:
Let us pick a random matrix as:
where is a matrix with the same support as and with each non-zero entry i.i.d. standard Gaussian, and is a diagonal matrix such that . Note that a consequence of the above choice of is that the sum of all its rows is the all s vector. Now, it can be shown that any columns of form an invertible sub-matrix with probability 1. Let be the support of the row of . The rows of have the supports . Now because of the cyclic support structure in , any collection satisfies the property:
Using Lemma 4 in Dau et al. (2013), this implies that there is a perfect matching between the rows of and any of its columns . Consequently, with probability 1, any columns of form an invertible sub-matrix. Also, since every column of contains at least one non-zero (again, owing to the support structure of ), this implies that with probability 1, all the diagonal entries of are non-zero. Combining the above two observations, we can infer that any columns of form an invertible sub-matrix with probability 1.
So far, we have shown existence of a matrix with the following properties: (i) has the same support structure as , (ii) any columns of form invertible sub-matrix, (iii) the sum of all rows of is the all s vector. Now, for any such , we shall show that there exists an such that such that any columns of form an invertible sub-matrix. This implies that when we run Algorithm 2 with this , the output matrix would be the same as on the rows in the set . The remainder of the proof then follows from our earlier discussion.
Now, consider any set . Suppose we pick any invertible , and set . Then, such an satisfies and its columns in the set form an invertible sub-matrix. Now, since invertibility on the set simply corresponds to (i.e. some fixed polynomial being non-zero), if we actually picked a uniformly random on the subspace , then
Taking a union bound over all s, we get that
Thus, there exists an satisfying with any of its columns forming an invertible sub-matrix. Also, since the sum of all rows of is , this implies .