-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add RayService vLLM TPU Inference script #1467
Open
ryanaoleary
wants to merge
18
commits into
GoogleCloudPlatform:main
Choose a base branch
from
ryanaoleary:multihost-example
base: main
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
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
dfd04dd
Add RayService vLLM TPU Inference script
ryanaoleary 335e75c
Fix inference script
ryanaoleary fe6440c
Add RayCluster and RayService CRs
ryanaoleary ee804fd
Fix working_dir link
ryanaoleary 94e0348
Merge branch 'main' into multihost-example
ryanaoleary 37f0280
Set max model length
ryanaoleary 11c7ee6
pass in max model len as env var
ryanaoleary deeee56
Add Ray CR manifests and fix inference script
ryanaoleary b12ffb7
Fix model id
ryanaoleary cbce283
Don't specify mxla and slicebuilder ports
ryanaoleary aa21d2c
Pass env vars to runtime env
ryanaoleary 5b42cb8
Add model composition example
ryanaoleary ced331d
Update name
ryanaoleary 9e9d466
Support passing TPU_HEADS as env var
ryanaoleary 06ed4fc
Update RayService TPU CR
ryanaoleary c6de351
Merge branch 'main' into multihost-example
ryanaoleary 41ed84a
Rescope PR to v5e and v6e for Llama-3.1-405B
ryanaoleary 53bbfeb
Add v5e and v6e RayServices
ryanaoleary 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# 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. | ||
|
||
# NOTE: this file was inspired from: https://github.com/richardsliu/vllm/blob/rayserve/examples/rayserve_tpu.py | ||
|
||
import os | ||
|
||
import json | ||
import logging | ||
from typing import Dict, List, Optional | ||
|
||
import ray | ||
from fastapi import FastAPI | ||
from ray import serve | ||
from starlette.requests import Request | ||
from starlette.responses import Response | ||
|
||
from vllm import LLM, SamplingParams | ||
|
||
logger = logging.getLogger("ray.serve") | ||
|
||
model_id = "meta-llama/Meta-Llama-3-70B" | ||
ryanaoleary marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
app = FastAPI() | ||
|
||
@serve.deployment(name="VLLMDeployment") | ||
@serve.ingress(app) | ||
class VLLMDeployment: | ||
def __init__( | ||
self, | ||
num_tpu_chips, | ||
): | ||
self.llm = LLM( | ||
model=model_id, | ||
tensor_parallel_size=num_tpu_chips, | ||
enforce_eager=True, | ||
) | ||
|
||
@app.post("/v1/generate") | ||
async def generate(self, request: Request): | ||
request_dict = await request.json() | ||
prompts = request_dict.pop("prompt") | ||
print("Processing prompt ", prompts) | ||
sampling_params = SamplingParams(temperature=0.7, | ||
top_p=1.0, | ||
n=1, | ||
max_tokens=1000) | ||
|
||
outputs = self.llm.generate(prompts, sampling_params) | ||
for output in outputs: | ||
prompt = output.prompt | ||
generated_text = "" | ||
token_ids = [] | ||
for completion_output in output.outputs: | ||
generated_text += completion_output.text | ||
token_ids.extend(list(completion_output.token_ids)) | ||
|
||
print("Generated text: ", generated_text) | ||
ret = { | ||
"prompt": prompt, | ||
"text": generated_text, | ||
"token_ids": token_ids, | ||
} | ||
|
||
return Response(content=json.dumps(ret)) | ||
|
||
def get_num_tpu_chips() -> int: | ||
if "TPU" not in ray.cluster_resources(): | ||
# Pass in TPU chips when the current Ray cluster resources can't be auto-detected (i.e for autoscaling). | ||
if os.environ.get('TPU_CHIPS') is not None: | ||
return int(os.environ.get('TPU_CHIPS')) | ||
return 0 | ||
return int(ray.cluster_resources()["TPU"]) | ||
|
||
def build_app(cli_args: Dict[str, str]) -> serve.Application: | ||
"""Builds the Serve app based on CLI arguments.""" | ||
ray.init(ignore_reinit_error=True) | ||
|
||
# Set the model to use, defaults to Llama-3-70B. | ||
if 'MODEL_ID' in os.environ: | ||
model_id = os.environ.get('MODEL_ID') | ||
|
||
num_tpu_chips = get_num_tpu_chips() | ||
pg_resources = [] | ||
pg_resources.append({"CPU": 1}) # for the deployment replica | ||
for i in range(num_tpu_chips): | ||
pg_resources.append({"CPU": 1, "TPU": 1}) # for the vLLM actors | ||
|
||
# Use PACK strategy since the deployment may use more than one TPU node. | ||
return VLLMDeployment.options( | ||
placement_group_bundles=pg_resources, | ||
placement_group_strategy="PACK").bind(num_tpu_chips) | ||
|
||
model = build_app({}) | ||
ryanaoleary marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
@richardsliu can we get this example merged into the vllm repo?
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 opened this one a while back: vllm-project/vllm#8038
I'll ping them again on it.