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

loop customization docs #9557

Closed
wants to merge 25 commits into from
Closed

loop customization docs #9557

wants to merge 25 commits into from

Conversation

awaelchli
Copy link
Contributor

@awaelchli awaelchli commented Sep 16, 2021

What does this PR do?

Adds a user guide for loops in Lightning. Contains:

  • Explanation of default loop structure in Lightning
  • How loop customization works
  • An example of a "yield loop" that transforms the training step into a generator

Also adds the loop base class to the API reference section.

You can check the docs build by clicking here:
image
Then go to Artifacts and navigate to loops.html.

Or if you have access to circle-ci: https://129946-178626720-gh.circle-artifacts.com/0/html/advanced/loops.html

Part of #7938

Before submitting

  • Was this discussed/approved via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)

PR review

Anyone in the community is free to review the PR once the tests have passed.
Before you start reviewing make sure you have read Review guidelines. In short, see the following bullet-list:

  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

Did you have fun?

I made sure I had fun coding 🙃

@awaelchli awaelchli added the docs Documentation related label Sep 16, 2021
@awaelchli awaelchli added this to the v1.5 milestone Sep 16, 2021
Comment on lines +26 to +27
+---------------------------------------------------------------+-----------------------------------------------------------------------+-------------------------------------------------------------------------------+
| Entry point | Trainer attribute | Loop class |
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is supposed to be a table, sorry this may look messed up in your browser when viewing the diff

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat ! Good idea to show entry points !

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks better on the source code. let's do it

@awaelchli awaelchli marked this pull request as ready for review September 16, 2021 13:44
@github-actions
Copy link
Contributor

Build Error! No Linked Issue found. Please link an issue or mention it in the body using #<issue_id>

@awaelchli
Copy link
Contributor Author

hey @tchaton can we disable the bot for documentation updates?

@codecov
Copy link

codecov bot commented Sep 16, 2021

Codecov Report

Merging #9557 (4add03c) into master (23450e2) will decrease coverage by 0%.
The diff coverage is n/a.

@@          Coverage Diff           @@
##           master   #9557   +/-   ##
======================================
- Coverage      93%     93%   -0%     
======================================
  Files         180     180           
  Lines       15090   15116   +26     
======================================
+ Hits        14019   14043   +24     
- Misses       1071    1073    +2     

docs/source/advanced/loops.rst Outdated Show resolved Hide resolved
docs/source/advanced/loops.rst Outdated Show resolved Hide resolved
docs/source/advanced/loops.rst Outdated Show resolved Hide resolved
docs/source/advanced/loops.rst Outdated Show resolved Hide resolved
docs/source/advanced/loops.rst Outdated Show resolved Hide resolved
docs/source/advanced/loops.rst Outdated Show resolved Hide resolved
docs/source/advanced/loops.rst Outdated Show resolved Hide resolved
docs/source/advanced/loops.rst Outdated Show resolved Hide resolved
docs/source/advanced/loops.rst Outdated Show resolved Hide resolved
docs/source/advanced/loops.rst Outdated Show resolved Hide resolved
@justusschock
Copy link
Member

Can we also add this to the Optional Extension section. I searched for it there and common usecases is IMO not the best fit.

Also in general: This is part of the public facing lightning API. I find it strange, that the API section only covers trainer and LightningModule.

@tchaton
Copy link
Contributor

tchaton commented Sep 17, 2021

hey @tchaton can we disable the bot for documentation updates?

Yes, I need to implement this. I will try this weekend.

Best,
T.C

Co-authored-by: Carlos Mocholí <carlossmocholi@gmail.com>
Copy link
Contributor

@tchaton tchaton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work !

docs/source/advanced/loops.rst Outdated Show resolved Hide resolved
- how you can create a custom loop for a new training step flavor,
- and how you can connect the custom loop and run it.

Most importantly, we will also provide guidelines when to use loop customization and when NOT to use it.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Most importantly, we will also provide guidelines when to use loop customization and when NOT to use it.
Most importantly, we will provide guidelines when to use loop customization and when NOT to use it.

Comment on lines +26 to +27
+---------------------------------------------------------------+-----------------------------------------------------------------------+-------------------------------------------------------------------------------+
| Entry point | Trainer attribute | Loop class |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat ! Good idea to show entry points !

