Repository navigation
Add opt-in memory and batch efficiency reports - #18603
Conversation
UNSLOTH_MEMORY_REPORT=1 prints, per device, memory held outside torch's caching allocator (CUDA context, NCCL, vLLM, other processes) at model load, vLLM init, the first forward and the first optimizer step. UNSLOTH_BATCH_REPORT=N prints the share of token slots holding real tokens and the share carrying a loss over the first N SFT micro-batches, so padded rows can be compared against packing / padding-free. Both are off by default and install nothing when unset.
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 410ceafb3a
ℹ️ 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".
…es the default window
[skip ci]
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 45e274c431
ℹ️ 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".
… CUDA context [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…ort and FLM host tests (#18654) ## Summary `Repo tests (CPU, rest-2)` is red on `main` (seen on #18650, reproduced on current `main`). `tests/test_source_read_encoding.py` flags three new reads that use the platform default encoding: ``` AssertionError: 2 file reads in the test trees touch a checked-in file with the platform default encoding ...: ['tests/test_run_report.py:246: read_text()', 'tests/test_run_report.py:267: read_text()'] AssertionError: 1 file reads in the test trees touch a checked-in file with the platform default encoding ...: ['studio/backend/tests/test_npu_flm_host.py:286: read_text()'] ``` ## Cause - #18603 (opt-in memory and batch efficiency reports) added the two reads in `tests/test_run_report.py`. - #18554 (FastFlowLM without Lemonade) added the read in `studio/backend/tests/test_npu_flm_host.py`. All three are bare `read_text()` calls on checked-in files. On Windows they would decode with the locale code page as soon as one of those files gains a non-ASCII byte. The guard is right; the reads are what needs fixing. ## Change - `encoding = "utf-8"` on those three `read_text` calls. No behaviour change. ## Testing - `tests/test_source_read_encoding.py` lists the three offenders on `main` and passes with this change. - `tests/test_source_read_encoding.py` and `tests/test_run_report.py`: 30 passed. - `studio/backend/tests/test_npu_flm_host.py`: 21 passed.
Summary
Two opt-in diagnostics. Both are off by default; when the variable is unset nothing is installed and no step does any extra work.
UNSLOTH_MEMORY_REPORT=1Per device, prints the memory held outside torch's caching allocator (
total - free - reserved: CUDA context, NCCL buffers, vLLM, cuBLAS workspaces, other processes) at four points:from_pretrainedreturn goes through_mark_requested_float32)fast_inference = True, afterload_vllminllama.pyandvision.py)TrainerCallback)torch.cuda.max_memory_reservednever sees this memory, so it is the part of an OOM the allocator numbers cannot explain. Each stage prints once per model / trainer. Only devices this process already uses are queried, so the report never opens a CUDA context on another card.UNSLOTH_BATCH_REPORT=N(1/true= 16)Wraps the SFT trainer's
training_stepfor the first N micro-batches and prints one line: the share of token slots holding real tokens and the share carrying a loss (the shifted causal targets, so a row's first label is not counted, matching what the loss sees). Real tokens come fromattention_mask, elsepacked_seq_lengths, else every slot. It sits outsideUNSLOTH_TOKEN_BUDGET, so it sees each micro-batch before any split. It costs one host sync per sampled micro-batch and nothing after. A run shorter than N micro-batches prints its line aton_train_end.This makes it easy to compare padded rows against packing / padding-free on your own data before choosing one.
Tests
tests/test_run_report.py(CPU, 11 tests):packed_seq_lengths), and padding-free rows without a mask but withcompletion_masktraining_stepis not wrapped, and the memory report never touchestorch.cudatraining_stepstill runs, and a short run is flushed at train endtransformers.Trainer: the batch line has the exact expected counts, the forward hook fires once, and the step callback runsinstall_token_budget, every TRL trainer gets the memory report, and both load points and both vLLM init points callreport_memoryThe new tests fail on the base commit (the module does not exist) and pass with the change.
tests/test_mfu_logging.pyandtests/test_token_budget_batches.pystill pass.GPU run (Colab L4)
Qwen3-0.6B 4-bit LoRA SFT, 6 steps per arm, same data (64 rows of mixed length), at the base commit plus this change:
The supervised counts above use the shifted targets (
labels[..., 1:]): padded rows drop one target per row (16 rows), so padded rows and padding-free agree at 1,946 on the same data (the padded figure is computed from the run's counts; the Colab run predates the shift). On the L4, memory outside the allocator was 0.24 to 0.27 GB (the CUDA context) at every stage.