{ "cells": [ { "cell_type": "markdown", "id": "da6e9cca-6893-4273-a558-3dc18d49615e", "metadata": {}, "source": [ "# Getting started with ONNX IR 🌱\n", "The ONNX IR ships with the ONNX Script package and is available as `onnxscript.ir`.\n", "To create an IR object from ONNX file, load it as `ModelProto` and call\n", "`ir.from_proto()` or `ir.serde.deserialize_model`:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Define an example model for this example\n", "MODEL_TEXT = r\"\"\"\n", "<\n", " ir_version: 8,\n", " opset_import: [\"\" : 18],\n", " producer_name: \"pytorch\",\n", " producer_version: \"2.0.0\"\n", ">\n", "torch_jit (float[5,5,5] input_0) => (float[5,5] val_19, float[5,5] val_6) {\n", " val_1 = Constant ()\n", " val_2 = Shape (val_1)\n", " val_3 = Size (val_2)\n", " val_4 = Constant ()\n", " val_5 = Equal (val_3, val_4)\n", " val_6 = ReduceMean (input_0, val_1)\n", " val_7 = ReduceMean (input_0, val_1)\n", " val_8 = Shape (input_0)\n", " val_9 = Gather (val_8, val_1)\n", " val_10 = ReduceProd (val_9)\n", " val_11 = Sub (input_0, val_7)\n", " val_12 = Mul (val_11, val_11)\n", " val_13 = ReduceMean (val_12, val_1)\n", " val_14 = Cast (val_10)\n", " val_15 = Mul (val_13, val_14)\n", " val_16 = Constant ()\n", " val_17 = Sub (val_10, val_16)\n", " val_18 = Cast (val_17)\n", " val_19 = Div (val_15, val_18)\n", "}\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 2, "id": "cb5e7520-1aba-491b-b3e9-7d013e42d4ff", "metadata": {}, "outputs": [], "source": [ "import onnx\n", "\n", "from onnxscript import ir\n", "\n", "# Load the model as onnx.ModelProto\n", "# You can also load the model from a file using onnx.load(\"model.onnx\")\n", "model_proto = onnx.parser.parse_model(MODEL_TEXT)\n", "\n", "# Create an IR object from the model\n", "model = ir.serde.deserialize_model(model_proto)" ] }, { "cell_type": "markdown", "id": "8f02f283-93c3-4e8f-b8f4-275f360ace61", "metadata": {}, "source": [ "Now we can explore the IR object" ] }, { "cell_type": "code", "execution_count": 3, "id": "969233d0-5e7a-4554-b4bc-ea06f448dd98", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The main graph has 19 nodes.\n" ] } ], "source": [ "print(f\"The main graph has {len(model.graph)} nodes.\")" ] }, { "cell_type": "markdown", "id": "0422514a-72d3-40a0-9734-c58911ddefc9", "metadata": {}, "source": [ "All inputs" ] }, { "cell_type": "code", "execution_count": 4, "id": "7b5689d8-dd2e-468f-9a87-653e97be7cf9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[Input('input_0', type=Tensor(FLOAT), shape=[5,5,5], producer=None, index=None)]\n" ] } ], "source": [ "print(model.graph.inputs)" ] }, { "cell_type": "markdown", "id": "d299db39-08f9-4646-856d-74e9cb18ee8a", "metadata": {}, "source": [ "All outputs" ] }, { "cell_type": "code", "execution_count": 5, "id": "e3fb01aa-2ca5-4839-80c4-2c2d1b916a1c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[Value('val_19', type=Tensor(FLOAT), shape=[5,5], producer=, index=0), Value('val_6', type=Tensor(FLOAT), shape=[5,5], producer=, index=0)]\n" ] } ], "source": [ "print(model.graph.outputs)" ] }, { "cell_type": "markdown", "id": "1c52c8a2-52b4-40f3-996a-d44488e62623", "metadata": {}, "source": [ "Nodes that uses the first input" ] }, { "cell_type": "code", "execution_count": 6, "id": "c4894e97-7a8f-4f61-86dd-dd44aced02ed", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[(Node(name='', domain='', op_type='ReduceMean', inputs=(Input('input_0', type=Tensor(FLOAT), shape=[5,5,5], producer=None, index=None), Value('val_1', type=None, shape=None, producer=, index=0)), attributes=OrderedDict([('keepdims', AttrInt64('keepdims', 0)), ('noop_with_empty_axes', AttrInt64('noop_with_empty_axes', 0))]), overload='', outputs=(Value('val_6', type=Tensor(FLOAT), shape=[5,5], producer=, index=0),), version=None, doc_string=None), 0), (Node(name='', domain='', op_type='ReduceMean', inputs=(Input('input_0', type=Tensor(FLOAT), shape=[5,5,5], producer=None, index=None), Value('val_1', type=None, shape=None, producer=, index=0)), attributes=OrderedDict([('keepdims', AttrInt64('keepdims', 1)), ('noop_with_empty_axes', AttrInt64('noop_with_empty_axes', 0))]), overload='', outputs=(Value('val_7', type=None, shape=None, producer=, index=0),), version=None, doc_string=None), 0), (Node(name='', domain='', op_type='Shape', inputs=(Input('input_0', type=Tensor(FLOAT), shape=[5,5,5], producer=None, index=None),), attributes=OrderedDict([('start', AttrInt64('start', 0))]), overload='', outputs=(Value('val_8', type=None, shape=None, producer=, index=0),), version=None, doc_string=None), 0), (Node(name='', domain='', op_type='Sub', inputs=(Input('input_0', type=Tensor(FLOAT), shape=[5,5,5], producer=None, index=None), Value('val_7', type=None, shape=None, producer=, index=0)), attributes=OrderedDict(), overload='', outputs=(Value('val_11', type=None, shape=None, producer=, index=0),), version=None, doc_string=None), 0)]\n" ] } ], "source": [ "print(list(model.graph.inputs[0].uses()))" ] }, { "cell_type": "markdown", "id": "36d935b0-1910-4e7b-a2d8-57f6fa129670", "metadata": {}, "source": [ "The node that produces the last output (as the i-th output)" ] }, { "cell_type": "code", "execution_count": 7, "id": "ac16cc49-9c82-4d5e-9c77-f0fd6260929b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "%\"val_6\" ⬅️ ::ReduceMean(%\"input_0\", %\"val_1\") {keepdims=0, noop_with_empty_axes=0}\n", "0\n" ] } ], "source": [ "print(model.graph.outputs[-1].producer())\n", "print(model.graph.outputs[-1].index())" ] }, { "cell_type": "markdown", "id": "d70a097f-da71-4299-bbc4-63ad3cc7be67", "metadata": {}, "source": [ "Print the graph" ] }, { "cell_type": "code", "execution_count": 9, "id": "772e831d-8d9d-4446-81ed-e119e8f2c0d6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
graph(\n",
       "    name=torch_jit,\n",
       "    inputs=(\n",
       "        %\"input_0\"<FLOAT,[5,5,5]>\n",
       "    ),\n",
       "    outputs=(\n",
       "        %\"val_19\"<FLOAT,[5,5]>,\n",
       "        %\"val_6\"<FLOAT,[5,5]>\n",
       "    ),\n",
       ") {\n",
       "     0 |  # :anonymous_node:128897555281104\n",
       "          %\"val_1\"<?,?> ⬅️ ::Constant() {value_int=[1]}\n",
       "     1 |  # :anonymous_node:128897554321872\n",
       "          %\"val_2\"<?,?> ⬅️ ::Shape(%\"val_1\") {start=0}\n",
       "     2 |  # :anonymous_node:128895578494032\n",
       "          %\"val_3\"<?,?> ⬅️ ::Size(%\"val_2\")\n",
       "     3 |  # :anonymous_node:128895578494352\n",
       "          %\"val_4\"<?,?> ⬅️ ::Constant() {value=TensorProtoTensor<INT64,[]>(name='')}\n",
       "     4 |  # :anonymous_node:128895578494512\n",
       "          %\"val_5\"<?,?> ⬅️ ::Equal(%\"val_3\", %\"val_4\")\n",
       "     5 |  # :anonymous_node:128895578494992\n",
       "          %\"val_6\"<FLOAT,[5,5]> ⬅️ ::ReduceMean(%\"input_0\", %\"val_1\") {keepdims=0, noop_with_empty_axes=0}\n",
       "     6 |  # :anonymous_node:128895578495312\n",
       "          %\"val_7\"<?,?> ⬅️ ::ReduceMean(%\"input_0\", %\"val_1\") {keepdims=1, noop_with_empty_axes=0}\n",
       "     7 |  # :anonymous_node:128895578495472\n",
       "          %\"val_8\"<?,?> ⬅️ ::Shape(%\"input_0\") {start=0}\n",
       "     8 |  # :anonymous_node:128895578495632\n",
       "          %\"val_9\"<?,?> ⬅️ ::Gather(%\"val_8\", %\"val_1\") {axis=0}\n",
       "     9 |  # :anonymous_node:128895578495952\n",
       "          %\"val_10\"<?,?> ⬅️ ::ReduceProd(%\"val_9\") {keepdims=0, noop_with_empty_axes=0}\n",
       "    10 |  # :anonymous_node:128895578496272\n",
       "          %\"val_11\"<?,?> ⬅️ ::Sub(%\"input_0\", %\"val_7\")\n",
       "    11 |  # :anonymous_node:128895578496592\n",
       "          %\"val_12\"<?,?> ⬅️ ::Mul(%\"val_11\", %\"val_11\")\n",
       "    12 |  # :anonymous_node:128895578497072\n",
       "          %\"val_13\"<?,?> ⬅️ ::ReduceMean(%\"val_12\", %\"val_1\") {keepdims=0, noop_with_empty_axes=0}\n",
       "    13 |  # :anonymous_node:128895578497712\n",
       "          %\"val_14\"<?,?> ⬅️ ::Cast(%\"val_10\") {to=1}\n",
       "    14 |  # :anonymous_node:128895578498192\n",
       "          %\"val_15\"<?,?> ⬅️ ::Mul(%\"val_13\", %\"val_14\")\n",
       "    15 |  # :anonymous_node:128895578498672\n",
       "          %\"val_16\"<?,?> ⬅️ ::Constant() {value=TensorProtoTensor<INT64,[]>(name='')}\n",
       "    16 |  # :anonymous_node:128895578498832\n",
       "          %\"val_17\"<?,?> ⬅️ ::Sub(%\"val_10\", %\"val_16\")\n",
       "    17 |  # :anonymous_node:128895578499152\n",
       "          %\"val_18\"<?,?> ⬅️ ::Cast(%\"val_17\") {to=1}\n",
       "    18 |  # :anonymous_node:128895578499632\n",
       "          %\"val_19\"<FLOAT,[5,5]> ⬅️ ::Div(%\"val_15\", %\"val_18\")\n",
       "    return %\"val_19\"<FLOAT,[5,5]>, %\"val_6\"<FLOAT,[5,5]>\n",
       "}\n",
       "
\n" ], "text/plain": [ "\u001b[1;35mgraph\u001b[0m\u001b[1m(\u001b[0m\n", " \u001b[33mname\u001b[0m=\u001b[35mtorch_jit\u001b[0m,\n", " \u001b[33minputs\u001b[0m=\u001b[1m(\u001b[0m\n", " %\u001b[32m\"input_0\"\u001b[0m\u001b[1m<\u001b[0m\u001b[1;95mFLOAT\u001b[0m\u001b[39m,\u001b[0m\u001b[1;39m[\u001b[0m\u001b[1;36m5\u001b[0m\u001b[39m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[39m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;39m]\u001b[0m\u001b[39m>\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;39m)\u001b[0m\u001b[39m,\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[33moutputs\u001b[0m\u001b[39m=\u001b[0m\u001b[1;39m(\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_19\"\u001b[0m\u001b[39m,\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_6\"\u001b[0m\u001b[39m\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;39m)\u001b[0m\u001b[39m,\u001b[0m\n", "\u001b[1;39m)\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m{\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;36m0\u001b[0m\u001b[39m | # :anonymous_no\u001b[0m\u001b[1;92mde:1288\u001b[0m\u001b[39m97555281104\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_1\"\u001b[0m\u001b[39m ⬅️ ::\u001b[0m\u001b[1;35mConstant\u001b[0m\u001b[1;39m(\u001b[0m\u001b[1;39m)\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m{\u001b[0m\u001b[33mvalue_int\u001b[0m\u001b[39m=\u001b[0m\u001b[1;39m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;39m]\u001b[0m\u001b[1;39m}\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;36m1\u001b[0m\u001b[39m | # :anonymous_no\u001b[0m\u001b[1;92mde:1288\u001b[0m\u001b[39m97554321872\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_2\"\u001b[0m\u001b[39m ⬅️ ::\u001b[0m\u001b[1;35mShape\u001b[0m\u001b[1;39m(\u001b[0m\u001b[39m%\u001b[0m\u001b[32m\"val_1\"\u001b[0m\u001b[1;39m)\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m{\u001b[0m\u001b[33mstart\u001b[0m\u001b[39m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;39m}\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;36m2\u001b[0m\u001b[39m | # :anonymous_no\u001b[0m\u001b[1;92mde:1288\u001b[0m\u001b[39m95578494032\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_3\"\u001b[0m\u001b[39m ⬅️ ::\u001b[0m\u001b[1;35mSize\u001b[0m\u001b[1;39m(\u001b[0m\u001b[39m%\u001b[0m\u001b[32m\"val_2\"\u001b[0m\u001b[1;39m)\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;36m3\u001b[0m\u001b[39m | # :anonymous_no\u001b[0m\u001b[1;92mde:1288\u001b[0m\u001b[39m95578494352\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_4\"\u001b[0m\u001b[39m ⬅️ ::\u001b[0m\u001b[1;35mConstant\u001b[0m\u001b[1;39m(\u001b[0m\u001b[1;39m)\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m{\u001b[0m\u001b[33mvalue\u001b[0m\u001b[39m=\u001b[0m\u001b[35mTensorProtoTensor\u001b[0m\u001b[39m\u001b[0m\u001b[1;39m(\u001b[0m\u001b[33mname\u001b[0m\u001b[39m=\u001b[0m\u001b[32m''\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1;39m}\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;36m4\u001b[0m\u001b[39m | # :anonymous_no\u001b[0m\u001b[1;92mde:1288\u001b[0m\u001b[39m95578494512\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_5\"\u001b[0m\u001b[39m ⬅️ ::\u001b[0m\u001b[1;35mEqual\u001b[0m\u001b[1;39m(\u001b[0m\u001b[39m%\u001b[0m\u001b[32m\"val_3\"\u001b[0m\u001b[39m, %\u001b[0m\u001b[32m\"val_4\"\u001b[0m\u001b[1;39m)\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;36m5\u001b[0m\u001b[39m | # :anonymous_no\u001b[0m\u001b[1;92mde:1288\u001b[0m\u001b[39m95578494992\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_6\"\u001b[0m\u001b[39m ⬅️ ::\u001b[0m\u001b[1;35mReduceMean\u001b[0m\u001b[1;39m(\u001b[0m\u001b[39m%\u001b[0m\u001b[32m\"input_0\"\u001b[0m\u001b[39m, %\u001b[0m\u001b[32m\"val_1\"\u001b[0m\u001b[1;39m)\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m{\u001b[0m\u001b[33mkeepdims\u001b[0m\u001b[39m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[39m, \u001b[0m\u001b[33mnoop_with_empty_axes\u001b[0m\u001b[39m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;39m}\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;36m6\u001b[0m\u001b[39m | # :anonymous_no\u001b[0m\u001b[1;92mde:1288\u001b[0m\u001b[39m95578495312\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_7\"\u001b[0m\u001b[39m ⬅️ ::\u001b[0m\u001b[1;35mReduceMean\u001b[0m\u001b[1;39m(\u001b[0m\u001b[39m%\u001b[0m\u001b[32m\"input_0\"\u001b[0m\u001b[39m, %\u001b[0m\u001b[32m\"val_1\"\u001b[0m\u001b[1;39m)\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m{\u001b[0m\u001b[33mkeepdims\u001b[0m\u001b[39m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[39m, \u001b[0m\u001b[33mnoop_with_empty_axes\u001b[0m\u001b[39m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;39m}\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;36m7\u001b[0m\u001b[39m | # :anonymous_no\u001b[0m\u001b[1;92mde:1288\u001b[0m\u001b[39m95578495472\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_8\"\u001b[0m\u001b[39m ⬅️ ::\u001b[0m\u001b[1;35mShape\u001b[0m\u001b[1;39m(\u001b[0m\u001b[39m%\u001b[0m\u001b[32m\"input_0\"\u001b[0m\u001b[1;39m)\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m{\u001b[0m\u001b[33mstart\u001b[0m\u001b[39m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;39m}\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;36m8\u001b[0m\u001b[39m | # :anonymous_no\u001b[0m\u001b[1;92mde:1288\u001b[0m\u001b[39m95578495632\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_9\"\u001b[0m\u001b[39m ⬅️ ::\u001b[0m\u001b[1;35mGather\u001b[0m\u001b[1;39m(\u001b[0m\u001b[39m%\u001b[0m\u001b[32m\"val_8\"\u001b[0m\u001b[39m, %\u001b[0m\u001b[32m\"val_1\"\u001b[0m\u001b[1;39m)\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m{\u001b[0m\u001b[33maxis\u001b[0m\u001b[39m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;39m}\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;36m9\u001b[0m\u001b[39m | # :anonymous_no\u001b[0m\u001b[1;92mde:1288\u001b[0m\u001b[39m95578495952\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_10\"\u001b[0m\u001b[39m ⬅️ ::\u001b[0m\u001b[1;35mReduceProd\u001b[0m\u001b[1;39m(\u001b[0m\u001b[39m%\u001b[0m\u001b[32m\"val_9\"\u001b[0m\u001b[1;39m)\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m{\u001b[0m\u001b[33mkeepdims\u001b[0m\u001b[39m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[39m, \u001b[0m\u001b[33mnoop_with_empty_axes\u001b[0m\u001b[39m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;39m}\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;36m10\u001b[0m\u001b[39m | # :anonymous_no\u001b[0m\u001b[1;92mde:1288\u001b[0m\u001b[39m95578496272\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_11\"\u001b[0m\u001b[39m ⬅️ ::\u001b[0m\u001b[1;35mSub\u001b[0m\u001b[1;39m(\u001b[0m\u001b[39m%\u001b[0m\u001b[32m\"input_0\"\u001b[0m\u001b[39m, %\u001b[0m\u001b[32m\"val_7\"\u001b[0m\u001b[1;39m)\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;36m11\u001b[0m\u001b[39m | # :anonymous_no\u001b[0m\u001b[1;92mde:1288\u001b[0m\u001b[39m95578496592\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_12\"\u001b[0m\u001b[39m ⬅️ ::\u001b[0m\u001b[1;35mMul\u001b[0m\u001b[1;39m(\u001b[0m\u001b[39m%\u001b[0m\u001b[32m\"val_11\"\u001b[0m\u001b[39m, %\u001b[0m\u001b[32m\"val_11\"\u001b[0m\u001b[1;39m)\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;36m12\u001b[0m\u001b[39m | # :anonymous_no\u001b[0m\u001b[1;92mde:1288\u001b[0m\u001b[39m95578497072\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_13\"\u001b[0m\u001b[39m ⬅️ ::\u001b[0m\u001b[1;35mReduceMean\u001b[0m\u001b[1;39m(\u001b[0m\u001b[39m%\u001b[0m\u001b[32m\"val_12\"\u001b[0m\u001b[39m, %\u001b[0m\u001b[32m\"val_1\"\u001b[0m\u001b[1;39m)\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m{\u001b[0m\u001b[33mkeepdims\u001b[0m\u001b[39m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[39m, \u001b[0m\u001b[33mnoop_with_empty_axes\u001b[0m\u001b[39m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;39m}\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;36m13\u001b[0m\u001b[39m | # :anonymous_no\u001b[0m\u001b[1;92mde:1288\u001b[0m\u001b[39m95578497712\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_14\"\u001b[0m\u001b[39m ⬅️ ::\u001b[0m\u001b[1;35mCast\u001b[0m\u001b[1;39m(\u001b[0m\u001b[39m%\u001b[0m\u001b[32m\"val_10\"\u001b[0m\u001b[1;39m)\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m{\u001b[0m\u001b[33mto\u001b[0m\u001b[39m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;39m}\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;36m14\u001b[0m\u001b[39m | # :anonymous_no\u001b[0m\u001b[1;92mde:1288\u001b[0m\u001b[39m95578498192\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_15\"\u001b[0m\u001b[39m ⬅️ ::\u001b[0m\u001b[1;35mMul\u001b[0m\u001b[1;39m(\u001b[0m\u001b[39m%\u001b[0m\u001b[32m\"val_13\"\u001b[0m\u001b[39m, %\u001b[0m\u001b[32m\"val_14\"\u001b[0m\u001b[1;39m)\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;36m15\u001b[0m\u001b[39m | # :anonymous_no\u001b[0m\u001b[1;92mde:1288\u001b[0m\u001b[39m95578498672\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_16\"\u001b[0m\u001b[39m ⬅️ ::\u001b[0m\u001b[1;35mConstant\u001b[0m\u001b[1;39m(\u001b[0m\u001b[1;39m)\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m{\u001b[0m\u001b[33mvalue\u001b[0m\u001b[39m=\u001b[0m\u001b[35mTensorProtoTensor\u001b[0m\u001b[39m\u001b[0m\u001b[1;39m(\u001b[0m\u001b[33mname\u001b[0m\u001b[39m=\u001b[0m\u001b[32m''\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1;39m}\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;36m16\u001b[0m\u001b[39m | # :anonymous_no\u001b[0m\u001b[1;92mde:1288\u001b[0m\u001b[39m95578498832\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_17\"\u001b[0m\u001b[39m ⬅️ ::\u001b[0m\u001b[1;35mSub\u001b[0m\u001b[1;39m(\u001b[0m\u001b[39m%\u001b[0m\u001b[32m\"val_10\"\u001b[0m\u001b[39m, %\u001b[0m\u001b[32m\"val_16\"\u001b[0m\u001b[1;39m)\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;36m17\u001b[0m\u001b[39m | # :anonymous_no\u001b[0m\u001b[1;92mde:1288\u001b[0m\u001b[39m95578499152\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_18\"\u001b[0m\u001b[39m ⬅️ ::\u001b[0m\u001b[1;35mCast\u001b[0m\u001b[1;39m(\u001b[0m\u001b[39m%\u001b[0m\u001b[32m\"val_17\"\u001b[0m\u001b[1;39m)\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m{\u001b[0m\u001b[33mto\u001b[0m\u001b[39m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;39m}\u001b[0m\n", "\u001b[39m \u001b[0m\u001b[1;36m18\u001b[0m\u001b[39m | # :anonymous_no\u001b[0m\u001b[1;92mde:1288\u001b[0m\u001b[39m95578499632\u001b[0m\n", "\u001b[39m %\u001b[0m\u001b[32m\"val_19\"\u001b[0m\u001b[39m ⬅️ ::\u001b[0m\u001b[1;35mDiv\u001b[0m\u001b[1;39m(\u001b[0m\u001b[39m%\u001b[0m\u001b[32m\"val_15\"\u001b[0m\u001b[39m, %\u001b[0m\u001b[32m\"val_18\"\u001b[0m\u001b[1;39m)\u001b[0m\n", "\u001b[39m return %\u001b[0m\u001b[32m\"val_19\"\u001b[0m\u001b[39m, %\u001b[0m\u001b[32m\"val_6\"\u001b[0m\u001b[39m\u001b[0m\n", "\u001b[1m}\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "model.graph.display(\n", " page=False\n", ") # Set page=True to use a pager in the terminal so long outputs are scrollable" ] }, { "cell_type": "markdown", "id": "cf19aa88-2063-4fee-9dd8-5fdca1dab398", "metadata": {}, "source": [ "Convert from the IR object back to ModelProto" ] }, { "cell_type": "code", "execution_count": 10, "id": "3b146b60-602a-4cb1-a5f8-d8d22c2a6a72", "metadata": {}, "outputs": [], "source": [ "model_proto_back = ir.serde.serialize_model(model)" ] }, { "cell_type": "markdown", "id": "85a23c5b-81b8-4a73-96e0-c8553712d46f", "metadata": {}, "source": [ "## Next steps\n", "\n", "Read the introductions for a more detailed introduction of the IR\n", "(Documentation in progress 🚧)" ] } ], "metadata": { "kernelspec": { "display_name": "onnx", "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.11.9" } }, "nbformat": 4, "nbformat_minor": 2 }