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

Test and fix issues with 2.35 RC #972

Merged
merged 8 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/R-CMD-check-wsl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ jobs:

- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2.8.7
- uses: r-lib/actions/setup-pandoc@v2.8.7
- uses: r-lib/actions/setup-r@v2.9.0
- uses: r-lib/actions/setup-pandoc@v2.9.0

- uses: r-lib/actions/setup-r-dependencies@v2.8.7
- uses: r-lib/actions/setup-r-dependencies@v2.9.0
with:
extra-packages: any::rcmdcheck, local::.

Expand Down Expand Up @@ -71,7 +71,7 @@ jobs:
sessioninfo::session_info(pkgs, include_base = TRUE)
shell: Rscript {0}

- uses: r-lib/actions/check-r-package@v2.8.7
- uses: r-lib/actions/check-r-package@v2.9.0
env:
_R_CHECK_CRAN_INCOMING_: false

Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
config:
- {os: macOS-latest, r: 'devel', rtools: ''}
- {os: macOS-latest, r: 'release', rtools: ''}
#- {os: windows-latest, r: 'devel', rtools: '44'}
- {os: windows-latest, r: 'devel', rtools: '44'}
- {os: windows-latest, r: 'release', rtools: '44'}
- {os: windows-latest, r: 'oldrel', rtools: '43'}
- {os: ubuntu-latest, r: 'devel', rtools: ''}
Expand All @@ -49,14 +49,14 @@ jobs:

- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2.8.7
- uses: r-lib/actions/setup-r@v2.9.0
with:
r-version: ${{ matrix.config.r }}
rtools-version: ${{ matrix.config.rtools }}

- uses: r-lib/actions/setup-pandoc@v2.8.7
- uses: r-lib/actions/setup-pandoc@v2.9.0

- uses: r-lib/actions/setup-r-dependencies@v2.8.7
- uses: r-lib/actions/setup-r-dependencies@v2.9.0
with:
extra-packages: any::rcmdcheck, local::.

Expand All @@ -73,7 +73,7 @@ jobs:
sessioninfo::session_info(pkgs, include_base = TRUE)
shell: Rscript {0}

- uses: r-lib/actions/check-r-package@v2.8.7
- uses: r-lib/actions/check-r-package@v2.9.0
env:
_R_CHECK_CRAN_INCOMING_: false

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/Test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ jobs:
if: "!startsWith(github.ref, 'refs/tags/') && github.ref != 'refs/heads/master'"
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2.8.7
- uses: r-lib/actions/setup-pandoc@v2.8.7
- uses: r-lib/actions/setup-r@v2.9.0
- uses: r-lib/actions/setup-pandoc@v2.9.0

- uses: r-lib/actions/setup-r-dependencies@v2.8.7
- uses: r-lib/actions/setup-r-dependencies@v2.9.0
with:
extra-packages: any::rcmdcheck, local::., any::covr, any::gridExtra

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/cmdstan-tarball-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ jobs:
sudo apt-get install -y libcurl4-openssl-dev || true
sudo apt-get install -y openmpi-bin openmpi-common libopenmpi-dev || true

- uses: r-lib/actions/setup-r@v2.8.7
- uses: r-lib/actions/setup-r@v2.9.0
with:
r-version: ${{ matrix.config.r }}
rtools-version: ${{ matrix.config.rtools }}

- uses: r-lib/actions/setup-pandoc@v2.8.7
- uses: r-lib/actions/setup-pandoc@v2.9.0

- name: Query dependencies
run: |
Expand Down
8 changes: 8 additions & 0 deletions R/csv.R
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,14 @@ read_csv_metadata <- function(csv_file) {
csv_file_info$fitted_params <- wsl_safe_path(csv_file_info$fitted_params,
revert = TRUE)
}
csv_file_info <- lapply(csv_file_info, function(item) {
if (is.character(item) && length(item) == 1) {
if (item %in% c("true", "false")) {
item <- as.integer(as.logical(item))
}
}
item
})
csv_file_info
}

Expand Down
15 changes: 10 additions & 5 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ get_cmdstan_flags <- function(flag_name) {
paste(flags, collapse = " ")
}

rcpp_source_stan <- function(code, env, verbose = FALSE) {
rcpp_source_stan <- function(code, env, verbose = FALSE, ...) {
cxxflags <- get_cmdstan_flags("CXXFLAGS")
cmdstanr_includes <- system.file("include", package = "cmdstanr", mustWork = TRUE)
cmdstanr_includes <- paste0(" -I\"", cmdstanr_includes,"\"")
Expand All @@ -746,7 +746,7 @@ rcpp_source_stan <- function(code, env, verbose = FALSE) {
PKG_CXXFLAGS = paste0(cxxflags, cmdstanr_includes, collapse = " "),
PKG_LIBS = libs
),
Rcpp::sourceCpp(code = code, env = env, verbose = verbose)
Rcpp::sourceCpp(code = code, env = env, verbose = verbose, ...)
)
)
invisible(NULL)
Expand Down Expand Up @@ -887,8 +887,12 @@ prep_fun_cpp <- function(fun_start, fun_end, model_lines) {
}
fun_body <- gsub("// [[stan::function]]", "// [[Rcpp::export]]\n", fun_body, fixed = TRUE)
fun_body <- gsub("std::ostream\\*\\s*pstream__\\s*=\\s*nullptr", "", fun_body)
fun_body <- gsub("boost::ecuyer1988&\\s*base_rng__", "SEXP base_rng_ptr", fun_body)
fun_body <- gsub("base_rng__,", "*(Rcpp::XPtr<boost::ecuyer1988>(base_rng_ptr).get()),", fun_body, fixed = TRUE)
if (cmdstan_version() < "2.35.0") {
fun_body <- gsub("boost::ecuyer1988&\\s*base_rng__", "SEXP base_rng_ptr", fun_body)
} else {
fun_body <- gsub("stan::rng_t&\\s*base_rng__", "SEXP base_rng_ptr", fun_body)
}
fun_body <- gsub("base_rng__,", "*(Rcpp::XPtr<stan::rng_t>(base_rng_ptr).get()),", fun_body, fixed = TRUE)
fun_body <- gsub("pstream__", "&Rcpp::Rcout", fun_body, fixed = TRUE)
fun_body <- paste(fun_body, collapse = "\n")
gsub(pattern = ",\\s*)", replacement = ")", fun_body)
Expand Down Expand Up @@ -921,6 +925,7 @@ compile_functions <- function(env, verbose = FALSE, global = FALSE) {
env$hpp_code[1:(funs[1] - 1)],
"#include <rcpp_tuple_interop.hpp>",
"#include <rcpp_eigen_interop.hpp>",
"#include <stan_rng.hpp>",
stan_funs),
collapse = "\n")
if (global) {
Expand All @@ -935,7 +940,7 @@ compile_functions <- function(env, verbose = FALSE, global = FALSE) {
if (length(rng_funs) > 0) {
rng_cpp <- system.file("include", "base_rng.cpp", package = "cmdstanr", mustWork = TRUE)
rcpp_source_stan(paste0(readLines(rng_cpp), collapse="\n"), env, verbose)
env$rng_ptr <- env$base_rng(seed=0)
env$rng_ptr <- env$base_rng(seed=1)
}

# For all RNG functions, pass the initialised Boost RNG by default
Expand Down
6 changes: 3 additions & 3 deletions inst/include/base_rng.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <Rcpp.h>
#include <boost/random/additive_combine.hpp>
#include <stan_rng.hpp>

// [[Rcpp::export]]
SEXP base_rng(boost::uint32_t seed = 0) {
Rcpp::XPtr<boost::ecuyer1988> rng_ptr(new boost::ecuyer1988(seed));
SEXP base_rng(boost::uint32_t seed = 1) {
Rcpp::XPtr<stan::rng_t> rng_ptr(new stan::rng_t(seed));
return rng_ptr;
}
6 changes: 3 additions & 3 deletions inst/include/model_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
#include <stan/model/model_base.hpp>
#include <stan/model/log_prob_grad.hpp>
#include <stan/model/log_prob_propto.hpp>
#include <boost/random/additive_combine.hpp>
#ifdef CMDSTAN_JSON
#include <cmdstan/io/json/json_data.hpp>
#else
#include <stan/io/json/json_data.hpp>
#include <stan/io/empty_var_context.hpp>
#endif
#include <stan_rng.hpp>

std::shared_ptr<stan::io::var_context> var_context(std::string file_path) {
if (file_path == "") {
Expand All @@ -36,7 +36,7 @@ Rcpp::List model_ptr(std::string data_path, boost::uint32_t seed) {
Rcpp::XPtr<stan::model::model_base> ptr(
&new_model(*var_context(data_path), seed, &Rcpp::Rcout)
);
Rcpp::XPtr<boost::ecuyer1988> base_rng(new boost::ecuyer1988(seed));
Rcpp::XPtr<stan::rng_t> base_rng(new stan::rng_t(seed));
return Rcpp::List::create(
Rcpp::Named("model_ptr") = ptr,
Rcpp::Named("base_rng") = base_rng
Expand Down Expand Up @@ -144,7 +144,7 @@ std::vector<double> constrain_variables(SEXP ext_model_ptr, SEXP base_rng,
bool return_trans_pars,
bool return_gen_quants) {
Rcpp::XPtr<stan::model::model_base> ptr(ext_model_ptr);
Rcpp::XPtr<boost::ecuyer1988> rng(base_rng);
Rcpp::XPtr<stan::rng_t> rng(base_rng);
std::vector<int> params_i;
std::vector<double> vars;

Expand Down
5 changes: 5 additions & 0 deletions inst/include/rcpp_tuple_interop.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef CMDSTANR_RCPP_TUPLE_INTEROP_HPP
#define CMDSTANR_RCPP_TUPLE_INTEROP_HPP

#include <stan/math/prim/functor/apply.hpp>
#include <Rcpp.h>

Expand Down Expand Up @@ -38,3 +41,5 @@ namespace Rcpp {
}, x);
}
}

#endif
17 changes: 17 additions & 0 deletions inst/include/stan_rng.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef CMDSTANR_STAN_RNG_HPP
#define CMDSTANR_STAN_RNG_HPP

#include <boost/random/additive_combine.hpp>
#include <stan/version.hpp>

// A consistent rng_t is defined from 2.35 onwards
// so add a fallback for older versions
#if STAN_MAJOR == 2 && STAN_MINOR >= 35
#include <stan/services/util/create_rng.hpp>
#else
namespace stan {
using rng_t = boost::ecuyer1988;
}
#endif

#endif
10 changes: 7 additions & 3 deletions tests/testthat/test-model-compile.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ context("model-compile")
set_cmdstan_path()
stan_program <- cmdstan_example_file()
mod <- cmdstan_model(stan_file = stan_program, compile = FALSE)

cmdstan_make_local(cpp_options = list("PRECOMPILED_HEADERS"="false"))

test_that("object initialized correctly", {
expect_equal(mod$stan_file(), stan_program)
Expand Down Expand Up @@ -130,6 +130,9 @@ test_that("name in STANCFLAGS is set correctly", {
test_that("switching threads on and off works without rebuild", {
main_path_o <- file.path(cmdstan_path(), "src", "cmdstan", "main.o")
main_path_threads_o <- file.path(cmdstan_path(), "src", "cmdstan", "main_threads.o")
backup <- cmdstan_make_local()
no_threads <- grep("STAN_THREADS", backup, invert = TRUE, value = TRUE)
cmdstan_make_local(cpp_options = list(no_threads), append = FALSE)
if (file.exists(main_path_threads_o)) {
file.remove(main_path_threads_o)
}
Expand All @@ -155,6 +158,7 @@ test_that("switching threads on and off works without rebuild", {
expect_equal(before_mtime, after_mtime)

expect_warning(mod$compile(threads = TRUE, dry_run = TRUE), "deprecated")
cmdstan_make_local(cpp_options = backup, append = FALSE)
})

test_that("multiple cpp_options work", {
Expand Down Expand Up @@ -483,19 +487,19 @@ test_that("include_paths_stanc3_args() works", {

test_that("cpp_options work with settings in make/local", {
backup <- cmdstan_make_local()
no_threads <- grep("STAN_THREADS", backup, invert = TRUE, value = TRUE)
cmdstan_make_local(cpp_options = list(no_threads), append = FALSE)

if (length(mod$exe_file()) > 0 && file.exists(mod$exe_file())) {
file.remove(mod$exe_file())
}

cmdstan_make_local(cpp_options = list(), append = TRUE)
rebuild_cmdstan()
mod <- cmdstan_model(stan_file = stan_program)
expect_null(mod$cpp_options()$STAN_THREADS)

file.remove(mod$exe_file())

cmdstan_make_local(cpp_options = backup, append = FALSE)
cmdstan_make_local(cpp_options = list(stan_threads = TRUE), append = TRUE)

file <- file.path(cmdstan_path(), "examples", "bernoulli", "bernoulli.stan")
Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/test-model-expose-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,13 @@ test_that("rng functions can be exposed", {

expect_equal(
fit$functions$wrap_normal_rng(5,10),
-4.5298764235381225873
# Stan RNG changed in 2.35
ifelse(cmdstan_version() < "2.35.0",-4.529876423, 0.02974925)
)

expect_equal(
fit$functions$wrap_normal_rng(5,10),
8.1295902610102039887
ifelse(cmdstan_version() < "2.35.0", 8.12959026, 10.3881349)
)
})

Expand Down
8 changes: 6 additions & 2 deletions tests/testthat/test-profiling.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ test_that("profiling works if profiling data is present", {
profiles <- fit$profiles()
expect_equal(length(profiles), 4)
expect_equal(dim(profiles[[1]]), c(3,9))
expect_equal(profiles[[1]][,"name"], c("glm", "priors", "udf"))
for (name in profiles[[1]][,"name"]) {
expect_true(name %in% c("udf", "priors", "glm"))
}

file.remove(fit$profile_files())
expect_error(
Expand All @@ -24,7 +26,9 @@ test_that("profiling works if profiling data is present", {
profiles_no_csv <- fit$profiles()
expect_equal(length(profiles_no_csv), 4)
expect_equal(dim(profiles_no_csv[[1]]), c(3,9))
expect_equal(profiles_no_csv[[1]][,"name"], c("glm", "priors", "udf"))
for (name in profiles_no_csv[[1]][,"name"]) {
expect_true(name %in% c("udf", "priors", "glm"))
}
})

test_that("profiling errors if no profiling files are present", {
Expand Down
Loading