Skip to content

feat!: default convert_to_format to rust - #5966

Open
BradyPlanden wants to merge 12 commits into
stack/10b-graph-inspectionfrom
stack/11-default-rust
Open

BradyPlanden wants to merge 12 commits into
stack/10b-graph-inspectionfrom
stack/11-default-rust

Conversation

@BradyPlanden

@BradyPlanden BradyPlanden commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Description

Models default to convert_to_format = "rust" instead of "casadi" (BaseModel._DEFAULT_CONVERT_TO_FORMAT). Every model is lowered to the Rust core unless it opts out with model.convert_to_format = "casadi". With the rest of the stack:

  • the default IDAKLUSolver solves and reads rust models in Rust;

  • ScipySolver, AlgebraicSolver and EIS evaluate them through compiled functions;

  • CasadiSolver and CasadiAlgebraicSolver convert them to CasADi, logging the conversion at INFO for a model left at the default format and at WARNING for a format chosen explicitly, so the deprecated solvers' DeprecationWarning stays the one warning a default model sees.

  • DiffsolSolver: overriding a model's chosen format now warns, naming the model and the format it replaces, where it logged at INFO.

  • Serialisation: saving falls back to the default format, and a file saved without a convert_to_format key loads as "rust".

  • Tests:

    • Tests that exercise CasADi on purpose pin "casadi": the CasADi solver suites (through autouse fixtures), CasADi leaves and pickles, AOT compilation, the lead-acid comparisons, the IDAKLU sparse-output sensitivity guard, and single tests in ten more files, among them the bitwise CasADi parity of input-dependent discontinuity times. Among those, test_multiple_inputs keeps covering CasADi with fewer solvers than threads.
    • standard_output_tests raises its absolute tolerance from 2e-14 to 5e-14, since round-off differs by backend.
  • Docs:

    • A new user guide page, user_guide/rust_backend.rst, covers choosing the backend, its differences from CasADi, DiffsolSolver's output times and Hermite reads, interrupting a solve, and adding a new expression type. The Ctrl-C and UnsupportedExpressionError paragraphs describe feat(rust): interpolant tables over input parameters and expressions #6134, which lands before this PR.
    • The multithreading notebook gains a prose section on num_threads across the backends.
    • rust_idaklu_build.md and the pybamm-rust README point to the guide.

What raises on the Rust backend:

  • IDAKLUSolver raises SolverError for sensitivities with respect to an input of width above 1, and solves with num_solvers equal to num_threads, as the guide says.
  • return_jacp_stacked and TensorField output variables also raise SolverError.
  • A symbol that cannot be lowered raises TypeError or NotImplementedError, and an interpolant table Rust refuses raises ModelError.
  • There is no automatic fallback to CasADi.

Performance

heatmap_light sweep_drive_cycle_light sweep_output_drive_cycle_light

A release needs pybammsolvers 0.14.0 on PyPI (#5961) and the platform wheels from #5958.

Type of change

Breaking change, under ## Breaking changes in CHANGELOG.md, with the migration note "Set model.convert_to_format = "casadi" to restore the previous backend."

Key checklist

  • No style issues: pre-commit run
  • Unit tests pass (N), as do the integration tests (N)
  • The documentation builds: nox -s doctests
  • Tests: the existing suites run on the Rust default, and the tests listed above pin "casadi". The CasADi solvers' conversion test, from feat(rust): evaluate rust models through compiled evaluators #5960, now checks the log level for a default and a chosen format
Rust execution engine stack: proposal #5667, showcase #5732

Each PR builds on the one above it.

# PR Adds
0 #5954 Function.create_copy keeps the function's class
0b #5955 CasADi's RegPower and uniform-grid interpolant lookups match evaluate
1 #5956 pybamm-core: expression graph, typed IR, interpreters, forward-mode AD
2 #5957 pybamm-core: CompiledModel, coloured Jacobians, the C ABI
3 #5958 pybamm.rust bindings, built into the pybamm wheel
4 #5959 Symbol.to_rust
5 #5960 convert_to_format = "rust" for ScipySolver, AlgebraicSolver and EIS
6 #5961 pybammsolvers: IDAKLU over the C ABI
7 #5962 IDAKLUSolver on rust models
8 #5963 variables and sensitivities read in Rust
9 #5964 pybamm-core: diffsol DAE solver
10 #5965 pybamm.DiffsolSolver
10a #6134 Interpolant over input parameters and expressions
10b #6138 pybamm.rust's own errors, array-likes, threads and Ctrl-C
10c #6135 pybamm.rust.inspection; every tape derived in one place
11 #5966 "rust" as the default convert_to_format ← this PR

@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@codecov

codecov Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (stack/10b-graph-inspection@a52b8c7). Learn more about missing BASE report.

Additional details and impacted files
@@                      Coverage Diff                      @@
##             stack/10b-graph-inspection    #5966   +/-   ##
=============================================================
  Coverage                              ?   98.44%           
=============================================================
  Files                                 ?      369           
  Lines                                 ?    39875           
  Branches                              ?        0           
=============================================================
  Hits                                  ?    39255           
  Misses                                ?      620           
  Partials                              ?        0           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@BradyPlanden
BradyPlanden force-pushed the stack/11-default-rust branch from cef6708 to 18aae11 Compare October 28, 2026 09:55
@BradyPlanden
BradyPlanden force-pushed the stack/11-default-rust branch 2 times, most recently from 58c95d5 to 2ef0d3e Compare October 28, 2026 14:11
@BradyPlanden
BradyPlanden force-pushed the stack/11-default-rust branch from 2ef0d3e to 9fbc37e Compare October 28, 2026 15:28
@BradyPlanden
BradyPlanden force-pushed the stack/11-default-rust branch from 9fbc37e to fd167d2 Compare October 29, 2026 08:01
@BradyPlanden
BradyPlanden marked this pull request as ready for review October 29, 2026 10:10
@BradyPlanden
BradyPlanden requested a review from a team as a code owner October 29, 2026 10:10
@BradyPlanden

Copy link
Copy Markdown
Member Author

I'm marking this ready-for-review to get benchers results with rust as the default backend. It should not be merged until the rust backend is verified as stable.

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

🐰 Bencher Report

ProjectPyBaMM
Branchstack/11-default-rust
Testbedbare-metal

⚠️ WARNING: Truncated view!

The full continuous benchmarking report exceeds the maximum length allowed on this platform.

🚨 1 Alert

🐰 View full continuous benchmarking report in Bencher

🔗 Link this project to GitHub

BaseModel defaults to "rust"; CasADi-specific tests pin their format;
user guide and multithreading docs describe the rust backend.
The Rust backend refuses a symbol it cannot lower with `NotImplementedError`.
The Rust backend guide listed events among DiffsolSolver's features without
saying that, unlike IDAKLUSolver's since #6056, they cannot read the state
time derivatives.
DiffsolSolver overrode a model's `convert_to_format` in place with only an
info log. Now that Rust is the default, any other format was chosen, so
overriding it warns, naming the model and the format replaced.
It said DiffsolSolver's sensitivities are always under error control,
now an option, and that IDAKLU's off-grid reads beat diffsol's on a coarse
`t_interp`, where IDAKLU drops its Hermite data and reads linearly. It
now also says when diffsol drops its own, that `on_failure` must be
"error", and that converting another format warns.
A Rust model was lowered several times over. Each integrator compiled
its CompiledModel through its own copy of the recipe, NonlinearSolver
compiled a second one for its Newton step, and process() gave every
symbol it compiled a graph of its own: the initial conditions,
discontinuity times and step state mappers of every solve, and every
part a solver other than IDAKLU read. Two class flags chose the path.

A model now keeps one RustModelLowering, made by RustModelLowering.of
and kept while its input names stay the same; a copy or pickle of the
model drops it. BaseSolver.set_up compiles the model's CompiledModel on
it for every solver: the residual, the algebraic block of a DAE, the
termination events, the df/dp of any sensitivities, the output
variables and the upsampling output a solver's _upsampling_variable
names. The generic rhs_eval, algebraic_eval and rhs_algebraic_eval,
their Jacobians and the termination events are views of it, as
RustEvaluator now takes a view's rows and a prepared Jacobian.
process(), the EIS output Jacobian and NonlinearSolver compile on the
same graph, and NonlinearSolver reuses the model's CompiledModel rather
than compiling its own. A model without states compiles over an empty
mass matrix.

A ScipySolver set-up of an SPM builds one graph rather than six. A DFN
set up for a Newton initialisation compiles one CompiledModel rather
than two, in 2.7 ms rather than 4.4 ms.
The guide's account of how a model compiles now says the graph fixes its inputs, and that a set-up compiles the CompiledModel whole or not at all.
…ough of

The guide claimed a failed set-up leaves the model as it was; it keeps
the evaluators of its last set-up, as the initial conditions are set
before the CompiledModel compiles. The Newton tests now make their
lowerings with RustModelLowering.of, as source does.
…onditions a graph of their own

The lowering kept the last compile's output lengths and sensitivity
names for IDAKLU and Diffsol to read back, so two solvers setting up
one model at once could read each other's, and it bound the model's
evaluators itself. Initial conditions compiled on the model's graph,
which grew by one node, carried by every pickle, for each initial
state a sweep tried.

compile_model now keeps only the CompiledModel, for the Newton step to
reuse. BaseSolver._set_up_rust_model compiles it, binds the model's
evaluators as its views, and hands it to the solver's
_prepare_integrator, which IDAKLU and Diffsol implement in place of
_set_up_rust and _prepare_solver; output lengths come from the
CompiledModel's outputs and sensitivity names from the model. Initial
conditions, which change from solve to solve, compile on a graph of
their own. IDAKLU refuses a model without states as it checks the
model, before anything compiles, and a Rust set-up returns before the
CasADi path's preparation.
Above 4096 output columns a solve stopped storing yp, so off-grid reads
fell from cubic to linear without saying so. The reason given, that a
dense grid's chord is already at the integration error floor, depends on
the model's timescales against the span, not on the column count, and
IDAKLU has no such cap. hermite_interpolation=False remains the way to
save the memory.
A single solve could factorise its sparse LU on num_threads workers of a
shared pool, documented as worthwhile from about 50k states. Timed on
Chen2020 DFNs of 962, 8262 and 50652 states and an SPM, 2, 4 and 8
threads were never faster than one, and on the 50652-state DFN 4 and 8
threads were 12% and 24% slower. IDAKLU's num_threads only solves input
sets in parallel, so DiffsolSolver's now does the same.

SolveRequest loses lu_threads, PreparedSolver.solve loses num_threads,
and the solve loses the lu_par it carried through Run, Recorder and its
LU helpers. Every LU still runs at an explicit serial Par, since a host
may disable faer's global parallelism.
…e Rust backend guide

The guide arrives with the switch to Rust by default, after the Ctrl-C
handling and pybamm.rust's exception classes it now describes.
@BradyPlanden
BradyPlanden force-pushed the stack/11-default-rust branch from ab67b6f to 6bc0a4d Compare December 4, 2026 10:47

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant