Skip to content

CPU backend: UNSLOTH_MEMORY_REPORT reports host RAM - #18807

Merged
danielhanchen merged 3 commits into
mainfrom
cpu-memory-report
Nov 22, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
cpu-memory-report

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

UNSLOTH_MEMORY_REPORT=1 only looked at CUDA devices, so on the CPU backend it printed nothing. CPU training is mostly bounded by RAM, so the same stages now report the process RSS, its peak and the RAM still available:

Unsloth: memory at load on cpu: process RSS 1.07 GB (peak 1.10 GB), 1526.76 GB of 1996.01 GB RAM available.
Unsloth: memory at first forward on cpu: process RSS 1.14 GB (peak 1.26 GB), 1521.21 GB of 1996.01 GB RAM available.
Unsloth: memory at first optimizer step on cpu: process RSS 1.15 GB (peak 1.26 GB), 1521.14 GB of 1996.01 GB RAM available.

Changes

  • unsloth/utils/run_report.py: cpu_memory_snapshot() (Linux /proc/self/status VmRSS / VmHWM and /proc/meminfo MemAvailable, total capped by a cgroup v2 memory.max; psutil on macOS / Windows; ru_maxrss as the peak fallback). report_memory(..., cpu = True) and install_memory_report(trainer, cpu = True) print the host line. The GPU route calls are unchanged: still report_memory(stage, owner) with no new keyword.
  • unsloth/_cpu_init.py: reports at FastLanguageModel.from_pretrained and installs the first-forward / first-optimizer-step hooks on CPU SFTTrainer / UnslothTrainer init. run_report.py is loaded by file because unsloth.utils imports the GPU model stack. Off unless UNSLOTH_MEMORY_REPORT is set.

Tests

  • tests/test_run_report.py: the snapshot is sane (rss <= peak, available <= total), the cpu line prints once per (stage, owner), nothing about the host is printed without cpu = True, and _cpu_init.py wires it without importing unsloth.utils. The existing end-to-end test still sees the GPU route's two-argument calls.
  • tests/test_run_report.py: 16 passed (2 of them new, and those fail on main). The CPU test files (test_cpu_backend_init, test_cpu_backend_tiled_mlp, test_fused_head_cpu_dispatch, test_gc_every_n_cpu_route, test_cpu_route_context_parallel) give the same result as main: one failure that main has too, test_fused_head_cpu_dispatch.py::test_cpu_dispatch_keeps_trl_output_count.

Independent of the unsloth-zoo CPU PRs (threads, memory defaults, compile, packing, merged save): it needs no zoo change, and they need none from it.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 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-22T03:29:58.922948Z c49c63f 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

@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: 5b37832bd3

ℹ️ 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
…e over the cgroup and its ancestors

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: bd174cda53

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

# Conflicts:
#	tests/test_run_report.py

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: c49c63feee

ℹ️ 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 69efcea into main Nov 22, 2026
@danielhanchen
danielhanchen deleted the cpu-memory-report branch November 22, 2026 05:24
danielhanchen added a commit that referenced this pull request Nov 22, 2026
…18846)

## Summary

`Source lint` (Lint CI) and the risky loader test in `workflow-trigger
lint` are red on `main` since #18807, and on every PR based after it
(#18840, #18841):

```
1 risky loader call site(s) not in the baseline:
  unsloth/_cpu_init.py:54  [dynamic-import] spec_from_file_location
```

#18807 (`UNSLOTH_MEMORY_REPORT` reports host RAM on the CPU backend)
loads `unsloth/utils/run_report.py` with
`importlib.util.spec_from_file_location` instead of importing
`unsloth.utils`, because that package's `__init__` pulls in the GPU
model stack the CPU route skips. The path is fixed, relative to
`_cpu_init.py` inside the installed package, so nothing external chooses
what gets executed. The call is fine and only needs recording.

## Fix

`python scripts/lint_risky_loaders.py --update`, with the new entry's
reason filled in.

## Testing

- `python scripts/lint_risky_loaders.py`: all 124 call sites recorded
(exits 1 on `main`).
- `tests/security/test_lint_risky_loaders.py`: 46 passed.
danielhanchen added a commit that referenced this pull request Nov 22, 2026
…rd red on main) (#18850)

## Summary

`Repo tests (CPU, rest-2)` is red on `main` since #18807:

```
tests/test_source_read_encoding.py::test_checked_in_file_reads_name_an_encoding[0]
AssertionError: 1 file reads in the test trees touch a checked-in file with the platform default encoding,
so they break on Windows as soon as that file gains a non-ASCII byte. Pass encoding = "utf-8":
['tests/test_run_report.py:307: read_text()']
```

#18807 added `test_cpu_memory_report_wired_on_cpu_route`, which reads
`unsloth/_cpu_init.py` with a bare `read_text()`. The guard is right: on
Windows that decodes with the locale code page, so the test breaks as
soon as the file gains a non-ASCII character.

## Fix

`read_text(encoding = "utf-8")`.

## Testing

- `tests/test_source_read_encoding.py`: fails on `main` with the site
above, passes with this change.
- `tests/test_run_report.py::test_cpu_memory_report_wired_on_cpu_route`:
passes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant