add testing PT 1.12 (#13386)

* add testing PT 1.12
* Fix quantization tests
* Fix another set of tests
* Fix check since https://github.com/pytorch/pytorch/pull/80139 is only going to be available for 1.13
* Skip this test for now for 1.12

Co-authored-by: SeanNaren <sean@grid.ai>
This commit is contained in:
Jirka Borovec 2022-07-15 19:41:23 +02:00 committed by GitHub
parent cdb493ec42
commit aa62fe36df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 70 additions and 42 deletions

View File

@ -15,6 +15,7 @@ from urllib import request
from urllib.request import Request, urlopen
import fire
import pkg_resources
REQUIREMENT_FILES = {
"pytorch": (
@ -78,17 +79,12 @@ class AssistantCLI:
@staticmethod
def _prune_packages(req_file: str, packages: Sequence[str]) -> None:
"""Remove some packages from given requirement files."""
with open(req_file) as fp:
lines = fp.readlines()
if isinstance(packages, str):
packages = [packages]
for pkg in packages:
lines = [ln for ln in lines if not ln.startswith(pkg)]
pprint(lines)
with open(req_file, "w") as fp:
fp.writelines(lines)
path = Path(req_file)
assert path.exists()
text = path.read_text()
final = [str(req) for req in pkg_resources.parse_requirements(text) if req.name not in packages]
pprint(final)
path.write_text("\n".join(final))
@staticmethod
def _replace_min(fname: str) -> None:

View File

@ -22,11 +22,12 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.8"] # previous to last Python version as that one is already used in test-full
pytorch-version: ["1.9", "1.10"]
# nightly: add when there's a release candidate
include:
- {python-version: "3.8", pytorch-version: "1.9"}
- {python-version: "3.8", pytorch-version: "1.10"}
- {python-version: "3.9", pytorch-version: "1.11"}
- {python-version: "3.9", pytorch-version: "1.12"}
timeout-minutes: 30
@ -66,6 +67,10 @@ jobs:
conda list
pip install -e .[test]
- name: Freeze PIL (hotfix)
# import of PILLOW_VERSION which they recently removed in v9.0 in favor of __version__
run: pip install "Pillow<9.0" # It messes with torchvision
- name: DocTests
if: ${{ (steps.skip.outputs.continue == '1') }}
working-directory: ./src
@ -79,10 +84,11 @@ jobs:
HOROVOD_WITHOUT_TENSORFLOW: 1
run: |
set -e
pip list
# adjust versions according installed Torch version
python ./requirements/pytorch/adjust-versions.py requirements/pytorch/extra.txt
python ./requirements/pytorch/adjust-versions.py requirements/pytorch/examples.txt
pip install -r requirements/pytorch/devel.txt --find-links https://download.pytorch.org/whl/cpu/torch_stable.html
pip install -r requirements/pytorch/devel.txt --find-links https://download.pytorch.org/whl/torch_stable.html
pip install -r requirements/pytorch/strategies.txt
# set a per-test timeout of 2.5 minutes to fail sooner; this aids with hanging tests
pip install pytest-timeout

View File

@ -127,7 +127,7 @@ jobs:
run: |
# adjust versions according installed Torch version
python ./requirements/pytorch/adjust-versions.py requirements/pytorch/extra.txt
pip install --requirement ./requirements/pytorch/extra.txt --find-links https://download.pytorch.org/whl/cpu/torch_stable.html --upgrade
pip install -r ./requirements/pytorch/extra.txt --find-links https://download.pytorch.org/whl/cpu/torch_stable.html --upgrade
pip list
shell: bash

View File

@ -71,8 +71,11 @@ ENV \
COPY environment.yml environment.yml
# conda init
RUN conda update -n base -c defaults conda && \
conda create -y --name $CONDA_ENV python=${PYTHON_VERSION} pytorch=${PYTORCH_VERSION} torchvision torchtext cudatoolkit=${CUDA_VERSION} -c nvidia -c pytorch -c pytorch-test -c pytorch-nightly && \
RUN \
conda update -n base -c defaults conda && \
conda create -y --name $CONDA_ENV \
python=${PYTHON_VERSION} pytorch=${PYTORCH_VERSION} torchvision torchtext cudatoolkit=${CUDA_VERSION} \
-c nvidia -c pytorch -c pytorch-test -c pytorch-nightly && \
conda init bash && \
# NOTE: this requires that the channel is presented in the yaml before packages \
printf "import re;\nfname = 'environment.yml';\nreq = open(fname).read();\nfor n in ['python', 'pytorch', 'torchtext', 'torchvision']:\n req = re.sub(rf'- {n}[>=]+', f'# - {n}=', req);\nopen(fname, 'w').write(req)" > prune.py && \
@ -80,6 +83,7 @@ RUN conda update -n base -c defaults conda && \
rm prune.py && \
cat environment.yml && \
conda env update --name $CONDA_ENV --file environment.yml && \
conda install "Pillow<9.0" && \
conda clean -ya && \
rm environment.yml
@ -94,12 +98,11 @@ RUN \
pip list | grep torch && \
python -c "import torch; print(torch.__version__)" && \
pip install -q fire && \
python requirements/pytorch/adjust-versions.py requirements/pytorch/extra.txt && \
python requirements/pytorch/adjust-versions.py requirements/pytorch/examples.txt && \
python assistant.py requirements_prune_pkgs torch,torchvision,torchtext && \
# Install remaining requirements
pip install -r requirements/pytorch/base.txt --no-cache-dir --find-links https://download.pytorch.org/whl/test/torch_test.html && \
pip install -r requirements/pytorch/extra.txt --no-cache-dir --find-links https://download.pytorch.org/whl/test/torch_test.html && \
pip install -r requirements/pytorch/examples.txt --no-cache-dir --find-links https://download.pytorch.org/whl/test/torch_test.html && \
pip install --no-cache-dir -r requirements/pytorch/base.txt \
-r requirements/pytorch/extra.txt \
-r requirements/pytorch/examples.txt && \
rm assistant.py
ENV \

View File

@ -5,9 +5,9 @@ from typing import Dict, Optional
# IMPORTANT: this list needs to be sorted in reverse
VERSIONS = [
dict(torch="1.12.0", torchvision="0.12.*", torchtext=""), # nightly
dict(torch="1.11.0", torchvision="0.12.0", torchtext="0.12.0"), # pre-release
dict(torch="1.10.2", torchvision="0.11.3", torchtext="0.11.2"), # stable
dict(torch="1.12.0", torchvision="0.13.0", torchtext="0.13.0"), # stable
dict(torch="1.11.0", torchvision="0.12.0", torchtext="0.12.0"),
dict(torch="1.10.2", torchvision="0.11.3", torchtext="0.11.2"),
dict(torch="1.10.1", torchvision="0.11.2", torchtext="0.11.1"),
dict(torch="1.10.0", torchvision="0.11.1", torchtext="0.11.0"),
dict(torch="1.9.1", torchvision="0.10.1", torchtext="0.10.1"),
@ -48,7 +48,7 @@ def main(req: str, torch_version: Optional[str] = None) -> str:
return req
def test():
def test_check():
requirements = """
torch>=1.2.*
torch==1.2.3
@ -74,7 +74,7 @@ def test():
if __name__ == "__main__":
test() # sanity check
test_check() # sanity check
if len(sys.argv) == 3:
requirements_path, torch_version = sys.argv[1:]

View File

@ -1,5 +1,5 @@
numpy>=1.17.2, <1.23.1
torch>=1.9.*, <=1.11.0 # strict
torch>=1.9.*, <=1.12.0
tqdm>=4.57.0, <=4.63.0
PyYAML>=5.4, <=6.0
fsspec[http]>=2021.05.0, !=2021.06.0, <2022.6.0

View File

@ -26,7 +26,7 @@ from torch.quantization import FakeQuantizeBase
import pytorch_lightning as pl
from pytorch_lightning.callbacks.callback import Callback
from pytorch_lightning.utilities import _TORCH_GREATER_EQUAL_1_10, _TORCH_GREATER_EQUAL_1_11
from pytorch_lightning.utilities import _TORCH_GREATER_EQUAL_1_10, _TORCH_GREATER_EQUAL_1_11, _TORCH_GREATER_EQUAL_1_12
from pytorch_lightning.utilities.exceptions import MisconfigurationException
if _TORCH_GREATER_EQUAL_1_10:
@ -247,10 +247,14 @@ class QuantizationAwareTraining(Callback):
if self._observer_type == "histogram":
model.qconfig = torch.quantization.get_default_qconfig(self._qconfig)
elif self._observer_type == "average":
extra_kwargs = {}
if _TORCH_GREATER_EQUAL_1_12:
extra_kwargs["version"] = 0
# version=None corresponds to using FakeQuantize rather than
# FusedMovingAvgObsFakeQuantize which was introduced in PT1.10
# details in https://github.com/pytorch/pytorch/issues/64564
extra_kwargs = dict(version=None) if _TORCH_GREATER_EQUAL_1_10 else {}
elif _TORCH_GREATER_EQUAL_1_10:
extra_kwargs["version"] = None
model.qconfig = torch.quantization.get_default_qat_qconfig(self._qconfig, **extra_kwargs)
elif isinstance(self._qconfig, QConfig):

View File

@ -45,7 +45,7 @@ from pytorch_lightning.utilities.apply_func import apply_to_collection, convert_
from pytorch_lightning.utilities.cloud_io import get_filesystem
from pytorch_lightning.utilities.distributed import distributed_available, sync_ddp
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities.imports import _TORCH_GREATER_EQUAL_1_11, _TORCH_GREATER_EQUAL_1_12
from pytorch_lightning.utilities.imports import _TORCH_GREATER_EQUAL_1_11, _TORCH_GREATER_EQUAL_1_13
from pytorch_lightning.utilities.parsing import collect_init_args
from pytorch_lightning.utilities.rank_zero import rank_zero_debug, rank_zero_deprecation, rank_zero_warn
from pytorch_lightning.utilities.signature_utils import is_param_in_hook_signature
@ -1987,7 +1987,7 @@ class LightningModule(
self._register_state_dict_hook(state_dict_hook)
if _TORCH_GREATER_EQUAL_1_12:
if _TORCH_GREATER_EQUAL_1_13:
self._register_load_state_dict_pre_hook(pre_load_state_dict_hook, True)
else:
# We need to make sure the self inside the method is a weakref proxy

View File

@ -44,6 +44,7 @@ from pytorch_lightning.utilities.imports import ( # noqa: F401
_POPTORCH_AVAILABLE,
_TORCH_GREATER_EQUAL_1_10,
_TORCH_GREATER_EQUAL_1_11,
_TORCH_GREATER_EQUAL_1_12,
_TORCH_QUANTIZE_AVAILABLE,
_TORCHTEXT_AVAILABLE,
_TORCHVISION_AVAILABLE,

View File

@ -128,7 +128,8 @@ _TORCH_GREATER_EQUAL_1_9_1 = _compare_version("torch", operator.ge, "1.9.1")
_TORCH_GREATER_EQUAL_1_10 = _compare_version("torch", operator.ge, "1.10.0")
_TORCH_LESSER_EQUAL_1_10_2 = _compare_version("torch", operator.le, "1.10.2")
_TORCH_GREATER_EQUAL_1_11 = _compare_version("torch", operator.ge, "1.11.0")
_TORCH_GREATER_EQUAL_1_12 = _compare_version("torch", operator.ge, "1.12.0", use_base_version=True)
_TORCH_GREATER_EQUAL_1_12 = _compare_version("torch", operator.ge, "1.12.0")
_TORCH_GREATER_EQUAL_1_13 = _compare_version("torch", operator.ge, "1.13.0", use_base_version=True)
_APEX_AVAILABLE = _module_available("apex.amp")
_DALI_AVAILABLE = _module_available("nvidia.dali")

View File

@ -22,7 +22,7 @@ from torch.utils.data import DataLoader
from pytorch_lightning import LightningModule, seed_everything, Trainer
from pytorch_lightning.callbacks import BackboneFinetuning, BaseFinetuning, ModelCheckpoint
from pytorch_lightning.demos.boring_classes import BoringModel, RandomDataset
from pytorch_lightning.utilities.imports import _TORCH_GREATER_EQUAL_1_11
from pytorch_lightning.utilities.imports import _TORCH_GREATER_EQUAL_1_11, _TORCH_GREATER_EQUAL_1_12
class TestBackboneFinetuningCallback(BackboneFinetuning):
@ -368,6 +368,8 @@ def test_callbacks_restore(tmpdir):
}
if _TORCH_GREATER_EQUAL_1_11:
expected["maximize"] = False
if _TORCH_GREATER_EQUAL_1_12:
expected["foreach"] = None
assert callback._internal_optimizer_metadata[0][0] == expected
@ -382,6 +384,9 @@ def test_callbacks_restore(tmpdir):
}
if _TORCH_GREATER_EQUAL_1_11:
expected["maximize"] = False
if _TORCH_GREATER_EQUAL_1_12:
expected["foreach"] = None
assert callback._internal_optimizer_metadata[0][1] == expected
trainer_kwargs["max_epochs"] = 3

View File

@ -30,10 +30,12 @@ from tests_pytorch.helpers.runif import RunIf
from tests_pytorch.helpers.simple_models import RegressionModel
# todo: [True-False-average] and [False-False-average] fail with 1.12
# error: assert False (tensor(0.3262), tensor(0.8754), atol=0.45)
@pytest.mark.parametrize("observe", ["average", "histogram"])
@pytest.mark.parametrize("fuse", [True, False])
@pytest.mark.parametrize("convert", [True, False])
@RunIf(quantization=True)
@RunIf(quantization=True, max_torch="1.11")
def test_quantization(tmpdir, observe: str, fuse: bool, convert: bool):
"""Parity test for quant model."""
cuda_available = GPUAccelerator.is_available()

View File

@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from unittest.mock import patch
import numpy as np
import onnxruntime
@ -22,6 +23,7 @@ import tests_pytorch.helpers.pipelines as tpipes
import tests_pytorch.helpers.utils as tutils
from pytorch_lightning import Trainer
from pytorch_lightning.demos.boring_classes import BoringModel
from pytorch_lightning.utilities.imports import _TORCH_GREATER_EQUAL_1_12
from tests_pytorch.helpers.runif import RunIf
from tests_pytorch.utilities.test_model_summary import UnorderedModel
@ -118,11 +120,18 @@ def test_verbose_param(tmpdir, capsys):
"""Test that output is present when verbose parameter is set."""
model = BoringModel()
model.example_input_array = torch.randn(5, 32)
file_path = os.path.join(tmpdir, "model.onnx")
model.to_onnx(file_path, verbose=True)
captured = capsys.readouterr()
assert "graph(%" in captured.out
if _TORCH_GREATER_EQUAL_1_12:
with patch("torch.onnx.log", autospec=True) as test:
model.to_onnx(file_path, verbose=True)
args, kwargs = test.call_args
prefix, graph = args
assert prefix == "Exported graph: "
else:
model.to_onnx(file_path, verbose=True)
captured = capsys.readouterr()
assert "graph(%" in captured.out
def test_error_if_no_input(tmpdir):

View File

@ -251,7 +251,8 @@ def _run_collab_training_fn(initial_peers, wait_seconds, barrier, recorded_proce
recorded_process_steps.append(recorded_global_steps)
@RunIf(hivemind=True)
# TODO: check why it fails with PT 1.12
@RunIf(hivemind=True, max_torch="1.12")
@mock.patch.dict(os.environ, {"HIVEMIND_MEMORY_SHARING_STRATEGY": "file_descriptor"}, clear=True)
@pytest.mark.parametrize(
"num_processes, wait_seconds",

View File

@ -1501,7 +1501,7 @@ def test_cli_trainer_no_callbacks():
def test_unresolvable_import_paths():
class TestModel(BoringModel):
def __init__(self, a_func: Callable = torch.softmax):
def __init__(self, a_func: Callable = torch.nn.Softmax):
super().__init__()
self.a_func = a_func
@ -1509,7 +1509,7 @@ def test_unresolvable_import_paths():
with mock.patch("sys.argv", ["any.py", "--print_config"]), redirect_stdout(out), pytest.raises(SystemExit):
LightningCLI(TestModel, run=False)
assert "a_func: torch.softmax" in out.getvalue()
assert "a_func: torch.nn.Softmax" in out.getvalue()
def test_pytorch_profiler_init_args():