向 ONNX Runtime 新增新的執行提供者

目錄

建立執行提供者

  1. 在 onnxruntime/core/providers 下建立資料夾
  2. 在 include/onnxruntime/core/providers 下建立資料夾,其名稱應與第一步相同。
  3. 建立一個新類,該類必須繼承自 IExecutionProvider。原始碼應放在“onnxruntime/core/providers/[你的提供者名稱]”中。
  4. 在 include/onnxruntime/core/providers/[你的提供者名稱] 下建立一個新的標頭檔案。該檔案應提供一個用於建立 OrtProviderFactoryInterface 的函式。你可以使用“include/onnxruntime/core/providers/cpu/cpu_provider_factory.h”作為模板。你不需要提供建立 MemoryInfo 的函式。
  5. 在“onnxruntime/core/providers/[你的提供者名稱]”下放置一個 symbols.txt 檔案。該檔案應包含所有將從你的提供者匯出的函式名稱。通常,只需一個用於建立提供者工廠的函式就足夠了。
  6. 在 onnxruntime_providers.cmake 中新增你的提供者。將其構建為靜態庫。
  7. 在 cmake/onnxruntime.cmake 的“target_link_libraries”函式呼叫中新增一行,將你的提供者放在其中。

示例

使用執行提供者

  1. 透過使用你在“symbols.txt”中匯出的 C 函式,為該提供者建立一個工廠
  2. 將提供者工廠放入會話選項
  3. 從該會話選項建立會話

示例

  OrtEnv* env;
  OrtInitialize(ORT_LOGGING_LEVEL_WARNING, "test", &env)
  OrtSessionOptions* session_option = OrtCreateSessionOptions();
  OrtProviderFactoryInterface** factory;
  OrtCreateCUDAExecutionProviderFactory(0, &factory);
  OrtSessionOptionsAppendExecutionProvider(session_option, factory);
  OrtReleaseObject(factory);
  OrtCreateSession(env, model_path, session_option, &session);

測試執行提供者

為了方便測試你的執行提供者,你可以透過在 onnxruntime/test/onnx/main.cc 檔案中為其新增一個新案例,並遵循其他現有提供者的模式,將其新增到 onnx_test_runner 命令中。

一旦設定好,你可以像這樣執行 onnx_test_runner

$ cd build/PLATFORM/CONFIGURATION
$ ./onnx_test_runner -e YOUR_BACKEND ./testdata/ort_minimal_e2e_test_data/
$ ./onnx_test_runner -e YOUR_BACKEND ./testdata/gemm_activation_fusion/