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.1.tar.gz (16.7 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.1-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file freegrad-1.0.1.tar.gz.

File metadata

  • Download URL: freegrad-1.0.1.tar.gz
  • Upload date:
  • Size: 16.7 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.1.tar.gz
Algorithm Hash digest
SHA256 a5b537206ee8104e98e855fc6941a9c2551597ad8aa92b9fb62bbcff28ae1de5
MD5 7efc1631c43853fc2f006400cfbe9415
BLAKE2b-256 96b13468b70adffba7c29cb9aa543ac62699f8da1e41b51612cacf8e4ee78c76

See more details on using hashes here.

Provenance

The following attestation bundles were made for freegrad-1.0.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: freegrad-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 13.1 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9909e913fcdb155897eb5a3e4b3ebac3a3dda97230cffeec919443bd088fcbb6
MD5 4841fc8c8356a5d53f8e2e25e82474fb
BLAKE2b-256 265add2c9501c5d16e6a5d9faf2fd43dd5462476b6ab6341afbb6a40b1c35941

See more details on using hashes here.

Provenance

The following attestation bundles were made for freegrad-1.0.1-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