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.
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
I suspect you have an old version. I just merged in the updates. They were on a branch.
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!)
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.
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
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.
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 ~
).
@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 :)
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
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)
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