-
Notifications
You must be signed in to change notification settings - Fork 196
feat/1001: sglang integration #1122
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
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5ecedc4
support sglang
Jayon02 ddde5cb
support structure output and SGLang openai client
Jayon02 f893c6d
modify function annotation
Jayon02 ba3a5a7
fix bugs in multiple generations and add test
Jayon02 f2b2064
fix test bugs
Jayon02 159f1ef
support sglang embedding
Jayon02 6c9059f
add detokenize
Jayon02 8d68ed9
Squashed commit of the following:
Jayon02 5a15126
Update sglang.py
Jayon02 e6e9be7
fix bug in embedding
Jayon02 c3740a9
update sglang.py
Jayon02 83984d3
Merge main into feature_branch
Jayon02 503b3d3
fix bug in test
Jayon02 fd946d7
modify toml and reformat
Jayon02 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
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 |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# Copyright 2023-present, Argilla, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union | ||
|
||
from pydantic import Field, PrivateAttr | ||
|
||
from distilabel.mixins.runtime_parameters import RuntimeParameter | ||
from distilabel.models.embeddings.base import Embeddings | ||
from distilabel.models.mixins.cuda_device_placement import CudaDevicePlacementMixin | ||
|
||
if TYPE_CHECKING: | ||
from sglang import Engine as _SGLang | ||
|
||
|
||
class SGLangEmbeddings(Embeddings, CudaDevicePlacementMixin): | ||
"""`sglang` library implementation for embedding generation. | ||
|
||
Attributes: | ||
model: the model Hugging Face Hub repo id or a path to a directory containing the | ||
model weights and configuration files. | ||
dtype: the data type to use for the model. Defaults to `auto`. | ||
trust_remote_code: whether to trust the remote code when loading the model. Defaults | ||
to `False`. | ||
quantization: the quantization mode to use for the model. Defaults to `None`. | ||
revision: the revision of the model to load. Defaults to `None`. | ||
seed: the seed to use for the random number generator. Defaults to `0`. | ||
extra_kwargs: additional dictionary of keyword arguments that will be passed to the | ||
`Engine` class of `sglang` library. Defaults to `{}`. | ||
_model: the `SGLang` model instance. This attribute is meant to be used internally | ||
and should not be accessed directly. It will be set in the `load` method. | ||
|
||
References: | ||
- https://github.com/sgl-project/sglang/blob/main/python/sglang/srt/server_args.py | ||
|
||
Examples: | ||
Generating sentence embeddings: | ||
|
||
```python | ||
if __name__ == "__main__": | ||
|
||
from distilabel.models import SGLangEmbeddings | ||
embeddings = SGLangEmbeddings(model="intfloat/e5-mistral-7b-instruct") | ||
embeddings.load() | ||
results = embeddings.encode(inputs=["distilabel is awesome!", "and Argilla!"]) | ||
print(results) | ||
# [ | ||
# [0.0203704833984375, -0.0060882568359375, ...], | ||
# [0.02398681640625, 0.0177001953125 ...], | ||
# ] | ||
``` | ||
""" | ||
|
||
model: str | ||
dtype: str = "auto" | ||
trust_remote_code: bool = False | ||
quantization: Optional[str] = None | ||
revision: Optional[str] = None | ||
|
||
seed: int = 0 | ||
|
||
extra_kwargs: Optional[RuntimeParameter[Dict[str, Any]]] = Field( | ||
default_factory=dict, | ||
description="Additional dictionary of keyword arguments that will be passed to the" | ||
" `Engine` class of `sglang` library. See all the supported arguments at: " | ||
"https://github.com/sgl-project/sglang/blob/main/python/sglang/srt/entrypoints/engine.py", | ||
) | ||
|
||
_model: "_SGLang" = PrivateAttr(None) | ||
|
||
def load(self) -> None: | ||
"""Loads the `sglang` model using either the path or the Hugging Face Hub repository id.""" | ||
super().load() | ||
|
||
CudaDevicePlacementMixin.load(self) | ||
|
||
try: | ||
from sglang import Engine as _SGLang | ||
except ImportError as err: | ||
raise ImportError( | ||
"sglang is not installed. Please install it with sglang document https://docs.sglang.ai/start/install.html." | ||
) from err | ||
|
||
self._model = _SGLang( | ||
model_path=self.model, | ||
dtype=self.dtype, | ||
trust_remote_code=self.trust_remote_code, | ||
quantization=self.quantization, | ||
revision=self.revision, | ||
random_seed=self.seed, | ||
**self.extra_kwargs, # type: ignore | ||
) | ||
|
||
def unload(self) -> None: | ||
"""Unloads the `SGLang` model.""" | ||
self._model = None | ||
CudaDevicePlacementMixin.unload(self) | ||
super().unload() | ||
|
||
@property | ||
def model_name(self) -> str: | ||
"""Returns the name of the model.""" | ||
return self.model | ||
|
||
def encode(self, inputs: List[str]) -> List[List[Union[int, float]]]: | ||
"""Generates embeddings for the provided inputs. | ||
|
||
Args: | ||
inputs: a list of texts for which an embedding has to be generated. | ||
|
||
Returns: | ||
The generated embeddings. | ||
""" | ||
return [output["embedding"] for output in self._model.encode(inputs)] |
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
Oops, something went wrong.
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.
why should use this by the name "_SGLang". COuld we directly use Engine?
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 align with the format in vllm. Should I reformat what I commit?
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.
you can use
from sglang import Engine
here, it's fine