0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Elasticsearch に Hugging Face からモデルをインポートする (ElasticsearchとElandのversionに注意が必要)

Last updated at Posted at 2025-02-03

やりたいこと

Hugging Face にある機械学習モデル cl-tohoku/bert-base-japanese-v2 を Elasticsearch にインポートする。

Add trained model の Manual Download タブにコマンドレベルで手順が書いてあります。しかし、これらコマンドはサンプルであり、このまま実行してもうまくいかないところがありましたので、うまくいった方法を記録します。

image.png

環境

今回、Elasticsearch環境はIBM Cloudの Databases for Elasticsearch を使っています。
2025年1月現在、Elasticsearchのバージョンは 8.15.0 です。

作業開始時点での作業マシン(Ubuntu on Windows)のPython仮想環境状態:

$ pip list
Package    Version
---------- -------
pip        23.0.1
setuptools 65.5.0

手順

Elasticsearch と Eland の compatibility に注意が必要です。
必ず、Elasticsearch と同じバージョンを指定して Eland をインストールするのがよさそうです。

Elastic doc に次の記述がありました。

Make sure your Eland major version matches the major version of your Elasticsearch cluster.

さらに、以下の記述もありました。

  • Supports Elasticsearch 8+ clusters, recommended 8.16 or later for all features to work. If you are using the NLP with PyTorch feature make sure your Eland minor version matches the minor version of your Elasticsearch cluster. For all other features it is sufficient for the major versions to match.

バージョン指定なしでインストールすると current version(2025年1月現在、8.17.0)がインストールされます。当初、current version の Eland 8.17.0 を使ってみたところ、次の eland_import_hub_model 実行時にエラーになりました。(このレベルの Eland が使っている PyTorch が 8.15.2 以降としか compatibility がないというエラーメッセージ)そこで、指示どおり Elasticsearch と同じバージョンの Eland をインストールします。

さらに、今回の用途の場合はPytorch extrasも一緒にインストールする必要があるので、次のコマンドで eland をインストールします。github: elastic/eland

If using Eland to upload NLP models to Elasticsearch install the PyTorch extras:
$ python -m pip install 'eland[pytorch]'

$ pip install eland==8.15.0 'eland[pytorch]'

上の pip install 完了後の状態:

$ pip list
Package                  Version
------------------------ -----------
accelerate               1.3.0
certifi                  2025.1.31
charset-normalizer       3.4.1
click                    8.1.8
contourpy                1.3.1
cycler                   0.12.1
eland                    8.15.0
elastic-transport        8.17.0
elasticsearch            8.17.1
filelock                 3.17.0
fonttools                4.55.8
fsspec                   2024.12.0
huggingface-hub          0.28.1
idna                     3.10
Jinja2                   3.1.5
joblib                   1.4.2
kiwisolver               1.4.8
MarkupSafe               3.0.2
matplotlib               3.10.0
mpmath                   1.3.0
networkx                 3.4.2
nltk                     3.9.1
numpy                    1.26.4
nvidia-cublas-cu12       12.1.3.1
nvidia-cuda-cupti-cu12   12.1.105
nvidia-cuda-nvrtc-cu12   12.1.105
nvidia-cuda-runtime-cu12 12.1.105
nvidia-cudnn-cu12        8.9.2.26
nvidia-cufft-cu12        11.0.2.54
nvidia-curand-cu12       10.3.2.106
nvidia-cusolver-cu12     11.4.5.107
nvidia-cusparse-cu12     12.1.0.106
nvidia-nccl-cu12         2.18.1
nvidia-nvjitlink-cu12    12.8.61
nvidia-nvtx-cu12         12.1.105
packaging                24.2
pandas                   1.5.3
pillow                   11.1.0
pip                      23.0.1
psutil                   6.1.1
pyparsing                3.2.1
python-dateutil          2.9.0.post0
pytz                     2025.1
PyYAML                   6.0.2
regex                    2024.11.6
requests                 2.32.3
safetensors              0.5.2
scikit-learn             1.6.1
scipy                    1.15.1
sentence-transformers    2.3.1
sentencepiece            0.2.0
setuptools               65.5.0
six                      1.17.0
sympy                    1.13.3
threadpoolctl            3.5.0
tokenizers               0.15.2
torch                    2.1.2
tqdm                     4.67.1
transformers             4.35.2
triton                   2.1.0
typing_extensions        4.12.2
urllib3                  2.3.0

さらに、日本語モデルを利用する際に必要となるものをインストールします。

$ pip install fugashi ipadic unidic_lite

上の pip install 完了後の状態:

$ pip list
Package                  Version
------------------------ -----------
accelerate               1.3.0
certifi                  2025.1.31
charset-normalizer       3.4.1
click                    8.1.8
contourpy                1.3.1
cycler                   0.12.1
eland                    8.15.0
elastic-transport        8.17.0
elasticsearch            8.17.1
filelock                 3.17.0
fonttools                4.55.8
fsspec                   2024.12.0
fugashi                  1.4.0
huggingface-hub          0.28.1
idna                     3.10
ipadic                   1.0.0
Jinja2                   3.1.5
joblib                   1.4.2
kiwisolver               1.4.8
MarkupSafe               3.0.2
matplotlib               3.10.0
mpmath                   1.3.0
networkx                 3.4.2
nltk                     3.9.1
numpy                    1.26.4
nvidia-cublas-cu12       12.1.3.1
nvidia-cuda-cupti-cu12   12.1.105
nvidia-cuda-nvrtc-cu12   12.1.105
nvidia-cuda-runtime-cu12 12.1.105
nvidia-cudnn-cu12        8.9.2.26
nvidia-cufft-cu12        11.0.2.54
nvidia-curand-cu12       10.3.2.106
nvidia-cusolver-cu12     11.4.5.107
nvidia-cusparse-cu12     12.1.0.106
nvidia-nccl-cu12         2.18.1
nvidia-nvjitlink-cu12    12.8.61
nvidia-nvtx-cu12         12.1.105
packaging                24.2
pandas                   1.5.3
pillow                   11.1.0
pip                      23.0.1
psutil                   6.1.1
pyparsing                3.2.1
python-dateutil          2.9.0.post0
pytz                     2025.1
PyYAML                   6.0.2
regex                    2024.11.6
requests                 2.32.3
safetensors              0.5.2
scikit-learn             1.6.1
scipy                    1.15.1
sentence-transformers    2.3.1
sentencepiece            0.2.0
setuptools               65.5.0
six                      1.17.0
sympy                    1.13.3
threadpoolctl            3.5.0
tokenizers               0.15.2
torch                    2.1.2
tqdm                     4.67.1
transformers             4.35.2
triton                   2.1.0
typing_extensions        4.12.2
unidic-lite              1.0.8
urllib3                  2.3.0

この後、次のコマンドで cl-tohoku/bert-base-japanese-v2 モデルをインポートします。
* 環境により、オプションの指定は異なります。
* userid 等の文字列は(""で囲んだりせず)そのままでよいようです。

eland_import_hub_model \
--url https://elasticsearch_server_url:port/ \
-u username -p password \
--hub-model-id cl-tohoku/bert-base-japanese-v2 \
--task-type text_embedding --insecure

以上の操作により、 cl-tohoku/bert-base-japanese-v2 モデルが Elasticsearch の Trained Models に追加されました。

image.png

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?