-
-
Notifications
You must be signed in to change notification settings - Fork 65
Add parsers for cpp opts #1085
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
Merged
Merged
Add parsers for cpp opts #1085
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
81152c9
Add parsers for cpp opts
katrinabrock 59660ce
fixup
katrinabrock 6afd881
fixup
katrinabrock e707676
Update R/cpp_opts.R
katrinabrock a336bdf
Apply suggestions from code review
katrinabrock 0f90699
Update test-cpp_opts.R
katrinabrock dde1164
fix test/code mismatch and add docs
katrinabrock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,197 @@ | ||||||||||||
# Internal functions for handling cpp options | ||||||||||||
|
||||||||||||
# running and parsing exe info -------------------------------- | ||||||||||||
# run <model> info command | ||||||||||||
run_info_cli <- function(exe_file) { | ||||||||||||
withr::with_path( | ||||||||||||
c( | ||||||||||||
toolchain_PATH_env_var(), | ||||||||||||
tbb_path() | ||||||||||||
), | ||||||||||||
ret <- wsl_compatible_run( | ||||||||||||
command = wsl_safe_path(exe_file), | ||||||||||||
args = "info", | ||||||||||||
echo = is_verbose_mode(), | ||||||||||||
error_on_status = FALSE | ||||||||||||
) | ||||||||||||
) | ||||||||||||
ret | ||||||||||||
} | ||||||||||||
|
||||||||||||
# new (future) parser | ||||||||||||
# Parse the string output of <model> `info` into an R object (list) | ||||||||||||
parse_exe_info_string <- function(ret_stdout) { | ||||||||||||
info <- list() | ||||||||||||
info_raw <- strsplit(strsplit(ret_stdout, "\n")[[1]], "=") | ||||||||||||
for (key_val in info_raw) { | ||||||||||||
if (length(key_val) > 1) { | ||||||||||||
key_val <- trimws(key_val) | ||||||||||||
val <- key_val[2] | ||||||||||||
if (!is.na(as.logical(val))) { | ||||||||||||
val <- as.logical(val) | ||||||||||||
} | ||||||||||||
info[[tolower(key_val[1])]] <- val | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
info[["stan_version"]] <- paste0( | ||||||||||||
info[["stan_version_major"]], | ||||||||||||
".", | ||||||||||||
info[["stan_version_minor"]], | ||||||||||||
".", info[["stan_version_patch"]] | ||||||||||||
) | ||||||||||||
info[["stan_version_major"]] <- NULL | ||||||||||||
info[["stan_version_minor"]] <- NULL | ||||||||||||
info[["stan_version_patch"]] <- NULL | ||||||||||||
|
||||||||||||
info | ||||||||||||
} | ||||||||||||
|
||||||||||||
# old (current) parser | ||||||||||||
model_compile_info <- function(exe_file, version) { | ||||||||||||
info <- NULL | ||||||||||||
if (version > "2.26.1") { | ||||||||||||
|
||||||||||||
ret <- run_info_cli(exe_file) | ||||||||||||
if (ret$status == 0) { | ||||||||||||
info <- list() | ||||||||||||
info_raw <- strsplit(strsplit(ret$stdout, "\n")[[1]], "=") | ||||||||||||
for (key_val in info_raw) { | ||||||||||||
if (length(key_val) > 1) { | ||||||||||||
key_val <- trimws(key_val) | ||||||||||||
val <- key_val[2] | ||||||||||||
if (!is.na(as.logical(val))) { | ||||||||||||
val <- as.logical(val) | ||||||||||||
} | ||||||||||||
info[[toupper(key_val[1])]] <- val | ||||||||||||
} | ||||||||||||
} | ||||||||||||
info[["STAN_VERSION"]] <- paste0(info[["STAN_VERSION_MAJOR"]], ".", info[["STAN_VERSION_MINOR"]], ".", info[["STAN_VERSION_PATCH"]]) | ||||||||||||
info[["STAN_VERSION_MAJOR"]] <- NULL | ||||||||||||
info[["STAN_VERSION_MINOR"]] <- NULL | ||||||||||||
info[["STAN_VERSION_PATCH"]] <- NULL | ||||||||||||
} | ||||||||||||
} | ||||||||||||
info | ||||||||||||
} | ||||||||||||
|
||||||||||||
# convert to compile flags -------------------- | ||||||||||||
cpp_options_to_compile_flags <- function(cpp_options) { | ||||||||||||
if (length(cpp_options) == 0) { | ||||||||||||
return(NULL) | ||||||||||||
} | ||||||||||||
cpp_built_options <- c() | ||||||||||||
for (i in seq_along(cpp_options)) { | ||||||||||||
option_name <- names(cpp_options)[i] | ||||||||||||
Comment on lines
+86
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
if (is.null(option_name) || !nzchar(option_name)) { | ||||||||||||
cpp_built_options <- c(cpp_built_options, cpp_options[[i]]) | ||||||||||||
} else { | ||||||||||||
cpp_built_options <- c(cpp_built_options, paste0(toupper(option_name), "=", cpp_options[[i]])) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
cpp_built_options | ||||||||||||
} | ||||||||||||
|
||||||||||||
|
||||||||||||
# check options overall for validity --------------------------------- | ||||||||||||
|
||||||||||||
validate_cpp_options <- function(cpp_options) { | ||||||||||||
if (is.null(cpp_options) || length(cpp_options) == 0) return(list()) | ||||||||||||
|
||||||||||||
if ( | ||||||||||||
!is.null(cpp_options[["user_header"]]) && | ||||||||||||
!is.null(cpp_options[["USER_HEADER"]]) | ||||||||||||
) { | ||||||||||||
warning( | ||||||||||||
"User header specified both via cpp_options[[\"USER_HEADER\"]] ", | ||||||||||||
"and cpp_options[[\"user_header\"]].", | ||||||||||||
katrinabrock marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
call. = FALSE | ||||||||||||
) | ||||||||||||
} | ||||||||||||
|
||||||||||||
names(cpp_options) <- tolower(names(cpp_options)) | ||||||||||||
flags_set_if_defined <- c( | ||||||||||||
# cmdstan | ||||||||||||
"stan_threads", "stan_mpi", "stan_opencl", | ||||||||||||
"stan_no_range_checks", "stan_cpp_optims", | ||||||||||||
# stan math | ||||||||||||
"integrated_opencl", "tbb_lib", "tbb_inc", "tbb_interface_new" | ||||||||||||
) | ||||||||||||
for (flag in flags_set_if_defined) { | ||||||||||||
if (isFALSE(cpp_options[[flag]])) warning( | ||||||||||||
toupper(flag), " set to ", cpp_options[flag], | ||||||||||||
" Since this is a non-empty value, ", | ||||||||||||
"it will result in the corresponding ccp option being turned ON. To turn this", | ||||||||||||
" option off, use cpp_options = list(", flag, " = NULL)." | ||||||||||||
) | ||||||||||||
} | ||||||||||||
SteveBronder marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
cpp_options | ||||||||||||
} | ||||||||||||
|
||||||||||||
# check specific options for validity --------------------------------- | ||||||||||||
|
||||||||||||
assert_valid_opencl <- function(opencl_ids, cpp_options) { | ||||||||||||
if (is.null(cpp_options[["stan_opencl"]]) | ||||||||||||
&& !is.null(opencl_ids)) { | ||||||||||||
stop("'opencl_ids' is set but the model was not compiled with for use with OpenCL.", | ||||||||||||
"\nRecompile the model with 'cpp_options = list(stan_opencl = TRUE)'", | ||||||||||||
katrinabrock marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
call. = FALSE) | ||||||||||||
} | ||||||||||||
invisible(opencl_ids) | ||||||||||||
} | ||||||||||||
|
||||||||||||
assert_valid_threads <- function(threads, cpp_options, multiple_chains = FALSE) { | ||||||||||||
threads_arg <- if (multiple_chains) "threads_per_chain" else "threads" | ||||||||||||
checkmate::assert_integerish(threads, .var.name = threads_arg, | ||||||||||||
null.ok = TRUE, lower = 1, len = 1) | ||||||||||||
if (is.null(cpp_options[["stan_threads"]]) || !isTRUE(cpp_options[["stan_threads"]])) { | ||||||||||||
if (!is.null(threads)) { | ||||||||||||
warning( | ||||||||||||
"'", threads_arg, "' is set but the model was not compiled with ", | ||||||||||||
"'cpp_options = list(stan_threads = TRUE)' ", | ||||||||||||
"so '", threads_arg, "' will have no effect!", | ||||||||||||
call. = FALSE | ||||||||||||
) | ||||||||||||
threads <- NULL | ||||||||||||
} | ||||||||||||
} else if (isTRUE(cpp_options[["stan_threads"]]) && is.null(threads)) { | ||||||||||||
stop( | ||||||||||||
"The model was compiled with 'cpp_options = list(stan_threads = TRUE)' ", | ||||||||||||
"but '", threads_arg, "' was not set!", | ||||||||||||
call. = FALSE | ||||||||||||
) | ||||||||||||
} | ||||||||||||
invisible(threads) | ||||||||||||
} | ||||||||||||
|
||||||||||||
# compare exe info and cpp options to one another | ||||||||||||
exe_info_style_cpp_options <- function(cpp_options) { | ||||||||||||
if(is.null(cpp_options)) cpp_options <- list() | ||||||||||||
names(cpp_options) <- tolower(names(cpp_options)) | ||||||||||||
flags_reported_in_exe_info <- c( | ||||||||||||
"stan_threads", "stan_mpi", "stan_opencl", | ||||||||||||
"stan_no_range_checks", "stan_cpp_optims" | ||||||||||||
) | ||||||||||||
for (flag in flags_reported_in_exe_info) { | ||||||||||||
cpp_options[[flag]] <- !( | ||||||||||||
is.null(cpp_options[[flag]]) || cpp_options[[flag]] == "" | ||||||||||||
) | ||||||||||||
} | ||||||||||||
cpp_options | ||||||||||||
} | ||||||||||||
|
||||||||||||
exe_info_reflects_cpp_options <- function(exe_info, cpp_options) { | ||||||||||||
if (length(exe_info) == 0) { | ||||||||||||
warning("Recompiling is recommended due to missing exe_info.") | ||||||||||||
return(TRUE) | ||||||||||||
} | ||||||||||||
if (is.null(cpp_options)) return(TRUE) | ||||||||||||
|
||||||||||||
cpp_options <- exe_info_style_cpp_options(cpp_options)[tolower(names(cpp_options))] | ||||||||||||
overlap <- names(cpp_options)[names(cpp_options) %in% names(exe_info)] | ||||||||||||
|
||||||||||||
if (length(overlap) == 0) TRUE else all.equal( | ||||||||||||
exe_info[overlap], | ||||||||||||
cpp_options[overlap] | ||||||||||||
) | ||||||||||||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
test_that("parse_exe_info_string works", { | ||
expect_equal_ignore_order( | ||
parse_exe_info_string(" | ||
stan_version_major = 2 | ||
stan_version_minor = 38 | ||
stan_version_patch = 0 | ||
STAN_THREADS=false | ||
STAN_MPI=false | ||
STAN_OPENCL=true | ||
STAN_NO_RANGE_CHECKS=false | ||
STAN_CPP_OPTIMS=false | ||
"), | ||
list( | ||
stan_version = "2.38.0", | ||
stan_threads = FALSE, | ||
stan_mpi = FALSE, | ||
stan_opencl = TRUE, | ||
stan_no_range_checks = FALSE, | ||
stan_cpp_optims = FALSE | ||
) | ||
) | ||
}) | ||
|
||
test_that("validate_cpp_options works", { | ||
expect_equal_ignore_order( | ||
validate_cpp_options(list( | ||
Stan_Threads = TRUE, | ||
STAN_OPENCL = NULL, | ||
aBc = FALSE | ||
)), | ||
list( | ||
stan_threads = TRUE, | ||
stan_opencl = NULL, | ||
abc = FALSE | ||
) | ||
) | ||
expect_warning(validate_cpp_options(list(STAN_OPENCL = FALSE))) | ||
}) | ||
|
||
|
||
test_that("exe_info cpp_options comparison works", { | ||
exe_info_all_flags_off <- exe_info_style_cpp_options(list()) | ||
exe_info_all_flags_off[["stan_version"]] <- "35.0.0" | ||
|
||
expect_true(exe_info_reflects_cpp_options( | ||
exe_info_all_flags_off, | ||
list() | ||
)) | ||
expect_true(exe_info_reflects_cpp_options( | ||
list(stan_opencl = FALSE), | ||
list(stan_opencl = NULL) | ||
)) | ||
expect_not_true(exe_info_reflects_cpp_options( | ||
list(stan_opencl = FALSE), | ||
list(stan_opencl = FALSE) | ||
)) | ||
expect_not_true(exe_info_reflects_cpp_options( | ||
list(stan_opencl = FALSE, stan_threads = FALSE), | ||
list(stan_opencl = NULL, stan_threads = TRUE) | ||
)) | ||
expect_not_true(exe_info_reflects_cpp_options( | ||
list(stan_opencl = FALSE, stan_threads = FALSE), | ||
list(stan_opencl = NULL, stan_threads = TRUE, EXTRA_ARG = TRUE) | ||
)) | ||
|
||
# no exe_info -> no recompile based on cpp info | ||
expect_warning( | ||
expect_true(exe_info_reflects_cpp_options(list(), list())), | ||
"Recompiling is recommended" | ||
) | ||
}) | ||
katrinabrock marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.