Skip to content

Fix Lesson 5 NLP PyTorch track: installable requirements and torchtext 0.18 imports - #782

Open
Qiming Bao (14H034160212) wants to merge 1 commit into
microsoft:mainfrom
14H034160212:fix-nlp-pytorch-torchtext
Open

Qiming Bao (14H034160212) wants to merge 1 commit into
microsoft:mainfrom
14H034160212:fix-nlp-pytorch-torchtext

Conversation

@14H034160212

Copy link
Copy Markdown

Fix Lesson 5 (NLP) PyTorch track: installable requirements + torchtext 0.18 imports

What is broken on main

1. lessons/5-NLP/requirements-pytorch.txt cannot be installed on any Python version.
It pins torch==2.13.0 together with torchaudio==0.8.1, torchvision==0.9.1 and torchtext==0.9.1, which belong to the torch 1.8.1 line and only ship wheels up to cp39, while nltk==3.10.4 needs Python >= 3.10. On Python 3.12:

× No solution found when resolving dependencies:
╰─▶ Because torchaudio==0.8.1 has no wheels with a matching Python ABI tag
    (e.g., `cp312`) and you require torchaudio==0.8.1, we can conclude that
    your requirements are unsatisfiable.

opencv-python==4.5.1.48 also has no wheel for Python 3.10+, and numpy==1.22.0 has no wheel for 3.11+.

2. With a consistent install (torch 2.3.1 + torchtext 0.18.0) the notebooks still fail. torchtext 0.18 no longer imports its submodules from import torchtext, so attribute access fails:

  • TextRepresentationPyTorch.ipynb, first cell:
    AttributeError: module 'torchtext' has no attribute 'datasets'
  • torchnlp.py (imported by the Embeddings, RNN, Generative and Transformers notebooks), on import:
    AttributeError: module 'torchtext' has no attribute 'data'
  • CBoW-PyTorch.ipynb also uses the legacy torchtext.vocab.Vocab(Counter, min_freq=...) constructor and vocab.itos, which were removed in torchtext 0.12:
    TypeError: Vocab.__init__() got an unexpected keyword argument 'min_freq'

Related: #632 (closed by #638, which fixed only the README typo and the gensim pin).

What this PR changes

  • lessons/5-NLP/requirements-pytorch.txt: a consistent, installable set for torchtext 0.18.0, the final torchtext release. torchtext is deprecated, and 0.18 only works with torch 2.3.
    • torch==2.3.1, torchaudio==2.3.1, torchvision==0.18.1, torchtext==0.18.0
    • add torchdata==0.8.0 and portalocker. torchtext.datasets.AG_NEWS needs DataPipes, which were removed in torchdata 0.10. The repo's environment.yml already lists torchdata.
    • numpy>=1.22,<2, because torch 2.3 is built against NumPy 1.x. This matches numpy=1.26 in environment.yml / .devcontainer.
    • opencv-python==4.11.0.86, the latest release that still has NumPy 1.x-compatible wheels for py3.11/3.12.
    • transformers==4.57.6. transformers 5.x refuses to use torch < 2.4 ("BertForSequenceClassification requires the PyTorch library but it was not found"). torchtext 0.18's C++ extension does not load on torch 2.4 (libtorchtext.so: undefined symbol: _ZN5torch3jit17parseSchemaOrNameERKSs).
    • Comments at the top of the file explain the constraints: Python 3.11 or 3.12 (torchtext has no cp313 wheels, Pillow 13 needs >= 3.11), and the torch family must be upgraded together.
  • Explicit torchtext submodule imports (import torchtext.data.utils, import torchtext.datasets, import torchtext.vocab). The rest of the code is unchanged. The new lines go in:
    • lessons/5-NLP/{14-Embeddings,16-RNN,17-GenerativeNetworks,18-Transformers}/torchnlp.py (all four identical copies)
    • 13-TextRep/TextRepresentationPyTorch.ipynb, 14-Embeddings/EmbeddingsPyTorch.ipynb, 15-LanguageModeling/CBoW-PyTorch.ipynb, 17-GenerativeNetworks/GenerativePyTorch.ipynb
    • RNNPyTorch.ipynb and TransformersPyTorch.ipynb only use torchtext through torchnlp.py, so they need no change.
  • CBoW-PyTorch.ipynb: port the vocab construction to the current API. torchtext.vocab.vocab(OrderedDict(counter.most_common(vocab_size)), min_freq=..., specials=['<unk>', '<pad>']) plus set_default_index(vocab['<unk>']) keeps the old behaviour: <unk>/<pad> specials, and OOV words map to <unk>. vocab.itos becomes vocab.get_itos().

Only the source lines of the affected code cells were edited, so existing cell outputs and notebook metadata are untouched. Only the English sources are changed (translations are synced automatically).

Verification

Fresh Python 3.12 venv with the CPU wheels of torch/torchvision/torchaudio/torchtext and pip install -r requirements-pytorch.txt. pip check is clean, and the file also resolves for Python 3.11. Each notebook was executed end-to-end on CPU (CUDA_VISIBLE_DEVICES="") with jupyter nbconvert --to notebook --execute into a temporary output path:

Notebook Result
13-TextRep/TextRepresentationPyTorch.ipynb all 16 code cells run, no errors; BoW classifier reaches acc 0.867 on 15k samples
14-Embeddings/EmbeddingsPyTorch.ipynb the AG_NEWS / Embedding / EmbeddingBag part (first 7 code cells) runs, acc ~0.76. The word2vec (1.6 GB gensim download) and GloVe 6B (822 MB download) sections were not executed. The torchtext.vocab.Vectors (GloVe base class) code path was checked separately on a small local vector file
15-LanguageModeling/CBoW-PyTorch.ipynb all 17 code cells run, no errors; 10 epochs, loss 6.12 → 5.56, close_words works
16-RNN/RNNPyTorch.ipynb all 8 code cells run, no errors; RNN 0.808, LSTM 0.663, packed LSTM 0.819 train acc
17-GenerativeNetworks/GenerativePyTorch.ipynb all 8 code cells run, no errors; char-LSTM trains (loss ~1.6) and generates text
18-Transformers/TransformersPyTorch.ipynb runs with bert_model = 'bert-base-uncased' and fewer iterations (30 train / 20 test); no errors
20-LangModels/GPT-PyTorch.ipynb all 8 code cells run with openai-gpt, no errors (this notebook does not use torchtext; run to check the transformers pin)

Note on the Transformers notebook: it sets bert_model = './bert' (a folder that only exists in the Microsoft Learn sandbox), so running it locally needs the line above it ('bert-base-uncased'). This PR does not change that.

Caveats

requirements-pytorch.txt was not installable: torch==2.13.0 was pinned
next to torchaudio/torchvision/torchtext from the torch 1.8 line, which
only ship wheels up to cp39, while nltk 3.10.4 needs Python >= 3.10.

- Pin a consistent set for torchtext 0.18.0, the final (deprecated)
  release: torch 2.3.1, torchaudio 2.3.1, torchvision 0.18.1,
  torchdata 0.8.0 and portalocker (needed by torchtext.datasets),
  numpy<2, opencv-python 4.11.0.86 and transformers 4.57.6
  (transformers 5.x requires torch >= 2.4). Document the
  Python 3.11/3.12 constraint.
- torchtext 0.18 no longer exposes datasets/data/vocab as attributes
  of the package, so import the submodules explicitly in the four
  torchnlp.py copies and the notebooks that use them directly.
- CBoW-PyTorch: replace the removed legacy Vocab(Counter, min_freq)
  constructor and vocab.itos with torchtext.vocab.vocab() and
  get_itos(), keeping <unk>/<pad> specials and <unk> as default index.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant