Skip to content

Commit

Permalink
[python] warn up python model. (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankfliu committed Oct 20, 2021
1 parent 3375b4c commit efb0720
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 10 deletions.
3 changes: 3 additions & 0 deletions engines/python/setup/djl_python/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ def get_as_numpy(self, key=None) -> list:
"""
return from_nd_list(self.get_as_bytes(key=key))

def is_empty(self):
return self.content.is_empty()

def read(self, conn):
prop_size = retrieve_short(conn)

Expand Down
6 changes: 3 additions & 3 deletions engines/python/setup/djl_python/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@


class Output(object):
def __init__(self):
self.code = 200
self.message = 'OK'
def __init__(self, code=200, message='OK'):
self.code = code
self.message = message
self.properties = dict()
self.content = PairList()

Expand Down
6 changes: 3 additions & 3 deletions engines/python/setup/djl_python_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ def run_server(self):
function_name = inputs.get_function_name()
try:
outputs = getattr(self.service, function_name)(inputs)
if outputs is None:
outputs = Output(code=204, message="No content")
except Exception as e:
logging.error(e, exc_info=True)
_, ex_value, _ = sys.exc_info()
outputs = Output()
outputs.set_code(500)
outputs.set_message(str(ex_value))
outputs = Output(code=500, message=str(ex_value))

if not cl_socket.sendall(outputs.encode()):
logging.debug("Outputs is sent to DJL engine.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Output send(Input input) throws ExecutionException, InterruptedException, Timeou
throw new IllegalStateException("Failed to send data to python.");
}
// TODO: make this configurable
return f.get(5, TimeUnit.MINUTES);
return f.get(10, TimeUnit.MINUTES);
}

private String[] getPythonStartCmd(PyEnv pyEnv, Model model) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ synchronized void startPythonProcess() {
}

connection.connect();

try {
// initialize model with an empty request
predict(new Input());
} catch (TranslateException ignore) {
logger.warn("Python model {} doesn't support warn up: ", model.getName());
}
} catch (InterruptedException e) {
throw new EngineException("Worker startup cancelled.", e);
} catch (IOException e) {
Expand Down
6 changes: 4 additions & 2 deletions engines/python/src/test/resources/accumulate/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

import logging
import numpy as np
from djl_python import *
from djl_python import Input
from djl_python import Output


class Accumulation(object):
Expand Down Expand Up @@ -65,7 +66,8 @@ def handle(inputs: Input):
# stateful model
_service.initialize(inputs.get_properties())

if inputs is None:
if inputs.is_empty():
# initialization request
return None

return _service.accumulate(inputs)
3 changes: 2 additions & 1 deletion engines/python/src/test/resources/resnet18/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ def handle(inputs: Input):
# stateful model
_service.initialize(inputs.get_properties())

if inputs is None:
if inputs.is_empty():
# initialization request
return None

return _service.inference(inputs)

0 comments on commit efb0720

Please sign in to comment.