Skip to content

Commit

Permalink
[Serve][Doc] Add http endpoint for dag pattern doc (#25243)
Browse files Browse the repository at this point in the history
  • Loading branch information
sihanwang41 authored Jun 1, 2022
1 parent 905258d commit 4ad7505
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ parts:
- file: serve/deployment-graph/chain_nodes_same_class_different_args
- file: serve/deployment-graph/combine_two_nodes_with_passing_input_parallel
- file: serve/deployment-graph/control_flow_based_on_user_inputs
- file: serve/deployment-graph/http_endpoint_for_dag_graph
- file: serve/architecture
- file: serve/tutorials/index
- file: serve/faq
Expand Down
1 change: 1 addition & 0 deletions doc/source/serve/deployment-graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ Jump striaght into a common design patterns using deployment graph:
- [Chain nodes with same class and different args](deployment-graph/chain_nodes_same_class_different_args.md)
- [Combine two nodes with passing same input in parallel](deployment-graph/combine_two_nodes_with_passing_input_parallel.md)
- [Control flow based on user inputs](deployment-graph/control_flow_based_on_user_inputs.md)
- [Http endpoint for dag graph](deployment-graph/http_endpoint_for_dag_graph.md)

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Pattern: Control flow based on the user inputs

The example shows how to user inputs to control the graph flow
The example shows how to use inputs to control the graph flow

![pic](https://raw.githubusercontent.com/ray-project/images/master/docs/serve/deployment-graph/control_flow_based_on_user_inputs.svg)

Expand Down
29 changes: 29 additions & 0 deletions doc/source/serve/deployment-graph/http_endpoint_for_dag_graph.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Pattern: Http endpoint for dag graph

This example shows how to configure ingress component of the deployment graph, such as HTTP endpoint prefix, HTTP to python object input adapter.

## Code

+++

```{eval-rst}
.. literalinclude:: ../doc_code/deployment_graph_dag_http.py
:language: python
```

````{note}
1. Serve provide a special driver ([DAGDriver](deployment-graph-e2e-tutorial.html#step-4-driver-deployment-to-handle-http-ingress)) to accept the http request and drive the dag graph execution
2. User can specify the customized http adapter to adopt the cusomized input format
````

## Outputs

Model output1: 1(input) + 0(weight) + 4(len("test")) = 5 \
Model output2: 1(input) + 1(weight) + 4(len("test")) = 6 \
So the combine sum is 11
```
11
11
```

+++
54 changes: 54 additions & 0 deletions doc/source/serve/doc_code/deployment_graph_dag_http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import requests
from pydantic import BaseModel

import ray
from ray import serve
from ray.serve.drivers import DAGDriver
from ray.experimental.dag.input_node import InputNode


ray.init()
serve.start()


class ModelInputData(BaseModel):
model_input1: int
model_input2: str


@serve.deployment
class Model:
def __init__(self, weight):
self.weight = weight

def forward(self, input: ModelInputData):
return input.model_input1 + len(input.model_input2) + self.weight


@serve.deployment
def combine(value_refs):
return sum(ray.get(value_refs))


with InputNode() as user_input:
model1 = Model.bind(0)
model2 = Model.bind(1)
output1 = model1.forward.bind(user_input)
output2 = model2.forward.bind(user_input)
dag = combine.bind([output1, output2])
serve_dag = DAGDriver.options(route_prefix="/my-dag").bind(
dag, http_adapter=ModelInputData
)

dag_handle = serve.run(serve_dag)

print(
ray.get(
dag_handle.predict.remote(ModelInputData(model_input1=1, model_input2="test"))
)
)
print(
requests.post(
"http://127.0.0.1:8000/my-dag", json={"model_input1": 1, "model_input2": "test"}
).text
)

0 comments on commit 4ad7505

Please sign in to comment.