Skip to content

fix(autogram): Reset even when exception is raised#417

Merged
ValerianRey merged 1 commit intomainfrom
fix-state-reset-when-exception
Sep 11, 2025
Merged

fix(autogram): Reset even when exception is raised#417
ValerianRey merged 1 commit intomainfrom
fix-state-reset-when-exception

Conversation

@ValerianRey
Copy link
Copy Markdown
Contributor

@ValerianRey ValerianRey commented Sep 11, 2025

Before this, the following would fail:

import torch
from torch.nn import Linear
from torchjd.autogram import Engine

m = Linear(2, 3)
t = torch.randn([16, 2])
engine = Engine([m])

output = m(t)
g = engine.compute_gramian(output)  # ok
g = engine.compute_gramian(output)  # ValueError because target edges have been reset, as expected

output = m(t)
g = engine.compute_gramian(output)  # ValueError again, but not expected this time, because in the previous call we didn't reset properly the state and thus everything is now fucked.

Now, we get:

import torch
from torch.nn import Linear
from torchjd.autogram import Engine

m = Linear(2, 3)
t = torch.randn([16, 2])
engine = Engine([m])

output = m(t)
g = engine.compute_gramian(output)  # ok
g = engine.compute_gramian(output)  # ValueError because target edges have been reset, as expected. Still reset state.

output = m(t)
g = engine.compute_gramian(output)  # ok

@codecov
Copy link
Copy Markdown

codecov bot commented Sep 11, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/torchjd/autogram/_engine.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ValerianRey ValerianRey self-assigned this Sep 11, 2025
@ValerianRey ValerianRey added cc: fix Conventional commit type for bug fixes of the actual library (changes to src). package: autogram labels Sep 11, 2025
@ValerianRey ValerianRey changed the title Fix state reset when exception is raised fix(autogram): Reset even when exception is raised Sep 11, 2025
@ValerianRey ValerianRey merged commit 6431bbf into main Sep 11, 2025
17 checks passed
@ValerianRey ValerianRey deleted the fix-state-reset-when-exception branch September 11, 2025 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cc: fix Conventional commit type for bug fixes of the actual library (changes to src). package: autogram

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant