Repository navigation
Fix Lesson 5 NLP PyTorch track: installable requirements and torchtext 0.18 imports - #782
Open
Qiming Bao (14H034160212) wants to merge 1 commit into
Open
Qiming Bao (14H034160212) wants to merge 1 commit into
Qiming Bao (14H034160212) wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix Lesson 5 (NLP) PyTorch track: installable requirements + torchtext 0.18 imports
What is broken on
main1.
lessons/5-NLP/requirements-pytorch.txtcannot be installed on any Python version.It pins
torch==2.13.0together withtorchaudio==0.8.1,torchvision==0.9.1andtorchtext==0.9.1, which belong to the torch 1.8.1 line and only ship wheels up to cp39, whilenltk==3.10.4needs Python >= 3.10. On Python 3.12:opencv-python==4.5.1.48also has no wheel for Python 3.10+, andnumpy==1.22.0has 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.ipynbalso uses the legacytorchtext.vocab.Vocab(Counter, min_freq=...)constructor andvocab.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.0torchdata==0.8.0andportalocker.torchtext.datasets.AG_NEWSneeds DataPipes, which were removed in torchdata 0.10. The repo'senvironment.ymlalready liststorchdata.numpy>=1.22,<2, because torch 2.3 is built against NumPy 1.x. This matchesnumpy=1.26inenvironment.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).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.ipynbRNNPyTorch.ipynbandTransformersPyTorch.ipynbonly use torchtext throughtorchnlp.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>'])plusset_default_index(vocab['<unk>'])keeps the old behaviour:<unk>/<pad>specials, and OOV words map to<unk>.vocab.itosbecomesvocab.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 checkis clean, and the file also resolves for Python 3.11. Each notebook was executed end-to-end on CPU (CUDA_VISIBLE_DEVICES="") withjupyter nbconvert --to notebook --executeinto a temporary output path:torchtext.vocab.Vectors(GloVe base class) code path was checked separately on a small local vector fileclose_wordsworksbert_model = 'bert-base-uncased'and fewer iterations (30 train / 20 test); no errorsopenai-gpt, no errors (this notebook does not use torchtext; run to check thetransformerspin)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
torchalone (as in chore(deps): bump torch from 2.8.0 to 2.12.0 in /lessons/5-NLP in the pip group across 1 directory #643 / chore(deps): bump torch from 2.8.0 to 2.12.1 in /lessons/5-NLP in the pip group across 1 directory #676 / chore(deps): bump torch from 2.12.1 to 2.13.0 in /lessons/5-NLP in the pip group across 1 directory #701) will break the lesson again. Longer term the lesson would need to move off torchtext (e.g. todatasets+ a plain tokenizer/vocab).torch.load(weights_only=True), fixed in 2.6). The notebooks do not load untrusted checkpoints, but reviewers may want to weigh this.