Skip to content

Cap a streamed eval split once per evaluate() call - #19244

Merged
danielhanchen merged 4 commits into
mainfrom
perf/r40-streaming-eval-cap-once
Nov 24, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
perf/r40-streaming-eval-cap-once

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

When the eval split is a streaming datasets.IterableDataset, the late eval cap (_wrap_sft_evaluate_cap) runs _cap twice on every evaluate() call. evaluate() caps the split, then Transformers calls get_eval_dataloader, which is wrapped too and sees the capped object. A stream has no _fingerprint, so _mutation_token returns None and _cap_still_holds rejects the mark the first pass just set. The second pass then:

  • runs another _column_names probe, next(iter(ds)), which pulls one whole batched-map batch through every upstream map (the tokenizer map included). With the default batch size of 1000, an eval split of up to 1000 rows is re-streamed and re-tokenized in full just to read one row;
  • stacks a second slice map and supervision filter on the chain the dataloader iterates.

This happens at every eval step during training (stored split) and for evaluate(eval_dataset = stream).

Change

_cap marks a stream view it built itself with map/filter (own __dict__ flag), and _cap_still_holds trusts that view the same way it already trusts _CappedBase wrappers: by cap and drop_unsupervised only. That object is a fresh lazy view nobody else holds, and every row it yields passes through the slice and filter it was built with, so the mark cannot go stale.

Unchanged on purpose:

  • A caller's own stream is never flagged, so it is still probed and capped on every call (it can change under the same identity).
  • Map-style copies are not flagged and stay on the fingerprint path.
  • A different cap or drop_unsupervised still rescans.

This is a runtime wrapper, not part of the generated trainer source, so the compiled cache is not involved.

Results

Local streaming split (IterableDataset.from_generator + batched tokenize map, batch size 1000 as zoo's _iterable_batch_size), cap 16, 3 calls each via stored split and via evaluate(eval_dataset = stream), measured through the real wrappers:

eval rows arm source rows pulled by the cap per call tokenize batches by the cap chain steps read by the dataloader rows yielded rows digest
300 base 600 2 6 292 2e5af3a16ee86e44
300 head 300 1 4 292 2e5af3a16ee86e44
2500 base 2000 2 6 2437 58ca3b515e235f0e
2500 head 1000 1 4 2437 58ca3b515e235f0e

Total upstream work per eval call (cap plus the eval read itself) drops from 900 to 600 source rows at 300 rows, and from 4500 to 3500 at 2500 rows. Yielded rows are identical in order and content across both arms and all six consecutive calls.

Tests

  • test_a_streamed_eval_split_is_capped_once_per_evaluate: four consecutive evaluations (stored and passed) probe the stream once each, stack exactly one slice and filter, restore the stored split and yield identical rows.
  • test_only_a_stream_the_cap_built_is_trusted_without_a_fingerprint: a caller's marked stream is still rechecked, only the literal flag counts, a different cap or mode rescans, and a map-style copy stays on the fingerprint path.
  • Full tests/version_compat/test_trl_padding_free_max_length.py passes.

evaluate() and get_eval_dataloader both run the late eval cap, and a
datasets.IterableDataset has no _fingerprint, so the second pass rejected
the mark the first one set, re-probed one more upstream batch (re-tokenizing
up to the whole eval split) and stacked a second slice + filter. Trust the
stream view _cap built itself, as _CappedBase wrappers already are; a
caller's own stream and map-style copies are unchanged.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T14:12:13.161855Z 95fecab 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 8c617ec941

ℹ️ 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".

[skip ci]
@danielhanchen danielhanchen self-assigned this Nov 24, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 24, 2026
@danielhanchen
danielhanchen marked this pull request as ready for review November 24, 2026 14:10
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 95fecab665

ℹ️ 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 added a commit that referenced this pull request Nov 24, 2026
## Problem

On runners with 16 GB of RAM, 35 llama.cpp placement tests fail across
`(Python 3.13, g-l)` and `(Python 3.13, m-r)`. The staging runs for
#19113, #19181 and #19244 all show them, for example:

```
test_llama_cpp_placement.py::test_the_moe_cache_stays_off[ram_cannot_pin_experts-]
  assert _load_mode(cmd) == "none"   (None == 'none')
Load mode: keeping mmap; pinning the host-side weights plus the 9.0 GB llama-server grows into
would hold more than 50% of the 15.3 GB of RAM, which the OS cannot page out.
```

They pass on larger runners and locally, so this only shows up on some
runners.

## Cause

#19231 (price llama-server host RAM growth before pinning a spilled
load) is correct. A pinned launch (`--load-mode none`) now also checks
that the pinned host-side weights plus llama-server's growth stay under
a safe share of total RAM. That total comes from
`_host_memory_capacity_mib()`, which reads the real `MemTotal` and the
cgroup limit.

The placement tests fake the GPUs and, where they care, the available
RAM. They never pin the total, so each launch decided against whatever
machine ran it. Reproduced locally by stubbing
`_total_system_memory_mib` to 15667 MiB: 35 failed, 770 passed in the
three files below.

## Fix

- `test_llama_cpp_placement.py` gets an autouse fixture,
`_pinned_host_ram`. It pins `LlamaCppBackend._total_system_memory_mib`
to 256 GiB and `_cgroup_memory_limit_mib` to `None`, so every launch
sees the same host on any runner.
- `test_lookup_table_fit.py` and `test_prompt_cache_sized_to_context.py`
launch through that file's helpers, so they import the fixture.
- Tests that are about host RAM still set their own total in the test
body, which overrides the fixture.

## Verification

- The three files on this branch: 805 passed.
- With the host forced to 15.3 GB (a plugin stubbing
`_total_system_memory_mib`): 805 passed on this branch, and 35 failed on
main.
- All 34 test files that import `test_llama_cpp_placement`: 2365 passed
and 24 skipped.
- One test failed:
`test_cached_gguf_routes.py::test_pipeline_class_guard_passes_every_shipped_family`.
- It fails identically on main, because this host has diffusers
0.41.0.dev0 and the test needs 0.42.0. It is unrelated to this change.
@danielhanchen
danielhanchen merged commit faf205f into main Nov 24, 2026
5 of 47 checks passed
@danielhanchen
danielhanchen deleted the perf/r40-streaming-eval-cap-once branch November 24, 2026 16:00
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