Repository navigation
docs(nowcast): bound fit concurrency instead of leaving cores idle - #241
Closed
seabbs-bot wants to merge 3 commits into
Closed
seabbs-bot wants to merge 3 commits into
seabbs-bot wants to merge 3 commits into
Conversation
The nowcast case study's four sample() calls (naive, right-truncate, report-triangle, complete) are independent but ran strictly sequentially, so MCMCThreads()'s min(nchains, nthreads()) = 2 threads per call was the most the page ever used, even when more were free. Restructure so the four fits are spawned with Threads.@Spawn and fetched afterwards, sharing whatever thread pool is available. Not yet verified at CI-realistic thread counts (2-4); if that turns out to oversubscribe rather than help, drop this in favour of the chain-count route. Co-authored-by: Sam Abbott <contact@samabbott.co.uk>
Spawning all four fits unconditionally (previous commit) asks for eight threads (four fits x two chains each via MCMCThreads()) and keeps four posteriors' sampler state live at once, regardless of how many threads are actually available. On a CI runner with two or four threads that oversubscribes rather than parallelising, and multiplies peak memory for no gain. Cap concurrent fits at Threads.nthreads() / 2 with asyncmap instead, matching the two-threads-per-fit budget exactly. Verified the scheduling behaviour with a synthetic two-chains-per-fit workload at nthreads = 1, 2 and 4: at 1-2 threads the bounded version is indistinguishable from the original sequential order (no regression, no added memory pressure); at 4 threads it halves wall time by running two fits at a time, matching the theoretical minimum. Real NUTS/Mooncake timings at CI's actual thread count are not yet confirmed; that needs a real CI run rather than this host, which is shared with other agents and not a trustworthy stand-in for a dedicated runner. Co-authored-by: Sam Abbott <contact@samabbott.co.uk>
Codecov Report✅ All modified and coverable lines are covered by tests.
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
Contributor
Try this Pull Request!Option 1: Julia Package ManagerOpen Julia and type: import Pkg
Pkg.activate(temp=true)
Pkg.add(url="https://github.com/EpiAware/ComposableTuringIDModels.jl", rev="fix/177-nowcast-thread-usage")
using ComposableTuringIDModelsOption 2: Local CheckoutIf you have the repo locally: git checkout fix/177-nowcast-thread-usage
julia --project=. -e "using Pkg; Pkg.instantiate()" |
Contributor
|
📖 Documentation preview is ready! View the docs for this PR at: https://EpiAware.github.io/ComposableTuringIDModels.jl/previews/PR241/ This preview will be updated automatically when you push new commits. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This is entirely from an agent so do not review until I have pinged for review as I will do a first pass
Summary
Closes #177 ("Nowcast docs are not using multiple cores correctly").
.github/workflows/document.yamlpasses
julia_num_threads: 'auto'into the reusabledocumentation.ymlworkflow (PR fix(ci): pass julia_num_threads to docs workflow #194), which sets it as a job-level
env:var. Thejulia-actions/julia-docdeploystep's "Build the documentation" step runsinclude("docs/make.jl")as a subprocess of that same job shell, so itinherits
JULIA_NUM_THREADS=auto(confirmed by reading both thepackage's and the reusable workflow's YAML directly). CI was not the
remaining problem.
task docs/task docs-fasthas no thread count at all.Taskfile.ymlis stamped "MANAGED by EpiAwarePackageTools.scaffold — donot edit by hand," and the scaffolded template
(
EpiAware/EpiAwarePackageTools.jltemplates/Taskfile.yml) has thesame gap, so this isn't fixable in this repo. Filed
Scaffolded docs/docs-fast Taskfile tasks don't set a thread count, so local docs builds sample MCMC chains serially EpiAwarePackageTools.jl#380 upstream instead of patching it
here.
MCMCThreads()caps a single fit atmin(nchains, Threads.nthreads())=2 threads (
nchainsis 2 throughout this page, for the cross-chainR-hat diagnostic). The page fits four independent models — naive,
right-truncate, report-triangle, complete — one after another, so any
threads beyond 2 sat idle for the whole page regardless of how many the
build process actually had.
The fix
docs/src/case-studies/realtime-nowcast.mdnow runs the four fits throughasyncmap(nuts_fit, (...); ntasks = Threads.nthreads() ÷ 2)instead offour sequential
sample()calls. Each fit still asksMCMCThreads()fortwo threads (one per chain), so
nthreads() ÷ 2is the largest number offits that can run at once without asking for more threads than exist:
ntasks = 1: fits run one at a time, in the sameorder as before. Identical to current
main.ntasks = 2: two fits run at once, each still gettingits full two-chain allocation. No oversubscription.
are only four).
What I measured vs. reasoned
I do not have a trustworthy CI-scale measurement of the real page, and I'm
not presenting one. Here's exactly what is and isn't backed by a number:
Measured, real dependency:
AbstractMCMC'ssample(..., MCMCThreads(), ...)warns and falls back to serial chainson a single thread (read from the installed
AbstractMCMC/*/src/sample.jl, all versions in this repo's manifests);it does not error. A single-threaded build looks fine and is just slow,
which is why this went unnoticed.
Measured, synthetic, wall time only: a stand-in for the four fits —
each spawning two ~2s busy-CPU tasks, matching
MCMCThreads()'s owntwo-tasks-per-fit shape, but with no Turing/Mooncake compilation, no AD,
no BLAS — run at
-t 1,-t 2, and-t 4on this machine when itbriefly had free capacity:
asyncmap)This is the basis for the
ntasksbound, not a claim about the realpage: at 2 threads (plausibly what a small GitHub-hosted runner has),
the bounded version is indistinguishable from today's sequential order,
as designed. At 4 threads it halves wall time, matching the theoretical
minimum (total work / threads). An earlier draft that spawned all four
fits unconditionally showed no wall-time penalty even at 2 threads in
this synthetic test — Julia's scheduler multiplexes pure CPU-bound tasks
fairly — which is why I don't have a timing argument against it. The
reason I dropped it anyway is memory, not speed:
Not measured, reasoned: peak memory. I have no RSS numbers for
either version, synthetic or real. The synthetic test's busy loop
allocates negligible persistent state, so it can't show a memory
difference even in principle. The structural argument for the bound is:
unconditional spawning holds all four posteriors' model state and
sampler state live simultaneously regardless of thread count, while
ntasks = nthreads() ÷ 2caps how many are live at once to the samenumber the thread budget would support running concurrently anyway. On
a memory-constrained CI runner — and this docs build has already timed
out in CI at 45 minutes, so it is not a machine with headroom to spare —
that seemed like the wrong thing to risk for a benefit I can't
demonstrate at the thread counts CI is likely to have.
Not measured at all: real wall time or memory for the actual
nowcast fits (Turing + Mooncake, real NUTS, real BLAS threading) at any
thread count, on this host or on CI. This dev machine was shared with
several other concurrent agents for most of this investigation
(
~/.claude/hooks/compute-budget.shread "red," load 16-100 on 16cores, for the majority of the session) and isn't a trustworthy stand-in
for an isolated 2-4-core CI runner even when idle. I did not attempt it
and am not reporting a speedup for the real page — only for the
synthetic scheduling model above. This PR's own CI run is the first
trustworthy signal on the real thing.
Verified locally (not performance, correctness): the docs
environment instantiates,
task formatandtask test-qualitypass,task test-fastpasses (1134/1134), and the page still resolves thesame
naive_chain/rt_chain/tri_chain/complete_chain/*_postvariable names the rest of the page (Recent R_t, plots, summary stats,
prior-vs-posterior) depends on.
Scope: the other seven
MCMCThreads(), 250, 2call sitescatalyst-ode.md,delays-dayofweek.md,renewal-negbin.md,split-observations.md, andsir-ode.md(twice) all use the samesample(..., MCMCThreads(), 250, 2)pattern. I checked each: none of themfit multiple independent models back-to-back the way nowcast does.
sir-ode.md's two calls are a base SIR fit and a separate stochasticascertainment model introduced later in the tutorial, built and discussed
sequentially rather than being parallel-fittable without restructuring the
narrative; the rest have exactly one fit each. So the specific defect this
PR fixes — independent fits serialised for no reason — doesn't recur on
those pages, and closing #177 doesn't imply they're covered by this
change.
They do all share the more basic property that a single fit's
MCMCThreads()call never uses more thanmin(nchains, nthreads())= 2threads, however many the build has. That's a real, separate question
(is 2 chains still right, should chain count track thread count, etc.)
that I haven't investigated here — it's not what #177 is about, and I
don't want to bundle an unreviewed change to seven other pages' sampling
behaviour into this PR. Happy to open a follow-up issue if that's wanted.
Notes
docs/src/case-studies/may still move under docs: move case studies to a top-level Tutorials section #229 (open); this branchis rebased onto current
main(post docs: describe components by what they do, not what they replaced #240, chore: auto-increment version to 0.1.2 #217) but not onto docs: move case studies to a top-level Tutorials section #229'srename.
docs/docs-fastTaskfile tasks set no thread count — scaffold-managed, not fixable
here).
This was opened by a bot. Please ping @seabbs for any questions.