\section{Introduction}
The steady growth of deep neural networks over the years has made it impractical to train them from scratch on a single \emph{worker} (i.e., computational device). Distributing the computations over several workers can drastically reduce the training time. However, due to the sequential nature of the widely used stochastic gradient descent (SGD) method, distributing the process is not an easy task.   

Synchronous SGD (SSGD) is the most common method used to distribute the learning process across multiple workers. Several recent works \cite{fast_imagenet_1,fast_imagenet_2,fast_imagenet_3,facebook1hour} have shown that SSGD can achieve large speedups while maintaining high accuracy. The major drawback of SSGD is that its speed is confined to the slowest worker in every iteration. This shortcoming is magnified in non-dedicated\footnote{An environment of computation nodes who are not specifically optimized to work together} environments such as cloud computing. For this reason, all the above mentioned works were forced to use homogeneous workers in a dedicated network, which serves to reduce the variance in the workers' iteration times. Unlike cloud computing, dedicated networks are expensive and therefore not available to most users.

In asynchronous SGD (ASGD), each worker communicates independently of the others, thereby addressing the major drawback of SSGD. ASGD enjoys linear speedup in terms of the number of workers, even on non-dedicated networks. This makes ASGD a potentially better alternative to SSGD when using cloud computing. Unfortunately, ASGD also has a significant weakness known as \emph{gradient staleness}; the gradients used to update the parameter server's (master) parameters are often based on older parameters and therefore are inaccurate.
Prior works have shown that gradient staleness severely hinders the convergence process by reaching reduced final accuracy \cite{chen2016revisiting,cui2016geeps}. \citet{begets} showed that gradient staleness also induces \emph{implicit momentum}, thus the momentum coefficient $\gamma$ must be decayed when scaling up the number of workers. Most research works measure the gradient staleness of a worker according to the \emph{delay}: the number of master updates since the worker began calculating the stochastic gradient $g$, until $g$ is used to update the master. To overcome gradient staleness, \citet{stale_aware} proposed \emph{Staleness-Aware} (SA), which penalizes the step size of stale gradients linearly to their \emph{delay}. This method was later embraced by other works \cite{jiang2017heterogeneity,edge,giladi2019stability} and is currently the common method for penalizing stale gradients.
Unfortunately, this method suffers from a degradation of the final accuracy, especially when scaling up the number of workers. In \Cref{sec:SA}, we show that the main reason for this degradation is the over-penalization and under-penalization caused by SA.

SSGD and ASGD rely on hyperparameter tuning for every different number of workers \cite{data_parallelism}. Tuning is extremely time-consuming, thus avoiding it is beneficial, whenever possible. 

\textbf{Our contribution:} To mitigate gradient staleness while minimizing the degradation of final accuracy, we define a measure of gradient staleness we refer to as the \emph{Gap}. The Gap is based on the difference between the parameters used to calculate the gradient and the parameters on which the gradient is applied. We propose a new method called \emph{Gap-Aware} (GA) that penalizes the step size of stale gradients linearly to their Gap, while eliminating the over-penalization or under-penalization of SA. No new hyperparameters are introduced using the GA method.
\begin{itemize}
	\item We show that GA out-performs SA, especially as the number of workers scales up.
 	\item We prove that the convergence rate of the GA-ASGD algorithm with a non-convex loss function is consistent with SGD: $\mathcal{O}\left(\frac{1}{\sqrt{BK}}\right)$ where $K$ is the total number of steps and B is the batch size.
 	\item We show that penalizing the gradient itself rather than the step-size, eliminates under-penalization.
 	\item Our results suggest that GA can be used without re-tuning the hyperparameters.
 	\item As opposed to conclusions by \citet{begets}, we show that applying momentum in an asynchronous environment is advantageous (using GA), even when multiple workers are used.
    \item We combine GA with Adam \cite{adam} (Adam-GA), and show that Adam-GA achieves almost two orders of magnitude better perplexity than Adam or Adam-SA (which combines Adam with SA) using several workers on the Transformers-XL model.
\end{itemize}
Our results establish GA as a superior gradient-penalizing option to SA and suggest that using GA is a preferable alternative to SSGD in non-dedicated networks such as cloud computing, even when scaling to large numbers of workers. 
To validate our claims, we performed experiments on the CIFAR10, CIFAR100 \cite{cifar}, ImageNet \cite{imagenet}, and WikiText-103 \cite{wikitext-103} datasets, using several state-of-the-art architectures. A version of GA has reached 72.18\% final test accuracy on the ImageNet dataset using 128 simulated asynchronous workers. As far as we know, this is the largest number of asynchronous workers reported to converge on ImageNet. 

