Skip to content

Reset shouldSaveState after use; clamp numThreads to corpus size - #223

Open
pangwangshu wants to merge 1 commit into
mimno:masterfrom
pangwangshu:wapang/issue219-shouldsavestate-numthreads-guard
Open

pangwangshu wants to merge 1 commit into
mimno:masterfrom
pangwangshu:wapang/issue219-shouldsavestate-numthreads-guard

Conversation

@pangwangshu

Copy link
Copy Markdown

Part of #219 — correctness finding #5 and the robustness finding about numThreads > numDocs. Two small, independent fixes.

1. shouldSaveState is never reset, so saveSampleInterval is a dead knob.
collectAlphaStatistics() arms WorkerCallable to collect docLengthCounts/topicDocCounts for the next call(). WorkerRunnable and DMRCallable both clear the flag at the end of their run; WorkerCallable — the class that replaced WorkerRunnable and the one ParallelTopicModel actually uses — never did. Once hyperparameter optimization starts collecting, every subsequent iteration kept accumulating instead of only every saveSampleInterval-th one, silently turning the intended thinned samples into one long, highly autocorrelated run. Now reset at the end of call(), after the armed pass has used it.

2. numThreads > numDocs silently serializes the run.
estimate() computed docsPerThread = data.size() / numThreads, which is 0 whenever numThreads exceeds the corpus size, so every thread but the last got zero documents while the last took the entire corpus. Output stayed correct (empty workers contribute all-zero counts to the merge), but with all parallelism lost and numThreads full copies of typeTopicCounts allocated regardless. estimate() now clamps numThreads to the corpus size, with a log warning, before any of that allocation happens.

Added TestWorkerCallable#shouldSaveStateResetsAfterOneCall (arms the flag, calls call() twice, confirms the histogram total after the second, unarmed call matches the first instead of doubling) and TestParallelTopicModelNumThreadsGuard (trains a 3-document corpus with numThreads=16, confirms it's clamped to 3 and the run still produces valid topic assignments).

Two small, independent robustness fixes from mimno#219.

1. Correctness finding mimno#5: shouldSaveState is never reset, so
   saveSampleInterval is a dead knob. collectAlphaStatistics() arms
   WorkerCallable to collect docLengthCounts/topicDocCounts for the next
   call(), but nothing ever cleared it back to false -- WorkerRunnable and
   DMRCallable both do this at the end of their run; WorkerCallable, which
   replaced WorkerRunnable and is what ParallelTopicModel actually uses,
   never did. Once hyperparameter optimization starts collecting, every
   subsequent call() (i.e. every iteration) kept accumulating into those
   histograms instead of only every saveSampleInterval-th one, silently
   turning the intended thinned samples into a long, highly autocorrelated
   run. Now reset at the end of call(), after the armed pass has used it.

2. Robustness finding: numThreads > numDocs silently serializes the run.
   estimate() computed docsPerThread = data.size() / numThreads, which is
   0 whenever numThreads exceeds the corpus size, so every thread but the
   last got zero documents while the last took the entire corpus. Output
   was still correct (empty workers contribute all-zero counts to the
   merge), but with all parallelism lost and numThreads full copies of
   typeTopicCounts allocated regardless. estimate() now clamps numThreads
   to the corpus size, with a log warning, before any of that allocation
   happens.

Added TestWorkerCallable#shouldSaveStateResetsAfterOneCall (arms the flag,
calls call() twice, confirms the histogram total after the second,
unarmed call() matches the first instead of doubling) and
TestParallelTopicModelNumThreadsGuard (trains a 3-document corpus with
numThreads=16, confirms numThreads is clamped to 3 and the run still
produces valid topic assignments).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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