perf: lazy-load heavy deps to speed up CLI startup#142
Open
lwalew wants to merge 1 commit into
Open
Conversation
Importing mlipaudit.benchmarks (which the CLI does to build the argument
parser) eagerly pulled in mlip.models, JAX/JAX-MD and scikit-learn, making
even `mlipaudit -h` take ~6s. These libraries are only needed when a
benchmark is actually run.
Defer those imports to call time:
- benchmark.py / utils.{inference,simulation,stability,unallowed_elements}:
move mlip.models / mlip.simulation.ase (JAX-MD) / jax / sklearn imports
into the functions that use them; type-only refs go under TYPE_CHECKING
with `from __future__ import annotations`.
- Move ASESimulationEngineWithCalculator into utils/_ase_engine.py so the
heavy ASE-engine base class is only imported when a simulation runs; keep
the historical import paths working via module-level __getattr__.
- nudged_elastic_band / conformer_selection / dihedral_scan /
water_radial_distribution: defer their mlip / sklearn imports.
- main.py: import launch_app / run_benchmarks inside their command branches.
Tests patched the now-lazy symbols at the use-site module; update them to
patch at the definition site (e.g. mlip.simulation.jax_md.JaxMDSimulationEngine),
which the lazy `from ... import` picks up at call time.
`mlipaudit -h` drops from ~5.6s to ~0.9s; importing mlipaudit.benchmarks
drops from ~5.0s to ~1.1s. All 128 tests pass.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
d317e03 to
d8a4bb2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
mlipaudit -htakes ~6s. The cost is entirely import time, not argument parsing:main.pyimportsmlipaudit.benchmarks(needed to build the--benchmarkschoices for the parser), and that transitively pulls in the full ML/plotting stack just to print a help string:mlip.models(ESEN network, flax)Benchmarkbase /ForceFieldtype hintsmlip.simulation.ase→jax_md.rigid_bodyutils.simulationenginesklearn.metricsimport jaxutils.stabilityNone of this is needed to list benchmarks or print help — only to actually run a benchmark.
What
Defer the heavy imports to call time:
benchmark.py,utils/{inference,simulation,stability,unallowed_elements}.py: movemlip.models/mlip.simulation.ase(JAX-MD) /jax/sklearnimports into the functions that use them; type-only references go underTYPE_CHECKINGwithfrom __future__ import annotations.utils/_ase_engine.py(new):ASESimulationEngineWithCalculatorsubclasses the heavyASESimulationEngine, so a class definition can't be lazy. Moved it to its own module imported lazily when a simulation runs; the historicalmlipaudit.utils[.simulation].ASESimulationEngineWithCalculatorpaths still work via module-level__getattr__.nudged_elastic_band,conformer_selection,dihedral_scan,water_radial_distribution: defer theirmlip/sklearnimports into the run/analyze methods.main.py: importlaunch_app/run_benchmarksinside their command branches.Tests
The benchmark tests patched the now-lazy symbols at the use-site module (e.g.
mlipaudit.utils.simulation.JaxMDSimulationEngine), which no longer exists there. Updated them to patch at the definition site (e.g.mlip.simulation.jax_md.JaxMDSimulationEngine) — the lazyfrom ... importpicks the patch up at call time, andcreate_autospecinconftest(which binds the real class at import) is unaffected.Result
mlipaudit -h: ~5.6s → ~0.9simport mlipaudit.benchmarks: ~5.0s → ~1.1sNotes
mlipaudit benchmark -hhas a pre-existing argparseAssertionError(unrelated to this change; reproduces onmain) and is left as-is.🤖 Generated with Claude Code