8000 fix(ingest): bounded window concurrency for neural _bulk embedding (#938 slice 1) by xerj-org · Pull Request #995 · xerj-org/xerj · GitHub
[go: up one dir, main page]

Skip to content

fix(ingest): bounded window concurrency for neural _bulk embedding (#938 slice 1) - #995

Merged
xerj-org merged 1 commit into
mainfrom
fix/issue-938-neural-ingest-threads
Sep 21, 2026
Merged

xerj-org merged 1 commit into
mainfrom
fix/issue-938-neural-ingest-threads

Conversation

@xerj-org
Copy link
Copy Markdown
Owner

Problem

With --embed-mode neural, one _bulk stream into a semantic_text field kept only ~3 of 32 hardware threads busy (#938). The _bulk embedding loop cut each request into scheduling windows of onnx_scheduling_window passages and then awaited them strictly one after another; the only concurrent scheduler in that function, collect_ordinal_buffered_two, is gated on dual_session_scheduler_enabled = onnx_pinned && pool_size == 2, which never applies to the neural backend. At any instant one request had exactly one BERT forward pass in flight, and the only parallelism was Candle's intra-op threading, which tops out near 3.5 cores.

Root cause

engine/crates/xerj-engine/src/index.rs, apply_semantic_embeddings_batch: window boundaries are computed up front (semantic_embedding_window_end) and each window's embed_batch call is awaited in a serial for loop. The neural embedder is &self and safe to call concurrently (NeuralEmbedder::embed_blocking behind Arc, one spawn_blocking per call), so the serialization is purely the caller-side loop.

Fix (slice 1 of the issue's plan — no dependency changes)

  • New collect_ordinal_buffered(count, bound, launch) beside the existing pair scheduler: a bounded-concurrency, order-preserving scheduler over std::future::poll_fn. At most bound futures in flight; finished futures retire strictly from the front; results return in ordinal order; a window that fails as a value neither cancels nor displaces its neighbours (same per-window failure contract as the pair scheduler, whose ONNX dual-session path is untouched).
  • The _bulk embedding loop routes every non-dual-ONNX backend through it with bound = embedding.neural_window_concurrency. Default 1 preserves the historical serial loop exactly.
  • New [embedding] key neural_window_concurrency (default 1, range 1..=64) with validation in xerj-common/src/config.rs, documented in xerj.default.toml; user-facing settings count 120 → 121 updated in lockstep everywhere the counting tests pin it (SETTINGS_BY_SECTION, module header, lib.rs, EXPECTED_SETTINGS).
  • Each window is still embedded as its own batch and reassembled by position, so per-passage vectors are identical to the serial loop — pinned by a unit test comparing window batches and vectors at bounds 1/2/8.
  • New repro harness benchmarks/neural-path-triage/window_concurrency_bench.py (synthetic corpus, no BEIR download; same reported columns as the existing ingest_bench.py: docs/s, server_cpu_s from /proc, avg cores busy).

Measurements (this sandbox, shared box loadavg 6–13 of 32 threads; release build; all-MiniLM-L6-v2 via candle on CPU — genuinely neural, not the lexical default; 300 synthetic docs, mean 1,470 chars ≈ 4 passages, one _bulk stream of 50; warmup pass excluded)

neural_window_concurrency docs/s server_cpu_s avg_cores_busy
1 (default, = main) 14.1 / 13.6 (re-run) 67.1 3.2 / 3.0
2 21.3 66.2 4.7
4 39.1 68.9 9.0
8 38.9 68.9 8.9
1, eight clients 54.0 71.3 12.8
4, eight clients 83.9 78.3 8000 21.9

2.8× from one stream at the same CPU-seconds (67.1 → 68.9 for 14.1 → 39.1 docs/s). One stream with the fix reaches ~72% of what eight client streams extract from the serial server. 8 == 4 because a 50-doc _bulk at this corpus shape holds ~4 windows of ≤64 passages, so 4 is the per-request ceiling — cross-request pool sharing is the issue's slice 3, as are Candle intra-op pinning (slice 2) and a non-serial default. These are my numbers on this box; the issue's 6.1 docs/s at 3.4 cores is its own measurement on different, more contended hardware and is context, not verification.

Test evidence

  • 3 new scheduler tests (full-window concurrency proven by an in-flight gate that cannot release before bound futures are simultaneously in flight; reversed completion drained in ordinal order with failure atomicity; output independence from the bound) and 2 new config tests (serial default round-trip, bounded range 0/65 rejected).
  • Fail-before proven by rebuilding with only the production code stripped (tests kept): E0432 unresolved import super::collect_ordinal_buffered, E0609 no field neural_window_concurrency on EmbeddingConfig; restored and green after.
  • Suites: xerj-common 114/0 (+3 doc), xerj-engine lib 686/0 (3 new), engine es_compat_tests 65/0 and integration 132/0 over the touched path, product_experience::journey_zero_config green at 121 settings.
  • ES-YAML conformance on a private port with a throwaway data dir: 1371 passed / 0 failed / 3 skipped (was 1371/0/3 on main).
  • cargo clippy -D warnings clean on both touched crates; cargo fmt applied.

Fixes #938

 slice 1)

Motivation: with --embed-mode neural, one _bulk stream into a semantic_text
field kept only ~3 of 32 threads busy. The _bulk embedding loop cut each
request into scheduling windows of onnx_scheduling_window passages and then
awaited them strictly in sequence (engine/crates/xerj-engine/src/index.rs,
the pre-change for-loop in apply_semantic_embeddings_batch); the only
concurrent scheduler, collect_ordinal_buffered_two, is gated on
dual_session_scheduler_enabled = onnx_pinned && pool_size == 2 and therefore
never applies to the neural backend. At any instant one request had exactly
one BERT forward pass in flight, and the only parallelism was Candle's
intra-op threading, which tops out near 3.5 cores (#938).

What changed (slice 1 of the issue's plan — window-level data parallelism,
no dependency changes; Candle intra-op pinning and pool sizing stay open):

- New collect_ordinal_buffered(count, bound, launch) in index.rs next to
  collect_ordinal_buffered_two: a bounded-concurrency, order-preserving
  scheduler over std::future::poll_fn. At most bound futures are in flight,
  finished futures are retired strictly from the front, results return in
  ordinal order, and a window that fails as a value neither cancels nor
  displaces its neighbours (same contract as the pair scheduler).
- The _bulk embedding loop now routes every non-dual-ONNX backend through
  it with bound = embedding.neural_window_concurrency. Default 1 preserves
  the historical serial loop exactly; the ONNX pool-size-2 path is
  untouched.
- New [embedding] key neural_window_concurrency (default 1, range 1..=64)
  in xerj-common config.rs with validation, plus xerj.default.toml
  documentation; user-facing settings count 120 -> 121 (section table,
  module header, lib.rs, default toml, EXPECTED_SETTINGS all updated in
  step, as the counting tests require).
- Each window is still embedded as its own batch and reassembled by
  position, so per-passage vectors are identical to the serial loop; a
  unit test pins that (same window batches, same vectors at bounds
  1/2/8).

Measured in THIS sandbox (shared box, loadavg 6-13 of 32 threads; release
build, all-MiniLM-L6-v2 via candle on CPU; synthetic corpus of 300 docs,
mean 1,470 chars ~= 4 passages each, one _bulk stream of 50 docs; harness
benchmarks/neural-path-triage/window_concurrency_bench.py, warmup pass
excluded, wall + server utime+stime from /proc):

  neural_window_concurrency   docs/s   server_cpu_s   avg_cores_busy
  1 (default, = main)         14.1/13.6   67.1        3.2/3.0
  2                           21.3        66.2        4.7
  4                           39.1        68.9        9.0
  8                           38.9        68.9        8.9
  1, eight clients            54.0        71.3        12.8
  4, eight clients            83.9        78.3        21.9

2.8x from one stream at the same CPU-seconds; one stream with the fix now
reaches ~72% of what eight client streams extract from the serial server.
8 == 4 because a 50-doc _bulk at this shape holds ~4 windows of <=64
passages, so 4 is the per-request ceiling (cross-request pool sharing is
the issue's slice 3). These are my numbers on this box; the issue's 6.1
docs/s at 3.4 cores is its own measurement on different hardware and is
not verification of this change.

Tests: 3 new scheduler tests (full-window concurrency proven via an
in-flight gate, reversed completion + failure atomicity, bound-independent
output) and 2 new config tests (serial default, bounded range) — fail-before
proven by rebuilding with the production code stripped: E0432 unresolved
import collect_ordinal_buffered, E0609 no field neural_window_concurrency.
xerj-common 114 passed/0 failed, xerj-engine lib 686/0, engine es_compat
65/0 and integration 132/0 over the touched path, product_experience
journey_zero_config green at 121 settings. ES-YAML conformance on a private
port with a throwaway data dir: 1371 passed / 0 failed / 3 skipped (was
1371/0/3 on main). cargo clippy -D warnings clean, cargo fmt applied.
@cla-bot cla-bot Bot added the cla-signed label Sep 20, 2026
@xerj-org
xerj-org merged commit 4985f93 into main Sep 21, 2026
21 of 22 checks passed
Amakurai pushed a commit to Amakurai/xerj that referenced this pull request Sep 27, 2026
…, rc.78 queue

The 2026-09-21 review of this file was stale within hours: xerj-org#950 closed
19:33, xerj-org#941 19:38, xerj-org#1015 20:49 (all 2026-09-21), xerj-org#874 2026-09-22, and the
stage-1 gating trio xerj-org#937/xerj-org#938/xerj-org#939 closed the same evening — every one
recorded here as open or "under way". This pass re-checks every status
claim against live tracker state and rolls the file forward:

- Next release: the two "in flight" items (xerj-org#1015, xerj-org#950) are closed with
  fixes on main riding to rc.78 — the section now records what actually
  landed since the rc.77 tag (xerj-org#1009, xerj-org#1017, xerj-org#1018, xerj-org#1020, xerj-org#1021, xerj-org#1023,
  xerj-org#1025, xerj-org#1026, xerj-org#1033, xerj-org#1034) with PR links, including xerj-org#874's budget met
  at ~3x margin (206 -> 64 kB per idle index) and discussion xerj-org#1012's
  email-labelling measurement (1.000 templated tier / 0.625 at 0.902
  confidence on the hard tier — wrong-and-confident).
- Open defects: now xerj-org#1031 and xerj-org#1032 only; xerj-org#1015/xerj-org#950 removed (closed);
  trackers sentence corrected (xerj-org#941, xerj-org#874 closed — only xerj-org#298 remains by
  design).
- GA gate: "Close the CHANGELOG gap" marked closed 2026-09-26 (the
  rc.19-rc.70 backfill, PR xerj-org#1035); the record-complete note replaces the
  gap warning under "Shipping today".
- Zero-token: stage-1 gating trio recorded as fixed (xerj-org#991, xerj-org#995, xerj-org#979)
  with the old measured costs kept as the before-state; the xerj-org#940
  hash-seed spread annotated as a pre-fix measurement; stage-2 object
  storage marked done (xerj-org#965 wired in rc.77 — the old bullet contradicted
  this file's own "Shipping today" section); mail ingest "unreleased"
  corrected to "shipped in rc.75"; three stale "In flight:" labels on
  landed stage-1 items relabelled "History:".
- Mail-ingest memory line: xerj-org#948's runaway is fixed (xerj-org#1002) — the line
  now carries the fixed numbers and points at xerj-org#1032 for the residual.

Review line updated to 2026-09-26 (machine-checked format), scoped
honestly as a desk review. The milestones line is true again: rc.78
milestone created, all four open issues triaged (xerj-org#1030/xerj-org#1031/xerj-org#1032 ->
rc.78, xerj-org#298 -> GA).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

1 participant

0