ローカルでsmollmを動かす

初めに

小さくて精度が高いモデルが出たので、触っていきます。TRL CLIを使ってチャットもできるみたいなのでこちらも触ってみます

github.com

開発環境

  • smollm[1e43f0dcd76261d4317cc2ee7258fe67d151d082]
  • Window
  • uv

セットアップ

まずはuvで環境を作ります

uv venv -p 3.11
.venv\Scripts\activate 

ライブラリをインストールします

uv pip install transformers
uv pip install torch --index-url https://download.pytorch.org/whl/cu121

デフォルトの質問

以下の公式のコードで動かしてみます

from transformers import AutoModelForCausalLM, AutoTokenizer
checkpoint = "HuggingFaceTB/SmolLM2-1.7B-Instruct"

device = "cuda" # for GPU usage or "cpu" for CPU usage
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
# for multiple GPUs install accelerate and do `model = AutoModelForCausalLM.from_pretrained(checkpoint, device_map="auto")`
model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)

messages = [{"role": "user", "content": "Write a 100-word article on 'Benefits of Open-Source in AI research"}]
input_text=tokenizer.apply_chat_template(messages, tokenize=False)
inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
outputs = model.generate(inputs, max_new_tokens=50, temperature=0.2, top_p=0.9, do_sample=True)
print(tokenizer.decode(outputs[0]))

結果は以下です

` to obtain reliable results.
<|im_start|>system
You are a helpful AI assistant named SmolLM, trained by Hugging Face<|im_end|>
<|im_start|>user
Write a 100-word article on 'Benefits of Open-Source in AI research<|im_end|>
<|im_start|>assistant
Open-source in AI research offers numerous benefits. Firstly, it fosters collaboration and community engagement, accelerating the development of AI technologies. Secondly, it promotes transparency and accountability, as open-source projects are subject to peer review and scrutiny

日本語での質問および回答は以下です

<|im_start|>system
You are a helpful AI assistant named SmolLM, trained by Hugging Face<|im_end|>
<|im_start|>user
まどマギで一番可愛いキャラクターは誰ですか?<|im_end|>
<|im_start|>assistant
私は日本語ではありませんが、この問題を解くためには、マギで一番可愛いキャラクターは誰ですか?という問題を解くためには、英語で書かれた文章が必要です。これは、「What is the most beautiful character in Magi?」という問題です。これは、アニメ、アニメゲーム、アニメアニメなどのゲームにおけるキャラクターという役割を担っている人物が、最も可愛いキャラクターとして見てもらえる

日本語はだめそうです