mcpField
M
model-pruning

model-pruning

github.com/kalarislabs其他7342 次安装4 天前更新
代码仓库

关于 model-pruning

Model Pruning 是介绍使用 Wanda、SparseGPT 和 N:M 剪枝等方法减少 LLM 权重的技能指南,面向为受限硬件或推理部署准备压缩模型的实践者,并提供方法选择和验证指导。

主要功能

  1. 01Wanda 和 SparseGPT 方法
  2. 02结构化与 N:M 稀疏化
  3. 03无需再训练的一次性剪枝
  4. 04逐层和迭代剪枝策略
  5. 05校准与结果验证指导

适用场景

  1. 01为受限硬件缩减模型体积
  2. 02准备稀疏模型用于推理
  3. 03为目标硬件选择剪枝模式
  4. 04通过验证比较不同剪枝方法

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/kalarislabs/research-agent-skills --skill model-pruning

安装后在 Claude Code 中输入/model-pruning确认已加载。

附带文件references/production-deployment.mdreferences/wanda.md

详细介绍

模型剪枝:压缩大模型

何时使用此技能

在以下情况下使用模型剪枝:

  • 将模型大小减少 40-60%,准确率损失低于 1%
  • 加速推理,利用硬件友好的稀疏性(提速 2-4 倍)
  • 在受限硬件上部署(移动设备、边缘设备)
  • 无需重新训练即可压缩,使用一次性方法
  • 实现高效服务,降低内存占用

关键技术:Wanda(权重 × 激活值)、SparseGPT(二阶)、结构化剪枝、N:M 稀疏性

论文:Wanda ICLR 2024(arXiv 2306.11695)、SparseGPT(arXiv 2301.00774)

安装

# Wanda implementation
git clone https://github.com/locuslab/wanda
cd wanda
pip install -r requirements.txt

# Optional: SparseGPT
git clone https://github.com/IST-DASLab/sparsegpt
cd sparsegpt
pip install -e .

# Dependencies
pip install torch transformers accelerate

快速入门

Wanda 剪枝(一次性,无需重新训练)

来源:ICLR 2024(arXiv 2306.11695)

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

# Load model
model = AutoModelForCausalLM.from_pretrained(
    "meta-llama/Llama-2-7b-hf",
    torch_dtype=torch.float16,
    device_map="cuda"
)
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf")

# Calibration data (small dataset for activation statistics)
calib_data = [
    "The quick brown fox jumps over the lazy dog.",
    "Machine learning is transforming the world.",
    "Artificial intelligence powers modern applications.",
]

# Wanda pruning function
def wanda_prune(model, calib_data, sparsity=0.5):
    """
    Wanda: Prune by weight magnitude × input activation.

    Args:
        sparsity: Fraction of weights to prune (0.5 = 50%)
    """
    # 1. Collect activation statistics
    activations = {}

    def hook_fn(name):
        def hook(module, input, output):
            # Store input activation norms
            activations[name] = input[0].detach().abs().mean(dim=0)
        return hook

    # Register hooks for all linear layers
    hooks = []
    for name, module in model.named_modules():
        if isinstance(module, torch.nn.Linear):
            hooks.append(module.register_forward_hook(hook_fn(name)))

    # Run calibration data
    model.eval()
    with torch.no_grad():
        for text in calib_data:
            inputs = tokenizer(text, return_tensors="pt").to(model.device)
            model(**inputs)

    # Remove hooks
    for hook in hooks:
        hook.remove()

    # 2. Prune weights based on |weight| × activation
    for name, module in model.named_modules():
        if isinstance(module, torch.nn.Linear) and name in activations:
            W = module.weight.data
            act = activations[name]

            # Compute importance: |weight| × activation
            importance = W.abs() * act.unsqueeze(0)

            # Flatten and find threshold
            threshold = torch.quantile(importance.flatten(), sparsity)

            # Create mask
            mask = importance >= threshold

            # Apply mask (prune)
            W *= mask.float()

    return model

# Apply Wanda pruning (50% sparsity, one-shot, no retraining)
pruned_model = wanda_prune(model, calib_data, sparsity=0.5)

# Save
pruned_model.save_pretrained("./llama-2-7b-wanda-50")
SparseGPT(二阶剪枝)

来源:arXiv 2301.00774

from sparsegpt import SparseGPT

# Load model
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-hf")

# Initialize SparseGPT
pruner = SparseGPT(model)

# Calibration data
calib_data = load_calibration_data()  # ~128 samples

# Prune (one-shot, layer-wise reconstruction)
pruned_model = pruner.prune(
    calib_data=calib_data,
    sparsity=0.5,           # 50% sparsity
    prunen=0,               # Unstructured (0) or N:M structured
    prunem=0,
    percdamp=0.01,          # Damping for Hessian inverse
)

# Results: Near-lossless pruning at 50% sparsity
N:M 结构化剪枝(硬件加速器)
def nm_prune(weight, n=2, m=4):
    """
    N:M pruning: Keep N weights per M consecutive weights.
    Example: 2:4 = keep 2 out of every 4 weights.

    Compatible with NVIDIA sparse tensor cores (2:4, 4:8).
    """
    # Reshape weight into groups of M
    shape = weight.shape
    weight_flat = weight.flatten()

    # Pad to multiple of M
    pad_size = (m - weight_flat.numel() % m) % m
    weight_padded = F.pad(weight_flat, (0, pad_size))

    # Reshape into (num_groups, m)
    weight_grouped = weight_padded.reshape(-1, m)

    # Find top-N in each group
    _, indices = torch.topk(weight_grouped.abs(), n, dim=-1)

    # Create mask
    mask = torch.zeros_like(weight_grouped)
    mask.scatter_(1, indices, 1.0)

    # Apply mask
    weight_pruned = weight_grouped * mask

    # Reshape back
    weight_pruned = weight_pruned.flatten()[:weight_flat.numel()]
    return weight_pruned.reshape(shape)

# Apply 2:4 sparsity (NVIDIA hardware)
for name, module in model.named_modules():
    if isinstance(module, torch.nn.Linear):
        module.weight.data = nm_prune(module.weight.data, n=2, m=4)

# 50% sparsity, 2× speedup on A100 with sparse tensor cores

核心概念

1. 剪枝标准

幅值剪枝(基线):

# Prune weights with smallest absolute values
importance = weight.abs()
threshold = torch.quantile(importance, sparsity)
mask = importance >= threshold

Wanda(权重 × 激活值):

# Importance = |weight| × input_activation
importance = weight.abs() * activation
# Better than magnitude alone (considers usage)

SparseGPT(二阶):

# Uses Hessian (second derivative) for importance
# More accurate but computationally expensive
importance = weight^2 / diag(Hessian)
2. 结构化与非结构化剪枝

非结构化(细粒度):

  • 剪除单个权重
  • 质量更高(准确率更好)
  • 无硬件加速(稀疏性不规则)

结构化(粗粒度):

  • 剪除整个神经元、注意力头或层
  • 质量较低(准确率损失更多)
  • 可实现硬件加速(稀疏性规则)

半结构化(N:M):

  • 兼得两者优点
  • 50% 稀疏性(2:4)→ NVIDIA GPU 上提速 2 倍
  • 准确率损失极小
3. 稀疏模式
# Unstructured (random)
# [1, 0, 1, 0, 1, 1, 0, 0]
# Pros: Flexible, high quality
# Cons: No speedup

# Structured (block)
# [1, 1, 0, 0, 1, 1, 0, 0]
# Pros: Hardware friendly
# Cons: More accuracy loss

# N:M (semi-structured)
# [1, 0, 1, 0] [1, 1, 0, 0]  (2:4 pattern)
# Pros: Hardware speedup + good quality
# Cons: Requires specific hardware (NVIDIA)

剪枝策略

策略 1:渐进式幅值剪枝
def gradual_prune(model, initial_sparsity=0.0, final_sparsity=0.5, num_steps=100):
    """Gradually increase sparsity during training."""
    for step in range(num_steps):
        # Current sparsity
        current_sparsity = initial_sparsity + (final_sparsity - initial_sparsity) * (step / num_steps)

        # Prune at current sparsity
        for module in model.modules():
            if isinstance(module, torch.nn.Linear):
                weight = module.weight.data
                threshold = torch.quantile(weight.abs().flatten(), current_sparsity)
                mask = weight.abs() >= threshold
                weight *= mask.float()

        # Train one step
        train_step(model)

    return model
策略 2:逐层剪枝
def layer_wise_prune(model, sparsity_per_layer):
    """Different sparsity for different layers."""
    # Early layers: Less pruning (more important)
    # Late layers: More pruning (less critical)

    sparsity_schedule = {
        "layer.0": 0.3,   # 30% sparsity
        "layer.1": 0.4,
        "layer.2": 0.5,
        "layer.3": 0.6,   # 60% sparsity
    }

    for name, module in model.named_modules():
        if isinstance(module, torch.nn.Linear):
            # Find layer index
            for layer_name, sparsity in sparsity_schedule.items():
                if layer_name in name:
                    # Prune at layer-specific sparsity
                    prune_layer(module, sparsity)
                    break

    return model
