Skip to main content

A library to augment LLMs with dynamic, trainable routers for efficient fine-tuning.

Project description

Sparrow 🐦: Dynamic Routers for Efficient LLM Fine-Tuning

PyPI version License: MIT

English | فارسی


English

Sparrow is a lightweight Python library that seamlessly integrates with Hugging Face transformers to make your Large Language Models (LLMs) smarter and more efficient. It augments pre-trained models with dynamic, trainable routers that learn to create sparse computational paths through the network.

This allows for highly parameter-efficient fine-tuning (PEFT) where you freeze the original model's weights and only train the tiny routers, saving immense computational resources and preventing catastrophic forgetting.

Key Features

  • Plug-and-Play: Convert a standard Hugging Face model into a dynamic, sparse model with a single line of code.
  • Parameter-Efficient: Fine-tune only a tiny fraction of the total parameters (often < 0.1%).
  • Autonomous Routing: Routers learn automatically how many and which neurons to activate, guided by a sparsity-regularized loss.
  • Deep Integration: Comes with a custom SparrowTrainer for seamless integration with the Hugging Face ecosystem.
  • Insightful Reporting: Includes utilities to generate reports on neuron activation and model sparsity.

Installation

pip install sparrow-lib

Quickstart

Here's how to fine-tune a BERT model by only training the routers:

from transformers import BertForSequenceClassification, AutoTokenizer, TrainingArguments
from sparrow import SparrowConfig, add_routers_to_model, SparrowTrainer, get_sparsity_report

# 1. Define your Sparrow configuration
sparrow_config = SparrowConfig(sparsity_lambda=0.01)

# 2. Load a pre-trained model and augment it with routers
model = BertForSequenceClassification.from_pretrained('bert-base-uncased')
model = add_routers_to_model(model, config=sparrow_config)

# 3. Freeze original model weights (only routers will be trainable)
for name, param in model.named_parameters():
    if 'router' not in name:
        param.requires_grad = False

# ... Load your dataset ...

# 4. Use the custom SparrowTrainer which handles the sparsity loss
training_args = TrainingArguments(output_dir="test_trainer", num_train_epochs=1)

trainer = SparrowTrainer(
    model=model,
    args=training_args,
    sparrow_config=sparrow_config,
    train_dataset=your_train_dataset,
    eval_dataset=your_eval_dataset
)

# 5. Start training!
trainer.train()

# 6. Get a report on how sparse your model became
trainer.evaluate()
report = get_sparsity_report(model)
print("\n--- Sparsity Report ---")
print(report)

فارسی

Sparrow 🐦 یک کتابخانه سبک پایتون است که به راحتی با transformers هاگینگ فیس ادغام می‌شود تا مدل‌های زبان بزرگ (LLM) شما را هوشمندتر و کارآمدتر کند. این کتابخانه مدل‌های از پیش آموزش‌دیده را با مسیریاب‌های دینامیک و قابل آموزش تقویت می‌کند که یاد می‌گیرند مسیرهای محاسباتی پراکنده (sparse) در شبکه ایجاد کنند.

این روش امکان تنظیم دقیق بهینه از نظر پارامتر (PEFT) را فراهم می‌کند، به طوری که شما وزن‌های مدل اصلی را منجمد کرده و فقط مسیریاب‌های کوچک را آموزش می‌دهید. این کار باعث صرفه‌جویی عظیم در منابع محاسباتی شده و از فراموشی فاجعه‌بار (catastrophic forgetting) جلوگیری می‌کند.

قابلیت‌های کلیدی

  • استفاده آسان: یک مدل استاندارد هاگینگ فیس را با یک خط کد به یک مدل دینامیک و پراکنده تبدیل کنید.
  • بهینه از نظر پارامتر: فقط بخش بسیار کوچکی از پارامترهای کل مدل (معمولاً کمتر از ۰.۱٪) را آموزش دهید.
  • مسیریابی خودکار: مسیریاب‌ها به طور خودکار و با هدایت یک تابع زیان مبتنی بر پراکندگی، یاد می‌گیرند که چه تعداد و کدام نورون‌ها را فعال کنند.
  • ادغام عمیق: شامل یک SparrowTrainer سفارشی برای ادغام کامل با اکوسیستم هاگینگ فیس.
  • گزارش‌گیری دقیق: شامل ابزارهایی برای تولید گزارش از میزان فعالیت نورون‌ها و پراکندگی مدل.

نصب

pip install sparrow-lib

شروع سریع

مثال بالا نحوه تنظیم دقیق مدل BERT را فقط با آموزش مسیریاب‌ها نشان می‌دهد.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sparrow_lib-2.0.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sparrow_lib-2.0.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file sparrow_lib-2.0.0.tar.gz.

File metadata

  • Download URL: sparrow_lib-2.0.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for sparrow_lib-2.0.0.tar.gz
Algorithm Hash digest
SHA256 4636f45f4440632d97cf2c3df7a5cb664d0c0ca134cb226dc12e99fa5a142c9e
MD5 e25b0b30e897e77451e20da6868c29a8
BLAKE2b-256 8c92322c0c178cb74ec62aab09ce7e74cc2d0e03cb9aaf06dbc06a0a2957b3c0

See more details on using hashes here.

File details

Details for the file sparrow_lib-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: sparrow_lib-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for sparrow_lib-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 84d269496e17fd8c317002dbe81a12c5395b9d09ffcadccc397178d9004d2b5b
MD5 9843712e8b7099b7a2dfb22dd9969ec9
BLAKE2b-256 3ead1c614b9bbecdd3f19d836a25105265b9ba646fe0d3d968b2d0a1f1fad08d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page