metrumresearchgroup / Torsten

library of C++ functions that support applications of Stan in Pharmacometrics
BSD 3-Clause "New" or "Revised" License
52 stars 11 forks source link

[BUG] cHat is always 0 for `pmx_solve_twocpt` #51

Closed jburos closed 6 months ago

jburos commented 6 months ago

Description

I'm trying to fit a 2-compartment pop-pk model and have found that pmx_solve_twocpt always return 0. Also, likely for this reason, the example-model pk2cpt.stan fails to initialize. This occurs with the develop branch, master branch, and v0.91.0rc2.

I'm on an M3 mac with empty Makevars file.

Example

First, the code I used to run the example model:

ref <- 'develop'
system(glue::glue("git clone -b {ref} https://github.com/metrumresearchgroup/Torsten.git ~/testing/Torsten_{ref}"))
torsten_dir <- glue::glue("~/testing/Torsten_{ref}")

# set cmdstanr to use torsten flavor of cmdstan
# Note: this can also be done with ENV vars
cmdstanr::set_cmdstan_path(fs::path(torsten_dir, "cmdstan"))
cmdstanr::check_cmdstan_toolchain()
cmdstanr::rebuild_cmdstan()

model_dir <- fs::path(torsten_dir, "example-models", "pk2cpt")
model_src <- fs::path_expand(fs::path(model_dir, "pk2cpt.stan"))
model <- cmdstanr::cmdstan_model(model_src)

# does not initialize
model_fit <- model$sample(data = file.path(model_dir, "pk2cpt.data.R"),
                          init = file.path(model_dir, "pk2cpt.init.R"),
                          seed = 1229453,
                          chains = 4,
                          parallel_chains = 2,
                          refresh = 500)

I tested this with a local version of pk2cpt.stan with a print statement after line 54:

model2_dir <- here::here()
model2_src <- fs::path_expand(fs::path(model2_dir, "pk2cpt.stan"))
model2 <- cmdstanr::cmdstan_model(model2_src)

model2_fit <- model2$sample(data = file.path(model_dir, "pk2cpt.data.R"),
                          init = file.path(model_dir, "pk2cpt.init.R"),
                          seed = 1229453,
                          chains = 1,
                          parallel_chains = 1,
                          refresh = 1)                     

I see:

Running MCMC with 1 chain...

Chain 1 cHatObs: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] 
Chain 1 Rejecting initial value:
Chain 1   Error evaluating the log probability at the initial value.
Chain 1 Exception: normal_lpdf: Location parameter[1] is -inf, but must be finite! (in '/var/folders/6r/tqtyyh5x67s_cjlvmm_lmm040000gn/T/RtmpwEkfLn/model-98381647a45e.stan', line 67, column 2 to column 40)
Chain 1 Exception: normal_lpdf: Location parameter[1] is -inf, but must be finite! (in '/var/folders/6r/tqtyyh5x67s_cjlvmm_lmm040000gn/T/RtmpwEkfLn/model-98381647a45e.stan', line 67, column 2 to column 40)
Chain 1 Warning: file '/Users/jacquelineburos/testing/Torsten_v0.91.0rc2/example-models/pk2cpt/pk2cpt.data.R' is being read as an 'RDump' file.
Chain 1     This format is deprecated and will not receive new features.
Chain 1     Consider saving your data in JSON format instead.
Chain 1 Warning: file '/Users/jacquelineburos/testing/Torsten_v0.91.0rc2/example-models/pk2cpt/pk2cpt.init.R' is being read as an 'RDump' file.
Chain 1     This format is deprecated and will not receive new features.
Chain 1     Consider saving your data in JSON format instead.
Chain 1 Initialization between (-2, 2) failed after 1 attempts. 
Chain 1  Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
Chain 1 Initialization failed.
Warning: Chain 1 finished unexpectedly!

Warning message:
No chains finished successfully. Unable to retrieve the fit. 

Expected Output

It's possible there is something wrong with my setup, but at least right now I'm not able to use this analytic solution while pmx_solve_linode works fine.

Current Version:

v0.91.0, develop, master, v0.89.0rc2

I would note, I tried to use v0.89.0 but I was not able to build cmdstan+torsten from this version.

../tbb_2020.3/src/tbbmalloc/frontend.cpp:779:8: error: invalid instruction, any one of the following would fix this:
       "clz %0, %1\n"
       ^
<inline asm>:1:2: note: instantiated into assembly here
        clz r2, r1
        ^
../tbb_2020.3/src/tbbmalloc/frontend.cpp:779:8: note: instruction requires: armv5t
       "clz %0, %1\n"
       ^
<inline asm>:1:2: note: instantiated into assembly here
        clz r2, r1
        ^
../tbb_2020.3/src/tbbmalloc/frontend.cpp:779:8: note: instruction requires: thumb2
       "clz %0, %1\n"
yizhang-yiz commented 6 months ago

Thanks for reporting. This has been noticed and currently under investigation. See

50

We've found that the issue only occurs on MacOS using llvm 15+, so most likely Ventura & Sonoma are generally affected. Before I can fix it the only solution would be to use older version llvm.

jburos commented 6 months ago

Thanks for your response and for looking into it; I will close this as a duplicate & try an earlier version of llvm.