0
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?

新しいGPUを購入した際の環境構築ガイド:CUDA、PyTorch、ONNXRuntimeの互換性を確保する

Posted at

新しいGPUを購入した際の環境構築ガイド:CUDA、PyTorch、ONNXRuntimeの互換性を確保する

新しいNVIDIA GPUを購入した後、機械学習環境を構築する際に、CUDA、PyTorch、ONNXRuntimeなどの互換性の問題に直面することがよくあります。このガイドでは、これらのコンポーネント間の互換性を確保するための手順と確認事項を詳しく解説します。

目次

  1. 環境構築の基本原則
  2. ステップ1:GPUドライバのインストール
  3. ステップ2:互換性のある組み合わせの選択
  4. ステップ3:CUDA Toolkitのインストール(任意)
  5. ステップ4:PyTorchのインストール
  6. ステップ5:ONNXRuntimeのインストール
  7. ステップ6:環境の検証
  8. トラブルシューティング
  9. バージョン互換性表
  10. まとめ

環境構築の基本原則

新しいGPUで機械学習環境を構築する際の基本原則は以下の通りです:

  1. 正しい順序でインストールする:GPUドライバ → PyTorch → ONNXRuntime
  2. 互換性のあるバージョンを選ぶ:各コンポーネント間の互換性を事前に確認する
  3. 仮想環境を使用する:プロジェクトごとに分離された環境を作成する
  4. DLLの共有を理解する:ライブラリがどのようにCUDA/cuDNNのDLLを共有するかを把握する

ステップ1:GPUドライバのインストール

手順

  1. 最新のGPUドライバをNVIDIA公式サイトからダウンロード

    • GPUモデル(例:RTX 5090)を選択
    • オペレーティングシステムを選択
    • 「ドライバタイプ」は通常「Standard」を選択
  2. インストール前に古いドライバをクリーンアンインストール

  3. ダウンロードしたドライバをインストール

  4. インストール完了後、再起動

確認事項

  • GPUドライバが正常にインストールされたか確認

    # Windowsの場合
    nvidia-smi
    
    # Linuxの場合
    nvidia-smi
    
  • 出力にGPUモデルドライババージョンが表示されることを確認

ステップ2:互換性のある組み合わせの選択

環境構築の前に、互換性のある各コンポーネントのバージョンを決定します。

確認事項

  1. GPUアーキテクチャを確認

    • RTX 30シリーズ:Ampere
    • RTX 40シリーズ:Ada Lovelace
    • RTX 50シリーズ:Blackwell
  2. サポートされているCUDAバージョンを確認

    • 新しいGPU(RTX 40シリーズ以降): CUDA 12.x が推奨
    • RTX 30シリーズ: CUDA 11.x または 12.x
  3. PyTorchとONNXRuntimeの互換性表を参照

    • 最新版はPyTorchウェブサイトとONNXRuntimeのドキュメントで確認
    • CUDA、cuDNN、PyTorch、ONNXRuntimeの互換性を確認

新しいRTX GPU (40/50シリーズ) での推奨組み合わせ例

コンポーネント 推奨バージョン
NVIDIA ドライバ 最新バージョン
CUDA 12.x (12.6以上推奨)
cuDNN 9.x
PyTorch 2.4.0以上
ONNXRuntime 1.18.0以上 (1.20.0以上推奨)

ステップ3:CUDA Toolkitのインストール(任意)

: 最近のPyTorchとONNXRuntimeは必要なCUDAライブラリを同梱しているため、多くの場合、このステップは省略可能です。

手順(必要な場合のみ)

  1. NVIDIA CUDA Toolkitから適切なバージョンをダウンロード

  2. インストール時のオプション

    • Visual Studioの統合: 必要に応じて選択
    • サンプル: 任意
    • ドライバ: すでに最新版をインストール済みなら「ドライバコンポーネント」のチェックを外す
  3. インストール完了後、環境変数を確認

    • CUDA_PATH が設定されていることを確認
    • 必要に応じて Path 環境変数に CUDA ディレクトリが追加されていることを確認

確認事項

# CUDAバージョンの確認
nvcc --version

ステップ4:PyTorchのインストール

手順

  1. 仮想環境を作成

    # venvを使用する場合
    python -m venv gpu_env
    
    # Windowsの場合
    gpu_env\Scripts\activate
    
    # Linux/macOSの場合
    source gpu_env/bin/activate
    
  2. PyTorch公式サイトでインストールコマンドを生成

    • PyTorch公式サイトにアクセス
    • オプションを選択:
      • PyTorch Build: Stable または Preview
      • OS: Windows/Linux
      • Package: Pip
      • Language: Python
      • Compute Platform: 適切なCUDAバージョン (例:CUDA 12.8)
  3. 生成されたコマンドでインストール

    # 例:CUDA 12.8用の安定版PyTorch
    pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128
    
    # または最新のプレビュー版
    pip3 install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cu128
    

確認事項

# PyTorchが正しくインストールされ、GPUが認識されているか確認
import torch
print(f"PyTorch version: {torch.__version__}")
print(f"CUDA available: {torch.cuda.is_available()}")
print(f"CUDA version: {torch.version.cuda}")
print(f"GPU count: {torch.cuda.device_count()}")
print(f"GPU name: {torch.cuda.get_device_name(0)}")

ステップ5:ONNXRuntimeのインストール

手順

  1. ONNXとONNXRuntimeをインストール

    # CUDA対応のONNXRuntime-GPUをインストール(追加のCUDA/cuDNN依存関係も含む)
    pip install onnx
    pip install onnxruntime-gpu[cuda,cudnn]>=1.20.0
    
  2. 特定のバージョンが必要な場合

    pip install onnxruntime-gpu==1.20.1
    

確認事項

# ONNXRuntimeが正しくインストールされ、CUDAプロバイダーが利用可能か確認
import onnxruntime
print(f"ONNXRuntime version: {onnxruntime.__version__}")
print(f"Available providers: {onnxruntime.get_available_providers()}")

# CUDAプロバイダーが含まれていることを確認
assert "CUDAExecutionProvider" in onnxruntime.get_available_providers()

ステップ6:環境の検証

手順

以下のテストスクリプトを作成して実行することで、環境が正しく構築されたか検証できます。

# test_environment.py
import os
import sys
import torch
import onnxruntime

def print_section(title):
    print("\n" + "="*50)
    print(f" {title}")
    print("="*50)

# システム情報
print_section("System Information")
print(f"Python version: {sys.version}")
print(f"Operating system: {os.name} - {sys.platform}")

# PyTorch情報
print_section("PyTorch Information")
print(f"PyTorch version: {torch.__version__}")
print(f"CUDA available: {torch.cuda.is_available()}")
if torch.cuda.is_available():
    print(f"CUDA version: {torch.version.cuda}")
    print(f"GPU count: {torch.cuda.device_count()}")
    for i in range(torch.cuda.device_count()):
        print(f"GPU {i}: {torch.cuda.get_device_name(i)}")

# ONNXRuntime情報
print_section("ONNXRuntime Information")
print(f"ONNXRuntime version: {onnxruntime.__version__}")
providers = onnxruntime.get_available_providers()
print(f"Available providers: {providers}")

if "CUDAExecutionProvider" in providers:
    print("✓ CUDA provider is available")
else:
    print("✗ CUDA provider is NOT available")

# 簡単なテスト(PyTorch)
print_section("PyTorch Test")
try:
    # 簡単なTensorをGPUに転送
    x = torch.ones(1, 3).cuda()
    print("✓ Successfully created GPU tensor")
except Exception as e:
    print(f"✗ Failed to create GPU tensor: {e}")

# 簡単なテスト(ONNXRuntime)
print_section("ONNXRuntime Test")
if "CUDAExecutionProvider" in providers:
    try:
        # セッションを作成し、CUDAプロバイダーを使用
        options = onnxruntime.SessionOptions()
        options.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL
        
        # ダミーセッションを作成
        print("✓ ONNXRuntime CUDA provider is ready to use")
    except Exception as e:
        print(f"✗ ONNXRuntime CUDA provider test failed: {e}")

print_section("Conclusion")
if torch.cuda.is_available() and "CUDAExecutionProvider" in providers:
    print("✓ Environment setup is complete and working correctly")
else:
    print("✗ Environment setup has issues that need to be resolved")
python test_environment.py

トラブルシューティング

DLLロードエラー (Error 126)

症状: onnxruntime_providers_cuda.dllが見つからないエラー

解決策:

  1. PyTorchをONNXRuntimeより先にインポートする

    import torch  # 先にインポート
    import onnxruntime
    
  2. ONNXRuntime 1.21.0以上を使用する場合はpreload_dlls()関数を使用

    import onnxruntime
    onnxruntime.preload_dlls()
    
  3. CUDA DLLパスを明示的に追加

    import os
    import torch
    
    # PyTorchのDLLパスを環境変数に追加
    torch_path = os.path.dirname(torch.__file__)
    dll_path = os.path.join(torch_path, 'lib')
    if os.path.exists(dll_path):
        os.environ['PATH'] = dll_path + os.pathsep + os.environ.get('PATH', '')
    

バージョン不一致エラー

症状: The CPU feature <feature> is required but not present on your machine

解決策:

  1. ONNXRuntimeのバージョンを変更

    pip uninstall -y onnxruntime-gpu
    pip install onnxruntime-gpu==<互換性のあるバージョン>
    
  2. 正しいCUDAアーキテクチャと互換性のあるONNXRuntimeバージョンを選択

CUDA Outof Memoryエラー

症状: CUDA out of memory

解決策:

  1. バッチサイズを減らす
  2. モデルの精度を下げる (例:FP32からFP16に変更)
  3. 小さなモデルを使用する

バージョン互換性表

ONNXRuntime と CUDA/cuDNN の互換性 (2025年4月時点)

ONNXRuntime CUDA cuDNN 備考
1.20.x 12.x 9.x PyTorch 2.4.0以上と互換 (CUDA 12.x)
1.20.x 11.8 8.x PyTorch 2.3.1以下と互換 (CUDA 11.8)
1.19.x 12.x 9.x PyTorch 2.4.0以上と互換 (CUDA 12.x)
1.18.x 12.x 8.x -
1.17.x 12.x 8.x -
1.16.x - 1.15.x 11.8 8.x CUDA 11.6-11.8, cuDNN 8.2-8.9と互換

PyTorch と CUDA/cuDNN の互換性

PyTorch CUDA cuDNN 備考
2.4.x 12.8 9.x RTX 40/50シリーズに最適
2.3.x 12.1 8.9 -
2.2.x 12.1 8.9 -
2.1.x 12.1 8.9 -
2.0.x 11.8 8.7 レガシーGPUに適している

まとめ

新しいNVIDIA GPUで機械学習環境を構築する際の重要ポイント:

  1. 正しい順序でインストール:GPUドライバ → PyTorch → ONNXRuntime
  2. 最新のRTX GPUでは CUDA 12.x を使用
  3. PyTorchとONNXRuntimeの組み合わせを慎重に選択
  4. DLLロード問題を解決するには:
    • PyTorchを先にインポート
    • ONNXRuntimeのpreload_dlls()関数を使用
    • 環境変数でDLLパスを明示的に設定
  5. 環境検証スクリプトを利用して設定を確認

これらの手順と確認事項に従うことで、新しいGPUでの開発環境構築における互換性の問題を回避し、スムーズに機械学習プロジェクトを開始できます。

0
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
0
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?