\section{Related Work}
Eliminating gradient staleness is a challenging task and several papers suggested techniques to reduce its detrimental effects.
\citet{dcasgd} proposed DC-ASGD, which uses a Taylor expansion to mitigate the gradient staleness. EASGD \cite{elastic} uses a \emph{center force} to pull the workers' parameters toward the master's parameters. Both DC-ASGD and EASGD achieve high accuracy on small numbers of workers, but fall short when trained on large clusters.
\citet{chan2014distributed} proposed penalizing stale gradients by reducing their size and thus limiting their effect on the learning process. They suggest decaying the learning rate exponentially to the \emph{delay}; this makes the step size arbitrarily small when the amount of workers grows, virtually ceasing the learning process.
As part of their convergence analysis, \citet{dutta2018slow} suggest a penalizing method that is linear to the norm between the master and worker's parameters. However, their method introduces another hyperparameter, which requires additional time to tune. As opposed to other methods, GA performs well even when the number of workers is large, without introducing new hyperparameters.

\citet{gupta2016model} as well as \citet{async_transformer} suggest collecting several gradients before updating the master to reduce the effects of gradient staleness. \citet{terngrad} propose minimizing the size of the gradients to reduce communication times. GA is orthogonal to both of these approaches.

\section{Background}
\label{sec:background}
The goal of an optimization procedure is to minimize $f(\theta)$, where $f$ is a smooth, but not necessarily convex, objective function (a.k.a. loss) and the vector $\theta \in \mathbb{R}^d$ is the model's parameters:

\begin{equation}
  \theta_* = \argmin_{\theta\in \mathbb{R}^d}  f(\theta) := \mathbb{E}_\xi[F(\theta; \xi )]
  \label{eq:main}
\end{equation}
where $\xi \in \Xi$ is a random variable from $\Xi$, the entire set of training samples $\Xi = \{1, 2, \cdots, M\}$. $F(\theta; \xi)$ is the stochastic loss function with respect to the training sample indexed by $\xi$. SGD is commonly the workhorse behind the optimization of deep neural networks. Denoting $\eta$ as the learning rate and $k$ as the step number, SGD's iterative update rule is: $\theta_{k+1}=\theta_k-\eta_k \nabla f(\theta_k)$.
We denote $X_k$ as the variable $X$ at the $k^{th}$ step, where $X$ is any variable.

\paragraph{Momentum}
Momentum \cite{momentum} is a widely adopted optimization technique due to its accelerated convergence and oscillation reduction \cite{sutskever}. 
Instead of simply using the gradient, the momentum iterative update rule\footnote{We consider the version of momentum without dampening} uses an exponentially-weighted moving average of gradients called the \emph{update vector}: $v_{k+1}=\gamma v_k+\nabla f(\theta_k)$. The update rule is: $\theta_{k+1}=\theta_k-\eta_k v_{k+1}$.
\emph{Nesterov's Accelerated Gradient} (NAG) \cite{nesterov} is a well-used variation of momentum that has been proven to achieve quadratic speedup in convergence rate compared to SGD.


\section{Asynchronous SGD (ASGD)}
\label{sec:asgd}
We consider the commonly used ASGD, which operates with a parameter-server (master), used to keep the model's most up-to-date parameters. Each worker maintains a replica of the model's parameters. The workers run in parallel and synchronize with the master independently from each other at the beginning of each batch iteration. We denote $\tau_k$ as the delay at the $k^{th}$ step.
The worker and master algorithms are given by \Cref{alg:NAG-ASGD_worker} and \ref{alg:NAG-ASGD_master}, respectively, where B is the batch-size and $\xi_{k,b}$ denotes the $b^{th}$ sample in the batch sampled at the $k^{th}$ iteration. \emph{Gradient staleness} appears when a gradient $g_k$ is computed on parameters $\theta_{k-\tau_k}$ but applied to different parameters $\theta_{k}$.

\begin{minipage}[t]{0.55\textwidth}
\vspace{-4ex}
\centering
\begin{algorithm}[H]
	\caption{Momentum-ASGD: worker $i$}
	\label{alg:NAG-ASGD_worker}
	\centering
	\begin{algorithmic}
	    \setlength{\itemindent}{-0.5em}
	    \STATE Always do:
		\STATE \quad Receive parameters $\theta_{k-\tau_k}$ from the master
		\STATE \quad Get $B$ training samples $\xi_{k-\tau_k,[1\dots B]}$
		\STATE \quad Compute gradient: $g^i_k \gets \sum_{b=1}^B\frac{\nabla F(\theta_{k-\tau_k}; \xi_{k-\tau_k,b})}{B}$
		\STATE \quad Send $g^i_k$ to the master
	\end{algorithmic}
\end{algorithm}
\end{minipage}
\hfill
\begin{minipage}[t]{0.42\textwidth}
\vspace{-4ex}
\centering
\begin{algorithm}[H]
\caption{Momentum-ASGD: master}
\label{alg:NAG-ASGD_master}
\begin{algorithmic}
    \setlength{\itemindent}{-0.5em}
    \STATE For k = 1...K do:
    \STATE \quad Receive gradient $g^i_k$ from worker $i$
    \STATE \quad Update momentum $v_{k+1} \gets \gamma v_k+g^i_k$
    \STATE \quad Update master's weights: \\ \quad $\theta_{k+1} \gets \theta_k-\eta_k \cdot v_{k+1}$
    \STATE \quad Send $\theta_{k+1}$ to worker $i$
\end{algorithmic}
\end{algorithm}
\end{minipage}

\subsection{Staleness-Aware}
\label{sec:SA}
\citet{stale_aware} proposed a gradient penalization method called \emph{Staleness-Aware} (SA). SA aims to reduce the effects of \emph{gradient staleness} by dividing the step size by its corresponding $\tau$ (\Cref{alg:Original_Stale-Aware_master}). The worker algorithm remains unchanged. The two ideas behind SA is that stale gradients should be penalized to reduce their impact and that gradient staleness scales up with $\tau$ . 
SA successfully mitigates the gradient staleness when $N$ is small. Although commonly used, SA can potentially over-penalize as well as under-penalize stale gradients as we show below. 

\begin{minipage}[t]{0.48\textwidth}
\vspace{-4ex}
\centering
\begin{algorithm}[H]
\caption{Staleness-Aware: master}
\label{alg:Original_Stale-Aware_master}
\begin{algorithmic}
    \setlength{\itemindent}{-0.5em}
    \STATE Initialize an iteration array: $iter = [0]*N$
    \STATE For k = 1...K do:
    \STATE \quad Receive gradient $g^i_k$ from worker $i$
    \STATE \quad Calculate worker $i$'s delay $\tau_k \gets k-iter[i]$
    \STATE \quad Update momentum $v_{k+1} \gets \gamma v_k+g^i_k$
    \STATE \quad Update master $\theta_{k+1} \gets \theta_k-\frac{\eta_k}{\tau_k} v_{k+1}$
    \STATE \quad Send $\theta_{k+1}$ to worker $i$
    \STATE \quad Save current iteration $iter[i] \gets k$
\end{algorithmic}
\end{algorithm}
\end{minipage}
\hfill
\begin{minipage}[t]{0.51\textwidth}
\vspace{-4ex}
\centering
\begin{algorithm}[H]
    \caption{Gap-Aware: master}
    \label{alg:Gap_master}
    \begin{algorithmic}
        \setlength{\itemindent}{-0.5em}
        \STATE For k = 1...K do:
        \STATE \quad Receive gradient $g_k^i$ from worker $i$
        \STATE \quad Calculate Gap: $G_k = \frac{|\theta_k-\theta_{k-\tau_k}|}{C}+\mathbf{1}^d$
        \STATE \quad Update momentum $v_{k+1} \gets \gamma v_k+\left(\frac{1}{G_k}\right)\odot g^i_k$
        \STATE \quad Update master $\theta_{k+1} \gets \theta_k-\eta_k v_{k+1}$
        \STATE \quad Save and send current parameters 
        \STATE \quad \quad $\theta_{k+1}$ to worker $i$
    \end{algorithmic}
\end{algorithm}
\end{minipage}

\paragraph{Over-Penalizing}
\label{sec:over_penalty}
Let us assume that at some step k, after $\tau$ master updates we get $\theta_k = \theta_{k-\tau}$ just as the gradient calculated on $\theta_{k-\tau}$ is applied. This means there is no gradient staleness for the next gradient update since it was computed using the same parameters on which it is applied. Unfortunately, the delay remains $\tau > 0$, thereby causing over-penalization when SA is used.

Additionally, $\tau$ scales linearly with $N$, which dramatically reduces $\eta$ when $N$ is large. Consequently, on large numbers of workers, the convergence rate of SA is sluggish and its accuracy plummets.

\paragraph{Under-Penalizing}
\label{sec:stale_ver}
\iffalse
\begin{figure*}[t]
    \centering
    \begin{subfigure}[t]{0.4\textwidth}
            \includegraphics[width=\textwidth]{Figures/TestError_gamma_CIFAR10resnet_StaleAware2.pdf}
        	\label{fig:test_stale_versions}
    \end{subfigure}
    \begin{subfigure}[t]{0.4\textwidth}
            \includegraphics[width=\textwidth]{Figures/TrainError_gamma_CIFAR10resnet_StaleAware2.pdf}
        	\label{fig:train_stale_versions}
    \end{subfigure}
    \caption{Final test and train error for different number of workers. The figure shows the average (bold line) and standard deviation (band) of 5 runs on the CIFAR10 dataset using the ResNet-20 model. The black dashed line represents the average result of sequential SGD using a single worker. Original SA penalizes $\eta$ while the new SA penalizes the gradient.}
    \label{fig:stale_versions}
\end{figure*}
\fi
The SA method doesn't take into account that when using momentum, the update step also contains past gradients. To emphasize the importance of this issue, let's examine a fictional example: assume that some gradient $g_k$ is very stale ($\tau_k$ is large). Following the SA technique, the update rule is: $\theta_{k+1} = \theta_{k}-\frac{\eta_k}{\tau_k} \cdot v_{k+1} = \theta_{k}-\frac{\eta_k}{\tau_k} \cdot (\gamma v_{k} +g_k)$.
This means the stale gradient $g_k$ is indeed penalized by being multiplied by $\frac{\eta_k}{\tau_{k}}$, which is small; thus $g_k$ doesn't change the parameters much. 
We further assume the next iteration is very fast ($\tau_{k+1}$ is small). The next update will be: $\theta_{k+2}= \theta_{k+1}-\frac{\eta_{k+1}}{\tau_{k+1}} \cdot (\gamma^2 v_{k} +\gamma g_{k} +g_{k+1})$.
The stale gradient $g_{k}$ is multiplied by $\gamma\frac{\eta_{k+1}}{\tau_{k+1}}$, which is large (assuming $\eta_k \approx \eta_{k+1}$). Despite the fact that $g_k$ was stale, it still has a significant impact on the learning process. In other words, $g_k$ is \emph{under-penalized}.

To eliminate this possibility we penalize the stale gradient itself rather than the learning rate.
Using this method, the staleness of each gradient is accounted for within the update vector $v$.

\section{Gap-Aware (GA)}
\label{sec:GA}
In this section we propose a new method called \emph{Gap-Aware} to mitigate over and under-penalization.

\subsection{The Gap as a Measure of Gradient Staleness}
An intuitive method to measure gradient staleness would be: $\norm{\nabla f(\theta_k) - \nabla f (\theta_{k-\tau_k})}$. This essentially measures the difference between the stale gradient and the accurate gradient that is computed on the up-to-date parameters. (Of course, $\nabla f(\theta_k)$ is never calculated in ASGD algorithms.) Commonly used in deep learning is the Lipschitzian gradients assumption:
\begin{equation}
\label{ass:Lip_g}
    \norm{\nabla f\left(x\right) - \nabla f\left(y\right)} \leq L\norm{x-y}, \quad \forall{x}, \forall{y}, L \in \mathbb{R}
\end{equation}
Setting $x = \theta_k, y= \theta_{k-\tau_k}$ into Equation~\ref{ass:Lip_g} we get: $\norm{\nabla f\left(\theta_k\right) - \nabla f\left(\theta_{k-\tau_k}\right)} \leq L\norm{\theta_k-\theta_{k-\tau_k}}$.
This implies that $\norm{\theta_k-\theta_{k-\tau_k}}$ is a valid (and easily calculated) measure of the gradient staleness. This measure also addresses the delay's over-penalization; using the same simple example described in \Cref{sec:over_penalty}, 
the term $\norm{\theta_k-\theta_{k-\tau_k}}$ will now be zero, correctly measuring the gradient staleness.

The learning rate $\eta$, commonly decays as the training progresses. This decay can be viewed as a built-in penalization to reduce variance.
Following this notion, we suggest reducing the staleness penalization as $\eta$ decays. 
To accommodate all the attributes above, we define the \emph{Gap}: 

\begin{definition} \label{def:gap}
$G_k$, the Gap at the $k^{th}$ step, is defined as the minimal number of updates required to traverse the current distance between the master's and worker's parameters using the maximal learning rate and assuming all gradients have an average norm. $G_k \in \mathbb{R}$ is defined as:
\begin{equation*}
    G_k = \norm{\theta_{k}-\theta_{k-\tau_k}}/{C}+ 1
\end{equation*}
Where $C = \eta_{max}\mathbb{E}_k[\norm{\nabla f(\theta_{k-\tau_k})}]$ is a constant representing the maximal distance the parameters can travel in a single update, given the gradient's norm is the average gradient norm.
\end{definition}

\Cref{def:gap} means that dividing $\eta$ by the Gap produces larger steps than those produced by \emph{SA}
This allows exploring more distant minimas while still mitigating the gradient staleness. 
Note that $\mathbb{E}[G_k] = \tau_k$ occurs only if all previous $\tau_k$ updates were in the exact same direction, which rarely happens. Empirically, we found that $\mathbb{E}[G_k] < \tau_k$ (See \Cref{sec:delay_gap}).

To mitigate the gradient staleness, while eliminating the over-penalization and under-penalization, we divide the gradients themselves by their respective Gap. 
We refer to this method as \emph{Gap-Aware} (GA).

\subsection{Convergence Analysis}
In this section we provide the outlines of the convergence analysis. The complete proofs are given in \Cref{sec:proofs}. The GA-ASGD update rule (without momentum) is:
\begin{equation} \label{eq:ASGD_update_2}
    \theta_{k+1}=\theta_{k}-\eta_k \left(\frac{1}{G_k}\right)\cdot\sum_{b=1}^B \nabla F(\theta_{k-\tau_k};\xi_{k-\tau_k,b})
\end{equation}
The convergence rate is the speed (or number of steps) at which a convergent sequence approaches its limit.
We follow ideas similar to \citet{asgd_conv} to show that the upper bound of the convergence rate of GA on a non-convex loss function is similar to that of SGD. 
\begin{assumption}
\label{ass:1}
We assume the following, commonly-used assumptions, hold:
\item[$\bullet$ \textbf{Unbiased gradient:}] The stochastic gradient $\nabla F(\theta; \xi)$ is unbiased:
\begin{equation}
\label{ass:unbiased_g}
    \nabla f(\theta) = \mathbb{E}_{\xi} [\nabla F(\theta;\xi)]
\end{equation}
\item[$\bullet$ \textbf{Bounded variance:}] The variance of the stochastic gradient is bounded:
\begin{equation}
\label{ass:bound_var}
   \mathbb{E}_{\xi}[\norm{\nabla F(\theta;\xi) -\nabla f(\theta)}^2] \leq \sigma ^2, \quad \forall \theta
\end{equation}
\item[$\bullet$ \textbf{Lipschitzian gradients:}] See Equation~\ref{ass:Lip_g}. 
\item[$\bullet$ \textbf{Independence:}]  All the random variables $\{\xi_{k,b}\}_{k=1\dots K; b=1\dots B},$ are independent.
\item[$\bullet$ \textbf{Bounded age:}]  All delay variables $\tau_1, ... \tau_K$ are bounded:
\begin{equation}
\label{ass:bound_age}
   \max_k {\tau_k} \leq T
\end{equation}
\end{assumption}

\begin{theorem} 
\label{thm:convergence} Assume that Assumption~\ref{ass:1} holds and the learning rate sequence $\{\eta_k\}_{k=1\cdots K}$ satisfies:
\begin{align}
    \frac{BL\eta_k}{G_k}+ 2B^2L^2T \sum_{t=1}^{T}\frac{\eta_{k+t}^2}{G_{k+t}^2}  \leq 1 \quad \text{for all $k=1,2,...$}
    \label{algo1ass}
\end{align}
We have the following ergodic convergence rate for the iteration of GA-ASGD:
\begin{equation}
\label{eq:thm_1}
    \frac{1}{\sum_{k=1}^K\frac{\eta_k}{G_k}}\sum_{k=1}^K\frac{\eta_k}{G_k} \mathbb{E}(\left\| \nabla f(\theta_{k}) \right\|^2) \leq \frac{\frac{2(f(\theta_1) -f(\theta^*))}{B} + \sum_{k=1}^K\left( \frac{\eta_k}{G_k} + 2B L \sum_{j=k-T}^{k-1} \frac{\eta_j^2}{G_j^2} \right)\frac{\eta_k L\sigma^2}{G_k}}{\sum_{k=1}^K\frac{\eta_k}{G_k}}
\end{equation}
Where $\mathbb{E}[\cdot]$ denotes taking expectation in terms of all random variables.
\end{theorem}
To simplify the upper bound in \Cref{thm:convergence}, we observed that setting the learning rate $\eta_k$ such that the expression $\frac{\eta_k}{G_k}$ is a constant value across all $k$ obtains the following convergence rate:
\begin{corollary} \label{cor:conv}
Assume that Assumption~\ref{ass:1} holds and that $\eta_k$ are set such that $\frac{\eta_k}{G_k}$ is constant for any $k$ as follows:
\begin{align}
    \frac{\eta_k}{G_k} := \eta = \sqrt{\frac{f(\theta_1) - f(\theta^*)} {BLK\sigma^2}}, \quad \forall k \in [1, \dots , K]
    \label{eq:coro_1}
\end{align}
If the maximal delay parameter $T$ satisfies:
\begin{align}
    K \geq \frac{4BL(T+1)^2(f(\theta_1)- f(\theta^*))}{\sigma^2}
    \label{eq:coro_2}
\end{align}
then the output of GA satisfies the following ergodic convergence rate:
\begin{equation}
\begin{split}
    &\min_{k\in \{1, \cdots, K\}}\mathbb{E}[\|\nabla f(\theta_k)\|^2] \leq {\frac{1}{K}}\sum_{k=1}^K 
    \mathbb{E}[\|\nabla f(\theta_k)\|^2] \leq             4\sqrt{\frac{(f(\theta_1)- f(\theta^*))L\sigma^2}{BK}}
\end{split}
\end{equation}
\end{corollary}
\Cref{cor:conv} claims that if the total iterations $K$ is greater than $\mathcal{O}(BT^2)$, the convergence rate achieves $\mathcal{O}({1 / \sqrt{BK}})$, which is consistent with the convergence rate of ASGD presented in \citet{asgd_conv}, and with the convergence rate of SGD. 


\subsection{Gap-Aware Versions}
\label{sec:gap_ver}
We explore three ways to measure $G_k$:
\begin{itemize}
\item \textbf{Global:} $G_k \in \mathbb{R}$ as defined in \Cref{def:gap}.
\item \textbf{Layer-wise:} Every layer is penalized differently and independently. $G_k \in \mathbb{R}^P$ where $P$ is the number of layers in the model. We denote  $\mathbf{1^S}$ as an S-dimensional vector of ones. We denote any vector $X_{*,p}$ is the $p^{th}$ layer in the vector $X_*$. Every element in $G_k$ is calculated per-layer:
\begin{equation}
\label{def:layer_wise}
    G_{k,p} = {\norm{\theta_{k,p}-\theta_{k-\tau_k,p}}}/{C_{p}}+ \mathbf{1^p}
\end{equation}
\item \textbf{Parameter-wise:} Every parameter (element in the parameter vector $\theta$) is penalized differently and independently. $G_k \in \mathbb{R}^d$ where $d$ is the number of parameters. We denote $|\cdot|$ on vector $X$, as the absolute value per element of $X$. Every element in $G_k$ is calculated and applied per-element:
\begin{equation}
\label{def:parameter_wise}
    G_{k} = {|\theta_{k}-\theta_{k-\tau_k}|}/{C}+ \mathbf{1^d}
\end{equation}
Where $C \in \mathbb{R}^d$ is also calculated element-wise.
Specifically, $C = \eta_{max}\mathbb{E}_k[|\nabla f(\theta_{k-\tau_k})|]$.
\end{itemize}

We tested these variations on three different frameworks\footnote{A framework is a unique combination of dataset and model. See full experiment in \Cref{sec:gap_ver_exp}.} to determine which technique has the best performance.
\Cref{fig:gap_versions} demonstrates that the \emph{parameter-wise} method (\eqref{def:parameter_wise}) resulted in the best test and train error. Since this phenomenon repeats across all frameworks, we henceforth use the \emph{parameter-wise} method in the Gap-Aware algorithm. We denote $\odot$ as an element-wise multiplication between vectors and describe the final GA algorithm of the master as \Cref{alg:Gap_master}.
The worker algorithm remains the same as in \Cref{alg:NAG-ASGD_worker}.
\begin{figure*}[t]
    \centering
    \begin{subfigure}[t]{0.4\textwidth}
            \includegraphics[width=\textwidth]{Figures/GapVersions_TestError_gamma_CIFAR10wr_new.pdf}
        	\label{fig:test_gap_CIFAR10wr}
    \end{subfigure}
    \begin{subfigure}[t]{0.4\textwidth}
            \includegraphics[width=\textwidth]{Figures/GapVersions_TrainError_gamma_CIFAR10wr_new.pdf}
        	\label{fig:train_gap_CIFAR10wr}
    \end{subfigure}
    \captionsetup{aboveskip=-2pt}
    \captionsetup{belowskip=-2pt}
    \caption{Final test and train error for different numbers of asynchronous workers $N$. The figure shows the average (bold line) and standard deviation (band) of 5 runs on the CIFAR10 dataset using the WideResNet model. The black dashed line is the SGD error using a single worker.}
    \label{fig:gap_versions}
\end{figure*}

\section{Experiments}
\label{sec:exp}
We simulated multiple distributed workers\footnote{A worker is a machine with one or more accelerators (i.e., GPU). ASGD methods treat each machine with multiple accelerators, all working synchronously locally, as a single worker.} to measure the final test error, train error, and convergence speed of different cluster sizes. To validate that penalizing linearly to the \emph{Gap} is the factor that leads to better performance, we used the same hyperparameters across all the tested algorithms (see \Cref{sec:hyperparameters}). These hyperparameters are the ones tuned for a single worker, suggested by the authors of the respective papers for each framework. We simulated the workers' execution time using a \emph{gamma-distributed model} \cite{Ali:2000:TET} (see \Cref{sec:gamma_distribution}), where the execution time for each individual batch was drawn from a gamma distribution. The gamma distribution is a well-accepted model for task execution time, which naturally gives rise to stragglers. The importance of asynchronous over synchronous training is explained in \Cref{sec:async_speedup}.

\paragraph{Combining GA with DANA}
One way to verify whether it is better to penalize using the Gap or the delay, is to change the Gap while fixing the delay, and examining the results using GA and SA. Momentum generally increases the norm of the update vector; this in turn, increases the effective step size thus increasing the Gap for a given delay. \citet{dana} introduced DANA, which uses the momentum to estimate the master's parameters at the time of the gradient update, thus decreasing the \emph{Gap}.
The combination of decreasing the \emph{Gap} using DANA and penalizing stale gradients using GA or SA is easily integrated since both methods are orthogonal. In our experiments, we also compared between DANA-Gap-Aware (DANA-GA) and DANA-Staleness-Aware (DANA-SA). We note that since DANA decreases the Gap, DANA-GA penalizes much less than DANA-SA (see \Cref{sec:delay_gap}).

\paragraph{Algorithms}
Our evaluations consist of the following algorithms:
\begin{itemize}
	\item \emph{Baseline:} Single worker with the same tuned hyperparameters as in the respective framework's paper. See \Cref{sec:hyperparameters}.
 	\item \emph{ASGD:} ASGD (\Cref{alg:NAG-ASGD_master}) without momentum ($\gamma = 0$).
	\item \emph{NAG-ASGD:} ASGD with momentum (\Cref{alg:NAG-ASGD_master}) using a NAG optimizer.
	\item \emph{Staleness-Aware:} SA as described in \Cref{alg:Original_Stale-Aware_master}, using a NAG optimizer.
	\item \emph{Gap-Aware:} Parameter-wise GA (\Cref{alg:Gap_master}) as described in Section~\ref{sec:GA}, using a NAG optimizer. 
	\item \emph{DANA:} DANA (\Cref{alg:DANA_master}) as described in \Cref{sec:ap_algorithms}.
	\item \emph{DANA-SA:} DANA-Staleness-Aware (\Cref{alg:DANA-Stale-Aware_master}) as described in \Cref{sec:ap_algorithms}.
	\item \emph{DANA-GA:} DANA-Gap-Aware (\Cref{alg:DANA-Gap-Aware_master}) as described in \Cref{sec:ap_algorithms}.
	\item \emph{Adam:} Adam (\Cref{alg:Adam_master}) as described in \Cref{sec:ap_algorithms}.
	\item \emph{Adam-SA:} Adam-Staleness-Aware (\Cref{alg:Adam-Stale-Aware_master}) as described in \Cref{sec:ap_algorithms}.
	\item \emph{Adam-GA:} Adam-Gap-Aware (\Cref{alg:Adam-Gap-Aware_master}) as described in \Cref{sec:ap_algorithms}.
\end{itemize}

Our evaluation was extensive on image classification tasks such as CIFAR10, CIFAR100 \cite{cifar}, and ImageNet \cite{imagenet}. It also included a language modeling task using the WikiText-103 corpus \cite{wikitext-103}. All datasets and models are detailed in \Cref{sec:data_model}.

\subsection{Evaluation on CIFAR}
\label{sec:exp_cifar}
\begin{figure*}[t]
    \centering
    \begin{subfigure}[t]{0.32\textwidth}
            \includegraphics[width=\textwidth]{Figures/TestError_gamma_CIFAR10resnet_all_algos_wd_bc_48.pdf}
            \caption{CIFAR10 ResNet-20}
        	\label{fig:test_CIFAR10resnet_all}
    \end{subfigure}
    \begin{subfigure}[t]{0.32\textwidth}
            \includegraphics[width=\textwidth]{Figures/TestError_gamma_CIFAR10_wr_all_algos_wd_bc_48.pdf}
        	\caption{CIFAR10 WideResNet}
        	\label{fig:test_CIFAR10wr_all}
    \end{subfigure}
        \begin{subfigure}[t]{0.32\textwidth}
            \includegraphics[width=\textwidth]{Figures/TestError_gamma_CIFAR100wr_all_algos_wd_bc_48.pdf}
        	\caption{CIFAR100 WideResNet}
        	\label{fig:test_CIFAR100wr_all}
    \end{subfigure}
    \caption{Final test error for different numbers of asynchronous workers $N$. Each line in the figure represents the average (bold line) and standard deviation (band) of 5 runs on a specific framework. The black dashed line represents the average result of SGD using a single worker.}
    \label{fig:test_all_algos}
\end{figure*}

\paragraph{Gradient Staleness Effects} In \Cref{fig:test_all_algos}, NAG-ASGD shows how gradient staleness is exacerbated by momentum. NAG-ASGD yields high accuracy with few workers, but the test error climbs sharply when more than 16 workers are used. On the other hand, ASGD without momentum performs poorly using few workers. When using many workers, ASGD significantly surpasses NAG-ASGD because of the implicit momentum generated in asynchronous training \cite{begets}.

\paragraph{SA \& GA}\Cref{fig:test_all_algos} also demonstrates that both staleness penalization methods (GA and SA) out-perform the naive NAG-ASGD. GA results in better final test error than SA across all experiments. This empirically proves that GA is the better method for penalizing the gradients. We claim this occurs mainly because SA over-penalizes the gradients, thereby making it impossible to reach any distant, good minima when the number of steps is limited (for more details see \Cref{sec:delay_gap}). 

\paragraph{DANA Versions} \Cref{fig:test_all_algos} shows that DANA potentially diverges when $N$ grows as opposed to DANA-GA and DANA-SA. This shows that DANA benefits from staleness penalization. Furthermore, DANA-GA out-performs all other methods and remains close to the baseline's error across all frameworks. The fact that DANA-GA out-performs DANA-SA validates that GA is superior to SA.

\iffalse
\paragraph{Convergence Rate} \Cref{fig:conv_all_algos} shows that the convergence rate of GA is similar to that of SA, despite using a larger step size. This suggests that GA does not require more steps than SA to reach its (better) minima. DANA-GA's convergence rate remains very close to the baseline, which suggests that it doesn't require more steps to converge. This means that DANA-GA reaches similar test error as the Baseline in the same number of steps, while enjoying asynchronous speedup.
\fi

\paragraph{Tuned ASGD}
To validate that the staleness penalization helps overcome the gradient staleness and improve the results, we tuned the momentum and learning rate of ASGD using 32 workers on the 3 frameworks shown in \Cref{fig:test_all_algos}. For each framework, we performed a grid search of 70 perturbations (See \Cref{sec:app_tuned}).
\Cref{tab:tuned-table} shows that GA and DANA-GA, using the same hyperparameters as the baseline, provide similar or better results than tuning both $\gamma$ and $\eta$, which is highly time-consuming. 

\begin{table}[t]
\begin{minipage}[t]{0.56\textwidth}
\centering
\caption[Test accuracy on different frameworks. N=32.]{Test accuracy on different frameworks\footnote{C10/C100=CIFAR10/100, R=ResNet, WR=WideResNet.}. N=32.}
\begin{tabular}{c c c c}
\toprule
Framework & Tuned ASGD  & GA & DANA-GA \\
\midrule
\textbf{C10 R} & 88\% &  87.9\% &   \textbf{91.1\%} \\
\midrule
\textbf{C10 WR} & 91.6\% &  92.3\% &   \textbf{94.3\%} \\
\midrule
\textbf{C100 WR} & 71.1\% &  70.6\% &   \textbf{74.3\%} \\
\bottomrule
\end{tabular}
\label{tab:tuned-table}
\end{minipage}
\hfill
\begin{minipage}[t]{0.43\textwidth}
\centering
\caption{Final test perplexity using Transformer-XL on WikiText-103. (Baseline $24.25$. Lower is better).}
\begin{tabular}{c c c c}
\toprule
N & Adam & Adam-SA & Adam-GA \\
\midrule
4 &  1644.76 &  1210.8 &   \textbf{26.48} \\
8 &  1603.01 &  1129.9 &   \textbf{28.7} \\
\bottomrule
\end{tabular}
\label{tab:NLP}
\end{minipage}
\end{table}

According to \citet{begets}, if momentum is used, the asynchronous implicit momentum should impede the convergence as N increases. However, GA and DANA-GA, which use a large momentum, generally perform better than the tuned ASGD even when $N$ is large. This phenomenon repeats across all frameworks, which suggests that GA, and especially DANA-GA, can mitigate the asynchronous implicit momentum problem.
Tuning these methods should further improve the results.

The graphs of the train error also show the same concepts discussed here regarding the test error and are presented in \Cref{fig:train_all_algos}, \Cref{sec:more_res}. The convergence rate analysis appears in \Cref{sec:conv_rate}.

\subsection{ImageNet Experiments}
\begin{table*}[t]
\centering
\captionof{table}{ResNet-50 ImageNet final test accuracy (Baseline $75.64\%$)}
\begin{tabular}{c c c c c c c c}
\toprule
N & ASGD & NAG-ASGD & SA & GA & DANA & DANA-SA & DANA-GA \\
\midrule
32 & 70.53\% & 70.64\% & 61.73\% & 70.27\%  & 74.89\% &     65.66\% & \textbf{75.06\%}    \\
\midrule
48 &  69.05\% & 66.78\% &  56.22\% & 67.75\% & 73.75\% &     61.16\%  & \textbf{74.23\%}   \\
\midrule
64  & 67.1\% &  59.81\% &  50.79\% & 64.78\% &   69.88\% &     56.98\% & \textbf{74.11\%}   \\ 
\midrule
128 & NaN &  NaN &       NaN & NaN &   NaN &        NaN  & \textbf{72.18\%}   \\ 
\bottomrule
\end{tabular}
\label{tab:imagenet}
\end{table*}

We conducted experiments on the ImageNet dataset using the ResNet-50 model \cite{resnet}. Every asynchronous worker is a machine with 8 GPUs, so the 128 workers in our experiments simulate a total of 1024 GPUs. For reference, \citet{facebook1hour} used 256 GPUs synchronously. The hyperparameters we used are those of the tuned single worker (see \Cref{sec:hyperparameters}). \Cref{tab:imagenet} shows that GA out-performs SA due to the high number of workers,
which exacerbates the over-penalizing of SA. Unlike SA, GA out-performs NAG-ASGD as $N$ increases due to successful staleness mitigation. DANA-GA remains close to the baseline and better than any other method as $N$ increases. DANA-GA reaches 72.18\% final test accuracy when using 128 workers, which is the most asynchronous workers shown to converge on ImageNet as far as we know.

\subsection{NLP Experiments}
NLP tasks are usually trained using Adam \cite{adam}. To test SA and GA we implemented a version of Adam-SA and Adam-GA given by \Cref{alg:Adam-Stale-Aware_master} and \ref{alg:Adam-Gap-Aware_master}, respectively (\Cref{sec:ap_algorithms}). Transformer-XL \cite{transformer-xl} is a state-of-the-art model for NLP tasks; however, its sensitivity to gradient staleness is catastrophic \cite{async_transformer}. \Cref{tab:NLP} shows that GA successfully mitigates the gradient staleness and achieves near-baseline perplexity while SA results in a higher perplexity by almost two orders of magnitude. In this scenario, SA completely fails to mitigate the gradient staleness, proving the superiority of GA. (See hyperparameters in \Cref{sec:hyperparameters}).


\section{Conclusions}
The goal of this work is to mitigate gradient staleness, one of the main challenges of ASGD. We argue that penalizing stale gradients linearly to the \emph{delay}, as done in the widely used SA method, flounders due to over and under-penalization. We defined the \emph{Gap} to measure gradient staleness and proposed GA, a novel asynchronous distributed technique that mitigates the gradient staleness by penalizing stale gradients linearly to the Gap. We showed that GA surpasses SA across all frameworks, especially in NLP problems or when the number of workers is large. This presents GA as a superior alternative for staleness penalizing. We further introduced DANA-GA and demonstrated that DANA-GA mitigates gradient staleness better than any of the other methods we compared. Despite prior belief, DANA-GA's superb performance enables the use of momentum in asynchronous environments with many workers; it presents a desirable alternative for parallel training with multiple workers, especially on non-dedicated environments such as cloud computing.
In future work, we plan to examine what makes GA perform so well in NLP tasks.

\section*{Acknowledgement}
This work was supported by The Hasso Plattner Institute.


