Skip to content

refactor(umbp): one incremental plan builder shared by every transfer engine - #774

Merged
TianDi101 merged 6 commits into
mainfrom
perf/umbp-plan-builder-common
Oct 15, 2026
Merged

TianDi101 merged 6 commits into
mainfrom
perf/umbp-plan-builder-common

Conversation

@TianDi101

@TianDi101 TianDi101 commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #773. Every transfer engine now plans incrementally, and that's the interface's contract rather than a convention. The planning code shrinks (net −121 source lines) while MORI-IO and GDS gain incremental builders.

Stacked on #765. The first commit (320ddabf9) is #765's change, cherry-picked onto main; it drops out once #765 lands. This PR's own commits are 486694eaf through 245d36041. The later ones rework the first, so review the combined diff.

Why

After #758 and #765, HbmCopyEngine and LocalCopyEngine had incremental builders, each a hand-written copy of the same skeleton. MoriIoEngine and GdsEngine planned only in batch, behind BufferedPlanBuilder, which copies every item into a vector first.

The interface was also the wrong way round. Plan() was the pure virtual, and CreatePlanBuilder() an optional override defaulting to the buffered builder. A new engine that implemented only Plan() would quietly get the slow path.

Change

One planning hook. TransferEngine::CreatePlanBuilder() is pure virtual. Plan() is a non-virtual base method: the builder run over the vector. Removed:

  • the five identical Plan() overrides;
  • BufferedPlanBuilder;
  • the PlanViaBuilder helper.

A new engine can't compile without a builder, so it can't silently plan in batch.

The rules every builder keeps are written on TransferPlanBuilder:

  • Add() does no IO and changes no engine state;
  • an empty item is dropped;
  • an item the engine can't carry (its CanHandle() refuses the pair, or a range is out of bounds) goes to rejected_tags;
  • every other item is named by one plan;
  • plans come out in a deterministic order.

Shared pieces (transfer/plan_builder.h):

  • PairPlanBuilder, a whole builder for engines with one plan per (src, dst) buffer pair. It does the admission check (CanHandle() + bounds), the pair map with a fast path for consecutive items of the same pair, and new plans. The engine passes only how to append a segment:
    • Local: AppendSegment.
    • Hbm: an append that splits at host registration pieces.
  • Helpers for engines that write their own builder:
    • AppendSegment: merge adjacent segments, optional length cap, de-duplicated tags;
    • ItemInBounds / RangeFits;
    • PlanBetween.

Engines:

