stan-dev / stan-dev/stanc3

Compile math library ahead-of-time and create a Stan interpreter

Open
#254 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

big-exciting-project
Dominant language
OCaml
Stars
160
Forks
59
Avg merge
21h 45m
Merged PRs (30d)
26

Description

(I'm going to try to start writing up issues for some of the ideas I have recorded over the past year or so for amazing things we can do with the new compiler infrastructure here. I'll apply this big-exciting-project label (open to new label names too 😅)).

So it is well known by Stan folks that compiling the entire supported Math library ahead of time is completely intractable, primarily because the number of vectorized and unvectorized types we support for every heavily templated function causes a combinatoric explosion in the amount of generated code. I've heard reports that just pieces of the math library compiled like this result in 100s of GBs of compiled code.

However, now we might actually be able to compile just a very small subset of the Math library where we just choose to e.g. only support Eigen types and no non-vectorized versions of functions. Then we can have a new backend change the Stan source code to move all scalars into 1-wide Eigen vectors; move all real[] to vector, and so on. There will be a few hangups with this:

  1. Obviously there will be some additional allocations, but I hope it will be quite low and possibly optimized by the compiler. And once we have an EigenBase refactor of the Math library, we can use fixed size Eigen types on the stack without allocating, which should take care of most of the rest of this issue.
  2. A couple of Math library functions were specifically written to only support std::vector<double>. We will have to either extend those or have the compiler translate before and after.

If anyone else can comment on the other potential pitfalls with this approach I'd really appreciate it. I'll try to keep this top-level comment up to date with additional content. //cc @bob-carpenter @bgoodri @syclik

Here's why this would be awesome:

  1. Standard wins due to the immediate feedback cycle during Stan and Stan model development. These are pretty well-documented elsewhere [citation needed].
  2. We'd be able to distribute a version of Stan / RStan / PyStan that did not need any C++ compiler or toolchain at runtime(!!!!)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files, tests, or entry points are named. Start by mapping the existing compiler infrastructure and Stan Math integration, then assess the proposed ahead-of-time subset and source transformation from scalars and real[] to Eigen vectors. Done would require an agreed design, an interpreter or backend implementation, and evidence that supported Stan models can run without a runtime C++ toolchain.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, ocaml
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
15/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.