Skip to content

Commit 24acf13

Browse files
authored
Merge pull request #1095 from stan-dev/exe-w-include_paths
Fix handling of `include_paths` for pre-compiled models
2 parents aa2b708 + 40cfbc6 commit 24acf13

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ set to `TRUE` for an entire R session via new global options. (#1159)
1111
* `save_metric_files()` now gives an informative error when metric files were not created and keeps saved metric files after the fitted model is garbage-collected. (#1021)
1212
* `cmdstan_model()` no longer fails when `MAKEFLAGS` enables directory-printing
1313
output while reading `STANCFLAGS` from `make`. (#1163)
14+
* `cmdstan_model()` now retains include paths when initialized with both a Stan file and a precompiled executable, fixing model introspection for programs that use `#include` (#1094).
1415
* `laplace()` no longer overwrites the internally generated optimizer CSV when
1516
`mode = NULL` and `output_basename` is supplied. The internally generated
1617
optimizer CSV now uses the filename `<output_basename>-mode-1.csv`.

R/model.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ CmdStanModel <- R6::R6Class(
270270
assert_file_exists(private$exe_file_, access = "r", extension = ext)
271271
private$model_name_ <- gsub(" ", "_", strip_ext(basename(private$exe_file_)))
272272
}
273+
private$include_paths_ <-
274+
private$precompile_include_paths_ %||% args$include_paths
273275
}
274276
if (!is.null(stan_file) && compile) {
275277
self$compile(...)

tests/testthat/test-model-compile.R

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,64 @@ test_that("compilation works with include_paths", {
109109
)
110110
})
111111

112+
test_that("precompiled models retain include paths", {
113+
model_dir <- withr::local_tempdir()
114+
write_stan_file(
115+
"
116+
functions {
117+
real silly_logit(real x) {
118+
return logit(x);
119+
}
120+
}
121+
",
122+
dir = file.path(model_dir, "utils"),
123+
basename = "silly.stan"
124+
)
125+
stan_file <- write_stan_file(
126+
"
127+
#include utils/silly.stan
128+
data {
129+
int<lower=0> N;
130+
array[N] int<lower=0, upper=1> y;
131+
}
132+
parameters {
133+
real<lower=0, upper=1> theta;
134+
}
135+
model {
136+
theta ~ beta(1, 1);
137+
y ~ bernoulli(theta);
138+
}
139+
generated quantities {
140+
real theta_lin = silly_logit(theta);
141+
}
142+
",
143+
dir = model_dir,
144+
basename = "bernoulli.stan"
145+
)
146+
compiled_model <- cmdstan_model(
147+
stan_file,
148+
include_paths = model_dir,
149+
quiet = TRUE
150+
)
151+
152+
model_with_explicit_path <- cmdstan_model(
153+
stan_file,
154+
exe_file = compiled_model$exe_file(),
155+
compile = FALSE,
156+
include_paths = model_dir
157+
)
158+
expect_equal(model_with_explicit_path$include_paths(), model_dir)
159+
expect_no_error(model_with_explicit_path$variables())
160+
161+
model_with_automatic_path <- cmdstan_model(
162+
stan_file,
163+
exe_file = compiled_model$exe_file(),
164+
compile = FALSE
165+
)
166+
expect_equal(model_with_automatic_path$include_paths(), dirname(stan_file))
167+
expect_no_error(model_with_automatic_path$variables())
168+
})
169+
112170
test_that("name in STANCFLAGS is set correctly", {
113171
local_reproducible_output()
114172
out <- utils::capture.output(mod$compile(quiet = FALSE, force_recompile = TRUE))

0 commit comments

Comments
 (0)