I am currently training some models using gradient accumulation since the model batches do not fit in GPU memory. Since I am using gradient accumulation, I had to tweak the training configuration a bit. There are two parameters that I tweaked: the batch size and the gradient accumulation steps. However, I am not sure about the effects of this modification, so I would like to fully understand what is the relationship between the gradient accumulation steps parameter and the batch size.
I know that when you accumulate the gradient you are just adding the gradient contributions for some steps before updating the weights. Normally, you would update the weights every time you compute the gradients (traditional approach):
$$w_{t+1} = w_t - \alpha \cdot \nabla_{w_t}loss$$
But when accumulating gradients you compute the gradients several times before updating the weights (being $N$ the number of gradient accumulation steps):
$$w_{t+1} = w_t - \alpha \cdot \sum_{0}^{N-1} \nabla_{w_t}loss$$
My question is: What is the relationship between the batch size $B$ and the gradient accumulation steps $N$?
By example: are the following configurations equivalent?
- $B=8, N=1$: No gradient accumulation (accumulating every step), batch size of 8 since it fits in memory.
- $B=2, N=4$: Gradient accumulation (accumulating every 4 steps), reduced batch size to 2 so it fits in memory.
My intuition is that they are but I am not sure. I am not sure either if I would have to modify the learning rate $\alpha$.