LLM-guided discovery of practical algorithms for least-squares.
This is the code for the paper
Discovering Practical Algorithms for Least Squares using Language Models.
Standard least-squares solvers are designed for the worst case: to
be stable on every possible input. AutoDiscoLS instead searches for the fastest algorithm for a specific
family of problems, min_x ||Ax - b||, at a specific accuracy target eps. The search is LLM-guided
program evolution with ShinkaEvolve: an LLM repeatedly edits a
Python program, and each candidate is scored by its measured runtime relative to the initial program. The
initial program is timed alongside every candidate, interleaved with it on the same instance, as a frozen
copy called the anchor. The accuracy target bounds the relative residual: ||Ax - b|| <= (1 + eps) * r*,
where r* is the optimal residual. A candidate that misses it on any training instance, in any repeat,
ranks below every candidate that reaches it.
A study runs in three phases:
- prep builds two bundles of least-squares instances: a training bundle for
the search and a test bundle of held-out test instances for the report. The two never share an instance,
and each instance's
r*is computed once here. - discover (the discovery phase) runs ShinkaEvolve on the training instances: the LLM proposes programs, each is timed against the anchor and checked against the accuracy target, and the best becomes the champion.
- report times the champion against the baselines on the held-out test instances, which the search never saw, and writes runtimes and speedups with confidence intervals.
The programs discovered for the paper's tasks, and the ShinkaEvolve configuration that found them, are in
discovered/.
Python ≥ 3.11:
python -m venv .venv && source .venv/bin/activate
pip install -e '.[dev]'
echo 'OPENROUTER_API_KEY=<your key>' > .env # or OPENAI_API_KEY, GEMINI_API_KEY, ...The search makes paid API calls, capped by pipeline.total_budget_usd.
examples/least_squares_tiny/ is a complete study that runs on a laptop
in minutes:
EX=examples/least_squares_tiny
bash $EX/prep.sh # prep: build the bundles
auto-disco-ls plan --config $EX/configs/config.yaml # check the study (free)
auto-disco-ls report --config $EX/configs/config.yaml --champion $EX/initial_program.py # (free)
auto-disco-ls all --config $EX/configs/config.yaml # discover, then report (< $1)The free report times the initial program against the baselines; its self-check line should read
close to 1.0.
The example exercises the pipeline, not the search: its matrices take milliseconds to solve, so on a laptop a candidate's score varies by about ±0.15 from run to run (a score of 0.15 means about 1.1x the initial program's speed), and the initial program can end up as the champion. To see real speedups, use larger matrices, more generations and an idle machine.
auto-disco-ls submit --config examples/least_squares_tiny/configs/config.yaml \
--prep --partition <partition> [--account <account>] [--dry-run]This submits prep -> discover -> report as chained jobs; the report job takes a whole node. See
auto-disco-ls submit --help for time and memory limits.
Copy examples/least_squares_tiny/ and edit it:
| file | what it is |
|---|---|
initial_program.py |
the initial program; the LLM evolves solve(A, b, eps, iter_budget) inside its EVOLVE-BLOCK |
anchor_lsqr.py |
the anchor: a frozen copy of the initial program; every score is relative to it |
program_run.py |
the hidden harness that times and grades each program |
evaluate.py |
turns the timings into the search's score |
configs/config.yaml |
the study's settings and the LLM's task prompt; every key is commented |
prep.sh |
builds the training and test bundles |
Any config key can be overridden with --set key=value. Bundles are built with
python -m autodiscols.benchmark (see --help); give the training and test bundles different --seed ranges.
Baselines for report, plus the study's anchor under its anchor_name:
| name | in the paper | algorithm |
|---|---|---|
sparse_lsqr |
LSQRsparse | OSNAP sparse sketch (d = 3n, 8 nonzeros per column) + preconditioned LSQR |
blendenpik_warm |
Blendenpik | randomized DCT sketch (d = 2n) + preconditioned LSQR, warm-started |
normal_chol |
Cholesky | normal equations + Cholesky |
scipy_gels |
QR | Householder QR, LAPACK gels (defines r*) |
To discover an algorithm for your own problems, point the config at your matrices and write your own prompt. Prep is then not needed, and report is optional:
pipeline:
bundles:
train: /path/to/my_matrices # a folder of .npy, .npz or .mtx files, or a single file
# test: /path/to/held_out # optional; without it there is no report phase
task_file: task.md # the LLM's prompt, replacing evolution.task_sys_msg- An
.npzfile holdsAand optionallyb; an.npyor Matrix Market.mtxfile holdsAalone. A matrix withoutbgets a random one with a known optimal residual. Sparse matrices are stored dense. - The first command that reads the matrices,
planincluded, imports them into the study'sbenchmarks/imported/folder and computes eachr*; they are imported again only if the files change. - The test set must not share any matrix with the training set.
Then run plan and discover as usual; all and submit skip the report when there is no test set.
Everything is written inside the study folder:
| path | contents |
|---|---|
results/<id>/discover_<timestamp>/ |
the ShinkaEvolve run |
champions/<id>/champion.py |
the best program found, with discover.json (score and API spend) |
champions/<id>/report_*.{json,csv,png} |
runtimes, accuracy and speedups with confidence intervals |
See docs/timing.md for how programs are timed and scored. Run the tests with
python -m pytest tests -q.
Released under the Apache License 2.0.
If you use this code, please cite the paper:
@article{pirazzini2026discovering,
title = {Discovering Practical Algorithms for Least Squares using Language Models},
author = {Pirazzini, Marco and Lange, Robert Tjarko},
journal = {arXiv preprint arXiv:2611.40024},
year = {2026}
}