XLA AOT with PyTorch

slai-nick: <p>Hi,</p>
<p>I saw PyTorch has support for XLA backend.<br>
Is it possible to do ahead-of-time compilation of PyTorch model with XLA like for Tensorflow with <a href="https://www.tensorflow.org/xla/tfcompile" rel="noopener nofollow ugc">tfcompile</a>?</p>

eqy: <p>Do you have a hard requirement for using XLA to generate code? Otherwise you might want to check out <code>torch.compile</code>: <a href="https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html" class="inline-onebox" rel="noopener nofollow ugc">torch.compile Tutorial — PyTorch Tutorials 1.13.1+cu117 documentation</a></p>

Jack_Cao: <p>The classic way of PyTorch/XLA is through a mechanism called Lazy tensor, you can checkout more details under <a href="https://github.com/pytorch/xla/blob/master/API_GUIDE.md#xla-tensor-deep-dive" class="inline-onebox" rel="noopener nofollow ugc">xla/API_GUIDE.md at master · pytorch/xla · GitHub</a>. What happened is we will still execute the python code but we will build a graph and compile/execute the graph upon a <code>mark_step</code> api call. We have a cache so you we only compile the same graph once(we need to trace graph for every step, but that can be overlapped with previous steps’s execution).</p>
<p>The alternate is our new TorchDynamo bridge(which is the torch.compile), you can check out more details in <a href="https://dev-discuss.pytorch.org/t/torchdynamo-update-10-integrating-with-pytorch-xla-for-inference-and-training/935" class="inline-onebox" rel="noopener nofollow ugc">TorchDynamo Update 10: Integrating with PyTorch/XLA for Inference and Training - compiler - PyTorch Dev Discussions</a>. The current status is that inference works great and we are improving the training bridge.</p>

slai-nick: <p>Thank you for your answers, I feel like I should have added some context.<br>
I am working on integrating a new kind of ML inference hardware accelerator with existing ML libraries and a possible path is to <a href="https://www.tensorflow.org/xla/developing_new_backend#:~:text=XLA%20provides%20an%20abstract%20interface,TensorFlow%20Op%20for%20new%20hardware" rel="noopener nofollow ugc">develop a new backend for XLA</a>.<br>
Depending on our customer application (I am thinking about edge devices here), we might want to be able to generate low overhead code.<br>
From what I read XLA has the benefit of being integrated with the 3 current major ML libraries and provide AOT compilation at least for tensorflow. If XLA AOT is only available for tensorflow models, that is something to consider.<br>
The other solution I am considering is TVM.</p>

Jack_Cao: <p>Thanks for the context, I think what you are looking for is AOTAutograd <a href="https://pytorch.org/functorch/stable/notebooks/aot_autograd_optimizations.html" class="inline-onebox" rel="noopener nofollow ugc">AOT Autograd - How to use and optimize? — functorch 1.13 documentation</a>. PyTorch/XLA has not test the integration with the AOTAutograd because we think most people will direcly use torch dynamo which integrate the AOTAutograd stack as well. It shouldn’t be too hard for PyTorch/XLA to support that through. AOT compile and execute is very natural for the XLA stack.</p>

slai-nick: <p>So if I understand correctly there’s an AOT feature in PyTorch and it might already integrate with XLA however no integration test has been done yet to confirm it?</p>

Jack_Cao: <p>I think you can play with AOT Autograd a bit(without XLA) and see if it gives the feature you want. If that’s the case you can file a feature request to <a href="https://github.com/pytorch/xla/issues" class="inline-onebox" rel="noopener nofollow ugc">Issues · pytorch/xla · GitHub</a> and we will see what does it take for us to support it.</p>

slai-nick: <p>Thank you very much for the quick help and support <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>