Skip to content

flow: Partition and Reduce, keyed partitions in flows (plan PR 1) - #176

Merged
winding-lines merged 2 commits into
mainfrom
flow-partition
Nov 29, 2026
Merged

winding-lines merged 2 commits into
mainfrom
flow-partition

Conversation

@winding-lines

Copy link
Copy Markdown
Contributor

PR 1 of DESIGN.md's "Keyed partitions in flows (plan, 2026-11-29)" (#175).

What was built

  • Partition[T: ManyLike, parts, budget=4]: one TaskMany per input, run on all of that input's batches in the read's order (rt.remote_many[T](refs, parts)). It makes parts pieces, and an input with no batches makes none. Its budget counts tasks in flight plus finished inputs waiting for one, and the read opens no new stream while it is full.

  • Reduce[T: WithLike, budget=16]: after the barrier, one TaskWith per partition j, on piece j of every input in input order. Each piece ref is released as soon as its reducer is submitted. Map/MapWith may follow it.

  • Executor: two chains joined by a barrier. The first chain ends at FlowRun.L, the operator before the Partition. The executor waits on each partition task's first piece only. At the barrier it checks every piece's error_of. next hands out one FlowResult per partition (input = partition index), and run returns results by partition. A failed flow releases every piece (_abandon).

  • Compile-time checks (Flow._check):

    • at most one Partition, and it is followed by a Reduce
    • a Reduce comes right after a Partition
    • the Partition's In is Seq[<prev Out>], checked by name
    • the Reduce's task takes an Int, and its pieces match the Partition's
    • parts >= 1

    At run time, start checks that a Partition's task is a TaskMany and a Reduce's is a TaskWith.

  • Core:

    • ManyLike.Part is now Movable & Deinitable.
    • New WithLike trait (exported from dista), exposing a TaskWith's element type X.
  • dista.flow.partition_of(key, parts): splitmix64 for an Int, FNV-1a 64 (fnv1a64) for a StringSpan, modulo parts. Unseeded and stable; the test pins fixed values.

  • README "Flows" bullet, CHANGELOG (Added and Changed), DESIGN.md: PR 1 marked done, with a "PR 1, as built" note.

Flows without a Partition have P = -1 and L = N - 1, and they run the same steps as before.

Deviations from the plan (also in DESIGN.md, "PR 1, as built")

  • TaskWith's element parameter is now X_, with comptime X = Self.X_. Mojo doesn't let a struct parameter stand for a trait's comptime member ("required member 'X' is not specified"). Positional uses are unchanged.
  • In this PR, a loss from the Partition on fails the flow instead of rerunning. That covers a partition task's loss, a piece's loss at the barrier, and a reducer's loss. Rerunning before the barrier, and the clear message after it, are PR 2. Losses before the Partition still rerun their input.
  • partition_of takes a StringSpan, the type fnv1a64 takes.
  • The Reduce's Int check is also asserted in Reduce.submit_with, which is where the compiler reports it.

The plan's unverified point

The head accepts a reducer with zero inputs. With no inputs, or with every input empty, all parts reducers run on an empty Seq and each partition is handed out once (test_inputs_with_no_batches_and_no_inputs).

Verified

On the Mac (osx-arm64):

  • tests/test_flow_partition.mojo: 7/7 pass.
    • group-by of 8 inputs × 20 batches × 500 rows, parts 4, with a Map before and after; counts and integer sums equal one process's Dict, every key exactly once, each partition handed out once
    • odd inputs with no batches, no inputs at all, and all-empty inputs
    • partitions no key reaches, each run once on empty pieces
    • a raising split, a split one piece short ("returned 3 results, not 4") and a raising reducer each fail the flow, and the stores are empty afterwards (rt.stats() objects, results, located and errors all 0)
    • two runs give bit-identical float sums (compared by to_bits, sums mixing 1e12 and 0.1)
    • partition_of's pinned values
  • tests/test_flow.mojo 15/15 and tests/test_shuffle.mojo 9/9 pass, unchanged.
  • tests/compile_fail.sh passes, with three new cases: reduce_without_partition, reduce_wrong_piece, reduce_not_int.
  • pixi run -e fmt format-check is clean.
  • pixi run -e embed bench-embed-stub builds and its check passes.
  • The full pixi run test passes (rc 0).

On Linux (sai, a fresh HTTPS clone of this branch): test_flow_partition 7/7 and test_flow 15/15 pass.

🤖 Generated with Claude Code

https://claude.ai/code/session_017TycsyUqdnuDeZrYc3z6wV

winding-lines and others added 2 commits November 29, 2026 05:26
Two operators that run a shuffle inside a flow. A Partition runs a TaskMany
once per input on all of that input's batches and makes `parts` pieces; once
every input's pieces exist (the barrier), Reduce runs a TaskWith per
partition on piece j of every input in input order, and Maps may follow.
Results come by partition. WithLike names a TaskWith's element type,
ManyLike.Part is Movable & Deinitable, and partition_of is a stable
splitmix64 / FNV-1a hash. Flows without a Partition run the same code.

Gate: tests/test_flow_partition.mojo and three compile-fail cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017TycsyUqdnuDeZrYc3z6wV
Renaming TaskWith's parameter would break callers passing X= by keyword.
The trait member is named Elem instead, set as `comptime Elem = Self.X`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017TycsyUqdnuDeZrYc3z6wV
@winding-lines
winding-lines merged commit dba7437 into main Nov 29, 2026
12 checks passed
@winding-lines
winding-lines deleted the flow-partition branch November 29, 2026 14:09
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.

1 participant