Skip to content

Commit

Permalink
fix importing torchtext batch (#6365)
Browse files Browse the repository at this point in the history
* copy torchtext batch

* update

* rev

* rev
  • Loading branch information
Borda authored and lexierule committed Mar 9, 2021
1 parent 7a39789 commit 60d0c95
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 15 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/docs-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ 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
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/extra.txt
pip install --requirement requirements/loggers.txt
pip install --requirement requirements/docs.txt
python --version
pip --version
pip list
shell: bash

Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions pytorch_lightning/utilities/apply_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down
8 changes: 8 additions & 0 deletions tests/helpers/imports.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion tests/models/test_gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

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
from pytorch_lightning import Trainer
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

Expand Down
9 changes: 4 additions & 5 deletions tests/utilities/test_apply_func_torchtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="<s>",
Expand All @@ -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,
Expand Down

0 comments on commit 60d0c95

Please sign in to comment.