From 60d0c95e07b8edfa22f5892ebb72d93b1d35832f Mon Sep 17 00:00:00 2001 From: Jirka Borovec Date: Fri, 5 Mar 2021 21:39:52 +0100 Subject: [PATCH] fix importing torchtext batch (#6365) * copy torchtext batch * update * rev * rev --- .github/workflows/docs-checks.yml | 10 +++++----- .../plugins/training_type/training_type_plugin.py | 2 +- pytorch_lightning/utilities/apply_func.py | 9 ++++++--- tests/helpers/imports.py | 8 ++++++++ tests/models/test_gpu.py | 2 +- tests/utilities/test_apply_func_torchtext.py | 9 ++++----- 6 files changed, 25 insertions(+), 15 deletions(-) create mode 100644 tests/helpers/imports.py diff --git a/.github/workflows/docs-checks.yml b/.github/workflows/docs-checks.yml index 347f20196d974..5ee4f23b4b3cc 100644 --- a/.github/workflows/docs-checks.yml +++ b/.github/workflows/docs-checks.yml @@ -41,6 +41,8 @@ jobs: - name: Install dependencies run: | + python --version + pip --version # remove Horovod from requirements python -c "fname = 'requirements/extra.txt' ; lines = [line for line in open(fname).readlines() if not line.startswith('horovod')] ; open(fname, 'w').writelines(lines)" # python -m pip install --upgrade --user pip @@ -48,8 +50,6 @@ jobs: pip install --requirement requirements/extra.txt pip install --requirement requirements/loggers.txt pip install --requirement requirements/docs.txt - python --version - pip --version pip list shell: bash @@ -84,12 +84,12 @@ jobs: - name: Install dependencies run: | - pip install --requirement requirements.txt --upgrade-strategy only-if-needed --find-links https://download.pytorch.org/whl/cpu/torch_stable.html --quiet + python --version + pip --version + # pip install --requirement requirements.txt --upgrade-strategy only-if-needed --find-links https://download.pytorch.org/whl/cpu/torch_stable.html --quiet pip install --requirement requirements/docs.txt # install Texlive, see https://linuxconfig.org/how-to-install-latex-on-ubuntu-20-04-focal-fossa-linux sudo apt-get update && sudo apt-get install -y texlive-latex-extra dvipng texlive-pictures - python --version - pip --version pip list shell: bash diff --git a/pytorch_lightning/plugins/training_type/training_type_plugin.py b/pytorch_lightning/plugins/training_type/training_type_plugin.py index 4c84ec8cbd5b4..d7c3b4d4d77e1 100644 --- a/pytorch_lightning/plugins/training_type/training_type_plugin.py +++ b/pytorch_lightning/plugins/training_type/training_type_plugin.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from abc import ABC, abstractmethod -from typing import Any, Callable, Dict, Iterable, Optional, TYPE_CHECKING, Union +from typing import Any, Callable, Iterable, Optional, TYPE_CHECKING, Union import torch from torch.nn import Module diff --git a/pytorch_lightning/utilities/apply_func.py b/pytorch_lightning/utilities/apply_func.py index 2f7425bf3beb0..9fd42008b9d8d 100644 --- a/pytorch_lightning/utilities/apply_func.py +++ b/pytorch_lightning/utilities/apply_func.py @@ -11,7 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +import operator from abc import ABC from collections.abc import Mapping, Sequence from copy import copy @@ -22,10 +22,13 @@ import torch from pytorch_lightning.utilities.exceptions import MisconfigurationException -from pytorch_lightning.utilities.imports import _TORCHTEXT_AVAILABLE +from pytorch_lightning.utilities.imports import _compare_version, _TORCHTEXT_AVAILABLE if _TORCHTEXT_AVAILABLE: - from torchtext.data import Batch + if _compare_version("torchtext", operator.ge, "0.9.0"): + from torchtext.legacy.data import Batch + else: + from torchtext.data import Batch else: Batch = type(None) diff --git a/tests/helpers/imports.py b/tests/helpers/imports.py new file mode 100644 index 0000000000000..4db9c00d45eab --- /dev/null +++ b/tests/helpers/imports.py @@ -0,0 +1,8 @@ +import operator + +from pytorch_lightning.utilities.imports import _compare_version + +if _compare_version("torchtext", operator.ge, "0.9.0"): + from torchtext.legacy.data import Batch, Dataset, Example, Field, Iterator, LabelField # noqa: F401 +else: + from torchtext.data import Batch, Dataset, Example, Field, Iterator, LabelField # noqa: F401 diff --git a/tests/models/test_gpu.py b/tests/models/test_gpu.py index f30f12009450e..fe5f507fbacb4 100644 --- a/tests/models/test_gpu.py +++ b/tests/models/test_gpu.py @@ -16,7 +16,6 @@ import pytest import torch -from torchtext.data import Batch, Dataset, Example, Field, LabelField import tests.helpers.pipelines as tpipes import tests.helpers.utils as tutils @@ -24,6 +23,7 @@ from pytorch_lightning.utilities import device_parser from pytorch_lightning.utilities.exceptions import MisconfigurationException from tests.helpers import BoringModel +from tests.helpers.imports import Batch, Dataset, Example, Field, LabelField PRETEND_N_OF_GPUS = 16 diff --git a/tests/utilities/test_apply_func_torchtext.py b/tests/utilities/test_apply_func_torchtext.py index c7fec954fdb2f..c5b80ebbb14ee 100644 --- a/tests/utilities/test_apply_func_torchtext.py +++ b/tests/utilities/test_apply_func_torchtext.py @@ -13,14 +13,13 @@ # limitations under the License. import pytest import torch -import torchtext -from torchtext.data.example import Example from pytorch_lightning.utilities.apply_func import move_data_to_device +from tests.helpers.imports import Dataset, Example, Field, Iterator def _get_torchtext_data_iterator(include_lengths=False): - text_field = torchtext.data.Field( + text_field = Field( sequential=True, pad_first=False, # nosec init_token="", @@ -32,13 +31,13 @@ def _get_torchtext_data_iterator(include_lengths=False): example2 = Example.fromdict({"text": "b c a a"}, {"text": ("text", text_field)}) example3 = Example.fromdict({"text": "c b a"}, {"text": ("text", text_field)}) - dataset = torchtext.data.Dataset( + dataset = Dataset( [example1, example2, example3], {"text": text_field}, ) text_field.build_vocab(dataset) - iterator = torchtext.data.Iterator( + iterator = Iterator( dataset, batch_size=3, sort_key=None,