3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

複数のLLMモデルファイルを保存しておいて、いちいちHuggingFaceからDLしなくて済むようにする

Last updated at Posted at 2023-11-23

環境

  • Macbook Pro(M3チップ)
  • Visual Studio Code
  • pyenv
  • poetry

概要

複数のLLMをいちいちHuggingfaceからDLしなくて済むように、modelsディレクトリにモデルファイルとトークナイザのファイルを置きつつ、作業用のworkディレクトリからモデルファイルを呼び出せるようにしたい

├── models
│   ├── model1
│   │   ├── model
│   │   │   ├── config.json
│   │   │   ├── generation_config.json
│   │   │   └── model.safetensors
│   │   └── tokenizer
│   │       ├── special_tokens_map.json
│   │       ├── tokenizer.config.json
│   │       └── tokenizer.json
│   └── model2
│       ├── model
│       │   ├── config.json
│       │   ├── generation_config.json
│       │   └── model.safetensors
│       └── tokenizer
│           ├── special_tokens_map.json
│           ├── tokenizer.json
│           └── tokenizer_config.json
├── poetry.lock
├── pyproject.toml
└── work
    └── work.py

手順

ライブラリをインポート

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import transformers

huggingfaceからモデル、トークナイザをダウンロード(以下はTinyllamaの例)

tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-step-50K-105b")
model = AutoModelForCausalLM.from_pretrained("TinyLlama/TinyLlama-1.1B-step-50K-105b")

HuggingFaceから読み込んだモデルファイル、トークナイザのファイルを保存

dir_name = 'models'
model_name = 'tinyllama-1.1B'
tokenizer.save_pretrained(f'{dir_name}/{model_name}/tokenizer')
model.save_pretrained(f'{dir_name}/{model_name}/model')

呼び出す時は以下

tokenizer = AutoTokenizer.from_pretrained(f'{dir_name}/{model_name}/tokenizer')
model = AutoModelForCausalLM.from_pretrained(f'{dir_name}/{model_name}/model')

ちなみに推論はメモリ不足で出来ず…Colabで動かしてみようかな?

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?