Skip to content

Fit lenses on any commit-pinned Hugging Face dataset - #59

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

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

Conversation

@a9lim

@a9lim a9lim commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

Why

drowse lens fit could stream only the hard-coded HuggingFaceFW/fineweb-edu sample-10BT head. Any other corpus had to go through --corpus FILE, which is recorded as file:<name> and so drops the Hub pin. Larger, well-provenanced refits need other datasets, starting with HuggingFaceFW/dclm_100BT-shuffled. That dataset is globally shuffled, so its head is a random sample and growing a fit adds a nested one.

Change

  • HfLensCorpus (drowse/io/lens.py) is a frozen (repo, config, revision, text_field). The default corpus is now just one instance of it, so the CLI, the server, sae train and its route share one streaming path.
  • --hf-corpus REPO[:CONFIG][@REV] and --text-field F (default text) on lens fit. --hf-corpus and --corpus are mutually exclusive, and --text-field is rejected without --hf-corpus. REV may be a branch, tag or commit; slashes are allowed, as in refs/convert/parquet.
  • Pinning. pin_lens_corpus resolves REV (default main) to its commit and an omitted CONFIG to the dataset's default at that commit. The CLI pins once per fit, so the model-free no-op preflight and the stream read the same commit.
  • Spec. hf:REPO/CONFIG@COMMIT;preprocess=1;doc_chars=4000, plus ;text_field=F when the text isn't in text. The default corpus produces exactly the spec its existing fits recorded, so their no-op preflight still holds.
  • Documents. The first N non-empty values of the train split, stripped and cut to 4000 characters, as before. Null values are now skipped like empty ones; they used to become the literal text "None". A missing column or a non-text value is an error instead of an endless scan. The read is prefix-stable, so a larger --prompts tops a fit up through the existing token-id prefix resume. To grow a fit after the dataset has moved, pass the recorded commit as REV.
  • Server. The lens fit preparation takes hf_corpus and text_field, parsed before the job starts, with a 400 when malformed. Pinning and streaming run in the cancellable worker.
  • Every corpus read runs in the spawned worker, not only the server's cancellable one, and the worker is terminated once it has sent its result. After a stream stops early, pyarrow's I/O pool can block interpreter exit forever (main thread in __cxa_finalize → arrow::internal::ThreadPool::~ThreadPool). An in-process read of dclm returned in ~10 s and then hung at exit in 4 of 4 runs, and so did a finished CLI fit. The FineWeb-Edu default on main exited in 4 of 4, which is presumably why nobody had hit this. Without the fix, any script running fits in sequence would stall after the first.
  • Renames, no aliases: stream_default_lens_corpus → stream_lens_corpus, resolved_default_lens_corpus_spec → pin_lens_corpus.

Tests

  • Spec string: dclm with the explicit default config; the default corpus's spec is unchanged; the text_field suffix; unpinned corpora refuse to name a spec; parsing round-trips and rejects malformed specs.
  • Pinning: the branch resolves to a commit, the config is read at that commit, and pinning an already-pinned corpus is idempotent with no Hub call.
  • Empty-text skip: empty, blank and null rows are skipped and don't count toward N; text is cut to LENS_DOC_CHARS; a missing field or a non-text value raises.
  • Resume by prefix: reading 2 and then 4 documents from one pinned dataset (with empty rows mixed in) opens the train split at the pinned commit both times and returns a prefix with the same spec. On the toy session, fitting 2 and then topping up to 4 runs the estimator on only the 2 new prompts and records the spec.
  • No-op preflight: it holds for the recorded corpus. It fails for another commit, another dataset or another column. That test now uses a well-formed sidecar, so its positive control means something.
  • CLI: the corpus is pinned once and the same pinned value reaches both the preflight and the stream; bad options exit with status 2.
  • Route: the requested corpus reaches the streamer; a malformed spec, or text_field alone, gets a 400.
  • Worker: with no cancel event, the read still goes through the worker, and a worker stuck after sending its result is terminated.

Verification

  • Live Hub: dclm pins to default@2fa015e4…, and its first 6 documents extend its first 3 exactly, both in-process and through the worker.
  • CLI end to end (hf-internal-testing/tiny-random-LlamaForCausalLM, isolated DROWSE_HOME): fit 3 dclm documents, then --prompts 6 ("resuming from 3 prompts (3 remaining)"), then --prompts 6 again, which is a model-free no-op. Every run exits cleanly, and lens show reports the pinned dclm spec.
  • Locally, pytest -m "not gpu": 3973 passed. The 2 failures are the browser affine-Fisher fixture and sampling-fixture case2, the same Mac-numerics failures that Record the RelP policy in R-lens checkpoints and repair mislabelled ones #58 recorded on clean main. ruff and pyright are clean.

Not in this PR

  • A bilingual mix. Both candidate corpora are English-only, while Qwen's workspace often reads in Chinese. Mixing could be prefix-stable with repeatable --hf-corpus plus weights, interleaved on a fixed schedule (smooth weighted round-robin). Document i's source would then not depend on N, the first N mixed documents would stay a prefix of the first M, and resume would still work; the spec would be something like mix:3*hf:…|1*hf:…. The harder part is the data. Most Chinese pretraining sets on the Hub are not globally shuffled; FineWeb-2 cmn_Hani, for example, is ordered by dump and shard, so its head is a biased sample. A streaming shuffle buffer should be prefix-stable for a fixed seed, but I haven't verified that, and it would tie reproducibility to the datasets version. The simplest route needs no code: publish one pre-shuffled bilingual mix and pin it with --hf-corpus.
  • sae train --hf-corpus. It now shares the corpus path, so adding the flag would be small.

lens fit could stream only the hard-coded FineWeb-Edu sample; any other
corpus went through --corpus FILE, recorded as file:<name>, which drops the
Hub pin. The default is now one instance of HfLensCorpus (repo, config,
revision, text field), and --hf-corpus REPO[:CONFIG][@rev] [--text-field F]
streams any other dataset the same way.

- pin_lens_corpus resolves REV (default main) to its commit and an omitted
  CONFIG to the dataset's default at that commit. The CLI pins once per fit,
  so the no-op preflight and the stream read the same commit.
- The recorded spec is hf:REPO/CONFIG@COMMIT;preprocess=1;doc_chars=4000,
  plus ;text_field=F off the default column. The default corpus keeps the
  exact spec its existing fits recorded, so their no-op preflight still holds.
- Documents are the first N non-empty values of the train split, stripped
  and sliced as before. Null values are skipped like empty ones (they used to
  become the text "None"); a missing column or a non-text value is an error
  rather than an endless scan. A larger N therefore reads a superset prefix,
  and the existing token-id prefix resume tops a fit up; on a globally
  shuffled dataset such as dclm_100BT-shuffled that adds a nested random
  sample.
- Every corpus read now runs in the spawned worker, not only the server's
  cancellable one, and the worker is terminated once it reports. After a
  stream stops early, pyarrow's I/O pool can block interpreter exit forever.
  An in-process read of dclm_100BT-shuffled returned in ~10 s and then hung
  at exit in 4 of 4 runs, as did a finished CLI fit; the FineWeb-Edu default
  exited in 4 of 4.
- The server's lens fit preparation takes hf_corpus and text_field, parsed
  before the job starts.
- stream_default_lens_corpus and resolved_default_lens_corpus_spec become
  stream_lens_corpus and pin_lens_corpus; sae train and its route call the
  new name with the default corpus.
@a9lim
a9lim enabled auto-merge October 28, 2026 20:55
@a9lim
a9lim merged commit 1233b7c 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