Skip to content
Merged
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
346 changes: 346 additions & 0 deletions examples/2d_cnf_maximum_likelihood.ipynb

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions flow_matching/solver/ode_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,26 @@ def dynamics_func(t, states):
# Compute exact divergence
div = 0
for i in range(ut.flatten(1).shape[1]):
div += gradient(ut[:, i], xt, create_graph=True)[:, i].detach()
g = gradient(ut[:, i], xt, create_graph=True)[:, i]
if not enable_grad:
g = g.detach()
div += g
else:
# Compute Hutchinson divergence estimator E[z^T D_x(ut) z]
ut_dot_z = torch.einsum(
"ij,ij->i", ut.flatten(start_dim=1), z.flatten(start_dim=1)
)
grad_ut_dot_z = gradient(ut_dot_z, xt)
grad_ut_dot_z = gradient(ut_dot_z, xt, create_graph=enable_grad)
div = torch.einsum(
"ij,ij->i",
grad_ut_dot_z.flatten(start_dim=1),
z.flatten(start_dim=1),
)

return ut.detach(), div.detach()
if not enable_grad:
ut = ut.detach()
div = div.detach()
return ut, div

y_init = (x_1, torch.zeros(x_1.shape[0], device=x_1.device))
ode_opts = {"step_size": step_size} if step_size is not None else {}
Expand Down
Loading