Skip to content

Conversation

@f-dangel
Copy link
Owner

@f-dangel f-dangel commented Nov 12, 2024

This PR adds an experimental AutomaticDerivatives class, which implements BackPACK's core functionality using torch.autograd.grad given a module's forward function. This simplifies supporting new layers at the cost of reduced performance. Currently, AutomaticDerivatives only implements VJPs w.r.t. layer inputs and parameters. This is enough to support the most common first- and second-order extensions.

A future PR will add an example to demonstrate how to use AutomaticDerivatives to add support for a new layer to a specific extension.

@f-dangel f-dangel requested a review from fKunstner November 12, 2024 22:10
@f-dangel
Copy link
Owner Author

@fKunstner could you have quick look, or give feedback whether you think this feature makes sense?

Copilot AI review requested due to automatic review settings November 26, 2025 22:46
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces experimental automatic derivative support for BackPACK through a new AutomaticDerivatives class that leverages torch.autograd.grad to compute derivatives automatically. This enables supporting new layers without manual derivative implementation, trading performance for convenience. The implementation currently supports VJPs (Vector-Jacobian Products) w.r.t. layer inputs and parameters, covering the most common first- and second-order extensions.

Key changes:

  • New AutomaticDerivatives class provides autograd-based derivative computation for arbitrary layers
  • Comprehensive test suite demonstrates usage for GGN diagonal and batch gradient computations
  • Updated type hints for better type safety in subsampling utilities
  • Documentation tracking and badge updates in README

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
backpack/core/derivatives/automatic.py Core implementation of AutomaticDerivatives using torch.func (vjp, vmap) for automatic derivative computation
test/test_automatic_support.py Test suite validating automatic derivatives against manual implementations for DiagGGN and BatchGrad extensions
test/automatic_extensions.py Example extension classes demonstrating how to use AutomaticDerivatives with existing BackPACK extension framework
test/utils/__init__.py Added popattr helper function for removing and returning object attributes in tests
backpack/utils/subsampling.py Updated type hints to use Optional[List[int]] for better type safety
fully_documented.txt Updated documentation tracking to include new files (contains reference to non-existent file)
README.md Updated badges (Travis to RTD) and minimum Python version (3.8+ to 3.9+)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants