A PyTorch extension for experimenting with alternative gradient flows.
Project description
FreeGrad
Alternative backward rules and gradient transforms alongside PyTorch autograd.
🚀 Features
- Register and compose custom gradient rules (backward transforms)
- Apply rules via a context manager to activations and/or params
- Lightweight wrappers for activation layers
- Works alongside standard autograd without patching PyTorch
📦 Installation
# Core package only (from PyPI)
pip install freegrad
# Development install (with testing, linting, docs, examples, etc.)
pip install -e '.[dev]'
💡 Note: If you’re using zsh (default on macOS), don’t forget the quotes around
.[dev].
🧪 Running Tests
After installing in development mode:
pip install -e '.[dev]'
Run the full test suite with:
pytest
Run with coverage reporting:
pytest --cov=freegrad --cov-report=term-missing
Run a specific test file or test:
pytest tests/test_wrappers.py -v
pytest tests/test_wrappers.py::test_activation_forward_relu -v
🎓 Running Examples
The repository includes runnable scripts under examples/ that replicate experiments from the paper.
Install dev dependencies:
pip install -e '.[dev]'
Run an example:
python examples/suc_logistic_vs_constant.py
python examples/mlp_digits_constant_vs_tied.py
python examples/lenet_mnist_rectangular.py
python examples/cnn_gradient_jamming.py
python examples/bnn_step_activation.py
💡 Some examples require datasets (e.g. MNIST via
torchvision, DIGITS viascikit-learn). They will be downloaded automatically the first time you run them.
⚡ Quickstart
import torch
import freegrad as fg
from freegrad.wrappers import Activation
x = torch.randn(8, requires_grad=True)
act = Activation(forward="ReLU")
with fg.use(rule="rectangular_jam", params={"a": -1.0, "b": 1.0}, scope="activations"):
y = act(x).sum()
y.backward()
print(x.grad)
🛠️ Makefile Shortcuts
This project includes a Makefile with useful commands:
# Run everything (install deps, build paper, tests, and examples)
make
# Build the JOSS-style paper PDF only
# Requires pandoc >= 2.11 and xelatex installed on your system
make pdf
# Run the test suite with coverage
make test
# Run all examples sequentially
make examples
# Run a specific example
make suc # Single-Unit Classifier (SUC)
make mlp # MLP on DIGITS
make lenet # LeNet on MNIST with Rectangular gradient
make cnn # CNN with Gradient Jamming
make bnn # BNN with Step activation
💡 The
installstep (pip install -e '.[dev]') is included automatically when runningmake,make test, ormake examples.
📖 Documentation
👉 Full docs available here: https://tbox98.github.io/FreeGrad/
🤝 Contributing
Contributions are welcome! Please read CONTRIBUTING.md.
📄 License
Distributed under the MIT License.
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 freegrad-1.0.0.tar.gz.
File metadata
- Download URL: freegrad-1.0.0.tar.gz
- Upload date:
- Size: 16.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
00c6a211fc3c4fe5e4f8647b3cbec27f33d15e6f5b80df92a7c53464e9435881
|
|
| MD5 |
56599ec827d40372187d5478df9adbaa
|
|
| BLAKE2b-256 |
89ced1842a4e05c4c4a7a79010bd0b1eb24a7836f99d900b7efb28ee9f4b39be
|
Provenance
The following attestation bundles were made for freegrad-1.0.0.tar.gz:
Publisher:
pypi.yml on tbox98/FreeGrad
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
freegrad-1.0.0.tar.gz -
Subject digest:
00c6a211fc3c4fe5e4f8647b3cbec27f33d15e6f5b80df92a7c53464e9435881 - Sigstore transparency entry: 708557584
- Sigstore integration time:
-
Permalink:
tbox98/FreeGrad@99edc63e3b3ba185a057ade4bc96743a6a087e19 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/tbox98
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@99edc63e3b3ba185a057ade4bc96743a6a087e19 -
Trigger Event:
release
-
Statement type:
File details
Details for the file freegrad-1.0.0-py3-none-any.whl.
File metadata
- Download URL: freegrad-1.0.0-py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ece1808748e5f0e424f4047ab9db350c4da298ab65526a4dc8e92a586441acc6
|
|
| MD5 |
ad00c30dd1effb6b6b1d57841a3a88a0
|
|
| BLAKE2b-256 |
f7f139b18ea8d4613cde70fcbf7358a79044d47dfb9f401d37fe53b5dfc7c267
|
Provenance
The following attestation bundles were made for freegrad-1.0.0-py3-none-any.whl:
Publisher:
pypi.yml on tbox98/FreeGrad
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
freegrad-1.0.0-py3-none-any.whl -
Subject digest:
ece1808748e5f0e424f4047ab9db350c4da298ab65526a4dc8e92a586441acc6 - Sigstore transparency entry: 708557585
- Sigstore integration time:
-
Permalink:
tbox98/FreeGrad@99edc63e3b3ba185a057ade4bc96743a6a087e19 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/tbox98
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@99edc63e3b3ba185a057ade4bc96743a6a087e19 -
Trigger Event:
release
-
Statement type: