Skip to content

Parallelization of loo using mirai and mori#378

Draft
florence-bockting wants to merge 11 commits into
loo-v3.0.0from
parallelization
Draft

Parallelization of loo using mirai and mori#378
florence-bockting wants to merge 11 commits into
loo-v3.0.0from
parallelization

Conversation

@florence-bockting

@florence-bockting florence-bockting commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #308

  • Replaces parallel::mclapply() / parLapply() with mirai + mori for per-observation parallelism (cross-platform, including Windows).
  • Introduces two recommended parallelism workflows:
    • User-managed pools via mirai::daemons() (local, remote/SSH/HPC); a connected pool always wins.
    • Map-style jobs via exported loo_mirai() (optional n_daemons for one-off pools).
  • Deprecates the cores argument and options(mc.cores) as loo parallelism controls (still functional as a per-call local pool bridge when > 1 and no pool is connected; once-per-session warning).
  • Removes options(loo.cores) (deprecated since 2.x).

What changed

Core (R/parallel.R): with_loo_daemons(), loo_map(), loo_pool_is_local(), loo_n_workers(), loo_mirai(); deprecation helpers for cores / mc.cores.

Parallelized functions: loo() (function method), psis()/sis()/tis(), relative_eff(), loo_subsample(), loo_moment_match(), loo_model_weights().

Pool precedence: any connected mirai::daemons() pool (user-managed) always wins > deprecated cores is ignored. Local pools use mori zero-copy for broadcast objects (e.g. draws); remote pools serialize (with conservative chunking when broadcasting).

Also: mirai + mori in DESCRIPTION; vignettes/loo2-parallel.Rmd; vignettes/migration-guide.Rmd;
tests/testthat/test_parallel.R; benchmark/ scripts + bench-comparison.md.

Review guide

  1. vignettes/loo2-parallel.Rmd shows the user-facing model
  2. vignettes/migration-guide.Rmd provides a parallelization migration table and examples
  3. R/parallel.R includes the pool lifecycle + loo_map() transport, loo_mirai()
  4. tests/testthat/test_parallel.R include serial/parallel equivalence, pool precedence, loo_mirai(), deprecation warnings
  5. benchmark/README.md includes first attempt of a small baseline vs new comparison (see first results in benchmark/bench-comparison.md)

Initial benchmarks (Linux, one machine): loo.function + large draws benefits most (~4× with persistent pool); matrix psis() does not (communication-bound); per-call pool pays ~1s spawn/teardown per call.

Follow-up work

  • Reviewing: Please have a look at the current implementation and check it for correctness and usability. Any comments and improvements are welcome.
  • Benchmarking: master vs this branch across problem sizes, all (or selected number of) parallelized functions, OSes (Linux/macOS/Windows), and metrics (wall-clock, allocation, peak RSS). You can for example extend benchmark/.
  • LSAT case study: posteriorDB lsat-data; showcase speedup and all three parallelism modes (one-off, persistent pool, simulation loop).
  • Remote SSH: two-machine test; verify correctness, measure speedup, document setup (mirai::daemons(url = ..., remote = ssh_config(...))).
  • Documentation: expand vignettes/loo2-parallel.Rmd with assumptions, when-to-use guidance, function-specific notes, memory model.

Current limitations of implementation

  • Matrix psis() rarely speeds up (large data shipped per worker).
  • Per-call cores > 1 can be slower than serial on small problems without loo.daemons.
  • Remote SSH untested in CI

This PR has been developed with assistance of Cursor.

@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown

This is how benchmark results would change (along with a 95% confidence interval in relative change) if 30834a4 is merged into loo-v3.0.0:

  • ❗🐌loo_function: 1.91s -> 2.03s [+6%, +6.91%]
  • ❗🐌loo_matrix: 1.71s -> 1.75s [+1.73%, +2.75%]
    Further explanation regarding interpretation and methodology can be found in the documentation.

@florence-bockting florence-bockting mentioned this pull request Jul 1, 2026
6 tasks
@florence-bockting florence-bockting changed the base branch from master to loo-v3.0.0 July 1, 2026 09:33
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.65625% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.39%. Comparing base (fe16a43) to head (30834a4).

Files with missing lines Patch % Lines
R/parallel.R 95.72% 5 Missing ⚠️
R/psis_approximate_posterior.R 50.00% 1 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff               @@
##           loo-v3.0.0     #378      +/-   ##
==============================================
+ Coverage       92.70%   94.39%   +1.68%     
==============================================
  Files              31       32       +1     
  Lines            3029     3121      +92     
==============================================
+ Hits             2808     2946     +138     
+ Misses            221      175      -46     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parallelisation of loo using Mirai

2 participants