gpt4 book ai didi

tensorflow - 当为量化模型分配张量时,TFlite解释器会引发RuntimeError。涉及scale_diff和output_scale的断言失败

转载 作者:行者123 更新时间:2023-12-03 09:05:06 25 4
gpt4 key购买 nike

亲爱的开发人员和NN发烧友,我已经对模型进行了量化(8位训练后量化),并且我试图使用tflite解释器对所得模型进行推断。
在某些情况下,解释器可以正常运行,并且我可以按预期对量化模型进行推断,其输出足够接近原始模型。因此,我的设置似乎是正确的。
但是,根据具体的量化模型,我经常偶然遇到以下RuntimeError。

Traceback (most recent call last):
File ".\quantize_model.py", line 328, in <module>
interpreter.allocate_tensors()
File "---path removed---tf-nightly_py37\lib\site-packages\tensorflow\lite\python\interpreter.py", line 243, in allocate_tensors
return self._interpreter.AllocateTensors()
RuntimeError: tensorflow/lite/kernels/kernel_util.cc:154 scale_diff / output_scale <= 0.02 was not true.Node number 26 (FULLY_CONNECTED) failed to prepare.
由于误差似乎与偏差的大小有关,因此我使用bias_regularizer重新训练了原始模型。但是,错误仍然存​​在。
您对如何避免此错误有任何建议?我应该以其他方式训练或设计模型吗?
是否可以抑制此错误并照常继续(即使精度降低)?
我已经使用Netron从量化的tflite模型中提取了有关“节点26”的一些细节:
*Node properties ->
type: FullyConnected, location:26. *Attributes asymmetric_quantization: false, fused_activation: NONE, keep_num_dims: false, weights_format: DEFAULT.
*Inputs ->
input. name: functional_3/tf_op_layer_Reshape/Reshape;StatefulPartitionedCall/functional_3/tf_op_layer_Reshape/Reshape
type: int8[1,34]
quantization: 0 ≤ 0.007448929361999035 * (q - -128) ≤ 1.8994770050048828
location: 98
weights. name: functional_3/tf_op_layer_MatMul_54/MatMul_54;StatefulPartitionedCall/functional_3/tf_op_layer_MatMul_54/MatMul_54
type: int8[34,34]
quantization: -0.3735211491584778 ≤ 0.002941111335530877 * q ≤ 0.1489555984735489
location: 42
[weights omitted to save space]
bias. name: functional_3/tf_op_layer_AddV2_93/AddV2_3/y;StatefulPartitionedCall/functional_3/tf_op_layer_AddV2_93/AddV2_3/y
type: int32[34]
quantization: 0.0002854724007192999 * q
location: 21
[13,-24,-19,-9,4,59,-18,9,14,-15,13,6,12,5,10,-2,-14,16,11,-1,12,7,-4,16,-8,6,-17,-7,9,-15,7,-29,5,3]
*outputs ->
output. name: functional_3/tf_op_layer_AddV2/AddV2;StatefulPartitionedCall/functional_3/tf_op_layer_AddV2/AddV2;functional_3/tf_op_layer_Reshape_99/Reshape_99/shape;StatefulPartitionedCall/functional_3/tf_op_layer_Reshape_99/Reshape_99/shape;functional_3/tf_op_layer_Reshape_1/Reshape_1;StatefulPartitionedCall/functional_3/tf_op_layer_Reshape_1/Reshape_1;functional_3/tf_op_layer_AddV2_93/AddV2_3/y;StatefulPartitionedCall/functional_3/tf_op_layer_AddV2_93/AddV2_3/y
type: int8[1,34]
quantization: -0.46506571769714355 ≤ 0.0031077787280082703 * (q - 22) ≤ 0.32741788029670715
location: 99

最佳答案

我找到了一种解决方法,其中涉及手动修改量化的tflite模型。
这是触发相关RuntimeError的文件(tensorflow/lite/kernels/kernel_utils.cc):

// TODO(ahentz): The following conditions must be guaranteed by the training pipeline.
...
const double scale_diff = std::abs(input_product_scale - bias_scale);
const double output_scale = static_cast<double>(output->params.scale);
TF_LITE_ENSURE(context, scale_diff / output_scale <= 0.02);
评论清楚表明,模型量化中的某些功能仍需要完成。失败条件与偏差的大小有关。我验证了我的量化模型不满足上述约束。为了手动修复量化模型,可以执行以下步骤:
  • 使用Netron打开量化模型并找到引起问题的节点(在我的情况下是节点26)
  • 提取该节点的偏差,输入和权重的比例。
  • 由于使用动态范围量化来表示偏差,因此表示不是唯一的。可以通过缩放偏置比例和偏置值(偏置零点为零,不需要更改)来创建另一种表示形式。找出一个系数k,使得abs(input_scale * weight_scale-bias_scale * k)<0.02。
  • 使用十六进制编辑器(例如,Ubuntu中的ghex)编辑tflite模型。将不正确的bias_scale替换为bias_scale * k。您还需要将偏差值替换为bias_values/k。 Bias_scale以32位ieee 754格式编码,即Little Endian (ieee-754 tool),而bias_values以int32格式编码Little Endian。
  • 保存已编辑的tflite模型,它现在应该通过所需的条件,可以与tflite解释器一起使用,并且该模型等效于修复之前的模型。

  • 当然,在纠正 tensorflow 量化器中的代码之前,此解决方案只是一个临时解决方法。

    关于tensorflow - 当为量化模型分配张量时,TFlite解释器会引发RuntimeError。涉及scale_diff和output_scale的断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62674711/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com