ODE Solvers¶
ODE integration methods for flow-based sampling.
ode_solvers
¶
ODE Solvers for Flow Matching Inference using torchdiffeq.
This module provides a unified interface for using various ODE solvers from the torchdiffeq library for flow matching inference. The solvers integrate the velocity field from t=0 (noise) to t=1 (solution).
Available solvers: - Explicit methods: euler, midpoint, rk4, dopri5 (adaptive) - Implicit methods: dopri8, adaptive_heun - Specialized: bosh3, tsit5
Refactored to inherit from flowpde.core.base_solver.ODESolver
VelocityField
¶
Bases: Module
Wraps a flow matching model as an ODE velocity field.
The flow matching model predicts \(v(x_t, ext{condition}, t)\), which is the time derivative \(dx/dt\) at time \(t\). This wrapper makes it compatible with torchdiffeq's interface.
Source code in flowpde/solvers/ode_solvers.py
__init__(model, condition)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
Flow matching model with signature model(x, condition, t) |
required |
condition
|
Tensor
|
Conditioning tensor (batch_size, dim) - fixed during integration |
required |
Source code in flowpde/solvers/ode_solvers.py
forward(t, x)
¶
Compute velocity field at time \(t\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
Tensor
|
Current time (scalar tensor) |
required |
x
|
Tensor
|
Current state (batch_size, dim) |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Velocity \(dx/dt\) at time \(t\) |
Source code in flowpde/solvers/ode_solvers.py
ODEFlowSolver
¶
Bases: ODESolver
ODE solver for flow matching inference using torchdiffeq.
This class provides a high-level interface for sampling from flow matching models using various ODE solvers from torchdiffeq.
Inherits from flowpde.core.base_solver.ODESolver
Source code in flowpde/solvers/ode_solvers.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | |
is_adaptive
property
¶
Whether this is an adaptive step-size solver.
supports_adjoint
property
¶
Whether this solver supports adjoint method for backprop.
__init__(model, method='dopri5', rtol=1e-05, atol=1e-07, adjoint=False, method_options=None)
¶
Initialize ODE solver.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
Flow matching model with signature model(x, condition, t) |
required |
method
|
str
|
ODE solver method. Options: - 'dopri5': Runge-Kutta 4(5) (adaptive, recommended) - 'dopri8': Runge-Kutta 7(8) (high accuracy) - 'bosh3': Bogacki-Shampine 2(3) (faster, less accurate) - 'tsit5': Tsitouras 5(4) (good balance) - 'euler': Explicit Euler (simple, fast, less accurate) - 'midpoint': Explicit midpoint - 'rk4': Classic 4th order Runge-Kutta - 'adaptive_heun': Adaptive Heun's method |
'dopri5'
|
rtol
|
float
|
Relative tolerance (for adaptive solvers) |
1e-05
|
atol
|
float
|
Absolute tolerance (for adaptive solvers) |
1e-07
|
adjoint
|
bool
|
If True, use adjoint method for memory-efficient backprop |
False
|
method_options
|
Optional[dict]
|
Additional options for the solver |
None
|
Source code in flowpde/solvers/ode_solvers.py
solve(func, y0, t_span, **kwargs)
¶
Solve the ODE \(dy/dt = f(t, y)\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[Tensor, Tensor], Tensor]
|
Function computing \(dy/dt\) given \((t, y)\) |
required |
y0
|
Tensor
|
Initial state (batch_size, dim) |
required |
t_span
|
Tuple[float, float]
|
Time interval \((t_{ ext{start}}, t_{ ext{end}})\) |
required |
**kwargs
|
Any
|
Additional solving parameters |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
y_final |
Tensor
|
Final state at t_end (batch_size, dim) |
Source code in flowpde/solvers/ode_solvers.py
solve_trajectory(func, y0, t_eval, **kwargs)
¶
Solve and return trajectory at specified time points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[Tensor, Tensor], Tensor]
|
Function computing dy/dt |
required |
y0
|
Tensor
|
Initial state (batch_size, dim) |
required |
t_eval
|
Tensor
|
Time points for evaluation (n_steps,) |
required |
**kwargs
|
Any
|
Additional parameters |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
trajectory |
Tensor
|
States at each time point (n_steps, batch_size, dim) |
Source code in flowpde/solvers/ode_solvers.py
sample(condition, x_init=None, t_span=(0.0, 1.0), return_trajectory=False, n_steps=None, no_grad=True)
¶
Sample from flow matching model by solving ODE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
condition
|
Tensor
|
Conditioning tensor (batch_size, dim) or (batch_size, H, W) |
required |
x_init
|
Optional[Tensor]
|
Initial noise (batch_size, dim). If None, sample from N(0, I) |
None
|
t_span
|
Tuple[float, float]
|
Time interval (t_start, t_end), typically (0.0, 1.0) |
(0.0, 1.0)
|
return_trajectory
|
bool
|
If True, return full trajectory |
False
|
n_steps
|
Optional[int]
|
Number of evaluation points (for fixed-step solvers or trajectory) If None, adaptive solvers choose steps automatically |
None
|
no_grad
|
bool
|
Integrate under |
True
|
Note
The model is switched to eval mode for the duration of the call and restored afterwards, so sampling mid-training does not silently disable dropout or freeze BatchNorm statistics.
Returns:
| Name | Type | Description |
|---|---|---|
samples |
Union[Tensor, Tuple[Tensor, Tensor]]
|
Final samples at t_end (batch_size, dim) or (batch_size, H, W) |
trajectory |
Union[Tensor, Tuple[Tensor, Tensor]]
|
(optional) Full trajectory if return_trajectory=True |
Source code in flowpde/solvers/ode_solvers.py
get_solver_info()
¶
sample_with_ode_solver(model, condition, solver='dopri5', rtol=1e-05, atol=1e-07, n_steps=None, device=None, return_trajectory=False)
¶
Convenience function for sampling with ODE solver.
This is a simpler interface to ODEFlowSolver for one-off sampling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
Flow matching model |
required |
condition
|
Tensor
|
Conditioning tensor |
required |
solver
|
str
|
ODE solver method (see ODEFlowSolver for options) |
'dopri5'
|
rtol
|
float
|
Relative tolerance |
1e-05
|
atol
|
float
|
Absolute tolerance |
1e-07
|
n_steps
|
Optional[int]
|
Number of steps (for fixed-step solvers) |
None
|
device
|
Optional[str]
|
Device for computation. Defaults to CUDA when available, CPU otherwise. |
None
|
return_trajectory
|
bool
|
If True, return full trajectory |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
samples |
Union[Tensor, Tuple[Tensor, Tensor]]
|
Final samples |
trajectory |
Union[Tensor, Tuple[Tensor, Tensor]]
|
(optional) Full trajectory if return_trajectory=True |
Example
samples = sample_with_ode_solver( ... model=trained_model, ... condition=f, ... solver='dopri5', ... device='cuda' ... )
Source code in flowpde/solvers/ode_solvers.py
compare_solvers(model, condition, ground_truth=None, solvers=None, device=None, n_steps=50)
¶
Compare different ODE solvers on the same input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
Flow matching model |
required |
condition
|
Tensor
|
Conditioning tensor |
required |
ground_truth
|
Optional[Tensor]
|
Optional ground truth for error computation |
None
|
solvers
|
Optional[List[str]]
|
List of solver names to compare. If None, uses default set |
None
|
device
|
Optional[str]
|
Device for computation. Defaults to CUDA when available, CPU otherwise. |
None
|
n_steps
|
int
|
Number of steps for fixed-step solvers |
50
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with results for each solver including: |
dict
|
|
dict
|
|
dict
|
|
Example
results = compare_solvers( ... model=trained_model, ... condition=f, ... ground_truth=u_true, ... solvers=['euler', 'rk4', 'dopri5'] ... ) for solver, info in results.items(): ... print(f"{solver}: error={info['error']:.6f}, time={info['time']:.3f}s")
Source code in flowpde/solvers/ode_solvers.py
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 | |