Skip to content

Commit

Permalink
refactor: Removing Server subclass from tritonfrontend (#7683)
Browse files Browse the repository at this point in the history
  • Loading branch information
KrishnanPrash authored Oct 8, 2024
1 parent d19c6ab commit 4dbb1b9
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 86 deletions.
6 changes: 3 additions & 3 deletions docs/customization_guide/tritonfrontend.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ Note: `model_path` may need to be edited depending on your setup.
```python
from tritonfrontend import KServeHttp, KServeGrpc
http_options = KServeHttp.Options(thread_count=5)
http_service = KServeHttp.Server(server, http_options)
http_service = KServeHttp(server, http_options)
http_service.start()

# Default options (if none provided)
grpc_service = KServeGrpc.Server(server)
grpc_service = KServeGrpc(server)
grpc_service.start()
```

Expand Down Expand Up @@ -110,7 +110,7 @@ from tritonfrontend import KServeHttp
import tritonclient.http as httpclient
import numpy as np # Use version numpy < 2

with KServeHttp.Server(server) as http_service:
with KServeHttp(server) as http_service:
# The identity model returns an exact duplicate of the input data as output
model_name = "identity"
url = "localhost:8000"
Expand Down
2 changes: 1 addition & 1 deletion qa/L0_python_api/test_kserve.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test_invalid_options(self, frontend):
tritonserver.InvalidArgumentError,
match="Incorrect type for options. options argument must be of type",
):
frontend.Server(server, {"port": 8001})
frontend(server, {"port": 8001})

utils.teardown_server(server)

Expand Down
2 changes: 1 addition & 1 deletion qa/L0_python_api/testing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def setup_service(
frontend: Union[KServeHttp, KServeGrpc],
options=None,
) -> Union[KServeHttp, KServeGrpc]:
service = frontend.Server(server=server, options=options)
service = frontend(server=server, options=options)
service.start()
return service

Expand Down
3 changes: 1 addition & 2 deletions src/python/examples/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def main():
http_options = KServeHttp.Options(port=8005)

# or http_service = KServeHttp.Server(server, http_options) & http_service.stop()
with KServeHttp.Server(server, http_options) as http_service:
with KServeHttp(server, http_options) as http_service:
# The identity model returns an exact duplicate of the input data as output
model_name = "identity"
url = "localhost:8005"
Expand All @@ -74,7 +74,6 @@ def main():
output_data = results.as_numpy("OUTPUT0")

print("--------------------- INFERENCE RESULTS ---------------------")
print("Input data:", input_data)
print("Output data:", output_data)
print("-------------------------------------------------------------")

Expand Down
83 changes: 41 additions & 42 deletions src/python/tritonfrontend/_api/_kservegrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,48 +90,47 @@ def __post_init__(self):
if isinstance(self.infer_compression_level, Grpc_compression_level):
self.infer_compression_level = self.infer_compression_level.value

class Server:
def __init__(self, server: tritonserver, options: "KServeGrpc.Options" = None):
try:
server_ptr = server._ptr() # TRITONSERVER_Server pointer

# If no options provided, default options are selected
if options is None:
options = KServeGrpc.Options()

if not isinstance(options, KServeGrpc.Options):
raise InvalidArgumentError(
"Incorrect type for options. options argument must be of type KServeGrpc.Options"
)

# Converts dataclass instance -> python dictionary -> unordered_map<string, std::variant<...>>
options_dict: dict[str, Union[int, bool, str]] = options.__dict__

self.triton_frontend = TritonFrontendGrpc(server_ptr, options_dict)
except TritonError:
exc_type, exc_value, _ = sys.exc_info()
# raise ... from None masks the tritonfrontend Error from being added in traceback
raise ERROR_MAPPING[exc_type](exc_value) from None

def __enter__(self):
def __init__(self, server: tritonserver, options: "KServeGrpc.Options" = None):
try:
server_ptr = server._ptr() # TRITONSERVER_Server pointer

# If no options provided, default options are selected
if options is None:
options = KServeGrpc.Options()

if not isinstance(options, KServeGrpc.Options):
raise InvalidArgumentError(
"Incorrect type for options. options argument must be of type KServeGrpc.Options"
)

# Converts dataclass instance -> python dictionary -> unordered_map<string, std::variant<...>>
options_dict: dict[str, Union[int, bool, str]] = options.__dict__

self.triton_frontend = TritonFrontendGrpc(server_ptr, options_dict)
except TritonError:
exc_type, exc_value, _ = sys.exc_info()
# raise ... from None masks the tritonfrontend Error from being added in traceback
raise ERROR_MAPPING[exc_type](exc_value) from None

def __enter__(self):
self.triton_frontend.start()
return self

def __exit__(self, exc_type, exc_value, traceback):
self.triton_frontend.stop()
if exc_type:
raise ERROR_MAPPING[exc_type](exc_value) from None

def start(self):
try:
self.triton_frontend.start()
return self
except TritonError:
exc_type, exc_value, _ = sys.exc_info()
raise ERROR_MAPPING[exc_type](exc_value) from None

def __exit__(self, exc_type, exc_value, traceback):
def stop(self):
try:
self.triton_frontend.stop()
if exc_type:
raise ERROR_MAPPING[exc_type](exc_value) from None

def start(self):
try:
self.triton_frontend.start()
except TritonError:
exc_type, exc_value, _ = sys.exc_info()
raise ERROR_MAPPING[exc_type](exc_value) from None

def stop(self):
try:
self.triton_frontend.stop()
except TritonError:
exc_type, exc_value, _ = sys.exc_info()
raise ERROR_MAPPING[exc_type](exc_value) from None
except TritonError:
exc_type, exc_value, _ = sys.exc_info()
raise ERROR_MAPPING[exc_type](exc_value) from None
73 changes: 36 additions & 37 deletions src/python/tritonfrontend/_api/_kservehttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,48 +50,47 @@ class Options:
# DLIS-7215: Add restricted protocol support
# restricted_protocols: list

class Server:
def __init__(self, server: tritonserver, options: "KServeHttp.Options" = None):
try:
server_ptr = server._ptr() # TRITONSERVER_Server pointer
def __init__(self, server: tritonserver, options: "KServeHttp.Options" = None):
try:
server_ptr = server._ptr() # TRITONSERVER_Server pointer

# If no options provided, default options are selected
if options is None:
options = KServeHttp.Options()
# If no options provided, default options are selected
if options is None:
options = KServeHttp.Options()

if not isinstance(options, KServeHttp.Options):
raise InvalidArgumentError(
"Incorrect type for options. options argument must be of type KServeHttp.Options"
)
if not isinstance(options, KServeHttp.Options):
raise InvalidArgumentError(
"Incorrect type for options. options argument must be of type KServeHttp.Options"
)

options_dict: dict[str, Union[int, bool, str]] = options.__dict__
# Converts dataclass instance -> python dictionary -> unordered_map<string, std::variant<...>>
options_dict: dict[str, Union[int, bool, str]] = options.__dict__
# Converts dataclass instance -> python dictionary -> unordered_map<string, std::variant<...>>

self.triton_frontend = TritonFrontendHttp(server_ptr, options_dict)
except TritonError:
exc_type, exc_value, _ = sys.exc_info()
# raise ... from None masks the tritonfrontend Error from being added in traceback
raise ERROR_MAPPING[exc_type](exc_value) from None
self.triton_frontend = TritonFrontendHttp(server_ptr, options_dict)
except TritonError:
exc_type, exc_value, _ = sys.exc_info()
# raise ... from None masks the tritonfrontend Error from being added in traceback
raise ERROR_MAPPING[exc_type](exc_value) from None

def __enter__(self):
self.triton_frontend.start()
return self
def __enter__(self):
self.triton_frontend.start()
return self

def __exit__(self, exc_type, exc_value, traceback):
self.triton_frontend.stop()
if exc_type:
raise ERROR_MAPPING[exc_type](exc_value) from None
def __exit__(self, exc_type, exc_value, traceback):
self.triton_frontend.stop()
if exc_type:
raise ERROR_MAPPING[exc_type](exc_value) from None

def start(self):
try:
self.triton_frontend.start()
except TritonError:
exc_type, exc_value, _ = sys.exc_info()
raise ERROR_MAPPING[exc_type](exc_value) from None
def start(self):
try:
self.triton_frontend.start()
except TritonError:
exc_type, exc_value, _ = sys.exc_info()
raise ERROR_MAPPING[exc_type](exc_value) from None

def stop(self):
try:
self.triton_frontend.stop()
except TritonError:
exc_type, exc_value, _ = sys.exc_info()
raise ERROR_MAPPING[exc_type](exc_value) from None
def stop(self):
try:
self.triton_frontend.stop()
except TritonError:
exc_type, exc_value, _ = sys.exc_info()
raise ERROR_MAPPING[exc_type](exc_value) from None

0 comments on commit 4dbb1b9

Please sign in to comment.