Skip to content

[Bug] MLX memory estimator reports 69.7 GiB for a model that loads on a 24 GB Mac #15935

Closed

Description

@kpulik

Environment

Where are you using Unsloth?

  • Unsloth desktop application
  • Unsloth web UI (unsloth studio)
  • Unsloth CLI
  • Python package or notebook
  • Colab or Kaggle

Operating system and version:

macOS Golden Gate 27.2 (26B123)

GPU model(s) and accelerator backend:

  • Apple M4 Pro
  • 24 GB unified memory
  • Apple MLX backend

Versions:

  • Unsloth Desktop: 0.1.912-beta
  • Unsloth package, backend, and llama.cpp versions:
    • llama.cpp version b11963-mix-a54b8dd
    • whisper.cpp version v1.9.5-unsloth.14
    • audio.cpp version v0.9.3-unsloth.1
    • MLX version 0.32.4

What happened?

Unsloth Studio's MLX memory estimator reports an unexpectedly large memory requirement for a 2-bit MLX model, despite the model successfully loading and running on a Mac with substantially less physical memory.

For a model with 7.91 GiB of weights, Unsloth estimates 69.70 GiB of total memory, including 52.67 GiB of compute buffers.

Steps to reproduce:

  1. Open Unsloth Desktop 0.1.912-beta on an Apple Silicon Mac.
  2. Select prism-ml/Ternary-Bonsai-27B-mlx-2bit.
  3. Set context length to 262,144 tokens.
  4. Select 8-bit KV cache quantization.
  5. Expand the "Estimated Memory Usage" section.
  6. Observe the estimated 69.70 GiB memory requirement.
  7. Load the model. It successfully initializes and runs on a Mac with 24 GB unified memory.

Expected behavior:

The memory estimator should accurately distinguish between:

  • Memory needed to initialize the model.
  • KV cache memory for the configured context length.
  • Temporary compute-buffer allocations.
  • Worst-case peak memory during long-context inference.

If the estimate represents a conservative worst-case scenario rather than expected memory consumption, the UI should clearly communicate that distinction.

Actual behavior:

Unsloth reports the following memory breakdown:

Component Estimated memory
Model weights 7.91 GiB
KV cache 9.12 GiB
Compute buffers 52.67 GiB
Total 69.70 GiB

Screenshot of the memory estimate:

Unsloth Studio MLX memory estimate showing 69.70 GiB total, including 52.67 GiB of compute buffers

Despite this estimate, the model successfully loads and runs on a 24 GB Mac.

I have not tested an actual 262K-token prompt, so I cannot confirm whether the reported peak memory is inaccurate at maximum context length.

Model and operation involved:

  • Model: prism-ml/Ternary-Bonsai-27B-mlx-2bit
  • Operation: Native MLX inference
  • Model quantization: 2-bit
  • KV cache: 8-bit
  • Configured context: 262,144 tokens
  • Hardware: Apple M4 Pro, 24 GB unified memory

Diagnostics and logs

Desktop application

No diagnostics collected yet.

If needed, I can provide relevant backend logs from ~/.unsloth/studio/ after removing sensitive information.

Unsloth web UI

Not applicable. Using the desktop application.

Model-specific logs, if applicable

This involves native MLX inference, not GGUF/llama.cpp.

Python package or notebook, if applicable

Not applicable.

Additional context

Possible source of the discrepancy

The MLX memory estimator is implemented in:

studio/backend/core/inference/mlx_memory.py

The _compute_bytes() function contains a fallback involving _widest_quantized_scores() when _dequantized_row_bytes() returns None:

if row is None:
    total += _widest_quantized_scores(
        n_ctx, quant_boundary, prefill_chunk
    ) * score

At long context lengths, this calculation may estimate extremely large temporary attention-score buffers.

It is unclear whether this represents actual peak allocations or an overly conservative estimation path.

Suggested investigation:

  1. Verify whether the estimated 52.67 GiB compute-buffer requirement corresponds to actual runtime allocations.
  2. Check whether the quantized KV cache triggers a fallback estimate that does not match the selected attention kernel.
  3. Consider displaying expected memory usage separately from worst-case peak usage.
  4. Investigate whether the estimation or underlying MLX inference path can be optimized for long contexts on Apple Silicon.

Relevant source:

https://github.com/unslothai/unsloth/blob/main/studio/backend/core/inference/mlx_memory.py

Activity

  1. added
    feature request
    on Oct 29, 2026
  2. self-assigned this
    on Oct 29, 2026
  3. debojyoti-dev commented on Oct 29, 2026

    @debojyoti-dev
    Contributor

    Hi @Lyxot, I see this is assigned to you. Would it be OK if I open a PR for the estimator side of it? I traced it down:

    • Since unsloth-zoo's quantized-attention routing started reading the cache length (fused_kernel_exists returns False for a causal call with more queries than keys), _dequantized_row_bytes in mlx_memory.py always gets None, because it probes with a one-token cache against a 2048-token prefill chunk. So every quantized-KV MLX estimate falls back to the full [heads, chunk, n_ctx] score matrix, even for models where zoo would really take the dequantized-copy route (head_dim 64-128 everywhere, and head_dim 256/512 on Macs with neural accelerators). For a Qwen2.5-7B-like model at 262K with 8-bit KV, that's ~64 GiB charged instead of ~1.5 GiB.
    • For this exact case (Ternary-Bonsai-27B is a Qwen3.5 model with head_dim 256, on an M4 Pro), mlx really does build the score matrix for every chunk after the first, so the ~48 GiB term is the genuine peak of the last 2048-token prefill chunk of a full 262K prompt. The load works because nothing that big is allocated until the prompt gets that long. On M5 Macs the fix brings it down to ~1.5 GiB.

    The fix is to probe the route with a cache one token longer than the chunk, like every chunk after the first sees. I'll hold off if you'd rather handle it yourself.

  4. danielhanchen commented on Oct 30, 2026

    @danielhanchen
    Member

    Fixed! The Mac memory estimate was charging a huge attention buffer that MLX no longer builds when the KV cache is quantized, and it missed the one it really does build with the default cache. Fixed in #15970 (quantized KV cache) and #16021 (default cache). At a full 262K context this model on an M1 to M4 Mac still needs about 31 GiB of compute buffers, because MLX really allocates that for a prompt that long, so the context Studio picks for a 24 GB Mac will be shorter.
    To update: Unsloth Desktop will have an update banner. For Studio, use unsloth studio update. For Docker, use docker exec <container> unsloth-studio-update. Reopen if it still happens.

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

Metadata

Metadata

Labels

bugfeature request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions