Skip to content

fix(core): free GPU memory when stopping pytorch rerank models - #5173

Merged
qinxuye merged 1 commit into
xorbitsai:mainfrom
OliverBryant:fix/rerank-vram-leak-on-stop
Jul 14, 2026
Merged

fix(core): free GPU memory when stopping pytorch rerank models#5173
qinxuye merged 1 commit into
xorbitsai:mainfrom
OliverBryant:fix/rerank-vram-leak-on-stop

Conversation

@OliverBryant

Copy link
Copy Markdown
Collaborator

Summary

  • RerankModel has no stop()/close(), and ModelActor.__pre_destroy__'s GPU-cleanup branch only covered LLM pytorch/vllm/sglang and EmbeddingModel, so stopping a pytorch-format rerank model (e.g. sentence_transformers) never released its CUDA tensors/VRAM.
  • The leaked VRAM/CUDA context then crashed the next model's subpool during CUDA init on relaunch, surfacing as xoscar.errors.ServerClosed: ... 0 bytes read at create_actor — reproducing the second-launch failure of Qwen3-Reranker-0.6B.
  • Extended __pre_destroy__ to also run the existing del self._model; gc.collect(); empty_cache() cleanup for pytorch-format RerankModel instances, guarded by model_format == "pytorch" so the torch-free llama.cpp (ggufv2) rerank path is unaffected.

Test plan

  • Added test_pre_destroy_frees_rerank_gpu_memory in xinference/core/tests/test_model.py, parametrized over pytorch (expects cleanup) and ggufv2 (expects skip).
  • pytest xinference/core/tests/test_model.py — 3 passed
  • pre-commit run --files xinference/core/model.py xinference/core/tests/test_model.py — black/isort/mypy/codespell pass (one pre-existing flake8 E231 false positive on an unrelated line is unchanged)

@XprobeBot XprobeBot added bug Something isn't working gpu labels Jul 13, 2026
@XprobeBot XprobeBot added this to the v2.x milestone Jul 13, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces GPU memory cleanup for PyTorch-backed rerank models during actor teardown in __pre_destroy__ and adds corresponding unit tests to verify this behavior. The reviewer suggests extending the model_format == "pytorch" guard to EmbeddingModel as well, which prevents torch-free GGUF embedding models from triggering torch-dependent cleanup blocks.

Comment thread xinference/core/model.py
@OliverBryant
OliverBryant force-pushed the fix/rerank-vram-leak-on-stop branch 2 times, most recently from 4e871e2 to 73742aa Compare July 13, 2026 07:10
RerankModel had no stop()/close(), and __pre_destroy__'s GPU-cleanup
branch only covered LLM pytorch/vllm/sglang and EmbeddingModel, so
stopping a sentence_transformers rerank model left its CUDA tensors
and VRAM allocated. The leaked memory then crashed the next model's
subpool during CUDA init on relaunch.

Also guard the EmbeddingModel branch with model_format == "pytorch"
(per review feedback) so the torch-free llama.cpp (ggufv2) embedding
path isn't pulled into the torch-dependent cleanup block.
@OliverBryant
OliverBryant force-pushed the fix/rerank-vram-leak-on-stop branch from 73742aa to 5bf977b Compare July 13, 2026 07:14

@qinxuye qinxuye left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@qinxuye
qinxuye merged commit 25be29c into xorbitsai:main Jul 14, 2026
23 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working gpu

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants