First Conv2D layer in model cannot be pruned with structured sparsity (sparsity_m_by_n)
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 35/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- python, tensorflow
调研方向
首先,使用 TensorFlow Model Optimization 0.8.0 运行提供的双 Conv2D 复现,并检查 pruning wrapper 和 callback 更新逻辑,尤其是第一层的处理方式。完成的标准是:第一层和后续 Conv2D 层都达到所请求的 2:4、约 50% 的稀疏度,且不使用手动 workaround。
由索引模型根据 Issue 内容生成。
描述
Bug Description
When applying structured pruning with sparsity_m_by_n parameter to a model containing multiple Conv2D layers, the first Conv2D layer consistently fails to be pruned while identical subsequent Conv2D layers are pruned correctly.
Environment
TensorFlow version: model trained with 2.19/keras v3 but recreating the model with tf_keras and v2 compatibility and loading just the pre-trained weights
TensorFlow Model Optimization version: 0.8.0
Python version: 3.10
Operating System: Linux
Expected Behavior
All Conv2D layers with identical pruning configuration should be pruned equally, achieving the target sparsity with the specified structured pattern (e.g., 2:4 sparsity).
Actual Behavior
First Conv2D layer: 0% sparsity (no pruning applied)
Subsequent Conv2D layers: 50% sparsity with correct 2:4 structured pattern
Example Code
import tensorflow as tf
import tf_keras as keras
import tensorflow_model_optimization as tfmot
import numpy as np
print(f"TensorFlow version: {tf.__version__}")
print(f"TFMOT version: {tfmot.__version__}")
def reproduce_first_layer_pruning_bug():
"""Demonstrates that the first Conv2D layer in a model cannot be pruned."""
# Create a simple model with two identical Conv2D layers
inputs = keras.layers.Input(shape=(None, 128, 2))
x = keras.layers.Conv2D(16, (1, 8), strides=(1, 2), padding='same', name='conv2d_first')(inputs)
x = keras.layers.Conv2D(16, (1, 8), strides=(1, 2), padding='same', name='conv2d_second')(x)
model = keras.Model(inputs=inputs, outputs=x)
# Apply identical pruning to both layers
def apply_pruning(layer):
if isinstance(layer, keras.layers.Conv2D):
return tfmot.sparsity.keras.prune_low_magnitude(
layer,
pruning_schedule=tfmot.sparsity.keras.ConstantSparsity(0.5, 0),
sparsity_m_by_n=(2, 4)
)
return layer
pruned_model = keras.models.clone_model(model, clone_function=apply_pruning)
pruned_model.compile(optimizer='adam', loss='mse')
# Train with dummy data
dummy_x = np.random.random((32, 100, 128, 2))
dummy_y = np.random.random((32, 100, 25, 16))
pruned_model.fit(dummy_x, dummy_y, epochs=3,
callbacks=[tfmot.sparsity.keras.UpdatePruningStep()],
verbose=0)
# Check sparsity of both layers
stripped_model = tfmot.sparsity.keras.strip_pruning(pruned_model)
first_layer = stripped_model.get_layer('conv2d_first')
second_layer = stripped_model.get_layer('conv2d_second')
first_sparsity = (first_layer.get_weights()[0] == 0).sum() / first_layer.get_weights()[0].size
second_sparsity = (second_layer.get_weights()[0] == 0).sum() / second_layer.get_weights()[0].size
print(f"First Conv2D layer sparsity: {first_sparsity:.2%}")
print(f"Second Conv2D layer sparsity: {second_sparsity:.2%}")
print(f"Bug reproduced: {first_sparsity == 0 and second_sparsity > 0}")
return first_sparsity == 0 and second_sparsity > 0
if __name__ == "__main__":
bug_reproduced = reproduce_first_layer_pruning_bug()
if bug_reproduced:
print("Bug is confirmed: First Conv2D layer cannot be pruned")
else:
print("No bug detected")
Additional Investigation
- The issue persists even when the first layer is tested in complete isolation (single-layer model)
- Different pruning schedules don't resolve the issue (ConstantSparsity, PolynomialDecay)
- The layer weights are normal (no zeros, good distribution, unique values)
- The pruning wrapper is correctly applied (visible in model summary and layer config)
- Manual weight copying doesn't help
- Recreating the layer with identical configuration doesn't help
Debugging Output Example
First layer config shows correct pruning wrapper:
'pruning_schedule': {'class_name': 'ConstantSparsity', 'config': {'target_sparsity': 0.5, 'begin_step': 0, 'end_step': -1, 'frequency': 100}}
Training monitoring shows:
Epoch 0 END: First Conv2D sparsity = 0.00%, Second Conv2D sparsity = 50.00%
Epoch 1 END: First Conv2D sparsity = 0.00%, Second Conv2D sparsity = 50.00%
Potential Root Cause
Maybe related to how the pruning mechanism handles the first layer in the model graph, possibly in the pruning callback or wrapper update logic.
Dummy Workaround
def apply_manual_2_4_pruning_to_first_layer(model, layer_name='conv2d'):
layer = model.get_layer(layer_name)
weights = layer.get_weights()
kernel = weights[0]
flat_weights = kernel.flatten()
# Apply 2:4 structured pruning: keep 2 largest in each group of 4
for i in range(0, len(flat_weights) - 3, 4):
group = flat_weights[i:i+4]
indices = np.argsort(np.abs(group))
flat_weights[indices[0]] = 0 # Zero smallest
flat_weights[indices[1]] = 0 # Zero second smallest
weights[0] = flat_weights.reshape(kernel.shape)
layer.set_weights(weights)
sparsity = (weights[0] == 0).sum() / weights[0].size
print(f"Applied manual 2:4 pruning to {layer_name}. Sparsity: {sparsity:.2%}")
The latter solution achieves a sparsity of ~50% (as intended).
Any help here is greatly appreciated.
- 主要语言
- Python
- 星标
- 1.6k
- 派生
- 349
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 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 ·
-
难度 4/5 3-5 天 新手友好度 35/100
tensorflow/model-optimization#1181 · 1 条评论 ·
查看 tensorflow/model-optimization 的全部 Issue
相似的 Issue
-
docs pydanty:is-working
难度 2/5 1-3 小时 新手友好度 75/100
pydantic/pydantic-ai#8863 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
run-llama/llama_index#23278 ·
维护者通常 2 天内回复
-
documentation from-review-extraction github-actions priority: low severity:nit
难度 1/5 1 小时以内 新手友好度 92/100
LearningCircuit/local-deep-research#6946 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
oracle/langchain-oracle#323 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
tenstorrent/tt-metal#58057 · 1 条评论 ·
维护者通常 1 天内回复