策略 3:迭代剪枝 + 微调
def iterative_prune_finetune(model, target_sparsity=0.5, iterations=5):
    """Prune gradually with fine-tuning between iterations."""
    current_sparsity = 0.0
    sparsity_increment = target_sparsity / iterations

    for i in range(iterations):
        # Increase sparsity
        current_sparsity += sparsity_increment

        # Prune
        prune_model(model, sparsity=current_sparsity)

        # Fine-tune (recover accuracy)
        fine_tune(model, epochs=2, lr=1e-5)

    return model

# Results: Better accuracy than one-shot at high sparsity

生产环境部署

详细信息、代码示例和参数表:references/production-deployment.md。在适用此步骤时阅读。

最佳实践

1. 稀疏度选择
# Conservative (safe)
sparsity = 0.3  # 30%, <0.5% loss

# Balanced (recommended)
sparsity = 0.5  # 50%, ~1% loss

# Aggressive (risky)
sparsity = 0.7  # 70%, 2-5% loss

# Extreme (model-dependent)
sparsity = 0.9  # 90%, significant degradation
2. 方法选择
# One-shot, no retraining → Wanda or SparseGPT
if no_retraining_budget:
    use_method = "wanda"  # Faster

# Best quality → SparseGPT
if need_best_quality:
    use_method = "sparsegpt"  # More accurate

# Hardware speedup → N:M structured
if need_speedup:
    use_method = "nm_prune"  # 2:4 or 4:8
3. 避免常见问题
# ❌ Bad: Pruning without calibration data
prune_random(model)  # No activation statistics

# ✅ Good: Use calibration data
prune_wanda(model, calib_data)

# ❌ Bad: Too high sparsity in one shot
prune(model, sparsity=0.9)  # Massive accuracy loss

# ✅ Good: Gradual or iterative
iterative_prune(model, target=0.9, steps=10)

性能对比

稀疏度为 50% 时的剪枝方法(LLaMA-7B):

方法准确率损失速度内存是否需要重新训练
幅值剪枝-2.5%1.0×-50%否
Wanda-0.8%1.0×-50%否
SparseGPT-0.4%1.0×-50%否
N:M(2:4)-1.0%2.0×-50%否
结构化剪枝-3.0%2.0×-50%否

来源:Wanda 论文(ICLR 2024)、SparseGPT 论文

资源

Agent 操作流程

  1. 检查环境。 确认 GPU 显存、模型检查点及许可证、框架版本和输入格式。
  2. 明确输入。 根据数据或用户确认格式、标识符和参数。任何会改变结果的值都应先询问,不要猜测。
  3. 先运行小规模版本。 先以低分辨率或较短长度对单个样本运行推理。
  4. 使用上述说明和参考资料执行完整任务。
  5. 验证结果。 通过定性检查和任务指标检查输出;确认生成内容符合许可证限制。
  6. 报告。 说明运行了什么(版本、命令、参数)、检查了什么,以及哪些方面仍不确定。
如果发生这种情况请这样做
大尺寸输入导致内存不足降低分辨率或序列长度、使用半精度,或对输入进行分块。
已安装版本中缺少这些说明提及的函数、标志或端点查阅已安装版本自身的文档(help()、--help、官方文档),进行调整并告知用户。切勿虚构 API。
必需的输入、标识符或参数不明确询问用户,或在运行前明确说明所采用的假设。

诚信规则

  • 切勿捏造结果、参数、标识符、引文或统计数据。如果某项内容无法运行或验证,请如实说明。
  • 商业使用或发布前,检查模型和数据集许可证。
  • 此处与版本相关的细节可能已过时:请对照已安装版本的官方文档确认。
  • 对于会产生费用、占用共享 GPU 或云配额、涉及个人或患者数据,或无法撤销的操作,请先征求同意。

相关技能

  • awq-quantization:针对 4-bit LLM 压缩的激活感知权重量化,可实现 3 倍加速且精度损失极小。
  • quantizing-models-bitsandbytes:将 LLM 量化为 8-bit 或 4-bit,可减少 50-75% 的内存占用且精度损失极小。
  • speculative-decoding:使用推测解码、Medusa 多头和前瞻解码技术加速 LLM 推理。

评论

支持 Markdown