Avoids fetching the same position many times in a batch - #2417
Conversation
There was a problem hiding this comment.
Pull request overview
Adds intra-batch deduplication of NN evaluation requests: when multiple positions in the same batch (or across overlapping batches) hash to the same cache key, only one is sent to the underlying backend; other queriers wait on a per-entry state machine for the result. Cache slots are reserved up-front via HashKeyedCache::Insert, and a new FETCHED_DELAYED AddInput result is plumbed through both classic and dag_classic search workers so the wait is accounted as out-of-order rather than a full network evaluation.
Changes:
- Introduce a
WaitableAtomic<T>shim (src/utils/atomic.h) that falls back to aMutex+std::condition_variablewhen__cpp_lib_atomic_waitis unavailable. - Rework
MemCacheComputationto insert a placeholderCachedValuewith a 4-state machine (NOT_QUEUED/NO_WAITERS/WAITERS/READY) and over-allocateentries_tobatch * (1 + max-out-of-order-evals-factor). - Add
FETCHED_DELAYEDtoBackendComputation::AddInputResultand a correspondingis_delayed_cache_hitflag that decrementsminibatch_sizeand skipsnetwork_evaluations_++in both search variants.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/utils/atomic.h | New WaitableAtomic<T> with fallback wait/notify via mutex+condvar. |
| src/utils/cache.h | HashKeyedCache::Insert now takes std::unique_ptr<V>& so the caller can tell whether ownership was actually transferred. |
| src/neural/backend.h | Adds FETCHED_DELAYED to AddInputResult. |
| src/neural/memcache.cc | Implements in-flight dedup with a 4-state machine, waits on WaitableAtomic, sizes entries_ using classic search params. |
| src/search/classic/search.{h,cc} | Adds is_delayed_cache_hit, treats it as out-of-order, and excludes it from network_evaluations_. |
| src/search/dag_classic/search.{h,cc} | Same plumbing for the dag-classic search. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Testing using cuda-fp16 on 5080. this pull request: It looks like I have to still check a little more code to avoid adding some bugs. |
|
dag-preview may gain a little because positions are inserted to tt only after evaluation. I used branch: |
|
Test results dag 8+0.08, dag 40+0.4, classic 8+0.08, and classic 40+0.4 |
|
Also passes nonregression on datageneration setup with fixed nodes: https://bench-direct.lczero.org/test/1019/ |
No description provided.