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

Add configure_gradient_clipping hook in LightningModule #9584

Merged
merged 26 commits into from
Oct 13, 2021

Conversation

rohitgr7
Copy link
Contributor

@rohitgr7 rohitgr7 commented Sep 17, 2021

What does this PR do?

Fixes #6346

Does your PR introduce any breaking changes? If yes, please list them.

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 list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)

PR review

Anyone in the community is welcome to review the PR.
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?

Make sure you had fun coding 🙃

@rohitgr7
Copy link
Contributor Author

@carmocca should we deprecate gradient_clip_val and gradient_clip_hook arguments from Trainer? Also initial thoughts on this please :)

@ananthsub
Copy link
Contributor

@rohitgr7 imo we should discuss this more on the issue. I think there are some design aspects that have to be worked out first

@rohitgr7 rohitgr7 changed the title Add clip_gradients hook in `LightningModule Add clip_gradients hook in LightningModule Sep 22, 2021
Copy link
Contributor

@awaelchli awaelchli left a comment

Choose a reason for hiding this comment

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

I don't understand this PR. It seems to be incomplete.
Today I am doing this:

Trainer(gradient_clip_val=15.0)

or

python train.py --trainer.gradient_clip_val 15.0

This PR aims at deprecating this option. How do I achieve the above now? There seems to be a hook attempting to replace this functionality, but this is missing the point why the Trainer argument exists in the first place. 99.9% of the time the user does not need to implement gradient clipping because Lightning can do it. But by removing this now, you force the user to 100% of the time have to implement a hook? IMO you are removing a perfectly fine feature and I vote against going in this direction. If configurability is desired, it should be entirely optional.

pytorch_lightning/core/lightning.py Outdated Show resolved Hide resolved
@rohitgr7 rohitgr7 marked this pull request as draft September 23, 2021 15:20
@rohitgr7
Copy link
Contributor Author

so for now I have implemented option 2 from here: #6346 (comment)

feel free to put your suggestions/recommendations here or on the issue :)

@rohitgr7 rohitgr7 marked this pull request as ready for review September 28, 2021 12:38
@codecov
Copy link

codecov bot commented Sep 28, 2021

Codecov Report

Merging #9584 (84003ec) into master (b530b7a) will increase coverage by 0%.
The diff coverage is 93%.

@@          Coverage Diff           @@
##           master   #9584   +/-   ##
======================================
  Coverage      93%     93%           
======================================
  Files         178     178           
  Lines       15652   15717   +65     
======================================
+ Hits        14508   14572   +64     
- Misses       1144    1145    +1     

@rohitgr7 rohitgr7 changed the title Add clip_gradients hook in LightningModule Add configure_gradient_clipping hook in LightningModule Sep 28, 2021
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.

LGTM ! Nice work :)

pytorch_lightning/core/lightning.py Outdated Show resolved Hide resolved
pytorch_lightning/core/lightning.py Outdated Show resolved Hide resolved
pytorch_lightning/core/lightning.py Outdated Show resolved Hide resolved
@mergify mergify bot added the ready PRs ready to be merged label Sep 28, 2021
Copy link
Contributor

@awaelchli awaelchli left a comment

Choose a reason for hiding this comment

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

while this addition is probably needed and desired from a practical point of view, it also shows where Lightning's automatic optimization breaks down imo, because the desire of configuration here means ignoring settings that were provided through the trainer, which then in turn also challenges reproducibility.

pytorch_lightning/core/lightning.py Outdated Show resolved Hide resolved
pytorch_lightning/core/lightning.py Outdated Show resolved Hide resolved
@rohitgr7
Copy link
Contributor Author

while this addition is probably needed and desired from a practical point of view, it also shows where Lightning's automatic optimization breaks down imo, because the desire of configuration here means ignoring settings that were provided through the trainer, which then in turn also challenges reproducibility.

settings provided in the trainer will still work and one doesn't need to change their code at all since those trainer args are passed here by default. This is completely optional for those who want to configure it as per their requirement. Doesn't break automatic optimization.

@awaelchli
Copy link
Contributor

Yes it does! Let me make it explicit with an example:

def configure_gradient_clipping(self,optimizer, optimizer_idx, gradient_clip_val, gradient_clip_algorithm)
    if optimizer_idx == 0:
        self.clip_gradients(optimizer, optimizer_idx, 10, "norm")
    else:
        self.clip_gradients(optimizer, optimizer_idx, 20, "norm")

Now:

trainer = Trainer(gradient_clip_val=30)  # has no effect!!!

Same issue when configuration is done externally:

python train.py --gradient_clip_val=30 

This is where Lightning reaches the limit and essentially breaks. This is an issue we will face more in the future.

@rohitgr7
Copy link
Contributor Author

rohitgr7 commented Sep 29, 2021

okay.. so for this what we can do is make these two trainer arguments None by default. Now inside clip_gradients we can check:

  1. if they are not None and different value is passed, then we can raise a warning/misconfigexception.
  2. if they are not None and same value is passed, then it's fine.
  3. if they are None and different value is passed, use the new value
  4. if they are None, and no value is passed, then use the current default values that we have in Trainer (0.0 and 'norm')

wdyt @awaelchli ?

@rohitgr7 rohitgr7 marked this pull request as draft September 29, 2021 15:04
@rohitgr7 rohitgr7 marked this pull request as ready for review September 30, 2021 11:32
@mergify mergify bot added the has conflicts label Oct 6, 2021
@mergify mergify bot removed the has conflicts label Oct 12, 2021
@rohitgr7 rohitgr7 merged commit 23e8b59 into master Oct 13, 2021
@rohitgr7 rohitgr7 deleted the feat/grad_clip_hook branch October 13, 2021 14:45
rohitgr7 added a commit to Tshimanga/pytorch-lightning that referenced this pull request Oct 18, 2021
…g-AI#9584)

* init hook

* docs

* dep train args

* update tests

* doc

* doc

* .gitignore

* not dep

* add trainer args

* add & update tests

* fix tests

* pre-commit

* docs

* add docs

* add exception

* code review

* deepspeed

* update tests

* not

* try fix

* Apply suggestions from code review

* update deepspeed

* disable some tests

* disable some tests

* enable all tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC] Gradient clipping hooks in the LightningModule
7 participants