Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion backpack/hessianfree/hvp.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ def hessian_vector_product(
)

gv = sum((g_i * v_i).sum() for g_i, v_i in zip(grad_params, v))
Hv = grad(gv, params, create_graph=True, retain_graph=True, materialize_grads=True)
Hv = grad(
gv,
params,
create_graph=not detach,
retain_graph=True,
materialize_grads=True,
)

return tuple(j.detach() for j in Hv) if detach else Hv
2 changes: 1 addition & 1 deletion backpack/hessianfree/lop.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def L_op(ys, xs, ws, retain_graph=True, detach=True):
ys,
xs,
grad_outputs=ws,
create_graph=True,
create_graph=not detach,
retain_graph=retain_graph,
allow_unused=True,
materialize_grads=True,
Expand Down
2 changes: 1 addition & 1 deletion backpack/hessianfree/rop.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def R_op(ys, xs, vs, retain_graph=True, detach=True):
gs,
ws,
grad_outputs=vs,
create_graph=True,
create_graph=not detach,
retain_graph=True,
allow_unused=True,
materialize_grads=True,
Expand Down