Question about batch support using custom handlers
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!
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
@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 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!
@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.
-
batch_size: This is the maximum batch size that a model is expected to handle. -
max_batch_delay: This is the maximum batch delay time (in milliseconds) TorchServe waits to receive abatch_sizenumber of requests. If TorchServe doesn't receive thebatch_sizenumber of requests before this timer time's out, it sends whatever requests that were received to the modelhandler.
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.
@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.
Closing as the query has been answered and no response from OP.
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!