brms:OpenCL function on MacOS Catalina

,

I had run brms model successfully with OpenCL fucntion on my Windows device. Now, I want to do the similar work on my Mac air laptop. However, When I run the example code as following:

library(brms)
library(cmdstanr)

n <- 300000
k <- 10
X <- matrix(rnorm(n * k), ncol = k)
y <- rbinom(n, size = 1, prob = plogis(10 * X[,1] + 9 * X[,2] + 8 * X[,3] + 7 * X[,4] + 6 * X[,5] + 5 * X[,6] + 4 * X[,7] + 3 * X[,8] + 2 * X[,9] + 2 * X[,10] + 1))

code <- make_stancode(y ~ X1 + X2 + X3 + X4 + X5 + X6 + X7 + X8 + X9 + X10 , data = data.frame(y, X), family = bernoulli(), refresh = 0)
data <- make_standata(y ~ X1 + X2 + X3 + X4 + X5 + X6 + X7 + X8 + X9 + X10, data = data.frame(y, X), family = bernoulli(), refresh = 0)
class(data) <- NULL

file_cl <- write_stan_file(code, hash_salt = "opencl")
mod_cl <- cmdstan_model(file_cl, cpp_options = list(stan_opencl=TRUE))
fit_cl <- mod_cl$sample(data = data, seed = 123, chains = 2, parallel_chains = 2, opencl_ids = c(0,0))

I got these error messages:

Running MCMC with 2 parallel chains...

Chain 1 Unrecoverable error evaluating the log probability at the initial value. 
Chain 1 Exception: results_cl.assignment: clEnqueueNDRangeKernel CL_INVALID_WORK_GROUP_SIZE: Unknown error -54 (in '/var/folders/ym/jpqfzsn938sdz_0h9fzq198r0000gn/T/RtmpkUypRt/model-127ad7995c6e3.stan', line 29, column 4 to column 61) 
Chain 1 Exception: results_cl.assignment: clEnqueueNDRangeKernel CL_INVALID_WORK_GROUP_SIZE: Unknown error -54 (in '/var/folders/ym/jpqfzsn938sdz_0h9fzq198r0000gn/T/RtmpkUypRt/model-127ad7995c6e3.stan', line 29, column 4 to column 61)
Chain 1 Exception: results_cl.assignment: clEnqueueNDRangeKernel CL_INVALID_WORK_GROUP_SIZE: Unknown error -54 (in '/var/folders/ym/jpqfzsn938sdz_0h9fzq198r0000gn/T/RtmpkUypRt/model-127ad7995c6e3.stan', line 29, column 4 to column 61)
Chain 2 Unrecoverable error evaluating the log probability at the initial value. 
Chain 2 Exception: results_cl.assignment: clEnqueueNDRangeKernel CL_INVALID_WORK_GROUP_SIZE: Unknown error -54 (in '/var/folders/ym/jpqfzsn938sdz_0h9fzq198r0000gn/T/RtmpkUypRt/model-127ad7995c6e3.stan', line 29, column 4 to column 61) 
Chain 2 Exception: results_cl.assignment: clEnqueueNDRangeKernel CL_INVALID_WORK_GROUP_SIZE: Unknown error -54 (in '/var/folders/ym/jpqfzsn938sdz_0h9fzq198r0000gn/T/RtmpkUypRt/model-127ad7995c6e3.stan', line 29, column 4 to column 61)
Chain 2 Exception: results_cl.assignment: clEnqueueNDRangeKernel CL_INVALID_WORK_GROUP_SIZE: Unknown error -54 (in '/var/folders/ym/jpqfzsn938sdz_0h9fzq198r0000gn/T/RtmpkUypRt/model-127ad7995c6e3.stan', line 29, column 4 to column 61)
Warning: Chain 1 finished unexpectedly!

Warning: Chain 2 finished unexpectedly!

Warning: Use read_cmdstan_csv() to read the results of the failed chains.
Warning messages:
1: All chains finished unexpectedly! Use the $output(chain_id) method for more information.
 
2: No chains finished successfully. Unable to retrieve the fit. 

Then I guess maybe I set a wrong opencl id. I checked my opencl id by clinfo:

 clinfo -l
