用於裝置端訓練的構建

先決條件

  • Python 3.x
  • CMake

訓練階段的構建說明

  1. 克隆倉庫

     git clone --recursive https://github.com/Microsoft/onnxruntime.git
     cd onnxruntime
    
  2. 構建用於 裝置端訓練 的 ONNX Runtime

    a. 對於 Windows

     .\build.bat --config RelWithDebInfo --cmake_generator "Visual Studio 17 2022" --build_shared_lib --parallel --enable_training_apis
    

    b. 對於 Linux

     ./build.sh --config RelWithDebInfo --build_shared_lib --parallel --enable_training_apis
    

    c. 對於 Android

    請參閱 Android 構建說明 並新增 --enable_training_apis 構建標誌。

    d. 對於 macOS

    請參閱 macOS 推理構建說明 並新增 --enable_training_apis 構建標誌。

    e. 對於 iOS

    請參閱 iOS 構建說明 並新增 --enable_training_apis 構建標誌。

    f. 對於 Web

    請參閱 Web 構建說明

注意

  • 要構建 C# 繫結,請在上述構建命令中新增 --build_nuget 標誌。

  • 要構建 Python wheel
    • 在上述構建命令中新增 --build_wheel 標誌。
    • 使用 python -m pip install build/Linux/RelWithDebInfo/dist/*.whl 安裝 wheel
  • config 標誌可以是 DebugRelWithDebInfoReleaseMinSizeRel 之一。請使用適合您用例的配置。

  • --enable_training_apis 標誌可以與 --minimal_build 標誌結合使用。

  • 生成訓練工件的離線階段只能透過 Python 進行(使用 --build_wheel 標誌)。

  • 上述構建命令僅針對 CPU 執行提供程式進行構建。要針對 CUDA 執行提供程式進行構建,請新增以下標誌:
    • --use_cuda
    • --cuda_home {您的 CUDA 主目錄路徑,例如 /usr/local/cuda/}
    • --cudnn_home {您的 cuDNN 主目錄路徑,例如 /usr/local/cuda/}
    • --cuda_version={版本號,例如 11.8}

用於大型模型訓練的構建

目錄

Linux

./build.sh --config RelWithDebInfo --build_shared_lib --parallel --enable_training

GPU / CUDA

先決條件

預設的 NVIDIA GPU 構建需要系統上安裝 CUDA 執行時庫

構建說明

  1. 檢出此程式碼倉庫:

     git clone https://github.com/microsoft/onnxruntime
     cd onnxruntime
    
  2. 設定環境變數:請根據您的構建機器上的位置調整路徑
     export CUDA_HOME=<location for CUDA libs> # e.g. /usr/local/cuda
     export CUDNN_HOME=<location for cuDNN libs> # e.g. /usr/local/cuda
     export CUDACXX=<location for NVCC> #e.g. /usr/local/cuda/bin/nvcc
    
  3. 建立 ONNX Runtime Python wheel

    ./build.sh --config=RelWithDebInfo --enable_training --build_wheel --use_cuda --cuda_home {location of cuda libs eg. /usr/local/cuda/} --cudnn_home {location of cudnn libs eg./usr/local/cuda/} --cuda_version={version for eg. 11.8}
    
  4. 為 ONNX Runtime 訓練安裝位於 ./build/Linux/RelWithDebInfo/dist 中的 .whl 檔案。

     python -m pip install build/Linux/RelWithDebInfo/dist/*.whl
    

就是這樣!構建完成後,您就可以在您的專案中開始使用 ONNX Runtime 庫和可執行檔案了。請注意,這些步驟是通用的,可能需要根據您的特定環境和要求進行調整。有關更多資訊,您可以在 ONNX Runtime GitHub 社群尋求幫助。

GPU / ROCm

先決條件

預設的 AMD GPU 構建需要系統上安裝 ROCm 軟體工具包

構建說明

  1. 檢出此程式碼倉庫:

     git clone https://github.com/microsoft/onnxruntime
     cd onnxruntime
    
  2. 建立 ONNX Runtime Python wheel

    ./build.sh --config Release --enable_training --build_wheel --parallel --skip_tests --use_rocm --rocm_home /opt/rocm
    
  3. 為 ONNX Runtime 訓練安裝位於 ./build/Linux/RelWithDebInfo/dist 中的 .whl 檔案。

     python -m pip install build/Linux/RelWithDebInfo/dist/*.whl
    

DNNL 和 MKLML

構建說明

Linux

./build.sh --enable_training --use_dnnl

Windows

.\build.bat --enable_training --use_dnnl

新增 --build_wheel 以構建 ONNX Runtime wheel。

這將在 build/Linux/RelWithDebInfo/dist 中為 ONNX Runtime 訓練生成一個 .whl 檔案。