Repository navigation
fix: sea-state batch workers write their own numba cache - #48
Conversation
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1d79224e92
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a6ee9660c5
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
020e544 to
150ceae
Compare
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
run_sea_state_batch with workers > 1 started its spawn workers with the caller's environment, so all of them shared one numba cache directory. numba numbers a new on-disk cache entry from the index it last read and saves the index before the data (numba/core/caching.py, IndexDataCacheFile.save): two workers compiling one kernel for different signatures at the same time could both take the same data file and leave the index pointing at the other's machine code, which a later process would load. Each worker is now a one-process spawn pool started while NUMBA_CACHE_DIR names its own directory in a temporary directory of the batch, removed afterwards; runs go to whichever worker is free, as with the shared pool. A worker that fails to start shuts down the pools already started, and the caller's NUMBA_CACHE_DIR is restored in every case. Each worker compiles the kernels it uses; results are unchanged. tests/test_sea_state_batch_workers.py: each worker sees its own cache directory; a cold-cache batch on two workers writes kernels into each worker's own directory, leaves nothing behind and matches the in-process batch; a failed start leaves no pool running. Signed-off-by: Jae Hoon Seo <jaehoon.seo@inha.ac.kr>
A temporary cache per worker made every batch recompile every kernel in every worker. Worker i now writes the slot folder slot-<i> under cablexsec/numba-batch in the user cache directory (LOCALAPPDATA on Windows, ~/Library/Caches on macOS, XDG_CACHE_HOME or ~/.cache elsewhere; CABLEXSEC_BATCH_CACHE_DIR overrides it), which later batches reuse. The batch holds a non-blocking exclusive lock file per slot (msvcrt or flock; released by the operating system if the process ends), so one slot is written by one process at a time. A batch beside another takes the next free slots out of 16; a worker without a free slot, or with an unwritable folder, gets a temporary cache that is removed afterwards. The tests give the batch caches a folder of the test process. New tests: slots are kept and a second batch compiles nothing (its index files are unchanged); results equal the in-process batch exactly when every process runs BLAS on one thread (as CI does), else to rtol 1e-10 with the reason stated; concurrent batches take the next free slots; the temporary fallback; an unusable folder; the platform folder and the override. Signed-off-by: Jae Hoon Seo <jaehoon.seo@inha.ac.kr>
The caller held the slot locks. If it was killed while a worker was still compiling, the locks were released at once while the orphaned worker kept writing its cache, and a later batch could take the same slot. The worker now locks its slot itself (_claim_slot, its first task) and holds it for its lifetime; the operating system releases it when the worker ends. The caller only checks that a slot looks free before starting a worker on it; if another process wins the slot meanwhile, the claim fails, that worker is shut down and the next slot is tried. Temporary fallback caches are removed after every pool has shut down. Tests: the worker, not the caller, holds the slot while it runs; a slot taken between the check and the claim is skipped. Signed-off-by: Jae Hoon Seo <jaehoon.seo@inha.ac.kr>
Two batches that start workers from different threads of one process could interleave their changes of the process-wide NUMBA_CACHE_DIR, so that both workers inherited the same cache directory and the caller's value was not restored. _start_worker now holds a process-wide lock around the whole save, set, spawn and restore sequence. Also rewraps a module docstring line over the line limit. Test: workers started from two threads at once each run with their own cache directory, and the caller's value is restored. Signed-off-by: Jae Hoon Seo <jaehoon.seo@inha.ac.kr>
Signed-off-by: Jae Hoon Seo <jaehoon.seo@inha.ac.kr>
150ceae to
fc12715
Compare
|
@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Summary
run_sea_state_batch(..., options=SeaStateBatchOptions(workers=N))withN > 1started its spawn workers with the caller's environment, so all of them wrote one numba cache directory. That cache is not safe for concurrent writers.IndexDataCacheFile.save(numba/core/caching.py):<kernel>.1.nbc,.2.nbc, ...);Two workers that compile the same kernel for different signatures at the same time can both pick
.1.nbc. The final index can then name one signature while the file holds the other's machine code, and a later process that loads that entry runs code compiled for different argument types.Change
Each worker writes a numba cache of its own, and that cache persists across batches.
slot-<i>undercablexsec/numba-batchin the user cache directory (%LOCALAPPDATA%on Windows,~/Library/Cacheson macOS,$XDG_CACHE_HOMEor~/.cacheelsewhere);CABLEXSEC_BATCH_CACHE_DIRoverrides it_claim_slot: non-blocking exclusivemsvcrt.locking/fcntl.flock). It holds the lock for its whole lifetime, so the slot stays taken even if the caller ends first; the operating system releases it when the worker ends.ProcessPoolExecutor(max_workers=N)_workers), each started whileNUMBA_CACHE_DIRnames its slotpool.mapThe caller's
NUMBA_CACHE_DIRis restored in every case, including a worker that fails to start; the pools already started are then shut down. The tests pointCABLEXSEC_BATCH_CACHE_DIRat a folder of the test process (tests/conftest.py), so they never write to the user cache directory.Wall time (local, Windows, 4 runs,
workers = 2, final code)workers = 1)Both worker batches are bit-identical to the in-process batch (largest relative difference 0). On a batch this small, the cost is mostly the spawn and import of the workers; the persistent slots save the recompilation, here 2 s.
workers = 4was not measured: this machine is limited to two parallel processes.Tests
tests/test_sea_state_batch_workers.py(no taxonomy marker, so the report's taxonomy counts do not change):NUMBA_CACHE_DIR, also when workers are started from several threads at once (the change of the process environment is serialised), and the caller's value is restored;.nbiindex file are unchanged.rtol=1e-10and states the reason: threaded BLAS reductions in the calling process differ at round-off.All of
test_sea_state_batch.pyandtest_sea_state_batch_inputs.pypass, also withCABLEXSEC_DISABLE_NUMBA=1. mypy reports no issues; ruff check and ruff format pass.