Skip to content

Commit

Permalink
Batch transform debug (awslabs#404)
Browse files Browse the repository at this point in the history
* Added debug to batch-transform.
  • Loading branch information
Jasper Schulz authored and Ayed committed Nov 29, 2019
1 parent ac782de commit 97950b3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
25 changes: 21 additions & 4 deletions src/gluonts/shell/serve/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,35 @@ def invocations() -> Response:


def batch_inference_invocations(predictor_factory, configuration) -> Flask:
DEBUG = configuration.dict().get("DEBUG")
predictor = predictor_factory({"configuration": configuration.dict()})

def invocations() -> Response:
request_data = request.data.decode("utf8").strip()
instances = list(map(json.loads, request_data.splitlines()))
predictions = []

predictions = handle_predictions(predictor, instances, configuration)
# we have to take this as the initial start-time since the first
# forecast is produced before the loop in predictor.predict
start = time.time()

lines = list(map(json.dumps, map(jsonify_floats, predictions)))
forecast_iter = predictor.predict(
ListDataset(instances, predictor.freq),
num_eval_samples=configuration.num_eval_samples,
)

for forecast in forecast_iter:
end = time.time()
prediction = forecast.as_json_dict(configuration)

# force line break at the end
lines.append("")
if DEBUG:
prediction["debug"] = {"timing": end - start}

predictions.append(prediction)

start = time.time()

lines = list(map(json.dumps, map(jsonify_floats, predictions)))
return Response("\n".join(lines), mimetype="application/jsonlines")

return invocations
Expand Down
3 changes: 2 additions & 1 deletion src/gluonts/testutil/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ def batch_invocations(
url=self.url("/invocations"), data="\n".join(instances)
)

assert response.status_code == 200
if response.status_code != 200:
raise RuntimeError(response.content.decode("utf-8"))

predictions = list(map(json.loads, response.text.splitlines()))
assert len(predictions) == len(instances)
Expand Down

0 comments on commit 97950b3

Please sign in to comment.