Bugfix/optuna - #305
Open
anay-rfai wants to merge 10 commits into
Open
Conversation
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Want higher recall? High effort reviews run extra passes and find more bugs. A team admin can switch effort levels in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit d72ee1c. Configure here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Changes
Adapted interim-vs-interim Optuna pruning. Vanilla Optuna compares a running trial's intermediate values against completed trials' intermediate values (interim-vs-complete), which is unusable in RapidFire's sharded pipeline because no trial is "complete" until the entire sweep finishes. This PR rewires the pruner to compare each running trial against the median of its simultaneously-running peers' intermediate values (interim-vs-interim). Because roughly half the population sits below the median of the rest by construction at every evaluation, the adapted
MedianPrunerbehaves like hyperband / successive halving: triggered on shard completion, the population halves (and is replaced if budget allows) at each successive shard. This is what produces the 7-of-8 and 5-of-6 prune rates shown in the tutorial screenshots. Pruning remains a heuristic compute-savings mechanism — it does not change Optuna's (already absent) guarantee thatstudy.best_trialis the global optimum, vanilla or adapted.Pruning comparison anchored on cumulative shards completed. The peer-median comparison is anchored on the cumulative number of shards completed, for both fit and evals modes (cumulative shard count in fit, raw shard id in evals), so trials are always compared at the same progress point regardless of when they started. A per-prune-check trace line (
[RFOptuna prune-check] trial=… step=… dir=… current=… peers=[…] median=… -> PRUNE/continue (reason)) is emitted so the runtime ordering / peer-availability gap — the fastest pipeline reaching each shard first and finding no peers (no_peers_at_step) — is visible in the dashboard/logs, alongsidecurrent_is_nan,worse_than_median, andbetter_than_median.Front-loaded batches for fit-mode shards. In fit mode, batches are now front-loaded for each shard of data so that pruning comparisons are fair between configs that have different effective batch sizes. Without front-loading, a config with a larger effective batch would have processed more samples at the same wall-clock checkpoint and look "better" purely from batching, biasing the peer-median comparison. Front-loading puts every config on the same samples-processed footing at each shard boundary before the prune decision runs.
Prune → replacement flow and the
build_all_indexesflag (evals mode). When a trial is pruned, Optuna is asked for a replacement trial. A replacement config needs its RAG index to already exist, which is governed by the newbuild_all_indexesflag onRFOptuna(evals mode only; ignored in fit mode):build_all_indexes=True(default): every RAG index the search space can reach is built up front, so any replacement Optuna suggests during the run already has its retriever available. The cost is that indexes Optuna never visits are built too — the same index countRFGridSearchwould build for the equivalent space.build_all_indexes=False: only the indexes needed by then_initialinitial configs are built. A replacement suggestion that needs an unbuilt index is rejected and resampled — cheaper up front, but it narrows the space Optuna can actually explore. A rejected candidate is toldFAIL(notCOMPLETE), which keeps it out ofbest_trialand out of the sampler's model so TPE is not taught that this region scored anything; only accepted suggestions consume budget.Index-affecting
Rangeknobs are discretised either way (each drawn down toRange.sample_ndistinct values bysample(n)), since the set of indexes must be finite and known before any query runs; the constructorseedselects which part of each range this run explores.Search/sampler knobs.
granularity("chunk"default, or"epoch") controls when pruning is evaluated in fit mode (ignored in evals). A singleseed(default 42) governs the algorithm's own stochastic state — everyRangegenerator, the global RNG used byList.sample()/ fallback draws, and the Optuna study sampler — soRFOptuna()is reproducible out of the box; the run-levelseedpassed torun_evals/run_fitis ignored for the algorithm's draws and only governs surrounding infrastructure (dataset sharding, etc.).Fixes. Fixes three tracked Optuna issues —
RF-OPT-1,RF-OPT-2, andRF-OPT-4— and an online-aggregation bug inrapidfireai/evals/metrics/online_strategies.py(with corresponding controller updates inrapidfireai/evals/scheduling/controller.py).Tutorials. Adds a global seed variable across all tutorial notebooks for reproducibility, adds a new Optuna RAG/scifact tutorial notebook (
tutorial_notebooks/rag-contexteng/rf-tutorial-optuna-rag-scifact.ipynb), and beefs up the existing Optuna tutorial notebooks with richer logging and walkthrough content.No breaking changes to public APIs.
Changelog Content
Additions
tutorial_notebooks/rag-contexteng/rf-tutorial-optuna-rag-scifact.ipynb).build_all_indexesflag onRFOptuna(evals mode) to control whether every reachable RAG index is built up front (True, default) or only then_initialconfigs' indexes (False, with missing-index suggestions rejected and resampled).Changes
Fixes
RF-OPT-1,RF-OPT-2,RF-OPT-4.rapidfireai/evals/metrics/online_strategies.py.Testing
Screenshots (if applicable)
Add screenshots to help explain your changes.
Checklist
Performance Impact
If this PR affects performance, describe the impact and any optimizations made.
Related Issues
Fixes #303 #302 #301 #300
Note
High Risk
Changes touch eval orchestration, RAG context lifecycle, Optuna trial sampling/pruning, and fit shard batching—misaligned hashes or coverage could break pipelines at runtime despite extensive new tests.
Overview
RFOptuna now prunes by comparing each trial’s intermediate metric to the median of concurrently running peers (not Optuna’s completed-trial median), with per-check
[RFOptuna prune-check]logging and dashboard wiring viaset_logger. Fit callbacks report one value per chunk at a cumulative chunks-completed step;pruner=Noneskips pruning viaNopPruner. Evals replacements can be rejected when their RAG index was never built (set_context_feasibility,FAILtrials,build_all_indexesdefault True), withget_context_coverage_leavesenumerating index-affecting combos beforeget_runsand the evals controller pre-building those contexts.Hyperparameter plumbing:
Rangeis a seeded pure sampler (sample(n),sample_n,set_seed); RFOptuna owns a range value cache so coverage enumeration and Optuna suggest share the same discretized values for index-affecting paths (retrieval-onlysearch_cfg/reranker_cfgstay continuous). NestedListof configs registers conditional Optuna params (api_config[idx].…); unreachableRange/Listin literals raises at sampling time. RFGridSearch errors onRange; RFRandomSearch / RFOptuna use constructorseed(run-level seed ignored for algorithm draws). Legacycreate_model_fnremoved fromAutoMLAlgorithm.Evals controller centralizes context hashing (
combine_context_hash,leaf_context_hash) and fails registration/launch when a pipeline’s context was not built. Fit chunking assigns extra batches to the first chunks (fairer for Optuna batch-size comparisons). Online metrics clamp means and guardsqrton negative variance.Reviewed by Cursor Bugbot for commit 0a84538. Bugbot is set up for automated code reviews on this repo. Configure here.