LiteRT 運算子版本

本文說明 LiteRT 的運算元版本管理結構定義。作業版本控管可讓開發人員在現有作業中新增功能和參數。此外,這項服務還保證下列事項:

  • 回溯相容性:新的 LiteRT 實作內容應能處理舊模型檔案。
  • 向前相容性:舊版 LiteRT 實作項目應可處理新版轉換器產生的新模型檔案,前提是未使用任何新功能。
  • 向前不相容性偵測:如果舊版 LiteRT 實作項目讀取的新模型包含不支援的新版作業,應回報錯誤。

範例:在深度方向的捲積中加入擴張

本文的其餘內容將說明 TFLite 中的運算子版本管理,並示範如何將擴張參數新增至深度可分離的捲積運算。

您不需要瞭解擴張,也能看懂這份文件。請注意:

  • 系統會新增 2 個整數參數:dilation_width_factordilation_height_factor
  • 不支援擴張的舊版深層卷積核心,相當於將擴張係數設為 1。

變更 FlatBuffer 結構定義

如要在作業中新增參數,請變更 lite/schema/schema.fbs 中的選項表格。

舉例來說,深度可分離式捲積的選項資料表如下所示:

table DepthwiseConv2DOptions {
  padding:Padding;
  stride_w:int;
  stride_h:int;
  depth_multiplier:int;
  fused_activation_function:ActivationFunctionType;
}

新增參數時:

  • 新增註解,指出各版本支援的參數。
  • 如果新導入的參數會取得新加入參數的預設值,運作方式應與舊版導入作業完全相同。

新增參數後,表格會如下所示:

table DepthwiseConv2DOptions {
  // Parameters for DepthwiseConv version 1 or above.
  padding:Padding;
  stride_w:int;
  stride_h:int;
  depth_multiplier:int;
  fused_activation_function:ActivationFunctionType;
  // Parameters for DepthwiseConv version 2 or above.
  dilation_w_factor:int = 1;
  dilation_h_factor:int = 1;
}

您應為新結構定義重新產生 lite/schema/schema_generated.h 檔案。

變更 C 結構和核心實作項目

在 LiteRT 中,核心實作項目與 FlatBuffer 定義分離。核心會從 lite/c/builtin_op_data.h 中定義的 C 結構讀取參數。

原始的深度可分離式捲積參數如下:

typedef struct {
  TfLitePadding padding;
  int stride_width;
  int stride_height;
  int depth_multiplier;
  TfLiteFusedActivation activation;
} TfLiteDepthwiseConvParams;

與 FlatBuffer 結構定義相同,請新增註解,指出從哪個版本開始支援哪些參數。結果如下所示:

typedef struct {
  // Parameters for DepthwiseConv version 1 or above.
  TfLitePadding padding;
  int stride_width;
  int stride_height;
  int depth_multiplier;
  TfLiteFusedActivation activation;
  // Parameters for DepthwiseConv version 2 or above.
  int dilation_width_factor;
  int dilation_height_factor;
} TfLiteDepthwiseConvParams;

請一併變更核心實作,從 C 結構讀取新加入的參數。本文省略了詳細資料。

變更 FlatBuffer 讀取程式碼

讀取 FlatBuffer 並產生 C 結構的邏輯位於 lite/core/api/flatbuffer_conversions.cc 中。

更新檔案以處理新參數,如下所示:

