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
Pytorch2 compile (#1505) #1516
base: main
Are you sure you want to change the base?
Pytorch2 compile (#1505) #1516
Conversation
@N-Friederich got it. It looks like most CI is passing except for exports. |
Also note Python 1.7 is not supported by torch 2.0, which is why 1.7 CI is passing. |
@glenn-jocher Good point. I'll take a look. |
@glenn-jocher Ok, but then this is a pytorch recommendation. Maybe we need to build a test case explicitly for Pytorch 2? |
No don't worry, CI is fine, since 1.13.1 is applied automatically for Python 1.7 which will end of life in a few months, all over CI's use torch 2.0 and are passing in main branch now. The problem is the compiled models don't seem to export correctly. Maybe we should only compile when an inference command is run, i.e. predict, train, val? |
@glenn-jocher I think a torch._dynamo.reset() do the job in the export method. I will test this later. |
@glenn-jocher I think I misunderstood the method. I am currently considering an alternative. |
@glenn-jocher I think the problem is difficult to solve. Because what if you call |
@glenn-jocher I'll look at it again in detail tomorrow. |
Yes might be a little complicated, but I think the first step would be to only compile on inference methods (predict, val, train), and then check in export if compiled: warning with explanation and exit. |
@N-Friederich I reviewed https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html, it seems that compile ops are best used for long term actions like training and val on large datasets. Their demo code for example shows that it takes several seconds to compile a model, so it may not be suitable for simply running inference on an image. Maybe we should opt-in with an argument to let the user decide, i.e. @AyushExel @Laughing-q what do you guys think of the new torch 2.0 compile examples in the link above? |
@N-Friederich I tried to compile on train, but getting compile errors. Seems like it may not be ready yet to apply by default, probably want an opt-in argument here. |
@glenn-jocher I have never received an error. Tested it for a Titan RTX and MPS (+CPU).Can you send me your error? |
@glenn-jocher That was my idea too. But I think it's a bit "ugly". That's why I wanted to build something better. But I think that is now the only option. |
Yeah I agree with having |
@glenn-jocher @AyushExel 1) The question is how we deal with benchmark. Because pytorch models should be supported. 2.) If export is used, only a warning and skipping, or an error? In case of an error, the tests would have to be adapted accordingly. |
- Set compile=False for all export operations
β¦pile # Conflicts: # ultralytics/yolo/engine/model.py
@glenn-jocher @AyushExel I have implemented it for now without any additional flag and only turned it off for export. Do you think we also need a flag? |
@N-Friederich got it! Please fix the CI errors that are appearing. |
@glenn-jocher This is interesting. I tested it locally before my commit and got no errors. I'll take a look at it. |
@glenn-jocher The problem is that the model is saved as a pickel. This is generally not recommended and leads to the error with compiled models. I have now changed the saving as recommended here. But their are some issues with weight loading. |
@N-Friederich I haven't looked too deeply into the changes but I see you've added a manual argument on model load, which we want to avoid. We infer a model type in AutoBackend and then naturally don't run compile commands if type is not PyTorch. YOLO() should be able to load and handle all export formats without any additional arguments required. |
@glenn-jocher The problem currently is rather the serialization. This is currently not possible directly from a compiled model. I.e. there must be considered an alternative. But I am not as deep in the code as you are. |
@glenn-jocher I just changed something in the YOLO.init(...), is that what you meant by "model load"? |
@N-Friederich yes, I meant the YOLO() initialization. You added a manual argument for model load, which we should try to avoid. Instead, we should infer the model type in AutoBackend to determine whether or not to run compile commands. Additionally, YOLO() should be able to load and handle all export formats without needing additional arguments. |
@glenn-jocher Ok, I'll take a look. Thanks for the info. |
@N-Friederich sure, I'd be happy to explain ideas and concepts without using code examples. Let me know what you need help with! |
@glenn-jocher From my perspective, there are several ways to resolve the problem. One could (similar to EMA) save a separate model for the compiled model and transfer the weights to the non-compiled model before saving. This is the best option from my perspective because as of now you can't serialize a compiled model directly (except half via the state_dict). |
β¦py to ultralytics.yolo.engine.trainer.py
# Conflicts: # ultralytics/yolo/cfg/__init__.py
@glenn-jocher @AyushExel I think I have found a relatively good way. Have implemented only for train for now, should we do it for the Val and Pred as well? I would say, if then we should compile the model before. Then the compile time is not included. |
@N-Friederich That's great to hear that you have found a solution to the problem of serializing compiled models. It's important to note that the solution should be scaleable and applicable for all stages of the pipeline, which means that it should be implemented for Val and Pred as well. If the model needs to be compiled before the serialization, then we should include the compile time in the overall processing time as well. It's understandable that it took a while to familiarize yourself with the changes from YOLOv5 to YOLOv8, but taking the time to understand the code is necessary to ensure the quality of the contributions. |
@N-Friederich @glenn-jocher Hey! is there any way to push this forward? |
Hi @nodeav , |
I think if it's only done for training purposes (for now?), it might be simpler? then export the non-compiled version, because pytorch doesn't support it yet AFAIK |
@nodeav absolutely, focusing on implementing this specifically for training purposes initially would simplify the process. After training, the non-compiled version can be exported, considering that PyTorch doesn't currently fully support exporting compiled models. This iterative approach maintains simplicity and can be expanded upon as the ecosystem evolves. |
Extension accordingly #1505
@AyushExel @glenn-jocher Maybe there is a better place. We'll have to discuss that. Currently, I see a significant speedup of 1.5 on Apple M2 Pro with MPS. Sorry, I am still in the process of understanding the code and the structure of the code modules.
π€ Generated by Copilot at e984a48
Summary
ππ οΈπ¦
This pull request adds an option to compile the
YOLO
model with the experimental PyTorch 2.0 feature, which improves the performance but prevents exporting. It also updates the tests, benchmarks, and exporter to disable this feature by default, to ensure compatibility and stability.Walkthrough
compile_model
argument toYOLO
constructor and check PyTorch version and compile model with experimental feature ifTrue
(link, link, link)ValueError
inYOLO.export
method ifcompile_model
isTrue
(link)compile_model=False
toYOLO
constructor inexport
function inultralytics/yolo/engine/exporter.py
andbenchmark
function inultralytics/yolo/utils/benchmarks.py
(link, link, link)compile_model
argument toYOLO
constructor based onmode
argument inentrypoint
function inultralytics/yolo/cfg/__init__.py
(link)compile_model=False
toYOLO
constructor intests/test_python.py
(link, link, link, link)check_version
function fromultralytics/yolo/utils/checks.py
toultralytics/yolo/engine/model.py
andultralytics/yolo/engine/trainer.py
(link, link)torch
toultralytics/yolo/engine/model.py
and remove unused import fromultralytics/yolo/cfg/__init__.py
(link, link)π οΈ PR Summary
Made with β€οΈ by Ultralytics Actions
π Key Changes
compile_model
flag to allow the compilation of PyTorch models with new Torch 2.0 features.trainer.py
to use the compiled model if thecompile_model
flag is true and the PyTorch version is 2.0 or above.autobackend.py
,predictor.py
, anddefault.yaml
.π― Purpose & Impact
compile_model
option.π Summary
Leverage PyTorch 2.0's model compilation for enhanced performance in Ultralytics' AI models. π