huggingface / optimum-intel

🤗 Optimum Intel: Accelerate inference with Intel optimization tools
https://huggingface.co/docs/optimum/main/en/intel/index
Apache License 2.0
418 stars 112 forks source link

Unable to load quantized model from huggingface #207

Closed plvckn closed 1 year ago

plvckn commented 1 year ago

Steps to reproduce: run code sample from https://huggingface.co/Intel/distilbart-cnn-12-6-int8-dynamic

from optimum.intel.neural_compressor.quantization import IncQuantizedModelForSeq2SeqLM
int8_model = IncQuantizedModelForSeq2SeqLM.from_pretrained(
    'Intel/distilbart-cnn-12-6-int8-dynamic',
)

Error message:

AttributeError                            Traceback (most recent call last)
<ipython-input-1-4760d454e35e> in <module>
      1 from optimum.intel.neural_compressor.quantization import IncQuantizedModelForSeq2SeqLM
----> 2 int8_model = IncQuantizedModelForSeq2SeqLM.from_pretrained(
      3     'Intel/distilbart-cnn-12-6-int8-dynamic',
      4 )

~/anaconda3/envs/nlp/lib/python3.9/site-packages/optimum/intel/neural_compressor/quantization.py in from_pretrained(cls, *args, **kwargs)
    599             f"`{cls.__name__.replace('IncQuantized', 'INC')}` instead."
    600         )
--> 601         return super().from_pretrained(*args, **kwargs)
    602 
    603 

~/anaconda3/envs/nlp/lib/python3.9/site-packages/optimum/intel/neural_compressor/quantization.py in from_pretrained(cls, model_name_or_path, q_model_name, **kwargs)
    540                     raise EnvironmentError(msg)
    541 
--> 542         if config.backend == "ipex":
    543             # NOTE: Will improve to use load function when Intel Neural Compressor next 2.1 release.
    544             # return load(state_dict_path)

~/anaconda3/envs/nlp/lib/python3.9/site-packages/transformers/configuration_utils.py in __getattribute__(self, key)
    258         if key != "attribute_map" and key in super().__getattribute__("attribute_map"):
    259             key = super().__getattribute__("attribute_map")[key]
--> 260         return super().__getattribute__(key)
    261 
    262     def __init__(self, **kwargs):

AttributeError: 'BartConfig' object has no attribute 'backend'

My environment:

absl-py                            1.0.0
aiohttp                            3.8.1
aiohttp-socks                      0.7.1
aiosignal                          1.2.0
aiosocks                           0.2.6
aiosocksy                          0.1.2
alabaster                          0.7.12
amazoncaptcha                      0.5.8
anaconda-client                    1.9.0
anaconda-project                   0.10.1
anyio                              2.2.0
anytree                            2.8.0
app-store-scraper                  0.3.5
appdirs                            1.4.4
argh                               0.26.2
argon2-cffi                        20.1.0
arrow                              0.13.1
asn1crypto                         1.4.0
astroid                            2.6.6
astunparse                         1.6.3
async-generator                    1.10
async-timeout                      4.0.1
atomicwrites                       1.4.0
attrs                              21.2.0
autopep8                           1.5.7
Babel                              2.9.1
backcall                           0.2.0
backoff                            2.0.1
backports.functools-lru-cache      1.6.4
backports.shutil-get-terminal-size 1.0.0
backports.tempfile                 1.0
backports.weakref                  1.0.post1
beautifulsoup4                     4.10.0
bertopic                           0.11.0
binaryornot                        0.4.4
bitarray                           2.3.0
black                              19.10b0
bleach                             4.0.0
blessed                            1.20.0
blinker                            1.5
blis                               0.7.5
boto                               2.49.0
bpemb                              0.3.3
bpython                            0.24
Brotli                             1.0.9
brotlipy                           0.7.0
bs4                                0.0.1
cached-property                    1.5.2
cachetools                         5.0.0
catalogue                          2.0.6
certifi                            2021.10.8
cffi                               1.14.6
chardet                            4.0.0
charset-normalizer                 2.0.4
click                              8.0.3
cloudpickle                        2.0.0
clyent                             1.2.2
colorama                           0.4.4
coloredlogs                        15.0.1
commonmark                         0.9.1
conda-content-trust                0+unknown
conda-pack                         0.6.0
conda-package-handling             1.7.3
conda-repo-cli                     1.0.4
conda-verify                       3.4.2
ConfigArgParse                     1.5.3
conllu                             4.4.1
contextlib2                        0.6.0.post1
cookiecutter                       1.7.2
coverage                           6.2
cryptography                       38.0.4
cssselect                          1.2.0
curtsies                           0.4.1
cwcwidth                           0.1.8
cycler                             0.10.0
cymem                              2.0.6
Cython                             0.29.24
cytoolz                            0.11.0
dask                               2021.10.0
datasets                           2.10.0
debugpy                            1.4.1
deckar01-ratelimit                 3.0.2
decorator                          5.1.0
deepl                              1.6.0
defusedxml                         0.7.1
Deprecated                         1.2.13
diff-match-patch                   20200713
dill                               0.3.4
distributed                        2021.10.0
docker-pycreds                     0.4.0
docopt                             0.6.2
docutils                           0.17.1
EasyProcess                        0.3
elasticsearch                      7.16.2
elasticsearch-dsl                  7.4.0
en-core-web-sm                     3.2.0
entrypoints                        0.3
et-xmlfile                         1.1.0
evaluate                           0.4.0
evdev                              1.5.0
fake-useragent                     1.1.1
fastcache                          1.1.0
filelock                           3.3.1
flair                              0.10
flake8                             3.9.2
Flask                              2.2.3
Flask-BasicAuth                    0.2.0
Flask-Cors                         3.0.10
flatbuffers                        2.0
fonttools                          4.25.0
frozenlist                         1.2.0
fsspec                             2023.1.0
ftfy                               6.0.3
furl                               2.1.3
future                             0.18.2
futures                            2.2.0
gast                               0.4.0
gdown                              3.12.2
gensim                             4.1.2
gevent                             21.8.0
geventhttpclient                   2.0.8
gitdb                              4.0.9
GitPython                          3.1.27
glob2                              0.7
gmpy2                              2.0.8
google-auth                        2.6.0
google-auth-oauthlib               0.4.6
google-pasta                       0.2.0
goslate                            1.5.2
graphviz                           0.19.1
greenlet                           1.1.1
grpcio                             1.43.0
gunicorn                           20.1.0
h11                                0.12.0
h2                                 4.1.0
h5py                               3.6.0
hdbscan                            0.8.28
HeapDict                           1.0.1
hpack                              4.0.0
html5lib                           1.1
huggingface-hub                    0.12.1
humanfriendly                      10.0
hyperframe                         6.0.1
idna                               3.2
imagesize                          1.2.0
importlib-metadata                 4.11.3
importlib-resources                5.10.2
inflection                         0.5.1
iniconfig                          1.1.1
intel-extension-for-pytorch        1.13.100
intervaltree                       3.1.0
ipykernel                          6.4.1
ipython                            7.29.0
ipython-genutils                   0.2.0
ipywidgets                         7.6.5
isort                              5.9.3
itsdangerous                       2.0.1
Janome                             0.4.1
jdcal                              1.4.1
jedi                               0.18.0
jeepney                            0.7.1
jellyfish                          0.9.0
Jinja2                             3.1.2
jinja2-time                        0.2.0
joblib                             1.1.0
json5                              0.9.6
jsonlines                          3.1.0
jsonschema                         3.2.0
jupyter                            1.0.0
jupyter-client                     6.1.12
jupyter-console                    6.4.0
jupyter-contrib-core               0.3.3
jupyter-contrib-nbextensions       0.5.1
jupyter-core                       4.8.1
jupyter-highlight-selected-word    0.2.0
jupyter-latex-envs                 1.4.6
jupyter-nbextensions-configurator  0.4.1
jupyter-resource-usage             0.6.1
jupyter-server                     1.4.1
jupyterlab                         3.2.1
jupyterlab-pygments                0.1.2
jupyterlab-server                  2.8.2
jupyterlab-widgets                 1.0.0
jupyterthemes                      0.20.0
kaggle                             1.5.12
kaitaistruct                       0.10
kaleido                            0.2.1
keras                              2.7.0
Keras-Preprocessing                1.1.2
keyboard                           0.13.5
keyphrase-vectorizers              0.0.10
keyring                            23.1.0
kiwisolver                         1.3.1
konoha                             4.6.5
langcodes                          3.3.0
langdetect                         1.0.9
lazy-object-proxy                  1.6.0
lesscpy                            0.15.1
libarchive-c                       2.9
libclang                           13.0.0
llvmlite                           0.38.0
locket                             0.2.1
locust                             2.14.2
lxml                               4.8.0
mando                              0.6.4
Markdown                           3.3.6
MarkupSafe                         2.1.2
matplotlib                         3.5.1
matplotlib-inline                  0.1.2
mccabe                             0.6.1
memory-profiler                    0.60.0
mistune                            0.8.4
mkl-service                        2.4.0
mock                               4.0.3
more-itertools                     8.8.0
mpld3                              0.3
mpmath                             1.2.1
msgpack                            1.0.2
multidict                          5.2.0
multipledispatch                   0.6.0
multiprocess                       0.70.12.2
munkres                            1.1.4
murmurhash                         1.0.6
mypy-extensions                    0.4.3
navigator-updater                  0.2.1
nbclassic                          0.2.6
nbclient                           0.5.3
nbconvert                          6.1.0
nbformat                           5.1.3
nest-asyncio                       1.5.6
networkx                           2.6.3
neural-compressor                  2.0
nltk                               3.6.5
nodebox_linguistics_extended       0.0.1
nose                               1.3.7
notebook                           6.4.5
numba                              0.55.0
numpy                              1.23.5
numpydoc                           1.1.0
nvidia-cublas-cu11                 11.10.3.66
nvidia-cuda-nvrtc-cu11             11.7.99
nvidia-cuda-runtime-cu11           11.7.99
nvidia-cudnn-cu11                  8.5.0.96
oauthlib                           3.2.0
olefile                            0.46
onnx                               1.13.1
onnxruntime                        1.14.0
opencv-python                      4.6.0.66
opendatasets                       0.1.22
openpyxl                           3.0.9
opt-einsum                         3.3.0
optimum                            1.6.4
optimum-graphcore                  0.3.1
optimum-intel                      1.6.2
orderedmultidict                   1.0.1
outcome                            1.1.0
overrides                          3.1.0
packaging                          21.0
pandas                             1.4.0
pandocfilters                      1.4.3
parse                              1.19.0
parsel                             1.7.0
parso                              0.8.2
partd                              1.2.0
path                               16.0.0
pathlib2                           2.3.6
pathspec                           0.7.0
pathtools                          0.1.2
pathy                              0.6.1
pep8                               1.7.1
pexpect                            4.8.0
pickleshare                        0.7.5
Pillow                             9.2.0
pip                                23.0.1
pipreqs                            0.4.11
pkginfo                            1.7.1
plotly                             5.5.0
pluggy                             0.13.1
ply                                3.11
poyo                               0.5.0
preshed                            3.0.6
prettytable                        3.6.0
prometheus-client                  0.11.0
promise                            2.3
prompt-toolkit                     3.0.20
protobuf                           3.20.2
psutil                             5.8.0
ptyprocess                         0.7.0
py                                 1.10.0
py-cpuinfo                         9.0.0
pyarrow                            6.0.1
pyasn1                             0.4.8
pyasn1-modules                     0.2.8
pyChainedProxy                     1.2
pycocotools                        2.0.6
pycodestyle                        2.7.0
pycosat                            0.6.3
pycountry                          22.3.5
pycparser                          2.20
pycurl                             7.44.1
pydantic                           1.8.2
pyDeprecate                        0.3.2
PyDictionary                       2.0.1
pydocstyle                         6.1.1
pydot                              1.4.2
pyee                               8.2.2
pyflakes                           2.3.1
Pygments                           2.10.0
PyJWT                              2.1.0
pylint                             2.9.6
pyls-spyder                        0.4.0
pynndescent                        0.5.5
pynput                             1.7.6
pyodbc                             4.0.0-unsupported
pyOpenSSL                          22.1.0
pyparsing                          3.0.4
pyppeteer                          1.0.2
pyquery                            1.4.3
pyrsistent                         0.18.0
PySide2                            5.15.2.1
PySocks                            1.7.1
pytest                             6.2.4
pytest-cov                         3.0.0
python-dateutil                    2.8.2
python-Levenshtein                 0.12.2
python-lsp-black                   1.0.0
python-lsp-jsonrpc                 1.0.0
python-lsp-server                  1.2.4
python-lzf                         0.2.4
python-slugify                     5.0.2
python-socks                       2.0.0
python-xlib                        0.31
pytorch-lightning                  1.6.2
pyTree                             2.0.0
pytz                               2021.3
PyVirtualDisplay                   2.2
pyxdg                              0.27
PyYAML                             5.4.1
pyzmq                              22.2.1
QDarkStyle                         3.0.2
qstylizer                          0.1.10
QtAwesome                          1.0.2
qtconsole                          5.1.1
QtPy                               1.10.0
radon                              5.1.0
rdbtools                           0.1.15
redis                              4.0.2
regex                              2021.8.3
requests                           2.27.1
requests-file                      1.5.1
requests-html                      0.10.0
requests-oauthlib                  1.3.1
responses                          0.18.0
rich                               11.1.0
rope                               0.19.0
rouge-score                        0.1.2
roundrobin                         0.0.4
rsa                                4.8
Rtree                              0.9.7
ruamel-yaml-conda                  0.15.100
sacremoses                         0.0.46
schema                             0.7.5
scikit-learn                       1.0.2
scipy                              1.10.1
SecretStorage                      3.3.1
segtok                             1.5.11
selectorlib                        0.16.0
selenium                           4.1.0
selenium-wire                      5.1.0
Send2Trash                         1.8.0
sentence-transformers              2.1.0
sentencepiece                      0.1.95
sentry-sdk                         1.9.5
setproctitle                       1.3.2
setuptools                         58.0.4
shiboken2                          5.15.2.1
shortuuid                          1.0.9
simplegeneric                      0.8.1
singledispatch                     3.7.0
sip                                4.19.13
six                                1.16.0
smart-open                         5.2.1
smmap                              5.0.0
sniffio                            1.2.0
snowballstemmer                    2.1.0
sockschain                         1.0.0
sortedcollections                  2.1.0
sortedcontainers                   2.4.0
soupsieve                          2.2.1
spacy                              3.2.1
spacy-alignments                   0.8.5
spacy-legacy                       3.0.8
spacy-loggers                      1.0.1
spacy-transformers                 1.1.7
Sphinx                             4.2.0
sphinxcontrib-applehelp            1.0.2
sphinxcontrib-devhelp              1.0.2
sphinxcontrib-htmlhelp             2.0.0
sphinxcontrib-jsmath               1.0.1
sphinxcontrib-qthelp               1.0.3
sphinxcontrib-serializinghtml      1.1.5
sphinxcontrib-websupport           1.2.4
spyder                             5.1.5
spyder-kernels                     2.1.3
SQLAlchemy                         1.4.22
sqlitedict                         1.7.0
srsly                              2.4.2
stem                               1.8.0
sympy                              1.9
tabulate                           0.8.9
TBB                                0.2
tblib                              1.7.0
tbselenium                         0.6.1
tenacity                           8.0.1
tensorboard                        2.8.0
tensorboard-data-server            0.6.1
tensorboard-plugin-wit             1.8.1
tensorflow                         2.7.0
tensorflow-estimator               2.7.0
tensorflow-hub                     0.12.0
tensorflow-io-gcs-filesystem       0.23.1
termcolor                          1.1.0
terminado                          0.9.4
testpath                           0.5.0
text-unidecode                     1.3
textdistance                       4.2.1
thefuzz                            0.19.0
thinc                              8.0.13
threadpoolctl                      2.2.0
three-merge                        0.1.1
tinycss                            0.4
tldextract                         3.1.2
tokenizers                         0.13.2
toml                               0.10.2
tomli                              1.2.2
tomotopy                           0.12.2
toolz                              0.11.1
torch                              1.13.1
torchaudio                         0.10.1+cpu
torchmetrics                       0.8.1
torchserve                         0.6.0
torchvision                        0.11.2
tornado                            6.1
tqdm                               4.62.3
traitlets                          5.1.0
transformers                       4.26.1
trio                               0.19.0
trio-websocket                     0.9.2
typed-ast                          1.4.3
typer                              0.4.0
typing_extensions                  4.2.0
ujson                              4.0.2
umap-learn                         0.5.2
undetected-chromedriver            3.1.7
unicodecsv                         0.14.1
Unidecode                          1.2.0
urllib3                            1.26.12
urllib3-secure-extra               0.1.0
vaderSentiment                     3.3.2
w3lib                              2.0.1
wandb                              0.13.2
wasabi                             0.9.0
watchdog                           2.1.3
wcwidth                            0.2.5
webencodings                       0.5.1
websockets                         10.4
Werkzeug                           2.2.3
wheel                              0.37.0
whichcraft                         0.6.1
widgetsnbextension                 3.5.1
Wikipedia-API                      0.5.4
wordcloud                          1.8.1
wordhoard                          1.5.1
wrapt                              1.12.1
wsproto                            1.0.0
wurlitzer                          2.1.1
xlrd                               2.0.1
XlsxWriter                         3.0.1
xlwt                               1.3.0
xmltodict                          0.12.0
xvfbwrapper                        0.2.9
xxhash                             2.0.2
yake                               0.4.8
yapf                               0.31.0
yarg                               0.1.9
yarl                               1.7.2
zict                               2.0.0
zipp                               3.6.0
zope.event                         4.5.0
zope.interface                     5.4.0
zstandard                          0.19.0

