转换器错误:操作 'Cumsum' 不支持量化。该操作没有注册的量化内核。
ConverterError: Quantization not supported for op 'Cumsum'. Op has no registered quantized kernel.
ID: tensorflow/tflite-converter-unsupported-op
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| TensorFlow 2.7.0 | active | — | — | — |
| TFLite 2.7.0 | active | — | — | — |
根因分析
在启用量化的 TFLite 转换过程中,某个操作(例如 Cumsum、Gather 或 TopK)没有量化的内核实现,导致转换器失败。
English
During TFLite conversion with quantization enabled, an operation (e.g., Cumsum, Gather, or TopK) does not have a quantized kernel implementation, causing the converter to fail.
官方文档
https://www.tensorflow.org/lite/performance/post_training_quantization#full_integer_quantization解决方案
-
使用选择性量化:仅对支持的操作应用量化,通过设置 `converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]`,并对不支持的操作回退到浮点。或者使用 `converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]` 允许选择 TensorFlow 操作。
-
如果不受支持的操作是关键性的,则在不量化的情况下转换模型(设置 optimizations=[]),然后使用避免该操作的代表性数据集应用训练后量化,或使用带有回退的全整数量化。
-
重写模型以避免不受支持的操作。例如,用循环或使用支持的操作(如 Add 和 Scan)的不同算法替换 tf.math.cumsum。
无效尝试
常见但无效的做法:
-
60% 失败
This may avoid the quantization error but results in a non-optimized model, losing the benefits of quantization (size reduction, speed).
-
80% 失败
Manual replacement is error-prone and may change model behavior; also, the custom op may not be supported in TFLite either.
-
50% 失败
Even in the latest versions, some ops still lack quantized kernels; upgrading alone does not guarantee support.