Search in sources :

Example 6 with APIException

use of io.seldon.engine.exception.APIException in project seldon-core by SeldonIO.

the class AverageCombinerUnit method aggregate.

@Override
public SeldonMessage aggregate(List<SeldonMessage> outputs, PredictiveUnitState state) {
    if (outputs.size() == 0) {
        throw new APIException(APIException.ApiExceptionType.ENGINE_INVALID_COMBINER_RESPONSE, String.format("Combiner received no inputs"));
    }
    int[] shape = PredictorUtils.getShape(outputs.get(0).getData());
    if (shape == null) {
        throw new APIException(APIException.ApiExceptionType.ENGINE_INVALID_COMBINER_RESPONSE, String.format("Combiner cannot extract data shape"));
    }
    if (shape.length != 2) {
        throw new APIException(APIException.ApiExceptionType.ENGINE_INVALID_COMBINER_RESPONSE, String.format("Combiner received data that is not 2 dimensional"));
    }
    INDArray currentSum = Nd4j.zeros(shape[0], shape[1]);
    SeldonMessage.Builder respBuilder = SeldonMessage.newBuilder();
    for (Iterator<SeldonMessage> i = outputs.iterator(); i.hasNext(); ) {
        DefaultData inputData = i.next().getData();
        int[] inputShape = PredictorUtils.getShape(inputData);
        if (inputShape == null) {
            throw new APIException(APIException.ApiExceptionType.ENGINE_INVALID_COMBINER_RESPONSE, String.format("Combiner cannot extract data shape"));
        }
        if (inputShape.length != 2) {
            throw new APIException(APIException.ApiExceptionType.ENGINE_INVALID_COMBINER_RESPONSE, String.format("Combiner received data that is not 2 dimensional"));
        }
        if (inputShape[0] != shape[0]) {
            throw new APIException(APIException.ApiExceptionType.ENGINE_INVALID_COMBINER_RESPONSE, String.format("Expected batch length %d but found %d", shape[0], inputShape[0]));
        }
        if (inputShape[1] != shape[1]) {
            throw new APIException(APIException.ApiExceptionType.ENGINE_INVALID_COMBINER_RESPONSE, String.format("Expected batch length %d but found %d", shape[1], inputShape[1]));
        }
        INDArray inputArr = PredictorUtils.getINDArray(inputData);
        currentSum = currentSum.add(inputArr);
    }
    currentSum = currentSum.div((float) outputs.size());
    DefaultData newData = PredictorUtils.updateData(outputs.get(0).getData(), currentSum);
    respBuilder.setData(newData);
    respBuilder.setMeta(outputs.get(0).getMeta());
    respBuilder.setStatus(outputs.get(0).getStatus());
    return respBuilder.build();
}
Also used : APIException(io.seldon.engine.exception.APIException) SeldonMessage(io.seldon.protos.PredictionProtos.SeldonMessage) INDArray(org.nd4j.linalg.api.ndarray.INDArray) DefaultData(io.seldon.protos.PredictionProtos.DefaultData)

Aggregations

APIException (io.seldon.engine.exception.APIException)6 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)3 SeldonMessage (io.seldon.protos.PredictionProtos.SeldonMessage)3 ExecutionException (java.util.concurrent.ExecutionException)2 INDArray (org.nd4j.linalg.api.ndarray.INDArray)2 ResponseEntity (org.springframework.http.ResponseEntity)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 CombinerBlockingStub (io.seldon.protos.CombinerGrpc.CombinerBlockingStub)1 Endpoint (io.seldon.protos.DeploymentProtos.Endpoint)1 GenericBlockingStub (io.seldon.protos.GenericGrpc.GenericBlockingStub)1 DefaultData (io.seldon.protos.PredictionProtos.DefaultData)1 Feedback (io.seldon.protos.PredictionProtos.Feedback)1 SeldonMessageList (io.seldon.protos.PredictionProtos.SeldonMessageList)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1