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

Jax export #1861

Merged
merged 94 commits into from
Nov 19, 2024
Merged
Changes from 1 commit
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
328d462
basic prototype
FFroehlich Aug 25, 2022
ffa5afb
Merge branch 'develop' into jax_export
FFroehlich Aug 26, 2022
d4f8552
add dimerization example, add second order code, refactor jit
FFroehlich Aug 26, 2022
d37a850
remove equinox dependency, list dependencies
FFroehlich Aug 26, 2022
ff37c7e
make jax optional
FFroehlich Aug 26, 2022
c3a77f7
Merge branch 'develop' into jax_export
FFroehlich Aug 26, 2022
7cd8553
support conservation laws
FFroehlich Aug 26, 2022
5177ad7
fixup
FFroehlich Aug 26, 2022
5612cfc
fix jit nesting
FFroehlich Aug 26, 2022
2dd0377
use vmap for vectorization
FFroehlich Aug 26, 2022
e9bd14f
fixups
FFroehlich Aug 26, 2022
bbb5246
add multithreaded simulation runner
FFroehlich Aug 26, 2022
9bd1004
fix my
FFroehlich Aug 26, 2022
1b06c24
Merge branch 'develop' into jax_export
FFroehlich Sep 9, 2022
599aa71
fixes
FFroehlich Sep 13, 2022
51812d6
merge
FFroehlich Apr 10, 2024
3fbd17a
fixup merge
FFroehlich Apr 10, 2024
5974d47
fix install
FFroehlich Apr 10, 2024
37cdc81
actually generate code
FFroehlich Apr 10, 2024
9e6a0ff
fix
FFroehlich Apr 10, 2024
22b2b38
fix
FFroehlich Apr 10, 2024
48a2e49
add better default coefficients, fix jax
FFroehlich Apr 10, 2024
481216d
ignore fujita in jax
FFroehlich Apr 10, 2024
85b8173
ignore smith
FFroehlich Apr 10, 2024
b213adb
optimize & fix bachmann
FFroehlich Apr 11, 2024
a1f37b7
fix import/wokflow
FFroehlich Apr 11, 2024
e09bb2f
Update __init__.template.py
FFroehlich Apr 12, 2024
d8d1900
fix jax imports
FFroehlich Apr 12, 2024
c24fe6b
Update setup.cfg
FFroehlich Apr 12, 2024
1ec591c
add preequilibration support
FFroehlich Apr 12, 2024
aebe07c
fix jax tests
FFroehlich Apr 13, 2024
4125c51
add filterwarning
FFroehlich Apr 14, 2024
8143cc2
fix parameter transformation
FFroehlich Apr 14, 2024
781bb3b
Merge branch 'develop' into jax_export
FFroehlich Oct 19, 2024
81e2aeb
reenable ruff format
FFroehlich Oct 19, 2024
c01f707
post merge cleanup
FFroehlich Oct 19, 2024
a5d356a
"fix" splines
FFroehlich Oct 19, 2024
9a021cf
Update .pre-commit-config.yaml
FFroehlich Oct 19, 2024
a02d215
Merge branch 'develop' into jax_export
FFroehlich Oct 19, 2024
50193d8
force optimistix 0.0.9
FFroehlich Oct 21, 2024
d6c5bcd
Merge branch 'jax_export' of https://github.com/AMICI-dev/AMICI into …
FFroehlich Oct 21, 2024
7faae32
add support for heavyside functions
FFroehlich Oct 21, 2024
907acb7
cleanup & actually run tests
FFroehlich Oct 21, 2024
82a01ba
simply tests + add support for non-dynamic simulation in jax
FFroehlich Oct 22, 2024
7c3aef9
Merge branch 'develop' into jax_export
FFroehlich Oct 23, 2024
c548c93
fix for NONCONST_CLS
FFroehlich Oct 24, 2024
7c27a21
fix petab path
FFroehlich Oct 24, 2024
b84dbdb
Merge branch 'develop' into jax_export
FFroehlich Oct 24, 2024
37b9329
Merge branch 'develop' into jax_export
FFroehlich Oct 24, 2024
956b0a6
fixup merge
FFroehlich Oct 24, 2024
2f3834d
support postequilibration
FFroehlich Oct 25, 2024
5366632
fixup
FFroehlich Oct 25, 2024
5a86f4c
fix
FFroehlich Oct 25, 2024
480b75a
fix gradients
FFroehlich Oct 25, 2024
8b9c10a
fix hessian
FFroehlich Oct 25, 2024
7dc81ac
Update test_petab_benchmark.py
FFroehlich Oct 25, 2024
866c811
Merge branch 'develop' into jax_export
FFroehlich Oct 27, 2024
02a1272
skip smith in jax
FFroehlich Oct 27, 2024
51bd18c
exclude more models
FFroehlich Oct 27, 2024
c7c5d4b
refactor: remove use of edatas
FFroehlich Nov 9, 2024
a514deb
update template
FFroehlich Nov 9, 2024
498681a
Update .pre-commit-config.yaml
FFroehlich Nov 9, 2024
4a5e7d2
Merge branch 'develop' into jax_export
FFroehlich Nov 11, 2024
f745be0
fix python jax tests
FFroehlich Nov 12, 2024
a64f89b
simplify petab interface
FFroehlich Nov 12, 2024
7292451
add parameter values to model class
FFroehlich Nov 12, 2024
da02106
refactor parameter mapping
FFroehlich Nov 12, 2024
a46e65d
refactor & simplify
FFroehlich Nov 12, 2024
404d82e
refsctor
FFroehlich Nov 16, 2024
e399f4c
update template
FFroehlich Nov 16, 2024
eaae778
Update .pre-commit-config.yaml
FFroehlich Nov 16, 2024
d79cfc1
refactor fix test
FFroehlich Nov 16, 2024
94aa679
Update petab.py
FFroehlich Nov 16, 2024
b129c86
fixups
FFroehlich Nov 17, 2024
9b6a62b
fixup
FFroehlich Nov 17, 2024
74cd498
add documentation and typing
FFroehlich Nov 17, 2024
d94714b
add runtime typechecks to jax tests
FFroehlich Nov 17, 2024
0a9fcdf
add coverage from benchmark tests
FFroehlich Nov 17, 2024
186805c
add api versioning and reenable jit compilation
FFroehlich Nov 17, 2024
250f9dd
review comments
FFroehlich Nov 18, 2024
dc4992e
use temporary directories
FFroehlich Nov 18, 2024
d547509
fix doc
FFroehlich Nov 18, 2024
82bfe31
Update test_jax.py
FFroehlich Nov 18, 2024
a010803
don't generate code if jax/diffrax not available
FFroehlich Nov 18, 2024
d9ae05e
Merge branch 'develop' into jax_export
FFroehlich Nov 18, 2024
f7c2c10
add example
FFroehlich Nov 19, 2024
5dc8735
fix doc
FFroehlich Nov 19, 2024
784ab2c
fix notebook symlink
FFroehlich Nov 19, 2024
d528168
update notebook
FFroehlich Nov 19, 2024
24d8c09
Update ExampleJaxPEtab.ipynb
FFroehlich Nov 19, 2024
5393e6c
Update ExampleJaxPEtab.ipynb
FFroehlich Nov 19, 2024
a22f099
fix compilation issue
FFroehlich Nov 19, 2024
a585414
Merge branch 'develop' into jax_export
FFroehlich Nov 19, 2024
c242b15
fix
FFroehlich Nov 19, 2024
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
Prev Previous commit
Next Next commit
fix python jax tests
FFroehlich committed Nov 12, 2024
commit f745be02b2b79a9ba5ad9c0bdb22a749d1df58c0
12 changes: 6 additions & 6 deletions python/sdist/amici/jax.py
Original file line number Diff line number Diff line change
@@ -119,15 +119,15 @@
)(p, pscale)

