Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
45479fb
tmp benchmark candidate
VisruthSK Nov 3, 2026
cbcff0c
Run parallel work on a default mizu pool when one is set
VisruthSK Nov 5, 2026
c4bb23e
Batch mizu work in blocks and strengthen backend tests
VisruthSK Nov 5, 2026
93386aa
Send one block per worker on the mizu pool
VisruthSK Nov 5, 2026
e824b2e
Revert mizu backend for loo_map
VisruthSK Nov 5, 2026
f0e4217
Added some notes on parallelization
VisruthSK Nov 5, 2026
a1f344e
Inline parallel_psis_list wrappers and make deprecated psislw() seque…
VisruthSK Nov 5, 2026
88c9d1b
Make mirai and mori suggested packages
VisruthSK Nov 5, 2026
358ceb4
Document what parallel = TRUE needs
VisruthSK Nov 5, 2026
dfc4c9c
Update tests and comments that still describe cores and forking
VisruthSK Nov 5, 2026
1a6fd2e
State that loo.cores and mc.cores are no longer read
VisruthSK Nov 5, 2026
83bf23c
Point dev-notes at bench/run.sh
VisruthSK Nov 5, 2026
f27b05f
Share large loo_map arguments with mori and copy small ones
VisruthSK Nov 7, 2026
e1b5560
Add loo.parallel and loo.share_bytes options
VisruthSK Nov 7, 2026
4d73f80
Compute normalizing constants and effective sample sizes on the daemons
VisruthSK Nov 7, 2026
51d6786
Compute pointwise loo values per column on the daemons
VisruthSK Nov 7, 2026
ccd65ad
Record the Windows benchmark results and mori sharing measurements in…
VisruthSK Nov 7, 2026
390eb2b
Run the touchstone benchmarks on both the cores and the parallel API
VisruthSK Nov 7, 2026
9bd4e7f
Run the touchstone benchmarks sequentially and on two workers on both…
VisruthSK Nov 7, 2026
24c52ca
Fixed touchstone
VisruthSK Nov 7, 2026
9149964
Record the full Windows and Linux foil benchmark results in dev-notes
VisruthSK Nov 7, 2026
74f038b
Explain the Linux benchmark slowdowns as daemon start-up
VisruthSK Nov 7, 2026
f85b289
Record per-call timings and closure size for the function-based paths
VisruthSK Nov 7, 2026
80c28ce
Separate daemon start-up from run time in the Linux benchmark comparison
VisruthSK Nov 7, 2026
c0de6b5
Add 32-worker Linux results, vignette timings and scaling plots to de…
VisruthSK Nov 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
9 changes: 6 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,24 @@ Depends:
Imports:
checkmate,
matrixStats (>= 0.52),
parallel,
posterior (>= 1.7.0),
stats
stats,
utils
Suggests:
bayesplot (>= 1.7.0),
brms (>= 2.10.0),
ggplot2,
graphics,
knitr,
mirai,
mori,
rmarkdown,
rstan,
rstanarm (>= 2.19.0),
rstantools,
spdep,
testthat (>= 3.0)
testthat (>= 3.0),
withr
VignetteBuilder:
knitr
Config/testthat/edition: 3
Expand Down
4 changes: 0 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,6 @@ importFrom(matrixStats,colSums2)
importFrom(matrixStats,colVars)
importFrom(matrixStats,logSumExp)
importFrom(matrixStats,rowMaxs)
importFrom(parallel,makePSOCKcluster)
importFrom(parallel,mclapply)
importFrom(parallel,parLapply)
importFrom(parallel,stopCluster)
importFrom(stats,constrOptim)
importFrom(stats,nobs)
importFrom(stats,qnorm)
Expand Down
4 changes: 3 additions & 1 deletion R/E_loo.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#'
#'
#' @examples
#' mirai::daemons(2)
#' \donttest{
#' if (requireNamespace("rstanarm", quietly = TRUE)) {
#' # Use rstanarm package to quickly fit a model and get both a log-likelihood
Expand All @@ -80,7 +81,7 @@
#' dim(log_ratios)
#'
#' r_eff <- relative_eff(exp(-log_ratios), chain_id = rep(1:4, each = 1000))
#' psis_object <- psis(log_ratios, r_eff = r_eff, cores = 2)
#' psis_object <- psis(log_ratios, r_eff = r_eff)
#'
#' E_loo(yrep, psis_object, type = "mean")
#' E_loo(yrep, psis_object, type = "var")
Expand All @@ -94,6 +95,7 @@
#' }
#' }
#'
#' mirai::daemons(0)
E_loo <- function(x, psis_object, ...) {
UseMethod("E_loo")
}
Expand Down
19 changes: 9 additions & 10 deletions R/crps.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ crps.numeric <- function(x, x2, y, ..., permutations = 1) {
#' @param log_lik A log-likelihood matrix the same size as `x`.
#' @param r_eff An optional vector of relative effective sample size estimates
#' containing one element per observation. See [psis()] for details.
#' @param cores The number of cores to use for parallelization of `[psis()]`.
#' See [psis()] for details.
#' @template parallel
loo_crps.matrix <-
function(x,
x2,
Expand All @@ -113,13 +112,13 @@ loo_crps.matrix <-
...,
permutations = 1,
r_eff = 1,
cores = getOption("mc.cores", 1)) {
parallel = getOption("loo.parallel", TRUE)) {
validate_crps_input(x, x2, y, log_lik)
repeats <- replicate(permutations,
EXX_loo_compute(x, x2, log_lik, r_eff = r_eff, ...),
EXX_loo_compute(x, x2, log_lik, r_eff = r_eff, parallel = parallel, ...),
simplify = F)
EXX <- Reduce(`+`, repeats) / permutations
psis_obj <- psis(-log_lik, r_eff = r_eff, cores = cores)
psis_obj <- psis(-log_lik, r_eff = r_eff, parallel = parallel)
EXy <- E_loo(abs(sweep(x, 2, y)), psis_obj, log_ratios = -log_lik, ...)$value
crps_output(.crps_fun(EXX, EXy))
}
Expand Down Expand Up @@ -155,13 +154,13 @@ loo_scrps.matrix <-
...,
permutations = 1,
r_eff = 1,
cores = getOption("mc.cores", 1)) {
parallel = getOption("loo.parallel", TRUE)) {
validate_crps_input(x, x2, y, log_lik)
repeats <- replicate(permutations,
EXX_loo_compute(x, x2, log_lik, r_eff = r_eff, ...),
EXX_loo_compute(x, x2, log_lik, r_eff = r_eff, parallel = parallel, ...),
simplify = F)
EXX <- Reduce(`+`, repeats) / permutations
psis_obj <- psis(-log_lik, r_eff = r_eff, cores = cores)
psis_obj <- psis(-log_lik, r_eff = r_eff, parallel = parallel)
EXy <- E_loo(abs(sweep(x, 2, y)), psis_obj, log_ratios = -log_lik, ...)$value
crps_output(.crps_fun(EXX, EXy, scale = TRUE))
}
Expand All @@ -175,12 +174,12 @@ EXX_compute <- function(x, x2) {
}


EXX_loo_compute <- function(x, x2, log_lik, r_eff = 1, ...) {
EXX_loo_compute <- function(x, x2, log_lik, r_eff = 1, parallel, ...) {
S <- nrow(x)
shuffle <- sample (1:S)
x2 <- x2[shuffle,]
log_lik2 <- log_lik[shuffle,]
psis_obj_joint <- psis(-log_lik - log_lik2 , r_eff = r_eff)
psis_obj_joint <- psis(-log_lik - log_lik2 , r_eff = r_eff, parallel = parallel)
E_loo(abs(x - x2), psis_obj_joint, log_ratios = -log_lik - log_lik2, ...)$value
}

Expand Down
97 changes: 34 additions & 63 deletions R/effective_sample_sizes.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
#' `x` (if a vector). No `chain_id` is needed if `x` is a 3-D
#' array. If there are `C` chains then valid chain indexes are values
#' in `1:C`.
#' @param cores The number of cores to use for parallelization.
#' @template parallel
#' @return A vector of relative effective sample sizes.
#'
#' @examples
#' mirai::daemons(2)
#' LLarr <- example_loglik_array()
#' LLmat <- example_loglik_matrix()
#' dim(LLarr)
Expand All @@ -31,6 +32,7 @@
#' rel_n_eff_2 <- relative_eff(exp(LLmat), chain_id = rep(1:2, each = 500))
#' all.equal(rel_n_eff_1, rel_n_eff_2)
#'
#' mirai::daemons(0)
relative_eff <- function(x, ...) {
UseMethod("relative_eff")
}
Expand All @@ -42,48 +44,33 @@ relative_eff <- function(x, ...) {
relative_eff.default <- function(x, chain_id, ...) {
dim(x) <- c(length(x), 1)
class(x) <- "matrix"
relative_eff.matrix(x, chain_id)
relative_eff.matrix(x, chain_id, parallel = FALSE)
}

#' @export
#' @templateVar fn relative_eff
#' @template matrix
#'
relative_eff.matrix <- function(x, chain_id, ..., cores = getOption("mc.cores", 1)) {
relative_eff.matrix <- function(x, chain_id, ..., parallel = getOption("loo.parallel", TRUE)) {
x <- llmatrix_to_array(x, chain_id)
relative_eff.array(x, cores = cores)
relative_eff.array(x, parallel = parallel)
}

#' @export
#' @templateVar fn relative_eff
#' @template array
#'
relative_eff.array <- function(x, ..., cores = getOption("mc.cores", 1)) {
relative_eff.array <- function(x, ..., parallel = getOption("loo.parallel", TRUE)) {
stopifnot(length(dim(x)) == 3)
S <- prod(dim(x)[1:2]) # posterior sample size = iter * chains

if (cores == 1) {
n_eff_vec <- apply(x, 3, posterior::ess_mean)
} else {
if (!os_is_windows()) {
n_eff_list <-
parallel::mclapply(
mc.cores = cores,
X = seq_len(dim(x)[3]),
FUN = function(i) posterior::ess_mean(x[, , i, drop = TRUE])
)
} else {
cl <- parallel::makePSOCKcluster(cores)
on.exit(parallel::stopCluster(cl))
n_eff_list <-
parallel::parLapply(
cl = cl,
X = seq_len(dim(x)[3]),
fun = function(i) posterior::ess_mean(x[, , i, drop = TRUE])
)
}
n_eff_vec <- unlist(n_eff_list, use.names = FALSE)
}
n_eff_list <- loo_map(
seq_len(dim(x)[3]),
ess_mean_i,
x = slices(x, parallel),
parallel = parallel
)
n_eff_vec <- unlist(n_eff_list, use.names = FALSE)

return(n_eff_vec / S)
}
Expand All @@ -97,53 +84,37 @@ relative_eff.function <-
function(x,
chain_id,
...,
cores = getOption("mc.cores", 1),
parallel = getOption("loo.parallel", TRUE),
data = NULL,
draws = NULL) {

f_i <- validate_llfun(x) # not really an llfun, should return exp(ll) or exp(-ll)
N <- dim(data)[1]

if (cores == 1) {
n_eff_list <-
lapply(
X = seq_len(N),
FUN = function(i) {
val_i <- f_i(data_i = data[i, , drop = FALSE], draws = draws, ...)
relative_eff.default(as.vector(val_i), chain_id = chain_id, cores = 1)
}
)
} else {
if (!os_is_windows()) {
n_eff_list <-
parallel::mclapply(
X = seq_len(N),
FUN = function(i) {
val_i <- f_i(data_i = data[i, , drop = FALSE], draws = draws, ...)
relative_eff.default(as.vector(val_i), chain_id = chain_id, cores = 1)
},
mc.cores = cores
)
} else {
cl <- parallel::makePSOCKcluster(cores)
parallel::clusterExport(cl=cl, varlist=c("draws", "chain_id", "data"), envir=environment())
on.exit(parallel::stopCluster(cl))
n_eff_list <-
parallel::parLapply(
cl = cl,
X = seq_len(N),
fun = function(i) {
val_i <- f_i(data_i = data[i, , drop = FALSE], draws = draws, ...)
relative_eff.default(as.vector(val_i), chain_id = chain_id, cores = 1)
}
)
}
}
n_eff_list <- loo_map(
seq_len(N),
relative_eff_i,
data = data,
f_i = f_i,
draws = draws,
chain_id = chain_id,
...,
parallel = parallel
)

n_eff_vec <- unlist(n_eff_list, use.names = FALSE)
return(n_eff_vec)
}

ess_mean_i <- function(i, x) {
posterior::ess_mean(slice(x, i))
}

relative_eff_i <- function(i, data, f_i, draws, chain_id, ...) {
val_i <- f_i(data_i = data[i, , drop = FALSE], draws = draws, ...)
relative_eff.default(as.vector(val_i), chain_id = chain_id)
}

#' @export
#' @describeIn relative_eff
#' If `x` is an object of class `"psis"`, `relative_eff()` simply returns
Expand Down
105 changes: 91 additions & 14 deletions R/helpers.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
#' Detect if OS is Windows
#' @noRd
os_is_windows <- function() {
checkmate::test_os("windows")
}

#' More stable version of `log(mean(exp(x)))`
#'
#' @noRd
Expand Down Expand Up @@ -253,16 +247,95 @@ nlist <- function(...) {
return(out)
}

#' Map a function over `X`, optionally on mirai daemons
#'
#' With `parallel = FALSE` this is `lapply()`. With `parallel = TRUE`, `X` is
#' split into blocks that run on the daemons set up with [mirai::daemons()], and
#' the results come back in the original order.
#'
#' `FUN` and `...` are sent to the daemons, so `FUN` should not capture a large
#' environment; pass data as arguments instead. Local daemons read arguments
#' larger than `share_bytes` (or all of them when `batch = FALSE`) from shared memory
#' ([mori::share()]) and copy the rest; remote daemons receive
#' copies. Warnings raised on a daemon are signalled again on the host, and the
#' first error stops the map.
#'
#' @noRd
#' @param X A vector or list to map over.
#' @param FUN The function to apply to each element of `X`.
#' @param ... Further arguments passed to `FUN`.
#' @param parallel If `TRUE`, run on mirai daemons.
#' @param batch If `TRUE`, send each daemon one block of consecutive elements
#' as a single task, which suits many cheap elements. If `FALSE`, send each
#' element as its own task so that idle daemons take the next one, which
#' suits a few expensive, uneven elements.
#' @param share_bytes Arguments larger than this many bytes are shared with
#' mori on local daemons; smaller ones are copied.
#' @return A list the length of `X`, as from `lapply()`.
loo_map <- function(X, FUN, ..., parallel, batch = TRUE,
share_bytes = getOption("loo.share_bytes", 4 * 2^20)) {
if (!parallel) {
return(lapply(X, FUN, ...))
}
check_parallel_packages()
mirai::require_daemons()
n_blocks <- if (batch) mirai::info()[["connections"]] else length(X)
groups <- unname(split(X, ceiling(seq_along(X) * n_blocks / length(X))))
share <- if (daemons_are_local()) function(x) share_if_large(x, batch, share_bytes) else identity
args <- c(list(.fun = FUN), lapply(list(...), share))
blocks <- mirai::mirai_map(groups, lapply_with_warnings, .args = args)[mirai::.stop]
lapply(concat_field(blocks, "warnings"), warning)
concat_field(blocks, "value")
}

#' Split a matrix or 3-d array along its last dimension
#'
#' mori shares a list of vectors without copying it, but gives every daemon a
#' private copy of a whole array as soon as the array is indexed. Daemons
#' therefore receive `slices(x, parallel)` and read it with `slice(x, i)`.
#' Sequential calls keep `x` as it is.
#'
#' @noRd
slices <- function(x, parallel) {
if (!parallel) {
return(x)
}
lapply(seq_len(dim(x)[length(dim(x))]), slice, x = x)
}

slice <- function(x, i) {
if (is.list(x)) x[[i]] else if (length(dim(x)) == 2L) x[, i] else x[, , i]
}

# Check how many cores to use and throw deprecation warning if loo.cores is used
loo_cores <- function(cores) {
loo_cores_op <- getOption("loo.cores", NA)
if (!is.na(loo_cores_op) && (loo_cores_op != cores)) {
cores <- loo_cores_op
warning("'loo.cores' is deprecated, please use 'mc.cores' or pass 'cores' explicitly.",
call. = FALSE)
check_parallel_packages <- function(packages = c("mirai", "mori")) {
missing <- packages[!vapply(packages, requireNamespace, NA, quietly = TRUE)]
if (length(missing) > 0L) {
stop(
"`parallel = TRUE` needs the ", paste0("'", missing, "'", collapse = " and "),
" package. Install it, or use `parallel = FALSE`.",
call. = FALSE
)
}
return(cores)
}

daemons_are_local <- function() {
isTRUE(grepl("^(ipc|abstract)://", mirai::nextget("url")))
}

concat_field <- function(blocks, field) {
unlist(lapply(blocks, `[[`, field), recursive = FALSE)
}

lapply_with_warnings <- function(block, .fun, ...) {
warnings <- list()
value <- withCallingHandlers(
lapply(block, .fun, ...),
warning = function(w) {
warnings <<- c(warnings, list(w))
invokeRestart("muffleWarning")
}
)
list(value = value, warnings = warnings)
}


Expand All @@ -280,3 +353,7 @@ release_questions <- function() {
is_constant <- function(x, tol = .Machine$double.eps) {
abs(max(x) - min(x)) < tol
}

share_if_large <- function(x, batch, share_bytes) {
if (!batch || utils::object.size(x) > share_bytes) mori::share(x) else x
}
Loading
Loading