-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add doc for TBPTT #20422
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
Merged
lantiga
merged 23 commits into
Lightning-AI:master
from
chualanagit:chualan/add-example-for-TBPTT
Dec 10, 2024
Merged
Add doc for TBPTT #20422
Changes from 12 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
1fc2cfa
Add doc for TBPTT
efae604
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 24bf319
Merge branch 'master' into chualan/add-example-for-TBPTT
chualanagit b296ec0
remove url to prevent linting error
7f9673d
attempt to fix linter
aca0046
Merge branch 'master' into chualan/add-example-for-TBPTT
chualanagit 4e37ab9
Merge branch 'master' into chualan/add-example-for-TBPTT
lantiga dce88a6
Merge branch 'master' into chualan/add-example-for-TBPTT
chualanagit 54c89a8
add tbptt.rst file
5c367c5
adjust doc:
610809c
nit
48dbdd2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6f7f206
Merge branch 'master' into chualan/add-example-for-TBPTT
chualanagit 5262534
make example easily copy and runnable
ce9790e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] bbf7c12
Merge branch 'master' into chualan/add-example-for-TBPTT
chualanagit ef2a826
address comments
5709003
Merge branch 'master' into chualan/add-example-for-TBPTT
chualanagit 3feec73
Merge branch 'master' into chualan/add-example-for-TBPTT
chualanagit 8aee16a
fix doc test warning
0d2a384
Update docs/source-pytorch/common/tbptt.rst
lantiga bbf2a3f
Merge branch 'master' into chualan/add-example-for-TBPTT
lantiga 66072cc
Merge branch 'master' into chualan/add-example-for-TBPTT
lantiga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
############## | ||
Truncated Backpropagation Through Time (TBPTT) | ||
############## | ||
|
||
Truncated Backpropagation Through Time (TBPTT) performs perform backpropogation every k steps of | ||
a much longer sequence. This is made possible by passing training batches | ||
split along the time-dimensions into splits of size k to the | ||
``training_step``. In order to keep the same forward propagation behavior, all | ||
hidden states should be kept in-between each time-dimension split. | ||
|
||
|
||
.. code-block:: python | ||
chualanagit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class LitModel(LightningModule): | ||
|
||
def __init__(self): | ||
super().__init__() | ||
|
||
# 1. Switch to manual optimization | ||
self.automatic_optimization = False | ||
|
||
self.truncated_bptt_steps = 10 | ||
self.my_rnn = ... | ||
|
||
# 2. Remove the `hiddens` argument | ||
def training_step(self, batch, batch_idx): | ||
|
||
# 3. Split the batch in chunks along the time dimension | ||
split_batches = split_batch(batch, self.truncated_bptt_steps) | ||
|
||
hiddens = ... # 3. Choose the initial hidden state | ||
for split_batch in range(split_batches): | ||
# 4. Perform the optimization in a loop | ||
loss, hiddens = self.my_rnn(split_batch, hiddens) | ||
self.backward(loss) | ||
optimizer.step() | ||
optimizer.zero_grad() | ||
|
||
# 5. "Truncate" | ||
hiddens = hiddens.detach() | ||
|
||
# 6. Remove the return of `hiddens` | ||
# Returning loss in manual optimization is not needed | ||
return None |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.