Skip to content

Commit

Permalink
del logits=(bs, seq_len, vocab_size) to save 3.9G memory (#391)
Browse files Browse the repository at this point in the history
logits=(bs, seq_len, vocab_size). call `del logits` to free it before
backward

<img width="1607" alt="Screenshot 2024-06-12 at 11 10 36 AM"
src="https://github.com/pytorch/torchtitan/assets/134637289/82db2792-59a3-40c4-9591-842be3dd9284">
  • Loading branch information
weifengpy authored Jun 12, 2024
1 parent 91937ef commit e29b6b4
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ def loss_fn(pred, labels):
with loss_parallel_ctx():
pred = model(input_ids)
loss = loss_fn(pred, labels)
# pred.shape=(bs, seq_len, vocab_size)
# need to free to before bwd to avoid peaking memory
del pred
loss.backward()

# clip gradients
Expand Down

0 comments on commit e29b6b4

Please sign in to comment.