forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[serve] move handle options to its own file (ray-project#48454)
## Why are these changes needed? Move handle options to `handle_options.py`. Signed-off-by: Cindy Zhang <[email protected]>
- Loading branch information
Showing
4 changed files
with
92 additions
and
87 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,71 @@ | ||
from abc import ABC, abstractmethod | ||
from dataclasses import dataclass, fields | ||
|
||
import ray | ||
from ray.serve._private.common import DeploymentHandleSource, RequestProtocol | ||
from ray.serve._private.utils import DEFAULT | ||
|
||
|
||
@dataclass(frozen=True) | ||
class InitHandleOptionsBase(ABC): | ||
"""Init options for each ServeHandle instance. | ||
These fields can be set by calling `.init()` on a handle before | ||
sending the first request. | ||
""" | ||
|
||
_prefer_local_routing: bool = False | ||
_source: DeploymentHandleSource = DeploymentHandleSource.UNKNOWN | ||
|
||
@classmethod | ||
@abstractmethod | ||
def create(cls, **kwargs) -> "InitHandleOptionsBase": | ||
raise NotImplementedError | ||
|
||
|
||
@dataclass(frozen=True) | ||
class InitHandleOptions(InitHandleOptionsBase): | ||
@classmethod | ||
def create(cls, **kwargs) -> "InitHandleOptions": | ||
for k in list(kwargs.keys()): | ||
if kwargs[k] == DEFAULT.VALUE: | ||
# Use default value | ||
del kwargs[k] | ||
|
||
# Detect replica source for handles | ||
if ( | ||
"_source" not in kwargs | ||
and ray.serve.context._get_internal_replica_context() is not None | ||
): | ||
kwargs["_source"] = DeploymentHandleSource.REPLICA | ||
|
||
return cls(**kwargs) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class DynamicHandleOptionsBase(ABC): | ||
"""Dynamic options for each ServeHandle instance. | ||
These fields can be changed by calling `.options()` on a handle. | ||
""" | ||
|
||
method_name: str = "__call__" | ||
multiplexed_model_id: str = "" | ||
stream: bool = False | ||
_request_protocol: str = RequestProtocol.UNDEFINED | ||
|
||
def copy_and_update(self, **kwargs) -> "DynamicHandleOptionsBase": | ||
new_kwargs = {} | ||
|
||
for f in fields(self): | ||
if f.name not in kwargs or kwargs[f.name] == DEFAULT.VALUE: | ||
new_kwargs[f.name] = getattr(self, f.name) | ||
else: | ||
new_kwargs[f.name] = kwargs[f.name] | ||
|
||
return DynamicHandleOptions(**new_kwargs) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class DynamicHandleOptions(DynamicHandleOptionsBase): | ||
pass |
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