My cpu

Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   39 bits physical, 48 bits virtual
CPU(s):                          8
On-line CPU(s) list:             0-7
Thread(s) per core:              2
Core(s) per socket:              4
Socket(s):                       1
NUMA node(s):                    1
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           158
Model name:                      Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz
Stepping:                        9
CPU MHz:                         3600.000
CPU max MHz:                     4200.0000
CPU min MHz:                     800.0000
BogoMIPS:                        7200.00
Virtualization:                  VT-x
L1d cache:                       128 KiB
L1i cache:                       128 KiB
L2 cache:                        1 MiB
L3 cache:                        8 MiB
NUMA node0 CPU(s):               0-7
Vulnerability Itlb multihit:     KVM: Mitigation: VMX disabled
Vulnerability L1tf:              Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Vulnerability Mds:               Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown:          Mitigation; PTI
Vulnerability Mmio stale data:   Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed:          Mitigation; IBRS
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds:             Mitigation; Microcode
Vulnerability Tsx async abort:   Mitigation; TSX disabled
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts a
                                 cpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch
                                 _perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq 
                                 dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_
                                 2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowp
                                 refetch cpuid_fault invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexprior
                                 ity ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed a
                                 dx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hw
                                 p hwp_notify hwp_act_window hwp_epp md_clear flush_l1d arch_capabilities
plvckn commented 1 year ago

UPDATE fixed last error by using a different version of a package: optimum-intel-1.7.0.dev0 however, running same code sample now returns a different error:

KeyError                                  Traceback (most recent call last)
<ipython-input-1-47896b3f0ea0> in <module>
      1 from optimum.intel.neural_compressor.quantization import INCModelForSeq2SeqLM
----> 2 int8_model = INCModelForSeq2SeqLM.from_pretrained(
      3     'Intel/distilbart-cnn-12-6-int8-dynamic',
      4 )

~/anaconda3/envs/nlp/lib/python3.9/site-packages/optimum/intel/neural_compressor/quantization.py in from_pretrained(cls, model_name_or_path, q_model_name, **kwargs)
    551 
    552         if "best_configure" in state_dict and state_dict["best_configure"] is not None:
--> 553             model = load(state_dict_path, model)
    554 
    555         return model.eval()

~/anaconda3/envs/nlp/lib/python3.9/site-packages/neural_compressor/utils/pytorch.py in load(checkpoint_dir, model, history_cfg, **kwargs)
    370         _set_activation_scale_zeropoint(q_model, history_cfg)
    371     else:
--> 372         q_model.load_state_dict(stat_dict)
    373     util.get_embedding_contiguous(q_model)
    374     return q_model

~/anaconda3/envs/nlp/lib/python3.9/site-packages/torch/nn/modules/module.py in load_state_dict(self, state_dict, strict)
   1655                 )
   1656 
-> 1657         load(self, state_dict)
   1658         del load
   1659 

~/anaconda3/envs/nlp/lib/python3.9/site-packages/torch/nn/modules/module.py in load(module, local_state_dict, prefix)
   1643                     child_prefix = prefix + name + '.'
   1644                     child_state_dict = {k: v for k, v in local_state_dict.items() if k.startswith(child_prefix)}
-> 1645                     load(child, child_state_dict, child_prefix)
   1646 
   1647             # Note that the hook can modify missing_keys and unexpected_keys.

~/anaconda3/envs/nlp/lib/python3.9/site-packages/torch/nn/modules/module.py in load(module, local_state_dict, prefix)
   1643                     child_prefix = prefix + name + '.'
   1644                     child_state_dict = {k: v for k, v in local_state_dict.items() if k.startswith(child_prefix)}
-> 1645                     load(child, child_state_dict, child_prefix)
   1646 
   1647             # Note that the hook can modify missing_keys and unexpected_keys.

~/anaconda3/envs/nlp/lib/python3.9/site-packages/torch/nn/modules/module.py in load(module, local_state_dict, prefix)
   1643                     child_prefix = prefix + name + '.'
   1644                     child_state_dict = {k: v for k, v in local_state_dict.items() if k.startswith(child_prefix)}
-> 1645                     load(child, child_state_dict, child_prefix)
   1646 
   1647             # Note that the hook can modify missing_keys and unexpected_keys.

~/anaconda3/envs/nlp/lib/python3.9/site-packages/torch/nn/modules/module.py in load(module, local_state_dict, prefix)
   1637         def load(module, local_state_dict, prefix=''):
   1638             local_metadata = {} if metadata is None else metadata.get(prefix[:-1], {})
-> 1639             module._load_from_state_dict(
   1640                 local_state_dict, prefix, local_metadata, True, missing_keys, unexpected_keys, error_msgs)
   1641             for name, child in module._modules.items():

~/anaconda3/envs/nlp/lib/python3.9/site-packages/torch/ao/nn/quantized/modules/embedding_ops.py in _load_from_state_dict(self, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs)
     55     def _load_from_state_dict(self, state_dict, prefix, local_metadata, strict,
     56                               missing_keys, unexpected_keys, error_msgs):
---> 57         self.dtype = state_dict[prefix + 'dtype']
     58         state_dict.pop(prefix + 'dtype')
     59 

KeyError: 'model.shared._packed_params.dtype'
xin3he commented 1 year ago

Hi, @plvckn I checked it and the root cause it's that transformers disabled sharing embedding, please try transformers <= v4.23.0. Thanks, I will add a note in the model card later.

plvckn commented 1 year ago

Downgrading to transformers v4.23 has solved this issue, thanks @xin3he