Stuck at Warmup iteration with no error : CmdStanR

Just be aware that the reduce_sum interface has changed in like the last two days. The official release for that is next Monday I think, so you might have working code that needs updated.

Also, I see you’ve got a large model in mind and are trimming it down to fit. Can you work the other direction? Start with something small and expand to the point where you’re happy?

The difficulty with scaling down is you never get the opportunity to check the assumptions you’re making (cause you presumably can’t run fits – if you could you wouldn’t need to scale down). If you’re scaling up from a small model you can check how things are doing.

1 Like

just hit this now - @bbbales2 is there some branch of stanc3 that is correct? trying to compile the example in your case study and getting a compiler error. I’m using the latest nightly build of stanc3 - no luck.

I used cmdstanr and installed cmdstan from develop. I just merged in the updated stuff which was in a pull request. Ignore the readme: https://github.com/bbbales2/cmdstan_map_rect_tutorial

1 Like

I suspect you have an old version. I just merged in the updates. They were on a branch.

3 Likes

right - this works!

> library("cmdstanr")
> set_cmdstan_path(file.path('~','github','stan-dev', 'cmdstan'))
CmdStan path set to: /Users/mitzi/github/stan-dev/cmdstan
> logistic1 <- cmdstan_model("logistic1.stan", threads = TRUE)
Compiling Stan program...

(once again, both CmdStanR and CmdStanPy are CmdStan install agnostic - so nice for testing!)

1 Like

Thanks so much that sounds like a much better approach to what I am currently doing.
I will scale up from small model incrementally and check the posterior predictive graphs along the way.

thanks for sharing that you faced compile error as well. I spend the day trying to find out why I am not able to compile the example. It compiles correctly now.

@bbbales2 does the Monday refer to 20th April USA time ?

Yeah that’s what I meant but it looks like it might getting pushed back a little. Vaguely sometime next week I think, sorry to be unclear lol.

1 Like

There’s a new release candidate available here: https://github.com/stan-dev/cmdstan/releases/tag/v2.23-rc2

@sam_learner please let us know is this works for you

1 Like

I am testing it now. thanks for sharing the case study on the 2.23-rc2 announcement page.

@bbbales2 update for 2.23 test : I am getting errors while compiling the logistic1.stan. Would you be able to please help me with identifying what might be going wrong as it will help me in testing out my code as well. The logistic0.stan runs perfectly and generates the same output as shown in the case study.

I have set the following values in make/local file in ~/.cmdstanr/cmdstan/make and ~/cmdstan-2.23-rc2/make:

CXXFLAGS += -DSTAN_THREADS
CXXFLAGS += -pthread
TBB_CXX_TYPE=gcc

I am attaching the stack trace for logistic1.stan and output file for logistic0.stan for reference. capture_logistic0.txt (2.2 KB) capture_logistic1.txt (2.7 KB)

CmdStanR is not using the correct version of Cmdstan.
in CmdStanR, try cmdstan_path() function.
it probably isn’t pointing to 2.23.rc-2

logistic0.stan will work with any version of CmdStan because is doesn’t call reduce_sum. logistic1.stan is the rewrite that does, and therefore requires 2.23.

1 Like

If your cmdstan is at

Do set_cmdstan_path("~/cmdstan-2.23-rc2") and try again (or set_cmdstan_path("./home/saumyap/cmdstan-2.23-rc2") if it doesn’t work with the ~).

1 Like

@bbbales2 @mitzimorris thanks so much for helping out with the error. It was indeed due to the cmdstan_path not set properly. I don’t know why but set_cmdstan_path("~/cmdstan-2.23-rc2") was not working with the ~ but specifying ./home made it work.

The speedup is massive when using reduce_sum. From 283.1 seconds for each chain to 39.7 seconds:) I am so excited to get reduce _sum to work on my code as well. Big thanks to all the Stan Developers for all the hardwork :)

2 Likes

Just make sure and double check you’re matching results with the serial version.

This is handy for doing that: https://github.com/jgabry/posterior

If you have two fits (f1 and f2) in cmdstanr you can do:

library(posterior)
bind_draws(f1$draws(), f2$draws(), along = "chain") %>%
  summarise_draws()

