-
-
Notifications
You must be signed in to change notification settings - Fork 714
fix(server): bound resident model memory with LRU eviction and idle TTL sweeper #913
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
Open
rkfshakti
wants to merge
2
commits into
Blaizzy:main
Choose a base branch
from
rkfshakti:fix/model-provider-idle-eviction
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+274
−12
Open
Changes from all commits
Commits
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
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import asyncio | ||
| import functools | ||
| import io | ||
| import json | ||
|
|
@@ -945,4 +946,132 @@ async def test_stream_inference_results_reraises_error_by_default(): | |
| pass | ||
|
|
||
| assert exc_info.value is error | ||
| assert handle.cancelled | ||
|
|
||
|
|
||
| # -- ModelProvider memory eviction ------------------------------------------ | ||
|
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. universally, we do small weights for testing. these tests don't really reflect that your change fixes the leak since this is not even calling nn.Module I'd say tests can be improved as well |
||
|
|
||
|
|
||
| def _make_provider(max_resident=1, idle_ttl=0.0): | ||
| from mlx_audio.server import ModelProvider | ||
|
|
||
| return ModelProvider(max_resident_models=max_resident, idle_ttl_seconds=idle_ttl) | ||
|
|
||
|
|
||
| def test_model_provider_lru_evicts_least_recently_used(monkeypatch): | ||
| loaded = [] | ||
| monkeypatch.setattr( | ||
| "mlx_audio.server.load_model", | ||
| lambda name: loaded.append(name) or f"model-{name}", | ||
| ) | ||
| provider = _make_provider(max_resident=2) | ||
|
|
||
| provider.load_model("a") | ||
| provider.load_model("b") | ||
| provider.load_model("a") # a becomes the most recently used | ||
| provider.load_model("c") # evicts b (LRU), keeps a | ||
|
|
||
| assert set(provider.models) == {"a", "c"} | ||
| assert set(provider._last_used.keys()) == {"a", "c"} | ||
|
|
||
|
|
||
| def test_model_provider_evicts_when_capacity_exceeded(monkeypatch): | ||
| loaded = [] | ||
| monkeypatch.setattr( | ||
| "mlx_audio.server.load_model", | ||
| lambda name: loaded.append(name) or f"model-{name}", | ||
| ) | ||
| provider = _make_provider(max_resident=1) | ||
|
|
||
| provider.load_model("a") | ||
| provider.load_model("b") | ||
|
|
||
| assert list(provider.models.keys()) == ["b"] | ||
| assert loaded == ["a", "b"] | ||
|
|
||
|
|
||
| async def test_model_provider_remove_model_clears_memory(monkeypatch): | ||
| cleared = [] | ||
| monkeypatch.setattr("mlx_audio.server.load_model", lambda name: f"model-{name}") | ||
| monkeypatch.setattr("mlx_audio.server.mx.clear_cache", lambda: cleared.append(1)) | ||
| provider = _make_provider() | ||
|
|
||
| provider.load_model("a") | ||
| assert "a" in provider.models | ||
|
|
||
| removed = await provider.remove_model("a") | ||
|
|
||
| assert removed is True | ||
| assert provider.models == {} | ||
| assert cleared == [1] | ||
| assert "a" not in provider._last_used | ||
|
|
||
|
|
||
| def test_model_provider_evict_collects_garbage_before_clearing_cache(monkeypatch): | ||
| order = [] | ||
| monkeypatch.setattr( | ||
| "mlx_audio.server.load_model", lambda name: f"model-{name}" | ||
| ) | ||
| monkeypatch.setattr("mlx_audio.server.gc.collect", lambda: order.append("gc.collect")) | ||
| monkeypatch.setattr( | ||
| "mlx_audio.server.mx.clear_cache", lambda: order.append("mx.clear_cache") | ||
| ) | ||
| provider = _make_provider() | ||
|
|
||
| provider.load_model("a") | ||
| provider.load_model("b") | ||
|
|
||
| assert order == ["gc.collect", "mx.clear_cache"] | ||
|
|
||
|
|
||
| async def test_model_provider_remove_model_missing_returns_false(): | ||
| provider = _make_provider() | ||
| assert await provider.remove_model("missing") is False | ||
|
|
||
|
|
||
| async def test_model_provider_idle_sweeper_evicts_stale_models(monkeypatch): | ||
| loaded = [] | ||
| monkeypatch.setattr( | ||
| "mlx_audio.server.load_model", | ||
| lambda name: loaded.append(name) or f"model-{name}", | ||
| ) | ||
| clock = {"now": 0.0} | ||
| monkeypatch.setattr("mlx_audio.server.time.monotonic", lambda: clock["now"]) | ||
| provider = _make_provider(idle_ttl=0.05) | ||
| provider.start_sweeper() | ||
| try: | ||
| provider.load_model("a") | ||
| clock["now"] = 0.06 | ||
| stale = provider.evict_stale(clock["now"]) | ||
| assert stale == ["a"] | ||
| assert "a" not in provider.models | ||
|
|
||
| provider.load_model("b") | ||
| clock["now"] = 0.10 # b is only 0.04s old — still fresh | ||
| assert provider.evict_stale(clock["now"]) == [] | ||
| assert "b" in provider.models | ||
|
|
||
| clock["now"] = 0.20 # b is now 0.14s old — stale | ||
| assert provider.evict_stale(clock["now"]) == ["b"] | ||
| assert "b" not in provider.models | ||
| finally: | ||
| provider.stop_sweeper() | ||
|
|
||
|
|
||
| def test_model_provider_sweeper_is_noop_when_ttl_disabled(monkeypatch): | ||
| provider = _make_provider(idle_ttl=0.0) | ||
| provider.start_sweeper() | ||
| assert provider._sweeper_task is None | ||
| provider.stop_sweeper() | ||
|
|
||
|
|
||
| def test_model_provider_zero_max_resident_is_unbounded(monkeypatch): | ||
| monkeypatch.setattr( | ||
| "mlx_audio.server.load_model", lambda name: f"model-{name}" | ||
| ) | ||
| provider = _make_provider(max_resident=0) | ||
| assert provider.max_resident_models == 0 | ||
|
|
||
| for i in range(5): | ||
| provider.load_model(f"m{i}") | ||
|
|
||
| assert len(provider.models) == 5 | ||
Oops, something went wrong.
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.
i think dropping the reference does not free the weights, since nn.Module has reference cycles