TfLiteStatus ParseDepthwiseConv2D(const Operator* op,
                                  ErrorReporter* error_reporter,
                                  BuiltinDataAllocator* allocator,
                                  void** builtin_data) {
  CheckParsePointerParams(op, error_reporter, allocator, builtin_data);

  SafeBuiltinDataAllocator safe_allocator(allocator);

  std::unique_ptr<TfLiteDepthwiseConvParams,
                  SafeBuiltinDataAllocator::BuiltinDataDeleter>
      params = safe_allocator.Allocate<TfLiteDepthwiseConvParams>();
  TF_LITE_ENSURE(error_reporter, params != nullptr);

  const DepthwiseConv2DOptions* schema_params =
      op->builtin_options_as_DepthwiseConv2DOptions();

  if (schema_params != nullptr) {
    params->padding = ConvertPadding(schema_params->padding());
    params->stride_width = schema_params->stride_w();
    params->stride_height = schema_params->stride_h();
    params->depth_multiplier = schema_params->depth_multiplier();
    params->activation =
        ConvertActivation(schema_params->fused_activation_function());

    params->dilation_width_factor = schema_params->dilation_w_factor();
    params->dilation_height_factor = schema_params->dilation_h_factor();
  }

  *builtin_data = params.release();
  return kTfLiteOk;
}

這裡不需要檢查作業系統版本。如果新實作項目讀取缺少擴張係數的舊模型檔案,系統會使用 1 做為預設值,新核心的運作方式會與舊核心一致。

變更核心註冊

MutableOpResolver (定義於 lite/mutable_op_resolver.h) 提供幾個函式來註冊運算元核心。預設的最低和最高版本為 1:

void AddBuiltin(tflite::BuiltinOperator op, TfLiteRegistration* registration,
                int min_version = 1, int max_version = 1);
void AddCustom(const char* name, TfLiteRegistration* registration,
               int min_version = 1, int max_version = 1);

內建作業會註冊在 lite/kernels/register.cc。在這個範例中,我們實作了可處理 DepthwiseConv2D 第 1 版和第 2 版的新運算子核心,因此需要變更這一行:

AddBuiltin(BuiltinOperator_DEPTHWISE_CONV_2D, Register_DEPTHWISE_CONV_2D());

改為下列程式碼:

AddBuiltin(BuiltinOperator_DEPTHWISE_CONV_2D, Register_DEPTHWISE_CONV_2D(),
             /* min_version = */ 1,
             /* max_version = */ 2);

變更 TFLite 作業版本

下一步是讓 TFLite 填入執行作業所需的最低版本。在本例中,這表示:

  • 當所有擴張係數皆為 1 時,請填入 version=1。
  • 否則請填入 version=2。

修改 GetBuiltinOperatorVersion 函式,在 DepthwiseConv2D 的情況下新增運算子的新版本,藉此修改 lite/tools/versioning/op_version.cc

case BuiltinOperator_DEPTHWISE_CONV_2D:
  auto depthwise_conv_params =
      reinterpret_cast<TfLiteDepthwiseConvParams*>(op_sig.builtin_data);
  TFLITE_DCHECK(depthwise_conv_params != nullptr);
  if (depthwise_conv_params->dilation_width_factor != 1 ||
       depthwise_conv_params->dilation_height_factor != 1) {
    return 2;
  }
  return 1;

更新運算子版本對應

最後一個步驟是將新版本資訊新增至運算子版本對應。這是必要步驟,因為我們需要根據這個版本對應表,產生模型所需的最低執行階段版本。

如要這樣做,您需要在 lite/tools/versioning/runtime_version.cc 中新增對應項目。

在本範例中,您需要在 op_version_map 中新增下列項目:

{ {BuiltinOperator_DEPTHWISE_CONV_2D, 2}, %CURRENT_RUNTIME_VERSION%}

其中 %CURRENT_RUNTIME_VERSION% 對應於 release_version.h 中定義的目前執行階段版本。

委派實作

LiteRT 提供委派 API,可將作業委派給硬體後端。在委派的 Prepare 函式中,檢查 Delegation 程式碼中每個節點是否支援該版本。

const int kMaxVersion = 1;
TfLiteNode* node;
TfLiteRegistration* registration = nullptr;
TF_LITE_ENSURE_STATUS(context->GetNodeAndRegistration(context, node_index, &node, &registration));

if (registration->version > kMaxVersion) {
  // Reject the node if the version isn't supported.
}

即使委派作業僅支援第 1 版作業,也必須提供這項資訊,這樣委派作業才能在取得較新版本的作業時偵測到不相容問題。