{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Model Local Functions\n\nA model in ONNX may contain model-local functions. When converting an *onnxscript*\nfunction to a ModelProto, the default behavior is to include function-definitions\nfor all transitively called function-ops as model-local functions in the generated\nmodel (for which an *onnxscript* function definition has been seen). Callers can\noverride this behavior by explicitly providing the list of FunctionProtos to be\nincluded in the generated model.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, let us define an ONNXScript function that calls other ONNXScript functions.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import onnx\n\nfrom onnxscript import FLOAT, script\nfrom onnxscript import opset15 as op\nfrom onnxscript.values import Opset\n\n# A dummy opset used for model-local functions\nlocal = Opset(\"local\", 1)\n\n\n@script(local, default_opset=op)\ndef diff_square(x, y):\n diff = x - y\n return diff * diff\n\n\n@script(local)\ndef sum(z):\n return op.ReduceSum(z, keepdims=1)\n\n\n@script()\ndef l2norm(x: FLOAT[\"N\"], y: FLOAT[\"N\"]) -> FLOAT[1]: # noqa: F821\n return op.Sqrt(sum(diff_square(x, y)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's see what the generated model looks like by default:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "model = l2norm.to_model_proto()\nprint(onnx.printer.to_text(model))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's now explicitly specify which functions to include.\nFirst, generate a model with no model-local functions:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "model = l2norm.to_model_proto(functions=[])\nprint(onnx.printer.to_text(model))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, generate a model with one model-local function:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "model = l2norm.to_model_proto(functions=[sum])\nprint(onnx.printer.to_text(model))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.15" } }, "nbformat": 4, "nbformat_minor": 0 }