Skip to content

fix(optim): fall back to non-paged state on all devices without managed memory - #2069

Open
egeozkoc wants to merge 3 commits into
bitsandbytes-foundation:mainfrom
egeozkoc:fix/paged-optimizer-non-cuda
Open

egeozkoc wants to merge 3 commits into
bitsandbytes-foundation:mainfrom
egeozkoc:fix/paged-optimizer-non-cuda

Conversation

@egeozkoc

@egeozkoc egeozkoc commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Paged optimizers on MPS could reach cget_managed_ptr and fail with a CPU-library error instead of allocating regular state buffers. AdEMAMix's separate double-buffer allocator also bypassed the fallback on both CPU and MPS for parameters with at least 100,000 elements.

Both allocation paths now use regular tensors on devices outside CUDA/ROCm/XPU, with the existing once-per-optimizer warning naming the device. CUDA/ROCm/XPU keep the managed-memory path. The existing test_optimizer32bit paged-case skip is widened to non-paging devices; the extra regression test was removed as requested.

Validation on Apple M5/macOS 26.0, Python 3.12.13, both PyTorch 2.12.1 and 2.15.0, with this patch applied to current main (a616886):

  • Existing optimizer selection: 72 passed, 72 skipped on each of CPU and MPS (108 deselected, including benchmarks).
  • Separate local integration checks: 48 state-allocation cases covering all 12 paged classes, small/large buffers, FP32/uint8 state and once-only warnings; 24 optimizer-step cases comparing PagedAdam/AdamW/Lion/AdEMAMix to their non-paged counterparts across FP32/FP16/BF16 on CPU/MPS. All pass with exact parameter/state parity. The 8-bit checks validate allocation only, not MPS 8-bit optimizer updates.
  • Before the AdEMAMix follow-up, four large-buffer allocation cases failed (PagedAdEMAMix and PagedAdEMAMix8bit on CPU/MPS); all 48 allocation cases now also pass on the exact PR head.
  • Applicable pre-commit hooks and git diff --check pass. The local validation scripts are kept outside the PR test suite.

CUDA/ROCm/XPU hardware was unavailable locally. Upstream CI remains required: the previous PR Tests run expired while awaiting maintainer approval. This change does not implement MPS 8-bit optimizers.

Ref #2011.

🤖 Generated with Claude Code; follow-up fix and validation assisted by Codex.

…ed memory

Paged optimizers only degraded gracefully on CPU. On MPS (and any other
device whose native library lacks cget_managed_ptr) they reached
F.get_paged and failed with "Method 'cget_managed_ptr' not available in
CPU-only version of bitsandbytes".

Generalize the fallback in Optimizer8bit.get_state_buffer to every device
type other than cuda/xpu, which are the two with managed-memory support,
and name the device in the one-time warning.

Tests: the paged_* skip in test_optimizer32bit now covers all non-paging
devices, and a new test checks the fallback warns once and keeps a regular
state tensor.

Ref bitsandbytes-foundation#2011

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@matthewdouglas matthewdouglas added Cross Platform Optimizers Issues or feature requests relating to optimizers labels Sep 3, 2026
@matthewdouglas

Copy link
Copy Markdown
Member

Thanks for the PR. Given that paged optimizers are only actually supported on CUDA/ROCm/XPU, it seems superfluous. But considering we'd later implement optimizers for MPS or fallback, I suppose it's reasonable in that respect.

With that said, the extra test for regression against previous behavior, literally saying "previously" in the comments, IMHO seems just unnecessary. I do not understand why the agents love adding these pointless tests so much, but please remove that.

@matthewdouglas matthewdouglas added the Low Priority (will be worked on after all priority issues) label Sep 4, 2026
@matthewdouglas matthewdouglas added this to the v0.50.3 milestone Sep 4, 2026
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Per review feedback on bitsandbytes-foundation#2069.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@egeozkoc

egeozkoc commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

Removed the test, and dropped a now-stale reference to it from the skip message. The remaining test change is just widening the existing paged_* skip in test_optimizer32bit from CPU to any non-paging device.

@egeozkoc

egeozkoc commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

@matthewdouglas, I’ve pushed 80a30b6 to cover a missed allocation path: large PagedAdEMAMix/PagedAdEMAMix8bit double buffers still called cget_managed_ptr on CPU/MPS. The helper now uses the same device allow-list as the base allocator. The removed regression test remains removed.

Refreshed validation on Apple M5/macOS 26.0, PyTorch 2.12.1 and 2.15.0, with the patch applied to current main (a616886):

  • 72 existing tests passed on each of CPU and MPS, with 72 expected skips per selection.
  • 72 separate local allocation/update checks passed per version: 48 state-allocation cases and 24 paged-versus-non-paged step comparisons. All 48 allocation cases also pass on the exact PR head; four AdEMAMix cases failed before this fix.
  • Applicable pre-commit hooks pass. These validation scripts are outside the PR test suite.

CUDA/ROCm/XPU hardware was unavailable locally; MPS 8-bit checks cover allocation only.

The fresh PR Tests, Lint, Python package, and documentation workflows are awaiting approval. Could you approve those runs and review the updated PR? The description now includes the fix and validation details.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Cross Platform Low Priority (will be worked on after all priority issues) Optimizers Issues or feature requests relating to optimizers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants