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.
Metadata
Release files for freegrad 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| freegrad-1.0.1.tar.gz | 16.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| freegrad-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 29.8 kB
Release files / freegrad-1.0.1.tar.gz
| Download URL | freegrad-1.0.1.tar.gz |
|---|---|
| Size | 16.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a5b537206ee8104e98e855fc6941a9c2551597ad8aa92b9fb62bbcff28ae1de5
|
|
BLAKE2b-256 checksum How to use checksums |
96b13468b70adffba7c29cb9aa543ac62699f8da1e41b51612cacf8e4ee78c76
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2025.
Transparency logRelease files / freegrad-1.0.1-py3-none-any.whl
| Download URL | freegrad-1.0.1-py3-none-any.whl |
|---|---|
| Size | 13.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9909e913fcdb155897eb5a3e4b3ebac3a3dda97230cffeec919443bd088fcbb6
|
|
BLAKE2b-256 checksum How to use checksums |
265add2c9501c5d16e6a5d9faf2fd43dd5462476b6ab6341afbb6a40b1c35941
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2025.
Transparency log