| :meth:`~pytorch_lightning.trainer.trainer.Trainer.predict` | :attr:`~pytorch_lightning.trainer.trainer.Trainer.predict_loop` | :class:`~pytorch_lightning.loops.dataloader.prediction_loop.PredictionLoop` |
+---------------------------------------------------------------+-----------------------------------------------------------------------+-------------------------------------------------------------------------------+

When the user calls :code:`Trainer.method`, it redirects to the corresponding :code:`Trainer.loop.run()` which implements the main logic of that particular Lightning loop.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
When the user calls :code:`Trainer.method`, it redirects to the corresponding :code:`Trainer.loop.run()` which implements the main logic of that particular Lightning loop.
When the user calls :code:`Trainer.method`, it redirects to the corresponding :code:`Trainer.loop.run()` which implements the associated logic of this particular Lightning Loop.

docs/source/advanced/loops.rst Outdated Show resolved Hide resolved
docs/source/advanced/loops.rst Outdated Show resolved Hide resolved
Set the environment variable `PL_FAULT_TOLERANT_TRAINING = 1` to enable saving the progress of loops.
Read more about :doc:`fault-tolerant training training <../advanced/fault_tolerant_training>`.

An interesting property of the abstract loop interface is that it can maintain a state.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
An interesting property of the abstract loop interface is that it can maintain a state.
The PyTorch Lightning Loop state management design is highly inspired from the PyTorch nn.Module and relies on the same underlying logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually all I want to express here is that the benefit of having loops as objects is that they can have state.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, but I built the state management exactly as pytorch ones. Advanced users are already familiar with it, and it means they will understand faster how it works. Just a personal suggestion.

Read more about :doc:`fault-tolerant training training <../advanced/fault_tolerant_training>`.

An interesting property of the abstract loop interface is that it can maintain a state.
It can save its state to a checkpoint through corresponding hooks and if implemented accordingly, resume it's state of exectuion at the appropriate place.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
It can save its state to a checkpoint through corresponding hooks and if implemented accordingly, resume it's state of exectuion at the appropriate place.
As a result, the loops can save their state and their children within the model checkpoint through corresponding hooks and if implemented accordingly, resume their state of execution at the appropriate place.


An interesting property of the abstract loop interface is that it can maintain a state.
It can save its state to a checkpoint through corresponding hooks and if implemented accordingly, resume it's state of exectuion at the appropriate place.
This design is particularly interesting for fault-tolerant training which is an experimental feature released in Lightning v1.5.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This design is particularly interesting for fault-tolerant training which is an experimental feature released in Lightning v1.5.
As a matter of facts, all the Lightning loops implements an advanced state management which is the underlying mechanism of a long waited feature: Fault Tolerant Training which is an experimental feature released in Lightning v1.5.

docs/source/advanced/loops.rst Outdated Show resolved Hide resolved
docs/source/advanced/loops.rst Outdated Show resolved Hide resolved
docs/source/advanced/loops.rst Outdated Show resolved Hide resolved

**How do I make sure a given LightningModule is compatible with my custom loop?**

To restrict the compatibility of a LightningModule to a particular loop type, we recommend to define a specific class mixin for this purpose.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dope ! Really neat :)

@awaelchli
Copy link
Contributor Author

@justusschock sounds good, can move it to "Optional Extensions" section under plugins if others agree @carmocca @tchaton .

Also in general: This is part of the public facing lightning API. I find it strange, that the API section only covers trainer and LightningModule.

We have two sections, one is just called "Lightning API" and the other "API Reference" which is supposed to contain all public facing api. The difference is that the former is more a user guide and the latter is just the api reference nothing more. we could probably do a better naming there.

awaelchli and others added 2 commits September 17, 2021 11:52
Co-authored-by: thomas chaton <thomas@grid.ai>
from pytorch_lightning.loops.utilities import _build_training_step_kwargs


class YieldLoop(OptimizerLoop):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@awaelchli, would mind making sure the code isn't too long. Maybe 80 lines. It doesn't render very well in the doc.

Screenshot 2021-09-18 at 12 18 17

@awaelchli
Copy link
Contributor Author

will rewrite the docs and send new pr.

@awaelchli awaelchli closed this Sep 18, 2021
@awaelchli awaelchli mentioned this pull request Sep 20, 2021
12 tasks
@carmocca carmocca deleted the docs/loop-customization branch September 21, 2021 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants