Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gradient accumulation #52

Open
SamDuffield opened this issue Apr 16, 2024 · 0 comments
Open

Gradient accumulation #52

SamDuffield opened this issue Apr 16, 2024 · 0 comments
Labels
enhancement New feature or request (beyond just a new method)

Comments

@SamDuffield
Copy link
Contributor

SamDuffield commented Apr 16, 2024

In some settings you might want to run multiple steps before doing an update (aka gradient accumulation), e.g. to reduce the variance of the stochastic gradient without increasing memory requirements.

Perhaps this could implemented with a unified API but we'd need to think carefully about it.
One option might be to change the API from
update(state: TensorTree, batch: TensorTree)
to
update(state: TensorTree, batch: TensorTree | AccumulateBatch[TensorTree])

where AccumulateBatch is just a NamedTuple so we can differentiate it from a and then e.g.

if not isinstance(batch, AccumulateBatch):
      batch = accumulate(batch) # convert batch to an AccumulateBatch of length 1

vals = []
aux = []
for b in batch:
     v, a = log_posterior(state.params, b)
     vals.append(v)
     aux.append(aux)

Some discussion on gradient accumulation here:

https://discuss.pytorch.org/t/why-do-we-need-to-set-the-gradients-manually-to-zero-in-pytorch/4903/19?u=alband

https://wandb.ai/wandb_fc/tips/reports/How-To-Implement-Gradient-Accumulation-in-PyTorch--VmlldzoyMjMwOTk5

@SamDuffield SamDuffield added the enhancement New feature or request (beyond just a new method) label Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request (beyond just a new method)
Projects
None yet
Development

No branches or pull requests

1 participant