-
Notifications
You must be signed in to change notification settings - Fork 4k
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
[FR] Saving torchscript models #2263
Comments
@hhsecond do you know what the relationship is between Torchscript & native PyTorch models & how Torchscript is commonly used? Just skimmed through the Torchscript tutorial, which suggests that Torchscript is an IR for native PyTorch models that's most-often used to score PyTorch models in non-Python contexts, rather than load them back for scoring in Python. If that's the case, then I think a variant of option 1) may make more sense, where we call both Will give this a more detailed look tomorrow, but if you have any pointers to docs/explanations on this topic that'd be welcome - thanks :) |
@smurching Your understanding is right. There are two decisions I am not sure about if we are letting
Am I making sense or did I miss anything obvious here? |
Good questions. Given that there's no way to actually execute a torchscript model in Python (Is this correct? I couldn't find anything about a Python runtime for torchscript models, only C++), I'm not sure it makes sense to introduce a Instead, for 1), could we simply persist the model in both For 2), the only code in MLflow that references the Pytorch flavor by name is within the mlflow.pytorch module itself, in order to provide a name to the flavor when saving pytorch models. Currently when we save pytorch models, we generate both:
Given that there's no "python-function" representation of a torchscript model, the Python-function flavor implemented within |
@smurching
|
Hi @hhsecond, thanks for looking into this! From looking at the history of this issue, my understanding is
If that is correct, I would propose the following api change: Does that sound reasonable to you? |
Yes, this was my initial thought too. The only concern here is for the folks who rely on getting the flavor name from the module instead of hardcoding i.e someone who calls
|
Hmm I am not sure I understand why do you want mlflow.pytorch.FLAVOR_NAME to also point to torchscript? I think if we are creating a separate torchscript flavor, the flavor name would be a separate constant. So e.g. mlflow.torchscript.FLAVOR_NAME if we decide we want a separate module for torchscript or mlflow.pytorch.TORCHSCRIPT_FLAVOR_NAME if we keep it inside of pytorch. |
I don't have any use case with the need for both flavor names. But I assumed you'd need this as the API's design when you said:
My bad. I apologize. One last thing that needed to be notified, is about this statement from your previous comment
Saving both pytorch and torchscript is not possible. Users can pass either a pytorch (native) model or torchscript model. We have two options to figure out whether it's torchscript or native pytorch model
Once we know what's the model object, we'll use the corresponding mechanism to save the model. The pyfunc model can load both pytorch and torchscript models into python. And if it's a C++ runtime, it can load torchscript in the way they want and will raise an error in case it's a native pytorch model |
I see. What would happen if you call mlflow.pytorch.load_model(path/to/torchscript/model)? I think we should also consider adding torchscript as an independent flavor, so that user would do: mlflow.torchscript.load/log/save_model(...) |
From the path, I am assuming you are passing the path to a torchscript model, It would fetch torchscript model loaded into python. So the catch here is if we have Adding torchscript as an independent flavor is the most straightforward and easy/simple solution, IMO. |
I agree! Let me know if you would like to contribute this, I'd be happy to review your PR. |
I'd love to. I'll send you a review request soon |
|
Describe the proposal
Option to save torchscript model using
torch.jit.save
instead oftorch.save
which enables the deployment toolkits to pickup the optimized torchscript model for productionMotivation
Mlflow currently doesn't distinguish between native pytorch model (subclass of
torch.nn.Module
) and torchscript model (subclass oftorch.nn.ScriptModule
). This is crucial since the production deployment system works efficiently on models saved as torchscript models (usingtorch.jit.save
)This is not possible to achieve with the current
pytorch.save_model
since we usetorch.save
internally.Proposed Changes
I have two proposals in mind after talking to @mateiz in slack
pytorch.save_model
itself with an argument user can control to tell mlflow that whether it's a torchscript model or atorch.nn.Module
model (or we can check the type of the incoming model and decide which flavor but explicit is better than implicit, I guess). The only constraint here is the samepytorch
file will now have to save two flavors ->pytorch
ortorchscript
. This sort of doesn't follow the MLFlow API design where every flavor gets onesave
,load
andlog
function.torchscript
and make a module for that. The API here becomesmlflow.torchscript
I have coded up a draft form of the second approach here but looking for suggestions before I make a PR for this to mlflow
The text was updated successfully, but these errors were encountered: