Skip to main content

A PyTorch extension for experimenting with alternative gradient flows.

Project description

FreeGrad

Alternative backward rules and gradient transforms alongside PyTorch autograd.

CI Tests PyPI License: MIT Docs


🚀 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 via scikit-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 install step (pip install -e '.[dev]') is included automatically when running make, make test, or make 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


Download files

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

Source Distribution

freegrad-1.0.0.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

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

freegrad-1.0.0-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

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

Hashes for freegrad-1.0.0.tar.gz
Algorithm Hash digest
SHA256 00c6a211fc3c4fe5e4f8647b3cbec27f33d15e6f5b80df92a7c53464e9435881
MD5 56599ec827d40372187d5478df9adbaa
BLAKE2b-256 89ced1842a4e05c4c4a7a79010bd0b1eb24a7836f99d900b7efb28ee9f4b39be

See more details on using hashes here.

Provenance

The following attestation bundles were made for freegrad-1.0.0.tar.gz:

Publisher: pypi.yml on tbox98/FreeGrad

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for freegrad-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ece1808748e5f0e424f4047ab9db350c4da298ab65526a4dc8e92a586441acc6
MD5 ad00c30dd1effb6b6b1d57841a3a88a0
BLAKE2b-256 f7f139b18ea8d4613cde70fcbf7358a79044d47dfb9f401d37fe53b5dfc7c267

See more details on using hashes here.

Provenance

The following attestation bundles were made for freegrad-1.0.0-py3-none-any.whl:

Publisher: pypi.yml on tbox98/FreeGrad

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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