serve icon indicating copy to clipboard operation
serve copied to clipboard

Question about Java code accessing Custom Handlers in Python

Open Damiox opened this issue 6 years ago • 9 comments

It is not clear to me that the server is implemented in Java, but we can define custom handlers in Python. How does that communication internally works? Does it mean we can deploy a pre-trained pytorch model that can use https://github.com/huggingface/transformers/ to run inferences (Python), and working around the GIL? How is this project handling the threads for CUDA? Is it loading the model multiple times in different processes, or using Java threads to run inferences?

Damiox avatar Apr 29 '20 03:04 Damiox

@Damiox Please refer #267 and related PR and let us know if you still have questions. In the backend, it spins multiple python processes [worker] hence no issue related to GIL. Also, a GPU core is allocated per worker in round-robin fashion.

codinnvrends avatar Apr 29 '20 03:04 codinnvrends

@dhaniram-kshirsagar thanks for the feedback, and that's a nice article! I saw it yesterday after submitting this question 😓 Could you please point me out or explain me briefly how is that the server is in Java and the code is in Python? How do they interact each other? Is that through the .mar files? My schema is a bit complex. I need to pre-process the data, run the model, use the past layers that is an output of my model, and re-run the model again, and then pos-process the data. Would that process be run in a single thread, or it can be run in multiple threads? I'm interested in pytorchserve as I'm currently having issues in Python like it seems my CPU is throttling the GPU during inferences, and I cannot leverage from threads because of the GIL, and using multiprocesses is complicated with CUDA...

Damiox avatar Apr 29 '20 12:04 Damiox

Hi @Damiox , thanks for your nice words on my article :) Full disclaimer - I am not part of the pytorch/serve team, so my answer might not be as detailed as you wish and be off in some places. Hope others can chime in and correct where I might be misreading things.

Yes, the java code and python code roughly interact through the .mar file. Custom handlers get bundled into the MAR (model archive) when you run the torch-model-archiver command and are registered for the corresponding model name on the JAVA server side: https://github.com/pytorch/serve/blob/719add34dce99680ed3f9ef11c927d02a8e05fbf/frontend/server/src/main/java/org/pytorch/serve/ModelServer.java#L174

Python workers are then responsible for loading the model service using the handler code: https://github.com/pytorch/serve/blob/719add34dce99680ed3f9ef11c927d02a8e05fbf/ts/model_service_worker.py#L82

...and then communicate with the JAVA server through socket connections: https://github.com/pytorch/serve/blob/719add34dce99680ed3f9ef11c927d02a8e05fbf/ts/model_service_worker.py#L102

This is how the predict call gets triggered from the server side and the response is communicated back. Hope that helps a bit.

@dhaniram-kshirsagar Please have a look and correct anything awfully wrong you can spot ;)

MFreidank avatar Apr 29 '20 13:04 MFreidank

Hi @Damiox , thanks for your nice words on my article :) Full disclaimer - I am not part of the pytorch/serve team, so my answer might not be as detailed as you wish and be off in some places. Hope others can chime in and correct where I might be misreading things.

Yes, the java code and python code roughly interact through the .mar file. Custom handlers get bundled into the MAR (model archive) when you run the torch-model-archiver command and are registered for the corresponding model name on the JAVA server side: https://github.com/pytorch/serve/blob/719add34dce99680ed3f9ef11c927d02a8e05fbf/frontend/server/src/main/java/org/pytorch/serve/ModelServer.java#L174

Python workers are then responsible for loading the model service using the handler code: https://github.com/pytorch/serve/blob/719add34dce99680ed3f9ef11c927d02a8e05fbf/ts/model_service_worker.py#L82

...and then communicate with the JAVA server through socket connections: https://github.com/pytorch/serve/blob/719add34dce99680ed3f9ef11c927d02a8e05fbf/ts/model_service_worker.py#L102

This is how the predict call gets triggered from the server side and the response is communicated back. Hope that helps a bit.

@dhaniram-kshirsagar Please have a look and correct anything awfully wrong you can spot ;)

You got it right!

@Damiox As indicated in PR#271 or custom_handler, you just have to write a handler with pre, inference, and post-processing steps. This way, you don't have to worry about any threading aspects. You can register your model with n-number of workers [=numbers of python processes in the backend]. When you are inferencing with this model then those requests will be addressed by registered n-workers [mostly in round-robin]. Also, you can increase or decrease these workers using 'update' api.

codinnvrends avatar Apr 29 '20 13:04 codinnvrends

If we add N workers on GPU, it means we need to load the model N times in the GPU?

Damiox avatar Apr 29 '20 13:04 Damiox

You just have to tell it how many workers and it will automatically use available GPUs.

codinnvrends avatar Apr 29 '20 14:04 codinnvrends

Thanks for the feedback. I think I read based on the docs that it's 1 worker per GPU, is that right? Does that mean my pipeline will be mono-thread if I have one GPU? I'd like to confirm that...

Damiox avatar Apr 29 '20 15:04 Damiox

Thanks for the feedback. I think I read based on the docs that it's 1 worker per GPU, is that right? Does that mean my pipeline will be mono-thread if I have one GPU? I'd like to confirm that...

Yes, its worker/GPU. e.g. If you register 4 workers and 1 GPU then all of them will be pointing to same gpu. You can find more details here 'setting gpu limit'.

codinnvrends avatar Apr 29 '20 16:04 codinnvrends

Alright. Thanks for the feedback guys! Amazing how fast you answer my questions. Feel free to close the ticket if you consider so.

Damiox avatar Apr 29 '20 16:04 Damiox