Skip to content

Commit 8184805

Browse files
committed
feat(inference): 新增 MPS MLX 推理缓存清理配置选项
- 【参数】注册 mps_mlx_clear_cache 到推理参数及透传列表 - 【校验】允许非 VR 模型类型使用该参数 - 【优化】新增上下文管理器以在评估后清理 MLX 缓存
1 parent d103f3a commit 8184805

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

pymss/separator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"cuda_attention_backend": "inference",
3636
"mps_attention_backend": "inference",
3737
"mps_mlx_min_tokens": "inference",
38+
"mps_mlx_clear_cache": "inference",
3839
"mps_model_backend": "inference",
3940
"mps_model_compute_dtype": "inference",
4041
"fuse_conv_bn": "inference",
@@ -55,6 +56,7 @@
5556
"use_amp",
5657
"cuda_attention_backend",
5758
"mps_attention_backend",
59+
"mps_mlx_clear_cache",
5860
"mps_model_backend",
5961
"mps_model_compute_dtype",
6062
"fuse_conv_bn",

pymss/server/state.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ def _is_parameter_supported(config, model_type, key, section_name):
183183
bool: True when the condition is satisfied."""
184184
if model_type == "vr" and key in VR_SUPPORTED_PARAMETERS:
185185
return True
186+
if key == "mps_mlx_clear_cache" and model_type != "vr":
187+
return True
186188
# standardize is legacy input standardization backed by MSS YAML inference.normalize.
187189
# normalize is output peak normalization owned by runtime inference params.
188190
config_key = "normalize" if key == "standardize" else key

pymss/utils.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,29 @@ def _mlx_fit_length(x, length):
792792
return x
793793

794794

795-
def _mlx_run_model_chunk(model, arr, chunk_size, progress_fraction_callback=None):
795+
@contextmanager
796+
def _mlx_clear_cache_after_eval(enabled=False):
797+
"""Clear MLX allocator cache after explicit eval points when requested."""
798+
if not enabled:
799+
yield
800+
return
801+
import mlx.core as mx
802+
803+
original_eval = mx.eval
804+
805+
def eval_and_clear(*args, **kwargs):
806+
result = original_eval(*args, **kwargs)
807+
clear_mlx_cache()
808+
return result
809+
810+
mx.eval = eval_and_clear
811+
try:
812+
yield
813+
finally:
814+
mx.eval = original_eval
815+
816+
817+
def _mlx_run_model_chunk(model, arr, chunk_size, progress_fraction_callback=None, clear_cache_after_eval=False):
796818
"""Implement the mlx run model chunk helper.
797819
798820
Args:
@@ -802,7 +824,7 @@ def _mlx_run_model_chunk(model, arr, chunk_size, progress_fraction_callback=None
802824
803825
Returns:
804826
Any: Computed result."""
805-
with _model_progress_fraction_context(model, progress_fraction_callback):
827+
with _mlx_clear_cache_after_eval(clear_cache_after_eval), _model_progress_fraction_context(model, progress_fraction_callback):
806828
y = model.mlx_forward_mx(arr)
807829
if y.ndim == arr.ndim:
808830
y = y[:, None]
@@ -926,6 +948,7 @@ def demix_track_mlx_full(config, model, mix, device, pbar=False, source_indices=
926948
mx.stack([chunk for (chunk, _), _ in batch], axis=0),
927949
C,
928950
lambda fraction: progress.emit(batch_done_before + round(batch_units * fraction)),
951+
clear_cache_after_eval=bool(config.inference.get("mps_mlx_clear_cache", False)),
929952
)
930953
chunks = _mlx_select_sources(chunks, source_indices)
931954
for j, ((_, length), idx) in enumerate(batch):

0 commit comments

Comments
 (0)