<edited Nov 25: knowing the data at “compile” time helps stanli less than I thought>
Hi everyone,
I’m seeing people start to pick up Stanli and link to this thread, so I thought I’d provide an update of the last 3.5 months of Stanli development since my first post. A lot of people are rightly asking how an interpreted version can be faster. The tl;dr is that Stanli does a ton of compiler optimizations on the compute graph. Most of them could move upstream to stanc3 over time and work about as well there. Knowing the data adds some huge wins on top, but only on a minority of models (~1.7x geomean on the 89 models data affects).
Stanli is still a work in progress, but it’s come SO far :) LLM-generated table illustrating:
| 0.6.1 (first post) | 0.30.1 (now) | |
|---|---|---|
| models benchmarked | 119 (posteriordb) | 363 (posteriordb, 124 brms models, rstanarm, Rethinking, and models people sent in) |
| median gradient vs CmdStan | 2.07x faster | 2.16x faster |
| models at least as fast as CmdStan | 100 of 119 | 346 of 362 |
| stanc3 test models verified | 10 | all 1,382, about 85 still differ (mostly unimplemented features) |
For the median model, a full sampling run (setup plus 1,000 warmup and 1,000 draws) is 23x faster than CmdStan, mostly because there’s no C++ compile. @stevebronder’s point about --O1 was a good one, so the benchmarks now also compare against CmdStan built with --O1 and loop vectorization on. Against that, stanli is 1.90x faster at the median. That said, there are still bugs in --O1 that are fixed in my fork that would prevent some users from using or trusting it.
Why it’s faster
An LLM turned each of stanli’s optimizations off one at a time, and then all together, across the 363 models (about 28,000 timed runs). tl;dr:
- With every optimization off, stanli runs at about CmdStan’s speed (1.08x faster at the median). So the fixed graph with preallocated memory only makes up for the cost of being an interpreter.
- The speedup comes from a handful of passes: re-rolling scalar loops into vector ops, sharing repeated work, fused density kernels, hoisting data-only code, and generated reverse passes for the small pieces that can’t be vectorized. Each one is worth 1.3x to 2.3x on the models it applies to. There isn’t one big trick.
- stanc3’s
--O1optimizer and loop vectorization add about 3% on top of that (stanli now uses the optimized MIR, as Steve suggested). stanli’s own passes already do most of what it does, plus things that need the data.
I expected the data specialization that we all talked about years ago to be most of the win (cc @rybern @Matthijs @WardBrian et al), but it doesn’t affect that many of the corpus models in practice. I had the LLM split out the wins that a compiler couldn’t get without the data’s values or sizes, like merging terms with equal data or grouping observations by a branch on data. Those are worth only 1.14x in geomean. A data-blind stanli would still be roughly 2.0x faster than CmdStan at the median. On the 89 models where the data is used it’s 1.7x in geomean, and a few go from CmdStan’s speed to 100x+ faster (e.g. an ordinal model with 7 outcomes over 9,930 rows becomes 7 terms times their counts). You need an interpreter, or something like one, to get those without recompiling every time the data changes.
The models that gain the most are regressions, GLMs and IRT models with many independent observations. Models dominated by dense linear algebra, ODEs or long sequential recurrences spend their time in Stan Math either way so they gain the least. GARCH and the HMMs are within 1.1x of CmdStan, and the ODE models are 1.6x to 1.9x faster.
For the full ablation test attempting to attribute the perf wins in Stanli to particular compiler passes and optimizations, see this LLM write up.
New since this post
- R:
install.packages("stanli", repos = "``https://seantalts.r-universe.dev``"), no toolchain needed. brms now hasbackend = "stanli"built in (thanks Paul!), and @StaffanBetner 's helpers cover moment-matched LOO. - Samplers and methods: NUTS with diagonal, dense or unit metrics and optimization in Python and R. R also has ADVI, Pathfinder, Laplace sampling and
bridge_sampler(). nutpie and walnutpie can sample stanli models through a BridgeStan-compatible C API. - Language: tuples finally (heh), complex numbers, the Laplace functions, and about 30 more library functions (
hmm_*,csr_*,qr_*,hypergeometric_*and others). - Threads:
reduce_sumruns in parallel within a chain. - Wheels for macOS, Linux and Windows, an npm package for browsers and Node, and a webR build.
- Some of the loop vectorization work also went upstream into stanc3 (stan-dev/stanc3#1683). There are still issues with the experimental stanc3 -O1 optimizations that I’ve fixed in my fork and I try to post most of those as sample model issues upstream.
All timings are on an M3 Ultra, one thread per chain. Issues and odd models are very welcome on GitHub: GitHub - seantalts/stanli: stanli, the Stan Language Interpreter: op-graph executor over precompiled stan-math kernels. Compile and sample Stan models with no C++ toolchain. · GitHub