fix(magic): reject chunked query sets in per-query MAGIC - #418
Merged
Conversation
luciaquirke
force-pushed
the
fix/reject-chunked-query-sets
branch
2 times, most recently
from
August 6, 2026 13:29
214812e to
6cfd0c9
Compare
Per-query MAGIC scores one column per query *document*, but a chunked
query set (`query.chunk_length > 0`) has rows that pack several
documents and documents that span several rows, so query `i` is not row
`i`. `compute_per_query_magic_scores` selected row `qi` and sized the
stream's weights by row, while `DataStream` indexes weights by document
id whenever the batch carries `doc_ids`, so a run crashed with:
IndexError: index 2 is out of bounds for dimension 0 with size 2
bergson/magic/data_stream.py:132 self.weights[indices]
Chunking exists to pack a training set efficiently; a query set is
small — 50 documents in the compare_wikitext runs — so packing it buys
nothing, and every shipped config already passes query.chunk_length 0.
Rather than teach the per-query path to split and repack documents,
require the query rows to be documents and say so at config time,
before a run trains for hours.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
luciaquirke
force-pushed
the
fix/reject-chunked-query-sets
branch
from
August 6, 2026 13:34
6cfd0c9 to
41bb037
Compare
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.
Third take on the bug behind #414 and #417, and the smallest one: 6 lines of source instead of ~70. Don't support chunked query sets in per-query MAGIC at all.
The bug
bergson magic runs/x --model EleutherAI/pythia-14m \ --data.dataset Salesforce/wikitext --data.subset wikitext-2-raw-v1 \ --data.split "train[:96]" --data.chunk_length 32 \ --query.dataset Salesforce/wikitext --query.subset wikitext-2-raw-v1 \ --query.split "train[300:320]" --query.chunk_length 32 --batch_size 2Per-query MAGIC scores one column per query document, but a chunked query set has rows that pack several documents and documents that span several rows, so query
iis not rowi. The per-query stream sizes its weights by row whileDataStreamindexes them by document id whenever the batch carriesdoc_ids.Why reject instead of support
Chunking exists to pack a training set efficiently. A query set is small — 50 documents in the
compare_wikitextruns — so packing it buys nothing, and splitting a document across rows only makes the per-query path reconstruct what chunking took apart.The check is at config time, so it fires in ~4s on the repro above rather than after a run has trained for hours:
It's scoped to per-query mode. The aggregate-query backward (
query_method: mean/sum) weights the query stream by document id consistently and is fine with chunked queries, so it's left alone — the test asserts both halves.What it costs
Every shipped config already passes
query.chunk_length: 0(examples/magic/*.yaml,examples/compare_wikitext/*.yaml,examples/replicate_bae_approx_unrolling_source/*.yaml), so no run configuration changes.One test did rely on the rejected combination:
test_distributed_magic.pybuilt its query from raw wikitext withchunk_length=32under the defaultquery_method="none", so the guard would have made every test in that file raise at config construction. Its query now comes fromEleutherAI/bergson-wikitext-512-chunksatchunk_length0 — the same pre-chunked dataset the example configs use, where a row is already a document. One ~650-token document either way, and the tests only assert FSDP-vs-DDP agreement, so the query content is immaterial. That file is@requires_multi_gpuand I have no GPU here, so it wants a run on a GPU box before merge.Compared to the alternatives
#414 rebuilds a masked mini-dataset per query; #417 teaches
DataStreamabout documents (rows=/doc_id=). Both produce bit-identical scores and both work; this one deletes the requirement instead of meeting it. Close whichever two lose.Residual worth knowing
The check keys on
query.chunk_length, which is the only way our pipeline puts adoc_idscolumn on a query set (setup_data_pipelineonly chunks;attach_doc_ids_if_missingis train-side). A hand-built query dataset that ships its owndoc_idscolumn would slip past it and hit the originalIndexError. Happy to add a one-line assert on the per-query path too, but it can only fire after training, which is why the config check is the useful one.Testing
--noskip_validation): scores written, leave-k-out validation completes.pytest tests/test_per_query_magic.py tests/test_config_runner.py tests/test_distributed_magic.py→ 21 passed, 5 skipped (the skips are the multi-GPU tests).MagicConfigwith a chunked query config while handingworker()pre-tokenized datasets directly; its query config is nowchunk_length0, which is what its data already was.🤖 Generated with Claude Code