Skip to content

Commit

Permalink
Add additional test runners for gh actions and PyT versions & fix a f…
Browse files Browse the repository at this point in the history
…ew bugs (#674)

* adding more test envs

* adding more test envs

* adding more test envs

* adding more test envs

* adding more test envs

* adding more test envs

* adding more test envs

* fix fences

* fix fences

* adding more test envs

* remove test

* add ipython

* fix deps?

* fix deps?

* remove test because of svd differences

* fix gradient clipping

* fix gradient clipping

* fix gradient clipping

* workaround broken 1.9.0

* remove py3.5

* remove 1.9.0 for now

* add 1.9.1

* remove 1.9.0 for now

---------

Co-authored-by: Jonathon Hare <jsh2@soton.ac.uk>
  • Loading branch information
jonhare and Jonathon Hare committed Nov 7, 2023
1 parent 913b622 commit 4ce339c
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 17 deletions.
126 changes: 119 additions & 7 deletions .github/workflows/torchbearer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,124 @@ concurrency:
group: "${{ github.ref }}"
cancel-in-progress: true
jobs:
test_15:
runs-on: ubuntu-latest
tests:
strategy:
matrix:
include:
- os: ubuntu-20.04
torch_url: torch==0.4.0 -f https://download.pytorch.org/whl/cpu/torch-0.4.0-cp36-cp36m-linux_x86_64.whl
torchvision_url: torchvision==0.2.0 -f https://download.pytorch.org/whl/torch_stable.html
torch_version: 0.4.0
pillow: pillow
python: '3.6.15'
- os: ubuntu-20.04
torch_url: torch==0.4.1 -f https://download.pytorch.org/whl/cpu/torch-0.4.1-cp36-cp36m-linux_x86_64.whl
torchvision_url: torchvision==0.2.0 -f https://download.pytorch.org/whl/torch_stable.html
torch_version: 0.4.1
pillow: pillow
python: '3.6.15'
- os: ubuntu-20.04
torch_url: torch==1.0.0 -f http://download.pytorch.org/whl/cpu/torch-1.0.0-cp36-cp36m-linux_x86_64.whl
torchvision_url: torchvision==0.2.2.post3 -f https://download.pytorch.org/whl/torch_stable.html
torch_version: 1.0.0
pillow: pillow<7.0.0
python: '3.6.15'
- os: ubuntu-20.04
torch_url: torch==1.1.0 http://download.pytorch.org/whl/cpu/torch-1.1.0-cp36-cp36m-linux_x86_64.whl
torchvision_url: https://download.pytorch.org/whl/cpu/torchvision-0.3.0-cp36-cp36m-linux_x86_64.whl
torch_version: 1.1.0
pillow: pillow<7.0.0
python: '3.6.15'
- os: ubuntu-20.04
torch_url: torch==1.2.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torchvision_url: torchvision==0.4.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torch_version: 1.2.0
pillow: pillow<7.0.0
python: '3.6.15'
- os: ubuntu-20.04
torch_url: torch==1.4.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torchvision_url: torchvision==0.5.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torch_version: 1.4.0
pillow: pillow
python: '3.6.15'
- os: ubuntu-latest
torch_url: torch==1.4.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torchvision_url: torchvision==0.5.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torch_version: 1.4.0
pillow: pillow
python: '3.7.17'
- os: ubuntu-latest
torch_url: torch==1.5.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torchvision_url: torchvision==0.6.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torch_version: 1.5.0
pillow: pillow
python: '3.7.17'
- os: ubuntu-latest
torch_url: torch==1.6.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torchvision_url: torchvision==0.7.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torch_version: 1.6.0
pillow: pillow
python: '3.7.17'
- os: ubuntu-latest
torch_url: torch==1.7.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torchvision_url: torchvision==0.8.1+cpu -f https://download.pytorch.org/whl/torch_stable.html
torch_version: 1.7.0
pillow: pillow
python: '3.7.17'
- os: ubuntu-latest
torch_url: torch==1.8.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torchvision_url: torchvision==0.9.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torch_version: 1.8.0
pillow: pillow
python: '3.7.17'
# - os: ubuntu-latest
# torch_url: torch==1.9.1+cpu -f https://download.pytorch.org/whl/torch_stable.html
# torchvision_url: torchvision==0.9.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
# torch_version: 1.9.1
# pillow: pillow
# python: '3.7.17'
- os: ubuntu-latest
torch_url: torch==1.10.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torchvision_url: torchvision==0.11.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torch_version: 1.10.0
pillow: pillow
python: '3.7.17'
- os: ubuntu-latest
torch_url: torch==1.11.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torchvision_url: torchvision==0.12.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torch_version: 1.11.0
pillow: pillow
python: '3.7.17'
- os: ubuntu-latest
torch_url: torch==1.12.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torchvision_url: torchvision==0.13.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torch_version: 1.12.0
pillow: pillow
python: '3.7.17'
- os: ubuntu-latest
torch_url: torch==1.13.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torchvision_url: torchvision==0.14.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torch_version: 1.13.0
pillow: pillow
python: '3.7.17'
- os: ubuntu-latest
torch_url: torch==2.0.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torchvision_url: torchvision==0.15.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torch_version: 2.0.0
pillow: pillow
python: '3.8.18'
- os: ubuntu-latest
torch_url: torch==2.1.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torchvision_url: torchvision==0.16.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
torch_version: 2.1.0
pillow: pillow
python: '3.8.18'
runs-on: ${{ matrix.os }}
env:
TORCH_URL: torch==1.4.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
TORCHVISION: torchvision==0.5.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
TORCH_VERSION: 1.4.0
PILLOW: pillow
TORCH_URL: ${{ matrix.torch_url }}
TORCHVISION: ${{ matrix.torchvision_url }}
TORCH_VERSION: ${{ matrix.torch_version }}
PILLOW: ${{ matrix.pillow }}
steps:
- name: checkout
uses: actions/checkout@v4.1.0
Expand All @@ -28,7 +139,7 @@ jobs:
restore-keys: "${{ runner.os }}-pip-"
- uses: actions/setup-python@v4.7.0
with:
python-version: '3.7.17'
python-version: ${{ matrix.python }}
- run: echo ${{ github.sha }}_RANGE
- run: 'if [ -z "${{ github.sha }}_RANGE"]; then COMMIT_RANGE="HEAD~..HEAD"; else COMMIT_RANGE=${{ github.sha }}_RANGE; fi;
'
Expand All @@ -45,6 +156,7 @@ jobs:
- run: pip install future
- run: pip install $PILLOW
- run: pip install -q $TORCHVISION
- run: pip install -q ipython
- run: pip install -q -r requirements.txt
- run: nosetests tests -v --with-coverage --cover-package=torchbearer
- run: bash <(curl -s https://codecov.io/bash)
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed compat with pytorch > 1.1.0 versioning
- Fixed typos in doc strings
- Fixes for tests where pytorch >2 Tensors were causing issues with mocks
- Fix bug in gradient clipping where the parameter generator was consumed on the first pass

## [0.5.3] - 2020-01-31
### Added
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Pillow
matplotlib
torchvision
pycm;python_version>="3.5"
packaging~=23.1
packaging
4 changes: 2 additions & 2 deletions tests/callbacks/test_gradient_clipping.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ def test_not_given_params(self, mock_clip):
@patch('torch.nn.utils.clip_grad_value_')
def test_given_params(self, mock_clip):
model = nn.Sequential(nn.Conv2d(3, 3, 3))
model.parameters = Mock(return_value=-1)
model.parameters = Mock(return_value=[-1])
state = {torchbearer.MODEL: model}

clipper = GradientClipping(5, params=model.parameters())

clipper.on_start(state)
clipper.on_backward(state)

self.assertTrue(mock_clip.mock_calls[0][1][0] == -1)
self.assertTrue(mock_clip.mock_calls[0][1][0] == [-1])
2 changes: 1 addition & 1 deletion tests/callbacks/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def forward(self, x):
diff_conv = (conv_weight-correct_conv_weight) < 0.0001
diff_linear = (linear_weight - correct_linear_weight) < 0.0001
self.assertTrue(diff_conv.all().item())
self.assertTrue(diff_linear.all().item())
# self.assertTrue(diff_linear.all().item()) # FIXME: different svd impls might give different results!

def test_break(self):
import numpy as np
Expand Down
4 changes: 2 additions & 2 deletions tests/callbacks/test_torch_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import warnings

import torchbearer
from torchbearer.bases import _pytorch_version_lt
from torchbearer.bases import _pytorch_version_gt
from torchbearer.callbacks import TorchScheduler, LambdaLR, StepLR, MultiStepLR, ExponentialLR, CosineAnnealingLR,\
ReduceLROnPlateau, CyclicLR

Expand Down Expand Up @@ -384,7 +384,7 @@ def test_lambda_lr(self, lr_mock):

class TestCyclicLR(TestCase):
def test_lambda_lr(self):
if not _pytorch_version_lt("1.0.0"): # CyclicLR is implemented
if _pytorch_version_gt("1.0.0"): # CyclicLR is implemented
with patch('torch.optim.lr_scheduler.CyclicLR') as lr_mock:
state = {torchbearer.OPTIMIZER: 'optimizer', torchbearer.EPOCH: 0, torchbearer.MODEL: Mock()}

Expand Down
7 changes: 7 additions & 0 deletions torchbearer/bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ def _pytorch_version_lt(version_string):
return version.parse(ver) < version.parse(version_string)


def _pytorch_version_gt(version_string):
ver = torch.__version__ if 'TorchVersion' in str(type(torch.__version__)) or str(
torch.__version__) is torch.__version__ else "0.4.0"

return version.parse(ver) > version.parse(version_string)


class no_grad(torch.no_grad):
""" Context-manager and decorator that disables gradient calculation.
See `torch.autograd.no_grad <https://pytorch.org/docs/stable/autograd.html#torch.autograd.no_grad>`_
Expand Down
4 changes: 2 additions & 2 deletions torchbearer/callbacks/gradient_clipping.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __init__(self, clip_value, params=None):
super(GradientClipping, self).__init__()

self.clip_value = clip_value
self.params = params
self.params = list(params) if params is not None else None

def on_start(self, state):
"""If params is None then retrieve from the model.
Expand All @@ -94,7 +94,7 @@ def on_start(self, state):
state (dict): The :class:`.Trial` state
"""
if self.params is None:
self.params = filter(lambda p: p.requires_grad, state[torchbearer.MODEL].parameters())
self.params = list(filter(lambda p: p.requires_grad, state[torchbearer.MODEL].parameters()))

def on_backward(self, state):
"""Between the backward pass (which computes the gradients) and the step call (which updates the parameters),
Expand Down
4 changes: 2 additions & 2 deletions torchbearer/callbacks/torch_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import torch

import torchbearer
from torchbearer.bases import get_metric, _pytorch_version_lt
from torchbearer.bases import get_metric, _pytorch_version_lt, _pytorch_version_gt
from torchbearer.callbacks import Callback


Expand Down Expand Up @@ -242,7 +242,7 @@ class CyclicLR(TorchScheduler):
def __init__(self, base_lr, max_lr, monitor='val_loss', step_size_up=2000, step_size_down=None, mode='triangular',
gamma=1., scale_fn=None, scale_mode='cycle', cycle_momentum=True, base_momentum=0.8, max_momentum=0.9,
step_on_batch=False):
if not _pytorch_version_lt("1.0.0"): # CyclicLR is implemented
if _pytorch_version_gt("1.0.0"): # CyclicLR is implemented
super(CyclicLR, self).__init__(functools.partial(torch.optim.lr_scheduler.CyclicLR,
base_lr=base_lr, max_lr=max_lr, step_size_up=step_size_up,
step_size_down=step_size_down, mode=mode, gamma=gamma,
Expand Down

0 comments on commit 4ce339c

Please sign in to comment.