Skip to content

feat: add memory::set_memory_limit and memory::get_memory_limit - #346

Open
dschulmeist wants to merge 1 commit into
oxiglade:mainfrom
dschulmeist:feat/memory-limit
Open

feat: add memory::set_memory_limit and memory::get_memory_limit#346
dschulmeist wants to merge 1 commit into
oxiglade:mainfrom
dschulmeist:feat/memory-limit

Conversation

@dschulmeist

Copy link
Copy Markdown

Closes #225.

Changes

Adds a new memory module under mlx-rs/src/memory.rs exposing:

  • memory::get_memory_limit() -> Result<usize>
  • memory::set_memory_limit(limit: usize) -> Result<usize> — returns the previous limit so callers can save and restore it.

The module is wired in via pub mod memory; in lib.rs. The error-handling pattern mirrors utils/io.rs (INIT_ERR_HANDLER + get_and_clear_last_mlx_error).

This gives mlx-rs parity with mlx-swift's Memory.memoryLimit getter/setter (referenced by mlx-swift#223).

A note on the issue body

The issue references mlx_metal_set_memory_limit, but that symbol no longer exists upstream — the memory limit functions were moved out of the metal namespace and the current mlx-c API is mlx_set_memory_limit / mlx_get_memory_limit. This PR binds those.

Scoped

Just the memory limit, matching the issue title. The other functions in mlx/c/memory.h (mlx_get_active_memory, mlx_get_cache_memory, mlx_get_peak_memory, mlx_set_cache_limit, mlx_clear_cache, etc.) are out of scope here and a good fit for a follow-up.

Testing

cargo test -p mlx-rs --lib memory:: — one combined test asserting:

  • Initial get returns the upstream default (1.5x recommended max working set on Apple Silicon).
  • set(a) then get() returns a.
  • Consecutive set(b) returns a (the previous limit contract).
  • Final get() reflects b, then restore.

The two contracts (round-trip and previous-value) live in one test on purpose: memory limit is global state on the active device, so a parallel test runner could otherwise interleave writes between two separate tests.

…lade#225)

Adds a memory module wrapping mlx-c's memory limit get/set APIs,
providing parity with mlx-swift's Memory.memoryLimit. Both functions
return Result<usize>; set_memory_limit returns the previous limit so
callers can restore it.

The issue references mlx_metal_set_memory_limit, but upstream MLX has
since moved the limit functions out of the metal namespace; this binds
mlx_set_memory_limit / mlx_get_memory_limit instead.
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.

feat: Set and get GPU memory limit

1 participant