Error in MovingAverageQuantizer with per_axis=True due to missing parameters in _add_range_weights
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 55/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 停滞
- 技术栈
- python, tensorflow
调研方向
从 tensorflow_model_optimization/python/core/quantization/keras/quantizers.py 中的 MovingAverageQuantizer.build 和 _add_range_weights 开始,然后检查 quant_ops.py 中 _FakeQuantWithMinMaxVars 的断言。复现 per_axis=True 示例,并验证 min/max 变量具有预期的列表形状,同时不破坏 per_axis=False 路径。
由索引模型根据 Issue 内容生成。
描述
Describe the bug
Using the MovingAverageQuantizer with parameter per_axis set to True results in this error:
tensorflow_model_optimization\python\core\quantization\keras\quant_ops.py", line 335, in _FakeQuantWithMinMaxVars *
assert len(min_var.get_shape()) == 1
It's caused by this helper function _add_range_weights called by the build function of the MovingAverageQuantizer where the per_axis and tensor_shape parameters are not passed on resulting in only initializing a scalar for the min/max variables. Which later fails the assert.
System information
TensorFlow version: 2.12.0 (binary)
TensorFlow Model Optimization version: 0.7.4 (binary)
Python version: 3.9.16
Describe the expected behavior
There should be a list of values in the min/max variables.
Describe the current behavior
Throws and error because of failed assert in _FakeQuantWithMinMaxVars
Code to reproduce the issue
import keras
import tensorflow_model_optimization as tfmot
class CustomConv2DQuantizeConfig(tfmot.quantization.keras.QuantizeConfig):
def get_weights_and_quantizers(self, layer):
return [(layer.kernel, tfmot.quantization.keras.quantizers.MovingAverageQuantizer(num_bits=8, per_axis=True, symmetric=True, narrow_range=True))]
def get_activations_and_quantizers(self, layer):
return [(layer.activation, tfmot.quantization.keras.quantizers.MovingAverageQuantizer(num_bits=8, per_axis=False, symmetric=False, narrow_range=False))]
def set_quantize_weights(self, layer, quantize_weights):
layer.kernel = quantize_weights[0]
def set_quantize_activations(self, layer, quantize_activations):
layer.activation = quantize_activations[0]
def get_output_quantizers(self, layer):
return []
def get_config(self):
return {}
annotated_model = keras.Sequential([
keras.Input(shape=(32,32,3)),
tfmot.quantization.keras.quantize_annotate_layer(keras.layers.Conv2D(32, kernel_size=(3, 3)), CustomConv2DQuantizeConfig())
])
with tfmot.quantization.keras.quantize_scope(
{'CustomConv2DQuantizeConfig': CustomConv2DQuantizeConfig}):
quant_aware_model = tfmot.quantization.keras.quantize_apply(annotated_model)
Additional context
Changing the build function of MovingAverageQuantizer to pass the mentioned parameters like:
def build(self, tensor_shape, name, layer):
return self._add_range_weights(layer, name, self.per_axis, tensor_shape)
fixes the issue and results in the expected behavior.
- 主要语言
- Python
- 星标
- 1.6k
- 派生
- 349
- 平均合并
- 3 天 2 小时
- 30 天内合并 PR
- 1
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
tensorflow/model-optimization 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 74/100
tensorflow/model-optimization#1301 · 1 条评论 ·
-
bug
难度 4/5 3-5 天 新手友好度 48/100
tensorflow/model-optimization#1272 ·
-
bug
难度 3/5 1-2 天 新手友好度 50/100
tensorflow/model-optimization#1270 · 2 条评论 ·
-
bug
难度 2/5 1-3 小时 新手友好度 48/100
tensorflow/model-optimization#1241 ·
-
bug
难度 4/5 3-5 天 新手友好度 35/100
tensorflow/model-optimization#1182 · 1 条评论 ·
查看 tensorflow/model-optimization 的全部 Issue
相似的 Issue
-
agent-ready documentation needs-triage
难度 1/5 1-3 小时 新手友好度 88/100
-
documentation
难度 1/5 1 小时以内 新手友好度 91/100
-
workflow-status page template still says reusable workflows are "triggered only by workflow_call:" 未关闭
难度 1/5 1 小时以内 新手友好度 92/100
-
instance instance add
难度 1/5 1 小时以内 新手友好度 72/100
searxng/searx-instances#939 · 1 条评论 ·
-
area-deployment area-integrations triage:bot-seen
难度 2/5 半天 新手友好度 86/100