forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Serve][Doc] Update the doc code to use new api (ray-project#27689)
Co-authored-by: Archit Kulkarni <[email protected]> Signed-off-by: Stefan van der Kleij <[email protected]>
- Loading branch information
1 parent
8eeafe6
commit 6e2111f
Showing
17 changed files
with
258 additions
and
215 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import subprocess | ||
|
||
# __deploy_in_single_file_1_start__ | ||
import ray | ||
from ray import serve | ||
|
||
|
||
@serve.deployment | ||
def my_func(request): | ||
return "hello" | ||
|
||
|
||
serve.run(my_func.bind()) | ||
# __deploy_in_single_file_1_end__ | ||
|
||
serve.shutdown() | ||
ray.shutdown() | ||
subprocess.check_output(["ray", "stop", "--force"]) | ||
subprocess.check_output(["ray", "start", "--head"]) | ||
|
||
# __deploy_in_single_file_2_start__ | ||
# This will connect to the running Ray cluster. | ||
ray.init(address="auto", namespace="serve") | ||
|
||
|
||
@serve.deployment | ||
def my_func(request): | ||
return "hello" | ||
|
||
|
||
serve.run(my_func.bind()) | ||
# __deploy_in_single_file_2_end__ | ||
|
||
serve.shutdown() | ||
ray.shutdown() | ||
subprocess.check_output(["ray", "stop", "--force"]) | ||
subprocess.check_output(["ray", "start", "--head"]) | ||
|
||
# __deploy_in_k8s_start__ | ||
# Connect to the running Ray cluster. | ||
ray.init(address="auto") | ||
|
||
|
||
@serve.deployment(route_prefix="/hello") | ||
def hello(request): | ||
return "hello world" | ||
|
||
|
||
serve.run(hello.bind()) | ||
# __deploy_in_k8s_end__ | ||
|
||
subprocess.check_output(["ray", "stop", "--force"]) |
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
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,44 @@ | ||
import ray | ||
from ray import serve | ||
import requests | ||
|
||
|
||
# __basic_example_start__ | ||
@serve.deployment | ||
class Deployment: | ||
def method1(self, arg): | ||
return f"Method1: {arg}" | ||
|
||
def __call__(self, arg): | ||
return f"__call__: {arg}" | ||
|
||
|
||
handle = serve.run(Deployment.bind()) | ||
|
||
ray.get(handle.remote("hi")) # Defaults to calling the __call__ method. | ||
ray.get(handle.method1.remote("hi")) # Call a different method. | ||
# __basic_example_end__ | ||
|
||
|
||
# __async_handle_start__ | ||
@serve.deployment(route_prefix="/api") | ||
class Deployment: | ||
def say_hello(self, name: str): | ||
return f"Hello {name}!" | ||
|
||
def __call__(self, request): | ||
return self.say_hello(request.query_params["name"]) | ||
|
||
|
||
handle = serve.run(Deployment.bind()) | ||
|
||
# __async_handle_end__ | ||
|
||
|
||
# __async_handle_print_start__ | ||
print(requests.get("https://localhost:8000/api?name=Alice")) | ||
# Hello Alice! | ||
|
||
print(ray.get(handle.say_hello.remote("Alice"))) | ||
# Hello Alice! | ||
# __async_handle_print_end__ |
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,79 @@ | ||
from ray import serve | ||
import time | ||
import os | ||
|
||
|
||
# __updating_a_deployment_start__ | ||
@serve.deployment(name="my_deployment", num_replicas=1) | ||
class SimpleDeployment: | ||
pass | ||
|
||
|
||
# Creates one initial replica. | ||
serve.run(SimpleDeployment.bind()) | ||
|
||
|
||
# Re-deploys, creating an additional replica. | ||
# This could be the SAME Python script, modified and re-run. | ||
@serve.deployment(name="my_deployment", num_replicas=2) | ||
class SimpleDeployment: | ||
pass | ||
|
||
|
||
serve.run(SimpleDeployment.bind()) | ||
|
||
# You can also use Deployment.options() to change options without redefining | ||
# the class. This is useful for programmatically updating deployments. | ||
serve.run(SimpleDeployment.options(num_replicas=2).bind()) | ||
# __updating_a_deployment_end__ | ||
|
||
|
||
# __scaling_out_start__ | ||
# Create with a single replica. | ||
@serve.deployment(num_replicas=1) | ||
def func(*args): | ||
pass | ||
|
||
|
||
serve.run(func.bind()) | ||
|
||
# Scale up to 3 replicas. | ||
serve.run(func.options(num_replicas=3).bind()) | ||
|
||
# Scale back down to 1 replica. | ||
serve.run(func.options(num_replicas=1).bind()) | ||
# __scaling_out_end__ | ||
|
||
|
||
# __autoscaling_start__ | ||
@serve.deployment( | ||
autoscaling_config={ | ||
"min_replicas": 1, | ||
"max_replicas": 5, | ||
"target_num_ongoing_requests_per_replica": 10, | ||
} | ||
) | ||
def func(_): | ||
time.sleep(1) | ||
return "" | ||
|
||
|
||
serve.run( | ||
func.bind() | ||
) # The func deployment will now autoscale based on requests demand. | ||
# __autoscaling_end__ | ||
|
||
|
||
# __configure_parallism_start__ | ||
@serve.deployment | ||
class MyDeployment: | ||
def __init__(self, parallelism: str): | ||
os.environ["OMP_NUM_THREADS"] = parallelism | ||
# Download model weights, initialize model, etc. | ||
|
||
def __call__(self): | ||
pass | ||
|
||
|
||
serve.run(MyDeployment.bind("12")) | ||
# __configure_parallism_end__ |
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,24 @@ | ||
from ray import serve | ||
from typing import List, Dict, Any | ||
|
||
|
||
# __batch_example_start__ | ||
@serve.deployment(route_prefix="/increment") | ||
class BatchingExample: | ||
def __init__(self): | ||
self.count = 0 | ||
|
||
@serve.batch | ||
async def handle_batch(self, requests: List[Any]) -> List[Dict]: | ||
responses = [] | ||
for request in requests: | ||
responses.append(request.json()) | ||
|
||
return responses | ||
|
||
async def __call__(self, request) -> List[Dict]: | ||
return await self.handle_batch(request) | ||
|
||
|
||
serve.run(BatchingExample.bind()) | ||
# __batch_example_end__ |
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
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
Oops, something went wrong.