Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using expose_functions() errors when model binary already exists #784

Closed
gobbios opened this issue Jul 14, 2023 · 4 comments
Closed

Using expose_functions() errors when model binary already exists #784

gobbios opened this issue Jul 14, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@gobbios
Copy link

gobbios commented Jul 14, 2023

Describe the bug
When using cmdstan_model("mymod.stan", compile_standalone = TRUE) and the mymod executable already exists (so nothing is (re)compiled) then $expose_functions() returns the error:

Error in function_env$external && cmdstan_version() < "2.32" :
  invalid 'x' type in 'x && y'

If this is not a bug, I'd think that maybe a clearer error message would help.

To Reproduce

library(cmdstanr)

stancode <- "
  functions {
    real add_up(real a, real b){
    return a + b;
    }
  }
  data {
    int<lower=0> N;
    array[N] int<lower=0,upper=1> y;
  }
  parameters {
    real<lower=0,upper=1> theta;
  }
  model {
    theta ~ beta(1, 1);
    y ~ bernoulli(theta);
  }
"

write_stan_file(stancode, dir = "", basename = "mymod")

On first compilation everything runs as expected.

mod <- cmdstan_model("mymod.stan", compile_standalone = TRUE)
mod$expose_functions(global = TRUE)
add_up(4, 5)
mod$functions$add_up(4, 5)

Then restart the R session

library(cmdstanr)
mod <- cmdstan_model("mymod.stan", compile_standalone = TRUE)
# Model executable is up to date!

# both lines result in the same error message:
mod$expose_functions(global = TRUE)
mod$expose_functions(global = FALSE)

# Error in function_env$external && cmdstan_version() < "2.32" :
#   invalid 'x' type in 'x && y'

mod$functions$add_up
# NULL

# sampling seems fine
mod$sample(data = list(N = 5, y = c(0, 1, 1, 0, 1)), refresh = 0, chains = 1, seed = 1)

Expected behavior

I expected that the Stan function would be available (in the global environment, or at least in the model environment) even when recompilation of the model is not necessary.

Operating system
macOS Ventura 13.1

CmdStanR version number
cmdstanr: 0.5.3 (from remotes::install_github("stan-dev/cmdstanr"))
cmdstan_version(): 2.32.2

Additional context
none

@gobbios gobbios added the bug Something isn't working label Jul 14, 2023
@jgabry
Copy link
Member

jgabry commented Jul 25, 2023

Thanks for reporting this. I agree there should be a more informative message. @andrjohns what do you think? Should we add a message saying that recompilation is necessary (if that's the issue here)?

@jgabry
Copy link
Member

jgabry commented Aug 15, 2023

This came up again in #819 from @maxbiostat. @andrjohns What was the intended behavior when the model is already compiled? Should there just be an informative error message saying to recompile? Should it recompile automatically in order to compile the functions? Something else? Thanks!

@jgabry
Copy link
Member

jgabry commented Aug 22, 2023

@andrjohns This error keeps coming up (see e.g. #819 and #830). Do you have time to look into this? If you don't have time right now no worries, I can try to take care of it, you're just more familiar with the expose_functions code.

@andrjohns
Copy link
Collaborator

Closed by #831

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants