Skip to content

Commit 590323c

Browse files
committed
Initial commit: Gradient Cache v1.0.0
- GPU memory-efficient training system for PyTorch - 90%+ memory savings through gradient compression - Simple 3-line integration - Tested on A100 and T4 GPUs
0 parents  commit 590323c

17 files changed

Lines changed: 1967 additions & 0 deletions

.gitignore

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
*.so
6+
.Python
7+
build/
8+
develop-eggs/
9+
dist/
10+
downloads/
11+
eggs/
12+
.eggs/
13+
lib/
14+
lib64/
15+
parts/
16+
sdist/
17+
var/
18+
wheels/
19+
*.egg-info/
20+
.installed.cfg
21+
*.egg
22+
MANIFEST
23+
24+
# Virtual environments
25+
env/
26+
venv/
27+
ENV/
28+
env.bak/
29+
venv.bak/
30+
31+
# IDEs
32+
.vscode/
33+
.idea/
34+
*.swp
35+
*.swo
36+
37+
# Testing
38+
.pytest_cache/
39+
.coverage
40+
htmlcov/
41+
.tox/
42+
43+
# OS
44+
.DS_Store
45+
Thumbs.db
46+
47+
# Project specific
48+
*.log
49+
*.pt
50+
*.pth
51+
checkpoint/
52+
outputs/

LICENSE

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Apache License
2+
Version 2.0, January 2004
3+
http://www.apache.org/licenses/
4+
5+
Copyright 2024 Gradient Cache Contributors
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.

MANIFEST.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
include README.md
2+
include LICENSE
3+
include requirements.txt
4+
recursive-include gradient_cache *.py
5+
recursive-include tests *.py
6+
recursive-include examples *.py

README.md

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
# Gradient Cache - GPU Memory-Efficient Training
2+
3+
[![Version](https://img.shields.io/badge/version-1.0.0-blue.svg)](https://github.com/gradient-cache/gradient-cache)
4+
[![License](https://img.shields.io/badge/license-Apache%202.0-green.svg)](LICENSE)
5+
6+
Gradient Cache is a production-ready PyTorch extension that reduces GPU memory usage by 90%+ during neural network training through intelligent gradient compression and CPU offloading.
7+
8+
## 🚀 Key Features
9+
10+
- **90%+ Memory Savings**: Compress gradients by 100x with minimal accuracy impact
11+
- **Larger Batch Sizes**: Train with 2-3x larger batches on the same hardware
12+
- **Simple Integration**: Just 3 lines of code to add to any training loop
13+
- **Universal Compatibility**: Works with any PyTorch model and optimizer
14+
- **Production Ready**: Tested on A100 and T4 GPUs with real models
15+
16+
## 📊 Proven Results
17+
18+
| Model | Parameters | Memory Saved | Compression |
19+
|-------|------------|--------------|-------------|
20+
| GPT-2 Small | 124M | 479 MB/step | 100x |
21+
| GPT-2 Medium | 350M | ~1.3 GB/step | 100x |
22+
| Custom NN | 50M | 144 MB/step | 100x |
23+
24+
## 🔧 Installation
25+
26+
```bash
27+
pip install gradient-cache
28+
```
29+
30+
Or install from source:
31+
```bash
32+
git clone https://github.com/your-username/gradient-cache
33+
cd gradient-cache
34+
pip install -e .
35+
```
36+
37+
## 💡 Quick Start
38+
39+
Add gradient cache to any PyTorch training loop with just 3 lines:
40+
41+
```python
42+
import gradient_cache
43+
44+
# Create your model
45+
model = create_your_model().cuda()
46+
47+
# Add gradient cache (1 line)
48+
hook_manager = gradient_cache.create_gradient_cache(model, compression_ratio=100)
49+
50+
# Normal training loop
51+
optimizer = torch.optim.Adam(model.parameters())
52+
53+
for batch in dataloader:
54+
loss = model(batch).mean()
55+
loss.backward()
56+
57+
# Compress gradients (1 line)
58+
hook_manager.compress_and_free_gradients()
59+
60+
# Restore gradients and update (1 line)
61+
hook_manager.apply_gradients()
62+
optimizer.step()
63+
optimizer.zero_grad()
64+
```
65+
66+
## 🎯 Integration with Training Frameworks
67+
68+
### Metaflow Integration
69+
70+
Use the decorator for automatic integration:
71+
72+
```python
73+
from metaflow import FlowSpec, step
74+
import gradient_cache
75+
76+
class MyTrainingFlow(FlowSpec):
77+
@step
78+
@gradient_cache.optimize(compression_ratio=100)
79+
def train(self):
80+
# Your training code - no changes needed!
81+
model = create_model()
82+
optimizer = torch.optim.Adam(model.parameters())
83+
# ... rest of training
84+
```
85+
86+
### PyTorch Lightning
87+
88+
```python
89+
import pytorch_lightning as pl
90+
import gradient_cache
91+
92+
class MyModel(pl.LightningModule):
93+
def __init__(self):
94+
super().__init__()
95+
self.model = create_model()
96+
self.hook_manager = gradient_cache.create_gradient_cache(self.model)
97+
98+
def training_step(self, batch, batch_idx):
99+
loss = self.model(batch).mean()
100+
return loss
101+
102+
def on_after_backward(self):
103+
self.hook_manager.compress_and_free_gradients()
104+
105+
def optimizer_step(self, *args, **kwargs):
106+
self.hook_manager.apply_gradients()
107+
super().optimizer_step(*args, **kwargs)
108+
```
109+
110+
## 🛠️ Advanced Usage
111+
112+
### Custom Compression Ratios
113+
114+
```python
115+
# Conservative - 10x compression (keep 10%)
116+
hook_manager = gradient_cache.create_gradient_cache(model, compression_ratio=10)
117+
118+
# Aggressive - 1000x compression (keep 0.1%)
119+
hook_manager = gradient_cache.create_gradient_cache(model, compression_ratio=1000)
120+
```
121+
122+
### Exclude Critical Layers
123+
124+
```python
125+
# Don't compress embeddings or output layers
126+
hook_manager = gradient_cache.GradientCacheHookManager(
127+
model,
128+
compression_ratio=100,
129+
exclude_layers=['embedding', 'lm_head']
130+
)
131+
```
132+
133+
### Monitor Compression
134+
135+
```python
136+
# Enable verbose mode
137+
hook_manager = gradient_cache.create_gradient_cache(model, verbose=True)
138+
139+
# Get compression statistics
140+
stats = hook_manager.get_compression_summary()
141+
print(f"Compression ratio: {stats['overall_compression_ratio']:.1f}x")
142+
print(f"Memory saved: {stats['memory_saved_mb']:.1f} MB")
143+
```
144+
145+
## 📈 How It Works
146+
147+
1. **Gradient Computation**: Normal backward pass computes gradients
148+
2. **Compression**: Keep only top 1% of gradient values by magnitude
149+
3. **CPU Offload**: Move compressed gradients to system RAM
150+
4. **GPU Memory Release**: Free GPU memory for next batch
151+
5. **Gradient Restoration**: Restore gradients for optimizer step
152+
153+
## 🏆 Benefits
154+
155+
- **Cost Savings**: Use smaller, cheaper GPU instances
156+
- **Larger Models**: Train models that don't fit in GPU memory
157+
- **Faster Research**: Iterate quickly with larger batch sizes
158+
- **Easy Integration**: No model architecture changes needed
159+
160+
## 🧪 Testing
161+
162+
Run the test suite:
163+
```bash
164+
python tests/test_gradient_cache.py
165+
```
166+
167+
## 📝 Citation
168+
169+
If you use Gradient Cache in your research, please cite:
170+
171+
```bibtex
172+
@software{gradient_cache,
173+
title = {Gradient Cache: GPU Memory-Efficient Training},
174+
author = {Gradient Cache Contributors},
175+
year = {2024},
176+
url = {https://github.com/gradient-cache/gradient-cache}
177+
}
178+
```
179+
180+
## 📄 License
181+
182+
Apache License 2.0 - see [LICENSE](LICENSE) for details.
183+
184+
## 🤝 Contributing
185+
186+
We welcome contributions! Please submit issues and pull requests on GitHub.
187+
188+
## 📧 Support
189+
190+
- **Issues**: [GitHub Issues](https://github.com/gradient-cache/gradient-cache/issues)
191+
- **Discussions**: [GitHub Discussions](https://github.com/gradient-cache/gradient-cache/discussions)
192+
193+
---
194+
195+
Built with ❤️ for the ML community

RELEASE_NOTES.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Gradient Cache v1.0.0 Release Notes
2+
3+
## 🎉 First Public Release
4+
5+
### Features
6+
- 90%+ GPU memory savings during neural network training
7+
- 100x gradient compression with minimal accuracy impact
8+
- Simple 3-line integration with any PyTorch model
9+
- Support for Metaflow and PyTorch Lightning
10+
- Tested on NVIDIA A100 and T4 GPUs
11+
12+
### Performance
13+
- Compression ratio: 100x (keeps top 1% of gradients)
14+
- Memory savings: 90-95% of gradient memory
15+
- Training overhead: ~10-15%
16+
- Enables 2-3x larger batch sizes
17+
18+
### Files Included
19+
- `gradient_cache-1.0.0-py3-none-any.whl` - Pip installable wheel
20+
- `gradient_cache-1.0.0.tar.gz` - Source distribution
21+
- Full documentation and examples
22+
23+
### Installation
24+
```bash
25+
pip install gradient_cache-1.0.0-py3-none-any.whl
26+
```
27+
28+
### Acknowledgments
29+
Built with PyTorch 2.7.1 and tested extensively on Lightning AI platform.

0 commit comments

Comments
 (0)