def _preeq(self, p, k):
x0 = self.x_solver(self.x0(p, k))
tcl = self.tcl(x0, p, k)
return self._eq(p, k, tcl, x0)

Check warning on line 124 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L122-L124

Added lines #L122 - L124 were not covered by tests

def _posteq(self, p, k, x, tcl):
return self._eq(p, k, tcl, x)

Check warning on line 127 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L127

Added line #L127 was not covered by tests

def _eq(self, p, k, tcl, x0):
sol = diffrax.diffeqsolve(

Check warning on line 130 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L130

Added line #L130 was not covered by tests
self.term,
self.solver,
args=(p, k, tcl),
@@ -139,7 +139,7 @@
max_steps=self.maxsteps,
event=diffrax.Event(cond_fn=diffrax.steady_state_event()),
)
return sol.ys

Check warning on line 142 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L142

Added line #L142 was not covered by tests

def _solve(self, ts, p, k, x0, checkpointed):
tcl = self.tcl(x0, p, k)
@@ -186,13 +186,13 @@
my: jnp.ndarray,
pscale: np.ndarray,
checkpointed=True,
dynamic=True,
dynamic="true",
):
ps = self.unscale_p(p, pscale)

# Pre-equilibration
if k_preeq.shape[0] > 0:
x0 = self._preeq(ps, k_preeq)

Check warning on line 195 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L195

Added line #L195 was not covered by tests
else:
x0 = self.x0(ps, k)

@@ -202,36 +202,36 @@
ts_dyn, ps, k, x0, checkpointed=checkpointed
)
else:
x = tuple(

Check warning on line 205 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L205

Added line #L205 was not covered by tests
jnp.array([x0_i] * len(ts_dyn)) for x0_i in self.x_solver(x0)
)
tcl = self.tcl(x0, ps, k)
stats = None

Check warning on line 209 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L208-L209

Added lines #L208 - L209 were not covered by tests

# Post-equilibration
if len(ts) > len(ts_dyn):
if len(ts_dyn) > 0:
x_final = tuple(x_i[-1] for x_i in x)

Check warning on line 214 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L213-L214

Added lines #L213 - L214 were not covered by tests
else:
x_final = self.x_solver(x0)
x_posteq = self._posteq(ps, k, x_final, tcl)
x_posteq = tuple(

Check warning on line 218 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L216-L218

Added lines #L216 - L218 were not covered by tests
jnp.array([x0_i] * (len(ts) - len(ts_dyn)))
for x0_i in x_posteq
)
if len(ts_dyn) > 0:
x = tuple(

Check warning on line 223 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L222-L223

Added lines #L222 - L223 were not covered by tests
jnp.concatenate((x_i, x_posteq_i), axis=0)
for x_i, x_posteq_i in zip(x, x_posteq)
)
else:
x = x_posteq

Check warning on line 228 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L228

Added line #L228 was not covered by tests

obs = self._obs(ts, x, ps, k, tcl)
obs = jnp.stack(self._obs(ts, x, ps, k, tcl), axis=1)
my_r = my.reshape((len(ts), -1))
sigmay = self._sigmay(obs, ps, k)
llh = self._loss(obs, sigmay, my_r)
x_rdata = self._x_rdata(x, tcl)
x_rdata = jnp.stack(self._x_rdata(x, tcl), axis=1)
return llh, (x_rdata, obs, stats)

@eqx.filter_jit
@@ -244,7 +244,7 @@
k_preeq: np.ndarray,
my: np.ndarray,
pscale: np.ndarray,
dynamic=True,
dynamic="true",
):
return self._run(
ts, ts_dyn, p, k, k_preeq, my, pscale, dynamic=dynamic
@@ -260,7 +260,7 @@
k_preeq: np.ndarray,
my: np.ndarray,
pscale: np.ndarray,
dynamic=True,
dynamic="true",
):
(llh, (x, obs, stats)), sllh = (
jax.value_and_grad(self._run, 2, True)
@@ -277,7 +277,7 @@
k_preeq: np.ndarray,
my: np.ndarray,
pscale: np.ndarray,
dynamic=True,
dynamic="true",
):
(llh, (x, obs, stats)), sllh = (
jax.value_and_grad(self._run, 2, True)
@@ -304,9 +304,9 @@
parameters: pd.DataFrame = None,
sensitivity_order: amici.SensitivityOrder = amici.SensitivityOrder.none,
):
cond_id, measurements_df = measurements
ts = _get_timepoints_with_replicates(measurements_df)
p = jnp.array(

Check warning on line 309 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L307-L309

Added lines #L307 - L309 were not covered by tests
[
pval
if isinstance(
@@ -319,51 +319,51 @@
for par in self.parameter_ids
]
)
pscale = jnp.array(

Check warning on line 322 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L322

Added line #L322 was not covered by tests
[
0 if s == petab.LIN else 1 if s == petab.LOG else 2
for s in parameter_mapping.scale_map_sim_var.values()
]
)
k_sim = np.array(

Check warning on line 328 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L328

Added line #L328 was not covered by tests
[
parameter_mapping.map_sim_fix[k]
for k in self.fixed_parameter_ids
]
)
k_preeq = np.array(

Check warning on line 334 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L334

Added line #L334 was not covered by tests
[
parameter_mapping.map_preeq_fix[k]
for k in self.fixed_parameter_ids
if k in parameter_mapping.map_preeq_fix
]
)
my = _get_measurements_and_sigmas(

Check warning on line 341 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L341

Added line #L341 was not covered by tests
measurements_df, ts, self.observable_ids
)[0].flatten()
ts = np.array(ts)
ts_dyn = ts[np.isfinite(ts)]
dynamic = "true" if len(ts_dyn) and np.max(ts_dyn) > 0 else "false"

Check warning on line 346 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L344-L346

Added lines #L344 - L346 were not covered by tests

rdata_kwargs = dict()

Check warning on line 348 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L348

Added line #L348 was not covered by tests

if sensitivity_order == amici.SensitivityOrder.none:
(

Check warning on line 351 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L350-L351

Added lines #L350 - L351 were not covered by tests
rdata_kwargs["llh"],
(rdata_kwargs["x"], rdata_kwargs["y"], rdata_kwargs["stats"]),
) = self.run(
ts, ts_dyn, p, k_sim, k_preeq, my, pscale, dynamic=dynamic
)
elif sensitivity_order == amici.SensitivityOrder.first:
(

Check warning on line 358 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L357-L358

Added lines #L357 - L358 were not covered by tests
rdata_kwargs["llh"],
rdata_kwargs["sllh"],
(rdata_kwargs["x"], rdata_kwargs["y"], rdata_kwargs["stats"]),
) = self.srun(
ts, ts_dyn, p, k_sim, k_preeq, my, pscale, dynamic=dynamic
)
elif sensitivity_order == amici.SensitivityOrder.second:
(

Check warning on line 366 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L365-L366

Added lines #L365 - L366 were not covered by tests
rdata_kwargs["llh"],
rdata_kwargs["sllh"],
rdata_kwargs["s2llh"],
@@ -372,17 +372,17 @@
ts, ts_dyn, p, k_sim, k_preeq, my, pscale, dynamic=dynamic
)

for field in rdata_kwargs.keys():
if field == "llh":
rdata_kwargs[field] = np.float64(rdata_kwargs[field])
elif field not in ["sllh", "s2llh"]:
rdata_kwargs[field] = np.asarray(rdata_kwargs[field]).T
if rdata_kwargs[field].ndim == 1:
rdata_kwargs[field] = np.expand_dims(

Check warning on line 381 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L375-L381

Added lines #L375 - L381 were not covered by tests
rdata_kwargs[field], 1
)

return ReturnDataJAX(**rdata_kwargs)

Check warning on line 385 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L385

Added line #L385 was not covered by tests

def run_simulations(
self,
@@ -393,12 +393,12 @@
simulation_conditions: pd.DataFrame = None,
measurements: pd.DataFrame = None,
):
fun = eqx.Partial(

Check warning on line 396 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L396

Added line #L396 was not covered by tests
self.run_simulation,
sensitivity_order=sensitivity_order,
parameters=parameters,
)
gb = (

Check warning on line 401 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L401

Added line #L401 was not covered by tests
[
petab.PREEQUILIBRATION_CONDITION_ID,
petab.SIMULATION_CONDITION_ID,
@@ -408,26 +408,26 @@
else petab.SIMULATION_CONDITION_ID
)

per_condition_measurements = measurements.groupby(gb)

Check warning on line 411 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L411

Added line #L411 was not covered by tests

order_conditions = [

Check warning on line 413 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L413

Added line #L413 was not covered by tests
tuple(c) if isinstance(c, np.ndarray) else c
for c in simulation_conditions[gb].values
]

sorted_mappings = [

Check warning on line 418 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L418

Added line #L418 was not covered by tests
parameter_mappings[order_conditions.index(condition)]
for condition in per_condition_measurements.groups.keys()
]

if num_threads > 1:
with ThreadPoolExecutor(max_workers=num_threads) as pool:
results = pool.map(

Check warning on line 425 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L423-L425

Added lines #L423 - L425 were not covered by tests
fun, sorted_mappings, per_condition_measurements
)
else:
results = map(fun, sorted_mappings, per_condition_measurements)
return list(results)

Check warning on line 430 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L429-L430

Added lines #L429 - L430 were not covered by tests


@dataclass
@@ -443,5 +443,5 @@
stats: dict = None

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__dict__ = self

Check warning on line 447 in python/sdist/amici/jax.py

Codecov / codecov/patch

python/sdist/amici/jax.py#L446-L447

Added lines #L446 - L447 were not covered by tests
56 changes: 41 additions & 15 deletions python/tests/test_jax.py
Original file line number Diff line number Diff line change
@@ -100,33 +100,29 @@ def _test_model(model_module, ts, p, k):

amici_model.setParameters(np.asarray(p, dtype=np.float64))
amici_model.setFixedParameters(np.asarray(k, dtype=np.float64))
edatas = (
amici.ExpData(sol_amici_ref, 1.0, 1.0),
amici.ExpData(sol_amici_ref, 1.0, 1.0),
)
for edata in edatas:
edata.parameters = amici_model.getParameters()
edata.fixedParameters = amici_model.getFixedParameters()
edata.pscale = amici_model.getParameterScale()
edata = amici.ExpData(sol_amici_ref, 1.0, 1.0)
edata.parameters = amici_model.getParameters()
edata.fixedParameters = amici_model.getFixedParameters()
edata.pscale = amici_model.getParameterScale()
amici_solver = amici_model.getSolver()
amici_solver.setSensitivityMethod(amici.SensitivityMethod.forward)
amici_solver.setSensitivityOrder(amici.SensitivityOrder.first)
rs_amici = amici.runAmiciSimulations(amici_model, amici_solver, edatas)
rs_amici = amici.runAmiciSimulations(amici_model, amici_solver, [edata])

check_fields_jax(rs_amici, jax_model, edatas, ["x", "y", "llh"])
check_fields_jax(rs_amici, jax_model, edata, ["x", "y", "llh"])

check_fields_jax(
rs_amici,
jax_model,
edatas,
edata,
["x", "y", "llh", "sllh"],
sensi_order=amici.SensitivityOrder.first,
)

check_fields_jax(
rs_amici,
jax_model,
edatas,
edata,
["x", "y", "llh", "sllh"],
sensi_order=amici.SensitivityOrder.second,
)
@@ -135,13 +131,43 @@ def _test_model(model_module, ts, p, k):
def check_fields_jax(
rs_amici,
jax_model,
edatas,
edata,
fields,
sensi_order=amici.SensitivityOrder.none,
):
rs_jax = jax_model.run_simulations(edatas, sensitivity_order=sensi_order)
r_jax = dict()
kwargs = {
"ts": np.array(edata.getTimepoints()),
"ts_dyn": np.array(edata.getTimepoints()),
"p": np.array(edata.parameters),
"k": np.array(edata.fixedParameters),
"k_preeq": np.array([]),
"my": np.array(edata.getObservedData()).reshape(
np.array(edata.getTimepoints()).shape[0], -1
),
"pscale": np.array(edata.pscale),
}
if sensi_order == amici.SensitivityOrder.none:
(
r_jax["llh"],
(r_jax["x"], r_jax["y"], r_jax["stats"]),
) = jax_model.run(**kwargs)
elif sensi_order == amici.SensitivityOrder.first:
(
r_jax["llh"],
r_jax["sllh"],
(r_jax["x"], r_jax["y"], r_jax["stats"]),
) = jax_model.srun(**kwargs)
elif sensi_order == amici.SensitivityOrder.second:
(
r_jax["llh"],
r_jax["sllh"],
r_jax["s2llh"],
(r_jax["x"], r_jax["y"], r_jax["stats"]),
) = jax_model.s2run(**kwargs)

for field in fields:
for r_amici, r_jax in zip(rs_amici, rs_jax):
for r_amici, r_jax in zip(rs_amici, [r_jax]):
assert_allclose(
actual=r_amici[field],
desired=r_jax[field],

Unchanged files with check annotations Beta

code = re.sub(r"numpy\.", r"jnp.", code)
return code
except TypeError as e:

Check warning on line 20 in python/sdist/amici/jaxcodeprinter.py

Codecov / codecov/patch

python/sdist/amici/jaxcodeprinter.py#L20

Added line #L20 was not covered by tests
raise ValueError(
f'Encountered unsupported function in expression "{expr}"'
) from e