stan-dev / stan-dev/stanc3

Add `std::move` optimization to last use of matrix and array like objects

Open
#1,721 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature optimization
Dominant language
OCaml
Stars
160
Forks
59
Avg merge
21h 45m
Merged PRs (30d)
26

Description

Given a model like below where we have a large N*N parameter matrix, it would be useful to be able to tell the stan math functions about the last place the matrix is used.

parameters {
  matrix[N, M] big_mat;
  matrix[M] big_vec;
  vector[N] sigma;
}

model {
 // Do other things here 
 // ...
 // last use of each parameter
 y ~ normal(big_data * big_mat * big_vec, sigma);
}

In the c++ it would be nice if we could use dependency analysis to detect the last time a matrix / array like object is used and add additional metadata flagging that. Then the C++ can write the following

lp_accum__.add(
  normal_lpdf(
    multiply(multiply(big_data, std::move(big_mat)), std::move(big_vec)),
    std::move(sigma)
  )
);

Internally, stan math can then store that memory instead of copying it over to the arena. The arena_matrix type has a special rvalue overload where it does not actually copy the memory of objects over to the stack allocator, but just stores the data in a place that it can be deleted once the user calls recover_memory(). So in most cases this optimization would save space on the stack allocator and remove a copy.

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

The issue names no implementation files or tests; start with the Stan model and the generated C++ example using std::move. Done means dependency analysis identifies last uses of matrix- and array-like objects and emits the move metadata so Stan Math can avoid copying their memory into the arena.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.