stan-dev / cmdstanr

CmdStanR: the R interface to CmdStan
https://mc-stan.org/cmdstanr/
Other
144 stars 63 forks source link

STAN_THREADS in make/local not respected due to capitalisation conflict #765

Open jaburgoyne opened 1 year ago

jaburgoyne commented 1 year ago

Describe the bug

If threading is enabled globally by way of STAN_THREADS=true in make/local (as opposed to locally via cpp_options = list(stan_threads = TRUE) in a call to model$compile()), cmdstanr does not respect the setting. Although model$cpp_options is TRUE for the key STAN_THREADS, assert_valid_threads() only checks for stan_threads in lowercase.

Although threading is perhaps the most common use case, the same bug would apply to STAN_OPENCL and any other C++ option. To fix the bug, CmdStanModel needs to standardise on all-caps or lowercase for its internal cpp_options representation. It seems to me that lowercase would be the most consistent (and thus model_compile_info() should use tolower() instead of toupper() here), but in case this has further-reaching consequences than I can see, I’m making an issue instead of a pull request.

To Reproduce

Set STAN_THREADS=true in make/local, compile a model, and then try to run model$sample() with threads_per_chain set to some integer greater than 1.

Expected behavior

The model should sample using multi-threading instead of complaining (erroneously) that the model has not been compiled with threading enabled.

Operating system

Although this should be platform-agnostic, I am running macOS 13.3.1 on an Apple M1 architecture.

CmdStanR version number

2.32.1

Additional context

None

katrinabrock commented 3 months ago

Hello,

I hit this one as well with open_cl. I did not even set STAN_THREADS or STAN_OPENCL globally. The steps were simply

(If these steps are not clear enough, I'd be happy to write up an MRE with code.)

Sampling will fail with the erroneous claim that the model was not compiled with stan_opencl = true. I think @jaburgoyne has correctly identified the root cause. I believe switching the assertions to check the uppercase version of the options would solve the issue without side effect.

@jgabry would you accept a PR to resolve this issue? If so, would you like to switch the checks to uppercase, model_compile_info setting to lowercase, or something else?

@jaburgoyne (and anyone else who is having this problem), I was able to workaround this issue with this ugliness:

mod$.__enclos_env__$private$cpp_options_$stan_opencl <- TRUE
katrinabrock commented 3 months ago

I went ahead and created a failing testcase for this issue: https://github.com/stan-dev/cmdstanr/compare/master...katrinabrock:cmdstanr:issue-765 (Just for the opencl part. We could create similar cases for the stan_threads part.)

jgabry commented 3 months ago

Thanks @jaburgoyne for reporting this. And thanks @katrinabrock for doing more digging and creating the failing test case. A PR would be great, thank you!

It seems to me that lowercase would be the most consistent (and thus model_compile_info() should use tolower() instead of toupper() here), but in case this has further-reaching consequences than I can see, I’m making an issue instead of a pull request.


This seems ok to me, but maybe @andrjohns has a different preference or can think of some issue with making this change.

katrinabrock commented 3 months ago

The opencl tests seem to be not working in general. Some are failing, and some, I believe, are passing but not checking the thing they are trying to check. Is there a reason these tests are not included in CI? (I understand why you would not want them to run on CRAN, but is there a reason not to run them in the github actions?)

I've identified two issues so far:

There are possibly other issues with this test script. I think before (or maybe just in parallel to) addressing the issue mentioned in this thread, it would be good to get that test script to a state where tests are passing and actually testing the existing functionality. Maybe I should create a separate issue for this effort?

katrinabrock commented 3 months ago

Made a PR to fix the tests: https://github.com/stan-dev/cmdstanr/pull/1022

I'd also be happy to help if there are steps needed to add this to CI e.g. find/configure a docker container that can run this.