Replace FLoops with manual Threads.@spawn#215
Open
devmotion wants to merge 1 commit intoJuliaML:mainfrom
Open
Conversation
Drops the FLoops dependency. Parallel iteration in `Loader` is rewritten as a recursive divide-and-conquer over `argiter[lo:hi]` using `Threads.@spawn`, mirroring `Transducers._reduce`: at each level, the right half is spawned and the left half recurses on the current task, with leaves (size <= basesize) processed sequentially. Default `basesize = length ÷ nthreads()` matches the FLoops `ThreadedEx` default, and the base-case check uses `max(basesize, 1)` so small inputs (`numobs < nthreads`) terminate. The outer dispatcher uses `Threads.@spawn` instead of `@async` to avoid making the caller's task non-migratable (per the `@async` docstring's warning about library code). Also moves Transducers from a required dep to a weakdep + extension (`ext/TransducersExt.jl`) and bumps the minimum Julia version to 1.10 (with the matching CI.yml `min-patch` switch). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
8cd5fea to
acbbb8f
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #215 +/- ##
==========================================
- Coverage 84.93% 84.83% -0.11%
==========================================
Files 15 18 +3
Lines 697 712 +15
==========================================
+ Hits 592 604 +12
- Misses 105 108 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Motivation
FLoops.jlandTransducers.jlare both registered out of the JuliaFolds2 organization, which forked the original JuliaFolds packages after upstream went silent (see JuliaFolds2/Transducers.jl#31, the issue that motivated the fork). The forks are alive but very low-velocity:JuliaFolds2/FLoops.jlhas had 14 commits in 2025 (almost entirely automated CompatHelper / pkg-update PRs), andJuliaFolds2/Transducers.jlhas had 1 commit in 2025 (housekeeping — drop Julia <1.10, remove Requires).Removing this part of the stack from
MLUtilshas been on the radar for a while:OhMyThreadsas a replacement.Other packages in the ecosystem have made the same call: AbstractMCMC.jl removed
Transducersas a hard dep in TuringLang/AbstractMCMC.jl#201 (merged April 2026), citing 11 transitive dependencies eliminated.In a fresh environment with this branch's
MLUtilsinstalled (61 packages resolved), addingFLoopsbrings the total to 85 packages — 24 added (about 20 non-stdlib):FLoops,FLoopsBase,Transducers,MLStyle,JuliaVariables,NameResolution,Setfield,ContextVariablesX,Accessors,BangBang,MicroCollections,SplittablesBase,InitialValues,DefineSingletons,Baselet,CompositionsBase,ConstructionBase,ArgCheck,PrettyPrint,InverseFunctions, plus a handful of stdlibs thatTransducerspulls. Some of these may well already be loaded for other reasons in any given user's project, but they are not currently shared with anything elseMLUtilsdepends on.Approach
Replace
FLoops.@floop ThreadedEx() for arg in argiterinLoaderwith a recursive divide-and-conquer usingThreads.@spawndirectly. The structure mirrorsTransducers._reduce: at each level the right half is@spawn'd, the left half recurses on the current task, thenwaiton the right.basesize = length ÷ Threads.nthreads()matches theTransducers.ThreadedExdefault; the base-case predicate usesmax(basesize, 1)(also lifted from Transducers) so thatnumobs < nthreadscases terminate.The outer dispatcher uses
Threads.@spawnrather than@asyncto avoid making the calling task non-migratable for the lifetime of the iteration, per the warning in the@asyncdocstring about library code.basesizeis currently computed inline. If there's interest, it could be exposed (as a kwarg oneachobsparallel/DataLoader) in a follow-up so users can tune task granularity per call without a major API change.Also moves
Transducersfrom a required dep to a weakdep + extension (ext/TransducersExt.jl), and bumps the minimum Julia version to 1.10 (needed for weakdeps; CI matrix updated tomin-patch).Alternatives considered
OhMyThreads.jl— the alternative explicitly raised in Floops depedency should be removed #175. More actively maintained, drop-in for the parallel dispatch, but its default chunking strategy differs from FLoops/Transducers — see the OhMyThreads column in the table below:TimeDataset n=10000, t=1e-3goes from3.06 s(master) to4.31 s. Recovering parity needs explicit tuning of the number of tasks (chunking=falsebrings it to2.85 s, but spawns one task per observation, which is its own footgun for largenumobs). Going with manualThreads.@spawnkeeps exactly the algorithm that was running underneath FLoops, i.e. characteristics don't change for users.FLoops, narrow the surface area — doesn't address the dependency-footprint or maintenance velocity concerns.Benchmarks
Julia 1.12.6, 6 threads. Single trials; ~10% run-to-run variance on the
TimeDatasetrows.Net: TimeDataset (representative of I/O-bound
getobs) is consistently slightly faster than master and substantially better than OhMyThreads at default settings. The CPU-boundCPUDataset bs=8row is dominated by@spawnsetup cost on a tiny total workload (63 batches of small matmul), so single-trial variance is high.Benchmark script
Raw outputs (master, this PR, OhMyThreads default)
master(FLoops):This PR (manual
Threads.@spawn):Earlier experiment with
OhMyThreads.tforeachat default chunking, for reference:Test plan
julia --project -t auto -e 'using Pkg; Pkg.test()'— full suite passeseachobsparalleltestset green (9/9)numobs < nthreads(n = 0, 1, 3, 5with 6 threads) — all terminate and produce the expected items🤖 Generated with Claude Code