Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manually reject step from ODE function with adaptive step solvers #210

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions torchdiffeq/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ._impl import odeint
from ._impl import odeint_adjoint
from ._impl import odeint_event
from ._impl import RejectStepError
__version__ = "0.2.3"
1 change: 1 addition & 0 deletions torchdiffeq/_impl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .odeint import odeint, odeint_event
from .adjoint import odeint_adjoint
from .rk_common import RejectStepError
53 changes: 32 additions & 21 deletions torchdiffeq/_impl/rk_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def backward(ctx, grad_scratch):
return grad_scratch, grad_scratch[ctx.index], None


class RejectStepError(Exception):
pass


def _runge_kutta_step(func, y0, f0, t0, dt, t1, tableau):
"""Take an arbitrary Runge-Kutta step and estimate error.
Args:
Expand Down Expand Up @@ -262,28 +266,35 @@ def _adaptive_step(self, rk_state):

# Must be arranged as doing all the step_t handling, then all the jump_t handling, in case we
# trigger both. (i.e. interleaving them would be wrong.)

y1, f1, y1_error, k = _runge_kutta_step(self.func, y0, f0, t0, dt, t1, tableau=self.tableau)
# dtypes:
# y1.dtype == self.y0.dtype
# f1.dtype == self.y0.dtype
# y1_error.dtype == self.dtype
# k.dtype == self.y0.dtype

########################################################
# Error Ratio #
########################################################
error_ratio = _compute_error_ratio(y1_error, self.rtol, self.atol, y0, y1, self.norm)
accept_step = error_ratio <= 1

# Handle min max stepping
if dt > self.max_step:
try:
y1, f1, y1_error, k = _runge_kutta_step(self.func, y0, f0, t0, dt, t1, tableau=self.tableau)
# dtypes:
# y1.dtype == self.y0.dtype
# f1.dtype == self.y0.dtype
# y1_error.dtype == self.dtype
# k.dtype == self.y0.dtype
except RejectStepError as ex:
# self.func requested the step be rejected
# If already at minimum step size, stop integration as can't proceed
if dt <= self.min_step:
raise(ex)
error_ratio = torch.tensor(10.0, dtype=self.dtype, device=self.y0.device)
accept_step = False
if dt <= self.min_step:
accept_step = True

# dtypes:
# error_ratio.dtype == self.dtype
else:
########################################################
# Error Ratio #
########################################################
error_ratio = _compute_error_ratio(y1_error, self.rtol, self.atol, y0, y1, self.norm)
accept_step = error_ratio <= 1

# Handle min max stepping
if dt > self.max_step:
accept_step = False
if dt <= self.min_step:
accept_step = True

# dtypes:
# error_ratio.dtype == self.dtype

########################################################
# Update RK State #
Expand Down