Search in sources :

Example 1 with InferenceException

use of io.hops.hopsworks.exceptions.InferenceException in project hopsworks by logicalclocks.

the class InferenceController method infer.

/**
 * Makes an inference request to a running serving instance
 *
 * @param project the project where the serving is running
 * @param modelName the name of the serving
 * @param modelVersion the version of the serving
 * @param verb the predictiont type (predict, regress, or classify)
 * @param inferenceRequestJson the user-provided JSON payload for the inference request
 * @return a string representation of the inference result
 * @throws InferenceException
 */
public String infer(Project project, String username, String modelName, Integer modelVersion, InferenceVerb verb, String inferenceRequestJson, String authHeader) throws InferenceException, ApiKeyException {
    Serving serving = servingFacade.findByProjectAndName(project, modelName);
    if (serving == null) {
        throw new InferenceException(RESTCodes.InferenceErrorCode.SERVING_NOT_FOUND, Level.FINE, "name: " + modelName);
    }
    if (verb == null) {
        throw new InferenceException(RESTCodes.InferenceErrorCode.MISSING_VERB, Level.FINE);
    }
    if (modelVersion != null && modelVersion < 0) {
        throw new InferenceException(RESTCodes.InferenceErrorCode.BAD_REQUEST, Level.FINE, "Model version must be " + "positive");
    }
    // ServingInferenceController is either localhost or kubernetes inference controller
    Pair<Integer, String> inferenceResult = servingInferenceController.infer(username, serving, modelVersion, verb, inferenceRequestJson, authHeader);
    // Log the inference
    for (InferenceLogger inferenceLogger : inferenceLoggers) {
        try {
            inferenceLogger.logInferenceRequest(serving, inferenceRequestJson, inferenceResult.getL(), inferenceResult.getR());
        } catch (Exception e) {
            // We don't want to fill the logs with inference logging errors
            logger.log(Level.FINE, "Error logging inference for logger: " + inferenceLogger.getClassName(), e);
        }
    }
    // If the inference server returned something different than 200 then throw an exception to the user
    if (inferenceResult.getL() >= 500) {
        logger.log(Level.FINE, "Request error: " + inferenceResult.getL() + " - " + inferenceResult.getR());
        throw new InferenceException(RESTCodes.InferenceErrorCode.SERVING_INSTANCE_INTERNAL, Level.FINE, inferenceResult.getR());
    } else if (inferenceResult.getL() >= 400) {
        logger.log(Level.FINE, "Request error: " + inferenceResult.getL() + " - " + inferenceResult.getR());
        throw new InferenceException(RESTCodes.InferenceErrorCode.SERVING_INSTANCE_BAD_REQUEST, Level.FINE, inferenceResult.getR());
    }
    return inferenceResult.getR();
}
Also used : Serving(io.hops.hopsworks.persistence.entity.serving.Serving) InferenceLogger(io.hops.hopsworks.common.serving.inference.logger.InferenceLogger) InferenceException(io.hops.hopsworks.exceptions.InferenceException) InferenceException(io.hops.hopsworks.exceptions.InferenceException) ApiKeyException(io.hops.hopsworks.exceptions.ApiKeyException)

Example 2 with InferenceException

use of io.hops.hopsworks.exceptions.InferenceException in project hopsworks by logicalclocks.

the class InferenceHttpClient method handleInferenceResponse.

/**
 * Handles a HTTP response to an inference request. Parses the response into a tuple of (statusCode, responseStr)
 *
 * @param response the HTTP response to handle
 * @return a tuple of (statusCode, responseStr)
 * @throws InferenceException in case there was an error handling the response
 */
public Pair<Integer, String> handleInferenceResponse(CloseableHttpResponse response) throws InferenceException {
    try {
        if (response == null) {
            throw new InferenceException(RESTCodes.InferenceErrorCode.EMPTY_RESPONSE, Level.INFO, "Received null response");
        }
        HttpEntity httpEntity = response.getEntity();
        if (httpEntity == null) {
            throw new InferenceException(RESTCodes.InferenceErrorCode.EMPTY_RESPONSE, Level.INFO, "Received null response");
        }
        try {
            // Return prediction
            String responseStr = EntityUtils.toString(httpEntity);
            EntityUtils.consume(httpEntity);
            return new Pair<>(response.getStatusLine().getStatusCode(), responseStr);
        } catch (IOException e) {
            throw new InferenceException(RESTCodes.InferenceErrorCode.ERROR_READING_RESPONSE, Level.INFO, "", e.getMessage(), e);
        }
    } finally {
        try {
            if (response != null) {
                response.close();
            }
        } catch (IOException ioe) {
            logger.log(Level.FINE, "Error closing response", ioe);
        }
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) InferenceException(io.hops.hopsworks.exceptions.InferenceException) IOException(java.io.IOException) Pair(io.hops.common.Pair)

Example 3 with InferenceException

use of io.hops.hopsworks.exceptions.InferenceException in project hopsworks by logicalclocks.

the class LocalhostInferenceController method infer.

/**
 * Local inference. Sends a JSON request to the REST API of a local serving server
 *
 * @param serving the serving instance to send the request to
 * @param modelVersion the version of the serving
 * @param verb the type of inference request (predict, regress, classify)
 * @param inferenceRequestJson the JSON payload of the inference request
 * @return the inference result returned by the serving server
 * @throws InferenceException
 */
public Pair<Integer, String> infer(String username, Serving serving, Integer modelVersion, InferenceVerb verb, String inferenceRequestJson, String authHeader) throws InferenceException {
    if (serving.getCid().equals(CID_STOPPED)) {
        throw new InferenceException(RESTCodes.InferenceErrorCode.SERVING_NOT_RUNNING, Level.FINE);
    }
    String path = getInferencePath(serving, modelVersion, verb);
    try {
        HttpPost request = servingInferenceUtils.buildInferenceRequest("localhost", serving.getLocalPort(), path, inferenceRequestJson);
        HttpContext context = HttpClientContext.create();
        CloseableHttpResponse response = inferenceHttpClient.execute(request, context);
        return inferenceHttpClient.handleInferenceResponse(response);
    } catch (URISyntaxException e) {
        throw new InferenceException(RESTCodes.InferenceErrorCode.REQUEST_ERROR, Level.SEVERE, null, e.getMessage(), e);
    } catch (UnsupportedCharsetException e) {
        throw new InferenceException(RESTCodes.InferenceErrorCode.BAD_REQUEST, Level.INFO, null, e.getMessage(), e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) HttpContext(org.apache.http.protocol.HttpContext) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) InferenceException(io.hops.hopsworks.exceptions.InferenceException) URISyntaxException(java.net.URISyntaxException)

Aggregations

InferenceException (io.hops.hopsworks.exceptions.InferenceException)3 Pair (io.hops.common.Pair)1 InferenceLogger (io.hops.hopsworks.common.serving.inference.logger.InferenceLogger)1 ApiKeyException (io.hops.hopsworks.exceptions.ApiKeyException)1 Serving (io.hops.hopsworks.persistence.entity.serving.Serving)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)1 HttpEntity (org.apache.http.HttpEntity)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 HttpContext (org.apache.http.protocol.HttpContext)1