Skip to content

[V1][Structured Output] Clear xgrammar compiler object when engine core shut down to avoid nanobind leaked warning #16954

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
merged 2 commits into from
Apr 24, 2025
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
1 change: 1 addition & 0 deletions vllm/v1/engine/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ def step_with_batch_queue(self) -> Optional[EngineCoreOutputs]:
return engine_core_outputs

def shutdown(self):
self.structured_output_manager.clear_backend()
if self.model_executor:
self.model_executor.shutdown()

Expand Down
4 changes: 4 additions & 0 deletions vllm/v1/structured_output/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,7 @@ def grammar_bitmask(
# np.ndarray, because that is much more efficient for serialization
# and deserialization when sending this to the GPU workers.
return bitmask_tensor.numpy()

def clear_backend(self) -> None:
if self.backend is not None:
self.backend.destroy()
3 changes: 3 additions & 0 deletions vllm/v1/structured_output/backend_guidance.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ def allocate_token_bitmask(self, max_num_seqs: int):
return llguidance_torch.allocate_token_bitmask(
max_num_seqs, self.ll_tokenizer.vocab_size)

def destroy(self):
pass


@dataclass
class GuidanceGrammar(StructuredOutputGrammar):
Expand Down
6 changes: 6 additions & 0 deletions vllm/v1/structured_output/backend_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,9 @@ def allocate_token_bitmask(self, max_num_seqs: int):
max_num_seqs (int): The maximum number of sequences for which
to allocate the bitmask.
"""

@abstractmethod
def destroy(self):
"""
Backend-specific cleanup.
"""
3 changes: 3 additions & 0 deletions vllm/v1/structured_output/backend_xgrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ def compile_grammar(self, request_type: StructuredOutputOptions,
def allocate_token_bitmask(self, max_num_seqs: int):
return xgr.allocate_token_bitmask(max_num_seqs, self.vocab_size)

def destroy(self):
Copy link
Collaborator

Choose a reason for hiding this comment

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

should probably use __del__ protocol instead.

https://docs.python.org/3/reference/datamodel.html#object.__del__

del self.compiler


@dataclass
class XgrammarGrammar(StructuredOutputGrammar):
Expand Down