DeepSeek-R1-Distillチュートリアル
DeepSeek-R1-Distillモデルを使用したPythonでの推論
Section titled “DeepSeek-R1-Distillモデルを使用したPythonでの推論”1. 前提条件:仮想環境を作成し、ONNX Runtime GenAIをインストールする
Section titled “1. 前提条件:仮想環境を作成し、ONNX Runtime GenAIをインストールする”# CPU用にonnxruntime-genai、olive、および依存関係をインストールするpython -m venv .venv && source .venv/bin/activatepip install requests numpy --pre onnxruntime-genai olive-ai# CUDA GPU用にonnxruntime-genai、olive、および依存関係をインストールするpython -m venv .venv && source .venv/bin/activatepip install requests numpy --pre onnxruntime-genai-cuda "olive-ai[gpu]"2. モデルの取得
Section titled “2. モデルの取得”モデルを選択し、ONNXに変換します。多くのLLMが動作するため、他のモデルも試してみてください。
# 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# 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を使用して直接ダウンロードします。
# huggingface cliを使用してモデルを直接ダウンロードするhuggingface-cli download onnxruntime/DeepSeek-R1-Distill-ONNX --include 'deepseek-r1-distill-qwen-1.5B/*' --local-dir .3. デバイスでモデルを試してみましょう!
Section titled “3. デバイスでモデルを試してみましょう!”# CPUチャット推論。huggingfaceからモデルを取得した場合は、モデルディレクトリ(-m)を適宜調整してくださいcurl -o https://raw.githubusercontent.com/microsoft/onnxruntime-genai/refs/heads/main/examples/python/model-chat.pypython model-chat.py -m deepseek-r1-distill-qwen-1.5B/model -e cpu --chat_template "<|begin of sentence|><|User|>{input}<|Assistant|>"# オンデバイスGPUチャット推論。Nvidia GPUを搭載したデバイスで動作します。huggingfaceからモデルを取得した場合は、モデルディレクトリ(-m)を適宜調整してくださいcurl -o https://raw.githubusercontent.com/microsoft/onnxruntime-genai/refs/heads/main/examples/python/model-chat.pypython model-chat.py -m deepseek-r1-distill-qwen-1.5B/model -e cuda --chat_template "<|begin of sentence|><|User|>{input}<|Assistant|>"