Skip to content

Mix lens corpora by weight and spread unshuffled ones over their files - #60

Merged
a9lim merged 1 commit into
mainfrom
feat/lens-corpus-mix
Oct 28, 2026
Merged

a9lim merged 1 commit into
mainfrom
feat/lens-corpus-mix

Conversation

@a9lim

@a9lim a9lim commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

Why

#59 let a lens fit read the head of any pinned Hub dataset. That head is a fair sample only when the dataset is globally shuffled, and the shuffled candidates (FineWeb-Edu's sample is not, dclm_100BT-shuffled is) are English-only. Qwen's workspace often reads in Chinese. I probed the broad Chinese web corpora on the Hub, and none is shuffled:

dataset first rows
HuggingFaceFW/fineweb-2 cmn_Hani (370 files) the 2013–14 dumps, mostly Traditional Chinese (tripadvisor.com.tw, big5 sites)
Skywork/SkyPile-150B (437 files) one crawl, 2020-40_zh_head_0000.jsonl
opencsg/Fineweb-Edu-Chinese-V2.1 (29k files) the 2_3 score band, every row from source CCI3

A bilingual fit therefore needs two things: a mix, and a way to spread the sample over a dataset whose rows are ordered.

Change

  • Per-source options. --hf-corpus REPO[:CONFIG][@REV][,KEY=VALUE…] takes text_field, files, seed and weight. text_field= replaces the --text-field flag from Fit lenses on any commit-pinned Hugging Face dataset #59 (merged earlier today, no alias), and the fit route's hf_corpus becomes a list of these sources. I used commas because a ; separator would split the command in an unquoted shell line.
  • files=K reads round-robin from K of the train split's data files. The files are ordered by sha256("{seed}:{path}") over their repository paths, so the selection is stable across Python and datasets versions and needs no row-level shuffle. On FineWeb-2 cmn_Hani, 64 files span all five file groups. Its row groups hold 1,000 rows (about 2 MB of text), so each file costs one small fetch.
  • Mix. Repeating --hf-corpus builds a LensCorpusMix, interleaved by smooth weighted round-robin. With weights 3,1 the order is A A B A A A B A …, ties go to the earlier corpus, and a corpus that runs dry leaves the schedule. Neither schedule depends on N, so the first N documents of a longer read are exactly those of a shorter one, and top-up, resume and the no-op preflight keep working by token-id prefix. The interleave assumes every stream is long enough, caps any stream that comes up short, and redraws the schedule. That gives the same result as drawing lazily, without holding every stream open at once.
  • Specs. A spread source adds ;files=K;seed=S, and a mix records mix:W*SPEC|W*SPEC in schedule order. Specs without the new options are unchanged, so existing fits still no-op.
  • Concurrency. An interleave reads up to 8 streams at a time. Opening a Hub data file costs seconds of round trips plus its first row group, and each stream's documents are fixed by its position in the data, so the concurrency can't change the result.
drowse lens fit Qwen/Qwen3-8B --prompts 1000 \
  --hf-corpus HuggingFaceFW/dclm_100BT-shuffled,weight=3 \
  --hf-corpus HuggingFaceFW/fineweb-2:cmn_Hani,files=64

Tests

  • Parsing: options round-trip, and malformed sources are rejected: unknown, duplicate or empty keys, non-integers, files=0, a seed without files, a negative seed, and a weight on a single corpus.
  • Specs: spread and mix specs; the default corpus's spec is unchanged.
  • Schedule: [3,1] gives 0 0 1 0 0 0 1 0, and equal weights are plain round-robin. A dry stream leaves the schedule. On 200 random weight and cap sets, every prefix of the schedule equals the longer schedule cut short.
  • Interleave: with streams running dry at different points, every prefix equals the full read cut short, and each stream keeps its own order.
  • Spread: files are selected by the seeded path hash and opened at the pinned commit through the data_files override; reading round-robins them and outlasts a dry file; every prefix is stable.
  • Mix read: weights 3,1 interleave exactly, and when the second corpus runs dry the first fills in.
  • Session: growing a fit on a two-corpus mix from 2 to 4 documents fits only the 2 new prompts and records the mix: spec.
  • CLI: a repeated --hf-corpus is pinned once as a mix and reaches the preflight and the stream unchanged. Route: a list hf_corpus reaches the streamer, and a bare string, an empty list or the old text_field field gets a 400.

Verification

  • Live Hub, through the worker: the mix above pins both commits. 80 documents extend 40 exactly, the language pattern is eeZe repeating (Chinese share 0.25), and the process exits cleanly.
  • CLI end to end (hf-internal-testing/tiny-random-LlamaForCausalLM, isolated DROWSE_HOME, the same mix): --prompts 20 fits in 38 s. --prompts 40 resumes from 20 ("20 remaining") in 161 s. Rerunning 40 is a 5 s model-free no-op. lens show reports the full mix: spec. The network was slow during these runs, with some Hub requests timing out and retrying.
  • Locally, pytest -m "not gpu": 3980 passed. The 2 failures are the browser affine-Fisher fixture and sampling-fixture case2, the same Mac-numerics failures as on clean main. ruff and pyright are clean.

Cost to expect

The read is latency-bound. Each spread file opens with a few seconds of Hub round trips and one row-group fetch. At --prompts 1000 with the mix above, the Chinese side opens all 64 files once, eight at a time; on this network, expect minutes. That is small next to a Qwen3-8B fit but noticeable in the web UI.

Not in this PR

  • Within-file order. files=K spreads across files, but documents within a file stay in file order, so each file contributes a contiguous run. A larger K spreads further.
  • sae train --hf-corpus. It shares the corpus path, so adding the flag would be small.

A lens fit could read one Hub dataset's head. That is a fair sample only of a
globally shuffled dataset, and the shuffled ones on offer are English-only,
while Qwen's workspace often reads in Chinese. The Chinese web corpora on the
Hub are ordered: fineweb-2 cmn_Hani opens on its 2013-14 dumps, SkyPile on
one 2020 crawl, Fineweb-Edu-Chinese on one score band of one source.

- --hf-corpus takes per-source options, REPO[:CONFIG][@rev][,KEY=VALUE...]:
  text_field, files, seed, weight. text_field replaces #59's --text-field
  flag, and the fit route's hf_corpus becomes a list of these sources.
- files=K reads round-robin from K of the train split's data files, ordered
  by a seed-keyed sha256 of their repository paths. The selection is stable
  across Python and datasets versions, and needs no row-level shuffle.
- Repeating --hf-corpus makes a LensCorpusMix, interleaved by smooth weighted
  round-robin (weights 3,1 read A A B A ...; ties go to the earlier corpus;
  a corpus that runs dry leaves the schedule). Neither schedule depends on N,
  so the first N documents of a longer read are those of a shorter one, and
  top-up and resume keep working by token-id prefix.
- Specs: a source gains ;files=K;seed=S when spread; a mix records
  mix:W*SPEC|W*SPEC in schedule order. Specs without the new options are
  unchanged.
- An interleave reads its streams on up to 8 threads. Opening a Hub data
  file costs seconds of latency, and each stream's documents are fixed by
  the data, so the concurrency cannot change the result.
@a9lim
a9lim enabled auto-merge October 28, 2026 21:50
@a9lim
a9lim merged commit fb42a63 into main Oct 28, 2026
20 of 21 checks passed
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