I want to run the 3 code snippets from this webpage.
I've made all 3 one post, as I am assuming it all stems from the same problem of optimum
not having been imported correctly?
Kernel: conda_pytorch_p36
Installations:
pip install optimum
OR
! pip install datasets transformers optimum[intel]
Both provide same Traceback:
Requirement already satisfied: optimum in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (0.1.3)
Requirement already satisfied: transformers>=4.12.0 in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from optimum) (4.15.0)
Requirement already satisfied: coloredlogs in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from optimum) (15.0.1)
Requirement already satisfied: torch>=1.9 in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from optimum) (1.10.1)
Requirement already satisfied: sympy in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from optimum) (1.8)
Requirement already satisfied: typing-extensions in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from torch>=1.9->optimum) (3.10.0.0)
Requirement already satisfied: dataclasses in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from torch>=1.9->optimum) (0.8)
Requirement already satisfied: numpy>=1.17 in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from transformers>=4.12.0->optimum) (1.19.5)
Requirement already satisfied: packaging>=20.0 in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from transformers>=4.12.0->optimum) (21.3)
Requirement already satisfied: pyyaml>=5.1 in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from transformers>=4.12.0->optimum) (5.4.1)
Requirement already satisfied: sacremoses in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from transformers>=4.12.0->optimum) (0.0.46)
Requirement already satisfied: tqdm>=4.27 in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from transformers>=4.12.0->optimum) (4.62.3)
Requirement already satisfied: regex!=2019.12.17 in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from transformers>=4.12.0->optimum) (2021.4.4)
Requirement already satisfied: requests in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from transformers>=4.12.0->optimum) (2.25.1)
Requirement already satisfied: huggingface-hub<1.0,>=0.1.0 in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from transformers>=4.12.0->optimum) (0.2.1)
Requirement already satisfied: tokenizers<0.11,>=0.10.1 in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from transformers>=4.12.0->optimum) (0.10.3)
Requirement already satisfied: importlib-metadata in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from transformers>=4.12.0->optimum) (4.5.0)
Requirement already satisfied: filelock in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from transformers>=4.12.0->optimum) (3.0.12)
Requirement already satisfied: humanfriendly>=9.1 in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from coloredlogs->optimum) (10.0)
Requirement already satisfied: mpmath>=0.19 in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from sympy->optimum) (1.2.1)
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from packaging>=20.0->transformers>=4.12.0->optimum) (2.4.7)
Requirement already satisfied: zipp>=0.5 in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from importlib-metadata->transformers>=4.12.0->optimum) (3.4.1)
Requirement already satisfied: idna<3,>=2.5 in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from requests->transformers>=4.12.0->optimum) (2.10)
Requirement already satisfied: certifi>=2017.4.17 in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from requests->transformers>=4.12.0->optimum) (2021.5.30)
Requirement already satisfied: chardet<5,>=3.0.2 in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from requests->transformers>=4.12.0->optimum) (4.0.0)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from requests->transformers>=4.12.0->optimum) (1.26.5)
Requirement already satisfied: joblib in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from sacremoses->transformers>=4.12.0->optimum) (1.0.1)
Requirement already satisfied: click in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from sacremoses->transformers>=4.12.0->optimum) (8.0.1)
Requirement already satisfied: six in /home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages (from sacremoses->transformers>=4.12.0->optimum) (1.16.0)
Note: you may need to restart the kernel to use updated packages.
from optimum.intel.lpot.quantization import LpotQuantizerForSequenceClassification
# Create quantizer from config
quantizer = LpotQuantizerForSequenceClassification.from_config(
"echarlaix/quantize-dynamic-test",
"quantization.yml",
model_name_or_path="textattack/bert-base-uncased-SST-2",
)
model = quantizer.fit_dynamic()
Traceback:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-6-9dcf25f181ea> in <module>
----> 1 from optimum.intel.lpot.quantization import LpotQuantizerForSequenceClassification
2
3 # Create quantizer from config
4 quantizer = LpotQuantizerForSequenceClassification.from_config(
5 "echarlaix/quantize-dynamic-test",
ModuleNotFoundError: No module named 'optimum.intel.lpot'
from optimum.intel.lpot.pruning import LpotPrunerForSequenceClassification
# Create pruner from config
pruner = LpotPrunerForSequenceClassification.from_config(
"echarlaix/magnitude-pruning-test",
"prune.yml",
model_name_or_path="textattack/bert-base-uncased-SST-2",
)
model = pruner.fit()
Traceback:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-7-e9872c164aee> in <module>
----> 1 from optimum.intel.lpot.pruning import LpotPrunerForSequenceClassification
2
3 # Create pruner from config
4 pruner = LpotPrunerForSequenceClassification.from_config(
5 "echarlaix/magnitude-pruning-test",
ModuleNotFoundError: No module named 'optimum.intel.lpot'
from optimum.graphcore import IPUTrainer
from optimum.graphcore.bert import BertIPUConfig
from transformers import BertForMaskedLM, BertTokenizer
from poptorch.optim import AdamW
# Allocate model and tokenizer as usual
tokenizer = BertTokenizer.from_pretrained("bert-base-cased")
model = BertForMaskedLM.from_pretrained("bert-base-cased")
# Trainer + poptorch custom configuration optional
ipu_config = BertIPUConfig()
trainer = IPUTrainer(model, trainings_args, config=ipu_config)
optimizer = AdamW(model.parameters)
# This is hidden from the user, it will be handled by the Trainer
with trainer.compile(some_data_loader) as model_f:
for steps in range(10): # !
outputs = trainer.step(optimizer)
# Save the model and/or push to hub
model.save_pretrained("...")
model.push_to_hub("...")
Traceback:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-8-921e03245390> in <module>
----> 1 from optimum.graphcore import IPUTrainer
2 from optimum.graphcore.bert import BertIPUConfig
3 from transformers import BertForMaskedLM, BertTokenizer
4 from poptorch.optim import AdamW
5
ModuleNotFoundError: No module named 'optimum.graphcore'
Please let me know if there's anything else I can add to post.
Pointed out by a Contributor of HuggingFace, on this Git Issue,
The library previously named LPOT has been renamed to Intel Neural Compressor (INC), which resulted in a change in the name of our subpackage from
lpot
toneural_compressor
. The correct way to import would now be fromoptimum.intel.neural_compressor.quantization import IncQuantizerForSequenceClassification
Concerning thegraphcore
subpackage, you need to install it first withpip install optimum[graphcore]
Furthermore you'll need to have access to an IPU in order to use it.
Solution
! pip install datasets transformers optimum[graphcore]
Instead of:
from optimum.intel.lpot.quantization import LpotQuantizerForSequenceClassification
from optimum.intel.lpot.pruning import LpotPrunerForSequenceClassification
from optimum.intel.neural_compressor.quantization import IncQuantizerForSequenceClassification
from optimum.intel.neural_compressor.pruning import IncPrunerForSequenceClassification