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

Stepwise LR scheduler #20211

Open
wants to merge 34 commits into
base: master
Choose a base branch
from

Conversation

01AbhiSingh
Copy link
Contributor

@01AbhiSingh 01AbhiSingh commented Aug 18, 2024

What does this PR do?

Fixes #<17544>

Hii @awaelchli. Can you please verify the changes I made. If they are correct then i will take up and correct any failing tests also.

Before submitting
  • Was this discussed/agreed 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 minor internal changes/refactors)

PR review

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

hii Reviewer checklist - [ ] 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

📚 Documentation preview 📚: https://pytorch-lightning--20211.org.readthedocs.build/en/20211/

@github-actions github-actions bot added the pl Generic label for PyTorch Lightning package label Aug 18, 2024
Copy link

codecov bot commented Aug 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81%. Comparing base (5be58f6) to head (3c48c9e).
Report is 14 commits behind head on master.

❗ There is a different number of reports uploaded between BASE (5be58f6) and HEAD (3c48c9e). Click for more details.

HEAD has 553 uploads less than BASE
Flag BASE (5be58f6) HEAD (3c48c9e)
cpu 147 21
lightning 106 16
pytest 87 2
python3.9 43 6
python3.10 42 6
lightning_fabric 25 0
gpu 4 2
python3.11 42 6
python3.12 20 3
pytorch2.1 38 12
pytest-full 64 21
pytorch2.3 9 3
pytorch_lightning 20 7
pytorch2.2 9 3
pytorch2.4 8 3
Additional details and impacted files
@@            Coverage Diff            @@
##           master   #20211     +/-   ##
=========================================
- Coverage      89%      81%     -8%     
=========================================
  Files         267      264      -3     
  Lines       23084    23029     -55     
=========================================
- Hits        20585    18618   -1967     
- Misses       2499     4411   +1912     

@01AbhiSingh
Copy link
Contributor Author

Hii @Borda. Do I need to make any kind of changes in the PR ?

@lantiga
Copy link
Collaborator

lantiga commented Oct 7, 2024

This looks good, thank you for the contribution @01AbhiSingh

Ideally we could add a test to verify the behavior described in #17544. The current test suite can't detect the current change and this is usually a sign of insufficient coverage. Would you be willing to contribute such test?

@01AbhiSingh
Copy link
Contributor Author

Yes, sure let me look into it.

@01AbhiSingh
Copy link
Contributor Author

Hi @lantiga , Do you want a new test written from scratch or need me to make the necessary changes in a preexisting file? All the tests have been passed. If the changes need to be made in a preexisting file, it would be very helpful if you could point out the test in which I need to make the changes, as all the tests have been passed, and due to that, I can't find the test.

@lantiga
Copy link
Collaborator

lantiga commented Nov 12, 2024

hey @01AbhiSingh sorry for the wait

You can take inspiration from:

def test_lr_scheduler_epoch_step_frequency(mocked_sched, check_val_every_n_epoch, tmp_path):

and add a new test where scheduling goes across epoch boundaries. Maybe @falckt can help too?

@lantiga
Copy link
Collaborator

lantiga commented Dec 6, 2024

@01AbhiSingh the test looks good! feel free to add it to test_optimizers.py and we should be ok

@01AbhiSingh
Copy link
Contributor Author

Done. sorry I was bogged down by few things. But, a lot of new PRs for all those issues coming your way now.

@lantiga
Copy link
Collaborator

lantiga commented Dec 8, 2024

there's an issue with mocked_sched that is undefined in the test, other than that things look good

@01AbhiSingh
Copy link
Contributor Author

there's an issue with mocked_sched that is undefined in the test, other than that things look good

do you mean I need to add fixture to the test function's parameters.

@pytest.fixture
def mocked_sched(mocker):
    return mocker.patch('torch.optim.lr_scheduler.StepLR.step')

@lantiga
Copy link
Collaborator

lantiga commented Dec 11, 2024

That's right, you can also decorate the test with

from unittest.mock import patch

...

@patch("torch.optim.lr_scheduler.StepLR.step")
def test_lr_scheduler_step_across_epoch_boundaries(mocked_sched, tmp_path):
    ...

this should work

@01AbhiSingh
Copy link
Contributor Author

Done please check

@lantiga
Copy link
Collaborator

lantiga commented Dec 11, 2024

Hey @01AbhiSingh can you import LightningModule here?

https://github.com/Lightning-AI/pytorch-lightning/pull/20211/files#diff-3c3f104dbdd06271c9e6e6d4fdf61398458148412401dd55a9bac1e9b5f913a8R19

Change:

from lightning.pytorch import Trainer

to

from lightning.pytorch import Trainer, LightningModule

this should fix the failing test

@01AbhiSingh
Copy link
Contributor Author

Yeah, my bad. Forgot to add it even after seeing it. Done, please check.

@01AbhiSingh
Copy link
Contributor Author

https://github.com/Lightning-AI/pytorch-lightning/actions/runs/12291356552/job/34299991507?pr=20211#:~:text=FAILED%20utilities/test_data.py%3A%3Atest_update_dataloader_typerror_custom_exception%20%2D%20AssertionError%3A%20Regex%20pattern%20did%20not%20match.

This is the test that is currently failing.

def train_dataloader(self):
           # Create a simple dataset for testing
           x = torch.randn(21, 32)  # 7 batches of size 3
           y = torch.randn(21, 2)
           return DataLoader(TensorDataset(x, y), batch_size=3)

should I add this and try to run the test again ?

@lantiga
Copy link
Collaborator

lantiga commented Dec 12, 2024

Go for it : )

You can also run this kind of test locally with pytest tests/tests_pytorch/<test_file>.py::<name_of_test> to make things quicker on your end. This test in particular can be ran on any machine (and you can use Lightning Studios for free if you want to run on GPUs ofc)

@01AbhiSingh
Copy link
Contributor Author

01AbhiSingh commented Dec 12, 2024

Go for it : )

You can also run this kind of test locally with pytest tests/tests_pytorch/<test_file>.py::<name_of_test> to make things quicker on your end. This test in particular can be ran on any machine (and you can use Lightning Studios for free if you want to run on GPUs ofc)

I actually tried to run the test locally with the method you suggested but this error keeps showing up ERROR: file or directory not found: tests/tests_pytorch/test_optimizers.py anyway I am trying to solve this problem on my local env.

Edit: I've solved this problem, will now update the PR only when it's running perfectly on my local environment. Thanks :)

Another Edit 😝 : updated the PR please check

@01AbhiSingh
Copy link
Contributor Author

Test passing on my local environment but not in the PR in the repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pl Generic label for PyTorch Lightning package waiting on author Waiting on user action, correction, or update
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants