Skip to content
Advertisement

TorchServe: How to convert bytes output to tensors

I have a model that is served using TorchServe. I’m communicating with the TorchServe server using gRPC. The final postprocess method of the custom handler defined returns a list which is converted into bytes for transfer over the network.

The post process method

JavaScript

The main issue is at the client where converting the received bytes from TorchServe to a torch Tensor is inefficiently done via ast.literal_eval

JavaScript

Using numpy.frombuffer or torch.frombuffer return the following error.

JavaScript

Using torch

JavaScript

Is there an alternative, more efficient solution of converting the received bytes into torch.Tensor?

Advertisement

Answer

One hack I’ve found that has significantly increased the performance while sending large tensors is to return a list of json.

In your handler’s postprocess function:

JavaScript

At the clients side when you receive the grpc response, decode it using json.loads

JavaScript

preds should have the output tensor

Update:

There’s an even faster method and should completely solve the bottleneck. Use tf.io.serialize_tensor from tensorflow to serialize your tensor inside postprocess

JavaScript

Decode it using tf.io.parse_tensor

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement