Replace FLoops with manual Threads.@spawn - #215
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 Harness. 🚀 New features to boost your workflow:
|
|
@CarloLucibello I saw you added a thumbs up above - would you be able to review the PR? |
| @@ -0,0 +1,46 @@ | |||
| module TransducersExt | |||
There was a problem hiding this comment.
change name to MLUtilsTransducersExt
There was a problem hiding this comment.
I don't mind, I can do that. Note, however, that the officially documented and suggested approach is to not include the main package's name in the name of the extension: https://pkgdocs.julialang.org/v1/creating-packages/#Code-structure
There was a problem hiding this comment.
my impression was that most packages are following MLUtilsTransducersExt instead, to avoid having many extensions with the same name (which maybe is irrelevant?).
There was a problem hiding this comment.
Same names are not a problem AFAIK and many packages have switched to the shorter names. Initially, I think one motivation for the more verbose names was that it would simplify debugging of precompilation but that concern was resolved by Pkg always printing the main package as well.
|
sorry for the delay. this looks fine except for the extension name. |
|
On a second thought, we could get rid of the Transducers implementation entirely |
|
I'll finish up this work on a follow up PR, since I also want to run some tests. |
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