{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# For tips on running notebooks in Google Colab, see\n# https://pytorch.org/tutorials/beginner/colab\n%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Introduction to ONNX](intro_onnx.html) \\|\\| [Exporting a PyTorch model\nto ONNX](export_simple_model_to_onnx_tutorial.html) \\|\\| **Extending the\nONNX exporter operator support** \\|\\| [Export a model with control flow\nto ONNX](export_control_flow_model_to_onnx_tutorial.html)\n\nExtending the ONNX Exporter Operator Support\n============================================\n\n**Authors:** [Ti-Tai Wang](titaiwang@microsoft.com), [Justin\nChu](justinchu@microsoft.com)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Overview\n========\n\nThis tutorial describes how you can create ONNX implementation for\nunsupported PyTorch operators or replace existing implementation with\nyour own.\n\nWe will cover three scenarios that require extending the ONNX\nexporter\\'s operator support:\n\n- Overriding the implementation of an existing PyTorch operator\n- Using custom ONNX operators\n- Supporting a custom PyTorch operator\n\nWhat you will learn:\n\n- How to override or add support for PyTorch operators in ONNX.\n- How to integrate custom ONNX operators for specialized runtimes.\n- How to implement and translate custom PyTorch operators to ONNX.\n\nPrerequisites\n-------------\n\nBefore starting this tutorial, make sure you have completed the\nfollowing prerequisites:\n\n- `torch >= 2.6`\n- The target PyTorch operator\n- Completed the [ONNX Script\n tutorial](https://github.com/microsoft/onnxscript/blob/main/docs/tutorial/index.md)\n before proceeding\n- The implementation of the operator using [ONNX\n Script](https://github.com/microsoft/onnxscript)\n\nOverriding the implementation of an existing PyTorch operator\n=============================================================\n\nAlthough the ONNX exporter team does their best efforts to support all\nPyTorch operators, some of them might not be supported yet. In this\nsection, we will demonstrate how you can add unsupported PyTorch\noperators to the ONNX Registry.\n\n```{=html}\n
The steps to implement unsupported PyTorch operators are the same as those for replacing the implementation of an existingPyTorch operator with a custom one.Because we don't actually have an unsupported PyTorch operator to use in this tutorial, we are going to leveragethis and replace the implementation of torch.ops.aten.add.Tensor
with a custom implementation the same way we wouldif the operator was not implemented by the ONNX exporter.