To stick them together and make sure things are mixing.

Edit: Added library load

1 Like

thanks so much @bbbales2 I will definitely check the serial results with parallel.

question :
1. If I have multiple likelihood statements in my model block, I am not sure how a function for each likelihood statement must be written using the partial_sum. I referred https://github.com/stan-dev/docs/blob/master/src/functions-reference/higher-order_functions.Rmd but I couldn’t get answer of my question here. I want to do something like the following and I know it is not allowed to have multiple functions with same signatures but I wanted advice on approaching this. Currently I am getting following error (Stack trace attached for reference capture_testparallel.txt (2.7 KB)).
I don’t have any prior experience of using functions in Stan and it is possible my question is very stupid

 Identifier 'partial_sum' is already in use.
functions {
  real partial_sum(int[] slice_location,int start, int end, matrix theta_loc_mu) {
    return categorical_lpmf(slice_location | softmax(to_vector(theta_loc_mu[start:end])));
  }
  
  real partial_sum(int[] slice_rating,int start, int end, matrix theta_rat_mu) {
    return categorical_lpmf(slice_rating | softmax(to_vector(theta_rat_mu[start:end])));
  }
  
  real partial_sum(int[] slice_domain,int start, int end, matrix theta_dom_mu) {
    return categorical_lpmf(slice_domain | softmax(to_vector(theta_dom_mu[start:end])));
  }
  
}



2. As per the suggestions in the tutorial I want to minimise computation in transformed parameters , however I am having some doubts. (Original serial code attached here tptrain.stan (2.7 KB)

Code : If I want to do something like the following I am not sure how to mention the dimensions of the matrix s and matrix eta_s_rating in the partial_sum function signature. The dimensions are different so I can’t use start and end values (please see original code parameters and transformed paramters block for reference

functions {
  real partial_sum(int[] slice_location,
                             int start, int end, 
                              vector attitude, 
                              row_vector eta_attitude_rating, 
                              matrix s, 
                              matrix eta_s_rating ) {
    return categorical_lpmf(slice_location | softmax(to_vector(attitude* eta_attitude_rating + s *eta_s_rating)));

// dimensions of the matrix
// matrix[N, K]  s
// matrix [K,R] eta_s_rating;
// row_vector[R] eta_attitude_rating;

  }

I would be extremely grateful if you could please spare few minutes to guide me on these

Identifier 'partial_sum' is already in use.

Oh whoops, maybe we should change this to make it clear in the tutorial, but you can call the functions whatever you want. So do something like:

  real myfunction1(int[] slice_location,int start, int end, matrix theta_loc_mu) {
    return categorical_lpmf(slice_location | softmax(to_vector(theta_loc_mu[start:end])));
  }
  
  real alligator(int[] slice_rating,int start, int end, matrix theta_rat_mu) {
    return categorical_lpmf(slice_rating | softmax(to_vector(theta_rat_mu[start:end])));
  }
  
  real parrot(int[] slice_domain,int start, int end, matrix theta_dom_mu) {
    return categorical_lpmf(slice_domain | softmax(to_vector(theta_dom_mu[start:end])));
  }
target += reduce_sum(myfunction1, ...)
target += reduce_sum(alligator, ...)
target += reduce_sum(parrot, ...)

should work.


When these are function arguments, you don’t specify dimensions. You can programmatically get the number of rows and columns if you need them: 5.1 Integer-Valued Matrix Size Functions | Stan Functions Reference

So like:

real partial_sum(int[] slice_location,
                             int start, int end, 
                              vector attitude, 
                              row_vector eta_attitude_rating, 
                              matrix s, 
                              matrix eta_s_rating ) {
    int N = rows(s); // get the rows of s
    int K = cols(s); // get the cols of s

    return categorical_lpmf(slice_location | softmax(to_vector(attitude* eta_attitude_rating + s *eta_s_rating)));
  }

That what you’re looking for?

Cool! You are welcome… I was really hoping that users will appreciate this new facility for its ease of use.

How many cores do you use to get this speedup?

(I just recently ported a model with categorical logit and saw really big speedups, which is great)

1 Like

thanks so much I will try now running with different name of function now.
lol the name of the functions we all miss going out :)

yes this solves my doubts. thanks