Lightning-AI / lightning-thunder

Make PyTorch models up to 40% faster! Thunder is a source to source compiler for PyTorch. It enables using different hardware executors at once; across one or thousands of GPUs.
Apache License 2.0
1.17k stars 77 forks source link

Support PyTorch custom ops when they have meta functions (also autograd) #1123

Open t-vi opened 1 month ago

t-vi commented 1 month ago

PyTorch allows to register meta functions with custom ops With that information, we should be able to apply the fallback mechanism that @kiya00 developed for PyTorch operations in thunder.

https://pytorch.org/docs/stable/library.html

There also is support for registering backward rules, which might also be of interest.

cc @apaz-cli

IvanYashchuk commented 3 weeks ago

Here's a better link showing how to use the API: https://pytorch.org/tutorials/advanced/python_custom_ops.html#python-custom-ops-tutorial.

What are the implications of including PyTorch custom ops in traces, considering semantic-changing transforms (like autocast, forward-backward, etc.) and overall system compatibility?

What should be the recommended way of registering arbitrary custom operations? Should the torch.library way always be preferred and Thunder would then need to understand PyTorch's format? When should it be recommended to add only Thunder-native registration?

t-vi commented 3 weeks ago

For the transform compatibility:

Until thunder achieves world domination, we likely want to support torch's mechanism here at least as a fallback. You would still be able to register custom implementations with the usual mechanisms.