Skip to content

Commit 70898c6

Browse files
feat: add unroll param to make_ode and improve docs
- Added `unroll` parameter to `vbjax.make_ode` to match `make_sde` API and enable loop unrolling optimization. - Updated `make_ode` docstring to include `method` and `unroll` parameters. - Updated `docs/integrators.rst` to match the implementation signature and document the new parameter. - Added `test_ode_unroll` test case.
1 parent e3c84a2 commit 70898c6

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

docs/integrators.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,13 @@ make_ode
120120

121121
Create an ordinary differential equation integrator.
122122

123-
.. function:: make_ode(dt, dfun, method='heun', adhoc=None)
123+
.. function:: make_ode(dt, dfun, adhoc=None, method='heun', unroll=10)
124124

125125
:param float dt: Time step size
126126
:param callable dfun: Function ``dfun(x, p)`` that returns dx/dt
127-
:param str method: Integration method - ``'euler'``, ``'heun'``, or ``'rk4'``
128127
:param callable adhoc: Optional function ``f(x, p)`` for post-step corrections
128+
:param str method: Integration method - ``'euler'``, ``'heun'``, or ``'rk4'``
129+
:param int unroll: Loop unroll factor for performance
129130
:return: Tuple of (step, loop) functions
130131
:rtype: tuple
131132

vbjax/loops.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def op(x, z):
175175
return step, loop
176176

177177

178-
def make_ode(dt, dfun, adhoc=None, method='heun'):
178+
def make_ode(dt, dfun, adhoc=None, method='heun', unroll=10):
179179
"""Use a Heun scheme to integrate autonomous ordinary differential
180180
equations (ODEs).
181181
@@ -189,6 +189,10 @@ def make_ode(dt, dfun, adhoc=None, method='heun'):
189189
adhoc : function or None
190190
Function of the form `f(x, p)` that allows making adhoc corrections
191191
to states after a step.
192+
method : str
193+
Integration method, one of 'euler', 'heun', 'rk4'.
194+
unroll : int
195+
Force unrolls the time stepping loop.
192196
193197
Returns
194198
=======
@@ -226,7 +230,7 @@ def loop(x0, ts, p):
226230
def op(x, t):
227231
x = step(x, t, p)
228232
return x, x
229-
return jax.lax.scan(op, x0, ts)[1]
233+
return jax.lax.scan(op, x0, ts, unroll=unroll)[1]
230234

231235
return step, loop
232236

vbjax/tests/test_loops.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ def test_ode():
2222
assert xs.shape == (64, 32)
2323

2424

25+
def test_ode_unroll():
26+
f = lambda x, _: -x
27+
dt = 0.1
28+
# Check if unroll parameter is accepted and works
29+
_, run = vb.make_ode(dt, f, unroll=5)
30+
x0 = np.r_[:32].astype('f')
31+
xs = run(x0, np.r_[:64], None)
32+
assert xs.shape == (64, 32)
33+
34+
2535
def test_dde():
2636
def dfun(xt, x, t, p):
2737
xd = xt[0, t-100]

0 commit comments

Comments
 (0)