Skip to content

helios: overlap startup allocation, registration, and setup with runtime-sized zero-copy weight loading - #726

Open
original-el8 wants to merge 1 commit into
mainfrom
perf/helios-overlapped-startup
Open

original-el8 wants to merge 1 commit into
mainfrom
perf/helios-overlapped-startup

Conversation

@original-el8

Copy link
Copy Markdown
Collaborator

Why

Model startup previously ran memory allocation, weight loading, kernel and graph preparation, KV cache allocation, host prefix cache initialization, and tokenizer loading strictly in series. On large models—especially host-tiered cores with hundreds of gigabytes of MappedHost storage—the NVMe storage array sat idle during single-threaded host pre-faulting and cuMemHostRegister_v2, while the GPU sat idle during MappedHost weight reads and post-load setup. In addition, the payload loader used fixed SLOTS = 4, READERS = 4, CHUNK = 4 MiB staging (preventing double-buffering between pread and cuMemcpyHtoDAsync) and copied all MappedHost payload bytes through pinned CUDA staging buffers instead of reading 4 KiB-aligned ranges directly into mapped host memory.

Overlapping host VMA preparation with Device weight uploads (holding zero CUDA context locks), pipelining 512 MiB-aligned cuMemHostRegister_v2 chunks ahead of zero-copy MappedHost O_DIRECT reads, running kernel/graph/cache/prefix-cache setup while NVMe streams MappedHost weights, and pre-loading the frontend tokenizer on a background CPU thread removes serial startup overhead across every GPU architecture without changing package formats, execution ABIs, or memory accounting.

What Changed

  • helios/src/runtime_core/payload.rs:
    • Replaced fixed staging constants with a runtime IoPlan (choose_io_plan) derived from CPU count, rank count, Linux block-device optimal_io_size (with non-Linux fallback), and available host memory, double-buffering SLOTS = 2 * READERS within a bounded cross-rank staging cap (<= 128 MiB).
    • Added zero-copy O_DIRECT reads directly into 4 KiB-aligned MappedHost destinations (splitting matching-alignment spans into staged edges and direct interior chunks), separated CUDA-free host loading (load_mapped_host_chunks) from Device loading (load_device), and added RegistrationGate to coordinate chunked host pinning with host readers.
  • helios/src/runtime_core/mod.rs and helios/src/runtime_core/format.rs:
    • Split allocate() into device allocation (allocate_device) and coalesced, THP-hinted MappedHost VMA preparation with ordered multi-threaded 2 MiB pre-faulting (prepare_mapped_host_vmas, holding no CUDA context locks) so host VMA setup overlaps with Device weight uploads.
    • Pipelined cuMemHostRegister_v2 in 512 MiB-aligned chunks (register_pending_mapped_host), tracking Unix host_mmap_spans and host_registrations separately from host_vmm host_mappings for clean reverse-order teardown.
  • helios/src/runtime_core/model.rs, helios/src/startup.rs, helios/src/server.rs, helios/src/server/config.rs, and helios/src/server/session.rs:
    • Overlapped MappedHost VMA preparation with Device weight loading, and overlapped chunked cuMemHostRegister_v2 followed by kernel, graph, embedding-table, KV cache, and host prefix cache setup with MappedHost weight loading (preserving exact post-registration, post-graph memory snapshots in allocate_caches() and replaying completed stage rows in canonical order via StartupProgress::replay_completed).
    • Pre-loaded the frontend tokenizer and chat template on a background CPU thread keyed by immutable frontend configuration fields for zero-cost reuse in LoadedSession::new.

Claimed Benefit and Result

Measured cold startup on 1× NVIDIA GB300 (SM103) loading a 552.1 GiB signed Amazon-Ergo core (240.5 GiB device + 316.6 GiB mapped host + 64 GiB Selene host prefix cache) from a 2× WD_BLACK SN850X PCIe Gen4×4 RAID0 (12.78 GiB/s measured raw O_DIRECT ceiling, 43.2 s physical I/O floor):

Phase Before After (critical path)
Verify core + Verify stored data + Prepare model + Initialize GPU 2.5 s 0.6 s
Allocate model memory 21.2 s 0.0 s
Load weights (552.1 GiB) 43.9 s (12.58 GiB/s) 43.8 s (12.90–13.00 GiB/s sustained)
Prepare kernels + Prepare execution + Allocate cache + Prepare host prefix cache 5.4 s 0.0 s (overlapped)
Load tokenizer 0.8 s 0.0 s (pre-loaded)
Start server 1.2 s 1.1 s
Total wall-clock ready 75.0 s 45.5 s (-29.5 s / -39.3%)
  • AnonHugePages: 94.0 GiB (up from 91.0 GiB); GPU/KV/prefix-cache allocations unchanged (241.16 GiB loaded, 7.81 GiB / 147,456 live GPU KV tokens, 64.0 GiB Selene).
  • Pure-GPU models retain exact post-load memory accounting while gaining runtime-sized double-buffered staging and background tokenizer pre-loading.

Supporting Evidence and Limitations

  • cargo test --locked --release --manifest-path helios/Cargo.toml --features runtime-core,server --lib (with generated frontend templates): 1,919 passed, 0 failed, 49 ignored, including 6 new unit tests covering IoPlan, zero-copy aligned/unaligned MappedHost reads, RegistrationGate, 512 MiB registration boundaries, and ordered 2 MiB pre-fault slices.
  • cargo test --locked --release --manifest-path flare/Cargo.toml: 255 passed, 0 failed, 4 ignored.
  • cargo test --locked --release --manifest-path flare-cli/Cargo.toml: 250 passed, 0 failed, 3 ignored.
  • cargo build --release --manifest-path flare-cli/Cargo.toml: clean release build on main (f705086b).
  • Live cold startup (75.0 s -> 45.5 s) and streaming serving verification were measured via flare run on 1× NVIDIA GB300 (SM103) on the equivalent feat/gb300-tiered-runtime build prior to rebasing cleanly onto main.
  • Material limitations: No package format, ABI, or PreparedContract changes. host_vmm (Windows and non-identity-mapped Linux GPUs), integrated-GPU mlock, async_transfers, and multi-rank TP startup overlap paths were not executed on hardware in this PR.

Discord: el8

…ime-sized zero-copy weight loading

Model startup previously executed memory allocation, weight loading, kernel and
graph preparation, cache allocation, host prefix cache setup, and tokenizer
loading strictly in series. On large models—especially host-tiered cores that
allocate and register hundreds of gigabytes of mapped host memory—the NVMe
storage array sat idle during single-threaded host pre-faulting and
`cuMemHostRegister_v2`, while the GPU sat idle during host weight reads and
subsequent CPU/CUDA setup. In addition, the payload loader used fixed
`SLOTS = 4, READERS = 4, CHUNK = 4 MiB` staging (preventing full
double-buffering between `pread` and `cuMemcpyHtoDAsync`) and copied every
`MappedHost` weight byte through pinned CUDA staging buffers instead of reading
4 KiB-aligned ranges directly into mapped host memory.

What changed:
- `helios/src/runtime_core/payload.rs`: replaced fixed staging constants with a
  runtime `IoPlan` (`choose_io_plan`) derived from CPU count, rank count, Linux
  block-device `optimal_io_size` (with non-Linux fallback), and available host
  memory, double-buffering `SLOTS = 2 * READERS` within a bounded staging cap
  (`<= 128 MiB` across ranks). Added zero-copy `O_DIRECT` reads directly into
  4 KiB-aligned `MappedHost` ranges (splitting matching-alignment spans into
  staged edges and direct interior chunks), separated CUDA-free host payload
  loading (`load_mapped_host_chunks`) from device payload loading
  (`load_device`), and added `RegistrationGate` to coordinate chunked host
  pinning with host `O_DIRECT` readers.
