मुख्य सामग्री पर जाएँ
Skillpedia
मशीन लर्निंग14k · #11241

model-pruning

यह क्या करती है

यह स्किल model pruning समझाती है, weights हटाकर भाषा मॉडल को छोटा और तेज़ बनाने का एक तरीका। यह उन डेवलपर्स के लिए है जो सीमित हार्डवेयर पर मॉडल डिप्लॉय करना चाहते हैं।

कब इस्तेमाल करें

  • ✓जब आप सटीकता में थोड़ी कमी के साथ मॉडल का आकार लगभग 40 से 60 प्रतिशत घटाना चाहते हैं।
  • ✓जब आपको मॉडल को मोबाइल या edge हार्डवेयर पर डिप्लॉय करना है।
  • ✓जब आप मॉडल को बिना दोबारा ट्रेनिंग के एक ही चरण में prune करना चाहते हैं।

Skill का लिंक

https://github.com/Orchestra-Research/AI-Research-SKILLs/blob/main/19-emerging-techniques/model-pruning/SKILL.md

इसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।

कैसे इस्तेमाल करें

सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।

पेस्ट करने के लिए तैयार मैसेज

इस skill से मेरी मदद करो: https://github.com/Orchestra-Research/AI-Research-SKILLs/blob/main/19-emerging-techniques/model-pruning/SKILL.md इस लिंक की SKILL.md फ़ाइल पढ़ो और मेरे काम में उसके निर्देशों का पालन करो।
क्या आप चाहते हैं कि आपका AI ऐसी skills खुद ढूँढे?मुफ़्त Skillpedia MCP इंस्टॉल करें: बस एक लाइन, और हर काम से पहले यह 15,000+ skills में खोजता है।1 मिनट में इंस्टॉल करें →

AI से क्या माँगें: उदाहरण

“इस 7B मॉडल के आधे weights prune कीजिए, ताकि वह मेरे सर्वर पर तेज़ चले, और उसे दोबारा ट्रेन न करना पड़े।”

हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।

यह कैसे काम करती है

Wanda जैसे तरीके हर weight को उसके आकार और इनपुट activations के आधार पर स्कोर करते हैं, फिर सबसे कम स्कोर वाले weights को शून्य कर देते हैं। sparsity बिना पैटर्न की हो सकती है, या ऐसे पैटर्न में जो कुछ हार्डवेयर पर तेज़ चलते हैं।

तकनीकी सामग्री (मूल SKILL.md)

लेखक का मूल टेक्स्ट, अंग्रेज़ी में, सीधे GitHub से।

Model Pruning: Compressing LLMs

When to Use This Skill

Use Model Pruning when you need to:

  • Reduce model size by 40-60% with <1% accuracy loss
  • Accelerate inference using hardware-friendly sparsity (2-4× speedup)
  • Deploy on constrained hardware (mobile, edge devices)
  • Compress without retraining using one-shot methods
  • Enable efficient serving with reduced memory footprint

Key Techniques: Wanda (weights × activations), SparseGPT (second-order), structured pruning, N:M sparsity

Papers: Wanda ICLR 2024 (arXiv 2306.11695), SparseGPT (arXiv 2301.00774)

Installation

# 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

Quick Start

Wanda Pruning (One-Shot, No Retraining)

Source: 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 (Second-Order Pruning)

Source: 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 Structured Pruning (Hardware Accelerator)

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))

    # Resha
अक्सर पूछे जाने वाले सवाल

Wanda क्या है?

स्किल Wanda को एक one-shot pruning तरीका बताती है, जो weights और activations इस्तेमाल करता है और दोबारा ट्रेनिंग नहीं माँगता।

कौन सा calibration डेटा चाहिए?

स्किल activation आँकड़े जुटाने के लिए एक छोटे डेटासेट का ज़िक्र करती है।

कितनी गति की उम्मीद करूँ?

स्किल कहती है कि हार्डवेयर-अनुकूल sparsity से लगभग 2 से 4 गुना गति मिल सकती है।

जानकारी

लेखक का मूल विवरण

Reduce LLM size and accelerate inference using pruning techniques like Wanda and SparseGPT. Use when compressing models without retraining, achieving 50% sparsity with minimal accuracy loss, or enabling faster inference on hardware accelerators. Covers unstructured pruning, structured pruning, N:M sparsity, magnitude pruning, and one-shot methods.

repo: Orchestra-Research/AI-Research-SKILLs

लोकप्रियता

GitHub स्टार
14k
रैंकिंग में स्थान
#11241
लाइसेंस
MIT
अपडेट की गई
10/10/2026

क्या यह skill काम की है?

0

लॉग इन करें वोट करने के लिए।

कमेंट

  • …

इस repository की और skills

autoresearch

14k

यह आपके एजेंट को साहित्य समीक्षा से प्रयोग और लेखन तक पूरे AI रिसर्च प्रोजेक्ट में मार्गदर्शन देती है, परीक्षण और समीक्षा के दोहराने वाले चक्र के साथ।

AI और एजेंटएजेंट

prompt-guard

14k

यह आपके एजेंट को Meta के छोटे classifier से टेक्स्ट को AI ऐप तक पहुँचने से पहले prompt injection और jailbreak कोशिशों के लिए जाँचने देती है।

कोडिंगसिक्योरिटी

pytorch-fsdp2

14k

यह आपके एजेंट को दिखाती है कि ट्रेनिंग स्क्रिप्ट में PyTorch FSDP2 कैसे जोड़ें, ताकि बड़े मॉडल GPU पर बँटें और सही तरीके से सेव हों।

डेटामशीन लर्निंग

ml-training-recipes

14k

यह आपके एजेंट को न्यूरल नेटवर्क के लिए परखे हुए ट्रेनिंग तरीके देती है, जिनमें मॉडल चुनाव, optimizers, learning rates और loss spikes ठीक करना शामिल है।

डेटामशीन लर्निंग

मिलती-जुलती skills