Skip to content

Studio: prepare decision datasets faster, in less memory, with row progress - #16106

Merged
danielhanchen merged 1 commit into
mainfrom
studio-decision-prep-speed
Oct 30, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
studio-decision-prep-speed

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Decision fine-tuning in Studio could sit on "Preparing decisions..." for the better part of an hour on a large dataset, with nothing on screen moving. A user training CNWPlayer/Vega-1.6-128M-Base on tasksource/procedural-typed-decisions (config all, 396k rows, 293 MB) saw the model row stuck on "Preparing decisions" and restarted, thinking it had hung.

What was going on

  1. Slow tokenization. For Clef models (including any plain LLM trained with a fresh Clef head), FastDecisionModel.build_dataset runs the vendored Clef encode_record once per row. That function tokenizes about 70 short strings per row, one tokenizer call each ("OPTION 1: ", "\n", "END FIELD\n", the field headers, the fixed system prompt). Nearly all of them repeat on every row, so the time goes to the tokenizer wrapper, not to real work: about 4.5 ms per row, single core.
  2. Memory. Every stored token id was its own Python int object, held by both item["input_ids"] and record.input_ids. That is about 54 KB per row, so roughly 20 GB of RAM for this dataset before training starts.
  3. No progress, on the wrong row. The status stayed at one "Preparing decisions..." string for the whole build, and the overlay files that wording under the model, so the Model weights row looked stuck while the Dataset row said Ready.

Changes

  • unsloth/models/decision.py: _clef_items hands encode_record a small memoizing tokenizer (_MemoTokenizer). It caches add_special_tokens=False results for strings up to 512 characters (the unique per-row state text skips the cache), caps the cache size, returns a fresh list on every call (encode_record extends the first list it gets), and interns token ints so the stored ids share one int object per token id. Anything other than the exact call encode_record makes passes straight through to the real tokenizer. The vendored Clef file is untouched; build_dataset's signature and item types are unchanged.
  • studio/backend/core/training/decision_trainer.py: rows are passed to build_dataset as _ProgressRows, a list that reports Preparing decision rows P% (n/total) at most once a second while the builder reads it, and checks for a stop on every row. The frontend already renders that exact shape as a determinate bar, and the word "rows" files it under the Dataset row, so there is no frontend change. Evaluation rows get their own Preparing evaluation rows label. This works for the CUDA and MLX builders alike, since both read the rows once in order.

Results

Real rows, the first all train shard (120,000 rows, 389,823 decisions), CNWPlayer/Vega-1.6-128M-Base tokenizer, transformers 5.3.0, Studio's own question validation included. Each arm runs its own tree (asserted from the imported module paths):

Time ms / row RAM for the built items Items (sha256 of ids, spans, targets, labels)
main 545.0 s 4.54 6.54 GB (54.5 KB / row) 8980a121...
this PR 195.4 s 1.63 4.14 GB (34.5 KB / row) 8980a121... (identical)

For the full 360k-row train split that is about 27 minutes and ~20 GB on main versus about 10 minutes and ~12 GB here, with a live progress bar the whole way.

The built dataset is byte-identical: the same token ids, question and option spans, targets and labels on every row.

Tests

  • tests/test_decision_model.py::test_clef_items_tokenize_each_repeated_string_once: _clef_items output equals plain per-row encode_record, and every distinct string reaches the tokenizer once. On main it makes 888 tokenizer calls for 46 distinct strings, so it fails there.
  • tests/test_decision_model.py::test_memo_tokenizer_returns_fresh_lists_and_passes_other_calls_through: mutating a returned list cannot corrupt the cache; long strings are not cached; other call shapes behave like the real tokenizer.
  • studio/backend/tests/test_decision_dataset_prefetch.py: the progress messages parse with the frontend's own COUNTED_PREPARATION_RE and classify as dataset work under its DATASET_PREPARATION_RE (both read from preparation-progress.ts, so the two sides cannot drift); a stop raised mid-read ends the build. Both fail on main.
  • Two existing tests that pinned the old "Preparing decisions..." text now use the new status.

Not changed: the MLX tokenization path (it goes through unsloth_zoo's pipeline), the training step, and the Laya layout.

Screenshots

Studio's training-start overlay 20 seconds into preparing 40,000 real rows of tasksource/procedural-typed-decisions, from two separate Studio installs built from the merge base (left) and this branch (right). The run is a real decision training run through POST /api/train/start on a small local Laya checkpoint. This host has no GPU, so Studio was started with UNSLOTH_ALLOW_CPU=1 and the page's GPU check was answered as present so that the Train page opens; everything else on screen comes from the running backend.

Before: the Model weights row shows "Preparing decisions" with no count. After: the Dataset row shows "Preparing decision rows", 14%, 5,952 / 40,000 and a filling bar

Left: one "Preparing decisions" step on the Model weights row for the whole build. Right: the same step on the Dataset row with a live percent, row count and bar. In the same run on Studio's worker, main sent 1 status during the ~145 seconds of preparation and this branch sent 147 (one a second); both runs ended with the same held-out accuracy (0.29 -> 0.32) and calibration error (0.07).

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T11:25:01.271332Z 990ae9a Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen danielhanchen self-assigned this Oct 30, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 990ae9a337

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 30, 2026
@danielhanchen
danielhanchen merged commit 3f8e4d3 into main Oct 30, 2026
4 of 53 checks passed
@danielhanchen
danielhanchen deleted the studio-decision-prep-speed branch October 30, 2026 12:14
danielhanchen added a commit that referenced this pull request Oct 30, 2026
…6123)

`Repo tests (CPU, rest-2)` is red on main since #16106 (prepare decision
datasets faster, in less memory, with row progress). It fails on
unrelated PRs, for example #16114:

```
tests/test_source_read_encoding.py::test_checked_in_file_reads_name_an_encoding[14]
AssertionError: 1 file reads in the test trees touch a checked-in file with the platform default encoding ...
  'studio/backend/tests/test_decision_dataset_prefetch.py:298: read_text()'
```

`_frontend_regex` reads
`studio/frontend/src/features/studio/preparation-progress.ts` with
`read_text()`. On Windows that decodes as cp1252 and breaks as soon as
the file gains a non-ASCII byte. The guard is right and the test was
wrong, so this adds `encoding = "utf-8"`. This is the same fix #16025
made for #15996.

## Verification

- `tests/test_source_read_encoding.py`: fails on main on the line above.
16 passed here.
- `studio/backend/tests/test_decision_dataset_prefetch.py`: 11 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