- `helios/src/runtime_core/mod.rs` and `helios/src/runtime_core/format.rs`:
  split `allocate()` into device allocation (`allocate_device`) and
  coalesced/THP-hinted mapped-host VMA preparation with ordered multi-threaded
  2 MiB pre-faulting (`prepare_mapped_host_vmas`, holding no CUDA context
  locks) so host VMA setup overlaps with device weight uploads. Pipelined
  `cuMemHostRegister_v2` in `512 MiB`-aligned chunks via
  `register_pending_mapped_host` (tracked in `host_mmap_spans` and
  `host_registrations` separately from `host_vmm` `host_mappings`) so host
  pinning never contends with `cuMemcpyHtoDAsync` while preserving hugepage
  alignment boundaries and reverse-order teardown.
- `helios/src/runtime_core/model.rs`, `helios/src/startup.rs`,
  `helios/src/server.rs`, `helios/src/server/config.rs`, and
  `helios/src/server/session.rs`: overlapped `MappedHost` VMA preparation with
  device weight loading, overlapped chunked `cuMemHostRegister_v2` followed by
  kernel, graph, embedding-table, KV cache, and host prefix cache setup with
  `MappedHost` weight loading (replaying completed phase records cleanly in
  canonical order via `StartupProgress::replay_completed`), and pre-loaded the
  frontend tokenizer/template on a background CPU thread keyed by immutable
  frontend config fields for reuse in `LoadedSession::new`.

Claimed benefit and result:
Measured cold startup for a 552.1 GiB host-tiered signed core (`240.5 GiB`
device + `316.6 GiB` mapped host + `64 GiB` host prefix cache) on a 2x PCIe
Gen4x4 NVMe RAID0 (`12.78 GiB/s` measured raw `O_DIRECT` ceiling, `43.2 s`
physical I/O floor) dropped from `75.0 s` to `45.5 s` (`-29.5 s` / `-39.3%`),
sustaining `12.90–13.00 GiB/s` during weight loading with `94.0 GiB`
`AnonHugePages` (up from `91.0 GiB`), identical GPU/KV/prefix-cache budgets,
and `0.0 s` critical-path cost for allocation, kernel/graph/cache/prefix-cache
preparation, and tokenizer loading. Pure-GPU models retain exact serial
post-load GPU memory accounting while gaining runtime-sized double-buffered
staging and background tokenizer pre-loading.

Supporting evidence and material limitations:
Validated with `cargo test --locked --release --manifest-path helios/Cargo.toml
--features runtime-core,server --lib` (1919 passed, 0 failed, 49 ignored),
`cargo test --locked --release --manifest-path flare/Cargo.toml` (255 passed,
0 failed, 4 ignored), `cargo test --locked --release --manifest-path
flare-cli/Cargo.toml` (250 passed, 0 failed, 3 ignored), targeted `runtime-core`
tests covering `IoPlan`, zero-copy aligned/unaligned `MappedHost` reads,
`RegistrationGate`, `512 MiB` registration chunks, and ordered `2 MiB` pre-fault
slices (6 passed, 0 failed), and `cargo build --release --manifest-path
flare-cli/Cargo.toml`. Live cold startup (`75.0 s -> 45.5 s`) and streaming
serving output were measured via `flare run` on 1x NVIDIA GB300 (`SM103`) on
the equivalent `feat/gb300-tiered-runtime` build prior to rebasing cleanly onto
`main`. Material limitations: `host_vmm` (Windows and non-identity-mapped
Linux GPUs), integrated-GPU `mlock`, `async_transfers`, and multi-rank TP
startup overlap paths were not executed on hardware in this change.

Design-Signed-Off-By: Jason Cook; design-review=2e5b
@flare-autobot

Copy link
Copy Markdown
Collaborator

A case file has been opened for 726.
Case File Intake Report

Future reviews will auto-run 90 minutes after code updates are pushed.

  • Re-review sooner: Call @flare-autobot after push.
  • Opt-out: Include #autobot:ignore in the PR or any comment.

@flare-autobot flare-autobot added performance Performance, latency, throughput, or memory-efficiency investigation or improvement. triage:review Proposed change needs code review; this does not imply merge readiness. labels Dec 4, 2026
@flare-autobot

Copy link
Copy Markdown
Collaborator

Autobot Review

Full Review · Value Proposition · Case File

Recommendation: Changes Requested

Right problem in the right shared runtime layer (helios/src/runtime_core), and the core zero-copy MappedHost / chunked-registration / teardown mechanics hold up under boundary review. But it is not ready to merge: the headline 75.0 s → 45.5 s speedup was measured pre-rebase on the combined feat/gb300-tiered-runtime stack (#710) instead of head 60d1b6b against main, and independent verification confirmed four real issues across error-path gate closing, partial-load success reporting, unconditional MADV_HUGEPAGE pre-faulting, and stale design docs / a multi-rank staging-cap overshoot.

  • Blockers: 1 · Should fix: 4 · Subtract/Simplify: 6 · Nits: 3
Walkthrough
  • helios/src/runtime_core/payload.rs (+689/−80): Replaces fixed 4 MiB / 4 readers / 4 slots staging constants with runtime IoPlan (choose_io_plan, detect_optimal_io_size, available_host_memory_bytes), splits Device loading (load_device) from CUDA-free MappedHost loading (collect_mapped_host_chunks, load_mapped_host_chunks), adds push_span_chunks / read_direct_into_host for 4 KiB-aligned zero-copy O_DIRECT reads with AlignedBuffer bounce fallback for unaligned edges, and introduces RegistrationGate. Adds 3 unit tests.
  • helios/src/runtime_core/mod.rs (+618/−86) & format.rs (+1/−1): Derives PartialEq, Eq on Memory (format.rs:51); splits allocate() into allocate_device, allocate_mapped_host_vmm, prepare_mapped_host_vmas (coalesced MAP_FIXED_NOREPLACE VMAs, Linux MADV_HUGEPAGE, ordered 2 MiB-strided multi-threaded pre-faulting without CUDA context locks), load_device_overlapping_mapped_host, and register_pending_mapped_host (512 MiB chunked cuMemHostRegister_v2 signaling RegistrationGate, followed by cuMemHostGetDevicePointer_v2 identity checks); tracks Unix host_registrations and host_mmap_spans separately for reverse-order teardown (release_execution_resources). Adds 2 unit tests (startup_overlap_tests).
  • helios/src/runtime_core/model.rs (+402/−152): Overlaps prepare_mapped_host_vmas with load_device_payload when host_bytes > 0 && device_payload_bytes > 0, and overlaps chunked register_pending_mapped_host + prepare_runtime (kernels, graphs, embedding tables, allocate_caches, prepare_offload) with load_mapped_host_chunks when host_payload_bytes > 0, running apply_payload_relocations and prepare_async_transfers after both join; spawns a background flare-frontend-preload thread on rank 0.
  • helios/src/startup.rs (+31/−8): Removes #[cfg(test)] from StartupObservation.completed / completed_phases() and adds StartupProgress::replay_completed to re-emit overlapped background stages with " · overlapped" detail while preserving original elapsed durations.
  • helios/src/server/{config.rs,session.rs} & helios/src/server.rs (+145/−58): Extracts load_frontend_inner, adds PreloadedFrontend / preload_frontend / frontend_cache_key / Bootstrap::load_with_preloaded, and consumes owner.take_preloaded_frontend() in LoadedSession::new.
Merge Risk: High · up to 60d1b6b
Pre-merge checks: ✅ 1 passed · ⚠️ 3 warnings

Have your agent review the Full Review for complete verified findings, file:line citations, and suggested changes.

To opt out, include #autobot:ignore in your post or comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance, latency, throughput, or memory-efficiency investigation or improvement. triage:review Proposed change needs code review; this does not imply merge readiness.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants