本文档介绍了 LiteRT 的操作版本控制架构。借助操作版本控制,开发者可以向现有操作中添加新功能和参数。此外,它还保证以下几点:
- 向后兼容性:新的 LiteRT 实现应处理旧版模型文件。
- 向前兼容性:只要不使用新功能,旧版 LiteRT 实现应能处理新版转换器生成的新模型文件。
- 向前兼容性检测:如果旧版 LiteRT 实现读取包含不受支持的操作的新版本的新模型,则应报告错误。
示例:将膨胀添加到深度卷积中
本文档的其余部分将通过展示如何向深度卷积运算添加膨胀参数,介绍 TFLite 中的运算版本控制。
无需了解膨胀即可理解本文档。请注意:
- 将添加 2 个新的整数参数:
dilation_width_factor
和dilation_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());
to:
AddBuiltin(BuiltinOperator_DEPTHWISE_CONV_2D, Register_DEPTHWISE_CONV_2D(),
/* min_version = */ 1,
/* max_version = */ 2);
更改 TFLite 运算版本
下一步是让 TFLite 填充执行此运算所需的最低版本。在此示例中,这意味着:
- 当膨胀系数均为 1 时,填充 version=1。
- 否则,请填充 version=2。
通过将新版本添加到 DepthwiseConv2D
的用例中,修改 lite/tools/versioning/op_version.cc
中运算符的 GetBuiltinOperatorVersion
函数:
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, ®istration));
if (registration->version > kMaxVersion) {
// Reject the node if the version isn't supported.
}
即使委托仅支持版本 1 操作,也必须执行此操作,以便委托在获取更高版本的操作时能够检测不兼容性。