serve icon indicating copy to clipboard operation
serve copied to clipboard

Question about batch support using custom handlers

Open veronikayurchuk opened this issue 6 years ago • 7 comments

Hi! I feel like I am missing something. Could you please help me to understand how can I benefit from batch support using custom handlers?

Currently, I have a custom handler that accepts 8 ordered images from a request and returns a list of one dictionary (as required to make TS work). As an intermediate step, it preprocesses images and forms a tensor of shape [8, 3, H, W] to send it to the model. With batch size = 1, Torchserve works with a list of one dict element that is a request and returns a list of one dict as a response. As far as I understand, with batch size = N, TS gets a list of N dicts elements and expects to output a list of N elements.

The question is: Should I manually rewrite all my intermediate steps to support a list of multiple elements (requests)? If so, I would have to form tensors with different shapes [8*number_of_request_in_batch, 3, H, W]. At the same time, I used torch.jit.trace to convert my model and it doesn't support various input shapes. (only [8, 3, H, W] as it was during the convertion) What should I do in this scenario? (torch.jit.script didn't work for me). Or I overcomplicate the issue and there is a simpler solution?

Thanks for any help!

veronikayurchuk avatar Nov 08 '20 16:11 veronikayurchuk

Hi @veronikayurchuk I just filed a new issue #805 where I need to pass a variable number of images to a custom handler. It looks like you have some success doing that.

If I do: curl localhost:8080/predictions/ -F "data[][email protected]" -F "data[][email protected]" the dictionary containing the data just has a single key with the name as data[] (containing information of image2.jpg) instead of it being an array containing information about both image1.jpg and image2.jpg.

Any thoughts on how I can pass a variable number of images to the handler and how the handler can access them?

Thanks

anustupc avatar Nov 26 '20 04:11 anustupc

@veronikayurchuk: Please refer to the following comment for sending multiple requests data to your handler

https://github.com/pytorch/serve/issues/529#issuecomment-658012913

This is now also documented as part of our FAQ here:

https://github.com/pytorch/serve/blob/master/docs/FAQs.md#how-can-pass-multiple-images-in-inference-request-call-to-my-model

harshbafna avatar Dec 18 '20 04:12 harshbafna

@harshbafna thank you for your answer, but it is not the problem I am facing.

My question was about batches. Please help me to understand how to run TorshSever with batch support receiving multiple images per request? Let's say my GPU can process 128 images simultaneously. For every request, I receive 8 images. In theory, I should be able to process 16(128/8) requests simultaneously. But I don't understand how to do it with TorshServe. When there is only one image per request it is obvious, but not for multiple images.

Thanks for the help in advance!

veronikayurchuk avatar Dec 18 '20 10:12 veronikayurchuk

@harshbafna thank you for your answer, but it is not the problem I am facing.

My question was about batches. Please help me to understand how to run TorshSever with batch support receiving multiple images per request? Let's say my GPU can process 128 images simultaneously. For every request, I receive 8 images. In theory, I should be able to process 16(128/8) requests simultaneously. But I don't understand how to do it with TorshServe. When there is only one image per request it is obvious, but not for multiple images.

Thanks for the help in advance!

@veronikayurchuk: TorchServe uses two parameters, batch_size and max_batch_delay, provided at the time of model registration for batching.

  1. batch_size: This is the maximum batch size that a model is expected to handle.

  2. max_batch_delay: This is the maximum batch delay time (in milliseconds) TorchServe waits to receive a batch_size number of requests. If TorchServe doesn't receive the batch_size number of requests before this timer time's out, it sends whatever requests that were received to the model handler.

In case, your GPU can process 128 images at once and each request has 8 images, so ideal batch size should be set to 16.

Let's say TorchServe receives 16 requests (with 8 images each) in the configured max_batch_delay time; the TorchServe will create a list of 16 requests and send it to your custom handler.

You can write a pre-processing function that will loop through these 16 requests and then loop through the images in each of these requests, and create a batch of tensors that can be forwarded to the model for inference.

harshbafna avatar Dec 18 '20 11:12 harshbafna

@veronikayurchuk:

You can refer to the vision handler's preprocessing, which handles the batch requests. However, it doesn't handle multiple inputs in each request.

harshbafna avatar Dec 18 '20 11:12 harshbafna

Closing as the query has been answered and no response from OP.

harshbafna avatar Dec 28 '20 03:12 harshbafna

hi, @harshbafna ! Thanks for the provided answer.

You can write a pre-processing function that will loop through these 16 requests and then loop through the images in each of these requests, and create a batch of tensors that can be forwarded to the model for inference.

This is exactly what I have already done. The problem comes while inferencing the model. I've got my model by tracing it with @torch.jit.trace. So, as far as I understand I can't use it for batch inferencing with TS as it expects fixed batch size and it wouldn't work if I have got not a full batch. And the only alternative for me is to use @torch.jit.script.

It is rather a comment for anyone who might read it in the future, but if you have anything to add I would very much appreciate it!

veronikayurchuk avatar Dec 31 '20 13:12 veronikayurchuk