Skip to content
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

[Engine] Fix the self-ref in engine #2367

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions python/mlc_llm/json_ffi/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,14 @@ def __init__( # pylint: disable=too-many-arguments,too-many-locals
}
self.tokenizer = Tokenizer(model_args[0][0])

def _background_loop():
self._ffi["run_background_loop"]()

def _background_stream_back_loop():
self._ffi["run_background_stream_back_loop"]()
# important: avoid self reference in closure
background_loop = self._ffi["run_background_loop"]
background_stream_back_loop = self._ffi["run_background_stream_back_loop"]

# Create the background engine-driving thread and start the loop.
self._background_loop_thread: threading.Thread = threading.Thread(target=_background_loop)
self._background_loop_thread: threading.Thread = threading.Thread(target=background_loop)
self._background_stream_back_loop_thread: threading.Thread = threading.Thread(
target=_background_stream_back_loop
target=background_stream_back_loop
)
self._background_loop_thread.start()
self._background_stream_back_loop_thread.start()
Expand Down Expand Up @@ -129,7 +127,12 @@ def _background_stream_back_loop():
)
self._ffi["reload"](self.engine_config.asjson())

def __del__(self):
self.terminate()

def terminate(self):
if self._terminated:
return
self._terminated = True
self._ffi["exit_background_loop"]()
self._background_loop_thread.join()
Expand All @@ -139,7 +142,7 @@ def chat_completion( # pylint: disable=too-many-arguments,too-many-locals
self,
*,
messages: List[Dict[str, Any]],
model: str,
model: str = None,
frequency_penalty: Optional[float] = None,
presence_penalty: Optional[float] = None,
logprobs: bool = False,
Expand Down
17 changes: 10 additions & 7 deletions python/mlc_llm/serve/engine_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,16 +479,13 @@ def __init__( # pylint: disable=too-many-arguments,too-many-locals
self.state.trace_recorder,
)

def _background_loop():
self._ffi["run_background_loop"]()

def _background_stream_back_loop():
self._ffi["run_background_stream_back_loop"]()
background_loop = self._ffi["run_background_loop"]
background_stream_back_loop = self._ffi["run_background_stream_back_loop"]

# - Create the background engine-driving thread and start the loop.
self._background_loop_thread: threading.Thread = threading.Thread(target=_background_loop)
self._background_loop_thread: threading.Thread = threading.Thread(target=background_loop)
self._background_stream_back_loop_thread: threading.Thread = threading.Thread(
target=_background_stream_back_loop
target=background_stream_back_loop
)
self._background_loop_thread.start()
self._background_stream_back_loop_thread.start()
Expand Down Expand Up @@ -519,8 +516,14 @@ def _background_stream_back_loop():
self.engine_config.max_total_sequence_length,
)

def __del__(self):
"""deleter, auto terminate"""
self.terminate()

def terminate(self):
"""Terminate the engine."""
if self._terminated:
return
self._terminated = True
self._ffi["exit_background_loop"]()
self._background_loop_thread.join()
Expand Down
Loading