-
Notifications
You must be signed in to change notification settings - Fork 300
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
vllm inference plugin #2967
Open
dansola
wants to merge
2
commits into
master
Choose a base branch
from
danielsola/par-41-create-ticket-to-convert-vllm-example-into-flytekit-plugin
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
vllm inference plugin #2967
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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
Empty file.
85 changes: 85 additions & 0 deletions
85
plugins/flytekit-inference/flytekitplugins/inference/vllm/serve.py
This file contains 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,85 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
from ..sidecar_template import ModelInferenceTemplate | ||
|
||
|
||
@dataclass | ||
class HFSecret: | ||
""" | ||
:param secrets_prefix: The secrets prefix that Flyte appends to all mounted secrets. | ||
:param hf_token_group: The group name for the HuggingFace token. | ||
:param hf_token_key: The key name for the HuggingFace token. | ||
""" | ||
|
||
secrets_prefix: str # _UNION_ or _FSEC_ | ||
hf_token_key: str | ||
hf_token_group: Optional[str] = None | ||
|
||
|
||
class VLLM(ModelInferenceTemplate): | ||
def __init__( | ||
self, | ||
hf_secret: HFSecret, | ||
arg_dict: Optional[dict] = None, | ||
image: str = "vllm/vllm-openai", | ||
health_endpoint: str = "/health", | ||
port: int = 8000, | ||
cpu: int = 2, | ||
gpu: int = 1, | ||
mem: str = "10Gi", | ||
): | ||
""" | ||
Initialize NIM class for managing a Kubernetes pod template. | ||
|
||
:param hf_secret: Instance of HFSecret for managing hugging face secrets. | ||
:param arg_dict: A dictionary of arguments for the VLLM model server (https://docs.vllm.ai/en/stable/models/engine_args.html). | ||
:param image: The Docker image to be used for the model server container. Default is "ç". | ||
samhita-alla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
:param health_endpoint: The health endpoint for the model server container. Default is "/health". | ||
:param port: The port number for the model server container. Default is 8000. | ||
:param cpu: The number of CPU cores requested for the model server container. Default is 2. | ||
:param gpu: The number of GPU cores requested for the model server container. Default is 1. | ||
:param mem: The amount of memory requested for the model server container. Default is "10Gi". | ||
""" | ||
if hf_secret.hf_token_key is None: | ||
raise ValueError("HuggingFace token key must be provided.") | ||
if hf_secret.secrets_prefix is None: | ||
raise ValueError("Secrets prefix must be provided.") | ||
|
||
self._hf_secret = hf_secret | ||
self._arg_dict = arg_dict | ||
|
||
super().__init__( | ||
image=image, | ||
health_endpoint=health_endpoint, | ||
port=port, | ||
cpu=cpu, | ||
gpu=gpu, | ||
mem=mem, | ||
) | ||
|
||
self.setup_vllm_pod_template() | ||
|
||
def setup_vllm_pod_template(self): | ||
from kubernetes.client.models import V1EnvVar | ||
|
||
model_server_container = self.pod_template.pod_spec.init_containers[0] | ||
|
||
if self._hf_secret.hf_token_group: | ||
hf_key = f"$({self._hf_secret.secrets_prefix}{self._hf_secret.hf_token_group}_{self._hf_secret.hf_token_key})".upper() | ||
else: | ||
hf_key = f"$({self._hf_secret.secrets_prefix}{self._hf_secret.hf_token_key})".upper() | ||
|
||
model_server_container.env = [ | ||
V1EnvVar(name="HUGGING_FACE_HUB_TOKEN", value=hf_key), | ||
] | ||
model_server_container.args = self.build_vllm_args() | ||
|
||
def build_vllm_args(self) -> list: | ||
args = [] | ||
if self._arg_dict: | ||
for key, value in self._arg_dict.items(): | ||
args.append(f"--{key}") | ||
if value is not None: | ||
args.append(str(value)) | ||
return args |
This file contains 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 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,60 @@ | ||
from flytekitplugins.inference import VLLM, HFSecret | ||
|
||
|
||
def test_vllm_init_valid_params(): | ||
vllm_args = { | ||
"model": "google/gemma-2b-it", | ||
"dtype": "half", | ||
"max-model-len": 2000, | ||
} | ||
|
||
hf_secrets = HFSecret( | ||
secrets_prefix="_UNION_", | ||
hf_token_key="vllm_hf_token" | ||
) | ||
|
||
vllm_instance = VLLM( | ||
hf_secret=hf_secrets, | ||
arg_dict=vllm_args, | ||
image='vllm/vllm-openai:my-tag', | ||
cpu='10', | ||
gpu='2', | ||
mem='50Gi', | ||
port=8080, | ||
) | ||
|
||
assert len(vllm_instance.pod_template.pod_spec.init_containers) == 1 | ||
assert ( | ||
vllm_instance.pod_template.pod_spec.init_containers[0].image | ||
== 'vllm/vllm-openai:my-tag' | ||
) | ||
assert ( | ||
vllm_instance.pod_template.pod_spec.init_containers[0].resources.requests[ | ||
"memory" | ||
] | ||
== "50Gi" | ||
) | ||
assert ( | ||
vllm_instance.pod_template.pod_spec.init_containers[0].ports[0].container_port | ||
== 8080 | ||
) | ||
assert vllm_instance.pod_template.pod_spec.init_containers[0].args == ['--model', 'google/gemma-2b-it', '--dtype', 'half', '--max-model-len', '2000'] | ||
assert vllm_instance.pod_template.pod_spec.init_containers[0].env[0].name == 'HUGGING_FACE_HUB_TOKEN' | ||
assert vllm_instance.pod_template.pod_spec.init_containers[0].env[0].value == '$(_UNION_VLLM_HF_TOKEN)' | ||
|
||
|
||
|
||
def test_vllm_default_params(): | ||
vllm_instance = VLLM(hf_secret=HFSecret(secrets_prefix="_FSEC_", hf_token_key="test_token")) | ||
|
||
assert vllm_instance.base_url == "http://localhost:8000" | ||
assert vllm_instance._image == 'vllm/vllm-openai' | ||
assert vllm_instance._port == 8000 | ||
assert vllm_instance._cpu == 2 | ||
assert vllm_instance._gpu == 1 | ||
assert vllm_instance._health_endpoint == "/health" | ||
assert vllm_instance._mem == "10Gi" | ||
assert vllm_instance._arg_dict == None | ||
assert vllm_instance._hf_secret.secrets_prefix == '_FSEC_' | ||
assert vllm_instance._hf_secret.hf_token_key == 'test_token' | ||
assert vllm_instance._hf_secret.hf_token_group == None |
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.