A simple yet powerful library to create and train custom LLMs with GGUF export
Project description
SwitLM - Simple Witty Language Model Training
SwitLM is a simple yet powerful Python library for creating and training custom Language Models with minimal code. Train your own LLM and export to GGUF format in just a few lines!
✨ Features
- 🚀 Simple API: Create and train LLMs with 3 lines of code
- 🎯 Multiple Model Sizes: From 50M to 3B parameters
- 🏗️ Modern Architecture: RoPE, RMSNorm, SwiGLU (LLaMA-style)
- 📊 Rich Dataset Support: WikiText, AG News, IMDB, SQuAD, TinyStories, and more
- 💾 GGUF Export: Direct export to GGUF format for llama.cpp
- ⚡ GPU Optimized: Memory-efficient training with gradient checkpointing
- 📈 W&B Integration: Built-in experiment tracking
- 🎨 Flexible Configuration: Easy customization of architecture and training
📦 Installation
pip install switlm
Or install from source:
git clone https://github.com/Avijit0001/switlm.git
cd switlm
pip install -e .
Requirements
- Python >= 3.8
- PyTorch >= 2.0.0
- transformers >= 4.30.0
- datasets >= 2.12.0
🚀 Quick Start
Basic Usage
from switlm import SwitLMTrainer
# Create and train a 1B parameter model
trainer = SwitLMTrainer(
n_parameters="1B",
dataset="wikitext",
num_layers=24
)
# Train the model
trainer.train()
# Save as GGUF (ready for llama.cpp)
trainer.save("my_model.gguf")
Generate Text
# Generate text with your trained model
output = trainer.generate(
"The future of artificial intelligence",
max_length=100,
temperature=0.7
)
print(output)
Multiple Datasets
# Train on multiple datasets sequentially
trainer = SwitLMTrainer(
n_parameters="500M",
dataset=["wikitext", "ag_news", "imdb"],
num_layers=16
)
trainer.train()
trainer.save("multi_dataset_model.gguf")
Custom Configuration
from switlm import SwitLMTrainer, ModelConfig, TrainingConfig
# Custom model architecture
model_config = ModelConfig(
n_parameters="custom",
num_layers=20,
hidden_size=1536,
num_heads=12,
intermediate_size=6144
)
# Custom training settings
training_config = TrainingConfig(
learning_rate=1e-4,
batch_size=4,
num_epochs=3,
use_wandb=True
)
trainer = SwitLMTrainer(
model_config=model_config,
training_config=training_config,
dataset="wikitext"
)
trainer.train()
trainer.save("custom_model.gguf")
🎯 Available Model Sizes
| Size | Parameters | Layers | Hidden Size | Heads | Use Case |
|---|---|---|---|---|---|
| 50M | ~50M | 8 | 512 | 8 | Quick experiments |
| 100M | ~100M | 12 | 768 | 12 | Small projects |
| 500M | ~500M | 16 | 1024 | 16 | Medium tasks |
| 1B | ~1B | 24 | 2048 | 16 | Serious applications |
| 3B | ~3B | 32 | 2560 | 32 | Production use |
📚 Supported Datasets
wikitext- Wikipedia articlesag_news- News classificationimdb- Movie reviewssquad- Question answeringtiny_stories- Short storiesopenwebtext- Web textc4- Colossal Clean Crawled Corpusbookcorpus- Bookspile- Diverse text corpus
🔧 Advanced Features
Load and Continue Training
# Load a previously trained model
trainer = SwitLMTrainer(n_parameters="1B")
trainer.load("my_model.pt")
# Continue training on new data
trainer.datasets = ["squad", "imdb"]
trainer.train(num_epochs=2)
trainer.save("continued_model.gguf")
Custom Text Generation
from switlm import TextGenerator
# Create generator
generator = TextGenerator(trainer.model, trainer.tokenizer)
# Generate with custom parameters
text = generator.generate(
"Once upon a time",
max_length=200,
temperature=0.8,
top_p=0.95,
top_k=50,
repetition_penalty=1.2
)
print(text)
Save in Multiple Formats
# Save as both PyTorch and GGUF
trainer.save("my_model", format="both")
# Outputs:
# - my_model.pt (PyTorch checkpoint)
# - my_model.gguf (GGUF format)
🏗️ Architecture Details
SwitLM implements a modern transformer architecture with:
- RoPE (Rotary Position Embeddings): Better positional encoding
- RMSNorm: More stable training than LayerNorm
- SwiGLU: Advanced activation function (from PaLM/LLaMA)
- Pre-norm Architecture: Better gradient flow
- Gradient Checkpointing: Memory-efficient training
- Mixed Precision: Faster training with FP16
📊 Training Configuration
TrainingConfig(
learning_rate=3e-4, # Learning rate
weight_decay=0.01, # Weight decay for regularization
beta1=0.9, # Adam beta1
beta2=0.95, # Adam beta2
warmup_ratio=0.1, # Warmup ratio
max_grad_norm=1.0, # Gradient clipping
batch_size=4, # Batch size (auto if None)
gradient_accumulation_steps=8, # Accumulation steps
max_length=512, # Max sequence length
num_epochs=1, # Number of epochs
use_wandb=True, # W&B logging
wandb_project="switlm" # W&B project name
)
💡 Tips and Best Practices
- Start Small: Begin with 50M or 100M models to test your pipeline
- GPU Memory: Larger models require more VRAM (1B model needs ~12GB)
- Dataset Size: More data generally means better models
- Learning Rate: Start with 3e-4, adjust based on loss curves
- Sequence Length: Shorter sequences (256-512) train faster
- Gradient Accumulation: Increase if you run out of memory
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Inspired by modern LLM architectures (LLaMA, GPT, PaLM)
- Built with PyTorch and HuggingFace Transformers
- GGUF format support for llama.cpp integration
🌟 Star History
If you find SwitLM useful, please consider giving it a star! ⭐
Made with ❤️ by the SwitLM team (Avijit Paul)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file switlm-0.1.1.tar.gz.
File metadata
- Download URL: switlm-0.1.1.tar.gz
- Upload date:
- Size: 20.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1219f0f07402a758409ffb38879d1e42ac5552b585b1c61a16c950133520dc25
|
|
| MD5 |
94ce26d7defe932b6a3576bc4642a7ee
|
|
| BLAKE2b-256 |
83427323ad63efd12ac66aa63ef75a93b599e638b62f7ab7ad5e6c0c9ded1173
|
File details
Details for the file switlm-0.1.1-py3-none-any.whl.
File metadata
- Download URL: switlm-0.1.1-py3-none-any.whl
- Upload date:
- Size: 18.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c65ab7ad985ab11f2b55c40a046961a03237d237b35f997e5b1c0f3eb03e41aa
|
|
| MD5 |
c21a2550bc5c813dc99e88c038d22f62
|
|
| BLAKE2b-256 |
dd44b965066c79aeb6f24dc1368ac5d1ef9854d587eed3c2d7a2ceb73fb31232
|