Skip to content

Add opt-in memory and batch efficiency reports - #18603

Merged
danielhanchen merged 4 commits into
mainfrom
cg7-diagnostics-report
Nov 20, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
cg7-diagnostics-report

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

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=1

Per 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:

  • model load (every from_pretrained return goes through _mark_requested_float32)
  • vLLM init (fast_inference = True, after load_vllm in llama.py and vision.py)
  • the first forward (a one-shot forward hook on the trainer's model)
  • the first optimizer step (a TrainerCallback)

torch.cuda.max_memory_reserved never 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: memory at first forward on cuda:0: 22.03 GB total, 21.12 GB free, torch reserved 0.68 GB (allocated 0.61 GB), outside torch's allocator 0.24 GB (CUDA context, NCCL, vLLM, other processes).

UNSLOTH_BATCH_REPORT=N (1 / true = 16)

Wraps the SFT trainer's training_step for 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 from attention_mask, else packed_seq_lengths, else every slot. It sits outside UNSLOTH_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 at on_train_end.

Unsloth: first 4 micro-batches (padded rows): 1,962 of 2,996 token slots hold real tokens (65.5%), 1,946 carry a loss (65.0%); 16 rows, widest 208 tokens.
Unsloth: first 4 micro-batches (padding-free): 1,962 of 1,962 token slots hold real tokens (100.0%), 1,946 carry a loss (99.2%); 4 rows, widest 550 tokens.

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):

  • token counts for padded rows, packed rows (packed_seq_lengths), and padding-free rows without a mask but with completion_mask
  • off by default: training_step is not wrapped, and the memory report never touches torch.cuda
  • the line prints exactly once after N micro-batches, every real training_step still runs, and a short run is flushed at train end
  • the memory report computes the outside-allocator figure, dedupes per stage, and does not query a device the process has not used
  • end to end on a tiny Llama with transformers.Trainer: the batch line has the exact expected counts, the forward hook fires once, and the step callback runs
  • wiring: the SFT init installs the batch report after install_token_budget, every TRL trainer gets the memory report, and both load points and both vLLM init points call report_memory

The new tests fail on the base commit (the module does not exist) and pass with the change. tests/test_mfu_logging.py and tests/test_token_budget_batches.py still 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:

Arm Report lines
reports off (padded rows) none
padded rows load, first forward, first optimizer step; batch line 65.5% real
padding-free the same three memory lines; batch line 100.0% real, 99.2% with a loss (packed boundary labels masked)
packing the same three memory lines; batch line 100.0% real

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.

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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 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-20T11:29:23.641105Z 1b899a5 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 chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread unsloth/utils/run_report.py Resolved
Comment thread unsloth/utils/run_report.py Resolved
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread unsloth/utils/run_report.py Resolved
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 1b899a549d

ℹ️ 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
danielhanchen merged commit 54af937 into main Nov 20, 2026
@danielhanchen
danielhanchen deleted the cg7-diagnostics-report branch November 20, 2026 13:24
danielhanchen added a commit that referenced this pull request Nov 20, 2026
…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.
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