コンテンツにスキップ

DeepSeek-R1-Distillチュートリアル

DeepSeek-R1-Distillモデルを使用したPythonでの推論

Section titled “DeepSeek-R1-Distillモデルを使用したPythonでの推論”

1. 前提条件:仮想環境を作成し、ONNX Runtime GenAIをインストールする

Section titled “1. 前提条件:仮想環境を作成し、ONNX Runtime GenAIをインストールする”
Terminal window
# CPU用にonnxruntime-genai、olive、および依存関係をインストールする
python -m venv .venv && source .venv/bin/activate
pip install requests numpy --pre onnxruntime-genai olive-ai
Terminal window
# CUDA GPU用にonnxruntime-genai、olive、および依存関係をインストールする
python -m venv .venv && source .venv/bin/activate
pip install requests numpy --pre onnxruntime-genai-cuda "olive-ai[gpu]"

モデルを選択し、ONNXに変換します。多くのLLMが動作するため、他のモデルも試してみてください。

Terminal window
# Olive auto-optを使用してhuggingfaceモデルを取得し、CPU用に最適化し、RTNを使用してINT4に量子化する
olive auto-opt --model_name_or_path deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B --output_path ./deepseek-r1-distill-qwen-1.5B --device cpu --provider CPUExecutionProvider --precision int4 --use_model_builder --log_level 1
Terminal window
# Olive auto-optを使用してhuggingfaceモデルを取得し、CUDA GPU用に最適化し、RTNを使用してINT4に量子化する
olive auto-opt --model_name_or_path deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B --output_path ./deepseek-r1-distill-qwen-1.5B --device gpu --provider CUDAExecutionProvider --precision int4 --use_model_builder --log_level 1

または、Huggingface CLIを使用して直接ダウンロードします。

Terminal window
# huggingface cliを使用してモデルを直接ダウンロードする
huggingface-cli download onnxruntime/DeepSeek-R1-Distill-ONNX --include 'deepseek-r1-distill-qwen-1.5B/*' --local-dir .

3. デバイスでモデルを試してみましょう!

Section titled “3. デバイスでモデルを試してみましょう!”
Terminal window
# CPUチャット推論。huggingfaceからモデルを取得した場合は、モデルディレクトリ(-m)を適宜調整してください
curl -o https://raw.githubusercontent.com/microsoft/onnxruntime-genai/refs/heads/main/examples/python/model-chat.py
python model-chat.py -m deepseek-r1-distill-qwen-1.5B/model -e cpu --chat_template "<|begin of sentence|><|User|>{input}<|Assistant|>"
Terminal window
# オンデバイスGPUチャット推論。Nvidia GPUを搭載したデバイスで動作します。huggingfaceからモデルを取得した場合は、モデルディレクトリ(-m)を適宜調整してください
curl -o https://raw.githubusercontent.com/microsoft/onnxruntime-genai/refs/heads/main/examples/python/model-chat.py
python model-chat.py -m deepseek-r1-distill-qwen-1.5B/model -e cuda --chat_template "<|begin of sentence|><|User|>{input}<|Assistant|>"