-
Notifications
You must be signed in to change notification settings - Fork 9
Expose MLX memory management APIs #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
polvalente
merged 4 commits into
elixir-nx:main
from
dannote:add-memory-management-apis
Feb 22, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
66c1287
Expose MLX memory management APIs
dannote f025210
Add doc examples, specs for void returns, stricter test assertions
dannote af02406
Apply suggestions from code review
polvalente 4321553
Merge branch 'main' into add-memory-management-apis
polvalente File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -339,4 +339,75 @@ defmodule EMLX do | |
| device = Keyword.get(opts, :device, :gpu) | ||
| {EMLX.Backend, device: device} | ||
| end | ||
|
|
||
| @doc """ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These docs could use some examples,.even if they aren't doctests per se |
||
| Returns a map with current memory usage information. | ||
|
|
||
| Keys: | ||
| * `:active_memory` - bytes currently allocated and in use | ||
| * `:peak_memory` - highest active memory since last reset | ||
| * `:cache_memory` - bytes in the allocator cache (freed but not returned to OS) | ||
|
|
||
| ## Examples | ||
|
|
||
| iex> info = EMLX.memory_info() | ||
| iex> is_integer(info.active_memory) and is_integer(info.peak_memory) and is_integer(info.cache_memory) | ||
| true | ||
| """ | ||
| def memory_info, do: EMLX.NIF.memory_info() |> unwrap!() | ||
|
|
||
| @doc """ | ||
| Clears the MLX memory cache, releasing unused GPU memory back to the system. | ||
|
|
||
| Useful after inference batches to prevent memory growth. Does not affect | ||
| tensors that are still referenced. | ||
|
|
||
| ## Examples | ||
|
|
||
| EMLX.clear_cache() | ||
| #=> :ok | ||
| """ | ||
| @spec clear_cache() :: :ok | ||
| def clear_cache, do: EMLX.NIF.clear_cache() |> unwrap!() | ||
|
|
||
| @doc """ | ||
| Resets the peak memory counter to zero. | ||
|
|
||
| ## Examples | ||
|
|
||
| EMLX.reset_peak_memory() | ||
| #=> :ok | ||
| """ | ||
| @spec reset_peak_memory() :: :ok | ||
| def reset_peak_memory, do: EMLX.NIF.reset_peak_memory() |> unwrap!() | ||
|
|
||
| @doc """ | ||
| Sets the memory limit in bytes. Returns the previous limit. | ||
|
|
||
| The memory limit is a guideline for maximum memory usage during graph | ||
| evaluation. Defaults to 1.5× the device's recommended working set size. | ||
|
|
||
| ## Examples | ||
|
|
||
| prev = EMLX.set_memory_limit(8_000_000_000) | ||
| EMLX.set_memory_limit(prev) | ||
| """ | ||
| def set_memory_limit(limit) when is_integer(limit) and limit >= 0 do | ||
| EMLX.NIF.set_memory_limit(limit) |> unwrap!() | ||
| end | ||
|
|
||
| @doc """ | ||
| Sets the cache limit in bytes. Returns the previous limit. | ||
|
|
||
| When cached memory exceeds this limit, it will be reclaimed on the next | ||
| allocation. Set to 0 to disable caching entirely. | ||
|
|
||
| ## Examples | ||
|
|
||
| prev = EMLX.set_cache_limit(500_000_000) | ||
| EMLX.set_cache_limit(prev) | ||
| """ | ||
| def set_cache_limit(limit) when is_integer(limit) and limit >= 0 do | ||
| EMLX.NIF.set_cache_limit(limit) |> unwrap!() | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| defmodule EMLX.MemoryTest do | ||
| use ExUnit.Case, async: false | ||
|
|
||
| test "memory_info returns active, peak, and cache" do | ||
| info = EMLX.memory_info() | ||
| assert is_map(info) | ||
| assert is_integer(info.active_memory) | ||
| assert is_integer(info.peak_memory) | ||
| assert is_integer(info.cache_memory) | ||
| assert info.active_memory >= 0 | ||
| assert info.peak_memory >= 0 | ||
| assert info.cache_memory >= 0 | ||
| end | ||
|
|
||
| test "allocating a tensor increases active memory" do | ||
| EMLX.clear_cache() | ||
| before = EMLX.memory_info().active_memory | ||
| t = Nx.iota({1024, 1024}, type: :f32, backend: EMLX.Backend) | ||
| EMLX.eval(EMLX.Backend.from_nx(t)) | ||
| after_alloc = EMLX.memory_info().active_memory | ||
| assert after_alloc >= before + 1024 * 1024 * 4 | ||
| end | ||
|
|
||
| test "clear_cache releases unused memory" do | ||
| t = Nx.iota({1024, 1024}, type: :f32, backend: EMLX.Backend) | ||
| EMLX.eval(EMLX.Backend.from_nx(t)) | ||
| Nx.backend_deallocate(t) | ||
| EMLX.clear_cache() | ||
| info = EMLX.memory_info() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have assertions on what info returns? |
||
| assert is_integer(info.active_memory) | ||
| assert is_integer(info.peak_memory) | ||
| assert info.cache_memory == 0 | ||
| end | ||
|
|
||
| test "reset_peak_memory resets the counter" do | ||
| _t = Nx.iota({1024, 1024}, type: :f32, backend: EMLX.Backend) | ||
| EMLX.reset_peak_memory() | ||
| assert EMLX.memory_info().peak_memory == 0 | ||
| end | ||
|
|
||
| test "set_memory_limit returns previous limit" do | ||
| prev = EMLX.set_memory_limit(1_000_000_000) | ||
| assert is_integer(prev) | ||
| EMLX.set_memory_limit(prev) | ||
| end | ||
|
|
||
| test "set_cache_limit returns previous limit" do | ||
| prev = EMLX.set_cache_limit(500_000_000) | ||
| assert is_integer(prev) | ||
| EMLX.set_cache_limit(prev) | ||
| end | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do these return void?