Platform #0: Apple
 +-- Device #0: Intel(R) Core(TM) i5-1030NG7 CPU @ 1.10GHz
 `-- Device #1: Intel(R) Iris(TM) Plus Graphics

I changed the opencl_ids = c(0,0) to opencl_ids = c(0,1), and run the model again. But, I got a new error like this:

unning MCMC with 2 parallel chains...

Chain 1 Unrecoverable error evaluating the log probability at the initial value. 
Chain 1 Exception: compile_kernel: calculate <program source>:1:5: warning: no previous prototype for function 'either' 
Chain 1 Exception: compile_kernel: calculate <program source>:1:5: warning: no previous prototype for function 'either'
Chain 1 int either(int left_view, int right_view) { return left_view | right_view; } int both(int left_view, int right_view) { return left_view & right_view; } bool contains_nonzero(int view, int part) { return both(view, part); }kernel void calculate(__global int* var5_global, int var5_rows, int var5_view, int var6, int var8, double var9, __global double* var16_global, int var16_rows, int var16_view, __global double* var19_global, int var19_rows, int var19_view, int var14_view, int var14_cols, int var14_vec_view, double var20, int var23, int var24, double var25, double var31, __global double* var34_global, int var34_rows, int var34_view, const int rows, const int cols){ 
Chain 1     ^ 
Chain 1 <program source>:1:82: warning: no previous prototype for function 'both' 
Chain 1 int either(int left_view, int right_view) { return left_view | right_view; } int both(int left_view, int right_view) { return left_view & right_view; } bool contains_nonzero(int view, int part) { return both(view, part); }kernel void calculate(__global int* var5_global, int var5_rows, int var5_view, int var6, int var8, double var9, __global double* var16_global, int var16_rows, int var16_view, __global double* var19_global, int var19_rows, int var19_view, int var14_view, int var14_cols, int var14_vec_view, double var20, int var23, int var24, double var25, double var31, __global double* var34_global, int var34_rows, int var34_view, const int rows, const int cols){ 
Chain 1                                                                                  ^ 
Chain 1 <program source>:1:158: warning: no previous prototype for function 'contains_nonzero' 
Chain 1 int either(int left_view, int right_view) { return left_view | right_view; } int both(int left_view, int right_view) { return left_view & right_view; } bool contains_nonzero(int view, int part) { return both(view, part); }kernel void calculate(__global int* var5_global, int var5_rows, int var5_view, int var6, int var8, double var9, __global double* var16_global, int var16_rows, int var16_view, __global double* var19_global, int var19_rows, int var19_view, int var14_view, int var14_cols, int var14_vec_view, double var20, int var23, int var24, double var25, double var31, __global double* var34_global, int var34_rows, int var34_view, const int rows, const int cols){ 

BTW:
I can run the normal brms model without opencl function on my Mac.
I can also run brms models by using “within-chain parallelization” function with the help of cmdstanr. (So, it seems that the only possibility is that there are some issues with the my GPU or OpenCL on the Mac).

My questions:
what is the meaning of “clEnqueueNDRangeKernel CL_INVALID_WORK_GROUP_SIZE” and "Unrecoverable error evaluating the log probability at the initial value. "? How to solve it?

  • Operating System: macOS Catalina 10.15.7 (19H2)
  • brms Version: 2.15.9
  • cmdstanr Version: 0.4.0.9000
  • cmdstan Version: 2.27.0
  • GPU: Intel(R) Iris™ Plus Graphics
  • CPU: Intel(R) Core™ i5-1030NG7 CPU @ 1.10
  • OpenCL 1.2 (Jun 8 2020 17:36:15)

Looking forward to your advice!

1 Like

Hi,
unfortunately, OpenCL support on Mac is pretty bad and their OpenCL implementation is quite old. I am not sure if that’s the source of the problem in this case, but we need to keep this in mind.

What’s weird is that there is only a warning, but that gets escalated to exception. Maybe @rok_cesnovar would know more?

1 Like

I got the same problem on MacOS sonoma.

fit ← brm(count ~ zAge + zBase * Trt + (1|patient),

  •        data = epilepsy, family = poisson(),
    
  •        chains = 4, cores = 2, opencl = opencl(c(0, 0)),
    
  •        backend = "cmdstanr")
    

Model executable is up to date!
Start sampling
Running MCMC with 4 chains, at most 2 in parallel…

Warning: Chain 1 finished unexpectedly!

Warning: Chain 2 finished unexpectedly!

Warning: Chain 3 finished unexpectedly!

Warning: Chain 4 finished unexpectedly!

Warning: Use read_cmdstan_csv() to read the results of the failed chains.
Error in cmdstanr::read_cmdstan_csv(out$output_files(), variables = “”, :
Assertion on ‘files’ failed: No file provided.
In addition: Warning messages:
1: All chains finished unexpectedly! Use the $output(chain_id) method for more information.

2: No chains finished successfully. Unable to retrieve the fit.