LiteRT 运算符版本

本文档介绍了 LiteRT 的 op 版本控制架构。通过操作版本控制,开发者可以向现有操作添加新功能和参数。此外,它还保证以下几点:

  • 向后兼容性:新的 LiteRT 实现应能处理旧模型文件。
  • 向前兼容性:旧版 LiteRT 实现应能处理新版转换器生成的新模型文件,前提是不使用任何新功能。
  • 向前不兼容性检测:如果旧版 LiteRT 实现读取包含不受支持的新版 op 的新模型,则应报告错误。

示例:在深度可分离卷积中添加空洞

本文档的其余部分将通过展示如何向深度可分离卷积运算添加扩张参数,来介绍 TFLite 中的 op 版本控制。

您无需了解膨胀即可理解本文档。请注意:

  • 将添加 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。

修改 lite/tools/versioning/op_version.cc 中运算符的 GetBuiltinOperatorVersion 函数,方法是将新版本添加到 DepthwiseConv2D 的情形中:

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 函数中,检查每个节点是否支持委托代码中的版本。

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 操作,也需要这样做,以便委托在获取更高版本的操作时检测到不兼容情况。