Engine Before After
LocalCopyEngine hand-written builder (#765) MakePairPlanBuilder(*this, AppendSegment): 6 lines
HbmCopyEngine hand-written builder (#758) MakePairPlanBuilder + its piece-split append
MoriIoEngine batch Plan() only incremental: its original grouping loop, run one item at a time
GdsEngine batch Plan() only incremental: one plan per item
CompositeTransferEngine separate partition-then-plan Plan() only its routing builder; Plan() comes from the base

Source lines (excluding tests and docs), against 320ddabf9: +324 / −445.

The umbp-add-transfer-engine skill now covers the hook, the rules, when to use PairPlanBuilder versus writing a builder, and the contract test helper. MORI-IO's planner is noted as unit-testable.

Behaviour change: composite plan order. For a mixed batch, the composite's Plan() now groups plans by engine, with engines in first-seen order. It was unordered_map order. Each engine's own plans are unchanged.

Verification

  • Plans are unchanged. A scratch harness (not in this PR) dumps every engine's Plan() for seeded random batches. Built against 320ddabf9 and against this PR, the dumps are byte-identical: 42 plan sets, 8,450 lines. Composite mixed batches are compared sorted, because of the ordering change above.

    Set (6 seeds) Plans Segments Tags Rejected Bounce plans
    local 18 4,353 10,352 4,562 0
    hbm (2 MiB host pieces) 18 5,229 10,191 4,652 0
    mori (zero-copy + staged, two peers sharing ids) 1,193 4,935 9,870 5,992 1,175
    mori, adjacent 3 GiB segments (SGE cap) 6 12 12 0 0
    gds 7,149 7,149 7,149 3,490 0
    composite, mixed / uniform 12 / 12 4,344 / 5,785 8,918 / 12,625 6,539 / 750 0
  • Unit tests: test_transfer_engine 21/21 and test_gds_engine 3/3 (built with the hipfile headers, including the GPU read).

    • New: MoriIoEngine.GroupsZeroCopyByEndpointsAndCapsSegments and MoriIoEngine.StagesUnregisteredMemoryThroughTheBouncePool, the first tests of MORI-IO's planner. It needs no fabric, so the engine is never Init()ed.
    • New: HbmCopyEngine.BuilderSplitsAtHostRegistrationPieces.
    • New: LocalCopyEngine.MergesWithinEachPairWhenPairsInterleave, which exercises the repeat-pair fast path.
    • ExpectPlanContract checks the builder rules on every engine's planning test: every item rejected or planned, accepted bytes planned exactly once.
    • The three test fakes now implement CreatePlanBuilder().
    • Removed: LocalCopyEngine.BuilderMergesAdjacentRanges and BuilderRejectsOutOfBoundsRanges, which repeat the Plan() tests now that Plan() is the builder. Their one extra case (an empty item is dropped) moved into the interleave test.
  • Full UMBP suite: 54/54. umbp_cross_node_smoke and umbp_pool_client_ranges abort under ctest -j 8 (RDMA device contention, as on main) and pass serially.

  • Planning cost: A/B of 320ddabf9 against this PR (cc78f01ca), run with perf(umbp): build LocalCopyEngine transfer plans incrementally #765's microbenchmark. Kimi-K3 TP8 FP8, 999,936 tokens; median of 36 runs pinned to one core. Plan checksums are identical in every cell.

    Incremental path (what BatchGetRanges uses), old → new:

    Destination Page Descs Plan step Build + plan
    GPU (Hbm) 64 KiB none 3.5 → 2.9 ms (−17%) 11.5 → 10.8 ms (−6%)
    host (Local) 64 KiB none 2.8 → 2.2 ms (−21%) 10.8 → 10.3 ms (−5%)
    host (Local) 64 KiB mori-io 2.8 → 2.1 ms (−22%) 19.6 → 19.2 ms (−2%)
    host (Local) 32 KiB none 4.8 → 3.8 ms (−21%) 19.8 → 19.1 ms (−4%)
    host (Local) 32 KiB mori-io 4.8 → 3.8 ms (−21%) 36.0 → 34.8 ms (−3%)
    all SSD (GDS) 64 KiB none 8.5 → 6.8 ms (−20%) 9.2 → 7.5 ms (−18%)
    half SSD, half DRAM 64 KiB none 3.4 → 2.8 ms (−18%) 7.5 → 6.9 ms (−8%)
    half SSD, half DRAM 32 KiB none 4.7 → 3.9 ms (−17%) 12.0 → 11.1 ms (−8%)

    Plan() over a vector: a mixed batch no longer copies its items into per-engine vectors, so it is about 2× faster end to end (70.1 → 37.5 ms at 64 KiB, 148.4 → 75.8 ms at 32 KiB). A uniform batch's plan step drops 6.7 → 3.7 ms (GPU) and 5.8 → 3.4 ms (host).

Merging with #763

#763 adds a FailingEngine test fake that overrides Plan(). Whichever of the two lands second switches it to CreatePlanBuilder(), a one-line change, as in merge 07c26da29 on test/umbp-e2e-v4.

Not in this PR

🤖 Generated with Claude Code

TianDi101 and others added 5 commits October 15, 2026 06:01
LocalCopyEngine plans a ranged get into host memory the way HbmCopyEngine
plans one into a GPU: one TransferItem per page fragment, grouped by
(src, dst) buffer and coalesced. Behind the default BufferedPlanBuilder it
still held every fragment of the batch, and the composite builder copied
each one into it once more, so host-destination restores got slower with
the incremental builder instead of faster.

Give it the builder HbmCopyEngine has: Plan()'s loop moves into Add(), and
Plan() is the builder over a vector, so there is one copy of the merge
logic. The pair map grows with distinct endpoint pairs instead of reserving
two buckets per item.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 8f65997)
… engine

#758 and #765 gave HbmCopyEngine and LocalCopyEngine incremental plan
builders, each a hand-written copy of the same skeleton; MoriIoEngine and
GdsEngine still planned only in batch, behind the buffering default.

- plan_builder.h holds what was duplicated: AdjacentNoOverflow (3 copies),
  PtrPairKey (2), the coalesce-and-dedupe-tag append (3, now AppendSegment
  with an optional merge veto and length cap), the bounds check, and
  PlanViaBuilder() for "Plan() is the builder over a vector".
- GroupedPlanBuilder<Traits> owns the skeleton: skip empty items, collect
  rejected tags, group into plans in first-seen order, Finish.  An engine
  supplies Admit / Joins / NewPlan / Append in a small Traits struct.
- Hbm and Local are ported onto it; MoriIoEngine (zero-copy groups keyed by
  memory ids and told apart by engine key, bounce groups retired when the
  pool fills) and GdsEngine (one plan per item) gain incremental builders.
- CompositeTransferEngine::Plan() is now its builder over a vector, so there
  is one routing path; mixed batches list engines in first-seen order
  instead of unordered_map order.

Plans are unchanged: a scratch harness dumping every engine's Plan() for 42
seeded random batches (merges, piece splits, bounce rollovers, the SGE cap,
every reject path) is byte-identical before and after.

Tests: MoriIoEngine's planner, untested until now (zero-copy grouping across
peers with colliding ids, the uint32 SGE cap, bounce staging and rollover),
Hbm's split at host registration pieces, and a builder-matches-Plan check for
every engine and the composite.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…at lookups

The first cut gave every group a vector of plan indices and a Joins loop,
which Local and Hbm never need: one buffer pair is one plan.  Their
incremental planning was 0.3-0.9 ms (8-18%) slower than the hand-written
builders it replaced.

kGroups becomes PlanGrouping: kNone (GDS), kOnePerKey (Local, Hbm: a plain
key -> plan index map, and an item with the previous item's key skips the
lookup, since a key's pages arrive back to back) and kManyPerKey (MORI-IO,
which keeps Joins).  Plans are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ir builder

Every engine's Plan() had become "my builder over a vector", so make that
the contract instead of a convention:

- TransferEngine::CreatePlanBuilder() is pure virtual and Plan() is a
  non-virtual base method (the builder over the vector).  Gone: the five
  identical Plan() overrides, BufferedPlanBuilder (the default that buffered
  every item) and PlanViaBuilder.  A new engine cannot compile without a
  builder, so it cannot silently plan in batch.
- The rules every builder keeps (no IO, empty items dropped, rejects into
  rejected_tags, each item named by one plan, deterministic order) are
  written on TransferPlanBuilder.
- The traits template was more machinery than its four users needed: two
  of its three grouping modes served one engine each.  It is now
  PairPlanBuilder, for the engines with one plan per buffer pair (Local,
  Hbm): admission (CanHandle + bounds), the pair map with its repeat-pair
  fast path, and new plans are shared, and an engine passes only how to
  append.  GdsEngine and MoriIoEngine get plain builders (MORI-IO's is its
  original Plan() loop, item by item) on the shared helpers.

Tests: builder-matches-Plan() checks are tautological now and are replaced by
ExpectPlanContract (every item rejected or planned, accepted bytes planned
exactly once), used on every engine; the three test fakes implement
CreatePlanBuilder().  The add-engine skill describes the hook, the rules and
when to use PairPlanBuilder.  Plans are unchanged (the scratch harness's 42
plan sets are byte-identical to 320ddab).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comments cut to one line of why.  LocalCopyEngine's two Builder* tests
repeat the Plan() tests now that Plan() is the builder; their one extra case
(an empty item is dropped) moves into the interleave test.  Trimmed asserts
that the plan count or ExpectPlanContract already cover.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@TianDi101
TianDi101 marked this pull request as ready for review October 15, 2026 07:05
@TianDi101
TianDi101 merged commit cf77a37 into main Oct 15, 2026
TianDi101 added a commit that referenced this pull request Oct 16, 2026
A local reader now protects what it resolved by keeping a shared_ptr hold
(ResolvedEntry::hold). The bytes stay put while any hold lives and come back
when the last one drops. This replaces the lease tokens, generations,
ReleaseResolved and kDeferred that the first version of this PR added.

- PageBackend: each committed slot owns a hold. Evict, EvictionCandidates and
  ClearLocal skip a slot whose hold is shared. A kLocal resolve takes no lease.
- SsdBackend: a staging span is reclaimed once its lease has expired and its
  last hold is gone. Reclaim runs in the reaper and on demand when staging is
  full. A kLocal batch is staged up to half the arena at a time; the rest
  comes back kBusy, and the caller serves it in further parts. A kRemote batch
  keeps all-or-nothing staging.
- PeerPool: a partial kLocal batch still touches and promotes what it served.
- PoolClient: ServeLocalGets and BatchGetRanges drop each part's holds before
  resolving the next part. The probes use BatchContains, so they stage nothing.

Also fixes test_peer_pool's FailingEngine to override CreatePlanBuilder
(#774's interface). Before this, test_peer_pool did not build on main.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
TianDi101 added a commit that referenced this pull request Oct 16, 2026
#774 made Plan() a non-virtual wrapper over CreatePlanBuilder().  PosixEngine
keys plans by (memory, file range, direction) because a file endpoint has no
host_ptr, so it gets its own builder rather than PairPlanBuilder.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
TianDi101 added a commit that referenced this pull request Oct 16, 2026
…790)

fix(umbp): port test_peer_pool's FailingEngine to CreatePlanBuilder (#790)

#763 added FailingEngine overriding Plan(), which #774 had made
non-virtual, so test_peer_pool failed to build. Delegate the new
pure-virtual CreatePlanBuilder() to the wrapped LocalCopyEngine,
as GatedEngine does.
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