Cmdstan error

My model works fine with rstan. I want to try GPU speed up so I changed my model accordingly. But when i compile I get errors:

This is my original code

data {
  int<lower=0> N;//Number of observations
  int<lower=1> J;//Number of predictors with random slope
  int<lower=1> K;//Number of predictors with non-random slope
  int<lower=1> L;//Number of customers/groups
  int<lower=0,upper=1> y[N];//Binary response variable
  int<lower=1,upper=L> ll[N];//Number of observations in groups
  matrix[N,K] x1;
  matrix[N,J] x2;
}
transformed data {
  vector[J] ones = rep_vector(1, J);
}
parameters {
  row_vector[J] rbeta_mu; //mean of distribution of beta parameters
  row_vector<lower=0>[J] rbeta_sigma; //variance of distribution of beta parameters
  row_vector[J] beta_raw[L]; //group-specific parameters beta
  vector[K] beta;
}
transformed parameters {
  matrix[L,J] rbeta;
  for (l in 1:L)
    rbeta[l] = rbeta_mu + rbeta_sigma .* beta_raw[l]; // coefficients on x
}
model {
  vector[N] p;
  rbeta_mu ~ normal(0,5);
  rbeta_sigma ~ cauchy(0,5);
  beta~normal(0,5);
  for (l in 1:L)
    beta_raw[l] ~ std_normal();

  p = x1 * beta + (x2 .* rbeta[ll]) * ones; // Multiplication by vector of ones as a row-wise summation of matrix
  y~bernoulli_logit(p);
}

This is the code i have modified for GPU speed up:

data {
  int<lower=0> N;//Number of observations
  int<lower=1> J;//Number of predictors with random slope
  int<lower=1> K;//Number of predictors with non-random slope
  int<lower=1> L;//Number of customers/groups
  int<lower=0,upper=1> y[N];//Binary response variable
  int<lower=1,upper=L> ll[N];//Number of observations in groups
  matrix[N,K] x1;
  matrix[N,J] x2;
}
transformed data {
  vector[J] ones = rep_vector(1, J);
}
parameters {
  row_vector[J] rbeta_mu; //mean of distribution of beta parameters
  row_vector<lower=0>[J] rbeta_sigma; //variance of distribution of beta parameters
  row_vector[J] beta_raw[L]; //group-specific parameters beta
  vector[K] beta;
}
transformed parameters {
  matrix[L,J] rbeta;
  for (l in 1:L)
    rbeta[l] = rbeta_mu + rbeta_sigma .* beta_raw[l]; // coefficients on x
}
model {
  vector[N] mu;
  target+=normal_lpdf(beta|0,5);
  target+=cauchy_lpdf(rbeta_sigma|0,2);
  target+=normal_lpdf(rbeta_mu|0,5);
  for (l in 1:L)
    beta_raw[l] ~ std_normal();
  mu = x1 * beta + (x2 .* rbeta[ll]) * ones; // Multiplication by vector of ones as a row-wise summation of matrix
  target += bernoulli_logit_lpmf(y | mu);
}

When i comile i get this error:

I am passing data as json.

model_data <- list(N=nrow(matdata1),K=ncol(matdata1),J=ncol(matdata2),x1=matdata1,x2=matdata2,y=data2$bought[1:10000],L=length(unique(data2$new_group[1:10000])),ll=data2$new_group[1:10000])


write_stan_json(model_data,"data_json.json")

opencl_options = list(
  stan_opencl = TRUE,
  opencl_platform_id = 0, # replace the ID based on step 3
  opencl_device_id = 0 # replace the ID based on step 3
)

mod <- cmdstan_model("./gpu_stan_code.stan", cpp_options = opencl_options)


fit <- mod$sample(data = "./data_json.json", num_samples=500, num_warmup = 500, num_chains = 1, num_cores = 1)

I have only one GPU so have left both ids as 0.

Please help how can i resolve this.

can you try adding PRECOMPILED_HEADERS = false to your opencl_options? Looking online this may be an actual internal compiler error when working with precompiled headers.

Also looking at your model I’m not sure much of it will be able to use the GPU stuff. We have a list here of stuff supported by the GPU directly (it’s mostly focused on gaussian processes)

http://mc-stan.org/math/opencl_support.html

I have added it to the opencl_options still no luck. Same error is shown upon compiling.

I understand. Could i still try replacing bernoulli_logit_lpmf with bernoulli_logit_glm_lpmf. Any speed up i could get would be useful.

What gcc/rtools version are you on? Can you copy/paste the full error message?

Yes it looks like the rest of your model should be able to use the gpu stuff with bernoulli_logit_glm_lpmf

This is the full error message

This is the gcc version

I am using rtools 4.0

@jonah is it possible to compile cmdstanr without the precompiled headers? Can users just add PRECOMPILED_HEADERS=0 to their ~/.R/Makevars

@oxyaks can you post your current ~/.R/Makevars

CXX14FLAGS += -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -Wno-ignored-attributes

This is the contents of my makevars file

When i try to convert the model block from :

model {
  vector[N] mu;
  target+=normal_lpdf(beta|0,5);
  target+=cauchy_lpdf(rbeta_sigma|0,2);
  target+=normal_lpdf(rbeta_mu|0,5);
  for (l in 1:L)
    beta_raw[l] ~ std_normal();
  mu = x1 * beta + (x2 .* rbeta[ll]) * ones; // Multiplication by vector of ones as a row-wise summation of matrix
  target += bernoulli_logit_lpmf(y | mu);
}

to:

model {
  vector[N] mu;
  target+=normal_lpdf(beta|0,5);
  target+=cauchy_lpdf(rbeta_sigma|0,2);
  target+=normal_lpdf(rbeta_mu|0,5);
  for (l in 1:L)
    beta_raw[l] ~ std_normal();
  mu = x1 * beta + (x2 .* rbeta[ll]) * ones; // Multiplication by vector of ones as a row-wise summation of matrix
  target += bernoulli_logit_glm_lpmf(y | mu);
}

I am shown this

why can’t i use mu.

See this section of the Stan manual for more information on using the GLM function: https://mc-stan.org/docs/2_24/functions-reference/bernoulli-logit-glm.html

Thank you. Now i understand it was a mismatch of number of arguements. But still do not know why I can’t run the example model for GPU operations given on github by @jonah.

Hi,
currently (in version 2.24) you can only move the GLM function to the GPU if both x and y (the first two arguments) are data. This will change for the next release, but you currently can not use them as mu in your case is a parameter.

They need to remove PRECOMPILED_HEADERS=true from the cmdstan installation make/local. That gets added by cmdstanr on install for Windows with RTools 4.0 (plain cmdstan does not use precompiled headers on windows by default as RTools 3.5 do not support them).

But I am not sure why this causes issues, it should not. It might be a cmdstan makefile thing we should address. Will check on Windows.

Look forward to hearing potential solution if there is infact a bug.