Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Gunicorn with Flask to serve a Pytorch Model #2157

Closed
kalyangvs opened this issue Nov 9, 2019 · 11 comments
Closed

Use Gunicorn with Flask to serve a Pytorch Model #2157

kalyangvs opened this issue Nov 9, 2019 · 11 comments
Assignees

Comments

@kalyangvs
Copy link

kalyangvs commented Nov 9, 2019

gunicorn is used as gunicorn app:app --preload --workers 3
Preload is used to share the resources among the workers.
Set the OMP_NUM_THREADS to 2.

app.py contains the following code

from flask import Flask, jsonify
import torch  
from create_model import testtype or paste code here
app = Flask(__name__) 
model = torch.load('model.pt')

@app.route('/predict',methods = ['POST', 'GET']) 
def prediction(): 
    constant_input = torch.randn(20, 16, 50, 100)
    prediction = model(constant_input)
    return jsonify(prediction)

model.pt is created using create_model.py containing

import torch
import torch.nn as nn
import torch.nn.functional as F

class test(nn.Module):
    def __init__(self):
        super(test, self).__init__()
        self.conv1 = nn.Conv2d(16, 33, 3, stride=2)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return x

m = test()
input = torch.randn(20, 16, 50, 100)
print(m(input))
torch.save(m, 'model.pt')

But I am not able to infer it .
Instead of using a torch model if I use some numpy operation and just return its output, it is able to.

  1. Though using gunicorn app:app --preload --workers 3 --threads 2 I am able to infer. But anyone please tell me why does it differ only when threads are used .
  2. Even with -k gthreads it works fine instead of threads
    Thanks.
@benoitc
Copy link
Owner

benoitc commented Nov 10, 2019

what do you mean by "But I am not able to infer it ." does it fails?

@kalyangvs
Copy link
Author

Yes it fails.
But works in the other two cases.

@benoitc
Copy link
Owner

benoitc commented Nov 11, 2019 via email

@kalyangvs
Copy link
Author

kalyangvs commented Nov 11, 2019

image
The worker gets timed out . I tried with various high timeout values and with working cases as in threads=2 it works!.

@tilgovi
Copy link
Collaborator

tilgovi commented Nov 12, 2019

I tried with various high timeout values and with working cases as in threads=2 it works!

Does this mean that the issue can be closed?

@kalyangvs
Copy link
Author

Can you please address why this does not work with not having threads?
Else can you please elaborate on why having gthreads works?

The worker gthread is a threaded worker. It accepts connections in the main loop, accepted connections are added to the thread pool as a connection job. On keepalive connections are put back in the loop waiting for an event. If no event happen after the keep alive timeout, the connection is closed.

@tilgovi
Copy link
Collaborator

tilgovi commented Nov 12, 2019

The default, synchronous worker will be killed if it does not generate a response within the timeout (default 30s). The threaded worker can signal liveness on a separate thread, so it generally does not time out (unless there is a bug in the interpreter, or unsafe C code that leads to a deadlock, or something like this).

How long do your requestts take?

@kalyangvs
Copy link
Author

It depends on no of words present in a sentence. But for the same sentence which took around 250 ms for serving the inference. The default setting with sync workers got timedout.

@kalyangvs
Copy link
Author

kalyangvs commented Nov 20, 2019

The reload option is not reloading the app. it detects a change is made, the debug log shows workers got reinitiated, but the changes I made which caused the reload did not get reflected.
image
Even put tried using on_reload in conf_ini but it does not enter here.

@tilgovi
Copy link
Collaborator

tilgovi commented Nov 21, 2019

Reload is incompatible with preload.

@benoitc benoitc closed this as completed Nov 22, 2019
@benoitc
Copy link
Owner

benoitc commented Nov 22, 2019

closing the issue as it seems answered. Feel free to reopen it if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants