stan-dev / stan-dev/stanc3

[FR] `reduce_sum` does not block `SoA`

Open
#1,236 3 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

Current Behavior:

Stan models using reduce_sum cannot benefit from the SoA optimisation whenever reduce_sum is used with a data only slicing argument.

Expected Behavior:

If the argument to be sliced for reduce_sum is data only, then SoA should work just fine, given that the shared arguments are simply copied as a whole. Moreover, whenever the slicing argument is an array of parameters, then SoA should also work given that an array ( std::vector) is being chopped into pieces.

Model Code
Model which exhibits the issue
  // likelihood including constants
  if (!prior_only) {
    // reduce_sum is blocking SoA, but it should not since the slice
    // argument is data in this case
    //target += reduce_sum(partial_log_lik_lpmf, seq, grainsize, Y, Xc, b, Intercept, sigma, J_1, Z_1_1, r_1_1);
    // this call instead gives me a full SoA program
    target += partial_log_lik_lpmf(seq| 1, N, Y, Xc, b, Intercept, sigma, J_1, Z_1_1, r_1_1);
  }
Environment:

Stan 2.30.0

Anything else:

Here is the full Stan model, which is a minor modification of a brms generated one. So the goal is to get brms to create reduce_sum programs which can benefit from SoA in addition to within-chain parallelisation.

soa-example-brms-soa_stan.txt

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

Start by reproducing the behavior with the attached soa-example-brms-soa_stan.txt model and the reduce_sum call shown in the issue. Trace how reduce_sum and SoA are handled in stanc3, then verify that data-only and array-of-parameters slicing no longer prevents SoA while the model still supports within-chain parallelisation.

Written by the indexing model from the issue text.

Assessment

Tech stack
ocaml
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.